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