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