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"
58 /* Event list of zebra. */
59 enum event
{ ZEBRA_SERV
, ZEBRA_READ
, ZEBRA_WRITE
};
61 static void zebra_event (enum event event
, int sock
, struct zserv
*client
);
63 extern struct zebra_privs_t zserv_privs
;
65 static void zebra_client_close (struct zserv
*client
);
68 zserv_delayed_close(struct thread
*thread
)
70 struct zserv
*client
= THREAD_ARG(thread
);
72 client
->t_suicide
= NULL
;
73 zebra_client_close(client
);
78 zserv_flush_data(struct thread
*thread
)
80 struct zserv
*client
= THREAD_ARG(thread
);
82 client
->t_write
= NULL
;
83 if (client
->t_suicide
)
85 zebra_client_close(client
);
88 switch (buffer_flush_available(client
->wb
, client
->sock
))
91 zlog_warn("%s: buffer_flush_available failed on zserv client fd %d, "
92 "closing", __func__
, client
->sock
);
93 zebra_client_close(client
);
97 client
->t_write
= NULL
;
98 thread_add_write(zebrad
.master
, zserv_flush_data
, client
, client
->sock
,
106 client
->last_write_time
= monotime(NULL
);
111 zebra_server_send_message(struct zserv
*client
)
113 if (client
->t_suicide
)
116 if (client
->is_synchronous
)
119 stream_set_getp(client
->obuf
, 0);
120 client
->last_write_cmd
= stream_getw_from(client
->obuf
, 6);
121 switch (buffer_write(client
->wb
, client
->sock
, STREAM_DATA(client
->obuf
),
122 stream_get_endp(client
->obuf
)))
125 zlog_warn("%s: buffer_write failed to zserv client fd %d, closing",
126 __func__
, client
->sock
);
127 /* Schedule a delayed close since many of the functions that call this
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 be
131 client
->t_suicide
= NULL
;
132 thread_add_event(zebrad
.master
, zserv_delayed_close
, client
, 0,
136 THREAD_OFF(client
->t_write
);
139 thread_add_write(zebrad
.master
, zserv_flush_data
, client
, client
->sock
,
144 client
->last_write_time
= monotime(NULL
);
149 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
);
156 stream_putw (s
, cmd
);
160 zserv_encode_interface (struct stream
*s
, struct interface
*ifp
)
162 /* Interface information. */
163 stream_put (s
, ifp
->name
, INTERFACE_NAMSIZ
);
164 stream_putl (s
, ifp
->ifindex
);
165 stream_putc (s
, ifp
->status
);
166 stream_putq (s
, ifp
->flags
);
167 stream_putc (s
, ifp
->ptm_enable
);
168 stream_putc (s
, ifp
->ptm_status
);
169 stream_putl (s
, ifp
->metric
);
170 stream_putl (s
, ifp
->speed
);
171 stream_putl (s
, ifp
->mtu
);
172 stream_putl (s
, ifp
->mtu6
);
173 stream_putl (s
, ifp
->bandwidth
);
174 stream_putl (s
, ifp
->ll_type
);
175 stream_putl (s
, ifp
->hw_addr_len
);
176 if (ifp
->hw_addr_len
)
177 stream_put (s
, ifp
->hw_addr
, ifp
->hw_addr_len
);
179 /* Then, Traffic Engineering parameters if any */
180 if (HAS_LINK_PARAMS(ifp
) && IS_LINK_PARAMS_SET(ifp
->link_params
))
183 zebra_interface_link_params_write (s
, ifp
);
188 /* Write packet size. */
189 stream_putw_at (s
, 0, stream_get_endp (s
));
193 zserv_encode_vrf (struct stream
*s
, struct zebra_vrf
*zvrf
)
195 struct vrf_data data
;
197 data
.l
.table_id
= zvrf
->table_id
;
198 /* Pass the tableid */
199 stream_put (s
, &data
, sizeof (struct vrf_data
));
200 /* Interface information. */
201 stream_put (s
, zvrf_name (zvrf
), VRF_NAMSIZ
);
203 /* Write packet size. */
204 stream_putw_at (s
, 0, stream_get_endp (s
));
207 /* Interface is added. Send ZEBRA_INTERFACE_ADD to client. */
209 * This function is called in the following situations:
210 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
212 * - at startup, when zebra figures out the available interfaces
213 * - when an interface is added (where support for
214 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
215 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
219 zsend_interface_add (struct zserv
*client
, struct interface
*ifp
)
226 zserv_create_header (s
, ZEBRA_INTERFACE_ADD
, ifp
->vrf_id
);
227 zserv_encode_interface (s
, ifp
);
230 return zebra_server_send_message(client
);
233 /* Interface deletion from zebra daemon. */
235 zsend_interface_delete (struct zserv
*client
, struct interface
*ifp
)
242 zserv_create_header (s
, ZEBRA_INTERFACE_DELETE
, ifp
->vrf_id
);
243 zserv_encode_interface (s
, ifp
);
246 return zebra_server_send_message (client
);
250 zsend_vrf_add (struct zserv
*client
, struct zebra_vrf
*zvrf
)
257 zserv_create_header (s
, ZEBRA_VRF_ADD
, zvrf_id (zvrf
));
258 zserv_encode_vrf (s
, zvrf
);
260 client
->vrfadd_cnt
++;
261 return zebra_server_send_message(client
);
264 /* VRF deletion from zebra daemon. */
266 zsend_vrf_delete (struct zserv
*client
, struct zebra_vrf
*zvrf
)
273 zserv_create_header (s
, ZEBRA_VRF_DELETE
, zvrf_id (zvrf
));
274 zserv_encode_vrf (s
, zvrf
);
276 client
->vrfdel_cnt
++;
277 return zebra_server_send_message (client
);
281 zsend_interface_link_params (struct zserv
*client
, struct interface
*ifp
)
285 /* Check this client need interface information. */
286 if (! client
->ifinfo
)
289 if (!ifp
->link_params
)
294 zserv_create_header (s
, ZEBRA_INTERFACE_LINK_PARAMS
, ifp
->vrf_id
);
296 /* Add Interface Index */
297 stream_putl (s
, ifp
->ifindex
);
299 /* Then TE Link Parameters */
300 if (zebra_interface_link_params_write (s
, ifp
) == 0)
303 /* Write packet size. */
304 stream_putw_at (s
, 0, stream_get_endp (s
));
306 return zebra_server_send_message (client
);
309 /* Interface address is added/deleted. Send ZEBRA_INTERFACE_ADDRESS_ADD or
310 * ZEBRA_INTERFACE_ADDRESS_DELETE to the client.
312 * A ZEBRA_INTERFACE_ADDRESS_ADD is sent in the following situations:
313 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
314 * from the client, after the ZEBRA_INTERFACE_ADD has been
315 * sent from zebra to the client
316 * - redistribute new address info to all clients in the following situations
317 * - at startup, when zebra figures out the available interfaces
318 * - when an interface is added (where support for
319 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
320 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
322 * - for the vty commands "ip address A.B.C.D/M [<secondary>|<label LINE>]"
323 * and "no bandwidth <1-10000000>", "ipv6 address X:X::X:X/M"
324 * - when an RTM_NEWADDR message is received from the kernel,
326 * The call tree that triggers ZEBRA_INTERFACE_ADDRESS_DELETE:
328 * zsend_interface_address(DELETE)
331 * zebra_interface_address_delete_update
333 * | | if_delete_update
335 * ip_address_uninstall connected_delete_ipv4
336 * [ipv6_addresss_uninstall] [connected_delete_ipv6]
339 * | RTM_NEWADDR on routing/netlink socket
342 * "no ip address A.B.C.D/M [label LINE]"
343 * "no ip address A.B.C.D/M secondary"
344 * ["no ipv6 address X:X::X:X/M"]
348 zsend_interface_address (int cmd
, struct zserv
*client
,
349 struct interface
*ifp
, struct connected
*ifc
)
358 zserv_create_header (s
, cmd
, ifp
->vrf_id
);
359 stream_putl (s
, ifp
->ifindex
);
361 /* Interface address flag. */
362 stream_putc (s
, ifc
->flags
);
364 /* Prefix information. */
366 stream_putc (s
, p
->family
);
367 blen
= prefix_blen (p
);
368 stream_put (s
, &p
->u
.prefix
, blen
);
371 * XXX gnu version does not send prefixlen for ZEBRA_INTERFACE_ADDRESS_DELETE
372 * but zebra_interface_address_delete_read() in the gnu version
375 stream_putc (s
, p
->prefixlen
);
378 p
= ifc
->destination
;
380 stream_put (s
, &p
->u
.prefix
, blen
);
382 stream_put (s
, NULL
, blen
);
384 /* Write packet size. */
385 stream_putw_at (s
, 0, stream_get_endp (s
));
387 client
->connected_rt_add_cnt
++;
388 return zebra_server_send_message(client
);
392 zsend_interface_nbr_address (int cmd
, struct zserv
*client
,
393 struct interface
*ifp
, struct nbr_connected
*ifc
)
402 zserv_create_header (s
, cmd
, ifp
->vrf_id
);
403 stream_putl (s
, ifp
->ifindex
);
405 /* Prefix information. */
407 stream_putc (s
, p
->family
);
408 blen
= prefix_blen (p
);
409 stream_put (s
, &p
->u
.prefix
, blen
);
412 * XXX gnu version does not send prefixlen for ZEBRA_INTERFACE_ADDRESS_DELETE
413 * but zebra_interface_address_delete_read() in the gnu version
416 stream_putc (s
, p
->prefixlen
);
418 /* Write packet size. */
419 stream_putw_at (s
, 0, stream_get_endp (s
));
421 return zebra_server_send_message(client
);
424 /* Interface address addition. */
426 zebra_interface_nbr_address_add_update (struct interface
*ifp
,
427 struct nbr_connected
*ifc
)
429 struct listnode
*node
, *nnode
;
430 struct zserv
*client
;
433 if (IS_ZEBRA_DEBUG_EVENT
)
435 char buf
[INET6_ADDRSTRLEN
];
438 zlog_debug ("MESSAGE: ZEBRA_INTERFACE_NBR_ADDRESS_ADD %s/%d on %s",
439 inet_ntop (p
->family
, &p
->u
.prefix
, buf
, INET6_ADDRSTRLEN
),
440 p
->prefixlen
, ifc
->ifp
->name
);
443 for (ALL_LIST_ELEMENTS (zebrad
.client_list
, node
, nnode
, client
))
444 zsend_interface_nbr_address (ZEBRA_INTERFACE_NBR_ADDRESS_ADD
, client
, ifp
, ifc
);
447 /* Interface address deletion. */
449 zebra_interface_nbr_address_delete_update (struct interface
*ifp
,
450 struct nbr_connected
*ifc
)
452 struct listnode
*node
, *nnode
;
453 struct zserv
*client
;
456 if (IS_ZEBRA_DEBUG_EVENT
)
458 char buf
[INET6_ADDRSTRLEN
];
461 zlog_debug ("MESSAGE: ZEBRA_INTERFACE_NBR_ADDRESS_DELETE %s/%d on %s",
462 inet_ntop (p
->family
, &p
->u
.prefix
, buf
, INET6_ADDRSTRLEN
),
463 p
->prefixlen
, ifc
->ifp
->name
);
466 for (ALL_LIST_ELEMENTS (zebrad
.client_list
, node
, nnode
, client
))
467 zsend_interface_nbr_address (ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
, client
, ifp
, ifc
);
470 /* Send addresses on interface to client */
472 zsend_interface_addresses (struct zserv
*client
, struct interface
*ifp
)
474 struct listnode
*cnode
, *cnnode
;
476 struct nbr_connected
*nc
;
478 /* Send interface addresses. */
479 for (ALL_LIST_ELEMENTS (ifp
->connected
, cnode
, cnnode
, c
))
481 if (!CHECK_FLAG (c
->conf
, ZEBRA_IFC_REAL
))
484 if (zsend_interface_address (ZEBRA_INTERFACE_ADDRESS_ADD
, client
,
489 /* Send interface neighbors. */
490 for (ALL_LIST_ELEMENTS (ifp
->nbr_connected
, cnode
, cnnode
, nc
))
492 if (zsend_interface_nbr_address (ZEBRA_INTERFACE_NBR_ADDRESS_ADD
,
493 client
, ifp
, nc
) < 0)
500 /* Notify client about interface moving from one VRF to another.
501 * Whether client is interested in old and new VRF is checked by caller.
504 zsend_interface_vrf_update (struct zserv
*client
, struct interface
*ifp
,
512 zserv_create_header (s
, ZEBRA_INTERFACE_VRF_UPDATE
, ifp
->vrf_id
);
514 /* Fill in the ifIndex of the interface and its new VRF (id) */
515 stream_putl (s
, ifp
->ifindex
);
516 stream_putw (s
, vrf_id
);
518 /* Write packet size. */
519 stream_putw_at (s
, 0, stream_get_endp (s
));
521 client
->if_vrfchg_cnt
++;
522 return zebra_server_send_message(client
);
525 /* Add new nbr connected IPv6 address */
527 nbr_connected_add_ipv6 (struct interface
*ifp
, struct in6_addr
*address
)
529 struct nbr_connected
*ifc
;
533 IPV6_ADDR_COPY (&p
.u
.prefix
, address
);
534 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
536 if (!(ifc
= listnode_head(ifp
->nbr_connected
)))
539 ifc
= nbr_connected_new ();
540 ifc
->address
= prefix_new();
542 listnode_add (ifp
->nbr_connected
, ifc
);
545 prefix_copy(ifc
->address
, &p
);
547 zebra_interface_nbr_address_add_update (ifp
, ifc
);
549 if_nbr_ipv6ll_to_ipv4ll_neigh_update (ifp
, address
, 1);
553 nbr_connected_delete_ipv6 (struct interface
*ifp
, struct in6_addr
*address
)
555 struct nbr_connected
*ifc
;
559 IPV6_ADDR_COPY (&p
.u
.prefix
, address
);
560 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
562 ifc
= nbr_connected_check(ifp
, &p
);
566 listnode_delete (ifp
->nbr_connected
, ifc
);
568 zebra_interface_nbr_address_delete_update (ifp
, ifc
);
570 if_nbr_ipv6ll_to_ipv4ll_neigh_update (ifp
, address
, 0);
572 nbr_connected_free (ifc
);
576 * The cmd passed to zsend_interface_update may be ZEBRA_INTERFACE_UP or
577 * ZEBRA_INTERFACE_DOWN.
579 * The ZEBRA_INTERFACE_UP message is sent from the zebra server to
580 * the clients in one of 2 situations:
581 * - an if_up is detected e.g., as a result of an RTM_IFINFO message
582 * - a vty command modifying the bandwidth of an interface is received.
583 * The ZEBRA_INTERFACE_DOWN message is sent when an if_down is detected.
586 zsend_interface_update (int cmd
, struct zserv
*client
, struct interface
*ifp
)
593 zserv_create_header (s
, cmd
, ifp
->vrf_id
);
594 zserv_encode_interface (s
, ifp
);
596 if (cmd
== ZEBRA_INTERFACE_UP
)
599 client
->ifdown_cnt
++;
601 return zebra_server_send_message(client
);
605 * This is the new function to announce and withdraw redistributed routes, used
606 * by Zebra. This is the old zsend_route_multipath() function. That function
607 * was duplicating code to send a lot of information that was essentially thrown
608 * away or ignored by the receiver. This is the leaner function that is not a
609 * duplicate of the zapi_ipv4_route_add/del.
611 * The primary difference is that this function merely sends a single NH instead of
615 zsend_redistribute_route (int add
, struct zserv
*client
, struct prefix
*p
,
616 struct prefix
*src_p
, struct route_entry
*re
)
622 struct nexthop
*nexthop
;
623 unsigned long nhnummark
= 0, messmark
= 0;
625 u_char zapi_flags
= 0;
626 struct nexthop dummy_nh
;
628 afi
= family2afi (p
->family
);
634 cmd
= ZEBRA_REDISTRIBUTE_IPV4_ADD
;
635 client
->redist_v4_add_cnt
++;
638 cmd
= ZEBRA_REDISTRIBUTE_IPV6_ADD
;
639 client
->redist_v6_add_cnt
++;
650 cmd
= ZEBRA_REDISTRIBUTE_IPV4_DEL
;
651 client
->redist_v4_del_cnt
++;
654 cmd
= ZEBRA_REDISTRIBUTE_IPV6_DEL
;
655 client
->redist_v6_del_cnt
++;
664 memset(&dummy_nh
, 0, sizeof(struct nexthop
));
666 zserv_create_header (s
, cmd
, re
->vrf_id
);
668 /* Put type and nexthop. */
669 stream_putc (s
, re
->type
);
670 stream_putw (s
, re
->instance
);
671 stream_putl (s
, re
->flags
);
673 /* marker for message flags field */
674 messmark
= stream_get_endp (s
);
678 psize
= PSIZE (p
->prefixlen
);
679 stream_putc (s
, p
->prefixlen
);
680 stream_write (s
, (u_char
*) & p
->u
.prefix
, psize
);
684 SET_FLAG (zapi_flags
, ZAPI_MESSAGE_SRCPFX
);
685 psize
= PSIZE (src_p
->prefixlen
);
686 stream_putc (s
, src_p
->prefixlen
);
687 stream_write (s
, (u_char
*) & src_p
->u
.prefix
, psize
);
690 for (nexthop
= re
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
692 /* We don't send any nexthops when there's a multipath */
693 if (re
->nexthop_active_num
> 1 && client
->proto
!= ZEBRA_ROUTE_LDP
)
695 SET_FLAG (zapi_flags
, ZAPI_MESSAGE_NEXTHOP
);
696 SET_FLAG (zapi_flags
, ZAPI_MESSAGE_IFINDEX
);
699 if (p
->family
== AF_INET
)
701 stream_put_in_addr (s
, &dummy_nh
.gate
.ipv4
);
703 else if (p
->family
== AF_INET6
)
705 stream_write (s
, (u_char
*) &dummy_nh
.gate
.ipv6
, 16);
709 /* We don't handle anything else now, abort */
710 zlog_err("%s: Unable to redistribute route of unknown family, %d\n",
711 __func__
, p
->family
);
715 stream_putl (s
, 0); /* dummy ifindex */
719 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
721 SET_FLAG (zapi_flags
, ZAPI_MESSAGE_NEXTHOP
);
722 SET_FLAG (zapi_flags
, ZAPI_MESSAGE_IFINDEX
);
725 nhnummark
= stream_get_endp (s
);
726 stream_putc (s
, 1); /* placeholder */
730 switch(nexthop
->type
)
732 case NEXTHOP_TYPE_IPV4
:
733 case NEXTHOP_TYPE_IPV4_IFINDEX
:
734 stream_put_in_addr (s
, &nexthop
->gate
.ipv4
);
736 case NEXTHOP_TYPE_IPV6
:
737 case NEXTHOP_TYPE_IPV6_IFINDEX
:
738 /* Only BGP supports IPv4 prefix with IPv6 NH, so kill this */
739 if (p
->family
== AF_INET
)
740 stream_put_in_addr(s
, &dummy_nh
.gate
.ipv4
);
742 stream_write (s
, (u_char
*) &nexthop
->gate
.ipv6
, 16);
745 if (cmd
== ZEBRA_REDISTRIBUTE_IPV4_ADD
746 || cmd
== ZEBRA_REDISTRIBUTE_IPV4_DEL
)
748 struct in_addr empty
;
749 memset (&empty
, 0, sizeof (struct in_addr
));
750 stream_write (s
, (u_char
*) &empty
, IPV4_MAX_BYTELEN
);
754 struct in6_addr empty
;
755 memset (&empty
, 0, sizeof (struct in6_addr
));
756 stream_write (s
, (u_char
*) &empty
, IPV6_MAX_BYTELEN
);
760 /* Interface index. */
762 stream_putl (s
, nexthop
->ifindex
);
764 /* ldpd needs all nexthops */
765 if (client
->proto
!= ZEBRA_ROUTE_LDP
)
771 SET_FLAG (zapi_flags
, ZAPI_MESSAGE_DISTANCE
);
772 stream_putc (s
, re
->distance
);
775 SET_FLAG (zapi_flags
, ZAPI_MESSAGE_METRIC
);
776 stream_putl (s
, re
->metric
);
781 SET_FLAG(zapi_flags
, ZAPI_MESSAGE_TAG
);
782 stream_putl(s
, re
->tag
);
786 SET_FLAG (zapi_flags
, ZAPI_MESSAGE_MTU
);
787 stream_putl (s
, re
->mtu
);
789 /* write real message flags value */
790 stream_putc_at (s
, messmark
, zapi_flags
);
792 /* Write next-hop number */
794 stream_putc_at (s
, nhnummark
, nhnum
);
796 /* Write packet size. */
797 stream_putw_at (s
, 0, stream_get_endp (s
));
799 return zebra_server_send_message(client
);
803 zsend_write_nexthop (struct stream
*s
, struct nexthop
*nexthop
)
805 stream_putc (s
, nexthop
->type
);
806 switch (nexthop
->type
)
808 case NEXTHOP_TYPE_IPV4
:
809 case NEXTHOP_TYPE_IPV4_IFINDEX
:
810 stream_put_in_addr (s
, &nexthop
->gate
.ipv4
);
811 stream_putl (s
, nexthop
->ifindex
);
813 case NEXTHOP_TYPE_IPV6
:
814 stream_put (s
, &nexthop
->gate
.ipv6
, 16);
816 case NEXTHOP_TYPE_IPV6_IFINDEX
:
817 stream_put (s
, &nexthop
->gate
.ipv6
, 16);
818 stream_putl (s
, nexthop
->ifindex
);
820 case NEXTHOP_TYPE_IFINDEX
:
821 stream_putl (s
, nexthop
->ifindex
);
830 /* Nexthop register */
832 zserv_rnh_register (struct zserv
*client
, int sock
, u_short length
,
833 rnh_type_t type
, struct zebra_vrf
*zvrf
)
841 if (IS_ZEBRA_DEBUG_NHT
)
842 zlog_debug("rnh_register msg from client %s: length=%d, type=%s\n",
843 zebra_route_string(client
->proto
), length
,
844 (type
== RNH_NEXTHOP_TYPE
) ? "nexthop" : "route");
848 client
->nh_reg_time
= monotime(NULL
);
852 flags
= stream_getc(s
);
853 p
.family
= stream_getw(s
);
854 p
.prefixlen
= stream_getc(s
);
856 if (p
.family
== AF_INET
)
858 p
.u
.prefix4
.s_addr
= stream_get_ipv4(s
);
859 l
+= IPV4_MAX_BYTELEN
;
861 else if (p
.family
== AF_INET6
)
863 stream_get(&p
.u
.prefix6
, s
, IPV6_MAX_BYTELEN
);
864 l
+= IPV6_MAX_BYTELEN
;
868 zlog_err("rnh_register: Received unknown family type %d\n",
872 rnh
= zebra_add_rnh(&p
, zvrf_id (zvrf
), type
);
873 if (type
== RNH_NEXTHOP_TYPE
)
875 if (flags
&& !CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
))
876 SET_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
);
877 else if (!flags
&& CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
))
878 UNSET_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
);
880 else if (type
== RNH_IMPORT_CHECK_TYPE
)
882 if (flags
&& !CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
))
883 SET_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
);
884 else if (!flags
&& CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
))
885 UNSET_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
);
888 zebra_add_rnh_client(rnh
, client
, type
, zvrf_id (zvrf
));
889 /* Anything not AF_INET/INET6 has been filtered out above */
890 zebra_evaluate_rnh(zvrf_id (zvrf
), p
.family
, 1, type
, &p
);
895 /* Nexthop register */
897 zserv_rnh_unregister (struct zserv
*client
, int sock
, u_short length
,
898 rnh_type_t type
, struct zebra_vrf
*zvrf
)
905 if (IS_ZEBRA_DEBUG_NHT
)
906 zlog_debug("rnh_unregister msg from client %s: length=%d\n",
907 zebra_route_string(client
->proto
), length
);
913 (void)stream_getc(s
); //Connected or not. Not used in this function
914 p
.family
= stream_getw(s
);
915 p
.prefixlen
= stream_getc(s
);
917 if (p
.family
== AF_INET
)
919 p
.u
.prefix4
.s_addr
= stream_get_ipv4(s
);
920 l
+= IPV4_MAX_BYTELEN
;
922 else if (p
.family
== AF_INET6
)
924 stream_get(&p
.u
.prefix6
, s
, IPV6_MAX_BYTELEN
);
925 l
+= IPV6_MAX_BYTELEN
;
929 zlog_err("rnh_register: Received unknown family type %d\n",
933 rnh
= zebra_lookup_rnh(&p
, zvrf_id (zvrf
), type
);
936 client
->nh_dereg_time
= monotime(NULL
);
937 zebra_remove_rnh_client(rnh
, client
, type
);
943 #define ZEBRA_MIN_FEC_LENGTH 5
947 zserv_fec_register (struct zserv
*client
, int sock
, u_short length
)
950 struct zebra_vrf
*zvrf
;
954 u_int32_t label_index
= MPLS_INVALID_LABEL_INDEX
;
957 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
959 return 0; // unexpected
962 * The minimum amount of data that can be sent for one fec
965 if (length
< ZEBRA_MIN_FEC_LENGTH
)
967 zlog_err ("fec_register: Received a fec register of length %d, it is of insufficient size to properly decode",
974 flags
= stream_getw(s
);
975 p
.family
= stream_getw(s
);
976 if (p
.family
!= AF_INET
&&
977 p
.family
!= AF_INET6
)
979 zlog_err ("fec_register: Received unknown family type %d\n",
983 p
.prefixlen
= stream_getc(s
);
985 stream_get(&p
.u
.prefix
, s
, PSIZE(p
.prefixlen
));
986 l
+= PSIZE(p
.prefixlen
);
987 if (flags
& ZEBRA_FEC_REGISTER_LABEL_INDEX
)
989 label_index
= stream_getl(s
);
992 zebra_mpls_fec_register (zvrf
, &p
, label_index
, client
);
1000 zserv_fec_unregister (struct zserv
*client
, int sock
, u_short length
)
1003 struct zebra_vrf
*zvrf
;
1009 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1011 return 0; // unexpected
1014 * The minimum amount of data that can be sent for one
1015 * fec unregistration
1017 if (length
< ZEBRA_MIN_FEC_LENGTH
)
1019 zlog_err ("fec_unregister: Received a fec unregister of length %d, it is of insufficient size to properly decode",
1026 //flags = stream_getw(s);
1027 (void)stream_getw(s
);
1028 p
.family
= stream_getw(s
);
1029 if (p
.family
!= AF_INET
&&
1030 p
.family
!= AF_INET6
)
1032 zlog_err ("fec_unregister: Received unknown family type %d\n",
1036 p
.prefixlen
= stream_getc(s
);
1038 stream_get(&p
.u
.prefix
, s
, PSIZE(p
.prefixlen
));
1039 l
+= PSIZE(p
.prefixlen
);
1040 zebra_mpls_fec_unregister (zvrf
, &p
, client
);
1047 Modified version of zsend_ipv4_nexthop_lookup():
1048 Query unicast rib if nexthop is not found on mrib.
1049 Returns both route metric and protocol distance.
1052 zsend_ipv4_nexthop_lookup_mrib (struct zserv
*client
, struct in_addr addr
, struct route_entry
*re
, struct zebra_vrf
*zvrf
)
1057 struct nexthop
*nexthop
;
1059 /* Get output stream. */
1063 /* Fill in result. */
1064 zserv_create_header (s
, ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB
, zvrf_id (zvrf
));
1065 stream_put_in_addr (s
, &addr
);
1069 stream_putc (s
, re
->distance
);
1070 stream_putl (s
, re
->metric
);
1072 nump
= stream_get_endp(s
); /* remember position for nexthop_num */
1073 stream_putc (s
, 0); /* reserve room for nexthop_num */
1074 /* Only non-recursive routes are elegible to resolve the nexthop we
1075 * are looking up. Therefore, we will just iterate over the top
1076 * chain of nexthops. */
1077 for (nexthop
= re
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
1078 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
1079 num
+= zsend_write_nexthop (s
, nexthop
);
1081 stream_putc_at (s
, nump
, num
); /* store nexthop_num */
1085 stream_putc (s
, 0); /* distance */
1086 stream_putl (s
, 0); /* metric */
1087 stream_putc (s
, 0); /* nexthop_num */
1090 stream_putw_at (s
, 0, stream_get_endp (s
));
1092 return zebra_server_send_message(client
);
1095 /* Router-id is updated. Send ZEBRA_ROUTER_ID_ADD to client. */
1097 zsend_router_id_update (struct zserv
*client
, struct prefix
*p
,
1103 /* Check this client need interface information. */
1104 if (! vrf_bitmap_check (client
->ridinfo
, vrf_id
))
1111 zserv_create_header (s
, ZEBRA_ROUTER_ID_UPDATE
, vrf_id
);
1113 /* Prefix information. */
1114 stream_putc (s
, p
->family
);
1115 blen
= prefix_blen (p
);
1116 stream_put (s
, &p
->u
.prefix
, blen
);
1117 stream_putc (s
, p
->prefixlen
);
1119 /* Write packet size. */
1120 stream_putw_at (s
, 0, stream_get_endp (s
));
1122 return zebra_server_send_message(client
);
1125 /* Register zebra server interface information. Send current all
1126 interface and address information. */
1128 zread_interface_add (struct zserv
*client
, u_short length
, struct zebra_vrf
*zvrf
)
1131 struct listnode
*ifnode
, *ifnnode
;
1132 struct interface
*ifp
;
1134 /* Interface information is needed. */
1135 vrf_bitmap_set (client
->ifinfo
, zvrf_id (zvrf
));
1137 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
)
1139 for (ALL_LIST_ELEMENTS (vrf
->iflist
, ifnode
, ifnnode
, ifp
))
1141 /* Skip pseudo interface. */
1142 if (! CHECK_FLAG (ifp
->status
, ZEBRA_INTERFACE_ACTIVE
))
1145 if (zsend_interface_add (client
, ifp
) < 0)
1148 if (zsend_interface_addresses (client
, ifp
) < 0)
1155 /* Unregister zebra server interface information. */
1157 zread_interface_delete (struct zserv
*client
, u_short length
, struct zebra_vrf
*zvrf
)
1159 vrf_bitmap_unset (client
->ifinfo
, zvrf_id (zvrf
));
1164 zserv_nexthop_num_warn (const char *caller
, const struct prefix
*p
, const unsigned int nexthop_num
)
1166 if (nexthop_num
> multipath_num
)
1168 char buff
[PREFIX2STR_BUFFER
];
1169 prefix2str(p
, buff
, sizeof (buff
));
1170 zlog_warn("%s: Prefix %s has %d nexthops, but we can only use the first %d",
1171 caller
, buff
, nexthop_num
, multipath_num
);
1175 /* This function support multiple nexthop. */
1177 * Parse the ZEBRA_IPV4_ROUTE_ADD sent from client. Update re and
1181 zread_ipv4_add (struct zserv
*client
, u_short length
, struct zebra_vrf
*zvrf
)
1184 struct route_entry
*re
;
1187 struct in_addr nhop_addr
;
1189 u_char nexthop_type
;
1195 struct nexthop
*nexthop
;
1197 /* Get input stream. */
1200 /* Allocate new re. */
1201 re
= XCALLOC (MTYPE_RE
, sizeof (struct route_entry
));
1203 /* Type, flags, message. */
1204 re
->type
= stream_getc (s
);
1205 re
->instance
= stream_getw (s
);
1206 re
->flags
= stream_getl (s
);
1207 message
= stream_getc (s
);
1208 safi
= stream_getw (s
);
1209 re
->uptime
= time (NULL
);
1212 memset (&p
, 0, sizeof (struct prefix_ipv4
));
1214 p
.prefixlen
= stream_getc (s
);
1215 stream_get (&p
.u
.prefix4
, s
, PSIZE (p
.prefixlen
));
1218 re
->vrf_id
= zvrf_id (zvrf
);
1220 /* Nexthop parse. */
1221 if (CHECK_FLAG (message
, ZAPI_MESSAGE_NEXTHOP
))
1223 nexthop_num
= stream_getc (s
);
1224 zserv_nexthop_num_warn(__func__
, (const struct prefix
*)&p
, nexthop_num
);
1226 for (i
= 0; i
< nexthop_num
; i
++)
1228 nexthop_type
= stream_getc (s
);
1230 switch (nexthop_type
)
1232 case NEXTHOP_TYPE_IFINDEX
:
1233 ifindex
= stream_getl (s
);
1234 route_entry_nexthop_ifindex_add (re
, ifindex
);
1236 case NEXTHOP_TYPE_IPV4
:
1237 nhop_addr
.s_addr
= stream_get_ipv4 (s
);
1238 nexthop
= route_entry_nexthop_ipv4_add (re
, &nhop_addr
, NULL
);
1239 /* For labeled-unicast, each nexthop is followed by label. */
1240 if (CHECK_FLAG (message
, ZAPI_MESSAGE_LABEL
))
1242 label
= (mpls_label_t
)stream_getl (s
);
1243 nexthop_add_labels (nexthop
, nexthop
->nh_label_type
, 1, &label
);
1246 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1247 nhop_addr
.s_addr
= stream_get_ipv4 (s
);
1248 ifindex
= stream_getl (s
);
1249 route_entry_nexthop_ipv4_ifindex_add (re
, &nhop_addr
, NULL
, ifindex
);
1251 case NEXTHOP_TYPE_IPV6
:
1252 stream_forward_getp (s
, IPV6_MAX_BYTELEN
);
1254 case NEXTHOP_TYPE_BLACKHOLE
:
1255 route_entry_nexthop_blackhole_add (re
);
1262 if (CHECK_FLAG (message
, ZAPI_MESSAGE_DISTANCE
))
1263 re
->distance
= stream_getc (s
);
1266 if (CHECK_FLAG (message
, ZAPI_MESSAGE_METRIC
))
1267 re
->metric
= stream_getl (s
);
1270 if (CHECK_FLAG (message
, ZAPI_MESSAGE_TAG
))
1271 re
->tag
= stream_getl (s
);
1275 if (CHECK_FLAG (message
, ZAPI_MESSAGE_MTU
))
1276 re
->mtu
= stream_getl (s
);
1281 re
->table
= zvrf
->table_id
;
1283 ret
= rib_add_multipath (AFI_IP
, safi
, &p
, NULL
, re
);
1287 client
->v4_route_add_cnt
++;
1289 client
->v4_route_upd8_cnt
++;
1293 /* Zebra server IPv4 prefix delete function. */
1295 zread_ipv4_delete (struct zserv
*client
, u_short length
, struct zebra_vrf
*zvrf
)
1299 struct zapi_ipv4 api
;
1300 struct in_addr nexthop
;
1301 union g_addr
*nexthop_p
;
1302 unsigned long ifindex
;
1305 u_char nexthop_type
;
1313 /* Type, flags, message. */
1314 api
.type
= stream_getc (s
);
1315 api
.instance
= stream_getw (s
);
1316 api
.flags
= stream_getl (s
);
1317 api
.message
= stream_getc (s
);
1318 api
.safi
= stream_getw (s
);
1321 memset (&p
, 0, sizeof (struct prefix
));
1323 p
.prefixlen
= stream_getc (s
);
1324 stream_get (&p
.u
.prefix4
, s
, PSIZE (p
.prefixlen
));
1326 /* Nexthop, ifindex, distance, metric. */
1327 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
))
1329 nexthop_num
= stream_getc (s
);
1331 for (i
= 0; i
< nexthop_num
; i
++)
1333 nexthop_type
= stream_getc (s
);
1335 switch (nexthop_type
)
1337 case NEXTHOP_TYPE_IFINDEX
:
1338 ifindex
= stream_getl (s
);
1340 case NEXTHOP_TYPE_IPV4
:
1341 nexthop
.s_addr
= stream_get_ipv4 (s
);
1342 /* For labeled-unicast, each nexthop is followed by label, but
1343 * we don't care for delete.
1345 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_LABEL
))
1346 stream_forward_getp (s
, sizeof(u_int32_t
));
1347 nexthop_p
= (union g_addr
*)&nexthop
;
1349 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1350 nexthop
.s_addr
= stream_get_ipv4 (s
);
1351 nexthop_p
= (union g_addr
*)&nexthop
;
1352 ifindex
= stream_getl (s
);
1354 case NEXTHOP_TYPE_IPV6
:
1355 stream_forward_getp (s
, IPV6_MAX_BYTELEN
);
1362 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_DISTANCE
))
1363 api
.distance
= stream_getc (s
);
1368 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_METRIC
))
1369 api
.metric
= stream_getl (s
);
1374 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_TAG
))
1375 api
.tag
= stream_getl (s
);
1379 table_id
= zvrf
->table_id
;
1381 rib_delete (AFI_IP
, api
.safi
, zvrf_id (zvrf
), api
.type
, api
.instance
,
1382 api
.flags
, &p
, NULL
, nexthop_p
, ifindex
, table_id
);
1383 client
->v4_route_del_cnt
++;
1387 /* MRIB Nexthop lookup for IPv4. */
1389 zread_ipv4_nexthop_lookup_mrib (struct zserv
*client
, u_short length
, struct zebra_vrf
*zvrf
)
1391 struct in_addr addr
;
1392 struct route_entry
*re
;
1394 addr
.s_addr
= stream_get_ipv4 (client
->ibuf
);
1395 re
= rib_match_ipv4_multicast (zvrf_id (zvrf
), addr
, NULL
);
1396 return zsend_ipv4_nexthop_lookup_mrib (client
, addr
, re
, zvrf
);
1399 /* Zebra server IPv6 prefix add function. */
1401 zread_ipv4_route_ipv6_nexthop_add (struct zserv
*client
, u_short length
, struct zebra_vrf
*zvrf
)
1405 struct in6_addr nhop_addr
;
1406 struct route_entry
*re
;
1409 u_char nexthop_type
;
1412 static struct in6_addr nexthops
[MULTIPATH_NUM
];
1413 static unsigned int ifindices
[MULTIPATH_NUM
];
1415 static mpls_label_t labels
[MULTIPATH_NUM
];
1417 struct nexthop
*nexthop
;
1419 /* Get input stream. */
1422 memset (&nhop_addr
, 0, sizeof (struct in6_addr
));
1424 /* Allocate new re. */
1425 re
= XCALLOC (MTYPE_RE
, sizeof (struct route_entry
));
1427 /* Type, flags, message. */
1428 re
->type
= stream_getc (s
);
1429 re
->instance
= stream_getw (s
);
1430 re
->flags
= stream_getl (s
);
1431 message
= stream_getc (s
);
1432 safi
= stream_getw (s
);
1433 re
->uptime
= time (NULL
);
1436 memset (&p
, 0, sizeof (struct prefix_ipv4
));
1438 p
.prefixlen
= stream_getc (s
);
1439 stream_get (&p
.u
.prefix4
, s
, PSIZE (p
.prefixlen
));
1442 re
->vrf_id
= zvrf_id (zvrf
);
1444 /* We need to give nh-addr, nh-ifindex with the same next-hop object
1445 * to the re to ensure that IPv6 multipathing works; need to coalesce
1446 * these. Clients should send the same number of paired set of
1447 * next-hop-addr/next-hop-ifindices. */
1448 if (CHECK_FLAG (message
, ZAPI_MESSAGE_NEXTHOP
))
1450 unsigned int nh_count
= 0;
1451 unsigned int if_count
= 0;
1452 unsigned int max_nh_if
= 0;
1454 nexthop_num
= stream_getc (s
);
1455 zserv_nexthop_num_warn(__func__
, (const struct prefix
*)&p
, nexthop_num
);
1456 for (i
= 0; i
< nexthop_num
; i
++)
1458 nexthop_type
= stream_getc (s
);
1460 switch (nexthop_type
)
1462 case NEXTHOP_TYPE_IPV6
:
1463 stream_get (&nhop_addr
, s
, 16);
1464 if (nh_count
< MULTIPATH_NUM
)
1466 /* For labeled-unicast, each nexthop is followed by label. */
1467 if (CHECK_FLAG (message
, ZAPI_MESSAGE_LABEL
))
1469 label
= (mpls_label_t
)stream_getl (s
);
1470 labels
[nh_count
] = label
;
1472 nexthops
[nh_count
] = nhop_addr
;
1476 case NEXTHOP_TYPE_IFINDEX
:
1477 if (if_count
< multipath_num
) {
1478 ifindices
[if_count
++] = stream_getl (s
);
1481 case NEXTHOP_TYPE_BLACKHOLE
:
1482 route_entry_nexthop_blackhole_add (re
);
1487 max_nh_if
= (nh_count
> if_count
) ? nh_count
: if_count
;
1488 for (i
= 0; i
< max_nh_if
; i
++)
1490 if ((i
< nh_count
) && !IN6_IS_ADDR_UNSPECIFIED (&nexthops
[i
])) {
1491 if ((i
< if_count
) && ifindices
[i
])
1492 nexthop
= route_entry_nexthop_ipv6_ifindex_add (re
, &nexthops
[i
], ifindices
[i
]);
1494 nexthop
= route_entry_nexthop_ipv6_add (re
, &nexthops
[i
]);
1496 if (CHECK_FLAG (message
, ZAPI_MESSAGE_LABEL
))
1497 nexthop_add_labels (nexthop
, nexthop
->nh_label_type
, 1, &labels
[i
]);
1500 if ((i
< if_count
) && ifindices
[i
])
1501 route_entry_nexthop_ifindex_add (re
, ifindices
[i
]);
1507 if (CHECK_FLAG (message
, ZAPI_MESSAGE_DISTANCE
))
1508 re
->distance
= stream_getc (s
);
1511 if (CHECK_FLAG (message
, ZAPI_MESSAGE_METRIC
))
1512 re
->metric
= stream_getl (s
);
1515 if (CHECK_FLAG (message
, ZAPI_MESSAGE_TAG
))
1516 re
->tag
= stream_getl (s
);
1520 if (CHECK_FLAG (message
, ZAPI_MESSAGE_MTU
))
1521 re
->mtu
= stream_getl (s
);
1526 re
->table
= zvrf
->table_id
;
1528 ret
= rib_add_multipath (AFI_IP6
, safi
, &p
, NULL
, re
);
1531 client
->v4_route_add_cnt
++;
1533 client
->v4_route_upd8_cnt
++;
1539 zread_ipv6_add (struct zserv
*client
, u_short length
, struct zebra_vrf
*zvrf
)
1543 struct in6_addr nhop_addr
;
1544 struct route_entry
*re
;
1547 u_char nexthop_type
;
1549 struct prefix_ipv6 src_p
, *src_pp
;
1551 static struct in6_addr nexthops
[MULTIPATH_NUM
];
1552 static unsigned int ifindices
[MULTIPATH_NUM
];
1554 static mpls_label_t labels
[MULTIPATH_NUM
];
1556 struct nexthop
*nexthop
;
1558 /* Get input stream. */
1561 memset (&nhop_addr
, 0, sizeof (struct in6_addr
));
1563 /* Allocate new re. */
1564 re
= XCALLOC (MTYPE_RE
, sizeof (struct route_entry
));
1566 /* Type, flags, message. */
1567 re
->type
= stream_getc (s
);
1568 re
->instance
= stream_getw (s
);
1569 re
->flags
= stream_getl (s
);
1570 message
= stream_getc (s
);
1571 safi
= stream_getw (s
);
1572 re
->uptime
= time (NULL
);
1575 memset (&p
, 0, sizeof (struct prefix_ipv6
));
1576 p
.family
= AF_INET6
;
1577 p
.prefixlen
= stream_getc (s
);
1578 stream_get (&p
.u
.prefix6
, s
, PSIZE (p
.prefixlen
));
1580 if (CHECK_FLAG (message
, ZAPI_MESSAGE_SRCPFX
))
1582 memset (&src_p
, 0, sizeof (struct prefix_ipv6
));
1583 src_p
.family
= AF_INET6
;
1584 src_p
.prefixlen
= stream_getc (s
);
1585 stream_get (&src_p
.prefix
, s
, PSIZE (src_p
.prefixlen
));
1591 /* We need to give nh-addr, nh-ifindex with the same next-hop object
1592 * to the re to ensure that IPv6 multipathing works; need to coalesce
1593 * these. Clients should send the same number of paired set of
1594 * next-hop-addr/next-hop-ifindices. */
1595 if (CHECK_FLAG (message
, ZAPI_MESSAGE_NEXTHOP
))
1597 unsigned int nh_count
= 0;
1598 unsigned int if_count
= 0;
1599 unsigned int max_nh_if
= 0;
1601 nexthop_num
= stream_getc (s
);
1602 zserv_nexthop_num_warn(__func__
, (const struct prefix
*)&p
, nexthop_num
);
1603 for (i
= 0; i
< nexthop_num
; i
++)
1605 nexthop_type
= stream_getc (s
);
1607 switch (nexthop_type
)
1609 case NEXTHOP_TYPE_IPV6
:
1610 stream_get (&nhop_addr
, s
, 16);
1611 if (nh_count
< MULTIPATH_NUM
)
1613 /* For labeled-unicast, each nexthop is followed by label. */
1614 if (CHECK_FLAG (message
, ZAPI_MESSAGE_LABEL
))
1616 label
= (mpls_label_t
)stream_getl (s
);
1617 labels
[nh_count
] = label
;
1619 nexthops
[nh_count
++] = nhop_addr
;
1622 case NEXTHOP_TYPE_IFINDEX
:
1623 if (if_count
< multipath_num
) {
1624 ifindices
[if_count
++] = stream_getl (s
);
1627 case NEXTHOP_TYPE_BLACKHOLE
:
1628 route_entry_nexthop_blackhole_add (re
);
1633 max_nh_if
= (nh_count
> if_count
) ? nh_count
: if_count
;
1634 for (i
= 0; i
< max_nh_if
; i
++)
1636 if ((i
< nh_count
) && !IN6_IS_ADDR_UNSPECIFIED (&nexthops
[i
])) {
1637 if ((i
< if_count
) && ifindices
[i
])
1638 nexthop
= route_entry_nexthop_ipv6_ifindex_add (re
, &nexthops
[i
], ifindices
[i
]);
1640 nexthop
= route_entry_nexthop_ipv6_add (re
, &nexthops
[i
]);
1641 if (CHECK_FLAG (message
, ZAPI_MESSAGE_LABEL
))
1642 nexthop_add_labels (nexthop
, nexthop
->nh_label_type
, 1, &labels
[i
]);
1645 if ((i
< if_count
) && ifindices
[i
])
1646 route_entry_nexthop_ifindex_add (re
, ifindices
[i
]);
1652 if (CHECK_FLAG (message
, ZAPI_MESSAGE_DISTANCE
))
1653 re
->distance
= stream_getc (s
);
1656 if (CHECK_FLAG (message
, ZAPI_MESSAGE_METRIC
))
1657 re
->metric
= stream_getl (s
);
1660 if (CHECK_FLAG (message
, ZAPI_MESSAGE_TAG
))
1661 re
->tag
= stream_getl (s
);
1665 if (CHECK_FLAG (message
, ZAPI_MESSAGE_MTU
))
1666 re
->mtu
= stream_getl (s
);
1671 re
->vrf_id
= zvrf_id (zvrf
);
1672 re
->table
= zvrf
->table_id
;
1674 ret
= rib_add_multipath (AFI_IP6
, safi
, &p
, src_pp
, re
);
1677 client
->v6_route_add_cnt
++;
1679 client
->v6_route_upd8_cnt
++;
1684 /* Zebra server IPv6 prefix delete function. */
1686 zread_ipv6_delete (struct zserv
*client
, u_short length
, struct zebra_vrf
*zvrf
)
1690 struct zapi_ipv6 api
;
1691 struct in6_addr nexthop
;
1692 union g_addr
*pnexthop
= NULL
;
1693 unsigned long ifindex
;
1695 struct prefix_ipv6 src_p
, *src_pp
;
1699 memset (&nexthop
, 0, sizeof (struct in6_addr
));
1701 /* Type, flags, message. */
1702 api
.type
= stream_getc (s
);
1703 api
.instance
= stream_getw (s
);
1704 api
.flags
= stream_getl (s
);
1705 api
.message
= stream_getc (s
);
1706 api
.safi
= stream_getw (s
);
1709 memset (&p
, 0, sizeof (struct prefix_ipv6
));
1710 p
.family
= AF_INET6
;
1711 p
.prefixlen
= stream_getc (s
);
1712 stream_get (&p
.u
.prefix6
, s
, PSIZE (p
.prefixlen
));
1714 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_SRCPFX
))
1716 memset (&src_p
, 0, sizeof (struct prefix_ipv6
));
1717 src_p
.family
= AF_INET6
;
1718 src_p
.prefixlen
= stream_getc (s
);
1719 stream_get (&src_p
.prefix
, s
, PSIZE (src_p
.prefixlen
));
1725 /* Nexthop, ifindex, distance, metric. */
1726 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
))
1728 u_char nexthop_type
;
1730 api
.nexthop_num
= stream_getc (s
);
1731 for (i
= 0; i
< api
.nexthop_num
; i
++)
1733 nexthop_type
= stream_getc (s
);
1735 switch (nexthop_type
)
1737 case NEXTHOP_TYPE_IPV6
:
1738 stream_get (&nexthop
, s
, 16);
1739 /* For labeled-unicast, each nexthop is followed by label, but
1740 * we don't care for delete.
1742 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_LABEL
))
1743 stream_forward_getp (s
, sizeof(u_int32_t
));
1744 pnexthop
= (union g_addr
*)&nexthop
;
1746 case NEXTHOP_TYPE_IFINDEX
:
1747 ifindex
= stream_getl (s
);
1754 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_DISTANCE
))
1755 api
.distance
= stream_getc (s
);
1760 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_METRIC
))
1761 api
.metric
= stream_getl (s
);
1766 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_TAG
))
1767 api
.tag
= stream_getl (s
);
1771 if (IN6_IS_ADDR_UNSPECIFIED (&nexthop
))
1772 rib_delete (AFI_IP6
, api
.safi
, zvrf_id (zvrf
), api
.type
, api
.instance
,
1773 api
.flags
, &p
, src_pp
, NULL
, ifindex
, client
->rtm_table
);
1775 rib_delete (AFI_IP6
, api
.safi
, zvrf_id (zvrf
), api
.type
, api
.instance
,
1776 api
.flags
, &p
, src_pp
, pnexthop
, ifindex
, client
->rtm_table
);
1778 client
->v6_route_del_cnt
++;
1782 /* Register zebra server router-id information. Send current router-id */
1784 zread_router_id_add (struct zserv
*client
, u_short length
, struct zebra_vrf
*zvrf
)
1788 /* Router-id information is needed. */
1789 vrf_bitmap_set (client
->ridinfo
, zvrf_id (zvrf
));
1791 router_id_get (&p
, zvrf_id (zvrf
));
1793 return zsend_router_id_update (client
, &p
, zvrf_id (zvrf
));
1796 /* Unregister zebra server router-id information. */
1798 zread_router_id_delete (struct zserv
*client
, u_short length
, struct zebra_vrf
*zvrf
)
1800 vrf_bitmap_unset (client
->ridinfo
, zvrf_id (zvrf
));
1804 /* Tie up route-type and client->sock */
1806 zread_hello (struct zserv
*client
)
1808 /* type of protocol (lib/zebra.h) */
1812 proto
= stream_getc (client
->ibuf
);
1813 instance
= stream_getw (client
->ibuf
);
1815 /* accept only dynamic routing protocols */
1816 if ((proto
< ZEBRA_ROUTE_MAX
)
1817 && (proto
> ZEBRA_ROUTE_STATIC
))
1819 zlog_notice ("client %d says hello and bids fair to announce only %s routes",
1820 client
->sock
, zebra_route_string(proto
));
1822 zlog_notice ("client protocol instance %d", instance
);
1824 client
->proto
= proto
;
1825 client
->instance
= instance
;
1829 /* Unregister all information in a VRF. */
1831 zread_vrf_unregister (struct zserv
*client
, u_short length
, struct zebra_vrf
*zvrf
)
1836 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
1837 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
1838 vrf_bitmap_unset (client
->redist
[afi
][i
], zvrf_id (zvrf
));
1839 vrf_bitmap_unset (client
->redist_default
, zvrf_id (zvrf
));
1840 vrf_bitmap_unset (client
->ifinfo
, zvrf_id (zvrf
));
1841 vrf_bitmap_unset (client
->ridinfo
, zvrf_id (zvrf
));
1847 zread_mpls_labels (int command
, struct zserv
*client
, u_short length
,
1851 enum lsp_types_t type
;
1852 struct prefix prefix
;
1853 enum nexthop_types_t gtype
;
1856 mpls_label_t in_label
, out_label
;
1858 struct zebra_vrf
*zvrf
;
1860 zvrf
= vrf_info_lookup (vrf_id
);
1864 /* Get input stream. */
1868 type
= stream_getc (s
);
1869 prefix
.family
= stream_getl (s
);
1870 switch (prefix
.family
)
1873 prefix
.u
.prefix4
.s_addr
= stream_get_ipv4 (s
);
1874 prefix
.prefixlen
= stream_getc (s
);
1875 gate
.ipv4
.s_addr
= stream_get_ipv4 (s
);
1878 stream_get (&prefix
.u
.prefix6
, s
, 16);
1879 prefix
.prefixlen
= stream_getc (s
);
1880 stream_get (&gate
.ipv6
, s
, 16);
1885 ifindex
= stream_getl (s
);
1886 distance
= stream_getc (s
);
1887 in_label
= stream_getl (s
);
1888 out_label
= stream_getl (s
);
1890 switch (prefix
.family
)
1894 gtype
= NEXTHOP_TYPE_IPV4_IFINDEX
;
1896 gtype
= NEXTHOP_TYPE_IPV4
;
1900 gtype
= NEXTHOP_TYPE_IPV6_IFINDEX
;
1902 gtype
= NEXTHOP_TYPE_IPV6
;
1911 if (command
== ZEBRA_MPLS_LABELS_ADD
)
1913 mpls_lsp_install (zvrf
, type
, in_label
, out_label
, gtype
, &gate
,
1915 if (out_label
!= MPLS_IMP_NULL_LABEL
)
1916 mpls_ftn_update (1, zvrf
, type
, &prefix
, gtype
, &gate
, ifindex
,
1917 distance
, out_label
);
1919 else if (command
== ZEBRA_MPLS_LABELS_DELETE
)
1921 mpls_lsp_uninstall (zvrf
, type
, in_label
, gtype
, &gate
, ifindex
);
1922 if (out_label
!= MPLS_IMP_NULL_LABEL
)
1923 mpls_ftn_update (0, zvrf
, type
, &prefix
, gtype
, &gate
, ifindex
,
1924 distance
, out_label
);
1927 /* Send response to a label manager connect request to client */
1929 zsend_label_manager_connect_response (struct zserv
*client
, vrf_id_t vrf_id
, u_short result
)
1936 zserv_create_header (s
, ZEBRA_LABEL_MANAGER_CONNECT
, vrf_id
);
1939 stream_putc (s
, result
);
1941 /* Write packet size. */
1942 stream_putw_at (s
, 0, stream_get_endp (s
));
1944 return writen (client
->sock
, s
->data
, stream_get_endp (s
));
1948 zread_label_manager_connect (struct zserv
*client
, vrf_id_t vrf_id
)
1951 /* type of protocol (lib/zebra.h) */
1955 /* Get input stream. */
1959 proto
= stream_getc (s
);
1960 instance
= stream_getw (s
);
1962 /* accept only dynamic routing protocols */
1963 if ((proto
>= ZEBRA_ROUTE_MAX
)
1964 || (proto
<= ZEBRA_ROUTE_STATIC
))
1966 zlog_err ("client %d has wrong protocol %s",
1967 client
->sock
, zebra_route_string(proto
));
1968 zsend_label_manager_connect_response (client
, vrf_id
, 1);
1971 zlog_notice ("client %d with instance %u connected as %s",
1972 client
->sock
, instance
, zebra_route_string(proto
));
1973 client
->proto
= proto
;
1974 client
->instance
= instance
;
1977 Release previous labels of same protocol and instance.
1978 This is done in case it restarted from an unexpected shutdown.
1980 release_daemon_chunks (proto
, instance
);
1982 zlog_debug (" Label Manager client connected: sock %d, proto %s, instance %u",
1983 client
->sock
, zebra_route_string(proto
), instance
);
1984 /* send response back */
1985 zsend_label_manager_connect_response (client
, vrf_id
, 0);
1987 /* Send response to a get label chunk request to client */
1989 zsend_assign_label_chunk_response (struct zserv
*client
, vrf_id_t vrf_id
,
1990 struct label_manager_chunk
*lmc
)
1997 zserv_create_header (s
, ZEBRA_GET_LABEL_CHUNK
, vrf_id
);
2002 stream_putc (s
, lmc
->keep
);
2003 /* start and end labels */
2004 stream_putl (s
, lmc
->start
);
2005 stream_putl (s
, lmc
->end
);
2009 /* Write packet size. */
2010 stream_putw_at (s
, 0, stream_get_endp (s
));
2012 return writen (client
->sock
, s
->data
, stream_get_endp (s
));
2016 zread_get_label_chunk (struct zserv
*client
, vrf_id_t vrf_id
)
2021 struct label_manager_chunk
*lmc
;
2023 /* Get input stream. */
2027 keep
= stream_getc (s
);
2028 size
= stream_getl (s
);
2030 lmc
= assign_label_chunk (client
->proto
, client
->instance
, keep
, size
);
2032 zlog_err ("%s: Unable to assign Label Chunk of size %u", __func__
, size
);
2034 zlog_debug ("Assigned Label Chunk %u - %u to %u",
2035 lmc
->start
, lmc
->end
, keep
);
2036 /* send response back */
2037 zsend_assign_label_chunk_response (client
, vrf_id
, lmc
);
2041 zread_release_label_chunk (struct zserv
*client
)
2044 uint32_t start
, end
;
2046 /* Get input stream. */
2050 start
= stream_getl (s
);
2051 end
= stream_getl (s
);
2053 release_label_chunk (client
->proto
, client
->instance
, start
, end
);
2056 zread_label_manager_request (int cmd
, struct zserv
*client
, vrf_id_t vrf_id
)
2058 /* to avoid sending other messages like ZERBA_INTERFACE_UP */
2059 if (cmd
== ZEBRA_LABEL_MANAGER_CONNECT
)
2060 client
->is_synchronous
= 1;
2062 /* external label manager */
2064 zread_relay_label_manager_request (cmd
, client
, vrf_id
);
2065 /* this is a label manager */
2068 if (cmd
== ZEBRA_LABEL_MANAGER_CONNECT
)
2069 zread_label_manager_connect (client
, vrf_id
);
2072 /* Sanity: don't allow 'unidentified' requests */
2075 zlog_err ("Got label request from an unidentified client");
2078 if (cmd
== ZEBRA_GET_LABEL_CHUNK
)
2079 zread_get_label_chunk (client
, vrf_id
);
2080 else if (cmd
== ZEBRA_RELEASE_LABEL_CHUNK
)
2081 zread_release_label_chunk (client
);
2086 /* Cleanup registered nexthops (across VRFs) upon client disconnect. */
2088 zebra_client_close_cleanup_rnh (struct zserv
*client
)
2091 struct zebra_vrf
*zvrf
;
2093 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
)
2095 if ((zvrf
= vrf
->info
) != NULL
)
2097 zebra_cleanup_rnh_client(zvrf_id (zvrf
), AF_INET
, client
, RNH_NEXTHOP_TYPE
);
2098 zebra_cleanup_rnh_client(zvrf_id (zvrf
), AF_INET6
, client
, RNH_NEXTHOP_TYPE
);
2099 zebra_cleanup_rnh_client(zvrf_id (zvrf
), AF_INET
, client
, RNH_IMPORT_CHECK_TYPE
);
2100 zebra_cleanup_rnh_client(zvrf_id (zvrf
), AF_INET6
, client
, RNH_IMPORT_CHECK_TYPE
);
2101 if (client
->proto
== ZEBRA_ROUTE_LDP
)
2103 hash_iterate(zvrf
->lsp_table
, mpls_ldp_lsp_uninstall_all
,
2105 mpls_ldp_ftn_uninstall_all (zvrf
, AFI_IP
);
2106 mpls_ldp_ftn_uninstall_all (zvrf
, AFI_IP6
);
2112 /* Close zebra client. */
2114 zebra_client_close (struct zserv
*client
)
2116 /* Send client de-registration to BFD */
2117 zebra_ptm_bfd_client_deregister(client
->proto
);
2119 /* Cleanup any registered nexthops - across all VRFs. */
2120 zebra_client_close_cleanup_rnh (client
);
2122 /* Release Label Manager chunks */
2123 release_daemon_chunks (client
->proto
, client
->instance
);
2125 /* Cleanup any FECs registered by this client. */
2126 zebra_mpls_cleanup_fecs_for_client (vrf_info_lookup(VRF_DEFAULT
), client
);
2128 /* Close file descriptor. */
2131 unsigned long nroutes
;
2133 close (client
->sock
);
2134 nroutes
= rib_score_proto (client
->proto
, client
->instance
);
2135 zlog_notice ("client %d disconnected. %lu %s routes removed from the rib",
2136 client
->sock
, nroutes
, zebra_route_string (client
->proto
));
2140 /* Free stream buffers. */
2142 stream_free (client
->ibuf
);
2144 stream_free (client
->obuf
);
2146 buffer_free(client
->wb
);
2148 /* Release threads. */
2150 thread_cancel (client
->t_read
);
2151 if (client
->t_write
)
2152 thread_cancel (client
->t_write
);
2153 if (client
->t_suicide
)
2154 thread_cancel (client
->t_suicide
);
2156 /* Free client structure. */
2157 listnode_delete (zebrad
.client_list
, client
);
2158 XFREE (MTYPE_TMP
, client
);
2161 /* Make new client. */
2163 zebra_client_create (int sock
)
2165 struct zserv
*client
;
2169 client
= XCALLOC (MTYPE_TMP
, sizeof (struct zserv
));
2171 /* Make client input/output buffer. */
2172 client
->sock
= sock
;
2173 client
->ibuf
= stream_new (ZEBRA_MAX_PACKET_SIZ
);
2174 client
->obuf
= stream_new (ZEBRA_MAX_PACKET_SIZ
);
2175 client
->wb
= buffer_new(0);
2177 /* Set table number. */
2178 client
->rtm_table
= zebrad
.rtm_table_default
;
2180 client
->connect_time
= monotime(NULL
);
2181 /* Initialize flags */
2182 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
2183 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
2184 client
->redist
[afi
][i
] = vrf_bitmap_init ();
2185 client
->redist_default
= vrf_bitmap_init ();
2186 client
->ifinfo
= vrf_bitmap_init ();
2187 client
->ridinfo
= vrf_bitmap_init ();
2189 /* by default, it's not a synchronous client */
2190 client
->is_synchronous
= 0;
2192 /* Add this client to linked list. */
2193 listnode_add (zebrad
.client_list
, client
);
2195 /* Make new read thread. */
2196 zebra_event (ZEBRA_READ
, sock
, client
);
2198 zebra_vrf_update_all (client
);
2201 /* Handler of zebra service request. */
2203 zebra_client_read (struct thread
*thread
)
2206 struct zserv
*client
;
2208 uint16_t length
, command
;
2209 uint8_t marker
, version
;
2211 struct zebra_vrf
*zvrf
;
2213 /* Get thread data. Reset reading thread because I'm running. */
2214 sock
= THREAD_FD (thread
);
2215 client
= THREAD_ARG (thread
);
2216 client
->t_read
= NULL
;
2218 if (client
->t_suicide
)
2220 zebra_client_close(client
);
2224 /* Read length and command (if we don't have it already). */
2225 if ((already
= stream_get_endp(client
->ibuf
)) < ZEBRA_HEADER_SIZE
)
2228 if (((nbyte
= stream_read_try (client
->ibuf
, sock
,
2229 ZEBRA_HEADER_SIZE
-already
)) == 0) ||
2232 if (IS_ZEBRA_DEBUG_EVENT
)
2233 zlog_debug ("connection closed socket [%d]", sock
);
2234 zebra_client_close (client
);
2237 if (nbyte
!= (ssize_t
)(ZEBRA_HEADER_SIZE
-already
))
2239 /* Try again later. */
2240 zebra_event (ZEBRA_READ
, sock
, client
);
2243 already
= ZEBRA_HEADER_SIZE
;
2246 /* Reset to read from the beginning of the incoming packet. */
2247 stream_set_getp(client
->ibuf
, 0);
2249 /* Fetch header values */
2250 length
= stream_getw (client
->ibuf
);
2251 marker
= stream_getc (client
->ibuf
);
2252 version
= stream_getc (client
->ibuf
);
2253 vrf_id
= stream_getw (client
->ibuf
);
2254 command
= stream_getw (client
->ibuf
);
2256 if (marker
!= ZEBRA_HEADER_MARKER
|| version
!= ZSERV_VERSION
)
2258 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
2259 __func__
, sock
, marker
, version
);
2260 zebra_client_close (client
);
2263 if (length
< ZEBRA_HEADER_SIZE
)
2265 zlog_warn("%s: socket %d message length %u is less than header size %d",
2266 __func__
, sock
, length
, ZEBRA_HEADER_SIZE
);
2267 zebra_client_close (client
);
2270 if (length
> STREAM_SIZE(client
->ibuf
))
2272 zlog_warn("%s: socket %d message length %u exceeds buffer size %lu",
2273 __func__
, sock
, length
, (u_long
)STREAM_SIZE(client
->ibuf
));
2274 zebra_client_close (client
);
2278 /* Read rest of data. */
2279 if (already
< length
)
2282 if (((nbyte
= stream_read_try (client
->ibuf
, sock
,
2283 length
-already
)) == 0) ||
2286 if (IS_ZEBRA_DEBUG_EVENT
)
2287 zlog_debug ("connection closed [%d] when reading zebra data", sock
);
2288 zebra_client_close (client
);
2291 if (nbyte
!= (ssize_t
)(length
-already
))
2293 /* Try again later. */
2294 zebra_event (ZEBRA_READ
, sock
, client
);
2299 length
-= ZEBRA_HEADER_SIZE
;
2301 /* Debug packet information. */
2302 if (IS_ZEBRA_DEBUG_EVENT
)
2303 zlog_debug ("zebra message comes from socket [%d]", sock
);
2305 if (IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
)
2306 zlog_debug ("zebra message received [%s] %d in VRF %u",
2307 zserv_command_string (command
), length
, vrf_id
);
2309 client
->last_read_time
= monotime(NULL
);
2310 client
->last_read_cmd
= command
;
2312 zvrf
= zebra_vrf_lookup_by_id (vrf_id
);
2315 if (IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
)
2316 zlog_debug ("zebra received unknown VRF[%u]", vrf_id
);
2317 goto zclient_read_out
;
2322 case ZEBRA_ROUTER_ID_ADD
:
2323 zread_router_id_add (client
, length
, zvrf
);
2325 case ZEBRA_ROUTER_ID_DELETE
:
2326 zread_router_id_delete (client
, length
, zvrf
);
2328 case ZEBRA_INTERFACE_ADD
:
2329 zread_interface_add (client
, length
, zvrf
);
2331 case ZEBRA_INTERFACE_DELETE
:
2332 zread_interface_delete (client
, length
, zvrf
);
2334 case ZEBRA_IPV4_ROUTE_ADD
:
2335 zread_ipv4_add (client
, length
, zvrf
);
2337 case ZEBRA_IPV4_ROUTE_DELETE
:
2338 zread_ipv4_delete (client
, length
, zvrf
);
2340 case ZEBRA_IPV4_ROUTE_IPV6_NEXTHOP_ADD
:
2341 zread_ipv4_route_ipv6_nexthop_add (client
, length
, zvrf
);
2343 case ZEBRA_IPV4_NEXTHOP_ADD
:
2344 zread_ipv4_add(client
, length
, zvrf
); /* LB: r1.0 merge - id was 1 */
2346 case ZEBRA_IPV4_NEXTHOP_DELETE
:
2347 zread_ipv4_delete(client
, length
, zvrf
); /* LB: r1.0 merge - id was 1 */
2349 case ZEBRA_IPV6_ROUTE_ADD
:
2350 zread_ipv6_add (client
, length
, zvrf
);
2352 case ZEBRA_IPV6_ROUTE_DELETE
:
2353 zread_ipv6_delete (client
, length
, zvrf
);
2355 case ZEBRA_REDISTRIBUTE_ADD
:
2356 zebra_redistribute_add (command
, client
, length
, zvrf
);
2358 case ZEBRA_REDISTRIBUTE_DELETE
:
2359 zebra_redistribute_delete (command
, client
, length
, zvrf
);
2361 case ZEBRA_REDISTRIBUTE_DEFAULT_ADD
:
2362 zebra_redistribute_default_add (command
, client
, length
, zvrf
);
2364 case ZEBRA_REDISTRIBUTE_DEFAULT_DELETE
:
2365 zebra_redistribute_default_delete (command
, client
, length
, zvrf
);
2367 case ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB
:
2368 zread_ipv4_nexthop_lookup_mrib (client
, length
, zvrf
);
2371 zread_hello (client
);
2373 case ZEBRA_NEXTHOP_REGISTER
:
2374 zserv_rnh_register(client
, sock
, length
, RNH_NEXTHOP_TYPE
, zvrf
);
2376 case ZEBRA_NEXTHOP_UNREGISTER
:
2377 zserv_rnh_unregister(client
, sock
, length
, RNH_NEXTHOP_TYPE
, zvrf
);
2379 case ZEBRA_IMPORT_ROUTE_REGISTER
:
2380 zserv_rnh_register(client
, sock
, length
, RNH_IMPORT_CHECK_TYPE
, zvrf
);
2382 case ZEBRA_IMPORT_ROUTE_UNREGISTER
:
2383 zserv_rnh_unregister(client
, sock
, length
, RNH_IMPORT_CHECK_TYPE
, zvrf
);
2385 case ZEBRA_BFD_DEST_UPDATE
:
2386 case ZEBRA_BFD_DEST_REGISTER
:
2387 zebra_ptm_bfd_dst_register(client
, sock
, length
, command
, zvrf
);
2389 case ZEBRA_BFD_DEST_DEREGISTER
:
2390 zebra_ptm_bfd_dst_deregister(client
, sock
, length
, zvrf
);
2392 case ZEBRA_VRF_UNREGISTER
:
2393 zread_vrf_unregister (client
, length
, zvrf
);
2395 case ZEBRA_BFD_CLIENT_REGISTER
:
2396 zebra_ptm_bfd_client_register(client
, sock
, length
);
2398 case ZEBRA_INTERFACE_ENABLE_RADV
:
2399 #if defined (HAVE_RTADV)
2400 zebra_interface_radv_set (client
, sock
, length
, zvrf
, 1);
2403 case ZEBRA_INTERFACE_DISABLE_RADV
:
2404 #if defined (HAVE_RTADV)
2405 zebra_interface_radv_set (client
, sock
, length
, zvrf
, 0);
2408 case ZEBRA_MPLS_LABELS_ADD
:
2409 case ZEBRA_MPLS_LABELS_DELETE
:
2410 zread_mpls_labels (command
, client
, length
, vrf_id
);
2412 case ZEBRA_IPMR_ROUTE_STATS
:
2413 zebra_ipmr_route_stats (client
, sock
, length
, zvrf
);
2415 case ZEBRA_LABEL_MANAGER_CONNECT
:
2416 case ZEBRA_GET_LABEL_CHUNK
:
2417 case ZEBRA_RELEASE_LABEL_CHUNK
:
2418 zread_label_manager_request (command
, client
, vrf_id
);
2420 case ZEBRA_FEC_REGISTER
:
2421 zserv_fec_register (client
, sock
, length
);
2423 case ZEBRA_FEC_UNREGISTER
:
2424 zserv_fec_unregister (client
, sock
, length
);
2427 zlog_info ("Zebra received unknown command %d", command
);
2431 if (client
->t_suicide
)
2433 /* No need to wait for thread callback, just kill immediately. */
2434 zebra_client_close(client
);
2439 stream_reset (client
->ibuf
);
2440 zebra_event (ZEBRA_READ
, sock
, client
);
2445 /* Accept code of zebra server socket. */
2447 zebra_accept (struct thread
*thread
)
2451 struct sockaddr_in client
;
2454 accept_sock
= THREAD_FD (thread
);
2456 /* Reregister myself. */
2457 zebra_event (ZEBRA_SERV
, accept_sock
, NULL
);
2459 len
= sizeof (struct sockaddr_in
);
2460 client_sock
= accept (accept_sock
, (struct sockaddr
*) &client
, &len
);
2462 if (client_sock
< 0)
2464 zlog_warn ("Can't accept zebra socket: %s", safe_strerror (errno
));
2468 /* Make client socket non-blocking. */
2469 set_nonblocking(client_sock
);
2471 /* Create new zebra client. */
2472 zebra_client_create (client_sock
);
2477 #ifdef HAVE_TCP_ZEBRA
2478 /* Make zebra's server socket. */
2484 struct sockaddr_in addr
;
2486 accept_sock
= socket (AF_INET
, SOCK_STREAM
, 0);
2488 if (accept_sock
< 0)
2490 zlog_warn ("Can't create zserv stream socket: %s",
2491 safe_strerror (errno
));
2492 zlog_warn ("zebra can't provice full functionality due to above error");
2496 memset (&addr
, 0, sizeof (struct sockaddr_in
));
2497 addr
.sin_family
= AF_INET
;
2498 addr
.sin_port
= htons (ZEBRA_PORT
);
2499 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
2500 addr
.sin_len
= sizeof (struct sockaddr_in
);
2501 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
2502 addr
.sin_addr
.s_addr
= htonl (INADDR_LOOPBACK
);
2504 sockopt_reuseaddr (accept_sock
);
2505 sockopt_reuseport (accept_sock
);
2507 if ( zserv_privs
.change(ZPRIVS_RAISE
) )
2508 zlog_err("Can't raise privileges");
2510 ret
= bind (accept_sock
, (struct sockaddr
*)&addr
,
2511 sizeof (struct sockaddr_in
));
2514 zlog_warn ("Can't bind to stream socket: %s",
2515 safe_strerror (errno
));
2516 zlog_warn ("zebra can't provice full functionality due to above error");
2517 close (accept_sock
); /* Avoid sd leak. */
2521 if ( zserv_privs
.change(ZPRIVS_LOWER
) )
2522 zlog_err("Can't lower privileges");
2524 ret
= listen (accept_sock
, 1);
2527 zlog_warn ("Can't listen to stream socket: %s",
2528 safe_strerror (errno
));
2529 zlog_warn ("zebra can't provice full functionality due to above error");
2530 close (accept_sock
); /* Avoid sd leak. */
2534 zebra_event (ZEBRA_SERV
, accept_sock
, NULL
);
2536 #else /* HAVE_TCP_ZEBRA */
2538 /* For sockaddr_un. */
2541 /* zebra server UNIX domain socket. */
2543 zebra_serv_un (const char *path
)
2547 struct sockaddr_un serv
;
2550 /* First of all, unlink existing socket */
2554 old_mask
= umask (0077);
2556 /* Make UNIX domain socket. */
2557 sock
= socket (AF_UNIX
, SOCK_STREAM
, 0);
2560 zlog_warn ("Can't create zserv unix socket: %s",
2561 safe_strerror (errno
));
2562 zlog_warn ("zebra can't provide full functionality due to above error");
2566 /* Make server socket. */
2567 memset (&serv
, 0, sizeof (struct sockaddr_un
));
2568 serv
.sun_family
= AF_UNIX
;
2569 strncpy (serv
.sun_path
, path
, strlen (path
));
2570 #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
2571 len
= serv
.sun_len
= SUN_LEN(&serv
);
2573 len
= sizeof (serv
.sun_family
) + strlen (serv
.sun_path
);
2574 #endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
2576 ret
= bind (sock
, (struct sockaddr
*) &serv
, len
);
2579 zlog_warn ("Can't bind to unix socket %s: %s",
2580 path
, safe_strerror (errno
));
2581 zlog_warn ("zebra can't provide full functionality due to above error");
2586 ret
= listen (sock
, 5);
2589 zlog_warn ("Can't listen to unix socket %s: %s",
2590 path
, safe_strerror (errno
));
2591 zlog_warn ("zebra can't provide full functionality due to above error");
2598 zebra_event (ZEBRA_SERV
, sock
, NULL
);
2600 #endif /* HAVE_TCP_ZEBRA */
2604 zebra_event (enum event event
, int sock
, struct zserv
*client
)
2609 thread_add_read(zebrad
.master
, zebra_accept
, client
, sock
, NULL
);
2612 client
->t_read
= NULL
;
2613 thread_add_read(zebrad
.master
, zebra_client_read
, client
, sock
,
2622 #define ZEBRA_TIME_BUF 32
2624 zserv_time_buf(time_t *time1
, char *buf
, int buflen
)
2629 assert (buf
!= NULL
);
2630 assert (buflen
>= ZEBRA_TIME_BUF
);
2631 assert (time1
!= NULL
);
2635 snprintf(buf
, buflen
, "never ");
2639 now
= monotime(NULL
);
2643 /* Making formatted timer strings. */
2644 #define ONE_DAY_SECOND 60*60*24
2645 #define ONE_WEEK_SECOND 60*60*24*7
2647 if (now
< ONE_DAY_SECOND
)
2648 snprintf (buf
, buflen
, "%02d:%02d:%02d",
2649 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
);
2650 else if (now
< ONE_WEEK_SECOND
)
2651 snprintf (buf
, buflen
, "%dd%02dh%02dm",
2652 tm
->tm_yday
, tm
->tm_hour
, tm
->tm_min
);
2654 snprintf (buf
, buflen
, "%02dw%dd%02dh",
2655 tm
->tm_yday
/7, tm
->tm_yday
- ((tm
->tm_yday
/7) * 7), tm
->tm_hour
);
2660 zebra_show_client_detail (struct vty
*vty
, struct zserv
*client
)
2662 char cbuf
[ZEBRA_TIME_BUF
], rbuf
[ZEBRA_TIME_BUF
];
2663 char wbuf
[ZEBRA_TIME_BUF
], nhbuf
[ZEBRA_TIME_BUF
], mbuf
[ZEBRA_TIME_BUF
];
2665 vty_out (vty
, "Client: %s", zebra_route_string(client
->proto
));
2666 if (client
->instance
)
2667 vty_out (vty
, " Instance: %d", client
->instance
);
2668 vty_out (vty
, "%s", VTY_NEWLINE
);
2670 vty_out (vty
, "------------------------ %s", VTY_NEWLINE
);
2671 vty_out (vty
, "FD: %d %s", client
->sock
, VTY_NEWLINE
);
2672 vty_out (vty
, "Route Table ID: %d %s", client
->rtm_table
, VTY_NEWLINE
);
2674 vty_out (vty
, "Connect Time: %s %s",
2675 zserv_time_buf(&client
->connect_time
, cbuf
, ZEBRA_TIME_BUF
),
2677 if (client
->nh_reg_time
)
2679 vty_out (vty
, "Nexthop Registry Time: %s %s",
2680 zserv_time_buf(&client
->nh_reg_time
, nhbuf
, ZEBRA_TIME_BUF
),
2682 if (client
->nh_last_upd_time
)
2683 vty_out (vty
, "Nexthop Last Update Time: %s %s",
2684 zserv_time_buf(&client
->nh_last_upd_time
, mbuf
, ZEBRA_TIME_BUF
),
2687 vty_out (vty
, "No Nexthop Update sent%s", VTY_NEWLINE
);
2690 vty_out (vty
, "Not registered for Nexthop Updates%s", VTY_NEWLINE
);
2692 vty_out (vty
, "Last Msg Rx Time: %s %s",
2693 zserv_time_buf(&client
->last_read_time
, rbuf
, ZEBRA_TIME_BUF
),
2695 vty_out (vty
, "Last Msg Tx Time: %s %s",
2696 zserv_time_buf(&client
->last_write_time
, wbuf
, ZEBRA_TIME_BUF
),
2698 if (client
->last_read_time
)
2699 vty_out (vty
, "Last Rcvd Cmd: %s %s",
2700 zserv_command_string(client
->last_read_cmd
), VTY_NEWLINE
);
2701 if (client
->last_write_time
)
2702 vty_out (vty
, "Last Sent Cmd: %s %s",
2703 zserv_command_string(client
->last_write_cmd
), VTY_NEWLINE
);
2704 vty_out (vty
, "%s", VTY_NEWLINE
);
2706 vty_out (vty
, "Type Add Update Del %s", VTY_NEWLINE
);
2707 vty_out (vty
, "================================================== %s", VTY_NEWLINE
);
2708 vty_out (vty
, "IPv4 %-12d%-12d%-12d%s", client
->v4_route_add_cnt
,
2709 client
->v4_route_upd8_cnt
, client
->v4_route_del_cnt
, VTY_NEWLINE
);
2710 vty_out (vty
, "IPv6 %-12d%-12d%-12d%s", client
->v6_route_add_cnt
,
2711 client
->v6_route_upd8_cnt
, client
->v6_route_del_cnt
, VTY_NEWLINE
);
2712 vty_out (vty
, "Redist:v4 %-12d%-12d%-12d%s", client
->redist_v4_add_cnt
, 0,
2713 client
->redist_v4_del_cnt
, VTY_NEWLINE
);
2714 vty_out (vty
, "Redist:v6 %-12d%-12d%-12d%s", client
->redist_v6_add_cnt
, 0,
2715 client
->redist_v6_del_cnt
, VTY_NEWLINE
);
2716 vty_out (vty
, "Connected %-12d%-12d%-12d%s", client
->ifadd_cnt
, 0,
2717 client
->ifdel_cnt
, VTY_NEWLINE
);
2718 vty_out (vty
, "BFD peer %-12d%-12d%-12d%s", client
->bfd_peer_add_cnt
,
2719 client
->bfd_peer_upd8_cnt
, client
->bfd_peer_del_cnt
, VTY_NEWLINE
);
2720 vty_out (vty
, "Interface Up Notifications: %d%s", client
->ifup_cnt
,
2722 vty_out (vty
, "Interface Down Notifications: %d%s", client
->ifdown_cnt
,
2725 vty_out (vty
, "%s", VTY_NEWLINE
);
2730 zebra_show_client_brief (struct vty
*vty
, struct zserv
*client
)
2732 char cbuf
[ZEBRA_TIME_BUF
], rbuf
[ZEBRA_TIME_BUF
];
2733 char wbuf
[ZEBRA_TIME_BUF
];
2735 vty_out (vty
, "%-8s%12s %12s%12s%8d/%-8d%8d/%-8d%s",
2736 zebra_route_string(client
->proto
),
2737 zserv_time_buf(&client
->connect_time
, cbuf
, ZEBRA_TIME_BUF
),
2738 zserv_time_buf(&client
->last_read_time
, rbuf
, ZEBRA_TIME_BUF
),
2739 zserv_time_buf(&client
->last_write_time
, wbuf
, ZEBRA_TIME_BUF
),
2740 client
->v4_route_add_cnt
+client
->v4_route_upd8_cnt
,
2741 client
->v4_route_del_cnt
,
2742 client
->v6_route_add_cnt
+client
->v6_route_upd8_cnt
,
2743 client
->v6_route_del_cnt
, VTY_NEWLINE
);
2748 zebra_find_client (u_char proto
)
2750 struct listnode
*node
, *nnode
;
2751 struct zserv
*client
;
2753 for (ALL_LIST_ELEMENTS (zebrad
.client_list
, node
, nnode
, client
))
2755 if (client
->proto
== proto
)
2763 /* Display default rtm_table for all clients. */
2768 "default routing table to use for all clients\n")
2770 vty_out (vty
, "table %d%s", zebrad
.rtm_table_default
,
2775 DEFUN (config_table
,
2778 "Configure target kernel routing table\n"
2781 zebrad
.rtm_table_default
= strtol (argv
[1]->arg
, (char**)0, 10);
2785 DEFUN (no_config_table
,
2786 no_config_table_cmd
,
2787 "no table [TABLENO]",
2789 "Configure target kernel routing table\n"
2792 zebrad
.rtm_table_default
= 0;
2797 DEFUN (ip_forwarding
,
2801 "Turn on IP forwarding")
2807 ret
= ipforward_on ();
2811 vty_out (vty
, "Can't turn on IP forwarding%s", VTY_NEWLINE
);
2818 DEFUN (no_ip_forwarding
,
2819 no_ip_forwarding_cmd
,
2823 "Turn off IP forwarding")
2829 ret
= ipforward_off ();
2833 vty_out (vty
, "Can't turn off IP forwarding%s", VTY_NEWLINE
);
2844 "Zebra information\n")
2848 vty_out (vty
, " Route Route Neighbor LSP LSP%s", VTY_NEWLINE
);
2849 vty_out (vty
, "VRF Installs Removals Updates Installs Removals%s", VTY_NEWLINE
);
2850 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
)
2852 struct zebra_vrf
*zvrf
= vrf
->info
;
2853 vty_out (vty
,"%-25s %10" PRIu64
" %10" PRIu64
" %10" PRIu64
" %10" PRIu64
" %10" PRIu64
"%s",
2854 vrf
->name
, zvrf
->installs
, zvrf
->removals
,
2855 zvrf
->neigh_updates
, zvrf
->lsp_installs
, zvrf
->lsp_removals
,
2862 /* This command is for debugging purpose. */
2863 DEFUN (show_zebra_client
,
2864 show_zebra_client_cmd
,
2865 "show zebra client",
2867 "Zebra information\n"
2868 "Client information\n")
2870 struct listnode
*node
;
2871 struct zserv
*client
;
2873 for (ALL_LIST_ELEMENTS_RO (zebrad
.client_list
, node
, client
))
2874 zebra_show_client_detail(vty
, client
);
2879 /* This command is for debugging purpose. */
2880 DEFUN (show_zebra_client_summary
,
2881 show_zebra_client_summary_cmd
,
2882 "show zebra client summary",
2884 "Zebra information brief\n"
2885 "Client information brief\n"
2888 struct listnode
*node
;
2889 struct zserv
*client
;
2891 vty_out (vty
, "Name Connect Time Last Read Last Write IPv4 Routes IPv6 Routes %s",
2893 vty_out (vty
,"--------------------------------------------------------------------------------%s",
2896 for (ALL_LIST_ELEMENTS_RO (zebrad
.client_list
, node
, client
))
2897 zebra_show_client_brief(vty
, client
);
2899 vty_out (vty
, "Routes column shows (added+updated)/deleted%s", VTY_NEWLINE
);
2903 /* Table configuration write function. */
2905 config_write_table (struct vty
*vty
)
2907 if (zebrad
.rtm_table_default
)
2908 vty_out (vty
, "table %d%s", zebrad
.rtm_table_default
,
2913 /* table node for routing tables. */
2914 static struct cmd_node table_node
=
2917 "", /* This node has no interface. */
2921 /* Only display ip forwarding is enabled or not. */
2922 DEFUN (show_ip_forwarding
,
2923 show_ip_forwarding_cmd
,
2924 "show ip forwarding",
2927 "IP forwarding status\n")
2934 vty_out (vty
, "IP forwarding is off%s", VTY_NEWLINE
);
2936 vty_out (vty
, "IP forwarding is on%s", VTY_NEWLINE
);
2940 /* Only display ipv6 forwarding is enabled or not. */
2941 DEFUN (show_ipv6_forwarding
,
2942 show_ipv6_forwarding_cmd
,
2943 "show ipv6 forwarding",
2945 "IPv6 information\n"
2946 "Forwarding status\n")
2950 ret
= ipforward_ipv6 ();
2955 vty_out (vty
, "ipv6 forwarding is unknown%s", VTY_NEWLINE
);
2958 vty_out (vty
, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE
);
2961 vty_out (vty
, "ipv6 forwarding is %s%s", "on", VTY_NEWLINE
);
2964 vty_out (vty
, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE
);
2970 DEFUN (ipv6_forwarding
,
2971 ipv6_forwarding_cmd
,
2974 "Turn on IPv6 forwarding")
2978 ret
= ipforward_ipv6 ();
2980 ret
= ipforward_ipv6_on ();
2984 vty_out (vty
, "Can't turn on IPv6 forwarding%s", VTY_NEWLINE
);
2991 DEFUN (no_ipv6_forwarding
,
2992 no_ipv6_forwarding_cmd
,
2993 "no ipv6 forwarding",
2996 "Turn off IPv6 forwarding")
3000 ret
= ipforward_ipv6 ();
3002 ret
= ipforward_ipv6_off ();
3006 vty_out (vty
, "Can't turn off IPv6 forwarding%s", VTY_NEWLINE
);
3013 /* IPForwarding configuration write function. */
3015 config_write_forwarding (struct vty
*vty
)
3017 /* FIXME: Find better place for that. */
3018 router_id_write (vty
);
3021 vty_out (vty
, "no ip forwarding%s", VTY_NEWLINE
);
3022 if (!ipforward_ipv6 ())
3023 vty_out (vty
, "no ipv6 forwarding%s", VTY_NEWLINE
);
3024 vty_out (vty
, "!%s", VTY_NEWLINE
);
3028 /* table node for routing tables. */
3029 static struct cmd_node forwarding_node
=
3032 "", /* This node has no interface. */
3036 /* Initialisation of zebra and installation of commands. */
3040 /* Client list init. */
3041 zebrad
.client_list
= list_new ();
3043 /* Install configuration write function. */
3044 install_node (&table_node
, config_write_table
);
3045 install_node (&forwarding_node
, config_write_forwarding
);
3047 install_element (VIEW_NODE
, &show_ip_forwarding_cmd
);
3048 install_element (CONFIG_NODE
, &ip_forwarding_cmd
);
3049 install_element (CONFIG_NODE
, &no_ip_forwarding_cmd
);
3050 install_element (ENABLE_NODE
, &show_zebra_cmd
);
3051 install_element (ENABLE_NODE
, &show_zebra_client_cmd
);
3052 install_element (ENABLE_NODE
, &show_zebra_client_summary_cmd
);
3055 install_element (VIEW_NODE
, &show_table_cmd
);
3056 install_element (CONFIG_NODE
, &config_table_cmd
);
3057 install_element (CONFIG_NODE
, &no_config_table_cmd
);
3058 #endif /* HAVE_NETLINK */
3060 install_element (VIEW_NODE
, &show_ipv6_forwarding_cmd
);
3061 install_element (CONFIG_NODE
, &ipv6_forwarding_cmd
);
3062 install_element (CONFIG_NODE
, &no_ipv6_forwarding_cmd
);
3065 zebra_route_map_init ();
3068 /* Make zebra server socket, wiping any existing one (see bug #403). */
3070 zebra_zserv_socket_init (char *path
)
3072 #ifdef HAVE_TCP_ZEBRA
3075 zebra_serv_un (path
? path
: ZEBRA_SERV_PATH
);
3076 #endif /* HAVE_TCP_ZEBRA */