blob: e0f2a8f2eb09a29c30430468ca11b07fc7ba0db5 [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 }
279 cp += SAROUNDUP(cp);
280 }
281 }
gdtda26e3b2004-01-05 17:20:59 +0000282
gdt4bfbea82004-01-06 01:13:05 +0000283 /* Ensure that sdl, if present, is actually a sockaddr_dl. */
284 if (sdl != NULL && sdl->sdl_family != AF_LINK)
285 {
286 zlog_err ("ifm_read: sockaddr_dl bad AF %d\n",
287 sdl->sdl_family);
288 return -1;
289 }
gdtda26e3b2004-01-05 17:20:59 +0000290
291 /*
gdt4bfbea82004-01-06 01:13:05 +0000292 * Look up on ifindex first, because ifindices are the primary
293 * handle for interfaces across the user/kernel boundary. (Some
294 * messages, such as up/down status changes on NetBSD, do not
295 * include a sockaddr_dl).
gdtda26e3b2004-01-05 17:20:59 +0000296 */
gdt4bfbea82004-01-06 01:13:05 +0000297 ifp = if_lookup_by_index (ifm->ifm_index);
gdtda26e3b2004-01-05 17:20:59 +0000298
gdtda26e3b2004-01-05 17:20:59 +0000299 /*
300 * If lookup by index was unsuccessful and we have a name, try
301 * looking up by name. Interfaces specified in the configuration
302 * file for which the ifindex has not been determined will have
303 * ifindex == -1, and such interfaces are found by this search, and
304 * then their ifindex values can be filled in.
305 */
gdtcb42c032004-01-05 17:55:46 +0000306 if (ifp == NULL && sdl != NULL)
paul3e95a072003-09-24 00:05:45 +0000307 {
gdtda26e3b2004-01-05 17:20:59 +0000308 /*
309 * paranoia: sanity check name length. nlen does not include
310 * trailing zero, but IFNAMSIZ max length does.
311 */
312 if (sdl->sdl_nlen >= IFNAMSIZ)
313 {
314 zlog_err ("ifm_read: illegal sdl_nlen %d\n", sdl->sdl_nlen);
315 return -1;
316 }
317
paul30be8022003-10-22 02:51:38 +0000318 memcpy (ifname, sdl->sdl_data, sdl->sdl_nlen);
paul3e95a072003-09-24 00:05:45 +0000319 ifname[sdl->sdl_nlen] = '\0';
320 ifp = if_lookup_by_name (ifname);
321 }
paul718e3742002-12-13 20:15:29 +0000322
gdtda26e3b2004-01-05 17:20:59 +0000323 /*
324 * If ifp does not exist or has an invalid index (-1), create or
325 * fill in an interface.
326 */
paul3e95a072003-09-24 00:05:45 +0000327 if ((ifp == NULL) || (ifp->ifindex == -1))
paul718e3742002-12-13 20:15:29 +0000328 {
gdt4bfbea82004-01-06 01:13:05 +0000329 /*
330 * To create or fill in an interface, a sockaddr_dl (via
331 * RTA_IFP) is required.
332 */
333 if (sdl == NULL)
paul718e3742002-12-13 20:15:29 +0000334 {
gdt4bfbea82004-01-06 01:13:05 +0000335 zlog_warn ("Interface index %d (new) missing RTA_IFP sockaddr_dl\n",
paul718e3742002-12-13 20:15:29 +0000336 ifm->ifm_index);
337 return -1;
338 }
339
paul3e95a072003-09-24 00:05:45 +0000340 if (ifp == NULL)
gdt4bfbea82004-01-06 01:13:05 +0000341 /* Interface that zebra was not previously aware of, so create. */
paul3e95a072003-09-24 00:05:45 +0000342 ifp = if_create (sdl->sdl_data, sdl->sdl_nlen);
paul718e3742002-12-13 20:15:29 +0000343
gdt4bfbea82004-01-06 01:13:05 +0000344 /*
345 * Fill in newly created interface structure, or larval
346 * structure with ifindex -1.
347 */
paul718e3742002-12-13 20:15:29 +0000348 ifp->ifindex = ifm->ifm_index;
349 ifp->flags = ifm->ifm_flags;
350#if defined(__bsdi__)
351 if_kvm_get_mtu (ifp);
352#else
353 if_get_mtu (ifp);
354#endif /* __bsdi__ */
355 if_get_metric (ifp);
356
gdt4bfbea82004-01-06 01:13:05 +0000357 /*
358 * XXX sockaddr_dl contents can be larger than the structure
359 * definition, so the user of the stored structure must be
360 * careful not to read off the end.
361 */
paul718e3742002-12-13 20:15:29 +0000362 memcpy (&ifp->sdl, sdl, sizeof (struct sockaddr_dl));
363
364 if_add_update (ifp);
365 }
366 else
gdtda26e3b2004-01-05 17:20:59 +0000367 /*
368 * Interface structure exists. Adjust stored flags from
369 * notification. If interface has up->down or down->up
370 * transition, call state change routines (to adjust routes,
371 * notify routing daemons, etc.). (Other flag changes are stored
372 * but apparently do not trigger action.)
373 */
paul718e3742002-12-13 20:15:29 +0000374 {
paul718e3742002-12-13 20:15:29 +0000375 if (if_is_up (ifp))
376 {
377 ifp->flags = ifm->ifm_flags;
378 if (! if_is_up (ifp))
379 if_down (ifp);
380 }
381 else
382 {
383 ifp->flags = ifm->ifm_flags;
384 if (if_is_up (ifp))
385 if_up (ifp);
386 }
387 }
388
389#ifdef HAVE_NET_RT_IFLIST
390 ifp->stats = ifm->ifm_data;
391#endif /* HAVE_NET_RT_IFLIST */
392
393 if (IS_ZEBRA_DEBUG_KERNEL)
ajsb6178002004-12-07 21:12:56 +0000394 zlog_debug ("interface %s index %d", ifp->name, ifp->ifindex);
paul718e3742002-12-13 20:15:29 +0000395
396 return 0;
397}
398
399/* Address read from struct ifa_msghdr. */
400void
401ifam_read_mesg (struct ifa_msghdr *ifm,
402 union sockunion *addr,
403 union sockunion *mask,
404 union sockunion *dest)
405{
406 caddr_t pnt, end;
407
408 pnt = (caddr_t)(ifm + 1);
409 end = ((caddr_t)ifm) + ifm->ifam_msglen;
410
411#define IFAMADDRGET(X,R) \
412 if (ifm->ifam_addrs & (R)) \
413 { \
gdt4bfbea82004-01-06 01:13:05 +0000414 int len = SAROUNDUP(pnt); \
paul718e3742002-12-13 20:15:29 +0000415 if (((X) != NULL) && af_check (((struct sockaddr *)pnt)->sa_family)) \
416 memcpy ((caddr_t)(X), pnt, len); \
417 pnt += len; \
418 }
419#define IFAMMASKGET(X,R) \
420 if (ifm->ifam_addrs & (R)) \
421 { \
gdt4bfbea82004-01-06 01:13:05 +0000422 int len = SAROUNDUP(pnt); \
paul718e3742002-12-13 20:15:29 +0000423 if ((X) != NULL) \
424 memcpy ((caddr_t)(X), pnt, len); \
425 pnt += len; \
426 }
427
428 /* Be sure structure is cleared */
429 memset (mask, 0, sizeof (union sockunion));
430 memset (addr, 0, sizeof (union sockunion));
431 memset (dest, 0, sizeof (union sockunion));
432
433 /* We fetch each socket variable into sockunion. */
434 IFAMADDRGET (NULL, RTA_DST);
435 IFAMADDRGET (NULL, RTA_GATEWAY);
436 IFAMMASKGET (mask, RTA_NETMASK);
437 IFAMADDRGET (NULL, RTA_GENMASK);
438 IFAMADDRGET (NULL, RTA_IFP);
439 IFAMADDRGET (addr, RTA_IFA);
440 IFAMADDRGET (NULL, RTA_AUTHOR);
441 IFAMADDRGET (dest, RTA_BRD);
442
443 /* Assert read up end point matches to end point */
444 if (pnt != end)
445 zlog_warn ("ifam_read() does't read all socket data");
446}
447
448/* Interface's address information get. */
449int
450ifam_read (struct ifa_msghdr *ifam)
451{
452 struct interface *ifp;
453 union sockunion addr, mask, gate;
454
455 /* Check does this interface exist or not. */
456 ifp = if_lookup_by_index (ifam->ifam_index);
457 if (ifp == NULL)
458 {
459 zlog_warn ("no interface for index %d", ifam->ifam_index);
460 return -1;
461 }
462
463 /* Allocate and read address information. */
464 ifam_read_mesg (ifam, &addr, &mask, &gate);
465
466 /* Check interface flag for implicit up of the interface. */
467 if_refresh (ifp);
468
469 /* Add connected address. */
470 switch (sockunion_family (&addr))
471 {
472 case AF_INET:
473 if (ifam->ifam_type == RTM_NEWADDR)
474 connected_add_ipv4 (ifp, 0, &addr.sin.sin_addr,
475 ip_masklen (mask.sin.sin_addr),
476 &gate.sin.sin_addr, NULL);
477 else
478 connected_delete_ipv4 (ifp, 0, &addr.sin.sin_addr,
479 ip_masklen (mask.sin.sin_addr),
480 &gate.sin.sin_addr, NULL);
481 break;
482#ifdef HAVE_IPV6
483 case AF_INET6:
484 /* Unset interface index from link-local address when IPv6 stack
485 is KAME. */
486 if (IN6_IS_ADDR_LINKLOCAL (&addr.sin6.sin6_addr))
487 SET_IN6_LINKLOCAL_IFINDEX (addr.sin6.sin6_addr, 0);
488
489 if (ifam->ifam_type == RTM_NEWADDR)
490 connected_add_ipv6 (ifp,
491 &addr.sin6.sin6_addr,
492 ip6_masklen (mask.sin6.sin6_addr),
493 &gate.sin6.sin6_addr);
494 else
495 connected_delete_ipv6 (ifp,
496 &addr.sin6.sin6_addr,
497 ip6_masklen (mask.sin6.sin6_addr),
498 &gate.sin6.sin6_addr);
499 break;
500#endif /* HAVE_IPV6 */
501 default:
502 /* Unsupported family silently ignore... */
503 break;
504 }
505 return 0;
506}
507
508/* Interface function for reading kernel routing table information. */
509int
510rtm_read_mesg (struct rt_msghdr *rtm,
511 union sockunion *dest,
512 union sockunion *mask,
513 union sockunion *gate)
514{
515 caddr_t pnt, end;
516
517 /* Pnt points out socket data start point. */
518 pnt = (caddr_t)(rtm + 1);
519 end = ((caddr_t)rtm) + rtm->rtm_msglen;
520
521 /* rt_msghdr version check. */
522 if (rtm->rtm_version != RTM_VERSION)
523 zlog (NULL, LOG_WARNING,
524 "Routing message version different %d should be %d."
525 "This may cause problem\n", rtm->rtm_version, RTM_VERSION);
526
527#define RTMADDRGET(X,R) \
528 if (rtm->rtm_addrs & (R)) \
529 { \
gdt4bfbea82004-01-06 01:13:05 +0000530 int len = SAROUNDUP (pnt); \
paul718e3742002-12-13 20:15:29 +0000531 if (((X) != NULL) && af_check (((struct sockaddr *)pnt)->sa_family)) \
532 memcpy ((caddr_t)(X), pnt, len); \
533 pnt += len; \
534 }
535#define RTMMASKGET(X,R) \
536 if (rtm->rtm_addrs & (R)) \
537 { \
gdt4bfbea82004-01-06 01:13:05 +0000538 int len = SAROUNDUP (pnt); \
paul718e3742002-12-13 20:15:29 +0000539 if ((X) != NULL) \
540 memcpy ((caddr_t)(X), pnt, len); \
541 pnt += len; \
542 }
543
544 /* Be sure structure is cleared */
545 memset (dest, 0, sizeof (union sockunion));
546 memset (gate, 0, sizeof (union sockunion));
547 memset (mask, 0, sizeof (union sockunion));
548
549 /* We fetch each socket variable into sockunion. */
550 RTMADDRGET (dest, RTA_DST);
551 RTMADDRGET (gate, RTA_GATEWAY);
552 RTMMASKGET (mask, RTA_NETMASK);
553 RTMADDRGET (NULL, RTA_GENMASK);
554 RTMADDRGET (NULL, RTA_IFP);
555 RTMADDRGET (NULL, RTA_IFA);
556 RTMADDRGET (NULL, RTA_AUTHOR);
557 RTMADDRGET (NULL, RTA_BRD);
558
559 /* If there is netmask information set it's family same as
560 destination family*/
561 if (rtm->rtm_addrs & RTA_NETMASK)
562 mask->sa.sa_family = dest->sa.sa_family;
563
564 /* Assert read up to the end of pointer. */
565 if (pnt != end)
566 zlog (NULL, LOG_WARNING, "rtm_read() does't read all socket data.");
567
568 return rtm->rtm_flags;
569}
570
571void
572rtm_read (struct rt_msghdr *rtm)
573{
574 int flags;
575 u_char zebra_flags;
576 union sockunion dest, mask, gate;
577
578 zebra_flags = 0;
579
580 /* Discard self send message. */
581 if (rtm->rtm_type != RTM_GET
582 && (rtm->rtm_pid == pid || rtm->rtm_pid == old_pid))
583 return;
584
585 /* Read destination and netmask and gateway from rtm message
586 structure. */
587 flags = rtm_read_mesg (rtm, &dest, &mask, &gate);
588
589#ifdef RTF_CLONED /*bsdi, netbsd 1.6*/
590 if (flags & RTF_CLONED)
591 return;
592#endif
593#ifdef RTF_WASCLONED /*freebsd*/
594 if (flags & RTF_WASCLONED)
595 return;
596#endif
597
598 if ((rtm->rtm_type == RTM_ADD) && ! (flags & RTF_UP))
599 return;
600
601 /* This is connected route. */
602 if (! (flags & RTF_GATEWAY))
603 return;
604
605 if (flags & RTF_PROTO1)
606 SET_FLAG (zebra_flags, ZEBRA_FLAG_SELFROUTE);
607
608 /* This is persistent route. */
609 if (flags & RTF_STATIC)
610 SET_FLAG (zebra_flags, ZEBRA_FLAG_STATIC);
611
hasso81dfcaa2003-05-25 19:21:25 +0000612 /* This is a reject or blackhole route */
613 if (flags & RTF_REJECT)
614 SET_FLAG (zebra_flags, ZEBRA_FLAG_REJECT);
615 if (flags & RTF_BLACKHOLE)
616 SET_FLAG (zebra_flags, ZEBRA_FLAG_BLACKHOLE);
617
paul718e3742002-12-13 20:15:29 +0000618 if (dest.sa.sa_family == AF_INET)
619 {
620 struct prefix_ipv4 p;
621
622 p.family = AF_INET;
623 p.prefix = dest.sin.sin_addr;
624 if (flags & RTF_HOST)
625 p.prefixlen = IPV4_MAX_PREFIXLEN;
626 else
627 p.prefixlen = ip_masklen (mask.sin.sin_addr);
628
629 if (rtm->rtm_type == RTM_GET || rtm->rtm_type == RTM_ADD)
630 rib_add_ipv4 (ZEBRA_ROUTE_KERNEL, zebra_flags,
631 &p, &gate.sin.sin_addr, 0, 0, 0, 0);
632 else
633 rib_delete_ipv4 (ZEBRA_ROUTE_KERNEL, zebra_flags,
634 &p, &gate.sin.sin_addr, 0, 0);
635 }
636#ifdef HAVE_IPV6
637 if (dest.sa.sa_family == AF_INET6)
638 {
639 struct prefix_ipv6 p;
640 unsigned int ifindex = 0;
641
642 p.family = AF_INET6;
643 p.prefix = dest.sin6.sin6_addr;
644 if (flags & RTF_HOST)
645 p.prefixlen = IPV6_MAX_PREFIXLEN;
646 else
647 p.prefixlen = ip6_masklen (mask.sin6.sin6_addr);
648
649#ifdef KAME
650 if (IN6_IS_ADDR_LINKLOCAL (&gate.sin6.sin6_addr))
651 {
652 ifindex = IN6_LINKLOCAL_IFINDEX (gate.sin6.sin6_addr);
653 SET_IN6_LINKLOCAL_IFINDEX (gate.sin6.sin6_addr, 0);
654 }
655#endif /* KAME */
656
657 if (rtm->rtm_type == RTM_GET || rtm->rtm_type == RTM_ADD)
658 rib_add_ipv6 (ZEBRA_ROUTE_KERNEL, zebra_flags,
659 &p, &gate.sin6.sin6_addr, ifindex, 0);
660 else
661 rib_delete_ipv6 (ZEBRA_ROUTE_KERNEL, zebra_flags,
662 &p, &gate.sin6.sin6_addr, ifindex, 0);
663 }
664#endif /* HAVE_IPV6 */
665}
666
667/* Interface function for the kernel routing table updates. Support
668 for RTM_CHANGE will be needed. */
669int
670rtm_write (int message,
671 union sockunion *dest,
672 union sockunion *mask,
673 union sockunion *gate,
674 unsigned int index,
675 int zebra_flags,
676 int metric)
677{
678 int ret;
679 caddr_t pnt;
680 struct interface *ifp;
681 struct sockaddr_in tmp_gate;
682#ifdef HAVE_IPV6
683 struct sockaddr_in6 tmp_gate6;
684#endif /* HAVE_IPV6 */
685
686 /* Sequencial number of routing message. */
687 static int msg_seq = 0;
688
689 /* Struct of rt_msghdr and buffer for storing socket's data. */
690 struct
691 {
692 struct rt_msghdr rtm;
693 char buf[512];
694 } msg;
695
696 memset (&tmp_gate, 0, sizeof (struct sockaddr_in));
697 tmp_gate.sin_family = AF_INET;
698#ifdef HAVE_SIN_LEN
699 tmp_gate.sin_len = sizeof (struct sockaddr_in);
700#endif /* HAVE_SIN_LEN */
701
702#ifdef HAVE_IPV6
703 memset (&tmp_gate6, 0, sizeof (struct sockaddr_in6));
704 tmp_gate6.sin6_family = AF_INET6;
705#ifdef SIN6_LEN
706 tmp_gate6.sin6_len = sizeof (struct sockaddr_in6);
707#endif /* SIN6_LEN */
708#endif /* HAVE_IPV6 */
709
710 if (routing_sock < 0)
711 return ZEBRA_ERR_EPERM;
712
713 /* Clear and set rt_msghdr values */
714 memset (&msg, 0, sizeof (struct rt_msghdr));
715 msg.rtm.rtm_version = RTM_VERSION;
716 msg.rtm.rtm_type = message;
717 msg.rtm.rtm_seq = msg_seq++;
718 msg.rtm.rtm_addrs = RTA_DST;
719 msg.rtm.rtm_addrs |= RTA_GATEWAY;
720 msg.rtm.rtm_flags = RTF_UP;
721 msg.rtm.rtm_index = index;
722
723 if (metric != 0)
724 {
725 msg.rtm.rtm_rmx.rmx_hopcount = metric;
726 msg.rtm.rtm_inits |= RTV_HOPCOUNT;
727 }
728
729 ifp = if_lookup_by_index (index);
730
731 if (gate && message == RTM_ADD)
732 msg.rtm.rtm_flags |= RTF_GATEWAY;
733
734 if (! gate && message == RTM_ADD && ifp &&
735 (ifp->flags & IFF_POINTOPOINT) == 0)
736 msg.rtm.rtm_flags |= RTF_CLONING;
737
738 /* If no protocol specific gateway is specified, use link
739 address for gateway. */
740 if (! gate)
741 {
742 if (!ifp)
743 {
744 zlog_warn ("no gateway found for interface index %d", index);
745 return -1;
746 }
747 gate = (union sockunion *) & ifp->sdl;
748 }
749
750 if (mask)
751 msg.rtm.rtm_addrs |= RTA_NETMASK;
752 else if (message == RTM_ADD)
753 msg.rtm.rtm_flags |= RTF_HOST;
754
755 /* Tagging route with flags */
756 msg.rtm.rtm_flags |= (RTF_PROTO1);
757
758 /* Additional flags. */
759 if (zebra_flags & ZEBRA_FLAG_BLACKHOLE)
760 msg.rtm.rtm_flags |= RTF_BLACKHOLE;
hasso81dfcaa2003-05-25 19:21:25 +0000761 if (zebra_flags & ZEBRA_FLAG_REJECT)
762 msg.rtm.rtm_flags |= RTF_REJECT;
763
paul718e3742002-12-13 20:15:29 +0000764
765#ifdef HAVE_SIN_LEN
766#define SOCKADDRSET(X,R) \
767 if (msg.rtm.rtm_addrs & (R)) \
768 { \
769 int len = ROUNDUP ((X)->sa.sa_len); \
770 memcpy (pnt, (caddr_t)(X), len); \
771 pnt += len; \
772 }
773#else
774#define SOCKADDRSET(X,R) \
775 if (msg.rtm.rtm_addrs & (R)) \
776 { \
777 int len = ROUNDUP (sizeof((X)->sa)); \
778 memcpy (pnt, (caddr_t)(X), len); \
779 pnt += len; \
780 }
781#endif /* HAVE_SIN_LEN */
782
783 pnt = (caddr_t) msg.buf;
784
785 /* Write each socket data into rtm message buffer */
786 SOCKADDRSET (dest, RTA_DST);
787 SOCKADDRSET (gate, RTA_GATEWAY);
788 SOCKADDRSET (mask, RTA_NETMASK);
789
790 msg.rtm.rtm_msglen = pnt - (caddr_t) &msg;
791
792 ret = write (routing_sock, &msg, msg.rtm.rtm_msglen);
793
794 if (ret != msg.rtm.rtm_msglen)
795 {
796 if (errno == EEXIST)
797 return ZEBRA_ERR_RTEXIST;
798 if (errno == ENETUNREACH)
799 return ZEBRA_ERR_RTUNREACH;
800
ajs6099b3b2004-11-20 02:06:59 +0000801 zlog_warn ("write : %s (%d)", safe_strerror (errno), errno);
paul718e3742002-12-13 20:15:29 +0000802 return -1;
803 }
804 return 0;
805}
806
807
808#include "thread.h"
809#include "zebra/zserv.h"
810
paul718e3742002-12-13 20:15:29 +0000811/* For debug purpose. */
ajsb6178002004-12-07 21:12:56 +0000812static void
paul718e3742002-12-13 20:15:29 +0000813rtmsg_debug (struct rt_msghdr *rtm)
814{
815 char *type = "Unknown";
816 struct message *mes;
817
818 for (mes = rtm_type_str; mes->str; mes++)
819 if (mes->key == rtm->rtm_type)
820 {
821 type = mes->str;
822 break;
823 }
824
ajsb6178002004-12-07 21:12:56 +0000825 zlog_debug ("Kernel: Len: %d Type: %s", rtm->rtm_msglen, type);
paul718e3742002-12-13 20:15:29 +0000826 rtm_flag_dump (rtm->rtm_flags);
ajsb6178002004-12-07 21:12:56 +0000827 zlog_debug ("Kernel: message seq %d", rtm->rtm_seq);
828 zlog_debug ("Kernel: pid %d", rtm->rtm_pid);
paul718e3742002-12-13 20:15:29 +0000829}
830
831/* This is pretty gross, better suggestions welcome -- mhandler */
832#ifndef RTAX_MAX
833#ifdef RTA_NUMBITS
834#define RTAX_MAX RTA_NUMBITS
835#else
836#define RTAX_MAX 8
837#endif /* RTA_NUMBITS */
838#endif /* RTAX_MAX */
839
840/* Kernel routing table and interface updates via routing socket. */
841int
842kernel_read (struct thread *thread)
843{
844 int sock;
845 int nbytes;
846 struct rt_msghdr *rtm;
847
gdtdbee01f2004-01-06 00:36:51 +0000848 /*
849 * This must be big enough for any message the kernel might send.
gdtb27900b2004-01-08 15:44:29 +0000850 * Rather than determining how many sockaddrs of what size might be
851 * in each particular message, just use RTAX_MAX of sockaddr_storage
852 * for each. Note that the sockaddrs must be after each message
853 * definition, or rather after whichever happens to be the largest,
854 * since the buffer needs to be big enough for a message and the
855 * sockaddrs together.
gdtdbee01f2004-01-06 00:36:51 +0000856 */
paul718e3742002-12-13 20:15:29 +0000857 union
858 {
859 /* Routing information. */
860 struct
861 {
862 struct rt_msghdr rtm;
gdtb27900b2004-01-08 15:44:29 +0000863 struct sockaddr_storage addr[RTAX_MAX];
paul718e3742002-12-13 20:15:29 +0000864 } r;
865
866 /* Interface information. */
867 struct
868 {
869 struct if_msghdr ifm;
gdtb27900b2004-01-08 15:44:29 +0000870 struct sockaddr_storage addr[RTAX_MAX];
paul718e3742002-12-13 20:15:29 +0000871 } im;
872
873 /* Interface address information. */
874 struct
875 {
876 struct ifa_msghdr ifa;
gdtb27900b2004-01-08 15:44:29 +0000877 struct sockaddr_storage addr[RTAX_MAX];
paul718e3742002-12-13 20:15:29 +0000878 } ia;
879
880#ifdef RTM_IFANNOUNCE
881 /* Interface arrival/departure */
882 struct
883 {
884 struct if_announcemsghdr ifan;
gdtb27900b2004-01-08 15:44:29 +0000885 struct sockaddr_storage addr[RTAX_MAX];
paul718e3742002-12-13 20:15:29 +0000886 } ian;
887#endif /* RTM_IFANNOUNCE */
888
889 } buf;
890
891 /* Fetch routing socket. */
892 sock = THREAD_FD (thread);
893
894 nbytes= read (sock, &buf, sizeof buf);
895
896 if (nbytes <= 0)
897 {
898 if (nbytes < 0 && errno != EWOULDBLOCK && errno != EAGAIN)
ajs6099b3b2004-11-20 02:06:59 +0000899 zlog_warn ("routing socket error: %s", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +0000900 return 0;
901 }
902
paul9bcdb632003-07-08 08:09:45 +0000903 thread_add_read (zebrad.master, kernel_read, NULL, sock);
paul718e3742002-12-13 20:15:29 +0000904
hasso726f9b22003-05-25 21:04:54 +0000905 if (IS_ZEBRA_DEBUG_KERNEL)
906 rtmsg_debug (&buf.r.rtm);
paul718e3742002-12-13 20:15:29 +0000907
908 rtm = &buf.r.rtm;
909
gdtb27900b2004-01-08 15:44:29 +0000910 /*
911 * Ensure that we didn't drop any data, so that processing routines
912 * can assume they have the whole message.
913 */
gdtda26e3b2004-01-05 17:20:59 +0000914 if (rtm->rtm_msglen != nbytes)
915 {
916 zlog_warn ("kernel_read: rtm->rtm_msglen %d, nbytes %d, type %d\n",
917 rtm->rtm_msglen, nbytes, rtm->rtm_type);
918 return -1;
919 }
920
paul718e3742002-12-13 20:15:29 +0000921 switch (rtm->rtm_type)
922 {
923 case RTM_ADD:
924 case RTM_DELETE:
925 rtm_read (rtm);
926 break;
927 case RTM_IFINFO:
928 ifm_read (&buf.im.ifm);
929 break;
930 case RTM_NEWADDR:
931 case RTM_DELADDR:
932 ifam_read (&buf.ia.ifa);
933 break;
934#ifdef RTM_IFANNOUNCE
935 case RTM_IFANNOUNCE:
936 ifan_read (&buf.ian.ifan);
937 break;
938#endif /* RTM_IFANNOUNCE */
939 default:
hasso726f9b22003-05-25 21:04:54 +0000940 if (IS_ZEBRA_DEBUG_KERNEL)
ajsb6178002004-12-07 21:12:56 +0000941 zlog_debug("Unprocessed RTM_type: %d", rtm->rtm_type);
paul718e3742002-12-13 20:15:29 +0000942 break;
943 }
944 return 0;
945}
946
947/* Make routing socket. */
948void
949routing_socket ()
950{
pauledd7c242003-06-04 13:59:38 +0000951 if ( zserv_privs.change (ZPRIVS_RAISE) )
952 zlog_err ("routing_socket: Can't raise privileges");
953
paul718e3742002-12-13 20:15:29 +0000954 routing_sock = socket (AF_ROUTE, SOCK_RAW, 0);
955
956 if (routing_sock < 0)
957 {
pauledd7c242003-06-04 13:59:38 +0000958 if ( zserv_privs.change (ZPRIVS_LOWER) )
959 zlog_err ("routing_socket: Can't lower privileges");
paul718e3742002-12-13 20:15:29 +0000960 zlog_warn ("Can't init kernel routing socket");
961 return;
962 }
963
964 if (fcntl (routing_sock, F_SETFL, O_NONBLOCK) < 0)
965 zlog_warn ("Can't set O_NONBLOCK to routing socket");
pauledd7c242003-06-04 13:59:38 +0000966 if ( zserv_privs.change (ZPRIVS_LOWER) )
967 zlog_err ("routing_socket: Can't lower privileges");
paul718e3742002-12-13 20:15:29 +0000968
969 /* kernel_read needs rewrite. */
paul9bcdb632003-07-08 08:09:45 +0000970 thread_add_read (zebrad.master, kernel_read, NULL, routing_sock);
paul718e3742002-12-13 20:15:29 +0000971}
972
973/* Exported interface function. This function simply calls
974 routing_socket (). */
975void
976kernel_init ()
977{
978 routing_socket ();
979}