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"
63 #include "zebra/zebra_pbr.h"
64 #include "zebra/table_manager.h"
66 /* Event list of zebra. */
67 enum event
{ ZEBRA_READ
, ZEBRA_WRITE
};
69 extern struct zebra_privs_t zserv_privs
;
70 /* post event into client */
71 static void zebra_event(struct zserv
*client
, enum event event
);
74 /* Public interface ======================================================== */
76 int zebra_server_send_message(struct zserv
*client
, struct stream
*msg
)
78 stream_fifo_push(client
->obuf_fifo
, msg
);
79 zebra_event(client
, ZEBRA_WRITE
);
83 /* Encoding helpers -------------------------------------------------------- */
85 static void zserv_encode_interface(struct stream
*s
, struct interface
*ifp
)
87 /* Interface information. */
88 stream_put(s
, ifp
->name
, INTERFACE_NAMSIZ
);
89 stream_putl(s
, ifp
->ifindex
);
90 stream_putc(s
, ifp
->status
);
91 stream_putq(s
, ifp
->flags
);
92 stream_putc(s
, ifp
->ptm_enable
);
93 stream_putc(s
, ifp
->ptm_status
);
94 stream_putl(s
, ifp
->metric
);
95 stream_putl(s
, ifp
->speed
);
96 stream_putl(s
, ifp
->mtu
);
97 stream_putl(s
, ifp
->mtu6
);
98 stream_putl(s
, ifp
->bandwidth
);
99 stream_putl(s
, ifp
->ll_type
);
100 stream_putl(s
, ifp
->hw_addr_len
);
101 if (ifp
->hw_addr_len
)
102 stream_put(s
, ifp
->hw_addr
, ifp
->hw_addr_len
);
104 /* Then, Traffic Engineering parameters if any */
105 if (HAS_LINK_PARAMS(ifp
) && IS_LINK_PARAMS_SET(ifp
->link_params
)) {
107 zebra_interface_link_params_write(s
, ifp
);
111 /* Write packet size. */
112 stream_putw_at(s
, 0, stream_get_endp(s
));
115 static void zserv_encode_vrf(struct stream
*s
, struct zebra_vrf
*zvrf
)
117 struct vrf_data data
;
118 const char *netns_name
= zvrf_ns_name(zvrf
);
120 data
.l
.table_id
= zvrf
->table_id
;
123 strlcpy(data
.l
.netns_name
, basename((char *)netns_name
),
126 memset(data
.l
.netns_name
, 0, NS_NAMSIZ
);
127 /* Pass the tableid and the netns NAME */
128 stream_put(s
, &data
, sizeof(struct vrf_data
));
129 /* Interface information. */
130 stream_put(s
, zvrf_name(zvrf
), VRF_NAMSIZ
);
131 /* Write packet size. */
132 stream_putw_at(s
, 0, stream_get_endp(s
));
135 static int zserv_encode_nexthop(struct stream
*s
, struct nexthop
*nexthop
)
137 stream_putc(s
, nexthop
->type
);
138 switch (nexthop
->type
) {
139 case NEXTHOP_TYPE_IPV4
:
140 case NEXTHOP_TYPE_IPV4_IFINDEX
:
141 stream_put_in_addr(s
, &nexthop
->gate
.ipv4
);
142 stream_putl(s
, nexthop
->ifindex
);
144 case NEXTHOP_TYPE_IPV6
:
145 stream_put(s
, &nexthop
->gate
.ipv6
, 16);
147 case NEXTHOP_TYPE_IPV6_IFINDEX
:
148 stream_put(s
, &nexthop
->gate
.ipv6
, 16);
149 stream_putl(s
, nexthop
->ifindex
);
151 case NEXTHOP_TYPE_IFINDEX
:
152 stream_putl(s
, nexthop
->ifindex
);
161 /* Send handlers ----------------------------------------------------------- */
163 /* Interface is added. Send ZEBRA_INTERFACE_ADD to client. */
165 * This function is called in the following situations:
166 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
168 * - at startup, when zebra figures out the available interfaces
169 * - when an interface is added (where support for
170 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
171 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
174 int zsend_interface_add(struct zserv
*client
, struct interface
*ifp
)
176 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
178 zclient_create_header(s
, ZEBRA_INTERFACE_ADD
, ifp
->vrf_id
);
179 zserv_encode_interface(s
, ifp
);
182 return zebra_server_send_message(client
, s
);
185 /* Interface deletion from zebra daemon. */
186 int zsend_interface_delete(struct zserv
*client
, struct interface
*ifp
)
188 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
190 zclient_create_header(s
, ZEBRA_INTERFACE_DELETE
, ifp
->vrf_id
);
191 zserv_encode_interface(s
, ifp
);
194 return zebra_server_send_message(client
, s
);
197 int zsend_vrf_add(struct zserv
*client
, struct zebra_vrf
*zvrf
)
199 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
201 zclient_create_header(s
, ZEBRA_VRF_ADD
, zvrf_id(zvrf
));
202 zserv_encode_vrf(s
, zvrf
);
204 client
->vrfadd_cnt
++;
205 return zebra_server_send_message(client
, s
);
208 /* VRF deletion from zebra daemon. */
209 int zsend_vrf_delete(struct zserv
*client
, struct zebra_vrf
*zvrf
)
212 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
214 zclient_create_header(s
, ZEBRA_VRF_DELETE
, zvrf_id(zvrf
));
215 zserv_encode_vrf(s
, zvrf
);
217 client
->vrfdel_cnt
++;
218 return zebra_server_send_message(client
, s
);
221 int zsend_interface_link_params(struct zserv
*client
, struct interface
*ifp
)
223 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
225 /* Check this client need interface information. */
226 if (!client
->ifinfo
) {
231 if (!ifp
->link_params
) {
236 zclient_create_header(s
, ZEBRA_INTERFACE_LINK_PARAMS
, ifp
->vrf_id
);
238 /* Add Interface Index */
239 stream_putl(s
, ifp
->ifindex
);
241 /* Then TE Link Parameters */
242 if (zebra_interface_link_params_write(s
, ifp
) == 0) {
247 /* Write packet size. */
248 stream_putw_at(s
, 0, stream_get_endp(s
));
250 return zebra_server_send_message(client
, s
);
253 /* Interface address is added/deleted. Send ZEBRA_INTERFACE_ADDRESS_ADD or
254 * ZEBRA_INTERFACE_ADDRESS_DELETE to the client.
256 * A ZEBRA_INTERFACE_ADDRESS_ADD is sent in the following situations:
257 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
258 * from the client, after the ZEBRA_INTERFACE_ADD has been
259 * sent from zebra to the client
260 * - redistribute new address info to all clients in the following situations
261 * - at startup, when zebra figures out the available interfaces
262 * - when an interface is added (where support for
263 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
264 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
266 * - for the vty commands "ip address A.B.C.D/M [<secondary>|<label LINE>]"
267 * and "no bandwidth <1-10000000>", "ipv6 address X:X::X:X/M"
268 * - when an RTM_NEWADDR message is received from the kernel,
270 * The call tree that triggers ZEBRA_INTERFACE_ADDRESS_DELETE:
272 * zsend_interface_address(DELETE)
275 * zebra_interface_address_delete_update
277 * | | if_delete_update
279 * ip_address_uninstall connected_delete_ipv4
280 * [ipv6_addresss_uninstall] [connected_delete_ipv6]
283 * | RTM_NEWADDR on routing/netlink socket
286 * "no ip address A.B.C.D/M [label LINE]"
287 * "no ip address A.B.C.D/M secondary"
288 * ["no ipv6 address X:X::X:X/M"]
291 int zsend_interface_address(int cmd
, struct zserv
*client
,
292 struct interface
*ifp
, struct connected
*ifc
)
296 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
298 zclient_create_header(s
, cmd
, ifp
->vrf_id
);
299 stream_putl(s
, ifp
->ifindex
);
301 /* Interface address flag. */
302 stream_putc(s
, ifc
->flags
);
304 /* Prefix information. */
306 stream_putc(s
, p
->family
);
307 blen
= prefix_blen(p
);
308 stream_put(s
, &p
->u
.prefix
, blen
);
311 * XXX gnu version does not send prefixlen for
312 * ZEBRA_INTERFACE_ADDRESS_DELETE
313 * but zebra_interface_address_delete_read() in the gnu version
316 stream_putc(s
, p
->prefixlen
);
319 p
= ifc
->destination
;
321 stream_put(s
, &p
->u
.prefix
, blen
);
323 stream_put(s
, NULL
, blen
);
325 /* Write packet size. */
326 stream_putw_at(s
, 0, stream_get_endp(s
));
328 client
->connected_rt_add_cnt
++;
329 return zebra_server_send_message(client
, s
);
332 static int zsend_interface_nbr_address(int cmd
, struct zserv
*client
,
333 struct interface
*ifp
,
334 struct nbr_connected
*ifc
)
337 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
340 zclient_create_header(s
, cmd
, ifp
->vrf_id
);
341 stream_putl(s
, ifp
->ifindex
);
343 /* Prefix information. */
345 stream_putc(s
, p
->family
);
346 blen
= prefix_blen(p
);
347 stream_put(s
, &p
->u
.prefix
, blen
);
350 * XXX gnu version does not send prefixlen for
351 * ZEBRA_INTERFACE_ADDRESS_DELETE
352 * but zebra_interface_address_delete_read() in the gnu version
355 stream_putc(s
, p
->prefixlen
);
357 /* Write packet size. */
358 stream_putw_at(s
, 0, stream_get_endp(s
));
360 return zebra_server_send_message(client
, s
);
363 /* Interface address addition. */
364 static void zebra_interface_nbr_address_add_update(struct interface
*ifp
,
365 struct nbr_connected
*ifc
)
367 struct listnode
*node
, *nnode
;
368 struct zserv
*client
;
371 if (IS_ZEBRA_DEBUG_EVENT
) {
372 char buf
[INET6_ADDRSTRLEN
];
376 "MESSAGE: ZEBRA_INTERFACE_NBR_ADDRESS_ADD %s/%d on %s",
377 inet_ntop(p
->family
, &p
->u
.prefix
, buf
,
379 p
->prefixlen
, ifc
->ifp
->name
);
382 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, node
, nnode
, client
))
383 zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_ADD
,
387 /* Interface address deletion. */
388 static void zebra_interface_nbr_address_delete_update(struct interface
*ifp
,
389 struct nbr_connected
*ifc
)
391 struct listnode
*node
, *nnode
;
392 struct zserv
*client
;
395 if (IS_ZEBRA_DEBUG_EVENT
) {
396 char buf
[INET6_ADDRSTRLEN
];
400 "MESSAGE: ZEBRA_INTERFACE_NBR_ADDRESS_DELETE %s/%d on %s",
401 inet_ntop(p
->family
, &p
->u
.prefix
, buf
,
403 p
->prefixlen
, ifc
->ifp
->name
);
406 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, node
, nnode
, client
))
407 zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
,
411 /* Send addresses on interface to client */
412 int zsend_interface_addresses(struct zserv
*client
, struct interface
*ifp
)
414 struct listnode
*cnode
, *cnnode
;
416 struct nbr_connected
*nc
;
418 /* Send interface addresses. */
419 for (ALL_LIST_ELEMENTS(ifp
->connected
, cnode
, cnnode
, c
)) {
420 if (!CHECK_FLAG(c
->conf
, ZEBRA_IFC_REAL
))
423 if (zsend_interface_address(ZEBRA_INTERFACE_ADDRESS_ADD
, client
,
429 /* Send interface neighbors. */
430 for (ALL_LIST_ELEMENTS(ifp
->nbr_connected
, cnode
, cnnode
, nc
)) {
431 if (zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_ADD
,
440 /* Notify client about interface moving from one VRF to another.
441 * Whether client is interested in old and new VRF is checked by caller.
443 int zsend_interface_vrf_update(struct zserv
*client
, struct interface
*ifp
,
446 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
448 zclient_create_header(s
, ZEBRA_INTERFACE_VRF_UPDATE
, ifp
->vrf_id
);
450 /* Fill in the ifIndex of the interface and its new VRF (id) */
451 stream_putl(s
, ifp
->ifindex
);
452 stream_putl(s
, vrf_id
);
454 /* Write packet size. */
455 stream_putw_at(s
, 0, stream_get_endp(s
));
457 client
->if_vrfchg_cnt
++;
458 return zebra_server_send_message(client
, s
);
461 /* Add new nbr connected IPv6 address */
462 void nbr_connected_add_ipv6(struct interface
*ifp
, struct in6_addr
*address
)
464 struct nbr_connected
*ifc
;
468 IPV6_ADDR_COPY(&p
.u
.prefix
, address
);
469 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
471 if (!(ifc
= listnode_head(ifp
->nbr_connected
))) {
473 ifc
= nbr_connected_new();
474 ifc
->address
= prefix_new();
476 listnode_add(ifp
->nbr_connected
, ifc
);
479 prefix_copy(ifc
->address
, &p
);
481 zebra_interface_nbr_address_add_update(ifp
, ifc
);
483 if_nbr_ipv6ll_to_ipv4ll_neigh_update(ifp
, address
, 1);
486 void nbr_connected_delete_ipv6(struct interface
*ifp
, struct in6_addr
*address
)
488 struct nbr_connected
*ifc
;
492 IPV6_ADDR_COPY(&p
.u
.prefix
, address
);
493 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
495 ifc
= nbr_connected_check(ifp
, &p
);
499 listnode_delete(ifp
->nbr_connected
, ifc
);
501 zebra_interface_nbr_address_delete_update(ifp
, ifc
);
503 if_nbr_ipv6ll_to_ipv4ll_neigh_update(ifp
, address
, 0);
505 nbr_connected_free(ifc
);
509 * The cmd passed to zsend_interface_update may be ZEBRA_INTERFACE_UP or
510 * ZEBRA_INTERFACE_DOWN.
512 * The ZEBRA_INTERFACE_UP message is sent from the zebra server to
513 * the clients in one of 2 situations:
514 * - an if_up is detected e.g., as a result of an RTM_IFINFO message
515 * - a vty command modifying the bandwidth of an interface is received.
516 * The ZEBRA_INTERFACE_DOWN message is sent when an if_down is detected.
518 int zsend_interface_update(int cmd
, struct zserv
*client
, struct interface
*ifp
)
520 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
522 zclient_create_header(s
, cmd
, ifp
->vrf_id
);
523 zserv_encode_interface(s
, ifp
);
525 if (cmd
== ZEBRA_INTERFACE_UP
)
528 client
->ifdown_cnt
++;
530 return zebra_server_send_message(client
, s
);
533 int zsend_redistribute_route(int cmd
, struct zserv
*client
, struct prefix
*p
,
534 struct prefix
*src_p
, struct route_entry
*re
)
536 struct zapi_route api
;
537 struct zapi_nexthop
*api_nh
;
538 struct nexthop
*nexthop
;
541 memset(&api
, 0, sizeof(api
));
542 api
.vrf_id
= re
->vrf_id
;
544 api
.instance
= re
->instance
;
545 api
.flags
= re
->flags
;
550 SET_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
);
551 memcpy(&api
.src_prefix
, src_p
, sizeof(api
.src_prefix
));
555 if (re
->nexthop_active_num
) {
556 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
557 api
.nexthop_num
= re
->nexthop_active_num
;
559 for (nexthop
= re
->ng
.nexthop
; nexthop
; nexthop
= nexthop
->next
) {
560 if (!CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
563 api_nh
= &api
.nexthops
[count
];
564 api_nh
->vrf_id
= nexthop
->vrf_id
;
565 api_nh
->type
= nexthop
->type
;
566 switch (nexthop
->type
) {
567 case NEXTHOP_TYPE_BLACKHOLE
:
568 api_nh
->bh_type
= nexthop
->bh_type
;
570 case NEXTHOP_TYPE_IPV4
:
571 api_nh
->gate
.ipv4
= nexthop
->gate
.ipv4
;
573 case NEXTHOP_TYPE_IPV4_IFINDEX
:
574 api_nh
->gate
.ipv4
= nexthop
->gate
.ipv4
;
575 api_nh
->ifindex
= nexthop
->ifindex
;
577 case NEXTHOP_TYPE_IFINDEX
:
578 api_nh
->ifindex
= nexthop
->ifindex
;
580 case NEXTHOP_TYPE_IPV6
:
581 api_nh
->gate
.ipv6
= nexthop
->gate
.ipv6
;
583 case NEXTHOP_TYPE_IPV6_IFINDEX
:
584 api_nh
->gate
.ipv6
= nexthop
->gate
.ipv6
;
585 api_nh
->ifindex
= nexthop
->ifindex
;
591 SET_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
);
592 api
.distance
= re
->distance
;
593 SET_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
);
594 api
.metric
= re
->metric
;
596 SET_FLAG(api
.message
, ZAPI_MESSAGE_TAG
);
599 SET_FLAG(api
.message
, ZAPI_MESSAGE_MTU
);
602 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
604 /* Encode route and send. */
605 if (zapi_route_encode(cmd
, s
, &api
) < 0) {
610 if (IS_ZEBRA_DEBUG_SEND
) {
611 char buf_prefix
[PREFIX_STRLEN
];
612 prefix2str(&api
.prefix
, buf_prefix
, sizeof(buf_prefix
));
614 zlog_debug("%s: %s to client %s: type %s, vrf_id %d, p %s",
615 __func__
, zserv_command_string(cmd
),
616 zebra_route_string(client
->proto
),
617 zebra_route_string(api
.type
), api
.vrf_id
,
620 return zebra_server_send_message(client
, s
);
624 * Modified version of zsend_ipv4_nexthop_lookup(): Query unicast rib if
625 * nexthop is not found on mrib. Returns both route metric and protocol
628 static int zsend_ipv4_nexthop_lookup_mrib(struct zserv
*client
,
630 struct route_entry
*re
,
631 struct zebra_vrf
*zvrf
)
636 struct nexthop
*nexthop
;
638 /* Get output stream. */
639 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
642 /* Fill in result. */
643 zclient_create_header(s
, ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB
, zvrf_id(zvrf
));
644 stream_put_in_addr(s
, &addr
);
647 stream_putc(s
, re
->distance
);
648 stream_putl(s
, re
->metric
);
650 nump
= stream_get_endp(
651 s
); /* remember position for nexthop_num */
652 stream_putc(s
, 0); /* reserve room for nexthop_num */
653 /* Only non-recursive routes are elegible to resolve the nexthop
655 * are looking up. Therefore, we will just iterate over the top
656 * chain of nexthops. */
657 for (nexthop
= re
->ng
.nexthop
; nexthop
; nexthop
= nexthop
->next
)
658 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
659 num
+= zserv_encode_nexthop(s
, nexthop
);
661 stream_putc_at(s
, nump
, num
); /* store nexthop_num */
663 stream_putc(s
, 0); /* distance */
664 stream_putl(s
, 0); /* metric */
665 stream_putc(s
, 0); /* nexthop_num */
668 stream_putw_at(s
, 0, stream_get_endp(s
));
670 return zebra_server_send_message(client
, s
);
673 int zsend_route_notify_owner(struct route_entry
*re
, struct prefix
*p
,
674 enum zapi_route_notify_owner note
)
676 struct zserv
*client
;
680 client
= zebra_find_client(re
->type
, re
->instance
);
681 if (!client
|| !client
->notify_owner
) {
682 if (IS_ZEBRA_DEBUG_PACKET
) {
683 char buff
[PREFIX_STRLEN
];
686 "Not Notifying Owner: %u about prefix %s(%u) %d vrf: %u",
687 re
->type
, prefix2str(p
, buff
, sizeof(buff
)),
688 re
->table
, note
, re
->vrf_id
);
693 if (IS_ZEBRA_DEBUG_PACKET
) {
694 char buff
[PREFIX_STRLEN
];
696 zlog_debug("Notifying Owner: %u about prefix %s(%u) %d vrf: %u",
697 re
->type
, prefix2str(p
, buff
, sizeof(buff
)),
698 re
->table
, note
, re
->vrf_id
);
701 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
704 zclient_create_header(s
, ZEBRA_ROUTE_NOTIFY_OWNER
, re
->vrf_id
);
706 stream_put(s
, ¬e
, sizeof(note
));
708 stream_putc(s
, p
->family
);
710 blen
= prefix_blen(p
);
711 stream_putc(s
, p
->prefixlen
);
712 stream_put(s
, &p
->u
.prefix
, blen
);
714 stream_putl(s
, re
->table
);
716 stream_putw_at(s
, 0, stream_get_endp(s
));
718 return zebra_server_send_message(client
, s
);
721 void zsend_rule_notify_owner(struct zebra_pbr_rule
*rule
,
722 enum zapi_rule_notify_owner note
)
724 struct listnode
*node
;
725 struct zserv
*client
;
728 if (IS_ZEBRA_DEBUG_PACKET
)
729 zlog_debug("%s: Notifying %u", __PRETTY_FUNCTION__
,
732 for (ALL_LIST_ELEMENTS_RO(zebrad
.client_list
, node
, client
)) {
733 if (rule
->sock
== client
->sock
)
740 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
742 zclient_create_header(s
, ZEBRA_RULE_NOTIFY_OWNER
, VRF_DEFAULT
);
743 stream_put(s
, ¬e
, sizeof(note
));
744 stream_putl(s
, rule
->seq
);
745 stream_putl(s
, rule
->priority
);
746 stream_putl(s
, rule
->unique
);
748 stream_putl(s
, rule
->ifp
->ifindex
);
752 stream_putw_at(s
, 0, stream_get_endp(s
));
754 zebra_server_send_message(client
, s
);
757 void zsend_ipset_notify_owner(struct zebra_pbr_ipset
*ipset
,
758 enum zapi_ipset_notify_owner note
)
760 struct listnode
*node
;
761 struct zserv
*client
;
764 if (IS_ZEBRA_DEBUG_PACKET
)
765 zlog_debug("%s: Notifying %u", __PRETTY_FUNCTION__
,
768 for (ALL_LIST_ELEMENTS_RO(zebrad
.client_list
, node
, client
)) {
769 if (ipset
->sock
== client
->sock
)
776 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
778 zclient_create_header(s
, ZEBRA_IPSET_NOTIFY_OWNER
, VRF_DEFAULT
);
779 stream_put(s
, ¬e
, sizeof(note
));
780 stream_putl(s
, ipset
->unique
);
781 stream_put(s
, ipset
->ipset_name
, ZEBRA_IPSET_NAME_SIZE
);
782 stream_putw_at(s
, 0, stream_get_endp(s
));
784 zebra_server_send_message(client
, s
);
787 void zsend_ipset_entry_notify_owner(
788 struct zebra_pbr_ipset_entry
*ipset
,
789 enum zapi_ipset_entry_notify_owner note
)
791 struct listnode
*node
;
792 struct zserv
*client
;
795 if (IS_ZEBRA_DEBUG_PACKET
)
796 zlog_debug("%s: Notifying %u", __PRETTY_FUNCTION__
,
799 for (ALL_LIST_ELEMENTS_RO(zebrad
.client_list
, node
, client
)) {
800 if (ipset
->sock
== client
->sock
)
807 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
809 zclient_create_header(s
, ZEBRA_IPSET_ENTRY_NOTIFY_OWNER
,
811 stream_put(s
, ¬e
, sizeof(note
));
812 stream_putl(s
, ipset
->unique
);
813 stream_put(s
, ipset
->backpointer
->ipset_name
,
814 ZEBRA_IPSET_NAME_SIZE
);
815 stream_putw_at(s
, 0, stream_get_endp(s
));
817 zebra_server_send_message(client
, s
);
820 /* Router-id is updated. Send ZEBRA_ROUTER_ID_ADD to client. */
821 int zsend_router_id_update(struct zserv
*client
, struct prefix
*p
,
826 /* Check this client need interface information. */
827 if (!vrf_bitmap_check(client
->ridinfo
, vrf_id
))
830 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
833 zclient_create_header(s
, ZEBRA_ROUTER_ID_UPDATE
, vrf_id
);
835 /* Prefix information. */
836 stream_putc(s
, p
->family
);
837 blen
= prefix_blen(p
);
838 stream_put(s
, &p
->u
.prefix
, blen
);
839 stream_putc(s
, p
->prefixlen
);
841 /* Write packet size. */
842 stream_putw_at(s
, 0, stream_get_endp(s
));
844 return zebra_server_send_message(client
, s
);
848 * Function used by Zebra to send a PW status update to LDP daemon
850 int zsend_pw_update(struct zserv
*client
, struct zebra_pw
*pw
)
852 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
854 zclient_create_header(s
, ZEBRA_PW_STATUS_UPDATE
, pw
->vrf_id
);
855 stream_write(s
, pw
->ifname
, IF_NAMESIZE
);
856 stream_putl(s
, pw
->ifindex
);
857 stream_putl(s
, pw
->status
);
859 /* Put length at the first point of the stream. */
860 stream_putw_at(s
, 0, stream_get_endp(s
));
862 return zebra_server_send_message(client
, s
);
865 /* Send response to a get label chunk request to client */
866 static int zsend_assign_label_chunk_response(struct zserv
*client
,
868 struct label_manager_chunk
*lmc
)
871 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
873 zclient_create_header(s
, ZEBRA_GET_LABEL_CHUNK
, vrf_id
);
877 stream_putc(s
, lmc
->keep
);
878 /* start and end labels */
879 stream_putl(s
, lmc
->start
);
880 stream_putl(s
, lmc
->end
);
883 /* Write packet size. */
884 stream_putw_at(s
, 0, stream_get_endp(s
));
886 ret
= writen(client
->sock
, s
->data
, stream_get_endp(s
));
891 /* Send response to a label manager connect request to client */
892 static int zsend_label_manager_connect_response(struct zserv
*client
,
894 unsigned short result
)
897 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
899 zclient_create_header(s
, ZEBRA_LABEL_MANAGER_CONNECT
, vrf_id
);
902 stream_putc(s
, result
);
904 /* Write packet size. */
905 stream_putw_at(s
, 0, stream_get_endp(s
));
907 ret
= writen(client
->sock
, s
->data
, stream_get_endp(s
));
913 /* Inbound message handling ------------------------------------------------ */
916 [ZEBRA_NEXTHOP_REGISTER
] = RNH_NEXTHOP_TYPE
,
917 [ZEBRA_NEXTHOP_UNREGISTER
] = RNH_NEXTHOP_TYPE
,
918 [ZEBRA_IMPORT_ROUTE_REGISTER
] = RNH_IMPORT_CHECK_TYPE
,
919 [ZEBRA_IMPORT_ROUTE_UNREGISTER
] = RNH_IMPORT_CHECK_TYPE
,
922 /* Nexthop register */
923 static void zread_rnh_register(ZAPI_HANDLER_ARGS
)
928 unsigned short l
= 0;
930 uint16_t type
= cmd2type
[hdr
->command
];
932 if (IS_ZEBRA_DEBUG_NHT
)
934 "rnh_register msg from client %s: hdr->length=%d, type=%s vrf=%u\n",
935 zebra_route_string(client
->proto
), hdr
->length
,
936 (type
== RNH_NEXTHOP_TYPE
) ? "nexthop" : "route",
941 client
->nh_reg_time
= monotime(NULL
);
943 while (l
< hdr
->length
) {
944 STREAM_GETC(s
, flags
);
945 STREAM_GETW(s
, p
.family
);
946 STREAM_GETC(s
, p
.prefixlen
);
948 if (p
.family
== AF_INET
) {
949 if (p
.prefixlen
> IPV4_MAX_BITLEN
) {
951 "%s: Specified prefix hdr->length %d is too large for a v4 address",
952 __PRETTY_FUNCTION__
, p
.prefixlen
);
955 STREAM_GET(&p
.u
.prefix4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
956 l
+= IPV4_MAX_BYTELEN
;
957 } else if (p
.family
== AF_INET6
) {
958 if (p
.prefixlen
> IPV6_MAX_BITLEN
) {
960 "%s: Specified prefix hdr->length %d is to large for a v6 address",
961 __PRETTY_FUNCTION__
, p
.prefixlen
);
964 STREAM_GET(&p
.u
.prefix6
, s
, IPV6_MAX_BYTELEN
);
965 l
+= IPV6_MAX_BYTELEN
;
968 "rnh_register: Received unknown family type %d\n",
972 rnh
= zebra_add_rnh(&p
, zvrf_id(zvrf
), type
);
973 if (type
== RNH_NEXTHOP_TYPE
) {
975 && !CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
))
976 SET_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
);
978 && CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
))
979 UNSET_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
);
980 } else if (type
== RNH_IMPORT_CHECK_TYPE
) {
982 && !CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
))
983 SET_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
);
985 && CHECK_FLAG(rnh
->flags
,
986 ZEBRA_NHT_EXACT_MATCH
))
987 UNSET_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
);
990 zebra_add_rnh_client(rnh
, client
, type
, zvrf_id(zvrf
));
991 /* Anything not AF_INET/INET6 has been filtered out above */
992 zebra_evaluate_rnh(zvrf_id(zvrf
), p
.family
, 1, type
, &p
);
999 /* Nexthop register */
1000 static void zread_rnh_unregister(ZAPI_HANDLER_ARGS
)
1005 unsigned short l
= 0;
1006 uint16_t type
= cmd2type
[hdr
->command
];
1008 if (IS_ZEBRA_DEBUG_NHT
)
1010 "rnh_unregister msg from client %s: hdr->length=%d vrf: %u\n",
1011 zebra_route_string(client
->proto
), hdr
->length
,
1016 while (l
< hdr
->length
) {
1019 STREAM_GETC(s
, flags
);
1021 goto stream_failure
;
1023 STREAM_GETW(s
, p
.family
);
1024 STREAM_GETC(s
, p
.prefixlen
);
1026 if (p
.family
== AF_INET
) {
1027 if (p
.prefixlen
> IPV4_MAX_BITLEN
) {
1029 "%s: Specified prefix hdr->length %d is to large for a v4 address",
1030 __PRETTY_FUNCTION__
, p
.prefixlen
);
1033 STREAM_GET(&p
.u
.prefix4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
1034 l
+= IPV4_MAX_BYTELEN
;
1035 } else if (p
.family
== AF_INET6
) {
1036 if (p
.prefixlen
> IPV6_MAX_BITLEN
) {
1038 "%s: Specified prefix hdr->length %d is to large for a v6 address",
1039 __PRETTY_FUNCTION__
, p
.prefixlen
);
1042 STREAM_GET(&p
.u
.prefix6
, s
, IPV6_MAX_BYTELEN
);
1043 l
+= IPV6_MAX_BYTELEN
;
1046 "rnh_register: Received unknown family type %d\n",
1050 rnh
= zebra_lookup_rnh(&p
, zvrf_id(zvrf
), type
);
1052 client
->nh_dereg_time
= monotime(NULL
);
1053 zebra_remove_rnh_client(rnh
, client
, type
);
1060 #define ZEBRA_MIN_FEC_LENGTH 5
1063 static void zread_fec_register(ZAPI_HANDLER_ARGS
)
1066 unsigned short l
= 0;
1069 uint32_t label_index
= MPLS_INVALID_LABEL_INDEX
;
1072 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1074 return; // unexpected
1077 * The minimum amount of data that can be sent for one fec
1080 if (hdr
->length
< ZEBRA_MIN_FEC_LENGTH
) {
1082 "fec_register: Received a fec register of hdr->length %d, it is of insufficient size to properly decode",
1087 while (l
< hdr
->length
) {
1088 STREAM_GETW(s
, flags
);
1089 memset(&p
, 0, sizeof(p
));
1090 STREAM_GETW(s
, p
.family
);
1091 if (p
.family
!= AF_INET
&& p
.family
!= AF_INET6
) {
1093 "fec_register: Received unknown family type %d\n",
1097 STREAM_GETC(s
, p
.prefixlen
);
1098 if ((p
.family
== AF_INET
&& p
.prefixlen
> IPV4_MAX_BITLEN
)
1099 || (p
.family
== AF_INET6
1100 && p
.prefixlen
> IPV6_MAX_BITLEN
)) {
1102 "%s: Specified prefix hdr->length: %d is to long for %d",
1103 __PRETTY_FUNCTION__
, p
.prefixlen
, p
.family
);
1107 STREAM_GET(&p
.u
.prefix
, s
, PSIZE(p
.prefixlen
));
1108 l
+= PSIZE(p
.prefixlen
);
1109 if (flags
& ZEBRA_FEC_REGISTER_LABEL_INDEX
) {
1110 STREAM_GETL(s
, label_index
);
1113 label_index
= MPLS_INVALID_LABEL_INDEX
;
1114 zebra_mpls_fec_register(zvrf
, &p
, label_index
, client
);
1121 /* FEC unregister */
1122 static void zread_fec_unregister(ZAPI_HANDLER_ARGS
)
1125 unsigned short l
= 0;
1130 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1132 return; // unexpected
1135 * The minimum amount of data that can be sent for one
1136 * fec unregistration
1138 if (hdr
->length
< ZEBRA_MIN_FEC_LENGTH
) {
1140 "fec_unregister: Received a fec unregister of hdr->length %d, it is of insufficient size to properly decode",
1145 while (l
< hdr
->length
) {
1146 STREAM_GETW(s
, flags
);
1148 goto stream_failure
;
1150 memset(&p
, 0, sizeof(p
));
1151 STREAM_GETW(s
, p
.family
);
1152 if (p
.family
!= AF_INET
&& p
.family
!= AF_INET6
) {
1154 "fec_unregister: Received unknown family type %d\n",
1158 STREAM_GETC(s
, p
.prefixlen
);
1159 if ((p
.family
== AF_INET
&& p
.prefixlen
> IPV4_MAX_BITLEN
)
1160 || (p
.family
== AF_INET6
1161 && p
.prefixlen
> IPV6_MAX_BITLEN
)) {
1163 "%s: Received prefix hdr->length %d which is greater than %d can support",
1164 __PRETTY_FUNCTION__
, p
.prefixlen
, p
.family
);
1168 STREAM_GET(&p
.u
.prefix
, s
, PSIZE(p
.prefixlen
));
1169 l
+= PSIZE(p
.prefixlen
);
1170 zebra_mpls_fec_unregister(zvrf
, &p
, client
);
1179 * Register zebra server interface information.
1180 * Send current all interface and address information.
1182 static void zread_interface_add(ZAPI_HANDLER_ARGS
)
1185 struct interface
*ifp
;
1187 /* Interface information is needed. */
1188 vrf_bitmap_set(client
->ifinfo
, zvrf_id(zvrf
));
1190 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
1191 FOR_ALL_INTERFACES (vrf
, ifp
) {
1192 /* Skip pseudo interface. */
1193 if (!CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_ACTIVE
))
1196 zsend_interface_add(client
, ifp
);
1197 zsend_interface_addresses(client
, ifp
);
1202 /* Unregister zebra server interface information. */
1203 static void zread_interface_delete(ZAPI_HANDLER_ARGS
)
1205 vrf_bitmap_unset(client
->ifinfo
, zvrf_id(zvrf
));
1208 void zserv_nexthop_num_warn(const char *caller
, const struct prefix
*p
,
1209 const unsigned int nexthop_num
)
1211 if (nexthop_num
> multipath_num
) {
1212 char buff
[PREFIX2STR_BUFFER
];
1213 prefix2str(p
, buff
, sizeof(buff
));
1215 "%s: Prefix %s has %d nexthops, but we can only use the first %d",
1216 caller
, buff
, nexthop_num
, multipath_num
);
1220 static void zread_route_add(ZAPI_HANDLER_ARGS
)
1223 struct zapi_route api
;
1224 struct zapi_nexthop
*api_nh
;
1226 struct prefix_ipv6
*src_p
= NULL
;
1227 struct route_entry
*re
;
1228 struct nexthop
*nexthop
= NULL
;
1230 vrf_id_t vrf_id
= 0;
1231 struct ipaddr vtep_ip
;
1234 if (zapi_route_decode(s
, &api
) < 0) {
1235 if (IS_ZEBRA_DEBUG_RECV
)
1236 zlog_debug("%s: Unable to decode zapi_route sent",
1237 __PRETTY_FUNCTION__
);
1241 if (IS_ZEBRA_DEBUG_RECV
) {
1242 char buf_prefix
[PREFIX_STRLEN
];
1243 prefix2str(&api
.prefix
, buf_prefix
, sizeof(buf_prefix
));
1244 zlog_debug("%s: p=%s, ZAPI_MESSAGE_LABEL: %sset, flags=0x%x",
1245 __func__
, buf_prefix
,
1246 (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_LABEL
) ? ""
1251 /* Allocate new route. */
1252 vrf_id
= zvrf_id(zvrf
);
1253 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
1254 re
->type
= api
.type
;
1255 re
->instance
= api
.instance
;
1256 re
->flags
= api
.flags
;
1257 re
->uptime
= time(NULL
);
1258 re
->vrf_id
= vrf_id
;
1259 if (api
.tableid
&& vrf_id
== VRF_DEFAULT
)
1260 re
->table
= api
.tableid
;
1262 re
->table
= zvrf
->table_id
;
1265 * TBD should _all_ of the nexthop add operations use
1266 * api_nh->vrf_id instead of re->vrf_id ? I only changed
1267 * for cases NEXTHOP_TYPE_IPV4 and NEXTHOP_TYPE_IPV6.
1269 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
)) {
1270 for (i
= 0; i
< api
.nexthop_num
; i
++) {
1271 api_nh
= &api
.nexthops
[i
];
1272 ifindex_t ifindex
= 0;
1274 if (IS_ZEBRA_DEBUG_RECV
) {
1275 zlog_debug("nh type %d", api_nh
->type
);
1278 switch (api_nh
->type
) {
1279 case NEXTHOP_TYPE_IFINDEX
:
1280 nexthop
= route_entry_nexthop_ifindex_add(
1281 re
, api_nh
->ifindex
, api_nh
->vrf_id
);
1283 case NEXTHOP_TYPE_IPV4
:
1284 if (IS_ZEBRA_DEBUG_RECV
) {
1285 char nhbuf
[INET6_ADDRSTRLEN
] = {0};
1286 inet_ntop(AF_INET
, &api_nh
->gate
.ipv4
,
1287 nhbuf
, INET6_ADDRSTRLEN
);
1288 zlog_debug("%s: nh=%s, vrf_id=%d",
1292 nexthop
= route_entry_nexthop_ipv4_add(
1293 re
, &api_nh
->gate
.ipv4
, NULL
,
1296 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1298 memset(&vtep_ip
, 0, sizeof(struct ipaddr
));
1299 if (CHECK_FLAG(api
.flags
,
1300 ZEBRA_FLAG_EVPN_ROUTE
)) {
1301 ifindex
= get_l3vni_svi_ifindex(vrf_id
);
1303 ifindex
= api_nh
->ifindex
;
1306 if (IS_ZEBRA_DEBUG_RECV
) {
1307 char nhbuf
[INET6_ADDRSTRLEN
] = {0};
1308 inet_ntop(AF_INET
, &api_nh
->gate
.ipv4
,
1309 nhbuf
, INET6_ADDRSTRLEN
);
1311 "%s: nh=%s, vrf_id=%d (re->vrf_id=%d), ifindex=%d",
1312 __func__
, nhbuf
, api_nh
->vrf_id
,
1313 re
->vrf_id
, ifindex
);
1315 nexthop
= route_entry_nexthop_ipv4_ifindex_add(
1316 re
, &api_nh
->gate
.ipv4
, NULL
, ifindex
,
1319 /* if this an EVPN route entry,
1320 * program the nh as neigh
1322 if (CHECK_FLAG(api
.flags
,
1323 ZEBRA_FLAG_EVPN_ROUTE
)) {
1324 SET_FLAG(nexthop
->flags
,
1325 NEXTHOP_FLAG_EVPN_RVTEP
);
1326 vtep_ip
.ipa_type
= IPADDR_V4
;
1327 memcpy(&(vtep_ip
.ipaddr_v4
),
1328 &(api_nh
->gate
.ipv4
),
1329 sizeof(struct in_addr
));
1330 zebra_vxlan_evpn_vrf_route_add(
1331 vrf_id
, &api
.rmac
, &vtep_ip
,
1335 case NEXTHOP_TYPE_IPV6
:
1336 nexthop
= route_entry_nexthop_ipv6_add(
1337 re
, &api_nh
->gate
.ipv6
, api_nh
->vrf_id
);
1339 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1340 memset(&vtep_ip
, 0, sizeof(struct ipaddr
));
1341 if (CHECK_FLAG(api
.flags
,
1342 ZEBRA_FLAG_EVPN_ROUTE
)) {
1344 get_l3vni_svi_ifindex(vrf_id
);
1346 ifindex
= api_nh
->ifindex
;
1349 nexthop
= route_entry_nexthop_ipv6_ifindex_add(
1350 re
, &api_nh
->gate
.ipv6
, ifindex
,
1353 /* if this an EVPN route entry,
1354 * program the nh as neigh
1356 if (CHECK_FLAG(api
.flags
,
1357 ZEBRA_FLAG_EVPN_ROUTE
)) {
1358 SET_FLAG(nexthop
->flags
,
1359 NEXTHOP_FLAG_EVPN_RVTEP
);
1360 vtep_ip
.ipa_type
= IPADDR_V6
;
1361 memcpy(&vtep_ip
.ipaddr_v6
,
1362 &(api_nh
->gate
.ipv6
),
1363 sizeof(struct in6_addr
));
1364 zebra_vxlan_evpn_vrf_route_add(
1371 case NEXTHOP_TYPE_BLACKHOLE
:
1372 nexthop
= route_entry_nexthop_blackhole_add(
1373 re
, api_nh
->bh_type
);
1379 "%s: Nexthops Specified: %d but we failed to properly create one",
1380 __PRETTY_FUNCTION__
, api
.nexthop_num
);
1381 nexthops_free(re
->ng
.nexthop
);
1382 XFREE(MTYPE_RE
, re
);
1385 /* MPLS labels for BGP-LU or Segment Routing */
1386 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_LABEL
)
1387 && api_nh
->type
!= NEXTHOP_TYPE_IFINDEX
1388 && api_nh
->type
!= NEXTHOP_TYPE_BLACKHOLE
) {
1389 enum lsp_types_t label_type
;
1392 lsp_type_from_re_type(client
->proto
);
1394 if (IS_ZEBRA_DEBUG_RECV
) {
1396 "%s: adding %d labels of type %d (1st=%u)",
1397 __func__
, api_nh
->label_num
,
1398 label_type
, api_nh
->labels
[0]);
1401 nexthop_add_labels(nexthop
, label_type
,
1403 &api_nh
->labels
[0]);
1408 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
))
1409 re
->distance
= api
.distance
;
1410 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
))
1411 re
->metric
= api
.metric
;
1412 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_TAG
))
1414 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_MTU
))
1417 afi
= family2afi(api
.prefix
.family
);
1418 if (afi
!= AFI_IP6
&& CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
)) {
1419 zlog_warn("%s: Received SRC Prefix but afi is not v6",
1420 __PRETTY_FUNCTION__
);
1421 nexthops_free(re
->ng
.nexthop
);
1422 XFREE(MTYPE_RE
, re
);
1425 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
))
1426 src_p
= &api
.src_prefix
;
1428 ret
= rib_add_multipath(afi
, api
.safi
, &api
.prefix
, src_p
, re
);
1431 switch (api
.prefix
.family
) {
1434 client
->v4_route_add_cnt
++;
1436 client
->v4_route_upd8_cnt
++;
1440 client
->v6_route_add_cnt
++;
1442 client
->v6_route_upd8_cnt
++;
1447 static void zread_route_del(ZAPI_HANDLER_ARGS
)
1450 struct zapi_route api
;
1452 struct prefix_ipv6
*src_p
= NULL
;
1456 if (zapi_route_decode(s
, &api
) < 0)
1459 afi
= family2afi(api
.prefix
.family
);
1460 if (afi
!= AFI_IP6
&& CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
)) {
1461 zlog_warn("%s: Received a src prefix while afi is not v6",
1462 __PRETTY_FUNCTION__
);
1465 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
))
1466 src_p
= &api
.src_prefix
;
1468 if (api
.vrf_id
== VRF_DEFAULT
&& api
.tableid
!= 0)
1469 table_id
= api
.tableid
;
1471 table_id
= zvrf
->table_id
;
1473 rib_delete(afi
, api
.safi
, zvrf_id(zvrf
), api
.type
, api
.instance
,
1474 api
.flags
, &api
.prefix
, src_p
, NULL
, table_id
,
1475 api
.metric
, false, &api
.rmac
);
1478 switch (api
.prefix
.family
) {
1480 client
->v4_route_del_cnt
++;
1483 client
->v6_route_del_cnt
++;
1488 /* This function support multiple nexthop. */
1490 * Parse the ZEBRA_IPV4_ROUTE_ADD sent from client. Update re and
1493 static void zread_ipv4_add(ZAPI_HANDLER_ARGS
)
1496 struct route_entry
*re
;
1499 struct in_addr nhop_addr
;
1500 uint8_t nexthop_num
;
1501 uint8_t nexthop_type
;
1506 enum lsp_types_t label_type
= ZEBRA_LSP_NONE
;
1508 struct nexthop
*nexthop
;
1509 enum blackhole_type bh_type
= BLACKHOLE_NULL
;
1511 /* Get input stream. */
1514 /* Allocate new re. */
1515 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
1517 /* Type, flags, message. */
1518 STREAM_GETC(s
, re
->type
);
1519 if (re
->type
> ZEBRA_ROUTE_MAX
) {
1520 zlog_warn("%s: Specified route type %d is not a legal value\n",
1521 __PRETTY_FUNCTION__
, re
->type
);
1522 XFREE(MTYPE_RE
, re
);
1525 STREAM_GETW(s
, re
->instance
);
1526 STREAM_GETL(s
, re
->flags
);
1527 STREAM_GETC(s
, message
);
1528 STREAM_GETW(s
, safi
);
1529 re
->uptime
= time(NULL
);
1532 memset(&p
, 0, sizeof(struct prefix_ipv4
));
1534 STREAM_GETC(s
, p
.prefixlen
);
1535 if (p
.prefixlen
> IPV4_MAX_BITLEN
) {
1537 "%s: Specified prefix length %d is greater than what v4 can be",
1538 __PRETTY_FUNCTION__
, p
.prefixlen
);
1539 XFREE(MTYPE_RE
, re
);
1542 STREAM_GET(&p
.u
.prefix4
, s
, PSIZE(p
.prefixlen
));
1545 re
->vrf_id
= zvrf_id(zvrf
);
1547 /* Nexthop parse. */
1548 if (CHECK_FLAG(message
, ZAPI_MESSAGE_NEXTHOP
)) {
1549 STREAM_GETC(s
, nexthop_num
);
1550 zserv_nexthop_num_warn(__func__
, (const struct prefix
*)&p
,
1553 if (CHECK_FLAG(message
, ZAPI_MESSAGE_LABEL
))
1554 label_type
= lsp_type_from_re_type(client
->proto
);
1556 for (i
= 0; i
< nexthop_num
; i
++) {
1557 STREAM_GETC(s
, nexthop_type
);
1559 switch (nexthop_type
) {
1560 case NEXTHOP_TYPE_IFINDEX
:
1561 STREAM_GETL(s
, ifindex
);
1562 route_entry_nexthop_ifindex_add(re
, ifindex
,
1565 case NEXTHOP_TYPE_IPV4
:
1566 STREAM_GET(&nhop_addr
.s_addr
, s
,
1568 nexthop
= route_entry_nexthop_ipv4_add(
1569 re
, &nhop_addr
, NULL
, re
->vrf_id
);
1570 /* For labeled-unicast, each nexthop is followed
1572 if (CHECK_FLAG(message
, ZAPI_MESSAGE_LABEL
)) {
1573 STREAM_GETL(s
, label
);
1574 nexthop_add_labels(nexthop
, label_type
,
1578 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1579 STREAM_GET(&nhop_addr
.s_addr
, s
,
1581 STREAM_GETL(s
, ifindex
);
1582 route_entry_nexthop_ipv4_ifindex_add(
1583 re
, &nhop_addr
, NULL
, ifindex
,
1586 case NEXTHOP_TYPE_IPV6
:
1588 "%s: Please use ZEBRA_ROUTE_ADD if you want to pass v6 nexthops",
1589 __PRETTY_FUNCTION__
);
1590 nexthops_free(re
->ng
.nexthop
);
1591 XFREE(MTYPE_RE
, re
);
1594 case NEXTHOP_TYPE_BLACKHOLE
:
1595 route_entry_nexthop_blackhole_add(re
, bh_type
);
1599 "%s: Specified nexthop type: %d does not exist",
1600 __PRETTY_FUNCTION__
, nexthop_type
);
1601 nexthops_free(re
->ng
.nexthop
);
1602 XFREE(MTYPE_RE
, re
);
1609 if (CHECK_FLAG(message
, ZAPI_MESSAGE_DISTANCE
))
1610 STREAM_GETC(s
, re
->distance
);
1613 if (CHECK_FLAG(message
, ZAPI_MESSAGE_METRIC
))
1614 STREAM_GETL(s
, re
->metric
);
1617 if (CHECK_FLAG(message
, ZAPI_MESSAGE_TAG
))
1618 STREAM_GETL(s
, re
->tag
);
1622 if (CHECK_FLAG(message
, ZAPI_MESSAGE_MTU
))
1623 STREAM_GETL(s
, re
->mtu
);
1628 re
->table
= zvrf
->table_id
;
1630 ret
= rib_add_multipath(AFI_IP
, safi
, &p
, NULL
, re
);
1634 client
->v4_route_add_cnt
++;
1636 client
->v4_route_upd8_cnt
++;
1641 nexthops_free(re
->ng
.nexthop
);
1642 XFREE(MTYPE_RE
, re
);
1645 /* Zebra server IPv4 prefix delete function. */
1646 static void zread_ipv4_delete(ZAPI_HANDLER_ARGS
)
1649 struct zapi_ipv4 api
;
1655 /* Type, flags, message. */
1656 STREAM_GETC(s
, api
.type
);
1657 STREAM_GETW(s
, api
.instance
);
1658 STREAM_GETL(s
, api
.flags
);
1659 STREAM_GETC(s
, api
.message
);
1660 STREAM_GETW(s
, api
.safi
);
1663 memset(&p
, 0, sizeof(struct prefix
));
1665 STREAM_GETC(s
, p
.prefixlen
);
1666 if (p
.prefixlen
> IPV4_MAX_BITLEN
) {
1667 zlog_warn("%s: Passed in prefixlen %d is impossible",
1668 __PRETTY_FUNCTION__
, p
.prefixlen
);
1671 STREAM_GET(&p
.u
.prefix4
, s
, PSIZE(p
.prefixlen
));
1673 table_id
= zvrf
->table_id
;
1675 rib_delete(AFI_IP
, api
.safi
, zvrf_id(zvrf
), api
.type
, api
.instance
,
1676 api
.flags
, &p
, NULL
, NULL
, table_id
, 0, false, NULL
);
1677 client
->v4_route_del_cnt
++;
1683 /* MRIB Nexthop lookup for IPv4. */
1684 static void zread_ipv4_nexthop_lookup_mrib(ZAPI_HANDLER_ARGS
)
1686 struct in_addr addr
;
1687 struct route_entry
*re
;
1689 STREAM_GET(&addr
.s_addr
, msg
, IPV4_MAX_BYTELEN
);
1690 re
= rib_match_ipv4_multicast(zvrf_id(zvrf
), addr
, NULL
);
1691 zsend_ipv4_nexthop_lookup_mrib(client
, addr
, re
, zvrf
);
1697 /* Zebra server IPv6 prefix add function. */
1698 static void zread_ipv4_route_ipv6_nexthop_add(ZAPI_HANDLER_ARGS
)
1702 struct in6_addr nhop_addr
;
1703 struct route_entry
*re
;
1705 uint8_t nexthop_num
;
1706 uint8_t nexthop_type
;
1709 static struct in6_addr nexthops
[MULTIPATH_NUM
];
1710 static unsigned int ifindices
[MULTIPATH_NUM
];
1712 static mpls_label_t labels
[MULTIPATH_NUM
];
1713 enum lsp_types_t label_type
= ZEBRA_LSP_NONE
;
1715 struct nexthop
*nexthop
;
1716 enum blackhole_type bh_type
= BLACKHOLE_NULL
;
1718 /* Get input stream. */
1721 memset(&nhop_addr
, 0, sizeof(struct in6_addr
));
1723 /* Allocate new re. */
1724 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
1726 /* Type, flags, message. */
1727 STREAM_GETC(s
, re
->type
);
1728 if (re
->type
> ZEBRA_ROUTE_MAX
) {
1729 zlog_warn("%s: Specified route type: %d is not a legal value\n",
1730 __PRETTY_FUNCTION__
, re
->type
);
1731 XFREE(MTYPE_RE
, re
);
1734 STREAM_GETW(s
, re
->instance
);
1735 STREAM_GETL(s
, re
->flags
);
1736 STREAM_GETC(s
, message
);
1737 STREAM_GETW(s
, safi
);
1738 re
->uptime
= time(NULL
);
1741 memset(&p
, 0, sizeof(struct prefix_ipv4
));
1743 STREAM_GETC(s
, p
.prefixlen
);
1744 if (p
.prefixlen
> IPV4_MAX_BITLEN
) {
1746 "%s: Prefix Length %d is greater than what a v4 address can use",
1747 __PRETTY_FUNCTION__
, p
.prefixlen
);
1748 XFREE(MTYPE_RE
, re
);
1751 STREAM_GET(&p
.u
.prefix4
, s
, PSIZE(p
.prefixlen
));
1754 re
->vrf_id
= zvrf_id(zvrf
);
1756 /* We need to give nh-addr, nh-ifindex with the same next-hop object
1757 * to the re to ensure that IPv6 multipathing works; need to coalesce
1758 * these. Clients should send the same number of paired set of
1759 * next-hop-addr/next-hop-ifindices. */
1760 if (CHECK_FLAG(message
, ZAPI_MESSAGE_NEXTHOP
)) {
1761 unsigned int nh_count
= 0;
1762 unsigned int if_count
= 0;
1763 unsigned int max_nh_if
= 0;
1765 STREAM_GETC(s
, nexthop_num
);
1766 zserv_nexthop_num_warn(__func__
, (const struct prefix
*)&p
,
1769 if (CHECK_FLAG(message
, ZAPI_MESSAGE_LABEL
))
1770 label_type
= lsp_type_from_re_type(client
->proto
);
1772 for (i
= 0; i
< nexthop_num
; i
++) {
1773 STREAM_GETC(s
, nexthop_type
);
1775 switch (nexthop_type
) {
1776 case NEXTHOP_TYPE_IPV6
:
1777 STREAM_GET(&nhop_addr
, s
, 16);
1778 if (nh_count
< MULTIPATH_NUM
) {
1779 /* For labeled-unicast, each nexthop is
1780 * followed by label. */
1781 if (CHECK_FLAG(message
,
1782 ZAPI_MESSAGE_LABEL
)) {
1783 STREAM_GETL(s
, label
);
1784 labels
[nh_count
] = label
;
1786 nexthops
[nh_count
] = nhop_addr
;
1790 case NEXTHOP_TYPE_IFINDEX
:
1791 if (if_count
< multipath_num
) {
1792 STREAM_GETL(s
, ifindices
[if_count
++]);
1795 case NEXTHOP_TYPE_BLACKHOLE
:
1796 route_entry_nexthop_blackhole_add(re
, bh_type
);
1800 "%s: Please use ZEBRA_ROUTE_ADD if you want to pass non v6 nexthops",
1801 __PRETTY_FUNCTION__
);
1802 nexthops_free(re
->ng
.nexthop
);
1803 XFREE(MTYPE_RE
, re
);
1808 max_nh_if
= (nh_count
> if_count
) ? nh_count
: if_count
;
1809 for (i
= 0; i
< max_nh_if
; i
++) {
1811 && !IN6_IS_ADDR_UNSPECIFIED(&nexthops
[i
])) {
1812 if ((i
< if_count
) && ifindices
[i
])
1814 route_entry_nexthop_ipv6_ifindex_add(
1819 nexthop
= route_entry_nexthop_ipv6_add(
1820 re
, &nexthops
[i
], re
->vrf_id
);
1822 if (CHECK_FLAG(message
, ZAPI_MESSAGE_LABEL
))
1823 nexthop_add_labels(nexthop
, label_type
,
1826 if ((i
< if_count
) && ifindices
[i
])
1827 route_entry_nexthop_ifindex_add(
1828 re
, ifindices
[i
], re
->vrf_id
);
1834 if (CHECK_FLAG(message
, ZAPI_MESSAGE_DISTANCE
))
1835 STREAM_GETC(s
, re
->distance
);
1838 if (CHECK_FLAG(message
, ZAPI_MESSAGE_METRIC
))
1839 STREAM_GETL(s
, re
->metric
);
1842 if (CHECK_FLAG(message
, ZAPI_MESSAGE_TAG
))
1843 STREAM_GETL(s
, re
->tag
);
1847 if (CHECK_FLAG(message
, ZAPI_MESSAGE_MTU
))
1848 STREAM_GETL(s
, re
->mtu
);
1853 re
->table
= zvrf
->table_id
;
1855 ret
= rib_add_multipath(AFI_IP6
, safi
, &p
, NULL
, re
);
1858 client
->v4_route_add_cnt
++;
1860 client
->v4_route_upd8_cnt
++;
1865 nexthops_free(re
->ng
.nexthop
);
1866 XFREE(MTYPE_RE
, re
);
1869 static void zread_ipv6_add(ZAPI_HANDLER_ARGS
)
1873 struct in6_addr nhop_addr
;
1875 struct route_entry
*re
;
1877 uint8_t nexthop_num
;
1878 uint8_t nexthop_type
;
1880 struct prefix_ipv6 src_p
, *src_pp
;
1882 static struct in6_addr nexthops
[MULTIPATH_NUM
];
1883 static unsigned int ifindices
[MULTIPATH_NUM
];
1885 static mpls_label_t labels
[MULTIPATH_NUM
];
1886 enum lsp_types_t label_type
= ZEBRA_LSP_NONE
;
1888 struct nexthop
*nexthop
;
1889 enum blackhole_type bh_type
= BLACKHOLE_NULL
;
1891 /* Get input stream. */
1894 memset(&nhop_addr
, 0, sizeof(struct in6_addr
));
1896 /* Allocate new re. */
1897 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
1899 /* Type, flags, message. */
1900 STREAM_GETC(s
, re
->type
);
1901 if (re
->type
> ZEBRA_ROUTE_MAX
) {
1902 zlog_warn("%s: Specified route type: %d is not a legal value\n",
1903 __PRETTY_FUNCTION__
, re
->type
);
1904 XFREE(MTYPE_RE
, re
);
1907 STREAM_GETW(s
, re
->instance
);
1908 STREAM_GETL(s
, re
->flags
);
1909 STREAM_GETC(s
, message
);
1910 STREAM_GETW(s
, safi
);
1911 re
->uptime
= time(NULL
);
1914 memset(&p
, 0, sizeof(p
));
1915 p
.family
= AF_INET6
;
1916 STREAM_GETC(s
, p
.prefixlen
);
1917 if (p
.prefixlen
> IPV6_MAX_BITLEN
) {
1919 "%s: Specified prefix length %d is to large for v6 prefix",
1920 __PRETTY_FUNCTION__
, p
.prefixlen
);
1921 XFREE(MTYPE_RE
, re
);
1924 STREAM_GET(&p
.u
.prefix6
, s
, PSIZE(p
.prefixlen
));
1926 if (CHECK_FLAG(message
, ZAPI_MESSAGE_SRCPFX
)) {
1927 memset(&src_p
, 0, sizeof(src_p
));
1928 src_p
.family
= AF_INET6
;
1929 STREAM_GETC(s
, src_p
.prefixlen
);
1930 if (src_p
.prefixlen
> IPV6_MAX_BITLEN
) {
1932 "%s: Specified src prefix length %d is to large for v6 prefix",
1933 __PRETTY_FUNCTION__
, src_p
.prefixlen
);
1934 XFREE(MTYPE_RE
, re
);
1937 STREAM_GET(&src_p
.prefix
, s
, PSIZE(src_p
.prefixlen
));
1943 re
->vrf_id
= zvrf_id(zvrf
);
1945 /* We need to give nh-addr, nh-ifindex with the same next-hop object
1946 * to the re to ensure that IPv6 multipathing works; need to coalesce
1947 * these. Clients should send the same number of paired set of
1948 * next-hop-addr/next-hop-ifindices. */
1949 if (CHECK_FLAG(message
, ZAPI_MESSAGE_NEXTHOP
)) {
1950 unsigned int nh_count
= 0;
1951 unsigned int if_count
= 0;
1952 unsigned int max_nh_if
= 0;
1954 STREAM_GETC(s
, nexthop_num
);
1955 zserv_nexthop_num_warn(__func__
, (const struct prefix
*)&p
,
1958 if (CHECK_FLAG(message
, ZAPI_MESSAGE_LABEL
))
1959 label_type
= lsp_type_from_re_type(client
->proto
);
1961 for (i
= 0; i
< nexthop_num
; i
++) {
1962 STREAM_GETC(s
, nexthop_type
);
1964 switch (nexthop_type
) {
1965 case NEXTHOP_TYPE_IPV6
:
1966 STREAM_GET(&nhop_addr
, s
, 16);
1967 if (nh_count
< MULTIPATH_NUM
) {
1968 /* For labeled-unicast, each nexthop is
1969 * followed by label. */
1970 if (CHECK_FLAG(message
,
1971 ZAPI_MESSAGE_LABEL
)) {
1972 STREAM_GETL(s
, label
);
1973 labels
[nh_count
] = label
;
1975 nexthops
[nh_count
++] = nhop_addr
;
1978 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1979 STREAM_GET(&nhop_addr
, s
, 16);
1980 STREAM_GETL(s
, ifindex
);
1981 route_entry_nexthop_ipv6_ifindex_add(
1982 re
, &nhop_addr
, ifindex
, re
->vrf_id
);
1984 case NEXTHOP_TYPE_IFINDEX
:
1985 if (if_count
< multipath_num
) {
1986 STREAM_GETL(s
, ifindices
[if_count
++]);
1989 case NEXTHOP_TYPE_BLACKHOLE
:
1990 route_entry_nexthop_blackhole_add(re
, bh_type
);
1994 "%s: Please use ZEBRA_ROUTE_ADD if you want to pass non v6 nexthops",
1995 __PRETTY_FUNCTION__
);
1996 nexthops_free(re
->ng
.nexthop
);
1997 XFREE(MTYPE_RE
, re
);
2002 max_nh_if
= (nh_count
> if_count
) ? nh_count
: if_count
;
2003 for (i
= 0; i
< max_nh_if
; i
++) {
2005 && !IN6_IS_ADDR_UNSPECIFIED(&nexthops
[i
])) {
2006 if ((i
< if_count
) && ifindices
[i
])
2008 route_entry_nexthop_ipv6_ifindex_add(
2013 nexthop
= route_entry_nexthop_ipv6_add(
2014 re
, &nexthops
[i
], re
->vrf_id
);
2015 if (CHECK_FLAG(message
, ZAPI_MESSAGE_LABEL
))
2016 nexthop_add_labels(nexthop
, label_type
,
2019 if ((i
< if_count
) && ifindices
[i
])
2020 route_entry_nexthop_ifindex_add(
2021 re
, ifindices
[i
], re
->vrf_id
);
2027 if (CHECK_FLAG(message
, ZAPI_MESSAGE_DISTANCE
))
2028 STREAM_GETC(s
, re
->distance
);
2031 if (CHECK_FLAG(message
, ZAPI_MESSAGE_METRIC
))
2032 STREAM_GETL(s
, re
->metric
);
2035 if (CHECK_FLAG(message
, ZAPI_MESSAGE_TAG
))
2036 STREAM_GETL(s
, re
->tag
);
2040 if (CHECK_FLAG(message
, ZAPI_MESSAGE_MTU
))
2041 STREAM_GETL(s
, re
->mtu
);
2045 re
->table
= zvrf
->table_id
;
2047 ret
= rib_add_multipath(AFI_IP6
, safi
, &p
, src_pp
, re
);
2050 client
->v6_route_add_cnt
++;
2052 client
->v6_route_upd8_cnt
++;
2057 nexthops_free(re
->ng
.nexthop
);
2058 XFREE(MTYPE_RE
, re
);
2061 /* Zebra server IPv6 prefix delete function. */
2062 static void zread_ipv6_delete(ZAPI_HANDLER_ARGS
)
2065 struct zapi_ipv6 api
;
2067 struct prefix_ipv6 src_p
, *src_pp
;
2071 /* Type, flags, message. */
2072 STREAM_GETC(s
, api
.type
);
2073 STREAM_GETW(s
, api
.instance
);
2074 STREAM_GETL(s
, api
.flags
);
2075 STREAM_GETC(s
, api
.message
);
2076 STREAM_GETW(s
, api
.safi
);
2079 memset(&p
, 0, sizeof(struct prefix
));
2080 p
.family
= AF_INET6
;
2081 STREAM_GETC(s
, p
.prefixlen
);
2082 STREAM_GET(&p
.u
.prefix6
, s
, PSIZE(p
.prefixlen
));
2084 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
)) {
2085 memset(&src_p
, 0, sizeof(struct prefix_ipv6
));
2086 src_p
.family
= AF_INET6
;
2087 STREAM_GETC(s
, src_p
.prefixlen
);
2088 STREAM_GET(&src_p
.prefix
, s
, PSIZE(src_p
.prefixlen
));
2093 rib_delete(AFI_IP6
, api
.safi
, zvrf_id(zvrf
), api
.type
, api
.instance
,
2094 api
.flags
, &p
, src_pp
, NULL
, client
->rtm_table
, 0, false,
2097 client
->v6_route_del_cnt
++;
2103 /* Register zebra server router-id information. Send current router-id */
2104 static void zread_router_id_add(ZAPI_HANDLER_ARGS
)
2108 /* Router-id information is needed. */
2109 vrf_bitmap_set(client
->ridinfo
, zvrf_id(zvrf
));
2111 router_id_get(&p
, zvrf_id(zvrf
));
2113 zsend_router_id_update(client
, &p
, zvrf_id(zvrf
));
2116 /* Unregister zebra server router-id information. */
2117 static void zread_router_id_delete(ZAPI_HANDLER_ARGS
)
2119 vrf_bitmap_unset(client
->ridinfo
, zvrf_id(zvrf
));
2122 /* Tie up route-type and client->sock */
2123 static void zread_hello(ZAPI_HANDLER_ARGS
)
2125 /* type of protocol (lib/zebra.h) */
2127 unsigned short instance
;
2130 STREAM_GETC(msg
, proto
);
2131 STREAM_GETW(msg
, instance
);
2132 STREAM_GETC(msg
, notify
);
2134 client
->notify_owner
= true;
2136 /* accept only dynamic routing protocols */
2137 if ((proto
< ZEBRA_ROUTE_MAX
) && (proto
> ZEBRA_ROUTE_STATIC
)) {
2139 "client %d says hello and bids fair to announce only %s routes vrf=%u",
2140 client
->sock
, zebra_route_string(proto
),
2143 zlog_notice("client protocol instance %d", instance
);
2145 client
->proto
= proto
;
2146 client
->instance
= instance
;
2153 /* Unregister all information in a VRF. */
2154 static void zread_vrf_unregister(ZAPI_HANDLER_ARGS
)
2159 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
2160 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
2161 vrf_bitmap_unset(client
->redist
[afi
][i
], zvrf_id(zvrf
));
2162 vrf_bitmap_unset(client
->redist_default
, zvrf_id(zvrf
));
2163 vrf_bitmap_unset(client
->ifinfo
, zvrf_id(zvrf
));
2164 vrf_bitmap_unset(client
->ridinfo
, zvrf_id(zvrf
));
2167 static void zread_mpls_labels(ZAPI_HANDLER_ARGS
)
2170 enum lsp_types_t type
;
2171 struct prefix prefix
;
2172 enum nexthop_types_t gtype
;
2175 mpls_label_t in_label
, out_label
;
2178 /* Get input stream. */
2182 STREAM_GETC(s
, type
);
2183 STREAM_GETL(s
, prefix
.family
);
2184 switch (prefix
.family
) {
2186 STREAM_GET(&prefix
.u
.prefix4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
2187 STREAM_GETC(s
, prefix
.prefixlen
);
2188 if (prefix
.prefixlen
> IPV4_MAX_BITLEN
) {
2190 "%s: Specified prefix length %d is greater than a v4 address can support",
2191 __PRETTY_FUNCTION__
, prefix
.prefixlen
);
2194 STREAM_GET(&gate
.ipv4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
2197 STREAM_GET(&prefix
.u
.prefix6
, s
, 16);
2198 STREAM_GETC(s
, prefix
.prefixlen
);
2199 if (prefix
.prefixlen
> IPV6_MAX_BITLEN
) {
2201 "%s: Specified prefix length %d is greater than a v6 address can support",
2202 __PRETTY_FUNCTION__
, prefix
.prefixlen
);
2205 STREAM_GET(&gate
.ipv6
, s
, 16);
2208 zlog_warn("%s: Specified AF %d is not supported for this call",
2209 __PRETTY_FUNCTION__
, prefix
.family
);
2212 STREAM_GETL(s
, ifindex
);
2213 STREAM_GETC(s
, distance
);
2214 STREAM_GETL(s
, in_label
);
2215 STREAM_GETL(s
, out_label
);
2217 switch (prefix
.family
) {
2220 gtype
= NEXTHOP_TYPE_IPV4_IFINDEX
;
2222 gtype
= NEXTHOP_TYPE_IPV4
;
2226 gtype
= NEXTHOP_TYPE_IPV6_IFINDEX
;
2228 gtype
= NEXTHOP_TYPE_IPV6
;
2237 if (hdr
->command
== ZEBRA_MPLS_LABELS_ADD
) {
2238 mpls_lsp_install(zvrf
, type
, in_label
, out_label
, gtype
, &gate
,
2240 mpls_ftn_update(1, zvrf
, type
, &prefix
, gtype
, &gate
, ifindex
,
2241 distance
, out_label
);
2242 } else if (hdr
->command
== ZEBRA_MPLS_LABELS_DELETE
) {
2243 mpls_lsp_uninstall(zvrf
, type
, in_label
, gtype
, &gate
, ifindex
);
2244 mpls_ftn_update(0, zvrf
, type
, &prefix
, gtype
, &gate
, ifindex
,
2245 distance
, out_label
);
2251 static int zsend_table_manager_connect_response(struct zserv
*client
,
2252 vrf_id_t vrf_id
, uint16_t result
)
2254 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
2256 zclient_create_header(s
, ZEBRA_TABLE_MANAGER_CONNECT
, vrf_id
);
2259 stream_putc(s
, result
);
2261 stream_putw_at(s
, 0, stream_get_endp(s
));
2263 return zebra_server_send_message(client
, s
);
2266 /* Send response to a table manager connect request to client */
2267 static void zread_table_manager_connect(struct zserv
*client
,
2278 STREAM_GETC(s
, proto
);
2279 STREAM_GETW(s
, instance
);
2281 /* accept only dynamic routing protocols */
2282 if ((proto
>= ZEBRA_ROUTE_MAX
) || (proto
<= ZEBRA_ROUTE_STATIC
)) {
2283 zlog_err("client %d has wrong protocol %s", client
->sock
,
2284 zebra_route_string(proto
));
2285 zsend_table_manager_connect_response(client
, vrf_id
, 1);
2288 zlog_notice("client %d with vrf %u instance %u connected as %s",
2289 client
->sock
, vrf_id
, instance
, zebra_route_string(proto
));
2290 client
->proto
= proto
;
2291 client
->instance
= instance
;
2294 * Release previous labels of same protocol and instance.
2295 * This is done in case it restarted from an unexpected shutdown.
2297 release_daemon_table_chunks(proto
, instance
);
2299 zsend_table_manager_connect_response(client
, vrf_id
, 0);
2305 static void zread_label_manager_connect(struct zserv
*client
,
2306 struct stream
*msg
, vrf_id_t vrf_id
)
2309 /* type of protocol (lib/zebra.h) */
2311 unsigned short instance
;
2313 /* Get input stream. */
2317 STREAM_GETC(s
, proto
);
2318 STREAM_GETW(s
, instance
);
2320 /* accept only dynamic routing protocols */
2321 if ((proto
>= ZEBRA_ROUTE_MAX
) || (proto
<= ZEBRA_ROUTE_STATIC
)) {
2322 zlog_err("client %d has wrong protocol %s", client
->sock
,
2323 zebra_route_string(proto
));
2324 zsend_label_manager_connect_response(client
, vrf_id
, 1);
2327 zlog_notice("client %d with vrf %u instance %u connected as %s",
2328 client
->sock
, vrf_id
, instance
, zebra_route_string(proto
));
2329 client
->proto
= proto
;
2330 client
->instance
= instance
;
2333 Release previous labels of same protocol and instance.
2334 This is done in case it restarted from an unexpected shutdown.
2336 release_daemon_label_chunks(proto
, instance
);
2339 " Label Manager client connected: sock %d, proto %s, vrf %u instance %u",
2340 client
->sock
, zebra_route_string(proto
), vrf_id
, instance
);
2341 /* send response back */
2342 zsend_label_manager_connect_response(client
, vrf_id
, 0);
2348 static void zread_get_label_chunk(struct zserv
*client
, struct stream
*msg
,
2354 struct label_manager_chunk
*lmc
;
2356 /* Get input stream. */
2360 STREAM_GETC(s
, keep
);
2361 STREAM_GETL(s
, size
);
2363 lmc
= assign_label_chunk(client
->proto
, client
->instance
, keep
, size
);
2365 zlog_err("%s: Unable to assign Label Chunk of size %u",
2368 zlog_debug("Assigned Label Chunk %u - %u to %u", lmc
->start
,
2370 /* send response back */
2371 zsend_assign_label_chunk_response(client
, vrf_id
, lmc
);
2377 static void zread_release_label_chunk(struct zserv
*client
, struct stream
*msg
)
2380 uint32_t start
, end
;
2382 /* Get input stream. */
2386 STREAM_GETL(s
, start
);
2387 STREAM_GETL(s
, end
);
2389 release_label_chunk(client
->proto
, client
->instance
, start
, end
);
2394 static void zread_label_manager_request(ZAPI_HANDLER_ARGS
)
2396 /* to avoid sending other messages like ZERBA_INTERFACE_UP */
2397 if (hdr
->command
== ZEBRA_LABEL_MANAGER_CONNECT
)
2398 client
->is_synchronous
= 1;
2400 /* external label manager */
2402 zread_relay_label_manager_request(hdr
->command
, client
,
2404 /* this is a label manager */
2406 if (hdr
->command
== ZEBRA_LABEL_MANAGER_CONNECT
)
2407 zread_label_manager_connect(client
, msg
, zvrf_id(zvrf
));
2409 /* Sanity: don't allow 'unidentified' requests */
2410 if (!client
->proto
) {
2412 "Got label request from an unidentified client");
2415 if (hdr
->command
== ZEBRA_GET_LABEL_CHUNK
)
2416 zread_get_label_chunk(client
, msg
,
2418 else if (hdr
->command
== ZEBRA_RELEASE_LABEL_CHUNK
)
2419 zread_release_label_chunk(client
, msg
);
2424 /* Send response to a get table chunk request to client */
2425 static int zsend_assign_table_chunk_response(struct zserv
*client
,
2427 struct table_manager_chunk
*tmc
)
2429 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
2431 zclient_create_header(s
, ZEBRA_GET_TABLE_CHUNK
, vrf_id
);
2434 /* start and end labels */
2435 stream_putl(s
, tmc
->start
);
2436 stream_putl(s
, tmc
->end
);
2439 /* Write packet size. */
2440 stream_putw_at(s
, 0, stream_get_endp(s
));
2442 return zebra_server_send_message(client
, s
);
2445 static void zread_get_table_chunk(struct zserv
*client
, struct stream
*msg
,
2450 struct table_manager_chunk
*tmc
;
2452 /* Get input stream. */
2456 STREAM_GETL(s
, size
);
2458 tmc
= assign_table_chunk(client
->proto
, client
->instance
, size
);
2460 zlog_err("%s: Unable to assign Table Chunk of size %u",
2463 zlog_debug("Assigned Table Chunk %u - %u", tmc
->start
,
2465 /* send response back */
2466 zsend_assign_table_chunk_response(client
, vrf_id
, tmc
);
2472 static void zread_release_table_chunk(struct zserv
*client
, struct stream
*msg
)
2475 uint32_t start
, end
;
2477 /* Get input stream. */
2481 STREAM_GETL(s
, start
);
2482 STREAM_GETL(s
, end
);
2484 release_table_chunk(client
->proto
, client
->instance
, start
, end
);
2490 static void zread_table_manager_request(ZAPI_HANDLER_ARGS
)
2492 /* to avoid sending other messages like ZERBA_INTERFACE_UP */
2493 if (hdr
->command
== ZEBRA_TABLE_MANAGER_CONNECT
)
2494 zread_table_manager_connect(client
, msg
, zvrf_id(zvrf
));
2496 /* Sanity: don't allow 'unidentified' requests */
2497 if (!client
->proto
) {
2499 "Got table request from an unidentified client");
2502 if (hdr
->command
== ZEBRA_GET_TABLE_CHUNK
)
2503 zread_get_table_chunk(client
, msg
,
2505 else if (hdr
->command
== ZEBRA_RELEASE_TABLE_CHUNK
)
2506 zread_release_table_chunk(client
, msg
);
2510 static void zread_pseudowire(ZAPI_HANDLER_ARGS
)
2513 char ifname
[IF_NAMESIZE
];
2517 union g_addr nexthop
;
2518 uint32_t local_label
;
2519 uint32_t remote_label
;
2521 union pw_protocol_fields data
;
2523 struct zebra_pw
*pw
;
2525 /* Get input stream. */
2529 STREAM_GET(ifname
, s
, IF_NAMESIZE
);
2530 STREAM_GETL(s
, ifindex
);
2531 STREAM_GETL(s
, type
);
2535 STREAM_GET(&nexthop
.ipv4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
2538 STREAM_GET(&nexthop
.ipv6
, s
, 16);
2543 STREAM_GETL(s
, local_label
);
2544 STREAM_GETL(s
, remote_label
);
2545 STREAM_GETC(s
, flags
);
2546 STREAM_GET(&data
, s
, sizeof(data
));
2547 protocol
= client
->proto
;
2549 pw
= zebra_pw_find(zvrf
, ifname
);
2550 switch (hdr
->command
) {
2553 zlog_warn("%s: pseudowire %s already exists [%s]",
2555 zserv_command_string(hdr
->command
));
2559 zebra_pw_add(zvrf
, ifname
, protocol
, client
);
2561 case ZEBRA_PW_DELETE
:
2563 zlog_warn("%s: pseudowire %s not found [%s]", __func__
,
2564 ifname
, zserv_command_string(hdr
->command
));
2568 zebra_pw_del(zvrf
, pw
);
2571 case ZEBRA_PW_UNSET
:
2573 zlog_warn("%s: pseudowire %s not found [%s]", __func__
,
2574 ifname
, zserv_command_string(hdr
->command
));
2578 switch (hdr
->command
) {
2582 case ZEBRA_PW_UNSET
:
2587 zebra_pw_change(pw
, ifindex
, type
, af
, &nexthop
, local_label
,
2588 remote_label
, flags
, &data
);
2596 /* Cleanup registered nexthops (across VRFs) upon client disconnect. */
2597 static void zebra_client_close_cleanup_rnh(struct zserv
*client
)
2600 struct zebra_vrf
*zvrf
;
2602 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
2603 if ((zvrf
= vrf
->info
) != NULL
) {
2604 zebra_cleanup_rnh_client(zvrf_id(zvrf
), AF_INET
, client
,
2606 zebra_cleanup_rnh_client(zvrf_id(zvrf
), AF_INET6
,
2607 client
, RNH_NEXTHOP_TYPE
);
2608 zebra_cleanup_rnh_client(zvrf_id(zvrf
), AF_INET
, client
,
2609 RNH_IMPORT_CHECK_TYPE
);
2610 zebra_cleanup_rnh_client(zvrf_id(zvrf
), AF_INET6
,
2611 client
, RNH_IMPORT_CHECK_TYPE
);
2612 if (client
->proto
== ZEBRA_ROUTE_LDP
) {
2613 hash_iterate(zvrf
->lsp_table
,
2614 mpls_ldp_lsp_uninstall_all
,
2616 mpls_ldp_ftn_uninstall_all(zvrf
, AFI_IP
);
2617 mpls_ldp_ftn_uninstall_all(zvrf
, AFI_IP6
);
2623 static void zread_interface_set_master(ZAPI_HANDLER_ARGS
)
2625 struct interface
*master
;
2626 struct interface
*slave
;
2627 struct stream
*s
= msg
;
2631 STREAM_GETL(s
, vrf_id
);
2632 STREAM_GETL(s
, ifindex
);
2633 master
= if_lookup_by_index(ifindex
, vrf_id
);
2635 STREAM_GETL(s
, vrf_id
);
2636 STREAM_GETL(s
, ifindex
);
2637 slave
= if_lookup_by_index(ifindex
, vrf_id
);
2639 if (!master
|| !slave
)
2642 kernel_interface_set_master(master
, slave
);
2649 static void zread_vrf_label(ZAPI_HANDLER_ARGS
)
2651 struct interface
*ifp
;
2652 mpls_label_t nlabel
;
2655 struct zebra_vrf
*def_zvrf
;
2656 enum lsp_types_t ltype
;
2659 STREAM_GETL(s
, nlabel
);
2660 STREAM_GETC(s
, afi
);
2661 if (nlabel
== zvrf
->label
[afi
]) {
2663 * Nothing to do here move along
2668 STREAM_GETC(s
, ltype
);
2670 if (zvrf
->vrf
->vrf_id
!= VRF_DEFAULT
)
2671 ifp
= if_lookup_by_name(zvrf
->vrf
->name
, zvrf
->vrf
->vrf_id
);
2673 ifp
= if_lookup_by_name("lo", VRF_DEFAULT
);
2676 zlog_debug("Unable to find specified Interface for %s",
2681 def_zvrf
= zebra_vrf_lookup_by_id(VRF_DEFAULT
);
2683 if (zvrf
->label
[afi
] != MPLS_LABEL_NONE
) {
2687 really_remove
= true;
2688 for (scrubber
= AFI_IP
; scrubber
< AFI_MAX
; scrubber
++) {
2689 if (scrubber
== afi
)
2692 if (zvrf
->label
[scrubber
] == MPLS_LABEL_NONE
)
2695 if (zvrf
->label
[afi
] == zvrf
->label
[scrubber
]) {
2696 really_remove
= false;
2702 mpls_lsp_uninstall(def_zvrf
, ltype
, zvrf
->label
[afi
],
2703 NEXTHOP_TYPE_IFINDEX
, NULL
,
2707 if (nlabel
!= MPLS_LABEL_NONE
)
2708 mpls_lsp_install(def_zvrf
, ltype
, nlabel
,
2709 MPLS_LABEL_IMPLICIT_NULL
, NEXTHOP_TYPE_IFINDEX
,
2710 NULL
, ifp
->ifindex
);
2712 zvrf
->label
[afi
] = nlabel
;
2717 static inline void zread_rule(ZAPI_HANDLER_ARGS
)
2719 struct zebra_pbr_rule zpr
;
2725 STREAM_GETL(s
, total
);
2727 for (i
= 0; i
< total
; i
++) {
2728 memset(&zpr
, 0, sizeof(zpr
));
2730 zpr
.sock
= client
->sock
;
2731 STREAM_GETL(s
, zpr
.seq
);
2732 STREAM_GETL(s
, zpr
.priority
);
2733 STREAM_GETL(s
, zpr
.unique
);
2734 STREAM_GETC(s
, zpr
.filter
.src_ip
.family
);
2735 STREAM_GETC(s
, zpr
.filter
.src_ip
.prefixlen
);
2736 STREAM_GET(&zpr
.filter
.src_ip
.u
.prefix
, s
,
2737 prefix_blen(&zpr
.filter
.src_ip
));
2738 STREAM_GETW(s
, zpr
.filter
.src_port
);
2739 STREAM_GETC(s
, zpr
.filter
.dst_ip
.family
);
2740 STREAM_GETC(s
, zpr
.filter
.dst_ip
.prefixlen
);
2741 STREAM_GET(&zpr
.filter
.dst_ip
.u
.prefix
, s
,
2742 prefix_blen(&zpr
.filter
.dst_ip
));
2743 STREAM_GETW(s
, zpr
.filter
.dst_port
);
2744 STREAM_GETL(s
, zpr
.action
.table
);
2745 STREAM_GETL(s
, ifindex
);
2747 zpr
.ifp
= if_lookup_by_index(ifindex
, VRF_UNKNOWN
);
2749 zlog_debug("Failed to lookup ifindex: %u", ifindex
);
2753 if (!is_default_prefix(&zpr
.filter
.src_ip
))
2754 zpr
.filter
.filter_bm
|= PBR_FILTER_SRC_IP
;
2756 if (!is_default_prefix(&zpr
.filter
.dst_ip
))
2757 zpr
.filter
.filter_bm
|= PBR_FILTER_DST_IP
;
2759 if (zpr
.filter
.src_port
)
2760 zpr
.filter
.filter_bm
|= PBR_FILTER_SRC_PORT
;
2762 if (zpr
.filter
.dst_port
)
2763 zpr
.filter
.filter_bm
|= PBR_FILTER_DST_PORT
;
2765 if (hdr
->command
== ZEBRA_RULE_ADD
)
2766 zebra_pbr_add_rule(zvrf
->zns
, &zpr
);
2768 zebra_pbr_del_rule(zvrf
->zns
, &zpr
);
2776 static inline void zread_ipset(ZAPI_HANDLER_ARGS
)
2778 struct zebra_pbr_ipset zpi
;
2783 STREAM_GETL(s
, total
);
2785 for (i
= 0; i
< total
; i
++) {
2786 memset(&zpi
, 0, sizeof(zpi
));
2788 zpi
.sock
= client
->sock
;
2789 STREAM_GETL(s
, zpi
.unique
);
2790 STREAM_GETL(s
, zpi
.type
);
2791 STREAM_GET(&zpi
.ipset_name
, s
,
2792 ZEBRA_IPSET_NAME_SIZE
);
2794 if (hdr
->command
== ZEBRA_IPSET_CREATE
)
2795 zebra_pbr_create_ipset(zvrf
->zns
, &zpi
);
2797 zebra_pbr_destroy_ipset(zvrf
->zns
, &zpi
);
2804 static inline void zread_ipset_entry(ZAPI_HANDLER_ARGS
)
2806 struct zebra_pbr_ipset_entry zpi
;
2807 struct zebra_pbr_ipset ipset
;
2812 STREAM_GETL(s
, total
);
2814 for (i
= 0; i
< total
; i
++) {
2815 memset(&zpi
, 0, sizeof(zpi
));
2816 memset(&ipset
, 0, sizeof(ipset
));
2818 zpi
.sock
= client
->sock
;
2819 STREAM_GETL(s
, zpi
.unique
);
2820 STREAM_GET(&ipset
.ipset_name
, s
,
2821 ZEBRA_IPSET_NAME_SIZE
);
2822 STREAM_GETC(s
, zpi
.src
.family
);
2823 STREAM_GETC(s
, zpi
.src
.prefixlen
);
2824 STREAM_GET(&zpi
.src
.u
.prefix
, s
,
2825 prefix_blen(&zpi
.src
));
2826 STREAM_GETC(s
, zpi
.dst
.family
);
2827 STREAM_GETC(s
, zpi
.dst
.prefixlen
);
2828 STREAM_GET(&zpi
.dst
.u
.prefix
, s
,
2829 prefix_blen(&zpi
.dst
));
2831 if (!is_default_prefix(&zpi
.src
))
2832 zpi
.filter_bm
|= PBR_FILTER_SRC_IP
;
2834 if (!is_default_prefix(&zpi
.dst
))
2835 zpi
.filter_bm
|= PBR_FILTER_DST_IP
;
2837 /* calculate backpointer */
2838 zpi
.backpointer
= zebra_pbr_lookup_ipset_pername(zvrf
->zns
,
2840 if (hdr
->command
== ZEBRA_IPSET_ENTRY_ADD
)
2841 zebra_pbr_add_ipset_entry(zvrf
->zns
, &zpi
);
2843 zebra_pbr_del_ipset_entry(zvrf
->zns
, &zpi
);
2851 void (*zserv_handlers
[])(ZAPI_HANDLER_ARGS
) = {
2852 [ZEBRA_ROUTER_ID_ADD
] = zread_router_id_add
,
2853 [ZEBRA_ROUTER_ID_DELETE
] = zread_router_id_delete
,
2854 [ZEBRA_INTERFACE_ADD
] = zread_interface_add
,
2855 [ZEBRA_INTERFACE_DELETE
] = zread_interface_delete
,
2856 [ZEBRA_ROUTE_ADD
] = zread_route_add
,
2857 [ZEBRA_ROUTE_DELETE
] = zread_route_del
,
2858 [ZEBRA_IPV4_ROUTE_ADD
] = zread_ipv4_add
,
2859 [ZEBRA_IPV4_ROUTE_DELETE
] = zread_ipv4_delete
,
2860 [ZEBRA_IPV4_ROUTE_IPV6_NEXTHOP_ADD
] = zread_ipv4_route_ipv6_nexthop_add
,
2861 [ZEBRA_IPV6_ROUTE_ADD
] = zread_ipv6_add
,
2862 [ZEBRA_IPV6_ROUTE_DELETE
] = zread_ipv6_delete
,
2863 [ZEBRA_REDISTRIBUTE_ADD
] = zebra_redistribute_add
,
2864 [ZEBRA_REDISTRIBUTE_DELETE
] = zebra_redistribute_delete
,
2865 [ZEBRA_REDISTRIBUTE_DEFAULT_ADD
] = zebra_redistribute_default_add
,
2866 [ZEBRA_REDISTRIBUTE_DEFAULT_DELETE
] = zebra_redistribute_default_delete
,
2867 [ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB
] = zread_ipv4_nexthop_lookup_mrib
,
2868 [ZEBRA_HELLO
] = zread_hello
,
2869 [ZEBRA_NEXTHOP_REGISTER
] = zread_rnh_register
,
2870 [ZEBRA_NEXTHOP_UNREGISTER
] = zread_rnh_unregister
,
2871 [ZEBRA_IMPORT_ROUTE_REGISTER
] = zread_rnh_register
,
2872 [ZEBRA_IMPORT_ROUTE_UNREGISTER
] = zread_rnh_unregister
,
2873 [ZEBRA_BFD_DEST_UPDATE
] = zebra_ptm_bfd_dst_register
,
2874 [ZEBRA_BFD_DEST_REGISTER
] = zebra_ptm_bfd_dst_register
,
2875 [ZEBRA_BFD_DEST_DEREGISTER
] = zebra_ptm_bfd_dst_deregister
,
2876 [ZEBRA_VRF_UNREGISTER
] = zread_vrf_unregister
,
2877 [ZEBRA_VRF_LABEL
] = zread_vrf_label
,
2878 [ZEBRA_BFD_CLIENT_REGISTER
] = zebra_ptm_bfd_client_register
,
2879 #if defined(HAVE_RTADV)
2880 [ZEBRA_INTERFACE_ENABLE_RADV
] = zebra_interface_radv_enable
,
2881 [ZEBRA_INTERFACE_DISABLE_RADV
] = zebra_interface_radv_disable
,
2883 [ZEBRA_INTERFACE_ENABLE_RADV
] = NULL
,
2884 [ZEBRA_INTERFACE_DISABLE_RADV
] = NULL
,
2886 [ZEBRA_MPLS_LABELS_ADD
] = zread_mpls_labels
,
2887 [ZEBRA_MPLS_LABELS_DELETE
] = zread_mpls_labels
,
2888 [ZEBRA_IPMR_ROUTE_STATS
] = zebra_ipmr_route_stats
,
2889 [ZEBRA_LABEL_MANAGER_CONNECT
] = zread_label_manager_request
,
2890 [ZEBRA_GET_LABEL_CHUNK
] = zread_label_manager_request
,
2891 [ZEBRA_RELEASE_LABEL_CHUNK
] = zread_label_manager_request
,
2892 [ZEBRA_FEC_REGISTER
] = zread_fec_register
,
2893 [ZEBRA_FEC_UNREGISTER
] = zread_fec_unregister
,
2894 [ZEBRA_ADVERTISE_DEFAULT_GW
] = zebra_vxlan_advertise_gw_macip
,
2895 [ZEBRA_ADVERTISE_SUBNET
] = zebra_vxlan_advertise_subnet
,
2896 [ZEBRA_ADVERTISE_ALL_VNI
] = zebra_vxlan_advertise_all_vni
,
2897 [ZEBRA_REMOTE_VTEP_ADD
] = zebra_vxlan_remote_vtep_add
,
2898 [ZEBRA_REMOTE_VTEP_DEL
] = zebra_vxlan_remote_vtep_del
,
2899 [ZEBRA_REMOTE_MACIP_ADD
] = zebra_vxlan_remote_macip_add
,
2900 [ZEBRA_REMOTE_MACIP_DEL
] = zebra_vxlan_remote_macip_del
,
2901 [ZEBRA_INTERFACE_SET_MASTER
] = zread_interface_set_master
,
2902 [ZEBRA_PW_ADD
] = zread_pseudowire
,
2903 [ZEBRA_PW_DELETE
] = zread_pseudowire
,
2904 [ZEBRA_PW_SET
] = zread_pseudowire
,
2905 [ZEBRA_PW_UNSET
] = zread_pseudowire
,
2906 [ZEBRA_RULE_ADD
] = zread_rule
,
2907 [ZEBRA_RULE_DELETE
] = zread_rule
,
2908 [ZEBRA_TABLE_MANAGER_CONNECT
] = zread_table_manager_request
,
2909 [ZEBRA_GET_TABLE_CHUNK
] = zread_table_manager_request
,
2910 [ZEBRA_RELEASE_TABLE_CHUNK
] = zread_table_manager_request
,
2911 [ZEBRA_IPSET_CREATE
] = zread_ipset
,
2912 [ZEBRA_IPSET_DESTROY
] = zread_ipset
,
2913 [ZEBRA_IPSET_ENTRY_ADD
] = zread_ipset_entry
,
2914 [ZEBRA_IPSET_ENTRY_DELETE
] = zread_ipset_entry
,
2917 static inline void zserv_handle_commands(struct zserv
*client
,
2918 struct zmsghdr
*hdr
,
2920 struct zebra_vrf
*zvrf
)
2922 if (hdr
->command
> array_size(zserv_handlers
)
2923 || zserv_handlers
[hdr
->command
] == NULL
)
2924 zlog_info("Zebra received unknown command %d", hdr
->command
);
2926 zserv_handlers
[hdr
->command
](client
, hdr
, msg
, zvrf
);
2931 /* Lifecycle ---------------------------------------------------------------- */
2933 /* free zebra client information. */
2934 static void zebra_client_free(struct zserv
*client
)
2936 /* Send client de-registration to BFD */
2937 zebra_ptm_bfd_client_deregister(client
->proto
);
2939 /* Cleanup any rules installed from this client */
2940 zebra_pbr_client_close_cleanup(client
->sock
);
2942 /* Cleanup any registered nexthops - across all VRFs. */
2943 zebra_client_close_cleanup_rnh(client
);
2945 /* Release Label Manager chunks */
2946 release_daemon_label_chunks(client
->proto
, client
->instance
);
2948 /* Release Table Manager chunks */
2949 release_daemon_table_chunks(client
->proto
, client
->instance
);
2951 /* Cleanup any FECs registered by this client. */
2952 zebra_mpls_cleanup_fecs_for_client(vrf_info_lookup(VRF_DEFAULT
),
2955 /* Remove pseudowires associated with this client */
2956 zebra_pw_client_close(client
);
2958 /* Close file descriptor. */
2960 unsigned long nroutes
;
2962 close(client
->sock
);
2963 nroutes
= rib_score_proto(client
->proto
, client
->instance
);
2965 "client %d disconnected. %lu %s routes removed from the rib",
2966 client
->sock
, nroutes
,
2967 zebra_route_string(client
->proto
));
2971 /* Free stream buffers. */
2972 if (client
->ibuf_work
)
2973 stream_free(client
->ibuf_work
);
2974 if (client
->obuf_work
)
2975 stream_free(client
->obuf_work
);
2976 if (client
->ibuf_fifo
)
2977 stream_fifo_free(client
->ibuf_fifo
);
2978 if (client
->obuf_fifo
)
2979 stream_fifo_free(client
->obuf_fifo
);
2981 buffer_free(client
->wb
);
2983 /* Release threads. */
2985 thread_cancel(client
->t_read
);
2986 if (client
->t_write
)
2987 thread_cancel(client
->t_write
);
2988 if (client
->t_suicide
)
2989 thread_cancel(client
->t_suicide
);
2992 for (afi_t afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
2993 for (int i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
2994 vrf_bitmap_free(client
->redist
[afi
][i
]);
2996 vrf_bitmap_free(client
->redist_default
);
2997 vrf_bitmap_free(client
->ifinfo
);
2998 vrf_bitmap_free(client
->ridinfo
);
3000 XFREE(MTYPE_TMP
, client
);
3004 * Called from client thread to terminate itself.
3006 static void zebra_client_close(struct zserv
*client
)
3008 listnode_delete(zebrad
.client_list
, client
);
3009 zebra_client_free(client
);
3012 /* Make new client. */
3013 static void zebra_client_create(int sock
)
3015 struct zserv
*client
;
3019 client
= XCALLOC(MTYPE_TMP
, sizeof(struct zserv
));
3021 /* Make client input/output buffer. */
3022 client
->sock
= sock
;
3023 client
->ibuf_fifo
= stream_fifo_new();
3024 client
->obuf_fifo
= stream_fifo_new();
3025 client
->ibuf_work
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
3026 client
->obuf_work
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
3027 client
->wb
= buffer_new(0);
3029 /* Set table number. */
3030 client
->rtm_table
= zebrad
.rtm_table_default
;
3032 client
->connect_time
= monotime(NULL
);
3033 /* Initialize flags */
3034 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
3035 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
3036 client
->redist
[afi
][i
] = vrf_bitmap_init();
3037 client
->redist_default
= vrf_bitmap_init();
3038 client
->ifinfo
= vrf_bitmap_init();
3039 client
->ridinfo
= vrf_bitmap_init();
3041 /* by default, it's not a synchronous client */
3042 client
->is_synchronous
= 0;
3044 /* Add this client to linked list. */
3045 listnode_add(zebrad
.client_list
, client
);
3047 zebra_vrf_update_all(client
);
3049 /* start read loop */
3050 zebra_event(client
, ZEBRA_READ
);
3053 static int zserv_delayed_close(struct thread
*thread
)
3055 struct zserv
*client
= THREAD_ARG(thread
);
3057 client
->t_suicide
= NULL
;
3058 zebra_client_close(client
);
3063 * Log zapi message to zlog.
3072 * The message header
3074 static void zserv_log_message(const char *errmsg
, struct stream
*msg
,
3075 struct zmsghdr
*hdr
)
3077 zlog_debug("Rx'd ZAPI message");
3079 zlog_debug("%s", errmsg
);
3081 zlog_debug(" Length: %d", hdr
->length
);
3082 zlog_debug("Command: %s", zserv_command_string(hdr
->command
));
3083 zlog_debug(" VRF: %u", hdr
->vrf_id
);
3085 zlog_hexdump(msg
->data
, STREAM_READABLE(msg
));
3088 static int zserv_flush_data(struct thread
*thread
)
3090 struct zserv
*client
= THREAD_ARG(thread
);
3092 client
->t_write
= NULL
;
3093 if (client
->t_suicide
) {
3094 zebra_client_close(client
);
3097 switch (buffer_flush_available(client
->wb
, client
->sock
)) {
3100 "%s: buffer_flush_available failed on zserv client fd %d, closing",
3101 __func__
, client
->sock
);
3102 zebra_client_close(client
);
3105 case BUFFER_PENDING
:
3106 client
->t_write
= NULL
;
3107 thread_add_write(zebrad
.master
, zserv_flush_data
, client
,
3108 client
->sock
, &client
->t_write
);
3115 client
->last_write_time
= monotime(NULL
);
3120 * Write a single packet.
3122 static int zserv_write(struct thread
*thread
)
3124 struct zserv
*client
= THREAD_ARG(thread
);
3128 if (client
->t_suicide
)
3131 if (client
->is_synchronous
)
3134 msg
= stream_fifo_pop(client
->obuf_fifo
);
3135 stream_set_getp(msg
, 0);
3136 client
->last_write_cmd
= stream_getw_from(msg
, 6);
3138 writerv
= buffer_write(client
->wb
, client
->sock
, STREAM_DATA(msg
),
3139 stream_get_endp(msg
));
3146 "%s: buffer_write failed to zserv client fd %d, closing",
3147 __func__
, client
->sock
);
3149 * Schedule a delayed close since many of the functions that
3150 * call this one do not check the return code. They do not
3151 * allow for the possibility that an I/O error may have caused
3152 * the client to be deleted.
3154 client
->t_suicide
= NULL
;
3155 thread_add_event(zebrad
.master
, zserv_delayed_close
, client
, 0,
3156 &client
->t_suicide
);
3159 THREAD_OFF(client
->t_write
);
3161 case BUFFER_PENDING
:
3162 thread_add_write(zebrad
.master
, zserv_flush_data
, client
,
3163 client
->sock
, &client
->t_write
);
3167 if (client
->obuf_fifo
->count
)
3168 zebra_event(client
, ZEBRA_WRITE
);
3170 client
->last_write_time
= monotime(NULL
);
3174 #if defined(HANDLE_ZAPI_FUZZING)
3175 static void zserv_write_incoming(struct stream
*orig
, uint16_t command
)
3177 char fname
[MAXPATHLEN
];
3178 struct stream
*copy
;
3181 copy
= stream_dup(orig
);
3182 stream_set_getp(copy
, 0);
3184 zserv_privs
.change(ZPRIVS_RAISE
);
3185 snprintf(fname
, MAXPATHLEN
, "%s/%u", DAEMON_VTY_DIR
, command
);
3186 fd
= open(fname
, O_CREAT
| O_WRONLY
| O_EXCL
, 0644);
3187 stream_flush(copy
, fd
);
3189 zserv_privs
.change(ZPRIVS_LOWER
);
3194 static int zserv_process_messages(struct thread
*thread
)
3196 struct zserv
*client
= THREAD_ARG(thread
);
3197 struct zebra_vrf
*zvrf
;
3203 msg
= stream_fifo_pop(client
->ibuf_fifo
);
3205 /* break if out of messages */
3209 /* read & check header */
3210 hdrvalid
= zapi_parse_header(msg
, &hdr
);
3211 if (!hdrvalid
&& IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
) {
3212 const char *emsg
= "Message has corrupt header";
3213 zserv_log_message(emsg
, msg
, NULL
);
3218 hdr
.length
-= ZEBRA_HEADER_SIZE
;
3220 zvrf
= zebra_vrf_lookup_by_id(hdr
.vrf_id
);
3221 if (!zvrf
&& IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
) {
3222 const char *emsg
= "Message specifies unknown VRF";
3223 zserv_log_message(emsg
, msg
, &hdr
);
3228 /* process commands */
3229 zserv_handle_commands(client
, &hdr
, msg
, zvrf
);
3236 /* Handler of zebra service request. */
3237 static int zserv_read(struct thread
*thread
)
3240 struct zserv
*client
;
3242 #if defined(HANDLE_ZAPI_FUZZING)
3245 int packets
= zebrad
.packets_to_process
;
3247 /* Get thread data. Reset reading thread because I'm running. */
3248 sock
= THREAD_FD(thread
);
3249 client
= THREAD_ARG(thread
);
3251 if (client
->t_suicide
) {
3252 zebra_client_close(client
);
3262 already
= stream_get_endp(client
->ibuf_work
);
3264 /* Read length and command (if we don't have it already). */
3265 if (already
< ZEBRA_HEADER_SIZE
) {
3266 nb
= stream_read_try(client
->ibuf_work
, sock
,
3267 ZEBRA_HEADER_SIZE
- already
);
3268 if ((nb
== 0 || nb
== -1) && IS_ZEBRA_DEBUG_EVENT
)
3269 zlog_debug("connection closed socket [%d]",
3271 if ((nb
== 0 || nb
== -1))
3273 if (nb
!= (ssize_t
)(ZEBRA_HEADER_SIZE
- already
)) {
3274 /* Try again later. */
3277 already
= ZEBRA_HEADER_SIZE
;
3280 /* Reset to read from the beginning of the incoming packet. */
3281 stream_set_getp(client
->ibuf_work
, 0);
3283 /* Fetch header values */
3284 hdrvalid
= zapi_parse_header(client
->ibuf_work
, &hdr
);
3287 snprintf(errmsg
, sizeof(errmsg
),
3288 "%s: Message has corrupt header", __func__
);
3289 zserv_log_message(errmsg
, client
->ibuf_work
, NULL
);
3293 /* Validate header */
3294 if (hdr
.marker
!= ZEBRA_HEADER_MARKER
3295 || hdr
.version
!= ZSERV_VERSION
) {
3297 errmsg
, sizeof(errmsg
),
3298 "Message has corrupt header\n%s: socket %d version mismatch, marker %d, version %d",
3299 __func__
, sock
, hdr
.marker
, hdr
.version
);
3300 zserv_log_message(errmsg
, client
->ibuf_work
, &hdr
);
3303 if (hdr
.length
< ZEBRA_HEADER_SIZE
) {
3305 errmsg
, sizeof(errmsg
),
3306 "Message has corrupt header\n%s: socket %d message length %u is less than header size %d",
3307 __func__
, sock
, hdr
.length
, ZEBRA_HEADER_SIZE
);
3308 zserv_log_message(errmsg
, client
->ibuf_work
, &hdr
);
3311 if (hdr
.length
> STREAM_SIZE(client
->ibuf_work
)) {
3313 errmsg
, sizeof(errmsg
),
3314 "Message has corrupt header\n%s: socket %d message length %u exceeds buffer size %lu",
3315 __func__
, sock
, hdr
.length
,
3316 (unsigned long)STREAM_SIZE(client
->ibuf_work
));
3320 /* Read rest of data. */
3321 if (already
< hdr
.length
) {
3322 nb
= stream_read_try(client
->ibuf_work
, sock
,
3323 hdr
.length
- already
);
3324 if ((nb
== 0 || nb
== -1) && IS_ZEBRA_DEBUG_EVENT
)
3326 "connection closed [%d] when reading zebra data",
3328 if ((nb
== 0 || nb
== -1))
3330 if (nb
!= (ssize_t
)(hdr
.length
- already
)) {
3331 /* Try again later. */
3336 #if defined(HANDLE_ZAPI_FUZZING)
3337 zserv_write_incoming(client
->ibuf_work
, command
);
3340 /* Debug packet information. */
3341 if (IS_ZEBRA_DEBUG_EVENT
)
3342 zlog_debug("zebra message comes from socket [%d]",
3345 if (IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
)
3346 zserv_log_message(NULL
, client
->ibuf_work
, &hdr
);
3348 client
->last_read_time
= monotime(NULL
);
3349 client
->last_read_cmd
= hdr
.command
;
3351 stream_set_getp(client
->ibuf_work
, 0);
3352 struct stream
*msg
= stream_dup(client
->ibuf_work
);
3354 stream_fifo_push(client
->ibuf_fifo
, msg
);
3356 if (client
->t_suicide
)
3360 stream_reset(client
->ibuf_work
);
3363 if (IS_ZEBRA_DEBUG_PACKET
)
3364 zlog_debug("Read %d packets",
3365 zebrad
.packets_to_process
- packets
);
3367 /* Schedule job to process those packets */
3368 thread_add_event(zebrad
.master
, &zserv_process_messages
, client
, 0,
3371 /* Reschedule ourselves */
3372 zebra_event(client
, ZEBRA_READ
);
3377 zebra_client_close(client
);
3381 static void zebra_event(struct zserv
*client
, enum event event
)
3385 thread_add_read(zebrad
.master
, zserv_read
, client
, client
->sock
,
3389 thread_add_write(zebrad
.master
, zserv_write
, client
,
3390 client
->sock
, &client
->t_write
);
3395 /* Accept code of zebra server socket. */
3396 static int zebra_accept(struct thread
*thread
)
3400 struct sockaddr_in client
;
3403 accept_sock
= THREAD_FD(thread
);
3405 /* Reregister myself. */
3406 thread_add_read(zebrad
.master
, zebra_accept
, NULL
, accept_sock
, NULL
);
3408 len
= sizeof(struct sockaddr_in
);
3409 client_sock
= accept(accept_sock
, (struct sockaddr
*)&client
, &len
);
3411 if (client_sock
< 0) {
3412 zlog_warn("Can't accept zebra socket: %s",
3413 safe_strerror(errno
));
3417 /* Make client socket non-blocking. */
3418 set_nonblocking(client_sock
);
3420 /* Create new zebra client. */
3421 zebra_client_create(client_sock
);
3426 /* Make zebra server socket, wiping any existing one (see bug #403). */
3427 void zebra_zserv_socket_init(char *path
)
3432 struct sockaddr_storage sa
;
3435 if (!frr_zclient_addr(&sa
, &sa_len
, path
))
3436 /* should be caught in zebra main() */
3440 old_mask
= umask(0077);
3442 /* Make UNIX domain socket. */
3443 sock
= socket(sa
.ss_family
, SOCK_STREAM
, 0);
3445 zlog_warn("Can't create zserv socket: %s",
3446 safe_strerror(errno
));
3448 "zebra can't provide full functionality due to above error");
3452 if (sa
.ss_family
!= AF_UNIX
) {
3453 sockopt_reuseaddr(sock
);
3454 sockopt_reuseport(sock
);
3456 struct sockaddr_un
*suna
= (struct sockaddr_un
*)&sa
;
3457 if (suna
->sun_path
[0])
3458 unlink(suna
->sun_path
);
3461 zserv_privs
.change(ZPRIVS_RAISE
);
3462 setsockopt_so_recvbuf(sock
, 1048576);
3463 setsockopt_so_sendbuf(sock
, 1048576);
3464 zserv_privs
.change(ZPRIVS_LOWER
);
3466 if (sa
.ss_family
!= AF_UNIX
&& zserv_privs
.change(ZPRIVS_RAISE
))
3467 zlog_err("Can't raise privileges");
3469 ret
= bind(sock
, (struct sockaddr
*)&sa
, sa_len
);
3471 zlog_warn("Can't bind zserv socket on %s: %s", path
,
3472 safe_strerror(errno
));
3474 "zebra can't provide full functionality due to above error");
3478 if (sa
.ss_family
!= AF_UNIX
&& zserv_privs
.change(ZPRIVS_LOWER
))
3479 zlog_err("Can't lower privileges");
3481 ret
= listen(sock
, 5);
3483 zlog_warn("Can't listen to zserv socket %s: %s", path
,
3484 safe_strerror(errno
));
3486 "zebra can't provide full functionality due to above error");
3493 thread_add_read(zebrad
.master
, zebra_accept
, NULL
, sock
, NULL
);
3496 #define ZEBRA_TIME_BUF 32
3497 static char *zserv_time_buf(time_t *time1
, char *buf
, int buflen
)
3502 assert(buf
!= NULL
);
3503 assert(buflen
>= ZEBRA_TIME_BUF
);
3504 assert(time1
!= NULL
);
3507 snprintf(buf
, buflen
, "never ");
3511 now
= monotime(NULL
);
3515 if (now
< ONE_DAY_SECOND
)
3516 snprintf(buf
, buflen
, "%02d:%02d:%02d", tm
->tm_hour
, tm
->tm_min
,
3518 else if (now
< ONE_WEEK_SECOND
)
3519 snprintf(buf
, buflen
, "%dd%02dh%02dm", tm
->tm_yday
, tm
->tm_hour
,
3522 snprintf(buf
, buflen
, "%02dw%dd%02dh", tm
->tm_yday
/ 7,
3523 tm
->tm_yday
- ((tm
->tm_yday
/ 7) * 7), tm
->tm_hour
);
3527 static void zebra_show_client_detail(struct vty
*vty
, struct zserv
*client
)
3529 char cbuf
[ZEBRA_TIME_BUF
], rbuf
[ZEBRA_TIME_BUF
];
3530 char wbuf
[ZEBRA_TIME_BUF
], nhbuf
[ZEBRA_TIME_BUF
], mbuf
[ZEBRA_TIME_BUF
];
3532 vty_out(vty
, "Client: %s", zebra_route_string(client
->proto
));
3533 if (client
->instance
)
3534 vty_out(vty
, " Instance: %d", client
->instance
);
3537 vty_out(vty
, "------------------------ \n");
3538 vty_out(vty
, "FD: %d \n", client
->sock
);
3539 vty_out(vty
, "Route Table ID: %d \n", client
->rtm_table
);
3541 vty_out(vty
, "Connect Time: %s \n",
3542 zserv_time_buf(&client
->connect_time
, cbuf
, ZEBRA_TIME_BUF
));
3543 if (client
->nh_reg_time
) {
3544 vty_out(vty
, "Nexthop Registry Time: %s \n",
3545 zserv_time_buf(&client
->nh_reg_time
, nhbuf
,
3547 if (client
->nh_last_upd_time
)
3548 vty_out(vty
, "Nexthop Last Update Time: %s \n",
3549 zserv_time_buf(&client
->nh_last_upd_time
, mbuf
,
3552 vty_out(vty
, "No Nexthop Update sent\n");
3554 vty_out(vty
, "Not registered for Nexthop Updates\n");
3556 vty_out(vty
, "Last Msg Rx Time: %s \n",
3557 zserv_time_buf(&client
->last_read_time
, rbuf
, ZEBRA_TIME_BUF
));
3558 vty_out(vty
, "Last Msg Tx Time: %s \n",
3559 zserv_time_buf(&client
->last_write_time
, wbuf
, ZEBRA_TIME_BUF
));
3560 if (client
->last_read_time
)
3561 vty_out(vty
, "Last Rcvd Cmd: %s \n",
3562 zserv_command_string(client
->last_read_cmd
));
3563 if (client
->last_write_time
)
3564 vty_out(vty
, "Last Sent Cmd: %s \n",
3565 zserv_command_string(client
->last_write_cmd
));
3568 vty_out(vty
, "Type Add Update Del \n");
3569 vty_out(vty
, "================================================== \n");
3570 vty_out(vty
, "IPv4 %-12d%-12d%-12d\n", client
->v4_route_add_cnt
,
3571 client
->v4_route_upd8_cnt
, client
->v4_route_del_cnt
);
3572 vty_out(vty
, "IPv6 %-12d%-12d%-12d\n", client
->v6_route_add_cnt
,
3573 client
->v6_route_upd8_cnt
, client
->v6_route_del_cnt
);
3574 vty_out(vty
, "Redist:v4 %-12d%-12d%-12d\n", client
->redist_v4_add_cnt
,
3575 0, client
->redist_v4_del_cnt
);
3576 vty_out(vty
, "Redist:v6 %-12d%-12d%-12d\n", client
->redist_v6_add_cnt
,
3577 0, client
->redist_v6_del_cnt
);
3578 vty_out(vty
, "Connected %-12d%-12d%-12d\n", client
->ifadd_cnt
, 0,
3580 vty_out(vty
, "BFD peer %-12d%-12d%-12d\n", client
->bfd_peer_add_cnt
,
3581 client
->bfd_peer_upd8_cnt
, client
->bfd_peer_del_cnt
);
3582 vty_out(vty
, "Interface Up Notifications: %d\n", client
->ifup_cnt
);
3583 vty_out(vty
, "Interface Down Notifications: %d\n", client
->ifdown_cnt
);
3584 vty_out(vty
, "VNI add notifications: %d\n", client
->vniadd_cnt
);
3585 vty_out(vty
, "VNI delete notifications: %d\n", client
->vnidel_cnt
);
3586 vty_out(vty
, "L3-VNI add notifications: %d\n", client
->l3vniadd_cnt
);
3587 vty_out(vty
, "L3-VNI delete notifications: %d\n", client
->l3vnidel_cnt
);
3588 vty_out(vty
, "MAC-IP add notifications: %d\n", client
->macipadd_cnt
);
3589 vty_out(vty
, "MAC-IP delete notifications: %d\n", client
->macipdel_cnt
);
3595 static void zebra_show_client_brief(struct vty
*vty
, struct zserv
*client
)
3597 char cbuf
[ZEBRA_TIME_BUF
], rbuf
[ZEBRA_TIME_BUF
];
3598 char wbuf
[ZEBRA_TIME_BUF
];
3600 vty_out(vty
, "%-8s%12s %12s%12s%8d/%-8d%8d/%-8d\n",
3601 zebra_route_string(client
->proto
),
3602 zserv_time_buf(&client
->connect_time
, cbuf
, ZEBRA_TIME_BUF
),
3603 zserv_time_buf(&client
->last_read_time
, rbuf
, ZEBRA_TIME_BUF
),
3604 zserv_time_buf(&client
->last_write_time
, wbuf
, ZEBRA_TIME_BUF
),
3605 client
->v4_route_add_cnt
+ client
->v4_route_upd8_cnt
,
3606 client
->v4_route_del_cnt
,
3607 client
->v6_route_add_cnt
+ client
->v6_route_upd8_cnt
,
3608 client
->v6_route_del_cnt
);
3611 struct zserv
*zebra_find_client(uint8_t proto
, unsigned short instance
)
3613 struct listnode
*node
, *nnode
;
3614 struct zserv
*client
;
3616 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, node
, nnode
, client
)) {
3617 if (client
->proto
== proto
&& client
->instance
== instance
)
3624 /* This command is for debugging purpose. */
3625 DEFUN (show_zebra_client
,
3626 show_zebra_client_cmd
,
3627 "show zebra client",
3630 "Client information\n")
3632 struct listnode
*node
;
3633 struct zserv
*client
;
3635 for (ALL_LIST_ELEMENTS_RO(zebrad
.client_list
, node
, client
))
3636 zebra_show_client_detail(vty
, client
);
3641 /* This command is for debugging purpose. */
3642 DEFUN (show_zebra_client_summary
,
3643 show_zebra_client_summary_cmd
,
3644 "show zebra client summary",
3647 "Client information brief\n"
3650 struct listnode
*node
;
3651 struct zserv
*client
;
3654 "Name Connect Time Last Read Last Write IPv4 Routes IPv6 Routes \n");
3656 "--------------------------------------------------------------------------------\n");
3658 for (ALL_LIST_ELEMENTS_RO(zebrad
.client_list
, node
, client
))
3659 zebra_show_client_brief(vty
, client
);
3661 vty_out(vty
, "Routes column shows (added+updated)/deleted\n");
3665 #if defined(HANDLE_ZAPI_FUZZING)
3666 void zserv_read_file(char *input
)
3669 struct zserv
*client
= NULL
;
3672 zebra_client_create(-1);
3673 client
= zebrad
.client_list
->head
->data
;
3676 fd
= open(input
, O_RDONLY
| O_NONBLOCK
);
3679 zebra_client_read(&t
);
3685 void zserv_init(void)
3687 /* Client list init. */
3688 zebrad
.client_list
= list_new();
3689 zebrad
.client_list
->del
= (void (*)(void *))zebra_client_free
;
3691 install_element(ENABLE_NODE
, &show_zebra_client_cmd
);
3692 install_element(ENABLE_NODE
, &show_zebra_client_summary_cmd
);