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
17 * along with GNU Zebra; see the file COPYING. If not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
30 #include "zebra_memory.h"
34 #include "sockunion.h"
43 #include "zebra/zserv.h"
44 #include "zebra/zebra_ns.h"
45 #include "zebra/zebra_vrf.h"
46 #include "zebra/router-id.h"
47 #include "zebra/redistribute.h"
48 #include "zebra/debug.h"
49 #include "zebra/ipforward.h"
50 #include "zebra/zebra_rnh.h"
51 #include "zebra/rt_netlink.h"
52 #include "zebra/interface.h"
53 #include "zebra/zebra_ptm.h"
54 #include "zebra/rtadv.h"
55 #include "zebra/zebra_mpls.h"
56 #include "zebra/zebra_mroute.h"
57 #include "zebra/label_manager.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
= thread_add_write(zebrad
.master
, zserv_flush_data
,
99 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
= thread_add_event(zebrad
.master
, zserv_delayed_close
,
135 THREAD_OFF(client
->t_write
);
138 THREAD_WRITE_ON(zebrad
.master
, client
->t_write
,
139 zserv_flush_data
, client
, client
->sock
);
143 client
->last_write_time
= monotime(NULL
);
148 zserv_create_header (struct stream
*s
, uint16_t cmd
, vrf_id_t vrf_id
)
150 /* length placeholder, caller can update */
151 stream_putw (s
, ZEBRA_HEADER_SIZE
);
152 stream_putc (s
, ZEBRA_HEADER_MARKER
);
153 stream_putc (s
, ZSERV_VERSION
);
154 stream_putw (s
, vrf_id
);
155 stream_putw (s
, cmd
);
159 zserv_encode_interface (struct stream
*s
, struct interface
*ifp
)
161 /* Interface information. */
162 stream_put (s
, ifp
->name
, INTERFACE_NAMSIZ
);
163 stream_putl (s
, ifp
->ifindex
);
164 stream_putc (s
, ifp
->status
);
165 stream_putq (s
, ifp
->flags
);
166 stream_putc (s
, ifp
->ptm_enable
);
167 stream_putc (s
, ifp
->ptm_status
);
168 stream_putl (s
, ifp
->metric
);
169 stream_putl (s
, ifp
->speed
);
170 stream_putl (s
, ifp
->mtu
);
171 stream_putl (s
, ifp
->mtu6
);
172 stream_putl (s
, ifp
->bandwidth
);
173 stream_putl (s
, ifp
->ll_type
);
174 stream_putl (s
, ifp
->hw_addr_len
);
175 if (ifp
->hw_addr_len
)
176 stream_put (s
, ifp
->hw_addr
, ifp
->hw_addr_len
);
178 /* Then, Traffic Engineering parameters if any */
179 if (HAS_LINK_PARAMS(ifp
) && IS_LINK_PARAMS_SET(ifp
->link_params
))
182 zebra_interface_link_params_write (s
, ifp
);
187 /* Write packet size. */
188 stream_putw_at (s
, 0, stream_get_endp (s
));
192 zserv_encode_vrf (struct stream
*s
, struct zebra_vrf
*zvrf
)
194 /* Interface information. */
195 stream_put (s
, zvrf_name (zvrf
), VRF_NAMSIZ
);
197 /* Write packet size. */
198 stream_putw_at (s
, 0, stream_get_endp (s
));
201 /* Interface is added. Send ZEBRA_INTERFACE_ADD to client. */
203 * This function is called in the following situations:
204 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
206 * - at startup, when zebra figures out the available interfaces
207 * - when an interface is added (where support for
208 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
209 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
213 zsend_interface_add (struct zserv
*client
, struct interface
*ifp
)
220 zserv_create_header (s
, ZEBRA_INTERFACE_ADD
, ifp
->vrf_id
);
221 zserv_encode_interface (s
, ifp
);
224 return zebra_server_send_message(client
);
227 /* Interface deletion from zebra daemon. */
229 zsend_interface_delete (struct zserv
*client
, struct interface
*ifp
)
236 zserv_create_header (s
, ZEBRA_INTERFACE_DELETE
, ifp
->vrf_id
);
237 zserv_encode_interface (s
, ifp
);
240 return zebra_server_send_message (client
);
244 zsend_vrf_add (struct zserv
*client
, struct zebra_vrf
*zvrf
)
251 zserv_create_header (s
, ZEBRA_VRF_ADD
, zvrf_id (zvrf
));
252 zserv_encode_vrf (s
, zvrf
);
254 client
->vrfadd_cnt
++;
255 return zebra_server_send_message(client
);
258 /* VRF deletion from zebra daemon. */
260 zsend_vrf_delete (struct zserv
*client
, struct zebra_vrf
*zvrf
)
267 zserv_create_header (s
, ZEBRA_VRF_DELETE
, zvrf_id (zvrf
));
268 zserv_encode_vrf (s
, zvrf
);
270 client
->vrfdel_cnt
++;
271 return zebra_server_send_message (client
);
275 zsend_interface_link_params (struct zserv
*client
, struct interface
*ifp
)
279 /* Check this client need interface information. */
280 if (! client
->ifinfo
)
283 if (!ifp
->link_params
)
288 zserv_create_header (s
, ZEBRA_INTERFACE_LINK_PARAMS
, ifp
->vrf_id
);
290 /* Add Interface Index */
291 stream_putl (s
, ifp
->ifindex
);
293 /* Then TE Link Parameters */
294 if (zebra_interface_link_params_write (s
, ifp
) == 0)
297 /* Write packet size. */
298 stream_putw_at (s
, 0, stream_get_endp (s
));
300 return zebra_server_send_message (client
);
303 /* Interface address is added/deleted. Send ZEBRA_INTERFACE_ADDRESS_ADD or
304 * ZEBRA_INTERFACE_ADDRESS_DELETE to the client.
306 * A ZEBRA_INTERFACE_ADDRESS_ADD is sent in the following situations:
307 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
308 * from the client, after the ZEBRA_INTERFACE_ADD has been
309 * sent from zebra to the client
310 * - redistribute new address info to all clients in the following situations
311 * - at startup, when zebra figures out the available interfaces
312 * - when an interface is added (where support for
313 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
314 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
316 * - for the vty commands "ip address A.B.C.D/M [<secondary>|<label LINE>]"
317 * and "no bandwidth <1-10000000>", "ipv6 address X:X::X:X/M"
318 * - when an RTM_NEWADDR message is received from the kernel,
320 * The call tree that triggers ZEBRA_INTERFACE_ADDRESS_DELETE:
322 * zsend_interface_address(DELETE)
325 * zebra_interface_address_delete_update
327 * | | if_delete_update
329 * ip_address_uninstall connected_delete_ipv4
330 * [ipv6_addresss_uninstall] [connected_delete_ipv6]
333 * | RTM_NEWADDR on routing/netlink socket
336 * "no ip address A.B.C.D/M [label LINE]"
337 * "no ip address A.B.C.D/M secondary"
338 * ["no ipv6 address X:X::X:X/M"]
342 zsend_interface_address (int cmd
, struct zserv
*client
,
343 struct interface
*ifp
, struct connected
*ifc
)
352 zserv_create_header (s
, cmd
, ifp
->vrf_id
);
353 stream_putl (s
, ifp
->ifindex
);
355 /* Interface address flag. */
356 stream_putc (s
, ifc
->flags
);
358 /* Prefix information. */
360 stream_putc (s
, p
->family
);
361 blen
= prefix_blen (p
);
362 stream_put (s
, &p
->u
.prefix
, blen
);
365 * XXX gnu version does not send prefixlen for ZEBRA_INTERFACE_ADDRESS_DELETE
366 * but zebra_interface_address_delete_read() in the gnu version
369 stream_putc (s
, p
->prefixlen
);
372 p
= ifc
->destination
;
374 stream_put (s
, &p
->u
.prefix
, blen
);
376 stream_put (s
, NULL
, blen
);
378 /* Write packet size. */
379 stream_putw_at (s
, 0, stream_get_endp (s
));
381 client
->connected_rt_add_cnt
++;
382 return zebra_server_send_message(client
);
386 zsend_interface_nbr_address (int cmd
, struct zserv
*client
,
387 struct interface
*ifp
, struct nbr_connected
*ifc
)
396 zserv_create_header (s
, cmd
, ifp
->vrf_id
);
397 stream_putl (s
, ifp
->ifindex
);
399 /* Prefix information. */
401 stream_putc (s
, p
->family
);
402 blen
= prefix_blen (p
);
403 stream_put (s
, &p
->u
.prefix
, blen
);
406 * XXX gnu version does not send prefixlen for ZEBRA_INTERFACE_ADDRESS_DELETE
407 * but zebra_interface_address_delete_read() in the gnu version
410 stream_putc (s
, p
->prefixlen
);
412 /* Write packet size. */
413 stream_putw_at (s
, 0, stream_get_endp (s
));
415 return zebra_server_send_message(client
);
418 /* Interface address addition. */
420 zebra_interface_nbr_address_add_update (struct interface
*ifp
,
421 struct nbr_connected
*ifc
)
423 struct listnode
*node
, *nnode
;
424 struct zserv
*client
;
427 if (IS_ZEBRA_DEBUG_EVENT
)
429 char buf
[INET6_ADDRSTRLEN
];
432 zlog_debug ("MESSAGE: ZEBRA_INTERFACE_NBR_ADDRESS_ADD %s/%d on %s",
433 inet_ntop (p
->family
, &p
->u
.prefix
, buf
, INET6_ADDRSTRLEN
),
434 p
->prefixlen
, ifc
->ifp
->name
);
437 for (ALL_LIST_ELEMENTS (zebrad
.client_list
, node
, nnode
, client
))
438 zsend_interface_nbr_address (ZEBRA_INTERFACE_NBR_ADDRESS_ADD
, client
, ifp
, ifc
);
441 /* Interface address deletion. */
443 zebra_interface_nbr_address_delete_update (struct interface
*ifp
,
444 struct nbr_connected
*ifc
)
446 struct listnode
*node
, *nnode
;
447 struct zserv
*client
;
450 if (IS_ZEBRA_DEBUG_EVENT
)
452 char buf
[INET6_ADDRSTRLEN
];
455 zlog_debug ("MESSAGE: ZEBRA_INTERFACE_NBR_ADDRESS_DELETE %s/%d on %s",
456 inet_ntop (p
->family
, &p
->u
.prefix
, buf
, INET6_ADDRSTRLEN
),
457 p
->prefixlen
, ifc
->ifp
->name
);
460 for (ALL_LIST_ELEMENTS (zebrad
.client_list
, node
, nnode
, client
))
461 zsend_interface_nbr_address (ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
, client
, ifp
, ifc
);
464 /* Send addresses on interface to client */
466 zsend_interface_addresses (struct zserv
*client
, struct interface
*ifp
)
468 struct listnode
*cnode
, *cnnode
;
470 struct nbr_connected
*nc
;
472 /* Send interface addresses. */
473 for (ALL_LIST_ELEMENTS (ifp
->connected
, cnode
, cnnode
, c
))
475 if (!CHECK_FLAG (c
->conf
, ZEBRA_IFC_REAL
))
478 if (zsend_interface_address (ZEBRA_INTERFACE_ADDRESS_ADD
, client
,
483 /* Send interface neighbors. */
484 for (ALL_LIST_ELEMENTS (ifp
->nbr_connected
, cnode
, cnnode
, nc
))
486 if (zsend_interface_nbr_address (ZEBRA_INTERFACE_NBR_ADDRESS_ADD
,
487 client
, ifp
, nc
) < 0)
494 /* Notify client about interface moving from one VRF to another.
495 * Whether client is interested in old and new VRF is checked by caller.
498 zsend_interface_vrf_update (struct zserv
*client
, struct interface
*ifp
,
506 zserv_create_header (s
, ZEBRA_INTERFACE_VRF_UPDATE
, ifp
->vrf_id
);
508 /* Fill in the ifIndex of the interface and its new VRF (id) */
509 stream_putl (s
, ifp
->ifindex
);
510 stream_putw (s
, vrf_id
);
512 /* Write packet size. */
513 stream_putw_at (s
, 0, stream_get_endp (s
));
515 client
->if_vrfchg_cnt
++;
516 return zebra_server_send_message(client
);
519 /* Add new nbr connected IPv6 address */
521 nbr_connected_add_ipv6 (struct interface
*ifp
, struct in6_addr
*address
)
523 struct nbr_connected
*ifc
;
527 IPV6_ADDR_COPY (&p
.u
.prefix
, address
);
528 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
530 if (!(ifc
= listnode_head(ifp
->nbr_connected
)))
533 ifc
= nbr_connected_new ();
534 ifc
->address
= prefix_new();
536 listnode_add (ifp
->nbr_connected
, ifc
);
539 prefix_copy(ifc
->address
, &p
);
541 zebra_interface_nbr_address_add_update (ifp
, ifc
);
543 if_nbr_ipv6ll_to_ipv4ll_neigh_update (ifp
, address
, 1);
547 nbr_connected_delete_ipv6 (struct interface
*ifp
, struct in6_addr
*address
)
549 struct nbr_connected
*ifc
;
553 IPV6_ADDR_COPY (&p
.u
.prefix
, address
);
554 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
556 ifc
= nbr_connected_check(ifp
, &p
);
560 listnode_delete (ifp
->nbr_connected
, ifc
);
562 zebra_interface_nbr_address_delete_update (ifp
, ifc
);
564 if_nbr_ipv6ll_to_ipv4ll_neigh_update (ifp
, address
, 0);
566 nbr_connected_free (ifc
);
570 * The cmd passed to zsend_interface_update may be ZEBRA_INTERFACE_UP or
571 * ZEBRA_INTERFACE_DOWN.
573 * The ZEBRA_INTERFACE_UP message is sent from the zebra server to
574 * the clients in one of 2 situations:
575 * - an if_up is detected e.g., as a result of an RTM_IFINFO message
576 * - a vty command modifying the bandwidth of an interface is received.
577 * The ZEBRA_INTERFACE_DOWN message is sent when an if_down is detected.
580 zsend_interface_update (int cmd
, struct zserv
*client
, struct interface
*ifp
)
587 zserv_create_header (s
, cmd
, ifp
->vrf_id
);
588 zserv_encode_interface (s
, ifp
);
590 if (cmd
== ZEBRA_INTERFACE_UP
)
593 client
->ifdown_cnt
++;
595 return zebra_server_send_message(client
);
599 * This is the new function to announce and withdraw redistributed routes, used
600 * by Zebra. This is the old zsend_route_multipath() function. That function
601 * was duplicating code to send a lot of information that was essentially thrown
602 * away or ignored by the receiver. This is the leaner function that is not a
603 * duplicate of the zapi_ipv4_route_add/del.
605 * The primary difference is that this function merely sends a single NH instead of
609 zsend_redistribute_route (int add
, struct zserv
*client
, struct prefix
*p
,
610 struct prefix
*src_p
, struct rib
*rib
)
616 struct nexthop
*nexthop
;
617 unsigned long nhnummark
= 0, messmark
= 0;
619 u_char zapi_flags
= 0;
620 struct nexthop dummy_nh
;
622 afi
= family2afi (p
->family
);
628 cmd
= ZEBRA_REDISTRIBUTE_IPV4_ADD
;
629 client
->redist_v4_add_cnt
++;
632 cmd
= ZEBRA_REDISTRIBUTE_IPV6_ADD
;
633 client
->redist_v6_add_cnt
++;
644 cmd
= ZEBRA_REDISTRIBUTE_IPV4_DEL
;
645 client
->redist_v4_del_cnt
++;
648 cmd
= ZEBRA_REDISTRIBUTE_IPV6_DEL
;
649 client
->redist_v6_del_cnt
++;
658 memset(&dummy_nh
, 0, sizeof(struct nexthop
));
660 zserv_create_header (s
, cmd
, rib
->vrf_id
);
662 /* Put type and nexthop. */
663 stream_putc (s
, rib
->type
);
664 stream_putw (s
, rib
->instance
);
665 stream_putl (s
, rib
->flags
);
667 /* marker for message flags field */
668 messmark
= stream_get_endp (s
);
672 psize
= PSIZE (p
->prefixlen
);
673 stream_putc (s
, p
->prefixlen
);
674 stream_write (s
, (u_char
*) & p
->u
.prefix
, psize
);
678 SET_FLAG (zapi_flags
, ZAPI_MESSAGE_SRCPFX
);
679 psize
= PSIZE (src_p
->prefixlen
);
680 stream_putc (s
, src_p
->prefixlen
);
681 stream_write (s
, (u_char
*) & src_p
->u
.prefix
, psize
);
684 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
686 /* We don't send any nexthops when there's a multipath */
687 if (rib
->nexthop_active_num
> 1 && client
->proto
!= ZEBRA_ROUTE_LDP
)
689 SET_FLAG (zapi_flags
, ZAPI_MESSAGE_NEXTHOP
);
690 SET_FLAG (zapi_flags
, ZAPI_MESSAGE_IFINDEX
);
693 if (p
->family
== AF_INET
)
695 stream_put_in_addr (s
, &dummy_nh
.gate
.ipv4
);
697 else if (p
->family
== AF_INET6
)
699 stream_write (s
, (u_char
*) &dummy_nh
.gate
.ipv6
, 16);
703 /* We don't handle anything else now, abort */
704 zlog_err("%s: Unable to redistribute route of unknown family, %d\n",
705 __func__
, p
->family
);
709 stream_putl (s
, 0); /* dummy ifindex */
713 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
715 SET_FLAG (zapi_flags
, ZAPI_MESSAGE_NEXTHOP
);
716 SET_FLAG (zapi_flags
, ZAPI_MESSAGE_IFINDEX
);
719 nhnummark
= stream_get_endp (s
);
720 stream_putc (s
, 1); /* placeholder */
724 switch(nexthop
->type
)
726 case NEXTHOP_TYPE_IPV4
:
727 case NEXTHOP_TYPE_IPV4_IFINDEX
:
728 stream_put_in_addr (s
, &nexthop
->gate
.ipv4
);
730 case NEXTHOP_TYPE_IPV6
:
731 case NEXTHOP_TYPE_IPV6_IFINDEX
:
732 /* Only BGP supports IPv4 prefix with IPv6 NH, so kill this */
733 if (p
->family
== AF_INET
)
734 stream_put_in_addr(s
, &dummy_nh
.gate
.ipv4
);
736 stream_write (s
, (u_char
*) &nexthop
->gate
.ipv6
, 16);
739 if (cmd
== ZEBRA_REDISTRIBUTE_IPV4_ADD
740 || cmd
== ZEBRA_REDISTRIBUTE_IPV4_DEL
)
742 struct in_addr empty
;
743 memset (&empty
, 0, sizeof (struct in_addr
));
744 stream_write (s
, (u_char
*) &empty
, IPV4_MAX_BYTELEN
);
748 struct in6_addr empty
;
749 memset (&empty
, 0, sizeof (struct in6_addr
));
750 stream_write (s
, (u_char
*) &empty
, IPV6_MAX_BYTELEN
);
754 /* Interface index. */
756 stream_putl (s
, nexthop
->ifindex
);
758 /* ldpd needs all nexthops */
759 if (client
->proto
!= ZEBRA_ROUTE_LDP
)
765 SET_FLAG (zapi_flags
, ZAPI_MESSAGE_DISTANCE
);
766 stream_putc (s
, rib
->distance
);
769 SET_FLAG (zapi_flags
, ZAPI_MESSAGE_METRIC
);
770 stream_putl (s
, rib
->metric
);
775 SET_FLAG(zapi_flags
, ZAPI_MESSAGE_TAG
);
776 stream_putl(s
, rib
->tag
);
780 SET_FLAG (zapi_flags
, ZAPI_MESSAGE_MTU
);
781 stream_putl (s
, rib
->mtu
);
783 /* write real message flags value */
784 stream_putc_at (s
, messmark
, zapi_flags
);
786 /* Write next-hop number */
788 stream_putc_at (s
, nhnummark
, nhnum
);
790 /* Write packet size. */
791 stream_putw_at (s
, 0, stream_get_endp (s
));
793 return zebra_server_send_message(client
);
797 zsend_write_nexthop (struct stream
*s
, struct nexthop
*nexthop
)
799 stream_putc (s
, nexthop
->type
);
800 switch (nexthop
->type
)
802 case NEXTHOP_TYPE_IPV4
:
803 case NEXTHOP_TYPE_IPV4_IFINDEX
:
804 stream_put_in_addr (s
, &nexthop
->gate
.ipv4
);
805 stream_putl (s
, nexthop
->ifindex
);
807 case NEXTHOP_TYPE_IPV6
:
808 stream_put (s
, &nexthop
->gate
.ipv6
, 16);
810 case NEXTHOP_TYPE_IPV6_IFINDEX
:
811 stream_put (s
, &nexthop
->gate
.ipv6
, 16);
812 stream_putl (s
, nexthop
->ifindex
);
814 case NEXTHOP_TYPE_IFINDEX
:
815 stream_putl (s
, nexthop
->ifindex
);
824 /* Nexthop register */
826 zserv_rnh_register (struct zserv
*client
, int sock
, u_short length
,
827 rnh_type_t type
, struct zebra_vrf
*zvrf
)
835 if (IS_ZEBRA_DEBUG_NHT
)
836 zlog_debug("rnh_register msg from client %s: length=%d, type=%s\n",
837 zebra_route_string(client
->proto
), length
,
838 (type
== RNH_NEXTHOP_TYPE
) ? "nexthop" : "route");
842 client
->nh_reg_time
= monotime(NULL
);
846 flags
= stream_getc(s
);
847 p
.family
= stream_getw(s
);
848 p
.prefixlen
= stream_getc(s
);
850 if (p
.family
== AF_INET
)
852 p
.u
.prefix4
.s_addr
= stream_get_ipv4(s
);
853 l
+= IPV4_MAX_BYTELEN
;
855 else if (p
.family
== AF_INET6
)
857 stream_get(&p
.u
.prefix6
, s
, IPV6_MAX_BYTELEN
);
858 l
+= IPV6_MAX_BYTELEN
;
862 zlog_err("rnh_register: Received unknown family type %d\n",
866 rnh
= zebra_add_rnh(&p
, zvrf_id (zvrf
), type
);
867 if (type
== RNH_NEXTHOP_TYPE
)
869 if (flags
&& !CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
))
870 SET_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
);
871 else if (!flags
&& CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
))
872 UNSET_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
);
874 else if (type
== RNH_IMPORT_CHECK_TYPE
)
876 if (flags
&& !CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
))
877 SET_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
);
878 else if (!flags
&& CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
))
879 UNSET_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
);
882 zebra_add_rnh_client(rnh
, client
, type
, zvrf_id (zvrf
));
883 /* Anything not AF_INET/INET6 has been filtered out above */
884 zebra_evaluate_rnh(zvrf_id (zvrf
), p
.family
, 1, type
, &p
);
889 /* Nexthop register */
891 zserv_rnh_unregister (struct zserv
*client
, int sock
, u_short length
,
892 rnh_type_t type
, struct zebra_vrf
*zvrf
)
899 if (IS_ZEBRA_DEBUG_NHT
)
900 zlog_debug("rnh_unregister msg from client %s: length=%d\n",
901 zebra_route_string(client
->proto
), length
);
907 (void)stream_getc(s
); //Connected or not. Not used in this function
908 p
.family
= stream_getw(s
);
909 p
.prefixlen
= stream_getc(s
);
911 if (p
.family
== AF_INET
)
913 p
.u
.prefix4
.s_addr
= stream_get_ipv4(s
);
914 l
+= IPV4_MAX_BYTELEN
;
916 else if (p
.family
== AF_INET6
)
918 stream_get(&p
.u
.prefix6
, s
, IPV6_MAX_BYTELEN
);
919 l
+= IPV6_MAX_BYTELEN
;
923 zlog_err("rnh_register: Received unknown family type %d\n",
927 rnh
= zebra_lookup_rnh(&p
, zvrf_id (zvrf
), type
);
930 client
->nh_dereg_time
= monotime(NULL
);
931 zebra_remove_rnh_client(rnh
, client
, type
);
938 Modified version of zsend_ipv4_nexthop_lookup():
939 Query unicast rib if nexthop is not found on mrib.
940 Returns both route metric and protocol distance.
943 zsend_ipv4_nexthop_lookup_mrib (struct zserv
*client
, struct in_addr addr
, struct rib
*rib
, struct zebra_vrf
*zvrf
)
948 struct nexthop
*nexthop
;
950 /* Get output stream. */
954 /* Fill in result. */
955 zserv_create_header (s
, ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB
, zvrf_id (zvrf
));
956 stream_put_in_addr (s
, &addr
);
960 stream_putc (s
, rib
->distance
);
961 stream_putl (s
, rib
->metric
);
963 nump
= stream_get_endp(s
); /* remember position for nexthop_num */
964 stream_putc (s
, 0); /* reserve room for nexthop_num */
965 /* Only non-recursive routes are elegible to resolve the nexthop we
966 * are looking up. Therefore, we will just iterate over the top
967 * chain of nexthops. */
968 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
969 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
970 num
+= zsend_write_nexthop (s
, nexthop
);
972 stream_putc_at (s
, nump
, num
); /* store nexthop_num */
976 stream_putc (s
, 0); /* distance */
977 stream_putl (s
, 0); /* metric */
978 stream_putc (s
, 0); /* nexthop_num */
981 stream_putw_at (s
, 0, stream_get_endp (s
));
983 return zebra_server_send_message(client
);
986 /* Router-id is updated. Send ZEBRA_ROUTER_ID_ADD to client. */
988 zsend_router_id_update (struct zserv
*client
, struct prefix
*p
,
994 /* Check this client need interface information. */
995 if (! vrf_bitmap_check (client
->ridinfo
, vrf_id
))
1002 zserv_create_header (s
, ZEBRA_ROUTER_ID_UPDATE
, vrf_id
);
1004 /* Prefix information. */
1005 stream_putc (s
, p
->family
);
1006 blen
= prefix_blen (p
);
1007 stream_put (s
, &p
->u
.prefix
, blen
);
1008 stream_putc (s
, p
->prefixlen
);
1010 /* Write packet size. */
1011 stream_putw_at (s
, 0, stream_get_endp (s
));
1013 return zebra_server_send_message(client
);
1016 /* Register zebra server interface information. Send current all
1017 interface and address information. */
1019 zread_interface_add (struct zserv
*client
, u_short length
, struct zebra_vrf
*zvrf
)
1022 struct listnode
*ifnode
, *ifnnode
;
1023 struct interface
*ifp
;
1025 /* Interface information is needed. */
1026 vrf_bitmap_set (client
->ifinfo
, zvrf_id (zvrf
));
1028 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
)
1030 for (ALL_LIST_ELEMENTS (vrf
->iflist
, ifnode
, ifnnode
, ifp
))
1032 /* Skip pseudo interface. */
1033 if (! CHECK_FLAG (ifp
->status
, ZEBRA_INTERFACE_ACTIVE
))
1036 if (zsend_interface_add (client
, ifp
) < 0)
1039 if (zsend_interface_addresses (client
, ifp
) < 0)
1046 /* Unregister zebra server interface information. */
1048 zread_interface_delete (struct zserv
*client
, u_short length
, struct zebra_vrf
*zvrf
)
1050 vrf_bitmap_unset (client
->ifinfo
, zvrf_id (zvrf
));
1055 zserv_nexthop_num_warn (const char *caller
, const struct prefix
*p
, const unsigned int nexthop_num
)
1057 if (nexthop_num
> multipath_num
)
1059 char buff
[PREFIX2STR_BUFFER
];
1060 prefix2str(p
, buff
, sizeof (buff
));
1061 zlog_warn("%s: Prefix %s has %d nexthops, but we can only use the first %d",
1062 caller
, buff
, nexthop_num
, multipath_num
);
1066 /* This function support multiple nexthop. */
1068 * Parse the ZEBRA_IPV4_ROUTE_ADD sent from client. Update rib and
1072 zread_ipv4_add (struct zserv
*client
, u_short length
, struct zebra_vrf
*zvrf
)
1078 struct in_addr nexthop
;
1080 u_char nexthop_type
;
1086 /* Get input stream. */
1089 /* Allocate new rib. */
1090 rib
= XCALLOC (MTYPE_RIB
, sizeof (struct rib
));
1092 /* Type, flags, message. */
1093 rib
->type
= stream_getc (s
);
1094 rib
->instance
= stream_getw (s
);
1095 rib
->flags
= stream_getl (s
);
1096 message
= stream_getc (s
);
1097 safi
= stream_getw (s
);
1098 rib
->uptime
= time (NULL
);
1101 memset (&p
, 0, sizeof (struct prefix_ipv4
));
1103 p
.prefixlen
= stream_getc (s
);
1104 stream_get (&p
.u
.prefix4
, s
, PSIZE (p
.prefixlen
));
1107 rib
->vrf_id
= zvrf_id (zvrf
);
1109 /* Nexthop parse. */
1110 if (CHECK_FLAG (message
, ZAPI_MESSAGE_NEXTHOP
))
1112 nexthop_num
= stream_getc (s
);
1113 zserv_nexthop_num_warn(__func__
, (const struct prefix
*)&p
, nexthop_num
);
1115 for (i
= 0; i
< nexthop_num
; i
++)
1117 nexthop_type
= stream_getc (s
);
1119 switch (nexthop_type
)
1121 case NEXTHOP_TYPE_IFINDEX
:
1122 ifindex
= stream_getl (s
);
1123 rib_nexthop_ifindex_add (rib
, ifindex
);
1125 case NEXTHOP_TYPE_IPV4
:
1126 nexthop
.s_addr
= stream_get_ipv4 (s
);
1127 rib_nexthop_ipv4_add (rib
, &nexthop
, NULL
);
1129 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1130 nexthop
.s_addr
= stream_get_ipv4 (s
);
1131 ifindex
= stream_getl (s
);
1132 rib_nexthop_ipv4_ifindex_add (rib
, &nexthop
, NULL
, ifindex
);
1134 case NEXTHOP_TYPE_IPV6
:
1135 stream_forward_getp (s
, IPV6_MAX_BYTELEN
);
1137 case NEXTHOP_TYPE_BLACKHOLE
:
1138 rib_nexthop_blackhole_add (rib
);
1145 if (CHECK_FLAG (message
, ZAPI_MESSAGE_DISTANCE
))
1146 rib
->distance
= stream_getc (s
);
1149 if (CHECK_FLAG (message
, ZAPI_MESSAGE_METRIC
))
1150 rib
->metric
= stream_getl (s
);
1153 if (CHECK_FLAG (message
, ZAPI_MESSAGE_TAG
))
1154 rib
->tag
= stream_getl (s
);
1158 if (CHECK_FLAG (message
, ZAPI_MESSAGE_MTU
))
1159 rib
->mtu
= stream_getl (s
);
1164 rib
->table
= zvrf
->table_id
;
1166 ret
= rib_add_multipath (AFI_IP
, safi
, &p
, NULL
, rib
);
1170 client
->v4_route_add_cnt
++;
1172 client
->v4_route_upd8_cnt
++;
1176 /* Zebra server IPv4 prefix delete function. */
1178 zread_ipv4_delete (struct zserv
*client
, u_short length
, struct zebra_vrf
*zvrf
)
1182 struct zapi_ipv4 api
;
1183 struct in_addr nexthop
;
1184 union g_addr
*nexthop_p
;
1185 unsigned long ifindex
;
1188 u_char nexthop_type
;
1196 /* Type, flags, message. */
1197 api
.type
= stream_getc (s
);
1198 api
.instance
= stream_getw (s
);
1199 api
.flags
= stream_getl (s
);
1200 api
.message
= stream_getc (s
);
1201 api
.safi
= stream_getw (s
);
1204 memset (&p
, 0, sizeof (struct prefix
));
1206 p
.prefixlen
= stream_getc (s
);
1207 stream_get (&p
.u
.prefix4
, s
, PSIZE (p
.prefixlen
));
1209 /* Nexthop, ifindex, distance, metric. */
1210 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
))
1212 nexthop_num
= stream_getc (s
);
1214 for (i
= 0; i
< nexthop_num
; i
++)
1216 nexthop_type
= stream_getc (s
);
1218 switch (nexthop_type
)
1220 case NEXTHOP_TYPE_IFINDEX
:
1221 ifindex
= stream_getl (s
);
1223 case NEXTHOP_TYPE_IPV4
:
1224 nexthop
.s_addr
= stream_get_ipv4 (s
);
1225 nexthop_p
= (union g_addr
*)&nexthop
;
1227 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1228 nexthop
.s_addr
= stream_get_ipv4 (s
);
1229 nexthop_p
= (union g_addr
*)&nexthop
;
1230 ifindex
= stream_getl (s
);
1232 case NEXTHOP_TYPE_IPV6
:
1233 stream_forward_getp (s
, IPV6_MAX_BYTELEN
);
1240 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_DISTANCE
))
1241 api
.distance
= stream_getc (s
);
1246 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_METRIC
))
1247 api
.metric
= stream_getl (s
);
1252 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_TAG
))
1253 api
.tag
= stream_getl (s
);
1257 table_id
= zvrf
->table_id
;
1259 rib_delete (AFI_IP
, api
.safi
, zvrf_id (zvrf
), api
.type
, api
.instance
,
1260 api
.flags
, &p
, NULL
, nexthop_p
, ifindex
, table_id
);
1261 client
->v4_route_del_cnt
++;
1265 /* MRIB Nexthop lookup for IPv4. */
1267 zread_ipv4_nexthop_lookup_mrib (struct zserv
*client
, u_short length
, struct zebra_vrf
*zvrf
)
1269 struct in_addr addr
;
1272 addr
.s_addr
= stream_get_ipv4 (client
->ibuf
);
1273 rib
= rib_match_ipv4_multicast (zvrf_id (zvrf
), addr
, NULL
);
1274 return zsend_ipv4_nexthop_lookup_mrib (client
, addr
, rib
, zvrf
);
1277 /* Zebra server IPv6 prefix add function. */
1279 zread_ipv4_route_ipv6_nexthop_add (struct zserv
*client
, u_short length
, struct zebra_vrf
*zvrf
)
1283 struct in6_addr nexthop
;
1287 u_char nexthop_type
;
1290 static struct in6_addr nexthops
[MULTIPATH_NUM
];
1291 static unsigned int ifindices
[MULTIPATH_NUM
];
1294 /* Get input stream. */
1297 memset (&nexthop
, 0, sizeof (struct in6_addr
));
1299 /* Allocate new rib. */
1300 rib
= XCALLOC (MTYPE_RIB
, sizeof (struct rib
));
1302 /* Type, flags, message. */
1303 rib
->type
= stream_getc (s
);
1304 rib
->instance
= stream_getw (s
);
1305 rib
->flags
= stream_getl (s
);
1306 message
= stream_getc (s
);
1307 safi
= stream_getw (s
);
1308 rib
->uptime
= time (NULL
);
1311 memset (&p
, 0, sizeof (struct prefix_ipv4
));
1313 p
.prefixlen
= stream_getc (s
);
1314 stream_get (&p
.u
.prefix4
, s
, PSIZE (p
.prefixlen
));
1317 rib
->vrf_id
= zvrf_id (zvrf
);
1319 /* We need to give nh-addr, nh-ifindex with the same next-hop object
1320 * to the rib to ensure that IPv6 multipathing works; need to coalesce
1321 * these. Clients should send the same number of paired set of
1322 * next-hop-addr/next-hop-ifindices. */
1323 if (CHECK_FLAG (message
, ZAPI_MESSAGE_NEXTHOP
))
1325 unsigned int nh_count
= 0;
1326 unsigned int if_count
= 0;
1327 unsigned int max_nh_if
= 0;
1329 nexthop_num
= stream_getc (s
);
1330 zserv_nexthop_num_warn(__func__
, (const struct prefix
*)&p
, nexthop_num
);
1331 for (i
= 0; i
< nexthop_num
; i
++)
1333 nexthop_type
= stream_getc (s
);
1335 switch (nexthop_type
)
1337 case NEXTHOP_TYPE_IPV6
:
1338 stream_get (&nexthop
, s
, 16);
1339 if (nh_count
< multipath_num
) {
1340 nexthops
[nh_count
++] = nexthop
;
1343 case NEXTHOP_TYPE_IFINDEX
:
1344 if (if_count
< multipath_num
) {
1345 ifindices
[if_count
++] = stream_getl (s
);
1348 case NEXTHOP_TYPE_BLACKHOLE
:
1349 rib_nexthop_blackhole_add (rib
);
1354 max_nh_if
= (nh_count
> if_count
) ? nh_count
: if_count
;
1355 for (i
= 0; i
< max_nh_if
; i
++)
1357 if ((i
< nh_count
) && !IN6_IS_ADDR_UNSPECIFIED (&nexthops
[i
])) {
1358 if ((i
< if_count
) && ifindices
[i
]) {
1359 rib_nexthop_ipv6_ifindex_add (rib
, &nexthops
[i
], ifindices
[i
]);
1362 rib_nexthop_ipv6_add (rib
, &nexthops
[i
]);
1366 if ((i
< if_count
) && ifindices
[i
]) {
1367 rib_nexthop_ifindex_add (rib
, ifindices
[i
]);
1374 if (CHECK_FLAG (message
, ZAPI_MESSAGE_DISTANCE
))
1375 rib
->distance
= stream_getc (s
);
1378 if (CHECK_FLAG (message
, ZAPI_MESSAGE_METRIC
))
1379 rib
->metric
= stream_getl (s
);
1382 if (CHECK_FLAG (message
, ZAPI_MESSAGE_TAG
))
1383 rib
->tag
= stream_getl (s
);
1387 if (CHECK_FLAG (message
, ZAPI_MESSAGE_MTU
))
1388 rib
->mtu
= stream_getl (s
);
1393 rib
->table
= zvrf
->table_id
;
1395 ret
= rib_add_multipath (AFI_IP6
, safi
, &p
, NULL
, rib
);
1398 client
->v4_route_add_cnt
++;
1400 client
->v4_route_upd8_cnt
++;
1406 zread_ipv6_add (struct zserv
*client
, u_short length
, struct zebra_vrf
*zvrf
)
1410 struct in6_addr nexthop
;
1414 u_char nexthop_type
;
1416 struct prefix_ipv6 src_p
, *src_pp
;
1418 static struct in6_addr nexthops
[MULTIPATH_NUM
];
1419 static unsigned int ifindices
[MULTIPATH_NUM
];
1422 /* Get input stream. */
1425 memset (&nexthop
, 0, sizeof (struct in6_addr
));
1427 /* Allocate new rib. */
1428 rib
= XCALLOC (MTYPE_RIB
, sizeof (struct rib
));
1430 /* Type, flags, message. */
1431 rib
->type
= stream_getc (s
);
1432 rib
->instance
= stream_getw (s
);
1433 rib
->flags
= stream_getl (s
);
1434 message
= stream_getc (s
);
1435 safi
= stream_getw (s
);
1436 rib
->uptime
= time (NULL
);
1439 memset (&p
, 0, sizeof (struct prefix_ipv6
));
1440 p
.family
= AF_INET6
;
1441 p
.prefixlen
= stream_getc (s
);
1442 stream_get (&p
.u
.prefix6
, s
, PSIZE (p
.prefixlen
));
1444 if (CHECK_FLAG (message
, ZAPI_MESSAGE_SRCPFX
))
1446 memset (&src_p
, 0, sizeof (struct prefix_ipv6
));
1447 src_p
.family
= AF_INET6
;
1448 src_p
.prefixlen
= stream_getc (s
);
1449 stream_get (&src_p
.prefix
, s
, PSIZE (src_p
.prefixlen
));
1455 /* We need to give nh-addr, nh-ifindex with the same next-hop object
1456 * to the rib to ensure that IPv6 multipathing works; need to coalesce
1457 * these. Clients should send the same number of paired set of
1458 * next-hop-addr/next-hop-ifindices. */
1459 if (CHECK_FLAG (message
, ZAPI_MESSAGE_NEXTHOP
))
1461 unsigned int nh_count
= 0;
1462 unsigned int if_count
= 0;
1463 unsigned int max_nh_if
= 0;
1465 nexthop_num
= stream_getc (s
);
1466 zserv_nexthop_num_warn(__func__
, (const struct prefix
*)&p
, nexthop_num
);
1467 for (i
= 0; i
< nexthop_num
; i
++)
1469 nexthop_type
= stream_getc (s
);
1471 switch (nexthop_type
)
1473 case NEXTHOP_TYPE_IPV6
:
1474 stream_get (&nexthop
, s
, 16);
1475 if (nh_count
< multipath_num
) {
1476 nexthops
[nh_count
++] = nexthop
;
1479 case NEXTHOP_TYPE_IFINDEX
:
1480 if (if_count
< multipath_num
) {
1481 ifindices
[if_count
++] = stream_getl (s
);
1484 case NEXTHOP_TYPE_BLACKHOLE
:
1485 rib_nexthop_blackhole_add (rib
);
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 rib_nexthop_ipv6_ifindex_add (rib
, &nexthops
[i
], ifindices
[i
]);
1497 rib_nexthop_ipv6_add (rib
, &nexthops
[i
]);
1500 if ((i
< if_count
) && ifindices
[i
])
1501 rib_nexthop_ifindex_add (rib
, ifindices
[i
]);
1507 if (CHECK_FLAG (message
, ZAPI_MESSAGE_DISTANCE
))
1508 rib
->distance
= stream_getc (s
);
1511 if (CHECK_FLAG (message
, ZAPI_MESSAGE_METRIC
))
1512 rib
->metric
= stream_getl (s
);
1515 if (CHECK_FLAG (message
, ZAPI_MESSAGE_TAG
))
1516 rib
->tag
= stream_getl (s
);
1520 if (CHECK_FLAG (message
, ZAPI_MESSAGE_MTU
))
1521 rib
->mtu
= stream_getl (s
);
1526 rib
->vrf_id
= zvrf_id (zvrf
);
1527 rib
->table
= zvrf
->table_id
;
1529 ret
= rib_add_multipath (AFI_IP6
, safi
, &p
, src_pp
, rib
);
1532 client
->v6_route_add_cnt
++;
1534 client
->v6_route_upd8_cnt
++;
1539 /* Zebra server IPv6 prefix delete function. */
1541 zread_ipv6_delete (struct zserv
*client
, u_short length
, struct zebra_vrf
*zvrf
)
1545 struct zapi_ipv6 api
;
1546 struct in6_addr nexthop
;
1547 union g_addr
*pnexthop
= NULL
;
1548 unsigned long ifindex
;
1550 struct prefix_ipv6 src_p
, *src_pp
;
1554 memset (&nexthop
, 0, sizeof (struct in6_addr
));
1556 /* Type, flags, message. */
1557 api
.type
= stream_getc (s
);
1558 api
.instance
= stream_getw (s
);
1559 api
.flags
= stream_getl (s
);
1560 api
.message
= stream_getc (s
);
1561 api
.safi
= stream_getw (s
);
1564 memset (&p
, 0, sizeof (struct prefix_ipv6
));
1565 p
.family
= AF_INET6
;
1566 p
.prefixlen
= stream_getc (s
);
1567 stream_get (&p
.u
.prefix6
, s
, PSIZE (p
.prefixlen
));
1569 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_SRCPFX
))
1571 memset (&src_p
, 0, sizeof (struct prefix_ipv6
));
1572 src_p
.family
= AF_INET6
;
1573 src_p
.prefixlen
= stream_getc (s
);
1574 stream_get (&src_p
.prefix
, s
, PSIZE (src_p
.prefixlen
));
1580 /* Nexthop, ifindex, distance, metric. */
1581 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
))
1583 u_char nexthop_type
;
1585 api
.nexthop_num
= stream_getc (s
);
1586 for (i
= 0; i
< api
.nexthop_num
; i
++)
1588 nexthop_type
= stream_getc (s
);
1590 switch (nexthop_type
)
1592 case NEXTHOP_TYPE_IPV6
:
1593 stream_get (&nexthop
, s
, 16);
1594 pnexthop
= (union g_addr
*)&nexthop
;
1596 case NEXTHOP_TYPE_IFINDEX
:
1597 ifindex
= stream_getl (s
);
1604 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_DISTANCE
))
1605 api
.distance
= stream_getc (s
);
1610 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_METRIC
))
1611 api
.metric
= stream_getl (s
);
1616 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_TAG
))
1617 api
.tag
= stream_getl (s
);
1621 if (IN6_IS_ADDR_UNSPECIFIED (&nexthop
))
1622 rib_delete (AFI_IP6
, api
.safi
, zvrf_id (zvrf
), api
.type
, api
.instance
,
1623 api
.flags
, &p
, src_pp
, NULL
, ifindex
, client
->rtm_table
);
1625 rib_delete (AFI_IP6
, api
.safi
, zvrf_id (zvrf
), api
.type
, api
.instance
,
1626 api
.flags
, &p
, src_pp
, pnexthop
, ifindex
, client
->rtm_table
);
1628 client
->v6_route_del_cnt
++;
1632 /* Register zebra server router-id information. Send current router-id */
1634 zread_router_id_add (struct zserv
*client
, u_short length
, struct zebra_vrf
*zvrf
)
1638 /* Router-id information is needed. */
1639 vrf_bitmap_set (client
->ridinfo
, zvrf_id (zvrf
));
1641 router_id_get (&p
, zvrf_id (zvrf
));
1643 return zsend_router_id_update (client
, &p
, zvrf_id (zvrf
));
1646 /* Unregister zebra server router-id information. */
1648 zread_router_id_delete (struct zserv
*client
, u_short length
, struct zebra_vrf
*zvrf
)
1650 vrf_bitmap_unset (client
->ridinfo
, zvrf_id (zvrf
));
1654 /* Tie up route-type and client->sock */
1656 zread_hello (struct zserv
*client
)
1658 /* type of protocol (lib/zebra.h) */
1662 proto
= stream_getc (client
->ibuf
);
1663 instance
= stream_getw (client
->ibuf
);
1665 /* accept only dynamic routing protocols */
1666 if ((proto
< ZEBRA_ROUTE_MAX
)
1667 && (proto
> ZEBRA_ROUTE_STATIC
))
1669 zlog_notice ("client %d says hello and bids fair to announce only %s routes",
1670 client
->sock
, zebra_route_string(proto
));
1672 zlog_notice ("client protocol instance %d", instance
);
1674 client
->proto
= proto
;
1675 client
->instance
= instance
;
1679 /* Unregister all information in a VRF. */
1681 zread_vrf_unregister (struct zserv
*client
, u_short length
, struct zebra_vrf
*zvrf
)
1686 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
1687 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
1688 vrf_bitmap_unset (client
->redist
[afi
][i
], zvrf_id (zvrf
));
1689 vrf_bitmap_unset (client
->redist_default
, zvrf_id (zvrf
));
1690 vrf_bitmap_unset (client
->ifinfo
, zvrf_id (zvrf
));
1691 vrf_bitmap_unset (client
->ridinfo
, zvrf_id (zvrf
));
1697 zread_mpls_labels (int command
, struct zserv
*client
, u_short length
,
1701 enum lsp_types_t type
;
1702 struct prefix prefix
;
1703 enum nexthop_types_t gtype
;
1706 mpls_label_t in_label
, out_label
;
1708 struct zebra_vrf
*zvrf
;
1710 zvrf
= vrf_info_lookup (vrf_id
);
1714 /* Get input stream. */
1718 type
= stream_getc (s
);
1719 prefix
.family
= stream_getl (s
);
1720 switch (prefix
.family
)
1723 prefix
.u
.prefix4
.s_addr
= stream_get_ipv4 (s
);
1724 prefix
.prefixlen
= stream_getc (s
);
1725 gate
.ipv4
.s_addr
= stream_get_ipv4 (s
);
1728 stream_get (&prefix
.u
.prefix6
, s
, 16);
1729 prefix
.prefixlen
= stream_getc (s
);
1730 stream_get (&gate
.ipv6
, s
, 16);
1735 ifindex
= stream_getl (s
);
1736 distance
= stream_getc (s
);
1737 in_label
= stream_getl (s
);
1738 out_label
= stream_getl (s
);
1740 switch (prefix
.family
)
1744 gtype
= NEXTHOP_TYPE_IPV4_IFINDEX
;
1746 gtype
= NEXTHOP_TYPE_IPV4
;
1750 gtype
= NEXTHOP_TYPE_IPV6_IFINDEX
;
1752 gtype
= NEXTHOP_TYPE_IPV6
;
1761 if (command
== ZEBRA_MPLS_LABELS_ADD
)
1763 mpls_lsp_install (zvrf
, type
, in_label
, out_label
, gtype
, &gate
,
1765 mpls_ftn_update (1, zvrf
, type
, &prefix
, gtype
, &gate
, ifindex
,
1766 distance
, out_label
);
1768 else if (command
== ZEBRA_MPLS_LABELS_DELETE
)
1770 mpls_lsp_uninstall (zvrf
, type
, in_label
, gtype
, &gate
, NULL
, ifindex
);
1771 mpls_ftn_update (0, zvrf
, type
, &prefix
, gtype
, &gate
, ifindex
,
1772 distance
, out_label
);
1775 /* Send response to a label manager connect request to client */
1777 zsend_label_manager_connect_response (struct zserv
*client
, vrf_id_t vrf_id
, u_short result
)
1784 zserv_create_header (s
, ZEBRA_LABEL_MANAGER_CONNECT
, vrf_id
);
1787 stream_putc (s
, result
);
1789 /* Write packet size. */
1790 stream_putw_at (s
, 0, stream_get_endp (s
));
1792 return writen (client
->sock
, s
->data
, stream_get_endp (s
));
1796 zread_label_manager_connect (struct zserv
*client
, vrf_id_t vrf_id
)
1799 /* type of protocol (lib/zebra.h) */
1803 /* Get input stream. */
1807 proto
= stream_getc (s
);
1808 instance
= stream_getw (s
);
1810 /* accept only dynamic routing protocols */
1811 if ((proto
>= ZEBRA_ROUTE_MAX
)
1812 || (proto
<= ZEBRA_ROUTE_STATIC
))
1814 zlog_err ("client %d has wrong protocol %s",
1815 client
->sock
, zebra_route_string(proto
));
1816 zsend_label_manager_connect_response (client
, vrf_id
, 1);
1819 zlog_notice ("client %d with instance %u connected as %s",
1820 client
->sock
, instance
, zebra_route_string(proto
));
1821 client
->proto
= proto
;
1822 client
->instance
= instance
;
1825 Release previous labels of same protocol and instance.
1826 This is done in case it restarted from an unexpected shutdown.
1828 release_daemon_chunks (proto
, instance
);
1830 zlog_debug (" Label Manager client connected: sock %d, proto %s, instance %u",
1831 client
->sock
, zebra_route_string(proto
), instance
);
1832 /* send response back */
1833 zsend_label_manager_connect_response (client
, vrf_id
, 0);
1835 /* Send response to a get label chunk request to client */
1837 zsend_assign_label_chunk_response (struct zserv
*client
, vrf_id_t vrf_id
,
1838 struct label_manager_chunk
*lmc
)
1845 zserv_create_header (s
, ZEBRA_GET_LABEL_CHUNK
, vrf_id
);
1850 stream_putc (s
, lmc
->keep
);
1851 /* start and end labels */
1852 stream_putl (s
, lmc
->start
);
1853 stream_putl (s
, lmc
->end
);
1857 /* Write packet size. */
1858 stream_putw_at (s
, 0, stream_get_endp (s
));
1860 return writen (client
->sock
, s
->data
, stream_get_endp (s
));
1864 zread_get_label_chunk (struct zserv
*client
, vrf_id_t vrf_id
)
1869 struct label_manager_chunk
*lmc
;
1871 /* Get input stream. */
1875 keep
= stream_getc (s
);
1876 size
= stream_getl (s
);
1878 lmc
= assign_label_chunk (client
->proto
, client
->instance
, keep
, size
);
1880 zlog_err ("%s: Unable to assign Label Chunk of size %u", __func__
, size
);
1882 zlog_debug ("Assigned Label Chunk %u - %u to %u",
1883 lmc
->start
, lmc
->end
, keep
);
1884 /* send response back */
1885 zsend_assign_label_chunk_response (client
, vrf_id
, lmc
);
1889 zread_release_label_chunk (struct zserv
*client
)
1892 uint32_t start
, end
;
1894 /* Get input stream. */
1898 start
= stream_getl (s
);
1899 end
= stream_getl (s
);
1901 release_label_chunk (client
->proto
, client
->instance
, start
, end
);
1904 zread_label_manager_request (int cmd
, struct zserv
*client
, vrf_id_t vrf_id
)
1906 /* to avoid sending other messages like ZERBA_INTERFACE_UP */
1907 if (cmd
== ZEBRA_LABEL_MANAGER_CONNECT
)
1908 client
->is_synchronous
= 1;
1910 /* external label manager */
1912 zread_relay_label_manager_request (cmd
, client
, vrf_id
);
1913 /* this is a label manager */
1916 if (cmd
== ZEBRA_LABEL_MANAGER_CONNECT
)
1917 zread_label_manager_connect (client
, vrf_id
);
1920 /* Sanity: don't allow 'unidentified' requests */
1923 zlog_err ("Got label request from an unidentified client");
1926 if (cmd
== ZEBRA_GET_LABEL_CHUNK
)
1927 zread_get_label_chunk (client
, vrf_id
);
1928 else if (cmd
== ZEBRA_RELEASE_LABEL_CHUNK
)
1929 zread_release_label_chunk (client
);
1934 /* Cleanup registered nexthops (across VRFs) upon client disconnect. */
1936 zebra_client_close_cleanup_rnh (struct zserv
*client
)
1939 struct zebra_vrf
*zvrf
;
1941 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
)
1943 if ((zvrf
= vrf
->info
) != NULL
)
1945 zebra_cleanup_rnh_client(zvrf_id (zvrf
), AF_INET
, client
, RNH_NEXTHOP_TYPE
);
1946 zebra_cleanup_rnh_client(zvrf_id (zvrf
), AF_INET6
, client
, RNH_NEXTHOP_TYPE
);
1947 zebra_cleanup_rnh_client(zvrf_id (zvrf
), AF_INET
, client
, RNH_IMPORT_CHECK_TYPE
);
1948 zebra_cleanup_rnh_client(zvrf_id (zvrf
), AF_INET6
, client
, RNH_IMPORT_CHECK_TYPE
);
1949 if (client
->proto
== ZEBRA_ROUTE_LDP
)
1951 hash_iterate(zvrf
->lsp_table
, mpls_ldp_lsp_uninstall_all
,
1953 mpls_ldp_ftn_uninstall_all (zvrf
, AFI_IP
);
1954 mpls_ldp_ftn_uninstall_all (zvrf
, AFI_IP6
);
1960 /* Close zebra client. */
1962 zebra_client_close (struct zserv
*client
)
1964 /* Send client de-registration to BFD */
1965 zebra_ptm_bfd_client_deregister(client
->proto
);
1967 /* Cleanup any registered nexthops - across all VRFs. */
1968 zebra_client_close_cleanup_rnh (client
);
1970 /* Release Label Manager chunks */
1971 release_daemon_chunks (client
->proto
, client
->instance
);
1973 /* Close file descriptor. */
1976 unsigned long nroutes
;
1978 close (client
->sock
);
1979 nroutes
= rib_score_proto (client
->proto
, client
->instance
);
1980 zlog_notice ("client %d disconnected. %lu %s routes removed from the rib",
1981 client
->sock
, nroutes
, zebra_route_string (client
->proto
));
1985 /* Free stream buffers. */
1987 stream_free (client
->ibuf
);
1989 stream_free (client
->obuf
);
1991 buffer_free(client
->wb
);
1993 /* Release threads. */
1995 thread_cancel (client
->t_read
);
1996 if (client
->t_write
)
1997 thread_cancel (client
->t_write
);
1998 if (client
->t_suicide
)
1999 thread_cancel (client
->t_suicide
);
2002 for (afi_t afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
2003 for (int i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
2004 vrf_bitmap_free (client
->redist
[afi
][i
]);
2006 vrf_bitmap_free (client
->redist_default
);
2007 vrf_bitmap_free (client
->ifinfo
);
2008 vrf_bitmap_free (client
->ridinfo
);
2010 /* Free client structure. */
2011 listnode_delete (zebrad
.client_list
, client
);
2012 XFREE (MTYPE_TMP
, client
);
2015 /* Make new client. */
2017 zebra_client_create (int sock
)
2019 struct zserv
*client
;
2023 client
= XCALLOC (MTYPE_TMP
, sizeof (struct zserv
));
2025 /* Make client input/output buffer. */
2026 client
->sock
= sock
;
2027 client
->ibuf
= stream_new (ZEBRA_MAX_PACKET_SIZ
);
2028 client
->obuf
= stream_new (ZEBRA_MAX_PACKET_SIZ
);
2029 client
->wb
= buffer_new(0);
2031 /* Set table number. */
2032 client
->rtm_table
= zebrad
.rtm_table_default
;
2034 client
->connect_time
= monotime(NULL
);
2035 /* Initialize flags */
2036 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
2037 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
2038 client
->redist
[afi
][i
] = vrf_bitmap_init ();
2039 client
->redist_default
= vrf_bitmap_init ();
2040 client
->ifinfo
= vrf_bitmap_init ();
2041 client
->ridinfo
= vrf_bitmap_init ();
2043 /* by default, it's not a synchronous client */
2044 client
->is_synchronous
= 0;
2046 /* Add this client to linked list. */
2047 listnode_add (zebrad
.client_list
, client
);
2049 /* Make new read thread. */
2050 zebra_event (ZEBRA_READ
, sock
, client
);
2052 zebra_vrf_update_all (client
);
2055 /* Handler of zebra service request. */
2057 zebra_client_read (struct thread
*thread
)
2060 struct zserv
*client
;
2062 uint16_t length
, command
;
2063 uint8_t marker
, version
;
2065 struct zebra_vrf
*zvrf
;
2067 /* Get thread data. Reset reading thread because I'm running. */
2068 sock
= THREAD_FD (thread
);
2069 client
= THREAD_ARG (thread
);
2070 client
->t_read
= NULL
;
2072 if (client
->t_suicide
)
2074 zebra_client_close(client
);
2078 /* Read length and command (if we don't have it already). */
2079 if ((already
= stream_get_endp(client
->ibuf
)) < ZEBRA_HEADER_SIZE
)
2082 if (((nbyte
= stream_read_try (client
->ibuf
, sock
,
2083 ZEBRA_HEADER_SIZE
-already
)) == 0) ||
2086 if (IS_ZEBRA_DEBUG_EVENT
)
2087 zlog_debug ("connection closed socket [%d]", sock
);
2088 zebra_client_close (client
);
2091 if (nbyte
!= (ssize_t
)(ZEBRA_HEADER_SIZE
-already
))
2093 /* Try again later. */
2094 zebra_event (ZEBRA_READ
, sock
, client
);
2097 already
= ZEBRA_HEADER_SIZE
;
2100 /* Reset to read from the beginning of the incoming packet. */
2101 stream_set_getp(client
->ibuf
, 0);
2103 /* Fetch header values */
2104 length
= stream_getw (client
->ibuf
);
2105 marker
= stream_getc (client
->ibuf
);
2106 version
= stream_getc (client
->ibuf
);
2107 vrf_id
= stream_getw (client
->ibuf
);
2108 command
= stream_getw (client
->ibuf
);
2110 if (marker
!= ZEBRA_HEADER_MARKER
|| version
!= ZSERV_VERSION
)
2112 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
2113 __func__
, sock
, marker
, version
);
2114 zebra_client_close (client
);
2117 if (length
< ZEBRA_HEADER_SIZE
)
2119 zlog_warn("%s: socket %d message length %u is less than header size %d",
2120 __func__
, sock
, length
, ZEBRA_HEADER_SIZE
);
2121 zebra_client_close (client
);
2124 if (length
> STREAM_SIZE(client
->ibuf
))
2126 zlog_warn("%s: socket %d message length %u exceeds buffer size %lu",
2127 __func__
, sock
, length
, (u_long
)STREAM_SIZE(client
->ibuf
));
2128 zebra_client_close (client
);
2132 /* Read rest of data. */
2133 if (already
< length
)
2136 if (((nbyte
= stream_read_try (client
->ibuf
, sock
,
2137 length
-already
)) == 0) ||
2140 if (IS_ZEBRA_DEBUG_EVENT
)
2141 zlog_debug ("connection closed [%d] when reading zebra data", sock
);
2142 zebra_client_close (client
);
2145 if (nbyte
!= (ssize_t
)(length
-already
))
2147 /* Try again later. */
2148 zebra_event (ZEBRA_READ
, sock
, client
);
2153 length
-= ZEBRA_HEADER_SIZE
;
2155 /* Debug packet information. */
2156 if (IS_ZEBRA_DEBUG_EVENT
)
2157 zlog_debug ("zebra message comes from socket [%d]", sock
);
2159 if (IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
)
2160 zlog_debug ("zebra message received [%s] %d in VRF %u",
2161 zserv_command_string (command
), length
, vrf_id
);
2163 client
->last_read_time
= monotime(NULL
);
2164 client
->last_read_cmd
= command
;
2166 zvrf
= zebra_vrf_lookup_by_id (vrf_id
);
2169 if (IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
)
2170 zlog_debug ("zebra received unknown VRF[%u]", vrf_id
);
2171 goto zclient_read_out
;
2176 case ZEBRA_ROUTER_ID_ADD
:
2177 zread_router_id_add (client
, length
, zvrf
);
2179 case ZEBRA_ROUTER_ID_DELETE
:
2180 zread_router_id_delete (client
, length
, zvrf
);
2182 case ZEBRA_INTERFACE_ADD
:
2183 zread_interface_add (client
, length
, zvrf
);
2185 case ZEBRA_INTERFACE_DELETE
:
2186 zread_interface_delete (client
, length
, zvrf
);
2188 case ZEBRA_IPV4_ROUTE_ADD
:
2189 zread_ipv4_add (client
, length
, zvrf
);
2191 case ZEBRA_IPV4_ROUTE_DELETE
:
2192 zread_ipv4_delete (client
, length
, zvrf
);
2194 case ZEBRA_IPV4_ROUTE_IPV6_NEXTHOP_ADD
:
2195 zread_ipv4_route_ipv6_nexthop_add (client
, length
, zvrf
);
2197 case ZEBRA_IPV4_NEXTHOP_ADD
:
2198 zread_ipv4_add(client
, length
, zvrf
); /* LB: r1.0 merge - id was 1 */
2200 case ZEBRA_IPV4_NEXTHOP_DELETE
:
2201 zread_ipv4_delete(client
, length
, zvrf
); /* LB: r1.0 merge - id was 1 */
2203 case ZEBRA_IPV6_ROUTE_ADD
:
2204 zread_ipv6_add (client
, length
, zvrf
);
2206 case ZEBRA_IPV6_ROUTE_DELETE
:
2207 zread_ipv6_delete (client
, length
, zvrf
);
2209 case ZEBRA_REDISTRIBUTE_ADD
:
2210 zebra_redistribute_add (command
, client
, length
, zvrf
);
2212 case ZEBRA_REDISTRIBUTE_DELETE
:
2213 zebra_redistribute_delete (command
, client
, length
, zvrf
);
2215 case ZEBRA_REDISTRIBUTE_DEFAULT_ADD
:
2216 zebra_redistribute_default_add (command
, client
, length
, zvrf
);
2218 case ZEBRA_REDISTRIBUTE_DEFAULT_DELETE
:
2219 zebra_redistribute_default_delete (command
, client
, length
, zvrf
);
2221 case ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB
:
2222 zread_ipv4_nexthop_lookup_mrib (client
, length
, zvrf
);
2225 zread_hello (client
);
2227 case ZEBRA_NEXTHOP_REGISTER
:
2228 zserv_rnh_register(client
, sock
, length
, RNH_NEXTHOP_TYPE
, zvrf
);
2230 case ZEBRA_NEXTHOP_UNREGISTER
:
2231 zserv_rnh_unregister(client
, sock
, length
, RNH_NEXTHOP_TYPE
, zvrf
);
2233 case ZEBRA_IMPORT_ROUTE_REGISTER
:
2234 zserv_rnh_register(client
, sock
, length
, RNH_IMPORT_CHECK_TYPE
, zvrf
);
2236 case ZEBRA_IMPORT_ROUTE_UNREGISTER
:
2237 zserv_rnh_unregister(client
, sock
, length
, RNH_IMPORT_CHECK_TYPE
, zvrf
);
2239 case ZEBRA_BFD_DEST_UPDATE
:
2240 case ZEBRA_BFD_DEST_REGISTER
:
2241 zebra_ptm_bfd_dst_register(client
, sock
, length
, command
, zvrf
);
2243 case ZEBRA_BFD_DEST_DEREGISTER
:
2244 zebra_ptm_bfd_dst_deregister(client
, sock
, length
, zvrf
);
2246 case ZEBRA_VRF_UNREGISTER
:
2247 zread_vrf_unregister (client
, length
, zvrf
);
2249 case ZEBRA_BFD_CLIENT_REGISTER
:
2250 zebra_ptm_bfd_client_register(client
, sock
, length
);
2252 case ZEBRA_INTERFACE_ENABLE_RADV
:
2253 zebra_interface_radv_set (client
, sock
, length
, zvrf
, 1);
2255 case ZEBRA_INTERFACE_DISABLE_RADV
:
2256 zebra_interface_radv_set (client
, sock
, length
, zvrf
, 0);
2258 case ZEBRA_MPLS_LABELS_ADD
:
2259 case ZEBRA_MPLS_LABELS_DELETE
:
2260 zread_mpls_labels (command
, client
, length
, vrf_id
);
2262 case ZEBRA_IPMR_ROUTE_STATS
:
2263 zebra_ipmr_route_stats (client
, sock
, length
, zvrf
);
2265 case ZEBRA_LABEL_MANAGER_CONNECT
:
2266 case ZEBRA_GET_LABEL_CHUNK
:
2267 case ZEBRA_RELEASE_LABEL_CHUNK
:
2268 zread_label_manager_request (command
, client
, vrf_id
);
2271 zlog_info ("Zebra received unknown command %d", command
);
2275 if (client
->t_suicide
)
2277 /* No need to wait for thread callback, just kill immediately. */
2278 zebra_client_close(client
);
2283 stream_reset (client
->ibuf
);
2284 zebra_event (ZEBRA_READ
, sock
, client
);
2289 /* Accept code of zebra server socket. */
2291 zebra_accept (struct thread
*thread
)
2295 struct sockaddr_in client
;
2298 accept_sock
= THREAD_FD (thread
);
2300 /* Reregister myself. */
2301 zebra_event (ZEBRA_SERV
, accept_sock
, NULL
);
2303 len
= sizeof (struct sockaddr_in
);
2304 client_sock
= accept (accept_sock
, (struct sockaddr
*) &client
, &len
);
2306 if (client_sock
< 0)
2308 zlog_warn ("Can't accept zebra socket: %s", safe_strerror (errno
));
2312 /* Make client socket non-blocking. */
2313 set_nonblocking(client_sock
);
2315 /* Create new zebra client. */
2316 zebra_client_create (client_sock
);
2321 #ifdef HAVE_TCP_ZEBRA
2322 /* Make zebra's server socket. */
2328 struct sockaddr_in addr
;
2330 accept_sock
= socket (AF_INET
, SOCK_STREAM
, 0);
2332 if (accept_sock
< 0)
2334 zlog_warn ("Can't create zserv stream socket: %s",
2335 safe_strerror (errno
));
2336 zlog_warn ("zebra can't provice full functionality due to above error");
2340 memset (&addr
, 0, sizeof (struct sockaddr_in
));
2341 addr
.sin_family
= AF_INET
;
2342 addr
.sin_port
= htons (ZEBRA_PORT
);
2343 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
2344 addr
.sin_len
= sizeof (struct sockaddr_in
);
2345 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
2346 addr
.sin_addr
.s_addr
= htonl (INADDR_LOOPBACK
);
2348 sockopt_reuseaddr (accept_sock
);
2349 sockopt_reuseport (accept_sock
);
2351 if ( zserv_privs
.change(ZPRIVS_RAISE
) )
2352 zlog_err("Can't raise privileges");
2354 ret
= bind (accept_sock
, (struct sockaddr
*)&addr
,
2355 sizeof (struct sockaddr_in
));
2358 zlog_warn ("Can't bind to stream socket: %s",
2359 safe_strerror (errno
));
2360 zlog_warn ("zebra can't provice full functionality due to above error");
2361 close (accept_sock
); /* Avoid sd leak. */
2365 if ( zserv_privs
.change(ZPRIVS_LOWER
) )
2366 zlog_err("Can't lower privileges");
2368 ret
= listen (accept_sock
, 1);
2371 zlog_warn ("Can't listen to stream socket: %s",
2372 safe_strerror (errno
));
2373 zlog_warn ("zebra can't provice full functionality due to above error");
2374 close (accept_sock
); /* Avoid sd leak. */
2378 zebra_event (ZEBRA_SERV
, accept_sock
, NULL
);
2380 #else /* HAVE_TCP_ZEBRA */
2382 /* For sockaddr_un. */
2385 /* zebra server UNIX domain socket. */
2387 zebra_serv_un (const char *path
)
2391 struct sockaddr_un serv
;
2394 /* First of all, unlink existing socket */
2398 old_mask
= umask (0077);
2400 /* Make UNIX domain socket. */
2401 sock
= socket (AF_UNIX
, SOCK_STREAM
, 0);
2404 zlog_warn ("Can't create zserv unix socket: %s",
2405 safe_strerror (errno
));
2406 zlog_warn ("zebra can't provide full functionality due to above error");
2410 /* Make server socket. */
2411 memset (&serv
, 0, sizeof (struct sockaddr_un
));
2412 serv
.sun_family
= AF_UNIX
;
2413 strncpy (serv
.sun_path
, path
, strlen (path
));
2414 #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
2415 len
= serv
.sun_len
= SUN_LEN(&serv
);
2417 len
= sizeof (serv
.sun_family
) + strlen (serv
.sun_path
);
2418 #endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
2420 ret
= bind (sock
, (struct sockaddr
*) &serv
, len
);
2423 zlog_warn ("Can't bind to unix socket %s: %s",
2424 path
, safe_strerror (errno
));
2425 zlog_warn ("zebra can't provide full functionality due to above error");
2430 ret
= listen (sock
, 5);
2433 zlog_warn ("Can't listen to unix socket %s: %s",
2434 path
, safe_strerror (errno
));
2435 zlog_warn ("zebra can't provide full functionality due to above error");
2442 zebra_event (ZEBRA_SERV
, sock
, NULL
);
2444 #endif /* HAVE_TCP_ZEBRA */
2448 zebra_event (enum event event
, int sock
, struct zserv
*client
)
2453 thread_add_read (zebrad
.master
, zebra_accept
, client
, sock
);
2457 thread_add_read (zebrad
.master
, zebra_client_read
, client
, sock
);
2465 #define ZEBRA_TIME_BUF 32
2467 zserv_time_buf(time_t *time1
, char *buf
, int buflen
)
2472 assert (buf
!= NULL
);
2473 assert (buflen
>= ZEBRA_TIME_BUF
);
2474 assert (time1
!= NULL
);
2478 snprintf(buf
, buflen
, "never ");
2482 now
= monotime(NULL
);
2486 /* Making formatted timer strings. */
2487 #define ONE_DAY_SECOND 60*60*24
2488 #define ONE_WEEK_SECOND 60*60*24*7
2490 if (now
< ONE_DAY_SECOND
)
2491 snprintf (buf
, buflen
, "%02d:%02d:%02d",
2492 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
);
2493 else if (now
< ONE_WEEK_SECOND
)
2494 snprintf (buf
, buflen
, "%dd%02dh%02dm",
2495 tm
->tm_yday
, tm
->tm_hour
, tm
->tm_min
);
2497 snprintf (buf
, buflen
, "%02dw%dd%02dh",
2498 tm
->tm_yday
/7, tm
->tm_yday
- ((tm
->tm_yday
/7) * 7), tm
->tm_hour
);
2503 zebra_show_client_detail (struct vty
*vty
, struct zserv
*client
)
2505 char cbuf
[ZEBRA_TIME_BUF
], rbuf
[ZEBRA_TIME_BUF
];
2506 char wbuf
[ZEBRA_TIME_BUF
], nhbuf
[ZEBRA_TIME_BUF
], mbuf
[ZEBRA_TIME_BUF
];
2508 vty_out (vty
, "Client: %s", zebra_route_string(client
->proto
));
2509 if (client
->instance
)
2510 vty_out (vty
, " Instance: %d", client
->instance
);
2511 vty_out (vty
, "%s", VTY_NEWLINE
);
2513 vty_out (vty
, "------------------------ %s", VTY_NEWLINE
);
2514 vty_out (vty
, "FD: %d %s", client
->sock
, VTY_NEWLINE
);
2515 vty_out (vty
, "Route Table ID: %d %s", client
->rtm_table
, VTY_NEWLINE
);
2517 vty_out (vty
, "Connect Time: %s %s",
2518 zserv_time_buf(&client
->connect_time
, cbuf
, ZEBRA_TIME_BUF
),
2520 if (client
->nh_reg_time
)
2522 vty_out (vty
, "Nexthop Registry Time: %s %s",
2523 zserv_time_buf(&client
->nh_reg_time
, nhbuf
, ZEBRA_TIME_BUF
),
2525 if (client
->nh_last_upd_time
)
2526 vty_out (vty
, "Nexthop Last Update Time: %s %s",
2527 zserv_time_buf(&client
->nh_last_upd_time
, mbuf
, ZEBRA_TIME_BUF
),
2530 vty_out (vty
, "No Nexthop Update sent%s", VTY_NEWLINE
);
2533 vty_out (vty
, "Not registered for Nexthop Updates%s", VTY_NEWLINE
);
2535 vty_out (vty
, "Last Msg Rx Time: %s %s",
2536 zserv_time_buf(&client
->last_read_time
, rbuf
, ZEBRA_TIME_BUF
),
2538 vty_out (vty
, "Last Msg Tx Time: %s %s",
2539 zserv_time_buf(&client
->last_write_time
, wbuf
, ZEBRA_TIME_BUF
),
2541 if (client
->last_read_time
)
2542 vty_out (vty
, "Last Rcvd Cmd: %s %s",
2543 zserv_command_string(client
->last_read_cmd
), VTY_NEWLINE
);
2544 if (client
->last_write_time
)
2545 vty_out (vty
, "Last Sent Cmd: %s %s",
2546 zserv_command_string(client
->last_write_cmd
), VTY_NEWLINE
);
2547 vty_out (vty
, "%s", VTY_NEWLINE
);
2549 vty_out (vty
, "Type Add Update Del %s", VTY_NEWLINE
);
2550 vty_out (vty
, "================================================== %s", VTY_NEWLINE
);
2551 vty_out (vty
, "IPv4 %-12d%-12d%-12d%s", client
->v4_route_add_cnt
,
2552 client
->v4_route_upd8_cnt
, client
->v4_route_del_cnt
, VTY_NEWLINE
);
2553 vty_out (vty
, "IPv6 %-12d%-12d%-12d%s", client
->v6_route_add_cnt
,
2554 client
->v6_route_upd8_cnt
, client
->v6_route_del_cnt
, VTY_NEWLINE
);
2555 vty_out (vty
, "Redist:v4 %-12d%-12d%-12d%s", client
->redist_v4_add_cnt
, 0,
2556 client
->redist_v4_del_cnt
, VTY_NEWLINE
);
2557 vty_out (vty
, "Redist:v6 %-12d%-12d%-12d%s", client
->redist_v6_add_cnt
, 0,
2558 client
->redist_v6_del_cnt
, VTY_NEWLINE
);
2559 vty_out (vty
, "Connected %-12d%-12d%-12d%s", client
->ifadd_cnt
, 0,
2560 client
->ifdel_cnt
, VTY_NEWLINE
);
2561 vty_out (vty
, "BFD peer %-12d%-12d%-12d%s", client
->bfd_peer_add_cnt
,
2562 client
->bfd_peer_upd8_cnt
, client
->bfd_peer_del_cnt
, VTY_NEWLINE
);
2563 vty_out (vty
, "Interface Up Notifications: %d%s", client
->ifup_cnt
,
2565 vty_out (vty
, "Interface Down Notifications: %d%s", client
->ifdown_cnt
,
2568 vty_out (vty
, "%s", VTY_NEWLINE
);
2573 zebra_show_client_brief (struct vty
*vty
, struct zserv
*client
)
2575 char cbuf
[ZEBRA_TIME_BUF
], rbuf
[ZEBRA_TIME_BUF
];
2576 char wbuf
[ZEBRA_TIME_BUF
];
2578 vty_out (vty
, "%-8s%12s %12s%12s%8d/%-8d%8d/%-8d%s",
2579 zebra_route_string(client
->proto
),
2580 zserv_time_buf(&client
->connect_time
, cbuf
, ZEBRA_TIME_BUF
),
2581 zserv_time_buf(&client
->last_read_time
, rbuf
, ZEBRA_TIME_BUF
),
2582 zserv_time_buf(&client
->last_write_time
, wbuf
, ZEBRA_TIME_BUF
),
2583 client
->v4_route_add_cnt
+client
->v4_route_upd8_cnt
,
2584 client
->v4_route_del_cnt
,
2585 client
->v6_route_add_cnt
+client
->v6_route_upd8_cnt
,
2586 client
->v6_route_del_cnt
, VTY_NEWLINE
);
2591 zebra_find_client (u_char proto
)
2593 struct listnode
*node
, *nnode
;
2594 struct zserv
*client
;
2596 for (ALL_LIST_ELEMENTS (zebrad
.client_list
, node
, nnode
, client
))
2598 if (client
->proto
== proto
)
2606 /* Display default rtm_table for all clients. */
2611 "default routing table to use for all clients\n")
2613 vty_out (vty
, "table %d%s", zebrad
.rtm_table_default
,
2618 DEFUN (config_table
,
2621 "Configure target kernel routing table\n"
2624 zebrad
.rtm_table_default
= strtol (argv
[1]->arg
, (char**)0, 10);
2628 DEFUN (no_config_table
,
2629 no_config_table_cmd
,
2630 "no table [TABLENO]",
2632 "Configure target kernel routing table\n"
2635 zebrad
.rtm_table_default
= 0;
2640 DEFUN (ip_forwarding
,
2644 "Turn on IP forwarding")
2650 ret
= ipforward_on ();
2654 vty_out (vty
, "Can't turn on IP forwarding%s", VTY_NEWLINE
);
2661 DEFUN (no_ip_forwarding
,
2662 no_ip_forwarding_cmd
,
2666 "Turn off IP forwarding")
2672 ret
= ipforward_off ();
2676 vty_out (vty
, "Can't turn off IP forwarding%s", VTY_NEWLINE
);
2683 /* This command is for debugging purpose. */
2684 DEFUN (show_zebra_client
,
2685 show_zebra_client_cmd
,
2686 "show zebra client",
2688 "Zebra information\n"
2689 "Client information\n")
2691 struct listnode
*node
;
2692 struct zserv
*client
;
2694 for (ALL_LIST_ELEMENTS_RO (zebrad
.client_list
, node
, client
))
2695 zebra_show_client_detail(vty
, client
);
2700 /* This command is for debugging purpose. */
2701 DEFUN (show_zebra_client_summary
,
2702 show_zebra_client_summary_cmd
,
2703 "show zebra client summary",
2705 "Zebra information brief\n"
2706 "Client information brief\n"
2709 struct listnode
*node
;
2710 struct zserv
*client
;
2712 vty_out (vty
, "Name Connect Time Last Read Last Write IPv4 Routes IPv6 Routes %s",
2714 vty_out (vty
,"--------------------------------------------------------------------------------%s",
2717 for (ALL_LIST_ELEMENTS_RO (zebrad
.client_list
, node
, client
))
2718 zebra_show_client_brief(vty
, client
);
2720 vty_out (vty
, "Routes column shows (added+updated)/deleted%s", VTY_NEWLINE
);
2724 /* Table configuration write function. */
2726 config_write_table (struct vty
*vty
)
2728 if (zebrad
.rtm_table_default
)
2729 vty_out (vty
, "table %d%s", zebrad
.rtm_table_default
,
2734 /* table node for routing tables. */
2735 static struct cmd_node table_node
=
2738 "", /* This node has no interface. */
2742 /* Only display ip forwarding is enabled or not. */
2743 DEFUN (show_ip_forwarding
,
2744 show_ip_forwarding_cmd
,
2745 "show ip forwarding",
2748 "IP forwarding status\n")
2755 vty_out (vty
, "IP forwarding is off%s", VTY_NEWLINE
);
2757 vty_out (vty
, "IP forwarding is on%s", VTY_NEWLINE
);
2761 /* Only display ipv6 forwarding is enabled or not. */
2762 DEFUN (show_ipv6_forwarding
,
2763 show_ipv6_forwarding_cmd
,
2764 "show ipv6 forwarding",
2766 "IPv6 information\n"
2767 "Forwarding status\n")
2771 ret
= ipforward_ipv6 ();
2776 vty_out (vty
, "ipv6 forwarding is unknown%s", VTY_NEWLINE
);
2779 vty_out (vty
, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE
);
2782 vty_out (vty
, "ipv6 forwarding is %s%s", "on", VTY_NEWLINE
);
2785 vty_out (vty
, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE
);
2791 DEFUN (ipv6_forwarding
,
2792 ipv6_forwarding_cmd
,
2795 "Turn on IPv6 forwarding")
2799 ret
= ipforward_ipv6 ();
2801 ret
= ipforward_ipv6_on ();
2805 vty_out (vty
, "Can't turn on IPv6 forwarding%s", VTY_NEWLINE
);
2812 DEFUN (no_ipv6_forwarding
,
2813 no_ipv6_forwarding_cmd
,
2814 "no ipv6 forwarding",
2817 "Turn off IPv6 forwarding")
2821 ret
= ipforward_ipv6 ();
2823 ret
= ipforward_ipv6_off ();
2827 vty_out (vty
, "Can't turn off IPv6 forwarding%s", VTY_NEWLINE
);
2834 /* IPForwarding configuration write function. */
2836 config_write_forwarding (struct vty
*vty
)
2838 /* FIXME: Find better place for that. */
2839 router_id_write (vty
);
2842 vty_out (vty
, "no ip forwarding%s", VTY_NEWLINE
);
2843 if (!ipforward_ipv6 ())
2844 vty_out (vty
, "no ipv6 forwarding%s", VTY_NEWLINE
);
2845 vty_out (vty
, "!%s", VTY_NEWLINE
);
2849 /* table node for routing tables. */
2850 static struct cmd_node forwarding_node
=
2853 "", /* This node has no interface. */
2857 /* Initialisation of zebra and installation of commands. */
2861 /* Client list init. */
2862 zebrad
.client_list
= list_new ();
2864 /* Install configuration write function. */
2865 install_node (&table_node
, config_write_table
);
2866 install_node (&forwarding_node
, config_write_forwarding
);
2868 install_element (VIEW_NODE
, &show_ip_forwarding_cmd
);
2869 install_element (CONFIG_NODE
, &ip_forwarding_cmd
);
2870 install_element (CONFIG_NODE
, &no_ip_forwarding_cmd
);
2871 install_element (ENABLE_NODE
, &show_zebra_client_cmd
);
2872 install_element (ENABLE_NODE
, &show_zebra_client_summary_cmd
);
2875 install_element (VIEW_NODE
, &show_table_cmd
);
2876 install_element (CONFIG_NODE
, &config_table_cmd
);
2877 install_element (CONFIG_NODE
, &no_config_table_cmd
);
2878 #endif /* HAVE_NETLINK */
2880 install_element (VIEW_NODE
, &show_ipv6_forwarding_cmd
);
2881 install_element (CONFIG_NODE
, &ipv6_forwarding_cmd
);
2882 install_element (CONFIG_NODE
, &no_ipv6_forwarding_cmd
);
2885 zebra_route_map_init ();
2888 /* Make zebra server socket, wiping any existing one (see bug #403). */
2890 zebra_zserv_socket_init (char *path
)
2892 #ifdef HAVE_TCP_ZEBRA
2895 zebra_serv_un (path
? path
: ZEBRA_SERV_PATH
);
2896 #endif /* HAVE_TCP_ZEBRA */