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"
44 #include "zebra/zserv.h"
45 #include "zebra/zebra_ns.h"
46 #include "zebra/zebra_vrf.h"
47 #include "zebra/router-id.h"
48 #include "zebra/redistribute.h"
49 #include "zebra/debug.h"
50 #include "zebra/ipforward.h"
51 #include "zebra/zebra_rnh.h"
52 #include "zebra/rt_netlink.h"
53 #include "zebra/interface.h"
54 #include "zebra/zebra_ptm.h"
55 #include "zebra/rtadv.h"
56 #include "zebra/zebra_mpls.h"
57 #include "zebra/zebra_mroute.h"
58 #include "zebra/label_manager.h"
59 #include "zebra/zebra_vxlan.h"
62 /* Event list of zebra. */
63 enum event
{ ZEBRA_SERV
, ZEBRA_READ
, ZEBRA_WRITE
};
65 static void zebra_event(enum event event
, int sock
, struct zserv
*client
);
67 extern struct zebra_privs_t zserv_privs
;
69 static void zebra_client_close(struct zserv
*client
);
71 static int zserv_delayed_close(struct thread
*thread
)
73 struct zserv
*client
= THREAD_ARG(thread
);
75 client
->t_suicide
= NULL
;
76 zebra_client_close(client
);
80 static int zserv_flush_data(struct thread
*thread
)
82 struct zserv
*client
= THREAD_ARG(thread
);
84 client
->t_write
= NULL
;
85 if (client
->t_suicide
) {
86 zebra_client_close(client
);
89 switch (buffer_flush_available(client
->wb
, client
->sock
)) {
92 "%s: buffer_flush_available failed on zserv client fd %d, "
94 __func__
, client
->sock
);
95 zebra_client_close(client
);
99 client
->t_write
= NULL
;
100 thread_add_write(zebrad
.master
, zserv_flush_data
, client
,
101 client
->sock
, &client
->t_write
);
108 client
->last_write_time
= monotime(NULL
);
112 int zebra_server_send_message(struct zserv
*client
)
114 if (client
->t_suicide
)
117 if (client
->is_synchronous
)
120 stream_set_getp(client
->obuf
, 0);
121 client
->last_write_cmd
= stream_getw_from(client
->obuf
, 6);
122 switch (buffer_write(client
->wb
, client
->sock
,
123 STREAM_DATA(client
->obuf
),
124 stream_get_endp(client
->obuf
))) {
127 "%s: buffer_write failed to zserv client fd %d, closing",
128 __func__
, client
->sock
);
129 /* Schedule a delayed close since many of the functions that
131 one do not check the return code. They do not allow for the
132 possibility that an I/O error may have caused the client to
135 client
->t_suicide
= NULL
;
136 thread_add_event(zebrad
.master
, zserv_delayed_close
, client
, 0,
140 THREAD_OFF(client
->t_write
);
143 thread_add_write(zebrad
.master
, zserv_flush_data
, client
,
144 client
->sock
, &client
->t_write
);
148 client
->last_write_time
= monotime(NULL
);
152 void zserv_create_header(struct stream
*s
, uint16_t cmd
, vrf_id_t vrf_id
)
154 /* length placeholder, caller can update */
155 stream_putw(s
, ZEBRA_HEADER_SIZE
);
156 stream_putc(s
, ZEBRA_HEADER_MARKER
);
157 stream_putc(s
, ZSERV_VERSION
);
158 stream_putw(s
, vrf_id
);
162 static void zserv_encode_interface(struct stream
*s
, struct interface
*ifp
)
164 /* Interface information. */
165 stream_put(s
, ifp
->name
, INTERFACE_NAMSIZ
);
166 stream_putl(s
, ifp
->ifindex
);
167 stream_putc(s
, ifp
->status
);
168 stream_putq(s
, ifp
->flags
);
169 stream_putc(s
, ifp
->ptm_enable
);
170 stream_putc(s
, ifp
->ptm_status
);
171 stream_putl(s
, ifp
->metric
);
172 stream_putl(s
, ifp
->speed
);
173 stream_putl(s
, ifp
->mtu
);
174 stream_putl(s
, ifp
->mtu6
);
175 stream_putl(s
, ifp
->bandwidth
);
176 stream_putl(s
, ifp
->ll_type
);
177 stream_putl(s
, ifp
->hw_addr_len
);
178 if (ifp
->hw_addr_len
)
179 stream_put(s
, ifp
->hw_addr
, ifp
->hw_addr_len
);
181 /* Then, Traffic Engineering parameters if any */
182 if (HAS_LINK_PARAMS(ifp
) && IS_LINK_PARAMS_SET(ifp
->link_params
)) {
184 zebra_interface_link_params_write(s
, ifp
);
188 /* Write packet size. */
189 stream_putw_at(s
, 0, stream_get_endp(s
));
192 static void zserv_encode_vrf(struct stream
*s
, struct zebra_vrf
*zvrf
)
194 struct vrf_data data
;
196 data
.l
.table_id
= zvrf
->table_id
;
197 /* Pass the tableid */
198 stream_put(s
, &data
, sizeof(struct vrf_data
));
199 /* Interface information. */
200 stream_put(s
, zvrf_name(zvrf
), VRF_NAMSIZ
);
202 /* Write packet size. */
203 stream_putw_at(s
, 0, stream_get_endp(s
));
206 /* Interface is added. Send ZEBRA_INTERFACE_ADD to client. */
208 * This function is called in the following situations:
209 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
211 * - at startup, when zebra figures out the available interfaces
212 * - when an interface is added (where support for
213 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
214 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
217 int zsend_interface_add(struct zserv
*client
, struct interface
*ifp
)
224 zserv_create_header(s
, ZEBRA_INTERFACE_ADD
, ifp
->vrf_id
);
225 zserv_encode_interface(s
, ifp
);
228 return zebra_server_send_message(client
);
231 /* Interface deletion from zebra daemon. */
232 int zsend_interface_delete(struct zserv
*client
, struct interface
*ifp
)
239 zserv_create_header(s
, ZEBRA_INTERFACE_DELETE
, ifp
->vrf_id
);
240 zserv_encode_interface(s
, ifp
);
243 return zebra_server_send_message(client
);
246 int zsend_vrf_add(struct zserv
*client
, struct zebra_vrf
*zvrf
)
253 zserv_create_header(s
, ZEBRA_VRF_ADD
, zvrf_id(zvrf
));
254 zserv_encode_vrf(s
, zvrf
);
256 client
->vrfadd_cnt
++;
257 return zebra_server_send_message(client
);
260 /* VRF deletion from zebra daemon. */
261 int zsend_vrf_delete(struct zserv
*client
, struct zebra_vrf
*zvrf
)
268 zserv_create_header(s
, ZEBRA_VRF_DELETE
, zvrf_id(zvrf
));
269 zserv_encode_vrf(s
, zvrf
);
271 client
->vrfdel_cnt
++;
272 return zebra_server_send_message(client
);
275 int zsend_interface_link_params(struct zserv
*client
, struct interface
*ifp
)
279 /* Check this client need interface information. */
283 if (!ifp
->link_params
)
288 zserv_create_header(s
, ZEBRA_INTERFACE_LINK_PARAMS
, ifp
->vrf_id
);
290 /* Add Interface Index */
291 stream_putl(s
, ifp
->ifindex
);
293 /* Then TE Link Parameters */
294 if (zebra_interface_link_params_write(s
, ifp
) == 0)
297 /* Write packet size. */
298 stream_putw_at(s
, 0, stream_get_endp(s
));
300 return zebra_server_send_message(client
);
303 /* Interface address is added/deleted. Send ZEBRA_INTERFACE_ADDRESS_ADD or
304 * ZEBRA_INTERFACE_ADDRESS_DELETE to the client.
306 * A ZEBRA_INTERFACE_ADDRESS_ADD is sent in the following situations:
307 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
308 * from the client, after the ZEBRA_INTERFACE_ADD has been
309 * sent from zebra to the client
310 * - redistribute new address info to all clients in the following situations
311 * - at startup, when zebra figures out the available interfaces
312 * - when an interface is added (where support for
313 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
314 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
316 * - for the vty commands "ip address A.B.C.D/M [<secondary>|<label LINE>]"
317 * and "no bandwidth <1-10000000>", "ipv6 address X:X::X:X/M"
318 * - when an RTM_NEWADDR message is received from the kernel,
320 * The call tree that triggers ZEBRA_INTERFACE_ADDRESS_DELETE:
322 * zsend_interface_address(DELETE)
325 * zebra_interface_address_delete_update
327 * | | if_delete_update
329 * ip_address_uninstall connected_delete_ipv4
330 * [ipv6_addresss_uninstall] [connected_delete_ipv6]
333 * | RTM_NEWADDR on routing/netlink socket
336 * "no ip address A.B.C.D/M [label LINE]"
337 * "no ip address A.B.C.D/M secondary"
338 * ["no ipv6 address X:X::X:X/M"]
341 int zsend_interface_address(int cmd
, struct zserv
*client
,
342 struct interface
*ifp
, struct connected
*ifc
)
351 zserv_create_header(s
, cmd
, ifp
->vrf_id
);
352 stream_putl(s
, ifp
->ifindex
);
354 /* Interface address flag. */
355 stream_putc(s
, ifc
->flags
);
357 /* Prefix information. */
359 stream_putc(s
, p
->family
);
360 blen
= prefix_blen(p
);
361 stream_put(s
, &p
->u
.prefix
, blen
);
364 * XXX gnu version does not send prefixlen for
365 * ZEBRA_INTERFACE_ADDRESS_DELETE
366 * but zebra_interface_address_delete_read() in the gnu version
369 stream_putc(s
, p
->prefixlen
);
372 p
= ifc
->destination
;
374 stream_put(s
, &p
->u
.prefix
, blen
);
376 stream_put(s
, NULL
, blen
);
378 /* Write packet size. */
379 stream_putw_at(s
, 0, stream_get_endp(s
));
381 client
->connected_rt_add_cnt
++;
382 return zebra_server_send_message(client
);
385 static int zsend_interface_nbr_address(int cmd
, struct zserv
*client
,
386 struct interface
*ifp
,
387 struct nbr_connected
*ifc
)
396 zserv_create_header(s
, cmd
, ifp
->vrf_id
);
397 stream_putl(s
, ifp
->ifindex
);
399 /* Prefix information. */
401 stream_putc(s
, p
->family
);
402 blen
= prefix_blen(p
);
403 stream_put(s
, &p
->u
.prefix
, blen
);
406 * XXX gnu version does not send prefixlen for
407 * ZEBRA_INTERFACE_ADDRESS_DELETE
408 * but zebra_interface_address_delete_read() in the gnu version
411 stream_putc(s
, p
->prefixlen
);
413 /* Write packet size. */
414 stream_putw_at(s
, 0, stream_get_endp(s
));
416 return zebra_server_send_message(client
);
419 /* Interface address addition. */
420 static void zebra_interface_nbr_address_add_update(struct interface
*ifp
,
421 struct nbr_connected
*ifc
)
423 struct listnode
*node
, *nnode
;
424 struct zserv
*client
;
427 if (IS_ZEBRA_DEBUG_EVENT
) {
428 char buf
[INET6_ADDRSTRLEN
];
432 "MESSAGE: ZEBRA_INTERFACE_NBR_ADDRESS_ADD %s/%d on %s",
433 inet_ntop(p
->family
, &p
->u
.prefix
, buf
,
435 p
->prefixlen
, ifc
->ifp
->name
);
438 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, node
, nnode
, client
))
439 zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_ADD
,
443 /* Interface address deletion. */
444 static void zebra_interface_nbr_address_delete_update(struct interface
*ifp
,
445 struct nbr_connected
*ifc
)
447 struct listnode
*node
, *nnode
;
448 struct zserv
*client
;
451 if (IS_ZEBRA_DEBUG_EVENT
) {
452 char buf
[INET6_ADDRSTRLEN
];
456 "MESSAGE: ZEBRA_INTERFACE_NBR_ADDRESS_DELETE %s/%d on %s",
457 inet_ntop(p
->family
, &p
->u
.prefix
, buf
,
459 p
->prefixlen
, ifc
->ifp
->name
);
462 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, node
, nnode
, client
))
463 zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
,
467 /* Send addresses on interface to client */
468 int zsend_interface_addresses(struct zserv
*client
, struct interface
*ifp
)
470 struct listnode
*cnode
, *cnnode
;
472 struct nbr_connected
*nc
;
474 /* Send interface addresses. */
475 for (ALL_LIST_ELEMENTS(ifp
->connected
, cnode
, cnnode
, c
)) {
476 if (!CHECK_FLAG(c
->conf
, ZEBRA_IFC_REAL
))
479 if (zsend_interface_address(ZEBRA_INTERFACE_ADDRESS_ADD
, client
,
485 /* Send interface neighbors. */
486 for (ALL_LIST_ELEMENTS(ifp
->nbr_connected
, cnode
, cnnode
, nc
)) {
487 if (zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_ADD
,
496 /* Notify client about interface moving from one VRF to another.
497 * Whether client is interested in old and new VRF is checked by caller.
499 int zsend_interface_vrf_update(struct zserv
*client
, struct interface
*ifp
,
507 zserv_create_header(s
, ZEBRA_INTERFACE_VRF_UPDATE
, ifp
->vrf_id
);
509 /* Fill in the ifIndex of the interface and its new VRF (id) */
510 stream_putl(s
, ifp
->ifindex
);
511 stream_putw(s
, vrf_id
);
513 /* Write packet size. */
514 stream_putw_at(s
, 0, stream_get_endp(s
));
516 client
->if_vrfchg_cnt
++;
517 return zebra_server_send_message(client
);
520 /* Add new nbr connected IPv6 address */
521 void nbr_connected_add_ipv6(struct interface
*ifp
, struct in6_addr
*address
)
523 struct nbr_connected
*ifc
;
527 IPV6_ADDR_COPY(&p
.u
.prefix
, address
);
528 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
530 if (!(ifc
= listnode_head(ifp
->nbr_connected
))) {
532 ifc
= nbr_connected_new();
533 ifc
->address
= prefix_new();
535 listnode_add(ifp
->nbr_connected
, ifc
);
538 prefix_copy(ifc
->address
, &p
);
540 zebra_interface_nbr_address_add_update(ifp
, ifc
);
542 if_nbr_ipv6ll_to_ipv4ll_neigh_update(ifp
, address
, 1);
545 void nbr_connected_delete_ipv6(struct interface
*ifp
, struct in6_addr
*address
)
547 struct nbr_connected
*ifc
;
551 IPV6_ADDR_COPY(&p
.u
.prefix
, address
);
552 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
554 ifc
= nbr_connected_check(ifp
, &p
);
558 listnode_delete(ifp
->nbr_connected
, ifc
);
560 zebra_interface_nbr_address_delete_update(ifp
, ifc
);
562 if_nbr_ipv6ll_to_ipv4ll_neigh_update(ifp
, address
, 0);
564 nbr_connected_free(ifc
);
568 * The cmd passed to zsend_interface_update may be ZEBRA_INTERFACE_UP or
569 * ZEBRA_INTERFACE_DOWN.
571 * The ZEBRA_INTERFACE_UP message is sent from the zebra server to
572 * the clients in one of 2 situations:
573 * - an if_up is detected e.g., as a result of an RTM_IFINFO message
574 * - a vty command modifying the bandwidth of an interface is received.
575 * The ZEBRA_INTERFACE_DOWN message is sent when an if_down is detected.
577 int zsend_interface_update(int cmd
, struct zserv
*client
, struct interface
*ifp
)
584 zserv_create_header(s
, cmd
, ifp
->vrf_id
);
585 zserv_encode_interface(s
, ifp
);
587 if (cmd
== ZEBRA_INTERFACE_UP
)
590 client
->ifdown_cnt
++;
592 return zebra_server_send_message(client
);
596 * This is the new function to announce and withdraw redistributed routes, used
597 * by Zebra. This is the old zsend_route_multipath() function. That function
598 * was duplicating code to send a lot of information that was essentially thrown
599 * away or ignored by the receiver. This is the leaner function that is not a
600 * duplicate of the zapi_ipv4_route_add/del.
602 * The primary difference is that this function merely sends a single NH instead
606 int zsend_redistribute_route(int add
, struct zserv
*client
, struct prefix
*p
,
607 struct prefix
*src_p
, struct route_entry
*re
)
613 struct nexthop
*nexthop
;
614 unsigned long nhnummark
= 0, messmark
= 0;
616 u_char zapi_flags
= 0;
617 struct nexthop dummy_nh
;
619 afi
= family2afi(p
->family
);
623 cmd
= ZEBRA_REDISTRIBUTE_IPV4_ADD
;
624 client
->redist_v4_add_cnt
++;
627 cmd
= ZEBRA_REDISTRIBUTE_IPV6_ADD
;
628 client
->redist_v6_add_cnt
++;
636 cmd
= ZEBRA_REDISTRIBUTE_IPV4_DEL
;
637 client
->redist_v4_del_cnt
++;
640 cmd
= ZEBRA_REDISTRIBUTE_IPV6_DEL
;
641 client
->redist_v6_del_cnt
++;
650 memset(&dummy_nh
, 0, sizeof(struct nexthop
));
652 zserv_create_header(s
, cmd
, re
->vrf_id
);
654 /* Put type and nexthop. */
655 stream_putc(s
, re
->type
);
656 stream_putw(s
, re
->instance
);
657 stream_putl(s
, re
->flags
);
659 /* marker for message flags field */
660 messmark
= stream_get_endp(s
);
664 psize
= PSIZE(p
->prefixlen
);
665 stream_putc(s
, p
->prefixlen
);
666 stream_write(s
, (u_char
*)&p
->u
.prefix
, psize
);
669 SET_FLAG(zapi_flags
, ZAPI_MESSAGE_SRCPFX
);
670 psize
= PSIZE(src_p
->prefixlen
);
671 stream_putc(s
, src_p
->prefixlen
);
672 stream_write(s
, (u_char
*)&src_p
->u
.prefix
, psize
);
675 for (nexthop
= re
->nexthop
; nexthop
; nexthop
= nexthop
->next
) {
676 /* We don't send any nexthops when there's a multipath */
677 if (re
->nexthop_active_num
> 1
678 && client
->proto
!= ZEBRA_ROUTE_LDP
) {
679 SET_FLAG(zapi_flags
, ZAPI_MESSAGE_NEXTHOP
);
680 SET_FLAG(zapi_flags
, ZAPI_MESSAGE_IFINDEX
);
683 if (p
->family
== AF_INET
) {
684 stream_put_in_addr(s
, &dummy_nh
.gate
.ipv4
);
685 } else if (p
->family
== AF_INET6
) {
686 stream_write(s
, (u_char
*)&dummy_nh
.gate
.ipv6
,
689 /* We don't handle anything else now, abort */
691 "%s: Unable to redistribute route of unknown family, %d\n",
692 __func__
, p
->family
);
696 stream_putl(s
, 0); /* dummy ifindex */
700 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
)) {
701 SET_FLAG(zapi_flags
, ZAPI_MESSAGE_NEXTHOP
);
702 SET_FLAG(zapi_flags
, ZAPI_MESSAGE_IFINDEX
);
703 if (nhnummark
== 0) {
704 nhnummark
= stream_get_endp(s
);
705 stream_putc(s
, 1); /* placeholder */
709 switch (nexthop
->type
) {
710 case NEXTHOP_TYPE_IPV4
:
711 case NEXTHOP_TYPE_IPV4_IFINDEX
:
712 stream_put_in_addr(s
, &nexthop
->gate
.ipv4
);
714 case NEXTHOP_TYPE_IPV6
:
715 case NEXTHOP_TYPE_IPV6_IFINDEX
:
716 /* Only BGP supports IPv4 prefix with IPv6 NH,
718 if (p
->family
== AF_INET
)
719 stream_put_in_addr(s
,
720 &dummy_nh
.gate
.ipv4
);
724 (u_char
*)&nexthop
->gate
.ipv6
,
728 if (cmd
== ZEBRA_REDISTRIBUTE_IPV4_ADD
729 || cmd
== ZEBRA_REDISTRIBUTE_IPV4_DEL
) {
730 struct in_addr empty
;
732 sizeof(struct in_addr
));
733 stream_write(s
, (u_char
*)&empty
,
736 struct in6_addr empty
;
738 sizeof(struct in6_addr
));
739 stream_write(s
, (u_char
*)&empty
,
744 /* Interface index. */
746 stream_putl(s
, nexthop
->ifindex
);
748 /* ldpd needs all nexthops */
749 if (client
->proto
!= ZEBRA_ROUTE_LDP
)
755 SET_FLAG(zapi_flags
, ZAPI_MESSAGE_DISTANCE
);
756 stream_putc(s
, re
->distance
);
759 SET_FLAG(zapi_flags
, ZAPI_MESSAGE_METRIC
);
760 stream_putl(s
, re
->metric
);
764 SET_FLAG(zapi_flags
, ZAPI_MESSAGE_TAG
);
765 stream_putl(s
, re
->tag
);
769 SET_FLAG(zapi_flags
, ZAPI_MESSAGE_MTU
);
770 stream_putl(s
, re
->mtu
);
772 /* write real message flags value */
773 stream_putc_at(s
, messmark
, zapi_flags
);
775 /* Write next-hop number */
777 stream_putc_at(s
, nhnummark
, nhnum
);
779 /* Write packet size. */
780 stream_putw_at(s
, 0, stream_get_endp(s
));
782 return zebra_server_send_message(client
);
785 static int zsend_write_nexthop(struct stream
*s
, struct nexthop
*nexthop
)
787 stream_putc(s
, nexthop
->type
);
788 switch (nexthop
->type
) {
789 case NEXTHOP_TYPE_IPV4
:
790 case NEXTHOP_TYPE_IPV4_IFINDEX
:
791 stream_put_in_addr(s
, &nexthop
->gate
.ipv4
);
792 stream_putl(s
, nexthop
->ifindex
);
794 case NEXTHOP_TYPE_IPV6
:
795 stream_put(s
, &nexthop
->gate
.ipv6
, 16);
797 case NEXTHOP_TYPE_IPV6_IFINDEX
:
798 stream_put(s
, &nexthop
->gate
.ipv6
, 16);
799 stream_putl(s
, nexthop
->ifindex
);
801 case NEXTHOP_TYPE_IFINDEX
:
802 stream_putl(s
, nexthop
->ifindex
);
811 /* Nexthop register */
812 static int zserv_rnh_register(struct zserv
*client
, int sock
, u_short length
,
813 rnh_type_t type
, struct zebra_vrf
*zvrf
)
821 if (IS_ZEBRA_DEBUG_NHT
)
823 "rnh_register msg from client %s: length=%d, type=%s\n",
824 zebra_route_string(client
->proto
), length
,
825 (type
== RNH_NEXTHOP_TYPE
) ? "nexthop" : "route");
829 client
->nh_reg_time
= monotime(NULL
);
832 flags
= stream_getc(s
);
833 p
.family
= stream_getw(s
);
834 p
.prefixlen
= stream_getc(s
);
836 if (p
.family
== AF_INET
) {
837 p
.u
.prefix4
.s_addr
= stream_get_ipv4(s
);
838 l
+= IPV4_MAX_BYTELEN
;
839 } else if (p
.family
== AF_INET6
) {
840 stream_get(&p
.u
.prefix6
, s
, IPV6_MAX_BYTELEN
);
841 l
+= IPV6_MAX_BYTELEN
;
844 "rnh_register: Received unknown family type %d\n",
848 rnh
= zebra_add_rnh(&p
, zvrf_id(zvrf
), type
);
849 if (type
== RNH_NEXTHOP_TYPE
) {
851 && !CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
))
852 SET_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
);
854 && CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
))
855 UNSET_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
);
856 } else if (type
== RNH_IMPORT_CHECK_TYPE
) {
858 && !CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
))
859 SET_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
);
860 else if (!flags
&& CHECK_FLAG(rnh
->flags
,
861 ZEBRA_NHT_EXACT_MATCH
))
862 UNSET_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
);
865 zebra_add_rnh_client(rnh
, client
, type
, zvrf_id(zvrf
));
866 /* Anything not AF_INET/INET6 has been filtered out above */
867 zebra_evaluate_rnh(zvrf_id(zvrf
), p
.family
, 1, type
, &p
);
872 /* Nexthop register */
873 static int zserv_rnh_unregister(struct zserv
*client
, int sock
, u_short length
,
874 rnh_type_t type
, struct zebra_vrf
*zvrf
)
881 if (IS_ZEBRA_DEBUG_NHT
)
882 zlog_debug("rnh_unregister msg from client %s: length=%d\n",
883 zebra_route_string(client
->proto
), length
);
889 s
); // Connected or not. Not used in this function
890 p
.family
= stream_getw(s
);
891 p
.prefixlen
= stream_getc(s
);
893 if (p
.family
== AF_INET
) {
894 p
.u
.prefix4
.s_addr
= stream_get_ipv4(s
);
895 l
+= IPV4_MAX_BYTELEN
;
896 } else if (p
.family
== AF_INET6
) {
897 stream_get(&p
.u
.prefix6
, s
, IPV6_MAX_BYTELEN
);
898 l
+= IPV6_MAX_BYTELEN
;
901 "rnh_register: Received unknown family type %d\n",
905 rnh
= zebra_lookup_rnh(&p
, zvrf_id(zvrf
), type
);
907 client
->nh_dereg_time
= monotime(NULL
);
908 zebra_remove_rnh_client(rnh
, client
, type
);
914 #define ZEBRA_MIN_FEC_LENGTH 5
917 static int zserv_fec_register(struct zserv
*client
, int sock
, u_short length
)
920 struct zebra_vrf
*zvrf
;
924 u_int32_t label_index
= MPLS_INVALID_LABEL_INDEX
;
927 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
929 return 0; // unexpected
932 * The minimum amount of data that can be sent for one fec
935 if (length
< ZEBRA_MIN_FEC_LENGTH
) {
937 "fec_register: Received a fec register of length %d, it is of insufficient size to properly decode",
943 flags
= stream_getw(s
);
944 p
.family
= stream_getw(s
);
945 if (p
.family
!= AF_INET
&& p
.family
!= AF_INET6
) {
947 "fec_register: Received unknown family type %d\n",
951 p
.prefixlen
= stream_getc(s
);
953 stream_get(&p
.u
.prefix
, s
, PSIZE(p
.prefixlen
));
954 l
+= PSIZE(p
.prefixlen
);
955 if (flags
& ZEBRA_FEC_REGISTER_LABEL_INDEX
) {
956 label_index
= stream_getl(s
);
959 label_index
= MPLS_INVALID_LABEL_INDEX
;
960 zebra_mpls_fec_register(zvrf
, &p
, label_index
, client
);
967 static int zserv_fec_unregister(struct zserv
*client
, int sock
, u_short length
)
970 struct zebra_vrf
*zvrf
;
976 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
978 return 0; // unexpected
981 * The minimum amount of data that can be sent for one
984 if (length
< ZEBRA_MIN_FEC_LENGTH
) {
986 "fec_unregister: Received a fec unregister of length %d, it is of insufficient size to properly decode",
992 // flags = stream_getw(s);
993 (void)stream_getw(s
);
994 p
.family
= stream_getw(s
);
995 if (p
.family
!= AF_INET
&& p
.family
!= AF_INET6
) {
997 "fec_unregister: Received unknown family type %d\n",
1001 p
.prefixlen
= stream_getc(s
);
1003 stream_get(&p
.u
.prefix
, s
, PSIZE(p
.prefixlen
));
1004 l
+= PSIZE(p
.prefixlen
);
1005 zebra_mpls_fec_unregister(zvrf
, &p
, client
);
1012 Modified version of zsend_ipv4_nexthop_lookup():
1013 Query unicast rib if nexthop is not found on mrib.
1014 Returns both route metric and protocol distance.
1016 static int zsend_ipv4_nexthop_lookup_mrib(struct zserv
*client
,
1017 struct in_addr addr
,
1018 struct route_entry
*re
,
1019 struct zebra_vrf
*zvrf
)
1024 struct nexthop
*nexthop
;
1026 /* Get output stream. */
1030 /* Fill in result. */
1031 zserv_create_header(s
, ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB
, zvrf_id(zvrf
));
1032 stream_put_in_addr(s
, &addr
);
1035 stream_putc(s
, re
->distance
);
1036 stream_putl(s
, re
->metric
);
1038 nump
= stream_get_endp(
1039 s
); /* remember position for nexthop_num */
1040 stream_putc(s
, 0); /* reserve room for nexthop_num */
1041 /* Only non-recursive routes are elegible to resolve the nexthop
1043 * are looking up. Therefore, we will just iterate over the top
1044 * chain of nexthops. */
1045 for (nexthop
= re
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
1046 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
1047 num
+= zsend_write_nexthop(s
, nexthop
);
1049 stream_putc_at(s
, nump
, num
); /* store nexthop_num */
1051 stream_putc(s
, 0); /* distance */
1052 stream_putl(s
, 0); /* metric */
1053 stream_putc(s
, 0); /* nexthop_num */
1056 stream_putw_at(s
, 0, stream_get_endp(s
));
1058 return zebra_server_send_message(client
);
1061 /* Router-id is updated. Send ZEBRA_ROUTER_ID_ADD to client. */
1062 int zsend_router_id_update(struct zserv
*client
, struct prefix
*p
,
1068 /* Check this client need interface information. */
1069 if (!vrf_bitmap_check(client
->ridinfo
, vrf_id
))
1076 zserv_create_header(s
, ZEBRA_ROUTER_ID_UPDATE
, vrf_id
);
1078 /* Prefix information. */
1079 stream_putc(s
, p
->family
);
1080 blen
= prefix_blen(p
);
1081 stream_put(s
, &p
->u
.prefix
, blen
);
1082 stream_putc(s
, p
->prefixlen
);
1084 /* Write packet size. */
1085 stream_putw_at(s
, 0, stream_get_endp(s
));
1087 return zebra_server_send_message(client
);
1090 /* Register zebra server interface information. Send current all
1091 interface and address information. */
1092 static int zread_interface_add(struct zserv
*client
, u_short length
,
1093 struct zebra_vrf
*zvrf
)
1096 struct listnode
*ifnode
, *ifnnode
;
1097 struct interface
*ifp
;
1099 /* Interface information is needed. */
1100 vrf_bitmap_set(client
->ifinfo
, zvrf_id(zvrf
));
1102 RB_FOREACH(vrf
, vrf_id_head
, &vrfs_by_id
)
1104 for (ALL_LIST_ELEMENTS(vrf
->iflist
, ifnode
, ifnnode
, ifp
)) {
1105 /* Skip pseudo interface. */
1106 if (!CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_ACTIVE
))
1109 if (zsend_interface_add(client
, ifp
) < 0)
1112 if (zsend_interface_addresses(client
, ifp
) < 0)
1119 /* Unregister zebra server interface information. */
1120 static int zread_interface_delete(struct zserv
*client
, u_short length
,
1121 struct zebra_vrf
*zvrf
)
1123 vrf_bitmap_unset(client
->ifinfo
, zvrf_id(zvrf
));
1127 void zserv_nexthop_num_warn(const char *caller
, const struct prefix
*p
,
1128 const unsigned int nexthop_num
)
1130 if (nexthop_num
> multipath_num
) {
1131 char buff
[PREFIX2STR_BUFFER
];
1132 prefix2str(p
, buff
, sizeof(buff
));
1134 "%s: Prefix %s has %d nexthops, but we can only use the first %d",
1135 caller
, buff
, nexthop_num
, multipath_num
);
1139 /* This function support multiple nexthop. */
1141 * Parse the ZEBRA_IPV4_ROUTE_ADD sent from client. Update re and
1144 static int zread_ipv4_add(struct zserv
*client
, u_short length
,
1145 struct zebra_vrf
*zvrf
)
1148 struct route_entry
*re
;
1151 struct in_addr nhop_addr
;
1153 u_char nexthop_type
;
1159 struct nexthop
*nexthop
;
1161 /* Get input stream. */
1164 /* Allocate new re. */
1165 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
1167 /* Type, flags, message. */
1168 re
->type
= stream_getc(s
);
1169 re
->instance
= stream_getw(s
);
1170 re
->flags
= stream_getl(s
);
1171 message
= stream_getc(s
);
1172 safi
= stream_getw(s
);
1173 re
->uptime
= time(NULL
);
1176 memset(&p
, 0, sizeof(struct prefix_ipv4
));
1178 p
.prefixlen
= stream_getc(s
);
1179 stream_get(&p
.u
.prefix4
, s
, PSIZE(p
.prefixlen
));
1182 re
->vrf_id
= zvrf_id(zvrf
);
1184 /* Nexthop parse. */
1185 if (CHECK_FLAG(message
, ZAPI_MESSAGE_NEXTHOP
)) {
1186 nexthop_num
= stream_getc(s
);
1187 zserv_nexthop_num_warn(__func__
, (const struct prefix
*)&p
,
1190 for (i
= 0; i
< nexthop_num
; i
++) {
1191 nexthop_type
= stream_getc(s
);
1193 switch (nexthop_type
) {
1194 case NEXTHOP_TYPE_IFINDEX
:
1195 ifindex
= stream_getl(s
);
1196 route_entry_nexthop_ifindex_add(re
, ifindex
);
1198 case NEXTHOP_TYPE_IPV4
:
1199 nhop_addr
.s_addr
= stream_get_ipv4(s
);
1200 nexthop
= route_entry_nexthop_ipv4_add(
1201 re
, &nhop_addr
, NULL
);
1202 /* For labeled-unicast, each nexthop is followed
1204 if (CHECK_FLAG(message
, ZAPI_MESSAGE_LABEL
)) {
1205 label
= (mpls_label_t
)stream_getl(s
);
1207 nexthop
, nexthop
->nh_label_type
,
1211 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1212 nhop_addr
.s_addr
= stream_get_ipv4(s
);
1213 ifindex
= stream_getl(s
);
1214 route_entry_nexthop_ipv4_ifindex_add(
1215 re
, &nhop_addr
, NULL
, ifindex
);
1217 case NEXTHOP_TYPE_IPV6
:
1218 stream_forward_getp(s
, IPV6_MAX_BYTELEN
);
1220 case NEXTHOP_TYPE_BLACKHOLE
:
1221 route_entry_nexthop_blackhole_add(re
);
1228 if (CHECK_FLAG(message
, ZAPI_MESSAGE_DISTANCE
))
1229 re
->distance
= stream_getc(s
);
1232 if (CHECK_FLAG(message
, ZAPI_MESSAGE_METRIC
))
1233 re
->metric
= stream_getl(s
);
1236 if (CHECK_FLAG(message
, ZAPI_MESSAGE_TAG
))
1237 re
->tag
= stream_getl(s
);
1241 if (CHECK_FLAG(message
, ZAPI_MESSAGE_MTU
))
1242 re
->mtu
= stream_getl(s
);
1247 re
->table
= zvrf
->table_id
;
1249 ret
= rib_add_multipath(AFI_IP
, safi
, &p
, NULL
, re
);
1253 client
->v4_route_add_cnt
++;
1255 client
->v4_route_upd8_cnt
++;
1259 /* Zebra server IPv4 prefix delete function. */
1260 static int zread_ipv4_delete(struct zserv
*client
, u_short length
,
1261 struct zebra_vrf
*zvrf
)
1265 struct zapi_ipv4 api
;
1266 struct in_addr nexthop
;
1267 union g_addr
*nexthop_p
;
1268 unsigned long ifindex
;
1271 u_char nexthop_type
;
1279 /* Type, flags, message. */
1280 api
.type
= stream_getc(s
);
1281 api
.instance
= stream_getw(s
);
1282 api
.flags
= stream_getl(s
);
1283 api
.message
= stream_getc(s
);
1284 api
.safi
= stream_getw(s
);
1287 memset(&p
, 0, sizeof(struct prefix
));
1289 p
.prefixlen
= stream_getc(s
);
1290 stream_get(&p
.u
.prefix4
, s
, PSIZE(p
.prefixlen
));
1292 /* Nexthop, ifindex, distance, metric. */
1293 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
)) {
1294 nexthop_num
= stream_getc(s
);
1296 for (i
= 0; i
< nexthop_num
; i
++) {
1297 nexthop_type
= stream_getc(s
);
1299 switch (nexthop_type
) {
1300 case NEXTHOP_TYPE_IFINDEX
:
1301 ifindex
= stream_getl(s
);
1303 case NEXTHOP_TYPE_IPV4
:
1304 nexthop
.s_addr
= stream_get_ipv4(s
);
1305 /* For labeled-unicast, each nexthop is followed
1307 * we don't care for delete.
1309 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_LABEL
))
1310 stream_forward_getp(s
,
1312 nexthop_p
= (union g_addr
*)&nexthop
;
1314 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1315 nexthop
.s_addr
= stream_get_ipv4(s
);
1316 nexthop_p
= (union g_addr
*)&nexthop
;
1317 ifindex
= stream_getl(s
);
1319 case NEXTHOP_TYPE_IPV6
:
1320 stream_forward_getp(s
, IPV6_MAX_BYTELEN
);
1327 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
))
1328 api
.distance
= stream_getc(s
);
1333 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
))
1334 api
.metric
= stream_getl(s
);
1339 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_TAG
))
1340 api
.tag
= stream_getl(s
);
1344 table_id
= zvrf
->table_id
;
1346 rib_delete(AFI_IP
, api
.safi
, zvrf_id(zvrf
), api
.type
, api
.instance
,
1347 api
.flags
, &p
, NULL
, nexthop_p
, ifindex
, table_id
);
1348 client
->v4_route_del_cnt
++;
1352 /* MRIB Nexthop lookup for IPv4. */
1353 static int zread_ipv4_nexthop_lookup_mrib(struct zserv
*client
, u_short length
,
1354 struct zebra_vrf
*zvrf
)
1356 struct in_addr addr
;
1357 struct route_entry
*re
;
1359 addr
.s_addr
= stream_get_ipv4(client
->ibuf
);
1360 re
= rib_match_ipv4_multicast(zvrf_id(zvrf
), addr
, NULL
);
1361 return zsend_ipv4_nexthop_lookup_mrib(client
, addr
, re
, zvrf
);
1364 /* Zebra server IPv6 prefix add function. */
1365 static int zread_ipv4_route_ipv6_nexthop_add(struct zserv
*client
,
1367 struct zebra_vrf
*zvrf
)
1371 struct in6_addr nhop_addr
;
1372 struct route_entry
*re
;
1375 u_char nexthop_type
;
1378 static struct in6_addr nexthops
[MULTIPATH_NUM
];
1379 static unsigned int ifindices
[MULTIPATH_NUM
];
1381 static mpls_label_t labels
[MULTIPATH_NUM
];
1383 struct nexthop
*nexthop
;
1385 /* Get input stream. */
1388 memset(&nhop_addr
, 0, sizeof(struct in6_addr
));
1390 /* Allocate new re. */
1391 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
1393 /* Type, flags, message. */
1394 re
->type
= stream_getc(s
);
1395 re
->instance
= stream_getw(s
);
1396 re
->flags
= stream_getl(s
);
1397 message
= stream_getc(s
);
1398 safi
= stream_getw(s
);
1399 re
->uptime
= time(NULL
);
1402 memset(&p
, 0, sizeof(struct prefix_ipv4
));
1404 p
.prefixlen
= stream_getc(s
);
1405 stream_get(&p
.u
.prefix4
, s
, PSIZE(p
.prefixlen
));
1408 re
->vrf_id
= zvrf_id(zvrf
);
1410 /* We need to give nh-addr, nh-ifindex with the same next-hop object
1411 * to the re to ensure that IPv6 multipathing works; need to coalesce
1412 * these. Clients should send the same number of paired set of
1413 * next-hop-addr/next-hop-ifindices. */
1414 if (CHECK_FLAG(message
, ZAPI_MESSAGE_NEXTHOP
)) {
1415 unsigned int nh_count
= 0;
1416 unsigned int if_count
= 0;
1417 unsigned int max_nh_if
= 0;
1419 nexthop_num
= stream_getc(s
);
1420 zserv_nexthop_num_warn(__func__
, (const struct prefix
*)&p
,
1422 for (i
= 0; i
< nexthop_num
; i
++) {
1423 nexthop_type
= stream_getc(s
);
1425 switch (nexthop_type
) {
1426 case NEXTHOP_TYPE_IPV6
:
1427 stream_get(&nhop_addr
, s
, 16);
1428 if (nh_count
< MULTIPATH_NUM
) {
1429 /* For labeled-unicast, each nexthop is
1430 * followed by label. */
1431 if (CHECK_FLAG(message
,
1432 ZAPI_MESSAGE_LABEL
)) {
1433 label
= (mpls_label_t
)
1435 labels
[nh_count
] = label
;
1437 nexthops
[nh_count
] = nhop_addr
;
1441 case NEXTHOP_TYPE_IFINDEX
:
1442 if (if_count
< multipath_num
) {
1443 ifindices
[if_count
++] = stream_getl(s
);
1446 case NEXTHOP_TYPE_BLACKHOLE
:
1447 route_entry_nexthop_blackhole_add(re
);
1452 max_nh_if
= (nh_count
> if_count
) ? nh_count
: if_count
;
1453 for (i
= 0; i
< max_nh_if
; i
++) {
1455 && !IN6_IS_ADDR_UNSPECIFIED(&nexthops
[i
])) {
1456 if ((i
< if_count
) && ifindices
[i
])
1458 route_entry_nexthop_ipv6_ifindex_add(
1462 nexthop
= route_entry_nexthop_ipv6_add(
1465 if (CHECK_FLAG(message
, ZAPI_MESSAGE_LABEL
))
1467 nexthop
, nexthop
->nh_label_type
,
1470 if ((i
< if_count
) && ifindices
[i
])
1471 route_entry_nexthop_ifindex_add(
1478 if (CHECK_FLAG(message
, ZAPI_MESSAGE_DISTANCE
))
1479 re
->distance
= stream_getc(s
);
1482 if (CHECK_FLAG(message
, ZAPI_MESSAGE_METRIC
))
1483 re
->metric
= stream_getl(s
);
1486 if (CHECK_FLAG(message
, ZAPI_MESSAGE_TAG
))
1487 re
->tag
= stream_getl(s
);
1491 if (CHECK_FLAG(message
, ZAPI_MESSAGE_MTU
))
1492 re
->mtu
= stream_getl(s
);
1497 re
->table
= zvrf
->table_id
;
1499 ret
= rib_add_multipath(AFI_IP6
, safi
, &p
, NULL
, re
);
1502 client
->v4_route_add_cnt
++;
1504 client
->v4_route_upd8_cnt
++;
1509 static int zread_ipv6_add(struct zserv
*client
, u_short length
,
1510 struct zebra_vrf
*zvrf
)
1514 struct in6_addr nhop_addr
;
1515 struct route_entry
*re
;
1518 u_char nexthop_type
;
1520 struct prefix_ipv6 src_p
, *src_pp
;
1522 static struct in6_addr nexthops
[MULTIPATH_NUM
];
1523 static unsigned int ifindices
[MULTIPATH_NUM
];
1525 static mpls_label_t labels
[MULTIPATH_NUM
];
1527 struct nexthop
*nexthop
;
1529 /* Get input stream. */
1532 memset(&nhop_addr
, 0, sizeof(struct in6_addr
));
1534 /* Allocate new re. */
1535 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
1537 /* Type, flags, message. */
1538 re
->type
= stream_getc(s
);
1539 re
->instance
= stream_getw(s
);
1540 re
->flags
= stream_getl(s
);
1541 message
= stream_getc(s
);
1542 safi
= stream_getw(s
);
1543 re
->uptime
= time(NULL
);
1546 memset(&p
, 0, sizeof(struct prefix_ipv6
));
1547 p
.family
= AF_INET6
;
1548 p
.prefixlen
= stream_getc(s
);
1549 stream_get(&p
.u
.prefix6
, s
, PSIZE(p
.prefixlen
));
1551 if (CHECK_FLAG(message
, ZAPI_MESSAGE_SRCPFX
)) {
1552 memset(&src_p
, 0, sizeof(struct prefix_ipv6
));
1553 src_p
.family
= AF_INET6
;
1554 src_p
.prefixlen
= stream_getc(s
);
1555 stream_get(&src_p
.prefix
, s
, PSIZE(src_p
.prefixlen
));
1560 /* We need to give nh-addr, nh-ifindex with the same next-hop object
1561 * to the re to ensure that IPv6 multipathing works; need to coalesce
1562 * these. Clients should send the same number of paired set of
1563 * next-hop-addr/next-hop-ifindices. */
1564 if (CHECK_FLAG(message
, ZAPI_MESSAGE_NEXTHOP
)) {
1565 unsigned int nh_count
= 0;
1566 unsigned int if_count
= 0;
1567 unsigned int max_nh_if
= 0;
1569 nexthop_num
= stream_getc(s
);
1570 zserv_nexthop_num_warn(__func__
, (const struct prefix
*)&p
,
1572 for (i
= 0; i
< nexthop_num
; i
++) {
1573 nexthop_type
= stream_getc(s
);
1575 switch (nexthop_type
) {
1576 case NEXTHOP_TYPE_IPV6
:
1577 stream_get(&nhop_addr
, s
, 16);
1578 if (nh_count
< MULTIPATH_NUM
) {
1579 /* For labeled-unicast, each nexthop is
1580 * followed by label. */
1581 if (CHECK_FLAG(message
,
1582 ZAPI_MESSAGE_LABEL
)) {
1583 label
= (mpls_label_t
)
1585 labels
[nh_count
] = label
;
1587 nexthops
[nh_count
++] = nhop_addr
;
1590 case NEXTHOP_TYPE_IFINDEX
:
1591 if (if_count
< multipath_num
) {
1592 ifindices
[if_count
++] = stream_getl(s
);
1595 case NEXTHOP_TYPE_BLACKHOLE
:
1596 route_entry_nexthop_blackhole_add(re
);
1601 max_nh_if
= (nh_count
> if_count
) ? nh_count
: if_count
;
1602 for (i
= 0; i
< max_nh_if
; i
++) {
1604 && !IN6_IS_ADDR_UNSPECIFIED(&nexthops
[i
])) {
1605 if ((i
< if_count
) && ifindices
[i
])
1607 route_entry_nexthop_ipv6_ifindex_add(
1611 nexthop
= route_entry_nexthop_ipv6_add(
1613 if (CHECK_FLAG(message
, ZAPI_MESSAGE_LABEL
))
1615 nexthop
, nexthop
->nh_label_type
,
1618 if ((i
< if_count
) && ifindices
[i
])
1619 route_entry_nexthop_ifindex_add(
1626 if (CHECK_FLAG(message
, ZAPI_MESSAGE_DISTANCE
))
1627 re
->distance
= stream_getc(s
);
1630 if (CHECK_FLAG(message
, ZAPI_MESSAGE_METRIC
))
1631 re
->metric
= stream_getl(s
);
1634 if (CHECK_FLAG(message
, ZAPI_MESSAGE_TAG
))
1635 re
->tag
= stream_getl(s
);
1639 if (CHECK_FLAG(message
, ZAPI_MESSAGE_MTU
))
1640 re
->mtu
= stream_getl(s
);
1645 re
->vrf_id
= zvrf_id(zvrf
);
1646 re
->table
= zvrf
->table_id
;
1648 ret
= rib_add_multipath(AFI_IP6
, safi
, &p
, src_pp
, re
);
1651 client
->v6_route_add_cnt
++;
1653 client
->v6_route_upd8_cnt
++;
1658 /* Zebra server IPv6 prefix delete function. */
1659 static int zread_ipv6_delete(struct zserv
*client
, u_short length
,
1660 struct zebra_vrf
*zvrf
)
1664 struct zapi_ipv6 api
;
1665 struct in6_addr nexthop
;
1666 union g_addr
*pnexthop
= NULL
;
1667 unsigned long ifindex
;
1669 struct prefix_ipv6 src_p
, *src_pp
;
1673 memset(&nexthop
, 0, sizeof(struct in6_addr
));
1675 /* Type, flags, message. */
1676 api
.type
= stream_getc(s
);
1677 api
.instance
= stream_getw(s
);
1678 api
.flags
= stream_getl(s
);
1679 api
.message
= stream_getc(s
);
1680 api
.safi
= stream_getw(s
);
1683 memset(&p
, 0, sizeof(struct prefix_ipv6
));
1684 p
.family
= AF_INET6
;
1685 p
.prefixlen
= stream_getc(s
);
1686 stream_get(&p
.u
.prefix6
, s
, PSIZE(p
.prefixlen
));
1688 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
)) {
1689 memset(&src_p
, 0, sizeof(struct prefix_ipv6
));
1690 src_p
.family
= AF_INET6
;
1691 src_p
.prefixlen
= stream_getc(s
);
1692 stream_get(&src_p
.prefix
, s
, PSIZE(src_p
.prefixlen
));
1697 /* Nexthop, ifindex, distance, metric. */
1698 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
)) {
1699 u_char nexthop_type
;
1701 api
.nexthop_num
= stream_getc(s
);
1702 for (i
= 0; i
< api
.nexthop_num
; i
++) {
1703 nexthop_type
= stream_getc(s
);
1705 switch (nexthop_type
) {
1706 case NEXTHOP_TYPE_IPV6
:
1707 stream_get(&nexthop
, s
, 16);
1708 /* For labeled-unicast, each nexthop is followed
1710 * we don't care for delete.
1712 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_LABEL
))
1713 stream_forward_getp(s
,
1715 pnexthop
= (union g_addr
*)&nexthop
;
1717 case NEXTHOP_TYPE_IFINDEX
:
1718 ifindex
= stream_getl(s
);
1725 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
))
1726 api
.distance
= stream_getc(s
);
1731 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
))
1732 api
.metric
= stream_getl(s
);
1737 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_TAG
))
1738 api
.tag
= stream_getl(s
);
1742 if (IN6_IS_ADDR_UNSPECIFIED(&nexthop
))
1743 rib_delete(AFI_IP6
, api
.safi
, zvrf_id(zvrf
), api
.type
,
1744 api
.instance
, api
.flags
, &p
, src_pp
, NULL
, ifindex
,
1747 rib_delete(AFI_IP6
, api
.safi
, zvrf_id(zvrf
), api
.type
,
1748 api
.instance
, api
.flags
, &p
, src_pp
, pnexthop
,
1749 ifindex
, client
->rtm_table
);
1751 client
->v6_route_del_cnt
++;
1755 /* Register zebra server router-id information. Send current router-id */
1756 static int zread_router_id_add(struct zserv
*client
, u_short length
,
1757 struct zebra_vrf
*zvrf
)
1761 /* Router-id information is needed. */
1762 vrf_bitmap_set(client
->ridinfo
, zvrf_id(zvrf
));
1764 router_id_get(&p
, zvrf_id(zvrf
));
1766 return zsend_router_id_update(client
, &p
, zvrf_id(zvrf
));
1769 /* Unregister zebra server router-id information. */
1770 static int zread_router_id_delete(struct zserv
*client
, u_short length
,
1771 struct zebra_vrf
*zvrf
)
1773 vrf_bitmap_unset(client
->ridinfo
, zvrf_id(zvrf
));
1777 /* Tie up route-type and client->sock */
1778 static void zread_hello(struct zserv
*client
)
1780 /* type of protocol (lib/zebra.h) */
1784 proto
= stream_getc(client
->ibuf
);
1785 instance
= stream_getw(client
->ibuf
);
1787 /* accept only dynamic routing protocols */
1788 if ((proto
< ZEBRA_ROUTE_MAX
) && (proto
> ZEBRA_ROUTE_STATIC
)) {
1790 "client %d says hello and bids fair to announce only %s routes",
1791 client
->sock
, zebra_route_string(proto
));
1793 zlog_notice("client protocol instance %d", instance
);
1795 client
->proto
= proto
;
1796 client
->instance
= instance
;
1800 /* Unregister all information in a VRF. */
1801 static int zread_vrf_unregister(struct zserv
*client
, u_short length
,
1802 struct zebra_vrf
*zvrf
)
1807 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
1808 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
1809 vrf_bitmap_unset(client
->redist
[afi
][i
], zvrf_id(zvrf
));
1810 vrf_bitmap_unset(client
->redist_default
, zvrf_id(zvrf
));
1811 vrf_bitmap_unset(client
->ifinfo
, zvrf_id(zvrf
));
1812 vrf_bitmap_unset(client
->ridinfo
, zvrf_id(zvrf
));
1817 static void zread_mpls_labels(int command
, struct zserv
*client
, u_short length
,
1821 enum lsp_types_t type
;
1822 struct prefix prefix
;
1823 enum nexthop_types_t gtype
;
1826 mpls_label_t in_label
, out_label
;
1828 struct zebra_vrf
*zvrf
;
1830 zvrf
= vrf_info_lookup(vrf_id
);
1834 /* Get input stream. */
1838 type
= stream_getc(s
);
1839 prefix
.family
= stream_getl(s
);
1840 switch (prefix
.family
) {
1842 prefix
.u
.prefix4
.s_addr
= stream_get_ipv4(s
);
1843 prefix
.prefixlen
= stream_getc(s
);
1844 gate
.ipv4
.s_addr
= stream_get_ipv4(s
);
1847 stream_get(&prefix
.u
.prefix6
, s
, 16);
1848 prefix
.prefixlen
= stream_getc(s
);
1849 stream_get(&gate
.ipv6
, s
, 16);
1854 ifindex
= stream_getl(s
);
1855 distance
= stream_getc(s
);
1856 in_label
= stream_getl(s
);
1857 out_label
= stream_getl(s
);
1859 switch (prefix
.family
) {
1862 gtype
= NEXTHOP_TYPE_IPV4_IFINDEX
;
1864 gtype
= NEXTHOP_TYPE_IPV4
;
1868 gtype
= NEXTHOP_TYPE_IPV6_IFINDEX
;
1870 gtype
= NEXTHOP_TYPE_IPV6
;
1879 if (command
== ZEBRA_MPLS_LABELS_ADD
) {
1880 mpls_lsp_install(zvrf
, type
, in_label
, out_label
, gtype
, &gate
,
1882 if (out_label
!= MPLS_IMP_NULL_LABEL
)
1883 mpls_ftn_update(1, zvrf
, type
, &prefix
, gtype
, &gate
,
1884 ifindex
, distance
, out_label
);
1885 } else if (command
== ZEBRA_MPLS_LABELS_DELETE
) {
1886 mpls_lsp_uninstall(zvrf
, type
, in_label
, gtype
, &gate
, ifindex
);
1887 if (out_label
!= MPLS_IMP_NULL_LABEL
)
1888 mpls_ftn_update(0, zvrf
, type
, &prefix
, gtype
, &gate
,
1889 ifindex
, distance
, out_label
);
1892 /* Send response to a label manager connect request to client */
1893 static int zsend_label_manager_connect_response(struct zserv
*client
,
1894 vrf_id_t vrf_id
, u_short result
)
1901 zserv_create_header(s
, ZEBRA_LABEL_MANAGER_CONNECT
, vrf_id
);
1904 stream_putc(s
, result
);
1906 /* Write packet size. */
1907 stream_putw_at(s
, 0, stream_get_endp(s
));
1909 return writen(client
->sock
, s
->data
, stream_get_endp(s
));
1912 static void zread_label_manager_connect(struct zserv
*client
, vrf_id_t vrf_id
)
1915 /* type of protocol (lib/zebra.h) */
1919 /* Get input stream. */
1923 proto
= stream_getc(s
);
1924 instance
= stream_getw(s
);
1926 /* accept only dynamic routing protocols */
1927 if ((proto
>= ZEBRA_ROUTE_MAX
) || (proto
<= ZEBRA_ROUTE_STATIC
)) {
1928 zlog_err("client %d has wrong protocol %s", client
->sock
,
1929 zebra_route_string(proto
));
1930 zsend_label_manager_connect_response(client
, vrf_id
, 1);
1933 zlog_notice("client %d with instance %u connected as %s", client
->sock
,
1934 instance
, zebra_route_string(proto
));
1935 client
->proto
= proto
;
1936 client
->instance
= instance
;
1939 Release previous labels of same protocol and instance.
1940 This is done in case it restarted from an unexpected shutdown.
1942 release_daemon_chunks(proto
, instance
);
1945 " Label Manager client connected: sock %d, proto %s, instance %u",
1946 client
->sock
, zebra_route_string(proto
), instance
);
1947 /* send response back */
1948 zsend_label_manager_connect_response(client
, vrf_id
, 0);
1950 /* Send response to a get label chunk request to client */
1951 static int zsend_assign_label_chunk_response(struct zserv
*client
,
1953 struct label_manager_chunk
*lmc
)
1960 zserv_create_header(s
, ZEBRA_GET_LABEL_CHUNK
, vrf_id
);
1964 stream_putc(s
, lmc
->keep
);
1965 /* start and end labels */
1966 stream_putl(s
, lmc
->start
);
1967 stream_putl(s
, lmc
->end
);
1970 /* Write packet size. */
1971 stream_putw_at(s
, 0, stream_get_endp(s
));
1973 return writen(client
->sock
, s
->data
, stream_get_endp(s
));
1976 static void zread_get_label_chunk(struct zserv
*client
, vrf_id_t vrf_id
)
1981 struct label_manager_chunk
*lmc
;
1983 /* Get input stream. */
1987 keep
= stream_getc(s
);
1988 size
= stream_getl(s
);
1990 lmc
= assign_label_chunk(client
->proto
, client
->instance
, keep
, size
);
1992 zlog_err("%s: Unable to assign Label Chunk of size %u",
1995 zlog_debug("Assigned Label Chunk %u - %u to %u", lmc
->start
,
1997 /* send response back */
1998 zsend_assign_label_chunk_response(client
, vrf_id
, lmc
);
2001 static void zread_release_label_chunk(struct zserv
*client
)
2004 uint32_t start
, end
;
2006 /* Get input stream. */
2010 start
= stream_getl(s
);
2011 end
= stream_getl(s
);
2013 release_label_chunk(client
->proto
, client
->instance
, start
, end
);
2015 static void zread_label_manager_request(int cmd
, struct zserv
*client
,
2018 /* to avoid sending other messages like ZERBA_INTERFACE_UP */
2019 if (cmd
== ZEBRA_LABEL_MANAGER_CONNECT
)
2020 client
->is_synchronous
= 1;
2022 /* external label manager */
2024 zread_relay_label_manager_request(cmd
, client
, vrf_id
);
2025 /* this is a label manager */
2027 if (cmd
== ZEBRA_LABEL_MANAGER_CONNECT
)
2028 zread_label_manager_connect(client
, vrf_id
);
2030 /* Sanity: don't allow 'unidentified' requests */
2031 if (!client
->proto
) {
2033 "Got label request from an unidentified client");
2036 if (cmd
== ZEBRA_GET_LABEL_CHUNK
)
2037 zread_get_label_chunk(client
, vrf_id
);
2038 else if (cmd
== ZEBRA_RELEASE_LABEL_CHUNK
)
2039 zread_release_label_chunk(client
);
2044 /* Cleanup registered nexthops (across VRFs) upon client disconnect. */
2045 static void zebra_client_close_cleanup_rnh(struct zserv
*client
)
2048 struct zebra_vrf
*zvrf
;
2050 RB_FOREACH(vrf
, vrf_id_head
, &vrfs_by_id
)
2052 if ((zvrf
= vrf
->info
) != NULL
) {
2053 zebra_cleanup_rnh_client(zvrf_id(zvrf
), AF_INET
, client
,
2055 zebra_cleanup_rnh_client(zvrf_id(zvrf
), AF_INET6
,
2056 client
, RNH_NEXTHOP_TYPE
);
2057 zebra_cleanup_rnh_client(zvrf_id(zvrf
), AF_INET
, client
,
2058 RNH_IMPORT_CHECK_TYPE
);
2059 zebra_cleanup_rnh_client(zvrf_id(zvrf
), AF_INET6
,
2060 client
, RNH_IMPORT_CHECK_TYPE
);
2061 if (client
->proto
== ZEBRA_ROUTE_LDP
) {
2062 hash_iterate(zvrf
->lsp_table
,
2063 mpls_ldp_lsp_uninstall_all
,
2065 mpls_ldp_ftn_uninstall_all(zvrf
, AFI_IP
);
2066 mpls_ldp_ftn_uninstall_all(zvrf
, AFI_IP6
);
2072 /* Close zebra client. */
2073 static void zebra_client_close(struct zserv
*client
)
2075 /* Send client de-registration to BFD */
2076 zebra_ptm_bfd_client_deregister(client
->proto
);
2078 /* Cleanup any registered nexthops - across all VRFs. */
2079 zebra_client_close_cleanup_rnh(client
);
2081 /* Release Label Manager chunks */
2082 release_daemon_chunks(client
->proto
, client
->instance
);
2084 /* Cleanup any FECs registered by this client. */
2085 zebra_mpls_cleanup_fecs_for_client(vrf_info_lookup(VRF_DEFAULT
),
2088 /* Close file descriptor. */
2090 unsigned long nroutes
;
2092 close(client
->sock
);
2093 nroutes
= rib_score_proto(client
->proto
, client
->instance
);
2095 "client %d disconnected. %lu %s routes removed from the rib",
2096 client
->sock
, nroutes
,
2097 zebra_route_string(client
->proto
));
2101 /* Free stream buffers. */
2103 stream_free(client
->ibuf
);
2105 stream_free(client
->obuf
);
2107 buffer_free(client
->wb
);
2109 /* Release threads. */
2111 thread_cancel(client
->t_read
);
2112 if (client
->t_write
)
2113 thread_cancel(client
->t_write
);
2114 if (client
->t_suicide
)
2115 thread_cancel(client
->t_suicide
);
2118 for (afi_t afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
2119 for (int i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
2120 vrf_bitmap_free(client
->redist
[afi
][i
]);
2122 vrf_bitmap_free(client
->redist_default
);
2123 vrf_bitmap_free(client
->ifinfo
);
2124 vrf_bitmap_free(client
->ridinfo
);
2126 /* Free client structure. */
2127 listnode_delete(zebrad
.client_list
, client
);
2128 XFREE(MTYPE_TMP
, client
);
2131 /* Make new client. */
2132 static void zebra_client_create(int sock
)
2134 struct zserv
*client
;
2138 client
= XCALLOC(MTYPE_TMP
, sizeof(struct zserv
));
2140 /* Make client input/output buffer. */
2141 client
->sock
= sock
;
2142 client
->ibuf
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
2143 client
->obuf
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
2144 client
->wb
= buffer_new(0);
2146 /* Set table number. */
2147 client
->rtm_table
= zebrad
.rtm_table_default
;
2149 client
->connect_time
= monotime(NULL
);
2150 /* Initialize flags */
2151 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
2152 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
2153 client
->redist
[afi
][i
] = vrf_bitmap_init();
2154 client
->redist_default
= vrf_bitmap_init();
2155 client
->ifinfo
= vrf_bitmap_init();
2156 client
->ridinfo
= vrf_bitmap_init();
2158 /* by default, it's not a synchronous client */
2159 client
->is_synchronous
= 0;
2161 /* Add this client to linked list. */
2162 listnode_add(zebrad
.client_list
, client
);
2164 /* Make new read thread. */
2165 zebra_event(ZEBRA_READ
, sock
, client
);
2167 zebra_vrf_update_all(client
);
2170 static int zread_interface_set_master(struct zserv
*client
, int sock
,
2173 struct interface
*master
;
2174 struct interface
*slave
;
2175 struct stream
*s
= client
->ibuf
;
2179 vrf_id
= stream_getw(s
);
2180 ifindex
= stream_getl(s
);
2181 master
= if_lookup_by_index(ifindex
, vrf_id
);
2183 vrf_id
= stream_getw(s
);
2184 ifindex
= stream_getl(s
);
2185 slave
= if_lookup_by_index(ifindex
, vrf_id
);
2187 if (!master
|| !slave
)
2190 kernel_interface_set_master(master
, slave
);
2195 /* Handler of zebra service request. */
2196 static int zebra_client_read(struct thread
*thread
)
2199 struct zserv
*client
;
2201 uint16_t length
, command
;
2202 uint8_t marker
, version
;
2204 struct zebra_vrf
*zvrf
;
2206 /* Get thread data. Reset reading thread because I'm running. */
2207 sock
= THREAD_FD(thread
);
2208 client
= THREAD_ARG(thread
);
2209 client
->t_read
= NULL
;
2211 if (client
->t_suicide
) {
2212 zebra_client_close(client
);
2216 /* Read length and command (if we don't have it already). */
2217 if ((already
= stream_get_endp(client
->ibuf
)) < ZEBRA_HEADER_SIZE
) {
2219 if (((nbyte
= stream_read_try(client
->ibuf
, sock
,
2220 ZEBRA_HEADER_SIZE
- already
))
2223 if (IS_ZEBRA_DEBUG_EVENT
)
2224 zlog_debug("connection closed socket [%d]",
2226 zebra_client_close(client
);
2229 if (nbyte
!= (ssize_t
)(ZEBRA_HEADER_SIZE
- already
)) {
2230 /* Try again later. */
2231 zebra_event(ZEBRA_READ
, sock
, client
);
2234 already
= ZEBRA_HEADER_SIZE
;
2237 /* Reset to read from the beginning of the incoming packet. */
2238 stream_set_getp(client
->ibuf
, 0);
2240 /* Fetch header values */
2241 length
= stream_getw(client
->ibuf
);
2242 marker
= stream_getc(client
->ibuf
);
2243 version
= stream_getc(client
->ibuf
);
2244 vrf_id
= stream_getw(client
->ibuf
);
2245 command
= stream_getw(client
->ibuf
);
2247 if (marker
!= ZEBRA_HEADER_MARKER
|| version
!= ZSERV_VERSION
) {
2249 "%s: socket %d version mismatch, marker %d, version %d",
2250 __func__
, sock
, marker
, version
);
2251 zebra_client_close(client
);
2254 if (length
< ZEBRA_HEADER_SIZE
) {
2256 "%s: socket %d message length %u is less than header size %d",
2257 __func__
, sock
, length
, ZEBRA_HEADER_SIZE
);
2258 zebra_client_close(client
);
2261 if (length
> STREAM_SIZE(client
->ibuf
)) {
2263 "%s: socket %d message length %u exceeds buffer size %lu",
2264 __func__
, sock
, length
,
2265 (u_long
)STREAM_SIZE(client
->ibuf
));
2266 zebra_client_close(client
);
2270 /* Read rest of data. */
2271 if (already
< length
) {
2273 if (((nbyte
= stream_read_try(client
->ibuf
, sock
,
2277 if (IS_ZEBRA_DEBUG_EVENT
)
2279 "connection closed [%d] when reading zebra data",
2281 zebra_client_close(client
);
2284 if (nbyte
!= (ssize_t
)(length
- already
)) {
2285 /* Try again later. */
2286 zebra_event(ZEBRA_READ
, sock
, client
);
2291 length
-= ZEBRA_HEADER_SIZE
;
2293 /* Debug packet information. */
2294 if (IS_ZEBRA_DEBUG_EVENT
)
2295 zlog_debug("zebra message comes from socket [%d]", sock
);
2297 if (IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
)
2298 zlog_debug("zebra message received [%s] %d in VRF %u",
2299 zserv_command_string(command
), length
, vrf_id
);
2301 client
->last_read_time
= monotime(NULL
);
2302 client
->last_read_cmd
= command
;
2304 zvrf
= zebra_vrf_lookup_by_id(vrf_id
);
2306 if (IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
)
2307 zlog_debug("zebra received unknown VRF[%u]", vrf_id
);
2308 goto zclient_read_out
;
2312 case ZEBRA_ROUTER_ID_ADD
:
2313 zread_router_id_add(client
, length
, zvrf
);
2315 case ZEBRA_ROUTER_ID_DELETE
:
2316 zread_router_id_delete(client
, length
, zvrf
);
2318 case ZEBRA_INTERFACE_ADD
:
2319 zread_interface_add(client
, length
, zvrf
);
2321 case ZEBRA_INTERFACE_DELETE
:
2322 zread_interface_delete(client
, length
, zvrf
);
2324 case ZEBRA_IPV4_ROUTE_ADD
:
2325 zread_ipv4_add(client
, length
, zvrf
);
2327 case ZEBRA_IPV4_ROUTE_DELETE
:
2328 zread_ipv4_delete(client
, length
, zvrf
);
2330 case ZEBRA_IPV4_ROUTE_IPV6_NEXTHOP_ADD
:
2331 zread_ipv4_route_ipv6_nexthop_add(client
, length
, zvrf
);
2333 case ZEBRA_IPV4_NEXTHOP_ADD
:
2334 zread_ipv4_add(client
, length
,
2335 zvrf
); /* LB: r1.0 merge - id was 1 */
2337 case ZEBRA_IPV4_NEXTHOP_DELETE
:
2338 zread_ipv4_delete(client
, length
,
2339 zvrf
); /* LB: r1.0 merge - id was 1 */
2341 case ZEBRA_IPV6_ROUTE_ADD
:
2342 zread_ipv6_add(client
, length
, zvrf
);
2344 case ZEBRA_IPV6_ROUTE_DELETE
:
2345 zread_ipv6_delete(client
, length
, zvrf
);
2347 case ZEBRA_REDISTRIBUTE_ADD
:
2348 zebra_redistribute_add(command
, client
, length
, zvrf
);
2350 case ZEBRA_REDISTRIBUTE_DELETE
:
2351 zebra_redistribute_delete(command
, client
, length
, zvrf
);
2353 case ZEBRA_REDISTRIBUTE_DEFAULT_ADD
:
2354 zebra_redistribute_default_add(command
, client
, length
, zvrf
);
2356 case ZEBRA_REDISTRIBUTE_DEFAULT_DELETE
:
2357 zebra_redistribute_default_delete(command
, client
, length
,
2360 case ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB
:
2361 zread_ipv4_nexthop_lookup_mrib(client
, length
, zvrf
);
2364 zread_hello(client
);
2366 case ZEBRA_NEXTHOP_REGISTER
:
2367 zserv_rnh_register(client
, sock
, length
, RNH_NEXTHOP_TYPE
,
2370 case ZEBRA_NEXTHOP_UNREGISTER
:
2371 zserv_rnh_unregister(client
, sock
, length
, RNH_NEXTHOP_TYPE
,
2374 case ZEBRA_IMPORT_ROUTE_REGISTER
:
2375 zserv_rnh_register(client
, sock
, length
, RNH_IMPORT_CHECK_TYPE
,
2378 case ZEBRA_IMPORT_ROUTE_UNREGISTER
:
2379 zserv_rnh_unregister(client
, sock
, length
,
2380 RNH_IMPORT_CHECK_TYPE
, zvrf
);
2382 case ZEBRA_BFD_DEST_UPDATE
:
2383 case ZEBRA_BFD_DEST_REGISTER
:
2384 zebra_ptm_bfd_dst_register(client
, sock
, length
, command
, zvrf
);
2386 case ZEBRA_BFD_DEST_DEREGISTER
:
2387 zebra_ptm_bfd_dst_deregister(client
, sock
, length
, zvrf
);
2389 case ZEBRA_VRF_UNREGISTER
:
2390 zread_vrf_unregister(client
, length
, zvrf
);
2392 case ZEBRA_BFD_CLIENT_REGISTER
:
2393 zebra_ptm_bfd_client_register(client
, sock
, length
);
2395 case ZEBRA_INTERFACE_ENABLE_RADV
:
2396 #if defined(HAVE_RTADV)
2397 zebra_interface_radv_set(client
, sock
, length
, zvrf
, 1);
2400 case ZEBRA_INTERFACE_DISABLE_RADV
:
2401 #if defined(HAVE_RTADV)
2402 zebra_interface_radv_set(client
, sock
, length
, zvrf
, 0);
2405 case ZEBRA_MPLS_LABELS_ADD
:
2406 case ZEBRA_MPLS_LABELS_DELETE
:
2407 zread_mpls_labels(command
, client
, length
, vrf_id
);
2409 case ZEBRA_IPMR_ROUTE_STATS
:
2410 zebra_ipmr_route_stats(client
, sock
, length
, zvrf
);
2412 case ZEBRA_LABEL_MANAGER_CONNECT
:
2413 case ZEBRA_GET_LABEL_CHUNK
:
2414 case ZEBRA_RELEASE_LABEL_CHUNK
:
2415 zread_label_manager_request(command
, client
, vrf_id
);
2417 case ZEBRA_FEC_REGISTER
:
2418 zserv_fec_register(client
, sock
, length
);
2420 case ZEBRA_FEC_UNREGISTER
:
2421 zserv_fec_unregister(client
, sock
, length
);
2423 case ZEBRA_ADVERTISE_ALL_VNI
:
2424 zebra_vxlan_advertise_all_vni(client
, sock
, length
, zvrf
);
2426 case ZEBRA_REMOTE_VTEP_ADD
:
2427 zebra_vxlan_remote_vtep_add(client
, sock
, length
, zvrf
);
2429 case ZEBRA_REMOTE_VTEP_DEL
:
2430 zebra_vxlan_remote_vtep_del(client
, sock
, length
, zvrf
);
2432 case ZEBRA_REMOTE_MACIP_ADD
:
2433 zebra_vxlan_remote_macip_add(client
, sock
, length
, zvrf
);
2435 case ZEBRA_REMOTE_MACIP_DEL
:
2436 zebra_vxlan_remote_macip_del(client
, sock
, length
, zvrf
);
2438 case ZEBRA_INTERFACE_SET_MASTER
:
2439 zread_interface_set_master(client
, sock
, length
);
2442 zlog_info("Zebra received unknown command %d", command
);
2446 if (client
->t_suicide
) {
2447 /* No need to wait for thread callback, just kill immediately.
2449 zebra_client_close(client
);
2454 stream_reset(client
->ibuf
);
2455 zebra_event(ZEBRA_READ
, sock
, client
);
2460 /* Accept code of zebra server socket. */
2461 static int zebra_accept(struct thread
*thread
)
2465 struct sockaddr_in client
;
2468 accept_sock
= THREAD_FD(thread
);
2470 /* Reregister myself. */
2471 zebra_event(ZEBRA_SERV
, accept_sock
, NULL
);
2473 len
= sizeof(struct sockaddr_in
);
2474 client_sock
= accept(accept_sock
, (struct sockaddr
*)&client
, &len
);
2476 if (client_sock
< 0) {
2477 zlog_warn("Can't accept zebra socket: %s",
2478 safe_strerror(errno
));
2482 /* Make client socket non-blocking. */
2483 set_nonblocking(client_sock
);
2485 /* Create new zebra client. */
2486 zebra_client_create(client_sock
);
2491 /* Make zebra server socket, wiping any existing one (see bug #403). */
2492 void zebra_zserv_socket_init(char *path
)
2497 struct sockaddr_storage sa
;
2500 if (!frr_zclient_addr(&sa
, &sa_len
, path
))
2501 /* should be caught in zebra main() */
2505 old_mask
= umask(0077);
2507 /* Make UNIX domain socket. */
2508 sock
= socket(sa
.ss_family
, SOCK_STREAM
, 0);
2510 zlog_warn("Can't create zserv socket: %s",
2511 safe_strerror(errno
));
2513 "zebra can't provide full functionality due to above error");
2517 if (sa
.ss_family
!= AF_UNIX
) {
2518 sockopt_reuseaddr(sock
);
2519 sockopt_reuseport(sock
);
2521 struct sockaddr_un
*suna
= (struct sockaddr_un
*)&sa
;
2522 if (suna
->sun_path
[0])
2523 unlink(suna
->sun_path
);
2526 if (zserv_privs
.change(ZPRIVS_RAISE
))
2527 zlog_err("Can't raise privileges");
2529 ret
= bind(sock
, (struct sockaddr
*)&sa
, sa_len
);
2531 zlog_warn("Can't bind zserv socket on %s: %s", path
,
2532 safe_strerror(errno
));
2534 "zebra can't provide full functionality due to above error");
2538 if (zserv_privs
.change(ZPRIVS_LOWER
))
2539 zlog_err("Can't lower privileges");
2541 ret
= listen(sock
, 5);
2543 zlog_warn("Can't listen to zserv socket %s: %s", path
,
2544 safe_strerror(errno
));
2546 "zebra can't provide full functionality due to above error");
2553 zebra_event(ZEBRA_SERV
, sock
, NULL
);
2557 static void zebra_event(enum event event
, int sock
, struct zserv
*client
)
2561 thread_add_read(zebrad
.master
, zebra_accept
, client
, sock
,
2565 client
->t_read
= NULL
;
2566 thread_add_read(zebrad
.master
, zebra_client_read
, client
, sock
,
2575 #define ZEBRA_TIME_BUF 32
2576 static char *zserv_time_buf(time_t *time1
, char *buf
, int buflen
)
2581 assert(buf
!= NULL
);
2582 assert(buflen
>= ZEBRA_TIME_BUF
);
2583 assert(time1
!= NULL
);
2586 snprintf(buf
, buflen
, "never ");
2590 now
= monotime(NULL
);
2594 /* Making formatted timer strings. */
2595 #define ONE_DAY_SECOND 60*60*24
2596 #define ONE_WEEK_SECOND 60*60*24*7
2598 if (now
< ONE_DAY_SECOND
)
2599 snprintf(buf
, buflen
, "%02d:%02d:%02d", tm
->tm_hour
, tm
->tm_min
,
2601 else if (now
< ONE_WEEK_SECOND
)
2602 snprintf(buf
, buflen
, "%dd%02dh%02dm", tm
->tm_yday
, tm
->tm_hour
,
2605 snprintf(buf
, buflen
, "%02dw%dd%02dh", tm
->tm_yday
/ 7,
2606 tm
->tm_yday
- ((tm
->tm_yday
/ 7) * 7), tm
->tm_hour
);
2610 static void zebra_show_client_detail(struct vty
*vty
, struct zserv
*client
)
2612 char cbuf
[ZEBRA_TIME_BUF
], rbuf
[ZEBRA_TIME_BUF
];
2613 char wbuf
[ZEBRA_TIME_BUF
], nhbuf
[ZEBRA_TIME_BUF
], mbuf
[ZEBRA_TIME_BUF
];
2615 vty_out(vty
, "Client: %s", zebra_route_string(client
->proto
));
2616 if (client
->instance
)
2617 vty_out(vty
, " Instance: %d", client
->instance
);
2620 vty_out(vty
, "------------------------ \n");
2621 vty_out(vty
, "FD: %d \n", client
->sock
);
2622 vty_out(vty
, "Route Table ID: %d \n", client
->rtm_table
);
2624 vty_out(vty
, "Connect Time: %s \n",
2625 zserv_time_buf(&client
->connect_time
, cbuf
, ZEBRA_TIME_BUF
));
2626 if (client
->nh_reg_time
) {
2627 vty_out(vty
, "Nexthop Registry Time: %s \n",
2628 zserv_time_buf(&client
->nh_reg_time
, nhbuf
,
2630 if (client
->nh_last_upd_time
)
2631 vty_out(vty
, "Nexthop Last Update Time: %s \n",
2632 zserv_time_buf(&client
->nh_last_upd_time
, mbuf
,
2635 vty_out(vty
, "No Nexthop Update sent\n");
2637 vty_out(vty
, "Not registered for Nexthop Updates\n");
2639 vty_out(vty
, "Last Msg Rx Time: %s \n",
2640 zserv_time_buf(&client
->last_read_time
, rbuf
, ZEBRA_TIME_BUF
));
2641 vty_out(vty
, "Last Msg Tx Time: %s \n",
2642 zserv_time_buf(&client
->last_write_time
, wbuf
, ZEBRA_TIME_BUF
));
2643 if (client
->last_read_time
)
2644 vty_out(vty
, "Last Rcvd Cmd: %s \n",
2645 zserv_command_string(client
->last_read_cmd
));
2646 if (client
->last_write_time
)
2647 vty_out(vty
, "Last Sent Cmd: %s \n",
2648 zserv_command_string(client
->last_write_cmd
));
2651 vty_out(vty
, "Type Add Update Del \n");
2652 vty_out(vty
, "================================================== \n");
2653 vty_out(vty
, "IPv4 %-12d%-12d%-12d\n", client
->v4_route_add_cnt
,
2654 client
->v4_route_upd8_cnt
, client
->v4_route_del_cnt
);
2655 vty_out(vty
, "IPv6 %-12d%-12d%-12d\n", client
->v6_route_add_cnt
,
2656 client
->v6_route_upd8_cnt
, client
->v6_route_del_cnt
);
2657 vty_out(vty
, "Redist:v4 %-12d%-12d%-12d\n", client
->redist_v4_add_cnt
,
2658 0, client
->redist_v4_del_cnt
);
2659 vty_out(vty
, "Redist:v6 %-12d%-12d%-12d\n", client
->redist_v6_add_cnt
,
2660 0, client
->redist_v6_del_cnt
);
2661 vty_out(vty
, "Connected %-12d%-12d%-12d\n", client
->ifadd_cnt
, 0,
2663 vty_out(vty
, "BFD peer %-12d%-12d%-12d\n", client
->bfd_peer_add_cnt
,
2664 client
->bfd_peer_upd8_cnt
, client
->bfd_peer_del_cnt
);
2665 vty_out(vty
, "Interface Up Notifications: %d\n", client
->ifup_cnt
);
2666 vty_out(vty
, "Interface Down Notifications: %d\n", client
->ifdown_cnt
);
2667 vty_out(vty
, "VNI add notifications: %d\n", client
->vniadd_cnt
);
2668 vty_out(vty
, "VNI delete notifications: %d\n", client
->vnidel_cnt
);
2669 vty_out(vty
, "MAC-IP add notifications: %d\n", client
->macipadd_cnt
);
2670 vty_out(vty
, "MAC-IP delete notifications: %d\n", client
->macipdel_cnt
);
2676 static void zebra_show_client_brief(struct vty
*vty
, struct zserv
*client
)
2678 char cbuf
[ZEBRA_TIME_BUF
], rbuf
[ZEBRA_TIME_BUF
];
2679 char wbuf
[ZEBRA_TIME_BUF
];
2681 vty_out(vty
, "%-8s%12s %12s%12s%8d/%-8d%8d/%-8d\n",
2682 zebra_route_string(client
->proto
),
2683 zserv_time_buf(&client
->connect_time
, cbuf
, ZEBRA_TIME_BUF
),
2684 zserv_time_buf(&client
->last_read_time
, rbuf
, ZEBRA_TIME_BUF
),
2685 zserv_time_buf(&client
->last_write_time
, wbuf
, ZEBRA_TIME_BUF
),
2686 client
->v4_route_add_cnt
+ client
->v4_route_upd8_cnt
,
2687 client
->v4_route_del_cnt
,
2688 client
->v6_route_add_cnt
+ client
->v6_route_upd8_cnt
,
2689 client
->v6_route_del_cnt
);
2692 struct zserv
*zebra_find_client(u_char proto
)
2694 struct listnode
*node
, *nnode
;
2695 struct zserv
*client
;
2697 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, node
, nnode
, client
)) {
2698 if (client
->proto
== proto
)
2706 /* Display default rtm_table for all clients. */
2711 "default routing table to use for all clients\n")
2713 vty_out(vty
, "table %d\n", zebrad
.rtm_table_default
);
2717 DEFUN (config_table
,
2720 "Configure target kernel routing table\n"
2723 zebrad
.rtm_table_default
= strtol(argv
[1]->arg
, (char **)0, 10);
2727 DEFUN (no_config_table
,
2728 no_config_table_cmd
,
2729 "no table [TABLENO]",
2731 "Configure target kernel routing table\n"
2734 zebrad
.rtm_table_default
= 0;
2739 DEFUN (ip_forwarding
,
2743 "Turn on IP forwarding")
2749 ret
= ipforward_on();
2752 vty_out(vty
, "Can't turn on IP forwarding\n");
2753 return CMD_WARNING_CONFIG_FAILED
;
2759 DEFUN (no_ip_forwarding
,
2760 no_ip_forwarding_cmd
,
2764 "Turn off IP forwarding")
2770 ret
= ipforward_off();
2773 vty_out(vty
, "Can't turn off IP forwarding\n");
2774 return CMD_WARNING_CONFIG_FAILED
;
2784 "Zebra information\n")
2789 " Route Route Neighbor LSP LSP\n");
2791 "VRF Installs Removals Updates Installs Removals\n");
2792 RB_FOREACH(vrf
, vrf_name_head
, &vrfs_by_name
)
2794 struct zebra_vrf
*zvrf
= vrf
->info
;
2795 vty_out(vty
, "%-25s %10" PRIu64
" %10" PRIu64
" %10" PRIu64
2796 " %10" PRIu64
" %10" PRIu64
"\n",
2797 vrf
->name
, zvrf
->installs
, zvrf
->removals
,
2798 zvrf
->neigh_updates
, zvrf
->lsp_installs
,
2799 zvrf
->lsp_removals
);
2805 /* This command is for debugging purpose. */
2806 DEFUN (show_zebra_client
,
2807 show_zebra_client_cmd
,
2808 "show zebra client",
2810 "Zebra information\n"
2811 "Client information\n")
2813 struct listnode
*node
;
2814 struct zserv
*client
;
2816 for (ALL_LIST_ELEMENTS_RO(zebrad
.client_list
, node
, client
))
2817 zebra_show_client_detail(vty
, client
);
2822 /* This command is for debugging purpose. */
2823 DEFUN (show_zebra_client_summary
,
2824 show_zebra_client_summary_cmd
,
2825 "show zebra client summary",
2827 "Zebra information brief\n"
2828 "Client information brief\n"
2831 struct listnode
*node
;
2832 struct zserv
*client
;
2835 "Name Connect Time Last Read Last Write IPv4 Routes IPv6 Routes \n");
2837 "--------------------------------------------------------------------------------\n");
2839 for (ALL_LIST_ELEMENTS_RO(zebrad
.client_list
, node
, client
))
2840 zebra_show_client_brief(vty
, client
);
2842 vty_out(vty
, "Routes column shows (added+updated)/deleted\n");
2846 /* Table configuration write function. */
2847 static int config_write_table(struct vty
*vty
)
2849 if (zebrad
.rtm_table_default
)
2850 vty_out(vty
, "table %d\n", zebrad
.rtm_table_default
);
2854 /* table node for routing tables. */
2855 static struct cmd_node table_node
= {TABLE_NODE
,
2856 "", /* This node has no interface. */
2859 /* Only display ip forwarding is enabled or not. */
2860 DEFUN (show_ip_forwarding
,
2861 show_ip_forwarding_cmd
,
2862 "show ip forwarding",
2865 "IP forwarding status\n")
2872 vty_out(vty
, "IP forwarding is off\n");
2874 vty_out(vty
, "IP forwarding is on\n");
2878 /* Only display ipv6 forwarding is enabled or not. */
2879 DEFUN (show_ipv6_forwarding
,
2880 show_ipv6_forwarding_cmd
,
2881 "show ipv6 forwarding",
2883 "IPv6 information\n"
2884 "Forwarding status\n")
2888 ret
= ipforward_ipv6();
2892 vty_out(vty
, "ipv6 forwarding is unknown\n");
2895 vty_out(vty
, "ipv6 forwarding is %s\n", "off");
2898 vty_out(vty
, "ipv6 forwarding is %s\n", "on");
2901 vty_out(vty
, "ipv6 forwarding is %s\n", "off");
2907 DEFUN (ipv6_forwarding
,
2908 ipv6_forwarding_cmd
,
2911 "Turn on IPv6 forwarding")
2915 ret
= ipforward_ipv6();
2917 ret
= ipforward_ipv6_on();
2920 vty_out(vty
, "Can't turn on IPv6 forwarding\n");
2921 return CMD_WARNING_CONFIG_FAILED
;
2927 DEFUN (no_ipv6_forwarding
,
2928 no_ipv6_forwarding_cmd
,
2929 "no ipv6 forwarding",
2932 "Turn off IPv6 forwarding")
2936 ret
= ipforward_ipv6();
2938 ret
= ipforward_ipv6_off();
2941 vty_out(vty
, "Can't turn off IPv6 forwarding\n");
2942 return CMD_WARNING_CONFIG_FAILED
;
2948 /* IPForwarding configuration write function. */
2949 static int config_write_forwarding(struct vty
*vty
)
2951 /* FIXME: Find better place for that. */
2952 router_id_write(vty
);
2955 vty_out(vty
, "no ip forwarding\n");
2956 if (!ipforward_ipv6())
2957 vty_out(vty
, "no ipv6 forwarding\n");
2958 vty_out(vty
, "!\n");
2962 /* table node for routing tables. */
2963 static struct cmd_node forwarding_node
= {FORWARDING_NODE
,
2964 "", /* This node has no interface. */
2967 /* Initialisation of zebra and installation of commands. */
2968 void zebra_init(void)
2970 /* Client list init. */
2971 zebrad
.client_list
= list_new();
2973 /* Install configuration write function. */
2974 install_node(&table_node
, config_write_table
);
2975 install_node(&forwarding_node
, config_write_forwarding
);
2977 install_element(VIEW_NODE
, &show_ip_forwarding_cmd
);
2978 install_element(CONFIG_NODE
, &ip_forwarding_cmd
);
2979 install_element(CONFIG_NODE
, &no_ip_forwarding_cmd
);
2980 install_element(ENABLE_NODE
, &show_zebra_cmd
);
2981 install_element(ENABLE_NODE
, &show_zebra_client_cmd
);
2982 install_element(ENABLE_NODE
, &show_zebra_client_summary_cmd
);
2985 install_element(VIEW_NODE
, &show_table_cmd
);
2986 install_element(CONFIG_NODE
, &config_table_cmd
);
2987 install_element(CONFIG_NODE
, &no_config_table_cmd
);
2988 #endif /* HAVE_NETLINK */
2990 install_element(VIEW_NODE
, &show_ipv6_forwarding_cmd
);
2991 install_element(CONFIG_NODE
, &ipv6_forwarding_cmd
);
2992 install_element(CONFIG_NODE
, &no_ipv6_forwarding_cmd
);
2995 zebra_route_map_init();