blob: fefccdceba550d40d5dd8811b08239f4257a5811 [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"
paul718e3742002-12-13 20:15:29 +000035
36#include "zebra/interface.h"
37#include "zebra/zserv.h"
38#include "zebra/debug.h"
paulec1a4282005-11-24 15:15:17 +000039#include "zebra/kernel_socket.h"
paul718e3742002-12-13 20:15:29 +000040
pauledd7c242003-06-04 13:59:38 +000041extern struct zebra_privs_t zserv_privs;
paul9bcdb632003-07-08 08:09:45 +000042extern struct zebra_t zebrad;
pauledd7c242003-06-04 13:59:38 +000043
gdt4bfbea82004-01-06 01:13:05 +000044/*
Greg Troxel273b1bd2014-12-02 14:51:49 -050045 * Historically, the BSD routing socket has aligned data following a
46 * struct sockaddr to sizeof(long), which was 4 bytes on some
47 * platforms, and 8 bytes on others. NetBSD 6 changed the routing
48 * socket to align to sizeof(uint64_t), which is 8 bytes. OS X
49 * appears to align to sizeof(int), which is 4 bytes.
gdt4bfbea82004-01-06 01:13:05 +000050 *
Greg Troxel273b1bd2014-12-02 14:51:49 -050051 * Alignment of zero-sized sockaddrs is nonsensical, but historically
52 * BSD defines RT_ROUNDUP(0) to be the alignment interval (rather than
53 * 0). We follow this practice without questioning it, but it is a
54 * bug if quagga calls ROUNDUP with 0.
gdt4bfbea82004-01-06 01:13:05 +000055 */
Greg Troxel273b1bd2014-12-02 14:51:49 -050056
57/*
58 * Because of these varying conventions, the only sane approach is for
59 * the <net/route.h> header to define some flavor of ROUNDUP macro.
60 */
David Lamparter7e923222015-03-03 21:04:20 +010061
62#if defined(SA_SIZE)
63/* SAROUNDUP is the only thing we need, and SA_SIZE provides that */
64#define SAROUNDUP(a) SA_SIZE(a)
65#else /* !SA_SIZE */
66
Greg Troxel273b1bd2014-12-02 14:51:49 -050067#if defined(RT_ROUNDUP)
68#define ROUNDUP(a) RT_ROUNDUP(a)
69#endif /* defined(RT_ROUNDUP) */
70
71/*
72 * If ROUNDUP has not yet been defined in terms of platform-provided
73 * defines, attempt to cope with heuristics.
74 */
75#if !defined(ROUNDUP)
76
77/*
78 * It's a bug for a platform not to define rounding/alignment for
79 * sockaddrs on the routing socket. This warning really is
80 * intentional, to provoke filing bug reports with operating systems
81 * that don't define RT_ROUNDUP or equivalent.
82 */
83#warning "net/route.h does not define RT_ROUNDUP; making unwarranted assumptions!"
84
85/* OS X (Xcode as of 2014-12) is known not to define RT_ROUNDUP */
Doug VanLeuven3b33de62012-10-10 22:10:14 +000086#ifdef __APPLE__
Greg Troxel273b1bd2014-12-02 14:51:49 -050087#define ROUNDUP_TYPE int
Greg Troxel941789e2015-03-23 15:16:29 -040088#else
89#define ROUNDUP_TYPE long
Doug VanLeuven3b33de62012-10-10 22:10:14 +000090#endif
paul718e3742002-12-13 20:15:29 +000091
Greg Troxel273b1bd2014-12-02 14:51:49 -050092#define ROUNDUP(a) \
93 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(ROUNDUP_TYPE) - 1))) : sizeof(ROUNDUP_TYPE))
94
95#endif /* defined(ROUNDUP) */
96
gdt4bfbea82004-01-06 01:13:05 +000097/*
98 * Given a pointer (sockaddr or void *), return the number of bytes
99 * taken up by the sockaddr and any padding needed for alignment.
100 */
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000101#if defined(HAVE_STRUCT_SOCKADDR_SA_LEN)
gdt4bfbea82004-01-06 01:13:05 +0000102#define SAROUNDUP(X) ROUNDUP(((struct sockaddr *)(X))->sa_len)
paul30be8022003-10-22 02:51:38 +0000103#elif defined(HAVE_IPV6)
gdt4bfbea82004-01-06 01:13:05 +0000104/*
105 * One would hope all fixed-size structure definitions are aligned,
106 * but round them up nonetheless.
107 */
108#define SAROUNDUP(X) \
paul3e95a072003-09-24 00:05:45 +0000109 (((struct sockaddr *)(X))->sa_family == AF_INET ? \
110 ROUNDUP(sizeof(struct sockaddr_in)):\
111 (((struct sockaddr *)(X))->sa_family == AF_INET6 ? \
112 ROUNDUP(sizeof(struct sockaddr_in6)) : \
113 (((struct sockaddr *)(X))->sa_family == AF_LINK ? \
paulc50ae8b2004-05-11 11:31:07 +0000114 ROUNDUP(sizeof(struct sockaddr_dl)) : sizeof(struct sockaddr))))
paul30be8022003-10-22 02:51:38 +0000115#else /* HAVE_IPV6 */
gdt4bfbea82004-01-06 01:13:05 +0000116#define SAROUNDUP(X) \
paul30be8022003-10-22 02:51:38 +0000117 (((struct sockaddr *)(X))->sa_family == AF_INET ? \
118 ROUNDUP(sizeof(struct sockaddr_in)):\
119 (((struct sockaddr *)(X))->sa_family == AF_LINK ? \
120 ROUNDUP(sizeof(struct sockaddr_dl)) : sizeof(struct sockaddr)))
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000121#endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
paul718e3742002-12-13 20:15:29 +0000122
David Lamparter7e923222015-03-03 21:04:20 +0100123#endif /* !SA_SIZE */
124
Doug VanLeuvena05df8f2012-10-10 16:11:36 -0700125/*
126 * We use a call to an inline function to copy (PNT) to (DEST)
127 * 1. Calculating the length of the copy requires an #ifdef to determine
128 * if sa_len is a field and can't be used directly inside a #define
129 * 2. So the compiler doesn't complain when DEST is NULL, which is only true
130 * when we are skipping the copy and incrementing to the next SA
paulec1a4282005-11-24 15:15:17 +0000131 */
David Lamparter3e9e2c92015-04-10 09:14:58 +0200132static inline void
Doug VanLeuvena05df8f2012-10-10 16:11:36 -0700133rta_copy (union sockunion *dest, caddr_t src) {
134 int len;
135#ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
136 len = (((struct sockaddr *)src)->sa_len > sizeof (*dest)) ?
137 sizeof (*dest) : ((struct sockaddr *)src)->sa_len ;
138#else
139 len = (SAROUNDUP (src) > sizeof (*dest)) ?
140 sizeof (*dest) : SAROUNDUP (src) ;
141#endif
142 memcpy (dest, src, len);
143}
144
paul62debbb2005-06-14 14:07:07 +0000145#define RTA_ADDR_GET(DEST, RTA, RTMADDRS, PNT) \
146 if ((RTMADDRS) & (RTA)) \
147 { \
148 int len = SAROUNDUP ((PNT)); \
paulea6f82b2005-06-28 17:20:26 +0000149 if ( ((DEST) != NULL) && \
paul62debbb2005-06-14 14:07:07 +0000150 af_check (((struct sockaddr *)(PNT))->sa_family)) \
Doug VanLeuvena05df8f2012-10-10 16:11:36 -0700151 rta_copy((DEST), (PNT)); \
paul62debbb2005-06-14 14:07:07 +0000152 (PNT) += len; \
153 }
154#define RTA_ATTR_GET(DEST, RTA, RTMADDRS, PNT) \
155 if ((RTMADDRS) & (RTA)) \
156 { \
157 int len = SAROUNDUP ((PNT)); \
paulec1a4282005-11-24 15:15:17 +0000158 if ((DEST) != NULL) \
Doug VanLeuvena05df8f2012-10-10 16:11:36 -0700159 rta_copy((DEST), (PNT)); \
paul62debbb2005-06-14 14:07:07 +0000160 (PNT) += len; \
161 }
162
paul6fe70d12005-11-12 22:55:10 +0000163#define RTA_NAME_GET(DEST, RTA, RTMADDRS, PNT, LEN) \
164 if ((RTMADDRS) & (RTA)) \
165 { \
paulec1a4282005-11-24 15:15:17 +0000166 u_char *pdest = (u_char *) (DEST); \
paul6fe70d12005-11-12 22:55:10 +0000167 int len = SAROUNDUP ((PNT)); \
168 struct sockaddr_dl *sdl = (struct sockaddr_dl *)(PNT); \
169 if (IS_ZEBRA_DEBUG_KERNEL) \
170 zlog_debug ("%s: RTA_SDL_GET nlen %d, alen %d", \
171 __func__, sdl->sdl_nlen, sdl->sdl_alen); \
172 if ( ((DEST) != NULL) && (sdl->sdl_family == AF_LINK) \
173 && (sdl->sdl_nlen < IFNAMSIZ) && (sdl->sdl_nlen <= len) ) \
174 { \
paulec1a4282005-11-24 15:15:17 +0000175 memcpy (pdest, sdl->sdl_data, sdl->sdl_nlen); \
176 pdest[sdl->sdl_nlen] = '\0'; \
paul6fe70d12005-11-12 22:55:10 +0000177 (LEN) = sdl->sdl_nlen; \
178 } \
179 (PNT) += len; \
180 } \
181 else \
182 { \
183 (LEN) = 0; \
184 }
paul718e3742002-12-13 20:15:29 +0000185/* Routing socket message types. */
Stephen Hemminger1423c802008-08-14 17:59:25 +0100186const struct message rtm_type_str[] =
paul718e3742002-12-13 20:15:29 +0000187{
188 {RTM_ADD, "RTM_ADD"},
189 {RTM_DELETE, "RTM_DELETE"},
190 {RTM_CHANGE, "RTM_CHANGE"},
191 {RTM_GET, "RTM_GET"},
192 {RTM_LOSING, "RTM_LOSING"},
193 {RTM_REDIRECT, "RTM_REDIRECT"},
194 {RTM_MISS, "RTM_MISS"},
195 {RTM_LOCK, "RTM_LOCK"},
Greg Troxel9458b812006-09-13 12:13:08 +0000196#ifdef OLDADD
paul718e3742002-12-13 20:15:29 +0000197 {RTM_OLDADD, "RTM_OLDADD"},
Greg Troxel9458b812006-09-13 12:13:08 +0000198#endif /* RTM_OLDADD */
199#ifdef RTM_OLDDEL
paul718e3742002-12-13 20:15:29 +0000200 {RTM_OLDDEL, "RTM_OLDDEL"},
Greg Troxel9458b812006-09-13 12:13:08 +0000201#endif /* RTM_OLDDEL */
paul718e3742002-12-13 20:15:29 +0000202 {RTM_RESOLVE, "RTM_RESOLVE"},
203 {RTM_NEWADDR, "RTM_NEWADDR"},
204 {RTM_DELADDR, "RTM_DELADDR"},
205 {RTM_IFINFO, "RTM_IFINFO"},
206#ifdef RTM_OIFINFO
207 {RTM_OIFINFO, "RTM_OIFINFO"},
208#endif /* RTM_OIFINFO */
209#ifdef RTM_NEWMADDR
210 {RTM_NEWMADDR, "RTM_NEWMADDR"},
211#endif /* RTM_NEWMADDR */
212#ifdef RTM_DELMADDR
213 {RTM_DELMADDR, "RTM_DELMADDR"},
214#endif /* RTM_DELMADDR */
215#ifdef RTM_IFANNOUNCE
216 {RTM_IFANNOUNCE, "RTM_IFANNOUNCE"},
217#endif /* RTM_IFANNOUNCE */
218 {0, NULL}
219};
220
Stephen Hemmingerce0db9c2009-05-15 10:47:04 -0700221static const struct message rtm_flag_str[] =
paul718e3742002-12-13 20:15:29 +0000222{
223 {RTF_UP, "UP"},
224 {RTF_GATEWAY, "GATEWAY"},
225 {RTF_HOST, "HOST"},
226 {RTF_REJECT, "REJECT"},
227 {RTF_DYNAMIC, "DYNAMIC"},
228 {RTF_MODIFIED, "MODIFIED"},
229 {RTF_DONE, "DONE"},
230#ifdef RTF_MASK
231 {RTF_MASK, "MASK"},
232#endif /* RTF_MASK */
David Warde6f148e2009-12-03 21:43:11 +0300233#ifdef RTF_CLONING
paul718e3742002-12-13 20:15:29 +0000234 {RTF_CLONING, "CLONING"},
David Warde6f148e2009-12-03 21:43:11 +0300235#endif /* RTF_CLONING */
paul718e3742002-12-13 20:15:29 +0000236 {RTF_XRESOLVE, "XRESOLVE"},
237 {RTF_LLINFO, "LLINFO"},
238 {RTF_STATIC, "STATIC"},
239 {RTF_BLACKHOLE, "BLACKHOLE"},
paul6fe70d12005-11-12 22:55:10 +0000240#ifdef RTF_PRIVATE
241 {RTF_PRIVATE, "PRIVATE"},
242#endif /* RTF_PRIVATE */
paul718e3742002-12-13 20:15:29 +0000243 {RTF_PROTO1, "PROTO1"},
244 {RTF_PROTO2, "PROTO2"},
245#ifdef RTF_PRCLONING
246 {RTF_PRCLONING, "PRCLONING"},
247#endif /* RTF_PRCLONING */
248#ifdef RTF_WASCLONED
249 {RTF_WASCLONED, "WASCLONED"},
250#endif /* RTF_WASCLONED */
251#ifdef RTF_PROTO3
252 {RTF_PROTO3, "PROTO3"},
253#endif /* RTF_PROTO3 */
254#ifdef RTF_PINNED
255 {RTF_PINNED, "PINNED"},
256#endif /* RTF_PINNED */
257#ifdef RTF_LOCAL
258 {RTF_LOCAL, "LOCAL"},
259#endif /* RTF_LOCAL */
260#ifdef RTF_BROADCAST
261 {RTF_BROADCAST, "BROADCAST"},
262#endif /* RTF_BROADCAST */
263#ifdef RTF_MULTICAST
264 {RTF_MULTICAST, "MULTICAST"},
265#endif /* RTF_MULTICAST */
paul6fe70d12005-11-12 22:55:10 +0000266#ifdef RTF_MULTIRT
267 {RTF_MULTIRT, "MULTIRT"},
268#endif /* RTF_MULTIRT */
269#ifdef RTF_SETSRC
270 {RTF_SETSRC, "SETSRC"},
271#endif /* RTF_SETSRC */
paul718e3742002-12-13 20:15:29 +0000272 {0, NULL}
273};
274
275/* Kernel routing update socket. */
276int routing_sock = -1;
277
278/* Yes I'm checking ugly routing socket behavior. */
279/* #define DEBUG */
280
281/* Supported address family check. */
David Lamparter3e9e2c92015-04-10 09:14:58 +0200282static inline int
paul718e3742002-12-13 20:15:29 +0000283af_check (int family)
284{
285 if (family == AF_INET)
286 return 1;
287#ifdef HAVE_IPV6
288 if (family == AF_INET6)
289 return 1;
290#endif /* HAVE_IPV6 */
291 return 0;
292}
David Lamparter6b0655a2014-06-04 06:53:35 +0200293
paul718e3742002-12-13 20:15:29 +0000294/* Dump routing table flag for debug purpose. */
ajsb6178002004-12-07 21:12:56 +0000295static void
paul718e3742002-12-13 20:15:29 +0000296rtm_flag_dump (int flag)
297{
Tom Goff80b2a942009-12-03 14:53:15 +0300298 const struct message *mes;
paul718e3742002-12-13 20:15:29 +0000299 static char buf[BUFSIZ];
300
gdtcced60d2004-07-13 16:45:54 +0000301 buf[0] = '\0';
paul718e3742002-12-13 20:15:29 +0000302 for (mes = rtm_flag_str; mes->key != 0; mes++)
303 {
304 if (mes->key & flag)
305 {
306 strlcat (buf, mes->str, BUFSIZ);
307 strlcat (buf, " ", BUFSIZ);
308 }
309 }
ajsb6178002004-12-07 21:12:56 +0000310 zlog_debug ("Kernel: %s", buf);
paul718e3742002-12-13 20:15:29 +0000311}
312
313#ifdef RTM_IFANNOUNCE
314/* Interface adding function */
paul6621ca82005-11-23 13:02:08 +0000315static int
paul718e3742002-12-13 20:15:29 +0000316ifan_read (struct if_announcemsghdr *ifan)
317{
318 struct interface *ifp;
paul6fe70d12005-11-12 22:55:10 +0000319
paul718e3742002-12-13 20:15:29 +0000320 ifp = if_lookup_by_index (ifan->ifan_index);
paul6fe70d12005-11-12 22:55:10 +0000321
322 if (ifp)
323 assert ( (ifp->ifindex == ifan->ifan_index)
324 || (ifp->ifindex == IFINDEX_INTERNAL) );
325
paulec1a4282005-11-24 15:15:17 +0000326 if ( (ifp == NULL)
327 || ((ifp->ifindex == IFINDEX_INTERNAL)
328 && (ifan->ifan_what == IFAN_ARRIVAL)) )
paul718e3742002-12-13 20:15:29 +0000329 {
paul6fe70d12005-11-12 22:55:10 +0000330 if (IS_ZEBRA_DEBUG_KERNEL)
331 zlog_debug ("%s: creating interface for ifindex %d, name %s",
332 __func__, ifan->ifan_index, ifan->ifan_name);
333
paul718e3742002-12-13 20:15:29 +0000334 /* Create Interface */
ajs08dbfb62005-04-03 03:40:52 +0000335 ifp = if_get_by_name_len(ifan->ifan_name,
336 strnlen(ifan->ifan_name,
337 sizeof(ifan->ifan_name)));
paul718e3742002-12-13 20:15:29 +0000338 ifp->ifindex = ifan->ifan_index;
339
Ingo Flaschberger1db65fa2011-04-17 18:28:20 +0000340 if_get_metric (ifp);
paul718e3742002-12-13 20:15:29 +0000341 if_add_update (ifp);
342 }
343 else if (ifp != NULL && ifan->ifan_what == IFAN_DEPARTURE)
paul6eb88272005-07-29 14:36:00 +0000344 if_delete_update (ifp);
paul718e3742002-12-13 20:15:29 +0000345
346 if_get_flags (ifp);
347 if_get_mtu (ifp);
348 if_get_metric (ifp);
349
350 if (IS_ZEBRA_DEBUG_KERNEL)
paul6fe70d12005-11-12 22:55:10 +0000351 zlog_debug ("%s: interface %s index %d",
352 __func__, ifan->ifan_name, ifan->ifan_index);
paul718e3742002-12-13 20:15:29 +0000353
354 return 0;
355}
356#endif /* RTM_IFANNOUNCE */
357
Doug VanLeuven9234b382012-10-10 16:12:32 -0700358#ifdef HAVE_BSD_IFI_LINK_STATE
Andrew J. Schorrc543a172008-01-10 15:24:32 +0000359/* BSD link detect translation */
360static void
361bsd_linkdetect_translate (struct if_msghdr *ifm)
362{
Andrew J. Schorr55edb0d2008-01-11 15:57:13 +0000363 if ((ifm->ifm_data.ifi_link_state >= LINK_STATE_UP) ||
364 (ifm->ifm_data.ifi_link_state == LINK_STATE_UNKNOWN))
Andrew J. Schorrc543a172008-01-10 15:24:32 +0000365 SET_FLAG(ifm->ifm_flags, IFF_RUNNING);
366 else
367 UNSET_FLAG(ifm->ifm_flags, IFF_RUNNING);
368}
Doug VanLeuven9234b382012-10-10 16:12:32 -0700369#endif /* HAVE_BSD_IFI_LINK_STATE */
Andrew J. Schorrc543a172008-01-10 15:24:32 +0000370
gdtda26e3b2004-01-05 17:20:59 +0000371/*
372 * Handle struct if_msghdr obtained from reading routing socket or
373 * sysctl (from interface_list). There may or may not be sockaddrs
374 * present after the header.
375 */
paulec1a4282005-11-24 15:15:17 +0000376int
paul718e3742002-12-13 20:15:29 +0000377ifm_read (struct if_msghdr *ifm)
378{
paul3e95a072003-09-24 00:05:45 +0000379 struct interface *ifp = NULL;
Tom Goffa34eb362009-11-25 20:36:06 +0000380 struct sockaddr_dl *sdl;
paul6fe70d12005-11-12 22:55:10 +0000381 char ifname[IFNAMSIZ];
382 short ifnlen = 0;
Doug VanLeuvena05df8f2012-10-10 16:11:36 -0700383 caddr_t cp;
paul6fe70d12005-11-12 22:55:10 +0000384
385 /* terminate ifname at head (for strnlen) and tail (for safety) */
386 ifname[IFNAMSIZ - 1] = '\0';
387
gdtda26e3b2004-01-05 17:20:59 +0000388 /* paranoia: sanity check structure */
389 if (ifm->ifm_msglen < sizeof(struct if_msghdr))
390 {
391 zlog_err ("ifm_read: ifm->ifm_msglen %d too short\n",
392 ifm->ifm_msglen);
393 return -1;
394 }
395
396 /*
gdt4bfbea82004-01-06 01:13:05 +0000397 * Check for a sockaddr_dl following the message. First, point to
398 * where a socakddr might be if one follows the message.
gdtda26e3b2004-01-05 17:20:59 +0000399 */
gdt4bfbea82004-01-06 01:13:05 +0000400 cp = (void *)(ifm + 1);
401
paul3e95a072003-09-24 00:05:45 +0000402#ifdef SUNOS_5
paul3e95a072003-09-24 00:05:45 +0000403 /*
gdt4bfbea82004-01-06 01:13:05 +0000404 * XXX This behavior should be narrowed to only the kernel versions
405 * for which the structures returned do not match the headers.
406 *
paul3e95a072003-09-24 00:05:45 +0000407 * if_msghdr_t on 64 bit kernels in Solaris 9 and earlier versions
gdt4bfbea82004-01-06 01:13:05 +0000408 * is 12 bytes larger than the 32 bit version.
paul3e95a072003-09-24 00:05:45 +0000409 */
gdt4bfbea82004-01-06 01:13:05 +0000410 if (((struct sockaddr *) cp)->sa_family == AF_UNSPEC)
paul3e95a072003-09-24 00:05:45 +0000411 cp = cp + 12;
paul3e95a072003-09-24 00:05:45 +0000412#endif
paul718e3742002-12-13 20:15:29 +0000413
paul6fe70d12005-11-12 22:55:10 +0000414 RTA_ADDR_GET (NULL, RTA_DST, ifm->ifm_addrs, cp);
415 RTA_ADDR_GET (NULL, RTA_GATEWAY, ifm->ifm_addrs, cp);
416 RTA_ATTR_GET (NULL, RTA_NETMASK, ifm->ifm_addrs, cp);
417 RTA_ADDR_GET (NULL, RTA_GENMASK, ifm->ifm_addrs, cp);
Tom Goffa34eb362009-11-25 20:36:06 +0000418 sdl = (struct sockaddr_dl *)cp;
paul6fe70d12005-11-12 22:55:10 +0000419 RTA_NAME_GET (ifname, RTA_IFP, ifm->ifm_addrs, cp, ifnlen);
420 RTA_ADDR_GET (NULL, RTA_IFA, ifm->ifm_addrs, cp);
421 RTA_ADDR_GET (NULL, RTA_AUTHOR, ifm->ifm_addrs, cp);
422 RTA_ADDR_GET (NULL, RTA_BRD, ifm->ifm_addrs, cp);
423
424 if (IS_ZEBRA_DEBUG_KERNEL)
425 zlog_debug ("%s: sdl ifname %s", __func__, (ifnlen ? ifname : "(nil)"));
426
gdt4bfbea82004-01-06 01:13:05 +0000427 /*
paul6fe70d12005-11-12 22:55:10 +0000428 * Look up on ifindex first, because ifindices are the primary handle for
429 * interfaces across the user/kernel boundary, for most systems. (Some
430 * messages, such as up/down status changes on NetBSD, do not include a
431 * sockaddr_dl).
gdt4bfbea82004-01-06 01:13:05 +0000432 */
paul6fe70d12005-11-12 22:55:10 +0000433 if ( (ifp = if_lookup_by_index (ifm->ifm_index)) != NULL )
gdt4bfbea82004-01-06 01:13:05 +0000434 {
paul6fe70d12005-11-12 22:55:10 +0000435 /* we have an ifp, verify that the name matches as some systems,
436 * eg Solaris, have a 1:many association of ifindex:ifname
437 * if they dont match, we dont have the correct ifp and should
438 * set it back to NULL to let next check do lookup by name
439 */
440 if (ifnlen && (strncmp (ifp->name, ifname, IFNAMSIZ) != 0) )
gdt4bfbea82004-01-06 01:13:05 +0000441 {
paul6fe70d12005-11-12 22:55:10 +0000442 if (IS_ZEBRA_DEBUG_KERNEL)
443 zlog_debug ("%s: ifp name %s doesnt match sdl name %s",
444 __func__, ifp->name, ifname);
445 ifp = NULL;
gdt4bfbea82004-01-06 01:13:05 +0000446 }
447 }
paul6fe70d12005-11-12 22:55:10 +0000448
gdtda26e3b2004-01-05 17:20:59 +0000449 /*
paul6fe70d12005-11-12 22:55:10 +0000450 * If we dont have an ifp, try looking up by name. Particularly as some
451 * systems (Solaris) have a 1:many mapping of ifindex:ifname - the ifname
452 * is therefore our unique handle to that interface.
453 *
454 * Interfaces specified in the configuration file for which the ifindex
455 * has not been determined will have ifindex == IFINDEX_INTERNAL, and such
456 * interfaces are found by this search, and then their ifindex values can
457 * be filled in.
gdtda26e3b2004-01-05 17:20:59 +0000458 */
paul6fe70d12005-11-12 22:55:10 +0000459 if ( (ifp == NULL) && ifnlen)
460 ifp = if_lookup_by_name (ifname);
paul718e3742002-12-13 20:15:29 +0000461
gdtda26e3b2004-01-05 17:20:59 +0000462 /*
paul6fe70d12005-11-12 22:55:10 +0000463 * If ifp still does not exist or has an invalid index (IFINDEX_INTERNAL),
464 * create or fill in an interface.
gdtda26e3b2004-01-05 17:20:59 +0000465 */
ajsd2fc8892005-04-02 18:38:43 +0000466 if ((ifp == NULL) || (ifp->ifindex == IFINDEX_INTERNAL))
paul718e3742002-12-13 20:15:29 +0000467 {
gdt4bfbea82004-01-06 01:13:05 +0000468 /*
469 * To create or fill in an interface, a sockaddr_dl (via
470 * RTA_IFP) is required.
471 */
paul6fe70d12005-11-12 22:55:10 +0000472 if (!ifnlen)
paul718e3742002-12-13 20:15:29 +0000473 {
paul6fe70d12005-11-12 22:55:10 +0000474 zlog_warn ("Interface index %d (new) missing ifname\n",
paul718e3742002-12-13 20:15:29 +0000475 ifm->ifm_index);
476 return -1;
477 }
paul5c78b3d2006-01-25 04:31:40 +0000478
479#ifndef RTM_IFANNOUNCE
480 /* Down->Down interface should be ignored here.
481 * See further comment below.
482 */
483 if (!CHECK_FLAG (ifm->ifm_flags, IFF_UP))
484 return 0;
485#endif /* !RTM_IFANNOUNCE */
paul6fe70d12005-11-12 22:55:10 +0000486
paul3e95a072003-09-24 00:05:45 +0000487 if (ifp == NULL)
paul6fe70d12005-11-12 22:55:10 +0000488 {
489 /* Interface that zebra was not previously aware of, so create. */
490 ifp = if_create (ifname, ifnlen);
491 if (IS_ZEBRA_DEBUG_KERNEL)
492 zlog_debug ("%s: creating ifp for ifindex %d",
493 __func__, ifm->ifm_index);
494 }
paul718e3742002-12-13 20:15:29 +0000495
paul6fe70d12005-11-12 22:55:10 +0000496 if (IS_ZEBRA_DEBUG_KERNEL)
497 zlog_debug ("%s: updated/created ifp, ifname %s, ifindex %d",
498 __func__, ifp->name, ifp->ifindex);
gdt4bfbea82004-01-06 01:13:05 +0000499 /*
500 * Fill in newly created interface structure, or larval
ajsd2fc8892005-04-02 18:38:43 +0000501 * structure with ifindex IFINDEX_INTERNAL.
gdt4bfbea82004-01-06 01:13:05 +0000502 */
paul718e3742002-12-13 20:15:29 +0000503 ifp->ifindex = ifm->ifm_index;
Andrew J. Schorrc543a172008-01-10 15:24:32 +0000504
Doug VanLeuven9234b382012-10-10 16:12:32 -0700505#ifdef HAVE_BSD_IFI_LINK_STATE /* translate BSD kernel msg for link-state */
Andrew J. Schorrc543a172008-01-10 15:24:32 +0000506 bsd_linkdetect_translate(ifm);
Doug VanLeuven9234b382012-10-10 16:12:32 -0700507#endif /* HAVE_BSD_IFI_LINK_STATE */
Andrew J. Schorrc543a172008-01-10 15:24:32 +0000508
paul5c78b3d2006-01-25 04:31:40 +0000509 if_flags_update (ifp, ifm->ifm_flags);
paul718e3742002-12-13 20:15:29 +0000510#if defined(__bsdi__)
511 if_kvm_get_mtu (ifp);
512#else
513 if_get_mtu (ifp);
514#endif /* __bsdi__ */
515 if_get_metric (ifp);
516
Tom Goffa34eb362009-11-25 20:36:06 +0000517 /*
518 * XXX sockaddr_dl contents can be larger than the structure
David Lamparterca3ccd82012-09-26 14:52:39 +0200519 * definition. There are 2 big families here:
520 * - BSD has sdl_len + sdl_data[16] + overruns sdl_data
521 * we MUST use sdl_len here or we'll truncate data.
522 * - Solaris has no sdl_len, but sdl_data[244]
523 * presumably, it's not going to run past that, so sizeof()
524 * is fine here.
Tom Goffa34eb362009-11-25 20:36:06 +0000525 * a nonzero ifnlen from RTA_NAME_GET() means sdl is valid
526 */
527 if (ifnlen)
David Lamparterca3ccd82012-09-26 14:52:39 +0200528 {
529#ifdef HAVE_STRUCT_SOCKADDR_DL_SDL_LEN
530 memcpy (&ifp->sdl, sdl, sdl->sdl_len);
531#else
Tom Goffa34eb362009-11-25 20:36:06 +0000532 memcpy (&ifp->sdl, sdl, sizeof (struct sockaddr_dl));
David Lamparterca3ccd82012-09-26 14:52:39 +0200533#endif /* HAVE_STRUCT_SOCKADDR_DL_SDL_LEN */
534 }
Tom Goffa34eb362009-11-25 20:36:06 +0000535
paul718e3742002-12-13 20:15:29 +0000536 if_add_update (ifp);
537 }
538 else
gdtda26e3b2004-01-05 17:20:59 +0000539 /*
540 * Interface structure exists. Adjust stored flags from
541 * notification. If interface has up->down or down->up
542 * transition, call state change routines (to adjust routes,
543 * notify routing daemons, etc.). (Other flag changes are stored
544 * but apparently do not trigger action.)
545 */
paul718e3742002-12-13 20:15:29 +0000546 {
paul6fe70d12005-11-12 22:55:10 +0000547 if (ifp->ifindex != ifm->ifm_index)
548 {
549 zlog_warn ("%s: index mismatch, ifname %s, ifp index %d, "
550 "ifm index %d",
551 __func__, ifp->name, ifp->ifindex, ifm->ifm_index);
552 return -1;
553 }
554
Doug VanLeuven9234b382012-10-10 16:12:32 -0700555#ifdef HAVE_BSD_IFI_LINK_STATE /* translate BSD kernel msg for link-state */
Andrew J. Schorrc543a172008-01-10 15:24:32 +0000556 bsd_linkdetect_translate(ifm);
Doug VanLeuven9234b382012-10-10 16:12:32 -0700557#endif /* HAVE_BSD_IFI_LINK_STATE */
Andrew J. Schorrc543a172008-01-10 15:24:32 +0000558
paul5c78b3d2006-01-25 04:31:40 +0000559 /* update flags and handle operative->inoperative transition, if any */
560 if_flags_update (ifp, ifm->ifm_flags);
561
paul6eb88272005-07-29 14:36:00 +0000562#ifndef RTM_IFANNOUNCE
paul5c78b3d2006-01-25 04:31:40 +0000563 if (!if_is_up (ifp))
564 {
565 /* No RTM_IFANNOUNCE on this platform, so we can never
566 * distinguish between ~IFF_UP and delete. We must presume
567 * it has been deleted.
568 * Eg, Solaris will not notify us of unplumb.
569 *
570 * XXX: Fixme - this should be runtime detected
571 * So that a binary compiled on a system with IFANNOUNCE
572 * will still behave correctly if run on a platform without
573 */
574 if_delete_update (ifp);
575 }
paul6eb88272005-07-29 14:36:00 +0000576#endif /* RTM_IFANNOUNCE */
Denis Ovsienko1ba27562007-08-21 16:15:39 +0000577 if (if_is_up (ifp))
578 {
579#if defined(__bsdi__)
580 if_kvm_get_mtu (ifp);
581#else
582 if_get_mtu (ifp);
583#endif /* __bsdi__ */
584 if_get_metric (ifp);
585 }
paul718e3742002-12-13 20:15:29 +0000586 }
paul5c78b3d2006-01-25 04:31:40 +0000587
paul718e3742002-12-13 20:15:29 +0000588#ifdef HAVE_NET_RT_IFLIST
589 ifp->stats = ifm->ifm_data;
590#endif /* HAVE_NET_RT_IFLIST */
591
592 if (IS_ZEBRA_DEBUG_KERNEL)
paul6fe70d12005-11-12 22:55:10 +0000593 zlog_debug ("%s: interface %s index %d",
594 __func__, ifp->name, ifp->ifindex);
paul718e3742002-12-13 20:15:29 +0000595
596 return 0;
597}
David Lamparter6b0655a2014-06-04 06:53:35 +0200598
paul718e3742002-12-13 20:15:29 +0000599/* Address read from struct ifa_msghdr. */
paul6621ca82005-11-23 13:02:08 +0000600static void
paul718e3742002-12-13 20:15:29 +0000601ifam_read_mesg (struct ifa_msghdr *ifm,
602 union sockunion *addr,
603 union sockunion *mask,
paul6fe70d12005-11-12 22:55:10 +0000604 union sockunion *brd,
605 char *ifname,
606 short *ifnlen)
paul718e3742002-12-13 20:15:29 +0000607{
608 caddr_t pnt, end;
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000609 union sockunion dst;
610 union sockunion gateway;
paul718e3742002-12-13 20:15:29 +0000611
612 pnt = (caddr_t)(ifm + 1);
613 end = ((caddr_t)ifm) + ifm->ifam_msglen;
614
paul718e3742002-12-13 20:15:29 +0000615 /* Be sure structure is cleared */
616 memset (mask, 0, sizeof (union sockunion));
617 memset (addr, 0, sizeof (union sockunion));
paul6621ca82005-11-23 13:02:08 +0000618 memset (brd, 0, sizeof (union sockunion));
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000619 memset (&dst, 0, sizeof (union sockunion));
620 memset (&gateway, 0, sizeof (union sockunion));
paul718e3742002-12-13 20:15:29 +0000621
622 /* We fetch each socket variable into sockunion. */
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000623 RTA_ADDR_GET (&dst, RTA_DST, ifm->ifam_addrs, pnt);
624 RTA_ADDR_GET (&gateway, RTA_GATEWAY, ifm->ifam_addrs, pnt);
paul62debbb2005-06-14 14:07:07 +0000625 RTA_ATTR_GET (mask, RTA_NETMASK, ifm->ifam_addrs, pnt);
626 RTA_ADDR_GET (NULL, RTA_GENMASK, ifm->ifam_addrs, pnt);
paul6fe70d12005-11-12 22:55:10 +0000627 RTA_NAME_GET (ifname, RTA_IFP, ifm->ifam_addrs, pnt, *ifnlen);
paul62debbb2005-06-14 14:07:07 +0000628 RTA_ADDR_GET (addr, RTA_IFA, ifm->ifam_addrs, pnt);
629 RTA_ADDR_GET (NULL, RTA_AUTHOR, ifm->ifam_addrs, pnt);
paul6fe70d12005-11-12 22:55:10 +0000630 RTA_ADDR_GET (brd, RTA_BRD, ifm->ifam_addrs, pnt);
paul718e3742002-12-13 20:15:29 +0000631
paul6fe70d12005-11-12 22:55:10 +0000632 if (IS_ZEBRA_DEBUG_KERNEL)
Andrew J. Schorr55196042006-05-17 15:04:59 +0000633 {
Timo Teräsbe6335d2015-05-23 11:08:41 +0300634 int family = sockunion_family(addr);
635 switch (family)
Andrew J. Schorr55196042006-05-17 15:04:59 +0000636 {
637 case AF_INET:
Timo Teräsbe6335d2015-05-23 11:08:41 +0300638#ifdef HAVE_IPV6
639 case AF_INET6:
640#endif
Andrew J. Schorr55196042006-05-17 15:04:59 +0000641 {
Timo Teräsbe6335d2015-05-23 11:08:41 +0300642 char buf[4][INET6_ADDRSTRLEN];
Andrew J. Schorr55196042006-05-17 15:04:59 +0000643 zlog_debug ("%s: ifindex %d, ifname %s, ifam_addrs 0x%x, "
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000644 "ifam_flags 0x%x, addr %s/%d broad %s dst %s "
645 "gateway %s",
646 __func__, ifm->ifam_index,
Andrew J. Schorr55196042006-05-17 15:04:59 +0000647 (ifnlen ? ifname : "(nil)"), ifm->ifam_addrs,
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000648 ifm->ifam_flags,
Timo Teräsbe6335d2015-05-23 11:08:41 +0300649 inet_ntop(family,&addr->sin.sin_addr,
Andrew J. Schorr55196042006-05-17 15:04:59 +0000650 buf[0],sizeof(buf[0])),
651 ip_masklen(mask->sin.sin_addr),
Timo Teräsbe6335d2015-05-23 11:08:41 +0300652 inet_ntop(family,&brd->sin.sin_addr,
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000653 buf[1],sizeof(buf[1])),
Timo Teräsbe6335d2015-05-23 11:08:41 +0300654 inet_ntop(family,&dst.sin.sin_addr,
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000655 buf[2],sizeof(buf[2])),
Timo Teräsbe6335d2015-05-23 11:08:41 +0300656 inet_ntop(family,&gateway.sin.sin_addr,
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000657 buf[3],sizeof(buf[3])));
Andrew J. Schorr55196042006-05-17 15:04:59 +0000658 }
659 break;
Andrew J. Schorr55196042006-05-17 15:04:59 +0000660 default:
661 zlog_debug ("%s: ifindex %d, ifname %s, ifam_addrs 0x%x",
662 __func__, ifm->ifam_index,
663 (ifnlen ? ifname : "(nil)"), ifm->ifam_addrs);
664 break;
665 }
666 }
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000667
paul718e3742002-12-13 20:15:29 +0000668 /* Assert read up end point matches to end point */
669 if (pnt != end)
Denis Ovsienko85a2ebf2011-12-05 19:36:06 +0400670 zlog_warn ("ifam_read() doesn't read all socket data");
paul718e3742002-12-13 20:15:29 +0000671}
672
673/* Interface's address information get. */
paulec1a4282005-11-24 15:15:17 +0000674int
paul718e3742002-12-13 20:15:29 +0000675ifam_read (struct ifa_msghdr *ifam)
676{
paul6fe70d12005-11-12 22:55:10 +0000677 struct interface *ifp = NULL;
paul0752ef02005-11-03 12:35:21 +0000678 union sockunion addr, mask, brd;
paul6fe70d12005-11-12 22:55:10 +0000679 char ifname[INTERFACE_NAMSIZ];
680 short ifnlen = 0;
681 char isalias = 0;
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000682 int flags = 0;
paul6fe70d12005-11-12 22:55:10 +0000683
684 ifname[0] = ifname[INTERFACE_NAMSIZ - 1] = '\0';
685
686 /* Allocate and read address information. */
687 ifam_read_mesg (ifam, &addr, &mask, &brd, ifname, &ifnlen);
688
689 if ((ifp = if_lookup_by_index(ifam->ifam_index)) == NULL)
paul718e3742002-12-13 20:15:29 +0000690 {
paul6fe70d12005-11-12 22:55:10 +0000691 zlog_warn ("%s: no interface for ifname %s, index %d",
692 __func__, ifname, ifam->ifam_index);
paul718e3742002-12-13 20:15:29 +0000693 return -1;
694 }
paul6fe70d12005-11-12 22:55:10 +0000695
696 if (ifnlen && strncmp (ifp->name, ifname, INTERFACE_NAMSIZ))
697 isalias = 1;
698
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000699 /* N.B. The info in ifa_msghdr does not tell us whether the RTA_BRD
700 field contains a broadcast address or a peer address, so we are forced to
701 rely upon the interface type. */
702 if (if_is_pointopoint(ifp))
703 SET_FLAG(flags, ZEBRA_IFA_PEER);
704
Paul Jakma65022082007-03-06 13:43:05 +0000705#if 0
706 /* it might seem cute to grab the interface metric here, however
707 * we're processing an address update message, and so some systems
708 * (e.g. FBSD) dont bother to fill in ifam_metric. Disabled, but left
709 * in deliberately, as comment.
710 */
pauld34b8992006-01-17 18:03:04 +0000711 ifp->metric = ifam->ifam_metric;
Paul Jakma65022082007-03-06 13:43:05 +0000712#endif
713
paul718e3742002-12-13 20:15:29 +0000714 /* Add connected address. */
715 switch (sockunion_family (&addr))
716 {
717 case AF_INET:
718 if (ifam->ifam_type == RTM_NEWADDR)
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000719 connected_add_ipv4 (ifp, flags, &addr.sin.sin_addr,
paul718e3742002-12-13 20:15:29 +0000720 ip_masklen (mask.sin.sin_addr),
pauld34b8992006-01-17 18:03:04 +0000721 &brd.sin.sin_addr,
722 (isalias ? ifname : NULL));
paul718e3742002-12-13 20:15:29 +0000723 else
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000724 connected_delete_ipv4 (ifp, flags, &addr.sin.sin_addr,
paul718e3742002-12-13 20:15:29 +0000725 ip_masklen (mask.sin.sin_addr),
paul0752ef02005-11-03 12:35:21 +0000726 &brd.sin.sin_addr);
paul718e3742002-12-13 20:15:29 +0000727 break;
728#ifdef HAVE_IPV6
729 case AF_INET6:
730 /* Unset interface index from link-local address when IPv6 stack
731 is KAME. */
732 if (IN6_IS_ADDR_LINKLOCAL (&addr.sin6.sin6_addr))
733 SET_IN6_LINKLOCAL_IFINDEX (addr.sin6.sin6_addr, 0);
734
735 if (ifam->ifam_type == RTM_NEWADDR)
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000736 connected_add_ipv6 (ifp, flags, &addr.sin6.sin6_addr,
paul718e3742002-12-13 20:15:29 +0000737 ip6_masklen (mask.sin6.sin6_addr),
pauld34b8992006-01-17 18:03:04 +0000738 &brd.sin6.sin6_addr,
739 (isalias ? ifname : NULL));
paul718e3742002-12-13 20:15:29 +0000740 else
741 connected_delete_ipv6 (ifp,
742 &addr.sin6.sin6_addr,
743 ip6_masklen (mask.sin6.sin6_addr),
paul0752ef02005-11-03 12:35:21 +0000744 &brd.sin6.sin6_addr);
paul718e3742002-12-13 20:15:29 +0000745 break;
746#endif /* HAVE_IPV6 */
747 default:
748 /* Unsupported family silently ignore... */
749 break;
750 }
paul5c78b3d2006-01-25 04:31:40 +0000751
752 /* Check interface flag for implicit up of the interface. */
753 if_refresh (ifp);
754
755#ifdef SUNOS_5
756 /* In addition to lacking IFANNOUNCE, on SUNOS IFF_UP is strange.
757 * See comments for SUNOS_5 in interface.c::if_flags_mangle.
758 *
759 * Here we take care of case where the real IFF_UP was previously
760 * unset (as kept in struct zebra_if.primary_state) and the mangled
761 * IFF_UP (ie IFF_UP set || listcount(connected) has now transitioned
762 * to unset due to the lost non-primary address having DELADDR'd.
763 *
764 * we must delete the interface, because in between here and next
765 * event for this interface-name the administrator could unplumb
766 * and replumb the interface.
767 */
768 if (!if_is_up (ifp))
769 if_delete_update (ifp);
770#endif /* SUNOS_5 */
771
paul718e3742002-12-13 20:15:29 +0000772 return 0;
773}
David Lamparter6b0655a2014-06-04 06:53:35 +0200774
paul718e3742002-12-13 20:15:29 +0000775/* Interface function for reading kernel routing table information. */
paul6621ca82005-11-23 13:02:08 +0000776static int
paul718e3742002-12-13 20:15:29 +0000777rtm_read_mesg (struct rt_msghdr *rtm,
778 union sockunion *dest,
779 union sockunion *mask,
paul6fe70d12005-11-12 22:55:10 +0000780 union sockunion *gate,
781 char *ifname,
782 short *ifnlen)
paul718e3742002-12-13 20:15:29 +0000783{
784 caddr_t pnt, end;
785
786 /* Pnt points out socket data start point. */
787 pnt = (caddr_t)(rtm + 1);
788 end = ((caddr_t)rtm) + rtm->rtm_msglen;
789
790 /* rt_msghdr version check. */
791 if (rtm->rtm_version != RTM_VERSION)
792 zlog (NULL, LOG_WARNING,
793 "Routing message version different %d should be %d."
794 "This may cause problem\n", rtm->rtm_version, RTM_VERSION);
paul62debbb2005-06-14 14:07:07 +0000795
paul718e3742002-12-13 20:15:29 +0000796 /* Be sure structure is cleared */
797 memset (dest, 0, sizeof (union sockunion));
798 memset (gate, 0, sizeof (union sockunion));
799 memset (mask, 0, sizeof (union sockunion));
800
801 /* We fetch each socket variable into sockunion. */
paul62debbb2005-06-14 14:07:07 +0000802 RTA_ADDR_GET (dest, RTA_DST, rtm->rtm_addrs, pnt);
803 RTA_ADDR_GET (gate, RTA_GATEWAY, rtm->rtm_addrs, pnt);
804 RTA_ATTR_GET (mask, RTA_NETMASK, rtm->rtm_addrs, pnt);
805 RTA_ADDR_GET (NULL, RTA_GENMASK, rtm->rtm_addrs, pnt);
paul6fe70d12005-11-12 22:55:10 +0000806 RTA_NAME_GET (ifname, RTA_IFP, rtm->rtm_addrs, pnt, *ifnlen);
paul62debbb2005-06-14 14:07:07 +0000807 RTA_ADDR_GET (NULL, RTA_IFA, rtm->rtm_addrs, pnt);
808 RTA_ADDR_GET (NULL, RTA_AUTHOR, rtm->rtm_addrs, pnt);
809 RTA_ADDR_GET (NULL, RTA_BRD, rtm->rtm_addrs, pnt);
paul718e3742002-12-13 20:15:29 +0000810
811 /* If there is netmask information set it's family same as
812 destination family*/
813 if (rtm->rtm_addrs & RTA_NETMASK)
814 mask->sa.sa_family = dest->sa.sa_family;
815
816 /* Assert read up to the end of pointer. */
817 if (pnt != end)
Denis Ovsienko85a2ebf2011-12-05 19:36:06 +0400818 zlog (NULL, LOG_WARNING, "rtm_read() doesn't read all socket data.");
paul718e3742002-12-13 20:15:29 +0000819
820 return rtm->rtm_flags;
821}
822
paulec1a4282005-11-24 15:15:17 +0000823void
paul718e3742002-12-13 20:15:29 +0000824rtm_read (struct rt_msghdr *rtm)
825{
826 int flags;
827 u_char zebra_flags;
828 union sockunion dest, mask, gate;
paul6fe70d12005-11-12 22:55:10 +0000829 char ifname[INTERFACE_NAMSIZ + 1];
830 short ifnlen = 0;
paul718e3742002-12-13 20:15:29 +0000831
832 zebra_flags = 0;
833
paul718e3742002-12-13 20:15:29 +0000834 /* Read destination and netmask and gateway from rtm message
835 structure. */
paul6fe70d12005-11-12 22:55:10 +0000836 flags = rtm_read_mesg (rtm, &dest, &mask, &gate, ifname, &ifnlen);
Denis Ovsienko6da59802007-08-17 14:16:30 +0000837 if (!(flags & RTF_DONE))
838 return;
Denis Ovsienkodc958242007-08-13 16:03:06 +0000839 if (IS_ZEBRA_DEBUG_KERNEL)
840 zlog_debug ("%s: got rtm of type %d (%s)", __func__, rtm->rtm_type,
Denis Ovsienko2d844522007-09-14 11:31:55 +0000841 lookup (rtm_type_str, rtm->rtm_type));
paul718e3742002-12-13 20:15:29 +0000842
843#ifdef RTF_CLONED /*bsdi, netbsd 1.6*/
844 if (flags & RTF_CLONED)
845 return;
846#endif
847#ifdef RTF_WASCLONED /*freebsd*/
848 if (flags & RTF_WASCLONED)
849 return;
850#endif
851
852 if ((rtm->rtm_type == RTM_ADD) && ! (flags & RTF_UP))
853 return;
854
855 /* This is connected route. */
856 if (! (flags & RTF_GATEWAY))
857 return;
858
859 if (flags & RTF_PROTO1)
860 SET_FLAG (zebra_flags, ZEBRA_FLAG_SELFROUTE);
861
862 /* This is persistent route. */
863 if (flags & RTF_STATIC)
864 SET_FLAG (zebra_flags, ZEBRA_FLAG_STATIC);
865
hasso81dfcaa2003-05-25 19:21:25 +0000866 /* This is a reject or blackhole route */
867 if (flags & RTF_REJECT)
868 SET_FLAG (zebra_flags, ZEBRA_FLAG_REJECT);
869 if (flags & RTF_BLACKHOLE)
870 SET_FLAG (zebra_flags, ZEBRA_FLAG_BLACKHOLE);
871
paul718e3742002-12-13 20:15:29 +0000872 if (dest.sa.sa_family == AF_INET)
873 {
874 struct prefix_ipv4 p;
875
876 p.family = AF_INET;
877 p.prefix = dest.sin.sin_addr;
878 if (flags & RTF_HOST)
879 p.prefixlen = IPV4_MAX_PREFIXLEN;
880 else
881 p.prefixlen = ip_masklen (mask.sin.sin_addr);
paulca162182005-09-12 16:58:52 +0000882
Denis Ovsienkodc958242007-08-13 16:03:06 +0000883 /* Catch self originated messages and match them against our current RIB.
884 * At the same time, ignore unconfirmed messages, they should be tracked
885 * by rtm_write() and kernel_rtm_ipv4().
886 */
Denis Ovsienko96934e62007-09-14 14:56:28 +0000887 if (rtm->rtm_type != RTM_GET && rtm->rtm_pid == pid)
Denis Ovsienkodc958242007-08-13 16:03:06 +0000888 {
Timo Teräsbe6335d2015-05-23 11:08:41 +0300889 char buf[PREFIX_STRLEN], gate_buf[INET_ADDRSTRLEN];
Denis Ovsienkodc958242007-08-13 16:03:06 +0000890 int ret;
Denis Ovsienkodc958242007-08-13 16:03:06 +0000891 if (! IS_ZEBRA_DEBUG_RIB)
892 return;
893 ret = rib_lookup_ipv4_route (&p, &gate);
Timo Teräsbe6335d2015-05-23 11:08:41 +0300894 prefix2str (&p, buf, sizeof(buf));
Denis Ovsienkodc958242007-08-13 16:03:06 +0000895 switch (rtm->rtm_type)
896 {
897 case RTM_ADD:
898 case RTM_GET:
899 case RTM_CHANGE:
900 /* The kernel notifies us about a new route in FIB created by us.
901 Do we have a correspondent entry in our RIB? */
902 switch (ret)
903 {
904 case ZEBRA_RIB_NOTFOUND:
Timo Teräsbe6335d2015-05-23 11:08:41 +0300905 zlog_debug ("%s: %s %s: desync: RR isn't yet in RIB, while already in FIB",
906 __func__, lookup (rtm_type_str, rtm->rtm_type), buf);
Denis Ovsienkodc958242007-08-13 16:03:06 +0000907 break;
908 case ZEBRA_RIB_FOUND_CONNECTED:
909 case ZEBRA_RIB_FOUND_NOGATE:
910 inet_ntop (AF_INET, &gate.sin.sin_addr, gate_buf, INET_ADDRSTRLEN);
Timo Teräsbe6335d2015-05-23 11:08:41 +0300911 zlog_debug ("%s: %s %s: desync: RR is in RIB, but gate differs (ours is %s)",
912 __func__, lookup (rtm_type_str, rtm->rtm_type), buf, gate_buf);
Denis Ovsienkodc958242007-08-13 16:03:06 +0000913 break;
914 case ZEBRA_RIB_FOUND_EXACT: /* RIB RR == FIB RR */
Timo Teräsbe6335d2015-05-23 11:08:41 +0300915 zlog_debug ("%s: %s %s: done Ok",
916 __func__, lookup (rtm_type_str, rtm->rtm_type), buf);
Denis Ovsienkodc958242007-08-13 16:03:06 +0000917 rib_lookup_and_dump (&p);
918 return;
919 break;
920 }
921 break;
922 case RTM_DELETE:
923 /* The kernel notifies us about a route deleted by us. Do we still
924 have it in the RIB? Do we have anything instead? */
925 switch (ret)
926 {
927 case ZEBRA_RIB_FOUND_EXACT:
Timo Teräsbe6335d2015-05-23 11:08:41 +0300928 zlog_debug ("%s: %s %s: desync: RR is still in RIB, while already not in FIB",
929 __func__, lookup (rtm_type_str, rtm->rtm_type), buf);
Denis Ovsienkodc958242007-08-13 16:03:06 +0000930 rib_lookup_and_dump (&p);
931 break;
932 case ZEBRA_RIB_FOUND_CONNECTED:
933 case ZEBRA_RIB_FOUND_NOGATE:
Timo Teräsbe6335d2015-05-23 11:08:41 +0300934 zlog_debug ("%s: %s %s: desync: RR is still in RIB, plus gate differs",
935 __func__, lookup (rtm_type_str, rtm->rtm_type), buf);
Denis Ovsienkodc958242007-08-13 16:03:06 +0000936 rib_lookup_and_dump (&p);
937 break;
938 case ZEBRA_RIB_NOTFOUND: /* RIB RR == FIB RR */
Timo Teräsbe6335d2015-05-23 11:08:41 +0300939 zlog_debug ("%s: %s %s: done Ok",
940 __func__, lookup (rtm_type_str, rtm->rtm_type), buf);
Denis Ovsienkodc958242007-08-13 16:03:06 +0000941 rib_lookup_and_dump (&p);
942 return;
943 break;
944 }
945 break;
946 default:
Timo Teräsbe6335d2015-05-23 11:08:41 +0300947 zlog_debug ("%s: %s: warning: loopback RTM of type %s received",
948 __func__, buf, lookup (rtm_type_str, rtm->rtm_type));
Denis Ovsienkodc958242007-08-13 16:03:06 +0000949 }
950 return;
951 }
952
paulca162182005-09-12 16:58:52 +0000953 /* Change, delete the old prefix, we have no further information
954 * to specify the route really
955 */
956 if (rtm->rtm_type == RTM_CHANGE)
957 rib_delete_ipv4 (ZEBRA_ROUTE_KERNEL, zebra_flags, &p,
Denis Ovsienko6eac79a2011-12-05 13:43:18 +0400958 NULL, 0, 0, SAFI_UNICAST);
paulca162182005-09-12 16:58:52 +0000959
960 if (rtm->rtm_type == RTM_GET
961 || rtm->rtm_type == RTM_ADD
962 || rtm->rtm_type == RTM_CHANGE)
paul718e3742002-12-13 20:15:29 +0000963 rib_add_ipv4 (ZEBRA_ROUTE_KERNEL, zebra_flags,
G.Balajicddf3912011-11-26 21:59:32 +0400964 &p, &gate.sin.sin_addr, NULL, 0, 0, 0, 0, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +0000965 else
966 rib_delete_ipv4 (ZEBRA_ROUTE_KERNEL, zebra_flags,
Denis Ovsienko6eac79a2011-12-05 13:43:18 +0400967 &p, &gate.sin.sin_addr, 0, 0, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +0000968 }
969#ifdef HAVE_IPV6
970 if (dest.sa.sa_family == AF_INET6)
971 {
Denis Ovsienko5619f562007-10-24 13:13:21 +0000972 /* One day we might have a debug section here like one in the
973 * IPv4 case above. Just ignore own messages at the moment.
974 */
975 if (rtm->rtm_type != RTM_GET && rtm->rtm_pid == pid)
976 return;
paul718e3742002-12-13 20:15:29 +0000977 struct prefix_ipv6 p;
978 unsigned int ifindex = 0;
979
980 p.family = AF_INET6;
981 p.prefix = dest.sin6.sin6_addr;
982 if (flags & RTF_HOST)
983 p.prefixlen = IPV6_MAX_PREFIXLEN;
984 else
985 p.prefixlen = ip6_masklen (mask.sin6.sin6_addr);
986
987#ifdef KAME
988 if (IN6_IS_ADDR_LINKLOCAL (&gate.sin6.sin6_addr))
989 {
990 ifindex = IN6_LINKLOCAL_IFINDEX (gate.sin6.sin6_addr);
991 SET_IN6_LINKLOCAL_IFINDEX (gate.sin6.sin6_addr, 0);
992 }
993#endif /* KAME */
994
paulca162182005-09-12 16:58:52 +0000995 /* CHANGE: delete the old prefix, we have no further information
996 * to specify the route really
997 */
998 if (rtm->rtm_type == RTM_CHANGE)
paul6621ca82005-11-23 13:02:08 +0000999 rib_delete_ipv6 (ZEBRA_ROUTE_KERNEL, zebra_flags, &p,
Denis Ovsienko6eac79a2011-12-05 13:43:18 +04001000 NULL, 0, 0, SAFI_UNICAST);
paulca162182005-09-12 16:58:52 +00001001
1002 if (rtm->rtm_type == RTM_GET
1003 || rtm->rtm_type == RTM_ADD
1004 || rtm->rtm_type == RTM_CHANGE)
paul718e3742002-12-13 20:15:29 +00001005 rib_add_ipv6 (ZEBRA_ROUTE_KERNEL, zebra_flags,
Denis Ovsienko6eac79a2011-12-05 13:43:18 +04001006 &p, &gate.sin6.sin6_addr, ifindex, 0, 0, 0, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +00001007 else
1008 rib_delete_ipv6 (ZEBRA_ROUTE_KERNEL, zebra_flags,
Denis Ovsienko6eac79a2011-12-05 13:43:18 +04001009 &p, &gate.sin6.sin6_addr, ifindex, 0, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +00001010 }
1011#endif /* HAVE_IPV6 */
1012}
1013
1014/* Interface function for the kernel routing table updates. Support
paul6621ca82005-11-23 13:02:08 +00001015 * for RTM_CHANGE will be needed.
1016 * Exported only for rt_socket.c
1017 */
paul718e3742002-12-13 20:15:29 +00001018int
1019rtm_write (int message,
1020 union sockunion *dest,
1021 union sockunion *mask,
1022 union sockunion *gate,
1023 unsigned int index,
1024 int zebra_flags,
1025 int metric)
1026{
1027 int ret;
1028 caddr_t pnt;
1029 struct interface *ifp;
paul718e3742002-12-13 20:15:29 +00001030
1031 /* Sequencial number of routing message. */
1032 static int msg_seq = 0;
1033
1034 /* Struct of rt_msghdr and buffer for storing socket's data. */
1035 struct
1036 {
1037 struct rt_msghdr rtm;
1038 char buf[512];
1039 } msg;
1040
paul718e3742002-12-13 20:15:29 +00001041 if (routing_sock < 0)
1042 return ZEBRA_ERR_EPERM;
1043
1044 /* Clear and set rt_msghdr values */
1045 memset (&msg, 0, sizeof (struct rt_msghdr));
1046 msg.rtm.rtm_version = RTM_VERSION;
1047 msg.rtm.rtm_type = message;
1048 msg.rtm.rtm_seq = msg_seq++;
1049 msg.rtm.rtm_addrs = RTA_DST;
1050 msg.rtm.rtm_addrs |= RTA_GATEWAY;
1051 msg.rtm.rtm_flags = RTF_UP;
1052 msg.rtm.rtm_index = index;
1053
1054 if (metric != 0)
1055 {
1056 msg.rtm.rtm_rmx.rmx_hopcount = metric;
1057 msg.rtm.rtm_inits |= RTV_HOPCOUNT;
1058 }
1059
1060 ifp = if_lookup_by_index (index);
1061
1062 if (gate && message == RTM_ADD)
1063 msg.rtm.rtm_flags |= RTF_GATEWAY;
1064
David Warde6f148e2009-12-03 21:43:11 +03001065 /* When RTF_CLONING is unavailable on BSD, should we set some
1066 * other flag instead?
1067 */
1068#ifdef RTF_CLONING
paul718e3742002-12-13 20:15:29 +00001069 if (! gate && message == RTM_ADD && ifp &&
1070 (ifp->flags & IFF_POINTOPOINT) == 0)
1071 msg.rtm.rtm_flags |= RTF_CLONING;
David Warde6f148e2009-12-03 21:43:11 +03001072#endif /* RTF_CLONING */
paul718e3742002-12-13 20:15:29 +00001073
1074 /* If no protocol specific gateway is specified, use link
1075 address for gateway. */
1076 if (! gate)
1077 {
1078 if (!ifp)
1079 {
Denis Ovsienkodc958242007-08-13 16:03:06 +00001080 char dest_buf[INET_ADDRSTRLEN] = "NULL", mask_buf[INET_ADDRSTRLEN] = "255.255.255.255";
1081 if (dest)
1082 inet_ntop (AF_INET, &dest->sin.sin_addr, dest_buf, INET_ADDRSTRLEN);
1083 if (mask)
1084 inet_ntop (AF_INET, &mask->sin.sin_addr, mask_buf, INET_ADDRSTRLEN);
1085 zlog_warn ("%s: %s/%s: gate == NULL and no gateway found for ifindex %d",
1086 __func__, dest_buf, mask_buf, index);
paul718e3742002-12-13 20:15:29 +00001087 return -1;
1088 }
1089 gate = (union sockunion *) & ifp->sdl;
1090 }
1091
1092 if (mask)
1093 msg.rtm.rtm_addrs |= RTA_NETMASK;
1094 else if (message == RTM_ADD)
1095 msg.rtm.rtm_flags |= RTF_HOST;
1096
1097 /* Tagging route with flags */
1098 msg.rtm.rtm_flags |= (RTF_PROTO1);
1099
1100 /* Additional flags. */
1101 if (zebra_flags & ZEBRA_FLAG_BLACKHOLE)
1102 msg.rtm.rtm_flags |= RTF_BLACKHOLE;
hasso81dfcaa2003-05-25 19:21:25 +00001103 if (zebra_flags & ZEBRA_FLAG_REJECT)
1104 msg.rtm.rtm_flags |= RTF_REJECT;
1105
paul718e3742002-12-13 20:15:29 +00001106
paul718e3742002-12-13 20:15:29 +00001107#define SOCKADDRSET(X,R) \
1108 if (msg.rtm.rtm_addrs & (R)) \
1109 { \
paul6fe70d12005-11-12 22:55:10 +00001110 int len = SAROUNDUP (X); \
paul718e3742002-12-13 20:15:29 +00001111 memcpy (pnt, (caddr_t)(X), len); \
1112 pnt += len; \
1113 }
paul718e3742002-12-13 20:15:29 +00001114
1115 pnt = (caddr_t) msg.buf;
1116
1117 /* Write each socket data into rtm message buffer */
1118 SOCKADDRSET (dest, RTA_DST);
1119 SOCKADDRSET (gate, RTA_GATEWAY);
1120 SOCKADDRSET (mask, RTA_NETMASK);
1121
1122 msg.rtm.rtm_msglen = pnt - (caddr_t) &msg;
1123
1124 ret = write (routing_sock, &msg, msg.rtm.rtm_msglen);
1125
1126 if (ret != msg.rtm.rtm_msglen)
1127 {
1128 if (errno == EEXIST)
1129 return ZEBRA_ERR_RTEXIST;
1130 if (errno == ENETUNREACH)
1131 return ZEBRA_ERR_RTUNREACH;
Denis Ovsienkodc958242007-08-13 16:03:06 +00001132 if (errno == ESRCH)
1133 return ZEBRA_ERR_RTNOEXIST;
paul718e3742002-12-13 20:15:29 +00001134
Denis Ovsienkodc958242007-08-13 16:03:06 +00001135 zlog_warn ("%s: write : %s (%d)", __func__, safe_strerror (errno), errno);
1136 return ZEBRA_ERR_KERNEL;
paul718e3742002-12-13 20:15:29 +00001137 }
Denis Ovsienkodc958242007-08-13 16:03:06 +00001138 return ZEBRA_ERR_NOERROR;
paul718e3742002-12-13 20:15:29 +00001139}
1140
David Lamparter6b0655a2014-06-04 06:53:35 +02001141
paul718e3742002-12-13 20:15:29 +00001142#include "thread.h"
1143#include "zebra/zserv.h"
1144
paul718e3742002-12-13 20:15:29 +00001145/* For debug purpose. */
ajsb6178002004-12-07 21:12:56 +00001146static void
paul718e3742002-12-13 20:15:29 +00001147rtmsg_debug (struct rt_msghdr *rtm)
1148{
Denis Ovsienko2d844522007-09-14 11:31:55 +00001149 zlog_debug ("Kernel: Len: %d Type: %s", rtm->rtm_msglen, lookup (rtm_type_str, rtm->rtm_type));
paul718e3742002-12-13 20:15:29 +00001150 rtm_flag_dump (rtm->rtm_flags);
ajsb6178002004-12-07 21:12:56 +00001151 zlog_debug ("Kernel: message seq %d", rtm->rtm_seq);
paul6fe70d12005-11-12 22:55:10 +00001152 zlog_debug ("Kernel: pid %d, rtm_addrs 0x%x", rtm->rtm_pid, rtm->rtm_addrs);
paul718e3742002-12-13 20:15:29 +00001153}
1154
1155/* This is pretty gross, better suggestions welcome -- mhandler */
1156#ifndef RTAX_MAX
1157#ifdef RTA_NUMBITS
1158#define RTAX_MAX RTA_NUMBITS
1159#else
1160#define RTAX_MAX 8
1161#endif /* RTA_NUMBITS */
1162#endif /* RTAX_MAX */
1163
1164/* Kernel routing table and interface updates via routing socket. */
paul6621ca82005-11-23 13:02:08 +00001165static int
paul718e3742002-12-13 20:15:29 +00001166kernel_read (struct thread *thread)
1167{
1168 int sock;
1169 int nbytes;
1170 struct rt_msghdr *rtm;
1171
gdtdbee01f2004-01-06 00:36:51 +00001172 /*
1173 * This must be big enough for any message the kernel might send.
gdtb27900b2004-01-08 15:44:29 +00001174 * Rather than determining how many sockaddrs of what size might be
1175 * in each particular message, just use RTAX_MAX of sockaddr_storage
1176 * for each. Note that the sockaddrs must be after each message
1177 * definition, or rather after whichever happens to be the largest,
1178 * since the buffer needs to be big enough for a message and the
1179 * sockaddrs together.
gdtdbee01f2004-01-06 00:36:51 +00001180 */
paul718e3742002-12-13 20:15:29 +00001181 union
1182 {
1183 /* Routing information. */
1184 struct
1185 {
1186 struct rt_msghdr rtm;
gdtb27900b2004-01-08 15:44:29 +00001187 struct sockaddr_storage addr[RTAX_MAX];
paul718e3742002-12-13 20:15:29 +00001188 } r;
1189
1190 /* Interface information. */
1191 struct
1192 {
1193 struct if_msghdr ifm;
gdtb27900b2004-01-08 15:44:29 +00001194 struct sockaddr_storage addr[RTAX_MAX];
paul718e3742002-12-13 20:15:29 +00001195 } im;
1196
1197 /* Interface address information. */
1198 struct
1199 {
1200 struct ifa_msghdr ifa;
gdtb27900b2004-01-08 15:44:29 +00001201 struct sockaddr_storage addr[RTAX_MAX];
paul718e3742002-12-13 20:15:29 +00001202 } ia;
1203
1204#ifdef RTM_IFANNOUNCE
1205 /* Interface arrival/departure */
1206 struct
1207 {
1208 struct if_announcemsghdr ifan;
gdtb27900b2004-01-08 15:44:29 +00001209 struct sockaddr_storage addr[RTAX_MAX];
paul718e3742002-12-13 20:15:29 +00001210 } ian;
1211#endif /* RTM_IFANNOUNCE */
1212
1213 } buf;
1214
1215 /* Fetch routing socket. */
1216 sock = THREAD_FD (thread);
1217
1218 nbytes= read (sock, &buf, sizeof buf);
1219
1220 if (nbytes <= 0)
1221 {
1222 if (nbytes < 0 && errno != EWOULDBLOCK && errno != EAGAIN)
ajs6099b3b2004-11-20 02:06:59 +00001223 zlog_warn ("routing socket error: %s", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001224 return 0;
1225 }
1226
paul9bcdb632003-07-08 08:09:45 +00001227 thread_add_read (zebrad.master, kernel_read, NULL, sock);
paul718e3742002-12-13 20:15:29 +00001228
hasso726f9b22003-05-25 21:04:54 +00001229 if (IS_ZEBRA_DEBUG_KERNEL)
1230 rtmsg_debug (&buf.r.rtm);
paul718e3742002-12-13 20:15:29 +00001231
1232 rtm = &buf.r.rtm;
1233
gdtb27900b2004-01-08 15:44:29 +00001234 /*
1235 * Ensure that we didn't drop any data, so that processing routines
1236 * can assume they have the whole message.
1237 */
gdtda26e3b2004-01-05 17:20:59 +00001238 if (rtm->rtm_msglen != nbytes)
1239 {
1240 zlog_warn ("kernel_read: rtm->rtm_msglen %d, nbytes %d, type %d\n",
1241 rtm->rtm_msglen, nbytes, rtm->rtm_type);
1242 return -1;
1243 }
1244
paul718e3742002-12-13 20:15:29 +00001245 switch (rtm->rtm_type)
1246 {
1247 case RTM_ADD:
1248 case RTM_DELETE:
paulca162182005-09-12 16:58:52 +00001249 case RTM_CHANGE:
paul718e3742002-12-13 20:15:29 +00001250 rtm_read (rtm);
1251 break;
1252 case RTM_IFINFO:
1253 ifm_read (&buf.im.ifm);
1254 break;
1255 case RTM_NEWADDR:
1256 case RTM_DELADDR:
1257 ifam_read (&buf.ia.ifa);
1258 break;
1259#ifdef RTM_IFANNOUNCE
1260 case RTM_IFANNOUNCE:
1261 ifan_read (&buf.ian.ifan);
1262 break;
1263#endif /* RTM_IFANNOUNCE */
1264 default:
hasso726f9b22003-05-25 21:04:54 +00001265 if (IS_ZEBRA_DEBUG_KERNEL)
ajsb6178002004-12-07 21:12:56 +00001266 zlog_debug("Unprocessed RTM_type: %d", rtm->rtm_type);
paul718e3742002-12-13 20:15:29 +00001267 break;
1268 }
1269 return 0;
1270}
1271
1272/* Make routing socket. */
paul6621ca82005-11-23 13:02:08 +00001273static void
1274routing_socket (void)
paul718e3742002-12-13 20:15:29 +00001275{
pauledd7c242003-06-04 13:59:38 +00001276 if ( zserv_privs.change (ZPRIVS_RAISE) )
1277 zlog_err ("routing_socket: Can't raise privileges");
1278
paul718e3742002-12-13 20:15:29 +00001279 routing_sock = socket (AF_ROUTE, SOCK_RAW, 0);
1280
1281 if (routing_sock < 0)
1282 {
pauledd7c242003-06-04 13:59:38 +00001283 if ( zserv_privs.change (ZPRIVS_LOWER) )
1284 zlog_err ("routing_socket: Can't lower privileges");
paul718e3742002-12-13 20:15:29 +00001285 zlog_warn ("Can't init kernel routing socket");
1286 return;
1287 }
1288
paul865b8522005-01-05 08:30:35 +00001289 /* XXX: Socket should be NONBLOCK, however as we currently
1290 * discard failed writes, this will lead to inconsistencies.
1291 * For now, socket must be blocking.
1292 */
1293 /*if (fcntl (routing_sock, F_SETFL, O_NONBLOCK) < 0)
1294 zlog_warn ("Can't set O_NONBLOCK to routing socket");*/
1295
pauledd7c242003-06-04 13:59:38 +00001296 if ( zserv_privs.change (ZPRIVS_LOWER) )
1297 zlog_err ("routing_socket: Can't lower privileges");
paul718e3742002-12-13 20:15:29 +00001298
1299 /* kernel_read needs rewrite. */
paul9bcdb632003-07-08 08:09:45 +00001300 thread_add_read (zebrad.master, kernel_read, NULL, routing_sock);
paul718e3742002-12-13 20:15:29 +00001301}
1302
1303/* Exported interface function. This function simply calls
1304 routing_socket (). */
1305void
paul6621ca82005-11-23 13:02:08 +00001306kernel_init (void)
paul718e3742002-12-13 20:15:29 +00001307{
1308 routing_socket ();
1309}