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_fpm.h"
58 /* Event list of zebra. */
59 enum event
{ ZEBRA_SERV
, ZEBRA_READ
, ZEBRA_WRITE
};
61 static void zebra_event (enum event event
, int sock
, struct zserv
*client
);
63 extern struct zebra_privs_t zserv_privs
;
65 static void zebra_client_close (struct zserv
*client
);
68 zserv_delayed_close(struct thread
*thread
)
70 struct zserv
*client
= THREAD_ARG(thread
);
72 client
->t_suicide
= NULL
;
73 zebra_client_close(client
);
78 zserv_flush_data(struct thread
*thread
)
80 struct zserv
*client
= THREAD_ARG(thread
);
82 client
->t_write
= NULL
;
83 if (client
->t_suicide
)
85 zebra_client_close(client
);
88 switch (buffer_flush_available(client
->wb
, client
->sock
))
91 zlog_warn("%s: buffer_flush_available failed on zserv client fd %d, "
92 "closing", __func__
, client
->sock
);
93 zebra_client_close(client
);
96 client
->t_write
= thread_add_write(zebrad
.master
, zserv_flush_data
,
97 client
, client
->sock
);
103 client
->last_write_time
= quagga_monotime();
108 zebra_server_send_message(struct zserv
*client
)
110 if (client
->t_suicide
)
113 stream_set_getp(client
->obuf
, 0);
114 client
->last_write_cmd
= stream_getw_from(client
->obuf
, 6);
115 switch (buffer_write(client
->wb
, client
->sock
, STREAM_DATA(client
->obuf
),
116 stream_get_endp(client
->obuf
)))
119 zlog_warn("%s: buffer_write failed to zserv client fd %d, closing",
120 __func__
, client
->sock
);
121 /* Schedule a delayed close since many of the functions that call this
122 one do not check the return code. They do not allow for the
123 possibility that an I/O error may have caused the client to be
125 client
->t_suicide
= thread_add_event(zebrad
.master
, zserv_delayed_close
,
129 THREAD_OFF(client
->t_write
);
132 THREAD_WRITE_ON(zebrad
.master
, client
->t_write
,
133 zserv_flush_data
, client
, client
->sock
);
137 client
->last_write_time
= quagga_monotime();
142 zserv_create_header (struct stream
*s
, uint16_t cmd
, vrf_id_t vrf_id
)
144 /* length placeholder, caller can update */
145 stream_putw (s
, ZEBRA_HEADER_SIZE
);
146 stream_putc (s
, ZEBRA_HEADER_MARKER
);
147 stream_putc (s
, ZSERV_VERSION
);
148 stream_putw (s
, vrf_id
);
149 stream_putw (s
, cmd
);
153 zserv_encode_interface (struct stream
*s
, struct interface
*ifp
)
155 /* Interface information. */
156 stream_put (s
, ifp
->name
, INTERFACE_NAMSIZ
);
157 stream_putl (s
, ifp
->ifindex
);
158 stream_putc (s
, ifp
->status
);
159 stream_putq (s
, ifp
->flags
);
160 stream_putc (s
, ifp
->ptm_enable
);
161 stream_putc (s
, ifp
->ptm_status
);
162 stream_putl (s
, ifp
->metric
);
163 stream_putl (s
, ifp
->mtu
);
164 stream_putl (s
, ifp
->mtu6
);
165 stream_putl (s
, ifp
->bandwidth
);
166 stream_putl (s
, ifp
->ll_type
);
167 stream_putl (s
, ifp
->hw_addr_len
);
168 if (ifp
->hw_addr_len
)
169 stream_put (s
, ifp
->hw_addr
, ifp
->hw_addr_len
);
171 /* Then, Traffic Engineering parameters if any */
172 if (HAS_LINK_PARAMS(ifp
) && IS_LINK_PARAMS_SET(ifp
->link_params
))
175 zebra_interface_link_params_write (s
, ifp
);
180 /* Write packet size. */
181 stream_putw_at (s
, 0, stream_get_endp (s
));
185 zserv_encode_vrf (struct stream
*s
, struct zebra_vrf
*zvrf
)
187 /* Interface information. */
188 stream_put (s
, zvrf_name (zvrf
), VRF_NAMSIZ
);
190 /* Write packet size. */
191 stream_putw_at (s
, 0, stream_get_endp (s
));
194 /* Interface is added. Send ZEBRA_INTERFACE_ADD to client. */
196 * This function is called in the following situations:
197 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
199 * - at startup, when zebra figures out the available interfaces
200 * - when an interface is added (where support for
201 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
202 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
206 zsend_interface_add (struct zserv
*client
, struct interface
*ifp
)
213 zserv_create_header (s
, ZEBRA_INTERFACE_ADD
, ifp
->vrf_id
);
214 zserv_encode_interface (s
, ifp
);
217 return zebra_server_send_message(client
);
220 /* Interface deletion from zebra daemon. */
222 zsend_interface_delete (struct zserv
*client
, struct interface
*ifp
)
229 zserv_create_header (s
, ZEBRA_INTERFACE_DELETE
, ifp
->vrf_id
);
230 zserv_encode_interface (s
, ifp
);
233 return zebra_server_send_message (client
);
237 zsend_vrf_add (struct zserv
*client
, struct zebra_vrf
*zvrf
)
244 zserv_create_header (s
, ZEBRA_VRF_ADD
, zvrf_id (zvrf
));
245 zserv_encode_vrf (s
, zvrf
);
247 client
->vrfadd_cnt
++;
248 return zebra_server_send_message(client
);
251 /* VRF deletion from zebra daemon. */
253 zsend_vrf_delete (struct zserv
*client
, struct zebra_vrf
*zvrf
)
260 zserv_create_header (s
, ZEBRA_VRF_DELETE
, zvrf_id (zvrf
));
261 zserv_encode_vrf (s
, zvrf
);
263 client
->vrfdel_cnt
++;
264 return zebra_server_send_message (client
);
268 zsend_interface_link_params (struct zserv
*client
, struct interface
*ifp
)
272 /* Check this client need interface information. */
273 if (! client
->ifinfo
)
276 if (!ifp
->link_params
)
281 zserv_create_header (s
, ZEBRA_INTERFACE_LINK_PARAMS
, ifp
->vrf_id
);
283 /* Add Interface Index */
284 stream_putl (s
, ifp
->ifindex
);
286 /* Then TE Link Parameters */
287 if (zebra_interface_link_params_write (s
, ifp
) == 0)
290 /* Write packet size. */
291 stream_putw_at (s
, 0, stream_get_endp (s
));
293 return zebra_server_send_message (client
);
296 /* Interface address is added/deleted. Send ZEBRA_INTERFACE_ADDRESS_ADD or
297 * ZEBRA_INTERFACE_ADDRESS_DELETE to the client.
299 * A ZEBRA_INTERFACE_ADDRESS_ADD is sent in the following situations:
300 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
301 * from the client, after the ZEBRA_INTERFACE_ADD has been
302 * sent from zebra to the client
303 * - redistribute new address info to all clients in the following situations
304 * - at startup, when zebra figures out the available interfaces
305 * - when an interface is added (where support for
306 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
307 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
309 * - for the vty commands "ip address A.B.C.D/M [<secondary>|<label LINE>]"
310 * and "no bandwidth <1-10000000>", "ipv6 address X:X::X:X/M"
311 * - when an RTM_NEWADDR message is received from the kernel,
313 * The call tree that triggers ZEBRA_INTERFACE_ADDRESS_DELETE:
315 * zsend_interface_address(DELETE)
318 * zebra_interface_address_delete_update
320 * | | if_delete_update
322 * ip_address_uninstall connected_delete_ipv4
323 * [ipv6_addresss_uninstall] [connected_delete_ipv6]
326 * | RTM_NEWADDR on routing/netlink socket
329 * "no ip address A.B.C.D/M [label LINE]"
330 * "no ip address A.B.C.D/M secondary"
331 * ["no ipv6 address X:X::X:X/M"]
335 zsend_interface_address (int cmd
, struct zserv
*client
,
336 struct interface
*ifp
, struct connected
*ifc
)
345 zserv_create_header (s
, cmd
, ifp
->vrf_id
);
346 stream_putl (s
, ifp
->ifindex
);
348 /* Interface address flag. */
349 stream_putc (s
, ifc
->flags
);
351 /* Prefix information. */
353 stream_putc (s
, p
->family
);
354 blen
= prefix_blen (p
);
355 stream_put (s
, &p
->u
.prefix
, blen
);
358 * XXX gnu version does not send prefixlen for ZEBRA_INTERFACE_ADDRESS_DELETE
359 * but zebra_interface_address_delete_read() in the gnu version
362 stream_putc (s
, p
->prefixlen
);
365 p
= ifc
->destination
;
367 stream_put (s
, &p
->u
.prefix
, blen
);
369 stream_put (s
, NULL
, blen
);
371 /* Write packet size. */
372 stream_putw_at (s
, 0, stream_get_endp (s
));
374 client
->connected_rt_add_cnt
++;
375 return zebra_server_send_message(client
);
379 zsend_interface_nbr_address (int cmd
, struct zserv
*client
,
380 struct interface
*ifp
, struct nbr_connected
*ifc
)
389 zserv_create_header (s
, cmd
, ifp
->vrf_id
);
390 stream_putl (s
, ifp
->ifindex
);
392 /* Prefix information. */
394 stream_putc (s
, p
->family
);
395 blen
= prefix_blen (p
);
396 stream_put (s
, &p
->u
.prefix
, blen
);
399 * XXX gnu version does not send prefixlen for ZEBRA_INTERFACE_ADDRESS_DELETE
400 * but zebra_interface_address_delete_read() in the gnu version
403 stream_putc (s
, p
->prefixlen
);
405 /* Write packet size. */
406 stream_putw_at (s
, 0, stream_get_endp (s
));
408 return zebra_server_send_message(client
);
411 /* Interface address addition. */
413 zebra_interface_nbr_address_add_update (struct interface
*ifp
,
414 struct nbr_connected
*ifc
)
416 struct listnode
*node
, *nnode
;
417 struct zserv
*client
;
420 if (IS_ZEBRA_DEBUG_EVENT
)
422 char buf
[INET6_ADDRSTRLEN
];
425 zlog_debug ("MESSAGE: ZEBRA_INTERFACE_NBR_ADDRESS_ADD %s/%d on %s",
426 inet_ntop (p
->family
, &p
->u
.prefix
, buf
, INET6_ADDRSTRLEN
),
427 p
->prefixlen
, ifc
->ifp
->name
);
430 for (ALL_LIST_ELEMENTS (zebrad
.client_list
, node
, nnode
, client
))
431 zsend_interface_nbr_address (ZEBRA_INTERFACE_NBR_ADDRESS_ADD
, client
, ifp
, ifc
);
434 /* Interface address deletion. */
436 zebra_interface_nbr_address_delete_update (struct interface
*ifp
,
437 struct nbr_connected
*ifc
)
439 struct listnode
*node
, *nnode
;
440 struct zserv
*client
;
443 if (IS_ZEBRA_DEBUG_EVENT
)
445 char buf
[INET6_ADDRSTRLEN
];
448 zlog_debug ("MESSAGE: ZEBRA_INTERFACE_NBR_ADDRESS_DELETE %s/%d on %s",
449 inet_ntop (p
->family
, &p
->u
.prefix
, buf
, INET6_ADDRSTRLEN
),
450 p
->prefixlen
, ifc
->ifp
->name
);
453 for (ALL_LIST_ELEMENTS (zebrad
.client_list
, node
, nnode
, client
))
454 zsend_interface_nbr_address (ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
, client
, ifp
, ifc
);
457 /* Send addresses on interface to client */
459 zsend_interface_addresses (struct zserv
*client
, struct interface
*ifp
)
461 struct listnode
*cnode
, *cnnode
;
463 struct nbr_connected
*nc
;
465 /* Send interface addresses. */
466 for (ALL_LIST_ELEMENTS (ifp
->connected
, cnode
, cnnode
, c
))
468 if (!CHECK_FLAG (c
->conf
, ZEBRA_IFC_REAL
))
471 if (zsend_interface_address (ZEBRA_INTERFACE_ADDRESS_ADD
, client
,
476 /* Send interface neighbors. */
477 for (ALL_LIST_ELEMENTS (ifp
->nbr_connected
, cnode
, cnnode
, nc
))
479 if (zsend_interface_nbr_address (ZEBRA_INTERFACE_NBR_ADDRESS_ADD
,
480 client
, ifp
, nc
) < 0)
487 /* Notify client about interface moving from one VRF to another.
488 * Whether client is interested in old and new VRF is checked by caller.
491 zsend_interface_vrf_update (struct zserv
*client
, struct interface
*ifp
,
499 zserv_create_header (s
, ZEBRA_INTERFACE_VRF_UPDATE
, ifp
->vrf_id
);
501 /* Fill in the ifIndex of the interface and its new VRF (id) */
502 stream_putl (s
, ifp
->ifindex
);
503 stream_putw (s
, vrf_id
);
505 /* Write packet size. */
506 stream_putw_at (s
, 0, stream_get_endp (s
));
508 client
->if_vrfchg_cnt
++;
509 return zebra_server_send_message(client
);
512 /* Add new nbr connected IPv6 address */
514 nbr_connected_add_ipv6 (struct interface
*ifp
, struct in6_addr
*address
)
516 struct nbr_connected
*ifc
;
520 IPV6_ADDR_COPY (&p
.u
.prefix
, address
);
521 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
523 if (!(ifc
= listnode_head(ifp
->nbr_connected
)))
526 ifc
= nbr_connected_new ();
527 ifc
->address
= prefix_new();
529 listnode_add (ifp
->nbr_connected
, ifc
);
532 prefix_copy(ifc
->address
, &p
);
534 zebra_interface_nbr_address_add_update (ifp
, ifc
);
536 if_nbr_ipv6ll_to_ipv4ll_neigh_update (ifp
, address
, 1);
540 nbr_connected_delete_ipv6 (struct interface
*ifp
, struct in6_addr
*address
)
542 struct nbr_connected
*ifc
;
546 IPV6_ADDR_COPY (&p
.u
.prefix
, address
);
547 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
549 ifc
= nbr_connected_check(ifp
, &p
);
553 listnode_delete (ifp
->nbr_connected
, ifc
);
555 zebra_interface_nbr_address_delete_update (ifp
, ifc
);
557 if_nbr_ipv6ll_to_ipv4ll_neigh_update (ifp
, address
, 0);
559 nbr_connected_free (ifc
);
563 * The cmd passed to zsend_interface_update may be ZEBRA_INTERFACE_UP or
564 * ZEBRA_INTERFACE_DOWN.
566 * The ZEBRA_INTERFACE_UP message is sent from the zebra server to
567 * the clients in one of 2 situations:
568 * - an if_up is detected e.g., as a result of an RTM_IFINFO message
569 * - a vty command modifying the bandwidth of an interface is received.
570 * The ZEBRA_INTERFACE_DOWN message is sent when an if_down is detected.
573 zsend_interface_update (int cmd
, struct zserv
*client
, struct interface
*ifp
)
580 zserv_create_header (s
, cmd
, ifp
->vrf_id
);
581 zserv_encode_interface (s
, ifp
);
583 if (cmd
== ZEBRA_INTERFACE_UP
)
586 client
->ifdown_cnt
++;
588 return zebra_server_send_message(client
);
592 * This is the new function to announce and withdraw redistributed routes, used
593 * by Zebra. This is the old zsend_route_multipath() function. That function
594 * was duplicating code to send a lot of information that was essentially thrown
595 * away or ignored by the receiver. This is the leaner function that is not a
596 * duplicate of the zapi_ipv4_route_add/del.
598 * The primary difference is that this function merely sends a single NH instead of
602 zsend_redistribute_route (int add
, struct zserv
*client
, struct prefix
*p
,
609 struct nexthop
*nexthop
;
610 unsigned long nhnummark
= 0, messmark
= 0;
612 u_char zapi_flags
= 0;
613 struct nexthop dummy_nh
;
615 afi
= family2afi (p
->family
);
621 cmd
= ZEBRA_REDISTRIBUTE_IPV4_ADD
;
622 client
->redist_v4_add_cnt
++;
625 cmd
= ZEBRA_REDISTRIBUTE_IPV6_ADD
;
626 client
->redist_v6_add_cnt
++;
637 cmd
= ZEBRA_REDISTRIBUTE_IPV4_DEL
;
638 client
->redist_v4_del_cnt
++;
641 cmd
= ZEBRA_REDISTRIBUTE_IPV6_DEL
;
642 client
->redist_v6_del_cnt
++;
651 memset(&dummy_nh
, 0, sizeof(struct nexthop
));
653 zserv_create_header (s
, cmd
, rib
->vrf_id
);
655 /* Put type and nexthop. */
656 stream_putc (s
, rib
->type
);
657 stream_putw (s
, rib
->instance
);
658 stream_putl (s
, rib
->flags
);
660 /* marker for message flags field */
661 messmark
= stream_get_endp (s
);
665 psize
= PSIZE (p
->prefixlen
);
666 stream_putc (s
, p
->prefixlen
);
667 stream_write (s
, (u_char
*) & p
->u
.prefix
, psize
);
669 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
671 /* We don't send any nexthops when there's a multipath */
672 if (rib
->nexthop_active_num
> 1 && client
->proto
!= ZEBRA_ROUTE_LDP
)
674 SET_FLAG (zapi_flags
, ZAPI_MESSAGE_NEXTHOP
);
675 SET_FLAG (zapi_flags
, ZAPI_MESSAGE_IFINDEX
);
678 if (p
->family
== AF_INET
)
680 stream_put_in_addr (s
, &dummy_nh
.gate
.ipv4
);
682 else if (p
->family
== AF_INET6
)
684 stream_write (s
, (u_char
*) &dummy_nh
.gate
.ipv6
, 16);
688 /* We don't handle anything else now, abort */
689 zlog_err("%s: Unable to redistribute route of unknown family, %d\n",
690 __func__
, p
->family
);
694 stream_putl (s
, 0); /* dummy ifindex */
698 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
700 SET_FLAG (zapi_flags
, ZAPI_MESSAGE_NEXTHOP
);
701 SET_FLAG (zapi_flags
, ZAPI_MESSAGE_IFINDEX
);
704 nhnummark
= stream_get_endp (s
);
705 stream_putc (s
, 1); /* placeholder */
709 switch(nexthop
->type
)
711 case NEXTHOP_TYPE_IPV4
:
712 case NEXTHOP_TYPE_IPV4_IFINDEX
:
713 stream_put_in_addr (s
, &nexthop
->gate
.ipv4
);
715 case NEXTHOP_TYPE_IPV6
:
716 case NEXTHOP_TYPE_IPV6_IFINDEX
:
717 /* Only BGP supports IPv4 prefix with IPv6 NH, so kill this */
718 if (p
->family
== AF_INET
)
719 stream_put_in_addr(s
, &dummy_nh
.gate
.ipv4
);
721 stream_write (s
, (u_char
*) &nexthop
->gate
.ipv6
, 16);
724 if (cmd
== ZEBRA_REDISTRIBUTE_IPV4_ADD
725 || cmd
== ZEBRA_REDISTRIBUTE_IPV4_DEL
)
727 struct in_addr empty
;
728 memset (&empty
, 0, sizeof (struct in_addr
));
729 stream_write (s
, (u_char
*) &empty
, IPV4_MAX_BYTELEN
);
733 struct in6_addr empty
;
734 memset (&empty
, 0, sizeof (struct in6_addr
));
735 stream_write (s
, (u_char
*) &empty
, IPV6_MAX_BYTELEN
);
739 /* Interface index. */
741 stream_putl (s
, nexthop
->ifindex
);
743 /* ldpd needs all nexthops */
744 if (client
->proto
!= ZEBRA_ROUTE_LDP
)
750 SET_FLAG (zapi_flags
, ZAPI_MESSAGE_DISTANCE
);
751 stream_putc (s
, rib
->distance
);
754 SET_FLAG (zapi_flags
, ZAPI_MESSAGE_METRIC
);
755 stream_putl (s
, rib
->metric
);
760 SET_FLAG(zapi_flags
, ZAPI_MESSAGE_TAG
);
761 stream_putl(s
, rib
->tag
);
765 SET_FLAG (zapi_flags
, ZAPI_MESSAGE_MTU
);
766 stream_putl (s
, rib
->mtu
);
768 /* write real message flags value */
769 stream_putc_at (s
, messmark
, zapi_flags
);
771 /* Write next-hop number */
773 stream_putc_at (s
, nhnummark
, nhnum
);
775 /* Write packet size. */
776 stream_putw_at (s
, 0, stream_get_endp (s
));
778 return zebra_server_send_message(client
);
782 zsend_write_nexthop (struct stream
*s
, struct nexthop
*nexthop
)
784 stream_putc (s
, nexthop
->type
);
785 switch (nexthop
->type
)
787 case NEXTHOP_TYPE_IPV4
:
788 stream_put_in_addr (s
, &nexthop
->gate
.ipv4
);
790 case NEXTHOP_TYPE_IPV4_IFINDEX
:
791 stream_put_in_addr (s
, &nexthop
->gate
.ipv4
);
792 stream_putl (s
, nexthop
->ifindex
);
794 case NEXTHOP_TYPE_IPV6
:
795 stream_put (s
, &nexthop
->gate
.ipv6
, 16);
797 case NEXTHOP_TYPE_IPV6_IFINDEX
:
798 stream_put (s
, &nexthop
->gate
.ipv6
, 16);
799 stream_putl (s
, nexthop
->ifindex
);
801 case NEXTHOP_TYPE_IFINDEX
:
802 stream_putl (s
, nexthop
->ifindex
);
811 /* Nexthop register */
813 zserv_rnh_register (struct zserv
*client
, int sock
, u_short length
,
814 rnh_type_t type
, struct zebra_vrf
*zvrf
)
822 if (IS_ZEBRA_DEBUG_NHT
)
823 zlog_debug("rnh_register msg from client %s: length=%d, type=%s\n",
824 zebra_route_string(client
->proto
), length
,
825 (type
== RNH_NEXTHOP_TYPE
) ? "nexthop" : "route");
829 client
->nh_reg_time
= quagga_monotime();
833 flags
= stream_getc(s
);
834 p
.family
= stream_getw(s
);
835 p
.prefixlen
= stream_getc(s
);
837 if (p
.family
== AF_INET
)
839 p
.u
.prefix4
.s_addr
= stream_get_ipv4(s
);
840 l
+= IPV4_MAX_BYTELEN
;
842 else if (p
.family
== AF_INET6
)
844 stream_get(&p
.u
.prefix6
, s
, IPV6_MAX_BYTELEN
);
845 l
+= IPV6_MAX_BYTELEN
;
849 zlog_err("rnh_register: Received unknown family type %d\n",
853 rnh
= zebra_add_rnh(&p
, zvrf_id (zvrf
), type
);
854 if (type
== RNH_NEXTHOP_TYPE
)
856 if (flags
&& !CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
))
857 SET_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
);
858 else if (!flags
&& CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
))
859 UNSET_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
);
861 else if (type
== RNH_IMPORT_CHECK_TYPE
)
863 if (flags
&& !CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
))
864 SET_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
);
865 else if (!flags
&& CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
))
866 UNSET_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
);
869 zebra_add_rnh_client(rnh
, client
, type
, zvrf_id (zvrf
));
870 /* Anything not AF_INET/INET6 has been filtered out above */
871 zebra_evaluate_rnh(zvrf_id (zvrf
), p
.family
, 1, type
, &p
);
876 /* Nexthop register */
878 zserv_rnh_unregister (struct zserv
*client
, int sock
, u_short length
,
879 rnh_type_t type
, struct zebra_vrf
*zvrf
)
886 if (IS_ZEBRA_DEBUG_NHT
)
887 zlog_debug("rnh_unregister msg from client %s: length=%d\n",
888 zebra_route_string(client
->proto
), length
);
894 (void)stream_getc(s
); //Connected or not. Not used in this function
895 p
.family
= stream_getw(s
);
896 p
.prefixlen
= stream_getc(s
);
898 if (p
.family
== AF_INET
)
900 p
.u
.prefix4
.s_addr
= stream_get_ipv4(s
);
901 l
+= IPV4_MAX_BYTELEN
;
903 else if (p
.family
== AF_INET6
)
905 stream_get(&p
.u
.prefix6
, s
, IPV6_MAX_BYTELEN
);
906 l
+= IPV6_MAX_BYTELEN
;
910 zlog_err("rnh_register: Received unknown family type %d\n",
914 rnh
= zebra_lookup_rnh(&p
, zvrf_id (zvrf
), type
);
917 client
->nh_dereg_time
= quagga_monotime();
918 zebra_remove_rnh_client(rnh
, client
, type
);
925 Modified version of zsend_ipv4_nexthop_lookup():
926 Query unicast rib if nexthop is not found on mrib.
927 Returns both route metric and protocol distance.
930 zsend_ipv4_nexthop_lookup_mrib (struct zserv
*client
, struct in_addr addr
, struct rib
*rib
, struct zebra_vrf
*zvrf
)
935 struct nexthop
*nexthop
;
937 /* Get output stream. */
941 /* Fill in result. */
942 zserv_create_header (s
, ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB
, zvrf_id (zvrf
));
943 stream_put_in_addr (s
, &addr
);
947 stream_putc (s
, rib
->distance
);
948 stream_putl (s
, rib
->metric
);
950 nump
= stream_get_endp(s
); /* remember position for nexthop_num */
951 stream_putc (s
, 0); /* reserve room for nexthop_num */
952 /* Only non-recursive routes are elegible to resolve the nexthop we
953 * are looking up. Therefore, we will just iterate over the top
954 * chain of nexthops. */
955 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
956 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
957 num
+= zsend_write_nexthop (s
, nexthop
);
959 stream_putc_at (s
, nump
, num
); /* store nexthop_num */
963 stream_putc (s
, 0); /* distance */
964 stream_putl (s
, 0); /* metric */
965 stream_putc (s
, 0); /* nexthop_num */
968 stream_putw_at (s
, 0, stream_get_endp (s
));
970 return zebra_server_send_message(client
);
973 /* Router-id is updated. Send ZEBRA_ROUTER_ID_ADD to client. */
975 zsend_router_id_update (struct zserv
*client
, struct prefix
*p
,
981 /* Check this client need interface information. */
982 if (! vrf_bitmap_check (client
->ridinfo
, vrf_id
))
989 zserv_create_header (s
, ZEBRA_ROUTER_ID_UPDATE
, vrf_id
);
991 /* Prefix information. */
992 stream_putc (s
, p
->family
);
993 blen
= prefix_blen (p
);
994 stream_put (s
, &p
->u
.prefix
, blen
);
995 stream_putc (s
, p
->prefixlen
);
997 /* Write packet size. */
998 stream_putw_at (s
, 0, stream_get_endp (s
));
1000 return zebra_server_send_message(client
);
1003 /* Register zebra server interface information. Send current all
1004 interface and address information. */
1006 zread_interface_add (struct zserv
*client
, u_short length
, struct zebra_vrf
*zvrf
)
1009 struct listnode
*ifnode
, *ifnnode
;
1010 struct interface
*ifp
;
1012 /* Interface information is needed. */
1013 vrf_bitmap_set (client
->ifinfo
, zvrf_id (zvrf
));
1015 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
)
1017 for (ALL_LIST_ELEMENTS (vrf
->iflist
, ifnode
, ifnnode
, ifp
))
1019 /* Skip pseudo interface. */
1020 if (! CHECK_FLAG (ifp
->status
, ZEBRA_INTERFACE_ACTIVE
))
1023 if (zsend_interface_add (client
, ifp
) < 0)
1026 if (zsend_interface_addresses (client
, ifp
) < 0)
1033 /* Unregister zebra server interface information. */
1035 zread_interface_delete (struct zserv
*client
, u_short length
, struct zebra_vrf
*zvrf
)
1037 vrf_bitmap_unset (client
->ifinfo
, zvrf_id (zvrf
));
1042 zserv_nexthop_num_warn (const char *caller
, const struct prefix
*p
, const unsigned int nexthop_num
)
1044 if (nexthop_num
> MULTIPATH_NUM
)
1046 char buff
[PREFIX2STR_BUFFER
];
1047 prefix2str(p
, buff
, sizeof (buff
));
1048 zlog_warn("%s: Prefix %s has %d nexthops, but we can only use the first %d",
1049 caller
, buff
, nexthop_num
, MULTIPATH_NUM
);
1053 /* This function support multiple nexthop. */
1055 * Parse the ZEBRA_IPV4_ROUTE_ADD sent from client. Update rib and
1059 zread_ipv4_add (struct zserv
*client
, u_short length
, struct zebra_vrf
*zvrf
)
1065 struct in_addr nexthop
;
1067 u_char nexthop_type
;
1073 /* Get input stream. */
1076 /* Allocate new rib. */
1077 rib
= XCALLOC (MTYPE_RIB
, sizeof (struct rib
));
1079 /* Type, flags, message. */
1080 rib
->type
= stream_getc (s
);
1081 rib
->instance
= stream_getw (s
);
1082 rib
->flags
= stream_getl (s
);
1083 message
= stream_getc (s
);
1084 safi
= stream_getw (s
);
1085 rib
->uptime
= time (NULL
);
1088 memset (&p
, 0, sizeof (struct prefix_ipv4
));
1090 p
.prefixlen
= stream_getc (s
);
1091 stream_get (&p
.u
.prefix4
, s
, PSIZE (p
.prefixlen
));
1094 rib
->vrf_id
= zvrf_id (zvrf
);
1096 /* Nexthop parse. */
1097 if (CHECK_FLAG (message
, ZAPI_MESSAGE_NEXTHOP
))
1099 nexthop_num
= stream_getc (s
);
1100 zserv_nexthop_num_warn(__func__
, (const struct prefix
*)&p
, nexthop_num
);
1102 for (i
= 0; i
< nexthop_num
; i
++)
1104 nexthop_type
= stream_getc (s
);
1106 switch (nexthop_type
)
1108 case NEXTHOP_TYPE_IFINDEX
:
1109 ifindex
= stream_getl (s
);
1110 rib_nexthop_ifindex_add (rib
, ifindex
);
1112 case NEXTHOP_TYPE_IPV4
:
1113 nexthop
.s_addr
= stream_get_ipv4 (s
);
1114 rib_nexthop_ipv4_add (rib
, &nexthop
, NULL
);
1116 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1117 nexthop
.s_addr
= stream_get_ipv4 (s
);
1118 ifindex
= stream_getl (s
);
1119 rib_nexthop_ipv4_ifindex_add (rib
, &nexthop
, NULL
, ifindex
);
1121 case NEXTHOP_TYPE_IPV6
:
1122 stream_forward_getp (s
, IPV6_MAX_BYTELEN
);
1124 case NEXTHOP_TYPE_BLACKHOLE
:
1125 rib_nexthop_blackhole_add (rib
);
1132 if (CHECK_FLAG (message
, ZAPI_MESSAGE_DISTANCE
))
1133 rib
->distance
= stream_getc (s
);
1136 if (CHECK_FLAG (message
, ZAPI_MESSAGE_METRIC
))
1137 rib
->metric
= stream_getl (s
);
1140 if (CHECK_FLAG (message
, ZAPI_MESSAGE_TAG
))
1141 rib
->tag
= stream_getl (s
);
1145 if (CHECK_FLAG (message
, ZAPI_MESSAGE_MTU
))
1146 rib
->mtu
= stream_getl (s
);
1151 rib
->table
= zvrf
->table_id
;
1153 ret
= rib_add_multipath (AFI_IP
, safi
, &p
, rib
);
1157 client
->v4_route_add_cnt
++;
1159 client
->v4_route_upd8_cnt
++;
1163 /* Zebra server IPv4 prefix delete function. */
1165 zread_ipv4_delete (struct zserv
*client
, u_short length
, struct zebra_vrf
*zvrf
)
1169 struct zapi_ipv4 api
;
1170 struct in_addr nexthop
;
1171 union g_addr
*nexthop_p
;
1172 unsigned long ifindex
;
1175 u_char nexthop_type
;
1183 /* Type, flags, message. */
1184 api
.type
= stream_getc (s
);
1185 api
.instance
= stream_getw (s
);
1186 api
.flags
= stream_getl (s
);
1187 api
.message
= stream_getc (s
);
1188 api
.safi
= stream_getw (s
);
1191 memset (&p
, 0, sizeof (struct prefix_ipv4
));
1193 p
.prefixlen
= stream_getc (s
);
1194 stream_get (&p
.u
.prefix4
, s
, PSIZE (p
.prefixlen
));
1196 /* Nexthop, ifindex, distance, metric. */
1197 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
))
1199 nexthop_num
= stream_getc (s
);
1201 for (i
= 0; i
< nexthop_num
; i
++)
1203 nexthop_type
= stream_getc (s
);
1205 switch (nexthop_type
)
1207 case NEXTHOP_TYPE_IFINDEX
:
1208 ifindex
= stream_getl (s
);
1210 case NEXTHOP_TYPE_IPV4
:
1211 nexthop
.s_addr
= stream_get_ipv4 (s
);
1212 nexthop_p
= (union g_addr
*)&nexthop
;
1214 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1215 nexthop
.s_addr
= stream_get_ipv4 (s
);
1216 nexthop_p
= (union g_addr
*)&nexthop
;
1217 ifindex
= stream_getl (s
);
1219 case NEXTHOP_TYPE_IPV6
:
1220 stream_forward_getp (s
, IPV6_MAX_BYTELEN
);
1227 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_DISTANCE
))
1228 api
.distance
= stream_getc (s
);
1233 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_METRIC
))
1234 api
.metric
= stream_getl (s
);
1239 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_TAG
))
1240 api
.tag
= stream_getl (s
);
1244 table_id
= zvrf
->table_id
;
1246 rib_delete (AFI_IP
, api
.safi
, zvrf_id (zvrf
), api
.type
, api
.instance
,
1247 api
.flags
, &p
, nexthop_p
, ifindex
, table_id
);
1248 client
->v4_route_del_cnt
++;
1252 /* MRIB Nexthop lookup for IPv4. */
1254 zread_ipv4_nexthop_lookup_mrib (struct zserv
*client
, u_short length
, struct zebra_vrf
*zvrf
)
1256 struct in_addr addr
;
1259 addr
.s_addr
= stream_get_ipv4 (client
->ibuf
);
1260 rib
= rib_match_ipv4_multicast (zvrf_id (zvrf
), addr
, NULL
);
1261 return zsend_ipv4_nexthop_lookup_mrib (client
, addr
, rib
, zvrf
);
1264 /* Zebra server IPv6 prefix add function. */
1266 zread_ipv4_route_ipv6_nexthop_add (struct zserv
*client
, u_short length
, struct zebra_vrf
*zvrf
)
1270 struct in6_addr nexthop
;
1274 u_char nexthop_type
;
1277 static struct in6_addr nexthops
[MULTIPATH_NUM
];
1278 static unsigned int ifindices
[MULTIPATH_NUM
];
1281 /* Get input stream. */
1284 memset (&nexthop
, 0, sizeof (struct in6_addr
));
1286 /* Allocate new rib. */
1287 rib
= XCALLOC (MTYPE_RIB
, sizeof (struct rib
));
1289 /* Type, flags, message. */
1290 rib
->type
= stream_getc (s
);
1291 rib
->instance
= stream_getw (s
);
1292 rib
->flags
= stream_getl (s
);
1293 message
= stream_getc (s
);
1294 safi
= stream_getw (s
);
1295 rib
->uptime
= time (NULL
);
1298 memset (&p
, 0, sizeof (struct prefix_ipv4
));
1300 p
.prefixlen
= stream_getc (s
);
1301 stream_get (&p
.u
.prefix4
, s
, PSIZE (p
.prefixlen
));
1304 rib
->vrf_id
= zvrf_id (zvrf
);
1306 /* We need to give nh-addr, nh-ifindex with the same next-hop object
1307 * to the rib to ensure that IPv6 multipathing works; need to coalesce
1308 * these. Clients should send the same number of paired set of
1309 * next-hop-addr/next-hop-ifindices. */
1310 if (CHECK_FLAG (message
, ZAPI_MESSAGE_NEXTHOP
))
1316 nexthop_num
= stream_getc (s
);
1317 zserv_nexthop_num_warn(__func__
, (const struct prefix
*)&p
, nexthop_num
);
1318 for (i
= 0; i
< nexthop_num
; i
++)
1320 nexthop_type
= stream_getc (s
);
1322 switch (nexthop_type
)
1324 case NEXTHOP_TYPE_IPV6
:
1325 stream_get (&nexthop
, s
, 16);
1326 if (nh_count
< MULTIPATH_NUM
) {
1327 nexthops
[nh_count
++] = nexthop
;
1330 case NEXTHOP_TYPE_IFINDEX
:
1331 if (if_count
< MULTIPATH_NUM
) {
1332 ifindices
[if_count
++] = stream_getl (s
);
1335 case NEXTHOP_TYPE_BLACKHOLE
:
1336 rib_nexthop_blackhole_add (rib
);
1341 max_nh_if
= (nh_count
> if_count
) ? nh_count
: if_count
;
1342 for (i
= 0; i
< max_nh_if
; i
++)
1344 if ((i
< nh_count
) && !IN6_IS_ADDR_UNSPECIFIED (&nexthops
[i
])) {
1345 if ((i
< if_count
) && ifindices
[i
]) {
1346 rib_nexthop_ipv6_ifindex_add (rib
, &nexthops
[i
], ifindices
[i
]);
1349 rib_nexthop_ipv6_add (rib
, &nexthops
[i
]);
1353 if ((i
< if_count
) && ifindices
[i
]) {
1354 rib_nexthop_ifindex_add (rib
, ifindices
[i
]);
1361 if (CHECK_FLAG (message
, ZAPI_MESSAGE_DISTANCE
))
1362 rib
->distance
= stream_getc (s
);
1365 if (CHECK_FLAG (message
, ZAPI_MESSAGE_METRIC
))
1366 rib
->metric
= stream_getl (s
);
1369 if (CHECK_FLAG (message
, ZAPI_MESSAGE_TAG
))
1370 rib
->tag
= stream_getl (s
);
1374 if (CHECK_FLAG (message
, ZAPI_MESSAGE_MTU
))
1375 rib
->mtu
= stream_getl (s
);
1380 rib
->table
= zvrf
->table_id
;
1382 ret
= rib_add_multipath (AFI_IP6
, safi
, &p
, rib
);
1385 client
->v4_route_add_cnt
++;
1387 client
->v4_route_upd8_cnt
++;
1393 zread_ipv6_add (struct zserv
*client
, u_short length
, struct zebra_vrf
*zvrf
)
1397 struct in6_addr nexthop
;
1401 u_char nexthop_type
;
1404 static struct in6_addr nexthops
[MULTIPATH_NUM
];
1405 static unsigned int ifindices
[MULTIPATH_NUM
];
1408 /* Get input stream. */
1411 memset (&nexthop
, 0, sizeof (struct in6_addr
));
1413 /* Allocate new rib. */
1414 rib
= XCALLOC (MTYPE_RIB
, sizeof (struct rib
));
1416 /* Type, flags, message. */
1417 rib
->type
= stream_getc (s
);
1418 rib
->instance
= stream_getw (s
);
1419 rib
->flags
= stream_getl (s
);
1420 message
= stream_getc (s
);
1421 safi
= stream_getw (s
);
1422 rib
->uptime
= time (NULL
);
1425 memset (&p
, 0, sizeof (struct prefix_ipv6
));
1426 p
.family
= AF_INET6
;
1427 p
.prefixlen
= stream_getc (s
);
1428 stream_get (&p
.u
.prefix6
, s
, PSIZE (p
.prefixlen
));
1430 /* We need to give nh-addr, nh-ifindex with the same next-hop object
1431 * to the rib to ensure that IPv6 multipathing works; need to coalesce
1432 * these. Clients should send the same number of paired set of
1433 * next-hop-addr/next-hop-ifindices. */
1434 if (CHECK_FLAG (message
, ZAPI_MESSAGE_NEXTHOP
))
1440 nexthop_num
= stream_getc (s
);
1441 zserv_nexthop_num_warn(__func__
, (const struct prefix
*)&p
, nexthop_num
);
1442 for (i
= 0; i
< nexthop_num
; i
++)
1444 nexthop_type
= stream_getc (s
);
1446 switch (nexthop_type
)
1448 case NEXTHOP_TYPE_IPV6
:
1449 stream_get (&nexthop
, s
, 16);
1450 if (nh_count
< MULTIPATH_NUM
) {
1451 nexthops
[nh_count
++] = nexthop
;
1454 case NEXTHOP_TYPE_IFINDEX
:
1455 if (if_count
< MULTIPATH_NUM
) {
1456 ifindices
[if_count
++] = stream_getl (s
);
1459 case NEXTHOP_TYPE_BLACKHOLE
:
1460 rib_nexthop_blackhole_add (rib
);
1465 max_nh_if
= (nh_count
> if_count
) ? nh_count
: if_count
;
1466 for (i
= 0; i
< max_nh_if
; i
++)
1468 if ((i
< nh_count
) && !IN6_IS_ADDR_UNSPECIFIED (&nexthops
[i
])) {
1469 if ((i
< if_count
) && ifindices
[i
])
1470 rib_nexthop_ipv6_ifindex_add (rib
, &nexthops
[i
], ifindices
[i
]);
1472 rib_nexthop_ipv6_add (rib
, &nexthops
[i
]);
1475 if ((i
< if_count
) && ifindices
[i
])
1476 rib_nexthop_ifindex_add (rib
, ifindices
[i
]);
1482 if (CHECK_FLAG (message
, ZAPI_MESSAGE_DISTANCE
))
1483 rib
->distance
= stream_getc (s
);
1486 if (CHECK_FLAG (message
, ZAPI_MESSAGE_METRIC
))
1487 rib
->metric
= stream_getl (s
);
1490 if (CHECK_FLAG (message
, ZAPI_MESSAGE_TAG
))
1491 rib
->tag
= stream_getl (s
);
1495 if (CHECK_FLAG (message
, ZAPI_MESSAGE_MTU
))
1496 rib
->mtu
= stream_getl (s
);
1501 rib
->vrf_id
= zvrf_id (zvrf
);
1502 rib
->table
= zvrf
->table_id
;
1504 ret
= rib_add_multipath (AFI_IP6
, safi
, &p
, rib
);
1507 client
->v6_route_add_cnt
++;
1509 client
->v6_route_upd8_cnt
++;
1514 /* Zebra server IPv6 prefix delete function. */
1516 zread_ipv6_delete (struct zserv
*client
, u_short length
, struct zebra_vrf
*zvrf
)
1520 struct zapi_ipv6 api
;
1521 struct in6_addr nexthop
;
1522 union g_addr
*pnexthop
;
1523 unsigned long ifindex
;
1528 memset (&nexthop
, 0, sizeof (struct in6_addr
));
1530 /* Type, flags, message. */
1531 api
.type
= stream_getc (s
);
1532 api
.instance
= stream_getw (s
);
1533 api
.flags
= stream_getl (s
);
1534 api
.message
= stream_getc (s
);
1535 api
.safi
= stream_getw (s
);
1538 memset (&p
, 0, sizeof (struct prefix_ipv6
));
1539 p
.family
= AF_INET6
;
1540 p
.prefixlen
= stream_getc (s
);
1541 stream_get (&p
.u
.prefix6
, s
, PSIZE (p
.prefixlen
));
1543 /* Nexthop, ifindex, distance, metric. */
1544 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
))
1546 u_char nexthop_type
;
1548 api
.nexthop_num
= stream_getc (s
);
1549 for (i
= 0; i
< api
.nexthop_num
; i
++)
1551 nexthop_type
= stream_getc (s
);
1553 switch (nexthop_type
)
1555 case NEXTHOP_TYPE_IPV6
:
1556 stream_get (&nexthop
, s
, 16);
1557 pnexthop
= (union g_addr
*)&nexthop
;
1559 case NEXTHOP_TYPE_IFINDEX
:
1560 ifindex
= stream_getl (s
);
1567 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_DISTANCE
))
1568 api
.distance
= stream_getc (s
);
1573 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_METRIC
))
1574 api
.metric
= stream_getl (s
);
1579 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_TAG
))
1580 api
.tag
= stream_getl (s
);
1584 if (IN6_IS_ADDR_UNSPECIFIED (&nexthop
))
1585 rib_delete (AFI_IP6
, api
.safi
, zvrf_id (zvrf
), api
.type
, api
.instance
,
1586 api
.flags
, &p
, NULL
, ifindex
, client
->rtm_table
);
1588 rib_delete (AFI_IP6
, api
.safi
, zvrf_id (zvrf
), api
.type
, api
.instance
,
1589 api
.flags
, &p
, pnexthop
, ifindex
, client
->rtm_table
);
1591 client
->v6_route_del_cnt
++;
1595 /* Register zebra server router-id information. Send current router-id */
1597 zread_router_id_add (struct zserv
*client
, u_short length
, struct zebra_vrf
*zvrf
)
1601 /* Router-id information is needed. */
1602 vrf_bitmap_set (client
->ridinfo
, zvrf_id (zvrf
));
1604 router_id_get (&p
, zvrf_id (zvrf
));
1606 return zsend_router_id_update (client
, &p
, zvrf_id (zvrf
));
1609 /* Unregister zebra server router-id information. */
1611 zread_router_id_delete (struct zserv
*client
, u_short length
, struct zebra_vrf
*zvrf
)
1613 vrf_bitmap_unset (client
->ridinfo
, zvrf_id (zvrf
));
1617 /* Tie up route-type and client->sock */
1619 zread_hello (struct zserv
*client
)
1621 /* type of protocol (lib/zebra.h) */
1625 proto
= stream_getc (client
->ibuf
);
1626 instance
= stream_getw (client
->ibuf
);
1628 /* accept only dynamic routing protocols */
1629 if ((proto
< ZEBRA_ROUTE_MAX
)
1630 && (proto
> ZEBRA_ROUTE_STATIC
))
1632 zlog_notice ("client %d says hello and bids fair to announce only %s routes",
1633 client
->sock
, zebra_route_string(proto
));
1635 zlog_notice ("client protocol instance %d", instance
);
1637 client
->proto
= proto
;
1638 client
->instance
= instance
;
1642 /* Unregister all information in a VRF. */
1644 zread_vrf_unregister (struct zserv
*client
, u_short length
, struct zebra_vrf
*zvrf
)
1649 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
1650 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
1651 vrf_bitmap_unset (client
->redist
[afi
][i
], zvrf_id (zvrf
));
1652 vrf_bitmap_unset (client
->redist_default
, zvrf_id (zvrf
));
1653 vrf_bitmap_unset (client
->ifinfo
, zvrf_id (zvrf
));
1654 vrf_bitmap_unset (client
->ridinfo
, zvrf_id (zvrf
));
1660 zread_mpls_labels (int command
, struct zserv
*client
, u_short length
,
1664 enum lsp_types_t type
;
1665 struct prefix prefix
;
1666 enum nexthop_types_t gtype
;
1669 mpls_label_t in_label
, out_label
;
1671 struct zebra_vrf
*zvrf
;
1673 zvrf
= vrf_info_lookup (vrf_id
);
1677 /* Get input stream. */
1681 type
= stream_getc (s
);
1682 prefix
.family
= stream_getl (s
);
1683 switch (prefix
.family
)
1686 prefix
.u
.prefix4
.s_addr
= stream_get_ipv4 (s
);
1687 prefix
.prefixlen
= stream_getc (s
);
1688 gate
.ipv4
.s_addr
= stream_get_ipv4 (s
);
1691 stream_get (&prefix
.u
.prefix6
, s
, 16);
1692 prefix
.prefixlen
= stream_getc (s
);
1693 stream_get (&gate
.ipv6
, s
, 16);
1698 ifindex
= stream_getl (s
);
1699 distance
= stream_getc (s
);
1700 in_label
= stream_getl (s
);
1701 out_label
= stream_getl (s
);
1703 switch (prefix
.family
)
1707 gtype
= NEXTHOP_TYPE_IPV4_IFINDEX
;
1709 gtype
= NEXTHOP_TYPE_IPV4
;
1713 gtype
= NEXTHOP_TYPE_IPV6_IFINDEX
;
1715 gtype
= NEXTHOP_TYPE_IPV6
;
1724 if (command
== ZEBRA_MPLS_LABELS_ADD
)
1726 mpls_lsp_install (zvrf
, type
, in_label
, out_label
, gtype
, &gate
,
1728 if (out_label
!= MPLS_IMP_NULL_LABEL
)
1729 mpls_ftn_update (1, zvrf
, type
, &prefix
, gtype
, &gate
, ifindex
,
1730 distance
, out_label
);
1732 else if (command
== ZEBRA_MPLS_LABELS_DELETE
)
1734 mpls_lsp_uninstall (zvrf
, type
, in_label
, gtype
, &gate
, NULL
, ifindex
);
1735 if (out_label
!= MPLS_IMP_NULL_LABEL
)
1736 mpls_ftn_update (0, zvrf
, type
, &prefix
, gtype
, &gate
, ifindex
,
1737 distance
, out_label
);
1741 /* Cleanup registered nexthops (across VRFs) upon client disconnect. */
1743 zebra_client_close_cleanup_rnh (struct zserv
*client
)
1746 struct zebra_vrf
*zvrf
;
1748 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
)
1750 if ((zvrf
= vrf
->info
) != NULL
)
1752 zebra_cleanup_rnh_client(zvrf_id (zvrf
), AF_INET
, client
, RNH_NEXTHOP_TYPE
);
1753 zebra_cleanup_rnh_client(zvrf_id (zvrf
), AF_INET6
, client
, RNH_NEXTHOP_TYPE
);
1754 zebra_cleanup_rnh_client(zvrf_id (zvrf
), AF_INET
, client
, RNH_IMPORT_CHECK_TYPE
);
1755 zebra_cleanup_rnh_client(zvrf_id (zvrf
), AF_INET6
, client
, RNH_IMPORT_CHECK_TYPE
);
1756 if (client
->proto
== ZEBRA_ROUTE_LDP
)
1758 hash_iterate(zvrf
->lsp_table
, mpls_ldp_lsp_uninstall_all
,
1760 mpls_ldp_ftn_uninstall_all (zvrf
, AFI_IP
);
1761 mpls_ldp_ftn_uninstall_all (zvrf
, AFI_IP6
);
1767 /* Close zebra client. */
1769 zebra_client_close (struct zserv
*client
)
1771 /* Send client de-registration to BFD */
1772 zebra_ptm_bfd_client_deregister(client
->proto
);
1774 /* Cleanup any registered nexthops - across all VRFs. */
1775 zebra_client_close_cleanup_rnh (client
);
1777 /* Close file descriptor. */
1780 unsigned long nroutes
;
1782 close (client
->sock
);
1783 nroutes
= rib_score_proto (client
->proto
, client
->instance
);
1784 zlog_notice ("client %d disconnected. %lu %s routes removed from the rib",
1785 client
->sock
, nroutes
, zebra_route_string (client
->proto
));
1789 /* Free stream buffers. */
1791 stream_free (client
->ibuf
);
1793 stream_free (client
->obuf
);
1795 buffer_free(client
->wb
);
1797 /* Release threads. */
1799 thread_cancel (client
->t_read
);
1800 if (client
->t_write
)
1801 thread_cancel (client
->t_write
);
1802 if (client
->t_suicide
)
1803 thread_cancel (client
->t_suicide
);
1805 /* Free client structure. */
1806 listnode_delete (zebrad
.client_list
, client
);
1807 XFREE (MTYPE_TMP
, client
);
1810 /* Make new client. */
1812 zebra_client_create (int sock
)
1814 struct zserv
*client
;
1818 client
= XCALLOC (MTYPE_TMP
, sizeof (struct zserv
));
1820 /* Make client input/output buffer. */
1821 client
->sock
= sock
;
1822 client
->ibuf
= stream_new (ZEBRA_MAX_PACKET_SIZ
);
1823 client
->obuf
= stream_new (ZEBRA_MAX_PACKET_SIZ
);
1824 client
->wb
= buffer_new(0);
1826 /* Set table number. */
1827 client
->rtm_table
= zebrad
.rtm_table_default
;
1829 client
->connect_time
= quagga_monotime();
1830 /* Initialize flags */
1831 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
1832 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
1833 client
->redist
[afi
][i
] = vrf_bitmap_init ();
1834 client
->redist_default
= vrf_bitmap_init ();
1835 client
->ifinfo
= vrf_bitmap_init ();
1836 client
->ridinfo
= vrf_bitmap_init ();
1838 /* Add this client to linked list. */
1839 listnode_add (zebrad
.client_list
, client
);
1841 /* Make new read thread. */
1842 zebra_event (ZEBRA_READ
, sock
, client
);
1844 zebra_vrf_update_all (client
);
1847 /* Handler of zebra service request. */
1849 zebra_client_read (struct thread
*thread
)
1852 struct zserv
*client
;
1854 uint16_t length
, command
;
1855 uint8_t marker
, version
;
1857 struct zebra_vrf
*zvrf
;
1859 /* Get thread data. Reset reading thread because I'm running. */
1860 sock
= THREAD_FD (thread
);
1861 client
= THREAD_ARG (thread
);
1862 client
->t_read
= NULL
;
1864 if (client
->t_suicide
)
1866 zebra_client_close(client
);
1870 /* Read length and command (if we don't have it already). */
1871 if ((already
= stream_get_endp(client
->ibuf
)) < ZEBRA_HEADER_SIZE
)
1874 if (((nbyte
= stream_read_try (client
->ibuf
, sock
,
1875 ZEBRA_HEADER_SIZE
-already
)) == 0) ||
1878 if (IS_ZEBRA_DEBUG_EVENT
)
1879 zlog_debug ("connection closed socket [%d]", sock
);
1880 zebra_client_close (client
);
1883 if (nbyte
!= (ssize_t
)(ZEBRA_HEADER_SIZE
-already
))
1885 /* Try again later. */
1886 zebra_event (ZEBRA_READ
, sock
, client
);
1889 already
= ZEBRA_HEADER_SIZE
;
1892 /* Reset to read from the beginning of the incoming packet. */
1893 stream_set_getp(client
->ibuf
, 0);
1895 /* Fetch header values */
1896 length
= stream_getw (client
->ibuf
);
1897 marker
= stream_getc (client
->ibuf
);
1898 version
= stream_getc (client
->ibuf
);
1899 vrf_id
= stream_getw (client
->ibuf
);
1900 command
= stream_getw (client
->ibuf
);
1902 if (marker
!= ZEBRA_HEADER_MARKER
|| version
!= ZSERV_VERSION
)
1904 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
1905 __func__
, sock
, marker
, version
);
1906 zebra_client_close (client
);
1909 if (length
< ZEBRA_HEADER_SIZE
)
1911 zlog_warn("%s: socket %d message length %u is less than header size %d",
1912 __func__
, sock
, length
, ZEBRA_HEADER_SIZE
);
1913 zebra_client_close (client
);
1916 if (length
> STREAM_SIZE(client
->ibuf
))
1918 zlog_warn("%s: socket %d message length %u exceeds buffer size %lu",
1919 __func__
, sock
, length
, (u_long
)STREAM_SIZE(client
->ibuf
));
1920 zebra_client_close (client
);
1924 /* Read rest of data. */
1925 if (already
< length
)
1928 if (((nbyte
= stream_read_try (client
->ibuf
, sock
,
1929 length
-already
)) == 0) ||
1932 if (IS_ZEBRA_DEBUG_EVENT
)
1933 zlog_debug ("connection closed [%d] when reading zebra data", sock
);
1934 zebra_client_close (client
);
1937 if (nbyte
!= (ssize_t
)(length
-already
))
1939 /* Try again later. */
1940 zebra_event (ZEBRA_READ
, sock
, client
);
1945 length
-= ZEBRA_HEADER_SIZE
;
1947 /* Debug packet information. */
1948 if (IS_ZEBRA_DEBUG_EVENT
)
1949 zlog_debug ("zebra message comes from socket [%d]", sock
);
1951 if (IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
)
1952 zlog_debug ("zebra message received [%s] %d in VRF %u",
1953 zserv_command_string (command
), length
, vrf_id
);
1955 client
->last_read_time
= quagga_monotime();
1956 client
->last_read_cmd
= command
;
1958 zvrf
= zebra_vrf_lookup_by_id (vrf_id
);
1961 if (IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
)
1962 zlog_debug ("zebra received unknown VRF[%u]", vrf_id
);
1963 goto zclient_read_out
;
1968 case ZEBRA_ROUTER_ID_ADD
:
1969 zread_router_id_add (client
, length
, zvrf
);
1971 case ZEBRA_ROUTER_ID_DELETE
:
1972 zread_router_id_delete (client
, length
, zvrf
);
1974 case ZEBRA_INTERFACE_ADD
:
1975 zread_interface_add (client
, length
, zvrf
);
1977 case ZEBRA_INTERFACE_DELETE
:
1978 zread_interface_delete (client
, length
, zvrf
);
1980 case ZEBRA_IPV4_ROUTE_ADD
:
1981 zread_ipv4_add (client
, length
, zvrf
);
1983 case ZEBRA_IPV4_ROUTE_DELETE
:
1984 zread_ipv4_delete (client
, length
, zvrf
);
1986 case ZEBRA_IPV4_ROUTE_IPV6_NEXTHOP_ADD
:
1987 zread_ipv4_route_ipv6_nexthop_add (client
, length
, zvrf
);
1989 case ZEBRA_IPV4_NEXTHOP_ADD
:
1990 zread_ipv4_add(client
, length
, zvrf
); /* LB: r1.0 merge - id was 1 */
1992 case ZEBRA_IPV4_NEXTHOP_DELETE
:
1993 zread_ipv4_delete(client
, length
, zvrf
); /* LB: r1.0 merge - id was 1 */
1995 case ZEBRA_IPV6_ROUTE_ADD
:
1996 zread_ipv6_add (client
, length
, zvrf
);
1998 case ZEBRA_IPV6_ROUTE_DELETE
:
1999 zread_ipv6_delete (client
, length
, zvrf
);
2001 case ZEBRA_REDISTRIBUTE_ADD
:
2002 zebra_redistribute_add (command
, client
, length
, zvrf
);
2004 case ZEBRA_REDISTRIBUTE_DELETE
:
2005 zebra_redistribute_delete (command
, client
, length
, zvrf
);
2007 case ZEBRA_REDISTRIBUTE_DEFAULT_ADD
:
2008 zebra_redistribute_default_add (command
, client
, length
, zvrf
);
2010 case ZEBRA_REDISTRIBUTE_DEFAULT_DELETE
:
2011 zebra_redistribute_default_delete (command
, client
, length
, zvrf
);
2013 case ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB
:
2014 zread_ipv4_nexthop_lookup_mrib (client
, length
, zvrf
);
2017 zread_hello (client
);
2019 case ZEBRA_NEXTHOP_REGISTER
:
2020 zserv_rnh_register(client
, sock
, length
, RNH_NEXTHOP_TYPE
, zvrf
);
2022 case ZEBRA_NEXTHOP_UNREGISTER
:
2023 zserv_rnh_unregister(client
, sock
, length
, RNH_NEXTHOP_TYPE
, zvrf
);
2025 case ZEBRA_IMPORT_ROUTE_REGISTER
:
2026 zserv_rnh_register(client
, sock
, length
, RNH_IMPORT_CHECK_TYPE
, zvrf
);
2028 case ZEBRA_IMPORT_ROUTE_UNREGISTER
:
2029 zserv_rnh_unregister(client
, sock
, length
, RNH_IMPORT_CHECK_TYPE
, zvrf
);
2031 case ZEBRA_BFD_DEST_UPDATE
:
2032 case ZEBRA_BFD_DEST_REGISTER
:
2033 zebra_ptm_bfd_dst_register(client
, sock
, length
, command
, zvrf
);
2035 case ZEBRA_BFD_DEST_DEREGISTER
:
2036 zebra_ptm_bfd_dst_deregister(client
, sock
, length
, zvrf
);
2038 case ZEBRA_VRF_UNREGISTER
:
2039 zread_vrf_unregister (client
, length
, zvrf
);
2041 case ZEBRA_BFD_CLIENT_REGISTER
:
2042 zebra_ptm_bfd_client_register(client
, sock
, length
);
2044 case ZEBRA_INTERFACE_ENABLE_RADV
:
2045 zebra_interface_radv_set (client
, sock
, length
, zvrf
, 1);
2047 case ZEBRA_INTERFACE_DISABLE_RADV
:
2048 zebra_interface_radv_set (client
, sock
, length
, zvrf
, 0);
2050 case ZEBRA_MPLS_LABELS_ADD
:
2051 case ZEBRA_MPLS_LABELS_DELETE
:
2052 zread_mpls_labels (command
, client
, length
, vrf_id
);
2055 zlog_info ("Zebra received unknown command %d", command
);
2059 if (client
->t_suicide
)
2061 /* No need to wait for thread callback, just kill immediately. */
2062 zebra_client_close(client
);
2067 stream_reset (client
->ibuf
);
2068 zebra_event (ZEBRA_READ
, sock
, client
);
2073 /* Accept code of zebra server socket. */
2075 zebra_accept (struct thread
*thread
)
2079 struct sockaddr_in client
;
2082 accept_sock
= THREAD_FD (thread
);
2084 /* Reregister myself. */
2085 zebra_event (ZEBRA_SERV
, accept_sock
, NULL
);
2087 len
= sizeof (struct sockaddr_in
);
2088 client_sock
= accept (accept_sock
, (struct sockaddr
*) &client
, &len
);
2090 if (client_sock
< 0)
2092 zlog_warn ("Can't accept zebra socket: %s", safe_strerror (errno
));
2096 /* Make client socket non-blocking. */
2097 set_nonblocking(client_sock
);
2099 /* Create new zebra client. */
2100 zebra_client_create (client_sock
);
2105 #ifdef HAVE_TCP_ZEBRA
2106 /* Make zebra's server socket. */
2112 struct sockaddr_in addr
;
2114 accept_sock
= socket (AF_INET
, SOCK_STREAM
, 0);
2116 if (accept_sock
< 0)
2118 zlog_warn ("Can't create zserv stream socket: %s",
2119 safe_strerror (errno
));
2120 zlog_warn ("zebra can't provice full functionality due to above error");
2124 memset (&addr
, 0, sizeof (struct sockaddr_in
));
2125 addr
.sin_family
= AF_INET
;
2126 addr
.sin_port
= htons (ZEBRA_PORT
);
2127 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
2128 addr
.sin_len
= sizeof (struct sockaddr_in
);
2129 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
2130 addr
.sin_addr
.s_addr
= htonl (INADDR_LOOPBACK
);
2132 sockopt_reuseaddr (accept_sock
);
2133 sockopt_reuseport (accept_sock
);
2135 if ( zserv_privs
.change(ZPRIVS_RAISE
) )
2136 zlog (NULL
, LOG_ERR
, "Can't raise privileges");
2138 ret
= bind (accept_sock
, (struct sockaddr
*)&addr
,
2139 sizeof (struct sockaddr_in
));
2142 zlog_warn ("Can't bind to stream socket: %s",
2143 safe_strerror (errno
));
2144 zlog_warn ("zebra can't provice full functionality due to above error");
2145 close (accept_sock
); /* Avoid sd leak. */
2149 if ( zserv_privs
.change(ZPRIVS_LOWER
) )
2150 zlog (NULL
, LOG_ERR
, "Can't lower privileges");
2152 ret
= listen (accept_sock
, 1);
2155 zlog_warn ("Can't listen to stream socket: %s",
2156 safe_strerror (errno
));
2157 zlog_warn ("zebra can't provice full functionality due to above error");
2158 close (accept_sock
); /* Avoid sd leak. */
2162 zebra_event (ZEBRA_SERV
, accept_sock
, NULL
);
2164 #else /* HAVE_TCP_ZEBRA */
2166 /* For sockaddr_un. */
2169 /* zebra server UNIX domain socket. */
2171 zebra_serv_un (const char *path
)
2175 struct sockaddr_un serv
;
2178 /* First of all, unlink existing socket */
2182 old_mask
= umask (0077);
2184 /* Make UNIX domain socket. */
2185 sock
= socket (AF_UNIX
, SOCK_STREAM
, 0);
2188 zlog_warn ("Can't create zserv unix socket: %s",
2189 safe_strerror (errno
));
2190 zlog_warn ("zebra can't provide full functionality due to above error");
2194 /* Make server socket. */
2195 memset (&serv
, 0, sizeof (struct sockaddr_un
));
2196 serv
.sun_family
= AF_UNIX
;
2197 strncpy (serv
.sun_path
, path
, strlen (path
));
2198 #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
2199 len
= serv
.sun_len
= SUN_LEN(&serv
);
2201 len
= sizeof (serv
.sun_family
) + strlen (serv
.sun_path
);
2202 #endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
2204 ret
= bind (sock
, (struct sockaddr
*) &serv
, len
);
2207 zlog_warn ("Can't bind to unix socket %s: %s",
2208 path
, safe_strerror (errno
));
2209 zlog_warn ("zebra can't provide full functionality due to above error");
2214 ret
= listen (sock
, 5);
2217 zlog_warn ("Can't listen to unix socket %s: %s",
2218 path
, safe_strerror (errno
));
2219 zlog_warn ("zebra can't provide full functionality due to above error");
2226 zebra_event (ZEBRA_SERV
, sock
, NULL
);
2228 #endif /* HAVE_TCP_ZEBRA */
2232 zebra_event (enum event event
, int sock
, struct zserv
*client
)
2237 thread_add_read (zebrad
.master
, zebra_accept
, client
, sock
);
2241 thread_add_read (zebrad
.master
, zebra_client_read
, client
, sock
);
2249 #define ZEBRA_TIME_BUF 32
2251 zserv_time_buf(time_t *time1
, char *buf
, int buflen
)
2256 assert (buf
!= NULL
);
2257 assert (buflen
>= ZEBRA_TIME_BUF
);
2258 assert (time1
!= NULL
);
2262 snprintf(buf
, buflen
, "never ");
2266 now
= quagga_monotime();
2270 /* Making formatted timer strings. */
2271 #define ONE_DAY_SECOND 60*60*24
2272 #define ONE_WEEK_SECOND 60*60*24*7
2274 if (now
< ONE_DAY_SECOND
)
2275 snprintf (buf
, buflen
, "%02d:%02d:%02d",
2276 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
);
2277 else if (now
< ONE_WEEK_SECOND
)
2278 snprintf (buf
, buflen
, "%dd%02dh%02dm",
2279 tm
->tm_yday
, tm
->tm_hour
, tm
->tm_min
);
2281 snprintf (buf
, buflen
, "%02dw%dd%02dh",
2282 tm
->tm_yday
/7, tm
->tm_yday
- ((tm
->tm_yday
/7) * 7), tm
->tm_hour
);
2287 zebra_show_client_detail (struct vty
*vty
, struct zserv
*client
)
2289 char cbuf
[ZEBRA_TIME_BUF
], rbuf
[ZEBRA_TIME_BUF
];
2290 char wbuf
[ZEBRA_TIME_BUF
], nhbuf
[ZEBRA_TIME_BUF
], mbuf
[ZEBRA_TIME_BUF
];
2292 vty_out (vty
, "Client: %s", zebra_route_string(client
->proto
));
2293 if (client
->instance
)
2294 vty_out (vty
, " Instance: %d", client
->instance
);
2295 vty_out (vty
, "%s", VTY_NEWLINE
);
2297 vty_out (vty
, "------------------------ %s", VTY_NEWLINE
);
2298 vty_out (vty
, "FD: %d %s", client
->sock
, VTY_NEWLINE
);
2299 vty_out (vty
, "Route Table ID: %d %s", client
->rtm_table
, VTY_NEWLINE
);
2301 vty_out (vty
, "Connect Time: %s %s",
2302 zserv_time_buf(&client
->connect_time
, cbuf
, ZEBRA_TIME_BUF
),
2304 if (client
->nh_reg_time
)
2306 vty_out (vty
, "Nexthop Registry Time: %s %s",
2307 zserv_time_buf(&client
->nh_reg_time
, nhbuf
, ZEBRA_TIME_BUF
),
2309 if (client
->nh_last_upd_time
)
2310 vty_out (vty
, "Nexthop Last Update Time: %s %s",
2311 zserv_time_buf(&client
->nh_last_upd_time
, mbuf
, ZEBRA_TIME_BUF
),
2314 vty_out (vty
, "No Nexthop Update sent%s", VTY_NEWLINE
);
2317 vty_out (vty
, "Not registered for Nexthop Updates%s", VTY_NEWLINE
);
2319 vty_out (vty
, "Last Msg Rx Time: %s %s",
2320 zserv_time_buf(&client
->last_read_time
, rbuf
, ZEBRA_TIME_BUF
),
2322 vty_out (vty
, "Last Msg Tx Time: %s %s",
2323 zserv_time_buf(&client
->last_write_time
, wbuf
, ZEBRA_TIME_BUF
),
2325 if (client
->last_read_time
)
2326 vty_out (vty
, "Last Rcvd Cmd: %s %s",
2327 zserv_command_string(client
->last_read_cmd
), VTY_NEWLINE
);
2328 if (client
->last_write_time
)
2329 vty_out (vty
, "Last Sent Cmd: %s %s",
2330 zserv_command_string(client
->last_write_cmd
), VTY_NEWLINE
);
2331 vty_out (vty
, "%s", VTY_NEWLINE
);
2333 vty_out (vty
, "Type Add Update Del %s", VTY_NEWLINE
);
2334 vty_out (vty
, "================================================== %s", VTY_NEWLINE
);
2335 vty_out (vty
, "IPv4 %-12d%-12d%-12d%s", client
->v4_route_add_cnt
,
2336 client
->v4_route_upd8_cnt
, client
->v4_route_del_cnt
, VTY_NEWLINE
);
2337 vty_out (vty
, "IPv6 %-12d%-12d%-12d%s", client
->v6_route_add_cnt
,
2338 client
->v6_route_upd8_cnt
, client
->v6_route_del_cnt
, VTY_NEWLINE
);
2339 vty_out (vty
, "Redist:v4 %-12d%-12d%-12d%s", client
->redist_v4_add_cnt
, 0,
2340 client
->redist_v4_del_cnt
, VTY_NEWLINE
);
2341 vty_out (vty
, "Redist:v6 %-12d%-12d%-12d%s", client
->redist_v6_add_cnt
, 0,
2342 client
->redist_v6_del_cnt
, VTY_NEWLINE
);
2343 vty_out (vty
, "Connected %-12d%-12d%-12d%s", client
->ifadd_cnt
, 0,
2344 client
->ifdel_cnt
, VTY_NEWLINE
);
2345 vty_out (vty
, "BFD peer %-12d%-12d%-12d%s", client
->bfd_peer_add_cnt
,
2346 client
->bfd_peer_upd8_cnt
, client
->bfd_peer_del_cnt
, VTY_NEWLINE
);
2347 vty_out (vty
, "Interface Up Notifications: %d%s", client
->ifup_cnt
,
2349 vty_out (vty
, "Interface Down Notifications: %d%s", client
->ifdown_cnt
,
2352 vty_out (vty
, "%s", VTY_NEWLINE
);
2357 zebra_show_client_brief (struct vty
*vty
, struct zserv
*client
)
2359 char cbuf
[ZEBRA_TIME_BUF
], rbuf
[ZEBRA_TIME_BUF
];
2360 char wbuf
[ZEBRA_TIME_BUF
];
2362 vty_out (vty
, "%-8s%12s %12s%12s%8d/%-8d%8d/%-8d%s",
2363 zebra_route_string(client
->proto
),
2364 zserv_time_buf(&client
->connect_time
, cbuf
, ZEBRA_TIME_BUF
),
2365 zserv_time_buf(&client
->last_read_time
, rbuf
, ZEBRA_TIME_BUF
),
2366 zserv_time_buf(&client
->last_write_time
, wbuf
, ZEBRA_TIME_BUF
),
2367 client
->v4_route_add_cnt
+client
->v4_route_upd8_cnt
,
2368 client
->v4_route_del_cnt
,
2369 client
->v6_route_add_cnt
+client
->v6_route_upd8_cnt
,
2370 client
->v6_route_del_cnt
, VTY_NEWLINE
);
2375 /* Display default rtm_table for all clients. */
2380 "default routing table to use for all clients\n")
2382 vty_out (vty
, "table %d%s", zebrad
.rtm_table_default
,
2387 DEFUN (config_table
,
2390 "Configure target kernel routing table\n"
2393 zebrad
.rtm_table_default
= strtol (argv
[1]->arg
, (char**)0, 10);
2397 DEFUN (no_config_table
,
2398 no_config_table_cmd
,
2399 "no table [TABLENO]",
2401 "Configure target kernel routing table\n"
2404 zebrad
.rtm_table_default
= 0;
2408 DEFUN (ip_forwarding
,
2412 "Turn on IP forwarding")
2418 ret
= ipforward_on ();
2422 vty_out (vty
, "Can't turn on IP forwarding%s", VTY_NEWLINE
);
2429 DEFUN (no_ip_forwarding
,
2430 no_ip_forwarding_cmd
,
2434 "Turn off IP forwarding")
2440 ret
= ipforward_off ();
2444 vty_out (vty
, "Can't turn off IP forwarding%s", VTY_NEWLINE
);
2451 /* This command is for debugging purpose. */
2452 DEFUN (show_zebra_client
,
2453 show_zebra_client_cmd
,
2454 "show zebra client",
2456 "Zebra information\n"
2457 "Client information")
2459 struct listnode
*node
;
2460 struct zserv
*client
;
2462 for (ALL_LIST_ELEMENTS_RO (zebrad
.client_list
, node
, client
))
2463 zebra_show_client_detail(vty
, client
);
2468 /* This command is for debugging purpose. */
2469 DEFUN (show_zebra_client_summary
,
2470 show_zebra_client_summary_cmd
,
2471 "show zebra client summary",
2473 "Zebra information brief"
2474 "Client information brief")
2476 struct listnode
*node
;
2477 struct zserv
*client
;
2479 vty_out (vty
, "Name Connect Time Last Read Last Write IPv4 Routes IPv6 Routes %s",
2481 vty_out (vty
,"--------------------------------------------------------------------------------%s",
2484 for (ALL_LIST_ELEMENTS_RO (zebrad
.client_list
, node
, client
))
2485 zebra_show_client_brief(vty
, client
);
2487 vty_out (vty
, "Routes column shows (added+updated)/deleted%s", VTY_NEWLINE
);
2491 /* Table configuration write function. */
2493 config_write_table (struct vty
*vty
)
2495 if (zebrad
.rtm_table_default
)
2496 vty_out (vty
, "table %d%s", zebrad
.rtm_table_default
,
2501 /* table node for routing tables. */
2502 static struct cmd_node table_node
=
2505 "", /* This node has no interface. */
2509 /* Only display ip forwarding is enabled or not. */
2510 DEFUN (show_ip_forwarding
,
2511 show_ip_forwarding_cmd
,
2512 "show ip forwarding",
2515 "IP forwarding status\n")
2522 vty_out (vty
, "IP forwarding is off%s", VTY_NEWLINE
);
2524 vty_out (vty
, "IP forwarding is on%s", VTY_NEWLINE
);
2528 /* Only display ipv6 forwarding is enabled or not. */
2529 DEFUN (show_ipv6_forwarding
,
2530 show_ipv6_forwarding_cmd
,
2531 "show ipv6 forwarding",
2533 "IPv6 information\n"
2534 "Forwarding status\n")
2538 ret
= ipforward_ipv6 ();
2543 vty_out (vty
, "ipv6 forwarding is unknown%s", VTY_NEWLINE
);
2546 vty_out (vty
, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE
);
2549 vty_out (vty
, "ipv6 forwarding is %s%s", "on", VTY_NEWLINE
);
2552 vty_out (vty
, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE
);
2558 DEFUN (ipv6_forwarding
,
2559 ipv6_forwarding_cmd
,
2562 "Turn on IPv6 forwarding")
2566 ret
= ipforward_ipv6 ();
2568 ret
= ipforward_ipv6_on ();
2572 vty_out (vty
, "Can't turn on IPv6 forwarding%s", VTY_NEWLINE
);
2579 DEFUN (no_ipv6_forwarding
,
2580 no_ipv6_forwarding_cmd
,
2581 "no ipv6 forwarding",
2584 "Turn off IPv6 forwarding")
2588 ret
= ipforward_ipv6 ();
2590 ret
= ipforward_ipv6_off ();
2594 vty_out (vty
, "Can't turn off IPv6 forwarding%s", VTY_NEWLINE
);
2601 /* IPForwarding configuration write function. */
2603 config_write_forwarding (struct vty
*vty
)
2605 /* FIXME: Find better place for that. */
2606 router_id_write (vty
);
2609 vty_out (vty
, "no ip forwarding%s", VTY_NEWLINE
);
2610 if (!ipforward_ipv6 ())
2611 vty_out (vty
, "no ipv6 forwarding%s", VTY_NEWLINE
);
2612 vty_out (vty
, "!%s", VTY_NEWLINE
);
2616 /* table node for routing tables. */
2617 static struct cmd_node forwarding_node
=
2620 "", /* This node has no interface. */
2625 /* function to write the fpm config info */
2627 config_write_fpm (struct vty
*vty
)
2630 fpm_remote_srv_write (vty
);
2634 static struct cmd_node zebra_node
=
2643 /* Initialisation of zebra and installation of commands. */
2647 /* Client list init. */
2648 zebrad
.client_list
= list_new ();
2650 /* Install configuration write function. */
2651 install_node (&table_node
, config_write_table
);
2652 install_node (&forwarding_node
, config_write_forwarding
);
2654 install_node (&zebra_node
, config_write_fpm
);
2657 install_element (VIEW_NODE
, &show_ip_forwarding_cmd
);
2658 install_element (CONFIG_NODE
, &ip_forwarding_cmd
);
2659 install_element (CONFIG_NODE
, &no_ip_forwarding_cmd
);
2660 install_element (ENABLE_NODE
, &show_zebra_client_cmd
);
2661 install_element (ENABLE_NODE
, &show_zebra_client_summary_cmd
);
2664 install_element (VIEW_NODE
, &show_table_cmd
);
2665 install_element (CONFIG_NODE
, &config_table_cmd
);
2666 install_element (CONFIG_NODE
, &no_config_table_cmd
);
2667 #endif /* HAVE_NETLINK */
2669 install_element (VIEW_NODE
, &show_ipv6_forwarding_cmd
);
2670 install_element (CONFIG_NODE
, &ipv6_forwarding_cmd
);
2671 install_element (CONFIG_NODE
, &no_ipv6_forwarding_cmd
);
2674 zebra_route_map_init ();
2677 /* Make zebra server socket, wiping any existing one (see bug #403). */
2679 zebra_zserv_socket_init (char *path
)
2681 #ifdef HAVE_TCP_ZEBRA
2684 zebra_serv_un (path
? path
: ZEBRA_SERV_PATH
);
2685 #endif /* HAVE_TCP_ZEBRA */