blob: 903d67908ad98440e6324274a970dd520ed7fda0 [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"
39
pauledd7c242003-06-04 13:59:38 +000040extern struct zebra_privs_t zserv_privs;
paul9bcdb632003-07-08 08:09:45 +000041extern struct zebra_t zebrad;
pauledd7c242003-06-04 13:59:38 +000042
gdt4bfbea82004-01-06 01:13:05 +000043/*
44 * Given a sockaddr length, round it up to include pad bytes following
45 * it. Assumes the kernel pads to sizeof(long).
46 *
47 * XXX: why is ROUNDUP(0) sizeof(long)? 0 is an illegal sockaddr
48 * length anyway (< sizeof (struct sockaddr)), so this shouldn't
49 * matter.
50 */
paul718e3742002-12-13 20:15:29 +000051#define ROUNDUP(a) \
52 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
53
gdt4bfbea82004-01-06 01:13:05 +000054/*
55 * Given a pointer (sockaddr or void *), return the number of bytes
56 * taken up by the sockaddr and any padding needed for alignment.
57 */
paul30be8022003-10-22 02:51:38 +000058#if defined(HAVE_SA_LEN)
gdt4bfbea82004-01-06 01:13:05 +000059#define SAROUNDUP(X) ROUNDUP(((struct sockaddr *)(X))->sa_len)
paul30be8022003-10-22 02:51:38 +000060#elif defined(HAVE_IPV6)
gdt4bfbea82004-01-06 01:13:05 +000061/*
62 * One would hope all fixed-size structure definitions are aligned,
63 * but round them up nonetheless.
64 */
65#define SAROUNDUP(X) \
paul3e95a072003-09-24 00:05:45 +000066 (((struct sockaddr *)(X))->sa_family == AF_INET ? \
67 ROUNDUP(sizeof(struct sockaddr_in)):\
68 (((struct sockaddr *)(X))->sa_family == AF_INET6 ? \
69 ROUNDUP(sizeof(struct sockaddr_in6)) : \
70 (((struct sockaddr *)(X))->sa_family == AF_LINK ? \
paulc50ae8b2004-05-11 11:31:07 +000071 ROUNDUP(sizeof(struct sockaddr_dl)) : sizeof(struct sockaddr))))
paul30be8022003-10-22 02:51:38 +000072#else /* HAVE_IPV6 */
gdt4bfbea82004-01-06 01:13:05 +000073#define SAROUNDUP(X) \
paul30be8022003-10-22 02:51:38 +000074 (((struct sockaddr *)(X))->sa_family == AF_INET ? \
75 ROUNDUP(sizeof(struct sockaddr_in)):\
76 (((struct sockaddr *)(X))->sa_family == AF_LINK ? \
77 ROUNDUP(sizeof(struct sockaddr_dl)) : sizeof(struct sockaddr)))
paul718e3742002-12-13 20:15:29 +000078#endif /* HAVE_SA_LEN */
79
paul62debbb2005-06-14 14:07:07 +000080#define RTA_ADDR_GET(DEST, RTA, RTMADDRS, PNT) \
81 if ((RTMADDRS) & (RTA)) \
82 { \
83 int len = SAROUNDUP ((PNT)); \
paulea6f82b2005-06-28 17:20:26 +000084 if ( ((DEST) != NULL) && \
paul62debbb2005-06-14 14:07:07 +000085 af_check (((struct sockaddr *)(PNT))->sa_family)) \
paul6fe70d12005-11-12 22:55:10 +000086 memcpy ((DEST), (PNT), len); \
paul62debbb2005-06-14 14:07:07 +000087 (PNT) += len; \
88 }
89#define RTA_ATTR_GET(DEST, RTA, RTMADDRS, PNT) \
90 if ((RTMADDRS) & (RTA)) \
91 { \
92 int len = SAROUNDUP ((PNT)); \
paulea6f82b2005-06-28 17:20:26 +000093 if ( ((DEST) != NULL) ) \
paul6fe70d12005-11-12 22:55:10 +000094 memcpy ((DEST), (PNT), len); \
paul62debbb2005-06-14 14:07:07 +000095 (PNT) += len; \
96 }
97
paul6fe70d12005-11-12 22:55:10 +000098#define RTA_NAME_GET(DEST, RTA, RTMADDRS, PNT, LEN) \
99 if ((RTMADDRS) & (RTA)) \
100 { \
101 int len = SAROUNDUP ((PNT)); \
102 struct sockaddr_dl *sdl = (struct sockaddr_dl *)(PNT); \
103 if (IS_ZEBRA_DEBUG_KERNEL) \
104 zlog_debug ("%s: RTA_SDL_GET nlen %d, alen %d", \
105 __func__, sdl->sdl_nlen, sdl->sdl_alen); \
106 if ( ((DEST) != NULL) && (sdl->sdl_family == AF_LINK) \
107 && (sdl->sdl_nlen < IFNAMSIZ) && (sdl->sdl_nlen <= len) ) \
108 { \
109 memcpy ((DEST), sdl->sdl_data, sdl->sdl_nlen); \
110 (DEST)[sdl->sdl_nlen] = '\0'; \
111 (LEN) = sdl->sdl_nlen; \
112 } \
113 (PNT) += len; \
114 } \
115 else \
116 { \
117 (LEN) = 0; \
118 }
paul718e3742002-12-13 20:15:29 +0000119/* Routing socket message types. */
120struct message rtm_type_str[] =
121{
122 {RTM_ADD, "RTM_ADD"},
123 {RTM_DELETE, "RTM_DELETE"},
124 {RTM_CHANGE, "RTM_CHANGE"},
125 {RTM_GET, "RTM_GET"},
126 {RTM_LOSING, "RTM_LOSING"},
127 {RTM_REDIRECT, "RTM_REDIRECT"},
128 {RTM_MISS, "RTM_MISS"},
129 {RTM_LOCK, "RTM_LOCK"},
130 {RTM_OLDADD, "RTM_OLDADD"},
131 {RTM_OLDDEL, "RTM_OLDDEL"},
132 {RTM_RESOLVE, "RTM_RESOLVE"},
133 {RTM_NEWADDR, "RTM_NEWADDR"},
134 {RTM_DELADDR, "RTM_DELADDR"},
135 {RTM_IFINFO, "RTM_IFINFO"},
136#ifdef RTM_OIFINFO
137 {RTM_OIFINFO, "RTM_OIFINFO"},
138#endif /* RTM_OIFINFO */
139#ifdef RTM_NEWMADDR
140 {RTM_NEWMADDR, "RTM_NEWMADDR"},
141#endif /* RTM_NEWMADDR */
142#ifdef RTM_DELMADDR
143 {RTM_DELMADDR, "RTM_DELMADDR"},
144#endif /* RTM_DELMADDR */
145#ifdef RTM_IFANNOUNCE
146 {RTM_IFANNOUNCE, "RTM_IFANNOUNCE"},
147#endif /* RTM_IFANNOUNCE */
148 {0, NULL}
149};
150
151struct message rtm_flag_str[] =
152{
153 {RTF_UP, "UP"},
154 {RTF_GATEWAY, "GATEWAY"},
155 {RTF_HOST, "HOST"},
156 {RTF_REJECT, "REJECT"},
157 {RTF_DYNAMIC, "DYNAMIC"},
158 {RTF_MODIFIED, "MODIFIED"},
159 {RTF_DONE, "DONE"},
160#ifdef RTF_MASK
161 {RTF_MASK, "MASK"},
162#endif /* RTF_MASK */
163 {RTF_CLONING, "CLONING"},
164 {RTF_XRESOLVE, "XRESOLVE"},
165 {RTF_LLINFO, "LLINFO"},
166 {RTF_STATIC, "STATIC"},
167 {RTF_BLACKHOLE, "BLACKHOLE"},
paul6fe70d12005-11-12 22:55:10 +0000168#ifdef RTF_PRIVATE
169 {RTF_PRIVATE, "PRIVATE"},
170#endif /* RTF_PRIVATE */
paul718e3742002-12-13 20:15:29 +0000171 {RTF_PROTO1, "PROTO1"},
172 {RTF_PROTO2, "PROTO2"},
173#ifdef RTF_PRCLONING
174 {RTF_PRCLONING, "PRCLONING"},
175#endif /* RTF_PRCLONING */
176#ifdef RTF_WASCLONED
177 {RTF_WASCLONED, "WASCLONED"},
178#endif /* RTF_WASCLONED */
179#ifdef RTF_PROTO3
180 {RTF_PROTO3, "PROTO3"},
181#endif /* RTF_PROTO3 */
182#ifdef RTF_PINNED
183 {RTF_PINNED, "PINNED"},
184#endif /* RTF_PINNED */
185#ifdef RTF_LOCAL
186 {RTF_LOCAL, "LOCAL"},
187#endif /* RTF_LOCAL */
188#ifdef RTF_BROADCAST
189 {RTF_BROADCAST, "BROADCAST"},
190#endif /* RTF_BROADCAST */
191#ifdef RTF_MULTICAST
192 {RTF_MULTICAST, "MULTICAST"},
193#endif /* RTF_MULTICAST */
paul6fe70d12005-11-12 22:55:10 +0000194#ifdef RTF_MULTIRT
195 {RTF_MULTIRT, "MULTIRT"},
196#endif /* RTF_MULTIRT */
197#ifdef RTF_SETSRC
198 {RTF_SETSRC, "SETSRC"},
199#endif /* RTF_SETSRC */
paul718e3742002-12-13 20:15:29 +0000200 {0, NULL}
201};
202
203/* Kernel routing update socket. */
204int routing_sock = -1;
205
206/* Yes I'm checking ugly routing socket behavior. */
207/* #define DEBUG */
208
209/* Supported address family check. */
paul62debbb2005-06-14 14:07:07 +0000210static int inline
paul718e3742002-12-13 20:15:29 +0000211af_check (int family)
212{
213 if (family == AF_INET)
214 return 1;
215#ifdef HAVE_IPV6
216 if (family == AF_INET6)
217 return 1;
218#endif /* HAVE_IPV6 */
219 return 0;
220}
221
222/* Dump routing table flag for debug purpose. */
ajsb6178002004-12-07 21:12:56 +0000223static void
paul718e3742002-12-13 20:15:29 +0000224rtm_flag_dump (int flag)
225{
226 struct message *mes;
227 static char buf[BUFSIZ];
228
gdtcced60d2004-07-13 16:45:54 +0000229 buf[0] = '\0';
paul718e3742002-12-13 20:15:29 +0000230 for (mes = rtm_flag_str; mes->key != 0; mes++)
231 {
232 if (mes->key & flag)
233 {
234 strlcat (buf, mes->str, BUFSIZ);
235 strlcat (buf, " ", BUFSIZ);
236 }
237 }
ajsb6178002004-12-07 21:12:56 +0000238 zlog_debug ("Kernel: %s", buf);
paul718e3742002-12-13 20:15:29 +0000239}
240
241#ifdef RTM_IFANNOUNCE
242/* Interface adding function */
paul6621ca82005-11-23 13:02:08 +0000243static int
paul718e3742002-12-13 20:15:29 +0000244ifan_read (struct if_announcemsghdr *ifan)
245{
246 struct interface *ifp;
paul6fe70d12005-11-12 22:55:10 +0000247
paul718e3742002-12-13 20:15:29 +0000248 ifp = if_lookup_by_index (ifan->ifan_index);
paul6fe70d12005-11-12 22:55:10 +0000249
250 if (ifp)
251 assert ( (ifp->ifindex == ifan->ifan_index)
252 || (ifp->ifindex == IFINDEX_INTERNAL) );
253
paul6621ca82005-11-23 13:02:08 +0000254 if ( (ifp == NULL) || (ifp->ifindex == IFINDEX_INTERNAL)
255 && (ifan->ifan_what == IFAN_ARRIVAL) )
paul718e3742002-12-13 20:15:29 +0000256 {
paul6fe70d12005-11-12 22:55:10 +0000257 if (IS_ZEBRA_DEBUG_KERNEL)
258 zlog_debug ("%s: creating interface for ifindex %d, name %s",
259 __func__, ifan->ifan_index, ifan->ifan_name);
260
paul718e3742002-12-13 20:15:29 +0000261 /* Create Interface */
ajs08dbfb62005-04-03 03:40:52 +0000262 ifp = if_get_by_name_len(ifan->ifan_name,
263 strnlen(ifan->ifan_name,
264 sizeof(ifan->ifan_name)));
paul718e3742002-12-13 20:15:29 +0000265 ifp->ifindex = ifan->ifan_index;
266
267 if_add_update (ifp);
268 }
269 else if (ifp != NULL && ifan->ifan_what == IFAN_DEPARTURE)
paul6eb88272005-07-29 14:36:00 +0000270 if_delete_update (ifp);
paul718e3742002-12-13 20:15:29 +0000271
272 if_get_flags (ifp);
273 if_get_mtu (ifp);
274 if_get_metric (ifp);
275
276 if (IS_ZEBRA_DEBUG_KERNEL)
paul6fe70d12005-11-12 22:55:10 +0000277 zlog_debug ("%s: interface %s index %d",
278 __func__, ifan->ifan_name, ifan->ifan_index);
paul718e3742002-12-13 20:15:29 +0000279
280 return 0;
281}
282#endif /* RTM_IFANNOUNCE */
283
gdtda26e3b2004-01-05 17:20:59 +0000284/*
285 * Handle struct if_msghdr obtained from reading routing socket or
286 * sysctl (from interface_list). There may or may not be sockaddrs
287 * present after the header.
288 */
paul6621ca82005-11-23 13:02:08 +0000289static int
paul718e3742002-12-13 20:15:29 +0000290ifm_read (struct if_msghdr *ifm)
291{
paul3e95a072003-09-24 00:05:45 +0000292 struct interface *ifp = NULL;
paul6fe70d12005-11-12 22:55:10 +0000293 char ifname[IFNAMSIZ];
294 short ifnlen = 0;
paul0994c3a2005-11-11 09:52:40 +0000295 caddr_t *cp;
paul6fe70d12005-11-12 22:55:10 +0000296
297 /* terminate ifname at head (for strnlen) and tail (for safety) */
298 ifname[IFNAMSIZ - 1] = '\0';
299
gdtda26e3b2004-01-05 17:20:59 +0000300 /* paranoia: sanity check structure */
301 if (ifm->ifm_msglen < sizeof(struct if_msghdr))
302 {
303 zlog_err ("ifm_read: ifm->ifm_msglen %d too short\n",
304 ifm->ifm_msglen);
305 return -1;
306 }
307
308 /*
gdt4bfbea82004-01-06 01:13:05 +0000309 * Check for a sockaddr_dl following the message. First, point to
310 * where a socakddr might be if one follows the message.
gdtda26e3b2004-01-05 17:20:59 +0000311 */
gdt4bfbea82004-01-06 01:13:05 +0000312 cp = (void *)(ifm + 1);
313
paul3e95a072003-09-24 00:05:45 +0000314#ifdef SUNOS_5
paul3e95a072003-09-24 00:05:45 +0000315 /*
gdt4bfbea82004-01-06 01:13:05 +0000316 * XXX This behavior should be narrowed to only the kernel versions
317 * for which the structures returned do not match the headers.
318 *
paul3e95a072003-09-24 00:05:45 +0000319 * if_msghdr_t on 64 bit kernels in Solaris 9 and earlier versions
gdt4bfbea82004-01-06 01:13:05 +0000320 * is 12 bytes larger than the 32 bit version.
paul3e95a072003-09-24 00:05:45 +0000321 */
gdt4bfbea82004-01-06 01:13:05 +0000322 if (((struct sockaddr *) cp)->sa_family == AF_UNSPEC)
paul3e95a072003-09-24 00:05:45 +0000323 cp = cp + 12;
paul3e95a072003-09-24 00:05:45 +0000324#endif
paul718e3742002-12-13 20:15:29 +0000325
paul6fe70d12005-11-12 22:55:10 +0000326 RTA_ADDR_GET (NULL, RTA_DST, ifm->ifm_addrs, cp);
327 RTA_ADDR_GET (NULL, RTA_GATEWAY, ifm->ifm_addrs, cp);
328 RTA_ATTR_GET (NULL, RTA_NETMASK, ifm->ifm_addrs, cp);
329 RTA_ADDR_GET (NULL, RTA_GENMASK, ifm->ifm_addrs, cp);
330 RTA_NAME_GET (ifname, RTA_IFP, ifm->ifm_addrs, cp, ifnlen);
331 RTA_ADDR_GET (NULL, RTA_IFA, ifm->ifm_addrs, cp);
332 RTA_ADDR_GET (NULL, RTA_AUTHOR, ifm->ifm_addrs, cp);
333 RTA_ADDR_GET (NULL, RTA_BRD, ifm->ifm_addrs, cp);
334
335 if (IS_ZEBRA_DEBUG_KERNEL)
336 zlog_debug ("%s: sdl ifname %s", __func__, (ifnlen ? ifname : "(nil)"));
337
gdt4bfbea82004-01-06 01:13:05 +0000338 /*
paul6fe70d12005-11-12 22:55:10 +0000339 * Look up on ifindex first, because ifindices are the primary handle for
340 * interfaces across the user/kernel boundary, for most systems. (Some
341 * messages, such as up/down status changes on NetBSD, do not include a
342 * sockaddr_dl).
gdt4bfbea82004-01-06 01:13:05 +0000343 */
paul6fe70d12005-11-12 22:55:10 +0000344 if ( (ifp = if_lookup_by_index (ifm->ifm_index)) != NULL )
gdt4bfbea82004-01-06 01:13:05 +0000345 {
paul6fe70d12005-11-12 22:55:10 +0000346 /* we have an ifp, verify that the name matches as some systems,
347 * eg Solaris, have a 1:many association of ifindex:ifname
348 * if they dont match, we dont have the correct ifp and should
349 * set it back to NULL to let next check do lookup by name
350 */
351 if (ifnlen && (strncmp (ifp->name, ifname, IFNAMSIZ) != 0) )
gdt4bfbea82004-01-06 01:13:05 +0000352 {
paul6fe70d12005-11-12 22:55:10 +0000353 if (IS_ZEBRA_DEBUG_KERNEL)
354 zlog_debug ("%s: ifp name %s doesnt match sdl name %s",
355 __func__, ifp->name, ifname);
356 ifp = NULL;
gdt4bfbea82004-01-06 01:13:05 +0000357 }
358 }
paul6fe70d12005-11-12 22:55:10 +0000359
gdtda26e3b2004-01-05 17:20:59 +0000360 /*
paul6fe70d12005-11-12 22:55:10 +0000361 * If we dont have an ifp, try looking up by name. Particularly as some
362 * systems (Solaris) have a 1:many mapping of ifindex:ifname - the ifname
363 * is therefore our unique handle to that interface.
364 *
365 * Interfaces specified in the configuration file for which the ifindex
366 * has not been determined will have ifindex == IFINDEX_INTERNAL, and such
367 * interfaces are found by this search, and then their ifindex values can
368 * be filled in.
gdtda26e3b2004-01-05 17:20:59 +0000369 */
paul6fe70d12005-11-12 22:55:10 +0000370 if ( (ifp == NULL) && ifnlen)
371 ifp = if_lookup_by_name (ifname);
paul718e3742002-12-13 20:15:29 +0000372
gdtda26e3b2004-01-05 17:20:59 +0000373 /*
paul6fe70d12005-11-12 22:55:10 +0000374 * If ifp still does not exist or has an invalid index (IFINDEX_INTERNAL),
375 * create or fill in an interface.
gdtda26e3b2004-01-05 17:20:59 +0000376 */
ajsd2fc8892005-04-02 18:38:43 +0000377 if ((ifp == NULL) || (ifp->ifindex == IFINDEX_INTERNAL))
paul718e3742002-12-13 20:15:29 +0000378 {
gdt4bfbea82004-01-06 01:13:05 +0000379 /*
380 * To create or fill in an interface, a sockaddr_dl (via
381 * RTA_IFP) is required.
382 */
paul6fe70d12005-11-12 22:55:10 +0000383 if (!ifnlen)
paul718e3742002-12-13 20:15:29 +0000384 {
paul6fe70d12005-11-12 22:55:10 +0000385 zlog_warn ("Interface index %d (new) missing ifname\n",
paul718e3742002-12-13 20:15:29 +0000386 ifm->ifm_index);
387 return -1;
388 }
paul6fe70d12005-11-12 22:55:10 +0000389
paul3e95a072003-09-24 00:05:45 +0000390 if (ifp == NULL)
paul6fe70d12005-11-12 22:55:10 +0000391 {
392 /* Interface that zebra was not previously aware of, so create. */
393 ifp = if_create (ifname, ifnlen);
394 if (IS_ZEBRA_DEBUG_KERNEL)
395 zlog_debug ("%s: creating ifp for ifindex %d",
396 __func__, ifm->ifm_index);
397 }
paul718e3742002-12-13 20:15:29 +0000398
paul6fe70d12005-11-12 22:55:10 +0000399 if (IS_ZEBRA_DEBUG_KERNEL)
400 zlog_debug ("%s: updated/created ifp, ifname %s, ifindex %d",
401 __func__, ifp->name, ifp->ifindex);
gdt4bfbea82004-01-06 01:13:05 +0000402 /*
403 * Fill in newly created interface structure, or larval
ajsd2fc8892005-04-02 18:38:43 +0000404 * structure with ifindex IFINDEX_INTERNAL.
gdt4bfbea82004-01-06 01:13:05 +0000405 */
paul718e3742002-12-13 20:15:29 +0000406 ifp->ifindex = ifm->ifm_index;
407 ifp->flags = ifm->ifm_flags;
408#if defined(__bsdi__)
409 if_kvm_get_mtu (ifp);
410#else
411 if_get_mtu (ifp);
412#endif /* __bsdi__ */
413 if_get_metric (ifp);
414
paul718e3742002-12-13 20:15:29 +0000415 if_add_update (ifp);
416 }
417 else
gdtda26e3b2004-01-05 17:20:59 +0000418 /*
419 * Interface structure exists. Adjust stored flags from
420 * notification. If interface has up->down or down->up
421 * transition, call state change routines (to adjust routes,
422 * notify routing daemons, etc.). (Other flag changes are stored
423 * but apparently do not trigger action.)
424 */
paul718e3742002-12-13 20:15:29 +0000425 {
paul6fe70d12005-11-12 22:55:10 +0000426 if (ifp->ifindex != ifm->ifm_index)
427 {
428 zlog_warn ("%s: index mismatch, ifname %s, ifp index %d, "
429 "ifm index %d",
430 __func__, ifp->name, ifp->ifindex, ifm->ifm_index);
431 return -1;
432 }
433
paul718e3742002-12-13 20:15:29 +0000434 if (if_is_up (ifp))
435 {
436 ifp->flags = ifm->ifm_flags;
437 if (! if_is_up (ifp))
paul6eb88272005-07-29 14:36:00 +0000438 {
439 if_down (ifp);
440#ifndef RTM_IFANNOUNCE
441 /* No RTM_IFANNOUNCE on this platform, so we can never
442 * distinguish between down and delete. We must presume
443 * it has been deleted.
444 * Eg, Solaris will not notify us of unplumb.
445 *
446 * XXX: Fixme - this should be runtime detected
447 * So that a binary compiled on a system with IFANNOUNCE
448 * will still behave correctly if run on a platform without
449 */
450 if_delete_update (ifp);
451#endif /* RTM_IFANNOUNCE */
452 }
paul718e3742002-12-13 20:15:29 +0000453 }
454 else
455 {
456 ifp->flags = ifm->ifm_flags;
457 if (if_is_up (ifp))
458 if_up (ifp);
459 }
460 }
461
462#ifdef HAVE_NET_RT_IFLIST
463 ifp->stats = ifm->ifm_data;
464#endif /* HAVE_NET_RT_IFLIST */
465
466 if (IS_ZEBRA_DEBUG_KERNEL)
paul6fe70d12005-11-12 22:55:10 +0000467 zlog_debug ("%s: interface %s index %d",
468 __func__, ifp->name, ifp->ifindex);
paul718e3742002-12-13 20:15:29 +0000469
470 return 0;
471}
472
473/* Address read from struct ifa_msghdr. */
paul6621ca82005-11-23 13:02:08 +0000474static void
paul718e3742002-12-13 20:15:29 +0000475ifam_read_mesg (struct ifa_msghdr *ifm,
476 union sockunion *addr,
477 union sockunion *mask,
paul6fe70d12005-11-12 22:55:10 +0000478 union sockunion *brd,
479 char *ifname,
480 short *ifnlen)
paul718e3742002-12-13 20:15:29 +0000481{
482 caddr_t pnt, end;
483
484 pnt = (caddr_t)(ifm + 1);
485 end = ((caddr_t)ifm) + ifm->ifam_msglen;
486
paul718e3742002-12-13 20:15:29 +0000487 /* Be sure structure is cleared */
488 memset (mask, 0, sizeof (union sockunion));
489 memset (addr, 0, sizeof (union sockunion));
paul6621ca82005-11-23 13:02:08 +0000490 memset (brd, 0, sizeof (union sockunion));
paul718e3742002-12-13 20:15:29 +0000491
492 /* We fetch each socket variable into sockunion. */
paul62debbb2005-06-14 14:07:07 +0000493 RTA_ADDR_GET (NULL, RTA_DST, ifm->ifam_addrs, pnt);
494 RTA_ADDR_GET (NULL, RTA_GATEWAY, ifm->ifam_addrs, pnt);
495 RTA_ATTR_GET (mask, RTA_NETMASK, ifm->ifam_addrs, pnt);
496 RTA_ADDR_GET (NULL, RTA_GENMASK, ifm->ifam_addrs, pnt);
paul6fe70d12005-11-12 22:55:10 +0000497 RTA_NAME_GET (ifname, RTA_IFP, ifm->ifam_addrs, pnt, *ifnlen);
paul62debbb2005-06-14 14:07:07 +0000498 RTA_ADDR_GET (addr, RTA_IFA, ifm->ifam_addrs, pnt);
499 RTA_ADDR_GET (NULL, RTA_AUTHOR, ifm->ifam_addrs, pnt);
paul6fe70d12005-11-12 22:55:10 +0000500 RTA_ADDR_GET (brd, RTA_BRD, ifm->ifam_addrs, pnt);
paul718e3742002-12-13 20:15:29 +0000501
paul6fe70d12005-11-12 22:55:10 +0000502 if (IS_ZEBRA_DEBUG_KERNEL)
503 zlog_debug ("%s: ifindex %d, ifname %s, ifam_addrs 0x%x",
504 __func__, ifm->ifam_index,
505 (ifnlen ? ifname : "(nil)"), ifm->ifam_addrs);
506
paul718e3742002-12-13 20:15:29 +0000507 /* Assert read up end point matches to end point */
508 if (pnt != end)
509 zlog_warn ("ifam_read() does't read all socket data");
510}
511
512/* Interface's address information get. */
paul6621ca82005-11-23 13:02:08 +0000513static int
paul718e3742002-12-13 20:15:29 +0000514ifam_read (struct ifa_msghdr *ifam)
515{
paul6fe70d12005-11-12 22:55:10 +0000516 struct interface *ifp = NULL;
paul0752ef02005-11-03 12:35:21 +0000517 union sockunion addr, mask, brd;
paul6fe70d12005-11-12 22:55:10 +0000518 char ifname[INTERFACE_NAMSIZ];
519 short ifnlen = 0;
520 char isalias = 0;
521
522 ifname[0] = ifname[INTERFACE_NAMSIZ - 1] = '\0';
523
524 /* Allocate and read address information. */
525 ifam_read_mesg (ifam, &addr, &mask, &brd, ifname, &ifnlen);
526
527 if ((ifp = if_lookup_by_index(ifam->ifam_index)) == NULL)
paul718e3742002-12-13 20:15:29 +0000528 {
paul6fe70d12005-11-12 22:55:10 +0000529 zlog_warn ("%s: no interface for ifname %s, index %d",
530 __func__, ifname, ifam->ifam_index);
paul718e3742002-12-13 20:15:29 +0000531 return -1;
532 }
paul6fe70d12005-11-12 22:55:10 +0000533
534 if (ifnlen && strncmp (ifp->name, ifname, INTERFACE_NAMSIZ))
535 isalias = 1;
536
paul718e3742002-12-13 20:15:29 +0000537 /* Check interface flag for implicit up of the interface. */
538 if_refresh (ifp);
539
540 /* Add connected address. */
541 switch (sockunion_family (&addr))
542 {
543 case AF_INET:
544 if (ifam->ifam_type == RTM_NEWADDR)
545 connected_add_ipv4 (ifp, 0, &addr.sin.sin_addr,
546 ip_masklen (mask.sin.sin_addr),
paul0994c3a2005-11-11 09:52:40 +0000547 &brd.sin.sin_addr, NULL);
paul718e3742002-12-13 20:15:29 +0000548 else
549 connected_delete_ipv4 (ifp, 0, &addr.sin.sin_addr,
550 ip_masklen (mask.sin.sin_addr),
paul0752ef02005-11-03 12:35:21 +0000551 &brd.sin.sin_addr);
paul718e3742002-12-13 20:15:29 +0000552 break;
553#ifdef HAVE_IPV6
554 case AF_INET6:
555 /* Unset interface index from link-local address when IPv6 stack
556 is KAME. */
557 if (IN6_IS_ADDR_LINKLOCAL (&addr.sin6.sin6_addr))
558 SET_IN6_LINKLOCAL_IFINDEX (addr.sin6.sin6_addr, 0);
559
560 if (ifam->ifam_type == RTM_NEWADDR)
561 connected_add_ipv6 (ifp,
562 &addr.sin6.sin6_addr,
563 ip6_masklen (mask.sin6.sin6_addr),
paul0994c3a2005-11-11 09:52:40 +0000564 &brd.sin6.sin6_addr, NULL);
paul718e3742002-12-13 20:15:29 +0000565 else
566 connected_delete_ipv6 (ifp,
567 &addr.sin6.sin6_addr,
568 ip6_masklen (mask.sin6.sin6_addr),
paul0752ef02005-11-03 12:35:21 +0000569 &brd.sin6.sin6_addr);
paul718e3742002-12-13 20:15:29 +0000570 break;
571#endif /* HAVE_IPV6 */
572 default:
573 /* Unsupported family silently ignore... */
574 break;
575 }
576 return 0;
577}
578
579/* Interface function for reading kernel routing table information. */
paul6621ca82005-11-23 13:02:08 +0000580static int
paul718e3742002-12-13 20:15:29 +0000581rtm_read_mesg (struct rt_msghdr *rtm,
582 union sockunion *dest,
583 union sockunion *mask,
paul6fe70d12005-11-12 22:55:10 +0000584 union sockunion *gate,
585 char *ifname,
586 short *ifnlen)
paul718e3742002-12-13 20:15:29 +0000587{
588 caddr_t pnt, end;
589
590 /* Pnt points out socket data start point. */
591 pnt = (caddr_t)(rtm + 1);
592 end = ((caddr_t)rtm) + rtm->rtm_msglen;
593
594 /* rt_msghdr version check. */
595 if (rtm->rtm_version != RTM_VERSION)
596 zlog (NULL, LOG_WARNING,
597 "Routing message version different %d should be %d."
598 "This may cause problem\n", rtm->rtm_version, RTM_VERSION);
paul62debbb2005-06-14 14:07:07 +0000599
paul718e3742002-12-13 20:15:29 +0000600 /* Be sure structure is cleared */
601 memset (dest, 0, sizeof (union sockunion));
602 memset (gate, 0, sizeof (union sockunion));
603 memset (mask, 0, sizeof (union sockunion));
604
605 /* We fetch each socket variable into sockunion. */
paul62debbb2005-06-14 14:07:07 +0000606 RTA_ADDR_GET (dest, RTA_DST, rtm->rtm_addrs, pnt);
607 RTA_ADDR_GET (gate, RTA_GATEWAY, rtm->rtm_addrs, pnt);
608 RTA_ATTR_GET (mask, RTA_NETMASK, rtm->rtm_addrs, pnt);
609 RTA_ADDR_GET (NULL, RTA_GENMASK, rtm->rtm_addrs, pnt);
paul6fe70d12005-11-12 22:55:10 +0000610 RTA_NAME_GET (ifname, RTA_IFP, rtm->rtm_addrs, pnt, *ifnlen);
paul62debbb2005-06-14 14:07:07 +0000611 RTA_ADDR_GET (NULL, RTA_IFA, rtm->rtm_addrs, pnt);
612 RTA_ADDR_GET (NULL, RTA_AUTHOR, rtm->rtm_addrs, pnt);
613 RTA_ADDR_GET (NULL, RTA_BRD, rtm->rtm_addrs, pnt);
paul718e3742002-12-13 20:15:29 +0000614
615 /* If there is netmask information set it's family same as
616 destination family*/
617 if (rtm->rtm_addrs & RTA_NETMASK)
618 mask->sa.sa_family = dest->sa.sa_family;
619
620 /* Assert read up to the end of pointer. */
621 if (pnt != end)
622 zlog (NULL, LOG_WARNING, "rtm_read() does't read all socket data.");
623
624 return rtm->rtm_flags;
625}
626
paul6621ca82005-11-23 13:02:08 +0000627static void
paul718e3742002-12-13 20:15:29 +0000628rtm_read (struct rt_msghdr *rtm)
629{
630 int flags;
631 u_char zebra_flags;
632 union sockunion dest, mask, gate;
paul6fe70d12005-11-12 22:55:10 +0000633 char ifname[INTERFACE_NAMSIZ + 1];
634 short ifnlen = 0;
paul718e3742002-12-13 20:15:29 +0000635
636 zebra_flags = 0;
637
638 /* Discard self send message. */
639 if (rtm->rtm_type != RTM_GET
640 && (rtm->rtm_pid == pid || rtm->rtm_pid == old_pid))
641 return;
642
643 /* Read destination and netmask and gateway from rtm message
644 structure. */
paul6fe70d12005-11-12 22:55:10 +0000645 flags = rtm_read_mesg (rtm, &dest, &mask, &gate, ifname, &ifnlen);
paul718e3742002-12-13 20:15:29 +0000646
647#ifdef RTF_CLONED /*bsdi, netbsd 1.6*/
648 if (flags & RTF_CLONED)
649 return;
650#endif
651#ifdef RTF_WASCLONED /*freebsd*/
652 if (flags & RTF_WASCLONED)
653 return;
654#endif
655
656 if ((rtm->rtm_type == RTM_ADD) && ! (flags & RTF_UP))
657 return;
658
659 /* This is connected route. */
660 if (! (flags & RTF_GATEWAY))
661 return;
662
663 if (flags & RTF_PROTO1)
664 SET_FLAG (zebra_flags, ZEBRA_FLAG_SELFROUTE);
665
666 /* This is persistent route. */
667 if (flags & RTF_STATIC)
668 SET_FLAG (zebra_flags, ZEBRA_FLAG_STATIC);
669
hasso81dfcaa2003-05-25 19:21:25 +0000670 /* This is a reject or blackhole route */
671 if (flags & RTF_REJECT)
672 SET_FLAG (zebra_flags, ZEBRA_FLAG_REJECT);
673 if (flags & RTF_BLACKHOLE)
674 SET_FLAG (zebra_flags, ZEBRA_FLAG_BLACKHOLE);
675
paul718e3742002-12-13 20:15:29 +0000676 if (dest.sa.sa_family == AF_INET)
677 {
678 struct prefix_ipv4 p;
679
680 p.family = AF_INET;
681 p.prefix = dest.sin.sin_addr;
682 if (flags & RTF_HOST)
683 p.prefixlen = IPV4_MAX_PREFIXLEN;
684 else
685 p.prefixlen = ip_masklen (mask.sin.sin_addr);
paulca162182005-09-12 16:58:52 +0000686
687 /* Change, delete the old prefix, we have no further information
688 * to specify the route really
689 */
690 if (rtm->rtm_type == RTM_CHANGE)
691 rib_delete_ipv4 (ZEBRA_ROUTE_KERNEL, zebra_flags, &p,
692 NULL, 0, 0);
693
694 if (rtm->rtm_type == RTM_GET
695 || rtm->rtm_type == RTM_ADD
696 || rtm->rtm_type == RTM_CHANGE)
paul718e3742002-12-13 20:15:29 +0000697 rib_add_ipv4 (ZEBRA_ROUTE_KERNEL, zebra_flags,
698 &p, &gate.sin.sin_addr, 0, 0, 0, 0);
699 else
700 rib_delete_ipv4 (ZEBRA_ROUTE_KERNEL, zebra_flags,
701 &p, &gate.sin.sin_addr, 0, 0);
702 }
703#ifdef HAVE_IPV6
704 if (dest.sa.sa_family == AF_INET6)
705 {
706 struct prefix_ipv6 p;
707 unsigned int ifindex = 0;
708
709 p.family = AF_INET6;
710 p.prefix = dest.sin6.sin6_addr;
711 if (flags & RTF_HOST)
712 p.prefixlen = IPV6_MAX_PREFIXLEN;
713 else
714 p.prefixlen = ip6_masklen (mask.sin6.sin6_addr);
715
716#ifdef KAME
717 if (IN6_IS_ADDR_LINKLOCAL (&gate.sin6.sin6_addr))
718 {
719 ifindex = IN6_LINKLOCAL_IFINDEX (gate.sin6.sin6_addr);
720 SET_IN6_LINKLOCAL_IFINDEX (gate.sin6.sin6_addr, 0);
721 }
722#endif /* KAME */
723
paulca162182005-09-12 16:58:52 +0000724 /* CHANGE: delete the old prefix, we have no further information
725 * to specify the route really
726 */
727 if (rtm->rtm_type == RTM_CHANGE)
paul6621ca82005-11-23 13:02:08 +0000728 rib_delete_ipv6 (ZEBRA_ROUTE_KERNEL, zebra_flags, &p,
paulca162182005-09-12 16:58:52 +0000729 NULL, 0, 0);
730
731 if (rtm->rtm_type == RTM_GET
732 || rtm->rtm_type == RTM_ADD
733 || rtm->rtm_type == RTM_CHANGE)
paul718e3742002-12-13 20:15:29 +0000734 rib_add_ipv6 (ZEBRA_ROUTE_KERNEL, zebra_flags,
hassobe61c4e2005-08-27 06:05:47 +0000735 &p, &gate.sin6.sin6_addr, ifindex, 0, 0, 0);
paul718e3742002-12-13 20:15:29 +0000736 else
737 rib_delete_ipv6 (ZEBRA_ROUTE_KERNEL, zebra_flags,
738 &p, &gate.sin6.sin6_addr, ifindex, 0);
739 }
740#endif /* HAVE_IPV6 */
741}
742
743/* Interface function for the kernel routing table updates. Support
paul6621ca82005-11-23 13:02:08 +0000744 * for RTM_CHANGE will be needed.
745 * Exported only for rt_socket.c
746 */
paul718e3742002-12-13 20:15:29 +0000747int
748rtm_write (int message,
749 union sockunion *dest,
750 union sockunion *mask,
751 union sockunion *gate,
752 unsigned int index,
753 int zebra_flags,
754 int metric)
755{
756 int ret;
757 caddr_t pnt;
758 struct interface *ifp;
759 struct sockaddr_in tmp_gate;
760#ifdef HAVE_IPV6
761 struct sockaddr_in6 tmp_gate6;
762#endif /* HAVE_IPV6 */
763
764 /* Sequencial number of routing message. */
765 static int msg_seq = 0;
766
767 /* Struct of rt_msghdr and buffer for storing socket's data. */
768 struct
769 {
770 struct rt_msghdr rtm;
771 char buf[512];
772 } msg;
773
774 memset (&tmp_gate, 0, sizeof (struct sockaddr_in));
775 tmp_gate.sin_family = AF_INET;
776#ifdef HAVE_SIN_LEN
777 tmp_gate.sin_len = sizeof (struct sockaddr_in);
778#endif /* HAVE_SIN_LEN */
779
780#ifdef HAVE_IPV6
781 memset (&tmp_gate6, 0, sizeof (struct sockaddr_in6));
782 tmp_gate6.sin6_family = AF_INET6;
783#ifdef SIN6_LEN
784 tmp_gate6.sin6_len = sizeof (struct sockaddr_in6);
785#endif /* SIN6_LEN */
786#endif /* HAVE_IPV6 */
787
788 if (routing_sock < 0)
789 return ZEBRA_ERR_EPERM;
790
791 /* Clear and set rt_msghdr values */
792 memset (&msg, 0, sizeof (struct rt_msghdr));
793 msg.rtm.rtm_version = RTM_VERSION;
794 msg.rtm.rtm_type = message;
795 msg.rtm.rtm_seq = msg_seq++;
796 msg.rtm.rtm_addrs = RTA_DST;
797 msg.rtm.rtm_addrs |= RTA_GATEWAY;
798 msg.rtm.rtm_flags = RTF_UP;
799 msg.rtm.rtm_index = index;
800
801 if (metric != 0)
802 {
803 msg.rtm.rtm_rmx.rmx_hopcount = metric;
804 msg.rtm.rtm_inits |= RTV_HOPCOUNT;
805 }
806
807 ifp = if_lookup_by_index (index);
808
809 if (gate && message == RTM_ADD)
810 msg.rtm.rtm_flags |= RTF_GATEWAY;
811
812 if (! gate && message == RTM_ADD && ifp &&
813 (ifp->flags & IFF_POINTOPOINT) == 0)
814 msg.rtm.rtm_flags |= RTF_CLONING;
815
816 /* If no protocol specific gateway is specified, use link
817 address for gateway. */
818 if (! gate)
819 {
820 if (!ifp)
821 {
822 zlog_warn ("no gateway found for interface index %d", index);
823 return -1;
824 }
825 gate = (union sockunion *) & ifp->sdl;
826 }
827
828 if (mask)
829 msg.rtm.rtm_addrs |= RTA_NETMASK;
830 else if (message == RTM_ADD)
831 msg.rtm.rtm_flags |= RTF_HOST;
832
833 /* Tagging route with flags */
834 msg.rtm.rtm_flags |= (RTF_PROTO1);
835
836 /* Additional flags. */
837 if (zebra_flags & ZEBRA_FLAG_BLACKHOLE)
838 msg.rtm.rtm_flags |= RTF_BLACKHOLE;
hasso81dfcaa2003-05-25 19:21:25 +0000839 if (zebra_flags & ZEBRA_FLAG_REJECT)
840 msg.rtm.rtm_flags |= RTF_REJECT;
841
paul718e3742002-12-13 20:15:29 +0000842
843#ifdef HAVE_SIN_LEN
844#define SOCKADDRSET(X,R) \
845 if (msg.rtm.rtm_addrs & (R)) \
846 { \
847 int len = ROUNDUP ((X)->sa.sa_len); \
848 memcpy (pnt, (caddr_t)(X), len); \
849 pnt += len; \
850 }
851#else
852#define SOCKADDRSET(X,R) \
853 if (msg.rtm.rtm_addrs & (R)) \
854 { \
paul6fe70d12005-11-12 22:55:10 +0000855 int len = SAROUNDUP (X); \
paul718e3742002-12-13 20:15:29 +0000856 memcpy (pnt, (caddr_t)(X), len); \
857 pnt += len; \
858 }
859#endif /* HAVE_SIN_LEN */
860
861 pnt = (caddr_t) msg.buf;
862
863 /* Write each socket data into rtm message buffer */
864 SOCKADDRSET (dest, RTA_DST);
865 SOCKADDRSET (gate, RTA_GATEWAY);
866 SOCKADDRSET (mask, RTA_NETMASK);
867
868 msg.rtm.rtm_msglen = pnt - (caddr_t) &msg;
869
870 ret = write (routing_sock, &msg, msg.rtm.rtm_msglen);
871
872 if (ret != msg.rtm.rtm_msglen)
873 {
874 if (errno == EEXIST)
875 return ZEBRA_ERR_RTEXIST;
876 if (errno == ENETUNREACH)
877 return ZEBRA_ERR_RTUNREACH;
878
ajs6099b3b2004-11-20 02:06:59 +0000879 zlog_warn ("write : %s (%d)", safe_strerror (errno), errno);
paul718e3742002-12-13 20:15:29 +0000880 return -1;
881 }
882 return 0;
883}
884
885
886#include "thread.h"
887#include "zebra/zserv.h"
888
paul718e3742002-12-13 20:15:29 +0000889/* For debug purpose. */
ajsb6178002004-12-07 21:12:56 +0000890static void
paul718e3742002-12-13 20:15:29 +0000891rtmsg_debug (struct rt_msghdr *rtm)
892{
gdt6a250b02004-12-09 14:48:12 +0000893 const char *type = "Unknown";
paul718e3742002-12-13 20:15:29 +0000894 struct message *mes;
895
896 for (mes = rtm_type_str; mes->str; mes++)
897 if (mes->key == rtm->rtm_type)
898 {
899 type = mes->str;
900 break;
901 }
902
ajsb6178002004-12-07 21:12:56 +0000903 zlog_debug ("Kernel: Len: %d Type: %s", rtm->rtm_msglen, type);
paul718e3742002-12-13 20:15:29 +0000904 rtm_flag_dump (rtm->rtm_flags);
ajsb6178002004-12-07 21:12:56 +0000905 zlog_debug ("Kernel: message seq %d", rtm->rtm_seq);
paul6fe70d12005-11-12 22:55:10 +0000906 zlog_debug ("Kernel: pid %d, rtm_addrs 0x%x", rtm->rtm_pid, rtm->rtm_addrs);
paul718e3742002-12-13 20:15:29 +0000907}
908
909/* This is pretty gross, better suggestions welcome -- mhandler */
910#ifndef RTAX_MAX
911#ifdef RTA_NUMBITS
912#define RTAX_MAX RTA_NUMBITS
913#else
914#define RTAX_MAX 8
915#endif /* RTA_NUMBITS */
916#endif /* RTAX_MAX */
917
918/* Kernel routing table and interface updates via routing socket. */
paul6621ca82005-11-23 13:02:08 +0000919static int
paul718e3742002-12-13 20:15:29 +0000920kernel_read (struct thread *thread)
921{
922 int sock;
923 int nbytes;
924 struct rt_msghdr *rtm;
925
gdtdbee01f2004-01-06 00:36:51 +0000926 /*
927 * This must be big enough for any message the kernel might send.
gdtb27900b2004-01-08 15:44:29 +0000928 * Rather than determining how many sockaddrs of what size might be
929 * in each particular message, just use RTAX_MAX of sockaddr_storage
930 * for each. Note that the sockaddrs must be after each message
931 * definition, or rather after whichever happens to be the largest,
932 * since the buffer needs to be big enough for a message and the
933 * sockaddrs together.
gdtdbee01f2004-01-06 00:36:51 +0000934 */
paul718e3742002-12-13 20:15:29 +0000935 union
936 {
937 /* Routing information. */
938 struct
939 {
940 struct rt_msghdr rtm;
gdtb27900b2004-01-08 15:44:29 +0000941 struct sockaddr_storage addr[RTAX_MAX];
paul718e3742002-12-13 20:15:29 +0000942 } r;
943
944 /* Interface information. */
945 struct
946 {
947 struct if_msghdr ifm;
gdtb27900b2004-01-08 15:44:29 +0000948 struct sockaddr_storage addr[RTAX_MAX];
paul718e3742002-12-13 20:15:29 +0000949 } im;
950
951 /* Interface address information. */
952 struct
953 {
954 struct ifa_msghdr ifa;
gdtb27900b2004-01-08 15:44:29 +0000955 struct sockaddr_storage addr[RTAX_MAX];
paul718e3742002-12-13 20:15:29 +0000956 } ia;
957
958#ifdef RTM_IFANNOUNCE
959 /* Interface arrival/departure */
960 struct
961 {
962 struct if_announcemsghdr ifan;
gdtb27900b2004-01-08 15:44:29 +0000963 struct sockaddr_storage addr[RTAX_MAX];
paul718e3742002-12-13 20:15:29 +0000964 } ian;
965#endif /* RTM_IFANNOUNCE */
966
967 } buf;
968
969 /* Fetch routing socket. */
970 sock = THREAD_FD (thread);
971
972 nbytes= read (sock, &buf, sizeof buf);
973
974 if (nbytes <= 0)
975 {
976 if (nbytes < 0 && errno != EWOULDBLOCK && errno != EAGAIN)
ajs6099b3b2004-11-20 02:06:59 +0000977 zlog_warn ("routing socket error: %s", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +0000978 return 0;
979 }
980
paul9bcdb632003-07-08 08:09:45 +0000981 thread_add_read (zebrad.master, kernel_read, NULL, sock);
paul718e3742002-12-13 20:15:29 +0000982
hasso726f9b22003-05-25 21:04:54 +0000983 if (IS_ZEBRA_DEBUG_KERNEL)
984 rtmsg_debug (&buf.r.rtm);
paul718e3742002-12-13 20:15:29 +0000985
986 rtm = &buf.r.rtm;
987
gdtb27900b2004-01-08 15:44:29 +0000988 /*
989 * Ensure that we didn't drop any data, so that processing routines
990 * can assume they have the whole message.
991 */
gdtda26e3b2004-01-05 17:20:59 +0000992 if (rtm->rtm_msglen != nbytes)
993 {
994 zlog_warn ("kernel_read: rtm->rtm_msglen %d, nbytes %d, type %d\n",
995 rtm->rtm_msglen, nbytes, rtm->rtm_type);
996 return -1;
997 }
998
paul718e3742002-12-13 20:15:29 +0000999 switch (rtm->rtm_type)
1000 {
1001 case RTM_ADD:
1002 case RTM_DELETE:
paulca162182005-09-12 16:58:52 +00001003 case RTM_CHANGE:
paul718e3742002-12-13 20:15:29 +00001004 rtm_read (rtm);
1005 break;
1006 case RTM_IFINFO:
1007 ifm_read (&buf.im.ifm);
1008 break;
1009 case RTM_NEWADDR:
1010 case RTM_DELADDR:
1011 ifam_read (&buf.ia.ifa);
1012 break;
1013#ifdef RTM_IFANNOUNCE
1014 case RTM_IFANNOUNCE:
1015 ifan_read (&buf.ian.ifan);
1016 break;
1017#endif /* RTM_IFANNOUNCE */
1018 default:
hasso726f9b22003-05-25 21:04:54 +00001019 if (IS_ZEBRA_DEBUG_KERNEL)
ajsb6178002004-12-07 21:12:56 +00001020 zlog_debug("Unprocessed RTM_type: %d", rtm->rtm_type);
paul718e3742002-12-13 20:15:29 +00001021 break;
1022 }
1023 return 0;
1024}
1025
1026/* Make routing socket. */
paul6621ca82005-11-23 13:02:08 +00001027static void
1028routing_socket (void)
paul718e3742002-12-13 20:15:29 +00001029{
pauledd7c242003-06-04 13:59:38 +00001030 if ( zserv_privs.change (ZPRIVS_RAISE) )
1031 zlog_err ("routing_socket: Can't raise privileges");
1032
paul718e3742002-12-13 20:15:29 +00001033 routing_sock = socket (AF_ROUTE, SOCK_RAW, 0);
1034
1035 if (routing_sock < 0)
1036 {
pauledd7c242003-06-04 13:59:38 +00001037 if ( zserv_privs.change (ZPRIVS_LOWER) )
1038 zlog_err ("routing_socket: Can't lower privileges");
paul718e3742002-12-13 20:15:29 +00001039 zlog_warn ("Can't init kernel routing socket");
1040 return;
1041 }
1042
paul865b8522005-01-05 08:30:35 +00001043 /* XXX: Socket should be NONBLOCK, however as we currently
1044 * discard failed writes, this will lead to inconsistencies.
1045 * For now, socket must be blocking.
1046 */
1047 /*if (fcntl (routing_sock, F_SETFL, O_NONBLOCK) < 0)
1048 zlog_warn ("Can't set O_NONBLOCK to routing socket");*/
1049
pauledd7c242003-06-04 13:59:38 +00001050 if ( zserv_privs.change (ZPRIVS_LOWER) )
1051 zlog_err ("routing_socket: Can't lower privileges");
paul718e3742002-12-13 20:15:29 +00001052
1053 /* kernel_read needs rewrite. */
paul9bcdb632003-07-08 08:09:45 +00001054 thread_add_read (zebrad.master, kernel_read, NULL, routing_sock);
paul718e3742002-12-13 20:15:29 +00001055}
1056
1057/* Exported interface function. This function simply calls
1058 routing_socket (). */
1059void
paul6621ca82005-11-23 13:02:08 +00001060kernel_init (void)
paul718e3742002-12-13 20:15:29 +00001061{
1062 routing_socket ();
1063}