]> git.proxmox.com Git - mirror_frr.git/blame - zebra/kernel_socket.c
bgpd, pimd: Remove undebug commands
[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
RZ
282 if (copylen > destlen) {
283 zlog_warn("%s: destination buffer too small (%lu vs %lu)",
284 __func__, copylen, destlen);
285 memcpy(dest, sap, destlen);
286 } else
287 memcpy(dest, sap, copylen);
288 }
289
290 return tlen;
291}
292
19fb538d
RZ
293size_t rta_get(caddr_t sap, void *destp, size_t destlen)
294{
295 return _rta_get(sap, destp, destlen, true);
296}
297
298size_t rta_getattr(caddr_t sap, void *destp, size_t destlen)
299{
300 return _rta_get(sap, destp, destlen, false);
301}
302
86c57af5
RZ
303size_t rta_getsdlname(caddr_t sap, void *destp, short *destlen)
304{
305 struct sockaddr_dl *sdl = (struct sockaddr_dl *)sap;
306 struct sockaddr *sa = (struct sockaddr *)sap;
307 uint8_t *dest = destp;
308 size_t tlen, copylen;
309
310 copylen = sdl->sdl_nlen;
311#ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
312 tlen = (sa->sa_len == 0) ? sizeof(ROUNDUP_TYPE) : ROUNDUP(sa->sa_len);
313#else /* !HAVE_STRUCT_SOCKADDR_SA_LEN */
314 tlen = SAROUNDUP(sap);
315#endif /* !HAVE_STRUCT_SOCKADDR_SA_LEN */
316
317 if (copylen > 0 && dest != NULL && sdl->sdl_family == AF_LINK) {
318 if (copylen > IFNAMSIZ) {
319 zlog_warn("%s: destination buffer too small (%lu vs %d)",
320 __func__, copylen, IFNAMSIZ);
321 memcpy(dest, sdl->sdl_data, IFNAMSIZ);
322 dest[IFNAMSIZ] = 0;
323 *destlen = IFNAMSIZ;
324 } else {
325 memcpy(dest, sdl->sdl_data, copylen);
326 dest[copylen] = 0;
327 *destlen = copylen;
328 }
329 } else
330 *destlen = 0;
331
332 return tlen;
333}
334
5b62211d
RZ
335const char *rtatostr(unsigned int flags, char *buf, size_t buflen)
336{
337 const char *flagstr, *bufstart;
338 int bit, wlen;
339 char ustr[32];
340
341 /* Hold the pointer to the buffer beginning. */
342 bufstart = buf;
343
344 for (bit = 1; bit; bit <<= 1) {
345 if ((flags & bit) == 0)
346 continue;
347
348 switch (bit) {
349 case RTA_DST:
350 flagstr = "DST";
351 break;
352 case RTA_GATEWAY:
353 flagstr = "GATEWAY";
354 break;
355 case RTA_NETMASK:
356 flagstr = "NETMASK";
357 break;
358#ifdef RTA_GENMASK
359 case RTA_GENMASK:
360 flagstr = "GENMASK";
361 break;
362#endif /* RTA_GENMASK */
363 case RTA_IFP:
364 flagstr = "IFP";
365 break;
366 case RTA_IFA:
367 flagstr = "IFA";
368 break;
369#ifdef RTA_AUTHOR
370 case RTA_AUTHOR:
371 flagstr = "AUTHOR";
372 break;
373#endif /* RTA_AUTHOR */
374 case RTA_BRD:
375 flagstr = "BRD";
376 break;
377#ifdef RTA_SRC
378 case RTA_SRC:
379 flagstr = "SRC";
380 break;
381#endif /* RTA_SRC */
382#ifdef RTA_SRCMASK
383 case RTA_SRCMASK:
384 flagstr = "SRCMASK";
385 break;
386#endif /* RTA_SRCMASK */
387#ifdef RTA_LABEL
388 case RTA_LABEL:
389 flagstr = "LABEL";
390 break;
391#endif /* RTA_LABEL */
392
393 default:
394 snprintf(ustr, sizeof(ustr), "0x%x", bit);
395 flagstr = ustr;
396 break;
397 }
398
399 wlen = snprintf(buf, buflen, "%s,", flagstr);
400 buf += wlen;
401 buflen -= wlen;
402 }
403
404 /* Check for empty buffer. */
405 if (bufstart != buf)
406 buf--;
407
408 /* Remove the last comma. */
409 *buf = 0;
410
411 return bufstart;
412}
413
718e3744 414/* Dump routing table flag for debug purpose. */
d62a17ae 415static void rtm_flag_dump(int flag)
718e3744 416{
d62a17ae 417 const struct message *mes;
418 static char buf[BUFSIZ];
419
420 buf[0] = '\0';
421 for (mes = rtm_flag_str; mes->key != 0; mes++) {
422 if (mes->key & flag) {
423 strlcat(buf, mes->str, BUFSIZ);
424 strlcat(buf, " ", BUFSIZ);
425 }
718e3744 426 }
d62a17ae 427 zlog_debug("Kernel: %s", buf);
718e3744 428}
429
430#ifdef RTM_IFANNOUNCE
431/* Interface adding function */
d62a17ae 432static int ifan_read(struct if_announcemsghdr *ifan)
718e3744 433{
d62a17ae 434 struct interface *ifp;
435
436 ifp = if_lookup_by_index(ifan->ifan_index, VRF_DEFAULT);
437
438 if (ifp)
439 assert((ifp->ifindex == ifan->ifan_index)
440 || (ifp->ifindex == IFINDEX_INTERNAL));
441
9d303b37
DL
442 if ((ifp == NULL) || ((ifp->ifindex == IFINDEX_INTERNAL)
443 && (ifan->ifan_what == IFAN_ARRIVAL))) {
d62a17ae 444 if (IS_ZEBRA_DEBUG_KERNEL)
445 zlog_debug(
446 "%s: creating interface for ifindex %d, name %s",
447 __func__, ifan->ifan_index, ifan->ifan_name);
448
449 /* Create Interface */
8f90d89b 450 ifp = if_get_by_name(ifan->ifan_name, VRF_DEFAULT);
ff880b78 451 if_set_index(ifp, ifan->ifan_index);
d62a17ae 452
453 if_get_metric(ifp);
454 if_add_update(ifp);
455 } else if (ifp != NULL && ifan->ifan_what == IFAN_DEPARTURE)
456 if_delete_update(ifp);
457
458 if_get_flags(ifp);
459 if_get_mtu(ifp);
460 if_get_metric(ifp);
461
462 if (IS_ZEBRA_DEBUG_KERNEL)
463 zlog_debug("%s: interface %s index %d", __func__,
464 ifan->ifan_name, ifan->ifan_index);
465
466 return 0;
718e3744 467}
468#endif /* RTM_IFANNOUNCE */
469
9234b382 470#ifdef HAVE_BSD_IFI_LINK_STATE
c543a173 471/* BSD link detect translation */
d62a17ae 472static void bsd_linkdetect_translate(struct if_msghdr *ifm)
c543a173 473{
d62a17ae 474 if ((ifm->ifm_data.ifi_link_state >= LINK_STATE_UP)
475 || (ifm->ifm_data.ifi_link_state == LINK_STATE_UNKNOWN))
476 SET_FLAG(ifm->ifm_flags, IFF_RUNNING);
477 else
478 UNSET_FLAG(ifm->ifm_flags, IFF_RUNNING);
c543a173 479}
9234b382 480#endif /* HAVE_BSD_IFI_LINK_STATE */
c543a173 481
d62a17ae 482static enum zebra_link_type sdl_to_zebra_link_type(unsigned int sdlt)
8ccc7e80 483{
d62a17ae 484 switch (sdlt) {
485 case IFT_ETHER:
486 return ZEBRA_LLT_ETHER;
487 case IFT_X25:
488 return ZEBRA_LLT_X25;
489 case IFT_FDDI:
490 return ZEBRA_LLT_FDDI;
491 case IFT_PPP:
492 return ZEBRA_LLT_PPP;
493 case IFT_LOOP:
494 return ZEBRA_LLT_LOOPBACK;
495 case IFT_SLIP:
496 return ZEBRA_LLT_SLIP;
497 case IFT_ARCNET:
498 return ZEBRA_LLT_ARCNET;
499 case IFT_ATM:
500 return ZEBRA_LLT_ATM;
501 case IFT_LOCALTALK:
502 return ZEBRA_LLT_LOCALTLK;
503 case IFT_HIPPI:
504 return ZEBRA_LLT_HIPPI;
8ccc7e80 505#ifdef IFT_IEEE1394
d62a17ae 506 case IFT_IEEE1394:
507 return ZEBRA_LLT_IEEE1394;
8ccc7e80
TT
508#endif
509
d62a17ae 510 default:
511 return ZEBRA_LLT_UNKNOWN;
512 }
8ccc7e80
TT
513}
514
da26e3b6 515/*
516 * Handle struct if_msghdr obtained from reading routing socket or
517 * sysctl (from interface_list). There may or may not be sockaddrs
518 * present after the header.
519 */
d62a17ae 520int ifm_read(struct if_msghdr *ifm)
718e3744 521{
d62a17ae 522 struct interface *ifp = NULL;
523 struct sockaddr_dl *sdl;
524 char ifname[IFNAMSIZ];
525 short ifnlen = 0;
86c57af5 526 int maskbit;
d62a17ae 527 caddr_t cp;
5b62211d 528 char fbuf[64];
d62a17ae 529
530 /* terminate ifname at head (for strnlen) and tail (for safety) */
531 ifname[IFNAMSIZ - 1] = '\0';
532
533 /* paranoia: sanity check structure */
534 if (ifm->ifm_msglen < sizeof(struct if_msghdr)) {
e914ccbe 535 flog_err(EC_ZEBRA_NETLINK_LENGTH_ERROR,
1c50c1c0
QY
536 "ifm_read: ifm->ifm_msglen %d too short\n",
537 ifm->ifm_msglen);
d62a17ae 538 return -1;
539 }
540
541 /*
542 * Check for a sockaddr_dl following the message. First, point to
543 * where a socakddr might be if one follows the message.
544 */
545 cp = (void *)(ifm + 1);
718e3744 546
4bfbea8c 547#ifdef SUNOS_5
d62a17ae 548 /*
549 * XXX This behavior should be narrowed to only the kernel versions
550 * for which the structures returned do not match the headers.
551 *
552 * if_msghdr_t on 64 bit kernels in Solaris 9 and earlier versions
553 * is 12 bytes larger than the 32 bit version.
554 */
555 if (((struct sockaddr *)cp)->sa_family == AF_UNSPEC)
556 cp = cp + 12;
4bfbea8c 557#endif
3e95a074 558
86c57af5
RZ
559 /* Look up for RTA_IFP and skip others. */
560 for (maskbit = 1; maskbit; maskbit <<= 1) {
561 if ((maskbit & ifm->ifm_addrs) == 0)
562 continue;
563 if (maskbit != RTA_IFP) {
564 cp += rta_get(cp, NULL, 0);
565 continue;
566 }
567
568 /* Save the pointer to the structure. */
569 sdl = (struct sockaddr_dl *)cp;
570 cp += rta_getsdlname(cp, ifname, &ifnlen);
571 }
d62a17ae 572
573 if (IS_ZEBRA_DEBUG_KERNEL)
5b62211d
RZ
574 zlog_debug("%s: sdl ifname %s addrs {%s}", __func__,
575 (ifnlen ? ifname : "(nil)"),
576 rtatostr(ifm->ifm_addrs, fbuf, sizeof(fbuf)));
d62a17ae 577
578 /*
579 * Look up on ifindex first, because ifindices are the primary handle
580 * for
581 * interfaces across the user/kernel boundary, for most systems. (Some
582 * messages, such as up/down status changes on NetBSD, do not include a
583 * sockaddr_dl).
584 */
585 if ((ifp = if_lookup_by_index(ifm->ifm_index, VRF_DEFAULT)) != NULL) {
586 /* we have an ifp, verify that the name matches as some systems,
587 * eg Solaris, have a 1:many association of ifindex:ifname
588 * if they dont match, we dont have the correct ifp and should
589 * set it back to NULL to let next check do lookup by name
590 */
591 if (ifnlen && (strncmp(ifp->name, ifname, IFNAMSIZ) != 0)) {
592 if (IS_ZEBRA_DEBUG_KERNEL)
593 zlog_debug(
0437e105 594 "%s: ifp name %s doesn't match sdl name %s",
d62a17ae 595 __func__, ifp->name, ifname);
596 ifp = NULL;
597 }
da26e3b6 598 }
5c78b3d0 599
d62a17ae 600 /*
601 * If we dont have an ifp, try looking up by name. Particularly as some
602 * systems (Solaris) have a 1:many mapping of ifindex:ifname - the
603 * ifname
604 * is therefore our unique handle to that interface.
605 *
606 * Interfaces specified in the configuration file for which the ifindex
607 * has not been determined will have ifindex == IFINDEX_INTERNAL, and
608 * such
609 * interfaces are found by this search, and then their ifindex values
610 * can
611 * be filled in.
612 */
613 if ((ifp == NULL) && ifnlen)
614 ifp = if_lookup_by_name(ifname, VRF_DEFAULT);
615
616 /*
617 * If ifp still does not exist or has an invalid index
618 * (IFINDEX_INTERNAL),
619 * create or fill in an interface.
620 */
621 if ((ifp == NULL) || (ifp->ifindex == IFINDEX_INTERNAL)) {
622 /*
623 * To create or fill in an interface, a sockaddr_dl (via
624 * RTA_IFP) is required.
625 */
626 if (!ifnlen) {
9165c5f5 627 zlog_debug("Interface index %d (new) missing ifname",
9df414fe 628 ifm->ifm_index);
d62a17ae 629 return -1;
630 }
631
5c78b3d0 632#ifndef RTM_IFANNOUNCE
d62a17ae 633 /* Down->Down interface should be ignored here.
634 * See further comment below.
635 */
636 if (!CHECK_FLAG(ifm->ifm_flags, IFF_UP))
637 return 0;
5c78b3d0 638#endif /* !RTM_IFANNOUNCE */
d62a17ae 639
640 if (ifp == NULL) {
641 /* Interface that zebra was not previously aware of, so
642 * create. */
bcc24579 643 ifp = if_create(ifname, VRF_DEFAULT);
d62a17ae 644 if (IS_ZEBRA_DEBUG_KERNEL)
645 zlog_debug("%s: creating ifp for ifindex %d",
646 __func__, ifm->ifm_index);
647 }
648
649 if (IS_ZEBRA_DEBUG_KERNEL)
650 zlog_debug(
651 "%s: updated/created ifp, ifname %s, ifindex %d",
652 __func__, ifp->name, ifp->ifindex);
653 /*
654 * Fill in newly created interface structure, or larval
655 * structure with ifindex IFINDEX_INTERNAL.
656 */
ff880b78 657 if_set_index(ifp, ifm->ifm_index);
d62a17ae 658
9234b382 659#ifdef HAVE_BSD_IFI_LINK_STATE /* translate BSD kernel msg for link-state */
d62a17ae 660 bsd_linkdetect_translate(ifm);
9234b382 661#endif /* HAVE_BSD_IFI_LINK_STATE */
c543a173 662
d62a17ae 663 if_flags_update(ifp, ifm->ifm_flags);
718e3744 664#if defined(__bsdi__)
d62a17ae 665 if_kvm_get_mtu(ifp);
718e3744 666#else
d62a17ae 667 if_get_mtu(ifp);
718e3744 668#endif /* __bsdi__ */
d62a17ae 669 if_get_metric(ifp);
670
671 /*
672 * XXX sockaddr_dl contents can be larger than the structure
673 * definition. There are 2 big families here:
674 * - BSD has sdl_len + sdl_data[16] + overruns sdl_data
675 * we MUST use sdl_len here or we'll truncate data.
676 * - Solaris has no sdl_len, but sdl_data[244]
677 * presumably, it's not going to run past that, so sizeof()
678 * is fine here.
86c57af5 679 * a nonzero ifnlen from rta_getsdlname() means sdl is valid
d62a17ae 680 */
681 ifp->ll_type = ZEBRA_LLT_UNKNOWN;
682 ifp->hw_addr_len = 0;
683 if (ifnlen) {
ca3ccd87 684#ifdef HAVE_STRUCT_SOCKADDR_DL_SDL_LEN
d62a17ae 685 memcpy(&((struct zebra_if *)ifp->info)->sdl, sdl,
686 sdl->sdl_len);
ca3ccd87 687#else
d62a17ae 688 memcpy(&((struct zebra_if *)ifp->info)->sdl, sdl,
689 sizeof(struct sockaddr_dl));
ca3ccd87 690#endif /* HAVE_STRUCT_SOCKADDR_DL_SDL_LEN */
8ccc7e80 691
d62a17ae 692 ifp->ll_type = sdl_to_zebra_link_type(sdl->sdl_type);
693 if (sdl->sdl_alen <= sizeof(ifp->hw_addr)) {
694 memcpy(ifp->hw_addr, LLADDR(sdl),
695 sdl->sdl_alen);
696 ifp->hw_addr_len = sdl->sdl_alen;
697 }
698 }
699
700 if_add_update(ifp);
701 } else
702 /*
703 * Interface structure exists. Adjust stored flags from
704 * notification. If interface has up->down or down->up
705 * transition, call state change routines (to adjust routes,
706 * notify routing daemons, etc.). (Other flag changes are stored
707 * but apparently do not trigger action.)
708 */
709 {
710 if (ifp->ifindex != ifm->ifm_index) {
9df414fe 711 zlog_debug(
d62a17ae 712 "%s: index mismatch, ifname %s, ifp index %d, "
713 "ifm index %d",
714 __func__, ifp->name, ifp->ifindex,
715 ifm->ifm_index);
716 return -1;
717 }
718
9234b382 719#ifdef HAVE_BSD_IFI_LINK_STATE /* translate BSD kernel msg for link-state */
d62a17ae 720 bsd_linkdetect_translate(ifm);
9234b382 721#endif /* HAVE_BSD_IFI_LINK_STATE */
c543a173 722
d62a17ae 723 /* update flags and handle operative->inoperative transition, if
724 * any */
725 if_flags_update(ifp, ifm->ifm_flags);
726
6eb8827d 727#ifndef RTM_IFANNOUNCE
d62a17ae 728 if (!if_is_up(ifp)) {
729 /* No RTM_IFANNOUNCE on this platform, so we can never
730 * distinguish between ~IFF_UP and delete. We must
731 * presume
732 * it has been deleted.
733 * Eg, Solaris will not notify us of unplumb.
734 *
735 * XXX: Fixme - this should be runtime detected
736 * So that a binary compiled on a system with IFANNOUNCE
737 * will still behave correctly if run on a platform
738 * without
739 */
740 if_delete_update(ifp);
741 }
6eb8827d 742#endif /* RTM_IFANNOUNCE */
d62a17ae 743 if (if_is_up(ifp)) {
1ba27564 744#if defined(__bsdi__)
d62a17ae 745 if_kvm_get_mtu(ifp);
1ba27564 746#else
d62a17ae 747 if_get_mtu(ifp);
1ba27564 748#endif /* __bsdi__ */
d62a17ae 749 if_get_metric(ifp);
750 }
751 }
5c78b3d0 752
718e3744 753#ifdef HAVE_NET_RT_IFLIST
d62a17ae 754 ifp->stats = ifm->ifm_data;
718e3744 755#endif /* HAVE_NET_RT_IFLIST */
04427fcb 756 ifp->speed = ifm->ifm_data.ifi_baudrate / 1000000;
718e3744 757
d62a17ae 758 if (IS_ZEBRA_DEBUG_KERNEL)
759 zlog_debug("%s: interface %s index %d", __func__, ifp->name,
760 ifp->ifindex);
718e3744 761
d62a17ae 762 return 0;
718e3744 763}
6b0655a2 764
718e3744 765/* Address read from struct ifa_msghdr. */
d62a17ae 766static void ifam_read_mesg(struct ifa_msghdr *ifm, union sockunion *addr,
767 union sockunion *mask, union sockunion *brd,
768 char *ifname, short *ifnlen)
718e3744 769{
d62a17ae 770 caddr_t pnt, end;
771 union sockunion dst;
772 union sockunion gateway;
86c57af5 773 int maskbit;
5b62211d 774 char fbuf[64];
d62a17ae 775
776 pnt = (caddr_t)(ifm + 1);
777 end = ((caddr_t)ifm) + ifm->ifam_msglen;
778
779 /* Be sure structure is cleared */
780 memset(mask, 0, sizeof(union sockunion));
781 memset(addr, 0, sizeof(union sockunion));
782 memset(brd, 0, sizeof(union sockunion));
783 memset(&dst, 0, sizeof(union sockunion));
784 memset(&gateway, 0, sizeof(union sockunion));
785
786 /* We fetch each socket variable into sockunion. */
86c57af5
RZ
787 for (maskbit = 1; maskbit; maskbit <<= 1) {
788 if ((maskbit & ifm->ifam_addrs) == 0)
789 continue;
790
791 switch (maskbit) {
792 case RTA_DST:
793 pnt += rta_get(pnt, &dst, sizeof(dst));
794 break;
795 case RTA_GATEWAY:
796 pnt += rta_get(pnt, &gateway, sizeof(gateway));
797 break;
798 case RTA_NETMASK:
19fb538d 799 pnt += rta_getattr(pnt, mask, sizeof(*mask));
86c57af5
RZ
800 break;
801 case RTA_IFP:
802 pnt += rta_getsdlname(pnt, ifname, ifnlen);
803 break;
804 case RTA_IFA:
805 pnt += rta_get(pnt, addr, sizeof(*addr));
806 break;
807 case RTA_BRD:
808 pnt += rta_get(pnt, brd, sizeof(*brd));
809 break;
810
811 default:
812 pnt += rta_get(pnt, NULL, 0);
813 break;
814 }
815
816 if (pnt > end) {
817 zlog_warn("%s: overflow detected (pnt:%p end:%p)",
818 __func__, pnt, end);
819 break;
820 }
821 }
d62a17ae 822
823 if (IS_ZEBRA_DEBUG_KERNEL) {
7053cae4 824 switch (sockunion_family(addr)) {
d62a17ae 825 case AF_INET:
826 case AF_INET6: {
827 char buf[4][INET6_ADDRSTRLEN];
7053cae4
RZ
828 int masklen =
829 (sockunion_family(addr) == AF_INET)
830 ? ip_masklen(mask->sin.sin_addr)
831 : ip6_masklen(mask->sin6.sin6_addr);
d62a17ae 832 zlog_debug(
5b62211d 833 "%s: ifindex %d, ifname %s, ifam_addrs {%s}, "
d62a17ae 834 "ifam_flags 0x%x, addr %s/%d broad %s dst %s "
835 "gateway %s",
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,
9df414fe
QY
978 "Routing message version different %d should be %d."
979 "This may cause problem\n",
980 rtm->rtm_version, RTM_VERSION);
d62a17ae 981
982 /* Be sure structure is cleared */
983 memset(dest, 0, sizeof(union sockunion));
984 memset(gate, 0, sizeof(union sockunion));
985 memset(mask, 0, sizeof(union sockunion));
986
987 /* We fetch each socket variable into sockunion. */
86c57af5
RZ
988 /* We fetch each socket variable into sockunion. */
989 for (maskbit = 1; maskbit; maskbit <<= 1) {
990 if ((maskbit & rtm->rtm_addrs) == 0)
991 continue;
992
993 switch (maskbit) {
994 case RTA_DST:
995 pnt += rta_get(pnt, dest, sizeof(*dest));
996 break;
997 case RTA_GATEWAY:
998 pnt += rta_get(pnt, gate, sizeof(*gate));
999 break;
1000 case RTA_NETMASK:
1583f8f5 1001 pnt += rta_getattr(pnt, mask, sizeof(*mask));
86c57af5
RZ
1002 break;
1003 case RTA_IFP:
1004 pnt += rta_getsdlname(pnt, ifname, ifnlen);
1005 break;
1006
1007 default:
1008 pnt += rta_get(pnt, NULL, 0);
1009 break;
1010 }
1011
1012 if (pnt > end) {
1013 zlog_warn("%s: overflow detected (pnt:%p end:%p)",
1014 __func__, pnt, end);
1015 break;
1016 }
1017 }
d62a17ae 1018
1019 /* If there is netmask information set it's family same as
1020 destination family*/
1021 if (rtm->rtm_addrs & RTA_NETMASK)
1022 mask->sa.sa_family = dest->sa.sa_family;
1023
1024 /* Assert read up to the end of pointer. */
1025 if (pnt != end)
9df414fe 1026 zlog_debug("rtm_read() doesn't read all socket data.");
d62a17ae 1027
1028 return rtm->rtm_flags;
718e3744 1029}
1030
d62a17ae 1031void rtm_read(struct rt_msghdr *rtm)
718e3744 1032{
d62a17ae 1033 int flags;
d7c0a89a 1034 uint8_t zebra_flags;
d62a17ae 1035 union sockunion dest, mask, gate;
1036 char ifname[INTERFACE_NAMSIZ + 1];
1037 short ifnlen = 0;
fd36be7e 1038 struct nexthop nh;
026745a2
DS
1039 struct prefix p;
1040 ifindex_t ifindex = 0;
1041 afi_t afi;
5b62211d 1042 char fbuf[64];
d62a17ae 1043
1044 zebra_flags = 0;
1045
1046 /* Read destination and netmask and gateway from rtm message
1047 structure. */
1048 flags = rtm_read_mesg(rtm, &dest, &mask, &gate, ifname, &ifnlen);
1049 if (!(flags & RTF_DONE))
1050 return;
1051 if (IS_ZEBRA_DEBUG_KERNEL)
5b62211d 1052 zlog_debug("%s: got rtm of type %d (%s) addrs {%s}", __func__,
d62a17ae 1053 rtm->rtm_type,
5b62211d
RZ
1054 lookup_msg(rtm_type_str, rtm->rtm_type, NULL),
1055 rtatostr(rtm->rtm_addrs, fbuf, sizeof(fbuf)));
d62a17ae 1056
1057#ifdef RTF_CLONED /*bsdi, netbsd 1.6*/
1058 if (flags & RTF_CLONED)
1059 return;
718e3744 1060#endif
d62a17ae 1061#ifdef RTF_WASCLONED /*freebsd*/
1062 if (flags & RTF_WASCLONED)
1063 return;
718e3744 1064#endif
1065
d62a17ae 1066 if ((rtm->rtm_type == RTM_ADD || rtm->rtm_type == RTM_CHANGE)
1067 && !(flags & RTF_UP))
1068 return;
1069
1070 /* This is connected route. */
1071 if (!(flags & RTF_GATEWAY))
1072 return;
1073
1074 if (flags & RTF_PROTO1)
1075 SET_FLAG(zebra_flags, ZEBRA_FLAG_SELFROUTE);
1076
a8309422 1077 memset(&nh, 0, sizeof(nh));
4a7371e9
DS
1078
1079 nh.vrf_id = VRF_DEFAULT;
d62a17ae 1080 /* This is a reject or blackhole route */
a8309422
DL
1081 if (flags & RTF_REJECT) {
1082 nh.type = NEXTHOP_TYPE_BLACKHOLE;
1083 nh.bh_type = BLACKHOLE_REJECT;
1084 } else if (flags & RTF_BLACKHOLE) {
1085 nh.type = NEXTHOP_TYPE_BLACKHOLE;
1086 nh.bh_type = BLACKHOLE_NULL;
1087 }
d62a17ae 1088
c626d369
DS
1089 /*
1090 * Ignore our own messages.
1091 */
1092 if (rtm->rtm_type != RTM_GET && rtm->rtm_pid == pid)
1093 return;
d62a17ae 1094
d62a17ae 1095 if (dest.sa.sa_family == AF_INET) {
026745a2 1096 afi = AFI_IP;
d62a17ae 1097 p.family = AF_INET;
1098 p.u.prefix4 = dest.sin.sin_addr;
1099 if (flags & RTF_HOST)
1100 p.prefixlen = IPV4_MAX_PREFIXLEN;
1101 else
1102 p.prefixlen = ip_masklen(mask.sin.sin_addr);
1103
a8309422
DL
1104 if (!nh.type) {
1105 nh.type = NEXTHOP_TYPE_IPV4;
1106 nh.gate.ipv4 = gate.sin.sin_addr;
1107 }
026745a2
DS
1108 } else if (dest.sa.sa_family == AF_INET6) {
1109 afi = AFI_IP6;
d62a17ae 1110 p.family = AF_INET6;
1111 p.u.prefix6 = dest.sin6.sin6_addr;
1112 if (flags & RTF_HOST)
1113 p.prefixlen = IPV6_MAX_PREFIXLEN;
1114 else
1115 p.prefixlen = ip6_masklen(mask.sin6.sin6_addr);
718e3744 1116
1117#ifdef KAME
d62a17ae 1118 if (IN6_IS_ADDR_LINKLOCAL(&gate.sin6.sin6_addr)) {
1119 ifindex = IN6_LINKLOCAL_IFINDEX(gate.sin6.sin6_addr);
1120 SET_IN6_LINKLOCAL_IFINDEX(gate.sin6.sin6_addr, 0);
1121 }
718e3744 1122#endif /* KAME */
1123
a8309422
DL
1124 if (!nh.type) {
1125 nh.type = ifindex ? NEXTHOP_TYPE_IPV6_IFINDEX
1126 : NEXTHOP_TYPE_IPV6;
1127 nh.gate.ipv6 = gate.sin6.sin6_addr;
1128 nh.ifindex = ifindex;
1129 }
026745a2
DS
1130 } else
1131 return;
d62a17ae 1132
026745a2
DS
1133 /*
1134 * CHANGE: delete the old prefix, we have no further information
1135 * to specify the route really
1136 */
1137 if (rtm->rtm_type == RTM_CHANGE)
1138 rib_delete(afi, SAFI_UNICAST, VRF_DEFAULT, ZEBRA_ROUTE_KERNEL,
1139 0, zebra_flags, &p, NULL, NULL, 0, 0, 0, true);
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,
1143 zebra_flags, &p, NULL, &nh, 0, 0, 0, 0, 0);
1144 else
1145 rib_delete(afi, SAFI_UNICAST, VRF_DEFAULT, ZEBRA_ROUTE_KERNEL,
1146 0, zebra_flags, &p, NULL, &nh, 0, 0, 0, true);
718e3744 1147}
1148
1149/* Interface function for the kernel routing table updates. Support
6621ca86 1150 * for RTM_CHANGE will be needed.
1151 * Exported only for rt_socket.c
1152 */
d62a17ae 1153int rtm_write(int message, union sockunion *dest, union sockunion *mask,
1154 union sockunion *gate, union sockunion *mpls, unsigned int index,
a8309422 1155 enum blackhole_type bh_type, int metric)
718e3744 1156{
d62a17ae 1157 int ret;
1158 caddr_t pnt;
1159 struct interface *ifp;
1160
1161 /* Sequencial number of routing message. */
1162 static int msg_seq = 0;
1163
1164 /* Struct of rt_msghdr and buffer for storing socket's data. */
1165 struct {
1166 struct rt_msghdr rtm;
1167 char buf[512];
1168 } msg;
1169
80776aec 1170 if (dplane_routing_sock < 0)
d62a17ae 1171 return ZEBRA_ERR_EPERM;
1172
1173 /* Clear and set rt_msghdr values */
1174 memset(&msg, 0, sizeof(struct rt_msghdr));
1175 msg.rtm.rtm_version = RTM_VERSION;
1176 msg.rtm.rtm_type = message;
1177 msg.rtm.rtm_seq = msg_seq++;
1178 msg.rtm.rtm_addrs = RTA_DST;
1179 msg.rtm.rtm_addrs |= RTA_GATEWAY;
1180 msg.rtm.rtm_flags = RTF_UP;
fe6c7157 1181#ifdef __OpenBSD__
d62a17ae 1182 msg.rtm.rtm_flags |= RTF_MPATH;
1183 msg.rtm.rtm_fmask = RTF_MPLS;
d3e2c74a 1184#endif
d62a17ae 1185 msg.rtm.rtm_index = index;
718e3744 1186
d62a17ae 1187 if (metric != 0) {
1188 msg.rtm.rtm_rmx.rmx_hopcount = metric;
1189 msg.rtm.rtm_inits |= RTV_HOPCOUNT;
1190 }
718e3744 1191
d62a17ae 1192 ifp = if_lookup_by_index(index, VRF_DEFAULT);
718e3744 1193
d62a17ae 1194 if (gate && (message == RTM_ADD || message == RTM_CHANGE))
1195 msg.rtm.rtm_flags |= RTF_GATEWAY;
718e3744 1196
d62a17ae 1197/* When RTF_CLONING is unavailable on BSD, should we set some
1198 * other flag instead?
1199 */
e6f148e6 1200#ifdef RTF_CLONING
d62a17ae 1201 if (!gate && (message == RTM_ADD || message == RTM_CHANGE) && ifp
1202 && (ifp->flags & IFF_POINTOPOINT) == 0)
1203 msg.rtm.rtm_flags |= RTF_CLONING;
e6f148e6 1204#endif /* RTF_CLONING */
718e3744 1205
d62a17ae 1206 /* If no protocol specific gateway is specified, use link
1207 address for gateway. */
1208 if (!gate) {
1209 if (!ifp) {
1210 char dest_buf[INET_ADDRSTRLEN] = "NULL",
1211 mask_buf[INET_ADDRSTRLEN] = "255.255.255.255";
1212 if (dest)
1213 inet_ntop(AF_INET, &dest->sin.sin_addr,
1214 dest_buf, INET_ADDRSTRLEN);
1215 if (mask)
1216 inet_ntop(AF_INET, &mask->sin.sin_addr,
1217 mask_buf, INET_ADDRSTRLEN);
9df414fe 1218 flog_warn(
e914ccbe 1219 EC_ZEBRA_RTM_NO_GATEWAY,
d62a17ae 1220 "%s: %s/%s: gate == NULL and no gateway found for ifindex %d",
1221 __func__, dest_buf, mask_buf, index);
1222 return -1;
1223 }
1224 gate = (union sockunion *)&((struct zebra_if *)ifp->info)->sdl;
1225 }
1226
1227 if (mask)
1228 msg.rtm.rtm_addrs |= RTA_NETMASK;
1229 else if (message == RTM_ADD || message == RTM_CHANGE)
1230 msg.rtm.rtm_flags |= RTF_HOST;
718e3744 1231
fe6c7157 1232#ifdef __OpenBSD__
d62a17ae 1233 if (mpls) {
1234 msg.rtm.rtm_addrs |= RTA_SRC;
1235 msg.rtm.rtm_flags |= RTF_MPLS;
1236
1237 if (mpls->smpls.smpls_label
70e98a7f 1238 != htonl(MPLS_LABEL_IMPLICIT_NULL << MPLS_LABEL_OFFSET))
d62a17ae 1239 msg.rtm.rtm_mpls = MPLS_OP_PUSH;
1240 }
d3e2c74a
RW
1241#endif
1242
d62a17ae 1243 /* Tagging route with flags */
1244 msg.rtm.rtm_flags |= (RTF_PROTO1);
718e3744 1245
a8309422
DL
1246 switch (bh_type) {
1247 case BLACKHOLE_UNSPEC:
1248 break;
1249 case BLACKHOLE_REJECT:
d62a17ae 1250 msg.rtm.rtm_flags |= RTF_REJECT;
a8309422
DL
1251 break;
1252 default:
1253 msg.rtm.rtm_flags |= RTF_BLACKHOLE;
1254 break;
1255 }
81dfcaa2 1256
718e3744 1257
d62a17ae 1258#define SOCKADDRSET(X, R) \
1259 if (msg.rtm.rtm_addrs & (R)) { \
1260 int len = SAROUNDUP(X); \
1261 memcpy(pnt, (caddr_t)(X), len); \
1262 pnt += len; \
1263 }
718e3744 1264
d62a17ae 1265 pnt = (caddr_t)msg.buf;
718e3744 1266
d62a17ae 1267 /* Write each socket data into rtm message buffer */
1268 SOCKADDRSET(dest, RTA_DST);
1269 SOCKADDRSET(gate, RTA_GATEWAY);
1270 SOCKADDRSET(mask, RTA_NETMASK);
fe6c7157 1271#ifdef __OpenBSD__
d62a17ae 1272 SOCKADDRSET(mpls, RTA_SRC);
d3e2c74a 1273#endif
718e3744 1274
d62a17ae 1275 msg.rtm.rtm_msglen = pnt - (caddr_t)&msg;
1276
80776aec 1277 ret = write(dplane_routing_sock, &msg, msg.rtm.rtm_msglen);
d62a17ae 1278
1279 if (ret != msg.rtm.rtm_msglen) {
1280 if (errno == EEXIST)
1281 return ZEBRA_ERR_RTEXIST;
1282 if (errno == ENETUNREACH)
1283 return ZEBRA_ERR_RTUNREACH;
1284 if (errno == ESRCH)
1285 return ZEBRA_ERR_RTNOEXIST;
1286
450971aa 1287 flog_err_sys(EC_LIB_SOCKET, "%s: write : %s (%d)", __func__,
9df414fe 1288 safe_strerror(errno), errno);
d62a17ae 1289 return ZEBRA_ERR_KERNEL;
1290 }
1291 return ZEBRA_ERR_NOERROR;
718e3744 1292}
1293
6b0655a2 1294
718e3744 1295#include "thread.h"
1296#include "zebra/zserv.h"
1297
718e3744 1298/* For debug purpose. */
d62a17ae 1299static void rtmsg_debug(struct rt_msghdr *rtm)
718e3744 1300{
5b62211d
RZ
1301 char fbuf[64];
1302
d62a17ae 1303 zlog_debug("Kernel: Len: %d Type: %s", rtm->rtm_msglen,
1304 lookup_msg(rtm_type_str, rtm->rtm_type, NULL));
1305 rtm_flag_dump(rtm->rtm_flags);
1306 zlog_debug("Kernel: message seq %d", rtm->rtm_seq);
5b62211d
RZ
1307 zlog_debug("Kernel: pid %lld, rtm_addrs {%s}", (long long)rtm->rtm_pid,
1308 rtatostr(rtm->rtm_addrs, fbuf, sizeof(fbuf)));
718e3744 1309}
1310
1311/* This is pretty gross, better suggestions welcome -- mhandler */
1312#ifndef RTAX_MAX
1313#ifdef RTA_NUMBITS
1314#define RTAX_MAX RTA_NUMBITS
1315#else
1316#define RTAX_MAX 8
1317#endif /* RTA_NUMBITS */
1318#endif /* RTAX_MAX */
1319
1320/* Kernel routing table and interface updates via routing socket. */
d62a17ae 1321static int kernel_read(struct thread *thread)
718e3744 1322{
d62a17ae 1323 int sock;
1324 int nbytes;
1325 struct rt_msghdr *rtm;
1326
1327 /*
1328 * This must be big enough for any message the kernel might send.
1329 * Rather than determining how many sockaddrs of what size might be
1330 * in each particular message, just use RTAX_MAX of sockaddr_storage
1331 * for each. Note that the sockaddrs must be after each message
1332 * definition, or rather after whichever happens to be the largest,
1333 * since the buffer needs to be big enough for a message and the
1334 * sockaddrs together.
1335 */
1336 union {
1337 /* Routing information. */
1338 struct {
1339 struct rt_msghdr rtm;
1340 struct sockaddr_storage addr[RTAX_MAX];
1341 } r;
1342
1343 /* Interface information. */
1344 struct {
1345 struct if_msghdr ifm;
1346 struct sockaddr_storage addr[RTAX_MAX];
1347 } im;
1348
1349 /* Interface address information. */
1350 struct {
1351 struct ifa_msghdr ifa;
1352 struct sockaddr_storage addr[RTAX_MAX];
1353 } ia;
718e3744 1354
1355#ifdef RTM_IFANNOUNCE
d62a17ae 1356 /* Interface arrival/departure */
1357 struct {
1358 struct if_announcemsghdr ifan;
1359 struct sockaddr_storage addr[RTAX_MAX];
1360 } ian;
718e3744 1361#endif /* RTM_IFANNOUNCE */
1362
d62a17ae 1363 } buf;
718e3744 1364
d62a17ae 1365 /* Fetch routing socket. */
1366 sock = THREAD_FD(thread);
718e3744 1367
d62a17ae 1368 nbytes = read(sock, &buf, sizeof buf);
718e3744 1369
d62a17ae 1370 if (nbytes <= 0) {
1371 if (nbytes < 0 && errno != EWOULDBLOCK && errno != EAGAIN)
450971aa 1372 flog_err_sys(EC_LIB_SOCKET, "routing socket error: %s",
9df414fe 1373 safe_strerror(errno));
d62a17ae 1374 return 0;
1375 }
718e3744 1376
3801e764 1377 thread_add_read(zrouter.master, kernel_read, NULL, sock, NULL);
718e3744 1378
d62a17ae 1379 if (IS_ZEBRA_DEBUG_KERNEL)
1380 rtmsg_debug(&buf.r.rtm);
718e3744 1381
d62a17ae 1382 rtm = &buf.r.rtm;
718e3744 1383
d62a17ae 1384 /*
1385 * Ensure that we didn't drop any data, so that processing routines
1386 * can assume they have the whole message.
1387 */
1388 if (rtm->rtm_msglen != nbytes) {
9df414fe 1389 zlog_debug(
d62a17ae 1390 "kernel_read: rtm->rtm_msglen %d, nbytes %d, type %d\n",
1391 rtm->rtm_msglen, nbytes, rtm->rtm_type);
1392 return -1;
1393 }
1394
1395 switch (rtm->rtm_type) {
1396 case RTM_ADD:
1397 case RTM_DELETE:
1398 case RTM_CHANGE:
1399 rtm_read(rtm);
1400 break;
1401 case RTM_IFINFO:
1402 ifm_read(&buf.im.ifm);
1403 break;
1404 case RTM_NEWADDR:
1405 case RTM_DELADDR:
1406 ifam_read(&buf.ia.ifa);
1407 break;
718e3744 1408#ifdef RTM_IFANNOUNCE
d62a17ae 1409 case RTM_IFANNOUNCE:
1410 ifan_read(&buf.ian.ifan);
1411 break;
718e3744 1412#endif /* RTM_IFANNOUNCE */
d62a17ae 1413 default:
1414 if (IS_ZEBRA_DEBUG_KERNEL)
1415 zlog_debug("Unprocessed RTM_type: %d", rtm->rtm_type);
1416 break;
1417 }
1418 return 0;
718e3744 1419}
1420
1421/* Make routing socket. */
d62a17ae 1422static void routing_socket(struct zebra_ns *zns)
718e3744 1423{
6bb30c2c
DL
1424 frr_elevate_privs(&zserv_privs) {
1425 routing_sock = ns_socket(AF_ROUTE, SOCK_RAW, 0, zns->ns_id);
80776aec
MS
1426
1427 dplane_routing_sock =
1428 ns_socket(AF_ROUTE, SOCK_RAW, 0, zns->ns_id);
6bb30c2c 1429 }
d62a17ae 1430
1431 if (routing_sock < 0) {
1c50c1c0 1432 flog_err_sys(EC_LIB_SOCKET, "Can't init kernel routing socket");
d62a17ae 1433 return;
1434 }
1435
80776aec
MS
1436 if (dplane_routing_sock < 0) {
1437 flog_err_sys(EC_LIB_SOCKET,
1438 "Can't init kernel dataplane routing socket");
1439 return;
1440 }
1441
d62a17ae 1442 /* XXX: Socket should be NONBLOCK, however as we currently
1443 * discard failed writes, this will lead to inconsistencies.
1444 * For now, socket must be blocking.
1445 */
1446 /*if (fcntl (routing_sock, F_SETFL, O_NONBLOCK) < 0)
1447 zlog_warn ("Can't set O_NONBLOCK to routing socket");*/
1448
d62a17ae 1449 /* kernel_read needs rewrite. */
3801e764 1450 thread_add_read(zrouter.master, kernel_read, NULL, routing_sock, NULL);
718e3744 1451}
1452
1453/* Exported interface function. This function simply calls
1454 routing_socket (). */
d62a17ae 1455void kernel_init(struct zebra_ns *zns)
8f7d9fc0 1456{
d62a17ae 1457 routing_socket(zns);
8f7d9fc0
FL
1458}
1459
62b8bb7a 1460void kernel_terminate(struct zebra_ns *zns, bool complete)
718e3744 1461{
d62a17ae 1462 return;
718e3744 1463}
ddfeb486
DL
1464
1465#endif /* !HAVE_NETLINK */