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"
40 #include "zebra/zserv.h"
41 #include "zebra/router-id.h"
42 #include "zebra/redistribute.h"
43 #include "zebra/debug.h"
44 #include "zebra/ipforward.h"
46 /* Event list of zebra. */
47 enum event
{ ZEBRA_SERV
, ZEBRA_READ
, ZEBRA_WRITE
};
49 extern struct zebra_t zebrad
;
51 static void zebra_event (enum event event
, int sock
, struct zserv
*client
);
53 extern struct zebra_privs_t zserv_privs
;
55 /* For logging of zebra meesages. */
56 static const char *zebra_command_str
[] =
59 "ZEBRA_INTERFACE_ADD",
60 "ZEBRA_INTERFACE_DELETE",
61 "ZEBRA_INTERFACE_ADDRESS_ADD",
62 "ZEBRA_INTERFACE_ADDRESS_DELETE",
64 "ZEBRA_INTERFACE_DOWN",
65 "ZEBRA_IPV4_ROUTE_ADD",
66 "ZEBRA_IPV4_ROUTE_DELETE",
67 "ZEBRA_IPV6_ROUTE_ADD",
68 "ZEBRA_IPV6_ROUTE_DELETE",
69 "ZEBRA_REDISTRIBUTE_ADD",
70 "ZEBRA_REDISTRIBUTE_DELETE",
71 "ZEBRA_REDISTRIBUTE_DEFAULT_ADD",
72 "ZEBRA_REDISTRIBUTE_DEFAULT_DELETE",
73 "ZEBRA_IPV4_NEXTHOP_LOOKUP",
74 "ZEBRA_IPV6_NEXTHOP_LOOKUP",
75 "ZEBRA_IPV4_IMPORT_LOOKUP",
76 "ZEBRA_IPV6_IMPORT_LOOKUP",
77 "ZEBRA_ROUTER_ID_ADD",
78 "ZEBRA_ROUTER_ID_DELETE",
79 "ZEBRA_ROUTER_ID_UPDATE"
83 static void zebra_client_close (struct zserv
*client
);
86 zserv_delayed_close(struct thread
*thread
)
88 struct zserv
*client
= THREAD_ARG(thread
);
90 client
->t_suicide
= NULL
;
91 zebra_client_close(client
);
96 zserv_flush_data(struct thread
*thread
)
98 struct zserv
*client
= THREAD_ARG(thread
);
100 client
->t_write
= NULL
;
101 if (client
->t_suicide
)
103 zebra_client_close(client
);
106 switch (buffer_flush_available(client
->wb
, client
->sock
))
109 zlog_warn("%s: buffer_flush_available failed on zserv client fd %d, "
110 "closing", __func__
, client
->sock
);
111 zebra_client_close(client
);
114 client
->t_write
= thread_add_write(zebrad
.master
, zserv_flush_data
,
115 client
, client
->sock
);
124 zebra_server_send_message(struct zserv
*client
)
126 if (client
->t_suicide
)
128 switch (buffer_write(client
->wb
, client
->sock
, STREAM_DATA(client
->obuf
),
129 stream_get_endp(client
->obuf
)))
132 zlog_warn("%s: buffer_write failed to zserv client fd %d, closing",
133 __func__
, client
->sock
);
134 /* Schedule a delayed close since many of the functions that call this
135 one do not check the return code. They do not allow for the
136 possibility that an I/O error may have caused the client to be
138 client
->t_suicide
= thread_add_event(zebrad
.master
, zserv_delayed_close
,
143 THREAD_OFF(client
->t_write
);
146 THREAD_WRITE_ON(zebrad
.master
, client
->t_write
,
147 zserv_flush_data
, client
, client
->sock
);
153 /* Interface is added. Send ZEBRA_INTERFACE_ADD to client. */
155 * This function is called in the following situations:
156 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
158 * - at startup, when zebra figures out the available interfaces
159 * - when an interface is added (where support for
160 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
161 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
165 zsend_interface_add (struct zserv
*client
, struct interface
*ifp
)
169 /* Check this client need interface information. */
170 if (! client
->ifinfo
)
176 /* Place holder for size. */
180 stream_putc (s
, ZEBRA_INTERFACE_ADD
);
182 /* Interface information. */
183 stream_put (s
, ifp
->name
, INTERFACE_NAMSIZ
);
184 stream_putl (s
, ifp
->ifindex
);
185 stream_putc (s
, ifp
->status
);
186 stream_putq (s
, ifp
->flags
);
187 stream_putl (s
, ifp
->metric
);
188 stream_putl (s
, ifp
->mtu
);
189 stream_putl (s
, ifp
->mtu6
);
190 stream_putl (s
, ifp
->bandwidth
);
191 #ifdef HAVE_SOCKADDR_DL
192 stream_put (s
, &ifp
->sdl
, sizeof (ifp
->sdl
));
194 stream_putl (s
, ifp
->hw_addr_len
);
195 if (ifp
->hw_addr_len
)
196 stream_put (s
, ifp
->hw_addr
, ifp
->hw_addr_len
);
197 #endif /* HAVE_SOCKADDR_DL */
199 /* Write packet size. */
200 stream_putw_at (s
, 0, stream_get_endp (s
));
202 return zebra_server_send_message(client
);
205 /* Interface deletion from zebra daemon. */
207 zsend_interface_delete (struct zserv
*client
, struct interface
*ifp
)
211 /* Check this client need interface information. */
212 if (! client
->ifinfo
)
218 /* Packet length placeholder. */
221 /* Interface information. */
222 stream_putc (s
, ZEBRA_INTERFACE_DELETE
);
223 stream_put (s
, ifp
->name
, INTERFACE_NAMSIZ
);
224 stream_putl (s
, ifp
->ifindex
);
225 stream_putc (s
, ifp
->status
);
226 stream_putq (s
, ifp
->flags
);
227 stream_putl (s
, ifp
->metric
);
228 stream_putl (s
, ifp
->mtu
);
229 stream_putl (s
, ifp
->mtu6
);
230 stream_putl (s
, ifp
->bandwidth
);
232 /* Write packet length. */
233 stream_putw_at (s
, 0, stream_get_endp (s
));
235 return zebra_server_send_message (client
);
238 /* Interface address is added/deleted. Send ZEBRA_INTERFACE_ADDRESS_ADD or
239 * ZEBRA_INTERFACE_ADDRESS_DELETE to the client.
241 * A ZEBRA_INTERFACE_ADDRESS_ADD is sent in the following situations:
242 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
243 * from the client, after the ZEBRA_INTERFACE_ADD has been
244 * sent from zebra to the client
245 * - redistribute new address info to all clients in the following situations
246 * - at startup, when zebra figures out the available interfaces
247 * - when an interface is added (where support for
248 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
249 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
251 * - for the vty commands "ip address A.B.C.D/M [<secondary>|<label LINE>]"
252 * and "no bandwidth <1-10000000>", "ipv6 address X:X::X:X/M"
253 * - when an RTM_NEWADDR message is received from the kernel,
255 * The call tree that triggers ZEBRA_INTERFACE_ADDRESS_DELETE:
257 * zsend_interface_address(DELETE)
260 * zebra_interface_address_delete_update
262 * | | if_delete_update
264 * ip_address_uninstall connected_delete_ipv4
265 * [ipv6_addresss_uninstall] [connected_delete_ipv6]
268 * | RTM_NEWADDR on routing/netlink socket
271 * "no ip address A.B.C.D/M [label LINE]"
272 * "no ip address A.B.C.D/M secondary"
273 * ["no ipv6 address X:X::X:X/M"]
277 zsend_interface_address (int cmd
, struct zserv
*client
,
278 struct interface
*ifp
, struct connected
*ifc
)
284 /* Check this client need interface information. */
285 if (! client
->ifinfo
)
291 /* Place holder for size. */
294 stream_putc (s
, cmd
);
295 stream_putl (s
, ifp
->ifindex
);
297 /* Interface address flag. */
298 stream_putc (s
, ifc
->flags
);
300 /* Prefix information. */
302 stream_putc (s
, p
->family
);
303 blen
= prefix_blen (p
);
304 stream_put (s
, &p
->u
.prefix
, blen
);
307 * XXX gnu version does not send prefixlen for ZEBRA_INTERFACE_ADDRESS_DELETE
308 * but zebra_interface_address_delete_read() in the gnu version
311 stream_putc (s
, p
->prefixlen
);
314 p
= ifc
->destination
;
316 stream_put (s
, &p
->u
.prefix
, blen
);
318 stream_put (s
, NULL
, blen
);
320 /* Write packet size. */
321 stream_putw_at (s
, 0, stream_get_endp (s
));
323 return zebra_server_send_message(client
);
327 * The cmd passed to zsend_interface_update may be ZEBRA_INTERFACE_UP or
328 * ZEBRA_INTERFACE_DOWN.
330 * The ZEBRA_INTERFACE_UP message is sent from the zebra server to
331 * the clients in one of 2 situations:
332 * - an if_up is detected e.g., as a result of an RTM_IFINFO message
333 * - a vty command modifying the bandwidth of an interface is received.
334 * The ZEBRA_INTERFACE_DOWN message is sent when an if_down is detected.
337 zsend_interface_update (int cmd
, struct zserv
*client
, struct interface
*ifp
)
341 /* Check this client need interface information. */
342 if (! client
->ifinfo
)
348 /* Place holder for size. */
352 stream_putc (s
, cmd
);
354 /* Interface information. */
355 stream_put (s
, ifp
->name
, INTERFACE_NAMSIZ
);
356 stream_putl (s
, ifp
->ifindex
);
357 stream_putc (s
, ifp
->status
);
358 stream_putq (s
, ifp
->flags
);
359 stream_putl (s
, ifp
->metric
);
360 stream_putl (s
, ifp
->mtu
);
361 stream_putl (s
, ifp
->mtu6
);
362 stream_putl (s
, ifp
->bandwidth
);
364 /* Write packet size. */
365 stream_putw_at (s
, 0, stream_get_endp (s
));
367 return zebra_server_send_message(client
);
371 * The zebra server sends the clients a ZEBRA_IPV4_ROUTE_ADD or a
372 * ZEBRA_IPV6_ROUTE_ADD via zsend_route_multipath in the following
374 * - when the client starts up, and requests default information
375 * by sending a ZEBRA_REDISTRIBUTE_DEFAULT_ADD to the zebra server, in the
376 * - case of rip, ripngd, ospfd and ospf6d, when the client sends a
377 * ZEBRA_REDISTRIBUTE_ADD as a result of the "redistribute" vty cmd,
378 * - when the zebra server redistributes routes after it updates its rib
380 * The zebra server sends clients a ZEBRA_IPV4_ROUTE_DELETE or a
381 * ZEBRA_IPV6_ROUTE_DELETE via zsend_route_multipath when:
382 * - a "ip route" or "ipv6 route" vty command is issued, a prefix is
383 * - deleted from zebra's rib, and this info
384 * has to be redistributed to the clients
386 * XXX The ZEBRA_IPV*_ROUTE_ADD message is also sent by the client to the
387 * zebra server when the client wants to tell the zebra server to add a
388 * route to the kernel (zapi_ipv4_add etc. ). Since it's essentially the
389 * same message being sent back and forth, this function and
390 * zapi_ipv{4,6}_{add, delete} should be re-written to avoid code
394 zsend_route_multipath (int cmd
, struct zserv
*client
, struct prefix
*p
,
399 struct nexthop
*nexthop
;
400 unsigned long nhnummark
= 0, messmark
= 0;
402 u_char zapi_flags
= 0;
407 /* Place holder for size. */
410 /* Put command, type and nexthop. */
411 stream_putc (s
, cmd
);
412 stream_putc (s
, rib
->type
);
413 stream_putc (s
, rib
->flags
);
415 /* marker for message flags field */
416 messmark
= stream_get_endp (s
);
420 psize
= PSIZE (p
->prefixlen
);
421 stream_putc (s
, p
->prefixlen
);
422 stream_write (s
, (u_char
*) & p
->u
.prefix
, psize
);
425 * XXX The message format sent by zebra below does not match the format
426 * of the corresponding message expected by the zebra server
427 * itself (e.g., see zread_ipv4_add). The nexthop_num is not set correctly,
428 * (is there a bug on the client side if more than one segment is sent?)
429 * nexthop ZEBRA_NEXTHOP_IPV4 is never set, ZEBRA_NEXTHOP_IFINDEX
434 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
436 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
438 SET_FLAG (zapi_flags
, ZAPI_MESSAGE_NEXTHOP
);
439 SET_FLAG (zapi_flags
, ZAPI_MESSAGE_IFINDEX
);
443 nhnummark
= stream_get_endp (s
);
444 stream_putc (s
, 1); /* placeholder */
449 switch(nexthop
->type
)
451 case NEXTHOP_TYPE_IPV4
:
452 case NEXTHOP_TYPE_IPV4_IFINDEX
:
453 stream_put_in_addr (s
, &nexthop
->gate
.ipv4
);
456 case NEXTHOP_TYPE_IPV6
:
457 case NEXTHOP_TYPE_IPV6_IFINDEX
:
458 case NEXTHOP_TYPE_IPV6_IFNAME
:
459 stream_write (s
, (u_char
*) &nexthop
->gate
.ipv6
, 16);
463 if (cmd
== ZEBRA_IPV4_ROUTE_ADD
464 || cmd
== ZEBRA_IPV4_ROUTE_DELETE
)
466 struct in_addr empty
;
467 memset (&empty
, 0, sizeof (struct in_addr
));
468 stream_write (s
, (u_char
*) &empty
, IPV4_MAX_BYTELEN
);
472 struct in6_addr empty
;
473 memset (&empty
, 0, sizeof (struct in6_addr
));
474 stream_write (s
, (u_char
*) &empty
, IPV6_MAX_BYTELEN
);
478 /* Interface index. */
480 stream_putl (s
, nexthop
->ifindex
);
487 if (cmd
== ZEBRA_IPV4_ROUTE_ADD
|| ZEBRA_IPV6_ROUTE_ADD
)
489 SET_FLAG (zapi_flags
, ZAPI_MESSAGE_DISTANCE
);
490 stream_putc (s
, rib
->distance
);
491 SET_FLAG (zapi_flags
, ZAPI_MESSAGE_METRIC
);
492 stream_putl (s
, rib
->metric
);
495 /* write real message flags value */
496 stream_putc_at (s
, messmark
, zapi_flags
);
498 /* Write next-hop number */
500 stream_putc_at (s
, nhnummark
, nhnum
);
502 /* Write packet size. */
503 stream_putw_at (s
, 0, stream_get_endp (s
));
505 return zebra_server_send_message(client
);
510 zsend_ipv6_nexthop_lookup (struct zserv
*client
, struct in6_addr
*addr
)
516 struct nexthop
*nexthop
;
518 /* Lookup nexthop. */
519 rib
= rib_match_ipv6 (addr
);
521 /* Get output stream. */
525 /* Fill in result. */
527 stream_putc (s
, ZEBRA_IPV6_NEXTHOP_LOOKUP
);
528 stream_put (s
, &addr
, 16);
532 stream_putl (s
, rib
->metric
);
534 nump
= stream_get_endp(s
);
536 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
537 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
539 stream_putc (s
, nexthop
->type
);
540 switch (nexthop
->type
)
542 case ZEBRA_NEXTHOP_IPV6
:
543 stream_put (s
, &nexthop
->gate
.ipv6
, 16);
545 case ZEBRA_NEXTHOP_IPV6_IFINDEX
:
546 case ZEBRA_NEXTHOP_IPV6_IFNAME
:
547 stream_put (s
, &nexthop
->gate
.ipv6
, 16);
548 stream_putl (s
, nexthop
->ifindex
);
550 case ZEBRA_NEXTHOP_IFINDEX
:
551 case ZEBRA_NEXTHOP_IFNAME
:
552 stream_putl (s
, nexthop
->ifindex
);
560 stream_putc_at (s
, nump
, num
);
568 stream_putw_at (s
, 0, stream_get_endp (s
));
570 return zebra_server_send_message(client
);
572 #endif /* HAVE_IPV6 */
575 zsend_ipv4_nexthop_lookup (struct zserv
*client
, struct in_addr addr
)
581 struct nexthop
*nexthop
;
583 /* Lookup nexthop. */
584 rib
= rib_match_ipv4 (addr
);
586 /* Get output stream. */
590 /* Fill in result. */
592 stream_putc (s
, ZEBRA_IPV4_NEXTHOP_LOOKUP
);
593 stream_put_in_addr (s
, &addr
);
597 stream_putl (s
, rib
->metric
);
599 nump
= stream_get_endp(s
);
601 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
602 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
604 stream_putc (s
, nexthop
->type
);
605 switch (nexthop
->type
)
607 case ZEBRA_NEXTHOP_IPV4
:
608 stream_put_in_addr (s
, &nexthop
->gate
.ipv4
);
610 case ZEBRA_NEXTHOP_IFINDEX
:
611 case ZEBRA_NEXTHOP_IFNAME
:
612 stream_putl (s
, nexthop
->ifindex
);
620 stream_putc_at (s
, nump
, num
);
628 stream_putw_at (s
, 0, stream_get_endp (s
));
630 return zebra_server_send_message(client
);
634 zsend_ipv4_import_lookup (struct zserv
*client
, struct prefix_ipv4
*p
)
640 struct nexthop
*nexthop
;
642 /* Lookup nexthop. */
643 rib
= rib_lookup_ipv4 (p
);
645 /* Get output stream. */
649 /* Fill in result. */
651 stream_putc (s
, ZEBRA_IPV4_IMPORT_LOOKUP
);
652 stream_put_in_addr (s
, &p
->prefix
);
656 stream_putl (s
, rib
->metric
);
658 nump
= stream_get_endp(s
);
660 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
661 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
663 stream_putc (s
, nexthop
->type
);
664 switch (nexthop
->type
)
666 case ZEBRA_NEXTHOP_IPV4
:
667 stream_put_in_addr (s
, &nexthop
->gate
.ipv4
);
669 case ZEBRA_NEXTHOP_IFINDEX
:
670 case ZEBRA_NEXTHOP_IFNAME
:
671 stream_putl (s
, nexthop
->ifindex
);
679 stream_putc_at (s
, nump
, num
);
687 stream_putw_at (s
, 0, stream_get_endp (s
));
689 return zebra_server_send_message(client
);
692 /* Router-id is updated. Send ZEBRA_ROUTER_ID_ADD to client. */
694 zsend_router_id_update (struct zserv
*client
, struct prefix
*p
)
699 /* Check this client need interface information. */
700 if (!client
->ridinfo
)
706 /* Place holder for size. */
710 stream_putc (s
, ZEBRA_ROUTER_ID_UPDATE
);
712 /* Prefix information. */
713 stream_putc (s
, p
->family
);
714 blen
= prefix_blen (p
);
715 stream_put (s
, &p
->u
.prefix
, blen
);
716 stream_putc (s
, p
->prefixlen
);
718 /* Write packet size. */
719 stream_putw_at (s
, 0, stream_get_endp (s
));
721 return zebra_server_send_message(client
);
724 /* Register zebra server interface information. Send current all
725 interface and address information. */
727 zread_interface_add (struct zserv
*client
, u_short length
)
729 struct listnode
*ifnode
, *ifnnode
;
730 struct listnode
*cnode
, *cnnode
;
731 struct interface
*ifp
;
734 /* Interface information is needed. */
737 for (ALL_LIST_ELEMENTS (iflist
, ifnode
, ifnnode
, ifp
))
739 /* Skip pseudo interface. */
740 if (! CHECK_FLAG (ifp
->status
, ZEBRA_INTERFACE_ACTIVE
))
743 if (zsend_interface_add (client
, ifp
) < 0)
746 for (ALL_LIST_ELEMENTS (ifp
->connected
, cnode
, cnnode
, c
))
748 if (CHECK_FLAG (c
->conf
, ZEBRA_IFC_REAL
) &&
749 (zsend_interface_address (ZEBRA_INTERFACE_ADDRESS_ADD
, client
,
757 /* Unregister zebra server interface information. */
759 zread_interface_delete (struct zserv
*client
, u_short length
)
765 /* This function support multiple nexthop. */
767 * Parse the ZEBRA_IPV4_ROUTE_ADD sent from client. Update rib and
771 zread_ipv4_add (struct zserv
*client
, u_short length
)
775 struct prefix_ipv4 p
;
777 struct in_addr nexthop
;
781 unsigned int ifindex
;
784 /* Get input stream. */
787 /* Allocate new rib. */
788 rib
= XCALLOC (MTYPE_RIB
, sizeof (struct rib
));
790 /* Type, flags, message. */
791 rib
->type
= stream_getc (s
);
792 rib
->flags
= stream_getc (s
);
793 message
= stream_getc (s
);
794 rib
->uptime
= time (NULL
);
797 memset (&p
, 0, sizeof (struct prefix_ipv4
));
799 p
.prefixlen
= stream_getc (s
);
800 stream_get (&p
.prefix
, s
, PSIZE (p
.prefixlen
));
803 if (CHECK_FLAG (message
, ZAPI_MESSAGE_NEXTHOP
))
805 nexthop_num
= stream_getc (s
);
807 for (i
= 0; i
< nexthop_num
; i
++)
809 nexthop_type
= stream_getc (s
);
811 switch (nexthop_type
)
813 case ZEBRA_NEXTHOP_IFINDEX
:
814 ifindex
= stream_getl (s
);
815 nexthop_ifindex_add (rib
, ifindex
);
817 case ZEBRA_NEXTHOP_IFNAME
:
818 ifname_len
= stream_getc (s
);
819 stream_forward_getp (s
, ifname_len
);
821 case ZEBRA_NEXTHOP_IPV4
:
822 nexthop
.s_addr
= stream_get_ipv4 (s
);
823 nexthop_ipv4_add (rib
, &nexthop
);
825 case ZEBRA_NEXTHOP_IPV6
:
826 stream_forward_getp (s
, IPV6_MAX_BYTELEN
);
828 case ZEBRA_NEXTHOP_BLACKHOLE
:
829 nexthop_blackhole_add (rib
);
836 if (CHECK_FLAG (message
, ZAPI_MESSAGE_DISTANCE
))
837 rib
->distance
= stream_getc (s
);
840 if (CHECK_FLAG (message
, ZAPI_MESSAGE_METRIC
))
841 rib
->metric
= stream_getl (s
);
843 rib_add_ipv4_multipath (&p
, rib
);
847 /* Zebra server IPv4 prefix delete function. */
849 zread_ipv4_delete (struct zserv
*client
, u_short length
)
853 struct zapi_ipv4 api
;
854 struct in_addr nexthop
;
855 unsigned long ifindex
;
856 struct prefix_ipv4 p
;
865 /* Type, flags, message. */
866 api
.type
= stream_getc (s
);
867 api
.flags
= stream_getc (s
);
868 api
.message
= stream_getc (s
);
871 memset (&p
, 0, sizeof (struct prefix_ipv4
));
873 p
.prefixlen
= stream_getc (s
);
874 stream_get (&p
.prefix
, s
, PSIZE (p
.prefixlen
));
876 /* Nexthop, ifindex, distance, metric. */
877 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
))
879 nexthop_num
= stream_getc (s
);
881 for (i
= 0; i
< nexthop_num
; i
++)
883 nexthop_type
= stream_getc (s
);
885 switch (nexthop_type
)
887 case ZEBRA_NEXTHOP_IFINDEX
:
888 ifindex
= stream_getl (s
);
890 case ZEBRA_NEXTHOP_IFNAME
:
891 ifname_len
= stream_getc (s
);
892 stream_forward_getp (s
, ifname_len
);
894 case ZEBRA_NEXTHOP_IPV4
:
895 nexthop
.s_addr
= stream_get_ipv4 (s
);
897 case ZEBRA_NEXTHOP_IPV6
:
898 stream_forward_getp (s
, IPV6_MAX_BYTELEN
);
905 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_DISTANCE
))
906 api
.distance
= stream_getc (s
);
911 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_METRIC
))
912 api
.metric
= stream_getl (s
);
916 rib_delete_ipv4 (api
.type
, api
.flags
, &p
, &nexthop
, ifindex
,
921 /* Nexthop lookup for IPv4. */
923 zread_ipv4_nexthop_lookup (struct zserv
*client
, u_short length
)
927 addr
.s_addr
= stream_get_ipv4 (client
->ibuf
);
928 return zsend_ipv4_nexthop_lookup (client
, addr
);
931 /* Nexthop lookup for IPv4. */
933 zread_ipv4_import_lookup (struct zserv
*client
, u_short length
)
935 struct prefix_ipv4 p
;
938 p
.prefixlen
= stream_getc (client
->ibuf
);
939 p
.prefix
.s_addr
= stream_get_ipv4 (client
->ibuf
);
941 return zsend_ipv4_import_lookup (client
, &p
);
945 /* Zebra server IPv6 prefix add function. */
947 zread_ipv6_add (struct zserv
*client
, u_short length
)
951 struct zapi_ipv6 api
;
952 struct in6_addr nexthop
;
953 unsigned long ifindex
;
954 struct prefix_ipv6 p
;
958 memset (&nexthop
, 0, sizeof (struct in6_addr
));
960 /* Type, flags, message. */
961 api
.type
= stream_getc (s
);
962 api
.flags
= stream_getc (s
);
963 api
.message
= stream_getc (s
);
966 memset (&p
, 0, sizeof (struct prefix_ipv6
));
968 p
.prefixlen
= stream_getc (s
);
969 stream_get (&p
.prefix
, s
, PSIZE (p
.prefixlen
));
971 /* Nexthop, ifindex, distance, metric. */
972 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
))
976 api
.nexthop_num
= stream_getc (s
);
977 for (i
= 0; i
< api
.nexthop_num
; i
++)
979 nexthop_type
= stream_getc (s
);
981 switch (nexthop_type
)
983 case ZEBRA_NEXTHOP_IPV6
:
984 stream_get (&nexthop
, s
, 16);
986 case ZEBRA_NEXTHOP_IFINDEX
:
987 ifindex
= stream_getl (s
);
993 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_DISTANCE
))
994 api
.distance
= stream_getc (s
);
998 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_METRIC
))
999 api
.metric
= stream_getl (s
);
1003 if (IN6_IS_ADDR_UNSPECIFIED (&nexthop
))
1004 rib_add_ipv6 (api
.type
, api
.flags
, &p
, NULL
, ifindex
, 0, api
.metric
,
1007 rib_add_ipv6 (api
.type
, api
.flags
, &p
, &nexthop
, ifindex
, 0, api
.metric
,
1012 /* Zebra server IPv6 prefix delete function. */
1014 zread_ipv6_delete (struct zserv
*client
, u_short length
)
1018 struct zapi_ipv6 api
;
1019 struct in6_addr nexthop
;
1020 unsigned long ifindex
;
1021 struct prefix_ipv6 p
;
1025 memset (&nexthop
, 0, sizeof (struct in6_addr
));
1027 /* Type, flags, message. */
1028 api
.type
= stream_getc (s
);
1029 api
.flags
= stream_getc (s
);
1030 api
.message
= stream_getc (s
);
1033 memset (&p
, 0, sizeof (struct prefix_ipv6
));
1034 p
.family
= AF_INET6
;
1035 p
.prefixlen
= stream_getc (s
);
1036 stream_get (&p
.prefix
, s
, PSIZE (p
.prefixlen
));
1038 /* Nexthop, ifindex, distance, metric. */
1039 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
))
1041 u_char nexthop_type
;
1043 api
.nexthop_num
= stream_getc (s
);
1044 for (i
= 0; i
< api
.nexthop_num
; i
++)
1046 nexthop_type
= stream_getc (s
);
1048 switch (nexthop_type
)
1050 case ZEBRA_NEXTHOP_IPV6
:
1051 stream_get (&nexthop
, s
, 16);
1053 case ZEBRA_NEXTHOP_IFINDEX
:
1054 ifindex
= stream_getl (s
);
1060 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_DISTANCE
))
1061 api
.distance
= stream_getc (s
);
1064 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_METRIC
))
1065 api
.metric
= stream_getl (s
);
1069 if (IN6_IS_ADDR_UNSPECIFIED (&nexthop
))
1070 rib_delete_ipv6 (api
.type
, api
.flags
, &p
, NULL
, ifindex
, 0);
1072 rib_delete_ipv6 (api
.type
, api
.flags
, &p
, &nexthop
, ifindex
, 0);
1077 zread_ipv6_nexthop_lookup (struct zserv
*client
, u_short length
)
1079 struct in6_addr addr
;
1082 stream_get (&addr
, client
->ibuf
, 16);
1083 printf ("DEBUG %s\n", inet_ntop (AF_INET6
, &addr
, buf
, BUFSIZ
));
1085 return zsend_ipv6_nexthop_lookup (client
, &addr
);
1087 #endif /* HAVE_IPV6 */
1089 /* Register zebra server router-id information. Send current router-id */
1091 zread_router_id_add (struct zserv
*client
, u_short length
)
1095 /* Router-id information is needed. */
1096 client
->ridinfo
= 1;
1100 return zsend_router_id_update (client
,&p
);
1103 /* Unregister zebra server router-id information. */
1105 zread_router_id_delete (struct zserv
*client
, u_short length
)
1107 client
->ridinfo
= 0;
1111 /* Close zebra client. */
1113 zebra_client_close (struct zserv
*client
)
1115 /* Close file descriptor. */
1118 close (client
->sock
);
1122 /* Free stream buffers. */
1124 stream_free (client
->ibuf
);
1126 stream_free (client
->obuf
);
1128 buffer_free(client
->wb
);
1130 /* Release threads. */
1132 thread_cancel (client
->t_read
);
1133 if (client
->t_write
)
1134 thread_cancel (client
->t_write
);
1135 if (client
->t_suicide
)
1136 thread_cancel (client
->t_suicide
);
1138 /* Free client structure. */
1139 listnode_delete (zebrad
.client_list
, client
);
1143 /* Make new client. */
1145 zebra_client_create (int sock
)
1147 struct zserv
*client
;
1149 client
= XCALLOC (0, sizeof (struct zserv
));
1151 /* Make client input/output buffer. */
1152 client
->sock
= sock
;
1153 client
->ibuf
= stream_new (ZEBRA_MAX_PACKET_SIZ
);
1154 client
->obuf
= stream_new (ZEBRA_MAX_PACKET_SIZ
);
1155 client
->wb
= buffer_new(0);
1157 /* Set table number. */
1158 client
->rtm_table
= zebrad
.rtm_table_default
;
1160 /* Add this client to linked list. */
1161 listnode_add (zebrad
.client_list
, client
);
1163 /* Make new read thread. */
1164 zebra_event (ZEBRA_READ
, sock
, client
);
1167 /* Handler of zebra service request. */
1169 zebra_client_read (struct thread
*thread
)
1172 struct zserv
*client
;
1177 /* Get thread data. Reset reading thread because I'm running. */
1178 sock
= THREAD_FD (thread
);
1179 client
= THREAD_ARG (thread
);
1180 client
->t_read
= NULL
;
1182 if (client
->t_suicide
)
1184 zebra_client_close(client
);
1188 /* Read length and command (if we don't have it already). */
1189 if ((already
= stream_get_endp(client
->ibuf
)) < ZEBRA_HEADER_SIZE
)
1192 if (((nbyte
= stream_read_try (client
->ibuf
, sock
,
1193 ZEBRA_HEADER_SIZE
-already
)) == 0) ||
1196 if (IS_ZEBRA_DEBUG_EVENT
)
1197 zlog_debug ("connection closed socket [%d]", sock
);
1198 zebra_client_close (client
);
1201 if (nbyte
!= (ssize_t
)(ZEBRA_HEADER_SIZE
-already
))
1203 /* Try again later. */
1204 zebra_event (ZEBRA_READ
, sock
, client
);
1207 already
= ZEBRA_HEADER_SIZE
;
1210 /* Reset to read from the beginning of the incoming packet. */
1211 stream_set_getp(client
->ibuf
, 0);
1213 length
= stream_getw (client
->ibuf
);
1214 command
= stream_getc (client
->ibuf
);
1216 if (length
< ZEBRA_HEADER_SIZE
)
1218 zlog_warn("%s: socket %d message length %u is less than header size %d",
1219 __func__
, sock
, length
, ZEBRA_HEADER_SIZE
);
1220 zebra_client_close (client
);
1223 if (length
> STREAM_SIZE(client
->ibuf
))
1225 zlog_warn("%s: socket %d message length %u exceeds buffer size %lu",
1226 __func__
, sock
, length
, (u_long
)STREAM_SIZE(client
->ibuf
));
1227 zebra_client_close (client
);
1231 /* Read rest of data. */
1232 if (already
< length
)
1235 if (((nbyte
= stream_read_try (client
->ibuf
, sock
,
1236 length
-already
)) == 0) ||
1239 if (IS_ZEBRA_DEBUG_EVENT
)
1240 zlog_debug ("connection closed [%d] when reading zebra data", sock
);
1241 zebra_client_close (client
);
1244 if (nbyte
!= (ssize_t
)(length
-already
))
1246 /* Try again later. */
1247 zebra_event (ZEBRA_READ
, sock
, client
);
1252 length
-= ZEBRA_HEADER_SIZE
;
1254 /* Debug packet information. */
1255 if (IS_ZEBRA_DEBUG_EVENT
)
1256 zlog_debug ("zebra message comes from socket [%d]", sock
);
1258 if (IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
)
1259 zlog_debug ("zebra message received [%s] %d",
1260 zebra_command_str
[command
], length
);
1264 case ZEBRA_ROUTER_ID_ADD
:
1265 zread_router_id_add (client
, length
);
1267 case ZEBRA_ROUTER_ID_DELETE
:
1268 zread_router_id_delete (client
, length
);
1270 case ZEBRA_INTERFACE_ADD
:
1271 zread_interface_add (client
, length
);
1273 case ZEBRA_INTERFACE_DELETE
:
1274 zread_interface_delete (client
, length
);
1276 case ZEBRA_IPV4_ROUTE_ADD
:
1277 zread_ipv4_add (client
, length
);
1279 case ZEBRA_IPV4_ROUTE_DELETE
:
1280 zread_ipv4_delete (client
, length
);
1283 case ZEBRA_IPV6_ROUTE_ADD
:
1284 zread_ipv6_add (client
, length
);
1286 case ZEBRA_IPV6_ROUTE_DELETE
:
1287 zread_ipv6_delete (client
, length
);
1289 #endif /* HAVE_IPV6 */
1290 case ZEBRA_REDISTRIBUTE_ADD
:
1291 zebra_redistribute_add (command
, client
, length
);
1293 case ZEBRA_REDISTRIBUTE_DELETE
:
1294 zebra_redistribute_delete (command
, client
, length
);
1296 case ZEBRA_REDISTRIBUTE_DEFAULT_ADD
:
1297 zebra_redistribute_default_add (command
, client
, length
);
1299 case ZEBRA_REDISTRIBUTE_DEFAULT_DELETE
:
1300 zebra_redistribute_default_delete (command
, client
, length
);
1302 case ZEBRA_IPV4_NEXTHOP_LOOKUP
:
1303 zread_ipv4_nexthop_lookup (client
, length
);
1306 case ZEBRA_IPV6_NEXTHOP_LOOKUP
:
1307 zread_ipv6_nexthop_lookup (client
, length
);
1309 #endif /* HAVE_IPV6 */
1310 case ZEBRA_IPV4_IMPORT_LOOKUP
:
1311 zread_ipv4_import_lookup (client
, length
);
1314 zlog_info ("Zebra received unknown command %d", command
);
1318 if (client
->t_suicide
)
1320 /* No need to wait for thread callback, just kill immediately. */
1321 zebra_client_close(client
);
1325 stream_reset (client
->ibuf
);
1326 zebra_event (ZEBRA_READ
, sock
, client
);
1331 /* Accept code of zebra server socket. */
1333 zebra_accept (struct thread
*thread
)
1337 struct sockaddr_in client
;
1340 accept_sock
= THREAD_FD (thread
);
1342 /* Reregister myself. */
1343 zebra_event (ZEBRA_SERV
, accept_sock
, NULL
);
1345 len
= sizeof (struct sockaddr_in
);
1346 client_sock
= accept (accept_sock
, (struct sockaddr
*) &client
, &len
);
1348 if (client_sock
< 0)
1350 zlog_warn ("Can't accept zebra socket: %s", safe_strerror (errno
));
1354 /* Make client socket non-blocking. */
1355 set_nonblocking(client_sock
);
1357 /* Create new zebra client. */
1358 zebra_client_create (client_sock
);
1363 #ifdef HAVE_TCP_ZEBRA
1364 /* Make zebra's server socket. */
1370 struct sockaddr_in addr
;
1372 accept_sock
= socket (AF_INET
, SOCK_STREAM
, 0);
1374 if (accept_sock
< 0)
1376 zlog_warn ("Can't create zserv stream socket: %s",
1377 safe_strerror (errno
));
1378 zlog_warn ("zebra can't provice full functionality due to above error");
1382 memset (&addr
, 0, sizeof (struct sockaddr_in
));
1383 addr
.sin_family
= AF_INET
;
1384 addr
.sin_port
= htons (ZEBRA_PORT
);
1386 addr
.sin_len
= sizeof (struct sockaddr_in
);
1387 #endif /* HAVE_SIN_LEN */
1388 addr
.sin_addr
.s_addr
= htonl (INADDR_LOOPBACK
);
1390 sockopt_reuseaddr (accept_sock
);
1391 sockopt_reuseport (accept_sock
);
1393 if ( zserv_privs
.change(ZPRIVS_RAISE
) )
1394 zlog (NULL
, LOG_ERR
, "Can't raise privileges");
1396 ret
= bind (accept_sock
, (struct sockaddr
*)&addr
,
1397 sizeof (struct sockaddr_in
));
1400 zlog_warn ("Can't bind to stream socket: %s",
1401 safe_strerror (errno
));
1402 zlog_warn ("zebra can't provice full functionality due to above error");
1403 close (accept_sock
); /* Avoid sd leak. */
1407 if ( zserv_privs
.change(ZPRIVS_LOWER
) )
1408 zlog (NULL
, LOG_ERR
, "Can't lower privileges");
1410 ret
= listen (accept_sock
, 1);
1413 zlog_warn ("Can't listen to stream socket: %s",
1414 safe_strerror (errno
));
1415 zlog_warn ("zebra can't provice full functionality due to above error");
1416 close (accept_sock
); /* Avoid sd leak. */
1420 zebra_event (ZEBRA_SERV
, accept_sock
, NULL
);
1422 #endif /* HAVE_TCP_ZEBRA */
1424 /* For sockaddr_un. */
1427 /* zebra server UNIX domain socket. */
1429 zebra_serv_un (const char *path
)
1433 struct sockaddr_un serv
;
1436 /* First of all, unlink existing socket */
1440 old_mask
= umask (0077);
1442 /* Make UNIX domain socket. */
1443 sock
= socket (AF_UNIX
, SOCK_STREAM
, 0);
1446 zlog_warn ("Can't create zserv unix socket: %s",
1447 safe_strerror (errno
));
1448 zlog_warn ("zebra can't provide full functionality due to above error");
1452 /* Make server socket. */
1453 memset (&serv
, 0, sizeof (struct sockaddr_un
));
1454 serv
.sun_family
= AF_UNIX
;
1455 strncpy (serv
.sun_path
, path
, strlen (path
));
1457 len
= serv
.sun_len
= SUN_LEN(&serv
);
1459 len
= sizeof (serv
.sun_family
) + strlen (serv
.sun_path
);
1460 #endif /* HAVE_SUN_LEN */
1462 ret
= bind (sock
, (struct sockaddr
*) &serv
, len
);
1465 zlog_warn ("Can't bind to unix socket %s: %s",
1466 path
, safe_strerror (errno
));
1467 zlog_warn ("zebra can't provide full functionality due to above error");
1472 ret
= listen (sock
, 5);
1475 zlog_warn ("Can't listen to unix socket %s: %s",
1476 path
, safe_strerror (errno
));
1477 zlog_warn ("zebra can't provide full functionality due to above error");
1484 zebra_event (ZEBRA_SERV
, sock
, NULL
);
1489 zebra_event (enum event event
, int sock
, struct zserv
*client
)
1494 thread_add_read (zebrad
.master
, zebra_accept
, client
, sock
);
1498 thread_add_read (zebrad
.master
, zebra_client_read
, client
, sock
);
1506 /* Display default rtm_table for all clients. */
1511 "default routing table to use for all clients\n")
1513 vty_out (vty
, "table %d%s", zebrad
.rtm_table_default
,
1518 DEFUN (config_table
,
1521 "Configure target kernel routing table\n"
1524 zebrad
.rtm_table_default
= strtol (argv
[0], (char**)0, 10);
1528 DEFUN (ip_forwarding
,
1532 "Turn on IP forwarding")
1538 ret
= ipforward_on ();
1542 vty_out (vty
, "Can't turn on IP forwarding%s", VTY_NEWLINE
);
1549 DEFUN (no_ip_forwarding
,
1550 no_ip_forwarding_cmd
,
1554 "Turn off IP forwarding")
1560 ret
= ipforward_off ();
1564 vty_out (vty
, "Can't turn off IP forwarding%s", VTY_NEWLINE
);
1571 /* This command is for debugging purpose. */
1572 DEFUN (show_zebra_client
,
1573 show_zebra_client_cmd
,
1574 "show zebra client",
1577 "Client information")
1579 struct listnode
*node
;
1580 struct zserv
*client
;
1582 for (ALL_LIST_ELEMENTS_RO (zebrad
.client_list
, node
, client
))
1583 vty_out (vty
, "Client fd %d%s", client
->sock
, VTY_NEWLINE
);
1588 /* Table configuration write function. */
1590 config_write_table (struct vty
*vty
)
1592 if (zebrad
.rtm_table_default
)
1593 vty_out (vty
, "table %d%s", zebrad
.rtm_table_default
,
1598 /* table node for routing tables. */
1599 struct cmd_node table_node
=
1602 "", /* This node has no interface. */
1606 /* Only display ip forwarding is enabled or not. */
1607 DEFUN (show_ip_forwarding
,
1608 show_ip_forwarding_cmd
,
1609 "show ip forwarding",
1612 "IP forwarding status\n")
1619 vty_out (vty
, "IP forwarding is off%s", VTY_NEWLINE
);
1621 vty_out (vty
, "IP forwarding is on%s", VTY_NEWLINE
);
1626 /* Only display ipv6 forwarding is enabled or not. */
1627 DEFUN (show_ipv6_forwarding
,
1628 show_ipv6_forwarding_cmd
,
1629 "show ipv6 forwarding",
1631 "IPv6 information\n"
1632 "Forwarding status\n")
1636 ret
= ipforward_ipv6 ();
1641 vty_out (vty
, "ipv6 forwarding is unknown%s", VTY_NEWLINE
);
1644 vty_out (vty
, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE
);
1647 vty_out (vty
, "ipv6 forwarding is %s%s", "on", VTY_NEWLINE
);
1650 vty_out (vty
, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE
);
1656 DEFUN (ipv6_forwarding
,
1657 ipv6_forwarding_cmd
,
1660 "Turn on IPv6 forwarding")
1664 ret
= ipforward_ipv6 ();
1666 ret
= ipforward_ipv6_on ();
1670 vty_out (vty
, "Can't turn on IPv6 forwarding%s", VTY_NEWLINE
);
1677 DEFUN (no_ipv6_forwarding
,
1678 no_ipv6_forwarding_cmd
,
1679 "no ipv6 forwarding",
1682 "Turn off IPv6 forwarding")
1686 ret
= ipforward_ipv6 ();
1688 ret
= ipforward_ipv6_off ();
1692 vty_out (vty
, "Can't turn off IPv6 forwarding%s", VTY_NEWLINE
);
1699 #endif /* HAVE_IPV6 */
1701 /* IPForwarding configuration write function. */
1703 config_write_forwarding (struct vty
*vty
)
1705 /* FIXME: Find better place for that. */
1706 router_id_write (vty
);
1709 vty_out (vty
, "ip forwarding%s", VTY_NEWLINE
);
1711 if (ipforward_ipv6 ())
1712 vty_out (vty
, "ipv6 forwarding%s", VTY_NEWLINE
);
1713 #endif /* HAVE_IPV6 */
1714 vty_out (vty
, "!%s", VTY_NEWLINE
);
1718 /* table node for routing tables. */
1719 struct cmd_node forwarding_node
=
1722 "", /* This node has no interface. */
1727 /* Initialisation of zebra and installation of commands. */
1731 /* Client list init. */
1732 zebrad
.client_list
= list_new ();
1734 /* Make zebra server socket. */
1735 #ifdef HAVE_TCP_ZEBRA
1738 zebra_serv_un (ZEBRA_SERV_PATH
);
1739 #endif /* HAVE_TCP_ZEBRA */
1741 /* Install configuration write function. */
1742 install_node (&table_node
, config_write_table
);
1743 install_node (&forwarding_node
, config_write_forwarding
);
1745 install_element (VIEW_NODE
, &show_ip_forwarding_cmd
);
1746 install_element (ENABLE_NODE
, &show_ip_forwarding_cmd
);
1747 install_element (CONFIG_NODE
, &ip_forwarding_cmd
);
1748 install_element (CONFIG_NODE
, &no_ip_forwarding_cmd
);
1749 install_element (ENABLE_NODE
, &show_zebra_client_cmd
);
1752 install_element (VIEW_NODE
, &show_table_cmd
);
1753 install_element (ENABLE_NODE
, &show_table_cmd
);
1754 install_element (CONFIG_NODE
, &config_table_cmd
);
1755 #endif /* HAVE_NETLINK */
1758 install_element (VIEW_NODE
, &show_ipv6_forwarding_cmd
);
1759 install_element (ENABLE_NODE
, &show_ipv6_forwarding_cmd
);
1760 install_element (CONFIG_NODE
, &ipv6_forwarding_cmd
);
1761 install_element (CONFIG_NODE
, &no_ipv6_forwarding_cmd
);
1762 #endif /* HAVE_IPV6 */