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_SERV
, ZEBRA_READ
, ZEBRA_WRITE
};
68 static void zebra_event(enum event event
, int sock
, struct zserv
*client
);
70 extern struct zebra_privs_t zserv_privs
;
72 static void zebra_client_close(struct zserv
*client
);
74 static int zserv_delayed_close(struct thread
*thread
)
76 struct zserv
*client
= THREAD_ARG(thread
);
78 client
->t_suicide
= NULL
;
79 zebra_client_close(client
);
83 static int zserv_flush_data(struct thread
*thread
)
85 struct zserv
*client
= THREAD_ARG(thread
);
87 client
->t_write
= NULL
;
88 if (client
->t_suicide
) {
89 zebra_client_close(client
);
92 switch (buffer_flush_available(client
->wb
, client
->sock
)) {
95 "%s: buffer_flush_available failed on zserv client fd %d, "
97 __func__
, client
->sock
);
98 zebra_client_close(client
);
102 client
->t_write
= NULL
;
103 thread_add_write(zebrad
.master
, zserv_flush_data
, client
,
104 client
->sock
, &client
->t_write
);
111 client
->last_write_time
= monotime(NULL
);
115 int zebra_server_send_message(struct zserv
*client
)
117 if (client
->t_suicide
)
120 if (client
->is_synchronous
)
123 stream_set_getp(client
->obuf
, 0);
124 client
->last_write_cmd
= stream_getw_from(client
->obuf
, 6);
125 switch (buffer_write(client
->wb
, client
->sock
,
126 STREAM_DATA(client
->obuf
),
127 stream_get_endp(client
->obuf
))) {
130 "%s: buffer_write failed to zserv client fd %d, closing",
131 __func__
, client
->sock
);
132 /* Schedule a delayed close since many of the functions that
134 one do not check the return code. They do not allow for the
135 possibility that an I/O error may have caused the client to
138 client
->t_suicide
= NULL
;
139 thread_add_event(zebrad
.master
, zserv_delayed_close
, client
, 0,
143 THREAD_OFF(client
->t_write
);
146 thread_add_write(zebrad
.master
, zserv_flush_data
, client
,
147 client
->sock
, &client
->t_write
);
151 client
->last_write_time
= monotime(NULL
);
155 static void zserv_encode_interface(struct stream
*s
, struct interface
*ifp
)
157 /* Interface information. */
158 stream_put(s
, ifp
->name
, INTERFACE_NAMSIZ
);
159 stream_putl(s
, ifp
->ifindex
);
160 stream_putc(s
, ifp
->status
);
161 stream_putq(s
, ifp
->flags
);
162 stream_putc(s
, ifp
->ptm_enable
);
163 stream_putc(s
, ifp
->ptm_status
);
164 stream_putl(s
, ifp
->metric
);
165 stream_putl(s
, ifp
->speed
);
166 stream_putl(s
, ifp
->mtu
);
167 stream_putl(s
, ifp
->mtu6
);
168 stream_putl(s
, ifp
->bandwidth
);
169 stream_putl(s
, ifp
->ll_type
);
170 stream_putl(s
, ifp
->hw_addr_len
);
171 if (ifp
->hw_addr_len
)
172 stream_put(s
, ifp
->hw_addr
, ifp
->hw_addr_len
);
174 /* Then, Traffic Engineering parameters if any */
175 if (HAS_LINK_PARAMS(ifp
) && IS_LINK_PARAMS_SET(ifp
->link_params
)) {
177 zebra_interface_link_params_write(s
, ifp
);
181 /* Write packet size. */
182 stream_putw_at(s
, 0, stream_get_endp(s
));
185 static void zserv_encode_vrf(struct stream
*s
, struct zebra_vrf
*zvrf
)
187 struct vrf_data data
;
188 const char *netns_name
= zvrf_ns_name(zvrf
);
190 data
.l
.table_id
= zvrf
->table_id
;
193 strlcpy(data
.l
.netns_name
, basename((char *)netns_name
),
196 memset(data
.l
.netns_name
, 0, NS_NAMSIZ
);
197 /* Pass the tableid and the netns NAME */
198 stream_put(s
, &data
, sizeof(struct vrf_data
));
199 /* Interface information. */
200 stream_put(s
, zvrf_name(zvrf
), VRF_NAMSIZ
);
201 /* Write packet size. */
202 stream_putw_at(s
, 0, stream_get_endp(s
));
205 /* Interface is added. Send ZEBRA_INTERFACE_ADD to client. */
207 * This function is called in the following situations:
208 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
210 * - at startup, when zebra figures out the available interfaces
211 * - when an interface is added (where support for
212 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
213 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
216 int zsend_interface_add(struct zserv
*client
, struct interface
*ifp
)
223 zclient_create_header(s
, ZEBRA_INTERFACE_ADD
, ifp
->vrf_id
);
224 zserv_encode_interface(s
, ifp
);
227 return zebra_server_send_message(client
);
230 /* Interface deletion from zebra daemon. */
231 int zsend_interface_delete(struct zserv
*client
, struct interface
*ifp
)
238 zclient_create_header(s
, ZEBRA_INTERFACE_DELETE
, ifp
->vrf_id
);
239 zserv_encode_interface(s
, ifp
);
242 return zebra_server_send_message(client
);
245 int zsend_vrf_add(struct zserv
*client
, struct zebra_vrf
*zvrf
)
252 zclient_create_header(s
, ZEBRA_VRF_ADD
, zvrf_id(zvrf
));
253 zserv_encode_vrf(s
, zvrf
);
255 client
->vrfadd_cnt
++;
256 return zebra_server_send_message(client
);
259 /* VRF deletion from zebra daemon. */
260 int zsend_vrf_delete(struct zserv
*client
, struct zebra_vrf
*zvrf
)
267 zclient_create_header(s
, ZEBRA_VRF_DELETE
, zvrf_id(zvrf
));
268 zserv_encode_vrf(s
, zvrf
);
270 client
->vrfdel_cnt
++;
271 return zebra_server_send_message(client
);
274 int zsend_interface_link_params(struct zserv
*client
, struct interface
*ifp
)
278 /* Check this client need interface information. */
282 if (!ifp
->link_params
)
287 zclient_create_header(s
, ZEBRA_INTERFACE_LINK_PARAMS
, ifp
->vrf_id
);
289 /* Add Interface Index */
290 stream_putl(s
, ifp
->ifindex
);
292 /* Then TE Link Parameters */
293 if (zebra_interface_link_params_write(s
, ifp
) == 0)
296 /* Write packet size. */
297 stream_putw_at(s
, 0, stream_get_endp(s
));
299 return zebra_server_send_message(client
);
302 /* Interface address is added/deleted. Send ZEBRA_INTERFACE_ADDRESS_ADD or
303 * ZEBRA_INTERFACE_ADDRESS_DELETE to the client.
305 * A ZEBRA_INTERFACE_ADDRESS_ADD is sent in the following situations:
306 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
307 * from the client, after the ZEBRA_INTERFACE_ADD has been
308 * sent from zebra to the client
309 * - redistribute new address info to all clients in the following situations
310 * - at startup, when zebra figures out the available interfaces
311 * - when an interface is added (where support for
312 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
313 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
315 * - for the vty commands "ip address A.B.C.D/M [<secondary>|<label LINE>]"
316 * and "no bandwidth <1-10000000>", "ipv6 address X:X::X:X/M"
317 * - when an RTM_NEWADDR message is received from the kernel,
319 * The call tree that triggers ZEBRA_INTERFACE_ADDRESS_DELETE:
321 * zsend_interface_address(DELETE)
324 * zebra_interface_address_delete_update
326 * | | if_delete_update
328 * ip_address_uninstall connected_delete_ipv4
329 * [ipv6_addresss_uninstall] [connected_delete_ipv6]
332 * | RTM_NEWADDR on routing/netlink socket
335 * "no ip address A.B.C.D/M [label LINE]"
336 * "no ip address A.B.C.D/M secondary"
337 * ["no ipv6 address X:X::X:X/M"]
340 int zsend_interface_address(int cmd
, struct zserv
*client
,
341 struct interface
*ifp
, struct connected
*ifc
)
350 zclient_create_header(s
, cmd
, ifp
->vrf_id
);
351 stream_putl(s
, ifp
->ifindex
);
353 /* Interface address flag. */
354 stream_putc(s
, ifc
->flags
);
356 /* Prefix information. */
358 stream_putc(s
, p
->family
);
359 blen
= prefix_blen(p
);
360 stream_put(s
, &p
->u
.prefix
, blen
);
363 * XXX gnu version does not send prefixlen for
364 * ZEBRA_INTERFACE_ADDRESS_DELETE
365 * but zebra_interface_address_delete_read() in the gnu version
368 stream_putc(s
, p
->prefixlen
);
371 p
= ifc
->destination
;
373 stream_put(s
, &p
->u
.prefix
, blen
);
375 stream_put(s
, NULL
, blen
);
377 /* Write packet size. */
378 stream_putw_at(s
, 0, stream_get_endp(s
));
380 client
->connected_rt_add_cnt
++;
381 return zebra_server_send_message(client
);
384 static int zsend_interface_nbr_address(int cmd
, struct zserv
*client
,
385 struct interface
*ifp
,
386 struct nbr_connected
*ifc
)
395 zclient_create_header(s
, cmd
, ifp
->vrf_id
);
396 stream_putl(s
, ifp
->ifindex
);
398 /* Prefix information. */
400 stream_putc(s
, p
->family
);
401 blen
= prefix_blen(p
);
402 stream_put(s
, &p
->u
.prefix
, blen
);
405 * XXX gnu version does not send prefixlen for
406 * ZEBRA_INTERFACE_ADDRESS_DELETE
407 * but zebra_interface_address_delete_read() in the gnu version
410 stream_putc(s
, p
->prefixlen
);
412 /* Write packet size. */
413 stream_putw_at(s
, 0, stream_get_endp(s
));
415 return zebra_server_send_message(client
);
418 /* Interface address addition. */
419 static void zebra_interface_nbr_address_add_update(struct interface
*ifp
,
420 struct nbr_connected
*ifc
)
422 struct listnode
*node
, *nnode
;
423 struct zserv
*client
;
426 if (IS_ZEBRA_DEBUG_EVENT
) {
427 char buf
[INET6_ADDRSTRLEN
];
431 "MESSAGE: ZEBRA_INTERFACE_NBR_ADDRESS_ADD %s/%d on %s",
432 inet_ntop(p
->family
, &p
->u
.prefix
, buf
,
434 p
->prefixlen
, ifc
->ifp
->name
);
437 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, node
, nnode
, client
))
438 zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_ADD
,
442 /* Interface address deletion. */
443 static void zebra_interface_nbr_address_delete_update(struct interface
*ifp
,
444 struct nbr_connected
*ifc
)
446 struct listnode
*node
, *nnode
;
447 struct zserv
*client
;
450 if (IS_ZEBRA_DEBUG_EVENT
) {
451 char buf
[INET6_ADDRSTRLEN
];
455 "MESSAGE: ZEBRA_INTERFACE_NBR_ADDRESS_DELETE %s/%d on %s",
456 inet_ntop(p
->family
, &p
->u
.prefix
, buf
,
458 p
->prefixlen
, ifc
->ifp
->name
);
461 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, node
, nnode
, client
))
462 zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
,
466 /* Send addresses on interface to client */
467 int zsend_interface_addresses(struct zserv
*client
, struct interface
*ifp
)
469 struct listnode
*cnode
, *cnnode
;
471 struct nbr_connected
*nc
;
473 /* Send interface addresses. */
474 for (ALL_LIST_ELEMENTS(ifp
->connected
, cnode
, cnnode
, c
)) {
475 if (!CHECK_FLAG(c
->conf
, ZEBRA_IFC_REAL
))
478 if (zsend_interface_address(ZEBRA_INTERFACE_ADDRESS_ADD
, client
,
484 /* Send interface neighbors. */
485 for (ALL_LIST_ELEMENTS(ifp
->nbr_connected
, cnode
, cnnode
, nc
)) {
486 if (zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_ADD
,
495 /* Notify client about interface moving from one VRF to another.
496 * Whether client is interested in old and new VRF is checked by caller.
498 int zsend_interface_vrf_update(struct zserv
*client
, struct interface
*ifp
,
506 zclient_create_header(s
, ZEBRA_INTERFACE_VRF_UPDATE
, ifp
->vrf_id
);
508 /* Fill in the ifIndex of the interface and its new VRF (id) */
509 stream_putl(s
, ifp
->ifindex
);
510 stream_putl(s
, vrf_id
);
512 /* Write packet size. */
513 stream_putw_at(s
, 0, stream_get_endp(s
));
515 client
->if_vrfchg_cnt
++;
516 return zebra_server_send_message(client
);
519 /* Add new nbr connected IPv6 address */
520 void nbr_connected_add_ipv6(struct interface
*ifp
, struct in6_addr
*address
)
522 struct nbr_connected
*ifc
;
526 IPV6_ADDR_COPY(&p
.u
.prefix
, address
);
527 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
529 if (!(ifc
= listnode_head(ifp
->nbr_connected
))) {
531 ifc
= nbr_connected_new();
532 ifc
->address
= prefix_new();
534 listnode_add(ifp
->nbr_connected
, ifc
);
537 prefix_copy(ifc
->address
, &p
);
539 zebra_interface_nbr_address_add_update(ifp
, ifc
);
541 if_nbr_ipv6ll_to_ipv4ll_neigh_update(ifp
, address
, 1);
544 void nbr_connected_delete_ipv6(struct interface
*ifp
, struct in6_addr
*address
)
546 struct nbr_connected
*ifc
;
550 IPV6_ADDR_COPY(&p
.u
.prefix
, address
);
551 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
553 ifc
= nbr_connected_check(ifp
, &p
);
557 listnode_delete(ifp
->nbr_connected
, ifc
);
559 zebra_interface_nbr_address_delete_update(ifp
, ifc
);
561 if_nbr_ipv6ll_to_ipv4ll_neigh_update(ifp
, address
, 0);
563 nbr_connected_free(ifc
);
567 * The cmd passed to zsend_interface_update may be ZEBRA_INTERFACE_UP or
568 * ZEBRA_INTERFACE_DOWN.
570 * The ZEBRA_INTERFACE_UP message is sent from the zebra server to
571 * the clients in one of 2 situations:
572 * - an if_up is detected e.g., as a result of an RTM_IFINFO message
573 * - a vty command modifying the bandwidth of an interface is received.
574 * The ZEBRA_INTERFACE_DOWN message is sent when an if_down is detected.
576 int zsend_interface_update(int cmd
, struct zserv
*client
, struct interface
*ifp
)
583 zclient_create_header(s
, cmd
, ifp
->vrf_id
);
584 zserv_encode_interface(s
, ifp
);
586 if (cmd
== ZEBRA_INTERFACE_UP
)
589 client
->ifdown_cnt
++;
591 return zebra_server_send_message(client
);
594 int zsend_redistribute_route(int cmd
, struct zserv
*client
, struct prefix
*p
,
595 struct prefix
*src_p
, struct route_entry
*re
)
597 struct zapi_route api
;
598 struct zapi_nexthop
*api_nh
;
599 struct nexthop
*nexthop
;
602 memset(&api
, 0, sizeof(api
));
603 api
.vrf_id
= re
->vrf_id
;
605 api
.instance
= re
->instance
;
606 api
.flags
= re
->flags
;
611 SET_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
);
612 memcpy(&api
.src_prefix
, src_p
, sizeof(api
.src_prefix
));
616 if (re
->nexthop_active_num
) {
617 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
618 api
.nexthop_num
= re
->nexthop_active_num
;
620 for (nexthop
= re
->ng
.nexthop
; nexthop
; nexthop
= nexthop
->next
) {
621 if (!CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
624 api_nh
= &api
.nexthops
[count
];
625 api_nh
->vrf_id
= nexthop
->vrf_id
;
626 api_nh
->type
= nexthop
->type
;
627 switch (nexthop
->type
) {
628 case NEXTHOP_TYPE_BLACKHOLE
:
629 api_nh
->bh_type
= nexthop
->bh_type
;
631 case NEXTHOP_TYPE_IPV4
:
632 api_nh
->gate
.ipv4
= nexthop
->gate
.ipv4
;
634 case NEXTHOP_TYPE_IPV4_IFINDEX
:
635 api_nh
->gate
.ipv4
= nexthop
->gate
.ipv4
;
636 api_nh
->ifindex
= nexthop
->ifindex
;
638 case NEXTHOP_TYPE_IFINDEX
:
639 api_nh
->ifindex
= nexthop
->ifindex
;
641 case NEXTHOP_TYPE_IPV6
:
642 api_nh
->gate
.ipv6
= nexthop
->gate
.ipv6
;
644 case NEXTHOP_TYPE_IPV6_IFINDEX
:
645 api_nh
->gate
.ipv6
= nexthop
->gate
.ipv6
;
646 api_nh
->ifindex
= nexthop
->ifindex
;
652 SET_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
);
653 api
.distance
= re
->distance
;
654 SET_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
);
655 api
.metric
= re
->metric
;
657 SET_FLAG(api
.message
, ZAPI_MESSAGE_TAG
);
660 SET_FLAG(api
.message
, ZAPI_MESSAGE_MTU
);
663 /* Encode route and send. */
664 if (zapi_route_encode(cmd
, client
->obuf
, &api
) < 0)
667 if (IS_ZEBRA_DEBUG_SEND
) {
668 char buf_prefix
[PREFIX_STRLEN
];
669 prefix2str(&api
.prefix
, buf_prefix
, sizeof(buf_prefix
));
671 zlog_debug("%s: %s to client %s: type %s, vrf_id %d, p %s",
672 __func__
, zserv_command_string(cmd
),
673 zebra_route_string(client
->proto
),
674 zebra_route_string(api
.type
), api
.vrf_id
,
677 return zebra_server_send_message(client
);
680 static int zsend_write_nexthop(struct stream
*s
, struct nexthop
*nexthop
)
682 stream_putc(s
, nexthop
->type
);
683 switch (nexthop
->type
) {
684 case NEXTHOP_TYPE_IPV4
:
685 case NEXTHOP_TYPE_IPV4_IFINDEX
:
686 stream_put_in_addr(s
, &nexthop
->gate
.ipv4
);
687 stream_putl(s
, nexthop
->ifindex
);
689 case NEXTHOP_TYPE_IPV6
:
690 stream_put(s
, &nexthop
->gate
.ipv6
, 16);
692 case NEXTHOP_TYPE_IPV6_IFINDEX
:
693 stream_put(s
, &nexthop
->gate
.ipv6
, 16);
694 stream_putl(s
, nexthop
->ifindex
);
696 case NEXTHOP_TYPE_IFINDEX
:
697 stream_putl(s
, nexthop
->ifindex
);
706 /* Nexthop register */
707 static int zserv_rnh_register(struct zserv
*client
, u_short length
,
708 rnh_type_t type
, struct zebra_vrf
*zvrf
)
716 if (IS_ZEBRA_DEBUG_NHT
)
718 "rnh_register msg from client %s: length=%d, type=%s\n",
719 zebra_route_string(client
->proto
), length
,
720 (type
== RNH_NEXTHOP_TYPE
) ? "nexthop" : "route");
724 client
->nh_reg_time
= monotime(NULL
);
727 STREAM_GETC(s
, flags
);
728 STREAM_GETW(s
, p
.family
);
729 STREAM_GETC(s
, p
.prefixlen
);
731 if (p
.family
== AF_INET
) {
732 if (p
.prefixlen
> IPV4_MAX_BITLEN
) {
734 "%s: Specified prefix length %d is too large for a v4 address",
735 __PRETTY_FUNCTION__
, p
.prefixlen
);
738 STREAM_GET(&p
.u
.prefix4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
739 l
+= IPV4_MAX_BYTELEN
;
740 } else if (p
.family
== AF_INET6
) {
741 if (p
.prefixlen
> IPV6_MAX_BITLEN
) {
743 "%s: Specified prefix length %d is to large for a v6 address",
744 __PRETTY_FUNCTION__
, p
.prefixlen
);
747 STREAM_GET(&p
.u
.prefix6
, s
, IPV6_MAX_BYTELEN
);
748 l
+= IPV6_MAX_BYTELEN
;
751 "rnh_register: Received unknown family type %d\n",
755 rnh
= zebra_add_rnh(&p
, zvrf_id(zvrf
), type
);
756 if (type
== RNH_NEXTHOP_TYPE
) {
758 && !CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
))
759 SET_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
);
761 && CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
))
762 UNSET_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
);
763 } else if (type
== RNH_IMPORT_CHECK_TYPE
) {
765 && !CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
))
766 SET_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
);
767 else if (!flags
&& CHECK_FLAG(rnh
->flags
,
768 ZEBRA_NHT_EXACT_MATCH
))
769 UNSET_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
);
772 zebra_add_rnh_client(rnh
, client
, type
, zvrf_id(zvrf
));
773 /* Anything not AF_INET/INET6 has been filtered out above */
774 zebra_evaluate_rnh(zvrf_id(zvrf
), p
.family
, 1, type
, &p
);
781 /* Nexthop register */
782 static int zserv_rnh_unregister(struct zserv
*client
, u_short length
,
783 rnh_type_t type
, struct zebra_vrf
*zvrf
)
790 if (IS_ZEBRA_DEBUG_NHT
)
791 zlog_debug("rnh_unregister msg from client %s: length=%d\n",
792 zebra_route_string(client
->proto
), length
);
799 STREAM_GETC(s
, flags
);
803 STREAM_GETW(s
, p
.family
);
804 STREAM_GETC(s
, p
.prefixlen
);
806 if (p
.family
== AF_INET
) {
807 if (p
.prefixlen
> IPV4_MAX_BITLEN
) {
809 "%s: Specified prefix length %d is to large for a v4 address",
810 __PRETTY_FUNCTION__
, p
.prefixlen
);
813 STREAM_GET(&p
.u
.prefix4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
814 l
+= IPV4_MAX_BYTELEN
;
815 } else if (p
.family
== AF_INET6
) {
816 if (p
.prefixlen
> IPV6_MAX_BITLEN
) {
818 "%s: Specified prefix length %d is to large for a v6 address",
819 __PRETTY_FUNCTION__
, p
.prefixlen
);
822 STREAM_GET(&p
.u
.prefix6
, s
, IPV6_MAX_BYTELEN
);
823 l
+= IPV6_MAX_BYTELEN
;
826 "rnh_register: Received unknown family type %d\n",
830 rnh
= zebra_lookup_rnh(&p
, zvrf_id(zvrf
), type
);
832 client
->nh_dereg_time
= monotime(NULL
);
833 zebra_remove_rnh_client(rnh
, client
, type
);
840 #define ZEBRA_MIN_FEC_LENGTH 5
843 static int zserv_fec_register(struct zserv
*client
, u_short length
)
846 struct zebra_vrf
*zvrf
;
850 u_int32_t label_index
= MPLS_INVALID_LABEL_INDEX
;
853 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
855 return 0; // unexpected
858 * The minimum amount of data that can be sent for one fec
861 if (length
< ZEBRA_MIN_FEC_LENGTH
) {
863 "fec_register: Received a fec register of length %d, it is of insufficient size to properly decode",
869 STREAM_GETW(s
, flags
);
870 memset(&p
, 0, sizeof(p
));
871 STREAM_GETW(s
, p
.family
);
872 if (p
.family
!= AF_INET
&& p
.family
!= AF_INET6
) {
874 "fec_register: Received unknown family type %d\n",
878 STREAM_GETC(s
, p
.prefixlen
);
879 if ((p
.family
== AF_INET
&& p
.prefixlen
> IPV4_MAX_BITLEN
)
880 || (p
.family
== AF_INET6
881 && p
.prefixlen
> IPV6_MAX_BITLEN
)) {
883 "%s: Specified prefix length: %d is to long for %d",
884 __PRETTY_FUNCTION__
, p
.prefixlen
, p
.family
);
888 STREAM_GET(&p
.u
.prefix
, s
, PSIZE(p
.prefixlen
));
889 l
+= PSIZE(p
.prefixlen
);
890 if (flags
& ZEBRA_FEC_REGISTER_LABEL_INDEX
) {
891 STREAM_GETL(s
, label_index
);
894 label_index
= MPLS_INVALID_LABEL_INDEX
;
895 zebra_mpls_fec_register(zvrf
, &p
, label_index
, client
);
903 static int zserv_fec_unregister(struct zserv
*client
, u_short length
)
906 struct zebra_vrf
*zvrf
;
912 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
914 return 0; // unexpected
917 * The minimum amount of data that can be sent for one
920 if (length
< ZEBRA_MIN_FEC_LENGTH
) {
922 "fec_unregister: Received a fec unregister of length %d, it is of insufficient size to properly decode",
928 STREAM_GETW(s
, flags
);
932 memset(&p
, 0, sizeof(p
));
933 STREAM_GETW(s
, p
.family
);
934 if (p
.family
!= AF_INET
&& p
.family
!= AF_INET6
) {
936 "fec_unregister: Received unknown family type %d\n",
940 STREAM_GETC(s
, p
.prefixlen
);
941 if ((p
.family
== AF_INET
&& p
.prefixlen
> IPV4_MAX_BITLEN
)
942 || (p
.family
== AF_INET6
943 && p
.prefixlen
> IPV6_MAX_BITLEN
)) {
945 "%s: Received prefix length %d which is greater than %d can support",
946 __PRETTY_FUNCTION__
, p
.prefixlen
, p
.family
);
950 STREAM_GET(&p
.u
.prefix
, s
, PSIZE(p
.prefixlen
));
951 l
+= PSIZE(p
.prefixlen
);
952 zebra_mpls_fec_unregister(zvrf
, &p
, client
);
960 Modified version of zsend_ipv4_nexthop_lookup():
961 Query unicast rib if nexthop is not found on mrib.
962 Returns both route metric and protocol distance.
964 static int zsend_ipv4_nexthop_lookup_mrib(struct zserv
*client
,
966 struct route_entry
*re
,
967 struct zebra_vrf
*zvrf
)
972 struct nexthop
*nexthop
;
974 /* Get output stream. */
978 /* Fill in result. */
979 zclient_create_header(s
, ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB
, zvrf_id(zvrf
));
980 stream_put_in_addr(s
, &addr
);
983 stream_putc(s
, re
->distance
);
984 stream_putl(s
, re
->metric
);
986 nump
= stream_get_endp(
987 s
); /* remember position for nexthop_num */
988 stream_putc(s
, 0); /* reserve room for nexthop_num */
989 /* Only non-recursive routes are elegible to resolve the nexthop
991 * are looking up. Therefore, we will just iterate over the top
992 * chain of nexthops. */
993 for (nexthop
= re
->ng
.nexthop
; nexthop
; nexthop
= nexthop
->next
)
994 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
995 num
+= zsend_write_nexthop(s
, nexthop
);
997 stream_putc_at(s
, nump
, num
); /* store nexthop_num */
999 stream_putc(s
, 0); /* distance */
1000 stream_putl(s
, 0); /* metric */
1001 stream_putc(s
, 0); /* nexthop_num */
1004 stream_putw_at(s
, 0, stream_get_endp(s
));
1006 return zebra_server_send_message(client
);
1009 int zsend_route_notify_owner(struct route_entry
*re
, struct prefix
*p
,
1010 enum zapi_route_notify_owner note
)
1012 struct zserv
*client
;
1016 client
= zebra_find_client(re
->type
, re
->instance
);
1017 if (!client
|| !client
->notify_owner
) {
1018 if (IS_ZEBRA_DEBUG_PACKET
) {
1019 char buff
[PREFIX_STRLEN
];
1022 "Not Notifying Owner: %u about prefix %s(%u) %d",
1023 re
->type
, prefix2str(p
, buff
, sizeof(buff
)),
1029 if (IS_ZEBRA_DEBUG_PACKET
) {
1030 char buff
[PREFIX_STRLEN
];
1032 zlog_debug("Notifying Owner: %u about prefix %s(%u) %d",
1033 re
->type
, prefix2str(p
, buff
, sizeof(buff
)),
1040 zclient_create_header(s
, ZEBRA_ROUTE_NOTIFY_OWNER
, re
->vrf_id
);
1042 stream_put(s
, ¬e
, sizeof(note
));
1044 stream_putc(s
, p
->family
);
1046 blen
= prefix_blen(p
);
1047 stream_putc(s
, p
->prefixlen
);
1048 stream_put(s
, &p
->u
.prefix
, blen
);
1050 stream_putl(s
, re
->table
);
1052 stream_putw_at(s
, 0, stream_get_endp(s
));
1054 return zebra_server_send_message(client
);
1057 void zsend_rule_notify_owner(struct zebra_pbr_rule
*rule
,
1058 enum zapi_rule_notify_owner note
)
1060 struct listnode
*node
;
1061 struct zserv
*client
;
1064 if (IS_ZEBRA_DEBUG_PACKET
) {
1065 zlog_debug("%s: Notifying %u",
1066 __PRETTY_FUNCTION__
, rule
->unique
);
1069 for (ALL_LIST_ELEMENTS_RO(zebrad
.client_list
, node
, client
)) {
1070 if (rule
->sock
== client
->sock
)
1080 zclient_create_header(s
, ZEBRA_RULE_NOTIFY_OWNER
, VRF_DEFAULT
);
1081 stream_put(s
, ¬e
, sizeof(note
));
1082 stream_putl(s
, rule
->seq
);
1083 stream_putl(s
, rule
->priority
);
1084 stream_putl(s
, rule
->unique
);
1086 stream_putl(s
, rule
->ifp
->ifindex
);
1090 stream_putw_at(s
, 0, stream_get_endp(s
));
1092 zebra_server_send_message(client
);
1095 /* Router-id is updated. Send ZEBRA_ROUTER_ID_ADD to client. */
1096 int zsend_router_id_update(struct zserv
*client
, struct prefix
*p
,
1102 /* Check this client need interface information. */
1103 if (!vrf_bitmap_check(client
->ridinfo
, vrf_id
))
1110 zclient_create_header(s
, ZEBRA_ROUTER_ID_UPDATE
, vrf_id
);
1112 /* Prefix information. */
1113 stream_putc(s
, p
->family
);
1114 blen
= prefix_blen(p
);
1115 stream_put(s
, &p
->u
.prefix
, blen
);
1116 stream_putc(s
, p
->prefixlen
);
1118 /* Write packet size. */
1119 stream_putw_at(s
, 0, stream_get_endp(s
));
1121 return zebra_server_send_message(client
);
1125 * Function used by Zebra to send a PW status update to LDP daemon
1127 int zsend_pw_update(struct zserv
*client
, struct zebra_pw
*pw
)
1134 zclient_create_header(s
, ZEBRA_PW_STATUS_UPDATE
, pw
->vrf_id
);
1135 stream_write(s
, pw
->ifname
, IF_NAMESIZE
);
1136 stream_putl(s
, pw
->ifindex
);
1137 stream_putl(s
, pw
->status
);
1139 /* Put length at the first point of the stream. */
1140 stream_putw_at(s
, 0, stream_get_endp(s
));
1142 return zebra_server_send_message(client
);
1145 /* Register zebra server interface information. Send current all
1146 interface and address information. */
1147 static int zread_interface_add(struct zserv
*client
, u_short length
,
1148 struct zebra_vrf
*zvrf
)
1151 struct interface
*ifp
;
1153 /* Interface information is needed. */
1154 vrf_bitmap_set(client
->ifinfo
, zvrf_id(zvrf
));
1156 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
1157 FOR_ALL_INTERFACES (vrf
, ifp
) {
1158 /* Skip pseudo interface. */
1159 if (!CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_ACTIVE
))
1162 if (zsend_interface_add(client
, ifp
) < 0)
1165 if (zsend_interface_addresses(client
, ifp
) < 0)
1172 /* Unregister zebra server interface information. */
1173 static int zread_interface_delete(struct zserv
*client
, u_short length
,
1174 struct zebra_vrf
*zvrf
)
1176 vrf_bitmap_unset(client
->ifinfo
, zvrf_id(zvrf
));
1180 void zserv_nexthop_num_warn(const char *caller
, const struct prefix
*p
,
1181 const unsigned int nexthop_num
)
1183 if (nexthop_num
> multipath_num
) {
1184 char buff
[PREFIX2STR_BUFFER
];
1185 prefix2str(p
, buff
, sizeof(buff
));
1187 "%s: Prefix %s has %d nexthops, but we can only use the first %d",
1188 caller
, buff
, nexthop_num
, multipath_num
);
1192 static int zread_route_add(struct zserv
*client
, u_short length
,
1193 struct zebra_vrf
*zvrf
)
1196 struct zapi_route api
;
1197 struct zapi_nexthop
*api_nh
;
1199 struct prefix_ipv6
*src_p
= NULL
;
1200 struct route_entry
*re
;
1201 struct nexthop
*nexthop
= NULL
;
1203 vrf_id_t vrf_id
= 0;
1206 if (zapi_route_decode(s
, &api
) < 0)
1209 if (IS_ZEBRA_DEBUG_RECV
) {
1210 char buf_prefix
[PREFIX_STRLEN
];
1211 prefix2str(&api
.prefix
, buf_prefix
, sizeof(buf_prefix
));
1212 zlog_debug("%s: p=%s, ZAPI_MESSAGE_LABEL: %sset, flags=0x%x",
1213 __func__
, buf_prefix
,
1214 (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_LABEL
) ? ""
1219 /* Allocate new route. */
1220 vrf_id
= zvrf_id(zvrf
);
1221 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
1222 re
->type
= api
.type
;
1223 re
->instance
= api
.instance
;
1224 re
->flags
= api
.flags
;
1225 re
->uptime
= time(NULL
);
1226 re
->vrf_id
= vrf_id
;
1227 if (api
.tableid
&& vrf_id
== VRF_DEFAULT
)
1228 re
->table
= api
.tableid
;
1230 re
->table
= zvrf
->table_id
;
1233 * TBD should _all_ of the nexthop add operations use
1234 * api_nh->vrf_id instead of re->vrf_id ? I only changed
1235 * for cases NEXTHOP_TYPE_IPV4 and NEXTHOP_TYPE_IPV6.
1237 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
)) {
1238 for (i
= 0; i
< api
.nexthop_num
; i
++) {
1239 api_nh
= &api
.nexthops
[i
];
1240 ifindex_t ifindex
= 0;
1242 if (IS_ZEBRA_DEBUG_RECV
) {
1243 zlog_debug("nh type %d", api_nh
->type
);
1246 switch (api_nh
->type
) {
1247 case NEXTHOP_TYPE_IFINDEX
:
1248 nexthop
= route_entry_nexthop_ifindex_add(
1249 re
, api_nh
->ifindex
, api_nh
->vrf_id
);
1251 case NEXTHOP_TYPE_IPV4
:
1252 if (IS_ZEBRA_DEBUG_RECV
) {
1253 char nhbuf
[INET6_ADDRSTRLEN
] = {0};
1254 inet_ntop(AF_INET
, &api_nh
->gate
.ipv4
,
1255 nhbuf
, INET6_ADDRSTRLEN
);
1256 zlog_debug("%s: nh=%s, vrf_id=%d",
1260 nexthop
= route_entry_nexthop_ipv4_add(
1261 re
, &api_nh
->gate
.ipv4
, NULL
,
1264 case NEXTHOP_TYPE_IPV4_IFINDEX
: {
1266 struct ipaddr vtep_ip
;
1268 memset(&vtep_ip
, 0, sizeof(struct ipaddr
));
1269 if (CHECK_FLAG(api
.flags
,
1270 ZEBRA_FLAG_EVPN_ROUTE
)) {
1271 ifindex
= get_l3vni_svi_ifindex(vrf_id
);
1273 ifindex
= api_nh
->ifindex
;
1276 if (IS_ZEBRA_DEBUG_RECV
) {
1277 char nhbuf
[INET6_ADDRSTRLEN
] = {0};
1278 inet_ntop(AF_INET
, &api_nh
->gate
.ipv4
,
1279 nhbuf
, INET6_ADDRSTRLEN
);
1281 "%s: nh=%s, vrf_id=%d (re->vrf_id=%d), ifindex=%d",
1282 __func__
, nhbuf
, api_nh
->vrf_id
,
1283 re
->vrf_id
, ifindex
);
1285 nexthop
= route_entry_nexthop_ipv4_ifindex_add(
1286 re
, &api_nh
->gate
.ipv4
, NULL
, ifindex
,
1289 /* if this an EVPN route entry,
1290 program the nh as neigh
1292 if (CHECK_FLAG(api
.flags
,
1293 ZEBRA_FLAG_EVPN_ROUTE
)) {
1294 SET_FLAG(nexthop
->flags
,
1295 NEXTHOP_FLAG_EVPN_RVTEP
);
1296 vtep_ip
.ipa_type
= IPADDR_V4
;
1297 memcpy(&(vtep_ip
.ipaddr_v4
),
1298 &(api_nh
->gate
.ipv4
),
1299 sizeof(struct in_addr
));
1300 zebra_vxlan_evpn_vrf_route_add(
1301 vrf_id
, &api
.rmac
, &vtep_ip
,
1306 case NEXTHOP_TYPE_IPV6
:
1307 nexthop
= route_entry_nexthop_ipv6_add(
1308 re
, &api_nh
->gate
.ipv6
, api_nh
->vrf_id
);
1310 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1311 nexthop
= route_entry_nexthop_ipv6_ifindex_add(
1312 re
, &api_nh
->gate
.ipv6
, api_nh
->ifindex
,
1315 case NEXTHOP_TYPE_BLACKHOLE
:
1316 nexthop
= route_entry_nexthop_blackhole_add(
1317 re
, api_nh
->bh_type
);
1323 "%s: Nexthops Specified: %d but we failed to properly create one",
1324 __PRETTY_FUNCTION__
, api
.nexthop_num
);
1325 nexthops_free(re
->ng
.nexthop
);
1326 XFREE(MTYPE_RE
, re
);
1329 /* MPLS labels for BGP-LU or Segment Routing */
1330 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_LABEL
)
1331 && api_nh
->type
!= NEXTHOP_TYPE_IFINDEX
1332 && api_nh
->type
!= NEXTHOP_TYPE_BLACKHOLE
) {
1333 enum lsp_types_t label_type
;
1336 lsp_type_from_re_type(client
->proto
);
1338 if (IS_ZEBRA_DEBUG_RECV
) {
1340 "%s: adding %d labels of type %d (1st=%u)",
1341 __func__
, api_nh
->label_num
,
1342 label_type
, api_nh
->labels
[0]);
1345 nexthop_add_labels(nexthop
, label_type
,
1347 &api_nh
->labels
[0]);
1352 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
))
1353 re
->distance
= api
.distance
;
1354 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
))
1355 re
->metric
= api
.metric
;
1356 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_TAG
))
1358 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_MTU
))
1361 afi
= family2afi(api
.prefix
.family
);
1362 if (afi
!= AFI_IP6
&& CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
)) {
1363 zlog_warn("%s: Received SRC Prefix but afi is not v6",
1364 __PRETTY_FUNCTION__
);
1365 nexthops_free(re
->ng
.nexthop
);
1366 XFREE(MTYPE_RE
, re
);
1369 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
))
1370 src_p
= &api
.src_prefix
;
1372 ret
= rib_add_multipath(afi
, api
.safi
, &api
.prefix
, src_p
, re
);
1375 switch (api
.prefix
.family
) {
1378 client
->v4_route_add_cnt
++;
1380 client
->v4_route_upd8_cnt
++;
1384 client
->v6_route_add_cnt
++;
1386 client
->v6_route_upd8_cnt
++;
1393 static int zread_route_del(struct zserv
*client
, u_short length
,
1394 struct zebra_vrf
*zvrf
)
1397 struct zapi_route api
;
1399 struct prefix_ipv6
*src_p
= NULL
;
1402 if (zapi_route_decode(s
, &api
) < 0)
1405 afi
= family2afi(api
.prefix
.family
);
1406 if (afi
!= AFI_IP6
&& CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
)) {
1407 zlog_warn("%s: Received a src prefix while afi is not v6",
1408 __PRETTY_FUNCTION__
);
1411 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
))
1412 src_p
= &api
.src_prefix
;
1414 rib_delete(afi
, api
.safi
, zvrf_id(zvrf
), api
.type
, api
.instance
,
1415 api
.flags
, &api
.prefix
, src_p
, NULL
, zvrf
->table_id
,
1416 api
.metric
, false, &api
.rmac
);
1419 switch (api
.prefix
.family
) {
1421 client
->v4_route_del_cnt
++;
1424 client
->v6_route_del_cnt
++;
1431 /* This function support multiple nexthop. */
1433 * Parse the ZEBRA_IPV4_ROUTE_ADD sent from client. Update re and
1436 static int zread_ipv4_add(struct zserv
*client
, u_short length
,
1437 struct zebra_vrf
*zvrf
)
1440 struct route_entry
*re
;
1443 struct in_addr nhop_addr
;
1445 u_char nexthop_type
;
1450 enum lsp_types_t label_type
= ZEBRA_LSP_NONE
;
1452 struct nexthop
*nexthop
;
1453 enum blackhole_type bh_type
= BLACKHOLE_NULL
;
1455 /* Get input stream. */
1458 /* Allocate new re. */
1459 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
1461 /* Type, flags, message. */
1462 STREAM_GETC(s
, re
->type
);
1463 if (re
->type
> ZEBRA_ROUTE_MAX
) {
1464 zlog_warn("%s: Specified route type %d is not a legal value\n",
1465 __PRETTY_FUNCTION__
, re
->type
);
1466 XFREE(MTYPE_RE
, re
);
1469 STREAM_GETW(s
, re
->instance
);
1470 STREAM_GETL(s
, re
->flags
);
1471 STREAM_GETC(s
, message
);
1472 STREAM_GETW(s
, safi
);
1473 re
->uptime
= time(NULL
);
1476 memset(&p
, 0, sizeof(struct prefix_ipv4
));
1478 STREAM_GETC(s
, p
.prefixlen
);
1479 if (p
.prefixlen
> IPV4_MAX_BITLEN
) {
1481 "%s: Specified prefix length %d is greater than what v4 can be",
1482 __PRETTY_FUNCTION__
, p
.prefixlen
);
1483 XFREE(MTYPE_RE
, re
);
1486 STREAM_GET(&p
.u
.prefix4
, s
, PSIZE(p
.prefixlen
));
1489 re
->vrf_id
= zvrf_id(zvrf
);
1491 /* Nexthop parse. */
1492 if (CHECK_FLAG(message
, ZAPI_MESSAGE_NEXTHOP
)) {
1493 STREAM_GETC(s
, nexthop_num
);
1494 zserv_nexthop_num_warn(__func__
, (const struct prefix
*)&p
,
1497 if (CHECK_FLAG(message
, ZAPI_MESSAGE_LABEL
))
1498 label_type
= lsp_type_from_re_type(client
->proto
);
1500 for (i
= 0; i
< nexthop_num
; i
++) {
1501 STREAM_GETC(s
, nexthop_type
);
1503 switch (nexthop_type
) {
1504 case NEXTHOP_TYPE_IFINDEX
:
1505 STREAM_GETL(s
, ifindex
);
1506 route_entry_nexthop_ifindex_add(re
, ifindex
,
1509 case NEXTHOP_TYPE_IPV4
:
1510 STREAM_GET(&nhop_addr
.s_addr
, s
,
1512 nexthop
= route_entry_nexthop_ipv4_add(
1513 re
, &nhop_addr
, NULL
, re
->vrf_id
);
1514 /* For labeled-unicast, each nexthop is followed
1516 if (CHECK_FLAG(message
, ZAPI_MESSAGE_LABEL
)) {
1517 STREAM_GETL(s
, label
);
1518 nexthop_add_labels(nexthop
, label_type
,
1522 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1523 STREAM_GET(&nhop_addr
.s_addr
, s
,
1525 STREAM_GETL(s
, ifindex
);
1526 route_entry_nexthop_ipv4_ifindex_add(
1527 re
, &nhop_addr
, NULL
, ifindex
,
1530 case NEXTHOP_TYPE_IPV6
:
1532 "%s: Please use ZEBRA_ROUTE_ADD if you want to pass v6 nexthops",
1533 __PRETTY_FUNCTION__
);
1534 nexthops_free(re
->ng
.nexthop
);
1535 XFREE(MTYPE_RE
, re
);
1538 case NEXTHOP_TYPE_BLACKHOLE
:
1539 route_entry_nexthop_blackhole_add(re
, bh_type
);
1543 "%s: Specified nexthop type: %d does not exist",
1544 __PRETTY_FUNCTION__
, nexthop_type
);
1545 nexthops_free(re
->ng
.nexthop
);
1546 XFREE(MTYPE_RE
, re
);
1553 if (CHECK_FLAG(message
, ZAPI_MESSAGE_DISTANCE
))
1554 STREAM_GETC(s
, re
->distance
);
1557 if (CHECK_FLAG(message
, ZAPI_MESSAGE_METRIC
))
1558 STREAM_GETL(s
, re
->metric
);
1561 if (CHECK_FLAG(message
, ZAPI_MESSAGE_TAG
))
1562 STREAM_GETL(s
, re
->tag
);
1566 if (CHECK_FLAG(message
, ZAPI_MESSAGE_MTU
))
1567 STREAM_GETL(s
, re
->mtu
);
1572 re
->table
= zvrf
->table_id
;
1574 ret
= rib_add_multipath(AFI_IP
, safi
, &p
, NULL
, re
);
1578 client
->v4_route_add_cnt
++;
1580 client
->v4_route_upd8_cnt
++;
1585 nexthops_free(re
->ng
.nexthop
);
1586 XFREE(MTYPE_RE
, re
);
1590 /* Zebra server IPv4 prefix delete function. */
1591 static int zread_ipv4_delete(struct zserv
*client
, u_short length
,
1592 struct zebra_vrf
*zvrf
)
1595 struct zapi_ipv4 api
;
1601 /* Type, flags, message. */
1602 STREAM_GETC(s
, api
.type
);
1603 STREAM_GETW(s
, api
.instance
);
1604 STREAM_GETL(s
, api
.flags
);
1605 STREAM_GETC(s
, api
.message
);
1606 STREAM_GETW(s
, api
.safi
);
1609 memset(&p
, 0, sizeof(struct prefix
));
1611 STREAM_GETC(s
, p
.prefixlen
);
1612 if (p
.prefixlen
> IPV4_MAX_BITLEN
) {
1613 zlog_warn("%s: Passed in prefixlen %d is impossible",
1614 __PRETTY_FUNCTION__
, p
.prefixlen
);
1617 STREAM_GET(&p
.u
.prefix4
, s
, PSIZE(p
.prefixlen
));
1619 table_id
= zvrf
->table_id
;
1621 rib_delete(AFI_IP
, api
.safi
, zvrf_id(zvrf
), api
.type
, api
.instance
,
1622 api
.flags
, &p
, NULL
, NULL
, table_id
, 0, false, NULL
);
1623 client
->v4_route_del_cnt
++;
1629 /* MRIB Nexthop lookup for IPv4. */
1630 static int zread_ipv4_nexthop_lookup_mrib(struct zserv
*client
, u_short length
,
1631 struct zebra_vrf
*zvrf
)
1633 struct in_addr addr
;
1634 struct route_entry
*re
;
1636 STREAM_GET(&addr
.s_addr
, client
->ibuf
, IPV4_MAX_BYTELEN
);
1637 re
= rib_match_ipv4_multicast(zvrf_id(zvrf
), addr
, NULL
);
1638 return zsend_ipv4_nexthop_lookup_mrib(client
, addr
, re
, zvrf
);
1644 /* Zebra server IPv6 prefix add function. */
1645 static int zread_ipv4_route_ipv6_nexthop_add(struct zserv
*client
,
1647 struct zebra_vrf
*zvrf
)
1651 struct in6_addr nhop_addr
;
1652 struct route_entry
*re
;
1655 u_char nexthop_type
;
1658 static struct in6_addr nexthops
[MULTIPATH_NUM
];
1659 static unsigned int ifindices
[MULTIPATH_NUM
];
1661 static mpls_label_t labels
[MULTIPATH_NUM
];
1662 enum lsp_types_t label_type
= ZEBRA_LSP_NONE
;
1664 struct nexthop
*nexthop
;
1665 enum blackhole_type bh_type
= BLACKHOLE_NULL
;
1667 /* Get input stream. */
1670 memset(&nhop_addr
, 0, sizeof(struct in6_addr
));
1672 /* Allocate new re. */
1673 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
1675 /* Type, flags, message. */
1676 STREAM_GETC(s
, re
->type
);
1677 if (re
->type
> ZEBRA_ROUTE_MAX
) {
1678 zlog_warn("%s: Specified route type: %d is not a legal value\n",
1679 __PRETTY_FUNCTION__
, re
->type
);
1680 XFREE(MTYPE_RE
, re
);
1683 STREAM_GETW(s
, re
->instance
);
1684 STREAM_GETL(s
, re
->flags
);
1685 STREAM_GETC(s
, message
);
1686 STREAM_GETW(s
, safi
);
1687 re
->uptime
= time(NULL
);
1690 memset(&p
, 0, sizeof(struct prefix_ipv4
));
1692 STREAM_GETC(s
, p
.prefixlen
);
1693 if (p
.prefixlen
> IPV4_MAX_BITLEN
) {
1695 "%s: Prefix Length %d is greater than what a v4 address can use",
1696 __PRETTY_FUNCTION__
, p
.prefixlen
);
1697 XFREE(MTYPE_RE
, re
);
1700 STREAM_GET(&p
.u
.prefix4
, s
, PSIZE(p
.prefixlen
));
1703 re
->vrf_id
= zvrf_id(zvrf
);
1705 /* We need to give nh-addr, nh-ifindex with the same next-hop object
1706 * to the re to ensure that IPv6 multipathing works; need to coalesce
1707 * these. Clients should send the same number of paired set of
1708 * next-hop-addr/next-hop-ifindices. */
1709 if (CHECK_FLAG(message
, ZAPI_MESSAGE_NEXTHOP
)) {
1710 unsigned int nh_count
= 0;
1711 unsigned int if_count
= 0;
1712 unsigned int max_nh_if
= 0;
1714 STREAM_GETC(s
, nexthop_num
);
1715 zserv_nexthop_num_warn(__func__
, (const struct prefix
*)&p
,
1718 if (CHECK_FLAG(message
, ZAPI_MESSAGE_LABEL
))
1719 label_type
= lsp_type_from_re_type(client
->proto
);
1721 for (i
= 0; i
< nexthop_num
; i
++) {
1722 STREAM_GETC(s
, nexthop_type
);
1724 switch (nexthop_type
) {
1725 case NEXTHOP_TYPE_IPV6
:
1726 STREAM_GET(&nhop_addr
, s
, 16);
1727 if (nh_count
< MULTIPATH_NUM
) {
1728 /* For labeled-unicast, each nexthop is
1729 * followed by label. */
1730 if (CHECK_FLAG(message
,
1731 ZAPI_MESSAGE_LABEL
)) {
1732 STREAM_GETL(s
, label
);
1733 labels
[nh_count
] = label
;
1735 nexthops
[nh_count
] = nhop_addr
;
1739 case NEXTHOP_TYPE_IFINDEX
:
1740 if (if_count
< multipath_num
) {
1741 STREAM_GETL(s
, ifindices
[if_count
++]);
1744 case NEXTHOP_TYPE_BLACKHOLE
:
1745 route_entry_nexthop_blackhole_add(re
, bh_type
);
1749 "%s: Please use ZEBRA_ROUTE_ADD if you want to pass non v6 nexthops",
1750 __PRETTY_FUNCTION__
);
1751 nexthops_free(re
->ng
.nexthop
);
1752 XFREE(MTYPE_RE
, re
);
1757 max_nh_if
= (nh_count
> if_count
) ? nh_count
: if_count
;
1758 for (i
= 0; i
< max_nh_if
; i
++) {
1760 && !IN6_IS_ADDR_UNSPECIFIED(&nexthops
[i
])) {
1761 if ((i
< if_count
) && ifindices
[i
])
1763 route_entry_nexthop_ipv6_ifindex_add(
1768 nexthop
= route_entry_nexthop_ipv6_add(
1769 re
, &nexthops
[i
], re
->vrf_id
);
1771 if (CHECK_FLAG(message
, ZAPI_MESSAGE_LABEL
))
1772 nexthop_add_labels(nexthop
, label_type
,
1775 if ((i
< if_count
) && ifindices
[i
])
1776 route_entry_nexthop_ifindex_add(
1777 re
, ifindices
[i
], re
->vrf_id
);
1783 if (CHECK_FLAG(message
, ZAPI_MESSAGE_DISTANCE
))
1784 STREAM_GETC(s
, re
->distance
);
1787 if (CHECK_FLAG(message
, ZAPI_MESSAGE_METRIC
))
1788 STREAM_GETL(s
, re
->metric
);
1791 if (CHECK_FLAG(message
, ZAPI_MESSAGE_TAG
))
1792 STREAM_GETL(s
, re
->tag
);
1796 if (CHECK_FLAG(message
, ZAPI_MESSAGE_MTU
))
1797 STREAM_GETL(s
, re
->mtu
);
1802 re
->table
= zvrf
->table_id
;
1804 ret
= rib_add_multipath(AFI_IP6
, safi
, &p
, NULL
, re
);
1807 client
->v4_route_add_cnt
++;
1809 client
->v4_route_upd8_cnt
++;
1814 nexthops_free(re
->ng
.nexthop
);
1815 XFREE(MTYPE_RE
, re
);
1819 static int zread_ipv6_add(struct zserv
*client
, u_short length
,
1820 struct zebra_vrf
*zvrf
)
1824 struct in6_addr nhop_addr
;
1826 struct route_entry
*re
;
1829 u_char nexthop_type
;
1831 struct prefix_ipv6 src_p
, *src_pp
;
1833 static struct in6_addr nexthops
[MULTIPATH_NUM
];
1834 static unsigned int ifindices
[MULTIPATH_NUM
];
1836 static mpls_label_t labels
[MULTIPATH_NUM
];
1837 enum lsp_types_t label_type
= ZEBRA_LSP_NONE
;
1839 struct nexthop
*nexthop
;
1840 enum blackhole_type bh_type
= BLACKHOLE_NULL
;
1842 /* Get input stream. */
1845 memset(&nhop_addr
, 0, sizeof(struct in6_addr
));
1847 /* Allocate new re. */
1848 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
1850 /* Type, flags, message. */
1851 STREAM_GETC(s
, re
->type
);
1852 if (re
->type
> ZEBRA_ROUTE_MAX
) {
1853 zlog_warn("%s: Specified route type: %d is not a legal value\n",
1854 __PRETTY_FUNCTION__
, re
->type
);
1855 XFREE(MTYPE_RE
, re
);
1858 STREAM_GETW(s
, re
->instance
);
1859 STREAM_GETL(s
, re
->flags
);
1860 STREAM_GETC(s
, message
);
1861 STREAM_GETW(s
, safi
);
1862 re
->uptime
= time(NULL
);
1865 memset(&p
, 0, sizeof(p
));
1866 p
.family
= AF_INET6
;
1867 STREAM_GETC(s
, p
.prefixlen
);
1868 if (p
.prefixlen
> IPV6_MAX_BITLEN
) {
1870 "%s: Specified prefix length %d is to large for v6 prefix",
1871 __PRETTY_FUNCTION__
, p
.prefixlen
);
1872 XFREE(MTYPE_RE
, re
);
1875 STREAM_GET(&p
.u
.prefix6
, s
, PSIZE(p
.prefixlen
));
1877 if (CHECK_FLAG(message
, ZAPI_MESSAGE_SRCPFX
)) {
1878 memset(&src_p
, 0, sizeof(src_p
));
1879 src_p
.family
= AF_INET6
;
1880 STREAM_GETC(s
, src_p
.prefixlen
);
1881 if (src_p
.prefixlen
> IPV6_MAX_BITLEN
) {
1883 "%s: Specified src prefix length %d is to large for v6 prefix",
1884 __PRETTY_FUNCTION__
, src_p
.prefixlen
);
1885 XFREE(MTYPE_RE
, re
);
1888 STREAM_GET(&src_p
.prefix
, s
, PSIZE(src_p
.prefixlen
));
1894 re
->vrf_id
= zvrf_id(zvrf
);
1896 /* We need to give nh-addr, nh-ifindex with the same next-hop object
1897 * to the re to ensure that IPv6 multipathing works; need to coalesce
1898 * these. Clients should send the same number of paired set of
1899 * next-hop-addr/next-hop-ifindices. */
1900 if (CHECK_FLAG(message
, ZAPI_MESSAGE_NEXTHOP
)) {
1901 unsigned int nh_count
= 0;
1902 unsigned int if_count
= 0;
1903 unsigned int max_nh_if
= 0;
1905 STREAM_GETC(s
, nexthop_num
);
1906 zserv_nexthop_num_warn(__func__
, (const struct prefix
*)&p
,
1909 if (CHECK_FLAG(message
, ZAPI_MESSAGE_LABEL
))
1910 label_type
= lsp_type_from_re_type(client
->proto
);
1912 for (i
= 0; i
< nexthop_num
; i
++) {
1913 STREAM_GETC(s
, nexthop_type
);
1915 switch (nexthop_type
) {
1916 case NEXTHOP_TYPE_IPV6
:
1917 STREAM_GET(&nhop_addr
, s
, 16);
1918 if (nh_count
< MULTIPATH_NUM
) {
1919 /* For labeled-unicast, each nexthop is
1920 * followed by label. */
1921 if (CHECK_FLAG(message
,
1922 ZAPI_MESSAGE_LABEL
)) {
1923 STREAM_GETL(s
, label
);
1924 labels
[nh_count
] = label
;
1926 nexthops
[nh_count
++] = nhop_addr
;
1929 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1930 STREAM_GET(&nhop_addr
, s
, 16);
1931 STREAM_GETL(s
, ifindex
);
1932 route_entry_nexthop_ipv6_ifindex_add(
1933 re
, &nhop_addr
, ifindex
, re
->vrf_id
);
1935 case NEXTHOP_TYPE_IFINDEX
:
1936 if (if_count
< multipath_num
) {
1937 STREAM_GETL(s
, ifindices
[if_count
++]);
1940 case NEXTHOP_TYPE_BLACKHOLE
:
1941 route_entry_nexthop_blackhole_add(re
, bh_type
);
1945 "%s: Please use ZEBRA_ROUTE_ADD if you want to pass non v6 nexthops",
1946 __PRETTY_FUNCTION__
);
1947 nexthops_free(re
->ng
.nexthop
);
1948 XFREE(MTYPE_RE
, re
);
1953 max_nh_if
= (nh_count
> if_count
) ? nh_count
: if_count
;
1954 for (i
= 0; i
< max_nh_if
; i
++) {
1956 && !IN6_IS_ADDR_UNSPECIFIED(&nexthops
[i
])) {
1957 if ((i
< if_count
) && ifindices
[i
])
1959 route_entry_nexthop_ipv6_ifindex_add(
1964 nexthop
= route_entry_nexthop_ipv6_add(
1965 re
, &nexthops
[i
], re
->vrf_id
);
1966 if (CHECK_FLAG(message
, ZAPI_MESSAGE_LABEL
))
1967 nexthop_add_labels(nexthop
, label_type
,
1970 if ((i
< if_count
) && ifindices
[i
])
1971 route_entry_nexthop_ifindex_add(
1972 re
, ifindices
[i
], re
->vrf_id
);
1978 if (CHECK_FLAG(message
, ZAPI_MESSAGE_DISTANCE
))
1979 STREAM_GETC(s
, re
->distance
);
1982 if (CHECK_FLAG(message
, ZAPI_MESSAGE_METRIC
))
1983 STREAM_GETL(s
, re
->metric
);
1986 if (CHECK_FLAG(message
, ZAPI_MESSAGE_TAG
))
1987 STREAM_GETL(s
, re
->tag
);
1991 if (CHECK_FLAG(message
, ZAPI_MESSAGE_MTU
))
1992 STREAM_GETL(s
, re
->mtu
);
1996 re
->table
= zvrf
->table_id
;
1998 ret
= rib_add_multipath(AFI_IP6
, safi
, &p
, src_pp
, re
);
2001 client
->v6_route_add_cnt
++;
2003 client
->v6_route_upd8_cnt
++;
2008 nexthops_free(re
->ng
.nexthop
);
2009 XFREE(MTYPE_RE
, re
);
2014 /* Zebra server IPv6 prefix delete function. */
2015 static int zread_ipv6_delete(struct zserv
*client
, u_short length
,
2016 struct zebra_vrf
*zvrf
)
2019 struct zapi_ipv6 api
;
2021 struct prefix_ipv6 src_p
, *src_pp
;
2025 /* Type, flags, message. */
2026 STREAM_GETC(s
, api
.type
);
2027 STREAM_GETW(s
, api
.instance
);
2028 STREAM_GETL(s
, api
.flags
);
2029 STREAM_GETC(s
, api
.message
);
2030 STREAM_GETW(s
, api
.safi
);
2033 memset(&p
, 0, sizeof(struct prefix
));
2034 p
.family
= AF_INET6
;
2035 STREAM_GETC(s
, p
.prefixlen
);
2036 STREAM_GET(&p
.u
.prefix6
, s
, PSIZE(p
.prefixlen
));
2038 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
)) {
2039 memset(&src_p
, 0, sizeof(struct prefix_ipv6
));
2040 src_p
.family
= AF_INET6
;
2041 STREAM_GETC(s
, src_p
.prefixlen
);
2042 STREAM_GET(&src_p
.prefix
, s
, PSIZE(src_p
.prefixlen
));
2047 rib_delete(AFI_IP6
, api
.safi
, zvrf_id(zvrf
), api
.type
, api
.instance
,
2048 api
.flags
, &p
, src_pp
, NULL
, client
->rtm_table
, 0, false,
2051 client
->v6_route_del_cnt
++;
2057 /* Register zebra server router-id information. Send current router-id */
2058 static int zread_router_id_add(struct zserv
*client
, u_short length
,
2059 struct zebra_vrf
*zvrf
)
2063 /* Router-id information is needed. */
2064 vrf_bitmap_set(client
->ridinfo
, zvrf_id(zvrf
));
2066 router_id_get(&p
, zvrf_id(zvrf
));
2068 return zsend_router_id_update(client
, &p
, zvrf_id(zvrf
));
2071 /* Unregister zebra server router-id information. */
2072 static int zread_router_id_delete(struct zserv
*client
, u_short length
,
2073 struct zebra_vrf
*zvrf
)
2075 vrf_bitmap_unset(client
->ridinfo
, zvrf_id(zvrf
));
2079 /* Tie up route-type and client->sock */
2080 static void zread_hello(struct zserv
*client
)
2082 /* type of protocol (lib/zebra.h) */
2087 STREAM_GETC(client
->ibuf
, proto
);
2088 STREAM_GETW(client
->ibuf
, instance
);
2089 STREAM_GETC(client
->ibuf
, notify
);
2091 client
->notify_owner
= true;
2093 /* accept only dynamic routing protocols */
2094 if ((proto
< ZEBRA_ROUTE_MAX
) && (proto
> ZEBRA_ROUTE_STATIC
)) {
2096 "client %d says hello and bids fair to announce only %s routes",
2097 client
->sock
, zebra_route_string(proto
));
2099 zlog_notice("client protocol instance %d", instance
);
2101 client
->proto
= proto
;
2102 client
->instance
= instance
;
2109 /* Unregister all information in a VRF. */
2110 static int zread_vrf_unregister(struct zserv
*client
, u_short length
,
2111 struct zebra_vrf
*zvrf
)
2116 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
2117 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
2118 vrf_bitmap_unset(client
->redist
[afi
][i
], zvrf_id(zvrf
));
2119 vrf_bitmap_unset(client
->redist_default
, zvrf_id(zvrf
));
2120 vrf_bitmap_unset(client
->ifinfo
, zvrf_id(zvrf
));
2121 vrf_bitmap_unset(client
->ridinfo
, zvrf_id(zvrf
));
2126 static void zread_mpls_labels(int command
, struct zserv
*client
, u_short length
,
2127 struct zebra_vrf
*zvrf
)
2130 enum lsp_types_t type
;
2131 struct prefix prefix
;
2132 enum nexthop_types_t gtype
;
2135 mpls_label_t in_label
, out_label
;
2138 /* Get input stream. */
2142 STREAM_GETC(s
, type
);
2143 STREAM_GETL(s
, prefix
.family
);
2144 switch (prefix
.family
) {
2146 STREAM_GET(&prefix
.u
.prefix4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
2147 STREAM_GETC(s
, prefix
.prefixlen
);
2148 if (prefix
.prefixlen
> IPV4_MAX_BITLEN
) {
2150 "%s: Specified prefix length %d is greater than a v4 address can support",
2151 __PRETTY_FUNCTION__
, prefix
.prefixlen
);
2154 STREAM_GET(&gate
.ipv4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
2157 STREAM_GET(&prefix
.u
.prefix6
, s
, 16);
2158 STREAM_GETC(s
, prefix
.prefixlen
);
2159 if (prefix
.prefixlen
> IPV6_MAX_BITLEN
) {
2161 "%s: Specified prefix length %d is greater than a v6 address can support",
2162 __PRETTY_FUNCTION__
, prefix
.prefixlen
);
2165 STREAM_GET(&gate
.ipv6
, s
, 16);
2168 zlog_warn("%s: Specified AF %d is not supported for this call",
2169 __PRETTY_FUNCTION__
, prefix
.family
);
2172 STREAM_GETL(s
, ifindex
);
2173 STREAM_GETC(s
, distance
);
2174 STREAM_GETL(s
, in_label
);
2175 STREAM_GETL(s
, out_label
);
2177 switch (prefix
.family
) {
2180 gtype
= NEXTHOP_TYPE_IPV4_IFINDEX
;
2182 gtype
= NEXTHOP_TYPE_IPV4
;
2186 gtype
= NEXTHOP_TYPE_IPV6_IFINDEX
;
2188 gtype
= NEXTHOP_TYPE_IPV6
;
2197 if (command
== ZEBRA_MPLS_LABELS_ADD
) {
2198 mpls_lsp_install(zvrf
, type
, in_label
, out_label
, gtype
, &gate
,
2200 mpls_ftn_update(1, zvrf
, type
, &prefix
, gtype
, &gate
, ifindex
,
2201 distance
, out_label
);
2202 } else if (command
== ZEBRA_MPLS_LABELS_DELETE
) {
2203 mpls_lsp_uninstall(zvrf
, type
, in_label
, gtype
, &gate
, ifindex
);
2204 mpls_ftn_update(0, zvrf
, type
, &prefix
, gtype
, &gate
, ifindex
,
2205 distance
, out_label
);
2210 /* Send response to a label manager connect request to client */
2211 static int zsend_label_manager_connect_response(struct zserv
*client
,
2212 vrf_id_t vrf_id
, u_short result
)
2219 zclient_create_header(s
, ZEBRA_LABEL_MANAGER_CONNECT
, vrf_id
);
2222 stream_putc(s
, result
);
2224 /* Write packet size. */
2225 stream_putw_at(s
, 0, stream_get_endp(s
));
2227 return writen(client
->sock
, s
->data
, stream_get_endp(s
));
2230 static void zread_label_manager_connect(struct zserv
*client
, vrf_id_t vrf_id
)
2233 /* type of protocol (lib/zebra.h) */
2237 /* Get input stream. */
2241 STREAM_GETC(s
, proto
);
2242 STREAM_GETW(s
, instance
);
2244 /* accept only dynamic routing protocols */
2245 if ((proto
>= ZEBRA_ROUTE_MAX
) || (proto
<= ZEBRA_ROUTE_STATIC
)) {
2246 zlog_err("client %d has wrong protocol %s", client
->sock
,
2247 zebra_route_string(proto
));
2248 zsend_label_manager_connect_response(client
, vrf_id
, 1);
2251 zlog_notice("client %d with instance %u connected as %s", client
->sock
,
2252 instance
, zebra_route_string(proto
));
2253 client
->proto
= proto
;
2254 client
->instance
= instance
;
2257 Release previous labels of same protocol and instance.
2258 This is done in case it restarted from an unexpected shutdown.
2260 release_daemon_chunks(proto
, instance
);
2263 " Label Manager client connected: sock %d, proto %s, instance %u",
2264 client
->sock
, zebra_route_string(proto
), instance
);
2265 /* send response back */
2266 zsend_label_manager_connect_response(client
, vrf_id
, 0);
2271 /* Send response to a get label chunk request to client */
2272 static int zsend_assign_label_chunk_response(struct zserv
*client
,
2274 struct label_manager_chunk
*lmc
)
2281 zclient_create_header(s
, ZEBRA_GET_LABEL_CHUNK
, vrf_id
);
2285 stream_putc(s
, lmc
->keep
);
2286 /* start and end labels */
2287 stream_putl(s
, lmc
->start
);
2288 stream_putl(s
, lmc
->end
);
2291 /* Write packet size. */
2292 stream_putw_at(s
, 0, stream_get_endp(s
));
2294 return writen(client
->sock
, s
->data
, stream_get_endp(s
));
2297 static void zread_get_label_chunk(struct zserv
*client
, vrf_id_t vrf_id
)
2302 struct label_manager_chunk
*lmc
;
2304 /* Get input stream. */
2308 STREAM_GETC(s
, keep
);
2309 STREAM_GETL(s
, size
);
2311 lmc
= assign_label_chunk(client
->proto
, client
->instance
, keep
, size
);
2313 zlog_err("%s: Unable to assign Label Chunk of size %u",
2316 zlog_debug("Assigned Label Chunk %u - %u to %u", lmc
->start
,
2318 /* send response back */
2319 zsend_assign_label_chunk_response(client
, vrf_id
, lmc
);
2325 static void zread_release_label_chunk(struct zserv
*client
)
2328 uint32_t start
, end
;
2330 /* Get input stream. */
2334 STREAM_GETL(s
, start
);
2335 STREAM_GETL(s
, end
);
2337 release_label_chunk(client
->proto
, client
->instance
, start
, end
);
2342 static void zread_label_manager_request(int cmd
, struct zserv
*client
,
2343 struct zebra_vrf
*zvrf
)
2345 /* to avoid sending other messages like ZERBA_INTERFACE_UP */
2346 if (cmd
== ZEBRA_LABEL_MANAGER_CONNECT
)
2347 client
->is_synchronous
= 1;
2349 /* external label manager */
2351 zread_relay_label_manager_request(cmd
, client
, zvrf_id(zvrf
));
2352 /* this is a label manager */
2354 if (cmd
== ZEBRA_LABEL_MANAGER_CONNECT
)
2355 zread_label_manager_connect(client
, zvrf_id(zvrf
));
2357 /* Sanity: don't allow 'unidentified' requests */
2358 if (!client
->proto
) {
2360 "Got label request from an unidentified client");
2363 if (cmd
== ZEBRA_GET_LABEL_CHUNK
)
2364 zread_get_label_chunk(client
, zvrf_id(zvrf
));
2365 else if (cmd
== ZEBRA_RELEASE_LABEL_CHUNK
)
2366 zread_release_label_chunk(client
);
2371 static int zread_pseudowire(int command
, struct zserv
*client
, u_short length
,
2372 struct zebra_vrf
*zvrf
)
2375 char ifname
[IF_NAMESIZE
];
2379 union g_addr nexthop
;
2380 uint32_t local_label
;
2381 uint32_t remote_label
;
2383 union pw_protocol_fields data
;
2385 struct zebra_pw
*pw
;
2387 /* Get input stream. */
2391 STREAM_GET(ifname
, s
, IF_NAMESIZE
);
2392 STREAM_GETL(s
, ifindex
);
2393 STREAM_GETL(s
, type
);
2397 STREAM_GET(&nexthop
.ipv4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
2400 STREAM_GET(&nexthop
.ipv6
, s
, 16);
2405 STREAM_GETL(s
, local_label
);
2406 STREAM_GETL(s
, remote_label
);
2407 STREAM_GETC(s
, flags
);
2408 STREAM_GET(&data
, s
, sizeof(data
));
2409 protocol
= client
->proto
;
2411 pw
= zebra_pw_find(zvrf
, ifname
);
2415 zlog_warn("%s: pseudowire %s already exists [%s]",
2417 zserv_command_string(command
));
2421 zebra_pw_add(zvrf
, ifname
, protocol
, client
);
2423 case ZEBRA_PW_DELETE
:
2425 zlog_warn("%s: pseudowire %s not found [%s]", __func__
,
2426 ifname
, zserv_command_string(command
));
2430 zebra_pw_del(zvrf
, pw
);
2433 case ZEBRA_PW_UNSET
:
2435 zlog_warn("%s: pseudowire %s not found [%s]", __func__
,
2436 ifname
, zserv_command_string(command
));
2444 case ZEBRA_PW_UNSET
:
2449 zebra_pw_change(pw
, ifindex
, type
, af
, &nexthop
, local_label
,
2450 remote_label
, flags
, &data
);
2458 /* Cleanup registered nexthops (across VRFs) upon client disconnect. */
2459 static void zebra_client_close_cleanup_rnh(struct zserv
*client
)
2462 struct zebra_vrf
*zvrf
;
2464 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
2465 if ((zvrf
= vrf
->info
) != NULL
) {
2466 zebra_cleanup_rnh_client(zvrf_id(zvrf
), AF_INET
, client
,
2468 zebra_cleanup_rnh_client(zvrf_id(zvrf
), AF_INET6
,
2469 client
, RNH_NEXTHOP_TYPE
);
2470 zebra_cleanup_rnh_client(zvrf_id(zvrf
), AF_INET
, client
,
2471 RNH_IMPORT_CHECK_TYPE
);
2472 zebra_cleanup_rnh_client(zvrf_id(zvrf
), AF_INET6
,
2473 client
, RNH_IMPORT_CHECK_TYPE
);
2474 if (client
->proto
== ZEBRA_ROUTE_LDP
) {
2475 hash_iterate(zvrf
->lsp_table
,
2476 mpls_ldp_lsp_uninstall_all
,
2478 mpls_ldp_ftn_uninstall_all(zvrf
, AFI_IP
);
2479 mpls_ldp_ftn_uninstall_all(zvrf
, AFI_IP6
);
2485 /* free zebra client information. */
2486 static void zebra_client_free(struct zserv
*client
)
2488 /* Send client de-registration to BFD */
2489 zebra_ptm_bfd_client_deregister(client
->proto
);
2491 /* Cleanup any registered nexthops - across all VRFs. */
2492 zebra_client_close_cleanup_rnh(client
);
2494 /* Release Label Manager chunks */
2495 release_daemon_chunks(client
->proto
, client
->instance
);
2497 /* Cleanup any FECs registered by this client. */
2498 zebra_mpls_cleanup_fecs_for_client(vrf_info_lookup(VRF_DEFAULT
),
2501 /* Remove pseudowires associated with this client */
2502 zebra_pw_client_close(client
);
2504 /* Close file descriptor. */
2506 unsigned long nroutes
;
2508 close(client
->sock
);
2509 nroutes
= rib_score_proto(client
->proto
, client
->instance
);
2511 "client %d disconnected. %lu %s routes removed from the rib",
2512 client
->sock
, nroutes
,
2513 zebra_route_string(client
->proto
));
2517 /* Free stream buffers. */
2519 stream_free(client
->ibuf
);
2521 stream_free(client
->obuf
);
2523 buffer_free(client
->wb
);
2525 /* Release threads. */
2527 thread_cancel(client
->t_read
);
2528 if (client
->t_write
)
2529 thread_cancel(client
->t_write
);
2530 if (client
->t_suicide
)
2531 thread_cancel(client
->t_suicide
);
2534 for (afi_t afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
2535 for (int i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
2536 vrf_bitmap_free(client
->redist
[afi
][i
]);
2538 vrf_bitmap_free(client
->redist_default
);
2539 vrf_bitmap_free(client
->ifinfo
);
2540 vrf_bitmap_free(client
->ridinfo
);
2542 XFREE(MTYPE_TMP
, client
);
2545 static void zebra_client_close(struct zserv
*client
)
2547 listnode_delete(zebrad
.client_list
, client
);
2548 zebra_client_free(client
);
2551 /* Make new client. */
2552 static void zebra_client_create(int sock
)
2554 struct zserv
*client
;
2558 client
= XCALLOC(MTYPE_TMP
, sizeof(struct zserv
));
2560 /* Make client input/output buffer. */
2561 client
->sock
= sock
;
2562 client
->ibuf
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
2563 client
->obuf
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
2564 client
->wb
= buffer_new(0);
2566 /* Set table number. */
2567 client
->rtm_table
= zebrad
.rtm_table_default
;
2569 client
->connect_time
= monotime(NULL
);
2570 /* Initialize flags */
2571 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
2572 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
2573 client
->redist
[afi
][i
] = vrf_bitmap_init();
2574 client
->redist_default
= vrf_bitmap_init();
2575 client
->ifinfo
= vrf_bitmap_init();
2576 client
->ridinfo
= vrf_bitmap_init();
2578 /* by default, it's not a synchronous client */
2579 client
->is_synchronous
= 0;
2581 /* Add this client to linked list. */
2582 listnode_add(zebrad
.client_list
, client
);
2584 /* Make new read thread. */
2585 zebra_event(ZEBRA_READ
, sock
, client
);
2587 zebra_vrf_update_all(client
);
2590 static int zread_interface_set_master(struct zserv
*client
, u_short length
)
2592 struct interface
*master
;
2593 struct interface
*slave
;
2594 struct stream
*s
= client
->ibuf
;
2598 STREAM_GETL(s
, vrf_id
);
2599 STREAM_GETL(s
, ifindex
);
2600 master
= if_lookup_by_index(ifindex
, vrf_id
);
2602 STREAM_GETL(s
, vrf_id
);
2603 STREAM_GETL(s
, ifindex
);
2604 slave
= if_lookup_by_index(ifindex
, vrf_id
);
2606 if (!master
|| !slave
)
2609 kernel_interface_set_master(master
, slave
);
2616 static void zread_vrf_label(struct zserv
*client
, struct zebra_vrf
*zvrf
)
2618 struct interface
*ifp
;
2619 mpls_label_t nlabel
;
2622 struct zebra_vrf
*def_zvrf
;
2623 enum lsp_types_t ltype
;
2626 STREAM_GETL(s
, nlabel
);
2627 STREAM_GETC(s
, afi
);
2628 if (nlabel
== zvrf
->label
[afi
]) {
2630 * Nothing to do here move along
2635 STREAM_GETC(s
, ltype
);
2637 if (zvrf
->vrf
->vrf_id
!= VRF_DEFAULT
)
2638 ifp
= if_lookup_by_name(zvrf
->vrf
->name
, zvrf
->vrf
->vrf_id
);
2640 ifp
= if_lookup_by_name("lo", VRF_DEFAULT
);
2643 zlog_debug("Unable to find specified Interface for %s",
2648 def_zvrf
= zebra_vrf_lookup_by_id(VRF_DEFAULT
);
2650 if (zvrf
->label
[afi
] != MPLS_LABEL_NONE
) {
2654 really_remove
= true;
2655 for (scrubber
= AFI_IP
; scrubber
< AFI_MAX
; scrubber
++) {
2656 if (scrubber
== afi
)
2659 if (zvrf
->label
[scrubber
] == MPLS_LABEL_NONE
)
2662 if (zvrf
->label
[afi
] == zvrf
->label
[scrubber
]) {
2663 really_remove
= false;
2669 mpls_lsp_uninstall(def_zvrf
, ltype
, zvrf
->label
[afi
],
2670 NEXTHOP_TYPE_IFINDEX
, NULL
,
2674 if (nlabel
!= MPLS_LABEL_NONE
)
2675 mpls_lsp_install(def_zvrf
, ltype
, nlabel
,
2676 MPLS_LABEL_IMPLICIT_NULL
, NEXTHOP_TYPE_IFINDEX
,
2677 NULL
, ifp
->ifindex
);
2679 zvrf
->label
[afi
] = nlabel
;
2684 static inline void zread_rule(uint16_t command
, struct zserv
*client
,
2685 uint16_t length
, struct zebra_vrf
*zvrf
)
2687 struct zebra_pbr_rule zpr
;
2693 STREAM_GETL(s
, total
);
2695 for (i
= 0; i
< total
; i
++) {
2696 memset(&zpr
, 0, sizeof(zpr
));
2698 zpr
.sock
= client
->sock
;
2699 STREAM_GETL(s
, zpr
.seq
);
2700 STREAM_GETL(s
, zpr
.priority
);
2701 STREAM_GETL(s
, zpr
.unique
);
2702 STREAM_GETC(s
, zpr
.filter
.src_ip
.family
);
2703 STREAM_GETC(s
, zpr
.filter
.src_ip
.prefixlen
);
2704 STREAM_GET(&zpr
.filter
.src_ip
.u
.prefix
, s
,
2705 prefix_blen(&zpr
.filter
.src_ip
));
2706 STREAM_GETW(s
, zpr
.filter
.src_port
);
2707 STREAM_GETC(s
, zpr
.filter
.dst_ip
.family
);
2708 STREAM_GETC(s
, zpr
.filter
.dst_ip
.prefixlen
);
2709 STREAM_GET(&zpr
.filter
.dst_ip
.u
.prefix
, s
,
2710 prefix_blen(&zpr
.filter
.dst_ip
));
2711 STREAM_GETW(s
, zpr
.filter
.dst_port
);
2712 STREAM_GETL(s
, zpr
.action
.table
);
2713 STREAM_GETL(s
, ifindex
);
2715 zpr
.ifp
= if_lookup_by_index(ifindex
, VRF_UNKNOWN
);
2717 zlog_debug("FAiled to lookup ifindex: %u", ifindex
);
2721 if (!is_default_prefix(&zpr
.filter
.src_ip
))
2722 zpr
.filter
.filter_bm
|= PBR_FILTER_SRC_IP
;
2724 if (!is_default_prefix(&zpr
.filter
.dst_ip
))
2725 zpr
.filter
.filter_bm
|= PBR_FILTER_DST_IP
;
2727 if (zpr
.filter
.src_port
)
2728 zpr
.filter
.filter_bm
|= PBR_FILTER_SRC_PORT
;
2730 if (zpr
.filter
.dst_port
)
2731 zpr
.filter
.filter_bm
|= PBR_FILTER_DST_PORT
;
2733 if (command
== ZEBRA_RULE_ADD
)
2734 zebra_pbr_add_rule(zvrf
->zns
, &zpr
);
2736 zebra_pbr_del_rule(zvrf
->zns
, &zpr
);
2743 static inline void zserv_handle_commands(struct zserv
*client
, uint16_t command
,
2745 struct zebra_vrf
*zvrf
)
2748 case ZEBRA_ROUTER_ID_ADD
:
2749 zread_router_id_add(client
, length
, zvrf
);
2751 case ZEBRA_ROUTER_ID_DELETE
:
2752 zread_router_id_delete(client
, length
, zvrf
);
2754 case ZEBRA_INTERFACE_ADD
:
2755 zread_interface_add(client
, length
, zvrf
);
2757 case ZEBRA_INTERFACE_DELETE
:
2758 zread_interface_delete(client
, length
, zvrf
);
2760 case ZEBRA_ROUTE_ADD
:
2761 zread_route_add(client
, length
, zvrf
);
2763 case ZEBRA_ROUTE_DELETE
:
2764 zread_route_del(client
, length
, zvrf
);
2766 case ZEBRA_IPV4_ROUTE_ADD
:
2767 zread_ipv4_add(client
, length
, zvrf
);
2769 case ZEBRA_IPV4_ROUTE_DELETE
:
2770 zread_ipv4_delete(client
, length
, zvrf
);
2772 case ZEBRA_IPV4_ROUTE_IPV6_NEXTHOP_ADD
:
2773 zread_ipv4_route_ipv6_nexthop_add(client
, length
, zvrf
);
2775 case ZEBRA_IPV6_ROUTE_ADD
:
2776 zread_ipv6_add(client
, length
, zvrf
);
2778 case ZEBRA_IPV6_ROUTE_DELETE
:
2779 zread_ipv6_delete(client
, length
, zvrf
);
2781 case ZEBRA_REDISTRIBUTE_ADD
:
2782 zebra_redistribute_add(command
, client
, length
, zvrf
);
2784 case ZEBRA_REDISTRIBUTE_DELETE
:
2785 zebra_redistribute_delete(command
, client
, length
, zvrf
);
2787 case ZEBRA_REDISTRIBUTE_DEFAULT_ADD
:
2788 zebra_redistribute_default_add(command
, client
, length
, zvrf
);
2790 case ZEBRA_REDISTRIBUTE_DEFAULT_DELETE
:
2791 zebra_redistribute_default_delete(command
, client
, length
,
2794 case ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB
:
2795 zread_ipv4_nexthop_lookup_mrib(client
, length
, zvrf
);
2798 zread_hello(client
);
2800 case ZEBRA_NEXTHOP_REGISTER
:
2801 zserv_rnh_register(client
, length
, RNH_NEXTHOP_TYPE
, zvrf
);
2803 case ZEBRA_NEXTHOP_UNREGISTER
:
2804 zserv_rnh_unregister(client
, length
, RNH_NEXTHOP_TYPE
, zvrf
);
2806 case ZEBRA_IMPORT_ROUTE_REGISTER
:
2807 zserv_rnh_register(client
, length
, RNH_IMPORT_CHECK_TYPE
, zvrf
);
2809 case ZEBRA_IMPORT_ROUTE_UNREGISTER
:
2810 zserv_rnh_unregister(client
, length
, RNH_IMPORT_CHECK_TYPE
,
2813 case ZEBRA_BFD_DEST_UPDATE
:
2814 case ZEBRA_BFD_DEST_REGISTER
:
2815 zebra_ptm_bfd_dst_register(client
, length
, command
, zvrf
);
2817 case ZEBRA_BFD_DEST_DEREGISTER
:
2818 zebra_ptm_bfd_dst_deregister(client
, length
, zvrf
);
2820 case ZEBRA_VRF_UNREGISTER
:
2821 zread_vrf_unregister(client
, length
, zvrf
);
2823 case ZEBRA_VRF_LABEL
:
2824 zread_vrf_label(client
, zvrf
);
2826 case ZEBRA_BFD_CLIENT_REGISTER
:
2827 zebra_ptm_bfd_client_register(client
, length
);
2829 case ZEBRA_INTERFACE_ENABLE_RADV
:
2830 #if defined(HAVE_RTADV)
2831 zebra_interface_radv_set(client
, length
, zvrf
, 1);
2834 case ZEBRA_INTERFACE_DISABLE_RADV
:
2835 #if defined(HAVE_RTADV)
2836 zebra_interface_radv_set(client
, length
, zvrf
, 0);
2839 case ZEBRA_MPLS_LABELS_ADD
:
2840 case ZEBRA_MPLS_LABELS_DELETE
:
2841 zread_mpls_labels(command
, client
, length
, zvrf
);
2843 case ZEBRA_IPMR_ROUTE_STATS
:
2844 zebra_ipmr_route_stats(client
, length
, zvrf
);
2846 case ZEBRA_LABEL_MANAGER_CONNECT
:
2847 case ZEBRA_GET_LABEL_CHUNK
:
2848 case ZEBRA_RELEASE_LABEL_CHUNK
:
2849 zread_label_manager_request(command
, client
, zvrf
);
2851 case ZEBRA_FEC_REGISTER
:
2852 zserv_fec_register(client
, length
);
2854 case ZEBRA_FEC_UNREGISTER
:
2855 zserv_fec_unregister(client
, length
);
2857 case ZEBRA_ADVERTISE_DEFAULT_GW
:
2858 zebra_vxlan_advertise_gw_macip(client
, length
, zvrf
);
2860 case ZEBRA_ADVERTISE_SUBNET
:
2861 zebra_vxlan_advertise_subnet(client
, length
, zvrf
);
2863 case ZEBRA_ADVERTISE_ALL_VNI
:
2864 zebra_vxlan_advertise_all_vni(client
, length
, zvrf
);
2866 case ZEBRA_REMOTE_VTEP_ADD
:
2867 zebra_vxlan_remote_vtep_add(client
, length
, zvrf
);
2869 case ZEBRA_REMOTE_VTEP_DEL
:
2870 zebra_vxlan_remote_vtep_del(client
, length
, zvrf
);
2872 case ZEBRA_REMOTE_MACIP_ADD
:
2873 zebra_vxlan_remote_macip_add(client
, length
, zvrf
);
2875 case ZEBRA_REMOTE_MACIP_DEL
:
2876 zebra_vxlan_remote_macip_del(client
, length
, zvrf
);
2878 case ZEBRA_INTERFACE_SET_MASTER
:
2879 zread_interface_set_master(client
, length
);
2882 case ZEBRA_PW_DELETE
:
2884 case ZEBRA_PW_UNSET
:
2885 zread_pseudowire(command
, client
, length
, zvrf
);
2887 case ZEBRA_RULE_ADD
:
2888 case ZEBRA_RULE_DELETE
:
2889 zread_rule(command
, client
, length
, zvrf
);
2892 zlog_info("Zebra received unknown command %d", command
);
2897 #if defined(HANDLE_ZAPI_FUZZING)
2898 static void zserv_write_incoming(struct stream
*orig
, uint16_t command
)
2900 char fname
[MAXPATHLEN
];
2901 struct stream
*copy
;
2904 copy
= stream_dup(orig
);
2905 stream_set_getp(copy
, 0);
2907 zserv_privs
.change(ZPRIVS_RAISE
);
2908 snprintf(fname
, MAXPATHLEN
, "%s/%u", DAEMON_VTY_DIR
, command
);
2909 fd
= open(fname
, O_CREAT
| O_WRONLY
| O_EXCL
, 0644);
2910 stream_flush(copy
, fd
);
2912 zserv_privs
.change(ZPRIVS_LOWER
);
2917 /* Handler of zebra service request. */
2918 static int zebra_client_read(struct thread
*thread
)
2921 struct zserv
*client
;
2923 uint16_t length
, command
;
2924 uint8_t marker
, version
;
2926 struct zebra_vrf
*zvrf
;
2927 #if defined(HANDLE_ZAPI_FUZZING)
2930 int packets
= zebrad
.packets_to_process
;
2933 /* Get thread data. Reset reading thread because I'm running. */
2934 sock
= THREAD_FD(thread
);
2935 client
= THREAD_ARG(thread
);
2936 client
->t_read
= NULL
;
2938 if (client
->t_suicide
) {
2939 zebra_client_close(client
);
2944 /* Read length and command (if we don't have it already). */
2945 if ((already
= stream_get_endp(client
->ibuf
))
2946 < ZEBRA_HEADER_SIZE
) {
2948 if (((nbyte
= stream_read_try(client
->ibuf
, sock
,
2953 if (IS_ZEBRA_DEBUG_EVENT
)
2955 "connection closed socket [%d]",
2957 zebra_client_close(client
);
2960 if (nbyte
!= (ssize_t
)(ZEBRA_HEADER_SIZE
- already
)) {
2961 /* Try again later. */
2962 zebra_event(ZEBRA_READ
, sock
, client
);
2965 already
= ZEBRA_HEADER_SIZE
;
2968 /* Reset to read from the beginning of the incoming packet. */
2969 stream_set_getp(client
->ibuf
, 0);
2971 /* Fetch header values */
2972 STREAM_GETW(client
->ibuf
, length
);
2973 STREAM_GETC(client
->ibuf
, marker
);
2974 STREAM_GETC(client
->ibuf
, version
);
2975 STREAM_GETL(client
->ibuf
, vrf_id
);
2976 STREAM_GETW(client
->ibuf
, command
);
2978 if (marker
!= ZEBRA_HEADER_MARKER
|| version
!= ZSERV_VERSION
) {
2980 "%s: socket %d version mismatch, marker %d, version %d",
2981 __func__
, sock
, marker
, version
);
2982 zebra_client_close(client
);
2985 if (length
< ZEBRA_HEADER_SIZE
) {
2987 "%s: socket %d message length %u is less than header size %d",
2988 __func__
, sock
, length
, ZEBRA_HEADER_SIZE
);
2989 zebra_client_close(client
);
2992 if (length
> STREAM_SIZE(client
->ibuf
)) {
2994 "%s: socket %d message length %u exceeds buffer size %lu",
2995 __func__
, sock
, length
,
2996 (u_long
)STREAM_SIZE(client
->ibuf
));
2997 zebra_client_close(client
);
3001 /* Read rest of data. */
3002 if (already
< length
) {
3004 if (((nbyte
= stream_read_try(client
->ibuf
, sock
,
3008 if (IS_ZEBRA_DEBUG_EVENT
)
3010 "connection closed [%d] when reading zebra data",
3012 zebra_client_close(client
);
3015 if (nbyte
!= (ssize_t
)(length
- already
)) {
3016 /* Try again later. */
3017 zebra_event(ZEBRA_READ
, sock
, client
);
3022 #if defined(HANDLE_ZAPI_FUZZING)
3023 zserv_write_incoming(client
->ibuf
, command
);
3025 length
-= ZEBRA_HEADER_SIZE
;
3027 /* Debug packet information. */
3028 if (IS_ZEBRA_DEBUG_EVENT
)
3029 zlog_debug("zebra message comes from socket [%d]",
3032 if (IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
)
3033 zlog_debug("zebra message received [%s] %d in VRF %u",
3034 zserv_command_string(command
), length
,
3037 client
->last_read_time
= monotime(NULL
);
3038 client
->last_read_cmd
= command
;
3040 zvrf
= zebra_vrf_lookup_by_id(vrf_id
);
3042 if (IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
)
3043 zlog_debug("zebra received unknown VRF[%u]",
3045 goto zclient_read_out
;
3048 zserv_handle_commands(client
, command
, length
, zvrf
);
3050 if (client
->t_suicide
) {
3051 /* No need to wait for thread callback, just kill
3054 zebra_client_close(client
);
3058 stream_reset(client
->ibuf
);
3063 stream_reset(client
->ibuf
);
3064 zebra_event(ZEBRA_READ
, sock
, client
);
3069 /* Accept code of zebra server socket. */
3070 static int zebra_accept(struct thread
*thread
)
3074 struct sockaddr_in client
;
3077 accept_sock
= THREAD_FD(thread
);
3079 /* Reregister myself. */
3080 zebra_event(ZEBRA_SERV
, accept_sock
, NULL
);
3082 len
= sizeof(struct sockaddr_in
);
3083 client_sock
= accept(accept_sock
, (struct sockaddr
*)&client
, &len
);
3085 if (client_sock
< 0) {
3086 zlog_warn("Can't accept zebra socket: %s",
3087 safe_strerror(errno
));
3091 /* Make client socket non-blocking. */
3092 set_nonblocking(client_sock
);
3094 /* Create new zebra client. */
3095 zebra_client_create(client_sock
);
3100 /* Make zebra server socket, wiping any existing one (see bug #403). */
3101 void zebra_zserv_socket_init(char *path
)
3106 struct sockaddr_storage sa
;
3109 if (!frr_zclient_addr(&sa
, &sa_len
, path
))
3110 /* should be caught in zebra main() */
3114 old_mask
= umask(0077);
3116 /* Make UNIX domain socket. */
3117 sock
= socket(sa
.ss_family
, SOCK_STREAM
, 0);
3119 zlog_warn("Can't create zserv socket: %s",
3120 safe_strerror(errno
));
3122 "zebra can't provide full functionality due to above error");
3126 if (sa
.ss_family
!= AF_UNIX
) {
3127 sockopt_reuseaddr(sock
);
3128 sockopt_reuseport(sock
);
3130 struct sockaddr_un
*suna
= (struct sockaddr_un
*)&sa
;
3131 if (suna
->sun_path
[0])
3132 unlink(suna
->sun_path
);
3135 zserv_privs
.change(ZPRIVS_RAISE
);
3136 setsockopt_so_recvbuf(sock
, 1048576);
3137 setsockopt_so_sendbuf(sock
, 1048576);
3138 zserv_privs
.change(ZPRIVS_LOWER
);
3140 if (sa
.ss_family
!= AF_UNIX
&& zserv_privs
.change(ZPRIVS_RAISE
))
3141 zlog_err("Can't raise privileges");
3143 ret
= bind(sock
, (struct sockaddr
*)&sa
, sa_len
);
3145 zlog_warn("Can't bind zserv socket on %s: %s", path
,
3146 safe_strerror(errno
));
3148 "zebra can't provide full functionality due to above error");
3152 if (sa
.ss_family
!= AF_UNIX
&& zserv_privs
.change(ZPRIVS_LOWER
))
3153 zlog_err("Can't lower privileges");
3155 ret
= listen(sock
, 5);
3157 zlog_warn("Can't listen to zserv socket %s: %s", path
,
3158 safe_strerror(errno
));
3160 "zebra can't provide full functionality due to above error");
3167 zebra_event(ZEBRA_SERV
, sock
, NULL
);
3171 static void zebra_event(enum event event
, int sock
, struct zserv
*client
)
3175 thread_add_read(zebrad
.master
, zebra_accept
, client
, sock
,
3179 client
->t_read
= NULL
;
3180 thread_add_read(zebrad
.master
, zebra_client_read
, client
, sock
,
3189 #define ZEBRA_TIME_BUF 32
3190 static char *zserv_time_buf(time_t *time1
, char *buf
, int buflen
)
3195 assert(buf
!= NULL
);
3196 assert(buflen
>= ZEBRA_TIME_BUF
);
3197 assert(time1
!= NULL
);
3200 snprintf(buf
, buflen
, "never ");
3204 now
= monotime(NULL
);
3208 if (now
< ONE_DAY_SECOND
)
3209 snprintf(buf
, buflen
, "%02d:%02d:%02d", tm
->tm_hour
, tm
->tm_min
,
3211 else if (now
< ONE_WEEK_SECOND
)
3212 snprintf(buf
, buflen
, "%dd%02dh%02dm", tm
->tm_yday
, tm
->tm_hour
,
3215 snprintf(buf
, buflen
, "%02dw%dd%02dh", tm
->tm_yday
/ 7,
3216 tm
->tm_yday
- ((tm
->tm_yday
/ 7) * 7), tm
->tm_hour
);
3220 static void zebra_show_client_detail(struct vty
*vty
, struct zserv
*client
)
3222 char cbuf
[ZEBRA_TIME_BUF
], rbuf
[ZEBRA_TIME_BUF
];
3223 char wbuf
[ZEBRA_TIME_BUF
], nhbuf
[ZEBRA_TIME_BUF
], mbuf
[ZEBRA_TIME_BUF
];
3225 vty_out(vty
, "Client: %s", zebra_route_string(client
->proto
));
3226 if (client
->instance
)
3227 vty_out(vty
, " Instance: %d", client
->instance
);
3230 vty_out(vty
, "------------------------ \n");
3231 vty_out(vty
, "FD: %d \n", client
->sock
);
3232 vty_out(vty
, "Route Table ID: %d \n", client
->rtm_table
);
3234 vty_out(vty
, "Connect Time: %s \n",
3235 zserv_time_buf(&client
->connect_time
, cbuf
, ZEBRA_TIME_BUF
));
3236 if (client
->nh_reg_time
) {
3237 vty_out(vty
, "Nexthop Registry Time: %s \n",
3238 zserv_time_buf(&client
->nh_reg_time
, nhbuf
,
3240 if (client
->nh_last_upd_time
)
3241 vty_out(vty
, "Nexthop Last Update Time: %s \n",
3242 zserv_time_buf(&client
->nh_last_upd_time
, mbuf
,
3245 vty_out(vty
, "No Nexthop Update sent\n");
3247 vty_out(vty
, "Not registered for Nexthop Updates\n");
3249 vty_out(vty
, "Last Msg Rx Time: %s \n",
3250 zserv_time_buf(&client
->last_read_time
, rbuf
, ZEBRA_TIME_BUF
));
3251 vty_out(vty
, "Last Msg Tx Time: %s \n",
3252 zserv_time_buf(&client
->last_write_time
, wbuf
, ZEBRA_TIME_BUF
));
3253 if (client
->last_read_time
)
3254 vty_out(vty
, "Last Rcvd Cmd: %s \n",
3255 zserv_command_string(client
->last_read_cmd
));
3256 if (client
->last_write_time
)
3257 vty_out(vty
, "Last Sent Cmd: %s \n",
3258 zserv_command_string(client
->last_write_cmd
));
3261 vty_out(vty
, "Type Add Update Del \n");
3262 vty_out(vty
, "================================================== \n");
3263 vty_out(vty
, "IPv4 %-12d%-12d%-12d\n", client
->v4_route_add_cnt
,
3264 client
->v4_route_upd8_cnt
, client
->v4_route_del_cnt
);
3265 vty_out(vty
, "IPv6 %-12d%-12d%-12d\n", client
->v6_route_add_cnt
,
3266 client
->v6_route_upd8_cnt
, client
->v6_route_del_cnt
);
3267 vty_out(vty
, "Redist:v4 %-12d%-12d%-12d\n", client
->redist_v4_add_cnt
,
3268 0, client
->redist_v4_del_cnt
);
3269 vty_out(vty
, "Redist:v6 %-12d%-12d%-12d\n", client
->redist_v6_add_cnt
,
3270 0, client
->redist_v6_del_cnt
);
3271 vty_out(vty
, "Connected %-12d%-12d%-12d\n", client
->ifadd_cnt
, 0,
3273 vty_out(vty
, "BFD peer %-12d%-12d%-12d\n", client
->bfd_peer_add_cnt
,
3274 client
->bfd_peer_upd8_cnt
, client
->bfd_peer_del_cnt
);
3275 vty_out(vty
, "Interface Up Notifications: %d\n", client
->ifup_cnt
);
3276 vty_out(vty
, "Interface Down Notifications: %d\n", client
->ifdown_cnt
);
3277 vty_out(vty
, "VNI add notifications: %d\n", client
->vniadd_cnt
);
3278 vty_out(vty
, "VNI delete notifications: %d\n", client
->vnidel_cnt
);
3279 vty_out(vty
, "L3-VNI add notifications: %d\n", client
->l3vniadd_cnt
);
3280 vty_out(vty
, "L3-VNI delete notifications: %d\n", client
->l3vnidel_cnt
);
3281 vty_out(vty
, "MAC-IP add notifications: %d\n", client
->macipadd_cnt
);
3282 vty_out(vty
, "MAC-IP delete notifications: %d\n", client
->macipdel_cnt
);
3288 static void zebra_show_client_brief(struct vty
*vty
, struct zserv
*client
)
3290 char cbuf
[ZEBRA_TIME_BUF
], rbuf
[ZEBRA_TIME_BUF
];
3291 char wbuf
[ZEBRA_TIME_BUF
];
3293 vty_out(vty
, "%-8s%12s %12s%12s%8d/%-8d%8d/%-8d\n",
3294 zebra_route_string(client
->proto
),
3295 zserv_time_buf(&client
->connect_time
, cbuf
, ZEBRA_TIME_BUF
),
3296 zserv_time_buf(&client
->last_read_time
, rbuf
, ZEBRA_TIME_BUF
),
3297 zserv_time_buf(&client
->last_write_time
, wbuf
, ZEBRA_TIME_BUF
),
3298 client
->v4_route_add_cnt
+ client
->v4_route_upd8_cnt
,
3299 client
->v4_route_del_cnt
,
3300 client
->v6_route_add_cnt
+ client
->v6_route_upd8_cnt
,
3301 client
->v6_route_del_cnt
);
3304 struct zserv
*zebra_find_client(u_char proto
, u_short instance
)
3306 struct listnode
*node
, *nnode
;
3307 struct zserv
*client
;
3309 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, node
, nnode
, client
)) {
3310 if (client
->proto
== proto
&& client
->instance
== instance
)
3317 /* This command is for debugging purpose. */
3318 DEFUN (show_zebra_client
,
3319 show_zebra_client_cmd
,
3320 "show zebra client",
3323 "Client information\n")
3325 struct listnode
*node
;
3326 struct zserv
*client
;
3328 for (ALL_LIST_ELEMENTS_RO(zebrad
.client_list
, node
, client
))
3329 zebra_show_client_detail(vty
, client
);
3334 /* This command is for debugging purpose. */
3335 DEFUN (show_zebra_client_summary
,
3336 show_zebra_client_summary_cmd
,
3337 "show zebra client summary",
3340 "Client information brief\n"
3343 struct listnode
*node
;
3344 struct zserv
*client
;
3347 "Name Connect Time Last Read Last Write IPv4 Routes IPv6 Routes \n");
3349 "--------------------------------------------------------------------------------\n");
3351 for (ALL_LIST_ELEMENTS_RO(zebrad
.client_list
, node
, client
))
3352 zebra_show_client_brief(vty
, client
);
3354 vty_out(vty
, "Routes column shows (added+updated)/deleted\n");
3358 #if defined(HANDLE_ZAPI_FUZZING)
3359 void zserv_read_file(char *input
)
3362 struct zserv
*client
= NULL
;
3365 zebra_client_create(-1);
3366 client
= zebrad
.client_list
->head
->data
;
3369 fd
= open(input
, O_RDONLY
| O_NONBLOCK
);
3372 zebra_client_read(&t
);
3378 void zserv_init(void)
3380 /* Client list init. */
3381 zebrad
.client_list
= list_new();
3382 zebrad
.client_list
->del
= (void (*)(void *))zebra_client_free
;
3384 install_element(ENABLE_NODE
, &show_zebra_client_cmd
);
3385 install_element(ENABLE_NODE
, &show_zebra_client_summary_cmd
);