2 * Kernel routing table updates by routing socket.
3 * Copyright (C) 1997, 98 Kunihiro Ishiguro
5 * This file is part of GNU Zebra.
7 * GNU Zebra is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
27 #include <netmpls/mpls.h>
32 #include "sockunion.h"
37 #include "zebra/debug.h"
38 #include "zebra/rib.h"
40 #include "zebra/kernel_socket.h"
41 #include "zebra/zebra_mpls.h"
43 extern struct zebra_privs_t zserv_privs
;
45 /* kernel socket export */
46 extern int rtm_write(int message
, union sockunion
*dest
, union sockunion
*mask
,
47 union sockunion
*gate
, union sockunion
*mpls
,
48 unsigned int index
, int zebra_flags
, int metric
);
50 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
51 /* Adjust netmask socket length. Return value is a adjusted sin_len
53 static int sin_masklen(struct in_addr mask
)
57 struct sockaddr_in sin
;
63 len
= sizeof(struct sockaddr_in
);
65 lim
= (char *)&sin
.sin_addr
;
66 p
= lim
+ sizeof(sin
.sin_addr
);
68 while (*--p
== 0 && p
>= lim
)
72 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
74 /* Interface between zebra message and rtm message. */
75 static int kernel_rtm_ipv4(int cmd
, struct prefix
*p
, struct route_entry
*re
)
78 struct sockaddr_in
*mask
= NULL
;
79 struct sockaddr_in sin_dest
, sin_mask
, sin_gate
;
81 struct sockaddr_mpls smpls
;
83 union sockunion
*smplsp
= NULL
;
84 struct nexthop
*nexthop
;
86 ifindex_t ifindex
= 0;
89 char prefix_buf
[PREFIX_STRLEN
];
91 if (IS_ZEBRA_DEBUG_RIB
)
92 prefix2str(p
, prefix_buf
, sizeof(prefix_buf
));
93 memset(&sin_dest
, 0, sizeof(struct sockaddr_in
));
94 sin_dest
.sin_family
= AF_INET
;
95 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
96 sin_dest
.sin_len
= sizeof(struct sockaddr_in
);
97 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
98 sin_dest
.sin_addr
= p
->u
.prefix4
;
100 memset(&sin_mask
, 0, sizeof(struct sockaddr_in
));
102 memset(&sin_gate
, 0, sizeof(struct sockaddr_in
));
103 sin_gate
.sin_family
= AF_INET
;
104 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
105 sin_gate
.sin_len
= sizeof(struct sockaddr_in
);
106 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
109 for (ALL_NEXTHOPS(re
->nexthop
, nexthop
)) {
110 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
114 char gate_buf
[INET_ADDRSTRLEN
] = "NULL";
117 * XXX We need to refrain from kernel operations in some cases,
118 * but this if statement seems overly cautious - what about
119 * other than ADD and DELETE?
122 && CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
123 || (cmd
== RTM_DELETE
124 && CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))) {
125 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
126 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
) {
127 sin_gate
.sin_addr
= nexthop
->gate
.ipv4
;
130 if (nexthop
->type
== NEXTHOP_TYPE_IFINDEX
131 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
)
132 ifindex
= nexthop
->ifindex
;
133 if (nexthop
->type
== NEXTHOP_TYPE_BLACKHOLE
) {
134 struct in_addr loopback
;
135 loopback
.s_addr
= htonl(INADDR_LOOPBACK
);
136 sin_gate
.sin_addr
= loopback
;
140 if (gate
&& p
->prefixlen
== 32)
143 masklen2ip(p
->prefixlen
, &sin_mask
.sin_addr
);
144 sin_mask
.sin_family
= AF_INET
;
145 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
147 sin_masklen(sin_mask
.sin_addr
);
148 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
153 if (nexthop
->nh_label
) {
154 memset(&smpls
, 0, sizeof(smpls
));
155 smpls
.smpls_len
= sizeof(smpls
);
156 smpls
.smpls_family
= AF_MPLS
;
158 htonl(nexthop
->nh_label
->label
[0]
159 << MPLS_LABEL_OFFSET
);
160 smplsp
= (union sockunion
*)&smpls
;
165 cmd
, (union sockunion
*)&sin_dest
,
166 (union sockunion
*)mask
,
167 gate
? (union sockunion
*)&sin_gate
: NULL
,
168 smplsp
, ifindex
, re
->flags
, re
->metric
);
170 if (IS_ZEBRA_DEBUG_RIB
) {
173 "%s: %s: attention! gate not found for re %p",
174 __func__
, prefix_buf
, re
);
175 route_entry_dump(p
, NULL
, re
);
177 inet_ntop(AF_INET
, &sin_gate
.sin_addr
,
178 gate_buf
, INET_ADDRSTRLEN
);
182 /* We only flag nexthops as being in FIB if rtm_write()
184 case ZEBRA_ERR_NOERROR
:
186 if (IS_ZEBRA_DEBUG_RIB
)
188 "%s: %s: successfully did NH %s",
189 __func__
, prefix_buf
, gate_buf
);
191 SET_FLAG(nexthop
->flags
,
195 /* The only valid case for this error is kernel's
197 * a multipath route, which is common for FreeBSD. This
199 * ignored silently, but logged as an error otherwise.
201 case ZEBRA_ERR_RTEXIST
:
204 "%s: rtm_write() returned %d for command %d",
205 __func__
, error
, cmd
);
209 /* Given that our NEXTHOP_FLAG_FIB matches real kernel
211 * normal to get any other messages in ANY case.
213 case ZEBRA_ERR_RTNOEXIST
:
214 case ZEBRA_ERR_RTUNREACH
:
217 "%s: %s: rtm_write() unexpectedly returned %d for command %s",
219 prefix2str(p
, prefix_buf
,
222 lookup_msg(rtm_type_str
, cmd
, NULL
));
225 } /* if (cmd and flags make sense) */
226 else if (IS_ZEBRA_DEBUG_RIB
)
227 zlog_debug("%s: odd command %s for flags %d", __func__
,
228 lookup_msg(rtm_type_str
, cmd
, NULL
),
230 } /* for (ALL_NEXTHOPS(...))*/
232 /* If there was no useful nexthop, then complain. */
233 if (nexthop_num
== 0 && IS_ZEBRA_DEBUG_KERNEL
)
234 zlog_debug("%s: No useful nexthops were found in RIB entry %p",
241 /* Calculate sin6_len value for netmask socket value. */
242 static int sin6_masklen(struct in6_addr mask
)
244 struct sockaddr_in6 sin6
;
248 if (IN6_IS_ADDR_UNSPECIFIED(&mask
))
251 sin6
.sin6_addr
= mask
;
252 len
= sizeof(struct sockaddr_in6
);
254 lim
= (char *)&sin6
.sin6_addr
;
255 p
= lim
+ sizeof(sin6
.sin6_addr
);
257 while (*--p
== 0 && p
>= lim
)
262 #endif /* SIN6_LEN */
264 /* Interface between zebra message and rtm message. */
265 static int kernel_rtm_ipv6(int cmd
, struct prefix
*p
, struct route_entry
*re
)
267 struct sockaddr_in6
*mask
;
268 struct sockaddr_in6 sin_dest
, sin_mask
, sin_gate
;
269 struct nexthop
*nexthop
;
271 ifindex_t ifindex
= 0;
275 memset(&sin_dest
, 0, sizeof(struct sockaddr_in6
));
276 sin_dest
.sin6_family
= AF_INET6
;
278 sin_dest
.sin6_len
= sizeof(struct sockaddr_in6
);
279 #endif /* SIN6_LEN */
280 sin_dest
.sin6_addr
= p
->u
.prefix6
;
282 memset(&sin_mask
, 0, sizeof(struct sockaddr_in6
));
284 memset(&sin_gate
, 0, sizeof(struct sockaddr_in6
));
285 sin_gate
.sin6_family
= AF_INET6
;
286 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
287 sin_gate
.sin6_len
= sizeof(struct sockaddr_in6
);
288 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
291 for (ALL_NEXTHOPS(re
->nexthop
, nexthop
)) {
292 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
298 && CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
299 || (cmd
== RTM_DELETE
301 && CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
)
304 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
305 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
) {
306 sin_gate
.sin6_addr
= nexthop
->gate
.ipv6
;
309 if (nexthop
->type
== NEXTHOP_TYPE_IFINDEX
310 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
)
311 ifindex
= nexthop
->ifindex
;
314 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
317 /* Under kame set interface index to link local address. */
320 #define SET_IN6_LINKLOCAL_IFINDEX(a, i) \
322 (a).s6_addr[2] = ((i) >> 8) & 0xff; \
323 (a).s6_addr[3] = (i)&0xff; \
326 if (gate
&& IN6_IS_ADDR_LINKLOCAL(&sin_gate
.sin6_addr
))
327 SET_IN6_LINKLOCAL_IFINDEX(sin_gate
.sin6_addr
, ifindex
);
330 if (gate
&& p
->prefixlen
== 128)
333 masklen2ip6(p
->prefixlen
, &sin_mask
.sin6_addr
);
334 sin_mask
.sin6_family
= AF_INET6
;
336 sin_mask
.sin6_len
= sin6_masklen(sin_mask
.sin6_addr
);
337 #endif /* SIN6_LEN */
341 error
= rtm_write(cmd
, (union sockunion
*)&sin_dest
,
342 (union sockunion
*)mask
,
343 gate
? (union sockunion
*)&sin_gate
: NULL
,
344 NULL
, ifindex
, re
->flags
, re
->metric
);
349 zlog_info ("kernel_rtm_ipv6(): nexthop %d add error=%d.",
359 /* If there is no useful nexthop then return. */
360 if (nexthop_num
== 0) {
361 if (IS_ZEBRA_DEBUG_KERNEL
)
362 zlog_debug("kernel_rtm_ipv6(): No useful nexthop.");
369 static int kernel_rtm(int cmd
, struct prefix
*p
, struct route_entry
*re
)
371 switch (PREFIX_FAMILY(p
)) {
373 return kernel_rtm_ipv4(cmd
, p
, re
);
375 return kernel_rtm_ipv6(cmd
, p
, re
);
380 int kernel_route_rib(struct prefix
*p
, struct prefix
*src_p
,
381 struct route_entry
*old
, struct route_entry
*new)
385 if (src_p
&& src_p
->prefixlen
) {
386 zlog_err("route add: IPv6 sourcedest routes unsupported!");
390 if (zserv_privs
.change(ZPRIVS_RAISE
))
391 zlog_err("Can't raise privileges");
394 route
|= kernel_rtm(RTM_DELETE
, p
, old
);
397 route
|= kernel_rtm(RTM_ADD
, p
, new);
399 if (zserv_privs
.change(ZPRIVS_LOWER
))
400 zlog_err("Can't lower privileges");
405 int kernel_neigh_update(int add
, int ifindex
, uint32_t addr
, char *lla
,
412 extern int kernel_get_ipmr_sg_stats(void *mroute
)
417 int kernel_add_vtep(vni_t vni
, struct interface
*ifp
, struct in_addr
*vtep_ip
)
422 int kernel_del_vtep(vni_t vni
, struct interface
*ifp
, struct in_addr
*vtep_ip
)
427 int kernel_add_mac(struct interface
*ifp
, vlanid_t vid
, struct ethaddr
*mac
,
428 struct in_addr vtep_ip
, u_char sticky
)
433 int kernel_del_mac(struct interface
*ifp
, vlanid_t vid
, struct ethaddr
*mac
,
434 struct in_addr vtep_ip
, int local
)
439 int kernel_add_neigh(struct interface
*ifp
, struct ipaddr
*ip
,
445 int kernel_del_neigh(struct interface
*ifp
, struct ipaddr
*ip
)
450 #endif /* !HAVE_NETLINK */