]> git.proxmox.com Git - mirror_frr.git/blob - zebra/kernel_socket.c
zebra: make RTF_LLINFO optional to fix FreeBSD
[mirror_frr.git] / zebra / kernel_socket.c
1 /* Kernel communication using routing socket.
2 * Copyright (C) 1999 Kunihiro Ishiguro
3 *
4 * This file is part of GNU Zebra.
5 *
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
9 * later version.
10 *
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.
15 *
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
19 * 02111-1307, USA.
20 */
21
22 #include <zebra.h>
23
24 #include "if.h"
25 #include "prefix.h"
26 #include "sockunion.h"
27 #include "connected.h"
28 #include "memory.h"
29 #include "ioctl.h"
30 #include "log.h"
31 #include "str.h"
32 #include "table.h"
33 #include "rib.h"
34 #include "privs.h"
35 #include "vrf.h"
36
37 #include "zebra/interface.h"
38 #include "zebra/zserv.h"
39 #include "zebra/debug.h"
40 #include "zebra/kernel_socket.h"
41 #include "zebra/rib.h"
42
43 extern struct zebra_privs_t zserv_privs;
44
45 /*
46 * Historically, the BSD routing socket has aligned data following a
47 * struct sockaddr to sizeof(long), which was 4 bytes on some
48 * platforms, and 8 bytes on others. NetBSD 6 changed the routing
49 * socket to align to sizeof(uint64_t), which is 8 bytes. OS X
50 * appears to align to sizeof(int), which is 4 bytes.
51 *
52 * Alignment of zero-sized sockaddrs is nonsensical, but historically
53 * BSD defines RT_ROUNDUP(0) to be the alignment interval (rather than
54 * 0). We follow this practice without questioning it, but it is a
55 * bug if quagga calls ROUNDUP with 0.
56 */
57
58 /*
59 * Because of these varying conventions, the only sane approach is for
60 * the <net/route.h> header to define some flavor of ROUNDUP macro.
61 */
62
63 #if defined(SA_SIZE)
64 /* SAROUNDUP is the only thing we need, and SA_SIZE provides that */
65 #define SAROUNDUP(a) SA_SIZE(a)
66 #else /* !SA_SIZE */
67
68 #if defined(RT_ROUNDUP)
69 #define ROUNDUP(a) RT_ROUNDUP(a)
70 #endif /* defined(RT_ROUNDUP) */
71
72 #if defined(SUNOS_5)
73 /* Solaris has struct sockaddr_in[6] definitions at 16 / 32 bytes size,
74 * so the whole concept doesn't really apply. */
75 #define ROUNDUP(a) (a)
76 #endif
77
78 /*
79 * If ROUNDUP has not yet been defined in terms of platform-provided
80 * defines, attempt to cope with heuristics.
81 */
82 #if !defined(ROUNDUP)
83
84 /*
85 * It's a bug for a platform not to define rounding/alignment for
86 * sockaddrs on the routing socket. This warning really is
87 * intentional, to provoke filing bug reports with operating systems
88 * that don't define RT_ROUNDUP or equivalent.
89 */
90 #warning "net/route.h does not define RT_ROUNDUP; making unwarranted assumptions!"
91
92 /* OS X (Xcode as of 2014-12) is known not to define RT_ROUNDUP */
93 #ifdef __APPLE__
94 #define ROUNDUP_TYPE int
95 #else
96 #define ROUNDUP_TYPE long
97 #endif
98
99 #define ROUNDUP(a) \
100 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(ROUNDUP_TYPE) - 1))) : sizeof(ROUNDUP_TYPE))
101
102 #endif /* defined(ROUNDUP) */
103
104 /*
105 * Given a pointer (sockaddr or void *), return the number of bytes
106 * taken up by the sockaddr and any padding needed for alignment.
107 */
108 #if defined(HAVE_STRUCT_SOCKADDR_SA_LEN)
109 #define SAROUNDUP(X) ROUNDUP(((struct sockaddr *)(X))->sa_len)
110 #elif defined(HAVE_IPV6)
111 /*
112 * One would hope all fixed-size structure definitions are aligned,
113 * but round them up nonetheless.
114 */
115 #define SAROUNDUP(X) \
116 (((struct sockaddr *)(X))->sa_family == AF_INET ? \
117 ROUNDUP(sizeof(struct sockaddr_in)):\
118 (((struct sockaddr *)(X))->sa_family == AF_INET6 ? \
119 ROUNDUP(sizeof(struct sockaddr_in6)) : \
120 (((struct sockaddr *)(X))->sa_family == AF_LINK ? \
121 ROUNDUP(sizeof(struct sockaddr_dl)) : sizeof(struct sockaddr))))
122 #else /* HAVE_IPV6 */
123 #define SAROUNDUP(X) \
124 (((struct sockaddr *)(X))->sa_family == AF_INET ? \
125 ROUNDUP(sizeof(struct sockaddr_in)):\
126 (((struct sockaddr *)(X))->sa_family == AF_LINK ? \
127 ROUNDUP(sizeof(struct sockaddr_dl)) : sizeof(struct sockaddr)))
128 #endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
129
130 #endif /* !SA_SIZE */
131
132 /*
133 * We use a call to an inline function to copy (PNT) to (DEST)
134 * 1. Calculating the length of the copy requires an #ifdef to determine
135 * if sa_len is a field and can't be used directly inside a #define
136 * 2. So the compiler doesn't complain when DEST is NULL, which is only true
137 * when we are skipping the copy and incrementing to the next SA
138 */
139 static inline void
140 rta_copy (union sockunion *dest, caddr_t src) {
141 int len;
142 if (!dest)
143 return;
144 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
145 len = (((struct sockaddr *)src)->sa_len > sizeof (*dest)) ?
146 sizeof (*dest) : ((struct sockaddr *)src)->sa_len ;
147 #else
148 len = (SAROUNDUP (src) > sizeof (*dest)) ?
149 sizeof (*dest) : SAROUNDUP (src) ;
150 #endif
151 memcpy (dest, src, len);
152 }
153
154 #define RTA_ADDR_GET(DEST, RTA, RTMADDRS, PNT) \
155 if ((RTMADDRS) & (RTA)) \
156 { \
157 int len = SAROUNDUP ((PNT)); \
158 if (af_check (((struct sockaddr *)(PNT))->sa_family)) \
159 rta_copy((DEST), (PNT)); \
160 (PNT) += len; \
161 }
162 #define RTA_ATTR_GET(DEST, RTA, RTMADDRS, PNT) \
163 if ((RTMADDRS) & (RTA)) \
164 { \
165 int len = SAROUNDUP ((PNT)); \
166 rta_copy((DEST), (PNT)); \
167 (PNT) += len; \
168 }
169
170 #define RTA_NAME_GET(DEST, RTA, RTMADDRS, PNT, LEN) \
171 if ((RTMADDRS) & (RTA)) \
172 { \
173 u_char *pdest = (u_char *) (DEST); \
174 int len = SAROUNDUP ((PNT)); \
175 struct sockaddr_dl *sdl = (struct sockaddr_dl *)(PNT); \
176 if (IS_ZEBRA_DEBUG_KERNEL) \
177 zlog_debug ("%s: RTA_SDL_GET nlen %d, alen %d", \
178 __func__, sdl->sdl_nlen, sdl->sdl_alen); \
179 if ( ((DEST) != NULL) && (sdl->sdl_family == AF_LINK) \
180 && (sdl->sdl_nlen < IFNAMSIZ) && (sdl->sdl_nlen <= len) ) \
181 { \
182 memcpy (pdest, sdl->sdl_data, sdl->sdl_nlen); \
183 pdest[sdl->sdl_nlen] = '\0'; \
184 (LEN) = sdl->sdl_nlen; \
185 } \
186 (PNT) += len; \
187 } \
188 else \
189 { \
190 (LEN) = 0; \
191 }
192 /* Routing socket message types. */
193 const struct message rtm_type_str[] =
194 {
195 {RTM_ADD, "RTM_ADD"},
196 {RTM_DELETE, "RTM_DELETE"},
197 {RTM_CHANGE, "RTM_CHANGE"},
198 {RTM_GET, "RTM_GET"},
199 {RTM_LOSING, "RTM_LOSING"},
200 {RTM_REDIRECT, "RTM_REDIRECT"},
201 {RTM_MISS, "RTM_MISS"},
202 {RTM_LOCK, "RTM_LOCK"},
203 #ifdef OLDADD
204 {RTM_OLDADD, "RTM_OLDADD"},
205 #endif /* RTM_OLDADD */
206 #ifdef RTM_OLDDEL
207 {RTM_OLDDEL, "RTM_OLDDEL"},
208 #endif /* RTM_OLDDEL */
209 {RTM_RESOLVE, "RTM_RESOLVE"},
210 {RTM_NEWADDR, "RTM_NEWADDR"},
211 {RTM_DELADDR, "RTM_DELADDR"},
212 {RTM_IFINFO, "RTM_IFINFO"},
213 #ifdef RTM_OIFINFO
214 {RTM_OIFINFO, "RTM_OIFINFO"},
215 #endif /* RTM_OIFINFO */
216 #ifdef RTM_NEWMADDR
217 {RTM_NEWMADDR, "RTM_NEWMADDR"},
218 #endif /* RTM_NEWMADDR */
219 #ifdef RTM_DELMADDR
220 {RTM_DELMADDR, "RTM_DELMADDR"},
221 #endif /* RTM_DELMADDR */
222 #ifdef RTM_IFANNOUNCE
223 {RTM_IFANNOUNCE, "RTM_IFANNOUNCE"},
224 #endif /* RTM_IFANNOUNCE */
225 {0, NULL}
226 };
227
228 static const struct message rtm_flag_str[] =
229 {
230 {RTF_UP, "UP"},
231 {RTF_GATEWAY, "GATEWAY"},
232 {RTF_HOST, "HOST"},
233 {RTF_REJECT, "REJECT"},
234 {RTF_DYNAMIC, "DYNAMIC"},
235 {RTF_MODIFIED, "MODIFIED"},
236 {RTF_DONE, "DONE"},
237 #ifdef RTF_MASK
238 {RTF_MASK, "MASK"},
239 #endif /* RTF_MASK */
240 #ifdef RTF_CLONING
241 {RTF_CLONING, "CLONING"},
242 #endif /* RTF_CLONING */
243 {RTF_XRESOLVE, "XRESOLVE"},
244 #ifdef RTF_LLINFO
245 {RTF_LLINFO, "LLINFO"},
246 #endif /* RTF_LLINFO */
247 {RTF_STATIC, "STATIC"},
248 {RTF_BLACKHOLE, "BLACKHOLE"},
249 #ifdef RTF_PRIVATE
250 {RTF_PRIVATE, "PRIVATE"},
251 #endif /* RTF_PRIVATE */
252 {RTF_PROTO1, "PROTO1"},
253 {RTF_PROTO2, "PROTO2"},
254 #ifdef RTF_PRCLONING
255 {RTF_PRCLONING, "PRCLONING"},
256 #endif /* RTF_PRCLONING */
257 #ifdef RTF_WASCLONED
258 {RTF_WASCLONED, "WASCLONED"},
259 #endif /* RTF_WASCLONED */
260 #ifdef RTF_PROTO3
261 {RTF_PROTO3, "PROTO3"},
262 #endif /* RTF_PROTO3 */
263 #ifdef RTF_PINNED
264 {RTF_PINNED, "PINNED"},
265 #endif /* RTF_PINNED */
266 #ifdef RTF_LOCAL
267 {RTF_LOCAL, "LOCAL"},
268 #endif /* RTF_LOCAL */
269 #ifdef RTF_BROADCAST
270 {RTF_BROADCAST, "BROADCAST"},
271 #endif /* RTF_BROADCAST */
272 #ifdef RTF_MULTICAST
273 {RTF_MULTICAST, "MULTICAST"},
274 #endif /* RTF_MULTICAST */
275 #ifdef RTF_MULTIRT
276 {RTF_MULTIRT, "MULTIRT"},
277 #endif /* RTF_MULTIRT */
278 #ifdef RTF_SETSRC
279 {RTF_SETSRC, "SETSRC"},
280 #endif /* RTF_SETSRC */
281 {0, NULL}
282 };
283
284 /* Kernel routing update socket. */
285 int routing_sock = -1;
286
287 /* Yes I'm checking ugly routing socket behavior. */
288 /* #define DEBUG */
289
290 /* Supported address family check. */
291 static inline int
292 af_check (int family)
293 {
294 if (family == AF_INET)
295 return 1;
296 #ifdef HAVE_IPV6
297 if (family == AF_INET6)
298 return 1;
299 #endif /* HAVE_IPV6 */
300 return 0;
301 }
302
303 /* Dump routing table flag for debug purpose. */
304 static void
305 rtm_flag_dump (int flag)
306 {
307 const struct message *mes;
308 static char buf[BUFSIZ];
309
310 buf[0] = '\0';
311 for (mes = rtm_flag_str; mes->key != 0; mes++)
312 {
313 if (mes->key & flag)
314 {
315 strlcat (buf, mes->str, BUFSIZ);
316 strlcat (buf, " ", BUFSIZ);
317 }
318 }
319 zlog_debug ("Kernel: %s", buf);
320 }
321
322 #ifdef RTM_IFANNOUNCE
323 /* Interface adding function */
324 static int
325 ifan_read (struct if_announcemsghdr *ifan)
326 {
327 struct interface *ifp;
328
329 ifp = if_lookup_by_index (ifan->ifan_index);
330
331 if (ifp)
332 assert ( (ifp->ifindex == ifan->ifan_index)
333 || (ifp->ifindex == IFINDEX_INTERNAL) );
334
335 if ( (ifp == NULL)
336 || ((ifp->ifindex == IFINDEX_INTERNAL)
337 && (ifan->ifan_what == IFAN_ARRIVAL)) )
338 {
339 if (IS_ZEBRA_DEBUG_KERNEL)
340 zlog_debug ("%s: creating interface for ifindex %d, name %s",
341 __func__, ifan->ifan_index, ifan->ifan_name);
342
343 /* Create Interface */
344 ifp = if_get_by_name_len(ifan->ifan_name,
345 strnlen(ifan->ifan_name,
346 sizeof(ifan->ifan_name)));
347 ifp->ifindex = ifan->ifan_index;
348
349 if_get_metric (ifp);
350 if_add_update (ifp);
351 }
352 else if (ifp != NULL && ifan->ifan_what == IFAN_DEPARTURE)
353 if_delete_update (ifp);
354
355 if_get_flags (ifp);
356 if_get_mtu (ifp);
357 if_get_metric (ifp);
358
359 if (IS_ZEBRA_DEBUG_KERNEL)
360 zlog_debug ("%s: interface %s index %d",
361 __func__, ifan->ifan_name, ifan->ifan_index);
362
363 return 0;
364 }
365 #endif /* RTM_IFANNOUNCE */
366
367 #ifdef HAVE_BSD_IFI_LINK_STATE
368 /* BSD link detect translation */
369 static void
370 bsd_linkdetect_translate (struct if_msghdr *ifm)
371 {
372 if ((ifm->ifm_data.ifi_link_state >= LINK_STATE_UP) ||
373 (ifm->ifm_data.ifi_link_state == LINK_STATE_UNKNOWN))
374 SET_FLAG(ifm->ifm_flags, IFF_RUNNING);
375 else
376 UNSET_FLAG(ifm->ifm_flags, IFF_RUNNING);
377 }
378 #endif /* HAVE_BSD_IFI_LINK_STATE */
379
380 /*
381 * Handle struct if_msghdr obtained from reading routing socket or
382 * sysctl (from interface_list). There may or may not be sockaddrs
383 * present after the header.
384 */
385 int
386 ifm_read (struct if_msghdr *ifm)
387 {
388 struct interface *ifp = NULL;
389 struct sockaddr_dl *sdl;
390 char ifname[IFNAMSIZ];
391 short ifnlen = 0;
392 caddr_t cp;
393
394 /* terminate ifname at head (for strnlen) and tail (for safety) */
395 ifname[IFNAMSIZ - 1] = '\0';
396
397 /* paranoia: sanity check structure */
398 if (ifm->ifm_msglen < sizeof(struct if_msghdr))
399 {
400 zlog_err ("ifm_read: ifm->ifm_msglen %d too short\n",
401 ifm->ifm_msglen);
402 return -1;
403 }
404
405 /*
406 * Check for a sockaddr_dl following the message. First, point to
407 * where a socakddr might be if one follows the message.
408 */
409 cp = (void *)(ifm + 1);
410
411 #ifdef SUNOS_5
412 /*
413 * XXX This behavior should be narrowed to only the kernel versions
414 * for which the structures returned do not match the headers.
415 *
416 * if_msghdr_t on 64 bit kernels in Solaris 9 and earlier versions
417 * is 12 bytes larger than the 32 bit version.
418 */
419 if (((struct sockaddr *) cp)->sa_family == AF_UNSPEC)
420 cp = cp + 12;
421 #endif
422
423 RTA_ADDR_GET (NULL, RTA_DST, ifm->ifm_addrs, cp);
424 RTA_ADDR_GET (NULL, RTA_GATEWAY, ifm->ifm_addrs, cp);
425 RTA_ATTR_GET (NULL, RTA_NETMASK, ifm->ifm_addrs, cp);
426 RTA_ADDR_GET (NULL, RTA_GENMASK, ifm->ifm_addrs, cp);
427 sdl = (struct sockaddr_dl *)cp;
428 RTA_NAME_GET (ifname, RTA_IFP, ifm->ifm_addrs, cp, ifnlen);
429 RTA_ADDR_GET (NULL, RTA_IFA, ifm->ifm_addrs, cp);
430 RTA_ADDR_GET (NULL, RTA_AUTHOR, ifm->ifm_addrs, cp);
431 RTA_ADDR_GET (NULL, RTA_BRD, ifm->ifm_addrs, cp);
432
433 if (IS_ZEBRA_DEBUG_KERNEL)
434 zlog_debug ("%s: sdl ifname %s", __func__, (ifnlen ? ifname : "(nil)"));
435
436 /*
437 * Look up on ifindex first, because ifindices are the primary handle for
438 * interfaces across the user/kernel boundary, for most systems. (Some
439 * messages, such as up/down status changes on NetBSD, do not include a
440 * sockaddr_dl).
441 */
442 if ( (ifp = if_lookup_by_index (ifm->ifm_index)) != NULL )
443 {
444 /* we have an ifp, verify that the name matches as some systems,
445 * eg Solaris, have a 1:many association of ifindex:ifname
446 * if they dont match, we dont have the correct ifp and should
447 * set it back to NULL to let next check do lookup by name
448 */
449 if (ifnlen && (strncmp (ifp->name, ifname, IFNAMSIZ) != 0) )
450 {
451 if (IS_ZEBRA_DEBUG_KERNEL)
452 zlog_debug ("%s: ifp name %s doesnt match sdl name %s",
453 __func__, ifp->name, ifname);
454 ifp = NULL;
455 }
456 }
457
458 /*
459 * If we dont have an ifp, try looking up by name. Particularly as some
460 * systems (Solaris) have a 1:many mapping of ifindex:ifname - the ifname
461 * is therefore our unique handle to that interface.
462 *
463 * Interfaces specified in the configuration file for which the ifindex
464 * has not been determined will have ifindex == IFINDEX_INTERNAL, and such
465 * interfaces are found by this search, and then their ifindex values can
466 * be filled in.
467 */
468 if ( (ifp == NULL) && ifnlen)
469 ifp = if_lookup_by_name (ifname);
470
471 /*
472 * If ifp still does not exist or has an invalid index (IFINDEX_INTERNAL),
473 * create or fill in an interface.
474 */
475 if ((ifp == NULL) || (ifp->ifindex == IFINDEX_INTERNAL))
476 {
477 /*
478 * To create or fill in an interface, a sockaddr_dl (via
479 * RTA_IFP) is required.
480 */
481 if (!ifnlen)
482 {
483 zlog_warn ("Interface index %d (new) missing ifname\n",
484 ifm->ifm_index);
485 return -1;
486 }
487
488 #ifndef RTM_IFANNOUNCE
489 /* Down->Down interface should be ignored here.
490 * See further comment below.
491 */
492 if (!CHECK_FLAG (ifm->ifm_flags, IFF_UP))
493 return 0;
494 #endif /* !RTM_IFANNOUNCE */
495
496 if (ifp == NULL)
497 {
498 /* Interface that zebra was not previously aware of, so create. */
499 ifp = if_create (ifname, ifnlen);
500 if (IS_ZEBRA_DEBUG_KERNEL)
501 zlog_debug ("%s: creating ifp for ifindex %d",
502 __func__, ifm->ifm_index);
503 }
504
505 if (IS_ZEBRA_DEBUG_KERNEL)
506 zlog_debug ("%s: updated/created ifp, ifname %s, ifindex %d",
507 __func__, ifp->name, ifp->ifindex);
508 /*
509 * Fill in newly created interface structure, or larval
510 * structure with ifindex IFINDEX_INTERNAL.
511 */
512 ifp->ifindex = ifm->ifm_index;
513
514 #ifdef HAVE_BSD_IFI_LINK_STATE /* translate BSD kernel msg for link-state */
515 bsd_linkdetect_translate(ifm);
516 #endif /* HAVE_BSD_IFI_LINK_STATE */
517
518 if_flags_update (ifp, ifm->ifm_flags);
519 #if defined(__bsdi__)
520 if_kvm_get_mtu (ifp);
521 #else
522 if_get_mtu (ifp);
523 #endif /* __bsdi__ */
524 if_get_metric (ifp);
525
526 /*
527 * XXX sockaddr_dl contents can be larger than the structure
528 * definition. There are 2 big families here:
529 * - BSD has sdl_len + sdl_data[16] + overruns sdl_data
530 * we MUST use sdl_len here or we'll truncate data.
531 * - Solaris has no sdl_len, but sdl_data[244]
532 * presumably, it's not going to run past that, so sizeof()
533 * is fine here.
534 * a nonzero ifnlen from RTA_NAME_GET() means sdl is valid
535 */
536 if (ifnlen)
537 {
538 #ifdef HAVE_STRUCT_SOCKADDR_DL_SDL_LEN
539 memcpy (&ifp->sdl, sdl, sdl->sdl_len);
540 #else
541 memcpy (&ifp->sdl, sdl, sizeof (struct sockaddr_dl));
542 #endif /* HAVE_STRUCT_SOCKADDR_DL_SDL_LEN */
543 }
544
545 if_add_update (ifp);
546 }
547 else
548 /*
549 * Interface structure exists. Adjust stored flags from
550 * notification. If interface has up->down or down->up
551 * transition, call state change routines (to adjust routes,
552 * notify routing daemons, etc.). (Other flag changes are stored
553 * but apparently do not trigger action.)
554 */
555 {
556 if (ifp->ifindex != ifm->ifm_index)
557 {
558 zlog_warn ("%s: index mismatch, ifname %s, ifp index %d, "
559 "ifm index %d",
560 __func__, ifp->name, ifp->ifindex, ifm->ifm_index);
561 return -1;
562 }
563
564 #ifdef HAVE_BSD_IFI_LINK_STATE /* translate BSD kernel msg for link-state */
565 bsd_linkdetect_translate(ifm);
566 #endif /* HAVE_BSD_IFI_LINK_STATE */
567
568 /* update flags and handle operative->inoperative transition, if any */
569 if_flags_update (ifp, ifm->ifm_flags);
570
571 #ifndef RTM_IFANNOUNCE
572 if (!if_is_up (ifp))
573 {
574 /* No RTM_IFANNOUNCE on this platform, so we can never
575 * distinguish between ~IFF_UP and delete. We must presume
576 * it has been deleted.
577 * Eg, Solaris will not notify us of unplumb.
578 *
579 * XXX: Fixme - this should be runtime detected
580 * So that a binary compiled on a system with IFANNOUNCE
581 * will still behave correctly if run on a platform without
582 */
583 if_delete_update (ifp);
584 }
585 #endif /* RTM_IFANNOUNCE */
586 if (if_is_up (ifp))
587 {
588 #if defined(__bsdi__)
589 if_kvm_get_mtu (ifp);
590 #else
591 if_get_mtu (ifp);
592 #endif /* __bsdi__ */
593 if_get_metric (ifp);
594 }
595 }
596
597 #ifdef HAVE_NET_RT_IFLIST
598 ifp->stats = ifm->ifm_data;
599 #endif /* HAVE_NET_RT_IFLIST */
600
601 if (IS_ZEBRA_DEBUG_KERNEL)
602 zlog_debug ("%s: interface %s index %d",
603 __func__, ifp->name, ifp->ifindex);
604
605 return 0;
606 }
607
608 /* Address read from struct ifa_msghdr. */
609 static void
610 ifam_read_mesg (struct ifa_msghdr *ifm,
611 union sockunion *addr,
612 union sockunion *mask,
613 union sockunion *brd,
614 char *ifname,
615 short *ifnlen)
616 {
617 caddr_t pnt, end;
618 union sockunion dst;
619 union sockunion gateway;
620
621 pnt = (caddr_t)(ifm + 1);
622 end = ((caddr_t)ifm) + ifm->ifam_msglen;
623
624 /* Be sure structure is cleared */
625 memset (mask, 0, sizeof (union sockunion));
626 memset (addr, 0, sizeof (union sockunion));
627 memset (brd, 0, sizeof (union sockunion));
628 memset (&dst, 0, sizeof (union sockunion));
629 memset (&gateway, 0, sizeof (union sockunion));
630
631 /* We fetch each socket variable into sockunion. */
632 RTA_ADDR_GET (&dst, RTA_DST, ifm->ifam_addrs, pnt);
633 RTA_ADDR_GET (&gateway, RTA_GATEWAY, ifm->ifam_addrs, pnt);
634 RTA_ATTR_GET (mask, RTA_NETMASK, ifm->ifam_addrs, pnt);
635 RTA_ADDR_GET (NULL, RTA_GENMASK, ifm->ifam_addrs, pnt);
636 RTA_NAME_GET (ifname, RTA_IFP, ifm->ifam_addrs, pnt, *ifnlen);
637 RTA_ADDR_GET (addr, RTA_IFA, ifm->ifam_addrs, pnt);
638 RTA_ADDR_GET (NULL, RTA_AUTHOR, ifm->ifam_addrs, pnt);
639 RTA_ADDR_GET (brd, RTA_BRD, ifm->ifam_addrs, pnt);
640
641 if (IS_ZEBRA_DEBUG_KERNEL)
642 {
643 switch (sockunion_family(addr))
644 {
645 case AF_INET:
646 {
647 char buf[4][INET_ADDRSTRLEN];
648 zlog_debug ("%s: ifindex %d, ifname %s, ifam_addrs 0x%x, "
649 "ifam_flags 0x%x, addr %s/%d broad %s dst %s "
650 "gateway %s",
651 __func__, ifm->ifam_index,
652 (ifnlen ? ifname : "(nil)"), ifm->ifam_addrs,
653 ifm->ifam_flags,
654 inet_ntop(AF_INET,&addr->sin.sin_addr,
655 buf[0],sizeof(buf[0])),
656 ip_masklen(mask->sin.sin_addr),
657 inet_ntop(AF_INET,&brd->sin.sin_addr,
658 buf[1],sizeof(buf[1])),
659 inet_ntop(AF_INET,&dst.sin.sin_addr,
660 buf[2],sizeof(buf[2])),
661 inet_ntop(AF_INET,&gateway.sin.sin_addr,
662 buf[3],sizeof(buf[3])));
663 }
664 break;
665 #ifdef HAVE_IPV6
666 case AF_INET6:
667 {
668 char buf[4][INET6_ADDRSTRLEN];
669 zlog_debug ("%s: ifindex %d, ifname %s, ifam_addrs 0x%x, "
670 "ifam_flags 0x%x, addr %s/%d broad %s dst %s "
671 "gateway %s",
672 __func__, ifm->ifam_index,
673 (ifnlen ? ifname : "(nil)"), ifm->ifam_addrs,
674 ifm->ifam_flags,
675 inet_ntop(AF_INET6,&addr->sin6.sin6_addr,
676 buf[0],sizeof(buf[0])),
677 ip6_masklen(mask->sin6.sin6_addr),
678 inet_ntop(AF_INET6,&brd->sin6.sin6_addr,
679 buf[1],sizeof(buf[1])),
680 inet_ntop(AF_INET6,&dst.sin6.sin6_addr,
681 buf[2],sizeof(buf[2])),
682 inet_ntop(AF_INET6,&gateway.sin6.sin6_addr,
683 buf[3],sizeof(buf[3])));
684 }
685 break;
686 #endif /* HAVE_IPV6 */
687 default:
688 zlog_debug ("%s: ifindex %d, ifname %s, ifam_addrs 0x%x",
689 __func__, ifm->ifam_index,
690 (ifnlen ? ifname : "(nil)"), ifm->ifam_addrs);
691 break;
692 }
693 }
694
695 /* Assert read up end point matches to end point */
696 if (pnt != end)
697 zlog_warn ("ifam_read() doesn't read all socket data");
698 }
699
700 /* Interface's address information get. */
701 int
702 ifam_read (struct ifa_msghdr *ifam)
703 {
704 struct interface *ifp = NULL;
705 union sockunion addr, mask, brd;
706 char ifname[INTERFACE_NAMSIZ];
707 short ifnlen = 0;
708 char isalias = 0;
709 int flags = 0;
710
711 ifname[0] = ifname[INTERFACE_NAMSIZ - 1] = '\0';
712
713 /* Allocate and read address information. */
714 ifam_read_mesg (ifam, &addr, &mask, &brd, ifname, &ifnlen);
715
716 if ((ifp = if_lookup_by_index(ifam->ifam_index)) == NULL)
717 {
718 zlog_warn ("%s: no interface for ifname %s, index %d",
719 __func__, ifname, ifam->ifam_index);
720 return -1;
721 }
722
723 if (ifnlen && strncmp (ifp->name, ifname, INTERFACE_NAMSIZ))
724 isalias = 1;
725
726 /* N.B. The info in ifa_msghdr does not tell us whether the RTA_BRD
727 field contains a broadcast address or a peer address, so we are forced to
728 rely upon the interface type. */
729 if (if_is_pointopoint(ifp))
730 SET_FLAG(flags, ZEBRA_IFA_PEER);
731
732 #if 0
733 /* it might seem cute to grab the interface metric here, however
734 * we're processing an address update message, and so some systems
735 * (e.g. FBSD) dont bother to fill in ifam_metric. Disabled, but left
736 * in deliberately, as comment.
737 */
738 ifp->metric = ifam->ifam_metric;
739 #endif
740
741 /* Add connected address. */
742 switch (sockunion_family (&addr))
743 {
744 case AF_INET:
745 if (ifam->ifam_type == RTM_NEWADDR)
746 connected_add_ipv4 (ifp, flags, &addr.sin.sin_addr,
747 ip_masklen (mask.sin.sin_addr),
748 &brd.sin.sin_addr,
749 (isalias ? ifname : NULL));
750 else
751 connected_delete_ipv4 (ifp, flags, &addr.sin.sin_addr,
752 ip_masklen (mask.sin.sin_addr),
753 &brd.sin.sin_addr);
754 break;
755 #ifdef HAVE_IPV6
756 case AF_INET6:
757 /* Unset interface index from link-local address when IPv6 stack
758 is KAME. */
759 if (IN6_IS_ADDR_LINKLOCAL (&addr.sin6.sin6_addr))
760 {
761 SET_IN6_LINKLOCAL_IFINDEX (addr.sin6.sin6_addr, 0);
762 }
763
764 if (ifam->ifam_type == RTM_NEWADDR)
765 connected_add_ipv6 (ifp, flags, &addr.sin6.sin6_addr,
766 ip6_masklen (mask.sin6.sin6_addr),
767 &brd.sin6.sin6_addr,
768 (isalias ? ifname : NULL));
769 else
770 connected_delete_ipv6 (ifp,
771 &addr.sin6.sin6_addr,
772 ip6_masklen (mask.sin6.sin6_addr),
773 &brd.sin6.sin6_addr);
774 break;
775 #endif /* HAVE_IPV6 */
776 default:
777 /* Unsupported family silently ignore... */
778 break;
779 }
780
781 /* Check interface flag for implicit up of the interface. */
782 if_refresh (ifp);
783
784 #ifdef SUNOS_5
785 /* In addition to lacking IFANNOUNCE, on SUNOS IFF_UP is strange.
786 * See comments for SUNOS_5 in interface.c::if_flags_mangle.
787 *
788 * Here we take care of case where the real IFF_UP was previously
789 * unset (as kept in struct zebra_if.primary_state) and the mangled
790 * IFF_UP (ie IFF_UP set || listcount(connected) has now transitioned
791 * to unset due to the lost non-primary address having DELADDR'd.
792 *
793 * we must delete the interface, because in between here and next
794 * event for this interface-name the administrator could unplumb
795 * and replumb the interface.
796 */
797 if (!if_is_up (ifp))
798 if_delete_update (ifp);
799 #endif /* SUNOS_5 */
800
801 return 0;
802 }
803
804 /* Interface function for reading kernel routing table information. */
805 static int
806 rtm_read_mesg (struct rt_msghdr *rtm,
807 union sockunion *dest,
808 union sockunion *mask,
809 union sockunion *gate,
810 char *ifname,
811 short *ifnlen)
812 {
813 caddr_t pnt, end;
814
815 /* Pnt points out socket data start point. */
816 pnt = (caddr_t)(rtm + 1);
817 end = ((caddr_t)rtm) + rtm->rtm_msglen;
818
819 /* rt_msghdr version check. */
820 if (rtm->rtm_version != RTM_VERSION)
821 zlog (NULL, LOG_WARNING,
822 "Routing message version different %d should be %d."
823 "This may cause problem\n", rtm->rtm_version, RTM_VERSION);
824
825 /* Be sure structure is cleared */
826 memset (dest, 0, sizeof (union sockunion));
827 memset (gate, 0, sizeof (union sockunion));
828 memset (mask, 0, sizeof (union sockunion));
829
830 /* We fetch each socket variable into sockunion. */
831 RTA_ADDR_GET (dest, RTA_DST, rtm->rtm_addrs, pnt);
832 RTA_ADDR_GET (gate, RTA_GATEWAY, rtm->rtm_addrs, pnt);
833 RTA_ATTR_GET (mask, RTA_NETMASK, rtm->rtm_addrs, pnt);
834 RTA_ADDR_GET (NULL, RTA_GENMASK, rtm->rtm_addrs, pnt);
835 RTA_NAME_GET (ifname, RTA_IFP, rtm->rtm_addrs, pnt, *ifnlen);
836 RTA_ADDR_GET (NULL, RTA_IFA, rtm->rtm_addrs, pnt);
837 RTA_ADDR_GET (NULL, RTA_AUTHOR, rtm->rtm_addrs, pnt);
838 RTA_ADDR_GET (NULL, RTA_BRD, rtm->rtm_addrs, pnt);
839
840 /* If there is netmask information set it's family same as
841 destination family*/
842 if (rtm->rtm_addrs & RTA_NETMASK)
843 mask->sa.sa_family = dest->sa.sa_family;
844
845 /* Assert read up to the end of pointer. */
846 if (pnt != end)
847 zlog (NULL, LOG_WARNING, "rtm_read() doesn't read all socket data.");
848
849 return rtm->rtm_flags;
850 }
851
852 void
853 rtm_read (struct rt_msghdr *rtm)
854 {
855 int flags;
856 u_char zebra_flags;
857 union sockunion dest, mask, gate;
858 char ifname[INTERFACE_NAMSIZ + 1];
859 short ifnlen = 0;
860
861 zebra_flags = 0;
862
863 /* Read destination and netmask and gateway from rtm message
864 structure. */
865 flags = rtm_read_mesg (rtm, &dest, &mask, &gate, ifname, &ifnlen);
866 if (!(flags & RTF_DONE))
867 return;
868 if (IS_ZEBRA_DEBUG_KERNEL)
869 zlog_debug ("%s: got rtm of type %d (%s)", __func__, rtm->rtm_type,
870 lookup (rtm_type_str, rtm->rtm_type));
871
872 #ifdef RTF_CLONED /*bsdi, netbsd 1.6*/
873 if (flags & RTF_CLONED)
874 return;
875 #endif
876 #ifdef RTF_WASCLONED /*freebsd*/
877 if (flags & RTF_WASCLONED)
878 return;
879 #endif
880
881 if ((rtm->rtm_type == RTM_ADD) && ! (flags & RTF_UP))
882 return;
883
884 /* This is connected route. */
885 if (! (flags & RTF_GATEWAY))
886 return;
887
888 if (flags & RTF_PROTO1)
889 SET_FLAG (zebra_flags, ZEBRA_FLAG_SELFROUTE);
890
891 /* This is persistent route. */
892 if (flags & RTF_STATIC)
893 SET_FLAG (zebra_flags, ZEBRA_FLAG_STATIC);
894
895 /* This is a reject or blackhole route */
896 if (flags & RTF_REJECT)
897 SET_FLAG (zebra_flags, ZEBRA_FLAG_REJECT);
898 if (flags & RTF_BLACKHOLE)
899 SET_FLAG (zebra_flags, ZEBRA_FLAG_BLACKHOLE);
900
901 if (dest.sa.sa_family == AF_INET)
902 {
903 struct prefix_ipv4 p;
904
905 p.family = AF_INET;
906 p.prefix = dest.sin.sin_addr;
907 if (flags & RTF_HOST)
908 p.prefixlen = IPV4_MAX_PREFIXLEN;
909 else
910 p.prefixlen = ip_masklen (mask.sin.sin_addr);
911
912 /* Catch self originated messages and match them against our current RIB.
913 * At the same time, ignore unconfirmed messages, they should be tracked
914 * by rtm_write() and kernel_rtm_ipv4().
915 */
916 if (rtm->rtm_type != RTM_GET && rtm->rtm_pid == pid)
917 {
918 char buf[PREFIX2STR_BUFFER], gate_buf[INET_ADDRSTRLEN];
919 int ret;
920 if (! IS_ZEBRA_DEBUG_RIB)
921 return;
922 ret = rib_lookup_ipv4_route (&p, &gate, VRF_DEFAULT);
923 prefix2str (&p, buf, sizeof(buf));
924 switch (rtm->rtm_type)
925 {
926 case RTM_ADD:
927 case RTM_GET:
928 case RTM_CHANGE:
929 /* The kernel notifies us about a new route in FIB created by us.
930 Do we have a correspondent entry in our RIB? */
931 switch (ret)
932 {
933 case ZEBRA_RIB_NOTFOUND:
934 zlog_debug ("%s: %s %s/%d: desync: RR isn't yet in RIB, while already in FIB",
935 __func__, lookup (rtm_type_str, rtm->rtm_type), buf, p.prefixlen);
936 break;
937 case ZEBRA_RIB_FOUND_CONNECTED:
938 case ZEBRA_RIB_FOUND_NOGATE:
939 inet_ntop (AF_INET, &gate.sin.sin_addr, gate_buf, INET_ADDRSTRLEN);
940 zlog_debug ("%s: %s %s/%d: desync: RR is in RIB, but gate differs (ours is %s)",
941 __func__, lookup (rtm_type_str, rtm->rtm_type), buf, p.prefixlen, gate_buf);
942 break;
943 case ZEBRA_RIB_FOUND_EXACT: /* RIB RR == FIB RR */
944 zlog_debug ("%s: %s %s/%d: done Ok",
945 __func__, lookup (rtm_type_str, rtm->rtm_type), buf, p.prefixlen);
946 rib_lookup_and_dump (&p);
947 return;
948 break;
949 }
950 break;
951 case RTM_DELETE:
952 /* The kernel notifies us about a route deleted by us. Do we still
953 have it in the RIB? Do we have anything instead? */
954 switch (ret)
955 {
956 case ZEBRA_RIB_FOUND_EXACT:
957 zlog_debug ("%s: %s %s/%d: desync: RR is still in RIB, while already not in FIB",
958 __func__, lookup (rtm_type_str, rtm->rtm_type), buf, p.prefixlen);
959 rib_lookup_and_dump (&p);
960 break;
961 case ZEBRA_RIB_FOUND_CONNECTED:
962 case ZEBRA_RIB_FOUND_NOGATE:
963 zlog_debug ("%s: %s %s/%d: desync: RR is still in RIB, plus gate differs",
964 __func__, lookup (rtm_type_str, rtm->rtm_type), buf, p.prefixlen);
965 rib_lookup_and_dump (&p);
966 break;
967 case ZEBRA_RIB_NOTFOUND: /* RIB RR == FIB RR */
968 zlog_debug ("%s: %s %s/%d: done Ok",
969 __func__, lookup (rtm_type_str, rtm->rtm_type), buf, p.prefixlen);
970 rib_lookup_and_dump (&p);
971 return;
972 break;
973 }
974 break;
975 default:
976 zlog_debug ("%s: %s/%d: warning: loopback RTM of type %s received",
977 __func__, buf, p.prefixlen, lookup (rtm_type_str, rtm->rtm_type));
978 }
979 return;
980 }
981
982 /* Change, delete the old prefix, we have no further information
983 * to specify the route really
984 */
985 if (rtm->rtm_type == RTM_CHANGE)
986 rib_delete_ipv4 (ZEBRA_ROUTE_KERNEL, 0, zebra_flags, &p,
987 NULL, 0, VRF_DEFAULT, SAFI_UNICAST);
988
989 if (rtm->rtm_type == RTM_GET
990 || rtm->rtm_type == RTM_ADD
991 || rtm->rtm_type == RTM_CHANGE)
992 rib_add_ipv4 (ZEBRA_ROUTE_KERNEL, 0, zebra_flags,
993 &p, &gate.sin.sin_addr, NULL, 0, VRF_DEFAULT, 0, 0, SAFI_UNICAST);
994 else
995 rib_delete_ipv4 (ZEBRA_ROUTE_KERNEL, 0 zebra_flags,
996 &p, &gate.sin.sin_addr, 0, VRF_DEFAULT, SAFI_UNICAST);
997 }
998 #ifdef HAVE_IPV6
999 if (dest.sa.sa_family == AF_INET6)
1000 {
1001 /* One day we might have a debug section here like one in the
1002 * IPv4 case above. Just ignore own messages at the moment.
1003 */
1004 if (rtm->rtm_type != RTM_GET && rtm->rtm_pid == pid)
1005 return;
1006 struct prefix_ipv6 p;
1007 unsigned int ifindex = 0;
1008
1009 p.family = AF_INET6;
1010 p.prefix = dest.sin6.sin6_addr;
1011 if (flags & RTF_HOST)
1012 p.prefixlen = IPV6_MAX_PREFIXLEN;
1013 else
1014 p.prefixlen = ip6_masklen (mask.sin6.sin6_addr);
1015
1016 #ifdef KAME
1017 if (IN6_IS_ADDR_LINKLOCAL (&gate.sin6.sin6_addr))
1018 {
1019 ifindex = IN6_LINKLOCAL_IFINDEX (gate.sin6.sin6_addr);
1020 SET_IN6_LINKLOCAL_IFINDEX (gate.sin6.sin6_addr, 0);
1021 }
1022 #endif /* KAME */
1023
1024 /* CHANGE: delete the old prefix, we have no further information
1025 * to specify the route really
1026 */
1027 if (rtm->rtm_type == RTM_CHANGE)
1028 rib_delete_ipv6 (ZEBRA_ROUTE_KERNEL, 0, zebra_flags, &p,
1029 NULL, 0, VRF_DEFAULT, SAFI_UNICAST);
1030
1031 if (rtm->rtm_type == RTM_GET
1032 || rtm->rtm_type == RTM_ADD
1033 || rtm->rtm_type == RTM_CHANGE)
1034 rib_add_ipv6 (ZEBRA_ROUTE_KERNEL, 0, zebra_flags,
1035 &p, &gate.sin6.sin6_addr, ifindex, VRF_DEFAULT, 0, 0, SAFI_UNICAST);
1036 else
1037 rib_delete_ipv6 (ZEBRA_ROUTE_KERNEL, 0, zebra_flags,
1038 &p, &gate.sin6.sin6_addr, ifindex, VRF_DEFAULT, SAFI_UNICAST);
1039 }
1040 #endif /* HAVE_IPV6 */
1041 }
1042
1043 /* Interface function for the kernel routing table updates. Support
1044 * for RTM_CHANGE will be needed.
1045 * Exported only for rt_socket.c
1046 */
1047 int
1048 rtm_write (int message,
1049 union sockunion *dest,
1050 union sockunion *mask,
1051 union sockunion *gate,
1052 unsigned int index,
1053 int zebra_flags,
1054 int metric)
1055 {
1056 int ret;
1057 caddr_t pnt;
1058 struct interface *ifp;
1059
1060 /* Sequencial number of routing message. */
1061 static int msg_seq = 0;
1062
1063 /* Struct of rt_msghdr and buffer for storing socket's data. */
1064 struct
1065 {
1066 struct rt_msghdr rtm;
1067 char buf[512];
1068 } msg;
1069
1070 if (routing_sock < 0)
1071 return ZEBRA_ERR_EPERM;
1072
1073 /* Clear and set rt_msghdr values */
1074 memset (&msg, 0, sizeof (struct rt_msghdr));
1075 msg.rtm.rtm_version = RTM_VERSION;
1076 msg.rtm.rtm_type = message;
1077 msg.rtm.rtm_seq = msg_seq++;
1078 msg.rtm.rtm_addrs = RTA_DST;
1079 msg.rtm.rtm_addrs |= RTA_GATEWAY;
1080 msg.rtm.rtm_flags = RTF_UP;
1081 msg.rtm.rtm_index = index;
1082
1083 if (metric != 0)
1084 {
1085 msg.rtm.rtm_rmx.rmx_hopcount = metric;
1086 msg.rtm.rtm_inits |= RTV_HOPCOUNT;
1087 }
1088
1089 ifp = if_lookup_by_index (index);
1090
1091 if (gate && message == RTM_ADD)
1092 msg.rtm.rtm_flags |= RTF_GATEWAY;
1093
1094 /* When RTF_CLONING is unavailable on BSD, should we set some
1095 * other flag instead?
1096 */
1097 #ifdef RTF_CLONING
1098 if (! gate && message == RTM_ADD && ifp &&
1099 (ifp->flags & IFF_POINTOPOINT) == 0)
1100 msg.rtm.rtm_flags |= RTF_CLONING;
1101 #endif /* RTF_CLONING */
1102
1103 /* If no protocol specific gateway is specified, use link
1104 address for gateway. */
1105 if (! gate)
1106 {
1107 if (!ifp)
1108 {
1109 char dest_buf[INET_ADDRSTRLEN] = "NULL", mask_buf[INET_ADDRSTRLEN] = "255.255.255.255";
1110 if (dest)
1111 inet_ntop (AF_INET, &dest->sin.sin_addr, dest_buf, INET_ADDRSTRLEN);
1112 if (mask)
1113 inet_ntop (AF_INET, &mask->sin.sin_addr, mask_buf, INET_ADDRSTRLEN);
1114 zlog_warn ("%s: %s/%s: gate == NULL and no gateway found for ifindex %d",
1115 __func__, dest_buf, mask_buf, index);
1116 return -1;
1117 }
1118 gate = (union sockunion *) & ifp->sdl;
1119 }
1120
1121 if (mask)
1122 msg.rtm.rtm_addrs |= RTA_NETMASK;
1123 else if (message == RTM_ADD)
1124 msg.rtm.rtm_flags |= RTF_HOST;
1125
1126 /* Tagging route with flags */
1127 msg.rtm.rtm_flags |= (RTF_PROTO1);
1128
1129 /* Additional flags. */
1130 if (zebra_flags & ZEBRA_FLAG_BLACKHOLE)
1131 msg.rtm.rtm_flags |= RTF_BLACKHOLE;
1132 if (zebra_flags & ZEBRA_FLAG_REJECT)
1133 msg.rtm.rtm_flags |= RTF_REJECT;
1134
1135
1136 #define SOCKADDRSET(X,R) \
1137 if (msg.rtm.rtm_addrs & (R)) \
1138 { \
1139 int len = SAROUNDUP (X); \
1140 memcpy (pnt, (caddr_t)(X), len); \
1141 pnt += len; \
1142 }
1143
1144 pnt = (caddr_t) msg.buf;
1145
1146 /* Write each socket data into rtm message buffer */
1147 SOCKADDRSET (dest, RTA_DST);
1148 SOCKADDRSET (gate, RTA_GATEWAY);
1149 SOCKADDRSET (mask, RTA_NETMASK);
1150
1151 msg.rtm.rtm_msglen = pnt - (caddr_t) &msg;
1152
1153 ret = write (routing_sock, &msg, msg.rtm.rtm_msglen);
1154
1155 if (ret != msg.rtm.rtm_msglen)
1156 {
1157 if (errno == EEXIST)
1158 return ZEBRA_ERR_RTEXIST;
1159 if (errno == ENETUNREACH)
1160 return ZEBRA_ERR_RTUNREACH;
1161 if (errno == ESRCH)
1162 return ZEBRA_ERR_RTNOEXIST;
1163
1164 zlog_warn ("%s: write : %s (%d)", __func__, safe_strerror (errno), errno);
1165 return ZEBRA_ERR_KERNEL;
1166 }
1167 return ZEBRA_ERR_NOERROR;
1168 }
1169
1170
1171 #include "thread.h"
1172 #include "zebra/zserv.h"
1173
1174 /* For debug purpose. */
1175 static void
1176 rtmsg_debug (struct rt_msghdr *rtm)
1177 {
1178 zlog_debug ("Kernel: Len: %d Type: %s", rtm->rtm_msglen, lookup (rtm_type_str, rtm->rtm_type));
1179 rtm_flag_dump (rtm->rtm_flags);
1180 zlog_debug ("Kernel: message seq %d", rtm->rtm_seq);
1181 zlog_debug ("Kernel: pid %lld, rtm_addrs 0x%x",
1182 (long long)rtm->rtm_pid, rtm->rtm_addrs);
1183 }
1184
1185 /* This is pretty gross, better suggestions welcome -- mhandler */
1186 #ifndef RTAX_MAX
1187 #ifdef RTA_NUMBITS
1188 #define RTAX_MAX RTA_NUMBITS
1189 #else
1190 #define RTAX_MAX 8
1191 #endif /* RTA_NUMBITS */
1192 #endif /* RTAX_MAX */
1193
1194 /* Kernel routing table and interface updates via routing socket. */
1195 static int
1196 kernel_read (struct thread *thread)
1197 {
1198 int sock;
1199 int nbytes;
1200 struct rt_msghdr *rtm;
1201
1202 /*
1203 * This must be big enough for any message the kernel might send.
1204 * Rather than determining how many sockaddrs of what size might be
1205 * in each particular message, just use RTAX_MAX of sockaddr_storage
1206 * for each. Note that the sockaddrs must be after each message
1207 * definition, or rather after whichever happens to be the largest,
1208 * since the buffer needs to be big enough for a message and the
1209 * sockaddrs together.
1210 */
1211 union
1212 {
1213 /* Routing information. */
1214 struct
1215 {
1216 struct rt_msghdr rtm;
1217 struct sockaddr_storage addr[RTAX_MAX];
1218 } r;
1219
1220 /* Interface information. */
1221 struct
1222 {
1223 struct if_msghdr ifm;
1224 struct sockaddr_storage addr[RTAX_MAX];
1225 } im;
1226
1227 /* Interface address information. */
1228 struct
1229 {
1230 struct ifa_msghdr ifa;
1231 struct sockaddr_storage addr[RTAX_MAX];
1232 } ia;
1233
1234 #ifdef RTM_IFANNOUNCE
1235 /* Interface arrival/departure */
1236 struct
1237 {
1238 struct if_announcemsghdr ifan;
1239 struct sockaddr_storage addr[RTAX_MAX];
1240 } ian;
1241 #endif /* RTM_IFANNOUNCE */
1242
1243 } buf;
1244
1245 /* Fetch routing socket. */
1246 sock = THREAD_FD (thread);
1247
1248 nbytes= read (sock, &buf, sizeof buf);
1249
1250 if (nbytes <= 0)
1251 {
1252 if (nbytes < 0 && errno != EWOULDBLOCK && errno != EAGAIN)
1253 zlog_warn ("routing socket error: %s", safe_strerror (errno));
1254 return 0;
1255 }
1256
1257 thread_add_read (zebrad.master, kernel_read, NULL, sock);
1258
1259 if (IS_ZEBRA_DEBUG_KERNEL)
1260 rtmsg_debug (&buf.r.rtm);
1261
1262 rtm = &buf.r.rtm;
1263
1264 /*
1265 * Ensure that we didn't drop any data, so that processing routines
1266 * can assume they have the whole message.
1267 */
1268 if (rtm->rtm_msglen != nbytes)
1269 {
1270 zlog_warn ("kernel_read: rtm->rtm_msglen %d, nbytes %d, type %d\n",
1271 rtm->rtm_msglen, nbytes, rtm->rtm_type);
1272 return -1;
1273 }
1274
1275 switch (rtm->rtm_type)
1276 {
1277 case RTM_ADD:
1278 case RTM_DELETE:
1279 case RTM_CHANGE:
1280 rtm_read (rtm);
1281 break;
1282 case RTM_IFINFO:
1283 ifm_read (&buf.im.ifm);
1284 break;
1285 case RTM_NEWADDR:
1286 case RTM_DELADDR:
1287 ifam_read (&buf.ia.ifa);
1288 break;
1289 #ifdef RTM_IFANNOUNCE
1290 case RTM_IFANNOUNCE:
1291 ifan_read (&buf.ian.ifan);
1292 break;
1293 #endif /* RTM_IFANNOUNCE */
1294 default:
1295 if (IS_ZEBRA_DEBUG_KERNEL)
1296 zlog_debug("Unprocessed RTM_type: %d", rtm->rtm_type);
1297 break;
1298 }
1299 return 0;
1300 }
1301
1302 /* Make routing socket. */
1303 static void
1304 routing_socket (struct zebra_ns *zns)
1305 {
1306 if ( zserv_privs.change (ZPRIVS_RAISE) )
1307 zlog_err ("routing_socket: Can't raise privileges");
1308
1309 routing_sock = socket (AF_ROUTE, SOCK_RAW, 0);
1310
1311 if (routing_sock < 0)
1312 {
1313 if ( zserv_privs.change (ZPRIVS_LOWER) )
1314 zlog_err ("routing_socket: Can't lower privileges");
1315 zlog_warn ("Can't init kernel routing socket");
1316 return;
1317 }
1318
1319 /* XXX: Socket should be NONBLOCK, however as we currently
1320 * discard failed writes, this will lead to inconsistencies.
1321 * For now, socket must be blocking.
1322 */
1323 /*if (fcntl (routing_sock, F_SETFL, O_NONBLOCK) < 0)
1324 zlog_warn ("Can't set O_NONBLOCK to routing socket");*/
1325
1326 if ( zserv_privs.change (ZPRIVS_LOWER) )
1327 zlog_err ("routing_socket: Can't lower privileges");
1328
1329 /* kernel_read needs rewrite. */
1330 thread_add_read (zebrad.master, kernel_read, NULL, routing_sock);
1331 }
1332
1333 /* Exported interface function. This function simply calls
1334 routing_socket (). */
1335 void
1336 kernel_init (struct zebra_ns *zns)
1337 {
1338 routing_socket (zns);
1339 }
1340
1341 void
1342 kernel_terminate (struct zebra_ns *zns)
1343 {
1344 return;
1345 }