1 /* Zebra daemon server routine.
2 * Copyright (C) 1997, 98, 99 Kunihiro Ishiguro
4 * This file is part of GNU Zebra.
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
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.
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
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
33 #include "sockunion.h"
37 #include "zebra/zserv.h"
38 #include "zebra/redistribute.h"
39 #include "zebra/debug.h"
40 #include "zebra/ipforward.h"
42 /* Event list of zebra. */
43 enum event
{ ZEBRA_SERV
, ZEBRA_READ
, ZEBRA_WRITE
};
45 /* Zebra client list. */
48 /* Default rtm_table for all clients */
49 int rtm_table_default
= 0;
51 void zebra_event (enum event event
, int sock
, struct zserv
*client
);
53 /* For logging of zebra meesages. */
54 char *zebra_command_str
[] =
57 "ZEBRA_INTERFACE_ADD",
58 "ZEBRA_INTERFACE_DELETE",
59 "ZEBRA_INTERFACE_ADDRESS_ADD",
60 "ZEBRA_INTERFACE_ADDRESS_DELETE",
62 "ZEBRA_INTERFACE_DOWN",
63 "ZEBRA_IPV4_ROUTE_ADD",
64 "ZEBRA_IPV4_ROUTE_DELETE",
65 "ZEBRA_IPV6_ROUTE_ADD",
66 "ZEBRA_IPV6_ROUTE_DELETE",
67 "ZEBRA_REDISTRIBUTE_ADD",
68 "ZEBRA_REDISTRIBUTE_DELETE",
69 "ZEBRA_REDISTRIBUTE_DEFAULT_ADD",
70 "ZEBRA_REDISTRIBUTE_DEFAULT_DELETE",
71 "ZEBRA_IPV4_NEXTHOP_LOOKUP",
72 "ZEBRA_IPV6_NEXTHOP_LOOKUP",
73 "ZEBRA_IPV4_IMPORT_LOOKUP",
74 "ZEBRA_IPV6_IMPORT_LOOKUP"
77 /* Interface is added. Send ZEBRA_INTERFACE_ADD to client. */
79 zsend_interface_add (struct zserv
*client
, struct interface
*ifp
)
83 /* Check this client need interface information. */
90 /* Place holder for size. */
94 stream_putc (s
, ZEBRA_INTERFACE_ADD
);
96 /* Interface information. */
97 stream_put (s
, ifp
->name
, INTERFACE_NAMSIZ
);
98 stream_putl (s
, ifp
->ifindex
);
99 stream_putc (s
, ifp
->status
);
100 stream_putl (s
, ifp
->flags
);
101 stream_putl (s
, ifp
->metric
);
102 stream_putl (s
, ifp
->mtu
);
103 stream_putl (s
, ifp
->bandwidth
);
104 #ifdef HAVE_SOCKADDR_DL
105 stream_put (s
, &ifp
->sdl
, sizeof (ifp
->sdl
));
107 stream_putl (s
, ifp
->hw_addr_len
);
108 if (ifp
->hw_addr_len
)
109 stream_put (s
, ifp
->hw_addr
, ifp
->hw_addr_len
);
110 #endif /* HAVE_SOCKADDR_DL */
112 /* Write packet size. */
113 stream_putw_at (s
, 0, stream_get_endp (s
));
115 return writen (client
->sock
, s
->data
, stream_get_endp (s
));
118 /* Interface deletion from zebra daemon. */
120 zsend_interface_delete (struct zserv
*client
, struct interface
*ifp
)
124 /* Check this client need interface information. */
125 if (! client
->ifinfo
)
131 /* Packet length placeholder. */
134 /* Interface information. */
135 stream_putc (s
, ZEBRA_INTERFACE_DELETE
);
136 stream_put (s
, ifp
->name
, INTERFACE_NAMSIZ
);
137 stream_putl (s
, ifp
->ifindex
);
138 stream_putc (s
, ifp
->status
);
139 stream_putl (s
, ifp
->flags
);
140 stream_putl (s
, ifp
->metric
);
141 stream_putl (s
, ifp
->mtu
);
142 stream_putl (s
, ifp
->bandwidth
);
144 /* Write packet length. */
145 stream_putw_at (s
, 0, stream_get_endp (s
));
147 return writen (client
->sock
, s
->data
, stream_get_endp (s
));
150 /* Interface address is added. Send ZEBRA_INTERFACE_ADDRESS_ADD to the
153 zsend_interface_address_add (struct zserv
*client
, struct interface
*ifp
,
154 struct connected
*ifc
)
160 /* Check this client need interface information. */
161 if (! client
->ifinfo
)
167 /* Place holder for size. */
170 stream_putc (s
, ZEBRA_INTERFACE_ADDRESS_ADD
);
171 stream_putl (s
, ifp
->ifindex
);
173 /* Interface address flag. */
174 stream_putc (s
, ifc
->flags
);
176 /* Prefix information. */
178 stream_putc (s
, p
->family
);
179 blen
= prefix_blen (p
);
180 stream_put (s
, &p
->u
.prefix
, blen
);
181 stream_putc (s
, p
->prefixlen
);
184 p
= ifc
->destination
;
186 stream_put (s
, &p
->u
.prefix
, blen
);
188 stream_put (s
, NULL
, blen
);
190 /* Write packet size. */
191 stream_putw_at (s
, 0, stream_get_endp (s
));
193 return writen (client
->sock
, s
->data
, stream_get_endp (s
));
196 /* Interface address is deleted. Send ZEBRA_INTERFACE_ADDRESS_DELETE
199 zsend_interface_address_delete (struct zserv
*client
, struct interface
*ifp
,
200 struct connected
*ifc
)
206 /* Check this client need interface information. */
207 if (! client
->ifinfo
)
213 /* Place holder for size. */
216 stream_putc (s
, ZEBRA_INTERFACE_ADDRESS_DELETE
);
217 stream_putl (s
, ifp
->ifindex
);
219 /* Interface address flag. */
220 stream_putc (s
, ifc
->flags
);
222 /* Prefix information. */
224 stream_putc (s
, p
->family
);
225 blen
= prefix_blen (p
);
226 stream_put (s
, &p
->u
.prefix
, blen
);
228 p
= ifc
->destination
;
230 stream_put (s
, &p
->u
.prefix
, blen
);
232 stream_put (s
, NULL
, blen
);
234 /* Write packet size. */
235 stream_putw_at (s
, 0, stream_get_endp (s
));
237 return writen (client
->sock
, s
->data
, stream_get_endp (s
));
241 zsend_interface_up (struct zserv
*client
, struct interface
*ifp
)
245 /* Check this client need interface information. */
246 if (! client
->ifinfo
)
252 /* Place holder for size. */
256 stream_putc (s
, ZEBRA_INTERFACE_UP
);
258 /* Interface information. */
259 stream_put (s
, ifp
->name
, INTERFACE_NAMSIZ
);
260 stream_putl (s
, ifp
->ifindex
);
261 stream_putc (s
, ifp
->status
);
262 stream_putl (s
, ifp
->flags
);
263 stream_putl (s
, ifp
->metric
);
264 stream_putl (s
, ifp
->mtu
);
265 stream_putl (s
, ifp
->bandwidth
);
267 /* Write packet size. */
268 stream_putw_at (s
, 0, stream_get_endp (s
));
270 return writen (client
->sock
, s
->data
, stream_get_endp (s
));
274 zsend_interface_down (struct zserv
*client
, struct interface
*ifp
)
278 /* Check this client need interface information. */
279 if (! client
->ifinfo
)
285 /* Place holder for size. */
289 stream_putc (s
, ZEBRA_INTERFACE_DOWN
);
291 /* Interface information. */
292 stream_put (s
, ifp
->name
, INTERFACE_NAMSIZ
);
293 stream_putl (s
, ifp
->ifindex
);
294 stream_putc (s
, ifp
->status
);
295 stream_putl (s
, ifp
->flags
);
296 stream_putl (s
, ifp
->metric
);
297 stream_putl (s
, ifp
->mtu
);
298 stream_putl (s
, ifp
->bandwidth
);
300 /* Write packet size. */
301 stream_putw_at (s
, 0, stream_get_endp (s
));
303 return writen (client
->sock
, s
->data
, stream_get_endp (s
));
307 zsend_ipv4_add_multipath (struct zserv
*client
, struct prefix
*p
,
312 struct nexthop
*nexthop
;
313 struct in_addr empty
;
319 /* Place holder for size. */
322 /* Put command, type and nexthop. */
323 stream_putc (s
, ZEBRA_IPV4_ROUTE_ADD
);
324 stream_putc (s
, rib
->type
);
325 stream_putc (s
, rib
->flags
);
326 stream_putc (s
, ZAPI_MESSAGE_NEXTHOP
| ZAPI_MESSAGE_IFINDEX
| ZAPI_MESSAGE_METRIC
);
329 psize
= PSIZE (p
->prefixlen
);
330 stream_putc (s
, p
->prefixlen
);
331 stream_write (s
, (u_char
*)&p
->u
.prefix
, psize
);
334 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
336 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
340 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
341 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
)
342 stream_put_in_addr (s
, &nexthop
->gate
.ipv4
);
344 stream_put_in_addr (s
, &empty
);
346 /* Interface index. */
348 stream_putl (s
, nexthop
->ifindex
);
355 stream_putl (s
, rib
->metric
);
357 /* Write packet size. */
358 stream_putw_at (s
, 0, stream_get_endp (s
));
360 return writen (client
->sock
, s
->data
, stream_get_endp (s
));
364 zsend_ipv4_delete_multipath (struct zserv
*client
, struct prefix
*p
,
369 struct nexthop
*nexthop
;
370 struct in_addr empty
;
377 /* Place holder for size. */
380 /* Put command, type and nexthop. */
381 stream_putc (s
, ZEBRA_IPV4_ROUTE_DELETE
);
382 stream_putc (s
, rib
->type
);
383 stream_putc (s
, rib
->flags
);
384 stream_putc (s
, ZAPI_MESSAGE_NEXTHOP
|ZAPI_MESSAGE_IFINDEX
);
387 psize
= PSIZE (p
->prefixlen
);
388 stream_putc (s
, p
->prefixlen
);
389 stream_write (s
, (u_char
*)&p
->u
.prefix
, psize
);
392 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
394 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
398 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
)
399 stream_put_in_addr (s
, &nexthop
->gate
.ipv4
);
401 stream_put_in_addr (s
, &empty
);
403 /* Interface index. */
405 stream_putl (s
, nexthop
->ifindex
);
411 /* Write packet size. */
412 stream_putw_at (s
, 0, stream_get_endp (s
));
414 return writen (client
->sock
, s
->data
, stream_get_endp (s
));
418 zsend_ipv4_add (struct zserv
*client
, int type
, int flags
,
419 struct prefix_ipv4
*p
, struct in_addr
*nexthop
,
420 unsigned int ifindex
)
428 /* Place holder for size. */
431 /* Put command, type and nexthop. */
432 stream_putc (s
, ZEBRA_IPV4_ROUTE_ADD
);
433 stream_putc (s
, type
);
434 stream_putc (s
, flags
);
435 stream_putc (s
, ZAPI_MESSAGE_NEXTHOP
|ZAPI_MESSAGE_IFINDEX
);
438 psize
= PSIZE (p
->prefixlen
);
439 stream_putc (s
, p
->prefixlen
);
440 stream_write (s
, (u_char
*)&p
->prefix
, psize
);
444 stream_put_in_addr (s
, nexthop
);
446 /* Interface index. */
448 stream_putl (s
, ifindex
);
450 /* Write packet size. */
451 stream_putw_at (s
, 0, stream_get_endp (s
));
453 return writen (client
->sock
, s
->data
, stream_get_endp (s
));
457 zsend_ipv4_delete (struct zserv
*client
, int type
, int flags
,
458 struct prefix_ipv4
*p
, struct in_addr
*nexthop
,
459 unsigned int ifindex
)
467 /* Place holder for size. */
470 /* Put command, type and nexthop. */
471 stream_putc (s
, ZEBRA_IPV4_ROUTE_DELETE
);
472 stream_putc (s
, type
);
473 stream_putc (s
, flags
);
474 stream_putc (s
, ZAPI_MESSAGE_NEXTHOP
|ZAPI_MESSAGE_IFINDEX
);
477 psize
= PSIZE (p
->prefixlen
);
478 stream_putc (s
, p
->prefixlen
);
479 stream_write (s
, (u_char
*)&p
->prefix
, psize
);
483 stream_put_in_addr (s
, nexthop
);
485 /* Interface index. */
487 stream_putl (s
, ifindex
);
489 /* Write packet size. */
490 stream_putw_at (s
, 0, stream_get_endp (s
));
492 return writen (client
->sock
, s
->data
, stream_get_endp (s
));
497 zsend_ipv6_add (struct zserv
*client
, int type
, int flags
,
498 struct prefix_ipv6
*p
, struct in6_addr
*nexthop
,
499 unsigned int ifindex
)
507 /* Place holder for size. */
510 /* Put command, type and nexthop. */
511 stream_putc (s
, ZEBRA_IPV6_ROUTE_ADD
);
512 stream_putc (s
, type
);
513 stream_putc (s
, flags
);
514 stream_putc (s
, ZAPI_MESSAGE_NEXTHOP
|ZAPI_MESSAGE_IFINDEX
);
517 psize
= PSIZE (p
->prefixlen
);
518 stream_putc (s
, p
->prefixlen
);
519 stream_write (s
, (u_char
*)&p
->prefix
, psize
);
523 stream_write (s
, (u_char
*)nexthop
, 16);
525 /* Interface index. */
527 stream_putl (s
, ifindex
);
529 /* Write packet size. */
530 stream_putw_at (s
, 0, stream_get_endp (s
));
532 return writen (client
->sock
, s
->data
, stream_get_endp (s
));
536 zsend_ipv6_add_multipath (struct zserv
*client
, struct prefix
*p
,
541 struct nexthop
*nexthop
;
542 struct in6_addr empty
;
544 memset (&empty
, 0, sizeof (struct in6_addr
));
548 /* Place holder for size. */
551 /* Put command, type and nexthop. */
552 stream_putc (s
, ZEBRA_IPV6_ROUTE_ADD
);
553 stream_putc (s
, rib
->type
);
554 stream_putc (s
, rib
->flags
);
555 stream_putc (s
, ZAPI_MESSAGE_NEXTHOP
| ZAPI_MESSAGE_IFINDEX
| ZAPI_MESSAGE_METRIC
);
558 psize
= PSIZE (p
->prefixlen
);
559 stream_putc (s
, p
->prefixlen
);
560 stream_write (s
, (u_char
*) &p
->u
.prefix
, psize
);
563 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
565 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
569 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
)
570 stream_write (s
, (u_char
*) &nexthop
->gate
.ipv6
, 16);
572 stream_write (s
, (u_char
*) &empty
, 16);
574 /* Interface index. */
576 stream_putl (s
, nexthop
->ifindex
);
583 stream_putl (s
, rib
->metric
);
585 /* Write packet size. */
586 stream_putw_at (s
, 0, stream_get_endp (s
));
588 return writen (client
->sock
, s
->data
, stream_get_endp (s
));
592 zsend_ipv6_delete (struct zserv
*client
, int type
, int flags
,
593 struct prefix_ipv6
*p
, struct in6_addr
*nexthop
,
594 unsigned int ifindex
)
602 /* Place holder for size. */
605 /* Put command, type and nexthop. */
606 stream_putc (s
, ZEBRA_IPV6_ROUTE_DELETE
);
607 stream_putc (s
, type
);
608 stream_putc (s
, flags
);
609 stream_putc (s
, ZAPI_MESSAGE_NEXTHOP
|ZAPI_MESSAGE_IFINDEX
);
612 psize
= PSIZE (p
->prefixlen
);
613 stream_putc (s
, p
->prefixlen
);
614 stream_write (s
, (u_char
*)&p
->prefix
, psize
);
618 stream_write (s
, (u_char
*)nexthop
, 16);
620 /* Interface index. */
622 stream_putl (s
, ifindex
);
624 /* Write packet size. */
625 stream_putw_at (s
, 0, stream_get_endp (s
));
627 return writen (client
->sock
, s
->data
, stream_get_endp (s
));
631 zsend_ipv6_delete_multipath (struct zserv
*client
, struct prefix
*p
,
636 struct nexthop
*nexthop
;
637 struct in6_addr empty
;
639 memset (&empty
, 0, sizeof (struct in6_addr
));
643 /* Place holder for size. */
646 /* Put command, type and nexthop. */
647 stream_putc (s
, ZEBRA_IPV6_ROUTE_DELETE
);
648 stream_putc (s
, rib
->type
);
649 stream_putc (s
, rib
->flags
);
650 stream_putc (s
, ZAPI_MESSAGE_NEXTHOP
|ZAPI_MESSAGE_IFINDEX
);
653 psize
= PSIZE (p
->prefixlen
);
654 stream_putc (s
, p
->prefixlen
);
655 stream_write (s
, (u_char
*)&p
->u
.prefix
, psize
);
658 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
660 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
664 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
)
665 stream_write (s
, (u_char
*) &nexthop
->gate
.ipv6
, 16);
667 stream_write (s
, (u_char
*) &empty
, 16);
669 /* Interface index. */
671 stream_putl (s
, nexthop
->ifindex
);
677 /* Write packet size. */
678 stream_putw_at (s
, 0, stream_get_endp (s
));
680 return writen (client
->sock
, s
->data
, stream_get_endp (s
));
684 zsend_ipv6_nexthop_lookup (struct zserv
*client
, struct in6_addr
*addr
)
690 struct nexthop
*nexthop
;
692 /* Lookup nexthop. */
693 rib
= rib_match_ipv6 (addr
);
695 /* Get output stream. */
699 /* Fill in result. */
701 stream_putc (s
, ZEBRA_IPV6_NEXTHOP_LOOKUP
);
702 stream_put (s
, &addr
, 16);
706 stream_putl (s
, rib
->metric
);
710 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
711 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
713 stream_putc (s
, nexthop
->type
);
714 switch (nexthop
->type
)
716 case ZEBRA_NEXTHOP_IPV6
:
717 stream_put (s
, &nexthop
->gate
.ipv6
, 16);
719 case ZEBRA_NEXTHOP_IPV6_IFINDEX
:
720 case ZEBRA_NEXTHOP_IPV6_IFNAME
:
721 stream_put (s
, &nexthop
->gate
.ipv6
, 16);
722 stream_putl (s
, nexthop
->ifindex
);
724 case ZEBRA_NEXTHOP_IFINDEX
:
725 case ZEBRA_NEXTHOP_IFNAME
:
726 stream_putl (s
, nexthop
->ifindex
);
731 stream_putc_at (s
, nump
, num
);
739 stream_putw_at (s
, 0, stream_get_endp (s
));
741 return writen (client
->sock
, s
->data
, stream_get_endp (s
));
743 #endif /* HAVE_IPV6 */
746 zsend_ipv4_nexthop_lookup (struct zserv
*client
, struct in_addr addr
)
752 struct nexthop
*nexthop
;
754 /* Lookup nexthop. */
755 rib
= rib_match_ipv4 (addr
);
757 /* Get output stream. */
761 /* Fill in result. */
763 stream_putc (s
, ZEBRA_IPV4_NEXTHOP_LOOKUP
);
764 stream_put_in_addr (s
, &addr
);
768 stream_putl (s
, rib
->metric
);
772 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
773 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
775 stream_putc (s
, nexthop
->type
);
776 switch (nexthop
->type
)
778 case ZEBRA_NEXTHOP_IPV4
:
779 stream_put_in_addr (s
, &nexthop
->gate
.ipv4
);
781 case ZEBRA_NEXTHOP_IFINDEX
:
782 case ZEBRA_NEXTHOP_IFNAME
:
783 stream_putl (s
, nexthop
->ifindex
);
788 stream_putc_at (s
, nump
, num
);
796 stream_putw_at (s
, 0, stream_get_endp (s
));
798 return writen (client
->sock
, s
->data
, stream_get_endp (s
));
802 zsend_ipv4_import_lookup (struct zserv
*client
, struct prefix_ipv4
*p
)
808 struct nexthop
*nexthop
;
810 /* Lookup nexthop. */
811 rib
= rib_lookup_ipv4 (p
);
813 /* Get output stream. */
817 /* Fill in result. */
819 stream_putc (s
, ZEBRA_IPV4_IMPORT_LOOKUP
);
820 stream_put_in_addr (s
, &p
->prefix
);
824 stream_putl (s
, rib
->metric
);
828 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
829 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
831 stream_putc (s
, nexthop
->type
);
832 switch (nexthop
->type
)
834 case ZEBRA_NEXTHOP_IPV4
:
835 stream_put_in_addr (s
, &nexthop
->gate
.ipv4
);
837 case ZEBRA_NEXTHOP_IFINDEX
:
838 case ZEBRA_NEXTHOP_IFNAME
:
839 stream_putl (s
, nexthop
->ifindex
);
844 stream_putc_at (s
, nump
, num
);
852 stream_putw_at (s
, 0, stream_get_endp (s
));
854 return writen (client
->sock
, s
->data
, stream_get_endp (s
));
857 /* Register zebra server interface information. Send current all
858 interface and address information. */
860 zread_interface_add (struct zserv
*client
, u_short length
)
864 struct interface
*ifp
;
867 /* Interface information is needed. */
870 for (ifnode
= listhead (iflist
); ifnode
; ifnode
= nextnode (ifnode
))
872 ifp
= getdata (ifnode
);
874 /* Skip pseudo interface. */
875 if (! CHECK_FLAG (ifp
->status
, ZEBRA_INTERFACE_ACTIVE
))
878 zsend_interface_add (client
, ifp
);
880 for (cnode
= listhead (ifp
->connected
); cnode
; nextnode (cnode
))
883 if (CHECK_FLAG (c
->conf
, ZEBRA_IFC_REAL
))
884 zsend_interface_address_add (client
, ifp
, c
);
889 /* Unregister zebra server interface information. */
891 zread_interface_delete (struct zserv
*client
, u_short length
)
896 /* This function support multiple nexthop. */
898 zread_ipv4_add (struct zserv
*client
, u_short length
)
902 struct prefix_ipv4 p
;
904 struct in_addr nexthop
;
908 unsigned int ifindex
;
911 /* Get input stream. */
914 /* Allocate new rib. */
915 rib
= XMALLOC (MTYPE_RIB
, sizeof (struct rib
));
916 memset (rib
, 0, sizeof (struct rib
));
918 /* Type, flags, message. */
919 rib
->type
= stream_getc (s
);
920 rib
->flags
= stream_getc (s
);
921 message
= stream_getc (s
);
922 rib
->uptime
= time (NULL
);
925 memset (&p
, 0, sizeof (struct prefix_ipv4
));
927 p
.prefixlen
= stream_getc (s
);
928 stream_get (&p
.prefix
, s
, PSIZE (p
.prefixlen
));
931 if (CHECK_FLAG (message
, ZAPI_MESSAGE_NEXTHOP
))
933 nexthop_num
= stream_getc (s
);
935 for (i
= 0; i
< nexthop_num
; i
++)
937 nexthop_type
= stream_getc (s
);
939 switch (nexthop_type
)
941 case ZEBRA_NEXTHOP_IFINDEX
:
942 ifindex
= stream_getl (s
);
943 nexthop_ifindex_add (rib
, ifindex
);
945 case ZEBRA_NEXTHOP_IFNAME
:
946 ifname_len
= stream_getc (s
);
947 stream_forward (s
, ifname_len
);
949 case ZEBRA_NEXTHOP_IPV4
:
950 nexthop
.s_addr
= stream_get_ipv4 (s
);
951 nexthop_ipv4_add (rib
, &nexthop
);
953 case ZEBRA_NEXTHOP_IPV6
:
954 stream_forward (s
, IPV6_MAX_BYTELEN
);
956 case ZEBRA_NEXTHOP_BLACKHOLE
:
957 nexthop_blackhole_add (rib
);
964 if (CHECK_FLAG (message
, ZAPI_MESSAGE_DISTANCE
))
965 rib
->distance
= stream_getc (s
);
968 if (CHECK_FLAG (message
, ZAPI_MESSAGE_METRIC
))
969 rib
->metric
= stream_getl (s
);
971 rib_add_ipv4_multipath (&p
, rib
);
974 /* Zebra server IPv4 prefix delete function. */
976 zread_ipv4_delete (struct zserv
*client
, u_short length
)
980 struct zapi_ipv4 api
;
981 struct in_addr nexthop
;
982 unsigned long ifindex
;
983 struct prefix_ipv4 p
;
992 /* Type, flags, message. */
993 api
.type
= stream_getc (s
);
994 api
.flags
= stream_getc (s
);
995 api
.message
= stream_getc (s
);
998 memset (&p
, 0, sizeof (struct prefix_ipv4
));
1000 p
.prefixlen
= stream_getc (s
);
1001 stream_get (&p
.prefix
, s
, PSIZE (p
.prefixlen
));
1003 /* Nexthop, ifindex, distance, metric. */
1004 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
))
1006 nexthop_num
= stream_getc (s
);
1008 for (i
= 0; i
< nexthop_num
; i
++)
1010 nexthop_type
= stream_getc (s
);
1012 switch (nexthop_type
)
1014 case ZEBRA_NEXTHOP_IFINDEX
:
1015 ifindex
= stream_getl (s
);
1017 case ZEBRA_NEXTHOP_IFNAME
:
1018 ifname_len
= stream_getc (s
);
1019 stream_forward (s
, ifname_len
);
1021 case ZEBRA_NEXTHOP_IPV4
:
1022 nexthop
.s_addr
= stream_get_ipv4 (s
);
1024 case ZEBRA_NEXTHOP_IPV6
:
1025 stream_forward (s
, IPV6_MAX_BYTELEN
);
1032 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_DISTANCE
))
1033 api
.distance
= stream_getc (s
);
1038 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_METRIC
))
1039 api
.metric
= stream_getl (s
);
1043 rib_delete_ipv4 (api
.type
, api
.flags
, &p
, &nexthop
, ifindex
,
1047 /* Nexthop lookup for IPv4. */
1049 zread_ipv4_nexthop_lookup (struct zserv
*client
, u_short length
)
1051 struct in_addr addr
;
1053 addr
.s_addr
= stream_get_ipv4 (client
->ibuf
);
1054 zsend_ipv4_nexthop_lookup (client
, addr
);
1057 /* Nexthop lookup for IPv4. */
1059 zread_ipv4_import_lookup (struct zserv
*client
, u_short length
)
1061 struct prefix_ipv4 p
;
1064 p
.prefixlen
= stream_getc (client
->ibuf
);
1065 p
.prefix
.s_addr
= stream_get_ipv4 (client
->ibuf
);
1067 zsend_ipv4_import_lookup (client
, &p
);
1071 /* Zebra server IPv6 prefix add function. */
1073 zread_ipv6_add (struct zserv
*client
, u_short length
)
1077 struct zapi_ipv6 api
;
1078 struct in6_addr nexthop
;
1079 unsigned long ifindex
;
1080 struct prefix_ipv6 p
;
1084 memset (&nexthop
, 0, sizeof (struct in6_addr
));
1086 /* Type, flags, message. */
1087 api
.type
= stream_getc (s
);
1088 api
.flags
= stream_getc (s
);
1089 api
.message
= stream_getc (s
);
1092 memset (&p
, 0, sizeof (struct prefix_ipv6
));
1093 p
.family
= AF_INET6
;
1094 p
.prefixlen
= stream_getc (s
);
1095 stream_get (&p
.prefix
, s
, PSIZE (p
.prefixlen
));
1097 /* Nexthop, ifindex, distance, metric. */
1098 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
))
1100 u_char nexthop_type
;
1102 api
.nexthop_num
= stream_getc (s
);
1103 for (i
= 0; i
< api
.nexthop_num
; i
++)
1105 nexthop_type
= stream_getc (s
);
1107 switch (nexthop_type
)
1109 case ZEBRA_NEXTHOP_IPV6
:
1110 stream_get (&nexthop
, s
, 16);
1112 case ZEBRA_NEXTHOP_IFINDEX
:
1113 ifindex
= stream_getl (s
);
1119 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_DISTANCE
))
1120 api
.distance
= stream_getc (s
);
1124 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_METRIC
))
1125 api
.metric
= stream_getl (s
);
1129 if (IN6_IS_ADDR_UNSPECIFIED (&nexthop
))
1130 rib_add_ipv6 (api
.type
, api
.flags
, &p
, NULL
, ifindex
, 0);
1132 rib_add_ipv6 (api
.type
, api
.flags
, &p
, &nexthop
, ifindex
, 0);
1135 /* Zebra server IPv6 prefix delete function. */
1137 zread_ipv6_delete (struct zserv
*client
, u_short length
)
1141 struct zapi_ipv6 api
;
1142 struct in6_addr nexthop
;
1143 unsigned long ifindex
;
1144 struct prefix_ipv6 p
;
1148 memset (&nexthop
, 0, sizeof (struct in6_addr
));
1150 /* Type, flags, message. */
1151 api
.type
= stream_getc (s
);
1152 api
.flags
= stream_getc (s
);
1153 api
.message
= stream_getc (s
);
1156 memset (&p
, 0, sizeof (struct prefix_ipv6
));
1157 p
.family
= AF_INET6
;
1158 p
.prefixlen
= stream_getc (s
);
1159 stream_get (&p
.prefix
, s
, PSIZE (p
.prefixlen
));
1161 /* Nexthop, ifindex, distance, metric. */
1162 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
))
1164 u_char nexthop_type
;
1166 api
.nexthop_num
= stream_getc (s
);
1167 for (i
= 0; i
< api
.nexthop_num
; i
++)
1169 nexthop_type
= stream_getc (s
);
1171 switch (nexthop_type
)
1173 case ZEBRA_NEXTHOP_IPV6
:
1174 stream_get (&nexthop
, s
, 16);
1176 case ZEBRA_NEXTHOP_IFINDEX
:
1177 ifindex
= stream_getl (s
);
1183 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_DISTANCE
))
1184 api
.distance
= stream_getc (s
);
1187 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_METRIC
))
1188 api
.metric
= stream_getl (s
);
1192 if (IN6_IS_ADDR_UNSPECIFIED (&nexthop
))
1193 rib_delete_ipv6 (api
.type
, api
.flags
, &p
, NULL
, ifindex
, 0);
1195 rib_delete_ipv6 (api
.type
, api
.flags
, &p
, &nexthop
, ifindex
, 0);
1199 zebra_read_ipv6 (int command
, struct zserv
*client
, u_short length
)
1203 struct in6_addr nexthop
, *gate
;
1206 unsigned int ifindex
;
1208 pnt
= stream_pnt (client
->ibuf
);
1211 type
= stream_getc (client
->ibuf
);
1212 flags
= stream_getc (client
->ibuf
);
1213 stream_get (&nexthop
, client
->ibuf
, sizeof (struct in6_addr
));
1215 while (stream_pnt (client
->ibuf
) < lim
)
1218 struct prefix_ipv6 p
;
1220 ifindex
= stream_getl (client
->ibuf
);
1222 memset (&p
, 0, sizeof (struct prefix_ipv6
));
1223 p
.family
= AF_INET6
;
1224 p
.prefixlen
= stream_getc (client
->ibuf
);
1225 size
= PSIZE(p
.prefixlen
);
1226 stream_get (&p
.prefix
, client
->ibuf
, size
);
1228 if (IN6_IS_ADDR_UNSPECIFIED (&nexthop
))
1233 if (command
== ZEBRA_IPV6_ROUTE_ADD
)
1234 rib_add_ipv6 (type
, flags
, &p
, gate
, ifindex
, 0);
1236 rib_delete_ipv6 (type
, flags
, &p
, gate
, ifindex
, 0);
1241 zread_ipv6_nexthop_lookup (struct zserv
*client
, u_short length
)
1243 struct in6_addr addr
;
1246 stream_get (&addr
, client
->ibuf
, 16);
1247 printf ("DEBUG %s\n", inet_ntop (AF_INET6
, &addr
, buf
, BUFSIZ
));
1249 zsend_ipv6_nexthop_lookup (client
, &addr
);
1251 #endif /* HAVE_IPV6 */
1253 /* Close zebra client. */
1255 zebra_client_close (struct zserv
*client
)
1257 /* Close file descriptor. */
1260 close (client
->sock
);
1264 /* Free stream buffers. */
1266 stream_free (client
->ibuf
);
1268 stream_free (client
->obuf
);
1270 /* Release threads. */
1272 thread_cancel (client
->t_read
);
1273 if (client
->t_write
)
1274 thread_cancel (client
->t_write
);
1276 /* Free client structure. */
1277 listnode_delete (client_list
, client
);
1281 /* Make new client. */
1283 zebra_client_create (int sock
)
1285 struct zserv
*client
;
1287 client
= XCALLOC (0, sizeof (struct zserv
));
1289 /* Make client input/output buffer. */
1290 client
->sock
= sock
;
1291 client
->ibuf
= stream_new (ZEBRA_MAX_PACKET_SIZ
);
1292 client
->obuf
= stream_new (ZEBRA_MAX_PACKET_SIZ
);
1294 /* Set table number. */
1295 client
->rtm_table
= rtm_table_default
;
1297 /* Add this client to linked list. */
1298 listnode_add (client_list
, client
);
1300 /* Make new read thread. */
1301 zebra_event (ZEBRA_READ
, sock
, client
);
1304 /* Handler of zebra service request. */
1306 zebra_client_read (struct thread
*thread
)
1309 struct zserv
*client
;
1314 /* Get thread data. Reset reading thread because I'm running. */
1315 sock
= THREAD_FD (thread
);
1316 client
= THREAD_ARG (thread
);
1317 client
->t_read
= NULL
;
1319 /* Read length and command. */
1320 nbyte
= stream_read (client
->ibuf
, sock
, 3);
1323 if (IS_ZEBRA_DEBUG_EVENT
)
1324 zlog_info ("connection closed socket [%d]", sock
);
1325 zebra_client_close (client
);
1328 length
= stream_getw (client
->ibuf
);
1329 command
= stream_getc (client
->ibuf
);
1333 if (IS_ZEBRA_DEBUG_EVENT
)
1334 zlog_info ("length %d is less than 3 ", length
);
1335 zebra_client_close (client
);
1341 /* Read rest of data. */
1344 nbyte
= stream_read (client
->ibuf
, sock
, length
);
1347 if (IS_ZEBRA_DEBUG_EVENT
)
1348 zlog_info ("connection closed [%d] when reading zebra data", sock
);
1349 zebra_client_close (client
);
1354 /* Debug packet information. */
1355 if (IS_ZEBRA_DEBUG_EVENT
)
1356 zlog_info ("zebra message comes from socket [%d]", sock
);
1358 if (IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
)
1359 zlog_info ("zebra message received [%s] %d",
1360 zebra_command_str
[command
], length
);
1364 case ZEBRA_INTERFACE_ADD
:
1365 zread_interface_add (client
, length
);
1367 case ZEBRA_INTERFACE_DELETE
:
1368 zread_interface_delete (client
, length
);
1370 case ZEBRA_IPV4_ROUTE_ADD
:
1371 zread_ipv4_add (client
, length
);
1373 case ZEBRA_IPV4_ROUTE_DELETE
:
1374 zread_ipv4_delete (client
, length
);
1377 case ZEBRA_IPV6_ROUTE_ADD
:
1378 zread_ipv6_add (client
, length
);
1380 case ZEBRA_IPV6_ROUTE_DELETE
:
1381 zread_ipv6_delete (client
, length
);
1383 #endif /* HAVE_IPV6 */
1384 case ZEBRA_REDISTRIBUTE_ADD
:
1385 zebra_redistribute_add (command
, client
, length
);
1387 case ZEBRA_REDISTRIBUTE_DELETE
:
1388 zebra_redistribute_delete (command
, client
, length
);
1390 case ZEBRA_REDISTRIBUTE_DEFAULT_ADD
:
1391 zebra_redistribute_default_add (command
, client
, length
);
1393 case ZEBRA_REDISTRIBUTE_DEFAULT_DELETE
:
1394 zebra_redistribute_default_delete (command
, client
, length
);
1396 case ZEBRA_IPV4_NEXTHOP_LOOKUP
:
1397 zread_ipv4_nexthop_lookup (client
, length
);
1400 case ZEBRA_IPV6_NEXTHOP_LOOKUP
:
1401 zread_ipv6_nexthop_lookup (client
, length
);
1403 #endif /* HAVE_IPV6 */
1404 case ZEBRA_IPV4_IMPORT_LOOKUP
:
1405 zread_ipv4_import_lookup (client
, length
);
1408 zlog_info ("Zebra received unknown command %d", command
);
1412 stream_reset (client
->ibuf
);
1413 zebra_event (ZEBRA_READ
, sock
, client
);
1418 /* Write output buffer to the socket. */
1420 zebra_write (struct thread
*thread
)
1423 struct zserv
*client
;
1425 /* Thread treatment. */
1426 sock
= THREAD_FD (thread
);
1427 client
= THREAD_ARG (thread
);
1428 client
->t_write
= NULL
;
1430 stream_flush (client
->obuf
, sock
);
1433 /* Accept code of zebra server socket. */
1435 zebra_accept (struct thread
*thread
)
1439 struct sockaddr_in client
;
1442 accept_sock
= THREAD_FD (thread
);
1444 len
= sizeof (struct sockaddr_in
);
1445 client_sock
= accept (accept_sock
, (struct sockaddr
*) &client
, &len
);
1447 if (client_sock
< 0)
1449 zlog_warn ("Can't accept zebra socket: %s", strerror (errno
));
1453 /* Create new zebra client. */
1454 zebra_client_create (client_sock
);
1456 /* Register myself. */
1457 zebra_event (ZEBRA_SERV
, accept_sock
, NULL
);
1462 /* Make zebra's server socket. */
1468 struct sockaddr_in addr
;
1470 accept_sock
= socket (AF_INET
, SOCK_STREAM
, 0);
1472 if (accept_sock
< 0)
1474 zlog_warn ("Can't bind to socket: %s", strerror (errno
));
1475 zlog_warn ("zebra can't provice full functionality due to above error");
1479 memset (&addr
, 0, sizeof (struct sockaddr_in
));
1480 addr
.sin_family
= AF_INET
;
1481 addr
.sin_port
= htons (ZEBRA_PORT
);
1483 addr
.sin_len
= sizeof (struct sockaddr_in
);
1484 #endif /* HAVE_SIN_LEN */
1485 addr
.sin_addr
.s_addr
= htonl (INADDR_LOOPBACK
);
1487 sockopt_reuseaddr (accept_sock
);
1488 sockopt_reuseport (accept_sock
);
1490 ret
= bind (accept_sock
, (struct sockaddr
*)&addr
,
1491 sizeof (struct sockaddr_in
));
1494 zlog_warn ("Can't bind to socket: %s", strerror (errno
));
1495 zlog_warn ("zebra can't provice full functionality due to above error");
1496 close (accept_sock
); /* Avoid sd leak. */
1500 ret
= listen (accept_sock
, 1);
1503 zlog_warn ("Can't listen to socket: %s", strerror (errno
));
1504 zlog_warn ("zebra can't provice full functionality due to above error");
1505 close (accept_sock
); /* Avoid sd leak. */
1509 zebra_event (ZEBRA_SERV
, accept_sock
, NULL
);
1512 /* For sockaddr_un. */
1515 /* zebra server UNIX domain socket. */
1517 zebra_serv_un (char *path
)
1521 struct sockaddr_un serv
;
1524 /* First of all, unlink existing socket */
1528 old_mask
= umask (0077);
1530 /* Make UNIX domain socket. */
1531 sock
= socket (AF_UNIX
, SOCK_STREAM
, 0);
1538 /* Make server socket. */
1539 memset (&serv
, 0, sizeof (struct sockaddr_un
));
1540 serv
.sun_family
= AF_UNIX
;
1541 strncpy (serv
.sun_path
, path
, strlen (path
));
1543 len
= serv
.sun_len
= SUN_LEN(&serv
);
1545 len
= sizeof (serv
.sun_family
) + strlen (serv
.sun_path
);
1546 #endif /* HAVE_SUN_LEN */
1548 ret
= bind (sock
, (struct sockaddr
*) &serv
, len
);
1556 ret
= listen (sock
, 5);
1566 zebra_event (ZEBRA_SERV
, sock
, NULL
);
1569 /* Zebra's event management function. */
1570 extern struct thread_master
*master
;
1573 zebra_event (enum event event
, int sock
, struct zserv
*client
)
1578 thread_add_read (master
, zebra_accept
, client
, sock
);
1582 thread_add_read (master
, zebra_client_read
, client
, sock
);
1590 /* Display default rtm_table for all clients. */
1595 "default routing table to use for all clients\n")
1597 vty_out (vty
, "table %d%s", rtm_table_default
,
1602 DEFUN (config_table
,
1605 "Configure target kernel routing table\n"
1608 rtm_table_default
= strtol (argv
[0], (char**)0, 10);
1612 DEFUN (no_ip_forwarding
,
1613 no_ip_forwarding_cmd
,
1617 "Turn off IP forwarding")
1625 vty_out (vty
, "IP forwarding is already off%s", VTY_NEWLINE
);
1626 return CMD_ERR_NOTHING_TODO
;
1629 ret
= ipforward_off ();
1632 vty_out (vty
, "Can't turn off IP forwarding%s", VTY_NEWLINE
);
1639 /* This command is for debugging purpose. */
1640 DEFUN (show_zebra_client
,
1641 show_zebra_client_cmd
,
1642 "show zebra client",
1645 "Client information")
1648 struct zserv
*client
;
1650 for (node
= listhead (client_list
); node
; nextnode (node
))
1652 client
= getdata (node
);
1653 vty_out (vty
, "Client fd %d%s", client
->sock
, VTY_NEWLINE
);
1658 /* Table configuration write function. */
1660 config_write_table (struct vty
*vty
)
1662 if (rtm_table_default
)
1663 vty_out (vty
, "table %d%s", rtm_table_default
,
1668 /* table node for routing tables. */
1669 struct cmd_node table_node
=
1672 "", /* This node has no interface. */
1676 /* Only display ip forwarding is enabled or not. */
1677 DEFUN (show_ip_forwarding
,
1678 show_ip_forwarding_cmd
,
1679 "show ip forwarding",
1682 "IP forwarding status\n")
1689 vty_out (vty
, "IP forwarding is off%s", VTY_NEWLINE
);
1691 vty_out (vty
, "IP forwarding is on%s", VTY_NEWLINE
);
1696 /* Only display ipv6 forwarding is enabled or not. */
1697 DEFUN (show_ipv6_forwarding
,
1698 show_ipv6_forwarding_cmd
,
1699 "show ipv6 forwarding",
1701 "IPv6 information\n"
1702 "Forwarding status\n")
1706 ret
= ipforward_ipv6 ();
1711 vty_out (vty
, "ipv6 forwarding is unknown%s", VTY_NEWLINE
);
1714 vty_out (vty
, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE
);
1717 vty_out (vty
, "ipv6 forwarding is %s%s", "on", VTY_NEWLINE
);
1720 vty_out (vty
, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE
);
1726 DEFUN (no_ipv6_forwarding
,
1727 no_ipv6_forwarding_cmd
,
1728 "no ipv6 forwarding",
1731 "Doesn't forward IPv6 protocol packet")
1735 ret
= ipforward_ipv6_off ();
1738 vty_out (vty
, "Can't turn off IPv6 forwarding%s", VTY_NEWLINE
);
1745 #endif /* HAVE_IPV6 */
1747 /* IPForwarding configuration write function. */
1749 config_write_forwarding (struct vty
*vty
)
1752 vty_out (vty
, "no ip forwarding%s", VTY_NEWLINE
);
1754 if (! ipforward_ipv6 ())
1755 vty_out (vty
, "no ipv6 forwarding%s", VTY_NEWLINE
);
1756 #endif /* HAVE_IPV6 */
1757 vty_out (vty
, "!%s", VTY_NEWLINE
);
1761 /* table node for routing tables. */
1762 struct cmd_node forwarding_node
=
1765 "", /* This node has no interface. */
1770 /* Initialisation of zebra and installation of commands. */
1774 /* Client list init. */
1775 client_list
= list_new ();
1777 /* Forwarding is on by default. */
1780 ipforward_ipv6_on ();
1781 #endif /* HAVE_IPV6 */
1783 /* Make zebra server socket. */
1784 #ifdef HAVE_TCP_ZEBRA
1787 zebra_serv_un (ZEBRA_SERV_PATH
);
1788 #endif /* HAVE_TCP_ZEBRA */
1790 /* Install configuration write function. */
1791 install_node (&table_node
, config_write_table
);
1792 install_node (&forwarding_node
, config_write_forwarding
);
1794 install_element (VIEW_NODE
, &show_ip_forwarding_cmd
);
1795 install_element (ENABLE_NODE
, &show_ip_forwarding_cmd
);
1796 install_element (CONFIG_NODE
, &no_ip_forwarding_cmd
);
1797 install_element (ENABLE_NODE
, &show_zebra_client_cmd
);
1800 install_element (VIEW_NODE
, &show_table_cmd
);
1801 install_element (ENABLE_NODE
, &show_table_cmd
);
1802 install_element (CONFIG_NODE
, &config_table_cmd
);
1803 #endif /* HAVE_NETLINK */
1806 install_element (VIEW_NODE
, &show_ipv6_forwarding_cmd
);
1807 install_element (ENABLE_NODE
, &show_ipv6_forwarding_cmd
);
1808 install_element (CONFIG_NODE
, &no_ipv6_forwarding_cmd
);
1809 #endif /* HAVE_IPV6 */