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