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_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_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 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 */
299 * Handle struct if_msghdr obtained from reading routing socket or
300 * sysctl (from interface_list). There may or may not be sockaddrs
301 * present after the header.
304 ifm_read (struct if_msghdr
*ifm
)
306 struct interface
*ifp
= NULL
;
307 char ifname
[IFNAMSIZ
];
311 /* terminate ifname at head (for strnlen) and tail (for safety) */
312 ifname
[IFNAMSIZ
- 1] = '\0';
314 /* paranoia: sanity check structure */
315 if (ifm
->ifm_msglen
< sizeof(struct if_msghdr
))
317 zlog_err ("ifm_read: ifm->ifm_msglen %d too short\n",
323 * Check for a sockaddr_dl following the message. First, point to
324 * where a socakddr might be if one follows the message.
326 cp
= (void *)(ifm
+ 1);
330 * XXX This behavior should be narrowed to only the kernel versions
331 * for which the structures returned do not match the headers.
333 * if_msghdr_t on 64 bit kernels in Solaris 9 and earlier versions
334 * is 12 bytes larger than the 32 bit version.
336 if (((struct sockaddr
*) cp
)->sa_family
== AF_UNSPEC
)
340 RTA_ADDR_GET (NULL
, RTA_DST
, ifm
->ifm_addrs
, cp
);
341 RTA_ADDR_GET (NULL
, RTA_GATEWAY
, ifm
->ifm_addrs
, cp
);
342 RTA_ATTR_GET (NULL
, RTA_NETMASK
, ifm
->ifm_addrs
, cp
);
343 RTA_ADDR_GET (NULL
, RTA_GENMASK
, ifm
->ifm_addrs
, cp
);
344 RTA_NAME_GET (ifname
, RTA_IFP
, ifm
->ifm_addrs
, cp
, ifnlen
);
345 RTA_ADDR_GET (NULL
, RTA_IFA
, ifm
->ifm_addrs
, cp
);
346 RTA_ADDR_GET (NULL
, RTA_AUTHOR
, ifm
->ifm_addrs
, cp
);
347 RTA_ADDR_GET (NULL
, RTA_BRD
, ifm
->ifm_addrs
, cp
);
349 if (IS_ZEBRA_DEBUG_KERNEL
)
350 zlog_debug ("%s: sdl ifname %s", __func__
, (ifnlen
? ifname
: "(nil)"));
353 * Look up on ifindex first, because ifindices are the primary handle for
354 * interfaces across the user/kernel boundary, for most systems. (Some
355 * messages, such as up/down status changes on NetBSD, do not include a
358 if ( (ifp
= if_lookup_by_index (ifm
->ifm_index
)) != NULL
)
360 /* we have an ifp, verify that the name matches as some systems,
361 * eg Solaris, have a 1:many association of ifindex:ifname
362 * if they dont match, we dont have the correct ifp and should
363 * set it back to NULL to let next check do lookup by name
365 if (ifnlen
&& (strncmp (ifp
->name
, ifname
, IFNAMSIZ
) != 0) )
367 if (IS_ZEBRA_DEBUG_KERNEL
)
368 zlog_debug ("%s: ifp name %s doesnt match sdl name %s",
369 __func__
, ifp
->name
, ifname
);
375 * If we dont have an ifp, try looking up by name. Particularly as some
376 * systems (Solaris) have a 1:many mapping of ifindex:ifname - the ifname
377 * is therefore our unique handle to that interface.
379 * Interfaces specified in the configuration file for which the ifindex
380 * has not been determined will have ifindex == IFINDEX_INTERNAL, and such
381 * interfaces are found by this search, and then their ifindex values can
384 if ( (ifp
== NULL
) && ifnlen
)
385 ifp
= if_lookup_by_name (ifname
);
388 * If ifp still does not exist or has an invalid index (IFINDEX_INTERNAL),
389 * create or fill in an interface.
391 if ((ifp
== NULL
) || (ifp
->ifindex
== IFINDEX_INTERNAL
))
394 * To create or fill in an interface, a sockaddr_dl (via
395 * RTA_IFP) is required.
399 zlog_warn ("Interface index %d (new) missing ifname\n",
404 #ifndef RTM_IFANNOUNCE
405 /* Down->Down interface should be ignored here.
406 * See further comment below.
408 if (!CHECK_FLAG (ifm
->ifm_flags
, IFF_UP
))
410 #endif /* !RTM_IFANNOUNCE */
414 /* Interface that zebra was not previously aware of, so create. */
415 ifp
= if_create (ifname
, ifnlen
);
416 if (IS_ZEBRA_DEBUG_KERNEL
)
417 zlog_debug ("%s: creating ifp for ifindex %d",
418 __func__
, ifm
->ifm_index
);
421 if (IS_ZEBRA_DEBUG_KERNEL
)
422 zlog_debug ("%s: updated/created ifp, ifname %s, ifindex %d",
423 __func__
, ifp
->name
, ifp
->ifindex
);
425 * Fill in newly created interface structure, or larval
426 * structure with ifindex IFINDEX_INTERNAL.
428 ifp
->ifindex
= ifm
->ifm_index
;
429 if_flags_update (ifp
, ifm
->ifm_flags
);
430 #if defined(__bsdi__)
431 if_kvm_get_mtu (ifp
);
434 #endif /* __bsdi__ */
441 * Interface structure exists. Adjust stored flags from
442 * notification. If interface has up->down or down->up
443 * transition, call state change routines (to adjust routes,
444 * notify routing daemons, etc.). (Other flag changes are stored
445 * but apparently do not trigger action.)
448 if (ifp
->ifindex
!= ifm
->ifm_index
)
450 zlog_warn ("%s: index mismatch, ifname %s, ifp index %d, "
452 __func__
, ifp
->name
, ifp
->ifindex
, ifm
->ifm_index
);
456 /* update flags and handle operative->inoperative transition, if any */
457 if_flags_update (ifp
, ifm
->ifm_flags
);
459 #ifndef RTM_IFANNOUNCE
462 /* No RTM_IFANNOUNCE on this platform, so we can never
463 * distinguish between ~IFF_UP and delete. We must presume
464 * it has been deleted.
465 * Eg, Solaris will not notify us of unplumb.
467 * XXX: Fixme - this should be runtime detected
468 * So that a binary compiled on a system with IFANNOUNCE
469 * will still behave correctly if run on a platform without
471 if_delete_update (ifp
);
473 #endif /* RTM_IFANNOUNCE */
476 #ifdef HAVE_NET_RT_IFLIST
477 ifp
->stats
= ifm
->ifm_data
;
478 #endif /* HAVE_NET_RT_IFLIST */
480 if (IS_ZEBRA_DEBUG_KERNEL
)
481 zlog_debug ("%s: interface %s index %d",
482 __func__
, ifp
->name
, ifp
->ifindex
);
487 /* Address read from struct ifa_msghdr. */
489 ifam_read_mesg (struct ifa_msghdr
*ifm
,
490 union sockunion
*addr
,
491 union sockunion
*mask
,
492 union sockunion
*brd
,
498 pnt
= (caddr_t
)(ifm
+ 1);
499 end
= ((caddr_t
)ifm
) + ifm
->ifam_msglen
;
501 /* Be sure structure is cleared */
502 memset (mask
, 0, sizeof (union sockunion
));
503 memset (addr
, 0, sizeof (union sockunion
));
504 memset (brd
, 0, sizeof (union sockunion
));
506 /* We fetch each socket variable into sockunion. */
507 RTA_ADDR_GET (NULL
, RTA_DST
, ifm
->ifam_addrs
, pnt
);
508 RTA_ADDR_GET (NULL
, RTA_GATEWAY
, ifm
->ifam_addrs
, pnt
);
509 RTA_ATTR_GET (mask
, RTA_NETMASK
, ifm
->ifam_addrs
, pnt
);
510 RTA_ADDR_GET (NULL
, RTA_GENMASK
, ifm
->ifam_addrs
, pnt
);
511 RTA_NAME_GET (ifname
, RTA_IFP
, ifm
->ifam_addrs
, pnt
, *ifnlen
);
512 RTA_ADDR_GET (addr
, RTA_IFA
, ifm
->ifam_addrs
, pnt
);
513 RTA_ADDR_GET (NULL
, RTA_AUTHOR
, ifm
->ifam_addrs
, pnt
);
514 RTA_ADDR_GET (brd
, RTA_BRD
, ifm
->ifam_addrs
, pnt
);
516 if (IS_ZEBRA_DEBUG_KERNEL
)
518 switch (sockunion_family(addr
))
522 char buf
[2][INET_ADDRSTRLEN
];
523 zlog_debug ("%s: ifindex %d, ifname %s, ifam_addrs 0x%x, "
524 "addr %s/%d broad %s",
525 __func__
, ifm
->ifam_index
,
526 (ifnlen
? ifname
: "(nil)"), ifm
->ifam_addrs
,
527 inet_ntop(AF_INET
,&addr
->sin
.sin_addr
,
528 buf
[0],sizeof(buf
[0])),
529 ip_masklen(mask
->sin
.sin_addr
),
530 inet_ntop(AF_INET
,&brd
->sin
.sin_addr
,
531 buf
[1],sizeof(buf
[1])));
537 char buf
[2][INET6_ADDRSTRLEN
];
538 zlog_debug ("%s: ifindex %d, ifname %s, ifam_addrs 0x%x, "
539 "addr %s/%d broad %s",
540 __func__
, ifm
->ifam_index
,
541 (ifnlen
? ifname
: "(nil)"), ifm
->ifam_addrs
,
542 inet_ntop(AF_INET6
,&addr
->sin6
.sin6_addr
,
543 buf
[0],sizeof(buf
[0])),
544 ip6_masklen(mask
->sin6
.sin6_addr
),
545 inet_ntop(AF_INET6
,&brd
->sin6
.sin6_addr
,
546 buf
[1],sizeof(buf
[1])));
549 #endif /* HAVE_IPV6 */
551 zlog_debug ("%s: ifindex %d, ifname %s, ifam_addrs 0x%x",
552 __func__
, ifm
->ifam_index
,
553 (ifnlen
? ifname
: "(nil)"), ifm
->ifam_addrs
);
558 /* Assert read up end point matches to end point */
560 zlog_warn ("ifam_read() does't read all socket data");
563 /* Interface's address information get. */
565 ifam_read (struct ifa_msghdr
*ifam
)
567 struct interface
*ifp
= NULL
;
568 union sockunion addr
, mask
, brd
;
569 char ifname
[INTERFACE_NAMSIZ
];
573 ifname
[0] = ifname
[INTERFACE_NAMSIZ
- 1] = '\0';
575 /* Allocate and read address information. */
576 ifam_read_mesg (ifam
, &addr
, &mask
, &brd
, ifname
, &ifnlen
);
578 if ((ifp
= if_lookup_by_index(ifam
->ifam_index
)) == NULL
)
580 zlog_warn ("%s: no interface for ifname %s, index %d",
581 __func__
, ifname
, ifam
->ifam_index
);
585 if (ifnlen
&& strncmp (ifp
->name
, ifname
, INTERFACE_NAMSIZ
))
588 ifp
->metric
= ifam
->ifam_metric
;
590 /* Add connected address. */
591 switch (sockunion_family (&addr
))
594 if (ifam
->ifam_type
== RTM_NEWADDR
)
595 connected_add_ipv4 (ifp
, 0, &addr
.sin
.sin_addr
,
596 ip_masklen (mask
.sin
.sin_addr
),
598 (isalias
? ifname
: NULL
));
600 connected_delete_ipv4 (ifp
, 0, &addr
.sin
.sin_addr
,
601 ip_masklen (mask
.sin
.sin_addr
),
606 /* Unset interface index from link-local address when IPv6 stack
608 if (IN6_IS_ADDR_LINKLOCAL (&addr
.sin6
.sin6_addr
))
609 SET_IN6_LINKLOCAL_IFINDEX (addr
.sin6
.sin6_addr
, 0);
611 if (ifam
->ifam_type
== RTM_NEWADDR
)
612 connected_add_ipv6 (ifp
, 0,
613 &addr
.sin6
.sin6_addr
,
614 ip6_masklen (mask
.sin6
.sin6_addr
),
616 (isalias
? ifname
: NULL
));
618 connected_delete_ipv6 (ifp
,
619 &addr
.sin6
.sin6_addr
,
620 ip6_masklen (mask
.sin6
.sin6_addr
),
621 &brd
.sin6
.sin6_addr
);
623 #endif /* HAVE_IPV6 */
625 /* Unsupported family silently ignore... */
629 /* Check interface flag for implicit up of the interface. */
633 /* In addition to lacking IFANNOUNCE, on SUNOS IFF_UP is strange.
634 * See comments for SUNOS_5 in interface.c::if_flags_mangle.
636 * Here we take care of case where the real IFF_UP was previously
637 * unset (as kept in struct zebra_if.primary_state) and the mangled
638 * IFF_UP (ie IFF_UP set || listcount(connected) has now transitioned
639 * to unset due to the lost non-primary address having DELADDR'd.
641 * we must delete the interface, because in between here and next
642 * event for this interface-name the administrator could unplumb
643 * and replumb the interface.
646 if_delete_update (ifp
);
652 /* Interface function for reading kernel routing table information. */
654 rtm_read_mesg (struct rt_msghdr
*rtm
,
655 union sockunion
*dest
,
656 union sockunion
*mask
,
657 union sockunion
*gate
,
663 /* Pnt points out socket data start point. */
664 pnt
= (caddr_t
)(rtm
+ 1);
665 end
= ((caddr_t
)rtm
) + rtm
->rtm_msglen
;
667 /* rt_msghdr version check. */
668 if (rtm
->rtm_version
!= RTM_VERSION
)
669 zlog (NULL
, LOG_WARNING
,
670 "Routing message version different %d should be %d."
671 "This may cause problem\n", rtm
->rtm_version
, RTM_VERSION
);
673 /* Be sure structure is cleared */
674 memset (dest
, 0, sizeof (union sockunion
));
675 memset (gate
, 0, sizeof (union sockunion
));
676 memset (mask
, 0, sizeof (union sockunion
));
678 /* We fetch each socket variable into sockunion. */
679 RTA_ADDR_GET (dest
, RTA_DST
, rtm
->rtm_addrs
, pnt
);
680 RTA_ADDR_GET (gate
, RTA_GATEWAY
, rtm
->rtm_addrs
, pnt
);
681 RTA_ATTR_GET (mask
, RTA_NETMASK
, rtm
->rtm_addrs
, pnt
);
682 RTA_ADDR_GET (NULL
, RTA_GENMASK
, rtm
->rtm_addrs
, pnt
);
683 RTA_NAME_GET (ifname
, RTA_IFP
, rtm
->rtm_addrs
, pnt
, *ifnlen
);
684 RTA_ADDR_GET (NULL
, RTA_IFA
, rtm
->rtm_addrs
, pnt
);
685 RTA_ADDR_GET (NULL
, RTA_AUTHOR
, rtm
->rtm_addrs
, pnt
);
686 RTA_ADDR_GET (NULL
, RTA_BRD
, rtm
->rtm_addrs
, pnt
);
688 /* If there is netmask information set it's family same as
690 if (rtm
->rtm_addrs
& RTA_NETMASK
)
691 mask
->sa
.sa_family
= dest
->sa
.sa_family
;
693 /* Assert read up to the end of pointer. */
695 zlog (NULL
, LOG_WARNING
, "rtm_read() does't read all socket data.");
697 return rtm
->rtm_flags
;
701 rtm_read (struct rt_msghdr
*rtm
)
705 union sockunion dest
, mask
, gate
;
706 char ifname
[INTERFACE_NAMSIZ
+ 1];
711 /* Discard self send message. */
712 if (rtm
->rtm_type
!= RTM_GET
713 && (rtm
->rtm_pid
== pid
|| rtm
->rtm_pid
== old_pid
))
716 /* Read destination and netmask and gateway from rtm message
718 flags
= rtm_read_mesg (rtm
, &dest
, &mask
, &gate
, ifname
, &ifnlen
);
720 #ifdef RTF_CLONED /*bsdi, netbsd 1.6*/
721 if (flags
& RTF_CLONED
)
724 #ifdef RTF_WASCLONED /*freebsd*/
725 if (flags
& RTF_WASCLONED
)
729 if ((rtm
->rtm_type
== RTM_ADD
) && ! (flags
& RTF_UP
))
732 /* This is connected route. */
733 if (! (flags
& RTF_GATEWAY
))
736 if (flags
& RTF_PROTO1
)
737 SET_FLAG (zebra_flags
, ZEBRA_FLAG_SELFROUTE
);
739 /* This is persistent route. */
740 if (flags
& RTF_STATIC
)
741 SET_FLAG (zebra_flags
, ZEBRA_FLAG_STATIC
);
743 /* This is a reject or blackhole route */
744 if (flags
& RTF_REJECT
)
745 SET_FLAG (zebra_flags
, ZEBRA_FLAG_REJECT
);
746 if (flags
& RTF_BLACKHOLE
)
747 SET_FLAG (zebra_flags
, ZEBRA_FLAG_BLACKHOLE
);
749 if (dest
.sa
.sa_family
== AF_INET
)
751 struct prefix_ipv4 p
;
754 p
.prefix
= dest
.sin
.sin_addr
;
755 if (flags
& RTF_HOST
)
756 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
758 p
.prefixlen
= ip_masklen (mask
.sin
.sin_addr
);
760 /* Change, delete the old prefix, we have no further information
761 * to specify the route really
763 if (rtm
->rtm_type
== RTM_CHANGE
)
764 rib_delete_ipv4 (ZEBRA_ROUTE_KERNEL
, zebra_flags
, &p
,
767 if (rtm
->rtm_type
== RTM_GET
768 || rtm
->rtm_type
== RTM_ADD
769 || rtm
->rtm_type
== RTM_CHANGE
)
770 rib_add_ipv4 (ZEBRA_ROUTE_KERNEL
, zebra_flags
,
771 &p
, &gate
.sin
.sin_addr
, 0, 0, 0, 0);
773 rib_delete_ipv4 (ZEBRA_ROUTE_KERNEL
, zebra_flags
,
774 &p
, &gate
.sin
.sin_addr
, 0, 0);
777 if (dest
.sa
.sa_family
== AF_INET6
)
779 struct prefix_ipv6 p
;
780 unsigned int ifindex
= 0;
783 p
.prefix
= dest
.sin6
.sin6_addr
;
784 if (flags
& RTF_HOST
)
785 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
787 p
.prefixlen
= ip6_masklen (mask
.sin6
.sin6_addr
);
790 if (IN6_IS_ADDR_LINKLOCAL (&gate
.sin6
.sin6_addr
))
792 ifindex
= IN6_LINKLOCAL_IFINDEX (gate
.sin6
.sin6_addr
);
793 SET_IN6_LINKLOCAL_IFINDEX (gate
.sin6
.sin6_addr
, 0);
797 /* CHANGE: delete the old prefix, we have no further information
798 * to specify the route really
800 if (rtm
->rtm_type
== RTM_CHANGE
)
801 rib_delete_ipv6 (ZEBRA_ROUTE_KERNEL
, zebra_flags
, &p
,
804 if (rtm
->rtm_type
== RTM_GET
805 || rtm
->rtm_type
== RTM_ADD
806 || rtm
->rtm_type
== RTM_CHANGE
)
807 rib_add_ipv6 (ZEBRA_ROUTE_KERNEL
, zebra_flags
,
808 &p
, &gate
.sin6
.sin6_addr
, ifindex
, 0, 0, 0);
810 rib_delete_ipv6 (ZEBRA_ROUTE_KERNEL
, zebra_flags
,
811 &p
, &gate
.sin6
.sin6_addr
, ifindex
, 0);
813 #endif /* HAVE_IPV6 */
816 /* Interface function for the kernel routing table updates. Support
817 * for RTM_CHANGE will be needed.
818 * Exported only for rt_socket.c
821 rtm_write (int message
,
822 union sockunion
*dest
,
823 union sockunion
*mask
,
824 union sockunion
*gate
,
831 struct interface
*ifp
;
833 /* Sequencial number of routing message. */
834 static int msg_seq
= 0;
836 /* Struct of rt_msghdr and buffer for storing socket's data. */
839 struct rt_msghdr rtm
;
843 if (routing_sock
< 0)
844 return ZEBRA_ERR_EPERM
;
846 /* Clear and set rt_msghdr values */
847 memset (&msg
, 0, sizeof (struct rt_msghdr
));
848 msg
.rtm
.rtm_version
= RTM_VERSION
;
849 msg
.rtm
.rtm_type
= message
;
850 msg
.rtm
.rtm_seq
= msg_seq
++;
851 msg
.rtm
.rtm_addrs
= RTA_DST
;
852 msg
.rtm
.rtm_addrs
|= RTA_GATEWAY
;
853 msg
.rtm
.rtm_flags
= RTF_UP
;
854 msg
.rtm
.rtm_index
= index
;
858 msg
.rtm
.rtm_rmx
.rmx_hopcount
= metric
;
859 msg
.rtm
.rtm_inits
|= RTV_HOPCOUNT
;
862 ifp
= if_lookup_by_index (index
);
864 if (gate
&& message
== RTM_ADD
)
865 msg
.rtm
.rtm_flags
|= RTF_GATEWAY
;
867 if (! gate
&& message
== RTM_ADD
&& ifp
&&
868 (ifp
->flags
& IFF_POINTOPOINT
) == 0)
869 msg
.rtm
.rtm_flags
|= RTF_CLONING
;
871 /* If no protocol specific gateway is specified, use link
872 address for gateway. */
877 zlog_warn ("no gateway found for interface index %d", index
);
880 gate
= (union sockunion
*) & ifp
->sdl
;
884 msg
.rtm
.rtm_addrs
|= RTA_NETMASK
;
885 else if (message
== RTM_ADD
)
886 msg
.rtm
.rtm_flags
|= RTF_HOST
;
888 /* Tagging route with flags */
889 msg
.rtm
.rtm_flags
|= (RTF_PROTO1
);
891 /* Additional flags. */
892 if (zebra_flags
& ZEBRA_FLAG_BLACKHOLE
)
893 msg
.rtm
.rtm_flags
|= RTF_BLACKHOLE
;
894 if (zebra_flags
& ZEBRA_FLAG_REJECT
)
895 msg
.rtm
.rtm_flags
|= RTF_REJECT
;
899 #define SOCKADDRSET(X,R) \
900 if (msg.rtm.rtm_addrs & (R)) \
902 int len = ROUNDUP ((X)->sa.sa_len); \
903 memcpy (pnt, (caddr_t)(X), len); \
907 #define SOCKADDRSET(X,R) \
908 if (msg.rtm.rtm_addrs & (R)) \
910 int len = SAROUNDUP (X); \
911 memcpy (pnt, (caddr_t)(X), len); \
914 #endif /* HAVE_SIN_LEN */
916 pnt
= (caddr_t
) msg
.buf
;
918 /* Write each socket data into rtm message buffer */
919 SOCKADDRSET (dest
, RTA_DST
);
920 SOCKADDRSET (gate
, RTA_GATEWAY
);
921 SOCKADDRSET (mask
, RTA_NETMASK
);
923 msg
.rtm
.rtm_msglen
= pnt
- (caddr_t
) &msg
;
925 ret
= write (routing_sock
, &msg
, msg
.rtm
.rtm_msglen
);
927 if (ret
!= msg
.rtm
.rtm_msglen
)
930 return ZEBRA_ERR_RTEXIST
;
931 if (errno
== ENETUNREACH
)
932 return ZEBRA_ERR_RTUNREACH
;
934 zlog_warn ("write : %s (%d)", safe_strerror (errno
), errno
);
942 #include "zebra/zserv.h"
944 /* For debug purpose. */
946 rtmsg_debug (struct rt_msghdr
*rtm
)
948 const char *type
= "Unknown";
951 for (mes
= rtm_type_str
; mes
->str
; mes
++)
952 if (mes
->key
== rtm
->rtm_type
)
958 zlog_debug ("Kernel: Len: %d Type: %s", rtm
->rtm_msglen
, type
);
959 rtm_flag_dump (rtm
->rtm_flags
);
960 zlog_debug ("Kernel: message seq %d", rtm
->rtm_seq
);
961 zlog_debug ("Kernel: pid %d, rtm_addrs 0x%x", rtm
->rtm_pid
, rtm
->rtm_addrs
);
964 /* This is pretty gross, better suggestions welcome -- mhandler */
967 #define RTAX_MAX RTA_NUMBITS
970 #endif /* RTA_NUMBITS */
971 #endif /* RTAX_MAX */
973 /* Kernel routing table and interface updates via routing socket. */
975 kernel_read (struct thread
*thread
)
979 struct rt_msghdr
*rtm
;
982 * This must be big enough for any message the kernel might send.
983 * Rather than determining how many sockaddrs of what size might be
984 * in each particular message, just use RTAX_MAX of sockaddr_storage
985 * for each. Note that the sockaddrs must be after each message
986 * definition, or rather after whichever happens to be the largest,
987 * since the buffer needs to be big enough for a message and the
988 * sockaddrs together.
992 /* Routing information. */
995 struct rt_msghdr rtm
;
996 struct sockaddr_storage addr
[RTAX_MAX
];
999 /* Interface information. */
1002 struct if_msghdr ifm
;
1003 struct sockaddr_storage addr
[RTAX_MAX
];
1006 /* Interface address information. */
1009 struct ifa_msghdr ifa
;
1010 struct sockaddr_storage addr
[RTAX_MAX
];
1013 #ifdef RTM_IFANNOUNCE
1014 /* Interface arrival/departure */
1017 struct if_announcemsghdr ifan
;
1018 struct sockaddr_storage addr
[RTAX_MAX
];
1020 #endif /* RTM_IFANNOUNCE */
1024 /* Fetch routing socket. */
1025 sock
= THREAD_FD (thread
);
1027 nbytes
= read (sock
, &buf
, sizeof buf
);
1031 if (nbytes
< 0 && errno
!= EWOULDBLOCK
&& errno
!= EAGAIN
)
1032 zlog_warn ("routing socket error: %s", safe_strerror (errno
));
1036 thread_add_read (zebrad
.master
, kernel_read
, NULL
, sock
);
1038 if (IS_ZEBRA_DEBUG_KERNEL
)
1039 rtmsg_debug (&buf
.r
.rtm
);
1044 * Ensure that we didn't drop any data, so that processing routines
1045 * can assume they have the whole message.
1047 if (rtm
->rtm_msglen
!= nbytes
)
1049 zlog_warn ("kernel_read: rtm->rtm_msglen %d, nbytes %d, type %d\n",
1050 rtm
->rtm_msglen
, nbytes
, rtm
->rtm_type
);
1054 switch (rtm
->rtm_type
)
1062 ifm_read (&buf
.im
.ifm
);
1066 ifam_read (&buf
.ia
.ifa
);
1068 #ifdef RTM_IFANNOUNCE
1069 case RTM_IFANNOUNCE
:
1070 ifan_read (&buf
.ian
.ifan
);
1072 #endif /* RTM_IFANNOUNCE */
1074 if (IS_ZEBRA_DEBUG_KERNEL
)
1075 zlog_debug("Unprocessed RTM_type: %d", rtm
->rtm_type
);
1081 /* Make routing socket. */
1083 routing_socket (void)
1085 if ( zserv_privs
.change (ZPRIVS_RAISE
) )
1086 zlog_err ("routing_socket: Can't raise privileges");
1088 routing_sock
= socket (AF_ROUTE
, SOCK_RAW
, 0);
1090 if (routing_sock
< 0)
1092 if ( zserv_privs
.change (ZPRIVS_LOWER
) )
1093 zlog_err ("routing_socket: Can't lower privileges");
1094 zlog_warn ("Can't init kernel routing socket");
1098 /* XXX: Socket should be NONBLOCK, however as we currently
1099 * discard failed writes, this will lead to inconsistencies.
1100 * For now, socket must be blocking.
1102 /*if (fcntl (routing_sock, F_SETFL, O_NONBLOCK) < 0)
1103 zlog_warn ("Can't set O_NONBLOCK to routing socket");*/
1105 if ( zserv_privs
.change (ZPRIVS_LOWER
) )
1106 zlog_err ("routing_socket: Can't lower privileges");
1108 /* kernel_read needs rewrite. */
1109 thread_add_read (zebrad
.master
, kernel_read
, NULL
, routing_sock
);
1112 /* Exported interface function. This function simply calls
1113 routing_socket (). */