]> git.proxmox.com Git - mirror_frr.git/blame - zebra/kernel_socket.c
zebra: add empty placeholders for tc via BSD socket
[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"
35#include "ioctl.h"
36#include "log.h"
718e3744 37#include "table.h"
38#include "rib.h"
edd7c245 39#include "privs.h"
78104b9b 40#include "vrf.h"
174482ef 41#include "lib_errors.h"
718e3744 42
a37bd5e0 43#include "zebra/rt.h"
718e3744 44#include "zebra/interface.h"
3801e764 45#include "zebra/zebra_router.h"
718e3744 46#include "zebra/debug.h"
ec1a4283 47#include "zebra/kernel_socket.h"
8f7d9fc0 48#include "zebra/rib.h"
98c15db6 49#include "zebra/zebra_errors.h"
01ce7cba 50#include "zebra/zebra_ptm.h"
718e3744 51
edd7c245 52extern struct zebra_privs_t zserv_privs;
53
4bfbea8c 54/*
cfa0ed09
GT
55 * Historically, the BSD routing socket has aligned data following a
56 * struct sockaddr to sizeof(long), which was 4 bytes on some
57 * platforms, and 8 bytes on others. NetBSD 6 changed the routing
58 * socket to align to sizeof(uint64_t), which is 8 bytes. OS X
59 * appears to align to sizeof(int), which is 4 bytes.
4bfbea8c 60 *
cfa0ed09
GT
61 * Alignment of zero-sized sockaddrs is nonsensical, but historically
62 * BSD defines RT_ROUNDUP(0) to be the alignment interval (rather than
63 * 0). We follow this practice without questioning it, but it is a
b72aae2e 64 * bug if frr calls ROUNDUP with 0.
4bfbea8c 65 */
cb800e35
RZ
66#ifdef __APPLE__
67#define ROUNDUP_TYPE int
68#else
69#define ROUNDUP_TYPE long
70#endif
cfa0ed09
GT
71
72/*
73 * Because of these varying conventions, the only sane approach is for
74 * the <net/route.h> header to define some flavor of ROUNDUP macro.
75 */
e7f0e649 76
cb800e35 77/* OS X (Xcode as of 2014-12) is known not to define RT_ROUNDUP */
cfa0ed09
GT
78#if defined(RT_ROUNDUP)
79#define ROUNDUP(a) RT_ROUNDUP(a)
80#endif /* defined(RT_ROUNDUP) */
81
82/*
83 * If ROUNDUP has not yet been defined in terms of platform-provided
84 * defines, attempt to cope with heuristics.
85 */
86#if !defined(ROUNDUP)
87
88/*
adf580f7
DL
89 * If you're porting to a platform that changed RT_ROUNDUP but doesn't
90 * have it in its headers, this will break rather obviously and you'll
91 * have to fix it here.
cfa0ed09 92 */
d62a17ae 93#define ROUNDUP(a) \
94 ((a) > 0 ? (1 + (((a)-1) | (sizeof(ROUNDUP_TYPE) - 1))) \
95 : sizeof(ROUNDUP_TYPE))
cfa0ed09
GT
96
97#endif /* defined(ROUNDUP) */
98
cb800e35
RZ
99
100#if defined(SA_SIZE)
101/* SAROUNDUP is the only thing we need, and SA_SIZE provides that */
102#define SAROUNDUP(a) SA_SIZE(a)
103#else /* !SA_SIZE */
4bfbea8c 104/*
105 * Given a pointer (sockaddr or void *), return the number of bytes
106 * taken up by the sockaddr and any padding needed for alignment.
107 */
6f0e3f6e 108#if defined(HAVE_STRUCT_SOCKADDR_SA_LEN)
4bfbea8c 109#define SAROUNDUP(X) ROUNDUP(((struct sockaddr *)(X))->sa_len)
56c1f7d8 110#else
4bfbea8c 111/*
112 * One would hope all fixed-size structure definitions are aligned,
113 * but round them up nonetheless.
114 */
d62a17ae 115#define SAROUNDUP(X) \
116 (((struct sockaddr *)(X))->sa_family == AF_INET \
117 ? ROUNDUP(sizeof(struct sockaddr_in)) \
118 : (((struct sockaddr *)(X))->sa_family == AF_INET6 \
119 ? ROUNDUP(sizeof(struct sockaddr_in6)) \
120 : (((struct sockaddr *)(X))->sa_family == AF_LINK \
121 ? ROUNDUP(sizeof(struct sockaddr_dl)) \
122 : sizeof(struct sockaddr))))
6f0e3f6e 123#endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
718e3744 124
e7f0e649
DL
125#endif /* !SA_SIZE */
126
718e3744 127/* Routing socket message types. */
d62a17ae 128const struct message rtm_type_str[] = {{RTM_ADD, "RTM_ADD"},
129 {RTM_DELETE, "RTM_DELETE"},
130 {RTM_CHANGE, "RTM_CHANGE"},
131 {RTM_GET, "RTM_GET"},
132 {RTM_LOSING, "RTM_LOSING"},
133 {RTM_REDIRECT, "RTM_REDIRECT"},
134 {RTM_MISS, "RTM_MISS"},
7f7651eb 135#ifdef RTM_LOCK
d62a17ae 136 {RTM_LOCK, "RTM_LOCK"},
7f7651eb 137#endif /* RTM_LOCK */
9458b819 138#ifdef OLDADD
d62a17ae 139 {RTM_OLDADD, "RTM_OLDADD"},
9458b819
GT
140#endif /* RTM_OLDADD */
141#ifdef RTM_OLDDEL
d62a17ae 142 {RTM_OLDDEL, "RTM_OLDDEL"},
9458b819 143#endif /* RTM_OLDDEL */
5f11807b 144#ifdef RTM_RESOLVE
d62a17ae 145 {RTM_RESOLVE, "RTM_RESOLVE"},
5f11807b 146#endif /* RTM_RESOLVE */
d62a17ae 147 {RTM_NEWADDR, "RTM_NEWADDR"},
148 {RTM_DELADDR, "RTM_DELADDR"},
149 {RTM_IFINFO, "RTM_IFINFO"},
718e3744 150#ifdef RTM_OIFINFO
d62a17ae 151 {RTM_OIFINFO, "RTM_OIFINFO"},
718e3744 152#endif /* RTM_OIFINFO */
153#ifdef RTM_NEWMADDR
d62a17ae 154 {RTM_NEWMADDR, "RTM_NEWMADDR"},
718e3744 155#endif /* RTM_NEWMADDR */
156#ifdef RTM_DELMADDR
d62a17ae 157 {RTM_DELMADDR, "RTM_DELMADDR"},
718e3744 158#endif /* RTM_DELMADDR */
159#ifdef RTM_IFANNOUNCE
d62a17ae 160 {RTM_IFANNOUNCE, "RTM_IFANNOUNCE"},
718e3744 161#endif /* RTM_IFANNOUNCE */
0081ab91
DS
162#ifdef RTM_IEEE80211
163 {RTM_IEEE80211, "RTM_IEEE80211"},
164#endif
d62a17ae 165 {0}};
166
167static const struct message rtm_flag_str[] = {{RTF_UP, "UP"},
168 {RTF_GATEWAY, "GATEWAY"},
169 {RTF_HOST, "HOST"},
170 {RTF_REJECT, "REJECT"},
171 {RTF_DYNAMIC, "DYNAMIC"},
172 {RTF_MODIFIED, "MODIFIED"},
173 {RTF_DONE, "DONE"},
718e3744 174#ifdef RTF_MASK
d62a17ae 175 {RTF_MASK, "MASK"},
718e3744 176#endif /* RTF_MASK */
e6f148e6 177#ifdef RTF_CLONING
d62a17ae 178 {RTF_CLONING, "CLONING"},
e6f148e6 179#endif /* RTF_CLONING */
23b1f400 180#ifdef RTF_XRESOLVE
d62a17ae 181 {RTF_XRESOLVE, "XRESOLVE"},
23b1f400 182#endif /* RTF_XRESOLVE */
c54632ec 183#ifdef RTF_LLINFO
d62a17ae 184 {RTF_LLINFO, "LLINFO"},
c54632ec 185#endif /* RTF_LLINFO */
d62a17ae 186 {RTF_STATIC, "STATIC"},
187 {RTF_BLACKHOLE, "BLACKHOLE"},
6fe70d1b 188#ifdef RTF_PRIVATE
d62a17ae 189 {RTF_PRIVATE, "PRIVATE"},
6fe70d1b 190#endif /* RTF_PRIVATE */
d62a17ae 191 {RTF_PROTO1, "PROTO1"},
192 {RTF_PROTO2, "PROTO2"},
718e3744 193#ifdef RTF_PRCLONING
d62a17ae 194 {RTF_PRCLONING, "PRCLONING"},
718e3744 195#endif /* RTF_PRCLONING */
196#ifdef RTF_WASCLONED
d62a17ae 197 {RTF_WASCLONED, "WASCLONED"},
718e3744 198#endif /* RTF_WASCLONED */
199#ifdef RTF_PROTO3
d62a17ae 200 {RTF_PROTO3, "PROTO3"},
718e3744 201#endif /* RTF_PROTO3 */
202#ifdef RTF_PINNED
d62a17ae 203 {RTF_PINNED, "PINNED"},
718e3744 204#endif /* RTF_PINNED */
205#ifdef RTF_LOCAL
d62a17ae 206 {RTF_LOCAL, "LOCAL"},
718e3744 207#endif /* RTF_LOCAL */
208#ifdef RTF_BROADCAST
d62a17ae 209 {RTF_BROADCAST, "BROADCAST"},
718e3744 210#endif /* RTF_BROADCAST */
211#ifdef RTF_MULTICAST
d62a17ae 212 {RTF_MULTICAST, "MULTICAST"},
718e3744 213#endif /* RTF_MULTICAST */
6fe70d1b 214#ifdef RTF_MULTIRT
d62a17ae 215 {RTF_MULTIRT, "MULTIRT"},
6fe70d1b 216#endif /* RTF_MULTIRT */
217#ifdef RTF_SETSRC
d62a17ae 218 {RTF_SETSRC, "SETSRC"},
6fe70d1b 219#endif /* RTF_SETSRC */
d62a17ae 220 {0}};
718e3744 221
222/* Kernel routing update socket. */
223int routing_sock = -1;
224
80776aec
MS
225/* Kernel dataplane routing update socket, used in the dataplane pthread
226 * context.
227 */
228int dplane_routing_sock = -1;
229
718e3744 230/* Yes I'm checking ugly routing socket behavior. */
231/* #define DEBUG */
232
19fb538d 233size_t _rta_get(caddr_t sap, void *destp, size_t destlen, bool checkaf);
86c57af5 234size_t rta_get(caddr_t sap, void *dest, size_t destlen);
19fb538d 235size_t rta_getattr(caddr_t sap, void *destp, size_t destlen);
86c57af5 236size_t rta_getsdlname(caddr_t sap, void *dest, short *destlen);
5b62211d 237const char *rtatostr(unsigned int flags, char *buf, size_t buflen);
86c57af5 238
718e3744 239/* Supported address family check. */
d62a17ae 240static inline int af_check(int family)
718e3744 241{
d62a17ae 242 if (family == AF_INET)
243 return 1;
244 if (family == AF_INET6)
245 return 1;
246 return 0;
718e3744 247}
6b0655a2 248
19fb538d 249size_t _rta_get(caddr_t sap, void *destp, size_t destlen, bool checkaf)
86c57af5
RZ
250{
251 struct sockaddr *sa = (struct sockaddr *)sap;
75e710df 252 struct sockaddr_dl *sdl;
86c57af5
RZ
253 uint8_t *dest = destp;
254 size_t tlen, copylen;
255
256#ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
257 copylen = sa->sa_len;
258 tlen = (copylen == 0) ? sizeof(ROUNDUP_TYPE) : ROUNDUP(copylen);
259#else /* !HAVE_STRUCT_SOCKADDR_SA_LEN */
260 copylen = tlen = SAROUNDUP(sap);
261#endif /* !HAVE_STRUCT_SOCKADDR_SA_LEN */
262
19fb538d
RZ
263 if (copylen > 0 && dest != NULL) {
264 if (checkaf && af_check(sa->sa_family) == 0)
265 return tlen;
75e710df
RZ
266 /*
267 * Handle sockaddr_dl corner case:
268 * RTA_NETMASK might be AF_LINK, but it doesn't anything
269 * relevant (e.g. zeroed out fields). Check for this
270 * case and avoid warning log message.
271 */
272 if (sa->sa_family == AF_LINK) {
273 sdl = (struct sockaddr_dl *)sa;
274 if (sdl->sdl_index == 0 || sdl->sdl_nlen == 0)
e0f741f4 275 copylen = destlen;
75e710df 276 }
19fb538d 277
86c57af5 278 if (copylen > destlen) {
fa0069c6
DS
279 zlog_warn(
280 "%s: destination buffer too small (%zu vs %zu)",
281 __func__, copylen, destlen);
86c57af5
RZ
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;
86c57af5
RZ
303 uint8_t *dest = destp;
304 size_t tlen, copylen;
305
306 copylen = sdl->sdl_nlen;
307#ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
8054cf97
QY
308 struct sockaddr *sa = (struct sockaddr *)sap;
309
86c57af5
RZ
310 tlen = (sa->sa_len == 0) ? sizeof(ROUNDUP_TYPE) : ROUNDUP(sa->sa_len);
311#else /* !HAVE_STRUCT_SOCKADDR_SA_LEN */
312 tlen = SAROUNDUP(sap);
313#endif /* !HAVE_STRUCT_SOCKADDR_SA_LEN */
314
315 if (copylen > 0 && dest != NULL && sdl->sdl_family == AF_LINK) {
316 if (copylen > IFNAMSIZ) {
fa0069c6
DS
317 zlog_warn(
318 "%s: destination buffer too small (%zu vs %d)",
319 __func__, copylen, IFNAMSIZ);
86c57af5
RZ
320 memcpy(dest, sdl->sdl_data, IFNAMSIZ);
321 dest[IFNAMSIZ] = 0;
322 *destlen = IFNAMSIZ;
323 } else {
324 memcpy(dest, sdl->sdl_data, copylen);
325 dest[copylen] = 0;
326 *destlen = copylen;
327 }
328 } else
329 *destlen = 0;
330
331 return tlen;
332}
333
5b62211d
RZ
334const char *rtatostr(unsigned int flags, char *buf, size_t buflen)
335{
336 const char *flagstr, *bufstart;
337 int bit, wlen;
338 char ustr[32];
339
340 /* Hold the pointer to the buffer beginning. */
341 bufstart = buf;
342
343 for (bit = 1; bit; bit <<= 1) {
344 if ((flags & bit) == 0)
345 continue;
346
347 switch (bit) {
348 case RTA_DST:
349 flagstr = "DST";
350 break;
351 case RTA_GATEWAY:
352 flagstr = "GATEWAY";
353 break;
354 case RTA_NETMASK:
355 flagstr = "NETMASK";
356 break;
357#ifdef RTA_GENMASK
358 case RTA_GENMASK:
359 flagstr = "GENMASK";
360 break;
361#endif /* RTA_GENMASK */
362 case RTA_IFP:
363 flagstr = "IFP";
364 break;
365 case RTA_IFA:
366 flagstr = "IFA";
367 break;
368#ifdef RTA_AUTHOR
369 case RTA_AUTHOR:
370 flagstr = "AUTHOR";
371 break;
372#endif /* RTA_AUTHOR */
373 case RTA_BRD:
374 flagstr = "BRD";
375 break;
376#ifdef RTA_SRC
377 case RTA_SRC:
378 flagstr = "SRC";
379 break;
380#endif /* RTA_SRC */
381#ifdef RTA_SRCMASK
382 case RTA_SRCMASK:
383 flagstr = "SRCMASK";
384 break;
385#endif /* RTA_SRCMASK */
386#ifdef RTA_LABEL
387 case RTA_LABEL:
388 flagstr = "LABEL";
389 break;
390#endif /* RTA_LABEL */
391
392 default:
393 snprintf(ustr, sizeof(ustr), "0x%x", bit);
394 flagstr = ustr;
395 break;
396 }
397
398 wlen = snprintf(buf, buflen, "%s,", flagstr);
399 buf += wlen;
400 buflen -= wlen;
401 }
402
403 /* Check for empty buffer. */
404 if (bufstart != buf)
405 buf--;
406
407 /* Remove the last comma. */
408 *buf = 0;
409
410 return bufstart;
411}
412
718e3744 413/* Dump routing table flag for debug purpose. */
d62a17ae 414static void rtm_flag_dump(int flag)
718e3744 415{
d62a17ae 416 const struct message *mes;
417 static char buf[BUFSIZ];
418
419 buf[0] = '\0';
420 for (mes = rtm_flag_str; mes->key != 0; mes++) {
421 if (mes->key & flag) {
422 strlcat(buf, mes->str, BUFSIZ);
423 strlcat(buf, " ", BUFSIZ);
424 }
718e3744 425 }
d62a17ae 426 zlog_debug("Kernel: %s", buf);
718e3744 427}
428
429#ifdef RTM_IFANNOUNCE
430/* Interface adding function */
d62a17ae 431static int ifan_read(struct if_announcemsghdr *ifan)
718e3744 432{
d62a17ae 433 struct interface *ifp;
434
435 ifp = if_lookup_by_index(ifan->ifan_index, VRF_DEFAULT);
436
437 if (ifp)
438 assert((ifp->ifindex == ifan->ifan_index)
439 || (ifp->ifindex == IFINDEX_INTERNAL));
440
9d303b37
DL
441 if ((ifp == NULL) || ((ifp->ifindex == IFINDEX_INTERNAL)
442 && (ifan->ifan_what == IFAN_ARRIVAL))) {
d62a17ae 443 if (IS_ZEBRA_DEBUG_KERNEL)
444 zlog_debug(
445 "%s: creating interface for ifindex %d, name %s",
446 __func__, ifan->ifan_index, ifan->ifan_name);
447
448 /* Create Interface */
f60a1188
IR
449 ifp = if_get_by_name(ifan->ifan_name, VRF_DEFAULT,
450 VRF_DEFAULT_NAME);
ff880b78 451 if_set_index(ifp, ifan->ifan_index);
d62a17ae 452
453 if_get_metric(ifp);
454 if_add_update(ifp);
455 } else if (ifp != NULL && ifan->ifan_what == IFAN_DEPARTURE)
d0438da6 456 if_delete_update(&ifp);
d62a17ae 457
d0438da6
DS
458 if (ifp) {
459 if_get_flags(ifp);
460 if_get_mtu(ifp);
461 if_get_metric(ifp);
462 }
d62a17ae 463 if (IS_ZEBRA_DEBUG_KERNEL)
464 zlog_debug("%s: interface %s index %d", __func__,
465 ifan->ifan_name, ifan->ifan_index);
466
467 return 0;
718e3744 468}
469#endif /* RTM_IFANNOUNCE */
470
9234b382 471#ifdef HAVE_BSD_IFI_LINK_STATE
c543a173 472/* BSD link detect translation */
d62a17ae 473static void bsd_linkdetect_translate(struct if_msghdr *ifm)
c543a173 474{
d62a17ae 475 if ((ifm->ifm_data.ifi_link_state >= LINK_STATE_UP)
476 || (ifm->ifm_data.ifi_link_state == LINK_STATE_UNKNOWN))
477 SET_FLAG(ifm->ifm_flags, IFF_RUNNING);
478 else
479 UNSET_FLAG(ifm->ifm_flags, IFF_RUNNING);
c543a173 480}
9234b382 481#endif /* HAVE_BSD_IFI_LINK_STATE */
c543a173 482
d62a17ae 483static enum zebra_link_type sdl_to_zebra_link_type(unsigned int sdlt)
8ccc7e80 484{
d62a17ae 485 switch (sdlt) {
486 case IFT_ETHER:
487 return ZEBRA_LLT_ETHER;
488 case IFT_X25:
489 return ZEBRA_LLT_X25;
490 case IFT_FDDI:
491 return ZEBRA_LLT_FDDI;
492 case IFT_PPP:
493 return ZEBRA_LLT_PPP;
494 case IFT_LOOP:
495 return ZEBRA_LLT_LOOPBACK;
496 case IFT_SLIP:
497 return ZEBRA_LLT_SLIP;
498 case IFT_ARCNET:
499 return ZEBRA_LLT_ARCNET;
500 case IFT_ATM:
501 return ZEBRA_LLT_ATM;
502 case IFT_LOCALTALK:
503 return ZEBRA_LLT_LOCALTLK;
504 case IFT_HIPPI:
505 return ZEBRA_LLT_HIPPI;
8ccc7e80 506#ifdef IFT_IEEE1394
d62a17ae 507 case IFT_IEEE1394:
508 return ZEBRA_LLT_IEEE1394;
8ccc7e80
TT
509#endif
510
d62a17ae 511 default:
512 return ZEBRA_LLT_UNKNOWN;
513 }
8ccc7e80
TT
514}
515
da26e3b6 516/*
517 * Handle struct if_msghdr obtained from reading routing socket or
518 * sysctl (from interface_list). There may or may not be sockaddrs
519 * present after the header.
520 */
d62a17ae 521int ifm_read(struct if_msghdr *ifm)
718e3744 522{
d62a17ae 523 struct interface *ifp = NULL;
ee74220b 524 struct sockaddr_dl *sdl = NULL;
d62a17ae 525 char ifname[IFNAMSIZ];
526 short ifnlen = 0;
86c57af5 527 int maskbit;
d62a17ae 528 caddr_t cp;
5b62211d 529 char fbuf[64];
d62a17ae 530
531 /* terminate ifname at head (for strnlen) and tail (for safety) */
532 ifname[IFNAMSIZ - 1] = '\0';
533
534 /* paranoia: sanity check structure */
535 if (ifm->ifm_msglen < sizeof(struct if_msghdr)) {
e914ccbe 536 flog_err(EC_ZEBRA_NETLINK_LENGTH_ERROR,
bcfce7ad 537 "ifm_read: ifm->ifm_msglen %d too short",
1c50c1c0 538 ifm->ifm_msglen);
d62a17ae 539 return -1;
540 }
541
542 /*
543 * Check for a sockaddr_dl following the message. First, point to
544 * where a socakddr might be if one follows the message.
545 */
546 cp = (void *)(ifm + 1);
718e3744 547
86c57af5
RZ
548 /* Look up for RTA_IFP and skip others. */
549 for (maskbit = 1; maskbit; maskbit <<= 1) {
550 if ((maskbit & ifm->ifm_addrs) == 0)
551 continue;
552 if (maskbit != RTA_IFP) {
553 cp += rta_get(cp, NULL, 0);
554 continue;
555 }
556
557 /* Save the pointer to the structure. */
558 sdl = (struct sockaddr_dl *)cp;
559 cp += rta_getsdlname(cp, ifname, &ifnlen);
560 }
d62a17ae 561
562 if (IS_ZEBRA_DEBUG_KERNEL)
5b62211d
RZ
563 zlog_debug("%s: sdl ifname %s addrs {%s}", __func__,
564 (ifnlen ? ifname : "(nil)"),
565 rtatostr(ifm->ifm_addrs, fbuf, sizeof(fbuf)));
d62a17ae 566
567 /*
568 * Look up on ifindex first, because ifindices are the primary handle
569 * for
570 * interfaces across the user/kernel boundary, for most systems. (Some
571 * messages, such as up/down status changes on NetBSD, do not include a
572 * sockaddr_dl).
573 */
574 if ((ifp = if_lookup_by_index(ifm->ifm_index, VRF_DEFAULT)) != NULL) {
575 /* we have an ifp, verify that the name matches as some systems,
576 * eg Solaris, have a 1:many association of ifindex:ifname
577 * if they dont match, we dont have the correct ifp and should
578 * set it back to NULL to let next check do lookup by name
579 */
580 if (ifnlen && (strncmp(ifp->name, ifname, IFNAMSIZ) != 0)) {
581 if (IS_ZEBRA_DEBUG_KERNEL)
582 zlog_debug(
0437e105 583 "%s: ifp name %s doesn't match sdl name %s",
d62a17ae 584 __func__, ifp->name, ifname);
585 ifp = NULL;
586 }
da26e3b6 587 }
5c78b3d0 588
d62a17ae 589 /*
590 * If we dont have an ifp, try looking up by name. Particularly as some
591 * systems (Solaris) have a 1:many mapping of ifindex:ifname - the
592 * ifname
593 * is therefore our unique handle to that interface.
594 *
595 * Interfaces specified in the configuration file for which the ifindex
596 * has not been determined will have ifindex == IFINDEX_INTERNAL, and
597 * such
598 * interfaces are found by this search, and then their ifindex values
599 * can
600 * be filled in.
601 */
602 if ((ifp == NULL) && ifnlen)
a36898e7 603 ifp = if_lookup_by_name(ifname, VRF_DEFAULT);
d62a17ae 604
605 /*
606 * If ifp still does not exist or has an invalid index
607 * (IFINDEX_INTERNAL),
608 * create or fill in an interface.
609 */
610 if ((ifp == NULL) || (ifp->ifindex == IFINDEX_INTERNAL)) {
611 /*
612 * To create or fill in an interface, a sockaddr_dl (via
613 * RTA_IFP) is required.
614 */
615 if (!ifnlen) {
9165c5f5 616 zlog_debug("Interface index %d (new) missing ifname",
9df414fe 617 ifm->ifm_index);
d62a17ae 618 return -1;
619 }
620
5c78b3d0 621#ifndef RTM_IFANNOUNCE
d62a17ae 622 /* Down->Down interface should be ignored here.
623 * See further comment below.
624 */
625 if (!CHECK_FLAG(ifm->ifm_flags, IFF_UP))
626 return 0;
5c78b3d0 627#endif /* !RTM_IFANNOUNCE */
d62a17ae 628
629 if (ifp == NULL) {
630 /* Interface that zebra was not previously aware of, so
631 * create. */
f60a1188
IR
632 ifp = if_get_by_name(ifname, VRF_DEFAULT,
633 VRF_DEFAULT_NAME);
d62a17ae 634 if (IS_ZEBRA_DEBUG_KERNEL)
635 zlog_debug("%s: creating ifp for ifindex %d",
636 __func__, ifm->ifm_index);
637 }
638
639 if (IS_ZEBRA_DEBUG_KERNEL)
640 zlog_debug(
641 "%s: updated/created ifp, ifname %s, ifindex %d",
642 __func__, ifp->name, ifp->ifindex);
643 /*
644 * Fill in newly created interface structure, or larval
645 * structure with ifindex IFINDEX_INTERNAL.
646 */
ff880b78 647 if_set_index(ifp, ifm->ifm_index);
d62a17ae 648
9234b382 649#ifdef HAVE_BSD_IFI_LINK_STATE /* translate BSD kernel msg for link-state */
d62a17ae 650 bsd_linkdetect_translate(ifm);
9234b382 651#endif /* HAVE_BSD_IFI_LINK_STATE */
c543a173 652
d62a17ae 653 if_flags_update(ifp, ifm->ifm_flags);
718e3744 654#if defined(__bsdi__)
d62a17ae 655 if_kvm_get_mtu(ifp);
718e3744 656#else
d62a17ae 657 if_get_mtu(ifp);
718e3744 658#endif /* __bsdi__ */
d62a17ae 659 if_get_metric(ifp);
660
661 /*
662 * XXX sockaddr_dl contents can be larger than the structure
663 * definition. There are 2 big families here:
664 * - BSD has sdl_len + sdl_data[16] + overruns sdl_data
665 * we MUST use sdl_len here or we'll truncate data.
666 * - Solaris has no sdl_len, but sdl_data[244]
667 * presumably, it's not going to run past that, so sizeof()
668 * is fine here.
86c57af5 669 * a nonzero ifnlen from rta_getsdlname() means sdl is valid
d62a17ae 670 */
671 ifp->ll_type = ZEBRA_LLT_UNKNOWN;
672 ifp->hw_addr_len = 0;
673 if (ifnlen) {
ca3ccd87 674#ifdef HAVE_STRUCT_SOCKADDR_DL_SDL_LEN
d62a17ae 675 memcpy(&((struct zebra_if *)ifp->info)->sdl, sdl,
676 sdl->sdl_len);
ca3ccd87 677#else
d62a17ae 678 memcpy(&((struct zebra_if *)ifp->info)->sdl, sdl,
679 sizeof(struct sockaddr_dl));
ca3ccd87 680#endif /* HAVE_STRUCT_SOCKADDR_DL_SDL_LEN */
8ccc7e80 681
d62a17ae 682 ifp->ll_type = sdl_to_zebra_link_type(sdl->sdl_type);
683 if (sdl->sdl_alen <= sizeof(ifp->hw_addr)) {
684 memcpy(ifp->hw_addr, LLADDR(sdl),
685 sdl->sdl_alen);
686 ifp->hw_addr_len = sdl->sdl_alen;
687 }
688 }
689
690 if_add_update(ifp);
691 } else
692 /*
693 * Interface structure exists. Adjust stored flags from
694 * notification. If interface has up->down or down->up
695 * transition, call state change routines (to adjust routes,
696 * notify routing daemons, etc.). (Other flag changes are stored
697 * but apparently do not trigger action.)
698 */
699 {
700 if (ifp->ifindex != ifm->ifm_index) {
9df414fe 701 zlog_debug(
3efd0893 702 "%s: index mismatch, ifname %s, ifp index %d, ifm index %d",
d62a17ae 703 __func__, ifp->name, ifp->ifindex,
704 ifm->ifm_index);
705 return -1;
706 }
707
9234b382 708#ifdef HAVE_BSD_IFI_LINK_STATE /* translate BSD kernel msg for link-state */
d62a17ae 709 bsd_linkdetect_translate(ifm);
9234b382 710#endif /* HAVE_BSD_IFI_LINK_STATE */
c543a173 711
d62a17ae 712 /* update flags and handle operative->inoperative transition, if
713 * any */
714 if_flags_update(ifp, ifm->ifm_flags);
715
6eb8827d 716#ifndef RTM_IFANNOUNCE
d62a17ae 717 if (!if_is_up(ifp)) {
718 /* No RTM_IFANNOUNCE on this platform, so we can never
719 * distinguish between ~IFF_UP and delete. We must
720 * presume
721 * it has been deleted.
722 * Eg, Solaris will not notify us of unplumb.
723 *
724 * XXX: Fixme - this should be runtime detected
725 * So that a binary compiled on a system with IFANNOUNCE
726 * will still behave correctly if run on a platform
727 * without
728 */
d0438da6 729 if_delete_update(&ifp);
d62a17ae 730 }
6eb8827d 731#endif /* RTM_IFANNOUNCE */
d0438da6 732 if (ifp && if_is_up(ifp)) {
1ba27564 733#if defined(__bsdi__)
d62a17ae 734 if_kvm_get_mtu(ifp);
1ba27564 735#else
d62a17ae 736 if_get_mtu(ifp);
1ba27564 737#endif /* __bsdi__ */
d62a17ae 738 if_get_metric(ifp);
739 }
740 }
5c78b3d0 741
d0438da6 742 if (ifp) {
718e3744 743#ifdef HAVE_NET_RT_IFLIST
d0438da6 744 ifp->stats = ifm->ifm_data;
718e3744 745#endif /* HAVE_NET_RT_IFLIST */
d0438da6 746 ifp->speed = ifm->ifm_data.ifi_baudrate / 1000000;
718e3744 747
d0438da6
DS
748 if (IS_ZEBRA_DEBUG_KERNEL)
749 zlog_debug("%s: interface %s index %d", __func__,
750 ifp->name, ifp->ifindex);
751 }
718e3744 752
d62a17ae 753 return 0;
718e3744 754}
6b0655a2 755
718e3744 756/* Address read from struct ifa_msghdr. */
d62a17ae 757static void ifam_read_mesg(struct ifa_msghdr *ifm, union sockunion *addr,
758 union sockunion *mask, union sockunion *brd,
759 char *ifname, short *ifnlen)
718e3744 760{
d62a17ae 761 caddr_t pnt, end;
762 union sockunion dst;
763 union sockunion gateway;
86c57af5 764 int maskbit;
5b62211d 765 char fbuf[64];
d62a17ae 766
767 pnt = (caddr_t)(ifm + 1);
768 end = ((caddr_t)ifm) + ifm->ifam_msglen;
769
770 /* Be sure structure is cleared */
771 memset(mask, 0, sizeof(union sockunion));
772 memset(addr, 0, sizeof(union sockunion));
773 memset(brd, 0, sizeof(union sockunion));
774 memset(&dst, 0, sizeof(union sockunion));
775 memset(&gateway, 0, sizeof(union sockunion));
776
777 /* We fetch each socket variable into sockunion. */
86c57af5
RZ
778 for (maskbit = 1; maskbit; maskbit <<= 1) {
779 if ((maskbit & ifm->ifam_addrs) == 0)
780 continue;
781
782 switch (maskbit) {
783 case RTA_DST:
784 pnt += rta_get(pnt, &dst, sizeof(dst));
785 break;
786 case RTA_GATEWAY:
787 pnt += rta_get(pnt, &gateway, sizeof(gateway));
788 break;
789 case RTA_NETMASK:
19fb538d 790 pnt += rta_getattr(pnt, mask, sizeof(*mask));
86c57af5
RZ
791 break;
792 case RTA_IFP:
793 pnt += rta_getsdlname(pnt, ifname, ifnlen);
794 break;
795 case RTA_IFA:
796 pnt += rta_get(pnt, addr, sizeof(*addr));
797 break;
798 case RTA_BRD:
799 pnt += rta_get(pnt, brd, sizeof(*brd));
800 break;
801
802 default:
803 pnt += rta_get(pnt, NULL, 0);
804 break;
805 }
806
807 if (pnt > end) {
808 zlog_warn("%s: overflow detected (pnt:%p end:%p)",
809 __func__, pnt, end);
810 break;
811 }
812 }
d62a17ae 813
814 if (IS_ZEBRA_DEBUG_KERNEL) {
7053cae4 815 switch (sockunion_family(addr)) {
d62a17ae 816 case AF_INET:
817 case AF_INET6: {
7053cae4
RZ
818 int masklen =
819 (sockunion_family(addr) == AF_INET)
820 ? ip_masklen(mask->sin.sin_addr)
821 : ip6_masklen(mask->sin6.sin6_addr);
d62a17ae 822 zlog_debug(
bcfce7ad 823 "%s: ifindex %d, ifname %s, ifam_addrs {%s}, ifam_flags 0x%x, addr %pSU/%d broad %pSU dst %pSU gateway %pSU",
d62a17ae 824 __func__, ifm->ifam_index,
5b62211d
RZ
825 (ifnlen ? ifname : "(nil)"),
826 rtatostr(ifm->ifam_addrs, fbuf, sizeof(fbuf)),
bcfce7ad
MS
827 ifm->ifam_flags, addr, masklen, brd, &dst,
828 &gateway);
d62a17ae 829 } break;
830 default:
5b62211d 831 zlog_debug("%s: ifindex %d, ifname %s, ifam_addrs {%s}",
d62a17ae 832 __func__, ifm->ifam_index,
833 (ifnlen ? ifname : "(nil)"),
5b62211d
RZ
834 rtatostr(ifm->ifam_addrs, fbuf,
835 sizeof(fbuf)));
d62a17ae 836 break;
837 }
838 }
839
840 /* Assert read up end point matches to end point */
86c57af5 841 pnt = (caddr_t)ROUNDUP((size_t)pnt);
2d100aa5 842 if (pnt != (caddr_t)ROUNDUP((size_t)end))
9df414fe 843 zlog_debug("ifam_read() doesn't read all socket data");
718e3744 844}
845
846/* Interface's address information get. */
d62a17ae 847int ifam_read(struct ifa_msghdr *ifam)
718e3744 848{
d62a17ae 849 struct interface *ifp = NULL;
850 union sockunion addr, mask, brd;
8c36640b 851 bool dest_same = false;
d62a17ae 852 char ifname[INTERFACE_NAMSIZ];
853 short ifnlen = 0;
ecff5258
DS
854 bool isalias = false;
855 uint32_t flags = 0;
d62a17ae 856
857 ifname[0] = ifname[INTERFACE_NAMSIZ - 1] = '\0';
858
859 /* Allocate and read address information. */
860 ifam_read_mesg(ifam, &addr, &mask, &brd, ifname, &ifnlen);
861
862 if ((ifp = if_lookup_by_index(ifam->ifam_index, VRF_DEFAULT)) == NULL) {
e914ccbe 863 flog_warn(EC_ZEBRA_UNKNOWN_INTERFACE,
9df414fe 864 "%s: no interface for ifname %s, index %d", __func__,
d62a17ae 865 ifname, ifam->ifam_index);
866 return -1;
867 }
868
869 if (ifnlen && strncmp(ifp->name, ifname, INTERFACE_NAMSIZ))
ecff5258
DS
870 isalias = true;
871
872 /*
873 * Mark the alias prefixes as secondary
874 */
875 if (isalias)
876 SET_FLAG(flags, ZEBRA_IFA_SECONDARY);
d62a17ae 877
878 /* N.B. The info in ifa_msghdr does not tell us whether the RTA_BRD
879 field contains a broadcast address or a peer address, so we are
880 forced to
881 rely upon the interface type. */
882 if (if_is_pointopoint(ifp))
883 SET_FLAG(flags, ZEBRA_IFA_PEER);
8c36640b
DS
884 else {
885 if (memcmp(&addr, &brd, sizeof(addr)) == 0)
886 dest_same = true;
887 }
7ab62c53 888
6502208c
PJ
889#if 0
890 /* it might seem cute to grab the interface metric here, however
891 * we're processing an address update message, and so some systems
892 * (e.g. FBSD) dont bother to fill in ifam_metric. Disabled, but left
893 * in deliberately, as comment.
894 */
d34b8991 895 ifp->metric = ifam->ifam_metric;
6502208c
PJ
896#endif
897
d62a17ae 898 /* Add connected address. */
899 switch (sockunion_family(&addr)) {
900 case AF_INET:
901 if (ifam->ifam_type == RTM_NEWADDR)
902 connected_add_ipv4(ifp, flags, &addr.sin.sin_addr,
903 ip_masklen(mask.sin.sin_addr),
8c36640b 904 dest_same ? NULL : &brd.sin.sin_addr,
cde1af84
AK
905 (isalias ? ifname : NULL),
906 METRIC_MAX);
d62a17ae 907 else
908 connected_delete_ipv4(ifp, flags, &addr.sin.sin_addr,
909 ip_masklen(mask.sin.sin_addr),
8c36640b
DS
910 dest_same ? NULL
911 : &brd.sin.sin_addr);
d62a17ae 912 break;
913 case AF_INET6:
914 /* Unset interface index from link-local address when IPv6 stack
915 is KAME. */
916 if (IN6_IS_ADDR_LINKLOCAL(&addr.sin6.sin6_addr)) {
917 SET_IN6_LINKLOCAL_IFINDEX(addr.sin6.sin6_addr, 0);
918 }
919
920 if (ifam->ifam_type == RTM_NEWADDR)
921 connected_add_ipv6(ifp, flags, &addr.sin6.sin6_addr,
60c0687a 922 NULL,
d62a17ae 923 ip6_masklen(mask.sin6.sin6_addr),
cde1af84
AK
924 (isalias ? ifname : NULL),
925 METRIC_MAX);
d62a17ae 926 else
60c0687a 927 connected_delete_ipv6(ifp, &addr.sin6.sin6_addr, NULL,
608105a7 928 ip6_masklen(mask.sin6.sin6_addr));
d62a17ae 929 break;
930 default:
931 /* Unsupported family silently ignore... */
932 break;
933 }
934
935 /* Check interface flag for implicit up of the interface. */
936 if_refresh(ifp);
5c78b3d0 937
d62a17ae 938 return 0;
718e3744 939}
6b0655a2 940
718e3744 941/* Interface function for reading kernel routing table information. */
d62a17ae 942static int rtm_read_mesg(struct rt_msghdr *rtm, union sockunion *dest,
943 union sockunion *mask, union sockunion *gate,
944 char *ifname, short *ifnlen)
718e3744 945{
d62a17ae 946 caddr_t pnt, end;
86c57af5 947 int maskbit;
d62a17ae 948
949 /* Pnt points out socket data start point. */
950 pnt = (caddr_t)(rtm + 1);
951 end = ((caddr_t)rtm) + rtm->rtm_msglen;
952
953 /* rt_msghdr version check. */
954 if (rtm->rtm_version != RTM_VERSION)
e914ccbe 955 flog_warn(EC_ZEBRA_RTM_VERSION_MISMATCH,
bcfce7ad 956 "Routing message version different %d should be %d.This may cause problem",
9df414fe 957 rtm->rtm_version, RTM_VERSION);
d62a17ae 958
959 /* Be sure structure is cleared */
960 memset(dest, 0, sizeof(union sockunion));
961 memset(gate, 0, sizeof(union sockunion));
962 memset(mask, 0, sizeof(union sockunion));
963
964 /* We fetch each socket variable into sockunion. */
86c57af5
RZ
965 /* We fetch each socket variable into sockunion. */
966 for (maskbit = 1; maskbit; maskbit <<= 1) {
967 if ((maskbit & rtm->rtm_addrs) == 0)
968 continue;
969
970 switch (maskbit) {
971 case RTA_DST:
972 pnt += rta_get(pnt, dest, sizeof(*dest));
973 break;
974 case RTA_GATEWAY:
975 pnt += rta_get(pnt, gate, sizeof(*gate));
976 break;
977 case RTA_NETMASK:
1583f8f5 978 pnt += rta_getattr(pnt, mask, sizeof(*mask));
86c57af5
RZ
979 break;
980 case RTA_IFP:
981 pnt += rta_getsdlname(pnt, ifname, ifnlen);
982 break;
983
984 default:
985 pnt += rta_get(pnt, NULL, 0);
986 break;
987 }
988
989 if (pnt > end) {
990 zlog_warn("%s: overflow detected (pnt:%p end:%p)",
991 __func__, pnt, end);
992 break;
993 }
994 }
d62a17ae 995
996 /* If there is netmask information set it's family same as
997 destination family*/
998 if (rtm->rtm_addrs & RTA_NETMASK)
999 mask->sa.sa_family = dest->sa.sa_family;
1000
1001 /* Assert read up to the end of pointer. */
1002 if (pnt != end)
9df414fe 1003 zlog_debug("rtm_read() doesn't read all socket data.");
d62a17ae 1004
1005 return rtm->rtm_flags;
718e3744 1006}
1007
d62a17ae 1008void rtm_read(struct rt_msghdr *rtm)
718e3744 1009{
d62a17ae 1010 int flags;
acde7f6b 1011 uint32_t zebra_flags;
d62a17ae 1012 union sockunion dest, mask, gate;
1013 char ifname[INTERFACE_NAMSIZ + 1];
1014 short ifnlen = 0;
fd36be7e 1015 struct nexthop nh;
026745a2
DS
1016 struct prefix p;
1017 ifindex_t ifindex = 0;
1018 afi_t afi;
5b62211d 1019 char fbuf[64];
73d3197c
DS
1020 int32_t proto = ZEBRA_ROUTE_KERNEL;
1021 uint8_t distance = 0;
d62a17ae 1022
1023 zebra_flags = 0;
1024
1025 /* Read destination and netmask and gateway from rtm message
1026 structure. */
1027 flags = rtm_read_mesg(rtm, &dest, &mask, &gate, ifname, &ifnlen);
1028 if (!(flags & RTF_DONE))
1029 return;
1030 if (IS_ZEBRA_DEBUG_KERNEL)
5b62211d 1031 zlog_debug("%s: got rtm of type %d (%s) addrs {%s}", __func__,
d62a17ae 1032 rtm->rtm_type,
5b62211d
RZ
1033 lookup_msg(rtm_type_str, rtm->rtm_type, NULL),
1034 rtatostr(rtm->rtm_addrs, fbuf, sizeof(fbuf)));
d62a17ae 1035
1036#ifdef RTF_CLONED /*bsdi, netbsd 1.6*/
1037 if (flags & RTF_CLONED)
1038 return;
718e3744 1039#endif
d62a17ae 1040#ifdef RTF_WASCLONED /*freebsd*/
1041 if (flags & RTF_WASCLONED)
1042 return;
718e3744 1043#endif
1044
d62a17ae 1045 if ((rtm->rtm_type == RTM_ADD || rtm->rtm_type == RTM_CHANGE)
1046 && !(flags & RTF_UP))
1047 return;
1048
1049 /* This is connected route. */
1050 if (!(flags & RTF_GATEWAY))
1051 return;
1052
73d3197c 1053 if (flags & RTF_PROTO1) {
d62a17ae 1054 SET_FLAG(zebra_flags, ZEBRA_FLAG_SELFROUTE);
73d3197c
DS
1055 proto = ZEBRA_ROUTE_STATIC;
1056 distance = 255;
1057 }
d62a17ae 1058
a8309422 1059 memset(&nh, 0, sizeof(nh));
4a7371e9
DS
1060
1061 nh.vrf_id = VRF_DEFAULT;
d62a17ae 1062 /* This is a reject or blackhole route */
a8309422
DL
1063 if (flags & RTF_REJECT) {
1064 nh.type = NEXTHOP_TYPE_BLACKHOLE;
1065 nh.bh_type = BLACKHOLE_REJECT;
1066 } else if (flags & RTF_BLACKHOLE) {
1067 nh.type = NEXTHOP_TYPE_BLACKHOLE;
1068 nh.bh_type = BLACKHOLE_NULL;
1069 }
d62a17ae 1070
c626d369
DS
1071 /*
1072 * Ignore our own messages.
1073 */
1074 if (rtm->rtm_type != RTM_GET && rtm->rtm_pid == pid)
1075 return;
d62a17ae 1076
d62a17ae 1077 if (dest.sa.sa_family == AF_INET) {
026745a2 1078 afi = AFI_IP;
d62a17ae 1079 p.family = AF_INET;
1080 p.u.prefix4 = dest.sin.sin_addr;
1081 if (flags & RTF_HOST)
936fbaef 1082 p.prefixlen = IPV4_MAX_BITLEN;
d62a17ae 1083 else
1084 p.prefixlen = ip_masklen(mask.sin.sin_addr);
1085
a8309422
DL
1086 if (!nh.type) {
1087 nh.type = NEXTHOP_TYPE_IPV4;
1088 nh.gate.ipv4 = gate.sin.sin_addr;
1089 }
026745a2
DS
1090 } else if (dest.sa.sa_family == AF_INET6) {
1091 afi = AFI_IP6;
d62a17ae 1092 p.family = AF_INET6;
1093 p.u.prefix6 = dest.sin6.sin6_addr;
1094 if (flags & RTF_HOST)
f4d81e55 1095 p.prefixlen = IPV6_MAX_BITLEN;
d62a17ae 1096 else
1097 p.prefixlen = ip6_masklen(mask.sin6.sin6_addr);
718e3744 1098
1099#ifdef KAME
d62a17ae 1100 if (IN6_IS_ADDR_LINKLOCAL(&gate.sin6.sin6_addr)) {
1101 ifindex = IN6_LINKLOCAL_IFINDEX(gate.sin6.sin6_addr);
1102 SET_IN6_LINKLOCAL_IFINDEX(gate.sin6.sin6_addr, 0);
1103 }
718e3744 1104#endif /* KAME */
1105
a8309422
DL
1106 if (!nh.type) {
1107 nh.type = ifindex ? NEXTHOP_TYPE_IPV6_IFINDEX
1108 : NEXTHOP_TYPE_IPV6;
1109 nh.gate.ipv6 = gate.sin6.sin6_addr;
1110 nh.ifindex = ifindex;
1111 }
026745a2
DS
1112 } else
1113 return;
d62a17ae 1114
026745a2
DS
1115 /*
1116 * CHANGE: delete the old prefix, we have no further information
1117 * to specify the route really
1118 */
1119 if (rtm->rtm_type == RTM_CHANGE)
1120 rib_delete(afi, SAFI_UNICAST, VRF_DEFAULT, ZEBRA_ROUTE_KERNEL,
bc541126 1121 0, zebra_flags, &p, NULL, NULL, 0, RT_TABLE_MAIN, 0,
3ceae22b 1122 0, true);
026745a2
DS
1123 if (rtm->rtm_type == RTM_GET || rtm->rtm_type == RTM_ADD
1124 || rtm->rtm_type == RTM_CHANGE)
73d3197c
DS
1125 rib_add(afi, SAFI_UNICAST, VRF_DEFAULT, proto, 0, zebra_flags,
1126 &p, NULL, &nh, 0, RT_TABLE_MAIN, 0, 0, distance, 0,
1127 false);
026745a2 1128 else
73d3197c
DS
1129 rib_delete(afi, SAFI_UNICAST, VRF_DEFAULT, proto, 0,
1130 zebra_flags, &p, NULL, &nh, 0, RT_TABLE_MAIN, 0,
1131 distance, true);
718e3744 1132}
1133
1134/* Interface function for the kernel routing table updates. Support
6621ca86 1135 * for RTM_CHANGE will be needed.
1136 * Exported only for rt_socket.c
1137 */
d62a17ae 1138int rtm_write(int message, union sockunion *dest, union sockunion *mask,
1139 union sockunion *gate, union sockunion *mpls, unsigned int index,
a8309422 1140 enum blackhole_type bh_type, int metric)
718e3744 1141{
d62a17ae 1142 int ret;
1143 caddr_t pnt;
1144 struct interface *ifp;
1145
1146 /* Sequencial number of routing message. */
1147 static int msg_seq = 0;
1148
1149 /* Struct of rt_msghdr and buffer for storing socket's data. */
1150 struct {
1151 struct rt_msghdr rtm;
1152 char buf[512];
1153 } msg;
1154
80776aec 1155 if (dplane_routing_sock < 0)
d62a17ae 1156 return ZEBRA_ERR_EPERM;
1157
1158 /* Clear and set rt_msghdr values */
6006b807 1159 memset(&msg, 0, sizeof(msg));
d62a17ae 1160 msg.rtm.rtm_version = RTM_VERSION;
1161 msg.rtm.rtm_type = message;
1162 msg.rtm.rtm_seq = msg_seq++;
1163 msg.rtm.rtm_addrs = RTA_DST;
1164 msg.rtm.rtm_addrs |= RTA_GATEWAY;
1165 msg.rtm.rtm_flags = RTF_UP;
fe6c7157 1166#ifdef __OpenBSD__
d62a17ae 1167 msg.rtm.rtm_flags |= RTF_MPATH;
1168 msg.rtm.rtm_fmask = RTF_MPLS;
d3e2c74a 1169#endif
d62a17ae 1170 msg.rtm.rtm_index = index;
718e3744 1171
d62a17ae 1172 if (metric != 0) {
1173 msg.rtm.rtm_rmx.rmx_hopcount = metric;
1174 msg.rtm.rtm_inits |= RTV_HOPCOUNT;
1175 }
718e3744 1176
d62a17ae 1177 ifp = if_lookup_by_index(index, VRF_DEFAULT);
718e3744 1178
d62a17ae 1179 if (gate && (message == RTM_ADD || message == RTM_CHANGE))
1180 msg.rtm.rtm_flags |= RTF_GATEWAY;
718e3744 1181
d62a17ae 1182/* When RTF_CLONING is unavailable on BSD, should we set some
1183 * other flag instead?
1184 */
e6f148e6 1185#ifdef RTF_CLONING
d62a17ae 1186 if (!gate && (message == RTM_ADD || message == RTM_CHANGE) && ifp
1187 && (ifp->flags & IFF_POINTOPOINT) == 0)
1188 msg.rtm.rtm_flags |= RTF_CLONING;
e6f148e6 1189#endif /* RTF_CLONING */
718e3744 1190
d62a17ae 1191 /* If no protocol specific gateway is specified, use link
1192 address for gateway. */
1193 if (!gate) {
1194 if (!ifp) {
1195 char dest_buf[INET_ADDRSTRLEN] = "NULL",
1196 mask_buf[INET_ADDRSTRLEN] = "255.255.255.255";
1197 if (dest)
1198 inet_ntop(AF_INET, &dest->sin.sin_addr,
1199 dest_buf, INET_ADDRSTRLEN);
1200 if (mask)
1201 inet_ntop(AF_INET, &mask->sin.sin_addr,
1202 mask_buf, INET_ADDRSTRLEN);
9df414fe 1203 flog_warn(
e914ccbe 1204 EC_ZEBRA_RTM_NO_GATEWAY,
d62a17ae 1205 "%s: %s/%s: gate == NULL and no gateway found for ifindex %d",
1206 __func__, dest_buf, mask_buf, index);
1207 return -1;
1208 }
1209 gate = (union sockunion *)&((struct zebra_if *)ifp->info)->sdl;
1210 }
1211
1212 if (mask)
1213 msg.rtm.rtm_addrs |= RTA_NETMASK;
1214 else if (message == RTM_ADD || message == RTM_CHANGE)
1215 msg.rtm.rtm_flags |= RTF_HOST;
718e3744 1216
fe6c7157 1217#ifdef __OpenBSD__
d62a17ae 1218 if (mpls) {
1219 msg.rtm.rtm_addrs |= RTA_SRC;
1220 msg.rtm.rtm_flags |= RTF_MPLS;
1221
1222 if (mpls->smpls.smpls_label
70e98a7f 1223 != htonl(MPLS_LABEL_IMPLICIT_NULL << MPLS_LABEL_OFFSET))
d62a17ae 1224 msg.rtm.rtm_mpls = MPLS_OP_PUSH;
1225 }
d3e2c74a
RW
1226#endif
1227
d62a17ae 1228 /* Tagging route with flags */
1229 msg.rtm.rtm_flags |= (RTF_PROTO1);
718e3744 1230
a8309422
DL
1231 switch (bh_type) {
1232 case BLACKHOLE_UNSPEC:
1233 break;
1234 case BLACKHOLE_REJECT:
d62a17ae 1235 msg.rtm.rtm_flags |= RTF_REJECT;
a8309422
DL
1236 break;
1237 default:
1238 msg.rtm.rtm_flags |= RTF_BLACKHOLE;
1239 break;
1240 }
81dfcaa2 1241
718e3744 1242
d62a17ae 1243#define SOCKADDRSET(X, R) \
1244 if (msg.rtm.rtm_addrs & (R)) { \
1245 int len = SAROUNDUP(X); \
1246 memcpy(pnt, (caddr_t)(X), len); \
1247 pnt += len; \
1248 }
718e3744 1249
d62a17ae 1250 pnt = (caddr_t)msg.buf;
718e3744 1251
d62a17ae 1252 /* Write each socket data into rtm message buffer */
1253 SOCKADDRSET(dest, RTA_DST);
1254 SOCKADDRSET(gate, RTA_GATEWAY);
1255 SOCKADDRSET(mask, RTA_NETMASK);
fe6c7157 1256#ifdef __OpenBSD__
d62a17ae 1257 SOCKADDRSET(mpls, RTA_SRC);
d3e2c74a 1258#endif
718e3744 1259
d62a17ae 1260 msg.rtm.rtm_msglen = pnt - (caddr_t)&msg;
1261
80776aec 1262 ret = write(dplane_routing_sock, &msg, msg.rtm.rtm_msglen);
d62a17ae 1263
1264 if (ret != msg.rtm.rtm_msglen) {
1265 if (errno == EEXIST)
1266 return ZEBRA_ERR_RTEXIST;
1267 if (errno == ENETUNREACH)
1268 return ZEBRA_ERR_RTUNREACH;
1269 if (errno == ESRCH)
1270 return ZEBRA_ERR_RTNOEXIST;
1271
450971aa 1272 flog_err_sys(EC_LIB_SOCKET, "%s: write : %s (%d)", __func__,
9df414fe 1273 safe_strerror(errno), errno);
d62a17ae 1274 return ZEBRA_ERR_KERNEL;
1275 }
1276 return ZEBRA_ERR_NOERROR;
718e3744 1277}
1278
6b0655a2 1279
718e3744 1280#include "thread.h"
1281#include "zebra/zserv.h"
1282
718e3744 1283/* For debug purpose. */
d62a17ae 1284static void rtmsg_debug(struct rt_msghdr *rtm)
718e3744 1285{
5b62211d
RZ
1286 char fbuf[64];
1287
d62a17ae 1288 zlog_debug("Kernel: Len: %d Type: %s", rtm->rtm_msglen,
1289 lookup_msg(rtm_type_str, rtm->rtm_type, NULL));
1290 rtm_flag_dump(rtm->rtm_flags);
1291 zlog_debug("Kernel: message seq %d", rtm->rtm_seq);
5b62211d
RZ
1292 zlog_debug("Kernel: pid %lld, rtm_addrs {%s}", (long long)rtm->rtm_pid,
1293 rtatostr(rtm->rtm_addrs, fbuf, sizeof(fbuf)));
718e3744 1294}
1295
1296/* This is pretty gross, better suggestions welcome -- mhandler */
1297#ifndef RTAX_MAX
1298#ifdef RTA_NUMBITS
1299#define RTAX_MAX RTA_NUMBITS
1300#else
1301#define RTAX_MAX 8
1302#endif /* RTA_NUMBITS */
1303#endif /* RTAX_MAX */
1304
1305/* Kernel routing table and interface updates via routing socket. */
cc9f21da 1306static void kernel_read(struct thread *thread)
718e3744 1307{
d62a17ae 1308 int sock;
1309 int nbytes;
1310 struct rt_msghdr *rtm;
1311
1312 /*
1313 * This must be big enough for any message the kernel might send.
1314 * Rather than determining how many sockaddrs of what size might be
1315 * in each particular message, just use RTAX_MAX of sockaddr_storage
1316 * for each. Note that the sockaddrs must be after each message
1317 * definition, or rather after whichever happens to be the largest,
1318 * since the buffer needs to be big enough for a message and the
1319 * sockaddrs together.
1320 */
1321 union {
1322 /* Routing information. */
1323 struct {
1324 struct rt_msghdr rtm;
1325 struct sockaddr_storage addr[RTAX_MAX];
1326 } r;
1327
1328 /* Interface information. */
1329 struct {
1330 struct if_msghdr ifm;
1331 struct sockaddr_storage addr[RTAX_MAX];
1332 } im;
1333
1334 /* Interface address information. */
1335 struct {
1336 struct ifa_msghdr ifa;
1337 struct sockaddr_storage addr[RTAX_MAX];
1338 } ia;
718e3744 1339
1340#ifdef RTM_IFANNOUNCE
d62a17ae 1341 /* Interface arrival/departure */
1342 struct {
1343 struct if_announcemsghdr ifan;
1344 struct sockaddr_storage addr[RTAX_MAX];
1345 } ian;
718e3744 1346#endif /* RTM_IFANNOUNCE */
1347
d62a17ae 1348 } buf;
718e3744 1349
d62a17ae 1350 /* Fetch routing socket. */
1351 sock = THREAD_FD(thread);
718e3744 1352
0d6f7fd6 1353 nbytes = read(sock, &buf, sizeof(buf));
718e3744 1354
68cd699d
RM
1355 if (nbytes < 0) {
1356 if (errno == ENOBUFS) {
26e95efa
RZ
1357#ifdef __FreeBSD__
1358 /*
1359 * ENOBUFS indicates a temporary resource
1360 * shortage and is not harmful for consistency of
1361 * reading the routing socket. Ignore it.
1362 */
1363 thread_add_read(zrouter.master, kernel_read, NULL, sock,
1364 NULL);
1365 return;
1366#else
68cd699d
RM
1367 flog_err(EC_ZEBRA_RECVMSG_OVERRUN,
1368 "routing socket overrun: %s",
1369 safe_strerror(errno));
1370 /*
1371 * In this case we are screwed.
1372 * There is no good way to
1373 * recover zebra at this point.
1374 */
1375 exit(-1);
26e95efa 1376#endif
68cd699d
RM
1377 }
1378 if (errno != EAGAIN && errno != EWOULDBLOCK)
450971aa 1379 flog_err_sys(EC_LIB_SOCKET, "routing socket error: %s",
9df414fe 1380 safe_strerror(errno));
cc9f21da 1381 return;
d62a17ae 1382 }
718e3744 1383
68cd699d 1384 if (nbytes == 0)
cc9f21da 1385 return;
68cd699d 1386
3801e764 1387 thread_add_read(zrouter.master, kernel_read, NULL, sock, NULL);
718e3744 1388
d62a17ae 1389 if (IS_ZEBRA_DEBUG_KERNEL)
1390 rtmsg_debug(&buf.r.rtm);
718e3744 1391
d62a17ae 1392 rtm = &buf.r.rtm;
718e3744 1393
d62a17ae 1394 /*
1395 * Ensure that we didn't drop any data, so that processing routines
1396 * can assume they have the whole message.
1397 */
1398 if (rtm->rtm_msglen != nbytes) {
9df414fe 1399 zlog_debug(
63efca0e 1400 "kernel_read: rtm->rtm_msglen %d, nbytes %d, type %d",
d62a17ae 1401 rtm->rtm_msglen, nbytes, rtm->rtm_type);
cc9f21da 1402 return;
d62a17ae 1403 }
1404
1405 switch (rtm->rtm_type) {
1406 case RTM_ADD:
1407 case RTM_DELETE:
1408 case RTM_CHANGE:
1409 rtm_read(rtm);
1410 break;
1411 case RTM_IFINFO:
1412 ifm_read(&buf.im.ifm);
1413 break;
1414 case RTM_NEWADDR:
1415 case RTM_DELADDR:
1416 ifam_read(&buf.ia.ifa);
1417 break;
718e3744 1418#ifdef RTM_IFANNOUNCE
d62a17ae 1419 case RTM_IFANNOUNCE:
1420 ifan_read(&buf.ian.ifan);
1421 break;
718e3744 1422#endif /* RTM_IFANNOUNCE */
d62a17ae 1423 default:
1424 if (IS_ZEBRA_DEBUG_KERNEL)
0081ab91
DS
1425 zlog_debug(
1426 "Unprocessed RTM_type: %s(%d)",
1427 lookup_msg(rtm_type_str, rtm->rtm_type, NULL),
1428 rtm->rtm_type);
d62a17ae 1429 break;
1430 }
718e3744 1431}
1432
1433/* Make routing socket. */
d62a17ae 1434static void routing_socket(struct zebra_ns *zns)
718e3744 1435{
9fb83b55
DS
1436 uint32_t default_rcvbuf;
1437 socklen_t optlen;
1438
0cf6db21 1439 frr_with_privs(&zserv_privs) {
6bb30c2c 1440 routing_sock = ns_socket(AF_ROUTE, SOCK_RAW, 0, zns->ns_id);
80776aec
MS
1441
1442 dplane_routing_sock =
1443 ns_socket(AF_ROUTE, SOCK_RAW, 0, zns->ns_id);
6bb30c2c 1444 }
d62a17ae 1445
1446 if (routing_sock < 0) {
1c50c1c0 1447 flog_err_sys(EC_LIB_SOCKET, "Can't init kernel routing socket");
d62a17ae 1448 return;
1449 }
1450
80776aec
MS
1451 if (dplane_routing_sock < 0) {
1452 flog_err_sys(EC_LIB_SOCKET,
1453 "Can't init kernel dataplane routing socket");
1454 return;
1455 }
1456
68cd699d
RM
1457#ifdef SO_RERROR
1458 /* Allow reporting of route(4) buffer overflow errors */
1459 int n = 1;
355c74b7 1460
68cd699d
RM
1461 if (setsockopt(routing_sock, SOL_SOCKET, SO_RERROR, &n, sizeof(n)) < 0)
1462 flog_err_sys(EC_LIB_SOCKET,
1463 "Can't set SO_RERROR on routing socket");
1464#endif
1465
d62a17ae 1466 /* XXX: Socket should be NONBLOCK, however as we currently
1467 * discard failed writes, this will lead to inconsistencies.
1468 * For now, socket must be blocking.
1469 */
1470 /*if (fcntl (routing_sock, F_SETFL, O_NONBLOCK) < 0)
1471 zlog_warn ("Can't set O_NONBLOCK to routing socket");*/
1472
9fb83b55
DS
1473 /*
1474 * Attempt to set a more useful receive buffer size
1475 */
1476 optlen = sizeof(default_rcvbuf);
1477 if (getsockopt(routing_sock, SOL_SOCKET, SO_RCVBUF, &default_rcvbuf,
1478 &optlen) == -1)
1479 flog_err_sys(EC_LIB_SOCKET,
1480 "routing_sock sockopt SOL_SOCKET SO_RCVBUF");
1481 else {
1482 for (; rcvbufsize > default_rcvbuf &&
1483 setsockopt(routing_sock, SOL_SOCKET, SO_RCVBUF,
1484 &rcvbufsize, sizeof(rcvbufsize)) == -1 &&
1485 errno == ENOBUFS;
1486 rcvbufsize /= 2)
1487 ;
1488 }
1489
d62a17ae 1490 /* kernel_read needs rewrite. */
3801e764 1491 thread_add_read(zrouter.master, kernel_read, NULL, routing_sock, NULL);
718e3744 1492}
1493
1494/* Exported interface function. This function simply calls
1495 routing_socket (). */
d62a17ae 1496void kernel_init(struct zebra_ns *zns)
8f7d9fc0 1497{
d62a17ae 1498 routing_socket(zns);
8f7d9fc0
FL
1499}
1500
62b8bb7a 1501void kernel_terminate(struct zebra_ns *zns, bool complete)
718e3744 1502{
d62a17ae 1503 return;
718e3744 1504}
ddfeb486 1505
34869809
MS
1506/*
1507 * Global init for platform-/OS-specific things
1508 */
1509void kernel_router_init(void)
1510{
1511}
1512
1513/*
1514 * Global deinit for platform-/OS-specific things
1515 */
1516void kernel_router_terminate(void)
1517{
1518}
1519
d166308b
MS
1520/*
1521 * Called by the dplane pthread to read incoming OS messages and dispatch them.
1522 */
1523int kernel_dplane_read(struct zebra_dplane_info *info)
1524{
1525 return 0;
1526}
1527
fef24b03
JU
1528void kernel_update_multi(struct dplane_ctx_q *ctx_list)
1529{
18f60fe9
JU
1530 struct zebra_dplane_ctx *ctx;
1531 struct dplane_ctx_q handled_list;
39ffa8e8 1532 enum zebra_dplane_result res = ZEBRA_DPLANE_REQUEST_SUCCESS;
fef24b03 1533
18f60fe9
JU
1534 TAILQ_INIT(&handled_list);
1535
1536 while (true) {
1537 ctx = dplane_ctx_dequeue(ctx_list);
1538 if (ctx == NULL)
1539 break;
1540
1541 /*
1542 * A previous provider plugin may have asked to skip the
1543 * kernel update.
1544 */
1545 if (dplane_ctx_is_skip_kernel(ctx)) {
1546 res = ZEBRA_DPLANE_REQUEST_SUCCESS;
1547 goto skip_one;
1548 }
1549
1550 switch (dplane_ctx_get_op(ctx)) {
1551
1552 case DPLANE_OP_ROUTE_INSTALL:
1553 case DPLANE_OP_ROUTE_UPDATE:
1554 case DPLANE_OP_ROUTE_DELETE:
1555 res = kernel_route_update(ctx);
1556 break;
1557
1558 case DPLANE_OP_NH_INSTALL:
1559 case DPLANE_OP_NH_UPDATE:
1560 case DPLANE_OP_NH_DELETE:
1561 res = kernel_nexthop_update(ctx);
1562 break;
1563
1564 case DPLANE_OP_LSP_INSTALL:
1565 case DPLANE_OP_LSP_UPDATE:
1566 case DPLANE_OP_LSP_DELETE:
1567 res = kernel_lsp_update(ctx);
1568 break;
1569
1570 case DPLANE_OP_PW_INSTALL:
1571 case DPLANE_OP_PW_UNINSTALL:
1572 res = kernel_pw_update(ctx);
1573 break;
1574
1575 case DPLANE_OP_ADDR_INSTALL:
1576 case DPLANE_OP_ADDR_UNINSTALL:
1577 res = kernel_address_update_ctx(ctx);
1578 break;
1579
1580 case DPLANE_OP_MAC_INSTALL:
1581 case DPLANE_OP_MAC_DELETE:
1582 res = kernel_mac_update_ctx(ctx);
1583 break;
1584
1585 case DPLANE_OP_NEIGH_INSTALL:
1586 case DPLANE_OP_NEIGH_UPDATE:
1587 case DPLANE_OP_NEIGH_DELETE:
1588 case DPLANE_OP_VTEP_ADD:
1589 case DPLANE_OP_VTEP_DELETE:
d68e74b4 1590 case DPLANE_OP_NEIGH_DISCOVER:
18f60fe9
JU
1591 res = kernel_neigh_update_ctx(ctx);
1592 break;
1593
1594 case DPLANE_OP_RULE_ADD:
1595 case DPLANE_OP_RULE_DELETE:
1596 case DPLANE_OP_RULE_UPDATE:
1597 res = kernel_pbr_rule_update(ctx);
1598 break;
1599
97c72633
SW
1600 case DPLANE_OP_INTF_INSTALL:
1601 case DPLANE_OP_INTF_UPDATE:
1602 case DPLANE_OP_INTF_DELETE:
1603 res = kernel_intf_update(ctx);
1604 break;
1605
c8e718ce
SY
1606 case DPLANE_OP_TC_INSTALL:
1607 case DPLANE_OP_TC_UPDATE:
1608 case DPLANE_OP_TC_DELETE:
1609 res = kernel_tc_update(ctx);
1610 break;
1611
18f60fe9
JU
1612 /* Ignore 'notifications' - no-op */
1613 case DPLANE_OP_SYS_ROUTE_ADD:
1614 case DPLANE_OP_SYS_ROUTE_DELETE:
1615 case DPLANE_OP_ROUTE_NOTIFY:
1616 case DPLANE_OP_LSP_NOTIFY:
1617 res = ZEBRA_DPLANE_REQUEST_SUCCESS;
1618 break;
1619
39ffa8e8
DS
1620 case DPLANE_OP_INTF_NETCONFIG:
1621 res = kernel_intf_netconf_update(ctx);
18f60fe9 1622 break;
39ffa8e8
DS
1623
1624 case DPLANE_OP_NONE:
1625 case DPLANE_OP_BR_PORT_UPDATE:
1626 case DPLANE_OP_IPTABLE_ADD:
1627 case DPLANE_OP_IPTABLE_DELETE:
1628 case DPLANE_OP_IPSET_ADD:
1629 case DPLANE_OP_IPSET_DELETE:
1630 case DPLANE_OP_IPSET_ENTRY_ADD:
1631 case DPLANE_OP_IPSET_ENTRY_DELETE:
1632 case DPLANE_OP_NEIGH_IP_INSTALL:
1633 case DPLANE_OP_NEIGH_IP_DELETE:
1634 case DPLANE_OP_NEIGH_TABLE_UPDATE:
1635 case DPLANE_OP_GRE_SET:
1636 case DPLANE_OP_INTF_ADDR_ADD:
1637 case DPLANE_OP_INTF_ADDR_DEL:
1638 zlog_err("Unhandled dplane data for %s",
1639 dplane_op2str(dplane_ctx_get_op(ctx)));
1640 res = ZEBRA_DPLANE_REQUEST_FAILURE;
18f60fe9
JU
1641 }
1642
1643 skip_one:
1644 dplane_ctx_set_status(ctx, res);
1645
1646 dplane_ctx_enqueue_tail(&handled_list, ctx);
1647 }
1648
1649 TAILQ_INIT(ctx_list);
1650 dplane_ctx_list_append(ctx_list, &handled_list);
fef24b03
JU
1651}
1652
ddfeb486 1653#endif /* !HAVE_NETLINK */