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 along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
32 #include "zebra_memory.h"
36 #include "sockunion.h"
47 #include "zebra/zserv.h"
48 #include "zebra/zebra_ns.h"
49 #include "zebra/zebra_vrf.h"
50 #include "zebra/router-id.h"
51 #include "zebra/redistribute.h"
52 #include "zebra/debug.h"
53 #include "zebra/zebra_rnh.h"
54 #include "zebra/rt_netlink.h"
55 #include "zebra/interface.h"
56 #include "zebra/zebra_ptm.h"
57 #include "zebra/rtadv.h"
58 #include "zebra/zebra_mpls.h"
59 #include "zebra/zebra_mroute.h"
60 #include "zebra/label_manager.h"
61 #include "zebra/zebra_vxlan.h"
64 /* Event list of zebra. */
65 enum event
{ ZEBRA_SERV
, ZEBRA_READ
, ZEBRA_WRITE
};
67 static void zebra_event(enum event event
, int sock
, struct zserv
*client
);
69 extern struct zebra_privs_t zserv_privs
;
71 static void zebra_client_close(struct zserv
*client
);
73 static int zserv_delayed_close(struct thread
*thread
)
75 struct zserv
*client
= THREAD_ARG(thread
);
77 client
->t_suicide
= NULL
;
78 zebra_client_close(client
);
82 static int zserv_flush_data(struct thread
*thread
)
84 struct zserv
*client
= THREAD_ARG(thread
);
86 client
->t_write
= NULL
;
87 if (client
->t_suicide
) {
88 zebra_client_close(client
);
91 switch (buffer_flush_available(client
->wb
, client
->sock
)) {
94 "%s: buffer_flush_available failed on zserv client fd %d, "
96 __func__
, client
->sock
);
97 zebra_client_close(client
);
101 client
->t_write
= NULL
;
102 thread_add_write(zebrad
.master
, zserv_flush_data
, client
,
103 client
->sock
, &client
->t_write
);
110 client
->last_write_time
= monotime(NULL
);
114 int zebra_server_send_message(struct zserv
*client
)
116 if (client
->t_suicide
)
119 if (client
->is_synchronous
)
122 stream_set_getp(client
->obuf
, 0);
123 client
->last_write_cmd
= stream_getw_from(client
->obuf
, 6);
124 switch (buffer_write(client
->wb
, client
->sock
,
125 STREAM_DATA(client
->obuf
),
126 stream_get_endp(client
->obuf
))) {
129 "%s: buffer_write failed to zserv client fd %d, closing",
130 __func__
, client
->sock
);
131 /* Schedule a delayed close since many of the functions that
133 one do not check the return code. They do not allow for the
134 possibility that an I/O error may have caused the client to
137 client
->t_suicide
= NULL
;
138 thread_add_event(zebrad
.master
, zserv_delayed_close
, client
, 0,
142 THREAD_OFF(client
->t_write
);
145 thread_add_write(zebrad
.master
, zserv_flush_data
, client
,
146 client
->sock
, &client
->t_write
);
150 client
->last_write_time
= monotime(NULL
);
154 static void zserv_encode_interface(struct stream
*s
, struct interface
*ifp
)
156 /* Interface information. */
157 stream_put(s
, ifp
->name
, INTERFACE_NAMSIZ
);
158 stream_putl(s
, ifp
->ifindex
);
159 stream_putc(s
, ifp
->status
);
160 stream_putq(s
, ifp
->flags
);
161 stream_putc(s
, ifp
->ptm_enable
);
162 stream_putc(s
, ifp
->ptm_status
);
163 stream_putl(s
, ifp
->metric
);
164 stream_putl(s
, ifp
->speed
);
165 stream_putl(s
, ifp
->mtu
);
166 stream_putl(s
, ifp
->mtu6
);
167 stream_putl(s
, ifp
->bandwidth
);
168 stream_putl(s
, ifp
->ll_type
);
169 stream_putl(s
, ifp
->hw_addr_len
);
170 if (ifp
->hw_addr_len
)
171 stream_put(s
, ifp
->hw_addr
, ifp
->hw_addr_len
);
173 /* Then, Traffic Engineering parameters if any */
174 if (HAS_LINK_PARAMS(ifp
) && IS_LINK_PARAMS_SET(ifp
->link_params
)) {
176 zebra_interface_link_params_write(s
, ifp
);
180 /* Write packet size. */
181 stream_putw_at(s
, 0, stream_get_endp(s
));
184 static void zserv_encode_vrf(struct stream
*s
, struct zebra_vrf
*zvrf
)
186 struct vrf_data data
;
187 const char *netns_name
= zvrf_ns_name(zvrf
);
189 data
.l
.table_id
= zvrf
->table_id
;
192 strlcpy(data
.l
.netns_name
, basename((char *)netns_name
),
195 memset(data
.l
.netns_name
, 0, NS_NAMSIZ
);
196 /* Pass the tableid and the netns NAME */
197 stream_put(s
, &data
, sizeof(struct vrf_data
));
198 /* Interface information. */
199 stream_put(s
, zvrf_name(zvrf
), VRF_NAMSIZ
);
200 /* Write packet size. */
201 stream_putw_at(s
, 0, stream_get_endp(s
));
204 /* Interface is added. Send ZEBRA_INTERFACE_ADD to client. */
206 * This function is called in the following situations:
207 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
209 * - at startup, when zebra figures out the available interfaces
210 * - when an interface is added (where support for
211 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
212 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
215 int zsend_interface_add(struct zserv
*client
, struct interface
*ifp
)
222 zclient_create_header(s
, ZEBRA_INTERFACE_ADD
, ifp
->vrf_id
);
223 zserv_encode_interface(s
, ifp
);
226 return zebra_server_send_message(client
);
229 /* Interface deletion from zebra daemon. */
230 int zsend_interface_delete(struct zserv
*client
, struct interface
*ifp
)
237 zclient_create_header(s
, ZEBRA_INTERFACE_DELETE
, ifp
->vrf_id
);
238 zserv_encode_interface(s
, ifp
);
241 return zebra_server_send_message(client
);
244 int zsend_vrf_add(struct zserv
*client
, struct zebra_vrf
*zvrf
)
251 zclient_create_header(s
, ZEBRA_VRF_ADD
, zvrf_id(zvrf
));
252 zserv_encode_vrf(s
, zvrf
);
254 client
->vrfadd_cnt
++;
255 return zebra_server_send_message(client
);
258 /* VRF deletion from zebra daemon. */
259 int zsend_vrf_delete(struct zserv
*client
, struct zebra_vrf
*zvrf
)
266 zclient_create_header(s
, ZEBRA_VRF_DELETE
, zvrf_id(zvrf
));
267 zserv_encode_vrf(s
, zvrf
);
269 client
->vrfdel_cnt
++;
270 return zebra_server_send_message(client
);
273 int zsend_interface_link_params(struct zserv
*client
, struct interface
*ifp
)
277 /* Check this client need interface information. */
281 if (!ifp
->link_params
)
286 zclient_create_header(s
, ZEBRA_INTERFACE_LINK_PARAMS
, ifp
->vrf_id
);
288 /* Add Interface Index */
289 stream_putl(s
, ifp
->ifindex
);
291 /* Then TE Link Parameters */
292 if (zebra_interface_link_params_write(s
, ifp
) == 0)
295 /* Write packet size. */
296 stream_putw_at(s
, 0, stream_get_endp(s
));
298 return zebra_server_send_message(client
);
301 /* Interface address is added/deleted. Send ZEBRA_INTERFACE_ADDRESS_ADD or
302 * ZEBRA_INTERFACE_ADDRESS_DELETE to the client.
304 * A ZEBRA_INTERFACE_ADDRESS_ADD is sent in the following situations:
305 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
306 * from the client, after the ZEBRA_INTERFACE_ADD has been
307 * sent from zebra to the client
308 * - redistribute new address info to all clients in the following situations
309 * - at startup, when zebra figures out the available interfaces
310 * - when an interface is added (where support for
311 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
312 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
314 * - for the vty commands "ip address A.B.C.D/M [<secondary>|<label LINE>]"
315 * and "no bandwidth <1-10000000>", "ipv6 address X:X::X:X/M"
316 * - when an RTM_NEWADDR message is received from the kernel,
318 * The call tree that triggers ZEBRA_INTERFACE_ADDRESS_DELETE:
320 * zsend_interface_address(DELETE)
323 * zebra_interface_address_delete_update
325 * | | if_delete_update
327 * ip_address_uninstall connected_delete_ipv4
328 * [ipv6_addresss_uninstall] [connected_delete_ipv6]
331 * | RTM_NEWADDR on routing/netlink socket
334 * "no ip address A.B.C.D/M [label LINE]"
335 * "no ip address A.B.C.D/M secondary"
336 * ["no ipv6 address X:X::X:X/M"]
339 int zsend_interface_address(int cmd
, struct zserv
*client
,
340 struct interface
*ifp
, struct connected
*ifc
)
349 zclient_create_header(s
, cmd
, ifp
->vrf_id
);
350 stream_putl(s
, ifp
->ifindex
);
352 /* Interface address flag. */
353 stream_putc(s
, ifc
->flags
);
355 /* Prefix information. */
357 stream_putc(s
, p
->family
);
358 blen
= prefix_blen(p
);
359 stream_put(s
, &p
->u
.prefix
, blen
);
362 * XXX gnu version does not send prefixlen for
363 * ZEBRA_INTERFACE_ADDRESS_DELETE
364 * but zebra_interface_address_delete_read() in the gnu version
367 stream_putc(s
, p
->prefixlen
);
370 p
= ifc
->destination
;
372 stream_put(s
, &p
->u
.prefix
, blen
);
374 stream_put(s
, NULL
, blen
);
376 /* Write packet size. */
377 stream_putw_at(s
, 0, stream_get_endp(s
));
379 client
->connected_rt_add_cnt
++;
380 return zebra_server_send_message(client
);
383 static int zsend_interface_nbr_address(int cmd
, struct zserv
*client
,
384 struct interface
*ifp
,
385 struct nbr_connected
*ifc
)
394 zclient_create_header(s
, cmd
, ifp
->vrf_id
);
395 stream_putl(s
, ifp
->ifindex
);
397 /* Prefix information. */
399 stream_putc(s
, p
->family
);
400 blen
= prefix_blen(p
);
401 stream_put(s
, &p
->u
.prefix
, blen
);
404 * XXX gnu version does not send prefixlen for
405 * ZEBRA_INTERFACE_ADDRESS_DELETE
406 * but zebra_interface_address_delete_read() in the gnu version
409 stream_putc(s
, p
->prefixlen
);
411 /* Write packet size. */
412 stream_putw_at(s
, 0, stream_get_endp(s
));
414 return zebra_server_send_message(client
);
417 /* Interface address addition. */
418 static void zebra_interface_nbr_address_add_update(struct interface
*ifp
,
419 struct nbr_connected
*ifc
)
421 struct listnode
*node
, *nnode
;
422 struct zserv
*client
;
425 if (IS_ZEBRA_DEBUG_EVENT
) {
426 char buf
[INET6_ADDRSTRLEN
];
430 "MESSAGE: ZEBRA_INTERFACE_NBR_ADDRESS_ADD %s/%d on %s",
431 inet_ntop(p
->family
, &p
->u
.prefix
, buf
,
433 p
->prefixlen
, ifc
->ifp
->name
);
436 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, node
, nnode
, client
))
437 zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_ADD
,
441 /* Interface address deletion. */
442 static void zebra_interface_nbr_address_delete_update(struct interface
*ifp
,
443 struct nbr_connected
*ifc
)
445 struct listnode
*node
, *nnode
;
446 struct zserv
*client
;
449 if (IS_ZEBRA_DEBUG_EVENT
) {
450 char buf
[INET6_ADDRSTRLEN
];
454 "MESSAGE: ZEBRA_INTERFACE_NBR_ADDRESS_DELETE %s/%d on %s",
455 inet_ntop(p
->family
, &p
->u
.prefix
, buf
,
457 p
->prefixlen
, ifc
->ifp
->name
);
460 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, node
, nnode
, client
))
461 zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
,
465 /* Send addresses on interface to client */
466 int zsend_interface_addresses(struct zserv
*client
, struct interface
*ifp
)
468 struct listnode
*cnode
, *cnnode
;
470 struct nbr_connected
*nc
;
472 /* Send interface addresses. */
473 for (ALL_LIST_ELEMENTS(ifp
->connected
, cnode
, cnnode
, c
)) {
474 if (!CHECK_FLAG(c
->conf
, ZEBRA_IFC_REAL
))
477 if (zsend_interface_address(ZEBRA_INTERFACE_ADDRESS_ADD
, client
,
483 /* Send interface neighbors. */
484 for (ALL_LIST_ELEMENTS(ifp
->nbr_connected
, cnode
, cnnode
, nc
)) {
485 if (zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_ADD
,
494 /* Notify client about interface moving from one VRF to another.
495 * Whether client is interested in old and new VRF is checked by caller.
497 int zsend_interface_vrf_update(struct zserv
*client
, struct interface
*ifp
,
505 zclient_create_header(s
, ZEBRA_INTERFACE_VRF_UPDATE
, ifp
->vrf_id
);
507 /* Fill in the ifIndex of the interface and its new VRF (id) */
508 stream_putl(s
, ifp
->ifindex
);
509 stream_putl(s
, vrf_id
);
511 /* Write packet size. */
512 stream_putw_at(s
, 0, stream_get_endp(s
));
514 client
->if_vrfchg_cnt
++;
515 return zebra_server_send_message(client
);
518 /* Add new nbr connected IPv6 address */
519 void nbr_connected_add_ipv6(struct interface
*ifp
, struct in6_addr
*address
)
521 struct nbr_connected
*ifc
;
525 IPV6_ADDR_COPY(&p
.u
.prefix
, address
);
526 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
528 if (!(ifc
= listnode_head(ifp
->nbr_connected
))) {
530 ifc
= nbr_connected_new();
531 ifc
->address
= prefix_new();
533 listnode_add(ifp
->nbr_connected
, ifc
);
536 prefix_copy(ifc
->address
, &p
);
538 zebra_interface_nbr_address_add_update(ifp
, ifc
);
540 if_nbr_ipv6ll_to_ipv4ll_neigh_update(ifp
, address
, 1);
543 void nbr_connected_delete_ipv6(struct interface
*ifp
, struct in6_addr
*address
)
545 struct nbr_connected
*ifc
;
549 IPV6_ADDR_COPY(&p
.u
.prefix
, address
);
550 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
552 ifc
= nbr_connected_check(ifp
, &p
);
556 listnode_delete(ifp
->nbr_connected
, ifc
);
558 zebra_interface_nbr_address_delete_update(ifp
, ifc
);
560 if_nbr_ipv6ll_to_ipv4ll_neigh_update(ifp
, address
, 0);
562 nbr_connected_free(ifc
);
566 * The cmd passed to zsend_interface_update may be ZEBRA_INTERFACE_UP or
567 * ZEBRA_INTERFACE_DOWN.
569 * The ZEBRA_INTERFACE_UP message is sent from the zebra server to
570 * the clients in one of 2 situations:
571 * - an if_up is detected e.g., as a result of an RTM_IFINFO message
572 * - a vty command modifying the bandwidth of an interface is received.
573 * The ZEBRA_INTERFACE_DOWN message is sent when an if_down is detected.
575 int zsend_interface_update(int cmd
, struct zserv
*client
, struct interface
*ifp
)
582 zclient_create_header(s
, cmd
, ifp
->vrf_id
);
583 zserv_encode_interface(s
, ifp
);
585 if (cmd
== ZEBRA_INTERFACE_UP
)
588 client
->ifdown_cnt
++;
590 return zebra_server_send_message(client
);
593 int zsend_redistribute_route(int cmd
, struct zserv
*client
, struct prefix
*p
,
594 struct prefix
*src_p
, struct route_entry
*re
)
596 struct zapi_route api
;
597 struct zapi_nexthop
*api_nh
;
598 struct nexthop
*nexthop
;
601 memset(&api
, 0, sizeof(api
));
602 api
.vrf_id
= re
->vrf_id
;
604 api
.instance
= re
->instance
;
605 api
.flags
= re
->flags
;
610 SET_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
);
611 memcpy(&api
.src_prefix
, src_p
, sizeof(api
.src_prefix
));
615 if (re
->nexthop_active_num
) {
616 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
617 api
.nexthop_num
= re
->nexthop_active_num
;
619 for (nexthop
= re
->nexthop
; nexthop
; nexthop
= nexthop
->next
) {
620 if (!CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
623 api_nh
= &api
.nexthops
[count
];
624 api_nh
->vrf_id
= nexthop
->vrf_id
;
625 api_nh
->type
= nexthop
->type
;
626 switch (nexthop
->type
) {
627 case NEXTHOP_TYPE_BLACKHOLE
:
628 api_nh
->bh_type
= nexthop
->bh_type
;
630 case NEXTHOP_TYPE_IPV4
:
631 api_nh
->gate
.ipv4
= nexthop
->gate
.ipv4
;
633 case NEXTHOP_TYPE_IPV4_IFINDEX
:
634 api_nh
->gate
.ipv4
= nexthop
->gate
.ipv4
;
635 api_nh
->ifindex
= nexthop
->ifindex
;
637 case NEXTHOP_TYPE_IFINDEX
:
638 api_nh
->ifindex
= nexthop
->ifindex
;
640 case NEXTHOP_TYPE_IPV6
:
641 api_nh
->gate
.ipv6
= nexthop
->gate
.ipv6
;
643 case NEXTHOP_TYPE_IPV6_IFINDEX
:
644 api_nh
->gate
.ipv6
= nexthop
->gate
.ipv6
;
645 api_nh
->ifindex
= nexthop
->ifindex
;
651 SET_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
);
652 api
.distance
= re
->distance
;
653 SET_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
);
654 api
.metric
= re
->metric
;
656 SET_FLAG(api
.message
, ZAPI_MESSAGE_TAG
);
659 SET_FLAG(api
.message
, ZAPI_MESSAGE_MTU
);
662 /* Encode route and send. */
663 if (zapi_route_encode(cmd
, client
->obuf
, &api
) < 0)
665 return zebra_server_send_message(client
);
668 static int zsend_write_nexthop(struct stream
*s
, struct nexthop
*nexthop
)
670 stream_putc(s
, nexthop
->type
);
671 switch (nexthop
->type
) {
672 case NEXTHOP_TYPE_IPV4
:
673 case NEXTHOP_TYPE_IPV4_IFINDEX
:
674 stream_put_in_addr(s
, &nexthop
->gate
.ipv4
);
675 stream_putl(s
, nexthop
->ifindex
);
677 case NEXTHOP_TYPE_IPV6
:
678 stream_put(s
, &nexthop
->gate
.ipv6
, 16);
680 case NEXTHOP_TYPE_IPV6_IFINDEX
:
681 stream_put(s
, &nexthop
->gate
.ipv6
, 16);
682 stream_putl(s
, nexthop
->ifindex
);
684 case NEXTHOP_TYPE_IFINDEX
:
685 stream_putl(s
, nexthop
->ifindex
);
694 /* Nexthop register */
695 static int zserv_rnh_register(struct zserv
*client
, u_short length
,
696 rnh_type_t type
, struct zebra_vrf
*zvrf
)
704 if (IS_ZEBRA_DEBUG_NHT
)
706 "rnh_register msg from client %s: length=%d, type=%s\n",
707 zebra_route_string(client
->proto
), length
,
708 (type
== RNH_NEXTHOP_TYPE
) ? "nexthop" : "route");
712 client
->nh_reg_time
= monotime(NULL
);
715 STREAM_GETC(s
, flags
);
716 STREAM_GETW(s
, p
.family
);
717 STREAM_GETC(s
, p
.prefixlen
);
719 if (p
.family
== AF_INET
) {
720 if (p
.prefixlen
> IPV4_MAX_BITLEN
) {
722 "%s: Specified prefix length %d is too large for a v4 address",
723 __PRETTY_FUNCTION__
, p
.prefixlen
);
726 STREAM_GET(&p
.u
.prefix4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
727 l
+= IPV4_MAX_BYTELEN
;
728 } else if (p
.family
== AF_INET6
) {
729 if (p
.prefixlen
> IPV6_MAX_BITLEN
) {
731 "%s: Specified prefix length %d is to large for a v6 address",
732 __PRETTY_FUNCTION__
, p
.prefixlen
);
735 STREAM_GET(&p
.u
.prefix6
, s
, IPV6_MAX_BYTELEN
);
736 l
+= IPV6_MAX_BYTELEN
;
739 "rnh_register: Received unknown family type %d\n",
743 rnh
= zebra_add_rnh(&p
, zvrf_id(zvrf
), type
);
744 if (type
== RNH_NEXTHOP_TYPE
) {
746 && !CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
))
747 SET_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
);
749 && CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
))
750 UNSET_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
);
751 } else if (type
== RNH_IMPORT_CHECK_TYPE
) {
753 && !CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
))
754 SET_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
);
755 else if (!flags
&& CHECK_FLAG(rnh
->flags
,
756 ZEBRA_NHT_EXACT_MATCH
))
757 UNSET_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
);
760 zebra_add_rnh_client(rnh
, client
, type
, zvrf_id(zvrf
));
761 /* Anything not AF_INET/INET6 has been filtered out above */
762 zebra_evaluate_rnh(zvrf_id(zvrf
), p
.family
, 1, type
, &p
);
769 /* Nexthop register */
770 static int zserv_rnh_unregister(struct zserv
*client
, u_short length
,
771 rnh_type_t type
, struct zebra_vrf
*zvrf
)
778 if (IS_ZEBRA_DEBUG_NHT
)
779 zlog_debug("rnh_unregister msg from client %s: length=%d\n",
780 zebra_route_string(client
->proto
), length
);
787 STREAM_GETC(s
, flags
);
791 STREAM_GETW(s
, p
.family
);
792 STREAM_GETC(s
, p
.prefixlen
);
794 if (p
.family
== AF_INET
) {
795 if (p
.prefixlen
> IPV4_MAX_BITLEN
) {
797 "%s: Specified prefix length %d is to large for a v4 address",
798 __PRETTY_FUNCTION__
, p
.prefixlen
);
801 STREAM_GET(&p
.u
.prefix4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
802 l
+= IPV4_MAX_BYTELEN
;
803 } else if (p
.family
== AF_INET6
) {
804 if (p
.prefixlen
> IPV6_MAX_BITLEN
) {
806 "%s: Specified prefix length %d is to large for a v6 address",
807 __PRETTY_FUNCTION__
, p
.prefixlen
);
810 STREAM_GET(&p
.u
.prefix6
, s
, IPV6_MAX_BYTELEN
);
811 l
+= IPV6_MAX_BYTELEN
;
814 "rnh_register: Received unknown family type %d\n",
818 rnh
= zebra_lookup_rnh(&p
, zvrf_id(zvrf
), type
);
820 client
->nh_dereg_time
= monotime(NULL
);
821 zebra_remove_rnh_client(rnh
, client
, type
);
828 #define ZEBRA_MIN_FEC_LENGTH 5
831 static int zserv_fec_register(struct zserv
*client
, u_short length
)
834 struct zebra_vrf
*zvrf
;
838 u_int32_t label_index
= MPLS_INVALID_LABEL_INDEX
;
841 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
843 return 0; // unexpected
846 * The minimum amount of data that can be sent for one fec
849 if (length
< ZEBRA_MIN_FEC_LENGTH
) {
851 "fec_register: Received a fec register of length %d, it is of insufficient size to properly decode",
857 STREAM_GETW(s
, flags
);
858 memset(&p
, 0, sizeof(p
));
859 STREAM_GETW(s
, p
.family
);
860 if (p
.family
!= AF_INET
&& p
.family
!= AF_INET6
) {
862 "fec_register: Received unknown family type %d\n",
866 STREAM_GETC(s
, p
.prefixlen
);
867 if ((p
.family
== AF_INET
&& p
.prefixlen
> IPV4_MAX_BITLEN
)
868 || (p
.family
== AF_INET6
869 && p
.prefixlen
> IPV6_MAX_BITLEN
)) {
871 "%s: Specified prefix length: %d is to long for %d",
872 __PRETTY_FUNCTION__
, p
.prefixlen
, p
.family
);
876 STREAM_GET(&p
.u
.prefix
, s
, PSIZE(p
.prefixlen
));
877 l
+= PSIZE(p
.prefixlen
);
878 if (flags
& ZEBRA_FEC_REGISTER_LABEL_INDEX
) {
879 STREAM_GETL(s
, label_index
);
882 label_index
= MPLS_INVALID_LABEL_INDEX
;
883 zebra_mpls_fec_register(zvrf
, &p
, label_index
, client
);
891 static int zserv_fec_unregister(struct zserv
*client
, u_short length
)
894 struct zebra_vrf
*zvrf
;
900 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
902 return 0; // unexpected
905 * The minimum amount of data that can be sent for one
908 if (length
< ZEBRA_MIN_FEC_LENGTH
) {
910 "fec_unregister: Received a fec unregister of length %d, it is of insufficient size to properly decode",
916 STREAM_GETW(s
, flags
);
920 memset(&p
, 0, sizeof(p
));
921 STREAM_GETW(s
, p
.family
);
922 if (p
.family
!= AF_INET
&& p
.family
!= AF_INET6
) {
924 "fec_unregister: Received unknown family type %d\n",
928 STREAM_GETC(s
, p
.prefixlen
);
929 if ((p
.family
== AF_INET
&& p
.prefixlen
> IPV4_MAX_BITLEN
)
930 || (p
.family
== AF_INET6
931 && p
.prefixlen
> IPV6_MAX_BITLEN
)) {
933 "%s: Received prefix length %d which is greater than %d can support",
934 __PRETTY_FUNCTION__
, p
.prefixlen
, p
.family
);
938 STREAM_GET(&p
.u
.prefix
, s
, PSIZE(p
.prefixlen
));
939 l
+= PSIZE(p
.prefixlen
);
940 zebra_mpls_fec_unregister(zvrf
, &p
, client
);
948 Modified version of zsend_ipv4_nexthop_lookup():
949 Query unicast rib if nexthop is not found on mrib.
950 Returns both route metric and protocol distance.
952 static int zsend_ipv4_nexthop_lookup_mrib(struct zserv
*client
,
954 struct route_entry
*re
,
955 struct zebra_vrf
*zvrf
)
960 struct nexthop
*nexthop
;
962 /* Get output stream. */
966 /* Fill in result. */
967 zclient_create_header(s
, ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB
, zvrf_id(zvrf
));
968 stream_put_in_addr(s
, &addr
);
971 stream_putc(s
, re
->distance
);
972 stream_putl(s
, re
->metric
);
974 nump
= stream_get_endp(
975 s
); /* remember position for nexthop_num */
976 stream_putc(s
, 0); /* reserve room for nexthop_num */
977 /* Only non-recursive routes are elegible to resolve the nexthop
979 * are looking up. Therefore, we will just iterate over the top
980 * chain of nexthops. */
981 for (nexthop
= re
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
982 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
983 num
+= zsend_write_nexthop(s
, nexthop
);
985 stream_putc_at(s
, nump
, num
); /* store nexthop_num */
987 stream_putc(s
, 0); /* distance */
988 stream_putl(s
, 0); /* metric */
989 stream_putc(s
, 0); /* nexthop_num */
992 stream_putw_at(s
, 0, stream_get_endp(s
));
994 return zebra_server_send_message(client
);
997 int zsend_route_notify_owner(struct route_entry
*re
, struct prefix
*p
,
998 enum zapi_route_notify_owner note
)
1000 struct zserv
*client
;
1004 client
= zebra_find_client(re
->type
, re
->instance
);
1005 if (!client
|| !client
->notify_owner
) {
1006 if (IS_ZEBRA_DEBUG_PACKET
) {
1007 char buff
[PREFIX_STRLEN
];
1010 "Not Notifying Owner: %u about prefix %s(%u) %d",
1011 re
->type
, prefix2str(p
, buff
, sizeof(buff
)),
1017 if (IS_ZEBRA_DEBUG_PACKET
) {
1018 char buff
[PREFIX_STRLEN
];
1020 zlog_debug("Notifying Owner: %u about prefix %s(%u) %d",
1021 re
->type
, prefix2str(p
, buff
, sizeof(buff
)),
1028 zclient_create_header(s
, ZEBRA_ROUTE_NOTIFY_OWNER
, re
->vrf_id
);
1030 stream_put(s
, ¬e
, sizeof(note
));
1032 stream_putc(s
, p
->family
);
1034 blen
= prefix_blen(p
);
1035 stream_putc(s
, p
->prefixlen
);
1036 stream_put(s
, &p
->u
.prefix
, blen
);
1038 stream_putl(s
, re
->table
);
1040 stream_putw_at(s
, 0, stream_get_endp(s
));
1042 return zebra_server_send_message(client
);
1045 /* Router-id is updated. Send ZEBRA_ROUTER_ID_ADD to client. */
1046 int zsend_router_id_update(struct zserv
*client
, struct prefix
*p
,
1052 /* Check this client need interface information. */
1053 if (!vrf_bitmap_check(client
->ridinfo
, vrf_id
))
1060 zclient_create_header(s
, ZEBRA_ROUTER_ID_UPDATE
, vrf_id
);
1062 /* Prefix information. */
1063 stream_putc(s
, p
->family
);
1064 blen
= prefix_blen(p
);
1065 stream_put(s
, &p
->u
.prefix
, blen
);
1066 stream_putc(s
, p
->prefixlen
);
1068 /* Write packet size. */
1069 stream_putw_at(s
, 0, stream_get_endp(s
));
1071 return zebra_server_send_message(client
);
1075 * Function used by Zebra to send a PW status update to LDP daemon
1077 int zsend_pw_update(struct zserv
*client
, struct zebra_pw
*pw
)
1084 zclient_create_header(s
, ZEBRA_PW_STATUS_UPDATE
, pw
->vrf_id
);
1085 stream_write(s
, pw
->ifname
, IF_NAMESIZE
);
1086 stream_putl(s
, pw
->ifindex
);
1087 stream_putl(s
, pw
->status
);
1089 /* Put length at the first point of the stream. */
1090 stream_putw_at(s
, 0, stream_get_endp(s
));
1092 return zebra_server_send_message(client
);
1095 /* Register zebra server interface information. Send current all
1096 interface and address information. */
1097 static int zread_interface_add(struct zserv
*client
, u_short length
,
1098 struct zebra_vrf
*zvrf
)
1101 struct interface
*ifp
;
1103 /* Interface information is needed. */
1104 vrf_bitmap_set(client
->ifinfo
, zvrf_id(zvrf
));
1106 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
1107 FOR_ALL_INTERFACES (vrf
, ifp
) {
1108 /* Skip pseudo interface. */
1109 if (!CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_ACTIVE
))
1112 if (zsend_interface_add(client
, ifp
) < 0)
1115 if (zsend_interface_addresses(client
, ifp
) < 0)
1122 /* Unregister zebra server interface information. */
1123 static int zread_interface_delete(struct zserv
*client
, u_short length
,
1124 struct zebra_vrf
*zvrf
)
1126 vrf_bitmap_unset(client
->ifinfo
, zvrf_id(zvrf
));
1130 void zserv_nexthop_num_warn(const char *caller
, const struct prefix
*p
,
1131 const unsigned int nexthop_num
)
1133 if (nexthop_num
> multipath_num
) {
1134 char buff
[PREFIX2STR_BUFFER
];
1135 prefix2str(p
, buff
, sizeof(buff
));
1137 "%s: Prefix %s has %d nexthops, but we can only use the first %d",
1138 caller
, buff
, nexthop_num
, multipath_num
);
1142 static int zread_route_add(struct zserv
*client
, u_short length
,
1143 struct zebra_vrf
*zvrf
)
1146 struct zapi_route api
;
1147 struct zapi_nexthop
*api_nh
;
1149 struct prefix_ipv6
*src_p
= NULL
;
1150 struct route_entry
*re
;
1151 struct nexthop
*nexthop
= NULL
;
1153 vrf_id_t vrf_id
= 0;
1156 if (zapi_route_decode(s
, &api
) < 0)
1159 /* Allocate new route. */
1160 vrf_id
= zvrf_id(zvrf
);
1161 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
1162 re
->type
= api
.type
;
1163 re
->instance
= api
.instance
;
1164 re
->flags
= api
.flags
;
1165 re
->uptime
= time(NULL
);
1166 re
->vrf_id
= vrf_id
;
1167 if (api
.tableid
&& vrf_id
== VRF_DEFAULT
)
1168 re
->table
= api
.tableid
;
1170 re
->table
= zvrf
->table_id
;
1172 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
)) {
1173 for (i
= 0; i
< api
.nexthop_num
; i
++) {
1174 api_nh
= &api
.nexthops
[i
];
1175 ifindex_t ifindex
= 0;
1177 switch (api_nh
->type
) {
1178 case NEXTHOP_TYPE_IFINDEX
:
1179 nexthop
= route_entry_nexthop_ifindex_add(
1180 re
, api_nh
->ifindex
, api_nh
->vrf_id
);
1182 case NEXTHOP_TYPE_IPV4
:
1183 nexthop
= route_entry_nexthop_ipv4_add(
1184 re
, &api_nh
->gate
.ipv4
, NULL
,
1187 case NEXTHOP_TYPE_IPV4_IFINDEX
: {
1189 struct ipaddr vtep_ip
;
1191 memset(&vtep_ip
, 0, sizeof(struct ipaddr
));
1192 if (CHECK_FLAG(api
.flags
,
1193 ZEBRA_FLAG_EVPN_ROUTE
)) {
1194 ifindex
= get_l3vni_svi_ifindex(vrf_id
);
1196 ifindex
= api_nh
->ifindex
;
1199 nexthop
= route_entry_nexthop_ipv4_ifindex_add(
1200 re
, &api_nh
->gate
.ipv4
, NULL
, ifindex
,
1203 /* if this an EVPN route entry,
1204 program the nh as neigh
1206 if (CHECK_FLAG(api
.flags
,
1207 ZEBRA_FLAG_EVPN_ROUTE
)) {
1208 SET_FLAG(nexthop
->flags
,
1209 NEXTHOP_FLAG_EVPN_RVTEP
);
1210 vtep_ip
.ipa_type
= IPADDR_V4
;
1211 memcpy(&(vtep_ip
.ipaddr_v4
),
1212 &(api_nh
->gate
.ipv4
),
1213 sizeof(struct in_addr
));
1214 zebra_vxlan_evpn_vrf_route_add(
1215 vrf_id
, &api
.rmac
, &vtep_ip
,
1220 case NEXTHOP_TYPE_IPV6
:
1221 nexthop
= route_entry_nexthop_ipv6_add(
1222 re
, &api_nh
->gate
.ipv6
, api_nh
->vrf_id
);
1224 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1225 nexthop
= route_entry_nexthop_ipv6_ifindex_add(
1226 re
, &api_nh
->gate
.ipv6
, api_nh
->ifindex
,
1229 case NEXTHOP_TYPE_BLACKHOLE
:
1230 nexthop
= route_entry_nexthop_blackhole_add(
1231 re
, api_nh
->bh_type
);
1237 "%s: Nexthops Specified: %d but we failed to properly create one",
1238 __PRETTY_FUNCTION__
, api
.nexthop_num
);
1239 nexthops_free(re
->nexthop
);
1240 XFREE(MTYPE_RE
, re
);
1243 /* MPLS labels for BGP-LU or Segment Routing */
1244 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_LABEL
)
1245 && api_nh
->type
!= NEXTHOP_TYPE_IFINDEX
1246 && api_nh
->type
!= NEXTHOP_TYPE_BLACKHOLE
) {
1247 enum lsp_types_t label_type
;
1250 lsp_type_from_re_type(client
->proto
);
1251 nexthop_add_labels(nexthop
, label_type
,
1253 &api_nh
->labels
[0]);
1258 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
))
1259 re
->distance
= api
.distance
;
1260 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
))
1261 re
->metric
= api
.metric
;
1262 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_TAG
))
1264 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_MTU
))
1267 afi
= family2afi(api
.prefix
.family
);
1268 if (afi
!= AFI_IP6
&& CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
)) {
1269 zlog_warn("%s: Received SRC Prefix but afi is not v6",
1270 __PRETTY_FUNCTION__
);
1271 nexthops_free(re
->nexthop
);
1272 XFREE(MTYPE_RE
, re
);
1275 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
))
1276 src_p
= &api
.src_prefix
;
1278 ret
= rib_add_multipath(afi
, api
.safi
, &api
.prefix
, src_p
, re
);
1281 switch (api
.prefix
.family
) {
1284 client
->v4_route_add_cnt
++;
1286 client
->v4_route_upd8_cnt
++;
1290 client
->v6_route_add_cnt
++;
1292 client
->v6_route_upd8_cnt
++;
1299 static int zread_route_del(struct zserv
*client
, u_short length
,
1300 struct zebra_vrf
*zvrf
)
1303 struct zapi_route api
;
1305 struct prefix_ipv6
*src_p
= NULL
;
1308 if (zapi_route_decode(s
, &api
) < 0)
1311 afi
= family2afi(api
.prefix
.family
);
1312 if (afi
!= AFI_IP6
&& CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
)) {
1313 zlog_warn("%s: Received a src prefix while afi is not v6",
1314 __PRETTY_FUNCTION__
);
1317 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
))
1318 src_p
= &api
.src_prefix
;
1320 rib_delete(afi
, api
.safi
, zvrf_id(zvrf
), api
.type
, api
.instance
,
1321 api
.flags
, &api
.prefix
, src_p
, NULL
, zvrf
->table_id
,
1322 api
.metric
, false, &api
.rmac
);
1325 switch (api
.prefix
.family
) {
1327 client
->v4_route_del_cnt
++;
1330 client
->v6_route_del_cnt
++;
1337 /* This function support multiple nexthop. */
1339 * Parse the ZEBRA_IPV4_ROUTE_ADD sent from client. Update re and
1342 static int zread_ipv4_add(struct zserv
*client
, u_short length
,
1343 struct zebra_vrf
*zvrf
)
1346 struct route_entry
*re
;
1349 struct in_addr nhop_addr
;
1351 u_char nexthop_type
;
1356 enum lsp_types_t label_type
= ZEBRA_LSP_NONE
;
1358 struct nexthop
*nexthop
;
1359 enum blackhole_type bh_type
= BLACKHOLE_NULL
;
1361 /* Get input stream. */
1364 /* Allocate new re. */
1365 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
1367 /* Type, flags, message. */
1368 STREAM_GETC(s
, re
->type
);
1369 if (re
->type
> ZEBRA_ROUTE_MAX
) {
1370 zlog_warn("%s: Specified route type %d is not a legal value\n",
1371 __PRETTY_FUNCTION__
, re
->type
);
1372 XFREE(MTYPE_RE
, re
);
1375 STREAM_GETW(s
, re
->instance
);
1376 STREAM_GETL(s
, re
->flags
);
1377 STREAM_GETC(s
, message
);
1378 STREAM_GETW(s
, safi
);
1379 re
->uptime
= time(NULL
);
1382 memset(&p
, 0, sizeof(struct prefix_ipv4
));
1384 STREAM_GETC(s
, p
.prefixlen
);
1385 if (p
.prefixlen
> IPV4_MAX_BITLEN
) {
1387 "%s: Specified prefix length %d is greater than what v4 can be",
1388 __PRETTY_FUNCTION__
, p
.prefixlen
);
1389 XFREE(MTYPE_RE
, re
);
1392 STREAM_GET(&p
.u
.prefix4
, s
, PSIZE(p
.prefixlen
));
1395 re
->vrf_id
= zvrf_id(zvrf
);
1397 /* Nexthop parse. */
1398 if (CHECK_FLAG(message
, ZAPI_MESSAGE_NEXTHOP
)) {
1399 STREAM_GETC(s
, nexthop_num
);
1400 zserv_nexthop_num_warn(__func__
, (const struct prefix
*)&p
,
1403 if (CHECK_FLAG(message
, ZAPI_MESSAGE_LABEL
))
1404 label_type
= lsp_type_from_re_type(client
->proto
);
1406 for (i
= 0; i
< nexthop_num
; i
++) {
1407 STREAM_GETC(s
, nexthop_type
);
1409 switch (nexthop_type
) {
1410 case NEXTHOP_TYPE_IFINDEX
:
1411 STREAM_GETL(s
, ifindex
);
1412 route_entry_nexthop_ifindex_add(re
, ifindex
,
1415 case NEXTHOP_TYPE_IPV4
:
1416 STREAM_GET(&nhop_addr
.s_addr
, s
,
1418 nexthop
= route_entry_nexthop_ipv4_add(
1419 re
, &nhop_addr
, NULL
, re
->vrf_id
);
1420 /* For labeled-unicast, each nexthop is followed
1422 if (CHECK_FLAG(message
, ZAPI_MESSAGE_LABEL
)) {
1423 STREAM_GETL(s
, label
);
1424 nexthop_add_labels(nexthop
, label_type
,
1428 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1429 STREAM_GET(&nhop_addr
.s_addr
, s
,
1431 STREAM_GETL(s
, ifindex
);
1432 route_entry_nexthop_ipv4_ifindex_add(
1433 re
, &nhop_addr
, NULL
, ifindex
,
1436 case NEXTHOP_TYPE_IPV6
:
1438 "%s: Please use ZEBRA_ROUTE_ADD if you want to pass v6 nexthops",
1439 __PRETTY_FUNCTION__
);
1440 nexthops_free(re
->nexthop
);
1441 XFREE(MTYPE_RE
, re
);
1444 case NEXTHOP_TYPE_BLACKHOLE
:
1445 route_entry_nexthop_blackhole_add(re
, bh_type
);
1449 "%s: Specified nexthop type: %d does not exist",
1450 __PRETTY_FUNCTION__
, nexthop_type
);
1451 nexthops_free(re
->nexthop
);
1452 XFREE(MTYPE_RE
, re
);
1459 if (CHECK_FLAG(message
, ZAPI_MESSAGE_DISTANCE
))
1460 STREAM_GETC(s
, re
->distance
);
1463 if (CHECK_FLAG(message
, ZAPI_MESSAGE_METRIC
))
1464 STREAM_GETL(s
, re
->metric
);
1467 if (CHECK_FLAG(message
, ZAPI_MESSAGE_TAG
))
1468 STREAM_GETL(s
, re
->tag
);
1472 if (CHECK_FLAG(message
, ZAPI_MESSAGE_MTU
))
1473 STREAM_GETL(s
, re
->mtu
);
1478 re
->table
= zvrf
->table_id
;
1480 ret
= rib_add_multipath(AFI_IP
, safi
, &p
, NULL
, re
);
1484 client
->v4_route_add_cnt
++;
1486 client
->v4_route_upd8_cnt
++;
1491 nexthops_free(re
->nexthop
);
1492 XFREE(MTYPE_RE
, re
);
1496 /* Zebra server IPv4 prefix delete function. */
1497 static int zread_ipv4_delete(struct zserv
*client
, u_short length
,
1498 struct zebra_vrf
*zvrf
)
1501 struct zapi_ipv4 api
;
1507 /* Type, flags, message. */
1508 STREAM_GETC(s
, api
.type
);
1509 STREAM_GETW(s
, api
.instance
);
1510 STREAM_GETL(s
, api
.flags
);
1511 STREAM_GETC(s
, api
.message
);
1512 STREAM_GETW(s
, api
.safi
);
1515 memset(&p
, 0, sizeof(struct prefix
));
1517 STREAM_GETC(s
, p
.prefixlen
);
1518 if (p
.prefixlen
> IPV4_MAX_BITLEN
) {
1519 zlog_warn("%s: Passed in prefixlen %d is impossible",
1520 __PRETTY_FUNCTION__
, p
.prefixlen
);
1523 STREAM_GET(&p
.u
.prefix4
, s
, PSIZE(p
.prefixlen
));
1525 table_id
= zvrf
->table_id
;
1527 rib_delete(AFI_IP
, api
.safi
, zvrf_id(zvrf
), api
.type
, api
.instance
,
1528 api
.flags
, &p
, NULL
, NULL
, table_id
, 0, false, NULL
);
1529 client
->v4_route_del_cnt
++;
1535 /* MRIB Nexthop lookup for IPv4. */
1536 static int zread_ipv4_nexthop_lookup_mrib(struct zserv
*client
, u_short length
,
1537 struct zebra_vrf
*zvrf
)
1539 struct in_addr addr
;
1540 struct route_entry
*re
;
1542 STREAM_GET(&addr
.s_addr
, client
->ibuf
, IPV4_MAX_BYTELEN
);
1543 re
= rib_match_ipv4_multicast(zvrf_id(zvrf
), addr
, NULL
);
1544 return zsend_ipv4_nexthop_lookup_mrib(client
, addr
, re
, zvrf
);
1550 /* Zebra server IPv6 prefix add function. */
1551 static int zread_ipv4_route_ipv6_nexthop_add(struct zserv
*client
,
1553 struct zebra_vrf
*zvrf
)
1557 struct in6_addr nhop_addr
;
1558 struct route_entry
*re
;
1561 u_char nexthop_type
;
1564 static struct in6_addr nexthops
[MULTIPATH_NUM
];
1565 static unsigned int ifindices
[MULTIPATH_NUM
];
1567 static mpls_label_t labels
[MULTIPATH_NUM
];
1568 enum lsp_types_t label_type
= ZEBRA_LSP_NONE
;
1570 struct nexthop
*nexthop
;
1571 enum blackhole_type bh_type
= BLACKHOLE_NULL
;
1573 /* Get input stream. */
1576 memset(&nhop_addr
, 0, sizeof(struct in6_addr
));
1578 /* Allocate new re. */
1579 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
1581 /* Type, flags, message. */
1582 STREAM_GETC(s
, re
->type
);
1583 if (re
->type
> ZEBRA_ROUTE_MAX
) {
1584 zlog_warn("%s: Specified route type: %d is not a legal value\n",
1585 __PRETTY_FUNCTION__
, re
->type
);
1586 XFREE(MTYPE_RE
, re
);
1589 STREAM_GETW(s
, re
->instance
);
1590 STREAM_GETL(s
, re
->flags
);
1591 STREAM_GETC(s
, message
);
1592 STREAM_GETW(s
, safi
);
1593 re
->uptime
= time(NULL
);
1596 memset(&p
, 0, sizeof(struct prefix_ipv4
));
1598 STREAM_GETC(s
, p
.prefixlen
);
1599 if (p
.prefixlen
> IPV4_MAX_BITLEN
) {
1601 "%s: Prefix Length %d is greater than what a v4 address can use",
1602 __PRETTY_FUNCTION__
, p
.prefixlen
);
1603 XFREE(MTYPE_RE
, re
);
1606 STREAM_GET(&p
.u
.prefix4
, s
, PSIZE(p
.prefixlen
));
1609 re
->vrf_id
= zvrf_id(zvrf
);
1611 /* We need to give nh-addr, nh-ifindex with the same next-hop object
1612 * to the re to ensure that IPv6 multipathing works; need to coalesce
1613 * these. Clients should send the same number of paired set of
1614 * next-hop-addr/next-hop-ifindices. */
1615 if (CHECK_FLAG(message
, ZAPI_MESSAGE_NEXTHOP
)) {
1616 unsigned int nh_count
= 0;
1617 unsigned int if_count
= 0;
1618 unsigned int max_nh_if
= 0;
1620 STREAM_GETC(s
, nexthop_num
);
1621 zserv_nexthop_num_warn(__func__
, (const struct prefix
*)&p
,
1624 if (CHECK_FLAG(message
, ZAPI_MESSAGE_LABEL
))
1625 label_type
= lsp_type_from_re_type(client
->proto
);
1627 for (i
= 0; i
< nexthop_num
; i
++) {
1628 STREAM_GETC(s
, nexthop_type
);
1630 switch (nexthop_type
) {
1631 case NEXTHOP_TYPE_IPV6
:
1632 STREAM_GET(&nhop_addr
, s
, 16);
1633 if (nh_count
< MULTIPATH_NUM
) {
1634 /* For labeled-unicast, each nexthop is
1635 * followed by label. */
1636 if (CHECK_FLAG(message
,
1637 ZAPI_MESSAGE_LABEL
)) {
1638 STREAM_GETL(s
, label
);
1639 labels
[nh_count
] = label
;
1641 nexthops
[nh_count
] = nhop_addr
;
1645 case NEXTHOP_TYPE_IFINDEX
:
1646 if (if_count
< multipath_num
) {
1647 STREAM_GETL(s
, ifindices
[if_count
++]);
1650 case NEXTHOP_TYPE_BLACKHOLE
:
1651 route_entry_nexthop_blackhole_add(re
, bh_type
);
1655 "%s: Please use ZEBRA_ROUTE_ADD if you want to pass non v6 nexthops",
1656 __PRETTY_FUNCTION__
);
1657 nexthops_free(re
->nexthop
);
1658 XFREE(MTYPE_RE
, re
);
1663 max_nh_if
= (nh_count
> if_count
) ? nh_count
: if_count
;
1664 for (i
= 0; i
< max_nh_if
; i
++) {
1666 && !IN6_IS_ADDR_UNSPECIFIED(&nexthops
[i
])) {
1667 if ((i
< if_count
) && ifindices
[i
])
1669 route_entry_nexthop_ipv6_ifindex_add(
1674 nexthop
= route_entry_nexthop_ipv6_add(
1675 re
, &nexthops
[i
], re
->vrf_id
);
1677 if (CHECK_FLAG(message
, ZAPI_MESSAGE_LABEL
))
1678 nexthop_add_labels(nexthop
, label_type
,
1681 if ((i
< if_count
) && ifindices
[i
])
1682 route_entry_nexthop_ifindex_add(
1683 re
, ifindices
[i
], re
->vrf_id
);
1689 if (CHECK_FLAG(message
, ZAPI_MESSAGE_DISTANCE
))
1690 STREAM_GETC(s
, re
->distance
);
1693 if (CHECK_FLAG(message
, ZAPI_MESSAGE_METRIC
))
1694 STREAM_GETL(s
, re
->metric
);
1697 if (CHECK_FLAG(message
, ZAPI_MESSAGE_TAG
))
1698 STREAM_GETL(s
, re
->tag
);
1702 if (CHECK_FLAG(message
, ZAPI_MESSAGE_MTU
))
1703 STREAM_GETL(s
, re
->mtu
);
1708 re
->table
= zvrf
->table_id
;
1710 ret
= rib_add_multipath(AFI_IP6
, safi
, &p
, NULL
, re
);
1713 client
->v4_route_add_cnt
++;
1715 client
->v4_route_upd8_cnt
++;
1720 nexthops_free(re
->nexthop
);
1721 XFREE(MTYPE_RE
, re
);
1725 static int zread_ipv6_add(struct zserv
*client
, u_short length
,
1726 struct zebra_vrf
*zvrf
)
1730 struct in6_addr nhop_addr
;
1732 struct route_entry
*re
;
1735 u_char nexthop_type
;
1737 struct prefix_ipv6 src_p
, *src_pp
;
1739 static struct in6_addr nexthops
[MULTIPATH_NUM
];
1740 static unsigned int ifindices
[MULTIPATH_NUM
];
1742 static mpls_label_t labels
[MULTIPATH_NUM
];
1743 enum lsp_types_t label_type
= ZEBRA_LSP_NONE
;
1745 struct nexthop
*nexthop
;
1746 enum blackhole_type bh_type
= BLACKHOLE_NULL
;
1748 /* Get input stream. */
1751 memset(&nhop_addr
, 0, sizeof(struct in6_addr
));
1753 /* Allocate new re. */
1754 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
1756 /* Type, flags, message. */
1757 STREAM_GETC(s
, re
->type
);
1758 if (re
->type
> ZEBRA_ROUTE_MAX
) {
1759 zlog_warn("%s: Specified route type: %d is not a legal value\n",
1760 __PRETTY_FUNCTION__
, re
->type
);
1761 XFREE(MTYPE_RE
, re
);
1764 STREAM_GETW(s
, re
->instance
);
1765 STREAM_GETL(s
, re
->flags
);
1766 STREAM_GETC(s
, message
);
1767 STREAM_GETW(s
, safi
);
1768 re
->uptime
= time(NULL
);
1771 memset(&p
, 0, sizeof(p
));
1772 p
.family
= AF_INET6
;
1773 STREAM_GETC(s
, p
.prefixlen
);
1774 if (p
.prefixlen
> IPV6_MAX_BITLEN
) {
1776 "%s: Specified prefix length %d is to large for v6 prefix",
1777 __PRETTY_FUNCTION__
, p
.prefixlen
);
1778 XFREE(MTYPE_RE
, re
);
1781 STREAM_GET(&p
.u
.prefix6
, s
, PSIZE(p
.prefixlen
));
1783 if (CHECK_FLAG(message
, ZAPI_MESSAGE_SRCPFX
)) {
1784 memset(&src_p
, 0, sizeof(src_p
));
1785 src_p
.family
= AF_INET6
;
1786 STREAM_GETC(s
, src_p
.prefixlen
);
1787 if (src_p
.prefixlen
> IPV6_MAX_BITLEN
) {
1789 "%s: Specified src prefix length %d is to large for v6 prefix",
1790 __PRETTY_FUNCTION__
, src_p
.prefixlen
);
1791 XFREE(MTYPE_RE
, re
);
1794 STREAM_GET(&src_p
.prefix
, s
, PSIZE(src_p
.prefixlen
));
1800 re
->vrf_id
= zvrf_id(zvrf
);
1802 /* We need to give nh-addr, nh-ifindex with the same next-hop object
1803 * to the re to ensure that IPv6 multipathing works; need to coalesce
1804 * these. Clients should send the same number of paired set of
1805 * next-hop-addr/next-hop-ifindices. */
1806 if (CHECK_FLAG(message
, ZAPI_MESSAGE_NEXTHOP
)) {
1807 unsigned int nh_count
= 0;
1808 unsigned int if_count
= 0;
1809 unsigned int max_nh_if
= 0;
1811 STREAM_GETC(s
, nexthop_num
);
1812 zserv_nexthop_num_warn(__func__
, (const struct prefix
*)&p
,
1815 if (CHECK_FLAG(message
, ZAPI_MESSAGE_LABEL
))
1816 label_type
= lsp_type_from_re_type(client
->proto
);
1818 for (i
= 0; i
< nexthop_num
; i
++) {
1819 STREAM_GETC(s
, nexthop_type
);
1821 switch (nexthop_type
) {
1822 case NEXTHOP_TYPE_IPV6
:
1823 STREAM_GET(&nhop_addr
, s
, 16);
1824 if (nh_count
< MULTIPATH_NUM
) {
1825 /* For labeled-unicast, each nexthop is
1826 * followed by label. */
1827 if (CHECK_FLAG(message
,
1828 ZAPI_MESSAGE_LABEL
)) {
1829 STREAM_GETL(s
, label
);
1830 labels
[nh_count
] = label
;
1832 nexthops
[nh_count
++] = nhop_addr
;
1835 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1836 STREAM_GET(&nhop_addr
, s
, 16);
1837 STREAM_GETL(s
, ifindex
);
1838 route_entry_nexthop_ipv6_ifindex_add(
1839 re
, &nhop_addr
, ifindex
, re
->vrf_id
);
1841 case NEXTHOP_TYPE_IFINDEX
:
1842 if (if_count
< multipath_num
) {
1843 STREAM_GETL(s
, ifindices
[if_count
++]);
1846 case NEXTHOP_TYPE_BLACKHOLE
:
1847 route_entry_nexthop_blackhole_add(re
, bh_type
);
1851 "%s: Please use ZEBRA_ROUTE_ADD if you want to pass non v6 nexthops",
1852 __PRETTY_FUNCTION__
);
1853 nexthops_free(re
->nexthop
);
1854 XFREE(MTYPE_RE
, re
);
1859 max_nh_if
= (nh_count
> if_count
) ? nh_count
: if_count
;
1860 for (i
= 0; i
< max_nh_if
; i
++) {
1862 && !IN6_IS_ADDR_UNSPECIFIED(&nexthops
[i
])) {
1863 if ((i
< if_count
) && ifindices
[i
])
1865 route_entry_nexthop_ipv6_ifindex_add(
1870 nexthop
= route_entry_nexthop_ipv6_add(
1871 re
, &nexthops
[i
], re
->vrf_id
);
1872 if (CHECK_FLAG(message
, ZAPI_MESSAGE_LABEL
))
1873 nexthop_add_labels(nexthop
, label_type
,
1876 if ((i
< if_count
) && ifindices
[i
])
1877 route_entry_nexthop_ifindex_add(
1878 re
, ifindices
[i
], re
->vrf_id
);
1884 if (CHECK_FLAG(message
, ZAPI_MESSAGE_DISTANCE
))
1885 STREAM_GETC(s
, re
->distance
);
1888 if (CHECK_FLAG(message
, ZAPI_MESSAGE_METRIC
))
1889 STREAM_GETL(s
, re
->metric
);
1892 if (CHECK_FLAG(message
, ZAPI_MESSAGE_TAG
))
1893 STREAM_GETL(s
, re
->tag
);
1897 if (CHECK_FLAG(message
, ZAPI_MESSAGE_MTU
))
1898 STREAM_GETL(s
, re
->mtu
);
1902 re
->table
= zvrf
->table_id
;
1904 ret
= rib_add_multipath(AFI_IP6
, safi
, &p
, src_pp
, re
);
1907 client
->v6_route_add_cnt
++;
1909 client
->v6_route_upd8_cnt
++;
1914 nexthops_free(re
->nexthop
);
1915 XFREE(MTYPE_RE
, re
);
1920 /* Zebra server IPv6 prefix delete function. */
1921 static int zread_ipv6_delete(struct zserv
*client
, u_short length
,
1922 struct zebra_vrf
*zvrf
)
1925 struct zapi_ipv6 api
;
1927 struct prefix_ipv6 src_p
, *src_pp
;
1931 /* Type, flags, message. */
1932 STREAM_GETC(s
, api
.type
);
1933 STREAM_GETW(s
, api
.instance
);
1934 STREAM_GETL(s
, api
.flags
);
1935 STREAM_GETC(s
, api
.message
);
1936 STREAM_GETW(s
, api
.safi
);
1939 memset(&p
, 0, sizeof(struct prefix
));
1940 p
.family
= AF_INET6
;
1941 STREAM_GETC(s
, p
.prefixlen
);
1942 STREAM_GET(&p
.u
.prefix6
, s
, PSIZE(p
.prefixlen
));
1944 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
)) {
1945 memset(&src_p
, 0, sizeof(struct prefix_ipv6
));
1946 src_p
.family
= AF_INET6
;
1947 STREAM_GETC(s
, src_p
.prefixlen
);
1948 STREAM_GET(&src_p
.prefix
, s
, PSIZE(src_p
.prefixlen
));
1953 rib_delete(AFI_IP6
, api
.safi
, zvrf_id(zvrf
), api
.type
, api
.instance
,
1954 api
.flags
, &p
, src_pp
, NULL
, client
->rtm_table
, 0, false,
1957 client
->v6_route_del_cnt
++;
1963 /* Register zebra server router-id information. Send current router-id */
1964 static int zread_router_id_add(struct zserv
*client
, u_short length
,
1965 struct zebra_vrf
*zvrf
)
1969 /* Router-id information is needed. */
1970 vrf_bitmap_set(client
->ridinfo
, zvrf_id(zvrf
));
1972 router_id_get(&p
, zvrf_id(zvrf
));
1974 return zsend_router_id_update(client
, &p
, zvrf_id(zvrf
));
1977 /* Unregister zebra server router-id information. */
1978 static int zread_router_id_delete(struct zserv
*client
, u_short length
,
1979 struct zebra_vrf
*zvrf
)
1981 vrf_bitmap_unset(client
->ridinfo
, zvrf_id(zvrf
));
1985 /* Tie up route-type and client->sock */
1986 static void zread_hello(struct zserv
*client
)
1988 /* type of protocol (lib/zebra.h) */
1993 STREAM_GETC(client
->ibuf
, proto
);
1994 STREAM_GETW(client
->ibuf
, instance
);
1995 STREAM_GETC(client
->ibuf
, notify
);
1997 client
->notify_owner
= true;
1999 /* accept only dynamic routing protocols */
2000 if ((proto
< ZEBRA_ROUTE_MAX
) && (proto
> ZEBRA_ROUTE_STATIC
)) {
2002 "client %d says hello and bids fair to announce only %s routes",
2003 client
->sock
, zebra_route_string(proto
));
2005 zlog_notice("client protocol instance %d", instance
);
2007 client
->proto
= proto
;
2008 client
->instance
= instance
;
2015 /* Unregister all information in a VRF. */
2016 static int zread_vrf_unregister(struct zserv
*client
, u_short length
,
2017 struct zebra_vrf
*zvrf
)
2022 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
2023 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
2024 vrf_bitmap_unset(client
->redist
[afi
][i
], zvrf_id(zvrf
));
2025 vrf_bitmap_unset(client
->redist_default
, zvrf_id(zvrf
));
2026 vrf_bitmap_unset(client
->ifinfo
, zvrf_id(zvrf
));
2027 vrf_bitmap_unset(client
->ridinfo
, zvrf_id(zvrf
));
2032 static void zread_mpls_labels(int command
, struct zserv
*client
, u_short length
,
2033 struct zebra_vrf
*zvrf
)
2036 enum lsp_types_t type
;
2037 struct prefix prefix
;
2038 enum nexthop_types_t gtype
;
2041 mpls_label_t in_label
, out_label
;
2044 /* Get input stream. */
2048 STREAM_GETC(s
, type
);
2049 STREAM_GETL(s
, prefix
.family
);
2050 switch (prefix
.family
) {
2052 STREAM_GET(&prefix
.u
.prefix4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
2053 STREAM_GETC(s
, prefix
.prefixlen
);
2054 if (prefix
.prefixlen
> IPV4_MAX_BITLEN
) {
2056 "%s: Specified prefix length %d is greater than a v4 address can support",
2057 __PRETTY_FUNCTION__
, prefix
.prefixlen
);
2060 STREAM_GET(&gate
.ipv4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
2063 STREAM_GET(&prefix
.u
.prefix6
, s
, 16);
2064 STREAM_GETC(s
, prefix
.prefixlen
);
2065 if (prefix
.prefixlen
> IPV6_MAX_BITLEN
) {
2067 "%s: Specified prefix length %d is greater than a v6 address can support",
2068 __PRETTY_FUNCTION__
, prefix
.prefixlen
);
2071 STREAM_GET(&gate
.ipv6
, s
, 16);
2074 zlog_warn("%s: Specified AF %d is not supported for this call",
2075 __PRETTY_FUNCTION__
, prefix
.family
);
2078 STREAM_GETL(s
, ifindex
);
2079 STREAM_GETC(s
, distance
);
2080 STREAM_GETL(s
, in_label
);
2081 STREAM_GETL(s
, out_label
);
2083 switch (prefix
.family
) {
2086 gtype
= NEXTHOP_TYPE_IPV4_IFINDEX
;
2088 gtype
= NEXTHOP_TYPE_IPV4
;
2092 gtype
= NEXTHOP_TYPE_IPV6_IFINDEX
;
2094 gtype
= NEXTHOP_TYPE_IPV6
;
2103 if (command
== ZEBRA_MPLS_LABELS_ADD
) {
2104 mpls_lsp_install(zvrf
, type
, in_label
, out_label
, gtype
, &gate
,
2106 mpls_ftn_update(1, zvrf
, type
, &prefix
, gtype
, &gate
, ifindex
,
2107 distance
, out_label
);
2108 } else if (command
== ZEBRA_MPLS_LABELS_DELETE
) {
2109 mpls_lsp_uninstall(zvrf
, type
, in_label
, gtype
, &gate
, ifindex
);
2110 mpls_ftn_update(0, zvrf
, type
, &prefix
, gtype
, &gate
, ifindex
,
2111 distance
, out_label
);
2116 /* Send response to a label manager connect request to client */
2117 static int zsend_label_manager_connect_response(struct zserv
*client
,
2118 vrf_id_t vrf_id
, u_short result
)
2125 zclient_create_header(s
, ZEBRA_LABEL_MANAGER_CONNECT
, vrf_id
);
2128 stream_putc(s
, result
);
2130 /* Write packet size. */
2131 stream_putw_at(s
, 0, stream_get_endp(s
));
2133 return writen(client
->sock
, s
->data
, stream_get_endp(s
));
2136 static void zread_label_manager_connect(struct zserv
*client
, vrf_id_t vrf_id
)
2139 /* type of protocol (lib/zebra.h) */
2143 /* Get input stream. */
2147 STREAM_GETC(s
, proto
);
2148 STREAM_GETW(s
, instance
);
2150 /* accept only dynamic routing protocols */
2151 if ((proto
>= ZEBRA_ROUTE_MAX
) || (proto
<= ZEBRA_ROUTE_STATIC
)) {
2152 zlog_err("client %d has wrong protocol %s", client
->sock
,
2153 zebra_route_string(proto
));
2154 zsend_label_manager_connect_response(client
, vrf_id
, 1);
2157 zlog_notice("client %d with instance %u connected as %s", client
->sock
,
2158 instance
, zebra_route_string(proto
));
2159 client
->proto
= proto
;
2160 client
->instance
= instance
;
2163 Release previous labels of same protocol and instance.
2164 This is done in case it restarted from an unexpected shutdown.
2166 release_daemon_chunks(proto
, instance
);
2169 " Label Manager client connected: sock %d, proto %s, instance %u",
2170 client
->sock
, zebra_route_string(proto
), instance
);
2171 /* send response back */
2172 zsend_label_manager_connect_response(client
, vrf_id
, 0);
2177 /* Send response to a get label chunk request to client */
2178 static int zsend_assign_label_chunk_response(struct zserv
*client
,
2180 struct label_manager_chunk
*lmc
)
2187 zclient_create_header(s
, ZEBRA_GET_LABEL_CHUNK
, vrf_id
);
2191 stream_putc(s
, lmc
->keep
);
2192 /* start and end labels */
2193 stream_putl(s
, lmc
->start
);
2194 stream_putl(s
, lmc
->end
);
2197 /* Write packet size. */
2198 stream_putw_at(s
, 0, stream_get_endp(s
));
2200 return writen(client
->sock
, s
->data
, stream_get_endp(s
));
2203 static void zread_get_label_chunk(struct zserv
*client
, vrf_id_t vrf_id
)
2208 struct label_manager_chunk
*lmc
;
2210 /* Get input stream. */
2214 STREAM_GETC(s
, keep
);
2215 STREAM_GETL(s
, size
);
2217 lmc
= assign_label_chunk(client
->proto
, client
->instance
, keep
, size
);
2219 zlog_err("%s: Unable to assign Label Chunk of size %u",
2222 zlog_debug("Assigned Label Chunk %u - %u to %u", lmc
->start
,
2224 /* send response back */
2225 zsend_assign_label_chunk_response(client
, vrf_id
, lmc
);
2231 static void zread_release_label_chunk(struct zserv
*client
)
2234 uint32_t start
, end
;
2236 /* Get input stream. */
2240 STREAM_GETL(s
, start
);
2241 STREAM_GETL(s
, end
);
2243 release_label_chunk(client
->proto
, client
->instance
, start
, end
);
2248 static void zread_label_manager_request(int cmd
, struct zserv
*client
,
2249 struct zebra_vrf
*zvrf
)
2251 /* to avoid sending other messages like ZERBA_INTERFACE_UP */
2252 if (cmd
== ZEBRA_LABEL_MANAGER_CONNECT
)
2253 client
->is_synchronous
= 1;
2255 /* external label manager */
2257 zread_relay_label_manager_request(cmd
, client
, zvrf_id(zvrf
));
2258 /* this is a label manager */
2260 if (cmd
== ZEBRA_LABEL_MANAGER_CONNECT
)
2261 zread_label_manager_connect(client
, zvrf_id(zvrf
));
2263 /* Sanity: don't allow 'unidentified' requests */
2264 if (!client
->proto
) {
2266 "Got label request from an unidentified client");
2269 if (cmd
== ZEBRA_GET_LABEL_CHUNK
)
2270 zread_get_label_chunk(client
, zvrf_id(zvrf
));
2271 else if (cmd
== ZEBRA_RELEASE_LABEL_CHUNK
)
2272 zread_release_label_chunk(client
);
2277 static int zread_pseudowire(int command
, struct zserv
*client
, u_short length
,
2278 struct zebra_vrf
*zvrf
)
2281 char ifname
[IF_NAMESIZE
];
2285 union g_addr nexthop
;
2286 uint32_t local_label
;
2287 uint32_t remote_label
;
2289 union pw_protocol_fields data
;
2291 struct zebra_pw
*pw
;
2293 /* Get input stream. */
2297 STREAM_GET(ifname
, s
, IF_NAMESIZE
);
2298 STREAM_GETL(s
, ifindex
);
2299 STREAM_GETL(s
, type
);
2303 STREAM_GET(&nexthop
.ipv4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
2306 STREAM_GET(&nexthop
.ipv6
, s
, 16);
2311 STREAM_GETL(s
, local_label
);
2312 STREAM_GETL(s
, remote_label
);
2313 STREAM_GETC(s
, flags
);
2314 STREAM_GET(&data
, s
, sizeof(data
));
2315 protocol
= client
->proto
;
2317 pw
= zebra_pw_find(zvrf
, ifname
);
2321 zlog_warn("%s: pseudowire %s already exists [%s]",
2323 zserv_command_string(command
));
2327 zebra_pw_add(zvrf
, ifname
, protocol
, client
);
2329 case ZEBRA_PW_DELETE
:
2331 zlog_warn("%s: pseudowire %s not found [%s]", __func__
,
2332 ifname
, zserv_command_string(command
));
2336 zebra_pw_del(zvrf
, pw
);
2339 case ZEBRA_PW_UNSET
:
2341 zlog_warn("%s: pseudowire %s not found [%s]", __func__
,
2342 ifname
, zserv_command_string(command
));
2350 case ZEBRA_PW_UNSET
:
2355 zebra_pw_change(pw
, ifindex
, type
, af
, &nexthop
, local_label
,
2356 remote_label
, flags
, &data
);
2364 /* Cleanup registered nexthops (across VRFs) upon client disconnect. */
2365 static void zebra_client_close_cleanup_rnh(struct zserv
*client
)
2368 struct zebra_vrf
*zvrf
;
2370 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
2371 if ((zvrf
= vrf
->info
) != NULL
) {
2372 zebra_cleanup_rnh_client(zvrf_id(zvrf
), AF_INET
, client
,
2374 zebra_cleanup_rnh_client(zvrf_id(zvrf
), AF_INET6
,
2375 client
, RNH_NEXTHOP_TYPE
);
2376 zebra_cleanup_rnh_client(zvrf_id(zvrf
), AF_INET
, client
,
2377 RNH_IMPORT_CHECK_TYPE
);
2378 zebra_cleanup_rnh_client(zvrf_id(zvrf
), AF_INET6
,
2379 client
, RNH_IMPORT_CHECK_TYPE
);
2380 if (client
->proto
== ZEBRA_ROUTE_LDP
) {
2381 hash_iterate(zvrf
->lsp_table
,
2382 mpls_ldp_lsp_uninstall_all
,
2384 mpls_ldp_ftn_uninstall_all(zvrf
, AFI_IP
);
2385 mpls_ldp_ftn_uninstall_all(zvrf
, AFI_IP6
);
2391 /* free zebra client information. */
2392 static void zebra_client_free(struct zserv
*client
)
2394 /* Send client de-registration to BFD */
2395 zebra_ptm_bfd_client_deregister(client
->proto
);
2397 /* Cleanup any registered nexthops - across all VRFs. */
2398 zebra_client_close_cleanup_rnh(client
);
2400 /* Release Label Manager chunks */
2401 release_daemon_chunks(client
->proto
, client
->instance
);
2403 /* Cleanup any FECs registered by this client. */
2404 zebra_mpls_cleanup_fecs_for_client(vrf_info_lookup(VRF_DEFAULT
),
2407 /* Remove pseudowires associated with this client */
2408 zebra_pw_client_close(client
);
2410 /* Close file descriptor. */
2412 unsigned long nroutes
;
2414 close(client
->sock
);
2415 nroutes
= rib_score_proto(client
->proto
, client
->instance
);
2417 "client %d disconnected. %lu %s routes removed from the rib",
2418 client
->sock
, nroutes
,
2419 zebra_route_string(client
->proto
));
2423 /* Free stream buffers. */
2425 stream_free(client
->ibuf
);
2427 stream_free(client
->obuf
);
2429 buffer_free(client
->wb
);
2431 /* Release threads. */
2433 thread_cancel(client
->t_read
);
2434 if (client
->t_write
)
2435 thread_cancel(client
->t_write
);
2436 if (client
->t_suicide
)
2437 thread_cancel(client
->t_suicide
);
2440 for (afi_t afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
2441 for (int i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
2442 vrf_bitmap_free(client
->redist
[afi
][i
]);
2444 vrf_bitmap_free(client
->redist_default
);
2445 vrf_bitmap_free(client
->ifinfo
);
2446 vrf_bitmap_free(client
->ridinfo
);
2448 XFREE(MTYPE_TMP
, client
);
2451 static void zebra_client_close(struct zserv
*client
)
2453 listnode_delete(zebrad
.client_list
, client
);
2454 zebra_client_free(client
);
2457 /* Make new client. */
2458 static void zebra_client_create(int sock
)
2460 struct zserv
*client
;
2464 client
= XCALLOC(MTYPE_TMP
, sizeof(struct zserv
));
2466 /* Make client input/output buffer. */
2467 client
->sock
= sock
;
2468 client
->ibuf
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
2469 client
->obuf
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
2470 client
->wb
= buffer_new(0);
2472 /* Set table number. */
2473 client
->rtm_table
= zebrad
.rtm_table_default
;
2475 client
->connect_time
= monotime(NULL
);
2476 /* Initialize flags */
2477 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
2478 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
2479 client
->redist
[afi
][i
] = vrf_bitmap_init();
2480 client
->redist_default
= vrf_bitmap_init();
2481 client
->ifinfo
= vrf_bitmap_init();
2482 client
->ridinfo
= vrf_bitmap_init();
2484 /* by default, it's not a synchronous client */
2485 client
->is_synchronous
= 0;
2487 /* Add this client to linked list. */
2488 listnode_add(zebrad
.client_list
, client
);
2490 /* Make new read thread. */
2491 zebra_event(ZEBRA_READ
, sock
, client
);
2493 zebra_vrf_update_all(client
);
2496 static int zread_interface_set_master(struct zserv
*client
, u_short length
)
2498 struct interface
*master
;
2499 struct interface
*slave
;
2500 struct stream
*s
= client
->ibuf
;
2504 STREAM_GETL(s
, vrf_id
);
2505 STREAM_GETL(s
, ifindex
);
2506 master
= if_lookup_by_index(ifindex
, vrf_id
);
2508 STREAM_GETL(s
, vrf_id
);
2509 STREAM_GETL(s
, ifindex
);
2510 slave
= if_lookup_by_index(ifindex
, vrf_id
);
2512 if (!master
|| !slave
)
2515 kernel_interface_set_master(master
, slave
);
2522 static void zread_vrf_label(struct zserv
*client
, struct zebra_vrf
*zvrf
)
2524 struct interface
*ifp
;
2525 mpls_label_t nlabel
;
2528 struct zebra_vrf
*def_zvrf
;
2529 enum lsp_types_t ltype
;
2532 STREAM_GETL(s
, nlabel
);
2533 STREAM_GETC(s
, afi
);
2534 if (nlabel
== zvrf
->label
[afi
]) {
2536 * Nothing to do here move along
2541 STREAM_GETC(s
, ltype
);
2543 if (zvrf
->vrf
->vrf_id
!= VRF_DEFAULT
)
2544 ifp
= if_lookup_by_name(zvrf
->vrf
->name
, zvrf
->vrf
->vrf_id
);
2546 ifp
= if_lookup_by_name("lo", VRF_DEFAULT
);
2549 zlog_debug("Unable to find specified Interface for %s",
2554 def_zvrf
= zebra_vrf_lookup_by_id(VRF_DEFAULT
);
2556 if (zvrf
->label
[afi
] != MPLS_LABEL_NONE
) {
2560 really_remove
= true;
2561 for (scrubber
= AFI_IP
; scrubber
< AFI_MAX
; scrubber
++) {
2562 if (scrubber
== afi
)
2565 if (zvrf
->label
[scrubber
] == MPLS_LABEL_NONE
)
2568 if (zvrf
->label
[afi
] == zvrf
->label
[scrubber
]) {
2569 really_remove
= false;
2575 mpls_lsp_uninstall(def_zvrf
, ltype
, zvrf
->label
[afi
],
2576 NEXTHOP_TYPE_IFINDEX
, NULL
,
2580 if (nlabel
!= MPLS_LABEL_NONE
)
2581 mpls_lsp_install(def_zvrf
, ltype
, nlabel
,
2582 MPLS_LABEL_IMPLICIT_NULL
, NEXTHOP_TYPE_IFINDEX
,
2583 NULL
, ifp
->ifindex
);
2585 zvrf
->label
[afi
] = nlabel
;
2590 static inline void zserv_handle_commands(struct zserv
*client
, uint16_t command
,
2592 struct zebra_vrf
*zvrf
)
2595 case ZEBRA_ROUTER_ID_ADD
:
2596 zread_router_id_add(client
, length
, zvrf
);
2598 case ZEBRA_ROUTER_ID_DELETE
:
2599 zread_router_id_delete(client
, length
, zvrf
);
2601 case ZEBRA_INTERFACE_ADD
:
2602 zread_interface_add(client
, length
, zvrf
);
2604 case ZEBRA_INTERFACE_DELETE
:
2605 zread_interface_delete(client
, length
, zvrf
);
2607 case ZEBRA_ROUTE_ADD
:
2608 zread_route_add(client
, length
, zvrf
);
2610 case ZEBRA_ROUTE_DELETE
:
2611 zread_route_del(client
, length
, zvrf
);
2613 case ZEBRA_IPV4_ROUTE_ADD
:
2614 zread_ipv4_add(client
, length
, zvrf
);
2616 case ZEBRA_IPV4_ROUTE_DELETE
:
2617 zread_ipv4_delete(client
, length
, zvrf
);
2619 case ZEBRA_IPV4_ROUTE_IPV6_NEXTHOP_ADD
:
2620 zread_ipv4_route_ipv6_nexthop_add(client
, length
, zvrf
);
2622 case ZEBRA_IPV6_ROUTE_ADD
:
2623 zread_ipv6_add(client
, length
, zvrf
);
2625 case ZEBRA_IPV6_ROUTE_DELETE
:
2626 zread_ipv6_delete(client
, length
, zvrf
);
2628 case ZEBRA_REDISTRIBUTE_ADD
:
2629 zebra_redistribute_add(command
, client
, length
, zvrf
);
2631 case ZEBRA_REDISTRIBUTE_DELETE
:
2632 zebra_redistribute_delete(command
, client
, length
, zvrf
);
2634 case ZEBRA_REDISTRIBUTE_DEFAULT_ADD
:
2635 zebra_redistribute_default_add(command
, client
, length
, zvrf
);
2637 case ZEBRA_REDISTRIBUTE_DEFAULT_DELETE
:
2638 zebra_redistribute_default_delete(command
, client
, length
,
2641 case ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB
:
2642 zread_ipv4_nexthop_lookup_mrib(client
, length
, zvrf
);
2645 zread_hello(client
);
2647 case ZEBRA_NEXTHOP_REGISTER
:
2648 zserv_rnh_register(client
, length
, RNH_NEXTHOP_TYPE
, zvrf
);
2650 case ZEBRA_NEXTHOP_UNREGISTER
:
2651 zserv_rnh_unregister(client
, length
, RNH_NEXTHOP_TYPE
, zvrf
);
2653 case ZEBRA_IMPORT_ROUTE_REGISTER
:
2654 zserv_rnh_register(client
, length
, RNH_IMPORT_CHECK_TYPE
, zvrf
);
2656 case ZEBRA_IMPORT_ROUTE_UNREGISTER
:
2657 zserv_rnh_unregister(client
, length
, RNH_IMPORT_CHECK_TYPE
,
2660 case ZEBRA_BFD_DEST_UPDATE
:
2661 case ZEBRA_BFD_DEST_REGISTER
:
2662 zebra_ptm_bfd_dst_register(client
, length
, command
, zvrf
);
2664 case ZEBRA_BFD_DEST_DEREGISTER
:
2665 zebra_ptm_bfd_dst_deregister(client
, length
, zvrf
);
2667 case ZEBRA_VRF_UNREGISTER
:
2668 zread_vrf_unregister(client
, length
, zvrf
);
2670 case ZEBRA_VRF_LABEL
:
2671 zread_vrf_label(client
, zvrf
);
2673 case ZEBRA_BFD_CLIENT_REGISTER
:
2674 zebra_ptm_bfd_client_register(client
, length
);
2676 case ZEBRA_INTERFACE_ENABLE_RADV
:
2677 #if defined(HAVE_RTADV)
2678 zebra_interface_radv_set(client
, length
, zvrf
, 1);
2681 case ZEBRA_INTERFACE_DISABLE_RADV
:
2682 #if defined(HAVE_RTADV)
2683 zebra_interface_radv_set(client
, length
, zvrf
, 0);
2686 case ZEBRA_MPLS_LABELS_ADD
:
2687 case ZEBRA_MPLS_LABELS_DELETE
:
2688 zread_mpls_labels(command
, client
, length
, zvrf
);
2690 case ZEBRA_IPMR_ROUTE_STATS
:
2691 zebra_ipmr_route_stats(client
, length
, zvrf
);
2693 case ZEBRA_LABEL_MANAGER_CONNECT
:
2694 case ZEBRA_GET_LABEL_CHUNK
:
2695 case ZEBRA_RELEASE_LABEL_CHUNK
:
2696 zread_label_manager_request(command
, client
, zvrf
);
2698 case ZEBRA_FEC_REGISTER
:
2699 zserv_fec_register(client
, length
);
2701 case ZEBRA_FEC_UNREGISTER
:
2702 zserv_fec_unregister(client
, length
);
2704 case ZEBRA_ADVERTISE_DEFAULT_GW
:
2705 zebra_vxlan_advertise_gw_macip(client
, length
, zvrf
);
2707 case ZEBRA_ADVERTISE_SUBNET
:
2708 zebra_vxlan_advertise_subnet(client
, length
, zvrf
);
2710 case ZEBRA_ADVERTISE_ALL_VNI
:
2711 zebra_vxlan_advertise_all_vni(client
, length
, zvrf
);
2713 case ZEBRA_REMOTE_VTEP_ADD
:
2714 zebra_vxlan_remote_vtep_add(client
, length
, zvrf
);
2716 case ZEBRA_REMOTE_VTEP_DEL
:
2717 zebra_vxlan_remote_vtep_del(client
, length
, zvrf
);
2719 case ZEBRA_REMOTE_MACIP_ADD
:
2720 zebra_vxlan_remote_macip_add(client
, length
, zvrf
);
2722 case ZEBRA_REMOTE_MACIP_DEL
:
2723 zebra_vxlan_remote_macip_del(client
, length
, zvrf
);
2725 case ZEBRA_INTERFACE_SET_MASTER
:
2726 zread_interface_set_master(client
, length
);
2729 case ZEBRA_PW_DELETE
:
2731 case ZEBRA_PW_UNSET
:
2732 zread_pseudowire(command
, client
, length
, zvrf
);
2735 zlog_info("Zebra received unknown command %d", command
);
2740 #if defined(HANDLE_ZAPI_FUZZING)
2741 static void zserv_write_incoming(struct stream
*orig
, uint16_t command
)
2743 char fname
[MAXPATHLEN
];
2744 struct stream
*copy
;
2747 copy
= stream_dup(orig
);
2748 stream_set_getp(copy
, 0);
2750 zserv_privs
.change(ZPRIVS_RAISE
);
2751 snprintf(fname
, MAXPATHLEN
, "%s/%u", DAEMON_VTY_DIR
, command
);
2752 fd
= open(fname
, O_CREAT
| O_WRONLY
| O_EXCL
, 0644);
2753 stream_flush(copy
, fd
);
2755 zserv_privs
.change(ZPRIVS_LOWER
);
2760 /* Handler of zebra service request. */
2761 static int zebra_client_read(struct thread
*thread
)
2764 struct zserv
*client
;
2766 uint16_t length
, command
;
2767 uint8_t marker
, version
;
2769 struct zebra_vrf
*zvrf
;
2770 #if defined(HANDLE_ZAPI_FUZZING)
2773 int packets
= zebrad
.packets_to_process
;
2776 /* Get thread data. Reset reading thread because I'm running. */
2777 sock
= THREAD_FD(thread
);
2778 client
= THREAD_ARG(thread
);
2779 client
->t_read
= NULL
;
2781 if (client
->t_suicide
) {
2782 zebra_client_close(client
);
2787 /* Read length and command (if we don't have it already). */
2788 if ((already
= stream_get_endp(client
->ibuf
))
2789 < ZEBRA_HEADER_SIZE
) {
2791 if (((nbyte
= stream_read_try(client
->ibuf
, sock
,
2796 if (IS_ZEBRA_DEBUG_EVENT
)
2798 "connection closed socket [%d]",
2800 zebra_client_close(client
);
2803 if (nbyte
!= (ssize_t
)(ZEBRA_HEADER_SIZE
- already
)) {
2804 /* Try again later. */
2805 zebra_event(ZEBRA_READ
, sock
, client
);
2808 already
= ZEBRA_HEADER_SIZE
;
2811 /* Reset to read from the beginning of the incoming packet. */
2812 stream_set_getp(client
->ibuf
, 0);
2814 /* Fetch header values */
2815 STREAM_GETW(client
->ibuf
, length
);
2816 STREAM_GETC(client
->ibuf
, marker
);
2817 STREAM_GETC(client
->ibuf
, version
);
2818 STREAM_GETL(client
->ibuf
, vrf_id
);
2819 STREAM_GETW(client
->ibuf
, command
);
2821 if (marker
!= ZEBRA_HEADER_MARKER
|| version
!= ZSERV_VERSION
) {
2823 "%s: socket %d version mismatch, marker %d, version %d",
2824 __func__
, sock
, marker
, version
);
2825 zebra_client_close(client
);
2828 if (length
< ZEBRA_HEADER_SIZE
) {
2830 "%s: socket %d message length %u is less than header size %d",
2831 __func__
, sock
, length
, ZEBRA_HEADER_SIZE
);
2832 zebra_client_close(client
);
2835 if (length
> STREAM_SIZE(client
->ibuf
)) {
2837 "%s: socket %d message length %u exceeds buffer size %lu",
2838 __func__
, sock
, length
,
2839 (u_long
)STREAM_SIZE(client
->ibuf
));
2840 zebra_client_close(client
);
2844 /* Read rest of data. */
2845 if (already
< length
) {
2847 if (((nbyte
= stream_read_try(client
->ibuf
, sock
,
2851 if (IS_ZEBRA_DEBUG_EVENT
)
2853 "connection closed [%d] when reading zebra data",
2855 zebra_client_close(client
);
2858 if (nbyte
!= (ssize_t
)(length
- already
)) {
2859 /* Try again later. */
2860 zebra_event(ZEBRA_READ
, sock
, client
);
2865 #if defined(HANDLE_ZAPI_FUZZING)
2866 zserv_write_incoming(client
->ibuf
, command
);
2868 length
-= ZEBRA_HEADER_SIZE
;
2870 /* Debug packet information. */
2871 if (IS_ZEBRA_DEBUG_EVENT
)
2872 zlog_debug("zebra message comes from socket [%d]",
2875 if (IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
)
2876 zlog_debug("zebra message received [%s] %d in VRF %u",
2877 zserv_command_string(command
), length
,
2880 client
->last_read_time
= monotime(NULL
);
2881 client
->last_read_cmd
= command
;
2883 zvrf
= zebra_vrf_lookup_by_id(vrf_id
);
2885 if (IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
)
2886 zlog_debug("zebra received unknown VRF[%u]",
2888 goto zclient_read_out
;
2891 zserv_handle_commands(client
, command
, length
, zvrf
);
2893 if (client
->t_suicide
) {
2894 /* No need to wait for thread callback, just kill
2897 zebra_client_close(client
);
2901 stream_reset(client
->ibuf
);
2906 stream_reset(client
->ibuf
);
2907 zebra_event(ZEBRA_READ
, sock
, client
);
2912 /* Accept code of zebra server socket. */
2913 static int zebra_accept(struct thread
*thread
)
2917 struct sockaddr_in client
;
2920 accept_sock
= THREAD_FD(thread
);
2922 /* Reregister myself. */
2923 zebra_event(ZEBRA_SERV
, accept_sock
, NULL
);
2925 len
= sizeof(struct sockaddr_in
);
2926 client_sock
= accept(accept_sock
, (struct sockaddr
*)&client
, &len
);
2928 if (client_sock
< 0) {
2929 zlog_warn("Can't accept zebra socket: %s",
2930 safe_strerror(errno
));
2934 /* Make client socket non-blocking. */
2935 set_nonblocking(client_sock
);
2937 /* Create new zebra client. */
2938 zebra_client_create(client_sock
);
2943 /* Make zebra server socket, wiping any existing one (see bug #403). */
2944 void zebra_zserv_socket_init(char *path
)
2949 struct sockaddr_storage sa
;
2952 if (!frr_zclient_addr(&sa
, &sa_len
, path
))
2953 /* should be caught in zebra main() */
2957 old_mask
= umask(0077);
2959 /* Make UNIX domain socket. */
2960 sock
= socket(sa
.ss_family
, SOCK_STREAM
, 0);
2962 zlog_warn("Can't create zserv socket: %s",
2963 safe_strerror(errno
));
2965 "zebra can't provide full functionality due to above error");
2969 if (sa
.ss_family
!= AF_UNIX
) {
2970 sockopt_reuseaddr(sock
);
2971 sockopt_reuseport(sock
);
2973 struct sockaddr_un
*suna
= (struct sockaddr_un
*)&sa
;
2974 if (suna
->sun_path
[0])
2975 unlink(suna
->sun_path
);
2978 zserv_privs
.change(ZPRIVS_RAISE
);
2979 setsockopt_so_recvbuf(sock
, 1048576);
2980 setsockopt_so_sendbuf(sock
, 1048576);
2981 zserv_privs
.change(ZPRIVS_LOWER
);
2983 if (sa
.ss_family
!= AF_UNIX
&& zserv_privs
.change(ZPRIVS_RAISE
))
2984 zlog_err("Can't raise privileges");
2986 ret
= bind(sock
, (struct sockaddr
*)&sa
, sa_len
);
2988 zlog_warn("Can't bind zserv socket on %s: %s", path
,
2989 safe_strerror(errno
));
2991 "zebra can't provide full functionality due to above error");
2995 if (sa
.ss_family
!= AF_UNIX
&& zserv_privs
.change(ZPRIVS_LOWER
))
2996 zlog_err("Can't lower privileges");
2998 ret
= listen(sock
, 5);
3000 zlog_warn("Can't listen to zserv socket %s: %s", path
,
3001 safe_strerror(errno
));
3003 "zebra can't provide full functionality due to above error");
3010 zebra_event(ZEBRA_SERV
, sock
, NULL
);
3014 static void zebra_event(enum event event
, int sock
, struct zserv
*client
)
3018 thread_add_read(zebrad
.master
, zebra_accept
, client
, sock
,
3022 client
->t_read
= NULL
;
3023 thread_add_read(zebrad
.master
, zebra_client_read
, client
, sock
,
3032 #define ZEBRA_TIME_BUF 32
3033 static char *zserv_time_buf(time_t *time1
, char *buf
, int buflen
)
3038 assert(buf
!= NULL
);
3039 assert(buflen
>= ZEBRA_TIME_BUF
);
3040 assert(time1
!= NULL
);
3043 snprintf(buf
, buflen
, "never ");
3047 now
= monotime(NULL
);
3051 if (now
< ONE_DAY_SECOND
)
3052 snprintf(buf
, buflen
, "%02d:%02d:%02d", tm
->tm_hour
, tm
->tm_min
,
3054 else if (now
< ONE_WEEK_SECOND
)
3055 snprintf(buf
, buflen
, "%dd%02dh%02dm", tm
->tm_yday
, tm
->tm_hour
,
3058 snprintf(buf
, buflen
, "%02dw%dd%02dh", tm
->tm_yday
/ 7,
3059 tm
->tm_yday
- ((tm
->tm_yday
/ 7) * 7), tm
->tm_hour
);
3063 static void zebra_show_client_detail(struct vty
*vty
, struct zserv
*client
)
3065 char cbuf
[ZEBRA_TIME_BUF
], rbuf
[ZEBRA_TIME_BUF
];
3066 char wbuf
[ZEBRA_TIME_BUF
], nhbuf
[ZEBRA_TIME_BUF
], mbuf
[ZEBRA_TIME_BUF
];
3068 vty_out(vty
, "Client: %s", zebra_route_string(client
->proto
));
3069 if (client
->instance
)
3070 vty_out(vty
, " Instance: %d", client
->instance
);
3073 vty_out(vty
, "------------------------ \n");
3074 vty_out(vty
, "FD: %d \n", client
->sock
);
3075 vty_out(vty
, "Route Table ID: %d \n", client
->rtm_table
);
3077 vty_out(vty
, "Connect Time: %s \n",
3078 zserv_time_buf(&client
->connect_time
, cbuf
, ZEBRA_TIME_BUF
));
3079 if (client
->nh_reg_time
) {
3080 vty_out(vty
, "Nexthop Registry Time: %s \n",
3081 zserv_time_buf(&client
->nh_reg_time
, nhbuf
,
3083 if (client
->nh_last_upd_time
)
3084 vty_out(vty
, "Nexthop Last Update Time: %s \n",
3085 zserv_time_buf(&client
->nh_last_upd_time
, mbuf
,
3088 vty_out(vty
, "No Nexthop Update sent\n");
3090 vty_out(vty
, "Not registered for Nexthop Updates\n");
3092 vty_out(vty
, "Last Msg Rx Time: %s \n",
3093 zserv_time_buf(&client
->last_read_time
, rbuf
, ZEBRA_TIME_BUF
));
3094 vty_out(vty
, "Last Msg Tx Time: %s \n",
3095 zserv_time_buf(&client
->last_write_time
, wbuf
, ZEBRA_TIME_BUF
));
3096 if (client
->last_read_time
)
3097 vty_out(vty
, "Last Rcvd Cmd: %s \n",
3098 zserv_command_string(client
->last_read_cmd
));
3099 if (client
->last_write_time
)
3100 vty_out(vty
, "Last Sent Cmd: %s \n",
3101 zserv_command_string(client
->last_write_cmd
));
3104 vty_out(vty
, "Type Add Update Del \n");
3105 vty_out(vty
, "================================================== \n");
3106 vty_out(vty
, "IPv4 %-12d%-12d%-12d\n", client
->v4_route_add_cnt
,
3107 client
->v4_route_upd8_cnt
, client
->v4_route_del_cnt
);
3108 vty_out(vty
, "IPv6 %-12d%-12d%-12d\n", client
->v6_route_add_cnt
,
3109 client
->v6_route_upd8_cnt
, client
->v6_route_del_cnt
);
3110 vty_out(vty
, "Redist:v4 %-12d%-12d%-12d\n", client
->redist_v4_add_cnt
,
3111 0, client
->redist_v4_del_cnt
);
3112 vty_out(vty
, "Redist:v6 %-12d%-12d%-12d\n", client
->redist_v6_add_cnt
,
3113 0, client
->redist_v6_del_cnt
);
3114 vty_out(vty
, "Connected %-12d%-12d%-12d\n", client
->ifadd_cnt
, 0,
3116 vty_out(vty
, "BFD peer %-12d%-12d%-12d\n", client
->bfd_peer_add_cnt
,
3117 client
->bfd_peer_upd8_cnt
, client
->bfd_peer_del_cnt
);
3118 vty_out(vty
, "Interface Up Notifications: %d\n", client
->ifup_cnt
);
3119 vty_out(vty
, "Interface Down Notifications: %d\n", client
->ifdown_cnt
);
3120 vty_out(vty
, "VNI add notifications: %d\n", client
->vniadd_cnt
);
3121 vty_out(vty
, "VNI delete notifications: %d\n", client
->vnidel_cnt
);
3122 vty_out(vty
, "L3-VNI add notifications: %d\n", client
->l3vniadd_cnt
);
3123 vty_out(vty
, "L3-VNI delete notifications: %d\n", client
->l3vnidel_cnt
);
3124 vty_out(vty
, "MAC-IP add notifications: %d\n", client
->macipadd_cnt
);
3125 vty_out(vty
, "MAC-IP delete notifications: %d\n", client
->macipdel_cnt
);
3131 static void zebra_show_client_brief(struct vty
*vty
, struct zserv
*client
)
3133 char cbuf
[ZEBRA_TIME_BUF
], rbuf
[ZEBRA_TIME_BUF
];
3134 char wbuf
[ZEBRA_TIME_BUF
];
3136 vty_out(vty
, "%-8s%12s %12s%12s%8d/%-8d%8d/%-8d\n",
3137 zebra_route_string(client
->proto
),
3138 zserv_time_buf(&client
->connect_time
, cbuf
, ZEBRA_TIME_BUF
),
3139 zserv_time_buf(&client
->last_read_time
, rbuf
, ZEBRA_TIME_BUF
),
3140 zserv_time_buf(&client
->last_write_time
, wbuf
, ZEBRA_TIME_BUF
),
3141 client
->v4_route_add_cnt
+ client
->v4_route_upd8_cnt
,
3142 client
->v4_route_del_cnt
,
3143 client
->v6_route_add_cnt
+ client
->v6_route_upd8_cnt
,
3144 client
->v6_route_del_cnt
);
3147 struct zserv
*zebra_find_client(u_char proto
, u_short instance
)
3149 struct listnode
*node
, *nnode
;
3150 struct zserv
*client
;
3152 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, node
, nnode
, client
)) {
3153 if (client
->proto
== proto
&& client
->instance
== instance
)
3160 /* This command is for debugging purpose. */
3161 DEFUN (show_zebra_client
,
3162 show_zebra_client_cmd
,
3163 "show zebra client",
3166 "Client information\n")
3168 struct listnode
*node
;
3169 struct zserv
*client
;
3171 for (ALL_LIST_ELEMENTS_RO(zebrad
.client_list
, node
, client
))
3172 zebra_show_client_detail(vty
, client
);
3177 /* This command is for debugging purpose. */
3178 DEFUN (show_zebra_client_summary
,
3179 show_zebra_client_summary_cmd
,
3180 "show zebra client summary",
3183 "Client information brief\n"
3186 struct listnode
*node
;
3187 struct zserv
*client
;
3190 "Name Connect Time Last Read Last Write IPv4 Routes IPv6 Routes \n");
3192 "--------------------------------------------------------------------------------\n");
3194 for (ALL_LIST_ELEMENTS_RO(zebrad
.client_list
, node
, client
))
3195 zebra_show_client_brief(vty
, client
);
3197 vty_out(vty
, "Routes column shows (added+updated)/deleted\n");
3201 #if defined(HANDLE_ZAPI_FUZZING)
3202 void zserv_read_file(char *input
)
3205 struct zserv
*client
= NULL
;
3208 zebra_client_create(-1);
3209 client
= zebrad
.client_list
->head
->data
;
3212 fd
= open(input
, O_RDONLY
| O_NONBLOCK
);
3215 zebra_client_read(&t
);
3221 void zserv_init(void)
3223 /* Client list init. */
3224 zebrad
.client_list
= list_new();
3225 zebrad
.client_list
->del
= (void (*)(void *))zebra_client_free
;
3227 install_element(ENABLE_NODE
, &show_zebra_client_cmd
);
3228 install_element(ENABLE_NODE
, &show_zebra_client_summary_cmd
);