blob: 916dad97f8c6db30e76dbd2f1969425284617ca3 [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 {
634 switch (sockunion_family(addr))
635 {
636 case AF_INET:
637 {
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000638 char buf[4][INET_ADDRSTRLEN];
Andrew J. Schorr55196042006-05-17 15:04:59 +0000639 zlog_debug ("%s: ifindex %d, ifname %s, ifam_addrs 0x%x, "
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000640 "ifam_flags 0x%x, addr %s/%d broad %s dst %s "
641 "gateway %s",
642 __func__, ifm->ifam_index,
Andrew J. Schorr55196042006-05-17 15:04:59 +0000643 (ifnlen ? ifname : "(nil)"), ifm->ifam_addrs,
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000644 ifm->ifam_flags,
Andrew J. Schorr55196042006-05-17 15:04:59 +0000645 inet_ntop(AF_INET,&addr->sin.sin_addr,
646 buf[0],sizeof(buf[0])),
647 ip_masklen(mask->sin.sin_addr),
648 inet_ntop(AF_INET,&brd->sin.sin_addr,
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000649 buf[1],sizeof(buf[1])),
650 inet_ntop(AF_INET,&dst.sin.sin_addr,
651 buf[2],sizeof(buf[2])),
652 inet_ntop(AF_INET,&gateway.sin.sin_addr,
653 buf[3],sizeof(buf[3])));
Andrew J. Schorr55196042006-05-17 15:04:59 +0000654 }
655 break;
656#ifdef HAVE_IPV6
657 case AF_INET6:
658 {
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000659 char buf[4][INET6_ADDRSTRLEN];
Andrew J. Schorr55196042006-05-17 15:04:59 +0000660 zlog_debug ("%s: ifindex %d, ifname %s, ifam_addrs 0x%x, "
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000661 "ifam_flags 0x%x, addr %s/%d broad %s dst %s "
662 "gateway %s",
Andrew J. Schorr55196042006-05-17 15:04:59 +0000663 __func__, ifm->ifam_index,
664 (ifnlen ? ifname : "(nil)"), ifm->ifam_addrs,
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000665 ifm->ifam_flags,
Andrew J. Schorr55196042006-05-17 15:04:59 +0000666 inet_ntop(AF_INET6,&addr->sin6.sin6_addr,
667 buf[0],sizeof(buf[0])),
668 ip6_masklen(mask->sin6.sin6_addr),
669 inet_ntop(AF_INET6,&brd->sin6.sin6_addr,
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000670 buf[1],sizeof(buf[1])),
671 inet_ntop(AF_INET6,&dst.sin6.sin6_addr,
672 buf[2],sizeof(buf[2])),
673 inet_ntop(AF_INET6,&gateway.sin6.sin6_addr,
674 buf[3],sizeof(buf[3])));
Andrew J. Schorr55196042006-05-17 15:04:59 +0000675 }
676 break;
677#endif /* HAVE_IPV6 */
678 default:
679 zlog_debug ("%s: ifindex %d, ifname %s, ifam_addrs 0x%x",
680 __func__, ifm->ifam_index,
681 (ifnlen ? ifname : "(nil)"), ifm->ifam_addrs);
682 break;
683 }
684 }
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000685
paul718e3742002-12-13 20:15:29 +0000686 /* Assert read up end point matches to end point */
687 if (pnt != end)
Denis Ovsienko85a2ebf2011-12-05 19:36:06 +0400688 zlog_warn ("ifam_read() doesn't read all socket data");
paul718e3742002-12-13 20:15:29 +0000689}
690
691/* Interface's address information get. */
paulec1a4282005-11-24 15:15:17 +0000692int
paul718e3742002-12-13 20:15:29 +0000693ifam_read (struct ifa_msghdr *ifam)
694{
paul6fe70d12005-11-12 22:55:10 +0000695 struct interface *ifp = NULL;
paul0752ef02005-11-03 12:35:21 +0000696 union sockunion addr, mask, brd;
paul6fe70d12005-11-12 22:55:10 +0000697 char ifname[INTERFACE_NAMSIZ];
698 short ifnlen = 0;
699 char isalias = 0;
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000700 int flags = 0;
paul6fe70d12005-11-12 22:55:10 +0000701
702 ifname[0] = ifname[INTERFACE_NAMSIZ - 1] = '\0';
703
704 /* Allocate and read address information. */
705 ifam_read_mesg (ifam, &addr, &mask, &brd, ifname, &ifnlen);
706
707 if ((ifp = if_lookup_by_index(ifam->ifam_index)) == NULL)
paul718e3742002-12-13 20:15:29 +0000708 {
paul6fe70d12005-11-12 22:55:10 +0000709 zlog_warn ("%s: no interface for ifname %s, index %d",
710 __func__, ifname, ifam->ifam_index);
paul718e3742002-12-13 20:15:29 +0000711 return -1;
712 }
paul6fe70d12005-11-12 22:55:10 +0000713
714 if (ifnlen && strncmp (ifp->name, ifname, INTERFACE_NAMSIZ))
715 isalias = 1;
716
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000717 /* N.B. The info in ifa_msghdr does not tell us whether the RTA_BRD
718 field contains a broadcast address or a peer address, so we are forced to
719 rely upon the interface type. */
720 if (if_is_pointopoint(ifp))
721 SET_FLAG(flags, ZEBRA_IFA_PEER);
722
Paul Jakma65022082007-03-06 13:43:05 +0000723#if 0
724 /* it might seem cute to grab the interface metric here, however
725 * we're processing an address update message, and so some systems
726 * (e.g. FBSD) dont bother to fill in ifam_metric. Disabled, but left
727 * in deliberately, as comment.
728 */
pauld34b8992006-01-17 18:03:04 +0000729 ifp->metric = ifam->ifam_metric;
Paul Jakma65022082007-03-06 13:43:05 +0000730#endif
731
paul718e3742002-12-13 20:15:29 +0000732 /* Add connected address. */
733 switch (sockunion_family (&addr))
734 {
735 case AF_INET:
736 if (ifam->ifam_type == RTM_NEWADDR)
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000737 connected_add_ipv4 (ifp, flags, &addr.sin.sin_addr,
paul718e3742002-12-13 20:15:29 +0000738 ip_masklen (mask.sin.sin_addr),
pauld34b8992006-01-17 18:03:04 +0000739 &brd.sin.sin_addr,
740 (isalias ? ifname : NULL));
paul718e3742002-12-13 20:15:29 +0000741 else
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000742 connected_delete_ipv4 (ifp, flags, &addr.sin.sin_addr,
paul718e3742002-12-13 20:15:29 +0000743 ip_masklen (mask.sin.sin_addr),
paul0752ef02005-11-03 12:35:21 +0000744 &brd.sin.sin_addr);
paul718e3742002-12-13 20:15:29 +0000745 break;
746#ifdef HAVE_IPV6
747 case AF_INET6:
748 /* Unset interface index from link-local address when IPv6 stack
749 is KAME. */
750 if (IN6_IS_ADDR_LINKLOCAL (&addr.sin6.sin6_addr))
751 SET_IN6_LINKLOCAL_IFINDEX (addr.sin6.sin6_addr, 0);
752
753 if (ifam->ifam_type == RTM_NEWADDR)
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000754 connected_add_ipv6 (ifp, flags, &addr.sin6.sin6_addr,
paul718e3742002-12-13 20:15:29 +0000755 ip6_masklen (mask.sin6.sin6_addr),
pauld34b8992006-01-17 18:03:04 +0000756 &brd.sin6.sin6_addr,
757 (isalias ? ifname : NULL));
paul718e3742002-12-13 20:15:29 +0000758 else
759 connected_delete_ipv6 (ifp,
760 &addr.sin6.sin6_addr,
761 ip6_masklen (mask.sin6.sin6_addr),
paul0752ef02005-11-03 12:35:21 +0000762 &brd.sin6.sin6_addr);
paul718e3742002-12-13 20:15:29 +0000763 break;
764#endif /* HAVE_IPV6 */
765 default:
766 /* Unsupported family silently ignore... */
767 break;
768 }
paul5c78b3d2006-01-25 04:31:40 +0000769
770 /* Check interface flag for implicit up of the interface. */
771 if_refresh (ifp);
772
773#ifdef SUNOS_5
774 /* In addition to lacking IFANNOUNCE, on SUNOS IFF_UP is strange.
775 * See comments for SUNOS_5 in interface.c::if_flags_mangle.
776 *
777 * Here we take care of case where the real IFF_UP was previously
778 * unset (as kept in struct zebra_if.primary_state) and the mangled
779 * IFF_UP (ie IFF_UP set || listcount(connected) has now transitioned
780 * to unset due to the lost non-primary address having DELADDR'd.
781 *
782 * we must delete the interface, because in between here and next
783 * event for this interface-name the administrator could unplumb
784 * and replumb the interface.
785 */
786 if (!if_is_up (ifp))
787 if_delete_update (ifp);
788#endif /* SUNOS_5 */
789
paul718e3742002-12-13 20:15:29 +0000790 return 0;
791}
David Lamparter6b0655a2014-06-04 06:53:35 +0200792
paul718e3742002-12-13 20:15:29 +0000793/* Interface function for reading kernel routing table information. */
paul6621ca82005-11-23 13:02:08 +0000794static int
paul718e3742002-12-13 20:15:29 +0000795rtm_read_mesg (struct rt_msghdr *rtm,
796 union sockunion *dest,
797 union sockunion *mask,
paul6fe70d12005-11-12 22:55:10 +0000798 union sockunion *gate,
799 char *ifname,
800 short *ifnlen)
paul718e3742002-12-13 20:15:29 +0000801{
802 caddr_t pnt, end;
803
804 /* Pnt points out socket data start point. */
805 pnt = (caddr_t)(rtm + 1);
806 end = ((caddr_t)rtm) + rtm->rtm_msglen;
807
808 /* rt_msghdr version check. */
809 if (rtm->rtm_version != RTM_VERSION)
810 zlog (NULL, LOG_WARNING,
811 "Routing message version different %d should be %d."
812 "This may cause problem\n", rtm->rtm_version, RTM_VERSION);
paul62debbb2005-06-14 14:07:07 +0000813
paul718e3742002-12-13 20:15:29 +0000814 /* Be sure structure is cleared */
815 memset (dest, 0, sizeof (union sockunion));
816 memset (gate, 0, sizeof (union sockunion));
817 memset (mask, 0, sizeof (union sockunion));
818
819 /* We fetch each socket variable into sockunion. */
paul62debbb2005-06-14 14:07:07 +0000820 RTA_ADDR_GET (dest, RTA_DST, rtm->rtm_addrs, pnt);
821 RTA_ADDR_GET (gate, RTA_GATEWAY, rtm->rtm_addrs, pnt);
822 RTA_ATTR_GET (mask, RTA_NETMASK, rtm->rtm_addrs, pnt);
823 RTA_ADDR_GET (NULL, RTA_GENMASK, rtm->rtm_addrs, pnt);
paul6fe70d12005-11-12 22:55:10 +0000824 RTA_NAME_GET (ifname, RTA_IFP, rtm->rtm_addrs, pnt, *ifnlen);
paul62debbb2005-06-14 14:07:07 +0000825 RTA_ADDR_GET (NULL, RTA_IFA, rtm->rtm_addrs, pnt);
826 RTA_ADDR_GET (NULL, RTA_AUTHOR, rtm->rtm_addrs, pnt);
827 RTA_ADDR_GET (NULL, RTA_BRD, rtm->rtm_addrs, pnt);
paul718e3742002-12-13 20:15:29 +0000828
829 /* If there is netmask information set it's family same as
830 destination family*/
831 if (rtm->rtm_addrs & RTA_NETMASK)
832 mask->sa.sa_family = dest->sa.sa_family;
833
834 /* Assert read up to the end of pointer. */
835 if (pnt != end)
Denis Ovsienko85a2ebf2011-12-05 19:36:06 +0400836 zlog (NULL, LOG_WARNING, "rtm_read() doesn't read all socket data.");
paul718e3742002-12-13 20:15:29 +0000837
838 return rtm->rtm_flags;
839}
840
paulec1a4282005-11-24 15:15:17 +0000841void
paul718e3742002-12-13 20:15:29 +0000842rtm_read (struct rt_msghdr *rtm)
843{
844 int flags;
845 u_char zebra_flags;
846 union sockunion dest, mask, gate;
paul6fe70d12005-11-12 22:55:10 +0000847 char ifname[INTERFACE_NAMSIZ + 1];
848 short ifnlen = 0;
paul718e3742002-12-13 20:15:29 +0000849
850 zebra_flags = 0;
851
paul718e3742002-12-13 20:15:29 +0000852 /* Read destination and netmask and gateway from rtm message
853 structure. */
paul6fe70d12005-11-12 22:55:10 +0000854 flags = rtm_read_mesg (rtm, &dest, &mask, &gate, ifname, &ifnlen);
Denis Ovsienko6da59802007-08-17 14:16:30 +0000855 if (!(flags & RTF_DONE))
856 return;
Denis Ovsienkodc958242007-08-13 16:03:06 +0000857 if (IS_ZEBRA_DEBUG_KERNEL)
858 zlog_debug ("%s: got rtm of type %d (%s)", __func__, rtm->rtm_type,
Denis Ovsienko2d844522007-09-14 11:31:55 +0000859 lookup (rtm_type_str, rtm->rtm_type));
paul718e3742002-12-13 20:15:29 +0000860
861#ifdef RTF_CLONED /*bsdi, netbsd 1.6*/
862 if (flags & RTF_CLONED)
863 return;
864#endif
865#ifdef RTF_WASCLONED /*freebsd*/
866 if (flags & RTF_WASCLONED)
867 return;
868#endif
869
870 if ((rtm->rtm_type == RTM_ADD) && ! (flags & RTF_UP))
871 return;
872
873 /* This is connected route. */
874 if (! (flags & RTF_GATEWAY))
875 return;
876
877 if (flags & RTF_PROTO1)
878 SET_FLAG (zebra_flags, ZEBRA_FLAG_SELFROUTE);
879
880 /* This is persistent route. */
881 if (flags & RTF_STATIC)
882 SET_FLAG (zebra_flags, ZEBRA_FLAG_STATIC);
883
hasso81dfcaa2003-05-25 19:21:25 +0000884 /* This is a reject or blackhole route */
885 if (flags & RTF_REJECT)
886 SET_FLAG (zebra_flags, ZEBRA_FLAG_REJECT);
887 if (flags & RTF_BLACKHOLE)
888 SET_FLAG (zebra_flags, ZEBRA_FLAG_BLACKHOLE);
889
paul718e3742002-12-13 20:15:29 +0000890 if (dest.sa.sa_family == AF_INET)
891 {
892 struct prefix_ipv4 p;
893
894 p.family = AF_INET;
895 p.prefix = dest.sin.sin_addr;
896 if (flags & RTF_HOST)
897 p.prefixlen = IPV4_MAX_PREFIXLEN;
898 else
899 p.prefixlen = ip_masklen (mask.sin.sin_addr);
paulca162182005-09-12 16:58:52 +0000900
Denis Ovsienkodc958242007-08-13 16:03:06 +0000901 /* Catch self originated messages and match them against our current RIB.
902 * At the same time, ignore unconfirmed messages, they should be tracked
903 * by rtm_write() and kernel_rtm_ipv4().
904 */
Denis Ovsienko96934e62007-09-14 14:56:28 +0000905 if (rtm->rtm_type != RTM_GET && rtm->rtm_pid == pid)
Denis Ovsienkodc958242007-08-13 16:03:06 +0000906 {
907 char buf[INET_ADDRSTRLEN], gate_buf[INET_ADDRSTRLEN];
908 int ret;
Denis Ovsienkodc958242007-08-13 16:03:06 +0000909 if (! IS_ZEBRA_DEBUG_RIB)
910 return;
911 ret = rib_lookup_ipv4_route (&p, &gate);
912 inet_ntop (AF_INET, &p.prefix, buf, INET_ADDRSTRLEN);
913 switch (rtm->rtm_type)
914 {
915 case RTM_ADD:
916 case RTM_GET:
917 case RTM_CHANGE:
918 /* The kernel notifies us about a new route in FIB created by us.
919 Do we have a correspondent entry in our RIB? */
920 switch (ret)
921 {
922 case ZEBRA_RIB_NOTFOUND:
923 zlog_debug ("%s: %s %s/%d: desync: RR isn't yet in RIB, while already in FIB",
Denis Ovsienko2d844522007-09-14 11:31:55 +0000924 __func__, lookup (rtm_type_str, rtm->rtm_type), buf, p.prefixlen);
Denis Ovsienkodc958242007-08-13 16:03:06 +0000925 break;
926 case ZEBRA_RIB_FOUND_CONNECTED:
927 case ZEBRA_RIB_FOUND_NOGATE:
928 inet_ntop (AF_INET, &gate.sin.sin_addr, gate_buf, INET_ADDRSTRLEN);
929 zlog_debug ("%s: %s %s/%d: desync: RR is in RIB, but gate differs (ours is %s)",
Denis Ovsienko2d844522007-09-14 11:31:55 +0000930 __func__, lookup (rtm_type_str, rtm->rtm_type), buf, p.prefixlen, gate_buf);
Denis Ovsienkodc958242007-08-13 16:03:06 +0000931 break;
932 case ZEBRA_RIB_FOUND_EXACT: /* RIB RR == FIB RR */
933 zlog_debug ("%s: %s %s/%d: done Ok",
Denis Ovsienko2d844522007-09-14 11:31:55 +0000934 __func__, lookup (rtm_type_str, rtm->rtm_type), buf, p.prefixlen);
Denis Ovsienkodc958242007-08-13 16:03:06 +0000935 rib_lookup_and_dump (&p);
936 return;
937 break;
938 }
939 break;
940 case RTM_DELETE:
941 /* The kernel notifies us about a route deleted by us. Do we still
942 have it in the RIB? Do we have anything instead? */
943 switch (ret)
944 {
945 case ZEBRA_RIB_FOUND_EXACT:
946 zlog_debug ("%s: %s %s/%d: desync: RR is still in RIB, while already not in FIB",
Denis Ovsienko2d844522007-09-14 11:31:55 +0000947 __func__, lookup (rtm_type_str, rtm->rtm_type), buf, p.prefixlen);
Denis Ovsienkodc958242007-08-13 16:03:06 +0000948 rib_lookup_and_dump (&p);
949 break;
950 case ZEBRA_RIB_FOUND_CONNECTED:
951 case ZEBRA_RIB_FOUND_NOGATE:
952 zlog_debug ("%s: %s %s/%d: desync: RR is still in RIB, plus gate differs",
Denis Ovsienko2d844522007-09-14 11:31:55 +0000953 __func__, lookup (rtm_type_str, rtm->rtm_type), buf, p.prefixlen);
Denis Ovsienkodc958242007-08-13 16:03:06 +0000954 rib_lookup_and_dump (&p);
955 break;
956 case ZEBRA_RIB_NOTFOUND: /* RIB RR == FIB RR */
957 zlog_debug ("%s: %s %s/%d: done Ok",
Denis Ovsienko2d844522007-09-14 11:31:55 +0000958 __func__, lookup (rtm_type_str, rtm->rtm_type), buf, p.prefixlen);
Denis Ovsienkodc958242007-08-13 16:03:06 +0000959 rib_lookup_and_dump (&p);
960 return;
961 break;
962 }
963 break;
964 default:
965 zlog_debug ("%s: %s/%d: warning: loopback RTM of type %s received",
Denis Ovsienko2d844522007-09-14 11:31:55 +0000966 __func__, buf, p.prefixlen, lookup (rtm_type_str, rtm->rtm_type));
Denis Ovsienkodc958242007-08-13 16:03:06 +0000967 }
968 return;
969 }
970
paulca162182005-09-12 16:58:52 +0000971 /* Change, delete the old prefix, we have no further information
972 * to specify the route really
973 */
974 if (rtm->rtm_type == RTM_CHANGE)
975 rib_delete_ipv4 (ZEBRA_ROUTE_KERNEL, zebra_flags, &p,
Denis Ovsienko6eac79a2011-12-05 13:43:18 +0400976 NULL, 0, 0, SAFI_UNICAST);
paulca162182005-09-12 16:58:52 +0000977
978 if (rtm->rtm_type == RTM_GET
979 || rtm->rtm_type == RTM_ADD
980 || rtm->rtm_type == RTM_CHANGE)
paul718e3742002-12-13 20:15:29 +0000981 rib_add_ipv4 (ZEBRA_ROUTE_KERNEL, zebra_flags,
G.Balajicddf3912011-11-26 21:59:32 +0400982 &p, &gate.sin.sin_addr, NULL, 0, 0, 0, 0, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +0000983 else
984 rib_delete_ipv4 (ZEBRA_ROUTE_KERNEL, zebra_flags,
Denis Ovsienko6eac79a2011-12-05 13:43:18 +0400985 &p, &gate.sin.sin_addr, 0, 0, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +0000986 }
987#ifdef HAVE_IPV6
988 if (dest.sa.sa_family == AF_INET6)
989 {
Denis Ovsienko5619f562007-10-24 13:13:21 +0000990 /* One day we might have a debug section here like one in the
991 * IPv4 case above. Just ignore own messages at the moment.
992 */
993 if (rtm->rtm_type != RTM_GET && rtm->rtm_pid == pid)
994 return;
paul718e3742002-12-13 20:15:29 +0000995 struct prefix_ipv6 p;
996 unsigned int ifindex = 0;
997
998 p.family = AF_INET6;
999 p.prefix = dest.sin6.sin6_addr;
1000 if (flags & RTF_HOST)
1001 p.prefixlen = IPV6_MAX_PREFIXLEN;
1002 else
1003 p.prefixlen = ip6_masklen (mask.sin6.sin6_addr);
1004
1005#ifdef KAME
1006 if (IN6_IS_ADDR_LINKLOCAL (&gate.sin6.sin6_addr))
1007 {
1008 ifindex = IN6_LINKLOCAL_IFINDEX (gate.sin6.sin6_addr);
1009 SET_IN6_LINKLOCAL_IFINDEX (gate.sin6.sin6_addr, 0);
1010 }
1011#endif /* KAME */
1012
paulca162182005-09-12 16:58:52 +00001013 /* CHANGE: delete the old prefix, we have no further information
1014 * to specify the route really
1015 */
1016 if (rtm->rtm_type == RTM_CHANGE)
paul6621ca82005-11-23 13:02:08 +00001017 rib_delete_ipv6 (ZEBRA_ROUTE_KERNEL, zebra_flags, &p,
Denis Ovsienko6eac79a2011-12-05 13:43:18 +04001018 NULL, 0, 0, SAFI_UNICAST);
paulca162182005-09-12 16:58:52 +00001019
1020 if (rtm->rtm_type == RTM_GET
1021 || rtm->rtm_type == RTM_ADD
1022 || rtm->rtm_type == RTM_CHANGE)
paul718e3742002-12-13 20:15:29 +00001023 rib_add_ipv6 (ZEBRA_ROUTE_KERNEL, zebra_flags,
Denis Ovsienko6eac79a2011-12-05 13:43:18 +04001024 &p, &gate.sin6.sin6_addr, ifindex, 0, 0, 0, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +00001025 else
1026 rib_delete_ipv6 (ZEBRA_ROUTE_KERNEL, zebra_flags,
Denis Ovsienko6eac79a2011-12-05 13:43:18 +04001027 &p, &gate.sin6.sin6_addr, ifindex, 0, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +00001028 }
1029#endif /* HAVE_IPV6 */
1030}
1031
1032/* Interface function for the kernel routing table updates. Support
paul6621ca82005-11-23 13:02:08 +00001033 * for RTM_CHANGE will be needed.
1034 * Exported only for rt_socket.c
1035 */
paul718e3742002-12-13 20:15:29 +00001036int
1037rtm_write (int message,
1038 union sockunion *dest,
1039 union sockunion *mask,
1040 union sockunion *gate,
1041 unsigned int index,
1042 int zebra_flags,
1043 int metric)
1044{
1045 int ret;
1046 caddr_t pnt;
1047 struct interface *ifp;
paul718e3742002-12-13 20:15:29 +00001048
1049 /* Sequencial number of routing message. */
1050 static int msg_seq = 0;
1051
1052 /* Struct of rt_msghdr and buffer for storing socket's data. */
1053 struct
1054 {
1055 struct rt_msghdr rtm;
1056 char buf[512];
1057 } msg;
1058
paul718e3742002-12-13 20:15:29 +00001059 if (routing_sock < 0)
1060 return ZEBRA_ERR_EPERM;
1061
1062 /* Clear and set rt_msghdr values */
1063 memset (&msg, 0, sizeof (struct rt_msghdr));
1064 msg.rtm.rtm_version = RTM_VERSION;
1065 msg.rtm.rtm_type = message;
1066 msg.rtm.rtm_seq = msg_seq++;
1067 msg.rtm.rtm_addrs = RTA_DST;
1068 msg.rtm.rtm_addrs |= RTA_GATEWAY;
1069 msg.rtm.rtm_flags = RTF_UP;
1070 msg.rtm.rtm_index = index;
1071
1072 if (metric != 0)
1073 {
1074 msg.rtm.rtm_rmx.rmx_hopcount = metric;
1075 msg.rtm.rtm_inits |= RTV_HOPCOUNT;
1076 }
1077
1078 ifp = if_lookup_by_index (index);
1079
1080 if (gate && message == RTM_ADD)
1081 msg.rtm.rtm_flags |= RTF_GATEWAY;
1082
David Warde6f148e2009-12-03 21:43:11 +03001083 /* When RTF_CLONING is unavailable on BSD, should we set some
1084 * other flag instead?
1085 */
1086#ifdef RTF_CLONING
paul718e3742002-12-13 20:15:29 +00001087 if (! gate && message == RTM_ADD && ifp &&
1088 (ifp->flags & IFF_POINTOPOINT) == 0)
1089 msg.rtm.rtm_flags |= RTF_CLONING;
David Warde6f148e2009-12-03 21:43:11 +03001090#endif /* RTF_CLONING */
paul718e3742002-12-13 20:15:29 +00001091
1092 /* If no protocol specific gateway is specified, use link
1093 address for gateway. */
1094 if (! gate)
1095 {
1096 if (!ifp)
1097 {
Denis Ovsienkodc958242007-08-13 16:03:06 +00001098 char dest_buf[INET_ADDRSTRLEN] = "NULL", mask_buf[INET_ADDRSTRLEN] = "255.255.255.255";
1099 if (dest)
1100 inet_ntop (AF_INET, &dest->sin.sin_addr, dest_buf, INET_ADDRSTRLEN);
1101 if (mask)
1102 inet_ntop (AF_INET, &mask->sin.sin_addr, mask_buf, INET_ADDRSTRLEN);
1103 zlog_warn ("%s: %s/%s: gate == NULL and no gateway found for ifindex %d",
1104 __func__, dest_buf, mask_buf, index);
paul718e3742002-12-13 20:15:29 +00001105 return -1;
1106 }
1107 gate = (union sockunion *) & ifp->sdl;
1108 }
1109
1110 if (mask)
1111 msg.rtm.rtm_addrs |= RTA_NETMASK;
1112 else if (message == RTM_ADD)
1113 msg.rtm.rtm_flags |= RTF_HOST;
1114
1115 /* Tagging route with flags */
1116 msg.rtm.rtm_flags |= (RTF_PROTO1);
1117
1118 /* Additional flags. */
1119 if (zebra_flags & ZEBRA_FLAG_BLACKHOLE)
1120 msg.rtm.rtm_flags |= RTF_BLACKHOLE;
hasso81dfcaa2003-05-25 19:21:25 +00001121 if (zebra_flags & ZEBRA_FLAG_REJECT)
1122 msg.rtm.rtm_flags |= RTF_REJECT;
1123
paul718e3742002-12-13 20:15:29 +00001124
paul718e3742002-12-13 20:15:29 +00001125#define SOCKADDRSET(X,R) \
1126 if (msg.rtm.rtm_addrs & (R)) \
1127 { \
paul6fe70d12005-11-12 22:55:10 +00001128 int len = SAROUNDUP (X); \
paul718e3742002-12-13 20:15:29 +00001129 memcpy (pnt, (caddr_t)(X), len); \
1130 pnt += len; \
1131 }
paul718e3742002-12-13 20:15:29 +00001132
1133 pnt = (caddr_t) msg.buf;
1134
1135 /* Write each socket data into rtm message buffer */
1136 SOCKADDRSET (dest, RTA_DST);
1137 SOCKADDRSET (gate, RTA_GATEWAY);
1138 SOCKADDRSET (mask, RTA_NETMASK);
1139
1140 msg.rtm.rtm_msglen = pnt - (caddr_t) &msg;
1141
1142 ret = write (routing_sock, &msg, msg.rtm.rtm_msglen);
1143
1144 if (ret != msg.rtm.rtm_msglen)
1145 {
1146 if (errno == EEXIST)
1147 return ZEBRA_ERR_RTEXIST;
1148 if (errno == ENETUNREACH)
1149 return ZEBRA_ERR_RTUNREACH;
Denis Ovsienkodc958242007-08-13 16:03:06 +00001150 if (errno == ESRCH)
1151 return ZEBRA_ERR_RTNOEXIST;
paul718e3742002-12-13 20:15:29 +00001152
Denis Ovsienkodc958242007-08-13 16:03:06 +00001153 zlog_warn ("%s: write : %s (%d)", __func__, safe_strerror (errno), errno);
1154 return ZEBRA_ERR_KERNEL;
paul718e3742002-12-13 20:15:29 +00001155 }
Denis Ovsienkodc958242007-08-13 16:03:06 +00001156 return ZEBRA_ERR_NOERROR;
paul718e3742002-12-13 20:15:29 +00001157}
1158
David Lamparter6b0655a2014-06-04 06:53:35 +02001159
paul718e3742002-12-13 20:15:29 +00001160#include "thread.h"
1161#include "zebra/zserv.h"
1162
paul718e3742002-12-13 20:15:29 +00001163/* For debug purpose. */
ajsb6178002004-12-07 21:12:56 +00001164static void
paul718e3742002-12-13 20:15:29 +00001165rtmsg_debug (struct rt_msghdr *rtm)
1166{
Denis Ovsienko2d844522007-09-14 11:31:55 +00001167 zlog_debug ("Kernel: Len: %d Type: %s", rtm->rtm_msglen, lookup (rtm_type_str, rtm->rtm_type));
paul718e3742002-12-13 20:15:29 +00001168 rtm_flag_dump (rtm->rtm_flags);
ajsb6178002004-12-07 21:12:56 +00001169 zlog_debug ("Kernel: message seq %d", rtm->rtm_seq);
paul6fe70d12005-11-12 22:55:10 +00001170 zlog_debug ("Kernel: pid %d, rtm_addrs 0x%x", rtm->rtm_pid, rtm->rtm_addrs);
paul718e3742002-12-13 20:15:29 +00001171}
1172
1173/* This is pretty gross, better suggestions welcome -- mhandler */
1174#ifndef RTAX_MAX
1175#ifdef RTA_NUMBITS
1176#define RTAX_MAX RTA_NUMBITS
1177#else
1178#define RTAX_MAX 8
1179#endif /* RTA_NUMBITS */
1180#endif /* RTAX_MAX */
1181
1182/* Kernel routing table and interface updates via routing socket. */
paul6621ca82005-11-23 13:02:08 +00001183static int
paul718e3742002-12-13 20:15:29 +00001184kernel_read (struct thread *thread)
1185{
1186 int sock;
1187 int nbytes;
1188 struct rt_msghdr *rtm;
1189
gdtdbee01f2004-01-06 00:36:51 +00001190 /*
1191 * This must be big enough for any message the kernel might send.
gdtb27900b2004-01-08 15:44:29 +00001192 * Rather than determining how many sockaddrs of what size might be
1193 * in each particular message, just use RTAX_MAX of sockaddr_storage
1194 * for each. Note that the sockaddrs must be after each message
1195 * definition, or rather after whichever happens to be the largest,
1196 * since the buffer needs to be big enough for a message and the
1197 * sockaddrs together.
gdtdbee01f2004-01-06 00:36:51 +00001198 */
paul718e3742002-12-13 20:15:29 +00001199 union
1200 {
1201 /* Routing information. */
1202 struct
1203 {
1204 struct rt_msghdr rtm;
gdtb27900b2004-01-08 15:44:29 +00001205 struct sockaddr_storage addr[RTAX_MAX];
paul718e3742002-12-13 20:15:29 +00001206 } r;
1207
1208 /* Interface information. */
1209 struct
1210 {
1211 struct if_msghdr ifm;
gdtb27900b2004-01-08 15:44:29 +00001212 struct sockaddr_storage addr[RTAX_MAX];
paul718e3742002-12-13 20:15:29 +00001213 } im;
1214
1215 /* Interface address information. */
1216 struct
1217 {
1218 struct ifa_msghdr ifa;
gdtb27900b2004-01-08 15:44:29 +00001219 struct sockaddr_storage addr[RTAX_MAX];
paul718e3742002-12-13 20:15:29 +00001220 } ia;
1221
1222#ifdef RTM_IFANNOUNCE
1223 /* Interface arrival/departure */
1224 struct
1225 {
1226 struct if_announcemsghdr ifan;
gdtb27900b2004-01-08 15:44:29 +00001227 struct sockaddr_storage addr[RTAX_MAX];
paul718e3742002-12-13 20:15:29 +00001228 } ian;
1229#endif /* RTM_IFANNOUNCE */
1230
1231 } buf;
1232
1233 /* Fetch routing socket. */
1234 sock = THREAD_FD (thread);
1235
1236 nbytes= read (sock, &buf, sizeof buf);
1237
1238 if (nbytes <= 0)
1239 {
1240 if (nbytes < 0 && errno != EWOULDBLOCK && errno != EAGAIN)
ajs6099b3b2004-11-20 02:06:59 +00001241 zlog_warn ("routing socket error: %s", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001242 return 0;
1243 }
1244
paul9bcdb632003-07-08 08:09:45 +00001245 thread_add_read (zebrad.master, kernel_read, NULL, sock);
paul718e3742002-12-13 20:15:29 +00001246
hasso726f9b22003-05-25 21:04:54 +00001247 if (IS_ZEBRA_DEBUG_KERNEL)
1248 rtmsg_debug (&buf.r.rtm);
paul718e3742002-12-13 20:15:29 +00001249
1250 rtm = &buf.r.rtm;
1251
gdtb27900b2004-01-08 15:44:29 +00001252 /*
1253 * Ensure that we didn't drop any data, so that processing routines
1254 * can assume they have the whole message.
1255 */
gdtda26e3b2004-01-05 17:20:59 +00001256 if (rtm->rtm_msglen != nbytes)
1257 {
1258 zlog_warn ("kernel_read: rtm->rtm_msglen %d, nbytes %d, type %d\n",
1259 rtm->rtm_msglen, nbytes, rtm->rtm_type);
1260 return -1;
1261 }
1262
paul718e3742002-12-13 20:15:29 +00001263 switch (rtm->rtm_type)
1264 {
1265 case RTM_ADD:
1266 case RTM_DELETE:
paulca162182005-09-12 16:58:52 +00001267 case RTM_CHANGE:
paul718e3742002-12-13 20:15:29 +00001268 rtm_read (rtm);
1269 break;
1270 case RTM_IFINFO:
1271 ifm_read (&buf.im.ifm);
1272 break;
1273 case RTM_NEWADDR:
1274 case RTM_DELADDR:
1275 ifam_read (&buf.ia.ifa);
1276 break;
1277#ifdef RTM_IFANNOUNCE
1278 case RTM_IFANNOUNCE:
1279 ifan_read (&buf.ian.ifan);
1280 break;
1281#endif /* RTM_IFANNOUNCE */
1282 default:
hasso726f9b22003-05-25 21:04:54 +00001283 if (IS_ZEBRA_DEBUG_KERNEL)
ajsb6178002004-12-07 21:12:56 +00001284 zlog_debug("Unprocessed RTM_type: %d", rtm->rtm_type);
paul718e3742002-12-13 20:15:29 +00001285 break;
1286 }
1287 return 0;
1288}
1289
1290/* Make routing socket. */
paul6621ca82005-11-23 13:02:08 +00001291static void
1292routing_socket (void)
paul718e3742002-12-13 20:15:29 +00001293{
pauledd7c242003-06-04 13:59:38 +00001294 if ( zserv_privs.change (ZPRIVS_RAISE) )
1295 zlog_err ("routing_socket: Can't raise privileges");
1296
paul718e3742002-12-13 20:15:29 +00001297 routing_sock = socket (AF_ROUTE, SOCK_RAW, 0);
1298
1299 if (routing_sock < 0)
1300 {
pauledd7c242003-06-04 13:59:38 +00001301 if ( zserv_privs.change (ZPRIVS_LOWER) )
1302 zlog_err ("routing_socket: Can't lower privileges");
paul718e3742002-12-13 20:15:29 +00001303 zlog_warn ("Can't init kernel routing socket");
1304 return;
1305 }
1306
paul865b8522005-01-05 08:30:35 +00001307 /* XXX: Socket should be NONBLOCK, however as we currently
1308 * discard failed writes, this will lead to inconsistencies.
1309 * For now, socket must be blocking.
1310 */
1311 /*if (fcntl (routing_sock, F_SETFL, O_NONBLOCK) < 0)
1312 zlog_warn ("Can't set O_NONBLOCK to routing socket");*/
1313
pauledd7c242003-06-04 13:59:38 +00001314 if ( zserv_privs.change (ZPRIVS_LOWER) )
1315 zlog_err ("routing_socket: Can't lower privileges");
paul718e3742002-12-13 20:15:29 +00001316
1317 /* kernel_read needs rewrite. */
paul9bcdb632003-07-08 08:09:45 +00001318 thread_add_read (zebrad.master, kernel_read, NULL, routing_sock);
paul718e3742002-12-13 20:15:29 +00001319}
1320
1321/* Exported interface function. This function simply calls
1322 routing_socket (). */
1323void
paul6621ca82005-11-23 13:02:08 +00001324kernel_init (void)
paul718e3742002-12-13 20:15:29 +00001325{
1326 routing_socket ();
1327}