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"
65 /* Event list of zebra. */
66 enum event
{ ZEBRA_READ
, ZEBRA_WRITE
};
68 extern struct zebra_privs_t zserv_privs
;
69 /* post event into client */
70 static void zebra_event(struct zserv
*client
, enum event event
);
73 /* Public interface ======================================================== */
75 int zebra_server_send_message(struct zserv
*client
, struct stream
*msg
)
77 stream_fifo_push(client
->obuf_fifo
, msg
);
78 zebra_event(client
, ZEBRA_WRITE
);
82 /* Encoding helpers -------------------------------------------------------- */
84 static void zserv_encode_interface(struct stream
*s
, struct interface
*ifp
)
86 /* Interface information. */
87 stream_put(s
, ifp
->name
, INTERFACE_NAMSIZ
);
88 stream_putl(s
, ifp
->ifindex
);
89 stream_putc(s
, ifp
->status
);
90 stream_putq(s
, ifp
->flags
);
91 stream_putc(s
, ifp
->ptm_enable
);
92 stream_putc(s
, ifp
->ptm_status
);
93 stream_putl(s
, ifp
->metric
);
94 stream_putl(s
, ifp
->speed
);
95 stream_putl(s
, ifp
->mtu
);
96 stream_putl(s
, ifp
->mtu6
);
97 stream_putl(s
, ifp
->bandwidth
);
98 stream_putl(s
, ifp
->ll_type
);
99 stream_putl(s
, ifp
->hw_addr_len
);
100 if (ifp
->hw_addr_len
)
101 stream_put(s
, ifp
->hw_addr
, ifp
->hw_addr_len
);
103 /* Then, Traffic Engineering parameters if any */
104 if (HAS_LINK_PARAMS(ifp
) && IS_LINK_PARAMS_SET(ifp
->link_params
)) {
106 zebra_interface_link_params_write(s
, ifp
);
110 /* Write packet size. */
111 stream_putw_at(s
, 0, stream_get_endp(s
));
114 static void zserv_encode_vrf(struct stream
*s
, struct zebra_vrf
*zvrf
)
116 struct vrf_data data
;
117 const char *netns_name
= zvrf_ns_name(zvrf
);
119 data
.l
.table_id
= zvrf
->table_id
;
122 strlcpy(data
.l
.netns_name
, basename((char *)netns_name
),
125 memset(data
.l
.netns_name
, 0, NS_NAMSIZ
);
126 /* Pass the tableid and the netns NAME */
127 stream_put(s
, &data
, sizeof(struct vrf_data
));
128 /* Interface information. */
129 stream_put(s
, zvrf_name(zvrf
), VRF_NAMSIZ
);
130 /* Write packet size. */
131 stream_putw_at(s
, 0, stream_get_endp(s
));
134 static int zserv_encode_nexthop(struct stream
*s
, struct nexthop
*nexthop
)
136 stream_putc(s
, nexthop
->type
);
137 switch (nexthop
->type
) {
138 case NEXTHOP_TYPE_IPV4
:
139 case NEXTHOP_TYPE_IPV4_IFINDEX
:
140 stream_put_in_addr(s
, &nexthop
->gate
.ipv4
);
141 stream_putl(s
, nexthop
->ifindex
);
143 case NEXTHOP_TYPE_IPV6
:
144 stream_put(s
, &nexthop
->gate
.ipv6
, 16);
146 case NEXTHOP_TYPE_IPV6_IFINDEX
:
147 stream_put(s
, &nexthop
->gate
.ipv6
, 16);
148 stream_putl(s
, nexthop
->ifindex
);
150 case NEXTHOP_TYPE_IFINDEX
:
151 stream_putl(s
, nexthop
->ifindex
);
160 /* Send handlers ----------------------------------------------------------- */
162 /* Interface is added. Send ZEBRA_INTERFACE_ADD to client. */
164 * This function is called in the following situations:
165 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
167 * - at startup, when zebra figures out the available interfaces
168 * - when an interface is added (where support for
169 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
170 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
173 int zsend_interface_add(struct zserv
*client
, struct interface
*ifp
)
175 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
177 zclient_create_header(s
, ZEBRA_INTERFACE_ADD
, ifp
->vrf_id
);
178 zserv_encode_interface(s
, ifp
);
181 return zebra_server_send_message(client
, s
);
184 /* Interface deletion from zebra daemon. */
185 int zsend_interface_delete(struct zserv
*client
, struct interface
*ifp
)
187 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
189 zclient_create_header(s
, ZEBRA_INTERFACE_DELETE
, ifp
->vrf_id
);
190 zserv_encode_interface(s
, ifp
);
193 return zebra_server_send_message(client
, s
);
196 int zsend_vrf_add(struct zserv
*client
, struct zebra_vrf
*zvrf
)
198 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
200 zclient_create_header(s
, ZEBRA_VRF_ADD
, zvrf_id(zvrf
));
201 zserv_encode_vrf(s
, zvrf
);
203 client
->vrfadd_cnt
++;
204 return zebra_server_send_message(client
, s
);
207 /* VRF deletion from zebra daemon. */
208 int zsend_vrf_delete(struct zserv
*client
, struct zebra_vrf
*zvrf
)
211 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
213 zclient_create_header(s
, ZEBRA_VRF_DELETE
, zvrf_id(zvrf
));
214 zserv_encode_vrf(s
, zvrf
);
216 client
->vrfdel_cnt
++;
217 return zebra_server_send_message(client
, s
);
220 int zsend_interface_link_params(struct zserv
*client
, struct interface
*ifp
)
222 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
224 /* Check this client need interface information. */
228 if (!ifp
->link_params
)
231 zclient_create_header(s
, ZEBRA_INTERFACE_LINK_PARAMS
, ifp
->vrf_id
);
233 /* Add Interface Index */
234 stream_putl(s
, ifp
->ifindex
);
236 /* Then TE Link Parameters */
237 if (zebra_interface_link_params_write(s
, ifp
) == 0)
240 /* Write packet size. */
241 stream_putw_at(s
, 0, stream_get_endp(s
));
243 return zebra_server_send_message(client
, s
);
246 /* Interface address is added/deleted. Send ZEBRA_INTERFACE_ADDRESS_ADD or
247 * ZEBRA_INTERFACE_ADDRESS_DELETE to the client.
249 * A ZEBRA_INTERFACE_ADDRESS_ADD is sent in the following situations:
250 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
251 * from the client, after the ZEBRA_INTERFACE_ADD has been
252 * sent from zebra to the client
253 * - redistribute new address info to all clients in the following situations
254 * - at startup, when zebra figures out the available interfaces
255 * - when an interface is added (where support for
256 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
257 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
259 * - for the vty commands "ip address A.B.C.D/M [<secondary>|<label LINE>]"
260 * and "no bandwidth <1-10000000>", "ipv6 address X:X::X:X/M"
261 * - when an RTM_NEWADDR message is received from the kernel,
263 * The call tree that triggers ZEBRA_INTERFACE_ADDRESS_DELETE:
265 * zsend_interface_address(DELETE)
268 * zebra_interface_address_delete_update
270 * | | if_delete_update
272 * ip_address_uninstall connected_delete_ipv4
273 * [ipv6_addresss_uninstall] [connected_delete_ipv6]
276 * | RTM_NEWADDR on routing/netlink socket
279 * "no ip address A.B.C.D/M [label LINE]"
280 * "no ip address A.B.C.D/M secondary"
281 * ["no ipv6 address X:X::X:X/M"]
284 int zsend_interface_address(int cmd
, struct zserv
*client
,
285 struct interface
*ifp
, struct connected
*ifc
)
289 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
291 zclient_create_header(s
, cmd
, ifp
->vrf_id
);
292 stream_putl(s
, ifp
->ifindex
);
294 /* Interface address flag. */
295 stream_putc(s
, ifc
->flags
);
297 /* Prefix information. */
299 stream_putc(s
, p
->family
);
300 blen
= prefix_blen(p
);
301 stream_put(s
, &p
->u
.prefix
, blen
);
304 * XXX gnu version does not send prefixlen for
305 * ZEBRA_INTERFACE_ADDRESS_DELETE
306 * but zebra_interface_address_delete_read() in the gnu version
309 stream_putc(s
, p
->prefixlen
);
312 p
= ifc
->destination
;
314 stream_put(s
, &p
->u
.prefix
, blen
);
316 stream_put(s
, NULL
, blen
);
318 /* Write packet size. */
319 stream_putw_at(s
, 0, stream_get_endp(s
));
321 client
->connected_rt_add_cnt
++;
322 return zebra_server_send_message(client
, s
);
325 static int zsend_interface_nbr_address(int cmd
, struct zserv
*client
,
326 struct interface
*ifp
,
327 struct nbr_connected
*ifc
)
330 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
333 zclient_create_header(s
, cmd
, ifp
->vrf_id
);
334 stream_putl(s
, ifp
->ifindex
);
336 /* Prefix information. */
338 stream_putc(s
, p
->family
);
339 blen
= prefix_blen(p
);
340 stream_put(s
, &p
->u
.prefix
, blen
);
343 * XXX gnu version does not send prefixlen for
344 * ZEBRA_INTERFACE_ADDRESS_DELETE
345 * but zebra_interface_address_delete_read() in the gnu version
348 stream_putc(s
, p
->prefixlen
);
350 /* Write packet size. */
351 stream_putw_at(s
, 0, stream_get_endp(s
));
353 return zebra_server_send_message(client
, s
);
356 /* Interface address addition. */
357 static void zebra_interface_nbr_address_add_update(struct interface
*ifp
,
358 struct nbr_connected
*ifc
)
360 struct listnode
*node
, *nnode
;
361 struct zserv
*client
;
364 if (IS_ZEBRA_DEBUG_EVENT
) {
365 char buf
[INET6_ADDRSTRLEN
];
369 "MESSAGE: ZEBRA_INTERFACE_NBR_ADDRESS_ADD %s/%d on %s",
370 inet_ntop(p
->family
, &p
->u
.prefix
, buf
,
372 p
->prefixlen
, ifc
->ifp
->name
);
375 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, node
, nnode
, client
))
376 zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_ADD
,
380 /* Interface address deletion. */
381 static void zebra_interface_nbr_address_delete_update(struct interface
*ifp
,
382 struct nbr_connected
*ifc
)
384 struct listnode
*node
, *nnode
;
385 struct zserv
*client
;
388 if (IS_ZEBRA_DEBUG_EVENT
) {
389 char buf
[INET6_ADDRSTRLEN
];
393 "MESSAGE: ZEBRA_INTERFACE_NBR_ADDRESS_DELETE %s/%d on %s",
394 inet_ntop(p
->family
, &p
->u
.prefix
, buf
,
396 p
->prefixlen
, ifc
->ifp
->name
);
399 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, node
, nnode
, client
))
400 zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
,
404 /* Send addresses on interface to client */
405 int zsend_interface_addresses(struct zserv
*client
, struct interface
*ifp
)
407 struct listnode
*cnode
, *cnnode
;
409 struct nbr_connected
*nc
;
411 /* Send interface addresses. */
412 for (ALL_LIST_ELEMENTS(ifp
->connected
, cnode
, cnnode
, c
)) {
413 if (!CHECK_FLAG(c
->conf
, ZEBRA_IFC_REAL
))
416 if (zsend_interface_address(ZEBRA_INTERFACE_ADDRESS_ADD
, client
,
422 /* Send interface neighbors. */
423 for (ALL_LIST_ELEMENTS(ifp
->nbr_connected
, cnode
, cnnode
, nc
)) {
424 if (zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_ADD
,
433 /* Notify client about interface moving from one VRF to another.
434 * Whether client is interested in old and new VRF is checked by caller.
436 int zsend_interface_vrf_update(struct zserv
*client
, struct interface
*ifp
,
439 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
441 zclient_create_header(s
, ZEBRA_INTERFACE_VRF_UPDATE
, ifp
->vrf_id
);
443 /* Fill in the ifIndex of the interface and its new VRF (id) */
444 stream_putl(s
, ifp
->ifindex
);
445 stream_putl(s
, vrf_id
);
447 /* Write packet size. */
448 stream_putw_at(s
, 0, stream_get_endp(s
));
450 client
->if_vrfchg_cnt
++;
451 return zebra_server_send_message(client
, s
);
454 /* Add new nbr connected IPv6 address */
455 void nbr_connected_add_ipv6(struct interface
*ifp
, struct in6_addr
*address
)
457 struct nbr_connected
*ifc
;
461 IPV6_ADDR_COPY(&p
.u
.prefix
, address
);
462 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
464 if (!(ifc
= listnode_head(ifp
->nbr_connected
))) {
466 ifc
= nbr_connected_new();
467 ifc
->address
= prefix_new();
469 listnode_add(ifp
->nbr_connected
, ifc
);
472 prefix_copy(ifc
->address
, &p
);
474 zebra_interface_nbr_address_add_update(ifp
, ifc
);
476 if_nbr_ipv6ll_to_ipv4ll_neigh_update(ifp
, address
, 1);
479 void nbr_connected_delete_ipv6(struct interface
*ifp
, struct in6_addr
*address
)
481 struct nbr_connected
*ifc
;
485 IPV6_ADDR_COPY(&p
.u
.prefix
, address
);
486 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
488 ifc
= nbr_connected_check(ifp
, &p
);
492 listnode_delete(ifp
->nbr_connected
, ifc
);
494 zebra_interface_nbr_address_delete_update(ifp
, ifc
);
496 if_nbr_ipv6ll_to_ipv4ll_neigh_update(ifp
, address
, 0);
498 nbr_connected_free(ifc
);
502 * The cmd passed to zsend_interface_update may be ZEBRA_INTERFACE_UP or
503 * ZEBRA_INTERFACE_DOWN.
505 * The ZEBRA_INTERFACE_UP message is sent from the zebra server to
506 * the clients in one of 2 situations:
507 * - an if_up is detected e.g., as a result of an RTM_IFINFO message
508 * - a vty command modifying the bandwidth of an interface is received.
509 * The ZEBRA_INTERFACE_DOWN message is sent when an if_down is detected.
511 int zsend_interface_update(int cmd
, struct zserv
*client
, struct interface
*ifp
)
513 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
515 zclient_create_header(s
, cmd
, ifp
->vrf_id
);
516 zserv_encode_interface(s
, ifp
);
518 if (cmd
== ZEBRA_INTERFACE_UP
)
521 client
->ifdown_cnt
++;
523 return zebra_server_send_message(client
, s
);
526 int zsend_redistribute_route(int cmd
, struct zserv
*client
, struct prefix
*p
,
527 struct prefix
*src_p
, struct route_entry
*re
)
529 struct zapi_route api
;
530 struct zapi_nexthop
*api_nh
;
531 struct nexthop
*nexthop
;
534 memset(&api
, 0, sizeof(api
));
535 api
.vrf_id
= re
->vrf_id
;
537 api
.instance
= re
->instance
;
538 api
.flags
= re
->flags
;
543 SET_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
);
544 memcpy(&api
.src_prefix
, src_p
, sizeof(api
.src_prefix
));
548 if (re
->nexthop_active_num
) {
549 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
550 api
.nexthop_num
= re
->nexthop_active_num
;
552 for (nexthop
= re
->ng
.nexthop
; nexthop
; nexthop
= nexthop
->next
) {
553 if (!CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
556 api_nh
= &api
.nexthops
[count
];
557 api_nh
->vrf_id
= nexthop
->vrf_id
;
558 api_nh
->type
= nexthop
->type
;
559 switch (nexthop
->type
) {
560 case NEXTHOP_TYPE_BLACKHOLE
:
561 api_nh
->bh_type
= nexthop
->bh_type
;
563 case NEXTHOP_TYPE_IPV4
:
564 api_nh
->gate
.ipv4
= nexthop
->gate
.ipv4
;
566 case NEXTHOP_TYPE_IPV4_IFINDEX
:
567 api_nh
->gate
.ipv4
= nexthop
->gate
.ipv4
;
568 api_nh
->ifindex
= nexthop
->ifindex
;
570 case NEXTHOP_TYPE_IFINDEX
:
571 api_nh
->ifindex
= nexthop
->ifindex
;
573 case NEXTHOP_TYPE_IPV6
:
574 api_nh
->gate
.ipv6
= nexthop
->gate
.ipv6
;
576 case NEXTHOP_TYPE_IPV6_IFINDEX
:
577 api_nh
->gate
.ipv6
= nexthop
->gate
.ipv6
;
578 api_nh
->ifindex
= nexthop
->ifindex
;
584 SET_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
);
585 api
.distance
= re
->distance
;
586 SET_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
);
587 api
.metric
= re
->metric
;
589 SET_FLAG(api
.message
, ZAPI_MESSAGE_TAG
);
592 SET_FLAG(api
.message
, ZAPI_MESSAGE_MTU
);
595 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
597 /* Encode route and send. */
598 if (zapi_route_encode(cmd
, s
, &api
) < 0)
600 return zebra_server_send_message(client
, s
);
604 * Modified version of zsend_ipv4_nexthop_lookup(): Query unicast rib if
605 * nexthop is not found on mrib. Returns both route metric and protocol
608 static int zsend_ipv4_nexthop_lookup_mrib(struct zserv
*client
,
610 struct route_entry
*re
,
611 struct zebra_vrf
*zvrf
)
616 struct nexthop
*nexthop
;
618 /* Get output stream. */
619 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
622 /* Fill in result. */
623 zclient_create_header(s
, ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB
, zvrf_id(zvrf
));
624 stream_put_in_addr(s
, &addr
);
627 stream_putc(s
, re
->distance
);
628 stream_putl(s
, re
->metric
);
630 nump
= stream_get_endp(
631 s
); /* remember position for nexthop_num */
632 stream_putc(s
, 0); /* reserve room for nexthop_num */
633 /* Only non-recursive routes are elegible to resolve the nexthop
635 * are looking up. Therefore, we will just iterate over the top
636 * chain of nexthops. */
637 for (nexthop
= re
->ng
.nexthop
; nexthop
; nexthop
= nexthop
->next
)
638 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
639 num
+= zserv_encode_nexthop(s
, nexthop
);
641 stream_putc_at(s
, nump
, num
); /* store nexthop_num */
643 stream_putc(s
, 0); /* distance */
644 stream_putl(s
, 0); /* metric */
645 stream_putc(s
, 0); /* nexthop_num */
648 stream_putw_at(s
, 0, stream_get_endp(s
));
650 return zebra_server_send_message(client
, s
);
653 int zsend_route_notify_owner(struct route_entry
*re
, struct prefix
*p
,
654 enum zapi_route_notify_owner note
)
656 struct zserv
*client
;
660 client
= zebra_find_client(re
->type
, re
->instance
);
661 if (!client
|| !client
->notify_owner
) {
662 if (IS_ZEBRA_DEBUG_PACKET
) {
663 char buff
[PREFIX_STRLEN
];
666 "Not Notifying Owner: %u about prefix %s(%u) %d",
667 re
->type
, prefix2str(p
, buff
, sizeof(buff
)),
673 if (IS_ZEBRA_DEBUG_PACKET
) {
674 char buff
[PREFIX_STRLEN
];
676 zlog_debug("Notifying Owner: %u about prefix %s(%u) %d",
677 re
->type
, prefix2str(p
, buff
, sizeof(buff
)),
681 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
684 zclient_create_header(s
, ZEBRA_ROUTE_NOTIFY_OWNER
, re
->vrf_id
);
686 stream_put(s
, ¬e
, sizeof(note
));
688 stream_putc(s
, p
->family
);
690 blen
= prefix_blen(p
);
691 stream_putc(s
, p
->prefixlen
);
692 stream_put(s
, &p
->u
.prefix
, blen
);
694 stream_putl(s
, re
->table
);
696 stream_putw_at(s
, 0, stream_get_endp(s
));
698 return zebra_server_send_message(client
, s
);
701 void zsend_rule_notify_owner(struct zebra_pbr_rule
*rule
,
702 enum zapi_rule_notify_owner note
)
704 struct listnode
*node
;
705 struct zserv
*client
;
708 if (IS_ZEBRA_DEBUG_PACKET
) {
709 zlog_debug("%s: Notifying %u", __PRETTY_FUNCTION__
,
713 for (ALL_LIST_ELEMENTS_RO(zebrad
.client_list
, node
, client
)) {
714 if (rule
->sock
== client
->sock
)
721 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
724 zclient_create_header(s
, ZEBRA_RULE_NOTIFY_OWNER
, VRF_DEFAULT
);
725 stream_put(s
, ¬e
, sizeof(note
));
726 stream_putl(s
, rule
->seq
);
727 stream_putl(s
, rule
->priority
);
728 stream_putl(s
, rule
->unique
);
730 stream_putl(s
, rule
->ifp
->ifindex
);
734 stream_putw_at(s
, 0, stream_get_endp(s
));
736 zebra_server_send_message(client
, s
);
739 /* Router-id is updated. Send ZEBRA_ROUTER_ID_ADD to client. */
740 int zsend_router_id_update(struct zserv
*client
, struct prefix
*p
,
745 /* Check this client need interface information. */
746 if (!vrf_bitmap_check(client
->ridinfo
, vrf_id
))
749 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
752 zclient_create_header(s
, ZEBRA_ROUTER_ID_UPDATE
, vrf_id
);
754 /* Prefix information. */
755 stream_putc(s
, p
->family
);
756 blen
= prefix_blen(p
);
757 stream_put(s
, &p
->u
.prefix
, blen
);
758 stream_putc(s
, p
->prefixlen
);
760 /* Write packet size. */
761 stream_putw_at(s
, 0, stream_get_endp(s
));
763 return zebra_server_send_message(client
, s
);
767 * Function used by Zebra to send a PW status update to LDP daemon
769 int zsend_pw_update(struct zserv
*client
, struct zebra_pw
*pw
)
771 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
773 zclient_create_header(s
, ZEBRA_PW_STATUS_UPDATE
, pw
->vrf_id
);
774 stream_write(s
, pw
->ifname
, IF_NAMESIZE
);
775 stream_putl(s
, pw
->ifindex
);
776 stream_putl(s
, pw
->status
);
778 /* Put length at the first point of the stream. */
779 stream_putw_at(s
, 0, stream_get_endp(s
));
781 return zebra_server_send_message(client
, s
);
784 /* Send response to a get label chunk request to client */
785 static int zsend_assign_label_chunk_response(struct zserv
*client
,
787 struct label_manager_chunk
*lmc
)
790 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
792 zclient_create_header(s
, ZEBRA_GET_LABEL_CHUNK
, vrf_id
);
796 stream_putc(s
, lmc
->keep
);
797 /* start and end labels */
798 stream_putl(s
, lmc
->start
);
799 stream_putl(s
, lmc
->end
);
802 /* Write packet size. */
803 stream_putw_at(s
, 0, stream_get_endp(s
));
805 ret
= writen(client
->sock
, s
->data
, stream_get_endp(s
));
810 /* Send response to a label manager connect request to client */
811 static int zsend_label_manager_connect_response(struct zserv
*client
,
812 vrf_id_t vrf_id
, u_short result
)
815 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
817 zclient_create_header(s
, ZEBRA_LABEL_MANAGER_CONNECT
, vrf_id
);
820 stream_putc(s
, result
);
822 /* Write packet size. */
823 stream_putw_at(s
, 0, stream_get_endp(s
));
825 ret
= writen(client
->sock
, s
->data
, stream_get_endp(s
));
831 /* Inbound message handling ------------------------------------------------ */
834 [ZEBRA_NEXTHOP_REGISTER
] = RNH_NEXTHOP_TYPE
,
835 [ZEBRA_NEXTHOP_UNREGISTER
] = RNH_NEXTHOP_TYPE
,
836 [ZEBRA_IMPORT_ROUTE_REGISTER
] = RNH_IMPORT_CHECK_TYPE
,
837 [ZEBRA_IMPORT_ROUTE_UNREGISTER
] = RNH_IMPORT_CHECK_TYPE
,
840 /* Nexthop register */
841 static void zread_rnh_register(ZAPI_HANDLER_ARGS
)
848 uint16_t type
= cmd2type
[hdr
->command
];
850 if (IS_ZEBRA_DEBUG_NHT
)
852 "rnh_register msg from client %s: hdr->length=%d, type=%s\n",
853 zebra_route_string(client
->proto
), hdr
->length
,
854 (type
== RNH_NEXTHOP_TYPE
) ? "nexthop" : "route");
858 client
->nh_reg_time
= monotime(NULL
);
860 while (l
< hdr
->length
) {
861 STREAM_GETC(s
, flags
);
862 STREAM_GETW(s
, p
.family
);
863 STREAM_GETC(s
, p
.prefixlen
);
865 if (p
.family
== AF_INET
) {
866 if (p
.prefixlen
> IPV4_MAX_BITLEN
) {
868 "%s: Specified prefix hdr->length %d is too large for a v4 address",
869 __PRETTY_FUNCTION__
, p
.prefixlen
);
872 STREAM_GET(&p
.u
.prefix4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
873 l
+= IPV4_MAX_BYTELEN
;
874 } else if (p
.family
== AF_INET6
) {
875 if (p
.prefixlen
> IPV6_MAX_BITLEN
) {
877 "%s: Specified prefix hdr->length %d is to large for a v6 address",
878 __PRETTY_FUNCTION__
, p
.prefixlen
);
881 STREAM_GET(&p
.u
.prefix6
, s
, IPV6_MAX_BYTELEN
);
882 l
+= IPV6_MAX_BYTELEN
;
885 "rnh_register: Received unknown family type %d\n",
889 rnh
= zebra_add_rnh(&p
, zvrf_id(zvrf
), type
);
890 if (type
== RNH_NEXTHOP_TYPE
) {
892 && !CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
))
893 SET_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
);
895 && CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
))
896 UNSET_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
);
897 } else if (type
== RNH_IMPORT_CHECK_TYPE
) {
899 && !CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
))
900 SET_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
);
902 && CHECK_FLAG(rnh
->flags
,
903 ZEBRA_NHT_EXACT_MATCH
))
904 UNSET_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
);
907 zebra_add_rnh_client(rnh
, client
, type
, zvrf_id(zvrf
));
908 /* Anything not AF_INET/INET6 has been filtered out above */
909 zebra_evaluate_rnh(zvrf_id(zvrf
), p
.family
, 1, type
, &p
);
916 /* Nexthop register */
917 static void zread_rnh_unregister(ZAPI_HANDLER_ARGS
)
923 uint16_t type
= cmd2type
[hdr
->command
];
925 if (IS_ZEBRA_DEBUG_NHT
)
927 "rnh_unregister msg from client %s: hdr->length=%d\n",
928 zebra_route_string(client
->proto
), hdr
->length
);
932 while (l
< hdr
->length
) {
935 STREAM_GETC(s
, flags
);
939 STREAM_GETW(s
, p
.family
);
940 STREAM_GETC(s
, p
.prefixlen
);
942 if (p
.family
== AF_INET
) {
943 if (p
.prefixlen
> IPV4_MAX_BITLEN
) {
945 "%s: Specified prefix hdr->length %d is to large for a v4 address",
946 __PRETTY_FUNCTION__
, p
.prefixlen
);
949 STREAM_GET(&p
.u
.prefix4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
950 l
+= IPV4_MAX_BYTELEN
;
951 } else if (p
.family
== AF_INET6
) {
952 if (p
.prefixlen
> IPV6_MAX_BITLEN
) {
954 "%s: Specified prefix hdr->length %d is to large for a v6 address",
955 __PRETTY_FUNCTION__
, p
.prefixlen
);
958 STREAM_GET(&p
.u
.prefix6
, s
, IPV6_MAX_BYTELEN
);
959 l
+= IPV6_MAX_BYTELEN
;
962 "rnh_register: Received unknown family type %d\n",
966 rnh
= zebra_lookup_rnh(&p
, zvrf_id(zvrf
), type
);
968 client
->nh_dereg_time
= monotime(NULL
);
969 zebra_remove_rnh_client(rnh
, client
, type
);
976 #define ZEBRA_MIN_FEC_LENGTH 5
979 static void zread_fec_register(ZAPI_HANDLER_ARGS
)
985 uint32_t label_index
= MPLS_INVALID_LABEL_INDEX
;
988 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
990 return; // unexpected
993 * The minimum amount of data that can be sent for one fec
996 if (hdr
->length
< ZEBRA_MIN_FEC_LENGTH
) {
998 "fec_register: Received a fec register of hdr->length %d, it is of insufficient size to properly decode",
1003 while (l
< hdr
->length
) {
1004 STREAM_GETW(s
, flags
);
1005 memset(&p
, 0, sizeof(p
));
1006 STREAM_GETW(s
, p
.family
);
1007 if (p
.family
!= AF_INET
&& p
.family
!= AF_INET6
) {
1009 "fec_register: Received unknown family type %d\n",
1013 STREAM_GETC(s
, p
.prefixlen
);
1014 if ((p
.family
== AF_INET
&& p
.prefixlen
> IPV4_MAX_BITLEN
)
1015 || (p
.family
== AF_INET6
1016 && p
.prefixlen
> IPV6_MAX_BITLEN
)) {
1018 "%s: Specified prefix hdr->length: %d is to long for %d",
1019 __PRETTY_FUNCTION__
, p
.prefixlen
, p
.family
);
1023 STREAM_GET(&p
.u
.prefix
, s
, PSIZE(p
.prefixlen
));
1024 l
+= PSIZE(p
.prefixlen
);
1025 if (flags
& ZEBRA_FEC_REGISTER_LABEL_INDEX
) {
1026 STREAM_GETL(s
, label_index
);
1029 label_index
= MPLS_INVALID_LABEL_INDEX
;
1030 zebra_mpls_fec_register(zvrf
, &p
, label_index
, client
);
1037 /* FEC unregister */
1038 static void zread_fec_unregister(ZAPI_HANDLER_ARGS
)
1046 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1048 return; // unexpected
1051 * The minimum amount of data that can be sent for one
1052 * fec unregistration
1054 if (hdr
->length
< ZEBRA_MIN_FEC_LENGTH
) {
1056 "fec_unregister: Received a fec unregister of hdr->length %d, it is of insufficient size to properly decode",
1061 while (l
< hdr
->length
) {
1062 STREAM_GETW(s
, flags
);
1064 goto stream_failure
;
1066 memset(&p
, 0, sizeof(p
));
1067 STREAM_GETW(s
, p
.family
);
1068 if (p
.family
!= AF_INET
&& p
.family
!= AF_INET6
) {
1070 "fec_unregister: Received unknown family type %d\n",
1074 STREAM_GETC(s
, p
.prefixlen
);
1075 if ((p
.family
== AF_INET
&& p
.prefixlen
> IPV4_MAX_BITLEN
)
1076 || (p
.family
== AF_INET6
1077 && p
.prefixlen
> IPV6_MAX_BITLEN
)) {
1079 "%s: Received prefix hdr->length %d which is greater than %d can support",
1080 __PRETTY_FUNCTION__
, p
.prefixlen
, p
.family
);
1084 STREAM_GET(&p
.u
.prefix
, s
, PSIZE(p
.prefixlen
));
1085 l
+= PSIZE(p
.prefixlen
);
1086 zebra_mpls_fec_unregister(zvrf
, &p
, client
);
1095 * Register zebra server interface information.
1096 * Send current all interface and address information.
1098 static void zread_interface_add(ZAPI_HANDLER_ARGS
)
1101 struct interface
*ifp
;
1103 /* Interface information is needed. */
1104 vrf_bitmap_set(client
->ifinfo
, zvrf_id(zvrf
));
1106 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
1107 FOR_ALL_INTERFACES (vrf
, ifp
) {
1108 /* Skip pseudo interface. */
1109 if (!CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_ACTIVE
))
1112 zsend_interface_add(client
, ifp
);
1113 zsend_interface_addresses(client
, ifp
);
1118 /* Unregister zebra server interface information. */
1119 static void zread_interface_delete(ZAPI_HANDLER_ARGS
)
1121 vrf_bitmap_unset(client
->ifinfo
, zvrf_id(zvrf
));
1124 void zserv_nexthop_num_warn(const char *caller
, const struct prefix
*p
,
1125 const unsigned int nexthop_num
)
1127 if (nexthop_num
> multipath_num
) {
1128 char buff
[PREFIX2STR_BUFFER
];
1129 prefix2str(p
, buff
, sizeof(buff
));
1131 "%s: Prefix %s has %d nexthops, but we can only use the first %d",
1132 caller
, buff
, nexthop_num
, multipath_num
);
1136 static void zread_route_add(ZAPI_HANDLER_ARGS
)
1139 struct zapi_route api
;
1140 struct zapi_nexthop
*api_nh
;
1142 struct prefix_ipv6
*src_p
= NULL
;
1143 struct route_entry
*re
;
1144 struct nexthop
*nexthop
= NULL
;
1146 vrf_id_t vrf_id
= 0;
1147 struct ipaddr vtep_ip
;
1150 zapi_route_decode(s
, &api
);
1152 /* Allocate new route. */
1153 vrf_id
= zvrf_id(zvrf
);
1154 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
1155 re
->type
= api
.type
;
1156 re
->instance
= api
.instance
;
1157 re
->flags
= api
.flags
;
1158 re
->uptime
= time(NULL
);
1159 re
->vrf_id
= vrf_id
;
1160 if (api
.tableid
&& vrf_id
== VRF_DEFAULT
)
1161 re
->table
= api
.tableid
;
1163 re
->table
= zvrf
->table_id
;
1165 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
)) {
1166 for (i
= 0; i
< api
.nexthop_num
; i
++) {
1167 api_nh
= &api
.nexthops
[i
];
1168 ifindex_t ifindex
= 0;
1170 switch (api_nh
->type
) {
1171 case NEXTHOP_TYPE_IFINDEX
:
1172 nexthop
= route_entry_nexthop_ifindex_add(
1173 re
, api_nh
->ifindex
, api_nh
->vrf_id
);
1175 case NEXTHOP_TYPE_IPV4
:
1176 nexthop
= route_entry_nexthop_ipv4_add(
1177 re
, &api_nh
->gate
.ipv4
, NULL
,
1180 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1182 memset(&vtep_ip
, 0, sizeof(struct ipaddr
));
1183 if (CHECK_FLAG(api
.flags
,
1184 ZEBRA_FLAG_EVPN_ROUTE
)) {
1185 ifindex
= get_l3vni_svi_ifindex(vrf_id
);
1187 ifindex
= api_nh
->ifindex
;
1190 nexthop
= route_entry_nexthop_ipv4_ifindex_add(
1191 re
, &api_nh
->gate
.ipv4
, NULL
, ifindex
,
1194 /* if this an EVPN route entry,
1195 * program the nh as neigh
1197 if (CHECK_FLAG(api
.flags
,
1198 ZEBRA_FLAG_EVPN_ROUTE
)) {
1199 SET_FLAG(nexthop
->flags
,
1200 NEXTHOP_FLAG_EVPN_RVTEP
);
1201 vtep_ip
.ipa_type
= IPADDR_V4
;
1202 memcpy(&(vtep_ip
.ipaddr_v4
),
1203 &(api_nh
->gate
.ipv4
),
1204 sizeof(struct in_addr
));
1205 zebra_vxlan_evpn_vrf_route_add(
1206 vrf_id
, &api
.rmac
, &vtep_ip
,
1210 case NEXTHOP_TYPE_IPV6
:
1211 nexthop
= route_entry_nexthop_ipv6_add(
1212 re
, &api_nh
->gate
.ipv6
, api_nh
->vrf_id
);
1214 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1215 memset(&vtep_ip
, 0, sizeof(struct ipaddr
));
1216 if (CHECK_FLAG(api
.flags
,
1217 ZEBRA_FLAG_EVPN_ROUTE
)) {
1219 get_l3vni_svi_ifindex(vrf_id
);
1221 ifindex
= api_nh
->ifindex
;
1224 nexthop
= route_entry_nexthop_ipv6_ifindex_add(
1225 re
, &api_nh
->gate
.ipv6
, ifindex
,
1228 /* if this an EVPN route entry,
1229 * program the nh as neigh
1231 if (CHECK_FLAG(api
.flags
,
1232 ZEBRA_FLAG_EVPN_ROUTE
)) {
1233 SET_FLAG(nexthop
->flags
,
1234 NEXTHOP_FLAG_EVPN_RVTEP
);
1235 vtep_ip
.ipa_type
= IPADDR_V6
;
1236 memcpy(&vtep_ip
.ipaddr_v6
,
1237 &(api_nh
->gate
.ipv6
),
1238 sizeof(struct in6_addr
));
1239 zebra_vxlan_evpn_vrf_route_add(
1246 case NEXTHOP_TYPE_BLACKHOLE
:
1247 nexthop
= route_entry_nexthop_blackhole_add(
1248 re
, api_nh
->bh_type
);
1254 "%s: Nexthops Specified: %d but we failed to properly create one",
1255 __PRETTY_FUNCTION__
, api
.nexthop_num
);
1256 nexthops_free(re
->ng
.nexthop
);
1257 XFREE(MTYPE_RE
, re
);
1260 /* MPLS labels for BGP-LU or Segment Routing */
1261 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_LABEL
)
1262 && api_nh
->type
!= NEXTHOP_TYPE_IFINDEX
1263 && api_nh
->type
!= NEXTHOP_TYPE_BLACKHOLE
) {
1264 enum lsp_types_t label_type
;
1267 lsp_type_from_re_type(client
->proto
);
1268 nexthop_add_labels(nexthop
, label_type
,
1270 &api_nh
->labels
[0]);
1275 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
))
1276 re
->distance
= api
.distance
;
1277 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
))
1278 re
->metric
= api
.metric
;
1279 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_TAG
))
1281 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_MTU
))
1284 afi
= family2afi(api
.prefix
.family
);
1285 if (afi
!= AFI_IP6
&& CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
)) {
1286 zlog_warn("%s: Received SRC Prefix but afi is not v6",
1287 __PRETTY_FUNCTION__
);
1288 nexthops_free(re
->ng
.nexthop
);
1289 XFREE(MTYPE_RE
, re
);
1292 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
))
1293 src_p
= &api
.src_prefix
;
1295 ret
= rib_add_multipath(afi
, api
.safi
, &api
.prefix
, src_p
, re
);
1298 switch (api
.prefix
.family
) {
1301 client
->v4_route_add_cnt
++;
1303 client
->v4_route_upd8_cnt
++;
1307 client
->v6_route_add_cnt
++;
1309 client
->v6_route_upd8_cnt
++;
1314 static void zread_route_del(ZAPI_HANDLER_ARGS
)
1317 struct zapi_route api
;
1319 struct prefix_ipv6
*src_p
= NULL
;
1322 if (zapi_route_decode(s
, &api
) < 0)
1325 afi
= family2afi(api
.prefix
.family
);
1326 if (afi
!= AFI_IP6
&& CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
)) {
1327 zlog_warn("%s: Received a src prefix while afi is not v6",
1328 __PRETTY_FUNCTION__
);
1331 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
))
1332 src_p
= &api
.src_prefix
;
1334 rib_delete(afi
, api
.safi
, zvrf_id(zvrf
), api
.type
, api
.instance
,
1335 api
.flags
, &api
.prefix
, src_p
, NULL
, zvrf
->table_id
,
1336 api
.metric
, false, &api
.rmac
);
1339 switch (api
.prefix
.family
) {
1341 client
->v4_route_del_cnt
++;
1344 client
->v6_route_del_cnt
++;
1349 /* This function support multiple nexthop. */
1351 * Parse the ZEBRA_IPV4_ROUTE_ADD sent from client. Update re and
1354 static void zread_ipv4_add(ZAPI_HANDLER_ARGS
)
1357 struct route_entry
*re
;
1360 struct in_addr nhop_addr
;
1362 u_char nexthop_type
;
1367 enum lsp_types_t label_type
= ZEBRA_LSP_NONE
;
1369 struct nexthop
*nexthop
;
1370 enum blackhole_type bh_type
= BLACKHOLE_NULL
;
1372 /* Get input stream. */
1375 /* Allocate new re. */
1376 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
1378 /* Type, flags, message. */
1379 STREAM_GETC(s
, re
->type
);
1380 if (re
->type
> ZEBRA_ROUTE_MAX
) {
1381 zlog_warn("%s: Specified route type %d is not a legal value\n",
1382 __PRETTY_FUNCTION__
, re
->type
);
1383 XFREE(MTYPE_RE
, re
);
1386 STREAM_GETW(s
, re
->instance
);
1387 STREAM_GETL(s
, re
->flags
);
1388 STREAM_GETC(s
, message
);
1389 STREAM_GETW(s
, safi
);
1390 re
->uptime
= time(NULL
);
1393 memset(&p
, 0, sizeof(struct prefix_ipv4
));
1395 STREAM_GETC(s
, p
.prefixlen
);
1396 if (p
.prefixlen
> IPV4_MAX_BITLEN
) {
1398 "%s: Specified prefix length %d is greater than what v4 can be",
1399 __PRETTY_FUNCTION__
, p
.prefixlen
);
1400 XFREE(MTYPE_RE
, re
);
1403 STREAM_GET(&p
.u
.prefix4
, s
, PSIZE(p
.prefixlen
));
1406 re
->vrf_id
= zvrf_id(zvrf
);
1408 /* Nexthop parse. */
1409 if (CHECK_FLAG(message
, ZAPI_MESSAGE_NEXTHOP
)) {
1410 STREAM_GETC(s
, nexthop_num
);
1411 zserv_nexthop_num_warn(__func__
, (const struct prefix
*)&p
,
1414 if (CHECK_FLAG(message
, ZAPI_MESSAGE_LABEL
))
1415 label_type
= lsp_type_from_re_type(client
->proto
);
1417 for (i
= 0; i
< nexthop_num
; i
++) {
1418 STREAM_GETC(s
, nexthop_type
);
1420 switch (nexthop_type
) {
1421 case NEXTHOP_TYPE_IFINDEX
:
1422 STREAM_GETL(s
, ifindex
);
1423 route_entry_nexthop_ifindex_add(re
, ifindex
,
1426 case NEXTHOP_TYPE_IPV4
:
1427 STREAM_GET(&nhop_addr
.s_addr
, s
,
1429 nexthop
= route_entry_nexthop_ipv4_add(
1430 re
, &nhop_addr
, NULL
, re
->vrf_id
);
1431 /* For labeled-unicast, each nexthop is followed
1433 if (CHECK_FLAG(message
, ZAPI_MESSAGE_LABEL
)) {
1434 STREAM_GETL(s
, label
);
1435 nexthop_add_labels(nexthop
, label_type
,
1439 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1440 STREAM_GET(&nhop_addr
.s_addr
, s
,
1442 STREAM_GETL(s
, ifindex
);
1443 route_entry_nexthop_ipv4_ifindex_add(
1444 re
, &nhop_addr
, NULL
, ifindex
,
1447 case NEXTHOP_TYPE_IPV6
:
1449 "%s: Please use ZEBRA_ROUTE_ADD if you want to pass v6 nexthops",
1450 __PRETTY_FUNCTION__
);
1451 nexthops_free(re
->ng
.nexthop
);
1452 XFREE(MTYPE_RE
, re
);
1455 case NEXTHOP_TYPE_BLACKHOLE
:
1456 route_entry_nexthop_blackhole_add(re
, bh_type
);
1460 "%s: Specified nexthop type: %d does not exist",
1461 __PRETTY_FUNCTION__
, nexthop_type
);
1462 nexthops_free(re
->ng
.nexthop
);
1463 XFREE(MTYPE_RE
, re
);
1470 if (CHECK_FLAG(message
, ZAPI_MESSAGE_DISTANCE
))
1471 STREAM_GETC(s
, re
->distance
);
1474 if (CHECK_FLAG(message
, ZAPI_MESSAGE_METRIC
))
1475 STREAM_GETL(s
, re
->metric
);
1478 if (CHECK_FLAG(message
, ZAPI_MESSAGE_TAG
))
1479 STREAM_GETL(s
, re
->tag
);
1483 if (CHECK_FLAG(message
, ZAPI_MESSAGE_MTU
))
1484 STREAM_GETL(s
, re
->mtu
);
1489 re
->table
= zvrf
->table_id
;
1491 ret
= rib_add_multipath(AFI_IP
, safi
, &p
, NULL
, re
);
1495 client
->v4_route_add_cnt
++;
1497 client
->v4_route_upd8_cnt
++;
1502 nexthops_free(re
->ng
.nexthop
);
1503 XFREE(MTYPE_RE
, re
);
1506 /* Zebra server IPv4 prefix delete function. */
1507 static void zread_ipv4_delete(ZAPI_HANDLER_ARGS
)
1510 struct zapi_ipv4 api
;
1516 /* Type, flags, message. */
1517 STREAM_GETC(s
, api
.type
);
1518 STREAM_GETW(s
, api
.instance
);
1519 STREAM_GETL(s
, api
.flags
);
1520 STREAM_GETC(s
, api
.message
);
1521 STREAM_GETW(s
, api
.safi
);
1524 memset(&p
, 0, sizeof(struct prefix
));
1526 STREAM_GETC(s
, p
.prefixlen
);
1527 if (p
.prefixlen
> IPV4_MAX_BITLEN
) {
1528 zlog_warn("%s: Passed in prefixlen %d is impossible",
1529 __PRETTY_FUNCTION__
, p
.prefixlen
);
1532 STREAM_GET(&p
.u
.prefix4
, s
, PSIZE(p
.prefixlen
));
1534 table_id
= zvrf
->table_id
;
1536 rib_delete(AFI_IP
, api
.safi
, zvrf_id(zvrf
), api
.type
, api
.instance
,
1537 api
.flags
, &p
, NULL
, NULL
, table_id
, 0, false, NULL
);
1538 client
->v4_route_del_cnt
++;
1544 /* MRIB Nexthop lookup for IPv4. */
1545 static void zread_ipv4_nexthop_lookup_mrib(ZAPI_HANDLER_ARGS
)
1547 struct in_addr addr
;
1548 struct route_entry
*re
;
1550 STREAM_GET(&addr
.s_addr
, msg
, IPV4_MAX_BYTELEN
);
1551 re
= rib_match_ipv4_multicast(zvrf_id(zvrf
), addr
, NULL
);
1552 zsend_ipv4_nexthop_lookup_mrib(client
, addr
, re
, zvrf
);
1558 /* Zebra server IPv6 prefix add function. */
1559 static void zread_ipv4_route_ipv6_nexthop_add(ZAPI_HANDLER_ARGS
)
1563 struct in6_addr nhop_addr
;
1564 struct route_entry
*re
;
1567 u_char nexthop_type
;
1570 static struct in6_addr nexthops
[MULTIPATH_NUM
];
1571 static unsigned int ifindices
[MULTIPATH_NUM
];
1573 static mpls_label_t labels
[MULTIPATH_NUM
];
1574 enum lsp_types_t label_type
= ZEBRA_LSP_NONE
;
1576 struct nexthop
*nexthop
;
1577 enum blackhole_type bh_type
= BLACKHOLE_NULL
;
1579 /* Get input stream. */
1582 memset(&nhop_addr
, 0, sizeof(struct in6_addr
));
1584 /* Allocate new re. */
1585 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
1587 /* Type, flags, message. */
1588 STREAM_GETC(s
, re
->type
);
1589 if (re
->type
> ZEBRA_ROUTE_MAX
) {
1590 zlog_warn("%s: Specified route type: %d is not a legal value\n",
1591 __PRETTY_FUNCTION__
, re
->type
);
1592 XFREE(MTYPE_RE
, re
);
1595 STREAM_GETW(s
, re
->instance
);
1596 STREAM_GETL(s
, re
->flags
);
1597 STREAM_GETC(s
, message
);
1598 STREAM_GETW(s
, safi
);
1599 re
->uptime
= time(NULL
);
1602 memset(&p
, 0, sizeof(struct prefix_ipv4
));
1604 STREAM_GETC(s
, p
.prefixlen
);
1605 if (p
.prefixlen
> IPV4_MAX_BITLEN
) {
1607 "%s: Prefix Length %d is greater than what a v4 address can use",
1608 __PRETTY_FUNCTION__
, p
.prefixlen
);
1609 XFREE(MTYPE_RE
, re
);
1612 STREAM_GET(&p
.u
.prefix4
, s
, PSIZE(p
.prefixlen
));
1615 re
->vrf_id
= zvrf_id(zvrf
);
1617 /* We need to give nh-addr, nh-ifindex with the same next-hop object
1618 * to the re to ensure that IPv6 multipathing works; need to coalesce
1619 * these. Clients should send the same number of paired set of
1620 * next-hop-addr/next-hop-ifindices. */
1621 if (CHECK_FLAG(message
, ZAPI_MESSAGE_NEXTHOP
)) {
1622 unsigned int nh_count
= 0;
1623 unsigned int if_count
= 0;
1624 unsigned int max_nh_if
= 0;
1626 STREAM_GETC(s
, nexthop_num
);
1627 zserv_nexthop_num_warn(__func__
, (const struct prefix
*)&p
,
1630 if (CHECK_FLAG(message
, ZAPI_MESSAGE_LABEL
))
1631 label_type
= lsp_type_from_re_type(client
->proto
);
1633 for (i
= 0; i
< nexthop_num
; i
++) {
1634 STREAM_GETC(s
, nexthop_type
);
1636 switch (nexthop_type
) {
1637 case NEXTHOP_TYPE_IPV6
:
1638 STREAM_GET(&nhop_addr
, s
, 16);
1639 if (nh_count
< MULTIPATH_NUM
) {
1640 /* For labeled-unicast, each nexthop is
1641 * followed by label. */
1642 if (CHECK_FLAG(message
,
1643 ZAPI_MESSAGE_LABEL
)) {
1644 STREAM_GETL(s
, label
);
1645 labels
[nh_count
] = label
;
1647 nexthops
[nh_count
] = nhop_addr
;
1651 case NEXTHOP_TYPE_IFINDEX
:
1652 if (if_count
< multipath_num
) {
1653 STREAM_GETL(s
, ifindices
[if_count
++]);
1656 case NEXTHOP_TYPE_BLACKHOLE
:
1657 route_entry_nexthop_blackhole_add(re
, bh_type
);
1661 "%s: Please use ZEBRA_ROUTE_ADD if you want to pass non v6 nexthops",
1662 __PRETTY_FUNCTION__
);
1663 nexthops_free(re
->ng
.nexthop
);
1664 XFREE(MTYPE_RE
, re
);
1669 max_nh_if
= (nh_count
> if_count
) ? nh_count
: if_count
;
1670 for (i
= 0; i
< max_nh_if
; i
++) {
1672 && !IN6_IS_ADDR_UNSPECIFIED(&nexthops
[i
])) {
1673 if ((i
< if_count
) && ifindices
[i
])
1675 route_entry_nexthop_ipv6_ifindex_add(
1680 nexthop
= route_entry_nexthop_ipv6_add(
1681 re
, &nexthops
[i
], re
->vrf_id
);
1683 if (CHECK_FLAG(message
, ZAPI_MESSAGE_LABEL
))
1684 nexthop_add_labels(nexthop
, label_type
,
1687 if ((i
< if_count
) && ifindices
[i
])
1688 route_entry_nexthop_ifindex_add(
1689 re
, ifindices
[i
], re
->vrf_id
);
1695 if (CHECK_FLAG(message
, ZAPI_MESSAGE_DISTANCE
))
1696 STREAM_GETC(s
, re
->distance
);
1699 if (CHECK_FLAG(message
, ZAPI_MESSAGE_METRIC
))
1700 STREAM_GETL(s
, re
->metric
);
1703 if (CHECK_FLAG(message
, ZAPI_MESSAGE_TAG
))
1704 STREAM_GETL(s
, re
->tag
);
1708 if (CHECK_FLAG(message
, ZAPI_MESSAGE_MTU
))
1709 STREAM_GETL(s
, re
->mtu
);
1714 re
->table
= zvrf
->table_id
;
1716 ret
= rib_add_multipath(AFI_IP6
, safi
, &p
, NULL
, re
);
1719 client
->v4_route_add_cnt
++;
1721 client
->v4_route_upd8_cnt
++;
1726 nexthops_free(re
->ng
.nexthop
);
1727 XFREE(MTYPE_RE
, re
);
1730 static void zread_ipv6_add(ZAPI_HANDLER_ARGS
)
1734 struct in6_addr nhop_addr
;
1736 struct route_entry
*re
;
1739 u_char nexthop_type
;
1741 struct prefix_ipv6 src_p
, *src_pp
;
1743 static struct in6_addr nexthops
[MULTIPATH_NUM
];
1744 static unsigned int ifindices
[MULTIPATH_NUM
];
1746 static mpls_label_t labels
[MULTIPATH_NUM
];
1747 enum lsp_types_t label_type
= ZEBRA_LSP_NONE
;
1749 struct nexthop
*nexthop
;
1750 enum blackhole_type bh_type
= BLACKHOLE_NULL
;
1752 /* Get input stream. */
1755 memset(&nhop_addr
, 0, sizeof(struct in6_addr
));
1757 /* Allocate new re. */
1758 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
1760 /* Type, flags, message. */
1761 STREAM_GETC(s
, re
->type
);
1762 if (re
->type
> ZEBRA_ROUTE_MAX
) {
1763 zlog_warn("%s: Specified route type: %d is not a legal value\n",
1764 __PRETTY_FUNCTION__
, re
->type
);
1765 XFREE(MTYPE_RE
, re
);
1768 STREAM_GETW(s
, re
->instance
);
1769 STREAM_GETL(s
, re
->flags
);
1770 STREAM_GETC(s
, message
);
1771 STREAM_GETW(s
, safi
);
1772 re
->uptime
= time(NULL
);
1775 memset(&p
, 0, sizeof(p
));
1776 p
.family
= AF_INET6
;
1777 STREAM_GETC(s
, p
.prefixlen
);
1778 if (p
.prefixlen
> IPV6_MAX_BITLEN
) {
1780 "%s: Specified prefix length %d is to large for v6 prefix",
1781 __PRETTY_FUNCTION__
, p
.prefixlen
);
1782 XFREE(MTYPE_RE
, re
);
1785 STREAM_GET(&p
.u
.prefix6
, s
, PSIZE(p
.prefixlen
));
1787 if (CHECK_FLAG(message
, ZAPI_MESSAGE_SRCPFX
)) {
1788 memset(&src_p
, 0, sizeof(src_p
));
1789 src_p
.family
= AF_INET6
;
1790 STREAM_GETC(s
, src_p
.prefixlen
);
1791 if (src_p
.prefixlen
> IPV6_MAX_BITLEN
) {
1793 "%s: Specified src prefix length %d is to large for v6 prefix",
1794 __PRETTY_FUNCTION__
, src_p
.prefixlen
);
1795 XFREE(MTYPE_RE
, re
);
1798 STREAM_GET(&src_p
.prefix
, s
, PSIZE(src_p
.prefixlen
));
1804 re
->vrf_id
= zvrf_id(zvrf
);
1806 /* We need to give nh-addr, nh-ifindex with the same next-hop object
1807 * to the re to ensure that IPv6 multipathing works; need to coalesce
1808 * these. Clients should send the same number of paired set of
1809 * next-hop-addr/next-hop-ifindices. */
1810 if (CHECK_FLAG(message
, ZAPI_MESSAGE_NEXTHOP
)) {
1811 unsigned int nh_count
= 0;
1812 unsigned int if_count
= 0;
1813 unsigned int max_nh_if
= 0;
1815 STREAM_GETC(s
, nexthop_num
);
1816 zserv_nexthop_num_warn(__func__
, (const struct prefix
*)&p
,
1819 if (CHECK_FLAG(message
, ZAPI_MESSAGE_LABEL
))
1820 label_type
= lsp_type_from_re_type(client
->proto
);
1822 for (i
= 0; i
< nexthop_num
; i
++) {
1823 STREAM_GETC(s
, nexthop_type
);
1825 switch (nexthop_type
) {
1826 case NEXTHOP_TYPE_IPV6
:
1827 STREAM_GET(&nhop_addr
, s
, 16);
1828 if (nh_count
< MULTIPATH_NUM
) {
1829 /* For labeled-unicast, each nexthop is
1830 * followed by label. */
1831 if (CHECK_FLAG(message
,
1832 ZAPI_MESSAGE_LABEL
)) {
1833 STREAM_GETL(s
, label
);
1834 labels
[nh_count
] = label
;
1836 nexthops
[nh_count
++] = nhop_addr
;
1839 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1840 STREAM_GET(&nhop_addr
, s
, 16);
1841 STREAM_GETL(s
, ifindex
);
1842 route_entry_nexthop_ipv6_ifindex_add(
1843 re
, &nhop_addr
, ifindex
, re
->vrf_id
);
1845 case NEXTHOP_TYPE_IFINDEX
:
1846 if (if_count
< multipath_num
) {
1847 STREAM_GETL(s
, ifindices
[if_count
++]);
1850 case NEXTHOP_TYPE_BLACKHOLE
:
1851 route_entry_nexthop_blackhole_add(re
, bh_type
);
1855 "%s: Please use ZEBRA_ROUTE_ADD if you want to pass non v6 nexthops",
1856 __PRETTY_FUNCTION__
);
1857 nexthops_free(re
->ng
.nexthop
);
1858 XFREE(MTYPE_RE
, re
);
1863 max_nh_if
= (nh_count
> if_count
) ? nh_count
: if_count
;
1864 for (i
= 0; i
< max_nh_if
; i
++) {
1866 && !IN6_IS_ADDR_UNSPECIFIED(&nexthops
[i
])) {
1867 if ((i
< if_count
) && ifindices
[i
])
1869 route_entry_nexthop_ipv6_ifindex_add(
1874 nexthop
= route_entry_nexthop_ipv6_add(
1875 re
, &nexthops
[i
], re
->vrf_id
);
1876 if (CHECK_FLAG(message
, ZAPI_MESSAGE_LABEL
))
1877 nexthop_add_labels(nexthop
, label_type
,
1880 if ((i
< if_count
) && ifindices
[i
])
1881 route_entry_nexthop_ifindex_add(
1882 re
, ifindices
[i
], re
->vrf_id
);
1888 if (CHECK_FLAG(message
, ZAPI_MESSAGE_DISTANCE
))
1889 STREAM_GETC(s
, re
->distance
);
1892 if (CHECK_FLAG(message
, ZAPI_MESSAGE_METRIC
))
1893 STREAM_GETL(s
, re
->metric
);
1896 if (CHECK_FLAG(message
, ZAPI_MESSAGE_TAG
))
1897 STREAM_GETL(s
, re
->tag
);
1901 if (CHECK_FLAG(message
, ZAPI_MESSAGE_MTU
))
1902 STREAM_GETL(s
, re
->mtu
);
1906 re
->table
= zvrf
->table_id
;
1908 ret
= rib_add_multipath(AFI_IP6
, safi
, &p
, src_pp
, re
);
1911 client
->v6_route_add_cnt
++;
1913 client
->v6_route_upd8_cnt
++;
1918 nexthops_free(re
->ng
.nexthop
);
1919 XFREE(MTYPE_RE
, re
);
1922 /* Zebra server IPv6 prefix delete function. */
1923 static void zread_ipv6_delete(ZAPI_HANDLER_ARGS
)
1926 struct zapi_ipv6 api
;
1928 struct prefix_ipv6 src_p
, *src_pp
;
1932 /* Type, flags, message. */
1933 STREAM_GETC(s
, api
.type
);
1934 STREAM_GETW(s
, api
.instance
);
1935 STREAM_GETL(s
, api
.flags
);
1936 STREAM_GETC(s
, api
.message
);
1937 STREAM_GETW(s
, api
.safi
);
1940 memset(&p
, 0, sizeof(struct prefix
));
1941 p
.family
= AF_INET6
;
1942 STREAM_GETC(s
, p
.prefixlen
);
1943 STREAM_GET(&p
.u
.prefix6
, s
, PSIZE(p
.prefixlen
));
1945 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
)) {
1946 memset(&src_p
, 0, sizeof(struct prefix_ipv6
));
1947 src_p
.family
= AF_INET6
;
1948 STREAM_GETC(s
, src_p
.prefixlen
);
1949 STREAM_GET(&src_p
.prefix
, s
, PSIZE(src_p
.prefixlen
));
1954 rib_delete(AFI_IP6
, api
.safi
, zvrf_id(zvrf
), api
.type
, api
.instance
,
1955 api
.flags
, &p
, src_pp
, NULL
, client
->rtm_table
, 0, false,
1958 client
->v6_route_del_cnt
++;
1964 /* Register zebra server router-id information. Send current router-id */
1965 static void zread_router_id_add(ZAPI_HANDLER_ARGS
)
1969 /* Router-id information is needed. */
1970 vrf_bitmap_set(client
->ridinfo
, zvrf_id(zvrf
));
1972 router_id_get(&p
, zvrf_id(zvrf
));
1974 zsend_router_id_update(client
, &p
, zvrf_id(zvrf
));
1977 /* Unregister zebra server router-id information. */
1978 static void zread_router_id_delete(ZAPI_HANDLER_ARGS
)
1980 vrf_bitmap_unset(client
->ridinfo
, zvrf_id(zvrf
));
1983 /* Tie up route-type and client->sock */
1984 static void zread_hello(ZAPI_HANDLER_ARGS
)
1986 /* type of protocol (lib/zebra.h) */
1991 STREAM_GETC(msg
, proto
);
1992 STREAM_GETW(msg
, instance
);
1993 STREAM_GETC(msg
, notify
);
1995 client
->notify_owner
= true;
1997 /* accept only dynamic routing protocols */
1998 if ((proto
< ZEBRA_ROUTE_MAX
) && (proto
> ZEBRA_ROUTE_STATIC
)) {
2000 "client %d says hello and bids fair to announce only %s routes",
2001 client
->sock
, zebra_route_string(proto
));
2003 zlog_notice("client protocol instance %d", instance
);
2005 client
->proto
= proto
;
2006 client
->instance
= instance
;
2013 /* Unregister all information in a VRF. */
2014 static void zread_vrf_unregister(ZAPI_HANDLER_ARGS
)
2019 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
2020 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
2021 vrf_bitmap_unset(client
->redist
[afi
][i
], zvrf_id(zvrf
));
2022 vrf_bitmap_unset(client
->redist_default
, zvrf_id(zvrf
));
2023 vrf_bitmap_unset(client
->ifinfo
, zvrf_id(zvrf
));
2024 vrf_bitmap_unset(client
->ridinfo
, zvrf_id(zvrf
));
2027 static void zread_mpls_labels(ZAPI_HANDLER_ARGS
)
2030 enum lsp_types_t type
;
2031 struct prefix prefix
;
2032 enum nexthop_types_t gtype
;
2035 mpls_label_t in_label
, out_label
;
2038 /* Get input stream. */
2042 STREAM_GETC(s
, type
);
2043 STREAM_GETL(s
, prefix
.family
);
2044 switch (prefix
.family
) {
2046 STREAM_GET(&prefix
.u
.prefix4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
2047 STREAM_GETC(s
, prefix
.prefixlen
);
2048 if (prefix
.prefixlen
> IPV4_MAX_BITLEN
) {
2050 "%s: Specified prefix length %d is greater than a v4 address can support",
2051 __PRETTY_FUNCTION__
, prefix
.prefixlen
);
2054 STREAM_GET(&gate
.ipv4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
2057 STREAM_GET(&prefix
.u
.prefix6
, s
, 16);
2058 STREAM_GETC(s
, prefix
.prefixlen
);
2059 if (prefix
.prefixlen
> IPV6_MAX_BITLEN
) {
2061 "%s: Specified prefix length %d is greater than a v6 address can support",
2062 __PRETTY_FUNCTION__
, prefix
.prefixlen
);
2065 STREAM_GET(&gate
.ipv6
, s
, 16);
2068 zlog_warn("%s: Specified AF %d is not supported for this call",
2069 __PRETTY_FUNCTION__
, prefix
.family
);
2072 STREAM_GETL(s
, ifindex
);
2073 STREAM_GETC(s
, distance
);
2074 STREAM_GETL(s
, in_label
);
2075 STREAM_GETL(s
, out_label
);
2077 switch (prefix
.family
) {
2080 gtype
= NEXTHOP_TYPE_IPV4_IFINDEX
;
2082 gtype
= NEXTHOP_TYPE_IPV4
;
2086 gtype
= NEXTHOP_TYPE_IPV6_IFINDEX
;
2088 gtype
= NEXTHOP_TYPE_IPV6
;
2097 if (hdr
->command
== ZEBRA_MPLS_LABELS_ADD
) {
2098 mpls_lsp_install(zvrf
, type
, in_label
, out_label
, gtype
, &gate
,
2100 mpls_ftn_update(1, zvrf
, type
, &prefix
, gtype
, &gate
, ifindex
,
2101 distance
, out_label
);
2102 } else if (hdr
->command
== ZEBRA_MPLS_LABELS_DELETE
) {
2103 mpls_lsp_uninstall(zvrf
, type
, in_label
, gtype
, &gate
, ifindex
);
2104 mpls_ftn_update(0, zvrf
, type
, &prefix
, gtype
, &gate
, ifindex
,
2105 distance
, out_label
);
2111 static void zread_label_manager_connect(struct zserv
*client
,
2112 struct stream
*msg
, vrf_id_t vrf_id
)
2115 /* type of protocol (lib/zebra.h) */
2119 /* Get input stream. */
2123 STREAM_GETC(s
, proto
);
2124 STREAM_GETW(s
, instance
);
2126 /* accept only dynamic routing protocols */
2127 if ((proto
>= ZEBRA_ROUTE_MAX
) || (proto
<= ZEBRA_ROUTE_STATIC
)) {
2128 zlog_err("client %d has wrong protocol %s", client
->sock
,
2129 zebra_route_string(proto
));
2130 zsend_label_manager_connect_response(client
, vrf_id
, 1);
2133 zlog_notice("client %d with instance %u connected as %s", client
->sock
,
2134 instance
, zebra_route_string(proto
));
2135 client
->proto
= proto
;
2136 client
->instance
= instance
;
2139 Release previous labels of same protocol and instance.
2140 This is done in case it restarted from an unexpected shutdown.
2142 release_daemon_chunks(proto
, instance
);
2145 " Label Manager client connected: sock %d, proto %s, instance %u",
2146 client
->sock
, zebra_route_string(proto
), instance
);
2147 /* send response back */
2148 zsend_label_manager_connect_response(client
, vrf_id
, 0);
2154 static void zread_get_label_chunk(struct zserv
*client
, struct stream
*msg
,
2160 struct label_manager_chunk
*lmc
;
2162 /* Get input stream. */
2166 STREAM_GETC(s
, keep
);
2167 STREAM_GETL(s
, size
);
2169 lmc
= assign_label_chunk(client
->proto
, client
->instance
, keep
, size
);
2171 zlog_err("%s: Unable to assign Label Chunk of size %u",
2174 zlog_debug("Assigned Label Chunk %u - %u to %u", lmc
->start
,
2176 /* send response back */
2177 zsend_assign_label_chunk_response(client
, vrf_id
, lmc
);
2183 static void zread_release_label_chunk(struct zserv
*client
, struct stream
*msg
)
2186 uint32_t start
, end
;
2188 /* Get input stream. */
2192 STREAM_GETL(s
, start
);
2193 STREAM_GETL(s
, end
);
2195 release_label_chunk(client
->proto
, client
->instance
, start
, end
);
2200 static void zread_label_manager_request(ZAPI_HANDLER_ARGS
)
2202 /* to avoid sending other messages like ZERBA_INTERFACE_UP */
2203 if (hdr
->command
== ZEBRA_LABEL_MANAGER_CONNECT
)
2204 client
->is_synchronous
= 1;
2206 /* external label manager */
2208 zread_relay_label_manager_request(hdr
->command
, client
,
2210 /* this is a label manager */
2212 if (hdr
->command
== ZEBRA_LABEL_MANAGER_CONNECT
)
2213 zread_label_manager_connect(client
, msg
, zvrf_id(zvrf
));
2215 /* Sanity: don't allow 'unidentified' requests */
2216 if (!client
->proto
) {
2218 "Got label request from an unidentified client");
2221 if (hdr
->command
== ZEBRA_GET_LABEL_CHUNK
)
2222 zread_get_label_chunk(client
, msg
,
2224 else if (hdr
->command
== ZEBRA_RELEASE_LABEL_CHUNK
)
2225 zread_release_label_chunk(client
, msg
);
2230 static void zread_pseudowire(ZAPI_HANDLER_ARGS
)
2233 char ifname
[IF_NAMESIZE
];
2237 union g_addr nexthop
;
2238 uint32_t local_label
;
2239 uint32_t remote_label
;
2241 union pw_protocol_fields data
;
2243 struct zebra_pw
*pw
;
2245 /* Get input stream. */
2249 STREAM_GET(ifname
, s
, IF_NAMESIZE
);
2250 STREAM_GETL(s
, ifindex
);
2251 STREAM_GETL(s
, type
);
2255 STREAM_GET(&nexthop
.ipv4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
2258 STREAM_GET(&nexthop
.ipv6
, s
, 16);
2263 STREAM_GETL(s
, local_label
);
2264 STREAM_GETL(s
, remote_label
);
2265 STREAM_GETC(s
, flags
);
2266 STREAM_GET(&data
, s
, sizeof(data
));
2267 protocol
= client
->proto
;
2269 pw
= zebra_pw_find(zvrf
, ifname
);
2270 switch (hdr
->command
) {
2273 zlog_warn("%s: pseudowire %s already exists [%s]",
2275 zserv_command_string(hdr
->command
));
2279 zebra_pw_add(zvrf
, ifname
, protocol
, client
);
2281 case ZEBRA_PW_DELETE
:
2283 zlog_warn("%s: pseudowire %s not found [%s]", __func__
,
2284 ifname
, zserv_command_string(hdr
->command
));
2288 zebra_pw_del(zvrf
, pw
);
2291 case ZEBRA_PW_UNSET
:
2293 zlog_warn("%s: pseudowire %s not found [%s]", __func__
,
2294 ifname
, zserv_command_string(hdr
->command
));
2298 switch (hdr
->command
) {
2302 case ZEBRA_PW_UNSET
:
2307 zebra_pw_change(pw
, ifindex
, type
, af
, &nexthop
, local_label
,
2308 remote_label
, flags
, &data
);
2316 /* Cleanup registered nexthops (across VRFs) upon client disconnect. */
2317 static void zebra_client_close_cleanup_rnh(struct zserv
*client
)
2320 struct zebra_vrf
*zvrf
;
2322 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
2323 if ((zvrf
= vrf
->info
) != NULL
) {
2324 zebra_cleanup_rnh_client(zvrf_id(zvrf
), AF_INET
, client
,
2326 zebra_cleanup_rnh_client(zvrf_id(zvrf
), AF_INET6
,
2327 client
, RNH_NEXTHOP_TYPE
);
2328 zebra_cleanup_rnh_client(zvrf_id(zvrf
), AF_INET
, client
,
2329 RNH_IMPORT_CHECK_TYPE
);
2330 zebra_cleanup_rnh_client(zvrf_id(zvrf
), AF_INET6
,
2331 client
, RNH_IMPORT_CHECK_TYPE
);
2332 if (client
->proto
== ZEBRA_ROUTE_LDP
) {
2333 hash_iterate(zvrf
->lsp_table
,
2334 mpls_ldp_lsp_uninstall_all
,
2336 mpls_ldp_ftn_uninstall_all(zvrf
, AFI_IP
);
2337 mpls_ldp_ftn_uninstall_all(zvrf
, AFI_IP6
);
2343 static void zread_interface_set_master(ZAPI_HANDLER_ARGS
)
2345 struct interface
*master
;
2346 struct interface
*slave
;
2347 struct stream
*s
= msg
;
2351 STREAM_GETL(s
, vrf_id
);
2352 STREAM_GETL(s
, ifindex
);
2353 master
= if_lookup_by_index(ifindex
, vrf_id
);
2355 STREAM_GETL(s
, vrf_id
);
2356 STREAM_GETL(s
, ifindex
);
2357 slave
= if_lookup_by_index(ifindex
, vrf_id
);
2359 if (!master
|| !slave
)
2362 kernel_interface_set_master(master
, slave
);
2369 static void zread_vrf_label(ZAPI_HANDLER_ARGS
)
2371 struct interface
*ifp
;
2372 mpls_label_t nlabel
;
2375 struct zebra_vrf
*def_zvrf
;
2376 enum lsp_types_t ltype
;
2379 STREAM_GETL(s
, nlabel
);
2380 STREAM_GETC(s
, afi
);
2381 if (nlabel
== zvrf
->label
[afi
]) {
2383 * Nothing to do here move along
2388 STREAM_GETC(s
, ltype
);
2390 if (zvrf
->vrf
->vrf_id
!= VRF_DEFAULT
)
2391 ifp
= if_lookup_by_name(zvrf
->vrf
->name
, zvrf
->vrf
->vrf_id
);
2393 ifp
= if_lookup_by_name("lo", VRF_DEFAULT
);
2396 zlog_debug("Unable to find specified Interface for %s",
2401 def_zvrf
= zebra_vrf_lookup_by_id(VRF_DEFAULT
);
2403 if (zvrf
->label
[afi
] != MPLS_LABEL_NONE
) {
2407 really_remove
= true;
2408 for (scrubber
= AFI_IP
; scrubber
< AFI_MAX
; scrubber
++) {
2409 if (scrubber
== afi
)
2412 if (zvrf
->label
[scrubber
] == MPLS_LABEL_NONE
)
2415 if (zvrf
->label
[afi
] == zvrf
->label
[scrubber
]) {
2416 really_remove
= false;
2422 mpls_lsp_uninstall(def_zvrf
, ltype
, zvrf
->label
[afi
],
2423 NEXTHOP_TYPE_IFINDEX
, NULL
,
2427 if (nlabel
!= MPLS_LABEL_NONE
)
2428 mpls_lsp_install(def_zvrf
, ltype
, nlabel
,
2429 MPLS_LABEL_IMPLICIT_NULL
, NEXTHOP_TYPE_IFINDEX
,
2430 NULL
, ifp
->ifindex
);
2432 zvrf
->label
[afi
] = nlabel
;
2437 static inline void zread_rule(ZAPI_HANDLER_ARGS
)
2439 struct zebra_pbr_rule zpr
;
2445 STREAM_GETL(s
, total
);
2447 for (i
= 0; i
< total
; i
++) {
2448 memset(&zpr
, 0, sizeof(zpr
));
2450 zpr
.sock
= client
->sock
;
2451 STREAM_GETL(s
, zpr
.seq
);
2452 STREAM_GETL(s
, zpr
.priority
);
2453 STREAM_GETL(s
, zpr
.unique
);
2454 STREAM_GETC(s
, zpr
.filter
.src_ip
.family
);
2455 STREAM_GETC(s
, zpr
.filter
.src_ip
.prefixlen
);
2456 STREAM_GET(&zpr
.filter
.src_ip
.u
.prefix
, s
,
2457 prefix_blen(&zpr
.filter
.src_ip
));
2458 STREAM_GETW(s
, zpr
.filter
.src_port
);
2459 STREAM_GETC(s
, zpr
.filter
.dst_ip
.family
);
2460 STREAM_GETC(s
, zpr
.filter
.dst_ip
.prefixlen
);
2461 STREAM_GET(&zpr
.filter
.dst_ip
.u
.prefix
, s
,
2462 prefix_blen(&zpr
.filter
.dst_ip
));
2463 STREAM_GETW(s
, zpr
.filter
.dst_port
);
2464 STREAM_GETL(s
, zpr
.action
.table
);
2465 STREAM_GETL(s
, ifindex
);
2467 zpr
.ifp
= if_lookup_by_index(ifindex
, VRF_UNKNOWN
);
2469 zlog_debug("FAiled to lookup ifindex: %u", ifindex
);
2473 if (!is_default_prefix(&zpr
.filter
.src_ip
))
2474 zpr
.filter
.filter_bm
|= PBR_FILTER_SRC_IP
;
2476 if (!is_default_prefix(&zpr
.filter
.dst_ip
))
2477 zpr
.filter
.filter_bm
|= PBR_FILTER_DST_IP
;
2479 if (zpr
.filter
.src_port
)
2480 zpr
.filter
.filter_bm
|= PBR_FILTER_SRC_PORT
;
2482 if (zpr
.filter
.dst_port
)
2483 zpr
.filter
.filter_bm
|= PBR_FILTER_DST_PORT
;
2485 if (hdr
->command
== ZEBRA_RULE_ADD
)
2486 zebra_pbr_add_rule(zvrf
->zns
, &zpr
);
2488 zebra_pbr_del_rule(zvrf
->zns
, &zpr
);
2495 void (*zserv_handlers
[])(ZAPI_HANDLER_ARGS
) = {
2496 [ZEBRA_ROUTER_ID_ADD
] = zread_router_id_add
,
2497 [ZEBRA_ROUTER_ID_DELETE
] = zread_router_id_delete
,
2498 [ZEBRA_INTERFACE_ADD
] = zread_interface_add
,
2499 [ZEBRA_INTERFACE_DELETE
] = zread_interface_delete
,
2500 [ZEBRA_ROUTE_ADD
] = zread_route_add
,
2501 [ZEBRA_ROUTE_DELETE
] = zread_route_del
,
2502 [ZEBRA_IPV4_ROUTE_ADD
] = zread_ipv4_add
,
2503 [ZEBRA_IPV4_ROUTE_DELETE
] = zread_ipv4_delete
,
2504 [ZEBRA_IPV4_ROUTE_IPV6_NEXTHOP_ADD
] = zread_ipv4_route_ipv6_nexthop_add
,
2505 [ZEBRA_IPV6_ROUTE_ADD
] = zread_ipv6_add
,
2506 [ZEBRA_IPV6_ROUTE_DELETE
] = zread_ipv6_delete
,
2507 [ZEBRA_REDISTRIBUTE_ADD
] = zebra_redistribute_add
,
2508 [ZEBRA_REDISTRIBUTE_DELETE
] = zebra_redistribute_delete
,
2509 [ZEBRA_REDISTRIBUTE_DEFAULT_ADD
] = zebra_redistribute_default_add
,
2510 [ZEBRA_REDISTRIBUTE_DEFAULT_DELETE
] = zebra_redistribute_default_delete
,
2511 [ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB
] = zread_ipv4_nexthop_lookup_mrib
,
2512 [ZEBRA_HELLO
] = zread_hello
,
2513 [ZEBRA_NEXTHOP_REGISTER
] = zread_rnh_register
,
2514 [ZEBRA_NEXTHOP_UNREGISTER
] = zread_rnh_unregister
,
2515 [ZEBRA_IMPORT_ROUTE_REGISTER
] = zread_rnh_register
,
2516 [ZEBRA_IMPORT_ROUTE_UNREGISTER
] = zread_rnh_unregister
,
2517 [ZEBRA_BFD_DEST_UPDATE
] = zebra_ptm_bfd_dst_register
,
2518 [ZEBRA_BFD_DEST_REGISTER
] = zebra_ptm_bfd_dst_register
,
2519 [ZEBRA_BFD_DEST_DEREGISTER
] = zebra_ptm_bfd_dst_deregister
,
2520 [ZEBRA_VRF_UNREGISTER
] = zread_vrf_unregister
,
2521 [ZEBRA_VRF_LABEL
] = zread_vrf_label
,
2522 [ZEBRA_BFD_CLIENT_REGISTER
] = zebra_ptm_bfd_client_register
,
2523 #if defined(HAVE_RTADV)
2524 [ZEBRA_INTERFACE_ENABLE_RADV
] = zebra_interface_radv_enable
,
2525 [ZEBRA_INTERFACE_DISABLE_RADV
] = zebra_interface_radv_disable
,
2527 [ZEBRA_INTERFACE_ENABLE_RADV
] = NULL
,
2528 [ZEBRA_INTERFACE_DISABLE_RADV
] = NULL
,
2530 [ZEBRA_MPLS_LABELS_ADD
] = zread_mpls_labels
,
2531 [ZEBRA_MPLS_LABELS_DELETE
] = zread_mpls_labels
,
2532 [ZEBRA_IPMR_ROUTE_STATS
] = zebra_ipmr_route_stats
,
2533 [ZEBRA_LABEL_MANAGER_CONNECT
] = zread_label_manager_request
,
2534 [ZEBRA_GET_LABEL_CHUNK
] = zread_label_manager_request
,
2535 [ZEBRA_RELEASE_LABEL_CHUNK
] = zread_label_manager_request
,
2536 [ZEBRA_FEC_REGISTER
] = zread_fec_register
,
2537 [ZEBRA_FEC_UNREGISTER
] = zread_fec_unregister
,
2538 [ZEBRA_ADVERTISE_DEFAULT_GW
] = zebra_vxlan_advertise_gw_macip
,
2539 [ZEBRA_ADVERTISE_SUBNET
] = zebra_vxlan_advertise_subnet
,
2540 [ZEBRA_ADVERTISE_ALL_VNI
] = zebra_vxlan_advertise_all_vni
,
2541 [ZEBRA_REMOTE_VTEP_ADD
] = zebra_vxlan_remote_vtep_add
,
2542 [ZEBRA_REMOTE_VTEP_DEL
] = zebra_vxlan_remote_vtep_del
,
2543 [ZEBRA_REMOTE_MACIP_ADD
] = zebra_vxlan_remote_macip_add
,
2544 [ZEBRA_REMOTE_MACIP_DEL
] = zebra_vxlan_remote_macip_del
,
2545 [ZEBRA_INTERFACE_SET_MASTER
] = zread_interface_set_master
,
2546 [ZEBRA_PW_ADD
] = zread_pseudowire
,
2547 [ZEBRA_PW_DELETE
] = zread_pseudowire
,
2548 [ZEBRA_PW_SET
] = zread_pseudowire
,
2549 [ZEBRA_PW_UNSET
] = zread_pseudowire
,
2550 [ZEBRA_RULE_ADD
] = zread_rule
,
2551 [ZEBRA_RULE_DELETE
] = zread_rule
,
2554 static inline void zserv_handle_commands(struct zserv
*client
,
2555 struct zmsghdr
*hdr
,
2557 struct zebra_vrf
*zvrf
)
2559 if (hdr
->command
> sizeof(zserv_handlers
)
2560 || zserv_handlers
[hdr
->command
] == NULL
)
2561 zlog_info("Zebra received unknown command %d", hdr
->command
);
2563 zserv_handlers
[hdr
->command
](client
, hdr
, msg
, zvrf
);
2568 /* Lifecycle ---------------------------------------------------------------- */
2570 /* free zebra client information. */
2571 static void zebra_client_free(struct zserv
*client
)
2573 /* Send client de-registration to BFD */
2574 zebra_ptm_bfd_client_deregister(client
->proto
);
2576 /* Cleanup any registered nexthops - across all VRFs. */
2577 zebra_client_close_cleanup_rnh(client
);
2579 /* Release Label Manager chunks */
2580 release_daemon_chunks(client
->proto
, client
->instance
);
2582 /* Cleanup any FECs registered by this client. */
2583 zebra_mpls_cleanup_fecs_for_client(vrf_info_lookup(VRF_DEFAULT
),
2586 /* Remove pseudowires associated with this client */
2587 zebra_pw_client_close(client
);
2589 /* Close file descriptor. */
2591 unsigned long nroutes
;
2593 close(client
->sock
);
2594 nroutes
= rib_score_proto(client
->proto
, client
->instance
);
2596 "client %d disconnected. %lu %s routes removed from the rib",
2597 client
->sock
, nroutes
,
2598 zebra_route_string(client
->proto
));
2602 /* Free stream buffers. */
2603 if (client
->ibuf_work
)
2604 stream_free(client
->ibuf_work
);
2605 if (client
->obuf_work
)
2606 stream_free(client
->obuf_work
);
2607 if (client
->ibuf_fifo
)
2608 stream_fifo_free(client
->ibuf_fifo
);
2609 if (client
->obuf_fifo
)
2610 stream_fifo_free(client
->obuf_fifo
);
2612 buffer_free(client
->wb
);
2614 /* Release threads. */
2616 thread_cancel(client
->t_read
);
2617 if (client
->t_write
)
2618 thread_cancel(client
->t_write
);
2619 if (client
->t_suicide
)
2620 thread_cancel(client
->t_suicide
);
2623 for (afi_t afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
2624 for (int i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
2625 vrf_bitmap_free(client
->redist
[afi
][i
]);
2627 vrf_bitmap_free(client
->redist_default
);
2628 vrf_bitmap_free(client
->ifinfo
);
2629 vrf_bitmap_free(client
->ridinfo
);
2631 XFREE(MTYPE_TMP
, client
);
2635 * Called from client thread to terminate itself.
2637 static void zebra_client_close(struct zserv
*client
)
2639 listnode_delete(zebrad
.client_list
, client
);
2640 zebra_client_free(client
);
2643 /* Make new client. */
2644 static void zebra_client_create(int sock
)
2646 struct zserv
*client
;
2650 client
= XCALLOC(MTYPE_TMP
, sizeof(struct zserv
));
2652 /* Make client input/output buffer. */
2653 client
->sock
= sock
;
2654 client
->ibuf_fifo
= stream_fifo_new();
2655 client
->obuf_fifo
= stream_fifo_new();
2656 client
->ibuf_work
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
2657 client
->obuf_work
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
2658 client
->wb
= buffer_new(0);
2660 /* Set table number. */
2661 client
->rtm_table
= zebrad
.rtm_table_default
;
2663 client
->connect_time
= monotime(NULL
);
2664 /* Initialize flags */
2665 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
2666 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
2667 client
->redist
[afi
][i
] = vrf_bitmap_init();
2668 client
->redist_default
= vrf_bitmap_init();
2669 client
->ifinfo
= vrf_bitmap_init();
2670 client
->ridinfo
= vrf_bitmap_init();
2672 /* by default, it's not a synchronous client */
2673 client
->is_synchronous
= 0;
2675 /* Add this client to linked list. */
2676 listnode_add(zebrad
.client_list
, client
);
2678 zebra_vrf_update_all(client
);
2680 /* start read loop */
2681 zebra_event(client
, ZEBRA_READ
);
2684 static int zserv_delayed_close(struct thread
*thread
)
2686 struct zserv
*client
= THREAD_ARG(thread
);
2688 client
->t_suicide
= NULL
;
2689 zebra_client_close(client
);
2694 * Log zapi message to zlog.
2703 * The message header
2705 static void zserv_log_message(const char *errmsg
, struct stream
*msg
,
2706 struct zmsghdr
*hdr
)
2708 zlog_debug("Rx'd ZAPI message");
2710 zlog_debug("%s", errmsg
);
2712 zlog_debug(" Length: %d", hdr
->length
);
2713 zlog_debug("Command: %s", zserv_command_string(hdr
->command
));
2714 zlog_debug(" VRF: %u", hdr
->vrf_id
);
2716 zlog_hexdump(msg
->data
, STREAM_READABLE(msg
));
2719 static int zserv_flush_data(struct thread
*thread
)
2721 struct zserv
*client
= THREAD_ARG(thread
);
2723 client
->t_write
= NULL
;
2724 if (client
->t_suicide
) {
2725 zebra_client_close(client
);
2728 switch (buffer_flush_available(client
->wb
, client
->sock
)) {
2731 "%s: buffer_flush_available failed on zserv client fd %d, closing",
2732 __func__
, client
->sock
);
2733 zebra_client_close(client
);
2736 case BUFFER_PENDING
:
2737 client
->t_write
= NULL
;
2738 thread_add_write(zebrad
.master
, zserv_flush_data
, client
,
2739 client
->sock
, &client
->t_write
);
2746 client
->last_write_time
= monotime(NULL
);
2751 * Write a single packet.
2753 static int zserv_write(struct thread
*thread
)
2755 struct zserv
*client
= THREAD_ARG(thread
);
2759 if (client
->t_suicide
)
2762 if (client
->is_synchronous
)
2765 msg
= stream_fifo_pop(client
->obuf_fifo
);
2766 stream_set_getp(msg
, 0);
2767 client
->last_write_cmd
= stream_getw_from(msg
, 6);
2769 writerv
= buffer_write(client
->wb
, client
->sock
, STREAM_DATA(msg
),
2770 stream_get_endp(msg
));
2777 "%s: buffer_write failed to zserv client fd %d, closing",
2778 __func__
, client
->sock
);
2780 * Schedule a delayed close since many of the functions that
2781 * call this one do not check the return code. They do not
2782 * allow for the possibility that an I/O error may have caused
2783 * the client to be deleted.
2785 client
->t_suicide
= NULL
;
2786 thread_add_event(zebrad
.master
, zserv_delayed_close
, client
, 0,
2787 &client
->t_suicide
);
2790 THREAD_OFF(client
->t_write
);
2792 case BUFFER_PENDING
:
2793 thread_add_write(zebrad
.master
, zserv_flush_data
, client
,
2794 client
->sock
, &client
->t_write
);
2798 if (client
->obuf_fifo
->count
)
2799 zebra_event(client
, ZEBRA_WRITE
);
2801 client
->last_write_time
= monotime(NULL
);
2805 #if defined(HANDLE_ZAPI_FUZZING)
2806 static void zserv_write_incoming(struct stream
*orig
, uint16_t command
)
2808 char fname
[MAXPATHLEN
];
2809 struct stream
*copy
;
2812 copy
= stream_dup(orig
);
2813 stream_set_getp(copy
, 0);
2815 zserv_privs
.change(ZPRIVS_RAISE
);
2816 snprintf(fname
, MAXPATHLEN
, "%s/%u", DAEMON_VTY_DIR
, command
);
2817 fd
= open(fname
, O_CREAT
| O_WRONLY
| O_EXCL
, 0644);
2818 stream_flush(copy
, fd
);
2820 zserv_privs
.change(ZPRIVS_LOWER
);
2825 static int zserv_process_messages(struct thread
*thread
)
2827 struct zserv
*client
= THREAD_ARG(thread
);
2828 struct zebra_vrf
*zvrf
;
2834 msg
= stream_fifo_pop(client
->ibuf_fifo
);
2836 /* break if out of messages */
2840 /* read & check header */
2841 hdrvalid
= zapi_parse_header(msg
, &hdr
);
2842 if (!hdrvalid
&& IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
) {
2843 const char *emsg
= "Message has corrupt header";
2844 zserv_log_message(emsg
, msg
, NULL
);
2850 zvrf
= zebra_vrf_lookup_by_id(hdr
.vrf_id
);
2851 if (!zvrf
&& IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
) {
2852 const char *emsg
= "Message specifies unknown VRF";
2853 zserv_log_message(emsg
, msg
, &hdr
);
2858 /* process commands */
2859 zserv_handle_commands(client
, &hdr
, msg
, zvrf
);
2866 /* Handler of zebra service request. */
2867 static int zserv_read(struct thread
*thread
)
2870 struct zserv
*client
;
2872 #if defined(HANDLE_ZAPI_FUZZING)
2875 int packets
= zebrad
.packets_to_process
;
2877 /* Get thread data. Reset reading thread because I'm running. */
2878 sock
= THREAD_FD(thread
);
2879 client
= THREAD_ARG(thread
);
2881 if (client
->t_suicide
) {
2882 zebra_client_close(client
);
2892 already
= stream_get_endp(client
->ibuf_work
);
2894 /* Read length and command (if we don't have it already). */
2895 if (already
< ZEBRA_HEADER_SIZE
) {
2896 nb
= stream_read_try(client
->ibuf_work
, sock
,
2897 ZEBRA_HEADER_SIZE
- already
);
2898 if ((nb
== 0 || nb
== -1) && IS_ZEBRA_DEBUG_EVENT
)
2899 zlog_debug("connection closed socket [%d]",
2901 if ((nb
== 0 || nb
== -1))
2903 if (nb
!= (ssize_t
)(ZEBRA_HEADER_SIZE
- already
)) {
2904 /* Try again later. */
2907 already
= ZEBRA_HEADER_SIZE
;
2910 /* Reset to read from the beginning of the incoming packet. */
2911 stream_set_getp(client
->ibuf_work
, 0);
2913 /* Fetch header values */
2914 hdrvalid
= zapi_parse_header(client
->ibuf_work
, &hdr
);
2917 snprintf(errmsg
, sizeof(errmsg
),
2918 "%s: Message has corrupt header", __func__
);
2919 zserv_log_message(errmsg
, client
->ibuf_work
, NULL
);
2923 /* Validate header */
2924 if (hdr
.marker
!= ZEBRA_HEADER_MARKER
2925 || hdr
.version
!= ZSERV_VERSION
) {
2927 errmsg
, sizeof(errmsg
),
2928 "Message has corrupt header\n%s: socket %d version mismatch, marker %d, version %d",
2929 __func__
, sock
, hdr
.marker
, hdr
.version
);
2930 zserv_log_message(errmsg
, client
->ibuf_work
, &hdr
);
2933 if (hdr
.length
< ZEBRA_HEADER_SIZE
) {
2935 errmsg
, sizeof(errmsg
),
2936 "Message has corrupt header\n%s: socket %d message length %u is less than header size %d",
2937 __func__
, sock
, hdr
.length
, ZEBRA_HEADER_SIZE
);
2938 zserv_log_message(errmsg
, client
->ibuf_work
, &hdr
);
2941 if (hdr
.length
> STREAM_SIZE(client
->ibuf_work
)) {
2943 errmsg
, sizeof(errmsg
),
2944 "Message has corrupt header\n%s: socket %d message length %u exceeds buffer size %lu",
2945 __func__
, sock
, hdr
.length
,
2946 (unsigned long)STREAM_SIZE(client
->ibuf_work
));
2950 /* Read rest of data. */
2951 if (already
< hdr
.length
) {
2952 nb
= stream_read_try(client
->ibuf_work
, sock
,
2953 hdr
.length
- already
);
2954 if ((nb
== 0 || nb
== -1) && IS_ZEBRA_DEBUG_EVENT
)
2956 "connection closed [%d] when reading zebra data",
2958 if ((nb
== 0 || nb
== -1))
2960 if (nb
!= (ssize_t
)(hdr
.length
- already
)) {
2961 /* Try again later. */
2966 #if defined(HANDLE_ZAPI_FUZZING)
2967 zserv_write_incoming(client
->ibuf_work
, command
);
2969 hdr
.length
-= ZEBRA_HEADER_SIZE
;
2971 /* Debug packet information. */
2972 if (IS_ZEBRA_DEBUG_EVENT
)
2973 zlog_debug("zebra message comes from socket [%d]",
2976 if (IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
)
2977 zserv_log_message(NULL
, client
->ibuf_work
, &hdr
);
2979 client
->last_read_time
= monotime(NULL
);
2980 client
->last_read_cmd
= hdr
.command
;
2982 stream_set_getp(client
->ibuf_work
, 0);
2983 struct stream
*msg
= stream_dup(client
->ibuf_work
);
2985 stream_fifo_push(client
->ibuf_fifo
, msg
);
2987 if (client
->t_suicide
)
2991 stream_reset(client
->ibuf_work
);
2994 if (IS_ZEBRA_DEBUG_PACKET
)
2995 zlog_debug("Read %d packets",
2996 zebrad
.packets_to_process
- packets
);
2998 /* Schedule job to process those packets */
2999 thread_add_event(zebrad
.master
, &zserv_process_messages
, client
, 0,
3002 /* Reschedule ourselves */
3003 zebra_event(client
, ZEBRA_READ
);
3008 zebra_client_close(client
);
3012 static void zebra_event(struct zserv
*client
, enum event event
)
3016 thread_add_read(zebrad
.master
, zserv_read
, client
, client
->sock
,
3020 thread_add_write(zebrad
.master
, zserv_write
, client
,
3021 client
->sock
, &client
->t_write
);
3026 /* Accept code of zebra server socket. */
3027 static int zebra_accept(struct thread
*thread
)
3031 struct sockaddr_in client
;
3034 accept_sock
= THREAD_FD(thread
);
3036 /* Reregister myself. */
3037 thread_add_read(zebrad
.master
, zebra_accept
, NULL
, accept_sock
, NULL
);
3039 len
= sizeof(struct sockaddr_in
);
3040 client_sock
= accept(accept_sock
, (struct sockaddr
*)&client
, &len
);
3042 if (client_sock
< 0) {
3043 zlog_warn("Can't accept zebra socket: %s",
3044 safe_strerror(errno
));
3048 /* Make client socket non-blocking. */
3049 set_nonblocking(client_sock
);
3051 /* Create new zebra client. */
3052 zebra_client_create(client_sock
);
3057 /* Make zebra server socket, wiping any existing one (see bug #403). */
3058 void zebra_zserv_socket_init(char *path
)
3063 struct sockaddr_storage sa
;
3066 if (!frr_zclient_addr(&sa
, &sa_len
, path
))
3067 /* should be caught in zebra main() */
3071 old_mask
= umask(0077);
3073 /* Make UNIX domain socket. */
3074 sock
= socket(sa
.ss_family
, SOCK_STREAM
, 0);
3076 zlog_warn("Can't create zserv socket: %s",
3077 safe_strerror(errno
));
3079 "zebra can't provide full functionality due to above error");
3083 if (sa
.ss_family
!= AF_UNIX
) {
3084 sockopt_reuseaddr(sock
);
3085 sockopt_reuseport(sock
);
3087 struct sockaddr_un
*suna
= (struct sockaddr_un
*)&sa
;
3088 if (suna
->sun_path
[0])
3089 unlink(suna
->sun_path
);
3092 zserv_privs
.change(ZPRIVS_RAISE
);
3093 setsockopt_so_recvbuf(sock
, 1048576);
3094 setsockopt_so_sendbuf(sock
, 1048576);
3095 zserv_privs
.change(ZPRIVS_LOWER
);
3097 if (sa
.ss_family
!= AF_UNIX
&& zserv_privs
.change(ZPRIVS_RAISE
))
3098 zlog_err("Can't raise privileges");
3100 ret
= bind(sock
, (struct sockaddr
*)&sa
, sa_len
);
3102 zlog_warn("Can't bind zserv socket on %s: %s", path
,
3103 safe_strerror(errno
));
3105 "zebra can't provide full functionality due to above error");
3109 if (sa
.ss_family
!= AF_UNIX
&& zserv_privs
.change(ZPRIVS_LOWER
))
3110 zlog_err("Can't lower privileges");
3112 ret
= listen(sock
, 5);
3114 zlog_warn("Can't listen to zserv socket %s: %s", path
,
3115 safe_strerror(errno
));
3117 "zebra can't provide full functionality due to above error");
3124 thread_add_read(zebrad
.master
, zebra_accept
, NULL
, sock
, NULL
);
3127 #define ZEBRA_TIME_BUF 32
3128 static char *zserv_time_buf(time_t *time1
, char *buf
, int buflen
)
3133 assert(buf
!= NULL
);
3134 assert(buflen
>= ZEBRA_TIME_BUF
);
3135 assert(time1
!= NULL
);
3138 snprintf(buf
, buflen
, "never ");
3142 now
= monotime(NULL
);
3146 if (now
< ONE_DAY_SECOND
)
3147 snprintf(buf
, buflen
, "%02d:%02d:%02d", tm
->tm_hour
, tm
->tm_min
,
3149 else if (now
< ONE_WEEK_SECOND
)
3150 snprintf(buf
, buflen
, "%dd%02dh%02dm", tm
->tm_yday
, tm
->tm_hour
,
3153 snprintf(buf
, buflen
, "%02dw%dd%02dh", tm
->tm_yday
/ 7,
3154 tm
->tm_yday
- ((tm
->tm_yday
/ 7) * 7), tm
->tm_hour
);
3158 static void zebra_show_client_detail(struct vty
*vty
, struct zserv
*client
)
3160 char cbuf
[ZEBRA_TIME_BUF
], rbuf
[ZEBRA_TIME_BUF
];
3161 char wbuf
[ZEBRA_TIME_BUF
], nhbuf
[ZEBRA_TIME_BUF
], mbuf
[ZEBRA_TIME_BUF
];
3163 vty_out(vty
, "Client: %s", zebra_route_string(client
->proto
));
3164 if (client
->instance
)
3165 vty_out(vty
, " Instance: %d", client
->instance
);
3168 vty_out(vty
, "------------------------ \n");
3169 vty_out(vty
, "FD: %d \n", client
->sock
);
3170 vty_out(vty
, "Route Table ID: %d \n", client
->rtm_table
);
3172 vty_out(vty
, "Connect Time: %s \n",
3173 zserv_time_buf(&client
->connect_time
, cbuf
, ZEBRA_TIME_BUF
));
3174 if (client
->nh_reg_time
) {
3175 vty_out(vty
, "Nexthop Registry Time: %s \n",
3176 zserv_time_buf(&client
->nh_reg_time
, nhbuf
,
3178 if (client
->nh_last_upd_time
)
3179 vty_out(vty
, "Nexthop Last Update Time: %s \n",
3180 zserv_time_buf(&client
->nh_last_upd_time
, mbuf
,
3183 vty_out(vty
, "No Nexthop Update sent\n");
3185 vty_out(vty
, "Not registered for Nexthop Updates\n");
3187 vty_out(vty
, "Last Msg Rx Time: %s \n",
3188 zserv_time_buf(&client
->last_read_time
, rbuf
, ZEBRA_TIME_BUF
));
3189 vty_out(vty
, "Last Msg Tx Time: %s \n",
3190 zserv_time_buf(&client
->last_write_time
, wbuf
, ZEBRA_TIME_BUF
));
3191 if (client
->last_read_time
)
3192 vty_out(vty
, "Last Rcvd Cmd: %s \n",
3193 zserv_command_string(client
->last_read_cmd
));
3194 if (client
->last_write_time
)
3195 vty_out(vty
, "Last Sent Cmd: %s \n",
3196 zserv_command_string(client
->last_write_cmd
));
3199 vty_out(vty
, "Type Add Update Del \n");
3200 vty_out(vty
, "================================================== \n");
3201 vty_out(vty
, "IPv4 %-12d%-12d%-12d\n", client
->v4_route_add_cnt
,
3202 client
->v4_route_upd8_cnt
, client
->v4_route_del_cnt
);
3203 vty_out(vty
, "IPv6 %-12d%-12d%-12d\n", client
->v6_route_add_cnt
,
3204 client
->v6_route_upd8_cnt
, client
->v6_route_del_cnt
);
3205 vty_out(vty
, "Redist:v4 %-12d%-12d%-12d\n", client
->redist_v4_add_cnt
,
3206 0, client
->redist_v4_del_cnt
);
3207 vty_out(vty
, "Redist:v6 %-12d%-12d%-12d\n", client
->redist_v6_add_cnt
,
3208 0, client
->redist_v6_del_cnt
);
3209 vty_out(vty
, "Connected %-12d%-12d%-12d\n", client
->ifadd_cnt
, 0,
3211 vty_out(vty
, "BFD peer %-12d%-12d%-12d\n", client
->bfd_peer_add_cnt
,
3212 client
->bfd_peer_upd8_cnt
, client
->bfd_peer_del_cnt
);
3213 vty_out(vty
, "Interface Up Notifications: %d\n", client
->ifup_cnt
);
3214 vty_out(vty
, "Interface Down Notifications: %d\n", client
->ifdown_cnt
);
3215 vty_out(vty
, "VNI add notifications: %d\n", client
->vniadd_cnt
);
3216 vty_out(vty
, "VNI delete notifications: %d\n", client
->vnidel_cnt
);
3217 vty_out(vty
, "L3-VNI add notifications: %d\n", client
->l3vniadd_cnt
);
3218 vty_out(vty
, "L3-VNI delete notifications: %d\n", client
->l3vnidel_cnt
);
3219 vty_out(vty
, "MAC-IP add notifications: %d\n", client
->macipadd_cnt
);
3220 vty_out(vty
, "MAC-IP delete notifications: %d\n", client
->macipdel_cnt
);
3226 static void zebra_show_client_brief(struct vty
*vty
, struct zserv
*client
)
3228 char cbuf
[ZEBRA_TIME_BUF
], rbuf
[ZEBRA_TIME_BUF
];
3229 char wbuf
[ZEBRA_TIME_BUF
];
3231 vty_out(vty
, "%-8s%12s %12s%12s%8d/%-8d%8d/%-8d\n",
3232 zebra_route_string(client
->proto
),
3233 zserv_time_buf(&client
->connect_time
, cbuf
, ZEBRA_TIME_BUF
),
3234 zserv_time_buf(&client
->last_read_time
, rbuf
, ZEBRA_TIME_BUF
),
3235 zserv_time_buf(&client
->last_write_time
, wbuf
, ZEBRA_TIME_BUF
),
3236 client
->v4_route_add_cnt
+ client
->v4_route_upd8_cnt
,
3237 client
->v4_route_del_cnt
,
3238 client
->v6_route_add_cnt
+ client
->v6_route_upd8_cnt
,
3239 client
->v6_route_del_cnt
);
3242 struct zserv
*zebra_find_client(u_char proto
, u_short instance
)
3244 struct listnode
*node
, *nnode
;
3245 struct zserv
*client
;
3247 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, node
, nnode
, client
)) {
3248 if (client
->proto
== proto
&& client
->instance
== instance
)
3255 /* This command is for debugging purpose. */
3256 DEFUN (show_zebra_client
,
3257 show_zebra_client_cmd
,
3258 "show zebra client",
3261 "Client information\n")
3263 struct listnode
*node
;
3264 struct zserv
*client
;
3266 for (ALL_LIST_ELEMENTS_RO(zebrad
.client_list
, node
, client
))
3267 zebra_show_client_detail(vty
, client
);
3272 /* This command is for debugging purpose. */
3273 DEFUN (show_zebra_client_summary
,
3274 show_zebra_client_summary_cmd
,
3275 "show zebra client summary",
3278 "Client information brief\n"
3281 struct listnode
*node
;
3282 struct zserv
*client
;
3285 "Name Connect Time Last Read Last Write IPv4 Routes IPv6 Routes \n");
3287 "--------------------------------------------------------------------------------\n");
3289 for (ALL_LIST_ELEMENTS_RO(zebrad
.client_list
, node
, client
))
3290 zebra_show_client_brief(vty
, client
);
3292 vty_out(vty
, "Routes column shows (added+updated)/deleted\n");
3296 #if defined(HANDLE_ZAPI_FUZZING)
3297 void zserv_read_file(char *input
)
3300 struct zserv
*client
= NULL
;
3303 zebra_client_create(-1);
3304 client
= zebrad
.client_list
->head
->data
;
3307 fd
= open(input
, O_RDONLY
| O_NONBLOCK
);
3310 zebra_client_read(&t
);
3316 void zserv_init(void)
3318 /* Client list init. */
3319 zebrad
.client_list
= list_new();
3320 zebrad
.client_list
->del
= (void (*)(void *))zebra_client_free
;
3322 install_element(ENABLE_NODE
, &show_zebra_client_cmd
);
3323 install_element(ENABLE_NODE
, &show_zebra_client_summary_cmd
);