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