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"
41 #include "zebra/zserv.h"
42 #include "zebra/router-id.h"
43 #include "zebra/redistribute.h"
44 #include "zebra/debug.h"
45 #include "zebra/ipforward.h"
46 #include "zebra/zebra_rnh.h"
48 /* Event list of zebra. */
49 enum event
{ ZEBRA_SERV
, ZEBRA_READ
, ZEBRA_WRITE
};
51 extern struct zebra_t zebrad
;
53 static void zebra_event (enum event event
, int sock
, struct zserv
*client
);
55 extern struct zebra_privs_t zserv_privs
;
57 static void zebra_client_close (struct zserv
*client
);
60 zserv_delayed_close(struct thread
*thread
)
62 struct zserv
*client
= THREAD_ARG(thread
);
64 client
->t_suicide
= NULL
;
65 zebra_client_close(client
);
69 /* When client connects, it sends hello message
70 * with promise to send zebra routes of specific type.
71 * Zebra stores a socket fd of the client into
72 * this array. And use it to clean up routes that
73 * client didn't remove for some reasons after closing
76 static int route_type_oaths
[ZEBRA_ROUTE_MAX
];
79 zserv_flush_data(struct thread
*thread
)
81 struct zserv
*client
= THREAD_ARG(thread
);
83 client
->t_write
= NULL
;
84 if (client
->t_suicide
)
86 zebra_client_close(client
);
89 switch (buffer_flush_available(client
->wb
, client
->sock
))
92 zlog_warn("%s: buffer_flush_available failed on zserv client fd %d, "
93 "closing", __func__
, client
->sock
);
94 zebra_client_close(client
);
97 client
->t_write
= thread_add_write(zebrad
.master
, zserv_flush_data
,
98 client
, client
->sock
);
107 zebra_server_send_message(struct zserv
*client
)
109 if (client
->t_suicide
)
111 switch (buffer_write(client
->wb
, client
->sock
, STREAM_DATA(client
->obuf
),
112 stream_get_endp(client
->obuf
)))
115 zlog_warn("%s: buffer_write failed to zserv client fd %d, closing",
116 __func__
, client
->sock
);
117 /* Schedule a delayed close since many of the functions that call this
118 one do not check the return code. They do not allow for the
119 possibility that an I/O error may have caused the client to be
121 client
->t_suicide
= thread_add_event(zebrad
.master
, zserv_delayed_close
,
125 THREAD_OFF(client
->t_write
);
128 THREAD_WRITE_ON(zebrad
.master
, client
->t_write
,
129 zserv_flush_data
, client
, client
->sock
);
136 zserv_create_header (struct stream
*s
, uint16_t cmd
)
138 /* length placeholder, caller can update */
139 stream_putw (s
, ZEBRA_HEADER_SIZE
);
140 stream_putc (s
, ZEBRA_HEADER_MARKER
);
141 stream_putc (s
, ZSERV_VERSION
);
142 stream_putw (s
, cmd
);
146 zserv_encode_interface (struct stream
*s
, struct interface
*ifp
)
148 /* Interface information. */
149 stream_put (s
, ifp
->name
, INTERFACE_NAMSIZ
);
150 stream_putl (s
, ifp
->ifindex
);
151 stream_putc (s
, ifp
->status
);
152 stream_putq (s
, ifp
->flags
);
153 stream_putl (s
, ifp
->metric
);
154 stream_putl (s
, ifp
->mtu
);
155 stream_putl (s
, ifp
->mtu6
);
156 stream_putl (s
, ifp
->bandwidth
);
157 #ifdef HAVE_STRUCT_SOCKADDR_DL
158 stream_put (s
, &ifp
->sdl
, sizeof (ifp
->sdl_storage
));
160 stream_putl (s
, ifp
->hw_addr_len
);
161 if (ifp
->hw_addr_len
)
162 stream_put (s
, ifp
->hw_addr
, ifp
->hw_addr_len
);
163 #endif /* HAVE_STRUCT_SOCKADDR_DL */
165 /* Write packet size. */
166 stream_putw_at (s
, 0, stream_get_endp (s
));
169 /* Interface is added. Send ZEBRA_INTERFACE_ADD to client. */
171 * This function is called in the following situations:
172 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
174 * - at startup, when zebra figures out the available interfaces
175 * - when an interface is added (where support for
176 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
177 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
181 zsend_interface_add (struct zserv
*client
, struct interface
*ifp
)
185 /* Check this client need interface information. */
186 if (! client
->ifinfo
)
192 zserv_create_header (s
, ZEBRA_INTERFACE_ADD
);
193 zserv_encode_interface (s
, ifp
);
195 return zebra_server_send_message(client
);
198 /* Interface deletion from zebra daemon. */
200 zsend_interface_delete (struct zserv
*client
, struct interface
*ifp
)
204 /* Check this client need interface information. */
205 if (! client
->ifinfo
)
211 zserv_create_header (s
, ZEBRA_INTERFACE_DELETE
);
212 zserv_encode_interface (s
, ifp
);
214 return zebra_server_send_message (client
);
217 /* Interface address is added/deleted. Send ZEBRA_INTERFACE_ADDRESS_ADD or
218 * ZEBRA_INTERFACE_ADDRESS_DELETE to the client.
220 * A ZEBRA_INTERFACE_ADDRESS_ADD is sent in the following situations:
221 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
222 * from the client, after the ZEBRA_INTERFACE_ADD has been
223 * sent from zebra to the client
224 * - redistribute new address info to all clients in the following situations
225 * - at startup, when zebra figures out the available interfaces
226 * - when an interface is added (where support for
227 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
228 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
230 * - for the vty commands "ip address A.B.C.D/M [<secondary>|<label LINE>]"
231 * and "no bandwidth <1-10000000>", "ipv6 address X:X::X:X/M"
232 * - when an RTM_NEWADDR message is received from the kernel,
234 * The call tree that triggers ZEBRA_INTERFACE_ADDRESS_DELETE:
236 * zsend_interface_address(DELETE)
239 * zebra_interface_address_delete_update
241 * | | if_delete_update
243 * ip_address_uninstall connected_delete_ipv4
244 * [ipv6_addresss_uninstall] [connected_delete_ipv6]
247 * | RTM_NEWADDR on routing/netlink socket
250 * "no ip address A.B.C.D/M [label LINE]"
251 * "no ip address A.B.C.D/M secondary"
252 * ["no ipv6 address X:X::X:X/M"]
256 zsend_interface_address (int cmd
, struct zserv
*client
,
257 struct interface
*ifp
, struct connected
*ifc
)
263 /* Check this client need interface information. */
264 if (! client
->ifinfo
)
270 zserv_create_header (s
, cmd
);
271 stream_putl (s
, ifp
->ifindex
);
273 /* Interface address flag. */
274 stream_putc (s
, ifc
->flags
);
276 /* Prefix information. */
278 stream_putc (s
, p
->family
);
279 blen
= prefix_blen (p
);
280 stream_put (s
, &p
->u
.prefix
, blen
);
283 * XXX gnu version does not send prefixlen for ZEBRA_INTERFACE_ADDRESS_DELETE
284 * but zebra_interface_address_delete_read() in the gnu version
287 stream_putc (s
, p
->prefixlen
);
290 p
= ifc
->destination
;
292 stream_put (s
, &p
->u
.prefix
, blen
);
294 stream_put (s
, NULL
, blen
);
296 /* Write packet size. */
297 stream_putw_at (s
, 0, stream_get_endp (s
));
299 return zebra_server_send_message(client
);
303 zsend_interface_nbr_address (int cmd
, struct zserv
*client
,
304 struct interface
*ifp
, struct nbr_connected
*ifc
)
310 /* Check this client need interface information. */
311 if (! client
->ifinfo
)
317 zserv_create_header (s
, cmd
);
318 stream_putl (s
, ifp
->ifindex
);
320 /* Prefix information. */
322 stream_putc (s
, p
->family
);
323 blen
= prefix_blen (p
);
324 stream_put (s
, &p
->u
.prefix
, blen
);
327 * XXX gnu version does not send prefixlen for ZEBRA_INTERFACE_ADDRESS_DELETE
328 * but zebra_interface_address_delete_read() in the gnu version
331 stream_putc (s
, p
->prefixlen
);
333 /* Write packet size. */
334 stream_putw_at (s
, 0, stream_get_endp (s
));
336 return zebra_server_send_message(client
);
339 /* Interface address addition. */
341 zebra_interface_nbr_address_add_update (struct interface
*ifp
,
342 struct nbr_connected
*ifc
)
344 struct listnode
*node
, *nnode
;
345 struct zserv
*client
;
348 if (IS_ZEBRA_DEBUG_EVENT
)
350 char buf
[INET6_ADDRSTRLEN
];
353 zlog_debug ("MESSAGE: ZEBRA_INTERFACE_NBR_ADDRESS_ADD %s/%d on %s",
354 inet_ntop (p
->family
, &p
->u
.prefix
, buf
, INET6_ADDRSTRLEN
),
355 p
->prefixlen
, ifc
->ifp
->name
);
358 for (ALL_LIST_ELEMENTS (zebrad
.client_list
, node
, nnode
, client
))
360 zsend_interface_nbr_address (ZEBRA_INTERFACE_NBR_ADDRESS_ADD
, client
, ifp
, ifc
);
363 /* Interface address deletion. */
365 zebra_interface_nbr_address_delete_update (struct interface
*ifp
,
366 struct nbr_connected
*ifc
)
368 struct listnode
*node
, *nnode
;
369 struct zserv
*client
;
372 if (IS_ZEBRA_DEBUG_EVENT
)
374 char buf
[INET6_ADDRSTRLEN
];
377 zlog_debug ("MESSAGE: ZEBRA_INTERFACE_NBR_ADDRESS_DELETE %s/%d on %s",
378 inet_ntop (p
->family
, &p
->u
.prefix
, buf
, INET6_ADDRSTRLEN
),
379 p
->prefixlen
, ifc
->ifp
->name
);
382 for (ALL_LIST_ELEMENTS (zebrad
.client_list
, node
, nnode
, client
))
384 zsend_interface_nbr_address (ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
, client
, ifp
, ifc
);
387 /* Add new nbr connected IPv6 address if none exists already, or replace the
388 existing one if an ifc entry is found on the interface. */
390 nbr_connected_replacement_add_ipv6 (struct interface
*ifp
, struct in6_addr
*address
,
393 struct nbr_connected
*ifc
;
397 IPV6_ADDR_COPY (&p
.u
.prefix
, address
);
398 p
.prefixlen
= prefixlen
;
400 if (nbr_connected_check(ifp
, &p
))
403 if (!(ifc
= listnode_head(ifp
->nbr_connected
)))
406 ifc
= nbr_connected_new ();
407 ifc
->address
= prefix_new();
409 listnode_add (ifp
->nbr_connected
, ifc
);
412 prefix_copy(ifc
->address
, &p
);
414 zebra_interface_nbr_address_add_update (ifp
, ifc
);
418 nbr_connected_delete_ipv6 (struct interface
*ifp
, struct in6_addr
*address
,
421 struct nbr_connected
*ifc
;
425 IPV6_ADDR_COPY (&p
.u
.prefix
, address
);
426 p
.prefixlen
= prefixlen
;
428 ifc
= nbr_connected_check(ifp
, &p
);
432 listnode_delete (ifp
->nbr_connected
, ifc
);
434 zebra_interface_nbr_address_delete_update (ifp
, ifc
);
436 nbr_connected_free (ifc
);
440 * The cmd passed to zsend_interface_update may be ZEBRA_INTERFACE_UP or
441 * ZEBRA_INTERFACE_DOWN.
443 * The ZEBRA_INTERFACE_UP message is sent from the zebra server to
444 * the clients in one of 2 situations:
445 * - an if_up is detected e.g., as a result of an RTM_IFINFO message
446 * - a vty command modifying the bandwidth of an interface is received.
447 * The ZEBRA_INTERFACE_DOWN message is sent when an if_down is detected.
450 zsend_interface_update (int cmd
, struct zserv
*client
, struct interface
*ifp
)
454 /* Check this client need interface information. */
455 if (! client
->ifinfo
)
461 zserv_create_header (s
, cmd
);
462 zserv_encode_interface (s
, ifp
);
464 return zebra_server_send_message(client
);
468 * The zebra server sends the clients a ZEBRA_IPV4_ROUTE_ADD or a
469 * ZEBRA_IPV6_ROUTE_ADD via zsend_route_multipath in the following
471 * - when the client starts up, and requests default information
472 * by sending a ZEBRA_REDISTRIBUTE_DEFAULT_ADD to the zebra server, in the
473 * - case of rip, ripngd, ospfd and ospf6d, when the client sends a
474 * ZEBRA_REDISTRIBUTE_ADD as a result of the "redistribute" vty cmd,
475 * - when the zebra server redistributes routes after it updates its rib
477 * The zebra server sends clients a ZEBRA_IPV4_ROUTE_DELETE or a
478 * ZEBRA_IPV6_ROUTE_DELETE via zsend_route_multipath when:
479 * - a "ip route" or "ipv6 route" vty command is issued, a prefix is
480 * - deleted from zebra's rib, and this info
481 * has to be redistributed to the clients
483 * XXX The ZEBRA_IPV*_ROUTE_ADD message is also sent by the client to the
484 * zebra server when the client wants to tell the zebra server to add a
485 * route to the kernel (zapi_ipv4_add etc. ). Since it's essentially the
486 * same message being sent back and forth, this function and
487 * zapi_ipv{4,6}_{add, delete} should be re-written to avoid code
491 zsend_route_multipath (int cmd
, struct zserv
*client
, struct prefix
*p
,
496 struct nexthop
*nexthop
;
497 unsigned long nhnummark
= 0, messmark
= 0;
499 u_char zapi_flags
= 0;
504 zserv_create_header (s
, cmd
);
506 /* Put type and nexthop. */
507 stream_putc (s
, rib
->type
);
508 stream_putc (s
, rib
->flags
);
510 /* marker for message flags field */
511 messmark
= stream_get_endp (s
);
515 psize
= PSIZE (p
->prefixlen
);
516 stream_putc (s
, p
->prefixlen
);
517 stream_write (s
, (u_char
*) & p
->u
.prefix
, psize
);
520 * XXX The message format sent by zebra below does not match the format
521 * of the corresponding message expected by the zebra server
522 * itself (e.g., see zread_ipv4_add). The nexthop_num is not set correctly,
523 * (is there a bug on the client side if more than one segment is sent?)
524 * nexthop ZEBRA_NEXTHOP_IPV4 is never set, ZEBRA_NEXTHOP_IFINDEX
529 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
531 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
)
532 || nexthop_has_fib_child(nexthop
))
534 SET_FLAG (zapi_flags
, ZAPI_MESSAGE_NEXTHOP
);
535 SET_FLAG (zapi_flags
, ZAPI_MESSAGE_IFINDEX
);
539 nhnummark
= stream_get_endp (s
);
540 stream_putc (s
, 1); /* placeholder */
545 switch(nexthop
->type
)
547 case NEXTHOP_TYPE_IPV4
:
548 case NEXTHOP_TYPE_IPV4_IFINDEX
:
549 stream_put_in_addr (s
, &nexthop
->gate
.ipv4
);
552 case NEXTHOP_TYPE_IPV6
:
553 case NEXTHOP_TYPE_IPV6_IFINDEX
:
554 case NEXTHOP_TYPE_IPV6_IFNAME
:
555 stream_write (s
, (u_char
*) &nexthop
->gate
.ipv6
, 16);
559 if (cmd
== ZEBRA_IPV4_ROUTE_ADD
560 || cmd
== ZEBRA_IPV4_ROUTE_DELETE
)
562 struct in_addr empty
;
563 memset (&empty
, 0, sizeof (struct in_addr
));
564 stream_write (s
, (u_char
*) &empty
, IPV4_MAX_BYTELEN
);
568 struct in6_addr empty
;
569 memset (&empty
, 0, sizeof (struct in6_addr
));
570 stream_write (s
, (u_char
*) &empty
, IPV6_MAX_BYTELEN
);
574 /* Interface index. */
576 stream_putl (s
, nexthop
->ifindex
);
583 if (cmd
== ZEBRA_IPV4_ROUTE_ADD
|| cmd
== ZEBRA_IPV6_ROUTE_ADD
)
585 SET_FLAG (zapi_flags
, ZAPI_MESSAGE_DISTANCE
);
586 stream_putc (s
, rib
->distance
);
587 SET_FLAG (zapi_flags
, ZAPI_MESSAGE_METRIC
);
588 stream_putl (s
, rib
->metric
);
591 /* write real message flags value */
592 stream_putc_at (s
, messmark
, zapi_flags
);
594 /* Write next-hop number */
596 stream_putc_at (s
, nhnummark
, nhnum
);
598 /* Write packet size. */
599 stream_putw_at (s
, 0, stream_get_endp (s
));
601 return zebra_server_send_message(client
);
606 zsend_ipv6_nexthop_lookup (struct zserv
*client
, struct in6_addr
*addr
)
612 struct nexthop
*nexthop
;
614 /* Lookup nexthop. */
615 rib
= rib_match_ipv6 (addr
);
617 /* Get output stream. */
621 /* Fill in result. */
622 zserv_create_header (s
, ZEBRA_IPV6_NEXTHOP_LOOKUP
);
623 stream_put (s
, &addr
, 16);
627 stream_putl (s
, rib
->metric
);
629 nump
= stream_get_endp(s
);
631 /* Only non-recursive routes are elegible to resolve nexthop we
632 * are looking up. Therefore, we will just iterate over the top
633 * chain of nexthops. */
634 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
635 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
637 stream_putc (s
, nexthop
->type
);
638 switch (nexthop
->type
)
640 case ZEBRA_NEXTHOP_IPV6
:
641 stream_put (s
, &nexthop
->gate
.ipv6
, 16);
643 case ZEBRA_NEXTHOP_IPV6_IFINDEX
:
644 case ZEBRA_NEXTHOP_IPV6_IFNAME
:
645 stream_put (s
, &nexthop
->gate
.ipv6
, 16);
646 stream_putl (s
, nexthop
->ifindex
);
648 case ZEBRA_NEXTHOP_IFINDEX
:
649 case ZEBRA_NEXTHOP_IFNAME
:
650 stream_putl (s
, nexthop
->ifindex
);
658 stream_putc_at (s
, nump
, num
);
666 stream_putw_at (s
, 0, stream_get_endp (s
));
668 return zebra_server_send_message(client
);
670 #endif /* HAVE_IPV6 */
673 zsend_ipv4_nexthop_lookup (struct zserv
*client
, struct in_addr addr
)
679 struct nexthop
*nexthop
;
681 /* Lookup nexthop. */
682 rib
= rib_match_ipv4 (addr
);
684 /* Get output stream. */
688 /* Fill in result. */
689 zserv_create_header (s
, ZEBRA_IPV4_NEXTHOP_LOOKUP
);
690 stream_put_in_addr (s
, &addr
);
694 if (IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
)
695 zlog_debug("%s: Matching rib entry found.", __func__
);
696 stream_putl (s
, rib
->metric
);
698 nump
= stream_get_endp(s
);
700 /* Only non-recursive routes are elegible to resolve the nexthop we
701 * are looking up. Therefore, we will just iterate over the top
702 * chain of nexthops. */
703 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
704 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
706 stream_putc (s
, nexthop
->type
);
707 switch (nexthop
->type
)
709 case ZEBRA_NEXTHOP_IPV4
:
710 stream_put_in_addr (s
, &nexthop
->gate
.ipv4
);
712 case ZEBRA_NEXTHOP_IPV4_IFINDEX
:
713 stream_put_in_addr (s
, &nexthop
->gate
.ipv4
);
714 stream_putl (s
, nexthop
->ifindex
);
716 case ZEBRA_NEXTHOP_IFINDEX
:
717 case ZEBRA_NEXTHOP_IFNAME
:
718 stream_putl (s
, nexthop
->ifindex
);
726 stream_putc_at (s
, nump
, num
);
730 if (IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
)
731 zlog_debug("%s: No matching rib entry found.", __func__
);
736 stream_putw_at (s
, 0, stream_get_endp (s
));
738 return zebra_server_send_message(client
);
741 /* Nexthop register */
743 zserv_nexthop_register (struct zserv
*client
, int sock
, u_short length
)
750 if (IS_ZEBRA_DEBUG_NHT
)
751 zlog_debug("nexthop_register msg from client %s: length=%d\n",
752 zebra_route_string(client
->proto
), length
);
758 p
.family
= stream_getw(s
);
759 p
.prefixlen
= stream_getc(s
);
761 stream_get(&p
.u
.prefix
, s
, PSIZE(p
.prefixlen
));
762 l
+= PSIZE(p
.prefixlen
);
763 rnh
= zebra_add_rnh(&p
, 0);
764 zebra_add_rnh_client(rnh
, client
);
766 zebra_evaluate_rnh_table(0, AF_INET
);
767 zebra_evaluate_rnh_table(0, AF_INET6
);
771 /* Nexthop register */
773 zserv_nexthop_unregister (struct zserv
*client
, int sock
, u_short length
)
780 if (IS_ZEBRA_DEBUG_NHT
)
781 zlog_debug("nexthop_unregister msg from client %s: length=%d\n",
782 zebra_route_string(client
->proto
), length
);
788 p
.family
= stream_getw(s
);
789 p
.prefixlen
= stream_getc(s
);
791 stream_get(&p
.u
.prefix
, s
, PSIZE(p
.prefixlen
));
792 l
+= PSIZE(p
.prefixlen
);
793 rnh
= zebra_lookup_rnh(&p
, 0);
795 zebra_remove_rnh_client(rnh
, client
);
801 zsend_ipv4_import_lookup (struct zserv
*client
, struct prefix_ipv4
*p
)
807 struct nexthop
*nexthop
;
809 /* Lookup nexthop. */
810 rib
= rib_lookup_ipv4 (p
);
812 /* Get output stream. */
816 /* Fill in result. */
817 zserv_create_header (s
, ZEBRA_IPV4_IMPORT_LOOKUP
);
818 stream_put_in_addr (s
, &p
->prefix
);
822 stream_putl (s
, rib
->metric
);
824 nump
= stream_get_endp(s
);
826 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
827 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
)
828 || nexthop_has_fib_child(nexthop
))
830 stream_putc (s
, nexthop
->type
);
831 switch (nexthop
->type
)
833 case ZEBRA_NEXTHOP_IPV4
:
834 stream_put_in_addr (s
, &nexthop
->gate
.ipv4
);
836 case ZEBRA_NEXTHOP_IPV4_IFINDEX
:
837 stream_put_in_addr (s
, &nexthop
->gate
.ipv4
);
838 stream_putl (s
, nexthop
->ifindex
);
840 case ZEBRA_NEXTHOP_IFINDEX
:
841 case ZEBRA_NEXTHOP_IFNAME
:
842 stream_putl (s
, nexthop
->ifindex
);
850 stream_putc_at (s
, nump
, num
);
858 stream_putw_at (s
, 0, stream_get_endp (s
));
860 return zebra_server_send_message(client
);
863 /* Router-id is updated. Send ZEBRA_ROUTER_ID_ADD to client. */
865 zsend_router_id_update (struct zserv
*client
, struct prefix
*p
)
870 /* Check this client need interface information. */
871 if (!client
->ridinfo
)
878 zserv_create_header (s
, ZEBRA_ROUTER_ID_UPDATE
);
880 /* Prefix information. */
881 stream_putc (s
, p
->family
);
882 blen
= prefix_blen (p
);
883 stream_put (s
, &p
->u
.prefix
, blen
);
884 stream_putc (s
, p
->prefixlen
);
886 /* Write packet size. */
887 stream_putw_at (s
, 0, stream_get_endp (s
));
889 return zebra_server_send_message(client
);
892 /* Register zebra server interface information. Send current all
893 interface and address information. */
895 zread_interface_add (struct zserv
*client
, u_short length
)
897 struct listnode
*ifnode
, *ifnnode
;
898 struct listnode
*cnode
, *cnnode
;
899 struct interface
*ifp
;
901 struct nbr_connected
*nc
;
903 /* Interface information is needed. */
906 for (ALL_LIST_ELEMENTS (iflist
, ifnode
, ifnnode
, ifp
))
908 /* Skip pseudo interface. */
909 if (! CHECK_FLAG (ifp
->status
, ZEBRA_INTERFACE_ACTIVE
))
912 if (zsend_interface_add (client
, ifp
) < 0)
915 for (ALL_LIST_ELEMENTS (ifp
->connected
, cnode
, cnnode
, c
))
917 if (CHECK_FLAG (c
->conf
, ZEBRA_IFC_REAL
) &&
918 (zsend_interface_address (ZEBRA_INTERFACE_ADDRESS_ADD
, client
,
922 for (ALL_LIST_ELEMENTS (ifp
->nbr_connected
, cnode
, cnnode
, nc
))
924 if (zsend_interface_nbr_address (ZEBRA_INTERFACE_NBR_ADDRESS_ADD
, client
,
933 /* Unregister zebra server interface information. */
935 zread_interface_delete (struct zserv
*client
, u_short length
)
941 /* This function support multiple nexthop. */
943 * Parse the ZEBRA_IPV4_ROUTE_ADD sent from client. Update rib and
947 zread_ipv4_add (struct zserv
*client
, u_short length
)
951 struct prefix_ipv4 p
;
953 struct in_addr nexthop
;
957 unsigned int ifindex
;
962 /* Get input stream. */
965 /* Allocate new rib. */
966 rib
= XCALLOC (MTYPE_RIB
, sizeof (struct rib
));
968 /* Type, flags, message. */
969 rib
->type
= stream_getc (s
);
970 rib
->flags
= stream_getc (s
);
971 message
= stream_getc (s
);
972 safi
= stream_getw (s
);
973 rib
->uptime
= time (NULL
);
976 memset (&p
, 0, sizeof (struct prefix_ipv4
));
978 p
.prefixlen
= stream_getc (s
);
979 stream_get (&p
.prefix
, s
, PSIZE (p
.prefixlen
));
982 if (CHECK_FLAG (message
, ZAPI_MESSAGE_NEXTHOP
))
984 nexthop_num
= stream_getc (s
);
986 for (i
= 0; i
< nexthop_num
; i
++)
988 nexthop_type
= stream_getc (s
);
990 switch (nexthop_type
)
992 case ZEBRA_NEXTHOP_IFINDEX
:
993 ifindex
= stream_getl (s
);
994 nexthop_ifindex_add (rib
, ifindex
);
996 case ZEBRA_NEXTHOP_IFNAME
:
997 ifname_len
= stream_getc (s
);
998 stream_forward_getp (s
, ifname_len
);
1000 case ZEBRA_NEXTHOP_IPV4
:
1001 nexthop
.s_addr
= stream_get_ipv4 (s
);
1002 nexthop_ipv4_add (rib
, &nexthop
, NULL
);
1004 case ZEBRA_NEXTHOP_IPV4_IFINDEX
:
1005 nexthop
.s_addr
= stream_get_ipv4 (s
);
1006 ifindex
= stream_getl (s
);
1007 nexthop_ipv4_ifindex_add (rib
, &nexthop
, NULL
, ifindex
);
1009 case ZEBRA_NEXTHOP_IPV6
:
1010 stream_forward_getp (s
, IPV6_MAX_BYTELEN
);
1012 case ZEBRA_NEXTHOP_BLACKHOLE
:
1013 nexthop_blackhole_add (rib
);
1020 if (CHECK_FLAG (message
, ZAPI_MESSAGE_DISTANCE
))
1021 rib
->distance
= stream_getc (s
);
1024 if (CHECK_FLAG (message
, ZAPI_MESSAGE_METRIC
))
1025 rib
->metric
= stream_getl (s
);
1028 rib
->table
=zebrad
.rtm_table_default
;
1029 rib_add_ipv4_multipath (&p
, rib
, safi
);
1033 /* Zebra server IPv4 prefix delete function. */
1035 zread_ipv4_delete (struct zserv
*client
, u_short length
)
1039 struct zapi_ipv4 api
;
1040 struct in_addr nexthop
, *nexthop_p
;
1041 unsigned long ifindex
;
1042 struct prefix_ipv4 p
;
1044 u_char nexthop_type
;
1052 /* Type, flags, message. */
1053 api
.type
= stream_getc (s
);
1054 api
.flags
= stream_getc (s
);
1055 api
.message
= stream_getc (s
);
1056 api
.safi
= stream_getw (s
);
1059 memset (&p
, 0, sizeof (struct prefix_ipv4
));
1061 p
.prefixlen
= stream_getc (s
);
1062 stream_get (&p
.prefix
, s
, PSIZE (p
.prefixlen
));
1064 /* Nexthop, ifindex, distance, metric. */
1065 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
))
1067 nexthop_num
= stream_getc (s
);
1069 for (i
= 0; i
< nexthop_num
; i
++)
1071 nexthop_type
= stream_getc (s
);
1073 switch (nexthop_type
)
1075 case ZEBRA_NEXTHOP_IFINDEX
:
1076 ifindex
= stream_getl (s
);
1078 case ZEBRA_NEXTHOP_IFNAME
:
1079 ifname_len
= stream_getc (s
);
1080 stream_forward_getp (s
, ifname_len
);
1082 case ZEBRA_NEXTHOP_IPV4
:
1083 nexthop
.s_addr
= stream_get_ipv4 (s
);
1084 nexthop_p
= &nexthop
;
1086 case ZEBRA_NEXTHOP_IPV4_IFINDEX
:
1087 nexthop
.s_addr
= stream_get_ipv4 (s
);
1088 nexthop_p
= &nexthop
;
1089 ifindex
= stream_getl (s
);
1091 case ZEBRA_NEXTHOP_IPV6
:
1092 stream_forward_getp (s
, IPV6_MAX_BYTELEN
);
1099 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_DISTANCE
))
1100 api
.distance
= stream_getc (s
);
1105 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_METRIC
))
1106 api
.metric
= stream_getl (s
);
1110 rib_delete_ipv4 (api
.type
, api
.flags
, &p
, nexthop_p
, ifindex
,
1111 client
->rtm_table
, api
.safi
);
1115 /* Nexthop lookup for IPv4. */
1117 zread_ipv4_nexthop_lookup (struct zserv
*client
, u_short length
)
1119 struct in_addr addr
;
1122 addr
.s_addr
= stream_get_ipv4 (client
->ibuf
);
1123 if (IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
)
1124 zlog_debug("%s: looking up %s", __func__
,
1125 inet_ntop (AF_INET
, &addr
, buf
, BUFSIZ
));
1126 return zsend_ipv4_nexthop_lookup (client
, addr
);
1129 /* Nexthop lookup for IPv4. */
1131 zread_ipv4_import_lookup (struct zserv
*client
, u_short length
)
1133 struct prefix_ipv4 p
;
1136 p
.prefixlen
= stream_getc (client
->ibuf
);
1137 p
.prefix
.s_addr
= stream_get_ipv4 (client
->ibuf
);
1139 return zsend_ipv4_import_lookup (client
, &p
);
1143 /* Zebra server IPv6 prefix add function. */
1145 zread_ipv6_add (struct zserv
*client
, u_short length
)
1149 struct in6_addr nexthop
;
1153 u_char nexthop_type
;
1154 unsigned long ifindex
;
1155 struct prefix_ipv6 p
;
1158 static struct in6_addr nexthops
[MULTIPATH_NUM
];
1159 static unsigned int ifindices
[MULTIPATH_NUM
];
1161 /* Get input stream. */
1165 memset (&nexthop
, 0, sizeof (struct in6_addr
));
1167 /* Allocate new rib. */
1168 rib
= XCALLOC (MTYPE_RIB
, sizeof (struct rib
));
1170 /* Type, flags, message. */
1171 rib
->type
= stream_getc (s
);
1172 rib
->flags
= stream_getc (s
);
1173 message
= stream_getc (s
);
1174 safi
= stream_getw (s
);
1175 rib
->uptime
= time (NULL
);
1178 memset (&p
, 0, sizeof (struct prefix_ipv6
));
1179 p
.family
= AF_INET6
;
1180 p
.prefixlen
= stream_getc (s
);
1181 stream_get (&p
.prefix
, s
, PSIZE (p
.prefixlen
));
1183 /* We need to give nh-addr, nh-ifindex with the same next-hop object
1184 * to the rib to ensure that IPv6 multipathing works; need to coalesce
1185 * these. Clients should send the same number of paired set of
1186 * next-hop-addr/next-hop-ifindices. */
1187 if (CHECK_FLAG (message
, ZAPI_MESSAGE_NEXTHOP
))
1193 nexthop_num
= stream_getc (s
);
1194 for (i
= 0; i
< nexthop_num
; i
++)
1196 nexthop_type
= stream_getc (s
);
1198 switch (nexthop_type
)
1200 case ZEBRA_NEXTHOP_IPV6
:
1201 stream_get (&nexthop
, s
, 16);
1202 if (nh_count
< MULTIPATH_NUM
) {
1203 nexthops
[nh_count
++] = nexthop
;
1206 case ZEBRA_NEXTHOP_IFINDEX
:
1207 if (if_count
< MULTIPATH_NUM
) {
1208 ifindices
[if_count
++] = stream_getl (s
);
1214 max_nh_if
= (nh_count
> if_count
) ? nh_count
: if_count
;
1215 for (i
= 0; i
< max_nh_if
; i
++)
1217 if ((i
< nh_count
) && !IN6_IS_ADDR_UNSPECIFIED (&nexthops
[i
])) {
1218 if ((i
< if_count
) && ifindices
[i
]) {
1219 if (rib_bogus_ipv6 (rib
->type
, &p
, &nexthops
[i
], ifindices
[i
], 0)) {
1222 nexthop_ipv6_ifindex_add (rib
, &nexthops
[i
], ifindices
[i
]);
1225 if (rib_bogus_ipv6 (rib
->type
, &p
, &nexthops
[i
], 0, 0)) {
1228 nexthop_ipv6_add (rib
, &nexthops
[i
]);
1232 if ((i
< if_count
) && ifindices
[i
]) {
1233 if (rib_bogus_ipv6 (rib
->type
, &p
, NULL
, ifindices
[i
], 0)) {
1236 nexthop_ifindex_add (rib
, ifindices
[i
]);
1243 if (CHECK_FLAG (message
, ZAPI_MESSAGE_DISTANCE
))
1244 rib
->distance
= stream_getc (s
);
1247 if (CHECK_FLAG (message
, ZAPI_MESSAGE_METRIC
))
1248 rib
->metric
= stream_getl (s
);
1251 rib
->table
=zebrad
.rtm_table_default
;
1252 rib_add_ipv6_multipath (&p
, rib
, safi
, ifindex
);
1256 /* Zebra server IPv6 prefix delete function. */
1258 zread_ipv6_delete (struct zserv
*client
, u_short length
)
1262 struct zapi_ipv6 api
;
1263 struct in6_addr nexthop
;
1264 unsigned long ifindex
;
1265 struct prefix_ipv6 p
;
1269 memset (&nexthop
, 0, sizeof (struct in6_addr
));
1271 /* Type, flags, message. */
1272 api
.type
= stream_getc (s
);
1273 api
.flags
= stream_getc (s
);
1274 api
.message
= stream_getc (s
);
1275 api
.safi
= stream_getw (s
);
1278 memset (&p
, 0, sizeof (struct prefix_ipv6
));
1279 p
.family
= AF_INET6
;
1280 p
.prefixlen
= stream_getc (s
);
1281 stream_get (&p
.prefix
, s
, PSIZE (p
.prefixlen
));
1283 /* Nexthop, ifindex, distance, metric. */
1284 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
))
1286 u_char nexthop_type
;
1288 api
.nexthop_num
= stream_getc (s
);
1289 for (i
= 0; i
< api
.nexthop_num
; i
++)
1291 nexthop_type
= stream_getc (s
);
1293 switch (nexthop_type
)
1295 case ZEBRA_NEXTHOP_IPV6
:
1296 stream_get (&nexthop
, s
, 16);
1298 case ZEBRA_NEXTHOP_IFINDEX
:
1299 ifindex
= stream_getl (s
);
1305 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_DISTANCE
))
1306 api
.distance
= stream_getc (s
);
1309 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_METRIC
))
1310 api
.metric
= stream_getl (s
);
1314 if (IN6_IS_ADDR_UNSPECIFIED (&nexthop
))
1315 rib_delete_ipv6 (api
.type
, api
.flags
, &p
, NULL
, ifindex
, client
->rtm_table
, api
.safi
);
1317 rib_delete_ipv6 (api
.type
, api
.flags
, &p
, &nexthop
, ifindex
, client
->rtm_table
, api
.safi
);
1322 zread_ipv6_nexthop_lookup (struct zserv
*client
, u_short length
)
1324 struct in6_addr addr
;
1327 stream_get (&addr
, client
->ibuf
, 16);
1328 if (IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
)
1329 zlog_debug("%s: looking up %s", __func__
,
1330 inet_ntop (AF_INET6
, &addr
, buf
, BUFSIZ
));
1332 return zsend_ipv6_nexthop_lookup (client
, &addr
);
1334 #endif /* HAVE_IPV6 */
1336 /* Register zebra server router-id information. Send current router-id */
1338 zread_router_id_add (struct zserv
*client
, u_short length
)
1342 /* Router-id information is needed. */
1343 client
->ridinfo
= 1;
1347 return zsend_router_id_update (client
,&p
);
1350 /* Unregister zebra server router-id information. */
1352 zread_router_id_delete (struct zserv
*client
, u_short length
)
1354 client
->ridinfo
= 0;
1358 /* Tie up route-type and client->sock */
1360 zread_hello (struct zserv
*client
)
1362 /* type of protocol (lib/zebra.h) */
1364 proto
= stream_getc (client
->ibuf
);
1366 /* accept only dynamic routing protocols */
1367 if ((proto
< ZEBRA_ROUTE_MAX
)
1368 && (proto
> ZEBRA_ROUTE_STATIC
))
1370 zlog_notice ("client %d says hello and bids fair to announce only %s routes",
1371 client
->sock
, zebra_route_string(proto
));
1373 /* if route-type was binded by other client */
1374 if (route_type_oaths
[proto
])
1375 zlog_warn ("sender of %s routes changed %c->%c",
1376 zebra_route_string(proto
), route_type_oaths
[proto
],
1379 route_type_oaths
[proto
] = client
->sock
;
1380 client
->proto
= proto
;
1384 /* If client sent routes of specific type, zebra removes it
1385 * and returns number of deleted routes.
1388 zebra_score_rib (int client_sock
)
1392 for (i
= ZEBRA_ROUTE_RIP
; i
< ZEBRA_ROUTE_MAX
; i
++)
1393 if (client_sock
== route_type_oaths
[i
])
1395 zlog_notice ("client %d disconnected. %lu %s routes removed from the rib",
1396 client_sock
, rib_score_proto (i
), zebra_route_string (i
));
1397 route_type_oaths
[i
] = 0;
1402 /* Close zebra client. */
1404 zebra_client_close (struct zserv
*client
)
1406 zebra_cleanup_rnh_client(0, AF_INET
, client
);
1407 zebra_cleanup_rnh_client(0, AF_INET6
, client
);
1409 /* Close file descriptor. */
1412 close (client
->sock
);
1413 zebra_score_rib (client
->sock
);
1417 /* Free stream buffers. */
1419 stream_free (client
->ibuf
);
1421 stream_free (client
->obuf
);
1423 buffer_free(client
->wb
);
1425 /* Release threads. */
1427 thread_cancel (client
->t_read
);
1428 if (client
->t_write
)
1429 thread_cancel (client
->t_write
);
1430 if (client
->t_suicide
)
1431 thread_cancel (client
->t_suicide
);
1433 /* Free client structure. */
1434 listnode_delete (zebrad
.client_list
, client
);
1438 /* Make new client. */
1440 zebra_client_create (int sock
)
1442 struct zserv
*client
;
1444 client
= XCALLOC (0, sizeof (struct zserv
));
1446 /* Make client input/output buffer. */
1447 client
->sock
= sock
;
1448 client
->ibuf
= stream_new (ZEBRA_MAX_PACKET_SIZ
);
1449 client
->obuf
= stream_new (ZEBRA_MAX_PACKET_SIZ
);
1450 client
->wb
= buffer_new(0);
1452 /* Set table number. */
1453 client
->rtm_table
= zebrad
.rtm_table_default
;
1455 /* Add this client to linked list. */
1456 listnode_add (zebrad
.client_list
, client
);
1458 /* Make new read thread. */
1459 zebra_event (ZEBRA_READ
, sock
, client
);
1462 /* Handler of zebra service request. */
1464 zebra_client_read (struct thread
*thread
)
1467 struct zserv
*client
;
1469 uint16_t length
, command
;
1470 uint8_t marker
, version
;
1472 /* Get thread data. Reset reading thread because I'm running. */
1473 sock
= THREAD_FD (thread
);
1474 client
= THREAD_ARG (thread
);
1475 client
->t_read
= NULL
;
1477 if (client
->t_suicide
)
1479 zebra_client_close(client
);
1483 /* Read length and command (if we don't have it already). */
1484 if ((already
= stream_get_endp(client
->ibuf
)) < ZEBRA_HEADER_SIZE
)
1487 if (((nbyte
= stream_read_try (client
->ibuf
, sock
,
1488 ZEBRA_HEADER_SIZE
-already
)) == 0) ||
1491 if (IS_ZEBRA_DEBUG_EVENT
)
1492 zlog_debug ("connection closed socket [%d]", sock
);
1493 zebra_client_close (client
);
1496 if (nbyte
!= (ssize_t
)(ZEBRA_HEADER_SIZE
-already
))
1498 /* Try again later. */
1499 zebra_event (ZEBRA_READ
, sock
, client
);
1502 already
= ZEBRA_HEADER_SIZE
;
1505 /* Reset to read from the beginning of the incoming packet. */
1506 stream_set_getp(client
->ibuf
, 0);
1508 /* Fetch header values */
1509 length
= stream_getw (client
->ibuf
);
1510 marker
= stream_getc (client
->ibuf
);
1511 version
= stream_getc (client
->ibuf
);
1512 command
= stream_getw (client
->ibuf
);
1514 if (marker
!= ZEBRA_HEADER_MARKER
|| version
!= ZSERV_VERSION
)
1516 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
1517 __func__
, sock
, marker
, version
);
1518 zebra_client_close (client
);
1521 if (length
< ZEBRA_HEADER_SIZE
)
1523 zlog_warn("%s: socket %d message length %u is less than header size %d",
1524 __func__
, sock
, length
, ZEBRA_HEADER_SIZE
);
1525 zebra_client_close (client
);
1528 if (length
> STREAM_SIZE(client
->ibuf
))
1530 zlog_warn("%s: socket %d message length %u exceeds buffer size %lu",
1531 __func__
, sock
, length
, (u_long
)STREAM_SIZE(client
->ibuf
));
1532 zebra_client_close (client
);
1536 /* Read rest of data. */
1537 if (already
< length
)
1540 if (((nbyte
= stream_read_try (client
->ibuf
, sock
,
1541 length
-already
)) == 0) ||
1544 if (IS_ZEBRA_DEBUG_EVENT
)
1545 zlog_debug ("connection closed [%d] when reading zebra data", sock
);
1546 zebra_client_close (client
);
1549 if (nbyte
!= (ssize_t
)(length
-already
))
1551 /* Try again later. */
1552 zebra_event (ZEBRA_READ
, sock
, client
);
1557 length
-= ZEBRA_HEADER_SIZE
;
1559 /* Debug packet information. */
1560 if (IS_ZEBRA_DEBUG_EVENT
)
1561 zlog_debug ("zebra message comes from socket [%d]", sock
);
1563 if (IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
)
1564 zlog_debug ("zebra message received [%s] %d",
1565 zserv_command_string (command
), length
);
1569 case ZEBRA_ROUTER_ID_ADD
:
1570 zread_router_id_add (client
, length
);
1572 case ZEBRA_ROUTER_ID_DELETE
:
1573 zread_router_id_delete (client
, length
);
1575 case ZEBRA_INTERFACE_ADD
:
1576 zread_interface_add (client
, length
);
1578 case ZEBRA_INTERFACE_DELETE
:
1579 zread_interface_delete (client
, length
);
1581 case ZEBRA_IPV4_ROUTE_ADD
:
1582 zread_ipv4_add (client
, length
);
1584 case ZEBRA_IPV4_ROUTE_DELETE
:
1585 zread_ipv4_delete (client
, length
);
1588 case ZEBRA_IPV6_ROUTE_ADD
:
1589 zread_ipv6_add (client
, length
);
1591 case ZEBRA_IPV6_ROUTE_DELETE
:
1592 zread_ipv6_delete (client
, length
);
1594 #endif /* HAVE_IPV6 */
1595 case ZEBRA_REDISTRIBUTE_ADD
:
1596 zebra_redistribute_add (command
, client
, length
);
1598 case ZEBRA_REDISTRIBUTE_DELETE
:
1599 zebra_redistribute_delete (command
, client
, length
);
1601 case ZEBRA_REDISTRIBUTE_DEFAULT_ADD
:
1602 zebra_redistribute_default_add (command
, client
, length
);
1604 case ZEBRA_REDISTRIBUTE_DEFAULT_DELETE
:
1605 zebra_redistribute_default_delete (command
, client
, length
);
1607 case ZEBRA_IPV4_NEXTHOP_LOOKUP
:
1608 zread_ipv4_nexthop_lookup (client
, length
);
1611 case ZEBRA_IPV6_NEXTHOP_LOOKUP
:
1612 zread_ipv6_nexthop_lookup (client
, length
);
1614 #endif /* HAVE_IPV6 */
1615 case ZEBRA_IPV4_IMPORT_LOOKUP
:
1616 zread_ipv4_import_lookup (client
, length
);
1619 zread_hello (client
);
1621 case ZEBRA_NEXTHOP_REGISTER
:
1622 zserv_nexthop_register(client
, sock
, length
);
1624 case ZEBRA_NEXTHOP_UNREGISTER
:
1625 zserv_nexthop_unregister(client
, sock
, length
);
1628 zlog_info ("Zebra received unknown command %d", command
);
1632 if (client
->t_suicide
)
1634 /* No need to wait for thread callback, just kill immediately. */
1635 zebra_client_close(client
);
1639 stream_reset (client
->ibuf
);
1640 zebra_event (ZEBRA_READ
, sock
, client
);
1645 /* Accept code of zebra server socket. */
1647 zebra_accept (struct thread
*thread
)
1651 struct sockaddr_in client
;
1654 accept_sock
= THREAD_FD (thread
);
1656 /* Reregister myself. */
1657 zebra_event (ZEBRA_SERV
, accept_sock
, NULL
);
1659 len
= sizeof (struct sockaddr_in
);
1660 client_sock
= accept (accept_sock
, (struct sockaddr
*) &client
, &len
);
1662 if (client_sock
< 0)
1664 zlog_warn ("Can't accept zebra socket: %s", safe_strerror (errno
));
1668 /* Make client socket non-blocking. */
1669 set_nonblocking(client_sock
);
1671 /* Create new zebra client. */
1672 zebra_client_create (client_sock
);
1677 #ifdef HAVE_TCP_ZEBRA
1678 /* Make zebra's server socket. */
1684 struct sockaddr_in addr
;
1686 accept_sock
= socket (AF_INET
, SOCK_STREAM
, 0);
1688 if (accept_sock
< 0)
1690 zlog_warn ("Can't create zserv stream socket: %s",
1691 safe_strerror (errno
));
1692 zlog_warn ("zebra can't provice full functionality due to above error");
1696 memset (&route_type_oaths
, 0, sizeof (route_type_oaths
));
1697 memset (&addr
, 0, sizeof (struct sockaddr_in
));
1698 addr
.sin_family
= AF_INET
;
1699 addr
.sin_port
= htons (ZEBRA_PORT
);
1700 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
1701 addr
.sin_len
= sizeof (struct sockaddr_in
);
1702 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
1703 addr
.sin_addr
.s_addr
= htonl (INADDR_LOOPBACK
);
1705 sockopt_reuseaddr (accept_sock
);
1706 sockopt_reuseport (accept_sock
);
1708 if ( zserv_privs
.change(ZPRIVS_RAISE
) )
1709 zlog (NULL
, LOG_ERR
, "Can't raise privileges");
1711 ret
= bind (accept_sock
, (struct sockaddr
*)&addr
,
1712 sizeof (struct sockaddr_in
));
1715 zlog_warn ("Can't bind to stream socket: %s",
1716 safe_strerror (errno
));
1717 zlog_warn ("zebra can't provice full functionality due to above error");
1718 close (accept_sock
); /* Avoid sd leak. */
1722 if ( zserv_privs
.change(ZPRIVS_LOWER
) )
1723 zlog (NULL
, LOG_ERR
, "Can't lower privileges");
1725 ret
= listen (accept_sock
, 1);
1728 zlog_warn ("Can't listen to stream socket: %s",
1729 safe_strerror (errno
));
1730 zlog_warn ("zebra can't provice full functionality due to above error");
1731 close (accept_sock
); /* Avoid sd leak. */
1735 zebra_event (ZEBRA_SERV
, accept_sock
, NULL
);
1737 #endif /* HAVE_TCP_ZEBRA */
1739 /* For sockaddr_un. */
1742 /* zebra server UNIX domain socket. */
1744 zebra_serv_un (const char *path
)
1748 struct sockaddr_un serv
;
1751 /* First of all, unlink existing socket */
1755 old_mask
= umask (0077);
1757 /* Make UNIX domain socket. */
1758 sock
= socket (AF_UNIX
, SOCK_STREAM
, 0);
1761 zlog_warn ("Can't create zserv unix socket: %s",
1762 safe_strerror (errno
));
1763 zlog_warn ("zebra can't provide full functionality due to above error");
1767 memset (&route_type_oaths
, 0, sizeof (route_type_oaths
));
1769 /* Make server socket. */
1770 memset (&serv
, 0, sizeof (struct sockaddr_un
));
1771 serv
.sun_family
= AF_UNIX
;
1772 strncpy (serv
.sun_path
, path
, strlen (path
));
1773 #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
1774 len
= serv
.sun_len
= SUN_LEN(&serv
);
1776 len
= sizeof (serv
.sun_family
) + strlen (serv
.sun_path
);
1777 #endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
1779 ret
= bind (sock
, (struct sockaddr
*) &serv
, len
);
1782 zlog_warn ("Can't bind to unix socket %s: %s",
1783 path
, safe_strerror (errno
));
1784 zlog_warn ("zebra can't provide full functionality due to above error");
1789 ret
= listen (sock
, 5);
1792 zlog_warn ("Can't listen to unix socket %s: %s",
1793 path
, safe_strerror (errno
));
1794 zlog_warn ("zebra can't provide full functionality due to above error");
1801 zebra_event (ZEBRA_SERV
, sock
, NULL
);
1806 zebra_event (enum event event
, int sock
, struct zserv
*client
)
1811 thread_add_read (zebrad
.master
, zebra_accept
, client
, sock
);
1815 thread_add_read (zebrad
.master
, zebra_client_read
, client
, sock
);
1823 /* Display default rtm_table for all clients. */
1828 "default routing table to use for all clients\n")
1830 vty_out (vty
, "table %d%s", zebrad
.rtm_table_default
,
1835 DEFUN (config_table
,
1838 "Configure target kernel routing table\n"
1841 zebrad
.rtm_table_default
= strtol (argv
[0], (char**)0, 10);
1845 DEFUN (ip_forwarding
,
1849 "Turn on IP forwarding")
1855 ret
= ipforward_on ();
1859 vty_out (vty
, "Can't turn on IP forwarding%s", VTY_NEWLINE
);
1866 DEFUN (no_ip_forwarding
,
1867 no_ip_forwarding_cmd
,
1871 "Turn off IP forwarding")
1877 ret
= ipforward_off ();
1881 vty_out (vty
, "Can't turn off IP forwarding%s", VTY_NEWLINE
);
1888 /* This command is for debugging purpose. */
1889 DEFUN (show_zebra_client
,
1890 show_zebra_client_cmd
,
1891 "show zebra client",
1894 "Client information")
1896 struct listnode
*node
;
1897 struct zserv
*client
;
1899 for (ALL_LIST_ELEMENTS_RO (zebrad
.client_list
, node
, client
))
1900 vty_out (vty
, "Client %s fd %d%s",
1901 zebra_route_string(client
->proto
), client
->sock
,
1907 /* Table configuration write function. */
1909 config_write_table (struct vty
*vty
)
1911 if (zebrad
.rtm_table_default
)
1912 vty_out (vty
, "table %d%s", zebrad
.rtm_table_default
,
1917 /* table node for routing tables. */
1918 static struct cmd_node table_node
=
1921 "", /* This node has no interface. */
1925 /* Only display ip forwarding is enabled or not. */
1926 DEFUN (show_ip_forwarding
,
1927 show_ip_forwarding_cmd
,
1928 "show ip forwarding",
1931 "IP forwarding status\n")
1938 vty_out (vty
, "IP forwarding is off%s", VTY_NEWLINE
);
1940 vty_out (vty
, "IP forwarding is on%s", VTY_NEWLINE
);
1945 /* Only display ipv6 forwarding is enabled or not. */
1946 DEFUN (show_ipv6_forwarding
,
1947 show_ipv6_forwarding_cmd
,
1948 "show ipv6 forwarding",
1950 "IPv6 information\n"
1951 "Forwarding status\n")
1955 ret
= ipforward_ipv6 ();
1960 vty_out (vty
, "ipv6 forwarding is unknown%s", VTY_NEWLINE
);
1963 vty_out (vty
, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE
);
1966 vty_out (vty
, "ipv6 forwarding is %s%s", "on", VTY_NEWLINE
);
1969 vty_out (vty
, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE
);
1975 DEFUN (ipv6_forwarding
,
1976 ipv6_forwarding_cmd
,
1979 "Turn on IPv6 forwarding")
1983 ret
= ipforward_ipv6 ();
1985 ret
= ipforward_ipv6_on ();
1989 vty_out (vty
, "Can't turn on IPv6 forwarding%s", VTY_NEWLINE
);
1996 DEFUN (no_ipv6_forwarding
,
1997 no_ipv6_forwarding_cmd
,
1998 "no ipv6 forwarding",
2001 "Turn off IPv6 forwarding")
2005 ret
= ipforward_ipv6 ();
2007 ret
= ipforward_ipv6_off ();
2011 vty_out (vty
, "Can't turn off IPv6 forwarding%s", VTY_NEWLINE
);
2018 #endif /* HAVE_IPV6 */
2020 /* IPForwarding configuration write function. */
2022 config_write_forwarding (struct vty
*vty
)
2024 /* FIXME: Find better place for that. */
2025 router_id_write (vty
);
2028 vty_out (vty
, "ip forwarding%s", VTY_NEWLINE
);
2030 if (ipforward_ipv6 ())
2031 vty_out (vty
, "ipv6 forwarding%s", VTY_NEWLINE
);
2032 #endif /* HAVE_IPV6 */
2033 vty_out (vty
, "!%s", VTY_NEWLINE
);
2037 /* table node for routing tables. */
2038 static struct cmd_node forwarding_node
=
2041 "", /* This node has no interface. */
2046 /* Initialisation of zebra and installation of commands. */
2050 /* Client list init. */
2051 zebrad
.client_list
= list_new ();
2053 /* Install configuration write function. */
2054 install_node (&table_node
, config_write_table
);
2055 install_node (&forwarding_node
, config_write_forwarding
);
2057 install_element (VIEW_NODE
, &show_ip_forwarding_cmd
);
2058 install_element (ENABLE_NODE
, &show_ip_forwarding_cmd
);
2059 install_element (CONFIG_NODE
, &ip_forwarding_cmd
);
2060 install_element (CONFIG_NODE
, &no_ip_forwarding_cmd
);
2061 install_element (ENABLE_NODE
, &show_zebra_client_cmd
);
2064 install_element (VIEW_NODE
, &show_table_cmd
);
2065 install_element (ENABLE_NODE
, &show_table_cmd
);
2066 install_element (CONFIG_NODE
, &config_table_cmd
);
2067 #endif /* HAVE_NETLINK */
2070 install_element (VIEW_NODE
, &show_ipv6_forwarding_cmd
);
2071 install_element (ENABLE_NODE
, &show_ipv6_forwarding_cmd
);
2072 install_element (CONFIG_NODE
, &ipv6_forwarding_cmd
);
2073 install_element (CONFIG_NODE
, &no_ipv6_forwarding_cmd
);
2074 #endif /* HAVE_IPV6 */
2077 zebra_route_map_init ();
2080 /* Make zebra server socket, wiping any existing one (see bug #403). */
2082 zebra_zserv_socket_init (char *path
)
2084 #ifdef HAVE_TCP_ZEBRA
2087 zebra_serv_un (path
? path
: ZEBRA_SERV_PATH
);
2088 #endif /* HAVE_TCP_ZEBRA */