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