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