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