]> git.proxmox.com Git - mirror_frr.git/blob - zebra/kernel_socket.c
Merge pull request #12798 from donaldsharp/rib_match_multicast
[mirror_frr.git] / zebra / kernel_socket.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Kernel communication using routing socket.
3 * Copyright (C) 1999 Kunihiro Ishiguro
4 */
5
6 #include <zebra.h>
7
8 #ifndef HAVE_NETLINK
9
10 #include <net/if_types.h>
11 #ifdef __OpenBSD__
12 #include <netmpls/mpls.h>
13 #endif
14
15 #include "if.h"
16 #include "prefix.h"
17 #include "sockunion.h"
18 #include "connected.h"
19 #include "memory.h"
20 #include "ioctl.h"
21 #include "log.h"
22 #include "table.h"
23 #include "rib.h"
24 #include "privs.h"
25 #include "vrf.h"
26 #include "lib_errors.h"
27
28 #include "zebra/rt.h"
29 #include "zebra/interface.h"
30 #include "zebra/zebra_router.h"
31 #include "zebra/debug.h"
32 #include "zebra/kernel_socket.h"
33 #include "zebra/rib.h"
34 #include "zebra/zebra_errors.h"
35 #include "zebra/zebra_ptm.h"
36
37 extern struct zebra_privs_t zserv_privs;
38
39 /*
40 * Historically, the BSD routing socket has aligned data following a
41 * struct sockaddr to sizeof(long), which was 4 bytes on some
42 * platforms, and 8 bytes on others. NetBSD 6 changed the routing
43 * socket to align to sizeof(uint64_t), which is 8 bytes. OS X
44 * appears to align to sizeof(int), which is 4 bytes.
45 *
46 * Alignment of zero-sized sockaddrs is nonsensical, but historically
47 * BSD defines RT_ROUNDUP(0) to be the alignment interval (rather than
48 * 0). We follow this practice without questioning it, but it is a
49 * bug if frr calls ROUNDUP with 0.
50 */
51 #ifdef __APPLE__
52 #define ROUNDUP_TYPE int
53 #else
54 #define ROUNDUP_TYPE long
55 #endif
56
57 /*
58 * Because of these varying conventions, the only sane approach is for
59 * the <net/route.h> header to define some flavor of ROUNDUP macro.
60 */
61
62 /* OS X (Xcode as of 2014-12) is known not to define RT_ROUNDUP */
63 #if defined(RT_ROUNDUP)
64 #define ROUNDUP(a) RT_ROUNDUP(a)
65 #endif /* defined(RT_ROUNDUP) */
66
67 /*
68 * If ROUNDUP has not yet been defined in terms of platform-provided
69 * defines, attempt to cope with heuristics.
70 */
71 #if !defined(ROUNDUP)
72
73 /*
74 * If you're porting to a platform that changed RT_ROUNDUP but doesn't
75 * have it in its headers, this will break rather obviously and you'll
76 * have to fix it here.
77 */
78 #define ROUNDUP(a) \
79 ((a) > 0 ? (1 + (((a)-1) | (sizeof(ROUNDUP_TYPE) - 1))) \
80 : sizeof(ROUNDUP_TYPE))
81
82 #endif /* defined(ROUNDUP) */
83
84
85 #if defined(SA_SIZE)
86 /* SAROUNDUP is the only thing we need, and SA_SIZE provides that */
87 #define SAROUNDUP(a) SA_SIZE(a)
88 #else /* !SA_SIZE */
89 /*
90 * Given a pointer (sockaddr or void *), return the number of bytes
91 * taken up by the sockaddr and any padding needed for alignment.
92 */
93 #if defined(HAVE_STRUCT_SOCKADDR_SA_LEN)
94 #define SAROUNDUP(X) ROUNDUP(((struct sockaddr *)(X))->sa_len)
95 #else
96 /*
97 * One would hope all fixed-size structure definitions are aligned,
98 * but round them up nonetheless.
99 */
100 #define SAROUNDUP(X) \
101 (((struct sockaddr *)(X))->sa_family == AF_INET \
102 ? ROUNDUP(sizeof(struct sockaddr_in)) \
103 : (((struct sockaddr *)(X))->sa_family == AF_INET6 \
104 ? ROUNDUP(sizeof(struct sockaddr_in6)) \
105 : (((struct sockaddr *)(X))->sa_family == AF_LINK \
106 ? ROUNDUP(sizeof(struct sockaddr_dl)) \
107 : sizeof(struct sockaddr))))
108 #endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
109
110 #endif /* !SA_SIZE */
111
112 /* Routing socket message types. */
113 const struct message rtm_type_str[] = {{RTM_ADD, "RTM_ADD"},
114 {RTM_DELETE, "RTM_DELETE"},
115 {RTM_CHANGE, "RTM_CHANGE"},
116 {RTM_GET, "RTM_GET"},
117 {RTM_LOSING, "RTM_LOSING"},
118 {RTM_REDIRECT, "RTM_REDIRECT"},
119 {RTM_MISS, "RTM_MISS"},
120 #ifdef RTM_LOCK
121 {RTM_LOCK, "RTM_LOCK"},
122 #endif /* RTM_LOCK */
123 #ifdef OLDADD
124 {RTM_OLDADD, "RTM_OLDADD"},
125 #endif /* RTM_OLDADD */
126 #ifdef RTM_OLDDEL
127 {RTM_OLDDEL, "RTM_OLDDEL"},
128 #endif /* RTM_OLDDEL */
129 #ifdef RTM_RESOLVE
130 {RTM_RESOLVE, "RTM_RESOLVE"},
131 #endif /* RTM_RESOLVE */
132 {RTM_NEWADDR, "RTM_NEWADDR"},
133 {RTM_DELADDR, "RTM_DELADDR"},
134 {RTM_IFINFO, "RTM_IFINFO"},
135 #ifdef RTM_OIFINFO
136 {RTM_OIFINFO, "RTM_OIFINFO"},
137 #endif /* RTM_OIFINFO */
138 #ifdef RTM_NEWMADDR
139 {RTM_NEWMADDR, "RTM_NEWMADDR"},
140 #endif /* RTM_NEWMADDR */
141 #ifdef RTM_DELMADDR
142 {RTM_DELMADDR, "RTM_DELMADDR"},
143 #endif /* RTM_DELMADDR */
144 #ifdef RTM_IFANNOUNCE
145 {RTM_IFANNOUNCE, "RTM_IFANNOUNCE"},
146 #endif /* RTM_IFANNOUNCE */
147 #ifdef RTM_IEEE80211
148 {RTM_IEEE80211, "RTM_IEEE80211"},
149 #endif
150 {0}};
151
152 static const struct message rtm_flag_str[] = {{RTF_UP, "UP"},
153 {RTF_GATEWAY, "GATEWAY"},
154 {RTF_HOST, "HOST"},
155 {RTF_REJECT, "REJECT"},
156 {RTF_DYNAMIC, "DYNAMIC"},
157 {RTF_MODIFIED, "MODIFIED"},
158 {RTF_DONE, "DONE"},
159 #ifdef RTF_MASK
160 {RTF_MASK, "MASK"},
161 #endif /* RTF_MASK */
162 #ifdef RTF_CLONING
163 {RTF_CLONING, "CLONING"},
164 #endif /* RTF_CLONING */
165 #ifdef RTF_XRESOLVE
166 {RTF_XRESOLVE, "XRESOLVE"},
167 #endif /* RTF_XRESOLVE */
168 #ifdef RTF_LLINFO
169 {RTF_LLINFO, "LLINFO"},
170 #endif /* RTF_LLINFO */
171 {RTF_STATIC, "STATIC"},
172 {RTF_BLACKHOLE, "BLACKHOLE"},
173 #ifdef RTF_PRIVATE
174 {RTF_PRIVATE, "PRIVATE"},
175 #endif /* RTF_PRIVATE */
176 {RTF_PROTO1, "PROTO1"},
177 {RTF_PROTO2, "PROTO2"},
178 #ifdef RTF_PRCLONING
179 {RTF_PRCLONING, "PRCLONING"},
180 #endif /* RTF_PRCLONING */
181 #ifdef RTF_WASCLONED
182 {RTF_WASCLONED, "WASCLONED"},
183 #endif /* RTF_WASCLONED */
184 #ifdef RTF_PROTO3
185 {RTF_PROTO3, "PROTO3"},
186 #endif /* RTF_PROTO3 */
187 #ifdef RTF_PINNED
188 {RTF_PINNED, "PINNED"},
189 #endif /* RTF_PINNED */
190 #ifdef RTF_LOCAL
191 {RTF_LOCAL, "LOCAL"},
192 #endif /* RTF_LOCAL */
193 #ifdef RTF_BROADCAST
194 {RTF_BROADCAST, "BROADCAST"},
195 #endif /* RTF_BROADCAST */
196 #ifdef RTF_MULTICAST
197 {RTF_MULTICAST, "MULTICAST"},
198 #endif /* RTF_MULTICAST */
199 #ifdef RTF_MULTIRT
200 {RTF_MULTIRT, "MULTIRT"},
201 #endif /* RTF_MULTIRT */
202 #ifdef RTF_SETSRC
203 {RTF_SETSRC, "SETSRC"},
204 #endif /* RTF_SETSRC */
205 {0}};
206
207 /* Kernel routing update socket. */
208 int routing_sock = -1;
209
210 /* Kernel dataplane routing update socket, used in the dataplane pthread
211 * context.
212 */
213 int dplane_routing_sock = -1;
214
215 /* Yes I'm checking ugly routing socket behavior. */
216 /* #define DEBUG */
217
218 size_t _rta_get(caddr_t sap, void *destp, size_t destlen, bool checkaf);
219 size_t rta_get(caddr_t sap, void *dest, size_t destlen);
220 size_t rta_getattr(caddr_t sap, void *destp, size_t destlen);
221 size_t rta_getsdlname(caddr_t sap, void *dest, short *destlen);
222 const char *rtatostr(unsigned int flags, char *buf, size_t buflen);
223
224 /* Supported address family check. */
225 static inline int af_check(int family)
226 {
227 if (family == AF_INET)
228 return 1;
229 if (family == AF_INET6)
230 return 1;
231 return 0;
232 }
233
234 size_t _rta_get(caddr_t sap, void *destp, size_t destlen, bool checkaf)
235 {
236 struct sockaddr *sa = (struct sockaddr *)sap;
237 struct sockaddr_dl *sdl;
238 uint8_t *dest = destp;
239 size_t tlen, copylen;
240
241 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
242 copylen = sa->sa_len;
243 tlen = (copylen == 0) ? sizeof(ROUNDUP_TYPE) : ROUNDUP(copylen);
244 #else /* !HAVE_STRUCT_SOCKADDR_SA_LEN */
245 copylen = tlen = SAROUNDUP(sap);
246 #endif /* !HAVE_STRUCT_SOCKADDR_SA_LEN */
247
248 if (copylen > 0 && dest != NULL) {
249 if (checkaf && af_check(sa->sa_family) == 0)
250 return tlen;
251 /*
252 * Handle sockaddr_dl corner case:
253 * RTA_NETMASK might be AF_LINK, but it doesn't anything
254 * relevant (e.g. zeroed out fields). Check for this
255 * case and avoid warning log message.
256 */
257 if (sa->sa_family == AF_LINK) {
258 sdl = (struct sockaddr_dl *)sa;
259 if (sdl->sdl_index == 0 || sdl->sdl_nlen == 0)
260 copylen = destlen;
261 }
262
263 if (copylen > destlen) {
264 zlog_warn(
265 "%s: destination buffer too small (%zu vs %zu)",
266 __func__, copylen, destlen);
267 memcpy(dest, sap, destlen);
268 } else
269 memcpy(dest, sap, copylen);
270 }
271
272 return tlen;
273 }
274
275 size_t rta_get(caddr_t sap, void *destp, size_t destlen)
276 {
277 return _rta_get(sap, destp, destlen, true);
278 }
279
280 size_t rta_getattr(caddr_t sap, void *destp, size_t destlen)
281 {
282 return _rta_get(sap, destp, destlen, false);
283 }
284
285 size_t rta_getsdlname(caddr_t sap, void *destp, short *destlen)
286 {
287 struct sockaddr_dl *sdl = (struct sockaddr_dl *)sap;
288 uint8_t *dest = destp;
289 size_t tlen, copylen;
290
291 copylen = sdl->sdl_nlen;
292 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
293 struct sockaddr *sa = (struct sockaddr *)sap;
294
295 tlen = (sa->sa_len == 0) ? sizeof(ROUNDUP_TYPE) : ROUNDUP(sa->sa_len);
296 #else /* !HAVE_STRUCT_SOCKADDR_SA_LEN */
297 tlen = SAROUNDUP(sap);
298 #endif /* !HAVE_STRUCT_SOCKADDR_SA_LEN */
299
300 if (copylen > 0 && dest != NULL && sdl->sdl_family == AF_LINK) {
301 if (copylen > IFNAMSIZ) {
302 zlog_warn(
303 "%s: destination buffer too small (%zu vs %d)",
304 __func__, copylen, IFNAMSIZ);
305 memcpy(dest, sdl->sdl_data, IFNAMSIZ);
306 dest[IFNAMSIZ] = 0;
307 *destlen = IFNAMSIZ;
308 } else {
309 memcpy(dest, sdl->sdl_data, copylen);
310 dest[copylen] = 0;
311 *destlen = copylen;
312 }
313 } else
314 *destlen = 0;
315
316 return tlen;
317 }
318
319 const char *rtatostr(unsigned int flags, char *buf, size_t buflen)
320 {
321 const char *flagstr, *bufstart;
322 int bit, wlen;
323 char ustr[32];
324
325 /* Hold the pointer to the buffer beginning. */
326 bufstart = buf;
327
328 for (bit = 1; bit; bit <<= 1) {
329 if ((flags & bit) == 0)
330 continue;
331
332 switch (bit) {
333 case RTA_DST:
334 flagstr = "DST";
335 break;
336 case RTA_GATEWAY:
337 flagstr = "GATEWAY";
338 break;
339 case RTA_NETMASK:
340 flagstr = "NETMASK";
341 break;
342 #ifdef RTA_GENMASK
343 case RTA_GENMASK:
344 flagstr = "GENMASK";
345 break;
346 #endif /* RTA_GENMASK */
347 case RTA_IFP:
348 flagstr = "IFP";
349 break;
350 case RTA_IFA:
351 flagstr = "IFA";
352 break;
353 #ifdef RTA_AUTHOR
354 case RTA_AUTHOR:
355 flagstr = "AUTHOR";
356 break;
357 #endif /* RTA_AUTHOR */
358 case RTA_BRD:
359 flagstr = "BRD";
360 break;
361 #ifdef RTA_SRC
362 case RTA_SRC:
363 flagstr = "SRC";
364 break;
365 #endif /* RTA_SRC */
366 #ifdef RTA_SRCMASK
367 case RTA_SRCMASK:
368 flagstr = "SRCMASK";
369 break;
370 #endif /* RTA_SRCMASK */
371 #ifdef RTA_LABEL
372 case RTA_LABEL:
373 flagstr = "LABEL";
374 break;
375 #endif /* RTA_LABEL */
376
377 default:
378 snprintf(ustr, sizeof(ustr), "0x%x", bit);
379 flagstr = ustr;
380 break;
381 }
382
383 wlen = snprintf(buf, buflen, "%s,", flagstr);
384 buf += wlen;
385 buflen -= wlen;
386 }
387
388 /* Check for empty buffer. */
389 if (bufstart != buf)
390 buf--;
391
392 /* Remove the last comma. */
393 *buf = 0;
394
395 return bufstart;
396 }
397
398 /* Dump routing table flag for debug purpose. */
399 static void rtm_flag_dump(int flag)
400 {
401 const struct message *mes;
402 static char buf[BUFSIZ];
403
404 buf[0] = '\0';
405 for (mes = rtm_flag_str; mes->key != 0; mes++) {
406 if (mes->key & flag) {
407 strlcat(buf, mes->str, BUFSIZ);
408 strlcat(buf, " ", BUFSIZ);
409 }
410 }
411 zlog_debug("Kernel: %s", buf);
412 }
413
414 #ifdef RTM_IFANNOUNCE
415 /* Interface adding function */
416 static int ifan_read(struct if_announcemsghdr *ifan)
417 {
418 struct interface *ifp;
419
420 ifp = if_lookup_by_index(ifan->ifan_index, VRF_DEFAULT);
421
422 if (ifp)
423 assert((ifp->ifindex == ifan->ifan_index)
424 || (ifp->ifindex == IFINDEX_INTERNAL));
425
426 if ((ifp == NULL) || ((ifp->ifindex == IFINDEX_INTERNAL)
427 && (ifan->ifan_what == IFAN_ARRIVAL))) {
428 if (IS_ZEBRA_DEBUG_KERNEL)
429 zlog_debug(
430 "%s: creating interface for ifindex %d, name %s",
431 __func__, ifan->ifan_index, ifan->ifan_name);
432
433 /* Create Interface */
434 ifp = if_get_by_name(ifan->ifan_name, VRF_DEFAULT,
435 VRF_DEFAULT_NAME);
436 if_set_index(ifp, ifan->ifan_index);
437
438 if_get_metric(ifp);
439 if_add_update(ifp);
440 } else if (ifp != NULL && ifan->ifan_what == IFAN_DEPARTURE)
441 if_delete_update(&ifp);
442
443 if (ifp) {
444 if_get_flags(ifp);
445 if_get_mtu(ifp);
446 if_get_metric(ifp);
447 }
448 if (IS_ZEBRA_DEBUG_KERNEL)
449 zlog_debug("%s: interface %s index %d", __func__,
450 ifan->ifan_name, ifan->ifan_index);
451
452 return 0;
453 }
454 #endif /* RTM_IFANNOUNCE */
455
456 #ifdef HAVE_BSD_IFI_LINK_STATE
457 /* BSD link detect translation */
458 static void bsd_linkdetect_translate(struct if_msghdr *ifm)
459 {
460 if ((ifm->ifm_data.ifi_link_state >= LINK_STATE_UP)
461 || (ifm->ifm_data.ifi_link_state == LINK_STATE_UNKNOWN))
462 SET_FLAG(ifm->ifm_flags, IFF_RUNNING);
463 else
464 UNSET_FLAG(ifm->ifm_flags, IFF_RUNNING);
465 }
466 #endif /* HAVE_BSD_IFI_LINK_STATE */
467
468 static enum zebra_link_type sdl_to_zebra_link_type(unsigned int sdlt)
469 {
470 switch (sdlt) {
471 case IFT_ETHER:
472 return ZEBRA_LLT_ETHER;
473 case IFT_X25:
474 return ZEBRA_LLT_X25;
475 case IFT_FDDI:
476 return ZEBRA_LLT_FDDI;
477 case IFT_PPP:
478 return ZEBRA_LLT_PPP;
479 case IFT_LOOP:
480 return ZEBRA_LLT_LOOPBACK;
481 case IFT_SLIP:
482 return ZEBRA_LLT_SLIP;
483 case IFT_ARCNET:
484 return ZEBRA_LLT_ARCNET;
485 case IFT_ATM:
486 return ZEBRA_LLT_ATM;
487 case IFT_LOCALTALK:
488 return ZEBRA_LLT_LOCALTLK;
489 case IFT_HIPPI:
490 return ZEBRA_LLT_HIPPI;
491 #ifdef IFT_IEEE1394
492 case IFT_IEEE1394:
493 return ZEBRA_LLT_IEEE1394;
494 #endif
495
496 default:
497 return ZEBRA_LLT_UNKNOWN;
498 }
499 }
500
501 /*
502 * Handle struct if_msghdr obtained from reading routing socket or
503 * sysctl (from interface_list). There may or may not be sockaddrs
504 * present after the header.
505 */
506 int ifm_read(struct if_msghdr *ifm)
507 {
508 struct interface *ifp = NULL;
509 struct sockaddr_dl *sdl = NULL;
510 char ifname[IFNAMSIZ];
511 short ifnlen = 0;
512 int maskbit;
513 caddr_t cp;
514 char fbuf[64];
515
516 /* terminate ifname at head (for strnlen) and tail (for safety) */
517 ifname[IFNAMSIZ - 1] = '\0';
518
519 /* paranoia: sanity check structure */
520 if (ifm->ifm_msglen < sizeof(struct if_msghdr)) {
521 flog_err(EC_ZEBRA_NETLINK_LENGTH_ERROR,
522 "%s: ifm->ifm_msglen %d too short", __func__,
523 ifm->ifm_msglen);
524 return -1;
525 }
526
527 /*
528 * Check for a sockaddr_dl following the message. First, point to
529 * where a socakddr might be if one follows the message.
530 */
531 cp = (void *)(ifm + 1);
532
533 /* Look up for RTA_IFP and skip others. */
534 for (maskbit = 1; maskbit; maskbit <<= 1) {
535 if ((maskbit & ifm->ifm_addrs) == 0)
536 continue;
537 if (maskbit != RTA_IFP) {
538 cp += rta_get(cp, NULL, 0);
539 continue;
540 }
541
542 /* Save the pointer to the structure. */
543 sdl = (struct sockaddr_dl *)cp;
544 cp += rta_getsdlname(cp, ifname, &ifnlen);
545 }
546
547 if (IS_ZEBRA_DEBUG_KERNEL)
548 zlog_debug("%s: sdl ifname %s addrs {%s}", __func__,
549 (ifnlen ? ifname : "(nil)"),
550 rtatostr(ifm->ifm_addrs, fbuf, sizeof(fbuf)));
551
552 /*
553 * Look up on ifindex first, because ifindices are the primary handle
554 * for
555 * interfaces across the user/kernel boundary, for most systems. (Some
556 * messages, such as up/down status changes on NetBSD, do not include a
557 * sockaddr_dl).
558 */
559 if ((ifp = if_lookup_by_index(ifm->ifm_index, VRF_DEFAULT)) != NULL) {
560 /* we have an ifp, verify that the name matches as some systems,
561 * eg Solaris, have a 1:many association of ifindex:ifname
562 * if they dont match, we dont have the correct ifp and should
563 * set it back to NULL to let next check do lookup by name
564 */
565 if (ifnlen && (strncmp(ifp->name, ifname, IFNAMSIZ) != 0)) {
566 if (IS_ZEBRA_DEBUG_KERNEL)
567 zlog_debug(
568 "%s: ifp name %s doesn't match sdl name %s",
569 __func__, ifp->name, ifname);
570 ifp = NULL;
571 }
572 }
573
574 /*
575 * If we dont have an ifp, try looking up by name. Particularly as some
576 * systems (Solaris) have a 1:many mapping of ifindex:ifname - the
577 * ifname
578 * is therefore our unique handle to that interface.
579 *
580 * Interfaces specified in the configuration file for which the ifindex
581 * has not been determined will have ifindex == IFINDEX_INTERNAL, and
582 * such
583 * interfaces are found by this search, and then their ifindex values
584 * can
585 * be filled in.
586 */
587 if ((ifp == NULL) && ifnlen)
588 ifp = if_lookup_by_name(ifname, VRF_DEFAULT);
589
590 /*
591 * If ifp still does not exist or has an invalid index
592 * (IFINDEX_INTERNAL),
593 * create or fill in an interface.
594 */
595 if ((ifp == NULL) || (ifp->ifindex == IFINDEX_INTERNAL)) {
596 /*
597 * To create or fill in an interface, a sockaddr_dl (via
598 * RTA_IFP) is required.
599 */
600 if (!ifnlen) {
601 zlog_debug("Interface index %d (new) missing ifname",
602 ifm->ifm_index);
603 return -1;
604 }
605
606 #ifndef RTM_IFANNOUNCE
607 /* Down->Down interface should be ignored here.
608 * See further comment below.
609 */
610 if (!CHECK_FLAG(ifm->ifm_flags, IFF_UP))
611 return 0;
612 #endif /* !RTM_IFANNOUNCE */
613
614 if (ifp == NULL) {
615 /* Interface that zebra was not previously aware of, so
616 * create. */
617 ifp = if_get_by_name(ifname, VRF_DEFAULT,
618 VRF_DEFAULT_NAME);
619 if (IS_ZEBRA_DEBUG_KERNEL)
620 zlog_debug("%s: creating ifp for ifindex %d",
621 __func__, ifm->ifm_index);
622 }
623
624 if (IS_ZEBRA_DEBUG_KERNEL)
625 zlog_debug(
626 "%s: updated/created ifp, ifname %s, ifindex %d",
627 __func__, ifp->name, ifp->ifindex);
628 /*
629 * Fill in newly created interface structure, or larval
630 * structure with ifindex IFINDEX_INTERNAL.
631 */
632 if_set_index(ifp, ifm->ifm_index);
633
634 #ifdef HAVE_BSD_IFI_LINK_STATE /* translate BSD kernel msg for link-state */
635 bsd_linkdetect_translate(ifm);
636 #endif /* HAVE_BSD_IFI_LINK_STATE */
637
638 if_flags_update(ifp, ifm->ifm_flags);
639 #if defined(__bsdi__)
640 if_kvm_get_mtu(ifp);
641 #else
642 if_get_mtu(ifp);
643 #endif /* __bsdi__ */
644 if_get_metric(ifp);
645
646 /*
647 * XXX sockaddr_dl contents can be larger than the structure
648 * definition. There are 2 big families here:
649 * - BSD has sdl_len + sdl_data[16] + overruns sdl_data
650 * we MUST use sdl_len here or we'll truncate data.
651 * - Solaris has no sdl_len, but sdl_data[244]
652 * presumably, it's not going to run past that, so sizeof()
653 * is fine here.
654 * a nonzero ifnlen from rta_getsdlname() means sdl is valid
655 */
656 ifp->ll_type = ZEBRA_LLT_UNKNOWN;
657 ifp->hw_addr_len = 0;
658 if (ifnlen) {
659 #ifdef HAVE_STRUCT_SOCKADDR_DL_SDL_LEN
660 memcpy(&((struct zebra_if *)ifp->info)->sdl, sdl,
661 sdl->sdl_len);
662 #else
663 memcpy(&((struct zebra_if *)ifp->info)->sdl, sdl,
664 sizeof(struct sockaddr_dl));
665 #endif /* HAVE_STRUCT_SOCKADDR_DL_SDL_LEN */
666
667 ifp->ll_type = sdl_to_zebra_link_type(sdl->sdl_type);
668 if (sdl->sdl_alen <= sizeof(ifp->hw_addr)) {
669 memcpy(ifp->hw_addr, LLADDR(sdl),
670 sdl->sdl_alen);
671 ifp->hw_addr_len = sdl->sdl_alen;
672 }
673 }
674
675 if_add_update(ifp);
676 } else
677 /*
678 * Interface structure exists. Adjust stored flags from
679 * notification. If interface has up->down or down->up
680 * transition, call state change routines (to adjust routes,
681 * notify routing daemons, etc.). (Other flag changes are stored
682 * but apparently do not trigger action.)
683 */
684 {
685 if (ifp->ifindex != ifm->ifm_index) {
686 zlog_debug(
687 "%s: index mismatch, ifname %s, ifp index %d, ifm index %d",
688 __func__, ifp->name, ifp->ifindex,
689 ifm->ifm_index);
690 return -1;
691 }
692
693 #ifdef HAVE_BSD_IFI_LINK_STATE /* translate BSD kernel msg for link-state */
694 bsd_linkdetect_translate(ifm);
695 #endif /* HAVE_BSD_IFI_LINK_STATE */
696
697 /* update flags and handle operative->inoperative transition, if
698 * any */
699 if_flags_update(ifp, ifm->ifm_flags);
700
701 #ifndef RTM_IFANNOUNCE
702 if (!if_is_up(ifp)) {
703 /* No RTM_IFANNOUNCE on this platform, so we can never
704 * distinguish between ~IFF_UP and delete. We must
705 * presume
706 * it has been deleted.
707 * Eg, Solaris will not notify us of unplumb.
708 *
709 * XXX: Fixme - this should be runtime detected
710 * So that a binary compiled on a system with IFANNOUNCE
711 * will still behave correctly if run on a platform
712 * without
713 */
714 if_delete_update(&ifp);
715 }
716 #endif /* RTM_IFANNOUNCE */
717 if (ifp && if_is_up(ifp)) {
718 #if defined(__bsdi__)
719 if_kvm_get_mtu(ifp);
720 #else
721 if_get_mtu(ifp);
722 #endif /* __bsdi__ */
723 if_get_metric(ifp);
724 }
725 }
726
727 if (ifp) {
728 #ifdef HAVE_NET_RT_IFLIST
729 ifp->stats = ifm->ifm_data;
730 #endif /* HAVE_NET_RT_IFLIST */
731 ifp->speed = ifm->ifm_data.ifi_baudrate / 1000000;
732
733 if (IS_ZEBRA_DEBUG_KERNEL)
734 zlog_debug("%s: interface %s index %d", __func__,
735 ifp->name, ifp->ifindex);
736 }
737
738 return 0;
739 }
740
741 /* Address read from struct ifa_msghdr. */
742 static void ifam_read_mesg(struct ifa_msghdr *ifm, union sockunion *addr,
743 union sockunion *mask, union sockunion *brd,
744 char *ifname, short *ifnlen)
745 {
746 caddr_t pnt, end;
747 union sockunion dst;
748 union sockunion gateway;
749 int maskbit;
750 char fbuf[64];
751
752 pnt = (caddr_t)(ifm + 1);
753 end = ((caddr_t)ifm) + ifm->ifam_msglen;
754
755 /* Be sure structure is cleared */
756 memset(mask, 0, sizeof(union sockunion));
757 memset(addr, 0, sizeof(union sockunion));
758 memset(brd, 0, sizeof(union sockunion));
759 memset(&dst, 0, sizeof(union sockunion));
760 memset(&gateway, 0, sizeof(union sockunion));
761
762 /* We fetch each socket variable into sockunion. */
763 for (maskbit = 1; maskbit; maskbit <<= 1) {
764 if ((maskbit & ifm->ifam_addrs) == 0)
765 continue;
766
767 switch (maskbit) {
768 case RTA_DST:
769 pnt += rta_get(pnt, &dst, sizeof(dst));
770 break;
771 case RTA_GATEWAY:
772 pnt += rta_get(pnt, &gateway, sizeof(gateway));
773 break;
774 case RTA_NETMASK:
775 pnt += rta_getattr(pnt, mask, sizeof(*mask));
776 break;
777 case RTA_IFP:
778 pnt += rta_getsdlname(pnt, ifname, ifnlen);
779 break;
780 case RTA_IFA:
781 pnt += rta_get(pnt, addr, sizeof(*addr));
782 break;
783 case RTA_BRD:
784 pnt += rta_get(pnt, brd, sizeof(*brd));
785 break;
786
787 default:
788 pnt += rta_get(pnt, NULL, 0);
789 break;
790 }
791
792 if (pnt > end) {
793 zlog_warn("%s: overflow detected (pnt:%p end:%p)",
794 __func__, pnt, end);
795 break;
796 }
797 }
798
799 if (IS_ZEBRA_DEBUG_KERNEL) {
800 switch (sockunion_family(addr)) {
801 case AF_INET:
802 case AF_INET6: {
803 int masklen =
804 (sockunion_family(addr) == AF_INET)
805 ? ip_masklen(mask->sin.sin_addr)
806 : ip6_masklen(mask->sin6.sin6_addr);
807 zlog_debug(
808 "%s: ifindex %d, ifname %s, ifam_addrs {%s}, ifam_flags 0x%x, addr %pSU/%d broad %pSU dst %pSU gateway %pSU",
809 __func__, ifm->ifam_index,
810 (ifnlen ? ifname : "(nil)"),
811 rtatostr(ifm->ifam_addrs, fbuf, sizeof(fbuf)),
812 ifm->ifam_flags, addr, masklen, brd, &dst,
813 &gateway);
814 } break;
815 default:
816 zlog_debug("%s: ifindex %d, ifname %s, ifam_addrs {%s}",
817 __func__, ifm->ifam_index,
818 (ifnlen ? ifname : "(nil)"),
819 rtatostr(ifm->ifam_addrs, fbuf,
820 sizeof(fbuf)));
821 break;
822 }
823 }
824
825 /* Assert read up end point matches to end point */
826 pnt = (caddr_t)ROUNDUP((size_t)pnt);
827 if (pnt != (caddr_t)ROUNDUP((size_t)end))
828 zlog_debug("ifam_read() doesn't read all socket data");
829 }
830
831 /* Interface's address information get. */
832 int ifam_read(struct ifa_msghdr *ifam)
833 {
834 struct interface *ifp = NULL;
835 union sockunion addr, mask, brd;
836 bool dest_same = false;
837 char ifname[INTERFACE_NAMSIZ];
838 short ifnlen = 0;
839 bool isalias = false;
840 uint32_t flags = 0;
841
842 ifname[0] = ifname[INTERFACE_NAMSIZ - 1] = '\0';
843
844 /* Allocate and read address information. */
845 ifam_read_mesg(ifam, &addr, &mask, &brd, ifname, &ifnlen);
846
847 if ((ifp = if_lookup_by_index(ifam->ifam_index, VRF_DEFAULT)) == NULL) {
848 flog_warn(EC_ZEBRA_UNKNOWN_INTERFACE,
849 "%s: no interface for ifname %s, index %d", __func__,
850 ifname, ifam->ifam_index);
851 return -1;
852 }
853
854 if (ifnlen && strncmp(ifp->name, ifname, INTERFACE_NAMSIZ))
855 isalias = true;
856
857 /*
858 * Mark the alias prefixes as secondary
859 */
860 if (isalias)
861 SET_FLAG(flags, ZEBRA_IFA_SECONDARY);
862
863 /* N.B. The info in ifa_msghdr does not tell us whether the RTA_BRD
864 field contains a broadcast address or a peer address, so we are
865 forced to
866 rely upon the interface type. */
867 if (if_is_pointopoint(ifp))
868 SET_FLAG(flags, ZEBRA_IFA_PEER);
869 else {
870 if (memcmp(&addr, &brd, sizeof(addr)) == 0)
871 dest_same = true;
872 }
873
874 #if 0
875 /* it might seem cute to grab the interface metric here, however
876 * we're processing an address update message, and so some systems
877 * (e.g. FBSD) dont bother to fill in ifam_metric. Disabled, but left
878 * in deliberately, as comment.
879 */
880 ifp->metric = ifam->ifam_metric;
881 #endif
882
883 /* Add connected address. */
884 switch (sockunion_family(&addr)) {
885 case AF_INET:
886 if (ifam->ifam_type == RTM_NEWADDR)
887 connected_add_ipv4(ifp, flags, &addr.sin.sin_addr,
888 ip_masklen(mask.sin.sin_addr),
889 dest_same ? NULL : &brd.sin.sin_addr,
890 (isalias ? ifname : NULL),
891 METRIC_MAX);
892 else
893 connected_delete_ipv4(ifp, flags, &addr.sin.sin_addr,
894 ip_masklen(mask.sin.sin_addr),
895 dest_same ? NULL
896 : &brd.sin.sin_addr);
897 break;
898 case AF_INET6:
899 /* Unset interface index from link-local address when IPv6 stack
900 is KAME. */
901 if (IN6_IS_ADDR_LINKLOCAL(&addr.sin6.sin6_addr)) {
902 SET_IN6_LINKLOCAL_IFINDEX(addr.sin6.sin6_addr, 0);
903 }
904
905 if (ifam->ifam_type == RTM_NEWADDR)
906 connected_add_ipv6(ifp, flags, &addr.sin6.sin6_addr,
907 NULL,
908 ip6_masklen(mask.sin6.sin6_addr),
909 (isalias ? ifname : NULL),
910 METRIC_MAX);
911 else
912 connected_delete_ipv6(ifp, &addr.sin6.sin6_addr, NULL,
913 ip6_masklen(mask.sin6.sin6_addr));
914 break;
915 default:
916 /* Unsupported family silently ignore... */
917 break;
918 }
919
920 /* Check interface flag for implicit up of the interface. */
921 if_refresh(ifp);
922
923 return 0;
924 }
925
926 /* Interface function for reading kernel routing table information. */
927 static int rtm_read_mesg(struct rt_msghdr *rtm, union sockunion *dest,
928 union sockunion *mask, union sockunion *gate,
929 char *ifname, short *ifnlen)
930 {
931 caddr_t pnt, end;
932 int maskbit;
933
934 /* Pnt points out socket data start point. */
935 pnt = (caddr_t)(rtm + 1);
936 end = ((caddr_t)rtm) + rtm->rtm_msglen;
937
938 /* rt_msghdr version check. */
939 if (rtm->rtm_version != RTM_VERSION)
940 flog_warn(EC_ZEBRA_RTM_VERSION_MISMATCH,
941 "Routing message version different %d should be %d.This may cause problem",
942 rtm->rtm_version, RTM_VERSION);
943
944 /* Be sure structure is cleared */
945 memset(dest, 0, sizeof(union sockunion));
946 memset(gate, 0, sizeof(union sockunion));
947 memset(mask, 0, sizeof(union sockunion));
948
949 /* We fetch each socket variable into sockunion. */
950 /* We fetch each socket variable into sockunion. */
951 for (maskbit = 1; maskbit; maskbit <<= 1) {
952 if ((maskbit & rtm->rtm_addrs) == 0)
953 continue;
954
955 switch (maskbit) {
956 case RTA_DST:
957 pnt += rta_get(pnt, dest, sizeof(*dest));
958 break;
959 case RTA_GATEWAY:
960 pnt += rta_get(pnt, gate, sizeof(*gate));
961 break;
962 case RTA_NETMASK:
963 pnt += rta_getattr(pnt, mask, sizeof(*mask));
964 break;
965 case RTA_IFP:
966 pnt += rta_getsdlname(pnt, ifname, ifnlen);
967 break;
968
969 default:
970 pnt += rta_get(pnt, NULL, 0);
971 break;
972 }
973
974 if (pnt > end) {
975 zlog_warn("%s: overflow detected (pnt:%p end:%p)",
976 __func__, pnt, end);
977 break;
978 }
979 }
980
981 /* If there is netmask information set it's family same as
982 destination family*/
983 if (rtm->rtm_addrs & RTA_NETMASK)
984 mask->sa.sa_family = dest->sa.sa_family;
985
986 /* Assert read up to the end of pointer. */
987 if (pnt != end)
988 zlog_debug("rtm_read() doesn't read all socket data.");
989
990 return rtm->rtm_flags;
991 }
992
993 void rtm_read(struct rt_msghdr *rtm)
994 {
995 int flags;
996 uint32_t zebra_flags;
997 union sockunion dest, mask, gate;
998 char ifname[INTERFACE_NAMSIZ + 1];
999 short ifnlen = 0;
1000 struct nexthop nh;
1001 struct prefix p;
1002 ifindex_t ifindex = 0;
1003 afi_t afi;
1004 char fbuf[64];
1005 int32_t proto = ZEBRA_ROUTE_KERNEL;
1006 uint8_t distance = 0;
1007
1008 zebra_flags = 0;
1009
1010 /* Read destination and netmask and gateway from rtm message
1011 structure. */
1012 flags = rtm_read_mesg(rtm, &dest, &mask, &gate, ifname, &ifnlen);
1013 if (!(flags & RTF_DONE))
1014 return;
1015 if (IS_ZEBRA_DEBUG_KERNEL)
1016 zlog_debug("%s: got rtm of type %d (%s) addrs {%s}", __func__,
1017 rtm->rtm_type,
1018 lookup_msg(rtm_type_str, rtm->rtm_type, NULL),
1019 rtatostr(rtm->rtm_addrs, fbuf, sizeof(fbuf)));
1020
1021 #ifdef RTF_CLONED /*bsdi, netbsd 1.6*/
1022 if (flags & RTF_CLONED)
1023 return;
1024 #endif
1025 #ifdef RTF_WASCLONED /*freebsd*/
1026 if (flags & RTF_WASCLONED)
1027 return;
1028 #endif
1029
1030 if ((rtm->rtm_type == RTM_ADD || rtm->rtm_type == RTM_CHANGE)
1031 && !(flags & RTF_UP))
1032 return;
1033
1034 /* This is connected route. */
1035 if (!(flags & RTF_GATEWAY))
1036 return;
1037
1038 if (flags & RTF_PROTO1) {
1039 SET_FLAG(zebra_flags, ZEBRA_FLAG_SELFROUTE);
1040 proto = ZEBRA_ROUTE_STATIC;
1041 distance = 255;
1042 }
1043
1044 memset(&nh, 0, sizeof(nh));
1045
1046 nh.vrf_id = VRF_DEFAULT;
1047 /* This is a reject or blackhole route */
1048 if (flags & RTF_REJECT) {
1049 nh.type = NEXTHOP_TYPE_BLACKHOLE;
1050 nh.bh_type = BLACKHOLE_REJECT;
1051 } else if (flags & RTF_BLACKHOLE) {
1052 nh.type = NEXTHOP_TYPE_BLACKHOLE;
1053 nh.bh_type = BLACKHOLE_NULL;
1054 }
1055
1056 /*
1057 * Ignore our own messages.
1058 */
1059 if (rtm->rtm_type != RTM_GET && rtm->rtm_pid == pid)
1060 return;
1061
1062 if (dest.sa.sa_family == AF_INET) {
1063 afi = AFI_IP;
1064 p.family = AF_INET;
1065 p.u.prefix4 = dest.sin.sin_addr;
1066 if (flags & RTF_HOST)
1067 p.prefixlen = IPV4_MAX_BITLEN;
1068 else
1069 p.prefixlen = ip_masklen(mask.sin.sin_addr);
1070
1071 if (!nh.type) {
1072 nh.type = NEXTHOP_TYPE_IPV4;
1073 nh.gate.ipv4 = gate.sin.sin_addr;
1074 }
1075 } else if (dest.sa.sa_family == AF_INET6) {
1076 afi = AFI_IP6;
1077 p.family = AF_INET6;
1078 p.u.prefix6 = dest.sin6.sin6_addr;
1079 if (flags & RTF_HOST)
1080 p.prefixlen = IPV6_MAX_BITLEN;
1081 else
1082 p.prefixlen = ip6_masklen(mask.sin6.sin6_addr);
1083
1084 #ifdef KAME
1085 if (IN6_IS_ADDR_LINKLOCAL(&gate.sin6.sin6_addr)) {
1086 ifindex = IN6_LINKLOCAL_IFINDEX(gate.sin6.sin6_addr);
1087 SET_IN6_LINKLOCAL_IFINDEX(gate.sin6.sin6_addr, 0);
1088 }
1089 #endif /* KAME */
1090
1091 if (!nh.type) {
1092 nh.type = ifindex ? NEXTHOP_TYPE_IPV6_IFINDEX
1093 : NEXTHOP_TYPE_IPV6;
1094 nh.gate.ipv6 = gate.sin6.sin6_addr;
1095 nh.ifindex = ifindex;
1096 }
1097 } else
1098 return;
1099
1100 if (rtm->rtm_type == RTM_GET || rtm->rtm_type == RTM_ADD
1101 || rtm->rtm_type == RTM_CHANGE)
1102 rib_add(afi, SAFI_UNICAST, VRF_DEFAULT, proto, 0, zebra_flags,
1103 &p, NULL, &nh, 0, RT_TABLE_MAIN, 0, 0, distance, 0,
1104 false);
1105 else
1106 rib_delete(afi, SAFI_UNICAST, VRF_DEFAULT, proto, 0,
1107 zebra_flags, &p, NULL, &nh, 0, RT_TABLE_MAIN, 0,
1108 distance, true);
1109 }
1110
1111 /* Interface function for the kernel routing table updates. Support
1112 * for RTM_CHANGE will be needed.
1113 * Exported only for rt_socket.c
1114 */
1115 int rtm_write(int message, union sockunion *dest, union sockunion *mask,
1116 union sockunion *gate, union sockunion *mpls, unsigned int index,
1117 enum blackhole_type bh_type, int metric)
1118 {
1119 int ret;
1120 caddr_t pnt;
1121 struct interface *ifp;
1122
1123 /* Sequencial number of routing message. */
1124 static int msg_seq = 0;
1125
1126 /* Struct of rt_msghdr and buffer for storing socket's data. */
1127 struct {
1128 struct rt_msghdr rtm;
1129 char buf[512];
1130 } msg;
1131
1132 if (dplane_routing_sock < 0)
1133 return ZEBRA_ERR_EPERM;
1134
1135 /* Clear and set rt_msghdr values */
1136 memset(&msg, 0, sizeof(msg));
1137 msg.rtm.rtm_version = RTM_VERSION;
1138 msg.rtm.rtm_type = message;
1139 msg.rtm.rtm_seq = msg_seq++;
1140 msg.rtm.rtm_addrs = RTA_DST;
1141 msg.rtm.rtm_addrs |= RTA_GATEWAY;
1142 msg.rtm.rtm_flags = RTF_UP;
1143 #ifdef __OpenBSD__
1144 msg.rtm.rtm_flags |= RTF_MPATH;
1145 msg.rtm.rtm_fmask = RTF_MPLS;
1146 #endif
1147 msg.rtm.rtm_index = index;
1148
1149 if (metric != 0) {
1150 msg.rtm.rtm_rmx.rmx_hopcount = metric;
1151 msg.rtm.rtm_inits |= RTV_HOPCOUNT;
1152 }
1153
1154 ifp = if_lookup_by_index(index, VRF_DEFAULT);
1155
1156 if (gate && (message == RTM_ADD || message == RTM_CHANGE))
1157 msg.rtm.rtm_flags |= RTF_GATEWAY;
1158
1159 /* When RTF_CLONING is unavailable on BSD, should we set some
1160 * other flag instead?
1161 */
1162 #ifdef RTF_CLONING
1163 if (!gate && (message == RTM_ADD || message == RTM_CHANGE) && ifp
1164 && (ifp->flags & IFF_POINTOPOINT) == 0)
1165 msg.rtm.rtm_flags |= RTF_CLONING;
1166 #endif /* RTF_CLONING */
1167
1168 /* If no protocol specific gateway is specified, use link
1169 address for gateway. */
1170 if (!gate) {
1171 if (!ifp) {
1172 char dest_buf[INET_ADDRSTRLEN] = "NULL",
1173 mask_buf[INET_ADDRSTRLEN] = "255.255.255.255";
1174 if (dest)
1175 inet_ntop(AF_INET, &dest->sin.sin_addr,
1176 dest_buf, INET_ADDRSTRLEN);
1177 if (mask)
1178 inet_ntop(AF_INET, &mask->sin.sin_addr,
1179 mask_buf, INET_ADDRSTRLEN);
1180 flog_warn(
1181 EC_ZEBRA_RTM_NO_GATEWAY,
1182 "%s: %s/%s: gate == NULL and no gateway found for ifindex %d",
1183 __func__, dest_buf, mask_buf, index);
1184 return -1;
1185 }
1186 gate = (union sockunion *)&((struct zebra_if *)ifp->info)->sdl;
1187 }
1188
1189 if (mask)
1190 msg.rtm.rtm_addrs |= RTA_NETMASK;
1191 else if (message == RTM_ADD || message == RTM_CHANGE)
1192 msg.rtm.rtm_flags |= RTF_HOST;
1193
1194 #ifdef __OpenBSD__
1195 if (mpls) {
1196 msg.rtm.rtm_addrs |= RTA_SRC;
1197 msg.rtm.rtm_flags |= RTF_MPLS;
1198
1199 if (mpls->smpls.smpls_label
1200 != htonl(MPLS_LABEL_IMPLICIT_NULL << MPLS_LABEL_OFFSET))
1201 msg.rtm.rtm_mpls = MPLS_OP_PUSH;
1202 }
1203 #endif
1204
1205 /* Tagging route with flags */
1206 msg.rtm.rtm_flags |= (RTF_PROTO1);
1207
1208 switch (bh_type) {
1209 case BLACKHOLE_UNSPEC:
1210 break;
1211 case BLACKHOLE_REJECT:
1212 msg.rtm.rtm_flags |= RTF_REJECT;
1213 break;
1214 case BLACKHOLE_NULL:
1215 case BLACKHOLE_ADMINPROHIB:
1216 msg.rtm.rtm_flags |= RTF_BLACKHOLE;
1217 break;
1218 }
1219
1220
1221 #define SOCKADDRSET(X, R) \
1222 if (msg.rtm.rtm_addrs & (R)) { \
1223 int len = SAROUNDUP(X); \
1224 memcpy(pnt, (caddr_t)(X), len); \
1225 pnt += len; \
1226 }
1227
1228 pnt = (caddr_t)msg.buf;
1229
1230 /* Write each socket data into rtm message buffer */
1231 SOCKADDRSET(dest, RTA_DST);
1232 SOCKADDRSET(gate, RTA_GATEWAY);
1233 SOCKADDRSET(mask, RTA_NETMASK);
1234 #ifdef __OpenBSD__
1235 SOCKADDRSET(mpls, RTA_SRC);
1236 #endif
1237
1238 msg.rtm.rtm_msglen = pnt - (caddr_t)&msg;
1239
1240 ret = write(dplane_routing_sock, &msg, msg.rtm.rtm_msglen);
1241
1242 if (ret != msg.rtm.rtm_msglen) {
1243 if (errno == EEXIST)
1244 return ZEBRA_ERR_RTEXIST;
1245 if (errno == ENETUNREACH)
1246 return ZEBRA_ERR_RTUNREACH;
1247 if (errno == ESRCH)
1248 return ZEBRA_ERR_RTNOEXIST;
1249
1250 flog_err_sys(EC_LIB_SOCKET, "%s: write : %s (%d)", __func__,
1251 safe_strerror(errno), errno);
1252 return ZEBRA_ERR_KERNEL;
1253 }
1254 return ZEBRA_ERR_NOERROR;
1255 }
1256
1257
1258 #include "thread.h"
1259 #include "zebra/zserv.h"
1260
1261 /* For debug purpose. */
1262 static void rtmsg_debug(struct rt_msghdr *rtm)
1263 {
1264 char fbuf[64];
1265
1266 zlog_debug("Kernel: Len: %d Type: %s", rtm->rtm_msglen,
1267 lookup_msg(rtm_type_str, rtm->rtm_type, NULL));
1268 rtm_flag_dump(rtm->rtm_flags);
1269 zlog_debug("Kernel: message seq %d", rtm->rtm_seq);
1270 zlog_debug("Kernel: pid %lld, rtm_addrs {%s}", (long long)rtm->rtm_pid,
1271 rtatostr(rtm->rtm_addrs, fbuf, sizeof(fbuf)));
1272 }
1273
1274 /* This is pretty gross, better suggestions welcome -- mhandler */
1275 #ifndef RTAX_MAX
1276 #ifdef RTA_NUMBITS
1277 #define RTAX_MAX RTA_NUMBITS
1278 #else
1279 #define RTAX_MAX 8
1280 #endif /* RTA_NUMBITS */
1281 #endif /* RTAX_MAX */
1282
1283 /* Kernel routing table and interface updates via routing socket. */
1284 static void kernel_read(struct thread *thread)
1285 {
1286 int sock;
1287 int nbytes;
1288 struct rt_msghdr *rtm;
1289
1290 /*
1291 * This must be big enough for any message the kernel might send.
1292 * Rather than determining how many sockaddrs of what size might be
1293 * in each particular message, just use RTAX_MAX of sockaddr_storage
1294 * for each. Note that the sockaddrs must be after each message
1295 * definition, or rather after whichever happens to be the largest,
1296 * since the buffer needs to be big enough for a message and the
1297 * sockaddrs together.
1298 */
1299 union {
1300 /* Routing information. */
1301 struct {
1302 struct rt_msghdr rtm;
1303 struct sockaddr_storage addr[RTAX_MAX];
1304 } r;
1305
1306 /* Interface information. */
1307 struct {
1308 struct if_msghdr ifm;
1309 struct sockaddr_storage addr[RTAX_MAX];
1310 } im;
1311
1312 /* Interface address information. */
1313 struct {
1314 struct ifa_msghdr ifa;
1315 struct sockaddr_storage addr[RTAX_MAX];
1316 } ia;
1317
1318 #ifdef RTM_IFANNOUNCE
1319 /* Interface arrival/departure */
1320 struct {
1321 struct if_announcemsghdr ifan;
1322 struct sockaddr_storage addr[RTAX_MAX];
1323 } ian;
1324 #endif /* RTM_IFANNOUNCE */
1325
1326 } buf;
1327
1328 /* Fetch routing socket. */
1329 sock = THREAD_FD(thread);
1330
1331 nbytes = read(sock, &buf, sizeof(buf));
1332
1333 if (nbytes < 0) {
1334 if (errno == ENOBUFS) {
1335 #ifdef __FreeBSD__
1336 /*
1337 * ENOBUFS indicates a temporary resource
1338 * shortage and is not harmful for consistency of
1339 * reading the routing socket. Ignore it.
1340 */
1341 thread_add_read(zrouter.master, kernel_read, NULL, sock,
1342 NULL);
1343 return;
1344 #else
1345 flog_err(EC_ZEBRA_RECVMSG_OVERRUN,
1346 "routing socket overrun: %s",
1347 safe_strerror(errno));
1348 /*
1349 * In this case we are screwed.
1350 * There is no good way to
1351 * recover zebra at this point.
1352 */
1353 exit(-1);
1354 #endif
1355 }
1356 if (errno != EAGAIN && errno != EWOULDBLOCK)
1357 flog_err_sys(EC_LIB_SOCKET, "routing socket error: %s",
1358 safe_strerror(errno));
1359 return;
1360 }
1361
1362 if (nbytes == 0)
1363 return;
1364
1365 thread_add_read(zrouter.master, kernel_read, NULL, sock, NULL);
1366
1367 if (IS_ZEBRA_DEBUG_KERNEL)
1368 rtmsg_debug(&buf.r.rtm);
1369
1370 rtm = &buf.r.rtm;
1371
1372 /*
1373 * Ensure that we didn't drop any data, so that processing routines
1374 * can assume they have the whole message.
1375 */
1376 if (rtm->rtm_msglen != nbytes) {
1377 zlog_debug("%s: rtm->rtm_msglen %d, nbytes %d, type %d",
1378 __func__, rtm->rtm_msglen, nbytes, rtm->rtm_type);
1379 return;
1380 }
1381
1382 switch (rtm->rtm_type) {
1383 case RTM_ADD:
1384 case RTM_DELETE:
1385 case RTM_CHANGE:
1386 rtm_read(rtm);
1387 break;
1388 case RTM_IFINFO:
1389 ifm_read(&buf.im.ifm);
1390 break;
1391 case RTM_NEWADDR:
1392 case RTM_DELADDR:
1393 ifam_read(&buf.ia.ifa);
1394 break;
1395 #ifdef RTM_IFANNOUNCE
1396 case RTM_IFANNOUNCE:
1397 ifan_read(&buf.ian.ifan);
1398 break;
1399 #endif /* RTM_IFANNOUNCE */
1400 default:
1401 if (IS_ZEBRA_DEBUG_KERNEL)
1402 zlog_debug(
1403 "Unprocessed RTM_type: %s(%d)",
1404 lookup_msg(rtm_type_str, rtm->rtm_type, NULL),
1405 rtm->rtm_type);
1406 break;
1407 }
1408 }
1409
1410 /* Make routing socket. */
1411 static void routing_socket(struct zebra_ns *zns)
1412 {
1413 uint32_t default_rcvbuf;
1414 socklen_t optlen;
1415
1416 frr_with_privs(&zserv_privs) {
1417 routing_sock = ns_socket(AF_ROUTE, SOCK_RAW, 0, zns->ns_id);
1418
1419 dplane_routing_sock =
1420 ns_socket(AF_ROUTE, SOCK_RAW, 0, zns->ns_id);
1421 }
1422
1423 if (routing_sock < 0) {
1424 flog_err_sys(EC_LIB_SOCKET, "Can't init kernel routing socket");
1425 return;
1426 }
1427
1428 if (dplane_routing_sock < 0) {
1429 flog_err_sys(EC_LIB_SOCKET,
1430 "Can't init kernel dataplane routing socket");
1431 return;
1432 }
1433
1434 #ifdef SO_RERROR
1435 /* Allow reporting of route(4) buffer overflow errors */
1436 int n = 1;
1437
1438 if (setsockopt(routing_sock, SOL_SOCKET, SO_RERROR, &n, sizeof(n)) < 0)
1439 flog_err_sys(EC_LIB_SOCKET,
1440 "Can't set SO_RERROR on routing socket");
1441 #endif
1442
1443 /* XXX: Socket should be NONBLOCK, however as we currently
1444 * discard failed writes, this will lead to inconsistencies.
1445 * For now, socket must be blocking.
1446 */
1447 /*if (fcntl (routing_sock, F_SETFL, O_NONBLOCK) < 0)
1448 zlog_warn ("Can't set O_NONBLOCK to routing socket");*/
1449
1450 /*
1451 * Attempt to set a more useful receive buffer size
1452 */
1453 optlen = sizeof(default_rcvbuf);
1454 if (getsockopt(routing_sock, SOL_SOCKET, SO_RCVBUF, &default_rcvbuf,
1455 &optlen) == -1)
1456 flog_err_sys(EC_LIB_SOCKET,
1457 "routing_sock sockopt SOL_SOCKET SO_RCVBUF");
1458 else {
1459 for (; rcvbufsize > default_rcvbuf &&
1460 setsockopt(routing_sock, SOL_SOCKET, SO_RCVBUF,
1461 &rcvbufsize, sizeof(rcvbufsize)) == -1 &&
1462 errno == ENOBUFS;
1463 rcvbufsize /= 2)
1464 ;
1465 }
1466
1467 /* kernel_read needs rewrite. */
1468 thread_add_read(zrouter.master, kernel_read, NULL, routing_sock, NULL);
1469 }
1470
1471 /* Exported interface function. This function simply calls
1472 routing_socket (). */
1473 void kernel_init(struct zebra_ns *zns)
1474 {
1475 routing_socket(zns);
1476 }
1477
1478 void kernel_terminate(struct zebra_ns *zns, bool complete)
1479 {
1480 return;
1481 }
1482
1483 /*
1484 * Global init for platform-/OS-specific things
1485 */
1486 void kernel_router_init(void)
1487 {
1488 }
1489
1490 /*
1491 * Global deinit for platform-/OS-specific things
1492 */
1493 void kernel_router_terminate(void)
1494 {
1495 }
1496
1497 /*
1498 * Called by the dplane pthread to read incoming OS messages and dispatch them.
1499 */
1500 int kernel_dplane_read(struct zebra_dplane_info *info)
1501 {
1502 return 0;
1503 }
1504
1505 void kernel_update_multi(struct dplane_ctx_list_head *ctx_list)
1506 {
1507 struct zebra_dplane_ctx *ctx;
1508 struct dplane_ctx_list_head handled_list;
1509 enum zebra_dplane_result res = ZEBRA_DPLANE_REQUEST_SUCCESS;
1510
1511 dplane_ctx_q_init(&handled_list);
1512
1513 while (true) {
1514 ctx = dplane_ctx_dequeue(ctx_list);
1515 if (ctx == NULL)
1516 break;
1517
1518 /*
1519 * A previous provider plugin may have asked to skip the
1520 * kernel update.
1521 */
1522 if (dplane_ctx_is_skip_kernel(ctx)) {
1523 res = ZEBRA_DPLANE_REQUEST_SUCCESS;
1524 goto skip_one;
1525 }
1526
1527 switch (dplane_ctx_get_op(ctx)) {
1528
1529 case DPLANE_OP_ROUTE_INSTALL:
1530 case DPLANE_OP_ROUTE_UPDATE:
1531 case DPLANE_OP_ROUTE_DELETE:
1532 res = kernel_route_update(ctx);
1533 break;
1534
1535 case DPLANE_OP_NH_INSTALL:
1536 case DPLANE_OP_NH_UPDATE:
1537 case DPLANE_OP_NH_DELETE:
1538 res = kernel_nexthop_update(ctx);
1539 break;
1540
1541 case DPLANE_OP_LSP_INSTALL:
1542 case DPLANE_OP_LSP_UPDATE:
1543 case DPLANE_OP_LSP_DELETE:
1544 res = kernel_lsp_update(ctx);
1545 break;
1546
1547 case DPLANE_OP_PW_INSTALL:
1548 case DPLANE_OP_PW_UNINSTALL:
1549 res = kernel_pw_update(ctx);
1550 break;
1551
1552 case DPLANE_OP_ADDR_INSTALL:
1553 case DPLANE_OP_ADDR_UNINSTALL:
1554 res = kernel_address_update_ctx(ctx);
1555 break;
1556
1557 case DPLANE_OP_MAC_INSTALL:
1558 case DPLANE_OP_MAC_DELETE:
1559 res = kernel_mac_update_ctx(ctx);
1560 break;
1561
1562 case DPLANE_OP_NEIGH_INSTALL:
1563 case DPLANE_OP_NEIGH_UPDATE:
1564 case DPLANE_OP_NEIGH_DELETE:
1565 case DPLANE_OP_VTEP_ADD:
1566 case DPLANE_OP_VTEP_DELETE:
1567 case DPLANE_OP_NEIGH_DISCOVER:
1568 res = kernel_neigh_update_ctx(ctx);
1569 break;
1570
1571 case DPLANE_OP_RULE_ADD:
1572 case DPLANE_OP_RULE_DELETE:
1573 case DPLANE_OP_RULE_UPDATE:
1574 res = kernel_pbr_rule_update(ctx);
1575 break;
1576
1577 case DPLANE_OP_INTF_INSTALL:
1578 case DPLANE_OP_INTF_UPDATE:
1579 case DPLANE_OP_INTF_DELETE:
1580 res = kernel_intf_update(ctx);
1581 break;
1582
1583 case DPLANE_OP_TC_QDISC_INSTALL:
1584 case DPLANE_OP_TC_QDISC_UNINSTALL:
1585 case DPLANE_OP_TC_CLASS_ADD:
1586 case DPLANE_OP_TC_CLASS_DELETE:
1587 case DPLANE_OP_TC_CLASS_UPDATE:
1588 case DPLANE_OP_TC_FILTER_ADD:
1589 case DPLANE_OP_TC_FILTER_DELETE:
1590 case DPLANE_OP_TC_FILTER_UPDATE:
1591 res = kernel_tc_update(ctx);
1592 break;
1593
1594 /* Ignore 'notifications' - no-op */
1595 case DPLANE_OP_SYS_ROUTE_ADD:
1596 case DPLANE_OP_SYS_ROUTE_DELETE:
1597 case DPLANE_OP_ROUTE_NOTIFY:
1598 case DPLANE_OP_LSP_NOTIFY:
1599 res = ZEBRA_DPLANE_REQUEST_SUCCESS;
1600 break;
1601
1602 case DPLANE_OP_INTF_NETCONFIG:
1603 res = kernel_intf_netconf_update(ctx);
1604 break;
1605
1606 case DPLANE_OP_NONE:
1607 case DPLANE_OP_BR_PORT_UPDATE:
1608 case DPLANE_OP_IPTABLE_ADD:
1609 case DPLANE_OP_IPTABLE_DELETE:
1610 case DPLANE_OP_IPSET_ADD:
1611 case DPLANE_OP_IPSET_DELETE:
1612 case DPLANE_OP_IPSET_ENTRY_ADD:
1613 case DPLANE_OP_IPSET_ENTRY_DELETE:
1614 case DPLANE_OP_NEIGH_IP_INSTALL:
1615 case DPLANE_OP_NEIGH_IP_DELETE:
1616 case DPLANE_OP_NEIGH_TABLE_UPDATE:
1617 case DPLANE_OP_GRE_SET:
1618 case DPLANE_OP_INTF_ADDR_ADD:
1619 case DPLANE_OP_INTF_ADDR_DEL:
1620 zlog_err("Unhandled dplane data for %s",
1621 dplane_op2str(dplane_ctx_get_op(ctx)));
1622 res = ZEBRA_DPLANE_REQUEST_FAILURE;
1623 }
1624
1625 skip_one:
1626 dplane_ctx_set_status(ctx, res);
1627
1628 dplane_ctx_enqueue_tail(&handled_list, ctx);
1629 }
1630
1631 dplane_ctx_q_init(ctx_list);
1632 dplane_ctx_list_append(ctx_list, &handled_list);
1633 }
1634
1635 #endif /* !HAVE_NETLINK */