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