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
17 * along with GNU Zebra; see the file COPYING. If not, write to the Free
18 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
26 #include "sockunion.h"
27 #include "connected.h"
36 #include "zebra/interface.h"
37 #include "zebra/zserv.h"
38 #include "zebra/debug.h"
39 #include "zebra/kernel_socket.h"
41 extern struct zebra_privs_t zserv_privs
;
42 extern struct zebra_t zebrad
;
45 * Given a sockaddr length, round it up to include pad bytes following
46 * it. Assumes the kernel pads to sizeof(long).
48 * XXX: why is ROUNDUP(0) sizeof(long)? 0 is an illegal sockaddr
49 * length anyway (< sizeof (struct sockaddr)), so this shouldn't
53 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
56 * Given a pointer (sockaddr or void *), return the number of bytes
57 * taken up by the sockaddr and any padding needed for alignment.
59 #if defined(HAVE_STRUCT_SOCKADDR_SA_LEN)
60 #define SAROUNDUP(X) ROUNDUP(((struct sockaddr *)(X))->sa_len)
61 #elif defined(HAVE_IPV6)
63 * One would hope all fixed-size structure definitions are aligned,
64 * but round them up nonetheless.
66 #define SAROUNDUP(X) \
67 (((struct sockaddr *)(X))->sa_family == AF_INET ? \
68 ROUNDUP(sizeof(struct sockaddr_in)):\
69 (((struct sockaddr *)(X))->sa_family == AF_INET6 ? \
70 ROUNDUP(sizeof(struct sockaddr_in6)) : \
71 (((struct sockaddr *)(X))->sa_family == AF_LINK ? \
72 ROUNDUP(sizeof(struct sockaddr_dl)) : sizeof(struct sockaddr))))
74 #define SAROUNDUP(X) \
75 (((struct sockaddr *)(X))->sa_family == AF_INET ? \
76 ROUNDUP(sizeof(struct sockaddr_in)):\
77 (((struct sockaddr *)(X))->sa_family == AF_LINK ? \
78 ROUNDUP(sizeof(struct sockaddr_dl)) : sizeof(struct sockaddr)))
79 #endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
81 /* We use an additional pointer in following, pdest, rather than (DEST)
82 * directly, because gcc will warn if the macro is expanded and DEST is NULL,
83 * complaining that memcpy is being passed a NULL value, despite the fact
84 * the if (NULL) makes it impossible.
86 #define RTA_ADDR_GET(DEST, RTA, RTMADDRS, PNT) \
87 if ((RTMADDRS) & (RTA)) \
89 void *pdest = (DEST); \
90 int len = SAROUNDUP ((PNT)); \
91 if ( ((DEST) != NULL) && \
92 af_check (((struct sockaddr *)(PNT))->sa_family)) \
93 memcpy (pdest, (PNT), len); \
96 #define RTA_ATTR_GET(DEST, RTA, RTMADDRS, PNT) \
97 if ((RTMADDRS) & (RTA)) \
99 void *pdest = (DEST); \
100 int len = SAROUNDUP ((PNT)); \
101 if ((DEST) != NULL) \
102 memcpy (pdest, (PNT), len); \
106 #define RTA_NAME_GET(DEST, RTA, RTMADDRS, PNT, LEN) \
107 if ((RTMADDRS) & (RTA)) \
109 u_char *pdest = (u_char *) (DEST); \
110 int len = SAROUNDUP ((PNT)); \
111 struct sockaddr_dl *sdl = (struct sockaddr_dl *)(PNT); \
112 if (IS_ZEBRA_DEBUG_KERNEL) \
113 zlog_debug ("%s: RTA_SDL_GET nlen %d, alen %d", \
114 __func__, sdl->sdl_nlen, sdl->sdl_alen); \
115 if ( ((DEST) != NULL) && (sdl->sdl_family == AF_LINK) \
116 && (sdl->sdl_nlen < IFNAMSIZ) && (sdl->sdl_nlen <= len) ) \
118 memcpy (pdest, sdl->sdl_data, sdl->sdl_nlen); \
119 pdest[sdl->sdl_nlen] = '\0'; \
120 (LEN) = sdl->sdl_nlen; \
128 /* Routing socket message types. */
129 const struct message rtm_type_str
[] =
131 {RTM_ADD
, "RTM_ADD"},
132 {RTM_DELETE
, "RTM_DELETE"},
133 {RTM_CHANGE
, "RTM_CHANGE"},
134 {RTM_GET
, "RTM_GET"},
135 {RTM_LOSING
, "RTM_LOSING"},
136 {RTM_REDIRECT
, "RTM_REDIRECT"},
137 {RTM_MISS
, "RTM_MISS"},
138 {RTM_LOCK
, "RTM_LOCK"},
140 {RTM_OLDADD
, "RTM_OLDADD"},
141 #endif /* RTM_OLDADD */
143 {RTM_OLDDEL
, "RTM_OLDDEL"},
144 #endif /* RTM_OLDDEL */
145 {RTM_RESOLVE
, "RTM_RESOLVE"},
146 {RTM_NEWADDR
, "RTM_NEWADDR"},
147 {RTM_DELADDR
, "RTM_DELADDR"},
148 {RTM_IFINFO
, "RTM_IFINFO"},
150 {RTM_OIFINFO
, "RTM_OIFINFO"},
151 #endif /* RTM_OIFINFO */
153 {RTM_NEWMADDR
, "RTM_NEWMADDR"},
154 #endif /* RTM_NEWMADDR */
156 {RTM_DELMADDR
, "RTM_DELMADDR"},
157 #endif /* RTM_DELMADDR */
158 #ifdef RTM_IFANNOUNCE
159 {RTM_IFANNOUNCE
, "RTM_IFANNOUNCE"},
160 #endif /* RTM_IFANNOUNCE */
164 struct message rtm_flag_str
[] =
167 {RTF_GATEWAY
, "GATEWAY"},
169 {RTF_REJECT
, "REJECT"},
170 {RTF_DYNAMIC
, "DYNAMIC"},
171 {RTF_MODIFIED
, "MODIFIED"},
175 #endif /* RTF_MASK */
176 {RTF_CLONING
, "CLONING"},
177 {RTF_XRESOLVE
, "XRESOLVE"},
178 {RTF_LLINFO
, "LLINFO"},
179 {RTF_STATIC
, "STATIC"},
180 {RTF_BLACKHOLE
, "BLACKHOLE"},
182 {RTF_PRIVATE
, "PRIVATE"},
183 #endif /* RTF_PRIVATE */
184 {RTF_PROTO1
, "PROTO1"},
185 {RTF_PROTO2
, "PROTO2"},
187 {RTF_PRCLONING
, "PRCLONING"},
188 #endif /* RTF_PRCLONING */
190 {RTF_WASCLONED
, "WASCLONED"},
191 #endif /* RTF_WASCLONED */
193 {RTF_PROTO3
, "PROTO3"},
194 #endif /* RTF_PROTO3 */
196 {RTF_PINNED
, "PINNED"},
197 #endif /* RTF_PINNED */
199 {RTF_LOCAL
, "LOCAL"},
200 #endif /* RTF_LOCAL */
202 {RTF_BROADCAST
, "BROADCAST"},
203 #endif /* RTF_BROADCAST */
205 {RTF_MULTICAST
, "MULTICAST"},
206 #endif /* RTF_MULTICAST */
208 {RTF_MULTIRT
, "MULTIRT"},
209 #endif /* RTF_MULTIRT */
211 {RTF_SETSRC
, "SETSRC"},
212 #endif /* RTF_SETSRC */
216 /* Kernel routing update socket. */
217 int routing_sock
= -1;
219 /* Yes I'm checking ugly routing socket behavior. */
222 /* Supported address family check. */
224 af_check (int family
)
226 if (family
== AF_INET
)
229 if (family
== AF_INET6
)
231 #endif /* HAVE_IPV6 */
235 /* Dump routing table flag for debug purpose. */
237 rtm_flag_dump (int flag
)
240 static char buf
[BUFSIZ
];
243 for (mes
= rtm_flag_str
; mes
->key
!= 0; mes
++)
247 strlcat (buf
, mes
->str
, BUFSIZ
);
248 strlcat (buf
, " ", BUFSIZ
);
251 zlog_debug ("Kernel: %s", buf
);
254 #ifdef RTM_IFANNOUNCE
255 /* Interface adding function */
257 ifan_read (struct if_announcemsghdr
*ifan
)
259 struct interface
*ifp
;
261 ifp
= if_lookup_by_index (ifan
->ifan_index
);
264 assert ( (ifp
->ifindex
== ifan
->ifan_index
)
265 || (ifp
->ifindex
== IFINDEX_INTERNAL
) );
268 || ((ifp
->ifindex
== IFINDEX_INTERNAL
)
269 && (ifan
->ifan_what
== IFAN_ARRIVAL
)) )
271 if (IS_ZEBRA_DEBUG_KERNEL
)
272 zlog_debug ("%s: creating interface for ifindex %d, name %s",
273 __func__
, ifan
->ifan_index
, ifan
->ifan_name
);
275 /* Create Interface */
276 ifp
= if_get_by_name_len(ifan
->ifan_name
,
277 strnlen(ifan
->ifan_name
,
278 sizeof(ifan
->ifan_name
)));
279 ifp
->ifindex
= ifan
->ifan_index
;
283 else if (ifp
!= NULL
&& ifan
->ifan_what
== IFAN_DEPARTURE
)
284 if_delete_update (ifp
);
290 if (IS_ZEBRA_DEBUG_KERNEL
)
291 zlog_debug ("%s: interface %s index %d",
292 __func__
, ifan
->ifan_name
, ifan
->ifan_index
);
296 #endif /* RTM_IFANNOUNCE */
298 #ifdef HAVE_BSD_LINK_DETECT
299 /* BSD link detect translation */
301 bsd_linkdetect_translate (struct if_msghdr
*ifm
)
303 if ((ifm
->ifm_data
.ifi_link_state
>= LINK_STATE_UP
) ||
304 (ifm
->ifm_data
.ifi_link_state
== LINK_STATE_UNKNOWN
))
305 SET_FLAG(ifm
->ifm_flags
, IFF_RUNNING
);
307 UNSET_FLAG(ifm
->ifm_flags
, IFF_RUNNING
);
309 #endif /* HAVE_BSD_LINK_DETECT */
312 * Handle struct if_msghdr obtained from reading routing socket or
313 * sysctl (from interface_list). There may or may not be sockaddrs
314 * present after the header.
317 ifm_read (struct if_msghdr
*ifm
)
319 struct interface
*ifp
= NULL
;
320 char ifname
[IFNAMSIZ
];
324 /* terminate ifname at head (for strnlen) and tail (for safety) */
325 ifname
[IFNAMSIZ
- 1] = '\0';
327 /* paranoia: sanity check structure */
328 if (ifm
->ifm_msglen
< sizeof(struct if_msghdr
))
330 zlog_err ("ifm_read: ifm->ifm_msglen %d too short\n",
336 * Check for a sockaddr_dl following the message. First, point to
337 * where a socakddr might be if one follows the message.
339 cp
= (void *)(ifm
+ 1);
343 * XXX This behavior should be narrowed to only the kernel versions
344 * for which the structures returned do not match the headers.
346 * if_msghdr_t on 64 bit kernels in Solaris 9 and earlier versions
347 * is 12 bytes larger than the 32 bit version.
349 if (((struct sockaddr
*) cp
)->sa_family
== AF_UNSPEC
)
353 RTA_ADDR_GET (NULL
, RTA_DST
, ifm
->ifm_addrs
, cp
);
354 RTA_ADDR_GET (NULL
, RTA_GATEWAY
, ifm
->ifm_addrs
, cp
);
355 RTA_ATTR_GET (NULL
, RTA_NETMASK
, ifm
->ifm_addrs
, cp
);
356 RTA_ADDR_GET (NULL
, RTA_GENMASK
, ifm
->ifm_addrs
, cp
);
357 RTA_NAME_GET (ifname
, RTA_IFP
, ifm
->ifm_addrs
, cp
, ifnlen
);
358 RTA_ADDR_GET (NULL
, RTA_IFA
, ifm
->ifm_addrs
, cp
);
359 RTA_ADDR_GET (NULL
, RTA_AUTHOR
, ifm
->ifm_addrs
, cp
);
360 RTA_ADDR_GET (NULL
, RTA_BRD
, ifm
->ifm_addrs
, cp
);
362 if (IS_ZEBRA_DEBUG_KERNEL
)
363 zlog_debug ("%s: sdl ifname %s", __func__
, (ifnlen
? ifname
: "(nil)"));
366 * Look up on ifindex first, because ifindices are the primary handle for
367 * interfaces across the user/kernel boundary, for most systems. (Some
368 * messages, such as up/down status changes on NetBSD, do not include a
371 if ( (ifp
= if_lookup_by_index (ifm
->ifm_index
)) != NULL
)
373 /* we have an ifp, verify that the name matches as some systems,
374 * eg Solaris, have a 1:many association of ifindex:ifname
375 * if they dont match, we dont have the correct ifp and should
376 * set it back to NULL to let next check do lookup by name
378 if (ifnlen
&& (strncmp (ifp
->name
, ifname
, IFNAMSIZ
) != 0) )
380 if (IS_ZEBRA_DEBUG_KERNEL
)
381 zlog_debug ("%s: ifp name %s doesnt match sdl name %s",
382 __func__
, ifp
->name
, ifname
);
388 * If we dont have an ifp, try looking up by name. Particularly as some
389 * systems (Solaris) have a 1:many mapping of ifindex:ifname - the ifname
390 * is therefore our unique handle to that interface.
392 * Interfaces specified in the configuration file for which the ifindex
393 * has not been determined will have ifindex == IFINDEX_INTERNAL, and such
394 * interfaces are found by this search, and then their ifindex values can
397 if ( (ifp
== NULL
) && ifnlen
)
398 ifp
= if_lookup_by_name (ifname
);
401 * If ifp still does not exist or has an invalid index (IFINDEX_INTERNAL),
402 * create or fill in an interface.
404 if ((ifp
== NULL
) || (ifp
->ifindex
== IFINDEX_INTERNAL
))
407 * To create or fill in an interface, a sockaddr_dl (via
408 * RTA_IFP) is required.
412 zlog_warn ("Interface index %d (new) missing ifname\n",
417 #ifndef RTM_IFANNOUNCE
418 /* Down->Down interface should be ignored here.
419 * See further comment below.
421 if (!CHECK_FLAG (ifm
->ifm_flags
, IFF_UP
))
423 #endif /* !RTM_IFANNOUNCE */
427 /* Interface that zebra was not previously aware of, so create. */
428 ifp
= if_create (ifname
, ifnlen
);
429 if (IS_ZEBRA_DEBUG_KERNEL
)
430 zlog_debug ("%s: creating ifp for ifindex %d",
431 __func__
, ifm
->ifm_index
);
434 if (IS_ZEBRA_DEBUG_KERNEL
)
435 zlog_debug ("%s: updated/created ifp, ifname %s, ifindex %d",
436 __func__
, ifp
->name
, ifp
->ifindex
);
438 * Fill in newly created interface structure, or larval
439 * structure with ifindex IFINDEX_INTERNAL.
441 ifp
->ifindex
= ifm
->ifm_index
;
443 #ifdef HAVE_BSD_LINK_DETECT /* translate BSD kernel msg for link-state */
444 bsd_linkdetect_translate(ifm
);
445 #endif /* HAVE_BSD_LINK_DETECT */
447 if_flags_update (ifp
, ifm
->ifm_flags
);
448 #if defined(__bsdi__)
449 if_kvm_get_mtu (ifp
);
452 #endif /* __bsdi__ */
459 * Interface structure exists. Adjust stored flags from
460 * notification. If interface has up->down or down->up
461 * transition, call state change routines (to adjust routes,
462 * notify routing daemons, etc.). (Other flag changes are stored
463 * but apparently do not trigger action.)
466 if (ifp
->ifindex
!= ifm
->ifm_index
)
468 zlog_warn ("%s: index mismatch, ifname %s, ifp index %d, "
470 __func__
, ifp
->name
, ifp
->ifindex
, ifm
->ifm_index
);
474 #ifdef HAVE_BSD_LINK_DETECT /* translate BSD kernel msg for link-state */
475 bsd_linkdetect_translate(ifm
);
476 #endif /* HAVE_BSD_LINK_DETECT */
478 /* update flags and handle operative->inoperative transition, if any */
479 if_flags_update (ifp
, ifm
->ifm_flags
);
481 #ifndef RTM_IFANNOUNCE
484 /* No RTM_IFANNOUNCE on this platform, so we can never
485 * distinguish between ~IFF_UP and delete. We must presume
486 * it has been deleted.
487 * Eg, Solaris will not notify us of unplumb.
489 * XXX: Fixme - this should be runtime detected
490 * So that a binary compiled on a system with IFANNOUNCE
491 * will still behave correctly if run on a platform without
493 if_delete_update (ifp
);
495 #endif /* RTM_IFANNOUNCE */
498 #if defined(__bsdi__)
499 if_kvm_get_mtu (ifp
);
502 #endif /* __bsdi__ */
507 #ifdef HAVE_NET_RT_IFLIST
508 ifp
->stats
= ifm
->ifm_data
;
509 #endif /* HAVE_NET_RT_IFLIST */
511 if (IS_ZEBRA_DEBUG_KERNEL
)
512 zlog_debug ("%s: interface %s index %d",
513 __func__
, ifp
->name
, ifp
->ifindex
);
518 /* Address read from struct ifa_msghdr. */
520 ifam_read_mesg (struct ifa_msghdr
*ifm
,
521 union sockunion
*addr
,
522 union sockunion
*mask
,
523 union sockunion
*brd
,
529 union sockunion gateway
;
531 pnt
= (caddr_t
)(ifm
+ 1);
532 end
= ((caddr_t
)ifm
) + ifm
->ifam_msglen
;
534 /* Be sure structure is cleared */
535 memset (mask
, 0, sizeof (union sockunion
));
536 memset (addr
, 0, sizeof (union sockunion
));
537 memset (brd
, 0, sizeof (union sockunion
));
538 memset (&dst
, 0, sizeof (union sockunion
));
539 memset (&gateway
, 0, sizeof (union sockunion
));
541 /* We fetch each socket variable into sockunion. */
542 RTA_ADDR_GET (&dst
, RTA_DST
, ifm
->ifam_addrs
, pnt
);
543 RTA_ADDR_GET (&gateway
, RTA_GATEWAY
, ifm
->ifam_addrs
, pnt
);
544 RTA_ATTR_GET (mask
, RTA_NETMASK
, ifm
->ifam_addrs
, pnt
);
545 RTA_ADDR_GET (NULL
, RTA_GENMASK
, ifm
->ifam_addrs
, pnt
);
546 RTA_NAME_GET (ifname
, RTA_IFP
, ifm
->ifam_addrs
, pnt
, *ifnlen
);
547 RTA_ADDR_GET (addr
, RTA_IFA
, ifm
->ifam_addrs
, pnt
);
548 RTA_ADDR_GET (NULL
, RTA_AUTHOR
, ifm
->ifam_addrs
, pnt
);
549 RTA_ADDR_GET (brd
, RTA_BRD
, ifm
->ifam_addrs
, pnt
);
551 if (IS_ZEBRA_DEBUG_KERNEL
)
553 switch (sockunion_family(addr
))
557 char buf
[4][INET_ADDRSTRLEN
];
558 zlog_debug ("%s: ifindex %d, ifname %s, ifam_addrs 0x%x, "
559 "ifam_flags 0x%x, addr %s/%d broad %s dst %s "
561 __func__
, ifm
->ifam_index
,
562 (ifnlen
? ifname
: "(nil)"), ifm
->ifam_addrs
,
564 inet_ntop(AF_INET
,&addr
->sin
.sin_addr
,
565 buf
[0],sizeof(buf
[0])),
566 ip_masklen(mask
->sin
.sin_addr
),
567 inet_ntop(AF_INET
,&brd
->sin
.sin_addr
,
568 buf
[1],sizeof(buf
[1])),
569 inet_ntop(AF_INET
,&dst
.sin
.sin_addr
,
570 buf
[2],sizeof(buf
[2])),
571 inet_ntop(AF_INET
,&gateway
.sin
.sin_addr
,
572 buf
[3],sizeof(buf
[3])));
578 char buf
[4][INET6_ADDRSTRLEN
];
579 zlog_debug ("%s: ifindex %d, ifname %s, ifam_addrs 0x%x, "
580 "ifam_flags 0x%x, addr %s/%d broad %s dst %s "
582 __func__
, ifm
->ifam_index
,
583 (ifnlen
? ifname
: "(nil)"), ifm
->ifam_addrs
,
585 inet_ntop(AF_INET6
,&addr
->sin6
.sin6_addr
,
586 buf
[0],sizeof(buf
[0])),
587 ip6_masklen(mask
->sin6
.sin6_addr
),
588 inet_ntop(AF_INET6
,&brd
->sin6
.sin6_addr
,
589 buf
[1],sizeof(buf
[1])),
590 inet_ntop(AF_INET6
,&dst
.sin6
.sin6_addr
,
591 buf
[2],sizeof(buf
[2])),
592 inet_ntop(AF_INET6
,&gateway
.sin6
.sin6_addr
,
593 buf
[3],sizeof(buf
[3])));
596 #endif /* HAVE_IPV6 */
598 zlog_debug ("%s: ifindex %d, ifname %s, ifam_addrs 0x%x",
599 __func__
, ifm
->ifam_index
,
600 (ifnlen
? ifname
: "(nil)"), ifm
->ifam_addrs
);
605 /* Assert read up end point matches to end point */
607 zlog_warn ("ifam_read() does't read all socket data");
610 /* Interface's address information get. */
612 ifam_read (struct ifa_msghdr
*ifam
)
614 struct interface
*ifp
= NULL
;
615 union sockunion addr
, mask
, brd
;
616 char ifname
[INTERFACE_NAMSIZ
];
621 ifname
[0] = ifname
[INTERFACE_NAMSIZ
- 1] = '\0';
623 /* Allocate and read address information. */
624 ifam_read_mesg (ifam
, &addr
, &mask
, &brd
, ifname
, &ifnlen
);
626 if ((ifp
= if_lookup_by_index(ifam
->ifam_index
)) == NULL
)
628 zlog_warn ("%s: no interface for ifname %s, index %d",
629 __func__
, ifname
, ifam
->ifam_index
);
633 if (ifnlen
&& strncmp (ifp
->name
, ifname
, INTERFACE_NAMSIZ
))
636 /* N.B. The info in ifa_msghdr does not tell us whether the RTA_BRD
637 field contains a broadcast address or a peer address, so we are forced to
638 rely upon the interface type. */
639 if (if_is_pointopoint(ifp
))
640 SET_FLAG(flags
, ZEBRA_IFA_PEER
);
643 /* it might seem cute to grab the interface metric here, however
644 * we're processing an address update message, and so some systems
645 * (e.g. FBSD) dont bother to fill in ifam_metric. Disabled, but left
646 * in deliberately, as comment.
648 ifp
->metric
= ifam
->ifam_metric
;
651 /* Add connected address. */
652 switch (sockunion_family (&addr
))
655 if (ifam
->ifam_type
== RTM_NEWADDR
)
656 connected_add_ipv4 (ifp
, flags
, &addr
.sin
.sin_addr
,
657 ip_masklen (mask
.sin
.sin_addr
),
659 (isalias
? ifname
: NULL
));
661 connected_delete_ipv4 (ifp
, flags
, &addr
.sin
.sin_addr
,
662 ip_masklen (mask
.sin
.sin_addr
),
667 /* Unset interface index from link-local address when IPv6 stack
669 if (IN6_IS_ADDR_LINKLOCAL (&addr
.sin6
.sin6_addr
))
670 SET_IN6_LINKLOCAL_IFINDEX (addr
.sin6
.sin6_addr
, 0);
672 if (ifam
->ifam_type
== RTM_NEWADDR
)
673 connected_add_ipv6 (ifp
, flags
, &addr
.sin6
.sin6_addr
,
674 ip6_masklen (mask
.sin6
.sin6_addr
),
676 (isalias
? ifname
: NULL
));
678 connected_delete_ipv6 (ifp
,
679 &addr
.sin6
.sin6_addr
,
680 ip6_masklen (mask
.sin6
.sin6_addr
),
681 &brd
.sin6
.sin6_addr
);
683 #endif /* HAVE_IPV6 */
685 /* Unsupported family silently ignore... */
689 /* Check interface flag for implicit up of the interface. */
693 /* In addition to lacking IFANNOUNCE, on SUNOS IFF_UP is strange.
694 * See comments for SUNOS_5 in interface.c::if_flags_mangle.
696 * Here we take care of case where the real IFF_UP was previously
697 * unset (as kept in struct zebra_if.primary_state) and the mangled
698 * IFF_UP (ie IFF_UP set || listcount(connected) has now transitioned
699 * to unset due to the lost non-primary address having DELADDR'd.
701 * we must delete the interface, because in between here and next
702 * event for this interface-name the administrator could unplumb
703 * and replumb the interface.
706 if_delete_update (ifp
);
712 /* Interface function for reading kernel routing table information. */
714 rtm_read_mesg (struct rt_msghdr
*rtm
,
715 union sockunion
*dest
,
716 union sockunion
*mask
,
717 union sockunion
*gate
,
723 /* Pnt points out socket data start point. */
724 pnt
= (caddr_t
)(rtm
+ 1);
725 end
= ((caddr_t
)rtm
) + rtm
->rtm_msglen
;
727 /* rt_msghdr version check. */
728 if (rtm
->rtm_version
!= RTM_VERSION
)
729 zlog (NULL
, LOG_WARNING
,
730 "Routing message version different %d should be %d."
731 "This may cause problem\n", rtm
->rtm_version
, RTM_VERSION
);
733 /* Be sure structure is cleared */
734 memset (dest
, 0, sizeof (union sockunion
));
735 memset (gate
, 0, sizeof (union sockunion
));
736 memset (mask
, 0, sizeof (union sockunion
));
738 /* We fetch each socket variable into sockunion. */
739 RTA_ADDR_GET (dest
, RTA_DST
, rtm
->rtm_addrs
, pnt
);
740 RTA_ADDR_GET (gate
, RTA_GATEWAY
, rtm
->rtm_addrs
, pnt
);
741 RTA_ATTR_GET (mask
, RTA_NETMASK
, rtm
->rtm_addrs
, pnt
);
742 RTA_ADDR_GET (NULL
, RTA_GENMASK
, rtm
->rtm_addrs
, pnt
);
743 RTA_NAME_GET (ifname
, RTA_IFP
, rtm
->rtm_addrs
, pnt
, *ifnlen
);
744 RTA_ADDR_GET (NULL
, RTA_IFA
, rtm
->rtm_addrs
, pnt
);
745 RTA_ADDR_GET (NULL
, RTA_AUTHOR
, rtm
->rtm_addrs
, pnt
);
746 RTA_ADDR_GET (NULL
, RTA_BRD
, rtm
->rtm_addrs
, pnt
);
748 /* If there is netmask information set it's family same as
750 if (rtm
->rtm_addrs
& RTA_NETMASK
)
751 mask
->sa
.sa_family
= dest
->sa
.sa_family
;
753 /* Assert read up to the end of pointer. */
755 zlog (NULL
, LOG_WARNING
, "rtm_read() does't read all socket data.");
757 return rtm
->rtm_flags
;
761 rtm_read (struct rt_msghdr
*rtm
)
765 union sockunion dest
, mask
, gate
;
766 char ifname
[INTERFACE_NAMSIZ
+ 1];
771 /* Read destination and netmask and gateway from rtm message
773 flags
= rtm_read_mesg (rtm
, &dest
, &mask
, &gate
, ifname
, &ifnlen
);
774 if (!(flags
& RTF_DONE
))
776 if (IS_ZEBRA_DEBUG_KERNEL
)
777 zlog_debug ("%s: got rtm of type %d (%s)", __func__
, rtm
->rtm_type
,
778 lookup (rtm_type_str
, rtm
->rtm_type
));
780 #ifdef RTF_CLONED /*bsdi, netbsd 1.6*/
781 if (flags
& RTF_CLONED
)
784 #ifdef RTF_WASCLONED /*freebsd*/
785 if (flags
& RTF_WASCLONED
)
789 if ((rtm
->rtm_type
== RTM_ADD
) && ! (flags
& RTF_UP
))
792 /* This is connected route. */
793 if (! (flags
& RTF_GATEWAY
))
796 if (flags
& RTF_PROTO1
)
797 SET_FLAG (zebra_flags
, ZEBRA_FLAG_SELFROUTE
);
799 /* This is persistent route. */
800 if (flags
& RTF_STATIC
)
801 SET_FLAG (zebra_flags
, ZEBRA_FLAG_STATIC
);
803 /* This is a reject or blackhole route */
804 if (flags
& RTF_REJECT
)
805 SET_FLAG (zebra_flags
, ZEBRA_FLAG_REJECT
);
806 if (flags
& RTF_BLACKHOLE
)
807 SET_FLAG (zebra_flags
, ZEBRA_FLAG_BLACKHOLE
);
809 if (dest
.sa
.sa_family
== AF_INET
)
811 struct prefix_ipv4 p
;
814 p
.prefix
= dest
.sin
.sin_addr
;
815 if (flags
& RTF_HOST
)
816 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
818 p
.prefixlen
= ip_masklen (mask
.sin
.sin_addr
);
820 /* Catch self originated messages and match them against our current RIB.
821 * At the same time, ignore unconfirmed messages, they should be tracked
822 * by rtm_write() and kernel_rtm_ipv4().
824 if (rtm
->rtm_type
!= RTM_GET
&& rtm
->rtm_pid
== pid
)
826 char buf
[INET_ADDRSTRLEN
], gate_buf
[INET_ADDRSTRLEN
];
828 if (! IS_ZEBRA_DEBUG_RIB
)
830 ret
= rib_lookup_ipv4_route (&p
, &gate
);
831 inet_ntop (AF_INET
, &p
.prefix
, buf
, INET_ADDRSTRLEN
);
832 switch (rtm
->rtm_type
)
837 /* The kernel notifies us about a new route in FIB created by us.
838 Do we have a correspondent entry in our RIB? */
841 case ZEBRA_RIB_NOTFOUND
:
842 zlog_debug ("%s: %s %s/%d: desync: RR isn't yet in RIB, while already in FIB",
843 __func__
, lookup (rtm_type_str
, rtm
->rtm_type
), buf
, p
.prefixlen
);
845 case ZEBRA_RIB_FOUND_CONNECTED
:
846 case ZEBRA_RIB_FOUND_NOGATE
:
847 inet_ntop (AF_INET
, &gate
.sin
.sin_addr
, gate_buf
, INET_ADDRSTRLEN
);
848 zlog_debug ("%s: %s %s/%d: desync: RR is in RIB, but gate differs (ours is %s)",
849 __func__
, lookup (rtm_type_str
, rtm
->rtm_type
), buf
, p
.prefixlen
, gate_buf
);
851 case ZEBRA_RIB_FOUND_EXACT
: /* RIB RR == FIB RR */
852 zlog_debug ("%s: %s %s/%d: done Ok",
853 __func__
, lookup (rtm_type_str
, rtm
->rtm_type
), buf
, p
.prefixlen
);
854 rib_lookup_and_dump (&p
);
860 /* The kernel notifies us about a route deleted by us. Do we still
861 have it in the RIB? Do we have anything instead? */
864 case ZEBRA_RIB_FOUND_EXACT
:
865 zlog_debug ("%s: %s %s/%d: desync: RR is still in RIB, while already not in FIB",
866 __func__
, lookup (rtm_type_str
, rtm
->rtm_type
), buf
, p
.prefixlen
);
867 rib_lookup_and_dump (&p
);
869 case ZEBRA_RIB_FOUND_CONNECTED
:
870 case ZEBRA_RIB_FOUND_NOGATE
:
871 zlog_debug ("%s: %s %s/%d: desync: RR is still in RIB, plus gate differs",
872 __func__
, lookup (rtm_type_str
, rtm
->rtm_type
), buf
, p
.prefixlen
);
873 rib_lookup_and_dump (&p
);
875 case ZEBRA_RIB_NOTFOUND
: /* RIB RR == FIB RR */
876 zlog_debug ("%s: %s %s/%d: done Ok",
877 __func__
, lookup (rtm_type_str
, rtm
->rtm_type
), buf
, p
.prefixlen
);
878 rib_lookup_and_dump (&p
);
884 zlog_debug ("%s: %s/%d: warning: loopback RTM of type %s received",
885 __func__
, buf
, p
.prefixlen
, lookup (rtm_type_str
, rtm
->rtm_type
));
890 /* Change, delete the old prefix, we have no further information
891 * to specify the route really
893 if (rtm
->rtm_type
== RTM_CHANGE
)
894 rib_delete_ipv4 (ZEBRA_ROUTE_KERNEL
, zebra_flags
, &p
,
897 if (rtm
->rtm_type
== RTM_GET
898 || rtm
->rtm_type
== RTM_ADD
899 || rtm
->rtm_type
== RTM_CHANGE
)
900 rib_add_ipv4 (ZEBRA_ROUTE_KERNEL
, zebra_flags
,
901 &p
, &gate
.sin
.sin_addr
, NULL
, 0, 0, 0, 0);
903 rib_delete_ipv4 (ZEBRA_ROUTE_KERNEL
, zebra_flags
,
904 &p
, &gate
.sin
.sin_addr
, 0, 0);
907 if (dest
.sa
.sa_family
== AF_INET6
)
909 /* One day we might have a debug section here like one in the
910 * IPv4 case above. Just ignore own messages at the moment.
912 if (rtm
->rtm_type
!= RTM_GET
&& rtm
->rtm_pid
== pid
)
914 struct prefix_ipv6 p
;
915 unsigned int ifindex
= 0;
918 p
.prefix
= dest
.sin6
.sin6_addr
;
919 if (flags
& RTF_HOST
)
920 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
922 p
.prefixlen
= ip6_masklen (mask
.sin6
.sin6_addr
);
925 if (IN6_IS_ADDR_LINKLOCAL (&gate
.sin6
.sin6_addr
))
927 ifindex
= IN6_LINKLOCAL_IFINDEX (gate
.sin6
.sin6_addr
);
928 SET_IN6_LINKLOCAL_IFINDEX (gate
.sin6
.sin6_addr
, 0);
932 /* CHANGE: delete the old prefix, we have no further information
933 * to specify the route really
935 if (rtm
->rtm_type
== RTM_CHANGE
)
936 rib_delete_ipv6 (ZEBRA_ROUTE_KERNEL
, zebra_flags
, &p
,
939 if (rtm
->rtm_type
== RTM_GET
940 || rtm
->rtm_type
== RTM_ADD
941 || rtm
->rtm_type
== RTM_CHANGE
)
942 rib_add_ipv6 (ZEBRA_ROUTE_KERNEL
, zebra_flags
,
943 &p
, &gate
.sin6
.sin6_addr
, ifindex
, 0, 0, 0);
945 rib_delete_ipv6 (ZEBRA_ROUTE_KERNEL
, zebra_flags
,
946 &p
, &gate
.sin6
.sin6_addr
, ifindex
, 0);
948 #endif /* HAVE_IPV6 */
951 /* Interface function for the kernel routing table updates. Support
952 * for RTM_CHANGE will be needed.
953 * Exported only for rt_socket.c
956 rtm_write (int message
,
957 union sockunion
*dest
,
958 union sockunion
*mask
,
959 union sockunion
*gate
,
966 struct interface
*ifp
;
968 /* Sequencial number of routing message. */
969 static int msg_seq
= 0;
971 /* Struct of rt_msghdr and buffer for storing socket's data. */
974 struct rt_msghdr rtm
;
978 if (routing_sock
< 0)
979 return ZEBRA_ERR_EPERM
;
981 /* Clear and set rt_msghdr values */
982 memset (&msg
, 0, sizeof (struct rt_msghdr
));
983 msg
.rtm
.rtm_version
= RTM_VERSION
;
984 msg
.rtm
.rtm_type
= message
;
985 msg
.rtm
.rtm_seq
= msg_seq
++;
986 msg
.rtm
.rtm_addrs
= RTA_DST
;
987 msg
.rtm
.rtm_addrs
|= RTA_GATEWAY
;
988 msg
.rtm
.rtm_flags
= RTF_UP
;
989 msg
.rtm
.rtm_index
= index
;
993 msg
.rtm
.rtm_rmx
.rmx_hopcount
= metric
;
994 msg
.rtm
.rtm_inits
|= RTV_HOPCOUNT
;
997 ifp
= if_lookup_by_index (index
);
999 if (gate
&& message
== RTM_ADD
)
1000 msg
.rtm
.rtm_flags
|= RTF_GATEWAY
;
1002 if (! gate
&& message
== RTM_ADD
&& ifp
&&
1003 (ifp
->flags
& IFF_POINTOPOINT
) == 0)
1004 msg
.rtm
.rtm_flags
|= RTF_CLONING
;
1006 /* If no protocol specific gateway is specified, use link
1007 address for gateway. */
1012 char dest_buf
[INET_ADDRSTRLEN
] = "NULL", mask_buf
[INET_ADDRSTRLEN
] = "255.255.255.255";
1014 inet_ntop (AF_INET
, &dest
->sin
.sin_addr
, dest_buf
, INET_ADDRSTRLEN
);
1016 inet_ntop (AF_INET
, &mask
->sin
.sin_addr
, mask_buf
, INET_ADDRSTRLEN
);
1017 zlog_warn ("%s: %s/%s: gate == NULL and no gateway found for ifindex %d",
1018 __func__
, dest_buf
, mask_buf
, index
);
1021 gate
= (union sockunion
*) & ifp
->sdl
;
1025 msg
.rtm
.rtm_addrs
|= RTA_NETMASK
;
1026 else if (message
== RTM_ADD
)
1027 msg
.rtm
.rtm_flags
|= RTF_HOST
;
1029 /* Tagging route with flags */
1030 msg
.rtm
.rtm_flags
|= (RTF_PROTO1
);
1032 /* Additional flags. */
1033 if (zebra_flags
& ZEBRA_FLAG_BLACKHOLE
)
1034 msg
.rtm
.rtm_flags
|= RTF_BLACKHOLE
;
1035 if (zebra_flags
& ZEBRA_FLAG_REJECT
)
1036 msg
.rtm
.rtm_flags
|= RTF_REJECT
;
1039 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
1040 #define SOCKADDRSET(X,R) \
1041 if (msg.rtm.rtm_addrs & (R)) \
1043 int len = ROUNDUP ((X)->sa.sa_len); \
1044 memcpy (pnt, (caddr_t)(X), len); \
1048 #define SOCKADDRSET(X,R) \
1049 if (msg.rtm.rtm_addrs & (R)) \
1051 int len = SAROUNDUP (X); \
1052 memcpy (pnt, (caddr_t)(X), len); \
1055 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
1057 pnt
= (caddr_t
) msg
.buf
;
1059 /* Write each socket data into rtm message buffer */
1060 SOCKADDRSET (dest
, RTA_DST
);
1061 SOCKADDRSET (gate
, RTA_GATEWAY
);
1062 SOCKADDRSET (mask
, RTA_NETMASK
);
1064 msg
.rtm
.rtm_msglen
= pnt
- (caddr_t
) &msg
;
1066 ret
= write (routing_sock
, &msg
, msg
.rtm
.rtm_msglen
);
1068 if (ret
!= msg
.rtm
.rtm_msglen
)
1070 if (errno
== EEXIST
)
1071 return ZEBRA_ERR_RTEXIST
;
1072 if (errno
== ENETUNREACH
)
1073 return ZEBRA_ERR_RTUNREACH
;
1075 return ZEBRA_ERR_RTNOEXIST
;
1077 zlog_warn ("%s: write : %s (%d)", __func__
, safe_strerror (errno
), errno
);
1078 return ZEBRA_ERR_KERNEL
;
1080 return ZEBRA_ERR_NOERROR
;
1085 #include "zebra/zserv.h"
1087 /* For debug purpose. */
1089 rtmsg_debug (struct rt_msghdr
*rtm
)
1091 zlog_debug ("Kernel: Len: %d Type: %s", rtm
->rtm_msglen
, lookup (rtm_type_str
, rtm
->rtm_type
));
1092 rtm_flag_dump (rtm
->rtm_flags
);
1093 zlog_debug ("Kernel: message seq %d", rtm
->rtm_seq
);
1094 zlog_debug ("Kernel: pid %d, rtm_addrs 0x%x", rtm
->rtm_pid
, rtm
->rtm_addrs
);
1097 /* This is pretty gross, better suggestions welcome -- mhandler */
1100 #define RTAX_MAX RTA_NUMBITS
1103 #endif /* RTA_NUMBITS */
1104 #endif /* RTAX_MAX */
1106 /* Kernel routing table and interface updates via routing socket. */
1108 kernel_read (struct thread
*thread
)
1112 struct rt_msghdr
*rtm
;
1115 * This must be big enough for any message the kernel might send.
1116 * Rather than determining how many sockaddrs of what size might be
1117 * in each particular message, just use RTAX_MAX of sockaddr_storage
1118 * for each. Note that the sockaddrs must be after each message
1119 * definition, or rather after whichever happens to be the largest,
1120 * since the buffer needs to be big enough for a message and the
1121 * sockaddrs together.
1125 /* Routing information. */
1128 struct rt_msghdr rtm
;
1129 struct sockaddr_storage addr
[RTAX_MAX
];
1132 /* Interface information. */
1135 struct if_msghdr ifm
;
1136 struct sockaddr_storage addr
[RTAX_MAX
];
1139 /* Interface address information. */
1142 struct ifa_msghdr ifa
;
1143 struct sockaddr_storage addr
[RTAX_MAX
];
1146 #ifdef RTM_IFANNOUNCE
1147 /* Interface arrival/departure */
1150 struct if_announcemsghdr ifan
;
1151 struct sockaddr_storage addr
[RTAX_MAX
];
1153 #endif /* RTM_IFANNOUNCE */
1157 /* Fetch routing socket. */
1158 sock
= THREAD_FD (thread
);
1160 nbytes
= read (sock
, &buf
, sizeof buf
);
1164 if (nbytes
< 0 && errno
!= EWOULDBLOCK
&& errno
!= EAGAIN
)
1165 zlog_warn ("routing socket error: %s", safe_strerror (errno
));
1169 thread_add_read (zebrad
.master
, kernel_read
, NULL
, sock
);
1171 if (IS_ZEBRA_DEBUG_KERNEL
)
1172 rtmsg_debug (&buf
.r
.rtm
);
1177 * Ensure that we didn't drop any data, so that processing routines
1178 * can assume they have the whole message.
1180 if (rtm
->rtm_msglen
!= nbytes
)
1182 zlog_warn ("kernel_read: rtm->rtm_msglen %d, nbytes %d, type %d\n",
1183 rtm
->rtm_msglen
, nbytes
, rtm
->rtm_type
);
1187 switch (rtm
->rtm_type
)
1195 ifm_read (&buf
.im
.ifm
);
1199 ifam_read (&buf
.ia
.ifa
);
1201 #ifdef RTM_IFANNOUNCE
1202 case RTM_IFANNOUNCE
:
1203 ifan_read (&buf
.ian
.ifan
);
1205 #endif /* RTM_IFANNOUNCE */
1207 if (IS_ZEBRA_DEBUG_KERNEL
)
1208 zlog_debug("Unprocessed RTM_type: %d", rtm
->rtm_type
);
1214 /* Make routing socket. */
1216 routing_socket (void)
1218 if ( zserv_privs
.change (ZPRIVS_RAISE
) )
1219 zlog_err ("routing_socket: Can't raise privileges");
1221 routing_sock
= socket (AF_ROUTE
, SOCK_RAW
, 0);
1223 if (routing_sock
< 0)
1225 if ( zserv_privs
.change (ZPRIVS_LOWER
) )
1226 zlog_err ("routing_socket: Can't lower privileges");
1227 zlog_warn ("Can't init kernel routing socket");
1231 /* XXX: Socket should be NONBLOCK, however as we currently
1232 * discard failed writes, this will lead to inconsistencies.
1233 * For now, socket must be blocking.
1235 /*if (fcntl (routing_sock, F_SETFL, O_NONBLOCK) < 0)
1236 zlog_warn ("Can't set O_NONBLOCK to routing socket");*/
1238 if ( zserv_privs
.change (ZPRIVS_LOWER
) )
1239 zlog_err ("routing_socket: Can't lower privileges");
1241 /* kernel_read needs rewrite. */
1242 thread_add_read (zebrad
.master
, kernel_read
, NULL
, routing_sock
);
1245 /* Exported interface function. This function simply calls
1246 routing_socket (). */