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