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