1 /* Zebra daemon server routine.
2 * Copyright (C) 1997, 98, 99 Kunihiro Ishiguro
4 * This file is part of GNU Zebra.
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
32 #include "zebra_memory.h"
36 #include "sockunion.h"
47 #include "zebra/zserv.h"
48 #include "zebra/zebra_ns.h"
49 #include "zebra/zebra_vrf.h"
50 #include "zebra/router-id.h"
51 #include "zebra/redistribute.h"
52 #include "zebra/debug.h"
53 #include "zebra/zebra_rnh.h"
54 #include "zebra/rt_netlink.h"
55 #include "zebra/interface.h"
56 #include "zebra/zebra_ptm.h"
57 #include "zebra/rtadv.h"
58 #include "zebra/zebra_mpls.h"
59 #include "zebra/zebra_mroute.h"
60 #include "zebra/label_manager.h"
61 #include "zebra/zebra_vxlan.h"
63 #include "zebra/zebra_pbr.h"
64 #include "zebra/table_manager.h"
66 /* Event list of zebra. */
67 enum event
{ ZEBRA_READ
, ZEBRA_WRITE
};
69 extern struct zebra_privs_t zserv_privs
;
70 /* post event into client */
71 static void zebra_event(struct zserv
*client
, enum event event
);
74 /* Public interface ======================================================== */
76 int zebra_server_send_message(struct zserv
*client
, struct stream
*msg
)
78 stream_fifo_push(client
->obuf_fifo
, msg
);
79 zebra_event(client
, ZEBRA_WRITE
);
83 /* Encoding helpers -------------------------------------------------------- */
85 static void zserv_encode_interface(struct stream
*s
, struct interface
*ifp
)
87 /* Interface information. */
88 stream_put(s
, ifp
->name
, INTERFACE_NAMSIZ
);
89 stream_putl(s
, ifp
->ifindex
);
90 stream_putc(s
, ifp
->status
);
91 stream_putq(s
, ifp
->flags
);
92 stream_putc(s
, ifp
->ptm_enable
);
93 stream_putc(s
, ifp
->ptm_status
);
94 stream_putl(s
, ifp
->metric
);
95 stream_putl(s
, ifp
->speed
);
96 stream_putl(s
, ifp
->mtu
);
97 stream_putl(s
, ifp
->mtu6
);
98 stream_putl(s
, ifp
->bandwidth
);
99 stream_putl(s
, ifp
->ll_type
);
100 stream_putl(s
, ifp
->hw_addr_len
);
101 if (ifp
->hw_addr_len
)
102 stream_put(s
, ifp
->hw_addr
, ifp
->hw_addr_len
);
104 /* Then, Traffic Engineering parameters if any */
105 if (HAS_LINK_PARAMS(ifp
) && IS_LINK_PARAMS_SET(ifp
->link_params
)) {
107 zebra_interface_link_params_write(s
, ifp
);
111 /* Write packet size. */
112 stream_putw_at(s
, 0, stream_get_endp(s
));
115 static void zserv_encode_vrf(struct stream
*s
, struct zebra_vrf
*zvrf
)
117 struct vrf_data data
;
118 const char *netns_name
= zvrf_ns_name(zvrf
);
120 data
.l
.table_id
= zvrf
->table_id
;
123 strlcpy(data
.l
.netns_name
, basename((char *)netns_name
),
126 memset(data
.l
.netns_name
, 0, NS_NAMSIZ
);
127 /* Pass the tableid and the netns NAME */
128 stream_put(s
, &data
, sizeof(struct vrf_data
));
129 /* Interface information. */
130 stream_put(s
, zvrf_name(zvrf
), VRF_NAMSIZ
);
131 /* Write packet size. */
132 stream_putw_at(s
, 0, stream_get_endp(s
));
135 static int zserv_encode_nexthop(struct stream
*s
, struct nexthop
*nexthop
)
137 stream_putc(s
, nexthop
->type
);
138 switch (nexthop
->type
) {
139 case NEXTHOP_TYPE_IPV4
:
140 case NEXTHOP_TYPE_IPV4_IFINDEX
:
141 stream_put_in_addr(s
, &nexthop
->gate
.ipv4
);
142 stream_putl(s
, nexthop
->ifindex
);
144 case NEXTHOP_TYPE_IPV6
:
145 stream_put(s
, &nexthop
->gate
.ipv6
, 16);
147 case NEXTHOP_TYPE_IPV6_IFINDEX
:
148 stream_put(s
, &nexthop
->gate
.ipv6
, 16);
149 stream_putl(s
, nexthop
->ifindex
);
151 case NEXTHOP_TYPE_IFINDEX
:
152 stream_putl(s
, nexthop
->ifindex
);
161 /* Send handlers ----------------------------------------------------------- */
163 /* Interface is added. Send ZEBRA_INTERFACE_ADD to client. */
165 * This function is called in the following situations:
166 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
168 * - at startup, when zebra figures out the available interfaces
169 * - when an interface is added (where support for
170 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
171 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
174 int zsend_interface_add(struct zserv
*client
, struct interface
*ifp
)
176 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
178 zclient_create_header(s
, ZEBRA_INTERFACE_ADD
, ifp
->vrf_id
);
179 zserv_encode_interface(s
, ifp
);
182 return zebra_server_send_message(client
, s
);
185 /* Interface deletion from zebra daemon. */
186 int zsend_interface_delete(struct zserv
*client
, struct interface
*ifp
)
188 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
190 zclient_create_header(s
, ZEBRA_INTERFACE_DELETE
, ifp
->vrf_id
);
191 zserv_encode_interface(s
, ifp
);
194 return zebra_server_send_message(client
, s
);
197 int zsend_vrf_add(struct zserv
*client
, struct zebra_vrf
*zvrf
)
199 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
201 zclient_create_header(s
, ZEBRA_VRF_ADD
, zvrf_id(zvrf
));
202 zserv_encode_vrf(s
, zvrf
);
204 client
->vrfadd_cnt
++;
205 return zebra_server_send_message(client
, s
);
208 /* VRF deletion from zebra daemon. */
209 int zsend_vrf_delete(struct zserv
*client
, struct zebra_vrf
*zvrf
)
212 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
214 zclient_create_header(s
, ZEBRA_VRF_DELETE
, zvrf_id(zvrf
));
215 zserv_encode_vrf(s
, zvrf
);
217 client
->vrfdel_cnt
++;
218 return zebra_server_send_message(client
, s
);
221 int zsend_interface_link_params(struct zserv
*client
, struct interface
*ifp
)
223 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
225 /* Check this client need interface information. */
226 if (!client
->ifinfo
) {
231 if (!ifp
->link_params
) {
236 zclient_create_header(s
, ZEBRA_INTERFACE_LINK_PARAMS
, ifp
->vrf_id
);
238 /* Add Interface Index */
239 stream_putl(s
, ifp
->ifindex
);
241 /* Then TE Link Parameters */
242 if (zebra_interface_link_params_write(s
, ifp
) == 0) {
247 /* Write packet size. */
248 stream_putw_at(s
, 0, stream_get_endp(s
));
250 return zebra_server_send_message(client
, s
);
253 /* Interface address is added/deleted. Send ZEBRA_INTERFACE_ADDRESS_ADD or
254 * ZEBRA_INTERFACE_ADDRESS_DELETE to the client.
256 * A ZEBRA_INTERFACE_ADDRESS_ADD is sent in the following situations:
257 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
258 * from the client, after the ZEBRA_INTERFACE_ADD has been
259 * sent from zebra to the client
260 * - redistribute new address info to all clients in the following situations
261 * - at startup, when zebra figures out the available interfaces
262 * - when an interface is added (where support for
263 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
264 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
266 * - for the vty commands "ip address A.B.C.D/M [<secondary>|<label LINE>]"
267 * and "no bandwidth <1-10000000>", "ipv6 address X:X::X:X/M"
268 * - when an RTM_NEWADDR message is received from the kernel,
270 * The call tree that triggers ZEBRA_INTERFACE_ADDRESS_DELETE:
272 * zsend_interface_address(DELETE)
275 * zebra_interface_address_delete_update
277 * | | if_delete_update
279 * ip_address_uninstall connected_delete_ipv4
280 * [ipv6_addresss_uninstall] [connected_delete_ipv6]
283 * | RTM_NEWADDR on routing/netlink socket
286 * "no ip address A.B.C.D/M [label LINE]"
287 * "no ip address A.B.C.D/M secondary"
288 * ["no ipv6 address X:X::X:X/M"]
291 int zsend_interface_address(int cmd
, struct zserv
*client
,
292 struct interface
*ifp
, struct connected
*ifc
)
296 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
298 zclient_create_header(s
, cmd
, ifp
->vrf_id
);
299 stream_putl(s
, ifp
->ifindex
);
301 /* Interface address flag. */
302 stream_putc(s
, ifc
->flags
);
304 /* Prefix information. */
306 stream_putc(s
, p
->family
);
307 blen
= prefix_blen(p
);
308 stream_put(s
, &p
->u
.prefix
, blen
);
311 * XXX gnu version does not send prefixlen for
312 * ZEBRA_INTERFACE_ADDRESS_DELETE
313 * but zebra_interface_address_delete_read() in the gnu version
316 stream_putc(s
, p
->prefixlen
);
319 p
= ifc
->destination
;
321 stream_put(s
, &p
->u
.prefix
, blen
);
323 stream_put(s
, NULL
, blen
);
325 /* Write packet size. */
326 stream_putw_at(s
, 0, stream_get_endp(s
));
328 client
->connected_rt_add_cnt
++;
329 return zebra_server_send_message(client
, s
);
332 static int zsend_interface_nbr_address(int cmd
, struct zserv
*client
,
333 struct interface
*ifp
,
334 struct nbr_connected
*ifc
)
337 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
340 zclient_create_header(s
, cmd
, ifp
->vrf_id
);
341 stream_putl(s
, ifp
->ifindex
);
343 /* Prefix information. */
345 stream_putc(s
, p
->family
);
346 blen
= prefix_blen(p
);
347 stream_put(s
, &p
->u
.prefix
, blen
);
350 * XXX gnu version does not send prefixlen for
351 * ZEBRA_INTERFACE_ADDRESS_DELETE
352 * but zebra_interface_address_delete_read() in the gnu version
355 stream_putc(s
, p
->prefixlen
);
357 /* Write packet size. */
358 stream_putw_at(s
, 0, stream_get_endp(s
));
360 return zebra_server_send_message(client
, s
);
363 /* Interface address addition. */
364 static void zebra_interface_nbr_address_add_update(struct interface
*ifp
,
365 struct nbr_connected
*ifc
)
367 struct listnode
*node
, *nnode
;
368 struct zserv
*client
;
371 if (IS_ZEBRA_DEBUG_EVENT
) {
372 char buf
[INET6_ADDRSTRLEN
];
376 "MESSAGE: ZEBRA_INTERFACE_NBR_ADDRESS_ADD %s/%d on %s",
377 inet_ntop(p
->family
, &p
->u
.prefix
, buf
,
379 p
->prefixlen
, ifc
->ifp
->name
);
382 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, node
, nnode
, client
))
383 zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_ADD
,
387 /* Interface address deletion. */
388 static void zebra_interface_nbr_address_delete_update(struct interface
*ifp
,
389 struct nbr_connected
*ifc
)
391 struct listnode
*node
, *nnode
;
392 struct zserv
*client
;
395 if (IS_ZEBRA_DEBUG_EVENT
) {
396 char buf
[INET6_ADDRSTRLEN
];
400 "MESSAGE: ZEBRA_INTERFACE_NBR_ADDRESS_DELETE %s/%d on %s",
401 inet_ntop(p
->family
, &p
->u
.prefix
, buf
,
403 p
->prefixlen
, ifc
->ifp
->name
);
406 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, node
, nnode
, client
))
407 zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
,
411 /* Send addresses on interface to client */
412 int zsend_interface_addresses(struct zserv
*client
, struct interface
*ifp
)
414 struct listnode
*cnode
, *cnnode
;
416 struct nbr_connected
*nc
;
418 /* Send interface addresses. */
419 for (ALL_LIST_ELEMENTS(ifp
->connected
, cnode
, cnnode
, c
)) {
420 if (!CHECK_FLAG(c
->conf
, ZEBRA_IFC_REAL
))
423 if (zsend_interface_address(ZEBRA_INTERFACE_ADDRESS_ADD
, client
,
429 /* Send interface neighbors. */
430 for (ALL_LIST_ELEMENTS(ifp
->nbr_connected
, cnode
, cnnode
, nc
)) {
431 if (zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_ADD
,
440 /* Notify client about interface moving from one VRF to another.
441 * Whether client is interested in old and new VRF is checked by caller.
443 int zsend_interface_vrf_update(struct zserv
*client
, struct interface
*ifp
,
446 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
448 zclient_create_header(s
, ZEBRA_INTERFACE_VRF_UPDATE
, ifp
->vrf_id
);
450 /* Fill in the ifIndex of the interface and its new VRF (id) */
451 stream_putl(s
, ifp
->ifindex
);
452 stream_putl(s
, vrf_id
);
454 /* Write packet size. */
455 stream_putw_at(s
, 0, stream_get_endp(s
));
457 client
->if_vrfchg_cnt
++;
458 return zebra_server_send_message(client
, s
);
461 /* Add new nbr connected IPv6 address */
462 void nbr_connected_add_ipv6(struct interface
*ifp
, struct in6_addr
*address
)
464 struct nbr_connected
*ifc
;
468 IPV6_ADDR_COPY(&p
.u
.prefix
, address
);
469 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
471 if (!(ifc
= listnode_head(ifp
->nbr_connected
))) {
473 ifc
= nbr_connected_new();
474 ifc
->address
= prefix_new();
476 listnode_add(ifp
->nbr_connected
, ifc
);
479 prefix_copy(ifc
->address
, &p
);
481 zebra_interface_nbr_address_add_update(ifp
, ifc
);
483 if_nbr_ipv6ll_to_ipv4ll_neigh_update(ifp
, address
, 1);
486 void nbr_connected_delete_ipv6(struct interface
*ifp
, struct in6_addr
*address
)
488 struct nbr_connected
*ifc
;
492 IPV6_ADDR_COPY(&p
.u
.prefix
, address
);
493 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
495 ifc
= nbr_connected_check(ifp
, &p
);
499 listnode_delete(ifp
->nbr_connected
, ifc
);
501 zebra_interface_nbr_address_delete_update(ifp
, ifc
);
503 if_nbr_ipv6ll_to_ipv4ll_neigh_update(ifp
, address
, 0);
505 nbr_connected_free(ifc
);
509 * The cmd passed to zsend_interface_update may be ZEBRA_INTERFACE_UP or
510 * ZEBRA_INTERFACE_DOWN.
512 * The ZEBRA_INTERFACE_UP message is sent from the zebra server to
513 * the clients in one of 2 situations:
514 * - an if_up is detected e.g., as a result of an RTM_IFINFO message
515 * - a vty command modifying the bandwidth of an interface is received.
516 * The ZEBRA_INTERFACE_DOWN message is sent when an if_down is detected.
518 int zsend_interface_update(int cmd
, struct zserv
*client
, struct interface
*ifp
)
520 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
522 zclient_create_header(s
, cmd
, ifp
->vrf_id
);
523 zserv_encode_interface(s
, ifp
);
525 if (cmd
== ZEBRA_INTERFACE_UP
)
528 client
->ifdown_cnt
++;
530 return zebra_server_send_message(client
, s
);
533 int zsend_redistribute_route(int cmd
, struct zserv
*client
, struct prefix
*p
,
534 struct prefix
*src_p
, struct route_entry
*re
)
536 struct zapi_route api
;
537 struct zapi_nexthop
*api_nh
;
538 struct nexthop
*nexthop
;
541 memset(&api
, 0, sizeof(api
));
542 api
.vrf_id
= re
->vrf_id
;
544 api
.instance
= re
->instance
;
545 api
.flags
= re
->flags
;
550 SET_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
);
551 memcpy(&api
.src_prefix
, src_p
, sizeof(api
.src_prefix
));
555 if (re
->nexthop_active_num
) {
556 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
557 api
.nexthop_num
= re
->nexthop_active_num
;
559 for (nexthop
= re
->ng
.nexthop
; nexthop
; nexthop
= nexthop
->next
) {
560 if (!CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
563 api_nh
= &api
.nexthops
[count
];
564 api_nh
->vrf_id
= nexthop
->vrf_id
;
565 api_nh
->type
= nexthop
->type
;
566 switch (nexthop
->type
) {
567 case NEXTHOP_TYPE_BLACKHOLE
:
568 api_nh
->bh_type
= nexthop
->bh_type
;
570 case NEXTHOP_TYPE_IPV4
:
571 api_nh
->gate
.ipv4
= nexthop
->gate
.ipv4
;
573 case NEXTHOP_TYPE_IPV4_IFINDEX
:
574 api_nh
->gate
.ipv4
= nexthop
->gate
.ipv4
;
575 api_nh
->ifindex
= nexthop
->ifindex
;
577 case NEXTHOP_TYPE_IFINDEX
:
578 api_nh
->ifindex
= nexthop
->ifindex
;
580 case NEXTHOP_TYPE_IPV6
:
581 api_nh
->gate
.ipv6
= nexthop
->gate
.ipv6
;
583 case NEXTHOP_TYPE_IPV6_IFINDEX
:
584 api_nh
->gate
.ipv6
= nexthop
->gate
.ipv6
;
585 api_nh
->ifindex
= nexthop
->ifindex
;
591 SET_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
);
592 api
.distance
= re
->distance
;
593 SET_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
);
594 api
.metric
= re
->metric
;
596 SET_FLAG(api
.message
, ZAPI_MESSAGE_TAG
);
599 SET_FLAG(api
.message
, ZAPI_MESSAGE_MTU
);
602 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
604 /* Encode route and send. */
605 if (zapi_route_encode(cmd
, s
, &api
) < 0) {
610 if (IS_ZEBRA_DEBUG_SEND
) {
611 char buf_prefix
[PREFIX_STRLEN
];
612 prefix2str(&api
.prefix
, buf_prefix
, sizeof(buf_prefix
));
614 zlog_debug("%s: %s to client %s: type %s, vrf_id %d, p %s",
615 __func__
, zserv_command_string(cmd
),
616 zebra_route_string(client
->proto
),
617 zebra_route_string(api
.type
), api
.vrf_id
,
620 return zebra_server_send_message(client
, s
);
624 * Modified version of zsend_ipv4_nexthop_lookup(): Query unicast rib if
625 * nexthop is not found on mrib. Returns both route metric and protocol
628 static int zsend_ipv4_nexthop_lookup_mrib(struct zserv
*client
,
630 struct route_entry
*re
,
631 struct zebra_vrf
*zvrf
)
636 struct nexthop
*nexthop
;
638 /* Get output stream. */
639 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
642 /* Fill in result. */
643 zclient_create_header(s
, ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB
, zvrf_id(zvrf
));
644 stream_put_in_addr(s
, &addr
);
647 stream_putc(s
, re
->distance
);
648 stream_putl(s
, re
->metric
);
650 nump
= stream_get_endp(
651 s
); /* remember position for nexthop_num */
652 stream_putc(s
, 0); /* reserve room for nexthop_num */
653 /* Only non-recursive routes are elegible to resolve the nexthop
655 * are looking up. Therefore, we will just iterate over the top
656 * chain of nexthops. */
657 for (nexthop
= re
->ng
.nexthop
; nexthop
; nexthop
= nexthop
->next
)
658 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
659 num
+= zserv_encode_nexthop(s
, nexthop
);
661 stream_putc_at(s
, nump
, num
); /* store nexthop_num */
663 stream_putc(s
, 0); /* distance */
664 stream_putl(s
, 0); /* metric */
665 stream_putc(s
, 0); /* nexthop_num */
668 stream_putw_at(s
, 0, stream_get_endp(s
));
670 return zebra_server_send_message(client
, s
);
673 int zsend_route_notify_owner(struct route_entry
*re
, struct prefix
*p
,
674 enum zapi_route_notify_owner note
)
676 struct zserv
*client
;
680 client
= zebra_find_client(re
->type
, re
->instance
);
681 if (!client
|| !client
->notify_owner
) {
682 if (IS_ZEBRA_DEBUG_PACKET
) {
683 char buff
[PREFIX_STRLEN
];
686 "Not Notifying Owner: %u about prefix %s(%u) %d vrf: %u",
687 re
->type
, prefix2str(p
, buff
, sizeof(buff
)),
688 re
->table
, note
, re
->vrf_id
);
693 if (IS_ZEBRA_DEBUG_PACKET
) {
694 char buff
[PREFIX_STRLEN
];
696 zlog_debug("Notifying Owner: %u about prefix %s(%u) %d vrf: %u",
697 re
->type
, prefix2str(p
, buff
, sizeof(buff
)),
698 re
->table
, note
, re
->vrf_id
);
701 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
704 zclient_create_header(s
, ZEBRA_ROUTE_NOTIFY_OWNER
, re
->vrf_id
);
706 stream_put(s
, ¬e
, sizeof(note
));
708 stream_putc(s
, p
->family
);
710 blen
= prefix_blen(p
);
711 stream_putc(s
, p
->prefixlen
);
712 stream_put(s
, &p
->u
.prefix
, blen
);
714 stream_putl(s
, re
->table
);
716 stream_putw_at(s
, 0, stream_get_endp(s
));
718 return zebra_server_send_message(client
, s
);
721 void zsend_rule_notify_owner(struct zebra_pbr_rule
*rule
,
722 enum zapi_rule_notify_owner note
)
724 struct listnode
*node
;
725 struct zserv
*client
;
728 if (IS_ZEBRA_DEBUG_PACKET
) {
729 zlog_debug("%s: Notifying %u", __PRETTY_FUNCTION__
,
733 for (ALL_LIST_ELEMENTS_RO(zebrad
.client_list
, node
, client
)) {
734 if (rule
->sock
== client
->sock
)
741 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
744 zclient_create_header(s
, ZEBRA_RULE_NOTIFY_OWNER
, VRF_DEFAULT
);
745 stream_put(s
, ¬e
, sizeof(note
));
746 stream_putl(s
, rule
->seq
);
747 stream_putl(s
, rule
->priority
);
748 stream_putl(s
, rule
->unique
);
750 stream_putl(s
, rule
->ifp
->ifindex
);
754 stream_putw_at(s
, 0, stream_get_endp(s
));
756 zebra_server_send_message(client
, s
);
759 /* Router-id is updated. Send ZEBRA_ROUTER_ID_ADD to client. */
760 int zsend_router_id_update(struct zserv
*client
, struct prefix
*p
,
765 /* Check this client need interface information. */
766 if (!vrf_bitmap_check(client
->ridinfo
, vrf_id
))
769 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
772 zclient_create_header(s
, ZEBRA_ROUTER_ID_UPDATE
, vrf_id
);
774 /* Prefix information. */
775 stream_putc(s
, p
->family
);
776 blen
= prefix_blen(p
);
777 stream_put(s
, &p
->u
.prefix
, blen
);
778 stream_putc(s
, p
->prefixlen
);
780 /* Write packet size. */
781 stream_putw_at(s
, 0, stream_get_endp(s
));
783 return zebra_server_send_message(client
, s
);
787 * Function used by Zebra to send a PW status update to LDP daemon
789 int zsend_pw_update(struct zserv
*client
, struct zebra_pw
*pw
)
791 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
793 zclient_create_header(s
, ZEBRA_PW_STATUS_UPDATE
, pw
->vrf_id
);
794 stream_write(s
, pw
->ifname
, IF_NAMESIZE
);
795 stream_putl(s
, pw
->ifindex
);
796 stream_putl(s
, pw
->status
);
798 /* Put length at the first point of the stream. */
799 stream_putw_at(s
, 0, stream_get_endp(s
));
801 return zebra_server_send_message(client
, s
);
804 /* Send response to a get label chunk request to client */
805 static int zsend_assign_label_chunk_response(struct zserv
*client
,
807 struct label_manager_chunk
*lmc
)
810 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
812 zclient_create_header(s
, ZEBRA_GET_LABEL_CHUNK
, vrf_id
);
816 stream_putc(s
, lmc
->keep
);
817 /* start and end labels */
818 stream_putl(s
, lmc
->start
);
819 stream_putl(s
, lmc
->end
);
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
));
830 /* Send response to a label manager connect request to client */
831 static int zsend_label_manager_connect_response(struct zserv
*client
,
833 unsigned short result
)
836 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
838 zclient_create_header(s
, ZEBRA_LABEL_MANAGER_CONNECT
, vrf_id
);
841 stream_putc(s
, result
);
843 /* Write packet size. */
844 stream_putw_at(s
, 0, stream_get_endp(s
));
846 ret
= writen(client
->sock
, s
->data
, stream_get_endp(s
));
852 /* Inbound message handling ------------------------------------------------ */
855 [ZEBRA_NEXTHOP_REGISTER
] = RNH_NEXTHOP_TYPE
,
856 [ZEBRA_NEXTHOP_UNREGISTER
] = RNH_NEXTHOP_TYPE
,
857 [ZEBRA_IMPORT_ROUTE_REGISTER
] = RNH_IMPORT_CHECK_TYPE
,
858 [ZEBRA_IMPORT_ROUTE_UNREGISTER
] = RNH_IMPORT_CHECK_TYPE
,
861 /* Nexthop register */
862 static void zread_rnh_register(ZAPI_HANDLER_ARGS
)
867 unsigned short l
= 0;
869 uint16_t type
= cmd2type
[hdr
->command
];
871 if (IS_ZEBRA_DEBUG_NHT
)
873 "rnh_register msg from client %s: hdr->length=%d, type=%s vrf=%u\n",
874 zebra_route_string(client
->proto
), hdr
->length
,
875 (type
== RNH_NEXTHOP_TYPE
) ? "nexthop" : "route",
880 client
->nh_reg_time
= monotime(NULL
);
882 while (l
< hdr
->length
) {
883 STREAM_GETC(s
, flags
);
884 STREAM_GETW(s
, p
.family
);
885 STREAM_GETC(s
, p
.prefixlen
);
887 if (p
.family
== AF_INET
) {
888 if (p
.prefixlen
> IPV4_MAX_BITLEN
) {
890 "%s: Specified prefix hdr->length %d is too large for a v4 address",
891 __PRETTY_FUNCTION__
, p
.prefixlen
);
894 STREAM_GET(&p
.u
.prefix4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
895 l
+= IPV4_MAX_BYTELEN
;
896 } else if (p
.family
== AF_INET6
) {
897 if (p
.prefixlen
> IPV6_MAX_BITLEN
) {
899 "%s: Specified prefix hdr->length %d is to large for a v6 address",
900 __PRETTY_FUNCTION__
, p
.prefixlen
);
903 STREAM_GET(&p
.u
.prefix6
, s
, IPV6_MAX_BYTELEN
);
904 l
+= IPV6_MAX_BYTELEN
;
907 "rnh_register: Received unknown family type %d\n",
911 rnh
= zebra_add_rnh(&p
, zvrf_id(zvrf
), type
);
912 if (type
== RNH_NEXTHOP_TYPE
) {
914 && !CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
))
915 SET_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
);
917 && CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
))
918 UNSET_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
);
919 } else if (type
== RNH_IMPORT_CHECK_TYPE
) {
921 && !CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
))
922 SET_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
);
924 && CHECK_FLAG(rnh
->flags
,
925 ZEBRA_NHT_EXACT_MATCH
))
926 UNSET_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
);
929 zebra_add_rnh_client(rnh
, client
, type
, zvrf_id(zvrf
));
930 /* Anything not AF_INET/INET6 has been filtered out above */
931 zebra_evaluate_rnh(zvrf_id(zvrf
), p
.family
, 1, type
, &p
);
938 /* Nexthop register */
939 static void zread_rnh_unregister(ZAPI_HANDLER_ARGS
)
944 unsigned short l
= 0;
945 uint16_t type
= cmd2type
[hdr
->command
];
947 if (IS_ZEBRA_DEBUG_NHT
)
949 "rnh_unregister msg from client %s: hdr->length=%d vrf: %u\n",
950 zebra_route_string(client
->proto
), hdr
->length
,
955 while (l
< hdr
->length
) {
958 STREAM_GETC(s
, flags
);
962 STREAM_GETW(s
, p
.family
);
963 STREAM_GETC(s
, p
.prefixlen
);
965 if (p
.family
== AF_INET
) {
966 if (p
.prefixlen
> IPV4_MAX_BITLEN
) {
968 "%s: Specified prefix hdr->length %d is to large for a v4 address",
969 __PRETTY_FUNCTION__
, p
.prefixlen
);
972 STREAM_GET(&p
.u
.prefix4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
973 l
+= IPV4_MAX_BYTELEN
;
974 } else if (p
.family
== AF_INET6
) {
975 if (p
.prefixlen
> IPV6_MAX_BITLEN
) {
977 "%s: Specified prefix hdr->length %d is to large for a v6 address",
978 __PRETTY_FUNCTION__
, p
.prefixlen
);
981 STREAM_GET(&p
.u
.prefix6
, s
, IPV6_MAX_BYTELEN
);
982 l
+= IPV6_MAX_BYTELEN
;
985 "rnh_register: Received unknown family type %d\n",
989 rnh
= zebra_lookup_rnh(&p
, zvrf_id(zvrf
), type
);
991 client
->nh_dereg_time
= monotime(NULL
);
992 zebra_remove_rnh_client(rnh
, client
, type
);
999 #define ZEBRA_MIN_FEC_LENGTH 5
1002 static void zread_fec_register(ZAPI_HANDLER_ARGS
)
1005 unsigned short l
= 0;
1008 uint32_t label_index
= MPLS_INVALID_LABEL_INDEX
;
1011 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1013 return; // unexpected
1016 * The minimum amount of data that can be sent for one fec
1019 if (hdr
->length
< ZEBRA_MIN_FEC_LENGTH
) {
1021 "fec_register: Received a fec register of hdr->length %d, it is of insufficient size to properly decode",
1026 while (l
< hdr
->length
) {
1027 STREAM_GETW(s
, flags
);
1028 memset(&p
, 0, sizeof(p
));
1029 STREAM_GETW(s
, p
.family
);
1030 if (p
.family
!= AF_INET
&& p
.family
!= AF_INET6
) {
1032 "fec_register: Received unknown family type %d\n",
1036 STREAM_GETC(s
, p
.prefixlen
);
1037 if ((p
.family
== AF_INET
&& p
.prefixlen
> IPV4_MAX_BITLEN
)
1038 || (p
.family
== AF_INET6
1039 && p
.prefixlen
> IPV6_MAX_BITLEN
)) {
1041 "%s: Specified prefix hdr->length: %d is to long for %d",
1042 __PRETTY_FUNCTION__
, p
.prefixlen
, p
.family
);
1046 STREAM_GET(&p
.u
.prefix
, s
, PSIZE(p
.prefixlen
));
1047 l
+= PSIZE(p
.prefixlen
);
1048 if (flags
& ZEBRA_FEC_REGISTER_LABEL_INDEX
) {
1049 STREAM_GETL(s
, label_index
);
1052 label_index
= MPLS_INVALID_LABEL_INDEX
;
1053 zebra_mpls_fec_register(zvrf
, &p
, label_index
, client
);
1060 /* FEC unregister */
1061 static void zread_fec_unregister(ZAPI_HANDLER_ARGS
)
1064 unsigned short l
= 0;
1069 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1071 return; // unexpected
1074 * The minimum amount of data that can be sent for one
1075 * fec unregistration
1077 if (hdr
->length
< ZEBRA_MIN_FEC_LENGTH
) {
1079 "fec_unregister: Received a fec unregister of hdr->length %d, it is of insufficient size to properly decode",
1084 while (l
< hdr
->length
) {
1085 STREAM_GETW(s
, flags
);
1087 goto stream_failure
;
1089 memset(&p
, 0, sizeof(p
));
1090 STREAM_GETW(s
, p
.family
);
1091 if (p
.family
!= AF_INET
&& p
.family
!= AF_INET6
) {
1093 "fec_unregister: Received unknown family type %d\n",
1097 STREAM_GETC(s
, p
.prefixlen
);
1098 if ((p
.family
== AF_INET
&& p
.prefixlen
> IPV4_MAX_BITLEN
)
1099 || (p
.family
== AF_INET6
1100 && p
.prefixlen
> IPV6_MAX_BITLEN
)) {
1102 "%s: Received prefix hdr->length %d which is greater than %d can support",
1103 __PRETTY_FUNCTION__
, p
.prefixlen
, p
.family
);
1107 STREAM_GET(&p
.u
.prefix
, s
, PSIZE(p
.prefixlen
));
1108 l
+= PSIZE(p
.prefixlen
);
1109 zebra_mpls_fec_unregister(zvrf
, &p
, client
);
1118 * Register zebra server interface information.
1119 * Send current all interface and address information.
1121 static void zread_interface_add(ZAPI_HANDLER_ARGS
)
1124 struct interface
*ifp
;
1126 /* Interface information is needed. */
1127 vrf_bitmap_set(client
->ifinfo
, zvrf_id(zvrf
));
1129 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
1130 FOR_ALL_INTERFACES (vrf
, ifp
) {
1131 /* Skip pseudo interface. */
1132 if (!CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_ACTIVE
))
1135 zsend_interface_add(client
, ifp
);
1136 zsend_interface_addresses(client
, ifp
);
1141 /* Unregister zebra server interface information. */
1142 static void zread_interface_delete(ZAPI_HANDLER_ARGS
)
1144 vrf_bitmap_unset(client
->ifinfo
, zvrf_id(zvrf
));
1147 void zserv_nexthop_num_warn(const char *caller
, const struct prefix
*p
,
1148 const unsigned int nexthop_num
)
1150 if (nexthop_num
> multipath_num
) {
1151 char buff
[PREFIX2STR_BUFFER
];
1152 prefix2str(p
, buff
, sizeof(buff
));
1154 "%s: Prefix %s has %d nexthops, but we can only use the first %d",
1155 caller
, buff
, nexthop_num
, multipath_num
);
1159 static void zread_route_add(ZAPI_HANDLER_ARGS
)
1162 struct zapi_route api
;
1163 struct zapi_nexthop
*api_nh
;
1165 struct prefix_ipv6
*src_p
= NULL
;
1166 struct route_entry
*re
;
1167 struct nexthop
*nexthop
= NULL
;
1169 vrf_id_t vrf_id
= 0;
1170 struct ipaddr vtep_ip
;
1173 if (zapi_route_decode(s
, &api
) < 0) {
1174 if (IS_ZEBRA_DEBUG_RECV
)
1175 zlog_debug("%s: Unable to decode zapi_route sent",
1176 __PRETTY_FUNCTION__
);
1180 if (IS_ZEBRA_DEBUG_RECV
) {
1181 char buf_prefix
[PREFIX_STRLEN
];
1182 prefix2str(&api
.prefix
, buf_prefix
, sizeof(buf_prefix
));
1183 zlog_debug("%s: p=%s, ZAPI_MESSAGE_LABEL: %sset, flags=0x%x",
1184 __func__
, buf_prefix
,
1185 (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_LABEL
) ? ""
1190 /* Allocate new route. */
1191 vrf_id
= zvrf_id(zvrf
);
1192 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
1193 re
->type
= api
.type
;
1194 re
->instance
= api
.instance
;
1195 re
->flags
= api
.flags
;
1196 re
->uptime
= time(NULL
);
1197 re
->vrf_id
= vrf_id
;
1198 if (api
.tableid
&& vrf_id
== VRF_DEFAULT
)
1199 re
->table
= api
.tableid
;
1201 re
->table
= zvrf
->table_id
;
1204 * TBD should _all_ of the nexthop add operations use
1205 * api_nh->vrf_id instead of re->vrf_id ? I only changed
1206 * for cases NEXTHOP_TYPE_IPV4 and NEXTHOP_TYPE_IPV6.
1208 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
)) {
1209 for (i
= 0; i
< api
.nexthop_num
; i
++) {
1210 api_nh
= &api
.nexthops
[i
];
1211 ifindex_t ifindex
= 0;
1213 if (IS_ZEBRA_DEBUG_RECV
) {
1214 zlog_debug("nh type %d", api_nh
->type
);
1217 switch (api_nh
->type
) {
1218 case NEXTHOP_TYPE_IFINDEX
:
1219 nexthop
= route_entry_nexthop_ifindex_add(
1220 re
, api_nh
->ifindex
, api_nh
->vrf_id
);
1222 case NEXTHOP_TYPE_IPV4
:
1223 if (IS_ZEBRA_DEBUG_RECV
) {
1224 char nhbuf
[INET6_ADDRSTRLEN
] = {0};
1225 inet_ntop(AF_INET
, &api_nh
->gate
.ipv4
,
1226 nhbuf
, INET6_ADDRSTRLEN
);
1227 zlog_debug("%s: nh=%s, vrf_id=%d",
1231 nexthop
= route_entry_nexthop_ipv4_add(
1232 re
, &api_nh
->gate
.ipv4
, NULL
,
1235 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1237 memset(&vtep_ip
, 0, sizeof(struct ipaddr
));
1238 if (CHECK_FLAG(api
.flags
,
1239 ZEBRA_FLAG_EVPN_ROUTE
)) {
1240 ifindex
= get_l3vni_svi_ifindex(vrf_id
);
1242 ifindex
= api_nh
->ifindex
;
1245 if (IS_ZEBRA_DEBUG_RECV
) {
1246 char nhbuf
[INET6_ADDRSTRLEN
] = {0};
1247 inet_ntop(AF_INET
, &api_nh
->gate
.ipv4
,
1248 nhbuf
, INET6_ADDRSTRLEN
);
1250 "%s: nh=%s, vrf_id=%d (re->vrf_id=%d), ifindex=%d",
1251 __func__
, nhbuf
, api_nh
->vrf_id
,
1252 re
->vrf_id
, ifindex
);
1254 nexthop
= route_entry_nexthop_ipv4_ifindex_add(
1255 re
, &api_nh
->gate
.ipv4
, NULL
, ifindex
,
1258 /* if this an EVPN route entry,
1259 * program the nh as neigh
1261 if (CHECK_FLAG(api
.flags
,
1262 ZEBRA_FLAG_EVPN_ROUTE
)) {
1263 SET_FLAG(nexthop
->flags
,
1264 NEXTHOP_FLAG_EVPN_RVTEP
);
1265 vtep_ip
.ipa_type
= IPADDR_V4
;
1266 memcpy(&(vtep_ip
.ipaddr_v4
),
1267 &(api_nh
->gate
.ipv4
),
1268 sizeof(struct in_addr
));
1269 zebra_vxlan_evpn_vrf_route_add(
1270 vrf_id
, &api
.rmac
, &vtep_ip
,
1274 case NEXTHOP_TYPE_IPV6
:
1275 nexthop
= route_entry_nexthop_ipv6_add(
1276 re
, &api_nh
->gate
.ipv6
, api_nh
->vrf_id
);
1278 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1279 memset(&vtep_ip
, 0, sizeof(struct ipaddr
));
1280 if (CHECK_FLAG(api
.flags
,
1281 ZEBRA_FLAG_EVPN_ROUTE
)) {
1283 get_l3vni_svi_ifindex(vrf_id
);
1285 ifindex
= api_nh
->ifindex
;
1288 nexthop
= route_entry_nexthop_ipv6_ifindex_add(
1289 re
, &api_nh
->gate
.ipv6
, ifindex
,
1292 /* if this an EVPN route entry,
1293 * program the nh as neigh
1295 if (CHECK_FLAG(api
.flags
,
1296 ZEBRA_FLAG_EVPN_ROUTE
)) {
1297 SET_FLAG(nexthop
->flags
,
1298 NEXTHOP_FLAG_EVPN_RVTEP
);
1299 vtep_ip
.ipa_type
= IPADDR_V6
;
1300 memcpy(&vtep_ip
.ipaddr_v6
,
1301 &(api_nh
->gate
.ipv6
),
1302 sizeof(struct in6_addr
));
1303 zebra_vxlan_evpn_vrf_route_add(
1310 case NEXTHOP_TYPE_BLACKHOLE
:
1311 nexthop
= route_entry_nexthop_blackhole_add(
1312 re
, api_nh
->bh_type
);
1318 "%s: Nexthops Specified: %d but we failed to properly create one",
1319 __PRETTY_FUNCTION__
, api
.nexthop_num
);
1320 nexthops_free(re
->ng
.nexthop
);
1321 XFREE(MTYPE_RE
, re
);
1324 /* MPLS labels for BGP-LU or Segment Routing */
1325 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_LABEL
)
1326 && api_nh
->type
!= NEXTHOP_TYPE_IFINDEX
1327 && api_nh
->type
!= NEXTHOP_TYPE_BLACKHOLE
) {
1328 enum lsp_types_t label_type
;
1331 lsp_type_from_re_type(client
->proto
);
1333 if (IS_ZEBRA_DEBUG_RECV
) {
1335 "%s: adding %d labels of type %d (1st=%u)",
1336 __func__
, api_nh
->label_num
,
1337 label_type
, api_nh
->labels
[0]);
1340 nexthop_add_labels(nexthop
, label_type
,
1342 &api_nh
->labels
[0]);
1347 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
))
1348 re
->distance
= api
.distance
;
1349 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
))
1350 re
->metric
= api
.metric
;
1351 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_TAG
))
1353 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_MTU
))
1356 afi
= family2afi(api
.prefix
.family
);
1357 if (afi
!= AFI_IP6
&& CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
)) {
1358 zlog_warn("%s: Received SRC Prefix but afi is not v6",
1359 __PRETTY_FUNCTION__
);
1360 nexthops_free(re
->ng
.nexthop
);
1361 XFREE(MTYPE_RE
, re
);
1364 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
))
1365 src_p
= &api
.src_prefix
;
1367 ret
= rib_add_multipath(afi
, api
.safi
, &api
.prefix
, src_p
, re
);
1370 switch (api
.prefix
.family
) {
1373 client
->v4_route_add_cnt
++;
1375 client
->v4_route_upd8_cnt
++;
1379 client
->v6_route_add_cnt
++;
1381 client
->v6_route_upd8_cnt
++;
1386 static void zread_route_del(ZAPI_HANDLER_ARGS
)
1389 struct zapi_route api
;
1391 struct prefix_ipv6
*src_p
= NULL
;
1395 if (zapi_route_decode(s
, &api
) < 0)
1398 afi
= family2afi(api
.prefix
.family
);
1399 if (afi
!= AFI_IP6
&& CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
)) {
1400 zlog_warn("%s: Received a src prefix while afi is not v6",
1401 __PRETTY_FUNCTION__
);
1404 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
))
1405 src_p
= &api
.src_prefix
;
1407 if (api
.vrf_id
== VRF_DEFAULT
&& api
.tableid
!= 0)
1408 table_id
= api
.tableid
;
1410 table_id
= zvrf
->table_id
;
1412 rib_delete(afi
, api
.safi
, zvrf_id(zvrf
), api
.type
, api
.instance
,
1413 api
.flags
, &api
.prefix
, src_p
, NULL
, table_id
,
1414 api
.metric
, false, &api
.rmac
);
1417 switch (api
.prefix
.family
) {
1419 client
->v4_route_del_cnt
++;
1422 client
->v6_route_del_cnt
++;
1427 /* This function support multiple nexthop. */
1429 * Parse the ZEBRA_IPV4_ROUTE_ADD sent from client. Update re and
1432 static void zread_ipv4_add(ZAPI_HANDLER_ARGS
)
1435 struct route_entry
*re
;
1438 struct in_addr nhop_addr
;
1439 uint8_t nexthop_num
;
1440 uint8_t nexthop_type
;
1445 enum lsp_types_t label_type
= ZEBRA_LSP_NONE
;
1447 struct nexthop
*nexthop
;
1448 enum blackhole_type bh_type
= BLACKHOLE_NULL
;
1450 /* Get input stream. */
1453 /* Allocate new re. */
1454 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
1456 /* Type, flags, message. */
1457 STREAM_GETC(s
, re
->type
);
1458 if (re
->type
> ZEBRA_ROUTE_MAX
) {
1459 zlog_warn("%s: Specified route type %d is not a legal value\n",
1460 __PRETTY_FUNCTION__
, re
->type
);
1461 XFREE(MTYPE_RE
, re
);
1464 STREAM_GETW(s
, re
->instance
);
1465 STREAM_GETL(s
, re
->flags
);
1466 STREAM_GETC(s
, message
);
1467 STREAM_GETW(s
, safi
);
1468 re
->uptime
= time(NULL
);
1471 memset(&p
, 0, sizeof(struct prefix_ipv4
));
1473 STREAM_GETC(s
, p
.prefixlen
);
1474 if (p
.prefixlen
> IPV4_MAX_BITLEN
) {
1476 "%s: Specified prefix length %d is greater than what v4 can be",
1477 __PRETTY_FUNCTION__
, p
.prefixlen
);
1478 XFREE(MTYPE_RE
, re
);
1481 STREAM_GET(&p
.u
.prefix4
, s
, PSIZE(p
.prefixlen
));
1484 re
->vrf_id
= zvrf_id(zvrf
);
1486 /* Nexthop parse. */
1487 if (CHECK_FLAG(message
, ZAPI_MESSAGE_NEXTHOP
)) {
1488 STREAM_GETC(s
, nexthop_num
);
1489 zserv_nexthop_num_warn(__func__
, (const struct prefix
*)&p
,
1492 if (CHECK_FLAG(message
, ZAPI_MESSAGE_LABEL
))
1493 label_type
= lsp_type_from_re_type(client
->proto
);
1495 for (i
= 0; i
< nexthop_num
; i
++) {
1496 STREAM_GETC(s
, nexthop_type
);
1498 switch (nexthop_type
) {
1499 case NEXTHOP_TYPE_IFINDEX
:
1500 STREAM_GETL(s
, ifindex
);
1501 route_entry_nexthop_ifindex_add(re
, ifindex
,
1504 case NEXTHOP_TYPE_IPV4
:
1505 STREAM_GET(&nhop_addr
.s_addr
, s
,
1507 nexthop
= route_entry_nexthop_ipv4_add(
1508 re
, &nhop_addr
, NULL
, re
->vrf_id
);
1509 /* For labeled-unicast, each nexthop is followed
1511 if (CHECK_FLAG(message
, ZAPI_MESSAGE_LABEL
)) {
1512 STREAM_GETL(s
, label
);
1513 nexthop_add_labels(nexthop
, label_type
,
1517 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1518 STREAM_GET(&nhop_addr
.s_addr
, s
,
1520 STREAM_GETL(s
, ifindex
);
1521 route_entry_nexthop_ipv4_ifindex_add(
1522 re
, &nhop_addr
, NULL
, ifindex
,
1525 case NEXTHOP_TYPE_IPV6
:
1527 "%s: Please use ZEBRA_ROUTE_ADD if you want to pass v6 nexthops",
1528 __PRETTY_FUNCTION__
);
1529 nexthops_free(re
->ng
.nexthop
);
1530 XFREE(MTYPE_RE
, re
);
1533 case NEXTHOP_TYPE_BLACKHOLE
:
1534 route_entry_nexthop_blackhole_add(re
, bh_type
);
1538 "%s: Specified nexthop type: %d does not exist",
1539 __PRETTY_FUNCTION__
, nexthop_type
);
1540 nexthops_free(re
->ng
.nexthop
);
1541 XFREE(MTYPE_RE
, re
);
1548 if (CHECK_FLAG(message
, ZAPI_MESSAGE_DISTANCE
))
1549 STREAM_GETC(s
, re
->distance
);
1552 if (CHECK_FLAG(message
, ZAPI_MESSAGE_METRIC
))
1553 STREAM_GETL(s
, re
->metric
);
1556 if (CHECK_FLAG(message
, ZAPI_MESSAGE_TAG
))
1557 STREAM_GETL(s
, re
->tag
);
1561 if (CHECK_FLAG(message
, ZAPI_MESSAGE_MTU
))
1562 STREAM_GETL(s
, re
->mtu
);
1567 re
->table
= zvrf
->table_id
;
1569 ret
= rib_add_multipath(AFI_IP
, safi
, &p
, NULL
, re
);
1573 client
->v4_route_add_cnt
++;
1575 client
->v4_route_upd8_cnt
++;
1580 nexthops_free(re
->ng
.nexthop
);
1581 XFREE(MTYPE_RE
, re
);
1584 /* Zebra server IPv4 prefix delete function. */
1585 static void zread_ipv4_delete(ZAPI_HANDLER_ARGS
)
1588 struct zapi_ipv4 api
;
1594 /* Type, flags, message. */
1595 STREAM_GETC(s
, api
.type
);
1596 STREAM_GETW(s
, api
.instance
);
1597 STREAM_GETL(s
, api
.flags
);
1598 STREAM_GETC(s
, api
.message
);
1599 STREAM_GETW(s
, api
.safi
);
1602 memset(&p
, 0, sizeof(struct prefix
));
1604 STREAM_GETC(s
, p
.prefixlen
);
1605 if (p
.prefixlen
> IPV4_MAX_BITLEN
) {
1606 zlog_warn("%s: Passed in prefixlen %d is impossible",
1607 __PRETTY_FUNCTION__
, p
.prefixlen
);
1610 STREAM_GET(&p
.u
.prefix4
, s
, PSIZE(p
.prefixlen
));
1612 table_id
= zvrf
->table_id
;
1614 rib_delete(AFI_IP
, api
.safi
, zvrf_id(zvrf
), api
.type
, api
.instance
,
1615 api
.flags
, &p
, NULL
, NULL
, table_id
, 0, false, NULL
);
1616 client
->v4_route_del_cnt
++;
1622 /* MRIB Nexthop lookup for IPv4. */
1623 static void zread_ipv4_nexthop_lookup_mrib(ZAPI_HANDLER_ARGS
)
1625 struct in_addr addr
;
1626 struct route_entry
*re
;
1628 STREAM_GET(&addr
.s_addr
, msg
, IPV4_MAX_BYTELEN
);
1629 re
= rib_match_ipv4_multicast(zvrf_id(zvrf
), addr
, NULL
);
1630 zsend_ipv4_nexthop_lookup_mrib(client
, addr
, re
, zvrf
);
1636 /* Zebra server IPv6 prefix add function. */
1637 static void zread_ipv4_route_ipv6_nexthop_add(ZAPI_HANDLER_ARGS
)
1641 struct in6_addr nhop_addr
;
1642 struct route_entry
*re
;
1644 uint8_t nexthop_num
;
1645 uint8_t nexthop_type
;
1648 static struct in6_addr nexthops
[MULTIPATH_NUM
];
1649 static unsigned int ifindices
[MULTIPATH_NUM
];
1651 static mpls_label_t labels
[MULTIPATH_NUM
];
1652 enum lsp_types_t label_type
= ZEBRA_LSP_NONE
;
1654 struct nexthop
*nexthop
;
1655 enum blackhole_type bh_type
= BLACKHOLE_NULL
;
1657 /* Get input stream. */
1660 memset(&nhop_addr
, 0, sizeof(struct in6_addr
));
1662 /* Allocate new re. */
1663 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
1665 /* Type, flags, message. */
1666 STREAM_GETC(s
, re
->type
);
1667 if (re
->type
> ZEBRA_ROUTE_MAX
) {
1668 zlog_warn("%s: Specified route type: %d is not a legal value\n",
1669 __PRETTY_FUNCTION__
, re
->type
);
1670 XFREE(MTYPE_RE
, re
);
1673 STREAM_GETW(s
, re
->instance
);
1674 STREAM_GETL(s
, re
->flags
);
1675 STREAM_GETC(s
, message
);
1676 STREAM_GETW(s
, safi
);
1677 re
->uptime
= time(NULL
);
1680 memset(&p
, 0, sizeof(struct prefix_ipv4
));
1682 STREAM_GETC(s
, p
.prefixlen
);
1683 if (p
.prefixlen
> IPV4_MAX_BITLEN
) {
1685 "%s: Prefix Length %d is greater than what a v4 address can use",
1686 __PRETTY_FUNCTION__
, p
.prefixlen
);
1687 XFREE(MTYPE_RE
, re
);
1690 STREAM_GET(&p
.u
.prefix4
, s
, PSIZE(p
.prefixlen
));
1693 re
->vrf_id
= zvrf_id(zvrf
);
1695 /* We need to give nh-addr, nh-ifindex with the same next-hop object
1696 * to the re to ensure that IPv6 multipathing works; need to coalesce
1697 * these. Clients should send the same number of paired set of
1698 * next-hop-addr/next-hop-ifindices. */
1699 if (CHECK_FLAG(message
, ZAPI_MESSAGE_NEXTHOP
)) {
1700 unsigned int nh_count
= 0;
1701 unsigned int if_count
= 0;
1702 unsigned int max_nh_if
= 0;
1704 STREAM_GETC(s
, nexthop_num
);
1705 zserv_nexthop_num_warn(__func__
, (const struct prefix
*)&p
,
1708 if (CHECK_FLAG(message
, ZAPI_MESSAGE_LABEL
))
1709 label_type
= lsp_type_from_re_type(client
->proto
);
1711 for (i
= 0; i
< nexthop_num
; i
++) {
1712 STREAM_GETC(s
, nexthop_type
);
1714 switch (nexthop_type
) {
1715 case NEXTHOP_TYPE_IPV6
:
1716 STREAM_GET(&nhop_addr
, s
, 16);
1717 if (nh_count
< MULTIPATH_NUM
) {
1718 /* For labeled-unicast, each nexthop is
1719 * followed by label. */
1720 if (CHECK_FLAG(message
,
1721 ZAPI_MESSAGE_LABEL
)) {
1722 STREAM_GETL(s
, label
);
1723 labels
[nh_count
] = label
;
1725 nexthops
[nh_count
] = nhop_addr
;
1729 case NEXTHOP_TYPE_IFINDEX
:
1730 if (if_count
< multipath_num
) {
1731 STREAM_GETL(s
, ifindices
[if_count
++]);
1734 case NEXTHOP_TYPE_BLACKHOLE
:
1735 route_entry_nexthop_blackhole_add(re
, bh_type
);
1739 "%s: Please use ZEBRA_ROUTE_ADD if you want to pass non v6 nexthops",
1740 __PRETTY_FUNCTION__
);
1741 nexthops_free(re
->ng
.nexthop
);
1742 XFREE(MTYPE_RE
, re
);
1747 max_nh_if
= (nh_count
> if_count
) ? nh_count
: if_count
;
1748 for (i
= 0; i
< max_nh_if
; i
++) {
1750 && !IN6_IS_ADDR_UNSPECIFIED(&nexthops
[i
])) {
1751 if ((i
< if_count
) && ifindices
[i
])
1753 route_entry_nexthop_ipv6_ifindex_add(
1758 nexthop
= route_entry_nexthop_ipv6_add(
1759 re
, &nexthops
[i
], re
->vrf_id
);
1761 if (CHECK_FLAG(message
, ZAPI_MESSAGE_LABEL
))
1762 nexthop_add_labels(nexthop
, label_type
,
1765 if ((i
< if_count
) && ifindices
[i
])
1766 route_entry_nexthop_ifindex_add(
1767 re
, ifindices
[i
], re
->vrf_id
);
1773 if (CHECK_FLAG(message
, ZAPI_MESSAGE_DISTANCE
))
1774 STREAM_GETC(s
, re
->distance
);
1777 if (CHECK_FLAG(message
, ZAPI_MESSAGE_METRIC
))
1778 STREAM_GETL(s
, re
->metric
);
1781 if (CHECK_FLAG(message
, ZAPI_MESSAGE_TAG
))
1782 STREAM_GETL(s
, re
->tag
);
1786 if (CHECK_FLAG(message
, ZAPI_MESSAGE_MTU
))
1787 STREAM_GETL(s
, re
->mtu
);
1792 re
->table
= zvrf
->table_id
;
1794 ret
= rib_add_multipath(AFI_IP6
, safi
, &p
, NULL
, re
);
1797 client
->v4_route_add_cnt
++;
1799 client
->v4_route_upd8_cnt
++;
1804 nexthops_free(re
->ng
.nexthop
);
1805 XFREE(MTYPE_RE
, re
);
1808 static void zread_ipv6_add(ZAPI_HANDLER_ARGS
)
1812 struct in6_addr nhop_addr
;
1814 struct route_entry
*re
;
1816 uint8_t nexthop_num
;
1817 uint8_t nexthop_type
;
1819 struct prefix_ipv6 src_p
, *src_pp
;
1821 static struct in6_addr nexthops
[MULTIPATH_NUM
];
1822 static unsigned int ifindices
[MULTIPATH_NUM
];
1824 static mpls_label_t labels
[MULTIPATH_NUM
];
1825 enum lsp_types_t label_type
= ZEBRA_LSP_NONE
;
1827 struct nexthop
*nexthop
;
1828 enum blackhole_type bh_type
= BLACKHOLE_NULL
;
1830 /* Get input stream. */
1833 memset(&nhop_addr
, 0, sizeof(struct in6_addr
));
1835 /* Allocate new re. */
1836 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
1838 /* Type, flags, message. */
1839 STREAM_GETC(s
, re
->type
);
1840 if (re
->type
> ZEBRA_ROUTE_MAX
) {
1841 zlog_warn("%s: Specified route type: %d is not a legal value\n",
1842 __PRETTY_FUNCTION__
, re
->type
);
1843 XFREE(MTYPE_RE
, re
);
1846 STREAM_GETW(s
, re
->instance
);
1847 STREAM_GETL(s
, re
->flags
);
1848 STREAM_GETC(s
, message
);
1849 STREAM_GETW(s
, safi
);
1850 re
->uptime
= time(NULL
);
1853 memset(&p
, 0, sizeof(p
));
1854 p
.family
= AF_INET6
;
1855 STREAM_GETC(s
, p
.prefixlen
);
1856 if (p
.prefixlen
> IPV6_MAX_BITLEN
) {
1858 "%s: Specified prefix length %d is to large for v6 prefix",
1859 __PRETTY_FUNCTION__
, p
.prefixlen
);
1860 XFREE(MTYPE_RE
, re
);
1863 STREAM_GET(&p
.u
.prefix6
, s
, PSIZE(p
.prefixlen
));
1865 if (CHECK_FLAG(message
, ZAPI_MESSAGE_SRCPFX
)) {
1866 memset(&src_p
, 0, sizeof(src_p
));
1867 src_p
.family
= AF_INET6
;
1868 STREAM_GETC(s
, src_p
.prefixlen
);
1869 if (src_p
.prefixlen
> IPV6_MAX_BITLEN
) {
1871 "%s: Specified src prefix length %d is to large for v6 prefix",
1872 __PRETTY_FUNCTION__
, src_p
.prefixlen
);
1873 XFREE(MTYPE_RE
, re
);
1876 STREAM_GET(&src_p
.prefix
, s
, PSIZE(src_p
.prefixlen
));
1882 re
->vrf_id
= zvrf_id(zvrf
);
1884 /* We need to give nh-addr, nh-ifindex with the same next-hop object
1885 * to the re to ensure that IPv6 multipathing works; need to coalesce
1886 * these. Clients should send the same number of paired set of
1887 * next-hop-addr/next-hop-ifindices. */
1888 if (CHECK_FLAG(message
, ZAPI_MESSAGE_NEXTHOP
)) {
1889 unsigned int nh_count
= 0;
1890 unsigned int if_count
= 0;
1891 unsigned int max_nh_if
= 0;
1893 STREAM_GETC(s
, nexthop_num
);
1894 zserv_nexthop_num_warn(__func__
, (const struct prefix
*)&p
,
1897 if (CHECK_FLAG(message
, ZAPI_MESSAGE_LABEL
))
1898 label_type
= lsp_type_from_re_type(client
->proto
);
1900 for (i
= 0; i
< nexthop_num
; i
++) {
1901 STREAM_GETC(s
, nexthop_type
);
1903 switch (nexthop_type
) {
1904 case NEXTHOP_TYPE_IPV6
:
1905 STREAM_GET(&nhop_addr
, s
, 16);
1906 if (nh_count
< MULTIPATH_NUM
) {
1907 /* For labeled-unicast, each nexthop is
1908 * followed by label. */
1909 if (CHECK_FLAG(message
,
1910 ZAPI_MESSAGE_LABEL
)) {
1911 STREAM_GETL(s
, label
);
1912 labels
[nh_count
] = label
;
1914 nexthops
[nh_count
++] = nhop_addr
;
1917 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1918 STREAM_GET(&nhop_addr
, s
, 16);
1919 STREAM_GETL(s
, ifindex
);
1920 route_entry_nexthop_ipv6_ifindex_add(
1921 re
, &nhop_addr
, ifindex
, re
->vrf_id
);
1923 case NEXTHOP_TYPE_IFINDEX
:
1924 if (if_count
< multipath_num
) {
1925 STREAM_GETL(s
, ifindices
[if_count
++]);
1928 case NEXTHOP_TYPE_BLACKHOLE
:
1929 route_entry_nexthop_blackhole_add(re
, bh_type
);
1933 "%s: Please use ZEBRA_ROUTE_ADD if you want to pass non v6 nexthops",
1934 __PRETTY_FUNCTION__
);
1935 nexthops_free(re
->ng
.nexthop
);
1936 XFREE(MTYPE_RE
, re
);
1941 max_nh_if
= (nh_count
> if_count
) ? nh_count
: if_count
;
1942 for (i
= 0; i
< max_nh_if
; i
++) {
1944 && !IN6_IS_ADDR_UNSPECIFIED(&nexthops
[i
])) {
1945 if ((i
< if_count
) && ifindices
[i
])
1947 route_entry_nexthop_ipv6_ifindex_add(
1952 nexthop
= route_entry_nexthop_ipv6_add(
1953 re
, &nexthops
[i
], re
->vrf_id
);
1954 if (CHECK_FLAG(message
, ZAPI_MESSAGE_LABEL
))
1955 nexthop_add_labels(nexthop
, label_type
,
1958 if ((i
< if_count
) && ifindices
[i
])
1959 route_entry_nexthop_ifindex_add(
1960 re
, ifindices
[i
], re
->vrf_id
);
1966 if (CHECK_FLAG(message
, ZAPI_MESSAGE_DISTANCE
))
1967 STREAM_GETC(s
, re
->distance
);
1970 if (CHECK_FLAG(message
, ZAPI_MESSAGE_METRIC
))
1971 STREAM_GETL(s
, re
->metric
);
1974 if (CHECK_FLAG(message
, ZAPI_MESSAGE_TAG
))
1975 STREAM_GETL(s
, re
->tag
);
1979 if (CHECK_FLAG(message
, ZAPI_MESSAGE_MTU
))
1980 STREAM_GETL(s
, re
->mtu
);
1984 re
->table
= zvrf
->table_id
;
1986 ret
= rib_add_multipath(AFI_IP6
, safi
, &p
, src_pp
, re
);
1989 client
->v6_route_add_cnt
++;
1991 client
->v6_route_upd8_cnt
++;
1996 nexthops_free(re
->ng
.nexthop
);
1997 XFREE(MTYPE_RE
, re
);
2000 /* Zebra server IPv6 prefix delete function. */
2001 static void zread_ipv6_delete(ZAPI_HANDLER_ARGS
)
2004 struct zapi_ipv6 api
;
2006 struct prefix_ipv6 src_p
, *src_pp
;
2010 /* Type, flags, message. */
2011 STREAM_GETC(s
, api
.type
);
2012 STREAM_GETW(s
, api
.instance
);
2013 STREAM_GETL(s
, api
.flags
);
2014 STREAM_GETC(s
, api
.message
);
2015 STREAM_GETW(s
, api
.safi
);
2018 memset(&p
, 0, sizeof(struct prefix
));
2019 p
.family
= AF_INET6
;
2020 STREAM_GETC(s
, p
.prefixlen
);
2021 STREAM_GET(&p
.u
.prefix6
, s
, PSIZE(p
.prefixlen
));
2023 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
)) {
2024 memset(&src_p
, 0, sizeof(struct prefix_ipv6
));
2025 src_p
.family
= AF_INET6
;
2026 STREAM_GETC(s
, src_p
.prefixlen
);
2027 STREAM_GET(&src_p
.prefix
, s
, PSIZE(src_p
.prefixlen
));
2032 rib_delete(AFI_IP6
, api
.safi
, zvrf_id(zvrf
), api
.type
, api
.instance
,
2033 api
.flags
, &p
, src_pp
, NULL
, client
->rtm_table
, 0, false,
2036 client
->v6_route_del_cnt
++;
2042 /* Register zebra server router-id information. Send current router-id */
2043 static void zread_router_id_add(ZAPI_HANDLER_ARGS
)
2047 /* Router-id information is needed. */
2048 vrf_bitmap_set(client
->ridinfo
, zvrf_id(zvrf
));
2050 router_id_get(&p
, zvrf_id(zvrf
));
2052 zsend_router_id_update(client
, &p
, zvrf_id(zvrf
));
2055 /* Unregister zebra server router-id information. */
2056 static void zread_router_id_delete(ZAPI_HANDLER_ARGS
)
2058 vrf_bitmap_unset(client
->ridinfo
, zvrf_id(zvrf
));
2061 /* Tie up route-type and client->sock */
2062 static void zread_hello(ZAPI_HANDLER_ARGS
)
2064 /* type of protocol (lib/zebra.h) */
2066 unsigned short instance
;
2069 STREAM_GETC(msg
, proto
);
2070 STREAM_GETW(msg
, instance
);
2071 STREAM_GETC(msg
, notify
);
2073 client
->notify_owner
= true;
2075 /* accept only dynamic routing protocols */
2076 if ((proto
< ZEBRA_ROUTE_MAX
) && (proto
> ZEBRA_ROUTE_STATIC
)) {
2078 "client %d says hello and bids fair to announce only %s routes vrf=%u",
2079 client
->sock
, zebra_route_string(proto
),
2082 zlog_notice("client protocol instance %d", instance
);
2084 client
->proto
= proto
;
2085 client
->instance
= instance
;
2092 /* Unregister all information in a VRF. */
2093 static void zread_vrf_unregister(ZAPI_HANDLER_ARGS
)
2098 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
2099 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
2100 vrf_bitmap_unset(client
->redist
[afi
][i
], zvrf_id(zvrf
));
2101 vrf_bitmap_unset(client
->redist_default
, zvrf_id(zvrf
));
2102 vrf_bitmap_unset(client
->ifinfo
, zvrf_id(zvrf
));
2103 vrf_bitmap_unset(client
->ridinfo
, zvrf_id(zvrf
));
2106 static void zread_mpls_labels(ZAPI_HANDLER_ARGS
)
2109 enum lsp_types_t type
;
2110 struct prefix prefix
;
2111 enum nexthop_types_t gtype
;
2114 mpls_label_t in_label
, out_label
;
2117 /* Get input stream. */
2121 STREAM_GETC(s
, type
);
2122 STREAM_GETL(s
, prefix
.family
);
2123 switch (prefix
.family
) {
2125 STREAM_GET(&prefix
.u
.prefix4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
2126 STREAM_GETC(s
, prefix
.prefixlen
);
2127 if (prefix
.prefixlen
> IPV4_MAX_BITLEN
) {
2129 "%s: Specified prefix length %d is greater than a v4 address can support",
2130 __PRETTY_FUNCTION__
, prefix
.prefixlen
);
2133 STREAM_GET(&gate
.ipv4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
2136 STREAM_GET(&prefix
.u
.prefix6
, s
, 16);
2137 STREAM_GETC(s
, prefix
.prefixlen
);
2138 if (prefix
.prefixlen
> IPV6_MAX_BITLEN
) {
2140 "%s: Specified prefix length %d is greater than a v6 address can support",
2141 __PRETTY_FUNCTION__
, prefix
.prefixlen
);
2144 STREAM_GET(&gate
.ipv6
, s
, 16);
2147 zlog_warn("%s: Specified AF %d is not supported for this call",
2148 __PRETTY_FUNCTION__
, prefix
.family
);
2151 STREAM_GETL(s
, ifindex
);
2152 STREAM_GETC(s
, distance
);
2153 STREAM_GETL(s
, in_label
);
2154 STREAM_GETL(s
, out_label
);
2156 switch (prefix
.family
) {
2159 gtype
= NEXTHOP_TYPE_IPV4_IFINDEX
;
2161 gtype
= NEXTHOP_TYPE_IPV4
;
2165 gtype
= NEXTHOP_TYPE_IPV6_IFINDEX
;
2167 gtype
= NEXTHOP_TYPE_IPV6
;
2176 if (hdr
->command
== ZEBRA_MPLS_LABELS_ADD
) {
2177 mpls_lsp_install(zvrf
, type
, in_label
, out_label
, gtype
, &gate
,
2179 mpls_ftn_update(1, zvrf
, type
, &prefix
, gtype
, &gate
, ifindex
,
2180 distance
, out_label
);
2181 } else if (hdr
->command
== ZEBRA_MPLS_LABELS_DELETE
) {
2182 mpls_lsp_uninstall(zvrf
, type
, in_label
, gtype
, &gate
, ifindex
);
2183 mpls_ftn_update(0, zvrf
, type
, &prefix
, gtype
, &gate
, ifindex
,
2184 distance
, out_label
);
2190 static int zsend_table_manager_connect_response(struct zserv
*client
,
2191 vrf_id_t vrf_id
, uint16_t result
)
2193 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
2195 zclient_create_header(s
, ZEBRA_TABLE_MANAGER_CONNECT
, vrf_id
);
2198 stream_putc(s
, result
);
2200 stream_putw_at(s
, 0, stream_get_endp(s
));
2202 return zebra_server_send_message(client
, s
);
2205 /* Send response to a table manager connect request to client */
2206 static void zread_table_manager_connect(struct zserv
*client
,
2217 STREAM_GETC(s
, proto
);
2218 STREAM_GETW(s
, instance
);
2220 /* accept only dynamic routing protocols */
2221 if ((proto
>= ZEBRA_ROUTE_MAX
) || (proto
<= ZEBRA_ROUTE_STATIC
)) {
2222 zlog_err("client %d has wrong protocol %s", client
->sock
,
2223 zebra_route_string(proto
));
2224 zsend_table_manager_connect_response(client
, vrf_id
, 1);
2227 zlog_notice("client %d with vrf %u instance %u connected as %s",
2228 client
->sock
, vrf_id
, instance
, zebra_route_string(proto
));
2229 client
->proto
= proto
;
2230 client
->instance
= instance
;
2233 * Release previous labels of same protocol and instance.
2234 * This is done in case it restarted from an unexpected shutdown.
2236 release_daemon_table_chunks(proto
, instance
);
2238 zsend_table_manager_connect_response(client
, vrf_id
, 0);
2244 static void zread_label_manager_connect(struct zserv
*client
,
2245 struct stream
*msg
, vrf_id_t vrf_id
)
2248 /* type of protocol (lib/zebra.h) */
2250 unsigned short instance
;
2252 /* Get input stream. */
2256 STREAM_GETC(s
, proto
);
2257 STREAM_GETW(s
, instance
);
2259 /* accept only dynamic routing protocols */
2260 if ((proto
>= ZEBRA_ROUTE_MAX
) || (proto
<= ZEBRA_ROUTE_STATIC
)) {
2261 zlog_err("client %d has wrong protocol %s", client
->sock
,
2262 zebra_route_string(proto
));
2263 zsend_label_manager_connect_response(client
, vrf_id
, 1);
2266 zlog_notice("client %d with vrf %u instance %u connected as %s",
2267 client
->sock
, vrf_id
, instance
, zebra_route_string(proto
));
2268 client
->proto
= proto
;
2269 client
->instance
= instance
;
2272 Release previous labels of same protocol and instance.
2273 This is done in case it restarted from an unexpected shutdown.
2275 release_daemon_label_chunks(proto
, instance
);
2278 " Label Manager client connected: sock %d, proto %s, vrf %u instance %u",
2279 client
->sock
, zebra_route_string(proto
), vrf_id
, instance
);
2280 /* send response back */
2281 zsend_label_manager_connect_response(client
, vrf_id
, 0);
2287 static void zread_get_label_chunk(struct zserv
*client
, struct stream
*msg
,
2293 struct label_manager_chunk
*lmc
;
2295 /* Get input stream. */
2299 STREAM_GETC(s
, keep
);
2300 STREAM_GETL(s
, size
);
2302 lmc
= assign_label_chunk(client
->proto
, client
->instance
, keep
, size
);
2304 zlog_err("%s: Unable to assign Label Chunk of size %u",
2307 zlog_debug("Assigned Label Chunk %u - %u to %u", lmc
->start
,
2309 /* send response back */
2310 zsend_assign_label_chunk_response(client
, vrf_id
, lmc
);
2316 static void zread_release_label_chunk(struct zserv
*client
, struct stream
*msg
)
2319 uint32_t start
, end
;
2321 /* Get input stream. */
2325 STREAM_GETL(s
, start
);
2326 STREAM_GETL(s
, end
);
2328 release_label_chunk(client
->proto
, client
->instance
, start
, end
);
2333 static void zread_label_manager_request(ZAPI_HANDLER_ARGS
)
2335 /* to avoid sending other messages like ZERBA_INTERFACE_UP */
2336 if (hdr
->command
== ZEBRA_LABEL_MANAGER_CONNECT
)
2337 client
->is_synchronous
= 1;
2339 /* external label manager */
2341 zread_relay_label_manager_request(hdr
->command
, client
,
2343 /* this is a label manager */
2345 if (hdr
->command
== ZEBRA_LABEL_MANAGER_CONNECT
)
2346 zread_label_manager_connect(client
, msg
, zvrf_id(zvrf
));
2348 /* Sanity: don't allow 'unidentified' requests */
2349 if (!client
->proto
) {
2351 "Got label request from an unidentified client");
2354 if (hdr
->command
== ZEBRA_GET_LABEL_CHUNK
)
2355 zread_get_label_chunk(client
, msg
,
2357 else if (hdr
->command
== ZEBRA_RELEASE_LABEL_CHUNK
)
2358 zread_release_label_chunk(client
, msg
);
2363 /* Send response to a get table chunk request to client */
2364 static int zsend_assign_table_chunk_response(struct zserv
*client
,
2366 struct table_manager_chunk
*tmc
)
2368 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
2370 zclient_create_header(s
, ZEBRA_GET_TABLE_CHUNK
, vrf_id
);
2373 /* start and end labels */
2374 stream_putl(s
, tmc
->start
);
2375 stream_putl(s
, tmc
->end
);
2378 /* Write packet size. */
2379 stream_putw_at(s
, 0, stream_get_endp(s
));
2381 return zebra_server_send_message(client
, s
);
2384 static void zread_get_table_chunk(struct zserv
*client
, struct stream
*msg
,
2389 struct table_manager_chunk
*tmc
;
2391 /* Get input stream. */
2395 STREAM_GETL(s
, size
);
2397 tmc
= assign_table_chunk(client
->proto
, client
->instance
, size
);
2399 zlog_err("%s: Unable to assign Table Chunk of size %u",
2402 zlog_debug("Assigned Table Chunk %u - %u", tmc
->start
,
2404 /* send response back */
2405 zsend_assign_table_chunk_response(client
, vrf_id
, tmc
);
2411 static void zread_release_table_chunk(struct zserv
*client
, struct stream
*msg
)
2414 uint32_t start
, end
;
2416 /* Get input stream. */
2420 STREAM_GETL(s
, start
);
2421 STREAM_GETL(s
, end
);
2423 release_table_chunk(client
->proto
, client
->instance
, start
, end
);
2429 static void zread_table_manager_request(ZAPI_HANDLER_ARGS
)
2431 /* to avoid sending other messages like ZERBA_INTERFACE_UP */
2432 if (hdr
->command
== ZEBRA_TABLE_MANAGER_CONNECT
)
2433 zread_table_manager_connect(client
, msg
, zvrf_id(zvrf
));
2435 /* Sanity: don't allow 'unidentified' requests */
2436 if (!client
->proto
) {
2438 "Got table request from an unidentified client");
2441 if (hdr
->command
== ZEBRA_GET_TABLE_CHUNK
)
2442 zread_get_table_chunk(client
, msg
,
2444 else if (hdr
->command
== ZEBRA_RELEASE_TABLE_CHUNK
)
2445 zread_release_table_chunk(client
, msg
);
2449 static void zread_pseudowire(ZAPI_HANDLER_ARGS
)
2452 char ifname
[IF_NAMESIZE
];
2456 union g_addr nexthop
;
2457 uint32_t local_label
;
2458 uint32_t remote_label
;
2460 union pw_protocol_fields data
;
2462 struct zebra_pw
*pw
;
2464 /* Get input stream. */
2468 STREAM_GET(ifname
, s
, IF_NAMESIZE
);
2469 STREAM_GETL(s
, ifindex
);
2470 STREAM_GETL(s
, type
);
2474 STREAM_GET(&nexthop
.ipv4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
2477 STREAM_GET(&nexthop
.ipv6
, s
, 16);
2482 STREAM_GETL(s
, local_label
);
2483 STREAM_GETL(s
, remote_label
);
2484 STREAM_GETC(s
, flags
);
2485 STREAM_GET(&data
, s
, sizeof(data
));
2486 protocol
= client
->proto
;
2488 pw
= zebra_pw_find(zvrf
, ifname
);
2489 switch (hdr
->command
) {
2492 zlog_warn("%s: pseudowire %s already exists [%s]",
2494 zserv_command_string(hdr
->command
));
2498 zebra_pw_add(zvrf
, ifname
, protocol
, client
);
2500 case ZEBRA_PW_DELETE
:
2502 zlog_warn("%s: pseudowire %s not found [%s]", __func__
,
2503 ifname
, zserv_command_string(hdr
->command
));
2507 zebra_pw_del(zvrf
, pw
);
2510 case ZEBRA_PW_UNSET
:
2512 zlog_warn("%s: pseudowire %s not found [%s]", __func__
,
2513 ifname
, zserv_command_string(hdr
->command
));
2517 switch (hdr
->command
) {
2521 case ZEBRA_PW_UNSET
:
2526 zebra_pw_change(pw
, ifindex
, type
, af
, &nexthop
, local_label
,
2527 remote_label
, flags
, &data
);
2535 /* Cleanup registered nexthops (across VRFs) upon client disconnect. */
2536 static void zebra_client_close_cleanup_rnh(struct zserv
*client
)
2539 struct zebra_vrf
*zvrf
;
2541 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
2542 if ((zvrf
= vrf
->info
) != NULL
) {
2543 zebra_cleanup_rnh_client(zvrf_id(zvrf
), AF_INET
, client
,
2545 zebra_cleanup_rnh_client(zvrf_id(zvrf
), AF_INET6
,
2546 client
, RNH_NEXTHOP_TYPE
);
2547 zebra_cleanup_rnh_client(zvrf_id(zvrf
), AF_INET
, client
,
2548 RNH_IMPORT_CHECK_TYPE
);
2549 zebra_cleanup_rnh_client(zvrf_id(zvrf
), AF_INET6
,
2550 client
, RNH_IMPORT_CHECK_TYPE
);
2551 if (client
->proto
== ZEBRA_ROUTE_LDP
) {
2552 hash_iterate(zvrf
->lsp_table
,
2553 mpls_ldp_lsp_uninstall_all
,
2555 mpls_ldp_ftn_uninstall_all(zvrf
, AFI_IP
);
2556 mpls_ldp_ftn_uninstall_all(zvrf
, AFI_IP6
);
2562 static void zread_interface_set_master(ZAPI_HANDLER_ARGS
)
2564 struct interface
*master
;
2565 struct interface
*slave
;
2566 struct stream
*s
= msg
;
2570 STREAM_GETL(s
, vrf_id
);
2571 STREAM_GETL(s
, ifindex
);
2572 master
= if_lookup_by_index(ifindex
, vrf_id
);
2574 STREAM_GETL(s
, vrf_id
);
2575 STREAM_GETL(s
, ifindex
);
2576 slave
= if_lookup_by_index(ifindex
, vrf_id
);
2578 if (!master
|| !slave
)
2581 kernel_interface_set_master(master
, slave
);
2588 static void zread_vrf_label(ZAPI_HANDLER_ARGS
)
2590 struct interface
*ifp
;
2591 mpls_label_t nlabel
;
2594 struct zebra_vrf
*def_zvrf
;
2595 enum lsp_types_t ltype
;
2598 STREAM_GETL(s
, nlabel
);
2599 STREAM_GETC(s
, afi
);
2600 if (nlabel
== zvrf
->label
[afi
]) {
2602 * Nothing to do here move along
2607 STREAM_GETC(s
, ltype
);
2609 if (zvrf
->vrf
->vrf_id
!= VRF_DEFAULT
)
2610 ifp
= if_lookup_by_name(zvrf
->vrf
->name
, zvrf
->vrf
->vrf_id
);
2612 ifp
= if_lookup_by_name("lo", VRF_DEFAULT
);
2615 zlog_debug("Unable to find specified Interface for %s",
2620 def_zvrf
= zebra_vrf_lookup_by_id(VRF_DEFAULT
);
2622 if (zvrf
->label
[afi
] != MPLS_LABEL_NONE
) {
2626 really_remove
= true;
2627 for (scrubber
= AFI_IP
; scrubber
< AFI_MAX
; scrubber
++) {
2628 if (scrubber
== afi
)
2631 if (zvrf
->label
[scrubber
] == MPLS_LABEL_NONE
)
2634 if (zvrf
->label
[afi
] == zvrf
->label
[scrubber
]) {
2635 really_remove
= false;
2641 mpls_lsp_uninstall(def_zvrf
, ltype
, zvrf
->label
[afi
],
2642 NEXTHOP_TYPE_IFINDEX
, NULL
,
2646 if (nlabel
!= MPLS_LABEL_NONE
)
2647 mpls_lsp_install(def_zvrf
, ltype
, nlabel
,
2648 MPLS_LABEL_IMPLICIT_NULL
, NEXTHOP_TYPE_IFINDEX
,
2649 NULL
, ifp
->ifindex
);
2651 zvrf
->label
[afi
] = nlabel
;
2656 static inline void zread_rule(ZAPI_HANDLER_ARGS
)
2658 struct zebra_pbr_rule zpr
;
2664 STREAM_GETL(s
, total
);
2666 for (i
= 0; i
< total
; i
++) {
2667 memset(&zpr
, 0, sizeof(zpr
));
2669 zpr
.sock
= client
->sock
;
2670 STREAM_GETL(s
, zpr
.seq
);
2671 STREAM_GETL(s
, zpr
.priority
);
2672 STREAM_GETL(s
, zpr
.unique
);
2673 STREAM_GETC(s
, zpr
.filter
.src_ip
.family
);
2674 STREAM_GETC(s
, zpr
.filter
.src_ip
.prefixlen
);
2675 STREAM_GET(&zpr
.filter
.src_ip
.u
.prefix
, s
,
2676 prefix_blen(&zpr
.filter
.src_ip
));
2677 STREAM_GETW(s
, zpr
.filter
.src_port
);
2678 STREAM_GETC(s
, zpr
.filter
.dst_ip
.family
);
2679 STREAM_GETC(s
, zpr
.filter
.dst_ip
.prefixlen
);
2680 STREAM_GET(&zpr
.filter
.dst_ip
.u
.prefix
, s
,
2681 prefix_blen(&zpr
.filter
.dst_ip
));
2682 STREAM_GETW(s
, zpr
.filter
.dst_port
);
2683 STREAM_GETL(s
, zpr
.action
.table
);
2684 STREAM_GETL(s
, ifindex
);
2686 zpr
.ifp
= if_lookup_by_index(ifindex
, VRF_UNKNOWN
);
2688 zlog_debug("FAiled to lookup ifindex: %u", ifindex
);
2692 if (!is_default_prefix(&zpr
.filter
.src_ip
))
2693 zpr
.filter
.filter_bm
|= PBR_FILTER_SRC_IP
;
2695 if (!is_default_prefix(&zpr
.filter
.dst_ip
))
2696 zpr
.filter
.filter_bm
|= PBR_FILTER_DST_IP
;
2698 if (zpr
.filter
.src_port
)
2699 zpr
.filter
.filter_bm
|= PBR_FILTER_SRC_PORT
;
2701 if (zpr
.filter
.dst_port
)
2702 zpr
.filter
.filter_bm
|= PBR_FILTER_DST_PORT
;
2704 if (hdr
->command
== ZEBRA_RULE_ADD
)
2705 zebra_pbr_add_rule(zvrf
->zns
, &zpr
);
2707 zebra_pbr_del_rule(zvrf
->zns
, &zpr
);
2714 void (*zserv_handlers
[])(ZAPI_HANDLER_ARGS
) = {
2715 [ZEBRA_ROUTER_ID_ADD
] = zread_router_id_add
,
2716 [ZEBRA_ROUTER_ID_DELETE
] = zread_router_id_delete
,
2717 [ZEBRA_INTERFACE_ADD
] = zread_interface_add
,
2718 [ZEBRA_INTERFACE_DELETE
] = zread_interface_delete
,
2719 [ZEBRA_ROUTE_ADD
] = zread_route_add
,
2720 [ZEBRA_ROUTE_DELETE
] = zread_route_del
,
2721 [ZEBRA_IPV4_ROUTE_ADD
] = zread_ipv4_add
,
2722 [ZEBRA_IPV4_ROUTE_DELETE
] = zread_ipv4_delete
,
2723 [ZEBRA_IPV4_ROUTE_IPV6_NEXTHOP_ADD
] = zread_ipv4_route_ipv6_nexthop_add
,
2724 [ZEBRA_IPV6_ROUTE_ADD
] = zread_ipv6_add
,
2725 [ZEBRA_IPV6_ROUTE_DELETE
] = zread_ipv6_delete
,
2726 [ZEBRA_REDISTRIBUTE_ADD
] = zebra_redistribute_add
,
2727 [ZEBRA_REDISTRIBUTE_DELETE
] = zebra_redistribute_delete
,
2728 [ZEBRA_REDISTRIBUTE_DEFAULT_ADD
] = zebra_redistribute_default_add
,
2729 [ZEBRA_REDISTRIBUTE_DEFAULT_DELETE
] = zebra_redistribute_default_delete
,
2730 [ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB
] = zread_ipv4_nexthop_lookup_mrib
,
2731 [ZEBRA_HELLO
] = zread_hello
,
2732 [ZEBRA_NEXTHOP_REGISTER
] = zread_rnh_register
,
2733 [ZEBRA_NEXTHOP_UNREGISTER
] = zread_rnh_unregister
,
2734 [ZEBRA_IMPORT_ROUTE_REGISTER
] = zread_rnh_register
,
2735 [ZEBRA_IMPORT_ROUTE_UNREGISTER
] = zread_rnh_unregister
,
2736 [ZEBRA_BFD_DEST_UPDATE
] = zebra_ptm_bfd_dst_register
,
2737 [ZEBRA_BFD_DEST_REGISTER
] = zebra_ptm_bfd_dst_register
,
2738 [ZEBRA_BFD_DEST_DEREGISTER
] = zebra_ptm_bfd_dst_deregister
,
2739 [ZEBRA_VRF_UNREGISTER
] = zread_vrf_unregister
,
2740 [ZEBRA_VRF_LABEL
] = zread_vrf_label
,
2741 [ZEBRA_BFD_CLIENT_REGISTER
] = zebra_ptm_bfd_client_register
,
2742 #if defined(HAVE_RTADV)
2743 [ZEBRA_INTERFACE_ENABLE_RADV
] = zebra_interface_radv_enable
,
2744 [ZEBRA_INTERFACE_DISABLE_RADV
] = zebra_interface_radv_disable
,
2746 [ZEBRA_INTERFACE_ENABLE_RADV
] = NULL
,
2747 [ZEBRA_INTERFACE_DISABLE_RADV
] = NULL
,
2749 [ZEBRA_MPLS_LABELS_ADD
] = zread_mpls_labels
,
2750 [ZEBRA_MPLS_LABELS_DELETE
] = zread_mpls_labels
,
2751 [ZEBRA_IPMR_ROUTE_STATS
] = zebra_ipmr_route_stats
,
2752 [ZEBRA_LABEL_MANAGER_CONNECT
] = zread_label_manager_request
,
2753 [ZEBRA_GET_LABEL_CHUNK
] = zread_label_manager_request
,
2754 [ZEBRA_RELEASE_LABEL_CHUNK
] = zread_label_manager_request
,
2755 [ZEBRA_FEC_REGISTER
] = zread_fec_register
,
2756 [ZEBRA_FEC_UNREGISTER
] = zread_fec_unregister
,
2757 [ZEBRA_ADVERTISE_DEFAULT_GW
] = zebra_vxlan_advertise_gw_macip
,
2758 [ZEBRA_ADVERTISE_SUBNET
] = zebra_vxlan_advertise_subnet
,
2759 [ZEBRA_ADVERTISE_ALL_VNI
] = zebra_vxlan_advertise_all_vni
,
2760 [ZEBRA_REMOTE_VTEP_ADD
] = zebra_vxlan_remote_vtep_add
,
2761 [ZEBRA_REMOTE_VTEP_DEL
] = zebra_vxlan_remote_vtep_del
,
2762 [ZEBRA_REMOTE_MACIP_ADD
] = zebra_vxlan_remote_macip_add
,
2763 [ZEBRA_REMOTE_MACIP_DEL
] = zebra_vxlan_remote_macip_del
,
2764 [ZEBRA_INTERFACE_SET_MASTER
] = zread_interface_set_master
,
2765 [ZEBRA_PW_ADD
] = zread_pseudowire
,
2766 [ZEBRA_PW_DELETE
] = zread_pseudowire
,
2767 [ZEBRA_PW_SET
] = zread_pseudowire
,
2768 [ZEBRA_PW_UNSET
] = zread_pseudowire
,
2769 [ZEBRA_RULE_ADD
] = zread_rule
,
2770 [ZEBRA_RULE_DELETE
] = zread_rule
,
2771 [ZEBRA_TABLE_MANAGER_CONNECT
] = zread_table_manager_request
,
2772 [ZEBRA_GET_TABLE_CHUNK
] = zread_table_manager_request
,
2773 [ZEBRA_RELEASE_TABLE_CHUNK
] = zread_table_manager_request
,
2776 static inline void zserv_handle_commands(struct zserv
*client
,
2777 struct zmsghdr
*hdr
,
2779 struct zebra_vrf
*zvrf
)
2781 if (hdr
->command
> array_size(zserv_handlers
)
2782 || zserv_handlers
[hdr
->command
] == NULL
)
2783 zlog_info("Zebra received unknown command %d", hdr
->command
);
2785 zserv_handlers
[hdr
->command
](client
, hdr
, msg
, zvrf
);
2790 /* Lifecycle ---------------------------------------------------------------- */
2792 /* free zebra client information. */
2793 static void zebra_client_free(struct zserv
*client
)
2795 /* Send client de-registration to BFD */
2796 zebra_ptm_bfd_client_deregister(client
->proto
);
2798 /* Cleanup any rules installed from this client */
2799 zebra_pbr_client_close_cleanup(client
->sock
);
2801 /* Cleanup any registered nexthops - across all VRFs. */
2802 zebra_client_close_cleanup_rnh(client
);
2804 /* Release Label Manager chunks */
2805 release_daemon_label_chunks(client
->proto
, client
->instance
);
2807 /* Release Table Manager chunks */
2808 release_daemon_table_chunks(client
->proto
, client
->instance
);
2810 /* Cleanup any FECs registered by this client. */
2811 zebra_mpls_cleanup_fecs_for_client(vrf_info_lookup(VRF_DEFAULT
),
2814 /* Remove pseudowires associated with this client */
2815 zebra_pw_client_close(client
);
2817 /* Close file descriptor. */
2819 unsigned long nroutes
;
2821 close(client
->sock
);
2822 nroutes
= rib_score_proto(client
->proto
, client
->instance
);
2824 "client %d disconnected. %lu %s routes removed from the rib",
2825 client
->sock
, nroutes
,
2826 zebra_route_string(client
->proto
));
2830 /* Free stream buffers. */
2831 if (client
->ibuf_work
)
2832 stream_free(client
->ibuf_work
);
2833 if (client
->obuf_work
)
2834 stream_free(client
->obuf_work
);
2835 if (client
->ibuf_fifo
)
2836 stream_fifo_free(client
->ibuf_fifo
);
2837 if (client
->obuf_fifo
)
2838 stream_fifo_free(client
->obuf_fifo
);
2840 buffer_free(client
->wb
);
2842 /* Release threads. */
2844 thread_cancel(client
->t_read
);
2845 if (client
->t_write
)
2846 thread_cancel(client
->t_write
);
2847 if (client
->t_suicide
)
2848 thread_cancel(client
->t_suicide
);
2851 for (afi_t afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
2852 for (int i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
2853 vrf_bitmap_free(client
->redist
[afi
][i
]);
2855 vrf_bitmap_free(client
->redist_default
);
2856 vrf_bitmap_free(client
->ifinfo
);
2857 vrf_bitmap_free(client
->ridinfo
);
2859 XFREE(MTYPE_TMP
, client
);
2863 * Called from client thread to terminate itself.
2865 static void zebra_client_close(struct zserv
*client
)
2867 listnode_delete(zebrad
.client_list
, client
);
2868 zebra_client_free(client
);
2871 /* Make new client. */
2872 static void zebra_client_create(int sock
)
2874 struct zserv
*client
;
2878 client
= XCALLOC(MTYPE_TMP
, sizeof(struct zserv
));
2880 /* Make client input/output buffer. */
2881 client
->sock
= sock
;
2882 client
->ibuf_fifo
= stream_fifo_new();
2883 client
->obuf_fifo
= stream_fifo_new();
2884 client
->ibuf_work
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
2885 client
->obuf_work
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
2886 client
->wb
= buffer_new(0);
2888 /* Set table number. */
2889 client
->rtm_table
= zebrad
.rtm_table_default
;
2891 client
->connect_time
= monotime(NULL
);
2892 /* Initialize flags */
2893 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
2894 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
2895 client
->redist
[afi
][i
] = vrf_bitmap_init();
2896 client
->redist_default
= vrf_bitmap_init();
2897 client
->ifinfo
= vrf_bitmap_init();
2898 client
->ridinfo
= vrf_bitmap_init();
2900 /* by default, it's not a synchronous client */
2901 client
->is_synchronous
= 0;
2903 /* Add this client to linked list. */
2904 listnode_add(zebrad
.client_list
, client
);
2906 zebra_vrf_update_all(client
);
2908 /* start read loop */
2909 zebra_event(client
, ZEBRA_READ
);
2912 static int zserv_delayed_close(struct thread
*thread
)
2914 struct zserv
*client
= THREAD_ARG(thread
);
2916 client
->t_suicide
= NULL
;
2917 zebra_client_close(client
);
2922 * Log zapi message to zlog.
2931 * The message header
2933 static void zserv_log_message(const char *errmsg
, struct stream
*msg
,
2934 struct zmsghdr
*hdr
)
2936 zlog_debug("Rx'd ZAPI message");
2938 zlog_debug("%s", errmsg
);
2940 zlog_debug(" Length: %d", hdr
->length
);
2941 zlog_debug("Command: %s", zserv_command_string(hdr
->command
));
2942 zlog_debug(" VRF: %u", hdr
->vrf_id
);
2944 zlog_hexdump(msg
->data
, STREAM_READABLE(msg
));
2947 static int zserv_flush_data(struct thread
*thread
)
2949 struct zserv
*client
= THREAD_ARG(thread
);
2951 client
->t_write
= NULL
;
2952 if (client
->t_suicide
) {
2953 zebra_client_close(client
);
2956 switch (buffer_flush_available(client
->wb
, client
->sock
)) {
2959 "%s: buffer_flush_available failed on zserv client fd %d, closing",
2960 __func__
, client
->sock
);
2961 zebra_client_close(client
);
2964 case BUFFER_PENDING
:
2965 client
->t_write
= NULL
;
2966 thread_add_write(zebrad
.master
, zserv_flush_data
, client
,
2967 client
->sock
, &client
->t_write
);
2974 client
->last_write_time
= monotime(NULL
);
2979 * Write a single packet.
2981 static int zserv_write(struct thread
*thread
)
2983 struct zserv
*client
= THREAD_ARG(thread
);
2987 if (client
->t_suicide
)
2990 if (client
->is_synchronous
)
2993 msg
= stream_fifo_pop(client
->obuf_fifo
);
2994 stream_set_getp(msg
, 0);
2995 client
->last_write_cmd
= stream_getw_from(msg
, 6);
2997 writerv
= buffer_write(client
->wb
, client
->sock
, STREAM_DATA(msg
),
2998 stream_get_endp(msg
));
3005 "%s: buffer_write failed to zserv client fd %d, closing",
3006 __func__
, client
->sock
);
3008 * Schedule a delayed close since many of the functions that
3009 * call this one do not check the return code. They do not
3010 * allow for the possibility that an I/O error may have caused
3011 * the client to be deleted.
3013 client
->t_suicide
= NULL
;
3014 thread_add_event(zebrad
.master
, zserv_delayed_close
, client
, 0,
3015 &client
->t_suicide
);
3018 THREAD_OFF(client
->t_write
);
3020 case BUFFER_PENDING
:
3021 thread_add_write(zebrad
.master
, zserv_flush_data
, client
,
3022 client
->sock
, &client
->t_write
);
3026 if (client
->obuf_fifo
->count
)
3027 zebra_event(client
, ZEBRA_WRITE
);
3029 client
->last_write_time
= monotime(NULL
);
3033 #if defined(HANDLE_ZAPI_FUZZING)
3034 static void zserv_write_incoming(struct stream
*orig
, uint16_t command
)
3036 char fname
[MAXPATHLEN
];
3037 struct stream
*copy
;
3040 copy
= stream_dup(orig
);
3041 stream_set_getp(copy
, 0);
3043 zserv_privs
.change(ZPRIVS_RAISE
);
3044 snprintf(fname
, MAXPATHLEN
, "%s/%u", DAEMON_VTY_DIR
, command
);
3045 fd
= open(fname
, O_CREAT
| O_WRONLY
| O_EXCL
, 0644);
3046 stream_flush(copy
, fd
);
3048 zserv_privs
.change(ZPRIVS_LOWER
);
3053 static int zserv_process_messages(struct thread
*thread
)
3055 struct zserv
*client
= THREAD_ARG(thread
);
3056 struct zebra_vrf
*zvrf
;
3062 msg
= stream_fifo_pop(client
->ibuf_fifo
);
3064 /* break if out of messages */
3068 /* read & check header */
3069 hdrvalid
= zapi_parse_header(msg
, &hdr
);
3070 if (!hdrvalid
&& IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
) {
3071 const char *emsg
= "Message has corrupt header";
3072 zserv_log_message(emsg
, msg
, NULL
);
3077 hdr
.length
-= ZEBRA_HEADER_SIZE
;
3079 zvrf
= zebra_vrf_lookup_by_id(hdr
.vrf_id
);
3080 if (!zvrf
&& IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
) {
3081 const char *emsg
= "Message specifies unknown VRF";
3082 zserv_log_message(emsg
, msg
, &hdr
);
3087 /* process commands */
3088 zserv_handle_commands(client
, &hdr
, msg
, zvrf
);
3095 /* Handler of zebra service request. */
3096 static int zserv_read(struct thread
*thread
)
3099 struct zserv
*client
;
3101 #if defined(HANDLE_ZAPI_FUZZING)
3104 int packets
= zebrad
.packets_to_process
;
3106 /* Get thread data. Reset reading thread because I'm running. */
3107 sock
= THREAD_FD(thread
);
3108 client
= THREAD_ARG(thread
);
3110 if (client
->t_suicide
) {
3111 zebra_client_close(client
);
3121 already
= stream_get_endp(client
->ibuf_work
);
3123 /* Read length and command (if we don't have it already). */
3124 if (already
< ZEBRA_HEADER_SIZE
) {
3125 nb
= stream_read_try(client
->ibuf_work
, sock
,
3126 ZEBRA_HEADER_SIZE
- already
);
3127 if ((nb
== 0 || nb
== -1) && IS_ZEBRA_DEBUG_EVENT
)
3128 zlog_debug("connection closed socket [%d]",
3130 if ((nb
== 0 || nb
== -1))
3132 if (nb
!= (ssize_t
)(ZEBRA_HEADER_SIZE
- already
)) {
3133 /* Try again later. */
3136 already
= ZEBRA_HEADER_SIZE
;
3139 /* Reset to read from the beginning of the incoming packet. */
3140 stream_set_getp(client
->ibuf_work
, 0);
3142 /* Fetch header values */
3143 hdrvalid
= zapi_parse_header(client
->ibuf_work
, &hdr
);
3146 snprintf(errmsg
, sizeof(errmsg
),
3147 "%s: Message has corrupt header", __func__
);
3148 zserv_log_message(errmsg
, client
->ibuf_work
, NULL
);
3152 /* Validate header */
3153 if (hdr
.marker
!= ZEBRA_HEADER_MARKER
3154 || hdr
.version
!= ZSERV_VERSION
) {
3156 errmsg
, sizeof(errmsg
),
3157 "Message has corrupt header\n%s: socket %d version mismatch, marker %d, version %d",
3158 __func__
, sock
, hdr
.marker
, hdr
.version
);
3159 zserv_log_message(errmsg
, client
->ibuf_work
, &hdr
);
3162 if (hdr
.length
< ZEBRA_HEADER_SIZE
) {
3164 errmsg
, sizeof(errmsg
),
3165 "Message has corrupt header\n%s: socket %d message length %u is less than header size %d",
3166 __func__
, sock
, hdr
.length
, ZEBRA_HEADER_SIZE
);
3167 zserv_log_message(errmsg
, client
->ibuf_work
, &hdr
);
3170 if (hdr
.length
> STREAM_SIZE(client
->ibuf_work
)) {
3172 errmsg
, sizeof(errmsg
),
3173 "Message has corrupt header\n%s: socket %d message length %u exceeds buffer size %lu",
3174 __func__
, sock
, hdr
.length
,
3175 (unsigned long)STREAM_SIZE(client
->ibuf_work
));
3179 /* Read rest of data. */
3180 if (already
< hdr
.length
) {
3181 nb
= stream_read_try(client
->ibuf_work
, sock
,
3182 hdr
.length
- already
);
3183 if ((nb
== 0 || nb
== -1) && IS_ZEBRA_DEBUG_EVENT
)
3185 "connection closed [%d] when reading zebra data",
3187 if ((nb
== 0 || nb
== -1))
3189 if (nb
!= (ssize_t
)(hdr
.length
- already
)) {
3190 /* Try again later. */
3195 #if defined(HANDLE_ZAPI_FUZZING)
3196 zserv_write_incoming(client
->ibuf_work
, command
);
3199 /* Debug packet information. */
3200 if (IS_ZEBRA_DEBUG_EVENT
)
3201 zlog_debug("zebra message comes from socket [%d]",
3204 if (IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
)
3205 zserv_log_message(NULL
, client
->ibuf_work
, &hdr
);
3207 client
->last_read_time
= monotime(NULL
);
3208 client
->last_read_cmd
= hdr
.command
;
3210 stream_set_getp(client
->ibuf_work
, 0);
3211 struct stream
*msg
= stream_dup(client
->ibuf_work
);
3213 stream_fifo_push(client
->ibuf_fifo
, msg
);
3215 if (client
->t_suicide
)
3219 stream_reset(client
->ibuf_work
);
3222 if (IS_ZEBRA_DEBUG_PACKET
)
3223 zlog_debug("Read %d packets",
3224 zebrad
.packets_to_process
- packets
);
3226 /* Schedule job to process those packets */
3227 thread_add_event(zebrad
.master
, &zserv_process_messages
, client
, 0,
3230 /* Reschedule ourselves */
3231 zebra_event(client
, ZEBRA_READ
);
3236 zebra_client_close(client
);
3240 static void zebra_event(struct zserv
*client
, enum event event
)
3244 thread_add_read(zebrad
.master
, zserv_read
, client
, client
->sock
,
3248 thread_add_write(zebrad
.master
, zserv_write
, client
,
3249 client
->sock
, &client
->t_write
);
3254 /* Accept code of zebra server socket. */
3255 static int zebra_accept(struct thread
*thread
)
3259 struct sockaddr_in client
;
3262 accept_sock
= THREAD_FD(thread
);
3264 /* Reregister myself. */
3265 thread_add_read(zebrad
.master
, zebra_accept
, NULL
, accept_sock
, NULL
);
3267 len
= sizeof(struct sockaddr_in
);
3268 client_sock
= accept(accept_sock
, (struct sockaddr
*)&client
, &len
);
3270 if (client_sock
< 0) {
3271 zlog_warn("Can't accept zebra socket: %s",
3272 safe_strerror(errno
));
3276 /* Make client socket non-blocking. */
3277 set_nonblocking(client_sock
);
3279 /* Create new zebra client. */
3280 zebra_client_create(client_sock
);
3285 /* Make zebra server socket, wiping any existing one (see bug #403). */
3286 void zebra_zserv_socket_init(char *path
)
3291 struct sockaddr_storage sa
;
3294 if (!frr_zclient_addr(&sa
, &sa_len
, path
))
3295 /* should be caught in zebra main() */
3299 old_mask
= umask(0077);
3301 /* Make UNIX domain socket. */
3302 sock
= socket(sa
.ss_family
, SOCK_STREAM
, 0);
3304 zlog_warn("Can't create zserv socket: %s",
3305 safe_strerror(errno
));
3307 "zebra can't provide full functionality due to above error");
3311 if (sa
.ss_family
!= AF_UNIX
) {
3312 sockopt_reuseaddr(sock
);
3313 sockopt_reuseport(sock
);
3315 struct sockaddr_un
*suna
= (struct sockaddr_un
*)&sa
;
3316 if (suna
->sun_path
[0])
3317 unlink(suna
->sun_path
);
3320 zserv_privs
.change(ZPRIVS_RAISE
);
3321 setsockopt_so_recvbuf(sock
, 1048576);
3322 setsockopt_so_sendbuf(sock
, 1048576);
3323 zserv_privs
.change(ZPRIVS_LOWER
);
3325 if (sa
.ss_family
!= AF_UNIX
&& zserv_privs
.change(ZPRIVS_RAISE
))
3326 zlog_err("Can't raise privileges");
3328 ret
= bind(sock
, (struct sockaddr
*)&sa
, sa_len
);
3330 zlog_warn("Can't bind zserv socket on %s: %s", path
,
3331 safe_strerror(errno
));
3333 "zebra can't provide full functionality due to above error");
3337 if (sa
.ss_family
!= AF_UNIX
&& zserv_privs
.change(ZPRIVS_LOWER
))
3338 zlog_err("Can't lower privileges");
3340 ret
= listen(sock
, 5);
3342 zlog_warn("Can't listen to zserv socket %s: %s", path
,
3343 safe_strerror(errno
));
3345 "zebra can't provide full functionality due to above error");
3352 thread_add_read(zebrad
.master
, zebra_accept
, NULL
, sock
, NULL
);
3355 #define ZEBRA_TIME_BUF 32
3356 static char *zserv_time_buf(time_t *time1
, char *buf
, int buflen
)
3361 assert(buf
!= NULL
);
3362 assert(buflen
>= ZEBRA_TIME_BUF
);
3363 assert(time1
!= NULL
);
3366 snprintf(buf
, buflen
, "never ");
3370 now
= monotime(NULL
);
3374 if (now
< ONE_DAY_SECOND
)
3375 snprintf(buf
, buflen
, "%02d:%02d:%02d", tm
->tm_hour
, tm
->tm_min
,
3377 else if (now
< ONE_WEEK_SECOND
)
3378 snprintf(buf
, buflen
, "%dd%02dh%02dm", tm
->tm_yday
, tm
->tm_hour
,
3381 snprintf(buf
, buflen
, "%02dw%dd%02dh", tm
->tm_yday
/ 7,
3382 tm
->tm_yday
- ((tm
->tm_yday
/ 7) * 7), tm
->tm_hour
);
3386 static void zebra_show_client_detail(struct vty
*vty
, struct zserv
*client
)
3388 char cbuf
[ZEBRA_TIME_BUF
], rbuf
[ZEBRA_TIME_BUF
];
3389 char wbuf
[ZEBRA_TIME_BUF
], nhbuf
[ZEBRA_TIME_BUF
], mbuf
[ZEBRA_TIME_BUF
];
3391 vty_out(vty
, "Client: %s", zebra_route_string(client
->proto
));
3392 if (client
->instance
)
3393 vty_out(vty
, " Instance: %d", client
->instance
);
3396 vty_out(vty
, "------------------------ \n");
3397 vty_out(vty
, "FD: %d \n", client
->sock
);
3398 vty_out(vty
, "Route Table ID: %d \n", client
->rtm_table
);
3400 vty_out(vty
, "Connect Time: %s \n",
3401 zserv_time_buf(&client
->connect_time
, cbuf
, ZEBRA_TIME_BUF
));
3402 if (client
->nh_reg_time
) {
3403 vty_out(vty
, "Nexthop Registry Time: %s \n",
3404 zserv_time_buf(&client
->nh_reg_time
, nhbuf
,
3406 if (client
->nh_last_upd_time
)
3407 vty_out(vty
, "Nexthop Last Update Time: %s \n",
3408 zserv_time_buf(&client
->nh_last_upd_time
, mbuf
,
3411 vty_out(vty
, "No Nexthop Update sent\n");
3413 vty_out(vty
, "Not registered for Nexthop Updates\n");
3415 vty_out(vty
, "Last Msg Rx Time: %s \n",
3416 zserv_time_buf(&client
->last_read_time
, rbuf
, ZEBRA_TIME_BUF
));
3417 vty_out(vty
, "Last Msg Tx Time: %s \n",
3418 zserv_time_buf(&client
->last_write_time
, wbuf
, ZEBRA_TIME_BUF
));
3419 if (client
->last_read_time
)
3420 vty_out(vty
, "Last Rcvd Cmd: %s \n",
3421 zserv_command_string(client
->last_read_cmd
));
3422 if (client
->last_write_time
)
3423 vty_out(vty
, "Last Sent Cmd: %s \n",
3424 zserv_command_string(client
->last_write_cmd
));
3427 vty_out(vty
, "Type Add Update Del \n");
3428 vty_out(vty
, "================================================== \n");
3429 vty_out(vty
, "IPv4 %-12d%-12d%-12d\n", client
->v4_route_add_cnt
,
3430 client
->v4_route_upd8_cnt
, client
->v4_route_del_cnt
);
3431 vty_out(vty
, "IPv6 %-12d%-12d%-12d\n", client
->v6_route_add_cnt
,
3432 client
->v6_route_upd8_cnt
, client
->v6_route_del_cnt
);
3433 vty_out(vty
, "Redist:v4 %-12d%-12d%-12d\n", client
->redist_v4_add_cnt
,
3434 0, client
->redist_v4_del_cnt
);
3435 vty_out(vty
, "Redist:v6 %-12d%-12d%-12d\n", client
->redist_v6_add_cnt
,
3436 0, client
->redist_v6_del_cnt
);
3437 vty_out(vty
, "Connected %-12d%-12d%-12d\n", client
->ifadd_cnt
, 0,
3439 vty_out(vty
, "BFD peer %-12d%-12d%-12d\n", client
->bfd_peer_add_cnt
,
3440 client
->bfd_peer_upd8_cnt
, client
->bfd_peer_del_cnt
);
3441 vty_out(vty
, "Interface Up Notifications: %d\n", client
->ifup_cnt
);
3442 vty_out(vty
, "Interface Down Notifications: %d\n", client
->ifdown_cnt
);
3443 vty_out(vty
, "VNI add notifications: %d\n", client
->vniadd_cnt
);
3444 vty_out(vty
, "VNI delete notifications: %d\n", client
->vnidel_cnt
);
3445 vty_out(vty
, "L3-VNI add notifications: %d\n", client
->l3vniadd_cnt
);
3446 vty_out(vty
, "L3-VNI delete notifications: %d\n", client
->l3vnidel_cnt
);
3447 vty_out(vty
, "MAC-IP add notifications: %d\n", client
->macipadd_cnt
);
3448 vty_out(vty
, "MAC-IP delete notifications: %d\n", client
->macipdel_cnt
);
3454 static void zebra_show_client_brief(struct vty
*vty
, struct zserv
*client
)
3456 char cbuf
[ZEBRA_TIME_BUF
], rbuf
[ZEBRA_TIME_BUF
];
3457 char wbuf
[ZEBRA_TIME_BUF
];
3459 vty_out(vty
, "%-8s%12s %12s%12s%8d/%-8d%8d/%-8d\n",
3460 zebra_route_string(client
->proto
),
3461 zserv_time_buf(&client
->connect_time
, cbuf
, ZEBRA_TIME_BUF
),
3462 zserv_time_buf(&client
->last_read_time
, rbuf
, ZEBRA_TIME_BUF
),
3463 zserv_time_buf(&client
->last_write_time
, wbuf
, ZEBRA_TIME_BUF
),
3464 client
->v4_route_add_cnt
+ client
->v4_route_upd8_cnt
,
3465 client
->v4_route_del_cnt
,
3466 client
->v6_route_add_cnt
+ client
->v6_route_upd8_cnt
,
3467 client
->v6_route_del_cnt
);
3470 struct zserv
*zebra_find_client(uint8_t proto
, unsigned short instance
)
3472 struct listnode
*node
, *nnode
;
3473 struct zserv
*client
;
3475 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, node
, nnode
, client
)) {
3476 if (client
->proto
== proto
&& client
->instance
== instance
)
3483 /* This command is for debugging purpose. */
3484 DEFUN (show_zebra_client
,
3485 show_zebra_client_cmd
,
3486 "show zebra client",
3489 "Client information\n")
3491 struct listnode
*node
;
3492 struct zserv
*client
;
3494 for (ALL_LIST_ELEMENTS_RO(zebrad
.client_list
, node
, client
))
3495 zebra_show_client_detail(vty
, client
);
3500 /* This command is for debugging purpose. */
3501 DEFUN (show_zebra_client_summary
,
3502 show_zebra_client_summary_cmd
,
3503 "show zebra client summary",
3506 "Client information brief\n"
3509 struct listnode
*node
;
3510 struct zserv
*client
;
3513 "Name Connect Time Last Read Last Write IPv4 Routes IPv6 Routes \n");
3515 "--------------------------------------------------------------------------------\n");
3517 for (ALL_LIST_ELEMENTS_RO(zebrad
.client_list
, node
, client
))
3518 zebra_show_client_brief(vty
, client
);
3520 vty_out(vty
, "Routes column shows (added+updated)/deleted\n");
3524 #if defined(HANDLE_ZAPI_FUZZING)
3525 void zserv_read_file(char *input
)
3528 struct zserv
*client
= NULL
;
3531 zebra_client_create(-1);
3532 client
= zebrad
.client_list
->head
->data
;
3535 fd
= open(input
, O_RDONLY
| O_NONBLOCK
);
3538 zebra_client_read(&t
);
3544 void zserv_init(void)
3546 /* Client list init. */
3547 zebrad
.client_list
= list_new();
3548 zebrad
.client_list
->del
= (void (*)(void *))zebra_client_free
;
3550 install_element(ENABLE_NODE
, &show_zebra_client_cmd
);
3551 install_element(ENABLE_NODE
, &show_zebra_client_summary_cmd
);