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
);
993 label_index
= MPLS_INVALID_LABEL_INDEX
;
994 zebra_mpls_fec_register (zvrf
, &p
, label_index
, client
);
1000 /* FEC unregister */
1002 zserv_fec_unregister (struct zserv
*client
, int sock
, u_short length
)
1005 struct zebra_vrf
*zvrf
;
1011 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1013 return 0; // unexpected
1016 * The minimum amount of data that can be sent for one
1017 * fec unregistration
1019 if (length
< ZEBRA_MIN_FEC_LENGTH
)
1021 zlog_err ("fec_unregister: Received a fec unregister of length %d, it is of insufficient size to properly decode",
1028 //flags = stream_getw(s);
1029 (void)stream_getw(s
);
1030 p
.family
= stream_getw(s
);
1031 if (p
.family
!= AF_INET
&&
1032 p
.family
!= AF_INET6
)
1034 zlog_err ("fec_unregister: Received unknown family type %d\n",
1038 p
.prefixlen
= stream_getc(s
);
1040 stream_get(&p
.u
.prefix
, s
, PSIZE(p
.prefixlen
));
1041 l
+= PSIZE(p
.prefixlen
);
1042 zebra_mpls_fec_unregister (zvrf
, &p
, client
);
1049 Modified version of zsend_ipv4_nexthop_lookup():
1050 Query unicast rib if nexthop is not found on mrib.
1051 Returns both route metric and protocol distance.
1054 zsend_ipv4_nexthop_lookup_mrib (struct zserv
*client
, struct in_addr addr
, struct route_entry
*re
, struct zebra_vrf
*zvrf
)
1059 struct nexthop
*nexthop
;
1061 /* Get output stream. */
1065 /* Fill in result. */
1066 zserv_create_header (s
, ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB
, zvrf_id (zvrf
));
1067 stream_put_in_addr (s
, &addr
);
1071 stream_putc (s
, re
->distance
);
1072 stream_putl (s
, re
->metric
);
1074 nump
= stream_get_endp(s
); /* remember position for nexthop_num */
1075 stream_putc (s
, 0); /* reserve room for nexthop_num */
1076 /* Only non-recursive routes are elegible to resolve the nexthop we
1077 * are looking up. Therefore, we will just iterate over the top
1078 * chain of nexthops. */
1079 for (nexthop
= re
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
1080 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
1081 num
+= zsend_write_nexthop (s
, nexthop
);
1083 stream_putc_at (s
, nump
, num
); /* store nexthop_num */
1087 stream_putc (s
, 0); /* distance */
1088 stream_putl (s
, 0); /* metric */
1089 stream_putc (s
, 0); /* nexthop_num */
1092 stream_putw_at (s
, 0, stream_get_endp (s
));
1094 return zebra_server_send_message(client
);
1097 /* Router-id is updated. Send ZEBRA_ROUTER_ID_ADD to client. */
1099 zsend_router_id_update (struct zserv
*client
, struct prefix
*p
,
1105 /* Check this client need interface information. */
1106 if (! vrf_bitmap_check (client
->ridinfo
, vrf_id
))
1113 zserv_create_header (s
, ZEBRA_ROUTER_ID_UPDATE
, vrf_id
);
1115 /* Prefix information. */
1116 stream_putc (s
, p
->family
);
1117 blen
= prefix_blen (p
);
1118 stream_put (s
, &p
->u
.prefix
, blen
);
1119 stream_putc (s
, p
->prefixlen
);
1121 /* Write packet size. */
1122 stream_putw_at (s
, 0, stream_get_endp (s
));
1124 return zebra_server_send_message(client
);
1127 /* Register zebra server interface information. Send current all
1128 interface and address information. */
1130 zread_interface_add (struct zserv
*client
, u_short length
, struct zebra_vrf
*zvrf
)
1133 struct listnode
*ifnode
, *ifnnode
;
1134 struct interface
*ifp
;
1136 /* Interface information is needed. */
1137 vrf_bitmap_set (client
->ifinfo
, zvrf_id (zvrf
));
1139 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
)
1141 for (ALL_LIST_ELEMENTS (vrf
->iflist
, ifnode
, ifnnode
, ifp
))
1143 /* Skip pseudo interface. */
1144 if (! CHECK_FLAG (ifp
->status
, ZEBRA_INTERFACE_ACTIVE
))
1147 if (zsend_interface_add (client
, ifp
) < 0)
1150 if (zsend_interface_addresses (client
, ifp
) < 0)
1157 /* Unregister zebra server interface information. */
1159 zread_interface_delete (struct zserv
*client
, u_short length
, struct zebra_vrf
*zvrf
)
1161 vrf_bitmap_unset (client
->ifinfo
, zvrf_id (zvrf
));
1166 zserv_nexthop_num_warn (const char *caller
, const struct prefix
*p
, const unsigned int nexthop_num
)
1168 if (nexthop_num
> multipath_num
)
1170 char buff
[PREFIX2STR_BUFFER
];
1171 prefix2str(p
, buff
, sizeof (buff
));
1172 zlog_warn("%s: Prefix %s has %d nexthops, but we can only use the first %d",
1173 caller
, buff
, nexthop_num
, multipath_num
);
1177 /* This function support multiple nexthop. */
1179 * Parse the ZEBRA_IPV4_ROUTE_ADD sent from client. Update re and
1183 zread_ipv4_add (struct zserv
*client
, u_short length
, struct zebra_vrf
*zvrf
)
1186 struct route_entry
*re
;
1189 struct in_addr nhop_addr
;
1191 u_char nexthop_type
;
1197 struct nexthop
*nexthop
;
1199 /* Get input stream. */
1202 /* Allocate new re. */
1203 re
= XCALLOC (MTYPE_RE
, sizeof (struct route_entry
));
1205 /* Type, flags, message. */
1206 re
->type
= stream_getc (s
);
1207 re
->instance
= stream_getw (s
);
1208 re
->flags
= stream_getl (s
);
1209 message
= stream_getc (s
);
1210 safi
= stream_getw (s
);
1211 re
->uptime
= time (NULL
);
1214 memset (&p
, 0, sizeof (struct prefix_ipv4
));
1216 p
.prefixlen
= stream_getc (s
);
1217 stream_get (&p
.u
.prefix4
, s
, PSIZE (p
.prefixlen
));
1220 re
->vrf_id
= zvrf_id (zvrf
);
1222 /* Nexthop parse. */
1223 if (CHECK_FLAG (message
, ZAPI_MESSAGE_NEXTHOP
))
1225 nexthop_num
= stream_getc (s
);
1226 zserv_nexthop_num_warn(__func__
, (const struct prefix
*)&p
, nexthop_num
);
1228 for (i
= 0; i
< nexthop_num
; i
++)
1230 nexthop_type
= stream_getc (s
);
1232 switch (nexthop_type
)
1234 case NEXTHOP_TYPE_IFINDEX
:
1235 ifindex
= stream_getl (s
);
1236 route_entry_nexthop_ifindex_add (re
, ifindex
);
1238 case NEXTHOP_TYPE_IPV4
:
1239 nhop_addr
.s_addr
= stream_get_ipv4 (s
);
1240 nexthop
= route_entry_nexthop_ipv4_add (re
, &nhop_addr
, NULL
);
1241 /* For labeled-unicast, each nexthop is followed by label. */
1242 if (CHECK_FLAG (message
, ZAPI_MESSAGE_LABEL
))
1244 label
= (mpls_label_t
)stream_getl (s
);
1245 nexthop_add_labels (nexthop
, nexthop
->nh_label_type
, 1, &label
);
1248 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1249 nhop_addr
.s_addr
= stream_get_ipv4 (s
);
1250 ifindex
= stream_getl (s
);
1251 route_entry_nexthop_ipv4_ifindex_add (re
, &nhop_addr
, NULL
, ifindex
);
1253 case NEXTHOP_TYPE_IPV6
:
1254 stream_forward_getp (s
, IPV6_MAX_BYTELEN
);
1256 case NEXTHOP_TYPE_BLACKHOLE
:
1257 route_entry_nexthop_blackhole_add (re
);
1264 if (CHECK_FLAG (message
, ZAPI_MESSAGE_DISTANCE
))
1265 re
->distance
= stream_getc (s
);
1268 if (CHECK_FLAG (message
, ZAPI_MESSAGE_METRIC
))
1269 re
->metric
= stream_getl (s
);
1272 if (CHECK_FLAG (message
, ZAPI_MESSAGE_TAG
))
1273 re
->tag
= stream_getl (s
);
1277 if (CHECK_FLAG (message
, ZAPI_MESSAGE_MTU
))
1278 re
->mtu
= stream_getl (s
);
1283 re
->table
= zvrf
->table_id
;
1285 ret
= rib_add_multipath (AFI_IP
, safi
, &p
, NULL
, re
);
1289 client
->v4_route_add_cnt
++;
1291 client
->v4_route_upd8_cnt
++;
1295 /* Zebra server IPv4 prefix delete function. */
1297 zread_ipv4_delete (struct zserv
*client
, u_short length
, struct zebra_vrf
*zvrf
)
1301 struct zapi_ipv4 api
;
1302 struct in_addr nexthop
;
1303 union g_addr
*nexthop_p
;
1304 unsigned long ifindex
;
1307 u_char nexthop_type
;
1315 /* Type, flags, message. */
1316 api
.type
= stream_getc (s
);
1317 api
.instance
= stream_getw (s
);
1318 api
.flags
= stream_getl (s
);
1319 api
.message
= stream_getc (s
);
1320 api
.safi
= stream_getw (s
);
1323 memset (&p
, 0, sizeof (struct prefix
));
1325 p
.prefixlen
= stream_getc (s
);
1326 stream_get (&p
.u
.prefix4
, s
, PSIZE (p
.prefixlen
));
1328 /* Nexthop, ifindex, distance, metric. */
1329 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
))
1331 nexthop_num
= stream_getc (s
);
1333 for (i
= 0; i
< nexthop_num
; i
++)
1335 nexthop_type
= stream_getc (s
);
1337 switch (nexthop_type
)
1339 case NEXTHOP_TYPE_IFINDEX
:
1340 ifindex
= stream_getl (s
);
1342 case NEXTHOP_TYPE_IPV4
:
1343 nexthop
.s_addr
= stream_get_ipv4 (s
);
1344 /* For labeled-unicast, each nexthop is followed by label, but
1345 * we don't care for delete.
1347 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_LABEL
))
1348 stream_forward_getp (s
, sizeof(u_int32_t
));
1349 nexthop_p
= (union g_addr
*)&nexthop
;
1351 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1352 nexthop
.s_addr
= stream_get_ipv4 (s
);
1353 nexthop_p
= (union g_addr
*)&nexthop
;
1354 ifindex
= stream_getl (s
);
1356 case NEXTHOP_TYPE_IPV6
:
1357 stream_forward_getp (s
, IPV6_MAX_BYTELEN
);
1364 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_DISTANCE
))
1365 api
.distance
= stream_getc (s
);
1370 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_METRIC
))
1371 api
.metric
= stream_getl (s
);
1376 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_TAG
))
1377 api
.tag
= stream_getl (s
);
1381 table_id
= zvrf
->table_id
;
1383 rib_delete (AFI_IP
, api
.safi
, zvrf_id (zvrf
), api
.type
, api
.instance
,
1384 api
.flags
, &p
, NULL
, nexthop_p
, ifindex
, table_id
);
1385 client
->v4_route_del_cnt
++;
1389 /* MRIB Nexthop lookup for IPv4. */
1391 zread_ipv4_nexthop_lookup_mrib (struct zserv
*client
, u_short length
, struct zebra_vrf
*zvrf
)
1393 struct in_addr addr
;
1394 struct route_entry
*re
;
1396 addr
.s_addr
= stream_get_ipv4 (client
->ibuf
);
1397 re
= rib_match_ipv4_multicast (zvrf_id (zvrf
), addr
, NULL
);
1398 return zsend_ipv4_nexthop_lookup_mrib (client
, addr
, re
, zvrf
);
1401 /* Zebra server IPv6 prefix add function. */
1403 zread_ipv4_route_ipv6_nexthop_add (struct zserv
*client
, u_short length
, struct zebra_vrf
*zvrf
)
1407 struct in6_addr nhop_addr
;
1408 struct route_entry
*re
;
1411 u_char nexthop_type
;
1414 static struct in6_addr nexthops
[MULTIPATH_NUM
];
1415 static unsigned int ifindices
[MULTIPATH_NUM
];
1417 static mpls_label_t labels
[MULTIPATH_NUM
];
1419 struct nexthop
*nexthop
;
1421 /* Get input stream. */
1424 memset (&nhop_addr
, 0, sizeof (struct in6_addr
));
1426 /* Allocate new re. */
1427 re
= XCALLOC (MTYPE_RE
, sizeof (struct route_entry
));
1429 /* Type, flags, message. */
1430 re
->type
= stream_getc (s
);
1431 re
->instance
= stream_getw (s
);
1432 re
->flags
= stream_getl (s
);
1433 message
= stream_getc (s
);
1434 safi
= stream_getw (s
);
1435 re
->uptime
= time (NULL
);
1438 memset (&p
, 0, sizeof (struct prefix_ipv4
));
1440 p
.prefixlen
= stream_getc (s
);
1441 stream_get (&p
.u
.prefix4
, s
, PSIZE (p
.prefixlen
));
1444 re
->vrf_id
= zvrf_id (zvrf
);
1446 /* We need to give nh-addr, nh-ifindex with the same next-hop object
1447 * to the re to ensure that IPv6 multipathing works; need to coalesce
1448 * these. Clients should send the same number of paired set of
1449 * next-hop-addr/next-hop-ifindices. */
1450 if (CHECK_FLAG (message
, ZAPI_MESSAGE_NEXTHOP
))
1452 unsigned int nh_count
= 0;
1453 unsigned int if_count
= 0;
1454 unsigned int max_nh_if
= 0;
1456 nexthop_num
= stream_getc (s
);
1457 zserv_nexthop_num_warn(__func__
, (const struct prefix
*)&p
, nexthop_num
);
1458 for (i
= 0; i
< nexthop_num
; i
++)
1460 nexthop_type
= stream_getc (s
);
1462 switch (nexthop_type
)
1464 case NEXTHOP_TYPE_IPV6
:
1465 stream_get (&nhop_addr
, s
, 16);
1466 if (nh_count
< MULTIPATH_NUM
)
1468 /* For labeled-unicast, each nexthop is followed by label. */
1469 if (CHECK_FLAG (message
, ZAPI_MESSAGE_LABEL
))
1471 label
= (mpls_label_t
)stream_getl (s
);
1472 labels
[nh_count
] = label
;
1474 nexthops
[nh_count
] = nhop_addr
;
1478 case NEXTHOP_TYPE_IFINDEX
:
1479 if (if_count
< multipath_num
) {
1480 ifindices
[if_count
++] = stream_getl (s
);
1483 case NEXTHOP_TYPE_BLACKHOLE
:
1484 route_entry_nexthop_blackhole_add (re
);
1489 max_nh_if
= (nh_count
> if_count
) ? nh_count
: if_count
;
1490 for (i
= 0; i
< max_nh_if
; i
++)
1492 if ((i
< nh_count
) && !IN6_IS_ADDR_UNSPECIFIED (&nexthops
[i
])) {
1493 if ((i
< if_count
) && ifindices
[i
])
1494 nexthop
= route_entry_nexthop_ipv6_ifindex_add (re
, &nexthops
[i
], ifindices
[i
]);
1496 nexthop
= route_entry_nexthop_ipv6_add (re
, &nexthops
[i
]);
1498 if (CHECK_FLAG (message
, ZAPI_MESSAGE_LABEL
))
1499 nexthop_add_labels (nexthop
, nexthop
->nh_label_type
, 1, &labels
[i
]);
1502 if ((i
< if_count
) && ifindices
[i
])
1503 route_entry_nexthop_ifindex_add (re
, ifindices
[i
]);
1509 if (CHECK_FLAG (message
, ZAPI_MESSAGE_DISTANCE
))
1510 re
->distance
= stream_getc (s
);
1513 if (CHECK_FLAG (message
, ZAPI_MESSAGE_METRIC
))
1514 re
->metric
= stream_getl (s
);
1517 if (CHECK_FLAG (message
, ZAPI_MESSAGE_TAG
))
1518 re
->tag
= stream_getl (s
);
1522 if (CHECK_FLAG (message
, ZAPI_MESSAGE_MTU
))
1523 re
->mtu
= stream_getl (s
);
1528 re
->table
= zvrf
->table_id
;
1530 ret
= rib_add_multipath (AFI_IP6
, safi
, &p
, NULL
, re
);
1533 client
->v4_route_add_cnt
++;
1535 client
->v4_route_upd8_cnt
++;
1541 zread_ipv6_add (struct zserv
*client
, u_short length
, struct zebra_vrf
*zvrf
)
1545 struct in6_addr nhop_addr
;
1546 struct route_entry
*re
;
1549 u_char nexthop_type
;
1551 struct prefix_ipv6 src_p
, *src_pp
;
1553 static struct in6_addr nexthops
[MULTIPATH_NUM
];
1554 static unsigned int ifindices
[MULTIPATH_NUM
];
1556 static mpls_label_t labels
[MULTIPATH_NUM
];
1558 struct nexthop
*nexthop
;
1560 /* Get input stream. */
1563 memset (&nhop_addr
, 0, sizeof (struct in6_addr
));
1565 /* Allocate new re. */
1566 re
= XCALLOC (MTYPE_RE
, sizeof (struct route_entry
));
1568 /* Type, flags, message. */
1569 re
->type
= stream_getc (s
);
1570 re
->instance
= stream_getw (s
);
1571 re
->flags
= stream_getl (s
);
1572 message
= stream_getc (s
);
1573 safi
= stream_getw (s
);
1574 re
->uptime
= time (NULL
);
1577 memset (&p
, 0, sizeof (struct prefix_ipv6
));
1578 p
.family
= AF_INET6
;
1579 p
.prefixlen
= stream_getc (s
);
1580 stream_get (&p
.u
.prefix6
, s
, PSIZE (p
.prefixlen
));
1582 if (CHECK_FLAG (message
, ZAPI_MESSAGE_SRCPFX
))
1584 memset (&src_p
, 0, sizeof (struct prefix_ipv6
));
1585 src_p
.family
= AF_INET6
;
1586 src_p
.prefixlen
= stream_getc (s
);
1587 stream_get (&src_p
.prefix
, s
, PSIZE (src_p
.prefixlen
));
1593 /* We need to give nh-addr, nh-ifindex with the same next-hop object
1594 * to the re to ensure that IPv6 multipathing works; need to coalesce
1595 * these. Clients should send the same number of paired set of
1596 * next-hop-addr/next-hop-ifindices. */
1597 if (CHECK_FLAG (message
, ZAPI_MESSAGE_NEXTHOP
))
1599 unsigned int nh_count
= 0;
1600 unsigned int if_count
= 0;
1601 unsigned int max_nh_if
= 0;
1603 nexthop_num
= stream_getc (s
);
1604 zserv_nexthop_num_warn(__func__
, (const struct prefix
*)&p
, nexthop_num
);
1605 for (i
= 0; i
< nexthop_num
; i
++)
1607 nexthop_type
= stream_getc (s
);
1609 switch (nexthop_type
)
1611 case NEXTHOP_TYPE_IPV6
:
1612 stream_get (&nhop_addr
, s
, 16);
1613 if (nh_count
< MULTIPATH_NUM
)
1615 /* For labeled-unicast, each nexthop is followed by label. */
1616 if (CHECK_FLAG (message
, ZAPI_MESSAGE_LABEL
))
1618 label
= (mpls_label_t
)stream_getl (s
);
1619 labels
[nh_count
] = label
;
1621 nexthops
[nh_count
++] = nhop_addr
;
1624 case NEXTHOP_TYPE_IFINDEX
:
1625 if (if_count
< multipath_num
) {
1626 ifindices
[if_count
++] = stream_getl (s
);
1629 case NEXTHOP_TYPE_BLACKHOLE
:
1630 route_entry_nexthop_blackhole_add (re
);
1635 max_nh_if
= (nh_count
> if_count
) ? nh_count
: if_count
;
1636 for (i
= 0; i
< max_nh_if
; i
++)
1638 if ((i
< nh_count
) && !IN6_IS_ADDR_UNSPECIFIED (&nexthops
[i
])) {
1639 if ((i
< if_count
) && ifindices
[i
])
1640 nexthop
= route_entry_nexthop_ipv6_ifindex_add (re
, &nexthops
[i
], ifindices
[i
]);
1642 nexthop
= route_entry_nexthop_ipv6_add (re
, &nexthops
[i
]);
1643 if (CHECK_FLAG (message
, ZAPI_MESSAGE_LABEL
))
1644 nexthop_add_labels (nexthop
, nexthop
->nh_label_type
, 1, &labels
[i
]);
1647 if ((i
< if_count
) && ifindices
[i
])
1648 route_entry_nexthop_ifindex_add (re
, ifindices
[i
]);
1654 if (CHECK_FLAG (message
, ZAPI_MESSAGE_DISTANCE
))
1655 re
->distance
= stream_getc (s
);
1658 if (CHECK_FLAG (message
, ZAPI_MESSAGE_METRIC
))
1659 re
->metric
= stream_getl (s
);
1662 if (CHECK_FLAG (message
, ZAPI_MESSAGE_TAG
))
1663 re
->tag
= stream_getl (s
);
1667 if (CHECK_FLAG (message
, ZAPI_MESSAGE_MTU
))
1668 re
->mtu
= stream_getl (s
);
1673 re
->vrf_id
= zvrf_id (zvrf
);
1674 re
->table
= zvrf
->table_id
;
1676 ret
= rib_add_multipath (AFI_IP6
, safi
, &p
, src_pp
, re
);
1679 client
->v6_route_add_cnt
++;
1681 client
->v6_route_upd8_cnt
++;
1686 /* Zebra server IPv6 prefix delete function. */
1688 zread_ipv6_delete (struct zserv
*client
, u_short length
, struct zebra_vrf
*zvrf
)
1692 struct zapi_ipv6 api
;
1693 struct in6_addr nexthop
;
1694 union g_addr
*pnexthop
= NULL
;
1695 unsigned long ifindex
;
1697 struct prefix_ipv6 src_p
, *src_pp
;
1701 memset (&nexthop
, 0, sizeof (struct in6_addr
));
1703 /* Type, flags, message. */
1704 api
.type
= stream_getc (s
);
1705 api
.instance
= stream_getw (s
);
1706 api
.flags
= stream_getl (s
);
1707 api
.message
= stream_getc (s
);
1708 api
.safi
= stream_getw (s
);
1711 memset (&p
, 0, sizeof (struct prefix_ipv6
));
1712 p
.family
= AF_INET6
;
1713 p
.prefixlen
= stream_getc (s
);
1714 stream_get (&p
.u
.prefix6
, s
, PSIZE (p
.prefixlen
));
1716 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_SRCPFX
))
1718 memset (&src_p
, 0, sizeof (struct prefix_ipv6
));
1719 src_p
.family
= AF_INET6
;
1720 src_p
.prefixlen
= stream_getc (s
);
1721 stream_get (&src_p
.prefix
, s
, PSIZE (src_p
.prefixlen
));
1727 /* Nexthop, ifindex, distance, metric. */
1728 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
))
1730 u_char nexthop_type
;
1732 api
.nexthop_num
= stream_getc (s
);
1733 for (i
= 0; i
< api
.nexthop_num
; i
++)
1735 nexthop_type
= stream_getc (s
);
1737 switch (nexthop_type
)
1739 case NEXTHOP_TYPE_IPV6
:
1740 stream_get (&nexthop
, s
, 16);
1741 /* For labeled-unicast, each nexthop is followed by label, but
1742 * we don't care for delete.
1744 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_LABEL
))
1745 stream_forward_getp (s
, sizeof(u_int32_t
));
1746 pnexthop
= (union g_addr
*)&nexthop
;
1748 case NEXTHOP_TYPE_IFINDEX
:
1749 ifindex
= stream_getl (s
);
1756 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_DISTANCE
))
1757 api
.distance
= stream_getc (s
);
1762 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_METRIC
))
1763 api
.metric
= stream_getl (s
);
1768 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_TAG
))
1769 api
.tag
= stream_getl (s
);
1773 if (IN6_IS_ADDR_UNSPECIFIED (&nexthop
))
1774 rib_delete (AFI_IP6
, api
.safi
, zvrf_id (zvrf
), api
.type
, api
.instance
,
1775 api
.flags
, &p
, src_pp
, NULL
, ifindex
, client
->rtm_table
);
1777 rib_delete (AFI_IP6
, api
.safi
, zvrf_id (zvrf
), api
.type
, api
.instance
,
1778 api
.flags
, &p
, src_pp
, pnexthop
, ifindex
, client
->rtm_table
);
1780 client
->v6_route_del_cnt
++;
1784 /* Register zebra server router-id information. Send current router-id */
1786 zread_router_id_add (struct zserv
*client
, u_short length
, struct zebra_vrf
*zvrf
)
1790 /* Router-id information is needed. */
1791 vrf_bitmap_set (client
->ridinfo
, zvrf_id (zvrf
));
1793 router_id_get (&p
, zvrf_id (zvrf
));
1795 return zsend_router_id_update (client
, &p
, zvrf_id (zvrf
));
1798 /* Unregister zebra server router-id information. */
1800 zread_router_id_delete (struct zserv
*client
, u_short length
, struct zebra_vrf
*zvrf
)
1802 vrf_bitmap_unset (client
->ridinfo
, zvrf_id (zvrf
));
1806 /* Tie up route-type and client->sock */
1808 zread_hello (struct zserv
*client
)
1810 /* type of protocol (lib/zebra.h) */
1814 proto
= stream_getc (client
->ibuf
);
1815 instance
= stream_getw (client
->ibuf
);
1817 /* accept only dynamic routing protocols */
1818 if ((proto
< ZEBRA_ROUTE_MAX
)
1819 && (proto
> ZEBRA_ROUTE_STATIC
))
1821 zlog_notice ("client %d says hello and bids fair to announce only %s routes",
1822 client
->sock
, zebra_route_string(proto
));
1824 zlog_notice ("client protocol instance %d", instance
);
1826 client
->proto
= proto
;
1827 client
->instance
= instance
;
1831 /* Unregister all information in a VRF. */
1833 zread_vrf_unregister (struct zserv
*client
, u_short length
, struct zebra_vrf
*zvrf
)
1838 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
1839 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
1840 vrf_bitmap_unset (client
->redist
[afi
][i
], zvrf_id (zvrf
));
1841 vrf_bitmap_unset (client
->redist_default
, zvrf_id (zvrf
));
1842 vrf_bitmap_unset (client
->ifinfo
, zvrf_id (zvrf
));
1843 vrf_bitmap_unset (client
->ridinfo
, zvrf_id (zvrf
));
1849 zread_mpls_labels (int command
, struct zserv
*client
, u_short length
,
1853 enum lsp_types_t type
;
1854 struct prefix prefix
;
1855 enum nexthop_types_t gtype
;
1858 mpls_label_t in_label
, out_label
;
1860 struct zebra_vrf
*zvrf
;
1862 zvrf
= vrf_info_lookup (vrf_id
);
1866 /* Get input stream. */
1870 type
= stream_getc (s
);
1871 prefix
.family
= stream_getl (s
);
1872 switch (prefix
.family
)
1875 prefix
.u
.prefix4
.s_addr
= stream_get_ipv4 (s
);
1876 prefix
.prefixlen
= stream_getc (s
);
1877 gate
.ipv4
.s_addr
= stream_get_ipv4 (s
);
1880 stream_get (&prefix
.u
.prefix6
, s
, 16);
1881 prefix
.prefixlen
= stream_getc (s
);
1882 stream_get (&gate
.ipv6
, s
, 16);
1887 ifindex
= stream_getl (s
);
1888 distance
= stream_getc (s
);
1889 in_label
= stream_getl (s
);
1890 out_label
= stream_getl (s
);
1892 switch (prefix
.family
)
1896 gtype
= NEXTHOP_TYPE_IPV4_IFINDEX
;
1898 gtype
= NEXTHOP_TYPE_IPV4
;
1902 gtype
= NEXTHOP_TYPE_IPV6_IFINDEX
;
1904 gtype
= NEXTHOP_TYPE_IPV6
;
1913 if (command
== ZEBRA_MPLS_LABELS_ADD
)
1915 mpls_lsp_install (zvrf
, type
, in_label
, out_label
, gtype
, &gate
,
1917 if (out_label
!= MPLS_IMP_NULL_LABEL
)
1918 mpls_ftn_update (1, zvrf
, type
, &prefix
, gtype
, &gate
, ifindex
,
1919 distance
, out_label
);
1921 else if (command
== ZEBRA_MPLS_LABELS_DELETE
)
1923 mpls_lsp_uninstall (zvrf
, type
, in_label
, gtype
, &gate
, ifindex
);
1924 if (out_label
!= MPLS_IMP_NULL_LABEL
)
1925 mpls_ftn_update (0, zvrf
, type
, &prefix
, gtype
, &gate
, ifindex
,
1926 distance
, out_label
);
1929 /* Send response to a label manager connect request to client */
1931 zsend_label_manager_connect_response (struct zserv
*client
, vrf_id_t vrf_id
, u_short result
)
1938 zserv_create_header (s
, ZEBRA_LABEL_MANAGER_CONNECT
, vrf_id
);
1941 stream_putc (s
, result
);
1943 /* Write packet size. */
1944 stream_putw_at (s
, 0, stream_get_endp (s
));
1946 return writen (client
->sock
, s
->data
, stream_get_endp (s
));
1950 zread_label_manager_connect (struct zserv
*client
, vrf_id_t vrf_id
)
1953 /* type of protocol (lib/zebra.h) */
1957 /* Get input stream. */
1961 proto
= stream_getc (s
);
1962 instance
= stream_getw (s
);
1964 /* accept only dynamic routing protocols */
1965 if ((proto
>= ZEBRA_ROUTE_MAX
)
1966 || (proto
<= ZEBRA_ROUTE_STATIC
))
1968 zlog_err ("client %d has wrong protocol %s",
1969 client
->sock
, zebra_route_string(proto
));
1970 zsend_label_manager_connect_response (client
, vrf_id
, 1);
1973 zlog_notice ("client %d with instance %u connected as %s",
1974 client
->sock
, instance
, zebra_route_string(proto
));
1975 client
->proto
= proto
;
1976 client
->instance
= instance
;
1979 Release previous labels of same protocol and instance.
1980 This is done in case it restarted from an unexpected shutdown.
1982 release_daemon_chunks (proto
, instance
);
1984 zlog_debug (" Label Manager client connected: sock %d, proto %s, instance %u",
1985 client
->sock
, zebra_route_string(proto
), instance
);
1986 /* send response back */
1987 zsend_label_manager_connect_response (client
, vrf_id
, 0);
1989 /* Send response to a get label chunk request to client */
1991 zsend_assign_label_chunk_response (struct zserv
*client
, vrf_id_t vrf_id
,
1992 struct label_manager_chunk
*lmc
)
1999 zserv_create_header (s
, ZEBRA_GET_LABEL_CHUNK
, vrf_id
);
2004 stream_putc (s
, lmc
->keep
);
2005 /* start and end labels */
2006 stream_putl (s
, lmc
->start
);
2007 stream_putl (s
, lmc
->end
);
2011 /* Write packet size. */
2012 stream_putw_at (s
, 0, stream_get_endp (s
));
2014 return writen (client
->sock
, s
->data
, stream_get_endp (s
));
2018 zread_get_label_chunk (struct zserv
*client
, vrf_id_t vrf_id
)
2023 struct label_manager_chunk
*lmc
;
2025 /* Get input stream. */
2029 keep
= stream_getc (s
);
2030 size
= stream_getl (s
);
2032 lmc
= assign_label_chunk (client
->proto
, client
->instance
, keep
, size
);
2034 zlog_err ("%s: Unable to assign Label Chunk of size %u", __func__
, size
);
2036 zlog_debug ("Assigned Label Chunk %u - %u to %u",
2037 lmc
->start
, lmc
->end
, keep
);
2038 /* send response back */
2039 zsend_assign_label_chunk_response (client
, vrf_id
, lmc
);
2043 zread_release_label_chunk (struct zserv
*client
)
2046 uint32_t start
, end
;
2048 /* Get input stream. */
2052 start
= stream_getl (s
);
2053 end
= stream_getl (s
);
2055 release_label_chunk (client
->proto
, client
->instance
, start
, end
);
2058 zread_label_manager_request (int cmd
, struct zserv
*client
, vrf_id_t vrf_id
)
2060 /* to avoid sending other messages like ZERBA_INTERFACE_UP */
2061 if (cmd
== ZEBRA_LABEL_MANAGER_CONNECT
)
2062 client
->is_synchronous
= 1;
2064 /* external label manager */
2066 zread_relay_label_manager_request (cmd
, client
, vrf_id
);
2067 /* this is a label manager */
2070 if (cmd
== ZEBRA_LABEL_MANAGER_CONNECT
)
2071 zread_label_manager_connect (client
, vrf_id
);
2074 /* Sanity: don't allow 'unidentified' requests */
2077 zlog_err ("Got label request from an unidentified client");
2080 if (cmd
== ZEBRA_GET_LABEL_CHUNK
)
2081 zread_get_label_chunk (client
, vrf_id
);
2082 else if (cmd
== ZEBRA_RELEASE_LABEL_CHUNK
)
2083 zread_release_label_chunk (client
);
2088 /* Cleanup registered nexthops (across VRFs) upon client disconnect. */
2090 zebra_client_close_cleanup_rnh (struct zserv
*client
)
2093 struct zebra_vrf
*zvrf
;
2095 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
)
2097 if ((zvrf
= vrf
->info
) != NULL
)
2099 zebra_cleanup_rnh_client(zvrf_id (zvrf
), AF_INET
, client
, RNH_NEXTHOP_TYPE
);
2100 zebra_cleanup_rnh_client(zvrf_id (zvrf
), AF_INET6
, client
, RNH_NEXTHOP_TYPE
);
2101 zebra_cleanup_rnh_client(zvrf_id (zvrf
), AF_INET
, client
, RNH_IMPORT_CHECK_TYPE
);
2102 zebra_cleanup_rnh_client(zvrf_id (zvrf
), AF_INET6
, client
, RNH_IMPORT_CHECK_TYPE
);
2103 if (client
->proto
== ZEBRA_ROUTE_LDP
)
2105 hash_iterate(zvrf
->lsp_table
, mpls_ldp_lsp_uninstall_all
,
2107 mpls_ldp_ftn_uninstall_all (zvrf
, AFI_IP
);
2108 mpls_ldp_ftn_uninstall_all (zvrf
, AFI_IP6
);
2114 /* Close zebra client. */
2116 zebra_client_close (struct zserv
*client
)
2118 /* Send client de-registration to BFD */
2119 zebra_ptm_bfd_client_deregister(client
->proto
);
2121 /* Cleanup any registered nexthops - across all VRFs. */
2122 zebra_client_close_cleanup_rnh (client
);
2124 /* Release Label Manager chunks */
2125 release_daemon_chunks (client
->proto
, client
->instance
);
2127 /* Cleanup any FECs registered by this client. */
2128 zebra_mpls_cleanup_fecs_for_client (vrf_info_lookup(VRF_DEFAULT
), client
);
2130 /* Close file descriptor. */
2133 unsigned long nroutes
;
2135 close (client
->sock
);
2136 nroutes
= rib_score_proto (client
->proto
, client
->instance
);
2137 zlog_notice ("client %d disconnected. %lu %s routes removed from the rib",
2138 client
->sock
, nroutes
, zebra_route_string (client
->proto
));
2142 /* Free stream buffers. */
2144 stream_free (client
->ibuf
);
2146 stream_free (client
->obuf
);
2148 buffer_free(client
->wb
);
2150 /* Release threads. */
2152 thread_cancel (client
->t_read
);
2153 if (client
->t_write
)
2154 thread_cancel (client
->t_write
);
2155 if (client
->t_suicide
)
2156 thread_cancel (client
->t_suicide
);
2159 for (afi_t afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
2160 for (int i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
2161 vrf_bitmap_free (client
->redist
[afi
][i
]);
2163 vrf_bitmap_free (client
->redist_default
);
2164 vrf_bitmap_free (client
->ifinfo
);
2165 vrf_bitmap_free (client
->ridinfo
);
2167 /* Free client structure. */
2168 listnode_delete (zebrad
.client_list
, client
);
2169 XFREE (MTYPE_TMP
, client
);
2172 /* Make new client. */
2174 zebra_client_create (int sock
)
2176 struct zserv
*client
;
2180 client
= XCALLOC (MTYPE_TMP
, sizeof (struct zserv
));
2182 /* Make client input/output buffer. */
2183 client
->sock
= sock
;
2184 client
->ibuf
= stream_new (ZEBRA_MAX_PACKET_SIZ
);
2185 client
->obuf
= stream_new (ZEBRA_MAX_PACKET_SIZ
);
2186 client
->wb
= buffer_new(0);
2188 /* Set table number. */
2189 client
->rtm_table
= zebrad
.rtm_table_default
;
2191 client
->connect_time
= monotime(NULL
);
2192 /* Initialize flags */
2193 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
2194 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
2195 client
->redist
[afi
][i
] = vrf_bitmap_init ();
2196 client
->redist_default
= vrf_bitmap_init ();
2197 client
->ifinfo
= vrf_bitmap_init ();
2198 client
->ridinfo
= vrf_bitmap_init ();
2200 /* by default, it's not a synchronous client */
2201 client
->is_synchronous
= 0;
2203 /* Add this client to linked list. */
2204 listnode_add (zebrad
.client_list
, client
);
2206 /* Make new read thread. */
2207 zebra_event (ZEBRA_READ
, sock
, client
);
2209 zebra_vrf_update_all (client
);
2212 /* Handler of zebra service request. */
2214 zebra_client_read (struct thread
*thread
)
2217 struct zserv
*client
;
2219 uint16_t length
, command
;
2220 uint8_t marker
, version
;
2222 struct zebra_vrf
*zvrf
;
2224 /* Get thread data. Reset reading thread because I'm running. */
2225 sock
= THREAD_FD (thread
);
2226 client
= THREAD_ARG (thread
);
2227 client
->t_read
= NULL
;
2229 if (client
->t_suicide
)
2231 zebra_client_close(client
);
2235 /* Read length and command (if we don't have it already). */
2236 if ((already
= stream_get_endp(client
->ibuf
)) < ZEBRA_HEADER_SIZE
)
2239 if (((nbyte
= stream_read_try (client
->ibuf
, sock
,
2240 ZEBRA_HEADER_SIZE
-already
)) == 0) ||
2243 if (IS_ZEBRA_DEBUG_EVENT
)
2244 zlog_debug ("connection closed socket [%d]", sock
);
2245 zebra_client_close (client
);
2248 if (nbyte
!= (ssize_t
)(ZEBRA_HEADER_SIZE
-already
))
2250 /* Try again later. */
2251 zebra_event (ZEBRA_READ
, sock
, client
);
2254 already
= ZEBRA_HEADER_SIZE
;
2257 /* Reset to read from the beginning of the incoming packet. */
2258 stream_set_getp(client
->ibuf
, 0);
2260 /* Fetch header values */
2261 length
= stream_getw (client
->ibuf
);
2262 marker
= stream_getc (client
->ibuf
);
2263 version
= stream_getc (client
->ibuf
);
2264 vrf_id
= stream_getw (client
->ibuf
);
2265 command
= stream_getw (client
->ibuf
);
2267 if (marker
!= ZEBRA_HEADER_MARKER
|| version
!= ZSERV_VERSION
)
2269 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
2270 __func__
, sock
, marker
, version
);
2271 zebra_client_close (client
);
2274 if (length
< ZEBRA_HEADER_SIZE
)
2276 zlog_warn("%s: socket %d message length %u is less than header size %d",
2277 __func__
, sock
, length
, ZEBRA_HEADER_SIZE
);
2278 zebra_client_close (client
);
2281 if (length
> STREAM_SIZE(client
->ibuf
))
2283 zlog_warn("%s: socket %d message length %u exceeds buffer size %lu",
2284 __func__
, sock
, length
, (u_long
)STREAM_SIZE(client
->ibuf
));
2285 zebra_client_close (client
);
2289 /* Read rest of data. */
2290 if (already
< length
)
2293 if (((nbyte
= stream_read_try (client
->ibuf
, sock
,
2294 length
-already
)) == 0) ||
2297 if (IS_ZEBRA_DEBUG_EVENT
)
2298 zlog_debug ("connection closed [%d] when reading zebra data", sock
);
2299 zebra_client_close (client
);
2302 if (nbyte
!= (ssize_t
)(length
-already
))
2304 /* Try again later. */
2305 zebra_event (ZEBRA_READ
, sock
, client
);
2310 length
-= ZEBRA_HEADER_SIZE
;
2312 /* Debug packet information. */
2313 if (IS_ZEBRA_DEBUG_EVENT
)
2314 zlog_debug ("zebra message comes from socket [%d]", sock
);
2316 if (IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
)
2317 zlog_debug ("zebra message received [%s] %d in VRF %u",
2318 zserv_command_string (command
), length
, vrf_id
);
2320 client
->last_read_time
= monotime(NULL
);
2321 client
->last_read_cmd
= command
;
2323 zvrf
= zebra_vrf_lookup_by_id (vrf_id
);
2326 if (IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
)
2327 zlog_debug ("zebra received unknown VRF[%u]", vrf_id
);
2328 goto zclient_read_out
;
2333 case ZEBRA_ROUTER_ID_ADD
:
2334 zread_router_id_add (client
, length
, zvrf
);
2336 case ZEBRA_ROUTER_ID_DELETE
:
2337 zread_router_id_delete (client
, length
, zvrf
);
2339 case ZEBRA_INTERFACE_ADD
:
2340 zread_interface_add (client
, length
, zvrf
);
2342 case ZEBRA_INTERFACE_DELETE
:
2343 zread_interface_delete (client
, length
, zvrf
);
2345 case ZEBRA_IPV4_ROUTE_ADD
:
2346 zread_ipv4_add (client
, length
, zvrf
);
2348 case ZEBRA_IPV4_ROUTE_DELETE
:
2349 zread_ipv4_delete (client
, length
, zvrf
);
2351 case ZEBRA_IPV4_ROUTE_IPV6_NEXTHOP_ADD
:
2352 zread_ipv4_route_ipv6_nexthop_add (client
, length
, zvrf
);
2354 case ZEBRA_IPV4_NEXTHOP_ADD
:
2355 zread_ipv4_add(client
, length
, zvrf
); /* LB: r1.0 merge - id was 1 */
2357 case ZEBRA_IPV4_NEXTHOP_DELETE
:
2358 zread_ipv4_delete(client
, length
, zvrf
); /* LB: r1.0 merge - id was 1 */
2360 case ZEBRA_IPV6_ROUTE_ADD
:
2361 zread_ipv6_add (client
, length
, zvrf
);
2363 case ZEBRA_IPV6_ROUTE_DELETE
:
2364 zread_ipv6_delete (client
, length
, zvrf
);
2366 case ZEBRA_REDISTRIBUTE_ADD
:
2367 zebra_redistribute_add (command
, client
, length
, zvrf
);
2369 case ZEBRA_REDISTRIBUTE_DELETE
:
2370 zebra_redistribute_delete (command
, client
, length
, zvrf
);
2372 case ZEBRA_REDISTRIBUTE_DEFAULT_ADD
:
2373 zebra_redistribute_default_add (command
, client
, length
, zvrf
);
2375 case ZEBRA_REDISTRIBUTE_DEFAULT_DELETE
:
2376 zebra_redistribute_default_delete (command
, client
, length
, zvrf
);
2378 case ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB
:
2379 zread_ipv4_nexthop_lookup_mrib (client
, length
, zvrf
);
2382 zread_hello (client
);
2384 case ZEBRA_NEXTHOP_REGISTER
:
2385 zserv_rnh_register(client
, sock
, length
, RNH_NEXTHOP_TYPE
, zvrf
);
2387 case ZEBRA_NEXTHOP_UNREGISTER
:
2388 zserv_rnh_unregister(client
, sock
, length
, RNH_NEXTHOP_TYPE
, zvrf
);
2390 case ZEBRA_IMPORT_ROUTE_REGISTER
:
2391 zserv_rnh_register(client
, sock
, length
, RNH_IMPORT_CHECK_TYPE
, zvrf
);
2393 case ZEBRA_IMPORT_ROUTE_UNREGISTER
:
2394 zserv_rnh_unregister(client
, sock
, length
, RNH_IMPORT_CHECK_TYPE
, zvrf
);
2396 case ZEBRA_BFD_DEST_UPDATE
:
2397 case ZEBRA_BFD_DEST_REGISTER
:
2398 zebra_ptm_bfd_dst_register(client
, sock
, length
, command
, zvrf
);
2400 case ZEBRA_BFD_DEST_DEREGISTER
:
2401 zebra_ptm_bfd_dst_deregister(client
, sock
, length
, zvrf
);
2403 case ZEBRA_VRF_UNREGISTER
:
2404 zread_vrf_unregister (client
, length
, zvrf
);
2406 case ZEBRA_BFD_CLIENT_REGISTER
:
2407 zebra_ptm_bfd_client_register(client
, sock
, length
);
2409 case ZEBRA_INTERFACE_ENABLE_RADV
:
2410 #if defined (HAVE_RTADV)
2411 zebra_interface_radv_set (client
, sock
, length
, zvrf
, 1);
2414 case ZEBRA_INTERFACE_DISABLE_RADV
:
2415 #if defined (HAVE_RTADV)
2416 zebra_interface_radv_set (client
, sock
, length
, zvrf
, 0);
2419 case ZEBRA_MPLS_LABELS_ADD
:
2420 case ZEBRA_MPLS_LABELS_DELETE
:
2421 zread_mpls_labels (command
, client
, length
, vrf_id
);
2423 case ZEBRA_IPMR_ROUTE_STATS
:
2424 zebra_ipmr_route_stats (client
, sock
, length
, zvrf
);
2426 case ZEBRA_LABEL_MANAGER_CONNECT
:
2427 case ZEBRA_GET_LABEL_CHUNK
:
2428 case ZEBRA_RELEASE_LABEL_CHUNK
:
2429 zread_label_manager_request (command
, client
, vrf_id
);
2431 case ZEBRA_FEC_REGISTER
:
2432 zserv_fec_register (client
, sock
, length
);
2434 case ZEBRA_FEC_UNREGISTER
:
2435 zserv_fec_unregister (client
, sock
, length
);
2438 zlog_info ("Zebra received unknown command %d", command
);
2442 if (client
->t_suicide
)
2444 /* No need to wait for thread callback, just kill immediately. */
2445 zebra_client_close(client
);
2450 stream_reset (client
->ibuf
);
2451 zebra_event (ZEBRA_READ
, sock
, client
);
2456 /* Accept code of zebra server socket. */
2458 zebra_accept (struct thread
*thread
)
2462 struct sockaddr_in client
;
2465 accept_sock
= THREAD_FD (thread
);
2467 /* Reregister myself. */
2468 zebra_event (ZEBRA_SERV
, accept_sock
, NULL
);
2470 len
= sizeof (struct sockaddr_in
);
2471 client_sock
= accept (accept_sock
, (struct sockaddr
*) &client
, &len
);
2473 if (client_sock
< 0)
2475 zlog_warn ("Can't accept zebra socket: %s", safe_strerror (errno
));
2479 /* Make client socket non-blocking. */
2480 set_nonblocking(client_sock
);
2482 /* Create new zebra client. */
2483 zebra_client_create (client_sock
);
2488 #ifdef HAVE_TCP_ZEBRA
2489 /* Make zebra's server socket. */
2495 struct sockaddr_in addr
;
2497 accept_sock
= socket (AF_INET
, SOCK_STREAM
, 0);
2499 if (accept_sock
< 0)
2501 zlog_warn ("Can't create zserv stream socket: %s",
2502 safe_strerror (errno
));
2503 zlog_warn ("zebra can't provice full functionality due to above error");
2507 memset (&addr
, 0, sizeof (struct sockaddr_in
));
2508 addr
.sin_family
= AF_INET
;
2509 addr
.sin_port
= htons (ZEBRA_PORT
);
2510 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
2511 addr
.sin_len
= sizeof (struct sockaddr_in
);
2512 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
2513 addr
.sin_addr
.s_addr
= htonl (INADDR_LOOPBACK
);
2515 sockopt_reuseaddr (accept_sock
);
2516 sockopt_reuseport (accept_sock
);
2518 if ( zserv_privs
.change(ZPRIVS_RAISE
) )
2519 zlog_err("Can't raise privileges");
2521 ret
= bind (accept_sock
, (struct sockaddr
*)&addr
,
2522 sizeof (struct sockaddr_in
));
2525 zlog_warn ("Can't bind to stream socket: %s",
2526 safe_strerror (errno
));
2527 zlog_warn ("zebra can't provice full functionality due to above error");
2528 close (accept_sock
); /* Avoid sd leak. */
2532 if ( zserv_privs
.change(ZPRIVS_LOWER
) )
2533 zlog_err("Can't lower privileges");
2535 ret
= listen (accept_sock
, 1);
2538 zlog_warn ("Can't listen to stream socket: %s",
2539 safe_strerror (errno
));
2540 zlog_warn ("zebra can't provice full functionality due to above error");
2541 close (accept_sock
); /* Avoid sd leak. */
2545 zebra_event (ZEBRA_SERV
, accept_sock
, NULL
);
2547 #else /* HAVE_TCP_ZEBRA */
2549 /* For sockaddr_un. */
2552 /* zebra server UNIX domain socket. */
2554 zebra_serv_un (const char *path
)
2558 struct sockaddr_un serv
;
2561 /* First of all, unlink existing socket */
2565 old_mask
= umask (0077);
2567 /* Make UNIX domain socket. */
2568 sock
= socket (AF_UNIX
, SOCK_STREAM
, 0);
2571 zlog_warn ("Can't create zserv unix socket: %s",
2572 safe_strerror (errno
));
2573 zlog_warn ("zebra can't provide full functionality due to above error");
2577 /* Make server socket. */
2578 memset (&serv
, 0, sizeof (struct sockaddr_un
));
2579 serv
.sun_family
= AF_UNIX
;
2580 strncpy (serv
.sun_path
, path
, strlen (path
));
2581 #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
2582 len
= serv
.sun_len
= SUN_LEN(&serv
);
2584 len
= sizeof (serv
.sun_family
) + strlen (serv
.sun_path
);
2585 #endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
2587 ret
= bind (sock
, (struct sockaddr
*) &serv
, len
);
2590 zlog_warn ("Can't bind to unix socket %s: %s",
2591 path
, safe_strerror (errno
));
2592 zlog_warn ("zebra can't provide full functionality due to above error");
2597 ret
= listen (sock
, 5);
2600 zlog_warn ("Can't listen to unix socket %s: %s",
2601 path
, safe_strerror (errno
));
2602 zlog_warn ("zebra can't provide full functionality due to above error");
2609 zebra_event (ZEBRA_SERV
, sock
, NULL
);
2611 #endif /* HAVE_TCP_ZEBRA */
2615 zebra_event (enum event event
, int sock
, struct zserv
*client
)
2620 thread_add_read(zebrad
.master
, zebra_accept
, client
, sock
, NULL
);
2623 client
->t_read
= NULL
;
2624 thread_add_read(zebrad
.master
, zebra_client_read
, client
, sock
,
2633 #define ZEBRA_TIME_BUF 32
2635 zserv_time_buf(time_t *time1
, char *buf
, int buflen
)
2640 assert (buf
!= NULL
);
2641 assert (buflen
>= ZEBRA_TIME_BUF
);
2642 assert (time1
!= NULL
);
2646 snprintf(buf
, buflen
, "never ");
2650 now
= monotime(NULL
);
2654 /* Making formatted timer strings. */
2655 #define ONE_DAY_SECOND 60*60*24
2656 #define ONE_WEEK_SECOND 60*60*24*7
2658 if (now
< ONE_DAY_SECOND
)
2659 snprintf (buf
, buflen
, "%02d:%02d:%02d",
2660 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
);
2661 else if (now
< ONE_WEEK_SECOND
)
2662 snprintf (buf
, buflen
, "%dd%02dh%02dm",
2663 tm
->tm_yday
, tm
->tm_hour
, tm
->tm_min
);
2665 snprintf (buf
, buflen
, "%02dw%dd%02dh",
2666 tm
->tm_yday
/7, tm
->tm_yday
- ((tm
->tm_yday
/7) * 7), tm
->tm_hour
);
2671 zebra_show_client_detail (struct vty
*vty
, struct zserv
*client
)
2673 char cbuf
[ZEBRA_TIME_BUF
], rbuf
[ZEBRA_TIME_BUF
];
2674 char wbuf
[ZEBRA_TIME_BUF
], nhbuf
[ZEBRA_TIME_BUF
], mbuf
[ZEBRA_TIME_BUF
];
2676 vty_out (vty
, "Client: %s", zebra_route_string(client
->proto
));
2677 if (client
->instance
)
2678 vty_out (vty
, " Instance: %d", client
->instance
);
2679 vty_out (vty
, "\n");
2681 vty_out (vty
, "------------------------ \n");
2682 vty_out (vty
, "FD: %d \n", client
->sock
);
2683 vty_out (vty
, "Route Table ID: %d \n", client
->rtm_table
);
2685 vty_out (vty
, "Connect Time: %s \n",
2686 zserv_time_buf(&client
->connect_time
, cbuf
, ZEBRA_TIME_BUF
));
2687 if (client
->nh_reg_time
)
2689 vty_out (vty
, "Nexthop Registry Time: %s \n",
2690 zserv_time_buf(&client
->nh_reg_time
, nhbuf
, ZEBRA_TIME_BUF
));
2691 if (client
->nh_last_upd_time
)
2692 vty_out (vty
, "Nexthop Last Update Time: %s \n",
2693 zserv_time_buf(&client
->nh_last_upd_time
, mbuf
, ZEBRA_TIME_BUF
));
2695 vty_out (vty
, "No Nexthop Update sent\n");
2698 vty_out (vty
, "Not registered for Nexthop Updates\n");
2700 vty_out (vty
, "Last Msg Rx Time: %s \n",
2701 zserv_time_buf(&client
->last_read_time
, rbuf
, ZEBRA_TIME_BUF
));
2702 vty_out (vty
, "Last Msg Tx Time: %s \n",
2703 zserv_time_buf(&client
->last_write_time
, wbuf
, ZEBRA_TIME_BUF
));
2704 if (client
->last_read_time
)
2705 vty_out (vty
, "Last Rcvd Cmd: %s \n",
2706 zserv_command_string(client
->last_read_cmd
));
2707 if (client
->last_write_time
)
2708 vty_out (vty
, "Last Sent Cmd: %s \n",
2709 zserv_command_string(client
->last_write_cmd
));
2710 vty_out (vty
, "\n");
2712 vty_out (vty
, "Type Add Update Del \n");
2713 vty_out (vty
, "================================================== \n");
2714 vty_out (vty
, "IPv4 %-12d%-12d%-12d\n", client
->v4_route_add_cnt
,
2715 client
->v4_route_upd8_cnt
, client
->v4_route_del_cnt
);
2716 vty_out (vty
, "IPv6 %-12d%-12d%-12d\n", client
->v6_route_add_cnt
,
2717 client
->v6_route_upd8_cnt
, client
->v6_route_del_cnt
);
2718 vty_out (vty
, "Redist:v4 %-12d%-12d%-12d\n", client
->redist_v4_add_cnt
, 0,
2719 client
->redist_v4_del_cnt
);
2720 vty_out (vty
, "Redist:v6 %-12d%-12d%-12d\n", client
->redist_v6_add_cnt
, 0,
2721 client
->redist_v6_del_cnt
);
2722 vty_out (vty
, "Connected %-12d%-12d%-12d\n", client
->ifadd_cnt
, 0,
2724 vty_out (vty
, "BFD peer %-12d%-12d%-12d\n", client
->bfd_peer_add_cnt
,
2725 client
->bfd_peer_upd8_cnt
, client
->bfd_peer_del_cnt
);
2726 vty_out (vty
, "Interface Up Notifications: %d\n",client
->ifup_cnt
);
2727 vty_out (vty
, "Interface Down Notifications: %d\n",client
->ifdown_cnt
);
2729 vty_out (vty
, "\n");
2734 zebra_show_client_brief (struct vty
*vty
, struct zserv
*client
)
2736 char cbuf
[ZEBRA_TIME_BUF
], rbuf
[ZEBRA_TIME_BUF
];
2737 char wbuf
[ZEBRA_TIME_BUF
];
2739 vty_out (vty
, "%-8s%12s %12s%12s%8d/%-8d%8d/%-8d\n",
2740 zebra_route_string(client
->proto
),
2741 zserv_time_buf(&client
->connect_time
, cbuf
, ZEBRA_TIME_BUF
),
2742 zserv_time_buf(&client
->last_read_time
, rbuf
, ZEBRA_TIME_BUF
),
2743 zserv_time_buf(&client
->last_write_time
, wbuf
, ZEBRA_TIME_BUF
),
2744 client
->v4_route_add_cnt
+client
->v4_route_upd8_cnt
,
2745 client
->v4_route_del_cnt
,
2746 client
->v6_route_add_cnt
+client
->v6_route_upd8_cnt
,
2747 client
->v6_route_del_cnt
);
2752 zebra_find_client (u_char proto
)
2754 struct listnode
*node
, *nnode
;
2755 struct zserv
*client
;
2757 for (ALL_LIST_ELEMENTS (zebrad
.client_list
, node
, nnode
, client
))
2759 if (client
->proto
== proto
)
2767 /* Display default rtm_table for all clients. */
2772 "default routing table to use for all clients\n")
2774 vty_out (vty
, "table %d\n",zebrad
.rtm_table_default
);
2778 DEFUN (config_table
,
2781 "Configure target kernel routing table\n"
2784 zebrad
.rtm_table_default
= strtol (argv
[1]->arg
, (char**)0, 10);
2788 DEFUN (no_config_table
,
2789 no_config_table_cmd
,
2790 "no table [TABLENO]",
2792 "Configure target kernel routing table\n"
2795 zebrad
.rtm_table_default
= 0;
2800 DEFUN (ip_forwarding
,
2804 "Turn on IP forwarding")
2810 ret
= ipforward_on ();
2814 vty_out (vty
, "Can't turn on IP forwarding\n");
2815 return CMD_WARNING_CONFIG_FAILED
;
2821 DEFUN (no_ip_forwarding
,
2822 no_ip_forwarding_cmd
,
2826 "Turn off IP forwarding")
2832 ret
= ipforward_off ();
2836 vty_out (vty
, "Can't turn off IP forwarding\n");
2837 return CMD_WARNING_CONFIG_FAILED
;
2847 "Zebra information\n")
2852 " Route Route Neighbor LSP LSP\n");
2854 "VRF Installs Removals Updates Installs Removals\n");
2855 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
)
2857 struct zebra_vrf
*zvrf
= vrf
->info
;
2858 vty_out (vty
,"%-25s %10" PRIu64
" %10" PRIu64
" %10" PRIu64
" %10" PRIu64
" %10" PRIu64
"\n",
2859 vrf
->name
, zvrf
->installs
, zvrf
->removals
,
2860 zvrf
->neigh_updates
, zvrf
->lsp_installs
,zvrf
->lsp_removals
);
2866 /* This command is for debugging purpose. */
2867 DEFUN (show_zebra_client
,
2868 show_zebra_client_cmd
,
2869 "show zebra client",
2871 "Zebra information\n"
2872 "Client information\n")
2874 struct listnode
*node
;
2875 struct zserv
*client
;
2877 for (ALL_LIST_ELEMENTS_RO (zebrad
.client_list
, node
, client
))
2878 zebra_show_client_detail(vty
, client
);
2883 /* This command is for debugging purpose. */
2884 DEFUN (show_zebra_client_summary
,
2885 show_zebra_client_summary_cmd
,
2886 "show zebra client summary",
2888 "Zebra information brief\n"
2889 "Client information brief\n"
2892 struct listnode
*node
;
2893 struct zserv
*client
;
2896 "Name Connect Time Last Read Last Write IPv4 Routes IPv6 Routes \n");
2898 "--------------------------------------------------------------------------------\n");
2900 for (ALL_LIST_ELEMENTS_RO (zebrad
.client_list
, node
, client
))
2901 zebra_show_client_brief(vty
, client
);
2903 vty_out (vty
, "Routes column shows (added+updated)/deleted\n");
2907 /* Table configuration write function. */
2909 config_write_table (struct vty
*vty
)
2911 if (zebrad
.rtm_table_default
)
2912 vty_out (vty
, "table %d\n",zebrad
.rtm_table_default
);
2916 /* table node for routing tables. */
2917 static struct cmd_node table_node
=
2920 "", /* This node has no interface. */
2924 /* Only display ip forwarding is enabled or not. */
2925 DEFUN (show_ip_forwarding
,
2926 show_ip_forwarding_cmd
,
2927 "show ip forwarding",
2930 "IP forwarding status\n")
2937 vty_out (vty
, "IP forwarding is off\n");
2939 vty_out (vty
, "IP forwarding is on\n");
2943 /* Only display ipv6 forwarding is enabled or not. */
2944 DEFUN (show_ipv6_forwarding
,
2945 show_ipv6_forwarding_cmd
,
2946 "show ipv6 forwarding",
2948 "IPv6 information\n"
2949 "Forwarding status\n")
2953 ret
= ipforward_ipv6 ();
2958 vty_out (vty
, "ipv6 forwarding is unknown\n");
2961 vty_out (vty
, "ipv6 forwarding is %s\n", "off");
2964 vty_out (vty
, "ipv6 forwarding is %s\n", "on");
2967 vty_out (vty
, "ipv6 forwarding is %s\n", "off");
2973 DEFUN (ipv6_forwarding
,
2974 ipv6_forwarding_cmd
,
2977 "Turn on IPv6 forwarding")
2981 ret
= ipforward_ipv6 ();
2983 ret
= ipforward_ipv6_on ();
2987 vty_out (vty
, "Can't turn on IPv6 forwarding\n");
2988 return CMD_WARNING_CONFIG_FAILED
;
2994 DEFUN (no_ipv6_forwarding
,
2995 no_ipv6_forwarding_cmd
,
2996 "no ipv6 forwarding",
2999 "Turn off IPv6 forwarding")
3003 ret
= ipforward_ipv6 ();
3005 ret
= ipforward_ipv6_off ();
3009 vty_out (vty
, "Can't turn off IPv6 forwarding\n");
3010 return CMD_WARNING_CONFIG_FAILED
;
3016 /* IPForwarding configuration write function. */
3018 config_write_forwarding (struct vty
*vty
)
3020 /* FIXME: Find better place for that. */
3021 router_id_write (vty
);
3024 vty_out (vty
, "no ip forwarding\n");
3025 if (!ipforward_ipv6 ())
3026 vty_out (vty
, "no ipv6 forwarding\n");
3027 vty_out (vty
, "!\n");
3031 /* table node for routing tables. */
3032 static struct cmd_node forwarding_node
=
3035 "", /* This node has no interface. */
3039 /* Initialisation of zebra and installation of commands. */
3043 /* Client list init. */
3044 zebrad
.client_list
= list_new ();
3046 /* Install configuration write function. */
3047 install_node (&table_node
, config_write_table
);
3048 install_node (&forwarding_node
, config_write_forwarding
);
3050 install_element (VIEW_NODE
, &show_ip_forwarding_cmd
);
3051 install_element (CONFIG_NODE
, &ip_forwarding_cmd
);
3052 install_element (CONFIG_NODE
, &no_ip_forwarding_cmd
);
3053 install_element (ENABLE_NODE
, &show_zebra_cmd
);
3054 install_element (ENABLE_NODE
, &show_zebra_client_cmd
);
3055 install_element (ENABLE_NODE
, &show_zebra_client_summary_cmd
);
3058 install_element (VIEW_NODE
, &show_table_cmd
);
3059 install_element (CONFIG_NODE
, &config_table_cmd
);
3060 install_element (CONFIG_NODE
, &no_config_table_cmd
);
3061 #endif /* HAVE_NETLINK */
3063 install_element (VIEW_NODE
, &show_ipv6_forwarding_cmd
);
3064 install_element (CONFIG_NODE
, &ipv6_forwarding_cmd
);
3065 install_element (CONFIG_NODE
, &no_ipv6_forwarding_cmd
);
3068 zebra_route_map_init ();
3071 /* Make zebra server socket, wiping any existing one (see bug #403). */
3073 zebra_zserv_socket_init (char *path
)
3075 #ifdef HAVE_TCP_ZEBRA
3078 zebra_serv_un (path
? path
: ZEBRA_SERV_PATH
);
3079 #endif /* HAVE_TCP_ZEBRA */