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
->rule
.seq
);
745 stream_putl(s
, rule
->rule
.priority
);
746 stream_putl(s
, rule
->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 void zsend_iptable_notify_owner(struct zebra_pbr_iptable
*iptable
,
821 enum zapi_iptable_notify_owner note
)
823 struct listnode
*node
;
824 struct zserv
*client
;
827 if (IS_ZEBRA_DEBUG_PACKET
)
828 zlog_debug("%s: Notifying %u", __PRETTY_FUNCTION__
,
831 for (ALL_LIST_ELEMENTS_RO(zebrad
.client_list
, node
, client
)) {
832 if (iptable
->sock
== client
->sock
)
839 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
841 zclient_create_header(s
, ZEBRA_IPTABLE_NOTIFY_OWNER
, VRF_DEFAULT
);
842 stream_put(s
, ¬e
, sizeof(note
));
843 stream_putl(s
, iptable
->unique
);
844 stream_putw_at(s
, 0, stream_get_endp(s
));
846 zebra_server_send_message(client
, s
);
849 /* Router-id is updated. Send ZEBRA_ROUTER_ID_ADD to client. */
850 int zsend_router_id_update(struct zserv
*client
, struct prefix
*p
,
855 /* Check this client need interface information. */
856 if (!vrf_bitmap_check(client
->ridinfo
, vrf_id
))
859 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
862 zclient_create_header(s
, ZEBRA_ROUTER_ID_UPDATE
, vrf_id
);
864 /* Prefix information. */
865 stream_putc(s
, p
->family
);
866 blen
= prefix_blen(p
);
867 stream_put(s
, &p
->u
.prefix
, blen
);
868 stream_putc(s
, p
->prefixlen
);
870 /* Write packet size. */
871 stream_putw_at(s
, 0, stream_get_endp(s
));
873 return zebra_server_send_message(client
, s
);
877 * Function used by Zebra to send a PW status update to LDP daemon
879 int zsend_pw_update(struct zserv
*client
, struct zebra_pw
*pw
)
881 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
883 zclient_create_header(s
, ZEBRA_PW_STATUS_UPDATE
, pw
->vrf_id
);
884 stream_write(s
, pw
->ifname
, IF_NAMESIZE
);
885 stream_putl(s
, pw
->ifindex
);
886 stream_putl(s
, pw
->status
);
888 /* Put length at the first point of the stream. */
889 stream_putw_at(s
, 0, stream_get_endp(s
));
891 return zebra_server_send_message(client
, s
);
894 /* Send response to a get label chunk request to client */
895 static int zsend_assign_label_chunk_response(struct zserv
*client
,
897 struct label_manager_chunk
*lmc
)
900 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
902 zclient_create_header(s
, ZEBRA_GET_LABEL_CHUNK
, vrf_id
);
906 stream_putc(s
, lmc
->keep
);
907 /* start and end labels */
908 stream_putl(s
, lmc
->start
);
909 stream_putl(s
, lmc
->end
);
912 /* Write packet size. */
913 stream_putw_at(s
, 0, stream_get_endp(s
));
915 ret
= writen(client
->sock
, s
->data
, stream_get_endp(s
));
920 /* Send response to a label manager connect request to client */
921 static int zsend_label_manager_connect_response(struct zserv
*client
,
923 unsigned short result
)
926 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
928 zclient_create_header(s
, ZEBRA_LABEL_MANAGER_CONNECT
, vrf_id
);
931 stream_putc(s
, result
);
933 /* Write packet size. */
934 stream_putw_at(s
, 0, stream_get_endp(s
));
936 ret
= writen(client
->sock
, s
->data
, stream_get_endp(s
));
942 /* Inbound message handling ------------------------------------------------ */
945 [ZEBRA_NEXTHOP_REGISTER
] = RNH_NEXTHOP_TYPE
,
946 [ZEBRA_NEXTHOP_UNREGISTER
] = RNH_NEXTHOP_TYPE
,
947 [ZEBRA_IMPORT_ROUTE_REGISTER
] = RNH_IMPORT_CHECK_TYPE
,
948 [ZEBRA_IMPORT_ROUTE_UNREGISTER
] = RNH_IMPORT_CHECK_TYPE
,
951 /* Nexthop register */
952 static void zread_rnh_register(ZAPI_HANDLER_ARGS
)
957 unsigned short l
= 0;
959 uint16_t type
= cmd2type
[hdr
->command
];
961 if (IS_ZEBRA_DEBUG_NHT
)
963 "rnh_register msg from client %s: hdr->length=%d, type=%s vrf=%u\n",
964 zebra_route_string(client
->proto
), hdr
->length
,
965 (type
== RNH_NEXTHOP_TYPE
) ? "nexthop" : "route",
970 client
->nh_reg_time
= monotime(NULL
);
972 while (l
< hdr
->length
) {
973 STREAM_GETC(s
, flags
);
974 STREAM_GETW(s
, p
.family
);
975 STREAM_GETC(s
, p
.prefixlen
);
977 if (p
.family
== AF_INET
) {
978 if (p
.prefixlen
> IPV4_MAX_BITLEN
) {
980 "%s: Specified prefix hdr->length %d is too large for a v4 address",
981 __PRETTY_FUNCTION__
, p
.prefixlen
);
984 STREAM_GET(&p
.u
.prefix4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
985 l
+= IPV4_MAX_BYTELEN
;
986 } else if (p
.family
== AF_INET6
) {
987 if (p
.prefixlen
> IPV6_MAX_BITLEN
) {
989 "%s: Specified prefix hdr->length %d is to large for a v6 address",
990 __PRETTY_FUNCTION__
, p
.prefixlen
);
993 STREAM_GET(&p
.u
.prefix6
, s
, IPV6_MAX_BYTELEN
);
994 l
+= IPV6_MAX_BYTELEN
;
997 "rnh_register: Received unknown family type %d\n",
1001 rnh
= zebra_add_rnh(&p
, zvrf_id(zvrf
), type
);
1002 if (type
== RNH_NEXTHOP_TYPE
) {
1004 && !CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
))
1005 SET_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
);
1007 && CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
))
1008 UNSET_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
);
1009 } else if (type
== RNH_IMPORT_CHECK_TYPE
) {
1011 && !CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
))
1012 SET_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
);
1014 && CHECK_FLAG(rnh
->flags
,
1015 ZEBRA_NHT_EXACT_MATCH
))
1016 UNSET_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
);
1019 zebra_add_rnh_client(rnh
, client
, type
, zvrf_id(zvrf
));
1020 /* Anything not AF_INET/INET6 has been filtered out above */
1021 zebra_evaluate_rnh(zvrf_id(zvrf
), p
.family
, 1, type
, &p
);
1028 /* Nexthop register */
1029 static void zread_rnh_unregister(ZAPI_HANDLER_ARGS
)
1034 unsigned short l
= 0;
1035 uint16_t type
= cmd2type
[hdr
->command
];
1037 if (IS_ZEBRA_DEBUG_NHT
)
1039 "rnh_unregister msg from client %s: hdr->length=%d vrf: %u\n",
1040 zebra_route_string(client
->proto
), hdr
->length
,
1045 while (l
< hdr
->length
) {
1048 STREAM_GETC(s
, flags
);
1050 goto stream_failure
;
1052 STREAM_GETW(s
, p
.family
);
1053 STREAM_GETC(s
, p
.prefixlen
);
1055 if (p
.family
== AF_INET
) {
1056 if (p
.prefixlen
> IPV4_MAX_BITLEN
) {
1058 "%s: Specified prefix hdr->length %d is to large for a v4 address",
1059 __PRETTY_FUNCTION__
, p
.prefixlen
);
1062 STREAM_GET(&p
.u
.prefix4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
1063 l
+= IPV4_MAX_BYTELEN
;
1064 } else if (p
.family
== AF_INET6
) {
1065 if (p
.prefixlen
> IPV6_MAX_BITLEN
) {
1067 "%s: Specified prefix hdr->length %d is to large for a v6 address",
1068 __PRETTY_FUNCTION__
, p
.prefixlen
);
1071 STREAM_GET(&p
.u
.prefix6
, s
, IPV6_MAX_BYTELEN
);
1072 l
+= IPV6_MAX_BYTELEN
;
1075 "rnh_register: Received unknown family type %d\n",
1079 rnh
= zebra_lookup_rnh(&p
, zvrf_id(zvrf
), type
);
1081 client
->nh_dereg_time
= monotime(NULL
);
1082 zebra_remove_rnh_client(rnh
, client
, type
);
1089 #define ZEBRA_MIN_FEC_LENGTH 5
1092 static void zread_fec_register(ZAPI_HANDLER_ARGS
)
1095 unsigned short l
= 0;
1098 uint32_t label_index
= MPLS_INVALID_LABEL_INDEX
;
1101 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1103 return; // unexpected
1106 * The minimum amount of data that can be sent for one fec
1109 if (hdr
->length
< ZEBRA_MIN_FEC_LENGTH
) {
1111 "fec_register: Received a fec register of hdr->length %d, it is of insufficient size to properly decode",
1116 while (l
< hdr
->length
) {
1117 STREAM_GETW(s
, flags
);
1118 memset(&p
, 0, sizeof(p
));
1119 STREAM_GETW(s
, p
.family
);
1120 if (p
.family
!= AF_INET
&& p
.family
!= AF_INET6
) {
1122 "fec_register: Received unknown family type %d\n",
1126 STREAM_GETC(s
, p
.prefixlen
);
1127 if ((p
.family
== AF_INET
&& p
.prefixlen
> IPV4_MAX_BITLEN
)
1128 || (p
.family
== AF_INET6
1129 && p
.prefixlen
> IPV6_MAX_BITLEN
)) {
1131 "%s: Specified prefix hdr->length: %d is to long for %d",
1132 __PRETTY_FUNCTION__
, p
.prefixlen
, p
.family
);
1136 STREAM_GET(&p
.u
.prefix
, s
, PSIZE(p
.prefixlen
));
1137 l
+= PSIZE(p
.prefixlen
);
1138 if (flags
& ZEBRA_FEC_REGISTER_LABEL_INDEX
) {
1139 STREAM_GETL(s
, label_index
);
1142 label_index
= MPLS_INVALID_LABEL_INDEX
;
1143 zebra_mpls_fec_register(zvrf
, &p
, label_index
, client
);
1150 /* FEC unregister */
1151 static void zread_fec_unregister(ZAPI_HANDLER_ARGS
)
1154 unsigned short l
= 0;
1159 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1161 return; // unexpected
1164 * The minimum amount of data that can be sent for one
1165 * fec unregistration
1167 if (hdr
->length
< ZEBRA_MIN_FEC_LENGTH
) {
1169 "fec_unregister: Received a fec unregister of hdr->length %d, it is of insufficient size to properly decode",
1174 while (l
< hdr
->length
) {
1175 STREAM_GETW(s
, flags
);
1177 goto stream_failure
;
1179 memset(&p
, 0, sizeof(p
));
1180 STREAM_GETW(s
, p
.family
);
1181 if (p
.family
!= AF_INET
&& p
.family
!= AF_INET6
) {
1183 "fec_unregister: Received unknown family type %d\n",
1187 STREAM_GETC(s
, p
.prefixlen
);
1188 if ((p
.family
== AF_INET
&& p
.prefixlen
> IPV4_MAX_BITLEN
)
1189 || (p
.family
== AF_INET6
1190 && p
.prefixlen
> IPV6_MAX_BITLEN
)) {
1192 "%s: Received prefix hdr->length %d which is greater than %d can support",
1193 __PRETTY_FUNCTION__
, p
.prefixlen
, p
.family
);
1197 STREAM_GET(&p
.u
.prefix
, s
, PSIZE(p
.prefixlen
));
1198 l
+= PSIZE(p
.prefixlen
);
1199 zebra_mpls_fec_unregister(zvrf
, &p
, client
);
1208 * Register zebra server interface information.
1209 * Send current all interface and address information.
1211 static void zread_interface_add(ZAPI_HANDLER_ARGS
)
1214 struct interface
*ifp
;
1216 /* Interface information is needed. */
1217 vrf_bitmap_set(client
->ifinfo
, zvrf_id(zvrf
));
1219 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
1220 FOR_ALL_INTERFACES (vrf
, ifp
) {
1221 /* Skip pseudo interface. */
1222 if (!CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_ACTIVE
))
1225 zsend_interface_add(client
, ifp
);
1226 zsend_interface_addresses(client
, ifp
);
1231 /* Unregister zebra server interface information. */
1232 static void zread_interface_delete(ZAPI_HANDLER_ARGS
)
1234 vrf_bitmap_unset(client
->ifinfo
, zvrf_id(zvrf
));
1237 void zserv_nexthop_num_warn(const char *caller
, const struct prefix
*p
,
1238 const unsigned int nexthop_num
)
1240 if (nexthop_num
> multipath_num
) {
1241 char buff
[PREFIX2STR_BUFFER
];
1242 prefix2str(p
, buff
, sizeof(buff
));
1244 "%s: Prefix %s has %d nexthops, but we can only use the first %d",
1245 caller
, buff
, nexthop_num
, multipath_num
);
1249 static void zread_route_add(ZAPI_HANDLER_ARGS
)
1252 struct zapi_route api
;
1253 struct zapi_nexthop
*api_nh
;
1255 struct prefix_ipv6
*src_p
= NULL
;
1256 struct route_entry
*re
;
1257 struct nexthop
*nexthop
= NULL
;
1259 vrf_id_t vrf_id
= 0;
1260 struct ipaddr vtep_ip
;
1263 if (zapi_route_decode(s
, &api
) < 0) {
1264 if (IS_ZEBRA_DEBUG_RECV
)
1265 zlog_debug("%s: Unable to decode zapi_route sent",
1266 __PRETTY_FUNCTION__
);
1270 if (IS_ZEBRA_DEBUG_RECV
) {
1271 char buf_prefix
[PREFIX_STRLEN
];
1272 prefix2str(&api
.prefix
, buf_prefix
, sizeof(buf_prefix
));
1273 zlog_debug("%s: p=%s, ZAPI_MESSAGE_LABEL: %sset, flags=0x%x",
1274 __func__
, buf_prefix
,
1275 (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_LABEL
) ? ""
1280 /* Allocate new route. */
1281 vrf_id
= zvrf_id(zvrf
);
1282 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
1283 re
->type
= api
.type
;
1284 re
->instance
= api
.instance
;
1285 re
->flags
= api
.flags
;
1286 re
->uptime
= time(NULL
);
1287 re
->vrf_id
= vrf_id
;
1288 if (api
.tableid
&& vrf_id
== VRF_DEFAULT
)
1289 re
->table
= api
.tableid
;
1291 re
->table
= zvrf
->table_id
;
1294 * TBD should _all_ of the nexthop add operations use
1295 * api_nh->vrf_id instead of re->vrf_id ? I only changed
1296 * for cases NEXTHOP_TYPE_IPV4 and NEXTHOP_TYPE_IPV6.
1298 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
)) {
1299 for (i
= 0; i
< api
.nexthop_num
; i
++) {
1300 api_nh
= &api
.nexthops
[i
];
1301 ifindex_t ifindex
= 0;
1303 if (IS_ZEBRA_DEBUG_RECV
) {
1304 zlog_debug("nh type %d", api_nh
->type
);
1307 switch (api_nh
->type
) {
1308 case NEXTHOP_TYPE_IFINDEX
:
1309 nexthop
= route_entry_nexthop_ifindex_add(
1310 re
, api_nh
->ifindex
, api_nh
->vrf_id
);
1312 case NEXTHOP_TYPE_IPV4
:
1313 if (IS_ZEBRA_DEBUG_RECV
) {
1314 char nhbuf
[INET6_ADDRSTRLEN
] = {0};
1315 inet_ntop(AF_INET
, &api_nh
->gate
.ipv4
,
1316 nhbuf
, INET6_ADDRSTRLEN
);
1317 zlog_debug("%s: nh=%s, vrf_id=%d",
1321 nexthop
= route_entry_nexthop_ipv4_add(
1322 re
, &api_nh
->gate
.ipv4
, NULL
,
1325 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1327 memset(&vtep_ip
, 0, sizeof(struct ipaddr
));
1328 if (CHECK_FLAG(api
.flags
,
1329 ZEBRA_FLAG_EVPN_ROUTE
)) {
1330 ifindex
= get_l3vni_svi_ifindex(vrf_id
);
1332 ifindex
= api_nh
->ifindex
;
1335 if (IS_ZEBRA_DEBUG_RECV
) {
1336 char nhbuf
[INET6_ADDRSTRLEN
] = {0};
1337 inet_ntop(AF_INET
, &api_nh
->gate
.ipv4
,
1338 nhbuf
, INET6_ADDRSTRLEN
);
1340 "%s: nh=%s, vrf_id=%d (re->vrf_id=%d), ifindex=%d",
1341 __func__
, nhbuf
, api_nh
->vrf_id
,
1342 re
->vrf_id
, ifindex
);
1344 nexthop
= route_entry_nexthop_ipv4_ifindex_add(
1345 re
, &api_nh
->gate
.ipv4
, NULL
, ifindex
,
1348 /* if this an EVPN route entry,
1349 * program the nh as neigh
1351 if (CHECK_FLAG(api
.flags
,
1352 ZEBRA_FLAG_EVPN_ROUTE
)) {
1353 SET_FLAG(nexthop
->flags
,
1354 NEXTHOP_FLAG_EVPN_RVTEP
);
1355 vtep_ip
.ipa_type
= IPADDR_V4
;
1356 memcpy(&(vtep_ip
.ipaddr_v4
),
1357 &(api_nh
->gate
.ipv4
),
1358 sizeof(struct in_addr
));
1359 zebra_vxlan_evpn_vrf_route_add(
1360 vrf_id
, &api
.rmac
, &vtep_ip
,
1364 case NEXTHOP_TYPE_IPV6
:
1365 nexthop
= route_entry_nexthop_ipv6_add(
1366 re
, &api_nh
->gate
.ipv6
, api_nh
->vrf_id
);
1368 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1369 memset(&vtep_ip
, 0, sizeof(struct ipaddr
));
1370 if (CHECK_FLAG(api
.flags
,
1371 ZEBRA_FLAG_EVPN_ROUTE
)) {
1373 get_l3vni_svi_ifindex(vrf_id
);
1375 ifindex
= api_nh
->ifindex
;
1378 nexthop
= route_entry_nexthop_ipv6_ifindex_add(
1379 re
, &api_nh
->gate
.ipv6
, ifindex
,
1382 /* if this an EVPN route entry,
1383 * program the nh as neigh
1385 if (CHECK_FLAG(api
.flags
,
1386 ZEBRA_FLAG_EVPN_ROUTE
)) {
1387 SET_FLAG(nexthop
->flags
,
1388 NEXTHOP_FLAG_EVPN_RVTEP
);
1389 vtep_ip
.ipa_type
= IPADDR_V6
;
1390 memcpy(&vtep_ip
.ipaddr_v6
,
1391 &(api_nh
->gate
.ipv6
),
1392 sizeof(struct in6_addr
));
1393 zebra_vxlan_evpn_vrf_route_add(
1400 case NEXTHOP_TYPE_BLACKHOLE
:
1401 nexthop
= route_entry_nexthop_blackhole_add(
1402 re
, api_nh
->bh_type
);
1408 "%s: Nexthops Specified: %d but we failed to properly create one",
1409 __PRETTY_FUNCTION__
, api
.nexthop_num
);
1410 nexthops_free(re
->ng
.nexthop
);
1411 XFREE(MTYPE_RE
, re
);
1414 /* MPLS labels for BGP-LU or Segment Routing */
1415 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_LABEL
)
1416 && api_nh
->type
!= NEXTHOP_TYPE_IFINDEX
1417 && api_nh
->type
!= NEXTHOP_TYPE_BLACKHOLE
) {
1418 enum lsp_types_t label_type
;
1421 lsp_type_from_re_type(client
->proto
);
1423 if (IS_ZEBRA_DEBUG_RECV
) {
1425 "%s: adding %d labels of type %d (1st=%u)",
1426 __func__
, api_nh
->label_num
,
1427 label_type
, api_nh
->labels
[0]);
1430 nexthop_add_labels(nexthop
, label_type
,
1432 &api_nh
->labels
[0]);
1437 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
))
1438 re
->distance
= api
.distance
;
1439 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
))
1440 re
->metric
= api
.metric
;
1441 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_TAG
))
1443 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_MTU
))
1446 afi
= family2afi(api
.prefix
.family
);
1447 if (afi
!= AFI_IP6
&& CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
)) {
1448 zlog_warn("%s: Received SRC Prefix but afi is not v6",
1449 __PRETTY_FUNCTION__
);
1450 nexthops_free(re
->ng
.nexthop
);
1451 XFREE(MTYPE_RE
, re
);
1454 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
))
1455 src_p
= &api
.src_prefix
;
1457 ret
= rib_add_multipath(afi
, api
.safi
, &api
.prefix
, src_p
, re
);
1460 switch (api
.prefix
.family
) {
1463 client
->v4_route_add_cnt
++;
1465 client
->v4_route_upd8_cnt
++;
1469 client
->v6_route_add_cnt
++;
1471 client
->v6_route_upd8_cnt
++;
1476 static void zread_route_del(ZAPI_HANDLER_ARGS
)
1479 struct zapi_route api
;
1481 struct prefix_ipv6
*src_p
= NULL
;
1485 if (zapi_route_decode(s
, &api
) < 0)
1488 afi
= family2afi(api
.prefix
.family
);
1489 if (afi
!= AFI_IP6
&& CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
)) {
1490 zlog_warn("%s: Received a src prefix while afi is not v6",
1491 __PRETTY_FUNCTION__
);
1494 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
))
1495 src_p
= &api
.src_prefix
;
1497 if (api
.vrf_id
== VRF_DEFAULT
&& api
.tableid
!= 0)
1498 table_id
= api
.tableid
;
1500 table_id
= zvrf
->table_id
;
1502 rib_delete(afi
, api
.safi
, zvrf_id(zvrf
), api
.type
, api
.instance
,
1503 api
.flags
, &api
.prefix
, src_p
, NULL
, table_id
,
1504 api
.metric
, false, &api
.rmac
);
1507 switch (api
.prefix
.family
) {
1509 client
->v4_route_del_cnt
++;
1512 client
->v6_route_del_cnt
++;
1517 /* This function support multiple nexthop. */
1519 * Parse the ZEBRA_IPV4_ROUTE_ADD sent from client. Update re and
1522 static void zread_ipv4_add(ZAPI_HANDLER_ARGS
)
1525 struct route_entry
*re
;
1528 struct in_addr nhop_addr
;
1529 uint8_t nexthop_num
;
1530 uint8_t nexthop_type
;
1535 enum lsp_types_t label_type
= ZEBRA_LSP_NONE
;
1537 struct nexthop
*nexthop
;
1538 enum blackhole_type bh_type
= BLACKHOLE_NULL
;
1540 /* Get input stream. */
1543 /* Allocate new re. */
1544 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
1546 /* Type, flags, message. */
1547 STREAM_GETC(s
, re
->type
);
1548 if (re
->type
> ZEBRA_ROUTE_MAX
) {
1549 zlog_warn("%s: Specified route type %d is not a legal value\n",
1550 __PRETTY_FUNCTION__
, re
->type
);
1551 XFREE(MTYPE_RE
, re
);
1554 STREAM_GETW(s
, re
->instance
);
1555 STREAM_GETL(s
, re
->flags
);
1556 STREAM_GETC(s
, message
);
1557 STREAM_GETW(s
, safi
);
1558 re
->uptime
= time(NULL
);
1561 memset(&p
, 0, sizeof(struct prefix_ipv4
));
1563 STREAM_GETC(s
, p
.prefixlen
);
1564 if (p
.prefixlen
> IPV4_MAX_BITLEN
) {
1566 "%s: Specified prefix length %d is greater than what v4 can be",
1567 __PRETTY_FUNCTION__
, p
.prefixlen
);
1568 XFREE(MTYPE_RE
, re
);
1571 STREAM_GET(&p
.u
.prefix4
, s
, PSIZE(p
.prefixlen
));
1574 re
->vrf_id
= zvrf_id(zvrf
);
1576 /* Nexthop parse. */
1577 if (CHECK_FLAG(message
, ZAPI_MESSAGE_NEXTHOP
)) {
1578 STREAM_GETC(s
, nexthop_num
);
1579 zserv_nexthop_num_warn(__func__
, (const struct prefix
*)&p
,
1582 if (CHECK_FLAG(message
, ZAPI_MESSAGE_LABEL
))
1583 label_type
= lsp_type_from_re_type(client
->proto
);
1585 for (i
= 0; i
< nexthop_num
; i
++) {
1586 STREAM_GETC(s
, nexthop_type
);
1588 switch (nexthop_type
) {
1589 case NEXTHOP_TYPE_IFINDEX
:
1590 STREAM_GETL(s
, ifindex
);
1591 route_entry_nexthop_ifindex_add(re
, ifindex
,
1594 case NEXTHOP_TYPE_IPV4
:
1595 STREAM_GET(&nhop_addr
.s_addr
, s
,
1597 nexthop
= route_entry_nexthop_ipv4_add(
1598 re
, &nhop_addr
, NULL
, re
->vrf_id
);
1599 /* For labeled-unicast, each nexthop is followed
1601 if (CHECK_FLAG(message
, ZAPI_MESSAGE_LABEL
)) {
1602 STREAM_GETL(s
, label
);
1603 nexthop_add_labels(nexthop
, label_type
,
1607 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1608 STREAM_GET(&nhop_addr
.s_addr
, s
,
1610 STREAM_GETL(s
, ifindex
);
1611 route_entry_nexthop_ipv4_ifindex_add(
1612 re
, &nhop_addr
, NULL
, ifindex
,
1615 case NEXTHOP_TYPE_IPV6
:
1617 "%s: Please use ZEBRA_ROUTE_ADD if you want to pass v6 nexthops",
1618 __PRETTY_FUNCTION__
);
1619 nexthops_free(re
->ng
.nexthop
);
1620 XFREE(MTYPE_RE
, re
);
1623 case NEXTHOP_TYPE_BLACKHOLE
:
1624 route_entry_nexthop_blackhole_add(re
, bh_type
);
1628 "%s: Specified nexthop type: %d does not exist",
1629 __PRETTY_FUNCTION__
, nexthop_type
);
1630 nexthops_free(re
->ng
.nexthop
);
1631 XFREE(MTYPE_RE
, re
);
1638 if (CHECK_FLAG(message
, ZAPI_MESSAGE_DISTANCE
))
1639 STREAM_GETC(s
, re
->distance
);
1642 if (CHECK_FLAG(message
, ZAPI_MESSAGE_METRIC
))
1643 STREAM_GETL(s
, re
->metric
);
1646 if (CHECK_FLAG(message
, ZAPI_MESSAGE_TAG
))
1647 STREAM_GETL(s
, re
->tag
);
1651 if (CHECK_FLAG(message
, ZAPI_MESSAGE_MTU
))
1652 STREAM_GETL(s
, re
->mtu
);
1657 re
->table
= zvrf
->table_id
;
1659 ret
= rib_add_multipath(AFI_IP
, safi
, &p
, NULL
, re
);
1663 client
->v4_route_add_cnt
++;
1665 client
->v4_route_upd8_cnt
++;
1670 nexthops_free(re
->ng
.nexthop
);
1671 XFREE(MTYPE_RE
, re
);
1674 /* Zebra server IPv4 prefix delete function. */
1675 static void zread_ipv4_delete(ZAPI_HANDLER_ARGS
)
1678 struct zapi_ipv4 api
;
1684 /* Type, flags, message. */
1685 STREAM_GETC(s
, api
.type
);
1686 STREAM_GETW(s
, api
.instance
);
1687 STREAM_GETL(s
, api
.flags
);
1688 STREAM_GETC(s
, api
.message
);
1689 STREAM_GETW(s
, api
.safi
);
1692 memset(&p
, 0, sizeof(struct prefix
));
1694 STREAM_GETC(s
, p
.prefixlen
);
1695 if (p
.prefixlen
> IPV4_MAX_BITLEN
) {
1696 zlog_warn("%s: Passed in prefixlen %d is impossible",
1697 __PRETTY_FUNCTION__
, p
.prefixlen
);
1700 STREAM_GET(&p
.u
.prefix4
, s
, PSIZE(p
.prefixlen
));
1702 table_id
= zvrf
->table_id
;
1704 rib_delete(AFI_IP
, api
.safi
, zvrf_id(zvrf
), api
.type
, api
.instance
,
1705 api
.flags
, &p
, NULL
, NULL
, table_id
, 0, false, NULL
);
1706 client
->v4_route_del_cnt
++;
1712 /* MRIB Nexthop lookup for IPv4. */
1713 static void zread_ipv4_nexthop_lookup_mrib(ZAPI_HANDLER_ARGS
)
1715 struct in_addr addr
;
1716 struct route_entry
*re
;
1718 STREAM_GET(&addr
.s_addr
, msg
, IPV4_MAX_BYTELEN
);
1719 re
= rib_match_ipv4_multicast(zvrf_id(zvrf
), addr
, NULL
);
1720 zsend_ipv4_nexthop_lookup_mrib(client
, addr
, re
, zvrf
);
1726 /* Zebra server IPv6 prefix add function. */
1727 static void zread_ipv4_route_ipv6_nexthop_add(ZAPI_HANDLER_ARGS
)
1731 struct in6_addr nhop_addr
;
1732 struct route_entry
*re
;
1734 uint8_t nexthop_num
;
1735 uint8_t nexthop_type
;
1738 static struct in6_addr nexthops
[MULTIPATH_NUM
];
1739 static unsigned int ifindices
[MULTIPATH_NUM
];
1741 static mpls_label_t labels
[MULTIPATH_NUM
];
1742 enum lsp_types_t label_type
= ZEBRA_LSP_NONE
;
1744 struct nexthop
*nexthop
;
1745 enum blackhole_type bh_type
= BLACKHOLE_NULL
;
1747 /* Get input stream. */
1750 memset(&nhop_addr
, 0, sizeof(struct in6_addr
));
1752 /* Allocate new re. */
1753 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
1755 /* Type, flags, message. */
1756 STREAM_GETC(s
, re
->type
);
1757 if (re
->type
> ZEBRA_ROUTE_MAX
) {
1758 zlog_warn("%s: Specified route type: %d is not a legal value\n",
1759 __PRETTY_FUNCTION__
, re
->type
);
1760 XFREE(MTYPE_RE
, re
);
1763 STREAM_GETW(s
, re
->instance
);
1764 STREAM_GETL(s
, re
->flags
);
1765 STREAM_GETC(s
, message
);
1766 STREAM_GETW(s
, safi
);
1767 re
->uptime
= time(NULL
);
1770 memset(&p
, 0, sizeof(struct prefix_ipv4
));
1772 STREAM_GETC(s
, p
.prefixlen
);
1773 if (p
.prefixlen
> IPV4_MAX_BITLEN
) {
1775 "%s: Prefix Length %d is greater than what a v4 address can use",
1776 __PRETTY_FUNCTION__
, p
.prefixlen
);
1777 XFREE(MTYPE_RE
, re
);
1780 STREAM_GET(&p
.u
.prefix4
, s
, PSIZE(p
.prefixlen
));
1783 re
->vrf_id
= zvrf_id(zvrf
);
1785 /* We need to give nh-addr, nh-ifindex with the same next-hop object
1786 * to the re to ensure that IPv6 multipathing works; need to coalesce
1787 * these. Clients should send the same number of paired set of
1788 * next-hop-addr/next-hop-ifindices. */
1789 if (CHECK_FLAG(message
, ZAPI_MESSAGE_NEXTHOP
)) {
1790 unsigned int nh_count
= 0;
1791 unsigned int if_count
= 0;
1792 unsigned int max_nh_if
= 0;
1794 STREAM_GETC(s
, nexthop_num
);
1795 zserv_nexthop_num_warn(__func__
, (const struct prefix
*)&p
,
1798 if (CHECK_FLAG(message
, ZAPI_MESSAGE_LABEL
))
1799 label_type
= lsp_type_from_re_type(client
->proto
);
1801 for (i
= 0; i
< nexthop_num
; i
++) {
1802 STREAM_GETC(s
, nexthop_type
);
1804 switch (nexthop_type
) {
1805 case NEXTHOP_TYPE_IPV6
:
1806 STREAM_GET(&nhop_addr
, s
, 16);
1807 if (nh_count
< MULTIPATH_NUM
) {
1808 /* For labeled-unicast, each nexthop is
1809 * followed by label. */
1810 if (CHECK_FLAG(message
,
1811 ZAPI_MESSAGE_LABEL
)) {
1812 STREAM_GETL(s
, label
);
1813 labels
[nh_count
] = label
;
1815 nexthops
[nh_count
] = nhop_addr
;
1819 case NEXTHOP_TYPE_IFINDEX
:
1820 if (if_count
< multipath_num
) {
1821 STREAM_GETL(s
, ifindices
[if_count
++]);
1824 case NEXTHOP_TYPE_BLACKHOLE
:
1825 route_entry_nexthop_blackhole_add(re
, bh_type
);
1829 "%s: Please use ZEBRA_ROUTE_ADD if you want to pass non v6 nexthops",
1830 __PRETTY_FUNCTION__
);
1831 nexthops_free(re
->ng
.nexthop
);
1832 XFREE(MTYPE_RE
, re
);
1837 max_nh_if
= (nh_count
> if_count
) ? nh_count
: if_count
;
1838 for (i
= 0; i
< max_nh_if
; i
++) {
1840 && !IN6_IS_ADDR_UNSPECIFIED(&nexthops
[i
])) {
1841 if ((i
< if_count
) && ifindices
[i
])
1843 route_entry_nexthop_ipv6_ifindex_add(
1848 nexthop
= route_entry_nexthop_ipv6_add(
1849 re
, &nexthops
[i
], re
->vrf_id
);
1851 if (CHECK_FLAG(message
, ZAPI_MESSAGE_LABEL
))
1852 nexthop_add_labels(nexthop
, label_type
,
1855 if ((i
< if_count
) && ifindices
[i
])
1856 route_entry_nexthop_ifindex_add(
1857 re
, ifindices
[i
], re
->vrf_id
);
1863 if (CHECK_FLAG(message
, ZAPI_MESSAGE_DISTANCE
))
1864 STREAM_GETC(s
, re
->distance
);
1867 if (CHECK_FLAG(message
, ZAPI_MESSAGE_METRIC
))
1868 STREAM_GETL(s
, re
->metric
);
1871 if (CHECK_FLAG(message
, ZAPI_MESSAGE_TAG
))
1872 STREAM_GETL(s
, re
->tag
);
1876 if (CHECK_FLAG(message
, ZAPI_MESSAGE_MTU
))
1877 STREAM_GETL(s
, re
->mtu
);
1882 re
->table
= zvrf
->table_id
;
1884 ret
= rib_add_multipath(AFI_IP6
, safi
, &p
, NULL
, re
);
1887 client
->v4_route_add_cnt
++;
1889 client
->v4_route_upd8_cnt
++;
1894 nexthops_free(re
->ng
.nexthop
);
1895 XFREE(MTYPE_RE
, re
);
1898 static void zread_ipv6_add(ZAPI_HANDLER_ARGS
)
1902 struct in6_addr nhop_addr
;
1904 struct route_entry
*re
;
1906 uint8_t nexthop_num
;
1907 uint8_t nexthop_type
;
1909 struct prefix_ipv6 src_p
, *src_pp
;
1911 static struct in6_addr nexthops
[MULTIPATH_NUM
];
1912 static unsigned int ifindices
[MULTIPATH_NUM
];
1914 static mpls_label_t labels
[MULTIPATH_NUM
];
1915 enum lsp_types_t label_type
= ZEBRA_LSP_NONE
;
1917 struct nexthop
*nexthop
;
1918 enum blackhole_type bh_type
= BLACKHOLE_NULL
;
1920 /* Get input stream. */
1923 memset(&nhop_addr
, 0, sizeof(struct in6_addr
));
1925 /* Allocate new re. */
1926 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
1928 /* Type, flags, message. */
1929 STREAM_GETC(s
, re
->type
);
1930 if (re
->type
> ZEBRA_ROUTE_MAX
) {
1931 zlog_warn("%s: Specified route type: %d is not a legal value\n",
1932 __PRETTY_FUNCTION__
, re
->type
);
1933 XFREE(MTYPE_RE
, re
);
1936 STREAM_GETW(s
, re
->instance
);
1937 STREAM_GETL(s
, re
->flags
);
1938 STREAM_GETC(s
, message
);
1939 STREAM_GETW(s
, safi
);
1940 re
->uptime
= time(NULL
);
1943 memset(&p
, 0, sizeof(p
));
1944 p
.family
= AF_INET6
;
1945 STREAM_GETC(s
, p
.prefixlen
);
1946 if (p
.prefixlen
> IPV6_MAX_BITLEN
) {
1948 "%s: Specified prefix length %d is to large for v6 prefix",
1949 __PRETTY_FUNCTION__
, p
.prefixlen
);
1950 XFREE(MTYPE_RE
, re
);
1953 STREAM_GET(&p
.u
.prefix6
, s
, PSIZE(p
.prefixlen
));
1955 if (CHECK_FLAG(message
, ZAPI_MESSAGE_SRCPFX
)) {
1956 memset(&src_p
, 0, sizeof(src_p
));
1957 src_p
.family
= AF_INET6
;
1958 STREAM_GETC(s
, src_p
.prefixlen
);
1959 if (src_p
.prefixlen
> IPV6_MAX_BITLEN
) {
1961 "%s: Specified src prefix length %d is to large for v6 prefix",
1962 __PRETTY_FUNCTION__
, src_p
.prefixlen
);
1963 XFREE(MTYPE_RE
, re
);
1966 STREAM_GET(&src_p
.prefix
, s
, PSIZE(src_p
.prefixlen
));
1972 re
->vrf_id
= zvrf_id(zvrf
);
1974 /* We need to give nh-addr, nh-ifindex with the same next-hop object
1975 * to the re to ensure that IPv6 multipathing works; need to coalesce
1976 * these. Clients should send the same number of paired set of
1977 * next-hop-addr/next-hop-ifindices. */
1978 if (CHECK_FLAG(message
, ZAPI_MESSAGE_NEXTHOP
)) {
1979 unsigned int nh_count
= 0;
1980 unsigned int if_count
= 0;
1981 unsigned int max_nh_if
= 0;
1983 STREAM_GETC(s
, nexthop_num
);
1984 zserv_nexthop_num_warn(__func__
, (const struct prefix
*)&p
,
1987 if (CHECK_FLAG(message
, ZAPI_MESSAGE_LABEL
))
1988 label_type
= lsp_type_from_re_type(client
->proto
);
1990 for (i
= 0; i
< nexthop_num
; i
++) {
1991 STREAM_GETC(s
, nexthop_type
);
1993 switch (nexthop_type
) {
1994 case NEXTHOP_TYPE_IPV6
:
1995 STREAM_GET(&nhop_addr
, s
, 16);
1996 if (nh_count
< MULTIPATH_NUM
) {
1997 /* For labeled-unicast, each nexthop is
1998 * followed by label. */
1999 if (CHECK_FLAG(message
,
2000 ZAPI_MESSAGE_LABEL
)) {
2001 STREAM_GETL(s
, label
);
2002 labels
[nh_count
] = label
;
2004 nexthops
[nh_count
++] = nhop_addr
;
2007 case NEXTHOP_TYPE_IPV6_IFINDEX
:
2008 STREAM_GET(&nhop_addr
, s
, 16);
2009 STREAM_GETL(s
, ifindex
);
2010 route_entry_nexthop_ipv6_ifindex_add(
2011 re
, &nhop_addr
, ifindex
, re
->vrf_id
);
2013 case NEXTHOP_TYPE_IFINDEX
:
2014 if (if_count
< multipath_num
) {
2015 STREAM_GETL(s
, ifindices
[if_count
++]);
2018 case NEXTHOP_TYPE_BLACKHOLE
:
2019 route_entry_nexthop_blackhole_add(re
, bh_type
);
2023 "%s: Please use ZEBRA_ROUTE_ADD if you want to pass non v6 nexthops",
2024 __PRETTY_FUNCTION__
);
2025 nexthops_free(re
->ng
.nexthop
);
2026 XFREE(MTYPE_RE
, re
);
2031 max_nh_if
= (nh_count
> if_count
) ? nh_count
: if_count
;
2032 for (i
= 0; i
< max_nh_if
; i
++) {
2034 && !IN6_IS_ADDR_UNSPECIFIED(&nexthops
[i
])) {
2035 if ((i
< if_count
) && ifindices
[i
])
2037 route_entry_nexthop_ipv6_ifindex_add(
2042 nexthop
= route_entry_nexthop_ipv6_add(
2043 re
, &nexthops
[i
], re
->vrf_id
);
2044 if (CHECK_FLAG(message
, ZAPI_MESSAGE_LABEL
))
2045 nexthop_add_labels(nexthop
, label_type
,
2048 if ((i
< if_count
) && ifindices
[i
])
2049 route_entry_nexthop_ifindex_add(
2050 re
, ifindices
[i
], re
->vrf_id
);
2056 if (CHECK_FLAG(message
, ZAPI_MESSAGE_DISTANCE
))
2057 STREAM_GETC(s
, re
->distance
);
2060 if (CHECK_FLAG(message
, ZAPI_MESSAGE_METRIC
))
2061 STREAM_GETL(s
, re
->metric
);
2064 if (CHECK_FLAG(message
, ZAPI_MESSAGE_TAG
))
2065 STREAM_GETL(s
, re
->tag
);
2069 if (CHECK_FLAG(message
, ZAPI_MESSAGE_MTU
))
2070 STREAM_GETL(s
, re
->mtu
);
2074 re
->table
= zvrf
->table_id
;
2076 ret
= rib_add_multipath(AFI_IP6
, safi
, &p
, src_pp
, re
);
2079 client
->v6_route_add_cnt
++;
2081 client
->v6_route_upd8_cnt
++;
2086 nexthops_free(re
->ng
.nexthop
);
2087 XFREE(MTYPE_RE
, re
);
2090 /* Zebra server IPv6 prefix delete function. */
2091 static void zread_ipv6_delete(ZAPI_HANDLER_ARGS
)
2094 struct zapi_ipv6 api
;
2096 struct prefix_ipv6 src_p
, *src_pp
;
2100 /* Type, flags, message. */
2101 STREAM_GETC(s
, api
.type
);
2102 STREAM_GETW(s
, api
.instance
);
2103 STREAM_GETL(s
, api
.flags
);
2104 STREAM_GETC(s
, api
.message
);
2105 STREAM_GETW(s
, api
.safi
);
2108 memset(&p
, 0, sizeof(struct prefix
));
2109 p
.family
= AF_INET6
;
2110 STREAM_GETC(s
, p
.prefixlen
);
2111 STREAM_GET(&p
.u
.prefix6
, s
, PSIZE(p
.prefixlen
));
2113 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
)) {
2114 memset(&src_p
, 0, sizeof(struct prefix_ipv6
));
2115 src_p
.family
= AF_INET6
;
2116 STREAM_GETC(s
, src_p
.prefixlen
);
2117 STREAM_GET(&src_p
.prefix
, s
, PSIZE(src_p
.prefixlen
));
2122 rib_delete(AFI_IP6
, api
.safi
, zvrf_id(zvrf
), api
.type
, api
.instance
,
2123 api
.flags
, &p
, src_pp
, NULL
, client
->rtm_table
, 0, false,
2126 client
->v6_route_del_cnt
++;
2132 /* Register zebra server router-id information. Send current router-id */
2133 static void zread_router_id_add(ZAPI_HANDLER_ARGS
)
2137 /* Router-id information is needed. */
2138 vrf_bitmap_set(client
->ridinfo
, zvrf_id(zvrf
));
2140 router_id_get(&p
, zvrf_id(zvrf
));
2142 zsend_router_id_update(client
, &p
, zvrf_id(zvrf
));
2145 /* Unregister zebra server router-id information. */
2146 static void zread_router_id_delete(ZAPI_HANDLER_ARGS
)
2148 vrf_bitmap_unset(client
->ridinfo
, zvrf_id(zvrf
));
2151 /* Tie up route-type and client->sock */
2152 static void zread_hello(ZAPI_HANDLER_ARGS
)
2154 /* type of protocol (lib/zebra.h) */
2156 unsigned short instance
;
2159 STREAM_GETC(msg
, proto
);
2160 STREAM_GETW(msg
, instance
);
2161 STREAM_GETC(msg
, notify
);
2163 client
->notify_owner
= true;
2165 /* accept only dynamic routing protocols */
2166 if ((proto
< ZEBRA_ROUTE_MAX
) && (proto
> ZEBRA_ROUTE_STATIC
)) {
2168 "client %d says hello and bids fair to announce only %s routes vrf=%u",
2169 client
->sock
, zebra_route_string(proto
),
2172 zlog_notice("client protocol instance %d", instance
);
2174 client
->proto
= proto
;
2175 client
->instance
= instance
;
2182 /* Unregister all information in a VRF. */
2183 static void zread_vrf_unregister(ZAPI_HANDLER_ARGS
)
2188 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
2189 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
2190 vrf_bitmap_unset(client
->redist
[afi
][i
], zvrf_id(zvrf
));
2191 vrf_bitmap_unset(client
->redist_default
, zvrf_id(zvrf
));
2192 vrf_bitmap_unset(client
->ifinfo
, zvrf_id(zvrf
));
2193 vrf_bitmap_unset(client
->ridinfo
, zvrf_id(zvrf
));
2196 static void zread_mpls_labels(ZAPI_HANDLER_ARGS
)
2199 enum lsp_types_t type
;
2200 struct prefix prefix
;
2201 enum nexthop_types_t gtype
;
2204 mpls_label_t in_label
, out_label
;
2207 /* Get input stream. */
2211 STREAM_GETC(s
, type
);
2212 STREAM_GETL(s
, prefix
.family
);
2213 switch (prefix
.family
) {
2215 STREAM_GET(&prefix
.u
.prefix4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
2216 STREAM_GETC(s
, prefix
.prefixlen
);
2217 if (prefix
.prefixlen
> IPV4_MAX_BITLEN
) {
2219 "%s: Specified prefix length %d is greater than a v4 address can support",
2220 __PRETTY_FUNCTION__
, prefix
.prefixlen
);
2223 STREAM_GET(&gate
.ipv4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
2226 STREAM_GET(&prefix
.u
.prefix6
, s
, 16);
2227 STREAM_GETC(s
, prefix
.prefixlen
);
2228 if (prefix
.prefixlen
> IPV6_MAX_BITLEN
) {
2230 "%s: Specified prefix length %d is greater than a v6 address can support",
2231 __PRETTY_FUNCTION__
, prefix
.prefixlen
);
2234 STREAM_GET(&gate
.ipv6
, s
, 16);
2237 zlog_warn("%s: Specified AF %d is not supported for this call",
2238 __PRETTY_FUNCTION__
, prefix
.family
);
2241 STREAM_GETL(s
, ifindex
);
2242 STREAM_GETC(s
, distance
);
2243 STREAM_GETL(s
, in_label
);
2244 STREAM_GETL(s
, out_label
);
2246 switch (prefix
.family
) {
2249 gtype
= NEXTHOP_TYPE_IPV4_IFINDEX
;
2251 gtype
= NEXTHOP_TYPE_IPV4
;
2255 gtype
= NEXTHOP_TYPE_IPV6_IFINDEX
;
2257 gtype
= NEXTHOP_TYPE_IPV6
;
2266 if (hdr
->command
== ZEBRA_MPLS_LABELS_ADD
) {
2267 mpls_lsp_install(zvrf
, type
, in_label
, out_label
, gtype
, &gate
,
2269 mpls_ftn_update(1, zvrf
, type
, &prefix
, gtype
, &gate
, ifindex
,
2270 distance
, out_label
);
2271 } else if (hdr
->command
== ZEBRA_MPLS_LABELS_DELETE
) {
2272 mpls_lsp_uninstall(zvrf
, type
, in_label
, gtype
, &gate
, ifindex
);
2273 mpls_ftn_update(0, zvrf
, type
, &prefix
, gtype
, &gate
, ifindex
,
2274 distance
, out_label
);
2280 static int zsend_table_manager_connect_response(struct zserv
*client
,
2281 vrf_id_t vrf_id
, uint16_t result
)
2283 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
2285 zclient_create_header(s
, ZEBRA_TABLE_MANAGER_CONNECT
, vrf_id
);
2288 stream_putc(s
, result
);
2290 stream_putw_at(s
, 0, stream_get_endp(s
));
2292 return zebra_server_send_message(client
, s
);
2295 /* Send response to a table manager connect request to client */
2296 static void zread_table_manager_connect(struct zserv
*client
,
2307 STREAM_GETC(s
, proto
);
2308 STREAM_GETW(s
, instance
);
2310 /* accept only dynamic routing protocols */
2311 if ((proto
>= ZEBRA_ROUTE_MAX
) || (proto
<= ZEBRA_ROUTE_STATIC
)) {
2312 zlog_err("client %d has wrong protocol %s", client
->sock
,
2313 zebra_route_string(proto
));
2314 zsend_table_manager_connect_response(client
, vrf_id
, 1);
2317 zlog_notice("client %d with vrf %u instance %u connected as %s",
2318 client
->sock
, vrf_id
, instance
, zebra_route_string(proto
));
2319 client
->proto
= proto
;
2320 client
->instance
= instance
;
2323 * Release previous labels of same protocol and instance.
2324 * This is done in case it restarted from an unexpected shutdown.
2326 release_daemon_table_chunks(proto
, instance
);
2328 zsend_table_manager_connect_response(client
, vrf_id
, 0);
2334 static void zread_label_manager_connect(struct zserv
*client
,
2335 struct stream
*msg
, vrf_id_t vrf_id
)
2338 /* type of protocol (lib/zebra.h) */
2340 unsigned short instance
;
2342 /* Get input stream. */
2346 STREAM_GETC(s
, proto
);
2347 STREAM_GETW(s
, instance
);
2349 /* accept only dynamic routing protocols */
2350 if ((proto
>= ZEBRA_ROUTE_MAX
) || (proto
<= ZEBRA_ROUTE_STATIC
)) {
2351 zlog_err("client %d has wrong protocol %s", client
->sock
,
2352 zebra_route_string(proto
));
2353 zsend_label_manager_connect_response(client
, vrf_id
, 1);
2356 zlog_notice("client %d with vrf %u instance %u connected as %s",
2357 client
->sock
, vrf_id
, instance
, zebra_route_string(proto
));
2358 client
->proto
= proto
;
2359 client
->instance
= instance
;
2362 Release previous labels of same protocol and instance.
2363 This is done in case it restarted from an unexpected shutdown.
2365 release_daemon_label_chunks(proto
, instance
);
2368 " Label Manager client connected: sock %d, proto %s, vrf %u instance %u",
2369 client
->sock
, zebra_route_string(proto
), vrf_id
, instance
);
2370 /* send response back */
2371 zsend_label_manager_connect_response(client
, vrf_id
, 0);
2377 static void zread_get_label_chunk(struct zserv
*client
, struct stream
*msg
,
2383 struct label_manager_chunk
*lmc
;
2385 /* Get input stream. */
2389 STREAM_GETC(s
, keep
);
2390 STREAM_GETL(s
, size
);
2392 lmc
= assign_label_chunk(client
->proto
, client
->instance
, keep
, size
);
2394 zlog_err("%s: Unable to assign Label Chunk of size %u",
2397 zlog_debug("Assigned Label Chunk %u - %u to %u", lmc
->start
,
2399 /* send response back */
2400 zsend_assign_label_chunk_response(client
, vrf_id
, lmc
);
2406 static void zread_release_label_chunk(struct zserv
*client
, struct stream
*msg
)
2409 uint32_t start
, end
;
2411 /* Get input stream. */
2415 STREAM_GETL(s
, start
);
2416 STREAM_GETL(s
, end
);
2418 release_label_chunk(client
->proto
, client
->instance
, start
, end
);
2423 static void zread_label_manager_request(ZAPI_HANDLER_ARGS
)
2425 /* to avoid sending other messages like ZERBA_INTERFACE_UP */
2426 if (hdr
->command
== ZEBRA_LABEL_MANAGER_CONNECT
)
2427 client
->is_synchronous
= 1;
2429 /* external label manager */
2431 zread_relay_label_manager_request(hdr
->command
, client
,
2433 /* this is a label manager */
2435 if (hdr
->command
== ZEBRA_LABEL_MANAGER_CONNECT
)
2436 zread_label_manager_connect(client
, msg
, zvrf_id(zvrf
));
2438 /* Sanity: don't allow 'unidentified' requests */
2439 if (!client
->proto
) {
2441 "Got label request from an unidentified client");
2444 if (hdr
->command
== ZEBRA_GET_LABEL_CHUNK
)
2445 zread_get_label_chunk(client
, msg
,
2447 else if (hdr
->command
== ZEBRA_RELEASE_LABEL_CHUNK
)
2448 zread_release_label_chunk(client
, msg
);
2453 /* Send response to a get table chunk request to client */
2454 static int zsend_assign_table_chunk_response(struct zserv
*client
,
2456 struct table_manager_chunk
*tmc
)
2458 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
2460 zclient_create_header(s
, ZEBRA_GET_TABLE_CHUNK
, vrf_id
);
2463 /* start and end labels */
2464 stream_putl(s
, tmc
->start
);
2465 stream_putl(s
, tmc
->end
);
2468 /* Write packet size. */
2469 stream_putw_at(s
, 0, stream_get_endp(s
));
2471 return zebra_server_send_message(client
, s
);
2474 static void zread_get_table_chunk(struct zserv
*client
, struct stream
*msg
,
2479 struct table_manager_chunk
*tmc
;
2481 /* Get input stream. */
2485 STREAM_GETL(s
, size
);
2487 tmc
= assign_table_chunk(client
->proto
, client
->instance
, size
);
2489 zlog_err("%s: Unable to assign Table Chunk of size %u",
2492 zlog_debug("Assigned Table Chunk %u - %u", tmc
->start
,
2494 /* send response back */
2495 zsend_assign_table_chunk_response(client
, vrf_id
, tmc
);
2501 static void zread_release_table_chunk(struct zserv
*client
, struct stream
*msg
)
2504 uint32_t start
, end
;
2506 /* Get input stream. */
2510 STREAM_GETL(s
, start
);
2511 STREAM_GETL(s
, end
);
2513 release_table_chunk(client
->proto
, client
->instance
, start
, end
);
2519 static void zread_table_manager_request(ZAPI_HANDLER_ARGS
)
2521 /* to avoid sending other messages like ZERBA_INTERFACE_UP */
2522 if (hdr
->command
== ZEBRA_TABLE_MANAGER_CONNECT
)
2523 zread_table_manager_connect(client
, msg
, zvrf_id(zvrf
));
2525 /* Sanity: don't allow 'unidentified' requests */
2526 if (!client
->proto
) {
2528 "Got table request from an unidentified client");
2531 if (hdr
->command
== ZEBRA_GET_TABLE_CHUNK
)
2532 zread_get_table_chunk(client
, msg
,
2534 else if (hdr
->command
== ZEBRA_RELEASE_TABLE_CHUNK
)
2535 zread_release_table_chunk(client
, msg
);
2539 static void zread_pseudowire(ZAPI_HANDLER_ARGS
)
2542 char ifname
[IF_NAMESIZE
];
2546 union g_addr nexthop
;
2547 uint32_t local_label
;
2548 uint32_t remote_label
;
2550 union pw_protocol_fields data
;
2552 struct zebra_pw
*pw
;
2554 /* Get input stream. */
2558 STREAM_GET(ifname
, s
, IF_NAMESIZE
);
2559 STREAM_GETL(s
, ifindex
);
2560 STREAM_GETL(s
, type
);
2564 STREAM_GET(&nexthop
.ipv4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
2567 STREAM_GET(&nexthop
.ipv6
, s
, 16);
2572 STREAM_GETL(s
, local_label
);
2573 STREAM_GETL(s
, remote_label
);
2574 STREAM_GETC(s
, flags
);
2575 STREAM_GET(&data
, s
, sizeof(data
));
2576 protocol
= client
->proto
;
2578 pw
= zebra_pw_find(zvrf
, ifname
);
2579 switch (hdr
->command
) {
2582 zlog_warn("%s: pseudowire %s already exists [%s]",
2584 zserv_command_string(hdr
->command
));
2588 zebra_pw_add(zvrf
, ifname
, protocol
, client
);
2590 case ZEBRA_PW_DELETE
:
2592 zlog_warn("%s: pseudowire %s not found [%s]", __func__
,
2593 ifname
, zserv_command_string(hdr
->command
));
2597 zebra_pw_del(zvrf
, pw
);
2600 case ZEBRA_PW_UNSET
:
2602 zlog_warn("%s: pseudowire %s not found [%s]", __func__
,
2603 ifname
, zserv_command_string(hdr
->command
));
2607 switch (hdr
->command
) {
2611 case ZEBRA_PW_UNSET
:
2616 zebra_pw_change(pw
, ifindex
, type
, af
, &nexthop
, local_label
,
2617 remote_label
, flags
, &data
);
2625 /* Cleanup registered nexthops (across VRFs) upon client disconnect. */
2626 static void zebra_client_close_cleanup_rnh(struct zserv
*client
)
2629 struct zebra_vrf
*zvrf
;
2631 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
2632 if ((zvrf
= vrf
->info
) != NULL
) {
2633 zebra_cleanup_rnh_client(zvrf_id(zvrf
), AF_INET
, client
,
2635 zebra_cleanup_rnh_client(zvrf_id(zvrf
), AF_INET6
,
2636 client
, RNH_NEXTHOP_TYPE
);
2637 zebra_cleanup_rnh_client(zvrf_id(zvrf
), AF_INET
, client
,
2638 RNH_IMPORT_CHECK_TYPE
);
2639 zebra_cleanup_rnh_client(zvrf_id(zvrf
), AF_INET6
,
2640 client
, RNH_IMPORT_CHECK_TYPE
);
2641 if (client
->proto
== ZEBRA_ROUTE_LDP
) {
2642 hash_iterate(zvrf
->lsp_table
,
2643 mpls_ldp_lsp_uninstall_all
,
2645 mpls_ldp_ftn_uninstall_all(zvrf
, AFI_IP
);
2646 mpls_ldp_ftn_uninstall_all(zvrf
, AFI_IP6
);
2652 static void zread_interface_set_master(ZAPI_HANDLER_ARGS
)
2654 struct interface
*master
;
2655 struct interface
*slave
;
2656 struct stream
*s
= msg
;
2660 STREAM_GETL(s
, vrf_id
);
2661 STREAM_GETL(s
, ifindex
);
2662 master
= if_lookup_by_index(ifindex
, vrf_id
);
2664 STREAM_GETL(s
, vrf_id
);
2665 STREAM_GETL(s
, ifindex
);
2666 slave
= if_lookup_by_index(ifindex
, vrf_id
);
2668 if (!master
|| !slave
)
2671 kernel_interface_set_master(master
, slave
);
2678 static void zread_vrf_label(ZAPI_HANDLER_ARGS
)
2680 struct interface
*ifp
;
2681 mpls_label_t nlabel
;
2684 struct zebra_vrf
*def_zvrf
;
2685 enum lsp_types_t ltype
;
2688 STREAM_GETL(s
, nlabel
);
2689 STREAM_GETC(s
, afi
);
2690 if (nlabel
== zvrf
->label
[afi
]) {
2692 * Nothing to do here move along
2697 STREAM_GETC(s
, ltype
);
2699 if (zvrf
->vrf
->vrf_id
!= VRF_DEFAULT
)
2700 ifp
= if_lookup_by_name(zvrf
->vrf
->name
, zvrf
->vrf
->vrf_id
);
2702 ifp
= if_lookup_by_name("lo", VRF_DEFAULT
);
2705 zlog_debug("Unable to find specified Interface for %s",
2710 def_zvrf
= zebra_vrf_lookup_by_id(VRF_DEFAULT
);
2712 if (zvrf
->label
[afi
] != MPLS_LABEL_NONE
) {
2716 really_remove
= true;
2717 for (scrubber
= AFI_IP
; scrubber
< AFI_MAX
; scrubber
++) {
2718 if (scrubber
== afi
)
2721 if (zvrf
->label
[scrubber
] == MPLS_LABEL_NONE
)
2724 if (zvrf
->label
[afi
] == zvrf
->label
[scrubber
]) {
2725 really_remove
= false;
2731 mpls_lsp_uninstall(def_zvrf
, ltype
, zvrf
->label
[afi
],
2732 NEXTHOP_TYPE_IFINDEX
, NULL
,
2736 if (nlabel
!= MPLS_LABEL_NONE
)
2737 mpls_lsp_install(def_zvrf
, ltype
, nlabel
,
2738 MPLS_LABEL_IMPLICIT_NULL
, NEXTHOP_TYPE_IFINDEX
,
2739 NULL
, ifp
->ifindex
);
2741 zvrf
->label
[afi
] = nlabel
;
2746 static inline void zread_rule(ZAPI_HANDLER_ARGS
)
2748 struct zebra_pbr_rule zpr
;
2754 STREAM_GETL(s
, total
);
2756 for (i
= 0; i
< total
; i
++) {
2757 memset(&zpr
, 0, sizeof(zpr
));
2759 zpr
.sock
= client
->sock
;
2760 zpr
.rule
.vrf_id
= hdr
->vrf_id
;
2761 STREAM_GETL(s
, zpr
.rule
.seq
);
2762 STREAM_GETL(s
, zpr
.rule
.priority
);
2763 STREAM_GETL(s
, zpr
.rule
.unique
);
2764 STREAM_GETC(s
, zpr
.rule
.filter
.src_ip
.family
);
2765 STREAM_GETC(s
, zpr
.rule
.filter
.src_ip
.prefixlen
);
2766 STREAM_GET(&zpr
.rule
.filter
.src_ip
.u
.prefix
, s
,
2767 prefix_blen(&zpr
.rule
.filter
.src_ip
));
2768 STREAM_GETW(s
, zpr
.rule
.filter
.src_port
);
2769 STREAM_GETC(s
, zpr
.rule
.filter
.dst_ip
.family
);
2770 STREAM_GETC(s
, zpr
.rule
.filter
.dst_ip
.prefixlen
);
2771 STREAM_GET(&zpr
.rule
.filter
.dst_ip
.u
.prefix
, s
,
2772 prefix_blen(&zpr
.rule
.filter
.dst_ip
));
2773 STREAM_GETW(s
, zpr
.rule
.filter
.dst_port
);
2774 STREAM_GETL(s
, zpr
.rule
.filter
.fwmark
);
2775 STREAM_GETL(s
, zpr
.rule
.action
.table
);
2776 STREAM_GETL(s
, ifindex
);
2779 zpr
.ifp
= if_lookup_by_index(ifindex
, VRF_UNKNOWN
);
2781 zlog_debug("Failed to lookup ifindex: %u",
2787 if (!is_default_prefix(&zpr
.rule
.filter
.src_ip
))
2788 zpr
.rule
.filter
.filter_bm
|= PBR_FILTER_SRC_IP
;
2790 if (!is_default_prefix(&zpr
.rule
.filter
.dst_ip
))
2791 zpr
.rule
.filter
.filter_bm
|= PBR_FILTER_DST_IP
;
2793 if (zpr
.rule
.filter
.src_port
)
2794 zpr
.rule
.filter
.filter_bm
|= PBR_FILTER_SRC_PORT
;
2796 if (zpr
.rule
.filter
.dst_port
)
2797 zpr
.rule
.filter
.filter_bm
|= PBR_FILTER_DST_PORT
;
2799 if (zpr
.rule
.filter
.fwmark
)
2800 zpr
.rule
.filter
.filter_bm
|= PBR_FILTER_FWMARK
;
2802 if (hdr
->command
== ZEBRA_RULE_ADD
)
2803 zebra_pbr_add_rule(zvrf
->zns
, &zpr
);
2805 zebra_pbr_del_rule(zvrf
->zns
, &zpr
);
2813 static inline void zread_ipset(ZAPI_HANDLER_ARGS
)
2815 struct zebra_pbr_ipset zpi
;
2820 STREAM_GETL(s
, total
);
2822 for (i
= 0; i
< total
; i
++) {
2823 memset(&zpi
, 0, sizeof(zpi
));
2825 zpi
.sock
= client
->sock
;
2826 STREAM_GETL(s
, zpi
.unique
);
2827 STREAM_GETL(s
, zpi
.type
);
2828 STREAM_GET(&zpi
.ipset_name
, s
,
2829 ZEBRA_IPSET_NAME_SIZE
);
2831 if (hdr
->command
== ZEBRA_IPSET_CREATE
)
2832 zebra_pbr_create_ipset(zvrf
->zns
, &zpi
);
2834 zebra_pbr_destroy_ipset(zvrf
->zns
, &zpi
);
2841 static inline void zread_ipset_entry(ZAPI_HANDLER_ARGS
)
2843 struct zebra_pbr_ipset_entry zpi
;
2844 struct zebra_pbr_ipset ipset
;
2849 STREAM_GETL(s
, total
);
2851 for (i
= 0; i
< total
; i
++) {
2852 memset(&zpi
, 0, sizeof(zpi
));
2853 memset(&ipset
, 0, sizeof(ipset
));
2855 zpi
.sock
= client
->sock
;
2856 STREAM_GETL(s
, zpi
.unique
);
2857 STREAM_GET(&ipset
.ipset_name
, s
,
2858 ZEBRA_IPSET_NAME_SIZE
);
2859 STREAM_GETC(s
, zpi
.src
.family
);
2860 STREAM_GETC(s
, zpi
.src
.prefixlen
);
2861 STREAM_GET(&zpi
.src
.u
.prefix
, s
,
2862 prefix_blen(&zpi
.src
));
2863 STREAM_GETC(s
, zpi
.dst
.family
);
2864 STREAM_GETC(s
, zpi
.dst
.prefixlen
);
2865 STREAM_GET(&zpi
.dst
.u
.prefix
, s
,
2866 prefix_blen(&zpi
.dst
));
2868 if (!is_default_prefix(&zpi
.src
))
2869 zpi
.filter_bm
|= PBR_FILTER_SRC_IP
;
2871 if (!is_default_prefix(&zpi
.dst
))
2872 zpi
.filter_bm
|= PBR_FILTER_DST_IP
;
2874 /* calculate backpointer */
2875 zpi
.backpointer
= zebra_pbr_lookup_ipset_pername(zvrf
->zns
,
2877 if (hdr
->command
== ZEBRA_IPSET_ENTRY_ADD
)
2878 zebra_pbr_add_ipset_entry(zvrf
->zns
, &zpi
);
2880 zebra_pbr_del_ipset_entry(zvrf
->zns
, &zpi
);
2887 static inline void zread_iptable(ZAPI_HANDLER_ARGS
)
2889 struct zebra_pbr_iptable zpi
;
2894 memset(&zpi
, 0, sizeof(zpi
));
2896 zpi
.sock
= client
->sock
;
2897 STREAM_GETL(s
, zpi
.unique
);
2898 STREAM_GETL(s
, zpi
.type
);
2899 STREAM_GETL(s
, zpi
.filter_bm
);
2900 STREAM_GETL(s
, zpi
.action
);
2901 STREAM_GETL(s
, zpi
.fwmark
);
2902 STREAM_GET(&zpi
.ipset_name
, s
,
2903 ZEBRA_IPSET_NAME_SIZE
);
2905 if (hdr
->command
== ZEBRA_IPTABLE_ADD
)
2906 zebra_pbr_add_iptable(zvrf
->zns
, &zpi
);
2908 zebra_pbr_del_iptable(zvrf
->zns
, &zpi
);
2913 void (*zserv_handlers
[])(ZAPI_HANDLER_ARGS
) = {
2914 [ZEBRA_ROUTER_ID_ADD
] = zread_router_id_add
,
2915 [ZEBRA_ROUTER_ID_DELETE
] = zread_router_id_delete
,
2916 [ZEBRA_INTERFACE_ADD
] = zread_interface_add
,
2917 [ZEBRA_INTERFACE_DELETE
] = zread_interface_delete
,
2918 [ZEBRA_ROUTE_ADD
] = zread_route_add
,
2919 [ZEBRA_ROUTE_DELETE
] = zread_route_del
,
2920 [ZEBRA_IPV4_ROUTE_ADD
] = zread_ipv4_add
,
2921 [ZEBRA_IPV4_ROUTE_DELETE
] = zread_ipv4_delete
,
2922 [ZEBRA_IPV4_ROUTE_IPV6_NEXTHOP_ADD
] = zread_ipv4_route_ipv6_nexthop_add
,
2923 [ZEBRA_IPV6_ROUTE_ADD
] = zread_ipv6_add
,
2924 [ZEBRA_IPV6_ROUTE_DELETE
] = zread_ipv6_delete
,
2925 [ZEBRA_REDISTRIBUTE_ADD
] = zebra_redistribute_add
,
2926 [ZEBRA_REDISTRIBUTE_DELETE
] = zebra_redistribute_delete
,
2927 [ZEBRA_REDISTRIBUTE_DEFAULT_ADD
] = zebra_redistribute_default_add
,
2928 [ZEBRA_REDISTRIBUTE_DEFAULT_DELETE
] = zebra_redistribute_default_delete
,
2929 [ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB
] = zread_ipv4_nexthop_lookup_mrib
,
2930 [ZEBRA_HELLO
] = zread_hello
,
2931 [ZEBRA_NEXTHOP_REGISTER
] = zread_rnh_register
,
2932 [ZEBRA_NEXTHOP_UNREGISTER
] = zread_rnh_unregister
,
2933 [ZEBRA_IMPORT_ROUTE_REGISTER
] = zread_rnh_register
,
2934 [ZEBRA_IMPORT_ROUTE_UNREGISTER
] = zread_rnh_unregister
,
2935 [ZEBRA_BFD_DEST_UPDATE
] = zebra_ptm_bfd_dst_register
,
2936 [ZEBRA_BFD_DEST_REGISTER
] = zebra_ptm_bfd_dst_register
,
2937 [ZEBRA_BFD_DEST_DEREGISTER
] = zebra_ptm_bfd_dst_deregister
,
2938 [ZEBRA_VRF_UNREGISTER
] = zread_vrf_unregister
,
2939 [ZEBRA_VRF_LABEL
] = zread_vrf_label
,
2940 [ZEBRA_BFD_CLIENT_REGISTER
] = zebra_ptm_bfd_client_register
,
2941 #if defined(HAVE_RTADV)
2942 [ZEBRA_INTERFACE_ENABLE_RADV
] = zebra_interface_radv_enable
,
2943 [ZEBRA_INTERFACE_DISABLE_RADV
] = zebra_interface_radv_disable
,
2945 [ZEBRA_INTERFACE_ENABLE_RADV
] = NULL
,
2946 [ZEBRA_INTERFACE_DISABLE_RADV
] = NULL
,
2948 [ZEBRA_MPLS_LABELS_ADD
] = zread_mpls_labels
,
2949 [ZEBRA_MPLS_LABELS_DELETE
] = zread_mpls_labels
,
2950 [ZEBRA_IPMR_ROUTE_STATS
] = zebra_ipmr_route_stats
,
2951 [ZEBRA_LABEL_MANAGER_CONNECT
] = zread_label_manager_request
,
2952 [ZEBRA_GET_LABEL_CHUNK
] = zread_label_manager_request
,
2953 [ZEBRA_RELEASE_LABEL_CHUNK
] = zread_label_manager_request
,
2954 [ZEBRA_FEC_REGISTER
] = zread_fec_register
,
2955 [ZEBRA_FEC_UNREGISTER
] = zread_fec_unregister
,
2956 [ZEBRA_ADVERTISE_DEFAULT_GW
] = zebra_vxlan_advertise_gw_macip
,
2957 [ZEBRA_ADVERTISE_SUBNET
] = zebra_vxlan_advertise_subnet
,
2958 [ZEBRA_ADVERTISE_ALL_VNI
] = zebra_vxlan_advertise_all_vni
,
2959 [ZEBRA_REMOTE_VTEP_ADD
] = zebra_vxlan_remote_vtep_add
,
2960 [ZEBRA_REMOTE_VTEP_DEL
] = zebra_vxlan_remote_vtep_del
,
2961 [ZEBRA_REMOTE_MACIP_ADD
] = zebra_vxlan_remote_macip_add
,
2962 [ZEBRA_REMOTE_MACIP_DEL
] = zebra_vxlan_remote_macip_del
,
2963 [ZEBRA_INTERFACE_SET_MASTER
] = zread_interface_set_master
,
2964 [ZEBRA_PW_ADD
] = zread_pseudowire
,
2965 [ZEBRA_PW_DELETE
] = zread_pseudowire
,
2966 [ZEBRA_PW_SET
] = zread_pseudowire
,
2967 [ZEBRA_PW_UNSET
] = zread_pseudowire
,
2968 [ZEBRA_RULE_ADD
] = zread_rule
,
2969 [ZEBRA_RULE_DELETE
] = zread_rule
,
2970 [ZEBRA_TABLE_MANAGER_CONNECT
] = zread_table_manager_request
,
2971 [ZEBRA_GET_TABLE_CHUNK
] = zread_table_manager_request
,
2972 [ZEBRA_RELEASE_TABLE_CHUNK
] = zread_table_manager_request
,
2973 [ZEBRA_IPSET_CREATE
] = zread_ipset
,
2974 [ZEBRA_IPSET_DESTROY
] = zread_ipset
,
2975 [ZEBRA_IPSET_ENTRY_ADD
] = zread_ipset_entry
,
2976 [ZEBRA_IPSET_ENTRY_DELETE
] = zread_ipset_entry
,
2977 [ZEBRA_IPTABLE_ADD
] = zread_iptable
,
2978 [ZEBRA_IPTABLE_DELETE
] = zread_iptable
,
2981 static inline void zserv_handle_commands(struct zserv
*client
,
2982 struct zmsghdr
*hdr
,
2984 struct zebra_vrf
*zvrf
)
2986 if (hdr
->command
> array_size(zserv_handlers
)
2987 || zserv_handlers
[hdr
->command
] == NULL
)
2988 zlog_info("Zebra received unknown command %d", hdr
->command
);
2990 zserv_handlers
[hdr
->command
](client
, hdr
, msg
, zvrf
);
2995 /* Lifecycle ---------------------------------------------------------------- */
2997 /* free zebra client information. */
2998 static void zebra_client_free(struct zserv
*client
)
3000 /* Send client de-registration to BFD */
3001 zebra_ptm_bfd_client_deregister(client
->proto
);
3003 /* Cleanup any rules installed from this client */
3004 zebra_pbr_client_close_cleanup(client
->sock
);
3006 /* Cleanup any registered nexthops - across all VRFs. */
3007 zebra_client_close_cleanup_rnh(client
);
3009 /* Release Label Manager chunks */
3010 release_daemon_label_chunks(client
->proto
, client
->instance
);
3012 /* Release Table Manager chunks */
3013 release_daemon_table_chunks(client
->proto
, client
->instance
);
3015 /* Cleanup any FECs registered by this client. */
3016 zebra_mpls_cleanup_fecs_for_client(vrf_info_lookup(VRF_DEFAULT
),
3019 /* Remove pseudowires associated with this client */
3020 zebra_pw_client_close(client
);
3022 /* Close file descriptor. */
3024 unsigned long nroutes
;
3026 close(client
->sock
);
3027 nroutes
= rib_score_proto(client
->proto
, client
->instance
);
3029 "client %d disconnected. %lu %s routes removed from the rib",
3030 client
->sock
, nroutes
,
3031 zebra_route_string(client
->proto
));
3035 /* Free stream buffers. */
3036 if (client
->ibuf_work
)
3037 stream_free(client
->ibuf_work
);
3038 if (client
->obuf_work
)
3039 stream_free(client
->obuf_work
);
3040 if (client
->ibuf_fifo
)
3041 stream_fifo_free(client
->ibuf_fifo
);
3042 if (client
->obuf_fifo
)
3043 stream_fifo_free(client
->obuf_fifo
);
3045 buffer_free(client
->wb
);
3047 /* Release threads. */
3049 thread_cancel(client
->t_read
);
3050 if (client
->t_write
)
3051 thread_cancel(client
->t_write
);
3052 if (client
->t_suicide
)
3053 thread_cancel(client
->t_suicide
);
3056 for (afi_t afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
3057 for (int i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
3058 vrf_bitmap_free(client
->redist
[afi
][i
]);
3060 vrf_bitmap_free(client
->redist_default
);
3061 vrf_bitmap_free(client
->ifinfo
);
3062 vrf_bitmap_free(client
->ridinfo
);
3064 XFREE(MTYPE_TMP
, client
);
3068 * Called from client thread to terminate itself.
3070 static void zebra_client_close(struct zserv
*client
)
3072 listnode_delete(zebrad
.client_list
, client
);
3073 zebra_client_free(client
);
3076 /* Make new client. */
3077 static void zebra_client_create(int sock
)
3079 struct zserv
*client
;
3083 client
= XCALLOC(MTYPE_TMP
, sizeof(struct zserv
));
3085 /* Make client input/output buffer. */
3086 client
->sock
= sock
;
3087 client
->ibuf_fifo
= stream_fifo_new();
3088 client
->obuf_fifo
= stream_fifo_new();
3089 client
->ibuf_work
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
3090 client
->obuf_work
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
3091 client
->wb
= buffer_new(0);
3093 /* Set table number. */
3094 client
->rtm_table
= zebrad
.rtm_table_default
;
3096 client
->connect_time
= monotime(NULL
);
3097 /* Initialize flags */
3098 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
3099 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
3100 client
->redist
[afi
][i
] = vrf_bitmap_init();
3101 client
->redist_default
= vrf_bitmap_init();
3102 client
->ifinfo
= vrf_bitmap_init();
3103 client
->ridinfo
= vrf_bitmap_init();
3105 /* by default, it's not a synchronous client */
3106 client
->is_synchronous
= 0;
3108 /* Add this client to linked list. */
3109 listnode_add(zebrad
.client_list
, client
);
3111 zebra_vrf_update_all(client
);
3113 /* start read loop */
3114 zebra_event(client
, ZEBRA_READ
);
3117 static int zserv_delayed_close(struct thread
*thread
)
3119 struct zserv
*client
= THREAD_ARG(thread
);
3121 client
->t_suicide
= NULL
;
3122 zebra_client_close(client
);
3127 * Log zapi message to zlog.
3136 * The message header
3138 static void zserv_log_message(const char *errmsg
, struct stream
*msg
,
3139 struct zmsghdr
*hdr
)
3141 zlog_debug("Rx'd ZAPI message");
3143 zlog_debug("%s", errmsg
);
3145 zlog_debug(" Length: %d", hdr
->length
);
3146 zlog_debug("Command: %s", zserv_command_string(hdr
->command
));
3147 zlog_debug(" VRF: %u", hdr
->vrf_id
);
3149 zlog_hexdump(msg
->data
, STREAM_READABLE(msg
));
3152 static int zserv_flush_data(struct thread
*thread
)
3154 struct zserv
*client
= THREAD_ARG(thread
);
3156 client
->t_write
= NULL
;
3157 if (client
->t_suicide
) {
3158 zebra_client_close(client
);
3161 switch (buffer_flush_available(client
->wb
, client
->sock
)) {
3164 "%s: buffer_flush_available failed on zserv client fd %d, closing",
3165 __func__
, client
->sock
);
3166 zebra_client_close(client
);
3169 case BUFFER_PENDING
:
3170 client
->t_write
= NULL
;
3171 thread_add_write(zebrad
.master
, zserv_flush_data
, client
,
3172 client
->sock
, &client
->t_write
);
3179 client
->last_write_time
= monotime(NULL
);
3184 * Write a single packet.
3186 static int zserv_write(struct thread
*thread
)
3188 struct zserv
*client
= THREAD_ARG(thread
);
3192 if (client
->t_suicide
)
3195 if (client
->is_synchronous
)
3198 msg
= stream_fifo_pop(client
->obuf_fifo
);
3199 stream_set_getp(msg
, 0);
3200 client
->last_write_cmd
= stream_getw_from(msg
, 6);
3202 writerv
= buffer_write(client
->wb
, client
->sock
, STREAM_DATA(msg
),
3203 stream_get_endp(msg
));
3210 "%s: buffer_write failed to zserv client fd %d, closing",
3211 __func__
, client
->sock
);
3213 * Schedule a delayed close since many of the functions that
3214 * call this one do not check the return code. They do not
3215 * allow for the possibility that an I/O error may have caused
3216 * the client to be deleted.
3218 client
->t_suicide
= NULL
;
3219 thread_add_event(zebrad
.master
, zserv_delayed_close
, client
, 0,
3220 &client
->t_suicide
);
3223 THREAD_OFF(client
->t_write
);
3225 case BUFFER_PENDING
:
3226 thread_add_write(zebrad
.master
, zserv_flush_data
, client
,
3227 client
->sock
, &client
->t_write
);
3231 if (client
->obuf_fifo
->count
)
3232 zebra_event(client
, ZEBRA_WRITE
);
3234 client
->last_write_time
= monotime(NULL
);
3238 #if defined(HANDLE_ZAPI_FUZZING)
3239 static void zserv_write_incoming(struct stream
*orig
, uint16_t command
)
3241 char fname
[MAXPATHLEN
];
3242 struct stream
*copy
;
3245 copy
= stream_dup(orig
);
3246 stream_set_getp(copy
, 0);
3248 zserv_privs
.change(ZPRIVS_RAISE
);
3249 snprintf(fname
, MAXPATHLEN
, "%s/%u", DAEMON_VTY_DIR
, command
);
3250 fd
= open(fname
, O_CREAT
| O_WRONLY
| O_EXCL
, 0644);
3251 stream_flush(copy
, fd
);
3253 zserv_privs
.change(ZPRIVS_LOWER
);
3258 static int zserv_process_messages(struct thread
*thread
)
3260 struct zserv
*client
= THREAD_ARG(thread
);
3261 struct zebra_vrf
*zvrf
;
3267 msg
= stream_fifo_pop(client
->ibuf_fifo
);
3269 /* break if out of messages */
3273 /* read & check header */
3274 hdrvalid
= zapi_parse_header(msg
, &hdr
);
3275 if (!hdrvalid
&& IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
) {
3276 const char *emsg
= "Message has corrupt header";
3277 zserv_log_message(emsg
, msg
, NULL
);
3282 hdr
.length
-= ZEBRA_HEADER_SIZE
;
3284 zvrf
= zebra_vrf_lookup_by_id(hdr
.vrf_id
);
3285 if (!zvrf
&& IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
) {
3286 const char *emsg
= "Message specifies unknown VRF";
3287 zserv_log_message(emsg
, msg
, &hdr
);
3292 /* process commands */
3293 zserv_handle_commands(client
, &hdr
, msg
, zvrf
);
3300 /* Handler of zebra service request. */
3301 static int zserv_read(struct thread
*thread
)
3304 struct zserv
*client
;
3306 #if defined(HANDLE_ZAPI_FUZZING)
3309 int packets
= zebrad
.packets_to_process
;
3311 /* Get thread data. Reset reading thread because I'm running. */
3312 sock
= THREAD_FD(thread
);
3313 client
= THREAD_ARG(thread
);
3315 if (client
->t_suicide
) {
3316 zebra_client_close(client
);
3326 already
= stream_get_endp(client
->ibuf_work
);
3328 /* Read length and command (if we don't have it already). */
3329 if (already
< ZEBRA_HEADER_SIZE
) {
3330 nb
= stream_read_try(client
->ibuf_work
, sock
,
3331 ZEBRA_HEADER_SIZE
- already
);
3332 if ((nb
== 0 || nb
== -1) && IS_ZEBRA_DEBUG_EVENT
)
3333 zlog_debug("connection closed socket [%d]",
3335 if ((nb
== 0 || nb
== -1))
3337 if (nb
!= (ssize_t
)(ZEBRA_HEADER_SIZE
- already
)) {
3338 /* Try again later. */
3341 already
= ZEBRA_HEADER_SIZE
;
3344 /* Reset to read from the beginning of the incoming packet. */
3345 stream_set_getp(client
->ibuf_work
, 0);
3347 /* Fetch header values */
3348 hdrvalid
= zapi_parse_header(client
->ibuf_work
, &hdr
);
3351 snprintf(errmsg
, sizeof(errmsg
),
3352 "%s: Message has corrupt header", __func__
);
3353 zserv_log_message(errmsg
, client
->ibuf_work
, NULL
);
3357 /* Validate header */
3358 if (hdr
.marker
!= ZEBRA_HEADER_MARKER
3359 || hdr
.version
!= ZSERV_VERSION
) {
3361 errmsg
, sizeof(errmsg
),
3362 "Message has corrupt header\n%s: socket %d version mismatch, marker %d, version %d",
3363 __func__
, sock
, hdr
.marker
, hdr
.version
);
3364 zserv_log_message(errmsg
, client
->ibuf_work
, &hdr
);
3367 if (hdr
.length
< ZEBRA_HEADER_SIZE
) {
3369 errmsg
, sizeof(errmsg
),
3370 "Message has corrupt header\n%s: socket %d message length %u is less than header size %d",
3371 __func__
, sock
, hdr
.length
, ZEBRA_HEADER_SIZE
);
3372 zserv_log_message(errmsg
, client
->ibuf_work
, &hdr
);
3375 if (hdr
.length
> STREAM_SIZE(client
->ibuf_work
)) {
3377 errmsg
, sizeof(errmsg
),
3378 "Message has corrupt header\n%s: socket %d message length %u exceeds buffer size %lu",
3379 __func__
, sock
, hdr
.length
,
3380 (unsigned long)STREAM_SIZE(client
->ibuf_work
));
3384 /* Read rest of data. */
3385 if (already
< hdr
.length
) {
3386 nb
= stream_read_try(client
->ibuf_work
, sock
,
3387 hdr
.length
- already
);
3388 if ((nb
== 0 || nb
== -1) && IS_ZEBRA_DEBUG_EVENT
)
3390 "connection closed [%d] when reading zebra data",
3392 if ((nb
== 0 || nb
== -1))
3394 if (nb
!= (ssize_t
)(hdr
.length
- already
)) {
3395 /* Try again later. */
3400 #if defined(HANDLE_ZAPI_FUZZING)
3401 zserv_write_incoming(client
->ibuf_work
, command
);
3404 /* Debug packet information. */
3405 if (IS_ZEBRA_DEBUG_EVENT
)
3406 zlog_debug("zebra message comes from socket [%d]",
3409 if (IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
)
3410 zserv_log_message(NULL
, client
->ibuf_work
, &hdr
);
3412 client
->last_read_time
= monotime(NULL
);
3413 client
->last_read_cmd
= hdr
.command
;
3415 stream_set_getp(client
->ibuf_work
, 0);
3416 struct stream
*msg
= stream_dup(client
->ibuf_work
);
3418 stream_fifo_push(client
->ibuf_fifo
, msg
);
3420 if (client
->t_suicide
)
3424 stream_reset(client
->ibuf_work
);
3427 if (IS_ZEBRA_DEBUG_PACKET
)
3428 zlog_debug("Read %d packets",
3429 zebrad
.packets_to_process
- packets
);
3431 /* Schedule job to process those packets */
3432 thread_add_event(zebrad
.master
, &zserv_process_messages
, client
, 0,
3435 /* Reschedule ourselves */
3436 zebra_event(client
, ZEBRA_READ
);
3441 zebra_client_close(client
);
3445 static void zebra_event(struct zserv
*client
, enum event event
)
3449 thread_add_read(zebrad
.master
, zserv_read
, client
, client
->sock
,
3453 thread_add_write(zebrad
.master
, zserv_write
, client
,
3454 client
->sock
, &client
->t_write
);
3459 /* Accept code of zebra server socket. */
3460 static int zebra_accept(struct thread
*thread
)
3464 struct sockaddr_in client
;
3467 accept_sock
= THREAD_FD(thread
);
3469 /* Reregister myself. */
3470 thread_add_read(zebrad
.master
, zebra_accept
, NULL
, accept_sock
, NULL
);
3472 len
= sizeof(struct sockaddr_in
);
3473 client_sock
= accept(accept_sock
, (struct sockaddr
*)&client
, &len
);
3475 if (client_sock
< 0) {
3476 zlog_warn("Can't accept zebra socket: %s",
3477 safe_strerror(errno
));
3481 /* Make client socket non-blocking. */
3482 set_nonblocking(client_sock
);
3484 /* Create new zebra client. */
3485 zebra_client_create(client_sock
);
3490 /* Make zebra server socket, wiping any existing one (see bug #403). */
3491 void zebra_zserv_socket_init(char *path
)
3496 struct sockaddr_storage sa
;
3499 if (!frr_zclient_addr(&sa
, &sa_len
, path
))
3500 /* should be caught in zebra main() */
3504 old_mask
= umask(0077);
3506 /* Make UNIX domain socket. */
3507 sock
= socket(sa
.ss_family
, SOCK_STREAM
, 0);
3509 zlog_warn("Can't create zserv socket: %s",
3510 safe_strerror(errno
));
3512 "zebra can't provide full functionality due to above error");
3516 if (sa
.ss_family
!= AF_UNIX
) {
3517 sockopt_reuseaddr(sock
);
3518 sockopt_reuseport(sock
);
3520 struct sockaddr_un
*suna
= (struct sockaddr_un
*)&sa
;
3521 if (suna
->sun_path
[0])
3522 unlink(suna
->sun_path
);
3525 zserv_privs
.change(ZPRIVS_RAISE
);
3526 setsockopt_so_recvbuf(sock
, 1048576);
3527 setsockopt_so_sendbuf(sock
, 1048576);
3528 zserv_privs
.change(ZPRIVS_LOWER
);
3530 if (sa
.ss_family
!= AF_UNIX
&& zserv_privs
.change(ZPRIVS_RAISE
))
3531 zlog_err("Can't raise privileges");
3533 ret
= bind(sock
, (struct sockaddr
*)&sa
, sa_len
);
3535 zlog_warn("Can't bind zserv socket on %s: %s", path
,
3536 safe_strerror(errno
));
3538 "zebra can't provide full functionality due to above error");
3542 if (sa
.ss_family
!= AF_UNIX
&& zserv_privs
.change(ZPRIVS_LOWER
))
3543 zlog_err("Can't lower privileges");
3545 ret
= listen(sock
, 5);
3547 zlog_warn("Can't listen to zserv socket %s: %s", path
,
3548 safe_strerror(errno
));
3550 "zebra can't provide full functionality due to above error");
3557 thread_add_read(zebrad
.master
, zebra_accept
, NULL
, sock
, NULL
);
3560 #define ZEBRA_TIME_BUF 32
3561 static char *zserv_time_buf(time_t *time1
, char *buf
, int buflen
)
3566 assert(buf
!= NULL
);
3567 assert(buflen
>= ZEBRA_TIME_BUF
);
3568 assert(time1
!= NULL
);
3571 snprintf(buf
, buflen
, "never ");
3575 now
= monotime(NULL
);
3579 if (now
< ONE_DAY_SECOND
)
3580 snprintf(buf
, buflen
, "%02d:%02d:%02d", tm
->tm_hour
, tm
->tm_min
,
3582 else if (now
< ONE_WEEK_SECOND
)
3583 snprintf(buf
, buflen
, "%dd%02dh%02dm", tm
->tm_yday
, tm
->tm_hour
,
3586 snprintf(buf
, buflen
, "%02dw%dd%02dh", tm
->tm_yday
/ 7,
3587 tm
->tm_yday
- ((tm
->tm_yday
/ 7) * 7), tm
->tm_hour
);
3591 static void zebra_show_client_detail(struct vty
*vty
, struct zserv
*client
)
3593 char cbuf
[ZEBRA_TIME_BUF
], rbuf
[ZEBRA_TIME_BUF
];
3594 char wbuf
[ZEBRA_TIME_BUF
], nhbuf
[ZEBRA_TIME_BUF
], mbuf
[ZEBRA_TIME_BUF
];
3596 vty_out(vty
, "Client: %s", zebra_route_string(client
->proto
));
3597 if (client
->instance
)
3598 vty_out(vty
, " Instance: %d", client
->instance
);
3601 vty_out(vty
, "------------------------ \n");
3602 vty_out(vty
, "FD: %d \n", client
->sock
);
3603 vty_out(vty
, "Route Table ID: %d \n", client
->rtm_table
);
3605 vty_out(vty
, "Connect Time: %s \n",
3606 zserv_time_buf(&client
->connect_time
, cbuf
, ZEBRA_TIME_BUF
));
3607 if (client
->nh_reg_time
) {
3608 vty_out(vty
, "Nexthop Registry Time: %s \n",
3609 zserv_time_buf(&client
->nh_reg_time
, nhbuf
,
3611 if (client
->nh_last_upd_time
)
3612 vty_out(vty
, "Nexthop Last Update Time: %s \n",
3613 zserv_time_buf(&client
->nh_last_upd_time
, mbuf
,
3616 vty_out(vty
, "No Nexthop Update sent\n");
3618 vty_out(vty
, "Not registered for Nexthop Updates\n");
3620 vty_out(vty
, "Last Msg Rx Time: %s \n",
3621 zserv_time_buf(&client
->last_read_time
, rbuf
, ZEBRA_TIME_BUF
));
3622 vty_out(vty
, "Last Msg Tx Time: %s \n",
3623 zserv_time_buf(&client
->last_write_time
, wbuf
, ZEBRA_TIME_BUF
));
3624 if (client
->last_read_time
)
3625 vty_out(vty
, "Last Rcvd Cmd: %s \n",
3626 zserv_command_string(client
->last_read_cmd
));
3627 if (client
->last_write_time
)
3628 vty_out(vty
, "Last Sent Cmd: %s \n",
3629 zserv_command_string(client
->last_write_cmd
));
3632 vty_out(vty
, "Type Add Update Del \n");
3633 vty_out(vty
, "================================================== \n");
3634 vty_out(vty
, "IPv4 %-12d%-12d%-12d\n", client
->v4_route_add_cnt
,
3635 client
->v4_route_upd8_cnt
, client
->v4_route_del_cnt
);
3636 vty_out(vty
, "IPv6 %-12d%-12d%-12d\n", client
->v6_route_add_cnt
,
3637 client
->v6_route_upd8_cnt
, client
->v6_route_del_cnt
);
3638 vty_out(vty
, "Redist:v4 %-12d%-12d%-12d\n", client
->redist_v4_add_cnt
,
3639 0, client
->redist_v4_del_cnt
);
3640 vty_out(vty
, "Redist:v6 %-12d%-12d%-12d\n", client
->redist_v6_add_cnt
,
3641 0, client
->redist_v6_del_cnt
);
3642 vty_out(vty
, "Connected %-12d%-12d%-12d\n", client
->ifadd_cnt
, 0,
3644 vty_out(vty
, "BFD peer %-12d%-12d%-12d\n", client
->bfd_peer_add_cnt
,
3645 client
->bfd_peer_upd8_cnt
, client
->bfd_peer_del_cnt
);
3646 vty_out(vty
, "Interface Up Notifications: %d\n", client
->ifup_cnt
);
3647 vty_out(vty
, "Interface Down Notifications: %d\n", client
->ifdown_cnt
);
3648 vty_out(vty
, "VNI add notifications: %d\n", client
->vniadd_cnt
);
3649 vty_out(vty
, "VNI delete notifications: %d\n", client
->vnidel_cnt
);
3650 vty_out(vty
, "L3-VNI add notifications: %d\n", client
->l3vniadd_cnt
);
3651 vty_out(vty
, "L3-VNI delete notifications: %d\n", client
->l3vnidel_cnt
);
3652 vty_out(vty
, "MAC-IP add notifications: %d\n", client
->macipadd_cnt
);
3653 vty_out(vty
, "MAC-IP delete notifications: %d\n", client
->macipdel_cnt
);
3659 static void zebra_show_client_brief(struct vty
*vty
, struct zserv
*client
)
3661 char cbuf
[ZEBRA_TIME_BUF
], rbuf
[ZEBRA_TIME_BUF
];
3662 char wbuf
[ZEBRA_TIME_BUF
];
3664 vty_out(vty
, "%-8s%12s %12s%12s%8d/%-8d%8d/%-8d\n",
3665 zebra_route_string(client
->proto
),
3666 zserv_time_buf(&client
->connect_time
, cbuf
, ZEBRA_TIME_BUF
),
3667 zserv_time_buf(&client
->last_read_time
, rbuf
, ZEBRA_TIME_BUF
),
3668 zserv_time_buf(&client
->last_write_time
, wbuf
, ZEBRA_TIME_BUF
),
3669 client
->v4_route_add_cnt
+ client
->v4_route_upd8_cnt
,
3670 client
->v4_route_del_cnt
,
3671 client
->v6_route_add_cnt
+ client
->v6_route_upd8_cnt
,
3672 client
->v6_route_del_cnt
);
3675 struct zserv
*zebra_find_client(uint8_t proto
, unsigned short instance
)
3677 struct listnode
*node
, *nnode
;
3678 struct zserv
*client
;
3680 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, node
, nnode
, client
)) {
3681 if (client
->proto
== proto
&& client
->instance
== instance
)
3688 /* This command is for debugging purpose. */
3689 DEFUN (show_zebra_client
,
3690 show_zebra_client_cmd
,
3691 "show zebra client",
3694 "Client information\n")
3696 struct listnode
*node
;
3697 struct zserv
*client
;
3699 for (ALL_LIST_ELEMENTS_RO(zebrad
.client_list
, node
, client
))
3700 zebra_show_client_detail(vty
, client
);
3705 /* This command is for debugging purpose. */
3706 DEFUN (show_zebra_client_summary
,
3707 show_zebra_client_summary_cmd
,
3708 "show zebra client summary",
3711 "Client information brief\n"
3714 struct listnode
*node
;
3715 struct zserv
*client
;
3718 "Name Connect Time Last Read Last Write IPv4 Routes IPv6 Routes \n");
3720 "--------------------------------------------------------------------------------\n");
3722 for (ALL_LIST_ELEMENTS_RO(zebrad
.client_list
, node
, client
))
3723 zebra_show_client_brief(vty
, client
);
3725 vty_out(vty
, "Routes column shows (added+updated)/deleted\n");
3729 #if defined(HANDLE_ZAPI_FUZZING)
3730 void zserv_read_file(char *input
)
3733 struct zserv
*client
= NULL
;
3736 zebra_client_create(-1);
3737 client
= zebrad
.client_list
->head
->data
;
3740 fd
= open(input
, O_RDONLY
| O_NONBLOCK
);
3743 zebra_client_read(&t
);
3749 void zserv_init(void)
3751 /* Client list init. */
3752 zebrad
.client_list
= list_new();
3753 zebrad
.client_list
->del
= (void (*)(void *))zebra_client_free
;
3755 install_element(ENABLE_NODE
, &show_zebra_client_cmd
);
3756 install_element(ENABLE_NODE
, &show_zebra_client_summary_cmd
);