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