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