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)
601 if (IS_ZEBRA_DEBUG_SEND
) {
602 char buf_prefix
[PREFIX_STRLEN
];
603 prefix2str(&api
.prefix
, buf_prefix
, sizeof(buf_prefix
));
605 zlog_debug("%s: %s to client %s: type %s, vrf_id %d, p %s",
606 __func__
, zserv_command_string(cmd
),
607 zebra_route_string(client
->proto
),
608 zebra_route_string(api
.type
), api
.vrf_id
,
611 return zebra_server_send_message(client
);
614 static int zsend_write_nexthop(struct stream
*s
, struct nexthop
*nexthop
)
616 stream_putc(s
, nexthop
->type
);
617 switch (nexthop
->type
) {
618 case NEXTHOP_TYPE_IPV4
:
619 case NEXTHOP_TYPE_IPV4_IFINDEX
:
620 stream_put_in_addr(s
, &nexthop
->gate
.ipv4
);
621 stream_putl(s
, nexthop
->ifindex
);
623 case NEXTHOP_TYPE_IPV6
:
624 stream_put(s
, &nexthop
->gate
.ipv6
, 16);
626 case NEXTHOP_TYPE_IPV6_IFINDEX
:
627 stream_put(s
, &nexthop
->gate
.ipv6
, 16);
628 stream_putl(s
, nexthop
->ifindex
);
630 case NEXTHOP_TYPE_IFINDEX
:
631 stream_putl(s
, nexthop
->ifindex
);
641 * Modified version of zsend_ipv4_nexthop_lookup(): Query unicast rib if
642 * nexthop is not found on mrib. Returns both route metric and protocol
645 static int zsend_ipv4_nexthop_lookup_mrib(struct zserv
*client
,
647 struct route_entry
*re
,
648 struct zebra_vrf
*zvrf
)
653 struct nexthop
*nexthop
;
655 /* Get output stream. */
656 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
659 /* Fill in result. */
660 zclient_create_header(s
, ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB
, zvrf_id(zvrf
));
661 stream_put_in_addr(s
, &addr
);
664 stream_putc(s
, re
->distance
);
665 stream_putl(s
, re
->metric
);
667 nump
= stream_get_endp(
668 s
); /* remember position for nexthop_num */
669 stream_putc(s
, 0); /* reserve room for nexthop_num */
670 /* Only non-recursive routes are elegible to resolve the nexthop
672 * are looking up. Therefore, we will just iterate over the top
673 * chain of nexthops. */
674 for (nexthop
= re
->ng
.nexthop
; nexthop
; nexthop
= nexthop
->next
)
675 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
676 num
+= zserv_encode_nexthop(s
, nexthop
);
678 stream_putc_at(s
, nump
, num
); /* store nexthop_num */
680 stream_putc(s
, 0); /* distance */
681 stream_putl(s
, 0); /* metric */
682 stream_putc(s
, 0); /* nexthop_num */
685 stream_putw_at(s
, 0, stream_get_endp(s
));
687 return zebra_server_send_message(client
, s
);
690 int zsend_route_notify_owner(struct route_entry
*re
, struct prefix
*p
,
691 enum zapi_route_notify_owner note
)
693 struct zserv
*client
;
697 client
= zebra_find_client(re
->type
, re
->instance
);
698 if (!client
|| !client
->notify_owner
) {
699 if (IS_ZEBRA_DEBUG_PACKET
) {
700 char buff
[PREFIX_STRLEN
];
703 "Not Notifying Owner: %u about prefix %s(%u) %d",
704 re
->type
, prefix2str(p
, buff
, sizeof(buff
)),
710 if (IS_ZEBRA_DEBUG_PACKET
) {
711 char buff
[PREFIX_STRLEN
];
713 zlog_debug("Notifying Owner: %u about prefix %s(%u) %d",
714 re
->type
, prefix2str(p
, buff
, sizeof(buff
)),
718 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
721 zclient_create_header(s
, ZEBRA_ROUTE_NOTIFY_OWNER
, re
->vrf_id
);
723 stream_put(s
, ¬e
, sizeof(note
));
725 stream_putc(s
, p
->family
);
727 blen
= prefix_blen(p
);
728 stream_putc(s
, p
->prefixlen
);
729 stream_put(s
, &p
->u
.prefix
, blen
);
731 stream_putl(s
, re
->table
);
733 stream_putw_at(s
, 0, stream_get_endp(s
));
735 return zebra_server_send_message(client
, s
);
738 void zsend_rule_notify_owner(struct zebra_pbr_rule
*rule
,
739 enum zapi_rule_notify_owner note
)
741 struct listnode
*node
;
742 struct zserv
*client
;
745 if (IS_ZEBRA_DEBUG_PACKET
) {
746 zlog_debug("%s: Notifying %u", __PRETTY_FUNCTION__
,
750 for (ALL_LIST_ELEMENTS_RO(zebrad
.client_list
, node
, client
)) {
751 if (rule
->sock
== client
->sock
)
758 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
761 zclient_create_header(s
, ZEBRA_RULE_NOTIFY_OWNER
, VRF_DEFAULT
);
762 stream_put(s
, ¬e
, sizeof(note
));
763 stream_putl(s
, rule
->seq
);
764 stream_putl(s
, rule
->priority
);
765 stream_putl(s
, rule
->unique
);
767 stream_putl(s
, rule
->ifp
->ifindex
);
771 stream_putw_at(s
, 0, stream_get_endp(s
));
773 zebra_server_send_message(client
, s
);
776 /* Router-id is updated. Send ZEBRA_ROUTER_ID_ADD to client. */
777 int zsend_router_id_update(struct zserv
*client
, struct prefix
*p
,
782 /* Check this client need interface information. */
783 if (!vrf_bitmap_check(client
->ridinfo
, vrf_id
))
786 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
789 zclient_create_header(s
, ZEBRA_ROUTER_ID_UPDATE
, vrf_id
);
791 /* Prefix information. */
792 stream_putc(s
, p
->family
);
793 blen
= prefix_blen(p
);
794 stream_put(s
, &p
->u
.prefix
, blen
);
795 stream_putc(s
, p
->prefixlen
);
797 /* Write packet size. */
798 stream_putw_at(s
, 0, stream_get_endp(s
));
800 return zebra_server_send_message(client
, s
);
804 * Function used by Zebra to send a PW status update to LDP daemon
806 int zsend_pw_update(struct zserv
*client
, struct zebra_pw
*pw
)
808 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
810 zclient_create_header(s
, ZEBRA_PW_STATUS_UPDATE
, pw
->vrf_id
);
811 stream_write(s
, pw
->ifname
, IF_NAMESIZE
);
812 stream_putl(s
, pw
->ifindex
);
813 stream_putl(s
, pw
->status
);
815 /* Put length at the first point of the stream. */
816 stream_putw_at(s
, 0, stream_get_endp(s
));
818 return zebra_server_send_message(client
, s
);
821 /* Send response to a get label chunk request to client */
822 static int zsend_assign_label_chunk_response(struct zserv
*client
,
824 struct label_manager_chunk
*lmc
)
827 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
829 zclient_create_header(s
, ZEBRA_GET_LABEL_CHUNK
, vrf_id
);
833 stream_putc(s
, lmc
->keep
);
834 /* start and end labels */
835 stream_putl(s
, lmc
->start
);
836 stream_putl(s
, lmc
->end
);
839 /* Write packet size. */
840 stream_putw_at(s
, 0, stream_get_endp(s
));
842 ret
= writen(client
->sock
, s
->data
, stream_get_endp(s
));
847 /* Send response to a label manager connect request to client */
848 static int zsend_label_manager_connect_response(struct zserv
*client
,
849 vrf_id_t vrf_id
, u_short result
)
852 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
854 zclient_create_header(s
, ZEBRA_LABEL_MANAGER_CONNECT
, vrf_id
);
857 stream_putc(s
, result
);
859 /* Write packet size. */
860 stream_putw_at(s
, 0, stream_get_endp(s
));
862 ret
= writen(client
->sock
, s
->data
, stream_get_endp(s
));
868 /* Inbound message handling ------------------------------------------------ */
871 [ZEBRA_NEXTHOP_REGISTER
] = RNH_NEXTHOP_TYPE
,
872 [ZEBRA_NEXTHOP_UNREGISTER
] = RNH_NEXTHOP_TYPE
,
873 [ZEBRA_IMPORT_ROUTE_REGISTER
] = RNH_IMPORT_CHECK_TYPE
,
874 [ZEBRA_IMPORT_ROUTE_UNREGISTER
] = RNH_IMPORT_CHECK_TYPE
,
877 /* Nexthop register */
878 static void zread_rnh_register(ZAPI_HANDLER_ARGS
)
885 uint16_t type
= cmd2type
[hdr
->command
];
887 if (IS_ZEBRA_DEBUG_NHT
)
889 "rnh_register msg from client %s: hdr->length=%d, type=%s\n",
890 zebra_route_string(client
->proto
), hdr
->length
,
891 (type
== RNH_NEXTHOP_TYPE
) ? "nexthop" : "route");
895 client
->nh_reg_time
= monotime(NULL
);
897 while (l
< hdr
->length
) {
898 STREAM_GETC(s
, flags
);
899 STREAM_GETW(s
, p
.family
);
900 STREAM_GETC(s
, p
.prefixlen
);
902 if (p
.family
== AF_INET
) {
903 if (p
.prefixlen
> IPV4_MAX_BITLEN
) {
905 "%s: Specified prefix hdr->length %d is too large for a v4 address",
906 __PRETTY_FUNCTION__
, p
.prefixlen
);
909 STREAM_GET(&p
.u
.prefix4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
910 l
+= IPV4_MAX_BYTELEN
;
911 } else if (p
.family
== AF_INET6
) {
912 if (p
.prefixlen
> IPV6_MAX_BITLEN
) {
914 "%s: Specified prefix hdr->length %d is to large for a v6 address",
915 __PRETTY_FUNCTION__
, p
.prefixlen
);
918 STREAM_GET(&p
.u
.prefix6
, s
, IPV6_MAX_BYTELEN
);
919 l
+= IPV6_MAX_BYTELEN
;
922 "rnh_register: Received unknown family type %d\n",
926 rnh
= zebra_add_rnh(&p
, zvrf_id(zvrf
), type
);
927 if (type
== RNH_NEXTHOP_TYPE
) {
929 && !CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
))
930 SET_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
);
932 && CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
))
933 UNSET_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
);
934 } else if (type
== RNH_IMPORT_CHECK_TYPE
) {
936 && !CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
))
937 SET_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
);
939 && CHECK_FLAG(rnh
->flags
,
940 ZEBRA_NHT_EXACT_MATCH
))
941 UNSET_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
);
944 zebra_add_rnh_client(rnh
, client
, type
, zvrf_id(zvrf
));
945 /* Anything not AF_INET/INET6 has been filtered out above */
946 zebra_evaluate_rnh(zvrf_id(zvrf
), p
.family
, 1, type
, &p
);
953 /* Nexthop register */
954 static void zread_rnh_unregister(ZAPI_HANDLER_ARGS
)
960 uint16_t type
= cmd2type
[hdr
->command
];
962 if (IS_ZEBRA_DEBUG_NHT
)
964 "rnh_unregister msg from client %s: hdr->length=%d\n",
965 zebra_route_string(client
->proto
), hdr
->length
);
969 while (l
< hdr
->length
) {
972 STREAM_GETC(s
, flags
);
976 STREAM_GETW(s
, p
.family
);
977 STREAM_GETC(s
, p
.prefixlen
);
979 if (p
.family
== AF_INET
) {
980 if (p
.prefixlen
> IPV4_MAX_BITLEN
) {
982 "%s: Specified prefix hdr->length %d is to large for a v4 address",
983 __PRETTY_FUNCTION__
, p
.prefixlen
);
986 STREAM_GET(&p
.u
.prefix4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
987 l
+= IPV4_MAX_BYTELEN
;
988 } else if (p
.family
== AF_INET6
) {
989 if (p
.prefixlen
> IPV6_MAX_BITLEN
) {
991 "%s: Specified prefix hdr->length %d is to large for a v6 address",
992 __PRETTY_FUNCTION__
, p
.prefixlen
);
995 STREAM_GET(&p
.u
.prefix6
, s
, IPV6_MAX_BYTELEN
);
996 l
+= IPV6_MAX_BYTELEN
;
999 "rnh_register: Received unknown family type %d\n",
1003 rnh
= zebra_lookup_rnh(&p
, zvrf_id(zvrf
), type
);
1005 client
->nh_dereg_time
= monotime(NULL
);
1006 zebra_remove_rnh_client(rnh
, client
, type
);
1013 #define ZEBRA_MIN_FEC_LENGTH 5
1016 static void zread_fec_register(ZAPI_HANDLER_ARGS
)
1022 uint32_t label_index
= MPLS_INVALID_LABEL_INDEX
;
1025 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1027 return; // unexpected
1030 * The minimum amount of data that can be sent for one fec
1033 if (hdr
->length
< ZEBRA_MIN_FEC_LENGTH
) {
1035 "fec_register: Received a fec register of hdr->length %d, it is of insufficient size to properly decode",
1040 while (l
< hdr
->length
) {
1041 STREAM_GETW(s
, flags
);
1042 memset(&p
, 0, sizeof(p
));
1043 STREAM_GETW(s
, p
.family
);
1044 if (p
.family
!= AF_INET
&& p
.family
!= AF_INET6
) {
1046 "fec_register: Received unknown family type %d\n",
1050 STREAM_GETC(s
, p
.prefixlen
);
1051 if ((p
.family
== AF_INET
&& p
.prefixlen
> IPV4_MAX_BITLEN
)
1052 || (p
.family
== AF_INET6
1053 && p
.prefixlen
> IPV6_MAX_BITLEN
)) {
1055 "%s: Specified prefix hdr->length: %d is to long for %d",
1056 __PRETTY_FUNCTION__
, p
.prefixlen
, p
.family
);
1060 STREAM_GET(&p
.u
.prefix
, s
, PSIZE(p
.prefixlen
));
1061 l
+= PSIZE(p
.prefixlen
);
1062 if (flags
& ZEBRA_FEC_REGISTER_LABEL_INDEX
) {
1063 STREAM_GETL(s
, label_index
);
1066 label_index
= MPLS_INVALID_LABEL_INDEX
;
1067 zebra_mpls_fec_register(zvrf
, &p
, label_index
, client
);
1074 /* FEC unregister */
1075 static void zread_fec_unregister(ZAPI_HANDLER_ARGS
)
1083 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1085 return; // unexpected
1088 * The minimum amount of data that can be sent for one
1089 * fec unregistration
1091 if (hdr
->length
< ZEBRA_MIN_FEC_LENGTH
) {
1093 "fec_unregister: Received a fec unregister of hdr->length %d, it is of insufficient size to properly decode",
1098 while (l
< hdr
->length
) {
1099 STREAM_GETW(s
, flags
);
1101 goto stream_failure
;
1103 memset(&p
, 0, sizeof(p
));
1104 STREAM_GETW(s
, p
.family
);
1105 if (p
.family
!= AF_INET
&& p
.family
!= AF_INET6
) {
1107 "fec_unregister: Received unknown family type %d\n",
1111 STREAM_GETC(s
, p
.prefixlen
);
1112 if ((p
.family
== AF_INET
&& p
.prefixlen
> IPV4_MAX_BITLEN
)
1113 || (p
.family
== AF_INET6
1114 && p
.prefixlen
> IPV6_MAX_BITLEN
)) {
1116 "%s: Received prefix hdr->length %d which is greater than %d can support",
1117 __PRETTY_FUNCTION__
, p
.prefixlen
, p
.family
);
1121 STREAM_GET(&p
.u
.prefix
, s
, PSIZE(p
.prefixlen
));
1122 l
+= PSIZE(p
.prefixlen
);
1123 zebra_mpls_fec_unregister(zvrf
, &p
, client
);
1132 * Register zebra server interface information.
1133 * Send current all interface and address information.
1135 static void zread_interface_add(ZAPI_HANDLER_ARGS
)
1138 struct interface
*ifp
;
1140 /* Interface information is needed. */
1141 vrf_bitmap_set(client
->ifinfo
, zvrf_id(zvrf
));
1143 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
1144 FOR_ALL_INTERFACES (vrf
, ifp
) {
1145 /* Skip pseudo interface. */
1146 if (!CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_ACTIVE
))
1149 zsend_interface_add(client
, ifp
);
1150 zsend_interface_addresses(client
, ifp
);
1155 /* Unregister zebra server interface information. */
1156 static void zread_interface_delete(ZAPI_HANDLER_ARGS
)
1158 vrf_bitmap_unset(client
->ifinfo
, zvrf_id(zvrf
));
1161 void zserv_nexthop_num_warn(const char *caller
, const struct prefix
*p
,
1162 const unsigned int nexthop_num
)
1164 if (nexthop_num
> multipath_num
) {
1165 char buff
[PREFIX2STR_BUFFER
];
1166 prefix2str(p
, buff
, sizeof(buff
));
1168 "%s: Prefix %s has %d nexthops, but we can only use the first %d",
1169 caller
, buff
, nexthop_num
, multipath_num
);
1173 static void zread_route_add(ZAPI_HANDLER_ARGS
)
1176 struct zapi_route api
;
1177 struct zapi_nexthop
*api_nh
;
1179 struct prefix_ipv6
*src_p
= NULL
;
1180 struct route_entry
*re
;
1181 struct nexthop
*nexthop
= NULL
;
1183 vrf_id_t vrf_id
= 0;
1184 struct ipaddr vtep_ip
;
1187 zapi_route_decode(s
, &api
);
1189 if (IS_ZEBRA_DEBUG_RECV
) {
1190 char buf_prefix
[PREFIX_STRLEN
];
1191 prefix2str(&api
.prefix
, buf_prefix
, sizeof(buf_prefix
));
1192 zlog_debug("%s: p=%s, ZAPI_MESSAGE_LABEL: %sset, flags=0x%x",
1193 __func__
, buf_prefix
,
1194 (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_LABEL
) ? ""
1199 /* Allocate new route. */
1200 vrf_id
= zvrf_id(zvrf
);
1201 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
1202 re
->type
= api
.type
;
1203 re
->instance
= api
.instance
;
1204 re
->flags
= api
.flags
;
1205 re
->uptime
= time(NULL
);
1206 re
->vrf_id
= vrf_id
;
1207 if (api
.tableid
&& vrf_id
== VRF_DEFAULT
)
1208 re
->table
= api
.tableid
;
1210 re
->table
= zvrf
->table_id
;
1213 * TBD should _all_ of the nexthop add operations use
1214 * api_nh->vrf_id instead of re->vrf_id ? I only changed
1215 * for cases NEXTHOP_TYPE_IPV4 and NEXTHOP_TYPE_IPV6.
1217 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
)) {
1218 for (i
= 0; i
< api
.nexthop_num
; i
++) {
1219 api_nh
= &api
.nexthops
[i
];
1220 ifindex_t ifindex
= 0;
1222 if (IS_ZEBRA_DEBUG_RECV
) {
1223 zlog_debug("nh type %d", api_nh
->type
);
1226 switch (api_nh
->type
) {
1227 case NEXTHOP_TYPE_IFINDEX
:
1228 nexthop
= route_entry_nexthop_ifindex_add(
1229 re
, api_nh
->ifindex
, api_nh
->vrf_id
);
1231 case NEXTHOP_TYPE_IPV4
:
1232 if (IS_ZEBRA_DEBUG_RECV
) {
1233 char nhbuf
[INET6_ADDRSTRLEN
] = {0};
1234 inet_ntop(AF_INET
, &api_nh
->gate
.ipv4
,
1235 nhbuf
, INET6_ADDRSTRLEN
);
1236 zlog_debug("%s: nh=%s, vrf_id=%d",
1240 nexthop
= route_entry_nexthop_ipv4_add(
1241 re
, &api_nh
->gate
.ipv4
, NULL
,
1244 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1246 memset(&vtep_ip
, 0, sizeof(struct ipaddr
));
1247 if (CHECK_FLAG(api
.flags
,
1248 ZEBRA_FLAG_EVPN_ROUTE
)) {
1249 ifindex
= get_l3vni_svi_ifindex(vrf_id
);
1251 ifindex
= api_nh
->ifindex
;
1254 if (IS_ZEBRA_DEBUG_RECV
) {
1255 char nhbuf
[INET6_ADDRSTRLEN
] = {0};
1256 inet_ntop(AF_INET
, &api_nh
->gate
.ipv4
,
1257 nhbuf
, INET6_ADDRSTRLEN
);
1259 "%s: nh=%s, vrf_id=%d (re->vrf_id=%d), ifindex=%d",
1260 __func__
, nhbuf
, api_nh
->vrf_id
,
1261 re
->vrf_id
, ifindex
);
1263 nexthop
= route_entry_nexthop_ipv4_ifindex_add(
1264 re
, &api_nh
->gate
.ipv4
, NULL
, ifindex
,
1267 /* if this an EVPN route entry,
1268 * program the nh as neigh
1270 if (CHECK_FLAG(api
.flags
,
1271 ZEBRA_FLAG_EVPN_ROUTE
)) {
1272 SET_FLAG(nexthop
->flags
,
1273 NEXTHOP_FLAG_EVPN_RVTEP
);
1274 vtep_ip
.ipa_type
= IPADDR_V4
;
1275 memcpy(&(vtep_ip
.ipaddr_v4
),
1276 &(api_nh
->gate
.ipv4
),
1277 sizeof(struct in_addr
));
1278 zebra_vxlan_evpn_vrf_route_add(
1279 vrf_id
, &api
.rmac
, &vtep_ip
,
1283 case NEXTHOP_TYPE_IPV6
:
1284 nexthop
= route_entry_nexthop_ipv6_add(
1285 re
, &api_nh
->gate
.ipv6
, api_nh
->vrf_id
);
1287 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1288 memset(&vtep_ip
, 0, sizeof(struct ipaddr
));
1289 if (CHECK_FLAG(api
.flags
,
1290 ZEBRA_FLAG_EVPN_ROUTE
)) {
1292 get_l3vni_svi_ifindex(vrf_id
);
1294 ifindex
= api_nh
->ifindex
;
1297 nexthop
= route_entry_nexthop_ipv6_ifindex_add(
1298 re
, &api_nh
->gate
.ipv6
, ifindex
,
1301 /* if this an EVPN route entry,
1302 * program the nh as neigh
1304 if (CHECK_FLAG(api
.flags
,
1305 ZEBRA_FLAG_EVPN_ROUTE
)) {
1306 SET_FLAG(nexthop
->flags
,
1307 NEXTHOP_FLAG_EVPN_RVTEP
);
1308 vtep_ip
.ipa_type
= IPADDR_V6
;
1309 memcpy(&vtep_ip
.ipaddr_v6
,
1310 &(api_nh
->gate
.ipv6
),
1311 sizeof(struct in6_addr
));
1312 zebra_vxlan_evpn_vrf_route_add(
1319 case NEXTHOP_TYPE_BLACKHOLE
:
1320 nexthop
= route_entry_nexthop_blackhole_add(
1321 re
, api_nh
->bh_type
);
1327 "%s: Nexthops Specified: %d but we failed to properly create one",
1328 __PRETTY_FUNCTION__
, api
.nexthop_num
);
1329 nexthops_free(re
->ng
.nexthop
);
1330 XFREE(MTYPE_RE
, re
);
1333 /* MPLS labels for BGP-LU or Segment Routing */
1334 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_LABEL
)
1335 && api_nh
->type
!= NEXTHOP_TYPE_IFINDEX
1336 && api_nh
->type
!= NEXTHOP_TYPE_BLACKHOLE
) {
1337 enum lsp_types_t label_type
;
1340 lsp_type_from_re_type(client
->proto
);
1342 if (IS_ZEBRA_DEBUG_RECV
) {
1344 "%s: adding %d labels of type %d (1st=%u)",
1345 __func__
, api_nh
->label_num
,
1346 label_type
, api_nh
->labels
[0]);
1349 nexthop_add_labels(nexthop
, label_type
,
1351 &api_nh
->labels
[0]);
1356 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
))
1357 re
->distance
= api
.distance
;
1358 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
))
1359 re
->metric
= api
.metric
;
1360 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_TAG
))
1362 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_MTU
))
1365 afi
= family2afi(api
.prefix
.family
);
1366 if (afi
!= AFI_IP6
&& CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
)) {
1367 zlog_warn("%s: Received SRC Prefix but afi is not v6",
1368 __PRETTY_FUNCTION__
);
1369 nexthops_free(re
->ng
.nexthop
);
1370 XFREE(MTYPE_RE
, re
);
1373 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
))
1374 src_p
= &api
.src_prefix
;
1376 ret
= rib_add_multipath(afi
, api
.safi
, &api
.prefix
, src_p
, re
);
1379 switch (api
.prefix
.family
) {
1382 client
->v4_route_add_cnt
++;
1384 client
->v4_route_upd8_cnt
++;
1388 client
->v6_route_add_cnt
++;
1390 client
->v6_route_upd8_cnt
++;
1395 static void zread_route_del(ZAPI_HANDLER_ARGS
)
1398 struct zapi_route api
;
1400 struct prefix_ipv6
*src_p
= NULL
;
1403 if (zapi_route_decode(s
, &api
) < 0)
1406 afi
= family2afi(api
.prefix
.family
);
1407 if (afi
!= AFI_IP6
&& CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
)) {
1408 zlog_warn("%s: Received a src prefix while afi is not v6",
1409 __PRETTY_FUNCTION__
);
1412 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
))
1413 src_p
= &api
.src_prefix
;
1415 rib_delete(afi
, api
.safi
, zvrf_id(zvrf
), api
.type
, api
.instance
,
1416 api
.flags
, &api
.prefix
, src_p
, NULL
, zvrf
->table_id
,
1417 api
.metric
, false, &api
.rmac
);
1420 switch (api
.prefix
.family
) {
1422 client
->v4_route_del_cnt
++;
1425 client
->v6_route_del_cnt
++;
1430 /* This function support multiple nexthop. */
1432 * Parse the ZEBRA_IPV4_ROUTE_ADD sent from client. Update re and
1435 static void zread_ipv4_add(ZAPI_HANDLER_ARGS
)
1438 struct route_entry
*re
;
1441 struct in_addr nhop_addr
;
1443 u_char nexthop_type
;
1448 enum lsp_types_t label_type
= ZEBRA_LSP_NONE
;
1450 struct nexthop
*nexthop
;
1451 enum blackhole_type bh_type
= BLACKHOLE_NULL
;
1453 /* Get input stream. */
1456 /* Allocate new re. */
1457 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
1459 /* Type, flags, message. */
1460 STREAM_GETC(s
, re
->type
);
1461 if (re
->type
> ZEBRA_ROUTE_MAX
) {
1462 zlog_warn("%s: Specified route type %d is not a legal value\n",
1463 __PRETTY_FUNCTION__
, re
->type
);
1464 XFREE(MTYPE_RE
, re
);
1467 STREAM_GETW(s
, re
->instance
);
1468 STREAM_GETL(s
, re
->flags
);
1469 STREAM_GETC(s
, message
);
1470 STREAM_GETW(s
, safi
);
1471 re
->uptime
= time(NULL
);
1474 memset(&p
, 0, sizeof(struct prefix_ipv4
));
1476 STREAM_GETC(s
, p
.prefixlen
);
1477 if (p
.prefixlen
> IPV4_MAX_BITLEN
) {
1479 "%s: Specified prefix length %d is greater than what v4 can be",
1480 __PRETTY_FUNCTION__
, p
.prefixlen
);
1481 XFREE(MTYPE_RE
, re
);
1484 STREAM_GET(&p
.u
.prefix4
, s
, PSIZE(p
.prefixlen
));
1487 re
->vrf_id
= zvrf_id(zvrf
);
1489 /* Nexthop parse. */
1490 if (CHECK_FLAG(message
, ZAPI_MESSAGE_NEXTHOP
)) {
1491 STREAM_GETC(s
, nexthop_num
);
1492 zserv_nexthop_num_warn(__func__
, (const struct prefix
*)&p
,
1495 if (CHECK_FLAG(message
, ZAPI_MESSAGE_LABEL
))
1496 label_type
= lsp_type_from_re_type(client
->proto
);
1498 for (i
= 0; i
< nexthop_num
; i
++) {
1499 STREAM_GETC(s
, nexthop_type
);
1501 switch (nexthop_type
) {
1502 case NEXTHOP_TYPE_IFINDEX
:
1503 STREAM_GETL(s
, ifindex
);
1504 route_entry_nexthop_ifindex_add(re
, ifindex
,
1507 case NEXTHOP_TYPE_IPV4
:
1508 STREAM_GET(&nhop_addr
.s_addr
, s
,
1510 nexthop
= route_entry_nexthop_ipv4_add(
1511 re
, &nhop_addr
, NULL
, re
->vrf_id
);
1512 /* For labeled-unicast, each nexthop is followed
1514 if (CHECK_FLAG(message
, ZAPI_MESSAGE_LABEL
)) {
1515 STREAM_GETL(s
, label
);
1516 nexthop_add_labels(nexthop
, label_type
,
1520 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1521 STREAM_GET(&nhop_addr
.s_addr
, s
,
1523 STREAM_GETL(s
, ifindex
);
1524 route_entry_nexthop_ipv4_ifindex_add(
1525 re
, &nhop_addr
, NULL
, ifindex
,
1528 case NEXTHOP_TYPE_IPV6
:
1530 "%s: Please use ZEBRA_ROUTE_ADD if you want to pass v6 nexthops",
1531 __PRETTY_FUNCTION__
);
1532 nexthops_free(re
->ng
.nexthop
);
1533 XFREE(MTYPE_RE
, re
);
1536 case NEXTHOP_TYPE_BLACKHOLE
:
1537 route_entry_nexthop_blackhole_add(re
, bh_type
);
1541 "%s: Specified nexthop type: %d does not exist",
1542 __PRETTY_FUNCTION__
, nexthop_type
);
1543 nexthops_free(re
->ng
.nexthop
);
1544 XFREE(MTYPE_RE
, re
);
1551 if (CHECK_FLAG(message
, ZAPI_MESSAGE_DISTANCE
))
1552 STREAM_GETC(s
, re
->distance
);
1555 if (CHECK_FLAG(message
, ZAPI_MESSAGE_METRIC
))
1556 STREAM_GETL(s
, re
->metric
);
1559 if (CHECK_FLAG(message
, ZAPI_MESSAGE_TAG
))
1560 STREAM_GETL(s
, re
->tag
);
1564 if (CHECK_FLAG(message
, ZAPI_MESSAGE_MTU
))
1565 STREAM_GETL(s
, re
->mtu
);
1570 re
->table
= zvrf
->table_id
;
1572 ret
= rib_add_multipath(AFI_IP
, safi
, &p
, NULL
, re
);
1576 client
->v4_route_add_cnt
++;
1578 client
->v4_route_upd8_cnt
++;
1583 nexthops_free(re
->ng
.nexthop
);
1584 XFREE(MTYPE_RE
, re
);
1587 /* Zebra server IPv4 prefix delete function. */
1588 static void zread_ipv4_delete(ZAPI_HANDLER_ARGS
)
1591 struct zapi_ipv4 api
;
1597 /* Type, flags, message. */
1598 STREAM_GETC(s
, api
.type
);
1599 STREAM_GETW(s
, api
.instance
);
1600 STREAM_GETL(s
, api
.flags
);
1601 STREAM_GETC(s
, api
.message
);
1602 STREAM_GETW(s
, api
.safi
);
1605 memset(&p
, 0, sizeof(struct prefix
));
1607 STREAM_GETC(s
, p
.prefixlen
);
1608 if (p
.prefixlen
> IPV4_MAX_BITLEN
) {
1609 zlog_warn("%s: Passed in prefixlen %d is impossible",
1610 __PRETTY_FUNCTION__
, p
.prefixlen
);
1613 STREAM_GET(&p
.u
.prefix4
, s
, PSIZE(p
.prefixlen
));
1615 table_id
= zvrf
->table_id
;
1617 rib_delete(AFI_IP
, api
.safi
, zvrf_id(zvrf
), api
.type
, api
.instance
,
1618 api
.flags
, &p
, NULL
, NULL
, table_id
, 0, false, NULL
);
1619 client
->v4_route_del_cnt
++;
1625 /* MRIB Nexthop lookup for IPv4. */
1626 static void zread_ipv4_nexthop_lookup_mrib(ZAPI_HANDLER_ARGS
)
1628 struct in_addr addr
;
1629 struct route_entry
*re
;
1631 STREAM_GET(&addr
.s_addr
, msg
, IPV4_MAX_BYTELEN
);
1632 re
= rib_match_ipv4_multicast(zvrf_id(zvrf
), addr
, NULL
);
1633 zsend_ipv4_nexthop_lookup_mrib(client
, addr
, re
, zvrf
);
1639 /* Zebra server IPv6 prefix add function. */
1640 static void zread_ipv4_route_ipv6_nexthop_add(ZAPI_HANDLER_ARGS
)
1644 struct in6_addr nhop_addr
;
1645 struct route_entry
*re
;
1648 u_char nexthop_type
;
1651 static struct in6_addr nexthops
[MULTIPATH_NUM
];
1652 static unsigned int ifindices
[MULTIPATH_NUM
];
1654 static mpls_label_t labels
[MULTIPATH_NUM
];
1655 enum lsp_types_t label_type
= ZEBRA_LSP_NONE
;
1657 struct nexthop
*nexthop
;
1658 enum blackhole_type bh_type
= BLACKHOLE_NULL
;
1660 /* Get input stream. */
1663 memset(&nhop_addr
, 0, sizeof(struct in6_addr
));
1665 /* Allocate new re. */
1666 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
1668 /* Type, flags, message. */
1669 STREAM_GETC(s
, re
->type
);
1670 if (re
->type
> ZEBRA_ROUTE_MAX
) {
1671 zlog_warn("%s: Specified route type: %d is not a legal value\n",
1672 __PRETTY_FUNCTION__
, re
->type
);
1673 XFREE(MTYPE_RE
, re
);
1676 STREAM_GETW(s
, re
->instance
);
1677 STREAM_GETL(s
, re
->flags
);
1678 STREAM_GETC(s
, message
);
1679 STREAM_GETW(s
, safi
);
1680 re
->uptime
= time(NULL
);
1683 memset(&p
, 0, sizeof(struct prefix_ipv4
));
1685 STREAM_GETC(s
, p
.prefixlen
);
1686 if (p
.prefixlen
> IPV4_MAX_BITLEN
) {
1688 "%s: Prefix Length %d is greater than what a v4 address can use",
1689 __PRETTY_FUNCTION__
, p
.prefixlen
);
1690 XFREE(MTYPE_RE
, re
);
1693 STREAM_GET(&p
.u
.prefix4
, s
, PSIZE(p
.prefixlen
));
1696 re
->vrf_id
= zvrf_id(zvrf
);
1698 /* We need to give nh-addr, nh-ifindex with the same next-hop object
1699 * to the re to ensure that IPv6 multipathing works; need to coalesce
1700 * these. Clients should send the same number of paired set of
1701 * next-hop-addr/next-hop-ifindices. */
1702 if (CHECK_FLAG(message
, ZAPI_MESSAGE_NEXTHOP
)) {
1703 unsigned int nh_count
= 0;
1704 unsigned int if_count
= 0;
1705 unsigned int max_nh_if
= 0;
1707 STREAM_GETC(s
, nexthop_num
);
1708 zserv_nexthop_num_warn(__func__
, (const struct prefix
*)&p
,
1711 if (CHECK_FLAG(message
, ZAPI_MESSAGE_LABEL
))
1712 label_type
= lsp_type_from_re_type(client
->proto
);
1714 for (i
= 0; i
< nexthop_num
; i
++) {
1715 STREAM_GETC(s
, nexthop_type
);
1717 switch (nexthop_type
) {
1718 case NEXTHOP_TYPE_IPV6
:
1719 STREAM_GET(&nhop_addr
, s
, 16);
1720 if (nh_count
< MULTIPATH_NUM
) {
1721 /* For labeled-unicast, each nexthop is
1722 * followed by label. */
1723 if (CHECK_FLAG(message
,
1724 ZAPI_MESSAGE_LABEL
)) {
1725 STREAM_GETL(s
, label
);
1726 labels
[nh_count
] = label
;
1728 nexthops
[nh_count
] = nhop_addr
;
1732 case NEXTHOP_TYPE_IFINDEX
:
1733 if (if_count
< multipath_num
) {
1734 STREAM_GETL(s
, ifindices
[if_count
++]);
1737 case NEXTHOP_TYPE_BLACKHOLE
:
1738 route_entry_nexthop_blackhole_add(re
, bh_type
);
1742 "%s: Please use ZEBRA_ROUTE_ADD if you want to pass non v6 nexthops",
1743 __PRETTY_FUNCTION__
);
1744 nexthops_free(re
->ng
.nexthop
);
1745 XFREE(MTYPE_RE
, re
);
1750 max_nh_if
= (nh_count
> if_count
) ? nh_count
: if_count
;
1751 for (i
= 0; i
< max_nh_if
; i
++) {
1753 && !IN6_IS_ADDR_UNSPECIFIED(&nexthops
[i
])) {
1754 if ((i
< if_count
) && ifindices
[i
])
1756 route_entry_nexthop_ipv6_ifindex_add(
1761 nexthop
= route_entry_nexthop_ipv6_add(
1762 re
, &nexthops
[i
], re
->vrf_id
);
1764 if (CHECK_FLAG(message
, ZAPI_MESSAGE_LABEL
))
1765 nexthop_add_labels(nexthop
, label_type
,
1768 if ((i
< if_count
) && ifindices
[i
])
1769 route_entry_nexthop_ifindex_add(
1770 re
, ifindices
[i
], re
->vrf_id
);
1776 if (CHECK_FLAG(message
, ZAPI_MESSAGE_DISTANCE
))
1777 STREAM_GETC(s
, re
->distance
);
1780 if (CHECK_FLAG(message
, ZAPI_MESSAGE_METRIC
))
1781 STREAM_GETL(s
, re
->metric
);
1784 if (CHECK_FLAG(message
, ZAPI_MESSAGE_TAG
))
1785 STREAM_GETL(s
, re
->tag
);
1789 if (CHECK_FLAG(message
, ZAPI_MESSAGE_MTU
))
1790 STREAM_GETL(s
, re
->mtu
);
1795 re
->table
= zvrf
->table_id
;
1797 ret
= rib_add_multipath(AFI_IP6
, safi
, &p
, NULL
, re
);
1800 client
->v4_route_add_cnt
++;
1802 client
->v4_route_upd8_cnt
++;
1807 nexthops_free(re
->ng
.nexthop
);
1808 XFREE(MTYPE_RE
, re
);
1811 static void zread_ipv6_add(ZAPI_HANDLER_ARGS
)
1815 struct in6_addr nhop_addr
;
1817 struct route_entry
*re
;
1820 u_char nexthop_type
;
1822 struct prefix_ipv6 src_p
, *src_pp
;
1824 static struct in6_addr nexthops
[MULTIPATH_NUM
];
1825 static unsigned int ifindices
[MULTIPATH_NUM
];
1827 static mpls_label_t labels
[MULTIPATH_NUM
];
1828 enum lsp_types_t label_type
= ZEBRA_LSP_NONE
;
1830 struct nexthop
*nexthop
;
1831 enum blackhole_type bh_type
= BLACKHOLE_NULL
;
1833 /* Get input stream. */
1836 memset(&nhop_addr
, 0, sizeof(struct in6_addr
));
1838 /* Allocate new re. */
1839 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
1841 /* Type, flags, message. */
1842 STREAM_GETC(s
, re
->type
);
1843 if (re
->type
> ZEBRA_ROUTE_MAX
) {
1844 zlog_warn("%s: Specified route type: %d is not a legal value\n",
1845 __PRETTY_FUNCTION__
, re
->type
);
1846 XFREE(MTYPE_RE
, re
);
1849 STREAM_GETW(s
, re
->instance
);
1850 STREAM_GETL(s
, re
->flags
);
1851 STREAM_GETC(s
, message
);
1852 STREAM_GETW(s
, safi
);
1853 re
->uptime
= time(NULL
);
1856 memset(&p
, 0, sizeof(p
));
1857 p
.family
= AF_INET6
;
1858 STREAM_GETC(s
, p
.prefixlen
);
1859 if (p
.prefixlen
> IPV6_MAX_BITLEN
) {
1861 "%s: Specified prefix length %d is to large for v6 prefix",
1862 __PRETTY_FUNCTION__
, p
.prefixlen
);
1863 XFREE(MTYPE_RE
, re
);
1866 STREAM_GET(&p
.u
.prefix6
, s
, PSIZE(p
.prefixlen
));
1868 if (CHECK_FLAG(message
, ZAPI_MESSAGE_SRCPFX
)) {
1869 memset(&src_p
, 0, sizeof(src_p
));
1870 src_p
.family
= AF_INET6
;
1871 STREAM_GETC(s
, src_p
.prefixlen
);
1872 if (src_p
.prefixlen
> IPV6_MAX_BITLEN
) {
1874 "%s: Specified src prefix length %d is to large for v6 prefix",
1875 __PRETTY_FUNCTION__
, src_p
.prefixlen
);
1876 XFREE(MTYPE_RE
, re
);
1879 STREAM_GET(&src_p
.prefix
, s
, PSIZE(src_p
.prefixlen
));
1885 re
->vrf_id
= zvrf_id(zvrf
);
1887 /* We need to give nh-addr, nh-ifindex with the same next-hop object
1888 * to the re to ensure that IPv6 multipathing works; need to coalesce
1889 * these. Clients should send the same number of paired set of
1890 * next-hop-addr/next-hop-ifindices. */
1891 if (CHECK_FLAG(message
, ZAPI_MESSAGE_NEXTHOP
)) {
1892 unsigned int nh_count
= 0;
1893 unsigned int if_count
= 0;
1894 unsigned int max_nh_if
= 0;
1896 STREAM_GETC(s
, nexthop_num
);
1897 zserv_nexthop_num_warn(__func__
, (const struct prefix
*)&p
,
1900 if (CHECK_FLAG(message
, ZAPI_MESSAGE_LABEL
))
1901 label_type
= lsp_type_from_re_type(client
->proto
);
1903 for (i
= 0; i
< nexthop_num
; i
++) {
1904 STREAM_GETC(s
, nexthop_type
);
1906 switch (nexthop_type
) {
1907 case NEXTHOP_TYPE_IPV6
:
1908 STREAM_GET(&nhop_addr
, s
, 16);
1909 if (nh_count
< MULTIPATH_NUM
) {
1910 /* For labeled-unicast, each nexthop is
1911 * followed by label. */
1912 if (CHECK_FLAG(message
,
1913 ZAPI_MESSAGE_LABEL
)) {
1914 STREAM_GETL(s
, label
);
1915 labels
[nh_count
] = label
;
1917 nexthops
[nh_count
++] = nhop_addr
;
1920 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1921 STREAM_GET(&nhop_addr
, s
, 16);
1922 STREAM_GETL(s
, ifindex
);
1923 route_entry_nexthop_ipv6_ifindex_add(
1924 re
, &nhop_addr
, ifindex
, re
->vrf_id
);
1926 case NEXTHOP_TYPE_IFINDEX
:
1927 if (if_count
< multipath_num
) {
1928 STREAM_GETL(s
, ifindices
[if_count
++]);
1931 case NEXTHOP_TYPE_BLACKHOLE
:
1932 route_entry_nexthop_blackhole_add(re
, bh_type
);
1936 "%s: Please use ZEBRA_ROUTE_ADD if you want to pass non v6 nexthops",
1937 __PRETTY_FUNCTION__
);
1938 nexthops_free(re
->ng
.nexthop
);
1939 XFREE(MTYPE_RE
, re
);
1944 max_nh_if
= (nh_count
> if_count
) ? nh_count
: if_count
;
1945 for (i
= 0; i
< max_nh_if
; i
++) {
1947 && !IN6_IS_ADDR_UNSPECIFIED(&nexthops
[i
])) {
1948 if ((i
< if_count
) && ifindices
[i
])
1950 route_entry_nexthop_ipv6_ifindex_add(
1955 nexthop
= route_entry_nexthop_ipv6_add(
1956 re
, &nexthops
[i
], re
->vrf_id
);
1957 if (CHECK_FLAG(message
, ZAPI_MESSAGE_LABEL
))
1958 nexthop_add_labels(nexthop
, label_type
,
1961 if ((i
< if_count
) && ifindices
[i
])
1962 route_entry_nexthop_ifindex_add(
1963 re
, ifindices
[i
], re
->vrf_id
);
1969 if (CHECK_FLAG(message
, ZAPI_MESSAGE_DISTANCE
))
1970 STREAM_GETC(s
, re
->distance
);
1973 if (CHECK_FLAG(message
, ZAPI_MESSAGE_METRIC
))
1974 STREAM_GETL(s
, re
->metric
);
1977 if (CHECK_FLAG(message
, ZAPI_MESSAGE_TAG
))
1978 STREAM_GETL(s
, re
->tag
);
1982 if (CHECK_FLAG(message
, ZAPI_MESSAGE_MTU
))
1983 STREAM_GETL(s
, re
->mtu
);
1987 re
->table
= zvrf
->table_id
;
1989 ret
= rib_add_multipath(AFI_IP6
, safi
, &p
, src_pp
, re
);
1992 client
->v6_route_add_cnt
++;
1994 client
->v6_route_upd8_cnt
++;
1999 nexthops_free(re
->ng
.nexthop
);
2000 XFREE(MTYPE_RE
, re
);
2003 /* Zebra server IPv6 prefix delete function. */
2004 static void zread_ipv6_delete(ZAPI_HANDLER_ARGS
)
2007 struct zapi_ipv6 api
;
2009 struct prefix_ipv6 src_p
, *src_pp
;
2013 /* Type, flags, message. */
2014 STREAM_GETC(s
, api
.type
);
2015 STREAM_GETW(s
, api
.instance
);
2016 STREAM_GETL(s
, api
.flags
);
2017 STREAM_GETC(s
, api
.message
);
2018 STREAM_GETW(s
, api
.safi
);
2021 memset(&p
, 0, sizeof(struct prefix
));
2022 p
.family
= AF_INET6
;
2023 STREAM_GETC(s
, p
.prefixlen
);
2024 STREAM_GET(&p
.u
.prefix6
, s
, PSIZE(p
.prefixlen
));
2026 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
)) {
2027 memset(&src_p
, 0, sizeof(struct prefix_ipv6
));
2028 src_p
.family
= AF_INET6
;
2029 STREAM_GETC(s
, src_p
.prefixlen
);
2030 STREAM_GET(&src_p
.prefix
, s
, PSIZE(src_p
.prefixlen
));
2035 rib_delete(AFI_IP6
, api
.safi
, zvrf_id(zvrf
), api
.type
, api
.instance
,
2036 api
.flags
, &p
, src_pp
, NULL
, client
->rtm_table
, 0, false,
2039 client
->v6_route_del_cnt
++;
2045 /* Register zebra server router-id information. Send current router-id */
2046 static void zread_router_id_add(ZAPI_HANDLER_ARGS
)
2050 /* Router-id information is needed. */
2051 vrf_bitmap_set(client
->ridinfo
, zvrf_id(zvrf
));
2053 router_id_get(&p
, zvrf_id(zvrf
));
2055 zsend_router_id_update(client
, &p
, zvrf_id(zvrf
));
2058 /* Unregister zebra server router-id information. */
2059 static void zread_router_id_delete(ZAPI_HANDLER_ARGS
)
2061 vrf_bitmap_unset(client
->ridinfo
, zvrf_id(zvrf
));
2064 /* Tie up route-type and client->sock */
2065 static void zread_hello(ZAPI_HANDLER_ARGS
)
2067 /* type of protocol (lib/zebra.h) */
2072 STREAM_GETC(msg
, proto
);
2073 STREAM_GETW(msg
, instance
);
2074 STREAM_GETC(msg
, notify
);
2076 client
->notify_owner
= true;
2078 /* accept only dynamic routing protocols */
2079 if ((proto
< ZEBRA_ROUTE_MAX
) && (proto
> ZEBRA_ROUTE_STATIC
)) {
2081 "client %d says hello and bids fair to announce only %s routes",
2082 client
->sock
, zebra_route_string(proto
));
2084 zlog_notice("client protocol instance %d", instance
);
2086 client
->proto
= proto
;
2087 client
->instance
= instance
;
2094 /* Unregister all information in a VRF. */
2095 static void zread_vrf_unregister(ZAPI_HANDLER_ARGS
)
2100 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
2101 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
2102 vrf_bitmap_unset(client
->redist
[afi
][i
], zvrf_id(zvrf
));
2103 vrf_bitmap_unset(client
->redist_default
, zvrf_id(zvrf
));
2104 vrf_bitmap_unset(client
->ifinfo
, zvrf_id(zvrf
));
2105 vrf_bitmap_unset(client
->ridinfo
, zvrf_id(zvrf
));
2108 static void zread_mpls_labels(ZAPI_HANDLER_ARGS
)
2111 enum lsp_types_t type
;
2112 struct prefix prefix
;
2113 enum nexthop_types_t gtype
;
2116 mpls_label_t in_label
, out_label
;
2119 /* Get input stream. */
2123 STREAM_GETC(s
, type
);
2124 STREAM_GETL(s
, prefix
.family
);
2125 switch (prefix
.family
) {
2127 STREAM_GET(&prefix
.u
.prefix4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
2128 STREAM_GETC(s
, prefix
.prefixlen
);
2129 if (prefix
.prefixlen
> IPV4_MAX_BITLEN
) {
2131 "%s: Specified prefix length %d is greater than a v4 address can support",
2132 __PRETTY_FUNCTION__
, prefix
.prefixlen
);
2135 STREAM_GET(&gate
.ipv4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
2138 STREAM_GET(&prefix
.u
.prefix6
, s
, 16);
2139 STREAM_GETC(s
, prefix
.prefixlen
);
2140 if (prefix
.prefixlen
> IPV6_MAX_BITLEN
) {
2142 "%s: Specified prefix length %d is greater than a v6 address can support",
2143 __PRETTY_FUNCTION__
, prefix
.prefixlen
);
2146 STREAM_GET(&gate
.ipv6
, s
, 16);
2149 zlog_warn("%s: Specified AF %d is not supported for this call",
2150 __PRETTY_FUNCTION__
, prefix
.family
);
2153 STREAM_GETL(s
, ifindex
);
2154 STREAM_GETC(s
, distance
);
2155 STREAM_GETL(s
, in_label
);
2156 STREAM_GETL(s
, out_label
);
2158 switch (prefix
.family
) {
2161 gtype
= NEXTHOP_TYPE_IPV4_IFINDEX
;
2163 gtype
= NEXTHOP_TYPE_IPV4
;
2167 gtype
= NEXTHOP_TYPE_IPV6_IFINDEX
;
2169 gtype
= NEXTHOP_TYPE_IPV6
;
2178 if (hdr
->command
== ZEBRA_MPLS_LABELS_ADD
) {
2179 mpls_lsp_install(zvrf
, type
, in_label
, out_label
, gtype
, &gate
,
2181 mpls_ftn_update(1, zvrf
, type
, &prefix
, gtype
, &gate
, ifindex
,
2182 distance
, out_label
);
2183 } else if (hdr
->command
== ZEBRA_MPLS_LABELS_DELETE
) {
2184 mpls_lsp_uninstall(zvrf
, type
, in_label
, gtype
, &gate
, ifindex
);
2185 mpls_ftn_update(0, zvrf
, type
, &prefix
, gtype
, &gate
, ifindex
,
2186 distance
, out_label
);
2192 static void zread_label_manager_connect(struct zserv
*client
,
2193 struct stream
*msg
, vrf_id_t vrf_id
)
2196 /* type of protocol (lib/zebra.h) */
2200 /* Get input stream. */
2204 STREAM_GETC(s
, proto
);
2205 STREAM_GETW(s
, instance
);
2207 /* accept only dynamic routing protocols */
2208 if ((proto
>= ZEBRA_ROUTE_MAX
) || (proto
<= ZEBRA_ROUTE_STATIC
)) {
2209 zlog_err("client %d has wrong protocol %s", client
->sock
,
2210 zebra_route_string(proto
));
2211 zsend_label_manager_connect_response(client
, vrf_id
, 1);
2214 zlog_notice("client %d with instance %u connected as %s", client
->sock
,
2215 instance
, zebra_route_string(proto
));
2216 client
->proto
= proto
;
2217 client
->instance
= instance
;
2220 Release previous labels of same protocol and instance.
2221 This is done in case it restarted from an unexpected shutdown.
2223 release_daemon_chunks(proto
, instance
);
2226 " Label Manager client connected: sock %d, proto %s, instance %u",
2227 client
->sock
, zebra_route_string(proto
), instance
);
2228 /* send response back */
2229 zsend_label_manager_connect_response(client
, vrf_id
, 0);
2235 static void zread_get_label_chunk(struct zserv
*client
, struct stream
*msg
,
2241 struct label_manager_chunk
*lmc
;
2243 /* Get input stream. */
2247 STREAM_GETC(s
, keep
);
2248 STREAM_GETL(s
, size
);
2250 lmc
= assign_label_chunk(client
->proto
, client
->instance
, keep
, size
);
2252 zlog_err("%s: Unable to assign Label Chunk of size %u",
2255 zlog_debug("Assigned Label Chunk %u - %u to %u", lmc
->start
,
2257 /* send response back */
2258 zsend_assign_label_chunk_response(client
, vrf_id
, lmc
);
2264 static void zread_release_label_chunk(struct zserv
*client
, struct stream
*msg
)
2267 uint32_t start
, end
;
2269 /* Get input stream. */
2273 STREAM_GETL(s
, start
);
2274 STREAM_GETL(s
, end
);
2276 release_label_chunk(client
->proto
, client
->instance
, start
, end
);
2281 static void zread_label_manager_request(ZAPI_HANDLER_ARGS
)
2283 /* to avoid sending other messages like ZERBA_INTERFACE_UP */
2284 if (hdr
->command
== ZEBRA_LABEL_MANAGER_CONNECT
)
2285 client
->is_synchronous
= 1;
2287 /* external label manager */
2289 zread_relay_label_manager_request(hdr
->command
, client
,
2291 /* this is a label manager */
2293 if (hdr
->command
== ZEBRA_LABEL_MANAGER_CONNECT
)
2294 zread_label_manager_connect(client
, msg
, zvrf_id(zvrf
));
2296 /* Sanity: don't allow 'unidentified' requests */
2297 if (!client
->proto
) {
2299 "Got label request from an unidentified client");
2302 if (hdr
->command
== ZEBRA_GET_LABEL_CHUNK
)
2303 zread_get_label_chunk(client
, msg
,
2305 else if (hdr
->command
== ZEBRA_RELEASE_LABEL_CHUNK
)
2306 zread_release_label_chunk(client
, msg
);
2311 static void zread_pseudowire(ZAPI_HANDLER_ARGS
)
2314 char ifname
[IF_NAMESIZE
];
2318 union g_addr nexthop
;
2319 uint32_t local_label
;
2320 uint32_t remote_label
;
2322 union pw_protocol_fields data
;
2324 struct zebra_pw
*pw
;
2326 /* Get input stream. */
2330 STREAM_GET(ifname
, s
, IF_NAMESIZE
);
2331 STREAM_GETL(s
, ifindex
);
2332 STREAM_GETL(s
, type
);
2336 STREAM_GET(&nexthop
.ipv4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
2339 STREAM_GET(&nexthop
.ipv6
, s
, 16);
2344 STREAM_GETL(s
, local_label
);
2345 STREAM_GETL(s
, remote_label
);
2346 STREAM_GETC(s
, flags
);
2347 STREAM_GET(&data
, s
, sizeof(data
));
2348 protocol
= client
->proto
;
2350 pw
= zebra_pw_find(zvrf
, ifname
);
2351 switch (hdr
->command
) {
2354 zlog_warn("%s: pseudowire %s already exists [%s]",
2356 zserv_command_string(hdr
->command
));
2360 zebra_pw_add(zvrf
, ifname
, protocol
, client
);
2362 case ZEBRA_PW_DELETE
:
2364 zlog_warn("%s: pseudowire %s not found [%s]", __func__
,
2365 ifname
, zserv_command_string(hdr
->command
));
2369 zebra_pw_del(zvrf
, pw
);
2372 case ZEBRA_PW_UNSET
:
2374 zlog_warn("%s: pseudowire %s not found [%s]", __func__
,
2375 ifname
, zserv_command_string(hdr
->command
));
2379 switch (hdr
->command
) {
2383 case ZEBRA_PW_UNSET
:
2388 zebra_pw_change(pw
, ifindex
, type
, af
, &nexthop
, local_label
,
2389 remote_label
, flags
, &data
);
2397 /* Cleanup registered nexthops (across VRFs) upon client disconnect. */
2398 static void zebra_client_close_cleanup_rnh(struct zserv
*client
)
2401 struct zebra_vrf
*zvrf
;
2403 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
2404 if ((zvrf
= vrf
->info
) != NULL
) {
2405 zebra_cleanup_rnh_client(zvrf_id(zvrf
), AF_INET
, client
,
2407 zebra_cleanup_rnh_client(zvrf_id(zvrf
), AF_INET6
,
2408 client
, RNH_NEXTHOP_TYPE
);
2409 zebra_cleanup_rnh_client(zvrf_id(zvrf
), AF_INET
, client
,
2410 RNH_IMPORT_CHECK_TYPE
);
2411 zebra_cleanup_rnh_client(zvrf_id(zvrf
), AF_INET6
,
2412 client
, RNH_IMPORT_CHECK_TYPE
);
2413 if (client
->proto
== ZEBRA_ROUTE_LDP
) {
2414 hash_iterate(zvrf
->lsp_table
,
2415 mpls_ldp_lsp_uninstall_all
,
2417 mpls_ldp_ftn_uninstall_all(zvrf
, AFI_IP
);
2418 mpls_ldp_ftn_uninstall_all(zvrf
, AFI_IP6
);
2424 static void zread_interface_set_master(ZAPI_HANDLER_ARGS
)
2426 struct interface
*master
;
2427 struct interface
*slave
;
2428 struct stream
*s
= msg
;
2432 STREAM_GETL(s
, vrf_id
);
2433 STREAM_GETL(s
, ifindex
);
2434 master
= if_lookup_by_index(ifindex
, vrf_id
);
2436 STREAM_GETL(s
, vrf_id
);
2437 STREAM_GETL(s
, ifindex
);
2438 slave
= if_lookup_by_index(ifindex
, vrf_id
);
2440 if (!master
|| !slave
)
2443 kernel_interface_set_master(master
, slave
);
2450 static void zread_vrf_label(ZAPI_HANDLER_ARGS
)
2452 struct interface
*ifp
;
2453 mpls_label_t nlabel
;
2456 struct zebra_vrf
*def_zvrf
;
2457 enum lsp_types_t ltype
;
2460 STREAM_GETL(s
, nlabel
);
2461 STREAM_GETC(s
, afi
);
2462 if (nlabel
== zvrf
->label
[afi
]) {
2464 * Nothing to do here move along
2469 STREAM_GETC(s
, ltype
);
2471 if (zvrf
->vrf
->vrf_id
!= VRF_DEFAULT
)
2472 ifp
= if_lookup_by_name(zvrf
->vrf
->name
, zvrf
->vrf
->vrf_id
);
2474 ifp
= if_lookup_by_name("lo", VRF_DEFAULT
);
2477 zlog_debug("Unable to find specified Interface for %s",
2482 def_zvrf
= zebra_vrf_lookup_by_id(VRF_DEFAULT
);
2484 if (zvrf
->label
[afi
] != MPLS_LABEL_NONE
) {
2488 really_remove
= true;
2489 for (scrubber
= AFI_IP
; scrubber
< AFI_MAX
; scrubber
++) {
2490 if (scrubber
== afi
)
2493 if (zvrf
->label
[scrubber
] == MPLS_LABEL_NONE
)
2496 if (zvrf
->label
[afi
] == zvrf
->label
[scrubber
]) {
2497 really_remove
= false;
2503 mpls_lsp_uninstall(def_zvrf
, ltype
, zvrf
->label
[afi
],
2504 NEXTHOP_TYPE_IFINDEX
, NULL
,
2508 if (nlabel
!= MPLS_LABEL_NONE
)
2509 mpls_lsp_install(def_zvrf
, ltype
, nlabel
,
2510 MPLS_LABEL_IMPLICIT_NULL
, NEXTHOP_TYPE_IFINDEX
,
2511 NULL
, ifp
->ifindex
);
2513 zvrf
->label
[afi
] = nlabel
;
2518 static inline void zread_rule(ZAPI_HANDLER_ARGS
)
2520 struct zebra_pbr_rule zpr
;
2526 STREAM_GETL(s
, total
);
2528 for (i
= 0; i
< total
; i
++) {
2529 memset(&zpr
, 0, sizeof(zpr
));
2531 zpr
.sock
= client
->sock
;
2532 STREAM_GETL(s
, zpr
.seq
);
2533 STREAM_GETL(s
, zpr
.priority
);
2534 STREAM_GETL(s
, zpr
.unique
);
2535 STREAM_GETC(s
, zpr
.filter
.src_ip
.family
);
2536 STREAM_GETC(s
, zpr
.filter
.src_ip
.prefixlen
);
2537 STREAM_GET(&zpr
.filter
.src_ip
.u
.prefix
, s
,
2538 prefix_blen(&zpr
.filter
.src_ip
));
2539 STREAM_GETW(s
, zpr
.filter
.src_port
);
2540 STREAM_GETC(s
, zpr
.filter
.dst_ip
.family
);
2541 STREAM_GETC(s
, zpr
.filter
.dst_ip
.prefixlen
);
2542 STREAM_GET(&zpr
.filter
.dst_ip
.u
.prefix
, s
,
2543 prefix_blen(&zpr
.filter
.dst_ip
));
2544 STREAM_GETW(s
, zpr
.filter
.dst_port
);
2545 STREAM_GETL(s
, zpr
.action
.table
);
2546 STREAM_GETL(s
, ifindex
);
2548 zpr
.ifp
= if_lookup_by_index(ifindex
, VRF_UNKNOWN
);
2550 zlog_debug("FAiled to lookup ifindex: %u", ifindex
);
2554 if (!is_default_prefix(&zpr
.filter
.src_ip
))
2555 zpr
.filter
.filter_bm
|= PBR_FILTER_SRC_IP
;
2557 if (!is_default_prefix(&zpr
.filter
.dst_ip
))
2558 zpr
.filter
.filter_bm
|= PBR_FILTER_DST_IP
;
2560 if (zpr
.filter
.src_port
)
2561 zpr
.filter
.filter_bm
|= PBR_FILTER_SRC_PORT
;
2563 if (zpr
.filter
.dst_port
)
2564 zpr
.filter
.filter_bm
|= PBR_FILTER_DST_PORT
;
2566 if (hdr
->command
== ZEBRA_RULE_ADD
)
2567 zebra_pbr_add_rule(zvrf
->zns
, &zpr
);
2569 zebra_pbr_del_rule(zvrf
->zns
, &zpr
);
2576 void (*zserv_handlers
[])(ZAPI_HANDLER_ARGS
) = {
2577 [ZEBRA_ROUTER_ID_ADD
] = zread_router_id_add
,
2578 [ZEBRA_ROUTER_ID_DELETE
] = zread_router_id_delete
,
2579 [ZEBRA_INTERFACE_ADD
] = zread_interface_add
,
2580 [ZEBRA_INTERFACE_DELETE
] = zread_interface_delete
,
2581 [ZEBRA_ROUTE_ADD
] = zread_route_add
,
2582 [ZEBRA_ROUTE_DELETE
] = zread_route_del
,
2583 [ZEBRA_IPV4_ROUTE_ADD
] = zread_ipv4_add
,
2584 [ZEBRA_IPV4_ROUTE_DELETE
] = zread_ipv4_delete
,
2585 [ZEBRA_IPV4_ROUTE_IPV6_NEXTHOP_ADD
] = zread_ipv4_route_ipv6_nexthop_add
,
2586 [ZEBRA_IPV6_ROUTE_ADD
] = zread_ipv6_add
,
2587 [ZEBRA_IPV6_ROUTE_DELETE
] = zread_ipv6_delete
,
2588 [ZEBRA_REDISTRIBUTE_ADD
] = zebra_redistribute_add
,
2589 [ZEBRA_REDISTRIBUTE_DELETE
] = zebra_redistribute_delete
,
2590 [ZEBRA_REDISTRIBUTE_DEFAULT_ADD
] = zebra_redistribute_default_add
,
2591 [ZEBRA_REDISTRIBUTE_DEFAULT_DELETE
] = zebra_redistribute_default_delete
,
2592 [ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB
] = zread_ipv4_nexthop_lookup_mrib
,
2593 [ZEBRA_HELLO
] = zread_hello
,
2594 [ZEBRA_NEXTHOP_REGISTER
] = zread_rnh_register
,
2595 [ZEBRA_NEXTHOP_UNREGISTER
] = zread_rnh_unregister
,
2596 [ZEBRA_IMPORT_ROUTE_REGISTER
] = zread_rnh_register
,
2597 [ZEBRA_IMPORT_ROUTE_UNREGISTER
] = zread_rnh_unregister
,
2598 [ZEBRA_BFD_DEST_UPDATE
] = zebra_ptm_bfd_dst_register
,
2599 [ZEBRA_BFD_DEST_REGISTER
] = zebra_ptm_bfd_dst_register
,
2600 [ZEBRA_BFD_DEST_DEREGISTER
] = zebra_ptm_bfd_dst_deregister
,
2601 [ZEBRA_VRF_UNREGISTER
] = zread_vrf_unregister
,
2602 [ZEBRA_VRF_LABEL
] = zread_vrf_label
,
2603 [ZEBRA_BFD_CLIENT_REGISTER
] = zebra_ptm_bfd_client_register
,
2604 #if defined(HAVE_RTADV)
2605 [ZEBRA_INTERFACE_ENABLE_RADV
] = zebra_interface_radv_enable
,
2606 [ZEBRA_INTERFACE_DISABLE_RADV
] = zebra_interface_radv_disable
,
2608 [ZEBRA_INTERFACE_ENABLE_RADV
] = NULL
,
2609 [ZEBRA_INTERFACE_DISABLE_RADV
] = NULL
,
2611 [ZEBRA_MPLS_LABELS_ADD
] = zread_mpls_labels
,
2612 [ZEBRA_MPLS_LABELS_DELETE
] = zread_mpls_labels
,
2613 [ZEBRA_IPMR_ROUTE_STATS
] = zebra_ipmr_route_stats
,
2614 [ZEBRA_LABEL_MANAGER_CONNECT
] = zread_label_manager_request
,
2615 [ZEBRA_GET_LABEL_CHUNK
] = zread_label_manager_request
,
2616 [ZEBRA_RELEASE_LABEL_CHUNK
] = zread_label_manager_request
,
2617 [ZEBRA_FEC_REGISTER
] = zread_fec_register
,
2618 [ZEBRA_FEC_UNREGISTER
] = zread_fec_unregister
,
2619 [ZEBRA_ADVERTISE_DEFAULT_GW
] = zebra_vxlan_advertise_gw_macip
,
2620 [ZEBRA_ADVERTISE_SUBNET
] = zebra_vxlan_advertise_subnet
,
2621 [ZEBRA_ADVERTISE_ALL_VNI
] = zebra_vxlan_advertise_all_vni
,
2622 [ZEBRA_REMOTE_VTEP_ADD
] = zebra_vxlan_remote_vtep_add
,
2623 [ZEBRA_REMOTE_VTEP_DEL
] = zebra_vxlan_remote_vtep_del
,
2624 [ZEBRA_REMOTE_MACIP_ADD
] = zebra_vxlan_remote_macip_add
,
2625 [ZEBRA_REMOTE_MACIP_DEL
] = zebra_vxlan_remote_macip_del
,
2626 [ZEBRA_INTERFACE_SET_MASTER
] = zread_interface_set_master
,
2627 [ZEBRA_PW_ADD
] = zread_pseudowire
,
2628 [ZEBRA_PW_DELETE
] = zread_pseudowire
,
2629 [ZEBRA_PW_SET
] = zread_pseudowire
,
2630 [ZEBRA_PW_UNSET
] = zread_pseudowire
,
2631 [ZEBRA_RULE_ADD
] = zread_rule
,
2632 [ZEBRA_RULE_DELETE
] = zread_rule
,
2635 static inline void zserv_handle_commands(struct zserv
*client
,
2636 struct zmsghdr
*hdr
,
2638 struct zebra_vrf
*zvrf
)
2640 if (hdr
->command
> sizeof(zserv_handlers
)
2641 || zserv_handlers
[hdr
->command
] == NULL
)
2642 zlog_info("Zebra received unknown command %d", hdr
->command
);
2644 zserv_handlers
[hdr
->command
](client
, hdr
, msg
, zvrf
);
2649 /* Lifecycle ---------------------------------------------------------------- */
2651 /* free zebra client information. */
2652 static void zebra_client_free(struct zserv
*client
)
2654 /* Send client de-registration to BFD */
2655 zebra_ptm_bfd_client_deregister(client
->proto
);
2657 /* Cleanup any registered nexthops - across all VRFs. */
2658 zebra_client_close_cleanup_rnh(client
);
2660 /* Release Label Manager chunks */
2661 release_daemon_chunks(client
->proto
, client
->instance
);
2663 /* Cleanup any FECs registered by this client. */
2664 zebra_mpls_cleanup_fecs_for_client(vrf_info_lookup(VRF_DEFAULT
),
2667 /* Remove pseudowires associated with this client */
2668 zebra_pw_client_close(client
);
2670 /* Close file descriptor. */
2672 unsigned long nroutes
;
2674 close(client
->sock
);
2675 nroutes
= rib_score_proto(client
->proto
, client
->instance
);
2677 "client %d disconnected. %lu %s routes removed from the rib",
2678 client
->sock
, nroutes
,
2679 zebra_route_string(client
->proto
));
2683 /* Free stream buffers. */
2684 if (client
->ibuf_work
)
2685 stream_free(client
->ibuf_work
);
2686 if (client
->obuf_work
)
2687 stream_free(client
->obuf_work
);
2688 if (client
->ibuf_fifo
)
2689 stream_fifo_free(client
->ibuf_fifo
);
2690 if (client
->obuf_fifo
)
2691 stream_fifo_free(client
->obuf_fifo
);
2693 buffer_free(client
->wb
);
2695 /* Release threads. */
2697 thread_cancel(client
->t_read
);
2698 if (client
->t_write
)
2699 thread_cancel(client
->t_write
);
2700 if (client
->t_suicide
)
2701 thread_cancel(client
->t_suicide
);
2704 for (afi_t afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
2705 for (int i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
2706 vrf_bitmap_free(client
->redist
[afi
][i
]);
2708 vrf_bitmap_free(client
->redist_default
);
2709 vrf_bitmap_free(client
->ifinfo
);
2710 vrf_bitmap_free(client
->ridinfo
);
2712 XFREE(MTYPE_TMP
, client
);
2716 * Called from client thread to terminate itself.
2718 static void zebra_client_close(struct zserv
*client
)
2720 listnode_delete(zebrad
.client_list
, client
);
2721 zebra_client_free(client
);
2724 /* Make new client. */
2725 static void zebra_client_create(int sock
)
2727 struct zserv
*client
;
2731 client
= XCALLOC(MTYPE_TMP
, sizeof(struct zserv
));
2733 /* Make client input/output buffer. */
2734 client
->sock
= sock
;
2735 client
->ibuf_fifo
= stream_fifo_new();
2736 client
->obuf_fifo
= stream_fifo_new();
2737 client
->ibuf_work
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
2738 client
->obuf_work
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
2739 client
->wb
= buffer_new(0);
2741 /* Set table number. */
2742 client
->rtm_table
= zebrad
.rtm_table_default
;
2744 client
->connect_time
= monotime(NULL
);
2745 /* Initialize flags */
2746 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
2747 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
2748 client
->redist
[afi
][i
] = vrf_bitmap_init();
2749 client
->redist_default
= vrf_bitmap_init();
2750 client
->ifinfo
= vrf_bitmap_init();
2751 client
->ridinfo
= vrf_bitmap_init();
2753 /* by default, it's not a synchronous client */
2754 client
->is_synchronous
= 0;
2756 /* Add this client to linked list. */
2757 listnode_add(zebrad
.client_list
, client
);
2759 zebra_vrf_update_all(client
);
2761 /* start read loop */
2762 zebra_event(client
, ZEBRA_READ
);
2765 static int zserv_delayed_close(struct thread
*thread
)
2767 struct zserv
*client
= THREAD_ARG(thread
);
2769 client
->t_suicide
= NULL
;
2770 zebra_client_close(client
);
2775 * Log zapi message to zlog.
2784 * The message header
2786 static void zserv_log_message(const char *errmsg
, struct stream
*msg
,
2787 struct zmsghdr
*hdr
)
2789 zlog_debug("Rx'd ZAPI message");
2791 zlog_debug("%s", errmsg
);
2793 zlog_debug(" Length: %d", hdr
->length
);
2794 zlog_debug("Command: %s", zserv_command_string(hdr
->command
));
2795 zlog_debug(" VRF: %u", hdr
->vrf_id
);
2797 zlog_hexdump(msg
->data
, STREAM_READABLE(msg
));
2800 static int zserv_flush_data(struct thread
*thread
)
2802 struct zserv
*client
= THREAD_ARG(thread
);
2804 client
->t_write
= NULL
;
2805 if (client
->t_suicide
) {
2806 zebra_client_close(client
);
2809 switch (buffer_flush_available(client
->wb
, client
->sock
)) {
2812 "%s: buffer_flush_available failed on zserv client fd %d, closing",
2813 __func__
, client
->sock
);
2814 zebra_client_close(client
);
2817 case BUFFER_PENDING
:
2818 client
->t_write
= NULL
;
2819 thread_add_write(zebrad
.master
, zserv_flush_data
, client
,
2820 client
->sock
, &client
->t_write
);
2827 client
->last_write_time
= monotime(NULL
);
2832 * Write a single packet.
2834 static int zserv_write(struct thread
*thread
)
2836 struct zserv
*client
= THREAD_ARG(thread
);
2840 if (client
->t_suicide
)
2843 if (client
->is_synchronous
)
2846 msg
= stream_fifo_pop(client
->obuf_fifo
);
2847 stream_set_getp(msg
, 0);
2848 client
->last_write_cmd
= stream_getw_from(msg
, 6);
2850 writerv
= buffer_write(client
->wb
, client
->sock
, STREAM_DATA(msg
),
2851 stream_get_endp(msg
));
2858 "%s: buffer_write failed to zserv client fd %d, closing",
2859 __func__
, client
->sock
);
2861 * Schedule a delayed close since many of the functions that
2862 * call this one do not check the return code. They do not
2863 * allow for the possibility that an I/O error may have caused
2864 * the client to be deleted.
2866 client
->t_suicide
= NULL
;
2867 thread_add_event(zebrad
.master
, zserv_delayed_close
, client
, 0,
2868 &client
->t_suicide
);
2871 THREAD_OFF(client
->t_write
);
2873 case BUFFER_PENDING
:
2874 thread_add_write(zebrad
.master
, zserv_flush_data
, client
,
2875 client
->sock
, &client
->t_write
);
2879 if (client
->obuf_fifo
->count
)
2880 zebra_event(client
, ZEBRA_WRITE
);
2882 client
->last_write_time
= monotime(NULL
);
2886 #if defined(HANDLE_ZAPI_FUZZING)
2887 static void zserv_write_incoming(struct stream
*orig
, uint16_t command
)
2889 char fname
[MAXPATHLEN
];
2890 struct stream
*copy
;
2893 copy
= stream_dup(orig
);
2894 stream_set_getp(copy
, 0);
2896 zserv_privs
.change(ZPRIVS_RAISE
);
2897 snprintf(fname
, MAXPATHLEN
, "%s/%u", DAEMON_VTY_DIR
, command
);
2898 fd
= open(fname
, O_CREAT
| O_WRONLY
| O_EXCL
, 0644);
2899 stream_flush(copy
, fd
);
2901 zserv_privs
.change(ZPRIVS_LOWER
);
2906 static int zserv_process_messages(struct thread
*thread
)
2908 struct zserv
*client
= THREAD_ARG(thread
);
2909 struct zebra_vrf
*zvrf
;
2915 msg
= stream_fifo_pop(client
->ibuf_fifo
);
2917 /* break if out of messages */
2921 /* read & check header */
2922 hdrvalid
= zapi_parse_header(msg
, &hdr
);
2923 if (!hdrvalid
&& IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
) {
2924 const char *emsg
= "Message has corrupt header";
2925 zserv_log_message(emsg
, msg
, NULL
);
2931 zvrf
= zebra_vrf_lookup_by_id(hdr
.vrf_id
);
2932 if (!zvrf
&& IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
) {
2933 const char *emsg
= "Message specifies unknown VRF";
2934 zserv_log_message(emsg
, msg
, &hdr
);
2939 /* process commands */
2940 zserv_handle_commands(client
, &hdr
, msg
, zvrf
);
2947 /* Handler of zebra service request. */
2948 static int zserv_read(struct thread
*thread
)
2951 struct zserv
*client
;
2953 #if defined(HANDLE_ZAPI_FUZZING)
2956 int packets
= zebrad
.packets_to_process
;
2958 /* Get thread data. Reset reading thread because I'm running. */
2959 sock
= THREAD_FD(thread
);
2960 client
= THREAD_ARG(thread
);
2962 if (client
->t_suicide
) {
2963 zebra_client_close(client
);
2973 already
= stream_get_endp(client
->ibuf_work
);
2975 /* Read length and command (if we don't have it already). */
2976 if (already
< ZEBRA_HEADER_SIZE
) {
2977 nb
= stream_read_try(client
->ibuf_work
, sock
,
2978 ZEBRA_HEADER_SIZE
- already
);
2979 if ((nb
== 0 || nb
== -1) && IS_ZEBRA_DEBUG_EVENT
)
2980 zlog_debug("connection closed socket [%d]",
2982 if ((nb
== 0 || nb
== -1))
2984 if (nb
!= (ssize_t
)(ZEBRA_HEADER_SIZE
- already
)) {
2985 /* Try again later. */
2988 already
= ZEBRA_HEADER_SIZE
;
2991 /* Reset to read from the beginning of the incoming packet. */
2992 stream_set_getp(client
->ibuf_work
, 0);
2994 /* Fetch header values */
2995 hdrvalid
= zapi_parse_header(client
->ibuf_work
, &hdr
);
2998 snprintf(errmsg
, sizeof(errmsg
),
2999 "%s: Message has corrupt header", __func__
);
3000 zserv_log_message(errmsg
, client
->ibuf_work
, NULL
);
3004 /* Validate header */
3005 if (hdr
.marker
!= ZEBRA_HEADER_MARKER
3006 || hdr
.version
!= ZSERV_VERSION
) {
3008 errmsg
, sizeof(errmsg
),
3009 "Message has corrupt header\n%s: socket %d version mismatch, marker %d, version %d",
3010 __func__
, sock
, hdr
.marker
, hdr
.version
);
3011 zserv_log_message(errmsg
, client
->ibuf_work
, &hdr
);
3014 if (hdr
.length
< ZEBRA_HEADER_SIZE
) {
3016 errmsg
, sizeof(errmsg
),
3017 "Message has corrupt header\n%s: socket %d message length %u is less than header size %d",
3018 __func__
, sock
, hdr
.length
, ZEBRA_HEADER_SIZE
);
3019 zserv_log_message(errmsg
, client
->ibuf_work
, &hdr
);
3022 if (hdr
.length
> STREAM_SIZE(client
->ibuf_work
)) {
3024 errmsg
, sizeof(errmsg
),
3025 "Message has corrupt header\n%s: socket %d message length %u exceeds buffer size %lu",
3026 __func__
, sock
, hdr
.length
,
3027 (unsigned long)STREAM_SIZE(client
->ibuf_work
));
3031 /* Read rest of data. */
3032 if (already
< hdr
.length
) {
3033 nb
= stream_read_try(client
->ibuf_work
, sock
,
3034 hdr
.length
- already
);
3035 if ((nb
== 0 || nb
== -1) && IS_ZEBRA_DEBUG_EVENT
)
3037 "connection closed [%d] when reading zebra data",
3039 if ((nb
== 0 || nb
== -1))
3041 if (nb
!= (ssize_t
)(hdr
.length
- already
)) {
3042 /* Try again later. */
3047 #if defined(HANDLE_ZAPI_FUZZING)
3048 zserv_write_incoming(client
->ibuf_work
, command
);
3050 hdr
.length
-= ZEBRA_HEADER_SIZE
;
3052 /* Debug packet information. */
3053 if (IS_ZEBRA_DEBUG_EVENT
)
3054 zlog_debug("zebra message comes from socket [%d]",
3057 if (IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
)
3058 zserv_log_message(NULL
, client
->ibuf_work
, &hdr
);
3060 client
->last_read_time
= monotime(NULL
);
3061 client
->last_read_cmd
= hdr
.command
;
3063 stream_set_getp(client
->ibuf_work
, 0);
3064 struct stream
*msg
= stream_dup(client
->ibuf_work
);
3066 stream_fifo_push(client
->ibuf_fifo
, msg
);
3068 if (client
->t_suicide
)
3072 stream_reset(client
->ibuf_work
);
3075 if (IS_ZEBRA_DEBUG_PACKET
)
3076 zlog_debug("Read %d packets",
3077 zebrad
.packets_to_process
- packets
);
3079 /* Schedule job to process those packets */
3080 thread_add_event(zebrad
.master
, &zserv_process_messages
, client
, 0,
3083 /* Reschedule ourselves */
3084 zebra_event(client
, ZEBRA_READ
);
3089 zebra_client_close(client
);
3093 static void zebra_event(struct zserv
*client
, enum event event
)
3097 thread_add_read(zebrad
.master
, zserv_read
, client
, client
->sock
,
3101 thread_add_write(zebrad
.master
, zserv_write
, client
,
3102 client
->sock
, &client
->t_write
);
3107 /* Accept code of zebra server socket. */
3108 static int zebra_accept(struct thread
*thread
)
3112 struct sockaddr_in client
;
3115 accept_sock
= THREAD_FD(thread
);
3117 /* Reregister myself. */
3118 thread_add_read(zebrad
.master
, zebra_accept
, NULL
, accept_sock
, NULL
);
3120 len
= sizeof(struct sockaddr_in
);
3121 client_sock
= accept(accept_sock
, (struct sockaddr
*)&client
, &len
);
3123 if (client_sock
< 0) {
3124 zlog_warn("Can't accept zebra socket: %s",
3125 safe_strerror(errno
));
3129 /* Make client socket non-blocking. */
3130 set_nonblocking(client_sock
);
3132 /* Create new zebra client. */
3133 zebra_client_create(client_sock
);
3138 /* Make zebra server socket, wiping any existing one (see bug #403). */
3139 void zebra_zserv_socket_init(char *path
)
3144 struct sockaddr_storage sa
;
3147 if (!frr_zclient_addr(&sa
, &sa_len
, path
))
3148 /* should be caught in zebra main() */
3152 old_mask
= umask(0077);
3154 /* Make UNIX domain socket. */
3155 sock
= socket(sa
.ss_family
, SOCK_STREAM
, 0);
3157 zlog_warn("Can't create zserv socket: %s",
3158 safe_strerror(errno
));
3160 "zebra can't provide full functionality due to above error");
3164 if (sa
.ss_family
!= AF_UNIX
) {
3165 sockopt_reuseaddr(sock
);
3166 sockopt_reuseport(sock
);
3168 struct sockaddr_un
*suna
= (struct sockaddr_un
*)&sa
;
3169 if (suna
->sun_path
[0])
3170 unlink(suna
->sun_path
);
3173 zserv_privs
.change(ZPRIVS_RAISE
);
3174 setsockopt_so_recvbuf(sock
, 1048576);
3175 setsockopt_so_sendbuf(sock
, 1048576);
3176 zserv_privs
.change(ZPRIVS_LOWER
);
3178 if (sa
.ss_family
!= AF_UNIX
&& zserv_privs
.change(ZPRIVS_RAISE
))
3179 zlog_err("Can't raise privileges");
3181 ret
= bind(sock
, (struct sockaddr
*)&sa
, sa_len
);
3183 zlog_warn("Can't bind zserv socket on %s: %s", path
,
3184 safe_strerror(errno
));
3186 "zebra can't provide full functionality due to above error");
3190 if (sa
.ss_family
!= AF_UNIX
&& zserv_privs
.change(ZPRIVS_LOWER
))
3191 zlog_err("Can't lower privileges");
3193 ret
= listen(sock
, 5);
3195 zlog_warn("Can't listen to zserv socket %s: %s", path
,
3196 safe_strerror(errno
));
3198 "zebra can't provide full functionality due to above error");
3205 thread_add_read(zebrad
.master
, zebra_accept
, NULL
, sock
, NULL
);
3208 #define ZEBRA_TIME_BUF 32
3209 static char *zserv_time_buf(time_t *time1
, char *buf
, int buflen
)
3214 assert(buf
!= NULL
);
3215 assert(buflen
>= ZEBRA_TIME_BUF
);
3216 assert(time1
!= NULL
);
3219 snprintf(buf
, buflen
, "never ");
3223 now
= monotime(NULL
);
3227 if (now
< ONE_DAY_SECOND
)
3228 snprintf(buf
, buflen
, "%02d:%02d:%02d", tm
->tm_hour
, tm
->tm_min
,
3230 else if (now
< ONE_WEEK_SECOND
)
3231 snprintf(buf
, buflen
, "%dd%02dh%02dm", tm
->tm_yday
, tm
->tm_hour
,
3234 snprintf(buf
, buflen
, "%02dw%dd%02dh", tm
->tm_yday
/ 7,
3235 tm
->tm_yday
- ((tm
->tm_yday
/ 7) * 7), tm
->tm_hour
);
3239 static void zebra_show_client_detail(struct vty
*vty
, struct zserv
*client
)
3241 char cbuf
[ZEBRA_TIME_BUF
], rbuf
[ZEBRA_TIME_BUF
];
3242 char wbuf
[ZEBRA_TIME_BUF
], nhbuf
[ZEBRA_TIME_BUF
], mbuf
[ZEBRA_TIME_BUF
];
3244 vty_out(vty
, "Client: %s", zebra_route_string(client
->proto
));
3245 if (client
->instance
)
3246 vty_out(vty
, " Instance: %d", client
->instance
);
3249 vty_out(vty
, "------------------------ \n");
3250 vty_out(vty
, "FD: %d \n", client
->sock
);
3251 vty_out(vty
, "Route Table ID: %d \n", client
->rtm_table
);
3253 vty_out(vty
, "Connect Time: %s \n",
3254 zserv_time_buf(&client
->connect_time
, cbuf
, ZEBRA_TIME_BUF
));
3255 if (client
->nh_reg_time
) {
3256 vty_out(vty
, "Nexthop Registry Time: %s \n",
3257 zserv_time_buf(&client
->nh_reg_time
, nhbuf
,
3259 if (client
->nh_last_upd_time
)
3260 vty_out(vty
, "Nexthop Last Update Time: %s \n",
3261 zserv_time_buf(&client
->nh_last_upd_time
, mbuf
,
3264 vty_out(vty
, "No Nexthop Update sent\n");
3266 vty_out(vty
, "Not registered for Nexthop Updates\n");
3268 vty_out(vty
, "Last Msg Rx Time: %s \n",
3269 zserv_time_buf(&client
->last_read_time
, rbuf
, ZEBRA_TIME_BUF
));
3270 vty_out(vty
, "Last Msg Tx Time: %s \n",
3271 zserv_time_buf(&client
->last_write_time
, wbuf
, ZEBRA_TIME_BUF
));
3272 if (client
->last_read_time
)
3273 vty_out(vty
, "Last Rcvd Cmd: %s \n",
3274 zserv_command_string(client
->last_read_cmd
));
3275 if (client
->last_write_time
)
3276 vty_out(vty
, "Last Sent Cmd: %s \n",
3277 zserv_command_string(client
->last_write_cmd
));
3280 vty_out(vty
, "Type Add Update Del \n");
3281 vty_out(vty
, "================================================== \n");
3282 vty_out(vty
, "IPv4 %-12d%-12d%-12d\n", client
->v4_route_add_cnt
,
3283 client
->v4_route_upd8_cnt
, client
->v4_route_del_cnt
);
3284 vty_out(vty
, "IPv6 %-12d%-12d%-12d\n", client
->v6_route_add_cnt
,
3285 client
->v6_route_upd8_cnt
, client
->v6_route_del_cnt
);
3286 vty_out(vty
, "Redist:v4 %-12d%-12d%-12d\n", client
->redist_v4_add_cnt
,
3287 0, client
->redist_v4_del_cnt
);
3288 vty_out(vty
, "Redist:v6 %-12d%-12d%-12d\n", client
->redist_v6_add_cnt
,
3289 0, client
->redist_v6_del_cnt
);
3290 vty_out(vty
, "Connected %-12d%-12d%-12d\n", client
->ifadd_cnt
, 0,
3292 vty_out(vty
, "BFD peer %-12d%-12d%-12d\n", client
->bfd_peer_add_cnt
,
3293 client
->bfd_peer_upd8_cnt
, client
->bfd_peer_del_cnt
);
3294 vty_out(vty
, "Interface Up Notifications: %d\n", client
->ifup_cnt
);
3295 vty_out(vty
, "Interface Down Notifications: %d\n", client
->ifdown_cnt
);
3296 vty_out(vty
, "VNI add notifications: %d\n", client
->vniadd_cnt
);
3297 vty_out(vty
, "VNI delete notifications: %d\n", client
->vnidel_cnt
);
3298 vty_out(vty
, "L3-VNI add notifications: %d\n", client
->l3vniadd_cnt
);
3299 vty_out(vty
, "L3-VNI delete notifications: %d\n", client
->l3vnidel_cnt
);
3300 vty_out(vty
, "MAC-IP add notifications: %d\n", client
->macipadd_cnt
);
3301 vty_out(vty
, "MAC-IP delete notifications: %d\n", client
->macipdel_cnt
);
3307 static void zebra_show_client_brief(struct vty
*vty
, struct zserv
*client
)
3309 char cbuf
[ZEBRA_TIME_BUF
], rbuf
[ZEBRA_TIME_BUF
];
3310 char wbuf
[ZEBRA_TIME_BUF
];
3312 vty_out(vty
, "%-8s%12s %12s%12s%8d/%-8d%8d/%-8d\n",
3313 zebra_route_string(client
->proto
),
3314 zserv_time_buf(&client
->connect_time
, cbuf
, ZEBRA_TIME_BUF
),
3315 zserv_time_buf(&client
->last_read_time
, rbuf
, ZEBRA_TIME_BUF
),
3316 zserv_time_buf(&client
->last_write_time
, wbuf
, ZEBRA_TIME_BUF
),
3317 client
->v4_route_add_cnt
+ client
->v4_route_upd8_cnt
,
3318 client
->v4_route_del_cnt
,
3319 client
->v6_route_add_cnt
+ client
->v6_route_upd8_cnt
,
3320 client
->v6_route_del_cnt
);
3323 struct zserv
*zebra_find_client(u_char proto
, u_short instance
)
3325 struct listnode
*node
, *nnode
;
3326 struct zserv
*client
;
3328 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, node
, nnode
, client
)) {
3329 if (client
->proto
== proto
&& client
->instance
== instance
)
3336 /* This command is for debugging purpose. */
3337 DEFUN (show_zebra_client
,
3338 show_zebra_client_cmd
,
3339 "show zebra client",
3342 "Client information\n")
3344 struct listnode
*node
;
3345 struct zserv
*client
;
3347 for (ALL_LIST_ELEMENTS_RO(zebrad
.client_list
, node
, client
))
3348 zebra_show_client_detail(vty
, client
);
3353 /* This command is for debugging purpose. */
3354 DEFUN (show_zebra_client_summary
,
3355 show_zebra_client_summary_cmd
,
3356 "show zebra client summary",
3359 "Client information brief\n"
3362 struct listnode
*node
;
3363 struct zserv
*client
;
3366 "Name Connect Time Last Read Last Write IPv4 Routes IPv6 Routes \n");
3368 "--------------------------------------------------------------------------------\n");
3370 for (ALL_LIST_ELEMENTS_RO(zebrad
.client_list
, node
, client
))
3371 zebra_show_client_brief(vty
, client
);
3373 vty_out(vty
, "Routes column shows (added+updated)/deleted\n");
3377 #if defined(HANDLE_ZAPI_FUZZING)
3378 void zserv_read_file(char *input
)
3381 struct zserv
*client
= NULL
;
3384 zebra_client_create(-1);
3385 client
= zebrad
.client_list
->head
->data
;
3388 fd
= open(input
, O_RDONLY
| O_NONBLOCK
);
3391 zebra_client_read(&t
);
3397 void zserv_init(void)
3399 /* Client list init. */
3400 zebrad
.client_list
= list_new();
3401 zebrad
.client_list
->del
= (void (*)(void *))zebra_client_free
;
3403 install_element(ENABLE_NODE
, &show_zebra_client_cmd
);
3404 install_element(ENABLE_NODE
, &show_zebra_client_summary_cmd
);