blob: bdfb148db4d8e67b44e946d077780de74b15b007 [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>
23
24#include "if.h"
25#include "prefix.h"
26#include "sockunion.h"
27#include "connected.h"
28#include "memory.h"
29#include "ioctl.h"
30#include "log.h"
31#include "str.h"
32#include "table.h"
33#include "rib.h"
pauledd7c242003-06-04 13:59:38 +000034#include "privs.h"
Feng Lu0d0686f2015-05-22 11:40:02 +020035#include "vrf.h"
paul718e3742002-12-13 20:15:29 +000036
37#include "zebra/interface.h"
38#include "zebra/zserv.h"
39#include "zebra/debug.h"
paulec1a4282005-11-24 15:15:17 +000040#include "zebra/kernel_socket.h"
paul718e3742002-12-13 20:15:29 +000041
pauledd7c242003-06-04 13:59:38 +000042extern struct zebra_privs_t zserv_privs;
paul9bcdb632003-07-08 08:09:45 +000043extern struct zebra_t zebrad;
pauledd7c242003-06-04 13:59:38 +000044
gdt4bfbea82004-01-06 01:13:05 +000045/*
Greg Troxel273b1bd2014-12-02 14:51:49 -050046 * Historically, the BSD routing socket has aligned data following a
47 * struct sockaddr to sizeof(long), which was 4 bytes on some
48 * platforms, and 8 bytes on others. NetBSD 6 changed the routing
49 * socket to align to sizeof(uint64_t), which is 8 bytes. OS X
50 * appears to align to sizeof(int), which is 4 bytes.
gdt4bfbea82004-01-06 01:13:05 +000051 *
Greg Troxel273b1bd2014-12-02 14:51:49 -050052 * Alignment of zero-sized sockaddrs is nonsensical, but historically
53 * BSD defines RT_ROUNDUP(0) to be the alignment interval (rather than
54 * 0). We follow this practice without questioning it, but it is a
55 * bug if quagga calls ROUNDUP with 0.
gdt4bfbea82004-01-06 01:13:05 +000056 */
Greg Troxel273b1bd2014-12-02 14:51:49 -050057
58/*
59 * Because of these varying conventions, the only sane approach is for
60 * the <net/route.h> header to define some flavor of ROUNDUP macro.
61 */
David Lamparter7e923222015-03-03 21:04:20 +010062
63#if defined(SA_SIZE)
64/* SAROUNDUP is the only thing we need, and SA_SIZE provides that */
65#define SAROUNDUP(a) SA_SIZE(a)
66#else /* !SA_SIZE */
67
Greg Troxel273b1bd2014-12-02 14:51:49 -050068#if defined(RT_ROUNDUP)
69#define ROUNDUP(a) RT_ROUNDUP(a)
70#endif /* defined(RT_ROUNDUP) */
71
72/*
73 * If ROUNDUP has not yet been defined in terms of platform-provided
74 * defines, attempt to cope with heuristics.
75 */
76#if !defined(ROUNDUP)
77
78/*
79 * It's a bug for a platform not to define rounding/alignment for
80 * sockaddrs on the routing socket. This warning really is
81 * intentional, to provoke filing bug reports with operating systems
82 * that don't define RT_ROUNDUP or equivalent.
83 */
84#warning "net/route.h does not define RT_ROUNDUP; making unwarranted assumptions!"
85
86/* OS X (Xcode as of 2014-12) is known not to define RT_ROUNDUP */
Doug VanLeuven3b33de62012-10-10 22:10:14 +000087#ifdef __APPLE__
Greg Troxel273b1bd2014-12-02 14:51:49 -050088#define ROUNDUP_TYPE int
Greg Troxel941789e2015-03-23 15:16:29 -040089#else
90#define ROUNDUP_TYPE long
Doug VanLeuven3b33de62012-10-10 22:10:14 +000091#endif
paul718e3742002-12-13 20:15:29 +000092
Greg Troxel273b1bd2014-12-02 14:51:49 -050093#define ROUNDUP(a) \
94 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(ROUNDUP_TYPE) - 1))) : sizeof(ROUNDUP_TYPE))
95
96#endif /* defined(ROUNDUP) */
97
gdt4bfbea82004-01-06 01:13:05 +000098/*
99 * Given a pointer (sockaddr or void *), return the number of bytes
100 * taken up by the sockaddr and any padding needed for alignment.
101 */
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000102#if defined(HAVE_STRUCT_SOCKADDR_SA_LEN)
gdt4bfbea82004-01-06 01:13:05 +0000103#define SAROUNDUP(X) ROUNDUP(((struct sockaddr *)(X))->sa_len)
paul30be8022003-10-22 02:51:38 +0000104#elif defined(HAVE_IPV6)
gdt4bfbea82004-01-06 01:13:05 +0000105/*
106 * One would hope all fixed-size structure definitions are aligned,
107 * but round them up nonetheless.
108 */
109#define SAROUNDUP(X) \
paul3e95a072003-09-24 00:05:45 +0000110 (((struct sockaddr *)(X))->sa_family == AF_INET ? \
111 ROUNDUP(sizeof(struct sockaddr_in)):\
112 (((struct sockaddr *)(X))->sa_family == AF_INET6 ? \
113 ROUNDUP(sizeof(struct sockaddr_in6)) : \
114 (((struct sockaddr *)(X))->sa_family == AF_LINK ? \
paulc50ae8b2004-05-11 11:31:07 +0000115 ROUNDUP(sizeof(struct sockaddr_dl)) : sizeof(struct sockaddr))))
paul30be8022003-10-22 02:51:38 +0000116#else /* HAVE_IPV6 */
gdt4bfbea82004-01-06 01:13:05 +0000117#define SAROUNDUP(X) \
paul30be8022003-10-22 02:51:38 +0000118 (((struct sockaddr *)(X))->sa_family == AF_INET ? \
119 ROUNDUP(sizeof(struct sockaddr_in)):\
120 (((struct sockaddr *)(X))->sa_family == AF_LINK ? \
121 ROUNDUP(sizeof(struct sockaddr_dl)) : sizeof(struct sockaddr)))
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000122#endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
paul718e3742002-12-13 20:15:29 +0000123
David Lamparter7e923222015-03-03 21:04:20 +0100124#endif /* !SA_SIZE */
125
Doug VanLeuvena05df8f2012-10-10 16:11:36 -0700126/*
127 * We use a call to an inline function to copy (PNT) to (DEST)
128 * 1. Calculating the length of the copy requires an #ifdef to determine
129 * if sa_len is a field and can't be used directly inside a #define
130 * 2. So the compiler doesn't complain when DEST is NULL, which is only true
131 * when we are skipping the copy and incrementing to the next SA
paulec1a4282005-11-24 15:15:17 +0000132 */
David Lamparter3e9e2c92015-04-10 09:14:58 +0200133static inline void
Doug VanLeuvena05df8f2012-10-10 16:11:36 -0700134rta_copy (union sockunion *dest, caddr_t src) {
135 int len;
136#ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
137 len = (((struct sockaddr *)src)->sa_len > sizeof (*dest)) ?
138 sizeof (*dest) : ((struct sockaddr *)src)->sa_len ;
139#else
140 len = (SAROUNDUP (src) > sizeof (*dest)) ?
141 sizeof (*dest) : SAROUNDUP (src) ;
142#endif
143 memcpy (dest, src, len);
144}
145
paul62debbb2005-06-14 14:07:07 +0000146#define RTA_ADDR_GET(DEST, RTA, RTMADDRS, PNT) \
147 if ((RTMADDRS) & (RTA)) \
148 { \
149 int len = SAROUNDUP ((PNT)); \
paulea6f82b2005-06-28 17:20:26 +0000150 if ( ((DEST) != NULL) && \
paul62debbb2005-06-14 14:07:07 +0000151 af_check (((struct sockaddr *)(PNT))->sa_family)) \
Doug VanLeuvena05df8f2012-10-10 16:11:36 -0700152 rta_copy((DEST), (PNT)); \
paul62debbb2005-06-14 14:07:07 +0000153 (PNT) += len; \
154 }
155#define RTA_ATTR_GET(DEST, RTA, RTMADDRS, PNT) \
156 if ((RTMADDRS) & (RTA)) \
157 { \
158 int len = SAROUNDUP ((PNT)); \
paulec1a4282005-11-24 15:15:17 +0000159 if ((DEST) != NULL) \
Doug VanLeuvena05df8f2012-10-10 16:11:36 -0700160 rta_copy((DEST), (PNT)); \
paul62debbb2005-06-14 14:07:07 +0000161 (PNT) += len; \
162 }
163
paul6fe70d12005-11-12 22:55:10 +0000164#define RTA_NAME_GET(DEST, RTA, RTMADDRS, PNT, LEN) \
165 if ((RTMADDRS) & (RTA)) \
166 { \
paulec1a4282005-11-24 15:15:17 +0000167 u_char *pdest = (u_char *) (DEST); \
paul6fe70d12005-11-12 22:55:10 +0000168 int len = SAROUNDUP ((PNT)); \
169 struct sockaddr_dl *sdl = (struct sockaddr_dl *)(PNT); \
170 if (IS_ZEBRA_DEBUG_KERNEL) \
171 zlog_debug ("%s: RTA_SDL_GET nlen %d, alen %d", \
172 __func__, sdl->sdl_nlen, sdl->sdl_alen); \
173 if ( ((DEST) != NULL) && (sdl->sdl_family == AF_LINK) \
174 && (sdl->sdl_nlen < IFNAMSIZ) && (sdl->sdl_nlen <= len) ) \
175 { \
paulec1a4282005-11-24 15:15:17 +0000176 memcpy (pdest, sdl->sdl_data, sdl->sdl_nlen); \
177 pdest[sdl->sdl_nlen] = '\0'; \
paul6fe70d12005-11-12 22:55:10 +0000178 (LEN) = sdl->sdl_nlen; \
179 } \
180 (PNT) += len; \
181 } \
182 else \
183 { \
184 (LEN) = 0; \
185 }
paul718e3742002-12-13 20:15:29 +0000186/* Routing socket message types. */
Stephen Hemminger1423c802008-08-14 17:59:25 +0100187const struct message rtm_type_str[] =
paul718e3742002-12-13 20:15:29 +0000188{
189 {RTM_ADD, "RTM_ADD"},
190 {RTM_DELETE, "RTM_DELETE"},
191 {RTM_CHANGE, "RTM_CHANGE"},
192 {RTM_GET, "RTM_GET"},
193 {RTM_LOSING, "RTM_LOSING"},
194 {RTM_REDIRECT, "RTM_REDIRECT"},
195 {RTM_MISS, "RTM_MISS"},
196 {RTM_LOCK, "RTM_LOCK"},
Greg Troxel9458b812006-09-13 12:13:08 +0000197#ifdef OLDADD
paul718e3742002-12-13 20:15:29 +0000198 {RTM_OLDADD, "RTM_OLDADD"},
Greg Troxel9458b812006-09-13 12:13:08 +0000199#endif /* RTM_OLDADD */
200#ifdef RTM_OLDDEL
paul718e3742002-12-13 20:15:29 +0000201 {RTM_OLDDEL, "RTM_OLDDEL"},
Greg Troxel9458b812006-09-13 12:13:08 +0000202#endif /* RTM_OLDDEL */
paul718e3742002-12-13 20:15:29 +0000203 {RTM_RESOLVE, "RTM_RESOLVE"},
204 {RTM_NEWADDR, "RTM_NEWADDR"},
205 {RTM_DELADDR, "RTM_DELADDR"},
206 {RTM_IFINFO, "RTM_IFINFO"},
207#ifdef RTM_OIFINFO
208 {RTM_OIFINFO, "RTM_OIFINFO"},
209#endif /* RTM_OIFINFO */
210#ifdef RTM_NEWMADDR
211 {RTM_NEWMADDR, "RTM_NEWMADDR"},
212#endif /* RTM_NEWMADDR */
213#ifdef RTM_DELMADDR
214 {RTM_DELMADDR, "RTM_DELMADDR"},
215#endif /* RTM_DELMADDR */
216#ifdef RTM_IFANNOUNCE
217 {RTM_IFANNOUNCE, "RTM_IFANNOUNCE"},
218#endif /* RTM_IFANNOUNCE */
219 {0, NULL}
220};
221
Stephen Hemmingerce0db9c2009-05-15 10:47:04 -0700222static const struct message rtm_flag_str[] =
paul718e3742002-12-13 20:15:29 +0000223{
224 {RTF_UP, "UP"},
225 {RTF_GATEWAY, "GATEWAY"},
226 {RTF_HOST, "HOST"},
227 {RTF_REJECT, "REJECT"},
228 {RTF_DYNAMIC, "DYNAMIC"},
229 {RTF_MODIFIED, "MODIFIED"},
230 {RTF_DONE, "DONE"},
231#ifdef RTF_MASK
232 {RTF_MASK, "MASK"},
233#endif /* RTF_MASK */
David Warde6f148e2009-12-03 21:43:11 +0300234#ifdef RTF_CLONING
paul718e3742002-12-13 20:15:29 +0000235 {RTF_CLONING, "CLONING"},
David Warde6f148e2009-12-03 21:43:11 +0300236#endif /* RTF_CLONING */
paul718e3742002-12-13 20:15:29 +0000237 {RTF_XRESOLVE, "XRESOLVE"},
238 {RTF_LLINFO, "LLINFO"},
239 {RTF_STATIC, "STATIC"},
240 {RTF_BLACKHOLE, "BLACKHOLE"},
paul6fe70d12005-11-12 22:55:10 +0000241#ifdef RTF_PRIVATE
242 {RTF_PRIVATE, "PRIVATE"},
243#endif /* RTF_PRIVATE */
paul718e3742002-12-13 20:15:29 +0000244 {RTF_PROTO1, "PROTO1"},
245 {RTF_PROTO2, "PROTO2"},
246#ifdef RTF_PRCLONING
247 {RTF_PRCLONING, "PRCLONING"},
248#endif /* RTF_PRCLONING */
249#ifdef RTF_WASCLONED
250 {RTF_WASCLONED, "WASCLONED"},
251#endif /* RTF_WASCLONED */
252#ifdef RTF_PROTO3
253 {RTF_PROTO3, "PROTO3"},
254#endif /* RTF_PROTO3 */
255#ifdef RTF_PINNED
256 {RTF_PINNED, "PINNED"},
257#endif /* RTF_PINNED */
258#ifdef RTF_LOCAL
259 {RTF_LOCAL, "LOCAL"},
260#endif /* RTF_LOCAL */
261#ifdef RTF_BROADCAST
262 {RTF_BROADCAST, "BROADCAST"},
263#endif /* RTF_BROADCAST */
264#ifdef RTF_MULTICAST
265 {RTF_MULTICAST, "MULTICAST"},
266#endif /* RTF_MULTICAST */
paul6fe70d12005-11-12 22:55:10 +0000267#ifdef RTF_MULTIRT
268 {RTF_MULTIRT, "MULTIRT"},
269#endif /* RTF_MULTIRT */
270#ifdef RTF_SETSRC
271 {RTF_SETSRC, "SETSRC"},
272#endif /* RTF_SETSRC */
paul718e3742002-12-13 20:15:29 +0000273 {0, NULL}
274};
275
276/* Kernel routing update socket. */
277int routing_sock = -1;
278
279/* Yes I'm checking ugly routing socket behavior. */
280/* #define DEBUG */
281
282/* Supported address family check. */
David Lamparter3e9e2c92015-04-10 09:14:58 +0200283static inline int
paul718e3742002-12-13 20:15:29 +0000284af_check (int family)
285{
286 if (family == AF_INET)
287 return 1;
288#ifdef HAVE_IPV6
289 if (family == AF_INET6)
290 return 1;
291#endif /* HAVE_IPV6 */
292 return 0;
293}
David Lamparter6b0655a2014-06-04 06:53:35 +0200294
paul718e3742002-12-13 20:15:29 +0000295/* Dump routing table flag for debug purpose. */
ajsb6178002004-12-07 21:12:56 +0000296static void
paul718e3742002-12-13 20:15:29 +0000297rtm_flag_dump (int flag)
298{
Tom Goff80b2a942009-12-03 14:53:15 +0300299 const struct message *mes;
paul718e3742002-12-13 20:15:29 +0000300 static char buf[BUFSIZ];
301
gdtcced60d2004-07-13 16:45:54 +0000302 buf[0] = '\0';
paul718e3742002-12-13 20:15:29 +0000303 for (mes = rtm_flag_str; mes->key != 0; mes++)
304 {
305 if (mes->key & flag)
306 {
307 strlcat (buf, mes->str, BUFSIZ);
308 strlcat (buf, " ", BUFSIZ);
309 }
310 }
ajsb6178002004-12-07 21:12:56 +0000311 zlog_debug ("Kernel: %s", buf);
paul718e3742002-12-13 20:15:29 +0000312}
313
314#ifdef RTM_IFANNOUNCE
315/* Interface adding function */
paul6621ca82005-11-23 13:02:08 +0000316static int
paul718e3742002-12-13 20:15:29 +0000317ifan_read (struct if_announcemsghdr *ifan)
318{
319 struct interface *ifp;
paul6fe70d12005-11-12 22:55:10 +0000320
paul718e3742002-12-13 20:15:29 +0000321 ifp = if_lookup_by_index (ifan->ifan_index);
paul6fe70d12005-11-12 22:55:10 +0000322
323 if (ifp)
324 assert ( (ifp->ifindex == ifan->ifan_index)
325 || (ifp->ifindex == IFINDEX_INTERNAL) );
326
paulec1a4282005-11-24 15:15:17 +0000327 if ( (ifp == NULL)
328 || ((ifp->ifindex == IFINDEX_INTERNAL)
329 && (ifan->ifan_what == IFAN_ARRIVAL)) )
paul718e3742002-12-13 20:15:29 +0000330 {
paul6fe70d12005-11-12 22:55:10 +0000331 if (IS_ZEBRA_DEBUG_KERNEL)
332 zlog_debug ("%s: creating interface for ifindex %d, name %s",
333 __func__, ifan->ifan_index, ifan->ifan_name);
334
paul718e3742002-12-13 20:15:29 +0000335 /* Create Interface */
ajs08dbfb62005-04-03 03:40:52 +0000336 ifp = if_get_by_name_len(ifan->ifan_name,
337 strnlen(ifan->ifan_name,
338 sizeof(ifan->ifan_name)));
paul718e3742002-12-13 20:15:29 +0000339 ifp->ifindex = ifan->ifan_index;
340
Ingo Flaschberger1db65fa2011-04-17 18:28:20 +0000341 if_get_metric (ifp);
paul718e3742002-12-13 20:15:29 +0000342 if_add_update (ifp);
343 }
344 else if (ifp != NULL && ifan->ifan_what == IFAN_DEPARTURE)
paul6eb88272005-07-29 14:36:00 +0000345 if_delete_update (ifp);
paul718e3742002-12-13 20:15:29 +0000346
347 if_get_flags (ifp);
348 if_get_mtu (ifp);
349 if_get_metric (ifp);
350
351 if (IS_ZEBRA_DEBUG_KERNEL)
paul6fe70d12005-11-12 22:55:10 +0000352 zlog_debug ("%s: interface %s index %d",
353 __func__, ifan->ifan_name, ifan->ifan_index);
paul718e3742002-12-13 20:15:29 +0000354
355 return 0;
356}
357#endif /* RTM_IFANNOUNCE */
358
Doug VanLeuven9234b382012-10-10 16:12:32 -0700359#ifdef HAVE_BSD_IFI_LINK_STATE
Andrew J. Schorrc543a172008-01-10 15:24:32 +0000360/* BSD link detect translation */
361static void
362bsd_linkdetect_translate (struct if_msghdr *ifm)
363{
Andrew J. Schorr55edb0d2008-01-11 15:57:13 +0000364 if ((ifm->ifm_data.ifi_link_state >= LINK_STATE_UP) ||
365 (ifm->ifm_data.ifi_link_state == LINK_STATE_UNKNOWN))
Andrew J. Schorrc543a172008-01-10 15:24:32 +0000366 SET_FLAG(ifm->ifm_flags, IFF_RUNNING);
367 else
368 UNSET_FLAG(ifm->ifm_flags, IFF_RUNNING);
369}
Doug VanLeuven9234b382012-10-10 16:12:32 -0700370#endif /* HAVE_BSD_IFI_LINK_STATE */
Andrew J. Schorrc543a172008-01-10 15:24:32 +0000371
gdtda26e3b2004-01-05 17:20:59 +0000372/*
373 * Handle struct if_msghdr obtained from reading routing socket or
374 * sysctl (from interface_list). There may or may not be sockaddrs
375 * present after the header.
376 */
paulec1a4282005-11-24 15:15:17 +0000377int
paul718e3742002-12-13 20:15:29 +0000378ifm_read (struct if_msghdr *ifm)
379{
paul3e95a072003-09-24 00:05:45 +0000380 struct interface *ifp = NULL;
Tom Goffa34eb362009-11-25 20:36:06 +0000381 struct sockaddr_dl *sdl;
paul6fe70d12005-11-12 22:55:10 +0000382 char ifname[IFNAMSIZ];
383 short ifnlen = 0;
Doug VanLeuvena05df8f2012-10-10 16:11:36 -0700384 caddr_t cp;
paul6fe70d12005-11-12 22:55:10 +0000385
386 /* terminate ifname at head (for strnlen) and tail (for safety) */
387 ifname[IFNAMSIZ - 1] = '\0';
388
gdtda26e3b2004-01-05 17:20:59 +0000389 /* paranoia: sanity check structure */
390 if (ifm->ifm_msglen < sizeof(struct if_msghdr))
391 {
392 zlog_err ("ifm_read: ifm->ifm_msglen %d too short\n",
393 ifm->ifm_msglen);
394 return -1;
395 }
396
397 /*
gdt4bfbea82004-01-06 01:13:05 +0000398 * Check for a sockaddr_dl following the message. First, point to
399 * where a socakddr might be if one follows the message.
gdtda26e3b2004-01-05 17:20:59 +0000400 */
gdt4bfbea82004-01-06 01:13:05 +0000401 cp = (void *)(ifm + 1);
402
paul3e95a072003-09-24 00:05:45 +0000403#ifdef SUNOS_5
paul3e95a072003-09-24 00:05:45 +0000404 /*
gdt4bfbea82004-01-06 01:13:05 +0000405 * XXX This behavior should be narrowed to only the kernel versions
406 * for which the structures returned do not match the headers.
407 *
paul3e95a072003-09-24 00:05:45 +0000408 * if_msghdr_t on 64 bit kernels in Solaris 9 and earlier versions
gdt4bfbea82004-01-06 01:13:05 +0000409 * is 12 bytes larger than the 32 bit version.
paul3e95a072003-09-24 00:05:45 +0000410 */
gdt4bfbea82004-01-06 01:13:05 +0000411 if (((struct sockaddr *) cp)->sa_family == AF_UNSPEC)
paul3e95a072003-09-24 00:05:45 +0000412 cp = cp + 12;
paul3e95a072003-09-24 00:05:45 +0000413#endif
paul718e3742002-12-13 20:15:29 +0000414
paul6fe70d12005-11-12 22:55:10 +0000415 RTA_ADDR_GET (NULL, RTA_DST, ifm->ifm_addrs, cp);
416 RTA_ADDR_GET (NULL, RTA_GATEWAY, ifm->ifm_addrs, cp);
417 RTA_ATTR_GET (NULL, RTA_NETMASK, ifm->ifm_addrs, cp);
418 RTA_ADDR_GET (NULL, RTA_GENMASK, ifm->ifm_addrs, cp);
Tom Goffa34eb362009-11-25 20:36:06 +0000419 sdl = (struct sockaddr_dl *)cp;
paul6fe70d12005-11-12 22:55:10 +0000420 RTA_NAME_GET (ifname, RTA_IFP, ifm->ifm_addrs, cp, ifnlen);
421 RTA_ADDR_GET (NULL, RTA_IFA, ifm->ifm_addrs, cp);
422 RTA_ADDR_GET (NULL, RTA_AUTHOR, ifm->ifm_addrs, cp);
423 RTA_ADDR_GET (NULL, RTA_BRD, ifm->ifm_addrs, cp);
424
425 if (IS_ZEBRA_DEBUG_KERNEL)
426 zlog_debug ("%s: sdl ifname %s", __func__, (ifnlen ? ifname : "(nil)"));
427
gdt4bfbea82004-01-06 01:13:05 +0000428 /*
paul6fe70d12005-11-12 22:55:10 +0000429 * Look up on ifindex first, because ifindices are the primary handle for
430 * interfaces across the user/kernel boundary, for most systems. (Some
431 * messages, such as up/down status changes on NetBSD, do not include a
432 * sockaddr_dl).
gdt4bfbea82004-01-06 01:13:05 +0000433 */
paul6fe70d12005-11-12 22:55:10 +0000434 if ( (ifp = if_lookup_by_index (ifm->ifm_index)) != NULL )
gdt4bfbea82004-01-06 01:13:05 +0000435 {
paul6fe70d12005-11-12 22:55:10 +0000436 /* we have an ifp, verify that the name matches as some systems,
437 * eg Solaris, have a 1:many association of ifindex:ifname
438 * if they dont match, we dont have the correct ifp and should
439 * set it back to NULL to let next check do lookup by name
440 */
441 if (ifnlen && (strncmp (ifp->name, ifname, IFNAMSIZ) != 0) )
gdt4bfbea82004-01-06 01:13:05 +0000442 {
paul6fe70d12005-11-12 22:55:10 +0000443 if (IS_ZEBRA_DEBUG_KERNEL)
444 zlog_debug ("%s: ifp name %s doesnt match sdl name %s",
445 __func__, ifp->name, ifname);
446 ifp = NULL;
gdt4bfbea82004-01-06 01:13:05 +0000447 }
448 }
paul6fe70d12005-11-12 22:55:10 +0000449
gdtda26e3b2004-01-05 17:20:59 +0000450 /*
paul6fe70d12005-11-12 22:55:10 +0000451 * If we dont have an ifp, try looking up by name. Particularly as some
452 * systems (Solaris) have a 1:many mapping of ifindex:ifname - the ifname
453 * is therefore our unique handle to that interface.
454 *
455 * Interfaces specified in the configuration file for which the ifindex
456 * has not been determined will have ifindex == IFINDEX_INTERNAL, and such
457 * interfaces are found by this search, and then their ifindex values can
458 * be filled in.
gdtda26e3b2004-01-05 17:20:59 +0000459 */
paul6fe70d12005-11-12 22:55:10 +0000460 if ( (ifp == NULL) && ifnlen)
461 ifp = if_lookup_by_name (ifname);
paul718e3742002-12-13 20:15:29 +0000462
gdtda26e3b2004-01-05 17:20:59 +0000463 /*
paul6fe70d12005-11-12 22:55:10 +0000464 * If ifp still does not exist or has an invalid index (IFINDEX_INTERNAL),
465 * create or fill in an interface.
gdtda26e3b2004-01-05 17:20:59 +0000466 */
ajsd2fc8892005-04-02 18:38:43 +0000467 if ((ifp == NULL) || (ifp->ifindex == IFINDEX_INTERNAL))
paul718e3742002-12-13 20:15:29 +0000468 {
gdt4bfbea82004-01-06 01:13:05 +0000469 /*
470 * To create or fill in an interface, a sockaddr_dl (via
471 * RTA_IFP) is required.
472 */
paul6fe70d12005-11-12 22:55:10 +0000473 if (!ifnlen)
paul718e3742002-12-13 20:15:29 +0000474 {
paul6fe70d12005-11-12 22:55:10 +0000475 zlog_warn ("Interface index %d (new) missing ifname\n",
paul718e3742002-12-13 20:15:29 +0000476 ifm->ifm_index);
477 return -1;
478 }
paul5c78b3d2006-01-25 04:31:40 +0000479
480#ifndef RTM_IFANNOUNCE
481 /* Down->Down interface should be ignored here.
482 * See further comment below.
483 */
484 if (!CHECK_FLAG (ifm->ifm_flags, IFF_UP))
485 return 0;
486#endif /* !RTM_IFANNOUNCE */
paul6fe70d12005-11-12 22:55:10 +0000487
paul3e95a072003-09-24 00:05:45 +0000488 if (ifp == NULL)
paul6fe70d12005-11-12 22:55:10 +0000489 {
490 /* Interface that zebra was not previously aware of, so create. */
491 ifp = if_create (ifname, ifnlen);
492 if (IS_ZEBRA_DEBUG_KERNEL)
493 zlog_debug ("%s: creating ifp for ifindex %d",
494 __func__, ifm->ifm_index);
495 }
paul718e3742002-12-13 20:15:29 +0000496
paul6fe70d12005-11-12 22:55:10 +0000497 if (IS_ZEBRA_DEBUG_KERNEL)
498 zlog_debug ("%s: updated/created ifp, ifname %s, ifindex %d",
499 __func__, ifp->name, ifp->ifindex);
gdt4bfbea82004-01-06 01:13:05 +0000500 /*
501 * Fill in newly created interface structure, or larval
ajsd2fc8892005-04-02 18:38:43 +0000502 * structure with ifindex IFINDEX_INTERNAL.
gdt4bfbea82004-01-06 01:13:05 +0000503 */
paul718e3742002-12-13 20:15:29 +0000504 ifp->ifindex = ifm->ifm_index;
Andrew J. Schorrc543a172008-01-10 15:24:32 +0000505
Doug VanLeuven9234b382012-10-10 16:12:32 -0700506#ifdef HAVE_BSD_IFI_LINK_STATE /* translate BSD kernel msg for link-state */
Andrew J. Schorrc543a172008-01-10 15:24:32 +0000507 bsd_linkdetect_translate(ifm);
Doug VanLeuven9234b382012-10-10 16:12:32 -0700508#endif /* HAVE_BSD_IFI_LINK_STATE */
Andrew J. Schorrc543a172008-01-10 15:24:32 +0000509
paul5c78b3d2006-01-25 04:31:40 +0000510 if_flags_update (ifp, ifm->ifm_flags);
paul718e3742002-12-13 20:15:29 +0000511#if defined(__bsdi__)
512 if_kvm_get_mtu (ifp);
513#else
514 if_get_mtu (ifp);
515#endif /* __bsdi__ */
516 if_get_metric (ifp);
517
Tom Goffa34eb362009-11-25 20:36:06 +0000518 /*
519 * XXX sockaddr_dl contents can be larger than the structure
David Lamparterca3ccd82012-09-26 14:52:39 +0200520 * definition. There are 2 big families here:
521 * - BSD has sdl_len + sdl_data[16] + overruns sdl_data
522 * we MUST use sdl_len here or we'll truncate data.
523 * - Solaris has no sdl_len, but sdl_data[244]
524 * presumably, it's not going to run past that, so sizeof()
525 * is fine here.
Tom Goffa34eb362009-11-25 20:36:06 +0000526 * a nonzero ifnlen from RTA_NAME_GET() means sdl is valid
527 */
528 if (ifnlen)
David Lamparterca3ccd82012-09-26 14:52:39 +0200529 {
530#ifdef HAVE_STRUCT_SOCKADDR_DL_SDL_LEN
531 memcpy (&ifp->sdl, sdl, sdl->sdl_len);
532#else
Tom Goffa34eb362009-11-25 20:36:06 +0000533 memcpy (&ifp->sdl, sdl, sizeof (struct sockaddr_dl));
David Lamparterca3ccd82012-09-26 14:52:39 +0200534#endif /* HAVE_STRUCT_SOCKADDR_DL_SDL_LEN */
535 }
Tom Goffa34eb362009-11-25 20:36:06 +0000536
paul718e3742002-12-13 20:15:29 +0000537 if_add_update (ifp);
538 }
539 else
gdtda26e3b2004-01-05 17:20:59 +0000540 /*
541 * Interface structure exists. Adjust stored flags from
542 * notification. If interface has up->down or down->up
543 * transition, call state change routines (to adjust routes,
544 * notify routing daemons, etc.). (Other flag changes are stored
545 * but apparently do not trigger action.)
546 */
paul718e3742002-12-13 20:15:29 +0000547 {
paul6fe70d12005-11-12 22:55:10 +0000548 if (ifp->ifindex != ifm->ifm_index)
549 {
550 zlog_warn ("%s: index mismatch, ifname %s, ifp index %d, "
551 "ifm index %d",
552 __func__, ifp->name, ifp->ifindex, ifm->ifm_index);
553 return -1;
554 }
555
Doug VanLeuven9234b382012-10-10 16:12:32 -0700556#ifdef HAVE_BSD_IFI_LINK_STATE /* translate BSD kernel msg for link-state */
Andrew J. Schorrc543a172008-01-10 15:24:32 +0000557 bsd_linkdetect_translate(ifm);
Doug VanLeuven9234b382012-10-10 16:12:32 -0700558#endif /* HAVE_BSD_IFI_LINK_STATE */
Andrew J. Schorrc543a172008-01-10 15:24:32 +0000559
paul5c78b3d2006-01-25 04:31:40 +0000560 /* update flags and handle operative->inoperative transition, if any */
561 if_flags_update (ifp, ifm->ifm_flags);
562
paul6eb88272005-07-29 14:36:00 +0000563#ifndef RTM_IFANNOUNCE
paul5c78b3d2006-01-25 04:31:40 +0000564 if (!if_is_up (ifp))
565 {
566 /* No RTM_IFANNOUNCE on this platform, so we can never
567 * distinguish between ~IFF_UP and delete. We must presume
568 * it has been deleted.
569 * Eg, Solaris will not notify us of unplumb.
570 *
571 * XXX: Fixme - this should be runtime detected
572 * So that a binary compiled on a system with IFANNOUNCE
573 * will still behave correctly if run on a platform without
574 */
575 if_delete_update (ifp);
576 }
paul6eb88272005-07-29 14:36:00 +0000577#endif /* RTM_IFANNOUNCE */
Denis Ovsienko1ba27562007-08-21 16:15:39 +0000578 if (if_is_up (ifp))
579 {
580#if defined(__bsdi__)
581 if_kvm_get_mtu (ifp);
582#else
583 if_get_mtu (ifp);
584#endif /* __bsdi__ */
585 if_get_metric (ifp);
586 }
paul718e3742002-12-13 20:15:29 +0000587 }
paul5c78b3d2006-01-25 04:31:40 +0000588
paul718e3742002-12-13 20:15:29 +0000589#ifdef HAVE_NET_RT_IFLIST
590 ifp->stats = ifm->ifm_data;
591#endif /* HAVE_NET_RT_IFLIST */
592
593 if (IS_ZEBRA_DEBUG_KERNEL)
paul6fe70d12005-11-12 22:55:10 +0000594 zlog_debug ("%s: interface %s index %d",
595 __func__, ifp->name, ifp->ifindex);
paul718e3742002-12-13 20:15:29 +0000596
597 return 0;
598}
David Lamparter6b0655a2014-06-04 06:53:35 +0200599
paul718e3742002-12-13 20:15:29 +0000600/* Address read from struct ifa_msghdr. */
paul6621ca82005-11-23 13:02:08 +0000601static void
paul718e3742002-12-13 20:15:29 +0000602ifam_read_mesg (struct ifa_msghdr *ifm,
603 union sockunion *addr,
604 union sockunion *mask,
paul6fe70d12005-11-12 22:55:10 +0000605 union sockunion *brd,
606 char *ifname,
607 short *ifnlen)
paul718e3742002-12-13 20:15:29 +0000608{
609 caddr_t pnt, end;
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000610 union sockunion dst;
611 union sockunion gateway;
paul718e3742002-12-13 20:15:29 +0000612
613 pnt = (caddr_t)(ifm + 1);
614 end = ((caddr_t)ifm) + ifm->ifam_msglen;
615
paul718e3742002-12-13 20:15:29 +0000616 /* Be sure structure is cleared */
617 memset (mask, 0, sizeof (union sockunion));
618 memset (addr, 0, sizeof (union sockunion));
paul6621ca82005-11-23 13:02:08 +0000619 memset (brd, 0, sizeof (union sockunion));
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000620 memset (&dst, 0, sizeof (union sockunion));
621 memset (&gateway, 0, sizeof (union sockunion));
paul718e3742002-12-13 20:15:29 +0000622
623 /* We fetch each socket variable into sockunion. */
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000624 RTA_ADDR_GET (&dst, RTA_DST, ifm->ifam_addrs, pnt);
625 RTA_ADDR_GET (&gateway, RTA_GATEWAY, ifm->ifam_addrs, pnt);
paul62debbb2005-06-14 14:07:07 +0000626 RTA_ATTR_GET (mask, RTA_NETMASK, ifm->ifam_addrs, pnt);
627 RTA_ADDR_GET (NULL, RTA_GENMASK, ifm->ifam_addrs, pnt);
paul6fe70d12005-11-12 22:55:10 +0000628 RTA_NAME_GET (ifname, RTA_IFP, ifm->ifam_addrs, pnt, *ifnlen);
paul62debbb2005-06-14 14:07:07 +0000629 RTA_ADDR_GET (addr, RTA_IFA, ifm->ifam_addrs, pnt);
630 RTA_ADDR_GET (NULL, RTA_AUTHOR, ifm->ifam_addrs, pnt);
paul6fe70d12005-11-12 22:55:10 +0000631 RTA_ADDR_GET (brd, RTA_BRD, ifm->ifam_addrs, pnt);
paul718e3742002-12-13 20:15:29 +0000632
paul6fe70d12005-11-12 22:55:10 +0000633 if (IS_ZEBRA_DEBUG_KERNEL)
Andrew J. Schorr55196042006-05-17 15:04:59 +0000634 {
Timo Teräsbe6335d2015-05-23 11:08:41 +0300635 int family = sockunion_family(addr);
636 switch (family)
Andrew J. Schorr55196042006-05-17 15:04:59 +0000637 {
638 case AF_INET:
Timo Teräsbe6335d2015-05-23 11:08:41 +0300639#ifdef HAVE_IPV6
640 case AF_INET6:
641#endif
Andrew J. Schorr55196042006-05-17 15:04:59 +0000642 {
Timo Teräsbe6335d2015-05-23 11:08:41 +0300643 char buf[4][INET6_ADDRSTRLEN];
Andrew J. Schorr55196042006-05-17 15:04:59 +0000644 zlog_debug ("%s: ifindex %d, ifname %s, ifam_addrs 0x%x, "
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000645 "ifam_flags 0x%x, addr %s/%d broad %s dst %s "
646 "gateway %s",
647 __func__, ifm->ifam_index,
Andrew J. Schorr55196042006-05-17 15:04:59 +0000648 (ifnlen ? ifname : "(nil)"), ifm->ifam_addrs,
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000649 ifm->ifam_flags,
Timo Teräsbe6335d2015-05-23 11:08:41 +0300650 inet_ntop(family,&addr->sin.sin_addr,
Andrew J. Schorr55196042006-05-17 15:04:59 +0000651 buf[0],sizeof(buf[0])),
652 ip_masklen(mask->sin.sin_addr),
Timo Teräsbe6335d2015-05-23 11:08:41 +0300653 inet_ntop(family,&brd->sin.sin_addr,
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000654 buf[1],sizeof(buf[1])),
Timo Teräsbe6335d2015-05-23 11:08:41 +0300655 inet_ntop(family,&dst.sin.sin_addr,
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000656 buf[2],sizeof(buf[2])),
Timo Teräsbe6335d2015-05-23 11:08:41 +0300657 inet_ntop(family,&gateway.sin.sin_addr,
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000658 buf[3],sizeof(buf[3])));
Andrew J. Schorr55196042006-05-17 15:04:59 +0000659 }
660 break;
Andrew J. Schorr55196042006-05-17 15:04:59 +0000661 default:
662 zlog_debug ("%s: ifindex %d, ifname %s, ifam_addrs 0x%x",
663 __func__, ifm->ifam_index,
664 (ifnlen ? ifname : "(nil)"), ifm->ifam_addrs);
665 break;
666 }
667 }
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000668
paul718e3742002-12-13 20:15:29 +0000669 /* Assert read up end point matches to end point */
670 if (pnt != end)
Denis Ovsienko85a2ebf2011-12-05 19:36:06 +0400671 zlog_warn ("ifam_read() doesn't read all socket data");
paul718e3742002-12-13 20:15:29 +0000672}
673
674/* Interface's address information get. */
paulec1a4282005-11-24 15:15:17 +0000675int
paul718e3742002-12-13 20:15:29 +0000676ifam_read (struct ifa_msghdr *ifam)
677{
paul6fe70d12005-11-12 22:55:10 +0000678 struct interface *ifp = NULL;
paul0752ef02005-11-03 12:35:21 +0000679 union sockunion addr, mask, brd;
paul6fe70d12005-11-12 22:55:10 +0000680 char ifname[INTERFACE_NAMSIZ];
681 short ifnlen = 0;
682 char isalias = 0;
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000683 int flags = 0;
paul6fe70d12005-11-12 22:55:10 +0000684
685 ifname[0] = ifname[INTERFACE_NAMSIZ - 1] = '\0';
686
687 /* Allocate and read address information. */
688 ifam_read_mesg (ifam, &addr, &mask, &brd, ifname, &ifnlen);
689
690 if ((ifp = if_lookup_by_index(ifam->ifam_index)) == NULL)
paul718e3742002-12-13 20:15:29 +0000691 {
paul6fe70d12005-11-12 22:55:10 +0000692 zlog_warn ("%s: no interface for ifname %s, index %d",
693 __func__, ifname, ifam->ifam_index);
paul718e3742002-12-13 20:15:29 +0000694 return -1;
695 }
paul6fe70d12005-11-12 22:55:10 +0000696
697 if (ifnlen && strncmp (ifp->name, ifname, INTERFACE_NAMSIZ))
698 isalias = 1;
699
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000700 /* N.B. The info in ifa_msghdr does not tell us whether the RTA_BRD
701 field contains a broadcast address or a peer address, so we are forced to
702 rely upon the interface type. */
703 if (if_is_pointopoint(ifp))
704 SET_FLAG(flags, ZEBRA_IFA_PEER);
705
Paul Jakma65022082007-03-06 13:43:05 +0000706#if 0
707 /* it might seem cute to grab the interface metric here, however
708 * we're processing an address update message, and so some systems
709 * (e.g. FBSD) dont bother to fill in ifam_metric. Disabled, but left
710 * in deliberately, as comment.
711 */
pauld34b8992006-01-17 18:03:04 +0000712 ifp->metric = ifam->ifam_metric;
Paul Jakma65022082007-03-06 13:43:05 +0000713#endif
714
paul718e3742002-12-13 20:15:29 +0000715 /* Add connected address. */
716 switch (sockunion_family (&addr))
717 {
718 case AF_INET:
719 if (ifam->ifam_type == RTM_NEWADDR)
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000720 connected_add_ipv4 (ifp, flags, &addr.sin.sin_addr,
paul718e3742002-12-13 20:15:29 +0000721 ip_masklen (mask.sin.sin_addr),
pauld34b8992006-01-17 18:03:04 +0000722 &brd.sin.sin_addr,
723 (isalias ? ifname : NULL));
paul718e3742002-12-13 20:15:29 +0000724 else
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000725 connected_delete_ipv4 (ifp, flags, &addr.sin.sin_addr,
paul718e3742002-12-13 20:15:29 +0000726 ip_masklen (mask.sin.sin_addr),
paul0752ef02005-11-03 12:35:21 +0000727 &brd.sin.sin_addr);
paul718e3742002-12-13 20:15:29 +0000728 break;
729#ifdef HAVE_IPV6
730 case AF_INET6:
731 /* Unset interface index from link-local address when IPv6 stack
732 is KAME. */
733 if (IN6_IS_ADDR_LINKLOCAL (&addr.sin6.sin6_addr))
734 SET_IN6_LINKLOCAL_IFINDEX (addr.sin6.sin6_addr, 0);
735
736 if (ifam->ifam_type == RTM_NEWADDR)
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000737 connected_add_ipv6 (ifp, flags, &addr.sin6.sin6_addr,
paul718e3742002-12-13 20:15:29 +0000738 ip6_masklen (mask.sin6.sin6_addr),
pauld34b8992006-01-17 18:03:04 +0000739 &brd.sin6.sin6_addr,
740 (isalias ? ifname : NULL));
paul718e3742002-12-13 20:15:29 +0000741 else
742 connected_delete_ipv6 (ifp,
743 &addr.sin6.sin6_addr,
744 ip6_masklen (mask.sin6.sin6_addr),
paul0752ef02005-11-03 12:35:21 +0000745 &brd.sin6.sin6_addr);
paul718e3742002-12-13 20:15:29 +0000746 break;
747#endif /* HAVE_IPV6 */
748 default:
749 /* Unsupported family silently ignore... */
750 break;
751 }
paul5c78b3d2006-01-25 04:31:40 +0000752
753 /* Check interface flag for implicit up of the interface. */
754 if_refresh (ifp);
755
756#ifdef SUNOS_5
757 /* In addition to lacking IFANNOUNCE, on SUNOS IFF_UP is strange.
758 * See comments for SUNOS_5 in interface.c::if_flags_mangle.
759 *
760 * Here we take care of case where the real IFF_UP was previously
761 * unset (as kept in struct zebra_if.primary_state) and the mangled
762 * IFF_UP (ie IFF_UP set || listcount(connected) has now transitioned
763 * to unset due to the lost non-primary address having DELADDR'd.
764 *
765 * we must delete the interface, because in between here and next
766 * event for this interface-name the administrator could unplumb
767 * and replumb the interface.
768 */
769 if (!if_is_up (ifp))
770 if_delete_update (ifp);
771#endif /* SUNOS_5 */
772
paul718e3742002-12-13 20:15:29 +0000773 return 0;
774}
David Lamparter6b0655a2014-06-04 06:53:35 +0200775
paul718e3742002-12-13 20:15:29 +0000776/* Interface function for reading kernel routing table information. */
paul6621ca82005-11-23 13:02:08 +0000777static int
paul718e3742002-12-13 20:15:29 +0000778rtm_read_mesg (struct rt_msghdr *rtm,
779 union sockunion *dest,
780 union sockunion *mask,
paul6fe70d12005-11-12 22:55:10 +0000781 union sockunion *gate,
782 char *ifname,
783 short *ifnlen)
paul718e3742002-12-13 20:15:29 +0000784{
785 caddr_t pnt, end;
786
787 /* Pnt points out socket data start point. */
788 pnt = (caddr_t)(rtm + 1);
789 end = ((caddr_t)rtm) + rtm->rtm_msglen;
790
791 /* rt_msghdr version check. */
792 if (rtm->rtm_version != RTM_VERSION)
793 zlog (NULL, LOG_WARNING,
794 "Routing message version different %d should be %d."
795 "This may cause problem\n", rtm->rtm_version, RTM_VERSION);
paul62debbb2005-06-14 14:07:07 +0000796
paul718e3742002-12-13 20:15:29 +0000797 /* Be sure structure is cleared */
798 memset (dest, 0, sizeof (union sockunion));
799 memset (gate, 0, sizeof (union sockunion));
800 memset (mask, 0, sizeof (union sockunion));
801
802 /* We fetch each socket variable into sockunion. */
paul62debbb2005-06-14 14:07:07 +0000803 RTA_ADDR_GET (dest, RTA_DST, rtm->rtm_addrs, pnt);
804 RTA_ADDR_GET (gate, RTA_GATEWAY, rtm->rtm_addrs, pnt);
805 RTA_ATTR_GET (mask, RTA_NETMASK, rtm->rtm_addrs, pnt);
806 RTA_ADDR_GET (NULL, RTA_GENMASK, rtm->rtm_addrs, pnt);
paul6fe70d12005-11-12 22:55:10 +0000807 RTA_NAME_GET (ifname, RTA_IFP, rtm->rtm_addrs, pnt, *ifnlen);
paul62debbb2005-06-14 14:07:07 +0000808 RTA_ADDR_GET (NULL, RTA_IFA, rtm->rtm_addrs, pnt);
809 RTA_ADDR_GET (NULL, RTA_AUTHOR, rtm->rtm_addrs, pnt);
810 RTA_ADDR_GET (NULL, RTA_BRD, rtm->rtm_addrs, pnt);
paul718e3742002-12-13 20:15:29 +0000811
812 /* If there is netmask information set it's family same as
813 destination family*/
814 if (rtm->rtm_addrs & RTA_NETMASK)
815 mask->sa.sa_family = dest->sa.sa_family;
816
817 /* Assert read up to the end of pointer. */
818 if (pnt != end)
Denis Ovsienko85a2ebf2011-12-05 19:36:06 +0400819 zlog (NULL, LOG_WARNING, "rtm_read() doesn't read all socket data.");
paul718e3742002-12-13 20:15:29 +0000820
821 return rtm->rtm_flags;
822}
823
paulec1a4282005-11-24 15:15:17 +0000824void
paul718e3742002-12-13 20:15:29 +0000825rtm_read (struct rt_msghdr *rtm)
826{
827 int flags;
828 u_char zebra_flags;
829 union sockunion dest, mask, gate;
paul6fe70d12005-11-12 22:55:10 +0000830 char ifname[INTERFACE_NAMSIZ + 1];
831 short ifnlen = 0;
paul718e3742002-12-13 20:15:29 +0000832
833 zebra_flags = 0;
834
paul718e3742002-12-13 20:15:29 +0000835 /* Read destination and netmask and gateway from rtm message
836 structure. */
paul6fe70d12005-11-12 22:55:10 +0000837 flags = rtm_read_mesg (rtm, &dest, &mask, &gate, ifname, &ifnlen);
Denis Ovsienko6da59802007-08-17 14:16:30 +0000838 if (!(flags & RTF_DONE))
839 return;
Denis Ovsienkodc958242007-08-13 16:03:06 +0000840 if (IS_ZEBRA_DEBUG_KERNEL)
841 zlog_debug ("%s: got rtm of type %d (%s)", __func__, rtm->rtm_type,
Denis Ovsienko2d844522007-09-14 11:31:55 +0000842 lookup (rtm_type_str, rtm->rtm_type));
paul718e3742002-12-13 20:15:29 +0000843
844#ifdef RTF_CLONED /*bsdi, netbsd 1.6*/
845 if (flags & RTF_CLONED)
846 return;
847#endif
848#ifdef RTF_WASCLONED /*freebsd*/
849 if (flags & RTF_WASCLONED)
850 return;
851#endif
852
853 if ((rtm->rtm_type == RTM_ADD) && ! (flags & RTF_UP))
854 return;
855
856 /* This is connected route. */
857 if (! (flags & RTF_GATEWAY))
858 return;
859
860 if (flags & RTF_PROTO1)
861 SET_FLAG (zebra_flags, ZEBRA_FLAG_SELFROUTE);
862
863 /* This is persistent route. */
864 if (flags & RTF_STATIC)
865 SET_FLAG (zebra_flags, ZEBRA_FLAG_STATIC);
866
hasso81dfcaa2003-05-25 19:21:25 +0000867 /* This is a reject or blackhole route */
868 if (flags & RTF_REJECT)
869 SET_FLAG (zebra_flags, ZEBRA_FLAG_REJECT);
870 if (flags & RTF_BLACKHOLE)
871 SET_FLAG (zebra_flags, ZEBRA_FLAG_BLACKHOLE);
872
paul718e3742002-12-13 20:15:29 +0000873 if (dest.sa.sa_family == AF_INET)
874 {
875 struct prefix_ipv4 p;
876
877 p.family = AF_INET;
878 p.prefix = dest.sin.sin_addr;
879 if (flags & RTF_HOST)
880 p.prefixlen = IPV4_MAX_PREFIXLEN;
881 else
882 p.prefixlen = ip_masklen (mask.sin.sin_addr);
paulca162182005-09-12 16:58:52 +0000883
Denis Ovsienkodc958242007-08-13 16:03:06 +0000884 /* Catch self originated messages and match them against our current RIB.
885 * At the same time, ignore unconfirmed messages, they should be tracked
886 * by rtm_write() and kernel_rtm_ipv4().
887 */
Denis Ovsienko96934e62007-09-14 14:56:28 +0000888 if (rtm->rtm_type != RTM_GET && rtm->rtm_pid == pid)
Denis Ovsienkodc958242007-08-13 16:03:06 +0000889 {
Timo Teräsbe6335d2015-05-23 11:08:41 +0300890 char buf[PREFIX_STRLEN], gate_buf[INET_ADDRSTRLEN];
Denis Ovsienkodc958242007-08-13 16:03:06 +0000891 int ret;
Denis Ovsienkodc958242007-08-13 16:03:06 +0000892 if (! IS_ZEBRA_DEBUG_RIB)
893 return;
Feng Lu0d0686f2015-05-22 11:40:02 +0200894 ret = rib_lookup_ipv4_route (&p, &gate, VRF_DEFAULT);
Timo Teräsbe6335d2015-05-23 11:08:41 +0300895 prefix2str (&p, buf, sizeof(buf));
Denis Ovsienkodc958242007-08-13 16:03:06 +0000896 switch (rtm->rtm_type)
897 {
898 case RTM_ADD:
899 case RTM_GET:
900 case RTM_CHANGE:
901 /* The kernel notifies us about a new route in FIB created by us.
902 Do we have a correspondent entry in our RIB? */
903 switch (ret)
904 {
905 case ZEBRA_RIB_NOTFOUND:
Timo Teräsbe6335d2015-05-23 11:08:41 +0300906 zlog_debug ("%s: %s %s: desync: RR isn't yet in RIB, while already in FIB",
907 __func__, lookup (rtm_type_str, rtm->rtm_type), buf);
Denis Ovsienkodc958242007-08-13 16:03:06 +0000908 break;
909 case ZEBRA_RIB_FOUND_CONNECTED:
910 case ZEBRA_RIB_FOUND_NOGATE:
911 inet_ntop (AF_INET, &gate.sin.sin_addr, gate_buf, INET_ADDRSTRLEN);
Timo Teräsbe6335d2015-05-23 11:08:41 +0300912 zlog_debug ("%s: %s %s: desync: RR is in RIB, but gate differs (ours is %s)",
913 __func__, lookup (rtm_type_str, rtm->rtm_type), buf, gate_buf);
Denis Ovsienkodc958242007-08-13 16:03:06 +0000914 break;
915 case ZEBRA_RIB_FOUND_EXACT: /* RIB RR == FIB RR */
Timo Teräsbe6335d2015-05-23 11:08:41 +0300916 zlog_debug ("%s: %s %s: done Ok",
917 __func__, lookup (rtm_type_str, rtm->rtm_type), buf);
Denis Ovsienkodc958242007-08-13 16:03:06 +0000918 rib_lookup_and_dump (&p);
919 return;
920 break;
921 }
922 break;
923 case RTM_DELETE:
924 /* The kernel notifies us about a route deleted by us. Do we still
925 have it in the RIB? Do we have anything instead? */
926 switch (ret)
927 {
928 case ZEBRA_RIB_FOUND_EXACT:
Timo Teräsbe6335d2015-05-23 11:08:41 +0300929 zlog_debug ("%s: %s %s: desync: RR is still in RIB, while already not in FIB",
930 __func__, lookup (rtm_type_str, rtm->rtm_type), buf);
Denis Ovsienkodc958242007-08-13 16:03:06 +0000931 rib_lookup_and_dump (&p);
932 break;
933 case ZEBRA_RIB_FOUND_CONNECTED:
934 case ZEBRA_RIB_FOUND_NOGATE:
Timo Teräsbe6335d2015-05-23 11:08:41 +0300935 zlog_debug ("%s: %s %s: desync: RR is still in RIB, plus gate differs",
936 __func__, lookup (rtm_type_str, rtm->rtm_type), buf);
Denis Ovsienkodc958242007-08-13 16:03:06 +0000937 rib_lookup_and_dump (&p);
938 break;
939 case ZEBRA_RIB_NOTFOUND: /* RIB RR == FIB RR */
Timo Teräsbe6335d2015-05-23 11:08:41 +0300940 zlog_debug ("%s: %s %s: done Ok",
941 __func__, lookup (rtm_type_str, rtm->rtm_type), buf);
Denis Ovsienkodc958242007-08-13 16:03:06 +0000942 rib_lookup_and_dump (&p);
943 return;
944 break;
945 }
946 break;
947 default:
Timo Teräsbe6335d2015-05-23 11:08:41 +0300948 zlog_debug ("%s: %s: warning: loopback RTM of type %s received",
949 __func__, buf, lookup (rtm_type_str, rtm->rtm_type));
Denis Ovsienkodc958242007-08-13 16:03:06 +0000950 }
951 return;
952 }
953
paulca162182005-09-12 16:58:52 +0000954 /* Change, delete the old prefix, we have no further information
955 * to specify the route really
956 */
957 if (rtm->rtm_type == RTM_CHANGE)
958 rib_delete_ipv4 (ZEBRA_ROUTE_KERNEL, zebra_flags, &p,
Feng Lu0d0686f2015-05-22 11:40:02 +0200959 NULL, 0, VRF_DEFAULT, SAFI_UNICAST);
paulca162182005-09-12 16:58:52 +0000960
961 if (rtm->rtm_type == RTM_GET
962 || rtm->rtm_type == RTM_ADD
963 || rtm->rtm_type == RTM_CHANGE)
Feng Lu0d0686f2015-05-22 11:40:02 +0200964 rib_add_ipv4 (ZEBRA_ROUTE_KERNEL, zebra_flags, &p, &gate.sin.sin_addr,
965 NULL, 0, VRF_DEFAULT, rtm->rtm_table, 0, 0, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +0000966 else
Feng Lu0d0686f2015-05-22 11:40:02 +0200967 rib_delete_ipv4 (ZEBRA_ROUTE_KERNEL, zebra_flags, &p,
968 &gate.sin.sin_addr, 0, VRF_DEFAULT, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +0000969 }
970#ifdef HAVE_IPV6
971 if (dest.sa.sa_family == AF_INET6)
972 {
Denis Ovsienko5619f562007-10-24 13:13:21 +0000973 /* One day we might have a debug section here like one in the
974 * IPv4 case above. Just ignore own messages at the moment.
975 */
976 if (rtm->rtm_type != RTM_GET && rtm->rtm_pid == pid)
977 return;
paul718e3742002-12-13 20:15:29 +0000978 struct prefix_ipv6 p;
979 unsigned int ifindex = 0;
980
981 p.family = AF_INET6;
982 p.prefix = dest.sin6.sin6_addr;
983 if (flags & RTF_HOST)
984 p.prefixlen = IPV6_MAX_PREFIXLEN;
985 else
986 p.prefixlen = ip6_masklen (mask.sin6.sin6_addr);
987
988#ifdef KAME
989 if (IN6_IS_ADDR_LINKLOCAL (&gate.sin6.sin6_addr))
990 {
991 ifindex = IN6_LINKLOCAL_IFINDEX (gate.sin6.sin6_addr);
992 SET_IN6_LINKLOCAL_IFINDEX (gate.sin6.sin6_addr, 0);
993 }
994#endif /* KAME */
995
paulca162182005-09-12 16:58:52 +0000996 /* CHANGE: delete the old prefix, we have no further information
997 * to specify the route really
998 */
999 if (rtm->rtm_type == RTM_CHANGE)
paul6621ca82005-11-23 13:02:08 +00001000 rib_delete_ipv6 (ZEBRA_ROUTE_KERNEL, zebra_flags, &p,
Feng Lu0d0686f2015-05-22 11:40:02 +02001001 NULL, 0, VRF_DEFAULT, SAFI_UNICAST);
paulca162182005-09-12 16:58:52 +00001002
1003 if (rtm->rtm_type == RTM_GET
1004 || rtm->rtm_type == RTM_ADD
1005 || rtm->rtm_type == RTM_CHANGE)
Feng Lu0d0686f2015-05-22 11:40:02 +02001006 rib_add_ipv6 (ZEBRA_ROUTE_KERNEL, zebra_flags, &p, &gate.sin6.sin6_addr,
1007 ifindex, VRF_DEFAULT, RT_TABLE_MAIN, 0, 0, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +00001008 else
Feng Lu0d0686f2015-05-22 11:40:02 +02001009 rib_delete_ipv6 (ZEBRA_ROUTE_KERNEL, zebra_flags, &p,
1010 &gate.sin6.sin6_addr, ifindex,
1011 VRF_DEFAULT, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +00001012 }
1013#endif /* HAVE_IPV6 */
1014}
1015
1016/* Interface function for the kernel routing table updates. Support
paul6621ca82005-11-23 13:02:08 +00001017 * for RTM_CHANGE will be needed.
1018 * Exported only for rt_socket.c
1019 */
paul718e3742002-12-13 20:15:29 +00001020int
1021rtm_write (int message,
1022 union sockunion *dest,
1023 union sockunion *mask,
1024 union sockunion *gate,
1025 unsigned int index,
1026 int zebra_flags,
1027 int metric)
1028{
1029 int ret;
1030 caddr_t pnt;
1031 struct interface *ifp;
paul718e3742002-12-13 20:15:29 +00001032
1033 /* Sequencial number of routing message. */
1034 static int msg_seq = 0;
1035
1036 /* Struct of rt_msghdr and buffer for storing socket's data. */
1037 struct
1038 {
1039 struct rt_msghdr rtm;
1040 char buf[512];
1041 } msg;
1042
paul718e3742002-12-13 20:15:29 +00001043 if (routing_sock < 0)
1044 return ZEBRA_ERR_EPERM;
1045
1046 /* Clear and set rt_msghdr values */
1047 memset (&msg, 0, sizeof (struct rt_msghdr));
1048 msg.rtm.rtm_version = RTM_VERSION;
1049 msg.rtm.rtm_type = message;
1050 msg.rtm.rtm_seq = msg_seq++;
1051 msg.rtm.rtm_addrs = RTA_DST;
1052 msg.rtm.rtm_addrs |= RTA_GATEWAY;
1053 msg.rtm.rtm_flags = RTF_UP;
1054 msg.rtm.rtm_index = index;
1055
1056 if (metric != 0)
1057 {
1058 msg.rtm.rtm_rmx.rmx_hopcount = metric;
1059 msg.rtm.rtm_inits |= RTV_HOPCOUNT;
1060 }
1061
1062 ifp = if_lookup_by_index (index);
1063
1064 if (gate && message == RTM_ADD)
1065 msg.rtm.rtm_flags |= RTF_GATEWAY;
1066
David Warde6f148e2009-12-03 21:43:11 +03001067 /* When RTF_CLONING is unavailable on BSD, should we set some
1068 * other flag instead?
1069 */
1070#ifdef RTF_CLONING
paul718e3742002-12-13 20:15:29 +00001071 if (! gate && message == RTM_ADD && ifp &&
1072 (ifp->flags & IFF_POINTOPOINT) == 0)
1073 msg.rtm.rtm_flags |= RTF_CLONING;
David Warde6f148e2009-12-03 21:43:11 +03001074#endif /* RTF_CLONING */
paul718e3742002-12-13 20:15:29 +00001075
1076 /* If no protocol specific gateway is specified, use link
1077 address for gateway. */
1078 if (! gate)
1079 {
1080 if (!ifp)
1081 {
Denis Ovsienkodc958242007-08-13 16:03:06 +00001082 char dest_buf[INET_ADDRSTRLEN] = "NULL", mask_buf[INET_ADDRSTRLEN] = "255.255.255.255";
1083 if (dest)
1084 inet_ntop (AF_INET, &dest->sin.sin_addr, dest_buf, INET_ADDRSTRLEN);
1085 if (mask)
1086 inet_ntop (AF_INET, &mask->sin.sin_addr, mask_buf, INET_ADDRSTRLEN);
1087 zlog_warn ("%s: %s/%s: gate == NULL and no gateway found for ifindex %d",
1088 __func__, dest_buf, mask_buf, index);
paul718e3742002-12-13 20:15:29 +00001089 return -1;
1090 }
1091 gate = (union sockunion *) & ifp->sdl;
1092 }
1093
1094 if (mask)
1095 msg.rtm.rtm_addrs |= RTA_NETMASK;
1096 else if (message == RTM_ADD)
1097 msg.rtm.rtm_flags |= RTF_HOST;
1098
1099 /* Tagging route with flags */
1100 msg.rtm.rtm_flags |= (RTF_PROTO1);
1101
1102 /* Additional flags. */
1103 if (zebra_flags & ZEBRA_FLAG_BLACKHOLE)
1104 msg.rtm.rtm_flags |= RTF_BLACKHOLE;
hasso81dfcaa2003-05-25 19:21:25 +00001105 if (zebra_flags & ZEBRA_FLAG_REJECT)
1106 msg.rtm.rtm_flags |= RTF_REJECT;
1107
paul718e3742002-12-13 20:15:29 +00001108
paul718e3742002-12-13 20:15:29 +00001109#define SOCKADDRSET(X,R) \
1110 if (msg.rtm.rtm_addrs & (R)) \
1111 { \
paul6fe70d12005-11-12 22:55:10 +00001112 int len = SAROUNDUP (X); \
paul718e3742002-12-13 20:15:29 +00001113 memcpy (pnt, (caddr_t)(X), len); \
1114 pnt += len; \
1115 }
paul718e3742002-12-13 20:15:29 +00001116
1117 pnt = (caddr_t) msg.buf;
1118
1119 /* Write each socket data into rtm message buffer */
1120 SOCKADDRSET (dest, RTA_DST);
1121 SOCKADDRSET (gate, RTA_GATEWAY);
1122 SOCKADDRSET (mask, RTA_NETMASK);
1123
1124 msg.rtm.rtm_msglen = pnt - (caddr_t) &msg;
1125
1126 ret = write (routing_sock, &msg, msg.rtm.rtm_msglen);
1127
1128 if (ret != msg.rtm.rtm_msglen)
1129 {
1130 if (errno == EEXIST)
1131 return ZEBRA_ERR_RTEXIST;
1132 if (errno == ENETUNREACH)
1133 return ZEBRA_ERR_RTUNREACH;
Denis Ovsienkodc958242007-08-13 16:03:06 +00001134 if (errno == ESRCH)
1135 return ZEBRA_ERR_RTNOEXIST;
paul718e3742002-12-13 20:15:29 +00001136
Denis Ovsienkodc958242007-08-13 16:03:06 +00001137 zlog_warn ("%s: write : %s (%d)", __func__, safe_strerror (errno), errno);
1138 return ZEBRA_ERR_KERNEL;
paul718e3742002-12-13 20:15:29 +00001139 }
Denis Ovsienkodc958242007-08-13 16:03:06 +00001140 return ZEBRA_ERR_NOERROR;
paul718e3742002-12-13 20:15:29 +00001141}
1142
David Lamparter6b0655a2014-06-04 06:53:35 +02001143
paul718e3742002-12-13 20:15:29 +00001144#include "thread.h"
1145#include "zebra/zserv.h"
1146
paul718e3742002-12-13 20:15:29 +00001147/* For debug purpose. */
ajsb6178002004-12-07 21:12:56 +00001148static void
paul718e3742002-12-13 20:15:29 +00001149rtmsg_debug (struct rt_msghdr *rtm)
1150{
Denis Ovsienko2d844522007-09-14 11:31:55 +00001151 zlog_debug ("Kernel: Len: %d Type: %s", rtm->rtm_msglen, lookup (rtm_type_str, rtm->rtm_type));
paul718e3742002-12-13 20:15:29 +00001152 rtm_flag_dump (rtm->rtm_flags);
ajsb6178002004-12-07 21:12:56 +00001153 zlog_debug ("Kernel: message seq %d", rtm->rtm_seq);
paul6fe70d12005-11-12 22:55:10 +00001154 zlog_debug ("Kernel: pid %d, rtm_addrs 0x%x", rtm->rtm_pid, rtm->rtm_addrs);
paul718e3742002-12-13 20:15:29 +00001155}
1156
1157/* This is pretty gross, better suggestions welcome -- mhandler */
1158#ifndef RTAX_MAX
1159#ifdef RTA_NUMBITS
1160#define RTAX_MAX RTA_NUMBITS
1161#else
1162#define RTAX_MAX 8
1163#endif /* RTA_NUMBITS */
1164#endif /* RTAX_MAX */
1165
1166/* Kernel routing table and interface updates via routing socket. */
paul6621ca82005-11-23 13:02:08 +00001167static int
paul718e3742002-12-13 20:15:29 +00001168kernel_read (struct thread *thread)
1169{
1170 int sock;
1171 int nbytes;
1172 struct rt_msghdr *rtm;
1173
gdtdbee01f2004-01-06 00:36:51 +00001174 /*
1175 * This must be big enough for any message the kernel might send.
gdtb27900b2004-01-08 15:44:29 +00001176 * Rather than determining how many sockaddrs of what size might be
1177 * in each particular message, just use RTAX_MAX of sockaddr_storage
1178 * for each. Note that the sockaddrs must be after each message
1179 * definition, or rather after whichever happens to be the largest,
1180 * since the buffer needs to be big enough for a message and the
1181 * sockaddrs together.
gdtdbee01f2004-01-06 00:36:51 +00001182 */
paul718e3742002-12-13 20:15:29 +00001183 union
1184 {
1185 /* Routing information. */
1186 struct
1187 {
1188 struct rt_msghdr rtm;
gdtb27900b2004-01-08 15:44:29 +00001189 struct sockaddr_storage addr[RTAX_MAX];
paul718e3742002-12-13 20:15:29 +00001190 } r;
1191
1192 /* Interface information. */
1193 struct
1194 {
1195 struct if_msghdr ifm;
gdtb27900b2004-01-08 15:44:29 +00001196 struct sockaddr_storage addr[RTAX_MAX];
paul718e3742002-12-13 20:15:29 +00001197 } im;
1198
1199 /* Interface address information. */
1200 struct
1201 {
1202 struct ifa_msghdr ifa;
gdtb27900b2004-01-08 15:44:29 +00001203 struct sockaddr_storage addr[RTAX_MAX];
paul718e3742002-12-13 20:15:29 +00001204 } ia;
1205
1206#ifdef RTM_IFANNOUNCE
1207 /* Interface arrival/departure */
1208 struct
1209 {
1210 struct if_announcemsghdr ifan;
gdtb27900b2004-01-08 15:44:29 +00001211 struct sockaddr_storage addr[RTAX_MAX];
paul718e3742002-12-13 20:15:29 +00001212 } ian;
1213#endif /* RTM_IFANNOUNCE */
1214
1215 } buf;
1216
1217 /* Fetch routing socket. */
1218 sock = THREAD_FD (thread);
1219
1220 nbytes= read (sock, &buf, sizeof buf);
1221
1222 if (nbytes <= 0)
1223 {
1224 if (nbytes < 0 && errno != EWOULDBLOCK && errno != EAGAIN)
ajs6099b3b2004-11-20 02:06:59 +00001225 zlog_warn ("routing socket error: %s", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001226 return 0;
1227 }
1228
paul9bcdb632003-07-08 08:09:45 +00001229 thread_add_read (zebrad.master, kernel_read, NULL, sock);
paul718e3742002-12-13 20:15:29 +00001230
hasso726f9b22003-05-25 21:04:54 +00001231 if (IS_ZEBRA_DEBUG_KERNEL)
1232 rtmsg_debug (&buf.r.rtm);
paul718e3742002-12-13 20:15:29 +00001233
1234 rtm = &buf.r.rtm;
1235
gdtb27900b2004-01-08 15:44:29 +00001236 /*
1237 * Ensure that we didn't drop any data, so that processing routines
1238 * can assume they have the whole message.
1239 */
gdtda26e3b2004-01-05 17:20:59 +00001240 if (rtm->rtm_msglen != nbytes)
1241 {
1242 zlog_warn ("kernel_read: rtm->rtm_msglen %d, nbytes %d, type %d\n",
1243 rtm->rtm_msglen, nbytes, rtm->rtm_type);
1244 return -1;
1245 }
1246
paul718e3742002-12-13 20:15:29 +00001247 switch (rtm->rtm_type)
1248 {
1249 case RTM_ADD:
1250 case RTM_DELETE:
paulca162182005-09-12 16:58:52 +00001251 case RTM_CHANGE:
paul718e3742002-12-13 20:15:29 +00001252 rtm_read (rtm);
1253 break;
1254 case RTM_IFINFO:
1255 ifm_read (&buf.im.ifm);
1256 break;
1257 case RTM_NEWADDR:
1258 case RTM_DELADDR:
1259 ifam_read (&buf.ia.ifa);
1260 break;
1261#ifdef RTM_IFANNOUNCE
1262 case RTM_IFANNOUNCE:
1263 ifan_read (&buf.ian.ifan);
1264 break;
1265#endif /* RTM_IFANNOUNCE */
1266 default:
hasso726f9b22003-05-25 21:04:54 +00001267 if (IS_ZEBRA_DEBUG_KERNEL)
ajsb6178002004-12-07 21:12:56 +00001268 zlog_debug("Unprocessed RTM_type: %d", rtm->rtm_type);
paul718e3742002-12-13 20:15:29 +00001269 break;
1270 }
1271 return 0;
1272}
1273
1274/* Make routing socket. */
paul6621ca82005-11-23 13:02:08 +00001275static void
1276routing_socket (void)
paul718e3742002-12-13 20:15:29 +00001277{
pauledd7c242003-06-04 13:59:38 +00001278 if ( zserv_privs.change (ZPRIVS_RAISE) )
1279 zlog_err ("routing_socket: Can't raise privileges");
1280
paul718e3742002-12-13 20:15:29 +00001281 routing_sock = socket (AF_ROUTE, SOCK_RAW, 0);
1282
1283 if (routing_sock < 0)
1284 {
pauledd7c242003-06-04 13:59:38 +00001285 if ( zserv_privs.change (ZPRIVS_LOWER) )
1286 zlog_err ("routing_socket: Can't lower privileges");
paul718e3742002-12-13 20:15:29 +00001287 zlog_warn ("Can't init kernel routing socket");
1288 return;
1289 }
1290
paul865b8522005-01-05 08:30:35 +00001291 /* XXX: Socket should be NONBLOCK, however as we currently
1292 * discard failed writes, this will lead to inconsistencies.
1293 * For now, socket must be blocking.
1294 */
1295 /*if (fcntl (routing_sock, F_SETFL, O_NONBLOCK) < 0)
1296 zlog_warn ("Can't set O_NONBLOCK to routing socket");*/
1297
pauledd7c242003-06-04 13:59:38 +00001298 if ( zserv_privs.change (ZPRIVS_LOWER) )
1299 zlog_err ("routing_socket: Can't lower privileges");
paul718e3742002-12-13 20:15:29 +00001300
1301 /* kernel_read needs rewrite. */
paul9bcdb632003-07-08 08:09:45 +00001302 thread_add_read (zebrad.master, kernel_read, NULL, routing_sock);
paul718e3742002-12-13 20:15:29 +00001303}
1304
1305/* Exported interface function. This function simply calls
1306 routing_socket (). */
1307void
paul6621ca82005-11-23 13:02:08 +00001308kernel_init (void)
paul718e3742002-12-13 20:15:29 +00001309{
1310 routing_socket ();
1311}