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
30 #include "zebra_memory.h"
34 #include "sockunion.h"
45 #include "zebra/zserv.h"
46 #include "zebra/zebra_ns.h"
47 #include "zebra/zebra_vrf.h"
48 #include "zebra/router-id.h"
49 #include "zebra/redistribute.h"
50 #include "zebra/debug.h"
51 #include "zebra/zebra_rnh.h"
52 #include "zebra/rt_netlink.h"
53 #include "zebra/interface.h"
54 #include "zebra/zebra_ptm.h"
55 #include "zebra/rtadv.h"
56 #include "zebra/zebra_mpls.h"
57 #include "zebra/zebra_mroute.h"
58 #include "zebra/label_manager.h"
59 #include "zebra/zebra_vxlan.h"
62 /* Event list of zebra. */
63 enum event
{ ZEBRA_SERV
, ZEBRA_READ
, ZEBRA_WRITE
};
65 static void zebra_event(enum event event
, int sock
, struct zserv
*client
);
67 extern struct zebra_privs_t zserv_privs
;
69 static void zebra_client_close(struct zserv
*client
);
71 static int zserv_delayed_close(struct thread
*thread
)
73 struct zserv
*client
= THREAD_ARG(thread
);
75 client
->t_suicide
= NULL
;
76 zebra_client_close(client
);
80 static int zserv_flush_data(struct thread
*thread
)
82 struct zserv
*client
= THREAD_ARG(thread
);
84 client
->t_write
= NULL
;
85 if (client
->t_suicide
) {
86 zebra_client_close(client
);
89 switch (buffer_flush_available(client
->wb
, client
->sock
)) {
92 "%s: buffer_flush_available failed on zserv client fd %d, "
94 __func__
, client
->sock
);
95 zebra_client_close(client
);
99 client
->t_write
= NULL
;
100 thread_add_write(zebrad
.master
, zserv_flush_data
, client
,
101 client
->sock
, &client
->t_write
);
108 client
->last_write_time
= monotime(NULL
);
112 int zebra_server_send_message(struct zserv
*client
)
114 if (client
->t_suicide
)
117 if (client
->is_synchronous
)
120 stream_set_getp(client
->obuf
, 0);
121 client
->last_write_cmd
= stream_getw_from(client
->obuf
, 6);
122 switch (buffer_write(client
->wb
, client
->sock
,
123 STREAM_DATA(client
->obuf
),
124 stream_get_endp(client
->obuf
))) {
127 "%s: buffer_write failed to zserv client fd %d, closing",
128 __func__
, client
->sock
);
129 /* Schedule a delayed close since many of the functions that
131 one do not check the return code. They do not allow for the
132 possibility that an I/O error may have caused the client to
135 client
->t_suicide
= NULL
;
136 thread_add_event(zebrad
.master
, zserv_delayed_close
, client
, 0,
140 THREAD_OFF(client
->t_write
);
143 thread_add_write(zebrad
.master
, zserv_flush_data
, client
,
144 client
->sock
, &client
->t_write
);
148 client
->last_write_time
= monotime(NULL
);
152 static void zserv_encode_interface(struct stream
*s
, struct interface
*ifp
)
154 /* Interface information. */
155 stream_put(s
, ifp
->name
, INTERFACE_NAMSIZ
);
156 stream_putl(s
, ifp
->ifindex
);
157 stream_putc(s
, ifp
->status
);
158 stream_putq(s
, ifp
->flags
);
159 stream_putc(s
, ifp
->ptm_enable
);
160 stream_putc(s
, ifp
->ptm_status
);
161 stream_putl(s
, ifp
->metric
);
162 stream_putl(s
, ifp
->speed
);
163 stream_putl(s
, ifp
->mtu
);
164 stream_putl(s
, ifp
->mtu6
);
165 stream_putl(s
, ifp
->bandwidth
);
166 stream_putl(s
, ifp
->ll_type
);
167 stream_putl(s
, ifp
->hw_addr_len
);
168 if (ifp
->hw_addr_len
)
169 stream_put(s
, ifp
->hw_addr
, ifp
->hw_addr_len
);
171 /* Then, Traffic Engineering parameters if any */
172 if (HAS_LINK_PARAMS(ifp
) && IS_LINK_PARAMS_SET(ifp
->link_params
)) {
174 zebra_interface_link_params_write(s
, ifp
);
178 /* Write packet size. */
179 stream_putw_at(s
, 0, stream_get_endp(s
));
182 static void zserv_encode_vrf(struct stream
*s
, struct zebra_vrf
*zvrf
)
184 struct vrf_data data
;
186 data
.l
.table_id
= zvrf
->table_id
;
187 /* Pass the tableid */
188 stream_put(s
, &data
, sizeof(struct vrf_data
));
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 zclient_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 zclient_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 zclient_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 zclient_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 zclient_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 zclient_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 zclient_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 zclient_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_putl(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 zclient_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
);
585 int zsend_redistribute_route(int cmd
, struct zserv
*client
, struct prefix
*p
,
586 struct prefix
*src_p
, struct route_entry
*re
)
588 struct zapi_route api
;
589 struct zapi_nexthop
*api_nh
;
590 struct nexthop
*nexthop
;
593 memset(&api
, 0, sizeof(api
));
594 api
.vrf_id
= re
->vrf_id
;
595 api
.nh_vrf_id
= re
->nh_vrf_id
;
597 api
.instance
= re
->instance
;
598 api
.flags
= re
->flags
;
603 SET_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
);
604 memcpy(&api
.src_prefix
, src_p
, sizeof(api
.src_prefix
));
608 if (re
->nexthop_active_num
) {
609 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
610 api
.nexthop_num
= re
->nexthop_active_num
;
612 for (nexthop
= re
->nexthop
; nexthop
; nexthop
= nexthop
->next
) {
613 if (!CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
616 api_nh
= &api
.nexthops
[count
];
617 api_nh
->type
= nexthop
->type
;
618 switch (nexthop
->type
) {
619 case NEXTHOP_TYPE_BLACKHOLE
:
620 api_nh
->bh_type
= nexthop
->bh_type
;
622 case NEXTHOP_TYPE_IPV4
:
623 api_nh
->gate
.ipv4
= nexthop
->gate
.ipv4
;
625 case NEXTHOP_TYPE_IPV4_IFINDEX
:
626 api_nh
->gate
.ipv4
= nexthop
->gate
.ipv4
;
627 api_nh
->ifindex
= nexthop
->ifindex
;
629 case NEXTHOP_TYPE_IFINDEX
:
630 api_nh
->ifindex
= nexthop
->ifindex
;
632 case NEXTHOP_TYPE_IPV6
:
633 api_nh
->gate
.ipv6
= nexthop
->gate
.ipv6
;
635 case NEXTHOP_TYPE_IPV6_IFINDEX
:
636 api_nh
->gate
.ipv6
= nexthop
->gate
.ipv6
;
637 api_nh
->ifindex
= nexthop
->ifindex
;
643 SET_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
);
644 api
.distance
= re
->distance
;
645 SET_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
);
646 api
.metric
= re
->metric
;
648 SET_FLAG(api
.message
, ZAPI_MESSAGE_TAG
);
651 SET_FLAG(api
.message
, ZAPI_MESSAGE_MTU
);
654 /* Encode route and send. */
655 if (zapi_route_encode(cmd
, client
->obuf
, &api
) < 0)
657 return zebra_server_send_message(client
);
660 static int zsend_write_nexthop(struct stream
*s
, struct nexthop
*nexthop
)
662 stream_putc(s
, nexthop
->type
);
663 switch (nexthop
->type
) {
664 case NEXTHOP_TYPE_IPV4
:
665 case NEXTHOP_TYPE_IPV4_IFINDEX
:
666 stream_put_in_addr(s
, &nexthop
->gate
.ipv4
);
667 stream_putl(s
, nexthop
->ifindex
);
669 case NEXTHOP_TYPE_IPV6
:
670 stream_put(s
, &nexthop
->gate
.ipv6
, 16);
672 case NEXTHOP_TYPE_IPV6_IFINDEX
:
673 stream_put(s
, &nexthop
->gate
.ipv6
, 16);
674 stream_putl(s
, nexthop
->ifindex
);
676 case NEXTHOP_TYPE_IFINDEX
:
677 stream_putl(s
, nexthop
->ifindex
);
686 /* Nexthop register */
687 static int zserv_rnh_register(struct zserv
*client
, u_short length
,
688 rnh_type_t type
, struct zebra_vrf
*zvrf
)
696 if (IS_ZEBRA_DEBUG_NHT
)
698 "rnh_register msg from client %s: length=%d, type=%s\n",
699 zebra_route_string(client
->proto
), length
,
700 (type
== RNH_NEXTHOP_TYPE
) ? "nexthop" : "route");
704 client
->nh_reg_time
= monotime(NULL
);
707 STREAM_GETC(s
, flags
);
708 STREAM_GETW(s
, p
.family
);
709 STREAM_GETC(s
, p
.prefixlen
);
711 if (p
.family
== AF_INET
) {
712 if (p
.prefixlen
> IPV4_MAX_BITLEN
) {
713 zlog_warn("%s: Specified prefix length %d is too large for a v4 address",
714 __PRETTY_FUNCTION__
, p
.prefixlen
);
717 STREAM_GET(&p
.u
.prefix4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
718 l
+= IPV4_MAX_BYTELEN
;
719 } else if (p
.family
== AF_INET6
) {
720 if (p
.prefixlen
> IPV6_MAX_BITLEN
) {
721 zlog_warn("%s: Specified prefix length %d is to large for a v6 address",
722 __PRETTY_FUNCTION__
, p
.prefixlen
);
725 STREAM_GET(&p
.u
.prefix6
, s
, IPV6_MAX_BYTELEN
);
726 l
+= IPV6_MAX_BYTELEN
;
729 "rnh_register: Received unknown family type %d\n",
733 rnh
= zebra_add_rnh(&p
, zvrf_id(zvrf
), type
);
734 if (type
== RNH_NEXTHOP_TYPE
) {
736 && !CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
))
737 SET_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
);
739 && CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
))
740 UNSET_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
);
741 } else if (type
== RNH_IMPORT_CHECK_TYPE
) {
743 && !CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
))
744 SET_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
);
745 else if (!flags
&& CHECK_FLAG(rnh
->flags
,
746 ZEBRA_NHT_EXACT_MATCH
))
747 UNSET_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
);
750 zebra_add_rnh_client(rnh
, client
, type
, zvrf_id(zvrf
));
751 /* Anything not AF_INET/INET6 has been filtered out above */
752 zebra_evaluate_rnh(zvrf_id(zvrf
), p
.family
, 1, type
, &p
);
759 /* Nexthop register */
760 static int zserv_rnh_unregister(struct zserv
*client
, u_short length
,
761 rnh_type_t type
, struct zebra_vrf
*zvrf
)
768 if (IS_ZEBRA_DEBUG_NHT
)
769 zlog_debug("rnh_unregister msg from client %s: length=%d\n",
770 zebra_route_string(client
->proto
), length
);
777 STREAM_GETC(s
, flags
);
781 STREAM_GETW(s
, p
.family
);
782 STREAM_GETC(s
, p
.prefixlen
);
784 if (p
.family
== AF_INET
) {
785 if (p
.prefixlen
> IPV4_MAX_BITLEN
) {
786 zlog_warn("%s: Specified prefix length %d is to large for a v4 address",
787 __PRETTY_FUNCTION__
, p
.prefixlen
);
790 STREAM_GET(&p
.u
.prefix4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
791 l
+= IPV4_MAX_BYTELEN
;
792 } else if (p
.family
== AF_INET6
) {
793 if (p
.prefixlen
> IPV6_MAX_BITLEN
) {
794 zlog_warn("%s: Specified prefix length %d is to large for a v6 address",
795 __PRETTY_FUNCTION__
, p
.prefixlen
);
798 STREAM_GET(&p
.u
.prefix6
, s
, IPV6_MAX_BYTELEN
);
799 l
+= IPV6_MAX_BYTELEN
;
802 "rnh_register: Received unknown family type %d\n",
806 rnh
= zebra_lookup_rnh(&p
, zvrf_id(zvrf
), type
);
808 client
->nh_dereg_time
= monotime(NULL
);
809 zebra_remove_rnh_client(rnh
, client
, type
);
816 #define ZEBRA_MIN_FEC_LENGTH 5
819 static int zserv_fec_register(struct zserv
*client
, u_short length
)
822 struct zebra_vrf
*zvrf
;
826 u_int32_t label_index
= MPLS_INVALID_LABEL_INDEX
;
829 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
831 return 0; // unexpected
834 * The minimum amount of data that can be sent for one fec
837 if (length
< ZEBRA_MIN_FEC_LENGTH
) {
839 "fec_register: Received a fec register of length %d, it is of insufficient size to properly decode",
845 STREAM_GETW(s
, flags
);
846 memset(&p
, 0, sizeof(p
));
847 STREAM_GETW(s
, p
.family
);
848 if (p
.family
!= AF_INET
&& p
.family
!= AF_INET6
) {
850 "fec_register: Received unknown family type %d\n",
854 STREAM_GETC(s
, p
.prefixlen
);
855 if ((p
.family
== AF_INET
&& p
.prefixlen
> IPV4_MAX_BITLEN
) ||
856 (p
.family
== AF_INET6
&& p
.prefixlen
> IPV6_MAX_BITLEN
)) {
857 zlog_warn("%s: Specified prefix length: %d is to long for %d",
858 __PRETTY_FUNCTION__
, p
.prefixlen
, p
.family
);
862 STREAM_GET(&p
.u
.prefix
, s
, PSIZE(p
.prefixlen
));
863 l
+= PSIZE(p
.prefixlen
);
864 if (flags
& ZEBRA_FEC_REGISTER_LABEL_INDEX
) {
865 STREAM_GETL(s
, label_index
);
868 label_index
= MPLS_INVALID_LABEL_INDEX
;
869 zebra_mpls_fec_register(zvrf
, &p
, label_index
, client
);
877 static int zserv_fec_unregister(struct zserv
*client
, u_short length
)
880 struct zebra_vrf
*zvrf
;
886 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
888 return 0; // unexpected
891 * The minimum amount of data that can be sent for one
894 if (length
< ZEBRA_MIN_FEC_LENGTH
) {
896 "fec_unregister: Received a fec unregister of length %d, it is of insufficient size to properly decode",
902 STREAM_GETW(s
, flags
);
906 memset(&p
, 0, sizeof(p
));
907 STREAM_GETW(s
, p
.family
);
908 if (p
.family
!= AF_INET
&& p
.family
!= AF_INET6
) {
910 "fec_unregister: Received unknown family type %d\n",
914 STREAM_GETC(s
, p
.prefixlen
);
915 if ((p
.family
== AF_INET
&& p
.prefixlen
> IPV4_MAX_BITLEN
) ||
916 (p
.family
== AF_INET6
&& p
.prefixlen
> IPV6_MAX_BITLEN
)) {
917 zlog_warn("%s: Received prefix length %d which is greater than %d can support",
918 __PRETTY_FUNCTION__
, p
.prefixlen
, p
.family
);
922 STREAM_GET(&p
.u
.prefix
, s
, PSIZE(p
.prefixlen
));
923 l
+= PSIZE(p
.prefixlen
);
924 zebra_mpls_fec_unregister(zvrf
, &p
, client
);
932 Modified version of zsend_ipv4_nexthop_lookup():
933 Query unicast rib if nexthop is not found on mrib.
934 Returns both route metric and protocol distance.
936 static int zsend_ipv4_nexthop_lookup_mrib(struct zserv
*client
,
938 struct route_entry
*re
,
939 struct zebra_vrf
*zvrf
)
944 struct nexthop
*nexthop
;
946 /* Get output stream. */
950 /* Fill in result. */
951 zclient_create_header(s
, ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB
, zvrf_id(zvrf
));
952 stream_put_in_addr(s
, &addr
);
955 stream_putc(s
, re
->distance
);
956 stream_putl(s
, re
->metric
);
958 nump
= stream_get_endp(
959 s
); /* remember position for nexthop_num */
960 stream_putc(s
, 0); /* reserve room for nexthop_num */
961 /* Only non-recursive routes are elegible to resolve the nexthop
963 * are looking up. Therefore, we will just iterate over the top
964 * chain of nexthops. */
965 for (nexthop
= re
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
966 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
967 num
+= zsend_write_nexthop(s
, nexthop
);
969 stream_putc_at(s
, nump
, num
); /* store nexthop_num */
971 stream_putc(s
, 0); /* distance */
972 stream_putl(s
, 0); /* metric */
973 stream_putc(s
, 0); /* nexthop_num */
976 stream_putw_at(s
, 0, stream_get_endp(s
));
978 return zebra_server_send_message(client
);
981 int zsend_route_notify_owner(u_char proto
, u_short instance
,
982 vrf_id_t vrf_id
, struct prefix
*p
,
983 enum zapi_route_notify_owner note
)
985 struct zserv
*client
;
989 client
= zebra_find_client(proto
, instance
);
990 if (!client
|| !client
->notify_owner
) {
991 if (IS_ZEBRA_DEBUG_PACKET
) {
992 char buff
[PREFIX_STRLEN
];
994 zlog_debug("Not Notifying Owner: %u about prefix %s",
995 proto
, prefix2str(p
, buff
, sizeof(buff
)));
1003 zclient_create_header(s
, ZEBRA_ROUTE_NOTIFY_OWNER
, vrf_id
);
1005 stream_put(s
, ¬e
, sizeof(note
));
1007 stream_putc(s
, p
->family
);
1009 blen
= prefix_blen(p
);
1010 stream_putc(s
, p
->prefixlen
);
1011 stream_put(s
, &p
->u
.prefix
, blen
);
1013 stream_putw_at(s
, 0, stream_get_endp(s
));
1015 return zebra_server_send_message(client
);
1018 /* Router-id is updated. Send ZEBRA_ROUTER_ID_ADD to client. */
1019 int zsend_router_id_update(struct zserv
*client
, struct prefix
*p
,
1025 /* Check this client need interface information. */
1026 if (!vrf_bitmap_check(client
->ridinfo
, vrf_id
))
1033 zclient_create_header(s
, ZEBRA_ROUTER_ID_UPDATE
, vrf_id
);
1035 /* Prefix information. */
1036 stream_putc(s
, p
->family
);
1037 blen
= prefix_blen(p
);
1038 stream_put(s
, &p
->u
.prefix
, blen
);
1039 stream_putc(s
, p
->prefixlen
);
1041 /* Write packet size. */
1042 stream_putw_at(s
, 0, stream_get_endp(s
));
1044 return zebra_server_send_message(client
);
1048 * Function used by Zebra to send a PW status update to LDP daemon
1050 int zsend_pw_update(struct zserv
*client
, struct zebra_pw
*pw
)
1057 zclient_create_header(s
, ZEBRA_PW_STATUS_UPDATE
, pw
->vrf_id
);
1058 stream_write(s
, pw
->ifname
, IF_NAMESIZE
);
1059 stream_putl(s
, pw
->ifindex
);
1060 stream_putl(s
, pw
->status
);
1062 /* Put length at the first point of the stream. */
1063 stream_putw_at(s
, 0, stream_get_endp(s
));
1065 return zebra_server_send_message(client
);
1068 /* Register zebra server interface information. Send current all
1069 interface and address information. */
1070 static int zread_interface_add(struct zserv
*client
, u_short length
,
1071 struct zebra_vrf
*zvrf
)
1074 struct interface
*ifp
;
1076 /* Interface information is needed. */
1077 vrf_bitmap_set(client
->ifinfo
, zvrf_id(zvrf
));
1079 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
1080 FOR_ALL_INTERFACES (vrf
, ifp
) {
1081 /* Skip pseudo interface. */
1082 if (!CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_ACTIVE
))
1085 if (zsend_interface_add(client
, ifp
) < 0)
1088 if (zsend_interface_addresses(client
, ifp
) < 0)
1095 /* Unregister zebra server interface information. */
1096 static int zread_interface_delete(struct zserv
*client
, u_short length
,
1097 struct zebra_vrf
*zvrf
)
1099 vrf_bitmap_unset(client
->ifinfo
, zvrf_id(zvrf
));
1103 void zserv_nexthop_num_warn(const char *caller
, const struct prefix
*p
,
1104 const unsigned int nexthop_num
)
1106 if (nexthop_num
> multipath_num
) {
1107 char buff
[PREFIX2STR_BUFFER
];
1108 prefix2str(p
, buff
, sizeof(buff
));
1110 "%s: Prefix %s has %d nexthops, but we can only use the first %d",
1111 caller
, buff
, nexthop_num
, multipath_num
);
1115 static int zread_route_add(struct zserv
*client
, u_short length
,
1116 struct zebra_vrf
*zvrf
)
1119 struct zapi_route api
;
1120 struct zapi_nexthop
*api_nh
;
1122 struct prefix_ipv6
*src_p
= NULL
;
1123 struct route_entry
*re
;
1124 struct nexthop
*nexthop
= NULL
;
1126 vrf_id_t vrf_id
= 0;
1129 if (zapi_route_decode(s
, &api
) < 0)
1132 /* Allocate new route. */
1133 vrf_id
= zvrf_id(zvrf
);
1134 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
1135 re
->type
= api
.type
;
1136 re
->instance
= api
.instance
;
1137 re
->flags
= api
.flags
;
1138 re
->uptime
= time(NULL
);
1139 re
->vrf_id
= vrf_id
;
1140 re
->nh_vrf_id
= api
.nh_vrf_id
;
1141 re
->table
= zvrf
->table_id
;
1143 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
)) {
1144 for (i
= 0; i
< api
.nexthop_num
; i
++) {
1145 api_nh
= &api
.nexthops
[i
];
1146 ifindex_t ifindex
= 0;
1148 switch (api_nh
->type
) {
1149 case NEXTHOP_TYPE_IFINDEX
:
1150 nexthop
= route_entry_nexthop_ifindex_add(
1151 re
, api_nh
->ifindex
);
1153 case NEXTHOP_TYPE_IPV4
:
1154 nexthop
= route_entry_nexthop_ipv4_add(
1155 re
, &api_nh
->gate
.ipv4
, NULL
);
1157 case NEXTHOP_TYPE_IPV4_IFINDEX
: {
1159 struct ipaddr vtep_ip
;
1161 memset(&vtep_ip
, 0, sizeof(struct ipaddr
));
1162 if (CHECK_FLAG(api
.flags
,
1163 ZEBRA_FLAG_EVPN_ROUTE
)) {
1165 get_l3vni_svi_ifindex(vrf_id
);
1167 ifindex
= api_nh
->ifindex
;
1170 nexthop
= route_entry_nexthop_ipv4_ifindex_add(
1171 re
, &api_nh
->gate
.ipv4
, NULL
,
1174 /* if this an EVPN route entry,
1175 program the nh as neigh
1177 if (CHECK_FLAG(api
.flags
,
1178 ZEBRA_FLAG_EVPN_ROUTE
)) {
1179 SET_FLAG(nexthop
->flags
,
1180 NEXTHOP_FLAG_EVPN_RVTEP
);
1181 vtep_ip
.ipa_type
= IPADDR_V4
;
1182 memcpy(&(vtep_ip
.ipaddr_v4
),
1183 &(api_nh
->gate
.ipv4
),
1184 sizeof(struct in_addr
));
1185 zebra_vxlan_evpn_vrf_route_add(
1193 case NEXTHOP_TYPE_IPV6
:
1194 nexthop
= route_entry_nexthop_ipv6_add(
1195 re
, &api_nh
->gate
.ipv6
);
1197 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1198 nexthop
= route_entry_nexthop_ipv6_ifindex_add(
1199 re
, &api_nh
->gate
.ipv6
,
1202 case NEXTHOP_TYPE_BLACKHOLE
:
1203 nexthop
= route_entry_nexthop_blackhole_add(
1204 re
, api_nh
->bh_type
);
1209 zlog_warn("%s: Nexthops Specified: %d but we failed to properly create one",
1210 __PRETTY_FUNCTION__
, api
.nexthop_num
);
1211 nexthops_free(re
->nexthop
);
1212 XFREE(MTYPE_RE
, re
);
1215 /* MPLS labels for BGP-LU or Segment Routing */
1216 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_LABEL
)
1217 && api_nh
->type
!= NEXTHOP_TYPE_IFINDEX
1218 && api_nh
->type
!= NEXTHOP_TYPE_BLACKHOLE
) {
1219 enum lsp_types_t label_type
;
1222 lsp_type_from_re_type(client
->proto
);
1223 nexthop_add_labels(nexthop
, label_type
,
1225 &api_nh
->labels
[0]);
1230 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
))
1231 re
->distance
= api
.distance
;
1232 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
))
1233 re
->metric
= api
.metric
;
1234 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_TAG
))
1236 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_MTU
))
1239 afi
= family2afi(api
.prefix
.family
);
1240 if (afi
!= AFI_IP6
&& CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
)) {
1241 zlog_warn("%s: Received SRC Prefix but afi is not v6",
1242 __PRETTY_FUNCTION__
);
1243 nexthops_free(re
->nexthop
);
1244 XFREE(MTYPE_RE
, re
);
1247 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
))
1248 src_p
= &api
.src_prefix
;
1250 ret
= rib_add_multipath(afi
, api
.safi
, &api
.prefix
, src_p
, re
);
1253 switch (api
.prefix
.family
) {
1256 client
->v4_route_add_cnt
++;
1258 client
->v4_route_upd8_cnt
++;
1262 client
->v6_route_add_cnt
++;
1264 client
->v6_route_upd8_cnt
++;
1271 static int zread_route_del(struct zserv
*client
, u_short length
,
1272 struct zebra_vrf
*zvrf
)
1275 struct zapi_route api
;
1277 struct prefix_ipv6
*src_p
= NULL
;
1280 if (zapi_route_decode(s
, &api
) < 0)
1283 afi
= family2afi(api
.prefix
.family
);
1284 if (afi
!= AFI_IP6
&& CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
)) {
1285 zlog_warn("%s: Received a src prefix while afi is not v6",
1286 __PRETTY_FUNCTION__
);
1289 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
))
1290 src_p
= &api
.src_prefix
;
1292 rib_delete(afi
, api
.safi
, zvrf_id(zvrf
), api
.type
, api
.instance
,
1293 api
.flags
, &api
.prefix
, src_p
, NULL
, zvrf
->table_id
,
1294 api
.metric
, false, &api
.rmac
);
1297 switch (api
.prefix
.family
) {
1299 client
->v4_route_del_cnt
++;
1302 client
->v6_route_del_cnt
++;
1309 /* This function support multiple nexthop. */
1311 * Parse the ZEBRA_IPV4_ROUTE_ADD sent from client. Update re and
1314 static int zread_ipv4_add(struct zserv
*client
, u_short length
,
1315 struct zebra_vrf
*zvrf
)
1318 struct route_entry
*re
;
1321 struct in_addr nhop_addr
;
1323 u_char nexthop_type
;
1328 enum lsp_types_t label_type
= ZEBRA_LSP_NONE
;
1330 struct nexthop
*nexthop
;
1331 enum blackhole_type bh_type
= BLACKHOLE_NULL
;
1333 /* Get input stream. */
1336 /* Allocate new re. */
1337 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
1339 /* Type, flags, message. */
1340 STREAM_GETC(s
, re
->type
);
1341 if (re
->type
> ZEBRA_ROUTE_MAX
) {
1342 zlog_warn("%s: Specified route type %d is not a legal value\n",
1343 __PRETTY_FUNCTION__
, re
->type
);
1344 XFREE(MTYPE_RE
, re
);
1347 STREAM_GETW(s
, re
->instance
);
1348 STREAM_GETL(s
, re
->flags
);
1349 STREAM_GETC(s
, message
);
1350 STREAM_GETW(s
, safi
);
1351 re
->uptime
= time(NULL
);
1354 memset(&p
, 0, sizeof(struct prefix_ipv4
));
1356 STREAM_GETC(s
, p
.prefixlen
);
1357 if (p
.prefixlen
> IPV4_MAX_BITLEN
) {
1358 zlog_warn("%s: Specified prefix length %d is greater than what v4 can be",
1359 __PRETTY_FUNCTION__
, p
.prefixlen
);
1360 XFREE(MTYPE_RE
, re
);
1363 STREAM_GET(&p
.u
.prefix4
, s
, PSIZE(p
.prefixlen
));
1366 re
->vrf_id
= zvrf_id(zvrf
);
1367 re
->nh_vrf_id
= zvrf_id(zvrf
);
1369 /* Nexthop parse. */
1370 if (CHECK_FLAG(message
, ZAPI_MESSAGE_NEXTHOP
)) {
1371 STREAM_GETC(s
, nexthop_num
);
1372 zserv_nexthop_num_warn(__func__
, (const struct prefix
*)&p
,
1375 if (CHECK_FLAG(message
, ZAPI_MESSAGE_LABEL
))
1376 label_type
= lsp_type_from_re_type(client
->proto
);
1378 for (i
= 0; i
< nexthop_num
; i
++) {
1379 STREAM_GETC(s
, nexthop_type
);
1381 switch (nexthop_type
) {
1382 case NEXTHOP_TYPE_IFINDEX
:
1383 STREAM_GETL(s
, ifindex
);
1384 route_entry_nexthop_ifindex_add(re
, ifindex
);
1386 case NEXTHOP_TYPE_IPV4
:
1387 STREAM_GET(&nhop_addr
.s_addr
, s
,
1389 nexthop
= route_entry_nexthop_ipv4_add(
1390 re
, &nhop_addr
, NULL
);
1391 /* For labeled-unicast, each nexthop is followed
1393 if (CHECK_FLAG(message
, ZAPI_MESSAGE_LABEL
)) {
1394 STREAM_GETL(s
, label
);
1395 nexthop_add_labels(nexthop
, label_type
,
1399 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1400 STREAM_GET(&nhop_addr
.s_addr
, s
,
1402 STREAM_GETL(s
, ifindex
);
1403 route_entry_nexthop_ipv4_ifindex_add(
1404 re
, &nhop_addr
, NULL
, ifindex
);
1406 case NEXTHOP_TYPE_IPV6
:
1407 zlog_warn("%s: Please use ZEBRA_ROUTE_ADD if you want to pass v6 nexthops",
1408 __PRETTY_FUNCTION__
);
1409 nexthops_free(re
->nexthop
);
1410 XFREE(MTYPE_RE
, re
);
1413 case NEXTHOP_TYPE_BLACKHOLE
:
1414 route_entry_nexthop_blackhole_add(re
, bh_type
);
1417 zlog_warn("%s: Specified nexthop type: %d does not exist",
1418 __PRETTY_FUNCTION__
, nexthop_type
);
1419 nexthops_free(re
->nexthop
);
1420 XFREE(MTYPE_RE
, re
);
1427 if (CHECK_FLAG(message
, ZAPI_MESSAGE_DISTANCE
))
1428 STREAM_GETC(s
, re
->distance
);
1431 if (CHECK_FLAG(message
, ZAPI_MESSAGE_METRIC
))
1432 STREAM_GETL(s
, re
->metric
);
1435 if (CHECK_FLAG(message
, ZAPI_MESSAGE_TAG
))
1436 STREAM_GETL(s
, re
->tag
);
1440 if (CHECK_FLAG(message
, ZAPI_MESSAGE_MTU
))
1441 STREAM_GETL(s
, re
->mtu
);
1446 re
->table
= zvrf
->table_id
;
1448 ret
= rib_add_multipath(AFI_IP
, safi
, &p
, NULL
, re
);
1452 client
->v4_route_add_cnt
++;
1454 client
->v4_route_upd8_cnt
++;
1459 nexthops_free(re
->nexthop
);
1460 XFREE(MTYPE_RE
, re
);
1464 /* Zebra server IPv4 prefix delete function. */
1465 static int zread_ipv4_delete(struct zserv
*client
, u_short length
,
1466 struct zebra_vrf
*zvrf
)
1469 struct zapi_ipv4 api
;
1475 /* Type, flags, message. */
1476 STREAM_GETC(s
, api
.type
);
1477 STREAM_GETW(s
, api
.instance
);
1478 STREAM_GETL(s
, api
.flags
);
1479 STREAM_GETC(s
, api
.message
);
1480 STREAM_GETW(s
, api
.safi
);
1483 memset(&p
, 0, sizeof(struct prefix
));
1485 STREAM_GETC(s
, p
.prefixlen
);
1486 if (p
.prefixlen
> IPV4_MAX_BITLEN
) {
1487 zlog_warn("%s: Passed in prefixlen %d is impossible",
1488 __PRETTY_FUNCTION__
, p
.prefixlen
);
1491 STREAM_GET(&p
.u
.prefix4
, s
, PSIZE(p
.prefixlen
));
1493 table_id
= zvrf
->table_id
;
1495 rib_delete(AFI_IP
, api
.safi
, zvrf_id(zvrf
), api
.type
, api
.instance
,
1496 api
.flags
, &p
, NULL
, NULL
, table_id
, 0, false, NULL
);
1497 client
->v4_route_del_cnt
++;
1503 /* MRIB Nexthop lookup for IPv4. */
1504 static int zread_ipv4_nexthop_lookup_mrib(struct zserv
*client
, u_short length
,
1505 struct zebra_vrf
*zvrf
)
1507 struct in_addr addr
;
1508 struct route_entry
*re
;
1510 STREAM_GET(&addr
.s_addr
, client
->ibuf
, IPV4_MAX_BYTELEN
);
1511 re
= rib_match_ipv4_multicast(zvrf_id(zvrf
), addr
, NULL
);
1512 return zsend_ipv4_nexthop_lookup_mrib(client
, addr
, re
, zvrf
);
1518 /* Zebra server IPv6 prefix add function. */
1519 static int zread_ipv4_route_ipv6_nexthop_add(struct zserv
*client
,
1521 struct zebra_vrf
*zvrf
)
1525 struct in6_addr nhop_addr
;
1526 struct route_entry
*re
;
1529 u_char nexthop_type
;
1532 static struct in6_addr nexthops
[MULTIPATH_NUM
];
1533 static unsigned int ifindices
[MULTIPATH_NUM
];
1535 static mpls_label_t labels
[MULTIPATH_NUM
];
1536 enum lsp_types_t label_type
= ZEBRA_LSP_NONE
;
1538 struct nexthop
*nexthop
;
1539 enum blackhole_type bh_type
= BLACKHOLE_NULL
;
1541 /* Get input stream. */
1544 memset(&nhop_addr
, 0, sizeof(struct in6_addr
));
1546 /* Allocate new re. */
1547 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
1549 /* Type, flags, message. */
1550 STREAM_GETC(s
, re
->type
);
1551 if (re
->type
> ZEBRA_ROUTE_MAX
) {
1552 zlog_warn("%s: Specified route type: %d is not a legal value\n",
1553 __PRETTY_FUNCTION__
, re
->type
);
1554 XFREE(MTYPE_RE
, re
);
1557 STREAM_GETW(s
, re
->instance
);
1558 STREAM_GETL(s
, re
->flags
);
1559 STREAM_GETC(s
, message
);
1560 STREAM_GETW(s
, safi
);
1561 re
->uptime
= time(NULL
);
1564 memset(&p
, 0, sizeof(struct prefix_ipv4
));
1566 STREAM_GETC(s
, p
.prefixlen
);
1567 if (p
.prefixlen
> IPV4_MAX_BITLEN
) {
1568 zlog_warn("%s: Prefix Length %d is greater than what a v4 address can use",
1569 __PRETTY_FUNCTION__
, p
.prefixlen
);
1570 XFREE(MTYPE_RE
, re
);
1573 STREAM_GET(&p
.u
.prefix4
, s
, PSIZE(p
.prefixlen
));
1576 re
->vrf_id
= zvrf_id(zvrf
);
1577 re
->nh_vrf_id
= zvrf_id(zvrf
);
1579 /* We need to give nh-addr, nh-ifindex with the same next-hop object
1580 * to the re to ensure that IPv6 multipathing works; need to coalesce
1581 * these. Clients should send the same number of paired set of
1582 * next-hop-addr/next-hop-ifindices. */
1583 if (CHECK_FLAG(message
, ZAPI_MESSAGE_NEXTHOP
)) {
1584 unsigned int nh_count
= 0;
1585 unsigned int if_count
= 0;
1586 unsigned int max_nh_if
= 0;
1588 STREAM_GETC(s
, nexthop_num
);
1589 zserv_nexthop_num_warn(__func__
, (const struct prefix
*)&p
,
1592 if (CHECK_FLAG(message
, ZAPI_MESSAGE_LABEL
))
1593 label_type
= lsp_type_from_re_type(client
->proto
);
1595 for (i
= 0; i
< nexthop_num
; i
++) {
1596 STREAM_GETC(s
, nexthop_type
);
1598 switch (nexthop_type
) {
1599 case NEXTHOP_TYPE_IPV6
:
1600 STREAM_GET(&nhop_addr
, s
, 16);
1601 if (nh_count
< MULTIPATH_NUM
) {
1602 /* For labeled-unicast, each nexthop is
1603 * followed by label. */
1604 if (CHECK_FLAG(message
,
1605 ZAPI_MESSAGE_LABEL
)) {
1606 STREAM_GETL(s
, label
);
1607 labels
[nh_count
] = label
;
1609 nexthops
[nh_count
] = nhop_addr
;
1613 case NEXTHOP_TYPE_IFINDEX
:
1614 if (if_count
< multipath_num
) {
1615 STREAM_GETL(s
, ifindices
[if_count
++]);
1618 case NEXTHOP_TYPE_BLACKHOLE
:
1619 route_entry_nexthop_blackhole_add(re
, bh_type
);
1622 zlog_warn("%s: Please use ZEBRA_ROUTE_ADD if you want to pass non v6 nexthops",
1623 __PRETTY_FUNCTION__
);
1624 nexthops_free(re
->nexthop
);
1625 XFREE(MTYPE_RE
, re
);
1630 max_nh_if
= (nh_count
> if_count
) ? nh_count
: if_count
;
1631 for (i
= 0; i
< max_nh_if
; i
++) {
1633 && !IN6_IS_ADDR_UNSPECIFIED(&nexthops
[i
])) {
1634 if ((i
< if_count
) && ifindices
[i
])
1636 route_entry_nexthop_ipv6_ifindex_add(
1640 nexthop
= route_entry_nexthop_ipv6_add(
1643 if (CHECK_FLAG(message
, ZAPI_MESSAGE_LABEL
))
1644 nexthop_add_labels(nexthop
, label_type
,
1647 if ((i
< if_count
) && ifindices
[i
])
1648 route_entry_nexthop_ifindex_add(
1655 if (CHECK_FLAG(message
, ZAPI_MESSAGE_DISTANCE
))
1656 STREAM_GETC(s
, re
->distance
);
1659 if (CHECK_FLAG(message
, ZAPI_MESSAGE_METRIC
))
1660 STREAM_GETL(s
, re
->metric
);
1663 if (CHECK_FLAG(message
, ZAPI_MESSAGE_TAG
))
1664 STREAM_GETL(s
, re
->tag
);
1668 if (CHECK_FLAG(message
, ZAPI_MESSAGE_MTU
))
1669 STREAM_GETL(s
, re
->mtu
);
1674 re
->table
= zvrf
->table_id
;
1676 ret
= rib_add_multipath(AFI_IP6
, safi
, &p
, NULL
, re
);
1679 client
->v4_route_add_cnt
++;
1681 client
->v4_route_upd8_cnt
++;
1686 nexthops_free(re
->nexthop
);
1687 XFREE(MTYPE_RE
, re
);
1691 static int zread_ipv6_add(struct zserv
*client
, u_short length
,
1692 struct zebra_vrf
*zvrf
)
1696 struct in6_addr nhop_addr
;
1698 struct route_entry
*re
;
1701 u_char nexthop_type
;
1703 struct prefix_ipv6 src_p
, *src_pp
;
1705 static struct in6_addr nexthops
[MULTIPATH_NUM
];
1706 static unsigned int ifindices
[MULTIPATH_NUM
];
1708 static mpls_label_t labels
[MULTIPATH_NUM
];
1709 enum lsp_types_t label_type
= ZEBRA_LSP_NONE
;
1711 struct nexthop
*nexthop
;
1712 enum blackhole_type bh_type
= BLACKHOLE_NULL
;
1714 /* Get input stream. */
1717 memset(&nhop_addr
, 0, sizeof(struct in6_addr
));
1719 /* Allocate new re. */
1720 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
1722 /* Type, flags, message. */
1723 STREAM_GETC(s
, re
->type
);
1724 if (re
->type
> ZEBRA_ROUTE_MAX
) {
1725 zlog_warn("%s: Specified route type: %d is not a legal value\n",
1726 __PRETTY_FUNCTION__
, re
->type
);
1727 XFREE(MTYPE_RE
, re
);
1730 STREAM_GETW(s
, re
->instance
);
1731 STREAM_GETL(s
, re
->flags
);
1732 STREAM_GETC(s
, message
);
1733 STREAM_GETW(s
, safi
);
1734 re
->uptime
= time(NULL
);
1737 memset(&p
, 0, sizeof(p
));
1738 p
.family
= AF_INET6
;
1739 STREAM_GETC(s
, p
.prefixlen
);
1740 if (p
.prefixlen
> IPV6_MAX_BITLEN
) {
1741 zlog_warn("%s: Specified prefix length %d is to large for v6 prefix",
1742 __PRETTY_FUNCTION__
, p
.prefixlen
);
1743 XFREE(MTYPE_RE
, re
);
1746 STREAM_GET(&p
.u
.prefix6
, s
, PSIZE(p
.prefixlen
));
1748 if (CHECK_FLAG(message
, ZAPI_MESSAGE_SRCPFX
)) {
1749 memset(&src_p
, 0, sizeof(src_p
));
1750 src_p
.family
= AF_INET6
;
1751 STREAM_GETC(s
, src_p
.prefixlen
);
1752 if (src_p
.prefixlen
> IPV6_MAX_BITLEN
) {
1753 zlog_warn("%s: Specified src prefix length %d is to large for v6 prefix",
1754 __PRETTY_FUNCTION__
, src_p
.prefixlen
);
1755 XFREE(MTYPE_RE
, re
);
1758 STREAM_GET(&src_p
.prefix
, s
, PSIZE(src_p
.prefixlen
));
1763 /* We need to give nh-addr, nh-ifindex with the same next-hop object
1764 * to the re to ensure that IPv6 multipathing works; need to coalesce
1765 * these. Clients should send the same number of paired set of
1766 * next-hop-addr/next-hop-ifindices. */
1767 if (CHECK_FLAG(message
, ZAPI_MESSAGE_NEXTHOP
)) {
1768 unsigned int nh_count
= 0;
1769 unsigned int if_count
= 0;
1770 unsigned int max_nh_if
= 0;
1772 STREAM_GETC(s
, nexthop_num
);
1773 zserv_nexthop_num_warn(__func__
, (const struct prefix
*)&p
,
1776 if (CHECK_FLAG(message
, ZAPI_MESSAGE_LABEL
))
1777 label_type
= lsp_type_from_re_type(client
->proto
);
1779 for (i
= 0; i
< nexthop_num
; i
++) {
1780 STREAM_GETC(s
, nexthop_type
);
1782 switch (nexthop_type
) {
1783 case NEXTHOP_TYPE_IPV6
:
1784 STREAM_GET(&nhop_addr
, s
, 16);
1785 if (nh_count
< MULTIPATH_NUM
) {
1786 /* For labeled-unicast, each nexthop is
1787 * followed by label. */
1788 if (CHECK_FLAG(message
,
1789 ZAPI_MESSAGE_LABEL
)) {
1790 STREAM_GETL(s
, label
);
1791 labels
[nh_count
] = label
;
1793 nexthops
[nh_count
++] = nhop_addr
;
1796 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1797 STREAM_GET(&nhop_addr
, s
, 16);
1798 STREAM_GETL(s
, ifindex
);
1799 route_entry_nexthop_ipv6_ifindex_add(
1800 re
, &nhop_addr
, ifindex
);
1802 case NEXTHOP_TYPE_IFINDEX
:
1803 if (if_count
< multipath_num
) {
1804 STREAM_GETL(s
, ifindices
[if_count
++]);
1807 case NEXTHOP_TYPE_BLACKHOLE
:
1808 route_entry_nexthop_blackhole_add(re
, bh_type
);
1811 zlog_warn("%s: Please use ZEBRA_ROUTE_ADD if you want to pass non v6 nexthops",
1812 __PRETTY_FUNCTION__
);
1813 nexthops_free(re
->nexthop
);
1814 XFREE(MTYPE_RE
, re
);
1819 max_nh_if
= (nh_count
> if_count
) ? nh_count
: if_count
;
1820 for (i
= 0; i
< max_nh_if
; i
++) {
1822 && !IN6_IS_ADDR_UNSPECIFIED(&nexthops
[i
])) {
1823 if ((i
< if_count
) && ifindices
[i
])
1825 route_entry_nexthop_ipv6_ifindex_add(
1829 nexthop
= route_entry_nexthop_ipv6_add(
1831 if (CHECK_FLAG(message
, ZAPI_MESSAGE_LABEL
))
1832 nexthop_add_labels(nexthop
, label_type
,
1835 if ((i
< if_count
) && ifindices
[i
])
1836 route_entry_nexthop_ifindex_add(
1843 if (CHECK_FLAG(message
, ZAPI_MESSAGE_DISTANCE
))
1844 STREAM_GETC(s
, re
->distance
);
1847 if (CHECK_FLAG(message
, ZAPI_MESSAGE_METRIC
))
1848 STREAM_GETL(s
, re
->metric
);
1851 if (CHECK_FLAG(message
, ZAPI_MESSAGE_TAG
))
1852 STREAM_GETL(s
, re
->tag
);
1856 if (CHECK_FLAG(message
, ZAPI_MESSAGE_MTU
))
1857 STREAM_GETL(s
, re
->mtu
);
1862 re
->vrf_id
= zvrf_id(zvrf
);
1863 re
->nh_vrf_id
= zvrf_id(zvrf
);
1865 re
->table
= zvrf
->table_id
;
1867 ret
= rib_add_multipath(AFI_IP6
, safi
, &p
, src_pp
, re
);
1870 client
->v6_route_add_cnt
++;
1872 client
->v6_route_upd8_cnt
++;
1877 nexthops_free(re
->nexthop
);
1878 XFREE(MTYPE_RE
, re
);
1883 /* Zebra server IPv6 prefix delete function. */
1884 static int zread_ipv6_delete(struct zserv
*client
, u_short length
,
1885 struct zebra_vrf
*zvrf
)
1888 struct zapi_ipv6 api
;
1890 struct prefix_ipv6 src_p
, *src_pp
;
1894 /* Type, flags, message. */
1895 STREAM_GETC(s
, api
.type
);
1896 STREAM_GETW(s
, api
.instance
);
1897 STREAM_GETL(s
, api
.flags
);
1898 STREAM_GETC(s
, api
.message
);
1899 STREAM_GETW(s
, api
.safi
);
1902 memset(&p
, 0, sizeof(struct prefix
));
1903 p
.family
= AF_INET6
;
1904 STREAM_GETC(s
, p
.prefixlen
);
1905 STREAM_GET(&p
.u
.prefix6
, s
, PSIZE(p
.prefixlen
));
1907 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
)) {
1908 memset(&src_p
, 0, sizeof(struct prefix_ipv6
));
1909 src_p
.family
= AF_INET6
;
1910 STREAM_GETC(s
, src_p
.prefixlen
);
1911 STREAM_GET(&src_p
.prefix
, s
, PSIZE(src_p
.prefixlen
));
1916 rib_delete(AFI_IP6
, api
.safi
, zvrf_id(zvrf
), api
.type
, api
.instance
,
1917 api
.flags
, &p
, src_pp
, NULL
, client
->rtm_table
, 0, false,
1920 client
->v6_route_del_cnt
++;
1926 /* Register zebra server router-id information. Send current router-id */
1927 static int zread_router_id_add(struct zserv
*client
, u_short length
,
1928 struct zebra_vrf
*zvrf
)
1932 /* Router-id information is needed. */
1933 vrf_bitmap_set(client
->ridinfo
, zvrf_id(zvrf
));
1935 router_id_get(&p
, zvrf_id(zvrf
));
1937 return zsend_router_id_update(client
, &p
, zvrf_id(zvrf
));
1940 /* Unregister zebra server router-id information. */
1941 static int zread_router_id_delete(struct zserv
*client
, u_short length
,
1942 struct zebra_vrf
*zvrf
)
1944 vrf_bitmap_unset(client
->ridinfo
, zvrf_id(zvrf
));
1948 /* Tie up route-type and client->sock */
1949 static void zread_hello(struct zserv
*client
)
1951 /* type of protocol (lib/zebra.h) */
1956 STREAM_GETC(client
->ibuf
, proto
);
1957 STREAM_GETW(client
->ibuf
, instance
);
1958 STREAM_GETC(client
->ibuf
, notify
);
1960 client
->notify_owner
= true;
1962 /* accept only dynamic routing protocols */
1963 if ((proto
< ZEBRA_ROUTE_MAX
) && (proto
> ZEBRA_ROUTE_STATIC
)) {
1965 "client %d says hello and bids fair to announce only %s routes",
1966 client
->sock
, zebra_route_string(proto
));
1968 zlog_notice("client protocol instance %d", instance
);
1970 client
->proto
= proto
;
1971 client
->instance
= instance
;
1978 /* Unregister all information in a VRF. */
1979 static int zread_vrf_unregister(struct zserv
*client
, u_short length
,
1980 struct zebra_vrf
*zvrf
)
1985 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
1986 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
1987 vrf_bitmap_unset(client
->redist
[afi
][i
], zvrf_id(zvrf
));
1988 vrf_bitmap_unset(client
->redist_default
, zvrf_id(zvrf
));
1989 vrf_bitmap_unset(client
->ifinfo
, zvrf_id(zvrf
));
1990 vrf_bitmap_unset(client
->ridinfo
, zvrf_id(zvrf
));
1995 static void zread_mpls_labels(int command
, struct zserv
*client
, u_short length
,
1996 struct zebra_vrf
*zvrf
)
1999 enum lsp_types_t type
;
2000 struct prefix prefix
;
2001 enum nexthop_types_t gtype
;
2004 mpls_label_t in_label
, out_label
;
2007 /* Get input stream. */
2011 STREAM_GETC(s
, type
);
2012 STREAM_GETL(s
, prefix
.family
);
2013 switch (prefix
.family
) {
2015 STREAM_GET(&prefix
.u
.prefix4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
2016 STREAM_GETC(s
, prefix
.prefixlen
);
2017 if (prefix
.prefixlen
> IPV4_MAX_BITLEN
) {
2018 zlog_warn("%s: Specified prefix length %d is greater than a v4 address can support",
2019 __PRETTY_FUNCTION__
,
2023 STREAM_GET(&gate
.ipv4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
2026 STREAM_GET(&prefix
.u
.prefix6
, s
, 16);
2027 STREAM_GETC(s
, prefix
.prefixlen
);
2028 if (prefix
.prefixlen
> IPV6_MAX_BITLEN
) {
2029 zlog_warn("%s: Specified prefix length %d is greater than a v6 address can support",
2030 __PRETTY_FUNCTION__
,
2034 STREAM_GET(&gate
.ipv6
, s
, 16);
2037 zlog_warn("%s: Specified AF %d is not supported for this call",
2038 __PRETTY_FUNCTION__
, prefix
.family
);
2041 STREAM_GETL(s
, ifindex
);
2042 STREAM_GETC(s
, distance
);
2043 STREAM_GETL(s
, in_label
);
2044 STREAM_GETL(s
, out_label
);
2046 switch (prefix
.family
) {
2049 gtype
= NEXTHOP_TYPE_IPV4_IFINDEX
;
2051 gtype
= NEXTHOP_TYPE_IPV4
;
2055 gtype
= NEXTHOP_TYPE_IPV6_IFINDEX
;
2057 gtype
= NEXTHOP_TYPE_IPV6
;
2066 if (command
== ZEBRA_MPLS_LABELS_ADD
) {
2067 mpls_lsp_install(zvrf
, type
, in_label
, out_label
, gtype
, &gate
,
2069 mpls_ftn_update(1, zvrf
, type
, &prefix
, gtype
, &gate
, ifindex
,
2070 distance
, out_label
);
2071 } else if (command
== ZEBRA_MPLS_LABELS_DELETE
) {
2072 mpls_lsp_uninstall(zvrf
, type
, in_label
, gtype
, &gate
, ifindex
);
2073 mpls_ftn_update(0, zvrf
, type
, &prefix
, gtype
, &gate
, ifindex
,
2074 distance
, out_label
);
2079 /* Send response to a label manager connect request to client */
2080 static int zsend_label_manager_connect_response(struct zserv
*client
,
2081 vrf_id_t vrf_id
, u_short result
)
2088 zclient_create_header(s
, ZEBRA_LABEL_MANAGER_CONNECT
, vrf_id
);
2091 stream_putc(s
, result
);
2093 /* Write packet size. */
2094 stream_putw_at(s
, 0, stream_get_endp(s
));
2096 return writen(client
->sock
, s
->data
, stream_get_endp(s
));
2099 static void zread_label_manager_connect(struct zserv
*client
, vrf_id_t vrf_id
)
2102 /* type of protocol (lib/zebra.h) */
2106 /* Get input stream. */
2110 STREAM_GETC(s
, proto
);
2111 STREAM_GETW(s
, instance
);
2113 /* accept only dynamic routing protocols */
2114 if ((proto
>= ZEBRA_ROUTE_MAX
) || (proto
<= ZEBRA_ROUTE_STATIC
)) {
2115 zlog_err("client %d has wrong protocol %s", client
->sock
,
2116 zebra_route_string(proto
));
2117 zsend_label_manager_connect_response(client
, vrf_id
, 1);
2120 zlog_notice("client %d with instance %u connected as %s", client
->sock
,
2121 instance
, zebra_route_string(proto
));
2122 client
->proto
= proto
;
2123 client
->instance
= instance
;
2126 Release previous labels of same protocol and instance.
2127 This is done in case it restarted from an unexpected shutdown.
2129 release_daemon_chunks(proto
, instance
);
2132 " Label Manager client connected: sock %d, proto %s, instance %u",
2133 client
->sock
, zebra_route_string(proto
), instance
);
2134 /* send response back */
2135 zsend_label_manager_connect_response(client
, vrf_id
, 0);
2140 /* Send response to a get label chunk request to client */
2141 static int zsend_assign_label_chunk_response(struct zserv
*client
,
2143 struct label_manager_chunk
*lmc
)
2150 zclient_create_header(s
, ZEBRA_GET_LABEL_CHUNK
, vrf_id
);
2154 stream_putc(s
, lmc
->keep
);
2155 /* start and end labels */
2156 stream_putl(s
, lmc
->start
);
2157 stream_putl(s
, lmc
->end
);
2160 /* Write packet size. */
2161 stream_putw_at(s
, 0, stream_get_endp(s
));
2163 return writen(client
->sock
, s
->data
, stream_get_endp(s
));
2166 static void zread_get_label_chunk(struct zserv
*client
, vrf_id_t vrf_id
)
2171 struct label_manager_chunk
*lmc
;
2173 /* Get input stream. */
2177 STREAM_GETC(s
, keep
);
2178 STREAM_GETL(s
, size
);
2180 lmc
= assign_label_chunk(client
->proto
, client
->instance
, keep
, size
);
2182 zlog_err("%s: Unable to assign Label Chunk of size %u",
2185 zlog_debug("Assigned Label Chunk %u - %u to %u", lmc
->start
,
2187 /* send response back */
2188 zsend_assign_label_chunk_response(client
, vrf_id
, lmc
);
2194 static void zread_release_label_chunk(struct zserv
*client
)
2197 uint32_t start
, end
;
2199 /* Get input stream. */
2203 STREAM_GETL(s
, start
);
2204 STREAM_GETL(s
, end
);
2206 release_label_chunk(client
->proto
, client
->instance
, start
, end
);
2211 static void zread_label_manager_request(int cmd
, struct zserv
*client
,
2212 struct zebra_vrf
*zvrf
)
2214 /* to avoid sending other messages like ZERBA_INTERFACE_UP */
2215 if (cmd
== ZEBRA_LABEL_MANAGER_CONNECT
)
2216 client
->is_synchronous
= 1;
2218 /* external label manager */
2220 zread_relay_label_manager_request(cmd
, client
,
2222 /* this is a label manager */
2224 if (cmd
== ZEBRA_LABEL_MANAGER_CONNECT
)
2225 zread_label_manager_connect(client
,
2228 /* Sanity: don't allow 'unidentified' requests */
2229 if (!client
->proto
) {
2231 "Got label request from an unidentified client");
2234 if (cmd
== ZEBRA_GET_LABEL_CHUNK
)
2235 zread_get_label_chunk(client
,
2237 else if (cmd
== ZEBRA_RELEASE_LABEL_CHUNK
)
2238 zread_release_label_chunk(client
);
2243 static int zread_pseudowire(int command
, struct zserv
*client
, u_short length
,
2244 struct zebra_vrf
*zvrf
)
2247 char ifname
[IF_NAMESIZE
];
2251 union g_addr nexthop
;
2252 uint32_t local_label
;
2253 uint32_t remote_label
;
2255 union pw_protocol_fields data
;
2257 struct zebra_pw
*pw
;
2259 /* Get input stream. */
2263 STREAM_GET(ifname
, s
, IF_NAMESIZE
);
2264 STREAM_GETL(s
, ifindex
);
2265 STREAM_GETL(s
, type
);
2269 STREAM_GET(&nexthop
.ipv4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
2272 STREAM_GET(&nexthop
.ipv6
, s
, 16);
2277 STREAM_GETL(s
, local_label
);
2278 STREAM_GETL(s
, remote_label
);
2279 STREAM_GETC(s
, flags
);
2280 STREAM_GET(&data
, s
, sizeof(data
));
2281 protocol
= client
->proto
;
2283 pw
= zebra_pw_find(zvrf
, ifname
);
2287 zlog_warn("%s: pseudowire %s already exists [%s]",
2289 zserv_command_string(command
));
2293 zebra_pw_add(zvrf
, ifname
, protocol
, client
);
2295 case ZEBRA_PW_DELETE
:
2297 zlog_warn("%s: pseudowire %s not found [%s]", __func__
,
2298 ifname
, zserv_command_string(command
));
2302 zebra_pw_del(zvrf
, pw
);
2305 case ZEBRA_PW_UNSET
:
2307 zlog_warn("%s: pseudowire %s not found [%s]", __func__
,
2308 ifname
, zserv_command_string(command
));
2316 case ZEBRA_PW_UNSET
:
2321 zebra_pw_change(pw
, ifindex
, type
, af
, &nexthop
, local_label
,
2322 remote_label
, flags
, &data
);
2330 /* Cleanup registered nexthops (across VRFs) upon client disconnect. */
2331 static void zebra_client_close_cleanup_rnh(struct zserv
*client
)
2334 struct zebra_vrf
*zvrf
;
2336 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
2337 if ((zvrf
= vrf
->info
) != NULL
) {
2338 zebra_cleanup_rnh_client(zvrf_id(zvrf
), AF_INET
, client
,
2340 zebra_cleanup_rnh_client(zvrf_id(zvrf
), AF_INET6
,
2341 client
, RNH_NEXTHOP_TYPE
);
2342 zebra_cleanup_rnh_client(zvrf_id(zvrf
), AF_INET
, client
,
2343 RNH_IMPORT_CHECK_TYPE
);
2344 zebra_cleanup_rnh_client(zvrf_id(zvrf
), AF_INET6
,
2345 client
, RNH_IMPORT_CHECK_TYPE
);
2346 if (client
->proto
== ZEBRA_ROUTE_LDP
) {
2347 hash_iterate(zvrf
->lsp_table
,
2348 mpls_ldp_lsp_uninstall_all
,
2350 mpls_ldp_ftn_uninstall_all(zvrf
, AFI_IP
);
2351 mpls_ldp_ftn_uninstall_all(zvrf
, AFI_IP6
);
2357 /* free zebra client information. */
2358 static void zebra_client_free(struct zserv
*client
)
2360 /* Send client de-registration to BFD */
2361 zebra_ptm_bfd_client_deregister(client
->proto
);
2363 /* Cleanup any registered nexthops - across all VRFs. */
2364 zebra_client_close_cleanup_rnh(client
);
2366 /* Release Label Manager chunks */
2367 release_daemon_chunks(client
->proto
, client
->instance
);
2369 /* Cleanup any FECs registered by this client. */
2370 zebra_mpls_cleanup_fecs_for_client(vrf_info_lookup(VRF_DEFAULT
),
2373 /* Remove pseudowires associated with this client */
2374 zebra_pw_client_close(client
);
2376 /* Close file descriptor. */
2378 unsigned long nroutes
;
2380 close(client
->sock
);
2381 nroutes
= rib_score_proto(client
->proto
, client
->instance
);
2383 "client %d disconnected. %lu %s routes removed from the rib",
2384 client
->sock
, nroutes
,
2385 zebra_route_string(client
->proto
));
2389 /* Free stream buffers. */
2391 stream_free(client
->ibuf
);
2393 stream_free(client
->obuf
);
2395 buffer_free(client
->wb
);
2397 /* Release threads. */
2399 thread_cancel(client
->t_read
);
2400 if (client
->t_write
)
2401 thread_cancel(client
->t_write
);
2402 if (client
->t_suicide
)
2403 thread_cancel(client
->t_suicide
);
2406 for (afi_t afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
2407 for (int i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
2408 vrf_bitmap_free(client
->redist
[afi
][i
]);
2410 vrf_bitmap_free(client
->redist_default
);
2411 vrf_bitmap_free(client
->ifinfo
);
2412 vrf_bitmap_free(client
->ridinfo
);
2414 XFREE(MTYPE_TMP
, client
);
2417 static void zebra_client_close(struct zserv
*client
)
2419 listnode_delete(zebrad
.client_list
, client
);
2420 zebra_client_free(client
);
2423 /* Make new client. */
2424 static void zebra_client_create(int sock
)
2426 struct zserv
*client
;
2430 client
= XCALLOC(MTYPE_TMP
, sizeof(struct zserv
));
2432 /* Make client input/output buffer. */
2433 client
->sock
= sock
;
2434 client
->ibuf
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
2435 client
->obuf
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
2436 client
->wb
= buffer_new(0);
2438 /* Set table number. */
2439 client
->rtm_table
= zebrad
.rtm_table_default
;
2441 client
->connect_time
= monotime(NULL
);
2442 /* Initialize flags */
2443 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
2444 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
2445 client
->redist
[afi
][i
] = vrf_bitmap_init();
2446 client
->redist_default
= vrf_bitmap_init();
2447 client
->ifinfo
= vrf_bitmap_init();
2448 client
->ridinfo
= vrf_bitmap_init();
2450 /* by default, it's not a synchronous client */
2451 client
->is_synchronous
= 0;
2453 /* Add this client to linked list. */
2454 listnode_add(zebrad
.client_list
, client
);
2456 /* Make new read thread. */
2457 zebra_event(ZEBRA_READ
, sock
, client
);
2459 zebra_vrf_update_all(client
);
2462 static int zread_interface_set_master(struct zserv
*client
,
2465 struct interface
*master
;
2466 struct interface
*slave
;
2467 struct stream
*s
= client
->ibuf
;
2471 STREAM_GETL(s
, vrf_id
);
2472 STREAM_GETL(s
, ifindex
);
2473 master
= if_lookup_by_index(ifindex
, vrf_id
);
2475 STREAM_GETL(s
, vrf_id
);
2476 STREAM_GETL(s
, ifindex
);
2477 slave
= if_lookup_by_index(ifindex
, vrf_id
);
2479 if (!master
|| !slave
)
2482 kernel_interface_set_master(master
, slave
);
2488 static inline void zserv_handle_commands(struct zserv
*client
,
2491 struct zebra_vrf
*zvrf
)
2494 case ZEBRA_ROUTER_ID_ADD
:
2495 zread_router_id_add(client
, length
, zvrf
);
2497 case ZEBRA_ROUTER_ID_DELETE
:
2498 zread_router_id_delete(client
, length
, zvrf
);
2500 case ZEBRA_INTERFACE_ADD
:
2501 zread_interface_add(client
, length
, zvrf
);
2503 case ZEBRA_INTERFACE_DELETE
:
2504 zread_interface_delete(client
, length
, zvrf
);
2506 case ZEBRA_ROUTE_ADD
:
2507 zread_route_add(client
, length
, zvrf
);
2509 case ZEBRA_ROUTE_DELETE
:
2510 zread_route_del(client
, length
, zvrf
);
2512 case ZEBRA_IPV4_ROUTE_ADD
:
2513 zread_ipv4_add(client
, length
, zvrf
);
2515 case ZEBRA_IPV4_ROUTE_DELETE
:
2516 zread_ipv4_delete(client
, length
, zvrf
);
2518 case ZEBRA_IPV4_ROUTE_IPV6_NEXTHOP_ADD
:
2519 zread_ipv4_route_ipv6_nexthop_add(client
, length
, zvrf
);
2521 case ZEBRA_IPV6_ROUTE_ADD
:
2522 zread_ipv6_add(client
, length
, zvrf
);
2524 case ZEBRA_IPV6_ROUTE_DELETE
:
2525 zread_ipv6_delete(client
, length
, zvrf
);
2527 case ZEBRA_REDISTRIBUTE_ADD
:
2528 zebra_redistribute_add(command
, client
, length
, zvrf
);
2530 case ZEBRA_REDISTRIBUTE_DELETE
:
2531 zebra_redistribute_delete(command
, client
, length
, zvrf
);
2533 case ZEBRA_REDISTRIBUTE_DEFAULT_ADD
:
2534 zebra_redistribute_default_add(command
, client
, length
, zvrf
);
2536 case ZEBRA_REDISTRIBUTE_DEFAULT_DELETE
:
2537 zebra_redistribute_default_delete(command
, client
, length
,
2540 case ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB
:
2541 zread_ipv4_nexthop_lookup_mrib(client
, length
, zvrf
);
2544 zread_hello(client
);
2546 case ZEBRA_NEXTHOP_REGISTER
:
2547 zserv_rnh_register(client
, length
, RNH_NEXTHOP_TYPE
,
2550 case ZEBRA_NEXTHOP_UNREGISTER
:
2551 zserv_rnh_unregister(client
, length
, RNH_NEXTHOP_TYPE
,
2554 case ZEBRA_IMPORT_ROUTE_REGISTER
:
2555 zserv_rnh_register(client
, length
, RNH_IMPORT_CHECK_TYPE
,
2558 case ZEBRA_IMPORT_ROUTE_UNREGISTER
:
2559 zserv_rnh_unregister(client
, length
,
2560 RNH_IMPORT_CHECK_TYPE
, zvrf
);
2562 case ZEBRA_BFD_DEST_UPDATE
:
2563 case ZEBRA_BFD_DEST_REGISTER
:
2564 zebra_ptm_bfd_dst_register(client
, length
, command
, zvrf
);
2566 case ZEBRA_BFD_DEST_DEREGISTER
:
2567 zebra_ptm_bfd_dst_deregister(client
, length
, zvrf
);
2569 case ZEBRA_VRF_UNREGISTER
:
2570 zread_vrf_unregister(client
, length
, zvrf
);
2572 case ZEBRA_BFD_CLIENT_REGISTER
:
2573 zebra_ptm_bfd_client_register(client
, length
);
2575 case ZEBRA_INTERFACE_ENABLE_RADV
:
2576 #if defined(HAVE_RTADV)
2577 zebra_interface_radv_set(client
, length
, zvrf
, 1);
2580 case ZEBRA_INTERFACE_DISABLE_RADV
:
2581 #if defined(HAVE_RTADV)
2582 zebra_interface_radv_set(client
, length
, zvrf
, 0);
2585 case ZEBRA_MPLS_LABELS_ADD
:
2586 case ZEBRA_MPLS_LABELS_DELETE
:
2587 zread_mpls_labels(command
, client
, length
, zvrf
);
2589 case ZEBRA_IPMR_ROUTE_STATS
:
2590 zebra_ipmr_route_stats(client
, length
, zvrf
);
2592 case ZEBRA_LABEL_MANAGER_CONNECT
:
2593 case ZEBRA_GET_LABEL_CHUNK
:
2594 case ZEBRA_RELEASE_LABEL_CHUNK
:
2595 zread_label_manager_request(command
, client
, zvrf
);
2597 case ZEBRA_FEC_REGISTER
:
2598 zserv_fec_register(client
, length
);
2600 case ZEBRA_FEC_UNREGISTER
:
2601 zserv_fec_unregister(client
, length
);
2603 case ZEBRA_ADVERTISE_DEFAULT_GW
:
2604 zebra_vxlan_advertise_gw_macip(client
, length
, zvrf
);
2606 case ZEBRA_ADVERTISE_SUBNET
:
2607 zebra_vxlan_advertise_subnet(client
, length
, zvrf
);
2609 case ZEBRA_ADVERTISE_ALL_VNI
:
2610 zebra_vxlan_advertise_all_vni(client
, length
, zvrf
);
2612 case ZEBRA_REMOTE_VTEP_ADD
:
2613 zebra_vxlan_remote_vtep_add(client
, length
, zvrf
);
2615 case ZEBRA_REMOTE_VTEP_DEL
:
2616 zebra_vxlan_remote_vtep_del(client
, length
, zvrf
);
2618 case ZEBRA_REMOTE_MACIP_ADD
:
2619 zebra_vxlan_remote_macip_add(client
, length
, zvrf
);
2621 case ZEBRA_REMOTE_MACIP_DEL
:
2622 zebra_vxlan_remote_macip_del(client
, length
, zvrf
);
2624 case ZEBRA_INTERFACE_SET_MASTER
:
2625 zread_interface_set_master(client
, length
);
2628 case ZEBRA_PW_DELETE
:
2630 case ZEBRA_PW_UNSET
:
2631 zread_pseudowire(command
, client
, length
, zvrf
);
2634 zlog_info("Zebra received unknown command %d", command
);
2639 #if defined(HANDLE_ZAPI_FUZZING)
2640 static void zserv_write_incoming(struct stream
*orig
, uint16_t command
)
2642 char fname
[MAXPATHLEN
];
2643 struct stream
*copy
;
2646 copy
= stream_dup(orig
);
2647 stream_set_getp(copy
, 0);
2649 zserv_privs
.change(ZPRIVS_RAISE
);
2650 snprintf(fname
, MAXPATHLEN
, "%s/%u", DAEMON_VTY_DIR
, command
);
2651 fd
= open(fname
, O_CREAT
| O_WRONLY
| O_EXCL
, 0644);
2652 stream_flush(copy
, fd
);
2654 zserv_privs
.change(ZPRIVS_LOWER
);
2659 /* Handler of zebra service request. */
2660 static int zebra_client_read(struct thread
*thread
)
2663 struct zserv
*client
;
2665 uint16_t length
, command
;
2666 uint8_t marker
, version
;
2668 struct zebra_vrf
*zvrf
;
2669 #if defined(HANDLE_ZAPI_FUZZING)
2672 int packets
= zebrad
.packets_to_process
;
2675 /* Get thread data. Reset reading thread because I'm running. */
2676 sock
= THREAD_FD(thread
);
2677 client
= THREAD_ARG(thread
);
2678 client
->t_read
= NULL
;
2680 if (client
->t_suicide
) {
2681 zebra_client_close(client
);
2686 /* Read length and command (if we don't have it already). */
2687 if ((already
= stream_get_endp(client
->ibuf
))
2688 < ZEBRA_HEADER_SIZE
) {
2691 stream_read_try(client
->ibuf
, sock
,
2692 ZEBRA_HEADER_SIZE
- already
))
2695 if (IS_ZEBRA_DEBUG_EVENT
)
2696 zlog_debug("connection closed socket [%d]",
2698 zebra_client_close(client
);
2701 if (nbyte
!= (ssize_t
)(ZEBRA_HEADER_SIZE
- already
)) {
2702 /* Try again later. */
2703 zebra_event(ZEBRA_READ
, sock
, client
);
2706 already
= ZEBRA_HEADER_SIZE
;
2709 /* Reset to read from the beginning of the incoming packet. */
2710 stream_set_getp(client
->ibuf
, 0);
2712 /* Fetch header values */
2713 STREAM_GETW(client
->ibuf
, length
);
2714 STREAM_GETC(client
->ibuf
, marker
);
2715 STREAM_GETC(client
->ibuf
, version
);
2716 STREAM_GETL(client
->ibuf
, vrf_id
);
2717 STREAM_GETW(client
->ibuf
, command
);
2719 if (marker
!= ZEBRA_HEADER_MARKER
|| version
!= ZSERV_VERSION
) {
2721 "%s: socket %d version mismatch, marker %d, version %d",
2722 __func__
, sock
, marker
, version
);
2723 zebra_client_close(client
);
2726 if (length
< ZEBRA_HEADER_SIZE
) {
2728 "%s: socket %d message length %u is less than header size %d",
2729 __func__
, sock
, length
, ZEBRA_HEADER_SIZE
);
2730 zebra_client_close(client
);
2733 if (length
> STREAM_SIZE(client
->ibuf
)) {
2735 "%s: socket %d message length %u exceeds buffer size %lu",
2736 __func__
, sock
, length
,
2737 (u_long
)STREAM_SIZE(client
->ibuf
));
2738 zebra_client_close(client
);
2742 /* Read rest of data. */
2743 if (already
< length
) {
2745 if (((nbyte
= stream_read_try(client
->ibuf
, sock
,
2749 if (IS_ZEBRA_DEBUG_EVENT
)
2751 "connection closed [%d] when reading zebra data",
2753 zebra_client_close(client
);
2756 if (nbyte
!= (ssize_t
)(length
- already
)) {
2757 /* Try again later. */
2758 zebra_event(ZEBRA_READ
, sock
, client
);
2763 #if defined(HANDLE_ZAPI_FUZZING)
2764 zserv_write_incoming(client
->ibuf
, command
);
2766 length
-= ZEBRA_HEADER_SIZE
;
2768 /* Debug packet information. */
2769 if (IS_ZEBRA_DEBUG_EVENT
)
2770 zlog_debug("zebra message comes from socket [%d]", sock
);
2772 if (IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
)
2773 zlog_debug("zebra message received [%s] %d in VRF %u",
2774 zserv_command_string(command
), length
, vrf_id
);
2776 client
->last_read_time
= monotime(NULL
);
2777 client
->last_read_cmd
= command
;
2779 zvrf
= zebra_vrf_lookup_by_id(vrf_id
);
2781 if (IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
)
2782 zlog_debug("zebra received unknown VRF[%u]", vrf_id
);
2783 goto zclient_read_out
;
2786 zserv_handle_commands(client
, command
, length
, zvrf
);
2788 if (client
->t_suicide
) {
2789 /* No need to wait for thread callback, just kill immediately.
2791 zebra_client_close(client
);
2795 stream_reset(client
->ibuf
);
2800 stream_reset(client
->ibuf
);
2801 zebra_event(ZEBRA_READ
, sock
, client
);
2806 /* Accept code of zebra server socket. */
2807 static int zebra_accept(struct thread
*thread
)
2811 struct sockaddr_in client
;
2814 accept_sock
= THREAD_FD(thread
);
2816 /* Reregister myself. */
2817 zebra_event(ZEBRA_SERV
, accept_sock
, NULL
);
2819 len
= sizeof(struct sockaddr_in
);
2820 client_sock
= accept(accept_sock
, (struct sockaddr
*)&client
, &len
);
2822 if (client_sock
< 0) {
2823 zlog_warn("Can't accept zebra socket: %s",
2824 safe_strerror(errno
));
2828 /* Make client socket non-blocking. */
2829 set_nonblocking(client_sock
);
2831 /* Create new zebra client. */
2832 zebra_client_create(client_sock
);
2837 /* Make zebra server socket, wiping any existing one (see bug #403). */
2838 void zebra_zserv_socket_init(char *path
)
2843 struct sockaddr_storage sa
;
2846 if (!frr_zclient_addr(&sa
, &sa_len
, path
))
2847 /* should be caught in zebra main() */
2851 old_mask
= umask(0077);
2853 /* Make UNIX domain socket. */
2854 sock
= socket(sa
.ss_family
, SOCK_STREAM
, 0);
2856 zlog_warn("Can't create zserv socket: %s",
2857 safe_strerror(errno
));
2859 "zebra can't provide full functionality due to above error");
2863 if (sa
.ss_family
!= AF_UNIX
) {
2864 sockopt_reuseaddr(sock
);
2865 sockopt_reuseport(sock
);
2867 struct sockaddr_un
*suna
= (struct sockaddr_un
*)&sa
;
2868 if (suna
->sun_path
[0])
2869 unlink(suna
->sun_path
);
2872 zserv_privs
.change(ZPRIVS_RAISE
);
2873 setsockopt_so_recvbuf(sock
, 1048576);
2874 setsockopt_so_sendbuf(sock
, 1048576);
2875 zserv_privs
.change(ZPRIVS_LOWER
);
2877 if (sa
.ss_family
!= AF_UNIX
&& zserv_privs
.change(ZPRIVS_RAISE
))
2878 zlog_err("Can't raise privileges");
2880 ret
= bind(sock
, (struct sockaddr
*)&sa
, sa_len
);
2882 zlog_warn("Can't bind zserv socket on %s: %s", path
,
2883 safe_strerror(errno
));
2885 "zebra can't provide full functionality due to above error");
2889 if (sa
.ss_family
!= AF_UNIX
&& zserv_privs
.change(ZPRIVS_LOWER
))
2890 zlog_err("Can't lower privileges");
2892 ret
= listen(sock
, 5);
2894 zlog_warn("Can't listen to zserv socket %s: %s", path
,
2895 safe_strerror(errno
));
2897 "zebra can't provide full functionality due to above error");
2904 zebra_event(ZEBRA_SERV
, sock
, NULL
);
2908 static void zebra_event(enum event event
, int sock
, struct zserv
*client
)
2912 thread_add_read(zebrad
.master
, zebra_accept
, client
, sock
,
2916 client
->t_read
= NULL
;
2917 thread_add_read(zebrad
.master
, zebra_client_read
, client
, sock
,
2926 #define ZEBRA_TIME_BUF 32
2927 static char *zserv_time_buf(time_t *time1
, char *buf
, int buflen
)
2932 assert(buf
!= NULL
);
2933 assert(buflen
>= ZEBRA_TIME_BUF
);
2934 assert(time1
!= NULL
);
2937 snprintf(buf
, buflen
, "never ");
2941 now
= monotime(NULL
);
2945 if (now
< ONE_DAY_SECOND
)
2946 snprintf(buf
, buflen
, "%02d:%02d:%02d", tm
->tm_hour
, tm
->tm_min
,
2948 else if (now
< ONE_WEEK_SECOND
)
2949 snprintf(buf
, buflen
, "%dd%02dh%02dm", tm
->tm_yday
, tm
->tm_hour
,
2952 snprintf(buf
, buflen
, "%02dw%dd%02dh", tm
->tm_yday
/ 7,
2953 tm
->tm_yday
- ((tm
->tm_yday
/ 7) * 7), tm
->tm_hour
);
2957 static void zebra_show_client_detail(struct vty
*vty
, struct zserv
*client
)
2959 char cbuf
[ZEBRA_TIME_BUF
], rbuf
[ZEBRA_TIME_BUF
];
2960 char wbuf
[ZEBRA_TIME_BUF
], nhbuf
[ZEBRA_TIME_BUF
], mbuf
[ZEBRA_TIME_BUF
];
2962 vty_out(vty
, "Client: %s", zebra_route_string(client
->proto
));
2963 if (client
->instance
)
2964 vty_out(vty
, " Instance: %d", client
->instance
);
2967 vty_out(vty
, "------------------------ \n");
2968 vty_out(vty
, "FD: %d \n", client
->sock
);
2969 vty_out(vty
, "Route Table ID: %d \n", client
->rtm_table
);
2971 vty_out(vty
, "Connect Time: %s \n",
2972 zserv_time_buf(&client
->connect_time
, cbuf
, ZEBRA_TIME_BUF
));
2973 if (client
->nh_reg_time
) {
2974 vty_out(vty
, "Nexthop Registry Time: %s \n",
2975 zserv_time_buf(&client
->nh_reg_time
, nhbuf
,
2977 if (client
->nh_last_upd_time
)
2978 vty_out(vty
, "Nexthop Last Update Time: %s \n",
2979 zserv_time_buf(&client
->nh_last_upd_time
, mbuf
,
2982 vty_out(vty
, "No Nexthop Update sent\n");
2984 vty_out(vty
, "Not registered for Nexthop Updates\n");
2986 vty_out(vty
, "Last Msg Rx Time: %s \n",
2987 zserv_time_buf(&client
->last_read_time
, rbuf
, ZEBRA_TIME_BUF
));
2988 vty_out(vty
, "Last Msg Tx Time: %s \n",
2989 zserv_time_buf(&client
->last_write_time
, wbuf
, ZEBRA_TIME_BUF
));
2990 if (client
->last_read_time
)
2991 vty_out(vty
, "Last Rcvd Cmd: %s \n",
2992 zserv_command_string(client
->last_read_cmd
));
2993 if (client
->last_write_time
)
2994 vty_out(vty
, "Last Sent Cmd: %s \n",
2995 zserv_command_string(client
->last_write_cmd
));
2998 vty_out(vty
, "Type Add Update Del \n");
2999 vty_out(vty
, "================================================== \n");
3000 vty_out(vty
, "IPv4 %-12d%-12d%-12d\n", client
->v4_route_add_cnt
,
3001 client
->v4_route_upd8_cnt
, client
->v4_route_del_cnt
);
3002 vty_out(vty
, "IPv6 %-12d%-12d%-12d\n", client
->v6_route_add_cnt
,
3003 client
->v6_route_upd8_cnt
, client
->v6_route_del_cnt
);
3004 vty_out(vty
, "Redist:v4 %-12d%-12d%-12d\n", client
->redist_v4_add_cnt
,
3005 0, client
->redist_v4_del_cnt
);
3006 vty_out(vty
, "Redist:v6 %-12d%-12d%-12d\n", client
->redist_v6_add_cnt
,
3007 0, client
->redist_v6_del_cnt
);
3008 vty_out(vty
, "Connected %-12d%-12d%-12d\n", client
->ifadd_cnt
, 0,
3010 vty_out(vty
, "BFD peer %-12d%-12d%-12d\n", client
->bfd_peer_add_cnt
,
3011 client
->bfd_peer_upd8_cnt
, client
->bfd_peer_del_cnt
);
3012 vty_out(vty
, "Interface Up Notifications: %d\n", client
->ifup_cnt
);
3013 vty_out(vty
, "Interface Down Notifications: %d\n", client
->ifdown_cnt
);
3014 vty_out(vty
, "VNI add notifications: %d\n", client
->vniadd_cnt
);
3015 vty_out(vty
, "VNI delete notifications: %d\n", client
->vnidel_cnt
);
3016 vty_out(vty
, "L3-VNI add notifications: %d\n", client
->l3vniadd_cnt
);
3017 vty_out(vty
, "L3-VNI delete notifications: %d\n", client
->l3vnidel_cnt
);
3018 vty_out(vty
, "MAC-IP add notifications: %d\n", client
->macipadd_cnt
);
3019 vty_out(vty
, "MAC-IP delete notifications: %d\n", client
->macipdel_cnt
);
3025 static void zebra_show_client_brief(struct vty
*vty
, struct zserv
*client
)
3027 char cbuf
[ZEBRA_TIME_BUF
], rbuf
[ZEBRA_TIME_BUF
];
3028 char wbuf
[ZEBRA_TIME_BUF
];
3030 vty_out(vty
, "%-8s%12s %12s%12s%8d/%-8d%8d/%-8d\n",
3031 zebra_route_string(client
->proto
),
3032 zserv_time_buf(&client
->connect_time
, cbuf
, ZEBRA_TIME_BUF
),
3033 zserv_time_buf(&client
->last_read_time
, rbuf
, ZEBRA_TIME_BUF
),
3034 zserv_time_buf(&client
->last_write_time
, wbuf
, ZEBRA_TIME_BUF
),
3035 client
->v4_route_add_cnt
+ client
->v4_route_upd8_cnt
,
3036 client
->v4_route_del_cnt
,
3037 client
->v6_route_add_cnt
+ client
->v6_route_upd8_cnt
,
3038 client
->v6_route_del_cnt
);
3041 struct zserv
*zebra_find_client(u_char proto
, u_short instance
)
3043 struct listnode
*node
, *nnode
;
3044 struct zserv
*client
;
3046 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, node
, nnode
, client
)) {
3047 if (client
->proto
== proto
&&
3048 client
->instance
== instance
)
3055 /* This command is for debugging purpose. */
3056 DEFUN (show_zebra_client
,
3057 show_zebra_client_cmd
,
3058 "show zebra client",
3061 "Client information\n")
3063 struct listnode
*node
;
3064 struct zserv
*client
;
3066 for (ALL_LIST_ELEMENTS_RO(zebrad
.client_list
, node
, client
))
3067 zebra_show_client_detail(vty
, client
);
3072 /* This command is for debugging purpose. */
3073 DEFUN (show_zebra_client_summary
,
3074 show_zebra_client_summary_cmd
,
3075 "show zebra client summary",
3078 "Client information brief\n"
3081 struct listnode
*node
;
3082 struct zserv
*client
;
3085 "Name Connect Time Last Read Last Write IPv4 Routes IPv6 Routes \n");
3087 "--------------------------------------------------------------------------------\n");
3089 for (ALL_LIST_ELEMENTS_RO(zebrad
.client_list
, node
, client
))
3090 zebra_show_client_brief(vty
, client
);
3092 vty_out(vty
, "Routes column shows (added+updated)/deleted\n");
3096 #if defined(HANDLE_ZAPI_FUZZING)
3097 void zserv_read_file(char *input
)
3100 struct zserv
*client
= NULL
;
3103 zebra_client_create(-1);
3104 client
= zebrad
.client_list
->head
->data
;
3107 fd
= open(input
, O_RDONLY
|O_NONBLOCK
);
3110 zebra_client_read(&t
);
3116 void zserv_init(void)
3118 /* Client list init. */
3119 zebrad
.client_list
= list_new();
3120 zebrad
.client_list
->del
= (void (*)(void *))zebra_client_free
;
3122 install_element(ENABLE_NODE
, &show_zebra_client_cmd
);
3123 install_element(ENABLE_NODE
, &show_zebra_client_summary_cmd
);