blob: a91d76f593430ab36f42fc7b6c7e9c2511facaf5 [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.
51 */
paul718e3742002-12-13 20:15:29 +000052#define ROUNDUP(a) \
53 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
54
gdt4bfbea82004-01-06 01:13:05 +000055/*
56 * Given a pointer (sockaddr or void *), return the number of bytes
57 * taken up by the sockaddr and any padding needed for alignment.
58 */
Paul Jakma6f0e3f62007-05-10 02:38:51 +000059#if defined(HAVE_STRUCT_SOCKADDR_SA_LEN)
gdt4bfbea82004-01-06 01:13:05 +000060#define SAROUNDUP(X) ROUNDUP(((struct sockaddr *)(X))->sa_len)
paul30be8022003-10-22 02:51:38 +000061#elif defined(HAVE_IPV6)
gdt4bfbea82004-01-06 01:13:05 +000062/*
63 * One would hope all fixed-size structure definitions are aligned,
64 * but round them up nonetheless.
65 */
66#define SAROUNDUP(X) \
paul3e95a072003-09-24 00:05:45 +000067 (((struct sockaddr *)(X))->sa_family == AF_INET ? \
68 ROUNDUP(sizeof(struct sockaddr_in)):\
69 (((struct sockaddr *)(X))->sa_family == AF_INET6 ? \
70 ROUNDUP(sizeof(struct sockaddr_in6)) : \
71 (((struct sockaddr *)(X))->sa_family == AF_LINK ? \
paulc50ae8b2004-05-11 11:31:07 +000072 ROUNDUP(sizeof(struct sockaddr_dl)) : sizeof(struct sockaddr))))
paul30be8022003-10-22 02:51:38 +000073#else /* HAVE_IPV6 */
gdt4bfbea82004-01-06 01:13:05 +000074#define SAROUNDUP(X) \
paul30be8022003-10-22 02:51:38 +000075 (((struct sockaddr *)(X))->sa_family == AF_INET ? \
76 ROUNDUP(sizeof(struct sockaddr_in)):\
77 (((struct sockaddr *)(X))->sa_family == AF_LINK ? \
78 ROUNDUP(sizeof(struct sockaddr_dl)) : sizeof(struct sockaddr)))
Paul Jakma6f0e3f62007-05-10 02:38:51 +000079#endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
paul718e3742002-12-13 20:15:29 +000080
paulec1a4282005-11-24 15:15:17 +000081/* We use an additional pointer in following, pdest, rather than (DEST)
82 * directly, because gcc will warn if the macro is expanded and DEST is NULL,
83 * complaining that memcpy is being passed a NULL value, despite the fact
84 * the if (NULL) makes it impossible.
85 */
paul62debbb2005-06-14 14:07:07 +000086#define RTA_ADDR_GET(DEST, RTA, RTMADDRS, PNT) \
87 if ((RTMADDRS) & (RTA)) \
88 { \
paulec1a4282005-11-24 15:15:17 +000089 void *pdest = (DEST); \
paul62debbb2005-06-14 14:07:07 +000090 int len = SAROUNDUP ((PNT)); \
paulea6f82b2005-06-28 17:20:26 +000091 if ( ((DEST) != NULL) && \
paul62debbb2005-06-14 14:07:07 +000092 af_check (((struct sockaddr *)(PNT))->sa_family)) \
paulec1a4282005-11-24 15:15:17 +000093 memcpy (pdest, (PNT), len); \
paul62debbb2005-06-14 14:07:07 +000094 (PNT) += len; \
95 }
96#define RTA_ATTR_GET(DEST, RTA, RTMADDRS, PNT) \
97 if ((RTMADDRS) & (RTA)) \
98 { \
paulec1a4282005-11-24 15:15:17 +000099 void *pdest = (DEST); \
paul62debbb2005-06-14 14:07:07 +0000100 int len = SAROUNDUP ((PNT)); \
paulec1a4282005-11-24 15:15:17 +0000101 if ((DEST) != NULL) \
102 memcpy (pdest, (PNT), len); \
paul62debbb2005-06-14 14:07:07 +0000103 (PNT) += len; \
104 }
105
paul6fe70d12005-11-12 22:55:10 +0000106#define RTA_NAME_GET(DEST, RTA, RTMADDRS, PNT, LEN) \
107 if ((RTMADDRS) & (RTA)) \
108 { \
paulec1a4282005-11-24 15:15:17 +0000109 u_char *pdest = (u_char *) (DEST); \
paul6fe70d12005-11-12 22:55:10 +0000110 int len = SAROUNDUP ((PNT)); \
111 struct sockaddr_dl *sdl = (struct sockaddr_dl *)(PNT); \
112 if (IS_ZEBRA_DEBUG_KERNEL) \
113 zlog_debug ("%s: RTA_SDL_GET nlen %d, alen %d", \
114 __func__, sdl->sdl_nlen, sdl->sdl_alen); \
115 if ( ((DEST) != NULL) && (sdl->sdl_family == AF_LINK) \
116 && (sdl->sdl_nlen < IFNAMSIZ) && (sdl->sdl_nlen <= len) ) \
117 { \
paulec1a4282005-11-24 15:15:17 +0000118 memcpy (pdest, sdl->sdl_data, sdl->sdl_nlen); \
119 pdest[sdl->sdl_nlen] = '\0'; \
paul6fe70d12005-11-12 22:55:10 +0000120 (LEN) = sdl->sdl_nlen; \
121 } \
122 (PNT) += len; \
123 } \
124 else \
125 { \
126 (LEN) = 0; \
127 }
paul718e3742002-12-13 20:15:29 +0000128/* Routing socket message types. */
129struct message rtm_type_str[] =
130{
131 {RTM_ADD, "RTM_ADD"},
132 {RTM_DELETE, "RTM_DELETE"},
133 {RTM_CHANGE, "RTM_CHANGE"},
134 {RTM_GET, "RTM_GET"},
135 {RTM_LOSING, "RTM_LOSING"},
136 {RTM_REDIRECT, "RTM_REDIRECT"},
137 {RTM_MISS, "RTM_MISS"},
138 {RTM_LOCK, "RTM_LOCK"},
Greg Troxel9458b812006-09-13 12:13:08 +0000139#ifdef OLDADD
paul718e3742002-12-13 20:15:29 +0000140 {RTM_OLDADD, "RTM_OLDADD"},
Greg Troxel9458b812006-09-13 12:13:08 +0000141#endif /* RTM_OLDADD */
142#ifdef RTM_OLDDEL
paul718e3742002-12-13 20:15:29 +0000143 {RTM_OLDDEL, "RTM_OLDDEL"},
Greg Troxel9458b812006-09-13 12:13:08 +0000144#endif /* RTM_OLDDEL */
paul718e3742002-12-13 20:15:29 +0000145 {RTM_RESOLVE, "RTM_RESOLVE"},
146 {RTM_NEWADDR, "RTM_NEWADDR"},
147 {RTM_DELADDR, "RTM_DELADDR"},
148 {RTM_IFINFO, "RTM_IFINFO"},
149#ifdef RTM_OIFINFO
150 {RTM_OIFINFO, "RTM_OIFINFO"},
151#endif /* RTM_OIFINFO */
152#ifdef RTM_NEWMADDR
153 {RTM_NEWMADDR, "RTM_NEWMADDR"},
154#endif /* RTM_NEWMADDR */
155#ifdef RTM_DELMADDR
156 {RTM_DELMADDR, "RTM_DELMADDR"},
157#endif /* RTM_DELMADDR */
158#ifdef RTM_IFANNOUNCE
159 {RTM_IFANNOUNCE, "RTM_IFANNOUNCE"},
160#endif /* RTM_IFANNOUNCE */
161 {0, NULL}
162};
163
164struct message rtm_flag_str[] =
165{
166 {RTF_UP, "UP"},
167 {RTF_GATEWAY, "GATEWAY"},
168 {RTF_HOST, "HOST"},
169 {RTF_REJECT, "REJECT"},
170 {RTF_DYNAMIC, "DYNAMIC"},
171 {RTF_MODIFIED, "MODIFIED"},
172 {RTF_DONE, "DONE"},
173#ifdef RTF_MASK
174 {RTF_MASK, "MASK"},
175#endif /* RTF_MASK */
176 {RTF_CLONING, "CLONING"},
177 {RTF_XRESOLVE, "XRESOLVE"},
178 {RTF_LLINFO, "LLINFO"},
179 {RTF_STATIC, "STATIC"},
180 {RTF_BLACKHOLE, "BLACKHOLE"},
paul6fe70d12005-11-12 22:55:10 +0000181#ifdef RTF_PRIVATE
182 {RTF_PRIVATE, "PRIVATE"},
183#endif /* RTF_PRIVATE */
paul718e3742002-12-13 20:15:29 +0000184 {RTF_PROTO1, "PROTO1"},
185 {RTF_PROTO2, "PROTO2"},
186#ifdef RTF_PRCLONING
187 {RTF_PRCLONING, "PRCLONING"},
188#endif /* RTF_PRCLONING */
189#ifdef RTF_WASCLONED
190 {RTF_WASCLONED, "WASCLONED"},
191#endif /* RTF_WASCLONED */
192#ifdef RTF_PROTO3
193 {RTF_PROTO3, "PROTO3"},
194#endif /* RTF_PROTO3 */
195#ifdef RTF_PINNED
196 {RTF_PINNED, "PINNED"},
197#endif /* RTF_PINNED */
198#ifdef RTF_LOCAL
199 {RTF_LOCAL, "LOCAL"},
200#endif /* RTF_LOCAL */
201#ifdef RTF_BROADCAST
202 {RTF_BROADCAST, "BROADCAST"},
203#endif /* RTF_BROADCAST */
204#ifdef RTF_MULTICAST
205 {RTF_MULTICAST, "MULTICAST"},
206#endif /* RTF_MULTICAST */
paul6fe70d12005-11-12 22:55:10 +0000207#ifdef RTF_MULTIRT
208 {RTF_MULTIRT, "MULTIRT"},
209#endif /* RTF_MULTIRT */
210#ifdef RTF_SETSRC
211 {RTF_SETSRC, "SETSRC"},
212#endif /* RTF_SETSRC */
paul718e3742002-12-13 20:15:29 +0000213 {0, NULL}
214};
215
216/* Kernel routing update socket. */
217int routing_sock = -1;
218
219/* Yes I'm checking ugly routing socket behavior. */
220/* #define DEBUG */
221
222/* Supported address family check. */
paul62debbb2005-06-14 14:07:07 +0000223static int inline
paul718e3742002-12-13 20:15:29 +0000224af_check (int family)
225{
226 if (family == AF_INET)
227 return 1;
228#ifdef HAVE_IPV6
229 if (family == AF_INET6)
230 return 1;
231#endif /* HAVE_IPV6 */
232 return 0;
233}
234
235/* Dump routing table flag for debug purpose. */
ajsb6178002004-12-07 21:12:56 +0000236static void
paul718e3742002-12-13 20:15:29 +0000237rtm_flag_dump (int flag)
238{
239 struct message *mes;
240 static char buf[BUFSIZ];
241
gdtcced60d2004-07-13 16:45:54 +0000242 buf[0] = '\0';
paul718e3742002-12-13 20:15:29 +0000243 for (mes = rtm_flag_str; mes->key != 0; mes++)
244 {
245 if (mes->key & flag)
246 {
247 strlcat (buf, mes->str, BUFSIZ);
248 strlcat (buf, " ", BUFSIZ);
249 }
250 }
ajsb6178002004-12-07 21:12:56 +0000251 zlog_debug ("Kernel: %s", buf);
paul718e3742002-12-13 20:15:29 +0000252}
253
254#ifdef RTM_IFANNOUNCE
255/* Interface adding function */
paul6621ca82005-11-23 13:02:08 +0000256static int
paul718e3742002-12-13 20:15:29 +0000257ifan_read (struct if_announcemsghdr *ifan)
258{
259 struct interface *ifp;
paul6fe70d12005-11-12 22:55:10 +0000260
paul718e3742002-12-13 20:15:29 +0000261 ifp = if_lookup_by_index (ifan->ifan_index);
paul6fe70d12005-11-12 22:55:10 +0000262
263 if (ifp)
264 assert ( (ifp->ifindex == ifan->ifan_index)
265 || (ifp->ifindex == IFINDEX_INTERNAL) );
266
paulec1a4282005-11-24 15:15:17 +0000267 if ( (ifp == NULL)
268 || ((ifp->ifindex == IFINDEX_INTERNAL)
269 && (ifan->ifan_what == IFAN_ARRIVAL)) )
paul718e3742002-12-13 20:15:29 +0000270 {
paul6fe70d12005-11-12 22:55:10 +0000271 if (IS_ZEBRA_DEBUG_KERNEL)
272 zlog_debug ("%s: creating interface for ifindex %d, name %s",
273 __func__, ifan->ifan_index, ifan->ifan_name);
274
paul718e3742002-12-13 20:15:29 +0000275 /* Create Interface */
ajs08dbfb62005-04-03 03:40:52 +0000276 ifp = if_get_by_name_len(ifan->ifan_name,
277 strnlen(ifan->ifan_name,
278 sizeof(ifan->ifan_name)));
paul718e3742002-12-13 20:15:29 +0000279 ifp->ifindex = ifan->ifan_index;
280
281 if_add_update (ifp);
282 }
283 else if (ifp != NULL && ifan->ifan_what == IFAN_DEPARTURE)
paul6eb88272005-07-29 14:36:00 +0000284 if_delete_update (ifp);
paul718e3742002-12-13 20:15:29 +0000285
286 if_get_flags (ifp);
287 if_get_mtu (ifp);
288 if_get_metric (ifp);
289
290 if (IS_ZEBRA_DEBUG_KERNEL)
paul6fe70d12005-11-12 22:55:10 +0000291 zlog_debug ("%s: interface %s index %d",
292 __func__, ifan->ifan_name, ifan->ifan_index);
paul718e3742002-12-13 20:15:29 +0000293
294 return 0;
295}
296#endif /* RTM_IFANNOUNCE */
297
gdtda26e3b2004-01-05 17:20:59 +0000298/*
299 * Handle struct if_msghdr obtained from reading routing socket or
300 * sysctl (from interface_list). There may or may not be sockaddrs
301 * present after the header.
302 */
paulec1a4282005-11-24 15:15:17 +0000303int
paul718e3742002-12-13 20:15:29 +0000304ifm_read (struct if_msghdr *ifm)
305{
paul3e95a072003-09-24 00:05:45 +0000306 struct interface *ifp = NULL;
paul6fe70d12005-11-12 22:55:10 +0000307 char ifname[IFNAMSIZ];
308 short ifnlen = 0;
paul0994c3a2005-11-11 09:52:40 +0000309 caddr_t *cp;
paul6fe70d12005-11-12 22:55:10 +0000310
311 /* terminate ifname at head (for strnlen) and tail (for safety) */
312 ifname[IFNAMSIZ - 1] = '\0';
313
gdtda26e3b2004-01-05 17:20:59 +0000314 /* paranoia: sanity check structure */
315 if (ifm->ifm_msglen < sizeof(struct if_msghdr))
316 {
317 zlog_err ("ifm_read: ifm->ifm_msglen %d too short\n",
318 ifm->ifm_msglen);
319 return -1;
320 }
321
322 /*
gdt4bfbea82004-01-06 01:13:05 +0000323 * Check for a sockaddr_dl following the message. First, point to
324 * where a socakddr might be if one follows the message.
gdtda26e3b2004-01-05 17:20:59 +0000325 */
gdt4bfbea82004-01-06 01:13:05 +0000326 cp = (void *)(ifm + 1);
327
paul3e95a072003-09-24 00:05:45 +0000328#ifdef SUNOS_5
paul3e95a072003-09-24 00:05:45 +0000329 /*
gdt4bfbea82004-01-06 01:13:05 +0000330 * XXX This behavior should be narrowed to only the kernel versions
331 * for which the structures returned do not match the headers.
332 *
paul3e95a072003-09-24 00:05:45 +0000333 * if_msghdr_t on 64 bit kernels in Solaris 9 and earlier versions
gdt4bfbea82004-01-06 01:13:05 +0000334 * is 12 bytes larger than the 32 bit version.
paul3e95a072003-09-24 00:05:45 +0000335 */
gdt4bfbea82004-01-06 01:13:05 +0000336 if (((struct sockaddr *) cp)->sa_family == AF_UNSPEC)
paul3e95a072003-09-24 00:05:45 +0000337 cp = cp + 12;
paul3e95a072003-09-24 00:05:45 +0000338#endif
paul718e3742002-12-13 20:15:29 +0000339
paul6fe70d12005-11-12 22:55:10 +0000340 RTA_ADDR_GET (NULL, RTA_DST, ifm->ifm_addrs, cp);
341 RTA_ADDR_GET (NULL, RTA_GATEWAY, ifm->ifm_addrs, cp);
342 RTA_ATTR_GET (NULL, RTA_NETMASK, ifm->ifm_addrs, cp);
343 RTA_ADDR_GET (NULL, RTA_GENMASK, ifm->ifm_addrs, cp);
344 RTA_NAME_GET (ifname, RTA_IFP, ifm->ifm_addrs, cp, ifnlen);
345 RTA_ADDR_GET (NULL, RTA_IFA, ifm->ifm_addrs, cp);
346 RTA_ADDR_GET (NULL, RTA_AUTHOR, ifm->ifm_addrs, cp);
347 RTA_ADDR_GET (NULL, RTA_BRD, ifm->ifm_addrs, cp);
348
349 if (IS_ZEBRA_DEBUG_KERNEL)
350 zlog_debug ("%s: sdl ifname %s", __func__, (ifnlen ? ifname : "(nil)"));
351
gdt4bfbea82004-01-06 01:13:05 +0000352 /*
paul6fe70d12005-11-12 22:55:10 +0000353 * Look up on ifindex first, because ifindices are the primary handle for
354 * interfaces across the user/kernel boundary, for most systems. (Some
355 * messages, such as up/down status changes on NetBSD, do not include a
356 * sockaddr_dl).
gdt4bfbea82004-01-06 01:13:05 +0000357 */
paul6fe70d12005-11-12 22:55:10 +0000358 if ( (ifp = if_lookup_by_index (ifm->ifm_index)) != NULL )
gdt4bfbea82004-01-06 01:13:05 +0000359 {
paul6fe70d12005-11-12 22:55:10 +0000360 /* we have an ifp, verify that the name matches as some systems,
361 * eg Solaris, have a 1:many association of ifindex:ifname
362 * if they dont match, we dont have the correct ifp and should
363 * set it back to NULL to let next check do lookup by name
364 */
365 if (ifnlen && (strncmp (ifp->name, ifname, IFNAMSIZ) != 0) )
gdt4bfbea82004-01-06 01:13:05 +0000366 {
paul6fe70d12005-11-12 22:55:10 +0000367 if (IS_ZEBRA_DEBUG_KERNEL)
368 zlog_debug ("%s: ifp name %s doesnt match sdl name %s",
369 __func__, ifp->name, ifname);
370 ifp = NULL;
gdt4bfbea82004-01-06 01:13:05 +0000371 }
372 }
paul6fe70d12005-11-12 22:55:10 +0000373
gdtda26e3b2004-01-05 17:20:59 +0000374 /*
paul6fe70d12005-11-12 22:55:10 +0000375 * If we dont have an ifp, try looking up by name. Particularly as some
376 * systems (Solaris) have a 1:many mapping of ifindex:ifname - the ifname
377 * is therefore our unique handle to that interface.
378 *
379 * Interfaces specified in the configuration file for which the ifindex
380 * has not been determined will have ifindex == IFINDEX_INTERNAL, and such
381 * interfaces are found by this search, and then their ifindex values can
382 * be filled in.
gdtda26e3b2004-01-05 17:20:59 +0000383 */
paul6fe70d12005-11-12 22:55:10 +0000384 if ( (ifp == NULL) && ifnlen)
385 ifp = if_lookup_by_name (ifname);
paul718e3742002-12-13 20:15:29 +0000386
gdtda26e3b2004-01-05 17:20:59 +0000387 /*
paul6fe70d12005-11-12 22:55:10 +0000388 * If ifp still does not exist or has an invalid index (IFINDEX_INTERNAL),
389 * create or fill in an interface.
gdtda26e3b2004-01-05 17:20:59 +0000390 */
ajsd2fc8892005-04-02 18:38:43 +0000391 if ((ifp == NULL) || (ifp->ifindex == IFINDEX_INTERNAL))
paul718e3742002-12-13 20:15:29 +0000392 {
gdt4bfbea82004-01-06 01:13:05 +0000393 /*
394 * To create or fill in an interface, a sockaddr_dl (via
395 * RTA_IFP) is required.
396 */
paul6fe70d12005-11-12 22:55:10 +0000397 if (!ifnlen)
paul718e3742002-12-13 20:15:29 +0000398 {
paul6fe70d12005-11-12 22:55:10 +0000399 zlog_warn ("Interface index %d (new) missing ifname\n",
paul718e3742002-12-13 20:15:29 +0000400 ifm->ifm_index);
401 return -1;
402 }
paul5c78b3d2006-01-25 04:31:40 +0000403
404#ifndef RTM_IFANNOUNCE
405 /* Down->Down interface should be ignored here.
406 * See further comment below.
407 */
408 if (!CHECK_FLAG (ifm->ifm_flags, IFF_UP))
409 return 0;
410#endif /* !RTM_IFANNOUNCE */
paul6fe70d12005-11-12 22:55:10 +0000411
paul3e95a072003-09-24 00:05:45 +0000412 if (ifp == NULL)
paul6fe70d12005-11-12 22:55:10 +0000413 {
414 /* Interface that zebra was not previously aware of, so create. */
415 ifp = if_create (ifname, ifnlen);
416 if (IS_ZEBRA_DEBUG_KERNEL)
417 zlog_debug ("%s: creating ifp for ifindex %d",
418 __func__, ifm->ifm_index);
419 }
paul718e3742002-12-13 20:15:29 +0000420
paul6fe70d12005-11-12 22:55:10 +0000421 if (IS_ZEBRA_DEBUG_KERNEL)
422 zlog_debug ("%s: updated/created ifp, ifname %s, ifindex %d",
423 __func__, ifp->name, ifp->ifindex);
gdt4bfbea82004-01-06 01:13:05 +0000424 /*
425 * Fill in newly created interface structure, or larval
ajsd2fc8892005-04-02 18:38:43 +0000426 * structure with ifindex IFINDEX_INTERNAL.
gdt4bfbea82004-01-06 01:13:05 +0000427 */
paul718e3742002-12-13 20:15:29 +0000428 ifp->ifindex = ifm->ifm_index;
paul5c78b3d2006-01-25 04:31:40 +0000429 if_flags_update (ifp, ifm->ifm_flags);
paul718e3742002-12-13 20:15:29 +0000430#if defined(__bsdi__)
431 if_kvm_get_mtu (ifp);
432#else
433 if_get_mtu (ifp);
434#endif /* __bsdi__ */
435 if_get_metric (ifp);
436
paul718e3742002-12-13 20:15:29 +0000437 if_add_update (ifp);
438 }
439 else
gdtda26e3b2004-01-05 17:20:59 +0000440 /*
441 * Interface structure exists. Adjust stored flags from
442 * notification. If interface has up->down or down->up
443 * transition, call state change routines (to adjust routes,
444 * notify routing daemons, etc.). (Other flag changes are stored
445 * but apparently do not trigger action.)
446 */
paul718e3742002-12-13 20:15:29 +0000447 {
paul6fe70d12005-11-12 22:55:10 +0000448 if (ifp->ifindex != ifm->ifm_index)
449 {
450 zlog_warn ("%s: index mismatch, ifname %s, ifp index %d, "
451 "ifm index %d",
452 __func__, ifp->name, ifp->ifindex, ifm->ifm_index);
453 return -1;
454 }
455
paul5c78b3d2006-01-25 04:31:40 +0000456 /* update flags and handle operative->inoperative transition, if any */
457 if_flags_update (ifp, ifm->ifm_flags);
458
paul6eb88272005-07-29 14:36:00 +0000459#ifndef RTM_IFANNOUNCE
paul5c78b3d2006-01-25 04:31:40 +0000460 if (!if_is_up (ifp))
461 {
462 /* No RTM_IFANNOUNCE on this platform, so we can never
463 * distinguish between ~IFF_UP and delete. We must presume
464 * it has been deleted.
465 * Eg, Solaris will not notify us of unplumb.
466 *
467 * XXX: Fixme - this should be runtime detected
468 * So that a binary compiled on a system with IFANNOUNCE
469 * will still behave correctly if run on a platform without
470 */
471 if_delete_update (ifp);
472 }
paul6eb88272005-07-29 14:36:00 +0000473#endif /* RTM_IFANNOUNCE */
Denis Ovsienko1ba27562007-08-21 16:15:39 +0000474 if (if_is_up (ifp))
475 {
476#if defined(__bsdi__)
477 if_kvm_get_mtu (ifp);
478#else
479 if_get_mtu (ifp);
480#endif /* __bsdi__ */
481 if_get_metric (ifp);
482 }
paul718e3742002-12-13 20:15:29 +0000483 }
paul5c78b3d2006-01-25 04:31:40 +0000484
paul718e3742002-12-13 20:15:29 +0000485#ifdef HAVE_NET_RT_IFLIST
486 ifp->stats = ifm->ifm_data;
487#endif /* HAVE_NET_RT_IFLIST */
488
489 if (IS_ZEBRA_DEBUG_KERNEL)
paul6fe70d12005-11-12 22:55:10 +0000490 zlog_debug ("%s: interface %s index %d",
491 __func__, ifp->name, ifp->ifindex);
paul718e3742002-12-13 20:15:29 +0000492
493 return 0;
494}
495
496/* Address read from struct ifa_msghdr. */
paul6621ca82005-11-23 13:02:08 +0000497static void
paul718e3742002-12-13 20:15:29 +0000498ifam_read_mesg (struct ifa_msghdr *ifm,
499 union sockunion *addr,
500 union sockunion *mask,
paul6fe70d12005-11-12 22:55:10 +0000501 union sockunion *brd,
502 char *ifname,
503 short *ifnlen)
paul718e3742002-12-13 20:15:29 +0000504{
505 caddr_t pnt, end;
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000506 union sockunion dst;
507 union sockunion gateway;
paul718e3742002-12-13 20:15:29 +0000508
509 pnt = (caddr_t)(ifm + 1);
510 end = ((caddr_t)ifm) + ifm->ifam_msglen;
511
paul718e3742002-12-13 20:15:29 +0000512 /* Be sure structure is cleared */
513 memset (mask, 0, sizeof (union sockunion));
514 memset (addr, 0, sizeof (union sockunion));
paul6621ca82005-11-23 13:02:08 +0000515 memset (brd, 0, sizeof (union sockunion));
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000516 memset (&dst, 0, sizeof (union sockunion));
517 memset (&gateway, 0, sizeof (union sockunion));
paul718e3742002-12-13 20:15:29 +0000518
519 /* We fetch each socket variable into sockunion. */
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000520 RTA_ADDR_GET (&dst, RTA_DST, ifm->ifam_addrs, pnt);
521 RTA_ADDR_GET (&gateway, RTA_GATEWAY, ifm->ifam_addrs, pnt);
paul62debbb2005-06-14 14:07:07 +0000522 RTA_ATTR_GET (mask, RTA_NETMASK, ifm->ifam_addrs, pnt);
523 RTA_ADDR_GET (NULL, RTA_GENMASK, ifm->ifam_addrs, pnt);
paul6fe70d12005-11-12 22:55:10 +0000524 RTA_NAME_GET (ifname, RTA_IFP, ifm->ifam_addrs, pnt, *ifnlen);
paul62debbb2005-06-14 14:07:07 +0000525 RTA_ADDR_GET (addr, RTA_IFA, ifm->ifam_addrs, pnt);
526 RTA_ADDR_GET (NULL, RTA_AUTHOR, ifm->ifam_addrs, pnt);
paul6fe70d12005-11-12 22:55:10 +0000527 RTA_ADDR_GET (brd, RTA_BRD, ifm->ifam_addrs, pnt);
paul718e3742002-12-13 20:15:29 +0000528
paul6fe70d12005-11-12 22:55:10 +0000529 if (IS_ZEBRA_DEBUG_KERNEL)
Andrew J. Schorr55196042006-05-17 15:04:59 +0000530 {
531 switch (sockunion_family(addr))
532 {
533 case AF_INET:
534 {
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000535 char buf[4][INET_ADDRSTRLEN];
Andrew J. Schorr55196042006-05-17 15:04:59 +0000536 zlog_debug ("%s: ifindex %d, ifname %s, ifam_addrs 0x%x, "
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000537 "ifam_flags 0x%x, addr %s/%d broad %s dst %s "
538 "gateway %s",
539 __func__, ifm->ifam_index,
Andrew J. Schorr55196042006-05-17 15:04:59 +0000540 (ifnlen ? ifname : "(nil)"), ifm->ifam_addrs,
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000541 ifm->ifam_flags,
Andrew J. Schorr55196042006-05-17 15:04:59 +0000542 inet_ntop(AF_INET,&addr->sin.sin_addr,
543 buf[0],sizeof(buf[0])),
544 ip_masklen(mask->sin.sin_addr),
545 inet_ntop(AF_INET,&brd->sin.sin_addr,
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000546 buf[1],sizeof(buf[1])),
547 inet_ntop(AF_INET,&dst.sin.sin_addr,
548 buf[2],sizeof(buf[2])),
549 inet_ntop(AF_INET,&gateway.sin.sin_addr,
550 buf[3],sizeof(buf[3])));
Andrew J. Schorr55196042006-05-17 15:04:59 +0000551 }
552 break;
553#ifdef HAVE_IPV6
554 case AF_INET6:
555 {
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000556 char buf[4][INET6_ADDRSTRLEN];
Andrew J. Schorr55196042006-05-17 15:04:59 +0000557 zlog_debug ("%s: ifindex %d, ifname %s, ifam_addrs 0x%x, "
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000558 "ifam_flags 0x%x, addr %s/%d broad %s dst %s "
559 "gateway %s",
Andrew J. Schorr55196042006-05-17 15:04:59 +0000560 __func__, ifm->ifam_index,
561 (ifnlen ? ifname : "(nil)"), ifm->ifam_addrs,
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000562 ifm->ifam_flags,
Andrew J. Schorr55196042006-05-17 15:04:59 +0000563 inet_ntop(AF_INET6,&addr->sin6.sin6_addr,
564 buf[0],sizeof(buf[0])),
565 ip6_masklen(mask->sin6.sin6_addr),
566 inet_ntop(AF_INET6,&brd->sin6.sin6_addr,
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000567 buf[1],sizeof(buf[1])),
568 inet_ntop(AF_INET6,&dst.sin6.sin6_addr,
569 buf[2],sizeof(buf[2])),
570 inet_ntop(AF_INET6,&gateway.sin6.sin6_addr,
571 buf[3],sizeof(buf[3])));
Andrew J. Schorr55196042006-05-17 15:04:59 +0000572 }
573 break;
574#endif /* HAVE_IPV6 */
575 default:
576 zlog_debug ("%s: ifindex %d, ifname %s, ifam_addrs 0x%x",
577 __func__, ifm->ifam_index,
578 (ifnlen ? ifname : "(nil)"), ifm->ifam_addrs);
579 break;
580 }
581 }
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000582
paul718e3742002-12-13 20:15:29 +0000583 /* Assert read up end point matches to end point */
584 if (pnt != end)
585 zlog_warn ("ifam_read() does't read all socket data");
586}
587
588/* Interface's address information get. */
paulec1a4282005-11-24 15:15:17 +0000589int
paul718e3742002-12-13 20:15:29 +0000590ifam_read (struct ifa_msghdr *ifam)
591{
paul6fe70d12005-11-12 22:55:10 +0000592 struct interface *ifp = NULL;
paul0752ef02005-11-03 12:35:21 +0000593 union sockunion addr, mask, brd;
paul6fe70d12005-11-12 22:55:10 +0000594 char ifname[INTERFACE_NAMSIZ];
595 short ifnlen = 0;
596 char isalias = 0;
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000597 int flags = 0;
paul6fe70d12005-11-12 22:55:10 +0000598
599 ifname[0] = ifname[INTERFACE_NAMSIZ - 1] = '\0';
600
601 /* Allocate and read address information. */
602 ifam_read_mesg (ifam, &addr, &mask, &brd, ifname, &ifnlen);
603
604 if ((ifp = if_lookup_by_index(ifam->ifam_index)) == NULL)
paul718e3742002-12-13 20:15:29 +0000605 {
paul6fe70d12005-11-12 22:55:10 +0000606 zlog_warn ("%s: no interface for ifname %s, index %d",
607 __func__, ifname, ifam->ifam_index);
paul718e3742002-12-13 20:15:29 +0000608 return -1;
609 }
paul6fe70d12005-11-12 22:55:10 +0000610
611 if (ifnlen && strncmp (ifp->name, ifname, INTERFACE_NAMSIZ))
612 isalias = 1;
613
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000614 /* N.B. The info in ifa_msghdr does not tell us whether the RTA_BRD
615 field contains a broadcast address or a peer address, so we are forced to
616 rely upon the interface type. */
617 if (if_is_pointopoint(ifp))
618 SET_FLAG(flags, ZEBRA_IFA_PEER);
619
Paul Jakma65022082007-03-06 13:43:05 +0000620#if 0
621 /* it might seem cute to grab the interface metric here, however
622 * we're processing an address update message, and so some systems
623 * (e.g. FBSD) dont bother to fill in ifam_metric. Disabled, but left
624 * in deliberately, as comment.
625 */
pauld34b8992006-01-17 18:03:04 +0000626 ifp->metric = ifam->ifam_metric;
Paul Jakma65022082007-03-06 13:43:05 +0000627#endif
628
paul718e3742002-12-13 20:15:29 +0000629 /* Add connected address. */
630 switch (sockunion_family (&addr))
631 {
632 case AF_INET:
633 if (ifam->ifam_type == RTM_NEWADDR)
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000634 connected_add_ipv4 (ifp, flags, &addr.sin.sin_addr,
paul718e3742002-12-13 20:15:29 +0000635 ip_masklen (mask.sin.sin_addr),
pauld34b8992006-01-17 18:03:04 +0000636 &brd.sin.sin_addr,
637 (isalias ? ifname : NULL));
paul718e3742002-12-13 20:15:29 +0000638 else
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000639 connected_delete_ipv4 (ifp, flags, &addr.sin.sin_addr,
paul718e3742002-12-13 20:15:29 +0000640 ip_masklen (mask.sin.sin_addr),
paul0752ef02005-11-03 12:35:21 +0000641 &brd.sin.sin_addr);
paul718e3742002-12-13 20:15:29 +0000642 break;
643#ifdef HAVE_IPV6
644 case AF_INET6:
645 /* Unset interface index from link-local address when IPv6 stack
646 is KAME. */
647 if (IN6_IS_ADDR_LINKLOCAL (&addr.sin6.sin6_addr))
648 SET_IN6_LINKLOCAL_IFINDEX (addr.sin6.sin6_addr, 0);
649
650 if (ifam->ifam_type == RTM_NEWADDR)
Andrew J. Schorr7ab62c52007-05-17 15:00:41 +0000651 connected_add_ipv6 (ifp, flags, &addr.sin6.sin6_addr,
paul718e3742002-12-13 20:15:29 +0000652 ip6_masklen (mask.sin6.sin6_addr),
pauld34b8992006-01-17 18:03:04 +0000653 &brd.sin6.sin6_addr,
654 (isalias ? ifname : NULL));
paul718e3742002-12-13 20:15:29 +0000655 else
656 connected_delete_ipv6 (ifp,
657 &addr.sin6.sin6_addr,
658 ip6_masklen (mask.sin6.sin6_addr),
paul0752ef02005-11-03 12:35:21 +0000659 &brd.sin6.sin6_addr);
paul718e3742002-12-13 20:15:29 +0000660 break;
661#endif /* HAVE_IPV6 */
662 default:
663 /* Unsupported family silently ignore... */
664 break;
665 }
paul5c78b3d2006-01-25 04:31:40 +0000666
667 /* Check interface flag for implicit up of the interface. */
668 if_refresh (ifp);
669
670#ifdef SUNOS_5
671 /* In addition to lacking IFANNOUNCE, on SUNOS IFF_UP is strange.
672 * See comments for SUNOS_5 in interface.c::if_flags_mangle.
673 *
674 * Here we take care of case where the real IFF_UP was previously
675 * unset (as kept in struct zebra_if.primary_state) and the mangled
676 * IFF_UP (ie IFF_UP set || listcount(connected) has now transitioned
677 * to unset due to the lost non-primary address having DELADDR'd.
678 *
679 * we must delete the interface, because in between here and next
680 * event for this interface-name the administrator could unplumb
681 * and replumb the interface.
682 */
683 if (!if_is_up (ifp))
684 if_delete_update (ifp);
685#endif /* SUNOS_5 */
686
paul718e3742002-12-13 20:15:29 +0000687 return 0;
688}
689
690/* Interface function for reading kernel routing table information. */
paul6621ca82005-11-23 13:02:08 +0000691static int
paul718e3742002-12-13 20:15:29 +0000692rtm_read_mesg (struct rt_msghdr *rtm,
693 union sockunion *dest,
694 union sockunion *mask,
paul6fe70d12005-11-12 22:55:10 +0000695 union sockunion *gate,
696 char *ifname,
697 short *ifnlen)
paul718e3742002-12-13 20:15:29 +0000698{
699 caddr_t pnt, end;
700
701 /* Pnt points out socket data start point. */
702 pnt = (caddr_t)(rtm + 1);
703 end = ((caddr_t)rtm) + rtm->rtm_msglen;
704
705 /* rt_msghdr version check. */
706 if (rtm->rtm_version != RTM_VERSION)
707 zlog (NULL, LOG_WARNING,
708 "Routing message version different %d should be %d."
709 "This may cause problem\n", rtm->rtm_version, RTM_VERSION);
paul62debbb2005-06-14 14:07:07 +0000710
paul718e3742002-12-13 20:15:29 +0000711 /* Be sure structure is cleared */
712 memset (dest, 0, sizeof (union sockunion));
713 memset (gate, 0, sizeof (union sockunion));
714 memset (mask, 0, sizeof (union sockunion));
715
716 /* We fetch each socket variable into sockunion. */
paul62debbb2005-06-14 14:07:07 +0000717 RTA_ADDR_GET (dest, RTA_DST, rtm->rtm_addrs, pnt);
718 RTA_ADDR_GET (gate, RTA_GATEWAY, rtm->rtm_addrs, pnt);
719 RTA_ATTR_GET (mask, RTA_NETMASK, rtm->rtm_addrs, pnt);
720 RTA_ADDR_GET (NULL, RTA_GENMASK, rtm->rtm_addrs, pnt);
paul6fe70d12005-11-12 22:55:10 +0000721 RTA_NAME_GET (ifname, RTA_IFP, rtm->rtm_addrs, pnt, *ifnlen);
paul62debbb2005-06-14 14:07:07 +0000722 RTA_ADDR_GET (NULL, RTA_IFA, rtm->rtm_addrs, pnt);
723 RTA_ADDR_GET (NULL, RTA_AUTHOR, rtm->rtm_addrs, pnt);
724 RTA_ADDR_GET (NULL, RTA_BRD, rtm->rtm_addrs, pnt);
paul718e3742002-12-13 20:15:29 +0000725
726 /* If there is netmask information set it's family same as
727 destination family*/
728 if (rtm->rtm_addrs & RTA_NETMASK)
729 mask->sa.sa_family = dest->sa.sa_family;
730
731 /* Assert read up to the end of pointer. */
732 if (pnt != end)
733 zlog (NULL, LOG_WARNING, "rtm_read() does't read all socket data.");
734
735 return rtm->rtm_flags;
736}
737
paulec1a4282005-11-24 15:15:17 +0000738void
paul718e3742002-12-13 20:15:29 +0000739rtm_read (struct rt_msghdr *rtm)
740{
741 int flags;
742 u_char zebra_flags;
743 union sockunion dest, mask, gate;
paul6fe70d12005-11-12 22:55:10 +0000744 char ifname[INTERFACE_NAMSIZ + 1];
745 short ifnlen = 0;
paul718e3742002-12-13 20:15:29 +0000746
747 zebra_flags = 0;
748
paul718e3742002-12-13 20:15:29 +0000749 /* Read destination and netmask and gateway from rtm message
750 structure. */
paul6fe70d12005-11-12 22:55:10 +0000751 flags = rtm_read_mesg (rtm, &dest, &mask, &gate, ifname, &ifnlen);
Denis Ovsienko6da59802007-08-17 14:16:30 +0000752 if (!(flags & RTF_DONE))
753 return;
Denis Ovsienkodc958242007-08-13 16:03:06 +0000754 if (IS_ZEBRA_DEBUG_KERNEL)
755 zlog_debug ("%s: got rtm of type %d (%s)", __func__, rtm->rtm_type,
Denis Ovsienko2d844522007-09-14 11:31:55 +0000756 lookup (rtm_type_str, rtm->rtm_type));
paul718e3742002-12-13 20:15:29 +0000757
758#ifdef RTF_CLONED /*bsdi, netbsd 1.6*/
759 if (flags & RTF_CLONED)
760 return;
761#endif
762#ifdef RTF_WASCLONED /*freebsd*/
763 if (flags & RTF_WASCLONED)
764 return;
765#endif
766
767 if ((rtm->rtm_type == RTM_ADD) && ! (flags & RTF_UP))
768 return;
769
770 /* This is connected route. */
771 if (! (flags & RTF_GATEWAY))
772 return;
773
774 if (flags & RTF_PROTO1)
775 SET_FLAG (zebra_flags, ZEBRA_FLAG_SELFROUTE);
776
777 /* This is persistent route. */
778 if (flags & RTF_STATIC)
779 SET_FLAG (zebra_flags, ZEBRA_FLAG_STATIC);
780
hasso81dfcaa2003-05-25 19:21:25 +0000781 /* This is a reject or blackhole route */
782 if (flags & RTF_REJECT)
783 SET_FLAG (zebra_flags, ZEBRA_FLAG_REJECT);
784 if (flags & RTF_BLACKHOLE)
785 SET_FLAG (zebra_flags, ZEBRA_FLAG_BLACKHOLE);
786
paul718e3742002-12-13 20:15:29 +0000787 if (dest.sa.sa_family == AF_INET)
788 {
789 struct prefix_ipv4 p;
790
791 p.family = AF_INET;
792 p.prefix = dest.sin.sin_addr;
793 if (flags & RTF_HOST)
794 p.prefixlen = IPV4_MAX_PREFIXLEN;
795 else
796 p.prefixlen = ip_masklen (mask.sin.sin_addr);
paulca162182005-09-12 16:58:52 +0000797
Denis Ovsienkodc958242007-08-13 16:03:06 +0000798 /* Catch self originated messages and match them against our current RIB.
799 * At the same time, ignore unconfirmed messages, they should be tracked
800 * by rtm_write() and kernel_rtm_ipv4().
801 */
Denis Ovsienko96934e62007-09-14 14:56:28 +0000802 if (rtm->rtm_type != RTM_GET && rtm->rtm_pid == pid)
Denis Ovsienkodc958242007-08-13 16:03:06 +0000803 {
804 char buf[INET_ADDRSTRLEN], gate_buf[INET_ADDRSTRLEN];
805 int ret;
Denis Ovsienkodc958242007-08-13 16:03:06 +0000806 if (! IS_ZEBRA_DEBUG_RIB)
807 return;
808 ret = rib_lookup_ipv4_route (&p, &gate);
809 inet_ntop (AF_INET, &p.prefix, buf, INET_ADDRSTRLEN);
810 switch (rtm->rtm_type)
811 {
812 case RTM_ADD:
813 case RTM_GET:
814 case RTM_CHANGE:
815 /* The kernel notifies us about a new route in FIB created by us.
816 Do we have a correspondent entry in our RIB? */
817 switch (ret)
818 {
819 case ZEBRA_RIB_NOTFOUND:
820 zlog_debug ("%s: %s %s/%d: desync: RR isn't yet in RIB, while already in FIB",
Denis Ovsienko2d844522007-09-14 11:31:55 +0000821 __func__, lookup (rtm_type_str, rtm->rtm_type), buf, p.prefixlen);
Denis Ovsienkodc958242007-08-13 16:03:06 +0000822 break;
823 case ZEBRA_RIB_FOUND_CONNECTED:
824 case ZEBRA_RIB_FOUND_NOGATE:
825 inet_ntop (AF_INET, &gate.sin.sin_addr, gate_buf, INET_ADDRSTRLEN);
826 zlog_debug ("%s: %s %s/%d: desync: RR is in RIB, but gate differs (ours is %s)",
Denis Ovsienko2d844522007-09-14 11:31:55 +0000827 __func__, lookup (rtm_type_str, rtm->rtm_type), buf, p.prefixlen, gate_buf);
Denis Ovsienkodc958242007-08-13 16:03:06 +0000828 break;
829 case ZEBRA_RIB_FOUND_EXACT: /* RIB RR == FIB RR */
830 zlog_debug ("%s: %s %s/%d: done Ok",
Denis Ovsienko2d844522007-09-14 11:31:55 +0000831 __func__, lookup (rtm_type_str, rtm->rtm_type), buf, p.prefixlen);
Denis Ovsienkodc958242007-08-13 16:03:06 +0000832 rib_lookup_and_dump (&p);
833 return;
834 break;
835 }
836 break;
837 case RTM_DELETE:
838 /* The kernel notifies us about a route deleted by us. Do we still
839 have it in the RIB? Do we have anything instead? */
840 switch (ret)
841 {
842 case ZEBRA_RIB_FOUND_EXACT:
843 zlog_debug ("%s: %s %s/%d: desync: RR is still in RIB, while already not in FIB",
Denis Ovsienko2d844522007-09-14 11:31:55 +0000844 __func__, lookup (rtm_type_str, rtm->rtm_type), buf, p.prefixlen);
Denis Ovsienkodc958242007-08-13 16:03:06 +0000845 rib_lookup_and_dump (&p);
846 break;
847 case ZEBRA_RIB_FOUND_CONNECTED:
848 case ZEBRA_RIB_FOUND_NOGATE:
849 zlog_debug ("%s: %s %s/%d: desync: RR is still in RIB, plus gate differs",
Denis Ovsienko2d844522007-09-14 11:31:55 +0000850 __func__, lookup (rtm_type_str, rtm->rtm_type), buf, p.prefixlen);
Denis Ovsienkodc958242007-08-13 16:03:06 +0000851 rib_lookup_and_dump (&p);
852 break;
853 case ZEBRA_RIB_NOTFOUND: /* RIB RR == FIB RR */
854 zlog_debug ("%s: %s %s/%d: done Ok",
Denis Ovsienko2d844522007-09-14 11:31:55 +0000855 __func__, lookup (rtm_type_str, rtm->rtm_type), buf, p.prefixlen);
Denis Ovsienkodc958242007-08-13 16:03:06 +0000856 rib_lookup_and_dump (&p);
857 return;
858 break;
859 }
860 break;
861 default:
862 zlog_debug ("%s: %s/%d: warning: loopback RTM of type %s received",
Denis Ovsienko2d844522007-09-14 11:31:55 +0000863 __func__, buf, p.prefixlen, lookup (rtm_type_str, rtm->rtm_type));
Denis Ovsienkodc958242007-08-13 16:03:06 +0000864 }
865 return;
866 }
867
paulca162182005-09-12 16:58:52 +0000868 /* Change, delete the old prefix, we have no further information
869 * to specify the route really
870 */
871 if (rtm->rtm_type == RTM_CHANGE)
872 rib_delete_ipv4 (ZEBRA_ROUTE_KERNEL, zebra_flags, &p,
873 NULL, 0, 0);
874
875 if (rtm->rtm_type == RTM_GET
876 || rtm->rtm_type == RTM_ADD
877 || rtm->rtm_type == RTM_CHANGE)
paul718e3742002-12-13 20:15:29 +0000878 rib_add_ipv4 (ZEBRA_ROUTE_KERNEL, zebra_flags,
Paul Jakma7514fb72007-05-02 16:05:35 +0000879 &p, &gate.sin.sin_addr, NULL, 0, 0, 0, 0);
paul718e3742002-12-13 20:15:29 +0000880 else
881 rib_delete_ipv4 (ZEBRA_ROUTE_KERNEL, zebra_flags,
882 &p, &gate.sin.sin_addr, 0, 0);
883 }
884#ifdef HAVE_IPV6
885 if (dest.sa.sa_family == AF_INET6)
886 {
Denis Ovsienko5619f562007-10-24 13:13:21 +0000887 /* One day we might have a debug section here like one in the
888 * IPv4 case above. Just ignore own messages at the moment.
889 */
890 if (rtm->rtm_type != RTM_GET && rtm->rtm_pid == pid)
891 return;
paul718e3742002-12-13 20:15:29 +0000892 struct prefix_ipv6 p;
893 unsigned int ifindex = 0;
894
895 p.family = AF_INET6;
896 p.prefix = dest.sin6.sin6_addr;
897 if (flags & RTF_HOST)
898 p.prefixlen = IPV6_MAX_PREFIXLEN;
899 else
900 p.prefixlen = ip6_masklen (mask.sin6.sin6_addr);
901
902#ifdef KAME
903 if (IN6_IS_ADDR_LINKLOCAL (&gate.sin6.sin6_addr))
904 {
905 ifindex = IN6_LINKLOCAL_IFINDEX (gate.sin6.sin6_addr);
906 SET_IN6_LINKLOCAL_IFINDEX (gate.sin6.sin6_addr, 0);
907 }
908#endif /* KAME */
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)
paul6621ca82005-11-23 13:02:08 +0000914 rib_delete_ipv6 (ZEBRA_ROUTE_KERNEL, zebra_flags, &p,
paulca162182005-09-12 16:58:52 +0000915 NULL, 0, 0);
916
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_ipv6 (ZEBRA_ROUTE_KERNEL, zebra_flags,
hassobe61c4e2005-08-27 06:05:47 +0000921 &p, &gate.sin6.sin6_addr, ifindex, 0, 0, 0);
paul718e3742002-12-13 20:15:29 +0000922 else
923 rib_delete_ipv6 (ZEBRA_ROUTE_KERNEL, zebra_flags,
924 &p, &gate.sin6.sin6_addr, ifindex, 0);
925 }
926#endif /* HAVE_IPV6 */
927}
928
929/* Interface function for the kernel routing table updates. Support
paul6621ca82005-11-23 13:02:08 +0000930 * for RTM_CHANGE will be needed.
931 * Exported only for rt_socket.c
932 */
paul718e3742002-12-13 20:15:29 +0000933int
934rtm_write (int message,
935 union sockunion *dest,
936 union sockunion *mask,
937 union sockunion *gate,
938 unsigned int index,
939 int zebra_flags,
940 int metric)
941{
942 int ret;
943 caddr_t pnt;
944 struct interface *ifp;
paul718e3742002-12-13 20:15:29 +0000945
946 /* Sequencial number of routing message. */
947 static int msg_seq = 0;
948
949 /* Struct of rt_msghdr and buffer for storing socket's data. */
950 struct
951 {
952 struct rt_msghdr rtm;
953 char buf[512];
954 } msg;
955
paul718e3742002-12-13 20:15:29 +0000956 if (routing_sock < 0)
957 return ZEBRA_ERR_EPERM;
958
959 /* Clear and set rt_msghdr values */
960 memset (&msg, 0, sizeof (struct rt_msghdr));
961 msg.rtm.rtm_version = RTM_VERSION;
962 msg.rtm.rtm_type = message;
963 msg.rtm.rtm_seq = msg_seq++;
964 msg.rtm.rtm_addrs = RTA_DST;
965 msg.rtm.rtm_addrs |= RTA_GATEWAY;
966 msg.rtm.rtm_flags = RTF_UP;
967 msg.rtm.rtm_index = index;
968
969 if (metric != 0)
970 {
971 msg.rtm.rtm_rmx.rmx_hopcount = metric;
972 msg.rtm.rtm_inits |= RTV_HOPCOUNT;
973 }
974
975 ifp = if_lookup_by_index (index);
976
977 if (gate && message == RTM_ADD)
978 msg.rtm.rtm_flags |= RTF_GATEWAY;
979
980 if (! gate && message == RTM_ADD && ifp &&
981 (ifp->flags & IFF_POINTOPOINT) == 0)
982 msg.rtm.rtm_flags |= RTF_CLONING;
983
984 /* If no protocol specific gateway is specified, use link
985 address for gateway. */
986 if (! gate)
987 {
988 if (!ifp)
989 {
Denis Ovsienkodc958242007-08-13 16:03:06 +0000990 char dest_buf[INET_ADDRSTRLEN] = "NULL", mask_buf[INET_ADDRSTRLEN] = "255.255.255.255";
991 if (dest)
992 inet_ntop (AF_INET, &dest->sin.sin_addr, dest_buf, INET_ADDRSTRLEN);
993 if (mask)
994 inet_ntop (AF_INET, &mask->sin.sin_addr, mask_buf, INET_ADDRSTRLEN);
995 zlog_warn ("%s: %s/%s: gate == NULL and no gateway found for ifindex %d",
996 __func__, dest_buf, mask_buf, index);
paul718e3742002-12-13 20:15:29 +0000997 return -1;
998 }
999 gate = (union sockunion *) & ifp->sdl;
1000 }
1001
1002 if (mask)
1003 msg.rtm.rtm_addrs |= RTA_NETMASK;
1004 else if (message == RTM_ADD)
1005 msg.rtm.rtm_flags |= RTF_HOST;
1006
1007 /* Tagging route with flags */
1008 msg.rtm.rtm_flags |= (RTF_PROTO1);
1009
1010 /* Additional flags. */
1011 if (zebra_flags & ZEBRA_FLAG_BLACKHOLE)
1012 msg.rtm.rtm_flags |= RTF_BLACKHOLE;
hasso81dfcaa2003-05-25 19:21:25 +00001013 if (zebra_flags & ZEBRA_FLAG_REJECT)
1014 msg.rtm.rtm_flags |= RTF_REJECT;
1015
paul718e3742002-12-13 20:15:29 +00001016
Paul Jakma6f0e3f62007-05-10 02:38:51 +00001017#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
paul718e3742002-12-13 20:15:29 +00001018#define SOCKADDRSET(X,R) \
1019 if (msg.rtm.rtm_addrs & (R)) \
1020 { \
1021 int len = ROUNDUP ((X)->sa.sa_len); \
1022 memcpy (pnt, (caddr_t)(X), len); \
1023 pnt += len; \
1024 }
1025#else
1026#define SOCKADDRSET(X,R) \
1027 if (msg.rtm.rtm_addrs & (R)) \
1028 { \
paul6fe70d12005-11-12 22:55:10 +00001029 int len = SAROUNDUP (X); \
paul718e3742002-12-13 20:15:29 +00001030 memcpy (pnt, (caddr_t)(X), len); \
1031 pnt += len; \
1032 }
Paul Jakma6f0e3f62007-05-10 02:38:51 +00001033#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
paul718e3742002-12-13 20:15:29 +00001034
1035 pnt = (caddr_t) msg.buf;
1036
1037 /* Write each socket data into rtm message buffer */
1038 SOCKADDRSET (dest, RTA_DST);
1039 SOCKADDRSET (gate, RTA_GATEWAY);
1040 SOCKADDRSET (mask, RTA_NETMASK);
1041
1042 msg.rtm.rtm_msglen = pnt - (caddr_t) &msg;
1043
1044 ret = write (routing_sock, &msg, msg.rtm.rtm_msglen);
1045
1046 if (ret != msg.rtm.rtm_msglen)
1047 {
1048 if (errno == EEXIST)
1049 return ZEBRA_ERR_RTEXIST;
1050 if (errno == ENETUNREACH)
1051 return ZEBRA_ERR_RTUNREACH;
Denis Ovsienkodc958242007-08-13 16:03:06 +00001052 if (errno == ESRCH)
1053 return ZEBRA_ERR_RTNOEXIST;
paul718e3742002-12-13 20:15:29 +00001054
Denis Ovsienkodc958242007-08-13 16:03:06 +00001055 zlog_warn ("%s: write : %s (%d)", __func__, safe_strerror (errno), errno);
1056 return ZEBRA_ERR_KERNEL;
paul718e3742002-12-13 20:15:29 +00001057 }
Denis Ovsienkodc958242007-08-13 16:03:06 +00001058 return ZEBRA_ERR_NOERROR;
paul718e3742002-12-13 20:15:29 +00001059}
1060
1061
1062#include "thread.h"
1063#include "zebra/zserv.h"
1064
paul718e3742002-12-13 20:15:29 +00001065/* For debug purpose. */
ajsb6178002004-12-07 21:12:56 +00001066static void
paul718e3742002-12-13 20:15:29 +00001067rtmsg_debug (struct rt_msghdr *rtm)
1068{
Denis Ovsienko2d844522007-09-14 11:31:55 +00001069 zlog_debug ("Kernel: Len: %d Type: %s", rtm->rtm_msglen, lookup (rtm_type_str, rtm->rtm_type));
paul718e3742002-12-13 20:15:29 +00001070 rtm_flag_dump (rtm->rtm_flags);
ajsb6178002004-12-07 21:12:56 +00001071 zlog_debug ("Kernel: message seq %d", rtm->rtm_seq);
paul6fe70d12005-11-12 22:55:10 +00001072 zlog_debug ("Kernel: pid %d, rtm_addrs 0x%x", rtm->rtm_pid, rtm->rtm_addrs);
paul718e3742002-12-13 20:15:29 +00001073}
1074
1075/* This is pretty gross, better suggestions welcome -- mhandler */
1076#ifndef RTAX_MAX
1077#ifdef RTA_NUMBITS
1078#define RTAX_MAX RTA_NUMBITS
1079#else
1080#define RTAX_MAX 8
1081#endif /* RTA_NUMBITS */
1082#endif /* RTAX_MAX */
1083
1084/* Kernel routing table and interface updates via routing socket. */
paul6621ca82005-11-23 13:02:08 +00001085static int
paul718e3742002-12-13 20:15:29 +00001086kernel_read (struct thread *thread)
1087{
1088 int sock;
1089 int nbytes;
1090 struct rt_msghdr *rtm;
1091
gdtdbee01f2004-01-06 00:36:51 +00001092 /*
1093 * This must be big enough for any message the kernel might send.
gdtb27900b2004-01-08 15:44:29 +00001094 * Rather than determining how many sockaddrs of what size might be
1095 * in each particular message, just use RTAX_MAX of sockaddr_storage
1096 * for each. Note that the sockaddrs must be after each message
1097 * definition, or rather after whichever happens to be the largest,
1098 * since the buffer needs to be big enough for a message and the
1099 * sockaddrs together.
gdtdbee01f2004-01-06 00:36:51 +00001100 */
paul718e3742002-12-13 20:15:29 +00001101 union
1102 {
1103 /* Routing information. */
1104 struct
1105 {
1106 struct rt_msghdr rtm;
gdtb27900b2004-01-08 15:44:29 +00001107 struct sockaddr_storage addr[RTAX_MAX];
paul718e3742002-12-13 20:15:29 +00001108 } r;
1109
1110 /* Interface information. */
1111 struct
1112 {
1113 struct if_msghdr ifm;
gdtb27900b2004-01-08 15:44:29 +00001114 struct sockaddr_storage addr[RTAX_MAX];
paul718e3742002-12-13 20:15:29 +00001115 } im;
1116
1117 /* Interface address information. */
1118 struct
1119 {
1120 struct ifa_msghdr ifa;
gdtb27900b2004-01-08 15:44:29 +00001121 struct sockaddr_storage addr[RTAX_MAX];
paul718e3742002-12-13 20:15:29 +00001122 } ia;
1123
1124#ifdef RTM_IFANNOUNCE
1125 /* Interface arrival/departure */
1126 struct
1127 {
1128 struct if_announcemsghdr ifan;
gdtb27900b2004-01-08 15:44:29 +00001129 struct sockaddr_storage addr[RTAX_MAX];
paul718e3742002-12-13 20:15:29 +00001130 } ian;
1131#endif /* RTM_IFANNOUNCE */
1132
1133 } buf;
1134
1135 /* Fetch routing socket. */
1136 sock = THREAD_FD (thread);
1137
1138 nbytes= read (sock, &buf, sizeof buf);
1139
1140 if (nbytes <= 0)
1141 {
1142 if (nbytes < 0 && errno != EWOULDBLOCK && errno != EAGAIN)
ajs6099b3b2004-11-20 02:06:59 +00001143 zlog_warn ("routing socket error: %s", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001144 return 0;
1145 }
1146
paul9bcdb632003-07-08 08:09:45 +00001147 thread_add_read (zebrad.master, kernel_read, NULL, sock);
paul718e3742002-12-13 20:15:29 +00001148
hasso726f9b22003-05-25 21:04:54 +00001149 if (IS_ZEBRA_DEBUG_KERNEL)
1150 rtmsg_debug (&buf.r.rtm);
paul718e3742002-12-13 20:15:29 +00001151
1152 rtm = &buf.r.rtm;
1153
gdtb27900b2004-01-08 15:44:29 +00001154 /*
1155 * Ensure that we didn't drop any data, so that processing routines
1156 * can assume they have the whole message.
1157 */
gdtda26e3b2004-01-05 17:20:59 +00001158 if (rtm->rtm_msglen != nbytes)
1159 {
1160 zlog_warn ("kernel_read: rtm->rtm_msglen %d, nbytes %d, type %d\n",
1161 rtm->rtm_msglen, nbytes, rtm->rtm_type);
1162 return -1;
1163 }
1164
paul718e3742002-12-13 20:15:29 +00001165 switch (rtm->rtm_type)
1166 {
1167 case RTM_ADD:
1168 case RTM_DELETE:
paulca162182005-09-12 16:58:52 +00001169 case RTM_CHANGE:
paul718e3742002-12-13 20:15:29 +00001170 rtm_read (rtm);
1171 break;
1172 case RTM_IFINFO:
1173 ifm_read (&buf.im.ifm);
1174 break;
1175 case RTM_NEWADDR:
1176 case RTM_DELADDR:
1177 ifam_read (&buf.ia.ifa);
1178 break;
1179#ifdef RTM_IFANNOUNCE
1180 case RTM_IFANNOUNCE:
1181 ifan_read (&buf.ian.ifan);
1182 break;
1183#endif /* RTM_IFANNOUNCE */
1184 default:
hasso726f9b22003-05-25 21:04:54 +00001185 if (IS_ZEBRA_DEBUG_KERNEL)
ajsb6178002004-12-07 21:12:56 +00001186 zlog_debug("Unprocessed RTM_type: %d", rtm->rtm_type);
paul718e3742002-12-13 20:15:29 +00001187 break;
1188 }
1189 return 0;
1190}
1191
1192/* Make routing socket. */
paul6621ca82005-11-23 13:02:08 +00001193static void
1194routing_socket (void)
paul718e3742002-12-13 20:15:29 +00001195{
pauledd7c242003-06-04 13:59:38 +00001196 if ( zserv_privs.change (ZPRIVS_RAISE) )
1197 zlog_err ("routing_socket: Can't raise privileges");
1198
paul718e3742002-12-13 20:15:29 +00001199 routing_sock = socket (AF_ROUTE, SOCK_RAW, 0);
1200
1201 if (routing_sock < 0)
1202 {
pauledd7c242003-06-04 13:59:38 +00001203 if ( zserv_privs.change (ZPRIVS_LOWER) )
1204 zlog_err ("routing_socket: Can't lower privileges");
paul718e3742002-12-13 20:15:29 +00001205 zlog_warn ("Can't init kernel routing socket");
1206 return;
1207 }
1208
paul865b8522005-01-05 08:30:35 +00001209 /* XXX: Socket should be NONBLOCK, however as we currently
1210 * discard failed writes, this will lead to inconsistencies.
1211 * For now, socket must be blocking.
1212 */
1213 /*if (fcntl (routing_sock, F_SETFL, O_NONBLOCK) < 0)
1214 zlog_warn ("Can't set O_NONBLOCK to routing socket");*/
1215
pauledd7c242003-06-04 13:59:38 +00001216 if ( zserv_privs.change (ZPRIVS_LOWER) )
1217 zlog_err ("routing_socket: Can't lower privileges");
paul718e3742002-12-13 20:15:29 +00001218
1219 /* kernel_read needs rewrite. */
paul9bcdb632003-07-08 08:09:45 +00001220 thread_add_read (zebrad.master, kernel_read, NULL, routing_sock);
paul718e3742002-12-13 20:15:29 +00001221}
1222
1223/* Exported interface function. This function simply calls
1224 routing_socket (). */
1225void
paul6621ca82005-11-23 13:02:08 +00001226kernel_init (void)
paul718e3742002-12-13 20:15:29 +00001227{
1228 routing_socket ();
1229}