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_putl (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 * This function is only called when support for
208 * RTM_IFANNOUNCE or AF_NETLINK sockets (RTM_DELLINK message)
209 * is available. It is not called on Solaris.
211 #if (defined(RTM_IFANNOUNCE) || defined(HAVE_NETLINK))
213 zsend_interface_delete (struct zserv
*client
, struct interface
*ifp
)
217 /* Check this client need interface information. */
218 if (! client
->ifinfo
)
224 /* Packet length placeholder. */
227 /* Interface information. */
228 stream_putc (s
, ZEBRA_INTERFACE_DELETE
);
229 stream_put (s
, ifp
->name
, INTERFACE_NAMSIZ
);
230 stream_putl (s
, ifp
->ifindex
);
231 stream_putc (s
, ifp
->status
);
232 stream_putl (s
, ifp
->flags
);
233 stream_putl (s
, ifp
->metric
);
234 stream_putl (s
, ifp
->mtu
);
235 stream_putl (s
, ifp
->mtu6
);
236 stream_putl (s
, ifp
->bandwidth
);
238 /* Write packet length. */
239 stream_putw_at (s
, 0, stream_get_endp (s
));
241 return zebra_server_send_message (client
);
243 #endif /* (defined(RTM_IFANNOUNCE) || defined(HAVE_LINUX_RTNETLINK_H)) */
245 /* Interface address is added/deleted. Send ZEBRA_INTERFACE_ADDRESS_ADD or
246 * ZEBRA_INTERFACE_ADDRESS_DELETE to the client.
248 * A ZEBRA_INTERFACE_ADDRESS_ADD is sent in the following situations:
249 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
250 * from the client, after the ZEBRA_INTERFACE_ADD has been
251 * sent from zebra to the client
252 * - redistribute new address info to all clients in the following situations
253 * - at startup, when zebra figures out the available interfaces
254 * - when an interface is added (where support for
255 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
256 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
258 * - for the vty commands "ip address A.B.C.D/M [<secondary>|<label LINE>]"
259 * and "no bandwidth <1-10000000>", "ipv6 address X:X::X:X/M"
260 * - when an RTM_NEWADDR message is received from the kernel,
262 * The call tree that triggers ZEBRA_INTERFACE_ADDRESS_DELETE:
264 * zsend_interface_address(DELETE)
267 * zebra_interface_address_delete_update
269 * | | if_delete_update (not called on
271 * ip_address_uninstall connected_delete_ipv4
272 * [ipv6_addresss_uninstall] [connected_delete_ipv6]
275 * | RTM_NEWADDR on routing/netlink socket
278 * "no ip address A.B.C.D/M [label LINE]"
279 * "no ip address A.B.C.D/M secondary"
280 * ["no ipv6 address X:X::X:X/M"]
284 zsend_interface_address (int cmd
, struct zserv
*client
,
285 struct interface
*ifp
, struct connected
*ifc
)
291 /* Check this client need interface information. */
292 if (! client
->ifinfo
)
298 /* Place holder for size. */
301 stream_putc (s
, cmd
);
302 stream_putl (s
, ifp
->ifindex
);
304 /* Interface address flag. */
305 stream_putc (s
, ifc
->flags
);
307 /* Prefix information. */
309 stream_putc (s
, p
->family
);
310 blen
= prefix_blen (p
);
311 stream_put (s
, &p
->u
.prefix
, blen
);
314 * XXX gnu version does not send prefixlen for ZEBRA_INTERFACE_ADDRESS_DELETE
315 * but zebra_interface_address_delete_read() in the gnu version
318 stream_putc (s
, p
->prefixlen
);
321 p
= ifc
->destination
;
323 stream_put (s
, &p
->u
.prefix
, blen
);
325 stream_put (s
, NULL
, blen
);
327 /* Write packet size. */
328 stream_putw_at (s
, 0, stream_get_endp (s
));
330 return zebra_server_send_message(client
);
334 * The cmd passed to zsend_interface_update may be ZEBRA_INTERFACE_UP or
335 * ZEBRA_INTERFACE_DOWN.
337 * The ZEBRA_INTERFACE_UP message is sent from the zebra server to
338 * the clients in one of 2 situations:
339 * - an if_up is detected e.g., as a result of an RTM_IFINFO message
340 * - a vty command modifying the bandwidth of an interface is received.
341 * The ZEBRA_INTERFACE_DOWN message is sent when an if_down is detected.
344 zsend_interface_update (int cmd
, struct zserv
*client
, struct interface
*ifp
)
348 /* Check this client need interface information. */
349 if (! client
->ifinfo
)
355 /* Place holder for size. */
359 stream_putc (s
, cmd
);
361 /* Interface information. */
362 stream_put (s
, ifp
->name
, INTERFACE_NAMSIZ
);
363 stream_putl (s
, ifp
->ifindex
);
364 stream_putc (s
, ifp
->status
);
365 stream_putl (s
, ifp
->flags
);
366 stream_putl (s
, ifp
->metric
);
367 stream_putl (s
, ifp
->mtu
);
368 stream_putl (s
, ifp
->mtu6
);
369 stream_putl (s
, ifp
->bandwidth
);
371 /* Write packet size. */
372 stream_putw_at (s
, 0, stream_get_endp (s
));
374 return zebra_server_send_message(client
);
378 * The zebra server sends the clients a ZEBRA_IPV4_ROUTE_ADD or a
379 * ZEBRA_IPV6_ROUTE_ADD via zsend_route_multipath in the following
381 * - when the client starts up, and requests default information
382 * by sending a ZEBRA_REDISTRIBUTE_DEFAULT_ADD to the zebra server, in the
383 * - case of rip, ripngd, ospfd and ospf6d, when the client sends a
384 * ZEBRA_REDISTRIBUTE_ADD as a result of the "redistribute" vty cmd,
385 * - when the zebra server redistributes routes after it updates its rib
387 * The zebra server sends clients a ZEBRA_IPV4_ROUTE_DELETE or a
388 * ZEBRA_IPV6_ROUTE_DELETE via zsend_route_multipath when:
389 * - a "ip route" or "ipv6 route" vty command is issued, a prefix is
390 * - deleted from zebra's rib, and this info
391 * has to be redistributed to the clients
393 * XXX The ZEBRA_IPV*_ROUTE_ADD message is also sent by the client to the
394 * zebra server when the client wants to tell the zebra server to add a
395 * route to the kernel (zapi_ipv4_add etc. ). Since it's essentially the
396 * same message being sent back and forth, this function and
397 * zapi_ipv{4,6}_{add, delete} should be re-written to avoid code
401 zsend_route_multipath (int cmd
, struct zserv
*client
, struct prefix
*p
,
406 struct nexthop
*nexthop
;
407 unsigned long nhnummark
= 0;
409 u_char zapi_flags
= ZAPI_MESSAGE_NEXTHOP
| ZAPI_MESSAGE_IFINDEX
;
414 /* Place holder for size. */
417 /* Put command, type and nexthop. */
418 stream_putc (s
, cmd
);
419 stream_putc (s
, rib
->type
);
420 stream_putc (s
, rib
->flags
);
423 * XXX no need to set ZAPI_MESSAGE_NEXTHOP if we are going to
424 * send empty nexthop?
426 if (cmd
== ZEBRA_IPV4_ROUTE_ADD
|| ZEBRA_IPV6_ROUTE_ADD
)
427 zapi_flags
|= ZAPI_MESSAGE_METRIC
;
429 stream_putc (s
, zapi_flags
);
432 psize
= PSIZE (p
->prefixlen
);
433 stream_putc (s
, p
->prefixlen
);
434 stream_write (s
, (u_char
*) & p
->u
.prefix
, psize
);
437 * XXX The message format sent by zebra below does not match the format
438 * of the corresponding message expected by the zebra server
439 * itself (e.g., see zread_ipv4_add). The nexthop_num is not set correctly,
440 * (is there a bug on the client side if more than one segment is sent?)
441 * nexthop ZEBRA_NEXTHOP_IPV4 is never set, ZEBRA_NEXTHOP_IFINDEX
445 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
447 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
449 nhnummark
= stream_get_endp (s
);
450 stream_putc (s
, 1); /* placeholder */
453 switch(nexthop
->type
)
455 case NEXTHOP_TYPE_IPV4
:
456 case NEXTHOP_TYPE_IPV4_IFINDEX
:
457 stream_put_in_addr (s
, &nexthop
->gate
.ipv4
);
460 case NEXTHOP_TYPE_IPV6
:
461 case NEXTHOP_TYPE_IPV6_IFINDEX
:
462 case NEXTHOP_TYPE_IPV6_IFNAME
:
463 stream_write (s
, (u_char
*) &nexthop
->gate
.ipv6
, 16);
467 if (cmd
== ZEBRA_IPV4_ROUTE_ADD
468 || cmd
== ZEBRA_IPV4_ROUTE_DELETE
)
470 struct in_addr empty
;
471 memset (&empty
, 0, sizeof (struct in_addr
));
472 stream_write (s
, (u_char
*) &empty
, IPV4_MAX_BYTELEN
);
476 struct in6_addr empty
;
477 memset (&empty
, 0, sizeof (struct in6_addr
));
478 stream_write (s
, (u_char
*) &empty
, IPV6_MAX_BYTELEN
);
482 /* Interface index. */
484 stream_putl (s
, nexthop
->ifindex
);
491 stream_putl (s
, rib
->metric
);
493 /* Write next-hop number */
495 stream_putc_at (s
, nhnummark
, nhnum
);
497 /* Write packet size. */
498 stream_putw_at (s
, 0, stream_get_endp (s
));
500 return zebra_server_send_message(client
);
505 zsend_ipv6_nexthop_lookup (struct zserv
*client
, struct in6_addr
*addr
)
511 struct nexthop
*nexthop
;
513 /* Lookup nexthop. */
514 rib
= rib_match_ipv6 (addr
);
516 /* Get output stream. */
520 /* Fill in result. */
522 stream_putc (s
, ZEBRA_IPV6_NEXTHOP_LOOKUP
);
523 stream_put (s
, &addr
, 16);
527 stream_putl (s
, rib
->metric
);
529 nump
= stream_get_endp(s
);
531 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
532 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
534 stream_putc (s
, nexthop
->type
);
535 switch (nexthop
->type
)
537 case ZEBRA_NEXTHOP_IPV6
:
538 stream_put (s
, &nexthop
->gate
.ipv6
, 16);
540 case ZEBRA_NEXTHOP_IPV6_IFINDEX
:
541 case ZEBRA_NEXTHOP_IPV6_IFNAME
:
542 stream_put (s
, &nexthop
->gate
.ipv6
, 16);
543 stream_putl (s
, nexthop
->ifindex
);
545 case ZEBRA_NEXTHOP_IFINDEX
:
546 case ZEBRA_NEXTHOP_IFNAME
:
547 stream_putl (s
, nexthop
->ifindex
);
555 stream_putc_at (s
, nump
, num
);
563 stream_putw_at (s
, 0, stream_get_endp (s
));
565 return zebra_server_send_message(client
);
567 #endif /* HAVE_IPV6 */
570 zsend_ipv4_nexthop_lookup (struct zserv
*client
, struct in_addr addr
)
576 struct nexthop
*nexthop
;
578 /* Lookup nexthop. */
579 rib
= rib_match_ipv4 (addr
);
581 /* Get output stream. */
585 /* Fill in result. */
587 stream_putc (s
, ZEBRA_IPV4_NEXTHOP_LOOKUP
);
588 stream_put_in_addr (s
, &addr
);
592 stream_putl (s
, rib
->metric
);
594 nump
= stream_get_endp(s
);
596 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
597 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
599 stream_putc (s
, nexthop
->type
);
600 switch (nexthop
->type
)
602 case ZEBRA_NEXTHOP_IPV4
:
603 stream_put_in_addr (s
, &nexthop
->gate
.ipv4
);
605 case ZEBRA_NEXTHOP_IFINDEX
:
606 case ZEBRA_NEXTHOP_IFNAME
:
607 stream_putl (s
, nexthop
->ifindex
);
615 stream_putc_at (s
, nump
, num
);
623 stream_putw_at (s
, 0, stream_get_endp (s
));
625 return zebra_server_send_message(client
);
629 zsend_ipv4_import_lookup (struct zserv
*client
, struct prefix_ipv4
*p
)
635 struct nexthop
*nexthop
;
637 /* Lookup nexthop. */
638 rib
= rib_lookup_ipv4 (p
);
640 /* Get output stream. */
644 /* Fill in result. */
646 stream_putc (s
, ZEBRA_IPV4_IMPORT_LOOKUP
);
647 stream_put_in_addr (s
, &p
->prefix
);
651 stream_putl (s
, rib
->metric
);
653 nump
= stream_get_endp(s
);
655 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
656 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
658 stream_putc (s
, nexthop
->type
);
659 switch (nexthop
->type
)
661 case ZEBRA_NEXTHOP_IPV4
:
662 stream_put_in_addr (s
, &nexthop
->gate
.ipv4
);
664 case ZEBRA_NEXTHOP_IFINDEX
:
665 case ZEBRA_NEXTHOP_IFNAME
:
666 stream_putl (s
, nexthop
->ifindex
);
674 stream_putc_at (s
, nump
, num
);
682 stream_putw_at (s
, 0, stream_get_endp (s
));
684 return zebra_server_send_message(client
);
687 /* Router-id is updated. Send ZEBRA_ROUTER_ID_ADD to client. */
689 zsend_router_id_update (struct zserv
*client
, struct prefix
*p
)
694 /* Check this client need interface information. */
695 if (!client
->ridinfo
)
701 /* Place holder for size. */
705 stream_putc (s
, ZEBRA_ROUTER_ID_UPDATE
);
707 /* Prefix information. */
708 stream_putc (s
, p
->family
);
709 blen
= prefix_blen (p
);
710 stream_put (s
, &p
->u
.prefix
, blen
);
711 stream_putc (s
, p
->prefixlen
);
713 /* Write packet size. */
714 stream_putw_at (s
, 0, stream_get_endp (s
));
716 return zebra_server_send_message(client
);
719 /* Register zebra server interface information. Send current all
720 interface and address information. */
722 zread_interface_add (struct zserv
*client
, u_short length
)
724 struct listnode
*ifnode
, *ifnnode
;
725 struct listnode
*cnode
, *cnnode
;
726 struct interface
*ifp
;
729 /* Interface information is needed. */
732 for (ALL_LIST_ELEMENTS (iflist
, ifnode
, ifnnode
, ifp
))
734 /* Skip pseudo interface. */
735 if (! CHECK_FLAG (ifp
->status
, ZEBRA_INTERFACE_ACTIVE
))
738 if (zsend_interface_add (client
, ifp
) < 0)
741 for (ALL_LIST_ELEMENTS (ifp
->connected
, cnode
, cnnode
, c
))
743 if (CHECK_FLAG (c
->conf
, ZEBRA_IFC_REAL
) &&
744 (zsend_interface_address (ZEBRA_INTERFACE_ADDRESS_ADD
, client
,
752 /* Unregister zebra server interface information. */
754 zread_interface_delete (struct zserv
*client
, u_short length
)
760 /* This function support multiple nexthop. */
762 * Parse the ZEBRA_IPV4_ROUTE_ADD sent from client. Update rib and
766 zread_ipv4_add (struct zserv
*client
, u_short length
)
770 struct prefix_ipv4 p
;
772 struct in_addr nexthop
;
776 unsigned int ifindex
;
779 /* Get input stream. */
782 /* Allocate new rib. */
783 rib
= XMALLOC (MTYPE_RIB
, sizeof (struct rib
));
784 memset (rib
, 0, sizeof (struct rib
));
786 /* Type, flags, message. */
787 rib
->type
= stream_getc (s
);
788 rib
->flags
= stream_getc (s
);
789 message
= stream_getc (s
);
790 rib
->uptime
= time (NULL
);
793 memset (&p
, 0, sizeof (struct prefix_ipv4
));
795 p
.prefixlen
= stream_getc (s
);
796 stream_get (&p
.prefix
, s
, PSIZE (p
.prefixlen
));
799 if (CHECK_FLAG (message
, ZAPI_MESSAGE_NEXTHOP
))
801 nexthop_num
= stream_getc (s
);
803 for (i
= 0; i
< nexthop_num
; i
++)
805 nexthop_type
= stream_getc (s
);
807 switch (nexthop_type
)
809 case ZEBRA_NEXTHOP_IFINDEX
:
810 ifindex
= stream_getl (s
);
811 nexthop_ifindex_add (rib
, ifindex
);
813 case ZEBRA_NEXTHOP_IFNAME
:
814 ifname_len
= stream_getc (s
);
815 stream_forward_getp (s
, ifname_len
);
817 case ZEBRA_NEXTHOP_IPV4
:
818 nexthop
.s_addr
= stream_get_ipv4 (s
);
819 nexthop_ipv4_add (rib
, &nexthop
);
821 case ZEBRA_NEXTHOP_IPV6
:
822 stream_forward_getp (s
, IPV6_MAX_BYTELEN
);
824 case ZEBRA_NEXTHOP_BLACKHOLE
:
825 nexthop_blackhole_add (rib
);
832 if (CHECK_FLAG (message
, ZAPI_MESSAGE_DISTANCE
))
833 rib
->distance
= stream_getc (s
);
836 if (CHECK_FLAG (message
, ZAPI_MESSAGE_METRIC
))
837 rib
->metric
= stream_getl (s
);
839 rib_add_ipv4_multipath (&p
, rib
);
843 /* Zebra server IPv4 prefix delete function. */
845 zread_ipv4_delete (struct zserv
*client
, u_short length
)
849 struct zapi_ipv4 api
;
850 struct in_addr nexthop
;
851 unsigned long ifindex
;
852 struct prefix_ipv4 p
;
861 /* Type, flags, message. */
862 api
.type
= stream_getc (s
);
863 api
.flags
= stream_getc (s
);
864 api
.message
= stream_getc (s
);
867 memset (&p
, 0, sizeof (struct prefix_ipv4
));
869 p
.prefixlen
= stream_getc (s
);
870 stream_get (&p
.prefix
, s
, PSIZE (p
.prefixlen
));
872 /* Nexthop, ifindex, distance, metric. */
873 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
))
875 nexthop_num
= stream_getc (s
);
877 for (i
= 0; i
< nexthop_num
; i
++)
879 nexthop_type
= stream_getc (s
);
881 switch (nexthop_type
)
883 case ZEBRA_NEXTHOP_IFINDEX
:
884 ifindex
= stream_getl (s
);
886 case ZEBRA_NEXTHOP_IFNAME
:
887 ifname_len
= stream_getc (s
);
888 stream_forward_getp (s
, ifname_len
);
890 case ZEBRA_NEXTHOP_IPV4
:
891 nexthop
.s_addr
= stream_get_ipv4 (s
);
893 case ZEBRA_NEXTHOP_IPV6
:
894 stream_forward_getp (s
, IPV6_MAX_BYTELEN
);
901 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_DISTANCE
))
902 api
.distance
= stream_getc (s
);
907 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_METRIC
))
908 api
.metric
= stream_getl (s
);
912 rib_delete_ipv4 (api
.type
, api
.flags
, &p
, &nexthop
, ifindex
,
917 /* Nexthop lookup for IPv4. */
919 zread_ipv4_nexthop_lookup (struct zserv
*client
, u_short length
)
923 addr
.s_addr
= stream_get_ipv4 (client
->ibuf
);
924 return zsend_ipv4_nexthop_lookup (client
, addr
);
927 /* Nexthop lookup for IPv4. */
929 zread_ipv4_import_lookup (struct zserv
*client
, u_short length
)
931 struct prefix_ipv4 p
;
934 p
.prefixlen
= stream_getc (client
->ibuf
);
935 p
.prefix
.s_addr
= stream_get_ipv4 (client
->ibuf
);
937 return zsend_ipv4_import_lookup (client
, &p
);
941 /* Zebra server IPv6 prefix add function. */
943 zread_ipv6_add (struct zserv
*client
, u_short length
)
947 struct zapi_ipv6 api
;
948 struct in6_addr nexthop
;
949 unsigned long ifindex
;
950 struct prefix_ipv6 p
;
954 memset (&nexthop
, 0, sizeof (struct in6_addr
));
956 /* Type, flags, message. */
957 api
.type
= stream_getc (s
);
958 api
.flags
= stream_getc (s
);
959 api
.message
= stream_getc (s
);
962 memset (&p
, 0, sizeof (struct prefix_ipv6
));
964 p
.prefixlen
= stream_getc (s
);
965 stream_get (&p
.prefix
, s
, PSIZE (p
.prefixlen
));
967 /* Nexthop, ifindex, distance, metric. */
968 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
))
972 api
.nexthop_num
= stream_getc (s
);
973 for (i
= 0; i
< api
.nexthop_num
; i
++)
975 nexthop_type
= stream_getc (s
);
977 switch (nexthop_type
)
979 case ZEBRA_NEXTHOP_IPV6
:
980 stream_get (&nexthop
, s
, 16);
982 case ZEBRA_NEXTHOP_IFINDEX
:
983 ifindex
= stream_getl (s
);
989 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_DISTANCE
))
990 api
.distance
= stream_getc (s
);
994 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_METRIC
))
995 api
.metric
= stream_getl (s
);
999 if (IN6_IS_ADDR_UNSPECIFIED (&nexthop
))
1000 rib_add_ipv6 (api
.type
, api
.flags
, &p
, NULL
, ifindex
, 0);
1002 rib_add_ipv6 (api
.type
, api
.flags
, &p
, &nexthop
, ifindex
, 0);
1006 /* Zebra server IPv6 prefix delete function. */
1008 zread_ipv6_delete (struct zserv
*client
, u_short length
)
1012 struct zapi_ipv6 api
;
1013 struct in6_addr nexthop
;
1014 unsigned long ifindex
;
1015 struct prefix_ipv6 p
;
1019 memset (&nexthop
, 0, sizeof (struct in6_addr
));
1021 /* Type, flags, message. */
1022 api
.type
= stream_getc (s
);
1023 api
.flags
= stream_getc (s
);
1024 api
.message
= stream_getc (s
);
1027 memset (&p
, 0, sizeof (struct prefix_ipv6
));
1028 p
.family
= AF_INET6
;
1029 p
.prefixlen
= stream_getc (s
);
1030 stream_get (&p
.prefix
, s
, PSIZE (p
.prefixlen
));
1032 /* Nexthop, ifindex, distance, metric. */
1033 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
))
1035 u_char nexthop_type
;
1037 api
.nexthop_num
= stream_getc (s
);
1038 for (i
= 0; i
< api
.nexthop_num
; i
++)
1040 nexthop_type
= stream_getc (s
);
1042 switch (nexthop_type
)
1044 case ZEBRA_NEXTHOP_IPV6
:
1045 stream_get (&nexthop
, s
, 16);
1047 case ZEBRA_NEXTHOP_IFINDEX
:
1048 ifindex
= stream_getl (s
);
1054 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_DISTANCE
))
1055 api
.distance
= stream_getc (s
);
1058 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_METRIC
))
1059 api
.metric
= stream_getl (s
);
1063 if (IN6_IS_ADDR_UNSPECIFIED (&nexthop
))
1064 rib_delete_ipv6 (api
.type
, api
.flags
, &p
, NULL
, ifindex
, 0);
1066 rib_delete_ipv6 (api
.type
, api
.flags
, &p
, &nexthop
, ifindex
, 0);
1071 zread_ipv6_nexthop_lookup (struct zserv
*client
, u_short length
)
1073 struct in6_addr addr
;
1076 stream_get (&addr
, client
->ibuf
, 16);
1077 printf ("DEBUG %s\n", inet_ntop (AF_INET6
, &addr
, buf
, BUFSIZ
));
1079 return zsend_ipv6_nexthop_lookup (client
, &addr
);
1081 #endif /* HAVE_IPV6 */
1083 /* Register zebra server router-id information. Send current router-id */
1085 zread_router_id_add (struct zserv
*client
, u_short length
)
1089 /* Router-id information is needed. */
1090 client
->ridinfo
= 1;
1094 return zsend_router_id_update (client
,&p
);
1097 /* Unregister zebra server router-id information. */
1099 zread_router_id_delete (struct zserv
*client
, u_short length
)
1101 client
->ridinfo
= 0;
1105 /* Close zebra client. */
1107 zebra_client_close (struct zserv
*client
)
1109 /* Close file descriptor. */
1112 close (client
->sock
);
1116 /* Free stream buffers. */
1118 stream_free (client
->ibuf
);
1120 stream_free (client
->obuf
);
1122 buffer_free(client
->wb
);
1124 /* Release threads. */
1126 thread_cancel (client
->t_read
);
1127 if (client
->t_write
)
1128 thread_cancel (client
->t_write
);
1129 if (client
->t_suicide
)
1130 thread_cancel (client
->t_suicide
);
1132 /* Free client structure. */
1133 listnode_delete (zebrad
.client_list
, client
);
1137 /* Make new client. */
1139 zebra_client_create (int sock
)
1141 struct zserv
*client
;
1143 client
= XCALLOC (0, sizeof (struct zserv
));
1145 /* Make client input/output buffer. */
1146 client
->sock
= sock
;
1147 client
->ibuf
= stream_new (ZEBRA_MAX_PACKET_SIZ
);
1148 client
->obuf
= stream_new (ZEBRA_MAX_PACKET_SIZ
);
1149 client
->wb
= buffer_new(0);
1151 /* Set table number. */
1152 client
->rtm_table
= zebrad
.rtm_table_default
;
1154 /* Add this client to linked list. */
1155 listnode_add (zebrad
.client_list
, client
);
1157 /* Make new read thread. */
1158 zebra_event (ZEBRA_READ
, sock
, client
);
1161 /* Handler of zebra service request. */
1163 zebra_client_read (struct thread
*thread
)
1166 struct zserv
*client
;
1171 /* Get thread data. Reset reading thread because I'm running. */
1172 sock
= THREAD_FD (thread
);
1173 client
= THREAD_ARG (thread
);
1174 client
->t_read
= NULL
;
1176 if (client
->t_suicide
)
1178 zebra_client_close(client
);
1182 /* Read length and command (if we don't have it already). */
1183 if (stream_get_endp(client
->ibuf
) < ZEBRA_HEADER_SIZE
)
1185 if (((nbyte
= stream_read_try (client
->ibuf
, sock
,
1186 ZEBRA_HEADER_SIZE
)) == 0) ||
1189 if (IS_ZEBRA_DEBUG_EVENT
)
1190 zlog_debug ("connection closed socket [%d]", sock
);
1191 zebra_client_close (client
);
1194 if (stream_get_endp(client
->ibuf
) < ZEBRA_HEADER_SIZE
)
1196 /* Try again later. */
1197 zebra_event (ZEBRA_READ
, sock
, client
);
1202 /* Reset to read from the beginning of the incoming packet. */
1203 stream_set_getp(client
->ibuf
, 0);
1205 length
= stream_getw (client
->ibuf
);
1206 command
= stream_getc (client
->ibuf
);
1208 if (length
< ZEBRA_HEADER_SIZE
)
1210 if (IS_ZEBRA_DEBUG_EVENT
)
1211 zlog_debug ("length %d is less than %d ", length
, ZEBRA_HEADER_SIZE
);
1212 zebra_client_close (client
);
1216 /* Read rest of data. */
1217 if (stream_get_endp(client
->ibuf
) < length
)
1219 nbyte
= stream_read_try (client
->ibuf
, sock
,
1220 length
-stream_get_endp(client
->ibuf
));
1221 if ((nbyte
== 0) || (nbyte
== -1))
1223 if (IS_ZEBRA_DEBUG_EVENT
)
1224 zlog_debug ("connection closed [%d] when reading zebra data", sock
);
1225 zebra_client_close (client
);
1228 if (stream_get_endp(client
->ibuf
) < length
)
1230 /* Try again later. */
1231 zebra_event (ZEBRA_READ
, sock
, client
);
1236 length
-= ZEBRA_HEADER_SIZE
;
1238 /* Debug packet information. */
1239 if (IS_ZEBRA_DEBUG_EVENT
)
1240 zlog_debug ("zebra message comes from socket [%d]", sock
);
1242 if (IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
)
1243 zlog_debug ("zebra message received [%s] %d",
1244 zebra_command_str
[command
], length
);
1248 case ZEBRA_ROUTER_ID_ADD
:
1249 zread_router_id_add (client
, length
);
1251 case ZEBRA_ROUTER_ID_DELETE
:
1252 zread_router_id_delete (client
, length
);
1254 case ZEBRA_INTERFACE_ADD
:
1255 zread_interface_add (client
, length
);
1257 case ZEBRA_INTERFACE_DELETE
:
1258 zread_interface_delete (client
, length
);
1260 case ZEBRA_IPV4_ROUTE_ADD
:
1261 zread_ipv4_add (client
, length
);
1263 case ZEBRA_IPV4_ROUTE_DELETE
:
1264 zread_ipv4_delete (client
, length
);
1267 case ZEBRA_IPV6_ROUTE_ADD
:
1268 zread_ipv6_add (client
, length
);
1270 case ZEBRA_IPV6_ROUTE_DELETE
:
1271 zread_ipv6_delete (client
, length
);
1273 #endif /* HAVE_IPV6 */
1274 case ZEBRA_REDISTRIBUTE_ADD
:
1275 zebra_redistribute_add (command
, client
, length
);
1277 case ZEBRA_REDISTRIBUTE_DELETE
:
1278 zebra_redistribute_delete (command
, client
, length
);
1280 case ZEBRA_REDISTRIBUTE_DEFAULT_ADD
:
1281 zebra_redistribute_default_add (command
, client
, length
);
1283 case ZEBRA_REDISTRIBUTE_DEFAULT_DELETE
:
1284 zebra_redistribute_default_delete (command
, client
, length
);
1286 case ZEBRA_IPV4_NEXTHOP_LOOKUP
:
1287 zread_ipv4_nexthop_lookup (client
, length
);
1290 case ZEBRA_IPV6_NEXTHOP_LOOKUP
:
1291 zread_ipv6_nexthop_lookup (client
, length
);
1293 #endif /* HAVE_IPV6 */
1294 case ZEBRA_IPV4_IMPORT_LOOKUP
:
1295 zread_ipv4_import_lookup (client
, length
);
1298 zlog_info ("Zebra received unknown command %d", command
);
1302 if (client
->t_suicide
)
1304 /* No need to wait for thread callback, just kill immediately. */
1305 zebra_client_close(client
);
1309 stream_reset (client
->ibuf
);
1310 zebra_event (ZEBRA_READ
, sock
, client
);
1315 /* Accept code of zebra server socket. */
1317 zebra_accept (struct thread
*thread
)
1321 struct sockaddr_in client
;
1324 accept_sock
= THREAD_FD (thread
);
1326 /* Reregister myself. */
1327 zebra_event (ZEBRA_SERV
, accept_sock
, NULL
);
1329 len
= sizeof (struct sockaddr_in
);
1330 client_sock
= accept (accept_sock
, (struct sockaddr
*) &client
, &len
);
1332 if (client_sock
< 0)
1334 zlog_warn ("Can't accept zebra socket: %s", safe_strerror (errno
));
1338 /* Make client socket non-blocking. */
1339 set_nonblocking(client_sock
);
1341 /* Create new zebra client. */
1342 zebra_client_create (client_sock
);
1347 #ifdef HAVE_TCP_ZEBRA
1348 /* Make zebra's server socket. */
1354 struct sockaddr_in addr
;
1356 accept_sock
= socket (AF_INET
, SOCK_STREAM
, 0);
1358 if (accept_sock
< 0)
1360 zlog_warn ("Can't create zserv stream socket: %s",
1361 safe_strerror (errno
));
1362 zlog_warn ("zebra can't provice full functionality due to above error");
1366 memset (&addr
, 0, sizeof (struct sockaddr_in
));
1367 addr
.sin_family
= AF_INET
;
1368 addr
.sin_port
= htons (ZEBRA_PORT
);
1370 addr
.sin_len
= sizeof (struct sockaddr_in
);
1371 #endif /* HAVE_SIN_LEN */
1372 addr
.sin_addr
.s_addr
= htonl (INADDR_LOOPBACK
);
1374 sockopt_reuseaddr (accept_sock
);
1375 sockopt_reuseport (accept_sock
);
1377 if ( zserv_privs
.change(ZPRIVS_RAISE
) )
1378 zlog (NULL
, LOG_ERR
, "Can't raise privileges");
1380 ret
= bind (accept_sock
, (struct sockaddr
*)&addr
,
1381 sizeof (struct sockaddr_in
));
1384 zlog_warn ("Can't bind to stream socket: %s",
1385 safe_strerror (errno
));
1386 zlog_warn ("zebra can't provice full functionality due to above error");
1387 close (accept_sock
); /* Avoid sd leak. */
1391 if ( zserv_privs
.change(ZPRIVS_LOWER
) )
1392 zlog (NULL
, LOG_ERR
, "Can't lower privileges");
1394 ret
= listen (accept_sock
, 1);
1397 zlog_warn ("Can't listen to stream socket: %s",
1398 safe_strerror (errno
));
1399 zlog_warn ("zebra can't provice full functionality due to above error");
1400 close (accept_sock
); /* Avoid sd leak. */
1404 zebra_event (ZEBRA_SERV
, accept_sock
, NULL
);
1406 #endif /* HAVE_TCP_ZEBRA */
1408 /* For sockaddr_un. */
1411 /* zebra server UNIX domain socket. */
1413 zebra_serv_un (const char *path
)
1417 struct sockaddr_un serv
;
1420 /* First of all, unlink existing socket */
1424 old_mask
= umask (0077);
1426 /* Make UNIX domain socket. */
1427 sock
= socket (AF_UNIX
, SOCK_STREAM
, 0);
1430 zlog_warn ("Can't create zserv unix socket: %s",
1431 safe_strerror (errno
));
1432 zlog_warn ("zebra can't provide full functionality due to above error");
1436 /* Make server socket. */
1437 memset (&serv
, 0, sizeof (struct sockaddr_un
));
1438 serv
.sun_family
= AF_UNIX
;
1439 strncpy (serv
.sun_path
, path
, strlen (path
));
1441 len
= serv
.sun_len
= SUN_LEN(&serv
);
1443 len
= sizeof (serv
.sun_family
) + strlen (serv
.sun_path
);
1444 #endif /* HAVE_SUN_LEN */
1446 ret
= bind (sock
, (struct sockaddr
*) &serv
, len
);
1449 zlog_warn ("Can't bind to unix socket %s: %s",
1450 path
, safe_strerror (errno
));
1451 zlog_warn ("zebra can't provide full functionality due to above error");
1456 ret
= listen (sock
, 5);
1459 zlog_warn ("Can't listen to unix socket %s: %s",
1460 path
, safe_strerror (errno
));
1461 zlog_warn ("zebra can't provide full functionality due to above error");
1468 zebra_event (ZEBRA_SERV
, sock
, NULL
);
1473 zebra_event (enum event event
, int sock
, struct zserv
*client
)
1478 thread_add_read (zebrad
.master
, zebra_accept
, client
, sock
);
1482 thread_add_read (zebrad
.master
, zebra_client_read
, client
, sock
);
1490 /* Display default rtm_table for all clients. */
1495 "default routing table to use for all clients\n")
1497 vty_out (vty
, "table %d%s", zebrad
.rtm_table_default
,
1502 DEFUN (config_table
,
1505 "Configure target kernel routing table\n"
1508 zebrad
.rtm_table_default
= strtol (argv
[0], (char**)0, 10);
1512 DEFUN (ip_forwarding
,
1516 "Turn on IP forwarding")
1522 ret
= ipforward_on ();
1526 vty_out (vty
, "Can't turn on IP forwarding%s", VTY_NEWLINE
);
1533 DEFUN (no_ip_forwarding
,
1534 no_ip_forwarding_cmd
,
1538 "Turn off IP forwarding")
1544 ret
= ipforward_off ();
1548 vty_out (vty
, "Can't turn off IP forwarding%s", VTY_NEWLINE
);
1555 /* This command is for debugging purpose. */
1556 DEFUN (show_zebra_client
,
1557 show_zebra_client_cmd
,
1558 "show zebra client",
1561 "Client information")
1563 struct listnode
*node
;
1564 struct zserv
*client
;
1566 for (ALL_LIST_ELEMENTS_RO (zebrad
.client_list
, node
, client
))
1567 vty_out (vty
, "Client fd %d%s", client
->sock
, VTY_NEWLINE
);
1572 /* Table configuration write function. */
1574 config_write_table (struct vty
*vty
)
1576 if (zebrad
.rtm_table_default
)
1577 vty_out (vty
, "table %d%s", zebrad
.rtm_table_default
,
1582 /* table node for routing tables. */
1583 struct cmd_node table_node
=
1586 "", /* This node has no interface. */
1590 /* Only display ip forwarding is enabled or not. */
1591 DEFUN (show_ip_forwarding
,
1592 show_ip_forwarding_cmd
,
1593 "show ip forwarding",
1596 "IP forwarding status\n")
1603 vty_out (vty
, "IP forwarding is off%s", VTY_NEWLINE
);
1605 vty_out (vty
, "IP forwarding is on%s", VTY_NEWLINE
);
1610 /* Only display ipv6 forwarding is enabled or not. */
1611 DEFUN (show_ipv6_forwarding
,
1612 show_ipv6_forwarding_cmd
,
1613 "show ipv6 forwarding",
1615 "IPv6 information\n"
1616 "Forwarding status\n")
1620 ret
= ipforward_ipv6 ();
1625 vty_out (vty
, "ipv6 forwarding is unknown%s", VTY_NEWLINE
);
1628 vty_out (vty
, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE
);
1631 vty_out (vty
, "ipv6 forwarding is %s%s", "on", VTY_NEWLINE
);
1634 vty_out (vty
, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE
);
1640 DEFUN (ipv6_forwarding
,
1641 ipv6_forwarding_cmd
,
1644 "Turn on IPv6 forwarding")
1648 ret
= ipforward_ipv6 ();
1650 ret
= ipforward_ipv6_on ();
1654 vty_out (vty
, "Can't turn on IPv6 forwarding%s", VTY_NEWLINE
);
1661 DEFUN (no_ipv6_forwarding
,
1662 no_ipv6_forwarding_cmd
,
1663 "no ipv6 forwarding",
1666 "Turn off IPv6 forwarding")
1670 ret
= ipforward_ipv6 ();
1672 ret
= ipforward_ipv6_off ();
1676 vty_out (vty
, "Can't turn off IPv6 forwarding%s", VTY_NEWLINE
);
1683 #endif /* HAVE_IPV6 */
1685 /* IPForwarding configuration write function. */
1687 config_write_forwarding (struct vty
*vty
)
1689 /* FIXME: Find better place for that. */
1690 router_id_write (vty
);
1693 vty_out (vty
, "ip forwarding%s", VTY_NEWLINE
);
1695 if (ipforward_ipv6 ())
1696 vty_out (vty
, "ipv6 forwarding%s", VTY_NEWLINE
);
1697 #endif /* HAVE_IPV6 */
1698 vty_out (vty
, "!%s", VTY_NEWLINE
);
1702 /* table node for routing tables. */
1703 struct cmd_node forwarding_node
=
1706 "", /* This node has no interface. */
1711 /* Initialisation of zebra and installation of commands. */
1715 /* Client list init. */
1716 zebrad
.client_list
= list_new ();
1718 /* Make zebra server socket. */
1719 #ifdef HAVE_TCP_ZEBRA
1722 zebra_serv_un (ZEBRA_SERV_PATH
);
1723 #endif /* HAVE_TCP_ZEBRA */
1725 /* Install configuration write function. */
1726 install_node (&table_node
, config_write_table
);
1727 install_node (&forwarding_node
, config_write_forwarding
);
1729 install_element (VIEW_NODE
, &show_ip_forwarding_cmd
);
1730 install_element (ENABLE_NODE
, &show_ip_forwarding_cmd
);
1731 install_element (CONFIG_NODE
, &ip_forwarding_cmd
);
1732 install_element (CONFIG_NODE
, &no_ip_forwarding_cmd
);
1733 install_element (ENABLE_NODE
, &show_zebra_client_cmd
);
1736 install_element (VIEW_NODE
, &show_table_cmd
);
1737 install_element (ENABLE_NODE
, &show_table_cmd
);
1738 install_element (CONFIG_NODE
, &config_table_cmd
);
1739 #endif /* HAVE_NETLINK */
1742 install_element (VIEW_NODE
, &show_ipv6_forwarding_cmd
);
1743 install_element (ENABLE_NODE
, &show_ipv6_forwarding_cmd
);
1744 install_element (CONFIG_NODE
, &ipv6_forwarding_cmd
);
1745 install_element (CONFIG_NODE
, &no_ipv6_forwarding_cmd
);
1746 #endif /* HAVE_IPV6 */