blob: 64c6cbbf2673b87c76e34d0e7d02b7ffbb18b7c1 [file] [log] [blame]
paul718e3742002-12-13 20:15:29 +00001/* 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>
Timo Teräs954c7d62016-01-15 17:36:33 +020023#include <net/if_types.h>
paul718e3742002-12-13 20:15:29 +000024
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"
pauledd7c242003-06-04 13:59:38 +000035#include "privs.h"
Feng Lu0d0686f2015-05-22 11:40:02 +020036#include "vrf.h"
paul718e3742002-12-13 20:15:29 +000037
38#include "zebra/interface.h"
39#include "zebra/zserv.h"
40#include "zebra/debug.h"
paulec1a4282005-11-24 15:15:17 +000041#include "zebra/kernel_socket.h"
Feng Lu758fb8f2014-07-03 18:23:09 +080042#include "zebra/rib.h"
paul718e3742002-12-13 20:15:29 +000043
pauledd7c242003-06-04 13:59:38 +000044extern struct zebra_privs_t zserv_privs;
paul9bcdb632003-07-08 08:09:45 +000045extern struct zebra_t zebrad;
pauledd7c242003-06-04 13:59:38 +000046
gdt4bfbea82004-01-06 01:13:05 +000047/*
Greg Troxel273b1bd2014-12-02 14:51:49 -050048 * Historically, the BSD routing socket has aligned data following a
49 * struct sockaddr to sizeof(long), which was 4 bytes on some
50 * platforms, and 8 bytes on others. NetBSD 6 changed the routing
51 * socket to align to sizeof(uint64_t), which is 8 bytes. OS X
52 * appears to align to sizeof(int), which is 4 bytes.
gdt4bfbea82004-01-06 01:13:05 +000053 *
Greg Troxel273b1bd2014-12-02 14:51:49 -050054 * Alignment of zero-sized sockaddrs is nonsensical, but historically
55 * BSD defines RT_ROUNDUP(0) to be the alignment interval (rather than
56 * 0). We follow this practice without questioning it, but it is a
57 * bug if quagga calls ROUNDUP with 0.
gdt4bfbea82004-01-06 01:13:05 +000058 */
Greg Troxel273b1bd2014-12-02 14:51:49 -050059
60/*
61 * Because of these varying conventions, the only sane approach is for
62 * the <net/route.h> header to define some flavor of ROUNDUP macro.
63 */
David Lamparter7e923222015-03-03 21:04:20 +010064
65#if defined(SA_SIZE)
66/* SAROUNDUP is the only thing we need, and SA_SIZE provides that */
67#define SAROUNDUP(a) SA_SIZE(a)
68#else /* !SA_SIZE */
69
Greg Troxel273b1bd2014-12-02 14:51:49 -050070#if defined(RT_ROUNDUP)
71#define ROUNDUP(a) RT_ROUNDUP(a)
72#endif /* defined(RT_ROUNDUP) */
73
David Lampartera2b50312015-09-15 19:35:41 -070074#if defined(SUNOS_5)
75/* Solaris has struct sockaddr_in[6] definitions at 16 / 32 bytes size,
76 * so the whole concept doesn't really apply. */
77#define ROUNDUP(a) (a)
78#endif
79
Greg Troxel273b1bd2014-12-02 14:51:49 -050080/*
81 * If ROUNDUP has not yet been defined in terms of platform-provided
82 * defines, attempt to cope with heuristics.
83 */
84#if !defined(ROUNDUP)
85
86/*
87 * It's a bug for a platform not to define rounding/alignment for
88 * sockaddrs on the routing socket. This warning really is
89 * intentional, to provoke filing bug reports with operating systems
90 * that don't define RT_ROUNDUP or equivalent.
91 */
92#warning "net/route.h does not define RT_ROUNDUP; making unwarranted assumptions!"
93
94/* OS X (Xcode as of 2014-12) is known not to define RT_ROUNDUP */
Doug VanLeuven3b33de62012-10-10 22:10:14 +000095#ifdef __APPLE__
Greg Troxel273b1bd2014-12-02 14:51:49 -050096#define ROUNDUP_TYPE int
Greg Troxel941789e2015-03-23 15:16:29 -040097#else
98#define ROUNDUP_TYPE long
Doug VanLeuven3b33de62012-10-10 22:10:14 +000099#endif
paul718e3742002-12-13 20:15:29 +0000100
Greg Troxel273b1bd2014-12-02 14:51:49 -0500101#define ROUNDUP(a) \
102 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(ROUNDUP_TYPE) - 1))) : sizeof(ROUNDUP_TYPE))
103
104#endif /* defined(ROUNDUP) */
105
gdt4bfbea82004-01-06 01:13:05 +0000106/*
107 * Given a pointer (sockaddr or void *), return the number of bytes
108 * taken up by the sockaddr and any padding needed for alignment.
109 */
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000110#if defined(HAVE_STRUCT_SOCKADDR_SA_LEN)
gdt4bfbea82004-01-06 01:13:05 +0000111#define SAROUNDUP(X) ROUNDUP(((struct sockaddr *)(X))->sa_len)
paul30be8022003-10-22 02:51:38 +0000112#elif defined(HAVE_IPV6)
gdt4bfbea82004-01-06 01:13:05 +0000113/*
114 * One would hope all fixed-size structure definitions are aligned,
115 * but round them up nonetheless.
116 */
117#define SAROUNDUP(X) \
paul3e95a072003-09-24 00:05:45 +0000118 (((struct sockaddr *)(X))->sa_family == AF_INET ? \
119 ROUNDUP(sizeof(struct sockaddr_in)):\
120 (((struct sockaddr *)(X))->sa_family == AF_INET6 ? \
121 ROUNDUP(sizeof(struct sockaddr_in6)) : \
122 (((struct sockaddr *)(X))->sa_family == AF_LINK ? \
paulc50ae8b2004-05-11 11:31:07 +0000123 ROUNDUP(sizeof(struct sockaddr_dl)) : sizeof(struct sockaddr))))
paul30be8022003-10-22 02:51:38 +0000124#else /* HAVE_IPV6 */
gdt4bfbea82004-01-06 01:13:05 +0000125#define SAROUNDUP(X) \
paul30be8022003-10-22 02:51:38 +0000126 (((struct sockaddr *)(X))->sa_family == AF_INET ? \
127 ROUNDUP(sizeof(struct sockaddr_in)):\
128 (((struct sockaddr *)(X))->sa_family == AF_LINK ? \
129 ROUNDUP(sizeof(struct sockaddr_dl)) : sizeof(struct sockaddr)))
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000130#endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
paul718e3742002-12-13 20:15:29 +0000131
David Lamparter7e923222015-03-03 21:04:20 +0100132#endif /* !SA_SIZE */
133
Doug VanLeuvena05df8f2012-10-10 16:11:36 -0700134/*
135 * We use a call to an inline function to copy (PNT) to (DEST)
136 * 1. Calculating the length of the copy requires an #ifdef to determine
137 * if sa_len is a field and can't be used directly inside a #define
138 * 2. So the compiler doesn't complain when DEST is NULL, which is only true
139 * when we are skipping the copy and incrementing to the next SA
paulec1a4282005-11-24 15:15:17 +0000140 */
David Lamparter3e9e2c92015-04-10 09:14:58 +0200141static inline void
Doug VanLeuvena05df8f2012-10-10 16:11:36 -0700142rta_copy (union sockunion *dest, caddr_t src) {
143 int len;
David Lamparterda1b7ea2015-09-15 20:36:20 -0700144 if (!dest)
145 return;
Doug VanLeuvena05df8f2012-10-10 16:11:36 -0700146#ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
147 len = (((struct sockaddr *)src)->sa_len > sizeof (*dest)) ?
148 sizeof (*dest) : ((struct sockaddr *)src)->sa_len ;
149#else
150 len = (SAROUNDUP (src) > sizeof (*dest)) ?
151 sizeof (*dest) : SAROUNDUP (src) ;
152#endif
153 memcpy (dest, src, len);
154}
155
paul62debbb2005-06-14 14:07:07 +0000156#define RTA_ADDR_GET(DEST, RTA, RTMADDRS, PNT) \
157 if ((RTMADDRS) & (RTA)) \
158 { \
159 int len = SAROUNDUP ((PNT)); \
David Lamparterda1b7ea2015-09-15 20:36:20 -0700160 if (af_check (((struct sockaddr *)(PNT))->sa_family)) \
Doug VanLeuvena05df8f2012-10-10 16:11:36 -0700161 rta_copy((DEST), (PNT)); \
paul62debbb2005-06-14 14:07:07 +0000162 (PNT) += len; \
163 }
164#define RTA_ATTR_GET(DEST, RTA, RTMADDRS, PNT) \
165 if ((RTMADDRS) & (RTA)) \
166 { \
167 int len = SAROUNDUP ((PNT)); \
David Lamparterda1b7ea2015-09-15 20:36:20 -0700168 rta_copy((DEST), (PNT)); \
paul62debbb2005-06-14 14:07:07 +0000169 (PNT) += len; \
170 }
171
paul6fe70d12005-11-12 22:55:10 +0000172#define RTA_NAME_GET(DEST, RTA, RTMADDRS, PNT, LEN) \
173 if ((RTMADDRS) & (RTA)) \
174 { \
paulec1a4282005-11-24 15:15:17 +0000175 u_char *pdest = (u_char *) (DEST); \
paul6fe70d12005-11-12 22:55:10 +0000176 int len = SAROUNDUP ((PNT)); \
177 struct sockaddr_dl *sdl = (struct sockaddr_dl *)(PNT); \
178 if (IS_ZEBRA_DEBUG_KERNEL) \
179 zlog_debug ("%s: RTA_SDL_GET nlen %d, alen %d", \
180 __func__, sdl->sdl_nlen, sdl->sdl_alen); \
181 if ( ((DEST) != NULL) && (sdl->sdl_family == AF_LINK) \
182 && (sdl->sdl_nlen < IFNAMSIZ) && (sdl->sdl_nlen <= len) ) \
183 { \
paulec1a4282005-11-24 15:15:17 +0000184 memcpy (pdest, sdl->sdl_data, sdl->sdl_nlen); \
185 pdest[sdl->sdl_nlen] = '\0'; \
paul6fe70d12005-11-12 22:55:10 +0000186 (LEN) = sdl->sdl_nlen; \
187 } \
188 (PNT) += len; \
189 } \
190 else \
191 { \
192 (LEN) = 0; \
193 }
paul718e3742002-12-13 20:15:29 +0000194/* Routing socket message types. */
Stephen Hemminger1423c802008-08-14 17:59:25 +0100195const struct message rtm_type_str[] =
paul718e3742002-12-13 20:15:29 +0000196{
197 {RTM_ADD, "RTM_ADD"},
198 {RTM_DELETE, "RTM_DELETE"},
199 {RTM_CHANGE, "RTM_CHANGE"},
200 {RTM_GET, "RTM_GET"},
201 {RTM_LOSING, "RTM_LOSING"},
202 {RTM_REDIRECT, "RTM_REDIRECT"},
203 {RTM_MISS, "RTM_MISS"},
204 {RTM_LOCK, "RTM_LOCK"},
Greg Troxel9458b812006-09-13 12:13:08 +0000205#ifdef OLDADD
paul718e3742002-12-13 20:15:29 +0000206 {RTM_OLDADD, "RTM_OLDADD"},
Greg Troxel9458b812006-09-13 12:13:08 +0000207#endif /* RTM_OLDADD */
208#ifdef RTM_OLDDEL
paul718e3742002-12-13 20:15:29 +0000209 {RTM_OLDDEL, "RTM_OLDDEL"},
Greg Troxel9458b812006-09-13 12:13:08 +0000210#endif /* RTM_OLDDEL */
paul718e3742002-12-13 20:15:29 +0000211 {RTM_RESOLVE, "RTM_RESOLVE"},
212 {RTM_NEWADDR, "RTM_NEWADDR"},
213 {RTM_DELADDR, "RTM_DELADDR"},
214 {RTM_IFINFO, "RTM_IFINFO"},
215#ifdef RTM_OIFINFO
216 {RTM_OIFINFO, "RTM_OIFINFO"},
217#endif /* RTM_OIFINFO */
218#ifdef RTM_NEWMADDR
219 {RTM_NEWMADDR, "RTM_NEWMADDR"},
220#endif /* RTM_NEWMADDR */
221#ifdef RTM_DELMADDR
222 {RTM_DELMADDR, "RTM_DELMADDR"},
223#endif /* RTM_DELMADDR */
224#ifdef RTM_IFANNOUNCE
225 {RTM_IFANNOUNCE, "RTM_IFANNOUNCE"},
226#endif /* RTM_IFANNOUNCE */
227 {0, NULL}
228};
229
Stephen Hemmingerce0db9c2009-05-15 10:47:04 -0700230static const struct message rtm_flag_str[] =
paul718e3742002-12-13 20:15:29 +0000231{
232 {RTF_UP, "UP"},
233 {RTF_GATEWAY, "GATEWAY"},
234 {RTF_HOST, "HOST"},
235 {RTF_REJECT, "REJECT"},
236 {RTF_DYNAMIC, "DYNAMIC"},
237 {RTF_MODIFIED, "MODIFIED"},
238 {RTF_DONE, "DONE"},
239#ifdef RTF_MASK
240 {RTF_MASK, "MASK"},
241#endif /* RTF_MASK */
David Warde6f148e2009-12-03 21:43:11 +0300242#ifdef RTF_CLONING
paul718e3742002-12-13 20:15:29 +0000243 {RTF_CLONING, "CLONING"},
David Warde6f148e2009-12-03 21:43:11 +0300244#endif /* RTF_CLONING */
Renato Westphala64c66b2017-01-06 16:30:03 +0700245#ifdef RTF_XRESOLVE
paul718e3742002-12-13 20:15:29 +0000246 {RTF_XRESOLVE, "XRESOLVE"},
Renato Westphala64c66b2017-01-06 16:30:03 +0700247#endif /* RTF_XRESOLVE */
Lou Bergerb05c6ca2016-01-12 13:41:44 -0500248#ifdef RTF_LLINFO
paul718e3742002-12-13 20:15:29 +0000249 {RTF_LLINFO, "LLINFO"},
Lou Bergerb05c6ca2016-01-12 13:41:44 -0500250#endif /* RTF_LLINFO */
paul718e3742002-12-13 20:15:29 +0000251 {RTF_STATIC, "STATIC"},
252 {RTF_BLACKHOLE, "BLACKHOLE"},
paul6fe70d12005-11-12 22:55:10 +0000253#ifdef RTF_PRIVATE
254 {RTF_PRIVATE, "PRIVATE"},
255#endif /* RTF_PRIVATE */
paul718e3742002-12-13 20:15:29 +0000256 {RTF_PROTO1, "PROTO1"},
257 {RTF_PROTO2, "PROTO2"},
258#ifdef RTF_PRCLONING
259 {RTF_PRCLONING, "PRCLONING"},
260#endif /* RTF_PRCLONING */
261#ifdef RTF_WASCLONED
262 {RTF_WASCLONED, "WASCLONED"},
263#endif /* RTF_WASCLONED */
264#ifdef RTF_PROTO3
265 {RTF_PROTO3, "PROTO3"},
266#endif /* RTF_PROTO3 */
267#ifdef RTF_PINNED
268 {RTF_PINNED, "PINNED"},
269#endif /* RTF_PINNED */
270#ifdef RTF_LOCAL
271 {RTF_LOCAL, "LOCAL"},
272#endif /* RTF_LOCAL */
273#ifdef RTF_BROADCAST
274 {RTF_BROADCAST, "BROADCAST"},
275#endif /* RTF_BROADCAST */
276#ifdef RTF_MULTICAST
277 {RTF_MULTICAST, "MULTICAST"},
278#endif /* RTF_MULTICAST */
paul6fe70d12005-11-12 22:55:10 +0000279#ifdef RTF_MULTIRT
280 {RTF_MULTIRT, "MULTIRT"},
281#endif /* RTF_MULTIRT */
282#ifdef RTF_SETSRC
283 {RTF_SETSRC, "SETSRC"},
284#endif /* RTF_SETSRC */
paul718e3742002-12-13 20:15:29 +0000285 {0, NULL}
286};
287
288/* Kernel routing update socket. */
289int routing_sock = -1;
290
291/* Yes I'm checking ugly routing socket behavior. */
292/* #define DEBUG */
293
294/* Supported address family check. */
David Lamparter3e9e2c92015-04-10 09:14:58 +0200295static inline int
paul718e3742002-12-13 20:15:29 +0000296af_check (int family)
297{
298 if (family == AF_INET)
299 return 1;
300#ifdef HAVE_IPV6
301 if (family == AF_INET6)
302 return 1;
303#endif /* HAVE_IPV6 */
304 return 0;
305}
David Lamparter6b0655a2014-06-04 06:53:35 +0200306
paul718e3742002-12-13 20:15:29 +0000307/* Dump routing table flag for debug purpose. */
ajsb6178002004-12-07 21:12:56 +0000308static void
paul718e3742002-12-13 20:15:29 +0000309rtm_flag_dump (int flag)
310{
Tom Goff80b2a942009-12-03 14:53:15 +0300311 const struct message *mes;
paul718e3742002-12-13 20:15:29 +0000312 static char buf[BUFSIZ];
313
gdtcced60d2004-07-13 16:45:54 +0000314 buf[0] = '\0';
paul718e3742002-12-13 20:15:29 +0000315 for (mes = rtm_flag_str; mes->key != 0; mes++)
316 {
317 if (mes->key & flag)
318 {
319 strlcat (buf, mes->str, BUFSIZ);
320 strlcat (buf, " ", BUFSIZ);
321 }
322 }
ajsb6178002004-12-07 21:12:56 +0000323 zlog_debug ("Kernel: %s", buf);
paul718e3742002-12-13 20:15:29 +0000324}
325
326#ifdef RTM_IFANNOUNCE
327/* Interface adding function */
paul6621ca82005-11-23 13:02:08 +0000328static int
paul718e3742002-12-13 20:15:29 +0000329ifan_read (struct if_announcemsghdr *ifan)
330{
331 struct interface *ifp;
paul6fe70d12005-11-12 22:55:10 +0000332
paul718e3742002-12-13 20:15:29 +0000333 ifp = if_lookup_by_index (ifan->ifan_index);
paul6fe70d12005-11-12 22:55:10 +0000334
335 if (ifp)
336 assert ( (ifp->ifindex == ifan->ifan_index)
337 || (ifp->ifindex == IFINDEX_INTERNAL) );
338
paulec1a4282005-11-24 15:15:17 +0000339 if ( (ifp == NULL)
340 || ((ifp->ifindex == IFINDEX_INTERNAL)
341 && (ifan->ifan_what == IFAN_ARRIVAL)) )
paul718e3742002-12-13 20:15:29 +0000342 {
paul6fe70d12005-11-12 22:55:10 +0000343 if (IS_ZEBRA_DEBUG_KERNEL)
344 zlog_debug ("%s: creating interface for ifindex %d, name %s",
345 __func__, ifan->ifan_index, ifan->ifan_name);
346
paul718e3742002-12-13 20:15:29 +0000347 /* Create Interface */
ajs08dbfb62005-04-03 03:40:52 +0000348 ifp = if_get_by_name_len(ifan->ifan_name,
349 strnlen(ifan->ifan_name,
350 sizeof(ifan->ifan_name)));
paul718e3742002-12-13 20:15:29 +0000351 ifp->ifindex = ifan->ifan_index;
352
Ingo Flaschberger1db65fa2011-04-17 18:28:20 +0000353 if_get_metric (ifp);
paul718e3742002-12-13 20:15:29 +0000354 if_add_update (ifp);
355 }
356 else if (ifp != NULL && ifan->ifan_what == IFAN_DEPARTURE)
paul6eb88272005-07-29 14:36:00 +0000357 if_delete_update (ifp);
paul718e3742002-12-13 20:15:29 +0000358
359 if_get_flags (ifp);
360 if_get_mtu (ifp);
361 if_get_metric (ifp);
362
363 if (IS_ZEBRA_DEBUG_KERNEL)
paul6fe70d12005-11-12 22:55:10 +0000364 zlog_debug ("%s: interface %s index %d",
365 __func__, ifan->ifan_name, ifan->ifan_index);
paul718e3742002-12-13 20:15:29 +0000366
367 return 0;
368}
369#endif /* RTM_IFANNOUNCE */
370
Doug VanLeuven9234b382012-10-10 16:12:32 -0700371#ifdef HAVE_BSD_IFI_LINK_STATE
Andrew J. Schorrc543a172008-01-10 15:24:32 +0000372/* BSD link detect translation */
373static void
374bsd_linkdetect_translate (struct if_msghdr *ifm)
375{
Andrew J. Schorr55edb0d2008-01-11 15:57:13 +0000376 if ((ifm->ifm_data.ifi_link_state >= LINK_STATE_UP) ||
377 (ifm->ifm_data.ifi_link_state == LINK_STATE_UNKNOWN))
Andrew J. Schorrc543a172008-01-10 15:24:32 +0000378 SET_FLAG(ifm->ifm_flags, IFF_RUNNING);
379 else
380 UNSET_FLAG(ifm->ifm_flags, IFF_RUNNING);
381}
Doug VanLeuven9234b382012-10-10 16:12:32 -0700382#endif /* HAVE_BSD_IFI_LINK_STATE */
Andrew J. Schorrc543a172008-01-10 15:24:32 +0000383
Timo Teräs954c7d62016-01-15 17:36:33 +0200384static enum zebra_link_type
385sdl_to_zebra_link_type (unsigned int sdlt)
386{
387 switch (sdlt)
388 {
389 case IFT_ETHER: return ZEBRA_LLT_ETHER;
390 case IFT_X25: return ZEBRA_LLT_X25;
391 case IFT_FDDI: return ZEBRA_LLT_FDDI;
392 case IFT_PPP: return ZEBRA_LLT_PPP;
393 case IFT_LOOP: return ZEBRA_LLT_LOOPBACK;
394 case IFT_SLIP: return ZEBRA_LLT_SLIP;
395 case IFT_ARCNET: return ZEBRA_LLT_ARCNET;
396 case IFT_ATM: return ZEBRA_LLT_ATM;
397 case IFT_LOCALTALK: return ZEBRA_LLT_LOCALTLK;
398 case IFT_HIPPI: return ZEBRA_LLT_HIPPI;
399#ifdef IFT_IEEE1394
400 case IFT_IEEE1394: return ZEBRA_LLT_IEEE1394;
401#endif
402
403 default: return ZEBRA_LLT_UNKNOWN;
404 }
405}
406
gdtda26e3b2004-01-05 17:20:59 +0000407/*
408 * Handle struct if_msghdr obtained from reading routing socket or
409 * sysctl (from interface_list). There may or may not be sockaddrs
410 * present after the header.
411 */
paulec1a4282005-11-24 15:15:17 +0000412int
paul718e3742002-12-13 20:15:29 +0000413ifm_read (struct if_msghdr *ifm)
414{
paul3e95a072003-09-24 00:05:45 +0000415 struct interface *ifp = NULL;
Tom Goffa34eb362009-11-25 20:36:06 +0000416 struct sockaddr_dl *sdl;
paul6fe70d12005-11-12 22:55:10 +0000417 char ifname[IFNAMSIZ];
418 short ifnlen = 0;
Doug VanLeuvena05df8f2012-10-10 16:11:36 -0700419 caddr_t cp;
paul6fe70d12005-11-12 22:55:10 +0000420
421 /* terminate ifname at head (for strnlen) and tail (for safety) */
422 ifname[IFNAMSIZ - 1] = '\0';
423
gdtda26e3b2004-01-05 17:20:59 +0000424 /* paranoia: sanity check structure */
425 if (ifm->ifm_msglen < sizeof(struct if_msghdr))
426 {
427 zlog_err ("ifm_read: ifm->ifm_msglen %d too short\n",
428 ifm->ifm_msglen);
429 return -1;
430 }
431
432 /*
gdt4bfbea82004-01-06 01:13:05 +0000433 * Check for a sockaddr_dl following the message. First, point to
434 * where a socakddr might be if one follows the message.
gdtda26e3b2004-01-05 17:20:59 +0000435 */
gdt4bfbea82004-01-06 01:13:05 +0000436 cp = (void *)(ifm + 1);
437
paul3e95a072003-09-24 00:05:45 +0000438#ifdef SUNOS_5
paul3e95a072003-09-24 00:05:45 +0000439 /*
gdt4bfbea82004-01-06 01:13:05 +0000440 * XXX This behavior should be narrowed to only the kernel versions
441 * for which the structures returned do not match the headers.
442 *
paul3e95a072003-09-24 00:05:45 +0000443 * if_msghdr_t on 64 bit kernels in Solaris 9 and earlier versions
gdt4bfbea82004-01-06 01:13:05 +0000444 * is 12 bytes larger than the 32 bit version.
paul3e95a072003-09-24 00:05:45 +0000445 */
gdt4bfbea82004-01-06 01:13:05 +0000446 if (((struct sockaddr *) cp)->sa_family == AF_UNSPEC)
paul3e95a072003-09-24 00:05:45 +0000447 cp = cp + 12;
paul3e95a072003-09-24 00:05:45 +0000448#endif
paul718e3742002-12-13 20:15:29 +0000449
paul6fe70d12005-11-12 22:55:10 +0000450 RTA_ADDR_GET (NULL, RTA_DST, ifm->ifm_addrs, cp);
451 RTA_ADDR_GET (NULL, RTA_GATEWAY, ifm->ifm_addrs, cp);
452 RTA_ATTR_GET (NULL, RTA_NETMASK, ifm->ifm_addrs, cp);
453 RTA_ADDR_GET (NULL, RTA_GENMASK, ifm->ifm_addrs, cp);
Tom Goffa34eb362009-11-25 20:36:06 +0000454 sdl = (struct sockaddr_dl *)cp;
paul6fe70d12005-11-12 22:55:10 +0000455 RTA_NAME_GET (ifname, RTA_IFP, ifm->ifm_addrs, cp, ifnlen);
456 RTA_ADDR_GET (NULL, RTA_IFA, ifm->ifm_addrs, cp);
457 RTA_ADDR_GET (NULL, RTA_AUTHOR, ifm->ifm_addrs, cp);
458 RTA_ADDR_GET (NULL, RTA_BRD, ifm->ifm_addrs, cp);
459
460 if (IS_ZEBRA_DEBUG_KERNEL)
461 zlog_debug ("%s: sdl ifname %s", __func__, (ifnlen ? ifname : "(nil)"));
462
gdt4bfbea82004-01-06 01:13:05 +0000463 /*
paul6fe70d12005-11-12 22:55:10 +0000464 * Look up on ifindex first, because ifindices are the primary handle for
465 * interfaces across the user/kernel boundary, for most systems. (Some
466 * messages, such as up/down status changes on NetBSD, do not include a
467 * sockaddr_dl).
gdt4bfbea82004-01-06 01:13:05 +0000468 */
paul6fe70d12005-11-12 22:55:10 +0000469 if ( (ifp = if_lookup_by_index (ifm->ifm_index)) != NULL )
gdt4bfbea82004-01-06 01:13:05 +0000470 {
paul6fe70d12005-11-12 22:55:10 +0000471 /* we have an ifp, verify that the name matches as some systems,
472 * eg Solaris, have a 1:many association of ifindex:ifname
473 * if they dont match, we dont have the correct ifp and should
474 * set it back to NULL to let next check do lookup by name
475 */
476 if (ifnlen && (strncmp (ifp->name, ifname, IFNAMSIZ) != 0) )
gdt4bfbea82004-01-06 01:13:05 +0000477 {
paul6fe70d12005-11-12 22:55:10 +0000478 if (IS_ZEBRA_DEBUG_KERNEL)
479 zlog_debug ("%s: ifp name %s doesnt match sdl name %s",
480 __func__, ifp->name, ifname);
481 ifp = NULL;
gdt4bfbea82004-01-06 01:13:05 +0000482 }
483 }
paul6fe70d12005-11-12 22:55:10 +0000484
gdtda26e3b2004-01-05 17:20:59 +0000485 /*
paul6fe70d12005-11-12 22:55:10 +0000486 * If we dont have an ifp, try looking up by name. Particularly as some
487 * systems (Solaris) have a 1:many mapping of ifindex:ifname - the ifname
488 * is therefore our unique handle to that interface.
489 *
490 * Interfaces specified in the configuration file for which the ifindex
491 * has not been determined will have ifindex == IFINDEX_INTERNAL, and such
492 * interfaces are found by this search, and then their ifindex values can
493 * be filled in.
gdtda26e3b2004-01-05 17:20:59 +0000494 */
paul6fe70d12005-11-12 22:55:10 +0000495 if ( (ifp == NULL) && ifnlen)
496 ifp = if_lookup_by_name (ifname);
paul718e3742002-12-13 20:15:29 +0000497
gdtda26e3b2004-01-05 17:20:59 +0000498 /*
paul6fe70d12005-11-12 22:55:10 +0000499 * If ifp still does not exist or has an invalid index (IFINDEX_INTERNAL),
500 * create or fill in an interface.
gdtda26e3b2004-01-05 17:20:59 +0000501 */
ajsd2fc8892005-04-02 18:38:43 +0000502 if ((ifp == NULL) || (ifp->ifindex == IFINDEX_INTERNAL))
paul718e3742002-12-13 20:15:29 +0000503 {
gdt4bfbea82004-01-06 01:13:05 +0000504 /*
505 * To create or fill in an interface, a sockaddr_dl (via
506 * RTA_IFP) is required.
507 */
paul6fe70d12005-11-12 22:55:10 +0000508 if (!ifnlen)
paul718e3742002-12-13 20:15:29 +0000509 {
paul6fe70d12005-11-12 22:55:10 +0000510 zlog_warn ("Interface index %d (new) missing ifname\n",
paul718e3742002-12-13 20:15:29 +0000511 ifm->ifm_index);
512 return -1;
513 }
paul5c78b3d2006-01-25 04:31:40 +0000514
515#ifndef RTM_IFANNOUNCE
516 /* Down->Down interface should be ignored here.
517 * See further comment below.
518 */
519 if (!CHECK_FLAG (ifm->ifm_flags, IFF_UP))
520 return 0;
521#endif /* !RTM_IFANNOUNCE */
paul6fe70d12005-11-12 22:55:10 +0000522
paul3e95a072003-09-24 00:05:45 +0000523 if (ifp == NULL)
paul6fe70d12005-11-12 22:55:10 +0000524 {
525 /* Interface that zebra was not previously aware of, so create. */
526 ifp = if_create (ifname, ifnlen);
527 if (IS_ZEBRA_DEBUG_KERNEL)
528 zlog_debug ("%s: creating ifp for ifindex %d",
529 __func__, ifm->ifm_index);
530 }
paul718e3742002-12-13 20:15:29 +0000531
paul6fe70d12005-11-12 22:55:10 +0000532 if (IS_ZEBRA_DEBUG_KERNEL)
533 zlog_debug ("%s: updated/created ifp, ifname %s, ifindex %d",
534 __func__, ifp->name, ifp->ifindex);
gdt4bfbea82004-01-06 01:13:05 +0000535 /*
536 * Fill in newly created interface structure, or larval
ajsd2fc8892005-04-02 18:38:43 +0000537 * structure with ifindex IFINDEX_INTERNAL.
gdt4bfbea82004-01-06 01:13:05 +0000538 */
paul718e3742002-12-13 20:15:29 +0000539 ifp->ifindex = ifm->ifm_index;
Andrew J. Schorrc543a172008-01-10 15:24:32 +0000540
Doug VanLeuven9234b382012-10-10 16:12:32 -0700541#ifdef HAVE_BSD_IFI_LINK_STATE /* translate BSD kernel msg for link-state */
Andrew J. Schorrc543a172008-01-10 15:24:32 +0000542 bsd_linkdetect_translate(ifm);
Doug VanLeuven9234b382012-10-10 16:12:32 -0700543#endif /* HAVE_BSD_IFI_LINK_STATE */
Andrew J. Schorrc543a172008-01-10 15:24:32 +0000544
paul5c78b3d2006-01-25 04:31:40 +0000545 if_flags_update (ifp, ifm->ifm_flags);
paul718e3742002-12-13 20:15:29 +0000546#if defined(__bsdi__)
547 if_kvm_get_mtu (ifp);
548#else
549 if_get_mtu (ifp);
550#endif /* __bsdi__ */
551 if_get_metric (ifp);
552
Tom Goffa34eb362009-11-25 20:36:06 +0000553 /*
554 * XXX sockaddr_dl contents can be larger than the structure
David Lamparterca3ccd82012-09-26 14:52:39 +0200555 * definition. There are 2 big families here:
556 * - BSD has sdl_len + sdl_data[16] + overruns sdl_data
557 * we MUST use sdl_len here or we'll truncate data.
558 * - Solaris has no sdl_len, but sdl_data[244]
559 * presumably, it's not going to run past that, so sizeof()
560 * is fine here.
Tom Goffa34eb362009-11-25 20:36:06 +0000561 * a nonzero ifnlen from RTA_NAME_GET() means sdl is valid
562 */
Timo Teräs954c7d62016-01-15 17:36:33 +0200563 ifp->ll_type = ZEBRA_LLT_UNKNOWN;
564 ifp->hw_addr_len = 0;
Tom Goffa34eb362009-11-25 20:36:06 +0000565 if (ifnlen)
Timo Teräs954c7d62016-01-15 17:36:33 +0200566 {
David Lamparterca3ccd82012-09-26 14:52:39 +0200567#ifdef HAVE_STRUCT_SOCKADDR_DL_SDL_LEN
Timo Teräs954c7d62016-01-15 17:36:33 +0200568 memcpy (&((struct zebra_if *)ifp->info)->sdl, sdl, sdl->sdl_len);
David Lamparterca3ccd82012-09-26 14:52:39 +0200569#else
Timo Teräs954c7d62016-01-15 17:36:33 +0200570 memcpy (&((struct zebra_if *)ifp->info)->sdl, sdl, sizeof (struct sockaddr_dl));
David Lamparterca3ccd82012-09-26 14:52:39 +0200571#endif /* HAVE_STRUCT_SOCKADDR_DL_SDL_LEN */
Timo Teräs954c7d62016-01-15 17:36:33 +0200572
573 ifp->ll_type = sdl_to_zebra_link_type (sdl->sdl_type);
574 if (sdl->sdl_alen <= sizeof(ifp->hw_addr))
575 {
576 memcpy (ifp->hw_addr, LLADDR(sdl), sdl->sdl_alen);
577 ifp->hw_addr_len = sdl->sdl_alen;
578 }
579 }
Tom Goffa34eb362009-11-25 20:36:06 +0000580
paul718e3742002-12-13 20:15:29 +0000581 if_add_update (ifp);
582 }
583 else
gdtda26e3b2004-01-05 17:20:59 +0000584 /*
585 * Interface structure exists. Adjust stored flags from
586 * notification. If interface has up->down or down->up
587 * transition, call state change routines (to adjust routes,
588 * notify routing daemons, etc.). (Other flag changes are stored
589 * but apparently do not trigger action.)
590 */
paul718e3742002-12-13 20:15:29 +0000591 {
paul6fe70d12005-11-12 22:55:10 +0000592 if (ifp->ifindex != ifm->ifm_index)
593 {
594 zlog_warn ("%s: index mismatch, ifname %s, ifp index %d, "
595 "ifm index %d",
596 __func__, ifp->name, ifp->ifindex, ifm->ifm_index);
597 return -1;
598 }
599
Doug VanLeuven9234b382012-10-10 16:12:32 -0700600#ifdef HAVE_BSD_IFI_LINK_STATE /* translate BSD kernel msg for link-state */
Andrew J. Schorrc543a172008-01-10 15:24:32 +0000601 bsd_linkdetect_translate(ifm);
Doug VanLeuven9234b382012-10-10 16:12:32 -0700602#endif /* HAVE_BSD_IFI_LINK_STATE */
Andrew J. Schorrc543a172008-01-10 15:24:32 +0000603
paul5c78b3d2006-01-25 04:31:40 +0000604 /* update flags and handle operative->inoperative transition, if any */
605 if_flags_update (ifp, ifm->ifm_flags);
606
paul6eb88272005-07-29 14:36:00 +0000607#ifndef RTM_IFANNOUNCE
paul5c78b3d2006-01-25 04:31:40 +0000608 if (!if_is_up (ifp))
609 {
610 /* No RTM_IFANNOUNCE on this platform, so we can never
611 * distinguish between ~IFF_UP and delete. We must presume
612 * it has been deleted.
613 * Eg, Solaris will not notify us of unplumb.
614 *
615 * XXX: Fixme - this should be runtime detected
616 * So that a binary compiled on a system with IFANNOUNCE
617 * will still behave correctly if run on a platform without
618 */
619 if_delete_update (ifp);
620 }
paul6eb88272005-07-29 14:36:00 +0000621#endif /* RTM_IFANNOUNCE */
Denis Ovsienko1ba27562007-08-21 16:15:39 +0000622 if (if_is_up (ifp))
623 {
624#if defined(__bsdi__)
625 if_kvm_get_mtu (ifp);
626#else
627 if_get_mtu (ifp);
628#endif /* __bsdi__ */
629 if_get_metric (ifp);
630 }
paul718e3742002-12-13 20:15:29 +0000631 }
paul5c78b3d2006-01-25 04:31:40 +0000632
paul718e3742002-12-13 20:15:29 +0000633#ifdef HAVE_NET_RT_IFLIST
634 ifp->stats = ifm->ifm_data;
635#endif /* HAVE_NET_RT_IFLIST */
636
637 if (IS_ZEBRA_DEBUG_KERNEL)
paul6fe70d12005-11-12 22:55:10 +0000638 zlog_debug ("%s: interface %s index %d",
639 __func__, ifp->name, ifp->ifindex);
paul718e3742002-12-13 20:15:29 +0000640
641 return 0;
642}
David Lamparter6b0655a2014-06-04 06:53:35 +0200643
paul718e3742002-12-13 20:15:29 +0000644/* Address read from struct ifa_msghdr. */
paul6621ca82005-11-23 13:02:08 +0000645static void
paul718e3742002-12-13 20:15:29 +0000646ifam_read_mesg (struct ifa_msghdr *ifm,
647 union sockunion *addr,
648 union sockunion *mask,
paul6fe70d12005-11-12 22:55:10 +0000649 union sockunion *brd,
650 char *ifname,
651 short *ifnlen)
paul718e3742002-12-13 20:15:29 +0000652{
653 caddr_t pnt, end;
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000654 union sockunion dst;
655 union sockunion gateway;
paul718e3742002-12-13 20:15:29 +0000656
657 pnt = (caddr_t)(ifm + 1);
658 end = ((caddr_t)ifm) + ifm->ifam_msglen;
659
paul718e3742002-12-13 20:15:29 +0000660 /* Be sure structure is cleared */
661 memset (mask, 0, sizeof (union sockunion));
662 memset (addr, 0, sizeof (union sockunion));
paul6621ca82005-11-23 13:02:08 +0000663 memset (brd, 0, sizeof (union sockunion));
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000664 memset (&dst, 0, sizeof (union sockunion));
665 memset (&gateway, 0, sizeof (union sockunion));
paul718e3742002-12-13 20:15:29 +0000666
667 /* We fetch each socket variable into sockunion. */
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000668 RTA_ADDR_GET (&dst, RTA_DST, ifm->ifam_addrs, pnt);
669 RTA_ADDR_GET (&gateway, RTA_GATEWAY, ifm->ifam_addrs, pnt);
paul62debbb2005-06-14 14:07:07 +0000670 RTA_ATTR_GET (mask, RTA_NETMASK, ifm->ifam_addrs, pnt);
671 RTA_ADDR_GET (NULL, RTA_GENMASK, ifm->ifam_addrs, pnt);
paul6fe70d12005-11-12 22:55:10 +0000672 RTA_NAME_GET (ifname, RTA_IFP, ifm->ifam_addrs, pnt, *ifnlen);
paul62debbb2005-06-14 14:07:07 +0000673 RTA_ADDR_GET (addr, RTA_IFA, ifm->ifam_addrs, pnt);
674 RTA_ADDR_GET (NULL, RTA_AUTHOR, ifm->ifam_addrs, pnt);
paul6fe70d12005-11-12 22:55:10 +0000675 RTA_ADDR_GET (brd, RTA_BRD, ifm->ifam_addrs, pnt);
paul718e3742002-12-13 20:15:29 +0000676
paul6fe70d12005-11-12 22:55:10 +0000677 if (IS_ZEBRA_DEBUG_KERNEL)
Andrew J. Schorr55196042006-05-17 15:04:59 +0000678 {
Timo Teräsbe6335d2015-05-23 11:08:41 +0300679 int family = sockunion_family(addr);
680 switch (family)
Andrew J. Schorr55196042006-05-17 15:04:59 +0000681 {
682 case AF_INET:
Timo Teräsbe6335d2015-05-23 11:08:41 +0300683#ifdef HAVE_IPV6
684 case AF_INET6:
685#endif
Andrew J. Schorr55196042006-05-17 15:04:59 +0000686 {
Timo Teräsbe6335d2015-05-23 11:08:41 +0300687 char buf[4][INET6_ADDRSTRLEN];
Andrew J. Schorr55196042006-05-17 15:04:59 +0000688 zlog_debug ("%s: ifindex %d, ifname %s, ifam_addrs 0x%x, "
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000689 "ifam_flags 0x%x, addr %s/%d broad %s dst %s "
690 "gateway %s",
691 __func__, ifm->ifam_index,
Andrew J. Schorr55196042006-05-17 15:04:59 +0000692 (ifnlen ? ifname : "(nil)"), ifm->ifam_addrs,
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000693 ifm->ifam_flags,
Timo Teräsbe6335d2015-05-23 11:08:41 +0300694 inet_ntop(family,&addr->sin.sin_addr,
Andrew J. Schorr55196042006-05-17 15:04:59 +0000695 buf[0],sizeof(buf[0])),
696 ip_masklen(mask->sin.sin_addr),
Timo Teräsbe6335d2015-05-23 11:08:41 +0300697 inet_ntop(family,&brd->sin.sin_addr,
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000698 buf[1],sizeof(buf[1])),
Timo Teräsbe6335d2015-05-23 11:08:41 +0300699 inet_ntop(family,&dst.sin.sin_addr,
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000700 buf[2],sizeof(buf[2])),
Timo Teräsbe6335d2015-05-23 11:08:41 +0300701 inet_ntop(family,&gateway.sin.sin_addr,
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000702 buf[3],sizeof(buf[3])));
Andrew J. Schorr55196042006-05-17 15:04:59 +0000703 }
704 break;
Andrew J. Schorr55196042006-05-17 15:04:59 +0000705 default:
706 zlog_debug ("%s: ifindex %d, ifname %s, ifam_addrs 0x%x",
707 __func__, ifm->ifam_index,
708 (ifnlen ? ifname : "(nil)"), ifm->ifam_addrs);
709 break;
710 }
711 }
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000712
paul718e3742002-12-13 20:15:29 +0000713 /* Assert read up end point matches to end point */
714 if (pnt != end)
Denis Ovsienko85a2ebf2011-12-05 19:36:06 +0400715 zlog_warn ("ifam_read() doesn't read all socket data");
paul718e3742002-12-13 20:15:29 +0000716}
717
718/* Interface's address information get. */
paulec1a4282005-11-24 15:15:17 +0000719int
paul718e3742002-12-13 20:15:29 +0000720ifam_read (struct ifa_msghdr *ifam)
721{
paul6fe70d12005-11-12 22:55:10 +0000722 struct interface *ifp = NULL;
paul0752ef02005-11-03 12:35:21 +0000723 union sockunion addr, mask, brd;
paul6fe70d12005-11-12 22:55:10 +0000724 char ifname[INTERFACE_NAMSIZ];
725 short ifnlen = 0;
726 char isalias = 0;
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000727 int flags = 0;
paul6fe70d12005-11-12 22:55:10 +0000728
729 ifname[0] = ifname[INTERFACE_NAMSIZ - 1] = '\0';
730
731 /* Allocate and read address information. */
732 ifam_read_mesg (ifam, &addr, &mask, &brd, ifname, &ifnlen);
733
734 if ((ifp = if_lookup_by_index(ifam->ifam_index)) == NULL)
paul718e3742002-12-13 20:15:29 +0000735 {
paul6fe70d12005-11-12 22:55:10 +0000736 zlog_warn ("%s: no interface for ifname %s, index %d",
737 __func__, ifname, ifam->ifam_index);
paul718e3742002-12-13 20:15:29 +0000738 return -1;
739 }
paul6fe70d12005-11-12 22:55:10 +0000740
741 if (ifnlen && strncmp (ifp->name, ifname, INTERFACE_NAMSIZ))
742 isalias = 1;
743
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000744 /* N.B. The info in ifa_msghdr does not tell us whether the RTA_BRD
745 field contains a broadcast address or a peer address, so we are forced to
746 rely upon the interface type. */
747 if (if_is_pointopoint(ifp))
748 SET_FLAG(flags, ZEBRA_IFA_PEER);
749
Paul Jakma65022082007-03-06 13:43:05 +0000750#if 0
751 /* it might seem cute to grab the interface metric here, however
752 * we're processing an address update message, and so some systems
753 * (e.g. FBSD) dont bother to fill in ifam_metric. Disabled, but left
754 * in deliberately, as comment.
755 */
pauld34b8992006-01-17 18:03:04 +0000756 ifp->metric = ifam->ifam_metric;
Paul Jakma65022082007-03-06 13:43:05 +0000757#endif
758
paul718e3742002-12-13 20:15:29 +0000759 /* Add connected address. */
760 switch (sockunion_family (&addr))
761 {
762 case AF_INET:
763 if (ifam->ifam_type == RTM_NEWADDR)
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000764 connected_add_ipv4 (ifp, flags, &addr.sin.sin_addr,
paul718e3742002-12-13 20:15:29 +0000765 ip_masklen (mask.sin.sin_addr),
pauld34b8992006-01-17 18:03:04 +0000766 &brd.sin.sin_addr,
767 (isalias ? ifname : NULL));
paul718e3742002-12-13 20:15:29 +0000768 else
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000769 connected_delete_ipv4 (ifp, flags, &addr.sin.sin_addr,
paul718e3742002-12-13 20:15:29 +0000770 ip_masklen (mask.sin.sin_addr),
paul0752ef02005-11-03 12:35:21 +0000771 &brd.sin.sin_addr);
paul718e3742002-12-13 20:15:29 +0000772 break;
773#ifdef HAVE_IPV6
774 case AF_INET6:
775 /* Unset interface index from link-local address when IPv6 stack
776 is KAME. */
777 if (IN6_IS_ADDR_LINKLOCAL (&addr.sin6.sin6_addr))
David Lamparterda1b7ea2015-09-15 20:36:20 -0700778 {
779 SET_IN6_LINKLOCAL_IFINDEX (addr.sin6.sin6_addr, 0);
780 }
paul718e3742002-12-13 20:15:29 +0000781
782 if (ifam->ifam_type == RTM_NEWADDR)
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000783 connected_add_ipv6 (ifp, flags, &addr.sin6.sin6_addr,
paul718e3742002-12-13 20:15:29 +0000784 ip6_masklen (mask.sin6.sin6_addr),
pauld34b8992006-01-17 18:03:04 +0000785 &brd.sin6.sin6_addr,
786 (isalias ? ifname : NULL));
paul718e3742002-12-13 20:15:29 +0000787 else
788 connected_delete_ipv6 (ifp,
789 &addr.sin6.sin6_addr,
790 ip6_masklen (mask.sin6.sin6_addr),
paul0752ef02005-11-03 12:35:21 +0000791 &brd.sin6.sin6_addr);
paul718e3742002-12-13 20:15:29 +0000792 break;
793#endif /* HAVE_IPV6 */
794 default:
795 /* Unsupported family silently ignore... */
796 break;
797 }
paul5c78b3d2006-01-25 04:31:40 +0000798
799 /* Check interface flag for implicit up of the interface. */
800 if_refresh (ifp);
801
802#ifdef SUNOS_5
803 /* In addition to lacking IFANNOUNCE, on SUNOS IFF_UP is strange.
804 * See comments for SUNOS_5 in interface.c::if_flags_mangle.
805 *
806 * Here we take care of case where the real IFF_UP was previously
807 * unset (as kept in struct zebra_if.primary_state) and the mangled
808 * IFF_UP (ie IFF_UP set || listcount(connected) has now transitioned
809 * to unset due to the lost non-primary address having DELADDR'd.
810 *
811 * we must delete the interface, because in between here and next
812 * event for this interface-name the administrator could unplumb
813 * and replumb the interface.
814 */
815 if (!if_is_up (ifp))
816 if_delete_update (ifp);
817#endif /* SUNOS_5 */
818
paul718e3742002-12-13 20:15:29 +0000819 return 0;
820}
David Lamparter6b0655a2014-06-04 06:53:35 +0200821
paul718e3742002-12-13 20:15:29 +0000822/* Interface function for reading kernel routing table information. */
paul6621ca82005-11-23 13:02:08 +0000823static int
paul718e3742002-12-13 20:15:29 +0000824rtm_read_mesg (struct rt_msghdr *rtm,
825 union sockunion *dest,
826 union sockunion *mask,
paul6fe70d12005-11-12 22:55:10 +0000827 union sockunion *gate,
828 char *ifname,
829 short *ifnlen)
paul718e3742002-12-13 20:15:29 +0000830{
831 caddr_t pnt, end;
832
833 /* Pnt points out socket data start point. */
834 pnt = (caddr_t)(rtm + 1);
835 end = ((caddr_t)rtm) + rtm->rtm_msglen;
836
837 /* rt_msghdr version check. */
838 if (rtm->rtm_version != RTM_VERSION)
839 zlog (NULL, LOG_WARNING,
840 "Routing message version different %d should be %d."
841 "This may cause problem\n", rtm->rtm_version, RTM_VERSION);
paul62debbb2005-06-14 14:07:07 +0000842
paul718e3742002-12-13 20:15:29 +0000843 /* Be sure structure is cleared */
844 memset (dest, 0, sizeof (union sockunion));
845 memset (gate, 0, sizeof (union sockunion));
846 memset (mask, 0, sizeof (union sockunion));
847
848 /* We fetch each socket variable into sockunion. */
paul62debbb2005-06-14 14:07:07 +0000849 RTA_ADDR_GET (dest, RTA_DST, rtm->rtm_addrs, pnt);
850 RTA_ADDR_GET (gate, RTA_GATEWAY, rtm->rtm_addrs, pnt);
851 RTA_ATTR_GET (mask, RTA_NETMASK, rtm->rtm_addrs, pnt);
852 RTA_ADDR_GET (NULL, RTA_GENMASK, rtm->rtm_addrs, pnt);
paul6fe70d12005-11-12 22:55:10 +0000853 RTA_NAME_GET (ifname, RTA_IFP, rtm->rtm_addrs, pnt, *ifnlen);
paul62debbb2005-06-14 14:07:07 +0000854 RTA_ADDR_GET (NULL, RTA_IFA, rtm->rtm_addrs, pnt);
855 RTA_ADDR_GET (NULL, RTA_AUTHOR, rtm->rtm_addrs, pnt);
856 RTA_ADDR_GET (NULL, RTA_BRD, rtm->rtm_addrs, pnt);
paul718e3742002-12-13 20:15:29 +0000857
858 /* If there is netmask information set it's family same as
859 destination family*/
860 if (rtm->rtm_addrs & RTA_NETMASK)
861 mask->sa.sa_family = dest->sa.sa_family;
862
863 /* Assert read up to the end of pointer. */
864 if (pnt != end)
Denis Ovsienko85a2ebf2011-12-05 19:36:06 +0400865 zlog (NULL, LOG_WARNING, "rtm_read() doesn't read all socket data.");
paul718e3742002-12-13 20:15:29 +0000866
867 return rtm->rtm_flags;
868}
869
paulec1a4282005-11-24 15:15:17 +0000870void
paul718e3742002-12-13 20:15:29 +0000871rtm_read (struct rt_msghdr *rtm)
872{
873 int flags;
874 u_char zebra_flags;
875 union sockunion dest, mask, gate;
paul6fe70d12005-11-12 22:55:10 +0000876 char ifname[INTERFACE_NAMSIZ + 1];
877 short ifnlen = 0;
paul718e3742002-12-13 20:15:29 +0000878
879 zebra_flags = 0;
880
paul718e3742002-12-13 20:15:29 +0000881 /* Read destination and netmask and gateway from rtm message
882 structure. */
paul6fe70d12005-11-12 22:55:10 +0000883 flags = rtm_read_mesg (rtm, &dest, &mask, &gate, ifname, &ifnlen);
Denis Ovsienko6da59802007-08-17 14:16:30 +0000884 if (!(flags & RTF_DONE))
885 return;
Denis Ovsienkodc958242007-08-13 16:03:06 +0000886 if (IS_ZEBRA_DEBUG_KERNEL)
887 zlog_debug ("%s: got rtm of type %d (%s)", __func__, rtm->rtm_type,
Denis Ovsienko2d844522007-09-14 11:31:55 +0000888 lookup (rtm_type_str, rtm->rtm_type));
paul718e3742002-12-13 20:15:29 +0000889
890#ifdef RTF_CLONED /*bsdi, netbsd 1.6*/
891 if (flags & RTF_CLONED)
892 return;
893#endif
894#ifdef RTF_WASCLONED /*freebsd*/
895 if (flags & RTF_WASCLONED)
896 return;
897#endif
898
Timo Teräs0abf6792016-01-15 17:36:29 +0200899 if ((rtm->rtm_type == RTM_ADD || rtm->rtm_type == RTM_CHANGE) && ! (flags & RTF_UP))
paul718e3742002-12-13 20:15:29 +0000900 return;
901
902 /* This is connected route. */
903 if (! (flags & RTF_GATEWAY))
904 return;
905
906 if (flags & RTF_PROTO1)
907 SET_FLAG (zebra_flags, ZEBRA_FLAG_SELFROUTE);
908
909 /* This is persistent route. */
910 if (flags & RTF_STATIC)
911 SET_FLAG (zebra_flags, ZEBRA_FLAG_STATIC);
912
hasso81dfcaa2003-05-25 19:21:25 +0000913 /* This is a reject or blackhole route */
914 if (flags & RTF_REJECT)
915 SET_FLAG (zebra_flags, ZEBRA_FLAG_REJECT);
916 if (flags & RTF_BLACKHOLE)
917 SET_FLAG (zebra_flags, ZEBRA_FLAG_BLACKHOLE);
918
paul718e3742002-12-13 20:15:29 +0000919 if (dest.sa.sa_family == AF_INET)
920 {
921 struct prefix_ipv4 p;
922
923 p.family = AF_INET;
924 p.prefix = dest.sin.sin_addr;
925 if (flags & RTF_HOST)
926 p.prefixlen = IPV4_MAX_PREFIXLEN;
927 else
928 p.prefixlen = ip_masklen (mask.sin.sin_addr);
paulca162182005-09-12 16:58:52 +0000929
Denis Ovsienkodc958242007-08-13 16:03:06 +0000930 /* Catch self originated messages and match them against our current RIB.
931 * At the same time, ignore unconfirmed messages, they should be tracked
932 * by rtm_write() and kernel_rtm_ipv4().
933 */
Denis Ovsienko96934e62007-09-14 14:56:28 +0000934 if (rtm->rtm_type != RTM_GET && rtm->rtm_pid == pid)
Denis Ovsienkodc958242007-08-13 16:03:06 +0000935 {
Timo Teräsbe6335d2015-05-23 11:08:41 +0300936 char buf[PREFIX_STRLEN], gate_buf[INET_ADDRSTRLEN];
Denis Ovsienkodc958242007-08-13 16:03:06 +0000937 int ret;
Denis Ovsienkodc958242007-08-13 16:03:06 +0000938 if (! IS_ZEBRA_DEBUG_RIB)
939 return;
Feng Lu0d0686f2015-05-22 11:40:02 +0200940 ret = rib_lookup_ipv4_route (&p, &gate, VRF_DEFAULT);
Timo Teräsbe6335d2015-05-23 11:08:41 +0300941 prefix2str (&p, buf, sizeof(buf));
Denis Ovsienkodc958242007-08-13 16:03:06 +0000942 switch (rtm->rtm_type)
943 {
944 case RTM_ADD:
945 case RTM_GET:
946 case RTM_CHANGE:
947 /* The kernel notifies us about a new route in FIB created by us.
948 Do we have a correspondent entry in our RIB? */
949 switch (ret)
950 {
951 case ZEBRA_RIB_NOTFOUND:
Timo Teräsbe6335d2015-05-23 11:08:41 +0300952 zlog_debug ("%s: %s %s: desync: RR isn't yet in RIB, while already in FIB",
953 __func__, lookup (rtm_type_str, rtm->rtm_type), buf);
Denis Ovsienkodc958242007-08-13 16:03:06 +0000954 break;
955 case ZEBRA_RIB_FOUND_CONNECTED:
956 case ZEBRA_RIB_FOUND_NOGATE:
957 inet_ntop (AF_INET, &gate.sin.sin_addr, gate_buf, INET_ADDRSTRLEN);
Timo Teräsbe6335d2015-05-23 11:08:41 +0300958 zlog_debug ("%s: %s %s: desync: RR is in RIB, but gate differs (ours is %s)",
959 __func__, lookup (rtm_type_str, rtm->rtm_type), buf, gate_buf);
Denis Ovsienkodc958242007-08-13 16:03:06 +0000960 break;
961 case ZEBRA_RIB_FOUND_EXACT: /* RIB RR == FIB RR */
Timo Teräsbe6335d2015-05-23 11:08:41 +0300962 zlog_debug ("%s: %s %s: done Ok",
963 __func__, lookup (rtm_type_str, rtm->rtm_type), buf);
Denis Ovsienkodc958242007-08-13 16:03:06 +0000964 rib_lookup_and_dump (&p);
965 return;
966 break;
967 }
968 break;
969 case RTM_DELETE:
970 /* The kernel notifies us about a route deleted by us. Do we still
971 have it in the RIB? Do we have anything instead? */
972 switch (ret)
973 {
974 case ZEBRA_RIB_FOUND_EXACT:
Timo Teräsbe6335d2015-05-23 11:08:41 +0300975 zlog_debug ("%s: %s %s: desync: RR is still in RIB, while already not in FIB",
976 __func__, lookup (rtm_type_str, rtm->rtm_type), buf);
Denis Ovsienkodc958242007-08-13 16:03:06 +0000977 rib_lookup_and_dump (&p);
978 break;
979 case ZEBRA_RIB_FOUND_CONNECTED:
980 case ZEBRA_RIB_FOUND_NOGATE:
Timo Teräsbe6335d2015-05-23 11:08:41 +0300981 zlog_debug ("%s: %s %s: desync: RR is still in RIB, plus gate differs",
982 __func__, lookup (rtm_type_str, rtm->rtm_type), buf);
Denis Ovsienkodc958242007-08-13 16:03:06 +0000983 rib_lookup_and_dump (&p);
984 break;
985 case ZEBRA_RIB_NOTFOUND: /* RIB RR == FIB RR */
Timo Teräsbe6335d2015-05-23 11:08:41 +0300986 zlog_debug ("%s: %s %s: done Ok",
987 __func__, lookup (rtm_type_str, rtm->rtm_type), buf);
Denis Ovsienkodc958242007-08-13 16:03:06 +0000988 rib_lookup_and_dump (&p);
989 return;
990 break;
991 }
992 break;
993 default:
Timo Teräsbe6335d2015-05-23 11:08:41 +0300994 zlog_debug ("%s: %s: warning: loopback RTM of type %s received",
995 __func__, buf, lookup (rtm_type_str, rtm->rtm_type));
Denis Ovsienkodc958242007-08-13 16:03:06 +0000996 }
997 return;
998 }
999
paulca162182005-09-12 16:58:52 +00001000 /* Change, delete the old prefix, we have no further information
1001 * to specify the route really
1002 */
1003 if (rtm->rtm_type == RTM_CHANGE)
1004 rib_delete_ipv4 (ZEBRA_ROUTE_KERNEL, zebra_flags, &p,
Feng Lu0d0686f2015-05-22 11:40:02 +02001005 NULL, 0, VRF_DEFAULT, SAFI_UNICAST);
paulca162182005-09-12 16:58:52 +00001006
1007 if (rtm->rtm_type == RTM_GET
1008 || rtm->rtm_type == RTM_ADD
1009 || rtm->rtm_type == RTM_CHANGE)
Feng Lu0d0686f2015-05-22 11:40:02 +02001010 rib_add_ipv4 (ZEBRA_ROUTE_KERNEL, zebra_flags, &p, &gate.sin.sin_addr,
Timo Teräsb11f3b52015-11-02 16:50:07 +02001011 NULL, 0, VRF_DEFAULT, 0, 0, 0, 0, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +00001012 else
Feng Lu0d0686f2015-05-22 11:40:02 +02001013 rib_delete_ipv4 (ZEBRA_ROUTE_KERNEL, zebra_flags, &p,
1014 &gate.sin.sin_addr, 0, VRF_DEFAULT, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +00001015 }
1016#ifdef HAVE_IPV6
1017 if (dest.sa.sa_family == AF_INET6)
1018 {
Denis Ovsienko5619f562007-10-24 13:13:21 +00001019 /* One day we might have a debug section here like one in the
1020 * IPv4 case above. Just ignore own messages at the moment.
1021 */
1022 if (rtm->rtm_type != RTM_GET && rtm->rtm_pid == pid)
1023 return;
paul718e3742002-12-13 20:15:29 +00001024 struct prefix_ipv6 p;
Paul Jakma9099f9b2016-01-18 10:12:10 +00001025 ifindex_t ifindex = 0;
paul718e3742002-12-13 20:15:29 +00001026
1027 p.family = AF_INET6;
1028 p.prefix = dest.sin6.sin6_addr;
1029 if (flags & RTF_HOST)
1030 p.prefixlen = IPV6_MAX_PREFIXLEN;
1031 else
1032 p.prefixlen = ip6_masklen (mask.sin6.sin6_addr);
1033
1034#ifdef KAME
1035 if (IN6_IS_ADDR_LINKLOCAL (&gate.sin6.sin6_addr))
1036 {
1037 ifindex = IN6_LINKLOCAL_IFINDEX (gate.sin6.sin6_addr);
1038 SET_IN6_LINKLOCAL_IFINDEX (gate.sin6.sin6_addr, 0);
1039 }
1040#endif /* KAME */
1041
paulca162182005-09-12 16:58:52 +00001042 /* CHANGE: delete the old prefix, we have no further information
1043 * to specify the route really
1044 */
1045 if (rtm->rtm_type == RTM_CHANGE)
paul6621ca82005-11-23 13:02:08 +00001046 rib_delete_ipv6 (ZEBRA_ROUTE_KERNEL, zebra_flags, &p,
Feng Lu0d0686f2015-05-22 11:40:02 +02001047 NULL, 0, VRF_DEFAULT, SAFI_UNICAST);
paulca162182005-09-12 16:58:52 +00001048
1049 if (rtm->rtm_type == RTM_GET
1050 || rtm->rtm_type == RTM_ADD
1051 || rtm->rtm_type == RTM_CHANGE)
Feng Lu0d0686f2015-05-22 11:40:02 +02001052 rib_add_ipv6 (ZEBRA_ROUTE_KERNEL, zebra_flags, &p, &gate.sin6.sin6_addr,
Timo Teräsb11f3b52015-11-02 16:50:07 +02001053 ifindex, VRF_DEFAULT, RT_TABLE_MAIN, 0, 0, 0, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +00001054 else
Feng Lu0d0686f2015-05-22 11:40:02 +02001055 rib_delete_ipv6 (ZEBRA_ROUTE_KERNEL, zebra_flags, &p,
1056 &gate.sin6.sin6_addr, ifindex,
1057 VRF_DEFAULT, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +00001058 }
1059#endif /* HAVE_IPV6 */
1060}
1061
1062/* Interface function for the kernel routing table updates. Support
paul6621ca82005-11-23 13:02:08 +00001063 * for RTM_CHANGE will be needed.
1064 * Exported only for rt_socket.c
1065 */
paul718e3742002-12-13 20:15:29 +00001066int
1067rtm_write (int message,
1068 union sockunion *dest,
1069 union sockunion *mask,
1070 union sockunion *gate,
1071 unsigned int index,
1072 int zebra_flags,
1073 int metric)
1074{
1075 int ret;
1076 caddr_t pnt;
1077 struct interface *ifp;
paul718e3742002-12-13 20:15:29 +00001078
1079 /* Sequencial number of routing message. */
1080 static int msg_seq = 0;
1081
1082 /* Struct of rt_msghdr and buffer for storing socket's data. */
1083 struct
1084 {
1085 struct rt_msghdr rtm;
1086 char buf[512];
1087 } msg;
1088
paul718e3742002-12-13 20:15:29 +00001089 if (routing_sock < 0)
1090 return ZEBRA_ERR_EPERM;
1091
1092 /* Clear and set rt_msghdr values */
1093 memset (&msg, 0, sizeof (struct rt_msghdr));
1094 msg.rtm.rtm_version = RTM_VERSION;
1095 msg.rtm.rtm_type = message;
1096 msg.rtm.rtm_seq = msg_seq++;
1097 msg.rtm.rtm_addrs = RTA_DST;
1098 msg.rtm.rtm_addrs |= RTA_GATEWAY;
1099 msg.rtm.rtm_flags = RTF_UP;
1100 msg.rtm.rtm_index = index;
1101
1102 if (metric != 0)
1103 {
1104 msg.rtm.rtm_rmx.rmx_hopcount = metric;
1105 msg.rtm.rtm_inits |= RTV_HOPCOUNT;
1106 }
1107
1108 ifp = if_lookup_by_index (index);
1109
Timo Teräs0abf6792016-01-15 17:36:29 +02001110 if (gate && (message == RTM_ADD || message == RTM_CHANGE))
paul718e3742002-12-13 20:15:29 +00001111 msg.rtm.rtm_flags |= RTF_GATEWAY;
1112
David Warde6f148e2009-12-03 21:43:11 +03001113 /* When RTF_CLONING is unavailable on BSD, should we set some
1114 * other flag instead?
1115 */
1116#ifdef RTF_CLONING
Timo Teräs0abf6792016-01-15 17:36:29 +02001117 if (! gate && (message == RTM_ADD || message == RTM_CHANGE) && ifp &&
paul718e3742002-12-13 20:15:29 +00001118 (ifp->flags & IFF_POINTOPOINT) == 0)
1119 msg.rtm.rtm_flags |= RTF_CLONING;
David Warde6f148e2009-12-03 21:43:11 +03001120#endif /* RTF_CLONING */
paul718e3742002-12-13 20:15:29 +00001121
1122 /* If no protocol specific gateway is specified, use link
1123 address for gateway. */
1124 if (! gate)
1125 {
1126 if (!ifp)
1127 {
Denis Ovsienkodc958242007-08-13 16:03:06 +00001128 char dest_buf[INET_ADDRSTRLEN] = "NULL", mask_buf[INET_ADDRSTRLEN] = "255.255.255.255";
1129 if (dest)
1130 inet_ntop (AF_INET, &dest->sin.sin_addr, dest_buf, INET_ADDRSTRLEN);
1131 if (mask)
1132 inet_ntop (AF_INET, &mask->sin.sin_addr, mask_buf, INET_ADDRSTRLEN);
1133 zlog_warn ("%s: %s/%s: gate == NULL and no gateway found for ifindex %d",
1134 __func__, dest_buf, mask_buf, index);
paul718e3742002-12-13 20:15:29 +00001135 return -1;
1136 }
Timo Teräs954c7d62016-01-15 17:36:33 +02001137 gate = (union sockunion *) &((struct zebra_if *)ifp->info)->sdl;
paul718e3742002-12-13 20:15:29 +00001138 }
1139
1140 if (mask)
1141 msg.rtm.rtm_addrs |= RTA_NETMASK;
Timo Teräs0abf6792016-01-15 17:36:29 +02001142 else if (message == RTM_ADD || message == RTM_CHANGE)
paul718e3742002-12-13 20:15:29 +00001143 msg.rtm.rtm_flags |= RTF_HOST;
1144
1145 /* Tagging route with flags */
1146 msg.rtm.rtm_flags |= (RTF_PROTO1);
1147
1148 /* Additional flags. */
1149 if (zebra_flags & ZEBRA_FLAG_BLACKHOLE)
1150 msg.rtm.rtm_flags |= RTF_BLACKHOLE;
hasso81dfcaa2003-05-25 19:21:25 +00001151 if (zebra_flags & ZEBRA_FLAG_REJECT)
1152 msg.rtm.rtm_flags |= RTF_REJECT;
1153
paul718e3742002-12-13 20:15:29 +00001154
paul718e3742002-12-13 20:15:29 +00001155#define SOCKADDRSET(X,R) \
1156 if (msg.rtm.rtm_addrs & (R)) \
1157 { \
paul6fe70d12005-11-12 22:55:10 +00001158 int len = SAROUNDUP (X); \
paul718e3742002-12-13 20:15:29 +00001159 memcpy (pnt, (caddr_t)(X), len); \
1160 pnt += len; \
1161 }
paul718e3742002-12-13 20:15:29 +00001162
1163 pnt = (caddr_t) msg.buf;
1164
1165 /* Write each socket data into rtm message buffer */
1166 SOCKADDRSET (dest, RTA_DST);
1167 SOCKADDRSET (gate, RTA_GATEWAY);
1168 SOCKADDRSET (mask, RTA_NETMASK);
1169
1170 msg.rtm.rtm_msglen = pnt - (caddr_t) &msg;
1171
1172 ret = write (routing_sock, &msg, msg.rtm.rtm_msglen);
1173
1174 if (ret != msg.rtm.rtm_msglen)
1175 {
1176 if (errno == EEXIST)
1177 return ZEBRA_ERR_RTEXIST;
1178 if (errno == ENETUNREACH)
1179 return ZEBRA_ERR_RTUNREACH;
Denis Ovsienkodc958242007-08-13 16:03:06 +00001180 if (errno == ESRCH)
1181 return ZEBRA_ERR_RTNOEXIST;
paul718e3742002-12-13 20:15:29 +00001182
Denis Ovsienkodc958242007-08-13 16:03:06 +00001183 zlog_warn ("%s: write : %s (%d)", __func__, safe_strerror (errno), errno);
1184 return ZEBRA_ERR_KERNEL;
paul718e3742002-12-13 20:15:29 +00001185 }
Denis Ovsienkodc958242007-08-13 16:03:06 +00001186 return ZEBRA_ERR_NOERROR;
paul718e3742002-12-13 20:15:29 +00001187}
1188
David Lamparter6b0655a2014-06-04 06:53:35 +02001189
paul718e3742002-12-13 20:15:29 +00001190#include "thread.h"
1191#include "zebra/zserv.h"
1192
paul718e3742002-12-13 20:15:29 +00001193/* For debug purpose. */
ajsb6178002004-12-07 21:12:56 +00001194static void
paul718e3742002-12-13 20:15:29 +00001195rtmsg_debug (struct rt_msghdr *rtm)
1196{
Denis Ovsienko2d844522007-09-14 11:31:55 +00001197 zlog_debug ("Kernel: Len: %d Type: %s", rtm->rtm_msglen, lookup (rtm_type_str, rtm->rtm_type));
paul718e3742002-12-13 20:15:29 +00001198 rtm_flag_dump (rtm->rtm_flags);
ajsb6178002004-12-07 21:12:56 +00001199 zlog_debug ("Kernel: message seq %d", rtm->rtm_seq);
David Lamparterda1b7ea2015-09-15 20:36:20 -07001200 zlog_debug ("Kernel: pid %lld, rtm_addrs 0x%x",
1201 (long long)rtm->rtm_pid, rtm->rtm_addrs);
paul718e3742002-12-13 20:15:29 +00001202}
1203
1204/* This is pretty gross, better suggestions welcome -- mhandler */
1205#ifndef RTAX_MAX
1206#ifdef RTA_NUMBITS
1207#define RTAX_MAX RTA_NUMBITS
1208#else
1209#define RTAX_MAX 8
1210#endif /* RTA_NUMBITS */
1211#endif /* RTAX_MAX */
1212
1213/* Kernel routing table and interface updates via routing socket. */
paul6621ca82005-11-23 13:02:08 +00001214static int
paul718e3742002-12-13 20:15:29 +00001215kernel_read (struct thread *thread)
1216{
1217 int sock;
1218 int nbytes;
1219 struct rt_msghdr *rtm;
1220
gdtdbee01f2004-01-06 00:36:51 +00001221 /*
1222 * This must be big enough for any message the kernel might send.
gdtb27900b2004-01-08 15:44:29 +00001223 * Rather than determining how many sockaddrs of what size might be
1224 * in each particular message, just use RTAX_MAX of sockaddr_storage
1225 * for each. Note that the sockaddrs must be after each message
1226 * definition, or rather after whichever happens to be the largest,
1227 * since the buffer needs to be big enough for a message and the
1228 * sockaddrs together.
gdtdbee01f2004-01-06 00:36:51 +00001229 */
paul718e3742002-12-13 20:15:29 +00001230 union
1231 {
1232 /* Routing information. */
1233 struct
1234 {
1235 struct rt_msghdr rtm;
gdtb27900b2004-01-08 15:44:29 +00001236 struct sockaddr_storage addr[RTAX_MAX];
paul718e3742002-12-13 20:15:29 +00001237 } r;
1238
1239 /* Interface information. */
1240 struct
1241 {
1242 struct if_msghdr ifm;
gdtb27900b2004-01-08 15:44:29 +00001243 struct sockaddr_storage addr[RTAX_MAX];
paul718e3742002-12-13 20:15:29 +00001244 } im;
1245
1246 /* Interface address information. */
1247 struct
1248 {
1249 struct ifa_msghdr ifa;
gdtb27900b2004-01-08 15:44:29 +00001250 struct sockaddr_storage addr[RTAX_MAX];
paul718e3742002-12-13 20:15:29 +00001251 } ia;
1252
1253#ifdef RTM_IFANNOUNCE
1254 /* Interface arrival/departure */
1255 struct
1256 {
1257 struct if_announcemsghdr ifan;
gdtb27900b2004-01-08 15:44:29 +00001258 struct sockaddr_storage addr[RTAX_MAX];
paul718e3742002-12-13 20:15:29 +00001259 } ian;
1260#endif /* RTM_IFANNOUNCE */
1261
1262 } buf;
1263
1264 /* Fetch routing socket. */
1265 sock = THREAD_FD (thread);
1266
1267 nbytes= read (sock, &buf, sizeof buf);
1268
1269 if (nbytes <= 0)
1270 {
1271 if (nbytes < 0 && errno != EWOULDBLOCK && errno != EAGAIN)
ajs6099b3b2004-11-20 02:06:59 +00001272 zlog_warn ("routing socket error: %s", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001273 return 0;
1274 }
1275
paul9bcdb632003-07-08 08:09:45 +00001276 thread_add_read (zebrad.master, kernel_read, NULL, sock);
paul718e3742002-12-13 20:15:29 +00001277
hasso726f9b22003-05-25 21:04:54 +00001278 if (IS_ZEBRA_DEBUG_KERNEL)
1279 rtmsg_debug (&buf.r.rtm);
paul718e3742002-12-13 20:15:29 +00001280
1281 rtm = &buf.r.rtm;
1282
gdtb27900b2004-01-08 15:44:29 +00001283 /*
1284 * Ensure that we didn't drop any data, so that processing routines
1285 * can assume they have the whole message.
1286 */
gdtda26e3b2004-01-05 17:20:59 +00001287 if (rtm->rtm_msglen != nbytes)
1288 {
1289 zlog_warn ("kernel_read: rtm->rtm_msglen %d, nbytes %d, type %d\n",
1290 rtm->rtm_msglen, nbytes, rtm->rtm_type);
1291 return -1;
1292 }
1293
paul718e3742002-12-13 20:15:29 +00001294 switch (rtm->rtm_type)
1295 {
1296 case RTM_ADD:
1297 case RTM_DELETE:
paulca162182005-09-12 16:58:52 +00001298 case RTM_CHANGE:
paul718e3742002-12-13 20:15:29 +00001299 rtm_read (rtm);
1300 break;
1301 case RTM_IFINFO:
1302 ifm_read (&buf.im.ifm);
1303 break;
1304 case RTM_NEWADDR:
1305 case RTM_DELADDR:
1306 ifam_read (&buf.ia.ifa);
1307 break;
1308#ifdef RTM_IFANNOUNCE
1309 case RTM_IFANNOUNCE:
1310 ifan_read (&buf.ian.ifan);
1311 break;
1312#endif /* RTM_IFANNOUNCE */
1313 default:
hasso726f9b22003-05-25 21:04:54 +00001314 if (IS_ZEBRA_DEBUG_KERNEL)
ajsb6178002004-12-07 21:12:56 +00001315 zlog_debug("Unprocessed RTM_type: %d", rtm->rtm_type);
paul718e3742002-12-13 20:15:29 +00001316 break;
1317 }
1318 return 0;
1319}
1320
1321/* Make routing socket. */
paul6621ca82005-11-23 13:02:08 +00001322static void
Feng Lu758fb8f2014-07-03 18:23:09 +08001323routing_socket (struct zebra_vrf *zvrf)
paul718e3742002-12-13 20:15:29 +00001324{
Feng Lu758fb8f2014-07-03 18:23:09 +08001325 if (zvrf->vrf_id != VRF_DEFAULT)
1326 return;
1327
pauledd7c242003-06-04 13:59:38 +00001328 if ( zserv_privs.change (ZPRIVS_RAISE) )
1329 zlog_err ("routing_socket: Can't raise privileges");
1330
paul718e3742002-12-13 20:15:29 +00001331 routing_sock = socket (AF_ROUTE, SOCK_RAW, 0);
1332
1333 if (routing_sock < 0)
1334 {
pauledd7c242003-06-04 13:59:38 +00001335 if ( zserv_privs.change (ZPRIVS_LOWER) )
1336 zlog_err ("routing_socket: Can't lower privileges");
paul718e3742002-12-13 20:15:29 +00001337 zlog_warn ("Can't init kernel routing socket");
1338 return;
1339 }
1340
paul865b8522005-01-05 08:30:35 +00001341 /* XXX: Socket should be NONBLOCK, however as we currently
1342 * discard failed writes, this will lead to inconsistencies.
1343 * For now, socket must be blocking.
1344 */
1345 /*if (fcntl (routing_sock, F_SETFL, O_NONBLOCK) < 0)
1346 zlog_warn ("Can't set O_NONBLOCK to routing socket");*/
1347
pauledd7c242003-06-04 13:59:38 +00001348 if ( zserv_privs.change (ZPRIVS_LOWER) )
1349 zlog_err ("routing_socket: Can't lower privileges");
paul718e3742002-12-13 20:15:29 +00001350
1351 /* kernel_read needs rewrite. */
paul9bcdb632003-07-08 08:09:45 +00001352 thread_add_read (zebrad.master, kernel_read, NULL, routing_sock);
paul718e3742002-12-13 20:15:29 +00001353}
1354
1355/* Exported interface function. This function simply calls
1356 routing_socket (). */
1357void
Feng Lu758fb8f2014-07-03 18:23:09 +08001358kernel_init (struct zebra_vrf *zvrf)
paul718e3742002-12-13 20:15:29 +00001359{
Feng Lu758fb8f2014-07-03 18:23:09 +08001360 routing_socket (zvrf);
1361}
1362
1363void
1364kernel_terminate (struct zebra_vrf *zvrf)
1365{
1366 return;
paul718e3742002-12-13 20:15:29 +00001367}