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