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