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/zebra_router.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 */
152 {RTM_RESOLVE
, "RTM_RESOLVE"},
153 #endif /* RTM_RESOLVE */
154 {RTM_NEWADDR
, "RTM_NEWADDR"},
155 {RTM_DELADDR
, "RTM_DELADDR"},
156 {RTM_IFINFO
, "RTM_IFINFO"},
158 {RTM_OIFINFO
, "RTM_OIFINFO"},
159 #endif /* RTM_OIFINFO */
161 {RTM_NEWMADDR
, "RTM_NEWMADDR"},
162 #endif /* RTM_NEWMADDR */
164 {RTM_DELMADDR
, "RTM_DELMADDR"},
165 #endif /* RTM_DELMADDR */
166 #ifdef RTM_IFANNOUNCE
167 {RTM_IFANNOUNCE
, "RTM_IFANNOUNCE"},
168 #endif /* RTM_IFANNOUNCE */
171 static const struct message rtm_flag_str
[] = {{RTF_UP
, "UP"},
172 {RTF_GATEWAY
, "GATEWAY"},
174 {RTF_REJECT
, "REJECT"},
175 {RTF_DYNAMIC
, "DYNAMIC"},
176 {RTF_MODIFIED
, "MODIFIED"},
180 #endif /* RTF_MASK */
182 {RTF_CLONING
, "CLONING"},
183 #endif /* RTF_CLONING */
185 {RTF_XRESOLVE
, "XRESOLVE"},
186 #endif /* RTF_XRESOLVE */
188 {RTF_LLINFO
, "LLINFO"},
189 #endif /* RTF_LLINFO */
190 {RTF_STATIC
, "STATIC"},
191 {RTF_BLACKHOLE
, "BLACKHOLE"},
193 {RTF_PRIVATE
, "PRIVATE"},
194 #endif /* RTF_PRIVATE */
195 {RTF_PROTO1
, "PROTO1"},
196 {RTF_PROTO2
, "PROTO2"},
198 {RTF_PRCLONING
, "PRCLONING"},
199 #endif /* RTF_PRCLONING */
201 {RTF_WASCLONED
, "WASCLONED"},
202 #endif /* RTF_WASCLONED */
204 {RTF_PROTO3
, "PROTO3"},
205 #endif /* RTF_PROTO3 */
207 {RTF_PINNED
, "PINNED"},
208 #endif /* RTF_PINNED */
210 {RTF_LOCAL
, "LOCAL"},
211 #endif /* RTF_LOCAL */
213 {RTF_BROADCAST
, "BROADCAST"},
214 #endif /* RTF_BROADCAST */
216 {RTF_MULTICAST
, "MULTICAST"},
217 #endif /* RTF_MULTICAST */
219 {RTF_MULTIRT
, "MULTIRT"},
220 #endif /* RTF_MULTIRT */
222 {RTF_SETSRC
, "SETSRC"},
223 #endif /* RTF_SETSRC */
226 /* Kernel routing update socket. */
227 int routing_sock
= -1;
229 /* Kernel dataplane routing update socket, used in the dataplane pthread
232 int dplane_routing_sock
= -1;
234 /* Yes I'm checking ugly routing socket behavior. */
237 size_t _rta_get(caddr_t sap
, void *destp
, size_t destlen
, bool checkaf
);
238 size_t rta_get(caddr_t sap
, void *dest
, size_t destlen
);
239 size_t rta_getattr(caddr_t sap
, void *destp
, size_t destlen
);
240 size_t rta_getsdlname(caddr_t sap
, void *dest
, short *destlen
);
241 const char *rtatostr(unsigned int flags
, char *buf
, size_t buflen
);
243 /* Supported address family check. */
244 static inline int af_check(int family
)
246 if (family
== AF_INET
)
248 if (family
== AF_INET6
)
253 size_t _rta_get(caddr_t sap
, void *destp
, size_t destlen
, bool checkaf
)
255 struct sockaddr
*sa
= (struct sockaddr
*)sap
;
256 struct sockaddr_dl
*sdl
;
257 uint8_t *dest
= destp
;
258 size_t tlen
, copylen
;
260 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
261 copylen
= sa
->sa_len
;
262 tlen
= (copylen
== 0) ? sizeof(ROUNDUP_TYPE
) : ROUNDUP(copylen
);
263 #else /* !HAVE_STRUCT_SOCKADDR_SA_LEN */
264 copylen
= tlen
= SAROUNDUP(sap
);
265 #endif /* !HAVE_STRUCT_SOCKADDR_SA_LEN */
267 if (copylen
> 0 && dest
!= NULL
) {
268 if (checkaf
&& af_check(sa
->sa_family
) == 0)
271 * Handle sockaddr_dl corner case:
272 * RTA_NETMASK might be AF_LINK, but it doesn't anything
273 * relevant (e.g. zeroed out fields). Check for this
274 * case and avoid warning log message.
276 if (sa
->sa_family
== AF_LINK
) {
277 sdl
= (struct sockaddr_dl
*)sa
;
278 if (sdl
->sdl_index
== 0 || sdl
->sdl_nlen
== 0)
282 if (copylen
> destlen
) {
284 "%s: destination buffer too small (%zu vs %zu)",
285 __func__
, copylen
, destlen
);
286 memcpy(dest
, sap
, destlen
);
288 memcpy(dest
, sap
, copylen
);
294 size_t rta_get(caddr_t sap
, void *destp
, size_t destlen
)
296 return _rta_get(sap
, destp
, destlen
, true);
299 size_t rta_getattr(caddr_t sap
, void *destp
, size_t destlen
)
301 return _rta_get(sap
, destp
, destlen
, false);
304 size_t rta_getsdlname(caddr_t sap
, void *destp
, short *destlen
)
306 struct sockaddr_dl
*sdl
= (struct sockaddr_dl
*)sap
;
307 uint8_t *dest
= destp
;
308 size_t tlen
, copylen
;
310 copylen
= sdl
->sdl_nlen
;
311 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
312 struct sockaddr
*sa
= (struct sockaddr
*)sap
;
314 tlen
= (sa
->sa_len
== 0) ? sizeof(ROUNDUP_TYPE
) : ROUNDUP(sa
->sa_len
);
315 #else /* !HAVE_STRUCT_SOCKADDR_SA_LEN */
316 tlen
= SAROUNDUP(sap
);
317 #endif /* !HAVE_STRUCT_SOCKADDR_SA_LEN */
319 if (copylen
> 0 && dest
!= NULL
&& sdl
->sdl_family
== AF_LINK
) {
320 if (copylen
> IFNAMSIZ
) {
322 "%s: destination buffer too small (%zu vs %d)",
323 __func__
, copylen
, IFNAMSIZ
);
324 memcpy(dest
, sdl
->sdl_data
, IFNAMSIZ
);
328 memcpy(dest
, sdl
->sdl_data
, copylen
);
338 const char *rtatostr(unsigned int flags
, char *buf
, size_t buflen
)
340 const char *flagstr
, *bufstart
;
344 /* Hold the pointer to the buffer beginning. */
347 for (bit
= 1; bit
; bit
<<= 1) {
348 if ((flags
& bit
) == 0)
365 #endif /* RTA_GENMASK */
376 #endif /* RTA_AUTHOR */
389 #endif /* RTA_SRCMASK */
394 #endif /* RTA_LABEL */
397 snprintf(ustr
, sizeof(ustr
), "0x%x", bit
);
402 wlen
= snprintf(buf
, buflen
, "%s,", flagstr
);
407 /* Check for empty buffer. */
411 /* Remove the last comma. */
417 /* Dump routing table flag for debug purpose. */
418 static void rtm_flag_dump(int flag
)
420 const struct message
*mes
;
421 static char buf
[BUFSIZ
];
424 for (mes
= rtm_flag_str
; mes
->key
!= 0; mes
++) {
425 if (mes
->key
& flag
) {
426 strlcat(buf
, mes
->str
, BUFSIZ
);
427 strlcat(buf
, " ", BUFSIZ
);
430 zlog_debug("Kernel: %s", buf
);
433 #ifdef RTM_IFANNOUNCE
434 /* Interface adding function */
435 static int ifan_read(struct if_announcemsghdr
*ifan
)
437 struct interface
*ifp
;
439 ifp
= if_lookup_by_index(ifan
->ifan_index
, VRF_DEFAULT
);
442 assert((ifp
->ifindex
== ifan
->ifan_index
)
443 || (ifp
->ifindex
== IFINDEX_INTERNAL
));
445 if ((ifp
== NULL
) || ((ifp
->ifindex
== IFINDEX_INTERNAL
)
446 && (ifan
->ifan_what
== IFAN_ARRIVAL
))) {
447 if (IS_ZEBRA_DEBUG_KERNEL
)
449 "%s: creating interface for ifindex %d, name %s",
450 __func__
, ifan
->ifan_index
, ifan
->ifan_name
);
452 /* Create Interface */
453 ifp
= if_get_by_name(ifan
->ifan_name
, VRF_DEFAULT
);
454 if_set_index(ifp
, ifan
->ifan_index
);
458 } else if (ifp
!= NULL
&& ifan
->ifan_what
== IFAN_DEPARTURE
)
459 if_delete_update(ifp
);
465 if (IS_ZEBRA_DEBUG_KERNEL
)
466 zlog_debug("%s: interface %s index %d", __func__
,
467 ifan
->ifan_name
, ifan
->ifan_index
);
471 #endif /* RTM_IFANNOUNCE */
473 #ifdef HAVE_BSD_IFI_LINK_STATE
474 /* BSD link detect translation */
475 static void bsd_linkdetect_translate(struct if_msghdr
*ifm
)
477 if ((ifm
->ifm_data
.ifi_link_state
>= LINK_STATE_UP
)
478 || (ifm
->ifm_data
.ifi_link_state
== LINK_STATE_UNKNOWN
))
479 SET_FLAG(ifm
->ifm_flags
, IFF_RUNNING
);
481 UNSET_FLAG(ifm
->ifm_flags
, IFF_RUNNING
);
483 #endif /* HAVE_BSD_IFI_LINK_STATE */
485 static enum zebra_link_type
sdl_to_zebra_link_type(unsigned int sdlt
)
489 return ZEBRA_LLT_ETHER
;
491 return ZEBRA_LLT_X25
;
493 return ZEBRA_LLT_FDDI
;
495 return ZEBRA_LLT_PPP
;
497 return ZEBRA_LLT_LOOPBACK
;
499 return ZEBRA_LLT_SLIP
;
501 return ZEBRA_LLT_ARCNET
;
503 return ZEBRA_LLT_ATM
;
505 return ZEBRA_LLT_LOCALTLK
;
507 return ZEBRA_LLT_HIPPI
;
510 return ZEBRA_LLT_IEEE1394
;
514 return ZEBRA_LLT_UNKNOWN
;
519 * Handle struct if_msghdr obtained from reading routing socket or
520 * sysctl (from interface_list). There may or may not be sockaddrs
521 * present after the header.
523 int ifm_read(struct if_msghdr
*ifm
)
525 struct interface
*ifp
= NULL
;
526 struct sockaddr_dl
*sdl
= NULL
;
527 char ifname
[IFNAMSIZ
];
533 /* terminate ifname at head (for strnlen) and tail (for safety) */
534 ifname
[IFNAMSIZ
- 1] = '\0';
536 /* paranoia: sanity check structure */
537 if (ifm
->ifm_msglen
< sizeof(struct if_msghdr
)) {
538 flog_err(EC_ZEBRA_NETLINK_LENGTH_ERROR
,
539 "ifm_read: ifm->ifm_msglen %d too short\n",
545 * Check for a sockaddr_dl following the message. First, point to
546 * where a socakddr might be if one follows the message.
548 cp
= (void *)(ifm
+ 1);
552 * XXX This behavior should be narrowed to only the kernel versions
553 * for which the structures returned do not match the headers.
555 * if_msghdr_t on 64 bit kernels in Solaris 9 and earlier versions
556 * is 12 bytes larger than the 32 bit version.
558 if (((struct sockaddr
*)cp
)->sa_family
== AF_UNSPEC
)
562 /* Look up for RTA_IFP and skip others. */
563 for (maskbit
= 1; maskbit
; maskbit
<<= 1) {
564 if ((maskbit
& ifm
->ifm_addrs
) == 0)
566 if (maskbit
!= RTA_IFP
) {
567 cp
+= rta_get(cp
, NULL
, 0);
571 /* Save the pointer to the structure. */
572 sdl
= (struct sockaddr_dl
*)cp
;
573 cp
+= rta_getsdlname(cp
, ifname
, &ifnlen
);
576 if (IS_ZEBRA_DEBUG_KERNEL
)
577 zlog_debug("%s: sdl ifname %s addrs {%s}", __func__
,
578 (ifnlen
? ifname
: "(nil)"),
579 rtatostr(ifm
->ifm_addrs
, fbuf
, sizeof(fbuf
)));
582 * Look up on ifindex first, because ifindices are the primary handle
584 * interfaces across the user/kernel boundary, for most systems. (Some
585 * messages, such as up/down status changes on NetBSD, do not include a
588 if ((ifp
= if_lookup_by_index(ifm
->ifm_index
, VRF_DEFAULT
)) != NULL
) {
589 /* we have an ifp, verify that the name matches as some systems,
590 * eg Solaris, have a 1:many association of ifindex:ifname
591 * if they dont match, we dont have the correct ifp and should
592 * set it back to NULL to let next check do lookup by name
594 if (ifnlen
&& (strncmp(ifp
->name
, ifname
, IFNAMSIZ
) != 0)) {
595 if (IS_ZEBRA_DEBUG_KERNEL
)
597 "%s: ifp name %s doesn't match sdl name %s",
598 __func__
, ifp
->name
, ifname
);
604 * If we dont have an ifp, try looking up by name. Particularly as some
605 * systems (Solaris) have a 1:many mapping of ifindex:ifname - the
607 * is therefore our unique handle to that interface.
609 * Interfaces specified in the configuration file for which the ifindex
610 * has not been determined will have ifindex == IFINDEX_INTERNAL, and
612 * interfaces are found by this search, and then their ifindex values
616 if ((ifp
== NULL
) && ifnlen
)
617 ifp
= if_lookup_by_name(ifname
, VRF_DEFAULT
);
620 * If ifp still does not exist or has an invalid index
621 * (IFINDEX_INTERNAL),
622 * create or fill in an interface.
624 if ((ifp
== NULL
) || (ifp
->ifindex
== IFINDEX_INTERNAL
)) {
626 * To create or fill in an interface, a sockaddr_dl (via
627 * RTA_IFP) is required.
630 zlog_debug("Interface index %d (new) missing ifname",
635 #ifndef RTM_IFANNOUNCE
636 /* Down->Down interface should be ignored here.
637 * See further comment below.
639 if (!CHECK_FLAG(ifm
->ifm_flags
, IFF_UP
))
641 #endif /* !RTM_IFANNOUNCE */
644 /* Interface that zebra was not previously aware of, so
646 ifp
= if_create(ifname
, VRF_DEFAULT
);
647 if (IS_ZEBRA_DEBUG_KERNEL
)
648 zlog_debug("%s: creating ifp for ifindex %d",
649 __func__
, ifm
->ifm_index
);
652 if (IS_ZEBRA_DEBUG_KERNEL
)
654 "%s: updated/created ifp, ifname %s, ifindex %d",
655 __func__
, ifp
->name
, ifp
->ifindex
);
657 * Fill in newly created interface structure, or larval
658 * structure with ifindex IFINDEX_INTERNAL.
660 if_set_index(ifp
, ifm
->ifm_index
);
662 #ifdef HAVE_BSD_IFI_LINK_STATE /* translate BSD kernel msg for link-state */
663 bsd_linkdetect_translate(ifm
);
664 #endif /* HAVE_BSD_IFI_LINK_STATE */
666 if_flags_update(ifp
, ifm
->ifm_flags
);
667 #if defined(__bsdi__)
671 #endif /* __bsdi__ */
675 * XXX sockaddr_dl contents can be larger than the structure
676 * definition. There are 2 big families here:
677 * - BSD has sdl_len + sdl_data[16] + overruns sdl_data
678 * we MUST use sdl_len here or we'll truncate data.
679 * - Solaris has no sdl_len, but sdl_data[244]
680 * presumably, it's not going to run past that, so sizeof()
682 * a nonzero ifnlen from rta_getsdlname() means sdl is valid
684 ifp
->ll_type
= ZEBRA_LLT_UNKNOWN
;
685 ifp
->hw_addr_len
= 0;
687 #ifdef HAVE_STRUCT_SOCKADDR_DL_SDL_LEN
688 memcpy(&((struct zebra_if
*)ifp
->info
)->sdl
, sdl
,
691 memcpy(&((struct zebra_if
*)ifp
->info
)->sdl
, sdl
,
692 sizeof(struct sockaddr_dl
));
693 #endif /* HAVE_STRUCT_SOCKADDR_DL_SDL_LEN */
695 ifp
->ll_type
= sdl_to_zebra_link_type(sdl
->sdl_type
);
696 if (sdl
->sdl_alen
<= sizeof(ifp
->hw_addr
)) {
697 memcpy(ifp
->hw_addr
, LLADDR(sdl
),
699 ifp
->hw_addr_len
= sdl
->sdl_alen
;
706 * Interface structure exists. Adjust stored flags from
707 * notification. If interface has up->down or down->up
708 * transition, call state change routines (to adjust routes,
709 * notify routing daemons, etc.). (Other flag changes are stored
710 * but apparently do not trigger action.)
713 if (ifp
->ifindex
!= ifm
->ifm_index
) {
715 "%s: index mismatch, ifname %s, ifp index %d, "
717 __func__
, ifp
->name
, ifp
->ifindex
,
722 #ifdef HAVE_BSD_IFI_LINK_STATE /* translate BSD kernel msg for link-state */
723 bsd_linkdetect_translate(ifm
);
724 #endif /* HAVE_BSD_IFI_LINK_STATE */
726 /* update flags and handle operative->inoperative transition, if
728 if_flags_update(ifp
, ifm
->ifm_flags
);
730 #ifndef RTM_IFANNOUNCE
731 if (!if_is_up(ifp
)) {
732 /* No RTM_IFANNOUNCE on this platform, so we can never
733 * distinguish between ~IFF_UP and delete. We must
735 * it has been deleted.
736 * Eg, Solaris will not notify us of unplumb.
738 * XXX: Fixme - this should be runtime detected
739 * So that a binary compiled on a system with IFANNOUNCE
740 * will still behave correctly if run on a platform
743 if_delete_update(ifp
);
745 #endif /* RTM_IFANNOUNCE */
747 #if defined(__bsdi__)
751 #endif /* __bsdi__ */
756 #ifdef HAVE_NET_RT_IFLIST
757 ifp
->stats
= ifm
->ifm_data
;
758 #endif /* HAVE_NET_RT_IFLIST */
759 ifp
->speed
= ifm
->ifm_data
.ifi_baudrate
/ 1000000;
761 if (IS_ZEBRA_DEBUG_KERNEL
)
762 zlog_debug("%s: interface %s index %d", __func__
, ifp
->name
,
768 /* Address read from struct ifa_msghdr. */
769 static void ifam_read_mesg(struct ifa_msghdr
*ifm
, union sockunion
*addr
,
770 union sockunion
*mask
, union sockunion
*brd
,
771 char *ifname
, short *ifnlen
)
775 union sockunion gateway
;
779 pnt
= (caddr_t
)(ifm
+ 1);
780 end
= ((caddr_t
)ifm
) + ifm
->ifam_msglen
;
782 /* Be sure structure is cleared */
783 memset(mask
, 0, sizeof(union sockunion
));
784 memset(addr
, 0, sizeof(union sockunion
));
785 memset(brd
, 0, sizeof(union sockunion
));
786 memset(&dst
, 0, sizeof(union sockunion
));
787 memset(&gateway
, 0, sizeof(union sockunion
));
789 /* We fetch each socket variable into sockunion. */
790 for (maskbit
= 1; maskbit
; maskbit
<<= 1) {
791 if ((maskbit
& ifm
->ifam_addrs
) == 0)
796 pnt
+= rta_get(pnt
, &dst
, sizeof(dst
));
799 pnt
+= rta_get(pnt
, &gateway
, sizeof(gateway
));
802 pnt
+= rta_getattr(pnt
, mask
, sizeof(*mask
));
805 pnt
+= rta_getsdlname(pnt
, ifname
, ifnlen
);
808 pnt
+= rta_get(pnt
, addr
, sizeof(*addr
));
811 pnt
+= rta_get(pnt
, brd
, sizeof(*brd
));
815 pnt
+= rta_get(pnt
, NULL
, 0);
820 zlog_warn("%s: overflow detected (pnt:%p end:%p)",
826 if (IS_ZEBRA_DEBUG_KERNEL
) {
827 switch (sockunion_family(addr
)) {
830 char buf
[4][INET6_ADDRSTRLEN
];
832 (sockunion_family(addr
) == AF_INET
)
833 ? ip_masklen(mask
->sin
.sin_addr
)
834 : ip6_masklen(mask
->sin6
.sin6_addr
);
836 "%s: ifindex %d, ifname %s, ifam_addrs {%s}, "
837 "ifam_flags 0x%x, addr %s/%d broad %s dst %s "
839 __func__
, ifm
->ifam_index
,
840 (ifnlen
? ifname
: "(nil)"),
841 rtatostr(ifm
->ifam_addrs
, fbuf
, sizeof(fbuf
)),
843 sockunion2str(addr
, buf
[0], sizeof(buf
[0])),
845 sockunion2str(brd
, buf
[1], sizeof(buf
[1])),
846 sockunion2str(&dst
, buf
[2], sizeof(buf
[2])),
847 sockunion2str(&gateway
, buf
[2],
851 zlog_debug("%s: ifindex %d, ifname %s, ifam_addrs {%s}",
852 __func__
, ifm
->ifam_index
,
853 (ifnlen
? ifname
: "(nil)"),
854 rtatostr(ifm
->ifam_addrs
, fbuf
,
860 /* Assert read up end point matches to end point */
861 pnt
= (caddr_t
)ROUNDUP((size_t)pnt
);
862 if (pnt
!= (caddr_t
)ROUNDUP((size_t)end
))
863 zlog_debug("ifam_read() doesn't read all socket data");
866 /* Interface's address information get. */
867 int ifam_read(struct ifa_msghdr
*ifam
)
869 struct interface
*ifp
= NULL
;
870 union sockunion addr
, mask
, brd
;
871 char ifname
[INTERFACE_NAMSIZ
];
876 ifname
[0] = ifname
[INTERFACE_NAMSIZ
- 1] = '\0';
878 /* Allocate and read address information. */
879 ifam_read_mesg(ifam
, &addr
, &mask
, &brd
, ifname
, &ifnlen
);
881 if ((ifp
= if_lookup_by_index(ifam
->ifam_index
, VRF_DEFAULT
)) == NULL
) {
882 flog_warn(EC_ZEBRA_UNKNOWN_INTERFACE
,
883 "%s: no interface for ifname %s, index %d", __func__
,
884 ifname
, ifam
->ifam_index
);
888 if (ifnlen
&& strncmp(ifp
->name
, ifname
, INTERFACE_NAMSIZ
))
891 /* N.B. The info in ifa_msghdr does not tell us whether the RTA_BRD
892 field contains a broadcast address or a peer address, so we are
894 rely upon the interface type. */
895 if (if_is_pointopoint(ifp
))
896 SET_FLAG(flags
, ZEBRA_IFA_PEER
);
899 /* it might seem cute to grab the interface metric here, however
900 * we're processing an address update message, and so some systems
901 * (e.g. FBSD) dont bother to fill in ifam_metric. Disabled, but left
902 * in deliberately, as comment.
904 ifp
->metric
= ifam
->ifam_metric
;
907 /* Add connected address. */
908 switch (sockunion_family(&addr
)) {
910 if (ifam
->ifam_type
== RTM_NEWADDR
)
911 connected_add_ipv4(ifp
, flags
, &addr
.sin
.sin_addr
,
912 ip_masklen(mask
.sin
.sin_addr
),
914 (isalias
? ifname
: NULL
),
917 connected_delete_ipv4(ifp
, flags
, &addr
.sin
.sin_addr
,
918 ip_masklen(mask
.sin
.sin_addr
),
922 /* Unset interface index from link-local address when IPv6 stack
924 if (IN6_IS_ADDR_LINKLOCAL(&addr
.sin6
.sin6_addr
)) {
925 SET_IN6_LINKLOCAL_IFINDEX(addr
.sin6
.sin6_addr
, 0);
928 if (ifam
->ifam_type
== RTM_NEWADDR
)
929 connected_add_ipv6(ifp
, flags
, &addr
.sin6
.sin6_addr
,
931 ip6_masklen(mask
.sin6
.sin6_addr
),
932 (isalias
? ifname
: NULL
),
935 connected_delete_ipv6(ifp
, &addr
.sin6
.sin6_addr
, NULL
,
936 ip6_masklen(mask
.sin6
.sin6_addr
));
939 /* Unsupported family silently ignore... */
943 /* Check interface flag for implicit up of the interface. */
947 /* In addition to lacking IFANNOUNCE, on SUNOS IFF_UP is strange.
948 * See comments for SUNOS_5 in interface.c::if_flags_mangle.
950 * Here we take care of case where the real IFF_UP was previously
951 * unset (as kept in struct zebra_if.primary_state) and the mangled
952 * IFF_UP (ie IFF_UP set || listcount(connected) has now transitioned
953 * to unset due to the lost non-primary address having DELADDR'd.
955 * we must delete the interface, because in between here and next
956 * event for this interface-name the administrator could unplumb
957 * and replumb the interface.
960 if_delete_update(ifp
);
966 /* Interface function for reading kernel routing table information. */
967 static int rtm_read_mesg(struct rt_msghdr
*rtm
, union sockunion
*dest
,
968 union sockunion
*mask
, union sockunion
*gate
,
969 char *ifname
, short *ifnlen
)
974 /* Pnt points out socket data start point. */
975 pnt
= (caddr_t
)(rtm
+ 1);
976 end
= ((caddr_t
)rtm
) + rtm
->rtm_msglen
;
978 /* rt_msghdr version check. */
979 if (rtm
->rtm_version
!= RTM_VERSION
)
980 flog_warn(EC_ZEBRA_RTM_VERSION_MISMATCH
,
981 "Routing message version different %d should be %d."
982 "This may cause problem\n",
983 rtm
->rtm_version
, RTM_VERSION
);
985 /* Be sure structure is cleared */
986 memset(dest
, 0, sizeof(union sockunion
));
987 memset(gate
, 0, sizeof(union sockunion
));
988 memset(mask
, 0, sizeof(union sockunion
));
990 /* We fetch each socket variable into sockunion. */
991 /* We fetch each socket variable into sockunion. */
992 for (maskbit
= 1; maskbit
; maskbit
<<= 1) {
993 if ((maskbit
& rtm
->rtm_addrs
) == 0)
998 pnt
+= rta_get(pnt
, dest
, sizeof(*dest
));
1001 pnt
+= rta_get(pnt
, gate
, sizeof(*gate
));
1004 pnt
+= rta_getattr(pnt
, mask
, sizeof(*mask
));
1007 pnt
+= rta_getsdlname(pnt
, ifname
, ifnlen
);
1011 pnt
+= rta_get(pnt
, NULL
, 0);
1016 zlog_warn("%s: overflow detected (pnt:%p end:%p)",
1017 __func__
, pnt
, end
);
1022 /* If there is netmask information set it's family same as
1023 destination family*/
1024 if (rtm
->rtm_addrs
& RTA_NETMASK
)
1025 mask
->sa
.sa_family
= dest
->sa
.sa_family
;
1027 /* Assert read up to the end of pointer. */
1029 zlog_debug("rtm_read() doesn't read all socket data.");
1031 return rtm
->rtm_flags
;
1034 void rtm_read(struct rt_msghdr
*rtm
)
1037 uint8_t zebra_flags
;
1038 union sockunion dest
, mask
, gate
;
1039 char ifname
[INTERFACE_NAMSIZ
+ 1];
1043 ifindex_t ifindex
= 0;
1049 /* Read destination and netmask and gateway from rtm message
1051 flags
= rtm_read_mesg(rtm
, &dest
, &mask
, &gate
, ifname
, &ifnlen
);
1052 if (!(flags
& RTF_DONE
))
1054 if (IS_ZEBRA_DEBUG_KERNEL
)
1055 zlog_debug("%s: got rtm of type %d (%s) addrs {%s}", __func__
,
1057 lookup_msg(rtm_type_str
, rtm
->rtm_type
, NULL
),
1058 rtatostr(rtm
->rtm_addrs
, fbuf
, sizeof(fbuf
)));
1060 #ifdef RTF_CLONED /*bsdi, netbsd 1.6*/
1061 if (flags
& RTF_CLONED
)
1064 #ifdef RTF_WASCLONED /*freebsd*/
1065 if (flags
& RTF_WASCLONED
)
1069 if ((rtm
->rtm_type
== RTM_ADD
|| rtm
->rtm_type
== RTM_CHANGE
)
1070 && !(flags
& RTF_UP
))
1073 /* This is connected route. */
1074 if (!(flags
& RTF_GATEWAY
))
1077 if (flags
& RTF_PROTO1
)
1078 SET_FLAG(zebra_flags
, ZEBRA_FLAG_SELFROUTE
);
1080 memset(&nh
, 0, sizeof(nh
));
1082 nh
.vrf_id
= VRF_DEFAULT
;
1083 /* This is a reject or blackhole route */
1084 if (flags
& RTF_REJECT
) {
1085 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
1086 nh
.bh_type
= BLACKHOLE_REJECT
;
1087 } else if (flags
& RTF_BLACKHOLE
) {
1088 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
1089 nh
.bh_type
= BLACKHOLE_NULL
;
1093 * Ignore our own messages.
1095 if (rtm
->rtm_type
!= RTM_GET
&& rtm
->rtm_pid
== pid
)
1098 if (dest
.sa
.sa_family
== AF_INET
) {
1101 p
.u
.prefix4
= dest
.sin
.sin_addr
;
1102 if (flags
& RTF_HOST
)
1103 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
1105 p
.prefixlen
= ip_masklen(mask
.sin
.sin_addr
);
1108 nh
.type
= NEXTHOP_TYPE_IPV4
;
1109 nh
.gate
.ipv4
= gate
.sin
.sin_addr
;
1111 } else if (dest
.sa
.sa_family
== AF_INET6
) {
1113 p
.family
= AF_INET6
;
1114 p
.u
.prefix6
= dest
.sin6
.sin6_addr
;
1115 if (flags
& RTF_HOST
)
1116 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
1118 p
.prefixlen
= ip6_masklen(mask
.sin6
.sin6_addr
);
1121 if (IN6_IS_ADDR_LINKLOCAL(&gate
.sin6
.sin6_addr
)) {
1122 ifindex
= IN6_LINKLOCAL_IFINDEX(gate
.sin6
.sin6_addr
);
1123 SET_IN6_LINKLOCAL_IFINDEX(gate
.sin6
.sin6_addr
, 0);
1128 nh
.type
= ifindex
? NEXTHOP_TYPE_IPV6_IFINDEX
1129 : NEXTHOP_TYPE_IPV6
;
1130 nh
.gate
.ipv6
= gate
.sin6
.sin6_addr
;
1131 nh
.ifindex
= ifindex
;
1137 * CHANGE: delete the old prefix, we have no further information
1138 * to specify the route really
1140 if (rtm
->rtm_type
== RTM_CHANGE
)
1141 rib_delete(afi
, SAFI_UNICAST
, VRF_DEFAULT
, ZEBRA_ROUTE_KERNEL
,
1142 0, zebra_flags
, &p
, NULL
, NULL
, RT_TABLE_MAIN
,
1144 if (rtm
->rtm_type
== RTM_GET
|| rtm
->rtm_type
== RTM_ADD
1145 || rtm
->rtm_type
== RTM_CHANGE
)
1146 rib_add(afi
, SAFI_UNICAST
, VRF_DEFAULT
, ZEBRA_ROUTE_KERNEL
, 0,
1147 zebra_flags
, &p
, NULL
, &nh
, RT_TABLE_MAIN
, 0, 0, 0, 0);
1149 rib_delete(afi
, SAFI_UNICAST
, VRF_DEFAULT
, ZEBRA_ROUTE_KERNEL
,
1150 0, zebra_flags
, &p
, NULL
, &nh
, RT_TABLE_MAIN
,
1154 /* Interface function for the kernel routing table updates. Support
1155 * for RTM_CHANGE will be needed.
1156 * Exported only for rt_socket.c
1158 int rtm_write(int message
, union sockunion
*dest
, union sockunion
*mask
,
1159 union sockunion
*gate
, union sockunion
*mpls
, unsigned int index
,
1160 enum blackhole_type bh_type
, int metric
)
1164 struct interface
*ifp
;
1166 /* Sequencial number of routing message. */
1167 static int msg_seq
= 0;
1169 /* Struct of rt_msghdr and buffer for storing socket's data. */
1171 struct rt_msghdr rtm
;
1175 if (dplane_routing_sock
< 0)
1176 return ZEBRA_ERR_EPERM
;
1178 /* Clear and set rt_msghdr values */
1179 memset(&msg
, 0, sizeof(struct rt_msghdr
));
1180 msg
.rtm
.rtm_version
= RTM_VERSION
;
1181 msg
.rtm
.rtm_type
= message
;
1182 msg
.rtm
.rtm_seq
= msg_seq
++;
1183 msg
.rtm
.rtm_addrs
= RTA_DST
;
1184 msg
.rtm
.rtm_addrs
|= RTA_GATEWAY
;
1185 msg
.rtm
.rtm_flags
= RTF_UP
;
1187 msg
.rtm
.rtm_flags
|= RTF_MPATH
;
1188 msg
.rtm
.rtm_fmask
= RTF_MPLS
;
1190 msg
.rtm
.rtm_index
= index
;
1193 msg
.rtm
.rtm_rmx
.rmx_hopcount
= metric
;
1194 msg
.rtm
.rtm_inits
|= RTV_HOPCOUNT
;
1197 ifp
= if_lookup_by_index(index
, VRF_DEFAULT
);
1199 if (gate
&& (message
== RTM_ADD
|| message
== RTM_CHANGE
))
1200 msg
.rtm
.rtm_flags
|= RTF_GATEWAY
;
1202 /* When RTF_CLONING is unavailable on BSD, should we set some
1203 * other flag instead?
1206 if (!gate
&& (message
== RTM_ADD
|| message
== RTM_CHANGE
) && ifp
1207 && (ifp
->flags
& IFF_POINTOPOINT
) == 0)
1208 msg
.rtm
.rtm_flags
|= RTF_CLONING
;
1209 #endif /* RTF_CLONING */
1211 /* If no protocol specific gateway is specified, use link
1212 address for gateway. */
1215 char dest_buf
[INET_ADDRSTRLEN
] = "NULL",
1216 mask_buf
[INET_ADDRSTRLEN
] = "255.255.255.255";
1218 inet_ntop(AF_INET
, &dest
->sin
.sin_addr
,
1219 dest_buf
, INET_ADDRSTRLEN
);
1221 inet_ntop(AF_INET
, &mask
->sin
.sin_addr
,
1222 mask_buf
, INET_ADDRSTRLEN
);
1224 EC_ZEBRA_RTM_NO_GATEWAY
,
1225 "%s: %s/%s: gate == NULL and no gateway found for ifindex %d",
1226 __func__
, dest_buf
, mask_buf
, index
);
1229 gate
= (union sockunion
*)&((struct zebra_if
*)ifp
->info
)->sdl
;
1233 msg
.rtm
.rtm_addrs
|= RTA_NETMASK
;
1234 else if (message
== RTM_ADD
|| message
== RTM_CHANGE
)
1235 msg
.rtm
.rtm_flags
|= RTF_HOST
;
1239 msg
.rtm
.rtm_addrs
|= RTA_SRC
;
1240 msg
.rtm
.rtm_flags
|= RTF_MPLS
;
1242 if (mpls
->smpls
.smpls_label
1243 != htonl(MPLS_LABEL_IMPLICIT_NULL
<< MPLS_LABEL_OFFSET
))
1244 msg
.rtm
.rtm_mpls
= MPLS_OP_PUSH
;
1248 /* Tagging route with flags */
1249 msg
.rtm
.rtm_flags
|= (RTF_PROTO1
);
1252 case BLACKHOLE_UNSPEC
:
1254 case BLACKHOLE_REJECT
:
1255 msg
.rtm
.rtm_flags
|= RTF_REJECT
;
1258 msg
.rtm
.rtm_flags
|= RTF_BLACKHOLE
;
1263 #define SOCKADDRSET(X, R) \
1264 if (msg.rtm.rtm_addrs & (R)) { \
1265 int len = SAROUNDUP(X); \
1266 memcpy(pnt, (caddr_t)(X), len); \
1270 pnt
= (caddr_t
)msg
.buf
;
1272 /* Write each socket data into rtm message buffer */
1273 SOCKADDRSET(dest
, RTA_DST
);
1274 SOCKADDRSET(gate
, RTA_GATEWAY
);
1275 SOCKADDRSET(mask
, RTA_NETMASK
);
1277 SOCKADDRSET(mpls
, RTA_SRC
);
1280 msg
.rtm
.rtm_msglen
= pnt
- (caddr_t
)&msg
;
1282 ret
= write(dplane_routing_sock
, &msg
, msg
.rtm
.rtm_msglen
);
1284 if (ret
!= msg
.rtm
.rtm_msglen
) {
1285 if (errno
== EEXIST
)
1286 return ZEBRA_ERR_RTEXIST
;
1287 if (errno
== ENETUNREACH
)
1288 return ZEBRA_ERR_RTUNREACH
;
1290 return ZEBRA_ERR_RTNOEXIST
;
1292 flog_err_sys(EC_LIB_SOCKET
, "%s: write : %s (%d)", __func__
,
1293 safe_strerror(errno
), errno
);
1294 return ZEBRA_ERR_KERNEL
;
1296 return ZEBRA_ERR_NOERROR
;
1301 #include "zebra/zserv.h"
1303 /* For debug purpose. */
1304 static void rtmsg_debug(struct rt_msghdr
*rtm
)
1308 zlog_debug("Kernel: Len: %d Type: %s", rtm
->rtm_msglen
,
1309 lookup_msg(rtm_type_str
, rtm
->rtm_type
, NULL
));
1310 rtm_flag_dump(rtm
->rtm_flags
);
1311 zlog_debug("Kernel: message seq %d", rtm
->rtm_seq
);
1312 zlog_debug("Kernel: pid %lld, rtm_addrs {%s}", (long long)rtm
->rtm_pid
,
1313 rtatostr(rtm
->rtm_addrs
, fbuf
, sizeof(fbuf
)));
1316 /* This is pretty gross, better suggestions welcome -- mhandler */
1319 #define RTAX_MAX RTA_NUMBITS
1322 #endif /* RTA_NUMBITS */
1323 #endif /* RTAX_MAX */
1325 /* Kernel routing table and interface updates via routing socket. */
1326 static int kernel_read(struct thread
*thread
)
1330 struct rt_msghdr
*rtm
;
1333 * This must be big enough for any message the kernel might send.
1334 * Rather than determining how many sockaddrs of what size might be
1335 * in each particular message, just use RTAX_MAX of sockaddr_storage
1336 * for each. Note that the sockaddrs must be after each message
1337 * definition, or rather after whichever happens to be the largest,
1338 * since the buffer needs to be big enough for a message and the
1339 * sockaddrs together.
1342 /* Routing information. */
1344 struct rt_msghdr rtm
;
1345 struct sockaddr_storage addr
[RTAX_MAX
];
1348 /* Interface information. */
1350 struct if_msghdr ifm
;
1351 struct sockaddr_storage addr
[RTAX_MAX
];
1354 /* Interface address information. */
1356 struct ifa_msghdr ifa
;
1357 struct sockaddr_storage addr
[RTAX_MAX
];
1360 #ifdef RTM_IFANNOUNCE
1361 /* Interface arrival/departure */
1363 struct if_announcemsghdr ifan
;
1364 struct sockaddr_storage addr
[RTAX_MAX
];
1366 #endif /* RTM_IFANNOUNCE */
1370 /* Fetch routing socket. */
1371 sock
= THREAD_FD(thread
);
1373 nbytes
= read(sock
, &buf
, sizeof buf
);
1376 if (nbytes
< 0 && errno
!= EWOULDBLOCK
&& errno
!= EAGAIN
)
1377 flog_err_sys(EC_LIB_SOCKET
, "routing socket error: %s",
1378 safe_strerror(errno
));
1382 thread_add_read(zrouter
.master
, kernel_read
, NULL
, sock
, NULL
);
1384 if (IS_ZEBRA_DEBUG_KERNEL
)
1385 rtmsg_debug(&buf
.r
.rtm
);
1390 * Ensure that we didn't drop any data, so that processing routines
1391 * can assume they have the whole message.
1393 if (rtm
->rtm_msglen
!= nbytes
) {
1395 "kernel_read: rtm->rtm_msglen %d, nbytes %d, type %d\n",
1396 rtm
->rtm_msglen
, nbytes
, rtm
->rtm_type
);
1400 switch (rtm
->rtm_type
) {
1407 ifm_read(&buf
.im
.ifm
);
1411 ifam_read(&buf
.ia
.ifa
);
1413 #ifdef RTM_IFANNOUNCE
1414 case RTM_IFANNOUNCE
:
1415 ifan_read(&buf
.ian
.ifan
);
1417 #endif /* RTM_IFANNOUNCE */
1419 if (IS_ZEBRA_DEBUG_KERNEL
)
1420 zlog_debug("Unprocessed RTM_type: %d", rtm
->rtm_type
);
1426 /* Make routing socket. */
1427 static void routing_socket(struct zebra_ns
*zns
)
1429 frr_elevate_privs(&zserv_privs
) {
1430 routing_sock
= ns_socket(AF_ROUTE
, SOCK_RAW
, 0, zns
->ns_id
);
1432 dplane_routing_sock
=
1433 ns_socket(AF_ROUTE
, SOCK_RAW
, 0, zns
->ns_id
);
1436 if (routing_sock
< 0) {
1437 flog_err_sys(EC_LIB_SOCKET
, "Can't init kernel routing socket");
1441 if (dplane_routing_sock
< 0) {
1442 flog_err_sys(EC_LIB_SOCKET
,
1443 "Can't init kernel dataplane routing socket");
1447 /* XXX: Socket should be NONBLOCK, however as we currently
1448 * discard failed writes, this will lead to inconsistencies.
1449 * For now, socket must be blocking.
1451 /*if (fcntl (routing_sock, F_SETFL, O_NONBLOCK) < 0)
1452 zlog_warn ("Can't set O_NONBLOCK to routing socket");*/
1454 /* kernel_read needs rewrite. */
1455 thread_add_read(zrouter
.master
, kernel_read
, NULL
, routing_sock
, NULL
);
1458 /* Exported interface function. This function simply calls
1459 routing_socket (). */
1460 void kernel_init(struct zebra_ns
*zns
)
1462 routing_socket(zns
);
1465 void kernel_terminate(struct zebra_ns
*zns
, bool complete
)
1470 #endif /* !HAVE_NETLINK */