blob: e4f85aa069a9070821679d526eafd8509b002a02 [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
paul718e3742002-12-13 20:15:29 +000043/* Socket length roundup function. */
44#define ROUNDUP(a) \
45 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
46
47/* And this macro is wrapper for handling sa_len. */
48#ifdef HAVE_SA_LEN
49#define WRAPUP(X) ROUNDUP(((struct sockaddr *)(X))->sa_len)
50#else
paul3e95a072003-09-24 00:05:45 +000051#define WRAPUP(X) \
52 (((struct sockaddr *)(X))->sa_family == AF_INET ? \
53 ROUNDUP(sizeof(struct sockaddr_in)):\
54 (((struct sockaddr *)(X))->sa_family == AF_INET6 ? \
55 ROUNDUP(sizeof(struct sockaddr_in6)) : \
56 (((struct sockaddr *)(X))->sa_family == AF_LINK ? \
57 ROUNDUP(sizeof(struct sockaddr_dl)) : sizeof(struct sockaddr))))
paul718e3742002-12-13 20:15:29 +000058#endif /* HAVE_SA_LEN */
59
60/* Routing socket message types. */
61struct message rtm_type_str[] =
62{
63 {RTM_ADD, "RTM_ADD"},
64 {RTM_DELETE, "RTM_DELETE"},
65 {RTM_CHANGE, "RTM_CHANGE"},
66 {RTM_GET, "RTM_GET"},
67 {RTM_LOSING, "RTM_LOSING"},
68 {RTM_REDIRECT, "RTM_REDIRECT"},
69 {RTM_MISS, "RTM_MISS"},
70 {RTM_LOCK, "RTM_LOCK"},
71 {RTM_OLDADD, "RTM_OLDADD"},
72 {RTM_OLDDEL, "RTM_OLDDEL"},
73 {RTM_RESOLVE, "RTM_RESOLVE"},
74 {RTM_NEWADDR, "RTM_NEWADDR"},
75 {RTM_DELADDR, "RTM_DELADDR"},
76 {RTM_IFINFO, "RTM_IFINFO"},
77#ifdef RTM_OIFINFO
78 {RTM_OIFINFO, "RTM_OIFINFO"},
79#endif /* RTM_OIFINFO */
80#ifdef RTM_NEWMADDR
81 {RTM_NEWMADDR, "RTM_NEWMADDR"},
82#endif /* RTM_NEWMADDR */
83#ifdef RTM_DELMADDR
84 {RTM_DELMADDR, "RTM_DELMADDR"},
85#endif /* RTM_DELMADDR */
86#ifdef RTM_IFANNOUNCE
87 {RTM_IFANNOUNCE, "RTM_IFANNOUNCE"},
88#endif /* RTM_IFANNOUNCE */
89 {0, NULL}
90};
91
92struct message rtm_flag_str[] =
93{
94 {RTF_UP, "UP"},
95 {RTF_GATEWAY, "GATEWAY"},
96 {RTF_HOST, "HOST"},
97 {RTF_REJECT, "REJECT"},
98 {RTF_DYNAMIC, "DYNAMIC"},
99 {RTF_MODIFIED, "MODIFIED"},
100 {RTF_DONE, "DONE"},
101#ifdef RTF_MASK
102 {RTF_MASK, "MASK"},
103#endif /* RTF_MASK */
104 {RTF_CLONING, "CLONING"},
105 {RTF_XRESOLVE, "XRESOLVE"},
106 {RTF_LLINFO, "LLINFO"},
107 {RTF_STATIC, "STATIC"},
108 {RTF_BLACKHOLE, "BLACKHOLE"},
109 {RTF_PROTO1, "PROTO1"},
110 {RTF_PROTO2, "PROTO2"},
111#ifdef RTF_PRCLONING
112 {RTF_PRCLONING, "PRCLONING"},
113#endif /* RTF_PRCLONING */
114#ifdef RTF_WASCLONED
115 {RTF_WASCLONED, "WASCLONED"},
116#endif /* RTF_WASCLONED */
117#ifdef RTF_PROTO3
118 {RTF_PROTO3, "PROTO3"},
119#endif /* RTF_PROTO3 */
120#ifdef RTF_PINNED
121 {RTF_PINNED, "PINNED"},
122#endif /* RTF_PINNED */
123#ifdef RTF_LOCAL
124 {RTF_LOCAL, "LOCAL"},
125#endif /* RTF_LOCAL */
126#ifdef RTF_BROADCAST
127 {RTF_BROADCAST, "BROADCAST"},
128#endif /* RTF_BROADCAST */
129#ifdef RTF_MULTICAST
130 {RTF_MULTICAST, "MULTICAST"},
131#endif /* RTF_MULTICAST */
132 {0, NULL}
133};
134
135/* Kernel routing update socket. */
136int routing_sock = -1;
137
138/* Yes I'm checking ugly routing socket behavior. */
139/* #define DEBUG */
140
141/* Supported address family check. */
142static int
143af_check (int family)
144{
145 if (family == AF_INET)
146 return 1;
147#ifdef HAVE_IPV6
148 if (family == AF_INET6)
149 return 1;
150#endif /* HAVE_IPV6 */
151 return 0;
152}
153
154/* Dump routing table flag for debug purpose. */
155void
156rtm_flag_dump (int flag)
157{
158 struct message *mes;
159 static char buf[BUFSIZ];
160
hasso81dfcaa2003-05-25 19:21:25 +0000161 buf[0] = '0';
paul718e3742002-12-13 20:15:29 +0000162 for (mes = rtm_flag_str; mes->key != 0; mes++)
163 {
164 if (mes->key & flag)
165 {
166 strlcat (buf, mes->str, BUFSIZ);
167 strlcat (buf, " ", BUFSIZ);
168 }
169 }
170 zlog_info ("Kernel: %s", buf);
171}
172
173#ifdef RTM_IFANNOUNCE
174/* Interface adding function */
175int
176ifan_read (struct if_announcemsghdr *ifan)
177{
178 struct interface *ifp;
179
180 ifp = if_lookup_by_index (ifan->ifan_index);
181 if (ifp == NULL && ifan->ifan_what == IFAN_ARRIVAL)
182 {
183 /* Create Interface */
184 ifp = if_get_by_name (ifan->ifan_name);
185 ifp->ifindex = ifan->ifan_index;
186
187 if_add_update (ifp);
188 }
189 else if (ifp != NULL && ifan->ifan_what == IFAN_DEPARTURE)
190 {
191 if_delete_update (ifp);
192 if_delete (ifp);
193 }
194
195 if_get_flags (ifp);
196 if_get_mtu (ifp);
197 if_get_metric (ifp);
198
199 if (IS_ZEBRA_DEBUG_KERNEL)
200 zlog_info ("interface %s index %d", ifp->name, ifp->ifindex);
201
202 return 0;
203}
204#endif /* RTM_IFANNOUNCE */
205
206/* Interface adding function called from interface_list. */
207int
208ifm_read (struct if_msghdr *ifm)
209{
paul3e95a072003-09-24 00:05:45 +0000210 struct interface *ifp = NULL;
paul718e3742002-12-13 20:15:29 +0000211 struct sockaddr_dl *sdl = NULL;
paul3e95a072003-09-24 00:05:45 +0000212 char ifname[IFNAMSIZ];
paul718e3742002-12-13 20:15:29 +0000213
paul3e95a072003-09-24 00:05:45 +0000214#ifdef SUNOS_5
215 int i;
216 struct sockaddr *sa;
217 u_char *cp = (u_char *)(ifm + 1);
218
219 /*
220 * if_msghdr_t on 64 bit kernels in Solaris 9 and earlier versions
221 * is 12 bytes larger than the 32 bit version, so make adjustment
222 * here.
223 */
224 sa = (struct sockaddr *)cp;
225 if (sa->sa_family == AF_UNSPEC)
226 cp = cp + 12;
227
228 for (i = 1; i != 0; i <<= 1)
229 {
230 if (i & ifm->ifm_addrs)
231 {
232 sa = (struct sockaddr *)cp;
233 cp += WRAPUP(sa);
234 if (i & RTA_IFP)
235 {
236 sdl = (struct sockaddr_dl *)sa;
237 break;
238 }
239 }
240 }
241#else
paul718e3742002-12-13 20:15:29 +0000242 sdl = (struct sockaddr_dl *)(ifm + 1);
paul3e95a072003-09-24 00:05:45 +0000243#endif
paul718e3742002-12-13 20:15:29 +0000244
paul3e95a072003-09-24 00:05:45 +0000245 /*
246 * Check if ifp already exists. If the interface has already been specified
247 * in the conf file, but is just getting created, we would have an
248 * entry in the iflist with incomplete data (e.g., ifindex == -1),
249 * so we lookup on name.
250 */
251 if (sdl != NULL)
252 {
253 bcopy(sdl->sdl_data, ifname, sdl->sdl_nlen);
254 ifname[sdl->sdl_nlen] = '\0';
255 ifp = if_lookup_by_name (ifname);
256 }
paul718e3742002-12-13 20:15:29 +0000257
paul3e95a072003-09-24 00:05:45 +0000258 if ((ifp == NULL) || (ifp->ifindex == -1))
paul718e3742002-12-13 20:15:29 +0000259 {
260 /* Check interface's address.*/
261 if (! (ifm->ifm_addrs & RTA_IFP))
262 {
263 zlog_warn ("There must be RTA_IFP address for ifindex %d\n",
264 ifm->ifm_index);
265 return -1;
266 }
267
paul3e95a072003-09-24 00:05:45 +0000268 if (ifp == NULL)
269 ifp = if_create (sdl->sdl_data, sdl->sdl_nlen);
paul718e3742002-12-13 20:15:29 +0000270
paul718e3742002-12-13 20:15:29 +0000271 ifp->ifindex = ifm->ifm_index;
272 ifp->flags = ifm->ifm_flags;
273#if defined(__bsdi__)
274 if_kvm_get_mtu (ifp);
275#else
276 if_get_mtu (ifp);
277#endif /* __bsdi__ */
278 if_get_metric (ifp);
279
280 /* Fetch hardware address. */
281 if (sdl->sdl_family != AF_LINK)
282 {
283 zlog_warn ("sockaddr_dl->sdl_family is not AF_LINK");
284 return -1;
285 }
286 memcpy (&ifp->sdl, sdl, sizeof (struct sockaddr_dl));
287
288 if_add_update (ifp);
289 }
290 else
291 {
292 /* There is a case of promisc, allmulti flag modification. */
293 if (if_is_up (ifp))
294 {
295 ifp->flags = ifm->ifm_flags;
296 if (! if_is_up (ifp))
297 if_down (ifp);
298 }
299 else
300 {
301 ifp->flags = ifm->ifm_flags;
302 if (if_is_up (ifp))
303 if_up (ifp);
304 }
305 }
306
307#ifdef HAVE_NET_RT_IFLIST
308 ifp->stats = ifm->ifm_data;
309#endif /* HAVE_NET_RT_IFLIST */
310
311 if (IS_ZEBRA_DEBUG_KERNEL)
312 zlog_info ("interface %s index %d", ifp->name, ifp->ifindex);
313
314 return 0;
315}
316
317/* Address read from struct ifa_msghdr. */
318void
319ifam_read_mesg (struct ifa_msghdr *ifm,
320 union sockunion *addr,
321 union sockunion *mask,
322 union sockunion *dest)
323{
324 caddr_t pnt, end;
325
326 pnt = (caddr_t)(ifm + 1);
327 end = ((caddr_t)ifm) + ifm->ifam_msglen;
328
329#define IFAMADDRGET(X,R) \
330 if (ifm->ifam_addrs & (R)) \
331 { \
332 int len = WRAPUP(pnt); \
333 if (((X) != NULL) && af_check (((struct sockaddr *)pnt)->sa_family)) \
334 memcpy ((caddr_t)(X), pnt, len); \
335 pnt += len; \
336 }
337#define IFAMMASKGET(X,R) \
338 if (ifm->ifam_addrs & (R)) \
339 { \
340 int len = WRAPUP(pnt); \
341 if ((X) != NULL) \
342 memcpy ((caddr_t)(X), pnt, len); \
343 pnt += len; \
344 }
345
346 /* Be sure structure is cleared */
347 memset (mask, 0, sizeof (union sockunion));
348 memset (addr, 0, sizeof (union sockunion));
349 memset (dest, 0, sizeof (union sockunion));
350
351 /* We fetch each socket variable into sockunion. */
352 IFAMADDRGET (NULL, RTA_DST);
353 IFAMADDRGET (NULL, RTA_GATEWAY);
354 IFAMMASKGET (mask, RTA_NETMASK);
355 IFAMADDRGET (NULL, RTA_GENMASK);
356 IFAMADDRGET (NULL, RTA_IFP);
357 IFAMADDRGET (addr, RTA_IFA);
358 IFAMADDRGET (NULL, RTA_AUTHOR);
359 IFAMADDRGET (dest, RTA_BRD);
360
361 /* Assert read up end point matches to end point */
362 if (pnt != end)
363 zlog_warn ("ifam_read() does't read all socket data");
364}
365
366/* Interface's address information get. */
367int
368ifam_read (struct ifa_msghdr *ifam)
369{
370 struct interface *ifp;
371 union sockunion addr, mask, gate;
372
373 /* Check does this interface exist or not. */
374 ifp = if_lookup_by_index (ifam->ifam_index);
375 if (ifp == NULL)
376 {
377 zlog_warn ("no interface for index %d", ifam->ifam_index);
378 return -1;
379 }
380
381 /* Allocate and read address information. */
382 ifam_read_mesg (ifam, &addr, &mask, &gate);
383
384 /* Check interface flag for implicit up of the interface. */
385 if_refresh (ifp);
386
387 /* Add connected address. */
388 switch (sockunion_family (&addr))
389 {
390 case AF_INET:
391 if (ifam->ifam_type == RTM_NEWADDR)
392 connected_add_ipv4 (ifp, 0, &addr.sin.sin_addr,
393 ip_masklen (mask.sin.sin_addr),
394 &gate.sin.sin_addr, NULL);
395 else
396 connected_delete_ipv4 (ifp, 0, &addr.sin.sin_addr,
397 ip_masklen (mask.sin.sin_addr),
398 &gate.sin.sin_addr, NULL);
399 break;
400#ifdef HAVE_IPV6
401 case AF_INET6:
402 /* Unset interface index from link-local address when IPv6 stack
403 is KAME. */
404 if (IN6_IS_ADDR_LINKLOCAL (&addr.sin6.sin6_addr))
405 SET_IN6_LINKLOCAL_IFINDEX (addr.sin6.sin6_addr, 0);
406
407 if (ifam->ifam_type == RTM_NEWADDR)
408 connected_add_ipv6 (ifp,
409 &addr.sin6.sin6_addr,
410 ip6_masklen (mask.sin6.sin6_addr),
411 &gate.sin6.sin6_addr);
412 else
413 connected_delete_ipv6 (ifp,
414 &addr.sin6.sin6_addr,
415 ip6_masklen (mask.sin6.sin6_addr),
416 &gate.sin6.sin6_addr);
417 break;
418#endif /* HAVE_IPV6 */
419 default:
420 /* Unsupported family silently ignore... */
421 break;
422 }
423 return 0;
424}
425
426/* Interface function for reading kernel routing table information. */
427int
428rtm_read_mesg (struct rt_msghdr *rtm,
429 union sockunion *dest,
430 union sockunion *mask,
431 union sockunion *gate)
432{
433 caddr_t pnt, end;
434
435 /* Pnt points out socket data start point. */
436 pnt = (caddr_t)(rtm + 1);
437 end = ((caddr_t)rtm) + rtm->rtm_msglen;
438
439 /* rt_msghdr version check. */
440 if (rtm->rtm_version != RTM_VERSION)
441 zlog (NULL, LOG_WARNING,
442 "Routing message version different %d should be %d."
443 "This may cause problem\n", rtm->rtm_version, RTM_VERSION);
444
445#define RTMADDRGET(X,R) \
446 if (rtm->rtm_addrs & (R)) \
447 { \
448 int len = WRAPUP (pnt); \
449 if (((X) != NULL) && af_check (((struct sockaddr *)pnt)->sa_family)) \
450 memcpy ((caddr_t)(X), pnt, len); \
451 pnt += len; \
452 }
453#define RTMMASKGET(X,R) \
454 if (rtm->rtm_addrs & (R)) \
455 { \
456 int len = WRAPUP (pnt); \
457 if ((X) != NULL) \
458 memcpy ((caddr_t)(X), pnt, len); \
459 pnt += len; \
460 }
461
462 /* Be sure structure is cleared */
463 memset (dest, 0, sizeof (union sockunion));
464 memset (gate, 0, sizeof (union sockunion));
465 memset (mask, 0, sizeof (union sockunion));
466
467 /* We fetch each socket variable into sockunion. */
468 RTMADDRGET (dest, RTA_DST);
469 RTMADDRGET (gate, RTA_GATEWAY);
470 RTMMASKGET (mask, RTA_NETMASK);
471 RTMADDRGET (NULL, RTA_GENMASK);
472 RTMADDRGET (NULL, RTA_IFP);
473 RTMADDRGET (NULL, RTA_IFA);
474 RTMADDRGET (NULL, RTA_AUTHOR);
475 RTMADDRGET (NULL, RTA_BRD);
476
477 /* If there is netmask information set it's family same as
478 destination family*/
479 if (rtm->rtm_addrs & RTA_NETMASK)
480 mask->sa.sa_family = dest->sa.sa_family;
481
482 /* Assert read up to the end of pointer. */
483 if (pnt != end)
484 zlog (NULL, LOG_WARNING, "rtm_read() does't read all socket data.");
485
486 return rtm->rtm_flags;
487}
488
489void
490rtm_read (struct rt_msghdr *rtm)
491{
492 int flags;
493 u_char zebra_flags;
494 union sockunion dest, mask, gate;
495
496 zebra_flags = 0;
497
498 /* Discard self send message. */
499 if (rtm->rtm_type != RTM_GET
500 && (rtm->rtm_pid == pid || rtm->rtm_pid == old_pid))
501 return;
502
503 /* Read destination and netmask and gateway from rtm message
504 structure. */
505 flags = rtm_read_mesg (rtm, &dest, &mask, &gate);
506
507#ifdef RTF_CLONED /*bsdi, netbsd 1.6*/
508 if (flags & RTF_CLONED)
509 return;
510#endif
511#ifdef RTF_WASCLONED /*freebsd*/
512 if (flags & RTF_WASCLONED)
513 return;
514#endif
515
516 if ((rtm->rtm_type == RTM_ADD) && ! (flags & RTF_UP))
517 return;
518
519 /* This is connected route. */
520 if (! (flags & RTF_GATEWAY))
521 return;
522
523 if (flags & RTF_PROTO1)
524 SET_FLAG (zebra_flags, ZEBRA_FLAG_SELFROUTE);
525
526 /* This is persistent route. */
527 if (flags & RTF_STATIC)
528 SET_FLAG (zebra_flags, ZEBRA_FLAG_STATIC);
529
hasso81dfcaa2003-05-25 19:21:25 +0000530 /* This is a reject or blackhole route */
531 if (flags & RTF_REJECT)
532 SET_FLAG (zebra_flags, ZEBRA_FLAG_REJECT);
533 if (flags & RTF_BLACKHOLE)
534 SET_FLAG (zebra_flags, ZEBRA_FLAG_BLACKHOLE);
535
paul718e3742002-12-13 20:15:29 +0000536 if (dest.sa.sa_family == AF_INET)
537 {
538 struct prefix_ipv4 p;
539
540 p.family = AF_INET;
541 p.prefix = dest.sin.sin_addr;
542 if (flags & RTF_HOST)
543 p.prefixlen = IPV4_MAX_PREFIXLEN;
544 else
545 p.prefixlen = ip_masklen (mask.sin.sin_addr);
546
547 if (rtm->rtm_type == RTM_GET || rtm->rtm_type == RTM_ADD)
548 rib_add_ipv4 (ZEBRA_ROUTE_KERNEL, zebra_flags,
549 &p, &gate.sin.sin_addr, 0, 0, 0, 0);
550 else
551 rib_delete_ipv4 (ZEBRA_ROUTE_KERNEL, zebra_flags,
552 &p, &gate.sin.sin_addr, 0, 0);
553 }
554#ifdef HAVE_IPV6
555 if (dest.sa.sa_family == AF_INET6)
556 {
557 struct prefix_ipv6 p;
558 unsigned int ifindex = 0;
559
560 p.family = AF_INET6;
561 p.prefix = dest.sin6.sin6_addr;
562 if (flags & RTF_HOST)
563 p.prefixlen = IPV6_MAX_PREFIXLEN;
564 else
565 p.prefixlen = ip6_masklen (mask.sin6.sin6_addr);
566
567#ifdef KAME
568 if (IN6_IS_ADDR_LINKLOCAL (&gate.sin6.sin6_addr))
569 {
570 ifindex = IN6_LINKLOCAL_IFINDEX (gate.sin6.sin6_addr);
571 SET_IN6_LINKLOCAL_IFINDEX (gate.sin6.sin6_addr, 0);
572 }
573#endif /* KAME */
574
575 if (rtm->rtm_type == RTM_GET || rtm->rtm_type == RTM_ADD)
576 rib_add_ipv6 (ZEBRA_ROUTE_KERNEL, zebra_flags,
577 &p, &gate.sin6.sin6_addr, ifindex, 0);
578 else
579 rib_delete_ipv6 (ZEBRA_ROUTE_KERNEL, zebra_flags,
580 &p, &gate.sin6.sin6_addr, ifindex, 0);
581 }
582#endif /* HAVE_IPV6 */
583}
584
585/* Interface function for the kernel routing table updates. Support
586 for RTM_CHANGE will be needed. */
587int
588rtm_write (int message,
589 union sockunion *dest,
590 union sockunion *mask,
591 union sockunion *gate,
592 unsigned int index,
593 int zebra_flags,
594 int metric)
595{
596 int ret;
597 caddr_t pnt;
598 struct interface *ifp;
599 struct sockaddr_in tmp_gate;
600#ifdef HAVE_IPV6
601 struct sockaddr_in6 tmp_gate6;
602#endif /* HAVE_IPV6 */
603
604 /* Sequencial number of routing message. */
605 static int msg_seq = 0;
606
607 /* Struct of rt_msghdr and buffer for storing socket's data. */
608 struct
609 {
610 struct rt_msghdr rtm;
611 char buf[512];
612 } msg;
613
614 memset (&tmp_gate, 0, sizeof (struct sockaddr_in));
615 tmp_gate.sin_family = AF_INET;
616#ifdef HAVE_SIN_LEN
617 tmp_gate.sin_len = sizeof (struct sockaddr_in);
618#endif /* HAVE_SIN_LEN */
619
620#ifdef HAVE_IPV6
621 memset (&tmp_gate6, 0, sizeof (struct sockaddr_in6));
622 tmp_gate6.sin6_family = AF_INET6;
623#ifdef SIN6_LEN
624 tmp_gate6.sin6_len = sizeof (struct sockaddr_in6);
625#endif /* SIN6_LEN */
626#endif /* HAVE_IPV6 */
627
628 if (routing_sock < 0)
629 return ZEBRA_ERR_EPERM;
630
631 /* Clear and set rt_msghdr values */
632 memset (&msg, 0, sizeof (struct rt_msghdr));
633 msg.rtm.rtm_version = RTM_VERSION;
634 msg.rtm.rtm_type = message;
635 msg.rtm.rtm_seq = msg_seq++;
636 msg.rtm.rtm_addrs = RTA_DST;
637 msg.rtm.rtm_addrs |= RTA_GATEWAY;
638 msg.rtm.rtm_flags = RTF_UP;
639 msg.rtm.rtm_index = index;
640
641 if (metric != 0)
642 {
643 msg.rtm.rtm_rmx.rmx_hopcount = metric;
644 msg.rtm.rtm_inits |= RTV_HOPCOUNT;
645 }
646
647 ifp = if_lookup_by_index (index);
648
649 if (gate && message == RTM_ADD)
650 msg.rtm.rtm_flags |= RTF_GATEWAY;
651
652 if (! gate && message == RTM_ADD && ifp &&
653 (ifp->flags & IFF_POINTOPOINT) == 0)
654 msg.rtm.rtm_flags |= RTF_CLONING;
655
656 /* If no protocol specific gateway is specified, use link
657 address for gateway. */
658 if (! gate)
659 {
660 if (!ifp)
661 {
662 zlog_warn ("no gateway found for interface index %d", index);
663 return -1;
664 }
665 gate = (union sockunion *) & ifp->sdl;
666 }
667
668 if (mask)
669 msg.rtm.rtm_addrs |= RTA_NETMASK;
670 else if (message == RTM_ADD)
671 msg.rtm.rtm_flags |= RTF_HOST;
672
673 /* Tagging route with flags */
674 msg.rtm.rtm_flags |= (RTF_PROTO1);
675
676 /* Additional flags. */
677 if (zebra_flags & ZEBRA_FLAG_BLACKHOLE)
678 msg.rtm.rtm_flags |= RTF_BLACKHOLE;
hasso81dfcaa2003-05-25 19:21:25 +0000679 if (zebra_flags & ZEBRA_FLAG_REJECT)
680 msg.rtm.rtm_flags |= RTF_REJECT;
681
paul718e3742002-12-13 20:15:29 +0000682
683#ifdef HAVE_SIN_LEN
684#define SOCKADDRSET(X,R) \
685 if (msg.rtm.rtm_addrs & (R)) \
686 { \
687 int len = ROUNDUP ((X)->sa.sa_len); \
688 memcpy (pnt, (caddr_t)(X), len); \
689 pnt += len; \
690 }
691#else
692#define SOCKADDRSET(X,R) \
693 if (msg.rtm.rtm_addrs & (R)) \
694 { \
695 int len = ROUNDUP (sizeof((X)->sa)); \
696 memcpy (pnt, (caddr_t)(X), len); \
697 pnt += len; \
698 }
699#endif /* HAVE_SIN_LEN */
700
701 pnt = (caddr_t) msg.buf;
702
703 /* Write each socket data into rtm message buffer */
704 SOCKADDRSET (dest, RTA_DST);
705 SOCKADDRSET (gate, RTA_GATEWAY);
706 SOCKADDRSET (mask, RTA_NETMASK);
707
708 msg.rtm.rtm_msglen = pnt - (caddr_t) &msg;
709
710 ret = write (routing_sock, &msg, msg.rtm.rtm_msglen);
711
712 if (ret != msg.rtm.rtm_msglen)
713 {
714 if (errno == EEXIST)
715 return ZEBRA_ERR_RTEXIST;
716 if (errno == ENETUNREACH)
717 return ZEBRA_ERR_RTUNREACH;
718
719 zlog_warn ("write : %s (%d)", strerror (errno), errno);
720 return -1;
721 }
722 return 0;
723}
724
725
726#include "thread.h"
727#include "zebra/zserv.h"
728
paul718e3742002-12-13 20:15:29 +0000729/* For debug purpose. */
730void
731rtmsg_debug (struct rt_msghdr *rtm)
732{
733 char *type = "Unknown";
734 struct message *mes;
735
736 for (mes = rtm_type_str; mes->str; mes++)
737 if (mes->key == rtm->rtm_type)
738 {
739 type = mes->str;
740 break;
741 }
742
743 zlog_info ("Kernel: Len: %d Type: %s", rtm->rtm_msglen, type);
744 rtm_flag_dump (rtm->rtm_flags);
745 zlog_info ("Kernel: message seq %d", rtm->rtm_seq);
746 zlog_info ("Kernel: pid %d", rtm->rtm_pid);
747}
748
749/* This is pretty gross, better suggestions welcome -- mhandler */
750#ifndef RTAX_MAX
751#ifdef RTA_NUMBITS
752#define RTAX_MAX RTA_NUMBITS
753#else
754#define RTAX_MAX 8
755#endif /* RTA_NUMBITS */
756#endif /* RTAX_MAX */
757
758/* Kernel routing table and interface updates via routing socket. */
759int
760kernel_read (struct thread *thread)
761{
762 int sock;
763 int nbytes;
764 struct rt_msghdr *rtm;
765
766 union
767 {
768 /* Routing information. */
769 struct
770 {
771 struct rt_msghdr rtm;
772 struct sockaddr addr[RTAX_MAX];
773 } r;
774
775 /* Interface information. */
776 struct
777 {
778 struct if_msghdr ifm;
779 struct sockaddr addr[RTAX_MAX];
780 } im;
781
782 /* Interface address information. */
783 struct
784 {
785 struct ifa_msghdr ifa;
786 struct sockaddr addr[RTAX_MAX];
787 } ia;
788
789#ifdef RTM_IFANNOUNCE
790 /* Interface arrival/departure */
791 struct
792 {
793 struct if_announcemsghdr ifan;
794 struct sockaddr addr[RTAX_MAX];
795 } ian;
796#endif /* RTM_IFANNOUNCE */
797
798 } buf;
799
800 /* Fetch routing socket. */
801 sock = THREAD_FD (thread);
802
803 nbytes= read (sock, &buf, sizeof buf);
804
805 if (nbytes <= 0)
806 {
807 if (nbytes < 0 && errno != EWOULDBLOCK && errno != EAGAIN)
808 zlog_warn ("routing socket error: %s", strerror (errno));
809 return 0;
810 }
811
paul9bcdb632003-07-08 08:09:45 +0000812 thread_add_read (zebrad.master, kernel_read, NULL, sock);
paul718e3742002-12-13 20:15:29 +0000813
hasso726f9b22003-05-25 21:04:54 +0000814 if (IS_ZEBRA_DEBUG_KERNEL)
815 rtmsg_debug (&buf.r.rtm);
paul718e3742002-12-13 20:15:29 +0000816
817 rtm = &buf.r.rtm;
818
819 switch (rtm->rtm_type)
820 {
821 case RTM_ADD:
822 case RTM_DELETE:
823 rtm_read (rtm);
824 break;
825 case RTM_IFINFO:
826 ifm_read (&buf.im.ifm);
827 break;
828 case RTM_NEWADDR:
829 case RTM_DELADDR:
830 ifam_read (&buf.ia.ifa);
831 break;
832#ifdef RTM_IFANNOUNCE
833 case RTM_IFANNOUNCE:
834 ifan_read (&buf.ian.ifan);
835 break;
836#endif /* RTM_IFANNOUNCE */
837 default:
hasso726f9b22003-05-25 21:04:54 +0000838 if (IS_ZEBRA_DEBUG_KERNEL)
839 zlog_info("Unprocessed RTM_type: %d", rtm->rtm_type);
paul718e3742002-12-13 20:15:29 +0000840 break;
841 }
842 return 0;
843}
844
845/* Make routing socket. */
846void
847routing_socket ()
848{
pauledd7c242003-06-04 13:59:38 +0000849 if ( zserv_privs.change (ZPRIVS_RAISE) )
850 zlog_err ("routing_socket: Can't raise privileges");
851
paul718e3742002-12-13 20:15:29 +0000852 routing_sock = socket (AF_ROUTE, SOCK_RAW, 0);
853
854 if (routing_sock < 0)
855 {
pauledd7c242003-06-04 13:59:38 +0000856 if ( zserv_privs.change (ZPRIVS_LOWER) )
857 zlog_err ("routing_socket: Can't lower privileges");
paul718e3742002-12-13 20:15:29 +0000858 zlog_warn ("Can't init kernel routing socket");
859 return;
860 }
861
862 if (fcntl (routing_sock, F_SETFL, O_NONBLOCK) < 0)
863 zlog_warn ("Can't set O_NONBLOCK to routing socket");
pauledd7c242003-06-04 13:59:38 +0000864 if ( zserv_privs.change (ZPRIVS_LOWER) )
865 zlog_err ("routing_socket: Can't lower privileges");
paul718e3742002-12-13 20:15:29 +0000866
867 /* kernel_read needs rewrite. */
paul9bcdb632003-07-08 08:09:45 +0000868 thread_add_read (zebrad.master, kernel_read, NULL, routing_sock);
paul718e3742002-12-13 20:15:29 +0000869}
870
871/* Exported interface function. This function simply calls
872 routing_socket (). */
873void
874kernel_init ()
875{
876 routing_socket ();
877}