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.
30 #include "zebra_memory.h"
34 #include "sockunion.h"
43 #include "zebra/zserv.h"
44 #include "zebra/zebra_ns.h"
45 #include "zebra/zebra_vrf.h"
46 #include "zebra/router-id.h"
47 #include "zebra/redistribute.h"
48 #include "zebra/debug.h"
49 #include "zebra/ipforward.h"
50 #include "zebra/zebra_rnh.h"
51 #include "zebra/rt_netlink.h"
52 #include "zebra/interface.h"
53 #include "zebra/zebra_ptm.h"
54 #include "zebra/rtadv.h"
55 #include "zebra/zebra_mpls.h"
56 #include "zebra/zebra_mroute.h"
57 #include "zebra/label_manager.h"
59 /* Event list of zebra. */
60 enum event
{ ZEBRA_SERV
, ZEBRA_READ
, ZEBRA_WRITE
};
62 static void zebra_event(enum event event
, int sock
, struct zserv
*client
);
64 extern struct zebra_privs_t zserv_privs
;
66 static void zebra_client_close(struct zserv
*client
);
68 static int zserv_delayed_close(struct thread
*thread
)
70 struct zserv
*client
= THREAD_ARG(thread
);
72 client
->t_suicide
= NULL
;
73 zebra_client_close(client
);
77 static int zserv_flush_data(struct thread
*thread
)
79 struct zserv
*client
= THREAD_ARG(thread
);
81 client
->t_write
= NULL
;
82 if (client
->t_suicide
) {
83 zebra_client_close(client
);
86 switch (buffer_flush_available(client
->wb
, client
->sock
)) {
89 "%s: buffer_flush_available failed on zserv client fd %d, "
91 __func__
, client
->sock
);
92 zebra_client_close(client
);
96 client
->t_write
= thread_add_write(
97 zebrad
.master
, zserv_flush_data
, client
, client
->sock
);
104 client
->last_write_time
= monotime(NULL
);
108 int zebra_server_send_message(struct zserv
*client
)
110 if (client
->t_suicide
)
113 if (client
->is_synchronous
)
116 stream_set_getp(client
->obuf
, 0);
117 client
->last_write_cmd
= stream_getw_from(client
->obuf
, 6);
118 switch (buffer_write(client
->wb
, client
->sock
,
119 STREAM_DATA(client
->obuf
),
120 stream_get_endp(client
->obuf
))) {
123 "%s: buffer_write failed to zserv client fd %d, closing",
124 __func__
, client
->sock
);
125 /* Schedule a delayed close since many of the functions that
127 one do not check the return code. They do not allow for the
128 possibility that an I/O error may have caused the client to
131 client
->t_suicide
= thread_add_event(
132 zebrad
.master
, zserv_delayed_close
, client
, 0);
135 THREAD_OFF(client
->t_write
);
138 THREAD_WRITE_ON(zebrad
.master
, client
->t_write
,
139 zserv_flush_data
, client
, client
->sock
);
143 client
->last_write_time
= monotime(NULL
);
147 void zserv_create_header(struct stream
*s
, uint16_t cmd
, vrf_id_t vrf_id
)
149 /* length placeholder, caller can update */
150 stream_putw(s
, ZEBRA_HEADER_SIZE
);
151 stream_putc(s
, ZEBRA_HEADER_MARKER
);
152 stream_putc(s
, ZSERV_VERSION
);
153 stream_putw(s
, vrf_id
);
157 static void zserv_encode_interface(struct stream
*s
, struct interface
*ifp
)
159 /* Interface information. */
160 stream_put(s
, ifp
->name
, INTERFACE_NAMSIZ
);
161 stream_putl(s
, ifp
->ifindex
);
162 stream_putc(s
, ifp
->status
);
163 stream_putq(s
, ifp
->flags
);
164 stream_putc(s
, ifp
->ptm_enable
);
165 stream_putc(s
, ifp
->ptm_status
);
166 stream_putl(s
, ifp
->metric
);
167 stream_putl(s
, ifp
->speed
);
168 stream_putl(s
, ifp
->mtu
);
169 stream_putl(s
, ifp
->mtu6
);
170 stream_putl(s
, ifp
->bandwidth
);
171 stream_putl(s
, ifp
->ll_type
);
172 stream_putl(s
, ifp
->hw_addr_len
);
173 if (ifp
->hw_addr_len
)
174 stream_put(s
, ifp
->hw_addr
, ifp
->hw_addr_len
);
176 /* Then, Traffic Engineering parameters if any */
177 if (HAS_LINK_PARAMS(ifp
) && IS_LINK_PARAMS_SET(ifp
->link_params
)) {
179 zebra_interface_link_params_write(s
, ifp
);
183 /* Write packet size. */
184 stream_putw_at(s
, 0, stream_get_endp(s
));
187 static void zserv_encode_vrf(struct stream
*s
, struct zebra_vrf
*zvrf
)
189 /* Interface information. */
190 stream_put(s
, zvrf_name(zvrf
), VRF_NAMSIZ
);
192 /* Write packet size. */
193 stream_putw_at(s
, 0, stream_get_endp(s
));
196 /* Interface is added. Send ZEBRA_INTERFACE_ADD to client. */
198 * This function is called in the following situations:
199 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
201 * - at startup, when zebra figures out the available interfaces
202 * - when an interface is added (where support for
203 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
204 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
207 int zsend_interface_add(struct zserv
*client
, struct interface
*ifp
)
214 zserv_create_header(s
, ZEBRA_INTERFACE_ADD
, ifp
->vrf_id
);
215 zserv_encode_interface(s
, ifp
);
218 return zebra_server_send_message(client
);
221 /* Interface deletion from zebra daemon. */
222 int zsend_interface_delete(struct zserv
*client
, struct interface
*ifp
)
229 zserv_create_header(s
, ZEBRA_INTERFACE_DELETE
, ifp
->vrf_id
);
230 zserv_encode_interface(s
, ifp
);
233 return zebra_server_send_message(client
);
236 int zsend_vrf_add(struct zserv
*client
, struct zebra_vrf
*zvrf
)
243 zserv_create_header(s
, ZEBRA_VRF_ADD
, zvrf_id(zvrf
));
244 zserv_encode_vrf(s
, zvrf
);
246 client
->vrfadd_cnt
++;
247 return zebra_server_send_message(client
);
250 /* VRF deletion from zebra daemon. */
251 int zsend_vrf_delete(struct zserv
*client
, struct zebra_vrf
*zvrf
)
258 zserv_create_header(s
, ZEBRA_VRF_DELETE
, zvrf_id(zvrf
));
259 zserv_encode_vrf(s
, zvrf
);
261 client
->vrfdel_cnt
++;
262 return zebra_server_send_message(client
);
265 int zsend_interface_link_params(struct zserv
*client
, struct interface
*ifp
)
269 /* Check this client need interface information. */
273 if (!ifp
->link_params
)
278 zserv_create_header(s
, ZEBRA_INTERFACE_LINK_PARAMS
, ifp
->vrf_id
);
280 /* Add Interface Index */
281 stream_putl(s
, ifp
->ifindex
);
283 /* Then TE Link Parameters */
284 if (zebra_interface_link_params_write(s
, ifp
) == 0)
287 /* Write packet size. */
288 stream_putw_at(s
, 0, stream_get_endp(s
));
290 return zebra_server_send_message(client
);
293 /* Interface address is added/deleted. Send ZEBRA_INTERFACE_ADDRESS_ADD or
294 * ZEBRA_INTERFACE_ADDRESS_DELETE to the client.
296 * A ZEBRA_INTERFACE_ADDRESS_ADD is sent in the following situations:
297 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
298 * from the client, after the ZEBRA_INTERFACE_ADD has been
299 * sent from zebra to the client
300 * - redistribute new address info to all clients in the following situations
301 * - at startup, when zebra figures out the available interfaces
302 * - when an interface is added (where support for
303 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
304 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
306 * - for the vty commands "ip address A.B.C.D/M [<secondary>|<label LINE>]"
307 * and "no bandwidth <1-10000000>", "ipv6 address X:X::X:X/M"
308 * - when an RTM_NEWADDR message is received from the kernel,
310 * The call tree that triggers ZEBRA_INTERFACE_ADDRESS_DELETE:
312 * zsend_interface_address(DELETE)
315 * zebra_interface_address_delete_update
317 * | | if_delete_update
319 * ip_address_uninstall connected_delete_ipv4
320 * [ipv6_addresss_uninstall] [connected_delete_ipv6]
323 * | RTM_NEWADDR on routing/netlink socket
326 * "no ip address A.B.C.D/M [label LINE]"
327 * "no ip address A.B.C.D/M secondary"
328 * ["no ipv6 address X:X::X:X/M"]
331 int zsend_interface_address(int cmd
, struct zserv
*client
,
332 struct interface
*ifp
, struct connected
*ifc
)
341 zserv_create_header(s
, cmd
, ifp
->vrf_id
);
342 stream_putl(s
, ifp
->ifindex
);
344 /* Interface address flag. */
345 stream_putc(s
, ifc
->flags
);
347 /* Prefix information. */
349 stream_putc(s
, p
->family
);
350 blen
= prefix_blen(p
);
351 stream_put(s
, &p
->u
.prefix
, blen
);
354 * XXX gnu version does not send prefixlen for
355 * ZEBRA_INTERFACE_ADDRESS_DELETE
356 * but zebra_interface_address_delete_read() in the gnu version
359 stream_putc(s
, p
->prefixlen
);
362 p
= ifc
->destination
;
364 stream_put(s
, &p
->u
.prefix
, blen
);
366 stream_put(s
, NULL
, blen
);
368 /* Write packet size. */
369 stream_putw_at(s
, 0, stream_get_endp(s
));
371 client
->connected_rt_add_cnt
++;
372 return zebra_server_send_message(client
);
375 static int zsend_interface_nbr_address(int cmd
, struct zserv
*client
,
376 struct interface
*ifp
,
377 struct nbr_connected
*ifc
)
386 zserv_create_header(s
, cmd
, ifp
->vrf_id
);
387 stream_putl(s
, ifp
->ifindex
);
389 /* Prefix information. */
391 stream_putc(s
, p
->family
);
392 blen
= prefix_blen(p
);
393 stream_put(s
, &p
->u
.prefix
, blen
);
396 * XXX gnu version does not send prefixlen for
397 * ZEBRA_INTERFACE_ADDRESS_DELETE
398 * but zebra_interface_address_delete_read() in the gnu version
401 stream_putc(s
, p
->prefixlen
);
403 /* Write packet size. */
404 stream_putw_at(s
, 0, stream_get_endp(s
));
406 return zebra_server_send_message(client
);
409 /* Interface address addition. */
410 static void zebra_interface_nbr_address_add_update(struct interface
*ifp
,
411 struct nbr_connected
*ifc
)
413 struct listnode
*node
, *nnode
;
414 struct zserv
*client
;
417 if (IS_ZEBRA_DEBUG_EVENT
) {
418 char buf
[INET6_ADDRSTRLEN
];
422 "MESSAGE: ZEBRA_INTERFACE_NBR_ADDRESS_ADD %s/%d on %s",
423 inet_ntop(p
->family
, &p
->u
.prefix
, buf
,
425 p
->prefixlen
, ifc
->ifp
->name
);
428 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, node
, nnode
, client
))
429 zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_ADD
,
433 /* Interface address deletion. */
434 static void zebra_interface_nbr_address_delete_update(struct interface
*ifp
,
435 struct nbr_connected
*ifc
)
437 struct listnode
*node
, *nnode
;
438 struct zserv
*client
;
441 if (IS_ZEBRA_DEBUG_EVENT
) {
442 char buf
[INET6_ADDRSTRLEN
];
446 "MESSAGE: ZEBRA_INTERFACE_NBR_ADDRESS_DELETE %s/%d on %s",
447 inet_ntop(p
->family
, &p
->u
.prefix
, buf
,
449 p
->prefixlen
, ifc
->ifp
->name
);
452 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, node
, nnode
, client
))
453 zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
,
457 /* Send addresses on interface to client */
458 int zsend_interface_addresses(struct zserv
*client
, struct interface
*ifp
)
460 struct listnode
*cnode
, *cnnode
;
462 struct nbr_connected
*nc
;
464 /* Send interface addresses. */
465 for (ALL_LIST_ELEMENTS(ifp
->connected
, cnode
, cnnode
, c
)) {
466 if (!CHECK_FLAG(c
->conf
, ZEBRA_IFC_REAL
))
469 if (zsend_interface_address(ZEBRA_INTERFACE_ADDRESS_ADD
, client
,
475 /* Send interface neighbors. */
476 for (ALL_LIST_ELEMENTS(ifp
->nbr_connected
, cnode
, cnnode
, nc
)) {
477 if (zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_ADD
,
486 /* Notify client about interface moving from one VRF to another.
487 * Whether client is interested in old and new VRF is checked by caller.
489 int zsend_interface_vrf_update(struct zserv
*client
, struct interface
*ifp
,
497 zserv_create_header(s
, ZEBRA_INTERFACE_VRF_UPDATE
, ifp
->vrf_id
);
499 /* Fill in the ifIndex of the interface and its new VRF (id) */
500 stream_putl(s
, ifp
->ifindex
);
501 stream_putw(s
, vrf_id
);
503 /* Write packet size. */
504 stream_putw_at(s
, 0, stream_get_endp(s
));
506 client
->if_vrfchg_cnt
++;
507 return zebra_server_send_message(client
);
510 /* Add new nbr connected IPv6 address */
511 void nbr_connected_add_ipv6(struct interface
*ifp
, struct in6_addr
*address
)
513 struct nbr_connected
*ifc
;
517 IPV6_ADDR_COPY(&p
.u
.prefix
, address
);
518 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
520 if (!(ifc
= listnode_head(ifp
->nbr_connected
))) {
522 ifc
= nbr_connected_new();
523 ifc
->address
= prefix_new();
525 listnode_add(ifp
->nbr_connected
, ifc
);
528 prefix_copy(ifc
->address
, &p
);
530 zebra_interface_nbr_address_add_update(ifp
, ifc
);
532 if_nbr_ipv6ll_to_ipv4ll_neigh_update(ifp
, address
, 1);
535 void nbr_connected_delete_ipv6(struct interface
*ifp
, struct in6_addr
*address
)
537 struct nbr_connected
*ifc
;
541 IPV6_ADDR_COPY(&p
.u
.prefix
, address
);
542 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
544 ifc
= nbr_connected_check(ifp
, &p
);
548 listnode_delete(ifp
->nbr_connected
, ifc
);
550 zebra_interface_nbr_address_delete_update(ifp
, ifc
);
552 if_nbr_ipv6ll_to_ipv4ll_neigh_update(ifp
, address
, 0);
554 nbr_connected_free(ifc
);
558 * The cmd passed to zsend_interface_update may be ZEBRA_INTERFACE_UP or
559 * ZEBRA_INTERFACE_DOWN.
561 * The ZEBRA_INTERFACE_UP message is sent from the zebra server to
562 * the clients in one of 2 situations:
563 * - an if_up is detected e.g., as a result of an RTM_IFINFO message
564 * - a vty command modifying the bandwidth of an interface is received.
565 * The ZEBRA_INTERFACE_DOWN message is sent when an if_down is detected.
567 int zsend_interface_update(int cmd
, struct zserv
*client
, struct interface
*ifp
)
574 zserv_create_header(s
, cmd
, ifp
->vrf_id
);
575 zserv_encode_interface(s
, ifp
);
577 if (cmd
== ZEBRA_INTERFACE_UP
)
580 client
->ifdown_cnt
++;
582 return zebra_server_send_message(client
);
586 * This is the new function to announce and withdraw redistributed routes, used
587 * by Zebra. This is the old zsend_route_multipath() function. That function
588 * was duplicating code to send a lot of information that was essentially thrown
589 * away or ignored by the receiver. This is the leaner function that is not a
590 * duplicate of the zapi_ipv4_route_add/del.
592 * The primary difference is that this function merely sends a single NH instead
596 int zsend_redistribute_route(int add
, struct zserv
*client
, struct prefix
*p
,
597 struct prefix
*src_p
, struct rib
*rib
)
603 struct nexthop
*nexthop
;
604 unsigned long nhnummark
= 0, messmark
= 0;
606 u_char zapi_flags
= 0;
607 struct nexthop dummy_nh
;
609 afi
= family2afi(p
->family
);
613 cmd
= ZEBRA_REDISTRIBUTE_IPV4_ADD
;
614 client
->redist_v4_add_cnt
++;
617 cmd
= ZEBRA_REDISTRIBUTE_IPV6_ADD
;
618 client
->redist_v6_add_cnt
++;
626 cmd
= ZEBRA_REDISTRIBUTE_IPV4_DEL
;
627 client
->redist_v4_del_cnt
++;
630 cmd
= ZEBRA_REDISTRIBUTE_IPV6_DEL
;
631 client
->redist_v6_del_cnt
++;
640 memset(&dummy_nh
, 0, sizeof(struct nexthop
));
642 zserv_create_header(s
, cmd
, rib
->vrf_id
);
644 /* Put type and nexthop. */
645 stream_putc(s
, rib
->type
);
646 stream_putw(s
, rib
->instance
);
647 stream_putl(s
, rib
->flags
);
649 /* marker for message flags field */
650 messmark
= stream_get_endp(s
);
654 psize
= PSIZE(p
->prefixlen
);
655 stream_putc(s
, p
->prefixlen
);
656 stream_write(s
, (u_char
*)&p
->u
.prefix
, psize
);
659 SET_FLAG(zapi_flags
, ZAPI_MESSAGE_SRCPFX
);
660 psize
= PSIZE(src_p
->prefixlen
);
661 stream_putc(s
, src_p
->prefixlen
);
662 stream_write(s
, (u_char
*)&src_p
->u
.prefix
, psize
);
665 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
) {
666 /* We don't send any nexthops when there's a multipath */
667 if (rib
->nexthop_active_num
> 1
668 && client
->proto
!= ZEBRA_ROUTE_LDP
) {
669 SET_FLAG(zapi_flags
, ZAPI_MESSAGE_NEXTHOP
);
670 SET_FLAG(zapi_flags
, ZAPI_MESSAGE_IFINDEX
);
673 if (p
->family
== AF_INET
) {
674 stream_put_in_addr(s
, &dummy_nh
.gate
.ipv4
);
675 } else if (p
->family
== AF_INET6
) {
676 stream_write(s
, (u_char
*)&dummy_nh
.gate
.ipv6
,
679 /* We don't handle anything else now, abort */
681 "%s: Unable to redistribute route of unknown family, %d\n",
682 __func__
, p
->family
);
686 stream_putl(s
, 0); /* dummy ifindex */
690 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
)) {
691 SET_FLAG(zapi_flags
, ZAPI_MESSAGE_NEXTHOP
);
692 SET_FLAG(zapi_flags
, ZAPI_MESSAGE_IFINDEX
);
693 if (nhnummark
== 0) {
694 nhnummark
= stream_get_endp(s
);
695 stream_putc(s
, 1); /* placeholder */
699 switch (nexthop
->type
) {
700 case NEXTHOP_TYPE_IPV4
:
701 case NEXTHOP_TYPE_IPV4_IFINDEX
:
702 stream_put_in_addr(s
, &nexthop
->gate
.ipv4
);
704 case NEXTHOP_TYPE_IPV6
:
705 case NEXTHOP_TYPE_IPV6_IFINDEX
:
706 /* Only BGP supports IPv4 prefix with IPv6 NH,
708 if (p
->family
== AF_INET
)
709 stream_put_in_addr(s
,
710 &dummy_nh
.gate
.ipv4
);
714 (u_char
*)&nexthop
->gate
.ipv6
,
718 if (cmd
== ZEBRA_REDISTRIBUTE_IPV4_ADD
719 || cmd
== ZEBRA_REDISTRIBUTE_IPV4_DEL
) {
720 struct in_addr empty
;
722 sizeof(struct in_addr
));
723 stream_write(s
, (u_char
*)&empty
,
726 struct in6_addr empty
;
728 sizeof(struct in6_addr
));
729 stream_write(s
, (u_char
*)&empty
,
734 /* Interface index. */
736 stream_putl(s
, nexthop
->ifindex
);
738 /* ldpd needs all nexthops */
739 if (client
->proto
!= ZEBRA_ROUTE_LDP
)
745 SET_FLAG(zapi_flags
, ZAPI_MESSAGE_DISTANCE
);
746 stream_putc(s
, rib
->distance
);
749 SET_FLAG(zapi_flags
, ZAPI_MESSAGE_METRIC
);
750 stream_putl(s
, rib
->metric
);
754 SET_FLAG(zapi_flags
, ZAPI_MESSAGE_TAG
);
755 stream_putl(s
, rib
->tag
);
759 SET_FLAG(zapi_flags
, ZAPI_MESSAGE_MTU
);
760 stream_putl(s
, rib
->mtu
);
762 /* write real message flags value */
763 stream_putc_at(s
, messmark
, zapi_flags
);
765 /* Write next-hop number */
767 stream_putc_at(s
, nhnummark
, nhnum
);
769 /* Write packet size. */
770 stream_putw_at(s
, 0, stream_get_endp(s
));
772 return zebra_server_send_message(client
);
775 static int zsend_write_nexthop(struct stream
*s
, struct nexthop
*nexthop
)
777 stream_putc(s
, nexthop
->type
);
778 switch (nexthop
->type
) {
779 case NEXTHOP_TYPE_IPV4
:
780 case NEXTHOP_TYPE_IPV4_IFINDEX
:
781 stream_put_in_addr(s
, &nexthop
->gate
.ipv4
);
782 stream_putl(s
, nexthop
->ifindex
);
784 case NEXTHOP_TYPE_IPV6
:
785 stream_put(s
, &nexthop
->gate
.ipv6
, 16);
787 case NEXTHOP_TYPE_IPV6_IFINDEX
:
788 stream_put(s
, &nexthop
->gate
.ipv6
, 16);
789 stream_putl(s
, nexthop
->ifindex
);
791 case NEXTHOP_TYPE_IFINDEX
:
792 stream_putl(s
, nexthop
->ifindex
);
801 /* Nexthop register */
802 static int zserv_rnh_register(struct zserv
*client
, int sock
, u_short length
,
803 rnh_type_t type
, struct zebra_vrf
*zvrf
)
811 if (IS_ZEBRA_DEBUG_NHT
)
813 "rnh_register msg from client %s: length=%d, type=%s\n",
814 zebra_route_string(client
->proto
), length
,
815 (type
== RNH_NEXTHOP_TYPE
) ? "nexthop" : "route");
819 client
->nh_reg_time
= monotime(NULL
);
822 flags
= stream_getc(s
);
823 p
.family
= stream_getw(s
);
824 p
.prefixlen
= stream_getc(s
);
826 if (p
.family
== AF_INET
) {
827 p
.u
.prefix4
.s_addr
= stream_get_ipv4(s
);
828 l
+= IPV4_MAX_BYTELEN
;
829 } else if (p
.family
== AF_INET6
) {
830 stream_get(&p
.u
.prefix6
, s
, IPV6_MAX_BYTELEN
);
831 l
+= IPV6_MAX_BYTELEN
;
834 "rnh_register: Received unknown family type %d\n",
838 rnh
= zebra_add_rnh(&p
, zvrf_id(zvrf
), type
);
839 if (type
== RNH_NEXTHOP_TYPE
) {
841 && !CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
))
842 SET_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
);
844 && CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
))
845 UNSET_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
);
846 } else if (type
== RNH_IMPORT_CHECK_TYPE
) {
848 && !CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
))
849 SET_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
);
850 else if (!flags
&& CHECK_FLAG(rnh
->flags
,
851 ZEBRA_NHT_EXACT_MATCH
))
852 UNSET_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
);
855 zebra_add_rnh_client(rnh
, client
, type
, zvrf_id(zvrf
));
856 /* Anything not AF_INET/INET6 has been filtered out above */
857 zebra_evaluate_rnh(zvrf_id(zvrf
), p
.family
, 1, type
, &p
);
862 /* Nexthop register */
863 static int zserv_rnh_unregister(struct zserv
*client
, int sock
, u_short length
,
864 rnh_type_t type
, struct zebra_vrf
*zvrf
)
871 if (IS_ZEBRA_DEBUG_NHT
)
872 zlog_debug("rnh_unregister msg from client %s: length=%d\n",
873 zebra_route_string(client
->proto
), length
);
879 s
); // Connected or not. Not used in this function
880 p
.family
= stream_getw(s
);
881 p
.prefixlen
= stream_getc(s
);
883 if (p
.family
== AF_INET
) {
884 p
.u
.prefix4
.s_addr
= stream_get_ipv4(s
);
885 l
+= IPV4_MAX_BYTELEN
;
886 } else if (p
.family
== AF_INET6
) {
887 stream_get(&p
.u
.prefix6
, s
, IPV6_MAX_BYTELEN
);
888 l
+= IPV6_MAX_BYTELEN
;
891 "rnh_register: Received unknown family type %d\n",
895 rnh
= zebra_lookup_rnh(&p
, zvrf_id(zvrf
), type
);
897 client
->nh_dereg_time
= monotime(NULL
);
898 zebra_remove_rnh_client(rnh
, client
, type
);
905 Modified version of zsend_ipv4_nexthop_lookup():
906 Query unicast rib if nexthop is not found on mrib.
907 Returns both route metric and protocol distance.
909 static int zsend_ipv4_nexthop_lookup_mrib(struct zserv
*client
,
910 struct in_addr addr
, struct rib
*rib
,
911 struct zebra_vrf
*zvrf
)
916 struct nexthop
*nexthop
;
918 /* Get output stream. */
922 /* Fill in result. */
923 zserv_create_header(s
, ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB
, zvrf_id(zvrf
));
924 stream_put_in_addr(s
, &addr
);
927 stream_putc(s
, rib
->distance
);
928 stream_putl(s
, rib
->metric
);
930 nump
= stream_get_endp(
931 s
); /* remember position for nexthop_num */
932 stream_putc(s
, 0); /* reserve room for nexthop_num */
933 /* Only non-recursive routes are elegible to resolve the nexthop
935 * are looking up. Therefore, we will just iterate over the top
936 * chain of nexthops. */
937 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
938 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
939 num
+= zsend_write_nexthop(s
, nexthop
);
941 stream_putc_at(s
, nump
, num
); /* store nexthop_num */
943 stream_putc(s
, 0); /* distance */
944 stream_putl(s
, 0); /* metric */
945 stream_putc(s
, 0); /* nexthop_num */
948 stream_putw_at(s
, 0, stream_get_endp(s
));
950 return zebra_server_send_message(client
);
953 /* Router-id is updated. Send ZEBRA_ROUTER_ID_ADD to client. */
954 int zsend_router_id_update(struct zserv
*client
, struct prefix
*p
,
960 /* Check this client need interface information. */
961 if (!vrf_bitmap_check(client
->ridinfo
, vrf_id
))
968 zserv_create_header(s
, ZEBRA_ROUTER_ID_UPDATE
, vrf_id
);
970 /* Prefix information. */
971 stream_putc(s
, p
->family
);
972 blen
= prefix_blen(p
);
973 stream_put(s
, &p
->u
.prefix
, blen
);
974 stream_putc(s
, p
->prefixlen
);
976 /* Write packet size. */
977 stream_putw_at(s
, 0, stream_get_endp(s
));
979 return zebra_server_send_message(client
);
983 * Function used by Zebra to send a PW status update to LDP daemon
985 int zsend_pw_update(struct zserv
*client
, struct zebra_pw
*pw
)
992 zserv_create_header(s
, ZEBRA_PW_STATUS_UPDATE
, pw
->vrf_id
);
993 stream_write(s
, pw
->ifname
, IF_NAMESIZE
);
994 stream_putl(s
, pw
->ifindex
);
995 stream_putl(s
, pw
->status
);
997 /* Put length at the first point of the stream. */
998 stream_putw_at(s
, 0, stream_get_endp(s
));
1000 return zebra_server_send_message(client
);
1003 /* Register zebra server interface information. Send current all
1004 interface and address information. */
1005 static int zread_interface_add(struct zserv
*client
, u_short length
,
1006 struct zebra_vrf
*zvrf
)
1009 struct listnode
*ifnode
, *ifnnode
;
1010 struct interface
*ifp
;
1012 /* Interface information is needed. */
1013 vrf_bitmap_set(client
->ifinfo
, zvrf_id(zvrf
));
1015 RB_FOREACH(vrf
, vrf_id_head
, &vrfs_by_id
)
1017 for (ALL_LIST_ELEMENTS(vrf
->iflist
, ifnode
, ifnnode
, ifp
)) {
1018 /* Skip pseudo interface. */
1019 if (!CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_ACTIVE
))
1022 if (zsend_interface_add(client
, ifp
) < 0)
1025 if (zsend_interface_addresses(client
, ifp
) < 0)
1032 /* Unregister zebra server interface information. */
1033 static int zread_interface_delete(struct zserv
*client
, u_short length
,
1034 struct zebra_vrf
*zvrf
)
1036 vrf_bitmap_unset(client
->ifinfo
, zvrf_id(zvrf
));
1040 void zserv_nexthop_num_warn(const char *caller
, const struct prefix
*p
,
1041 const unsigned int nexthop_num
)
1043 if (nexthop_num
> multipath_num
) {
1044 char buff
[PREFIX2STR_BUFFER
];
1045 prefix2str(p
, buff
, sizeof(buff
));
1047 "%s: Prefix %s has %d nexthops, but we can only use the first %d",
1048 caller
, buff
, nexthop_num
, multipath_num
);
1052 /* This function support multiple nexthop. */
1054 * Parse the ZEBRA_IPV4_ROUTE_ADD sent from client. Update rib and
1057 static int zread_ipv4_add(struct zserv
*client
, u_short length
,
1058 struct zebra_vrf
*zvrf
)
1064 struct in_addr nexthop
;
1066 u_char nexthop_type
;
1072 /* Get input stream. */
1075 /* Allocate new rib. */
1076 rib
= XCALLOC(MTYPE_RIB
, sizeof(struct rib
));
1078 /* Type, flags, message. */
1079 rib
->type
= stream_getc(s
);
1080 rib
->instance
= stream_getw(s
);
1081 rib
->flags
= stream_getl(s
);
1082 message
= stream_getc(s
);
1083 safi
= stream_getw(s
);
1084 rib
->uptime
= time(NULL
);
1087 memset(&p
, 0, sizeof(struct prefix_ipv4
));
1089 p
.prefixlen
= stream_getc(s
);
1090 stream_get(&p
.u
.prefix4
, s
, PSIZE(p
.prefixlen
));
1093 rib
->vrf_id
= zvrf_id(zvrf
);
1095 /* Nexthop parse. */
1096 if (CHECK_FLAG(message
, ZAPI_MESSAGE_NEXTHOP
)) {
1097 nexthop_num
= stream_getc(s
);
1098 zserv_nexthop_num_warn(__func__
, (const struct prefix
*)&p
,
1101 for (i
= 0; i
< nexthop_num
; i
++) {
1102 nexthop_type
= stream_getc(s
);
1104 switch (nexthop_type
) {
1105 case NEXTHOP_TYPE_IFINDEX
:
1106 ifindex
= stream_getl(s
);
1107 rib_nexthop_ifindex_add(rib
, ifindex
);
1109 case NEXTHOP_TYPE_IPV4
:
1110 nexthop
.s_addr
= stream_get_ipv4(s
);
1111 rib_nexthop_ipv4_add(rib
, &nexthop
, NULL
);
1113 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1114 nexthop
.s_addr
= stream_get_ipv4(s
);
1115 ifindex
= stream_getl(s
);
1116 rib_nexthop_ipv4_ifindex_add(rib
, &nexthop
,
1119 case NEXTHOP_TYPE_IPV6
:
1120 stream_forward_getp(s
, IPV6_MAX_BYTELEN
);
1122 case NEXTHOP_TYPE_BLACKHOLE
:
1123 rib_nexthop_blackhole_add(rib
);
1130 if (CHECK_FLAG(message
, ZAPI_MESSAGE_DISTANCE
))
1131 rib
->distance
= stream_getc(s
);
1134 if (CHECK_FLAG(message
, ZAPI_MESSAGE_METRIC
))
1135 rib
->metric
= stream_getl(s
);
1138 if (CHECK_FLAG(message
, ZAPI_MESSAGE_TAG
))
1139 rib
->tag
= stream_getl(s
);
1143 if (CHECK_FLAG(message
, ZAPI_MESSAGE_MTU
))
1144 rib
->mtu
= stream_getl(s
);
1149 rib
->table
= zvrf
->table_id
;
1151 ret
= rib_add_multipath(AFI_IP
, safi
, &p
, NULL
, rib
);
1155 client
->v4_route_add_cnt
++;
1157 client
->v4_route_upd8_cnt
++;
1161 /* Zebra server IPv4 prefix delete function. */
1162 static int zread_ipv4_delete(struct zserv
*client
, u_short length
,
1163 struct zebra_vrf
*zvrf
)
1167 struct zapi_ipv4 api
;
1168 struct in_addr nexthop
;
1169 union g_addr
*nexthop_p
;
1170 unsigned long ifindex
;
1173 u_char nexthop_type
;
1181 /* Type, flags, message. */
1182 api
.type
= stream_getc(s
);
1183 api
.instance
= stream_getw(s
);
1184 api
.flags
= stream_getl(s
);
1185 api
.message
= stream_getc(s
);
1186 api
.safi
= stream_getw(s
);
1189 memset(&p
, 0, sizeof(struct prefix
));
1191 p
.prefixlen
= stream_getc(s
);
1192 stream_get(&p
.u
.prefix4
, s
, PSIZE(p
.prefixlen
));
1194 /* Nexthop, ifindex, distance, metric. */
1195 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
)) {
1196 nexthop_num
= stream_getc(s
);
1198 for (i
= 0; i
< nexthop_num
; i
++) {
1199 nexthop_type
= stream_getc(s
);
1201 switch (nexthop_type
) {
1202 case NEXTHOP_TYPE_IFINDEX
:
1203 ifindex
= stream_getl(s
);
1205 case NEXTHOP_TYPE_IPV4
:
1206 nexthop
.s_addr
= stream_get_ipv4(s
);
1207 nexthop_p
= (union g_addr
*)&nexthop
;
1209 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1210 nexthop
.s_addr
= stream_get_ipv4(s
);
1211 nexthop_p
= (union g_addr
*)&nexthop
;
1212 ifindex
= stream_getl(s
);
1214 case NEXTHOP_TYPE_IPV6
:
1215 stream_forward_getp(s
, IPV6_MAX_BYTELEN
);
1222 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
))
1223 api
.distance
= stream_getc(s
);
1228 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
))
1229 api
.metric
= stream_getl(s
);
1234 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_TAG
))
1235 api
.tag
= stream_getl(s
);
1239 table_id
= zvrf
->table_id
;
1241 rib_delete(AFI_IP
, api
.safi
, zvrf_id(zvrf
), api
.type
, api
.instance
,
1242 api
.flags
, &p
, NULL
, nexthop_p
, ifindex
, table_id
);
1243 client
->v4_route_del_cnt
++;
1247 /* MRIB Nexthop lookup for IPv4. */
1248 static int zread_ipv4_nexthop_lookup_mrib(struct zserv
*client
, u_short length
,
1249 struct zebra_vrf
*zvrf
)
1251 struct in_addr addr
;
1254 addr
.s_addr
= stream_get_ipv4(client
->ibuf
);
1255 rib
= rib_match_ipv4_multicast(zvrf_id(zvrf
), addr
, NULL
);
1256 return zsend_ipv4_nexthop_lookup_mrib(client
, addr
, rib
, zvrf
);
1259 /* Zebra server IPv6 prefix add function. */
1260 static int zread_ipv4_route_ipv6_nexthop_add(struct zserv
*client
,
1262 struct zebra_vrf
*zvrf
)
1266 struct in6_addr nexthop
;
1270 u_char nexthop_type
;
1273 static struct in6_addr nexthops
[MULTIPATH_NUM
];
1274 static unsigned int ifindices
[MULTIPATH_NUM
];
1277 /* Get input stream. */
1280 memset(&nexthop
, 0, sizeof(struct in6_addr
));
1282 /* Allocate new rib. */
1283 rib
= XCALLOC(MTYPE_RIB
, sizeof(struct rib
));
1285 /* Type, flags, message. */
1286 rib
->type
= stream_getc(s
);
1287 rib
->instance
= stream_getw(s
);
1288 rib
->flags
= stream_getl(s
);
1289 message
= stream_getc(s
);
1290 safi
= stream_getw(s
);
1291 rib
->uptime
= time(NULL
);
1294 memset(&p
, 0, sizeof(struct prefix_ipv4
));
1296 p
.prefixlen
= stream_getc(s
);
1297 stream_get(&p
.u
.prefix4
, s
, PSIZE(p
.prefixlen
));
1300 rib
->vrf_id
= zvrf_id(zvrf
);
1302 /* We need to give nh-addr, nh-ifindex with the same next-hop object
1303 * to the rib to ensure that IPv6 multipathing works; need to coalesce
1304 * these. Clients should send the same number of paired set of
1305 * next-hop-addr/next-hop-ifindices. */
1306 if (CHECK_FLAG(message
, ZAPI_MESSAGE_NEXTHOP
)) {
1307 unsigned int nh_count
= 0;
1308 unsigned int if_count
= 0;
1309 unsigned int max_nh_if
= 0;
1311 nexthop_num
= stream_getc(s
);
1312 zserv_nexthop_num_warn(__func__
, (const struct prefix
*)&p
,
1314 for (i
= 0; i
< nexthop_num
; i
++) {
1315 nexthop_type
= stream_getc(s
);
1317 switch (nexthop_type
) {
1318 case NEXTHOP_TYPE_IPV6
:
1319 stream_get(&nexthop
, s
, 16);
1320 if (nh_count
< multipath_num
) {
1321 nexthops
[nh_count
++] = nexthop
;
1324 case NEXTHOP_TYPE_IFINDEX
:
1325 if (if_count
< multipath_num
) {
1326 ifindices
[if_count
++] = stream_getl(s
);
1329 case NEXTHOP_TYPE_BLACKHOLE
:
1330 rib_nexthop_blackhole_add(rib
);
1335 max_nh_if
= (nh_count
> if_count
) ? nh_count
: if_count
;
1336 for (i
= 0; i
< max_nh_if
; i
++) {
1338 && !IN6_IS_ADDR_UNSPECIFIED(&nexthops
[i
])) {
1339 if ((i
< if_count
) && ifindices
[i
]) {
1340 rib_nexthop_ipv6_ifindex_add(
1344 rib_nexthop_ipv6_add(rib
, &nexthops
[i
]);
1347 if ((i
< if_count
) && ifindices
[i
]) {
1348 rib_nexthop_ifindex_add(rib
,
1356 if (CHECK_FLAG(message
, ZAPI_MESSAGE_DISTANCE
))
1357 rib
->distance
= stream_getc(s
);
1360 if (CHECK_FLAG(message
, ZAPI_MESSAGE_METRIC
))
1361 rib
->metric
= stream_getl(s
);
1364 if (CHECK_FLAG(message
, ZAPI_MESSAGE_TAG
))
1365 rib
->tag
= stream_getl(s
);
1369 if (CHECK_FLAG(message
, ZAPI_MESSAGE_MTU
))
1370 rib
->mtu
= stream_getl(s
);
1375 rib
->table
= zvrf
->table_id
;
1377 ret
= rib_add_multipath(AFI_IP6
, safi
, &p
, NULL
, rib
);
1380 client
->v4_route_add_cnt
++;
1382 client
->v4_route_upd8_cnt
++;
1387 static int zread_ipv6_add(struct zserv
*client
, u_short length
,
1388 struct zebra_vrf
*zvrf
)
1392 struct in6_addr nexthop
;
1396 u_char nexthop_type
;
1398 struct prefix_ipv6 src_p
, *src_pp
;
1400 static struct in6_addr nexthops
[MULTIPATH_NUM
];
1401 static unsigned int ifindices
[MULTIPATH_NUM
];
1404 /* Get input stream. */
1407 memset(&nexthop
, 0, sizeof(struct in6_addr
));
1409 /* Allocate new rib. */
1410 rib
= XCALLOC(MTYPE_RIB
, sizeof(struct rib
));
1412 /* Type, flags, message. */
1413 rib
->type
= stream_getc(s
);
1414 rib
->instance
= stream_getw(s
);
1415 rib
->flags
= stream_getl(s
);
1416 message
= stream_getc(s
);
1417 safi
= stream_getw(s
);
1418 rib
->uptime
= time(NULL
);
1421 memset(&p
, 0, sizeof(struct prefix_ipv6
));
1422 p
.family
= AF_INET6
;
1423 p
.prefixlen
= stream_getc(s
);
1424 stream_get(&p
.u
.prefix6
, s
, PSIZE(p
.prefixlen
));
1426 if (CHECK_FLAG(message
, ZAPI_MESSAGE_SRCPFX
)) {
1427 memset(&src_p
, 0, sizeof(struct prefix_ipv6
));
1428 src_p
.family
= AF_INET6
;
1429 src_p
.prefixlen
= stream_getc(s
);
1430 stream_get(&src_p
.prefix
, s
, PSIZE(src_p
.prefixlen
));
1435 /* We need to give nh-addr, nh-ifindex with the same next-hop object
1436 * to the rib to ensure that IPv6 multipathing works; need to coalesce
1437 * these. Clients should send the same number of paired set of
1438 * next-hop-addr/next-hop-ifindices. */
1439 if (CHECK_FLAG(message
, ZAPI_MESSAGE_NEXTHOP
)) {
1440 unsigned int nh_count
= 0;
1441 unsigned int if_count
= 0;
1442 unsigned int max_nh_if
= 0;
1444 nexthop_num
= stream_getc(s
);
1445 zserv_nexthop_num_warn(__func__
, (const struct prefix
*)&p
,
1447 for (i
= 0; i
< nexthop_num
; i
++) {
1448 nexthop_type
= stream_getc(s
);
1450 switch (nexthop_type
) {
1451 case NEXTHOP_TYPE_IPV6
:
1452 stream_get(&nexthop
, s
, 16);
1453 if (nh_count
< multipath_num
) {
1454 nexthops
[nh_count
++] = nexthop
;
1457 case NEXTHOP_TYPE_IFINDEX
:
1458 if (if_count
< multipath_num
) {
1459 ifindices
[if_count
++] = stream_getl(s
);
1462 case NEXTHOP_TYPE_BLACKHOLE
:
1463 rib_nexthop_blackhole_add(rib
);
1468 max_nh_if
= (nh_count
> if_count
) ? nh_count
: if_count
;
1469 for (i
= 0; i
< max_nh_if
; i
++) {
1471 && !IN6_IS_ADDR_UNSPECIFIED(&nexthops
[i
])) {
1472 if ((i
< if_count
) && ifindices
[i
])
1473 rib_nexthop_ipv6_ifindex_add(
1477 rib_nexthop_ipv6_add(rib
, &nexthops
[i
]);
1479 if ((i
< if_count
) && ifindices
[i
])
1480 rib_nexthop_ifindex_add(rib
,
1487 if (CHECK_FLAG(message
, ZAPI_MESSAGE_DISTANCE
))
1488 rib
->distance
= stream_getc(s
);
1491 if (CHECK_FLAG(message
, ZAPI_MESSAGE_METRIC
))
1492 rib
->metric
= stream_getl(s
);
1495 if (CHECK_FLAG(message
, ZAPI_MESSAGE_TAG
))
1496 rib
->tag
= stream_getl(s
);
1500 if (CHECK_FLAG(message
, ZAPI_MESSAGE_MTU
))
1501 rib
->mtu
= stream_getl(s
);
1506 rib
->vrf_id
= zvrf_id(zvrf
);
1507 rib
->table
= zvrf
->table_id
;
1509 ret
= rib_add_multipath(AFI_IP6
, safi
, &p
, src_pp
, rib
);
1512 client
->v6_route_add_cnt
++;
1514 client
->v6_route_upd8_cnt
++;
1519 /* Zebra server IPv6 prefix delete function. */
1520 static int zread_ipv6_delete(struct zserv
*client
, u_short length
,
1521 struct zebra_vrf
*zvrf
)
1525 struct zapi_ipv6 api
;
1526 struct in6_addr nexthop
;
1527 union g_addr
*pnexthop
= NULL
;
1528 unsigned long ifindex
;
1530 struct prefix_ipv6 src_p
, *src_pp
;
1534 memset(&nexthop
, 0, sizeof(struct in6_addr
));
1536 /* Type, flags, message. */
1537 api
.type
= stream_getc(s
);
1538 api
.instance
= stream_getw(s
);
1539 api
.flags
= stream_getl(s
);
1540 api
.message
= stream_getc(s
);
1541 api
.safi
= stream_getw(s
);
1544 memset(&p
, 0, sizeof(struct prefix_ipv6
));
1545 p
.family
= AF_INET6
;
1546 p
.prefixlen
= stream_getc(s
);
1547 stream_get(&p
.u
.prefix6
, s
, PSIZE(p
.prefixlen
));
1549 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
)) {
1550 memset(&src_p
, 0, sizeof(struct prefix_ipv6
));
1551 src_p
.family
= AF_INET6
;
1552 src_p
.prefixlen
= stream_getc(s
);
1553 stream_get(&src_p
.prefix
, s
, PSIZE(src_p
.prefixlen
));
1558 /* Nexthop, ifindex, distance, metric. */
1559 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
)) {
1560 u_char nexthop_type
;
1562 api
.nexthop_num
= stream_getc(s
);
1563 for (i
= 0; i
< api
.nexthop_num
; i
++) {
1564 nexthop_type
= stream_getc(s
);
1566 switch (nexthop_type
) {
1567 case NEXTHOP_TYPE_IPV6
:
1568 stream_get(&nexthop
, s
, 16);
1569 pnexthop
= (union g_addr
*)&nexthop
;
1571 case NEXTHOP_TYPE_IFINDEX
:
1572 ifindex
= stream_getl(s
);
1579 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
))
1580 api
.distance
= stream_getc(s
);
1585 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
))
1586 api
.metric
= stream_getl(s
);
1591 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_TAG
))
1592 api
.tag
= stream_getl(s
);
1596 if (IN6_IS_ADDR_UNSPECIFIED(&nexthop
))
1597 rib_delete(AFI_IP6
, api
.safi
, zvrf_id(zvrf
), api
.type
,
1598 api
.instance
, api
.flags
, &p
, src_pp
, NULL
, ifindex
,
1601 rib_delete(AFI_IP6
, api
.safi
, zvrf_id(zvrf
), api
.type
,
1602 api
.instance
, api
.flags
, &p
, src_pp
, pnexthop
,
1603 ifindex
, client
->rtm_table
);
1605 client
->v6_route_del_cnt
++;
1609 /* Register zebra server router-id information. Send current router-id */
1610 static int zread_router_id_add(struct zserv
*client
, u_short length
,
1611 struct zebra_vrf
*zvrf
)
1615 /* Router-id information is needed. */
1616 vrf_bitmap_set(client
->ridinfo
, zvrf_id(zvrf
));
1618 router_id_get(&p
, zvrf_id(zvrf
));
1620 return zsend_router_id_update(client
, &p
, zvrf_id(zvrf
));
1623 /* Unregister zebra server router-id information. */
1624 static int zread_router_id_delete(struct zserv
*client
, u_short length
,
1625 struct zebra_vrf
*zvrf
)
1627 vrf_bitmap_unset(client
->ridinfo
, zvrf_id(zvrf
));
1631 /* Tie up route-type and client->sock */
1632 static void zread_hello(struct zserv
*client
)
1634 /* type of protocol (lib/zebra.h) */
1638 proto
= stream_getc(client
->ibuf
);
1639 instance
= stream_getw(client
->ibuf
);
1641 /* accept only dynamic routing protocols */
1642 if ((proto
< ZEBRA_ROUTE_MAX
) && (proto
> ZEBRA_ROUTE_STATIC
)) {
1644 "client %d says hello and bids fair to announce only %s routes",
1645 client
->sock
, zebra_route_string(proto
));
1647 zlog_notice("client protocol instance %d", instance
);
1649 client
->proto
= proto
;
1650 client
->instance
= instance
;
1654 /* Unregister all information in a VRF. */
1655 static int zread_vrf_unregister(struct zserv
*client
, u_short length
,
1656 struct zebra_vrf
*zvrf
)
1661 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
1662 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
1663 vrf_bitmap_unset(client
->redist
[afi
][i
], zvrf_id(zvrf
));
1664 vrf_bitmap_unset(client
->redist_default
, zvrf_id(zvrf
));
1665 vrf_bitmap_unset(client
->ifinfo
, zvrf_id(zvrf
));
1666 vrf_bitmap_unset(client
->ridinfo
, zvrf_id(zvrf
));
1671 static void zread_mpls_labels(int command
, struct zserv
*client
, u_short length
,
1675 enum lsp_types_t type
;
1676 struct prefix prefix
;
1677 enum nexthop_types_t gtype
;
1680 mpls_label_t in_label
, out_label
;
1682 struct zebra_vrf
*zvrf
;
1684 zvrf
= vrf_info_lookup(vrf_id
);
1688 /* Get input stream. */
1692 type
= stream_getc(s
);
1693 prefix
.family
= stream_getl(s
);
1694 switch (prefix
.family
) {
1696 prefix
.u
.prefix4
.s_addr
= stream_get_ipv4(s
);
1697 prefix
.prefixlen
= stream_getc(s
);
1698 gate
.ipv4
.s_addr
= stream_get_ipv4(s
);
1701 stream_get(&prefix
.u
.prefix6
, s
, 16);
1702 prefix
.prefixlen
= stream_getc(s
);
1703 stream_get(&gate
.ipv6
, s
, 16);
1708 ifindex
= stream_getl(s
);
1709 distance
= stream_getc(s
);
1710 in_label
= stream_getl(s
);
1711 out_label
= stream_getl(s
);
1713 switch (prefix
.family
) {
1716 gtype
= NEXTHOP_TYPE_IPV4_IFINDEX
;
1718 gtype
= NEXTHOP_TYPE_IPV4
;
1722 gtype
= NEXTHOP_TYPE_IPV6_IFINDEX
;
1724 gtype
= NEXTHOP_TYPE_IPV6
;
1733 if (command
== ZEBRA_MPLS_LABELS_ADD
) {
1734 mpls_lsp_install(zvrf
, type
, in_label
, out_label
, gtype
, &gate
,
1736 mpls_ftn_update(1, zvrf
, type
, &prefix
, gtype
, &gate
, ifindex
,
1737 distance
, out_label
);
1738 } else if (command
== ZEBRA_MPLS_LABELS_DELETE
) {
1739 mpls_lsp_uninstall(zvrf
, type
, in_label
, gtype
, &gate
, NULL
,
1741 mpls_ftn_update(0, zvrf
, type
, &prefix
, gtype
, &gate
, ifindex
,
1742 distance
, out_label
);
1745 /* Send response to a label manager connect request to client */
1746 static int zsend_label_manager_connect_response(struct zserv
*client
,
1747 vrf_id_t vrf_id
, u_short result
)
1754 zserv_create_header(s
, ZEBRA_LABEL_MANAGER_CONNECT
, vrf_id
);
1757 stream_putc(s
, result
);
1759 /* Write packet size. */
1760 stream_putw_at(s
, 0, stream_get_endp(s
));
1762 return writen(client
->sock
, s
->data
, stream_get_endp(s
));
1765 static void zread_label_manager_connect(struct zserv
*client
, vrf_id_t vrf_id
)
1768 /* type of protocol (lib/zebra.h) */
1772 /* Get input stream. */
1776 proto
= stream_getc(s
);
1777 instance
= stream_getw(s
);
1779 /* accept only dynamic routing protocols */
1780 if ((proto
>= ZEBRA_ROUTE_MAX
) || (proto
<= ZEBRA_ROUTE_STATIC
)) {
1781 zlog_err("client %d has wrong protocol %s", client
->sock
,
1782 zebra_route_string(proto
));
1783 zsend_label_manager_connect_response(client
, vrf_id
, 1);
1786 zlog_notice("client %d with instance %u connected as %s", client
->sock
,
1787 instance
, zebra_route_string(proto
));
1788 client
->proto
= proto
;
1789 client
->instance
= instance
;
1792 Release previous labels of same protocol and instance.
1793 This is done in case it restarted from an unexpected shutdown.
1795 release_daemon_chunks(proto
, instance
);
1798 " Label Manager client connected: sock %d, proto %s, instance %u",
1799 client
->sock
, zebra_route_string(proto
), instance
);
1800 /* send response back */
1801 zsend_label_manager_connect_response(client
, vrf_id
, 0);
1803 /* Send response to a get label chunk request to client */
1804 static int zsend_assign_label_chunk_response(struct zserv
*client
,
1806 struct label_manager_chunk
*lmc
)
1813 zserv_create_header(s
, ZEBRA_GET_LABEL_CHUNK
, vrf_id
);
1817 stream_putc(s
, lmc
->keep
);
1818 /* start and end labels */
1819 stream_putl(s
, lmc
->start
);
1820 stream_putl(s
, lmc
->end
);
1823 /* Write packet size. */
1824 stream_putw_at(s
, 0, stream_get_endp(s
));
1826 return writen(client
->sock
, s
->data
, stream_get_endp(s
));
1829 static void zread_get_label_chunk(struct zserv
*client
, vrf_id_t vrf_id
)
1834 struct label_manager_chunk
*lmc
;
1836 /* Get input stream. */
1840 keep
= stream_getc(s
);
1841 size
= stream_getl(s
);
1843 lmc
= assign_label_chunk(client
->proto
, client
->instance
, keep
, size
);
1845 zlog_err("%s: Unable to assign Label Chunk of size %u",
1848 zlog_debug("Assigned Label Chunk %u - %u to %u", lmc
->start
,
1850 /* send response back */
1851 zsend_assign_label_chunk_response(client
, vrf_id
, lmc
);
1854 static void zread_release_label_chunk(struct zserv
*client
)
1857 uint32_t start
, end
;
1859 /* Get input stream. */
1863 start
= stream_getl(s
);
1864 end
= stream_getl(s
);
1866 release_label_chunk(client
->proto
, client
->instance
, start
, end
);
1868 static void zread_label_manager_request(int cmd
, struct zserv
*client
,
1871 /* to avoid sending other messages like ZERBA_INTERFACE_UP */
1872 if (cmd
== ZEBRA_LABEL_MANAGER_CONNECT
)
1873 client
->is_synchronous
= 1;
1875 /* external label manager */
1877 zread_relay_label_manager_request(cmd
, client
, vrf_id
);
1878 /* this is a label manager */
1880 if (cmd
== ZEBRA_LABEL_MANAGER_CONNECT
)
1881 zread_label_manager_connect(client
, vrf_id
);
1883 /* Sanity: don't allow 'unidentified' requests */
1884 if (!client
->proto
) {
1886 "Got label request from an unidentified client");
1889 if (cmd
== ZEBRA_GET_LABEL_CHUNK
)
1890 zread_get_label_chunk(client
, vrf_id
);
1891 else if (cmd
== ZEBRA_RELEASE_LABEL_CHUNK
)
1892 zread_release_label_chunk(client
);
1897 static int zread_pseudowire(int command
, struct zserv
*client
, u_short length
,
1901 struct zebra_vrf
*zvrf
;
1902 char ifname
[IF_NAMESIZE
];
1906 union g_addr nexthop
;
1907 uint32_t local_label
;
1908 uint32_t remote_label
;
1910 union pw_protocol_fields data
;
1912 struct zebra_pw
*pw
;
1914 zvrf
= vrf_info_lookup(vrf_id
);
1918 /* Get input stream. */
1922 stream_get(ifname
, s
, IF_NAMESIZE
);
1923 ifindex
= stream_getl(s
);
1924 type
= stream_getl(s
);
1925 af
= stream_getl(s
);
1928 nexthop
.ipv4
.s_addr
= stream_get_ipv4(s
);
1931 stream_get(&nexthop
.ipv6
, s
, 16);
1936 local_label
= stream_getl(s
);
1937 remote_label
= stream_getl(s
);
1938 flags
= stream_getc(s
);
1939 stream_get(&data
, s
, sizeof(data
));
1940 protocol
= client
->proto
;
1942 pw
= zebra_pw_find(zvrf
, ifname
);
1946 zlog_warn("%s: pseudowire %s already exists [%s]",
1948 zserv_command_string(command
));
1952 zebra_pw_add(zvrf
, ifname
, protocol
, client
);
1954 case ZEBRA_PW_DELETE
:
1956 zlog_warn("%s: pseudowire %s not found [%s]", __func__
,
1957 ifname
, zserv_command_string(command
));
1961 zebra_pw_del(zvrf
, pw
);
1964 case ZEBRA_PW_UNSET
:
1966 zlog_warn("%s: pseudowire %s not found [%s]", __func__
,
1967 ifname
, zserv_command_string(command
));
1975 case ZEBRA_PW_UNSET
:
1980 zebra_pw_change(pw
, ifindex
, type
, af
, &nexthop
, local_label
,
1981 remote_label
, flags
, &data
);
1988 /* Cleanup registered nexthops (across VRFs) upon client disconnect. */
1989 static void zebra_client_close_cleanup_rnh(struct zserv
*client
)
1992 struct zebra_vrf
*zvrf
;
1994 RB_FOREACH(vrf
, vrf_id_head
, &vrfs_by_id
)
1996 if ((zvrf
= vrf
->info
) != NULL
) {
1997 zebra_cleanup_rnh_client(zvrf_id(zvrf
), AF_INET
, client
,
1999 zebra_cleanup_rnh_client(zvrf_id(zvrf
), AF_INET6
,
2000 client
, RNH_NEXTHOP_TYPE
);
2001 zebra_cleanup_rnh_client(zvrf_id(zvrf
), AF_INET
, client
,
2002 RNH_IMPORT_CHECK_TYPE
);
2003 zebra_cleanup_rnh_client(zvrf_id(zvrf
), AF_INET6
,
2004 client
, RNH_IMPORT_CHECK_TYPE
);
2005 if (client
->proto
== ZEBRA_ROUTE_LDP
) {
2006 hash_iterate(zvrf
->lsp_table
,
2007 mpls_ldp_lsp_uninstall_all
,
2009 mpls_ldp_ftn_uninstall_all(zvrf
, AFI_IP
);
2010 mpls_ldp_ftn_uninstall_all(zvrf
, AFI_IP6
);
2016 /* free zebra client information. */
2017 static void zebra_client_free(struct zserv
*client
)
2019 /* Send client de-registration to BFD */
2020 zebra_ptm_bfd_client_deregister(client
->proto
);
2022 /* Cleanup any registered nexthops - across all VRFs. */
2023 zebra_client_close_cleanup_rnh(client
);
2025 /* Release Label Manager chunks */
2026 release_daemon_chunks(client
->proto
, client
->instance
);
2028 /* Remove pseudowires associated with this client */
2029 zebra_pw_client_close(client
);
2031 /* Close file descriptor. */
2033 unsigned long nroutes
;
2035 close(client
->sock
);
2036 nroutes
= rib_score_proto(client
->proto
, client
->instance
);
2038 "client %d disconnected. %lu %s routes removed from the rib",
2039 client
->sock
, nroutes
,
2040 zebra_route_string(client
->proto
));
2044 /* Free stream buffers. */
2046 stream_free(client
->ibuf
);
2048 stream_free(client
->obuf
);
2050 buffer_free(client
->wb
);
2052 /* Release threads. */
2054 thread_cancel(client
->t_read
);
2055 if (client
->t_write
)
2056 thread_cancel(client
->t_write
);
2057 if (client
->t_suicide
)
2058 thread_cancel(client
->t_suicide
);
2061 for (afi_t afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
2062 for (int i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
2063 vrf_bitmap_free(client
->redist
[afi
][i
]);
2065 vrf_bitmap_free(client
->redist_default
);
2066 vrf_bitmap_free(client
->ifinfo
);
2067 vrf_bitmap_free(client
->ridinfo
);
2069 XFREE(MTYPE_TMP
, client
);
2072 static void zebra_client_close(struct zserv
*client
)
2074 listnode_delete(zebrad
.client_list
, client
);
2075 zebra_client_free(client
);
2078 /* Make new client. */
2079 static void zebra_client_create(int sock
)
2081 struct zserv
*client
;
2085 client
= XCALLOC(MTYPE_TMP
, sizeof(struct zserv
));
2087 /* Make client input/output buffer. */
2088 client
->sock
= sock
;
2089 client
->ibuf
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
2090 client
->obuf
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
2091 client
->wb
= buffer_new(0);
2093 /* Set table number. */
2094 client
->rtm_table
= zebrad
.rtm_table_default
;
2096 client
->connect_time
= monotime(NULL
);
2097 /* Initialize flags */
2098 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
2099 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
2100 client
->redist
[afi
][i
] = vrf_bitmap_init();
2101 client
->redist_default
= vrf_bitmap_init();
2102 client
->ifinfo
= vrf_bitmap_init();
2103 client
->ridinfo
= vrf_bitmap_init();
2105 /* by default, it's not a synchronous client */
2106 client
->is_synchronous
= 0;
2108 /* Add this client to linked list. */
2109 listnode_add(zebrad
.client_list
, client
);
2111 /* Make new read thread. */
2112 zebra_event(ZEBRA_READ
, sock
, client
);
2114 zebra_vrf_update_all(client
);
2117 /* Handler of zebra service request. */
2118 static int zebra_client_read(struct thread
*thread
)
2121 struct zserv
*client
;
2123 uint16_t length
, command
;
2124 uint8_t marker
, version
;
2126 struct zebra_vrf
*zvrf
;
2128 /* Get thread data. Reset reading thread because I'm running. */
2129 sock
= THREAD_FD(thread
);
2130 client
= THREAD_ARG(thread
);
2131 client
->t_read
= NULL
;
2133 if (client
->t_suicide
) {
2134 zebra_client_close(client
);
2138 /* Read length and command (if we don't have it already). */
2139 if ((already
= stream_get_endp(client
->ibuf
)) < ZEBRA_HEADER_SIZE
) {
2141 if (((nbyte
= stream_read_try(client
->ibuf
, sock
,
2142 ZEBRA_HEADER_SIZE
- already
))
2145 if (IS_ZEBRA_DEBUG_EVENT
)
2146 zlog_debug("connection closed socket [%d]",
2148 zebra_client_close(client
);
2151 if (nbyte
!= (ssize_t
)(ZEBRA_HEADER_SIZE
- already
)) {
2152 /* Try again later. */
2153 zebra_event(ZEBRA_READ
, sock
, client
);
2156 already
= ZEBRA_HEADER_SIZE
;
2159 /* Reset to read from the beginning of the incoming packet. */
2160 stream_set_getp(client
->ibuf
, 0);
2162 /* Fetch header values */
2163 length
= stream_getw(client
->ibuf
);
2164 marker
= stream_getc(client
->ibuf
);
2165 version
= stream_getc(client
->ibuf
);
2166 vrf_id
= stream_getw(client
->ibuf
);
2167 command
= stream_getw(client
->ibuf
);
2169 if (marker
!= ZEBRA_HEADER_MARKER
|| version
!= ZSERV_VERSION
) {
2171 "%s: socket %d version mismatch, marker %d, version %d",
2172 __func__
, sock
, marker
, version
);
2173 zebra_client_close(client
);
2176 if (length
< ZEBRA_HEADER_SIZE
) {
2178 "%s: socket %d message length %u is less than header size %d",
2179 __func__
, sock
, length
, ZEBRA_HEADER_SIZE
);
2180 zebra_client_close(client
);
2183 if (length
> STREAM_SIZE(client
->ibuf
)) {
2185 "%s: socket %d message length %u exceeds buffer size %lu",
2186 __func__
, sock
, length
,
2187 (u_long
)STREAM_SIZE(client
->ibuf
));
2188 zebra_client_close(client
);
2192 /* Read rest of data. */
2193 if (already
< length
) {
2195 if (((nbyte
= stream_read_try(client
->ibuf
, sock
,
2199 if (IS_ZEBRA_DEBUG_EVENT
)
2201 "connection closed [%d] when reading zebra data",
2203 zebra_client_close(client
);
2206 if (nbyte
!= (ssize_t
)(length
- already
)) {
2207 /* Try again later. */
2208 zebra_event(ZEBRA_READ
, sock
, client
);
2213 length
-= ZEBRA_HEADER_SIZE
;
2215 /* Debug packet information. */
2216 if (IS_ZEBRA_DEBUG_EVENT
)
2217 zlog_debug("zebra message comes from socket [%d]", sock
);
2219 if (IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
)
2220 zlog_debug("zebra message received [%s] %d in VRF %u",
2221 zserv_command_string(command
), length
, vrf_id
);
2223 client
->last_read_time
= monotime(NULL
);
2224 client
->last_read_cmd
= command
;
2226 zvrf
= zebra_vrf_lookup_by_id(vrf_id
);
2228 if (IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
)
2229 zlog_debug("zebra received unknown VRF[%u]", vrf_id
);
2230 goto zclient_read_out
;
2234 case ZEBRA_ROUTER_ID_ADD
:
2235 zread_router_id_add(client
, length
, zvrf
);
2237 case ZEBRA_ROUTER_ID_DELETE
:
2238 zread_router_id_delete(client
, length
, zvrf
);
2240 case ZEBRA_INTERFACE_ADD
:
2241 zread_interface_add(client
, length
, zvrf
);
2243 case ZEBRA_INTERFACE_DELETE
:
2244 zread_interface_delete(client
, length
, zvrf
);
2246 case ZEBRA_IPV4_ROUTE_ADD
:
2247 zread_ipv4_add(client
, length
, zvrf
);
2249 case ZEBRA_IPV4_ROUTE_DELETE
:
2250 zread_ipv4_delete(client
, length
, zvrf
);
2252 case ZEBRA_IPV4_ROUTE_IPV6_NEXTHOP_ADD
:
2253 zread_ipv4_route_ipv6_nexthop_add(client
, length
, zvrf
);
2255 case ZEBRA_IPV4_NEXTHOP_ADD
:
2256 zread_ipv4_add(client
, length
,
2257 zvrf
); /* LB: r1.0 merge - id was 1 */
2259 case ZEBRA_IPV4_NEXTHOP_DELETE
:
2260 zread_ipv4_delete(client
, length
,
2261 zvrf
); /* LB: r1.0 merge - id was 1 */
2263 case ZEBRA_IPV6_ROUTE_ADD
:
2264 zread_ipv6_add(client
, length
, zvrf
);
2266 case ZEBRA_IPV6_ROUTE_DELETE
:
2267 zread_ipv6_delete(client
, length
, zvrf
);
2269 case ZEBRA_REDISTRIBUTE_ADD
:
2270 zebra_redistribute_add(command
, client
, length
, zvrf
);
2272 case ZEBRA_REDISTRIBUTE_DELETE
:
2273 zebra_redistribute_delete(command
, client
, length
, zvrf
);
2275 case ZEBRA_REDISTRIBUTE_DEFAULT_ADD
:
2276 zebra_redistribute_default_add(command
, client
, length
, zvrf
);
2278 case ZEBRA_REDISTRIBUTE_DEFAULT_DELETE
:
2279 zebra_redistribute_default_delete(command
, client
, length
,
2282 case ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB
:
2283 zread_ipv4_nexthop_lookup_mrib(client
, length
, zvrf
);
2286 zread_hello(client
);
2288 case ZEBRA_NEXTHOP_REGISTER
:
2289 zserv_rnh_register(client
, sock
, length
, RNH_NEXTHOP_TYPE
,
2292 case ZEBRA_NEXTHOP_UNREGISTER
:
2293 zserv_rnh_unregister(client
, sock
, length
, RNH_NEXTHOP_TYPE
,
2296 case ZEBRA_IMPORT_ROUTE_REGISTER
:
2297 zserv_rnh_register(client
, sock
, length
, RNH_IMPORT_CHECK_TYPE
,
2300 case ZEBRA_IMPORT_ROUTE_UNREGISTER
:
2301 zserv_rnh_unregister(client
, sock
, length
,
2302 RNH_IMPORT_CHECK_TYPE
, zvrf
);
2304 case ZEBRA_BFD_DEST_UPDATE
:
2305 case ZEBRA_BFD_DEST_REGISTER
:
2306 zebra_ptm_bfd_dst_register(client
, sock
, length
, command
, zvrf
);
2308 case ZEBRA_BFD_DEST_DEREGISTER
:
2309 zebra_ptm_bfd_dst_deregister(client
, sock
, length
, zvrf
);
2311 case ZEBRA_VRF_UNREGISTER
:
2312 zread_vrf_unregister(client
, length
, zvrf
);
2314 case ZEBRA_BFD_CLIENT_REGISTER
:
2315 zebra_ptm_bfd_client_register(client
, sock
, length
);
2317 case ZEBRA_INTERFACE_ENABLE_RADV
:
2318 zebra_interface_radv_set(client
, sock
, length
, zvrf
, 1);
2320 case ZEBRA_INTERFACE_DISABLE_RADV
:
2321 zebra_interface_radv_set(client
, sock
, length
, zvrf
, 0);
2323 case ZEBRA_MPLS_LABELS_ADD
:
2324 case ZEBRA_MPLS_LABELS_DELETE
:
2325 zread_mpls_labels(command
, client
, length
, vrf_id
);
2327 case ZEBRA_IPMR_ROUTE_STATS
:
2328 zebra_ipmr_route_stats(client
, sock
, length
, zvrf
);
2330 case ZEBRA_LABEL_MANAGER_CONNECT
:
2331 case ZEBRA_GET_LABEL_CHUNK
:
2332 case ZEBRA_RELEASE_LABEL_CHUNK
:
2333 zread_label_manager_request(command
, client
, vrf_id
);
2336 case ZEBRA_PW_DELETE
:
2338 case ZEBRA_PW_UNSET
:
2339 zread_pseudowire(command
, client
, length
, vrf_id
);
2342 zlog_info("Zebra received unknown command %d", command
);
2346 if (client
->t_suicide
) {
2347 /* No need to wait for thread callback, just kill immediately.
2349 zebra_client_close(client
);
2354 stream_reset(client
->ibuf
);
2355 zebra_event(ZEBRA_READ
, sock
, client
);
2360 /* Accept code of zebra server socket. */
2361 static int zebra_accept(struct thread
*thread
)
2365 struct sockaddr_in client
;
2368 accept_sock
= THREAD_FD(thread
);
2370 /* Reregister myself. */
2371 zebra_event(ZEBRA_SERV
, accept_sock
, NULL
);
2373 len
= sizeof(struct sockaddr_in
);
2374 client_sock
= accept(accept_sock
, (struct sockaddr
*)&client
, &len
);
2376 if (client_sock
< 0) {
2377 zlog_warn("Can't accept zebra socket: %s",
2378 safe_strerror(errno
));
2382 /* Make client socket non-blocking. */
2383 set_nonblocking(client_sock
);
2385 /* Create new zebra client. */
2386 zebra_client_create(client_sock
);
2391 #ifdef HAVE_TCP_ZEBRA
2392 /* Make zebra's server socket. */
2393 static void zebra_serv()
2397 struct sockaddr_in addr
;
2399 accept_sock
= socket(AF_INET
, SOCK_STREAM
, 0);
2401 if (accept_sock
< 0) {
2402 zlog_warn("Can't create zserv stream socket: %s",
2403 safe_strerror(errno
));
2405 "zebra can't provice full functionality due to above error");
2409 memset(&addr
, 0, sizeof(struct sockaddr_in
));
2410 addr
.sin_family
= AF_INET
;
2411 addr
.sin_port
= htons(ZEBRA_PORT
);
2412 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
2413 addr
.sin_len
= sizeof(struct sockaddr_in
);
2414 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
2415 addr
.sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
);
2417 sockopt_reuseaddr(accept_sock
);
2418 sockopt_reuseport(accept_sock
);
2420 if (zserv_privs
.change(ZPRIVS_RAISE
))
2421 zlog_err("Can't raise privileges");
2423 ret
= bind(accept_sock
, (struct sockaddr
*)&addr
,
2424 sizeof(struct sockaddr_in
));
2426 zlog_warn("Can't bind to stream socket: %s",
2427 safe_strerror(errno
));
2429 "zebra can't provice full functionality due to above error");
2430 close(accept_sock
); /* Avoid sd leak. */
2434 if (zserv_privs
.change(ZPRIVS_LOWER
))
2435 zlog_err("Can't lower privileges");
2437 ret
= listen(accept_sock
, 1);
2439 zlog_warn("Can't listen to stream socket: %s",
2440 safe_strerror(errno
));
2442 "zebra can't provice full functionality due to above error");
2443 close(accept_sock
); /* Avoid sd leak. */
2447 zebra_event(ZEBRA_SERV
, accept_sock
, NULL
);
2449 #else /* HAVE_TCP_ZEBRA */
2451 /* For sockaddr_un. */
2454 /* zebra server UNIX domain socket. */
2455 static void zebra_serv_un(const char *path
)
2459 struct sockaddr_un serv
;
2462 /* First of all, unlink existing socket */
2466 old_mask
= umask(0077);
2468 /* Make UNIX domain socket. */
2469 sock
= socket(AF_UNIX
, SOCK_STREAM
, 0);
2471 zlog_warn("Can't create zserv unix socket: %s",
2472 safe_strerror(errno
));
2474 "zebra can't provide full functionality due to above error");
2478 /* Make server socket. */
2479 memset(&serv
, 0, sizeof(struct sockaddr_un
));
2480 serv
.sun_family
= AF_UNIX
;
2481 strncpy(serv
.sun_path
, path
, strlen(path
));
2482 #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
2483 len
= serv
.sun_len
= SUN_LEN(&serv
);
2485 len
= sizeof(serv
.sun_family
) + strlen(serv
.sun_path
);
2486 #endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
2488 ret
= bind(sock
, (struct sockaddr
*)&serv
, len
);
2490 zlog_warn("Can't bind to unix socket %s: %s", path
,
2491 safe_strerror(errno
));
2493 "zebra can't provide full functionality due to above error");
2498 ret
= listen(sock
, 5);
2500 zlog_warn("Can't listen to unix socket %s: %s", path
,
2501 safe_strerror(errno
));
2503 "zebra can't provide full functionality due to above error");
2510 zebra_event(ZEBRA_SERV
, sock
, NULL
);
2512 #endif /* HAVE_TCP_ZEBRA */
2515 static void zebra_event(enum event event
, int sock
, struct zserv
*client
)
2519 thread_add_read(zebrad
.master
, zebra_accept
, client
, sock
);
2522 client
->t_read
= thread_add_read(
2523 zebrad
.master
, zebra_client_read
, client
, sock
);
2531 #define ZEBRA_TIME_BUF 32
2532 static char *zserv_time_buf(time_t *time1
, char *buf
, int buflen
)
2537 assert(buf
!= NULL
);
2538 assert(buflen
>= ZEBRA_TIME_BUF
);
2539 assert(time1
!= NULL
);
2542 snprintf(buf
, buflen
, "never ");
2546 now
= monotime(NULL
);
2550 /* Making formatted timer strings. */
2551 #define ONE_DAY_SECOND 60*60*24
2552 #define ONE_WEEK_SECOND 60*60*24*7
2554 if (now
< ONE_DAY_SECOND
)
2555 snprintf(buf
, buflen
, "%02d:%02d:%02d", tm
->tm_hour
, tm
->tm_min
,
2557 else if (now
< ONE_WEEK_SECOND
)
2558 snprintf(buf
, buflen
, "%dd%02dh%02dm", tm
->tm_yday
, tm
->tm_hour
,
2561 snprintf(buf
, buflen
, "%02dw%dd%02dh", tm
->tm_yday
/ 7,
2562 tm
->tm_yday
- ((tm
->tm_yday
/ 7) * 7), tm
->tm_hour
);
2566 static void zebra_show_client_detail(struct vty
*vty
, struct zserv
*client
)
2568 char cbuf
[ZEBRA_TIME_BUF
], rbuf
[ZEBRA_TIME_BUF
];
2569 char wbuf
[ZEBRA_TIME_BUF
], nhbuf
[ZEBRA_TIME_BUF
], mbuf
[ZEBRA_TIME_BUF
];
2571 vty_out(vty
, "Client: %s", zebra_route_string(client
->proto
));
2572 if (client
->instance
)
2573 vty_out(vty
, " Instance: %d", client
->instance
);
2574 vty_out(vty
, "%s", VTY_NEWLINE
);
2576 vty_out(vty
, "------------------------ %s", VTY_NEWLINE
);
2577 vty_out(vty
, "FD: %d %s", client
->sock
, VTY_NEWLINE
);
2578 vty_out(vty
, "Route Table ID: %d %s", client
->rtm_table
, VTY_NEWLINE
);
2580 vty_out(vty
, "Connect Time: %s %s",
2581 zserv_time_buf(&client
->connect_time
, cbuf
, ZEBRA_TIME_BUF
),
2583 if (client
->nh_reg_time
) {
2584 vty_out(vty
, "Nexthop Registry Time: %s %s",
2585 zserv_time_buf(&client
->nh_reg_time
, nhbuf
,
2588 if (client
->nh_last_upd_time
)
2589 vty_out(vty
, "Nexthop Last Update Time: %s %s",
2590 zserv_time_buf(&client
->nh_last_upd_time
, mbuf
,
2594 vty_out(vty
, "No Nexthop Update sent%s", VTY_NEWLINE
);
2596 vty_out(vty
, "Not registered for Nexthop Updates%s",
2599 vty_out(vty
, "Last Msg Rx Time: %s %s",
2600 zserv_time_buf(&client
->last_read_time
, rbuf
, ZEBRA_TIME_BUF
),
2602 vty_out(vty
, "Last Msg Tx Time: %s %s",
2603 zserv_time_buf(&client
->last_write_time
, wbuf
, ZEBRA_TIME_BUF
),
2605 if (client
->last_read_time
)
2606 vty_out(vty
, "Last Rcvd Cmd: %s %s",
2607 zserv_command_string(client
->last_read_cmd
),
2609 if (client
->last_write_time
)
2610 vty_out(vty
, "Last Sent Cmd: %s %s",
2611 zserv_command_string(client
->last_write_cmd
),
2613 vty_out(vty
, "%s", VTY_NEWLINE
);
2615 vty_out(vty
, "Type Add Update Del %s", VTY_NEWLINE
);
2616 vty_out(vty
, "================================================== %s",
2618 vty_out(vty
, "IPv4 %-12d%-12d%-12d%s", client
->v4_route_add_cnt
,
2619 client
->v4_route_upd8_cnt
, client
->v4_route_del_cnt
,
2621 vty_out(vty
, "IPv6 %-12d%-12d%-12d%s", client
->v6_route_add_cnt
,
2622 client
->v6_route_upd8_cnt
, client
->v6_route_del_cnt
,
2624 vty_out(vty
, "Redist:v4 %-12d%-12d%-12d%s", client
->redist_v4_add_cnt
,
2625 0, client
->redist_v4_del_cnt
, VTY_NEWLINE
);
2626 vty_out(vty
, "Redist:v6 %-12d%-12d%-12d%s", client
->redist_v6_add_cnt
,
2627 0, client
->redist_v6_del_cnt
, VTY_NEWLINE
);
2628 vty_out(vty
, "Connected %-12d%-12d%-12d%s", client
->ifadd_cnt
, 0,
2629 client
->ifdel_cnt
, VTY_NEWLINE
);
2630 vty_out(vty
, "BFD peer %-12d%-12d%-12d%s", client
->bfd_peer_add_cnt
,
2631 client
->bfd_peer_upd8_cnt
, client
->bfd_peer_del_cnt
,
2633 vty_out(vty
, "Interface Up Notifications: %d%s", client
->ifup_cnt
,
2635 vty_out(vty
, "Interface Down Notifications: %d%s", client
->ifdown_cnt
,
2638 vty_out(vty
, "%s", VTY_NEWLINE
);
2642 static void zebra_show_client_brief(struct vty
*vty
, struct zserv
*client
)
2644 char cbuf
[ZEBRA_TIME_BUF
], rbuf
[ZEBRA_TIME_BUF
];
2645 char wbuf
[ZEBRA_TIME_BUF
];
2647 vty_out(vty
, "%-8s%12s %12s%12s%8d/%-8d%8d/%-8d%s",
2648 zebra_route_string(client
->proto
),
2649 zserv_time_buf(&client
->connect_time
, cbuf
, ZEBRA_TIME_BUF
),
2650 zserv_time_buf(&client
->last_read_time
, rbuf
, ZEBRA_TIME_BUF
),
2651 zserv_time_buf(&client
->last_write_time
, wbuf
, ZEBRA_TIME_BUF
),
2652 client
->v4_route_add_cnt
+ client
->v4_route_upd8_cnt
,
2653 client
->v4_route_del_cnt
,
2654 client
->v6_route_add_cnt
+ client
->v6_route_upd8_cnt
,
2655 client
->v6_route_del_cnt
, VTY_NEWLINE
);
2658 struct zserv
*zebra_find_client(u_char proto
)
2660 struct listnode
*node
, *nnode
;
2661 struct zserv
*client
;
2663 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, node
, nnode
, client
)) {
2664 if (client
->proto
== proto
)
2672 /* Display default rtm_table for all clients. */
2677 "default routing table to use for all clients\n")
2679 vty_out(vty
, "table %d%s", zebrad
.rtm_table_default
, VTY_NEWLINE
);
2683 DEFUN (config_table
,
2686 "Configure target kernel routing table\n"
2689 zebrad
.rtm_table_default
= strtol(argv
[1]->arg
, (char **)0, 10);
2693 DEFUN (no_config_table
,
2694 no_config_table_cmd
,
2695 "no table [TABLENO]",
2697 "Configure target kernel routing table\n"
2700 zebrad
.rtm_table_default
= 0;
2705 DEFUN (ip_forwarding
,
2709 "Turn on IP forwarding")
2715 ret
= ipforward_on();
2718 vty_out(vty
, "Can't turn on IP forwarding%s", VTY_NEWLINE
);
2725 DEFUN (no_ip_forwarding
,
2726 no_ip_forwarding_cmd
,
2730 "Turn off IP forwarding")
2736 ret
= ipforward_off();
2739 vty_out(vty
, "Can't turn off IP forwarding%s", VTY_NEWLINE
);
2746 /* This command is for debugging purpose. */
2747 DEFUN (show_zebra_client
,
2748 show_zebra_client_cmd
,
2749 "show zebra client",
2751 "Zebra information\n"
2752 "Client information\n")
2754 struct listnode
*node
;
2755 struct zserv
*client
;
2757 for (ALL_LIST_ELEMENTS_RO(zebrad
.client_list
, node
, client
))
2758 zebra_show_client_detail(vty
, client
);
2763 /* This command is for debugging purpose. */
2764 DEFUN (show_zebra_client_summary
,
2765 show_zebra_client_summary_cmd
,
2766 "show zebra client summary",
2768 "Zebra information brief\n"
2769 "Client information brief\n"
2772 struct listnode
*node
;
2773 struct zserv
*client
;
2776 "Name Connect Time Last Read Last Write IPv4 Routes IPv6 Routes %s",
2779 "--------------------------------------------------------------------------------%s",
2782 for (ALL_LIST_ELEMENTS_RO(zebrad
.client_list
, node
, client
))
2783 zebra_show_client_brief(vty
, client
);
2785 vty_out(vty
, "Routes column shows (added+updated)/deleted%s",
2790 /* Table configuration write function. */
2791 static int config_write_table(struct vty
*vty
)
2793 if (zebrad
.rtm_table_default
)
2794 vty_out(vty
, "table %d%s", zebrad
.rtm_table_default
,
2799 /* table node for routing tables. */
2800 static struct cmd_node table_node
= {TABLE_NODE
,
2801 "", /* This node has no interface. */
2804 /* Only display ip forwarding is enabled or not. */
2805 DEFUN (show_ip_forwarding
,
2806 show_ip_forwarding_cmd
,
2807 "show ip forwarding",
2810 "IP forwarding status\n")
2817 vty_out(vty
, "IP forwarding is off%s", VTY_NEWLINE
);
2819 vty_out(vty
, "IP forwarding is on%s", VTY_NEWLINE
);
2823 /* Only display ipv6 forwarding is enabled or not. */
2824 DEFUN (show_ipv6_forwarding
,
2825 show_ipv6_forwarding_cmd
,
2826 "show ipv6 forwarding",
2828 "IPv6 information\n"
2829 "Forwarding status\n")
2833 ret
= ipforward_ipv6();
2837 vty_out(vty
, "ipv6 forwarding is unknown%s", VTY_NEWLINE
);
2840 vty_out(vty
, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE
);
2843 vty_out(vty
, "ipv6 forwarding is %s%s", "on", VTY_NEWLINE
);
2846 vty_out(vty
, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE
);
2852 DEFUN (ipv6_forwarding
,
2853 ipv6_forwarding_cmd
,
2856 "Turn on IPv6 forwarding")
2860 ret
= ipforward_ipv6();
2862 ret
= ipforward_ipv6_on();
2865 vty_out(vty
, "Can't turn on IPv6 forwarding%s", VTY_NEWLINE
);
2872 DEFUN (no_ipv6_forwarding
,
2873 no_ipv6_forwarding_cmd
,
2874 "no ipv6 forwarding",
2877 "Turn off IPv6 forwarding")
2881 ret
= ipforward_ipv6();
2883 ret
= ipforward_ipv6_off();
2886 vty_out(vty
, "Can't turn off IPv6 forwarding%s", VTY_NEWLINE
);
2893 /* IPForwarding configuration write function. */
2894 static int config_write_forwarding(struct vty
*vty
)
2896 /* FIXME: Find better place for that. */
2897 router_id_write(vty
);
2900 vty_out(vty
, "no ip forwarding%s", VTY_NEWLINE
);
2901 if (!ipforward_ipv6())
2902 vty_out(vty
, "no ipv6 forwarding%s", VTY_NEWLINE
);
2903 vty_out(vty
, "!%s", VTY_NEWLINE
);
2907 /* table node for routing tables. */
2908 static struct cmd_node forwarding_node
= {FORWARDING_NODE
,
2909 "", /* This node has no interface. */
2912 /* Initialisation of zebra and installation of commands. */
2913 void zebra_init(void)
2915 /* Client list init. */
2916 zebrad
.client_list
= list_new();
2917 zebrad
.client_list
->del
= (void (*)(void *))zebra_client_free
;
2919 /* Install configuration write function. */
2920 install_node(&table_node
, config_write_table
);
2921 install_node(&forwarding_node
, config_write_forwarding
);
2923 install_element(VIEW_NODE
, &show_ip_forwarding_cmd
);
2924 install_element(CONFIG_NODE
, &ip_forwarding_cmd
);
2925 install_element(CONFIG_NODE
, &no_ip_forwarding_cmd
);
2926 install_element(ENABLE_NODE
, &show_zebra_client_cmd
);
2927 install_element(ENABLE_NODE
, &show_zebra_client_summary_cmd
);
2930 install_element(VIEW_NODE
, &show_table_cmd
);
2931 install_element(CONFIG_NODE
, &config_table_cmd
);
2932 install_element(CONFIG_NODE
, &no_config_table_cmd
);
2933 #endif /* HAVE_NETLINK */
2935 install_element(VIEW_NODE
, &show_ipv6_forwarding_cmd
);
2936 install_element(CONFIG_NODE
, &ipv6_forwarding_cmd
);
2937 install_element(CONFIG_NODE
, &no_ipv6_forwarding_cmd
);
2940 zebra_route_map_init();
2943 /* Make zebra server socket, wiping any existing one (see bug #403). */
2944 void zebra_zserv_socket_init(char *path
)
2946 #ifdef HAVE_TCP_ZEBRA
2949 zebra_serv_un(path
? path
: ZEBRA_SERV_PATH
);
2950 #endif /* HAVE_TCP_ZEBRA */