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"
57 #include "zebra/zebra_mroute.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
);
97 client
->t_write
= thread_add_write(zebrad
.master
, zserv_flush_data
,
98 client
, client
->sock
);
104 client
->last_write_time
= quagga_monotime();
109 zebra_server_send_message(struct zserv
*client
)
111 if (client
->t_suicide
)
114 stream_set_getp(client
->obuf
, 0);
115 client
->last_write_cmd
= stream_getw_from(client
->obuf
, 6);
116 switch (buffer_write(client
->wb
, client
->sock
, STREAM_DATA(client
->obuf
),
117 stream_get_endp(client
->obuf
)))
120 zlog_warn("%s: buffer_write failed to zserv client fd %d, closing",
121 __func__
, client
->sock
);
122 /* Schedule a delayed close since many of the functions that call this
123 one do not check the return code. They do not allow for the
124 possibility that an I/O error may have caused the client to be
126 client
->t_suicide
= thread_add_event(zebrad
.master
, zserv_delayed_close
,
130 THREAD_OFF(client
->t_write
);
133 THREAD_WRITE_ON(zebrad
.master
, client
->t_write
,
134 zserv_flush_data
, client
, client
->sock
);
138 client
->last_write_time
= quagga_monotime();
143 zserv_create_header (struct stream
*s
, uint16_t cmd
, vrf_id_t vrf_id
)
145 /* length placeholder, caller can update */
146 stream_putw (s
, ZEBRA_HEADER_SIZE
);
147 stream_putc (s
, ZEBRA_HEADER_MARKER
);
148 stream_putc (s
, ZSERV_VERSION
);
149 stream_putw (s
, vrf_id
);
150 stream_putw (s
, cmd
);
154 zserv_encode_interface (struct stream
*s
, struct interface
*ifp
)
156 /* Interface information. */
157 stream_put (s
, ifp
->name
, INTERFACE_NAMSIZ
);
158 stream_putl (s
, ifp
->ifindex
);
159 stream_putc (s
, ifp
->status
);
160 stream_putq (s
, ifp
->flags
);
161 stream_putc (s
, ifp
->ptm_enable
);
162 stream_putc (s
, ifp
->ptm_status
);
163 stream_putl (s
, ifp
->metric
);
164 stream_putl (s
, ifp
->mtu
);
165 stream_putl (s
, ifp
->mtu6
);
166 stream_putl (s
, ifp
->bandwidth
);
167 stream_putl (s
, ifp
->ll_type
);
168 stream_putl (s
, ifp
->hw_addr_len
);
169 if (ifp
->hw_addr_len
)
170 stream_put (s
, ifp
->hw_addr
, ifp
->hw_addr_len
);
172 /* Then, Traffic Engineering parameters if any */
173 if (HAS_LINK_PARAMS(ifp
) && IS_LINK_PARAMS_SET(ifp
->link_params
))
176 zebra_interface_link_params_write (s
, ifp
);
181 /* Write packet size. */
182 stream_putw_at (s
, 0, stream_get_endp (s
));
186 zserv_encode_vrf (struct stream
*s
, struct zebra_vrf
*zvrf
)
188 /* Interface information. */
189 stream_put (s
, zvrf_name (zvrf
), VRF_NAMSIZ
);
191 /* Write packet size. */
192 stream_putw_at (s
, 0, stream_get_endp (s
));
195 /* Interface is added. Send ZEBRA_INTERFACE_ADD to client. */
197 * This function is called in the following situations:
198 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
200 * - at startup, when zebra figures out the available interfaces
201 * - when an interface is added (where support for
202 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
203 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
207 zsend_interface_add (struct zserv
*client
, struct interface
*ifp
)
214 zserv_create_header (s
, ZEBRA_INTERFACE_ADD
, ifp
->vrf_id
);
215 zserv_encode_interface (s
, ifp
);
218 return zebra_server_send_message(client
);
221 /* Interface deletion from zebra daemon. */
223 zsend_interface_delete (struct zserv
*client
, struct interface
*ifp
)
230 zserv_create_header (s
, ZEBRA_INTERFACE_DELETE
, ifp
->vrf_id
);
231 zserv_encode_interface (s
, ifp
);
234 return zebra_server_send_message (client
);
238 zsend_vrf_add (struct zserv
*client
, struct zebra_vrf
*zvrf
)
245 zserv_create_header (s
, ZEBRA_VRF_ADD
, zvrf_id (zvrf
));
246 zserv_encode_vrf (s
, zvrf
);
248 client
->vrfadd_cnt
++;
249 return zebra_server_send_message(client
);
252 /* VRF deletion from zebra daemon. */
254 zsend_vrf_delete (struct zserv
*client
, struct zebra_vrf
*zvrf
)
261 zserv_create_header (s
, ZEBRA_VRF_DELETE
, zvrf_id (zvrf
));
262 zserv_encode_vrf (s
, zvrf
);
264 client
->vrfdel_cnt
++;
265 return zebra_server_send_message (client
);
269 zsend_interface_link_params (struct zserv
*client
, struct interface
*ifp
)
273 /* Check this client need interface information. */
274 if (! client
->ifinfo
)
277 if (!ifp
->link_params
)
282 zserv_create_header (s
, ZEBRA_INTERFACE_LINK_PARAMS
, ifp
->vrf_id
);
284 /* Add Interface Index */
285 stream_putl (s
, ifp
->ifindex
);
287 /* Then TE Link Parameters */
288 if (zebra_interface_link_params_write (s
, ifp
) == 0)
291 /* Write packet size. */
292 stream_putw_at (s
, 0, stream_get_endp (s
));
294 return zebra_server_send_message (client
);
297 /* Interface address is added/deleted. Send ZEBRA_INTERFACE_ADDRESS_ADD or
298 * ZEBRA_INTERFACE_ADDRESS_DELETE to the client.
300 * A ZEBRA_INTERFACE_ADDRESS_ADD is sent in the following situations:
301 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
302 * from the client, after the ZEBRA_INTERFACE_ADD has been
303 * sent from zebra to the client
304 * - redistribute new address info to all clients in the following situations
305 * - at startup, when zebra figures out the available interfaces
306 * - when an interface is added (where support for
307 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
308 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
310 * - for the vty commands "ip address A.B.C.D/M [<secondary>|<label LINE>]"
311 * and "no bandwidth <1-10000000>", "ipv6 address X:X::X:X/M"
312 * - when an RTM_NEWADDR message is received from the kernel,
314 * The call tree that triggers ZEBRA_INTERFACE_ADDRESS_DELETE:
316 * zsend_interface_address(DELETE)
319 * zebra_interface_address_delete_update
321 * | | if_delete_update
323 * ip_address_uninstall connected_delete_ipv4
324 * [ipv6_addresss_uninstall] [connected_delete_ipv6]
327 * | RTM_NEWADDR on routing/netlink socket
330 * "no ip address A.B.C.D/M [label LINE]"
331 * "no ip address A.B.C.D/M secondary"
332 * ["no ipv6 address X:X::X:X/M"]
336 zsend_interface_address (int cmd
, struct zserv
*client
,
337 struct interface
*ifp
, struct connected
*ifc
)
346 zserv_create_header (s
, cmd
, ifp
->vrf_id
);
347 stream_putl (s
, ifp
->ifindex
);
349 /* Interface address flag. */
350 stream_putc (s
, ifc
->flags
);
352 /* Prefix information. */
354 stream_putc (s
, p
->family
);
355 blen
= prefix_blen (p
);
356 stream_put (s
, &p
->u
.prefix
, blen
);
359 * XXX gnu version does not send prefixlen for ZEBRA_INTERFACE_ADDRESS_DELETE
360 * but zebra_interface_address_delete_read() in the gnu version
363 stream_putc (s
, p
->prefixlen
);
366 p
= ifc
->destination
;
368 stream_put (s
, &p
->u
.prefix
, blen
);
370 stream_put (s
, NULL
, blen
);
372 /* Write packet size. */
373 stream_putw_at (s
, 0, stream_get_endp (s
));
375 client
->connected_rt_add_cnt
++;
376 return zebra_server_send_message(client
);
380 zsend_interface_nbr_address (int cmd
, struct zserv
*client
,
381 struct interface
*ifp
, struct nbr_connected
*ifc
)
390 zserv_create_header (s
, cmd
, ifp
->vrf_id
);
391 stream_putl (s
, ifp
->ifindex
);
393 /* Prefix information. */
395 stream_putc (s
, p
->family
);
396 blen
= prefix_blen (p
);
397 stream_put (s
, &p
->u
.prefix
, blen
);
400 * XXX gnu version does not send prefixlen for ZEBRA_INTERFACE_ADDRESS_DELETE
401 * but zebra_interface_address_delete_read() in the gnu version
404 stream_putc (s
, p
->prefixlen
);
406 /* Write packet size. */
407 stream_putw_at (s
, 0, stream_get_endp (s
));
409 return zebra_server_send_message(client
);
412 /* Interface address addition. */
414 zebra_interface_nbr_address_add_update (struct interface
*ifp
,
415 struct nbr_connected
*ifc
)
417 struct listnode
*node
, *nnode
;
418 struct zserv
*client
;
421 if (IS_ZEBRA_DEBUG_EVENT
)
423 char buf
[INET6_ADDRSTRLEN
];
426 zlog_debug ("MESSAGE: ZEBRA_INTERFACE_NBR_ADDRESS_ADD %s/%d on %s",
427 inet_ntop (p
->family
, &p
->u
.prefix
, buf
, INET6_ADDRSTRLEN
),
428 p
->prefixlen
, ifc
->ifp
->name
);
431 for (ALL_LIST_ELEMENTS (zebrad
.client_list
, node
, nnode
, client
))
432 zsend_interface_nbr_address (ZEBRA_INTERFACE_NBR_ADDRESS_ADD
, client
, ifp
, ifc
);
435 /* Interface address deletion. */
437 zebra_interface_nbr_address_delete_update (struct interface
*ifp
,
438 struct nbr_connected
*ifc
)
440 struct listnode
*node
, *nnode
;
441 struct zserv
*client
;
444 if (IS_ZEBRA_DEBUG_EVENT
)
446 char buf
[INET6_ADDRSTRLEN
];
449 zlog_debug ("MESSAGE: ZEBRA_INTERFACE_NBR_ADDRESS_DELETE %s/%d on %s",
450 inet_ntop (p
->family
, &p
->u
.prefix
, buf
, INET6_ADDRSTRLEN
),
451 p
->prefixlen
, ifc
->ifp
->name
);
454 for (ALL_LIST_ELEMENTS (zebrad
.client_list
, node
, nnode
, client
))
455 zsend_interface_nbr_address (ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
, client
, ifp
, ifc
);
458 /* Send addresses on interface to client */
460 zsend_interface_addresses (struct zserv
*client
, struct interface
*ifp
)
462 struct listnode
*cnode
, *cnnode
;
464 struct nbr_connected
*nc
;
466 /* Send interface addresses. */
467 for (ALL_LIST_ELEMENTS (ifp
->connected
, cnode
, cnnode
, c
))
469 if (!CHECK_FLAG (c
->conf
, ZEBRA_IFC_REAL
))
472 if (zsend_interface_address (ZEBRA_INTERFACE_ADDRESS_ADD
, client
,
477 /* Send interface neighbors. */
478 for (ALL_LIST_ELEMENTS (ifp
->nbr_connected
, cnode
, cnnode
, nc
))
480 if (zsend_interface_nbr_address (ZEBRA_INTERFACE_NBR_ADDRESS_ADD
,
481 client
, ifp
, nc
) < 0)
488 /* Notify client about interface moving from one VRF to another.
489 * Whether client is interested in old and new VRF is checked by caller.
492 zsend_interface_vrf_update (struct zserv
*client
, struct interface
*ifp
,
500 zserv_create_header (s
, ZEBRA_INTERFACE_VRF_UPDATE
, ifp
->vrf_id
);
502 /* Fill in the ifIndex of the interface and its new VRF (id) */
503 stream_putl (s
, ifp
->ifindex
);
504 stream_putw (s
, vrf_id
);
506 /* Write packet size. */
507 stream_putw_at (s
, 0, stream_get_endp (s
));
509 client
->if_vrfchg_cnt
++;
510 return zebra_server_send_message(client
);
513 /* Add new nbr connected IPv6 address */
515 nbr_connected_add_ipv6 (struct interface
*ifp
, struct in6_addr
*address
)
517 struct nbr_connected
*ifc
;
521 IPV6_ADDR_COPY (&p
.u
.prefix
, address
);
522 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
524 if (!(ifc
= listnode_head(ifp
->nbr_connected
)))
527 ifc
= nbr_connected_new ();
528 ifc
->address
= prefix_new();
530 listnode_add (ifp
->nbr_connected
, ifc
);
533 prefix_copy(ifc
->address
, &p
);
535 zebra_interface_nbr_address_add_update (ifp
, ifc
);
537 if_nbr_ipv6ll_to_ipv4ll_neigh_update (ifp
, address
, 1);
541 nbr_connected_delete_ipv6 (struct interface
*ifp
, struct in6_addr
*address
)
543 struct nbr_connected
*ifc
;
547 IPV6_ADDR_COPY (&p
.u
.prefix
, address
);
548 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
550 ifc
= nbr_connected_check(ifp
, &p
);
554 listnode_delete (ifp
->nbr_connected
, ifc
);
556 zebra_interface_nbr_address_delete_update (ifp
, ifc
);
558 if_nbr_ipv6ll_to_ipv4ll_neigh_update (ifp
, address
, 0);
560 nbr_connected_free (ifc
);
564 * The cmd passed to zsend_interface_update may be ZEBRA_INTERFACE_UP or
565 * ZEBRA_INTERFACE_DOWN.
567 * The ZEBRA_INTERFACE_UP message is sent from the zebra server to
568 * the clients in one of 2 situations:
569 * - an if_up is detected e.g., as a result of an RTM_IFINFO message
570 * - a vty command modifying the bandwidth of an interface is received.
571 * The ZEBRA_INTERFACE_DOWN message is sent when an if_down is detected.
574 zsend_interface_update (int cmd
, struct zserv
*client
, struct interface
*ifp
)
581 zserv_create_header (s
, cmd
, ifp
->vrf_id
);
582 zserv_encode_interface (s
, ifp
);
584 if (cmd
== ZEBRA_INTERFACE_UP
)
587 client
->ifdown_cnt
++;
589 return zebra_server_send_message(client
);
593 * This is the new function to announce and withdraw redistributed routes, used
594 * by Zebra. This is the old zsend_route_multipath() function. That function
595 * was duplicating code to send a lot of information that was essentially thrown
596 * away or ignored by the receiver. This is the leaner function that is not a
597 * duplicate of the zapi_ipv4_route_add/del.
599 * The primary difference is that this function merely sends a single NH instead of
603 zsend_redistribute_route (int add
, struct zserv
*client
, struct prefix
*p
,
610 struct nexthop
*nexthop
;
611 unsigned long nhnummark
= 0, messmark
= 0;
613 u_char zapi_flags
= 0;
614 struct nexthop dummy_nh
;
616 afi
= family2afi (p
->family
);
622 cmd
= ZEBRA_REDISTRIBUTE_IPV4_ADD
;
623 client
->redist_v4_add_cnt
++;
626 cmd
= ZEBRA_REDISTRIBUTE_IPV6_ADD
;
627 client
->redist_v6_add_cnt
++;
638 cmd
= ZEBRA_REDISTRIBUTE_IPV4_DEL
;
639 client
->redist_v4_del_cnt
++;
642 cmd
= ZEBRA_REDISTRIBUTE_IPV6_DEL
;
643 client
->redist_v6_del_cnt
++;
652 memset(&dummy_nh
, 0, sizeof(struct nexthop
));
654 zserv_create_header (s
, cmd
, rib
->vrf_id
);
656 /* Put type and nexthop. */
657 stream_putc (s
, rib
->type
);
658 stream_putw (s
, rib
->instance
);
659 stream_putl (s
, rib
->flags
);
661 /* marker for message flags field */
662 messmark
= stream_get_endp (s
);
666 psize
= PSIZE (p
->prefixlen
);
667 stream_putc (s
, p
->prefixlen
);
668 stream_write (s
, (u_char
*) & p
->u
.prefix
, psize
);
670 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
672 /* We don't send any nexthops when there's a multipath */
673 if (rib
->nexthop_active_num
> 1 && client
->proto
!= ZEBRA_ROUTE_LDP
)
675 SET_FLAG (zapi_flags
, ZAPI_MESSAGE_NEXTHOP
);
676 SET_FLAG (zapi_flags
, ZAPI_MESSAGE_IFINDEX
);
679 if (p
->family
== AF_INET
)
681 stream_put_in_addr (s
, &dummy_nh
.gate
.ipv4
);
683 else if (p
->family
== AF_INET6
)
685 stream_write (s
, (u_char
*) &dummy_nh
.gate
.ipv6
, 16);
689 /* We don't handle anything else now, abort */
690 zlog_err("%s: Unable to redistribute route of unknown family, %d\n",
691 __func__
, p
->family
);
695 stream_putl (s
, 0); /* dummy ifindex */
699 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
701 SET_FLAG (zapi_flags
, ZAPI_MESSAGE_NEXTHOP
);
702 SET_FLAG (zapi_flags
, ZAPI_MESSAGE_IFINDEX
);
705 nhnummark
= stream_get_endp (s
);
706 stream_putc (s
, 1); /* placeholder */
710 switch(nexthop
->type
)
712 case NEXTHOP_TYPE_IPV4
:
713 case NEXTHOP_TYPE_IPV4_IFINDEX
:
714 stream_put_in_addr (s
, &nexthop
->gate
.ipv4
);
716 case NEXTHOP_TYPE_IPV6
:
717 case NEXTHOP_TYPE_IPV6_IFINDEX
:
718 /* Only BGP supports IPv4 prefix with IPv6 NH, so kill this */
719 if (p
->family
== AF_INET
)
720 stream_put_in_addr(s
, &dummy_nh
.gate
.ipv4
);
722 stream_write (s
, (u_char
*) &nexthop
->gate
.ipv6
, 16);
725 if (cmd
== ZEBRA_REDISTRIBUTE_IPV4_ADD
726 || cmd
== ZEBRA_REDISTRIBUTE_IPV4_DEL
)
728 struct in_addr empty
;
729 memset (&empty
, 0, sizeof (struct in_addr
));
730 stream_write (s
, (u_char
*) &empty
, IPV4_MAX_BYTELEN
);
734 struct in6_addr empty
;
735 memset (&empty
, 0, sizeof (struct in6_addr
));
736 stream_write (s
, (u_char
*) &empty
, IPV6_MAX_BYTELEN
);
740 /* Interface index. */
742 stream_putl (s
, nexthop
->ifindex
);
744 /* ldpd needs all nexthops */
745 if (client
->proto
!= ZEBRA_ROUTE_LDP
)
751 SET_FLAG (zapi_flags
, ZAPI_MESSAGE_DISTANCE
);
752 stream_putc (s
, rib
->distance
);
755 SET_FLAG (zapi_flags
, ZAPI_MESSAGE_METRIC
);
756 stream_putl (s
, rib
->metric
);
761 SET_FLAG(zapi_flags
, ZAPI_MESSAGE_TAG
);
762 stream_putl(s
, rib
->tag
);
766 SET_FLAG (zapi_flags
, ZAPI_MESSAGE_MTU
);
767 stream_putl (s
, rib
->mtu
);
769 /* write real message flags value */
770 stream_putc_at (s
, messmark
, zapi_flags
);
772 /* Write next-hop number */
774 stream_putc_at (s
, nhnummark
, nhnum
);
776 /* Write packet size. */
777 stream_putw_at (s
, 0, stream_get_endp (s
));
779 return zebra_server_send_message(client
);
783 zsend_write_nexthop (struct stream
*s
, struct nexthop
*nexthop
)
785 stream_putc (s
, nexthop
->type
);
786 switch (nexthop
->type
)
788 case NEXTHOP_TYPE_IPV4
:
789 case NEXTHOP_TYPE_IPV4_IFINDEX
:
790 stream_put_in_addr (s
, &nexthop
->gate
.ipv4
);
791 stream_putl (s
, nexthop
->ifindex
);
793 case NEXTHOP_TYPE_IPV6
:
794 stream_put (s
, &nexthop
->gate
.ipv6
, 16);
796 case NEXTHOP_TYPE_IPV6_IFINDEX
:
797 stream_put (s
, &nexthop
->gate
.ipv6
, 16);
798 stream_putl (s
, nexthop
->ifindex
);
800 case NEXTHOP_TYPE_IFINDEX
:
801 stream_putl (s
, nexthop
->ifindex
);
810 /* Nexthop register */
812 zserv_rnh_register (struct zserv
*client
, int sock
, u_short length
,
813 rnh_type_t type
, struct zebra_vrf
*zvrf
)
821 if (IS_ZEBRA_DEBUG_NHT
)
822 zlog_debug("rnh_register msg from client %s: length=%d, type=%s\n",
823 zebra_route_string(client
->proto
), length
,
824 (type
== RNH_NEXTHOP_TYPE
) ? "nexthop" : "route");
828 client
->nh_reg_time
= quagga_monotime();
832 flags
= stream_getc(s
);
833 p
.family
= stream_getw(s
);
834 p
.prefixlen
= stream_getc(s
);
836 if (p
.family
== AF_INET
)
838 p
.u
.prefix4
.s_addr
= stream_get_ipv4(s
);
839 l
+= IPV4_MAX_BYTELEN
;
841 else if (p
.family
== AF_INET6
)
843 stream_get(&p
.u
.prefix6
, s
, IPV6_MAX_BYTELEN
);
844 l
+= IPV6_MAX_BYTELEN
;
848 zlog_err("rnh_register: Received unknown family type %d\n",
852 rnh
= zebra_add_rnh(&p
, zvrf_id (zvrf
), type
);
853 if (type
== RNH_NEXTHOP_TYPE
)
855 if (flags
&& !CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
))
856 SET_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
);
857 else if (!flags
&& CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
))
858 UNSET_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
);
860 else if (type
== RNH_IMPORT_CHECK_TYPE
)
862 if (flags
&& !CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
))
863 SET_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
);
864 else if (!flags
&& CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
))
865 UNSET_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
);
868 zebra_add_rnh_client(rnh
, client
, type
, zvrf_id (zvrf
));
869 /* Anything not AF_INET/INET6 has been filtered out above */
870 zebra_evaluate_rnh(zvrf_id (zvrf
), p
.family
, 1, type
, &p
);
875 /* Nexthop register */
877 zserv_rnh_unregister (struct zserv
*client
, int sock
, u_short length
,
878 rnh_type_t type
, struct zebra_vrf
*zvrf
)
885 if (IS_ZEBRA_DEBUG_NHT
)
886 zlog_debug("rnh_unregister msg from client %s: length=%d\n",
887 zebra_route_string(client
->proto
), length
);
893 (void)stream_getc(s
); //Connected or not. Not used in this function
894 p
.family
= stream_getw(s
);
895 p
.prefixlen
= stream_getc(s
);
897 if (p
.family
== AF_INET
)
899 p
.u
.prefix4
.s_addr
= stream_get_ipv4(s
);
900 l
+= IPV4_MAX_BYTELEN
;
902 else if (p
.family
== AF_INET6
)
904 stream_get(&p
.u
.prefix6
, s
, IPV6_MAX_BYTELEN
);
905 l
+= IPV6_MAX_BYTELEN
;
909 zlog_err("rnh_register: Received unknown family type %d\n",
913 rnh
= zebra_lookup_rnh(&p
, zvrf_id (zvrf
), type
);
916 client
->nh_dereg_time
= quagga_monotime();
917 zebra_remove_rnh_client(rnh
, client
, type
);
924 Modified version of zsend_ipv4_nexthop_lookup():
925 Query unicast rib if nexthop is not found on mrib.
926 Returns both route metric and protocol distance.
929 zsend_ipv4_nexthop_lookup_mrib (struct zserv
*client
, struct in_addr addr
, struct rib
*rib
, struct zebra_vrf
*zvrf
)
934 struct nexthop
*nexthop
;
936 /* Get output stream. */
940 /* Fill in result. */
941 zserv_create_header (s
, ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB
, zvrf_id (zvrf
));
942 stream_put_in_addr (s
, &addr
);
946 stream_putc (s
, rib
->distance
);
947 stream_putl (s
, rib
->metric
);
949 nump
= stream_get_endp(s
); /* remember position for nexthop_num */
950 stream_putc (s
, 0); /* reserve room for nexthop_num */
951 /* Only non-recursive routes are elegible to resolve the nexthop we
952 * are looking up. Therefore, we will just iterate over the top
953 * chain of nexthops. */
954 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
955 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
956 num
+= zsend_write_nexthop (s
, nexthop
);
958 stream_putc_at (s
, nump
, num
); /* store nexthop_num */
962 stream_putc (s
, 0); /* distance */
963 stream_putl (s
, 0); /* metric */
964 stream_putc (s
, 0); /* nexthop_num */
967 stream_putw_at (s
, 0, stream_get_endp (s
));
969 return zebra_server_send_message(client
);
972 /* Router-id is updated. Send ZEBRA_ROUTER_ID_ADD to client. */
974 zsend_router_id_update (struct zserv
*client
, struct prefix
*p
,
980 /* Check this client need interface information. */
981 if (! vrf_bitmap_check (client
->ridinfo
, vrf_id
))
988 zserv_create_header (s
, ZEBRA_ROUTER_ID_UPDATE
, vrf_id
);
990 /* Prefix information. */
991 stream_putc (s
, p
->family
);
992 blen
= prefix_blen (p
);
993 stream_put (s
, &p
->u
.prefix
, blen
);
994 stream_putc (s
, p
->prefixlen
);
996 /* Write packet size. */
997 stream_putw_at (s
, 0, stream_get_endp (s
));
999 return zebra_server_send_message(client
);
1002 /* Register zebra server interface information. Send current all
1003 interface and address information. */
1005 zread_interface_add (struct zserv
*client
, u_short length
, struct zebra_vrf
*zvrf
)
1008 struct listnode
*ifnode
, *ifnnode
;
1009 struct interface
*ifp
;
1011 /* Interface information is needed. */
1012 vrf_bitmap_set (client
->ifinfo
, zvrf_id (zvrf
));
1014 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
)
1016 for (ALL_LIST_ELEMENTS (vrf
->iflist
, ifnode
, ifnnode
, ifp
))
1018 /* Skip pseudo interface. */
1019 if (! CHECK_FLAG (ifp
->status
, ZEBRA_INTERFACE_ACTIVE
))
1022 if (zsend_interface_add (client
, ifp
) < 0)
1025 if (zsend_interface_addresses (client
, ifp
) < 0)
1032 /* Unregister zebra server interface information. */
1034 zread_interface_delete (struct zserv
*client
, u_short length
, struct zebra_vrf
*zvrf
)
1036 vrf_bitmap_unset (client
->ifinfo
, zvrf_id (zvrf
));
1041 zserv_nexthop_num_warn (const char *caller
, const struct prefix
*p
, const unsigned int nexthop_num
)
1043 if (nexthop_num
> MULTIPATH_NUM
)
1045 char buff
[PREFIX2STR_BUFFER
];
1046 prefix2str(p
, buff
, sizeof (buff
));
1047 zlog_warn("%s: Prefix %s has %d nexthops, but we can only use the first %d",
1048 caller
, buff
, nexthop_num
, MULTIPATH_NUM
);
1052 /* This function support multiple nexthop. */
1054 * Parse the ZEBRA_IPV4_ROUTE_ADD sent from client. Update rib and
1058 zread_ipv4_add (struct zserv
*client
, u_short length
, struct zebra_vrf
*zvrf
)
1064 struct in_addr nexthop
;
1066 u_char nexthop_type
;
1072 /* Get input stream. */
1075 /* Allocate new rib. */
1076 rib
= XCALLOC (MTYPE_RIB
, sizeof (struct rib
));
1078 /* Type, flags, message. */
1079 rib
->type
= stream_getc (s
);
1080 rib
->instance
= stream_getw (s
);
1081 rib
->flags
= stream_getl (s
);
1082 message
= stream_getc (s
);
1083 safi
= stream_getw (s
);
1084 rib
->uptime
= time (NULL
);
1087 memset (&p
, 0, sizeof (struct prefix_ipv4
));
1089 p
.prefixlen
= stream_getc (s
);
1090 stream_get (&p
.u
.prefix4
, s
, PSIZE (p
.prefixlen
));
1093 rib
->vrf_id
= zvrf_id (zvrf
);
1095 /* Nexthop parse. */
1096 if (CHECK_FLAG (message
, ZAPI_MESSAGE_NEXTHOP
))
1098 nexthop_num
= stream_getc (s
);
1099 zserv_nexthop_num_warn(__func__
, (const struct prefix
*)&p
, nexthop_num
);
1101 for (i
= 0; i
< nexthop_num
; i
++)
1103 nexthop_type
= stream_getc (s
);
1105 switch (nexthop_type
)
1107 case NEXTHOP_TYPE_IFINDEX
:
1108 ifindex
= stream_getl (s
);
1109 rib_nexthop_ifindex_add (rib
, ifindex
);
1111 case NEXTHOP_TYPE_IPV4
:
1112 nexthop
.s_addr
= stream_get_ipv4 (s
);
1113 rib_nexthop_ipv4_add (rib
, &nexthop
, NULL
);
1115 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1116 nexthop
.s_addr
= stream_get_ipv4 (s
);
1117 ifindex
= stream_getl (s
);
1118 rib_nexthop_ipv4_ifindex_add (rib
, &nexthop
, NULL
, ifindex
);
1120 case NEXTHOP_TYPE_IPV6
:
1121 stream_forward_getp (s
, IPV6_MAX_BYTELEN
);
1123 case NEXTHOP_TYPE_BLACKHOLE
:
1124 rib_nexthop_blackhole_add (rib
);
1131 if (CHECK_FLAG (message
, ZAPI_MESSAGE_DISTANCE
))
1132 rib
->distance
= stream_getc (s
);
1135 if (CHECK_FLAG (message
, ZAPI_MESSAGE_METRIC
))
1136 rib
->metric
= stream_getl (s
);
1139 if (CHECK_FLAG (message
, ZAPI_MESSAGE_TAG
))
1140 rib
->tag
= stream_getl (s
);
1144 if (CHECK_FLAG (message
, ZAPI_MESSAGE_MTU
))
1145 rib
->mtu
= stream_getl (s
);
1150 rib
->table
= zvrf
->table_id
;
1152 ret
= rib_add_multipath (AFI_IP
, safi
, &p
, rib
);
1156 client
->v4_route_add_cnt
++;
1158 client
->v4_route_upd8_cnt
++;
1162 /* Zebra server IPv4 prefix delete function. */
1164 zread_ipv4_delete (struct zserv
*client
, u_short length
, struct zebra_vrf
*zvrf
)
1168 struct zapi_ipv4 api
;
1169 struct in_addr nexthop
;
1170 union g_addr
*nexthop_p
;
1171 unsigned long ifindex
;
1174 u_char nexthop_type
;
1182 /* Type, flags, message. */
1183 api
.type
= stream_getc (s
);
1184 api
.instance
= stream_getw (s
);
1185 api
.flags
= stream_getl (s
);
1186 api
.message
= stream_getc (s
);
1187 api
.safi
= stream_getw (s
);
1190 memset (&p
, 0, sizeof (struct prefix_ipv4
));
1192 p
.prefixlen
= stream_getc (s
);
1193 stream_get (&p
.u
.prefix4
, s
, PSIZE (p
.prefixlen
));
1195 /* Nexthop, ifindex, distance, metric. */
1196 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
))
1198 nexthop_num
= stream_getc (s
);
1200 for (i
= 0; i
< nexthop_num
; i
++)
1202 nexthop_type
= stream_getc (s
);
1204 switch (nexthop_type
)
1206 case NEXTHOP_TYPE_IFINDEX
:
1207 ifindex
= stream_getl (s
);
1209 case NEXTHOP_TYPE_IPV4
:
1210 nexthop
.s_addr
= stream_get_ipv4 (s
);
1211 nexthop_p
= (union g_addr
*)&nexthop
;
1213 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1214 nexthop
.s_addr
= stream_get_ipv4 (s
);
1215 nexthop_p
= (union g_addr
*)&nexthop
;
1216 ifindex
= stream_getl (s
);
1218 case NEXTHOP_TYPE_IPV6
:
1219 stream_forward_getp (s
, IPV6_MAX_BYTELEN
);
1226 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_DISTANCE
))
1227 api
.distance
= stream_getc (s
);
1232 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_METRIC
))
1233 api
.metric
= stream_getl (s
);
1238 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_TAG
))
1239 api
.tag
= stream_getl (s
);
1243 table_id
= zvrf
->table_id
;
1245 rib_delete (AFI_IP
, api
.safi
, zvrf_id (zvrf
), api
.type
, api
.instance
,
1246 api
.flags
, &p
, nexthop_p
, ifindex
, table_id
);
1247 client
->v4_route_del_cnt
++;
1251 /* MRIB Nexthop lookup for IPv4. */
1253 zread_ipv4_nexthop_lookup_mrib (struct zserv
*client
, u_short length
, struct zebra_vrf
*zvrf
)
1255 struct in_addr addr
;
1258 addr
.s_addr
= stream_get_ipv4 (client
->ibuf
);
1259 rib
= rib_match_ipv4_multicast (zvrf_id (zvrf
), addr
, NULL
);
1260 return zsend_ipv4_nexthop_lookup_mrib (client
, addr
, rib
, zvrf
);
1263 /* Zebra server IPv6 prefix add function. */
1265 zread_ipv4_route_ipv6_nexthop_add (struct zserv
*client
, u_short length
, struct zebra_vrf
*zvrf
)
1269 struct in6_addr nexthop
;
1273 u_char nexthop_type
;
1276 static struct in6_addr nexthops
[MULTIPATH_NUM
];
1277 static unsigned int ifindices
[MULTIPATH_NUM
];
1280 /* Get input stream. */
1283 memset (&nexthop
, 0, sizeof (struct in6_addr
));
1285 /* Allocate new rib. */
1286 rib
= XCALLOC (MTYPE_RIB
, sizeof (struct rib
));
1288 /* Type, flags, message. */
1289 rib
->type
= stream_getc (s
);
1290 rib
->instance
= stream_getw (s
);
1291 rib
->flags
= stream_getl (s
);
1292 message
= stream_getc (s
);
1293 safi
= stream_getw (s
);
1294 rib
->uptime
= time (NULL
);
1297 memset (&p
, 0, sizeof (struct prefix_ipv4
));
1299 p
.prefixlen
= stream_getc (s
);
1300 stream_get (&p
.u
.prefix4
, s
, PSIZE (p
.prefixlen
));
1303 rib
->vrf_id
= zvrf_id (zvrf
);
1305 /* We need to give nh-addr, nh-ifindex with the same next-hop object
1306 * to the rib to ensure that IPv6 multipathing works; need to coalesce
1307 * these. Clients should send the same number of paired set of
1308 * next-hop-addr/next-hop-ifindices. */
1309 if (CHECK_FLAG (message
, ZAPI_MESSAGE_NEXTHOP
))
1315 nexthop_num
= stream_getc (s
);
1316 zserv_nexthop_num_warn(__func__
, (const struct prefix
*)&p
, nexthop_num
);
1317 for (i
= 0; i
< nexthop_num
; i
++)
1319 nexthop_type
= stream_getc (s
);
1321 switch (nexthop_type
)
1323 case NEXTHOP_TYPE_IPV6
:
1324 stream_get (&nexthop
, s
, 16);
1325 if (nh_count
< MULTIPATH_NUM
) {
1326 nexthops
[nh_count
++] = nexthop
;
1329 case NEXTHOP_TYPE_IFINDEX
:
1330 if (if_count
< MULTIPATH_NUM
) {
1331 ifindices
[if_count
++] = stream_getl (s
);
1334 case NEXTHOP_TYPE_BLACKHOLE
:
1335 rib_nexthop_blackhole_add (rib
);
1340 max_nh_if
= (nh_count
> if_count
) ? nh_count
: if_count
;
1341 for (i
= 0; i
< max_nh_if
; i
++)
1343 if ((i
< nh_count
) && !IN6_IS_ADDR_UNSPECIFIED (&nexthops
[i
])) {
1344 if ((i
< if_count
) && ifindices
[i
]) {
1345 rib_nexthop_ipv6_ifindex_add (rib
, &nexthops
[i
], ifindices
[i
]);
1348 rib_nexthop_ipv6_add (rib
, &nexthops
[i
]);
1352 if ((i
< if_count
) && ifindices
[i
]) {
1353 rib_nexthop_ifindex_add (rib
, ifindices
[i
]);
1360 if (CHECK_FLAG (message
, ZAPI_MESSAGE_DISTANCE
))
1361 rib
->distance
= stream_getc (s
);
1364 if (CHECK_FLAG (message
, ZAPI_MESSAGE_METRIC
))
1365 rib
->metric
= stream_getl (s
);
1368 if (CHECK_FLAG (message
, ZAPI_MESSAGE_TAG
))
1369 rib
->tag
= stream_getl (s
);
1373 if (CHECK_FLAG (message
, ZAPI_MESSAGE_MTU
))
1374 rib
->mtu
= stream_getl (s
);
1379 rib
->table
= zvrf
->table_id
;
1381 ret
= rib_add_multipath (AFI_IP6
, safi
, &p
, rib
);
1384 client
->v4_route_add_cnt
++;
1386 client
->v4_route_upd8_cnt
++;
1392 zread_ipv6_add (struct zserv
*client
, u_short length
, struct zebra_vrf
*zvrf
)
1396 struct in6_addr nexthop
;
1400 u_char nexthop_type
;
1403 static struct in6_addr nexthops
[MULTIPATH_NUM
];
1404 static unsigned int ifindices
[MULTIPATH_NUM
];
1407 /* Get input stream. */
1410 memset (&nexthop
, 0, sizeof (struct in6_addr
));
1412 /* Allocate new rib. */
1413 rib
= XCALLOC (MTYPE_RIB
, sizeof (struct rib
));
1415 /* Type, flags, message. */
1416 rib
->type
= stream_getc (s
);
1417 rib
->instance
= stream_getw (s
);
1418 rib
->flags
= stream_getl (s
);
1419 message
= stream_getc (s
);
1420 safi
= stream_getw (s
);
1421 rib
->uptime
= time (NULL
);
1424 memset (&p
, 0, sizeof (struct prefix_ipv6
));
1425 p
.family
= AF_INET6
;
1426 p
.prefixlen
= stream_getc (s
);
1427 stream_get (&p
.u
.prefix6
, s
, PSIZE (p
.prefixlen
));
1429 /* We need to give nh-addr, nh-ifindex with the same next-hop object
1430 * to the rib to ensure that IPv6 multipathing works; need to coalesce
1431 * these. Clients should send the same number of paired set of
1432 * next-hop-addr/next-hop-ifindices. */
1433 if (CHECK_FLAG (message
, ZAPI_MESSAGE_NEXTHOP
))
1439 nexthop_num
= stream_getc (s
);
1440 zserv_nexthop_num_warn(__func__
, (const struct prefix
*)&p
, nexthop_num
);
1441 for (i
= 0; i
< nexthop_num
; i
++)
1443 nexthop_type
= stream_getc (s
);
1445 switch (nexthop_type
)
1447 case NEXTHOP_TYPE_IPV6
:
1448 stream_get (&nexthop
, s
, 16);
1449 if (nh_count
< MULTIPATH_NUM
) {
1450 nexthops
[nh_count
++] = nexthop
;
1453 case NEXTHOP_TYPE_IFINDEX
:
1454 if (if_count
< MULTIPATH_NUM
) {
1455 ifindices
[if_count
++] = stream_getl (s
);
1458 case NEXTHOP_TYPE_BLACKHOLE
:
1459 rib_nexthop_blackhole_add (rib
);
1464 max_nh_if
= (nh_count
> if_count
) ? nh_count
: if_count
;
1465 for (i
= 0; i
< max_nh_if
; i
++)
1467 if ((i
< nh_count
) && !IN6_IS_ADDR_UNSPECIFIED (&nexthops
[i
])) {
1468 if ((i
< if_count
) && ifindices
[i
])
1469 rib_nexthop_ipv6_ifindex_add (rib
, &nexthops
[i
], ifindices
[i
]);
1471 rib_nexthop_ipv6_add (rib
, &nexthops
[i
]);
1474 if ((i
< if_count
) && ifindices
[i
])
1475 rib_nexthop_ifindex_add (rib
, ifindices
[i
]);
1481 if (CHECK_FLAG (message
, ZAPI_MESSAGE_DISTANCE
))
1482 rib
->distance
= stream_getc (s
);
1485 if (CHECK_FLAG (message
, ZAPI_MESSAGE_METRIC
))
1486 rib
->metric
= stream_getl (s
);
1489 if (CHECK_FLAG (message
, ZAPI_MESSAGE_TAG
))
1490 rib
->tag
= stream_getl (s
);
1494 if (CHECK_FLAG (message
, ZAPI_MESSAGE_MTU
))
1495 rib
->mtu
= stream_getl (s
);
1500 rib
->vrf_id
= zvrf_id (zvrf
);
1501 rib
->table
= zvrf
->table_id
;
1503 ret
= rib_add_multipath (AFI_IP6
, safi
, &p
, rib
);
1506 client
->v6_route_add_cnt
++;
1508 client
->v6_route_upd8_cnt
++;
1513 /* Zebra server IPv6 prefix delete function. */
1515 zread_ipv6_delete (struct zserv
*client
, u_short length
, struct zebra_vrf
*zvrf
)
1519 struct zapi_ipv6 api
;
1520 struct in6_addr nexthop
;
1521 union g_addr
*pnexthop
= NULL
;
1522 unsigned long ifindex
;
1527 memset (&nexthop
, 0, sizeof (struct in6_addr
));
1529 /* Type, flags, message. */
1530 api
.type
= stream_getc (s
);
1531 api
.instance
= stream_getw (s
);
1532 api
.flags
= stream_getl (s
);
1533 api
.message
= stream_getc (s
);
1534 api
.safi
= stream_getw (s
);
1537 memset (&p
, 0, sizeof (struct prefix_ipv6
));
1538 p
.family
= AF_INET6
;
1539 p
.prefixlen
= stream_getc (s
);
1540 stream_get (&p
.u
.prefix6
, s
, PSIZE (p
.prefixlen
));
1542 /* Nexthop, ifindex, distance, metric. */
1543 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
))
1545 u_char nexthop_type
;
1547 api
.nexthop_num
= stream_getc (s
);
1548 for (i
= 0; i
< api
.nexthop_num
; i
++)
1550 nexthop_type
= stream_getc (s
);
1552 switch (nexthop_type
)
1554 case NEXTHOP_TYPE_IPV6
:
1555 stream_get (&nexthop
, s
, 16);
1556 pnexthop
= (union g_addr
*)&nexthop
;
1558 case NEXTHOP_TYPE_IFINDEX
:
1559 ifindex
= stream_getl (s
);
1566 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_DISTANCE
))
1567 api
.distance
= stream_getc (s
);
1572 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_METRIC
))
1573 api
.metric
= stream_getl (s
);
1578 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_TAG
))
1579 api
.tag
= stream_getl (s
);
1583 if (IN6_IS_ADDR_UNSPECIFIED (&nexthop
))
1584 rib_delete (AFI_IP6
, api
.safi
, zvrf_id (zvrf
), api
.type
, api
.instance
,
1585 api
.flags
, &p
, NULL
, ifindex
, client
->rtm_table
);
1587 rib_delete (AFI_IP6
, api
.safi
, zvrf_id (zvrf
), api
.type
, api
.instance
,
1588 api
.flags
, &p
, pnexthop
, ifindex
, client
->rtm_table
);
1590 client
->v6_route_del_cnt
++;
1594 /* Register zebra server router-id information. Send current router-id */
1596 zread_router_id_add (struct zserv
*client
, u_short length
, struct zebra_vrf
*zvrf
)
1600 /* Router-id information is needed. */
1601 vrf_bitmap_set (client
->ridinfo
, zvrf_id (zvrf
));
1603 router_id_get (&p
, zvrf_id (zvrf
));
1605 return zsend_router_id_update (client
, &p
, zvrf_id (zvrf
));
1608 /* Unregister zebra server router-id information. */
1610 zread_router_id_delete (struct zserv
*client
, u_short length
, struct zebra_vrf
*zvrf
)
1612 vrf_bitmap_unset (client
->ridinfo
, zvrf_id (zvrf
));
1616 /* Tie up route-type and client->sock */
1618 zread_hello (struct zserv
*client
)
1620 /* type of protocol (lib/zebra.h) */
1624 proto
= stream_getc (client
->ibuf
);
1625 instance
= stream_getw (client
->ibuf
);
1627 /* accept only dynamic routing protocols */
1628 if ((proto
< ZEBRA_ROUTE_MAX
)
1629 && (proto
> ZEBRA_ROUTE_STATIC
))
1631 zlog_notice ("client %d says hello and bids fair to announce only %s routes",
1632 client
->sock
, zebra_route_string(proto
));
1634 zlog_notice ("client protocol instance %d", instance
);
1636 client
->proto
= proto
;
1637 client
->instance
= instance
;
1641 /* Unregister all information in a VRF. */
1643 zread_vrf_unregister (struct zserv
*client
, u_short length
, struct zebra_vrf
*zvrf
)
1648 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
1649 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
1650 vrf_bitmap_unset (client
->redist
[afi
][i
], zvrf_id (zvrf
));
1651 vrf_bitmap_unset (client
->redist_default
, zvrf_id (zvrf
));
1652 vrf_bitmap_unset (client
->ifinfo
, zvrf_id (zvrf
));
1653 vrf_bitmap_unset (client
->ridinfo
, zvrf_id (zvrf
));
1659 zread_mpls_labels (int command
, struct zserv
*client
, u_short length
,
1663 enum lsp_types_t type
;
1664 struct prefix prefix
;
1665 enum nexthop_types_t gtype
;
1668 mpls_label_t in_label
, out_label
;
1670 struct zebra_vrf
*zvrf
;
1672 zvrf
= vrf_info_lookup (vrf_id
);
1676 /* Get input stream. */
1680 type
= stream_getc (s
);
1681 prefix
.family
= stream_getl (s
);
1682 switch (prefix
.family
)
1685 prefix
.u
.prefix4
.s_addr
= stream_get_ipv4 (s
);
1686 prefix
.prefixlen
= stream_getc (s
);
1687 gate
.ipv4
.s_addr
= stream_get_ipv4 (s
);
1690 stream_get (&prefix
.u
.prefix6
, s
, 16);
1691 prefix
.prefixlen
= stream_getc (s
);
1692 stream_get (&gate
.ipv6
, s
, 16);
1697 ifindex
= stream_getl (s
);
1698 distance
= stream_getc (s
);
1699 in_label
= stream_getl (s
);
1700 out_label
= stream_getl (s
);
1702 switch (prefix
.family
)
1706 gtype
= NEXTHOP_TYPE_IPV4_IFINDEX
;
1708 gtype
= NEXTHOP_TYPE_IPV4
;
1712 gtype
= NEXTHOP_TYPE_IPV6_IFINDEX
;
1714 gtype
= NEXTHOP_TYPE_IPV6
;
1723 if (command
== ZEBRA_MPLS_LABELS_ADD
)
1725 mpls_lsp_install (zvrf
, type
, in_label
, out_label
, gtype
, &gate
,
1727 if (out_label
!= MPLS_IMP_NULL_LABEL
)
1728 mpls_ftn_update (1, zvrf
, type
, &prefix
, gtype
, &gate
, ifindex
,
1729 distance
, out_label
);
1731 else if (command
== ZEBRA_MPLS_LABELS_DELETE
)
1733 mpls_lsp_uninstall (zvrf
, type
, in_label
, gtype
, &gate
, NULL
, ifindex
);
1734 if (out_label
!= MPLS_IMP_NULL_LABEL
)
1735 mpls_ftn_update (0, zvrf
, type
, &prefix
, gtype
, &gate
, ifindex
,
1736 distance
, out_label
);
1740 /* Cleanup registered nexthops (across VRFs) upon client disconnect. */
1742 zebra_client_close_cleanup_rnh (struct zserv
*client
)
1745 struct zebra_vrf
*zvrf
;
1747 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
)
1749 if ((zvrf
= vrf
->info
) != NULL
)
1751 zebra_cleanup_rnh_client(zvrf_id (zvrf
), AF_INET
, client
, RNH_NEXTHOP_TYPE
);
1752 zebra_cleanup_rnh_client(zvrf_id (zvrf
), AF_INET6
, client
, RNH_NEXTHOP_TYPE
);
1753 zebra_cleanup_rnh_client(zvrf_id (zvrf
), AF_INET
, client
, RNH_IMPORT_CHECK_TYPE
);
1754 zebra_cleanup_rnh_client(zvrf_id (zvrf
), AF_INET6
, client
, RNH_IMPORT_CHECK_TYPE
);
1755 if (client
->proto
== ZEBRA_ROUTE_LDP
)
1757 hash_iterate(zvrf
->lsp_table
, mpls_ldp_lsp_uninstall_all
,
1759 mpls_ldp_ftn_uninstall_all (zvrf
, AFI_IP
);
1760 mpls_ldp_ftn_uninstall_all (zvrf
, AFI_IP6
);
1766 /* Close zebra client. */
1768 zebra_client_close (struct zserv
*client
)
1770 /* Send client de-registration to BFD */
1771 zebra_ptm_bfd_client_deregister(client
->proto
);
1773 /* Cleanup any registered nexthops - across all VRFs. */
1774 zebra_client_close_cleanup_rnh (client
);
1776 /* Close file descriptor. */
1779 unsigned long nroutes
;
1781 close (client
->sock
);
1782 nroutes
= rib_score_proto (client
->proto
, client
->instance
);
1783 zlog_notice ("client %d disconnected. %lu %s routes removed from the rib",
1784 client
->sock
, nroutes
, zebra_route_string (client
->proto
));
1788 /* Free stream buffers. */
1790 stream_free (client
->ibuf
);
1792 stream_free (client
->obuf
);
1794 buffer_free(client
->wb
);
1796 /* Release threads. */
1798 thread_cancel (client
->t_read
);
1799 if (client
->t_write
)
1800 thread_cancel (client
->t_write
);
1801 if (client
->t_suicide
)
1802 thread_cancel (client
->t_suicide
);
1804 /* Free client structure. */
1805 listnode_delete (zebrad
.client_list
, client
);
1806 XFREE (MTYPE_TMP
, client
);
1809 /* Make new client. */
1811 zebra_client_create (int sock
)
1813 struct zserv
*client
;
1817 client
= XCALLOC (MTYPE_TMP
, sizeof (struct zserv
));
1819 /* Make client input/output buffer. */
1820 client
->sock
= sock
;
1821 client
->ibuf
= stream_new (ZEBRA_MAX_PACKET_SIZ
);
1822 client
->obuf
= stream_new (ZEBRA_MAX_PACKET_SIZ
);
1823 client
->wb
= buffer_new(0);
1825 /* Set table number. */
1826 client
->rtm_table
= zebrad
.rtm_table_default
;
1828 client
->connect_time
= quagga_monotime();
1829 /* Initialize flags */
1830 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
1831 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
1832 client
->redist
[afi
][i
] = vrf_bitmap_init ();
1833 client
->redist_default
= vrf_bitmap_init ();
1834 client
->ifinfo
= vrf_bitmap_init ();
1835 client
->ridinfo
= vrf_bitmap_init ();
1837 /* Add this client to linked list. */
1838 listnode_add (zebrad
.client_list
, client
);
1840 /* Make new read thread. */
1841 zebra_event (ZEBRA_READ
, sock
, client
);
1843 zebra_vrf_update_all (client
);
1846 /* Handler of zebra service request. */
1848 zebra_client_read (struct thread
*thread
)
1851 struct zserv
*client
;
1853 uint16_t length
, command
;
1854 uint8_t marker
, version
;
1856 struct zebra_vrf
*zvrf
;
1858 /* Get thread data. Reset reading thread because I'm running. */
1859 sock
= THREAD_FD (thread
);
1860 client
= THREAD_ARG (thread
);
1861 client
->t_read
= NULL
;
1863 if (client
->t_suicide
)
1865 zebra_client_close(client
);
1869 /* Read length and command (if we don't have it already). */
1870 if ((already
= stream_get_endp(client
->ibuf
)) < ZEBRA_HEADER_SIZE
)
1873 if (((nbyte
= stream_read_try (client
->ibuf
, sock
,
1874 ZEBRA_HEADER_SIZE
-already
)) == 0) ||
1877 if (IS_ZEBRA_DEBUG_EVENT
)
1878 zlog_debug ("connection closed socket [%d]", sock
);
1879 zebra_client_close (client
);
1882 if (nbyte
!= (ssize_t
)(ZEBRA_HEADER_SIZE
-already
))
1884 /* Try again later. */
1885 zebra_event (ZEBRA_READ
, sock
, client
);
1888 already
= ZEBRA_HEADER_SIZE
;
1891 /* Reset to read from the beginning of the incoming packet. */
1892 stream_set_getp(client
->ibuf
, 0);
1894 /* Fetch header values */
1895 length
= stream_getw (client
->ibuf
);
1896 marker
= stream_getc (client
->ibuf
);
1897 version
= stream_getc (client
->ibuf
);
1898 vrf_id
= stream_getw (client
->ibuf
);
1899 command
= stream_getw (client
->ibuf
);
1901 if (marker
!= ZEBRA_HEADER_MARKER
|| version
!= ZSERV_VERSION
)
1903 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
1904 __func__
, sock
, marker
, version
);
1905 zebra_client_close (client
);
1908 if (length
< ZEBRA_HEADER_SIZE
)
1910 zlog_warn("%s: socket %d message length %u is less than header size %d",
1911 __func__
, sock
, length
, ZEBRA_HEADER_SIZE
);
1912 zebra_client_close (client
);
1915 if (length
> STREAM_SIZE(client
->ibuf
))
1917 zlog_warn("%s: socket %d message length %u exceeds buffer size %lu",
1918 __func__
, sock
, length
, (u_long
)STREAM_SIZE(client
->ibuf
));
1919 zebra_client_close (client
);
1923 /* Read rest of data. */
1924 if (already
< length
)
1927 if (((nbyte
= stream_read_try (client
->ibuf
, sock
,
1928 length
-already
)) == 0) ||
1931 if (IS_ZEBRA_DEBUG_EVENT
)
1932 zlog_debug ("connection closed [%d] when reading zebra data", sock
);
1933 zebra_client_close (client
);
1936 if (nbyte
!= (ssize_t
)(length
-already
))
1938 /* Try again later. */
1939 zebra_event (ZEBRA_READ
, sock
, client
);
1944 length
-= ZEBRA_HEADER_SIZE
;
1946 /* Debug packet information. */
1947 if (IS_ZEBRA_DEBUG_EVENT
)
1948 zlog_debug ("zebra message comes from socket [%d]", sock
);
1950 if (IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
)
1951 zlog_debug ("zebra message received [%s] %d in VRF %u",
1952 zserv_command_string (command
), length
, vrf_id
);
1954 client
->last_read_time
= quagga_monotime();
1955 client
->last_read_cmd
= command
;
1957 zvrf
= zebra_vrf_lookup_by_id (vrf_id
);
1960 if (IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
)
1961 zlog_debug ("zebra received unknown VRF[%u]", vrf_id
);
1962 goto zclient_read_out
;
1967 case ZEBRA_ROUTER_ID_ADD
:
1968 zread_router_id_add (client
, length
, zvrf
);
1970 case ZEBRA_ROUTER_ID_DELETE
:
1971 zread_router_id_delete (client
, length
, zvrf
);
1973 case ZEBRA_INTERFACE_ADD
:
1974 zread_interface_add (client
, length
, zvrf
);
1976 case ZEBRA_INTERFACE_DELETE
:
1977 zread_interface_delete (client
, length
, zvrf
);
1979 case ZEBRA_IPV4_ROUTE_ADD
:
1980 zread_ipv4_add (client
, length
, zvrf
);
1982 case ZEBRA_IPV4_ROUTE_DELETE
:
1983 zread_ipv4_delete (client
, length
, zvrf
);
1985 case ZEBRA_IPV4_ROUTE_IPV6_NEXTHOP_ADD
:
1986 zread_ipv4_route_ipv6_nexthop_add (client
, length
, zvrf
);
1988 case ZEBRA_IPV4_NEXTHOP_ADD
:
1989 zread_ipv4_add(client
, length
, zvrf
); /* LB: r1.0 merge - id was 1 */
1991 case ZEBRA_IPV4_NEXTHOP_DELETE
:
1992 zread_ipv4_delete(client
, length
, zvrf
); /* LB: r1.0 merge - id was 1 */
1994 case ZEBRA_IPV6_ROUTE_ADD
:
1995 zread_ipv6_add (client
, length
, zvrf
);
1997 case ZEBRA_IPV6_ROUTE_DELETE
:
1998 zread_ipv6_delete (client
, length
, zvrf
);
2000 case ZEBRA_REDISTRIBUTE_ADD
:
2001 zebra_redistribute_add (command
, client
, length
, zvrf
);
2003 case ZEBRA_REDISTRIBUTE_DELETE
:
2004 zebra_redistribute_delete (command
, client
, length
, zvrf
);
2006 case ZEBRA_REDISTRIBUTE_DEFAULT_ADD
:
2007 zebra_redistribute_default_add (command
, client
, length
, zvrf
);
2009 case ZEBRA_REDISTRIBUTE_DEFAULT_DELETE
:
2010 zebra_redistribute_default_delete (command
, client
, length
, zvrf
);
2012 case ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB
:
2013 zread_ipv4_nexthop_lookup_mrib (client
, length
, zvrf
);
2016 zread_hello (client
);
2018 case ZEBRA_NEXTHOP_REGISTER
:
2019 zserv_rnh_register(client
, sock
, length
, RNH_NEXTHOP_TYPE
, zvrf
);
2021 case ZEBRA_NEXTHOP_UNREGISTER
:
2022 zserv_rnh_unregister(client
, sock
, length
, RNH_NEXTHOP_TYPE
, zvrf
);
2024 case ZEBRA_IMPORT_ROUTE_REGISTER
:
2025 zserv_rnh_register(client
, sock
, length
, RNH_IMPORT_CHECK_TYPE
, zvrf
);
2027 case ZEBRA_IMPORT_ROUTE_UNREGISTER
:
2028 zserv_rnh_unregister(client
, sock
, length
, RNH_IMPORT_CHECK_TYPE
, zvrf
);
2030 case ZEBRA_BFD_DEST_UPDATE
:
2031 case ZEBRA_BFD_DEST_REGISTER
:
2032 zebra_ptm_bfd_dst_register(client
, sock
, length
, command
, zvrf
);
2034 case ZEBRA_BFD_DEST_DEREGISTER
:
2035 zebra_ptm_bfd_dst_deregister(client
, sock
, length
, zvrf
);
2037 case ZEBRA_VRF_UNREGISTER
:
2038 zread_vrf_unregister (client
, length
, zvrf
);
2040 case ZEBRA_BFD_CLIENT_REGISTER
:
2041 zebra_ptm_bfd_client_register(client
, sock
, length
);
2043 case ZEBRA_INTERFACE_ENABLE_RADV
:
2044 zebra_interface_radv_set (client
, sock
, length
, zvrf
, 1);
2046 case ZEBRA_INTERFACE_DISABLE_RADV
:
2047 zebra_interface_radv_set (client
, sock
, length
, zvrf
, 0);
2049 case ZEBRA_MPLS_LABELS_ADD
:
2050 case ZEBRA_MPLS_LABELS_DELETE
:
2051 zread_mpls_labels (command
, client
, length
, vrf_id
);
2053 case ZEBRA_IPMR_ROUTE_STATS
:
2054 zebra_ipmr_route_stats (client
, sock
, length
, zvrf
);
2057 zlog_info ("Zebra received unknown command %d", command
);
2061 if (client
->t_suicide
)
2063 /* No need to wait for thread callback, just kill immediately. */
2064 zebra_client_close(client
);
2069 stream_reset (client
->ibuf
);
2070 zebra_event (ZEBRA_READ
, sock
, client
);
2075 /* Accept code of zebra server socket. */
2077 zebra_accept (struct thread
*thread
)
2081 struct sockaddr_in client
;
2084 accept_sock
= THREAD_FD (thread
);
2086 /* Reregister myself. */
2087 zebra_event (ZEBRA_SERV
, accept_sock
, NULL
);
2089 len
= sizeof (struct sockaddr_in
);
2090 client_sock
= accept (accept_sock
, (struct sockaddr
*) &client
, &len
);
2092 if (client_sock
< 0)
2094 zlog_warn ("Can't accept zebra socket: %s", safe_strerror (errno
));
2098 /* Make client socket non-blocking. */
2099 set_nonblocking(client_sock
);
2101 /* Create new zebra client. */
2102 zebra_client_create (client_sock
);
2107 #ifdef HAVE_TCP_ZEBRA
2108 /* Make zebra's server socket. */
2114 struct sockaddr_in addr
;
2116 accept_sock
= socket (AF_INET
, SOCK_STREAM
, 0);
2118 if (accept_sock
< 0)
2120 zlog_warn ("Can't create zserv stream socket: %s",
2121 safe_strerror (errno
));
2122 zlog_warn ("zebra can't provice full functionality due to above error");
2126 memset (&addr
, 0, sizeof (struct sockaddr_in
));
2127 addr
.sin_family
= AF_INET
;
2128 addr
.sin_port
= htons (ZEBRA_PORT
);
2129 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
2130 addr
.sin_len
= sizeof (struct sockaddr_in
);
2131 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
2132 addr
.sin_addr
.s_addr
= htonl (INADDR_LOOPBACK
);
2134 sockopt_reuseaddr (accept_sock
);
2135 sockopt_reuseport (accept_sock
);
2137 if ( zserv_privs
.change(ZPRIVS_RAISE
) )
2138 zlog (NULL
, LOG_ERR
, "Can't raise privileges");
2140 ret
= bind (accept_sock
, (struct sockaddr
*)&addr
,
2141 sizeof (struct sockaddr_in
));
2144 zlog_warn ("Can't bind to stream socket: %s",
2145 safe_strerror (errno
));
2146 zlog_warn ("zebra can't provice full functionality due to above error");
2147 close (accept_sock
); /* Avoid sd leak. */
2151 if ( zserv_privs
.change(ZPRIVS_LOWER
) )
2152 zlog (NULL
, LOG_ERR
, "Can't lower privileges");
2154 ret
= listen (accept_sock
, 1);
2157 zlog_warn ("Can't listen to stream socket: %s",
2158 safe_strerror (errno
));
2159 zlog_warn ("zebra can't provice full functionality due to above error");
2160 close (accept_sock
); /* Avoid sd leak. */
2164 zebra_event (ZEBRA_SERV
, accept_sock
, NULL
);
2166 #else /* HAVE_TCP_ZEBRA */
2168 /* For sockaddr_un. */
2171 /* zebra server UNIX domain socket. */
2173 zebra_serv_un (const char *path
)
2177 struct sockaddr_un serv
;
2180 /* First of all, unlink existing socket */
2184 old_mask
= umask (0077);
2186 /* Make UNIX domain socket. */
2187 sock
= socket (AF_UNIX
, SOCK_STREAM
, 0);
2190 zlog_warn ("Can't create zserv unix socket: %s",
2191 safe_strerror (errno
));
2192 zlog_warn ("zebra can't provide full functionality due to above error");
2196 /* Make server socket. */
2197 memset (&serv
, 0, sizeof (struct sockaddr_un
));
2198 serv
.sun_family
= AF_UNIX
;
2199 strncpy (serv
.sun_path
, path
, strlen (path
));
2200 #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
2201 len
= serv
.sun_len
= SUN_LEN(&serv
);
2203 len
= sizeof (serv
.sun_family
) + strlen (serv
.sun_path
);
2204 #endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
2206 ret
= bind (sock
, (struct sockaddr
*) &serv
, len
);
2209 zlog_warn ("Can't bind to unix socket %s: %s",
2210 path
, safe_strerror (errno
));
2211 zlog_warn ("zebra can't provide full functionality due to above error");
2216 ret
= listen (sock
, 5);
2219 zlog_warn ("Can't listen to unix socket %s: %s",
2220 path
, safe_strerror (errno
));
2221 zlog_warn ("zebra can't provide full functionality due to above error");
2228 zebra_event (ZEBRA_SERV
, sock
, NULL
);
2230 #endif /* HAVE_TCP_ZEBRA */
2234 zebra_event (enum event event
, int sock
, struct zserv
*client
)
2239 thread_add_read (zebrad
.master
, zebra_accept
, client
, sock
);
2243 thread_add_read (zebrad
.master
, zebra_client_read
, client
, sock
);
2251 #define ZEBRA_TIME_BUF 32
2253 zserv_time_buf(time_t *time1
, char *buf
, int buflen
)
2258 assert (buf
!= NULL
);
2259 assert (buflen
>= ZEBRA_TIME_BUF
);
2260 assert (time1
!= NULL
);
2264 snprintf(buf
, buflen
, "never ");
2268 now
= quagga_monotime();
2272 /* Making formatted timer strings. */
2273 #define ONE_DAY_SECOND 60*60*24
2274 #define ONE_WEEK_SECOND 60*60*24*7
2276 if (now
< ONE_DAY_SECOND
)
2277 snprintf (buf
, buflen
, "%02d:%02d:%02d",
2278 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
);
2279 else if (now
< ONE_WEEK_SECOND
)
2280 snprintf (buf
, buflen
, "%dd%02dh%02dm",
2281 tm
->tm_yday
, tm
->tm_hour
, tm
->tm_min
);
2283 snprintf (buf
, buflen
, "%02dw%dd%02dh",
2284 tm
->tm_yday
/7, tm
->tm_yday
- ((tm
->tm_yday
/7) * 7), tm
->tm_hour
);
2289 zebra_show_client_detail (struct vty
*vty
, struct zserv
*client
)
2291 char cbuf
[ZEBRA_TIME_BUF
], rbuf
[ZEBRA_TIME_BUF
];
2292 char wbuf
[ZEBRA_TIME_BUF
], nhbuf
[ZEBRA_TIME_BUF
], mbuf
[ZEBRA_TIME_BUF
];
2294 vty_out (vty
, "Client: %s", zebra_route_string(client
->proto
));
2295 if (client
->instance
)
2296 vty_out (vty
, " Instance: %d", client
->instance
);
2297 vty_out (vty
, "%s", VTY_NEWLINE
);
2299 vty_out (vty
, "------------------------ %s", VTY_NEWLINE
);
2300 vty_out (vty
, "FD: %d %s", client
->sock
, VTY_NEWLINE
);
2301 vty_out (vty
, "Route Table ID: %d %s", client
->rtm_table
, VTY_NEWLINE
);
2303 vty_out (vty
, "Connect Time: %s %s",
2304 zserv_time_buf(&client
->connect_time
, cbuf
, ZEBRA_TIME_BUF
),
2306 if (client
->nh_reg_time
)
2308 vty_out (vty
, "Nexthop Registry Time: %s %s",
2309 zserv_time_buf(&client
->nh_reg_time
, nhbuf
, ZEBRA_TIME_BUF
),
2311 if (client
->nh_last_upd_time
)
2312 vty_out (vty
, "Nexthop Last Update Time: %s %s",
2313 zserv_time_buf(&client
->nh_last_upd_time
, mbuf
, ZEBRA_TIME_BUF
),
2316 vty_out (vty
, "No Nexthop Update sent%s", VTY_NEWLINE
);
2319 vty_out (vty
, "Not registered for Nexthop Updates%s", VTY_NEWLINE
);
2321 vty_out (vty
, "Last Msg Rx Time: %s %s",
2322 zserv_time_buf(&client
->last_read_time
, rbuf
, ZEBRA_TIME_BUF
),
2324 vty_out (vty
, "Last Msg Tx Time: %s %s",
2325 zserv_time_buf(&client
->last_write_time
, wbuf
, ZEBRA_TIME_BUF
),
2327 if (client
->last_read_time
)
2328 vty_out (vty
, "Last Rcvd Cmd: %s %s",
2329 zserv_command_string(client
->last_read_cmd
), VTY_NEWLINE
);
2330 if (client
->last_write_time
)
2331 vty_out (vty
, "Last Sent Cmd: %s %s",
2332 zserv_command_string(client
->last_write_cmd
), VTY_NEWLINE
);
2333 vty_out (vty
, "%s", VTY_NEWLINE
);
2335 vty_out (vty
, "Type Add Update Del %s", VTY_NEWLINE
);
2336 vty_out (vty
, "================================================== %s", VTY_NEWLINE
);
2337 vty_out (vty
, "IPv4 %-12d%-12d%-12d%s", client
->v4_route_add_cnt
,
2338 client
->v4_route_upd8_cnt
, client
->v4_route_del_cnt
, VTY_NEWLINE
);
2339 vty_out (vty
, "IPv6 %-12d%-12d%-12d%s", client
->v6_route_add_cnt
,
2340 client
->v6_route_upd8_cnt
, client
->v6_route_del_cnt
, VTY_NEWLINE
);
2341 vty_out (vty
, "Redist:v4 %-12d%-12d%-12d%s", client
->redist_v4_add_cnt
, 0,
2342 client
->redist_v4_del_cnt
, VTY_NEWLINE
);
2343 vty_out (vty
, "Redist:v6 %-12d%-12d%-12d%s", client
->redist_v6_add_cnt
, 0,
2344 client
->redist_v6_del_cnt
, VTY_NEWLINE
);
2345 vty_out (vty
, "Connected %-12d%-12d%-12d%s", client
->ifadd_cnt
, 0,
2346 client
->ifdel_cnt
, VTY_NEWLINE
);
2347 vty_out (vty
, "BFD peer %-12d%-12d%-12d%s", client
->bfd_peer_add_cnt
,
2348 client
->bfd_peer_upd8_cnt
, client
->bfd_peer_del_cnt
, VTY_NEWLINE
);
2349 vty_out (vty
, "Interface Up Notifications: %d%s", client
->ifup_cnt
,
2351 vty_out (vty
, "Interface Down Notifications: %d%s", client
->ifdown_cnt
,
2354 vty_out (vty
, "%s", VTY_NEWLINE
);
2359 zebra_show_client_brief (struct vty
*vty
, struct zserv
*client
)
2361 char cbuf
[ZEBRA_TIME_BUF
], rbuf
[ZEBRA_TIME_BUF
];
2362 char wbuf
[ZEBRA_TIME_BUF
];
2364 vty_out (vty
, "%-8s%12s %12s%12s%8d/%-8d%8d/%-8d%s",
2365 zebra_route_string(client
->proto
),
2366 zserv_time_buf(&client
->connect_time
, cbuf
, ZEBRA_TIME_BUF
),
2367 zserv_time_buf(&client
->last_read_time
, rbuf
, ZEBRA_TIME_BUF
),
2368 zserv_time_buf(&client
->last_write_time
, wbuf
, ZEBRA_TIME_BUF
),
2369 client
->v4_route_add_cnt
+client
->v4_route_upd8_cnt
,
2370 client
->v4_route_del_cnt
,
2371 client
->v6_route_add_cnt
+client
->v6_route_upd8_cnt
,
2372 client
->v6_route_del_cnt
, VTY_NEWLINE
);
2377 zebra_find_client (u_char proto
)
2379 struct listnode
*node
, *nnode
;
2380 struct zserv
*client
;
2382 for (ALL_LIST_ELEMENTS (zebrad
.client_list
, node
, nnode
, client
))
2384 if (client
->proto
== proto
)
2392 /* Display default rtm_table for all clients. */
2397 "default routing table to use for all clients\n")
2399 vty_out (vty
, "table %d%s", zebrad
.rtm_table_default
,
2404 DEFUN (config_table
,
2407 "Configure target kernel routing table\n"
2410 zebrad
.rtm_table_default
= strtol (argv
[1]->arg
, (char**)0, 10);
2414 DEFUN (no_config_table
,
2415 no_config_table_cmd
,
2416 "no table [TABLENO]",
2418 "Configure target kernel routing table\n"
2421 zebrad
.rtm_table_default
= 0;
2426 DEFUN (ip_forwarding
,
2430 "Turn on IP forwarding")
2436 ret
= ipforward_on ();
2440 vty_out (vty
, "Can't turn on IP forwarding%s", VTY_NEWLINE
);
2447 DEFUN (no_ip_forwarding
,
2448 no_ip_forwarding_cmd
,
2452 "Turn off IP forwarding")
2458 ret
= ipforward_off ();
2462 vty_out (vty
, "Can't turn off IP forwarding%s", VTY_NEWLINE
);
2469 /* This command is for debugging purpose. */
2470 DEFUN (show_zebra_client
,
2471 show_zebra_client_cmd
,
2472 "show zebra client",
2474 "Zebra information\n"
2475 "Client information")
2477 struct listnode
*node
;
2478 struct zserv
*client
;
2480 for (ALL_LIST_ELEMENTS_RO (zebrad
.client_list
, node
, client
))
2481 zebra_show_client_detail(vty
, client
);
2486 /* This command is for debugging purpose. */
2487 DEFUN (show_zebra_client_summary
,
2488 show_zebra_client_summary_cmd
,
2489 "show zebra client summary",
2491 "Zebra information brief"
2492 "Client information brief")
2494 struct listnode
*node
;
2495 struct zserv
*client
;
2497 vty_out (vty
, "Name Connect Time Last Read Last Write IPv4 Routes IPv6 Routes %s",
2499 vty_out (vty
,"--------------------------------------------------------------------------------%s",
2502 for (ALL_LIST_ELEMENTS_RO (zebrad
.client_list
, node
, client
))
2503 zebra_show_client_brief(vty
, client
);
2505 vty_out (vty
, "Routes column shows (added+updated)/deleted%s", VTY_NEWLINE
);
2509 /* Table configuration write function. */
2511 config_write_table (struct vty
*vty
)
2513 if (zebrad
.rtm_table_default
)
2514 vty_out (vty
, "table %d%s", zebrad
.rtm_table_default
,
2519 /* table node for routing tables. */
2520 static struct cmd_node table_node
=
2523 "", /* This node has no interface. */
2527 /* Only display ip forwarding is enabled or not. */
2528 DEFUN (show_ip_forwarding
,
2529 show_ip_forwarding_cmd
,
2530 "show ip forwarding",
2533 "IP forwarding status\n")
2540 vty_out (vty
, "IP forwarding is off%s", VTY_NEWLINE
);
2542 vty_out (vty
, "IP forwarding is on%s", VTY_NEWLINE
);
2546 /* Only display ipv6 forwarding is enabled or not. */
2547 DEFUN (show_ipv6_forwarding
,
2548 show_ipv6_forwarding_cmd
,
2549 "show ipv6 forwarding",
2551 "IPv6 information\n"
2552 "Forwarding status\n")
2556 ret
= ipforward_ipv6 ();
2561 vty_out (vty
, "ipv6 forwarding is unknown%s", VTY_NEWLINE
);
2564 vty_out (vty
, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE
);
2567 vty_out (vty
, "ipv6 forwarding is %s%s", "on", VTY_NEWLINE
);
2570 vty_out (vty
, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE
);
2576 DEFUN (ipv6_forwarding
,
2577 ipv6_forwarding_cmd
,
2580 "Turn on IPv6 forwarding")
2584 ret
= ipforward_ipv6 ();
2586 ret
= ipforward_ipv6_on ();
2590 vty_out (vty
, "Can't turn on IPv6 forwarding%s", VTY_NEWLINE
);
2597 DEFUN (no_ipv6_forwarding
,
2598 no_ipv6_forwarding_cmd
,
2599 "no ipv6 forwarding",
2602 "Turn off IPv6 forwarding")
2606 ret
= ipforward_ipv6 ();
2608 ret
= ipforward_ipv6_off ();
2612 vty_out (vty
, "Can't turn off IPv6 forwarding%s", VTY_NEWLINE
);
2619 /* IPForwarding configuration write function. */
2621 config_write_forwarding (struct vty
*vty
)
2623 /* FIXME: Find better place for that. */
2624 router_id_write (vty
);
2627 vty_out (vty
, "no ip forwarding%s", VTY_NEWLINE
);
2628 if (!ipforward_ipv6 ())
2629 vty_out (vty
, "no ipv6 forwarding%s", VTY_NEWLINE
);
2630 vty_out (vty
, "!%s", VTY_NEWLINE
);
2634 /* table node for routing tables. */
2635 static struct cmd_node forwarding_node
=
2638 "", /* This node has no interface. */
2643 /* function to write the fpm config info */
2645 config_write_fpm (struct vty
*vty
)
2648 fpm_remote_srv_write (vty
);
2652 static struct cmd_node zebra_node
=
2661 /* Initialisation of zebra and installation of commands. */
2665 /* Client list init. */
2666 zebrad
.client_list
= list_new ();
2668 /* Install configuration write function. */
2669 install_node (&table_node
, config_write_table
);
2670 install_node (&forwarding_node
, config_write_forwarding
);
2672 install_node (&zebra_node
, config_write_fpm
);
2675 install_element (VIEW_NODE
, &show_ip_forwarding_cmd
);
2676 install_element (CONFIG_NODE
, &ip_forwarding_cmd
);
2677 install_element (CONFIG_NODE
, &no_ip_forwarding_cmd
);
2678 install_element (ENABLE_NODE
, &show_zebra_client_cmd
);
2679 install_element (ENABLE_NODE
, &show_zebra_client_summary_cmd
);
2682 install_element (VIEW_NODE
, &show_table_cmd
);
2683 install_element (CONFIG_NODE
, &config_table_cmd
);
2684 install_element (CONFIG_NODE
, &no_config_table_cmd
);
2685 #endif /* HAVE_NETLINK */
2687 install_element (VIEW_NODE
, &show_ipv6_forwarding_cmd
);
2688 install_element (CONFIG_NODE
, &ipv6_forwarding_cmd
);
2689 install_element (CONFIG_NODE
, &no_ipv6_forwarding_cmd
);
2692 zebra_route_map_init ();
2695 /* Make zebra server socket, wiping any existing one (see bug #403). */
2697 zebra_zserv_socket_init (char *path
)
2699 #ifdef HAVE_TCP_ZEBRA
2702 zebra_serv_un (path
? path
: ZEBRA_SERV_PATH
);
2703 #endif /* HAVE_TCP_ZEBRA */