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
+= zsend_write_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",
710 __PRETTY_FUNCTION__
, rule
->unique
);
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
);
1096 * Register zebra server interface information.
1097 * Send current all interface and address information.
1099 static void zread_interface_add(ZAPI_HANDLER_ARGS
)
1102 struct interface
*ifp
;
1104 /* Interface information is needed. */
1105 vrf_bitmap_set(client
->ifinfo
, zvrf_id(zvrf
));
1107 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
1108 FOR_ALL_INTERFACES (vrf
, ifp
) {
1109 /* Skip pseudo interface. */
1110 if (!CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_ACTIVE
))
1113 zsend_interface_add(client
, ifp
);
1114 zsend_interface_addresses(client
, ifp
);
1119 /* Unregister zebra server interface information. */
1120 static void zread_interface_delete(ZAPI_HANDLER_ARGS
)
1122 vrf_bitmap_unset(client
->ifinfo
, zvrf_id(zvrf
));
1125 void zserv_nexthop_num_warn(const char *caller
, const struct prefix
*p
,
1126 const unsigned int nexthop_num
)
1128 if (nexthop_num
> multipath_num
) {
1129 char buff
[PREFIX2STR_BUFFER
];
1130 prefix2str(p
, buff
, sizeof(buff
));
1132 "%s: Prefix %s has %d nexthops, but we can only use the first %d",
1133 caller
, buff
, nexthop_num
, multipath_num
);
1137 static void zread_route_add(ZAPI_HANDLER_ARGS
)
1140 struct zapi_route api
;
1141 struct zapi_nexthop
*api_nh
;
1143 struct prefix_ipv6
*src_p
= NULL
;
1144 struct route_entry
*re
;
1145 struct nexthop
*nexthop
= NULL
;
1147 vrf_id_t vrf_id
= 0;
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 struct ipaddr vtep_ip
;
1184 memset(&vtep_ip
, 0, sizeof(struct ipaddr
));
1185 if (CHECK_FLAG(api
.flags
,
1186 ZEBRA_FLAG_EVPN_ROUTE
)) {
1187 ifindex
= get_l3vni_svi_ifindex(vrf_id
);
1189 ifindex
= api_nh
->ifindex
;
1192 nexthop
= route_entry_nexthop_ipv4_ifindex_add(
1193 re
, &api_nh
->gate
.ipv4
, NULL
, ifindex
,
1196 /* if this an EVPN route entry,
1197 program the nh as neigh
1199 if (CHECK_FLAG(api
.flags
,
1200 ZEBRA_FLAG_EVPN_ROUTE
)) {
1201 SET_FLAG(nexthop
->flags
,
1202 NEXTHOP_FLAG_EVPN_RVTEP
);
1203 vtep_ip
.ipa_type
= IPADDR_V4
;
1204 memcpy(&(vtep_ip
.ipaddr_v4
),
1205 &(api_nh
->gate
.ipv4
),
1206 sizeof(struct in_addr
));
1207 zebra_vxlan_evpn_vrf_route_add(
1208 vrf_id
, &api
.rmac
, &vtep_ip
,
1213 case NEXTHOP_TYPE_IPV6
:
1214 nexthop
= route_entry_nexthop_ipv6_add(
1215 re
, &api_nh
->gate
.ipv6
, api_nh
->vrf_id
);
1217 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1218 nexthop
= route_entry_nexthop_ipv6_ifindex_add(
1219 re
, &api_nh
->gate
.ipv6
, api_nh
->ifindex
,
1222 case NEXTHOP_TYPE_BLACKHOLE
:
1223 nexthop
= route_entry_nexthop_blackhole_add(
1224 re
, api_nh
->bh_type
);
1230 "%s: Nexthops Specified: %d but we failed to properly create one",
1231 __PRETTY_FUNCTION__
, api
.nexthop_num
);
1232 nexthops_free(re
->ng
.nexthop
);
1233 XFREE(MTYPE_RE
, re
);
1236 /* MPLS labels for BGP-LU or Segment Routing */
1237 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_LABEL
)
1238 && api_nh
->type
!= NEXTHOP_TYPE_IFINDEX
1239 && api_nh
->type
!= NEXTHOP_TYPE_BLACKHOLE
) {
1240 enum lsp_types_t label_type
;
1243 lsp_type_from_re_type(client
->proto
);
1244 nexthop_add_labels(nexthop
, label_type
,
1246 &api_nh
->labels
[0]);
1251 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
))
1252 re
->distance
= api
.distance
;
1253 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
))
1254 re
->metric
= api
.metric
;
1255 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_TAG
))
1257 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_MTU
))
1260 afi
= family2afi(api
.prefix
.family
);
1261 if (afi
!= AFI_IP6
&& CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
)) {
1262 zlog_warn("%s: Received SRC Prefix but afi is not v6",
1263 __PRETTY_FUNCTION__
);
1264 nexthops_free(re
->ng
.nexthop
);
1265 XFREE(MTYPE_RE
, re
);
1268 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
))
1269 src_p
= &api
.src_prefix
;
1271 ret
= rib_add_multipath(afi
, api
.safi
, &api
.prefix
, src_p
, re
);
1274 switch (api
.prefix
.family
) {
1277 client
->v4_route_add_cnt
++;
1279 client
->v4_route_upd8_cnt
++;
1283 client
->v6_route_add_cnt
++;
1285 client
->v6_route_upd8_cnt
++;
1290 static void zread_route_del(ZAPI_HANDLER_ARGS
)
1293 struct zapi_route api
;
1295 struct prefix_ipv6
*src_p
= NULL
;
1298 if (zapi_route_decode(s
, &api
) < 0)
1301 afi
= family2afi(api
.prefix
.family
);
1302 if (afi
!= AFI_IP6
&& CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
)) {
1303 zlog_warn("%s: Received a src prefix while afi is not v6",
1304 __PRETTY_FUNCTION__
);
1307 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
))
1308 src_p
= &api
.src_prefix
;
1310 rib_delete(afi
, api
.safi
, zvrf_id(zvrf
), api
.type
, api
.instance
,
1311 api
.flags
, &api
.prefix
, src_p
, NULL
, zvrf
->table_id
,
1312 api
.metric
, false, &api
.rmac
);
1315 switch (api
.prefix
.family
) {
1317 client
->v4_route_del_cnt
++;
1320 client
->v6_route_del_cnt
++;
1325 /* This function support multiple nexthop. */
1327 * Parse the ZEBRA_IPV4_ROUTE_ADD sent from client. Update re and
1330 static void zread_ipv4_add(ZAPI_HANDLER_ARGS
)
1333 struct route_entry
*re
;
1336 struct in_addr nhop_addr
;
1338 u_char nexthop_type
;
1343 enum lsp_types_t label_type
= ZEBRA_LSP_NONE
;
1345 struct nexthop
*nexthop
;
1346 enum blackhole_type bh_type
= BLACKHOLE_NULL
;
1348 /* Get input stream. */
1351 /* Allocate new re. */
1352 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
1354 /* Type, flags, message. */
1355 STREAM_GETC(s
, re
->type
);
1356 if (re
->type
> ZEBRA_ROUTE_MAX
) {
1357 zlog_warn("%s: Specified route type %d is not a legal value\n",
1358 __PRETTY_FUNCTION__
, re
->type
);
1359 XFREE(MTYPE_RE
, re
);
1362 STREAM_GETW(s
, re
->instance
);
1363 STREAM_GETL(s
, re
->flags
);
1364 STREAM_GETC(s
, message
);
1365 STREAM_GETW(s
, safi
);
1366 re
->uptime
= time(NULL
);
1369 memset(&p
, 0, sizeof(struct prefix_ipv4
));
1371 STREAM_GETC(s
, p
.prefixlen
);
1372 if (p
.prefixlen
> IPV4_MAX_BITLEN
) {
1374 "%s: Specified prefix length %d is greater than what v4 can be",
1375 __PRETTY_FUNCTION__
, p
.prefixlen
);
1376 XFREE(MTYPE_RE
, re
);
1379 STREAM_GET(&p
.u
.prefix4
, s
, PSIZE(p
.prefixlen
));
1382 re
->vrf_id
= zvrf_id(zvrf
);
1384 /* Nexthop parse. */
1385 if (CHECK_FLAG(message
, ZAPI_MESSAGE_NEXTHOP
)) {
1386 STREAM_GETC(s
, nexthop_num
);
1387 zserv_nexthop_num_warn(__func__
, (const struct prefix
*)&p
,
1390 if (CHECK_FLAG(message
, ZAPI_MESSAGE_LABEL
))
1391 label_type
= lsp_type_from_re_type(client
->proto
);
1393 for (i
= 0; i
< nexthop_num
; i
++) {
1394 STREAM_GETC(s
, nexthop_type
);
1396 switch (nexthop_type
) {
1397 case NEXTHOP_TYPE_IFINDEX
:
1398 STREAM_GETL(s
, ifindex
);
1399 route_entry_nexthop_ifindex_add(re
, ifindex
,
1402 case NEXTHOP_TYPE_IPV4
:
1403 STREAM_GET(&nhop_addr
.s_addr
, s
,
1405 nexthop
= route_entry_nexthop_ipv4_add(
1406 re
, &nhop_addr
, NULL
, re
->vrf_id
);
1407 /* For labeled-unicast, each nexthop is followed
1409 if (CHECK_FLAG(message
, ZAPI_MESSAGE_LABEL
)) {
1410 STREAM_GETL(s
, label
);
1411 nexthop_add_labels(nexthop
, label_type
,
1415 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1416 STREAM_GET(&nhop_addr
.s_addr
, s
,
1418 STREAM_GETL(s
, ifindex
);
1419 route_entry_nexthop_ipv4_ifindex_add(
1420 re
, &nhop_addr
, NULL
, ifindex
,
1423 case NEXTHOP_TYPE_IPV6
:
1425 "%s: Please use ZEBRA_ROUTE_ADD if you want to pass v6 nexthops",
1426 __PRETTY_FUNCTION__
);
1427 nexthops_free(re
->ng
.nexthop
);
1428 XFREE(MTYPE_RE
, re
);
1431 case NEXTHOP_TYPE_BLACKHOLE
:
1432 route_entry_nexthop_blackhole_add(re
, bh_type
);
1436 "%s: Specified nexthop type: %d does not exist",
1437 __PRETTY_FUNCTION__
, nexthop_type
);
1438 nexthops_free(re
->ng
.nexthop
);
1439 XFREE(MTYPE_RE
, re
);
1446 if (CHECK_FLAG(message
, ZAPI_MESSAGE_DISTANCE
))
1447 STREAM_GETC(s
, re
->distance
);
1450 if (CHECK_FLAG(message
, ZAPI_MESSAGE_METRIC
))
1451 STREAM_GETL(s
, re
->metric
);
1454 if (CHECK_FLAG(message
, ZAPI_MESSAGE_TAG
))
1455 STREAM_GETL(s
, re
->tag
);
1459 if (CHECK_FLAG(message
, ZAPI_MESSAGE_MTU
))
1460 STREAM_GETL(s
, re
->mtu
);
1465 re
->table
= zvrf
->table_id
;
1467 ret
= rib_add_multipath(AFI_IP
, safi
, &p
, NULL
, re
);
1471 client
->v4_route_add_cnt
++;
1473 client
->v4_route_upd8_cnt
++;
1478 nexthops_free(re
->ng
.nexthop
);
1479 XFREE(MTYPE_RE
, re
);
1482 /* Zebra server IPv4 prefix delete function. */
1483 static void zread_ipv4_delete(ZAPI_HANDLER_ARGS
)
1486 struct zapi_ipv4 api
;
1492 /* Type, flags, message. */
1493 STREAM_GETC(s
, api
.type
);
1494 STREAM_GETW(s
, api
.instance
);
1495 STREAM_GETL(s
, api
.flags
);
1496 STREAM_GETC(s
, api
.message
);
1497 STREAM_GETW(s
, api
.safi
);
1500 memset(&p
, 0, sizeof(struct prefix
));
1502 STREAM_GETC(s
, p
.prefixlen
);
1503 if (p
.prefixlen
> IPV4_MAX_BITLEN
) {
1504 zlog_warn("%s: Passed in prefixlen %d is impossible",
1505 __PRETTY_FUNCTION__
, p
.prefixlen
);
1508 STREAM_GET(&p
.u
.prefix4
, s
, PSIZE(p
.prefixlen
));
1510 table_id
= zvrf
->table_id
;
1512 rib_delete(AFI_IP
, api
.safi
, zvrf_id(zvrf
), api
.type
, api
.instance
,
1513 api
.flags
, &p
, NULL
, NULL
, table_id
, 0, false, NULL
);
1514 client
->v4_route_del_cnt
++;
1520 /* MRIB Nexthop lookup for IPv4. */
1521 static void zread_ipv4_nexthop_lookup_mrib(ZAPI_HANDLER_ARGS
)
1523 struct in_addr addr
;
1524 struct route_entry
*re
;
1526 STREAM_GET(&addr
.s_addr
, msg
, IPV4_MAX_BYTELEN
);
1527 re
= rib_match_ipv4_multicast(zvrf_id(zvrf
), addr
, NULL
);
1528 zsend_ipv4_nexthop_lookup_mrib(client
, addr
, re
, zvrf
);
1534 /* Zebra server IPv6 prefix add function. */
1535 static void zread_ipv4_route_ipv6_nexthop_add(ZAPI_HANDLER_ARGS
)
1539 struct in6_addr nhop_addr
;
1540 struct route_entry
*re
;
1543 u_char nexthop_type
;
1546 static struct in6_addr nexthops
[MULTIPATH_NUM
];
1547 static unsigned int ifindices
[MULTIPATH_NUM
];
1549 static mpls_label_t labels
[MULTIPATH_NUM
];
1550 enum lsp_types_t label_type
= ZEBRA_LSP_NONE
;
1552 struct nexthop
*nexthop
;
1553 enum blackhole_type bh_type
= BLACKHOLE_NULL
;
1555 /* Get input stream. */
1558 memset(&nhop_addr
, 0, sizeof(struct in6_addr
));
1560 /* Allocate new re. */
1561 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
1563 /* Type, flags, message. */
1564 STREAM_GETC(s
, re
->type
);
1565 if (re
->type
> ZEBRA_ROUTE_MAX
) {
1566 zlog_warn("%s: Specified route type: %d is not a legal value\n",
1567 __PRETTY_FUNCTION__
, re
->type
);
1568 XFREE(MTYPE_RE
, re
);
1571 STREAM_GETW(s
, re
->instance
);
1572 STREAM_GETL(s
, re
->flags
);
1573 STREAM_GETC(s
, message
);
1574 STREAM_GETW(s
, safi
);
1575 re
->uptime
= time(NULL
);
1578 memset(&p
, 0, sizeof(struct prefix_ipv4
));
1580 STREAM_GETC(s
, p
.prefixlen
);
1581 if (p
.prefixlen
> IPV4_MAX_BITLEN
) {
1583 "%s: Prefix Length %d is greater than what a v4 address can use",
1584 __PRETTY_FUNCTION__
, p
.prefixlen
);
1585 XFREE(MTYPE_RE
, re
);
1588 STREAM_GET(&p
.u
.prefix4
, s
, PSIZE(p
.prefixlen
));
1591 re
->vrf_id
= zvrf_id(zvrf
);
1593 /* We need to give nh-addr, nh-ifindex with the same next-hop object
1594 * to the re to ensure that IPv6 multipathing works; need to coalesce
1595 * these. Clients should send the same number of paired set of
1596 * next-hop-addr/next-hop-ifindices. */
1597 if (CHECK_FLAG(message
, ZAPI_MESSAGE_NEXTHOP
)) {
1598 unsigned int nh_count
= 0;
1599 unsigned int if_count
= 0;
1600 unsigned int max_nh_if
= 0;
1602 STREAM_GETC(s
, nexthop_num
);
1603 zserv_nexthop_num_warn(__func__
, (const struct prefix
*)&p
,
1606 if (CHECK_FLAG(message
, ZAPI_MESSAGE_LABEL
))
1607 label_type
= lsp_type_from_re_type(client
->proto
);
1609 for (i
= 0; i
< nexthop_num
; i
++) {
1610 STREAM_GETC(s
, nexthop_type
);
1612 switch (nexthop_type
) {
1613 case NEXTHOP_TYPE_IPV6
:
1614 STREAM_GET(&nhop_addr
, s
, 16);
1615 if (nh_count
< MULTIPATH_NUM
) {
1616 /* For labeled-unicast, each nexthop is
1617 * followed by label. */
1618 if (CHECK_FLAG(message
,
1619 ZAPI_MESSAGE_LABEL
)) {
1620 STREAM_GETL(s
, label
);
1621 labels
[nh_count
] = label
;
1623 nexthops
[nh_count
] = nhop_addr
;
1627 case NEXTHOP_TYPE_IFINDEX
:
1628 if (if_count
< multipath_num
) {
1629 STREAM_GETL(s
, ifindices
[if_count
++]);
1632 case NEXTHOP_TYPE_BLACKHOLE
:
1633 route_entry_nexthop_blackhole_add(re
, bh_type
);
1637 "%s: Please use ZEBRA_ROUTE_ADD if you want to pass non v6 nexthops",
1638 __PRETTY_FUNCTION__
);
1639 nexthops_free(re
->ng
.nexthop
);
1640 XFREE(MTYPE_RE
, re
);
1645 max_nh_if
= (nh_count
> if_count
) ? nh_count
: if_count
;
1646 for (i
= 0; i
< max_nh_if
; i
++) {
1648 && !IN6_IS_ADDR_UNSPECIFIED(&nexthops
[i
])) {
1649 if ((i
< if_count
) && ifindices
[i
])
1651 route_entry_nexthop_ipv6_ifindex_add(
1656 nexthop
= route_entry_nexthop_ipv6_add(
1657 re
, &nexthops
[i
], re
->vrf_id
);
1659 if (CHECK_FLAG(message
, ZAPI_MESSAGE_LABEL
))
1660 nexthop_add_labels(nexthop
, label_type
,
1663 if ((i
< if_count
) && ifindices
[i
])
1664 route_entry_nexthop_ifindex_add(
1665 re
, ifindices
[i
], re
->vrf_id
);
1671 if (CHECK_FLAG(message
, ZAPI_MESSAGE_DISTANCE
))
1672 STREAM_GETC(s
, re
->distance
);
1675 if (CHECK_FLAG(message
, ZAPI_MESSAGE_METRIC
))
1676 STREAM_GETL(s
, re
->metric
);
1679 if (CHECK_FLAG(message
, ZAPI_MESSAGE_TAG
))
1680 STREAM_GETL(s
, re
->tag
);
1684 if (CHECK_FLAG(message
, ZAPI_MESSAGE_MTU
))
1685 STREAM_GETL(s
, re
->mtu
);
1690 re
->table
= zvrf
->table_id
;
1692 ret
= rib_add_multipath(AFI_IP6
, safi
, &p
, NULL
, re
);
1695 client
->v4_route_add_cnt
++;
1697 client
->v4_route_upd8_cnt
++;
1702 nexthops_free(re
->ng
.nexthop
);
1703 XFREE(MTYPE_RE
, re
);
1706 static void zread_ipv6_add(ZAPI_HANDLER_ARGS
)
1710 struct in6_addr nhop_addr
;
1712 struct route_entry
*re
;
1715 u_char nexthop_type
;
1717 struct prefix_ipv6 src_p
, *src_pp
;
1719 static struct in6_addr nexthops
[MULTIPATH_NUM
];
1720 static unsigned int ifindices
[MULTIPATH_NUM
];
1722 static mpls_label_t labels
[MULTIPATH_NUM
];
1723 enum lsp_types_t label_type
= ZEBRA_LSP_NONE
;
1725 struct nexthop
*nexthop
;
1726 enum blackhole_type bh_type
= BLACKHOLE_NULL
;
1728 /* Get input stream. */
1731 memset(&nhop_addr
, 0, sizeof(struct in6_addr
));
1733 /* Allocate new re. */
1734 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
1736 /* Type, flags, message. */
1737 STREAM_GETC(s
, re
->type
);
1738 if (re
->type
> ZEBRA_ROUTE_MAX
) {
1739 zlog_warn("%s: Specified route type: %d is not a legal value\n",
1740 __PRETTY_FUNCTION__
, re
->type
);
1741 XFREE(MTYPE_RE
, re
);
1744 STREAM_GETW(s
, re
->instance
);
1745 STREAM_GETL(s
, re
->flags
);
1746 STREAM_GETC(s
, message
);
1747 STREAM_GETW(s
, safi
);
1748 re
->uptime
= time(NULL
);
1751 memset(&p
, 0, sizeof(p
));
1752 p
.family
= AF_INET6
;
1753 STREAM_GETC(s
, p
.prefixlen
);
1754 if (p
.prefixlen
> IPV6_MAX_BITLEN
) {
1756 "%s: Specified prefix length %d is to large for v6 prefix",
1757 __PRETTY_FUNCTION__
, p
.prefixlen
);
1758 XFREE(MTYPE_RE
, re
);
1761 STREAM_GET(&p
.u
.prefix6
, s
, PSIZE(p
.prefixlen
));
1763 if (CHECK_FLAG(message
, ZAPI_MESSAGE_SRCPFX
)) {
1764 memset(&src_p
, 0, sizeof(src_p
));
1765 src_p
.family
= AF_INET6
;
1766 STREAM_GETC(s
, src_p
.prefixlen
);
1767 if (src_p
.prefixlen
> IPV6_MAX_BITLEN
) {
1769 "%s: Specified src prefix length %d is to large for v6 prefix",
1770 __PRETTY_FUNCTION__
, src_p
.prefixlen
);
1771 XFREE(MTYPE_RE
, re
);
1774 STREAM_GET(&src_p
.prefix
, s
, PSIZE(src_p
.prefixlen
));
1780 re
->vrf_id
= zvrf_id(zvrf
);
1782 /* We need to give nh-addr, nh-ifindex with the same next-hop object
1783 * to the re to ensure that IPv6 multipathing works; need to coalesce
1784 * these. Clients should send the same number of paired set of
1785 * next-hop-addr/next-hop-ifindices. */
1786 if (CHECK_FLAG(message
, ZAPI_MESSAGE_NEXTHOP
)) {
1787 unsigned int nh_count
= 0;
1788 unsigned int if_count
= 0;
1789 unsigned int max_nh_if
= 0;
1791 STREAM_GETC(s
, nexthop_num
);
1792 zserv_nexthop_num_warn(__func__
, (const struct prefix
*)&p
,
1795 if (CHECK_FLAG(message
, ZAPI_MESSAGE_LABEL
))
1796 label_type
= lsp_type_from_re_type(client
->proto
);
1798 for (i
= 0; i
< nexthop_num
; i
++) {
1799 STREAM_GETC(s
, nexthop_type
);
1801 switch (nexthop_type
) {
1802 case NEXTHOP_TYPE_IPV6
:
1803 STREAM_GET(&nhop_addr
, s
, 16);
1804 if (nh_count
< MULTIPATH_NUM
) {
1805 /* For labeled-unicast, each nexthop is
1806 * followed by label. */
1807 if (CHECK_FLAG(message
,
1808 ZAPI_MESSAGE_LABEL
)) {
1809 STREAM_GETL(s
, label
);
1810 labels
[nh_count
] = label
;
1812 nexthops
[nh_count
++] = nhop_addr
;
1815 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1816 STREAM_GET(&nhop_addr
, s
, 16);
1817 STREAM_GETL(s
, ifindex
);
1818 route_entry_nexthop_ipv6_ifindex_add(
1819 re
, &nhop_addr
, ifindex
, re
->vrf_id
);
1821 case NEXTHOP_TYPE_IFINDEX
:
1822 if (if_count
< multipath_num
) {
1823 STREAM_GETL(s
, ifindices
[if_count
++]);
1826 case NEXTHOP_TYPE_BLACKHOLE
:
1827 route_entry_nexthop_blackhole_add(re
, bh_type
);
1831 "%s: Please use ZEBRA_ROUTE_ADD if you want to pass non v6 nexthops",
1832 __PRETTY_FUNCTION__
);
1833 nexthops_free(re
->ng
.nexthop
);
1834 XFREE(MTYPE_RE
, re
);
1839 max_nh_if
= (nh_count
> if_count
) ? nh_count
: if_count
;
1840 for (i
= 0; i
< max_nh_if
; i
++) {
1842 && !IN6_IS_ADDR_UNSPECIFIED(&nexthops
[i
])) {
1843 if ((i
< if_count
) && ifindices
[i
])
1845 route_entry_nexthop_ipv6_ifindex_add(
1850 nexthop
= route_entry_nexthop_ipv6_add(
1851 re
, &nexthops
[i
], re
->vrf_id
);
1852 if (CHECK_FLAG(message
, ZAPI_MESSAGE_LABEL
))
1853 nexthop_add_labels(nexthop
, label_type
,
1856 if ((i
< if_count
) && ifindices
[i
])
1857 route_entry_nexthop_ifindex_add(
1858 re
, ifindices
[i
], re
->vrf_id
);
1864 if (CHECK_FLAG(message
, ZAPI_MESSAGE_DISTANCE
))
1865 STREAM_GETC(s
, re
->distance
);
1868 if (CHECK_FLAG(message
, ZAPI_MESSAGE_METRIC
))
1869 STREAM_GETL(s
, re
->metric
);
1872 if (CHECK_FLAG(message
, ZAPI_MESSAGE_TAG
))
1873 STREAM_GETL(s
, re
->tag
);
1877 if (CHECK_FLAG(message
, ZAPI_MESSAGE_MTU
))
1878 STREAM_GETL(s
, re
->mtu
);
1882 re
->table
= zvrf
->table_id
;
1884 ret
= rib_add_multipath(AFI_IP6
, safi
, &p
, src_pp
, re
);
1887 client
->v6_route_add_cnt
++;
1889 client
->v6_route_upd8_cnt
++;
1894 nexthops_free(re
->ng
.nexthop
);
1895 XFREE(MTYPE_RE
, re
);
1898 /* Zebra server IPv6 prefix delete function. */
1899 static void zread_ipv6_delete(ZAPI_HANDLER_ARGS
)
1902 struct zapi_ipv6 api
;
1904 struct prefix_ipv6 src_p
, *src_pp
;
1908 /* Type, flags, message. */
1909 STREAM_GETC(s
, api
.type
);
1910 STREAM_GETW(s
, api
.instance
);
1911 STREAM_GETL(s
, api
.flags
);
1912 STREAM_GETC(s
, api
.message
);
1913 STREAM_GETW(s
, api
.safi
);
1916 memset(&p
, 0, sizeof(struct prefix
));
1917 p
.family
= AF_INET6
;
1918 STREAM_GETC(s
, p
.prefixlen
);
1919 STREAM_GET(&p
.u
.prefix6
, s
, PSIZE(p
.prefixlen
));
1921 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
)) {
1922 memset(&src_p
, 0, sizeof(struct prefix_ipv6
));
1923 src_p
.family
= AF_INET6
;
1924 STREAM_GETC(s
, src_p
.prefixlen
);
1925 STREAM_GET(&src_p
.prefix
, s
, PSIZE(src_p
.prefixlen
));
1930 rib_delete(AFI_IP6
, api
.safi
, zvrf_id(zvrf
), api
.type
, api
.instance
,
1931 api
.flags
, &p
, src_pp
, NULL
, client
->rtm_table
, 0, false,
1934 client
->v6_route_del_cnt
++;
1940 /* Register zebra server router-id information. Send current router-id */
1941 static void zread_router_id_add(ZAPI_HANDLER_ARGS
)
1945 /* Router-id information is needed. */
1946 vrf_bitmap_set(client
->ridinfo
, zvrf_id(zvrf
));
1948 router_id_get(&p
, zvrf_id(zvrf
));
1950 zsend_router_id_update(client
, &p
, zvrf_id(zvrf
));
1953 /* Unregister zebra server router-id information. */
1954 static void zread_router_id_delete(ZAPI_HANDLER_ARGS
)
1956 vrf_bitmap_unset(client
->ridinfo
, zvrf_id(zvrf
));
1959 /* Tie up route-type and client->sock */
1960 static void zread_hello(ZAPI_HANDLER_ARGS
)
1962 /* type of protocol (lib/zebra.h) */
1967 STREAM_GETC(msg
, proto
);
1968 STREAM_GETW(msg
, instance
);
1969 STREAM_GETC(msg
, notify
);
1971 client
->notify_owner
= true;
1973 /* accept only dynamic routing protocols */
1974 if ((proto
< ZEBRA_ROUTE_MAX
) && (proto
> ZEBRA_ROUTE_STATIC
)) {
1976 "client %d says hello and bids fair to announce only %s routes",
1977 client
->sock
, zebra_route_string(proto
));
1979 zlog_notice("client protocol instance %d", instance
);
1981 client
->proto
= proto
;
1982 client
->instance
= instance
;
1989 /* Unregister all information in a VRF. */
1990 static void zread_vrf_unregister(ZAPI_HANDLER_ARGS
)
1995 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
1996 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
1997 vrf_bitmap_unset(client
->redist
[afi
][i
], zvrf_id(zvrf
));
1998 vrf_bitmap_unset(client
->redist_default
, zvrf_id(zvrf
));
1999 vrf_bitmap_unset(client
->ifinfo
, zvrf_id(zvrf
));
2000 vrf_bitmap_unset(client
->ridinfo
, zvrf_id(zvrf
));
2003 static void zread_mpls_labels(ZAPI_HANDLER_ARGS
)
2006 enum lsp_types_t type
;
2007 struct prefix prefix
;
2008 enum nexthop_types_t gtype
;
2011 mpls_label_t in_label
, out_label
;
2014 /* Get input stream. */
2018 STREAM_GETC(s
, type
);
2019 STREAM_GETL(s
, prefix
.family
);
2020 switch (prefix
.family
) {
2022 STREAM_GET(&prefix
.u
.prefix4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
2023 STREAM_GETC(s
, prefix
.prefixlen
);
2024 if (prefix
.prefixlen
> IPV4_MAX_BITLEN
) {
2026 "%s: Specified prefix length %d is greater than a v4 address can support",
2027 __PRETTY_FUNCTION__
, prefix
.prefixlen
);
2030 STREAM_GET(&gate
.ipv4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
2033 STREAM_GET(&prefix
.u
.prefix6
, s
, 16);
2034 STREAM_GETC(s
, prefix
.prefixlen
);
2035 if (prefix
.prefixlen
> IPV6_MAX_BITLEN
) {
2037 "%s: Specified prefix length %d is greater than a v6 address can support",
2038 __PRETTY_FUNCTION__
, prefix
.prefixlen
);
2041 STREAM_GET(&gate
.ipv6
, s
, 16);
2044 zlog_warn("%s: Specified AF %d is not supported for this call",
2045 __PRETTY_FUNCTION__
, prefix
.family
);
2048 STREAM_GETL(s
, ifindex
);
2049 STREAM_GETC(s
, distance
);
2050 STREAM_GETL(s
, in_label
);
2051 STREAM_GETL(s
, out_label
);
2053 switch (prefix
.family
) {
2056 gtype
= NEXTHOP_TYPE_IPV4_IFINDEX
;
2058 gtype
= NEXTHOP_TYPE_IPV4
;
2062 gtype
= NEXTHOP_TYPE_IPV6_IFINDEX
;
2064 gtype
= NEXTHOP_TYPE_IPV6
;
2073 if (hdr
->command
== ZEBRA_MPLS_LABELS_ADD
) {
2074 mpls_lsp_install(zvrf
, type
, in_label
, out_label
, gtype
, &gate
,
2076 mpls_ftn_update(1, zvrf
, type
, &prefix
, gtype
, &gate
, ifindex
,
2077 distance
, out_label
);
2078 } else if (hdr
->command
== ZEBRA_MPLS_LABELS_DELETE
) {
2079 mpls_lsp_uninstall(zvrf
, type
, in_label
, gtype
, &gate
, ifindex
);
2080 mpls_ftn_update(0, zvrf
, type
, &prefix
, gtype
, &gate
, ifindex
,
2081 distance
, out_label
);
2087 static void zread_label_manager_connect(struct zserv
*client
,
2088 struct stream
*msg
, vrf_id_t vrf_id
)
2091 /* type of protocol (lib/zebra.h) */
2095 /* Get input stream. */
2099 STREAM_GETC(s
, proto
);
2100 STREAM_GETW(s
, instance
);
2102 /* accept only dynamic routing protocols */
2103 if ((proto
>= ZEBRA_ROUTE_MAX
) || (proto
<= ZEBRA_ROUTE_STATIC
)) {
2104 zlog_err("client %d has wrong protocol %s", client
->sock
,
2105 zebra_route_string(proto
));
2106 zsend_label_manager_connect_response(client
, vrf_id
, 1);
2109 zlog_notice("client %d with instance %u connected as %s", client
->sock
,
2110 instance
, zebra_route_string(proto
));
2111 client
->proto
= proto
;
2112 client
->instance
= instance
;
2115 Release previous labels of same protocol and instance.
2116 This is done in case it restarted from an unexpected shutdown.
2118 release_daemon_chunks(proto
, instance
);
2121 " Label Manager client connected: sock %d, proto %s, instance %u",
2122 client
->sock
, zebra_route_string(proto
), instance
);
2123 /* send response back */
2124 zsend_label_manager_connect_response(client
, vrf_id
, 0);
2130 static void zread_get_label_chunk(struct zserv
*client
, struct stream
*msg
,
2136 struct label_manager_chunk
*lmc
;
2138 /* Get input stream. */
2142 STREAM_GETC(s
, keep
);
2143 STREAM_GETL(s
, size
);
2145 lmc
= assign_label_chunk(client
->proto
, client
->instance
, keep
, size
);
2147 zlog_err("%s: Unable to assign Label Chunk of size %u",
2150 zlog_debug("Assigned Label Chunk %u - %u to %u", lmc
->start
,
2152 /* send response back */
2153 zsend_assign_label_chunk_response(client
, vrf_id
, lmc
);
2159 static void zread_release_label_chunk(struct zserv
*client
, struct stream
*msg
)
2162 uint32_t start
, end
;
2164 /* Get input stream. */
2168 STREAM_GETL(s
, start
);
2169 STREAM_GETL(s
, end
);
2171 release_label_chunk(client
->proto
, client
->instance
, start
, end
);
2176 static void zread_label_manager_request(ZAPI_HANDLER_ARGS
)
2178 /* to avoid sending other messages like ZERBA_INTERFACE_UP */
2179 if (hdr
->command
== ZEBRA_LABEL_MANAGER_CONNECT
)
2180 client
->is_synchronous
= 1;
2182 /* external label manager */
2184 zread_relay_label_manager_request(hdr
->command
, client
,
2186 /* this is a label manager */
2188 if (hdr
->command
== ZEBRA_LABEL_MANAGER_CONNECT
)
2189 zread_label_manager_connect(client
, msg
, zvrf_id(zvrf
));
2191 /* Sanity: don't allow 'unidentified' requests */
2192 if (!client
->proto
) {
2194 "Got label request from an unidentified client");
2197 if (hdr
->command
== ZEBRA_GET_LABEL_CHUNK
)
2198 zread_get_label_chunk(client
, msg
,
2200 else if (hdr
->command
== ZEBRA_RELEASE_LABEL_CHUNK
)
2201 zread_release_label_chunk(client
, msg
);
2206 static void zread_pseudowire(ZAPI_HANDLER_ARGS
)
2209 char ifname
[IF_NAMESIZE
];
2213 union g_addr nexthop
;
2214 uint32_t local_label
;
2215 uint32_t remote_label
;
2217 union pw_protocol_fields data
;
2219 struct zebra_pw
*pw
;
2221 /* Get input stream. */
2225 STREAM_GET(ifname
, s
, IF_NAMESIZE
);
2226 STREAM_GETL(s
, ifindex
);
2227 STREAM_GETL(s
, type
);
2231 STREAM_GET(&nexthop
.ipv4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
2234 STREAM_GET(&nexthop
.ipv6
, s
, 16);
2239 STREAM_GETL(s
, local_label
);
2240 STREAM_GETL(s
, remote_label
);
2241 STREAM_GETC(s
, flags
);
2242 STREAM_GET(&data
, s
, sizeof(data
));
2243 protocol
= client
->proto
;
2245 pw
= zebra_pw_find(zvrf
, ifname
);
2246 switch (hdr
->command
) {
2249 zlog_warn("%s: pseudowire %s already exists [%s]",
2251 zserv_command_string(hdr
->command
));
2255 zebra_pw_add(zvrf
, ifname
, protocol
, client
);
2257 case ZEBRA_PW_DELETE
:
2259 zlog_warn("%s: pseudowire %s not found [%s]", __func__
,
2260 ifname
, zserv_command_string(hdr
->command
));
2264 zebra_pw_del(zvrf
, pw
);
2267 case ZEBRA_PW_UNSET
:
2269 zlog_warn("%s: pseudowire %s not found [%s]", __func__
,
2270 ifname
, zserv_command_string(hdr
->command
));
2274 switch (hdr
->command
) {
2278 case ZEBRA_PW_UNSET
:
2283 zebra_pw_change(pw
, ifindex
, type
, af
, &nexthop
, local_label
,
2284 remote_label
, flags
, &data
);
2292 /* Cleanup registered nexthops (across VRFs) upon client disconnect. */
2293 static void zebra_client_close_cleanup_rnh(struct zserv
*client
)
2296 struct zebra_vrf
*zvrf
;
2298 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
2299 if ((zvrf
= vrf
->info
) != NULL
) {
2300 zebra_cleanup_rnh_client(zvrf_id(zvrf
), AF_INET
, client
,
2302 zebra_cleanup_rnh_client(zvrf_id(zvrf
), AF_INET6
,
2303 client
, RNH_NEXTHOP_TYPE
);
2304 zebra_cleanup_rnh_client(zvrf_id(zvrf
), AF_INET
, client
,
2305 RNH_IMPORT_CHECK_TYPE
);
2306 zebra_cleanup_rnh_client(zvrf_id(zvrf
), AF_INET6
,
2307 client
, RNH_IMPORT_CHECK_TYPE
);
2308 if (client
->proto
== ZEBRA_ROUTE_LDP
) {
2309 hash_iterate(zvrf
->lsp_table
,
2310 mpls_ldp_lsp_uninstall_all
,
2312 mpls_ldp_ftn_uninstall_all(zvrf
, AFI_IP
);
2313 mpls_ldp_ftn_uninstall_all(zvrf
, AFI_IP6
);
2319 static void zread_interface_set_master(ZAPI_HANDLER_ARGS
)
2321 struct interface
*master
;
2322 struct interface
*slave
;
2323 struct stream
*s
= msg
;
2327 STREAM_GETL(s
, vrf_id
);
2328 STREAM_GETL(s
, ifindex
);
2329 master
= if_lookup_by_index(ifindex
, vrf_id
);
2331 STREAM_GETL(s
, vrf_id
);
2332 STREAM_GETL(s
, ifindex
);
2333 slave
= if_lookup_by_index(ifindex
, vrf_id
);
2335 if (!master
|| !slave
)
2338 kernel_interface_set_master(master
, slave
);
2345 static void zread_vrf_label(ZAPI_HANDLER_ARGS
)
2347 struct interface
*ifp
;
2348 mpls_label_t nlabel
;
2351 struct zebra_vrf
*def_zvrf
;
2352 enum lsp_types_t ltype
;
2355 STREAM_GETL(s
, nlabel
);
2356 STREAM_GETC(s
, afi
);
2357 if (nlabel
== zvrf
->label
[afi
]) {
2359 * Nothing to do here move along
2364 STREAM_GETC(s
, ltype
);
2366 if (zvrf
->vrf
->vrf_id
!= VRF_DEFAULT
)
2367 ifp
= if_lookup_by_name(zvrf
->vrf
->name
, zvrf
->vrf
->vrf_id
);
2369 ifp
= if_lookup_by_name("lo", VRF_DEFAULT
);
2372 zlog_debug("Unable to find specified Interface for %s",
2377 def_zvrf
= zebra_vrf_lookup_by_id(VRF_DEFAULT
);
2379 if (zvrf
->label
[afi
] != MPLS_LABEL_NONE
) {
2383 really_remove
= true;
2384 for (scrubber
= AFI_IP
; scrubber
< AFI_MAX
; scrubber
++) {
2385 if (scrubber
== afi
)
2388 if (zvrf
->label
[scrubber
] == MPLS_LABEL_NONE
)
2391 if (zvrf
->label
[afi
] == zvrf
->label
[scrubber
]) {
2392 really_remove
= false;
2398 mpls_lsp_uninstall(def_zvrf
, ltype
, zvrf
->label
[afi
],
2399 NEXTHOP_TYPE_IFINDEX
, NULL
,
2403 if (nlabel
!= MPLS_LABEL_NONE
)
2404 mpls_lsp_install(def_zvrf
, ltype
, nlabel
,
2405 MPLS_LABEL_IMPLICIT_NULL
, NEXTHOP_TYPE_IFINDEX
,
2406 NULL
, ifp
->ifindex
);
2408 zvrf
->label
[afi
] = nlabel
;
2413 static inline void zread_rule(ZAPI_HANDLER_ARGS
)
2415 struct zebra_pbr_rule zpr
;
2421 STREAM_GETL(s
, total
);
2423 for (i
= 0; i
< total
; i
++) {
2424 memset(&zpr
, 0, sizeof(zpr
));
2426 zpr
.sock
= client
->sock
;
2427 STREAM_GETL(s
, zpr
.seq
);
2428 STREAM_GETL(s
, zpr
.priority
);
2429 STREAM_GETL(s
, zpr
.unique
);
2430 STREAM_GETC(s
, zpr
.filter
.src_ip
.family
);
2431 STREAM_GETC(s
, zpr
.filter
.src_ip
.prefixlen
);
2432 STREAM_GET(&zpr
.filter
.src_ip
.u
.prefix
, s
,
2433 prefix_blen(&zpr
.filter
.src_ip
));
2434 STREAM_GETW(s
, zpr
.filter
.src_port
);
2435 STREAM_GETC(s
, zpr
.filter
.dst_ip
.family
);
2436 STREAM_GETC(s
, zpr
.filter
.dst_ip
.prefixlen
);
2437 STREAM_GET(&zpr
.filter
.dst_ip
.u
.prefix
, s
,
2438 prefix_blen(&zpr
.filter
.dst_ip
));
2439 STREAM_GETW(s
, zpr
.filter
.dst_port
);
2440 STREAM_GETL(s
, zpr
.action
.table
);
2441 STREAM_GETL(s
, ifindex
);
2443 zpr
.ifp
= if_lookup_by_index(ifindex
, VRF_UNKNOWN
);
2445 zlog_debug("FAiled to lookup ifindex: %u", ifindex
);
2449 if (!is_default_prefix(&zpr
.filter
.src_ip
))
2450 zpr
.filter
.filter_bm
|= PBR_FILTER_SRC_IP
;
2452 if (!is_default_prefix(&zpr
.filter
.dst_ip
))
2453 zpr
.filter
.filter_bm
|= PBR_FILTER_DST_IP
;
2455 if (zpr
.filter
.src_port
)
2456 zpr
.filter
.filter_bm
|= PBR_FILTER_SRC_PORT
;
2458 if (zpr
.filter
.dst_port
)
2459 zpr
.filter
.filter_bm
|= PBR_FILTER_DST_PORT
;
2461 if (hdr
->command
== ZEBRA_RULE_ADD
)
2462 zebra_pbr_add_rule(zvrf
->zns
, &zpr
);
2464 zebra_pbr_del_rule(zvrf
->zns
, &zpr
);
2471 void (*zserv_handlers
[])(ZAPI_HANDLER_ARGS
) = {
2472 [ZEBRA_ROUTER_ID_ADD
] = zread_router_id_add
,
2473 [ZEBRA_ROUTER_ID_DELETE
] = zread_router_id_delete
,
2474 [ZEBRA_INTERFACE_ADD
] = zread_interface_add
,
2475 [ZEBRA_INTERFACE_DELETE
] = zread_interface_delete
,
2476 [ZEBRA_ROUTE_ADD
] = zread_route_add
,
2477 [ZEBRA_ROUTE_DELETE
] = zread_route_del
,
2478 [ZEBRA_IPV4_ROUTE_ADD
] = zread_ipv4_add
,
2479 [ZEBRA_IPV4_ROUTE_DELETE
] = zread_ipv4_delete
,
2480 [ZEBRA_IPV4_ROUTE_IPV6_NEXTHOP_ADD
] = zread_ipv4_route_ipv6_nexthop_add
,
2481 [ZEBRA_IPV6_ROUTE_ADD
] = zread_ipv6_add
,
2482 [ZEBRA_IPV6_ROUTE_DELETE
] = zread_ipv6_delete
,
2483 [ZEBRA_REDISTRIBUTE_ADD
] = zebra_redistribute_add
,
2484 [ZEBRA_REDISTRIBUTE_DELETE
] = zebra_redistribute_delete
,
2485 [ZEBRA_REDISTRIBUTE_DEFAULT_ADD
] = zebra_redistribute_default_add
,
2486 [ZEBRA_REDISTRIBUTE_DEFAULT_DELETE
] = zebra_redistribute_default_delete
,
2487 [ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB
] = zread_ipv4_nexthop_lookup_mrib
,
2488 [ZEBRA_HELLO
] = zread_hello
,
2489 [ZEBRA_NEXTHOP_REGISTER
] = zread_rnh_register
,
2490 [ZEBRA_NEXTHOP_UNREGISTER
] = zread_rnh_unregister
,
2491 [ZEBRA_IMPORT_ROUTE_REGISTER
] = zread_rnh_register
,
2492 [ZEBRA_IMPORT_ROUTE_UNREGISTER
] = zread_rnh_unregister
,
2493 [ZEBRA_BFD_DEST_UPDATE
] = zebra_ptm_bfd_dst_register
,
2494 [ZEBRA_BFD_DEST_REGISTER
] = zebra_ptm_bfd_dst_register
,
2495 [ZEBRA_BFD_DEST_DEREGISTER
] = zebra_ptm_bfd_dst_deregister
,
2496 [ZEBRA_VRF_UNREGISTER
] = zread_vrf_unregister
,
2497 [ZEBRA_VRF_LABEL
] = zread_vrf_label
,
2498 [ZEBRA_BFD_CLIENT_REGISTER
] = zebra_ptm_bfd_client_register
,
2499 #if defined(HAVE_RTADV)
2500 [ZEBRA_INTERFACE_ENABLE_RADV
] = zebra_interface_radv_enable
,
2501 [ZEBRA_INTERFACE_DISABLE_RADV
] = zebra_interface_radv_disable
,
2503 [ZEBRA_INTERFACE_ENABLE_RADV
] = NULL
,
2504 [ZEBRA_INTERFACE_DISABLE_RADV
] = NULL
,
2506 [ZEBRA_MPLS_LABELS_ADD
] = zread_mpls_labels
,
2507 [ZEBRA_MPLS_LABELS_DELETE
] = zread_mpls_labels
,
2508 [ZEBRA_IPMR_ROUTE_STATS
] = zebra_ipmr_route_stats
,
2509 [ZEBRA_LABEL_MANAGER_CONNECT
] = zread_label_manager_request
,
2510 [ZEBRA_GET_LABEL_CHUNK
] = zread_label_manager_request
,
2511 [ZEBRA_RELEASE_LABEL_CHUNK
] = zread_label_manager_request
,
2512 [ZEBRA_FEC_REGISTER
] = zread_fec_register
,
2513 [ZEBRA_FEC_UNREGISTER
] = zread_fec_unregister
,
2514 [ZEBRA_ADVERTISE_DEFAULT_GW
] = zebra_vxlan_advertise_gw_macip
,
2515 [ZEBRA_ADVERTISE_SUBNET
] = zebra_vxlan_advertise_subnet
,
2516 [ZEBRA_ADVERTISE_ALL_VNI
] = zebra_vxlan_advertise_all_vni
,
2517 [ZEBRA_REMOTE_VTEP_ADD
] = zebra_vxlan_remote_vtep_add
,
2518 [ZEBRA_REMOTE_VTEP_DEL
] = zebra_vxlan_remote_vtep_del
,
2519 [ZEBRA_REMOTE_MACIP_ADD
] = zebra_vxlan_remote_macip_add
,
2520 [ZEBRA_REMOTE_MACIP_DEL
] = zebra_vxlan_remote_macip_del
,
2521 [ZEBRA_INTERFACE_SET_MASTER
] = zread_interface_set_master
,
2522 [ZEBRA_PW_ADD
] = zread_pseudowire
,
2523 [ZEBRA_PW_DELETE
] = zread_pseudowire
,
2524 [ZEBRA_PW_SET
] = zread_pseudowire
,
2525 [ZEBRA_PW_UNSET
] = zread_pseudowire
,
2526 [ZEBRA_RULE_ADD
] = zread_rule
,
2527 [ZEBRA_RULE_DELETE
] = zread_rule
,
2530 static inline void zserv_handle_commands(struct zserv
*client
,
2531 struct zmsghdr
*hdr
,
2533 struct zebra_vrf
*zvrf
)
2535 if (hdr
->command
> sizeof(zserv_handlers
)
2536 || zserv_handlers
[hdr
->command
] == NULL
)
2537 zlog_info("Zebra received unknown command %d", hdr
->command
);
2539 zserv_handlers
[hdr
->command
](client
, hdr
, msg
, zvrf
);
2544 /* Lifecycle ---------------------------------------------------------------- */
2546 /* free zebra client information. */
2547 static void zebra_client_free(struct zserv
*client
)
2549 /* Send client de-registration to BFD */
2550 zebra_ptm_bfd_client_deregister(client
->proto
);
2552 /* Cleanup any registered nexthops - across all VRFs. */
2553 zebra_client_close_cleanup_rnh(client
);
2555 /* Release Label Manager chunks */
2556 release_daemon_chunks(client
->proto
, client
->instance
);
2558 /* Cleanup any FECs registered by this client. */
2559 zebra_mpls_cleanup_fecs_for_client(vrf_info_lookup(VRF_DEFAULT
),
2562 /* Remove pseudowires associated with this client */
2563 zebra_pw_client_close(client
);
2565 /* Close file descriptor. */
2567 unsigned long nroutes
;
2569 close(client
->sock
);
2570 nroutes
= rib_score_proto(client
->proto
, client
->instance
);
2572 "client %d disconnected. %lu %s routes removed from the rib",
2573 client
->sock
, nroutes
,
2574 zebra_route_string(client
->proto
));
2578 /* Free stream buffers. */
2579 if (client
->ibuf_work
)
2580 stream_free(client
->ibuf_work
);
2581 if (client
->obuf_work
)
2582 stream_free(client
->obuf_work
);
2583 if (client
->ibuf_fifo
)
2584 stream_fifo_free(client
->ibuf_fifo
);
2585 if (client
->obuf_fifo
)
2586 stream_fifo_free(client
->obuf_fifo
);
2588 buffer_free(client
->wb
);
2590 /* Release threads. */
2592 thread_cancel(client
->t_read
);
2593 if (client
->t_write
)
2594 thread_cancel(client
->t_write
);
2595 if (client
->t_suicide
)
2596 thread_cancel(client
->t_suicide
);
2599 for (afi_t afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
2600 for (int i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
2601 vrf_bitmap_free(client
->redist
[afi
][i
]);
2603 vrf_bitmap_free(client
->redist_default
);
2604 vrf_bitmap_free(client
->ifinfo
);
2605 vrf_bitmap_free(client
->ridinfo
);
2607 XFREE(MTYPE_TMP
, client
);
2611 * Called from client thread to terminate itself.
2613 static void zebra_client_close(struct zserv
*client
)
2615 listnode_delete(zebrad
.client_list
, client
);
2616 zebra_client_free(client
);
2619 /* Make new client. */
2620 static void zebra_client_create(int sock
)
2622 struct zserv
*client
;
2626 client
= XCALLOC(MTYPE_TMP
, sizeof(struct zserv
));
2628 /* Make client input/output buffer. */
2629 client
->sock
= sock
;
2630 client
->ibuf_fifo
= stream_fifo_new();
2631 client
->obuf_fifo
= stream_fifo_new();
2632 client
->ibuf_work
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
2633 client
->obuf_work
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
2634 client
->wb
= buffer_new(0);
2636 /* Set table number. */
2637 client
->rtm_table
= zebrad
.rtm_table_default
;
2639 client
->connect_time
= monotime(NULL
);
2640 /* Initialize flags */
2641 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
2642 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
2643 client
->redist
[afi
][i
] = vrf_bitmap_init();
2644 client
->redist_default
= vrf_bitmap_init();
2645 client
->ifinfo
= vrf_bitmap_init();
2646 client
->ridinfo
= vrf_bitmap_init();
2648 /* by default, it's not a synchronous client */
2649 client
->is_synchronous
= 0;
2651 /* Add this client to linked list. */
2652 listnode_add(zebrad
.client_list
, client
);
2654 zebra_vrf_update_all(client
);
2656 /* start read loop */
2657 zebra_event(client
, ZEBRA_READ
);
2660 static int zserv_delayed_close(struct thread
*thread
)
2662 struct zserv
*client
= THREAD_ARG(thread
);
2664 client
->t_suicide
= NULL
;
2665 zebra_client_close(client
);
2670 * Log zapi message to zlog.
2679 * The message header
2681 static void zserv_log_message(const char *errmsg
, struct stream
*msg
,
2682 struct zmsghdr
*hdr
)
2684 zlog_debug("Rx'd ZAPI message");
2686 zlog_debug("%s", errmsg
);
2688 zlog_debug(" Length: %d", hdr
->length
);
2689 zlog_debug("Command: %s", zserv_command_string(hdr
->command
));
2690 zlog_debug(" VRF: %u", hdr
->vrf_id
);
2692 zlog_hexdump(msg
->data
, STREAM_READABLE(msg
));
2695 static int zserv_flush_data(struct thread
*thread
)
2697 struct zserv
*client
= THREAD_ARG(thread
);
2699 client
->t_write
= NULL
;
2700 if (client
->t_suicide
) {
2701 zebra_client_close(client
);
2704 switch (buffer_flush_available(client
->wb
, client
->sock
)) {
2707 "%s: buffer_flush_available failed on zserv client fd %d, closing",
2708 __func__
, client
->sock
);
2709 zebra_client_close(client
);
2712 case BUFFER_PENDING
:
2713 client
->t_write
= NULL
;
2714 thread_add_write(zebrad
.master
, zserv_flush_data
, client
,
2715 client
->sock
, &client
->t_write
);
2722 client
->last_write_time
= monotime(NULL
);
2727 * Write a single packet.
2729 static int zserv_write(struct thread
*thread
)
2731 struct zserv
*client
= THREAD_ARG(thread
);
2735 if (client
->t_suicide
)
2738 if (client
->is_synchronous
)
2741 msg
= stream_fifo_pop(client
->obuf_fifo
);
2742 stream_set_getp(msg
, 0);
2743 client
->last_write_cmd
= stream_getw_from(msg
, 6);
2745 writerv
= buffer_write(client
->wb
, client
->sock
, STREAM_DATA(msg
),
2746 stream_get_endp(msg
));
2753 "%s: buffer_write failed to zserv client fd %d, closing",
2754 __func__
, client
->sock
);
2756 * Schedule a delayed close since many of the functions that
2757 * call this one do not check the return code. They do not
2758 * allow for the possibility that an I/O error may have caused
2759 * the client to be deleted.
2761 client
->t_suicide
= NULL
;
2762 thread_add_event(zebrad
.master
, zserv_delayed_close
, client
, 0,
2763 &client
->t_suicide
);
2766 THREAD_OFF(client
->t_write
);
2768 case BUFFER_PENDING
:
2769 thread_add_write(zebrad
.master
, zserv_flush_data
, client
,
2770 client
->sock
, &client
->t_write
);
2774 if (client
->obuf_fifo
->count
)
2775 zebra_event(client
, ZEBRA_WRITE
);
2777 client
->last_write_time
= monotime(NULL
);
2781 #if defined(HANDLE_ZAPI_FUZZING)
2782 static void zserv_write_incoming(struct stream
*orig
, uint16_t command
)
2784 char fname
[MAXPATHLEN
];
2785 struct stream
*copy
;
2788 copy
= stream_dup(orig
);
2789 stream_set_getp(copy
, 0);
2791 zserv_privs
.change(ZPRIVS_RAISE
);
2792 snprintf(fname
, MAXPATHLEN
, "%s/%u", DAEMON_VTY_DIR
, command
);
2793 fd
= open(fname
, O_CREAT
| O_WRONLY
| O_EXCL
, 0644);
2794 stream_flush(copy
, fd
);
2796 zserv_privs
.change(ZPRIVS_LOWER
);
2801 static int zserv_process_messages(struct thread
*thread
)
2803 struct zserv
*client
= THREAD_ARG(thread
);
2804 struct zebra_vrf
*zvrf
;
2810 msg
= stream_fifo_pop(client
->ibuf_fifo
);
2812 /* break if out of messages */
2816 /* read & check header */
2817 hdrvalid
= zapi_parse_header(msg
, &hdr
);
2818 if (!hdrvalid
&& IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
) {
2819 const char *emsg
= "Message has corrupt header";
2820 zserv_log_message(emsg
, msg
, NULL
);
2826 zvrf
= zebra_vrf_lookup_by_id(hdr
.vrf_id
);
2827 if (!zvrf
&& IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
) {
2828 const char *emsg
= "Message specifies unknown VRF";
2829 zserv_log_message(emsg
, msg
, &hdr
);
2834 /* process commands */
2835 zserv_handle_commands(client
, &hdr
, msg
, zvrf
);
2842 /* Handler of zebra service request. */
2843 static int zserv_read(struct thread
*thread
)
2846 struct zserv
*client
;
2848 #if defined(HANDLE_ZAPI_FUZZING)
2851 int packets
= zebrad
.packets_to_process
;
2858 /* Get thread data. Reset reading thread because I'm running. */
2859 sock
= THREAD_FD(thread
);
2860 client
= THREAD_ARG(thread
);
2862 if (client
->t_suicide
) {
2863 zebra_client_close(client
);
2868 already
= stream_get_endp(client
->ibuf_work
);
2870 /* Read length and command (if we don't have it already). */
2871 if (already
< ZEBRA_HEADER_SIZE
) {
2872 nb
= stream_read_try(client
->ibuf_work
, sock
,
2873 ZEBRA_HEADER_SIZE
- already
);
2874 if ((nb
== 0 || nb
== -1) && IS_ZEBRA_DEBUG_EVENT
)
2875 zlog_debug("connection closed socket [%d]",
2877 if ((nb
== 0 || nb
== -1))
2879 if (nb
!= (ssize_t
)(ZEBRA_HEADER_SIZE
- already
)) {
2880 /* Try again later. */
2883 already
= ZEBRA_HEADER_SIZE
;
2886 /* Reset to read from the beginning of the incoming packet. */
2887 stream_set_getp(client
->ibuf_work
, 0);
2889 /* Fetch header values */
2890 hdrvalid
= zapi_parse_header(client
->ibuf_work
, &hdr
);
2893 snprintf(errmsg
, sizeof(errmsg
),
2894 "%s: Message has corrupt header", __func__
);
2895 zserv_log_message(errmsg
, client
->ibuf_work
, NULL
);
2899 /* Validate header */
2900 if (hdr
.marker
!= ZEBRA_HEADER_MARKER
2901 || hdr
.version
!= ZSERV_VERSION
) {
2903 errmsg
, sizeof(errmsg
),
2904 "Message has corrupt header\n%s: socket %d version mismatch, marker %d, version %d",
2905 __func__
, sock
, hdr
.marker
, hdr
.version
);
2906 zserv_log_message(errmsg
, client
->ibuf_work
, &hdr
);
2909 if (hdr
.length
< ZEBRA_HEADER_SIZE
) {
2911 errmsg
, sizeof(errmsg
),
2912 "Message has corrupt header\n%s: socket %d message length %u is less than header size %d",
2913 __func__
, sock
, hdr
.length
, ZEBRA_HEADER_SIZE
);
2914 zserv_log_message(errmsg
, client
->ibuf_work
, &hdr
);
2917 if (hdr
.length
> STREAM_SIZE(client
->ibuf_work
)) {
2919 errmsg
, sizeof(errmsg
),
2920 "Message has corrupt header\n%s: socket %d message length %u exceeds buffer size %lu",
2921 __func__
, sock
, hdr
.length
,
2922 (unsigned long)STREAM_SIZE(client
->ibuf_work
));
2926 /* Read rest of data. */
2927 if (already
< hdr
.length
) {
2928 nb
= stream_read_try(client
->ibuf_work
, sock
,
2929 hdr
.length
- already
);
2930 if ((nb
== 0 || nb
== -1) && IS_ZEBRA_DEBUG_EVENT
)
2932 "connection closed [%d] when reading zebra data",
2934 if ((nb
== 0 || nb
== -1))
2936 if (nb
!= (ssize_t
)(hdr
.length
- already
)) {
2937 /* Try again later. */
2942 #if defined(HANDLE_ZAPI_FUZZING)
2943 zserv_write_incoming(client
->ibuf_work
, command
);
2945 hdr
.length
-= ZEBRA_HEADER_SIZE
;
2947 /* Debug packet information. */
2948 if (IS_ZEBRA_DEBUG_EVENT
)
2949 zlog_debug("zebra message comes from socket [%d]",
2952 if (IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
)
2953 zserv_log_message(NULL
, client
->ibuf_work
, &hdr
);
2955 client
->last_read_time
= monotime(NULL
);
2956 client
->last_read_cmd
= hdr
.command
;
2958 stream_set_getp(client
->ibuf_work
, 0);
2959 struct stream
*msg
= stream_dup(client
->ibuf_work
);
2961 stream_fifo_push(client
->ibuf_fifo
, msg
);
2963 if (client
->t_suicide
)
2967 stream_reset(client
->ibuf_work
);
2970 if (IS_ZEBRA_DEBUG_PACKET
)
2971 zlog_debug("Read %d packets",
2972 zebrad
.packets_to_process
- packets
);
2974 /* Schedule job to process those packets */
2975 thread_add_event(zebrad
.master
, &zserv_process_messages
, client
, 0,
2978 /* Reschedule ourselves */
2979 zebra_event(client
, ZEBRA_READ
);
2984 zebra_client_close(client
);
2988 static void zebra_event(struct zserv
*client
, enum event event
)
2992 thread_add_read(zebrad
.master
, zserv_read
, client
, client
->sock
,
2996 thread_add_write(zebrad
.master
, zserv_write
, client
,
2997 client
->sock
, &client
->t_write
);
3002 /* Accept code of zebra server socket. */
3003 static int zebra_accept(struct thread
*thread
)
3007 struct sockaddr_in client
;
3010 accept_sock
= THREAD_FD(thread
);
3012 /* Reregister myself. */
3013 thread_add_read(zebrad
.master
, zebra_accept
, NULL
, accept_sock
, NULL
);
3015 len
= sizeof(struct sockaddr_in
);
3016 client_sock
= accept(accept_sock
, (struct sockaddr
*)&client
, &len
);
3018 if (client_sock
< 0) {
3019 zlog_warn("Can't accept zebra socket: %s",
3020 safe_strerror(errno
));
3024 /* Make client socket non-blocking. */
3025 set_nonblocking(client_sock
);
3027 /* Create new zebra client. */
3028 zebra_client_create(client_sock
);
3033 /* Make zebra server socket, wiping any existing one (see bug #403). */
3034 void zebra_zserv_socket_init(char *path
)
3039 struct sockaddr_storage sa
;
3042 if (!frr_zclient_addr(&sa
, &sa_len
, path
))
3043 /* should be caught in zebra main() */
3047 old_mask
= umask(0077);
3049 /* Make UNIX domain socket. */
3050 sock
= socket(sa
.ss_family
, SOCK_STREAM
, 0);
3052 zlog_warn("Can't create zserv socket: %s",
3053 safe_strerror(errno
));
3055 "zebra can't provide full functionality due to above error");
3059 if (sa
.ss_family
!= AF_UNIX
) {
3060 sockopt_reuseaddr(sock
);
3061 sockopt_reuseport(sock
);
3063 struct sockaddr_un
*suna
= (struct sockaddr_un
*)&sa
;
3064 if (suna
->sun_path
[0])
3065 unlink(suna
->sun_path
);
3068 zserv_privs
.change(ZPRIVS_RAISE
);
3069 setsockopt_so_recvbuf(sock
, 1048576);
3070 setsockopt_so_sendbuf(sock
, 1048576);
3071 zserv_privs
.change(ZPRIVS_LOWER
);
3073 if (sa
.ss_family
!= AF_UNIX
&& zserv_privs
.change(ZPRIVS_RAISE
))
3074 zlog_err("Can't raise privileges");
3076 ret
= bind(sock
, (struct sockaddr
*)&sa
, sa_len
);
3078 zlog_warn("Can't bind zserv socket on %s: %s", path
,
3079 safe_strerror(errno
));
3081 "zebra can't provide full functionality due to above error");
3085 if (sa
.ss_family
!= AF_UNIX
&& zserv_privs
.change(ZPRIVS_LOWER
))
3086 zlog_err("Can't lower privileges");
3088 ret
= listen(sock
, 5);
3090 zlog_warn("Can't listen to zserv socket %s: %s", path
,
3091 safe_strerror(errno
));
3093 "zebra can't provide full functionality due to above error");
3100 thread_add_read(zebrad
.master
, zebra_accept
, NULL
, sock
, NULL
);
3103 #define ZEBRA_TIME_BUF 32
3104 static char *zserv_time_buf(time_t *time1
, char *buf
, int buflen
)
3109 assert(buf
!= NULL
);
3110 assert(buflen
>= ZEBRA_TIME_BUF
);
3111 assert(time1
!= NULL
);
3114 snprintf(buf
, buflen
, "never ");
3118 now
= monotime(NULL
);
3122 if (now
< ONE_DAY_SECOND
)
3123 snprintf(buf
, buflen
, "%02d:%02d:%02d", tm
->tm_hour
, tm
->tm_min
,
3125 else if (now
< ONE_WEEK_SECOND
)
3126 snprintf(buf
, buflen
, "%dd%02dh%02dm", tm
->tm_yday
, tm
->tm_hour
,
3129 snprintf(buf
, buflen
, "%02dw%dd%02dh", tm
->tm_yday
/ 7,
3130 tm
->tm_yday
- ((tm
->tm_yday
/ 7) * 7), tm
->tm_hour
);
3134 static void zebra_show_client_detail(struct vty
*vty
, struct zserv
*client
)
3136 char cbuf
[ZEBRA_TIME_BUF
], rbuf
[ZEBRA_TIME_BUF
];
3137 char wbuf
[ZEBRA_TIME_BUF
], nhbuf
[ZEBRA_TIME_BUF
], mbuf
[ZEBRA_TIME_BUF
];
3139 vty_out(vty
, "Client: %s", zebra_route_string(client
->proto
));
3140 if (client
->instance
)
3141 vty_out(vty
, " Instance: %d", client
->instance
);
3144 vty_out(vty
, "------------------------ \n");
3145 vty_out(vty
, "FD: %d \n", client
->sock
);
3146 vty_out(vty
, "Route Table ID: %d \n", client
->rtm_table
);
3148 vty_out(vty
, "Connect Time: %s \n",
3149 zserv_time_buf(&client
->connect_time
, cbuf
, ZEBRA_TIME_BUF
));
3150 if (client
->nh_reg_time
) {
3151 vty_out(vty
, "Nexthop Registry Time: %s \n",
3152 zserv_time_buf(&client
->nh_reg_time
, nhbuf
,
3154 if (client
->nh_last_upd_time
)
3155 vty_out(vty
, "Nexthop Last Update Time: %s \n",
3156 zserv_time_buf(&client
->nh_last_upd_time
, mbuf
,
3159 vty_out(vty
, "No Nexthop Update sent\n");
3161 vty_out(vty
, "Not registered for Nexthop Updates\n");
3163 vty_out(vty
, "Last Msg Rx Time: %s \n",
3164 zserv_time_buf(&client
->last_read_time
, rbuf
, ZEBRA_TIME_BUF
));
3165 vty_out(vty
, "Last Msg Tx Time: %s \n",
3166 zserv_time_buf(&client
->last_write_time
, wbuf
, ZEBRA_TIME_BUF
));
3167 if (client
->last_read_time
)
3168 vty_out(vty
, "Last Rcvd Cmd: %s \n",
3169 zserv_command_string(client
->last_read_cmd
));
3170 if (client
->last_write_time
)
3171 vty_out(vty
, "Last Sent Cmd: %s \n",
3172 zserv_command_string(client
->last_write_cmd
));
3175 vty_out(vty
, "Type Add Update Del \n");
3176 vty_out(vty
, "================================================== \n");
3177 vty_out(vty
, "IPv4 %-12d%-12d%-12d\n", client
->v4_route_add_cnt
,
3178 client
->v4_route_upd8_cnt
, client
->v4_route_del_cnt
);
3179 vty_out(vty
, "IPv6 %-12d%-12d%-12d\n", client
->v6_route_add_cnt
,
3180 client
->v6_route_upd8_cnt
, client
->v6_route_del_cnt
);
3181 vty_out(vty
, "Redist:v4 %-12d%-12d%-12d\n", client
->redist_v4_add_cnt
,
3182 0, client
->redist_v4_del_cnt
);
3183 vty_out(vty
, "Redist:v6 %-12d%-12d%-12d\n", client
->redist_v6_add_cnt
,
3184 0, client
->redist_v6_del_cnt
);
3185 vty_out(vty
, "Connected %-12d%-12d%-12d\n", client
->ifadd_cnt
, 0,
3187 vty_out(vty
, "BFD peer %-12d%-12d%-12d\n", client
->bfd_peer_add_cnt
,
3188 client
->bfd_peer_upd8_cnt
, client
->bfd_peer_del_cnt
);
3189 vty_out(vty
, "Interface Up Notifications: %d\n", client
->ifup_cnt
);
3190 vty_out(vty
, "Interface Down Notifications: %d\n", client
->ifdown_cnt
);
3191 vty_out(vty
, "VNI add notifications: %d\n", client
->vniadd_cnt
);
3192 vty_out(vty
, "VNI delete notifications: %d\n", client
->vnidel_cnt
);
3193 vty_out(vty
, "L3-VNI add notifications: %d\n", client
->l3vniadd_cnt
);
3194 vty_out(vty
, "L3-VNI delete notifications: %d\n", client
->l3vnidel_cnt
);
3195 vty_out(vty
, "MAC-IP add notifications: %d\n", client
->macipadd_cnt
);
3196 vty_out(vty
, "MAC-IP delete notifications: %d\n", client
->macipdel_cnt
);
3202 static void zebra_show_client_brief(struct vty
*vty
, struct zserv
*client
)
3204 char cbuf
[ZEBRA_TIME_BUF
], rbuf
[ZEBRA_TIME_BUF
];
3205 char wbuf
[ZEBRA_TIME_BUF
];
3207 vty_out(vty
, "%-8s%12s %12s%12s%8d/%-8d%8d/%-8d\n",
3208 zebra_route_string(client
->proto
),
3209 zserv_time_buf(&client
->connect_time
, cbuf
, ZEBRA_TIME_BUF
),
3210 zserv_time_buf(&client
->last_read_time
, rbuf
, ZEBRA_TIME_BUF
),
3211 zserv_time_buf(&client
->last_write_time
, wbuf
, ZEBRA_TIME_BUF
),
3212 client
->v4_route_add_cnt
+ client
->v4_route_upd8_cnt
,
3213 client
->v4_route_del_cnt
,
3214 client
->v6_route_add_cnt
+ client
->v6_route_upd8_cnt
,
3215 client
->v6_route_del_cnt
);
3218 struct zserv
*zebra_find_client(u_char proto
, u_short instance
)
3220 struct listnode
*node
, *nnode
;
3221 struct zserv
*client
;
3223 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, node
, nnode
, client
)) {
3224 if (client
->proto
== proto
&& client
->instance
== instance
)
3231 /* This command is for debugging purpose. */
3232 DEFUN (show_zebra_client
,
3233 show_zebra_client_cmd
,
3234 "show zebra client",
3237 "Client information\n")
3239 struct listnode
*node
;
3240 struct zserv
*client
;
3242 for (ALL_LIST_ELEMENTS_RO(zebrad
.client_list
, node
, client
))
3243 zebra_show_client_detail(vty
, client
);
3248 /* This command is for debugging purpose. */
3249 DEFUN (show_zebra_client_summary
,
3250 show_zebra_client_summary_cmd
,
3251 "show zebra client summary",
3254 "Client information brief\n"
3257 struct listnode
*node
;
3258 struct zserv
*client
;
3261 "Name Connect Time Last Read Last Write IPv4 Routes IPv6 Routes \n");
3263 "--------------------------------------------------------------------------------\n");
3265 for (ALL_LIST_ELEMENTS_RO(zebrad
.client_list
, node
, client
))
3266 zebra_show_client_brief(vty
, client
);
3268 vty_out(vty
, "Routes column shows (added+updated)/deleted\n");
3272 #if defined(HANDLE_ZAPI_FUZZING)
3273 void zserv_read_file(char *input
)
3276 struct zserv
*client
= NULL
;
3279 zebra_client_create(-1);
3280 client
= zebrad
.client_list
->head
->data
;
3283 fd
= open(input
, O_RDONLY
| O_NONBLOCK
);
3286 zebra_client_read(&t
);
3292 void zserv_init(void)
3294 /* Client list init. */
3295 zebrad
.client_list
= list_new();
3296 zebrad
.client_list
->del
= (void (*)(void *))zebra_client_free
;
3298 install_element(ENABLE_NODE
, &show_zebra_client_cmd
);
3299 install_element(ENABLE_NODE
, &show_zebra_client_summary_cmd
);