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