blob: 9027cd6a1f43a6c611e9a73fb3e1df26ee6fba5e [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
80/* Routing socket message types. */
81struct message rtm_type_str[] =
82{
83 {RTM_ADD, "RTM_ADD"},
84 {RTM_DELETE, "RTM_DELETE"},
85 {RTM_CHANGE, "RTM_CHANGE"},
86 {RTM_GET, "RTM_GET"},
87 {RTM_LOSING, "RTM_LOSING"},
88 {RTM_REDIRECT, "RTM_REDIRECT"},
89 {RTM_MISS, "RTM_MISS"},
90 {RTM_LOCK, "RTM_LOCK"},
91 {RTM_OLDADD, "RTM_OLDADD"},
92 {RTM_OLDDEL, "RTM_OLDDEL"},
93 {RTM_RESOLVE, "RTM_RESOLVE"},
94 {RTM_NEWADDR, "RTM_NEWADDR"},
95 {RTM_DELADDR, "RTM_DELADDR"},
96 {RTM_IFINFO, "RTM_IFINFO"},
97#ifdef RTM_OIFINFO
98 {RTM_OIFINFO, "RTM_OIFINFO"},
99#endif /* RTM_OIFINFO */
100#ifdef RTM_NEWMADDR
101 {RTM_NEWMADDR, "RTM_NEWMADDR"},
102#endif /* RTM_NEWMADDR */
103#ifdef RTM_DELMADDR
104 {RTM_DELMADDR, "RTM_DELMADDR"},
105#endif /* RTM_DELMADDR */
106#ifdef RTM_IFANNOUNCE
107 {RTM_IFANNOUNCE, "RTM_IFANNOUNCE"},
108#endif /* RTM_IFANNOUNCE */
109 {0, NULL}
110};
111
112struct message rtm_flag_str[] =
113{
114 {RTF_UP, "UP"},
115 {RTF_GATEWAY, "GATEWAY"},
116 {RTF_HOST, "HOST"},
117 {RTF_REJECT, "REJECT"},
118 {RTF_DYNAMIC, "DYNAMIC"},
119 {RTF_MODIFIED, "MODIFIED"},
120 {RTF_DONE, "DONE"},
121#ifdef RTF_MASK
122 {RTF_MASK, "MASK"},
123#endif /* RTF_MASK */
124 {RTF_CLONING, "CLONING"},
125 {RTF_XRESOLVE, "XRESOLVE"},
126 {RTF_LLINFO, "LLINFO"},
127 {RTF_STATIC, "STATIC"},
128 {RTF_BLACKHOLE, "BLACKHOLE"},
129 {RTF_PROTO1, "PROTO1"},
130 {RTF_PROTO2, "PROTO2"},
131#ifdef RTF_PRCLONING
132 {RTF_PRCLONING, "PRCLONING"},
133#endif /* RTF_PRCLONING */
134#ifdef RTF_WASCLONED
135 {RTF_WASCLONED, "WASCLONED"},
136#endif /* RTF_WASCLONED */
137#ifdef RTF_PROTO3
138 {RTF_PROTO3, "PROTO3"},
139#endif /* RTF_PROTO3 */
140#ifdef RTF_PINNED
141 {RTF_PINNED, "PINNED"},
142#endif /* RTF_PINNED */
143#ifdef RTF_LOCAL
144 {RTF_LOCAL, "LOCAL"},
145#endif /* RTF_LOCAL */
146#ifdef RTF_BROADCAST
147 {RTF_BROADCAST, "BROADCAST"},
148#endif /* RTF_BROADCAST */
149#ifdef RTF_MULTICAST
150 {RTF_MULTICAST, "MULTICAST"},
151#endif /* RTF_MULTICAST */
152 {0, NULL}
153};
154
155/* Kernel routing update socket. */
156int routing_sock = -1;
157
158/* Yes I'm checking ugly routing socket behavior. */
159/* #define DEBUG */
160
161/* Supported address family check. */
162static int
163af_check (int family)
164{
165 if (family == AF_INET)
166 return 1;
167#ifdef HAVE_IPV6
168 if (family == AF_INET6)
169 return 1;
170#endif /* HAVE_IPV6 */
171 return 0;
172}
173
174/* Dump routing table flag for debug purpose. */
ajsb6178002004-12-07 21:12:56 +0000175static void
paul718e3742002-12-13 20:15:29 +0000176rtm_flag_dump (int flag)
177{
178 struct message *mes;
179 static char buf[BUFSIZ];
180
gdtcced60d2004-07-13 16:45:54 +0000181 buf[0] = '\0';
paul718e3742002-12-13 20:15:29 +0000182 for (mes = rtm_flag_str; mes->key != 0; mes++)
183 {
184 if (mes->key & flag)
185 {
186 strlcat (buf, mes->str, BUFSIZ);
187 strlcat (buf, " ", BUFSIZ);
188 }
189 }
ajsb6178002004-12-07 21:12:56 +0000190 zlog_debug ("Kernel: %s", buf);
paul718e3742002-12-13 20:15:29 +0000191}
192
193#ifdef RTM_IFANNOUNCE
194/* Interface adding function */
195int
196ifan_read (struct if_announcemsghdr *ifan)
197{
198 struct interface *ifp;
199
200 ifp = if_lookup_by_index (ifan->ifan_index);
201 if (ifp == NULL && ifan->ifan_what == IFAN_ARRIVAL)
202 {
203 /* Create Interface */
204 ifp = if_get_by_name (ifan->ifan_name);
205 ifp->ifindex = ifan->ifan_index;
206
207 if_add_update (ifp);
208 }
209 else if (ifp != NULL && ifan->ifan_what == IFAN_DEPARTURE)
210 {
211 if_delete_update (ifp);
212 if_delete (ifp);
213 }
214
215 if_get_flags (ifp);
216 if_get_mtu (ifp);
217 if_get_metric (ifp);
218
219 if (IS_ZEBRA_DEBUG_KERNEL)
ajsb6178002004-12-07 21:12:56 +0000220 zlog_debug ("interface %s index %d", ifp->name, ifp->ifindex);
paul718e3742002-12-13 20:15:29 +0000221
222 return 0;
223}
224#endif /* RTM_IFANNOUNCE */
225
gdtda26e3b2004-01-05 17:20:59 +0000226/*
227 * Handle struct if_msghdr obtained from reading routing socket or
228 * sysctl (from interface_list). There may or may not be sockaddrs
229 * present after the header.
230 */
paul718e3742002-12-13 20:15:29 +0000231int
232ifm_read (struct if_msghdr *ifm)
233{
paul3e95a072003-09-24 00:05:45 +0000234 struct interface *ifp = NULL;
paul718e3742002-12-13 20:15:29 +0000235 struct sockaddr_dl *sdl = NULL;
gdt4bfbea82004-01-06 01:13:05 +0000236 void *cp;
237 unsigned int i;
paul3e95a072003-09-24 00:05:45 +0000238 char ifname[IFNAMSIZ];
paul718e3742002-12-13 20:15:29 +0000239
gdtda26e3b2004-01-05 17:20:59 +0000240 /* paranoia: sanity check structure */
241 if (ifm->ifm_msglen < sizeof(struct if_msghdr))
242 {
243 zlog_err ("ifm_read: ifm->ifm_msglen %d too short\n",
244 ifm->ifm_msglen);
245 return -1;
246 }
247
248 /*
gdt4bfbea82004-01-06 01:13:05 +0000249 * Check for a sockaddr_dl following the message. First, point to
250 * where a socakddr might be if one follows the message.
gdtda26e3b2004-01-05 17:20:59 +0000251 */
gdt4bfbea82004-01-06 01:13:05 +0000252 cp = (void *)(ifm + 1);
253
paul3e95a072003-09-24 00:05:45 +0000254#ifdef SUNOS_5
paul3e95a072003-09-24 00:05:45 +0000255 /*
gdt4bfbea82004-01-06 01:13:05 +0000256 * XXX This behavior should be narrowed to only the kernel versions
257 * for which the structures returned do not match the headers.
258 *
paul3e95a072003-09-24 00:05:45 +0000259 * if_msghdr_t on 64 bit kernels in Solaris 9 and earlier versions
gdt4bfbea82004-01-06 01:13:05 +0000260 * is 12 bytes larger than the 32 bit version.
paul3e95a072003-09-24 00:05:45 +0000261 */
gdt4bfbea82004-01-06 01:13:05 +0000262 if (((struct sockaddr *) cp)->sa_family == AF_UNSPEC)
paul3e95a072003-09-24 00:05:45 +0000263 cp = cp + 12;
paul3e95a072003-09-24 00:05:45 +0000264#endif
paul718e3742002-12-13 20:15:29 +0000265
gdt4bfbea82004-01-06 01:13:05 +0000266 /*
267 * Check for each sockaddr in turn, advancing over it. After this
268 * loop, sdl should point to a sockaddr_dl iff one was present.
269 */
270 for (i = 1; i != 0; i <<= 1)
271 {
272 if (i & ifm->ifm_addrs)
273 {
274 if (i == RTA_IFP)
275 {
276 sdl = (struct sockaddr_dl *)cp;
277 break;
278 }
gdt6a250b02004-12-09 14:48:12 +0000279 /* XXX warning: pointer of type `void *' used in arithmetic */
gdt4bfbea82004-01-06 01:13:05 +0000280 cp += SAROUNDUP(cp);
281 }
282 }
gdtda26e3b2004-01-05 17:20:59 +0000283
gdt4bfbea82004-01-06 01:13:05 +0000284 /* Ensure that sdl, if present, is actually a sockaddr_dl. */
285 if (sdl != NULL && sdl->sdl_family != AF_LINK)
286 {
287 zlog_err ("ifm_read: sockaddr_dl bad AF %d\n",
288 sdl->sdl_family);
289 return -1;
290 }
gdtda26e3b2004-01-05 17:20:59 +0000291
292 /*
gdt4bfbea82004-01-06 01:13:05 +0000293 * Look up on ifindex first, because ifindices are the primary
294 * handle for interfaces across the user/kernel boundary. (Some
295 * messages, such as up/down status changes on NetBSD, do not
296 * include a sockaddr_dl).
gdtda26e3b2004-01-05 17:20:59 +0000297 */
gdt4bfbea82004-01-06 01:13:05 +0000298 ifp = if_lookup_by_index (ifm->ifm_index);
gdtda26e3b2004-01-05 17:20:59 +0000299
gdtda26e3b2004-01-05 17:20:59 +0000300 /*
301 * If lookup by index was unsuccessful and we have a name, try
302 * looking up by name. Interfaces specified in the configuration
303 * file for which the ifindex has not been determined will have
304 * ifindex == -1, and such interfaces are found by this search, and
305 * then their ifindex values can be filled in.
306 */
gdtcb42c032004-01-05 17:55:46 +0000307 if (ifp == NULL && sdl != NULL)
paul3e95a072003-09-24 00:05:45 +0000308 {
gdtda26e3b2004-01-05 17:20:59 +0000309 /*
310 * paranoia: sanity check name length. nlen does not include
311 * trailing zero, but IFNAMSIZ max length does.
312 */
313 if (sdl->sdl_nlen >= IFNAMSIZ)
314 {
315 zlog_err ("ifm_read: illegal sdl_nlen %d\n", sdl->sdl_nlen);
316 return -1;
317 }
318
paul30be8022003-10-22 02:51:38 +0000319 memcpy (ifname, sdl->sdl_data, sdl->sdl_nlen);
paul3e95a072003-09-24 00:05:45 +0000320 ifname[sdl->sdl_nlen] = '\0';
321 ifp = if_lookup_by_name (ifname);
322 }
paul718e3742002-12-13 20:15:29 +0000323
gdtda26e3b2004-01-05 17:20:59 +0000324 /*
325 * If ifp does not exist or has an invalid index (-1), create or
326 * fill in an interface.
327 */
gdt6a250b02004-12-09 14:48:12 +0000328 /*
329 * XXX warning: comparison between signed and unsigned
330 * ifindex should probably be signed
331 */
paul3e95a072003-09-24 00:05:45 +0000332 if ((ifp == NULL) || (ifp->ifindex == -1))
paul718e3742002-12-13 20:15:29 +0000333 {
gdt4bfbea82004-01-06 01:13:05 +0000334 /*
335 * To create or fill in an interface, a sockaddr_dl (via
336 * RTA_IFP) is required.
337 */
338 if (sdl == NULL)
paul718e3742002-12-13 20:15:29 +0000339 {
gdt4bfbea82004-01-06 01:13:05 +0000340 zlog_warn ("Interface index %d (new) missing RTA_IFP sockaddr_dl\n",
paul718e3742002-12-13 20:15:29 +0000341 ifm->ifm_index);
342 return -1;
343 }
344
paul3e95a072003-09-24 00:05:45 +0000345 if (ifp == NULL)
gdt4bfbea82004-01-06 01:13:05 +0000346 /* Interface that zebra was not previously aware of, so create. */
paul3e95a072003-09-24 00:05:45 +0000347 ifp = if_create (sdl->sdl_data, sdl->sdl_nlen);
paul718e3742002-12-13 20:15:29 +0000348
gdt4bfbea82004-01-06 01:13:05 +0000349 /*
350 * Fill in newly created interface structure, or larval
351 * structure with ifindex -1.
352 */
paul718e3742002-12-13 20:15:29 +0000353 ifp->ifindex = ifm->ifm_index;
354 ifp->flags = ifm->ifm_flags;
355#if defined(__bsdi__)
356 if_kvm_get_mtu (ifp);
357#else
358 if_get_mtu (ifp);
359#endif /* __bsdi__ */
360 if_get_metric (ifp);
361
gdt4bfbea82004-01-06 01:13:05 +0000362 /*
363 * XXX sockaddr_dl contents can be larger than the structure
364 * definition, so the user of the stored structure must be
365 * careful not to read off the end.
366 */
paul718e3742002-12-13 20:15:29 +0000367 memcpy (&ifp->sdl, sdl, sizeof (struct sockaddr_dl));
368
369 if_add_update (ifp);
370 }
371 else
gdtda26e3b2004-01-05 17:20:59 +0000372 /*
373 * Interface structure exists. Adjust stored flags from
374 * notification. If interface has up->down or down->up
375 * transition, call state change routines (to adjust routes,
376 * notify routing daemons, etc.). (Other flag changes are stored
377 * but apparently do not trigger action.)
378 */
paul718e3742002-12-13 20:15:29 +0000379 {
paul718e3742002-12-13 20:15:29 +0000380 if (if_is_up (ifp))
381 {
382 ifp->flags = ifm->ifm_flags;
383 if (! if_is_up (ifp))
384 if_down (ifp);
385 }
386 else
387 {
388 ifp->flags = ifm->ifm_flags;
389 if (if_is_up (ifp))
390 if_up (ifp);
391 }
392 }
393
394#ifdef HAVE_NET_RT_IFLIST
395 ifp->stats = ifm->ifm_data;
396#endif /* HAVE_NET_RT_IFLIST */
397
398 if (IS_ZEBRA_DEBUG_KERNEL)
ajsb6178002004-12-07 21:12:56 +0000399 zlog_debug ("interface %s index %d", ifp->name, ifp->ifindex);
paul718e3742002-12-13 20:15:29 +0000400
401 return 0;
402}
403
404/* Address read from struct ifa_msghdr. */
405void
406ifam_read_mesg (struct ifa_msghdr *ifm,
407 union sockunion *addr,
408 union sockunion *mask,
409 union sockunion *dest)
410{
411 caddr_t pnt, end;
412
413 pnt = (caddr_t)(ifm + 1);
414 end = ((caddr_t)ifm) + ifm->ifam_msglen;
415
416#define IFAMADDRGET(X,R) \
417 if (ifm->ifam_addrs & (R)) \
418 { \
gdt4bfbea82004-01-06 01:13:05 +0000419 int len = SAROUNDUP(pnt); \
paul718e3742002-12-13 20:15:29 +0000420 if (((X) != NULL) && af_check (((struct sockaddr *)pnt)->sa_family)) \
421 memcpy ((caddr_t)(X), pnt, len); \
422 pnt += len; \
423 }
424#define IFAMMASKGET(X,R) \
425 if (ifm->ifam_addrs & (R)) \
426 { \
gdt4bfbea82004-01-06 01:13:05 +0000427 int len = SAROUNDUP(pnt); \
paul718e3742002-12-13 20:15:29 +0000428 if ((X) != NULL) \
429 memcpy ((caddr_t)(X), pnt, len); \
430 pnt += len; \
431 }
432
433 /* Be sure structure is cleared */
434 memset (mask, 0, sizeof (union sockunion));
435 memset (addr, 0, sizeof (union sockunion));
436 memset (dest, 0, sizeof (union sockunion));
437
438 /* We fetch each socket variable into sockunion. */
439 IFAMADDRGET (NULL, RTA_DST);
440 IFAMADDRGET (NULL, RTA_GATEWAY);
441 IFAMMASKGET (mask, RTA_NETMASK);
442 IFAMADDRGET (NULL, RTA_GENMASK);
443 IFAMADDRGET (NULL, RTA_IFP);
444 IFAMADDRGET (addr, RTA_IFA);
445 IFAMADDRGET (NULL, RTA_AUTHOR);
446 IFAMADDRGET (dest, RTA_BRD);
447
448 /* Assert read up end point matches to end point */
449 if (pnt != end)
450 zlog_warn ("ifam_read() does't read all socket data");
451}
452
453/* Interface's address information get. */
454int
455ifam_read (struct ifa_msghdr *ifam)
456{
457 struct interface *ifp;
458 union sockunion addr, mask, gate;
459
460 /* Check does this interface exist or not. */
461 ifp = if_lookup_by_index (ifam->ifam_index);
462 if (ifp == NULL)
463 {
464 zlog_warn ("no interface for index %d", ifam->ifam_index);
465 return -1;
466 }
467
468 /* Allocate and read address information. */
469 ifam_read_mesg (ifam, &addr, &mask, &gate);
470
471 /* Check interface flag for implicit up of the interface. */
472 if_refresh (ifp);
473
474 /* Add connected address. */
475 switch (sockunion_family (&addr))
476 {
477 case AF_INET:
478 if (ifam->ifam_type == RTM_NEWADDR)
479 connected_add_ipv4 (ifp, 0, &addr.sin.sin_addr,
480 ip_masklen (mask.sin.sin_addr),
481 &gate.sin.sin_addr, NULL);
482 else
483 connected_delete_ipv4 (ifp, 0, &addr.sin.sin_addr,
484 ip_masklen (mask.sin.sin_addr),
485 &gate.sin.sin_addr, NULL);
486 break;
487#ifdef HAVE_IPV6
488 case AF_INET6:
489 /* Unset interface index from link-local address when IPv6 stack
490 is KAME. */
491 if (IN6_IS_ADDR_LINKLOCAL (&addr.sin6.sin6_addr))
492 SET_IN6_LINKLOCAL_IFINDEX (addr.sin6.sin6_addr, 0);
493
494 if (ifam->ifam_type == RTM_NEWADDR)
495 connected_add_ipv6 (ifp,
496 &addr.sin6.sin6_addr,
497 ip6_masklen (mask.sin6.sin6_addr),
498 &gate.sin6.sin6_addr);
499 else
500 connected_delete_ipv6 (ifp,
501 &addr.sin6.sin6_addr,
502 ip6_masklen (mask.sin6.sin6_addr),
503 &gate.sin6.sin6_addr);
504 break;
505#endif /* HAVE_IPV6 */
506 default:
507 /* Unsupported family silently ignore... */
508 break;
509 }
510 return 0;
511}
512
513/* Interface function for reading kernel routing table information. */
514int
515rtm_read_mesg (struct rt_msghdr *rtm,
516 union sockunion *dest,
517 union sockunion *mask,
518 union sockunion *gate)
519{
520 caddr_t pnt, end;
521
522 /* Pnt points out socket data start point. */
523 pnt = (caddr_t)(rtm + 1);
524 end = ((caddr_t)rtm) + rtm->rtm_msglen;
525
526 /* rt_msghdr version check. */
527 if (rtm->rtm_version != RTM_VERSION)
528 zlog (NULL, LOG_WARNING,
529 "Routing message version different %d should be %d."
530 "This may cause problem\n", rtm->rtm_version, RTM_VERSION);
531
532#define RTMADDRGET(X,R) \
533 if (rtm->rtm_addrs & (R)) \
534 { \
gdt4bfbea82004-01-06 01:13:05 +0000535 int len = SAROUNDUP (pnt); \
paul718e3742002-12-13 20:15:29 +0000536 if (((X) != NULL) && af_check (((struct sockaddr *)pnt)->sa_family)) \
537 memcpy ((caddr_t)(X), pnt, len); \
538 pnt += len; \
539 }
540#define RTMMASKGET(X,R) \
541 if (rtm->rtm_addrs & (R)) \
542 { \
gdt4bfbea82004-01-06 01:13:05 +0000543 int len = SAROUNDUP (pnt); \
paul718e3742002-12-13 20:15:29 +0000544 if ((X) != NULL) \
545 memcpy ((caddr_t)(X), pnt, len); \
546 pnt += len; \
547 }
548
549 /* Be sure structure is cleared */
550 memset (dest, 0, sizeof (union sockunion));
551 memset (gate, 0, sizeof (union sockunion));
552 memset (mask, 0, sizeof (union sockunion));
553
554 /* We fetch each socket variable into sockunion. */
555 RTMADDRGET (dest, RTA_DST);
556 RTMADDRGET (gate, RTA_GATEWAY);
557 RTMMASKGET (mask, RTA_NETMASK);
558 RTMADDRGET (NULL, RTA_GENMASK);
559 RTMADDRGET (NULL, RTA_IFP);
560 RTMADDRGET (NULL, RTA_IFA);
561 RTMADDRGET (NULL, RTA_AUTHOR);
562 RTMADDRGET (NULL, RTA_BRD);
563
564 /* If there is netmask information set it's family same as
565 destination family*/
566 if (rtm->rtm_addrs & RTA_NETMASK)
567 mask->sa.sa_family = dest->sa.sa_family;
568
569 /* Assert read up to the end of pointer. */
570 if (pnt != end)
571 zlog (NULL, LOG_WARNING, "rtm_read() does't read all socket data.");
572
573 return rtm->rtm_flags;
574}
575
576void
577rtm_read (struct rt_msghdr *rtm)
578{
579 int flags;
580 u_char zebra_flags;
581 union sockunion dest, mask, gate;
582
583 zebra_flags = 0;
584
585 /* Discard self send message. */
586 if (rtm->rtm_type != RTM_GET
587 && (rtm->rtm_pid == pid || rtm->rtm_pid == old_pid))
588 return;
589
590 /* Read destination and netmask and gateway from rtm message
591 structure. */
592 flags = rtm_read_mesg (rtm, &dest, &mask, &gate);
593
594#ifdef RTF_CLONED /*bsdi, netbsd 1.6*/
595 if (flags & RTF_CLONED)
596 return;
597#endif
598#ifdef RTF_WASCLONED /*freebsd*/
599 if (flags & RTF_WASCLONED)
600 return;
601#endif
602
603 if ((rtm->rtm_type == RTM_ADD) && ! (flags & RTF_UP))
604 return;
605
606 /* This is connected route. */
607 if (! (flags & RTF_GATEWAY))
608 return;
609
610 if (flags & RTF_PROTO1)
611 SET_FLAG (zebra_flags, ZEBRA_FLAG_SELFROUTE);
612
613 /* This is persistent route. */
614 if (flags & RTF_STATIC)
615 SET_FLAG (zebra_flags, ZEBRA_FLAG_STATIC);
616
hasso81dfcaa2003-05-25 19:21:25 +0000617 /* This is a reject or blackhole route */
618 if (flags & RTF_REJECT)
619 SET_FLAG (zebra_flags, ZEBRA_FLAG_REJECT);
620 if (flags & RTF_BLACKHOLE)
621 SET_FLAG (zebra_flags, ZEBRA_FLAG_BLACKHOLE);
622
paul718e3742002-12-13 20:15:29 +0000623 if (dest.sa.sa_family == AF_INET)
624 {
625 struct prefix_ipv4 p;
626
627 p.family = AF_INET;
628 p.prefix = dest.sin.sin_addr;
629 if (flags & RTF_HOST)
630 p.prefixlen = IPV4_MAX_PREFIXLEN;
631 else
632 p.prefixlen = ip_masklen (mask.sin.sin_addr);
633
634 if (rtm->rtm_type == RTM_GET || rtm->rtm_type == RTM_ADD)
635 rib_add_ipv4 (ZEBRA_ROUTE_KERNEL, zebra_flags,
636 &p, &gate.sin.sin_addr, 0, 0, 0, 0);
637 else
638 rib_delete_ipv4 (ZEBRA_ROUTE_KERNEL, zebra_flags,
639 &p, &gate.sin.sin_addr, 0, 0);
640 }
641#ifdef HAVE_IPV6
642 if (dest.sa.sa_family == AF_INET6)
643 {
644 struct prefix_ipv6 p;
645 unsigned int ifindex = 0;
646
647 p.family = AF_INET6;
648 p.prefix = dest.sin6.sin6_addr;
649 if (flags & RTF_HOST)
650 p.prefixlen = IPV6_MAX_PREFIXLEN;
651 else
652 p.prefixlen = ip6_masklen (mask.sin6.sin6_addr);
653
654#ifdef KAME
655 if (IN6_IS_ADDR_LINKLOCAL (&gate.sin6.sin6_addr))
656 {
657 ifindex = IN6_LINKLOCAL_IFINDEX (gate.sin6.sin6_addr);
658 SET_IN6_LINKLOCAL_IFINDEX (gate.sin6.sin6_addr, 0);
659 }
660#endif /* KAME */
661
662 if (rtm->rtm_type == RTM_GET || rtm->rtm_type == RTM_ADD)
663 rib_add_ipv6 (ZEBRA_ROUTE_KERNEL, zebra_flags,
664 &p, &gate.sin6.sin6_addr, ifindex, 0);
665 else
666 rib_delete_ipv6 (ZEBRA_ROUTE_KERNEL, zebra_flags,
667 &p, &gate.sin6.sin6_addr, ifindex, 0);
668 }
669#endif /* HAVE_IPV6 */
670}
671
672/* Interface function for the kernel routing table updates. Support
673 for RTM_CHANGE will be needed. */
674int
675rtm_write (int message,
676 union sockunion *dest,
677 union sockunion *mask,
678 union sockunion *gate,
679 unsigned int index,
680 int zebra_flags,
681 int metric)
682{
683 int ret;
684 caddr_t pnt;
685 struct interface *ifp;
686 struct sockaddr_in tmp_gate;
687#ifdef HAVE_IPV6
688 struct sockaddr_in6 tmp_gate6;
689#endif /* HAVE_IPV6 */
690
691 /* Sequencial number of routing message. */
692 static int msg_seq = 0;
693
694 /* Struct of rt_msghdr and buffer for storing socket's data. */
695 struct
696 {
697 struct rt_msghdr rtm;
698 char buf[512];
699 } msg;
700
701 memset (&tmp_gate, 0, sizeof (struct sockaddr_in));
702 tmp_gate.sin_family = AF_INET;
703#ifdef HAVE_SIN_LEN
704 tmp_gate.sin_len = sizeof (struct sockaddr_in);
705#endif /* HAVE_SIN_LEN */
706
707#ifdef HAVE_IPV6
708 memset (&tmp_gate6, 0, sizeof (struct sockaddr_in6));
709 tmp_gate6.sin6_family = AF_INET6;
710#ifdef SIN6_LEN
711 tmp_gate6.sin6_len = sizeof (struct sockaddr_in6);
712#endif /* SIN6_LEN */
713#endif /* HAVE_IPV6 */
714
715 if (routing_sock < 0)
716 return ZEBRA_ERR_EPERM;
717
718 /* Clear and set rt_msghdr values */
719 memset (&msg, 0, sizeof (struct rt_msghdr));
720 msg.rtm.rtm_version = RTM_VERSION;
721 msg.rtm.rtm_type = message;
722 msg.rtm.rtm_seq = msg_seq++;
723 msg.rtm.rtm_addrs = RTA_DST;
724 msg.rtm.rtm_addrs |= RTA_GATEWAY;
725 msg.rtm.rtm_flags = RTF_UP;
726 msg.rtm.rtm_index = index;
727
728 if (metric != 0)
729 {
730 msg.rtm.rtm_rmx.rmx_hopcount = metric;
731 msg.rtm.rtm_inits |= RTV_HOPCOUNT;
732 }
733
734 ifp = if_lookup_by_index (index);
735
736 if (gate && message == RTM_ADD)
737 msg.rtm.rtm_flags |= RTF_GATEWAY;
738
739 if (! gate && message == RTM_ADD && ifp &&
740 (ifp->flags & IFF_POINTOPOINT) == 0)
741 msg.rtm.rtm_flags |= RTF_CLONING;
742
743 /* If no protocol specific gateway is specified, use link
744 address for gateway. */
745 if (! gate)
746 {
747 if (!ifp)
748 {
749 zlog_warn ("no gateway found for interface index %d", index);
750 return -1;
751 }
752 gate = (union sockunion *) & ifp->sdl;
753 }
754
755 if (mask)
756 msg.rtm.rtm_addrs |= RTA_NETMASK;
757 else if (message == RTM_ADD)
758 msg.rtm.rtm_flags |= RTF_HOST;
759
760 /* Tagging route with flags */
761 msg.rtm.rtm_flags |= (RTF_PROTO1);
762
763 /* Additional flags. */
764 if (zebra_flags & ZEBRA_FLAG_BLACKHOLE)
765 msg.rtm.rtm_flags |= RTF_BLACKHOLE;
hasso81dfcaa2003-05-25 19:21:25 +0000766 if (zebra_flags & ZEBRA_FLAG_REJECT)
767 msg.rtm.rtm_flags |= RTF_REJECT;
768
paul718e3742002-12-13 20:15:29 +0000769
770#ifdef HAVE_SIN_LEN
771#define SOCKADDRSET(X,R) \
772 if (msg.rtm.rtm_addrs & (R)) \
773 { \
774 int len = ROUNDUP ((X)->sa.sa_len); \
775 memcpy (pnt, (caddr_t)(X), len); \
776 pnt += len; \
777 }
778#else
779#define SOCKADDRSET(X,R) \
780 if (msg.rtm.rtm_addrs & (R)) \
781 { \
782 int len = ROUNDUP (sizeof((X)->sa)); \
783 memcpy (pnt, (caddr_t)(X), len); \
784 pnt += len; \
785 }
786#endif /* HAVE_SIN_LEN */
787
788 pnt = (caddr_t) msg.buf;
789
790 /* Write each socket data into rtm message buffer */
791 SOCKADDRSET (dest, RTA_DST);
792 SOCKADDRSET (gate, RTA_GATEWAY);
793 SOCKADDRSET (mask, RTA_NETMASK);
794
795 msg.rtm.rtm_msglen = pnt - (caddr_t) &msg;
796
797 ret = write (routing_sock, &msg, msg.rtm.rtm_msglen);
798
799 if (ret != msg.rtm.rtm_msglen)
800 {
801 if (errno == EEXIST)
802 return ZEBRA_ERR_RTEXIST;
803 if (errno == ENETUNREACH)
804 return ZEBRA_ERR_RTUNREACH;
805
ajs6099b3b2004-11-20 02:06:59 +0000806 zlog_warn ("write : %s (%d)", safe_strerror (errno), errno);
paul718e3742002-12-13 20:15:29 +0000807 return -1;
808 }
809 return 0;
810}
811
812
813#include "thread.h"
814#include "zebra/zserv.h"
815
paul718e3742002-12-13 20:15:29 +0000816/* For debug purpose. */
ajsb6178002004-12-07 21:12:56 +0000817static void
paul718e3742002-12-13 20:15:29 +0000818rtmsg_debug (struct rt_msghdr *rtm)
819{
gdt6a250b02004-12-09 14:48:12 +0000820 const char *type = "Unknown";
paul718e3742002-12-13 20:15:29 +0000821 struct message *mes;
822
823 for (mes = rtm_type_str; mes->str; mes++)
824 if (mes->key == rtm->rtm_type)
825 {
826 type = mes->str;
827 break;
828 }
829
ajsb6178002004-12-07 21:12:56 +0000830 zlog_debug ("Kernel: Len: %d Type: %s", rtm->rtm_msglen, type);
paul718e3742002-12-13 20:15:29 +0000831 rtm_flag_dump (rtm->rtm_flags);
ajsb6178002004-12-07 21:12:56 +0000832 zlog_debug ("Kernel: message seq %d", rtm->rtm_seq);
833 zlog_debug ("Kernel: pid %d", rtm->rtm_pid);
paul718e3742002-12-13 20:15:29 +0000834}
835
836/* This is pretty gross, better suggestions welcome -- mhandler */
837#ifndef RTAX_MAX
838#ifdef RTA_NUMBITS
839#define RTAX_MAX RTA_NUMBITS
840#else
841#define RTAX_MAX 8
842#endif /* RTA_NUMBITS */
843#endif /* RTAX_MAX */
844
845/* Kernel routing table and interface updates via routing socket. */
846int
847kernel_read (struct thread *thread)
848{
849 int sock;
850 int nbytes;
851 struct rt_msghdr *rtm;
852
gdtdbee01f2004-01-06 00:36:51 +0000853 /*
854 * This must be big enough for any message the kernel might send.
gdtb27900b2004-01-08 15:44:29 +0000855 * Rather than determining how many sockaddrs of what size might be
856 * in each particular message, just use RTAX_MAX of sockaddr_storage
857 * for each. Note that the sockaddrs must be after each message
858 * definition, or rather after whichever happens to be the largest,
859 * since the buffer needs to be big enough for a message and the
860 * sockaddrs together.
gdtdbee01f2004-01-06 00:36:51 +0000861 */
paul718e3742002-12-13 20:15:29 +0000862 union
863 {
864 /* Routing information. */
865 struct
866 {
867 struct rt_msghdr rtm;
gdtb27900b2004-01-08 15:44:29 +0000868 struct sockaddr_storage addr[RTAX_MAX];
paul718e3742002-12-13 20:15:29 +0000869 } r;
870
871 /* Interface information. */
872 struct
873 {
874 struct if_msghdr ifm;
gdtb27900b2004-01-08 15:44:29 +0000875 struct sockaddr_storage addr[RTAX_MAX];
paul718e3742002-12-13 20:15:29 +0000876 } im;
877
878 /* Interface address information. */
879 struct
880 {
881 struct ifa_msghdr ifa;
gdtb27900b2004-01-08 15:44:29 +0000882 struct sockaddr_storage addr[RTAX_MAX];
paul718e3742002-12-13 20:15:29 +0000883 } ia;
884
885#ifdef RTM_IFANNOUNCE
886 /* Interface arrival/departure */
887 struct
888 {
889 struct if_announcemsghdr ifan;
gdtb27900b2004-01-08 15:44:29 +0000890 struct sockaddr_storage addr[RTAX_MAX];
paul718e3742002-12-13 20:15:29 +0000891 } ian;
892#endif /* RTM_IFANNOUNCE */
893
894 } buf;
895
896 /* Fetch routing socket. */
897 sock = THREAD_FD (thread);
898
899 nbytes= read (sock, &buf, sizeof buf);
900
901 if (nbytes <= 0)
902 {
903 if (nbytes < 0 && errno != EWOULDBLOCK && errno != EAGAIN)
ajs6099b3b2004-11-20 02:06:59 +0000904 zlog_warn ("routing socket error: %s", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +0000905 return 0;
906 }
907
paul9bcdb632003-07-08 08:09:45 +0000908 thread_add_read (zebrad.master, kernel_read, NULL, sock);
paul718e3742002-12-13 20:15:29 +0000909
hasso726f9b22003-05-25 21:04:54 +0000910 if (IS_ZEBRA_DEBUG_KERNEL)
911 rtmsg_debug (&buf.r.rtm);
paul718e3742002-12-13 20:15:29 +0000912
913 rtm = &buf.r.rtm;
914
gdtb27900b2004-01-08 15:44:29 +0000915 /*
916 * Ensure that we didn't drop any data, so that processing routines
917 * can assume they have the whole message.
918 */
gdtda26e3b2004-01-05 17:20:59 +0000919 if (rtm->rtm_msglen != nbytes)
920 {
921 zlog_warn ("kernel_read: rtm->rtm_msglen %d, nbytes %d, type %d\n",
922 rtm->rtm_msglen, nbytes, rtm->rtm_type);
923 return -1;
924 }
925
paul718e3742002-12-13 20:15:29 +0000926 switch (rtm->rtm_type)
927 {
928 case RTM_ADD:
929 case RTM_DELETE:
930 rtm_read (rtm);
931 break;
932 case RTM_IFINFO:
933 ifm_read (&buf.im.ifm);
934 break;
935 case RTM_NEWADDR:
936 case RTM_DELADDR:
937 ifam_read (&buf.ia.ifa);
938 break;
939#ifdef RTM_IFANNOUNCE
940 case RTM_IFANNOUNCE:
941 ifan_read (&buf.ian.ifan);
942 break;
943#endif /* RTM_IFANNOUNCE */
944 default:
hasso726f9b22003-05-25 21:04:54 +0000945 if (IS_ZEBRA_DEBUG_KERNEL)
ajsb6178002004-12-07 21:12:56 +0000946 zlog_debug("Unprocessed RTM_type: %d", rtm->rtm_type);
paul718e3742002-12-13 20:15:29 +0000947 break;
948 }
949 return 0;
950}
951
952/* Make routing socket. */
953void
954routing_socket ()
955{
pauledd7c242003-06-04 13:59:38 +0000956 if ( zserv_privs.change (ZPRIVS_RAISE) )
957 zlog_err ("routing_socket: Can't raise privileges");
958
paul718e3742002-12-13 20:15:29 +0000959 routing_sock = socket (AF_ROUTE, SOCK_RAW, 0);
960
961 if (routing_sock < 0)
962 {
pauledd7c242003-06-04 13:59:38 +0000963 if ( zserv_privs.change (ZPRIVS_LOWER) )
964 zlog_err ("routing_socket: Can't lower privileges");
paul718e3742002-12-13 20:15:29 +0000965 zlog_warn ("Can't init kernel routing socket");
966 return;
967 }
968
969 if (fcntl (routing_sock, F_SETFL, O_NONBLOCK) < 0)
970 zlog_warn ("Can't set O_NONBLOCK to routing socket");
pauledd7c242003-06-04 13:59:38 +0000971 if ( zserv_privs.change (ZPRIVS_LOWER) )
972 zlog_err ("routing_socket: Can't lower privileges");
paul718e3742002-12-13 20:15:29 +0000973
974 /* kernel_read needs rewrite. */
paul9bcdb632003-07-08 08:09:45 +0000975 thread_add_read (zebrad.master, kernel_read, NULL, routing_sock);
paul718e3742002-12-13 20:15:29 +0000976}
977
978/* Exported interface function. This function simply calls
979 routing_socket (). */
980void
981kernel_init ()
982{
983 routing_socket ();
984}