blob: 121256302ce6763db208b27eadfbc3519b44dbd1 [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;
paul718e3742002-12-13 20:15:29 +0000238
gdtda26e3b2004-01-05 17:20:59 +0000239 /* paranoia: sanity check structure */
240 if (ifm->ifm_msglen < sizeof(struct if_msghdr))
241 {
242 zlog_err ("ifm_read: ifm->ifm_msglen %d too short\n",
243 ifm->ifm_msglen);
244 return -1;
245 }
246
247 /*
gdt4bfbea82004-01-06 01:13:05 +0000248 * Check for a sockaddr_dl following the message. First, point to
249 * where a socakddr might be if one follows the message.
gdtda26e3b2004-01-05 17:20:59 +0000250 */
gdt4bfbea82004-01-06 01:13:05 +0000251 cp = (void *)(ifm + 1);
252
paul3e95a072003-09-24 00:05:45 +0000253#ifdef SUNOS_5
paul3e95a072003-09-24 00:05:45 +0000254 /*
gdt4bfbea82004-01-06 01:13:05 +0000255 * XXX This behavior should be narrowed to only the kernel versions
256 * for which the structures returned do not match the headers.
257 *
paul3e95a072003-09-24 00:05:45 +0000258 * if_msghdr_t on 64 bit kernels in Solaris 9 and earlier versions
gdt4bfbea82004-01-06 01:13:05 +0000259 * is 12 bytes larger than the 32 bit version.
paul3e95a072003-09-24 00:05:45 +0000260 */
gdt4bfbea82004-01-06 01:13:05 +0000261 if (((struct sockaddr *) cp)->sa_family == AF_UNSPEC)
paul3e95a072003-09-24 00:05:45 +0000262 cp = cp + 12;
paul3e95a072003-09-24 00:05:45 +0000263#endif
paul718e3742002-12-13 20:15:29 +0000264
gdt4bfbea82004-01-06 01:13:05 +0000265 /*
266 * Check for each sockaddr in turn, advancing over it. After this
267 * loop, sdl should point to a sockaddr_dl iff one was present.
268 */
269 for (i = 1; i != 0; i <<= 1)
270 {
271 if (i & ifm->ifm_addrs)
272 {
273 if (i == RTA_IFP)
274 {
275 sdl = (struct sockaddr_dl *)cp;
276 break;
277 }
gdt6a250b02004-12-09 14:48:12 +0000278 /* XXX warning: pointer of type `void *' used in arithmetic */
gdt4bfbea82004-01-06 01:13:05 +0000279 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
ajsd2fc8892005-04-02 18:38:43 +0000303 * ifindex == IFINDEX_INTERNAL, and such interfaces are found by this search,
304 * and then their ifindex values can be filled in.
gdtda26e3b2004-01-05 17:20:59 +0000305 */
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.
ajsa3491982005-04-02 22:50:38 +0000311 *
312 * XXX Is this test correct? Should it be '>=' or '>'? And is it even
313 * necessary now that we are using if_lookup_by_name_len?
gdtda26e3b2004-01-05 17:20:59 +0000314 */
315 if (sdl->sdl_nlen >= IFNAMSIZ)
316 {
317 zlog_err ("ifm_read: illegal sdl_nlen %d\n", sdl->sdl_nlen);
318 return -1;
319 }
320
ajsa3491982005-04-02 22:50:38 +0000321 ifp = if_lookup_by_name_len (sdl->sdl_data, sdl->sdl_nlen);
paul3e95a072003-09-24 00:05:45 +0000322 }
paul718e3742002-12-13 20:15:29 +0000323
gdtda26e3b2004-01-05 17:20:59 +0000324 /*
ajsd2fc8892005-04-02 18:38:43 +0000325 * If ifp does not exist or has an invalid index (IFINDEX_INTERNAL), create or
gdtda26e3b2004-01-05 17:20:59 +0000326 * fill in an interface.
327 */
ajsd2fc8892005-04-02 18:38:43 +0000328 if ((ifp == NULL) || (ifp->ifindex == IFINDEX_INTERNAL))
paul718e3742002-12-13 20:15:29 +0000329 {
gdt4bfbea82004-01-06 01:13:05 +0000330 /*
331 * To create or fill in an interface, a sockaddr_dl (via
332 * RTA_IFP) is required.
333 */
334 if (sdl == NULL)
paul718e3742002-12-13 20:15:29 +0000335 {
gdt4bfbea82004-01-06 01:13:05 +0000336 zlog_warn ("Interface index %d (new) missing RTA_IFP sockaddr_dl\n",
paul718e3742002-12-13 20:15:29 +0000337 ifm->ifm_index);
338 return -1;
339 }
340
paul3e95a072003-09-24 00:05:45 +0000341 if (ifp == NULL)
gdt4bfbea82004-01-06 01:13:05 +0000342 /* Interface that zebra was not previously aware of, so create. */
paul3e95a072003-09-24 00:05:45 +0000343 ifp = if_create (sdl->sdl_data, sdl->sdl_nlen);
paul718e3742002-12-13 20:15:29 +0000344
gdt4bfbea82004-01-06 01:13:05 +0000345 /*
346 * Fill in newly created interface structure, or larval
ajsd2fc8892005-04-02 18:38:43 +0000347 * structure with ifindex IFINDEX_INTERNAL.
gdt4bfbea82004-01-06 01:13:05 +0000348 */
paul718e3742002-12-13 20:15:29 +0000349 ifp->ifindex = ifm->ifm_index;
350 ifp->flags = ifm->ifm_flags;
351#if defined(__bsdi__)
352 if_kvm_get_mtu (ifp);
353#else
354 if_get_mtu (ifp);
355#endif /* __bsdi__ */
356 if_get_metric (ifp);
357
gdt4bfbea82004-01-06 01:13:05 +0000358 /*
359 * XXX sockaddr_dl contents can be larger than the structure
360 * definition, so the user of the stored structure must be
361 * careful not to read off the end.
362 */
paul718e3742002-12-13 20:15:29 +0000363 memcpy (&ifp->sdl, sdl, sizeof (struct sockaddr_dl));
364
365 if_add_update (ifp);
366 }
367 else
gdtda26e3b2004-01-05 17:20:59 +0000368 /*
369 * Interface structure exists. Adjust stored flags from
370 * notification. If interface has up->down or down->up
371 * transition, call state change routines (to adjust routes,
372 * notify routing daemons, etc.). (Other flag changes are stored
373 * but apparently do not trigger action.)
374 */
paul718e3742002-12-13 20:15:29 +0000375 {
paul718e3742002-12-13 20:15:29 +0000376 if (if_is_up (ifp))
377 {
378 ifp->flags = ifm->ifm_flags;
379 if (! if_is_up (ifp))
380 if_down (ifp);
381 }
382 else
383 {
384 ifp->flags = ifm->ifm_flags;
385 if (if_is_up (ifp))
386 if_up (ifp);
387 }
388 }
389
390#ifdef HAVE_NET_RT_IFLIST
391 ifp->stats = ifm->ifm_data;
392#endif /* HAVE_NET_RT_IFLIST */
393
394 if (IS_ZEBRA_DEBUG_KERNEL)
ajsb6178002004-12-07 21:12:56 +0000395 zlog_debug ("interface %s index %d", ifp->name, ifp->ifindex);
paul718e3742002-12-13 20:15:29 +0000396
397 return 0;
398}
399
400/* Address read from struct ifa_msghdr. */
401void
402ifam_read_mesg (struct ifa_msghdr *ifm,
403 union sockunion *addr,
404 union sockunion *mask,
405 union sockunion *dest)
406{
407 caddr_t pnt, end;
408
409 pnt = (caddr_t)(ifm + 1);
410 end = ((caddr_t)ifm) + ifm->ifam_msglen;
411
412#define IFAMADDRGET(X,R) \
413 if (ifm->ifam_addrs & (R)) \
414 { \
gdt4bfbea82004-01-06 01:13:05 +0000415 int len = SAROUNDUP(pnt); \
paul718e3742002-12-13 20:15:29 +0000416 if (((X) != NULL) && af_check (((struct sockaddr *)pnt)->sa_family)) \
417 memcpy ((caddr_t)(X), pnt, len); \
418 pnt += len; \
419 }
420#define IFAMMASKGET(X,R) \
421 if (ifm->ifam_addrs & (R)) \
422 { \
gdt4bfbea82004-01-06 01:13:05 +0000423 int len = SAROUNDUP(pnt); \
paul718e3742002-12-13 20:15:29 +0000424 if ((X) != NULL) \
425 memcpy ((caddr_t)(X), pnt, len); \
426 pnt += len; \
427 }
428
429 /* Be sure structure is cleared */
430 memset (mask, 0, sizeof (union sockunion));
431 memset (addr, 0, sizeof (union sockunion));
432 memset (dest, 0, sizeof (union sockunion));
433
434 /* We fetch each socket variable into sockunion. */
435 IFAMADDRGET (NULL, RTA_DST);
436 IFAMADDRGET (NULL, RTA_GATEWAY);
437 IFAMMASKGET (mask, RTA_NETMASK);
438 IFAMADDRGET (NULL, RTA_GENMASK);
439 IFAMADDRGET (NULL, RTA_IFP);
440 IFAMADDRGET (addr, RTA_IFA);
441 IFAMADDRGET (NULL, RTA_AUTHOR);
442 IFAMADDRGET (dest, RTA_BRD);
443
444 /* Assert read up end point matches to end point */
445 if (pnt != end)
446 zlog_warn ("ifam_read() does't read all socket data");
447}
448
449/* Interface's address information get. */
450int
451ifam_read (struct ifa_msghdr *ifam)
452{
453 struct interface *ifp;
454 union sockunion addr, mask, gate;
455
456 /* Check does this interface exist or not. */
457 ifp = if_lookup_by_index (ifam->ifam_index);
458 if (ifp == NULL)
459 {
460 zlog_warn ("no interface for index %d", ifam->ifam_index);
461 return -1;
462 }
463
464 /* Allocate and read address information. */
465 ifam_read_mesg (ifam, &addr, &mask, &gate);
466
467 /* Check interface flag for implicit up of the interface. */
468 if_refresh (ifp);
469
470 /* Add connected address. */
471 switch (sockunion_family (&addr))
472 {
473 case AF_INET:
474 if (ifam->ifam_type == RTM_NEWADDR)
475 connected_add_ipv4 (ifp, 0, &addr.sin.sin_addr,
476 ip_masklen (mask.sin.sin_addr),
477 &gate.sin.sin_addr, NULL);
478 else
479 connected_delete_ipv4 (ifp, 0, &addr.sin.sin_addr,
480 ip_masklen (mask.sin.sin_addr),
481 &gate.sin.sin_addr, NULL);
482 break;
483#ifdef HAVE_IPV6
484 case AF_INET6:
485 /* Unset interface index from link-local address when IPv6 stack
486 is KAME. */
487 if (IN6_IS_ADDR_LINKLOCAL (&addr.sin6.sin6_addr))
488 SET_IN6_LINKLOCAL_IFINDEX (addr.sin6.sin6_addr, 0);
489
490 if (ifam->ifam_type == RTM_NEWADDR)
491 connected_add_ipv6 (ifp,
492 &addr.sin6.sin6_addr,
493 ip6_masklen (mask.sin6.sin6_addr),
494 &gate.sin6.sin6_addr);
495 else
496 connected_delete_ipv6 (ifp,
497 &addr.sin6.sin6_addr,
498 ip6_masklen (mask.sin6.sin6_addr),
499 &gate.sin6.sin6_addr);
500 break;
501#endif /* HAVE_IPV6 */
502 default:
503 /* Unsupported family silently ignore... */
504 break;
505 }
506 return 0;
507}
508
509/* Interface function for reading kernel routing table information. */
510int
511rtm_read_mesg (struct rt_msghdr *rtm,
512 union sockunion *dest,
513 union sockunion *mask,
514 union sockunion *gate)
515{
516 caddr_t pnt, end;
517
518 /* Pnt points out socket data start point. */
519 pnt = (caddr_t)(rtm + 1);
520 end = ((caddr_t)rtm) + rtm->rtm_msglen;
521
522 /* rt_msghdr version check. */
523 if (rtm->rtm_version != RTM_VERSION)
524 zlog (NULL, LOG_WARNING,
525 "Routing message version different %d should be %d."
526 "This may cause problem\n", rtm->rtm_version, RTM_VERSION);
527
528#define RTMADDRGET(X,R) \
529 if (rtm->rtm_addrs & (R)) \
530 { \
gdt4bfbea82004-01-06 01:13:05 +0000531 int len = SAROUNDUP (pnt); \
paul718e3742002-12-13 20:15:29 +0000532 if (((X) != NULL) && af_check (((struct sockaddr *)pnt)->sa_family)) \
533 memcpy ((caddr_t)(X), pnt, len); \
534 pnt += len; \
535 }
536#define RTMMASKGET(X,R) \
537 if (rtm->rtm_addrs & (R)) \
538 { \
gdt4bfbea82004-01-06 01:13:05 +0000539 int len = SAROUNDUP (pnt); \
paul718e3742002-12-13 20:15:29 +0000540 if ((X) != NULL) \
541 memcpy ((caddr_t)(X), pnt, len); \
542 pnt += len; \
543 }
544
545 /* Be sure structure is cleared */
546 memset (dest, 0, sizeof (union sockunion));
547 memset (gate, 0, sizeof (union sockunion));
548 memset (mask, 0, sizeof (union sockunion));
549
550 /* We fetch each socket variable into sockunion. */
551 RTMADDRGET (dest, RTA_DST);
552 RTMADDRGET (gate, RTA_GATEWAY);
553 RTMMASKGET (mask, RTA_NETMASK);
554 RTMADDRGET (NULL, RTA_GENMASK);
555 RTMADDRGET (NULL, RTA_IFP);
556 RTMADDRGET (NULL, RTA_IFA);
557 RTMADDRGET (NULL, RTA_AUTHOR);
558 RTMADDRGET (NULL, RTA_BRD);
559
560 /* If there is netmask information set it's family same as
561 destination family*/
562 if (rtm->rtm_addrs & RTA_NETMASK)
563 mask->sa.sa_family = dest->sa.sa_family;
564
565 /* Assert read up to the end of pointer. */
566 if (pnt != end)
567 zlog (NULL, LOG_WARNING, "rtm_read() does't read all socket data.");
568
569 return rtm->rtm_flags;
570}
571
572void
573rtm_read (struct rt_msghdr *rtm)
574{
575 int flags;
576 u_char zebra_flags;
577 union sockunion dest, mask, gate;
578
579 zebra_flags = 0;
580
581 /* Discard self send message. */
582 if (rtm->rtm_type != RTM_GET
583 && (rtm->rtm_pid == pid || rtm->rtm_pid == old_pid))
584 return;
585
586 /* Read destination and netmask and gateway from rtm message
587 structure. */
588 flags = rtm_read_mesg (rtm, &dest, &mask, &gate);
589
590#ifdef RTF_CLONED /*bsdi, netbsd 1.6*/
591 if (flags & RTF_CLONED)
592 return;
593#endif
594#ifdef RTF_WASCLONED /*freebsd*/
595 if (flags & RTF_WASCLONED)
596 return;
597#endif
598
599 if ((rtm->rtm_type == RTM_ADD) && ! (flags & RTF_UP))
600 return;
601
602 /* This is connected route. */
603 if (! (flags & RTF_GATEWAY))
604 return;
605
606 if (flags & RTF_PROTO1)
607 SET_FLAG (zebra_flags, ZEBRA_FLAG_SELFROUTE);
608
609 /* This is persistent route. */
610 if (flags & RTF_STATIC)
611 SET_FLAG (zebra_flags, ZEBRA_FLAG_STATIC);
612
hasso81dfcaa2003-05-25 19:21:25 +0000613 /* This is a reject or blackhole route */
614 if (flags & RTF_REJECT)
615 SET_FLAG (zebra_flags, ZEBRA_FLAG_REJECT);
616 if (flags & RTF_BLACKHOLE)
617 SET_FLAG (zebra_flags, ZEBRA_FLAG_BLACKHOLE);
618
paul718e3742002-12-13 20:15:29 +0000619 if (dest.sa.sa_family == AF_INET)
620 {
621 struct prefix_ipv4 p;
622
623 p.family = AF_INET;
624 p.prefix = dest.sin.sin_addr;
625 if (flags & RTF_HOST)
626 p.prefixlen = IPV4_MAX_PREFIXLEN;
627 else
628 p.prefixlen = ip_masklen (mask.sin.sin_addr);
629
630 if (rtm->rtm_type == RTM_GET || rtm->rtm_type == RTM_ADD)
631 rib_add_ipv4 (ZEBRA_ROUTE_KERNEL, zebra_flags,
632 &p, &gate.sin.sin_addr, 0, 0, 0, 0);
633 else
634 rib_delete_ipv4 (ZEBRA_ROUTE_KERNEL, zebra_flags,
635 &p, &gate.sin.sin_addr, 0, 0);
636 }
637#ifdef HAVE_IPV6
638 if (dest.sa.sa_family == AF_INET6)
639 {
640 struct prefix_ipv6 p;
641 unsigned int ifindex = 0;
642
643 p.family = AF_INET6;
644 p.prefix = dest.sin6.sin6_addr;
645 if (flags & RTF_HOST)
646 p.prefixlen = IPV6_MAX_PREFIXLEN;
647 else
648 p.prefixlen = ip6_masklen (mask.sin6.sin6_addr);
649
650#ifdef KAME
651 if (IN6_IS_ADDR_LINKLOCAL (&gate.sin6.sin6_addr))
652 {
653 ifindex = IN6_LINKLOCAL_IFINDEX (gate.sin6.sin6_addr);
654 SET_IN6_LINKLOCAL_IFINDEX (gate.sin6.sin6_addr, 0);
655 }
656#endif /* KAME */
657
658 if (rtm->rtm_type == RTM_GET || rtm->rtm_type == RTM_ADD)
659 rib_add_ipv6 (ZEBRA_ROUTE_KERNEL, zebra_flags,
660 &p, &gate.sin6.sin6_addr, ifindex, 0);
661 else
662 rib_delete_ipv6 (ZEBRA_ROUTE_KERNEL, zebra_flags,
663 &p, &gate.sin6.sin6_addr, ifindex, 0);
664 }
665#endif /* HAVE_IPV6 */
666}
667
668/* Interface function for the kernel routing table updates. Support
669 for RTM_CHANGE will be needed. */
670int
671rtm_write (int message,
672 union sockunion *dest,
673 union sockunion *mask,
674 union sockunion *gate,
675 unsigned int index,
676 int zebra_flags,
677 int metric)
678{
679 int ret;
680 caddr_t pnt;
681 struct interface *ifp;
682 struct sockaddr_in tmp_gate;
683#ifdef HAVE_IPV6
684 struct sockaddr_in6 tmp_gate6;
685#endif /* HAVE_IPV6 */
686
687 /* Sequencial number of routing message. */
688 static int msg_seq = 0;
689
690 /* Struct of rt_msghdr and buffer for storing socket's data. */
691 struct
692 {
693 struct rt_msghdr rtm;
694 char buf[512];
695 } msg;
696
697 memset (&tmp_gate, 0, sizeof (struct sockaddr_in));
698 tmp_gate.sin_family = AF_INET;
699#ifdef HAVE_SIN_LEN
700 tmp_gate.sin_len = sizeof (struct sockaddr_in);
701#endif /* HAVE_SIN_LEN */
702
703#ifdef HAVE_IPV6
704 memset (&tmp_gate6, 0, sizeof (struct sockaddr_in6));
705 tmp_gate6.sin6_family = AF_INET6;
706#ifdef SIN6_LEN
707 tmp_gate6.sin6_len = sizeof (struct sockaddr_in6);
708#endif /* SIN6_LEN */
709#endif /* HAVE_IPV6 */
710
711 if (routing_sock < 0)
712 return ZEBRA_ERR_EPERM;
713
714 /* Clear and set rt_msghdr values */
715 memset (&msg, 0, sizeof (struct rt_msghdr));
716 msg.rtm.rtm_version = RTM_VERSION;
717 msg.rtm.rtm_type = message;
718 msg.rtm.rtm_seq = msg_seq++;
719 msg.rtm.rtm_addrs = RTA_DST;
720 msg.rtm.rtm_addrs |= RTA_GATEWAY;
721 msg.rtm.rtm_flags = RTF_UP;
722 msg.rtm.rtm_index = index;
723
724 if (metric != 0)
725 {
726 msg.rtm.rtm_rmx.rmx_hopcount = metric;
727 msg.rtm.rtm_inits |= RTV_HOPCOUNT;
728 }
729
730 ifp = if_lookup_by_index (index);
731
732 if (gate && message == RTM_ADD)
733 msg.rtm.rtm_flags |= RTF_GATEWAY;
734
735 if (! gate && message == RTM_ADD && ifp &&
736 (ifp->flags & IFF_POINTOPOINT) == 0)
737 msg.rtm.rtm_flags |= RTF_CLONING;
738
739 /* If no protocol specific gateway is specified, use link
740 address for gateway. */
741 if (! gate)
742 {
743 if (!ifp)
744 {
745 zlog_warn ("no gateway found for interface index %d", index);
746 return -1;
747 }
748 gate = (union sockunion *) & ifp->sdl;
749 }
750
751 if (mask)
752 msg.rtm.rtm_addrs |= RTA_NETMASK;
753 else if (message == RTM_ADD)
754 msg.rtm.rtm_flags |= RTF_HOST;
755
756 /* Tagging route with flags */
757 msg.rtm.rtm_flags |= (RTF_PROTO1);
758
759 /* Additional flags. */
760 if (zebra_flags & ZEBRA_FLAG_BLACKHOLE)
761 msg.rtm.rtm_flags |= RTF_BLACKHOLE;
hasso81dfcaa2003-05-25 19:21:25 +0000762 if (zebra_flags & ZEBRA_FLAG_REJECT)
763 msg.rtm.rtm_flags |= RTF_REJECT;
764
paul718e3742002-12-13 20:15:29 +0000765
766#ifdef HAVE_SIN_LEN
767#define SOCKADDRSET(X,R) \
768 if (msg.rtm.rtm_addrs & (R)) \
769 { \
770 int len = ROUNDUP ((X)->sa.sa_len); \
771 memcpy (pnt, (caddr_t)(X), len); \
772 pnt += len; \
773 }
774#else
775#define SOCKADDRSET(X,R) \
776 if (msg.rtm.rtm_addrs & (R)) \
777 { \
778 int len = ROUNDUP (sizeof((X)->sa)); \
779 memcpy (pnt, (caddr_t)(X), len); \
780 pnt += len; \
781 }
782#endif /* HAVE_SIN_LEN */
783
784 pnt = (caddr_t) msg.buf;
785
786 /* Write each socket data into rtm message buffer */
787 SOCKADDRSET (dest, RTA_DST);
788 SOCKADDRSET (gate, RTA_GATEWAY);
789 SOCKADDRSET (mask, RTA_NETMASK);
790
791 msg.rtm.rtm_msglen = pnt - (caddr_t) &msg;
792
793 ret = write (routing_sock, &msg, msg.rtm.rtm_msglen);
794
795 if (ret != msg.rtm.rtm_msglen)
796 {
797 if (errno == EEXIST)
798 return ZEBRA_ERR_RTEXIST;
799 if (errno == ENETUNREACH)
800 return ZEBRA_ERR_RTUNREACH;
801
ajs6099b3b2004-11-20 02:06:59 +0000802 zlog_warn ("write : %s (%d)", safe_strerror (errno), errno);
paul718e3742002-12-13 20:15:29 +0000803 return -1;
804 }
805 return 0;
806}
807
808
809#include "thread.h"
810#include "zebra/zserv.h"
811
paul718e3742002-12-13 20:15:29 +0000812/* For debug purpose. */
ajsb6178002004-12-07 21:12:56 +0000813static void
paul718e3742002-12-13 20:15:29 +0000814rtmsg_debug (struct rt_msghdr *rtm)
815{
gdt6a250b02004-12-09 14:48:12 +0000816 const char *type = "Unknown";
paul718e3742002-12-13 20:15:29 +0000817 struct message *mes;
818
819 for (mes = rtm_type_str; mes->str; mes++)
820 if (mes->key == rtm->rtm_type)
821 {
822 type = mes->str;
823 break;
824 }
825
ajsb6178002004-12-07 21:12:56 +0000826 zlog_debug ("Kernel: Len: %d Type: %s", rtm->rtm_msglen, type);
paul718e3742002-12-13 20:15:29 +0000827 rtm_flag_dump (rtm->rtm_flags);
ajsb6178002004-12-07 21:12:56 +0000828 zlog_debug ("Kernel: message seq %d", rtm->rtm_seq);
829 zlog_debug ("Kernel: pid %d", rtm->rtm_pid);
paul718e3742002-12-13 20:15:29 +0000830}
831
832/* This is pretty gross, better suggestions welcome -- mhandler */
833#ifndef RTAX_MAX
834#ifdef RTA_NUMBITS
835#define RTAX_MAX RTA_NUMBITS
836#else
837#define RTAX_MAX 8
838#endif /* RTA_NUMBITS */
839#endif /* RTAX_MAX */
840
841/* Kernel routing table and interface updates via routing socket. */
842int
843kernel_read (struct thread *thread)
844{
845 int sock;
846 int nbytes;
847 struct rt_msghdr *rtm;
848
gdtdbee01f2004-01-06 00:36:51 +0000849 /*
850 * This must be big enough for any message the kernel might send.
gdtb27900b2004-01-08 15:44:29 +0000851 * Rather than determining how many sockaddrs of what size might be
852 * in each particular message, just use RTAX_MAX of sockaddr_storage
853 * for each. Note that the sockaddrs must be after each message
854 * definition, or rather after whichever happens to be the largest,
855 * since the buffer needs to be big enough for a message and the
856 * sockaddrs together.
gdtdbee01f2004-01-06 00:36:51 +0000857 */
paul718e3742002-12-13 20:15:29 +0000858 union
859 {
860 /* Routing information. */
861 struct
862 {
863 struct rt_msghdr rtm;
gdtb27900b2004-01-08 15:44:29 +0000864 struct sockaddr_storage addr[RTAX_MAX];
paul718e3742002-12-13 20:15:29 +0000865 } r;
866
867 /* Interface information. */
868 struct
869 {
870 struct if_msghdr ifm;
gdtb27900b2004-01-08 15:44:29 +0000871 struct sockaddr_storage addr[RTAX_MAX];
paul718e3742002-12-13 20:15:29 +0000872 } im;
873
874 /* Interface address information. */
875 struct
876 {
877 struct ifa_msghdr ifa;
gdtb27900b2004-01-08 15:44:29 +0000878 struct sockaddr_storage addr[RTAX_MAX];
paul718e3742002-12-13 20:15:29 +0000879 } ia;
880
881#ifdef RTM_IFANNOUNCE
882 /* Interface arrival/departure */
883 struct
884 {
885 struct if_announcemsghdr ifan;
gdtb27900b2004-01-08 15:44:29 +0000886 struct sockaddr_storage addr[RTAX_MAX];
paul718e3742002-12-13 20:15:29 +0000887 } ian;
888#endif /* RTM_IFANNOUNCE */
889
890 } buf;
891
892 /* Fetch routing socket. */
893 sock = THREAD_FD (thread);
894
895 nbytes= read (sock, &buf, sizeof buf);
896
897 if (nbytes <= 0)
898 {
899 if (nbytes < 0 && errno != EWOULDBLOCK && errno != EAGAIN)
ajs6099b3b2004-11-20 02:06:59 +0000900 zlog_warn ("routing socket error: %s", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +0000901 return 0;
902 }
903
paul9bcdb632003-07-08 08:09:45 +0000904 thread_add_read (zebrad.master, kernel_read, NULL, sock);
paul718e3742002-12-13 20:15:29 +0000905
hasso726f9b22003-05-25 21:04:54 +0000906 if (IS_ZEBRA_DEBUG_KERNEL)
907 rtmsg_debug (&buf.r.rtm);
paul718e3742002-12-13 20:15:29 +0000908
909 rtm = &buf.r.rtm;
910
gdtb27900b2004-01-08 15:44:29 +0000911 /*
912 * Ensure that we didn't drop any data, so that processing routines
913 * can assume they have the whole message.
914 */
gdtda26e3b2004-01-05 17:20:59 +0000915 if (rtm->rtm_msglen != nbytes)
916 {
917 zlog_warn ("kernel_read: rtm->rtm_msglen %d, nbytes %d, type %d\n",
918 rtm->rtm_msglen, nbytes, rtm->rtm_type);
919 return -1;
920 }
921
paul718e3742002-12-13 20:15:29 +0000922 switch (rtm->rtm_type)
923 {
924 case RTM_ADD:
925 case RTM_DELETE:
926 rtm_read (rtm);
927 break;
928 case RTM_IFINFO:
929 ifm_read (&buf.im.ifm);
930 break;
931 case RTM_NEWADDR:
932 case RTM_DELADDR:
933 ifam_read (&buf.ia.ifa);
934 break;
935#ifdef RTM_IFANNOUNCE
936 case RTM_IFANNOUNCE:
937 ifan_read (&buf.ian.ifan);
938 break;
939#endif /* RTM_IFANNOUNCE */
940 default:
hasso726f9b22003-05-25 21:04:54 +0000941 if (IS_ZEBRA_DEBUG_KERNEL)
ajsb6178002004-12-07 21:12:56 +0000942 zlog_debug("Unprocessed RTM_type: %d", rtm->rtm_type);
paul718e3742002-12-13 20:15:29 +0000943 break;
944 }
945 return 0;
946}
947
948/* Make routing socket. */
949void
950routing_socket ()
951{
pauledd7c242003-06-04 13:59:38 +0000952 if ( zserv_privs.change (ZPRIVS_RAISE) )
953 zlog_err ("routing_socket: Can't raise privileges");
954
paul718e3742002-12-13 20:15:29 +0000955 routing_sock = socket (AF_ROUTE, SOCK_RAW, 0);
956
957 if (routing_sock < 0)
958 {
pauledd7c242003-06-04 13:59:38 +0000959 if ( zserv_privs.change (ZPRIVS_LOWER) )
960 zlog_err ("routing_socket: Can't lower privileges");
paul718e3742002-12-13 20:15:29 +0000961 zlog_warn ("Can't init kernel routing socket");
962 return;
963 }
964
paul865b8522005-01-05 08:30:35 +0000965 /* XXX: Socket should be NONBLOCK, however as we currently
966 * discard failed writes, this will lead to inconsistencies.
967 * For now, socket must be blocking.
968 */
969 /*if (fcntl (routing_sock, F_SETFL, O_NONBLOCK) < 0)
970 zlog_warn ("Can't set O_NONBLOCK to routing socket");*/
971
pauledd7c242003-06-04 13:59:38 +0000972 if ( zserv_privs.change (ZPRIVS_LOWER) )
973 zlog_err ("routing_socket: Can't lower privileges");
paul718e3742002-12-13 20:15:29 +0000974
975 /* kernel_read needs rewrite. */
paul9bcdb632003-07-08 08:09:45 +0000976 thread_add_read (zebrad.master, kernel_read, NULL, routing_sock);
paul718e3742002-12-13 20:15:29 +0000977}
978
979/* Exported interface function. This function simply calls
980 routing_socket (). */
981void
982kernel_init ()
983{
984 routing_socket ();
985}