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
30 #include "zebra_memory.h"
34 #include "sockunion.h"
45 #include "zebra/zserv.h"
46 #include "zebra/zebra_ns.h"
47 #include "zebra/zebra_vrf.h"
48 #include "zebra/router-id.h"
49 #include "zebra/redistribute.h"
50 #include "zebra/debug.h"
51 #include "zebra/ipforward.h"
52 #include "zebra/zebra_rnh.h"
53 #include "zebra/rt_netlink.h"
54 #include "zebra/interface.h"
55 #include "zebra/zebra_ptm.h"
56 #include "zebra/rtadv.h"
57 #include "zebra/zebra_mpls.h"
58 #include "zebra/zebra_mroute.h"
59 #include "zebra/label_manager.h"
60 #include "zebra/zebra_vxlan.h"
63 /* Event list of zebra. */
64 enum event
{ ZEBRA_SERV
, ZEBRA_READ
, ZEBRA_WRITE
};
66 static void zebra_event(enum event event
, int sock
, struct zserv
*client
);
68 extern struct zebra_privs_t zserv_privs
;
70 static void zebra_client_close(struct zserv
*client
);
72 static int zserv_delayed_close(struct thread
*thread
)
74 struct zserv
*client
= THREAD_ARG(thread
);
76 client
->t_suicide
= NULL
;
77 zebra_client_close(client
);
81 static int zserv_flush_data(struct thread
*thread
)
83 struct zserv
*client
= THREAD_ARG(thread
);
85 client
->t_write
= NULL
;
86 if (client
->t_suicide
) {
87 zebra_client_close(client
);
90 switch (buffer_flush_available(client
->wb
, client
->sock
)) {
93 "%s: buffer_flush_available failed on zserv client fd %d, "
95 __func__
, client
->sock
);
96 zebra_client_close(client
);
100 client
->t_write
= NULL
;
101 thread_add_write(zebrad
.master
, zserv_flush_data
, client
,
102 client
->sock
, &client
->t_write
);
109 client
->last_write_time
= monotime(NULL
);
113 int zebra_server_send_message(struct zserv
*client
)
115 if (client
->t_suicide
)
118 if (client
->is_synchronous
)
121 stream_set_getp(client
->obuf
, 0);
122 client
->last_write_cmd
= stream_getw_from(client
->obuf
, 6);
123 switch (buffer_write(client
->wb
, client
->sock
,
124 STREAM_DATA(client
->obuf
),
125 stream_get_endp(client
->obuf
))) {
128 "%s: buffer_write failed to zserv client fd %d, closing",
129 __func__
, client
->sock
);
130 /* Schedule a delayed close since many of the functions that
132 one do not check the return code. They do not allow for the
133 possibility that an I/O error may have caused the client to
136 client
->t_suicide
= NULL
;
137 thread_add_event(zebrad
.master
, zserv_delayed_close
, client
, 0,
141 THREAD_OFF(client
->t_write
);
144 thread_add_write(zebrad
.master
, zserv_flush_data
, client
,
145 client
->sock
, &client
->t_write
);
149 client
->last_write_time
= monotime(NULL
);
153 void zserv_create_header(struct stream
*s
, uint16_t cmd
, vrf_id_t vrf_id
)
155 /* length placeholder, caller can update */
156 stream_putw(s
, ZEBRA_HEADER_SIZE
);
157 stream_putc(s
, ZEBRA_HEADER_MARKER
);
158 stream_putc(s
, ZSERV_VERSION
);
159 stream_putw(s
, vrf_id
);
163 static void zserv_encode_interface(struct stream
*s
, struct interface
*ifp
)
165 /* Interface information. */
166 stream_put(s
, ifp
->name
, INTERFACE_NAMSIZ
);
167 stream_putl(s
, ifp
->ifindex
);
168 stream_putc(s
, ifp
->status
);
169 stream_putq(s
, ifp
->flags
);
170 stream_putc(s
, ifp
->ptm_enable
);
171 stream_putc(s
, ifp
->ptm_status
);
172 stream_putl(s
, ifp
->metric
);
173 stream_putl(s
, ifp
->speed
);
174 stream_putl(s
, ifp
->mtu
);
175 stream_putl(s
, ifp
->mtu6
);
176 stream_putl(s
, ifp
->bandwidth
);
177 stream_putl(s
, ifp
->ll_type
);
178 stream_putl(s
, ifp
->hw_addr_len
);
179 if (ifp
->hw_addr_len
)
180 stream_put(s
, ifp
->hw_addr
, ifp
->hw_addr_len
);
182 /* Then, Traffic Engineering parameters if any */
183 if (HAS_LINK_PARAMS(ifp
) && IS_LINK_PARAMS_SET(ifp
->link_params
)) {
185 zebra_interface_link_params_write(s
, ifp
);
189 /* Write packet size. */
190 stream_putw_at(s
, 0, stream_get_endp(s
));
193 static void zserv_encode_vrf(struct stream
*s
, struct zebra_vrf
*zvrf
)
195 struct vrf_data data
;
197 data
.l
.table_id
= zvrf
->table_id
;
198 /* Pass the tableid */
199 stream_put(s
, &data
, sizeof(struct vrf_data
));
200 /* Interface information. */
201 stream_put(s
, zvrf_name(zvrf
), VRF_NAMSIZ
);
203 /* Write packet size. */
204 stream_putw_at(s
, 0, stream_get_endp(s
));
207 /* Interface is added. Send ZEBRA_INTERFACE_ADD to client. */
209 * This function is called in the following situations:
210 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
212 * - at startup, when zebra figures out the available interfaces
213 * - when an interface is added (where support for
214 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
215 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
218 int zsend_interface_add(struct zserv
*client
, struct interface
*ifp
)
225 zserv_create_header(s
, ZEBRA_INTERFACE_ADD
, ifp
->vrf_id
);
226 zserv_encode_interface(s
, ifp
);
229 return zebra_server_send_message(client
);
232 /* Interface deletion from zebra daemon. */
233 int zsend_interface_delete(struct zserv
*client
, struct interface
*ifp
)
240 zserv_create_header(s
, ZEBRA_INTERFACE_DELETE
, ifp
->vrf_id
);
241 zserv_encode_interface(s
, ifp
);
244 return zebra_server_send_message(client
);
247 int zsend_vrf_add(struct zserv
*client
, struct zebra_vrf
*zvrf
)
254 zserv_create_header(s
, ZEBRA_VRF_ADD
, zvrf_id(zvrf
));
255 zserv_encode_vrf(s
, zvrf
);
257 client
->vrfadd_cnt
++;
258 return zebra_server_send_message(client
);
261 /* VRF deletion from zebra daemon. */
262 int zsend_vrf_delete(struct zserv
*client
, struct zebra_vrf
*zvrf
)
269 zserv_create_header(s
, ZEBRA_VRF_DELETE
, zvrf_id(zvrf
));
270 zserv_encode_vrf(s
, zvrf
);
272 client
->vrfdel_cnt
++;
273 return zebra_server_send_message(client
);
276 int zsend_interface_link_params(struct zserv
*client
, struct interface
*ifp
)
280 /* Check this client need interface information. */
284 if (!ifp
->link_params
)
289 zserv_create_header(s
, ZEBRA_INTERFACE_LINK_PARAMS
, ifp
->vrf_id
);
291 /* Add Interface Index */
292 stream_putl(s
, ifp
->ifindex
);
294 /* Then TE Link Parameters */
295 if (zebra_interface_link_params_write(s
, ifp
) == 0)
298 /* Write packet size. */
299 stream_putw_at(s
, 0, stream_get_endp(s
));
301 return zebra_server_send_message(client
);
304 /* Interface address is added/deleted. Send ZEBRA_INTERFACE_ADDRESS_ADD or
305 * ZEBRA_INTERFACE_ADDRESS_DELETE to the client.
307 * A ZEBRA_INTERFACE_ADDRESS_ADD is sent in the following situations:
308 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
309 * from the client, after the ZEBRA_INTERFACE_ADD has been
310 * sent from zebra to the client
311 * - redistribute new address info to all clients in the following situations
312 * - at startup, when zebra figures out the available interfaces
313 * - when an interface is added (where support for
314 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
315 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
317 * - for the vty commands "ip address A.B.C.D/M [<secondary>|<label LINE>]"
318 * and "no bandwidth <1-10000000>", "ipv6 address X:X::X:X/M"
319 * - when an RTM_NEWADDR message is received from the kernel,
321 * The call tree that triggers ZEBRA_INTERFACE_ADDRESS_DELETE:
323 * zsend_interface_address(DELETE)
326 * zebra_interface_address_delete_update
328 * | | if_delete_update
330 * ip_address_uninstall connected_delete_ipv4
331 * [ipv6_addresss_uninstall] [connected_delete_ipv6]
334 * | RTM_NEWADDR on routing/netlink socket
337 * "no ip address A.B.C.D/M [label LINE]"
338 * "no ip address A.B.C.D/M secondary"
339 * ["no ipv6 address X:X::X:X/M"]
342 int zsend_interface_address(int cmd
, struct zserv
*client
,
343 struct interface
*ifp
, struct connected
*ifc
)
352 zserv_create_header(s
, cmd
, ifp
->vrf_id
);
353 stream_putl(s
, ifp
->ifindex
);
355 /* Interface address flag. */
356 stream_putc(s
, ifc
->flags
);
358 /* Prefix information. */
360 stream_putc(s
, p
->family
);
361 blen
= prefix_blen(p
);
362 stream_put(s
, &p
->u
.prefix
, blen
);
365 * XXX gnu version does not send prefixlen for
366 * ZEBRA_INTERFACE_ADDRESS_DELETE
367 * but zebra_interface_address_delete_read() in the gnu version
370 stream_putc(s
, p
->prefixlen
);
373 p
= ifc
->destination
;
375 stream_put(s
, &p
->u
.prefix
, blen
);
377 stream_put(s
, NULL
, blen
);
379 /* Write packet size. */
380 stream_putw_at(s
, 0, stream_get_endp(s
));
382 client
->connected_rt_add_cnt
++;
383 return zebra_server_send_message(client
);
386 static int zsend_interface_nbr_address(int cmd
, struct zserv
*client
,
387 struct interface
*ifp
,
388 struct nbr_connected
*ifc
)
397 zserv_create_header(s
, cmd
, ifp
->vrf_id
);
398 stream_putl(s
, ifp
->ifindex
);
400 /* Prefix information. */
402 stream_putc(s
, p
->family
);
403 blen
= prefix_blen(p
);
404 stream_put(s
, &p
->u
.prefix
, blen
);
407 * XXX gnu version does not send prefixlen for
408 * ZEBRA_INTERFACE_ADDRESS_DELETE
409 * but zebra_interface_address_delete_read() in the gnu version
412 stream_putc(s
, p
->prefixlen
);
414 /* Write packet size. */
415 stream_putw_at(s
, 0, stream_get_endp(s
));
417 return zebra_server_send_message(client
);
420 /* Interface address addition. */
421 static void zebra_interface_nbr_address_add_update(struct interface
*ifp
,
422 struct nbr_connected
*ifc
)
424 struct listnode
*node
, *nnode
;
425 struct zserv
*client
;
428 if (IS_ZEBRA_DEBUG_EVENT
) {
429 char buf
[INET6_ADDRSTRLEN
];
433 "MESSAGE: ZEBRA_INTERFACE_NBR_ADDRESS_ADD %s/%d on %s",
434 inet_ntop(p
->family
, &p
->u
.prefix
, buf
,
436 p
->prefixlen
, ifc
->ifp
->name
);
439 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, node
, nnode
, client
))
440 zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_ADD
,
444 /* Interface address deletion. */
445 static void zebra_interface_nbr_address_delete_update(struct interface
*ifp
,
446 struct nbr_connected
*ifc
)
448 struct listnode
*node
, *nnode
;
449 struct zserv
*client
;
452 if (IS_ZEBRA_DEBUG_EVENT
) {
453 char buf
[INET6_ADDRSTRLEN
];
457 "MESSAGE: ZEBRA_INTERFACE_NBR_ADDRESS_DELETE %s/%d on %s",
458 inet_ntop(p
->family
, &p
->u
.prefix
, buf
,
460 p
->prefixlen
, ifc
->ifp
->name
);
463 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, node
, nnode
, client
))
464 zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
,
468 /* Send addresses on interface to client */
469 int zsend_interface_addresses(struct zserv
*client
, struct interface
*ifp
)
471 struct listnode
*cnode
, *cnnode
;
473 struct nbr_connected
*nc
;
475 /* Send interface addresses. */
476 for (ALL_LIST_ELEMENTS(ifp
->connected
, cnode
, cnnode
, c
)) {
477 if (!CHECK_FLAG(c
->conf
, ZEBRA_IFC_REAL
))
480 if (zsend_interface_address(ZEBRA_INTERFACE_ADDRESS_ADD
, client
,
486 /* Send interface neighbors. */
487 for (ALL_LIST_ELEMENTS(ifp
->nbr_connected
, cnode
, cnnode
, nc
)) {
488 if (zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_ADD
,
497 /* Notify client about interface moving from one VRF to another.
498 * Whether client is interested in old and new VRF is checked by caller.
500 int zsend_interface_vrf_update(struct zserv
*client
, struct interface
*ifp
,
508 zserv_create_header(s
, ZEBRA_INTERFACE_VRF_UPDATE
, ifp
->vrf_id
);
510 /* Fill in the ifIndex of the interface and its new VRF (id) */
511 stream_putl(s
, ifp
->ifindex
);
512 stream_putw(s
, vrf_id
);
514 /* Write packet size. */
515 stream_putw_at(s
, 0, stream_get_endp(s
));
517 client
->if_vrfchg_cnt
++;
518 return zebra_server_send_message(client
);
521 /* Add new nbr connected IPv6 address */
522 void nbr_connected_add_ipv6(struct interface
*ifp
, struct in6_addr
*address
)
524 struct nbr_connected
*ifc
;
528 IPV6_ADDR_COPY(&p
.u
.prefix
, address
);
529 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
531 if (!(ifc
= listnode_head(ifp
->nbr_connected
))) {
533 ifc
= nbr_connected_new();
534 ifc
->address
= prefix_new();
536 listnode_add(ifp
->nbr_connected
, ifc
);
539 prefix_copy(ifc
->address
, &p
);
541 zebra_interface_nbr_address_add_update(ifp
, ifc
);
543 if_nbr_ipv6ll_to_ipv4ll_neigh_update(ifp
, address
, 1);
546 void nbr_connected_delete_ipv6(struct interface
*ifp
, struct in6_addr
*address
)
548 struct nbr_connected
*ifc
;
552 IPV6_ADDR_COPY(&p
.u
.prefix
, address
);
553 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
555 ifc
= nbr_connected_check(ifp
, &p
);
559 listnode_delete(ifp
->nbr_connected
, ifc
);
561 zebra_interface_nbr_address_delete_update(ifp
, ifc
);
563 if_nbr_ipv6ll_to_ipv4ll_neigh_update(ifp
, address
, 0);
565 nbr_connected_free(ifc
);
569 * The cmd passed to zsend_interface_update may be ZEBRA_INTERFACE_UP or
570 * ZEBRA_INTERFACE_DOWN.
572 * The ZEBRA_INTERFACE_UP message is sent from the zebra server to
573 * the clients in one of 2 situations:
574 * - an if_up is detected e.g., as a result of an RTM_IFINFO message
575 * - a vty command modifying the bandwidth of an interface is received.
576 * The ZEBRA_INTERFACE_DOWN message is sent when an if_down is detected.
578 int zsend_interface_update(int cmd
, struct zserv
*client
, struct interface
*ifp
)
585 zserv_create_header(s
, cmd
, ifp
->vrf_id
);
586 zserv_encode_interface(s
, ifp
);
588 if (cmd
== ZEBRA_INTERFACE_UP
)
591 client
->ifdown_cnt
++;
593 return zebra_server_send_message(client
);
596 int zsend_redistribute_route(int cmd
, struct zserv
*client
, struct prefix
*p
,
597 struct prefix
*src_p
, struct route_entry
*re
)
599 struct zapi_route api
;
600 struct zapi_nexthop
*api_nh
;
601 struct nexthop
*nexthop
;
604 memset(&api
, 0, sizeof(api
));
605 api
.vrf_id
= re
->vrf_id
;
607 api
.instance
= re
->instance
;
608 api
.flags
= re
->flags
;
613 SET_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
);
614 memcpy(&api
.src_prefix
, src_p
, sizeof(api
.src_prefix
));
618 if (re
->nexthop_active_num
) {
619 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
620 api
.nexthop_num
= re
->nexthop_active_num
;
622 for (nexthop
= re
->nexthop
; nexthop
; nexthop
= nexthop
->next
) {
623 if (!CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
626 api_nh
= &api
.nexthops
[count
];
627 api_nh
->type
= nexthop
->type
;
628 switch (nexthop
->type
) {
629 case NEXTHOP_TYPE_BLACKHOLE
:
630 api_nh
->bh_type
= nexthop
->bh_type
;
632 case NEXTHOP_TYPE_IPV4
:
633 api_nh
->gate
.ipv4
= nexthop
->gate
.ipv4
;
635 case NEXTHOP_TYPE_IPV4_IFINDEX
:
636 api_nh
->gate
.ipv4
= nexthop
->gate
.ipv4
;
637 api_nh
->ifindex
= nexthop
->ifindex
;
639 case NEXTHOP_TYPE_IFINDEX
:
640 api_nh
->ifindex
= nexthop
->ifindex
;
642 case NEXTHOP_TYPE_IPV6
:
643 api_nh
->gate
.ipv6
= nexthop
->gate
.ipv6
;
645 case NEXTHOP_TYPE_IPV6_IFINDEX
:
646 api_nh
->gate
.ipv6
= nexthop
->gate
.ipv6
;
647 api_nh
->ifindex
= nexthop
->ifindex
;
653 SET_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
);
654 api
.distance
= re
->distance
;
655 SET_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
);
656 api
.metric
= re
->metric
;
658 SET_FLAG(api
.message
, ZAPI_MESSAGE_TAG
);
661 SET_FLAG(api
.message
, ZAPI_MESSAGE_MTU
);
664 /* Encode route and send. */
665 if (zapi_route_encode(cmd
, client
->obuf
, &api
) < 0)
667 return zebra_server_send_message(client
);
670 static int zsend_write_nexthop(struct stream
*s
, struct nexthop
*nexthop
)
672 stream_putc(s
, nexthop
->type
);
673 switch (nexthop
->type
) {
674 case NEXTHOP_TYPE_IPV4
:
675 case NEXTHOP_TYPE_IPV4_IFINDEX
:
676 stream_put_in_addr(s
, &nexthop
->gate
.ipv4
);
677 stream_putl(s
, nexthop
->ifindex
);
679 case NEXTHOP_TYPE_IPV6
:
680 stream_put(s
, &nexthop
->gate
.ipv6
, 16);
682 case NEXTHOP_TYPE_IPV6_IFINDEX
:
683 stream_put(s
, &nexthop
->gate
.ipv6
, 16);
684 stream_putl(s
, nexthop
->ifindex
);
686 case NEXTHOP_TYPE_IFINDEX
:
687 stream_putl(s
, nexthop
->ifindex
);
696 /* Nexthop register */
697 static int zserv_rnh_register(struct zserv
*client
, u_short length
,
698 rnh_type_t type
, struct zebra_vrf
*zvrf
)
706 if (IS_ZEBRA_DEBUG_NHT
)
708 "rnh_register msg from client %s: length=%d, type=%s\n",
709 zebra_route_string(client
->proto
), length
,
710 (type
== RNH_NEXTHOP_TYPE
) ? "nexthop" : "route");
714 client
->nh_reg_time
= monotime(NULL
);
717 STREAM_GETC(s
, flags
);
718 STREAM_GETW(s
, p
.family
);
719 STREAM_GETC(s
, p
.prefixlen
);
721 if (p
.family
== AF_INET
) {
722 if (p
.prefixlen
> IPV4_MAX_BITLEN
) {
723 zlog_warn("%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
) {
731 zlog_warn("%s: Specified prefix length %d is to large for a v6 address",
732 __PRETTY_FUNCTION__
, p
.prefixlen
);
735 STREAM_GET(&p
.u
.prefix6
, s
, IPV6_MAX_BYTELEN
);
736 l
+= IPV6_MAX_BYTELEN
;
739 "rnh_register: Received unknown family type %d\n",
743 rnh
= zebra_add_rnh(&p
, zvrf_id(zvrf
), type
);
744 if (type
== RNH_NEXTHOP_TYPE
) {
746 && !CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
))
747 SET_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
);
749 && CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
))
750 UNSET_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
);
751 } else if (type
== RNH_IMPORT_CHECK_TYPE
) {
753 && !CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
))
754 SET_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
);
755 else if (!flags
&& CHECK_FLAG(rnh
->flags
,
756 ZEBRA_NHT_EXACT_MATCH
))
757 UNSET_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
);
760 zebra_add_rnh_client(rnh
, client
, type
, zvrf_id(zvrf
));
761 /* Anything not AF_INET/INET6 has been filtered out above */
762 zebra_evaluate_rnh(zvrf_id(zvrf
), p
.family
, 1, type
, &p
);
769 /* Nexthop register */
770 static int zserv_rnh_unregister(struct zserv
*client
, u_short length
,
771 rnh_type_t type
, struct zebra_vrf
*zvrf
)
778 if (IS_ZEBRA_DEBUG_NHT
)
779 zlog_debug("rnh_unregister msg from client %s: length=%d\n",
780 zebra_route_string(client
->proto
), length
);
787 STREAM_GETC(s
, flags
);
791 STREAM_GETW(s
, p
.family
);
792 STREAM_GETC(s
, p
.prefixlen
);
794 if (p
.family
== AF_INET
) {
795 if (p
.prefixlen
> IPV4_MAX_BITLEN
) {
796 zlog_warn("%s: Specified prefix length %d is to large for a v4 address",
797 __PRETTY_FUNCTION__
, p
.prefixlen
);
800 STREAM_GET(&p
.u
.prefix4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
801 l
+= IPV4_MAX_BYTELEN
;
802 } else if (p
.family
== AF_INET6
) {
803 if (p
.prefixlen
> IPV6_MAX_BITLEN
) {
804 zlog_warn("%s: Specified prefix length %d is to large for a v6 address",
805 __PRETTY_FUNCTION__
, p
.prefixlen
);
808 STREAM_GET(&p
.u
.prefix6
, s
, IPV6_MAX_BYTELEN
);
809 l
+= IPV6_MAX_BYTELEN
;
812 "rnh_register: Received unknown family type %d\n",
816 rnh
= zebra_lookup_rnh(&p
, zvrf_id(zvrf
), type
);
818 client
->nh_dereg_time
= monotime(NULL
);
819 zebra_remove_rnh_client(rnh
, client
, type
);
826 #define ZEBRA_MIN_FEC_LENGTH 5
829 static int zserv_fec_register(struct zserv
*client
, u_short length
)
832 struct zebra_vrf
*zvrf
;
836 u_int32_t label_index
= MPLS_INVALID_LABEL_INDEX
;
839 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
841 return 0; // unexpected
844 * The minimum amount of data that can be sent for one fec
847 if (length
< ZEBRA_MIN_FEC_LENGTH
) {
849 "fec_register: Received a fec register of length %d, it is of insufficient size to properly decode",
855 STREAM_GETW(s
, flags
);
856 memset(&p
, 0, sizeof(p
));
857 STREAM_GETW(s
, p
.family
);
858 if (p
.family
!= AF_INET
&& p
.family
!= AF_INET6
) {
860 "fec_register: Received unknown family type %d\n",
864 STREAM_GETC(s
, p
.prefixlen
);
865 if ((p
.family
== AF_INET
&& p
.prefixlen
> IPV4_MAX_BITLEN
) ||
866 (p
.family
== AF_INET6
&& p
.prefixlen
> IPV6_MAX_BITLEN
)) {
867 zlog_warn("%s: Specified prefix length: %d is to long for %d",
868 __PRETTY_FUNCTION__
, p
.prefixlen
, p
.family
);
872 STREAM_GET(&p
.u
.prefix
, s
, PSIZE(p
.prefixlen
));
873 l
+= PSIZE(p
.prefixlen
);
874 if (flags
& ZEBRA_FEC_REGISTER_LABEL_INDEX
) {
875 STREAM_GETL(s
, label_index
);
878 label_index
= MPLS_INVALID_LABEL_INDEX
;
879 zebra_mpls_fec_register(zvrf
, &p
, label_index
, client
);
887 static int zserv_fec_unregister(struct zserv
*client
, u_short length
)
890 struct zebra_vrf
*zvrf
;
896 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
898 return 0; // unexpected
901 * The minimum amount of data that can be sent for one
904 if (length
< ZEBRA_MIN_FEC_LENGTH
) {
906 "fec_unregister: Received a fec unregister of length %d, it is of insufficient size to properly decode",
912 STREAM_GETW(s
, flags
);
916 memset(&p
, 0, sizeof(p
));
917 STREAM_GETW(s
, p
.family
);
918 if (p
.family
!= AF_INET
&& p
.family
!= AF_INET6
) {
920 "fec_unregister: Received unknown family type %d\n",
924 STREAM_GETC(s
, p
.prefixlen
);
925 if ((p
.family
== AF_INET
&& p
.prefixlen
> IPV4_MAX_BITLEN
) ||
926 (p
.family
== AF_INET6
&& p
.prefixlen
> IPV6_MAX_BITLEN
)) {
927 zlog_warn("%s: Received prefix length %d which is greater than %d can support",
928 __PRETTY_FUNCTION__
, p
.prefixlen
, p
.family
);
932 STREAM_GET(&p
.u
.prefix
, s
, PSIZE(p
.prefixlen
));
933 l
+= PSIZE(p
.prefixlen
);
934 zebra_mpls_fec_unregister(zvrf
, &p
, client
);
942 Modified version of zsend_ipv4_nexthop_lookup():
943 Query unicast rib if nexthop is not found on mrib.
944 Returns both route metric and protocol distance.
946 static int zsend_ipv4_nexthop_lookup_mrib(struct zserv
*client
,
948 struct route_entry
*re
,
949 struct zebra_vrf
*zvrf
)
954 struct nexthop
*nexthop
;
956 /* Get output stream. */
960 /* Fill in result. */
961 zserv_create_header(s
, ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB
, zvrf_id(zvrf
));
962 stream_put_in_addr(s
, &addr
);
965 stream_putc(s
, re
->distance
);
966 stream_putl(s
, re
->metric
);
968 nump
= stream_get_endp(
969 s
); /* remember position for nexthop_num */
970 stream_putc(s
, 0); /* reserve room for nexthop_num */
971 /* Only non-recursive routes are elegible to resolve the nexthop
973 * are looking up. Therefore, we will just iterate over the top
974 * chain of nexthops. */
975 for (nexthop
= re
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
976 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
977 num
+= zsend_write_nexthop(s
, nexthop
);
979 stream_putc_at(s
, nump
, num
); /* store nexthop_num */
981 stream_putc(s
, 0); /* distance */
982 stream_putl(s
, 0); /* metric */
983 stream_putc(s
, 0); /* nexthop_num */
986 stream_putw_at(s
, 0, stream_get_endp(s
));
988 return zebra_server_send_message(client
);
991 int zsend_route_notify_owner(u_char proto
, vrf_id_t vrf_id
,
993 enum zapi_route_notify_owner note
)
995 struct zserv
*client
;
999 client
= zebra_find_client(proto
);
1000 if (!client
|| !client
->notify_owner
) {
1001 if (IS_ZEBRA_DEBUG_PACKET
) {
1002 char buff
[PREFIX_STRLEN
];
1004 zlog_debug("Not Notifying Owner: %u about prefix %s",
1005 proto
, prefix2str(p
, buff
, sizeof(buff
)));
1013 zserv_create_header(s
, ZEBRA_ROUTE_NOTIFY_OWNER
, vrf_id
);
1015 stream_put(s
, ¬e
, sizeof(note
));
1017 stream_putc(s
, p
->family
);
1019 blen
= prefix_blen(p
);
1020 stream_putc(s
, p
->prefixlen
);
1021 stream_put(s
, &p
->u
.prefix
, blen
);
1023 stream_putw_at(s
, 0, stream_get_endp(s
));
1025 return zebra_server_send_message(client
);
1028 /* Router-id is updated. Send ZEBRA_ROUTER_ID_ADD to client. */
1029 int zsend_router_id_update(struct zserv
*client
, struct prefix
*p
,
1035 /* Check this client need interface information. */
1036 if (!vrf_bitmap_check(client
->ridinfo
, vrf_id
))
1043 zserv_create_header(s
, ZEBRA_ROUTER_ID_UPDATE
, vrf_id
);
1045 /* Prefix information. */
1046 stream_putc(s
, p
->family
);
1047 blen
= prefix_blen(p
);
1048 stream_put(s
, &p
->u
.prefix
, blen
);
1049 stream_putc(s
, p
->prefixlen
);
1051 /* Write packet size. */
1052 stream_putw_at(s
, 0, stream_get_endp(s
));
1054 return zebra_server_send_message(client
);
1058 * Function used by Zebra to send a PW status update to LDP daemon
1060 int zsend_pw_update(struct zserv
*client
, struct zebra_pw
*pw
)
1067 zserv_create_header(s
, ZEBRA_PW_STATUS_UPDATE
, pw
->vrf_id
);
1068 stream_write(s
, pw
->ifname
, IF_NAMESIZE
);
1069 stream_putl(s
, pw
->ifindex
);
1070 stream_putl(s
, pw
->status
);
1072 /* Put length at the first point of the stream. */
1073 stream_putw_at(s
, 0, stream_get_endp(s
));
1075 return zebra_server_send_message(client
);
1078 /* Register zebra server interface information. Send current all
1079 interface and address information. */
1080 static int zread_interface_add(struct zserv
*client
, u_short length
,
1081 struct zebra_vrf
*zvrf
)
1084 struct interface
*ifp
;
1086 /* Interface information is needed. */
1087 vrf_bitmap_set(client
->ifinfo
, zvrf_id(zvrf
));
1089 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
1090 FOR_ALL_INTERFACES (vrf
, ifp
) {
1091 /* Skip pseudo interface. */
1092 if (!CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_ACTIVE
))
1095 if (zsend_interface_add(client
, ifp
) < 0)
1098 if (zsend_interface_addresses(client
, ifp
) < 0)
1105 /* Unregister zebra server interface information. */
1106 static int zread_interface_delete(struct zserv
*client
, u_short length
,
1107 struct zebra_vrf
*zvrf
)
1109 vrf_bitmap_unset(client
->ifinfo
, zvrf_id(zvrf
));
1113 void zserv_nexthop_num_warn(const char *caller
, const struct prefix
*p
,
1114 const unsigned int nexthop_num
)
1116 if (nexthop_num
> multipath_num
) {
1117 char buff
[PREFIX2STR_BUFFER
];
1118 prefix2str(p
, buff
, sizeof(buff
));
1120 "%s: Prefix %s has %d nexthops, but we can only use the first %d",
1121 caller
, buff
, nexthop_num
, multipath_num
);
1125 static int zread_route_add(struct zserv
*client
, u_short length
,
1126 struct zebra_vrf
*zvrf
)
1129 struct zapi_route api
;
1130 struct zapi_nexthop
*api_nh
;
1132 struct prefix_ipv6
*src_p
= NULL
;
1133 struct route_entry
*re
;
1134 struct nexthop
*nexthop
= NULL
;
1138 if (zapi_route_decode(s
, &api
) < 0)
1141 /* Allocate new route. */
1142 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
1143 re
->type
= api
.type
;
1144 re
->instance
= api
.instance
;
1145 re
->flags
= api
.flags
;
1146 re
->uptime
= time(NULL
);
1147 re
->vrf_id
= zvrf_id(zvrf
);
1148 re
->table
= zvrf
->table_id
;
1150 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
)) {
1151 for (i
= 0; i
< api
.nexthop_num
; i
++) {
1152 api_nh
= &api
.nexthops
[i
];
1154 switch (api_nh
->type
) {
1155 case NEXTHOP_TYPE_IFINDEX
:
1156 nexthop
= route_entry_nexthop_ifindex_add(
1157 re
, api_nh
->ifindex
);
1159 case NEXTHOP_TYPE_IPV4
:
1160 nexthop
= route_entry_nexthop_ipv4_add(
1161 re
, &api_nh
->gate
.ipv4
, NULL
);
1163 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1164 nexthop
= route_entry_nexthop_ipv4_ifindex_add(
1165 re
, &api_nh
->gate
.ipv4
, NULL
,
1168 case NEXTHOP_TYPE_IPV6
:
1169 nexthop
= route_entry_nexthop_ipv6_add(
1170 re
, &api_nh
->gate
.ipv6
);
1172 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1173 nexthop
= route_entry_nexthop_ipv6_ifindex_add(
1174 re
, &api_nh
->gate
.ipv6
,
1177 case NEXTHOP_TYPE_BLACKHOLE
:
1178 nexthop
= route_entry_nexthop_blackhole_add(
1179 re
, api_nh
->bh_type
);
1184 zlog_warn("%s: Nexthops Specified: %d but we failed to properly create one",
1185 __PRETTY_FUNCTION__
, api
.nexthop_num
);
1186 nexthops_free(re
->nexthop
);
1187 XFREE(MTYPE_RE
, re
);
1190 /* MPLS labels for BGP-LU or Segment Routing */
1191 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_LABEL
)
1192 && api_nh
->type
!= NEXTHOP_TYPE_IFINDEX
1193 && api_nh
->type
!= NEXTHOP_TYPE_BLACKHOLE
) {
1194 enum lsp_types_t label_type
;
1197 lsp_type_from_re_type(client
->proto
);
1198 nexthop_add_labels(nexthop
, label_type
,
1200 &api_nh
->labels
[0]);
1205 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
))
1206 re
->distance
= api
.distance
;
1207 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
))
1208 re
->metric
= api
.metric
;
1209 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_TAG
))
1211 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_MTU
))
1214 afi
= family2afi(api
.prefix
.family
);
1215 if (afi
!= AFI_IP6
&& CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
)) {
1216 zlog_warn("%s: Received SRC Prefix but afi is not v6",
1217 __PRETTY_FUNCTION__
);
1218 nexthops_free(re
->nexthop
);
1219 XFREE(MTYPE_RE
, re
);
1222 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
))
1223 src_p
= &api
.src_prefix
;
1225 ret
= rib_add_multipath(afi
, api
.safi
, &api
.prefix
, src_p
, re
);
1228 switch (api
.prefix
.family
) {
1231 client
->v4_route_add_cnt
++;
1233 client
->v4_route_upd8_cnt
++;
1237 client
->v6_route_add_cnt
++;
1239 client
->v6_route_upd8_cnt
++;
1246 static int zread_route_del(struct zserv
*client
, u_short length
,
1247 struct zebra_vrf
*zvrf
)
1250 struct zapi_route api
;
1252 struct prefix_ipv6
*src_p
= NULL
;
1255 if (zapi_route_decode(s
, &api
) < 0)
1258 afi
= family2afi(api
.prefix
.family
);
1259 if (afi
!= AFI_IP6
&& CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
)) {
1260 zlog_warn("%s: Received a src prefix while afi is not v6",
1261 __PRETTY_FUNCTION__
);
1264 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
))
1265 src_p
= &api
.src_prefix
;
1267 rib_delete(afi
, api
.safi
, zvrf_id(zvrf
), api
.type
, api
.instance
,
1268 api
.flags
, &api
.prefix
, src_p
, NULL
, zvrf
->table_id
,
1272 switch (api
.prefix
.family
) {
1274 client
->v4_route_del_cnt
++;
1277 client
->v6_route_del_cnt
++;
1284 /* This function support multiple nexthop. */
1286 * Parse the ZEBRA_IPV4_ROUTE_ADD sent from client. Update re and
1289 static int zread_ipv4_add(struct zserv
*client
, u_short length
,
1290 struct zebra_vrf
*zvrf
)
1293 struct route_entry
*re
;
1296 struct in_addr nhop_addr
;
1298 u_char nexthop_type
;
1303 enum lsp_types_t label_type
= ZEBRA_LSP_NONE
;
1305 struct nexthop
*nexthop
;
1306 enum blackhole_type bh_type
= BLACKHOLE_NULL
;
1308 /* Get input stream. */
1311 /* Allocate new re. */
1312 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
1314 /* Type, flags, message. */
1315 STREAM_GETC(s
, re
->type
);
1316 if (re
->type
> ZEBRA_ROUTE_MAX
) {
1317 zlog_warn("%s: Specified route type %d is not a legal value\n",
1318 __PRETTY_FUNCTION__
, re
->type
);
1319 XFREE(MTYPE_RE
, re
);
1322 STREAM_GETW(s
, re
->instance
);
1323 STREAM_GETL(s
, re
->flags
);
1324 STREAM_GETC(s
, message
);
1325 STREAM_GETW(s
, safi
);
1326 re
->uptime
= time(NULL
);
1329 memset(&p
, 0, sizeof(struct prefix_ipv4
));
1331 STREAM_GETC(s
, p
.prefixlen
);
1332 if (p
.prefixlen
> IPV4_MAX_BITLEN
) {
1333 zlog_warn("%s: Specified prefix length %d is greater than what v4 can be",
1334 __PRETTY_FUNCTION__
, p
.prefixlen
);
1335 XFREE(MTYPE_RE
, re
);
1338 STREAM_GET(&p
.u
.prefix4
, s
, PSIZE(p
.prefixlen
));
1341 re
->vrf_id
= zvrf_id(zvrf
);
1343 /* Nexthop parse. */
1344 if (CHECK_FLAG(message
, ZAPI_MESSAGE_NEXTHOP
)) {
1345 STREAM_GETC(s
, nexthop_num
);
1346 zserv_nexthop_num_warn(__func__
, (const struct prefix
*)&p
,
1349 if (CHECK_FLAG(message
, ZAPI_MESSAGE_LABEL
))
1350 label_type
= lsp_type_from_re_type(client
->proto
);
1352 for (i
= 0; i
< nexthop_num
; i
++) {
1353 STREAM_GETC(s
, nexthop_type
);
1355 switch (nexthop_type
) {
1356 case NEXTHOP_TYPE_IFINDEX
:
1357 STREAM_GETL(s
, ifindex
);
1358 route_entry_nexthop_ifindex_add(re
, ifindex
);
1360 case NEXTHOP_TYPE_IPV4
:
1361 STREAM_GET(&nhop_addr
.s_addr
, s
,
1363 nexthop
= route_entry_nexthop_ipv4_add(
1364 re
, &nhop_addr
, NULL
);
1365 /* For labeled-unicast, each nexthop is followed
1367 if (CHECK_FLAG(message
, ZAPI_MESSAGE_LABEL
)) {
1368 STREAM_GETL(s
, label
);
1369 nexthop_add_labels(nexthop
, label_type
,
1373 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1374 STREAM_GET(&nhop_addr
.s_addr
, s
,
1376 STREAM_GETL(s
, ifindex
);
1377 route_entry_nexthop_ipv4_ifindex_add(
1378 re
, &nhop_addr
, NULL
, ifindex
);
1380 case NEXTHOP_TYPE_IPV6
:
1381 zlog_warn("%s: Please use ZEBRA_ROUTE_ADD if you want to pass v6 nexthops",
1382 __PRETTY_FUNCTION__
);
1383 nexthops_free(re
->nexthop
);
1384 XFREE(MTYPE_RE
, re
);
1387 case NEXTHOP_TYPE_BLACKHOLE
:
1388 route_entry_nexthop_blackhole_add(re
, bh_type
);
1391 zlog_warn("%s: Specified nexthop type: %d does not exist",
1392 __PRETTY_FUNCTION__
, nexthop_type
);
1393 nexthops_free(re
->nexthop
);
1394 XFREE(MTYPE_RE
, re
);
1401 if (CHECK_FLAG(message
, ZAPI_MESSAGE_DISTANCE
))
1402 STREAM_GETC(s
, re
->distance
);
1405 if (CHECK_FLAG(message
, ZAPI_MESSAGE_METRIC
))
1406 STREAM_GETL(s
, re
->metric
);
1409 if (CHECK_FLAG(message
, ZAPI_MESSAGE_TAG
))
1410 STREAM_GETL(s
, re
->tag
);
1414 if (CHECK_FLAG(message
, ZAPI_MESSAGE_MTU
))
1415 STREAM_GETL(s
, re
->mtu
);
1420 re
->table
= zvrf
->table_id
;
1422 ret
= rib_add_multipath(AFI_IP
, safi
, &p
, NULL
, re
);
1426 client
->v4_route_add_cnt
++;
1428 client
->v4_route_upd8_cnt
++;
1433 nexthops_free(re
->nexthop
);
1434 XFREE(MTYPE_RE
, re
);
1438 /* Zebra server IPv4 prefix delete function. */
1439 static int zread_ipv4_delete(struct zserv
*client
, u_short length
,
1440 struct zebra_vrf
*zvrf
)
1443 struct zapi_ipv4 api
;
1449 /* Type, flags, message. */
1450 STREAM_GETC(s
, api
.type
);
1451 STREAM_GETW(s
, api
.instance
);
1452 STREAM_GETL(s
, api
.flags
);
1453 STREAM_GETC(s
, api
.message
);
1454 STREAM_GETW(s
, api
.safi
);
1457 memset(&p
, 0, sizeof(struct prefix
));
1459 STREAM_GETC(s
, p
.prefixlen
);
1460 if (p
.prefixlen
> IPV4_MAX_BITLEN
) {
1461 zlog_warn("%s: Passed in prefixlen %d is impossible",
1462 __PRETTY_FUNCTION__
, p
.prefixlen
);
1465 STREAM_GET(&p
.u
.prefix4
, s
, PSIZE(p
.prefixlen
));
1467 table_id
= zvrf
->table_id
;
1469 rib_delete(AFI_IP
, api
.safi
, zvrf_id(zvrf
), api
.type
, api
.instance
,
1470 api
.flags
, &p
, NULL
, NULL
, table_id
, 0, false);
1471 client
->v4_route_del_cnt
++;
1477 /* MRIB Nexthop lookup for IPv4. */
1478 static int zread_ipv4_nexthop_lookup_mrib(struct zserv
*client
, u_short length
,
1479 struct zebra_vrf
*zvrf
)
1481 struct in_addr addr
;
1482 struct route_entry
*re
;
1484 STREAM_GET(&addr
.s_addr
, client
->ibuf
, IPV4_MAX_BYTELEN
);
1485 re
= rib_match_ipv4_multicast(zvrf_id(zvrf
), addr
, NULL
);
1486 return zsend_ipv4_nexthop_lookup_mrib(client
, addr
, re
, zvrf
);
1492 /* Zebra server IPv6 prefix add function. */
1493 static int zread_ipv4_route_ipv6_nexthop_add(struct zserv
*client
,
1495 struct zebra_vrf
*zvrf
)
1499 struct in6_addr nhop_addr
;
1500 struct route_entry
*re
;
1503 u_char nexthop_type
;
1506 static struct in6_addr nexthops
[MULTIPATH_NUM
];
1507 static unsigned int ifindices
[MULTIPATH_NUM
];
1509 static mpls_label_t labels
[MULTIPATH_NUM
];
1510 enum lsp_types_t label_type
= ZEBRA_LSP_NONE
;
1512 struct nexthop
*nexthop
;
1513 enum blackhole_type bh_type
= BLACKHOLE_NULL
;
1515 /* Get input stream. */
1518 memset(&nhop_addr
, 0, sizeof(struct in6_addr
));
1520 /* Allocate new re. */
1521 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
1523 /* Type, flags, message. */
1524 STREAM_GETC(s
, re
->type
);
1525 if (re
->type
> ZEBRA_ROUTE_MAX
) {
1526 zlog_warn("%s: Specified route type: %d is not a legal value\n",
1527 __PRETTY_FUNCTION__
, re
->type
);
1528 XFREE(MTYPE_RE
, re
);
1531 STREAM_GETW(s
, re
->instance
);
1532 STREAM_GETL(s
, re
->flags
);
1533 STREAM_GETC(s
, message
);
1534 STREAM_GETW(s
, safi
);
1535 re
->uptime
= time(NULL
);
1538 memset(&p
, 0, sizeof(struct prefix_ipv4
));
1540 STREAM_GETC(s
, p
.prefixlen
);
1541 if (p
.prefixlen
> IPV4_MAX_BITLEN
) {
1542 zlog_warn("%s: Prefix Length %d is greater than what a v4 address can use",
1543 __PRETTY_FUNCTION__
, p
.prefixlen
);
1544 XFREE(MTYPE_RE
, re
);
1547 STREAM_GET(&p
.u
.prefix4
, s
, PSIZE(p
.prefixlen
));
1550 re
->vrf_id
= zvrf_id(zvrf
);
1552 /* We need to give nh-addr, nh-ifindex with the same next-hop object
1553 * to the re to ensure that IPv6 multipathing works; need to coalesce
1554 * these. Clients should send the same number of paired set of
1555 * next-hop-addr/next-hop-ifindices. */
1556 if (CHECK_FLAG(message
, ZAPI_MESSAGE_NEXTHOP
)) {
1557 unsigned int nh_count
= 0;
1558 unsigned int if_count
= 0;
1559 unsigned int max_nh_if
= 0;
1561 STREAM_GETC(s
, nexthop_num
);
1562 zserv_nexthop_num_warn(__func__
, (const struct prefix
*)&p
,
1565 if (CHECK_FLAG(message
, ZAPI_MESSAGE_LABEL
))
1566 label_type
= lsp_type_from_re_type(client
->proto
);
1568 for (i
= 0; i
< nexthop_num
; i
++) {
1569 STREAM_GETC(s
, nexthop_type
);
1571 switch (nexthop_type
) {
1572 case NEXTHOP_TYPE_IPV6
:
1573 STREAM_GET(&nhop_addr
, s
, 16);
1574 if (nh_count
< MULTIPATH_NUM
) {
1575 /* For labeled-unicast, each nexthop is
1576 * followed by label. */
1577 if (CHECK_FLAG(message
,
1578 ZAPI_MESSAGE_LABEL
)) {
1579 STREAM_GETL(s
, label
);
1580 labels
[nh_count
] = label
;
1582 nexthops
[nh_count
] = nhop_addr
;
1586 case NEXTHOP_TYPE_IFINDEX
:
1587 if (if_count
< multipath_num
) {
1588 STREAM_GETL(s
, ifindices
[if_count
++]);
1591 case NEXTHOP_TYPE_BLACKHOLE
:
1592 route_entry_nexthop_blackhole_add(re
, bh_type
);
1595 zlog_warn("%s: Please use ZEBRA_ROUTE_ADD if you want to pass non v6 nexthops",
1596 __PRETTY_FUNCTION__
);
1597 nexthops_free(re
->nexthop
);
1598 XFREE(MTYPE_RE
, re
);
1603 max_nh_if
= (nh_count
> if_count
) ? nh_count
: if_count
;
1604 for (i
= 0; i
< max_nh_if
; i
++) {
1606 && !IN6_IS_ADDR_UNSPECIFIED(&nexthops
[i
])) {
1607 if ((i
< if_count
) && ifindices
[i
])
1609 route_entry_nexthop_ipv6_ifindex_add(
1613 nexthop
= route_entry_nexthop_ipv6_add(
1616 if (CHECK_FLAG(message
, ZAPI_MESSAGE_LABEL
))
1617 nexthop_add_labels(nexthop
, label_type
,
1620 if ((i
< if_count
) && ifindices
[i
])
1621 route_entry_nexthop_ifindex_add(
1628 if (CHECK_FLAG(message
, ZAPI_MESSAGE_DISTANCE
))
1629 STREAM_GETC(s
, re
->distance
);
1632 if (CHECK_FLAG(message
, ZAPI_MESSAGE_METRIC
))
1633 STREAM_GETL(s
, re
->metric
);
1636 if (CHECK_FLAG(message
, ZAPI_MESSAGE_TAG
))
1637 STREAM_GETL(s
, re
->tag
);
1641 if (CHECK_FLAG(message
, ZAPI_MESSAGE_MTU
))
1642 STREAM_GETL(s
, re
->mtu
);
1647 re
->table
= zvrf
->table_id
;
1649 ret
= rib_add_multipath(AFI_IP6
, safi
, &p
, NULL
, re
);
1652 client
->v4_route_add_cnt
++;
1654 client
->v4_route_upd8_cnt
++;
1659 nexthops_free(re
->nexthop
);
1660 XFREE(MTYPE_RE
, re
);
1664 static int zread_ipv6_add(struct zserv
*client
, u_short length
,
1665 struct zebra_vrf
*zvrf
)
1669 struct in6_addr nhop_addr
;
1671 struct route_entry
*re
;
1674 u_char nexthop_type
;
1676 struct prefix_ipv6 src_p
, *src_pp
;
1678 static struct in6_addr nexthops
[MULTIPATH_NUM
];
1679 static unsigned int ifindices
[MULTIPATH_NUM
];
1681 static mpls_label_t labels
[MULTIPATH_NUM
];
1682 enum lsp_types_t label_type
= ZEBRA_LSP_NONE
;
1684 struct nexthop
*nexthop
;
1685 enum blackhole_type bh_type
= BLACKHOLE_NULL
;
1687 /* Get input stream. */
1690 memset(&nhop_addr
, 0, sizeof(struct in6_addr
));
1692 /* Allocate new re. */
1693 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
1695 /* Type, flags, message. */
1696 STREAM_GETC(s
, re
->type
);
1697 if (re
->type
> ZEBRA_ROUTE_MAX
) {
1698 zlog_warn("%s: Specified route type: %d is not a legal value\n",
1699 __PRETTY_FUNCTION__
, re
->type
);
1700 XFREE(MTYPE_RE
, re
);
1703 STREAM_GETW(s
, re
->instance
);
1704 STREAM_GETL(s
, re
->flags
);
1705 STREAM_GETC(s
, message
);
1706 STREAM_GETW(s
, safi
);
1707 re
->uptime
= time(NULL
);
1710 memset(&p
, 0, sizeof(p
));
1711 p
.family
= AF_INET6
;
1712 STREAM_GETC(s
, p
.prefixlen
);
1713 if (p
.prefixlen
> IPV6_MAX_BITLEN
) {
1714 zlog_warn("%s: Specified prefix length %d is to large for v6 prefix",
1715 __PRETTY_FUNCTION__
, p
.prefixlen
);
1716 XFREE(MTYPE_RE
, re
);
1719 STREAM_GET(&p
.u
.prefix6
, s
, PSIZE(p
.prefixlen
));
1721 if (CHECK_FLAG(message
, ZAPI_MESSAGE_SRCPFX
)) {
1722 memset(&src_p
, 0, sizeof(src_p
));
1723 src_p
.family
= AF_INET6
;
1724 STREAM_GETC(s
, src_p
.prefixlen
);
1725 if (src_p
.prefixlen
> IPV6_MAX_BITLEN
) {
1726 zlog_warn("%s: Specified src prefix length %d is to large for v6 prefix",
1727 __PRETTY_FUNCTION__
, src_p
.prefixlen
);
1728 XFREE(MTYPE_RE
, re
);
1731 STREAM_GET(&src_p
.prefix
, s
, PSIZE(src_p
.prefixlen
));
1736 /* We need to give nh-addr, nh-ifindex with the same next-hop object
1737 * to the re to ensure that IPv6 multipathing works; need to coalesce
1738 * these. Clients should send the same number of paired set of
1739 * next-hop-addr/next-hop-ifindices. */
1740 if (CHECK_FLAG(message
, ZAPI_MESSAGE_NEXTHOP
)) {
1741 unsigned int nh_count
= 0;
1742 unsigned int if_count
= 0;
1743 unsigned int max_nh_if
= 0;
1745 STREAM_GETC(s
, nexthop_num
);
1746 zserv_nexthop_num_warn(__func__
, (const struct prefix
*)&p
,
1749 if (CHECK_FLAG(message
, ZAPI_MESSAGE_LABEL
))
1750 label_type
= lsp_type_from_re_type(client
->proto
);
1752 for (i
= 0; i
< nexthop_num
; i
++) {
1753 STREAM_GETC(s
, nexthop_type
);
1755 switch (nexthop_type
) {
1756 case NEXTHOP_TYPE_IPV6
:
1757 STREAM_GET(&nhop_addr
, s
, 16);
1758 if (nh_count
< MULTIPATH_NUM
) {
1759 /* For labeled-unicast, each nexthop is
1760 * followed by label. */
1761 if (CHECK_FLAG(message
,
1762 ZAPI_MESSAGE_LABEL
)) {
1763 STREAM_GETL(s
, label
);
1764 labels
[nh_count
] = label
;
1766 nexthops
[nh_count
++] = nhop_addr
;
1769 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1770 STREAM_GET(&nhop_addr
, s
, 16);
1771 STREAM_GETL(s
, ifindex
);
1772 route_entry_nexthop_ipv6_ifindex_add(
1773 re
, &nhop_addr
, ifindex
);
1775 case NEXTHOP_TYPE_IFINDEX
:
1776 if (if_count
< multipath_num
) {
1777 STREAM_GETL(s
, ifindices
[if_count
++]);
1780 case NEXTHOP_TYPE_BLACKHOLE
:
1781 route_entry_nexthop_blackhole_add(re
, bh_type
);
1784 zlog_warn("%s: Please use ZEBRA_ROUTE_ADD if you want to pass non v6 nexthops",
1785 __PRETTY_FUNCTION__
);
1786 nexthops_free(re
->nexthop
);
1787 XFREE(MTYPE_RE
, re
);
1792 max_nh_if
= (nh_count
> if_count
) ? nh_count
: if_count
;
1793 for (i
= 0; i
< max_nh_if
; i
++) {
1795 && !IN6_IS_ADDR_UNSPECIFIED(&nexthops
[i
])) {
1796 if ((i
< if_count
) && ifindices
[i
])
1798 route_entry_nexthop_ipv6_ifindex_add(
1802 nexthop
= route_entry_nexthop_ipv6_add(
1804 if (CHECK_FLAG(message
, ZAPI_MESSAGE_LABEL
))
1805 nexthop_add_labels(nexthop
, label_type
,
1808 if ((i
< if_count
) && ifindices
[i
])
1809 route_entry_nexthop_ifindex_add(
1816 if (CHECK_FLAG(message
, ZAPI_MESSAGE_DISTANCE
))
1817 STREAM_GETC(s
, re
->distance
);
1820 if (CHECK_FLAG(message
, ZAPI_MESSAGE_METRIC
))
1821 STREAM_GETL(s
, re
->metric
);
1824 if (CHECK_FLAG(message
, ZAPI_MESSAGE_TAG
))
1825 STREAM_GETL(s
, re
->tag
);
1829 if (CHECK_FLAG(message
, ZAPI_MESSAGE_MTU
))
1830 STREAM_GETL(s
, re
->mtu
);
1835 re
->vrf_id
= zvrf_id(zvrf
);
1836 re
->table
= zvrf
->table_id
;
1838 ret
= rib_add_multipath(AFI_IP6
, safi
, &p
, src_pp
, re
);
1841 client
->v6_route_add_cnt
++;
1843 client
->v6_route_upd8_cnt
++;
1848 nexthops_free(re
->nexthop
);
1849 XFREE(MTYPE_RE
, re
);
1854 /* Zebra server IPv6 prefix delete function. */
1855 static int zread_ipv6_delete(struct zserv
*client
, u_short length
,
1856 struct zebra_vrf
*zvrf
)
1859 struct zapi_ipv6 api
;
1861 struct prefix_ipv6 src_p
, *src_pp
;
1865 /* Type, flags, message. */
1866 STREAM_GETC(s
, api
.type
);
1867 STREAM_GETW(s
, api
.instance
);
1868 STREAM_GETL(s
, api
.flags
);
1869 STREAM_GETC(s
, api
.message
);
1870 STREAM_GETW(s
, api
.safi
);
1873 memset(&p
, 0, sizeof(struct prefix
));
1874 p
.family
= AF_INET6
;
1875 STREAM_GETC(s
, p
.prefixlen
);
1876 STREAM_GET(&p
.u
.prefix6
, s
, PSIZE(p
.prefixlen
));
1878 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
)) {
1879 memset(&src_p
, 0, sizeof(struct prefix_ipv6
));
1880 src_p
.family
= AF_INET6
;
1881 STREAM_GETC(s
, src_p
.prefixlen
);
1882 STREAM_GET(&src_p
.prefix
, s
, PSIZE(src_p
.prefixlen
));
1887 rib_delete(AFI_IP6
, api
.safi
, zvrf_id(zvrf
), api
.type
, api
.instance
,
1888 api
.flags
, &p
, src_pp
, NULL
, client
->rtm_table
, 0, false);
1890 client
->v6_route_del_cnt
++;
1896 /* Register zebra server router-id information. Send current router-id */
1897 static int zread_router_id_add(struct zserv
*client
, u_short length
,
1898 struct zebra_vrf
*zvrf
)
1902 /* Router-id information is needed. */
1903 vrf_bitmap_set(client
->ridinfo
, zvrf_id(zvrf
));
1905 router_id_get(&p
, zvrf_id(zvrf
));
1907 return zsend_router_id_update(client
, &p
, zvrf_id(zvrf
));
1910 /* Unregister zebra server router-id information. */
1911 static int zread_router_id_delete(struct zserv
*client
, u_short length
,
1912 struct zebra_vrf
*zvrf
)
1914 vrf_bitmap_unset(client
->ridinfo
, zvrf_id(zvrf
));
1918 /* Tie up route-type and client->sock */
1919 static void zread_hello(struct zserv
*client
)
1921 /* type of protocol (lib/zebra.h) */
1926 STREAM_GETC(client
->ibuf
, proto
);
1927 STREAM_GETW(client
->ibuf
, instance
);
1928 STREAM_GETC(client
->ibuf
, notify
);
1930 client
->notify_owner
= true;
1932 /* accept only dynamic routing protocols */
1933 if ((proto
< ZEBRA_ROUTE_MAX
) && (proto
> ZEBRA_ROUTE_STATIC
)) {
1935 "client %d says hello and bids fair to announce only %s routes",
1936 client
->sock
, zebra_route_string(proto
));
1938 zlog_notice("client protocol instance %d", instance
);
1940 client
->proto
= proto
;
1941 client
->instance
= instance
;
1948 /* Unregister all information in a VRF. */
1949 static int zread_vrf_unregister(struct zserv
*client
, u_short length
,
1950 struct zebra_vrf
*zvrf
)
1955 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
1956 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
1957 vrf_bitmap_unset(client
->redist
[afi
][i
], zvrf_id(zvrf
));
1958 vrf_bitmap_unset(client
->redist_default
, zvrf_id(zvrf
));
1959 vrf_bitmap_unset(client
->ifinfo
, zvrf_id(zvrf
));
1960 vrf_bitmap_unset(client
->ridinfo
, zvrf_id(zvrf
));
1965 static void zread_mpls_labels(int command
, struct zserv
*client
, u_short length
,
1966 struct zebra_vrf
*zvrf
)
1969 enum lsp_types_t type
;
1970 struct prefix prefix
;
1971 enum nexthop_types_t gtype
;
1974 mpls_label_t in_label
, out_label
;
1977 /* Get input stream. */
1981 STREAM_GETC(s
, type
);
1982 STREAM_GETL(s
, prefix
.family
);
1983 switch (prefix
.family
) {
1985 STREAM_GET(&prefix
.u
.prefix4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
1986 STREAM_GETC(s
, prefix
.prefixlen
);
1987 if (prefix
.prefixlen
> IPV4_MAX_BITLEN
) {
1988 zlog_warn("%s: Specified prefix length %d is greater than a v4 address can support",
1989 __PRETTY_FUNCTION__
,
1993 STREAM_GET(&gate
.ipv4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
1996 STREAM_GET(&prefix
.u
.prefix6
, s
, 16);
1997 STREAM_GETC(s
, prefix
.prefixlen
);
1998 if (prefix
.prefixlen
> IPV6_MAX_BITLEN
) {
1999 zlog_warn("%s: Specified prefix length %d is greater than a v6 address can support",
2000 __PRETTY_FUNCTION__
,
2004 STREAM_GET(&gate
.ipv6
, s
, 16);
2007 zlog_warn("%s: Specified AF %d is not supported for this call",
2008 __PRETTY_FUNCTION__
, prefix
.family
);
2011 STREAM_GETL(s
, ifindex
);
2012 STREAM_GETC(s
, distance
);
2013 STREAM_GETL(s
, in_label
);
2014 STREAM_GETL(s
, out_label
);
2016 switch (prefix
.family
) {
2019 gtype
= NEXTHOP_TYPE_IPV4_IFINDEX
;
2021 gtype
= NEXTHOP_TYPE_IPV4
;
2025 gtype
= NEXTHOP_TYPE_IPV6_IFINDEX
;
2027 gtype
= NEXTHOP_TYPE_IPV6
;
2036 if (command
== ZEBRA_MPLS_LABELS_ADD
) {
2037 mpls_lsp_install(zvrf
, type
, in_label
, out_label
, gtype
, &gate
,
2039 mpls_ftn_update(1, zvrf
, type
, &prefix
, gtype
, &gate
, ifindex
,
2040 distance
, out_label
);
2041 } else if (command
== ZEBRA_MPLS_LABELS_DELETE
) {
2042 mpls_lsp_uninstall(zvrf
, type
, in_label
, gtype
, &gate
, ifindex
);
2043 mpls_ftn_update(0, zvrf
, type
, &prefix
, gtype
, &gate
, ifindex
,
2044 distance
, out_label
);
2049 /* Send response to a label manager connect request to client */
2050 static int zsend_label_manager_connect_response(struct zserv
*client
,
2051 vrf_id_t vrf_id
, u_short result
)
2058 zserv_create_header(s
, ZEBRA_LABEL_MANAGER_CONNECT
, vrf_id
);
2061 stream_putc(s
, result
);
2063 /* Write packet size. */
2064 stream_putw_at(s
, 0, stream_get_endp(s
));
2066 return writen(client
->sock
, s
->data
, stream_get_endp(s
));
2069 static void zread_label_manager_connect(struct zserv
*client
, vrf_id_t vrf_id
)
2072 /* type of protocol (lib/zebra.h) */
2076 /* Get input stream. */
2080 STREAM_GETC(s
, proto
);
2081 STREAM_GETW(s
, instance
);
2083 /* accept only dynamic routing protocols */
2084 if ((proto
>= ZEBRA_ROUTE_MAX
) || (proto
<= ZEBRA_ROUTE_STATIC
)) {
2085 zlog_err("client %d has wrong protocol %s", client
->sock
,
2086 zebra_route_string(proto
));
2087 zsend_label_manager_connect_response(client
, vrf_id
, 1);
2090 zlog_notice("client %d with instance %u connected as %s", client
->sock
,
2091 instance
, zebra_route_string(proto
));
2092 client
->proto
= proto
;
2093 client
->instance
= instance
;
2096 Release previous labels of same protocol and instance.
2097 This is done in case it restarted from an unexpected shutdown.
2099 release_daemon_chunks(proto
, instance
);
2102 " Label Manager client connected: sock %d, proto %s, instance %u",
2103 client
->sock
, zebra_route_string(proto
), instance
);
2104 /* send response back */
2105 zsend_label_manager_connect_response(client
, vrf_id
, 0);
2110 /* Send response to a get label chunk request to client */
2111 static int zsend_assign_label_chunk_response(struct zserv
*client
,
2113 struct label_manager_chunk
*lmc
)
2120 zserv_create_header(s
, ZEBRA_GET_LABEL_CHUNK
, vrf_id
);
2124 stream_putc(s
, lmc
->keep
);
2125 /* start and end labels */
2126 stream_putl(s
, lmc
->start
);
2127 stream_putl(s
, lmc
->end
);
2130 /* Write packet size. */
2131 stream_putw_at(s
, 0, stream_get_endp(s
));
2133 return writen(client
->sock
, s
->data
, stream_get_endp(s
));
2136 static void zread_get_label_chunk(struct zserv
*client
, vrf_id_t vrf_id
)
2141 struct label_manager_chunk
*lmc
;
2143 /* Get input stream. */
2147 STREAM_GETC(s
, keep
);
2148 STREAM_GETL(s
, size
);
2150 lmc
= assign_label_chunk(client
->proto
, client
->instance
, keep
, size
);
2152 zlog_err("%s: Unable to assign Label Chunk of size %u",
2155 zlog_debug("Assigned Label Chunk %u - %u to %u", lmc
->start
,
2157 /* send response back */
2158 zsend_assign_label_chunk_response(client
, vrf_id
, lmc
);
2164 static void zread_release_label_chunk(struct zserv
*client
)
2167 uint32_t start
, end
;
2169 /* Get input stream. */
2173 STREAM_GETL(s
, start
);
2174 STREAM_GETL(s
, end
);
2176 release_label_chunk(client
->proto
, client
->instance
, start
, end
);
2181 static void zread_label_manager_request(int cmd
, struct zserv
*client
,
2182 struct zebra_vrf
*zvrf
)
2184 /* to avoid sending other messages like ZERBA_INTERFACE_UP */
2185 if (cmd
== ZEBRA_LABEL_MANAGER_CONNECT
)
2186 client
->is_synchronous
= 1;
2188 /* external label manager */
2190 zread_relay_label_manager_request(cmd
, client
,
2192 /* this is a label manager */
2194 if (cmd
== ZEBRA_LABEL_MANAGER_CONNECT
)
2195 zread_label_manager_connect(client
,
2198 /* Sanity: don't allow 'unidentified' requests */
2199 if (!client
->proto
) {
2201 "Got label request from an unidentified client");
2204 if (cmd
== ZEBRA_GET_LABEL_CHUNK
)
2205 zread_get_label_chunk(client
,
2207 else if (cmd
== ZEBRA_RELEASE_LABEL_CHUNK
)
2208 zread_release_label_chunk(client
);
2213 static int zread_pseudowire(int command
, struct zserv
*client
, u_short length
,
2214 struct zebra_vrf
*zvrf
)
2217 char ifname
[IF_NAMESIZE
];
2221 union g_addr nexthop
;
2222 uint32_t local_label
;
2223 uint32_t remote_label
;
2225 union pw_protocol_fields data
;
2227 struct zebra_pw
*pw
;
2229 /* Get input stream. */
2233 STREAM_GET(ifname
, s
, IF_NAMESIZE
);
2234 STREAM_GETL(s
, ifindex
);
2235 STREAM_GETL(s
, type
);
2239 STREAM_GET(&nexthop
.ipv4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
2242 STREAM_GET(&nexthop
.ipv6
, s
, 16);
2247 STREAM_GETL(s
, local_label
);
2248 STREAM_GETL(s
, remote_label
);
2249 STREAM_GETC(s
, flags
);
2250 STREAM_GET(&data
, s
, sizeof(data
));
2251 protocol
= client
->proto
;
2253 pw
= zebra_pw_find(zvrf
, ifname
);
2257 zlog_warn("%s: pseudowire %s already exists [%s]",
2259 zserv_command_string(command
));
2263 zebra_pw_add(zvrf
, ifname
, protocol
, client
);
2265 case ZEBRA_PW_DELETE
:
2267 zlog_warn("%s: pseudowire %s not found [%s]", __func__
,
2268 ifname
, zserv_command_string(command
));
2272 zebra_pw_del(zvrf
, pw
);
2275 case ZEBRA_PW_UNSET
:
2277 zlog_warn("%s: pseudowire %s not found [%s]", __func__
,
2278 ifname
, zserv_command_string(command
));
2286 case ZEBRA_PW_UNSET
:
2291 zebra_pw_change(pw
, ifindex
, type
, af
, &nexthop
, local_label
,
2292 remote_label
, flags
, &data
);
2300 /* Cleanup registered nexthops (across VRFs) upon client disconnect. */
2301 static void zebra_client_close_cleanup_rnh(struct zserv
*client
)
2304 struct zebra_vrf
*zvrf
;
2306 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
2307 if ((zvrf
= vrf
->info
) != NULL
) {
2308 zebra_cleanup_rnh_client(zvrf_id(zvrf
), AF_INET
, client
,
2310 zebra_cleanup_rnh_client(zvrf_id(zvrf
), AF_INET6
,
2311 client
, RNH_NEXTHOP_TYPE
);
2312 zebra_cleanup_rnh_client(zvrf_id(zvrf
), AF_INET
, client
,
2313 RNH_IMPORT_CHECK_TYPE
);
2314 zebra_cleanup_rnh_client(zvrf_id(zvrf
), AF_INET6
,
2315 client
, RNH_IMPORT_CHECK_TYPE
);
2316 if (client
->proto
== ZEBRA_ROUTE_LDP
) {
2317 hash_iterate(zvrf
->lsp_table
,
2318 mpls_ldp_lsp_uninstall_all
,
2320 mpls_ldp_ftn_uninstall_all(zvrf
, AFI_IP
);
2321 mpls_ldp_ftn_uninstall_all(zvrf
, AFI_IP6
);
2327 /* free zebra client information. */
2328 static void zebra_client_free(struct zserv
*client
)
2330 /* Send client de-registration to BFD */
2331 zebra_ptm_bfd_client_deregister(client
->proto
);
2333 /* Cleanup any registered nexthops - across all VRFs. */
2334 zebra_client_close_cleanup_rnh(client
);
2336 /* Release Label Manager chunks */
2337 release_daemon_chunks(client
->proto
, client
->instance
);
2339 /* Cleanup any FECs registered by this client. */
2340 zebra_mpls_cleanup_fecs_for_client(vrf_info_lookup(VRF_DEFAULT
),
2343 /* Remove pseudowires associated with this client */
2344 zebra_pw_client_close(client
);
2346 /* Close file descriptor. */
2348 unsigned long nroutes
;
2350 close(client
->sock
);
2351 nroutes
= rib_score_proto(client
->proto
, client
->instance
);
2353 "client %d disconnected. %lu %s routes removed from the rib",
2354 client
->sock
, nroutes
,
2355 zebra_route_string(client
->proto
));
2359 /* Free stream buffers. */
2361 stream_free(client
->ibuf
);
2363 stream_free(client
->obuf
);
2365 buffer_free(client
->wb
);
2367 /* Release threads. */
2369 thread_cancel(client
->t_read
);
2370 if (client
->t_write
)
2371 thread_cancel(client
->t_write
);
2372 if (client
->t_suicide
)
2373 thread_cancel(client
->t_suicide
);
2376 for (afi_t afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
2377 for (int i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
2378 vrf_bitmap_free(client
->redist
[afi
][i
]);
2380 vrf_bitmap_free(client
->redist_default
);
2381 vrf_bitmap_free(client
->ifinfo
);
2382 vrf_bitmap_free(client
->ridinfo
);
2384 XFREE(MTYPE_TMP
, client
);
2387 static void zebra_client_close(struct zserv
*client
)
2389 listnode_delete(zebrad
.client_list
, client
);
2390 zebra_client_free(client
);
2393 /* Make new client. */
2394 static void zebra_client_create(int sock
)
2396 struct zserv
*client
;
2400 client
= XCALLOC(MTYPE_TMP
, sizeof(struct zserv
));
2402 /* Make client input/output buffer. */
2403 client
->sock
= sock
;
2404 client
->ibuf
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
2405 client
->obuf
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
2406 client
->wb
= buffer_new(0);
2408 /* Set table number. */
2409 client
->rtm_table
= zebrad
.rtm_table_default
;
2411 client
->connect_time
= monotime(NULL
);
2412 /* Initialize flags */
2413 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
2414 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
2415 client
->redist
[afi
][i
] = vrf_bitmap_init();
2416 client
->redist_default
= vrf_bitmap_init();
2417 client
->ifinfo
= vrf_bitmap_init();
2418 client
->ridinfo
= vrf_bitmap_init();
2420 /* by default, it's not a synchronous client */
2421 client
->is_synchronous
= 0;
2423 /* Add this client to linked list. */
2424 listnode_add(zebrad
.client_list
, client
);
2426 /* Make new read thread. */
2427 zebra_event(ZEBRA_READ
, sock
, client
);
2429 zebra_vrf_update_all(client
);
2432 static int zread_interface_set_master(struct zserv
*client
,
2435 struct interface
*master
;
2436 struct interface
*slave
;
2437 struct stream
*s
= client
->ibuf
;
2441 STREAM_GETW(s
, vrf_id
);
2442 STREAM_GETL(s
, ifindex
);
2443 master
= if_lookup_by_index(ifindex
, vrf_id
);
2445 STREAM_GETW(s
, vrf_id
);
2446 STREAM_GETL(s
, ifindex
);
2447 slave
= if_lookup_by_index(ifindex
, vrf_id
);
2449 if (!master
|| !slave
)
2452 kernel_interface_set_master(master
, slave
);
2458 static inline void zserv_handle_commands(struct zserv
*client
,
2461 struct zebra_vrf
*zvrf
)
2464 case ZEBRA_ROUTER_ID_ADD
:
2465 zread_router_id_add(client
, length
, zvrf
);
2467 case ZEBRA_ROUTER_ID_DELETE
:
2468 zread_router_id_delete(client
, length
, zvrf
);
2470 case ZEBRA_INTERFACE_ADD
:
2471 zread_interface_add(client
, length
, zvrf
);
2473 case ZEBRA_INTERFACE_DELETE
:
2474 zread_interface_delete(client
, length
, zvrf
);
2476 case ZEBRA_ROUTE_ADD
:
2477 zread_route_add(client
, length
, zvrf
);
2479 case ZEBRA_ROUTE_DELETE
:
2480 zread_route_del(client
, length
, zvrf
);
2482 case ZEBRA_IPV4_ROUTE_ADD
:
2483 zread_ipv4_add(client
, length
, zvrf
);
2485 case ZEBRA_IPV4_ROUTE_DELETE
:
2486 zread_ipv4_delete(client
, length
, zvrf
);
2488 case ZEBRA_IPV4_ROUTE_IPV6_NEXTHOP_ADD
:
2489 zread_ipv4_route_ipv6_nexthop_add(client
, length
, zvrf
);
2491 case ZEBRA_IPV6_ROUTE_ADD
:
2492 zread_ipv6_add(client
, length
, zvrf
);
2494 case ZEBRA_IPV6_ROUTE_DELETE
:
2495 zread_ipv6_delete(client
, length
, zvrf
);
2497 case ZEBRA_REDISTRIBUTE_ADD
:
2498 zebra_redistribute_add(command
, client
, length
, zvrf
);
2500 case ZEBRA_REDISTRIBUTE_DELETE
:
2501 zebra_redistribute_delete(command
, client
, length
, zvrf
);
2503 case ZEBRA_REDISTRIBUTE_DEFAULT_ADD
:
2504 zebra_redistribute_default_add(command
, client
, length
, zvrf
);
2506 case ZEBRA_REDISTRIBUTE_DEFAULT_DELETE
:
2507 zebra_redistribute_default_delete(command
, client
, length
,
2510 case ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB
:
2511 zread_ipv4_nexthop_lookup_mrib(client
, length
, zvrf
);
2514 zread_hello(client
);
2516 case ZEBRA_NEXTHOP_REGISTER
:
2517 zserv_rnh_register(client
, length
, RNH_NEXTHOP_TYPE
,
2520 case ZEBRA_NEXTHOP_UNREGISTER
:
2521 zserv_rnh_unregister(client
, length
, RNH_NEXTHOP_TYPE
,
2524 case ZEBRA_IMPORT_ROUTE_REGISTER
:
2525 zserv_rnh_register(client
, length
, RNH_IMPORT_CHECK_TYPE
,
2528 case ZEBRA_IMPORT_ROUTE_UNREGISTER
:
2529 zserv_rnh_unregister(client
, length
,
2530 RNH_IMPORT_CHECK_TYPE
, zvrf
);
2532 case ZEBRA_BFD_DEST_UPDATE
:
2533 case ZEBRA_BFD_DEST_REGISTER
:
2534 zebra_ptm_bfd_dst_register(client
, length
, command
, zvrf
);
2536 case ZEBRA_BFD_DEST_DEREGISTER
:
2537 zebra_ptm_bfd_dst_deregister(client
, length
, zvrf
);
2539 case ZEBRA_VRF_UNREGISTER
:
2540 zread_vrf_unregister(client
, length
, zvrf
);
2542 case ZEBRA_BFD_CLIENT_REGISTER
:
2543 zebra_ptm_bfd_client_register(client
, length
);
2545 case ZEBRA_INTERFACE_ENABLE_RADV
:
2546 #if defined(HAVE_RTADV)
2547 zebra_interface_radv_set(client
, length
, zvrf
, 1);
2550 case ZEBRA_INTERFACE_DISABLE_RADV
:
2551 #if defined(HAVE_RTADV)
2552 zebra_interface_radv_set(client
, length
, zvrf
, 0);
2555 case ZEBRA_MPLS_LABELS_ADD
:
2556 case ZEBRA_MPLS_LABELS_DELETE
:
2557 zread_mpls_labels(command
, client
, length
, zvrf
);
2559 case ZEBRA_IPMR_ROUTE_STATS
:
2560 zebra_ipmr_route_stats(client
, length
, zvrf
);
2562 case ZEBRA_LABEL_MANAGER_CONNECT
:
2563 case ZEBRA_GET_LABEL_CHUNK
:
2564 case ZEBRA_RELEASE_LABEL_CHUNK
:
2565 zread_label_manager_request(command
, client
, zvrf
);
2567 case ZEBRA_FEC_REGISTER
:
2568 zserv_fec_register(client
, length
);
2570 case ZEBRA_FEC_UNREGISTER
:
2571 zserv_fec_unregister(client
, length
);
2573 case ZEBRA_ADVERTISE_DEFAULT_GW
:
2574 zebra_vxlan_advertise_gw_macip(client
, length
, zvrf
);
2576 case ZEBRA_ADVERTISE_ALL_VNI
:
2577 zebra_vxlan_advertise_all_vni(client
, length
, zvrf
);
2579 case ZEBRA_REMOTE_VTEP_ADD
:
2580 zebra_vxlan_remote_vtep_add(client
, length
, zvrf
);
2582 case ZEBRA_REMOTE_VTEP_DEL
:
2583 zebra_vxlan_remote_vtep_del(client
, length
, zvrf
);
2585 case ZEBRA_REMOTE_MACIP_ADD
:
2586 zebra_vxlan_remote_macip_add(client
, length
, zvrf
);
2588 case ZEBRA_REMOTE_MACIP_DEL
:
2589 zebra_vxlan_remote_macip_del(client
, length
, zvrf
);
2591 case ZEBRA_INTERFACE_SET_MASTER
:
2592 zread_interface_set_master(client
, length
);
2595 case ZEBRA_PW_DELETE
:
2597 case ZEBRA_PW_UNSET
:
2598 zread_pseudowire(command
, client
, length
, zvrf
);
2601 zlog_info("Zebra received unknown command %d", command
);
2606 #if defined(HANDLE_ZAPI_FUZZING)
2607 static void zserv_write_incoming(struct stream
*orig
, uint16_t command
)
2609 char fname
[MAXPATHLEN
];
2610 struct stream
*copy
;
2613 copy
= stream_dup(orig
);
2614 stream_set_getp(copy
, 0);
2616 zserv_privs
.change(ZPRIVS_RAISE
);
2617 snprintf(fname
, MAXPATHLEN
, "%s/%u", DAEMON_VTY_DIR
, command
);
2618 fd
= open(fname
, O_CREAT
| O_WRONLY
| O_EXCL
, 0644);
2619 stream_flush(copy
, fd
);
2621 zserv_privs
.change(ZPRIVS_LOWER
);
2626 /* Handler of zebra service request. */
2627 static int zebra_client_read(struct thread
*thread
)
2630 struct zserv
*client
;
2632 uint16_t length
, command
;
2633 uint8_t marker
, version
;
2635 struct zebra_vrf
*zvrf
;
2636 #if defined(HANDLE_ZAPI_FUZZING)
2639 int packets
= zebrad
.packets_to_process
;
2642 /* Get thread data. Reset reading thread because I'm running. */
2643 sock
= THREAD_FD(thread
);
2644 client
= THREAD_ARG(thread
);
2645 client
->t_read
= NULL
;
2647 if (client
->t_suicide
) {
2648 zebra_client_close(client
);
2653 /* Read length and command (if we don't have it already). */
2654 if ((already
= stream_get_endp(client
->ibuf
))
2655 < ZEBRA_HEADER_SIZE
) {
2658 stream_read_try(client
->ibuf
, sock
,
2659 ZEBRA_HEADER_SIZE
- already
))
2662 if (IS_ZEBRA_DEBUG_EVENT
)
2663 zlog_debug("connection closed socket [%d]",
2665 zebra_client_close(client
);
2668 if (nbyte
!= (ssize_t
)(ZEBRA_HEADER_SIZE
- already
)) {
2669 /* Try again later. */
2670 zebra_event(ZEBRA_READ
, sock
, client
);
2673 already
= ZEBRA_HEADER_SIZE
;
2676 /* Reset to read from the beginning of the incoming packet. */
2677 stream_set_getp(client
->ibuf
, 0);
2679 /* Fetch header values */
2680 STREAM_GETW(client
->ibuf
, length
);
2681 STREAM_GETC(client
->ibuf
, marker
);
2682 STREAM_GETC(client
->ibuf
, version
);
2683 STREAM_GETW(client
->ibuf
, vrf_id
);
2684 STREAM_GETW(client
->ibuf
, command
);
2686 if (marker
!= ZEBRA_HEADER_MARKER
|| version
!= ZSERV_VERSION
) {
2688 "%s: socket %d version mismatch, marker %d, version %d",
2689 __func__
, sock
, marker
, version
);
2690 zebra_client_close(client
);
2693 if (length
< ZEBRA_HEADER_SIZE
) {
2695 "%s: socket %d message length %u is less than header size %d",
2696 __func__
, sock
, length
, ZEBRA_HEADER_SIZE
);
2697 zebra_client_close(client
);
2700 if (length
> STREAM_SIZE(client
->ibuf
)) {
2702 "%s: socket %d message length %u exceeds buffer size %lu",
2703 __func__
, sock
, length
,
2704 (u_long
)STREAM_SIZE(client
->ibuf
));
2705 zebra_client_close(client
);
2709 /* Read rest of data. */
2710 if (already
< length
) {
2712 if (((nbyte
= stream_read_try(client
->ibuf
, sock
,
2716 if (IS_ZEBRA_DEBUG_EVENT
)
2718 "connection closed [%d] when reading zebra data",
2720 zebra_client_close(client
);
2723 if (nbyte
!= (ssize_t
)(length
- already
)) {
2724 /* Try again later. */
2725 zebra_event(ZEBRA_READ
, sock
, client
);
2730 #if defined(HANDLE_ZAPI_FUZZING)
2731 zserv_write_incoming(client
->ibuf
, command
);
2733 length
-= ZEBRA_HEADER_SIZE
;
2735 /* Debug packet information. */
2736 if (IS_ZEBRA_DEBUG_EVENT
)
2737 zlog_debug("zebra message comes from socket [%d]", sock
);
2739 if (IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
)
2740 zlog_debug("zebra message received [%s] %d in VRF %u",
2741 zserv_command_string(command
), length
, vrf_id
);
2743 client
->last_read_time
= monotime(NULL
);
2744 client
->last_read_cmd
= command
;
2746 zvrf
= zebra_vrf_lookup_by_id(vrf_id
);
2748 if (IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
)
2749 zlog_debug("zebra received unknown VRF[%u]", vrf_id
);
2750 goto zclient_read_out
;
2753 zserv_handle_commands(client
, command
, length
, zvrf
);
2755 if (client
->t_suicide
) {
2756 /* No need to wait for thread callback, just kill immediately.
2758 zebra_client_close(client
);
2762 stream_reset(client
->ibuf
);
2767 stream_reset(client
->ibuf
);
2768 zebra_event(ZEBRA_READ
, sock
, client
);
2773 /* Accept code of zebra server socket. */
2774 static int zebra_accept(struct thread
*thread
)
2778 struct sockaddr_in client
;
2781 accept_sock
= THREAD_FD(thread
);
2783 /* Reregister myself. */
2784 zebra_event(ZEBRA_SERV
, accept_sock
, NULL
);
2786 len
= sizeof(struct sockaddr_in
);
2787 client_sock
= accept(accept_sock
, (struct sockaddr
*)&client
, &len
);
2789 if (client_sock
< 0) {
2790 zlog_warn("Can't accept zebra socket: %s",
2791 safe_strerror(errno
));
2795 /* Make client socket non-blocking. */
2796 set_nonblocking(client_sock
);
2798 /* Create new zebra client. */
2799 zebra_client_create(client_sock
);
2804 /* Make zebra server socket, wiping any existing one (see bug #403). */
2805 void zebra_zserv_socket_init(char *path
)
2810 struct sockaddr_storage sa
;
2813 if (!frr_zclient_addr(&sa
, &sa_len
, path
))
2814 /* should be caught in zebra main() */
2818 old_mask
= umask(0077);
2820 /* Make UNIX domain socket. */
2821 sock
= socket(sa
.ss_family
, SOCK_STREAM
, 0);
2823 zlog_warn("Can't create zserv socket: %s",
2824 safe_strerror(errno
));
2826 "zebra can't provide full functionality due to above error");
2830 if (sa
.ss_family
!= AF_UNIX
) {
2831 sockopt_reuseaddr(sock
);
2832 sockopt_reuseport(sock
);
2834 struct sockaddr_un
*suna
= (struct sockaddr_un
*)&sa
;
2835 if (suna
->sun_path
[0])
2836 unlink(suna
->sun_path
);
2839 zserv_privs
.change(ZPRIVS_RAISE
);
2840 setsockopt_so_recvbuf(sock
, 1048576);
2841 setsockopt_so_sendbuf(sock
, 1048576);
2842 zserv_privs
.change(ZPRIVS_LOWER
);
2844 if (sa
.ss_family
!= AF_UNIX
&& zserv_privs
.change(ZPRIVS_RAISE
))
2845 zlog_err("Can't raise privileges");
2847 ret
= bind(sock
, (struct sockaddr
*)&sa
, sa_len
);
2849 zlog_warn("Can't bind zserv socket on %s: %s", path
,
2850 safe_strerror(errno
));
2852 "zebra can't provide full functionality due to above error");
2856 if (sa
.ss_family
!= AF_UNIX
&& zserv_privs
.change(ZPRIVS_LOWER
))
2857 zlog_err("Can't lower privileges");
2859 ret
= listen(sock
, 5);
2861 zlog_warn("Can't listen to zserv socket %s: %s", path
,
2862 safe_strerror(errno
));
2864 "zebra can't provide full functionality due to above error");
2871 zebra_event(ZEBRA_SERV
, sock
, NULL
);
2875 static void zebra_event(enum event event
, int sock
, struct zserv
*client
)
2879 thread_add_read(zebrad
.master
, zebra_accept
, client
, sock
,
2883 client
->t_read
= NULL
;
2884 thread_add_read(zebrad
.master
, zebra_client_read
, client
, sock
,
2893 #define ZEBRA_TIME_BUF 32
2894 static char *zserv_time_buf(time_t *time1
, char *buf
, int buflen
)
2899 assert(buf
!= NULL
);
2900 assert(buflen
>= ZEBRA_TIME_BUF
);
2901 assert(time1
!= NULL
);
2904 snprintf(buf
, buflen
, "never ");
2908 now
= monotime(NULL
);
2912 if (now
< ONE_DAY_SECOND
)
2913 snprintf(buf
, buflen
, "%02d:%02d:%02d", tm
->tm_hour
, tm
->tm_min
,
2915 else if (now
< ONE_WEEK_SECOND
)
2916 snprintf(buf
, buflen
, "%dd%02dh%02dm", tm
->tm_yday
, tm
->tm_hour
,
2919 snprintf(buf
, buflen
, "%02dw%dd%02dh", tm
->tm_yday
/ 7,
2920 tm
->tm_yday
- ((tm
->tm_yday
/ 7) * 7), tm
->tm_hour
);
2924 static void zebra_show_client_detail(struct vty
*vty
, struct zserv
*client
)
2926 char cbuf
[ZEBRA_TIME_BUF
], rbuf
[ZEBRA_TIME_BUF
];
2927 char wbuf
[ZEBRA_TIME_BUF
], nhbuf
[ZEBRA_TIME_BUF
], mbuf
[ZEBRA_TIME_BUF
];
2929 vty_out(vty
, "Client: %s", zebra_route_string(client
->proto
));
2930 if (client
->instance
)
2931 vty_out(vty
, " Instance: %d", client
->instance
);
2934 vty_out(vty
, "------------------------ \n");
2935 vty_out(vty
, "FD: %d \n", client
->sock
);
2936 vty_out(vty
, "Route Table ID: %d \n", client
->rtm_table
);
2938 vty_out(vty
, "Connect Time: %s \n",
2939 zserv_time_buf(&client
->connect_time
, cbuf
, ZEBRA_TIME_BUF
));
2940 if (client
->nh_reg_time
) {
2941 vty_out(vty
, "Nexthop Registry Time: %s \n",
2942 zserv_time_buf(&client
->nh_reg_time
, nhbuf
,
2944 if (client
->nh_last_upd_time
)
2945 vty_out(vty
, "Nexthop Last Update Time: %s \n",
2946 zserv_time_buf(&client
->nh_last_upd_time
, mbuf
,
2949 vty_out(vty
, "No Nexthop Update sent\n");
2951 vty_out(vty
, "Not registered for Nexthop Updates\n");
2953 vty_out(vty
, "Last Msg Rx Time: %s \n",
2954 zserv_time_buf(&client
->last_read_time
, rbuf
, ZEBRA_TIME_BUF
));
2955 vty_out(vty
, "Last Msg Tx Time: %s \n",
2956 zserv_time_buf(&client
->last_write_time
, wbuf
, ZEBRA_TIME_BUF
));
2957 if (client
->last_read_time
)
2958 vty_out(vty
, "Last Rcvd Cmd: %s \n",
2959 zserv_command_string(client
->last_read_cmd
));
2960 if (client
->last_write_time
)
2961 vty_out(vty
, "Last Sent Cmd: %s \n",
2962 zserv_command_string(client
->last_write_cmd
));
2965 vty_out(vty
, "Type Add Update Del \n");
2966 vty_out(vty
, "================================================== \n");
2967 vty_out(vty
, "IPv4 %-12d%-12d%-12d\n", client
->v4_route_add_cnt
,
2968 client
->v4_route_upd8_cnt
, client
->v4_route_del_cnt
);
2969 vty_out(vty
, "IPv6 %-12d%-12d%-12d\n", client
->v6_route_add_cnt
,
2970 client
->v6_route_upd8_cnt
, client
->v6_route_del_cnt
);
2971 vty_out(vty
, "Redist:v4 %-12d%-12d%-12d\n", client
->redist_v4_add_cnt
,
2972 0, client
->redist_v4_del_cnt
);
2973 vty_out(vty
, "Redist:v6 %-12d%-12d%-12d\n", client
->redist_v6_add_cnt
,
2974 0, client
->redist_v6_del_cnt
);
2975 vty_out(vty
, "Connected %-12d%-12d%-12d\n", client
->ifadd_cnt
, 0,
2977 vty_out(vty
, "BFD peer %-12d%-12d%-12d\n", client
->bfd_peer_add_cnt
,
2978 client
->bfd_peer_upd8_cnt
, client
->bfd_peer_del_cnt
);
2979 vty_out(vty
, "Interface Up Notifications: %d\n", client
->ifup_cnt
);
2980 vty_out(vty
, "Interface Down Notifications: %d\n", client
->ifdown_cnt
);
2981 vty_out(vty
, "VNI add notifications: %d\n", client
->vniadd_cnt
);
2982 vty_out(vty
, "VNI delete notifications: %d\n", client
->vnidel_cnt
);
2983 vty_out(vty
, "MAC-IP add notifications: %d\n", client
->macipadd_cnt
);
2984 vty_out(vty
, "MAC-IP delete notifications: %d\n", client
->macipdel_cnt
);
2990 static void zebra_show_client_brief(struct vty
*vty
, struct zserv
*client
)
2992 char cbuf
[ZEBRA_TIME_BUF
], rbuf
[ZEBRA_TIME_BUF
];
2993 char wbuf
[ZEBRA_TIME_BUF
];
2995 vty_out(vty
, "%-8s%12s %12s%12s%8d/%-8d%8d/%-8d\n",
2996 zebra_route_string(client
->proto
),
2997 zserv_time_buf(&client
->connect_time
, cbuf
, ZEBRA_TIME_BUF
),
2998 zserv_time_buf(&client
->last_read_time
, rbuf
, ZEBRA_TIME_BUF
),
2999 zserv_time_buf(&client
->last_write_time
, wbuf
, ZEBRA_TIME_BUF
),
3000 client
->v4_route_add_cnt
+ client
->v4_route_upd8_cnt
,
3001 client
->v4_route_del_cnt
,
3002 client
->v6_route_add_cnt
+ client
->v6_route_upd8_cnt
,
3003 client
->v6_route_del_cnt
);
3006 struct zserv
*zebra_find_client(u_char proto
)
3008 struct listnode
*node
, *nnode
;
3009 struct zserv
*client
;
3011 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, node
, nnode
, client
)) {
3012 if (client
->proto
== proto
)
3020 /* Display default rtm_table for all clients. */
3025 "default routing table to use for all clients\n")
3027 vty_out(vty
, "table %d\n", zebrad
.rtm_table_default
);
3031 DEFUN (config_table
,
3034 "Configure target kernel routing table\n"
3037 zebrad
.rtm_table_default
= strtol(argv
[1]->arg
, (char **)0, 10);
3041 DEFUN (no_config_table
,
3042 no_config_table_cmd
,
3043 "no table [TABLENO]",
3045 "Configure target kernel routing table\n"
3048 zebrad
.rtm_table_default
= 0;
3053 DEFUN (ip_forwarding
,
3057 "Turn on IP forwarding\n")
3063 ret
= ipforward_on();
3066 vty_out(vty
, "Can't turn on IP forwarding\n");
3067 return CMD_WARNING_CONFIG_FAILED
;
3073 DEFUN (no_ip_forwarding
,
3074 no_ip_forwarding_cmd
,
3078 "Turn off IP forwarding\n")
3084 ret
= ipforward_off();
3087 vty_out(vty
, "Can't turn off IP forwarding\n");
3088 return CMD_WARNING_CONFIG_FAILED
;
3103 " Route Route Neighbor LSP LSP\n");
3105 "VRF Installs Removals Updates Installs Removals\n");
3106 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
) {
3107 struct zebra_vrf
*zvrf
= vrf
->info
;
3108 vty_out(vty
, "%-25s %10" PRIu64
" %10" PRIu64
" %10" PRIu64
3109 " %10" PRIu64
" %10" PRIu64
"\n",
3110 vrf
->name
, zvrf
->installs
, zvrf
->removals
,
3111 zvrf
->neigh_updates
, zvrf
->lsp_installs
,
3112 zvrf
->lsp_removals
);
3118 /* This command is for debugging purpose. */
3119 DEFUN (show_zebra_client
,
3120 show_zebra_client_cmd
,
3121 "show zebra client",
3124 "Client information\n")
3126 struct listnode
*node
;
3127 struct zserv
*client
;
3129 for (ALL_LIST_ELEMENTS_RO(zebrad
.client_list
, node
, client
))
3130 zebra_show_client_detail(vty
, client
);
3135 /* This command is for debugging purpose. */
3136 DEFUN (show_zebra_client_summary
,
3137 show_zebra_client_summary_cmd
,
3138 "show zebra client summary",
3141 "Client information brief\n"
3144 struct listnode
*node
;
3145 struct zserv
*client
;
3148 "Name Connect Time Last Read Last Write IPv4 Routes IPv6 Routes \n");
3150 "--------------------------------------------------------------------------------\n");
3152 for (ALL_LIST_ELEMENTS_RO(zebrad
.client_list
, node
, client
))
3153 zebra_show_client_brief(vty
, client
);
3155 vty_out(vty
, "Routes column shows (added+updated)/deleted\n");
3159 /* Table configuration write function. */
3160 static int config_write_table(struct vty
*vty
)
3162 if (zebrad
.rtm_table_default
)
3163 vty_out(vty
, "table %d\n", zebrad
.rtm_table_default
);
3167 /* table node for routing tables. */
3168 static struct cmd_node table_node
= {TABLE_NODE
,
3169 "", /* This node has no interface. */
3172 /* Only display ip forwarding is enabled or not. */
3173 DEFUN (show_ip_forwarding
,
3174 show_ip_forwarding_cmd
,
3175 "show ip forwarding",
3178 "IP forwarding status\n")
3185 vty_out(vty
, "IP forwarding is off\n");
3187 vty_out(vty
, "IP forwarding is on\n");
3191 /* Only display ipv6 forwarding is enabled or not. */
3192 DEFUN (show_ipv6_forwarding
,
3193 show_ipv6_forwarding_cmd
,
3194 "show ipv6 forwarding",
3196 "IPv6 information\n"
3197 "Forwarding status\n")
3201 ret
= ipforward_ipv6();
3205 vty_out(vty
, "ipv6 forwarding is unknown\n");
3208 vty_out(vty
, "ipv6 forwarding is %s\n", "off");
3211 vty_out(vty
, "ipv6 forwarding is %s\n", "on");
3214 vty_out(vty
, "ipv6 forwarding is %s\n", "off");
3220 DEFUN (ipv6_forwarding
,
3221 ipv6_forwarding_cmd
,
3224 "Turn on IPv6 forwarding\n")
3228 ret
= ipforward_ipv6();
3230 ret
= ipforward_ipv6_on();
3233 vty_out(vty
, "Can't turn on IPv6 forwarding\n");
3234 return CMD_WARNING_CONFIG_FAILED
;
3240 DEFUN (no_ipv6_forwarding
,
3241 no_ipv6_forwarding_cmd
,
3242 "no ipv6 forwarding",
3245 "Turn off IPv6 forwarding\n")
3249 ret
= ipforward_ipv6();
3251 ret
= ipforward_ipv6_off();
3254 vty_out(vty
, "Can't turn off IPv6 forwarding\n");
3255 return CMD_WARNING_CONFIG_FAILED
;
3261 /* IPForwarding configuration write function. */
3262 static int config_write_forwarding(struct vty
*vty
)
3264 /* FIXME: Find better place for that. */
3265 router_id_write(vty
);
3268 vty_out(vty
, "no ip forwarding\n");
3269 if (!ipforward_ipv6())
3270 vty_out(vty
, "no ipv6 forwarding\n");
3271 vty_out(vty
, "!\n");
3275 /* table node for routing tables. */
3276 static struct cmd_node forwarding_node
= {FORWARDING_NODE
,
3277 "", /* This node has no interface. */
3280 #if defined(HANDLE_ZAPI_FUZZING)
3281 void zserv_read_file(char *input
)
3284 struct zserv
*client
= NULL
;
3287 zebra_client_create(-1);
3288 client
= zebrad
.client_list
->head
->data
;
3291 fd
= open(input
, O_RDONLY
|O_NONBLOCK
);
3294 zebra_client_read(&t
);
3300 /* Initialisation of zebra and installation of commands. */
3301 void zebra_init(void)
3303 /* Client list init. */
3304 zebrad
.client_list
= list_new();
3305 zebrad
.client_list
->del
= (void (*)(void *))zebra_client_free
;
3307 /* Install configuration write function. */
3308 install_node(&table_node
, config_write_table
);
3309 install_node(&forwarding_node
, config_write_forwarding
);
3311 install_element(VIEW_NODE
, &show_ip_forwarding_cmd
);
3312 install_element(CONFIG_NODE
, &ip_forwarding_cmd
);
3313 install_element(CONFIG_NODE
, &no_ip_forwarding_cmd
);
3314 install_element(ENABLE_NODE
, &show_zebra_cmd
);
3315 install_element(ENABLE_NODE
, &show_zebra_client_cmd
);
3316 install_element(ENABLE_NODE
, &show_zebra_client_summary_cmd
);
3319 install_element(VIEW_NODE
, &show_table_cmd
);
3320 install_element(CONFIG_NODE
, &config_table_cmd
);
3321 install_element(CONFIG_NODE
, &no_config_table_cmd
);
3322 #endif /* HAVE_NETLINK */
3324 install_element(VIEW_NODE
, &show_ipv6_forwarding_cmd
);
3325 install_element(CONFIG_NODE
, &ipv6_forwarding_cmd
);
3326 install_element(CONFIG_NODE
, &no_ipv6_forwarding_cmd
);
3329 zebra_route_map_init();