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