1 /* Zebra daemon server routine.
2 * Copyright (C) 1997, 98, 99 Kunihiro Ishiguro
4 * This file is part of GNU Zebra.
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
29 #include "zebra_memory.h"
33 #include "sockunion.h"
42 #include "zebra/zserv.h"
43 #include "zebra/zebra_ns.h"
44 #include "zebra/zebra_vrf.h"
45 #include "zebra/router-id.h"
46 #include "zebra/redistribute.h"
47 #include "zebra/debug.h"
48 #include "zebra/ipforward.h"
49 #include "zebra/zebra_rnh.h"
50 #include "zebra/rt_netlink.h"
51 #include "zebra/interface.h"
52 #include "zebra/zebra_ptm.h"
53 #include "zebra/rtadv.h"
54 #include "zebra/zebra_mpls.h"
55 #include "zebra/zebra_mroute.h"
56 #include "zebra/label_manager.h"
57 #include "zebra/zebra_vxlan.h"
59 /* Event list of zebra. */
60 enum event
{ ZEBRA_SERV
, ZEBRA_READ
, ZEBRA_WRITE
};
62 static void zebra_event (enum event event
, int sock
, struct zserv
*client
);
64 extern struct zebra_privs_t zserv_privs
;
66 static void zebra_client_close (struct zserv
*client
);
69 zserv_delayed_close(struct thread
*thread
)
71 struct zserv
*client
= THREAD_ARG(thread
);
73 client
->t_suicide
= NULL
;
74 zebra_client_close(client
);
79 zserv_flush_data(struct thread
*thread
)
81 struct zserv
*client
= THREAD_ARG(thread
);
83 client
->t_write
= NULL
;
84 if (client
->t_suicide
)
86 zebra_client_close(client
);
89 switch (buffer_flush_available(client
->wb
, client
->sock
))
92 zlog_warn("%s: buffer_flush_available failed on zserv client fd %d, "
93 "closing", __func__
, client
->sock
);
94 zebra_client_close(client
);
98 client
->t_write
= NULL
;
99 thread_add_write(zebrad
.master
, zserv_flush_data
, client
, client
->sock
,
107 client
->last_write_time
= monotime(NULL
);
112 zebra_server_send_message(struct zserv
*client
)
114 if (client
->t_suicide
)
117 if (client
->is_synchronous
)
120 stream_set_getp(client
->obuf
, 0);
121 client
->last_write_cmd
= stream_getw_from(client
->obuf
, 6);
122 switch (buffer_write(client
->wb
, client
->sock
, STREAM_DATA(client
->obuf
),
123 stream_get_endp(client
->obuf
)))
126 zlog_warn("%s: buffer_write failed to zserv client fd %d, closing",
127 __func__
, client
->sock
);
128 /* Schedule a delayed close since many of the functions that call this
129 one do not check the return code. They do not allow for the
130 possibility that an I/O error may have caused the client to be
132 client
->t_suicide
= NULL
;
133 thread_add_event(zebrad
.master
, zserv_delayed_close
, client
, 0,
137 THREAD_OFF(client
->t_write
);
140 thread_add_write(zebrad
.master
, zserv_flush_data
, client
, client
->sock
,
145 client
->last_write_time
= monotime(NULL
);
150 zserv_create_header (struct stream
*s
, uint16_t cmd
, vrf_id_t vrf_id
)
152 /* length placeholder, caller can update */
153 stream_putw (s
, ZEBRA_HEADER_SIZE
);
154 stream_putc (s
, ZEBRA_HEADER_MARKER
);
155 stream_putc (s
, ZSERV_VERSION
);
156 stream_putw (s
, vrf_id
);
157 stream_putw (s
, cmd
);
161 zserv_encode_interface (struct stream
*s
, struct interface
*ifp
)
163 /* Interface information. */
164 stream_put (s
, ifp
->name
, INTERFACE_NAMSIZ
);
165 stream_putl (s
, ifp
->ifindex
);
166 stream_putc (s
, ifp
->status
);
167 stream_putq (s
, ifp
->flags
);
168 stream_putc (s
, ifp
->ptm_enable
);
169 stream_putc (s
, ifp
->ptm_status
);
170 stream_putl (s
, ifp
->metric
);
171 stream_putl (s
, ifp
->speed
);
172 stream_putl (s
, ifp
->mtu
);
173 stream_putl (s
, ifp
->mtu6
);
174 stream_putl (s
, ifp
->bandwidth
);
175 stream_putl (s
, ifp
->ll_type
);
176 stream_putl (s
, ifp
->hw_addr_len
);
177 if (ifp
->hw_addr_len
)
178 stream_put (s
, ifp
->hw_addr
, ifp
->hw_addr_len
);
180 /* Then, Traffic Engineering parameters if any */
181 if (HAS_LINK_PARAMS(ifp
) && IS_LINK_PARAMS_SET(ifp
->link_params
))
184 zebra_interface_link_params_write (s
, ifp
);
189 /* Write packet size. */
190 stream_putw_at (s
, 0, stream_get_endp (s
));
194 zserv_encode_vrf (struct stream
*s
, struct zebra_vrf
*zvrf
)
196 struct vrf_data data
;
198 data
.l
.table_id
= zvrf
->table_id
;
199 /* Pass the tableid */
200 stream_put (s
, &data
, sizeof (struct vrf_data
));
201 /* Interface information. */
202 stream_put (s
, zvrf_name (zvrf
), VRF_NAMSIZ
);
204 /* Write packet size. */
205 stream_putw_at (s
, 0, stream_get_endp (s
));
208 /* Interface is added. Send ZEBRA_INTERFACE_ADD to client. */
210 * This function is called in the following situations:
211 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
213 * - at startup, when zebra figures out the available interfaces
214 * - when an interface is added (where support for
215 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
216 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
220 zsend_interface_add (struct zserv
*client
, struct interface
*ifp
)
227 zserv_create_header (s
, ZEBRA_INTERFACE_ADD
, ifp
->vrf_id
);
228 zserv_encode_interface (s
, ifp
);
231 return zebra_server_send_message(client
);
234 /* Interface deletion from zebra daemon. */
236 zsend_interface_delete (struct zserv
*client
, struct interface
*ifp
)
243 zserv_create_header (s
, ZEBRA_INTERFACE_DELETE
, ifp
->vrf_id
);
244 zserv_encode_interface (s
, ifp
);
247 return zebra_server_send_message (client
);
251 zsend_vrf_add (struct zserv
*client
, struct zebra_vrf
*zvrf
)
258 zserv_create_header (s
, ZEBRA_VRF_ADD
, zvrf_id (zvrf
));
259 zserv_encode_vrf (s
, zvrf
);
261 client
->vrfadd_cnt
++;
262 return zebra_server_send_message(client
);
265 /* VRF deletion from zebra daemon. */
267 zsend_vrf_delete (struct zserv
*client
, struct zebra_vrf
*zvrf
)
274 zserv_create_header (s
, ZEBRA_VRF_DELETE
, zvrf_id (zvrf
));
275 zserv_encode_vrf (s
, zvrf
);
277 client
->vrfdel_cnt
++;
278 return zebra_server_send_message (client
);
282 zsend_interface_link_params (struct zserv
*client
, struct interface
*ifp
)
286 /* Check this client need interface information. */
287 if (! client
->ifinfo
)
290 if (!ifp
->link_params
)
295 zserv_create_header (s
, ZEBRA_INTERFACE_LINK_PARAMS
, ifp
->vrf_id
);
297 /* Add Interface Index */
298 stream_putl (s
, ifp
->ifindex
);
300 /* Then TE Link Parameters */
301 if (zebra_interface_link_params_write (s
, ifp
) == 0)
304 /* Write packet size. */
305 stream_putw_at (s
, 0, stream_get_endp (s
));
307 return zebra_server_send_message (client
);
310 /* Interface address is added/deleted. Send ZEBRA_INTERFACE_ADDRESS_ADD or
311 * ZEBRA_INTERFACE_ADDRESS_DELETE to the client.
313 * A ZEBRA_INTERFACE_ADDRESS_ADD is sent in the following situations:
314 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
315 * from the client, after the ZEBRA_INTERFACE_ADD has been
316 * sent from zebra to the client
317 * - redistribute new address info to all clients in the following situations
318 * - at startup, when zebra figures out the available interfaces
319 * - when an interface is added (where support for
320 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
321 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
323 * - for the vty commands "ip address A.B.C.D/M [<secondary>|<label LINE>]"
324 * and "no bandwidth <1-10000000>", "ipv6 address X:X::X:X/M"
325 * - when an RTM_NEWADDR message is received from the kernel,
327 * The call tree that triggers ZEBRA_INTERFACE_ADDRESS_DELETE:
329 * zsend_interface_address(DELETE)
332 * zebra_interface_address_delete_update
334 * | | if_delete_update
336 * ip_address_uninstall connected_delete_ipv4
337 * [ipv6_addresss_uninstall] [connected_delete_ipv6]
340 * | RTM_NEWADDR on routing/netlink socket
343 * "no ip address A.B.C.D/M [label LINE]"
344 * "no ip address A.B.C.D/M secondary"
345 * ["no ipv6 address X:X::X:X/M"]
349 zsend_interface_address (int cmd
, struct zserv
*client
,
350 struct interface
*ifp
, struct connected
*ifc
)
359 zserv_create_header (s
, cmd
, ifp
->vrf_id
);
360 stream_putl (s
, ifp
->ifindex
);
362 /* Interface address flag. */
363 stream_putc (s
, ifc
->flags
);
365 /* Prefix information. */
367 stream_putc (s
, p
->family
);
368 blen
= prefix_blen (p
);
369 stream_put (s
, &p
->u
.prefix
, blen
);
372 * XXX gnu version does not send prefixlen for ZEBRA_INTERFACE_ADDRESS_DELETE
373 * but zebra_interface_address_delete_read() in the gnu version
376 stream_putc (s
, p
->prefixlen
);
379 p
= ifc
->destination
;
381 stream_put (s
, &p
->u
.prefix
, blen
);
383 stream_put (s
, NULL
, blen
);
385 /* Write packet size. */
386 stream_putw_at (s
, 0, stream_get_endp (s
));
388 client
->connected_rt_add_cnt
++;
389 return zebra_server_send_message(client
);
393 zsend_interface_nbr_address (int cmd
, struct zserv
*client
,
394 struct interface
*ifp
, struct nbr_connected
*ifc
)
403 zserv_create_header (s
, cmd
, ifp
->vrf_id
);
404 stream_putl (s
, ifp
->ifindex
);
406 /* Prefix information. */
408 stream_putc (s
, p
->family
);
409 blen
= prefix_blen (p
);
410 stream_put (s
, &p
->u
.prefix
, blen
);
413 * XXX gnu version does not send prefixlen for ZEBRA_INTERFACE_ADDRESS_DELETE
414 * but zebra_interface_address_delete_read() in the gnu version
417 stream_putc (s
, p
->prefixlen
);
419 /* Write packet size. */
420 stream_putw_at (s
, 0, stream_get_endp (s
));
422 return zebra_server_send_message(client
);
425 /* Interface address addition. */
427 zebra_interface_nbr_address_add_update (struct interface
*ifp
,
428 struct nbr_connected
*ifc
)
430 struct listnode
*node
, *nnode
;
431 struct zserv
*client
;
434 if (IS_ZEBRA_DEBUG_EVENT
)
436 char buf
[INET6_ADDRSTRLEN
];
439 zlog_debug ("MESSAGE: ZEBRA_INTERFACE_NBR_ADDRESS_ADD %s/%d on %s",
440 inet_ntop (p
->family
, &p
->u
.prefix
, buf
, INET6_ADDRSTRLEN
),
441 p
->prefixlen
, ifc
->ifp
->name
);
444 for (ALL_LIST_ELEMENTS (zebrad
.client_list
, node
, nnode
, client
))
445 zsend_interface_nbr_address (ZEBRA_INTERFACE_NBR_ADDRESS_ADD
, client
, ifp
, ifc
);
448 /* Interface address deletion. */
450 zebra_interface_nbr_address_delete_update (struct interface
*ifp
,
451 struct nbr_connected
*ifc
)
453 struct listnode
*node
, *nnode
;
454 struct zserv
*client
;
457 if (IS_ZEBRA_DEBUG_EVENT
)
459 char buf
[INET6_ADDRSTRLEN
];
462 zlog_debug ("MESSAGE: ZEBRA_INTERFACE_NBR_ADDRESS_DELETE %s/%d on %s",
463 inet_ntop (p
->family
, &p
->u
.prefix
, buf
, INET6_ADDRSTRLEN
),
464 p
->prefixlen
, ifc
->ifp
->name
);
467 for (ALL_LIST_ELEMENTS (zebrad
.client_list
, node
, nnode
, client
))
468 zsend_interface_nbr_address (ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
, client
, ifp
, ifc
);
471 /* Send addresses on interface to client */
473 zsend_interface_addresses (struct zserv
*client
, struct interface
*ifp
)
475 struct listnode
*cnode
, *cnnode
;
477 struct nbr_connected
*nc
;
479 /* Send interface addresses. */
480 for (ALL_LIST_ELEMENTS (ifp
->connected
, cnode
, cnnode
, c
))
482 if (!CHECK_FLAG (c
->conf
, ZEBRA_IFC_REAL
))
485 if (zsend_interface_address (ZEBRA_INTERFACE_ADDRESS_ADD
, client
,
490 /* Send interface neighbors. */
491 for (ALL_LIST_ELEMENTS (ifp
->nbr_connected
, cnode
, cnnode
, nc
))
493 if (zsend_interface_nbr_address (ZEBRA_INTERFACE_NBR_ADDRESS_ADD
,
494 client
, ifp
, nc
) < 0)
501 /* Notify client about interface moving from one VRF to another.
502 * Whether client is interested in old and new VRF is checked by caller.
505 zsend_interface_vrf_update (struct zserv
*client
, struct interface
*ifp
,
513 zserv_create_header (s
, ZEBRA_INTERFACE_VRF_UPDATE
, ifp
->vrf_id
);
515 /* Fill in the ifIndex of the interface and its new VRF (id) */
516 stream_putl (s
, ifp
->ifindex
);
517 stream_putw (s
, vrf_id
);
519 /* Write packet size. */
520 stream_putw_at (s
, 0, stream_get_endp (s
));
522 client
->if_vrfchg_cnt
++;
523 return zebra_server_send_message(client
);
526 /* Add new nbr connected IPv6 address */
528 nbr_connected_add_ipv6 (struct interface
*ifp
, struct in6_addr
*address
)
530 struct nbr_connected
*ifc
;
534 IPV6_ADDR_COPY (&p
.u
.prefix
, address
);
535 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
537 if (!(ifc
= listnode_head(ifp
->nbr_connected
)))
540 ifc
= nbr_connected_new ();
541 ifc
->address
= prefix_new();
543 listnode_add (ifp
->nbr_connected
, ifc
);
546 prefix_copy(ifc
->address
, &p
);
548 zebra_interface_nbr_address_add_update (ifp
, ifc
);
550 if_nbr_ipv6ll_to_ipv4ll_neigh_update (ifp
, address
, 1);
554 nbr_connected_delete_ipv6 (struct interface
*ifp
, struct in6_addr
*address
)
556 struct nbr_connected
*ifc
;
560 IPV6_ADDR_COPY (&p
.u
.prefix
, address
);
561 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
563 ifc
= nbr_connected_check(ifp
, &p
);
567 listnode_delete (ifp
->nbr_connected
, ifc
);
569 zebra_interface_nbr_address_delete_update (ifp
, ifc
);
571 if_nbr_ipv6ll_to_ipv4ll_neigh_update (ifp
, address
, 0);
573 nbr_connected_free (ifc
);
577 * The cmd passed to zsend_interface_update may be ZEBRA_INTERFACE_UP or
578 * ZEBRA_INTERFACE_DOWN.
580 * The ZEBRA_INTERFACE_UP message is sent from the zebra server to
581 * the clients in one of 2 situations:
582 * - an if_up is detected e.g., as a result of an RTM_IFINFO message
583 * - a vty command modifying the bandwidth of an interface is received.
584 * The ZEBRA_INTERFACE_DOWN message is sent when an if_down is detected.
587 zsend_interface_update (int cmd
, struct zserv
*client
, struct interface
*ifp
)
594 zserv_create_header (s
, cmd
, ifp
->vrf_id
);
595 zserv_encode_interface (s
, ifp
);
597 if (cmd
== ZEBRA_INTERFACE_UP
)
600 client
->ifdown_cnt
++;
602 return zebra_server_send_message(client
);
606 * This is the new function to announce and withdraw redistributed routes, used
607 * by Zebra. This is the old zsend_route_multipath() function. That function
608 * was duplicating code to send a lot of information that was essentially thrown
609 * away or ignored by the receiver. This is the leaner function that is not a
610 * duplicate of the zapi_ipv4_route_add/del.
612 * The primary difference is that this function merely sends a single NH instead of
616 zsend_redistribute_route (int add
, struct zserv
*client
, struct prefix
*p
,
617 struct prefix
*src_p
, struct route_entry
*re
)
623 struct nexthop
*nexthop
;
624 unsigned long nhnummark
= 0, messmark
= 0;
626 u_char zapi_flags
= 0;
627 struct nexthop dummy_nh
;
629 afi
= family2afi (p
->family
);
635 cmd
= ZEBRA_REDISTRIBUTE_IPV4_ADD
;
636 client
->redist_v4_add_cnt
++;
639 cmd
= ZEBRA_REDISTRIBUTE_IPV6_ADD
;
640 client
->redist_v6_add_cnt
++;
651 cmd
= ZEBRA_REDISTRIBUTE_IPV4_DEL
;
652 client
->redist_v4_del_cnt
++;
655 cmd
= ZEBRA_REDISTRIBUTE_IPV6_DEL
;
656 client
->redist_v6_del_cnt
++;
665 memset(&dummy_nh
, 0, sizeof(struct nexthop
));
667 zserv_create_header (s
, cmd
, re
->vrf_id
);
669 /* Put type and nexthop. */
670 stream_putc (s
, re
->type
);
671 stream_putw (s
, re
->instance
);
672 stream_putl (s
, re
->flags
);
674 /* marker for message flags field */
675 messmark
= stream_get_endp (s
);
679 psize
= PSIZE (p
->prefixlen
);
680 stream_putc (s
, p
->prefixlen
);
681 stream_write (s
, (u_char
*) & p
->u
.prefix
, psize
);
685 SET_FLAG (zapi_flags
, ZAPI_MESSAGE_SRCPFX
);
686 psize
= PSIZE (src_p
->prefixlen
);
687 stream_putc (s
, src_p
->prefixlen
);
688 stream_write (s
, (u_char
*) & src_p
->u
.prefix
, psize
);
691 for (nexthop
= re
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
693 /* We don't send any nexthops when there's a multipath */
694 if (re
->nexthop_active_num
> 1 && client
->proto
!= ZEBRA_ROUTE_LDP
)
696 SET_FLAG (zapi_flags
, ZAPI_MESSAGE_NEXTHOP
);
697 SET_FLAG (zapi_flags
, ZAPI_MESSAGE_IFINDEX
);
700 if (p
->family
== AF_INET
)
702 stream_put_in_addr (s
, &dummy_nh
.gate
.ipv4
);
704 else if (p
->family
== AF_INET6
)
706 stream_write (s
, (u_char
*) &dummy_nh
.gate
.ipv6
, 16);
710 /* We don't handle anything else now, abort */
711 zlog_err("%s: Unable to redistribute route of unknown family, %d\n",
712 __func__
, p
->family
);
716 stream_putl (s
, 0); /* dummy ifindex */
720 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
722 SET_FLAG (zapi_flags
, ZAPI_MESSAGE_NEXTHOP
);
723 SET_FLAG (zapi_flags
, ZAPI_MESSAGE_IFINDEX
);
726 nhnummark
= stream_get_endp (s
);
727 stream_putc (s
, 1); /* placeholder */
731 switch(nexthop
->type
)
733 case NEXTHOP_TYPE_IPV4
:
734 case NEXTHOP_TYPE_IPV4_IFINDEX
:
735 stream_put_in_addr (s
, &nexthop
->gate
.ipv4
);
737 case NEXTHOP_TYPE_IPV6
:
738 case NEXTHOP_TYPE_IPV6_IFINDEX
:
739 /* Only BGP supports IPv4 prefix with IPv6 NH, so kill this */
740 if (p
->family
== AF_INET
)
741 stream_put_in_addr(s
, &dummy_nh
.gate
.ipv4
);
743 stream_write (s
, (u_char
*) &nexthop
->gate
.ipv6
, 16);
746 if (cmd
== ZEBRA_REDISTRIBUTE_IPV4_ADD
747 || cmd
== ZEBRA_REDISTRIBUTE_IPV4_DEL
)
749 struct in_addr empty
;
750 memset (&empty
, 0, sizeof (struct in_addr
));
751 stream_write (s
, (u_char
*) &empty
, IPV4_MAX_BYTELEN
);
755 struct in6_addr empty
;
756 memset (&empty
, 0, sizeof (struct in6_addr
));
757 stream_write (s
, (u_char
*) &empty
, IPV6_MAX_BYTELEN
);
761 /* Interface index. */
763 stream_putl (s
, nexthop
->ifindex
);
765 /* ldpd needs all nexthops */
766 if (client
->proto
!= ZEBRA_ROUTE_LDP
)
772 SET_FLAG (zapi_flags
, ZAPI_MESSAGE_DISTANCE
);
773 stream_putc (s
, re
->distance
);
776 SET_FLAG (zapi_flags
, ZAPI_MESSAGE_METRIC
);
777 stream_putl (s
, re
->metric
);
782 SET_FLAG(zapi_flags
, ZAPI_MESSAGE_TAG
);
783 stream_putl(s
, re
->tag
);
787 SET_FLAG (zapi_flags
, ZAPI_MESSAGE_MTU
);
788 stream_putl (s
, re
->mtu
);
790 /* write real message flags value */
791 stream_putc_at (s
, messmark
, zapi_flags
);
793 /* Write next-hop number */
795 stream_putc_at (s
, nhnummark
, nhnum
);
797 /* Write packet size. */
798 stream_putw_at (s
, 0, stream_get_endp (s
));
800 return zebra_server_send_message(client
);
804 zsend_write_nexthop (struct stream
*s
, struct nexthop
*nexthop
)
806 stream_putc (s
, nexthop
->type
);
807 switch (nexthop
->type
)
809 case NEXTHOP_TYPE_IPV4
:
810 case NEXTHOP_TYPE_IPV4_IFINDEX
:
811 stream_put_in_addr (s
, &nexthop
->gate
.ipv4
);
812 stream_putl (s
, nexthop
->ifindex
);
814 case NEXTHOP_TYPE_IPV6
:
815 stream_put (s
, &nexthop
->gate
.ipv6
, 16);
817 case NEXTHOP_TYPE_IPV6_IFINDEX
:
818 stream_put (s
, &nexthop
->gate
.ipv6
, 16);
819 stream_putl (s
, nexthop
->ifindex
);
821 case NEXTHOP_TYPE_IFINDEX
:
822 stream_putl (s
, nexthop
->ifindex
);
831 /* Nexthop register */
833 zserv_rnh_register (struct zserv
*client
, int sock
, u_short length
,
834 rnh_type_t type
, struct zebra_vrf
*zvrf
)
842 if (IS_ZEBRA_DEBUG_NHT
)
843 zlog_debug("rnh_register msg from client %s: length=%d, type=%s\n",
844 zebra_route_string(client
->proto
), length
,
845 (type
== RNH_NEXTHOP_TYPE
) ? "nexthop" : "route");
849 client
->nh_reg_time
= monotime(NULL
);
853 flags
= stream_getc(s
);
854 p
.family
= stream_getw(s
);
855 p
.prefixlen
= stream_getc(s
);
857 if (p
.family
== AF_INET
)
859 p
.u
.prefix4
.s_addr
= stream_get_ipv4(s
);
860 l
+= IPV4_MAX_BYTELEN
;
862 else if (p
.family
== AF_INET6
)
864 stream_get(&p
.u
.prefix6
, s
, IPV6_MAX_BYTELEN
);
865 l
+= IPV6_MAX_BYTELEN
;
869 zlog_err("rnh_register: Received unknown family type %d\n",
873 rnh
= zebra_add_rnh(&p
, zvrf_id (zvrf
), type
);
874 if (type
== RNH_NEXTHOP_TYPE
)
876 if (flags
&& !CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
))
877 SET_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
);
878 else if (!flags
&& CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
))
879 UNSET_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
);
881 else if (type
== RNH_IMPORT_CHECK_TYPE
)
883 if (flags
&& !CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
))
884 SET_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
);
885 else if (!flags
&& CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
))
886 UNSET_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
);
889 zebra_add_rnh_client(rnh
, client
, type
, zvrf_id (zvrf
));
890 /* Anything not AF_INET/INET6 has been filtered out above */
891 zebra_evaluate_rnh(zvrf_id (zvrf
), p
.family
, 1, type
, &p
);
896 /* Nexthop register */
898 zserv_rnh_unregister (struct zserv
*client
, int sock
, u_short length
,
899 rnh_type_t type
, struct zebra_vrf
*zvrf
)
906 if (IS_ZEBRA_DEBUG_NHT
)
907 zlog_debug("rnh_unregister msg from client %s: length=%d\n",
908 zebra_route_string(client
->proto
), length
);
914 (void)stream_getc(s
); //Connected or not. Not used in this function
915 p
.family
= stream_getw(s
);
916 p
.prefixlen
= stream_getc(s
);
918 if (p
.family
== AF_INET
)
920 p
.u
.prefix4
.s_addr
= stream_get_ipv4(s
);
921 l
+= IPV4_MAX_BYTELEN
;
923 else if (p
.family
== AF_INET6
)
925 stream_get(&p
.u
.prefix6
, s
, IPV6_MAX_BYTELEN
);
926 l
+= IPV6_MAX_BYTELEN
;
930 zlog_err("rnh_register: Received unknown family type %d\n",
934 rnh
= zebra_lookup_rnh(&p
, zvrf_id (zvrf
), type
);
937 client
->nh_dereg_time
= monotime(NULL
);
938 zebra_remove_rnh_client(rnh
, client
, type
);
944 #define ZEBRA_MIN_FEC_LENGTH 5
948 zserv_fec_register (struct zserv
*client
, int sock
, u_short length
)
951 struct zebra_vrf
*zvrf
;
955 u_int32_t label_index
= MPLS_INVALID_LABEL_INDEX
;
958 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
960 return 0; // unexpected
963 * The minimum amount of data that can be sent for one fec
966 if (length
< ZEBRA_MIN_FEC_LENGTH
)
968 zlog_err ("fec_register: Received a fec register of length %d, it is of insufficient size to properly decode",
975 flags
= stream_getw(s
);
976 p
.family
= stream_getw(s
);
977 if (p
.family
!= AF_INET
&&
978 p
.family
!= AF_INET6
)
980 zlog_err ("fec_register: Received unknown family type %d\n",
984 p
.prefixlen
= stream_getc(s
);
986 stream_get(&p
.u
.prefix
, s
, PSIZE(p
.prefixlen
));
987 l
+= PSIZE(p
.prefixlen
);
988 if (flags
& ZEBRA_FEC_REGISTER_LABEL_INDEX
)
990 label_index
= stream_getl(s
);
994 label_index
= MPLS_INVALID_LABEL_INDEX
;
995 zebra_mpls_fec_register (zvrf
, &p
, label_index
, client
);
1001 /* FEC unregister */
1003 zserv_fec_unregister (struct zserv
*client
, int sock
, u_short length
)
1006 struct zebra_vrf
*zvrf
;
1012 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1014 return 0; // unexpected
1017 * The minimum amount of data that can be sent for one
1018 * fec unregistration
1020 if (length
< ZEBRA_MIN_FEC_LENGTH
)
1022 zlog_err ("fec_unregister: Received a fec unregister of length %d, it is of insufficient size to properly decode",
1029 //flags = stream_getw(s);
1030 (void)stream_getw(s
);
1031 p
.family
= stream_getw(s
);
1032 if (p
.family
!= AF_INET
&&
1033 p
.family
!= AF_INET6
)
1035 zlog_err ("fec_unregister: Received unknown family type %d\n",
1039 p
.prefixlen
= stream_getc(s
);
1041 stream_get(&p
.u
.prefix
, s
, PSIZE(p
.prefixlen
));
1042 l
+= PSIZE(p
.prefixlen
);
1043 zebra_mpls_fec_unregister (zvrf
, &p
, client
);
1050 Modified version of zsend_ipv4_nexthop_lookup():
1051 Query unicast rib if nexthop is not found on mrib.
1052 Returns both route metric and protocol distance.
1055 zsend_ipv4_nexthop_lookup_mrib (struct zserv
*client
, struct in_addr addr
, struct route_entry
*re
, struct zebra_vrf
*zvrf
)
1060 struct nexthop
*nexthop
;
1062 /* Get output stream. */
1066 /* Fill in result. */
1067 zserv_create_header (s
, ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB
, zvrf_id (zvrf
));
1068 stream_put_in_addr (s
, &addr
);
1072 stream_putc (s
, re
->distance
);
1073 stream_putl (s
, re
->metric
);
1075 nump
= stream_get_endp(s
); /* remember position for nexthop_num */
1076 stream_putc (s
, 0); /* reserve room for nexthop_num */
1077 /* Only non-recursive routes are elegible to resolve the nexthop we
1078 * are looking up. Therefore, we will just iterate over the top
1079 * chain of nexthops. */
1080 for (nexthop
= re
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
1081 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
1082 num
+= zsend_write_nexthop (s
, nexthop
);
1084 stream_putc_at (s
, nump
, num
); /* store nexthop_num */
1088 stream_putc (s
, 0); /* distance */
1089 stream_putl (s
, 0); /* metric */
1090 stream_putc (s
, 0); /* nexthop_num */
1093 stream_putw_at (s
, 0, stream_get_endp (s
));
1095 return zebra_server_send_message(client
);
1098 /* Router-id is updated. Send ZEBRA_ROUTER_ID_ADD to client. */
1100 zsend_router_id_update (struct zserv
*client
, struct prefix
*p
,
1106 /* Check this client need interface information. */
1107 if (! vrf_bitmap_check (client
->ridinfo
, vrf_id
))
1114 zserv_create_header (s
, ZEBRA_ROUTER_ID_UPDATE
, vrf_id
);
1116 /* Prefix information. */
1117 stream_putc (s
, p
->family
);
1118 blen
= prefix_blen (p
);
1119 stream_put (s
, &p
->u
.prefix
, blen
);
1120 stream_putc (s
, p
->prefixlen
);
1122 /* Write packet size. */
1123 stream_putw_at (s
, 0, stream_get_endp (s
));
1125 return zebra_server_send_message(client
);
1128 /* Register zebra server interface information. Send current all
1129 interface and address information. */
1131 zread_interface_add (struct zserv
*client
, u_short length
, struct zebra_vrf
*zvrf
)
1134 struct listnode
*ifnode
, *ifnnode
;
1135 struct interface
*ifp
;
1137 /* Interface information is needed. */
1138 vrf_bitmap_set (client
->ifinfo
, zvrf_id (zvrf
));
1140 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
)
1142 for (ALL_LIST_ELEMENTS (vrf
->iflist
, ifnode
, ifnnode
, ifp
))
1144 /* Skip pseudo interface. */
1145 if (! CHECK_FLAG (ifp
->status
, ZEBRA_INTERFACE_ACTIVE
))
1148 if (zsend_interface_add (client
, ifp
) < 0)
1151 if (zsend_interface_addresses (client
, ifp
) < 0)
1158 /* Unregister zebra server interface information. */
1160 zread_interface_delete (struct zserv
*client
, u_short length
, struct zebra_vrf
*zvrf
)
1162 vrf_bitmap_unset (client
->ifinfo
, zvrf_id (zvrf
));
1167 zserv_nexthop_num_warn (const char *caller
, const struct prefix
*p
, const unsigned int nexthop_num
)
1169 if (nexthop_num
> multipath_num
)
1171 char buff
[PREFIX2STR_BUFFER
];
1172 prefix2str(p
, buff
, sizeof (buff
));
1173 zlog_warn("%s: Prefix %s has %d nexthops, but we can only use the first %d",
1174 caller
, buff
, nexthop_num
, multipath_num
);
1178 /* This function support multiple nexthop. */
1180 * Parse the ZEBRA_IPV4_ROUTE_ADD sent from client. Update re and
1184 zread_ipv4_add (struct zserv
*client
, u_short length
, struct zebra_vrf
*zvrf
)
1187 struct route_entry
*re
;
1190 struct in_addr nhop_addr
;
1192 u_char nexthop_type
;
1198 struct nexthop
*nexthop
;
1200 /* Get input stream. */
1203 /* Allocate new re. */
1204 re
= XCALLOC (MTYPE_RE
, sizeof (struct route_entry
));
1206 /* Type, flags, message. */
1207 re
->type
= stream_getc (s
);
1208 re
->instance
= stream_getw (s
);
1209 re
->flags
= stream_getl (s
);
1210 message
= stream_getc (s
);
1211 safi
= stream_getw (s
);
1212 re
->uptime
= time (NULL
);
1215 memset (&p
, 0, sizeof (struct prefix_ipv4
));
1217 p
.prefixlen
= stream_getc (s
);
1218 stream_get (&p
.u
.prefix4
, s
, PSIZE (p
.prefixlen
));
1221 re
->vrf_id
= zvrf_id (zvrf
);
1223 /* Nexthop parse. */
1224 if (CHECK_FLAG (message
, ZAPI_MESSAGE_NEXTHOP
))
1226 nexthop_num
= stream_getc (s
);
1227 zserv_nexthop_num_warn(__func__
, (const struct prefix
*)&p
, nexthop_num
);
1229 for (i
= 0; i
< nexthop_num
; i
++)
1231 nexthop_type
= stream_getc (s
);
1233 switch (nexthop_type
)
1235 case NEXTHOP_TYPE_IFINDEX
:
1236 ifindex
= stream_getl (s
);
1237 route_entry_nexthop_ifindex_add (re
, ifindex
);
1239 case NEXTHOP_TYPE_IPV4
:
1240 nhop_addr
.s_addr
= stream_get_ipv4 (s
);
1241 nexthop
= route_entry_nexthop_ipv4_add (re
, &nhop_addr
, NULL
);
1242 /* For labeled-unicast, each nexthop is followed by label. */
1243 if (CHECK_FLAG (message
, ZAPI_MESSAGE_LABEL
))
1245 label
= (mpls_label_t
)stream_getl (s
);
1246 nexthop_add_labels (nexthop
, nexthop
->nh_label_type
, 1, &label
);
1249 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1250 nhop_addr
.s_addr
= stream_get_ipv4 (s
);
1251 ifindex
= stream_getl (s
);
1252 route_entry_nexthop_ipv4_ifindex_add (re
, &nhop_addr
, NULL
, ifindex
);
1254 case NEXTHOP_TYPE_IPV6
:
1255 stream_forward_getp (s
, IPV6_MAX_BYTELEN
);
1257 case NEXTHOP_TYPE_BLACKHOLE
:
1258 route_entry_nexthop_blackhole_add (re
);
1265 if (CHECK_FLAG (message
, ZAPI_MESSAGE_DISTANCE
))
1266 re
->distance
= stream_getc (s
);
1269 if (CHECK_FLAG (message
, ZAPI_MESSAGE_METRIC
))
1270 re
->metric
= stream_getl (s
);
1273 if (CHECK_FLAG (message
, ZAPI_MESSAGE_TAG
))
1274 re
->tag
= stream_getl (s
);
1278 if (CHECK_FLAG (message
, ZAPI_MESSAGE_MTU
))
1279 re
->mtu
= stream_getl (s
);
1284 re
->table
= zvrf
->table_id
;
1286 ret
= rib_add_multipath (AFI_IP
, safi
, &p
, NULL
, re
);
1290 client
->v4_route_add_cnt
++;
1292 client
->v4_route_upd8_cnt
++;
1296 /* Zebra server IPv4 prefix delete function. */
1298 zread_ipv4_delete (struct zserv
*client
, u_short length
, struct zebra_vrf
*zvrf
)
1302 struct zapi_ipv4 api
;
1303 struct in_addr nexthop
;
1304 union g_addr
*nexthop_p
;
1305 unsigned long ifindex
;
1308 u_char nexthop_type
;
1316 /* Type, flags, message. */
1317 api
.type
= stream_getc (s
);
1318 api
.instance
= stream_getw (s
);
1319 api
.flags
= stream_getl (s
);
1320 api
.message
= stream_getc (s
);
1321 api
.safi
= stream_getw (s
);
1324 memset (&p
, 0, sizeof (struct prefix
));
1326 p
.prefixlen
= stream_getc (s
);
1327 stream_get (&p
.u
.prefix4
, s
, PSIZE (p
.prefixlen
));
1329 /* Nexthop, ifindex, distance, metric. */
1330 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
))
1332 nexthop_num
= stream_getc (s
);
1334 for (i
= 0; i
< nexthop_num
; i
++)
1336 nexthop_type
= stream_getc (s
);
1338 switch (nexthop_type
)
1340 case NEXTHOP_TYPE_IFINDEX
:
1341 ifindex
= stream_getl (s
);
1343 case NEXTHOP_TYPE_IPV4
:
1344 nexthop
.s_addr
= stream_get_ipv4 (s
);
1345 /* For labeled-unicast, each nexthop is followed by label, but
1346 * we don't care for delete.
1348 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_LABEL
))
1349 stream_forward_getp (s
, sizeof(u_int32_t
));
1350 nexthop_p
= (union g_addr
*)&nexthop
;
1352 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1353 nexthop
.s_addr
= stream_get_ipv4 (s
);
1354 nexthop_p
= (union g_addr
*)&nexthop
;
1355 ifindex
= stream_getl (s
);
1357 case NEXTHOP_TYPE_IPV6
:
1358 stream_forward_getp (s
, IPV6_MAX_BYTELEN
);
1365 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_DISTANCE
))
1366 api
.distance
= stream_getc (s
);
1371 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_METRIC
))
1372 api
.metric
= stream_getl (s
);
1377 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_TAG
))
1378 api
.tag
= stream_getl (s
);
1382 table_id
= zvrf
->table_id
;
1384 rib_delete (AFI_IP
, api
.safi
, zvrf_id (zvrf
), api
.type
, api
.instance
,
1385 api
.flags
, &p
, NULL
, nexthop_p
, ifindex
, table_id
);
1386 client
->v4_route_del_cnt
++;
1390 /* MRIB Nexthop lookup for IPv4. */
1392 zread_ipv4_nexthop_lookup_mrib (struct zserv
*client
, u_short length
, struct zebra_vrf
*zvrf
)
1394 struct in_addr addr
;
1395 struct route_entry
*re
;
1397 addr
.s_addr
= stream_get_ipv4 (client
->ibuf
);
1398 re
= rib_match_ipv4_multicast (zvrf_id (zvrf
), addr
, NULL
);
1399 return zsend_ipv4_nexthop_lookup_mrib (client
, addr
, re
, zvrf
);
1402 /* Zebra server IPv6 prefix add function. */
1404 zread_ipv4_route_ipv6_nexthop_add (struct zserv
*client
, u_short length
, struct zebra_vrf
*zvrf
)
1408 struct in6_addr nhop_addr
;
1409 struct route_entry
*re
;
1412 u_char nexthop_type
;
1415 static struct in6_addr nexthops
[MULTIPATH_NUM
];
1416 static unsigned int ifindices
[MULTIPATH_NUM
];
1418 static mpls_label_t labels
[MULTIPATH_NUM
];
1420 struct nexthop
*nexthop
;
1422 /* Get input stream. */
1425 memset (&nhop_addr
, 0, sizeof (struct in6_addr
));
1427 /* Allocate new re. */
1428 re
= XCALLOC (MTYPE_RE
, sizeof (struct route_entry
));
1430 /* Type, flags, message. */
1431 re
->type
= stream_getc (s
);
1432 re
->instance
= stream_getw (s
);
1433 re
->flags
= stream_getl (s
);
1434 message
= stream_getc (s
);
1435 safi
= stream_getw (s
);
1436 re
->uptime
= time (NULL
);
1439 memset (&p
, 0, sizeof (struct prefix_ipv4
));
1441 p
.prefixlen
= stream_getc (s
);
1442 stream_get (&p
.u
.prefix4
, s
, PSIZE (p
.prefixlen
));
1445 re
->vrf_id
= zvrf_id (zvrf
);
1447 /* We need to give nh-addr, nh-ifindex with the same next-hop object
1448 * to the re to ensure that IPv6 multipathing works; need to coalesce
1449 * these. Clients should send the same number of paired set of
1450 * next-hop-addr/next-hop-ifindices. */
1451 if (CHECK_FLAG (message
, ZAPI_MESSAGE_NEXTHOP
))
1453 unsigned int nh_count
= 0;
1454 unsigned int if_count
= 0;
1455 unsigned int max_nh_if
= 0;
1457 nexthop_num
= stream_getc (s
);
1458 zserv_nexthop_num_warn(__func__
, (const struct prefix
*)&p
, nexthop_num
);
1459 for (i
= 0; i
< nexthop_num
; i
++)
1461 nexthop_type
= stream_getc (s
);
1463 switch (nexthop_type
)
1465 case NEXTHOP_TYPE_IPV6
:
1466 stream_get (&nhop_addr
, s
, 16);
1467 if (nh_count
< MULTIPATH_NUM
)
1469 /* For labeled-unicast, each nexthop is followed by label. */
1470 if (CHECK_FLAG (message
, ZAPI_MESSAGE_LABEL
))
1472 label
= (mpls_label_t
)stream_getl (s
);
1473 labels
[nh_count
] = label
;
1475 nexthops
[nh_count
] = nhop_addr
;
1479 case NEXTHOP_TYPE_IFINDEX
:
1480 if (if_count
< multipath_num
) {
1481 ifindices
[if_count
++] = stream_getl (s
);
1484 case NEXTHOP_TYPE_BLACKHOLE
:
1485 route_entry_nexthop_blackhole_add (re
);
1490 max_nh_if
= (nh_count
> if_count
) ? nh_count
: if_count
;
1491 for (i
= 0; i
< max_nh_if
; i
++)
1493 if ((i
< nh_count
) && !IN6_IS_ADDR_UNSPECIFIED (&nexthops
[i
])) {
1494 if ((i
< if_count
) && ifindices
[i
])
1495 nexthop
= route_entry_nexthop_ipv6_ifindex_add (re
, &nexthops
[i
], ifindices
[i
]);
1497 nexthop
= route_entry_nexthop_ipv6_add (re
, &nexthops
[i
]);
1499 if (CHECK_FLAG (message
, ZAPI_MESSAGE_LABEL
))
1500 nexthop_add_labels (nexthop
, nexthop
->nh_label_type
, 1, &labels
[i
]);
1503 if ((i
< if_count
) && ifindices
[i
])
1504 route_entry_nexthop_ifindex_add (re
, ifindices
[i
]);
1510 if (CHECK_FLAG (message
, ZAPI_MESSAGE_DISTANCE
))
1511 re
->distance
= stream_getc (s
);
1514 if (CHECK_FLAG (message
, ZAPI_MESSAGE_METRIC
))
1515 re
->metric
= stream_getl (s
);
1518 if (CHECK_FLAG (message
, ZAPI_MESSAGE_TAG
))
1519 re
->tag
= stream_getl (s
);
1523 if (CHECK_FLAG (message
, ZAPI_MESSAGE_MTU
))
1524 re
->mtu
= stream_getl (s
);
1529 re
->table
= zvrf
->table_id
;
1531 ret
= rib_add_multipath (AFI_IP6
, safi
, &p
, NULL
, re
);
1534 client
->v4_route_add_cnt
++;
1536 client
->v4_route_upd8_cnt
++;
1542 zread_ipv6_add (struct zserv
*client
, u_short length
, struct zebra_vrf
*zvrf
)
1546 struct in6_addr nhop_addr
;
1547 struct route_entry
*re
;
1550 u_char nexthop_type
;
1552 struct prefix_ipv6 src_p
, *src_pp
;
1554 static struct in6_addr nexthops
[MULTIPATH_NUM
];
1555 static unsigned int ifindices
[MULTIPATH_NUM
];
1557 static mpls_label_t labels
[MULTIPATH_NUM
];
1559 struct nexthop
*nexthop
;
1561 /* Get input stream. */
1564 memset (&nhop_addr
, 0, sizeof (struct in6_addr
));
1566 /* Allocate new re. */
1567 re
= XCALLOC (MTYPE_RE
, sizeof (struct route_entry
));
1569 /* Type, flags, message. */
1570 re
->type
= stream_getc (s
);
1571 re
->instance
= stream_getw (s
);
1572 re
->flags
= stream_getl (s
);
1573 message
= stream_getc (s
);
1574 safi
= stream_getw (s
);
1575 re
->uptime
= time (NULL
);
1578 memset (&p
, 0, sizeof (struct prefix_ipv6
));
1579 p
.family
= AF_INET6
;
1580 p
.prefixlen
= stream_getc (s
);
1581 stream_get (&p
.u
.prefix6
, s
, PSIZE (p
.prefixlen
));
1583 if (CHECK_FLAG (message
, ZAPI_MESSAGE_SRCPFX
))
1585 memset (&src_p
, 0, sizeof (struct prefix_ipv6
));
1586 src_p
.family
= AF_INET6
;
1587 src_p
.prefixlen
= stream_getc (s
);
1588 stream_get (&src_p
.prefix
, s
, PSIZE (src_p
.prefixlen
));
1594 /* We need to give nh-addr, nh-ifindex with the same next-hop object
1595 * to the re to ensure that IPv6 multipathing works; need to coalesce
1596 * these. Clients should send the same number of paired set of
1597 * next-hop-addr/next-hop-ifindices. */
1598 if (CHECK_FLAG (message
, ZAPI_MESSAGE_NEXTHOP
))
1600 unsigned int nh_count
= 0;
1601 unsigned int if_count
= 0;
1602 unsigned int max_nh_if
= 0;
1604 nexthop_num
= stream_getc (s
);
1605 zserv_nexthop_num_warn(__func__
, (const struct prefix
*)&p
, nexthop_num
);
1606 for (i
= 0; i
< nexthop_num
; i
++)
1608 nexthop_type
= stream_getc (s
);
1610 switch (nexthop_type
)
1612 case NEXTHOP_TYPE_IPV6
:
1613 stream_get (&nhop_addr
, s
, 16);
1614 if (nh_count
< MULTIPATH_NUM
)
1616 /* For labeled-unicast, each nexthop is followed by label. */
1617 if (CHECK_FLAG (message
, ZAPI_MESSAGE_LABEL
))
1619 label
= (mpls_label_t
)stream_getl (s
);
1620 labels
[nh_count
] = label
;
1622 nexthops
[nh_count
++] = nhop_addr
;
1625 case NEXTHOP_TYPE_IFINDEX
:
1626 if (if_count
< multipath_num
) {
1627 ifindices
[if_count
++] = stream_getl (s
);
1630 case NEXTHOP_TYPE_BLACKHOLE
:
1631 route_entry_nexthop_blackhole_add (re
);
1636 max_nh_if
= (nh_count
> if_count
) ? nh_count
: if_count
;
1637 for (i
= 0; i
< max_nh_if
; i
++)
1639 if ((i
< nh_count
) && !IN6_IS_ADDR_UNSPECIFIED (&nexthops
[i
])) {
1640 if ((i
< if_count
) && ifindices
[i
])
1641 nexthop
= route_entry_nexthop_ipv6_ifindex_add (re
, &nexthops
[i
], ifindices
[i
]);
1643 nexthop
= route_entry_nexthop_ipv6_add (re
, &nexthops
[i
]);
1644 if (CHECK_FLAG (message
, ZAPI_MESSAGE_LABEL
))
1645 nexthop_add_labels (nexthop
, nexthop
->nh_label_type
, 1, &labels
[i
]);
1648 if ((i
< if_count
) && ifindices
[i
])
1649 route_entry_nexthop_ifindex_add (re
, ifindices
[i
]);
1655 if (CHECK_FLAG (message
, ZAPI_MESSAGE_DISTANCE
))
1656 re
->distance
= stream_getc (s
);
1659 if (CHECK_FLAG (message
, ZAPI_MESSAGE_METRIC
))
1660 re
->metric
= stream_getl (s
);
1663 if (CHECK_FLAG (message
, ZAPI_MESSAGE_TAG
))
1664 re
->tag
= stream_getl (s
);
1668 if (CHECK_FLAG (message
, ZAPI_MESSAGE_MTU
))
1669 re
->mtu
= stream_getl (s
);
1674 re
->vrf_id
= zvrf_id (zvrf
);
1675 re
->table
= zvrf
->table_id
;
1677 ret
= rib_add_multipath (AFI_IP6
, safi
, &p
, src_pp
, re
);
1680 client
->v6_route_add_cnt
++;
1682 client
->v6_route_upd8_cnt
++;
1687 /* Zebra server IPv6 prefix delete function. */
1689 zread_ipv6_delete (struct zserv
*client
, u_short length
, struct zebra_vrf
*zvrf
)
1693 struct zapi_ipv6 api
;
1694 struct in6_addr nexthop
;
1695 union g_addr
*pnexthop
= NULL
;
1696 unsigned long ifindex
;
1698 struct prefix_ipv6 src_p
, *src_pp
;
1702 memset (&nexthop
, 0, sizeof (struct in6_addr
));
1704 /* Type, flags, message. */
1705 api
.type
= stream_getc (s
);
1706 api
.instance
= stream_getw (s
);
1707 api
.flags
= stream_getl (s
);
1708 api
.message
= stream_getc (s
);
1709 api
.safi
= stream_getw (s
);
1712 memset (&p
, 0, sizeof (struct prefix_ipv6
));
1713 p
.family
= AF_INET6
;
1714 p
.prefixlen
= stream_getc (s
);
1715 stream_get (&p
.u
.prefix6
, s
, PSIZE (p
.prefixlen
));
1717 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_SRCPFX
))
1719 memset (&src_p
, 0, sizeof (struct prefix_ipv6
));
1720 src_p
.family
= AF_INET6
;
1721 src_p
.prefixlen
= stream_getc (s
);
1722 stream_get (&src_p
.prefix
, s
, PSIZE (src_p
.prefixlen
));
1728 /* Nexthop, ifindex, distance, metric. */
1729 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
))
1731 u_char nexthop_type
;
1733 api
.nexthop_num
= stream_getc (s
);
1734 for (i
= 0; i
< api
.nexthop_num
; i
++)
1736 nexthop_type
= stream_getc (s
);
1738 switch (nexthop_type
)
1740 case NEXTHOP_TYPE_IPV6
:
1741 stream_get (&nexthop
, s
, 16);
1742 /* For labeled-unicast, each nexthop is followed by label, but
1743 * we don't care for delete.
1745 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_LABEL
))
1746 stream_forward_getp (s
, sizeof(u_int32_t
));
1747 pnexthop
= (union g_addr
*)&nexthop
;
1749 case NEXTHOP_TYPE_IFINDEX
:
1750 ifindex
= stream_getl (s
);
1757 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_DISTANCE
))
1758 api
.distance
= stream_getc (s
);
1763 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_METRIC
))
1764 api
.metric
= stream_getl (s
);
1769 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_TAG
))
1770 api
.tag
= stream_getl (s
);
1774 if (IN6_IS_ADDR_UNSPECIFIED (&nexthop
))
1775 rib_delete (AFI_IP6
, api
.safi
, zvrf_id (zvrf
), api
.type
, api
.instance
,
1776 api
.flags
, &p
, src_pp
, NULL
, ifindex
, client
->rtm_table
);
1778 rib_delete (AFI_IP6
, api
.safi
, zvrf_id (zvrf
), api
.type
, api
.instance
,
1779 api
.flags
, &p
, src_pp
, pnexthop
, ifindex
, client
->rtm_table
);
1781 client
->v6_route_del_cnt
++;
1785 /* Register zebra server router-id information. Send current router-id */
1787 zread_router_id_add (struct zserv
*client
, u_short length
, struct zebra_vrf
*zvrf
)
1791 /* Router-id information is needed. */
1792 vrf_bitmap_set (client
->ridinfo
, zvrf_id (zvrf
));
1794 router_id_get (&p
, zvrf_id (zvrf
));
1796 return zsend_router_id_update (client
, &p
, zvrf_id (zvrf
));
1799 /* Unregister zebra server router-id information. */
1801 zread_router_id_delete (struct zserv
*client
, u_short length
, struct zebra_vrf
*zvrf
)
1803 vrf_bitmap_unset (client
->ridinfo
, zvrf_id (zvrf
));
1807 /* Tie up route-type and client->sock */
1809 zread_hello (struct zserv
*client
)
1811 /* type of protocol (lib/zebra.h) */
1815 proto
= stream_getc (client
->ibuf
);
1816 instance
= stream_getw (client
->ibuf
);
1818 /* accept only dynamic routing protocols */
1819 if ((proto
< ZEBRA_ROUTE_MAX
)
1820 && (proto
> ZEBRA_ROUTE_STATIC
))
1822 zlog_notice ("client %d says hello and bids fair to announce only %s routes",
1823 client
->sock
, zebra_route_string(proto
));
1825 zlog_notice ("client protocol instance %d", instance
);
1827 client
->proto
= proto
;
1828 client
->instance
= instance
;
1832 /* Unregister all information in a VRF. */
1834 zread_vrf_unregister (struct zserv
*client
, u_short length
, struct zebra_vrf
*zvrf
)
1839 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
1840 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
1841 vrf_bitmap_unset (client
->redist
[afi
][i
], zvrf_id (zvrf
));
1842 vrf_bitmap_unset (client
->redist_default
, zvrf_id (zvrf
));
1843 vrf_bitmap_unset (client
->ifinfo
, zvrf_id (zvrf
));
1844 vrf_bitmap_unset (client
->ridinfo
, zvrf_id (zvrf
));
1850 zread_mpls_labels (int command
, struct zserv
*client
, u_short length
,
1854 enum lsp_types_t type
;
1855 struct prefix prefix
;
1856 enum nexthop_types_t gtype
;
1859 mpls_label_t in_label
, out_label
;
1861 struct zebra_vrf
*zvrf
;
1863 zvrf
= vrf_info_lookup (vrf_id
);
1867 /* Get input stream. */
1871 type
= stream_getc (s
);
1872 prefix
.family
= stream_getl (s
);
1873 switch (prefix
.family
)
1876 prefix
.u
.prefix4
.s_addr
= stream_get_ipv4 (s
);
1877 prefix
.prefixlen
= stream_getc (s
);
1878 gate
.ipv4
.s_addr
= stream_get_ipv4 (s
);
1881 stream_get (&prefix
.u
.prefix6
, s
, 16);
1882 prefix
.prefixlen
= stream_getc (s
);
1883 stream_get (&gate
.ipv6
, s
, 16);
1888 ifindex
= stream_getl (s
);
1889 distance
= stream_getc (s
);
1890 in_label
= stream_getl (s
);
1891 out_label
= stream_getl (s
);
1893 switch (prefix
.family
)
1897 gtype
= NEXTHOP_TYPE_IPV4_IFINDEX
;
1899 gtype
= NEXTHOP_TYPE_IPV4
;
1903 gtype
= NEXTHOP_TYPE_IPV6_IFINDEX
;
1905 gtype
= NEXTHOP_TYPE_IPV6
;
1914 if (command
== ZEBRA_MPLS_LABELS_ADD
)
1916 mpls_lsp_install (zvrf
, type
, in_label
, out_label
, gtype
, &gate
,
1918 if (out_label
!= MPLS_IMP_NULL_LABEL
)
1919 mpls_ftn_update (1, zvrf
, type
, &prefix
, gtype
, &gate
, ifindex
,
1920 distance
, out_label
);
1922 else if (command
== ZEBRA_MPLS_LABELS_DELETE
)
1924 mpls_lsp_uninstall (zvrf
, type
, in_label
, gtype
, &gate
, ifindex
);
1925 if (out_label
!= MPLS_IMP_NULL_LABEL
)
1926 mpls_ftn_update (0, zvrf
, type
, &prefix
, gtype
, &gate
, ifindex
,
1927 distance
, out_label
);
1930 /* Send response to a label manager connect request to client */
1932 zsend_label_manager_connect_response (struct zserv
*client
, vrf_id_t vrf_id
, u_short result
)
1939 zserv_create_header (s
, ZEBRA_LABEL_MANAGER_CONNECT
, vrf_id
);
1942 stream_putc (s
, result
);
1944 /* Write packet size. */
1945 stream_putw_at (s
, 0, stream_get_endp (s
));
1947 return writen (client
->sock
, s
->data
, stream_get_endp (s
));
1951 zread_label_manager_connect (struct zserv
*client
, vrf_id_t vrf_id
)
1954 /* type of protocol (lib/zebra.h) */
1958 /* Get input stream. */
1962 proto
= stream_getc (s
);
1963 instance
= stream_getw (s
);
1965 /* accept only dynamic routing protocols */
1966 if ((proto
>= ZEBRA_ROUTE_MAX
)
1967 || (proto
<= ZEBRA_ROUTE_STATIC
))
1969 zlog_err ("client %d has wrong protocol %s",
1970 client
->sock
, zebra_route_string(proto
));
1971 zsend_label_manager_connect_response (client
, vrf_id
, 1);
1974 zlog_notice ("client %d with instance %u connected as %s",
1975 client
->sock
, instance
, zebra_route_string(proto
));
1976 client
->proto
= proto
;
1977 client
->instance
= instance
;
1980 Release previous labels of same protocol and instance.
1981 This is done in case it restarted from an unexpected shutdown.
1983 release_daemon_chunks (proto
, instance
);
1985 zlog_debug (" Label Manager client connected: sock %d, proto %s, instance %u",
1986 client
->sock
, zebra_route_string(proto
), instance
);
1987 /* send response back */
1988 zsend_label_manager_connect_response (client
, vrf_id
, 0);
1990 /* Send response to a get label chunk request to client */
1992 zsend_assign_label_chunk_response (struct zserv
*client
, vrf_id_t vrf_id
,
1993 struct label_manager_chunk
*lmc
)
2000 zserv_create_header (s
, ZEBRA_GET_LABEL_CHUNK
, vrf_id
);
2005 stream_putc (s
, lmc
->keep
);
2006 /* start and end labels */
2007 stream_putl (s
, lmc
->start
);
2008 stream_putl (s
, lmc
->end
);
2012 /* Write packet size. */
2013 stream_putw_at (s
, 0, stream_get_endp (s
));
2015 return writen (client
->sock
, s
->data
, stream_get_endp (s
));
2019 zread_get_label_chunk (struct zserv
*client
, vrf_id_t vrf_id
)
2024 struct label_manager_chunk
*lmc
;
2026 /* Get input stream. */
2030 keep
= stream_getc (s
);
2031 size
= stream_getl (s
);
2033 lmc
= assign_label_chunk (client
->proto
, client
->instance
, keep
, size
);
2035 zlog_err ("%s: Unable to assign Label Chunk of size %u", __func__
, size
);
2037 zlog_debug ("Assigned Label Chunk %u - %u to %u",
2038 lmc
->start
, lmc
->end
, keep
);
2039 /* send response back */
2040 zsend_assign_label_chunk_response (client
, vrf_id
, lmc
);
2044 zread_release_label_chunk (struct zserv
*client
)
2047 uint32_t start
, end
;
2049 /* Get input stream. */
2053 start
= stream_getl (s
);
2054 end
= stream_getl (s
);
2056 release_label_chunk (client
->proto
, client
->instance
, start
, end
);
2059 zread_label_manager_request (int cmd
, struct zserv
*client
, vrf_id_t vrf_id
)
2061 /* to avoid sending other messages like ZERBA_INTERFACE_UP */
2062 if (cmd
== ZEBRA_LABEL_MANAGER_CONNECT
)
2063 client
->is_synchronous
= 1;
2065 /* external label manager */
2067 zread_relay_label_manager_request (cmd
, client
, vrf_id
);
2068 /* this is a label manager */
2071 if (cmd
== ZEBRA_LABEL_MANAGER_CONNECT
)
2072 zread_label_manager_connect (client
, vrf_id
);
2075 /* Sanity: don't allow 'unidentified' requests */
2078 zlog_err ("Got label request from an unidentified client");
2081 if (cmd
== ZEBRA_GET_LABEL_CHUNK
)
2082 zread_get_label_chunk (client
, vrf_id
);
2083 else if (cmd
== ZEBRA_RELEASE_LABEL_CHUNK
)
2084 zread_release_label_chunk (client
);
2089 /* Cleanup registered nexthops (across VRFs) upon client disconnect. */
2091 zebra_client_close_cleanup_rnh (struct zserv
*client
)
2094 struct zebra_vrf
*zvrf
;
2096 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
)
2098 if ((zvrf
= vrf
->info
) != NULL
)
2100 zebra_cleanup_rnh_client(zvrf_id (zvrf
), AF_INET
, client
, RNH_NEXTHOP_TYPE
);
2101 zebra_cleanup_rnh_client(zvrf_id (zvrf
), AF_INET6
, client
, RNH_NEXTHOP_TYPE
);
2102 zebra_cleanup_rnh_client(zvrf_id (zvrf
), AF_INET
, client
, RNH_IMPORT_CHECK_TYPE
);
2103 zebra_cleanup_rnh_client(zvrf_id (zvrf
), AF_INET6
, client
, RNH_IMPORT_CHECK_TYPE
);
2104 if (client
->proto
== ZEBRA_ROUTE_LDP
)
2106 hash_iterate(zvrf
->lsp_table
, mpls_ldp_lsp_uninstall_all
,
2108 mpls_ldp_ftn_uninstall_all (zvrf
, AFI_IP
);
2109 mpls_ldp_ftn_uninstall_all (zvrf
, AFI_IP6
);
2115 /* Close zebra client. */
2117 zebra_client_close (struct zserv
*client
)
2119 /* Send client de-registration to BFD */
2120 zebra_ptm_bfd_client_deregister(client
->proto
);
2122 /* Cleanup any registered nexthops - across all VRFs. */
2123 zebra_client_close_cleanup_rnh (client
);
2125 /* Release Label Manager chunks */
2126 release_daemon_chunks (client
->proto
, client
->instance
);
2128 /* Cleanup any FECs registered by this client. */
2129 zebra_mpls_cleanup_fecs_for_client (vrf_info_lookup(VRF_DEFAULT
), client
);
2131 /* Close file descriptor. */
2134 unsigned long nroutes
;
2136 close (client
->sock
);
2137 nroutes
= rib_score_proto (client
->proto
, client
->instance
);
2138 zlog_notice ("client %d disconnected. %lu %s routes removed from the rib",
2139 client
->sock
, nroutes
, zebra_route_string (client
->proto
));
2143 /* Free stream buffers. */
2145 stream_free (client
->ibuf
);
2147 stream_free (client
->obuf
);
2149 buffer_free(client
->wb
);
2151 /* Release threads. */
2153 thread_cancel (client
->t_read
);
2154 if (client
->t_write
)
2155 thread_cancel (client
->t_write
);
2156 if (client
->t_suicide
)
2157 thread_cancel (client
->t_suicide
);
2160 for (afi_t afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
2161 for (int i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
2162 vrf_bitmap_free (client
->redist
[afi
][i
]);
2164 vrf_bitmap_free (client
->redist_default
);
2165 vrf_bitmap_free (client
->ifinfo
);
2166 vrf_bitmap_free (client
->ridinfo
);
2168 /* Free client structure. */
2169 listnode_delete (zebrad
.client_list
, client
);
2170 XFREE (MTYPE_TMP
, client
);
2173 /* Make new client. */
2175 zebra_client_create (int sock
)
2177 struct zserv
*client
;
2181 client
= XCALLOC (MTYPE_TMP
, sizeof (struct zserv
));
2183 /* Make client input/output buffer. */
2184 client
->sock
= sock
;
2185 client
->ibuf
= stream_new (ZEBRA_MAX_PACKET_SIZ
);
2186 client
->obuf
= stream_new (ZEBRA_MAX_PACKET_SIZ
);
2187 client
->wb
= buffer_new(0);
2189 /* Set table number. */
2190 client
->rtm_table
= zebrad
.rtm_table_default
;
2192 client
->connect_time
= monotime(NULL
);
2193 /* Initialize flags */
2194 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
2195 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
2196 client
->redist
[afi
][i
] = vrf_bitmap_init ();
2197 client
->redist_default
= vrf_bitmap_init ();
2198 client
->ifinfo
= vrf_bitmap_init ();
2199 client
->ridinfo
= vrf_bitmap_init ();
2201 /* by default, it's not a synchronous client */
2202 client
->is_synchronous
= 0;
2204 /* Add this client to linked list. */
2205 listnode_add (zebrad
.client_list
, client
);
2207 /* Make new read thread. */
2208 zebra_event (ZEBRA_READ
, sock
, client
);
2210 zebra_vrf_update_all (client
);
2213 /* Handler of zebra service request. */
2215 zebra_client_read (struct thread
*thread
)
2218 struct zserv
*client
;
2220 uint16_t length
, command
;
2221 uint8_t marker
, version
;
2223 struct zebra_vrf
*zvrf
;
2225 /* Get thread data. Reset reading thread because I'm running. */
2226 sock
= THREAD_FD (thread
);
2227 client
= THREAD_ARG (thread
);
2228 client
->t_read
= NULL
;
2230 if (client
->t_suicide
)
2232 zebra_client_close(client
);
2236 /* Read length and command (if we don't have it already). */
2237 if ((already
= stream_get_endp(client
->ibuf
)) < ZEBRA_HEADER_SIZE
)
2240 if (((nbyte
= stream_read_try (client
->ibuf
, sock
,
2241 ZEBRA_HEADER_SIZE
-already
)) == 0) ||
2244 if (IS_ZEBRA_DEBUG_EVENT
)
2245 zlog_debug ("connection closed socket [%d]", sock
);
2246 zebra_client_close (client
);
2249 if (nbyte
!= (ssize_t
)(ZEBRA_HEADER_SIZE
-already
))
2251 /* Try again later. */
2252 zebra_event (ZEBRA_READ
, sock
, client
);
2255 already
= ZEBRA_HEADER_SIZE
;
2258 /* Reset to read from the beginning of the incoming packet. */
2259 stream_set_getp(client
->ibuf
, 0);
2261 /* Fetch header values */
2262 length
= stream_getw (client
->ibuf
);
2263 marker
= stream_getc (client
->ibuf
);
2264 version
= stream_getc (client
->ibuf
);
2265 vrf_id
= stream_getw (client
->ibuf
);
2266 command
= stream_getw (client
->ibuf
);
2268 if (marker
!= ZEBRA_HEADER_MARKER
|| version
!= ZSERV_VERSION
)
2270 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
2271 __func__
, sock
, marker
, version
);
2272 zebra_client_close (client
);
2275 if (length
< ZEBRA_HEADER_SIZE
)
2277 zlog_warn("%s: socket %d message length %u is less than header size %d",
2278 __func__
, sock
, length
, ZEBRA_HEADER_SIZE
);
2279 zebra_client_close (client
);
2282 if (length
> STREAM_SIZE(client
->ibuf
))
2284 zlog_warn("%s: socket %d message length %u exceeds buffer size %lu",
2285 __func__
, sock
, length
, (u_long
)STREAM_SIZE(client
->ibuf
));
2286 zebra_client_close (client
);
2290 /* Read rest of data. */
2291 if (already
< length
)
2294 if (((nbyte
= stream_read_try (client
->ibuf
, sock
,
2295 length
-already
)) == 0) ||
2298 if (IS_ZEBRA_DEBUG_EVENT
)
2299 zlog_debug ("connection closed [%d] when reading zebra data", sock
);
2300 zebra_client_close (client
);
2303 if (nbyte
!= (ssize_t
)(length
-already
))
2305 /* Try again later. */
2306 zebra_event (ZEBRA_READ
, sock
, client
);
2311 length
-= ZEBRA_HEADER_SIZE
;
2313 /* Debug packet information. */
2314 if (IS_ZEBRA_DEBUG_EVENT
)
2315 zlog_debug ("zebra message comes from socket [%d]", sock
);
2317 if (IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
)
2318 zlog_debug ("zebra message received [%s] %d in VRF %u",
2319 zserv_command_string (command
), length
, vrf_id
);
2321 client
->last_read_time
= monotime(NULL
);
2322 client
->last_read_cmd
= command
;
2324 zvrf
= zebra_vrf_lookup_by_id (vrf_id
);
2327 if (IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
)
2328 zlog_debug ("zebra received unknown VRF[%u]", vrf_id
);
2329 goto zclient_read_out
;
2334 case ZEBRA_ROUTER_ID_ADD
:
2335 zread_router_id_add (client
, length
, zvrf
);
2337 case ZEBRA_ROUTER_ID_DELETE
:
2338 zread_router_id_delete (client
, length
, zvrf
);
2340 case ZEBRA_INTERFACE_ADD
:
2341 zread_interface_add (client
, length
, zvrf
);
2343 case ZEBRA_INTERFACE_DELETE
:
2344 zread_interface_delete (client
, length
, zvrf
);
2346 case ZEBRA_IPV4_ROUTE_ADD
:
2347 zread_ipv4_add (client
, length
, zvrf
);
2349 case ZEBRA_IPV4_ROUTE_DELETE
:
2350 zread_ipv4_delete (client
, length
, zvrf
);
2352 case ZEBRA_IPV4_ROUTE_IPV6_NEXTHOP_ADD
:
2353 zread_ipv4_route_ipv6_nexthop_add (client
, length
, zvrf
);
2355 case ZEBRA_IPV4_NEXTHOP_ADD
:
2356 zread_ipv4_add(client
, length
, zvrf
); /* LB: r1.0 merge - id was 1 */
2358 case ZEBRA_IPV4_NEXTHOP_DELETE
:
2359 zread_ipv4_delete(client
, length
, zvrf
); /* LB: r1.0 merge - id was 1 */
2361 case ZEBRA_IPV6_ROUTE_ADD
:
2362 zread_ipv6_add (client
, length
, zvrf
);
2364 case ZEBRA_IPV6_ROUTE_DELETE
:
2365 zread_ipv6_delete (client
, length
, zvrf
);
2367 case ZEBRA_REDISTRIBUTE_ADD
:
2368 zebra_redistribute_add (command
, client
, length
, zvrf
);
2370 case ZEBRA_REDISTRIBUTE_DELETE
:
2371 zebra_redistribute_delete (command
, client
, length
, zvrf
);
2373 case ZEBRA_REDISTRIBUTE_DEFAULT_ADD
:
2374 zebra_redistribute_default_add (command
, client
, length
, zvrf
);
2376 case ZEBRA_REDISTRIBUTE_DEFAULT_DELETE
:
2377 zebra_redistribute_default_delete (command
, client
, length
, zvrf
);
2379 case ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB
:
2380 zread_ipv4_nexthop_lookup_mrib (client
, length
, zvrf
);
2383 zread_hello (client
);
2385 case ZEBRA_NEXTHOP_REGISTER
:
2386 zserv_rnh_register(client
, sock
, length
, RNH_NEXTHOP_TYPE
, zvrf
);
2388 case ZEBRA_NEXTHOP_UNREGISTER
:
2389 zserv_rnh_unregister(client
, sock
, length
, RNH_NEXTHOP_TYPE
, zvrf
);
2391 case ZEBRA_IMPORT_ROUTE_REGISTER
:
2392 zserv_rnh_register(client
, sock
, length
, RNH_IMPORT_CHECK_TYPE
, zvrf
);
2394 case ZEBRA_IMPORT_ROUTE_UNREGISTER
:
2395 zserv_rnh_unregister(client
, sock
, length
, RNH_IMPORT_CHECK_TYPE
, zvrf
);
2397 case ZEBRA_BFD_DEST_UPDATE
:
2398 case ZEBRA_BFD_DEST_REGISTER
:
2399 zebra_ptm_bfd_dst_register(client
, sock
, length
, command
, zvrf
);
2401 case ZEBRA_BFD_DEST_DEREGISTER
:
2402 zebra_ptm_bfd_dst_deregister(client
, sock
, length
, zvrf
);
2404 case ZEBRA_VRF_UNREGISTER
:
2405 zread_vrf_unregister (client
, length
, zvrf
);
2407 case ZEBRA_BFD_CLIENT_REGISTER
:
2408 zebra_ptm_bfd_client_register(client
, sock
, length
);
2410 case ZEBRA_INTERFACE_ENABLE_RADV
:
2411 #if defined (HAVE_RTADV)
2412 zebra_interface_radv_set (client
, sock
, length
, zvrf
, 1);
2415 case ZEBRA_INTERFACE_DISABLE_RADV
:
2416 #if defined (HAVE_RTADV)
2417 zebra_interface_radv_set (client
, sock
, length
, zvrf
, 0);
2420 case ZEBRA_MPLS_LABELS_ADD
:
2421 case ZEBRA_MPLS_LABELS_DELETE
:
2422 zread_mpls_labels (command
, client
, length
, vrf_id
);
2424 case ZEBRA_IPMR_ROUTE_STATS
:
2425 zebra_ipmr_route_stats (client
, sock
, length
, zvrf
);
2427 case ZEBRA_LABEL_MANAGER_CONNECT
:
2428 case ZEBRA_GET_LABEL_CHUNK
:
2429 case ZEBRA_RELEASE_LABEL_CHUNK
:
2430 zread_label_manager_request (command
, client
, vrf_id
);
2432 case ZEBRA_FEC_REGISTER
:
2433 zserv_fec_register (client
, sock
, length
);
2435 case ZEBRA_FEC_UNREGISTER
:
2436 zserv_fec_unregister (client
, sock
, length
);
2438 case ZEBRA_ADVERTISE_ALL_VNI
:
2439 zebra_vxlan_advertise_all_vni (client
, sock
, length
, zvrf
);
2441 case ZEBRA_REMOTE_VTEP_ADD
:
2442 zebra_vxlan_remote_vtep_add (client
, sock
, length
, zvrf
);
2444 case ZEBRA_REMOTE_VTEP_DEL
:
2445 zebra_vxlan_remote_vtep_del (client
, sock
, length
, zvrf
);
2447 case ZEBRA_REMOTE_MACIP_ADD
:
2448 zebra_vxlan_remote_macip_add (client
, sock
, length
, zvrf
);
2450 case ZEBRA_REMOTE_MACIP_DEL
:
2451 zebra_vxlan_remote_macip_del (client
, sock
, length
, zvrf
);
2454 zlog_info ("Zebra received unknown command %d", command
);
2458 if (client
->t_suicide
)
2460 /* No need to wait for thread callback, just kill immediately. */
2461 zebra_client_close(client
);
2466 stream_reset (client
->ibuf
);
2467 zebra_event (ZEBRA_READ
, sock
, client
);
2472 /* Accept code of zebra server socket. */
2474 zebra_accept (struct thread
*thread
)
2478 struct sockaddr_in client
;
2481 accept_sock
= THREAD_FD (thread
);
2483 /* Reregister myself. */
2484 zebra_event (ZEBRA_SERV
, accept_sock
, NULL
);
2486 len
= sizeof (struct sockaddr_in
);
2487 client_sock
= accept (accept_sock
, (struct sockaddr
*) &client
, &len
);
2489 if (client_sock
< 0)
2491 zlog_warn ("Can't accept zebra socket: %s", safe_strerror (errno
));
2495 /* Make client socket non-blocking. */
2496 set_nonblocking(client_sock
);
2498 /* Create new zebra client. */
2499 zebra_client_create (client_sock
);
2504 #ifdef HAVE_TCP_ZEBRA
2505 /* Make zebra's server socket. */
2511 struct sockaddr_in addr
;
2513 accept_sock
= socket (AF_INET
, SOCK_STREAM
, 0);
2515 if (accept_sock
< 0)
2517 zlog_warn ("Can't create zserv stream socket: %s",
2518 safe_strerror (errno
));
2519 zlog_warn ("zebra can't provice full functionality due to above error");
2523 memset (&addr
, 0, sizeof (struct sockaddr_in
));
2524 addr
.sin_family
= AF_INET
;
2525 addr
.sin_port
= htons (ZEBRA_PORT
);
2526 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
2527 addr
.sin_len
= sizeof (struct sockaddr_in
);
2528 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
2529 addr
.sin_addr
.s_addr
= htonl (INADDR_LOOPBACK
);
2531 sockopt_reuseaddr (accept_sock
);
2532 sockopt_reuseport (accept_sock
);
2534 if ( zserv_privs
.change(ZPRIVS_RAISE
) )
2535 zlog_err("Can't raise privileges");
2537 ret
= bind (accept_sock
, (struct sockaddr
*)&addr
,
2538 sizeof (struct sockaddr_in
));
2541 zlog_warn ("Can't bind to stream socket: %s",
2542 safe_strerror (errno
));
2543 zlog_warn ("zebra can't provice full functionality due to above error");
2544 close (accept_sock
); /* Avoid sd leak. */
2548 if ( zserv_privs
.change(ZPRIVS_LOWER
) )
2549 zlog_err("Can't lower privileges");
2551 ret
= listen (accept_sock
, 1);
2554 zlog_warn ("Can't listen to stream socket: %s",
2555 safe_strerror (errno
));
2556 zlog_warn ("zebra can't provice full functionality due to above error");
2557 close (accept_sock
); /* Avoid sd leak. */
2561 zebra_event (ZEBRA_SERV
, accept_sock
, NULL
);
2563 #else /* HAVE_TCP_ZEBRA */
2565 /* For sockaddr_un. */
2568 /* zebra server UNIX domain socket. */
2570 zebra_serv_un (const char *path
)
2574 struct sockaddr_un serv
;
2577 /* First of all, unlink existing socket */
2581 old_mask
= umask (0077);
2583 /* Make UNIX domain socket. */
2584 sock
= socket (AF_UNIX
, SOCK_STREAM
, 0);
2587 zlog_warn ("Can't create zserv unix socket: %s",
2588 safe_strerror (errno
));
2589 zlog_warn ("zebra can't provide full functionality due to above error");
2593 /* Make server socket. */
2594 memset (&serv
, 0, sizeof (struct sockaddr_un
));
2595 serv
.sun_family
= AF_UNIX
;
2596 strncpy (serv
.sun_path
, path
, strlen (path
));
2597 #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
2598 len
= serv
.sun_len
= SUN_LEN(&serv
);
2600 len
= sizeof (serv
.sun_family
) + strlen (serv
.sun_path
);
2601 #endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
2603 ret
= bind (sock
, (struct sockaddr
*) &serv
, len
);
2606 zlog_warn ("Can't bind to unix socket %s: %s",
2607 path
, safe_strerror (errno
));
2608 zlog_warn ("zebra can't provide full functionality due to above error");
2613 ret
= listen (sock
, 5);
2616 zlog_warn ("Can't listen to unix socket %s: %s",
2617 path
, safe_strerror (errno
));
2618 zlog_warn ("zebra can't provide full functionality due to above error");
2625 zebra_event (ZEBRA_SERV
, sock
, NULL
);
2627 #endif /* HAVE_TCP_ZEBRA */
2631 zebra_event (enum event event
, int sock
, struct zserv
*client
)
2636 thread_add_read(zebrad
.master
, zebra_accept
, client
, sock
, NULL
);
2639 client
->t_read
= NULL
;
2640 thread_add_read(zebrad
.master
, zebra_client_read
, client
, sock
,
2649 #define ZEBRA_TIME_BUF 32
2651 zserv_time_buf(time_t *time1
, char *buf
, int buflen
)
2656 assert (buf
!= NULL
);
2657 assert (buflen
>= ZEBRA_TIME_BUF
);
2658 assert (time1
!= NULL
);
2662 snprintf(buf
, buflen
, "never ");
2666 now
= monotime(NULL
);
2670 /* Making formatted timer strings. */
2671 #define ONE_DAY_SECOND 60*60*24
2672 #define ONE_WEEK_SECOND 60*60*24*7
2674 if (now
< ONE_DAY_SECOND
)
2675 snprintf (buf
, buflen
, "%02d:%02d:%02d",
2676 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
);
2677 else if (now
< ONE_WEEK_SECOND
)
2678 snprintf (buf
, buflen
, "%dd%02dh%02dm",
2679 tm
->tm_yday
, tm
->tm_hour
, tm
->tm_min
);
2681 snprintf (buf
, buflen
, "%02dw%dd%02dh",
2682 tm
->tm_yday
/7, tm
->tm_yday
- ((tm
->tm_yday
/7) * 7), tm
->tm_hour
);
2687 zebra_show_client_detail (struct vty
*vty
, struct zserv
*client
)
2689 char cbuf
[ZEBRA_TIME_BUF
], rbuf
[ZEBRA_TIME_BUF
];
2690 char wbuf
[ZEBRA_TIME_BUF
], nhbuf
[ZEBRA_TIME_BUF
], mbuf
[ZEBRA_TIME_BUF
];
2692 vty_out (vty
, "Client: %s", zebra_route_string(client
->proto
));
2693 if (client
->instance
)
2694 vty_out (vty
, " Instance: %d", client
->instance
);
2695 vty_out (vty
, VTYNL
);
2697 vty_outln (vty
, "------------------------ ");
2698 vty_outln (vty
, "FD: %d ", client
->sock
);
2699 vty_outln (vty
, "Route Table ID: %d ", client
->rtm_table
);
2701 vty_outln (vty
, "Connect Time: %s ",
2702 zserv_time_buf(&client
->connect_time
, cbuf
, ZEBRA_TIME_BUF
));
2703 if (client
->nh_reg_time
)
2705 vty_outln (vty
, "Nexthop Registry Time: %s ",
2706 zserv_time_buf(&client
->nh_reg_time
, nhbuf
, ZEBRA_TIME_BUF
));
2707 if (client
->nh_last_upd_time
)
2708 vty_outln (vty
, "Nexthop Last Update Time: %s ",
2709 zserv_time_buf(&client
->nh_last_upd_time
, mbuf
, ZEBRA_TIME_BUF
));
2711 vty_outln (vty
, "No Nexthop Update sent");
2714 vty_outln (vty
, "Not registered for Nexthop Updates");
2716 vty_outln (vty
, "Last Msg Rx Time: %s ",
2717 zserv_time_buf(&client
->last_read_time
, rbuf
, ZEBRA_TIME_BUF
));
2718 vty_outln (vty
, "Last Msg Tx Time: %s ",
2719 zserv_time_buf(&client
->last_write_time
, wbuf
, ZEBRA_TIME_BUF
));
2720 if (client
->last_read_time
)
2721 vty_outln (vty
, "Last Rcvd Cmd: %s ",
2722 zserv_command_string(client
->last_read_cmd
));
2723 if (client
->last_write_time
)
2724 vty_outln (vty
, "Last Sent Cmd: %s ",
2725 zserv_command_string(client
->last_write_cmd
));
2726 vty_out (vty
, VTYNL
);
2728 vty_outln (vty
, "Type Add Update Del ");
2729 vty_outln (vty
, "================================================== ");
2730 vty_outln (vty
, "IPv4 %-12d%-12d%-12d", client
->v4_route_add_cnt
,
2731 client
->v4_route_upd8_cnt
, client
->v4_route_del_cnt
);
2732 vty_outln (vty
, "IPv6 %-12d%-12d%-12d", client
->v6_route_add_cnt
,
2733 client
->v6_route_upd8_cnt
, client
->v6_route_del_cnt
);
2734 vty_outln (vty
, "Redist:v4 %-12d%-12d%-12d", client
->redist_v4_add_cnt
, 0,
2735 client
->redist_v4_del_cnt
);
2736 vty_outln (vty
, "Redist:v6 %-12d%-12d%-12d", client
->redist_v6_add_cnt
, 0,
2737 client
->redist_v6_del_cnt
);
2738 vty_outln (vty
, "Connected %-12d%-12d%-12d", client
->ifadd_cnt
, 0,
2740 vty_outln (vty
, "BFD peer %-12d%-12d%-12d", client
->bfd_peer_add_cnt
,
2741 client
->bfd_peer_upd8_cnt
, client
->bfd_peer_del_cnt
);
2742 vty_outln (vty
, "Interface Up Notifications: %d",client
->ifup_cnt
);
2743 vty_outln (vty
, "Interface Down Notifications: %d",client
->ifdown_cnt
);
2744 vty_outln (vty
, "VNI add notifications: %d", client
->vniadd_cnt
);
2745 vty_outln (vty
, "VNI delete notifications: %d", client
->vnidel_cnt
);
2746 vty_outln (vty
, "MAC-IP add notifications: %d", client
->macipadd_cnt
);
2747 vty_outln (vty
, "MAC-IP delete notifications: %d", client
->macipdel_cnt
);
2749 vty_out (vty
, VTYNL
);
2754 zebra_show_client_brief (struct vty
*vty
, struct zserv
*client
)
2756 char cbuf
[ZEBRA_TIME_BUF
], rbuf
[ZEBRA_TIME_BUF
];
2757 char wbuf
[ZEBRA_TIME_BUF
];
2759 vty_outln (vty
, "%-8s%12s %12s%12s%8d/%-8d%8d/%-8d",
2760 zebra_route_string(client
->proto
),
2761 zserv_time_buf(&client
->connect_time
, cbuf
, ZEBRA_TIME_BUF
),
2762 zserv_time_buf(&client
->last_read_time
, rbuf
, ZEBRA_TIME_BUF
),
2763 zserv_time_buf(&client
->last_write_time
, wbuf
, ZEBRA_TIME_BUF
),
2764 client
->v4_route_add_cnt
+client
->v4_route_upd8_cnt
,
2765 client
->v4_route_del_cnt
,
2766 client
->v6_route_add_cnt
+client
->v6_route_upd8_cnt
,
2767 client
->v6_route_del_cnt
);
2772 zebra_find_client (u_char proto
)
2774 struct listnode
*node
, *nnode
;
2775 struct zserv
*client
;
2777 for (ALL_LIST_ELEMENTS (zebrad
.client_list
, node
, nnode
, client
))
2779 if (client
->proto
== proto
)
2787 /* Display default rtm_table for all clients. */
2792 "default routing table to use for all clients\n")
2794 vty_outln (vty
, "table %d",zebrad
.rtm_table_default
);
2798 DEFUN (config_table
,
2801 "Configure target kernel routing table\n"
2804 zebrad
.rtm_table_default
= strtol (argv
[1]->arg
, (char**)0, 10);
2808 DEFUN (no_config_table
,
2809 no_config_table_cmd
,
2810 "no table [TABLENO]",
2812 "Configure target kernel routing table\n"
2815 zebrad
.rtm_table_default
= 0;
2820 DEFUN (ip_forwarding
,
2824 "Turn on IP forwarding")
2830 ret
= ipforward_on ();
2834 vty_outln (vty
, "Can't turn on IP forwarding");
2835 return CMD_WARNING_CONFIG_FAILED
;
2841 DEFUN (no_ip_forwarding
,
2842 no_ip_forwarding_cmd
,
2846 "Turn off IP forwarding")
2852 ret
= ipforward_off ();
2856 vty_outln (vty
, "Can't turn off IP forwarding");
2857 return CMD_WARNING_CONFIG_FAILED
;
2867 "Zebra information\n")
2872 " Route Route Neighbor LSP LSP");
2874 "VRF Installs Removals Updates Installs Removals");
2875 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
)
2877 struct zebra_vrf
*zvrf
= vrf
->info
;
2878 vty_outln (vty
,"%-25s %10" PRIu64
" %10" PRIu64
" %10" PRIu64
" %10" PRIu64
" %10" PRIu64
"",
2879 vrf
->name
, zvrf
->installs
, zvrf
->removals
,
2880 zvrf
->neigh_updates
, zvrf
->lsp_installs
,zvrf
->lsp_removals
);
2886 /* This command is for debugging purpose. */
2887 DEFUN (show_zebra_client
,
2888 show_zebra_client_cmd
,
2889 "show zebra client",
2891 "Zebra information\n"
2892 "Client information\n")
2894 struct listnode
*node
;
2895 struct zserv
*client
;
2897 for (ALL_LIST_ELEMENTS_RO (zebrad
.client_list
, node
, client
))
2898 zebra_show_client_detail(vty
, client
);
2903 /* This command is for debugging purpose. */
2904 DEFUN (show_zebra_client_summary
,
2905 show_zebra_client_summary_cmd
,
2906 "show zebra client summary",
2908 "Zebra information brief\n"
2909 "Client information brief\n"
2912 struct listnode
*node
;
2913 struct zserv
*client
;
2916 "Name Connect Time Last Read Last Write IPv4 Routes IPv6 Routes ");
2918 "--------------------------------------------------------------------------------");
2920 for (ALL_LIST_ELEMENTS_RO (zebrad
.client_list
, node
, client
))
2921 zebra_show_client_brief(vty
, client
);
2923 vty_outln (vty
, "Routes column shows (added+updated)/deleted");
2927 /* Table configuration write function. */
2929 config_write_table (struct vty
*vty
)
2931 if (zebrad
.rtm_table_default
)
2932 vty_outln (vty
, "table %d",zebrad
.rtm_table_default
);
2936 /* table node for routing tables. */
2937 static struct cmd_node table_node
=
2940 "", /* This node has no interface. */
2944 /* Only display ip forwarding is enabled or not. */
2945 DEFUN (show_ip_forwarding
,
2946 show_ip_forwarding_cmd
,
2947 "show ip forwarding",
2950 "IP forwarding status\n")
2957 vty_outln (vty
, "IP forwarding is off");
2959 vty_outln (vty
, "IP forwarding is on");
2963 /* Only display ipv6 forwarding is enabled or not. */
2964 DEFUN (show_ipv6_forwarding
,
2965 show_ipv6_forwarding_cmd
,
2966 "show ipv6 forwarding",
2968 "IPv6 information\n"
2969 "Forwarding status\n")
2973 ret
= ipforward_ipv6 ();
2978 vty_outln (vty
, "ipv6 forwarding is unknown");
2981 vty_outln (vty
, "ipv6 forwarding is %s", "off");
2984 vty_outln (vty
, "ipv6 forwarding is %s", "on");
2987 vty_outln (vty
, "ipv6 forwarding is %s", "off");
2993 DEFUN (ipv6_forwarding
,
2994 ipv6_forwarding_cmd
,
2997 "Turn on IPv6 forwarding")
3001 ret
= ipforward_ipv6 ();
3003 ret
= ipforward_ipv6_on ();
3007 vty_outln (vty
, "Can't turn on IPv6 forwarding");
3008 return CMD_WARNING_CONFIG_FAILED
;
3014 DEFUN (no_ipv6_forwarding
,
3015 no_ipv6_forwarding_cmd
,
3016 "no ipv6 forwarding",
3019 "Turn off IPv6 forwarding")
3023 ret
= ipforward_ipv6 ();
3025 ret
= ipforward_ipv6_off ();
3029 vty_outln (vty
, "Can't turn off IPv6 forwarding");
3030 return CMD_WARNING_CONFIG_FAILED
;
3036 /* IPForwarding configuration write function. */
3038 config_write_forwarding (struct vty
*vty
)
3040 /* FIXME: Find better place for that. */
3041 router_id_write (vty
);
3044 vty_outln (vty
, "no ip forwarding");
3045 if (!ipforward_ipv6 ())
3046 vty_outln (vty
, "no ipv6 forwarding");
3047 vty_outln (vty
, "!");
3051 /* table node for routing tables. */
3052 static struct cmd_node forwarding_node
=
3055 "", /* This node has no interface. */
3059 /* Initialisation of zebra and installation of commands. */
3063 /* Client list init. */
3064 zebrad
.client_list
= list_new ();
3066 /* Install configuration write function. */
3067 install_node (&table_node
, config_write_table
);
3068 install_node (&forwarding_node
, config_write_forwarding
);
3070 install_element (VIEW_NODE
, &show_ip_forwarding_cmd
);
3071 install_element (CONFIG_NODE
, &ip_forwarding_cmd
);
3072 install_element (CONFIG_NODE
, &no_ip_forwarding_cmd
);
3073 install_element (ENABLE_NODE
, &show_zebra_cmd
);
3074 install_element (ENABLE_NODE
, &show_zebra_client_cmd
);
3075 install_element (ENABLE_NODE
, &show_zebra_client_summary_cmd
);
3078 install_element (VIEW_NODE
, &show_table_cmd
);
3079 install_element (CONFIG_NODE
, &config_table_cmd
);
3080 install_element (CONFIG_NODE
, &no_config_table_cmd
);
3081 #endif /* HAVE_NETLINK */
3083 install_element (VIEW_NODE
, &show_ipv6_forwarding_cmd
);
3084 install_element (CONFIG_NODE
, &ipv6_forwarding_cmd
);
3085 install_element (CONFIG_NODE
, &no_ipv6_forwarding_cmd
);
3088 zebra_route_map_init ();
3091 /* Make zebra server socket, wiping any existing one (see bug #403). */
3093 zebra_zserv_socket_init (char *path
)
3095 #ifdef HAVE_TCP_ZEBRA
3098 zebra_serv_un (path
? path
: ZEBRA_SERV_PATH
);
3099 #endif /* HAVE_TCP_ZEBRA */