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