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