1 /* Kernel communication using routing socket.
2 * Copyright (C) 1999 Kunihiro Ishiguro
4 * This file is part of GNU Zebra.
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 #include <net/if_types.h>
27 #include <netmpls/mpls.h>
32 #include "sockunion.h"
33 #include "connected.h"
35 #include "zebra_memory.h"
42 #include "lib_errors.h"
45 #include "zebra/interface.h"
46 #include "zebra/zserv.h"
47 #include "zebra/debug.h"
48 #include "zebra/kernel_socket.h"
49 #include "zebra/rib.h"
50 #include "zebra/zebra_errors.h"
51 #include "zebra/zebra_ptm.h"
53 extern struct zebra_privs_t zserv_privs
;
56 * Historically, the BSD routing socket has aligned data following a
57 * struct sockaddr to sizeof(long), which was 4 bytes on some
58 * platforms, and 8 bytes on others. NetBSD 6 changed the routing
59 * socket to align to sizeof(uint64_t), which is 8 bytes. OS X
60 * appears to align to sizeof(int), which is 4 bytes.
62 * Alignment of zero-sized sockaddrs is nonsensical, but historically
63 * BSD defines RT_ROUNDUP(0) to be the alignment interval (rather than
64 * 0). We follow this practice without questioning it, but it is a
65 * bug if quagga calls ROUNDUP with 0.
68 #define ROUNDUP_TYPE int
70 #define ROUNDUP_TYPE long
74 * Because of these varying conventions, the only sane approach is for
75 * the <net/route.h> header to define some flavor of ROUNDUP macro.
78 /* OS X (Xcode as of 2014-12) is known not to define RT_ROUNDUP */
79 #if defined(RT_ROUNDUP)
80 #define ROUNDUP(a) RT_ROUNDUP(a)
81 #endif /* defined(RT_ROUNDUP) */
84 /* Solaris has struct sockaddr_in[6] definitions at 16 / 32 bytes size,
85 * so the whole concept doesn't really apply. */
86 #define ROUNDUP(a) (a)
90 * If ROUNDUP has not yet been defined in terms of platform-provided
91 * defines, attempt to cope with heuristics.
96 * If you're porting to a platform that changed RT_ROUNDUP but doesn't
97 * have it in its headers, this will break rather obviously and you'll
98 * have to fix it here.
101 ((a) > 0 ? (1 + (((a)-1) | (sizeof(ROUNDUP_TYPE) - 1))) \
102 : sizeof(ROUNDUP_TYPE))
104 #endif /* defined(ROUNDUP) */
108 /* SAROUNDUP is the only thing we need, and SA_SIZE provides that */
109 #define SAROUNDUP(a) SA_SIZE(a)
112 * Given a pointer (sockaddr or void *), return the number of bytes
113 * taken up by the sockaddr and any padding needed for alignment.
115 #if defined(HAVE_STRUCT_SOCKADDR_SA_LEN)
116 #define SAROUNDUP(X) ROUNDUP(((struct sockaddr *)(X))->sa_len)
119 * One would hope all fixed-size structure definitions are aligned,
120 * but round them up nonetheless.
122 #define SAROUNDUP(X) \
123 (((struct sockaddr *)(X))->sa_family == AF_INET \
124 ? ROUNDUP(sizeof(struct sockaddr_in)) \
125 : (((struct sockaddr *)(X))->sa_family == AF_INET6 \
126 ? ROUNDUP(sizeof(struct sockaddr_in6)) \
127 : (((struct sockaddr *)(X))->sa_family == AF_LINK \
128 ? ROUNDUP(sizeof(struct sockaddr_dl)) \
129 : sizeof(struct sockaddr))))
130 #endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
132 #endif /* !SA_SIZE */
134 /* Routing socket message types. */
135 const struct message rtm_type_str
[] = {{RTM_ADD
, "RTM_ADD"},
136 {RTM_DELETE
, "RTM_DELETE"},
137 {RTM_CHANGE
, "RTM_CHANGE"},
138 {RTM_GET
, "RTM_GET"},
139 {RTM_LOSING
, "RTM_LOSING"},
140 {RTM_REDIRECT
, "RTM_REDIRECT"},
141 {RTM_MISS
, "RTM_MISS"},
143 {RTM_LOCK
, "RTM_LOCK"},
144 #endif /* RTM_LOCK */
146 {RTM_OLDADD
, "RTM_OLDADD"},
147 #endif /* RTM_OLDADD */
149 {RTM_OLDDEL
, "RTM_OLDDEL"},
150 #endif /* RTM_OLDDEL */
151 {RTM_RESOLVE
, "RTM_RESOLVE"},
152 {RTM_NEWADDR
, "RTM_NEWADDR"},
153 {RTM_DELADDR
, "RTM_DELADDR"},
154 {RTM_IFINFO
, "RTM_IFINFO"},
156 {RTM_OIFINFO
, "RTM_OIFINFO"},
157 #endif /* RTM_OIFINFO */
159 {RTM_NEWMADDR
, "RTM_NEWMADDR"},
160 #endif /* RTM_NEWMADDR */
162 {RTM_DELMADDR
, "RTM_DELMADDR"},
163 #endif /* RTM_DELMADDR */
164 #ifdef RTM_IFANNOUNCE
165 {RTM_IFANNOUNCE
, "RTM_IFANNOUNCE"},
166 #endif /* RTM_IFANNOUNCE */
169 static const struct message rtm_flag_str
[] = {{RTF_UP
, "UP"},
170 {RTF_GATEWAY
, "GATEWAY"},
172 {RTF_REJECT
, "REJECT"},
173 {RTF_DYNAMIC
, "DYNAMIC"},
174 {RTF_MODIFIED
, "MODIFIED"},
178 #endif /* RTF_MASK */
180 {RTF_CLONING
, "CLONING"},
181 #endif /* RTF_CLONING */
183 {RTF_XRESOLVE
, "XRESOLVE"},
184 #endif /* RTF_XRESOLVE */
186 {RTF_LLINFO
, "LLINFO"},
187 #endif /* RTF_LLINFO */
188 {RTF_STATIC
, "STATIC"},
189 {RTF_BLACKHOLE
, "BLACKHOLE"},
191 {RTF_PRIVATE
, "PRIVATE"},
192 #endif /* RTF_PRIVATE */
193 {RTF_PROTO1
, "PROTO1"},
194 {RTF_PROTO2
, "PROTO2"},
196 {RTF_PRCLONING
, "PRCLONING"},
197 #endif /* RTF_PRCLONING */
199 {RTF_WASCLONED
, "WASCLONED"},
200 #endif /* RTF_WASCLONED */
202 {RTF_PROTO3
, "PROTO3"},
203 #endif /* RTF_PROTO3 */
205 {RTF_PINNED
, "PINNED"},
206 #endif /* RTF_PINNED */
208 {RTF_LOCAL
, "LOCAL"},
209 #endif /* RTF_LOCAL */
211 {RTF_BROADCAST
, "BROADCAST"},
212 #endif /* RTF_BROADCAST */
214 {RTF_MULTICAST
, "MULTICAST"},
215 #endif /* RTF_MULTICAST */
217 {RTF_MULTIRT
, "MULTIRT"},
218 #endif /* RTF_MULTIRT */
220 {RTF_SETSRC
, "SETSRC"},
221 #endif /* RTF_SETSRC */
224 /* Kernel routing update socket. */
225 int routing_sock
= -1;
227 /* Kernel dataplane routing update socket, used in the dataplane pthread
230 int dplane_routing_sock
= -1;
232 /* Yes I'm checking ugly routing socket behavior. */
235 size_t _rta_get(caddr_t sap
, void *destp
, size_t destlen
, bool checkaf
);
236 size_t rta_get(caddr_t sap
, void *dest
, size_t destlen
);
237 size_t rta_getattr(caddr_t sap
, void *destp
, size_t destlen
);
238 size_t rta_getsdlname(caddr_t sap
, void *dest
, short *destlen
);
239 const char *rtatostr(unsigned int flags
, char *buf
, size_t buflen
);
241 /* Supported address family check. */
242 static inline int af_check(int family
)
244 if (family
== AF_INET
)
246 if (family
== AF_INET6
)
251 size_t _rta_get(caddr_t sap
, void *destp
, size_t destlen
, bool checkaf
)
253 struct sockaddr
*sa
= (struct sockaddr
*)sap
;
254 struct sockaddr_dl
*sdl
;
255 uint8_t *dest
= destp
;
256 size_t tlen
, copylen
;
258 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
259 copylen
= sa
->sa_len
;
260 tlen
= (copylen
== 0) ? sizeof(ROUNDUP_TYPE
) : ROUNDUP(copylen
);
261 #else /* !HAVE_STRUCT_SOCKADDR_SA_LEN */
262 copylen
= tlen
= SAROUNDUP(sap
);
263 #endif /* !HAVE_STRUCT_SOCKADDR_SA_LEN */
265 if (copylen
> 0 && dest
!= NULL
) {
266 if (checkaf
&& af_check(sa
->sa_family
) == 0)
269 * Handle sockaddr_dl corner case:
270 * RTA_NETMASK might be AF_LINK, but it doesn't anything
271 * relevant (e.g. zeroed out fields). Check for this
272 * case and avoid warning log message.
274 if (sa
->sa_family
== AF_LINK
) {
275 sdl
= (struct sockaddr_dl
*)sa
;
276 if (sdl
->sdl_index
== 0 || sdl
->sdl_nlen
== 0)
280 if (copylen
> destlen
) {
281 zlog_warn("%s: destination buffer too small (%lu vs %lu)",
282 __func__
, copylen
, destlen
);
283 memcpy(dest
, sap
, destlen
);
285 memcpy(dest
, sap
, copylen
);
291 size_t rta_get(caddr_t sap
, void *destp
, size_t destlen
)
293 return _rta_get(sap
, destp
, destlen
, true);
296 size_t rta_getattr(caddr_t sap
, void *destp
, size_t destlen
)
298 return _rta_get(sap
, destp
, destlen
, false);
301 size_t rta_getsdlname(caddr_t sap
, void *destp
, short *destlen
)
303 struct sockaddr_dl
*sdl
= (struct sockaddr_dl
*)sap
;
304 struct sockaddr
*sa
= (struct sockaddr
*)sap
;
305 uint8_t *dest
= destp
;
306 size_t tlen
, copylen
;
308 copylen
= sdl
->sdl_nlen
;
309 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
310 tlen
= (sa
->sa_len
== 0) ? sizeof(ROUNDUP_TYPE
) : ROUNDUP(sa
->sa_len
);
311 #else /* !HAVE_STRUCT_SOCKADDR_SA_LEN */
312 tlen
= SAROUNDUP(sap
);
313 #endif /* !HAVE_STRUCT_SOCKADDR_SA_LEN */
315 if (copylen
> 0 && dest
!= NULL
&& sdl
->sdl_family
== AF_LINK
) {
316 if (copylen
> IFNAMSIZ
) {
317 zlog_warn("%s: destination buffer too small (%lu vs %d)",
318 __func__
, copylen
, IFNAMSIZ
);
319 memcpy(dest
, sdl
->sdl_data
, IFNAMSIZ
);
323 memcpy(dest
, sdl
->sdl_data
, copylen
);
333 const char *rtatostr(unsigned int flags
, char *buf
, size_t buflen
)
335 const char *flagstr
, *bufstart
;
339 /* Hold the pointer to the buffer beginning. */
342 for (bit
= 1; bit
; bit
<<= 1) {
343 if ((flags
& bit
) == 0)
360 #endif /* RTA_GENMASK */
371 #endif /* RTA_AUTHOR */
384 #endif /* RTA_SRCMASK */
389 #endif /* RTA_LABEL */
392 snprintf(ustr
, sizeof(ustr
), "0x%x", bit
);
397 wlen
= snprintf(buf
, buflen
, "%s,", flagstr
);
402 /* Check for empty buffer. */
406 /* Remove the last comma. */
412 /* Dump routing table flag for debug purpose. */
413 static void rtm_flag_dump(int flag
)
415 const struct message
*mes
;
416 static char buf
[BUFSIZ
];
419 for (mes
= rtm_flag_str
; mes
->key
!= 0; mes
++) {
420 if (mes
->key
& flag
) {
421 strlcat(buf
, mes
->str
, BUFSIZ
);
422 strlcat(buf
, " ", BUFSIZ
);
425 zlog_debug("Kernel: %s", buf
);
428 #ifdef RTM_IFANNOUNCE
429 /* Interface adding function */
430 static int ifan_read(struct if_announcemsghdr
*ifan
)
432 struct interface
*ifp
;
434 ifp
= if_lookup_by_index(ifan
->ifan_index
, VRF_DEFAULT
);
437 assert((ifp
->ifindex
== ifan
->ifan_index
)
438 || (ifp
->ifindex
== IFINDEX_INTERNAL
));
440 if ((ifp
== NULL
) || ((ifp
->ifindex
== IFINDEX_INTERNAL
)
441 && (ifan
->ifan_what
== IFAN_ARRIVAL
))) {
442 if (IS_ZEBRA_DEBUG_KERNEL
)
444 "%s: creating interface for ifindex %d, name %s",
445 __func__
, ifan
->ifan_index
, ifan
->ifan_name
);
447 /* Create Interface */
448 ifp
= if_get_by_name(ifan
->ifan_name
, VRF_DEFAULT
);
449 if_set_index(ifp
, ifan
->ifan_index
);
453 } else if (ifp
!= NULL
&& ifan
->ifan_what
== IFAN_DEPARTURE
)
454 if_delete_update(ifp
);
460 if (IS_ZEBRA_DEBUG_KERNEL
)
461 zlog_debug("%s: interface %s index %d", __func__
,
462 ifan
->ifan_name
, ifan
->ifan_index
);
466 #endif /* RTM_IFANNOUNCE */
468 #ifdef HAVE_BSD_IFI_LINK_STATE
469 /* BSD link detect translation */
470 static void bsd_linkdetect_translate(struct if_msghdr
*ifm
)
472 if ((ifm
->ifm_data
.ifi_link_state
>= LINK_STATE_UP
)
473 || (ifm
->ifm_data
.ifi_link_state
== LINK_STATE_UNKNOWN
))
474 SET_FLAG(ifm
->ifm_flags
, IFF_RUNNING
);
476 UNSET_FLAG(ifm
->ifm_flags
, IFF_RUNNING
);
478 #endif /* HAVE_BSD_IFI_LINK_STATE */
480 static enum zebra_link_type
sdl_to_zebra_link_type(unsigned int sdlt
)
484 return ZEBRA_LLT_ETHER
;
486 return ZEBRA_LLT_X25
;
488 return ZEBRA_LLT_FDDI
;
490 return ZEBRA_LLT_PPP
;
492 return ZEBRA_LLT_LOOPBACK
;
494 return ZEBRA_LLT_SLIP
;
496 return ZEBRA_LLT_ARCNET
;
498 return ZEBRA_LLT_ATM
;
500 return ZEBRA_LLT_LOCALTLK
;
502 return ZEBRA_LLT_HIPPI
;
505 return ZEBRA_LLT_IEEE1394
;
509 return ZEBRA_LLT_UNKNOWN
;
514 * Handle struct if_msghdr obtained from reading routing socket or
515 * sysctl (from interface_list). There may or may not be sockaddrs
516 * present after the header.
518 int ifm_read(struct if_msghdr
*ifm
)
520 struct interface
*ifp
= NULL
;
521 struct sockaddr_dl
*sdl
;
522 char ifname
[IFNAMSIZ
];
528 /* terminate ifname at head (for strnlen) and tail (for safety) */
529 ifname
[IFNAMSIZ
- 1] = '\0';
531 /* paranoia: sanity check structure */
532 if (ifm
->ifm_msglen
< sizeof(struct if_msghdr
)) {
533 flog_err(EC_ZEBRA_NETLINK_LENGTH_ERROR
,
534 "ifm_read: ifm->ifm_msglen %d too short\n",
540 * Check for a sockaddr_dl following the message. First, point to
541 * where a socakddr might be if one follows the message.
543 cp
= (void *)(ifm
+ 1);
547 * XXX This behavior should be narrowed to only the kernel versions
548 * for which the structures returned do not match the headers.
550 * if_msghdr_t on 64 bit kernels in Solaris 9 and earlier versions
551 * is 12 bytes larger than the 32 bit version.
553 if (((struct sockaddr
*)cp
)->sa_family
== AF_UNSPEC
)
557 /* Look up for RTA_IFP and skip others. */
558 for (maskbit
= 1; maskbit
; maskbit
<<= 1) {
559 if ((maskbit
& ifm
->ifm_addrs
) == 0)
561 if (maskbit
!= RTA_IFP
) {
562 cp
+= rta_get(cp
, NULL
, 0);
566 /* Save the pointer to the structure. */
567 sdl
= (struct sockaddr_dl
*)cp
;
568 cp
+= rta_getsdlname(cp
, ifname
, &ifnlen
);
571 if (IS_ZEBRA_DEBUG_KERNEL
)
572 zlog_debug("%s: sdl ifname %s addrs {%s}", __func__
,
573 (ifnlen
? ifname
: "(nil)"),
574 rtatostr(ifm
->ifm_addrs
, fbuf
, sizeof(fbuf
)));
577 * Look up on ifindex first, because ifindices are the primary handle
579 * interfaces across the user/kernel boundary, for most systems. (Some
580 * messages, such as up/down status changes on NetBSD, do not include a
583 if ((ifp
= if_lookup_by_index(ifm
->ifm_index
, VRF_DEFAULT
)) != NULL
) {
584 /* we have an ifp, verify that the name matches as some systems,
585 * eg Solaris, have a 1:many association of ifindex:ifname
586 * if they dont match, we dont have the correct ifp and should
587 * set it back to NULL to let next check do lookup by name
589 if (ifnlen
&& (strncmp(ifp
->name
, ifname
, IFNAMSIZ
) != 0)) {
590 if (IS_ZEBRA_DEBUG_KERNEL
)
592 "%s: ifp name %s doesn't match sdl name %s",
593 __func__
, ifp
->name
, ifname
);
599 * If we dont have an ifp, try looking up by name. Particularly as some
600 * systems (Solaris) have a 1:many mapping of ifindex:ifname - the
602 * is therefore our unique handle to that interface.
604 * Interfaces specified in the configuration file for which the ifindex
605 * has not been determined will have ifindex == IFINDEX_INTERNAL, and
607 * interfaces are found by this search, and then their ifindex values
611 if ((ifp
== NULL
) && ifnlen
)
612 ifp
= if_lookup_by_name(ifname
, VRF_DEFAULT
);
615 * If ifp still does not exist or has an invalid index
616 * (IFINDEX_INTERNAL),
617 * create or fill in an interface.
619 if ((ifp
== NULL
) || (ifp
->ifindex
== IFINDEX_INTERNAL
)) {
621 * To create or fill in an interface, a sockaddr_dl (via
622 * RTA_IFP) is required.
625 zlog_debug("Interface index %d (new) missing ifname\n",
630 #ifndef RTM_IFANNOUNCE
631 /* Down->Down interface should be ignored here.
632 * See further comment below.
634 if (!CHECK_FLAG(ifm
->ifm_flags
, IFF_UP
))
636 #endif /* !RTM_IFANNOUNCE */
639 /* Interface that zebra was not previously aware of, so
641 ifp
= if_create(ifname
, VRF_DEFAULT
);
642 if (IS_ZEBRA_DEBUG_KERNEL
)
643 zlog_debug("%s: creating ifp for ifindex %d",
644 __func__
, ifm
->ifm_index
);
647 if (IS_ZEBRA_DEBUG_KERNEL
)
649 "%s: updated/created ifp, ifname %s, ifindex %d",
650 __func__
, ifp
->name
, ifp
->ifindex
);
652 * Fill in newly created interface structure, or larval
653 * structure with ifindex IFINDEX_INTERNAL.
655 if_set_index(ifp
, ifm
->ifm_index
);
657 #ifdef HAVE_BSD_IFI_LINK_STATE /* translate BSD kernel msg for link-state */
658 bsd_linkdetect_translate(ifm
);
659 #endif /* HAVE_BSD_IFI_LINK_STATE */
661 if_flags_update(ifp
, ifm
->ifm_flags
);
662 #if defined(__bsdi__)
666 #endif /* __bsdi__ */
670 * XXX sockaddr_dl contents can be larger than the structure
671 * definition. There are 2 big families here:
672 * - BSD has sdl_len + sdl_data[16] + overruns sdl_data
673 * we MUST use sdl_len here or we'll truncate data.
674 * - Solaris has no sdl_len, but sdl_data[244]
675 * presumably, it's not going to run past that, so sizeof()
677 * a nonzero ifnlen from rta_getsdlname() means sdl is valid
679 ifp
->ll_type
= ZEBRA_LLT_UNKNOWN
;
680 ifp
->hw_addr_len
= 0;
682 #ifdef HAVE_STRUCT_SOCKADDR_DL_SDL_LEN
683 memcpy(&((struct zebra_if
*)ifp
->info
)->sdl
, sdl
,
686 memcpy(&((struct zebra_if
*)ifp
->info
)->sdl
, sdl
,
687 sizeof(struct sockaddr_dl
));
688 #endif /* HAVE_STRUCT_SOCKADDR_DL_SDL_LEN */
690 ifp
->ll_type
= sdl_to_zebra_link_type(sdl
->sdl_type
);
691 if (sdl
->sdl_alen
<= sizeof(ifp
->hw_addr
)) {
692 memcpy(ifp
->hw_addr
, LLADDR(sdl
),
694 ifp
->hw_addr_len
= sdl
->sdl_alen
;
701 * Interface structure exists. Adjust stored flags from
702 * notification. If interface has up->down or down->up
703 * transition, call state change routines (to adjust routes,
704 * notify routing daemons, etc.). (Other flag changes are stored
705 * but apparently do not trigger action.)
708 if (ifp
->ifindex
!= ifm
->ifm_index
) {
710 "%s: index mismatch, ifname %s, ifp index %d, "
712 __func__
, ifp
->name
, ifp
->ifindex
,
717 #ifdef HAVE_BSD_IFI_LINK_STATE /* translate BSD kernel msg for link-state */
718 bsd_linkdetect_translate(ifm
);
719 #endif /* HAVE_BSD_IFI_LINK_STATE */
721 /* update flags and handle operative->inoperative transition, if
723 if_flags_update(ifp
, ifm
->ifm_flags
);
725 #ifndef RTM_IFANNOUNCE
726 if (!if_is_up(ifp
)) {
727 /* No RTM_IFANNOUNCE on this platform, so we can never
728 * distinguish between ~IFF_UP and delete. We must
730 * it has been deleted.
731 * Eg, Solaris will not notify us of unplumb.
733 * XXX: Fixme - this should be runtime detected
734 * So that a binary compiled on a system with IFANNOUNCE
735 * will still behave correctly if run on a platform
738 if_delete_update(ifp
);
740 #endif /* RTM_IFANNOUNCE */
742 #if defined(__bsdi__)
746 #endif /* __bsdi__ */
751 #ifdef HAVE_NET_RT_IFLIST
752 ifp
->stats
= ifm
->ifm_data
;
753 #endif /* HAVE_NET_RT_IFLIST */
754 ifp
->speed
= ifm
->ifm_data
.ifi_baudrate
/ 1000000;
756 if (IS_ZEBRA_DEBUG_KERNEL
)
757 zlog_debug("%s: interface %s index %d", __func__
, ifp
->name
,
763 /* Address read from struct ifa_msghdr. */
764 static void ifam_read_mesg(struct ifa_msghdr
*ifm
, union sockunion
*addr
,
765 union sockunion
*mask
, union sockunion
*brd
,
766 char *ifname
, short *ifnlen
)
770 union sockunion gateway
;
774 pnt
= (caddr_t
)(ifm
+ 1);
775 end
= ((caddr_t
)ifm
) + ifm
->ifam_msglen
;
777 /* Be sure structure is cleared */
778 memset(mask
, 0, sizeof(union sockunion
));
779 memset(addr
, 0, sizeof(union sockunion
));
780 memset(brd
, 0, sizeof(union sockunion
));
781 memset(&dst
, 0, sizeof(union sockunion
));
782 memset(&gateway
, 0, sizeof(union sockunion
));
784 /* We fetch each socket variable into sockunion. */
785 for (maskbit
= 1; maskbit
; maskbit
<<= 1) {
786 if ((maskbit
& ifm
->ifam_addrs
) == 0)
791 pnt
+= rta_get(pnt
, &dst
, sizeof(dst
));
794 pnt
+= rta_get(pnt
, &gateway
, sizeof(gateway
));
797 pnt
+= rta_getattr(pnt
, mask
, sizeof(*mask
));
800 pnt
+= rta_getsdlname(pnt
, ifname
, ifnlen
);
803 pnt
+= rta_get(pnt
, addr
, sizeof(*addr
));
806 pnt
+= rta_get(pnt
, brd
, sizeof(*brd
));
810 pnt
+= rta_get(pnt
, NULL
, 0);
815 zlog_warn("%s: overflow detected (pnt:%p end:%p)",
821 if (IS_ZEBRA_DEBUG_KERNEL
) {
822 switch (sockunion_family(addr
)) {
825 char buf
[4][INET6_ADDRSTRLEN
];
827 (sockunion_family(addr
) == AF_INET
)
828 ? ip_masklen(mask
->sin
.sin_addr
)
829 : ip6_masklen(mask
->sin6
.sin6_addr
);
831 "%s: ifindex %d, ifname %s, ifam_addrs {%s}, "
832 "ifam_flags 0x%x, addr %s/%d broad %s dst %s "
834 __func__
, ifm
->ifam_index
,
835 (ifnlen
? ifname
: "(nil)"),
836 rtatostr(ifm
->ifam_addrs
, fbuf
, sizeof(fbuf
)),
838 sockunion2str(addr
, buf
[0], sizeof(buf
[0])),
840 sockunion2str(brd
, buf
[1], sizeof(buf
[1])),
841 sockunion2str(&dst
, buf
[2], sizeof(buf
[2])),
842 sockunion2str(&gateway
, buf
[2],
846 zlog_debug("%s: ifindex %d, ifname %s, ifam_addrs {%s}",
847 __func__
, ifm
->ifam_index
,
848 (ifnlen
? ifname
: "(nil)"),
849 rtatostr(ifm
->ifam_addrs
, fbuf
,
855 /* Assert read up end point matches to end point */
856 pnt
= (caddr_t
)ROUNDUP((size_t)pnt
);
857 if (pnt
!= (caddr_t
)ROUNDUP((size_t)end
))
858 zlog_debug("ifam_read() doesn't read all socket data");
861 /* Interface's address information get. */
862 int ifam_read(struct ifa_msghdr
*ifam
)
864 struct interface
*ifp
= NULL
;
865 union sockunion addr
, mask
, brd
;
866 char ifname
[INTERFACE_NAMSIZ
];
871 ifname
[0] = ifname
[INTERFACE_NAMSIZ
- 1] = '\0';
873 /* Allocate and read address information. */
874 ifam_read_mesg(ifam
, &addr
, &mask
, &brd
, ifname
, &ifnlen
);
876 if ((ifp
= if_lookup_by_index(ifam
->ifam_index
, VRF_DEFAULT
)) == NULL
) {
877 flog_warn(EC_ZEBRA_UNKNOWN_INTERFACE
,
878 "%s: no interface for ifname %s, index %d", __func__
,
879 ifname
, ifam
->ifam_index
);
883 if (ifnlen
&& strncmp(ifp
->name
, ifname
, INTERFACE_NAMSIZ
))
886 /* N.B. The info in ifa_msghdr does not tell us whether the RTA_BRD
887 field contains a broadcast address or a peer address, so we are
889 rely upon the interface type. */
890 if (if_is_pointopoint(ifp
))
891 SET_FLAG(flags
, ZEBRA_IFA_PEER
);
894 /* it might seem cute to grab the interface metric here, however
895 * we're processing an address update message, and so some systems
896 * (e.g. FBSD) dont bother to fill in ifam_metric. Disabled, but left
897 * in deliberately, as comment.
899 ifp
->metric
= ifam
->ifam_metric
;
902 /* Add connected address. */
903 switch (sockunion_family(&addr
)) {
905 if (ifam
->ifam_type
== RTM_NEWADDR
)
906 connected_add_ipv4(ifp
, flags
, &addr
.sin
.sin_addr
,
907 ip_masklen(mask
.sin
.sin_addr
),
909 (isalias
? ifname
: NULL
));
911 connected_delete_ipv4(ifp
, flags
, &addr
.sin
.sin_addr
,
912 ip_masklen(mask
.sin
.sin_addr
),
916 /* Unset interface index from link-local address when IPv6 stack
918 if (IN6_IS_ADDR_LINKLOCAL(&addr
.sin6
.sin6_addr
)) {
919 SET_IN6_LINKLOCAL_IFINDEX(addr
.sin6
.sin6_addr
, 0);
922 if (ifam
->ifam_type
== RTM_NEWADDR
)
923 connected_add_ipv6(ifp
, flags
, &addr
.sin6
.sin6_addr
,
925 ip6_masklen(mask
.sin6
.sin6_addr
),
926 (isalias
? ifname
: NULL
));
928 connected_delete_ipv6(ifp
, &addr
.sin6
.sin6_addr
, NULL
,
929 ip6_masklen(mask
.sin6
.sin6_addr
));
932 /* Unsupported family silently ignore... */
936 /* Check interface flag for implicit up of the interface. */
940 /* In addition to lacking IFANNOUNCE, on SUNOS IFF_UP is strange.
941 * See comments for SUNOS_5 in interface.c::if_flags_mangle.
943 * Here we take care of case where the real IFF_UP was previously
944 * unset (as kept in struct zebra_if.primary_state) and the mangled
945 * IFF_UP (ie IFF_UP set || listcount(connected) has now transitioned
946 * to unset due to the lost non-primary address having DELADDR'd.
948 * we must delete the interface, because in between here and next
949 * event for this interface-name the administrator could unplumb
950 * and replumb the interface.
953 if_delete_update(ifp
);
959 /* Interface function for reading kernel routing table information. */
960 static int rtm_read_mesg(struct rt_msghdr
*rtm
, union sockunion
*dest
,
961 union sockunion
*mask
, union sockunion
*gate
,
962 char *ifname
, short *ifnlen
)
967 /* Pnt points out socket data start point. */
968 pnt
= (caddr_t
)(rtm
+ 1);
969 end
= ((caddr_t
)rtm
) + rtm
->rtm_msglen
;
971 /* rt_msghdr version check. */
972 if (rtm
->rtm_version
!= RTM_VERSION
)
973 flog_warn(EC_ZEBRA_RTM_VERSION_MISMATCH
,
974 "Routing message version different %d should be %d."
975 "This may cause problem\n",
976 rtm
->rtm_version
, RTM_VERSION
);
978 /* Be sure structure is cleared */
979 memset(dest
, 0, sizeof(union sockunion
));
980 memset(gate
, 0, sizeof(union sockunion
));
981 memset(mask
, 0, sizeof(union sockunion
));
983 /* We fetch each socket variable into sockunion. */
984 /* We fetch each socket variable into sockunion. */
985 for (maskbit
= 1; maskbit
; maskbit
<<= 1) {
986 if ((maskbit
& rtm
->rtm_addrs
) == 0)
991 pnt
+= rta_get(pnt
, dest
, sizeof(*dest
));
994 pnt
+= rta_get(pnt
, gate
, sizeof(*gate
));
997 pnt
+= rta_getattr(pnt
, mask
, sizeof(*mask
));
1000 pnt
+= rta_getsdlname(pnt
, ifname
, ifnlen
);
1004 pnt
+= rta_get(pnt
, NULL
, 0);
1009 zlog_warn("%s: overflow detected (pnt:%p end:%p)",
1010 __func__
, pnt
, end
);
1015 /* If there is netmask information set it's family same as
1016 destination family*/
1017 if (rtm
->rtm_addrs
& RTA_NETMASK
)
1018 mask
->sa
.sa_family
= dest
->sa
.sa_family
;
1020 /* Assert read up to the end of pointer. */
1022 zlog_debug("rtm_read() doesn't read all socket data.");
1024 return rtm
->rtm_flags
;
1027 void rtm_read(struct rt_msghdr
*rtm
)
1030 uint8_t zebra_flags
;
1031 union sockunion dest
, mask
, gate
;
1032 char ifname
[INTERFACE_NAMSIZ
+ 1];
1036 ifindex_t ifindex
= 0;
1042 /* Read destination and netmask and gateway from rtm message
1044 flags
= rtm_read_mesg(rtm
, &dest
, &mask
, &gate
, ifname
, &ifnlen
);
1045 if (!(flags
& RTF_DONE
))
1047 if (IS_ZEBRA_DEBUG_KERNEL
)
1048 zlog_debug("%s: got rtm of type %d (%s) addrs {%s}", __func__
,
1050 lookup_msg(rtm_type_str
, rtm
->rtm_type
, NULL
),
1051 rtatostr(rtm
->rtm_addrs
, fbuf
, sizeof(fbuf
)));
1053 #ifdef RTF_CLONED /*bsdi, netbsd 1.6*/
1054 if (flags
& RTF_CLONED
)
1057 #ifdef RTF_WASCLONED /*freebsd*/
1058 if (flags
& RTF_WASCLONED
)
1062 if ((rtm
->rtm_type
== RTM_ADD
|| rtm
->rtm_type
== RTM_CHANGE
)
1063 && !(flags
& RTF_UP
))
1066 /* This is connected route. */
1067 if (!(flags
& RTF_GATEWAY
))
1070 if (flags
& RTF_PROTO1
)
1071 SET_FLAG(zebra_flags
, ZEBRA_FLAG_SELFROUTE
);
1073 memset(&nh
, 0, sizeof(nh
));
1075 nh
.vrf_id
= VRF_DEFAULT
;
1076 /* This is a reject or blackhole route */
1077 if (flags
& RTF_REJECT
) {
1078 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
1079 nh
.bh_type
= BLACKHOLE_REJECT
;
1080 } else if (flags
& RTF_BLACKHOLE
) {
1081 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
1082 nh
.bh_type
= BLACKHOLE_NULL
;
1086 * Ignore our own messages.
1088 if (rtm
->rtm_type
!= RTM_GET
&& rtm
->rtm_pid
== pid
)
1091 if (dest
.sa
.sa_family
== AF_INET
) {
1094 p
.u
.prefix4
= dest
.sin
.sin_addr
;
1095 if (flags
& RTF_HOST
)
1096 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
1098 p
.prefixlen
= ip_masklen(mask
.sin
.sin_addr
);
1101 nh
.type
= NEXTHOP_TYPE_IPV4
;
1102 nh
.gate
.ipv4
= gate
.sin
.sin_addr
;
1104 } else if (dest
.sa
.sa_family
== AF_INET6
) {
1106 p
.family
= AF_INET6
;
1107 p
.u
.prefix6
= dest
.sin6
.sin6_addr
;
1108 if (flags
& RTF_HOST
)
1109 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
1111 p
.prefixlen
= ip6_masklen(mask
.sin6
.sin6_addr
);
1114 if (IN6_IS_ADDR_LINKLOCAL(&gate
.sin6
.sin6_addr
)) {
1115 ifindex
= IN6_LINKLOCAL_IFINDEX(gate
.sin6
.sin6_addr
);
1116 SET_IN6_LINKLOCAL_IFINDEX(gate
.sin6
.sin6_addr
, 0);
1121 nh
.type
= ifindex
? NEXTHOP_TYPE_IPV6_IFINDEX
1122 : NEXTHOP_TYPE_IPV6
;
1123 nh
.gate
.ipv6
= gate
.sin6
.sin6_addr
;
1124 nh
.ifindex
= ifindex
;
1130 * CHANGE: delete the old prefix, we have no further information
1131 * to specify the route really
1133 if (rtm
->rtm_type
== RTM_CHANGE
)
1134 rib_delete(afi
, SAFI_UNICAST
, VRF_DEFAULT
, ZEBRA_ROUTE_KERNEL
,
1135 0, zebra_flags
, &p
, NULL
, NULL
, 0, 0, 0, true);
1136 if (rtm
->rtm_type
== RTM_GET
|| rtm
->rtm_type
== RTM_ADD
1137 || rtm
->rtm_type
== RTM_CHANGE
)
1138 rib_add(afi
, SAFI_UNICAST
, VRF_DEFAULT
, ZEBRA_ROUTE_KERNEL
, 0,
1139 zebra_flags
, &p
, NULL
, &nh
, 0, 0, 0, 0, 0);
1141 rib_delete(afi
, SAFI_UNICAST
, VRF_DEFAULT
, ZEBRA_ROUTE_KERNEL
,
1142 0, zebra_flags
, &p
, NULL
, &nh
, 0, 0, 0, true);
1145 /* Interface function for the kernel routing table updates. Support
1146 * for RTM_CHANGE will be needed.
1147 * Exported only for rt_socket.c
1149 int rtm_write(int message
, union sockunion
*dest
, union sockunion
*mask
,
1150 union sockunion
*gate
, union sockunion
*mpls
, unsigned int index
,
1151 enum blackhole_type bh_type
, int metric
)
1155 struct interface
*ifp
;
1157 /* Sequencial number of routing message. */
1158 static int msg_seq
= 0;
1160 /* Struct of rt_msghdr and buffer for storing socket's data. */
1162 struct rt_msghdr rtm
;
1166 if (dplane_routing_sock
< 0)
1167 return ZEBRA_ERR_EPERM
;
1169 /* Clear and set rt_msghdr values */
1170 memset(&msg
, 0, sizeof(struct rt_msghdr
));
1171 msg
.rtm
.rtm_version
= RTM_VERSION
;
1172 msg
.rtm
.rtm_type
= message
;
1173 msg
.rtm
.rtm_seq
= msg_seq
++;
1174 msg
.rtm
.rtm_addrs
= RTA_DST
;
1175 msg
.rtm
.rtm_addrs
|= RTA_GATEWAY
;
1176 msg
.rtm
.rtm_flags
= RTF_UP
;
1178 msg
.rtm
.rtm_flags
|= RTF_MPATH
;
1179 msg
.rtm
.rtm_fmask
= RTF_MPLS
;
1181 msg
.rtm
.rtm_index
= index
;
1184 msg
.rtm
.rtm_rmx
.rmx_hopcount
= metric
;
1185 msg
.rtm
.rtm_inits
|= RTV_HOPCOUNT
;
1188 ifp
= if_lookup_by_index(index
, VRF_DEFAULT
);
1190 if (gate
&& (message
== RTM_ADD
|| message
== RTM_CHANGE
))
1191 msg
.rtm
.rtm_flags
|= RTF_GATEWAY
;
1193 /* When RTF_CLONING is unavailable on BSD, should we set some
1194 * other flag instead?
1197 if (!gate
&& (message
== RTM_ADD
|| message
== RTM_CHANGE
) && ifp
1198 && (ifp
->flags
& IFF_POINTOPOINT
) == 0)
1199 msg
.rtm
.rtm_flags
|= RTF_CLONING
;
1200 #endif /* RTF_CLONING */
1202 /* If no protocol specific gateway is specified, use link
1203 address for gateway. */
1206 char dest_buf
[INET_ADDRSTRLEN
] = "NULL",
1207 mask_buf
[INET_ADDRSTRLEN
] = "255.255.255.255";
1209 inet_ntop(AF_INET
, &dest
->sin
.sin_addr
,
1210 dest_buf
, INET_ADDRSTRLEN
);
1212 inet_ntop(AF_INET
, &mask
->sin
.sin_addr
,
1213 mask_buf
, INET_ADDRSTRLEN
);
1215 EC_ZEBRA_RTM_NO_GATEWAY
,
1216 "%s: %s/%s: gate == NULL and no gateway found for ifindex %d",
1217 __func__
, dest_buf
, mask_buf
, index
);
1220 gate
= (union sockunion
*)&((struct zebra_if
*)ifp
->info
)->sdl
;
1224 msg
.rtm
.rtm_addrs
|= RTA_NETMASK
;
1225 else if (message
== RTM_ADD
|| message
== RTM_CHANGE
)
1226 msg
.rtm
.rtm_flags
|= RTF_HOST
;
1230 msg
.rtm
.rtm_addrs
|= RTA_SRC
;
1231 msg
.rtm
.rtm_flags
|= RTF_MPLS
;
1233 if (mpls
->smpls
.smpls_label
1234 != htonl(MPLS_LABEL_IMPLICIT_NULL
<< MPLS_LABEL_OFFSET
))
1235 msg
.rtm
.rtm_mpls
= MPLS_OP_PUSH
;
1239 /* Tagging route with flags */
1240 msg
.rtm
.rtm_flags
|= (RTF_PROTO1
);
1243 case BLACKHOLE_UNSPEC
:
1245 case BLACKHOLE_REJECT
:
1246 msg
.rtm
.rtm_flags
|= RTF_REJECT
;
1249 msg
.rtm
.rtm_flags
|= RTF_BLACKHOLE
;
1254 #define SOCKADDRSET(X, R) \
1255 if (msg.rtm.rtm_addrs & (R)) { \
1256 int len = SAROUNDUP(X); \
1257 memcpy(pnt, (caddr_t)(X), len); \
1261 pnt
= (caddr_t
)msg
.buf
;
1263 /* Write each socket data into rtm message buffer */
1264 SOCKADDRSET(dest
, RTA_DST
);
1265 SOCKADDRSET(gate
, RTA_GATEWAY
);
1266 SOCKADDRSET(mask
, RTA_NETMASK
);
1268 SOCKADDRSET(mpls
, RTA_SRC
);
1271 msg
.rtm
.rtm_msglen
= pnt
- (caddr_t
)&msg
;
1273 ret
= write(dplane_routing_sock
, &msg
, msg
.rtm
.rtm_msglen
);
1275 if (ret
!= msg
.rtm
.rtm_msglen
) {
1276 if (errno
== EEXIST
)
1277 return ZEBRA_ERR_RTEXIST
;
1278 if (errno
== ENETUNREACH
)
1279 return ZEBRA_ERR_RTUNREACH
;
1281 return ZEBRA_ERR_RTNOEXIST
;
1283 flog_err_sys(EC_LIB_SOCKET
, "%s: write : %s (%d)", __func__
,
1284 safe_strerror(errno
), errno
);
1285 return ZEBRA_ERR_KERNEL
;
1287 return ZEBRA_ERR_NOERROR
;
1292 #include "zebra/zserv.h"
1294 /* For debug purpose. */
1295 static void rtmsg_debug(struct rt_msghdr
*rtm
)
1299 zlog_debug("Kernel: Len: %d Type: %s", rtm
->rtm_msglen
,
1300 lookup_msg(rtm_type_str
, rtm
->rtm_type
, NULL
));
1301 rtm_flag_dump(rtm
->rtm_flags
);
1302 zlog_debug("Kernel: message seq %d", rtm
->rtm_seq
);
1303 zlog_debug("Kernel: pid %lld, rtm_addrs {%s}", (long long)rtm
->rtm_pid
,
1304 rtatostr(rtm
->rtm_addrs
, fbuf
, sizeof(fbuf
)));
1307 /* This is pretty gross, better suggestions welcome -- mhandler */
1310 #define RTAX_MAX RTA_NUMBITS
1313 #endif /* RTA_NUMBITS */
1314 #endif /* RTAX_MAX */
1316 /* Kernel routing table and interface updates via routing socket. */
1317 static int kernel_read(struct thread
*thread
)
1321 struct rt_msghdr
*rtm
;
1324 * This must be big enough for any message the kernel might send.
1325 * Rather than determining how many sockaddrs of what size might be
1326 * in each particular message, just use RTAX_MAX of sockaddr_storage
1327 * for each. Note that the sockaddrs must be after each message
1328 * definition, or rather after whichever happens to be the largest,
1329 * since the buffer needs to be big enough for a message and the
1330 * sockaddrs together.
1333 /* Routing information. */
1335 struct rt_msghdr rtm
;
1336 struct sockaddr_storage addr
[RTAX_MAX
];
1339 /* Interface information. */
1341 struct if_msghdr ifm
;
1342 struct sockaddr_storage addr
[RTAX_MAX
];
1345 /* Interface address information. */
1347 struct ifa_msghdr ifa
;
1348 struct sockaddr_storage addr
[RTAX_MAX
];
1351 #ifdef RTM_IFANNOUNCE
1352 /* Interface arrival/departure */
1354 struct if_announcemsghdr ifan
;
1355 struct sockaddr_storage addr
[RTAX_MAX
];
1357 #endif /* RTM_IFANNOUNCE */
1361 /* Fetch routing socket. */
1362 sock
= THREAD_FD(thread
);
1364 nbytes
= read(sock
, &buf
, sizeof buf
);
1367 if (nbytes
< 0 && errno
!= EWOULDBLOCK
&& errno
!= EAGAIN
)
1368 flog_err_sys(EC_LIB_SOCKET
, "routing socket error: %s",
1369 safe_strerror(errno
));
1373 thread_add_read(zebrad
.master
, kernel_read
, NULL
, sock
, NULL
);
1375 if (IS_ZEBRA_DEBUG_KERNEL
)
1376 rtmsg_debug(&buf
.r
.rtm
);
1381 * Ensure that we didn't drop any data, so that processing routines
1382 * can assume they have the whole message.
1384 if (rtm
->rtm_msglen
!= nbytes
) {
1386 "kernel_read: rtm->rtm_msglen %d, nbytes %d, type %d\n",
1387 rtm
->rtm_msglen
, nbytes
, rtm
->rtm_type
);
1391 switch (rtm
->rtm_type
) {
1398 ifm_read(&buf
.im
.ifm
);
1402 ifam_read(&buf
.ia
.ifa
);
1404 #ifdef RTM_IFANNOUNCE
1405 case RTM_IFANNOUNCE
:
1406 ifan_read(&buf
.ian
.ifan
);
1408 #endif /* RTM_IFANNOUNCE */
1410 if (IS_ZEBRA_DEBUG_KERNEL
)
1411 zlog_debug("Unprocessed RTM_type: %d", rtm
->rtm_type
);
1417 /* Make routing socket. */
1418 static void routing_socket(struct zebra_ns
*zns
)
1420 frr_elevate_privs(&zserv_privs
) {
1421 routing_sock
= ns_socket(AF_ROUTE
, SOCK_RAW
, 0, zns
->ns_id
);
1423 dplane_routing_sock
=
1424 ns_socket(AF_ROUTE
, SOCK_RAW
, 0, zns
->ns_id
);
1427 if (routing_sock
< 0) {
1428 flog_err_sys(EC_LIB_SOCKET
, "Can't init kernel routing socket");
1432 if (dplane_routing_sock
< 0) {
1433 flog_err_sys(EC_LIB_SOCKET
,
1434 "Can't init kernel dataplane routing socket");
1438 /* XXX: Socket should be NONBLOCK, however as we currently
1439 * discard failed writes, this will lead to inconsistencies.
1440 * For now, socket must be blocking.
1442 /*if (fcntl (routing_sock, F_SETFL, O_NONBLOCK) < 0)
1443 zlog_warn ("Can't set O_NONBLOCK to routing socket");*/
1445 /* kernel_read needs rewrite. */
1446 thread_add_read(zebrad
.master
, kernel_read
, NULL
, routing_sock
, NULL
);
1449 /* Exported interface function. This function simply calls
1450 routing_socket (). */
1451 void kernel_init(struct zebra_ns
*zns
)
1453 routing_socket(zns
);
1456 void kernel_terminate(struct zebra_ns
*zns
, bool complete
)
1461 #endif /* !HAVE_NETLINK */