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