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)
666 return zebra_server_send_message(client
);
669 static int zsend_write_nexthop(struct stream
*s
, struct nexthop
*nexthop
)
671 stream_putc(s
, nexthop
->type
);
672 switch (nexthop
->type
) {
673 case NEXTHOP_TYPE_IPV4
:
674 case NEXTHOP_TYPE_IPV4_IFINDEX
:
675 stream_put_in_addr(s
, &nexthop
->gate
.ipv4
);
676 stream_putl(s
, nexthop
->ifindex
);
678 case NEXTHOP_TYPE_IPV6
:
679 stream_put(s
, &nexthop
->gate
.ipv6
, 16);
681 case NEXTHOP_TYPE_IPV6_IFINDEX
:
682 stream_put(s
, &nexthop
->gate
.ipv6
, 16);
683 stream_putl(s
, nexthop
->ifindex
);
685 case NEXTHOP_TYPE_IFINDEX
:
686 stream_putl(s
, nexthop
->ifindex
);
695 /* Nexthop register */
696 static int zserv_rnh_register(struct zserv
*client
, u_short length
,
697 rnh_type_t type
, struct zebra_vrf
*zvrf
)
705 if (IS_ZEBRA_DEBUG_NHT
)
707 "rnh_register msg from client %s: length=%d, type=%s\n",
708 zebra_route_string(client
->proto
), length
,
709 (type
== RNH_NEXTHOP_TYPE
) ? "nexthop" : "route");
713 client
->nh_reg_time
= monotime(NULL
);
716 STREAM_GETC(s
, flags
);
717 STREAM_GETW(s
, p
.family
);
718 STREAM_GETC(s
, p
.prefixlen
);
720 if (p
.family
== AF_INET
) {
721 if (p
.prefixlen
> IPV4_MAX_BITLEN
) {
723 "%s: Specified prefix length %d is too large for a v4 address",
724 __PRETTY_FUNCTION__
, p
.prefixlen
);
727 STREAM_GET(&p
.u
.prefix4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
728 l
+= IPV4_MAX_BYTELEN
;
729 } else if (p
.family
== AF_INET6
) {
730 if (p
.prefixlen
> IPV6_MAX_BITLEN
) {
732 "%s: Specified prefix length %d is to large for a v6 address",
733 __PRETTY_FUNCTION__
, p
.prefixlen
);
736 STREAM_GET(&p
.u
.prefix6
, s
, IPV6_MAX_BYTELEN
);
737 l
+= IPV6_MAX_BYTELEN
;
740 "rnh_register: Received unknown family type %d\n",
744 rnh
= zebra_add_rnh(&p
, zvrf_id(zvrf
), type
);
745 if (type
== RNH_NEXTHOP_TYPE
) {
747 && !CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
))
748 SET_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
);
750 && CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
))
751 UNSET_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
);
752 } else if (type
== RNH_IMPORT_CHECK_TYPE
) {
754 && !CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
))
755 SET_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
);
756 else if (!flags
&& CHECK_FLAG(rnh
->flags
,
757 ZEBRA_NHT_EXACT_MATCH
))
758 UNSET_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
);
761 zebra_add_rnh_client(rnh
, client
, type
, zvrf_id(zvrf
));
762 /* Anything not AF_INET/INET6 has been filtered out above */
763 zebra_evaluate_rnh(zvrf_id(zvrf
), p
.family
, 1, type
, &p
);
770 /* Nexthop register */
771 static int zserv_rnh_unregister(struct zserv
*client
, u_short length
,
772 rnh_type_t type
, struct zebra_vrf
*zvrf
)
779 if (IS_ZEBRA_DEBUG_NHT
)
780 zlog_debug("rnh_unregister msg from client %s: length=%d\n",
781 zebra_route_string(client
->proto
), length
);
788 STREAM_GETC(s
, flags
);
792 STREAM_GETW(s
, p
.family
);
793 STREAM_GETC(s
, p
.prefixlen
);
795 if (p
.family
== AF_INET
) {
796 if (p
.prefixlen
> IPV4_MAX_BITLEN
) {
798 "%s: Specified prefix length %d is to large for a v4 address",
799 __PRETTY_FUNCTION__
, p
.prefixlen
);
802 STREAM_GET(&p
.u
.prefix4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
803 l
+= IPV4_MAX_BYTELEN
;
804 } else if (p
.family
== AF_INET6
) {
805 if (p
.prefixlen
> IPV6_MAX_BITLEN
) {
807 "%s: Specified prefix length %d is to large for a v6 address",
808 __PRETTY_FUNCTION__
, p
.prefixlen
);
811 STREAM_GET(&p
.u
.prefix6
, s
, IPV6_MAX_BYTELEN
);
812 l
+= IPV6_MAX_BYTELEN
;
815 "rnh_register: Received unknown family type %d\n",
819 rnh
= zebra_lookup_rnh(&p
, zvrf_id(zvrf
), type
);
821 client
->nh_dereg_time
= monotime(NULL
);
822 zebra_remove_rnh_client(rnh
, client
, type
);
829 #define ZEBRA_MIN_FEC_LENGTH 5
832 static int zserv_fec_register(struct zserv
*client
, u_short length
)
835 struct zebra_vrf
*zvrf
;
839 u_int32_t label_index
= MPLS_INVALID_LABEL_INDEX
;
842 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
844 return 0; // unexpected
847 * The minimum amount of data that can be sent for one fec
850 if (length
< ZEBRA_MIN_FEC_LENGTH
) {
852 "fec_register: Received a fec register of length %d, it is of insufficient size to properly decode",
858 STREAM_GETW(s
, flags
);
859 memset(&p
, 0, sizeof(p
));
860 STREAM_GETW(s
, p
.family
);
861 if (p
.family
!= AF_INET
&& p
.family
!= AF_INET6
) {
863 "fec_register: Received unknown family type %d\n",
867 STREAM_GETC(s
, p
.prefixlen
);
868 if ((p
.family
== AF_INET
&& p
.prefixlen
> IPV4_MAX_BITLEN
)
869 || (p
.family
== AF_INET6
870 && p
.prefixlen
> IPV6_MAX_BITLEN
)) {
872 "%s: Specified prefix length: %d is to long for %d",
873 __PRETTY_FUNCTION__
, p
.prefixlen
, p
.family
);
877 STREAM_GET(&p
.u
.prefix
, s
, PSIZE(p
.prefixlen
));
878 l
+= PSIZE(p
.prefixlen
);
879 if (flags
& ZEBRA_FEC_REGISTER_LABEL_INDEX
) {
880 STREAM_GETL(s
, label_index
);
883 label_index
= MPLS_INVALID_LABEL_INDEX
;
884 zebra_mpls_fec_register(zvrf
, &p
, label_index
, client
);
892 static int zserv_fec_unregister(struct zserv
*client
, u_short length
)
895 struct zebra_vrf
*zvrf
;
901 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
903 return 0; // unexpected
906 * The minimum amount of data that can be sent for one
909 if (length
< ZEBRA_MIN_FEC_LENGTH
) {
911 "fec_unregister: Received a fec unregister of length %d, it is of insufficient size to properly decode",
917 STREAM_GETW(s
, flags
);
921 memset(&p
, 0, sizeof(p
));
922 STREAM_GETW(s
, p
.family
);
923 if (p
.family
!= AF_INET
&& p
.family
!= AF_INET6
) {
925 "fec_unregister: Received unknown family type %d\n",
929 STREAM_GETC(s
, p
.prefixlen
);
930 if ((p
.family
== AF_INET
&& p
.prefixlen
> IPV4_MAX_BITLEN
)
931 || (p
.family
== AF_INET6
932 && p
.prefixlen
> IPV6_MAX_BITLEN
)) {
934 "%s: Received prefix length %d which is greater than %d can support",
935 __PRETTY_FUNCTION__
, p
.prefixlen
, p
.family
);
939 STREAM_GET(&p
.u
.prefix
, s
, PSIZE(p
.prefixlen
));
940 l
+= PSIZE(p
.prefixlen
);
941 zebra_mpls_fec_unregister(zvrf
, &p
, client
);
949 Modified version of zsend_ipv4_nexthop_lookup():
950 Query unicast rib if nexthop is not found on mrib.
951 Returns both route metric and protocol distance.
953 static int zsend_ipv4_nexthop_lookup_mrib(struct zserv
*client
,
955 struct route_entry
*re
,
956 struct zebra_vrf
*zvrf
)
961 struct nexthop
*nexthop
;
963 /* Get output stream. */
967 /* Fill in result. */
968 zclient_create_header(s
, ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB
, zvrf_id(zvrf
));
969 stream_put_in_addr(s
, &addr
);
972 stream_putc(s
, re
->distance
);
973 stream_putl(s
, re
->metric
);
975 nump
= stream_get_endp(
976 s
); /* remember position for nexthop_num */
977 stream_putc(s
, 0); /* reserve room for nexthop_num */
978 /* Only non-recursive routes are elegible to resolve the nexthop
980 * are looking up. Therefore, we will just iterate over the top
981 * chain of nexthops. */
982 for (nexthop
= re
->ng
.nexthop
; nexthop
; nexthop
= nexthop
->next
)
983 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
984 num
+= zsend_write_nexthop(s
, nexthop
);
986 stream_putc_at(s
, nump
, num
); /* store nexthop_num */
988 stream_putc(s
, 0); /* distance */
989 stream_putl(s
, 0); /* metric */
990 stream_putc(s
, 0); /* nexthop_num */
993 stream_putw_at(s
, 0, stream_get_endp(s
));
995 return zebra_server_send_message(client
);
998 int zsend_route_notify_owner(struct route_entry
*re
, struct prefix
*p
,
999 enum zapi_route_notify_owner note
)
1001 struct zserv
*client
;
1005 client
= zebra_find_client(re
->type
, re
->instance
);
1006 if (!client
|| !client
->notify_owner
) {
1007 if (IS_ZEBRA_DEBUG_PACKET
) {
1008 char buff
[PREFIX_STRLEN
];
1011 "Not Notifying Owner: %u about prefix %s(%u) %d",
1012 re
->type
, prefix2str(p
, buff
, sizeof(buff
)),
1018 if (IS_ZEBRA_DEBUG_PACKET
) {
1019 char buff
[PREFIX_STRLEN
];
1021 zlog_debug("Notifying Owner: %u about prefix %s(%u) %d",
1022 re
->type
, prefix2str(p
, buff
, sizeof(buff
)),
1029 zclient_create_header(s
, ZEBRA_ROUTE_NOTIFY_OWNER
, re
->vrf_id
);
1031 stream_put(s
, ¬e
, sizeof(note
));
1033 stream_putc(s
, p
->family
);
1035 blen
= prefix_blen(p
);
1036 stream_putc(s
, p
->prefixlen
);
1037 stream_put(s
, &p
->u
.prefix
, blen
);
1039 stream_putl(s
, re
->table
);
1041 stream_putw_at(s
, 0, stream_get_endp(s
));
1043 return zebra_server_send_message(client
);
1046 void zsend_rule_notify_owner(struct zebra_pbr_rule
*rule
,
1047 enum zapi_rule_notify_owner note
)
1049 struct listnode
*node
;
1050 struct zserv
*client
;
1053 if (IS_ZEBRA_DEBUG_PACKET
) {
1054 zlog_debug("%s: Notifying %u",
1055 __PRETTY_FUNCTION__
, rule
->unique
);
1058 for (ALL_LIST_ELEMENTS_RO(zebrad
.client_list
, node
, client
)) {
1059 if (rule
->sock
== client
->sock
)
1069 zclient_create_header(s
, ZEBRA_RULE_NOTIFY_OWNER
, VRF_DEFAULT
);
1070 stream_put(s
, ¬e
, sizeof(note
));
1071 stream_putl(s
, rule
->seq
);
1072 stream_putl(s
, rule
->priority
);
1073 stream_putl(s
, rule
->unique
);
1075 stream_putl(s
, rule
->ifp
->ifindex
);
1079 stream_putw_at(s
, 0, stream_get_endp(s
));
1081 zebra_server_send_message(client
);
1084 /* Router-id is updated. Send ZEBRA_ROUTER_ID_ADD to client. */
1085 int zsend_router_id_update(struct zserv
*client
, struct prefix
*p
,
1091 /* Check this client need interface information. */
1092 if (!vrf_bitmap_check(client
->ridinfo
, vrf_id
))
1099 zclient_create_header(s
, ZEBRA_ROUTER_ID_UPDATE
, vrf_id
);
1101 /* Prefix information. */
1102 stream_putc(s
, p
->family
);
1103 blen
= prefix_blen(p
);
1104 stream_put(s
, &p
->u
.prefix
, blen
);
1105 stream_putc(s
, p
->prefixlen
);
1107 /* Write packet size. */
1108 stream_putw_at(s
, 0, stream_get_endp(s
));
1110 return zebra_server_send_message(client
);
1114 * Function used by Zebra to send a PW status update to LDP daemon
1116 int zsend_pw_update(struct zserv
*client
, struct zebra_pw
*pw
)
1123 zclient_create_header(s
, ZEBRA_PW_STATUS_UPDATE
, pw
->vrf_id
);
1124 stream_write(s
, pw
->ifname
, IF_NAMESIZE
);
1125 stream_putl(s
, pw
->ifindex
);
1126 stream_putl(s
, pw
->status
);
1128 /* Put length at the first point of the stream. */
1129 stream_putw_at(s
, 0, stream_get_endp(s
));
1131 return zebra_server_send_message(client
);
1134 /* Register zebra server interface information. Send current all
1135 interface and address information. */
1136 static int zread_interface_add(struct zserv
*client
, u_short length
,
1137 struct zebra_vrf
*zvrf
)
1140 struct interface
*ifp
;
1142 /* Interface information is needed. */
1143 vrf_bitmap_set(client
->ifinfo
, zvrf_id(zvrf
));
1145 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
1146 FOR_ALL_INTERFACES (vrf
, ifp
) {
1147 /* Skip pseudo interface. */
1148 if (!CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_ACTIVE
))
1151 if (zsend_interface_add(client
, ifp
) < 0)
1154 if (zsend_interface_addresses(client
, ifp
) < 0)
1161 /* Unregister zebra server interface information. */
1162 static int zread_interface_delete(struct zserv
*client
, u_short length
,
1163 struct zebra_vrf
*zvrf
)
1165 vrf_bitmap_unset(client
->ifinfo
, zvrf_id(zvrf
));
1169 void zserv_nexthop_num_warn(const char *caller
, const struct prefix
*p
,
1170 const unsigned int nexthop_num
)
1172 if (nexthop_num
> multipath_num
) {
1173 char buff
[PREFIX2STR_BUFFER
];
1174 prefix2str(p
, buff
, sizeof(buff
));
1176 "%s: Prefix %s has %d nexthops, but we can only use the first %d",
1177 caller
, buff
, nexthop_num
, multipath_num
);
1181 static int zread_route_add(struct zserv
*client
, u_short length
,
1182 struct zebra_vrf
*zvrf
)
1185 struct zapi_route api
;
1186 struct zapi_nexthop
*api_nh
;
1188 struct prefix_ipv6
*src_p
= NULL
;
1189 struct route_entry
*re
;
1190 struct nexthop
*nexthop
= NULL
;
1192 vrf_id_t vrf_id
= 0;
1195 if (zapi_route_decode(s
, &api
) < 0)
1198 /* Allocate new route. */
1199 vrf_id
= zvrf_id(zvrf
);
1200 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
1201 re
->type
= api
.type
;
1202 re
->instance
= api
.instance
;
1203 re
->flags
= api
.flags
;
1204 re
->uptime
= time(NULL
);
1205 re
->vrf_id
= vrf_id
;
1206 if (api
.tableid
&& vrf_id
== VRF_DEFAULT
)
1207 re
->table
= api
.tableid
;
1209 re
->table
= zvrf
->table_id
;
1211 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
)) {
1212 for (i
= 0; i
< api
.nexthop_num
; i
++) {
1213 api_nh
= &api
.nexthops
[i
];
1214 ifindex_t ifindex
= 0;
1216 switch (api_nh
->type
) {
1217 case NEXTHOP_TYPE_IFINDEX
:
1218 nexthop
= route_entry_nexthop_ifindex_add(
1219 re
, api_nh
->ifindex
, api_nh
->vrf_id
);
1221 case NEXTHOP_TYPE_IPV4
:
1222 nexthop
= route_entry_nexthop_ipv4_add(
1223 re
, &api_nh
->gate
.ipv4
, NULL
,
1226 case NEXTHOP_TYPE_IPV4_IFINDEX
: {
1228 struct ipaddr vtep_ip
;
1230 memset(&vtep_ip
, 0, sizeof(struct ipaddr
));
1231 if (CHECK_FLAG(api
.flags
,
1232 ZEBRA_FLAG_EVPN_ROUTE
)) {
1233 ifindex
= get_l3vni_svi_ifindex(vrf_id
);
1235 ifindex
= api_nh
->ifindex
;
1238 nexthop
= route_entry_nexthop_ipv4_ifindex_add(
1239 re
, &api_nh
->gate
.ipv4
, NULL
, ifindex
,
1242 /* if this an EVPN route entry,
1243 program the nh as neigh
1245 if (CHECK_FLAG(api
.flags
,
1246 ZEBRA_FLAG_EVPN_ROUTE
)) {
1247 SET_FLAG(nexthop
->flags
,
1248 NEXTHOP_FLAG_EVPN_RVTEP
);
1249 vtep_ip
.ipa_type
= IPADDR_V4
;
1250 memcpy(&(vtep_ip
.ipaddr_v4
),
1251 &(api_nh
->gate
.ipv4
),
1252 sizeof(struct in_addr
));
1253 zebra_vxlan_evpn_vrf_route_add(
1254 vrf_id
, &api
.rmac
, &vtep_ip
,
1259 case NEXTHOP_TYPE_IPV6
:
1260 nexthop
= route_entry_nexthop_ipv6_add(
1261 re
, &api_nh
->gate
.ipv6
, api_nh
->vrf_id
);
1263 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1264 nexthop
= route_entry_nexthop_ipv6_ifindex_add(
1265 re
, &api_nh
->gate
.ipv6
, api_nh
->ifindex
,
1268 case NEXTHOP_TYPE_BLACKHOLE
:
1269 nexthop
= route_entry_nexthop_blackhole_add(
1270 re
, api_nh
->bh_type
);
1276 "%s: Nexthops Specified: %d but we failed to properly create one",
1277 __PRETTY_FUNCTION__
, api
.nexthop_num
);
1278 nexthops_free(re
->ng
.nexthop
);
1279 XFREE(MTYPE_RE
, re
);
1282 /* MPLS labels for BGP-LU or Segment Routing */
1283 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_LABEL
)
1284 && api_nh
->type
!= NEXTHOP_TYPE_IFINDEX
1285 && api_nh
->type
!= NEXTHOP_TYPE_BLACKHOLE
) {
1286 enum lsp_types_t label_type
;
1289 lsp_type_from_re_type(client
->proto
);
1290 nexthop_add_labels(nexthop
, label_type
,
1292 &api_nh
->labels
[0]);
1297 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
))
1298 re
->distance
= api
.distance
;
1299 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
))
1300 re
->metric
= api
.metric
;
1301 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_TAG
))
1303 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_MTU
))
1306 afi
= family2afi(api
.prefix
.family
);
1307 if (afi
!= AFI_IP6
&& CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
)) {
1308 zlog_warn("%s: Received SRC Prefix but afi is not v6",
1309 __PRETTY_FUNCTION__
);
1310 nexthops_free(re
->ng
.nexthop
);
1311 XFREE(MTYPE_RE
, re
);
1314 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
))
1315 src_p
= &api
.src_prefix
;
1317 ret
= rib_add_multipath(afi
, api
.safi
, &api
.prefix
, src_p
, re
);
1320 switch (api
.prefix
.family
) {
1323 client
->v4_route_add_cnt
++;
1325 client
->v4_route_upd8_cnt
++;
1329 client
->v6_route_add_cnt
++;
1331 client
->v6_route_upd8_cnt
++;
1338 static int zread_route_del(struct zserv
*client
, u_short length
,
1339 struct zebra_vrf
*zvrf
)
1342 struct zapi_route api
;
1344 struct prefix_ipv6
*src_p
= NULL
;
1347 if (zapi_route_decode(s
, &api
) < 0)
1350 afi
= family2afi(api
.prefix
.family
);
1351 if (afi
!= AFI_IP6
&& CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
)) {
1352 zlog_warn("%s: Received a src prefix while afi is not v6",
1353 __PRETTY_FUNCTION__
);
1356 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
))
1357 src_p
= &api
.src_prefix
;
1359 rib_delete(afi
, api
.safi
, zvrf_id(zvrf
), api
.type
, api
.instance
,
1360 api
.flags
, &api
.prefix
, src_p
, NULL
, zvrf
->table_id
,
1361 api
.metric
, false, &api
.rmac
);
1364 switch (api
.prefix
.family
) {
1366 client
->v4_route_del_cnt
++;
1369 client
->v6_route_del_cnt
++;
1376 /* This function support multiple nexthop. */
1378 * Parse the ZEBRA_IPV4_ROUTE_ADD sent from client. Update re and
1381 static int zread_ipv4_add(struct zserv
*client
, u_short length
,
1382 struct zebra_vrf
*zvrf
)
1385 struct route_entry
*re
;
1388 struct in_addr nhop_addr
;
1390 u_char nexthop_type
;
1395 enum lsp_types_t label_type
= ZEBRA_LSP_NONE
;
1397 struct nexthop
*nexthop
;
1398 enum blackhole_type bh_type
= BLACKHOLE_NULL
;
1400 /* Get input stream. */
1403 /* Allocate new re. */
1404 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
1406 /* Type, flags, message. */
1407 STREAM_GETC(s
, re
->type
);
1408 if (re
->type
> ZEBRA_ROUTE_MAX
) {
1409 zlog_warn("%s: Specified route type %d is not a legal value\n",
1410 __PRETTY_FUNCTION__
, re
->type
);
1411 XFREE(MTYPE_RE
, re
);
1414 STREAM_GETW(s
, re
->instance
);
1415 STREAM_GETL(s
, re
->flags
);
1416 STREAM_GETC(s
, message
);
1417 STREAM_GETW(s
, safi
);
1418 re
->uptime
= time(NULL
);
1421 memset(&p
, 0, sizeof(struct prefix_ipv4
));
1423 STREAM_GETC(s
, p
.prefixlen
);
1424 if (p
.prefixlen
> IPV4_MAX_BITLEN
) {
1426 "%s: Specified prefix length %d is greater than what v4 can be",
1427 __PRETTY_FUNCTION__
, p
.prefixlen
);
1428 XFREE(MTYPE_RE
, re
);
1431 STREAM_GET(&p
.u
.prefix4
, s
, PSIZE(p
.prefixlen
));
1434 re
->vrf_id
= zvrf_id(zvrf
);
1436 /* Nexthop parse. */
1437 if (CHECK_FLAG(message
, ZAPI_MESSAGE_NEXTHOP
)) {
1438 STREAM_GETC(s
, nexthop_num
);
1439 zserv_nexthop_num_warn(__func__
, (const struct prefix
*)&p
,
1442 if (CHECK_FLAG(message
, ZAPI_MESSAGE_LABEL
))
1443 label_type
= lsp_type_from_re_type(client
->proto
);
1445 for (i
= 0; i
< nexthop_num
; i
++) {
1446 STREAM_GETC(s
, nexthop_type
);
1448 switch (nexthop_type
) {
1449 case NEXTHOP_TYPE_IFINDEX
:
1450 STREAM_GETL(s
, ifindex
);
1451 route_entry_nexthop_ifindex_add(re
, ifindex
,
1454 case NEXTHOP_TYPE_IPV4
:
1455 STREAM_GET(&nhop_addr
.s_addr
, s
,
1457 nexthop
= route_entry_nexthop_ipv4_add(
1458 re
, &nhop_addr
, NULL
, re
->vrf_id
);
1459 /* For labeled-unicast, each nexthop is followed
1461 if (CHECK_FLAG(message
, ZAPI_MESSAGE_LABEL
)) {
1462 STREAM_GETL(s
, label
);
1463 nexthop_add_labels(nexthop
, label_type
,
1467 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1468 STREAM_GET(&nhop_addr
.s_addr
, s
,
1470 STREAM_GETL(s
, ifindex
);
1471 route_entry_nexthop_ipv4_ifindex_add(
1472 re
, &nhop_addr
, NULL
, ifindex
,
1475 case NEXTHOP_TYPE_IPV6
:
1477 "%s: Please use ZEBRA_ROUTE_ADD if you want to pass v6 nexthops",
1478 __PRETTY_FUNCTION__
);
1479 nexthops_free(re
->ng
.nexthop
);
1480 XFREE(MTYPE_RE
, re
);
1483 case NEXTHOP_TYPE_BLACKHOLE
:
1484 route_entry_nexthop_blackhole_add(re
, bh_type
);
1488 "%s: Specified nexthop type: %d does not exist",
1489 __PRETTY_FUNCTION__
, nexthop_type
);
1490 nexthops_free(re
->ng
.nexthop
);
1491 XFREE(MTYPE_RE
, re
);
1498 if (CHECK_FLAG(message
, ZAPI_MESSAGE_DISTANCE
))
1499 STREAM_GETC(s
, re
->distance
);
1502 if (CHECK_FLAG(message
, ZAPI_MESSAGE_METRIC
))
1503 STREAM_GETL(s
, re
->metric
);
1506 if (CHECK_FLAG(message
, ZAPI_MESSAGE_TAG
))
1507 STREAM_GETL(s
, re
->tag
);
1511 if (CHECK_FLAG(message
, ZAPI_MESSAGE_MTU
))
1512 STREAM_GETL(s
, re
->mtu
);
1517 re
->table
= zvrf
->table_id
;
1519 ret
= rib_add_multipath(AFI_IP
, safi
, &p
, NULL
, re
);
1523 client
->v4_route_add_cnt
++;
1525 client
->v4_route_upd8_cnt
++;
1530 nexthops_free(re
->ng
.nexthop
);
1531 XFREE(MTYPE_RE
, re
);
1535 /* Zebra server IPv4 prefix delete function. */
1536 static int zread_ipv4_delete(struct zserv
*client
, u_short length
,
1537 struct zebra_vrf
*zvrf
)
1540 struct zapi_ipv4 api
;
1546 /* Type, flags, message. */
1547 STREAM_GETC(s
, api
.type
);
1548 STREAM_GETW(s
, api
.instance
);
1549 STREAM_GETL(s
, api
.flags
);
1550 STREAM_GETC(s
, api
.message
);
1551 STREAM_GETW(s
, api
.safi
);
1554 memset(&p
, 0, sizeof(struct prefix
));
1556 STREAM_GETC(s
, p
.prefixlen
);
1557 if (p
.prefixlen
> IPV4_MAX_BITLEN
) {
1558 zlog_warn("%s: Passed in prefixlen %d is impossible",
1559 __PRETTY_FUNCTION__
, p
.prefixlen
);
1562 STREAM_GET(&p
.u
.prefix4
, s
, PSIZE(p
.prefixlen
));
1564 table_id
= zvrf
->table_id
;
1566 rib_delete(AFI_IP
, api
.safi
, zvrf_id(zvrf
), api
.type
, api
.instance
,
1567 api
.flags
, &p
, NULL
, NULL
, table_id
, 0, false, NULL
);
1568 client
->v4_route_del_cnt
++;
1574 /* MRIB Nexthop lookup for IPv4. */
1575 static int zread_ipv4_nexthop_lookup_mrib(struct zserv
*client
, u_short length
,
1576 struct zebra_vrf
*zvrf
)
1578 struct in_addr addr
;
1579 struct route_entry
*re
;
1581 STREAM_GET(&addr
.s_addr
, client
->ibuf
, IPV4_MAX_BYTELEN
);
1582 re
= rib_match_ipv4_multicast(zvrf_id(zvrf
), addr
, NULL
);
1583 return zsend_ipv4_nexthop_lookup_mrib(client
, addr
, re
, zvrf
);
1589 /* Zebra server IPv6 prefix add function. */
1590 static int zread_ipv4_route_ipv6_nexthop_add(struct zserv
*client
,
1592 struct zebra_vrf
*zvrf
)
1596 struct in6_addr nhop_addr
;
1597 struct route_entry
*re
;
1600 u_char nexthop_type
;
1603 static struct in6_addr nexthops
[MULTIPATH_NUM
];
1604 static unsigned int ifindices
[MULTIPATH_NUM
];
1606 static mpls_label_t labels
[MULTIPATH_NUM
];
1607 enum lsp_types_t label_type
= ZEBRA_LSP_NONE
;
1609 struct nexthop
*nexthop
;
1610 enum blackhole_type bh_type
= BLACKHOLE_NULL
;
1612 /* Get input stream. */
1615 memset(&nhop_addr
, 0, sizeof(struct in6_addr
));
1617 /* Allocate new re. */
1618 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
1620 /* Type, flags, message. */
1621 STREAM_GETC(s
, re
->type
);
1622 if (re
->type
> ZEBRA_ROUTE_MAX
) {
1623 zlog_warn("%s: Specified route type: %d is not a legal value\n",
1624 __PRETTY_FUNCTION__
, re
->type
);
1625 XFREE(MTYPE_RE
, re
);
1628 STREAM_GETW(s
, re
->instance
);
1629 STREAM_GETL(s
, re
->flags
);
1630 STREAM_GETC(s
, message
);
1631 STREAM_GETW(s
, safi
);
1632 re
->uptime
= time(NULL
);
1635 memset(&p
, 0, sizeof(struct prefix_ipv4
));
1637 STREAM_GETC(s
, p
.prefixlen
);
1638 if (p
.prefixlen
> IPV4_MAX_BITLEN
) {
1640 "%s: Prefix Length %d is greater than what a v4 address can use",
1641 __PRETTY_FUNCTION__
, p
.prefixlen
);
1642 XFREE(MTYPE_RE
, re
);
1645 STREAM_GET(&p
.u
.prefix4
, s
, PSIZE(p
.prefixlen
));
1648 re
->vrf_id
= zvrf_id(zvrf
);
1650 /* We need to give nh-addr, nh-ifindex with the same next-hop object
1651 * to the re to ensure that IPv6 multipathing works; need to coalesce
1652 * these. Clients should send the same number of paired set of
1653 * next-hop-addr/next-hop-ifindices. */
1654 if (CHECK_FLAG(message
, ZAPI_MESSAGE_NEXTHOP
)) {
1655 unsigned int nh_count
= 0;
1656 unsigned int if_count
= 0;
1657 unsigned int max_nh_if
= 0;
1659 STREAM_GETC(s
, nexthop_num
);
1660 zserv_nexthop_num_warn(__func__
, (const struct prefix
*)&p
,
1663 if (CHECK_FLAG(message
, ZAPI_MESSAGE_LABEL
))
1664 label_type
= lsp_type_from_re_type(client
->proto
);
1666 for (i
= 0; i
< nexthop_num
; i
++) {
1667 STREAM_GETC(s
, nexthop_type
);
1669 switch (nexthop_type
) {
1670 case NEXTHOP_TYPE_IPV6
:
1671 STREAM_GET(&nhop_addr
, s
, 16);
1672 if (nh_count
< MULTIPATH_NUM
) {
1673 /* For labeled-unicast, each nexthop is
1674 * followed by label. */
1675 if (CHECK_FLAG(message
,
1676 ZAPI_MESSAGE_LABEL
)) {
1677 STREAM_GETL(s
, label
);
1678 labels
[nh_count
] = label
;
1680 nexthops
[nh_count
] = nhop_addr
;
1684 case NEXTHOP_TYPE_IFINDEX
:
1685 if (if_count
< multipath_num
) {
1686 STREAM_GETL(s
, ifindices
[if_count
++]);
1689 case NEXTHOP_TYPE_BLACKHOLE
:
1690 route_entry_nexthop_blackhole_add(re
, bh_type
);
1694 "%s: Please use ZEBRA_ROUTE_ADD if you want to pass non v6 nexthops",
1695 __PRETTY_FUNCTION__
);
1696 nexthops_free(re
->ng
.nexthop
);
1697 XFREE(MTYPE_RE
, re
);
1702 max_nh_if
= (nh_count
> if_count
) ? nh_count
: if_count
;
1703 for (i
= 0; i
< max_nh_if
; i
++) {
1705 && !IN6_IS_ADDR_UNSPECIFIED(&nexthops
[i
])) {
1706 if ((i
< if_count
) && ifindices
[i
])
1708 route_entry_nexthop_ipv6_ifindex_add(
1713 nexthop
= route_entry_nexthop_ipv6_add(
1714 re
, &nexthops
[i
], re
->vrf_id
);
1716 if (CHECK_FLAG(message
, ZAPI_MESSAGE_LABEL
))
1717 nexthop_add_labels(nexthop
, label_type
,
1720 if ((i
< if_count
) && ifindices
[i
])
1721 route_entry_nexthop_ifindex_add(
1722 re
, ifindices
[i
], re
->vrf_id
);
1728 if (CHECK_FLAG(message
, ZAPI_MESSAGE_DISTANCE
))
1729 STREAM_GETC(s
, re
->distance
);
1732 if (CHECK_FLAG(message
, ZAPI_MESSAGE_METRIC
))
1733 STREAM_GETL(s
, re
->metric
);
1736 if (CHECK_FLAG(message
, ZAPI_MESSAGE_TAG
))
1737 STREAM_GETL(s
, re
->tag
);
1741 if (CHECK_FLAG(message
, ZAPI_MESSAGE_MTU
))
1742 STREAM_GETL(s
, re
->mtu
);
1747 re
->table
= zvrf
->table_id
;
1749 ret
= rib_add_multipath(AFI_IP6
, safi
, &p
, NULL
, re
);
1752 client
->v4_route_add_cnt
++;
1754 client
->v4_route_upd8_cnt
++;
1759 nexthops_free(re
->ng
.nexthop
);
1760 XFREE(MTYPE_RE
, re
);
1764 static int zread_ipv6_add(struct zserv
*client
, u_short length
,
1765 struct zebra_vrf
*zvrf
)
1769 struct in6_addr nhop_addr
;
1771 struct route_entry
*re
;
1774 u_char nexthop_type
;
1776 struct prefix_ipv6 src_p
, *src_pp
;
1778 static struct in6_addr nexthops
[MULTIPATH_NUM
];
1779 static unsigned int ifindices
[MULTIPATH_NUM
];
1781 static mpls_label_t labels
[MULTIPATH_NUM
];
1782 enum lsp_types_t label_type
= ZEBRA_LSP_NONE
;
1784 struct nexthop
*nexthop
;
1785 enum blackhole_type bh_type
= BLACKHOLE_NULL
;
1787 /* Get input stream. */
1790 memset(&nhop_addr
, 0, sizeof(struct in6_addr
));
1792 /* Allocate new re. */
1793 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
1795 /* Type, flags, message. */
1796 STREAM_GETC(s
, re
->type
);
1797 if (re
->type
> ZEBRA_ROUTE_MAX
) {
1798 zlog_warn("%s: Specified route type: %d is not a legal value\n",
1799 __PRETTY_FUNCTION__
, re
->type
);
1800 XFREE(MTYPE_RE
, re
);
1803 STREAM_GETW(s
, re
->instance
);
1804 STREAM_GETL(s
, re
->flags
);
1805 STREAM_GETC(s
, message
);
1806 STREAM_GETW(s
, safi
);
1807 re
->uptime
= time(NULL
);
1810 memset(&p
, 0, sizeof(p
));
1811 p
.family
= AF_INET6
;
1812 STREAM_GETC(s
, p
.prefixlen
);
1813 if (p
.prefixlen
> IPV6_MAX_BITLEN
) {
1815 "%s: Specified prefix length %d is to large for v6 prefix",
1816 __PRETTY_FUNCTION__
, p
.prefixlen
);
1817 XFREE(MTYPE_RE
, re
);
1820 STREAM_GET(&p
.u
.prefix6
, s
, PSIZE(p
.prefixlen
));
1822 if (CHECK_FLAG(message
, ZAPI_MESSAGE_SRCPFX
)) {
1823 memset(&src_p
, 0, sizeof(src_p
));
1824 src_p
.family
= AF_INET6
;
1825 STREAM_GETC(s
, src_p
.prefixlen
);
1826 if (src_p
.prefixlen
> IPV6_MAX_BITLEN
) {
1828 "%s: Specified src prefix length %d is to large for v6 prefix",
1829 __PRETTY_FUNCTION__
, src_p
.prefixlen
);
1830 XFREE(MTYPE_RE
, re
);
1833 STREAM_GET(&src_p
.prefix
, s
, PSIZE(src_p
.prefixlen
));
1839 re
->vrf_id
= zvrf_id(zvrf
);
1841 /* We need to give nh-addr, nh-ifindex with the same next-hop object
1842 * to the re to ensure that IPv6 multipathing works; need to coalesce
1843 * these. Clients should send the same number of paired set of
1844 * next-hop-addr/next-hop-ifindices. */
1845 if (CHECK_FLAG(message
, ZAPI_MESSAGE_NEXTHOP
)) {
1846 unsigned int nh_count
= 0;
1847 unsigned int if_count
= 0;
1848 unsigned int max_nh_if
= 0;
1850 STREAM_GETC(s
, nexthop_num
);
1851 zserv_nexthop_num_warn(__func__
, (const struct prefix
*)&p
,
1854 if (CHECK_FLAG(message
, ZAPI_MESSAGE_LABEL
))
1855 label_type
= lsp_type_from_re_type(client
->proto
);
1857 for (i
= 0; i
< nexthop_num
; i
++) {
1858 STREAM_GETC(s
, nexthop_type
);
1860 switch (nexthop_type
) {
1861 case NEXTHOP_TYPE_IPV6
:
1862 STREAM_GET(&nhop_addr
, s
, 16);
1863 if (nh_count
< MULTIPATH_NUM
) {
1864 /* For labeled-unicast, each nexthop is
1865 * followed by label. */
1866 if (CHECK_FLAG(message
,
1867 ZAPI_MESSAGE_LABEL
)) {
1868 STREAM_GETL(s
, label
);
1869 labels
[nh_count
] = label
;
1871 nexthops
[nh_count
++] = nhop_addr
;
1874 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1875 STREAM_GET(&nhop_addr
, s
, 16);
1876 STREAM_GETL(s
, ifindex
);
1877 route_entry_nexthop_ipv6_ifindex_add(
1878 re
, &nhop_addr
, ifindex
, re
->vrf_id
);
1880 case NEXTHOP_TYPE_IFINDEX
:
1881 if (if_count
< multipath_num
) {
1882 STREAM_GETL(s
, ifindices
[if_count
++]);
1885 case NEXTHOP_TYPE_BLACKHOLE
:
1886 route_entry_nexthop_blackhole_add(re
, bh_type
);
1890 "%s: Please use ZEBRA_ROUTE_ADD if you want to pass non v6 nexthops",
1891 __PRETTY_FUNCTION__
);
1892 nexthops_free(re
->ng
.nexthop
);
1893 XFREE(MTYPE_RE
, re
);
1898 max_nh_if
= (nh_count
> if_count
) ? nh_count
: if_count
;
1899 for (i
= 0; i
< max_nh_if
; i
++) {
1901 && !IN6_IS_ADDR_UNSPECIFIED(&nexthops
[i
])) {
1902 if ((i
< if_count
) && ifindices
[i
])
1904 route_entry_nexthop_ipv6_ifindex_add(
1909 nexthop
= route_entry_nexthop_ipv6_add(
1910 re
, &nexthops
[i
], re
->vrf_id
);
1911 if (CHECK_FLAG(message
, ZAPI_MESSAGE_LABEL
))
1912 nexthop_add_labels(nexthop
, label_type
,
1915 if ((i
< if_count
) && ifindices
[i
])
1916 route_entry_nexthop_ifindex_add(
1917 re
, ifindices
[i
], re
->vrf_id
);
1923 if (CHECK_FLAG(message
, ZAPI_MESSAGE_DISTANCE
))
1924 STREAM_GETC(s
, re
->distance
);
1927 if (CHECK_FLAG(message
, ZAPI_MESSAGE_METRIC
))
1928 STREAM_GETL(s
, re
->metric
);
1931 if (CHECK_FLAG(message
, ZAPI_MESSAGE_TAG
))
1932 STREAM_GETL(s
, re
->tag
);
1936 if (CHECK_FLAG(message
, ZAPI_MESSAGE_MTU
))
1937 STREAM_GETL(s
, re
->mtu
);
1941 re
->table
= zvrf
->table_id
;
1943 ret
= rib_add_multipath(AFI_IP6
, safi
, &p
, src_pp
, re
);
1946 client
->v6_route_add_cnt
++;
1948 client
->v6_route_upd8_cnt
++;
1953 nexthops_free(re
->ng
.nexthop
);
1954 XFREE(MTYPE_RE
, re
);
1959 /* Zebra server IPv6 prefix delete function. */
1960 static int zread_ipv6_delete(struct zserv
*client
, u_short length
,
1961 struct zebra_vrf
*zvrf
)
1964 struct zapi_ipv6 api
;
1966 struct prefix_ipv6 src_p
, *src_pp
;
1970 /* Type, flags, message. */
1971 STREAM_GETC(s
, api
.type
);
1972 STREAM_GETW(s
, api
.instance
);
1973 STREAM_GETL(s
, api
.flags
);
1974 STREAM_GETC(s
, api
.message
);
1975 STREAM_GETW(s
, api
.safi
);
1978 memset(&p
, 0, sizeof(struct prefix
));
1979 p
.family
= AF_INET6
;
1980 STREAM_GETC(s
, p
.prefixlen
);
1981 STREAM_GET(&p
.u
.prefix6
, s
, PSIZE(p
.prefixlen
));
1983 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
)) {
1984 memset(&src_p
, 0, sizeof(struct prefix_ipv6
));
1985 src_p
.family
= AF_INET6
;
1986 STREAM_GETC(s
, src_p
.prefixlen
);
1987 STREAM_GET(&src_p
.prefix
, s
, PSIZE(src_p
.prefixlen
));
1992 rib_delete(AFI_IP6
, api
.safi
, zvrf_id(zvrf
), api
.type
, api
.instance
,
1993 api
.flags
, &p
, src_pp
, NULL
, client
->rtm_table
, 0, false,
1996 client
->v6_route_del_cnt
++;
2002 /* Register zebra server router-id information. Send current router-id */
2003 static int zread_router_id_add(struct zserv
*client
, u_short length
,
2004 struct zebra_vrf
*zvrf
)
2008 /* Router-id information is needed. */
2009 vrf_bitmap_set(client
->ridinfo
, zvrf_id(zvrf
));
2011 router_id_get(&p
, zvrf_id(zvrf
));
2013 return zsend_router_id_update(client
, &p
, zvrf_id(zvrf
));
2016 /* Unregister zebra server router-id information. */
2017 static int zread_router_id_delete(struct zserv
*client
, u_short length
,
2018 struct zebra_vrf
*zvrf
)
2020 vrf_bitmap_unset(client
->ridinfo
, zvrf_id(zvrf
));
2024 /* Tie up route-type and client->sock */
2025 static void zread_hello(struct zserv
*client
)
2027 /* type of protocol (lib/zebra.h) */
2032 STREAM_GETC(client
->ibuf
, proto
);
2033 STREAM_GETW(client
->ibuf
, instance
);
2034 STREAM_GETC(client
->ibuf
, notify
);
2036 client
->notify_owner
= true;
2038 /* accept only dynamic routing protocols */
2039 if ((proto
< ZEBRA_ROUTE_MAX
) && (proto
> ZEBRA_ROUTE_STATIC
)) {
2041 "client %d says hello and bids fair to announce only %s routes",
2042 client
->sock
, zebra_route_string(proto
));
2044 zlog_notice("client protocol instance %d", instance
);
2046 client
->proto
= proto
;
2047 client
->instance
= instance
;
2054 /* Unregister all information in a VRF. */
2055 static int zread_vrf_unregister(struct zserv
*client
, u_short length
,
2056 struct zebra_vrf
*zvrf
)
2061 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
2062 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
2063 vrf_bitmap_unset(client
->redist
[afi
][i
], zvrf_id(zvrf
));
2064 vrf_bitmap_unset(client
->redist_default
, zvrf_id(zvrf
));
2065 vrf_bitmap_unset(client
->ifinfo
, zvrf_id(zvrf
));
2066 vrf_bitmap_unset(client
->ridinfo
, zvrf_id(zvrf
));
2071 static void zread_mpls_labels(int command
, struct zserv
*client
, u_short length
,
2072 struct zebra_vrf
*zvrf
)
2075 enum lsp_types_t type
;
2076 struct prefix prefix
;
2077 enum nexthop_types_t gtype
;
2080 mpls_label_t in_label
, out_label
;
2083 /* Get input stream. */
2087 STREAM_GETC(s
, type
);
2088 STREAM_GETL(s
, prefix
.family
);
2089 switch (prefix
.family
) {
2091 STREAM_GET(&prefix
.u
.prefix4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
2092 STREAM_GETC(s
, prefix
.prefixlen
);
2093 if (prefix
.prefixlen
> IPV4_MAX_BITLEN
) {
2095 "%s: Specified prefix length %d is greater than a v4 address can support",
2096 __PRETTY_FUNCTION__
, prefix
.prefixlen
);
2099 STREAM_GET(&gate
.ipv4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
2102 STREAM_GET(&prefix
.u
.prefix6
, s
, 16);
2103 STREAM_GETC(s
, prefix
.prefixlen
);
2104 if (prefix
.prefixlen
> IPV6_MAX_BITLEN
) {
2106 "%s: Specified prefix length %d is greater than a v6 address can support",
2107 __PRETTY_FUNCTION__
, prefix
.prefixlen
);
2110 STREAM_GET(&gate
.ipv6
, s
, 16);
2113 zlog_warn("%s: Specified AF %d is not supported for this call",
2114 __PRETTY_FUNCTION__
, prefix
.family
);
2117 STREAM_GETL(s
, ifindex
);
2118 STREAM_GETC(s
, distance
);
2119 STREAM_GETL(s
, in_label
);
2120 STREAM_GETL(s
, out_label
);
2122 switch (prefix
.family
) {
2125 gtype
= NEXTHOP_TYPE_IPV4_IFINDEX
;
2127 gtype
= NEXTHOP_TYPE_IPV4
;
2131 gtype
= NEXTHOP_TYPE_IPV6_IFINDEX
;
2133 gtype
= NEXTHOP_TYPE_IPV6
;
2142 if (command
== ZEBRA_MPLS_LABELS_ADD
) {
2143 mpls_lsp_install(zvrf
, type
, in_label
, out_label
, gtype
, &gate
,
2145 mpls_ftn_update(1, zvrf
, type
, &prefix
, gtype
, &gate
, ifindex
,
2146 distance
, out_label
);
2147 } else if (command
== ZEBRA_MPLS_LABELS_DELETE
) {
2148 mpls_lsp_uninstall(zvrf
, type
, in_label
, gtype
, &gate
, ifindex
);
2149 mpls_ftn_update(0, zvrf
, type
, &prefix
, gtype
, &gate
, ifindex
,
2150 distance
, out_label
);
2155 /* Send response to a label manager connect request to client */
2156 static int zsend_label_manager_connect_response(struct zserv
*client
,
2157 vrf_id_t vrf_id
, u_short result
)
2164 zclient_create_header(s
, ZEBRA_LABEL_MANAGER_CONNECT
, vrf_id
);
2167 stream_putc(s
, result
);
2169 /* Write packet size. */
2170 stream_putw_at(s
, 0, stream_get_endp(s
));
2172 return writen(client
->sock
, s
->data
, stream_get_endp(s
));
2175 static void zread_label_manager_connect(struct zserv
*client
, vrf_id_t vrf_id
)
2178 /* type of protocol (lib/zebra.h) */
2182 /* Get input stream. */
2186 STREAM_GETC(s
, proto
);
2187 STREAM_GETW(s
, instance
);
2189 /* accept only dynamic routing protocols */
2190 if ((proto
>= ZEBRA_ROUTE_MAX
) || (proto
<= ZEBRA_ROUTE_STATIC
)) {
2191 zlog_err("client %d has wrong protocol %s", client
->sock
,
2192 zebra_route_string(proto
));
2193 zsend_label_manager_connect_response(client
, vrf_id
, 1);
2196 zlog_notice("client %d with instance %u connected as %s", client
->sock
,
2197 instance
, zebra_route_string(proto
));
2198 client
->proto
= proto
;
2199 client
->instance
= instance
;
2202 Release previous labels of same protocol and instance.
2203 This is done in case it restarted from an unexpected shutdown.
2205 release_daemon_chunks(proto
, instance
);
2208 " Label Manager client connected: sock %d, proto %s, instance %u",
2209 client
->sock
, zebra_route_string(proto
), instance
);
2210 /* send response back */
2211 zsend_label_manager_connect_response(client
, vrf_id
, 0);
2216 /* Send response to a get label chunk request to client */
2217 static int zsend_assign_label_chunk_response(struct zserv
*client
,
2219 struct label_manager_chunk
*lmc
)
2226 zclient_create_header(s
, ZEBRA_GET_LABEL_CHUNK
, vrf_id
);
2230 stream_putc(s
, lmc
->keep
);
2231 /* start and end labels */
2232 stream_putl(s
, lmc
->start
);
2233 stream_putl(s
, lmc
->end
);
2236 /* Write packet size. */
2237 stream_putw_at(s
, 0, stream_get_endp(s
));
2239 return writen(client
->sock
, s
->data
, stream_get_endp(s
));
2242 static void zread_get_label_chunk(struct zserv
*client
, vrf_id_t vrf_id
)
2247 struct label_manager_chunk
*lmc
;
2249 /* Get input stream. */
2253 STREAM_GETC(s
, keep
);
2254 STREAM_GETL(s
, size
);
2256 lmc
= assign_label_chunk(client
->proto
, client
->instance
, keep
, size
);
2258 zlog_err("%s: Unable to assign Label Chunk of size %u",
2261 zlog_debug("Assigned Label Chunk %u - %u to %u", lmc
->start
,
2263 /* send response back */
2264 zsend_assign_label_chunk_response(client
, vrf_id
, lmc
);
2270 static void zread_release_label_chunk(struct zserv
*client
)
2273 uint32_t start
, end
;
2275 /* Get input stream. */
2279 STREAM_GETL(s
, start
);
2280 STREAM_GETL(s
, end
);
2282 release_label_chunk(client
->proto
, client
->instance
, start
, end
);
2287 static void zread_label_manager_request(int cmd
, struct zserv
*client
,
2288 struct zebra_vrf
*zvrf
)
2290 /* to avoid sending other messages like ZERBA_INTERFACE_UP */
2291 if (cmd
== ZEBRA_LABEL_MANAGER_CONNECT
)
2292 client
->is_synchronous
= 1;
2294 /* external label manager */
2296 zread_relay_label_manager_request(cmd
, client
, zvrf_id(zvrf
));
2297 /* this is a label manager */
2299 if (cmd
== ZEBRA_LABEL_MANAGER_CONNECT
)
2300 zread_label_manager_connect(client
, zvrf_id(zvrf
));
2302 /* Sanity: don't allow 'unidentified' requests */
2303 if (!client
->proto
) {
2305 "Got label request from an unidentified client");
2308 if (cmd
== ZEBRA_GET_LABEL_CHUNK
)
2309 zread_get_label_chunk(client
, zvrf_id(zvrf
));
2310 else if (cmd
== ZEBRA_RELEASE_LABEL_CHUNK
)
2311 zread_release_label_chunk(client
);
2316 static int zread_pseudowire(int command
, struct zserv
*client
, u_short length
,
2317 struct zebra_vrf
*zvrf
)
2320 char ifname
[IF_NAMESIZE
];
2324 union g_addr nexthop
;
2325 uint32_t local_label
;
2326 uint32_t remote_label
;
2328 union pw_protocol_fields data
;
2330 struct zebra_pw
*pw
;
2332 /* Get input stream. */
2336 STREAM_GET(ifname
, s
, IF_NAMESIZE
);
2337 STREAM_GETL(s
, ifindex
);
2338 STREAM_GETL(s
, type
);
2342 STREAM_GET(&nexthop
.ipv4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
2345 STREAM_GET(&nexthop
.ipv6
, s
, 16);
2350 STREAM_GETL(s
, local_label
);
2351 STREAM_GETL(s
, remote_label
);
2352 STREAM_GETC(s
, flags
);
2353 STREAM_GET(&data
, s
, sizeof(data
));
2354 protocol
= client
->proto
;
2356 pw
= zebra_pw_find(zvrf
, ifname
);
2360 zlog_warn("%s: pseudowire %s already exists [%s]",
2362 zserv_command_string(command
));
2366 zebra_pw_add(zvrf
, ifname
, protocol
, client
);
2368 case ZEBRA_PW_DELETE
:
2370 zlog_warn("%s: pseudowire %s not found [%s]", __func__
,
2371 ifname
, zserv_command_string(command
));
2375 zebra_pw_del(zvrf
, pw
);
2378 case ZEBRA_PW_UNSET
:
2380 zlog_warn("%s: pseudowire %s not found [%s]", __func__
,
2381 ifname
, zserv_command_string(command
));
2389 case ZEBRA_PW_UNSET
:
2394 zebra_pw_change(pw
, ifindex
, type
, af
, &nexthop
, local_label
,
2395 remote_label
, flags
, &data
);
2403 /* Cleanup registered nexthops (across VRFs) upon client disconnect. */
2404 static void zebra_client_close_cleanup_rnh(struct zserv
*client
)
2407 struct zebra_vrf
*zvrf
;
2409 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
2410 if ((zvrf
= vrf
->info
) != NULL
) {
2411 zebra_cleanup_rnh_client(zvrf_id(zvrf
), AF_INET
, client
,
2413 zebra_cleanup_rnh_client(zvrf_id(zvrf
), AF_INET6
,
2414 client
, RNH_NEXTHOP_TYPE
);
2415 zebra_cleanup_rnh_client(zvrf_id(zvrf
), AF_INET
, client
,
2416 RNH_IMPORT_CHECK_TYPE
);
2417 zebra_cleanup_rnh_client(zvrf_id(zvrf
), AF_INET6
,
2418 client
, RNH_IMPORT_CHECK_TYPE
);
2419 if (client
->proto
== ZEBRA_ROUTE_LDP
) {
2420 hash_iterate(zvrf
->lsp_table
,
2421 mpls_ldp_lsp_uninstall_all
,
2423 mpls_ldp_ftn_uninstall_all(zvrf
, AFI_IP
);
2424 mpls_ldp_ftn_uninstall_all(zvrf
, AFI_IP6
);
2430 /* free zebra client information. */
2431 static void zebra_client_free(struct zserv
*client
)
2433 /* Send client de-registration to BFD */
2434 zebra_ptm_bfd_client_deregister(client
->proto
);
2436 /* Cleanup any registered nexthops - across all VRFs. */
2437 zebra_client_close_cleanup_rnh(client
);
2439 /* Release Label Manager chunks */
2440 release_daemon_chunks(client
->proto
, client
->instance
);
2442 /* Cleanup any FECs registered by this client. */
2443 zebra_mpls_cleanup_fecs_for_client(vrf_info_lookup(VRF_DEFAULT
),
2446 /* Remove pseudowires associated with this client */
2447 zebra_pw_client_close(client
);
2449 /* Close file descriptor. */
2451 unsigned long nroutes
;
2453 close(client
->sock
);
2454 nroutes
= rib_score_proto(client
->proto
, client
->instance
);
2456 "client %d disconnected. %lu %s routes removed from the rib",
2457 client
->sock
, nroutes
,
2458 zebra_route_string(client
->proto
));
2462 /* Free stream buffers. */
2464 stream_free(client
->ibuf
);
2466 stream_free(client
->obuf
);
2468 buffer_free(client
->wb
);
2470 /* Release threads. */
2472 thread_cancel(client
->t_read
);
2473 if (client
->t_write
)
2474 thread_cancel(client
->t_write
);
2475 if (client
->t_suicide
)
2476 thread_cancel(client
->t_suicide
);
2479 for (afi_t afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
2480 for (int i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
2481 vrf_bitmap_free(client
->redist
[afi
][i
]);
2483 vrf_bitmap_free(client
->redist_default
);
2484 vrf_bitmap_free(client
->ifinfo
);
2485 vrf_bitmap_free(client
->ridinfo
);
2487 XFREE(MTYPE_TMP
, client
);
2490 static void zebra_client_close(struct zserv
*client
)
2492 listnode_delete(zebrad
.client_list
, client
);
2493 zebra_client_free(client
);
2496 /* Make new client. */
2497 static void zebra_client_create(int sock
)
2499 struct zserv
*client
;
2503 client
= XCALLOC(MTYPE_TMP
, sizeof(struct zserv
));
2505 /* Make client input/output buffer. */
2506 client
->sock
= sock
;
2507 client
->ibuf
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
2508 client
->obuf
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
2509 client
->wb
= buffer_new(0);
2511 /* Set table number. */
2512 client
->rtm_table
= zebrad
.rtm_table_default
;
2514 client
->connect_time
= monotime(NULL
);
2515 /* Initialize flags */
2516 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
2517 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
2518 client
->redist
[afi
][i
] = vrf_bitmap_init();
2519 client
->redist_default
= vrf_bitmap_init();
2520 client
->ifinfo
= vrf_bitmap_init();
2521 client
->ridinfo
= vrf_bitmap_init();
2523 /* by default, it's not a synchronous client */
2524 client
->is_synchronous
= 0;
2526 /* Add this client to linked list. */
2527 listnode_add(zebrad
.client_list
, client
);
2529 /* Make new read thread. */
2530 zebra_event(ZEBRA_READ
, sock
, client
);
2532 zebra_vrf_update_all(client
);
2535 static int zread_interface_set_master(struct zserv
*client
, u_short length
)
2537 struct interface
*master
;
2538 struct interface
*slave
;
2539 struct stream
*s
= client
->ibuf
;
2543 STREAM_GETL(s
, vrf_id
);
2544 STREAM_GETL(s
, ifindex
);
2545 master
= if_lookup_by_index(ifindex
, vrf_id
);
2547 STREAM_GETL(s
, vrf_id
);
2548 STREAM_GETL(s
, ifindex
);
2549 slave
= if_lookup_by_index(ifindex
, vrf_id
);
2551 if (!master
|| !slave
)
2554 kernel_interface_set_master(master
, slave
);
2561 static void zread_vrf_label(struct zserv
*client
, struct zebra_vrf
*zvrf
)
2563 struct interface
*ifp
;
2564 mpls_label_t nlabel
;
2567 struct zebra_vrf
*def_zvrf
;
2568 enum lsp_types_t ltype
;
2571 STREAM_GETL(s
, nlabel
);
2572 STREAM_GETC(s
, afi
);
2573 if (nlabel
== zvrf
->label
[afi
]) {
2575 * Nothing to do here move along
2580 STREAM_GETC(s
, ltype
);
2582 if (zvrf
->vrf
->vrf_id
!= VRF_DEFAULT
)
2583 ifp
= if_lookup_by_name(zvrf
->vrf
->name
, zvrf
->vrf
->vrf_id
);
2585 ifp
= if_lookup_by_name("lo", VRF_DEFAULT
);
2588 zlog_debug("Unable to find specified Interface for %s",
2593 def_zvrf
= zebra_vrf_lookup_by_id(VRF_DEFAULT
);
2595 if (zvrf
->label
[afi
] != MPLS_LABEL_NONE
) {
2599 really_remove
= true;
2600 for (scrubber
= AFI_IP
; scrubber
< AFI_MAX
; scrubber
++) {
2601 if (scrubber
== afi
)
2604 if (zvrf
->label
[scrubber
] == MPLS_LABEL_NONE
)
2607 if (zvrf
->label
[afi
] == zvrf
->label
[scrubber
]) {
2608 really_remove
= false;
2614 mpls_lsp_uninstall(def_zvrf
, ltype
, zvrf
->label
[afi
],
2615 NEXTHOP_TYPE_IFINDEX
, NULL
,
2619 if (nlabel
!= MPLS_LABEL_NONE
)
2620 mpls_lsp_install(def_zvrf
, ltype
, nlabel
,
2621 MPLS_LABEL_IMPLICIT_NULL
, NEXTHOP_TYPE_IFINDEX
,
2622 NULL
, ifp
->ifindex
);
2624 zvrf
->label
[afi
] = nlabel
;
2629 static inline void zread_rule(uint16_t command
, struct zserv
*client
,
2630 uint16_t length
, struct zebra_vrf
*zvrf
)
2632 struct zebra_pbr_rule zpr
;
2638 STREAM_GETL(s
, total
);
2640 for (i
= 0; i
< total
; i
++) {
2641 memset(&zpr
, 0, sizeof(zpr
));
2643 zpr
.sock
= client
->sock
;
2644 STREAM_GETL(s
, zpr
.seq
);
2645 STREAM_GETL(s
, zpr
.priority
);
2646 STREAM_GETL(s
, zpr
.unique
);
2647 STREAM_GETC(s
, zpr
.filter
.src_ip
.family
);
2648 STREAM_GETC(s
, zpr
.filter
.src_ip
.prefixlen
);
2649 STREAM_GET(&zpr
.filter
.src_ip
.u
.prefix
, s
,
2650 prefix_blen(&zpr
.filter
.src_ip
));
2651 STREAM_GETW(s
, zpr
.filter
.src_port
);
2652 STREAM_GETC(s
, zpr
.filter
.dst_ip
.family
);
2653 STREAM_GETC(s
, zpr
.filter
.dst_ip
.prefixlen
);
2654 STREAM_GET(&zpr
.filter
.dst_ip
.u
.prefix
, s
,
2655 prefix_blen(&zpr
.filter
.dst_ip
));
2656 STREAM_GETW(s
, zpr
.filter
.dst_port
);
2657 STREAM_GETL(s
, zpr
.action
.table
);
2658 STREAM_GETL(s
, ifindex
);
2660 zpr
.ifp
= if_lookup_by_index(ifindex
, VRF_UNKNOWN
);
2662 zlog_debug("FAiled to lookup ifindex: %u", ifindex
);
2666 if (!is_default_prefix(&zpr
.filter
.src_ip
))
2667 zpr
.filter
.filter_bm
|= PBR_FILTER_SRC_IP
;
2669 if (!is_default_prefix(&zpr
.filter
.dst_ip
))
2670 zpr
.filter
.filter_bm
|= PBR_FILTER_DST_IP
;
2672 if (zpr
.filter
.src_port
)
2673 zpr
.filter
.filter_bm
|= PBR_FILTER_SRC_PORT
;
2675 if (zpr
.filter
.dst_port
)
2676 zpr
.filter
.filter_bm
|= PBR_FILTER_DST_PORT
;
2678 if (command
== ZEBRA_RULE_ADD
)
2679 zebra_pbr_add_rule(zvrf
->zns
, &zpr
);
2681 zebra_pbr_del_rule(zvrf
->zns
, &zpr
);
2688 static inline void zserv_handle_commands(struct zserv
*client
, uint16_t command
,
2690 struct zebra_vrf
*zvrf
)
2693 case ZEBRA_ROUTER_ID_ADD
:
2694 zread_router_id_add(client
, length
, zvrf
);
2696 case ZEBRA_ROUTER_ID_DELETE
:
2697 zread_router_id_delete(client
, length
, zvrf
);
2699 case ZEBRA_INTERFACE_ADD
:
2700 zread_interface_add(client
, length
, zvrf
);
2702 case ZEBRA_INTERFACE_DELETE
:
2703 zread_interface_delete(client
, length
, zvrf
);
2705 case ZEBRA_ROUTE_ADD
:
2706 zread_route_add(client
, length
, zvrf
);
2708 case ZEBRA_ROUTE_DELETE
:
2709 zread_route_del(client
, length
, zvrf
);
2711 case ZEBRA_IPV4_ROUTE_ADD
:
2712 zread_ipv4_add(client
, length
, zvrf
);
2714 case ZEBRA_IPV4_ROUTE_DELETE
:
2715 zread_ipv4_delete(client
, length
, zvrf
);
2717 case ZEBRA_IPV4_ROUTE_IPV6_NEXTHOP_ADD
:
2718 zread_ipv4_route_ipv6_nexthop_add(client
, length
, zvrf
);
2720 case ZEBRA_IPV6_ROUTE_ADD
:
2721 zread_ipv6_add(client
, length
, zvrf
);
2723 case ZEBRA_IPV6_ROUTE_DELETE
:
2724 zread_ipv6_delete(client
, length
, zvrf
);
2726 case ZEBRA_REDISTRIBUTE_ADD
:
2727 zebra_redistribute_add(command
, client
, length
, zvrf
);
2729 case ZEBRA_REDISTRIBUTE_DELETE
:
2730 zebra_redistribute_delete(command
, client
, length
, zvrf
);
2732 case ZEBRA_REDISTRIBUTE_DEFAULT_ADD
:
2733 zebra_redistribute_default_add(command
, client
, length
, zvrf
);
2735 case ZEBRA_REDISTRIBUTE_DEFAULT_DELETE
:
2736 zebra_redistribute_default_delete(command
, client
, length
,
2739 case ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB
:
2740 zread_ipv4_nexthop_lookup_mrib(client
, length
, zvrf
);
2743 zread_hello(client
);
2745 case ZEBRA_NEXTHOP_REGISTER
:
2746 zserv_rnh_register(client
, length
, RNH_NEXTHOP_TYPE
, zvrf
);
2748 case ZEBRA_NEXTHOP_UNREGISTER
:
2749 zserv_rnh_unregister(client
, length
, RNH_NEXTHOP_TYPE
, zvrf
);
2751 case ZEBRA_IMPORT_ROUTE_REGISTER
:
2752 zserv_rnh_register(client
, length
, RNH_IMPORT_CHECK_TYPE
, zvrf
);
2754 case ZEBRA_IMPORT_ROUTE_UNREGISTER
:
2755 zserv_rnh_unregister(client
, length
, RNH_IMPORT_CHECK_TYPE
,
2758 case ZEBRA_BFD_DEST_UPDATE
:
2759 case ZEBRA_BFD_DEST_REGISTER
:
2760 zebra_ptm_bfd_dst_register(client
, length
, command
, zvrf
);
2762 case ZEBRA_BFD_DEST_DEREGISTER
:
2763 zebra_ptm_bfd_dst_deregister(client
, length
, zvrf
);
2765 case ZEBRA_VRF_UNREGISTER
:
2766 zread_vrf_unregister(client
, length
, zvrf
);
2768 case ZEBRA_VRF_LABEL
:
2769 zread_vrf_label(client
, zvrf
);
2771 case ZEBRA_BFD_CLIENT_REGISTER
:
2772 zebra_ptm_bfd_client_register(client
, length
);
2774 case ZEBRA_INTERFACE_ENABLE_RADV
:
2775 #if defined(HAVE_RTADV)
2776 zebra_interface_radv_set(client
, length
, zvrf
, 1);
2779 case ZEBRA_INTERFACE_DISABLE_RADV
:
2780 #if defined(HAVE_RTADV)
2781 zebra_interface_radv_set(client
, length
, zvrf
, 0);
2784 case ZEBRA_MPLS_LABELS_ADD
:
2785 case ZEBRA_MPLS_LABELS_DELETE
:
2786 zread_mpls_labels(command
, client
, length
, zvrf
);
2788 case ZEBRA_IPMR_ROUTE_STATS
:
2789 zebra_ipmr_route_stats(client
, length
, zvrf
);
2791 case ZEBRA_LABEL_MANAGER_CONNECT
:
2792 case ZEBRA_GET_LABEL_CHUNK
:
2793 case ZEBRA_RELEASE_LABEL_CHUNK
:
2794 zread_label_manager_request(command
, client
, zvrf
);
2796 case ZEBRA_FEC_REGISTER
:
2797 zserv_fec_register(client
, length
);
2799 case ZEBRA_FEC_UNREGISTER
:
2800 zserv_fec_unregister(client
, length
);
2802 case ZEBRA_ADVERTISE_DEFAULT_GW
:
2803 zebra_vxlan_advertise_gw_macip(client
, length
, zvrf
);
2805 case ZEBRA_ADVERTISE_SUBNET
:
2806 zebra_vxlan_advertise_subnet(client
, length
, zvrf
);
2808 case ZEBRA_ADVERTISE_ALL_VNI
:
2809 zebra_vxlan_advertise_all_vni(client
, length
, zvrf
);
2811 case ZEBRA_REMOTE_VTEP_ADD
:
2812 zebra_vxlan_remote_vtep_add(client
, length
, zvrf
);
2814 case ZEBRA_REMOTE_VTEP_DEL
:
2815 zebra_vxlan_remote_vtep_del(client
, length
, zvrf
);
2817 case ZEBRA_REMOTE_MACIP_ADD
:
2818 zebra_vxlan_remote_macip_add(client
, length
, zvrf
);
2820 case ZEBRA_REMOTE_MACIP_DEL
:
2821 zebra_vxlan_remote_macip_del(client
, length
, zvrf
);
2823 case ZEBRA_INTERFACE_SET_MASTER
:
2824 zread_interface_set_master(client
, length
);
2827 case ZEBRA_PW_DELETE
:
2829 case ZEBRA_PW_UNSET
:
2830 zread_pseudowire(command
, client
, length
, zvrf
);
2832 case ZEBRA_RULE_ADD
:
2833 case ZEBRA_RULE_DELETE
:
2834 zread_rule(command
, client
, length
, zvrf
);
2837 zlog_info("Zebra received unknown command %d", command
);
2842 #if defined(HANDLE_ZAPI_FUZZING)
2843 static void zserv_write_incoming(struct stream
*orig
, uint16_t command
)
2845 char fname
[MAXPATHLEN
];
2846 struct stream
*copy
;
2849 copy
= stream_dup(orig
);
2850 stream_set_getp(copy
, 0);
2852 zserv_privs
.change(ZPRIVS_RAISE
);
2853 snprintf(fname
, MAXPATHLEN
, "%s/%u", DAEMON_VTY_DIR
, command
);
2854 fd
= open(fname
, O_CREAT
| O_WRONLY
| O_EXCL
, 0644);
2855 stream_flush(copy
, fd
);
2857 zserv_privs
.change(ZPRIVS_LOWER
);
2862 /* Handler of zebra service request. */
2863 static int zebra_client_read(struct thread
*thread
)
2866 struct zserv
*client
;
2868 uint16_t length
, command
;
2869 uint8_t marker
, version
;
2871 struct zebra_vrf
*zvrf
;
2872 #if defined(HANDLE_ZAPI_FUZZING)
2875 int packets
= zebrad
.packets_to_process
;
2878 /* Get thread data. Reset reading thread because I'm running. */
2879 sock
= THREAD_FD(thread
);
2880 client
= THREAD_ARG(thread
);
2881 client
->t_read
= NULL
;
2883 if (client
->t_suicide
) {
2884 zebra_client_close(client
);
2889 /* Read length and command (if we don't have it already). */
2890 if ((already
= stream_get_endp(client
->ibuf
))
2891 < ZEBRA_HEADER_SIZE
) {
2893 if (((nbyte
= stream_read_try(client
->ibuf
, sock
,
2898 if (IS_ZEBRA_DEBUG_EVENT
)
2900 "connection closed socket [%d]",
2902 zebra_client_close(client
);
2905 if (nbyte
!= (ssize_t
)(ZEBRA_HEADER_SIZE
- already
)) {
2906 /* Try again later. */
2907 zebra_event(ZEBRA_READ
, sock
, client
);
2910 already
= ZEBRA_HEADER_SIZE
;
2913 /* Reset to read from the beginning of the incoming packet. */
2914 stream_set_getp(client
->ibuf
, 0);
2916 /* Fetch header values */
2917 STREAM_GETW(client
->ibuf
, length
);
2918 STREAM_GETC(client
->ibuf
, marker
);
2919 STREAM_GETC(client
->ibuf
, version
);
2920 STREAM_GETL(client
->ibuf
, vrf_id
);
2921 STREAM_GETW(client
->ibuf
, command
);
2923 if (marker
!= ZEBRA_HEADER_MARKER
|| version
!= ZSERV_VERSION
) {
2925 "%s: socket %d version mismatch, marker %d, version %d",
2926 __func__
, sock
, marker
, version
);
2927 zebra_client_close(client
);
2930 if (length
< ZEBRA_HEADER_SIZE
) {
2932 "%s: socket %d message length %u is less than header size %d",
2933 __func__
, sock
, length
, ZEBRA_HEADER_SIZE
);
2934 zebra_client_close(client
);
2937 if (length
> STREAM_SIZE(client
->ibuf
)) {
2939 "%s: socket %d message length %u exceeds buffer size %lu",
2940 __func__
, sock
, length
,
2941 (u_long
)STREAM_SIZE(client
->ibuf
));
2942 zebra_client_close(client
);
2946 /* Read rest of data. */
2947 if (already
< length
) {
2949 if (((nbyte
= stream_read_try(client
->ibuf
, sock
,
2953 if (IS_ZEBRA_DEBUG_EVENT
)
2955 "connection closed [%d] when reading zebra data",
2957 zebra_client_close(client
);
2960 if (nbyte
!= (ssize_t
)(length
- already
)) {
2961 /* Try again later. */
2962 zebra_event(ZEBRA_READ
, sock
, client
);
2967 #if defined(HANDLE_ZAPI_FUZZING)
2968 zserv_write_incoming(client
->ibuf
, command
);
2970 length
-= ZEBRA_HEADER_SIZE
;
2972 /* Debug packet information. */
2973 if (IS_ZEBRA_DEBUG_EVENT
)
2974 zlog_debug("zebra message comes from socket [%d]",
2977 if (IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
)
2978 zlog_debug("zebra message received [%s] %d in VRF %u",
2979 zserv_command_string(command
), length
,
2982 client
->last_read_time
= monotime(NULL
);
2983 client
->last_read_cmd
= command
;
2985 zvrf
= zebra_vrf_lookup_by_id(vrf_id
);
2987 if (IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
)
2988 zlog_debug("zebra received unknown VRF[%u]",
2990 goto zclient_read_out
;
2993 zserv_handle_commands(client
, command
, length
, zvrf
);
2995 if (client
->t_suicide
) {
2996 /* No need to wait for thread callback, just kill
2999 zebra_client_close(client
);
3003 stream_reset(client
->ibuf
);
3008 stream_reset(client
->ibuf
);
3009 zebra_event(ZEBRA_READ
, sock
, client
);
3014 /* Accept code of zebra server socket. */
3015 static int zebra_accept(struct thread
*thread
)
3019 struct sockaddr_in client
;
3022 accept_sock
= THREAD_FD(thread
);
3024 /* Reregister myself. */
3025 zebra_event(ZEBRA_SERV
, accept_sock
, NULL
);
3027 len
= sizeof(struct sockaddr_in
);
3028 client_sock
= accept(accept_sock
, (struct sockaddr
*)&client
, &len
);
3030 if (client_sock
< 0) {
3031 zlog_warn("Can't accept zebra socket: %s",
3032 safe_strerror(errno
));
3036 /* Make client socket non-blocking. */
3037 set_nonblocking(client_sock
);
3039 /* Create new zebra client. */
3040 zebra_client_create(client_sock
);
3045 /* Make zebra server socket, wiping any existing one (see bug #403). */
3046 void zebra_zserv_socket_init(char *path
)
3051 struct sockaddr_storage sa
;
3054 if (!frr_zclient_addr(&sa
, &sa_len
, path
))
3055 /* should be caught in zebra main() */
3059 old_mask
= umask(0077);
3061 /* Make UNIX domain socket. */
3062 sock
= socket(sa
.ss_family
, SOCK_STREAM
, 0);
3064 zlog_warn("Can't create zserv socket: %s",
3065 safe_strerror(errno
));
3067 "zebra can't provide full functionality due to above error");
3071 if (sa
.ss_family
!= AF_UNIX
) {
3072 sockopt_reuseaddr(sock
);
3073 sockopt_reuseport(sock
);
3075 struct sockaddr_un
*suna
= (struct sockaddr_un
*)&sa
;
3076 if (suna
->sun_path
[0])
3077 unlink(suna
->sun_path
);
3080 zserv_privs
.change(ZPRIVS_RAISE
);
3081 setsockopt_so_recvbuf(sock
, 1048576);
3082 setsockopt_so_sendbuf(sock
, 1048576);
3083 zserv_privs
.change(ZPRIVS_LOWER
);
3085 if (sa
.ss_family
!= AF_UNIX
&& zserv_privs
.change(ZPRIVS_RAISE
))
3086 zlog_err("Can't raise privileges");
3088 ret
= bind(sock
, (struct sockaddr
*)&sa
, sa_len
);
3090 zlog_warn("Can't bind zserv socket on %s: %s", path
,
3091 safe_strerror(errno
));
3093 "zebra can't provide full functionality due to above error");
3097 if (sa
.ss_family
!= AF_UNIX
&& zserv_privs
.change(ZPRIVS_LOWER
))
3098 zlog_err("Can't lower privileges");
3100 ret
= listen(sock
, 5);
3102 zlog_warn("Can't listen to zserv socket %s: %s", path
,
3103 safe_strerror(errno
));
3105 "zebra can't provide full functionality due to above error");
3112 zebra_event(ZEBRA_SERV
, sock
, NULL
);
3116 static void zebra_event(enum event event
, int sock
, struct zserv
*client
)
3120 thread_add_read(zebrad
.master
, zebra_accept
, client
, sock
,
3124 client
->t_read
= NULL
;
3125 thread_add_read(zebrad
.master
, zebra_client_read
, client
, sock
,
3134 #define ZEBRA_TIME_BUF 32
3135 static char *zserv_time_buf(time_t *time1
, char *buf
, int buflen
)
3140 assert(buf
!= NULL
);
3141 assert(buflen
>= ZEBRA_TIME_BUF
);
3142 assert(time1
!= NULL
);
3145 snprintf(buf
, buflen
, "never ");
3149 now
= monotime(NULL
);
3153 if (now
< ONE_DAY_SECOND
)
3154 snprintf(buf
, buflen
, "%02d:%02d:%02d", tm
->tm_hour
, tm
->tm_min
,
3156 else if (now
< ONE_WEEK_SECOND
)
3157 snprintf(buf
, buflen
, "%dd%02dh%02dm", tm
->tm_yday
, tm
->tm_hour
,
3160 snprintf(buf
, buflen
, "%02dw%dd%02dh", tm
->tm_yday
/ 7,
3161 tm
->tm_yday
- ((tm
->tm_yday
/ 7) * 7), tm
->tm_hour
);
3165 static void zebra_show_client_detail(struct vty
*vty
, struct zserv
*client
)
3167 char cbuf
[ZEBRA_TIME_BUF
], rbuf
[ZEBRA_TIME_BUF
];
3168 char wbuf
[ZEBRA_TIME_BUF
], nhbuf
[ZEBRA_TIME_BUF
], mbuf
[ZEBRA_TIME_BUF
];
3170 vty_out(vty
, "Client: %s", zebra_route_string(client
->proto
));
3171 if (client
->instance
)
3172 vty_out(vty
, " Instance: %d", client
->instance
);
3175 vty_out(vty
, "------------------------ \n");
3176 vty_out(vty
, "FD: %d \n", client
->sock
);
3177 vty_out(vty
, "Route Table ID: %d \n", client
->rtm_table
);
3179 vty_out(vty
, "Connect Time: %s \n",
3180 zserv_time_buf(&client
->connect_time
, cbuf
, ZEBRA_TIME_BUF
));
3181 if (client
->nh_reg_time
) {
3182 vty_out(vty
, "Nexthop Registry Time: %s \n",
3183 zserv_time_buf(&client
->nh_reg_time
, nhbuf
,
3185 if (client
->nh_last_upd_time
)
3186 vty_out(vty
, "Nexthop Last Update Time: %s \n",
3187 zserv_time_buf(&client
->nh_last_upd_time
, mbuf
,
3190 vty_out(vty
, "No Nexthop Update sent\n");
3192 vty_out(vty
, "Not registered for Nexthop Updates\n");
3194 vty_out(vty
, "Last Msg Rx Time: %s \n",
3195 zserv_time_buf(&client
->last_read_time
, rbuf
, ZEBRA_TIME_BUF
));
3196 vty_out(vty
, "Last Msg Tx Time: %s \n",
3197 zserv_time_buf(&client
->last_write_time
, wbuf
, ZEBRA_TIME_BUF
));
3198 if (client
->last_read_time
)
3199 vty_out(vty
, "Last Rcvd Cmd: %s \n",
3200 zserv_command_string(client
->last_read_cmd
));
3201 if (client
->last_write_time
)
3202 vty_out(vty
, "Last Sent Cmd: %s \n",
3203 zserv_command_string(client
->last_write_cmd
));
3206 vty_out(vty
, "Type Add Update Del \n");
3207 vty_out(vty
, "================================================== \n");
3208 vty_out(vty
, "IPv4 %-12d%-12d%-12d\n", client
->v4_route_add_cnt
,
3209 client
->v4_route_upd8_cnt
, client
->v4_route_del_cnt
);
3210 vty_out(vty
, "IPv6 %-12d%-12d%-12d\n", client
->v6_route_add_cnt
,
3211 client
->v6_route_upd8_cnt
, client
->v6_route_del_cnt
);
3212 vty_out(vty
, "Redist:v4 %-12d%-12d%-12d\n", client
->redist_v4_add_cnt
,
3213 0, client
->redist_v4_del_cnt
);
3214 vty_out(vty
, "Redist:v6 %-12d%-12d%-12d\n", client
->redist_v6_add_cnt
,
3215 0, client
->redist_v6_del_cnt
);
3216 vty_out(vty
, "Connected %-12d%-12d%-12d\n", client
->ifadd_cnt
, 0,
3218 vty_out(vty
, "BFD peer %-12d%-12d%-12d\n", client
->bfd_peer_add_cnt
,
3219 client
->bfd_peer_upd8_cnt
, client
->bfd_peer_del_cnt
);
3220 vty_out(vty
, "Interface Up Notifications: %d\n", client
->ifup_cnt
);
3221 vty_out(vty
, "Interface Down Notifications: %d\n", client
->ifdown_cnt
);
3222 vty_out(vty
, "VNI add notifications: %d\n", client
->vniadd_cnt
);
3223 vty_out(vty
, "VNI delete notifications: %d\n", client
->vnidel_cnt
);
3224 vty_out(vty
, "L3-VNI add notifications: %d\n", client
->l3vniadd_cnt
);
3225 vty_out(vty
, "L3-VNI delete notifications: %d\n", client
->l3vnidel_cnt
);
3226 vty_out(vty
, "MAC-IP add notifications: %d\n", client
->macipadd_cnt
);
3227 vty_out(vty
, "MAC-IP delete notifications: %d\n", client
->macipdel_cnt
);
3233 static void zebra_show_client_brief(struct vty
*vty
, struct zserv
*client
)
3235 char cbuf
[ZEBRA_TIME_BUF
], rbuf
[ZEBRA_TIME_BUF
];
3236 char wbuf
[ZEBRA_TIME_BUF
];
3238 vty_out(vty
, "%-8s%12s %12s%12s%8d/%-8d%8d/%-8d\n",
3239 zebra_route_string(client
->proto
),
3240 zserv_time_buf(&client
->connect_time
, cbuf
, ZEBRA_TIME_BUF
),
3241 zserv_time_buf(&client
->last_read_time
, rbuf
, ZEBRA_TIME_BUF
),
3242 zserv_time_buf(&client
->last_write_time
, wbuf
, ZEBRA_TIME_BUF
),
3243 client
->v4_route_add_cnt
+ client
->v4_route_upd8_cnt
,
3244 client
->v4_route_del_cnt
,
3245 client
->v6_route_add_cnt
+ client
->v6_route_upd8_cnt
,
3246 client
->v6_route_del_cnt
);
3249 struct zserv
*zebra_find_client(u_char proto
, u_short instance
)
3251 struct listnode
*node
, *nnode
;
3252 struct zserv
*client
;
3254 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, node
, nnode
, client
)) {
3255 if (client
->proto
== proto
&& client
->instance
== instance
)
3262 /* This command is for debugging purpose. */
3263 DEFUN (show_zebra_client
,
3264 show_zebra_client_cmd
,
3265 "show zebra client",
3268 "Client information\n")
3270 struct listnode
*node
;
3271 struct zserv
*client
;
3273 for (ALL_LIST_ELEMENTS_RO(zebrad
.client_list
, node
, client
))
3274 zebra_show_client_detail(vty
, client
);
3279 /* This command is for debugging purpose. */
3280 DEFUN (show_zebra_client_summary
,
3281 show_zebra_client_summary_cmd
,
3282 "show zebra client summary",
3285 "Client information brief\n"
3288 struct listnode
*node
;
3289 struct zserv
*client
;
3292 "Name Connect Time Last Read Last Write IPv4 Routes IPv6 Routes \n");
3294 "--------------------------------------------------------------------------------\n");
3296 for (ALL_LIST_ELEMENTS_RO(zebrad
.client_list
, node
, client
))
3297 zebra_show_client_brief(vty
, client
);
3299 vty_out(vty
, "Routes column shows (added+updated)/deleted\n");
3303 #if defined(HANDLE_ZAPI_FUZZING)
3304 void zserv_read_file(char *input
)
3307 struct zserv
*client
= NULL
;
3310 zebra_client_create(-1);
3311 client
= zebrad
.client_list
->head
->data
;
3314 fd
= open(input
, O_RDONLY
| O_NONBLOCK
);
3317 zebra_client_read(&t
);
3323 void zserv_init(void)
3325 /* Client list init. */
3326 zebrad
.client_list
= list_new();
3327 zebrad
.client_list
->del
= (void (*)(void *))zebra_client_free
;
3329 install_element(ENABLE_NODE
, &show_zebra_client_cmd
);
3330 install_element(ENABLE_NODE
, &show_zebra_client_summary_cmd
);