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
29 #include "zebra_memory.h"
33 #include "sockunion.h"
42 #include "zebra/zserv.h"
43 #include "zebra/zebra_ns.h"
44 #include "zebra/zebra_vrf.h"
45 #include "zebra/router-id.h"
46 #include "zebra/redistribute.h"
47 #include "zebra/debug.h"
48 #include "zebra/ipforward.h"
49 #include "zebra/zebra_rnh.h"
50 #include "zebra/rt_netlink.h"
51 #include "zebra/interface.h"
52 #include "zebra/zebra_ptm.h"
53 #include "zebra/rtadv.h"
54 #include "zebra/zebra_mpls.h"
55 #include "zebra/zebra_mroute.h"
56 #include "zebra/label_manager.h"
57 #include "zebra/zebra_vxlan.h"
59 /* Event list of zebra. */
60 enum event
{ ZEBRA_SERV
, ZEBRA_READ
, ZEBRA_WRITE
};
62 static void zebra_event(enum event event
, int sock
, struct zserv
*client
);
64 extern struct zebra_privs_t zserv_privs
;
66 static void zebra_client_close(struct zserv
*client
);
68 static int zserv_delayed_close(struct thread
*thread
)
70 struct zserv
*client
= THREAD_ARG(thread
);
72 client
->t_suicide
= NULL
;
73 zebra_client_close(client
);
77 static int zserv_flush_data(struct thread
*thread
)
79 struct zserv
*client
= THREAD_ARG(thread
);
81 client
->t_write
= NULL
;
82 if (client
->t_suicide
) {
83 zebra_client_close(client
);
86 switch (buffer_flush_available(client
->wb
, client
->sock
)) {
89 "%s: buffer_flush_available failed on zserv client fd %d, "
91 __func__
, client
->sock
);
92 zebra_client_close(client
);
96 client
->t_write
= NULL
;
97 thread_add_write(zebrad
.master
, zserv_flush_data
, client
,
98 client
->sock
, &client
->t_write
);
105 client
->last_write_time
= monotime(NULL
);
109 int zebra_server_send_message(struct zserv
*client
)
111 if (client
->t_suicide
)
114 if (client
->is_synchronous
)
117 stream_set_getp(client
->obuf
, 0);
118 client
->last_write_cmd
= stream_getw_from(client
->obuf
, 6);
119 switch (buffer_write(client
->wb
, client
->sock
,
120 STREAM_DATA(client
->obuf
),
121 stream_get_endp(client
->obuf
))) {
124 "%s: buffer_write failed to zserv client fd %d, closing",
125 __func__
, client
->sock
);
126 /* Schedule a delayed close since many of the functions that
128 one do not check the return code. They do not allow for the
129 possibility that an I/O error may have caused the client to
132 client
->t_suicide
= NULL
;
133 thread_add_event(zebrad
.master
, zserv_delayed_close
, client
, 0,
137 THREAD_OFF(client
->t_write
);
140 thread_add_write(zebrad
.master
, zserv_flush_data
, client
,
141 client
->sock
, &client
->t_write
);
145 client
->last_write_time
= monotime(NULL
);
149 void zserv_create_header(struct stream
*s
, uint16_t cmd
, vrf_id_t vrf_id
)
151 /* length placeholder, caller can update */
152 stream_putw(s
, ZEBRA_HEADER_SIZE
);
153 stream_putc(s
, ZEBRA_HEADER_MARKER
);
154 stream_putc(s
, ZSERV_VERSION
);
155 stream_putw(s
, vrf_id
);
159 static void zserv_encode_interface(struct stream
*s
, struct interface
*ifp
)
161 /* Interface information. */
162 stream_put(s
, ifp
->name
, INTERFACE_NAMSIZ
);
163 stream_putl(s
, ifp
->ifindex
);
164 stream_putc(s
, ifp
->status
);
165 stream_putq(s
, ifp
->flags
);
166 stream_putc(s
, ifp
->ptm_enable
);
167 stream_putc(s
, ifp
->ptm_status
);
168 stream_putl(s
, ifp
->metric
);
169 stream_putl(s
, ifp
->speed
);
170 stream_putl(s
, ifp
->mtu
);
171 stream_putl(s
, ifp
->mtu6
);
172 stream_putl(s
, ifp
->bandwidth
);
173 stream_putl(s
, ifp
->ll_type
);
174 stream_putl(s
, ifp
->hw_addr_len
);
175 if (ifp
->hw_addr_len
)
176 stream_put(s
, ifp
->hw_addr
, ifp
->hw_addr_len
);
178 /* Then, Traffic Engineering parameters if any */
179 if (HAS_LINK_PARAMS(ifp
) && IS_LINK_PARAMS_SET(ifp
->link_params
)) {
181 zebra_interface_link_params_write(s
, ifp
);
185 /* Write packet size. */
186 stream_putw_at(s
, 0, stream_get_endp(s
));
189 static void zserv_encode_vrf(struct stream
*s
, struct zebra_vrf
*zvrf
)
191 struct vrf_data data
;
193 data
.l
.table_id
= zvrf
->table_id
;
194 /* Pass the tableid */
195 stream_put(s
, &data
, sizeof(struct vrf_data
));
196 /* Interface information. */
197 stream_put(s
, zvrf_name(zvrf
), VRF_NAMSIZ
);
199 /* Write packet size. */
200 stream_putw_at(s
, 0, stream_get_endp(s
));
203 /* Interface is added. Send ZEBRA_INTERFACE_ADD to client. */
205 * This function is called in the following situations:
206 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
208 * - at startup, when zebra figures out the available interfaces
209 * - when an interface is added (where support for
210 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
211 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
214 int zsend_interface_add(struct zserv
*client
, struct interface
*ifp
)
221 zserv_create_header(s
, ZEBRA_INTERFACE_ADD
, ifp
->vrf_id
);
222 zserv_encode_interface(s
, ifp
);
225 return zebra_server_send_message(client
);
228 /* Interface deletion from zebra daemon. */
229 int zsend_interface_delete(struct zserv
*client
, struct interface
*ifp
)
236 zserv_create_header(s
, ZEBRA_INTERFACE_DELETE
, ifp
->vrf_id
);
237 zserv_encode_interface(s
, ifp
);
240 return zebra_server_send_message(client
);
243 int zsend_vrf_add(struct zserv
*client
, struct zebra_vrf
*zvrf
)
250 zserv_create_header(s
, ZEBRA_VRF_ADD
, zvrf_id(zvrf
));
251 zserv_encode_vrf(s
, zvrf
);
253 client
->vrfadd_cnt
++;
254 return zebra_server_send_message(client
);
257 /* VRF deletion from zebra daemon. */
258 int zsend_vrf_delete(struct zserv
*client
, struct zebra_vrf
*zvrf
)
265 zserv_create_header(s
, ZEBRA_VRF_DELETE
, zvrf_id(zvrf
));
266 zserv_encode_vrf(s
, zvrf
);
268 client
->vrfdel_cnt
++;
269 return zebra_server_send_message(client
);
272 int zsend_interface_link_params(struct zserv
*client
, struct interface
*ifp
)
276 /* Check this client need interface information. */
280 if (!ifp
->link_params
)
285 zserv_create_header(s
, ZEBRA_INTERFACE_LINK_PARAMS
, ifp
->vrf_id
);
287 /* Add Interface Index */
288 stream_putl(s
, ifp
->ifindex
);
290 /* Then TE Link Parameters */
291 if (zebra_interface_link_params_write(s
, ifp
) == 0)
294 /* Write packet size. */
295 stream_putw_at(s
, 0, stream_get_endp(s
));
297 return zebra_server_send_message(client
);
300 /* Interface address is added/deleted. Send ZEBRA_INTERFACE_ADDRESS_ADD or
301 * ZEBRA_INTERFACE_ADDRESS_DELETE to the client.
303 * A ZEBRA_INTERFACE_ADDRESS_ADD is sent in the following situations:
304 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
305 * from the client, after the ZEBRA_INTERFACE_ADD has been
306 * sent from zebra to the client
307 * - redistribute new address info to all clients in the following situations
308 * - at startup, when zebra figures out the available interfaces
309 * - when an interface is added (where support for
310 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
311 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
313 * - for the vty commands "ip address A.B.C.D/M [<secondary>|<label LINE>]"
314 * and "no bandwidth <1-10000000>", "ipv6 address X:X::X:X/M"
315 * - when an RTM_NEWADDR message is received from the kernel,
317 * The call tree that triggers ZEBRA_INTERFACE_ADDRESS_DELETE:
319 * zsend_interface_address(DELETE)
322 * zebra_interface_address_delete_update
324 * | | if_delete_update
326 * ip_address_uninstall connected_delete_ipv4
327 * [ipv6_addresss_uninstall] [connected_delete_ipv6]
330 * | RTM_NEWADDR on routing/netlink socket
333 * "no ip address A.B.C.D/M [label LINE]"
334 * "no ip address A.B.C.D/M secondary"
335 * ["no ipv6 address X:X::X:X/M"]
338 int zsend_interface_address(int cmd
, struct zserv
*client
,
339 struct interface
*ifp
, struct connected
*ifc
)
348 zserv_create_header(s
, cmd
, ifp
->vrf_id
);
349 stream_putl(s
, ifp
->ifindex
);
351 /* Interface address flag. */
352 stream_putc(s
, ifc
->flags
);
354 /* Prefix information. */
356 stream_putc(s
, p
->family
);
357 blen
= prefix_blen(p
);
358 stream_put(s
, &p
->u
.prefix
, blen
);
361 * XXX gnu version does not send prefixlen for
362 * ZEBRA_INTERFACE_ADDRESS_DELETE
363 * but zebra_interface_address_delete_read() in the gnu version
366 stream_putc(s
, p
->prefixlen
);
369 p
= ifc
->destination
;
371 stream_put(s
, &p
->u
.prefix
, blen
);
373 stream_put(s
, NULL
, blen
);
375 /* Write packet size. */
376 stream_putw_at(s
, 0, stream_get_endp(s
));
378 client
->connected_rt_add_cnt
++;
379 return zebra_server_send_message(client
);
382 static int zsend_interface_nbr_address(int cmd
, struct zserv
*client
,
383 struct interface
*ifp
,
384 struct nbr_connected
*ifc
)
393 zserv_create_header(s
, cmd
, ifp
->vrf_id
);
394 stream_putl(s
, ifp
->ifindex
);
396 /* Prefix information. */
398 stream_putc(s
, p
->family
);
399 blen
= prefix_blen(p
);
400 stream_put(s
, &p
->u
.prefix
, blen
);
403 * XXX gnu version does not send prefixlen for
404 * ZEBRA_INTERFACE_ADDRESS_DELETE
405 * but zebra_interface_address_delete_read() in the gnu version
408 stream_putc(s
, p
->prefixlen
);
410 /* Write packet size. */
411 stream_putw_at(s
, 0, stream_get_endp(s
));
413 return zebra_server_send_message(client
);
416 /* Interface address addition. */
417 static void zebra_interface_nbr_address_add_update(struct interface
*ifp
,
418 struct nbr_connected
*ifc
)
420 struct listnode
*node
, *nnode
;
421 struct zserv
*client
;
424 if (IS_ZEBRA_DEBUG_EVENT
) {
425 char buf
[INET6_ADDRSTRLEN
];
429 "MESSAGE: ZEBRA_INTERFACE_NBR_ADDRESS_ADD %s/%d on %s",
430 inet_ntop(p
->family
, &p
->u
.prefix
, buf
,
432 p
->prefixlen
, ifc
->ifp
->name
);
435 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, node
, nnode
, client
))
436 zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_ADD
,
440 /* Interface address deletion. */
441 static void zebra_interface_nbr_address_delete_update(struct interface
*ifp
,
442 struct nbr_connected
*ifc
)
444 struct listnode
*node
, *nnode
;
445 struct zserv
*client
;
448 if (IS_ZEBRA_DEBUG_EVENT
) {
449 char buf
[INET6_ADDRSTRLEN
];
453 "MESSAGE: ZEBRA_INTERFACE_NBR_ADDRESS_DELETE %s/%d on %s",
454 inet_ntop(p
->family
, &p
->u
.prefix
, buf
,
456 p
->prefixlen
, ifc
->ifp
->name
);
459 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, node
, nnode
, client
))
460 zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
,
464 /* Send addresses on interface to client */
465 int zsend_interface_addresses(struct zserv
*client
, struct interface
*ifp
)
467 struct listnode
*cnode
, *cnnode
;
469 struct nbr_connected
*nc
;
471 /* Send interface addresses. */
472 for (ALL_LIST_ELEMENTS(ifp
->connected
, cnode
, cnnode
, c
)) {
473 if (!CHECK_FLAG(c
->conf
, ZEBRA_IFC_REAL
))
476 if (zsend_interface_address(ZEBRA_INTERFACE_ADDRESS_ADD
, client
,
482 /* Send interface neighbors. */
483 for (ALL_LIST_ELEMENTS(ifp
->nbr_connected
, cnode
, cnnode
, nc
)) {
484 if (zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_ADD
,
493 /* Notify client about interface moving from one VRF to another.
494 * Whether client is interested in old and new VRF is checked by caller.
496 int zsend_interface_vrf_update(struct zserv
*client
, struct interface
*ifp
,
504 zserv_create_header(s
, ZEBRA_INTERFACE_VRF_UPDATE
, ifp
->vrf_id
);
506 /* Fill in the ifIndex of the interface and its new VRF (id) */
507 stream_putl(s
, ifp
->ifindex
);
508 stream_putw(s
, vrf_id
);
510 /* Write packet size. */
511 stream_putw_at(s
, 0, stream_get_endp(s
));
513 client
->if_vrfchg_cnt
++;
514 return zebra_server_send_message(client
);
517 /* Add new nbr connected IPv6 address */
518 void nbr_connected_add_ipv6(struct interface
*ifp
, struct in6_addr
*address
)
520 struct nbr_connected
*ifc
;
524 IPV6_ADDR_COPY(&p
.u
.prefix
, address
);
525 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
527 if (!(ifc
= listnode_head(ifp
->nbr_connected
))) {
529 ifc
= nbr_connected_new();
530 ifc
->address
= prefix_new();
532 listnode_add(ifp
->nbr_connected
, ifc
);
535 prefix_copy(ifc
->address
, &p
);
537 zebra_interface_nbr_address_add_update(ifp
, ifc
);
539 if_nbr_ipv6ll_to_ipv4ll_neigh_update(ifp
, address
, 1);
542 void nbr_connected_delete_ipv6(struct interface
*ifp
, struct in6_addr
*address
)
544 struct nbr_connected
*ifc
;
548 IPV6_ADDR_COPY(&p
.u
.prefix
, address
);
549 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
551 ifc
= nbr_connected_check(ifp
, &p
);
555 listnode_delete(ifp
->nbr_connected
, ifc
);
557 zebra_interface_nbr_address_delete_update(ifp
, ifc
);
559 if_nbr_ipv6ll_to_ipv4ll_neigh_update(ifp
, address
, 0);
561 nbr_connected_free(ifc
);
565 * The cmd passed to zsend_interface_update may be ZEBRA_INTERFACE_UP or
566 * ZEBRA_INTERFACE_DOWN.
568 * The ZEBRA_INTERFACE_UP message is sent from the zebra server to
569 * the clients in one of 2 situations:
570 * - an if_up is detected e.g., as a result of an RTM_IFINFO message
571 * - a vty command modifying the bandwidth of an interface is received.
572 * The ZEBRA_INTERFACE_DOWN message is sent when an if_down is detected.
574 int zsend_interface_update(int cmd
, struct zserv
*client
, struct interface
*ifp
)
581 zserv_create_header(s
, cmd
, ifp
->vrf_id
);
582 zserv_encode_interface(s
, ifp
);
584 if (cmd
== ZEBRA_INTERFACE_UP
)
587 client
->ifdown_cnt
++;
589 return zebra_server_send_message(client
);
593 * This is the new function to announce and withdraw redistributed routes, used
594 * by Zebra. This is the old zsend_route_multipath() function. That function
595 * was duplicating code to send a lot of information that was essentially thrown
596 * away or ignored by the receiver. This is the leaner function that is not a
597 * duplicate of the zapi_ipv4_route_add/del.
599 * The primary difference is that this function merely sends a single NH instead
603 int zsend_redistribute_route(int add
, struct zserv
*client
, struct prefix
*p
,
604 struct prefix
*src_p
, struct route_entry
*re
)
610 struct nexthop
*nexthop
;
611 unsigned long nhnummark
= 0, messmark
= 0;
613 u_char zapi_flags
= 0;
614 struct nexthop dummy_nh
;
616 afi
= family2afi(p
->family
);
620 cmd
= ZEBRA_REDISTRIBUTE_IPV4_ADD
;
621 client
->redist_v4_add_cnt
++;
624 cmd
= ZEBRA_REDISTRIBUTE_IPV6_ADD
;
625 client
->redist_v6_add_cnt
++;
633 cmd
= ZEBRA_REDISTRIBUTE_IPV4_DEL
;
634 client
->redist_v4_del_cnt
++;
637 cmd
= ZEBRA_REDISTRIBUTE_IPV6_DEL
;
638 client
->redist_v6_del_cnt
++;
647 memset(&dummy_nh
, 0, sizeof(struct nexthop
));
649 zserv_create_header(s
, cmd
, re
->vrf_id
);
651 /* Put type and nexthop. */
652 stream_putc(s
, re
->type
);
653 stream_putw(s
, re
->instance
);
654 stream_putl(s
, re
->flags
);
656 /* marker for message flags field */
657 messmark
= stream_get_endp(s
);
661 psize
= PSIZE(p
->prefixlen
);
662 stream_putc(s
, p
->prefixlen
);
663 stream_write(s
, (u_char
*)&p
->u
.prefix
, psize
);
666 SET_FLAG(zapi_flags
, ZAPI_MESSAGE_SRCPFX
);
667 psize
= PSIZE(src_p
->prefixlen
);
668 stream_putc(s
, src_p
->prefixlen
);
669 stream_write(s
, (u_char
*)&src_p
->u
.prefix
, psize
);
672 for (nexthop
= re
->nexthop
; nexthop
; nexthop
= nexthop
->next
) {
673 /* We don't send any nexthops when there's a multipath */
674 if (re
->nexthop_active_num
> 1
675 && client
->proto
!= ZEBRA_ROUTE_LDP
) {
676 SET_FLAG(zapi_flags
, ZAPI_MESSAGE_NEXTHOP
);
677 SET_FLAG(zapi_flags
, ZAPI_MESSAGE_IFINDEX
);
680 if (p
->family
== AF_INET
) {
681 stream_put_in_addr(s
, &dummy_nh
.gate
.ipv4
);
682 } else if (p
->family
== AF_INET6
) {
683 stream_write(s
, (u_char
*)&dummy_nh
.gate
.ipv6
,
686 /* We don't handle anything else now, abort */
688 "%s: Unable to redistribute route of unknown family, %d\n",
689 __func__
, p
->family
);
693 stream_putl(s
, 0); /* dummy ifindex */
697 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
)) {
698 SET_FLAG(zapi_flags
, ZAPI_MESSAGE_NEXTHOP
);
699 SET_FLAG(zapi_flags
, ZAPI_MESSAGE_IFINDEX
);
700 if (nhnummark
== 0) {
701 nhnummark
= stream_get_endp(s
);
702 stream_putc(s
, 1); /* placeholder */
706 switch (nexthop
->type
) {
707 case NEXTHOP_TYPE_IPV4
:
708 case NEXTHOP_TYPE_IPV4_IFINDEX
:
709 stream_put_in_addr(s
, &nexthop
->gate
.ipv4
);
711 case NEXTHOP_TYPE_IPV6
:
712 case NEXTHOP_TYPE_IPV6_IFINDEX
:
713 /* Only BGP supports IPv4 prefix with IPv6 NH,
715 if (p
->family
== AF_INET
)
716 stream_put_in_addr(s
,
717 &dummy_nh
.gate
.ipv4
);
721 (u_char
*)&nexthop
->gate
.ipv6
,
725 if (cmd
== ZEBRA_REDISTRIBUTE_IPV4_ADD
726 || cmd
== ZEBRA_REDISTRIBUTE_IPV4_DEL
) {
727 struct in_addr empty
;
729 sizeof(struct in_addr
));
730 stream_write(s
, (u_char
*)&empty
,
733 struct in6_addr empty
;
735 sizeof(struct in6_addr
));
736 stream_write(s
, (u_char
*)&empty
,
741 /* Interface index. */
743 stream_putl(s
, nexthop
->ifindex
);
745 /* ldpd needs all nexthops */
746 if (client
->proto
!= ZEBRA_ROUTE_LDP
)
752 SET_FLAG(zapi_flags
, ZAPI_MESSAGE_DISTANCE
);
753 stream_putc(s
, re
->distance
);
756 SET_FLAG(zapi_flags
, ZAPI_MESSAGE_METRIC
);
757 stream_putl(s
, re
->metric
);
761 SET_FLAG(zapi_flags
, ZAPI_MESSAGE_TAG
);
762 stream_putl(s
, re
->tag
);
766 SET_FLAG(zapi_flags
, ZAPI_MESSAGE_MTU
);
767 stream_putl(s
, re
->mtu
);
769 /* write real message flags value */
770 stream_putc_at(s
, messmark
, zapi_flags
);
772 /* Write next-hop number */
774 stream_putc_at(s
, nhnummark
, nhnum
);
776 /* Write packet size. */
777 stream_putw_at(s
, 0, stream_get_endp(s
));
779 return zebra_server_send_message(client
);
782 static int zsend_write_nexthop(struct stream
*s
, struct nexthop
*nexthop
)
784 stream_putc(s
, nexthop
->type
);
785 switch (nexthop
->type
) {
786 case NEXTHOP_TYPE_IPV4
:
787 case NEXTHOP_TYPE_IPV4_IFINDEX
:
788 stream_put_in_addr(s
, &nexthop
->gate
.ipv4
);
789 stream_putl(s
, nexthop
->ifindex
);
791 case NEXTHOP_TYPE_IPV6
:
792 stream_put(s
, &nexthop
->gate
.ipv6
, 16);
794 case NEXTHOP_TYPE_IPV6_IFINDEX
:
795 stream_put(s
, &nexthop
->gate
.ipv6
, 16);
796 stream_putl(s
, nexthop
->ifindex
);
798 case NEXTHOP_TYPE_IFINDEX
:
799 stream_putl(s
, nexthop
->ifindex
);
808 /* Nexthop register */
809 static int zserv_rnh_register(struct zserv
*client
, int sock
, u_short length
,
810 rnh_type_t type
, struct zebra_vrf
*zvrf
)
818 if (IS_ZEBRA_DEBUG_NHT
)
820 "rnh_register msg from client %s: length=%d, type=%s\n",
821 zebra_route_string(client
->proto
), length
,
822 (type
== RNH_NEXTHOP_TYPE
) ? "nexthop" : "route");
826 client
->nh_reg_time
= monotime(NULL
);
829 flags
= stream_getc(s
);
830 p
.family
= stream_getw(s
);
831 p
.prefixlen
= stream_getc(s
);
833 if (p
.family
== AF_INET
) {
834 p
.u
.prefix4
.s_addr
= stream_get_ipv4(s
);
835 l
+= IPV4_MAX_BYTELEN
;
836 } else if (p
.family
== AF_INET6
) {
837 stream_get(&p
.u
.prefix6
, s
, IPV6_MAX_BYTELEN
);
838 l
+= IPV6_MAX_BYTELEN
;
841 "rnh_register: Received unknown family type %d\n",
845 rnh
= zebra_add_rnh(&p
, zvrf_id(zvrf
), type
);
846 if (type
== RNH_NEXTHOP_TYPE
) {
848 && !CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
))
849 SET_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
);
851 && CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
))
852 UNSET_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
);
853 } else if (type
== RNH_IMPORT_CHECK_TYPE
) {
855 && !CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
))
856 SET_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
);
857 else if (!flags
&& CHECK_FLAG(rnh
->flags
,
858 ZEBRA_NHT_EXACT_MATCH
))
859 UNSET_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
);
862 zebra_add_rnh_client(rnh
, client
, type
, zvrf_id(zvrf
));
863 /* Anything not AF_INET/INET6 has been filtered out above */
864 zebra_evaluate_rnh(zvrf_id(zvrf
), p
.family
, 1, type
, &p
);
869 /* Nexthop register */
870 static int zserv_rnh_unregister(struct zserv
*client
, int sock
, u_short length
,
871 rnh_type_t type
, struct zebra_vrf
*zvrf
)
878 if (IS_ZEBRA_DEBUG_NHT
)
879 zlog_debug("rnh_unregister msg from client %s: length=%d\n",
880 zebra_route_string(client
->proto
), length
);
886 s
); // Connected or not. Not used in this function
887 p
.family
= stream_getw(s
);
888 p
.prefixlen
= stream_getc(s
);
890 if (p
.family
== AF_INET
) {
891 p
.u
.prefix4
.s_addr
= stream_get_ipv4(s
);
892 l
+= IPV4_MAX_BYTELEN
;
893 } else if (p
.family
== AF_INET6
) {
894 stream_get(&p
.u
.prefix6
, s
, IPV6_MAX_BYTELEN
);
895 l
+= IPV6_MAX_BYTELEN
;
898 "rnh_register: Received unknown family type %d\n",
902 rnh
= zebra_lookup_rnh(&p
, zvrf_id(zvrf
), type
);
904 client
->nh_dereg_time
= monotime(NULL
);
905 zebra_remove_rnh_client(rnh
, client
, type
);
911 #define ZEBRA_MIN_FEC_LENGTH 5
914 static int zserv_fec_register(struct zserv
*client
, int sock
, u_short length
)
917 struct zebra_vrf
*zvrf
;
921 u_int32_t label_index
= MPLS_INVALID_LABEL_INDEX
;
924 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
926 return 0; // unexpected
929 * The minimum amount of data that can be sent for one fec
932 if (length
< ZEBRA_MIN_FEC_LENGTH
) {
934 "fec_register: Received a fec register of length %d, it is of insufficient size to properly decode",
940 flags
= stream_getw(s
);
941 p
.family
= stream_getw(s
);
942 if (p
.family
!= AF_INET
&& p
.family
!= AF_INET6
) {
944 "fec_register: Received unknown family type %d\n",
948 p
.prefixlen
= stream_getc(s
);
950 stream_get(&p
.u
.prefix
, s
, PSIZE(p
.prefixlen
));
951 l
+= PSIZE(p
.prefixlen
);
952 if (flags
& ZEBRA_FEC_REGISTER_LABEL_INDEX
) {
953 label_index
= stream_getl(s
);
956 label_index
= MPLS_INVALID_LABEL_INDEX
;
957 zebra_mpls_fec_register(zvrf
, &p
, label_index
, client
);
964 static int zserv_fec_unregister(struct zserv
*client
, int sock
, u_short length
)
967 struct zebra_vrf
*zvrf
;
973 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
975 return 0; // unexpected
978 * The minimum amount of data that can be sent for one
981 if (length
< ZEBRA_MIN_FEC_LENGTH
) {
983 "fec_unregister: Received a fec unregister of length %d, it is of insufficient size to properly decode",
989 // flags = stream_getw(s);
990 (void)stream_getw(s
);
991 p
.family
= stream_getw(s
);
992 if (p
.family
!= AF_INET
&& p
.family
!= AF_INET6
) {
994 "fec_unregister: Received unknown family type %d\n",
998 p
.prefixlen
= stream_getc(s
);
1000 stream_get(&p
.u
.prefix
, s
, PSIZE(p
.prefixlen
));
1001 l
+= PSIZE(p
.prefixlen
);
1002 zebra_mpls_fec_unregister(zvrf
, &p
, client
);
1009 Modified version of zsend_ipv4_nexthop_lookup():
1010 Query unicast rib if nexthop is not found on mrib.
1011 Returns both route metric and protocol distance.
1013 static int zsend_ipv4_nexthop_lookup_mrib(struct zserv
*client
,
1014 struct in_addr addr
,
1015 struct route_entry
*re
,
1016 struct zebra_vrf
*zvrf
)
1021 struct nexthop
*nexthop
;
1023 /* Get output stream. */
1027 /* Fill in result. */
1028 zserv_create_header(s
, ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB
, zvrf_id(zvrf
));
1029 stream_put_in_addr(s
, &addr
);
1032 stream_putc(s
, re
->distance
);
1033 stream_putl(s
, re
->metric
);
1035 nump
= stream_get_endp(
1036 s
); /* remember position for nexthop_num */
1037 stream_putc(s
, 0); /* reserve room for nexthop_num */
1038 /* Only non-recursive routes are elegible to resolve the nexthop
1040 * are looking up. Therefore, we will just iterate over the top
1041 * chain of nexthops. */
1042 for (nexthop
= re
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
1043 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
1044 num
+= zsend_write_nexthop(s
, nexthop
);
1046 stream_putc_at(s
, nump
, num
); /* store nexthop_num */
1048 stream_putc(s
, 0); /* distance */
1049 stream_putl(s
, 0); /* metric */
1050 stream_putc(s
, 0); /* nexthop_num */
1053 stream_putw_at(s
, 0, stream_get_endp(s
));
1055 return zebra_server_send_message(client
);
1058 /* Router-id is updated. Send ZEBRA_ROUTER_ID_ADD to client. */
1059 int zsend_router_id_update(struct zserv
*client
, struct prefix
*p
,
1065 /* Check this client need interface information. */
1066 if (!vrf_bitmap_check(client
->ridinfo
, vrf_id
))
1073 zserv_create_header(s
, ZEBRA_ROUTER_ID_UPDATE
, vrf_id
);
1075 /* Prefix information. */
1076 stream_putc(s
, p
->family
);
1077 blen
= prefix_blen(p
);
1078 stream_put(s
, &p
->u
.prefix
, blen
);
1079 stream_putc(s
, p
->prefixlen
);
1081 /* Write packet size. */
1082 stream_putw_at(s
, 0, stream_get_endp(s
));
1084 return zebra_server_send_message(client
);
1087 /* Register zebra server interface information. Send current all
1088 interface and address information. */
1089 static int zread_interface_add(struct zserv
*client
, u_short length
,
1090 struct zebra_vrf
*zvrf
)
1093 struct listnode
*ifnode
, *ifnnode
;
1094 struct interface
*ifp
;
1096 /* Interface information is needed. */
1097 vrf_bitmap_set(client
->ifinfo
, zvrf_id(zvrf
));
1099 RB_FOREACH(vrf
, vrf_id_head
, &vrfs_by_id
)
1101 for (ALL_LIST_ELEMENTS(vrf
->iflist
, ifnode
, ifnnode
, ifp
)) {
1102 /* Skip pseudo interface. */
1103 if (!CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_ACTIVE
))
1106 if (zsend_interface_add(client
, ifp
) < 0)
1109 if (zsend_interface_addresses(client
, ifp
) < 0)
1116 /* Unregister zebra server interface information. */
1117 static int zread_interface_delete(struct zserv
*client
, u_short length
,
1118 struct zebra_vrf
*zvrf
)
1120 vrf_bitmap_unset(client
->ifinfo
, zvrf_id(zvrf
));
1124 void zserv_nexthop_num_warn(const char *caller
, const struct prefix
*p
,
1125 const unsigned int nexthop_num
)
1127 if (nexthop_num
> multipath_num
) {
1128 char buff
[PREFIX2STR_BUFFER
];
1129 prefix2str(p
, buff
, sizeof(buff
));
1131 "%s: Prefix %s has %d nexthops, but we can only use the first %d",
1132 caller
, buff
, nexthop_num
, multipath_num
);
1136 /* This function support multiple nexthop. */
1138 * Parse the ZEBRA_IPV4_ROUTE_ADD sent from client. Update re and
1141 static int zread_ipv4_add(struct zserv
*client
, u_short length
,
1142 struct zebra_vrf
*zvrf
)
1145 struct route_entry
*re
;
1148 struct in_addr nhop_addr
;
1150 u_char nexthop_type
;
1156 struct nexthop
*nexthop
;
1158 /* Get input stream. */
1161 /* Allocate new re. */
1162 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
1164 /* Type, flags, message. */
1165 re
->type
= stream_getc(s
);
1166 re
->instance
= stream_getw(s
);
1167 re
->flags
= stream_getl(s
);
1168 message
= stream_getc(s
);
1169 safi
= stream_getw(s
);
1170 re
->uptime
= time(NULL
);
1173 memset(&p
, 0, sizeof(struct prefix_ipv4
));
1175 p
.prefixlen
= stream_getc(s
);
1176 stream_get(&p
.u
.prefix4
, s
, PSIZE(p
.prefixlen
));
1179 re
->vrf_id
= zvrf_id(zvrf
);
1181 /* Nexthop parse. */
1182 if (CHECK_FLAG(message
, ZAPI_MESSAGE_NEXTHOP
)) {
1183 nexthop_num
= stream_getc(s
);
1184 zserv_nexthop_num_warn(__func__
, (const struct prefix
*)&p
,
1187 for (i
= 0; i
< nexthop_num
; i
++) {
1188 nexthop_type
= stream_getc(s
);
1190 switch (nexthop_type
) {
1191 case NEXTHOP_TYPE_IFINDEX
:
1192 ifindex
= stream_getl(s
);
1193 route_entry_nexthop_ifindex_add(re
, ifindex
);
1195 case NEXTHOP_TYPE_IPV4
:
1196 nhop_addr
.s_addr
= stream_get_ipv4(s
);
1197 nexthop
= route_entry_nexthop_ipv4_add(
1198 re
, &nhop_addr
, NULL
);
1199 /* For labeled-unicast, each nexthop is followed
1201 if (CHECK_FLAG(message
, ZAPI_MESSAGE_LABEL
)) {
1202 label
= (mpls_label_t
)stream_getl(s
);
1204 nexthop
, nexthop
->nh_label_type
,
1208 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1209 nhop_addr
.s_addr
= stream_get_ipv4(s
);
1210 ifindex
= stream_getl(s
);
1211 route_entry_nexthop_ipv4_ifindex_add(
1212 re
, &nhop_addr
, NULL
, ifindex
);
1214 case NEXTHOP_TYPE_IPV6
:
1215 stream_forward_getp(s
, IPV6_MAX_BYTELEN
);
1217 case NEXTHOP_TYPE_BLACKHOLE
:
1218 route_entry_nexthop_blackhole_add(re
);
1225 if (CHECK_FLAG(message
, ZAPI_MESSAGE_DISTANCE
))
1226 re
->distance
= stream_getc(s
);
1229 if (CHECK_FLAG(message
, ZAPI_MESSAGE_METRIC
))
1230 re
->metric
= stream_getl(s
);
1233 if (CHECK_FLAG(message
, ZAPI_MESSAGE_TAG
))
1234 re
->tag
= stream_getl(s
);
1238 if (CHECK_FLAG(message
, ZAPI_MESSAGE_MTU
))
1239 re
->mtu
= stream_getl(s
);
1244 re
->table
= zvrf
->table_id
;
1246 ret
= rib_add_multipath(AFI_IP
, safi
, &p
, NULL
, re
);
1250 client
->v4_route_add_cnt
++;
1252 client
->v4_route_upd8_cnt
++;
1256 /* Zebra server IPv4 prefix delete function. */
1257 static int zread_ipv4_delete(struct zserv
*client
, u_short length
,
1258 struct zebra_vrf
*zvrf
)
1262 struct zapi_ipv4 api
;
1263 struct in_addr nexthop
;
1264 union g_addr
*nexthop_p
;
1265 unsigned long ifindex
;
1268 u_char nexthop_type
;
1276 /* Type, flags, message. */
1277 api
.type
= stream_getc(s
);
1278 api
.instance
= stream_getw(s
);
1279 api
.flags
= stream_getl(s
);
1280 api
.message
= stream_getc(s
);
1281 api
.safi
= stream_getw(s
);
1284 memset(&p
, 0, sizeof(struct prefix
));
1286 p
.prefixlen
= stream_getc(s
);
1287 stream_get(&p
.u
.prefix4
, s
, PSIZE(p
.prefixlen
));
1289 /* Nexthop, ifindex, distance, metric. */
1290 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
)) {
1291 nexthop_num
= stream_getc(s
);
1293 for (i
= 0; i
< nexthop_num
; i
++) {
1294 nexthop_type
= stream_getc(s
);
1296 switch (nexthop_type
) {
1297 case NEXTHOP_TYPE_IFINDEX
:
1298 ifindex
= stream_getl(s
);
1300 case NEXTHOP_TYPE_IPV4
:
1301 nexthop
.s_addr
= stream_get_ipv4(s
);
1302 /* For labeled-unicast, each nexthop is followed
1304 * we don't care for delete.
1306 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_LABEL
))
1307 stream_forward_getp(s
,
1309 nexthop_p
= (union g_addr
*)&nexthop
;
1311 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1312 nexthop
.s_addr
= stream_get_ipv4(s
);
1313 nexthop_p
= (union g_addr
*)&nexthop
;
1314 ifindex
= stream_getl(s
);
1316 case NEXTHOP_TYPE_IPV6
:
1317 stream_forward_getp(s
, IPV6_MAX_BYTELEN
);
1324 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
))
1325 api
.distance
= stream_getc(s
);
1330 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
))
1331 api
.metric
= stream_getl(s
);
1336 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_TAG
))
1337 api
.tag
= stream_getl(s
);
1341 table_id
= zvrf
->table_id
;
1343 rib_delete(AFI_IP
, api
.safi
, zvrf_id(zvrf
), api
.type
, api
.instance
,
1344 api
.flags
, &p
, NULL
, nexthop_p
, ifindex
, table_id
);
1345 client
->v4_route_del_cnt
++;
1349 /* MRIB Nexthop lookup for IPv4. */
1350 static int zread_ipv4_nexthop_lookup_mrib(struct zserv
*client
, u_short length
,
1351 struct zebra_vrf
*zvrf
)
1353 struct in_addr addr
;
1354 struct route_entry
*re
;
1356 addr
.s_addr
= stream_get_ipv4(client
->ibuf
);
1357 re
= rib_match_ipv4_multicast(zvrf_id(zvrf
), addr
, NULL
);
1358 return zsend_ipv4_nexthop_lookup_mrib(client
, addr
, re
, zvrf
);
1361 /* Zebra server IPv6 prefix add function. */
1362 static int zread_ipv4_route_ipv6_nexthop_add(struct zserv
*client
,
1364 struct zebra_vrf
*zvrf
)
1368 struct in6_addr nhop_addr
;
1369 struct route_entry
*re
;
1372 u_char nexthop_type
;
1375 static struct in6_addr nexthops
[MULTIPATH_NUM
];
1376 static unsigned int ifindices
[MULTIPATH_NUM
];
1378 static mpls_label_t labels
[MULTIPATH_NUM
];
1380 struct nexthop
*nexthop
;
1382 /* Get input stream. */
1385 memset(&nhop_addr
, 0, sizeof(struct in6_addr
));
1387 /* Allocate new re. */
1388 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
1390 /* Type, flags, message. */
1391 re
->type
= stream_getc(s
);
1392 re
->instance
= stream_getw(s
);
1393 re
->flags
= stream_getl(s
);
1394 message
= stream_getc(s
);
1395 safi
= stream_getw(s
);
1396 re
->uptime
= time(NULL
);
1399 memset(&p
, 0, sizeof(struct prefix_ipv4
));
1401 p
.prefixlen
= stream_getc(s
);
1402 stream_get(&p
.u
.prefix4
, s
, PSIZE(p
.prefixlen
));
1405 re
->vrf_id
= zvrf_id(zvrf
);
1407 /* We need to give nh-addr, nh-ifindex with the same next-hop object
1408 * to the re to ensure that IPv6 multipathing works; need to coalesce
1409 * these. Clients should send the same number of paired set of
1410 * next-hop-addr/next-hop-ifindices. */
1411 if (CHECK_FLAG(message
, ZAPI_MESSAGE_NEXTHOP
)) {
1412 unsigned int nh_count
= 0;
1413 unsigned int if_count
= 0;
1414 unsigned int max_nh_if
= 0;
1416 nexthop_num
= stream_getc(s
);
1417 zserv_nexthop_num_warn(__func__
, (const struct prefix
*)&p
,
1419 for (i
= 0; i
< nexthop_num
; i
++) {
1420 nexthop_type
= stream_getc(s
);
1422 switch (nexthop_type
) {
1423 case NEXTHOP_TYPE_IPV6
:
1424 stream_get(&nhop_addr
, s
, 16);
1425 if (nh_count
< MULTIPATH_NUM
) {
1426 /* For labeled-unicast, each nexthop is
1427 * followed by label. */
1428 if (CHECK_FLAG(message
,
1429 ZAPI_MESSAGE_LABEL
)) {
1430 label
= (mpls_label_t
)
1432 labels
[nh_count
] = label
;
1434 nexthops
[nh_count
] = nhop_addr
;
1438 case NEXTHOP_TYPE_IFINDEX
:
1439 if (if_count
< multipath_num
) {
1440 ifindices
[if_count
++] = stream_getl(s
);
1443 case NEXTHOP_TYPE_BLACKHOLE
:
1444 route_entry_nexthop_blackhole_add(re
);
1449 max_nh_if
= (nh_count
> if_count
) ? nh_count
: if_count
;
1450 for (i
= 0; i
< max_nh_if
; i
++) {
1452 && !IN6_IS_ADDR_UNSPECIFIED(&nexthops
[i
])) {
1453 if ((i
< if_count
) && ifindices
[i
])
1455 route_entry_nexthop_ipv6_ifindex_add(
1459 nexthop
= route_entry_nexthop_ipv6_add(
1462 if (CHECK_FLAG(message
, ZAPI_MESSAGE_LABEL
))
1464 nexthop
, nexthop
->nh_label_type
,
1467 if ((i
< if_count
) && ifindices
[i
])
1468 route_entry_nexthop_ifindex_add(
1475 if (CHECK_FLAG(message
, ZAPI_MESSAGE_DISTANCE
))
1476 re
->distance
= stream_getc(s
);
1479 if (CHECK_FLAG(message
, ZAPI_MESSAGE_METRIC
))
1480 re
->metric
= stream_getl(s
);
1483 if (CHECK_FLAG(message
, ZAPI_MESSAGE_TAG
))
1484 re
->tag
= stream_getl(s
);
1488 if (CHECK_FLAG(message
, ZAPI_MESSAGE_MTU
))
1489 re
->mtu
= stream_getl(s
);
1494 re
->table
= zvrf
->table_id
;
1496 ret
= rib_add_multipath(AFI_IP6
, safi
, &p
, NULL
, re
);
1499 client
->v4_route_add_cnt
++;
1501 client
->v4_route_upd8_cnt
++;
1506 static int zread_ipv6_add(struct zserv
*client
, u_short length
,
1507 struct zebra_vrf
*zvrf
)
1511 struct in6_addr nhop_addr
;
1512 struct route_entry
*re
;
1515 u_char nexthop_type
;
1517 struct prefix_ipv6 src_p
, *src_pp
;
1519 static struct in6_addr nexthops
[MULTIPATH_NUM
];
1520 static unsigned int ifindices
[MULTIPATH_NUM
];
1522 static mpls_label_t labels
[MULTIPATH_NUM
];
1524 struct nexthop
*nexthop
;
1526 /* Get input stream. */
1529 memset(&nhop_addr
, 0, sizeof(struct in6_addr
));
1531 /* Allocate new re. */
1532 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
1534 /* Type, flags, message. */
1535 re
->type
= stream_getc(s
);
1536 re
->instance
= stream_getw(s
);
1537 re
->flags
= stream_getl(s
);
1538 message
= stream_getc(s
);
1539 safi
= stream_getw(s
);
1540 re
->uptime
= time(NULL
);
1543 memset(&p
, 0, sizeof(struct prefix_ipv6
));
1544 p
.family
= AF_INET6
;
1545 p
.prefixlen
= stream_getc(s
);
1546 stream_get(&p
.u
.prefix6
, s
, PSIZE(p
.prefixlen
));
1548 if (CHECK_FLAG(message
, ZAPI_MESSAGE_SRCPFX
)) {
1549 memset(&src_p
, 0, sizeof(struct prefix_ipv6
));
1550 src_p
.family
= AF_INET6
;
1551 src_p
.prefixlen
= stream_getc(s
);
1552 stream_get(&src_p
.prefix
, s
, PSIZE(src_p
.prefixlen
));
1557 /* We need to give nh-addr, nh-ifindex with the same next-hop object
1558 * to the re to ensure that IPv6 multipathing works; need to coalesce
1559 * these. Clients should send the same number of paired set of
1560 * next-hop-addr/next-hop-ifindices. */
1561 if (CHECK_FLAG(message
, ZAPI_MESSAGE_NEXTHOP
)) {
1562 unsigned int nh_count
= 0;
1563 unsigned int if_count
= 0;
1564 unsigned int max_nh_if
= 0;
1566 nexthop_num
= stream_getc(s
);
1567 zserv_nexthop_num_warn(__func__
, (const struct prefix
*)&p
,
1569 for (i
= 0; i
< nexthop_num
; i
++) {
1570 nexthop_type
= stream_getc(s
);
1572 switch (nexthop_type
) {
1573 case NEXTHOP_TYPE_IPV6
:
1574 stream_get(&nhop_addr
, s
, 16);
1575 if (nh_count
< MULTIPATH_NUM
) {
1576 /* For labeled-unicast, each nexthop is
1577 * followed by label. */
1578 if (CHECK_FLAG(message
,
1579 ZAPI_MESSAGE_LABEL
)) {
1580 label
= (mpls_label_t
)
1582 labels
[nh_count
] = label
;
1584 nexthops
[nh_count
++] = nhop_addr
;
1587 case NEXTHOP_TYPE_IFINDEX
:
1588 if (if_count
< multipath_num
) {
1589 ifindices
[if_count
++] = stream_getl(s
);
1592 case NEXTHOP_TYPE_BLACKHOLE
:
1593 route_entry_nexthop_blackhole_add(re
);
1598 max_nh_if
= (nh_count
> if_count
) ? nh_count
: if_count
;
1599 for (i
= 0; i
< max_nh_if
; i
++) {
1601 && !IN6_IS_ADDR_UNSPECIFIED(&nexthops
[i
])) {
1602 if ((i
< if_count
) && ifindices
[i
])
1604 route_entry_nexthop_ipv6_ifindex_add(
1608 nexthop
= route_entry_nexthop_ipv6_add(
1610 if (CHECK_FLAG(message
, ZAPI_MESSAGE_LABEL
))
1612 nexthop
, nexthop
->nh_label_type
,
1615 if ((i
< if_count
) && ifindices
[i
])
1616 route_entry_nexthop_ifindex_add(
1623 if (CHECK_FLAG(message
, ZAPI_MESSAGE_DISTANCE
))
1624 re
->distance
= stream_getc(s
);
1627 if (CHECK_FLAG(message
, ZAPI_MESSAGE_METRIC
))
1628 re
->metric
= stream_getl(s
);
1631 if (CHECK_FLAG(message
, ZAPI_MESSAGE_TAG
))
1632 re
->tag
= stream_getl(s
);
1636 if (CHECK_FLAG(message
, ZAPI_MESSAGE_MTU
))
1637 re
->mtu
= stream_getl(s
);
1642 re
->vrf_id
= zvrf_id(zvrf
);
1643 re
->table
= zvrf
->table_id
;
1645 ret
= rib_add_multipath(AFI_IP6
, safi
, &p
, src_pp
, re
);
1648 client
->v6_route_add_cnt
++;
1650 client
->v6_route_upd8_cnt
++;
1655 /* Zebra server IPv6 prefix delete function. */
1656 static int zread_ipv6_delete(struct zserv
*client
, u_short length
,
1657 struct zebra_vrf
*zvrf
)
1661 struct zapi_ipv6 api
;
1662 struct in6_addr nexthop
;
1663 union g_addr
*pnexthop
= NULL
;
1664 unsigned long ifindex
;
1666 struct prefix_ipv6 src_p
, *src_pp
;
1670 memset(&nexthop
, 0, sizeof(struct in6_addr
));
1672 /* Type, flags, message. */
1673 api
.type
= stream_getc(s
);
1674 api
.instance
= stream_getw(s
);
1675 api
.flags
= stream_getl(s
);
1676 api
.message
= stream_getc(s
);
1677 api
.safi
= stream_getw(s
);
1680 memset(&p
, 0, sizeof(struct prefix_ipv6
));
1681 p
.family
= AF_INET6
;
1682 p
.prefixlen
= stream_getc(s
);
1683 stream_get(&p
.u
.prefix6
, s
, PSIZE(p
.prefixlen
));
1685 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
)) {
1686 memset(&src_p
, 0, sizeof(struct prefix_ipv6
));
1687 src_p
.family
= AF_INET6
;
1688 src_p
.prefixlen
= stream_getc(s
);
1689 stream_get(&src_p
.prefix
, s
, PSIZE(src_p
.prefixlen
));
1694 /* Nexthop, ifindex, distance, metric. */
1695 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
)) {
1696 u_char nexthop_type
;
1698 api
.nexthop_num
= stream_getc(s
);
1699 for (i
= 0; i
< api
.nexthop_num
; i
++) {
1700 nexthop_type
= stream_getc(s
);
1702 switch (nexthop_type
) {
1703 case NEXTHOP_TYPE_IPV6
:
1704 stream_get(&nexthop
, s
, 16);
1705 /* For labeled-unicast, each nexthop is followed
1707 * we don't care for delete.
1709 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_LABEL
))
1710 stream_forward_getp(s
,
1712 pnexthop
= (union g_addr
*)&nexthop
;
1714 case NEXTHOP_TYPE_IFINDEX
:
1715 ifindex
= stream_getl(s
);
1722 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
))
1723 api
.distance
= stream_getc(s
);
1728 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
))
1729 api
.metric
= stream_getl(s
);
1734 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_TAG
))
1735 api
.tag
= stream_getl(s
);
1739 if (IN6_IS_ADDR_UNSPECIFIED(&nexthop
))
1740 rib_delete(AFI_IP6
, api
.safi
, zvrf_id(zvrf
), api
.type
,
1741 api
.instance
, api
.flags
, &p
, src_pp
, NULL
, ifindex
,
1744 rib_delete(AFI_IP6
, api
.safi
, zvrf_id(zvrf
), api
.type
,
1745 api
.instance
, api
.flags
, &p
, src_pp
, pnexthop
,
1746 ifindex
, client
->rtm_table
);
1748 client
->v6_route_del_cnt
++;
1752 /* Register zebra server router-id information. Send current router-id */
1753 static int zread_router_id_add(struct zserv
*client
, u_short length
,
1754 struct zebra_vrf
*zvrf
)
1758 /* Router-id information is needed. */
1759 vrf_bitmap_set(client
->ridinfo
, zvrf_id(zvrf
));
1761 router_id_get(&p
, zvrf_id(zvrf
));
1763 return zsend_router_id_update(client
, &p
, zvrf_id(zvrf
));
1766 /* Unregister zebra server router-id information. */
1767 static int zread_router_id_delete(struct zserv
*client
, u_short length
,
1768 struct zebra_vrf
*zvrf
)
1770 vrf_bitmap_unset(client
->ridinfo
, zvrf_id(zvrf
));
1774 /* Tie up route-type and client->sock */
1775 static void zread_hello(struct zserv
*client
)
1777 /* type of protocol (lib/zebra.h) */
1781 proto
= stream_getc(client
->ibuf
);
1782 instance
= stream_getw(client
->ibuf
);
1784 /* accept only dynamic routing protocols */
1785 if ((proto
< ZEBRA_ROUTE_MAX
) && (proto
> ZEBRA_ROUTE_STATIC
)) {
1787 "client %d says hello and bids fair to announce only %s routes",
1788 client
->sock
, zebra_route_string(proto
));
1790 zlog_notice("client protocol instance %d", instance
);
1792 client
->proto
= proto
;
1793 client
->instance
= instance
;
1797 /* Unregister all information in a VRF. */
1798 static int zread_vrf_unregister(struct zserv
*client
, u_short length
,
1799 struct zebra_vrf
*zvrf
)
1804 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
1805 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
1806 vrf_bitmap_unset(client
->redist
[afi
][i
], zvrf_id(zvrf
));
1807 vrf_bitmap_unset(client
->redist_default
, zvrf_id(zvrf
));
1808 vrf_bitmap_unset(client
->ifinfo
, zvrf_id(zvrf
));
1809 vrf_bitmap_unset(client
->ridinfo
, zvrf_id(zvrf
));
1814 static void zread_mpls_labels(int command
, struct zserv
*client
, u_short length
,
1818 enum lsp_types_t type
;
1819 struct prefix prefix
;
1820 enum nexthop_types_t gtype
;
1823 mpls_label_t in_label
, out_label
;
1825 struct zebra_vrf
*zvrf
;
1827 zvrf
= vrf_info_lookup(vrf_id
);
1831 /* Get input stream. */
1835 type
= stream_getc(s
);
1836 prefix
.family
= stream_getl(s
);
1837 switch (prefix
.family
) {
1839 prefix
.u
.prefix4
.s_addr
= stream_get_ipv4(s
);
1840 prefix
.prefixlen
= stream_getc(s
);
1841 gate
.ipv4
.s_addr
= stream_get_ipv4(s
);
1844 stream_get(&prefix
.u
.prefix6
, s
, 16);
1845 prefix
.prefixlen
= stream_getc(s
);
1846 stream_get(&gate
.ipv6
, s
, 16);
1851 ifindex
= stream_getl(s
);
1852 distance
= stream_getc(s
);
1853 in_label
= stream_getl(s
);
1854 out_label
= stream_getl(s
);
1856 switch (prefix
.family
) {
1859 gtype
= NEXTHOP_TYPE_IPV4_IFINDEX
;
1861 gtype
= NEXTHOP_TYPE_IPV4
;
1865 gtype
= NEXTHOP_TYPE_IPV6_IFINDEX
;
1867 gtype
= NEXTHOP_TYPE_IPV6
;
1876 if (command
== ZEBRA_MPLS_LABELS_ADD
) {
1877 mpls_lsp_install(zvrf
, type
, in_label
, out_label
, gtype
, &gate
,
1879 if (out_label
!= MPLS_IMP_NULL_LABEL
)
1880 mpls_ftn_update(1, zvrf
, type
, &prefix
, gtype
, &gate
,
1881 ifindex
, distance
, out_label
);
1882 } else if (command
== ZEBRA_MPLS_LABELS_DELETE
) {
1883 mpls_lsp_uninstall(zvrf
, type
, in_label
, gtype
, &gate
, ifindex
);
1884 if (out_label
!= MPLS_IMP_NULL_LABEL
)
1885 mpls_ftn_update(0, zvrf
, type
, &prefix
, gtype
, &gate
,
1886 ifindex
, distance
, out_label
);
1889 /* Send response to a label manager connect request to client */
1890 static int zsend_label_manager_connect_response(struct zserv
*client
,
1891 vrf_id_t vrf_id
, u_short result
)
1898 zserv_create_header(s
, ZEBRA_LABEL_MANAGER_CONNECT
, vrf_id
);
1901 stream_putc(s
, result
);
1903 /* Write packet size. */
1904 stream_putw_at(s
, 0, stream_get_endp(s
));
1906 return writen(client
->sock
, s
->data
, stream_get_endp(s
));
1909 static void zread_label_manager_connect(struct zserv
*client
, vrf_id_t vrf_id
)
1912 /* type of protocol (lib/zebra.h) */
1916 /* Get input stream. */
1920 proto
= stream_getc(s
);
1921 instance
= stream_getw(s
);
1923 /* accept only dynamic routing protocols */
1924 if ((proto
>= ZEBRA_ROUTE_MAX
) || (proto
<= ZEBRA_ROUTE_STATIC
)) {
1925 zlog_err("client %d has wrong protocol %s", client
->sock
,
1926 zebra_route_string(proto
));
1927 zsend_label_manager_connect_response(client
, vrf_id
, 1);
1930 zlog_notice("client %d with instance %u connected as %s", client
->sock
,
1931 instance
, zebra_route_string(proto
));
1932 client
->proto
= proto
;
1933 client
->instance
= instance
;
1936 Release previous labels of same protocol and instance.
1937 This is done in case it restarted from an unexpected shutdown.
1939 release_daemon_chunks(proto
, instance
);
1942 " Label Manager client connected: sock %d, proto %s, instance %u",
1943 client
->sock
, zebra_route_string(proto
), instance
);
1944 /* send response back */
1945 zsend_label_manager_connect_response(client
, vrf_id
, 0);
1947 /* Send response to a get label chunk request to client */
1948 static int zsend_assign_label_chunk_response(struct zserv
*client
,
1950 struct label_manager_chunk
*lmc
)
1957 zserv_create_header(s
, ZEBRA_GET_LABEL_CHUNK
, vrf_id
);
1961 stream_putc(s
, lmc
->keep
);
1962 /* start and end labels */
1963 stream_putl(s
, lmc
->start
);
1964 stream_putl(s
, lmc
->end
);
1967 /* Write packet size. */
1968 stream_putw_at(s
, 0, stream_get_endp(s
));
1970 return writen(client
->sock
, s
->data
, stream_get_endp(s
));
1973 static void zread_get_label_chunk(struct zserv
*client
, vrf_id_t vrf_id
)
1978 struct label_manager_chunk
*lmc
;
1980 /* Get input stream. */
1984 keep
= stream_getc(s
);
1985 size
= stream_getl(s
);
1987 lmc
= assign_label_chunk(client
->proto
, client
->instance
, keep
, size
);
1989 zlog_err("%s: Unable to assign Label Chunk of size %u",
1992 zlog_debug("Assigned Label Chunk %u - %u to %u", lmc
->start
,
1994 /* send response back */
1995 zsend_assign_label_chunk_response(client
, vrf_id
, lmc
);
1998 static void zread_release_label_chunk(struct zserv
*client
)
2001 uint32_t start
, end
;
2003 /* Get input stream. */
2007 start
= stream_getl(s
);
2008 end
= stream_getl(s
);
2010 release_label_chunk(client
->proto
, client
->instance
, start
, end
);
2012 static void zread_label_manager_request(int cmd
, struct zserv
*client
,
2015 /* to avoid sending other messages like ZERBA_INTERFACE_UP */
2016 if (cmd
== ZEBRA_LABEL_MANAGER_CONNECT
)
2017 client
->is_synchronous
= 1;
2019 /* external label manager */
2021 zread_relay_label_manager_request(cmd
, client
, vrf_id
);
2022 /* this is a label manager */
2024 if (cmd
== ZEBRA_LABEL_MANAGER_CONNECT
)
2025 zread_label_manager_connect(client
, vrf_id
);
2027 /* Sanity: don't allow 'unidentified' requests */
2028 if (!client
->proto
) {
2030 "Got label request from an unidentified client");
2033 if (cmd
== ZEBRA_GET_LABEL_CHUNK
)
2034 zread_get_label_chunk(client
, vrf_id
);
2035 else if (cmd
== ZEBRA_RELEASE_LABEL_CHUNK
)
2036 zread_release_label_chunk(client
);
2041 /* Cleanup registered nexthops (across VRFs) upon client disconnect. */
2042 static void zebra_client_close_cleanup_rnh(struct zserv
*client
)
2045 struct zebra_vrf
*zvrf
;
2047 RB_FOREACH(vrf
, vrf_id_head
, &vrfs_by_id
)
2049 if ((zvrf
= vrf
->info
) != NULL
) {
2050 zebra_cleanup_rnh_client(zvrf_id(zvrf
), AF_INET
, client
,
2052 zebra_cleanup_rnh_client(zvrf_id(zvrf
), AF_INET6
,
2053 client
, RNH_NEXTHOP_TYPE
);
2054 zebra_cleanup_rnh_client(zvrf_id(zvrf
), AF_INET
, client
,
2055 RNH_IMPORT_CHECK_TYPE
);
2056 zebra_cleanup_rnh_client(zvrf_id(zvrf
), AF_INET6
,
2057 client
, RNH_IMPORT_CHECK_TYPE
);
2058 if (client
->proto
== ZEBRA_ROUTE_LDP
) {
2059 hash_iterate(zvrf
->lsp_table
,
2060 mpls_ldp_lsp_uninstall_all
,
2062 mpls_ldp_ftn_uninstall_all(zvrf
, AFI_IP
);
2063 mpls_ldp_ftn_uninstall_all(zvrf
, AFI_IP6
);
2069 /* Close zebra client. */
2070 static void zebra_client_close(struct zserv
*client
)
2072 /* Send client de-registration to BFD */
2073 zebra_ptm_bfd_client_deregister(client
->proto
);
2075 /* Cleanup any registered nexthops - across all VRFs. */
2076 zebra_client_close_cleanup_rnh(client
);
2078 /* Release Label Manager chunks */
2079 release_daemon_chunks(client
->proto
, client
->instance
);
2081 /* Cleanup any FECs registered by this client. */
2082 zebra_mpls_cleanup_fecs_for_client(vrf_info_lookup(VRF_DEFAULT
),
2085 /* Close file descriptor. */
2087 unsigned long nroutes
;
2089 close(client
->sock
);
2090 nroutes
= rib_score_proto(client
->proto
, client
->instance
);
2092 "client %d disconnected. %lu %s routes removed from the rib",
2093 client
->sock
, nroutes
,
2094 zebra_route_string(client
->proto
));
2098 /* Free stream buffers. */
2100 stream_free(client
->ibuf
);
2102 stream_free(client
->obuf
);
2104 buffer_free(client
->wb
);
2106 /* Release threads. */
2108 thread_cancel(client
->t_read
);
2109 if (client
->t_write
)
2110 thread_cancel(client
->t_write
);
2111 if (client
->t_suicide
)
2112 thread_cancel(client
->t_suicide
);
2115 for (afi_t afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
2116 for (int i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
2117 vrf_bitmap_free(client
->redist
[afi
][i
]);
2119 vrf_bitmap_free(client
->redist_default
);
2120 vrf_bitmap_free(client
->ifinfo
);
2121 vrf_bitmap_free(client
->ridinfo
);
2123 /* Free client structure. */
2124 listnode_delete(zebrad
.client_list
, client
);
2125 XFREE(MTYPE_TMP
, client
);
2128 /* Make new client. */
2129 static void zebra_client_create(int sock
)
2131 struct zserv
*client
;
2135 client
= XCALLOC(MTYPE_TMP
, sizeof(struct zserv
));
2137 /* Make client input/output buffer. */
2138 client
->sock
= sock
;
2139 client
->ibuf
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
2140 client
->obuf
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
2141 client
->wb
= buffer_new(0);
2143 /* Set table number. */
2144 client
->rtm_table
= zebrad
.rtm_table_default
;
2146 client
->connect_time
= monotime(NULL
);
2147 /* Initialize flags */
2148 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
2149 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
2150 client
->redist
[afi
][i
] = vrf_bitmap_init();
2151 client
->redist_default
= vrf_bitmap_init();
2152 client
->ifinfo
= vrf_bitmap_init();
2153 client
->ridinfo
= vrf_bitmap_init();
2155 /* by default, it's not a synchronous client */
2156 client
->is_synchronous
= 0;
2158 /* Add this client to linked list. */
2159 listnode_add(zebrad
.client_list
, client
);
2161 /* Make new read thread. */
2162 zebra_event(ZEBRA_READ
, sock
, client
);
2164 zebra_vrf_update_all(client
);
2167 /* Handler of zebra service request. */
2168 static int zebra_client_read(struct thread
*thread
)
2171 struct zserv
*client
;
2173 uint16_t length
, command
;
2174 uint8_t marker
, version
;
2176 struct zebra_vrf
*zvrf
;
2178 /* Get thread data. Reset reading thread because I'm running. */
2179 sock
= THREAD_FD(thread
);
2180 client
= THREAD_ARG(thread
);
2181 client
->t_read
= NULL
;
2183 if (client
->t_suicide
) {
2184 zebra_client_close(client
);
2188 /* Read length and command (if we don't have it already). */
2189 if ((already
= stream_get_endp(client
->ibuf
)) < ZEBRA_HEADER_SIZE
) {
2191 if (((nbyte
= stream_read_try(client
->ibuf
, sock
,
2192 ZEBRA_HEADER_SIZE
- already
))
2195 if (IS_ZEBRA_DEBUG_EVENT
)
2196 zlog_debug("connection closed socket [%d]",
2198 zebra_client_close(client
);
2201 if (nbyte
!= (ssize_t
)(ZEBRA_HEADER_SIZE
- already
)) {
2202 /* Try again later. */
2203 zebra_event(ZEBRA_READ
, sock
, client
);
2206 already
= ZEBRA_HEADER_SIZE
;
2209 /* Reset to read from the beginning of the incoming packet. */
2210 stream_set_getp(client
->ibuf
, 0);
2212 /* Fetch header values */
2213 length
= stream_getw(client
->ibuf
);
2214 marker
= stream_getc(client
->ibuf
);
2215 version
= stream_getc(client
->ibuf
);
2216 vrf_id
= stream_getw(client
->ibuf
);
2217 command
= stream_getw(client
->ibuf
);
2219 if (marker
!= ZEBRA_HEADER_MARKER
|| version
!= ZSERV_VERSION
) {
2221 "%s: socket %d version mismatch, marker %d, version %d",
2222 __func__
, sock
, marker
, version
);
2223 zebra_client_close(client
);
2226 if (length
< ZEBRA_HEADER_SIZE
) {
2228 "%s: socket %d message length %u is less than header size %d",
2229 __func__
, sock
, length
, ZEBRA_HEADER_SIZE
);
2230 zebra_client_close(client
);
2233 if (length
> STREAM_SIZE(client
->ibuf
)) {
2235 "%s: socket %d message length %u exceeds buffer size %lu",
2236 __func__
, sock
, length
,
2237 (u_long
)STREAM_SIZE(client
->ibuf
));
2238 zebra_client_close(client
);
2242 /* Read rest of data. */
2243 if (already
< length
) {
2245 if (((nbyte
= stream_read_try(client
->ibuf
, sock
,
2249 if (IS_ZEBRA_DEBUG_EVENT
)
2251 "connection closed [%d] when reading zebra data",
2253 zebra_client_close(client
);
2256 if (nbyte
!= (ssize_t
)(length
- already
)) {
2257 /* Try again later. */
2258 zebra_event(ZEBRA_READ
, sock
, client
);
2263 length
-= ZEBRA_HEADER_SIZE
;
2265 /* Debug packet information. */
2266 if (IS_ZEBRA_DEBUG_EVENT
)
2267 zlog_debug("zebra message comes from socket [%d]", sock
);
2269 if (IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
)
2270 zlog_debug("zebra message received [%s] %d in VRF %u",
2271 zserv_command_string(command
), length
, vrf_id
);
2273 client
->last_read_time
= monotime(NULL
);
2274 client
->last_read_cmd
= command
;
2276 zvrf
= zebra_vrf_lookup_by_id(vrf_id
);
2278 if (IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
)
2279 zlog_debug("zebra received unknown VRF[%u]", vrf_id
);
2280 goto zclient_read_out
;
2284 case ZEBRA_ROUTER_ID_ADD
:
2285 zread_router_id_add(client
, length
, zvrf
);
2287 case ZEBRA_ROUTER_ID_DELETE
:
2288 zread_router_id_delete(client
, length
, zvrf
);
2290 case ZEBRA_INTERFACE_ADD
:
2291 zread_interface_add(client
, length
, zvrf
);
2293 case ZEBRA_INTERFACE_DELETE
:
2294 zread_interface_delete(client
, length
, zvrf
);
2296 case ZEBRA_IPV4_ROUTE_ADD
:
2297 zread_ipv4_add(client
, length
, zvrf
);
2299 case ZEBRA_IPV4_ROUTE_DELETE
:
2300 zread_ipv4_delete(client
, length
, zvrf
);
2302 case ZEBRA_IPV4_ROUTE_IPV6_NEXTHOP_ADD
:
2303 zread_ipv4_route_ipv6_nexthop_add(client
, length
, zvrf
);
2305 case ZEBRA_IPV4_NEXTHOP_ADD
:
2306 zread_ipv4_add(client
, length
,
2307 zvrf
); /* LB: r1.0 merge - id was 1 */
2309 case ZEBRA_IPV4_NEXTHOP_DELETE
:
2310 zread_ipv4_delete(client
, length
,
2311 zvrf
); /* LB: r1.0 merge - id was 1 */
2313 case ZEBRA_IPV6_ROUTE_ADD
:
2314 zread_ipv6_add(client
, length
, zvrf
);
2316 case ZEBRA_IPV6_ROUTE_DELETE
:
2317 zread_ipv6_delete(client
, length
, zvrf
);
2319 case ZEBRA_REDISTRIBUTE_ADD
:
2320 zebra_redistribute_add(command
, client
, length
, zvrf
);
2322 case ZEBRA_REDISTRIBUTE_DELETE
:
2323 zebra_redistribute_delete(command
, client
, length
, zvrf
);
2325 case ZEBRA_REDISTRIBUTE_DEFAULT_ADD
:
2326 zebra_redistribute_default_add(command
, client
, length
, zvrf
);
2328 case ZEBRA_REDISTRIBUTE_DEFAULT_DELETE
:
2329 zebra_redistribute_default_delete(command
, client
, length
,
2332 case ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB
:
2333 zread_ipv4_nexthop_lookup_mrib(client
, length
, zvrf
);
2336 zread_hello(client
);
2338 case ZEBRA_NEXTHOP_REGISTER
:
2339 zserv_rnh_register(client
, sock
, length
, RNH_NEXTHOP_TYPE
,
2342 case ZEBRA_NEXTHOP_UNREGISTER
:
2343 zserv_rnh_unregister(client
, sock
, length
, RNH_NEXTHOP_TYPE
,
2346 case ZEBRA_IMPORT_ROUTE_REGISTER
:
2347 zserv_rnh_register(client
, sock
, length
, RNH_IMPORT_CHECK_TYPE
,
2350 case ZEBRA_IMPORT_ROUTE_UNREGISTER
:
2351 zserv_rnh_unregister(client
, sock
, length
,
2352 RNH_IMPORT_CHECK_TYPE
, zvrf
);
2354 case ZEBRA_BFD_DEST_UPDATE
:
2355 case ZEBRA_BFD_DEST_REGISTER
:
2356 zebra_ptm_bfd_dst_register(client
, sock
, length
, command
, zvrf
);
2358 case ZEBRA_BFD_DEST_DEREGISTER
:
2359 zebra_ptm_bfd_dst_deregister(client
, sock
, length
, zvrf
);
2361 case ZEBRA_VRF_UNREGISTER
:
2362 zread_vrf_unregister(client
, length
, zvrf
);
2364 case ZEBRA_BFD_CLIENT_REGISTER
:
2365 zebra_ptm_bfd_client_register(client
, sock
, length
);
2367 case ZEBRA_INTERFACE_ENABLE_RADV
:
2368 #if defined(HAVE_RTADV)
2369 zebra_interface_radv_set(client
, sock
, length
, zvrf
, 1);
2372 case ZEBRA_INTERFACE_DISABLE_RADV
:
2373 #if defined(HAVE_RTADV)
2374 zebra_interface_radv_set(client
, sock
, length
, zvrf
, 0);
2377 case ZEBRA_MPLS_LABELS_ADD
:
2378 case ZEBRA_MPLS_LABELS_DELETE
:
2379 zread_mpls_labels(command
, client
, length
, vrf_id
);
2381 case ZEBRA_IPMR_ROUTE_STATS
:
2382 zebra_ipmr_route_stats(client
, sock
, length
, zvrf
);
2384 case ZEBRA_LABEL_MANAGER_CONNECT
:
2385 case ZEBRA_GET_LABEL_CHUNK
:
2386 case ZEBRA_RELEASE_LABEL_CHUNK
:
2387 zread_label_manager_request(command
, client
, vrf_id
);
2389 case ZEBRA_FEC_REGISTER
:
2390 zserv_fec_register(client
, sock
, length
);
2392 case ZEBRA_FEC_UNREGISTER
:
2393 zserv_fec_unregister(client
, sock
, length
);
2395 case ZEBRA_ADVERTISE_ALL_VNI
:
2396 zebra_vxlan_advertise_all_vni(client
, sock
, length
, zvrf
);
2398 case ZEBRA_REMOTE_VTEP_ADD
:
2399 zebra_vxlan_remote_vtep_add(client
, sock
, length
, zvrf
);
2401 case ZEBRA_REMOTE_VTEP_DEL
:
2402 zebra_vxlan_remote_vtep_del(client
, sock
, length
, zvrf
);
2404 case ZEBRA_REMOTE_MACIP_ADD
:
2405 zebra_vxlan_remote_macip_add(client
, sock
, length
, zvrf
);
2407 case ZEBRA_REMOTE_MACIP_DEL
:
2408 zebra_vxlan_remote_macip_del(client
, sock
, length
, zvrf
);
2411 zlog_info("Zebra received unknown command %d", command
);
2415 if (client
->t_suicide
) {
2416 /* No need to wait for thread callback, just kill immediately.
2418 zebra_client_close(client
);
2423 stream_reset(client
->ibuf
);
2424 zebra_event(ZEBRA_READ
, sock
, client
);
2429 /* Accept code of zebra server socket. */
2430 static int zebra_accept(struct thread
*thread
)
2434 struct sockaddr_in client
;
2437 accept_sock
= THREAD_FD(thread
);
2439 /* Reregister myself. */
2440 zebra_event(ZEBRA_SERV
, accept_sock
, NULL
);
2442 len
= sizeof(struct sockaddr_in
);
2443 client_sock
= accept(accept_sock
, (struct sockaddr
*)&client
, &len
);
2445 if (client_sock
< 0) {
2446 zlog_warn("Can't accept zebra socket: %s",
2447 safe_strerror(errno
));
2451 /* Make client socket non-blocking. */
2452 set_nonblocking(client_sock
);
2454 /* Create new zebra client. */
2455 zebra_client_create(client_sock
);
2460 #ifdef HAVE_TCP_ZEBRA
2461 /* Make zebra's server socket. */
2462 static void zebra_serv()
2466 struct sockaddr_in addr
;
2468 accept_sock
= socket(AF_INET
, SOCK_STREAM
, 0);
2470 if (accept_sock
< 0) {
2471 zlog_warn("Can't create zserv stream socket: %s",
2472 safe_strerror(errno
));
2474 "zebra can't provice full functionality due to above error");
2478 memset(&addr
, 0, sizeof(struct sockaddr_in
));
2479 addr
.sin_family
= AF_INET
;
2480 addr
.sin_port
= htons(ZEBRA_PORT
);
2481 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
2482 addr
.sin_len
= sizeof(struct sockaddr_in
);
2483 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
2484 addr
.sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
);
2486 sockopt_reuseaddr(accept_sock
);
2487 sockopt_reuseport(accept_sock
);
2489 if (zserv_privs
.change(ZPRIVS_RAISE
))
2490 zlog_err("Can't raise privileges");
2492 ret
= bind(accept_sock
, (struct sockaddr
*)&addr
,
2493 sizeof(struct sockaddr_in
));
2495 zlog_warn("Can't bind to stream socket: %s",
2496 safe_strerror(errno
));
2498 "zebra can't provice full functionality due to above error");
2499 close(accept_sock
); /* Avoid sd leak. */
2503 if (zserv_privs
.change(ZPRIVS_LOWER
))
2504 zlog_err("Can't lower privileges");
2506 ret
= listen(accept_sock
, 1);
2508 zlog_warn("Can't listen to stream socket: %s",
2509 safe_strerror(errno
));
2511 "zebra can't provice full functionality due to above error");
2512 close(accept_sock
); /* Avoid sd leak. */
2516 zebra_event(ZEBRA_SERV
, accept_sock
, NULL
);
2518 #else /* HAVE_TCP_ZEBRA */
2520 /* For sockaddr_un. */
2523 /* zebra server UNIX domain socket. */
2524 static void zebra_serv_un(const char *path
)
2528 struct sockaddr_un serv
;
2531 /* First of all, unlink existing socket */
2535 old_mask
= umask(0077);
2537 /* Make UNIX domain socket. */
2538 sock
= socket(AF_UNIX
, SOCK_STREAM
, 0);
2540 zlog_warn("Can't create zserv unix socket: %s",
2541 safe_strerror(errno
));
2543 "zebra can't provide full functionality due to above error");
2547 /* Make server socket. */
2548 memset(&serv
, 0, sizeof(struct sockaddr_un
));
2549 serv
.sun_family
= AF_UNIX
;
2550 strncpy(serv
.sun_path
, path
, strlen(path
));
2551 #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
2552 len
= serv
.sun_len
= SUN_LEN(&serv
);
2554 len
= sizeof(serv
.sun_family
) + strlen(serv
.sun_path
);
2555 #endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
2557 ret
= bind(sock
, (struct sockaddr
*)&serv
, len
);
2559 zlog_warn("Can't bind to unix socket %s: %s", path
,
2560 safe_strerror(errno
));
2562 "zebra can't provide full functionality due to above error");
2567 ret
= listen(sock
, 5);
2569 zlog_warn("Can't listen to unix socket %s: %s", path
,
2570 safe_strerror(errno
));
2572 "zebra can't provide full functionality due to above error");
2579 zebra_event(ZEBRA_SERV
, sock
, NULL
);
2581 #endif /* HAVE_TCP_ZEBRA */
2584 static void zebra_event(enum event event
, int sock
, struct zserv
*client
)
2588 thread_add_read(zebrad
.master
, zebra_accept
, client
, sock
,
2592 client
->t_read
= NULL
;
2593 thread_add_read(zebrad
.master
, zebra_client_read
, client
, sock
,
2602 #define ZEBRA_TIME_BUF 32
2603 static char *zserv_time_buf(time_t *time1
, char *buf
, int buflen
)
2608 assert(buf
!= NULL
);
2609 assert(buflen
>= ZEBRA_TIME_BUF
);
2610 assert(time1
!= NULL
);
2613 snprintf(buf
, buflen
, "never ");
2617 now
= monotime(NULL
);
2621 /* Making formatted timer strings. */
2622 #define ONE_DAY_SECOND 60*60*24
2623 #define ONE_WEEK_SECOND 60*60*24*7
2625 if (now
< ONE_DAY_SECOND
)
2626 snprintf(buf
, buflen
, "%02d:%02d:%02d", tm
->tm_hour
, tm
->tm_min
,
2628 else if (now
< ONE_WEEK_SECOND
)
2629 snprintf(buf
, buflen
, "%dd%02dh%02dm", tm
->tm_yday
, tm
->tm_hour
,
2632 snprintf(buf
, buflen
, "%02dw%dd%02dh", tm
->tm_yday
/ 7,
2633 tm
->tm_yday
- ((tm
->tm_yday
/ 7) * 7), tm
->tm_hour
);
2637 static void zebra_show_client_detail(struct vty
*vty
, struct zserv
*client
)
2639 char cbuf
[ZEBRA_TIME_BUF
], rbuf
[ZEBRA_TIME_BUF
];
2640 char wbuf
[ZEBRA_TIME_BUF
], nhbuf
[ZEBRA_TIME_BUF
], mbuf
[ZEBRA_TIME_BUF
];
2642 vty_out(vty
, "Client: %s", zebra_route_string(client
->proto
));
2643 if (client
->instance
)
2644 vty_out(vty
, " Instance: %d", client
->instance
);
2647 vty_out(vty
, "------------------------ \n");
2648 vty_out(vty
, "FD: %d \n", client
->sock
);
2649 vty_out(vty
, "Route Table ID: %d \n", client
->rtm_table
);
2651 vty_out(vty
, "Connect Time: %s \n",
2652 zserv_time_buf(&client
->connect_time
, cbuf
, ZEBRA_TIME_BUF
));
2653 if (client
->nh_reg_time
) {
2654 vty_out(vty
, "Nexthop Registry Time: %s \n",
2655 zserv_time_buf(&client
->nh_reg_time
, nhbuf
,
2657 if (client
->nh_last_upd_time
)
2658 vty_out(vty
, "Nexthop Last Update Time: %s \n",
2659 zserv_time_buf(&client
->nh_last_upd_time
, mbuf
,
2662 vty_out(vty
, "No Nexthop Update sent\n");
2664 vty_out(vty
, "Not registered for Nexthop Updates\n");
2666 vty_out(vty
, "Last Msg Rx Time: %s \n",
2667 zserv_time_buf(&client
->last_read_time
, rbuf
, ZEBRA_TIME_BUF
));
2668 vty_out(vty
, "Last Msg Tx Time: %s \n",
2669 zserv_time_buf(&client
->last_write_time
, wbuf
, ZEBRA_TIME_BUF
));
2670 if (client
->last_read_time
)
2671 vty_out(vty
, "Last Rcvd Cmd: %s \n",
2672 zserv_command_string(client
->last_read_cmd
));
2673 if (client
->last_write_time
)
2674 vty_out(vty
, "Last Sent Cmd: %s \n",
2675 zserv_command_string(client
->last_write_cmd
));
2678 vty_out(vty
, "Type Add Update Del \n");
2679 vty_out(vty
, "================================================== \n");
2680 vty_out(vty
, "IPv4 %-12d%-12d%-12d\n", client
->v4_route_add_cnt
,
2681 client
->v4_route_upd8_cnt
, client
->v4_route_del_cnt
);
2682 vty_out(vty
, "IPv6 %-12d%-12d%-12d\n", client
->v6_route_add_cnt
,
2683 client
->v6_route_upd8_cnt
, client
->v6_route_del_cnt
);
2684 vty_out(vty
, "Redist:v4 %-12d%-12d%-12d\n", client
->redist_v4_add_cnt
,
2685 0, client
->redist_v4_del_cnt
);
2686 vty_out(vty
, "Redist:v6 %-12d%-12d%-12d\n", client
->redist_v6_add_cnt
,
2687 0, client
->redist_v6_del_cnt
);
2688 vty_out(vty
, "Connected %-12d%-12d%-12d\n", client
->ifadd_cnt
, 0,
2690 vty_out(vty
, "BFD peer %-12d%-12d%-12d\n", client
->bfd_peer_add_cnt
,
2691 client
->bfd_peer_upd8_cnt
, client
->bfd_peer_del_cnt
);
2692 vty_out(vty
, "Interface Up Notifications: %d\n", client
->ifup_cnt
);
2693 vty_out(vty
, "Interface Down Notifications: %d\n", client
->ifdown_cnt
);
2694 vty_out(vty
, "VNI add notifications: %d\n", client
->vniadd_cnt
);
2695 vty_out(vty
, "VNI delete notifications: %d\n", client
->vnidel_cnt
);
2696 vty_out(vty
, "MAC-IP add notifications: %d\n", client
->macipadd_cnt
);
2697 vty_out(vty
, "MAC-IP delete notifications: %d\n", client
->macipdel_cnt
);
2703 static void zebra_show_client_brief(struct vty
*vty
, struct zserv
*client
)
2705 char cbuf
[ZEBRA_TIME_BUF
], rbuf
[ZEBRA_TIME_BUF
];
2706 char wbuf
[ZEBRA_TIME_BUF
];
2708 vty_out(vty
, "%-8s%12s %12s%12s%8d/%-8d%8d/%-8d\n",
2709 zebra_route_string(client
->proto
),
2710 zserv_time_buf(&client
->connect_time
, cbuf
, ZEBRA_TIME_BUF
),
2711 zserv_time_buf(&client
->last_read_time
, rbuf
, ZEBRA_TIME_BUF
),
2712 zserv_time_buf(&client
->last_write_time
, wbuf
, ZEBRA_TIME_BUF
),
2713 client
->v4_route_add_cnt
+ client
->v4_route_upd8_cnt
,
2714 client
->v4_route_del_cnt
,
2715 client
->v6_route_add_cnt
+ client
->v6_route_upd8_cnt
,
2716 client
->v6_route_del_cnt
);
2719 struct zserv
*zebra_find_client(u_char proto
)
2721 struct listnode
*node
, *nnode
;
2722 struct zserv
*client
;
2724 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, node
, nnode
, client
)) {
2725 if (client
->proto
== proto
)
2733 /* Display default rtm_table for all clients. */
2738 "default routing table to use for all clients\n")
2740 vty_out(vty
, "table %d\n", zebrad
.rtm_table_default
);
2744 DEFUN (config_table
,
2747 "Configure target kernel routing table\n"
2750 zebrad
.rtm_table_default
= strtol(argv
[1]->arg
, (char **)0, 10);
2754 DEFUN (no_config_table
,
2755 no_config_table_cmd
,
2756 "no table [TABLENO]",
2758 "Configure target kernel routing table\n"
2761 zebrad
.rtm_table_default
= 0;
2766 DEFUN (ip_forwarding
,
2770 "Turn on IP forwarding")
2776 ret
= ipforward_on();
2779 vty_out(vty
, "Can't turn on IP forwarding\n");
2780 return CMD_WARNING_CONFIG_FAILED
;
2786 DEFUN (no_ip_forwarding
,
2787 no_ip_forwarding_cmd
,
2791 "Turn off IP forwarding")
2797 ret
= ipforward_off();
2800 vty_out(vty
, "Can't turn off IP forwarding\n");
2801 return CMD_WARNING_CONFIG_FAILED
;
2811 "Zebra information\n")
2816 " Route Route Neighbor LSP LSP\n");
2818 "VRF Installs Removals Updates Installs Removals\n");
2819 RB_FOREACH(vrf
, vrf_name_head
, &vrfs_by_name
)
2821 struct zebra_vrf
*zvrf
= vrf
->info
;
2822 vty_out(vty
, "%-25s %10" PRIu64
" %10" PRIu64
" %10" PRIu64
2823 " %10" PRIu64
" %10" PRIu64
"\n",
2824 vrf
->name
, zvrf
->installs
, zvrf
->removals
,
2825 zvrf
->neigh_updates
, zvrf
->lsp_installs
,
2826 zvrf
->lsp_removals
);
2832 /* This command is for debugging purpose. */
2833 DEFUN (show_zebra_client
,
2834 show_zebra_client_cmd
,
2835 "show zebra client",
2837 "Zebra information\n"
2838 "Client information\n")
2840 struct listnode
*node
;
2841 struct zserv
*client
;
2843 for (ALL_LIST_ELEMENTS_RO(zebrad
.client_list
, node
, client
))
2844 zebra_show_client_detail(vty
, client
);
2849 /* This command is for debugging purpose. */
2850 DEFUN (show_zebra_client_summary
,
2851 show_zebra_client_summary_cmd
,
2852 "show zebra client summary",
2854 "Zebra information brief\n"
2855 "Client information brief\n"
2858 struct listnode
*node
;
2859 struct zserv
*client
;
2862 "Name Connect Time Last Read Last Write IPv4 Routes IPv6 Routes \n");
2864 "--------------------------------------------------------------------------------\n");
2866 for (ALL_LIST_ELEMENTS_RO(zebrad
.client_list
, node
, client
))
2867 zebra_show_client_brief(vty
, client
);
2869 vty_out(vty
, "Routes column shows (added+updated)/deleted\n");
2873 /* Table configuration write function. */
2874 static int config_write_table(struct vty
*vty
)
2876 if (zebrad
.rtm_table_default
)
2877 vty_out(vty
, "table %d\n", zebrad
.rtm_table_default
);
2881 /* table node for routing tables. */
2882 static struct cmd_node table_node
= {TABLE_NODE
,
2883 "", /* This node has no interface. */
2886 /* Only display ip forwarding is enabled or not. */
2887 DEFUN (show_ip_forwarding
,
2888 show_ip_forwarding_cmd
,
2889 "show ip forwarding",
2892 "IP forwarding status\n")
2899 vty_out(vty
, "IP forwarding is off\n");
2901 vty_out(vty
, "IP forwarding is on\n");
2905 /* Only display ipv6 forwarding is enabled or not. */
2906 DEFUN (show_ipv6_forwarding
,
2907 show_ipv6_forwarding_cmd
,
2908 "show ipv6 forwarding",
2910 "IPv6 information\n"
2911 "Forwarding status\n")
2915 ret
= ipforward_ipv6();
2919 vty_out(vty
, "ipv6 forwarding is unknown\n");
2922 vty_out(vty
, "ipv6 forwarding is %s\n", "off");
2925 vty_out(vty
, "ipv6 forwarding is %s\n", "on");
2928 vty_out(vty
, "ipv6 forwarding is %s\n", "off");
2934 DEFUN (ipv6_forwarding
,
2935 ipv6_forwarding_cmd
,
2938 "Turn on IPv6 forwarding")
2942 ret
= ipforward_ipv6();
2944 ret
= ipforward_ipv6_on();
2947 vty_out(vty
, "Can't turn on IPv6 forwarding\n");
2948 return CMD_WARNING_CONFIG_FAILED
;
2954 DEFUN (no_ipv6_forwarding
,
2955 no_ipv6_forwarding_cmd
,
2956 "no ipv6 forwarding",
2959 "Turn off IPv6 forwarding")
2963 ret
= ipforward_ipv6();
2965 ret
= ipforward_ipv6_off();
2968 vty_out(vty
, "Can't turn off IPv6 forwarding\n");
2969 return CMD_WARNING_CONFIG_FAILED
;
2975 /* IPForwarding configuration write function. */
2976 static int config_write_forwarding(struct vty
*vty
)
2978 /* FIXME: Find better place for that. */
2979 router_id_write(vty
);
2982 vty_out(vty
, "no ip forwarding\n");
2983 if (!ipforward_ipv6())
2984 vty_out(vty
, "no ipv6 forwarding\n");
2985 vty_out(vty
, "!\n");
2989 /* table node for routing tables. */
2990 static struct cmd_node forwarding_node
= {FORWARDING_NODE
,
2991 "", /* This node has no interface. */
2994 /* Initialisation of zebra and installation of commands. */
2995 void zebra_init(void)
2997 /* Client list init. */
2998 zebrad
.client_list
= list_new();
3000 /* Install configuration write function. */
3001 install_node(&table_node
, config_write_table
);
3002 install_node(&forwarding_node
, config_write_forwarding
);
3004 install_element(VIEW_NODE
, &show_ip_forwarding_cmd
);
3005 install_element(CONFIG_NODE
, &ip_forwarding_cmd
);
3006 install_element(CONFIG_NODE
, &no_ip_forwarding_cmd
);
3007 install_element(ENABLE_NODE
, &show_zebra_cmd
);
3008 install_element(ENABLE_NODE
, &show_zebra_client_cmd
);
3009 install_element(ENABLE_NODE
, &show_zebra_client_summary_cmd
);
3012 install_element(VIEW_NODE
, &show_table_cmd
);
3013 install_element(CONFIG_NODE
, &config_table_cmd
);
3014 install_element(CONFIG_NODE
, &no_config_table_cmd
);
3015 #endif /* HAVE_NETLINK */
3017 install_element(VIEW_NODE
, &show_ipv6_forwarding_cmd
);
3018 install_element(CONFIG_NODE
, &ipv6_forwarding_cmd
);
3019 install_element(CONFIG_NODE
, &no_ipv6_forwarding_cmd
);
3022 zebra_route_map_init();
3025 /* Make zebra server socket, wiping any existing one (see bug #403). */
3026 void zebra_zserv_socket_init(char *path
)
3028 #ifdef HAVE_TCP_ZEBRA
3031 zebra_serv_un(path
? path
: ZEBRA_SERV_PATH
);
3032 #endif /* HAVE_TCP_ZEBRA */