2 * Zebra API message creation & consumption.
4 * Copyright (C) 1997-1999 Kunihiro Ishiguro
5 * Copyright (C) 2015-2018 Cumulus Networks, Inc.
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the Free
10 * Software Foundation; either version 2 of the License, or (at your option)
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
18 * You should have received a copy of the GNU General Public License along
19 * with this program; see the file COPYING; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 #include "lib/prefix.h"
27 #include "lib/command.h"
29 #include "lib/thread.h"
30 #include "lib/stream.h"
31 #include "lib/memory.h"
32 #include "lib/table.h"
33 #include "lib/network.h"
34 #include "lib/sockunion.h"
36 #include "lib/zclient.h"
37 #include "lib/privs.h"
38 #include "lib/network.h"
39 #include "lib/buffer.h"
40 #include "lib/nexthop.h"
42 #include "lib/libfrr.h"
43 #include "lib/sockopt.h"
45 #include "zebra/zebra_router.h"
46 #include "zebra/rib.h"
47 #include "zebra/zebra_memory.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/zebra_vxlan.h"
62 #include "zebra/zebra_pbr.h"
63 #include "zebra/table_manager.h"
64 #include "zebra/zapi_msg.h"
65 #include "zebra/zebra_errors.h"
66 #include "zebra/zebra_mlag.h"
68 /* Encoding helpers -------------------------------------------------------- */
70 static void zserv_encode_interface(struct stream
*s
, struct interface
*ifp
)
72 /* Interface information. */
73 struct zebra_if
*zif
= ifp
->info
;
75 stream_put(s
, ifp
->name
, INTERFACE_NAMSIZ
);
76 stream_putl(s
, ifp
->ifindex
);
77 stream_putc(s
, ifp
->status
);
78 stream_putq(s
, ifp
->flags
);
79 stream_putc(s
, ifp
->ptm_enable
);
80 stream_putc(s
, ifp
->ptm_status
);
81 stream_putl(s
, ifp
->metric
);
82 stream_putl(s
, ifp
->speed
);
83 stream_putl(s
, ifp
->mtu
);
84 stream_putl(s
, ifp
->mtu6
);
85 stream_putl(s
, ifp
->bandwidth
);
86 stream_putl(s
, zif
->link_ifindex
);
87 stream_putl(s
, ifp
->ll_type
);
88 stream_putl(s
, ifp
->hw_addr_len
);
90 stream_put(s
, ifp
->hw_addr
, ifp
->hw_addr_len
);
92 /* Then, Traffic Engineering parameters if any */
93 if (HAS_LINK_PARAMS(ifp
) && IS_LINK_PARAMS_SET(ifp
->link_params
)) {
95 zebra_interface_link_params_write(s
, ifp
);
99 /* Write packet size. */
100 stream_putw_at(s
, 0, stream_get_endp(s
));
103 static void zserv_encode_vrf(struct stream
*s
, struct zebra_vrf
*zvrf
)
105 struct vrf_data data
;
106 const char *netns_name
= zvrf_ns_name(zvrf
);
108 data
.l
.table_id
= zvrf
->table_id
;
111 strlcpy(data
.l
.netns_name
, basename((char *)netns_name
),
114 memset(data
.l
.netns_name
, 0, NS_NAMSIZ
);
115 /* Pass the tableid and the netns NAME */
116 stream_put(s
, &data
, sizeof(struct vrf_data
));
117 /* Interface information. */
118 stream_put(s
, zvrf_name(zvrf
), VRF_NAMSIZ
);
119 /* Write packet size. */
120 stream_putw_at(s
, 0, stream_get_endp(s
));
123 static int zserv_encode_nexthop(struct stream
*s
, struct nexthop
*nexthop
)
125 stream_putl(s
, nexthop
->vrf_id
);
126 stream_putc(s
, nexthop
->type
);
127 switch (nexthop
->type
) {
128 case NEXTHOP_TYPE_IPV4
:
129 case NEXTHOP_TYPE_IPV4_IFINDEX
:
130 stream_put_in_addr(s
, &nexthop
->gate
.ipv4
);
131 stream_putl(s
, nexthop
->ifindex
);
133 case NEXTHOP_TYPE_IPV6
:
134 stream_put(s
, &nexthop
->gate
.ipv6
, 16);
136 case NEXTHOP_TYPE_IPV6_IFINDEX
:
137 stream_put(s
, &nexthop
->gate
.ipv6
, 16);
138 stream_putl(s
, nexthop
->ifindex
);
140 case NEXTHOP_TYPE_IFINDEX
:
141 stream_putl(s
, nexthop
->ifindex
);
150 /* Send handlers ----------------------------------------------------------- */
152 /* Interface is added. Send ZEBRA_INTERFACE_ADD to client. */
154 * This function is called in the following situations:
155 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
157 * - at startup, when zebra figures out the available interfaces
158 * - when an interface is added (where support for
159 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
160 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
163 int zsend_interface_add(struct zserv
*client
, struct interface
*ifp
)
165 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
167 zclient_create_header(s
, ZEBRA_INTERFACE_ADD
, ifp
->vrf_id
);
168 zserv_encode_interface(s
, ifp
);
171 return zserv_send_message(client
, s
);
174 /* Interface deletion from zebra daemon. */
175 int zsend_interface_delete(struct zserv
*client
, struct interface
*ifp
)
177 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
179 zclient_create_header(s
, ZEBRA_INTERFACE_DELETE
, ifp
->vrf_id
);
180 zserv_encode_interface(s
, ifp
);
183 return zserv_send_message(client
, s
);
186 int zsend_vrf_add(struct zserv
*client
, struct zebra_vrf
*zvrf
)
188 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
190 zclient_create_header(s
, ZEBRA_VRF_ADD
, zvrf_id(zvrf
));
191 zserv_encode_vrf(s
, zvrf
);
193 client
->vrfadd_cnt
++;
194 return zserv_send_message(client
, s
);
197 /* VRF deletion from zebra daemon. */
198 int zsend_vrf_delete(struct zserv
*client
, struct zebra_vrf
*zvrf
)
201 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
203 zclient_create_header(s
, ZEBRA_VRF_DELETE
, zvrf_id(zvrf
));
204 zserv_encode_vrf(s
, zvrf
);
206 client
->vrfdel_cnt
++;
207 return zserv_send_message(client
, s
);
210 int zsend_interface_link_params(struct zserv
*client
, struct interface
*ifp
)
212 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
214 if (!ifp
->link_params
) {
219 zclient_create_header(s
, ZEBRA_INTERFACE_LINK_PARAMS
, ifp
->vrf_id
);
221 /* Add Interface Index */
222 stream_putl(s
, ifp
->ifindex
);
224 /* Then TE Link Parameters */
225 if (zebra_interface_link_params_write(s
, ifp
) == 0) {
230 /* Write packet size. */
231 stream_putw_at(s
, 0, stream_get_endp(s
));
233 return zserv_send_message(client
, s
);
236 /* Interface address is added/deleted. Send ZEBRA_INTERFACE_ADDRESS_ADD or
237 * ZEBRA_INTERFACE_ADDRESS_DELETE to the client.
239 * A ZEBRA_INTERFACE_ADDRESS_ADD is sent in the following situations:
240 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
241 * from the client, after the ZEBRA_INTERFACE_ADD has been
242 * sent from zebra to the client
243 * - redistribute new address info to all clients in the following situations
244 * - at startup, when zebra figures out the available interfaces
245 * - when an interface is added (where support for
246 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
247 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
249 * - for the vty commands "ip address A.B.C.D/M [<label LINE>]"
250 * and "no bandwidth <1-10000000>", "ipv6 address X:X::X:X/M"
251 * - when an RTM_NEWADDR message is received from the kernel,
253 * The call tree that triggers ZEBRA_INTERFACE_ADDRESS_DELETE:
255 * zsend_interface_address(DELETE)
258 * zebra_interface_address_delete_update
260 * | | if_delete_update
262 * ip_address_uninstall connected_delete_ipv4
263 * [ipv6_addresss_uninstall] [connected_delete_ipv6]
266 * | RTM_NEWADDR on routing/netlink socket
269 * "no ip address A.B.C.D/M [label LINE]"
270 * "no ip address A.B.C.D/M"
271 * ["no ipv6 address X:X::X:X/M"]
274 int zsend_interface_address(int cmd
, struct zserv
*client
,
275 struct interface
*ifp
, struct connected
*ifc
)
279 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
281 zclient_create_header(s
, cmd
, ifp
->vrf_id
);
282 stream_putl(s
, ifp
->ifindex
);
284 /* Interface address flag. */
285 stream_putc(s
, ifc
->flags
);
287 /* Prefix information. */
289 stream_putc(s
, p
->family
);
290 blen
= prefix_blen(p
);
291 stream_put(s
, &p
->u
.prefix
, blen
);
294 * XXX gnu version does not send prefixlen for
295 * ZEBRA_INTERFACE_ADDRESS_DELETE
296 * but zebra_interface_address_delete_read() in the gnu version
299 stream_putc(s
, p
->prefixlen
);
302 p
= ifc
->destination
;
304 stream_put(s
, &p
->u
.prefix
, blen
);
306 stream_put(s
, NULL
, blen
);
308 /* Write packet size. */
309 stream_putw_at(s
, 0, stream_get_endp(s
));
311 client
->connected_rt_add_cnt
++;
312 return zserv_send_message(client
, s
);
315 static int zsend_interface_nbr_address(int cmd
, struct zserv
*client
,
316 struct interface
*ifp
,
317 struct nbr_connected
*ifc
)
320 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
323 zclient_create_header(s
, cmd
, ifp
->vrf_id
);
324 stream_putl(s
, ifp
->ifindex
);
326 /* Prefix information. */
328 stream_putc(s
, p
->family
);
329 blen
= prefix_blen(p
);
330 stream_put(s
, &p
->u
.prefix
, blen
);
333 * XXX gnu version does not send prefixlen for
334 * ZEBRA_INTERFACE_ADDRESS_DELETE
335 * but zebra_interface_address_delete_read() in the gnu version
338 stream_putc(s
, p
->prefixlen
);
340 /* Write packet size. */
341 stream_putw_at(s
, 0, stream_get_endp(s
));
343 return zserv_send_message(client
, s
);
346 /* Interface address addition. */
347 static void zebra_interface_nbr_address_add_update(struct interface
*ifp
,
348 struct nbr_connected
*ifc
)
350 struct listnode
*node
, *nnode
;
351 struct zserv
*client
;
354 if (IS_ZEBRA_DEBUG_EVENT
) {
355 char buf
[INET6_ADDRSTRLEN
];
359 "MESSAGE: ZEBRA_INTERFACE_NBR_ADDRESS_ADD %s/%d on %s",
360 inet_ntop(p
->family
, &p
->u
.prefix
, buf
,
362 p
->prefixlen
, ifc
->ifp
->name
);
365 for (ALL_LIST_ELEMENTS(zrouter
.client_list
, node
, nnode
, client
))
366 zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_ADD
,
370 /* Interface address deletion. */
371 static void zebra_interface_nbr_address_delete_update(struct interface
*ifp
,
372 struct nbr_connected
*ifc
)
374 struct listnode
*node
, *nnode
;
375 struct zserv
*client
;
378 if (IS_ZEBRA_DEBUG_EVENT
) {
379 char buf
[INET6_ADDRSTRLEN
];
383 "MESSAGE: ZEBRA_INTERFACE_NBR_ADDRESS_DELETE %s/%d on %s",
384 inet_ntop(p
->family
, &p
->u
.prefix
, buf
,
386 p
->prefixlen
, ifc
->ifp
->name
);
389 for (ALL_LIST_ELEMENTS(zrouter
.client_list
, node
, nnode
, client
))
390 zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
,
394 /* Send addresses on interface to client */
395 int zsend_interface_addresses(struct zserv
*client
, struct interface
*ifp
)
397 struct listnode
*cnode
, *cnnode
;
399 struct nbr_connected
*nc
;
401 /* Send interface addresses. */
402 for (ALL_LIST_ELEMENTS(ifp
->connected
, cnode
, cnnode
, c
)) {
403 if (!CHECK_FLAG(c
->conf
, ZEBRA_IFC_REAL
))
406 if (zsend_interface_address(ZEBRA_INTERFACE_ADDRESS_ADD
, client
,
412 /* Send interface neighbors. */
413 for (ALL_LIST_ELEMENTS(ifp
->nbr_connected
, cnode
, cnnode
, nc
)) {
414 if (zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_ADD
,
423 /* Notify client about interface moving from one VRF to another.
424 * Whether client is interested in old and new VRF is checked by caller.
426 int zsend_interface_vrf_update(struct zserv
*client
, struct interface
*ifp
,
429 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
431 zclient_create_header(s
, ZEBRA_INTERFACE_VRF_UPDATE
, ifp
->vrf_id
);
433 /* Fill in the name of the interface and its new VRF (id) */
434 stream_put(s
, ifp
->name
, INTERFACE_NAMSIZ
);
435 stream_putl(s
, vrf_id
);
437 /* Write packet size. */
438 stream_putw_at(s
, 0, stream_get_endp(s
));
440 client
->if_vrfchg_cnt
++;
441 return zserv_send_message(client
, s
);
444 /* Add new nbr connected IPv6 address */
445 void nbr_connected_add_ipv6(struct interface
*ifp
, struct in6_addr
*address
)
447 struct nbr_connected
*ifc
;
451 IPV6_ADDR_COPY(&p
.u
.prefix6
, address
);
452 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
454 ifc
= listnode_head(ifp
->nbr_connected
);
457 ifc
= nbr_connected_new();
458 ifc
->address
= prefix_new();
460 listnode_add(ifp
->nbr_connected
, ifc
);
463 prefix_copy(ifc
->address
, &p
);
465 zebra_interface_nbr_address_add_update(ifp
, ifc
);
467 if_nbr_ipv6ll_to_ipv4ll_neigh_update(ifp
, address
, 1);
470 void nbr_connected_delete_ipv6(struct interface
*ifp
, struct in6_addr
*address
)
472 struct nbr_connected
*ifc
;
476 IPV6_ADDR_COPY(&p
.u
.prefix6
, address
);
477 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
479 ifc
= nbr_connected_check(ifp
, &p
);
483 listnode_delete(ifp
->nbr_connected
, ifc
);
485 zebra_interface_nbr_address_delete_update(ifp
, ifc
);
487 if_nbr_ipv6ll_to_ipv4ll_neigh_update(ifp
, address
, 0);
489 nbr_connected_free(ifc
);
493 * The cmd passed to zsend_interface_update may be ZEBRA_INTERFACE_UP or
494 * ZEBRA_INTERFACE_DOWN.
496 * The ZEBRA_INTERFACE_UP message is sent from the zebra server to
497 * the clients in one of 2 situations:
498 * - an if_up is detected e.g., as a result of an RTM_IFINFO message
499 * - a vty command modifying the bandwidth of an interface is received.
500 * The ZEBRA_INTERFACE_DOWN message is sent when an if_down is detected.
502 int zsend_interface_update(int cmd
, struct zserv
*client
, struct interface
*ifp
)
504 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
506 zclient_create_header(s
, cmd
, ifp
->vrf_id
);
507 zserv_encode_interface(s
, ifp
);
509 if (cmd
== ZEBRA_INTERFACE_UP
)
512 client
->ifdown_cnt
++;
514 return zserv_send_message(client
, s
);
517 int zsend_redistribute_route(int cmd
, struct zserv
*client
,
518 const struct prefix
*p
,
519 const struct prefix
*src_p
,
520 const struct route_entry
*re
)
522 struct zapi_route api
;
523 struct zapi_nexthop
*api_nh
;
524 struct nexthop
*nexthop
;
528 MAX(ZEBRA_MAX_PACKET_SIZ
, sizeof(struct zapi_route
));
530 memset(&api
, 0, sizeof(api
));
531 api
.vrf_id
= re
->vrf_id
;
533 api
.safi
= SAFI_UNICAST
;
534 api
.instance
= re
->instance
;
535 api
.flags
= re
->flags
;
537 afi
= family2afi(p
->family
);
540 if (cmd
== ZEBRA_REDISTRIBUTE_ROUTE_ADD
)
541 client
->redist_v4_add_cnt
++;
543 client
->redist_v4_del_cnt
++;
546 if (cmd
== ZEBRA_REDISTRIBUTE_ROUTE_ADD
)
547 client
->redist_v6_add_cnt
++;
549 client
->redist_v6_del_cnt
++;
558 SET_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
);
559 memcpy(&api
.src_prefix
, src_p
, sizeof(api
.src_prefix
));
562 for (nexthop
= re
->ng
->nexthop
; nexthop
; nexthop
= nexthop
->next
) {
563 if (!CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
566 api_nh
= &api
.nexthops
[count
];
567 api_nh
->vrf_id
= nexthop
->vrf_id
;
568 api_nh
->type
= nexthop
->type
;
569 switch (nexthop
->type
) {
570 case NEXTHOP_TYPE_BLACKHOLE
:
571 api_nh
->bh_type
= nexthop
->bh_type
;
573 case NEXTHOP_TYPE_IPV4
:
574 api_nh
->gate
.ipv4
= nexthop
->gate
.ipv4
;
576 case NEXTHOP_TYPE_IPV4_IFINDEX
:
577 api_nh
->gate
.ipv4
= nexthop
->gate
.ipv4
;
578 api_nh
->ifindex
= nexthop
->ifindex
;
580 case NEXTHOP_TYPE_IFINDEX
:
581 api_nh
->ifindex
= nexthop
->ifindex
;
583 case NEXTHOP_TYPE_IPV6
:
584 api_nh
->gate
.ipv6
= nexthop
->gate
.ipv6
;
586 case NEXTHOP_TYPE_IPV6_IFINDEX
:
587 api_nh
->gate
.ipv6
= nexthop
->gate
.ipv6
;
588 api_nh
->ifindex
= nexthop
->ifindex
;
595 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
596 api
.nexthop_num
= count
;
600 SET_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
);
601 api
.distance
= re
->distance
;
602 SET_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
);
603 api
.metric
= re
->metric
;
605 SET_FLAG(api
.message
, ZAPI_MESSAGE_TAG
);
608 SET_FLAG(api
.message
, ZAPI_MESSAGE_MTU
);
611 struct stream
*s
= stream_new(stream_size
);
613 /* Encode route and send. */
614 if (zapi_route_encode(cmd
, s
, &api
) < 0) {
619 if (IS_ZEBRA_DEBUG_SEND
) {
620 char buf_prefix
[PREFIX_STRLEN
];
622 prefix2str(&api
.prefix
, buf_prefix
, sizeof(buf_prefix
));
624 zlog_debug("%s: %s to client %s: type %s, vrf_id %d, p %s",
625 __func__
, zserv_command_string(cmd
),
626 zebra_route_string(client
->proto
),
627 zebra_route_string(api
.type
), api
.vrf_id
,
630 return zserv_send_message(client
, s
);
634 * Modified version of zsend_ipv4_nexthop_lookup(): Query unicast rib if
635 * nexthop is not found on mrib. Returns both route metric and protocol
638 static int zsend_ipv4_nexthop_lookup_mrib(struct zserv
*client
,
640 struct route_entry
*re
,
641 struct zebra_vrf
*zvrf
)
646 struct nexthop
*nexthop
;
648 /* Get output stream. */
649 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
652 /* Fill in result. */
653 zclient_create_header(s
, ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB
, zvrf_id(zvrf
));
654 stream_put_in_addr(s
, &addr
);
657 stream_putc(s
, re
->distance
);
658 stream_putl(s
, re
->metric
);
660 /* remember position for nexthop_num */
661 nump
= stream_get_endp(s
);
662 /* reserve room for nexthop_num */
665 * Only non-recursive routes are elegible to resolve the
666 * nexthop we are looking up. Therefore, we will just iterate
667 * over the top chain of nexthops.
669 for (nexthop
= re
->ng
->nexthop
; nexthop
;
670 nexthop
= nexthop
->next
)
671 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
672 num
+= zserv_encode_nexthop(s
, nexthop
);
674 /* store nexthop_num */
675 stream_putc_at(s
, nump
, num
);
677 stream_putc(s
, 0); /* distance */
678 stream_putl(s
, 0); /* metric */
679 stream_putc(s
, 0); /* nexthop_num */
682 stream_putw_at(s
, 0, stream_get_endp(s
));
684 return zserv_send_message(client
, s
);
688 * Common utility send route notification, called from a path using a
689 * route_entry and from a path using a dataplane context.
691 static int route_notify_internal(const struct prefix
*p
, int type
,
692 uint16_t instance
, vrf_id_t vrf_id
,
694 enum zapi_route_notify_owner note
)
696 struct zserv
*client
;
700 client
= zserv_find_client(type
, instance
);
701 if (!client
|| !client
->notify_owner
) {
702 if (IS_ZEBRA_DEBUG_PACKET
) {
703 char buff
[PREFIX_STRLEN
];
706 "Not Notifying Owner: %u about prefix %s(%u) %d vrf: %u",
707 type
, prefix2str(p
, buff
, sizeof(buff
)),
708 table_id
, note
, vrf_id
);
713 if (IS_ZEBRA_DEBUG_PACKET
) {
714 char buff
[PREFIX_STRLEN
];
716 zlog_debug("Notifying Owner: %u about prefix %s(%u) %d vrf: %u",
717 type
, prefix2str(p
, buff
, sizeof(buff
)),
718 table_id
, note
, vrf_id
);
721 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
724 zclient_create_header(s
, ZEBRA_ROUTE_NOTIFY_OWNER
, vrf_id
);
726 stream_put(s
, ¬e
, sizeof(note
));
728 stream_putc(s
, p
->family
);
730 blen
= prefix_blen(p
);
731 stream_putc(s
, p
->prefixlen
);
732 stream_put(s
, &p
->u
.prefix
, blen
);
734 stream_putl(s
, table_id
);
736 stream_putw_at(s
, 0, stream_get_endp(s
));
738 return zserv_send_message(client
, s
);
741 int zsend_route_notify_owner(struct route_entry
*re
, const struct prefix
*p
,
742 enum zapi_route_notify_owner note
)
744 return (route_notify_internal(p
, re
->type
, re
->instance
, re
->vrf_id
,
749 * Route-owner notification using info from dataplane update context.
751 int zsend_route_notify_owner_ctx(const struct zebra_dplane_ctx
*ctx
,
752 enum zapi_route_notify_owner note
)
754 return (route_notify_internal(dplane_ctx_get_dest(ctx
),
755 dplane_ctx_get_type(ctx
),
756 dplane_ctx_get_instance(ctx
),
757 dplane_ctx_get_vrf(ctx
),
758 dplane_ctx_get_table(ctx
),
762 void zsend_rule_notify_owner(struct zebra_pbr_rule
*rule
,
763 enum zapi_rule_notify_owner note
)
765 struct listnode
*node
;
766 struct zserv
*client
;
769 if (IS_ZEBRA_DEBUG_PACKET
)
770 zlog_debug("%s: Notifying %u", __PRETTY_FUNCTION__
,
773 for (ALL_LIST_ELEMENTS_RO(zrouter
.client_list
, node
, client
)) {
774 if (rule
->sock
== client
->sock
)
781 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
783 zclient_create_header(s
, ZEBRA_RULE_NOTIFY_OWNER
, VRF_DEFAULT
);
784 stream_put(s
, ¬e
, sizeof(note
));
785 stream_putl(s
, rule
->rule
.seq
);
786 stream_putl(s
, rule
->rule
.priority
);
787 stream_putl(s
, rule
->rule
.unique
);
788 stream_putl(s
, rule
->rule
.ifindex
);
790 stream_putw_at(s
, 0, stream_get_endp(s
));
792 zserv_send_message(client
, s
);
795 void zsend_ipset_notify_owner(struct zebra_pbr_ipset
*ipset
,
796 enum zapi_ipset_notify_owner note
)
798 struct listnode
*node
;
799 struct zserv
*client
;
802 if (IS_ZEBRA_DEBUG_PACKET
)
803 zlog_debug("%s: Notifying %u", __PRETTY_FUNCTION__
,
806 for (ALL_LIST_ELEMENTS_RO(zrouter
.client_list
, node
, client
)) {
807 if (ipset
->sock
== client
->sock
)
814 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
816 zclient_create_header(s
, ZEBRA_IPSET_NOTIFY_OWNER
, VRF_DEFAULT
);
817 stream_put(s
, ¬e
, sizeof(note
));
818 stream_putl(s
, ipset
->unique
);
819 stream_put(s
, ipset
->ipset_name
, ZEBRA_IPSET_NAME_SIZE
);
820 stream_putw_at(s
, 0, stream_get_endp(s
));
822 zserv_send_message(client
, s
);
825 void zsend_ipset_entry_notify_owner(struct zebra_pbr_ipset_entry
*ipset
,
826 enum zapi_ipset_entry_notify_owner note
)
828 struct listnode
*node
;
829 struct zserv
*client
;
832 if (IS_ZEBRA_DEBUG_PACKET
)
833 zlog_debug("%s: Notifying %u", __PRETTY_FUNCTION__
,
836 for (ALL_LIST_ELEMENTS_RO(zrouter
.client_list
, node
, client
)) {
837 if (ipset
->sock
== client
->sock
)
844 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
846 zclient_create_header(s
, ZEBRA_IPSET_ENTRY_NOTIFY_OWNER
, VRF_DEFAULT
);
847 stream_put(s
, ¬e
, sizeof(note
));
848 stream_putl(s
, ipset
->unique
);
849 stream_put(s
, ipset
->backpointer
->ipset_name
, ZEBRA_IPSET_NAME_SIZE
);
850 stream_putw_at(s
, 0, stream_get_endp(s
));
852 zserv_send_message(client
, s
);
855 void zsend_iptable_notify_owner(struct zebra_pbr_iptable
*iptable
,
856 enum zapi_iptable_notify_owner note
)
858 struct listnode
*node
;
859 struct zserv
*client
;
862 if (IS_ZEBRA_DEBUG_PACKET
)
863 zlog_debug("%s: Notifying %u", __PRETTY_FUNCTION__
,
866 for (ALL_LIST_ELEMENTS_RO(zrouter
.client_list
, node
, client
)) {
867 if (iptable
->sock
== client
->sock
)
874 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
876 zclient_create_header(s
, ZEBRA_IPTABLE_NOTIFY_OWNER
, VRF_DEFAULT
);
877 stream_put(s
, ¬e
, sizeof(note
));
878 stream_putl(s
, iptable
->unique
);
879 stream_putw_at(s
, 0, stream_get_endp(s
));
881 zserv_send_message(client
, s
);
884 /* Router-id is updated. Send ZEBRA_ROUTER_ID_ADD to client. */
885 int zsend_router_id_update(struct zserv
*client
, struct prefix
*p
,
890 /* Check this client need interface information. */
891 if (!vrf_bitmap_check(client
->ridinfo
, vrf_id
))
894 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
897 zclient_create_header(s
, ZEBRA_ROUTER_ID_UPDATE
, vrf_id
);
899 /* Prefix information. */
900 stream_putc(s
, p
->family
);
901 blen
= prefix_blen(p
);
902 stream_put(s
, &p
->u
.prefix
, blen
);
903 stream_putc(s
, p
->prefixlen
);
905 /* Write packet size. */
906 stream_putw_at(s
, 0, stream_get_endp(s
));
908 return zserv_send_message(client
, s
);
912 * Function used by Zebra to send a PW status update to LDP daemon
914 int zsend_pw_update(struct zserv
*client
, struct zebra_pw
*pw
)
916 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
918 zclient_create_header(s
, ZEBRA_PW_STATUS_UPDATE
, pw
->vrf_id
);
919 stream_write(s
, pw
->ifname
, IF_NAMESIZE
);
920 stream_putl(s
, pw
->ifindex
);
921 stream_putl(s
, pw
->status
);
923 /* Put length at the first point of the stream. */
924 stream_putw_at(s
, 0, stream_get_endp(s
));
926 return zserv_send_message(client
, s
);
929 /* Send response to a get label chunk request to client */
930 int zsend_assign_label_chunk_response(struct zserv
*client
, vrf_id_t vrf_id
,
931 uint8_t proto
, uint16_t instance
,
932 struct label_manager_chunk
*lmc
)
935 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
937 zclient_create_header(s
, ZEBRA_GET_LABEL_CHUNK
, vrf_id
);
939 stream_putc(s
, proto
);
941 stream_putw(s
, instance
);
945 stream_putc(s
, lmc
->keep
);
946 /* start and end labels */
947 stream_putl(s
, lmc
->start
);
948 stream_putl(s
, lmc
->end
);
951 /* Write packet size. */
952 stream_putw_at(s
, 0, stream_get_endp(s
));
954 ret
= writen(client
->sock
, s
->data
, stream_get_endp(s
));
959 /* Send response to a label manager connect request to client */
960 int zsend_label_manager_connect_response(struct zserv
*client
, vrf_id_t vrf_id
,
961 unsigned short result
)
964 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
966 zclient_create_header(s
, ZEBRA_LABEL_MANAGER_CONNECT
, vrf_id
);
969 stream_putc(s
, client
->proto
);
972 stream_putw(s
, client
->instance
);
975 stream_putc(s
, result
);
977 /* Write packet size. */
978 stream_putw_at(s
, 0, stream_get_endp(s
));
980 ret
= writen(client
->sock
, s
->data
, stream_get_endp(s
));
986 /* Send response to a get table chunk request to client */
987 static int zsend_assign_table_chunk_response(struct zserv
*client
,
989 struct table_manager_chunk
*tmc
)
991 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
993 zclient_create_header(s
, ZEBRA_GET_TABLE_CHUNK
, vrf_id
);
996 /* start and end labels */
997 stream_putl(s
, tmc
->start
);
998 stream_putl(s
, tmc
->end
);
1001 /* Write packet size. */
1002 stream_putw_at(s
, 0, stream_get_endp(s
));
1004 return zserv_send_message(client
, s
);
1007 static int zsend_table_manager_connect_response(struct zserv
*client
,
1011 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
1013 zclient_create_header(s
, ZEBRA_TABLE_MANAGER_CONNECT
, vrf_id
);
1016 stream_putc(s
, result
);
1018 stream_putw_at(s
, 0, stream_get_endp(s
));
1020 return zserv_send_message(client
, s
);
1023 /* Inbound message handling ------------------------------------------------ */
1026 [ZEBRA_NEXTHOP_REGISTER
] = RNH_NEXTHOP_TYPE
,
1027 [ZEBRA_NEXTHOP_UNREGISTER
] = RNH_NEXTHOP_TYPE
,
1028 [ZEBRA_IMPORT_ROUTE_REGISTER
] = RNH_IMPORT_CHECK_TYPE
,
1029 [ZEBRA_IMPORT_ROUTE_UNREGISTER
] = RNH_IMPORT_CHECK_TYPE
,
1032 /* Nexthop register */
1033 static void zread_rnh_register(ZAPI_HANDLER_ARGS
)
1038 unsigned short l
= 0;
1040 uint16_t type
= cmd2type
[hdr
->command
];
1042 bool flag_changed
= false;
1045 if (IS_ZEBRA_DEBUG_NHT
)
1047 "rnh_register msg from client %s: hdr->length=%d, type=%s vrf=%u\n",
1048 zebra_route_string(client
->proto
), hdr
->length
,
1049 (type
== RNH_NEXTHOP_TYPE
) ? "nexthop" : "route",
1054 client
->nh_reg_time
= monotime(NULL
);
1056 while (l
< hdr
->length
) {
1057 STREAM_GETC(s
, flags
);
1058 STREAM_GETW(s
, p
.family
);
1059 STREAM_GETC(s
, p
.prefixlen
);
1061 if (p
.family
== AF_INET
) {
1062 client
->v4_nh_watch_add_cnt
++;
1063 if (p
.prefixlen
> IPV4_MAX_BITLEN
) {
1065 "%s: Specified prefix hdr->length %d is too large for a v4 address",
1066 __PRETTY_FUNCTION__
, p
.prefixlen
);
1069 STREAM_GET(&p
.u
.prefix4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
1070 l
+= IPV4_MAX_BYTELEN
;
1071 } else if (p
.family
== AF_INET6
) {
1072 client
->v6_nh_watch_add_cnt
++;
1073 if (p
.prefixlen
> IPV6_MAX_BITLEN
) {
1075 "%s: Specified prefix hdr->length %d is to large for a v6 address",
1076 __PRETTY_FUNCTION__
, p
.prefixlen
);
1079 STREAM_GET(&p
.u
.prefix6
, s
, IPV6_MAX_BYTELEN
);
1080 l
+= IPV6_MAX_BYTELEN
;
1083 EC_ZEBRA_UNKNOWN_FAMILY
,
1084 "rnh_register: Received unknown family type %d\n",
1088 rnh
= zebra_add_rnh(&p
, zvrf_id(zvrf
), type
, &exist
);
1092 orig_flags
= rnh
->flags
;
1093 if (type
== RNH_NEXTHOP_TYPE
) {
1095 && !CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
))
1096 SET_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
);
1098 && CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
))
1099 UNSET_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
);
1100 } else if (type
== RNH_IMPORT_CHECK_TYPE
) {
1102 && !CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
))
1103 SET_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
);
1105 && CHECK_FLAG(rnh
->flags
,
1106 ZEBRA_NHT_EXACT_MATCH
))
1107 UNSET_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
);
1110 if (orig_flags
!= rnh
->flags
)
1111 flag_changed
= true;
1113 /* Anything not AF_INET/INET6 has been filtered out above */
1114 if (!exist
|| flag_changed
)
1115 zebra_evaluate_rnh(zvrf
, family2afi(p
.family
), 1, type
,
1118 zebra_add_rnh_client(rnh
, client
, type
, zvrf_id(zvrf
));
1125 /* Nexthop register */
1126 static void zread_rnh_unregister(ZAPI_HANDLER_ARGS
)
1131 unsigned short l
= 0;
1132 uint16_t type
= cmd2type
[hdr
->command
];
1134 if (IS_ZEBRA_DEBUG_NHT
)
1136 "rnh_unregister msg from client %s: hdr->length=%d vrf: %u\n",
1137 zebra_route_string(client
->proto
), hdr
->length
,
1142 while (l
< hdr
->length
) {
1145 STREAM_GETC(s
, flags
);
1147 goto stream_failure
;
1149 STREAM_GETW(s
, p
.family
);
1150 STREAM_GETC(s
, p
.prefixlen
);
1152 if (p
.family
== AF_INET
) {
1153 client
->v4_nh_watch_rem_cnt
++;
1154 if (p
.prefixlen
> IPV4_MAX_BITLEN
) {
1156 "%s: Specified prefix hdr->length %d is to large for a v4 address",
1157 __PRETTY_FUNCTION__
, p
.prefixlen
);
1160 STREAM_GET(&p
.u
.prefix4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
1161 l
+= IPV4_MAX_BYTELEN
;
1162 } else if (p
.family
== AF_INET6
) {
1163 client
->v6_nh_watch_rem_cnt
++;
1164 if (p
.prefixlen
> IPV6_MAX_BITLEN
) {
1166 "%s: Specified prefix hdr->length %d is to large for a v6 address",
1167 __PRETTY_FUNCTION__
, p
.prefixlen
);
1170 STREAM_GET(&p
.u
.prefix6
, s
, IPV6_MAX_BYTELEN
);
1171 l
+= IPV6_MAX_BYTELEN
;
1174 EC_ZEBRA_UNKNOWN_FAMILY
,
1175 "rnh_register: Received unknown family type %d\n",
1179 rnh
= zebra_lookup_rnh(&p
, zvrf_id(zvrf
), type
);
1181 client
->nh_dereg_time
= monotime(NULL
);
1182 zebra_remove_rnh_client(rnh
, client
, type
);
1189 #define ZEBRA_MIN_FEC_LENGTH 5
1192 static void zread_fec_register(ZAPI_HANDLER_ARGS
)
1195 unsigned short l
= 0;
1198 uint32_t label
= MPLS_INVALID_LABEL
;
1199 uint32_t label_index
= MPLS_INVALID_LABEL_INDEX
;
1202 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1207 * The minimum amount of data that can be sent for one fec
1210 if (hdr
->length
< ZEBRA_MIN_FEC_LENGTH
) {
1212 EC_ZEBRA_IRDP_LEN_MISMATCH
,
1213 "fec_register: Received a fec register of hdr->length %d, it is of insufficient size to properly decode",
1218 while (l
< hdr
->length
) {
1219 STREAM_GETW(s
, flags
);
1220 memset(&p
, 0, sizeof(p
));
1221 STREAM_GETW(s
, p
.family
);
1222 if (p
.family
!= AF_INET
&& p
.family
!= AF_INET6
) {
1224 EC_ZEBRA_UNKNOWN_FAMILY
,
1225 "fec_register: Received unknown family type %d\n",
1229 STREAM_GETC(s
, p
.prefixlen
);
1230 if ((p
.family
== AF_INET
&& p
.prefixlen
> IPV4_MAX_BITLEN
)
1231 || (p
.family
== AF_INET6
1232 && p
.prefixlen
> IPV6_MAX_BITLEN
)) {
1234 "%s: Specified prefix hdr->length: %d is to long for %d",
1235 __PRETTY_FUNCTION__
, p
.prefixlen
, p
.family
);
1239 STREAM_GET(&p
.u
.prefix
, s
, PSIZE(p
.prefixlen
));
1240 l
+= PSIZE(p
.prefixlen
);
1241 if (flags
& ZEBRA_FEC_REGISTER_LABEL
) {
1242 STREAM_GETL(s
, label
);
1244 } else if (flags
& ZEBRA_FEC_REGISTER_LABEL_INDEX
) {
1245 STREAM_GETL(s
, label_index
);
1249 zebra_mpls_fec_register(zvrf
, &p
, label
, label_index
, client
);
1256 /* FEC unregister */
1257 static void zread_fec_unregister(ZAPI_HANDLER_ARGS
)
1260 unsigned short l
= 0;
1265 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1270 * The minimum amount of data that can be sent for one
1271 * fec unregistration
1273 if (hdr
->length
< ZEBRA_MIN_FEC_LENGTH
) {
1275 EC_ZEBRA_IRDP_LEN_MISMATCH
,
1276 "fec_unregister: Received a fec unregister of hdr->length %d, it is of insufficient size to properly decode",
1281 while (l
< hdr
->length
) {
1282 STREAM_GETW(s
, flags
);
1284 goto stream_failure
;
1286 memset(&p
, 0, sizeof(p
));
1287 STREAM_GETW(s
, p
.family
);
1288 if (p
.family
!= AF_INET
&& p
.family
!= AF_INET6
) {
1290 EC_ZEBRA_UNKNOWN_FAMILY
,
1291 "fec_unregister: Received unknown family type %d\n",
1295 STREAM_GETC(s
, p
.prefixlen
);
1296 if ((p
.family
== AF_INET
&& p
.prefixlen
> IPV4_MAX_BITLEN
)
1297 || (p
.family
== AF_INET6
1298 && p
.prefixlen
> IPV6_MAX_BITLEN
)) {
1300 "%s: Received prefix hdr->length %d which is greater than %d can support",
1301 __PRETTY_FUNCTION__
, p
.prefixlen
, p
.family
);
1305 STREAM_GET(&p
.u
.prefix
, s
, PSIZE(p
.prefixlen
));
1306 l
+= PSIZE(p
.prefixlen
);
1307 zebra_mpls_fec_unregister(zvrf
, &p
, client
);
1316 * Register zebra server interface information.
1317 * Send current all interface and address information.
1319 static void zread_interface_add(ZAPI_HANDLER_ARGS
)
1322 struct interface
*ifp
;
1324 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
1325 FOR_ALL_INTERFACES (vrf
, ifp
) {
1326 /* Skip pseudo interface. */
1327 if (!CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_ACTIVE
))
1330 zsend_interface_add(client
, ifp
);
1331 zsend_interface_link_params(client
, ifp
);
1332 zsend_interface_addresses(client
, ifp
);
1337 /* Unregister zebra server interface information. */
1338 static void zread_interface_delete(ZAPI_HANDLER_ARGS
)
1343 * Handle message requesting interface be set up or down.
1345 static void zread_interface_set_protodown(ZAPI_HANDLER_ARGS
)
1348 struct interface
*ifp
;
1351 STREAM_GETL(msg
, ifindex
);
1352 STREAM_GETC(msg
, down
);
1355 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(NS_DEFAULT
), ifindex
);
1358 zlog_info("Setting interface %s (%u): protodown %s", ifp
->name
,
1359 ifindex
, down
? "on" : "off");
1360 zebra_if_set_protodown(ifp
, down
);
1363 "Cannot set protodown %s for interface %u; does not exist",
1364 down
? "on" : "off", ifindex
);
1373 void zserv_nexthop_num_warn(const char *caller
, const struct prefix
*p
,
1374 const unsigned int nexthop_num
)
1376 if (nexthop_num
> zrouter
.multipath_num
) {
1377 char buff
[PREFIX2STR_BUFFER
];
1379 prefix2str(p
, buff
, sizeof(buff
));
1381 EC_ZEBRA_MORE_NH_THAN_MULTIPATH
,
1382 "%s: Prefix %s has %d nexthops, but we can only use the first %d",
1383 caller
, buff
, nexthop_num
, zrouter
.multipath_num
);
1387 static void zread_route_add(ZAPI_HANDLER_ARGS
)
1390 struct zapi_route api
;
1391 struct zapi_nexthop
*api_nh
;
1393 struct prefix_ipv6
*src_p
= NULL
;
1394 struct route_entry
*re
;
1395 struct nexthop
*nexthop
= NULL
;
1397 vrf_id_t vrf_id
= 0;
1398 struct ipaddr vtep_ip
;
1401 if (zapi_route_decode(s
, &api
) < 0) {
1402 if (IS_ZEBRA_DEBUG_RECV
)
1403 zlog_debug("%s: Unable to decode zapi_route sent",
1404 __PRETTY_FUNCTION__
);
1408 if (IS_ZEBRA_DEBUG_RECV
) {
1409 char buf_prefix
[PREFIX_STRLEN
];
1411 prefix2str(&api
.prefix
, buf_prefix
, sizeof(buf_prefix
));
1412 zlog_debug("%s: p=%s, ZAPI_MESSAGE_LABEL: %sset, flags=0x%x",
1413 __func__
, buf_prefix
,
1414 (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_LABEL
) ? ""
1419 /* Allocate new route. */
1420 vrf_id
= zvrf_id(zvrf
);
1421 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
1422 re
->type
= api
.type
;
1423 re
->instance
= api
.instance
;
1424 re
->flags
= api
.flags
;
1425 re
->uptime
= monotime(NULL
);
1426 re
->vrf_id
= vrf_id
;
1427 re
->ng
= nexthop_group_new();
1430 re
->table
= api
.tableid
;
1432 re
->table
= zvrf
->table_id
;
1434 if (!CHECK_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
)
1435 || api
.nexthop_num
== 0) {
1436 flog_warn(EC_ZEBRA_RX_ROUTE_NO_NEXTHOPS
,
1437 "%s: received a route without nexthops for prefix %pFX from client %s",
1438 __func__
, &api
.prefix
,
1439 zebra_route_string(client
->proto
));
1441 nexthop_group_delete(&re
->ng
);
1442 XFREE(MTYPE_RE
, re
);
1447 * TBD should _all_ of the nexthop add operations use
1448 * api_nh->vrf_id instead of re->vrf_id ? I only changed
1449 * for cases NEXTHOP_TYPE_IPV4 and NEXTHOP_TYPE_IPV6.
1451 for (i
= 0; i
< api
.nexthop_num
; i
++) {
1452 api_nh
= &api
.nexthops
[i
];
1453 ifindex_t ifindex
= 0;
1455 if (IS_ZEBRA_DEBUG_RECV
)
1456 zlog_debug("nh type %d", api_nh
->type
);
1458 switch (api_nh
->type
) {
1459 case NEXTHOP_TYPE_IFINDEX
:
1460 nexthop
= route_entry_nexthop_ifindex_add(
1461 re
, api_nh
->ifindex
, api_nh
->vrf_id
);
1463 case NEXTHOP_TYPE_IPV4
:
1464 if (IS_ZEBRA_DEBUG_RECV
) {
1465 char nhbuf
[INET6_ADDRSTRLEN
] = {0};
1467 inet_ntop(AF_INET
, &api_nh
->gate
.ipv4
, nhbuf
,
1469 zlog_debug("%s: nh=%s, vrf_id=%d", __func__
,
1470 nhbuf
, api_nh
->vrf_id
);
1472 nexthop
= route_entry_nexthop_ipv4_add(
1473 re
, &api_nh
->gate
.ipv4
, NULL
, api_nh
->vrf_id
);
1475 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1477 memset(&vtep_ip
, 0, sizeof(struct ipaddr
));
1478 ifindex
= api_nh
->ifindex
;
1479 if (IS_ZEBRA_DEBUG_RECV
) {
1480 char nhbuf
[INET6_ADDRSTRLEN
] = {0};
1482 inet_ntop(AF_INET
, &api_nh
->gate
.ipv4
, nhbuf
,
1485 "%s: nh=%s, vrf_id=%d (re->vrf_id=%d), ifindex=%d",
1486 __func__
, nhbuf
, api_nh
->vrf_id
,
1487 re
->vrf_id
, ifindex
);
1489 nexthop
= route_entry_nexthop_ipv4_ifindex_add(
1490 re
, &api_nh
->gate
.ipv4
, NULL
, ifindex
,
1493 /* Special handling for IPv4 routes sourced from EVPN:
1494 * the nexthop and associated MAC need to be installed.
1496 if (CHECK_FLAG(api
.flags
, ZEBRA_FLAG_EVPN_ROUTE
)) {
1497 vtep_ip
.ipa_type
= IPADDR_V4
;
1498 memcpy(&(vtep_ip
.ipaddr_v4
),
1499 &(api_nh
->gate
.ipv4
),
1500 sizeof(struct in_addr
));
1501 zebra_vxlan_evpn_vrf_route_add(
1502 api_nh
->vrf_id
, &api_nh
->rmac
,
1503 &vtep_ip
, &api
.prefix
);
1506 case NEXTHOP_TYPE_IPV6
:
1507 nexthop
= route_entry_nexthop_ipv6_add(
1508 re
, &api_nh
->gate
.ipv6
, api_nh
->vrf_id
);
1510 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1511 memset(&vtep_ip
, 0, sizeof(struct ipaddr
));
1512 ifindex
= api_nh
->ifindex
;
1513 nexthop
= route_entry_nexthop_ipv6_ifindex_add(
1514 re
, &api_nh
->gate
.ipv6
, ifindex
,
1517 /* Special handling for IPv6 routes sourced from EVPN:
1518 * the nexthop and associated MAC need to be installed.
1520 if (CHECK_FLAG(api
.flags
, ZEBRA_FLAG_EVPN_ROUTE
)) {
1521 vtep_ip
.ipa_type
= IPADDR_V6
;
1522 memcpy(&vtep_ip
.ipaddr_v6
, &(api_nh
->gate
.ipv6
),
1523 sizeof(struct in6_addr
));
1524 zebra_vxlan_evpn_vrf_route_add(
1525 api_nh
->vrf_id
, &api_nh
->rmac
,
1526 &vtep_ip
, &api
.prefix
);
1529 case NEXTHOP_TYPE_BLACKHOLE
:
1530 nexthop
= route_entry_nexthop_blackhole_add(
1531 re
, api_nh
->bh_type
);
1537 EC_ZEBRA_NEXTHOP_CREATION_FAILED
,
1538 "%s: Nexthops Specified: %d but we failed to properly create one",
1539 __PRETTY_FUNCTION__
, api
.nexthop_num
);
1540 nexthop_group_delete(&re
->ng
);
1541 XFREE(MTYPE_RE
, re
);
1545 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
);
1547 /* MPLS labels for BGP-LU or Segment Routing */
1548 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_LABEL
)
1549 && api_nh
->type
!= NEXTHOP_TYPE_IFINDEX
1550 && api_nh
->type
!= NEXTHOP_TYPE_BLACKHOLE
) {
1551 enum lsp_types_t label_type
;
1553 label_type
= lsp_type_from_re_type(client
->proto
);
1555 if (IS_ZEBRA_DEBUG_RECV
) {
1557 "%s: adding %d labels of type %d (1st=%u)",
1558 __func__
, api_nh
->label_num
, label_type
,
1562 nexthop_add_labels(nexthop
, label_type
,
1564 &api_nh
->labels
[0]);
1568 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
))
1569 re
->distance
= api
.distance
;
1570 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
))
1571 re
->metric
= api
.metric
;
1572 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_TAG
))
1574 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_MTU
))
1577 afi
= family2afi(api
.prefix
.family
);
1578 if (afi
!= AFI_IP6
&& CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
)) {
1579 flog_warn(EC_ZEBRA_RX_SRCDEST_WRONG_AFI
,
1580 "%s: Received SRC Prefix but afi is not v6",
1581 __PRETTY_FUNCTION__
);
1582 nexthop_group_delete(&re
->ng
);
1583 XFREE(MTYPE_RE
, re
);
1586 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
))
1587 src_p
= &api
.src_prefix
;
1589 ret
= rib_add_multipath(afi
, api
.safi
, &api
.prefix
, src_p
, re
);
1592 switch (api
.prefix
.family
) {
1595 client
->v4_route_add_cnt
++;
1597 client
->v4_route_upd8_cnt
++;
1601 client
->v6_route_add_cnt
++;
1603 client
->v6_route_upd8_cnt
++;
1608 static void zread_route_del(ZAPI_HANDLER_ARGS
)
1611 struct zapi_route api
;
1613 struct prefix_ipv6
*src_p
= NULL
;
1617 if (zapi_route_decode(s
, &api
) < 0)
1620 afi
= family2afi(api
.prefix
.family
);
1621 if (afi
!= AFI_IP6
&& CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
)) {
1622 flog_warn(EC_ZEBRA_RX_SRCDEST_WRONG_AFI
,
1623 "%s: Received a src prefix while afi is not v6",
1624 __PRETTY_FUNCTION__
);
1627 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
))
1628 src_p
= &api
.src_prefix
;
1631 table_id
= api
.tableid
;
1633 table_id
= zvrf
->table_id
;
1635 rib_delete(afi
, api
.safi
, zvrf_id(zvrf
), api
.type
, api
.instance
,
1636 api
.flags
, &api
.prefix
, src_p
, NULL
, 0, table_id
, api
.metric
,
1637 api
.distance
, false);
1640 switch (api
.prefix
.family
) {
1642 client
->v4_route_del_cnt
++;
1645 client
->v6_route_del_cnt
++;
1650 /* MRIB Nexthop lookup for IPv4. */
1651 static void zread_ipv4_nexthop_lookup_mrib(ZAPI_HANDLER_ARGS
)
1653 struct in_addr addr
;
1654 struct route_entry
*re
;
1656 STREAM_GET(&addr
.s_addr
, msg
, IPV4_MAX_BYTELEN
);
1657 re
= rib_match_ipv4_multicast(zvrf_id(zvrf
), addr
, NULL
);
1658 zsend_ipv4_nexthop_lookup_mrib(client
, addr
, re
, zvrf
);
1664 /* Register zebra server router-id information. Send current router-id */
1665 static void zread_router_id_add(ZAPI_HANDLER_ARGS
)
1669 /* Router-id information is needed. */
1670 vrf_bitmap_set(client
->ridinfo
, zvrf_id(zvrf
));
1672 router_id_get(&p
, zvrf_id(zvrf
));
1674 zsend_router_id_update(client
, &p
, zvrf_id(zvrf
));
1677 /* Unregister zebra server router-id information. */
1678 static void zread_router_id_delete(ZAPI_HANDLER_ARGS
)
1680 vrf_bitmap_unset(client
->ridinfo
, zvrf_id(zvrf
));
1683 static void zsend_capabilities(struct zserv
*client
, struct zebra_vrf
*zvrf
)
1685 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
1687 zclient_create_header(s
, ZEBRA_CAPABILITIES
, zvrf
->vrf
->vrf_id
);
1688 stream_putl(s
, vrf_get_backend());
1689 stream_putc(s
, mpls_enabled
);
1690 stream_putl(s
, zrouter
.multipath_num
);
1691 stream_putc(s
, zebra_mlag_get_role());
1693 stream_putw_at(s
, 0, stream_get_endp(s
));
1694 zserv_send_message(client
, s
);
1697 void zsend_capabilities_all_clients(void)
1699 struct listnode
*node
, *nnode
;
1700 struct zebra_vrf
*zvrf
;
1701 struct zserv
*client
;
1703 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1704 for (ALL_LIST_ELEMENTS(zrouter
.client_list
, node
, nnode
, client
)) {
1705 zsend_capabilities(client
, zvrf
);
1709 /* Tie up route-type and client->sock */
1710 static void zread_hello(ZAPI_HANDLER_ARGS
)
1712 /* type of protocol (lib/zebra.h) */
1714 unsigned short instance
;
1717 STREAM_GETC(msg
, proto
);
1718 STREAM_GETW(msg
, instance
);
1719 STREAM_GETC(msg
, notify
);
1721 client
->notify_owner
= true;
1723 /* accept only dynamic routing protocols */
1724 if ((proto
< ZEBRA_ROUTE_MAX
) && (proto
> ZEBRA_ROUTE_CONNECT
)) {
1726 "client %d says hello and bids fair to announce only %s routes vrf=%u",
1727 client
->sock
, zebra_route_string(proto
),
1730 zlog_notice("client protocol instance %d", instance
);
1732 client
->proto
= proto
;
1733 client
->instance
= instance
;
1736 zsend_capabilities(client
, zvrf
);
1737 zebra_vrf_update_all(client
);
1742 /* Unregister all information in a VRF. */
1743 static void zread_vrf_unregister(ZAPI_HANDLER_ARGS
)
1748 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
1749 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
1750 vrf_bitmap_unset(client
->redist
[afi
][i
], zvrf_id(zvrf
));
1751 vrf_bitmap_unset(client
->redist_default
[afi
], zvrf_id(zvrf
));
1753 vrf_bitmap_unset(client
->ridinfo
, zvrf_id(zvrf
));
1757 * Handle request to create an MPLS LSP.
1759 * A single message can fully specify an LSP with multiple nexthops.
1761 * When the optional ZAPI_LABELS_FTN flag is set, the specified FEC (route) is
1762 * updated to use the received label(s).
1764 static void zread_mpls_labels_add(ZAPI_HANDLER_ARGS
)
1767 struct zapi_labels zl
;
1769 /* Get input stream. */
1771 if (zapi_labels_decode(s
, &zl
) < 0) {
1772 if (IS_ZEBRA_DEBUG_RECV
)
1773 zlog_debug("%s: Unable to decode zapi_labels sent",
1774 __PRETTY_FUNCTION__
);
1781 for (int i
= 0; i
< zl
.nexthop_num
; i
++) {
1782 struct zapi_nexthop_label
*znh
;
1784 znh
= &zl
.nexthops
[i
];
1785 mpls_lsp_install(zvrf
, zl
.type
, zl
.local_label
, znh
->label
,
1786 znh
->type
, &znh
->address
, znh
->ifindex
);
1788 if (CHECK_FLAG(zl
.message
, ZAPI_LABELS_FTN
))
1789 mpls_ftn_update(1, zvrf
, zl
.type
, &zl
.route
.prefix
,
1790 znh
->type
, &znh
->address
, znh
->ifindex
,
1791 zl
.route
.type
, zl
.route
.instance
,
1797 * Handle request to delete an MPLS LSP.
1799 * An LSP is identified by its type and local label. When the received message
1800 * doesn't contain any nexthop, the whole LSP is deleted. Otherwise, only the
1801 * listed LSP nexthops (aka NHLFEs) are deleted.
1803 * When the optional ZAPI_LABELS_FTN flag is set, the labels of the specified
1804 * FEC (route) nexthops are deleted.
1806 static void zread_mpls_labels_delete(ZAPI_HANDLER_ARGS
)
1809 struct zapi_labels zl
;
1811 /* Get input stream. */
1813 if (zapi_labels_decode(s
, &zl
) < 0) {
1814 if (IS_ZEBRA_DEBUG_RECV
)
1815 zlog_debug("%s: Unable to decode zapi_labels sent",
1816 __PRETTY_FUNCTION__
);
1823 if (zl
.nexthop_num
> 0) {
1824 for (int i
= 0; i
< zl
.nexthop_num
; i
++) {
1825 struct zapi_nexthop_label
*znh
;
1827 znh
= &zl
.nexthops
[i
];
1828 mpls_lsp_uninstall(zvrf
, zl
.type
, zl
.local_label
,
1829 znh
->type
, &znh
->address
,
1832 if (CHECK_FLAG(zl
.message
, ZAPI_LABELS_FTN
))
1833 mpls_ftn_update(0, zvrf
, zl
.type
,
1834 &zl
.route
.prefix
, znh
->type
,
1835 &znh
->address
, znh
->ifindex
,
1837 zl
.route
.instance
, znh
->label
);
1840 mpls_lsp_uninstall_all_vrf(zvrf
, zl
.type
, zl
.local_label
);
1842 if (CHECK_FLAG(zl
.message
, ZAPI_LABELS_FTN
))
1843 mpls_ftn_uninstall(zvrf
, zl
.type
, &zl
.route
.prefix
,
1844 zl
.route
.type
, zl
.route
.instance
);
1849 * Handle request to add an MPLS LSP or change an existing one.
1851 * A single message can fully specify an LSP with multiple nexthops.
1853 * When the optional ZAPI_LABELS_FTN flag is set, the specified FEC (route) is
1854 * updated to use the received label(s).
1856 * NOTE: zebra will use route replace semantics (make-before-break) to update
1857 * the LSP in the forwarding plane if that's supported by the underlying
1860 static void zread_mpls_labels_replace(ZAPI_HANDLER_ARGS
)
1863 struct zapi_labels zl
;
1865 /* Get input stream. */
1867 if (zapi_labels_decode(s
, &zl
) < 0) {
1868 if (IS_ZEBRA_DEBUG_RECV
)
1869 zlog_debug("%s: Unable to decode zapi_labels sent",
1870 __PRETTY_FUNCTION__
);
1877 mpls_lsp_uninstall_all_vrf(zvrf
, zl
.type
, zl
.local_label
);
1878 if (CHECK_FLAG(zl
.message
, ZAPI_LABELS_FTN
))
1879 mpls_ftn_uninstall(zvrf
, zl
.type
, &zl
.route
.prefix
,
1880 zl
.route
.type
, zl
.route
.instance
);
1882 for (int i
= 0; i
< zl
.nexthop_num
; i
++) {
1883 struct zapi_nexthop_label
*znh
;
1885 znh
= &zl
.nexthops
[i
];
1886 mpls_lsp_install(zvrf
, zl
.type
, zl
.local_label
, znh
->label
,
1887 znh
->type
, &znh
->address
, znh
->ifindex
);
1889 if (CHECK_FLAG(zl
.message
, ZAPI_LABELS_FTN
)) {
1890 mpls_ftn_update(1, zvrf
, zl
.type
, &zl
.route
.prefix
,
1891 znh
->type
, &znh
->address
, znh
->ifindex
,
1892 zl
.route
.type
, zl
.route
.instance
,
1898 /* Send response to a table manager connect request to client */
1899 static void zread_table_manager_connect(struct zserv
*client
,
1900 struct stream
*msg
, vrf_id_t vrf_id
)
1909 STREAM_GETC(s
, proto
);
1910 STREAM_GETW(s
, instance
);
1912 /* accept only dynamic routing protocols */
1913 if ((proto
>= ZEBRA_ROUTE_MAX
) || (proto
<= ZEBRA_ROUTE_STATIC
)) {
1914 flog_err(EC_ZEBRA_TM_WRONG_PROTO
,
1915 "client %d has wrong protocol %s", client
->sock
,
1916 zebra_route_string(proto
));
1917 zsend_table_manager_connect_response(client
, vrf_id
, 1);
1920 zlog_notice("client %d with vrf %u instance %u connected as %s",
1921 client
->sock
, vrf_id
, instance
, zebra_route_string(proto
));
1922 client
->proto
= proto
;
1923 client
->instance
= instance
;
1926 * Release previous labels of same protocol and instance.
1927 * This is done in case it restarted from an unexpected shutdown.
1929 release_daemon_table_chunks(client
);
1931 zsend_table_manager_connect_response(client
, vrf_id
, 0);
1937 static void zread_label_manager_connect(struct zserv
*client
,
1938 struct stream
*msg
, vrf_id_t vrf_id
)
1941 /* type of protocol (lib/zebra.h) */
1943 unsigned short instance
;
1945 /* Get input stream. */
1949 STREAM_GETC(s
, proto
);
1950 STREAM_GETW(s
, instance
);
1952 /* accept only dynamic routing protocols */
1953 if ((proto
>= ZEBRA_ROUTE_MAX
) || (proto
<= ZEBRA_ROUTE_STATIC
)) {
1954 flog_err(EC_ZEBRA_TM_WRONG_PROTO
,
1955 "client %d has wrong protocol %s", client
->sock
,
1956 zebra_route_string(proto
));
1957 zsend_label_manager_connect_response(client
, vrf_id
, 1);
1961 /* recall proto and instance in this socket */
1962 client
->proto
= proto
;
1963 client
->instance
= instance
;
1965 /* call hook for connection using wrapper */
1966 lm_client_connect_call(proto
, instance
, vrf_id
);
1972 static void zread_get_label_chunk(struct zserv
*client
, struct stream
*msg
,
1977 uint32_t size
, base
;
1978 struct label_manager_chunk
*lmc
= NULL
;
1980 unsigned short instance
;
1982 /* Get input stream. */
1986 STREAM_GETC(s
, proto
);
1987 STREAM_GETW(s
, instance
);
1988 STREAM_GETC(s
, keep
);
1989 STREAM_GETL(s
, size
);
1990 STREAM_GETL(s
, base
);
1992 /* call hook to get a chunk using wrapper */
1993 lm_get_chunk_call(&lmc
, proto
, instance
, keep
, size
, base
, vrf_id
);
1997 EC_ZEBRA_LM_CANNOT_ASSIGN_CHUNK
,
1998 "Unable to assign Label Chunk of size %u to %s instance %u",
1999 size
, zebra_route_string(proto
), instance
);
2001 if (IS_ZEBRA_DEBUG_PACKET
)
2002 zlog_debug("Assigned Label Chunk %u - %u to %s instance %u",
2003 lmc
->start
, lmc
->end
,
2004 zebra_route_string(proto
), instance
);
2010 static void zread_release_label_chunk(struct zserv
*client
, struct stream
*msg
)
2013 uint32_t start
, end
;
2015 unsigned short instance
;
2017 /* Get input stream. */
2021 STREAM_GETC(s
, proto
);
2022 STREAM_GETW(s
, instance
);
2023 STREAM_GETL(s
, start
);
2024 STREAM_GETL(s
, end
);
2026 /* call hook to release a chunk using wrapper */
2027 lm_release_chunk_call(proto
, instance
, start
, end
);
2033 static void zread_label_manager_request(ZAPI_HANDLER_ARGS
)
2035 if (hdr
->command
== ZEBRA_LABEL_MANAGER_CONNECT
2036 || hdr
->command
== ZEBRA_LABEL_MANAGER_CONNECT_ASYNC
)
2037 zread_label_manager_connect(client
, msg
, zvrf_id(zvrf
));
2039 if (hdr
->command
== ZEBRA_GET_LABEL_CHUNK
)
2040 zread_get_label_chunk(client
, msg
, zvrf_id(zvrf
));
2041 else if (hdr
->command
== ZEBRA_RELEASE_LABEL_CHUNK
)
2042 zread_release_label_chunk(client
, msg
);
2046 static void zread_get_table_chunk(struct zserv
*client
, struct stream
*msg
,
2051 struct table_manager_chunk
*tmc
;
2053 /* Get input stream. */
2057 STREAM_GETL(s
, size
);
2059 tmc
= assign_table_chunk(client
->proto
, client
->instance
, size
);
2061 flog_err(EC_ZEBRA_TM_CANNOT_ASSIGN_CHUNK
,
2062 "%s: Unable to assign Table Chunk of size %u",
2065 zlog_debug("Assigned Table Chunk %u - %u", tmc
->start
,
2067 /* send response back */
2068 zsend_assign_table_chunk_response(client
, vrf_id
, tmc
);
2074 static void zread_release_table_chunk(struct zserv
*client
, struct stream
*msg
)
2077 uint32_t start
, end
;
2079 /* Get input stream. */
2083 STREAM_GETL(s
, start
);
2084 STREAM_GETL(s
, end
);
2086 release_table_chunk(client
->proto
, client
->instance
, start
, end
);
2092 static void zread_table_manager_request(ZAPI_HANDLER_ARGS
)
2094 /* to avoid sending other messages like ZERBA_INTERFACE_UP */
2095 if (hdr
->command
== ZEBRA_TABLE_MANAGER_CONNECT
)
2096 zread_table_manager_connect(client
, msg
, zvrf_id(zvrf
));
2098 /* Sanity: don't allow 'unidentified' requests */
2099 if (!client
->proto
) {
2102 "Got table request from an unidentified client");
2105 if (hdr
->command
== ZEBRA_GET_TABLE_CHUNK
)
2106 zread_get_table_chunk(client
, msg
, zvrf_id(zvrf
));
2107 else if (hdr
->command
== ZEBRA_RELEASE_TABLE_CHUNK
)
2108 zread_release_table_chunk(client
, msg
);
2112 static void zread_pseudowire(ZAPI_HANDLER_ARGS
)
2115 char ifname
[IF_NAMESIZE
];
2119 union g_addr nexthop
;
2120 uint32_t local_label
;
2121 uint32_t remote_label
;
2123 union pw_protocol_fields data
;
2125 struct zebra_pw
*pw
;
2127 /* Get input stream. */
2131 STREAM_GET(ifname
, s
, IF_NAMESIZE
);
2132 STREAM_GETL(s
, ifindex
);
2133 STREAM_GETL(s
, type
);
2137 STREAM_GET(&nexthop
.ipv4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
2140 STREAM_GET(&nexthop
.ipv6
, s
, 16);
2145 STREAM_GETL(s
, local_label
);
2146 STREAM_GETL(s
, remote_label
);
2147 STREAM_GETC(s
, flags
);
2148 STREAM_GET(&data
, s
, sizeof(data
));
2149 protocol
= client
->proto
;
2151 pw
= zebra_pw_find(zvrf
, ifname
);
2152 switch (hdr
->command
) {
2155 flog_warn(EC_ZEBRA_PSEUDOWIRE_EXISTS
,
2156 "%s: pseudowire %s already exists [%s]",
2158 zserv_command_string(hdr
->command
));
2162 zebra_pw_add(zvrf
, ifname
, protocol
, client
);
2164 case ZEBRA_PW_DELETE
:
2166 flog_warn(EC_ZEBRA_PSEUDOWIRE_NONEXISTENT
,
2167 "%s: pseudowire %s not found [%s]", __func__
,
2168 ifname
, zserv_command_string(hdr
->command
));
2172 zebra_pw_del(zvrf
, pw
);
2175 case ZEBRA_PW_UNSET
:
2177 flog_warn(EC_ZEBRA_PSEUDOWIRE_NONEXISTENT
,
2178 "%s: pseudowire %s not found [%s]", __func__
,
2179 ifname
, zserv_command_string(hdr
->command
));
2183 switch (hdr
->command
) {
2187 case ZEBRA_PW_UNSET
:
2192 zebra_pw_change(pw
, ifindex
, type
, af
, &nexthop
, local_label
,
2193 remote_label
, flags
, &data
);
2201 static void zread_interface_set_master(ZAPI_HANDLER_ARGS
)
2203 struct interface
*master
;
2204 struct interface
*slave
;
2205 struct stream
*s
= msg
;
2209 STREAM_GETL(s
, vrf_id
);
2210 STREAM_GETL(s
, ifindex
);
2211 master
= if_lookup_by_index(ifindex
, vrf_id
);
2213 STREAM_GETL(s
, vrf_id
);
2214 STREAM_GETL(s
, ifindex
);
2215 slave
= if_lookup_by_index(ifindex
, vrf_id
);
2217 if (!master
|| !slave
)
2220 kernel_interface_set_master(master
, slave
);
2227 static void zread_vrf_label(ZAPI_HANDLER_ARGS
)
2229 struct interface
*ifp
;
2230 mpls_label_t nlabel
;
2233 struct zebra_vrf
*def_zvrf
;
2234 enum lsp_types_t ltype
;
2237 STREAM_GETL(s
, nlabel
);
2238 STREAM_GETC(s
, afi
);
2239 if (nlabel
== zvrf
->label
[afi
]) {
2241 * Nothing to do here move along
2246 STREAM_GETC(s
, ltype
);
2248 if (zvrf
->vrf
->vrf_id
!= VRF_DEFAULT
)
2249 ifp
= if_lookup_by_name(zvrf
->vrf
->name
, zvrf
->vrf
->vrf_id
);
2251 ifp
= if_lookup_by_name("lo", VRF_DEFAULT
);
2254 zlog_debug("Unable to find specified Interface for %s",
2259 def_zvrf
= zebra_vrf_lookup_by_id(VRF_DEFAULT
);
2261 if (zvrf
->label
[afi
] != MPLS_LABEL_NONE
) {
2265 really_remove
= true;
2266 for (scrubber
= AFI_IP
; scrubber
< AFI_MAX
; scrubber
++) {
2267 if (scrubber
== afi
)
2270 if (zvrf
->label
[scrubber
] == MPLS_LABEL_NONE
)
2273 if (zvrf
->label
[afi
] == zvrf
->label
[scrubber
]) {
2274 really_remove
= false;
2280 mpls_lsp_uninstall(def_zvrf
, ltype
, zvrf
->label
[afi
],
2281 NEXTHOP_TYPE_IFINDEX
, NULL
,
2285 if (nlabel
!= MPLS_LABEL_NONE
)
2286 mpls_lsp_install(def_zvrf
, ltype
, nlabel
,
2287 MPLS_LABEL_IMPLICIT_NULL
, NEXTHOP_TYPE_IFINDEX
,
2288 NULL
, ifp
->ifindex
);
2290 zvrf
->label
[afi
] = nlabel
;
2295 static inline void zread_rule(ZAPI_HANDLER_ARGS
)
2297 struct zebra_pbr_rule zpr
;
2302 STREAM_GETL(s
, total
);
2304 for (i
= 0; i
< total
; i
++) {
2305 memset(&zpr
, 0, sizeof(zpr
));
2307 zpr
.sock
= client
->sock
;
2308 zpr
.rule
.vrf_id
= hdr
->vrf_id
;
2309 STREAM_GETL(s
, zpr
.rule
.seq
);
2310 STREAM_GETL(s
, zpr
.rule
.priority
);
2311 STREAM_GETL(s
, zpr
.rule
.unique
);
2312 STREAM_GETC(s
, zpr
.rule
.filter
.src_ip
.family
);
2313 STREAM_GETC(s
, zpr
.rule
.filter
.src_ip
.prefixlen
);
2314 STREAM_GET(&zpr
.rule
.filter
.src_ip
.u
.prefix
, s
,
2315 prefix_blen(&zpr
.rule
.filter
.src_ip
));
2316 STREAM_GETW(s
, zpr
.rule
.filter
.src_port
);
2317 STREAM_GETC(s
, zpr
.rule
.filter
.dst_ip
.family
);
2318 STREAM_GETC(s
, zpr
.rule
.filter
.dst_ip
.prefixlen
);
2319 STREAM_GET(&zpr
.rule
.filter
.dst_ip
.u
.prefix
, s
,
2320 prefix_blen(&zpr
.rule
.filter
.dst_ip
));
2321 STREAM_GETW(s
, zpr
.rule
.filter
.dst_port
);
2322 STREAM_GETL(s
, zpr
.rule
.filter
.fwmark
);
2323 STREAM_GETL(s
, zpr
.rule
.action
.table
);
2324 STREAM_GETL(s
, zpr
.rule
.ifindex
);
2326 if (zpr
.rule
.ifindex
) {
2327 struct interface
*ifp
;
2329 ifp
= if_lookup_by_index_per_ns(zvrf
->zns
,
2332 zlog_debug("Failed to lookup ifindex: %u",
2337 strlcpy(zpr
.ifname
, ifp
->name
, sizeof(zpr
.ifname
));
2340 if (!is_default_prefix(&zpr
.rule
.filter
.src_ip
))
2341 zpr
.rule
.filter
.filter_bm
|= PBR_FILTER_SRC_IP
;
2343 if (!is_default_prefix(&zpr
.rule
.filter
.dst_ip
))
2344 zpr
.rule
.filter
.filter_bm
|= PBR_FILTER_DST_IP
;
2346 if (zpr
.rule
.filter
.src_port
)
2347 zpr
.rule
.filter
.filter_bm
|= PBR_FILTER_SRC_PORT
;
2349 if (zpr
.rule
.filter
.dst_port
)
2350 zpr
.rule
.filter
.filter_bm
|= PBR_FILTER_DST_PORT
;
2352 if (zpr
.rule
.filter
.fwmark
)
2353 zpr
.rule
.filter
.filter_bm
|= PBR_FILTER_FWMARK
;
2355 zpr
.vrf_id
= zvrf
->vrf
->vrf_id
;
2356 if (hdr
->command
== ZEBRA_RULE_ADD
)
2357 zebra_pbr_add_rule(&zpr
);
2359 zebra_pbr_del_rule(&zpr
);
2366 static inline void zread_ipset(ZAPI_HANDLER_ARGS
)
2368 struct zebra_pbr_ipset zpi
;
2373 STREAM_GETL(s
, total
);
2375 for (i
= 0; i
< total
; i
++) {
2376 memset(&zpi
, 0, sizeof(zpi
));
2378 zpi
.sock
= client
->sock
;
2379 zpi
.vrf_id
= zvrf
->vrf
->vrf_id
;
2380 STREAM_GETL(s
, zpi
.unique
);
2381 STREAM_GETL(s
, zpi
.type
);
2382 STREAM_GET(&zpi
.ipset_name
, s
, ZEBRA_IPSET_NAME_SIZE
);
2384 if (hdr
->command
== ZEBRA_IPSET_CREATE
)
2385 zebra_pbr_create_ipset(&zpi
);
2387 zebra_pbr_destroy_ipset(&zpi
);
2394 static inline void zread_ipset_entry(ZAPI_HANDLER_ARGS
)
2396 struct zebra_pbr_ipset_entry zpi
;
2397 struct zebra_pbr_ipset ipset
;
2402 STREAM_GETL(s
, total
);
2404 for (i
= 0; i
< total
; i
++) {
2405 memset(&zpi
, 0, sizeof(zpi
));
2406 memset(&ipset
, 0, sizeof(ipset
));
2408 zpi
.sock
= client
->sock
;
2409 STREAM_GETL(s
, zpi
.unique
);
2410 STREAM_GET(&ipset
.ipset_name
, s
, ZEBRA_IPSET_NAME_SIZE
);
2411 STREAM_GETC(s
, zpi
.src
.family
);
2412 STREAM_GETC(s
, zpi
.src
.prefixlen
);
2413 STREAM_GET(&zpi
.src
.u
.prefix
, s
, prefix_blen(&zpi
.src
));
2414 STREAM_GETC(s
, zpi
.dst
.family
);
2415 STREAM_GETC(s
, zpi
.dst
.prefixlen
);
2416 STREAM_GET(&zpi
.dst
.u
.prefix
, s
, prefix_blen(&zpi
.dst
));
2418 STREAM_GETW(s
, zpi
.src_port_min
);
2419 STREAM_GETW(s
, zpi
.src_port_max
);
2420 STREAM_GETW(s
, zpi
.dst_port_min
);
2421 STREAM_GETW(s
, zpi
.dst_port_max
);
2422 STREAM_GETC(s
, zpi
.proto
);
2423 if (!is_default_prefix(&zpi
.src
))
2424 zpi
.filter_bm
|= PBR_FILTER_SRC_IP
;
2426 if (!is_default_prefix(&zpi
.dst
))
2427 zpi
.filter_bm
|= PBR_FILTER_DST_IP
;
2428 if (zpi
.dst_port_min
!= 0 || zpi
.proto
== IPPROTO_ICMP
)
2429 zpi
.filter_bm
|= PBR_FILTER_DST_PORT
;
2430 if (zpi
.src_port_min
!= 0 || zpi
.proto
== IPPROTO_ICMP
)
2431 zpi
.filter_bm
|= PBR_FILTER_SRC_PORT
;
2432 if (zpi
.dst_port_max
!= 0)
2433 zpi
.filter_bm
|= PBR_FILTER_DST_PORT_RANGE
;
2434 if (zpi
.src_port_max
!= 0)
2435 zpi
.filter_bm
|= PBR_FILTER_SRC_PORT_RANGE
;
2437 zpi
.filter_bm
|= PBR_FILTER_PROTO
;
2439 /* calculate backpointer */
2441 zebra_pbr_lookup_ipset_pername(ipset
.ipset_name
);
2442 if (hdr
->command
== ZEBRA_IPSET_ENTRY_ADD
)
2443 zebra_pbr_add_ipset_entry(&zpi
);
2445 zebra_pbr_del_ipset_entry(&zpi
);
2452 static inline void zread_iptable(ZAPI_HANDLER_ARGS
)
2454 struct zebra_pbr_iptable zpi
;
2459 memset(&zpi
, 0, sizeof(zpi
));
2461 zpi
.interface_name_list
= list_new();
2462 zpi
.sock
= client
->sock
;
2463 zpi
.vrf_id
= zvrf
->vrf
->vrf_id
;
2464 STREAM_GETL(s
, zpi
.unique
);
2465 STREAM_GETL(s
, zpi
.type
);
2466 STREAM_GETL(s
, zpi
.filter_bm
);
2467 STREAM_GETL(s
, zpi
.action
);
2468 STREAM_GETL(s
, zpi
.fwmark
);
2469 STREAM_GET(&zpi
.ipset_name
, s
, ZEBRA_IPSET_NAME_SIZE
);
2470 STREAM_GETW(s
, zpi
.pkt_len_min
);
2471 STREAM_GETW(s
, zpi
.pkt_len_max
);
2472 STREAM_GETW(s
, zpi
.tcp_flags
);
2473 STREAM_GETW(s
, zpi
.tcp_mask_flags
);
2474 STREAM_GETC(s
, zpi
.dscp_value
);
2475 STREAM_GETC(s
, zpi
.fragment
);
2476 STREAM_GETC(s
, zpi
.protocol
);
2477 STREAM_GETL(s
, zpi
.nb_interface
);
2478 zebra_pbr_iptable_update_interfacelist(s
, &zpi
);
2480 if (hdr
->command
== ZEBRA_IPTABLE_ADD
)
2481 zebra_pbr_add_iptable(&zpi
);
2483 zebra_pbr_del_iptable(&zpi
);
2488 void (*zserv_handlers
[])(ZAPI_HANDLER_ARGS
) = {
2489 [ZEBRA_ROUTER_ID_ADD
] = zread_router_id_add
,
2490 [ZEBRA_ROUTER_ID_DELETE
] = zread_router_id_delete
,
2491 [ZEBRA_INTERFACE_ADD
] = zread_interface_add
,
2492 [ZEBRA_INTERFACE_DELETE
] = zread_interface_delete
,
2493 [ZEBRA_INTERFACE_SET_PROTODOWN
] = zread_interface_set_protodown
,
2494 [ZEBRA_ROUTE_ADD
] = zread_route_add
,
2495 [ZEBRA_ROUTE_DELETE
] = zread_route_del
,
2496 [ZEBRA_REDISTRIBUTE_ADD
] = zebra_redistribute_add
,
2497 [ZEBRA_REDISTRIBUTE_DELETE
] = zebra_redistribute_delete
,
2498 [ZEBRA_REDISTRIBUTE_DEFAULT_ADD
] = zebra_redistribute_default_add
,
2499 [ZEBRA_REDISTRIBUTE_DEFAULT_DELETE
] = zebra_redistribute_default_delete
,
2500 [ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB
] = zread_ipv4_nexthop_lookup_mrib
,
2501 [ZEBRA_HELLO
] = zread_hello
,
2502 [ZEBRA_NEXTHOP_REGISTER
] = zread_rnh_register
,
2503 [ZEBRA_NEXTHOP_UNREGISTER
] = zread_rnh_unregister
,
2504 [ZEBRA_IMPORT_ROUTE_REGISTER
] = zread_rnh_register
,
2505 [ZEBRA_IMPORT_ROUTE_UNREGISTER
] = zread_rnh_unregister
,
2506 [ZEBRA_BFD_DEST_UPDATE
] = zebra_ptm_bfd_dst_register
,
2507 [ZEBRA_BFD_DEST_REGISTER
] = zebra_ptm_bfd_dst_register
,
2508 [ZEBRA_BFD_DEST_DEREGISTER
] = zebra_ptm_bfd_dst_deregister
,
2510 [ZEBRA_BFD_DEST_REPLAY
] = zebra_ptm_bfd_dst_replay
,
2511 #endif /* HAVE_BFDD */
2512 [ZEBRA_VRF_UNREGISTER
] = zread_vrf_unregister
,
2513 [ZEBRA_VRF_LABEL
] = zread_vrf_label
,
2514 [ZEBRA_BFD_CLIENT_REGISTER
] = zebra_ptm_bfd_client_register
,
2515 #if defined(HAVE_RTADV)
2516 [ZEBRA_INTERFACE_ENABLE_RADV
] = zebra_interface_radv_enable
,
2517 [ZEBRA_INTERFACE_DISABLE_RADV
] = zebra_interface_radv_disable
,
2519 [ZEBRA_INTERFACE_ENABLE_RADV
] = NULL
,
2520 [ZEBRA_INTERFACE_DISABLE_RADV
] = NULL
,
2522 [ZEBRA_MPLS_LABELS_ADD
] = zread_mpls_labels_add
,
2523 [ZEBRA_MPLS_LABELS_DELETE
] = zread_mpls_labels_delete
,
2524 [ZEBRA_MPLS_LABELS_REPLACE
] = zread_mpls_labels_replace
,
2525 [ZEBRA_IPMR_ROUTE_STATS
] = zebra_ipmr_route_stats
,
2526 [ZEBRA_LABEL_MANAGER_CONNECT
] = zread_label_manager_request
,
2527 [ZEBRA_LABEL_MANAGER_CONNECT_ASYNC
] = zread_label_manager_request
,
2528 [ZEBRA_GET_LABEL_CHUNK
] = zread_label_manager_request
,
2529 [ZEBRA_RELEASE_LABEL_CHUNK
] = zread_label_manager_request
,
2530 [ZEBRA_FEC_REGISTER
] = zread_fec_register
,
2531 [ZEBRA_FEC_UNREGISTER
] = zread_fec_unregister
,
2532 [ZEBRA_ADVERTISE_DEFAULT_GW
] = zebra_vxlan_advertise_gw_macip
,
2533 [ZEBRA_ADVERTISE_SVI_MACIP
] = zebra_vxlan_advertise_svi_macip
,
2534 [ZEBRA_ADVERTISE_SUBNET
] = zebra_vxlan_advertise_subnet
,
2535 [ZEBRA_ADVERTISE_ALL_VNI
] = zebra_vxlan_advertise_all_vni
,
2536 [ZEBRA_REMOTE_VTEP_ADD
] = zebra_vxlan_remote_vtep_add
,
2537 [ZEBRA_REMOTE_VTEP_DEL
] = zebra_vxlan_remote_vtep_del
,
2538 [ZEBRA_REMOTE_MACIP_ADD
] = zebra_vxlan_remote_macip_add
,
2539 [ZEBRA_REMOTE_MACIP_DEL
] = zebra_vxlan_remote_macip_del
,
2540 [ZEBRA_DUPLICATE_ADDR_DETECTION
] = zebra_vxlan_dup_addr_detection
,
2541 [ZEBRA_INTERFACE_SET_MASTER
] = zread_interface_set_master
,
2542 [ZEBRA_PW_ADD
] = zread_pseudowire
,
2543 [ZEBRA_PW_DELETE
] = zread_pseudowire
,
2544 [ZEBRA_PW_SET
] = zread_pseudowire
,
2545 [ZEBRA_PW_UNSET
] = zread_pseudowire
,
2546 [ZEBRA_RULE_ADD
] = zread_rule
,
2547 [ZEBRA_RULE_DELETE
] = zread_rule
,
2548 [ZEBRA_TABLE_MANAGER_CONNECT
] = zread_table_manager_request
,
2549 [ZEBRA_GET_TABLE_CHUNK
] = zread_table_manager_request
,
2550 [ZEBRA_RELEASE_TABLE_CHUNK
] = zread_table_manager_request
,
2551 [ZEBRA_IPSET_CREATE
] = zread_ipset
,
2552 [ZEBRA_IPSET_DESTROY
] = zread_ipset
,
2553 [ZEBRA_IPSET_ENTRY_ADD
] = zread_ipset_entry
,
2554 [ZEBRA_IPSET_ENTRY_DELETE
] = zread_ipset_entry
,
2555 [ZEBRA_IPTABLE_ADD
] = zread_iptable
,
2556 [ZEBRA_IPTABLE_DELETE
] = zread_iptable
,
2557 [ZEBRA_VXLAN_FLOOD_CONTROL
] = zebra_vxlan_flood_control
,
2558 [ZEBRA_VXLAN_SG_REPLAY
] = zebra_vxlan_sg_replay
,
2561 #if defined(HANDLE_ZAPI_FUZZING)
2562 extern struct zebra_privs_t zserv_privs
;
2564 static void zserv_write_incoming(struct stream
*orig
, uint16_t command
)
2566 char fname
[MAXPATHLEN
];
2567 struct stream
*copy
;
2570 copy
= stream_dup(orig
);
2571 stream_set_getp(copy
, 0);
2573 snprintf(fname
, MAXPATHLEN
, "%s/%u", frr_vtydir
, command
);
2575 frr_with_privs(&zserv_privs
) {
2576 fd
= open(fname
, O_CREAT
| O_WRONLY
| O_EXCL
, 0644);
2578 stream_flush(copy
, fd
);
2584 void zserv_handle_commands(struct zserv
*client
, struct stream
*msg
)
2587 struct zebra_vrf
*zvrf
;
2589 zapi_parse_header(msg
, &hdr
);
2591 if (IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
)
2592 zserv_log_message(NULL
, msg
, &hdr
);
2594 #if defined(HANDLE_ZAPI_FUZZING)
2595 zserv_write_incoming(msg
, hdr
.command
);
2598 hdr
.length
-= ZEBRA_HEADER_SIZE
;
2601 zvrf
= zebra_vrf_lookup_by_id(hdr
.vrf_id
);
2603 if (IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
)
2604 zlog_debug("ZAPI message specifies unknown VRF: %d",
2609 if (hdr
.command
>= array_size(zserv_handlers
)
2610 || zserv_handlers
[hdr
.command
] == NULL
)
2611 zlog_info("Zebra received unknown command %d", hdr
.command
);
2613 zserv_handlers
[hdr
.command
](client
, &hdr
, msg
, zvrf
);