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