blob: fd0d8fd1b6560821d43889a4fe277a5943e0db81 [file] [log] [blame]
paul718e3742002-12-13 20:15:29 +00001/* Kernel communication using routing socket.
2 * Copyright (C) 1999 Kunihiro Ishiguro
3 *
4 * This file is part of GNU Zebra.
5 *
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
9 * later version.
10 *
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with GNU Zebra; see the file COPYING. If not, write to the Free
18 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 * 02111-1307, USA.
20 */
21
22#include <zebra.h>
23
24#include "if.h"
25#include "prefix.h"
26#include "sockunion.h"
27#include "connected.h"
28#include "memory.h"
29#include "ioctl.h"
30#include "log.h"
31#include "str.h"
32#include "table.h"
33#include "rib.h"
pauledd7c242003-06-04 13:59:38 +000034#include "privs.h"
Feng Lu0d0686f2015-05-22 11:40:02 +020035#include "vrf.h"
paul718e3742002-12-13 20:15:29 +000036
37#include "zebra/interface.h"
38#include "zebra/zserv.h"
39#include "zebra/debug.h"
paulec1a4282005-11-24 15:15:17 +000040#include "zebra/kernel_socket.h"
Feng Lu758fb8f2014-07-03 18:23:09 +080041#include "zebra/rib.h"
paul718e3742002-12-13 20:15:29 +000042
pauledd7c242003-06-04 13:59:38 +000043extern struct zebra_privs_t zserv_privs;
paul9bcdb632003-07-08 08:09:45 +000044extern struct zebra_t zebrad;
pauledd7c242003-06-04 13:59:38 +000045
gdt4bfbea82004-01-06 01:13:05 +000046/*
Greg Troxel273b1bd2014-12-02 14:51:49 -050047 * Historically, the BSD routing socket has aligned data following a
48 * struct sockaddr to sizeof(long), which was 4 bytes on some
49 * platforms, and 8 bytes on others. NetBSD 6 changed the routing
50 * socket to align to sizeof(uint64_t), which is 8 bytes. OS X
51 * appears to align to sizeof(int), which is 4 bytes.
gdt4bfbea82004-01-06 01:13:05 +000052 *
Greg Troxel273b1bd2014-12-02 14:51:49 -050053 * Alignment of zero-sized sockaddrs is nonsensical, but historically
54 * BSD defines RT_ROUNDUP(0) to be the alignment interval (rather than
55 * 0). We follow this practice without questioning it, but it is a
56 * bug if quagga calls ROUNDUP with 0.
gdt4bfbea82004-01-06 01:13:05 +000057 */
Greg Troxel273b1bd2014-12-02 14:51:49 -050058
59/*
60 * Because of these varying conventions, the only sane approach is for
61 * the <net/route.h> header to define some flavor of ROUNDUP macro.
62 */
David Lamparter7e923222015-03-03 21:04:20 +010063
64#if defined(SA_SIZE)
65/* SAROUNDUP is the only thing we need, and SA_SIZE provides that */
66#define SAROUNDUP(a) SA_SIZE(a)
67#else /* !SA_SIZE */
68
Greg Troxel273b1bd2014-12-02 14:51:49 -050069#if defined(RT_ROUNDUP)
70#define ROUNDUP(a) RT_ROUNDUP(a)
71#endif /* defined(RT_ROUNDUP) */
72
73/*
74 * If ROUNDUP has not yet been defined in terms of platform-provided
75 * defines, attempt to cope with heuristics.
76 */
77#if !defined(ROUNDUP)
78
79/*
80 * It's a bug for a platform not to define rounding/alignment for
81 * sockaddrs on the routing socket. This warning really is
82 * intentional, to provoke filing bug reports with operating systems
83 * that don't define RT_ROUNDUP or equivalent.
84 */
85#warning "net/route.h does not define RT_ROUNDUP; making unwarranted assumptions!"
86
87/* OS X (Xcode as of 2014-12) is known not to define RT_ROUNDUP */
Doug VanLeuven3b33de62012-10-10 22:10:14 +000088#ifdef __APPLE__
Greg Troxel273b1bd2014-12-02 14:51:49 -050089#define ROUNDUP_TYPE int
Greg Troxel941789e2015-03-23 15:16:29 -040090#else
91#define ROUNDUP_TYPE long
Doug VanLeuven3b33de62012-10-10 22:10:14 +000092#endif
paul718e3742002-12-13 20:15:29 +000093
Greg Troxel273b1bd2014-12-02 14:51:49 -050094#define ROUNDUP(a) \
95 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(ROUNDUP_TYPE) - 1))) : sizeof(ROUNDUP_TYPE))
96
97#endif /* defined(ROUNDUP) */
98
gdt4bfbea82004-01-06 01:13:05 +000099/*
100 * Given a pointer (sockaddr or void *), return the number of bytes
101 * taken up by the sockaddr and any padding needed for alignment.
102 */
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000103#if defined(HAVE_STRUCT_SOCKADDR_SA_LEN)
gdt4bfbea82004-01-06 01:13:05 +0000104#define SAROUNDUP(X) ROUNDUP(((struct sockaddr *)(X))->sa_len)
paul30be8022003-10-22 02:51:38 +0000105#elif defined(HAVE_IPV6)
gdt4bfbea82004-01-06 01:13:05 +0000106/*
107 * One would hope all fixed-size structure definitions are aligned,
108 * but round them up nonetheless.
109 */
110#define SAROUNDUP(X) \
paul3e95a072003-09-24 00:05:45 +0000111 (((struct sockaddr *)(X))->sa_family == AF_INET ? \
112 ROUNDUP(sizeof(struct sockaddr_in)):\
113 (((struct sockaddr *)(X))->sa_family == AF_INET6 ? \
114 ROUNDUP(sizeof(struct sockaddr_in6)) : \
115 (((struct sockaddr *)(X))->sa_family == AF_LINK ? \
paulc50ae8b2004-05-11 11:31:07 +0000116 ROUNDUP(sizeof(struct sockaddr_dl)) : sizeof(struct sockaddr))))
paul30be8022003-10-22 02:51:38 +0000117#else /* HAVE_IPV6 */
gdt4bfbea82004-01-06 01:13:05 +0000118#define SAROUNDUP(X) \
paul30be8022003-10-22 02:51:38 +0000119 (((struct sockaddr *)(X))->sa_family == AF_INET ? \
120 ROUNDUP(sizeof(struct sockaddr_in)):\
121 (((struct sockaddr *)(X))->sa_family == AF_LINK ? \
122 ROUNDUP(sizeof(struct sockaddr_dl)) : sizeof(struct sockaddr)))
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000123#endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
paul718e3742002-12-13 20:15:29 +0000124
David Lamparter7e923222015-03-03 21:04:20 +0100125#endif /* !SA_SIZE */
126
Doug VanLeuvena05df8f2012-10-10 16:11:36 -0700127/*
128 * We use a call to an inline function to copy (PNT) to (DEST)
129 * 1. Calculating the length of the copy requires an #ifdef to determine
130 * if sa_len is a field and can't be used directly inside a #define
131 * 2. So the compiler doesn't complain when DEST is NULL, which is only true
132 * when we are skipping the copy and incrementing to the next SA
paulec1a4282005-11-24 15:15:17 +0000133 */
David Lamparter3e9e2c92015-04-10 09:14:58 +0200134static inline void
Doug VanLeuvena05df8f2012-10-10 16:11:36 -0700135rta_copy (union sockunion *dest, caddr_t src) {
136 int len;
137#ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
138 len = (((struct sockaddr *)src)->sa_len > sizeof (*dest)) ?
139 sizeof (*dest) : ((struct sockaddr *)src)->sa_len ;
140#else
141 len = (SAROUNDUP (src) > sizeof (*dest)) ?
142 sizeof (*dest) : SAROUNDUP (src) ;
143#endif
144 memcpy (dest, src, len);
145}
146
paul62debbb2005-06-14 14:07:07 +0000147#define RTA_ADDR_GET(DEST, RTA, RTMADDRS, PNT) \
148 if ((RTMADDRS) & (RTA)) \
149 { \
150 int len = SAROUNDUP ((PNT)); \
paulea6f82b2005-06-28 17:20:26 +0000151 if ( ((DEST) != NULL) && \
paul62debbb2005-06-14 14:07:07 +0000152 af_check (((struct sockaddr *)(PNT))->sa_family)) \
Doug VanLeuvena05df8f2012-10-10 16:11:36 -0700153 rta_copy((DEST), (PNT)); \
paul62debbb2005-06-14 14:07:07 +0000154 (PNT) += len; \
155 }
156#define RTA_ATTR_GET(DEST, RTA, RTMADDRS, PNT) \
157 if ((RTMADDRS) & (RTA)) \
158 { \
159 int len = SAROUNDUP ((PNT)); \
paulec1a4282005-11-24 15:15:17 +0000160 if ((DEST) != NULL) \
Doug VanLeuvena05df8f2012-10-10 16:11:36 -0700161 rta_copy((DEST), (PNT)); \
paul62debbb2005-06-14 14:07:07 +0000162 (PNT) += len; \
163 }
164
paul6fe70d12005-11-12 22:55:10 +0000165#define RTA_NAME_GET(DEST, RTA, RTMADDRS, PNT, LEN) \
166 if ((RTMADDRS) & (RTA)) \
167 { \
paulec1a4282005-11-24 15:15:17 +0000168 u_char *pdest = (u_char *) (DEST); \
paul6fe70d12005-11-12 22:55:10 +0000169 int len = SAROUNDUP ((PNT)); \
170 struct sockaddr_dl *sdl = (struct sockaddr_dl *)(PNT); \
171 if (IS_ZEBRA_DEBUG_KERNEL) \
172 zlog_debug ("%s: RTA_SDL_GET nlen %d, alen %d", \
173 __func__, sdl->sdl_nlen, sdl->sdl_alen); \
174 if ( ((DEST) != NULL) && (sdl->sdl_family == AF_LINK) \
175 && (sdl->sdl_nlen < IFNAMSIZ) && (sdl->sdl_nlen <= len) ) \
176 { \
paulec1a4282005-11-24 15:15:17 +0000177 memcpy (pdest, sdl->sdl_data, sdl->sdl_nlen); \
178 pdest[sdl->sdl_nlen] = '\0'; \
paul6fe70d12005-11-12 22:55:10 +0000179 (LEN) = sdl->sdl_nlen; \
180 } \
181 (PNT) += len; \
182 } \
183 else \
184 { \
185 (LEN) = 0; \
186 }
paul718e3742002-12-13 20:15:29 +0000187/* Routing socket message types. */
Stephen Hemminger1423c802008-08-14 17:59:25 +0100188const struct message rtm_type_str[] =
paul718e3742002-12-13 20:15:29 +0000189{
190 {RTM_ADD, "RTM_ADD"},
191 {RTM_DELETE, "RTM_DELETE"},
192 {RTM_CHANGE, "RTM_CHANGE"},
193 {RTM_GET, "RTM_GET"},
194 {RTM_LOSING, "RTM_LOSING"},
195 {RTM_REDIRECT, "RTM_REDIRECT"},
196 {RTM_MISS, "RTM_MISS"},
197 {RTM_LOCK, "RTM_LOCK"},
Greg Troxel9458b812006-09-13 12:13:08 +0000198#ifdef OLDADD
paul718e3742002-12-13 20:15:29 +0000199 {RTM_OLDADD, "RTM_OLDADD"},
Greg Troxel9458b812006-09-13 12:13:08 +0000200#endif /* RTM_OLDADD */
201#ifdef RTM_OLDDEL
paul718e3742002-12-13 20:15:29 +0000202 {RTM_OLDDEL, "RTM_OLDDEL"},
Greg Troxel9458b812006-09-13 12:13:08 +0000203#endif /* RTM_OLDDEL */
paul718e3742002-12-13 20:15:29 +0000204 {RTM_RESOLVE, "RTM_RESOLVE"},
205 {RTM_NEWADDR, "RTM_NEWADDR"},
206 {RTM_DELADDR, "RTM_DELADDR"},
207 {RTM_IFINFO, "RTM_IFINFO"},
208#ifdef RTM_OIFINFO
209 {RTM_OIFINFO, "RTM_OIFINFO"},
210#endif /* RTM_OIFINFO */
211#ifdef RTM_NEWMADDR
212 {RTM_NEWMADDR, "RTM_NEWMADDR"},
213#endif /* RTM_NEWMADDR */
214#ifdef RTM_DELMADDR
215 {RTM_DELMADDR, "RTM_DELMADDR"},
216#endif /* RTM_DELMADDR */
217#ifdef RTM_IFANNOUNCE
218 {RTM_IFANNOUNCE, "RTM_IFANNOUNCE"},
219#endif /* RTM_IFANNOUNCE */
220 {0, NULL}
221};
222
Stephen Hemmingerce0db9c2009-05-15 10:47:04 -0700223static const struct message rtm_flag_str[] =
paul718e3742002-12-13 20:15:29 +0000224{
225 {RTF_UP, "UP"},
226 {RTF_GATEWAY, "GATEWAY"},
227 {RTF_HOST, "HOST"},
228 {RTF_REJECT, "REJECT"},
229 {RTF_DYNAMIC, "DYNAMIC"},
230 {RTF_MODIFIED, "MODIFIED"},
231 {RTF_DONE, "DONE"},
232#ifdef RTF_MASK
233 {RTF_MASK, "MASK"},
234#endif /* RTF_MASK */
David Warde6f148e2009-12-03 21:43:11 +0300235#ifdef RTF_CLONING
paul718e3742002-12-13 20:15:29 +0000236 {RTF_CLONING, "CLONING"},
David Warde6f148e2009-12-03 21:43:11 +0300237#endif /* RTF_CLONING */
paul718e3742002-12-13 20:15:29 +0000238 {RTF_XRESOLVE, "XRESOLVE"},
239 {RTF_LLINFO, "LLINFO"},
240 {RTF_STATIC, "STATIC"},
241 {RTF_BLACKHOLE, "BLACKHOLE"},
paul6fe70d12005-11-12 22:55:10 +0000242#ifdef RTF_PRIVATE
243 {RTF_PRIVATE, "PRIVATE"},
244#endif /* RTF_PRIVATE */
paul718e3742002-12-13 20:15:29 +0000245 {RTF_PROTO1, "PROTO1"},
246 {RTF_PROTO2, "PROTO2"},
247#ifdef RTF_PRCLONING
248 {RTF_PRCLONING, "PRCLONING"},
249#endif /* RTF_PRCLONING */
250#ifdef RTF_WASCLONED
251 {RTF_WASCLONED, "WASCLONED"},
252#endif /* RTF_WASCLONED */
253#ifdef RTF_PROTO3
254 {RTF_PROTO3, "PROTO3"},
255#endif /* RTF_PROTO3 */
256#ifdef RTF_PINNED
257 {RTF_PINNED, "PINNED"},
258#endif /* RTF_PINNED */
259#ifdef RTF_LOCAL
260 {RTF_LOCAL, "LOCAL"},
261#endif /* RTF_LOCAL */
262#ifdef RTF_BROADCAST
263 {RTF_BROADCAST, "BROADCAST"},
264#endif /* RTF_BROADCAST */
265#ifdef RTF_MULTICAST
266 {RTF_MULTICAST, "MULTICAST"},
267#endif /* RTF_MULTICAST */
paul6fe70d12005-11-12 22:55:10 +0000268#ifdef RTF_MULTIRT
269 {RTF_MULTIRT, "MULTIRT"},
270#endif /* RTF_MULTIRT */
271#ifdef RTF_SETSRC
272 {RTF_SETSRC, "SETSRC"},
273#endif /* RTF_SETSRC */
paul718e3742002-12-13 20:15:29 +0000274 {0, NULL}
275};
276
277/* Kernel routing update socket. */
278int routing_sock = -1;
279
280/* Yes I'm checking ugly routing socket behavior. */
281/* #define DEBUG */
282
283/* Supported address family check. */
David Lamparter3e9e2c92015-04-10 09:14:58 +0200284static inline int
paul718e3742002-12-13 20:15:29 +0000285af_check (int family)
286{
287 if (family == AF_INET)
288 return 1;
289#ifdef HAVE_IPV6
290 if (family == AF_INET6)
291 return 1;
292#endif /* HAVE_IPV6 */
293 return 0;
294}
David Lamparter6b0655a2014-06-04 06:53:35 +0200295
paul718e3742002-12-13 20:15:29 +0000296/* Dump routing table flag for debug purpose. */
ajsb6178002004-12-07 21:12:56 +0000297static void
paul718e3742002-12-13 20:15:29 +0000298rtm_flag_dump (int flag)
299{
Tom Goff80b2a942009-12-03 14:53:15 +0300300 const struct message *mes;
paul718e3742002-12-13 20:15:29 +0000301 static char buf[BUFSIZ];
302
gdtcced60d2004-07-13 16:45:54 +0000303 buf[0] = '\0';
paul718e3742002-12-13 20:15:29 +0000304 for (mes = rtm_flag_str; mes->key != 0; mes++)
305 {
306 if (mes->key & flag)
307 {
308 strlcat (buf, mes->str, BUFSIZ);
309 strlcat (buf, " ", BUFSIZ);
310 }
311 }
ajsb6178002004-12-07 21:12:56 +0000312 zlog_debug ("Kernel: %s", buf);
paul718e3742002-12-13 20:15:29 +0000313}
314
315#ifdef RTM_IFANNOUNCE
316/* Interface adding function */
paul6621ca82005-11-23 13:02:08 +0000317static int
paul718e3742002-12-13 20:15:29 +0000318ifan_read (struct if_announcemsghdr *ifan)
319{
320 struct interface *ifp;
paul6fe70d12005-11-12 22:55:10 +0000321
paul718e3742002-12-13 20:15:29 +0000322 ifp = if_lookup_by_index (ifan->ifan_index);
paul6fe70d12005-11-12 22:55:10 +0000323
324 if (ifp)
325 assert ( (ifp->ifindex == ifan->ifan_index)
326 || (ifp->ifindex == IFINDEX_INTERNAL) );
327
paulec1a4282005-11-24 15:15:17 +0000328 if ( (ifp == NULL)
329 || ((ifp->ifindex == IFINDEX_INTERNAL)
330 && (ifan->ifan_what == IFAN_ARRIVAL)) )
paul718e3742002-12-13 20:15:29 +0000331 {
paul6fe70d12005-11-12 22:55:10 +0000332 if (IS_ZEBRA_DEBUG_KERNEL)
333 zlog_debug ("%s: creating interface for ifindex %d, name %s",
334 __func__, ifan->ifan_index, ifan->ifan_name);
335
paul718e3742002-12-13 20:15:29 +0000336 /* Create Interface */
ajs08dbfb62005-04-03 03:40:52 +0000337 ifp = if_get_by_name_len(ifan->ifan_name,
338 strnlen(ifan->ifan_name,
339 sizeof(ifan->ifan_name)));
paul718e3742002-12-13 20:15:29 +0000340 ifp->ifindex = ifan->ifan_index;
341
Ingo Flaschberger1db65fa2011-04-17 18:28:20 +0000342 if_get_metric (ifp);
paul718e3742002-12-13 20:15:29 +0000343 if_add_update (ifp);
344 }
345 else if (ifp != NULL && ifan->ifan_what == IFAN_DEPARTURE)
paul6eb88272005-07-29 14:36:00 +0000346 if_delete_update (ifp);
paul718e3742002-12-13 20:15:29 +0000347
348 if_get_flags (ifp);
349 if_get_mtu (ifp);
350 if_get_metric (ifp);
351
352 if (IS_ZEBRA_DEBUG_KERNEL)
paul6fe70d12005-11-12 22:55:10 +0000353 zlog_debug ("%s: interface %s index %d",
354 __func__, ifan->ifan_name, ifan->ifan_index);
paul718e3742002-12-13 20:15:29 +0000355
356 return 0;
357}
358#endif /* RTM_IFANNOUNCE */
359
Doug VanLeuven9234b382012-10-10 16:12:32 -0700360#ifdef HAVE_BSD_IFI_LINK_STATE
Andrew J. Schorrc543a172008-01-10 15:24:32 +0000361/* BSD link detect translation */
362static void
363bsd_linkdetect_translate (struct if_msghdr *ifm)
364{
Andrew J. Schorr55edb0d2008-01-11 15:57:13 +0000365 if ((ifm->ifm_data.ifi_link_state >= LINK_STATE_UP) ||
366 (ifm->ifm_data.ifi_link_state == LINK_STATE_UNKNOWN))
Andrew J. Schorrc543a172008-01-10 15:24:32 +0000367 SET_FLAG(ifm->ifm_flags, IFF_RUNNING);
368 else
369 UNSET_FLAG(ifm->ifm_flags, IFF_RUNNING);
370}
Doug VanLeuven9234b382012-10-10 16:12:32 -0700371#endif /* HAVE_BSD_IFI_LINK_STATE */
Andrew J. Schorrc543a172008-01-10 15:24:32 +0000372
gdtda26e3b2004-01-05 17:20:59 +0000373/*
374 * Handle struct if_msghdr obtained from reading routing socket or
375 * sysctl (from interface_list). There may or may not be sockaddrs
376 * present after the header.
377 */
paulec1a4282005-11-24 15:15:17 +0000378int
paul718e3742002-12-13 20:15:29 +0000379ifm_read (struct if_msghdr *ifm)
380{
paul3e95a072003-09-24 00:05:45 +0000381 struct interface *ifp = NULL;
Tom Goffa34eb362009-11-25 20:36:06 +0000382 struct sockaddr_dl *sdl;
paul6fe70d12005-11-12 22:55:10 +0000383 char ifname[IFNAMSIZ];
384 short ifnlen = 0;
Doug VanLeuvena05df8f2012-10-10 16:11:36 -0700385 caddr_t cp;
paul6fe70d12005-11-12 22:55:10 +0000386
387 /* terminate ifname at head (for strnlen) and tail (for safety) */
388 ifname[IFNAMSIZ - 1] = '\0';
389
gdtda26e3b2004-01-05 17:20:59 +0000390 /* paranoia: sanity check structure */
391 if (ifm->ifm_msglen < sizeof(struct if_msghdr))
392 {
393 zlog_err ("ifm_read: ifm->ifm_msglen %d too short\n",
394 ifm->ifm_msglen);
395 return -1;
396 }
397
398 /*
gdt4bfbea82004-01-06 01:13:05 +0000399 * Check for a sockaddr_dl following the message. First, point to
400 * where a socakddr might be if one follows the message.
gdtda26e3b2004-01-05 17:20:59 +0000401 */
gdt4bfbea82004-01-06 01:13:05 +0000402 cp = (void *)(ifm + 1);
403
paul3e95a072003-09-24 00:05:45 +0000404#ifdef SUNOS_5
paul3e95a072003-09-24 00:05:45 +0000405 /*
gdt4bfbea82004-01-06 01:13:05 +0000406 * XXX This behavior should be narrowed to only the kernel versions
407 * for which the structures returned do not match the headers.
408 *
paul3e95a072003-09-24 00:05:45 +0000409 * if_msghdr_t on 64 bit kernels in Solaris 9 and earlier versions
gdt4bfbea82004-01-06 01:13:05 +0000410 * is 12 bytes larger than the 32 bit version.
paul3e95a072003-09-24 00:05:45 +0000411 */
gdt4bfbea82004-01-06 01:13:05 +0000412 if (((struct sockaddr *) cp)->sa_family == AF_UNSPEC)
paul3e95a072003-09-24 00:05:45 +0000413 cp = cp + 12;
paul3e95a072003-09-24 00:05:45 +0000414#endif
paul718e3742002-12-13 20:15:29 +0000415
paul6fe70d12005-11-12 22:55:10 +0000416 RTA_ADDR_GET (NULL, RTA_DST, ifm->ifm_addrs, cp);
417 RTA_ADDR_GET (NULL, RTA_GATEWAY, ifm->ifm_addrs, cp);
418 RTA_ATTR_GET (NULL, RTA_NETMASK, ifm->ifm_addrs, cp);
419 RTA_ADDR_GET (NULL, RTA_GENMASK, ifm->ifm_addrs, cp);
Tom Goffa34eb362009-11-25 20:36:06 +0000420 sdl = (struct sockaddr_dl *)cp;
paul6fe70d12005-11-12 22:55:10 +0000421 RTA_NAME_GET (ifname, RTA_IFP, ifm->ifm_addrs, cp, ifnlen);
422 RTA_ADDR_GET (NULL, RTA_IFA, ifm->ifm_addrs, cp);
423 RTA_ADDR_GET (NULL, RTA_AUTHOR, ifm->ifm_addrs, cp);
424 RTA_ADDR_GET (NULL, RTA_BRD, ifm->ifm_addrs, cp);
425
426 if (IS_ZEBRA_DEBUG_KERNEL)
427 zlog_debug ("%s: sdl ifname %s", __func__, (ifnlen ? ifname : "(nil)"));
428
gdt4bfbea82004-01-06 01:13:05 +0000429 /*
paul6fe70d12005-11-12 22:55:10 +0000430 * Look up on ifindex first, because ifindices are the primary handle for
431 * interfaces across the user/kernel boundary, for most systems. (Some
432 * messages, such as up/down status changes on NetBSD, do not include a
433 * sockaddr_dl).
gdt4bfbea82004-01-06 01:13:05 +0000434 */
paul6fe70d12005-11-12 22:55:10 +0000435 if ( (ifp = if_lookup_by_index (ifm->ifm_index)) != NULL )
gdt4bfbea82004-01-06 01:13:05 +0000436 {
paul6fe70d12005-11-12 22:55:10 +0000437 /* we have an ifp, verify that the name matches as some systems,
438 * eg Solaris, have a 1:many association of ifindex:ifname
439 * if they dont match, we dont have the correct ifp and should
440 * set it back to NULL to let next check do lookup by name
441 */
442 if (ifnlen && (strncmp (ifp->name, ifname, IFNAMSIZ) != 0) )
gdt4bfbea82004-01-06 01:13:05 +0000443 {
paul6fe70d12005-11-12 22:55:10 +0000444 if (IS_ZEBRA_DEBUG_KERNEL)
445 zlog_debug ("%s: ifp name %s doesnt match sdl name %s",
446 __func__, ifp->name, ifname);
447 ifp = NULL;
gdt4bfbea82004-01-06 01:13:05 +0000448 }
449 }
paul6fe70d12005-11-12 22:55:10 +0000450
gdtda26e3b2004-01-05 17:20:59 +0000451 /*
paul6fe70d12005-11-12 22:55:10 +0000452 * If we dont have an ifp, try looking up by name. Particularly as some
453 * systems (Solaris) have a 1:many mapping of ifindex:ifname - the ifname
454 * is therefore our unique handle to that interface.
455 *
456 * Interfaces specified in the configuration file for which the ifindex
457 * has not been determined will have ifindex == IFINDEX_INTERNAL, and such
458 * interfaces are found by this search, and then their ifindex values can
459 * be filled in.
gdtda26e3b2004-01-05 17:20:59 +0000460 */
paul6fe70d12005-11-12 22:55:10 +0000461 if ( (ifp == NULL) && ifnlen)
462 ifp = if_lookup_by_name (ifname);
paul718e3742002-12-13 20:15:29 +0000463
gdtda26e3b2004-01-05 17:20:59 +0000464 /*
paul6fe70d12005-11-12 22:55:10 +0000465 * If ifp still does not exist or has an invalid index (IFINDEX_INTERNAL),
466 * create or fill in an interface.
gdtda26e3b2004-01-05 17:20:59 +0000467 */
ajsd2fc8892005-04-02 18:38:43 +0000468 if ((ifp == NULL) || (ifp->ifindex == IFINDEX_INTERNAL))
paul718e3742002-12-13 20:15:29 +0000469 {
gdt4bfbea82004-01-06 01:13:05 +0000470 /*
471 * To create or fill in an interface, a sockaddr_dl (via
472 * RTA_IFP) is required.
473 */
paul6fe70d12005-11-12 22:55:10 +0000474 if (!ifnlen)
paul718e3742002-12-13 20:15:29 +0000475 {
paul6fe70d12005-11-12 22:55:10 +0000476 zlog_warn ("Interface index %d (new) missing ifname\n",
paul718e3742002-12-13 20:15:29 +0000477 ifm->ifm_index);
478 return -1;
479 }
paul5c78b3d2006-01-25 04:31:40 +0000480
481#ifndef RTM_IFANNOUNCE
482 /* Down->Down interface should be ignored here.
483 * See further comment below.
484 */
485 if (!CHECK_FLAG (ifm->ifm_flags, IFF_UP))
486 return 0;
487#endif /* !RTM_IFANNOUNCE */
paul6fe70d12005-11-12 22:55:10 +0000488
paul3e95a072003-09-24 00:05:45 +0000489 if (ifp == NULL)
paul6fe70d12005-11-12 22:55:10 +0000490 {
491 /* Interface that zebra was not previously aware of, so create. */
492 ifp = if_create (ifname, ifnlen);
493 if (IS_ZEBRA_DEBUG_KERNEL)
494 zlog_debug ("%s: creating ifp for ifindex %d",
495 __func__, ifm->ifm_index);
496 }
paul718e3742002-12-13 20:15:29 +0000497
paul6fe70d12005-11-12 22:55:10 +0000498 if (IS_ZEBRA_DEBUG_KERNEL)
499 zlog_debug ("%s: updated/created ifp, ifname %s, ifindex %d",
500 __func__, ifp->name, ifp->ifindex);
gdt4bfbea82004-01-06 01:13:05 +0000501 /*
502 * Fill in newly created interface structure, or larval
ajsd2fc8892005-04-02 18:38:43 +0000503 * structure with ifindex IFINDEX_INTERNAL.
gdt4bfbea82004-01-06 01:13:05 +0000504 */
paul718e3742002-12-13 20:15:29 +0000505 ifp->ifindex = ifm->ifm_index;
Andrew J. Schorrc543a172008-01-10 15:24:32 +0000506
Doug VanLeuven9234b382012-10-10 16:12:32 -0700507#ifdef HAVE_BSD_IFI_LINK_STATE /* translate BSD kernel msg for link-state */
Andrew J. Schorrc543a172008-01-10 15:24:32 +0000508 bsd_linkdetect_translate(ifm);
Doug VanLeuven9234b382012-10-10 16:12:32 -0700509#endif /* HAVE_BSD_IFI_LINK_STATE */
Andrew J. Schorrc543a172008-01-10 15:24:32 +0000510
paul5c78b3d2006-01-25 04:31:40 +0000511 if_flags_update (ifp, ifm->ifm_flags);
paul718e3742002-12-13 20:15:29 +0000512#if defined(__bsdi__)
513 if_kvm_get_mtu (ifp);
514#else
515 if_get_mtu (ifp);
516#endif /* __bsdi__ */
517 if_get_metric (ifp);
518
Tom Goffa34eb362009-11-25 20:36:06 +0000519 /*
520 * XXX sockaddr_dl contents can be larger than the structure
David Lamparterca3ccd82012-09-26 14:52:39 +0200521 * definition. There are 2 big families here:
522 * - BSD has sdl_len + sdl_data[16] + overruns sdl_data
523 * we MUST use sdl_len here or we'll truncate data.
524 * - Solaris has no sdl_len, but sdl_data[244]
525 * presumably, it's not going to run past that, so sizeof()
526 * is fine here.
Tom Goffa34eb362009-11-25 20:36:06 +0000527 * a nonzero ifnlen from RTA_NAME_GET() means sdl is valid
528 */
529 if (ifnlen)
David Lamparterca3ccd82012-09-26 14:52:39 +0200530 {
531#ifdef HAVE_STRUCT_SOCKADDR_DL_SDL_LEN
532 memcpy (&ifp->sdl, sdl, sdl->sdl_len);
533#else
Tom Goffa34eb362009-11-25 20:36:06 +0000534 memcpy (&ifp->sdl, sdl, sizeof (struct sockaddr_dl));
David Lamparterca3ccd82012-09-26 14:52:39 +0200535#endif /* HAVE_STRUCT_SOCKADDR_DL_SDL_LEN */
536 }
Tom Goffa34eb362009-11-25 20:36:06 +0000537
paul718e3742002-12-13 20:15:29 +0000538 if_add_update (ifp);
539 }
540 else
gdtda26e3b2004-01-05 17:20:59 +0000541 /*
542 * Interface structure exists. Adjust stored flags from
543 * notification. If interface has up->down or down->up
544 * transition, call state change routines (to adjust routes,
545 * notify routing daemons, etc.). (Other flag changes are stored
546 * but apparently do not trigger action.)
547 */
paul718e3742002-12-13 20:15:29 +0000548 {
paul6fe70d12005-11-12 22:55:10 +0000549 if (ifp->ifindex != ifm->ifm_index)
550 {
551 zlog_warn ("%s: index mismatch, ifname %s, ifp index %d, "
552 "ifm index %d",
553 __func__, ifp->name, ifp->ifindex, ifm->ifm_index);
554 return -1;
555 }
556
Doug VanLeuven9234b382012-10-10 16:12:32 -0700557#ifdef HAVE_BSD_IFI_LINK_STATE /* translate BSD kernel msg for link-state */
Andrew J. Schorrc543a172008-01-10 15:24:32 +0000558 bsd_linkdetect_translate(ifm);
Doug VanLeuven9234b382012-10-10 16:12:32 -0700559#endif /* HAVE_BSD_IFI_LINK_STATE */
Andrew J. Schorrc543a172008-01-10 15:24:32 +0000560
paul5c78b3d2006-01-25 04:31:40 +0000561 /* update flags and handle operative->inoperative transition, if any */
562 if_flags_update (ifp, ifm->ifm_flags);
563
paul6eb88272005-07-29 14:36:00 +0000564#ifndef RTM_IFANNOUNCE
paul5c78b3d2006-01-25 04:31:40 +0000565 if (!if_is_up (ifp))
566 {
567 /* No RTM_IFANNOUNCE on this platform, so we can never
568 * distinguish between ~IFF_UP and delete. We must presume
569 * it has been deleted.
570 * Eg, Solaris will not notify us of unplumb.
571 *
572 * XXX: Fixme - this should be runtime detected
573 * So that a binary compiled on a system with IFANNOUNCE
574 * will still behave correctly if run on a platform without
575 */
576 if_delete_update (ifp);
577 }
paul6eb88272005-07-29 14:36:00 +0000578#endif /* RTM_IFANNOUNCE */
Denis Ovsienko1ba27562007-08-21 16:15:39 +0000579 if (if_is_up (ifp))
580 {
581#if defined(__bsdi__)
582 if_kvm_get_mtu (ifp);
583#else
584 if_get_mtu (ifp);
585#endif /* __bsdi__ */
586 if_get_metric (ifp);
587 }
paul718e3742002-12-13 20:15:29 +0000588 }
paul5c78b3d2006-01-25 04:31:40 +0000589
paul718e3742002-12-13 20:15:29 +0000590#ifdef HAVE_NET_RT_IFLIST
591 ifp->stats = ifm->ifm_data;
592#endif /* HAVE_NET_RT_IFLIST */
593
594 if (IS_ZEBRA_DEBUG_KERNEL)
paul6fe70d12005-11-12 22:55:10 +0000595 zlog_debug ("%s: interface %s index %d",
596 __func__, ifp->name, ifp->ifindex);
paul718e3742002-12-13 20:15:29 +0000597
598 return 0;
599}
David Lamparter6b0655a2014-06-04 06:53:35 +0200600
paul718e3742002-12-13 20:15:29 +0000601/* Address read from struct ifa_msghdr. */
paul6621ca82005-11-23 13:02:08 +0000602static void
paul718e3742002-12-13 20:15:29 +0000603ifam_read_mesg (struct ifa_msghdr *ifm,
604 union sockunion *addr,
605 union sockunion *mask,
paul6fe70d12005-11-12 22:55:10 +0000606 union sockunion *brd,
607 char *ifname,
608 short *ifnlen)
paul718e3742002-12-13 20:15:29 +0000609{
610 caddr_t pnt, end;
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000611 union sockunion dst;
612 union sockunion gateway;
paul718e3742002-12-13 20:15:29 +0000613
614 pnt = (caddr_t)(ifm + 1);
615 end = ((caddr_t)ifm) + ifm->ifam_msglen;
616
paul718e3742002-12-13 20:15:29 +0000617 /* Be sure structure is cleared */
618 memset (mask, 0, sizeof (union sockunion));
619 memset (addr, 0, sizeof (union sockunion));
paul6621ca82005-11-23 13:02:08 +0000620 memset (brd, 0, sizeof (union sockunion));
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000621 memset (&dst, 0, sizeof (union sockunion));
622 memset (&gateway, 0, sizeof (union sockunion));
paul718e3742002-12-13 20:15:29 +0000623
624 /* We fetch each socket variable into sockunion. */
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000625 RTA_ADDR_GET (&dst, RTA_DST, ifm->ifam_addrs, pnt);
626 RTA_ADDR_GET (&gateway, RTA_GATEWAY, ifm->ifam_addrs, pnt);
paul62debbb2005-06-14 14:07:07 +0000627 RTA_ATTR_GET (mask, RTA_NETMASK, ifm->ifam_addrs, pnt);
628 RTA_ADDR_GET (NULL, RTA_GENMASK, ifm->ifam_addrs, pnt);
paul6fe70d12005-11-12 22:55:10 +0000629 RTA_NAME_GET (ifname, RTA_IFP, ifm->ifam_addrs, pnt, *ifnlen);
paul62debbb2005-06-14 14:07:07 +0000630 RTA_ADDR_GET (addr, RTA_IFA, ifm->ifam_addrs, pnt);
631 RTA_ADDR_GET (NULL, RTA_AUTHOR, ifm->ifam_addrs, pnt);
paul6fe70d12005-11-12 22:55:10 +0000632 RTA_ADDR_GET (brd, RTA_BRD, ifm->ifam_addrs, pnt);
paul718e3742002-12-13 20:15:29 +0000633
paul6fe70d12005-11-12 22:55:10 +0000634 if (IS_ZEBRA_DEBUG_KERNEL)
Andrew J. Schorr55196042006-05-17 15:04:59 +0000635 {
Timo Teräsbe6335d2015-05-23 11:08:41 +0300636 int family = sockunion_family(addr);
637 switch (family)
Andrew J. Schorr55196042006-05-17 15:04:59 +0000638 {
639 case AF_INET:
Timo Teräsbe6335d2015-05-23 11:08:41 +0300640#ifdef HAVE_IPV6
641 case AF_INET6:
642#endif
Andrew J. Schorr55196042006-05-17 15:04:59 +0000643 {
Timo Teräsbe6335d2015-05-23 11:08:41 +0300644 char buf[4][INET6_ADDRSTRLEN];
Andrew J. Schorr55196042006-05-17 15:04:59 +0000645 zlog_debug ("%s: ifindex %d, ifname %s, ifam_addrs 0x%x, "
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000646 "ifam_flags 0x%x, addr %s/%d broad %s dst %s "
647 "gateway %s",
648 __func__, ifm->ifam_index,
Andrew J. Schorr55196042006-05-17 15:04:59 +0000649 (ifnlen ? ifname : "(nil)"), ifm->ifam_addrs,
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000650 ifm->ifam_flags,
Timo Teräsbe6335d2015-05-23 11:08:41 +0300651 inet_ntop(family,&addr->sin.sin_addr,
Andrew J. Schorr55196042006-05-17 15:04:59 +0000652 buf[0],sizeof(buf[0])),
653 ip_masklen(mask->sin.sin_addr),
Timo Teräsbe6335d2015-05-23 11:08:41 +0300654 inet_ntop(family,&brd->sin.sin_addr,
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000655 buf[1],sizeof(buf[1])),
Timo Teräsbe6335d2015-05-23 11:08:41 +0300656 inet_ntop(family,&dst.sin.sin_addr,
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000657 buf[2],sizeof(buf[2])),
Timo Teräsbe6335d2015-05-23 11:08:41 +0300658 inet_ntop(family,&gateway.sin.sin_addr,
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000659 buf[3],sizeof(buf[3])));
Andrew J. Schorr55196042006-05-17 15:04:59 +0000660 }
661 break;
Andrew J. Schorr55196042006-05-17 15:04:59 +0000662 default:
663 zlog_debug ("%s: ifindex %d, ifname %s, ifam_addrs 0x%x",
664 __func__, ifm->ifam_index,
665 (ifnlen ? ifname : "(nil)"), ifm->ifam_addrs);
666 break;
667 }
668 }
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000669
paul718e3742002-12-13 20:15:29 +0000670 /* Assert read up end point matches to end point */
671 if (pnt != end)
Denis Ovsienko85a2ebf2011-12-05 19:36:06 +0400672 zlog_warn ("ifam_read() doesn't read all socket data");
paul718e3742002-12-13 20:15:29 +0000673}
674
675/* Interface's address information get. */
paulec1a4282005-11-24 15:15:17 +0000676int
paul718e3742002-12-13 20:15:29 +0000677ifam_read (struct ifa_msghdr *ifam)
678{
paul6fe70d12005-11-12 22:55:10 +0000679 struct interface *ifp = NULL;
paul0752ef02005-11-03 12:35:21 +0000680 union sockunion addr, mask, brd;
paul6fe70d12005-11-12 22:55:10 +0000681 char ifname[INTERFACE_NAMSIZ];
682 short ifnlen = 0;
683 char isalias = 0;
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000684 int flags = 0;
paul6fe70d12005-11-12 22:55:10 +0000685
686 ifname[0] = ifname[INTERFACE_NAMSIZ - 1] = '\0';
687
688 /* Allocate and read address information. */
689 ifam_read_mesg (ifam, &addr, &mask, &brd, ifname, &ifnlen);
690
691 if ((ifp = if_lookup_by_index(ifam->ifam_index)) == NULL)
paul718e3742002-12-13 20:15:29 +0000692 {
paul6fe70d12005-11-12 22:55:10 +0000693 zlog_warn ("%s: no interface for ifname %s, index %d",
694 __func__, ifname, ifam->ifam_index);
paul718e3742002-12-13 20:15:29 +0000695 return -1;
696 }
paul6fe70d12005-11-12 22:55:10 +0000697
698 if (ifnlen && strncmp (ifp->name, ifname, INTERFACE_NAMSIZ))
699 isalias = 1;
700
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000701 /* N.B. The info in ifa_msghdr does not tell us whether the RTA_BRD
702 field contains a broadcast address or a peer address, so we are forced to
703 rely upon the interface type. */
704 if (if_is_pointopoint(ifp))
705 SET_FLAG(flags, ZEBRA_IFA_PEER);
706
Paul Jakma65022082007-03-06 13:43:05 +0000707#if 0
708 /* it might seem cute to grab the interface metric here, however
709 * we're processing an address update message, and so some systems
710 * (e.g. FBSD) dont bother to fill in ifam_metric. Disabled, but left
711 * in deliberately, as comment.
712 */
pauld34b8992006-01-17 18:03:04 +0000713 ifp->metric = ifam->ifam_metric;
Paul Jakma65022082007-03-06 13:43:05 +0000714#endif
715
paul718e3742002-12-13 20:15:29 +0000716 /* Add connected address. */
717 switch (sockunion_family (&addr))
718 {
719 case AF_INET:
720 if (ifam->ifam_type == RTM_NEWADDR)
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000721 connected_add_ipv4 (ifp, flags, &addr.sin.sin_addr,
paul718e3742002-12-13 20:15:29 +0000722 ip_masklen (mask.sin.sin_addr),
pauld34b8992006-01-17 18:03:04 +0000723 &brd.sin.sin_addr,
724 (isalias ? ifname : NULL));
paul718e3742002-12-13 20:15:29 +0000725 else
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000726 connected_delete_ipv4 (ifp, flags, &addr.sin.sin_addr,
paul718e3742002-12-13 20:15:29 +0000727 ip_masklen (mask.sin.sin_addr),
paul0752ef02005-11-03 12:35:21 +0000728 &brd.sin.sin_addr);
paul718e3742002-12-13 20:15:29 +0000729 break;
730#ifdef HAVE_IPV6
731 case AF_INET6:
732 /* Unset interface index from link-local address when IPv6 stack
733 is KAME. */
734 if (IN6_IS_ADDR_LINKLOCAL (&addr.sin6.sin6_addr))
735 SET_IN6_LINKLOCAL_IFINDEX (addr.sin6.sin6_addr, 0);
736
737 if (ifam->ifam_type == RTM_NEWADDR)
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000738 connected_add_ipv6 (ifp, flags, &addr.sin6.sin6_addr,
paul718e3742002-12-13 20:15:29 +0000739 ip6_masklen (mask.sin6.sin6_addr),
pauld34b8992006-01-17 18:03:04 +0000740 &brd.sin6.sin6_addr,
741 (isalias ? ifname : NULL));
paul718e3742002-12-13 20:15:29 +0000742 else
743 connected_delete_ipv6 (ifp,
744 &addr.sin6.sin6_addr,
745 ip6_masklen (mask.sin6.sin6_addr),
paul0752ef02005-11-03 12:35:21 +0000746 &brd.sin6.sin6_addr);
paul718e3742002-12-13 20:15:29 +0000747 break;
748#endif /* HAVE_IPV6 */
749 default:
750 /* Unsupported family silently ignore... */
751 break;
752 }
paul5c78b3d2006-01-25 04:31:40 +0000753
754 /* Check interface flag for implicit up of the interface. */
755 if_refresh (ifp);
756
757#ifdef SUNOS_5
758 /* In addition to lacking IFANNOUNCE, on SUNOS IFF_UP is strange.
759 * See comments for SUNOS_5 in interface.c::if_flags_mangle.
760 *
761 * Here we take care of case where the real IFF_UP was previously
762 * unset (as kept in struct zebra_if.primary_state) and the mangled
763 * IFF_UP (ie IFF_UP set || listcount(connected) has now transitioned
764 * to unset due to the lost non-primary address having DELADDR'd.
765 *
766 * we must delete the interface, because in between here and next
767 * event for this interface-name the administrator could unplumb
768 * and replumb the interface.
769 */
770 if (!if_is_up (ifp))
771 if_delete_update (ifp);
772#endif /* SUNOS_5 */
773
paul718e3742002-12-13 20:15:29 +0000774 return 0;
775}
David Lamparter6b0655a2014-06-04 06:53:35 +0200776
paul718e3742002-12-13 20:15:29 +0000777/* Interface function for reading kernel routing table information. */
paul6621ca82005-11-23 13:02:08 +0000778static int
paul718e3742002-12-13 20:15:29 +0000779rtm_read_mesg (struct rt_msghdr *rtm,
780 union sockunion *dest,
781 union sockunion *mask,
paul6fe70d12005-11-12 22:55:10 +0000782 union sockunion *gate,
783 char *ifname,
784 short *ifnlen)
paul718e3742002-12-13 20:15:29 +0000785{
786 caddr_t pnt, end;
787
788 /* Pnt points out socket data start point. */
789 pnt = (caddr_t)(rtm + 1);
790 end = ((caddr_t)rtm) + rtm->rtm_msglen;
791
792 /* rt_msghdr version check. */
793 if (rtm->rtm_version != RTM_VERSION)
794 zlog (NULL, LOG_WARNING,
795 "Routing message version different %d should be %d."
796 "This may cause problem\n", rtm->rtm_version, RTM_VERSION);
paul62debbb2005-06-14 14:07:07 +0000797
paul718e3742002-12-13 20:15:29 +0000798 /* Be sure structure is cleared */
799 memset (dest, 0, sizeof (union sockunion));
800 memset (gate, 0, sizeof (union sockunion));
801 memset (mask, 0, sizeof (union sockunion));
802
803 /* We fetch each socket variable into sockunion. */
paul62debbb2005-06-14 14:07:07 +0000804 RTA_ADDR_GET (dest, RTA_DST, rtm->rtm_addrs, pnt);
805 RTA_ADDR_GET (gate, RTA_GATEWAY, rtm->rtm_addrs, pnt);
806 RTA_ATTR_GET (mask, RTA_NETMASK, rtm->rtm_addrs, pnt);
807 RTA_ADDR_GET (NULL, RTA_GENMASK, rtm->rtm_addrs, pnt);
paul6fe70d12005-11-12 22:55:10 +0000808 RTA_NAME_GET (ifname, RTA_IFP, rtm->rtm_addrs, pnt, *ifnlen);
paul62debbb2005-06-14 14:07:07 +0000809 RTA_ADDR_GET (NULL, RTA_IFA, rtm->rtm_addrs, pnt);
810 RTA_ADDR_GET (NULL, RTA_AUTHOR, rtm->rtm_addrs, pnt);
811 RTA_ADDR_GET (NULL, RTA_BRD, rtm->rtm_addrs, pnt);
paul718e3742002-12-13 20:15:29 +0000812
813 /* If there is netmask information set it's family same as
814 destination family*/
815 if (rtm->rtm_addrs & RTA_NETMASK)
816 mask->sa.sa_family = dest->sa.sa_family;
817
818 /* Assert read up to the end of pointer. */
819 if (pnt != end)
Denis Ovsienko85a2ebf2011-12-05 19:36:06 +0400820 zlog (NULL, LOG_WARNING, "rtm_read() doesn't read all socket data.");
paul718e3742002-12-13 20:15:29 +0000821
822 return rtm->rtm_flags;
823}
824
paulec1a4282005-11-24 15:15:17 +0000825void
paul718e3742002-12-13 20:15:29 +0000826rtm_read (struct rt_msghdr *rtm)
827{
828 int flags;
829 u_char zebra_flags;
830 union sockunion dest, mask, gate;
paul6fe70d12005-11-12 22:55:10 +0000831 char ifname[INTERFACE_NAMSIZ + 1];
832 short ifnlen = 0;
paul718e3742002-12-13 20:15:29 +0000833
834 zebra_flags = 0;
835
paul718e3742002-12-13 20:15:29 +0000836 /* Read destination and netmask and gateway from rtm message
837 structure. */
paul6fe70d12005-11-12 22:55:10 +0000838 flags = rtm_read_mesg (rtm, &dest, &mask, &gate, ifname, &ifnlen);
Denis Ovsienko6da59802007-08-17 14:16:30 +0000839 if (!(flags & RTF_DONE))
840 return;
Denis Ovsienkodc958242007-08-13 16:03:06 +0000841 if (IS_ZEBRA_DEBUG_KERNEL)
842 zlog_debug ("%s: got rtm of type %d (%s)", __func__, rtm->rtm_type,
Denis Ovsienko2d844522007-09-14 11:31:55 +0000843 lookup (rtm_type_str, rtm->rtm_type));
paul718e3742002-12-13 20:15:29 +0000844
845#ifdef RTF_CLONED /*bsdi, netbsd 1.6*/
846 if (flags & RTF_CLONED)
847 return;
848#endif
849#ifdef RTF_WASCLONED /*freebsd*/
850 if (flags & RTF_WASCLONED)
851 return;
852#endif
853
854 if ((rtm->rtm_type == RTM_ADD) && ! (flags & RTF_UP))
855 return;
856
857 /* This is connected route. */
858 if (! (flags & RTF_GATEWAY))
859 return;
860
861 if (flags & RTF_PROTO1)
862 SET_FLAG (zebra_flags, ZEBRA_FLAG_SELFROUTE);
863
864 /* This is persistent route. */
865 if (flags & RTF_STATIC)
866 SET_FLAG (zebra_flags, ZEBRA_FLAG_STATIC);
867
hasso81dfcaa2003-05-25 19:21:25 +0000868 /* This is a reject or blackhole route */
869 if (flags & RTF_REJECT)
870 SET_FLAG (zebra_flags, ZEBRA_FLAG_REJECT);
871 if (flags & RTF_BLACKHOLE)
872 SET_FLAG (zebra_flags, ZEBRA_FLAG_BLACKHOLE);
873
paul718e3742002-12-13 20:15:29 +0000874 if (dest.sa.sa_family == AF_INET)
875 {
876 struct prefix_ipv4 p;
877
878 p.family = AF_INET;
879 p.prefix = dest.sin.sin_addr;
880 if (flags & RTF_HOST)
881 p.prefixlen = IPV4_MAX_PREFIXLEN;
882 else
883 p.prefixlen = ip_masklen (mask.sin.sin_addr);
paulca162182005-09-12 16:58:52 +0000884
Denis Ovsienkodc958242007-08-13 16:03:06 +0000885 /* Catch self originated messages and match them against our current RIB.
886 * At the same time, ignore unconfirmed messages, they should be tracked
887 * by rtm_write() and kernel_rtm_ipv4().
888 */
Denis Ovsienko96934e62007-09-14 14:56:28 +0000889 if (rtm->rtm_type != RTM_GET && rtm->rtm_pid == pid)
Denis Ovsienkodc958242007-08-13 16:03:06 +0000890 {
Timo Teräsbe6335d2015-05-23 11:08:41 +0300891 char buf[PREFIX_STRLEN], gate_buf[INET_ADDRSTRLEN];
Denis Ovsienkodc958242007-08-13 16:03:06 +0000892 int ret;
Denis Ovsienkodc958242007-08-13 16:03:06 +0000893 if (! IS_ZEBRA_DEBUG_RIB)
894 return;
Feng Lu0d0686f2015-05-22 11:40:02 +0200895 ret = rib_lookup_ipv4_route (&p, &gate, VRF_DEFAULT);
Timo Teräsbe6335d2015-05-23 11:08:41 +0300896 prefix2str (&p, buf, sizeof(buf));
Denis Ovsienkodc958242007-08-13 16:03:06 +0000897 switch (rtm->rtm_type)
898 {
899 case RTM_ADD:
900 case RTM_GET:
901 case RTM_CHANGE:
902 /* The kernel notifies us about a new route in FIB created by us.
903 Do we have a correspondent entry in our RIB? */
904 switch (ret)
905 {
906 case ZEBRA_RIB_NOTFOUND:
Timo Teräsbe6335d2015-05-23 11:08:41 +0300907 zlog_debug ("%s: %s %s: desync: RR isn't yet in RIB, while already in FIB",
908 __func__, lookup (rtm_type_str, rtm->rtm_type), buf);
Denis Ovsienkodc958242007-08-13 16:03:06 +0000909 break;
910 case ZEBRA_RIB_FOUND_CONNECTED:
911 case ZEBRA_RIB_FOUND_NOGATE:
912 inet_ntop (AF_INET, &gate.sin.sin_addr, gate_buf, INET_ADDRSTRLEN);
Timo Teräsbe6335d2015-05-23 11:08:41 +0300913 zlog_debug ("%s: %s %s: desync: RR is in RIB, but gate differs (ours is %s)",
914 __func__, lookup (rtm_type_str, rtm->rtm_type), buf, gate_buf);
Denis Ovsienkodc958242007-08-13 16:03:06 +0000915 break;
916 case ZEBRA_RIB_FOUND_EXACT: /* RIB RR == FIB RR */
Timo Teräsbe6335d2015-05-23 11:08:41 +0300917 zlog_debug ("%s: %s %s: done Ok",
918 __func__, lookup (rtm_type_str, rtm->rtm_type), buf);
Denis Ovsienkodc958242007-08-13 16:03:06 +0000919 rib_lookup_and_dump (&p);
920 return;
921 break;
922 }
923 break;
924 case RTM_DELETE:
925 /* The kernel notifies us about a route deleted by us. Do we still
926 have it in the RIB? Do we have anything instead? */
927 switch (ret)
928 {
929 case ZEBRA_RIB_FOUND_EXACT:
Timo Teräsbe6335d2015-05-23 11:08:41 +0300930 zlog_debug ("%s: %s %s: desync: RR is still in RIB, while already not in FIB",
931 __func__, lookup (rtm_type_str, rtm->rtm_type), buf);
Denis Ovsienkodc958242007-08-13 16:03:06 +0000932 rib_lookup_and_dump (&p);
933 break;
934 case ZEBRA_RIB_FOUND_CONNECTED:
935 case ZEBRA_RIB_FOUND_NOGATE:
Timo Teräsbe6335d2015-05-23 11:08:41 +0300936 zlog_debug ("%s: %s %s: desync: RR is still in RIB, plus gate differs",
937 __func__, lookup (rtm_type_str, rtm->rtm_type), buf);
Denis Ovsienkodc958242007-08-13 16:03:06 +0000938 rib_lookup_and_dump (&p);
939 break;
940 case ZEBRA_RIB_NOTFOUND: /* RIB RR == FIB RR */
Timo Teräsbe6335d2015-05-23 11:08:41 +0300941 zlog_debug ("%s: %s %s: done Ok",
942 __func__, lookup (rtm_type_str, rtm->rtm_type), buf);
Denis Ovsienkodc958242007-08-13 16:03:06 +0000943 rib_lookup_and_dump (&p);
944 return;
945 break;
946 }
947 break;
948 default:
Timo Teräsbe6335d2015-05-23 11:08:41 +0300949 zlog_debug ("%s: %s: warning: loopback RTM of type %s received",
950 __func__, buf, lookup (rtm_type_str, rtm->rtm_type));
Denis Ovsienkodc958242007-08-13 16:03:06 +0000951 }
952 return;
953 }
954
paulca162182005-09-12 16:58:52 +0000955 /* Change, delete the old prefix, we have no further information
956 * to specify the route really
957 */
958 if (rtm->rtm_type == RTM_CHANGE)
959 rib_delete_ipv4 (ZEBRA_ROUTE_KERNEL, zebra_flags, &p,
Feng Lu0d0686f2015-05-22 11:40:02 +0200960 NULL, 0, VRF_DEFAULT, SAFI_UNICAST);
paulca162182005-09-12 16:58:52 +0000961
962 if (rtm->rtm_type == RTM_GET
963 || rtm->rtm_type == RTM_ADD
964 || rtm->rtm_type == RTM_CHANGE)
Feng Lu0d0686f2015-05-22 11:40:02 +0200965 rib_add_ipv4 (ZEBRA_ROUTE_KERNEL, zebra_flags, &p, &gate.sin.sin_addr,
David Lamparterd6cf5132015-06-02 08:31:38 +0200966 NULL, 0, VRF_DEFAULT, 0, 0, 0, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +0000967 else
Feng Lu0d0686f2015-05-22 11:40:02 +0200968 rib_delete_ipv4 (ZEBRA_ROUTE_KERNEL, zebra_flags, &p,
969 &gate.sin.sin_addr, 0, VRF_DEFAULT, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +0000970 }
971#ifdef HAVE_IPV6
972 if (dest.sa.sa_family == AF_INET6)
973 {
Denis Ovsienko5619f562007-10-24 13:13:21 +0000974 /* One day we might have a debug section here like one in the
975 * IPv4 case above. Just ignore own messages at the moment.
976 */
977 if (rtm->rtm_type != RTM_GET && rtm->rtm_pid == pid)
978 return;
paul718e3742002-12-13 20:15:29 +0000979 struct prefix_ipv6 p;
980 unsigned int ifindex = 0;
981
982 p.family = AF_INET6;
983 p.prefix = dest.sin6.sin6_addr;
984 if (flags & RTF_HOST)
985 p.prefixlen = IPV6_MAX_PREFIXLEN;
986 else
987 p.prefixlen = ip6_masklen (mask.sin6.sin6_addr);
988
989#ifdef KAME
990 if (IN6_IS_ADDR_LINKLOCAL (&gate.sin6.sin6_addr))
991 {
992 ifindex = IN6_LINKLOCAL_IFINDEX (gate.sin6.sin6_addr);
993 SET_IN6_LINKLOCAL_IFINDEX (gate.sin6.sin6_addr, 0);
994 }
995#endif /* KAME */
996
paulca162182005-09-12 16:58:52 +0000997 /* CHANGE: delete the old prefix, we have no further information
998 * to specify the route really
999 */
1000 if (rtm->rtm_type == RTM_CHANGE)
paul6621ca82005-11-23 13:02:08 +00001001 rib_delete_ipv6 (ZEBRA_ROUTE_KERNEL, zebra_flags, &p,
Feng Lu0d0686f2015-05-22 11:40:02 +02001002 NULL, 0, VRF_DEFAULT, SAFI_UNICAST);
paulca162182005-09-12 16:58:52 +00001003
1004 if (rtm->rtm_type == RTM_GET
1005 || rtm->rtm_type == RTM_ADD
1006 || rtm->rtm_type == RTM_CHANGE)
Feng Lu0d0686f2015-05-22 11:40:02 +02001007 rib_add_ipv6 (ZEBRA_ROUTE_KERNEL, zebra_flags, &p, &gate.sin6.sin6_addr,
1008 ifindex, VRF_DEFAULT, RT_TABLE_MAIN, 0, 0, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +00001009 else
Feng Lu0d0686f2015-05-22 11:40:02 +02001010 rib_delete_ipv6 (ZEBRA_ROUTE_KERNEL, zebra_flags, &p,
1011 &gate.sin6.sin6_addr, ifindex,
1012 VRF_DEFAULT, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +00001013 }
1014#endif /* HAVE_IPV6 */
1015}
1016
1017/* Interface function for the kernel routing table updates. Support
paul6621ca82005-11-23 13:02:08 +00001018 * for RTM_CHANGE will be needed.
1019 * Exported only for rt_socket.c
1020 */
paul718e3742002-12-13 20:15:29 +00001021int
1022rtm_write (int message,
1023 union sockunion *dest,
1024 union sockunion *mask,
1025 union sockunion *gate,
1026 unsigned int index,
1027 int zebra_flags,
1028 int metric)
1029{
1030 int ret;
1031 caddr_t pnt;
1032 struct interface *ifp;
paul718e3742002-12-13 20:15:29 +00001033
1034 /* Sequencial number of routing message. */
1035 static int msg_seq = 0;
1036
1037 /* Struct of rt_msghdr and buffer for storing socket's data. */
1038 struct
1039 {
1040 struct rt_msghdr rtm;
1041 char buf[512];
1042 } msg;
1043
paul718e3742002-12-13 20:15:29 +00001044 if (routing_sock < 0)
1045 return ZEBRA_ERR_EPERM;
1046
1047 /* Clear and set rt_msghdr values */
1048 memset (&msg, 0, sizeof (struct rt_msghdr));
1049 msg.rtm.rtm_version = RTM_VERSION;
1050 msg.rtm.rtm_type = message;
1051 msg.rtm.rtm_seq = msg_seq++;
1052 msg.rtm.rtm_addrs = RTA_DST;
1053 msg.rtm.rtm_addrs |= RTA_GATEWAY;
1054 msg.rtm.rtm_flags = RTF_UP;
1055 msg.rtm.rtm_index = index;
1056
1057 if (metric != 0)
1058 {
1059 msg.rtm.rtm_rmx.rmx_hopcount = metric;
1060 msg.rtm.rtm_inits |= RTV_HOPCOUNT;
1061 }
1062
1063 ifp = if_lookup_by_index (index);
1064
1065 if (gate && message == RTM_ADD)
1066 msg.rtm.rtm_flags |= RTF_GATEWAY;
1067
David Warde6f148e2009-12-03 21:43:11 +03001068 /* When RTF_CLONING is unavailable on BSD, should we set some
1069 * other flag instead?
1070 */
1071#ifdef RTF_CLONING
paul718e3742002-12-13 20:15:29 +00001072 if (! gate && message == RTM_ADD && ifp &&
1073 (ifp->flags & IFF_POINTOPOINT) == 0)
1074 msg.rtm.rtm_flags |= RTF_CLONING;
David Warde6f148e2009-12-03 21:43:11 +03001075#endif /* RTF_CLONING */
paul718e3742002-12-13 20:15:29 +00001076
1077 /* If no protocol specific gateway is specified, use link
1078 address for gateway. */
1079 if (! gate)
1080 {
1081 if (!ifp)
1082 {
Denis Ovsienkodc958242007-08-13 16:03:06 +00001083 char dest_buf[INET_ADDRSTRLEN] = "NULL", mask_buf[INET_ADDRSTRLEN] = "255.255.255.255";
1084 if (dest)
1085 inet_ntop (AF_INET, &dest->sin.sin_addr, dest_buf, INET_ADDRSTRLEN);
1086 if (mask)
1087 inet_ntop (AF_INET, &mask->sin.sin_addr, mask_buf, INET_ADDRSTRLEN);
1088 zlog_warn ("%s: %s/%s: gate == NULL and no gateway found for ifindex %d",
1089 __func__, dest_buf, mask_buf, index);
paul718e3742002-12-13 20:15:29 +00001090 return -1;
1091 }
1092 gate = (union sockunion *) & ifp->sdl;
1093 }
1094
1095 if (mask)
1096 msg.rtm.rtm_addrs |= RTA_NETMASK;
1097 else if (message == RTM_ADD)
1098 msg.rtm.rtm_flags |= RTF_HOST;
1099
1100 /* Tagging route with flags */
1101 msg.rtm.rtm_flags |= (RTF_PROTO1);
1102
1103 /* Additional flags. */
1104 if (zebra_flags & ZEBRA_FLAG_BLACKHOLE)
1105 msg.rtm.rtm_flags |= RTF_BLACKHOLE;
hasso81dfcaa2003-05-25 19:21:25 +00001106 if (zebra_flags & ZEBRA_FLAG_REJECT)
1107 msg.rtm.rtm_flags |= RTF_REJECT;
1108
paul718e3742002-12-13 20:15:29 +00001109
paul718e3742002-12-13 20:15:29 +00001110#define SOCKADDRSET(X,R) \
1111 if (msg.rtm.rtm_addrs & (R)) \
1112 { \
paul6fe70d12005-11-12 22:55:10 +00001113 int len = SAROUNDUP (X); \
paul718e3742002-12-13 20:15:29 +00001114 memcpy (pnt, (caddr_t)(X), len); \
1115 pnt += len; \
1116 }
paul718e3742002-12-13 20:15:29 +00001117
1118 pnt = (caddr_t) msg.buf;
1119
1120 /* Write each socket data into rtm message buffer */
1121 SOCKADDRSET (dest, RTA_DST);
1122 SOCKADDRSET (gate, RTA_GATEWAY);
1123 SOCKADDRSET (mask, RTA_NETMASK);
1124
1125 msg.rtm.rtm_msglen = pnt - (caddr_t) &msg;
1126
1127 ret = write (routing_sock, &msg, msg.rtm.rtm_msglen);
1128
1129 if (ret != msg.rtm.rtm_msglen)
1130 {
1131 if (errno == EEXIST)
1132 return ZEBRA_ERR_RTEXIST;
1133 if (errno == ENETUNREACH)
1134 return ZEBRA_ERR_RTUNREACH;
Denis Ovsienkodc958242007-08-13 16:03:06 +00001135 if (errno == ESRCH)
1136 return ZEBRA_ERR_RTNOEXIST;
paul718e3742002-12-13 20:15:29 +00001137
Denis Ovsienkodc958242007-08-13 16:03:06 +00001138 zlog_warn ("%s: write : %s (%d)", __func__, safe_strerror (errno), errno);
1139 return ZEBRA_ERR_KERNEL;
paul718e3742002-12-13 20:15:29 +00001140 }
Denis Ovsienkodc958242007-08-13 16:03:06 +00001141 return ZEBRA_ERR_NOERROR;
paul718e3742002-12-13 20:15:29 +00001142}
1143
David Lamparter6b0655a2014-06-04 06:53:35 +02001144
paul718e3742002-12-13 20:15:29 +00001145#include "thread.h"
1146#include "zebra/zserv.h"
1147
paul718e3742002-12-13 20:15:29 +00001148/* For debug purpose. */
ajsb6178002004-12-07 21:12:56 +00001149static void
paul718e3742002-12-13 20:15:29 +00001150rtmsg_debug (struct rt_msghdr *rtm)
1151{
Denis Ovsienko2d844522007-09-14 11:31:55 +00001152 zlog_debug ("Kernel: Len: %d Type: %s", rtm->rtm_msglen, lookup (rtm_type_str, rtm->rtm_type));
paul718e3742002-12-13 20:15:29 +00001153 rtm_flag_dump (rtm->rtm_flags);
ajsb6178002004-12-07 21:12:56 +00001154 zlog_debug ("Kernel: message seq %d", rtm->rtm_seq);
paul6fe70d12005-11-12 22:55:10 +00001155 zlog_debug ("Kernel: pid %d, rtm_addrs 0x%x", rtm->rtm_pid, rtm->rtm_addrs);
paul718e3742002-12-13 20:15:29 +00001156}
1157
1158/* This is pretty gross, better suggestions welcome -- mhandler */
1159#ifndef RTAX_MAX
1160#ifdef RTA_NUMBITS
1161#define RTAX_MAX RTA_NUMBITS
1162#else
1163#define RTAX_MAX 8
1164#endif /* RTA_NUMBITS */
1165#endif /* RTAX_MAX */
1166
1167/* Kernel routing table and interface updates via routing socket. */
paul6621ca82005-11-23 13:02:08 +00001168static int
paul718e3742002-12-13 20:15:29 +00001169kernel_read (struct thread *thread)
1170{
1171 int sock;
1172 int nbytes;
1173 struct rt_msghdr *rtm;
1174
gdtdbee01f2004-01-06 00:36:51 +00001175 /*
1176 * This must be big enough for any message the kernel might send.
gdtb27900b2004-01-08 15:44:29 +00001177 * Rather than determining how many sockaddrs of what size might be
1178 * in each particular message, just use RTAX_MAX of sockaddr_storage
1179 * for each. Note that the sockaddrs must be after each message
1180 * definition, or rather after whichever happens to be the largest,
1181 * since the buffer needs to be big enough for a message and the
1182 * sockaddrs together.
gdtdbee01f2004-01-06 00:36:51 +00001183 */
paul718e3742002-12-13 20:15:29 +00001184 union
1185 {
1186 /* Routing information. */
1187 struct
1188 {
1189 struct rt_msghdr rtm;
gdtb27900b2004-01-08 15:44:29 +00001190 struct sockaddr_storage addr[RTAX_MAX];
paul718e3742002-12-13 20:15:29 +00001191 } r;
1192
1193 /* Interface information. */
1194 struct
1195 {
1196 struct if_msghdr ifm;
gdtb27900b2004-01-08 15:44:29 +00001197 struct sockaddr_storage addr[RTAX_MAX];
paul718e3742002-12-13 20:15:29 +00001198 } im;
1199
1200 /* Interface address information. */
1201 struct
1202 {
1203 struct ifa_msghdr ifa;
gdtb27900b2004-01-08 15:44:29 +00001204 struct sockaddr_storage addr[RTAX_MAX];
paul718e3742002-12-13 20:15:29 +00001205 } ia;
1206
1207#ifdef RTM_IFANNOUNCE
1208 /* Interface arrival/departure */
1209 struct
1210 {
1211 struct if_announcemsghdr ifan;
gdtb27900b2004-01-08 15:44:29 +00001212 struct sockaddr_storage addr[RTAX_MAX];
paul718e3742002-12-13 20:15:29 +00001213 } ian;
1214#endif /* RTM_IFANNOUNCE */
1215
1216 } buf;
1217
1218 /* Fetch routing socket. */
1219 sock = THREAD_FD (thread);
1220
1221 nbytes= read (sock, &buf, sizeof buf);
1222
1223 if (nbytes <= 0)
1224 {
1225 if (nbytes < 0 && errno != EWOULDBLOCK && errno != EAGAIN)
ajs6099b3b2004-11-20 02:06:59 +00001226 zlog_warn ("routing socket error: %s", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001227 return 0;
1228 }
1229
paul9bcdb632003-07-08 08:09:45 +00001230 thread_add_read (zebrad.master, kernel_read, NULL, sock);
paul718e3742002-12-13 20:15:29 +00001231
hasso726f9b22003-05-25 21:04:54 +00001232 if (IS_ZEBRA_DEBUG_KERNEL)
1233 rtmsg_debug (&buf.r.rtm);
paul718e3742002-12-13 20:15:29 +00001234
1235 rtm = &buf.r.rtm;
1236
gdtb27900b2004-01-08 15:44:29 +00001237 /*
1238 * Ensure that we didn't drop any data, so that processing routines
1239 * can assume they have the whole message.
1240 */
gdtda26e3b2004-01-05 17:20:59 +00001241 if (rtm->rtm_msglen != nbytes)
1242 {
1243 zlog_warn ("kernel_read: rtm->rtm_msglen %d, nbytes %d, type %d\n",
1244 rtm->rtm_msglen, nbytes, rtm->rtm_type);
1245 return -1;
1246 }
1247
paul718e3742002-12-13 20:15:29 +00001248 switch (rtm->rtm_type)
1249 {
1250 case RTM_ADD:
1251 case RTM_DELETE:
paulca162182005-09-12 16:58:52 +00001252 case RTM_CHANGE:
paul718e3742002-12-13 20:15:29 +00001253 rtm_read (rtm);
1254 break;
1255 case RTM_IFINFO:
1256 ifm_read (&buf.im.ifm);
1257 break;
1258 case RTM_NEWADDR:
1259 case RTM_DELADDR:
1260 ifam_read (&buf.ia.ifa);
1261 break;
1262#ifdef RTM_IFANNOUNCE
1263 case RTM_IFANNOUNCE:
1264 ifan_read (&buf.ian.ifan);
1265 break;
1266#endif /* RTM_IFANNOUNCE */
1267 default:
hasso726f9b22003-05-25 21:04:54 +00001268 if (IS_ZEBRA_DEBUG_KERNEL)
ajsb6178002004-12-07 21:12:56 +00001269 zlog_debug("Unprocessed RTM_type: %d", rtm->rtm_type);
paul718e3742002-12-13 20:15:29 +00001270 break;
1271 }
1272 return 0;
1273}
1274
1275/* Make routing socket. */
paul6621ca82005-11-23 13:02:08 +00001276static void
Feng Lu758fb8f2014-07-03 18:23:09 +08001277routing_socket (struct zebra_vrf *zvrf)
paul718e3742002-12-13 20:15:29 +00001278{
Feng Lu758fb8f2014-07-03 18:23:09 +08001279 if (zvrf->vrf_id != VRF_DEFAULT)
1280 return;
1281
pauledd7c242003-06-04 13:59:38 +00001282 if ( zserv_privs.change (ZPRIVS_RAISE) )
1283 zlog_err ("routing_socket: Can't raise privileges");
1284
paul718e3742002-12-13 20:15:29 +00001285 routing_sock = socket (AF_ROUTE, SOCK_RAW, 0);
1286
1287 if (routing_sock < 0)
1288 {
pauledd7c242003-06-04 13:59:38 +00001289 if ( zserv_privs.change (ZPRIVS_LOWER) )
1290 zlog_err ("routing_socket: Can't lower privileges");
paul718e3742002-12-13 20:15:29 +00001291 zlog_warn ("Can't init kernel routing socket");
1292 return;
1293 }
1294
paul865b8522005-01-05 08:30:35 +00001295 /* XXX: Socket should be NONBLOCK, however as we currently
1296 * discard failed writes, this will lead to inconsistencies.
1297 * For now, socket must be blocking.
1298 */
1299 /*if (fcntl (routing_sock, F_SETFL, O_NONBLOCK) < 0)
1300 zlog_warn ("Can't set O_NONBLOCK to routing socket");*/
1301
pauledd7c242003-06-04 13:59:38 +00001302 if ( zserv_privs.change (ZPRIVS_LOWER) )
1303 zlog_err ("routing_socket: Can't lower privileges");
paul718e3742002-12-13 20:15:29 +00001304
1305 /* kernel_read needs rewrite. */
paul9bcdb632003-07-08 08:09:45 +00001306 thread_add_read (zebrad.master, kernel_read, NULL, routing_sock);
paul718e3742002-12-13 20:15:29 +00001307}
1308
1309/* Exported interface function. This function simply calls
1310 routing_socket (). */
1311void
Feng Lu758fb8f2014-07-03 18:23:09 +08001312kernel_init (struct zebra_vrf *zvrf)
paul718e3742002-12-13 20:15:29 +00001313{
Feng Lu758fb8f2014-07-03 18:23:09 +08001314 routing_socket (zvrf);
1315}
1316
1317void
1318kernel_terminate (struct zebra_vrf *zvrf)
1319{
1320 return;
paul718e3742002-12-13 20:15:29 +00001321}