blob: ecc6e7905d0f925e4295f2c20c4f2e28d021bb93 [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/*
45 * Given a sockaddr length, round it up to include pad bytes following
46 * it. Assumes the kernel pads to sizeof(long).
47 *
48 * XXX: why is ROUNDUP(0) sizeof(long)? 0 is an illegal sockaddr
49 * length anyway (< sizeof (struct sockaddr)), so this shouldn't
50 * matter.
Doug VanLeuven3b33de62012-10-10 22:10:14 +000051 * On OS X, both 32, 64bit syatems align on 4 byte boundary
gdt4bfbea82004-01-06 01:13:05 +000052 */
Doug VanLeuven3b33de62012-10-10 22:10:14 +000053#ifdef __APPLE__
54#define ROUNDUP(a) \
55 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(int) - 1))) : sizeof(int))
56#else
paul718e3742002-12-13 20:15:29 +000057#define ROUNDUP(a) \
58 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
Doug VanLeuven3b33de62012-10-10 22:10:14 +000059#endif
paul718e3742002-12-13 20:15:29 +000060
gdt4bfbea82004-01-06 01:13:05 +000061/*
62 * Given a pointer (sockaddr or void *), return the number of bytes
63 * taken up by the sockaddr and any padding needed for alignment.
64 */
Paul Jakma6f0e3f62007-05-10 02:38:51 +000065#if defined(HAVE_STRUCT_SOCKADDR_SA_LEN)
gdt4bfbea82004-01-06 01:13:05 +000066#define SAROUNDUP(X) ROUNDUP(((struct sockaddr *)(X))->sa_len)
paul30be8022003-10-22 02:51:38 +000067#elif defined(HAVE_IPV6)
gdt4bfbea82004-01-06 01:13:05 +000068/*
69 * One would hope all fixed-size structure definitions are aligned,
70 * but round them up nonetheless.
71 */
72#define SAROUNDUP(X) \
paul3e95a072003-09-24 00:05:45 +000073 (((struct sockaddr *)(X))->sa_family == AF_INET ? \
74 ROUNDUP(sizeof(struct sockaddr_in)):\
75 (((struct sockaddr *)(X))->sa_family == AF_INET6 ? \
76 ROUNDUP(sizeof(struct sockaddr_in6)) : \
77 (((struct sockaddr *)(X))->sa_family == AF_LINK ? \
paulc50ae8b2004-05-11 11:31:07 +000078 ROUNDUP(sizeof(struct sockaddr_dl)) : sizeof(struct sockaddr))))
paul30be8022003-10-22 02:51:38 +000079#else /* HAVE_IPV6 */
gdt4bfbea82004-01-06 01:13:05 +000080#define SAROUNDUP(X) \
paul30be8022003-10-22 02:51:38 +000081 (((struct sockaddr *)(X))->sa_family == AF_INET ? \
82 ROUNDUP(sizeof(struct sockaddr_in)):\
83 (((struct sockaddr *)(X))->sa_family == AF_LINK ? \
84 ROUNDUP(sizeof(struct sockaddr_dl)) : sizeof(struct sockaddr)))
Paul Jakma6f0e3f62007-05-10 02:38:51 +000085#endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
paul718e3742002-12-13 20:15:29 +000086
paulec1a4282005-11-24 15:15:17 +000087/* We use an additional pointer in following, pdest, rather than (DEST)
88 * directly, because gcc will warn if the macro is expanded and DEST is NULL,
89 * complaining that memcpy is being passed a NULL value, despite the fact
90 * the if (NULL) makes it impossible.
91 */
paul62debbb2005-06-14 14:07:07 +000092#define RTA_ADDR_GET(DEST, RTA, RTMADDRS, PNT) \
93 if ((RTMADDRS) & (RTA)) \
94 { \
paulec1a4282005-11-24 15:15:17 +000095 void *pdest = (DEST); \
paul62debbb2005-06-14 14:07:07 +000096 int len = SAROUNDUP ((PNT)); \
paulea6f82b2005-06-28 17:20:26 +000097 if ( ((DEST) != NULL) && \
paul62debbb2005-06-14 14:07:07 +000098 af_check (((struct sockaddr *)(PNT))->sa_family)) \
paulec1a4282005-11-24 15:15:17 +000099 memcpy (pdest, (PNT), len); \
paul62debbb2005-06-14 14:07:07 +0000100 (PNT) += len; \
101 }
102#define RTA_ATTR_GET(DEST, RTA, RTMADDRS, PNT) \
103 if ((RTMADDRS) & (RTA)) \
104 { \
paulec1a4282005-11-24 15:15:17 +0000105 void *pdest = (DEST); \
paul62debbb2005-06-14 14:07:07 +0000106 int len = SAROUNDUP ((PNT)); \
paulec1a4282005-11-24 15:15:17 +0000107 if ((DEST) != NULL) \
108 memcpy (pdest, (PNT), len); \
paul62debbb2005-06-14 14:07:07 +0000109 (PNT) += len; \
110 }
111
paul6fe70d12005-11-12 22:55:10 +0000112#define RTA_NAME_GET(DEST, RTA, RTMADDRS, PNT, LEN) \
113 if ((RTMADDRS) & (RTA)) \
114 { \
paulec1a4282005-11-24 15:15:17 +0000115 u_char *pdest = (u_char *) (DEST); \
paul6fe70d12005-11-12 22:55:10 +0000116 int len = SAROUNDUP ((PNT)); \
117 struct sockaddr_dl *sdl = (struct sockaddr_dl *)(PNT); \
118 if (IS_ZEBRA_DEBUG_KERNEL) \
119 zlog_debug ("%s: RTA_SDL_GET nlen %d, alen %d", \
120 __func__, sdl->sdl_nlen, sdl->sdl_alen); \
121 if ( ((DEST) != NULL) && (sdl->sdl_family == AF_LINK) \
122 && (sdl->sdl_nlen < IFNAMSIZ) && (sdl->sdl_nlen <= len) ) \
123 { \
paulec1a4282005-11-24 15:15:17 +0000124 memcpy (pdest, sdl->sdl_data, sdl->sdl_nlen); \
125 pdest[sdl->sdl_nlen] = '\0'; \
paul6fe70d12005-11-12 22:55:10 +0000126 (LEN) = sdl->sdl_nlen; \
127 } \
128 (PNT) += len; \
129 } \
130 else \
131 { \
132 (LEN) = 0; \
133 }
paul718e3742002-12-13 20:15:29 +0000134/* Routing socket message types. */
Stephen Hemminger1423c802008-08-14 17:59:25 +0100135const struct message rtm_type_str[] =
paul718e3742002-12-13 20:15:29 +0000136{
137 {RTM_ADD, "RTM_ADD"},
138 {RTM_DELETE, "RTM_DELETE"},
139 {RTM_CHANGE, "RTM_CHANGE"},
140 {RTM_GET, "RTM_GET"},
141 {RTM_LOSING, "RTM_LOSING"},
142 {RTM_REDIRECT, "RTM_REDIRECT"},
143 {RTM_MISS, "RTM_MISS"},
144 {RTM_LOCK, "RTM_LOCK"},
Greg Troxel9458b812006-09-13 12:13:08 +0000145#ifdef OLDADD
paul718e3742002-12-13 20:15:29 +0000146 {RTM_OLDADD, "RTM_OLDADD"},
Greg Troxel9458b812006-09-13 12:13:08 +0000147#endif /* RTM_OLDADD */
148#ifdef RTM_OLDDEL
paul718e3742002-12-13 20:15:29 +0000149 {RTM_OLDDEL, "RTM_OLDDEL"},
Greg Troxel9458b812006-09-13 12:13:08 +0000150#endif /* RTM_OLDDEL */
paul718e3742002-12-13 20:15:29 +0000151 {RTM_RESOLVE, "RTM_RESOLVE"},
152 {RTM_NEWADDR, "RTM_NEWADDR"},
153 {RTM_DELADDR, "RTM_DELADDR"},
154 {RTM_IFINFO, "RTM_IFINFO"},
155#ifdef RTM_OIFINFO
156 {RTM_OIFINFO, "RTM_OIFINFO"},
157#endif /* RTM_OIFINFO */
158#ifdef RTM_NEWMADDR
159 {RTM_NEWMADDR, "RTM_NEWMADDR"},
160#endif /* RTM_NEWMADDR */
161#ifdef RTM_DELMADDR
162 {RTM_DELMADDR, "RTM_DELMADDR"},
163#endif /* RTM_DELMADDR */
164#ifdef RTM_IFANNOUNCE
165 {RTM_IFANNOUNCE, "RTM_IFANNOUNCE"},
166#endif /* RTM_IFANNOUNCE */
167 {0, NULL}
168};
169
Stephen Hemmingerce0db9c2009-05-15 10:47:04 -0700170static const struct message rtm_flag_str[] =
paul718e3742002-12-13 20:15:29 +0000171{
172 {RTF_UP, "UP"},
173 {RTF_GATEWAY, "GATEWAY"},
174 {RTF_HOST, "HOST"},
175 {RTF_REJECT, "REJECT"},
176 {RTF_DYNAMIC, "DYNAMIC"},
177 {RTF_MODIFIED, "MODIFIED"},
178 {RTF_DONE, "DONE"},
179#ifdef RTF_MASK
180 {RTF_MASK, "MASK"},
181#endif /* RTF_MASK */
David Warde6f148e2009-12-03 21:43:11 +0300182#ifdef RTF_CLONING
paul718e3742002-12-13 20:15:29 +0000183 {RTF_CLONING, "CLONING"},
David Warde6f148e2009-12-03 21:43:11 +0300184#endif /* RTF_CLONING */
paul718e3742002-12-13 20:15:29 +0000185 {RTF_XRESOLVE, "XRESOLVE"},
186 {RTF_LLINFO, "LLINFO"},
187 {RTF_STATIC, "STATIC"},
188 {RTF_BLACKHOLE, "BLACKHOLE"},
paul6fe70d12005-11-12 22:55:10 +0000189#ifdef RTF_PRIVATE
190 {RTF_PRIVATE, "PRIVATE"},
191#endif /* RTF_PRIVATE */
paul718e3742002-12-13 20:15:29 +0000192 {RTF_PROTO1, "PROTO1"},
193 {RTF_PROTO2, "PROTO2"},
194#ifdef RTF_PRCLONING
195 {RTF_PRCLONING, "PRCLONING"},
196#endif /* RTF_PRCLONING */
197#ifdef RTF_WASCLONED
198 {RTF_WASCLONED, "WASCLONED"},
199#endif /* RTF_WASCLONED */
200#ifdef RTF_PROTO3
201 {RTF_PROTO3, "PROTO3"},
202#endif /* RTF_PROTO3 */
203#ifdef RTF_PINNED
204 {RTF_PINNED, "PINNED"},
205#endif /* RTF_PINNED */
206#ifdef RTF_LOCAL
207 {RTF_LOCAL, "LOCAL"},
208#endif /* RTF_LOCAL */
209#ifdef RTF_BROADCAST
210 {RTF_BROADCAST, "BROADCAST"},
211#endif /* RTF_BROADCAST */
212#ifdef RTF_MULTICAST
213 {RTF_MULTICAST, "MULTICAST"},
214#endif /* RTF_MULTICAST */
paul6fe70d12005-11-12 22:55:10 +0000215#ifdef RTF_MULTIRT
216 {RTF_MULTIRT, "MULTIRT"},
217#endif /* RTF_MULTIRT */
218#ifdef RTF_SETSRC
219 {RTF_SETSRC, "SETSRC"},
220#endif /* RTF_SETSRC */
paul718e3742002-12-13 20:15:29 +0000221 {0, NULL}
222};
223
224/* Kernel routing update socket. */
225int routing_sock = -1;
226
227/* Yes I'm checking ugly routing socket behavior. */
228/* #define DEBUG */
229
230/* Supported address family check. */
paul62debbb2005-06-14 14:07:07 +0000231static int inline
paul718e3742002-12-13 20:15:29 +0000232af_check (int family)
233{
234 if (family == AF_INET)
235 return 1;
236#ifdef HAVE_IPV6
237 if (family == AF_INET6)
238 return 1;
239#endif /* HAVE_IPV6 */
240 return 0;
241}
242
243/* Dump routing table flag for debug purpose. */
ajsb6178002004-12-07 21:12:56 +0000244static void
paul718e3742002-12-13 20:15:29 +0000245rtm_flag_dump (int flag)
246{
Tom Goff80b2a942009-12-03 14:53:15 +0300247 const struct message *mes;
paul718e3742002-12-13 20:15:29 +0000248 static char buf[BUFSIZ];
249
gdtcced60d2004-07-13 16:45:54 +0000250 buf[0] = '\0';
paul718e3742002-12-13 20:15:29 +0000251 for (mes = rtm_flag_str; mes->key != 0; mes++)
252 {
253 if (mes->key & flag)
254 {
255 strlcat (buf, mes->str, BUFSIZ);
256 strlcat (buf, " ", BUFSIZ);
257 }
258 }
ajsb6178002004-12-07 21:12:56 +0000259 zlog_debug ("Kernel: %s", buf);
paul718e3742002-12-13 20:15:29 +0000260}
261
262#ifdef RTM_IFANNOUNCE
263/* Interface adding function */
paul6621ca82005-11-23 13:02:08 +0000264static int
paul718e3742002-12-13 20:15:29 +0000265ifan_read (struct if_announcemsghdr *ifan)
266{
267 struct interface *ifp;
paul6fe70d12005-11-12 22:55:10 +0000268
paul718e3742002-12-13 20:15:29 +0000269 ifp = if_lookup_by_index (ifan->ifan_index);
paul6fe70d12005-11-12 22:55:10 +0000270
271 if (ifp)
272 assert ( (ifp->ifindex == ifan->ifan_index)
273 || (ifp->ifindex == IFINDEX_INTERNAL) );
274
paulec1a4282005-11-24 15:15:17 +0000275 if ( (ifp == NULL)
276 || ((ifp->ifindex == IFINDEX_INTERNAL)
277 && (ifan->ifan_what == IFAN_ARRIVAL)) )
paul718e3742002-12-13 20:15:29 +0000278 {
paul6fe70d12005-11-12 22:55:10 +0000279 if (IS_ZEBRA_DEBUG_KERNEL)
280 zlog_debug ("%s: creating interface for ifindex %d, name %s",
281 __func__, ifan->ifan_index, ifan->ifan_name);
282
paul718e3742002-12-13 20:15:29 +0000283 /* Create Interface */
ajs08dbfb62005-04-03 03:40:52 +0000284 ifp = if_get_by_name_len(ifan->ifan_name,
285 strnlen(ifan->ifan_name,
286 sizeof(ifan->ifan_name)));
paul718e3742002-12-13 20:15:29 +0000287 ifp->ifindex = ifan->ifan_index;
288
289 if_add_update (ifp);
290 }
291 else if (ifp != NULL && ifan->ifan_what == IFAN_DEPARTURE)
paul6eb88272005-07-29 14:36:00 +0000292 if_delete_update (ifp);
paul718e3742002-12-13 20:15:29 +0000293
294 if_get_flags (ifp);
295 if_get_mtu (ifp);
296 if_get_metric (ifp);
297
298 if (IS_ZEBRA_DEBUG_KERNEL)
paul6fe70d12005-11-12 22:55:10 +0000299 zlog_debug ("%s: interface %s index %d",
300 __func__, ifan->ifan_name, ifan->ifan_index);
paul718e3742002-12-13 20:15:29 +0000301
302 return 0;
303}
304#endif /* RTM_IFANNOUNCE */
305
Andrew J. Schorrc543a172008-01-10 15:24:32 +0000306#ifdef HAVE_BSD_LINK_DETECT
307/* BSD link detect translation */
308static void
309bsd_linkdetect_translate (struct if_msghdr *ifm)
310{
Andrew J. Schorr55edb0d2008-01-11 15:57:13 +0000311 if ((ifm->ifm_data.ifi_link_state >= LINK_STATE_UP) ||
312 (ifm->ifm_data.ifi_link_state == LINK_STATE_UNKNOWN))
Andrew J. Schorrc543a172008-01-10 15:24:32 +0000313 SET_FLAG(ifm->ifm_flags, IFF_RUNNING);
314 else
315 UNSET_FLAG(ifm->ifm_flags, IFF_RUNNING);
316}
317#endif /* HAVE_BSD_LINK_DETECT */
318
gdtda26e3b2004-01-05 17:20:59 +0000319/*
320 * Handle struct if_msghdr obtained from reading routing socket or
321 * sysctl (from interface_list). There may or may not be sockaddrs
322 * present after the header.
323 */
paulec1a4282005-11-24 15:15:17 +0000324int
paul718e3742002-12-13 20:15:29 +0000325ifm_read (struct if_msghdr *ifm)
326{
paul3e95a072003-09-24 00:05:45 +0000327 struct interface *ifp = NULL;
Tom Goffa34eb362009-11-25 20:36:06 +0000328 struct sockaddr_dl *sdl;
paul6fe70d12005-11-12 22:55:10 +0000329 char ifname[IFNAMSIZ];
330 short ifnlen = 0;
paul0994c3a2005-11-11 09:52:40 +0000331 caddr_t *cp;
paul6fe70d12005-11-12 22:55:10 +0000332
333 /* terminate ifname at head (for strnlen) and tail (for safety) */
334 ifname[IFNAMSIZ - 1] = '\0';
335
gdtda26e3b2004-01-05 17:20:59 +0000336 /* paranoia: sanity check structure */
337 if (ifm->ifm_msglen < sizeof(struct if_msghdr))
338 {
339 zlog_err ("ifm_read: ifm->ifm_msglen %d too short\n",
340 ifm->ifm_msglen);
341 return -1;
342 }
343
344 /*
gdt4bfbea82004-01-06 01:13:05 +0000345 * Check for a sockaddr_dl following the message. First, point to
346 * where a socakddr might be if one follows the message.
gdtda26e3b2004-01-05 17:20:59 +0000347 */
gdt4bfbea82004-01-06 01:13:05 +0000348 cp = (void *)(ifm + 1);
349
paul3e95a072003-09-24 00:05:45 +0000350#ifdef SUNOS_5
paul3e95a072003-09-24 00:05:45 +0000351 /*
gdt4bfbea82004-01-06 01:13:05 +0000352 * XXX This behavior should be narrowed to only the kernel versions
353 * for which the structures returned do not match the headers.
354 *
paul3e95a072003-09-24 00:05:45 +0000355 * if_msghdr_t on 64 bit kernels in Solaris 9 and earlier versions
gdt4bfbea82004-01-06 01:13:05 +0000356 * is 12 bytes larger than the 32 bit version.
paul3e95a072003-09-24 00:05:45 +0000357 */
gdt4bfbea82004-01-06 01:13:05 +0000358 if (((struct sockaddr *) cp)->sa_family == AF_UNSPEC)
paul3e95a072003-09-24 00:05:45 +0000359 cp = cp + 12;
paul3e95a072003-09-24 00:05:45 +0000360#endif
paul718e3742002-12-13 20:15:29 +0000361
paul6fe70d12005-11-12 22:55:10 +0000362 RTA_ADDR_GET (NULL, RTA_DST, ifm->ifm_addrs, cp);
363 RTA_ADDR_GET (NULL, RTA_GATEWAY, ifm->ifm_addrs, cp);
364 RTA_ATTR_GET (NULL, RTA_NETMASK, ifm->ifm_addrs, cp);
365 RTA_ADDR_GET (NULL, RTA_GENMASK, ifm->ifm_addrs, cp);
Tom Goffa34eb362009-11-25 20:36:06 +0000366 sdl = (struct sockaddr_dl *)cp;
paul6fe70d12005-11-12 22:55:10 +0000367 RTA_NAME_GET (ifname, RTA_IFP, ifm->ifm_addrs, cp, ifnlen);
368 RTA_ADDR_GET (NULL, RTA_IFA, ifm->ifm_addrs, cp);
369 RTA_ADDR_GET (NULL, RTA_AUTHOR, ifm->ifm_addrs, cp);
370 RTA_ADDR_GET (NULL, RTA_BRD, ifm->ifm_addrs, cp);
371
372 if (IS_ZEBRA_DEBUG_KERNEL)
373 zlog_debug ("%s: sdl ifname %s", __func__, (ifnlen ? ifname : "(nil)"));
374
gdt4bfbea82004-01-06 01:13:05 +0000375 /*
paul6fe70d12005-11-12 22:55:10 +0000376 * Look up on ifindex first, because ifindices are the primary handle for
377 * interfaces across the user/kernel boundary, for most systems. (Some
378 * messages, such as up/down status changes on NetBSD, do not include a
379 * sockaddr_dl).
gdt4bfbea82004-01-06 01:13:05 +0000380 */
paul6fe70d12005-11-12 22:55:10 +0000381 if ( (ifp = if_lookup_by_index (ifm->ifm_index)) != NULL )
gdt4bfbea82004-01-06 01:13:05 +0000382 {
paul6fe70d12005-11-12 22:55:10 +0000383 /* we have an ifp, verify that the name matches as some systems,
384 * eg Solaris, have a 1:many association of ifindex:ifname
385 * if they dont match, we dont have the correct ifp and should
386 * set it back to NULL to let next check do lookup by name
387 */
388 if (ifnlen && (strncmp (ifp->name, ifname, IFNAMSIZ) != 0) )
gdt4bfbea82004-01-06 01:13:05 +0000389 {
paul6fe70d12005-11-12 22:55:10 +0000390 if (IS_ZEBRA_DEBUG_KERNEL)
391 zlog_debug ("%s: ifp name %s doesnt match sdl name %s",
392 __func__, ifp->name, ifname);
393 ifp = NULL;
gdt4bfbea82004-01-06 01:13:05 +0000394 }
395 }
paul6fe70d12005-11-12 22:55:10 +0000396
gdtda26e3b2004-01-05 17:20:59 +0000397 /*
paul6fe70d12005-11-12 22:55:10 +0000398 * If we dont have an ifp, try looking up by name. Particularly as some
399 * systems (Solaris) have a 1:many mapping of ifindex:ifname - the ifname
400 * is therefore our unique handle to that interface.
401 *
402 * Interfaces specified in the configuration file for which the ifindex
403 * has not been determined will have ifindex == IFINDEX_INTERNAL, and such
404 * interfaces are found by this search, and then their ifindex values can
405 * be filled in.
gdtda26e3b2004-01-05 17:20:59 +0000406 */
paul6fe70d12005-11-12 22:55:10 +0000407 if ( (ifp == NULL) && ifnlen)
408 ifp = if_lookup_by_name (ifname);
paul718e3742002-12-13 20:15:29 +0000409
gdtda26e3b2004-01-05 17:20:59 +0000410 /*
paul6fe70d12005-11-12 22:55:10 +0000411 * If ifp still does not exist or has an invalid index (IFINDEX_INTERNAL),
412 * create or fill in an interface.
gdtda26e3b2004-01-05 17:20:59 +0000413 */
ajsd2fc8892005-04-02 18:38:43 +0000414 if ((ifp == NULL) || (ifp->ifindex == IFINDEX_INTERNAL))
paul718e3742002-12-13 20:15:29 +0000415 {
gdt4bfbea82004-01-06 01:13:05 +0000416 /*
417 * To create or fill in an interface, a sockaddr_dl (via
418 * RTA_IFP) is required.
419 */
paul6fe70d12005-11-12 22:55:10 +0000420 if (!ifnlen)
paul718e3742002-12-13 20:15:29 +0000421 {
paul6fe70d12005-11-12 22:55:10 +0000422 zlog_warn ("Interface index %d (new) missing ifname\n",
paul718e3742002-12-13 20:15:29 +0000423 ifm->ifm_index);
424 return -1;
425 }
paul5c78b3d2006-01-25 04:31:40 +0000426
427#ifndef RTM_IFANNOUNCE
428 /* Down->Down interface should be ignored here.
429 * See further comment below.
430 */
431 if (!CHECK_FLAG (ifm->ifm_flags, IFF_UP))
432 return 0;
433#endif /* !RTM_IFANNOUNCE */
paul6fe70d12005-11-12 22:55:10 +0000434
paul3e95a072003-09-24 00:05:45 +0000435 if (ifp == NULL)
paul6fe70d12005-11-12 22:55:10 +0000436 {
437 /* Interface that zebra was not previously aware of, so create. */
438 ifp = if_create (ifname, ifnlen);
439 if (IS_ZEBRA_DEBUG_KERNEL)
440 zlog_debug ("%s: creating ifp for ifindex %d",
441 __func__, ifm->ifm_index);
442 }
paul718e3742002-12-13 20:15:29 +0000443
paul6fe70d12005-11-12 22:55:10 +0000444 if (IS_ZEBRA_DEBUG_KERNEL)
445 zlog_debug ("%s: updated/created ifp, ifname %s, ifindex %d",
446 __func__, ifp->name, ifp->ifindex);
gdt4bfbea82004-01-06 01:13:05 +0000447 /*
448 * Fill in newly created interface structure, or larval
ajsd2fc8892005-04-02 18:38:43 +0000449 * structure with ifindex IFINDEX_INTERNAL.
gdt4bfbea82004-01-06 01:13:05 +0000450 */
paul718e3742002-12-13 20:15:29 +0000451 ifp->ifindex = ifm->ifm_index;
Andrew J. Schorrc543a172008-01-10 15:24:32 +0000452
453#ifdef HAVE_BSD_LINK_DETECT /* translate BSD kernel msg for link-state */
454 bsd_linkdetect_translate(ifm);
455#endif /* HAVE_BSD_LINK_DETECT */
456
paul5c78b3d2006-01-25 04:31:40 +0000457 if_flags_update (ifp, ifm->ifm_flags);
paul718e3742002-12-13 20:15:29 +0000458#if defined(__bsdi__)
459 if_kvm_get_mtu (ifp);
460#else
461 if_get_mtu (ifp);
462#endif /* __bsdi__ */
463 if_get_metric (ifp);
464
Tom Goffa34eb362009-11-25 20:36:06 +0000465 /*
466 * XXX sockaddr_dl contents can be larger than the structure
467 * definition, so the user of the stored structure must be
468 * careful not to read off the end.
469 *
470 * a nonzero ifnlen from RTA_NAME_GET() means sdl is valid
471 */
472 if (ifnlen)
473 memcpy (&ifp->sdl, sdl, sizeof (struct sockaddr_dl));
474
paul718e3742002-12-13 20:15:29 +0000475 if_add_update (ifp);
476 }
477 else
gdtda26e3b2004-01-05 17:20:59 +0000478 /*
479 * Interface structure exists. Adjust stored flags from
480 * notification. If interface has up->down or down->up
481 * transition, call state change routines (to adjust routes,
482 * notify routing daemons, etc.). (Other flag changes are stored
483 * but apparently do not trigger action.)
484 */
paul718e3742002-12-13 20:15:29 +0000485 {
paul6fe70d12005-11-12 22:55:10 +0000486 if (ifp->ifindex != ifm->ifm_index)
487 {
488 zlog_warn ("%s: index mismatch, ifname %s, ifp index %d, "
489 "ifm index %d",
490 __func__, ifp->name, ifp->ifindex, ifm->ifm_index);
491 return -1;
492 }
493
Andrew J. Schorrc543a172008-01-10 15:24:32 +0000494#ifdef HAVE_BSD_LINK_DETECT /* translate BSD kernel msg for link-state */
495 bsd_linkdetect_translate(ifm);
496#endif /* HAVE_BSD_LINK_DETECT */
497
paul5c78b3d2006-01-25 04:31:40 +0000498 /* update flags and handle operative->inoperative transition, if any */
499 if_flags_update (ifp, ifm->ifm_flags);
500
paul6eb88272005-07-29 14:36:00 +0000501#ifndef RTM_IFANNOUNCE
paul5c78b3d2006-01-25 04:31:40 +0000502 if (!if_is_up (ifp))
503 {
504 /* No RTM_IFANNOUNCE on this platform, so we can never
505 * distinguish between ~IFF_UP and delete. We must presume
506 * it has been deleted.
507 * Eg, Solaris will not notify us of unplumb.
508 *
509 * XXX: Fixme - this should be runtime detected
510 * So that a binary compiled on a system with IFANNOUNCE
511 * will still behave correctly if run on a platform without
512 */
513 if_delete_update (ifp);
514 }
paul6eb88272005-07-29 14:36:00 +0000515#endif /* RTM_IFANNOUNCE */
Denis Ovsienko1ba27562007-08-21 16:15:39 +0000516 if (if_is_up (ifp))
517 {
518#if defined(__bsdi__)
519 if_kvm_get_mtu (ifp);
520#else
521 if_get_mtu (ifp);
522#endif /* __bsdi__ */
523 if_get_metric (ifp);
524 }
paul718e3742002-12-13 20:15:29 +0000525 }
paul5c78b3d2006-01-25 04:31:40 +0000526
paul718e3742002-12-13 20:15:29 +0000527#ifdef HAVE_NET_RT_IFLIST
528 ifp->stats = ifm->ifm_data;
529#endif /* HAVE_NET_RT_IFLIST */
530
531 if (IS_ZEBRA_DEBUG_KERNEL)
paul6fe70d12005-11-12 22:55:10 +0000532 zlog_debug ("%s: interface %s index %d",
533 __func__, ifp->name, ifp->ifindex);
paul718e3742002-12-13 20:15:29 +0000534
535 return 0;
536}
537
538/* Address read from struct ifa_msghdr. */
paul6621ca82005-11-23 13:02:08 +0000539static void
paul718e3742002-12-13 20:15:29 +0000540ifam_read_mesg (struct ifa_msghdr *ifm,
541 union sockunion *addr,
542 union sockunion *mask,
paul6fe70d12005-11-12 22:55:10 +0000543 union sockunion *brd,
544 char *ifname,
545 short *ifnlen)
paul718e3742002-12-13 20:15:29 +0000546{
547 caddr_t pnt, end;
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000548 union sockunion dst;
549 union sockunion gateway;
paul718e3742002-12-13 20:15:29 +0000550
551 pnt = (caddr_t)(ifm + 1);
552 end = ((caddr_t)ifm) + ifm->ifam_msglen;
553
paul718e3742002-12-13 20:15:29 +0000554 /* Be sure structure is cleared */
555 memset (mask, 0, sizeof (union sockunion));
556 memset (addr, 0, sizeof (union sockunion));
paul6621ca82005-11-23 13:02:08 +0000557 memset (brd, 0, sizeof (union sockunion));
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000558 memset (&dst, 0, sizeof (union sockunion));
559 memset (&gateway, 0, sizeof (union sockunion));
paul718e3742002-12-13 20:15:29 +0000560
561 /* We fetch each socket variable into sockunion. */
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000562 RTA_ADDR_GET (&dst, RTA_DST, ifm->ifam_addrs, pnt);
563 RTA_ADDR_GET (&gateway, RTA_GATEWAY, ifm->ifam_addrs, pnt);
paul62debbb2005-06-14 14:07:07 +0000564 RTA_ATTR_GET (mask, RTA_NETMASK, ifm->ifam_addrs, pnt);
565 RTA_ADDR_GET (NULL, RTA_GENMASK, ifm->ifam_addrs, pnt);
paul6fe70d12005-11-12 22:55:10 +0000566 RTA_NAME_GET (ifname, RTA_IFP, ifm->ifam_addrs, pnt, *ifnlen);
paul62debbb2005-06-14 14:07:07 +0000567 RTA_ADDR_GET (addr, RTA_IFA, ifm->ifam_addrs, pnt);
568 RTA_ADDR_GET (NULL, RTA_AUTHOR, ifm->ifam_addrs, pnt);
paul6fe70d12005-11-12 22:55:10 +0000569 RTA_ADDR_GET (brd, RTA_BRD, ifm->ifam_addrs, pnt);
paul718e3742002-12-13 20:15:29 +0000570
paul6fe70d12005-11-12 22:55:10 +0000571 if (IS_ZEBRA_DEBUG_KERNEL)
Andrew J. Schorr55196042006-05-17 15:04:59 +0000572 {
573 switch (sockunion_family(addr))
574 {
575 case AF_INET:
576 {
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000577 char buf[4][INET_ADDRSTRLEN];
Andrew J. Schorr55196042006-05-17 15:04:59 +0000578 zlog_debug ("%s: ifindex %d, ifname %s, ifam_addrs 0x%x, "
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000579 "ifam_flags 0x%x, addr %s/%d broad %s dst %s "
580 "gateway %s",
581 __func__, ifm->ifam_index,
Andrew J. Schorr55196042006-05-17 15:04:59 +0000582 (ifnlen ? ifname : "(nil)"), ifm->ifam_addrs,
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000583 ifm->ifam_flags,
Andrew J. Schorr55196042006-05-17 15:04:59 +0000584 inet_ntop(AF_INET,&addr->sin.sin_addr,
585 buf[0],sizeof(buf[0])),
586 ip_masklen(mask->sin.sin_addr),
587 inet_ntop(AF_INET,&brd->sin.sin_addr,
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000588 buf[1],sizeof(buf[1])),
589 inet_ntop(AF_INET,&dst.sin.sin_addr,
590 buf[2],sizeof(buf[2])),
591 inet_ntop(AF_INET,&gateway.sin.sin_addr,
592 buf[3],sizeof(buf[3])));
Andrew J. Schorr55196042006-05-17 15:04:59 +0000593 }
594 break;
595#ifdef HAVE_IPV6
596 case AF_INET6:
597 {
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000598 char buf[4][INET6_ADDRSTRLEN];
Andrew J. Schorr55196042006-05-17 15:04:59 +0000599 zlog_debug ("%s: ifindex %d, ifname %s, ifam_addrs 0x%x, "
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000600 "ifam_flags 0x%x, addr %s/%d broad %s dst %s "
601 "gateway %s",
Andrew J. Schorr55196042006-05-17 15:04:59 +0000602 __func__, ifm->ifam_index,
603 (ifnlen ? ifname : "(nil)"), ifm->ifam_addrs,
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000604 ifm->ifam_flags,
Andrew J. Schorr55196042006-05-17 15:04:59 +0000605 inet_ntop(AF_INET6,&addr->sin6.sin6_addr,
606 buf[0],sizeof(buf[0])),
607 ip6_masklen(mask->sin6.sin6_addr),
608 inet_ntop(AF_INET6,&brd->sin6.sin6_addr,
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000609 buf[1],sizeof(buf[1])),
610 inet_ntop(AF_INET6,&dst.sin6.sin6_addr,
611 buf[2],sizeof(buf[2])),
612 inet_ntop(AF_INET6,&gateway.sin6.sin6_addr,
613 buf[3],sizeof(buf[3])));
Andrew J. Schorr55196042006-05-17 15:04:59 +0000614 }
615 break;
616#endif /* HAVE_IPV6 */
617 default:
618 zlog_debug ("%s: ifindex %d, ifname %s, ifam_addrs 0x%x",
619 __func__, ifm->ifam_index,
620 (ifnlen ? ifname : "(nil)"), ifm->ifam_addrs);
621 break;
622 }
623 }
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000624
paul718e3742002-12-13 20:15:29 +0000625 /* Assert read up end point matches to end point */
626 if (pnt != end)
Denis Ovsienko85a2ebf2011-12-05 19:36:06 +0400627 zlog_warn ("ifam_read() doesn't read all socket data");
paul718e3742002-12-13 20:15:29 +0000628}
629
630/* Interface's address information get. */
paulec1a4282005-11-24 15:15:17 +0000631int
paul718e3742002-12-13 20:15:29 +0000632ifam_read (struct ifa_msghdr *ifam)
633{
paul6fe70d12005-11-12 22:55:10 +0000634 struct interface *ifp = NULL;
paul0752ef02005-11-03 12:35:21 +0000635 union sockunion addr, mask, brd;
paul6fe70d12005-11-12 22:55:10 +0000636 char ifname[INTERFACE_NAMSIZ];
637 short ifnlen = 0;
638 char isalias = 0;
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000639 int flags = 0;
paul6fe70d12005-11-12 22:55:10 +0000640
641 ifname[0] = ifname[INTERFACE_NAMSIZ - 1] = '\0';
642
643 /* Allocate and read address information. */
644 ifam_read_mesg (ifam, &addr, &mask, &brd, ifname, &ifnlen);
645
646 if ((ifp = if_lookup_by_index(ifam->ifam_index)) == NULL)
paul718e3742002-12-13 20:15:29 +0000647 {
paul6fe70d12005-11-12 22:55:10 +0000648 zlog_warn ("%s: no interface for ifname %s, index %d",
649 __func__, ifname, ifam->ifam_index);
paul718e3742002-12-13 20:15:29 +0000650 return -1;
651 }
paul6fe70d12005-11-12 22:55:10 +0000652
653 if (ifnlen && strncmp (ifp->name, ifname, INTERFACE_NAMSIZ))
654 isalias = 1;
655
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000656 /* N.B. The info in ifa_msghdr does not tell us whether the RTA_BRD
657 field contains a broadcast address or a peer address, so we are forced to
658 rely upon the interface type. */
659 if (if_is_pointopoint(ifp))
660 SET_FLAG(flags, ZEBRA_IFA_PEER);
661
Paul Jakma65022082007-03-06 13:43:05 +0000662#if 0
663 /* it might seem cute to grab the interface metric here, however
664 * we're processing an address update message, and so some systems
665 * (e.g. FBSD) dont bother to fill in ifam_metric. Disabled, but left
666 * in deliberately, as comment.
667 */
pauld34b8992006-01-17 18:03:04 +0000668 ifp->metric = ifam->ifam_metric;
Paul Jakma65022082007-03-06 13:43:05 +0000669#endif
670
paul718e3742002-12-13 20:15:29 +0000671 /* Add connected address. */
672 switch (sockunion_family (&addr))
673 {
674 case AF_INET:
675 if (ifam->ifam_type == RTM_NEWADDR)
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000676 connected_add_ipv4 (ifp, flags, &addr.sin.sin_addr,
paul718e3742002-12-13 20:15:29 +0000677 ip_masklen (mask.sin.sin_addr),
pauld34b8992006-01-17 18:03:04 +0000678 &brd.sin.sin_addr,
679 (isalias ? ifname : NULL));
paul718e3742002-12-13 20:15:29 +0000680 else
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000681 connected_delete_ipv4 (ifp, flags, &addr.sin.sin_addr,
paul718e3742002-12-13 20:15:29 +0000682 ip_masklen (mask.sin.sin_addr),
paul0752ef02005-11-03 12:35:21 +0000683 &brd.sin.sin_addr);
paul718e3742002-12-13 20:15:29 +0000684 break;
685#ifdef HAVE_IPV6
686 case AF_INET6:
687 /* Unset interface index from link-local address when IPv6 stack
688 is KAME. */
689 if (IN6_IS_ADDR_LINKLOCAL (&addr.sin6.sin6_addr))
690 SET_IN6_LINKLOCAL_IFINDEX (addr.sin6.sin6_addr, 0);
691
692 if (ifam->ifam_type == RTM_NEWADDR)
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000693 connected_add_ipv6 (ifp, flags, &addr.sin6.sin6_addr,
paul718e3742002-12-13 20:15:29 +0000694 ip6_masklen (mask.sin6.sin6_addr),
pauld34b8992006-01-17 18:03:04 +0000695 &brd.sin6.sin6_addr,
696 (isalias ? ifname : NULL));
paul718e3742002-12-13 20:15:29 +0000697 else
698 connected_delete_ipv6 (ifp,
699 &addr.sin6.sin6_addr,
700 ip6_masklen (mask.sin6.sin6_addr),
paul0752ef02005-11-03 12:35:21 +0000701 &brd.sin6.sin6_addr);
paul718e3742002-12-13 20:15:29 +0000702 break;
703#endif /* HAVE_IPV6 */
704 default:
705 /* Unsupported family silently ignore... */
706 break;
707 }
paul5c78b3d2006-01-25 04:31:40 +0000708
709 /* Check interface flag for implicit up of the interface. */
710 if_refresh (ifp);
711
712#ifdef SUNOS_5
713 /* In addition to lacking IFANNOUNCE, on SUNOS IFF_UP is strange.
714 * See comments for SUNOS_5 in interface.c::if_flags_mangle.
715 *
716 * Here we take care of case where the real IFF_UP was previously
717 * unset (as kept in struct zebra_if.primary_state) and the mangled
718 * IFF_UP (ie IFF_UP set || listcount(connected) has now transitioned
719 * to unset due to the lost non-primary address having DELADDR'd.
720 *
721 * we must delete the interface, because in between here and next
722 * event for this interface-name the administrator could unplumb
723 * and replumb the interface.
724 */
725 if (!if_is_up (ifp))
726 if_delete_update (ifp);
727#endif /* SUNOS_5 */
728
paul718e3742002-12-13 20:15:29 +0000729 return 0;
730}
731
732/* Interface function for reading kernel routing table information. */
paul6621ca82005-11-23 13:02:08 +0000733static int
paul718e3742002-12-13 20:15:29 +0000734rtm_read_mesg (struct rt_msghdr *rtm,
735 union sockunion *dest,
736 union sockunion *mask,
paul6fe70d12005-11-12 22:55:10 +0000737 union sockunion *gate,
738 char *ifname,
739 short *ifnlen)
paul718e3742002-12-13 20:15:29 +0000740{
741 caddr_t pnt, end;
742
743 /* Pnt points out socket data start point. */
744 pnt = (caddr_t)(rtm + 1);
745 end = ((caddr_t)rtm) + rtm->rtm_msglen;
746
747 /* rt_msghdr version check. */
748 if (rtm->rtm_version != RTM_VERSION)
749 zlog (NULL, LOG_WARNING,
750 "Routing message version different %d should be %d."
751 "This may cause problem\n", rtm->rtm_version, RTM_VERSION);
paul62debbb2005-06-14 14:07:07 +0000752
paul718e3742002-12-13 20:15:29 +0000753 /* Be sure structure is cleared */
754 memset (dest, 0, sizeof (union sockunion));
755 memset (gate, 0, sizeof (union sockunion));
756 memset (mask, 0, sizeof (union sockunion));
757
758 /* We fetch each socket variable into sockunion. */
paul62debbb2005-06-14 14:07:07 +0000759 RTA_ADDR_GET (dest, RTA_DST, rtm->rtm_addrs, pnt);
760 RTA_ADDR_GET (gate, RTA_GATEWAY, rtm->rtm_addrs, pnt);
761 RTA_ATTR_GET (mask, RTA_NETMASK, rtm->rtm_addrs, pnt);
762 RTA_ADDR_GET (NULL, RTA_GENMASK, rtm->rtm_addrs, pnt);
paul6fe70d12005-11-12 22:55:10 +0000763 RTA_NAME_GET (ifname, RTA_IFP, rtm->rtm_addrs, pnt, *ifnlen);
paul62debbb2005-06-14 14:07:07 +0000764 RTA_ADDR_GET (NULL, RTA_IFA, rtm->rtm_addrs, pnt);
765 RTA_ADDR_GET (NULL, RTA_AUTHOR, rtm->rtm_addrs, pnt);
766 RTA_ADDR_GET (NULL, RTA_BRD, rtm->rtm_addrs, pnt);
paul718e3742002-12-13 20:15:29 +0000767
768 /* If there is netmask information set it's family same as
769 destination family*/
770 if (rtm->rtm_addrs & RTA_NETMASK)
771 mask->sa.sa_family = dest->sa.sa_family;
772
773 /* Assert read up to the end of pointer. */
774 if (pnt != end)
Denis Ovsienko85a2ebf2011-12-05 19:36:06 +0400775 zlog (NULL, LOG_WARNING, "rtm_read() doesn't read all socket data.");
paul718e3742002-12-13 20:15:29 +0000776
777 return rtm->rtm_flags;
778}
779
paulec1a4282005-11-24 15:15:17 +0000780void
paul718e3742002-12-13 20:15:29 +0000781rtm_read (struct rt_msghdr *rtm)
782{
783 int flags;
784 u_char zebra_flags;
785 union sockunion dest, mask, gate;
paul6fe70d12005-11-12 22:55:10 +0000786 char ifname[INTERFACE_NAMSIZ + 1];
787 short ifnlen = 0;
paul718e3742002-12-13 20:15:29 +0000788
789 zebra_flags = 0;
790
paul718e3742002-12-13 20:15:29 +0000791 /* Read destination and netmask and gateway from rtm message
792 structure. */
paul6fe70d12005-11-12 22:55:10 +0000793 flags = rtm_read_mesg (rtm, &dest, &mask, &gate, ifname, &ifnlen);
Denis Ovsienko6da59802007-08-17 14:16:30 +0000794 if (!(flags & RTF_DONE))
795 return;
Denis Ovsienkodc958242007-08-13 16:03:06 +0000796 if (IS_ZEBRA_DEBUG_KERNEL)
797 zlog_debug ("%s: got rtm of type %d (%s)", __func__, rtm->rtm_type,
Denis Ovsienko2d844522007-09-14 11:31:55 +0000798 lookup (rtm_type_str, rtm->rtm_type));
paul718e3742002-12-13 20:15:29 +0000799
800#ifdef RTF_CLONED /*bsdi, netbsd 1.6*/
801 if (flags & RTF_CLONED)
802 return;
803#endif
804#ifdef RTF_WASCLONED /*freebsd*/
805 if (flags & RTF_WASCLONED)
806 return;
807#endif
808
809 if ((rtm->rtm_type == RTM_ADD) && ! (flags & RTF_UP))
810 return;
811
812 /* This is connected route. */
813 if (! (flags & RTF_GATEWAY))
814 return;
815
816 if (flags & RTF_PROTO1)
817 SET_FLAG (zebra_flags, ZEBRA_FLAG_SELFROUTE);
818
819 /* This is persistent route. */
820 if (flags & RTF_STATIC)
821 SET_FLAG (zebra_flags, ZEBRA_FLAG_STATIC);
822
hasso81dfcaa2003-05-25 19:21:25 +0000823 /* This is a reject or blackhole route */
824 if (flags & RTF_REJECT)
825 SET_FLAG (zebra_flags, ZEBRA_FLAG_REJECT);
826 if (flags & RTF_BLACKHOLE)
827 SET_FLAG (zebra_flags, ZEBRA_FLAG_BLACKHOLE);
828
paul718e3742002-12-13 20:15:29 +0000829 if (dest.sa.sa_family == AF_INET)
830 {
831 struct prefix_ipv4 p;
832
833 p.family = AF_INET;
834 p.prefix = dest.sin.sin_addr;
835 if (flags & RTF_HOST)
836 p.prefixlen = IPV4_MAX_PREFIXLEN;
837 else
838 p.prefixlen = ip_masklen (mask.sin.sin_addr);
paulca162182005-09-12 16:58:52 +0000839
Denis Ovsienkodc958242007-08-13 16:03:06 +0000840 /* Catch self originated messages and match them against our current RIB.
841 * At the same time, ignore unconfirmed messages, they should be tracked
842 * by rtm_write() and kernel_rtm_ipv4().
843 */
Denis Ovsienko96934e62007-09-14 14:56:28 +0000844 if (rtm->rtm_type != RTM_GET && rtm->rtm_pid == pid)
Denis Ovsienkodc958242007-08-13 16:03:06 +0000845 {
846 char buf[INET_ADDRSTRLEN], gate_buf[INET_ADDRSTRLEN];
847 int ret;
Denis Ovsienkodc958242007-08-13 16:03:06 +0000848 if (! IS_ZEBRA_DEBUG_RIB)
849 return;
850 ret = rib_lookup_ipv4_route (&p, &gate);
851 inet_ntop (AF_INET, &p.prefix, buf, INET_ADDRSTRLEN);
852 switch (rtm->rtm_type)
853 {
854 case RTM_ADD:
855 case RTM_GET:
856 case RTM_CHANGE:
857 /* The kernel notifies us about a new route in FIB created by us.
858 Do we have a correspondent entry in our RIB? */
859 switch (ret)
860 {
861 case ZEBRA_RIB_NOTFOUND:
862 zlog_debug ("%s: %s %s/%d: desync: RR isn't yet in RIB, while already in FIB",
Denis Ovsienko2d844522007-09-14 11:31:55 +0000863 __func__, lookup (rtm_type_str, rtm->rtm_type), buf, p.prefixlen);
Denis Ovsienkodc958242007-08-13 16:03:06 +0000864 break;
865 case ZEBRA_RIB_FOUND_CONNECTED:
866 case ZEBRA_RIB_FOUND_NOGATE:
867 inet_ntop (AF_INET, &gate.sin.sin_addr, gate_buf, INET_ADDRSTRLEN);
868 zlog_debug ("%s: %s %s/%d: desync: RR is in RIB, but gate differs (ours is %s)",
Denis Ovsienko2d844522007-09-14 11:31:55 +0000869 __func__, lookup (rtm_type_str, rtm->rtm_type), buf, p.prefixlen, gate_buf);
Denis Ovsienkodc958242007-08-13 16:03:06 +0000870 break;
871 case ZEBRA_RIB_FOUND_EXACT: /* RIB RR == FIB RR */
872 zlog_debug ("%s: %s %s/%d: done Ok",
Denis Ovsienko2d844522007-09-14 11:31:55 +0000873 __func__, lookup (rtm_type_str, rtm->rtm_type), buf, p.prefixlen);
Denis Ovsienkodc958242007-08-13 16:03:06 +0000874 rib_lookup_and_dump (&p);
875 return;
876 break;
877 }
878 break;
879 case RTM_DELETE:
880 /* The kernel notifies us about a route deleted by us. Do we still
881 have it in the RIB? Do we have anything instead? */
882 switch (ret)
883 {
884 case ZEBRA_RIB_FOUND_EXACT:
885 zlog_debug ("%s: %s %s/%d: desync: RR is still in RIB, while already not in FIB",
Denis Ovsienko2d844522007-09-14 11:31:55 +0000886 __func__, lookup (rtm_type_str, rtm->rtm_type), buf, p.prefixlen);
Denis Ovsienkodc958242007-08-13 16:03:06 +0000887 rib_lookup_and_dump (&p);
888 break;
889 case ZEBRA_RIB_FOUND_CONNECTED:
890 case ZEBRA_RIB_FOUND_NOGATE:
891 zlog_debug ("%s: %s %s/%d: desync: RR is still in RIB, plus gate differs",
Denis Ovsienko2d844522007-09-14 11:31:55 +0000892 __func__, lookup (rtm_type_str, rtm->rtm_type), buf, p.prefixlen);
Denis Ovsienkodc958242007-08-13 16:03:06 +0000893 rib_lookup_and_dump (&p);
894 break;
895 case ZEBRA_RIB_NOTFOUND: /* RIB RR == FIB RR */
896 zlog_debug ("%s: %s %s/%d: done Ok",
Denis Ovsienko2d844522007-09-14 11:31:55 +0000897 __func__, lookup (rtm_type_str, rtm->rtm_type), buf, p.prefixlen);
Denis Ovsienkodc958242007-08-13 16:03:06 +0000898 rib_lookup_and_dump (&p);
899 return;
900 break;
901 }
902 break;
903 default:
904 zlog_debug ("%s: %s/%d: warning: loopback RTM of type %s received",
Denis Ovsienko2d844522007-09-14 11:31:55 +0000905 __func__, buf, p.prefixlen, lookup (rtm_type_str, rtm->rtm_type));
Denis Ovsienkodc958242007-08-13 16:03:06 +0000906 }
907 return;
908 }
909
paulca162182005-09-12 16:58:52 +0000910 /* Change, delete the old prefix, we have no further information
911 * to specify the route really
912 */
913 if (rtm->rtm_type == RTM_CHANGE)
914 rib_delete_ipv4 (ZEBRA_ROUTE_KERNEL, zebra_flags, &p,
Denis Ovsienko6eac79a2011-12-05 13:43:18 +0400915 NULL, 0, 0, SAFI_UNICAST);
paulca162182005-09-12 16:58:52 +0000916
917 if (rtm->rtm_type == RTM_GET
918 || rtm->rtm_type == RTM_ADD
919 || rtm->rtm_type == RTM_CHANGE)
paul718e3742002-12-13 20:15:29 +0000920 rib_add_ipv4 (ZEBRA_ROUTE_KERNEL, zebra_flags,
G.Balajicddf3912011-11-26 21:59:32 +0400921 &p, &gate.sin.sin_addr, NULL, 0, 0, 0, 0, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +0000922 else
923 rib_delete_ipv4 (ZEBRA_ROUTE_KERNEL, zebra_flags,
Denis Ovsienko6eac79a2011-12-05 13:43:18 +0400924 &p, &gate.sin.sin_addr, 0, 0, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +0000925 }
926#ifdef HAVE_IPV6
927 if (dest.sa.sa_family == AF_INET6)
928 {
Denis Ovsienko5619f562007-10-24 13:13:21 +0000929 /* One day we might have a debug section here like one in the
930 * IPv4 case above. Just ignore own messages at the moment.
931 */
932 if (rtm->rtm_type != RTM_GET && rtm->rtm_pid == pid)
933 return;
paul718e3742002-12-13 20:15:29 +0000934 struct prefix_ipv6 p;
935 unsigned int ifindex = 0;
936
937 p.family = AF_INET6;
938 p.prefix = dest.sin6.sin6_addr;
939 if (flags & RTF_HOST)
940 p.prefixlen = IPV6_MAX_PREFIXLEN;
941 else
942 p.prefixlen = ip6_masklen (mask.sin6.sin6_addr);
943
944#ifdef KAME
945 if (IN6_IS_ADDR_LINKLOCAL (&gate.sin6.sin6_addr))
946 {
947 ifindex = IN6_LINKLOCAL_IFINDEX (gate.sin6.sin6_addr);
948 SET_IN6_LINKLOCAL_IFINDEX (gate.sin6.sin6_addr, 0);
949 }
950#endif /* KAME */
951
paulca162182005-09-12 16:58:52 +0000952 /* CHANGE: delete the old prefix, we have no further information
953 * to specify the route really
954 */
955 if (rtm->rtm_type == RTM_CHANGE)
paul6621ca82005-11-23 13:02:08 +0000956 rib_delete_ipv6 (ZEBRA_ROUTE_KERNEL, zebra_flags, &p,
Denis Ovsienko6eac79a2011-12-05 13:43:18 +0400957 NULL, 0, 0, SAFI_UNICAST);
paulca162182005-09-12 16:58:52 +0000958
959 if (rtm->rtm_type == RTM_GET
960 || rtm->rtm_type == RTM_ADD
961 || rtm->rtm_type == RTM_CHANGE)
paul718e3742002-12-13 20:15:29 +0000962 rib_add_ipv6 (ZEBRA_ROUTE_KERNEL, zebra_flags,
Denis Ovsienko6eac79a2011-12-05 13:43:18 +0400963 &p, &gate.sin6.sin6_addr, ifindex, 0, 0, 0, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +0000964 else
965 rib_delete_ipv6 (ZEBRA_ROUTE_KERNEL, zebra_flags,
Denis Ovsienko6eac79a2011-12-05 13:43:18 +0400966 &p, &gate.sin6.sin6_addr, ifindex, 0, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +0000967 }
968#endif /* HAVE_IPV6 */
969}
970
971/* Interface function for the kernel routing table updates. Support
paul6621ca82005-11-23 13:02:08 +0000972 * for RTM_CHANGE will be needed.
973 * Exported only for rt_socket.c
974 */
paul718e3742002-12-13 20:15:29 +0000975int
976rtm_write (int message,
977 union sockunion *dest,
978 union sockunion *mask,
979 union sockunion *gate,
980 unsigned int index,
981 int zebra_flags,
982 int metric)
983{
984 int ret;
985 caddr_t pnt;
986 struct interface *ifp;
paul718e3742002-12-13 20:15:29 +0000987
988 /* Sequencial number of routing message. */
989 static int msg_seq = 0;
990
991 /* Struct of rt_msghdr and buffer for storing socket's data. */
992 struct
993 {
994 struct rt_msghdr rtm;
995 char buf[512];
996 } msg;
997
paul718e3742002-12-13 20:15:29 +0000998 if (routing_sock < 0)
999 return ZEBRA_ERR_EPERM;
1000
1001 /* Clear and set rt_msghdr values */
1002 memset (&msg, 0, sizeof (struct rt_msghdr));
1003 msg.rtm.rtm_version = RTM_VERSION;
1004 msg.rtm.rtm_type = message;
1005 msg.rtm.rtm_seq = msg_seq++;
1006 msg.rtm.rtm_addrs = RTA_DST;
1007 msg.rtm.rtm_addrs |= RTA_GATEWAY;
1008 msg.rtm.rtm_flags = RTF_UP;
1009 msg.rtm.rtm_index = index;
1010
1011 if (metric != 0)
1012 {
1013 msg.rtm.rtm_rmx.rmx_hopcount = metric;
1014 msg.rtm.rtm_inits |= RTV_HOPCOUNT;
1015 }
1016
1017 ifp = if_lookup_by_index (index);
1018
1019 if (gate && message == RTM_ADD)
1020 msg.rtm.rtm_flags |= RTF_GATEWAY;
1021
David Warde6f148e2009-12-03 21:43:11 +03001022 /* When RTF_CLONING is unavailable on BSD, should we set some
1023 * other flag instead?
1024 */
1025#ifdef RTF_CLONING
paul718e3742002-12-13 20:15:29 +00001026 if (! gate && message == RTM_ADD && ifp &&
1027 (ifp->flags & IFF_POINTOPOINT) == 0)
1028 msg.rtm.rtm_flags |= RTF_CLONING;
David Warde6f148e2009-12-03 21:43:11 +03001029#endif /* RTF_CLONING */
paul718e3742002-12-13 20:15:29 +00001030
1031 /* If no protocol specific gateway is specified, use link
1032 address for gateway. */
1033 if (! gate)
1034 {
1035 if (!ifp)
1036 {
Denis Ovsienkodc958242007-08-13 16:03:06 +00001037 char dest_buf[INET_ADDRSTRLEN] = "NULL", mask_buf[INET_ADDRSTRLEN] = "255.255.255.255";
1038 if (dest)
1039 inet_ntop (AF_INET, &dest->sin.sin_addr, dest_buf, INET_ADDRSTRLEN);
1040 if (mask)
1041 inet_ntop (AF_INET, &mask->sin.sin_addr, mask_buf, INET_ADDRSTRLEN);
1042 zlog_warn ("%s: %s/%s: gate == NULL and no gateway found for ifindex %d",
1043 __func__, dest_buf, mask_buf, index);
paul718e3742002-12-13 20:15:29 +00001044 return -1;
1045 }
1046 gate = (union sockunion *) & ifp->sdl;
1047 }
1048
1049 if (mask)
1050 msg.rtm.rtm_addrs |= RTA_NETMASK;
1051 else if (message == RTM_ADD)
1052 msg.rtm.rtm_flags |= RTF_HOST;
1053
1054 /* Tagging route with flags */
1055 msg.rtm.rtm_flags |= (RTF_PROTO1);
1056
1057 /* Additional flags. */
1058 if (zebra_flags & ZEBRA_FLAG_BLACKHOLE)
1059 msg.rtm.rtm_flags |= RTF_BLACKHOLE;
hasso81dfcaa2003-05-25 19:21:25 +00001060 if (zebra_flags & ZEBRA_FLAG_REJECT)
1061 msg.rtm.rtm_flags |= RTF_REJECT;
1062
paul718e3742002-12-13 20:15:29 +00001063
Paul Jakma6f0e3f62007-05-10 02:38:51 +00001064#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
paul718e3742002-12-13 20:15:29 +00001065#define SOCKADDRSET(X,R) \
1066 if (msg.rtm.rtm_addrs & (R)) \
1067 { \
1068 int len = ROUNDUP ((X)->sa.sa_len); \
1069 memcpy (pnt, (caddr_t)(X), len); \
1070 pnt += len; \
1071 }
1072#else
1073#define SOCKADDRSET(X,R) \
1074 if (msg.rtm.rtm_addrs & (R)) \
1075 { \
paul6fe70d12005-11-12 22:55:10 +00001076 int len = SAROUNDUP (X); \
paul718e3742002-12-13 20:15:29 +00001077 memcpy (pnt, (caddr_t)(X), len); \
1078 pnt += len; \
1079 }
Paul Jakma6f0e3f62007-05-10 02:38:51 +00001080#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
paul718e3742002-12-13 20:15:29 +00001081
1082 pnt = (caddr_t) msg.buf;
1083
1084 /* Write each socket data into rtm message buffer */
1085 SOCKADDRSET (dest, RTA_DST);
1086 SOCKADDRSET (gate, RTA_GATEWAY);
1087 SOCKADDRSET (mask, RTA_NETMASK);
1088
1089 msg.rtm.rtm_msglen = pnt - (caddr_t) &msg;
1090
1091 ret = write (routing_sock, &msg, msg.rtm.rtm_msglen);
1092
1093 if (ret != msg.rtm.rtm_msglen)
1094 {
1095 if (errno == EEXIST)
1096 return ZEBRA_ERR_RTEXIST;
1097 if (errno == ENETUNREACH)
1098 return ZEBRA_ERR_RTUNREACH;
Denis Ovsienkodc958242007-08-13 16:03:06 +00001099 if (errno == ESRCH)
1100 return ZEBRA_ERR_RTNOEXIST;
paul718e3742002-12-13 20:15:29 +00001101
Denis Ovsienkodc958242007-08-13 16:03:06 +00001102 zlog_warn ("%s: write : %s (%d)", __func__, safe_strerror (errno), errno);
1103 return ZEBRA_ERR_KERNEL;
paul718e3742002-12-13 20:15:29 +00001104 }
Denis Ovsienkodc958242007-08-13 16:03:06 +00001105 return ZEBRA_ERR_NOERROR;
paul718e3742002-12-13 20:15:29 +00001106}
1107
1108
1109#include "thread.h"
1110#include "zebra/zserv.h"
1111
paul718e3742002-12-13 20:15:29 +00001112/* For debug purpose. */
ajsb6178002004-12-07 21:12:56 +00001113static void
paul718e3742002-12-13 20:15:29 +00001114rtmsg_debug (struct rt_msghdr *rtm)
1115{
Denis Ovsienko2d844522007-09-14 11:31:55 +00001116 zlog_debug ("Kernel: Len: %d Type: %s", rtm->rtm_msglen, lookup (rtm_type_str, rtm->rtm_type));
paul718e3742002-12-13 20:15:29 +00001117 rtm_flag_dump (rtm->rtm_flags);
ajsb6178002004-12-07 21:12:56 +00001118 zlog_debug ("Kernel: message seq %d", rtm->rtm_seq);
paul6fe70d12005-11-12 22:55:10 +00001119 zlog_debug ("Kernel: pid %d, rtm_addrs 0x%x", rtm->rtm_pid, rtm->rtm_addrs);
paul718e3742002-12-13 20:15:29 +00001120}
1121
1122/* This is pretty gross, better suggestions welcome -- mhandler */
1123#ifndef RTAX_MAX
1124#ifdef RTA_NUMBITS
1125#define RTAX_MAX RTA_NUMBITS
1126#else
1127#define RTAX_MAX 8
1128#endif /* RTA_NUMBITS */
1129#endif /* RTAX_MAX */
1130
1131/* Kernel routing table and interface updates via routing socket. */
paul6621ca82005-11-23 13:02:08 +00001132static int
paul718e3742002-12-13 20:15:29 +00001133kernel_read (struct thread *thread)
1134{
1135 int sock;
1136 int nbytes;
1137 struct rt_msghdr *rtm;
1138
gdtdbee01f2004-01-06 00:36:51 +00001139 /*
1140 * This must be big enough for any message the kernel might send.
gdtb27900b2004-01-08 15:44:29 +00001141 * Rather than determining how many sockaddrs of what size might be
1142 * in each particular message, just use RTAX_MAX of sockaddr_storage
1143 * for each. Note that the sockaddrs must be after each message
1144 * definition, or rather after whichever happens to be the largest,
1145 * since the buffer needs to be big enough for a message and the
1146 * sockaddrs together.
gdtdbee01f2004-01-06 00:36:51 +00001147 */
paul718e3742002-12-13 20:15:29 +00001148 union
1149 {
1150 /* Routing information. */
1151 struct
1152 {
1153 struct rt_msghdr rtm;
gdtb27900b2004-01-08 15:44:29 +00001154 struct sockaddr_storage addr[RTAX_MAX];
paul718e3742002-12-13 20:15:29 +00001155 } r;
1156
1157 /* Interface information. */
1158 struct
1159 {
1160 struct if_msghdr ifm;
gdtb27900b2004-01-08 15:44:29 +00001161 struct sockaddr_storage addr[RTAX_MAX];
paul718e3742002-12-13 20:15:29 +00001162 } im;
1163
1164 /* Interface address information. */
1165 struct
1166 {
1167 struct ifa_msghdr ifa;
gdtb27900b2004-01-08 15:44:29 +00001168 struct sockaddr_storage addr[RTAX_MAX];
paul718e3742002-12-13 20:15:29 +00001169 } ia;
1170
1171#ifdef RTM_IFANNOUNCE
1172 /* Interface arrival/departure */
1173 struct
1174 {
1175 struct if_announcemsghdr ifan;
gdtb27900b2004-01-08 15:44:29 +00001176 struct sockaddr_storage addr[RTAX_MAX];
paul718e3742002-12-13 20:15:29 +00001177 } ian;
1178#endif /* RTM_IFANNOUNCE */
1179
1180 } buf;
1181
1182 /* Fetch routing socket. */
1183 sock = THREAD_FD (thread);
1184
1185 nbytes= read (sock, &buf, sizeof buf);
1186
1187 if (nbytes <= 0)
1188 {
1189 if (nbytes < 0 && errno != EWOULDBLOCK && errno != EAGAIN)
ajs6099b3b2004-11-20 02:06:59 +00001190 zlog_warn ("routing socket error: %s", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001191 return 0;
1192 }
1193
paul9bcdb632003-07-08 08:09:45 +00001194 thread_add_read (zebrad.master, kernel_read, NULL, sock);
paul718e3742002-12-13 20:15:29 +00001195
hasso726f9b22003-05-25 21:04:54 +00001196 if (IS_ZEBRA_DEBUG_KERNEL)
1197 rtmsg_debug (&buf.r.rtm);
paul718e3742002-12-13 20:15:29 +00001198
1199 rtm = &buf.r.rtm;
1200
gdtb27900b2004-01-08 15:44:29 +00001201 /*
1202 * Ensure that we didn't drop any data, so that processing routines
1203 * can assume they have the whole message.
1204 */
gdtda26e3b2004-01-05 17:20:59 +00001205 if (rtm->rtm_msglen != nbytes)
1206 {
1207 zlog_warn ("kernel_read: rtm->rtm_msglen %d, nbytes %d, type %d\n",
1208 rtm->rtm_msglen, nbytes, rtm->rtm_type);
1209 return -1;
1210 }
1211
paul718e3742002-12-13 20:15:29 +00001212 switch (rtm->rtm_type)
1213 {
1214 case RTM_ADD:
1215 case RTM_DELETE:
paulca162182005-09-12 16:58:52 +00001216 case RTM_CHANGE:
paul718e3742002-12-13 20:15:29 +00001217 rtm_read (rtm);
1218 break;
1219 case RTM_IFINFO:
1220 ifm_read (&buf.im.ifm);
1221 break;
1222 case RTM_NEWADDR:
1223 case RTM_DELADDR:
1224 ifam_read (&buf.ia.ifa);
1225 break;
1226#ifdef RTM_IFANNOUNCE
1227 case RTM_IFANNOUNCE:
1228 ifan_read (&buf.ian.ifan);
1229 break;
1230#endif /* RTM_IFANNOUNCE */
1231 default:
hasso726f9b22003-05-25 21:04:54 +00001232 if (IS_ZEBRA_DEBUG_KERNEL)
ajsb6178002004-12-07 21:12:56 +00001233 zlog_debug("Unprocessed RTM_type: %d", rtm->rtm_type);
paul718e3742002-12-13 20:15:29 +00001234 break;
1235 }
1236 return 0;
1237}
1238
1239/* Make routing socket. */
paul6621ca82005-11-23 13:02:08 +00001240static void
1241routing_socket (void)
paul718e3742002-12-13 20:15:29 +00001242{
pauledd7c242003-06-04 13:59:38 +00001243 if ( zserv_privs.change (ZPRIVS_RAISE) )
1244 zlog_err ("routing_socket: Can't raise privileges");
1245
paul718e3742002-12-13 20:15:29 +00001246 routing_sock = socket (AF_ROUTE, SOCK_RAW, 0);
1247
1248 if (routing_sock < 0)
1249 {
pauledd7c242003-06-04 13:59:38 +00001250 if ( zserv_privs.change (ZPRIVS_LOWER) )
1251 zlog_err ("routing_socket: Can't lower privileges");
paul718e3742002-12-13 20:15:29 +00001252 zlog_warn ("Can't init kernel routing socket");
1253 return;
1254 }
1255
paul865b8522005-01-05 08:30:35 +00001256 /* XXX: Socket should be NONBLOCK, however as we currently
1257 * discard failed writes, this will lead to inconsistencies.
1258 * For now, socket must be blocking.
1259 */
1260 /*if (fcntl (routing_sock, F_SETFL, O_NONBLOCK) < 0)
1261 zlog_warn ("Can't set O_NONBLOCK to routing socket");*/
1262
pauledd7c242003-06-04 13:59:38 +00001263 if ( zserv_privs.change (ZPRIVS_LOWER) )
1264 zlog_err ("routing_socket: Can't lower privileges");
paul718e3742002-12-13 20:15:29 +00001265
1266 /* kernel_read needs rewrite. */
paul9bcdb632003-07-08 08:09:45 +00001267 thread_add_read (zebrad.master, kernel_read, NULL, routing_sock);
paul718e3742002-12-13 20:15:29 +00001268}
1269
1270/* Exported interface function. This function simply calls
1271 routing_socket (). */
1272void
paul6621ca82005-11-23 13:02:08 +00001273kernel_init (void)
paul718e3742002-12-13 20:15:29 +00001274{
1275 routing_socket ();
1276}