1 /* Zebra daemon server routine.
2 * Copyright (C) 1997, 98, 99 Kunihiro Ishiguro
4 * This file is part of GNU Zebra.
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
32 #include "zebra_memory.h"
36 #include "sockunion.h"
47 #include "zebra/zserv.h"
48 #include "zebra/zebra_ns.h"
49 #include "zebra/zebra_vrf.h"
50 #include "zebra/router-id.h"
51 #include "zebra/redistribute.h"
52 #include "zebra/debug.h"
53 #include "zebra/zebra_rnh.h"
54 #include "zebra/rt_netlink.h"
55 #include "zebra/interface.h"
56 #include "zebra/zebra_ptm.h"
57 #include "zebra/rtadv.h"
58 #include "zebra/zebra_mpls.h"
59 #include "zebra/zebra_mroute.h"
60 #include "zebra/label_manager.h"
61 #include "zebra/zebra_vxlan.h"
63 #include "zebra/zebra_pbr.h"
65 /* Event list of zebra. */
66 enum event
{ ZEBRA_READ
, ZEBRA_WRITE
};
68 extern struct zebra_privs_t zserv_privs
;
69 /* post event into client */
70 static void zebra_event(struct zserv
*client
, enum event event
);
73 /* Public interface ======================================================== */
75 int zebra_server_send_message(struct zserv
*client
, struct stream
*msg
)
77 stream_fifo_push(client
->obuf_fifo
, msg
);
78 zebra_event(client
, ZEBRA_WRITE
);
82 /* Encoding helpers -------------------------------------------------------- */
84 static void zserv_encode_interface(struct stream
*s
, struct interface
*ifp
)
86 /* Interface information. */
87 stream_put(s
, ifp
->name
, INTERFACE_NAMSIZ
);
88 stream_putl(s
, ifp
->ifindex
);
89 stream_putc(s
, ifp
->status
);
90 stream_putq(s
, ifp
->flags
);
91 stream_putc(s
, ifp
->ptm_enable
);
92 stream_putc(s
, ifp
->ptm_status
);
93 stream_putl(s
, ifp
->metric
);
94 stream_putl(s
, ifp
->speed
);
95 stream_putl(s
, ifp
->mtu
);
96 stream_putl(s
, ifp
->mtu6
);
97 stream_putl(s
, ifp
->bandwidth
);
98 stream_putl(s
, ifp
->ll_type
);
99 stream_putl(s
, ifp
->hw_addr_len
);
100 if (ifp
->hw_addr_len
)
101 stream_put(s
, ifp
->hw_addr
, ifp
->hw_addr_len
);
103 /* Then, Traffic Engineering parameters if any */
104 if (HAS_LINK_PARAMS(ifp
) && IS_LINK_PARAMS_SET(ifp
->link_params
)) {
106 zebra_interface_link_params_write(s
, ifp
);
110 /* Write packet size. */
111 stream_putw_at(s
, 0, stream_get_endp(s
));
114 static void zserv_encode_vrf(struct stream
*s
, struct zebra_vrf
*zvrf
)
116 struct vrf_data data
;
117 const char *netns_name
= zvrf_ns_name(zvrf
);
119 data
.l
.table_id
= zvrf
->table_id
;
122 strlcpy(data
.l
.netns_name
, basename((char *)netns_name
),
125 memset(data
.l
.netns_name
, 0, NS_NAMSIZ
);
126 /* Pass the tableid and the netns NAME */
127 stream_put(s
, &data
, sizeof(struct vrf_data
));
128 /* Interface information. */
129 stream_put(s
, zvrf_name(zvrf
), VRF_NAMSIZ
);
130 /* Write packet size. */
131 stream_putw_at(s
, 0, stream_get_endp(s
));
134 static int zserv_encode_nexthop(struct stream
*s
, struct nexthop
*nexthop
)
136 stream_putc(s
, nexthop
->type
);
137 switch (nexthop
->type
) {
138 case NEXTHOP_TYPE_IPV4
:
139 case NEXTHOP_TYPE_IPV4_IFINDEX
:
140 stream_put_in_addr(s
, &nexthop
->gate
.ipv4
);
141 stream_putl(s
, nexthop
->ifindex
);
143 case NEXTHOP_TYPE_IPV6
:
144 stream_put(s
, &nexthop
->gate
.ipv6
, 16);
146 case NEXTHOP_TYPE_IPV6_IFINDEX
:
147 stream_put(s
, &nexthop
->gate
.ipv6
, 16);
148 stream_putl(s
, nexthop
->ifindex
);
150 case NEXTHOP_TYPE_IFINDEX
:
151 stream_putl(s
, nexthop
->ifindex
);
160 /* Send handlers ----------------------------------------------------------- */
162 /* Interface is added. Send ZEBRA_INTERFACE_ADD to client. */
164 * This function is called in the following situations:
165 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
167 * - at startup, when zebra figures out the available interfaces
168 * - when an interface is added (where support for
169 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
170 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
173 int zsend_interface_add(struct zserv
*client
, struct interface
*ifp
)
175 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
177 zclient_create_header(s
, ZEBRA_INTERFACE_ADD
, ifp
->vrf_id
);
178 zserv_encode_interface(s
, ifp
);
181 return zebra_server_send_message(client
, s
);
184 /* Interface deletion from zebra daemon. */
185 int zsend_interface_delete(struct zserv
*client
, struct interface
*ifp
)
187 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
189 zclient_create_header(s
, ZEBRA_INTERFACE_DELETE
, ifp
->vrf_id
);
190 zserv_encode_interface(s
, ifp
);
193 return zebra_server_send_message(client
, s
);
196 int zsend_vrf_add(struct zserv
*client
, struct zebra_vrf
*zvrf
)
198 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
200 zclient_create_header(s
, ZEBRA_VRF_ADD
, zvrf_id(zvrf
));
201 zserv_encode_vrf(s
, zvrf
);
203 client
->vrfadd_cnt
++;
204 return zebra_server_send_message(client
, s
);
207 /* VRF deletion from zebra daemon. */
208 int zsend_vrf_delete(struct zserv
*client
, struct zebra_vrf
*zvrf
)
211 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
213 zclient_create_header(s
, ZEBRA_VRF_DELETE
, zvrf_id(zvrf
));
214 zserv_encode_vrf(s
, zvrf
);
216 client
->vrfdel_cnt
++;
217 return zebra_server_send_message(client
, s
);
220 int zsend_interface_link_params(struct zserv
*client
, struct interface
*ifp
)
222 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
224 /* Check this client need interface information. */
225 if (!client
->ifinfo
) {
230 if (!ifp
->link_params
) {
235 zclient_create_header(s
, ZEBRA_INTERFACE_LINK_PARAMS
, ifp
->vrf_id
);
237 /* Add Interface Index */
238 stream_putl(s
, ifp
->ifindex
);
240 /* Then TE Link Parameters */
241 if (zebra_interface_link_params_write(s
, ifp
) == 0) {
246 /* Write packet size. */
247 stream_putw_at(s
, 0, stream_get_endp(s
));
249 return zebra_server_send_message(client
, s
);
252 /* Interface address is added/deleted. Send ZEBRA_INTERFACE_ADDRESS_ADD or
253 * ZEBRA_INTERFACE_ADDRESS_DELETE to the client.
255 * A ZEBRA_INTERFACE_ADDRESS_ADD is sent in the following situations:
256 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
257 * from the client, after the ZEBRA_INTERFACE_ADD has been
258 * sent from zebra to the client
259 * - redistribute new address info to all clients in the following situations
260 * - at startup, when zebra figures out the available interfaces
261 * - when an interface is added (where support for
262 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
263 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
265 * - for the vty commands "ip address A.B.C.D/M [<secondary>|<label LINE>]"
266 * and "no bandwidth <1-10000000>", "ipv6 address X:X::X:X/M"
267 * - when an RTM_NEWADDR message is received from the kernel,
269 * The call tree that triggers ZEBRA_INTERFACE_ADDRESS_DELETE:
271 * zsend_interface_address(DELETE)
274 * zebra_interface_address_delete_update
276 * | | if_delete_update
278 * ip_address_uninstall connected_delete_ipv4
279 * [ipv6_addresss_uninstall] [connected_delete_ipv6]
282 * | RTM_NEWADDR on routing/netlink socket
285 * "no ip address A.B.C.D/M [label LINE]"
286 * "no ip address A.B.C.D/M secondary"
287 * ["no ipv6 address X:X::X:X/M"]
290 int zsend_interface_address(int cmd
, struct zserv
*client
,
291 struct interface
*ifp
, struct connected
*ifc
)
295 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
297 zclient_create_header(s
, cmd
, ifp
->vrf_id
);
298 stream_putl(s
, ifp
->ifindex
);
300 /* Interface address flag. */
301 stream_putc(s
, ifc
->flags
);
303 /* Prefix information. */
305 stream_putc(s
, p
->family
);
306 blen
= prefix_blen(p
);
307 stream_put(s
, &p
->u
.prefix
, blen
);
310 * XXX gnu version does not send prefixlen for
311 * ZEBRA_INTERFACE_ADDRESS_DELETE
312 * but zebra_interface_address_delete_read() in the gnu version
315 stream_putc(s
, p
->prefixlen
);
318 p
= ifc
->destination
;
320 stream_put(s
, &p
->u
.prefix
, blen
);
322 stream_put(s
, NULL
, blen
);
324 /* Write packet size. */
325 stream_putw_at(s
, 0, stream_get_endp(s
));
327 client
->connected_rt_add_cnt
++;
328 return zebra_server_send_message(client
, s
);
331 static int zsend_interface_nbr_address(int cmd
, struct zserv
*client
,
332 struct interface
*ifp
,
333 struct nbr_connected
*ifc
)
336 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
339 zclient_create_header(s
, cmd
, ifp
->vrf_id
);
340 stream_putl(s
, ifp
->ifindex
);
342 /* Prefix information. */
344 stream_putc(s
, p
->family
);
345 blen
= prefix_blen(p
);
346 stream_put(s
, &p
->u
.prefix
, blen
);
349 * XXX gnu version does not send prefixlen for
350 * ZEBRA_INTERFACE_ADDRESS_DELETE
351 * but zebra_interface_address_delete_read() in the gnu version
354 stream_putc(s
, p
->prefixlen
);
356 /* Write packet size. */
357 stream_putw_at(s
, 0, stream_get_endp(s
));
359 return zebra_server_send_message(client
, s
);
362 /* Interface address addition. */
363 static void zebra_interface_nbr_address_add_update(struct interface
*ifp
,
364 struct nbr_connected
*ifc
)
366 struct listnode
*node
, *nnode
;
367 struct zserv
*client
;
370 if (IS_ZEBRA_DEBUG_EVENT
) {
371 char buf
[INET6_ADDRSTRLEN
];
375 "MESSAGE: ZEBRA_INTERFACE_NBR_ADDRESS_ADD %s/%d on %s",
376 inet_ntop(p
->family
, &p
->u
.prefix
, buf
,
378 p
->prefixlen
, ifc
->ifp
->name
);
381 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, node
, nnode
, client
))
382 zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_ADD
,
386 /* Interface address deletion. */
387 static void zebra_interface_nbr_address_delete_update(struct interface
*ifp
,
388 struct nbr_connected
*ifc
)
390 struct listnode
*node
, *nnode
;
391 struct zserv
*client
;
394 if (IS_ZEBRA_DEBUG_EVENT
) {
395 char buf
[INET6_ADDRSTRLEN
];
399 "MESSAGE: ZEBRA_INTERFACE_NBR_ADDRESS_DELETE %s/%d on %s",
400 inet_ntop(p
->family
, &p
->u
.prefix
, buf
,
402 p
->prefixlen
, ifc
->ifp
->name
);
405 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, node
, nnode
, client
))
406 zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
,
410 /* Send addresses on interface to client */
411 int zsend_interface_addresses(struct zserv
*client
, struct interface
*ifp
)
413 struct listnode
*cnode
, *cnnode
;
415 struct nbr_connected
*nc
;
417 /* Send interface addresses. */
418 for (ALL_LIST_ELEMENTS(ifp
->connected
, cnode
, cnnode
, c
)) {
419 if (!CHECK_FLAG(c
->conf
, ZEBRA_IFC_REAL
))
422 if (zsend_interface_address(ZEBRA_INTERFACE_ADDRESS_ADD
, client
,
428 /* Send interface neighbors. */
429 for (ALL_LIST_ELEMENTS(ifp
->nbr_connected
, cnode
, cnnode
, nc
)) {
430 if (zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_ADD
,
439 /* Notify client about interface moving from one VRF to another.
440 * Whether client is interested in old and new VRF is checked by caller.
442 int zsend_interface_vrf_update(struct zserv
*client
, struct interface
*ifp
,
445 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
447 zclient_create_header(s
, ZEBRA_INTERFACE_VRF_UPDATE
, ifp
->vrf_id
);
449 /* Fill in the ifIndex of the interface and its new VRF (id) */
450 stream_putl(s
, ifp
->ifindex
);
451 stream_putl(s
, vrf_id
);
453 /* Write packet size. */
454 stream_putw_at(s
, 0, stream_get_endp(s
));
456 client
->if_vrfchg_cnt
++;
457 return zebra_server_send_message(client
, s
);
460 /* Add new nbr connected IPv6 address */
461 void nbr_connected_add_ipv6(struct interface
*ifp
, struct in6_addr
*address
)
463 struct nbr_connected
*ifc
;
467 IPV6_ADDR_COPY(&p
.u
.prefix
, address
);
468 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
470 if (!(ifc
= listnode_head(ifp
->nbr_connected
))) {
472 ifc
= nbr_connected_new();
473 ifc
->address
= prefix_new();
475 listnode_add(ifp
->nbr_connected
, ifc
);
478 prefix_copy(ifc
->address
, &p
);
480 zebra_interface_nbr_address_add_update(ifp
, ifc
);
482 if_nbr_ipv6ll_to_ipv4ll_neigh_update(ifp
, address
, 1);
485 void nbr_connected_delete_ipv6(struct interface
*ifp
, struct in6_addr
*address
)
487 struct nbr_connected
*ifc
;
491 IPV6_ADDR_COPY(&p
.u
.prefix
, address
);
492 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
494 ifc
= nbr_connected_check(ifp
, &p
);
498 listnode_delete(ifp
->nbr_connected
, ifc
);
500 zebra_interface_nbr_address_delete_update(ifp
, ifc
);
502 if_nbr_ipv6ll_to_ipv4ll_neigh_update(ifp
, address
, 0);
504 nbr_connected_free(ifc
);
508 * The cmd passed to zsend_interface_update may be ZEBRA_INTERFACE_UP or
509 * ZEBRA_INTERFACE_DOWN.
511 * The ZEBRA_INTERFACE_UP message is sent from the zebra server to
512 * the clients in one of 2 situations:
513 * - an if_up is detected e.g., as a result of an RTM_IFINFO message
514 * - a vty command modifying the bandwidth of an interface is received.
515 * The ZEBRA_INTERFACE_DOWN message is sent when an if_down is detected.
517 int zsend_interface_update(int cmd
, struct zserv
*client
, struct interface
*ifp
)
519 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
521 zclient_create_header(s
, cmd
, ifp
->vrf_id
);
522 zserv_encode_interface(s
, ifp
);
524 if (cmd
== ZEBRA_INTERFACE_UP
)
527 client
->ifdown_cnt
++;
529 return zebra_server_send_message(client
, s
);
532 int zsend_redistribute_route(int cmd
, struct zserv
*client
, struct prefix
*p
,
533 struct prefix
*src_p
, struct route_entry
*re
)
535 struct zapi_route api
;
536 struct zapi_nexthop
*api_nh
;
537 struct nexthop
*nexthop
;
540 memset(&api
, 0, sizeof(api
));
541 api
.vrf_id
= re
->vrf_id
;
543 api
.instance
= re
->instance
;
544 api
.flags
= re
->flags
;
549 SET_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
);
550 memcpy(&api
.src_prefix
, src_p
, sizeof(api
.src_prefix
));
554 if (re
->nexthop_active_num
) {
555 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
556 api
.nexthop_num
= re
->nexthop_active_num
;
558 for (nexthop
= re
->ng
.nexthop
; nexthop
; nexthop
= nexthop
->next
) {
559 if (!CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
562 api_nh
= &api
.nexthops
[count
];
563 api_nh
->vrf_id
= nexthop
->vrf_id
;
564 api_nh
->type
= nexthop
->type
;
565 switch (nexthop
->type
) {
566 case NEXTHOP_TYPE_BLACKHOLE
:
567 api_nh
->bh_type
= nexthop
->bh_type
;
569 case NEXTHOP_TYPE_IPV4
:
570 api_nh
->gate
.ipv4
= nexthop
->gate
.ipv4
;
572 case NEXTHOP_TYPE_IPV4_IFINDEX
:
573 api_nh
->gate
.ipv4
= nexthop
->gate
.ipv4
;
574 api_nh
->ifindex
= nexthop
->ifindex
;
576 case NEXTHOP_TYPE_IFINDEX
:
577 api_nh
->ifindex
= nexthop
->ifindex
;
579 case NEXTHOP_TYPE_IPV6
:
580 api_nh
->gate
.ipv6
= nexthop
->gate
.ipv6
;
582 case NEXTHOP_TYPE_IPV6_IFINDEX
:
583 api_nh
->gate
.ipv6
= nexthop
->gate
.ipv6
;
584 api_nh
->ifindex
= nexthop
->ifindex
;
590 SET_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
);
591 api
.distance
= re
->distance
;
592 SET_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
);
593 api
.metric
= re
->metric
;
595 SET_FLAG(api
.message
, ZAPI_MESSAGE_TAG
);
598 SET_FLAG(api
.message
, ZAPI_MESSAGE_MTU
);
601 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
603 /* Encode route and send. */
604 if (zapi_route_encode(cmd
, s
, &api
) < 0) {
609 if (IS_ZEBRA_DEBUG_SEND
) {
610 char buf_prefix
[PREFIX_STRLEN
];
611 prefix2str(&api
.prefix
, buf_prefix
, sizeof(buf_prefix
));
613 zlog_debug("%s: %s to client %s: type %s, vrf_id %d, p %s",
614 __func__
, zserv_command_string(cmd
),
615 zebra_route_string(client
->proto
),
616 zebra_route_string(api
.type
), api
.vrf_id
,
619 return zebra_server_send_message(client
, s
);
623 * Modified version of zsend_ipv4_nexthop_lookup(): Query unicast rib if
624 * nexthop is not found on mrib. Returns both route metric and protocol
627 static int zsend_ipv4_nexthop_lookup_mrib(struct zserv
*client
,
629 struct route_entry
*re
,
630 struct zebra_vrf
*zvrf
)
635 struct nexthop
*nexthop
;
637 /* Get output stream. */
638 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
641 /* Fill in result. */
642 zclient_create_header(s
, ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB
, zvrf_id(zvrf
));
643 stream_put_in_addr(s
, &addr
);
646 stream_putc(s
, re
->distance
);
647 stream_putl(s
, re
->metric
);
649 nump
= stream_get_endp(
650 s
); /* remember position for nexthop_num */
651 stream_putc(s
, 0); /* reserve room for nexthop_num */
652 /* Only non-recursive routes are elegible to resolve the nexthop
654 * are looking up. Therefore, we will just iterate over the top
655 * chain of nexthops. */
656 for (nexthop
= re
->ng
.nexthop
; nexthop
; nexthop
= nexthop
->next
)
657 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
658 num
+= zserv_encode_nexthop(s
, nexthop
);
660 stream_putc_at(s
, nump
, num
); /* store nexthop_num */
662 stream_putc(s
, 0); /* distance */
663 stream_putl(s
, 0); /* metric */
664 stream_putc(s
, 0); /* nexthop_num */
667 stream_putw_at(s
, 0, stream_get_endp(s
));
669 return zebra_server_send_message(client
, s
);
672 int zsend_route_notify_owner(struct route_entry
*re
, struct prefix
*p
,
673 enum zapi_route_notify_owner note
)
675 struct zserv
*client
;
679 client
= zebra_find_client(re
->type
, re
->instance
);
680 if (!client
|| !client
->notify_owner
) {
681 if (IS_ZEBRA_DEBUG_PACKET
) {
682 char buff
[PREFIX_STRLEN
];
685 "Not Notifying Owner: %u about prefix %s(%u) %d vrf: %u",
686 re
->type
, prefix2str(p
, buff
, sizeof(buff
)),
687 re
->table
, note
, re
->vrf_id
);
692 if (IS_ZEBRA_DEBUG_PACKET
) {
693 char buff
[PREFIX_STRLEN
];
695 zlog_debug("Notifying Owner: %u about prefix %s(%u) %d vrf: %u",
696 re
->type
, prefix2str(p
, buff
, sizeof(buff
)),
697 re
->table
, note
, re
->vrf_id
);
700 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
703 zclient_create_header(s
, ZEBRA_ROUTE_NOTIFY_OWNER
, re
->vrf_id
);
705 stream_put(s
, ¬e
, sizeof(note
));
707 stream_putc(s
, p
->family
);
709 blen
= prefix_blen(p
);
710 stream_putc(s
, p
->prefixlen
);
711 stream_put(s
, &p
->u
.prefix
, blen
);
713 stream_putl(s
, re
->table
);
715 stream_putw_at(s
, 0, stream_get_endp(s
));
717 return zebra_server_send_message(client
, s
);
720 void zsend_rule_notify_owner(struct zebra_pbr_rule
*rule
,
721 enum zapi_rule_notify_owner note
)
723 struct listnode
*node
;
724 struct zserv
*client
;
727 if (IS_ZEBRA_DEBUG_PACKET
) {
728 zlog_debug("%s: Notifying %u", __PRETTY_FUNCTION__
,
732 for (ALL_LIST_ELEMENTS_RO(zebrad
.client_list
, node
, client
)) {
733 if (rule
->sock
== client
->sock
)
740 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
743 zclient_create_header(s
, ZEBRA_RULE_NOTIFY_OWNER
, VRF_DEFAULT
);
744 stream_put(s
, ¬e
, sizeof(note
));
745 stream_putl(s
, rule
->seq
);
746 stream_putl(s
, rule
->priority
);
747 stream_putl(s
, rule
->unique
);
749 stream_putl(s
, rule
->ifp
->ifindex
);
753 stream_putw_at(s
, 0, stream_get_endp(s
));
755 zebra_server_send_message(client
, s
);
758 /* Router-id is updated. Send ZEBRA_ROUTER_ID_ADD to client. */
759 int zsend_router_id_update(struct zserv
*client
, struct prefix
*p
,
764 /* Check this client need interface information. */
765 if (!vrf_bitmap_check(client
->ridinfo
, vrf_id
))
768 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
771 zclient_create_header(s
, ZEBRA_ROUTER_ID_UPDATE
, vrf_id
);
773 /* Prefix information. */
774 stream_putc(s
, p
->family
);
775 blen
= prefix_blen(p
);
776 stream_put(s
, &p
->u
.prefix
, blen
);
777 stream_putc(s
, p
->prefixlen
);
779 /* Write packet size. */
780 stream_putw_at(s
, 0, stream_get_endp(s
));
782 return zebra_server_send_message(client
, s
);
786 * Function used by Zebra to send a PW status update to LDP daemon
788 int zsend_pw_update(struct zserv
*client
, struct zebra_pw
*pw
)
790 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
792 zclient_create_header(s
, ZEBRA_PW_STATUS_UPDATE
, pw
->vrf_id
);
793 stream_write(s
, pw
->ifname
, IF_NAMESIZE
);
794 stream_putl(s
, pw
->ifindex
);
795 stream_putl(s
, pw
->status
);
797 /* Put length at the first point of the stream. */
798 stream_putw_at(s
, 0, stream_get_endp(s
));
800 return zebra_server_send_message(client
, s
);
803 /* Send response to a get label chunk request to client */
804 static int zsend_assign_label_chunk_response(struct zserv
*client
,
806 struct label_manager_chunk
*lmc
)
809 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
811 zclient_create_header(s
, ZEBRA_GET_LABEL_CHUNK
, vrf_id
);
815 stream_putc(s
, lmc
->keep
);
816 /* start and end labels */
817 stream_putl(s
, lmc
->start
);
818 stream_putl(s
, lmc
->end
);
821 /* Write packet size. */
822 stream_putw_at(s
, 0, stream_get_endp(s
));
824 ret
= writen(client
->sock
, s
->data
, stream_get_endp(s
));
829 /* Send response to a label manager connect request to client */
830 static int zsend_label_manager_connect_response(struct zserv
*client
,
831 vrf_id_t vrf_id
, u_short result
)
834 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
836 zclient_create_header(s
, ZEBRA_LABEL_MANAGER_CONNECT
, vrf_id
);
839 stream_putc(s
, result
);
841 /* Write packet size. */
842 stream_putw_at(s
, 0, stream_get_endp(s
));
844 ret
= writen(client
->sock
, s
->data
, stream_get_endp(s
));
850 /* Inbound message handling ------------------------------------------------ */
853 [ZEBRA_NEXTHOP_REGISTER
] = RNH_NEXTHOP_TYPE
,
854 [ZEBRA_NEXTHOP_UNREGISTER
] = RNH_NEXTHOP_TYPE
,
855 [ZEBRA_IMPORT_ROUTE_REGISTER
] = RNH_IMPORT_CHECK_TYPE
,
856 [ZEBRA_IMPORT_ROUTE_UNREGISTER
] = RNH_IMPORT_CHECK_TYPE
,
859 /* Nexthop register */
860 static void zread_rnh_register(ZAPI_HANDLER_ARGS
)
867 uint16_t type
= cmd2type
[hdr
->command
];
869 if (IS_ZEBRA_DEBUG_NHT
)
871 "rnh_register msg from client %s: hdr->length=%d, type=%s vrf=%u\n",
872 zebra_route_string(client
->proto
), hdr
->length
,
873 (type
== RNH_NEXTHOP_TYPE
) ? "nexthop" : "route",
878 client
->nh_reg_time
= monotime(NULL
);
880 while (l
< hdr
->length
) {
881 STREAM_GETC(s
, flags
);
882 STREAM_GETW(s
, p
.family
);
883 STREAM_GETC(s
, p
.prefixlen
);
885 if (p
.family
== AF_INET
) {
886 if (p
.prefixlen
> IPV4_MAX_BITLEN
) {
888 "%s: Specified prefix hdr->length %d is too large for a v4 address",
889 __PRETTY_FUNCTION__
, p
.prefixlen
);
892 STREAM_GET(&p
.u
.prefix4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
893 l
+= IPV4_MAX_BYTELEN
;
894 } else if (p
.family
== AF_INET6
) {
895 if (p
.prefixlen
> IPV6_MAX_BITLEN
) {
897 "%s: Specified prefix hdr->length %d is to large for a v6 address",
898 __PRETTY_FUNCTION__
, p
.prefixlen
);
901 STREAM_GET(&p
.u
.prefix6
, s
, IPV6_MAX_BYTELEN
);
902 l
+= IPV6_MAX_BYTELEN
;
905 "rnh_register: Received unknown family type %d\n",
909 rnh
= zebra_add_rnh(&p
, zvrf_id(zvrf
), type
);
910 if (type
== RNH_NEXTHOP_TYPE
) {
912 && !CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
))
913 SET_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
);
915 && CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
))
916 UNSET_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
);
917 } else if (type
== RNH_IMPORT_CHECK_TYPE
) {
919 && !CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
))
920 SET_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
);
922 && CHECK_FLAG(rnh
->flags
,
923 ZEBRA_NHT_EXACT_MATCH
))
924 UNSET_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
);
927 zebra_add_rnh_client(rnh
, client
, type
, zvrf_id(zvrf
));
928 /* Anything not AF_INET/INET6 has been filtered out above */
929 zebra_evaluate_rnh(zvrf_id(zvrf
), p
.family
, 1, type
, &p
);
936 /* Nexthop register */
937 static void zread_rnh_unregister(ZAPI_HANDLER_ARGS
)
943 uint16_t type
= cmd2type
[hdr
->command
];
945 if (IS_ZEBRA_DEBUG_NHT
)
947 "rnh_unregister msg from client %s: hdr->length=%d vrf: %u\n",
948 zebra_route_string(client
->proto
), hdr
->length
,
953 while (l
< hdr
->length
) {
956 STREAM_GETC(s
, flags
);
960 STREAM_GETW(s
, p
.family
);
961 STREAM_GETC(s
, p
.prefixlen
);
963 if (p
.family
== AF_INET
) {
964 if (p
.prefixlen
> IPV4_MAX_BITLEN
) {
966 "%s: Specified prefix hdr->length %d is to large for a v4 address",
967 __PRETTY_FUNCTION__
, p
.prefixlen
);
970 STREAM_GET(&p
.u
.prefix4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
971 l
+= IPV4_MAX_BYTELEN
;
972 } else if (p
.family
== AF_INET6
) {
973 if (p
.prefixlen
> IPV6_MAX_BITLEN
) {
975 "%s: Specified prefix hdr->length %d is to large for a v6 address",
976 __PRETTY_FUNCTION__
, p
.prefixlen
);
979 STREAM_GET(&p
.u
.prefix6
, s
, IPV6_MAX_BYTELEN
);
980 l
+= IPV6_MAX_BYTELEN
;
983 "rnh_register: Received unknown family type %d\n",
987 rnh
= zebra_lookup_rnh(&p
, zvrf_id(zvrf
), type
);
989 client
->nh_dereg_time
= monotime(NULL
);
990 zebra_remove_rnh_client(rnh
, client
, type
);
997 #define ZEBRA_MIN_FEC_LENGTH 5
1000 static void zread_fec_register(ZAPI_HANDLER_ARGS
)
1006 uint32_t label_index
= MPLS_INVALID_LABEL_INDEX
;
1009 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1011 return; // unexpected
1014 * The minimum amount of data that can be sent for one fec
1017 if (hdr
->length
< ZEBRA_MIN_FEC_LENGTH
) {
1019 "fec_register: Received a fec register of hdr->length %d, it is of insufficient size to properly decode",
1024 while (l
< hdr
->length
) {
1025 STREAM_GETW(s
, flags
);
1026 memset(&p
, 0, sizeof(p
));
1027 STREAM_GETW(s
, p
.family
);
1028 if (p
.family
!= AF_INET
&& p
.family
!= AF_INET6
) {
1030 "fec_register: Received unknown family type %d\n",
1034 STREAM_GETC(s
, p
.prefixlen
);
1035 if ((p
.family
== AF_INET
&& p
.prefixlen
> IPV4_MAX_BITLEN
)
1036 || (p
.family
== AF_INET6
1037 && p
.prefixlen
> IPV6_MAX_BITLEN
)) {
1039 "%s: Specified prefix hdr->length: %d is to long for %d",
1040 __PRETTY_FUNCTION__
, p
.prefixlen
, p
.family
);
1044 STREAM_GET(&p
.u
.prefix
, s
, PSIZE(p
.prefixlen
));
1045 l
+= PSIZE(p
.prefixlen
);
1046 if (flags
& ZEBRA_FEC_REGISTER_LABEL_INDEX
) {
1047 STREAM_GETL(s
, label_index
);
1050 label_index
= MPLS_INVALID_LABEL_INDEX
;
1051 zebra_mpls_fec_register(zvrf
, &p
, label_index
, client
);
1058 /* FEC unregister */
1059 static void zread_fec_unregister(ZAPI_HANDLER_ARGS
)
1067 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1069 return; // unexpected
1072 * The minimum amount of data that can be sent for one
1073 * fec unregistration
1075 if (hdr
->length
< ZEBRA_MIN_FEC_LENGTH
) {
1077 "fec_unregister: Received a fec unregister of hdr->length %d, it is of insufficient size to properly decode",
1082 while (l
< hdr
->length
) {
1083 STREAM_GETW(s
, flags
);
1085 goto stream_failure
;
1087 memset(&p
, 0, sizeof(p
));
1088 STREAM_GETW(s
, p
.family
);
1089 if (p
.family
!= AF_INET
&& p
.family
!= AF_INET6
) {
1091 "fec_unregister: Received unknown family type %d\n",
1095 STREAM_GETC(s
, p
.prefixlen
);
1096 if ((p
.family
== AF_INET
&& p
.prefixlen
> IPV4_MAX_BITLEN
)
1097 || (p
.family
== AF_INET6
1098 && p
.prefixlen
> IPV6_MAX_BITLEN
)) {
1100 "%s: Received prefix hdr->length %d which is greater than %d can support",
1101 __PRETTY_FUNCTION__
, p
.prefixlen
, p
.family
);
1105 STREAM_GET(&p
.u
.prefix
, s
, PSIZE(p
.prefixlen
));
1106 l
+= PSIZE(p
.prefixlen
);
1107 zebra_mpls_fec_unregister(zvrf
, &p
, client
);
1116 * Register zebra server interface information.
1117 * Send current all interface and address information.
1119 static void zread_interface_add(ZAPI_HANDLER_ARGS
)
1122 struct interface
*ifp
;
1124 /* Interface information is needed. */
1125 vrf_bitmap_set(client
->ifinfo
, zvrf_id(zvrf
));
1127 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
1128 FOR_ALL_INTERFACES (vrf
, ifp
) {
1129 /* Skip pseudo interface. */
1130 if (!CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_ACTIVE
))
1133 zsend_interface_add(client
, ifp
);
1134 zsend_interface_addresses(client
, ifp
);
1139 /* Unregister zebra server interface information. */
1140 static void zread_interface_delete(ZAPI_HANDLER_ARGS
)
1142 vrf_bitmap_unset(client
->ifinfo
, zvrf_id(zvrf
));
1145 void zserv_nexthop_num_warn(const char *caller
, const struct prefix
*p
,
1146 const unsigned int nexthop_num
)
1148 if (nexthop_num
> multipath_num
) {
1149 char buff
[PREFIX2STR_BUFFER
];
1150 prefix2str(p
, buff
, sizeof(buff
));
1152 "%s: Prefix %s has %d nexthops, but we can only use the first %d",
1153 caller
, buff
, nexthop_num
, multipath_num
);
1157 static void zread_route_add(ZAPI_HANDLER_ARGS
)
1160 struct zapi_route api
;
1161 struct zapi_nexthop
*api_nh
;
1163 struct prefix_ipv6
*src_p
= NULL
;
1164 struct route_entry
*re
;
1165 struct nexthop
*nexthop
= NULL
;
1167 vrf_id_t vrf_id
= 0;
1168 struct ipaddr vtep_ip
;
1171 if (zapi_route_decode(s
, &api
) < 0) {
1172 if (IS_ZEBRA_DEBUG_RECV
)
1173 zlog_debug("%s: Unable to decode zapi_route sent",
1174 __PRETTY_FUNCTION__
);
1178 if (IS_ZEBRA_DEBUG_RECV
) {
1179 char buf_prefix
[PREFIX_STRLEN
];
1180 prefix2str(&api
.prefix
, buf_prefix
, sizeof(buf_prefix
));
1181 zlog_debug("%s: p=%s, ZAPI_MESSAGE_LABEL: %sset, flags=0x%x",
1182 __func__
, buf_prefix
,
1183 (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_LABEL
) ? ""
1188 /* Allocate new route. */
1189 vrf_id
= zvrf_id(zvrf
);
1190 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
1191 re
->type
= api
.type
;
1192 re
->instance
= api
.instance
;
1193 re
->flags
= api
.flags
;
1194 re
->uptime
= time(NULL
);
1195 re
->vrf_id
= vrf_id
;
1196 if (api
.tableid
&& vrf_id
== VRF_DEFAULT
)
1197 re
->table
= api
.tableid
;
1199 re
->table
= zvrf
->table_id
;
1202 * TBD should _all_ of the nexthop add operations use
1203 * api_nh->vrf_id instead of re->vrf_id ? I only changed
1204 * for cases NEXTHOP_TYPE_IPV4 and NEXTHOP_TYPE_IPV6.
1206 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
)) {
1207 for (i
= 0; i
< api
.nexthop_num
; i
++) {
1208 api_nh
= &api
.nexthops
[i
];
1209 ifindex_t ifindex
= 0;
1211 if (IS_ZEBRA_DEBUG_RECV
) {
1212 zlog_debug("nh type %d", api_nh
->type
);
1215 switch (api_nh
->type
) {
1216 case NEXTHOP_TYPE_IFINDEX
:
1217 nexthop
= route_entry_nexthop_ifindex_add(
1218 re
, api_nh
->ifindex
, api_nh
->vrf_id
);
1220 case NEXTHOP_TYPE_IPV4
:
1221 if (IS_ZEBRA_DEBUG_RECV
) {
1222 char nhbuf
[INET6_ADDRSTRLEN
] = {0};
1223 inet_ntop(AF_INET
, &api_nh
->gate
.ipv4
,
1224 nhbuf
, INET6_ADDRSTRLEN
);
1225 zlog_debug("%s: nh=%s, vrf_id=%d",
1229 nexthop
= route_entry_nexthop_ipv4_add(
1230 re
, &api_nh
->gate
.ipv4
, NULL
,
1233 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1235 memset(&vtep_ip
, 0, sizeof(struct ipaddr
));
1236 if (CHECK_FLAG(api
.flags
,
1237 ZEBRA_FLAG_EVPN_ROUTE
)) {
1238 ifindex
= get_l3vni_svi_ifindex(vrf_id
);
1240 ifindex
= api_nh
->ifindex
;
1243 if (IS_ZEBRA_DEBUG_RECV
) {
1244 char nhbuf
[INET6_ADDRSTRLEN
] = {0};
1245 inet_ntop(AF_INET
, &api_nh
->gate
.ipv4
,
1246 nhbuf
, INET6_ADDRSTRLEN
);
1248 "%s: nh=%s, vrf_id=%d (re->vrf_id=%d), ifindex=%d",
1249 __func__
, nhbuf
, api_nh
->vrf_id
,
1250 re
->vrf_id
, ifindex
);
1252 nexthop
= route_entry_nexthop_ipv4_ifindex_add(
1253 re
, &api_nh
->gate
.ipv4
, NULL
, ifindex
,
1256 /* if this an EVPN route entry,
1257 * program the nh as neigh
1259 if (CHECK_FLAG(api
.flags
,
1260 ZEBRA_FLAG_EVPN_ROUTE
)) {
1261 SET_FLAG(nexthop
->flags
,
1262 NEXTHOP_FLAG_EVPN_RVTEP
);
1263 vtep_ip
.ipa_type
= IPADDR_V4
;
1264 memcpy(&(vtep_ip
.ipaddr_v4
),
1265 &(api_nh
->gate
.ipv4
),
1266 sizeof(struct in_addr
));
1267 zebra_vxlan_evpn_vrf_route_add(
1268 vrf_id
, &api
.rmac
, &vtep_ip
,
1272 case NEXTHOP_TYPE_IPV6
:
1273 nexthop
= route_entry_nexthop_ipv6_add(
1274 re
, &api_nh
->gate
.ipv6
, api_nh
->vrf_id
);
1276 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1277 memset(&vtep_ip
, 0, sizeof(struct ipaddr
));
1278 if (CHECK_FLAG(api
.flags
,
1279 ZEBRA_FLAG_EVPN_ROUTE
)) {
1281 get_l3vni_svi_ifindex(vrf_id
);
1283 ifindex
= api_nh
->ifindex
;
1286 nexthop
= route_entry_nexthop_ipv6_ifindex_add(
1287 re
, &api_nh
->gate
.ipv6
, ifindex
,
1290 /* if this an EVPN route entry,
1291 * program the nh as neigh
1293 if (CHECK_FLAG(api
.flags
,
1294 ZEBRA_FLAG_EVPN_ROUTE
)) {
1295 SET_FLAG(nexthop
->flags
,
1296 NEXTHOP_FLAG_EVPN_RVTEP
);
1297 vtep_ip
.ipa_type
= IPADDR_V6
;
1298 memcpy(&vtep_ip
.ipaddr_v6
,
1299 &(api_nh
->gate
.ipv6
),
1300 sizeof(struct in6_addr
));
1301 zebra_vxlan_evpn_vrf_route_add(
1308 case NEXTHOP_TYPE_BLACKHOLE
:
1309 nexthop
= route_entry_nexthop_blackhole_add(
1310 re
, api_nh
->bh_type
);
1316 "%s: Nexthops Specified: %d but we failed to properly create one",
1317 __PRETTY_FUNCTION__
, api
.nexthop_num
);
1318 nexthops_free(re
->ng
.nexthop
);
1319 XFREE(MTYPE_RE
, re
);
1322 /* MPLS labels for BGP-LU or Segment Routing */
1323 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_LABEL
)
1324 && api_nh
->type
!= NEXTHOP_TYPE_IFINDEX
1325 && api_nh
->type
!= NEXTHOP_TYPE_BLACKHOLE
) {
1326 enum lsp_types_t label_type
;
1329 lsp_type_from_re_type(client
->proto
);
1331 if (IS_ZEBRA_DEBUG_RECV
) {
1333 "%s: adding %d labels of type %d (1st=%u)",
1334 __func__
, api_nh
->label_num
,
1335 label_type
, api_nh
->labels
[0]);
1338 nexthop_add_labels(nexthop
, label_type
,
1340 &api_nh
->labels
[0]);
1345 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
))
1346 re
->distance
= api
.distance
;
1347 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
))
1348 re
->metric
= api
.metric
;
1349 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_TAG
))
1351 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_MTU
))
1354 afi
= family2afi(api
.prefix
.family
);
1355 if (afi
!= AFI_IP6
&& CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
)) {
1356 zlog_warn("%s: Received SRC Prefix but afi is not v6",
1357 __PRETTY_FUNCTION__
);
1358 nexthops_free(re
->ng
.nexthop
);
1359 XFREE(MTYPE_RE
, re
);
1362 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
))
1363 src_p
= &api
.src_prefix
;
1365 ret
= rib_add_multipath(afi
, api
.safi
, &api
.prefix
, src_p
, re
);
1368 switch (api
.prefix
.family
) {
1371 client
->v4_route_add_cnt
++;
1373 client
->v4_route_upd8_cnt
++;
1377 client
->v6_route_add_cnt
++;
1379 client
->v6_route_upd8_cnt
++;
1384 static void zread_route_del(ZAPI_HANDLER_ARGS
)
1387 struct zapi_route api
;
1389 struct prefix_ipv6
*src_p
= NULL
;
1393 if (zapi_route_decode(s
, &api
) < 0)
1396 afi
= family2afi(api
.prefix
.family
);
1397 if (afi
!= AFI_IP6
&& CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
)) {
1398 zlog_warn("%s: Received a src prefix while afi is not v6",
1399 __PRETTY_FUNCTION__
);
1402 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
))
1403 src_p
= &api
.src_prefix
;
1405 if (api
.vrf_id
== VRF_DEFAULT
&& api
.tableid
!= 0)
1406 table_id
= api
.tableid
;
1408 table_id
= zvrf
->table_id
;
1410 rib_delete(afi
, api
.safi
, zvrf_id(zvrf
), api
.type
, api
.instance
,
1411 api
.flags
, &api
.prefix
, src_p
, NULL
, table_id
,
1412 api
.metric
, false, &api
.rmac
);
1415 switch (api
.prefix
.family
) {
1417 client
->v4_route_del_cnt
++;
1420 client
->v6_route_del_cnt
++;
1425 /* This function support multiple nexthop. */
1427 * Parse the ZEBRA_IPV4_ROUTE_ADD sent from client. Update re and
1430 static void zread_ipv4_add(ZAPI_HANDLER_ARGS
)
1433 struct route_entry
*re
;
1436 struct in_addr nhop_addr
;
1438 u_char nexthop_type
;
1443 enum lsp_types_t label_type
= ZEBRA_LSP_NONE
;
1445 struct nexthop
*nexthop
;
1446 enum blackhole_type bh_type
= BLACKHOLE_NULL
;
1448 /* Get input stream. */
1451 /* Allocate new re. */
1452 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
1454 /* Type, flags, message. */
1455 STREAM_GETC(s
, re
->type
);
1456 if (re
->type
> ZEBRA_ROUTE_MAX
) {
1457 zlog_warn("%s: Specified route type %d is not a legal value\n",
1458 __PRETTY_FUNCTION__
, re
->type
);
1459 XFREE(MTYPE_RE
, re
);
1462 STREAM_GETW(s
, re
->instance
);
1463 STREAM_GETL(s
, re
->flags
);
1464 STREAM_GETC(s
, message
);
1465 STREAM_GETW(s
, safi
);
1466 re
->uptime
= time(NULL
);
1469 memset(&p
, 0, sizeof(struct prefix_ipv4
));
1471 STREAM_GETC(s
, p
.prefixlen
);
1472 if (p
.prefixlen
> IPV4_MAX_BITLEN
) {
1474 "%s: Specified prefix length %d is greater than what v4 can be",
1475 __PRETTY_FUNCTION__
, p
.prefixlen
);
1476 XFREE(MTYPE_RE
, re
);
1479 STREAM_GET(&p
.u
.prefix4
, s
, PSIZE(p
.prefixlen
));
1482 re
->vrf_id
= zvrf_id(zvrf
);
1484 /* Nexthop parse. */
1485 if (CHECK_FLAG(message
, ZAPI_MESSAGE_NEXTHOP
)) {
1486 STREAM_GETC(s
, nexthop_num
);
1487 zserv_nexthop_num_warn(__func__
, (const struct prefix
*)&p
,
1490 if (CHECK_FLAG(message
, ZAPI_MESSAGE_LABEL
))
1491 label_type
= lsp_type_from_re_type(client
->proto
);
1493 for (i
= 0; i
< nexthop_num
; i
++) {
1494 STREAM_GETC(s
, nexthop_type
);
1496 switch (nexthop_type
) {
1497 case NEXTHOP_TYPE_IFINDEX
:
1498 STREAM_GETL(s
, ifindex
);
1499 route_entry_nexthop_ifindex_add(re
, ifindex
,
1502 case NEXTHOP_TYPE_IPV4
:
1503 STREAM_GET(&nhop_addr
.s_addr
, s
,
1505 nexthop
= route_entry_nexthop_ipv4_add(
1506 re
, &nhop_addr
, NULL
, re
->vrf_id
);
1507 /* For labeled-unicast, each nexthop is followed
1509 if (CHECK_FLAG(message
, ZAPI_MESSAGE_LABEL
)) {
1510 STREAM_GETL(s
, label
);
1511 nexthop_add_labels(nexthop
, label_type
,
1515 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1516 STREAM_GET(&nhop_addr
.s_addr
, s
,
1518 STREAM_GETL(s
, ifindex
);
1519 route_entry_nexthop_ipv4_ifindex_add(
1520 re
, &nhop_addr
, NULL
, ifindex
,
1523 case NEXTHOP_TYPE_IPV6
:
1525 "%s: Please use ZEBRA_ROUTE_ADD if you want to pass v6 nexthops",
1526 __PRETTY_FUNCTION__
);
1527 nexthops_free(re
->ng
.nexthop
);
1528 XFREE(MTYPE_RE
, re
);
1531 case NEXTHOP_TYPE_BLACKHOLE
:
1532 route_entry_nexthop_blackhole_add(re
, bh_type
);
1536 "%s: Specified nexthop type: %d does not exist",
1537 __PRETTY_FUNCTION__
, nexthop_type
);
1538 nexthops_free(re
->ng
.nexthop
);
1539 XFREE(MTYPE_RE
, re
);
1546 if (CHECK_FLAG(message
, ZAPI_MESSAGE_DISTANCE
))
1547 STREAM_GETC(s
, re
->distance
);
1550 if (CHECK_FLAG(message
, ZAPI_MESSAGE_METRIC
))
1551 STREAM_GETL(s
, re
->metric
);
1554 if (CHECK_FLAG(message
, ZAPI_MESSAGE_TAG
))
1555 STREAM_GETL(s
, re
->tag
);
1559 if (CHECK_FLAG(message
, ZAPI_MESSAGE_MTU
))
1560 STREAM_GETL(s
, re
->mtu
);
1565 re
->table
= zvrf
->table_id
;
1567 ret
= rib_add_multipath(AFI_IP
, safi
, &p
, NULL
, re
);
1571 client
->v4_route_add_cnt
++;
1573 client
->v4_route_upd8_cnt
++;
1578 nexthops_free(re
->ng
.nexthop
);
1579 XFREE(MTYPE_RE
, re
);
1582 /* Zebra server IPv4 prefix delete function. */
1583 static void zread_ipv4_delete(ZAPI_HANDLER_ARGS
)
1586 struct zapi_ipv4 api
;
1592 /* Type, flags, message. */
1593 STREAM_GETC(s
, api
.type
);
1594 STREAM_GETW(s
, api
.instance
);
1595 STREAM_GETL(s
, api
.flags
);
1596 STREAM_GETC(s
, api
.message
);
1597 STREAM_GETW(s
, api
.safi
);
1600 memset(&p
, 0, sizeof(struct prefix
));
1602 STREAM_GETC(s
, p
.prefixlen
);
1603 if (p
.prefixlen
> IPV4_MAX_BITLEN
) {
1604 zlog_warn("%s: Passed in prefixlen %d is impossible",
1605 __PRETTY_FUNCTION__
, p
.prefixlen
);
1608 STREAM_GET(&p
.u
.prefix4
, s
, PSIZE(p
.prefixlen
));
1610 table_id
= zvrf
->table_id
;
1612 rib_delete(AFI_IP
, api
.safi
, zvrf_id(zvrf
), api
.type
, api
.instance
,
1613 api
.flags
, &p
, NULL
, NULL
, table_id
, 0, false, NULL
);
1614 client
->v4_route_del_cnt
++;
1620 /* MRIB Nexthop lookup for IPv4. */
1621 static void zread_ipv4_nexthop_lookup_mrib(ZAPI_HANDLER_ARGS
)
1623 struct in_addr addr
;
1624 struct route_entry
*re
;
1626 STREAM_GET(&addr
.s_addr
, msg
, IPV4_MAX_BYTELEN
);
1627 re
= rib_match_ipv4_multicast(zvrf_id(zvrf
), addr
, NULL
);
1628 zsend_ipv4_nexthop_lookup_mrib(client
, addr
, re
, zvrf
);
1634 /* Zebra server IPv6 prefix add function. */
1635 static void zread_ipv4_route_ipv6_nexthop_add(ZAPI_HANDLER_ARGS
)
1639 struct in6_addr nhop_addr
;
1640 struct route_entry
*re
;
1643 u_char nexthop_type
;
1646 static struct in6_addr nexthops
[MULTIPATH_NUM
];
1647 static unsigned int ifindices
[MULTIPATH_NUM
];
1649 static mpls_label_t labels
[MULTIPATH_NUM
];
1650 enum lsp_types_t label_type
= ZEBRA_LSP_NONE
;
1652 struct nexthop
*nexthop
;
1653 enum blackhole_type bh_type
= BLACKHOLE_NULL
;
1655 /* Get input stream. */
1658 memset(&nhop_addr
, 0, sizeof(struct in6_addr
));
1660 /* Allocate new re. */
1661 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
1663 /* Type, flags, message. */
1664 STREAM_GETC(s
, re
->type
);
1665 if (re
->type
> ZEBRA_ROUTE_MAX
) {
1666 zlog_warn("%s: Specified route type: %d is not a legal value\n",
1667 __PRETTY_FUNCTION__
, re
->type
);
1668 XFREE(MTYPE_RE
, re
);
1671 STREAM_GETW(s
, re
->instance
);
1672 STREAM_GETL(s
, re
->flags
);
1673 STREAM_GETC(s
, message
);
1674 STREAM_GETW(s
, safi
);
1675 re
->uptime
= time(NULL
);
1678 memset(&p
, 0, sizeof(struct prefix_ipv4
));
1680 STREAM_GETC(s
, p
.prefixlen
);
1681 if (p
.prefixlen
> IPV4_MAX_BITLEN
) {
1683 "%s: Prefix Length %d is greater than what a v4 address can use",
1684 __PRETTY_FUNCTION__
, p
.prefixlen
);
1685 XFREE(MTYPE_RE
, re
);
1688 STREAM_GET(&p
.u
.prefix4
, s
, PSIZE(p
.prefixlen
));
1691 re
->vrf_id
= zvrf_id(zvrf
);
1693 /* We need to give nh-addr, nh-ifindex with the same next-hop object
1694 * to the re to ensure that IPv6 multipathing works; need to coalesce
1695 * these. Clients should send the same number of paired set of
1696 * next-hop-addr/next-hop-ifindices. */
1697 if (CHECK_FLAG(message
, ZAPI_MESSAGE_NEXTHOP
)) {
1698 unsigned int nh_count
= 0;
1699 unsigned int if_count
= 0;
1700 unsigned int max_nh_if
= 0;
1702 STREAM_GETC(s
, nexthop_num
);
1703 zserv_nexthop_num_warn(__func__
, (const struct prefix
*)&p
,
1706 if (CHECK_FLAG(message
, ZAPI_MESSAGE_LABEL
))
1707 label_type
= lsp_type_from_re_type(client
->proto
);
1709 for (i
= 0; i
< nexthop_num
; i
++) {
1710 STREAM_GETC(s
, nexthop_type
);
1712 switch (nexthop_type
) {
1713 case NEXTHOP_TYPE_IPV6
:
1714 STREAM_GET(&nhop_addr
, s
, 16);
1715 if (nh_count
< MULTIPATH_NUM
) {
1716 /* For labeled-unicast, each nexthop is
1717 * followed by label. */
1718 if (CHECK_FLAG(message
,
1719 ZAPI_MESSAGE_LABEL
)) {
1720 STREAM_GETL(s
, label
);
1721 labels
[nh_count
] = label
;
1723 nexthops
[nh_count
] = nhop_addr
;
1727 case NEXTHOP_TYPE_IFINDEX
:
1728 if (if_count
< multipath_num
) {
1729 STREAM_GETL(s
, ifindices
[if_count
++]);
1732 case NEXTHOP_TYPE_BLACKHOLE
:
1733 route_entry_nexthop_blackhole_add(re
, bh_type
);
1737 "%s: Please use ZEBRA_ROUTE_ADD if you want to pass non v6 nexthops",
1738 __PRETTY_FUNCTION__
);
1739 nexthops_free(re
->ng
.nexthop
);
1740 XFREE(MTYPE_RE
, re
);
1745 max_nh_if
= (nh_count
> if_count
) ? nh_count
: if_count
;
1746 for (i
= 0; i
< max_nh_if
; i
++) {
1748 && !IN6_IS_ADDR_UNSPECIFIED(&nexthops
[i
])) {
1749 if ((i
< if_count
) && ifindices
[i
])
1751 route_entry_nexthop_ipv6_ifindex_add(
1756 nexthop
= route_entry_nexthop_ipv6_add(
1757 re
, &nexthops
[i
], re
->vrf_id
);
1759 if (CHECK_FLAG(message
, ZAPI_MESSAGE_LABEL
))
1760 nexthop_add_labels(nexthop
, label_type
,
1763 if ((i
< if_count
) && ifindices
[i
])
1764 route_entry_nexthop_ifindex_add(
1765 re
, ifindices
[i
], re
->vrf_id
);
1771 if (CHECK_FLAG(message
, ZAPI_MESSAGE_DISTANCE
))
1772 STREAM_GETC(s
, re
->distance
);
1775 if (CHECK_FLAG(message
, ZAPI_MESSAGE_METRIC
))
1776 STREAM_GETL(s
, re
->metric
);
1779 if (CHECK_FLAG(message
, ZAPI_MESSAGE_TAG
))
1780 STREAM_GETL(s
, re
->tag
);
1784 if (CHECK_FLAG(message
, ZAPI_MESSAGE_MTU
))
1785 STREAM_GETL(s
, re
->mtu
);
1790 re
->table
= zvrf
->table_id
;
1792 ret
= rib_add_multipath(AFI_IP6
, safi
, &p
, NULL
, re
);
1795 client
->v4_route_add_cnt
++;
1797 client
->v4_route_upd8_cnt
++;
1802 nexthops_free(re
->ng
.nexthop
);
1803 XFREE(MTYPE_RE
, re
);
1806 static void zread_ipv6_add(ZAPI_HANDLER_ARGS
)
1810 struct in6_addr nhop_addr
;
1812 struct route_entry
*re
;
1815 u_char nexthop_type
;
1817 struct prefix_ipv6 src_p
, *src_pp
;
1819 static struct in6_addr nexthops
[MULTIPATH_NUM
];
1820 static unsigned int ifindices
[MULTIPATH_NUM
];
1822 static mpls_label_t labels
[MULTIPATH_NUM
];
1823 enum lsp_types_t label_type
= ZEBRA_LSP_NONE
;
1825 struct nexthop
*nexthop
;
1826 enum blackhole_type bh_type
= BLACKHOLE_NULL
;
1828 /* Get input stream. */
1831 memset(&nhop_addr
, 0, sizeof(struct in6_addr
));
1833 /* Allocate new re. */
1834 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
1836 /* Type, flags, message. */
1837 STREAM_GETC(s
, re
->type
);
1838 if (re
->type
> ZEBRA_ROUTE_MAX
) {
1839 zlog_warn("%s: Specified route type: %d is not a legal value\n",
1840 __PRETTY_FUNCTION__
, re
->type
);
1841 XFREE(MTYPE_RE
, re
);
1844 STREAM_GETW(s
, re
->instance
);
1845 STREAM_GETL(s
, re
->flags
);
1846 STREAM_GETC(s
, message
);
1847 STREAM_GETW(s
, safi
);
1848 re
->uptime
= time(NULL
);
1851 memset(&p
, 0, sizeof(p
));
1852 p
.family
= AF_INET6
;
1853 STREAM_GETC(s
, p
.prefixlen
);
1854 if (p
.prefixlen
> IPV6_MAX_BITLEN
) {
1856 "%s: Specified prefix length %d is to large for v6 prefix",
1857 __PRETTY_FUNCTION__
, p
.prefixlen
);
1858 XFREE(MTYPE_RE
, re
);
1861 STREAM_GET(&p
.u
.prefix6
, s
, PSIZE(p
.prefixlen
));
1863 if (CHECK_FLAG(message
, ZAPI_MESSAGE_SRCPFX
)) {
1864 memset(&src_p
, 0, sizeof(src_p
));
1865 src_p
.family
= AF_INET6
;
1866 STREAM_GETC(s
, src_p
.prefixlen
);
1867 if (src_p
.prefixlen
> IPV6_MAX_BITLEN
) {
1869 "%s: Specified src prefix length %d is to large for v6 prefix",
1870 __PRETTY_FUNCTION__
, src_p
.prefixlen
);
1871 XFREE(MTYPE_RE
, re
);
1874 STREAM_GET(&src_p
.prefix
, s
, PSIZE(src_p
.prefixlen
));
1880 re
->vrf_id
= zvrf_id(zvrf
);
1882 /* We need to give nh-addr, nh-ifindex with the same next-hop object
1883 * to the re to ensure that IPv6 multipathing works; need to coalesce
1884 * these. Clients should send the same number of paired set of
1885 * next-hop-addr/next-hop-ifindices. */
1886 if (CHECK_FLAG(message
, ZAPI_MESSAGE_NEXTHOP
)) {
1887 unsigned int nh_count
= 0;
1888 unsigned int if_count
= 0;
1889 unsigned int max_nh_if
= 0;
1891 STREAM_GETC(s
, nexthop_num
);
1892 zserv_nexthop_num_warn(__func__
, (const struct prefix
*)&p
,
1895 if (CHECK_FLAG(message
, ZAPI_MESSAGE_LABEL
))
1896 label_type
= lsp_type_from_re_type(client
->proto
);
1898 for (i
= 0; i
< nexthop_num
; i
++) {
1899 STREAM_GETC(s
, nexthop_type
);
1901 switch (nexthop_type
) {
1902 case NEXTHOP_TYPE_IPV6
:
1903 STREAM_GET(&nhop_addr
, s
, 16);
1904 if (nh_count
< MULTIPATH_NUM
) {
1905 /* For labeled-unicast, each nexthop is
1906 * followed by label. */
1907 if (CHECK_FLAG(message
,
1908 ZAPI_MESSAGE_LABEL
)) {
1909 STREAM_GETL(s
, label
);
1910 labels
[nh_count
] = label
;
1912 nexthops
[nh_count
++] = nhop_addr
;
1915 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1916 STREAM_GET(&nhop_addr
, s
, 16);
1917 STREAM_GETL(s
, ifindex
);
1918 route_entry_nexthop_ipv6_ifindex_add(
1919 re
, &nhop_addr
, ifindex
, re
->vrf_id
);
1921 case NEXTHOP_TYPE_IFINDEX
:
1922 if (if_count
< multipath_num
) {
1923 STREAM_GETL(s
, ifindices
[if_count
++]);
1926 case NEXTHOP_TYPE_BLACKHOLE
:
1927 route_entry_nexthop_blackhole_add(re
, bh_type
);
1931 "%s: Please use ZEBRA_ROUTE_ADD if you want to pass non v6 nexthops",
1932 __PRETTY_FUNCTION__
);
1933 nexthops_free(re
->ng
.nexthop
);
1934 XFREE(MTYPE_RE
, re
);
1939 max_nh_if
= (nh_count
> if_count
) ? nh_count
: if_count
;
1940 for (i
= 0; i
< max_nh_if
; i
++) {
1942 && !IN6_IS_ADDR_UNSPECIFIED(&nexthops
[i
])) {
1943 if ((i
< if_count
) && ifindices
[i
])
1945 route_entry_nexthop_ipv6_ifindex_add(
1950 nexthop
= route_entry_nexthop_ipv6_add(
1951 re
, &nexthops
[i
], re
->vrf_id
);
1952 if (CHECK_FLAG(message
, ZAPI_MESSAGE_LABEL
))
1953 nexthop_add_labels(nexthop
, label_type
,
1956 if ((i
< if_count
) && ifindices
[i
])
1957 route_entry_nexthop_ifindex_add(
1958 re
, ifindices
[i
], re
->vrf_id
);
1964 if (CHECK_FLAG(message
, ZAPI_MESSAGE_DISTANCE
))
1965 STREAM_GETC(s
, re
->distance
);
1968 if (CHECK_FLAG(message
, ZAPI_MESSAGE_METRIC
))
1969 STREAM_GETL(s
, re
->metric
);
1972 if (CHECK_FLAG(message
, ZAPI_MESSAGE_TAG
))
1973 STREAM_GETL(s
, re
->tag
);
1977 if (CHECK_FLAG(message
, ZAPI_MESSAGE_MTU
))
1978 STREAM_GETL(s
, re
->mtu
);
1982 re
->table
= zvrf
->table_id
;
1984 ret
= rib_add_multipath(AFI_IP6
, safi
, &p
, src_pp
, re
);
1987 client
->v6_route_add_cnt
++;
1989 client
->v6_route_upd8_cnt
++;
1994 nexthops_free(re
->ng
.nexthop
);
1995 XFREE(MTYPE_RE
, re
);
1998 /* Zebra server IPv6 prefix delete function. */
1999 static void zread_ipv6_delete(ZAPI_HANDLER_ARGS
)
2002 struct zapi_ipv6 api
;
2004 struct prefix_ipv6 src_p
, *src_pp
;
2008 /* Type, flags, message. */
2009 STREAM_GETC(s
, api
.type
);
2010 STREAM_GETW(s
, api
.instance
);
2011 STREAM_GETL(s
, api
.flags
);
2012 STREAM_GETC(s
, api
.message
);
2013 STREAM_GETW(s
, api
.safi
);
2016 memset(&p
, 0, sizeof(struct prefix
));
2017 p
.family
= AF_INET6
;
2018 STREAM_GETC(s
, p
.prefixlen
);
2019 STREAM_GET(&p
.u
.prefix6
, s
, PSIZE(p
.prefixlen
));
2021 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
)) {
2022 memset(&src_p
, 0, sizeof(struct prefix_ipv6
));
2023 src_p
.family
= AF_INET6
;
2024 STREAM_GETC(s
, src_p
.prefixlen
);
2025 STREAM_GET(&src_p
.prefix
, s
, PSIZE(src_p
.prefixlen
));
2030 rib_delete(AFI_IP6
, api
.safi
, zvrf_id(zvrf
), api
.type
, api
.instance
,
2031 api
.flags
, &p
, src_pp
, NULL
, client
->rtm_table
, 0, false,
2034 client
->v6_route_del_cnt
++;
2040 /* Register zebra server router-id information. Send current router-id */
2041 static void zread_router_id_add(ZAPI_HANDLER_ARGS
)
2045 /* Router-id information is needed. */
2046 vrf_bitmap_set(client
->ridinfo
, zvrf_id(zvrf
));
2048 router_id_get(&p
, zvrf_id(zvrf
));
2050 zsend_router_id_update(client
, &p
, zvrf_id(zvrf
));
2053 /* Unregister zebra server router-id information. */
2054 static void zread_router_id_delete(ZAPI_HANDLER_ARGS
)
2056 vrf_bitmap_unset(client
->ridinfo
, zvrf_id(zvrf
));
2059 /* Tie up route-type and client->sock */
2060 static void zread_hello(ZAPI_HANDLER_ARGS
)
2062 /* type of protocol (lib/zebra.h) */
2067 STREAM_GETC(msg
, proto
);
2068 STREAM_GETW(msg
, instance
);
2069 STREAM_GETC(msg
, notify
);
2071 client
->notify_owner
= true;
2073 /* accept only dynamic routing protocols */
2074 if ((proto
< ZEBRA_ROUTE_MAX
) && (proto
> ZEBRA_ROUTE_STATIC
)) {
2076 "client %d says hello and bids fair to announce only %s routes vrf=%u",
2077 client
->sock
, zebra_route_string(proto
),
2080 zlog_notice("client protocol instance %d", instance
);
2082 client
->proto
= proto
;
2083 client
->instance
= instance
;
2090 /* Unregister all information in a VRF. */
2091 static void zread_vrf_unregister(ZAPI_HANDLER_ARGS
)
2096 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
2097 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
2098 vrf_bitmap_unset(client
->redist
[afi
][i
], zvrf_id(zvrf
));
2099 vrf_bitmap_unset(client
->redist_default
, zvrf_id(zvrf
));
2100 vrf_bitmap_unset(client
->ifinfo
, zvrf_id(zvrf
));
2101 vrf_bitmap_unset(client
->ridinfo
, zvrf_id(zvrf
));
2104 static void zread_mpls_labels(ZAPI_HANDLER_ARGS
)
2107 enum lsp_types_t type
;
2108 struct prefix prefix
;
2109 enum nexthop_types_t gtype
;
2112 mpls_label_t in_label
, out_label
;
2115 /* Get input stream. */
2119 STREAM_GETC(s
, type
);
2120 STREAM_GETL(s
, prefix
.family
);
2121 switch (prefix
.family
) {
2123 STREAM_GET(&prefix
.u
.prefix4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
2124 STREAM_GETC(s
, prefix
.prefixlen
);
2125 if (prefix
.prefixlen
> IPV4_MAX_BITLEN
) {
2127 "%s: Specified prefix length %d is greater than a v4 address can support",
2128 __PRETTY_FUNCTION__
, prefix
.prefixlen
);
2131 STREAM_GET(&gate
.ipv4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
2134 STREAM_GET(&prefix
.u
.prefix6
, s
, 16);
2135 STREAM_GETC(s
, prefix
.prefixlen
);
2136 if (prefix
.prefixlen
> IPV6_MAX_BITLEN
) {
2138 "%s: Specified prefix length %d is greater than a v6 address can support",
2139 __PRETTY_FUNCTION__
, prefix
.prefixlen
);
2142 STREAM_GET(&gate
.ipv6
, s
, 16);
2145 zlog_warn("%s: Specified AF %d is not supported for this call",
2146 __PRETTY_FUNCTION__
, prefix
.family
);
2149 STREAM_GETL(s
, ifindex
);
2150 STREAM_GETC(s
, distance
);
2151 STREAM_GETL(s
, in_label
);
2152 STREAM_GETL(s
, out_label
);
2154 switch (prefix
.family
) {
2157 gtype
= NEXTHOP_TYPE_IPV4_IFINDEX
;
2159 gtype
= NEXTHOP_TYPE_IPV4
;
2163 gtype
= NEXTHOP_TYPE_IPV6_IFINDEX
;
2165 gtype
= NEXTHOP_TYPE_IPV6
;
2174 if (hdr
->command
== ZEBRA_MPLS_LABELS_ADD
) {
2175 mpls_lsp_install(zvrf
, type
, in_label
, out_label
, gtype
, &gate
,
2177 mpls_ftn_update(1, zvrf
, type
, &prefix
, gtype
, &gate
, ifindex
,
2178 distance
, out_label
);
2179 } else if (hdr
->command
== ZEBRA_MPLS_LABELS_DELETE
) {
2180 mpls_lsp_uninstall(zvrf
, type
, in_label
, gtype
, &gate
, ifindex
);
2181 mpls_ftn_update(0, zvrf
, type
, &prefix
, gtype
, &gate
, ifindex
,
2182 distance
, out_label
);
2188 static void zread_label_manager_connect(struct zserv
*client
,
2189 struct stream
*msg
, vrf_id_t vrf_id
)
2192 /* type of protocol (lib/zebra.h) */
2196 /* Get input stream. */
2200 STREAM_GETC(s
, proto
);
2201 STREAM_GETW(s
, instance
);
2203 /* accept only dynamic routing protocols */
2204 if ((proto
>= ZEBRA_ROUTE_MAX
) || (proto
<= ZEBRA_ROUTE_STATIC
)) {
2205 zlog_err("client %d has wrong protocol %s", client
->sock
,
2206 zebra_route_string(proto
));
2207 zsend_label_manager_connect_response(client
, vrf_id
, 1);
2210 zlog_notice("client %d with vrf %u instance %u connected as %s",
2211 client
->sock
, vrf_id
, instance
, zebra_route_string(proto
));
2212 client
->proto
= proto
;
2213 client
->instance
= instance
;
2216 Release previous labels of same protocol and instance.
2217 This is done in case it restarted from an unexpected shutdown.
2219 release_daemon_chunks(proto
, instance
);
2222 " Label Manager client connected: sock %d, proto %s, vrf %u instance %u",
2223 client
->sock
, zebra_route_string(proto
), vrf_id
, instance
);
2224 /* send response back */
2225 zsend_label_manager_connect_response(client
, vrf_id
, 0);
2231 static void zread_get_label_chunk(struct zserv
*client
, struct stream
*msg
,
2237 struct label_manager_chunk
*lmc
;
2239 /* Get input stream. */
2243 STREAM_GETC(s
, keep
);
2244 STREAM_GETL(s
, size
);
2246 lmc
= assign_label_chunk(client
->proto
, client
->instance
, keep
, size
);
2248 zlog_err("%s: Unable to assign Label Chunk of size %u",
2251 zlog_debug("Assigned Label Chunk %u - %u to %u", lmc
->start
,
2253 /* send response back */
2254 zsend_assign_label_chunk_response(client
, vrf_id
, lmc
);
2260 static void zread_release_label_chunk(struct zserv
*client
, struct stream
*msg
)
2263 uint32_t start
, end
;
2265 /* Get input stream. */
2269 STREAM_GETL(s
, start
);
2270 STREAM_GETL(s
, end
);
2272 release_label_chunk(client
->proto
, client
->instance
, start
, end
);
2277 static void zread_label_manager_request(ZAPI_HANDLER_ARGS
)
2279 /* to avoid sending other messages like ZERBA_INTERFACE_UP */
2280 if (hdr
->command
== ZEBRA_LABEL_MANAGER_CONNECT
)
2281 client
->is_synchronous
= 1;
2283 /* external label manager */
2285 zread_relay_label_manager_request(hdr
->command
, client
,
2287 /* this is a label manager */
2289 if (hdr
->command
== ZEBRA_LABEL_MANAGER_CONNECT
)
2290 zread_label_manager_connect(client
, msg
, zvrf_id(zvrf
));
2292 /* Sanity: don't allow 'unidentified' requests */
2293 if (!client
->proto
) {
2295 "Got label request from an unidentified client");
2298 if (hdr
->command
== ZEBRA_GET_LABEL_CHUNK
)
2299 zread_get_label_chunk(client
, msg
,
2301 else if (hdr
->command
== ZEBRA_RELEASE_LABEL_CHUNK
)
2302 zread_release_label_chunk(client
, msg
);
2307 static void zread_pseudowire(ZAPI_HANDLER_ARGS
)
2310 char ifname
[IF_NAMESIZE
];
2314 union g_addr nexthop
;
2315 uint32_t local_label
;
2316 uint32_t remote_label
;
2318 union pw_protocol_fields data
;
2320 struct zebra_pw
*pw
;
2322 /* Get input stream. */
2326 STREAM_GET(ifname
, s
, IF_NAMESIZE
);
2327 STREAM_GETL(s
, ifindex
);
2328 STREAM_GETL(s
, type
);
2332 STREAM_GET(&nexthop
.ipv4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
2335 STREAM_GET(&nexthop
.ipv6
, s
, 16);
2340 STREAM_GETL(s
, local_label
);
2341 STREAM_GETL(s
, remote_label
);
2342 STREAM_GETC(s
, flags
);
2343 STREAM_GET(&data
, s
, sizeof(data
));
2344 protocol
= client
->proto
;
2346 pw
= zebra_pw_find(zvrf
, ifname
);
2347 switch (hdr
->command
) {
2350 zlog_warn("%s: pseudowire %s already exists [%s]",
2352 zserv_command_string(hdr
->command
));
2356 zebra_pw_add(zvrf
, ifname
, protocol
, client
);
2358 case ZEBRA_PW_DELETE
:
2360 zlog_warn("%s: pseudowire %s not found [%s]", __func__
,
2361 ifname
, zserv_command_string(hdr
->command
));
2365 zebra_pw_del(zvrf
, pw
);
2368 case ZEBRA_PW_UNSET
:
2370 zlog_warn("%s: pseudowire %s not found [%s]", __func__
,
2371 ifname
, zserv_command_string(hdr
->command
));
2375 switch (hdr
->command
) {
2379 case ZEBRA_PW_UNSET
:
2384 zebra_pw_change(pw
, ifindex
, type
, af
, &nexthop
, local_label
,
2385 remote_label
, flags
, &data
);
2393 /* Cleanup registered nexthops (across VRFs) upon client disconnect. */
2394 static void zebra_client_close_cleanup_rnh(struct zserv
*client
)
2397 struct zebra_vrf
*zvrf
;
2399 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
2400 if ((zvrf
= vrf
->info
) != NULL
) {
2401 zebra_cleanup_rnh_client(zvrf_id(zvrf
), AF_INET
, client
,
2403 zebra_cleanup_rnh_client(zvrf_id(zvrf
), AF_INET6
,
2404 client
, RNH_NEXTHOP_TYPE
);
2405 zebra_cleanup_rnh_client(zvrf_id(zvrf
), AF_INET
, client
,
2406 RNH_IMPORT_CHECK_TYPE
);
2407 zebra_cleanup_rnh_client(zvrf_id(zvrf
), AF_INET6
,
2408 client
, RNH_IMPORT_CHECK_TYPE
);
2409 if (client
->proto
== ZEBRA_ROUTE_LDP
) {
2410 hash_iterate(zvrf
->lsp_table
,
2411 mpls_ldp_lsp_uninstall_all
,
2413 mpls_ldp_ftn_uninstall_all(zvrf
, AFI_IP
);
2414 mpls_ldp_ftn_uninstall_all(zvrf
, AFI_IP6
);
2420 static void zread_interface_set_master(ZAPI_HANDLER_ARGS
)
2422 struct interface
*master
;
2423 struct interface
*slave
;
2424 struct stream
*s
= msg
;
2428 STREAM_GETL(s
, vrf_id
);
2429 STREAM_GETL(s
, ifindex
);
2430 master
= if_lookup_by_index(ifindex
, vrf_id
);
2432 STREAM_GETL(s
, vrf_id
);
2433 STREAM_GETL(s
, ifindex
);
2434 slave
= if_lookup_by_index(ifindex
, vrf_id
);
2436 if (!master
|| !slave
)
2439 kernel_interface_set_master(master
, slave
);
2446 static void zread_vrf_label(ZAPI_HANDLER_ARGS
)
2448 struct interface
*ifp
;
2449 mpls_label_t nlabel
;
2452 struct zebra_vrf
*def_zvrf
;
2453 enum lsp_types_t ltype
;
2456 STREAM_GETL(s
, nlabel
);
2457 STREAM_GETC(s
, afi
);
2458 if (nlabel
== zvrf
->label
[afi
]) {
2460 * Nothing to do here move along
2465 STREAM_GETC(s
, ltype
);
2467 if (zvrf
->vrf
->vrf_id
!= VRF_DEFAULT
)
2468 ifp
= if_lookup_by_name(zvrf
->vrf
->name
, zvrf
->vrf
->vrf_id
);
2470 ifp
= if_lookup_by_name("lo", VRF_DEFAULT
);
2473 zlog_debug("Unable to find specified Interface for %s",
2478 def_zvrf
= zebra_vrf_lookup_by_id(VRF_DEFAULT
);
2480 if (zvrf
->label
[afi
] != MPLS_LABEL_NONE
) {
2484 really_remove
= true;
2485 for (scrubber
= AFI_IP
; scrubber
< AFI_MAX
; scrubber
++) {
2486 if (scrubber
== afi
)
2489 if (zvrf
->label
[scrubber
] == MPLS_LABEL_NONE
)
2492 if (zvrf
->label
[afi
] == zvrf
->label
[scrubber
]) {
2493 really_remove
= false;
2499 mpls_lsp_uninstall(def_zvrf
, ltype
, zvrf
->label
[afi
],
2500 NEXTHOP_TYPE_IFINDEX
, NULL
,
2504 if (nlabel
!= MPLS_LABEL_NONE
)
2505 mpls_lsp_install(def_zvrf
, ltype
, nlabel
,
2506 MPLS_LABEL_IMPLICIT_NULL
, NEXTHOP_TYPE_IFINDEX
,
2507 NULL
, ifp
->ifindex
);
2509 zvrf
->label
[afi
] = nlabel
;
2514 static inline void zread_rule(ZAPI_HANDLER_ARGS
)
2516 struct zebra_pbr_rule zpr
;
2522 STREAM_GETL(s
, total
);
2524 for (i
= 0; i
< total
; i
++) {
2525 memset(&zpr
, 0, sizeof(zpr
));
2527 zpr
.sock
= client
->sock
;
2528 STREAM_GETL(s
, zpr
.seq
);
2529 STREAM_GETL(s
, zpr
.priority
);
2530 STREAM_GETL(s
, zpr
.unique
);
2531 STREAM_GETC(s
, zpr
.filter
.src_ip
.family
);
2532 STREAM_GETC(s
, zpr
.filter
.src_ip
.prefixlen
);
2533 STREAM_GET(&zpr
.filter
.src_ip
.u
.prefix
, s
,
2534 prefix_blen(&zpr
.filter
.src_ip
));
2535 STREAM_GETW(s
, zpr
.filter
.src_port
);
2536 STREAM_GETC(s
, zpr
.filter
.dst_ip
.family
);
2537 STREAM_GETC(s
, zpr
.filter
.dst_ip
.prefixlen
);
2538 STREAM_GET(&zpr
.filter
.dst_ip
.u
.prefix
, s
,
2539 prefix_blen(&zpr
.filter
.dst_ip
));
2540 STREAM_GETW(s
, zpr
.filter
.dst_port
);
2541 STREAM_GETL(s
, zpr
.action
.table
);
2542 STREAM_GETL(s
, ifindex
);
2544 zpr
.ifp
= if_lookup_by_index(ifindex
, VRF_UNKNOWN
);
2546 zlog_debug("FAiled to lookup ifindex: %u", ifindex
);
2550 if (!is_default_prefix(&zpr
.filter
.src_ip
))
2551 zpr
.filter
.filter_bm
|= PBR_FILTER_SRC_IP
;
2553 if (!is_default_prefix(&zpr
.filter
.dst_ip
))
2554 zpr
.filter
.filter_bm
|= PBR_FILTER_DST_IP
;
2556 if (zpr
.filter
.src_port
)
2557 zpr
.filter
.filter_bm
|= PBR_FILTER_SRC_PORT
;
2559 if (zpr
.filter
.dst_port
)
2560 zpr
.filter
.filter_bm
|= PBR_FILTER_DST_PORT
;
2562 if (hdr
->command
== ZEBRA_RULE_ADD
)
2563 zebra_pbr_add_rule(zvrf
->zns
, &zpr
);
2565 zebra_pbr_del_rule(zvrf
->zns
, &zpr
);
2572 void (*zserv_handlers
[])(ZAPI_HANDLER_ARGS
) = {
2573 [ZEBRA_ROUTER_ID_ADD
] = zread_router_id_add
,
2574 [ZEBRA_ROUTER_ID_DELETE
] = zread_router_id_delete
,
2575 [ZEBRA_INTERFACE_ADD
] = zread_interface_add
,
2576 [ZEBRA_INTERFACE_DELETE
] = zread_interface_delete
,
2577 [ZEBRA_ROUTE_ADD
] = zread_route_add
,
2578 [ZEBRA_ROUTE_DELETE
] = zread_route_del
,
2579 [ZEBRA_IPV4_ROUTE_ADD
] = zread_ipv4_add
,
2580 [ZEBRA_IPV4_ROUTE_DELETE
] = zread_ipv4_delete
,
2581 [ZEBRA_IPV4_ROUTE_IPV6_NEXTHOP_ADD
] = zread_ipv4_route_ipv6_nexthop_add
,
2582 [ZEBRA_IPV6_ROUTE_ADD
] = zread_ipv6_add
,
2583 [ZEBRA_IPV6_ROUTE_DELETE
] = zread_ipv6_delete
,
2584 [ZEBRA_REDISTRIBUTE_ADD
] = zebra_redistribute_add
,
2585 [ZEBRA_REDISTRIBUTE_DELETE
] = zebra_redistribute_delete
,
2586 [ZEBRA_REDISTRIBUTE_DEFAULT_ADD
] = zebra_redistribute_default_add
,
2587 [ZEBRA_REDISTRIBUTE_DEFAULT_DELETE
] = zebra_redistribute_default_delete
,
2588 [ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB
] = zread_ipv4_nexthop_lookup_mrib
,
2589 [ZEBRA_HELLO
] = zread_hello
,
2590 [ZEBRA_NEXTHOP_REGISTER
] = zread_rnh_register
,
2591 [ZEBRA_NEXTHOP_UNREGISTER
] = zread_rnh_unregister
,
2592 [ZEBRA_IMPORT_ROUTE_REGISTER
] = zread_rnh_register
,
2593 [ZEBRA_IMPORT_ROUTE_UNREGISTER
] = zread_rnh_unregister
,
2594 [ZEBRA_BFD_DEST_UPDATE
] = zebra_ptm_bfd_dst_register
,
2595 [ZEBRA_BFD_DEST_REGISTER
] = zebra_ptm_bfd_dst_register
,
2596 [ZEBRA_BFD_DEST_DEREGISTER
] = zebra_ptm_bfd_dst_deregister
,
2597 [ZEBRA_VRF_UNREGISTER
] = zread_vrf_unregister
,
2598 [ZEBRA_VRF_LABEL
] = zread_vrf_label
,
2599 [ZEBRA_BFD_CLIENT_REGISTER
] = zebra_ptm_bfd_client_register
,
2600 #if defined(HAVE_RTADV)
2601 [ZEBRA_INTERFACE_ENABLE_RADV
] = zebra_interface_radv_enable
,
2602 [ZEBRA_INTERFACE_DISABLE_RADV
] = zebra_interface_radv_disable
,
2604 [ZEBRA_INTERFACE_ENABLE_RADV
] = NULL
,
2605 [ZEBRA_INTERFACE_DISABLE_RADV
] = NULL
,
2607 [ZEBRA_MPLS_LABELS_ADD
] = zread_mpls_labels
,
2608 [ZEBRA_MPLS_LABELS_DELETE
] = zread_mpls_labels
,
2609 [ZEBRA_IPMR_ROUTE_STATS
] = zebra_ipmr_route_stats
,
2610 [ZEBRA_LABEL_MANAGER_CONNECT
] = zread_label_manager_request
,
2611 [ZEBRA_GET_LABEL_CHUNK
] = zread_label_manager_request
,
2612 [ZEBRA_RELEASE_LABEL_CHUNK
] = zread_label_manager_request
,
2613 [ZEBRA_FEC_REGISTER
] = zread_fec_register
,
2614 [ZEBRA_FEC_UNREGISTER
] = zread_fec_unregister
,
2615 [ZEBRA_ADVERTISE_DEFAULT_GW
] = zebra_vxlan_advertise_gw_macip
,
2616 [ZEBRA_ADVERTISE_SUBNET
] = zebra_vxlan_advertise_subnet
,
2617 [ZEBRA_ADVERTISE_ALL_VNI
] = zebra_vxlan_advertise_all_vni
,
2618 [ZEBRA_REMOTE_VTEP_ADD
] = zebra_vxlan_remote_vtep_add
,
2619 [ZEBRA_REMOTE_VTEP_DEL
] = zebra_vxlan_remote_vtep_del
,
2620 [ZEBRA_REMOTE_MACIP_ADD
] = zebra_vxlan_remote_macip_add
,
2621 [ZEBRA_REMOTE_MACIP_DEL
] = zebra_vxlan_remote_macip_del
,
2622 [ZEBRA_INTERFACE_SET_MASTER
] = zread_interface_set_master
,
2623 [ZEBRA_PW_ADD
] = zread_pseudowire
,
2624 [ZEBRA_PW_DELETE
] = zread_pseudowire
,
2625 [ZEBRA_PW_SET
] = zread_pseudowire
,
2626 [ZEBRA_PW_UNSET
] = zread_pseudowire
,
2627 [ZEBRA_RULE_ADD
] = zread_rule
,
2628 [ZEBRA_RULE_DELETE
] = zread_rule
,
2631 static inline void zserv_handle_commands(struct zserv
*client
,
2632 struct zmsghdr
*hdr
,
2634 struct zebra_vrf
*zvrf
)
2636 if (hdr
->command
> array_size(zserv_handlers
)
2637 || zserv_handlers
[hdr
->command
] == NULL
)
2638 zlog_info("Zebra received unknown command %d", hdr
->command
);
2640 zserv_handlers
[hdr
->command
](client
, hdr
, msg
, zvrf
);
2645 /* Lifecycle ---------------------------------------------------------------- */
2647 /* free zebra client information. */
2648 static void zebra_client_free(struct zserv
*client
)
2650 /* Send client de-registration to BFD */
2651 zebra_ptm_bfd_client_deregister(client
->proto
);
2653 /* Cleanup any rules installed from this client */
2654 zebra_pbr_client_close_cleanup(client
->sock
);
2656 /* Cleanup any registered nexthops - across all VRFs. */
2657 zebra_client_close_cleanup_rnh(client
);
2659 /* Release Label Manager chunks */
2660 release_daemon_chunks(client
->proto
, client
->instance
);
2662 /* Cleanup any FECs registered by this client. */
2663 zebra_mpls_cleanup_fecs_for_client(vrf_info_lookup(VRF_DEFAULT
),
2666 /* Remove pseudowires associated with this client */
2667 zebra_pw_client_close(client
);
2669 /* Close file descriptor. */
2671 unsigned long nroutes
;
2673 close(client
->sock
);
2674 nroutes
= rib_score_proto(client
->proto
, client
->instance
);
2676 "client %d disconnected. %lu %s routes removed from the rib",
2677 client
->sock
, nroutes
,
2678 zebra_route_string(client
->proto
));
2682 /* Free stream buffers. */
2683 if (client
->ibuf_work
)
2684 stream_free(client
->ibuf_work
);
2685 if (client
->obuf_work
)
2686 stream_free(client
->obuf_work
);
2687 if (client
->ibuf_fifo
)
2688 stream_fifo_free(client
->ibuf_fifo
);
2689 if (client
->obuf_fifo
)
2690 stream_fifo_free(client
->obuf_fifo
);
2692 buffer_free(client
->wb
);
2694 /* Release threads. */
2696 thread_cancel(client
->t_read
);
2697 if (client
->t_write
)
2698 thread_cancel(client
->t_write
);
2699 if (client
->t_suicide
)
2700 thread_cancel(client
->t_suicide
);
2703 for (afi_t afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
2704 for (int i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
2705 vrf_bitmap_free(client
->redist
[afi
][i
]);
2707 vrf_bitmap_free(client
->redist_default
);
2708 vrf_bitmap_free(client
->ifinfo
);
2709 vrf_bitmap_free(client
->ridinfo
);
2711 XFREE(MTYPE_TMP
, client
);
2715 * Called from client thread to terminate itself.
2717 static void zebra_client_close(struct zserv
*client
)
2719 listnode_delete(zebrad
.client_list
, client
);
2720 zebra_client_free(client
);
2723 /* Make new client. */
2724 static void zebra_client_create(int sock
)
2726 struct zserv
*client
;
2730 client
= XCALLOC(MTYPE_TMP
, sizeof(struct zserv
));
2732 /* Make client input/output buffer. */
2733 client
->sock
= sock
;
2734 client
->ibuf_fifo
= stream_fifo_new();
2735 client
->obuf_fifo
= stream_fifo_new();
2736 client
->ibuf_work
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
2737 client
->obuf_work
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
2738 client
->wb
= buffer_new(0);
2740 /* Set table number. */
2741 client
->rtm_table
= zebrad
.rtm_table_default
;
2743 client
->connect_time
= monotime(NULL
);
2744 /* Initialize flags */
2745 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
2746 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
2747 client
->redist
[afi
][i
] = vrf_bitmap_init();
2748 client
->redist_default
= vrf_bitmap_init();
2749 client
->ifinfo
= vrf_bitmap_init();
2750 client
->ridinfo
= vrf_bitmap_init();
2752 /* by default, it's not a synchronous client */
2753 client
->is_synchronous
= 0;
2755 /* Add this client to linked list. */
2756 listnode_add(zebrad
.client_list
, client
);
2758 zebra_vrf_update_all(client
);
2760 /* start read loop */
2761 zebra_event(client
, ZEBRA_READ
);
2764 static int zserv_delayed_close(struct thread
*thread
)
2766 struct zserv
*client
= THREAD_ARG(thread
);
2768 client
->t_suicide
= NULL
;
2769 zebra_client_close(client
);
2774 * Log zapi message to zlog.
2783 * The message header
2785 static void zserv_log_message(const char *errmsg
, struct stream
*msg
,
2786 struct zmsghdr
*hdr
)
2788 zlog_debug("Rx'd ZAPI message");
2790 zlog_debug("%s", errmsg
);
2792 zlog_debug(" Length: %d", hdr
->length
);
2793 zlog_debug("Command: %s", zserv_command_string(hdr
->command
));
2794 zlog_debug(" VRF: %u", hdr
->vrf_id
);
2796 zlog_hexdump(msg
->data
, STREAM_READABLE(msg
));
2799 static int zserv_flush_data(struct thread
*thread
)
2801 struct zserv
*client
= THREAD_ARG(thread
);
2803 client
->t_write
= NULL
;
2804 if (client
->t_suicide
) {
2805 zebra_client_close(client
);
2808 switch (buffer_flush_available(client
->wb
, client
->sock
)) {
2811 "%s: buffer_flush_available failed on zserv client fd %d, closing",
2812 __func__
, client
->sock
);
2813 zebra_client_close(client
);
2816 case BUFFER_PENDING
:
2817 client
->t_write
= NULL
;
2818 thread_add_write(zebrad
.master
, zserv_flush_data
, client
,
2819 client
->sock
, &client
->t_write
);
2826 client
->last_write_time
= monotime(NULL
);
2831 * Write a single packet.
2833 static int zserv_write(struct thread
*thread
)
2835 struct zserv
*client
= THREAD_ARG(thread
);
2839 if (client
->t_suicide
)
2842 if (client
->is_synchronous
)
2845 msg
= stream_fifo_pop(client
->obuf_fifo
);
2846 stream_set_getp(msg
, 0);
2847 client
->last_write_cmd
= stream_getw_from(msg
, 6);
2849 writerv
= buffer_write(client
->wb
, client
->sock
, STREAM_DATA(msg
),
2850 stream_get_endp(msg
));
2857 "%s: buffer_write failed to zserv client fd %d, closing",
2858 __func__
, client
->sock
);
2860 * Schedule a delayed close since many of the functions that
2861 * call this one do not check the return code. They do not
2862 * allow for the possibility that an I/O error may have caused
2863 * the client to be deleted.
2865 client
->t_suicide
= NULL
;
2866 thread_add_event(zebrad
.master
, zserv_delayed_close
, client
, 0,
2867 &client
->t_suicide
);
2870 THREAD_OFF(client
->t_write
);
2872 case BUFFER_PENDING
:
2873 thread_add_write(zebrad
.master
, zserv_flush_data
, client
,
2874 client
->sock
, &client
->t_write
);
2878 if (client
->obuf_fifo
->count
)
2879 zebra_event(client
, ZEBRA_WRITE
);
2881 client
->last_write_time
= monotime(NULL
);
2885 #if defined(HANDLE_ZAPI_FUZZING)
2886 static void zserv_write_incoming(struct stream
*orig
, uint16_t command
)
2888 char fname
[MAXPATHLEN
];
2889 struct stream
*copy
;
2892 copy
= stream_dup(orig
);
2893 stream_set_getp(copy
, 0);
2895 zserv_privs
.change(ZPRIVS_RAISE
);
2896 snprintf(fname
, MAXPATHLEN
, "%s/%u", DAEMON_VTY_DIR
, command
);
2897 fd
= open(fname
, O_CREAT
| O_WRONLY
| O_EXCL
, 0644);
2898 stream_flush(copy
, fd
);
2900 zserv_privs
.change(ZPRIVS_LOWER
);
2905 static int zserv_process_messages(struct thread
*thread
)
2907 struct zserv
*client
= THREAD_ARG(thread
);
2908 struct zebra_vrf
*zvrf
;
2914 msg
= stream_fifo_pop(client
->ibuf_fifo
);
2916 /* break if out of messages */
2920 /* read & check header */
2921 hdrvalid
= zapi_parse_header(msg
, &hdr
);
2922 if (!hdrvalid
&& IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
) {
2923 const char *emsg
= "Message has corrupt header";
2924 zserv_log_message(emsg
, msg
, NULL
);
2930 zvrf
= zebra_vrf_lookup_by_id(hdr
.vrf_id
);
2931 if (!zvrf
&& IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
) {
2932 const char *emsg
= "Message specifies unknown VRF";
2933 zserv_log_message(emsg
, msg
, &hdr
);
2938 /* process commands */
2939 zserv_handle_commands(client
, &hdr
, msg
, zvrf
);
2946 /* Handler of zebra service request. */
2947 static int zserv_read(struct thread
*thread
)
2950 struct zserv
*client
;
2952 #if defined(HANDLE_ZAPI_FUZZING)
2955 int packets
= zebrad
.packets_to_process
;
2957 /* Get thread data. Reset reading thread because I'm running. */
2958 sock
= THREAD_FD(thread
);
2959 client
= THREAD_ARG(thread
);
2961 if (client
->t_suicide
) {
2962 zebra_client_close(client
);
2972 already
= stream_get_endp(client
->ibuf_work
);
2974 /* Read length and command (if we don't have it already). */
2975 if (already
< ZEBRA_HEADER_SIZE
) {
2976 nb
= stream_read_try(client
->ibuf_work
, sock
,
2977 ZEBRA_HEADER_SIZE
- already
);
2978 if ((nb
== 0 || nb
== -1) && IS_ZEBRA_DEBUG_EVENT
)
2979 zlog_debug("connection closed socket [%d]",
2981 if ((nb
== 0 || nb
== -1))
2983 if (nb
!= (ssize_t
)(ZEBRA_HEADER_SIZE
- already
)) {
2984 /* Try again later. */
2987 already
= ZEBRA_HEADER_SIZE
;
2990 /* Reset to read from the beginning of the incoming packet. */
2991 stream_set_getp(client
->ibuf_work
, 0);
2993 /* Fetch header values */
2994 hdrvalid
= zapi_parse_header(client
->ibuf_work
, &hdr
);
2997 snprintf(errmsg
, sizeof(errmsg
),
2998 "%s: Message has corrupt header", __func__
);
2999 zserv_log_message(errmsg
, client
->ibuf_work
, NULL
);
3003 /* Validate header */
3004 if (hdr
.marker
!= ZEBRA_HEADER_MARKER
3005 || hdr
.version
!= ZSERV_VERSION
) {
3007 errmsg
, sizeof(errmsg
),
3008 "Message has corrupt header\n%s: socket %d version mismatch, marker %d, version %d",
3009 __func__
, sock
, hdr
.marker
, hdr
.version
);
3010 zserv_log_message(errmsg
, client
->ibuf_work
, &hdr
);
3013 if (hdr
.length
< ZEBRA_HEADER_SIZE
) {
3015 errmsg
, sizeof(errmsg
),
3016 "Message has corrupt header\n%s: socket %d message length %u is less than header size %d",
3017 __func__
, sock
, hdr
.length
, ZEBRA_HEADER_SIZE
);
3018 zserv_log_message(errmsg
, client
->ibuf_work
, &hdr
);
3021 if (hdr
.length
> STREAM_SIZE(client
->ibuf_work
)) {
3023 errmsg
, sizeof(errmsg
),
3024 "Message has corrupt header\n%s: socket %d message length %u exceeds buffer size %lu",
3025 __func__
, sock
, hdr
.length
,
3026 (unsigned long)STREAM_SIZE(client
->ibuf_work
));
3030 /* Read rest of data. */
3031 if (already
< hdr
.length
) {
3032 nb
= stream_read_try(client
->ibuf_work
, sock
,
3033 hdr
.length
- already
);
3034 if ((nb
== 0 || nb
== -1) && IS_ZEBRA_DEBUG_EVENT
)
3036 "connection closed [%d] when reading zebra data",
3038 if ((nb
== 0 || nb
== -1))
3040 if (nb
!= (ssize_t
)(hdr
.length
- already
)) {
3041 /* Try again later. */
3046 #if defined(HANDLE_ZAPI_FUZZING)
3047 zserv_write_incoming(client
->ibuf_work
, command
);
3049 hdr
.length
-= ZEBRA_HEADER_SIZE
;
3051 /* Debug packet information. */
3052 if (IS_ZEBRA_DEBUG_EVENT
)
3053 zlog_debug("zebra message comes from socket [%d]",
3056 if (IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
)
3057 zserv_log_message(NULL
, client
->ibuf_work
, &hdr
);
3059 client
->last_read_time
= monotime(NULL
);
3060 client
->last_read_cmd
= hdr
.command
;
3062 stream_set_getp(client
->ibuf_work
, 0);
3063 struct stream
*msg
= stream_dup(client
->ibuf_work
);
3065 stream_fifo_push(client
->ibuf_fifo
, msg
);
3067 if (client
->t_suicide
)
3071 stream_reset(client
->ibuf_work
);
3074 if (IS_ZEBRA_DEBUG_PACKET
)
3075 zlog_debug("Read %d packets",
3076 zebrad
.packets_to_process
- packets
);
3078 /* Schedule job to process those packets */
3079 thread_add_event(zebrad
.master
, &zserv_process_messages
, client
, 0,
3082 /* Reschedule ourselves */
3083 zebra_event(client
, ZEBRA_READ
);
3088 zebra_client_close(client
);
3092 static void zebra_event(struct zserv
*client
, enum event event
)
3096 thread_add_read(zebrad
.master
, zserv_read
, client
, client
->sock
,
3100 thread_add_write(zebrad
.master
, zserv_write
, client
,
3101 client
->sock
, &client
->t_write
);
3106 /* Accept code of zebra server socket. */
3107 static int zebra_accept(struct thread
*thread
)
3111 struct sockaddr_in client
;
3114 accept_sock
= THREAD_FD(thread
);
3116 /* Reregister myself. */
3117 thread_add_read(zebrad
.master
, zebra_accept
, NULL
, accept_sock
, NULL
);
3119 len
= sizeof(struct sockaddr_in
);
3120 client_sock
= accept(accept_sock
, (struct sockaddr
*)&client
, &len
);
3122 if (client_sock
< 0) {
3123 zlog_warn("Can't accept zebra socket: %s",
3124 safe_strerror(errno
));
3128 /* Make client socket non-blocking. */
3129 set_nonblocking(client_sock
);
3131 /* Create new zebra client. */
3132 zebra_client_create(client_sock
);
3137 /* Make zebra server socket, wiping any existing one (see bug #403). */
3138 void zebra_zserv_socket_init(char *path
)
3143 struct sockaddr_storage sa
;
3146 if (!frr_zclient_addr(&sa
, &sa_len
, path
))
3147 /* should be caught in zebra main() */
3151 old_mask
= umask(0077);
3153 /* Make UNIX domain socket. */
3154 sock
= socket(sa
.ss_family
, SOCK_STREAM
, 0);
3156 zlog_warn("Can't create zserv socket: %s",
3157 safe_strerror(errno
));
3159 "zebra can't provide full functionality due to above error");
3163 if (sa
.ss_family
!= AF_UNIX
) {
3164 sockopt_reuseaddr(sock
);
3165 sockopt_reuseport(sock
);
3167 struct sockaddr_un
*suna
= (struct sockaddr_un
*)&sa
;
3168 if (suna
->sun_path
[0])
3169 unlink(suna
->sun_path
);
3172 zserv_privs
.change(ZPRIVS_RAISE
);
3173 setsockopt_so_recvbuf(sock
, 1048576);
3174 setsockopt_so_sendbuf(sock
, 1048576);
3175 zserv_privs
.change(ZPRIVS_LOWER
);
3177 if (sa
.ss_family
!= AF_UNIX
&& zserv_privs
.change(ZPRIVS_RAISE
))
3178 zlog_err("Can't raise privileges");
3180 ret
= bind(sock
, (struct sockaddr
*)&sa
, sa_len
);
3182 zlog_warn("Can't bind zserv socket on %s: %s", path
,
3183 safe_strerror(errno
));
3185 "zebra can't provide full functionality due to above error");
3189 if (sa
.ss_family
!= AF_UNIX
&& zserv_privs
.change(ZPRIVS_LOWER
))
3190 zlog_err("Can't lower privileges");
3192 ret
= listen(sock
, 5);
3194 zlog_warn("Can't listen to zserv socket %s: %s", path
,
3195 safe_strerror(errno
));
3197 "zebra can't provide full functionality due to above error");
3204 thread_add_read(zebrad
.master
, zebra_accept
, NULL
, sock
, NULL
);
3207 #define ZEBRA_TIME_BUF 32
3208 static char *zserv_time_buf(time_t *time1
, char *buf
, int buflen
)
3213 assert(buf
!= NULL
);
3214 assert(buflen
>= ZEBRA_TIME_BUF
);
3215 assert(time1
!= NULL
);
3218 snprintf(buf
, buflen
, "never ");
3222 now
= monotime(NULL
);
3226 if (now
< ONE_DAY_SECOND
)
3227 snprintf(buf
, buflen
, "%02d:%02d:%02d", tm
->tm_hour
, tm
->tm_min
,
3229 else if (now
< ONE_WEEK_SECOND
)
3230 snprintf(buf
, buflen
, "%dd%02dh%02dm", tm
->tm_yday
, tm
->tm_hour
,
3233 snprintf(buf
, buflen
, "%02dw%dd%02dh", tm
->tm_yday
/ 7,
3234 tm
->tm_yday
- ((tm
->tm_yday
/ 7) * 7), tm
->tm_hour
);
3238 static void zebra_show_client_detail(struct vty
*vty
, struct zserv
*client
)
3240 char cbuf
[ZEBRA_TIME_BUF
], rbuf
[ZEBRA_TIME_BUF
];
3241 char wbuf
[ZEBRA_TIME_BUF
], nhbuf
[ZEBRA_TIME_BUF
], mbuf
[ZEBRA_TIME_BUF
];
3243 vty_out(vty
, "Client: %s", zebra_route_string(client
->proto
));
3244 if (client
->instance
)
3245 vty_out(vty
, " Instance: %d", client
->instance
);
3248 vty_out(vty
, "------------------------ \n");
3249 vty_out(vty
, "FD: %d \n", client
->sock
);
3250 vty_out(vty
, "Route Table ID: %d \n", client
->rtm_table
);
3252 vty_out(vty
, "Connect Time: %s \n",
3253 zserv_time_buf(&client
->connect_time
, cbuf
, ZEBRA_TIME_BUF
));
3254 if (client
->nh_reg_time
) {
3255 vty_out(vty
, "Nexthop Registry Time: %s \n",
3256 zserv_time_buf(&client
->nh_reg_time
, nhbuf
,
3258 if (client
->nh_last_upd_time
)
3259 vty_out(vty
, "Nexthop Last Update Time: %s \n",
3260 zserv_time_buf(&client
->nh_last_upd_time
, mbuf
,
3263 vty_out(vty
, "No Nexthop Update sent\n");
3265 vty_out(vty
, "Not registered for Nexthop Updates\n");
3267 vty_out(vty
, "Last Msg Rx Time: %s \n",
3268 zserv_time_buf(&client
->last_read_time
, rbuf
, ZEBRA_TIME_BUF
));
3269 vty_out(vty
, "Last Msg Tx Time: %s \n",
3270 zserv_time_buf(&client
->last_write_time
, wbuf
, ZEBRA_TIME_BUF
));
3271 if (client
->last_read_time
)
3272 vty_out(vty
, "Last Rcvd Cmd: %s \n",
3273 zserv_command_string(client
->last_read_cmd
));
3274 if (client
->last_write_time
)
3275 vty_out(vty
, "Last Sent Cmd: %s \n",
3276 zserv_command_string(client
->last_write_cmd
));
3279 vty_out(vty
, "Type Add Update Del \n");
3280 vty_out(vty
, "================================================== \n");
3281 vty_out(vty
, "IPv4 %-12d%-12d%-12d\n", client
->v4_route_add_cnt
,
3282 client
->v4_route_upd8_cnt
, client
->v4_route_del_cnt
);
3283 vty_out(vty
, "IPv6 %-12d%-12d%-12d\n", client
->v6_route_add_cnt
,
3284 client
->v6_route_upd8_cnt
, client
->v6_route_del_cnt
);
3285 vty_out(vty
, "Redist:v4 %-12d%-12d%-12d\n", client
->redist_v4_add_cnt
,
3286 0, client
->redist_v4_del_cnt
);
3287 vty_out(vty
, "Redist:v6 %-12d%-12d%-12d\n", client
->redist_v6_add_cnt
,
3288 0, client
->redist_v6_del_cnt
);
3289 vty_out(vty
, "Connected %-12d%-12d%-12d\n", client
->ifadd_cnt
, 0,
3291 vty_out(vty
, "BFD peer %-12d%-12d%-12d\n", client
->bfd_peer_add_cnt
,
3292 client
->bfd_peer_upd8_cnt
, client
->bfd_peer_del_cnt
);
3293 vty_out(vty
, "Interface Up Notifications: %d\n", client
->ifup_cnt
);
3294 vty_out(vty
, "Interface Down Notifications: %d\n", client
->ifdown_cnt
);
3295 vty_out(vty
, "VNI add notifications: %d\n", client
->vniadd_cnt
);
3296 vty_out(vty
, "VNI delete notifications: %d\n", client
->vnidel_cnt
);
3297 vty_out(vty
, "L3-VNI add notifications: %d\n", client
->l3vniadd_cnt
);
3298 vty_out(vty
, "L3-VNI delete notifications: %d\n", client
->l3vnidel_cnt
);
3299 vty_out(vty
, "MAC-IP add notifications: %d\n", client
->macipadd_cnt
);
3300 vty_out(vty
, "MAC-IP delete notifications: %d\n", client
->macipdel_cnt
);
3306 static void zebra_show_client_brief(struct vty
*vty
, struct zserv
*client
)
3308 char cbuf
[ZEBRA_TIME_BUF
], rbuf
[ZEBRA_TIME_BUF
];
3309 char wbuf
[ZEBRA_TIME_BUF
];
3311 vty_out(vty
, "%-8s%12s %12s%12s%8d/%-8d%8d/%-8d\n",
3312 zebra_route_string(client
->proto
),
3313 zserv_time_buf(&client
->connect_time
, cbuf
, ZEBRA_TIME_BUF
),
3314 zserv_time_buf(&client
->last_read_time
, rbuf
, ZEBRA_TIME_BUF
),
3315 zserv_time_buf(&client
->last_write_time
, wbuf
, ZEBRA_TIME_BUF
),
3316 client
->v4_route_add_cnt
+ client
->v4_route_upd8_cnt
,
3317 client
->v4_route_del_cnt
,
3318 client
->v6_route_add_cnt
+ client
->v6_route_upd8_cnt
,
3319 client
->v6_route_del_cnt
);
3322 struct zserv
*zebra_find_client(u_char proto
, u_short instance
)
3324 struct listnode
*node
, *nnode
;
3325 struct zserv
*client
;
3327 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, node
, nnode
, client
)) {
3328 if (client
->proto
== proto
&& client
->instance
== instance
)
3335 /* This command is for debugging purpose. */
3336 DEFUN (show_zebra_client
,
3337 show_zebra_client_cmd
,
3338 "show zebra client",
3341 "Client information\n")
3343 struct listnode
*node
;
3344 struct zserv
*client
;
3346 for (ALL_LIST_ELEMENTS_RO(zebrad
.client_list
, node
, client
))
3347 zebra_show_client_detail(vty
, client
);
3352 /* This command is for debugging purpose. */
3353 DEFUN (show_zebra_client_summary
,
3354 show_zebra_client_summary_cmd
,
3355 "show zebra client summary",
3358 "Client information brief\n"
3361 struct listnode
*node
;
3362 struct zserv
*client
;
3365 "Name Connect Time Last Read Last Write IPv4 Routes IPv6 Routes \n");
3367 "--------------------------------------------------------------------------------\n");
3369 for (ALL_LIST_ELEMENTS_RO(zebrad
.client_list
, node
, client
))
3370 zebra_show_client_brief(vty
, client
);
3372 vty_out(vty
, "Routes column shows (added+updated)/deleted\n");
3376 #if defined(HANDLE_ZAPI_FUZZING)
3377 void zserv_read_file(char *input
)
3380 struct zserv
*client
= NULL
;
3383 zebra_client_create(-1);
3384 client
= zebrad
.client_list
->head
->data
;
3387 fd
= open(input
, O_RDONLY
| O_NONBLOCK
);
3390 zebra_client_read(&t
);
3396 void zserv_init(void)
3398 /* Client list init. */
3399 zebrad
.client_list
= list_new();
3400 zebrad
.client_list
->del
= (void (*)(void *))zebra_client_free
;
3402 install_element(ENABLE_NODE
, &show_zebra_client_cmd
);
3403 install_element(ENABLE_NODE
, &show_zebra_client_summary_cmd
);