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