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