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"
36 #include "lib_errors.h"
38 #include "zebra/debug.h"
39 #include "zebra/rib.h"
41 #include "zebra/kernel_socket.h"
42 #include "zebra/zebra_mpls.h"
43 #include "zebra/zebra_errors.h"
45 extern struct zebra_privs_t zserv_privs
;
47 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
48 /* Adjust netmask socket length. Return value is a adjusted sin_len
50 static int sin_masklen(struct in_addr mask
)
54 struct sockaddr_in sin
;
60 len
= sizeof(struct sockaddr_in
);
62 lim
= (char *)&sin
.sin_addr
;
63 p
= lim
+ sizeof(sin
.sin_addr
);
65 while (*--p
== 0 && p
>= lim
)
69 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
72 static int kernel_rtm_add_labels(struct mpls_label_stack
*nh_label
,
73 struct sockaddr_mpls
*smpls
)
75 if (nh_label
->num_labels
> 1) {
76 flog_warn(EC_ZEBRA_MAX_LABELS_PUSH
,
77 "%s: can't push %u labels at "
78 "once (maximum is 1)",
79 __func__
, nh_label
->num_labels
);
83 memset(smpls
, 0, sizeof(*smpls
));
84 smpls
->smpls_len
= sizeof(*smpls
);
85 smpls
->smpls_family
= AF_MPLS
;
86 smpls
->smpls_label
= htonl(nh_label
->label
[0] << MPLS_LABEL_OFFSET
);
92 /* Interface between zebra message and rtm message. */
93 static int kernel_rtm_ipv4(int cmd
, const struct prefix
*p
,
94 struct route_entry
*re
)
97 struct sockaddr_in
*mask
= NULL
;
98 struct sockaddr_in sin_dest
, sin_mask
, sin_gate
;
100 struct sockaddr_mpls smpls
;
102 union sockunion
*smplsp
= NULL
;
103 struct nexthop
*nexthop
;
105 ifindex_t ifindex
= 0;
108 char prefix_buf
[PREFIX_STRLEN
];
109 enum blackhole_type bh_type
= BLACKHOLE_UNSPEC
;
111 if (IS_ZEBRA_DEBUG_RIB
)
112 prefix2str(p
, prefix_buf
, sizeof(prefix_buf
));
113 memset(&sin_dest
, 0, sizeof(struct sockaddr_in
));
114 sin_dest
.sin_family
= AF_INET
;
115 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
116 sin_dest
.sin_len
= sizeof(struct sockaddr_in
);
117 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
118 sin_dest
.sin_addr
= p
->u
.prefix4
;
120 memset(&sin_mask
, 0, sizeof(struct sockaddr_in
));
122 memset(&sin_gate
, 0, sizeof(struct sockaddr_in
));
123 sin_gate
.sin_family
= AF_INET
;
124 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
125 sin_gate
.sin_len
= sizeof(struct sockaddr_in
);
126 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
129 for (ALL_NEXTHOPS(re
->ng
, nexthop
)) {
130 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
134 char gate_buf
[INET_ADDRSTRLEN
] = "NULL";
137 * XXX We need to refrain from kernel operations in some cases,
138 * but this if statement seems overly cautious - what about
139 * other than ADD and DELETE?
141 if ((cmd
== RTM_ADD
&& NEXTHOP_IS_ACTIVE(nexthop
->flags
))
142 || (cmd
== RTM_DELETE
143 && CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))) {
144 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
145 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
) {
146 sin_gate
.sin_addr
= nexthop
->gate
.ipv4
;
149 if (nexthop
->type
== NEXTHOP_TYPE_IFINDEX
150 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
)
151 ifindex
= nexthop
->ifindex
;
152 if (nexthop
->type
== NEXTHOP_TYPE_BLACKHOLE
) {
153 struct in_addr loopback
;
154 loopback
.s_addr
= htonl(INADDR_LOOPBACK
);
155 sin_gate
.sin_addr
= loopback
;
156 bh_type
= nexthop
->bh_type
;
160 if (gate
&& p
->prefixlen
== 32)
163 masklen2ip(p
->prefixlen
, &sin_mask
.sin_addr
);
164 sin_mask
.sin_family
= AF_INET
;
165 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
167 sin_masklen(sin_mask
.sin_addr
);
168 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
173 if (nexthop
->nh_label
174 && !kernel_rtm_add_labels(nexthop
->nh_label
,
177 smplsp
= (union sockunion
*)&smpls
;
180 error
= rtm_write(cmd
, (union sockunion
*)&sin_dest
,
181 (union sockunion
*)mask
,
182 gate
? (union sockunion
*)&sin_gate
184 smplsp
, ifindex
, bh_type
, re
->metric
);
186 if (IS_ZEBRA_DEBUG_RIB
) {
189 "%s: %s: attention! gate not found for re %p",
190 __func__
, prefix_buf
, re
);
191 route_entry_dump(p
, NULL
, re
);
193 inet_ntop(AF_INET
, &sin_gate
.sin_addr
,
194 gate_buf
, INET_ADDRSTRLEN
);
198 /* We only flag nexthops as being in FIB if rtm_write()
200 case ZEBRA_ERR_NOERROR
:
202 if (IS_ZEBRA_DEBUG_RIB
)
204 "%s: %s: successfully did NH %s",
205 __func__
, prefix_buf
, gate_buf
);
208 /* The only valid case for this error is kernel's
210 * a multipath route, which is common for FreeBSD. This
212 * ignored silently, but logged as an error otherwise.
214 case ZEBRA_ERR_RTEXIST
:
218 "%s: rtm_write() returned %d for command %d",
219 __func__
, error
, cmd
);
223 /* Given that our NEXTHOP_FLAG_FIB matches real kernel
225 * normal to get any other messages in ANY case.
227 case ZEBRA_ERR_RTNOEXIST
:
228 case ZEBRA_ERR_RTUNREACH
:
232 "%s: %s: rtm_write() unexpectedly returned %d for command %s",
234 prefix2str(p
, prefix_buf
,
237 lookup_msg(rtm_type_str
, cmd
, NULL
));
240 } /* if (cmd and flags make sense) */
241 else if (IS_ZEBRA_DEBUG_RIB
)
242 zlog_debug("%s: odd command %s for flags %d", __func__
,
243 lookup_msg(rtm_type_str
, cmd
, NULL
),
245 } /* for (ALL_NEXTHOPS(...))*/
247 /* If there was no useful nexthop, then complain. */
248 if (nexthop_num
== 0 && IS_ZEBRA_DEBUG_KERNEL
)
249 zlog_debug("%s: No useful nexthops were found in RIB entry %p",
256 /* Calculate sin6_len value for netmask socket value. */
257 static int sin6_masklen(struct in6_addr mask
)
259 struct sockaddr_in6 sin6
;
263 if (IN6_IS_ADDR_UNSPECIFIED(&mask
))
266 sin6
.sin6_addr
= mask
;
267 len
= sizeof(struct sockaddr_in6
);
269 lim
= (char *)&sin6
.sin6_addr
;
270 p
= lim
+ sizeof(sin6
.sin6_addr
);
272 while (*--p
== 0 && p
>= lim
)
277 #endif /* SIN6_LEN */
279 /* Interface between zebra message and rtm message. */
280 static int kernel_rtm_ipv6(int cmd
, const struct prefix
*p
,
281 struct route_entry
*re
)
283 struct sockaddr_in6
*mask
;
284 struct sockaddr_in6 sin_dest
, sin_mask
, sin_gate
;
286 struct sockaddr_mpls smpls
;
288 union sockunion
*smplsp
= NULL
;
289 struct nexthop
*nexthop
;
291 ifindex_t ifindex
= 0;
294 enum blackhole_type bh_type
= BLACKHOLE_UNSPEC
;
296 memset(&sin_dest
, 0, sizeof(struct sockaddr_in6
));
297 sin_dest
.sin6_family
= AF_INET6
;
299 sin_dest
.sin6_len
= sizeof(struct sockaddr_in6
);
300 #endif /* SIN6_LEN */
301 sin_dest
.sin6_addr
= p
->u
.prefix6
;
303 memset(&sin_mask
, 0, sizeof(struct sockaddr_in6
));
305 memset(&sin_gate
, 0, sizeof(struct sockaddr_in6
));
306 sin_gate
.sin6_family
= AF_INET6
;
307 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
308 sin_gate
.sin6_len
= sizeof(struct sockaddr_in6
);
309 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
312 for (ALL_NEXTHOPS(re
->ng
, nexthop
)) {
313 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
318 if ((cmd
== RTM_ADD
&& NEXTHOP_IS_ACTIVE(nexthop
->flags
))
319 || (cmd
== RTM_DELETE
)) {
320 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
321 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
) {
322 sin_gate
.sin6_addr
= nexthop
->gate
.ipv6
;
325 if (nexthop
->type
== NEXTHOP_TYPE_IFINDEX
326 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
)
327 ifindex
= nexthop
->ifindex
;
329 if (nexthop
->type
== NEXTHOP_TYPE_BLACKHOLE
)
330 bh_type
= nexthop
->bh_type
;
333 /* Under kame set interface index to link local address. */
336 #define SET_IN6_LINKLOCAL_IFINDEX(a, i) \
338 (a).s6_addr[2] = ((i) >> 8) & 0xff; \
339 (a).s6_addr[3] = (i)&0xff; \
342 if (gate
&& IN6_IS_ADDR_LINKLOCAL(&sin_gate
.sin6_addr
))
343 SET_IN6_LINKLOCAL_IFINDEX(sin_gate
.sin6_addr
, ifindex
);
346 if (gate
&& p
->prefixlen
== 128)
349 masklen2ip6(p
->prefixlen
, &sin_mask
.sin6_addr
);
350 sin_mask
.sin6_family
= AF_INET6
;
352 sin_mask
.sin6_len
= sin6_masklen(sin_mask
.sin6_addr
);
353 #endif /* SIN6_LEN */
358 if (nexthop
->nh_label
359 && !kernel_rtm_add_labels(nexthop
->nh_label
, &smpls
))
361 smplsp
= (union sockunion
*)&smpls
;
364 error
= rtm_write(cmd
, (union sockunion
*)&sin_dest
,
365 (union sockunion
*)mask
,
366 gate
? (union sockunion
*)&sin_gate
: NULL
,
367 smplsp
, ifindex
, bh_type
, re
->metric
);
373 /* If there is no useful nexthop then return. */
374 if (nexthop_num
== 0) {
375 if (IS_ZEBRA_DEBUG_KERNEL
)
376 zlog_debug("kernel_rtm_ipv6(): No useful nexthop.");
383 static int kernel_rtm(int cmd
, const struct prefix
*p
, struct route_entry
*re
)
385 switch (PREFIX_FAMILY(p
)) {
387 return kernel_rtm_ipv4(cmd
, p
, re
);
389 return kernel_rtm_ipv6(cmd
, p
, re
);
394 enum zebra_dplane_result
kernel_route_rib(struct route_node
*rn
,
395 const struct prefix
*p
,
396 const struct prefix
*src_p
,
397 struct route_entry
*old
,
398 struct route_entry
*new)
402 if (src_p
&& src_p
->prefixlen
) {
403 flog_warn(EC_ZEBRA_UNSUPPORTED_V6_SRCDEST
,
404 "%s: IPv6 sourcedest routes unsupported!", __func__
);
405 return ZEBRA_DPLANE_REQUEST_FAILURE
;
408 frr_elevate_privs(&zserv_privs
) {
411 route
|= kernel_rtm(RTM_DELETE
, p
, old
);
414 route
|= kernel_rtm(RTM_ADD
, p
, new);
419 kernel_route_rib_pass_fail(
421 (!route
) ? ZEBRA_DPLANE_INSTALL_SUCCESS
422 : ZEBRA_DPLANE_INSTALL_FAILURE
);
424 kernel_route_rib_pass_fail(rn
, p
, old
,
426 ? ZEBRA_DPLANE_DELETE_SUCCESS
427 : ZEBRA_DPLANE_DELETE_FAILURE
);
430 return ZEBRA_DPLANE_REQUEST_SUCCESS
;
433 int kernel_neigh_update(int add
, int ifindex
, uint32_t addr
, char *lla
,
434 int llalen
, ns_id_t ns_id
)
440 extern int kernel_get_ipmr_sg_stats(struct zebra_vrf
*zvrf
, void *mroute
)
445 int kernel_add_vtep(vni_t vni
, struct interface
*ifp
, struct in_addr
*vtep_ip
)
450 int kernel_del_vtep(vni_t vni
, struct interface
*ifp
, struct in_addr
*vtep_ip
)
455 int kernel_add_mac(struct interface
*ifp
, vlanid_t vid
, struct ethaddr
*mac
,
456 struct in_addr vtep_ip
, bool sticky
)
461 int kernel_del_mac(struct interface
*ifp
, vlanid_t vid
, struct ethaddr
*mac
,
462 struct in_addr vtep_ip
)
467 int kernel_add_neigh(struct interface
*ifp
, struct ipaddr
*ip
,
468 struct ethaddr
*mac
, uint8_t flags
)
473 int kernel_del_neigh(struct interface
*ifp
, struct ipaddr
*ip
)
478 extern int kernel_interface_set_master(struct interface
*master
,
479 struct interface
*slave
)
484 uint32_t kernel_get_speed(struct interface
*ifp
)
489 #endif /* !HAVE_NETLINK */