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.
33 #include "sockunion.h"
38 #include "zebra/zserv.h"
39 #include "zebra/redistribute.h"
40 #include "zebra/debug.h"
41 #include "zebra/ipforward.h"
43 /* Event list of zebra. */
44 enum event
{ ZEBRA_SERV
, ZEBRA_READ
, ZEBRA_WRITE
};
46 extern struct zebra_t zebrad
;
48 static void zebra_event (enum event event
, int sock
, struct zserv
*client
);
50 extern struct zebra_privs_t zserv_privs
;
52 /* For logging of zebra meesages. */
53 static char *zebra_command_str
[] =
56 "ZEBRA_INTERFACE_ADD",
57 "ZEBRA_INTERFACE_DELETE",
58 "ZEBRA_INTERFACE_ADDRESS_ADD",
59 "ZEBRA_INTERFACE_ADDRESS_DELETE",
61 "ZEBRA_INTERFACE_DOWN",
62 "ZEBRA_IPV4_ROUTE_ADD",
63 "ZEBRA_IPV4_ROUTE_DELETE",
64 "ZEBRA_IPV6_ROUTE_ADD",
65 "ZEBRA_IPV6_ROUTE_DELETE",
66 "ZEBRA_REDISTRIBUTE_ADD",
67 "ZEBRA_REDISTRIBUTE_DELETE",
68 "ZEBRA_REDISTRIBUTE_DEFAULT_ADD",
69 "ZEBRA_REDISTRIBUTE_DEFAULT_DELETE",
70 "ZEBRA_IPV4_NEXTHOP_LOOKUP",
71 "ZEBRA_IPV6_NEXTHOP_LOOKUP",
72 "ZEBRA_IPV4_IMPORT_LOOKUP",
73 "ZEBRA_IPV6_IMPORT_LOOKUP"
76 struct zebra_message_queue
78 struct nsm_message_queue
*next
;
79 struct nsm_message_queue
*prev
;
86 struct thread
*t_write
;
87 struct fifo message_queue
;
90 zebra_server_dequeue (struct thread
*t
)
94 struct zebra_message_queue
*queue
;
99 queue
= (struct zebra_message_queue
*) FIFO_HEAD (&message_queue
);
102 nbytes
= write (sock
, queue
->buf
+ queue
->written
,
103 queue
->length
- queue
->written
);
110 else if (nbytes
!= (queue
->length
- queue
->written
))
112 queue
->written
+= nbytes
;
117 XFREE (MTYPE_TMP
, queue
->buf
);
118 XFREE (MTYPE_TMP
, queue
);
122 if (FIFO_TOP (&message_queue
))
123 THREAD_WRITE_ON (zebrad
.master
, t_write
, zebra_server_dequeue
,
129 /* Enqueu message. */
131 zebra_server_enqueue (int sock
, u_char
*buf
, unsigned long length
,
132 unsigned long written
)
134 struct zebra_message_queue
*queue
;
136 queue
= XCALLOC (MTYPE_TMP
, sizeof (struct zebra_message_queue
));
137 queue
->buf
= XMALLOC (MTYPE_TMP
, length
);
138 memcpy (queue
->buf
, buf
, length
);
139 queue
->length
= length
;
140 queue
->written
= written
;
142 FIFO_ADD (&message_queue
, queue
);
144 THREAD_WRITE_ON (zebrad
.master
, t_write
, zebra_server_dequeue
, NULL
, sock
);
148 zebra_server_send_message (int sock
, u_char
*buf
, unsigned long length
)
152 if (FIFO_TOP (&message_queue
))
154 zebra_server_enqueue (sock
, buf
, length
, 0);
159 nbytes
= write (sock
, buf
, length
);
164 zebra_server_enqueue (sock
, buf
, length
, 0);
168 else if (nbytes
!= length
)
169 zebra_server_enqueue (sock
, buf
, length
, nbytes
);
174 /* Interface is added. Send ZEBRA_INTERFACE_ADD to client. */
176 * This function is called in the following situations:
177 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
179 * - at startup, when zebra figures out the available interfaces
180 * - when an interface is added (where support for
181 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
182 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
186 zsend_interface_add (struct zserv
*client
, struct interface
*ifp
)
190 /* Check this client need interface information. */
191 if (! client
->ifinfo
)
197 /* Place holder for size. */
201 stream_putc (s
, ZEBRA_INTERFACE_ADD
);
203 /* Interface information. */
204 stream_put (s
, ifp
->name
, INTERFACE_NAMSIZ
);
205 stream_putl (s
, ifp
->ifindex
);
206 stream_putc (s
, ifp
->status
);
207 stream_putl (s
, ifp
->flags
);
208 stream_putl (s
, ifp
->metric
);
209 stream_putl (s
, ifp
->mtu
);
210 stream_putl (s
, ifp
->mtu6
);
211 stream_putl (s
, ifp
->bandwidth
);
212 #ifdef HAVE_SOCKADDR_DL
213 stream_put (s
, &ifp
->sdl
, sizeof (ifp
->sdl
));
215 stream_putl (s
, ifp
->hw_addr_len
);
216 if (ifp
->hw_addr_len
)
217 stream_put (s
, ifp
->hw_addr
, ifp
->hw_addr_len
);
218 #endif /* HAVE_SOCKADDR_DL */
220 /* Write packet size. */
221 stream_putw_at (s
, 0, stream_get_endp (s
));
223 zebra_server_send_message (client
->sock
, s
->data
, stream_get_endp (s
));
228 /* Interface deletion from zebra daemon. */
230 * This function is only called when support for
231 * RTM_IFANNOUNCE or AF_NETLINK sockets (RTM_DELLINK message)
232 * is available. It is not called on Solaris.
234 #if (defined(RTM_IFANNOUNCE) || defined(HAVE_NETLINK))
236 zsend_interface_delete (struct zserv
*client
, struct interface
*ifp
)
240 /* Check this client need interface information. */
241 if (! client
->ifinfo
)
247 /* Packet length placeholder. */
250 /* Interface information. */
251 stream_putc (s
, ZEBRA_INTERFACE_DELETE
);
252 stream_put (s
, ifp
->name
, INTERFACE_NAMSIZ
);
253 stream_putl (s
, ifp
->ifindex
);
254 stream_putc (s
, ifp
->status
);
255 stream_putl (s
, ifp
->flags
);
256 stream_putl (s
, ifp
->metric
);
257 stream_putl (s
, ifp
->mtu
);
258 stream_putl (s
, ifp
->mtu6
);
259 stream_putl (s
, ifp
->bandwidth
);
261 /* Write packet length. */
262 stream_putw_at (s
, 0, stream_get_endp (s
));
264 zebra_server_send_message (client
->sock
, s
->data
, stream_get_endp (s
));
268 #endif /* (defined(RTM_IFANNOUNCE) || defined(HAVE_LINUX_RTNETLINK_H)) */
270 /* Interface address is added/deleted. Send ZEBRA_INTERFACE_ADDRESS_ADD or
271 * ZEBRA_INTERFACE_ADDRESS_DELETE to the client.
273 * A ZEBRA_INTERFACE_ADDRESS_ADD is sent in the following situations:
274 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
275 * from the client, after the ZEBRA_INTERFACE_ADD has been
276 * sent from zebra to the client
277 * - redistribute new address info to all clients in the following situations
278 * - at startup, when zebra figures out the available interfaces
279 * - when an interface is added (where support for
280 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
281 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
283 * - for the vty commands "ip address A.B.C.D/M [<secondary>|<label LINE>]"
284 * and "no bandwidth <1-10000000>", "ipv6 address X:X::X:X/M"
285 * - when an RTM_NEWADDR message is received from the kernel,
287 * The call tree that triggers ZEBRA_INTERFACE_ADDRESS_DELETE:
289 * zsend_interface_address(DELETE)
292 * zebra_interface_address_delete_update
294 * | | if_delete_update (not called on
296 * ip_address_uninstall connected_delete_ipv4
297 * [ipv6_addresss_uninstall] [connected_delete_ipv6]
300 * | RTM_NEWADDR on routing/netlink socket
303 * "no ip address A.B.C.D/M [label LINE]"
304 * "no ip address A.B.C.D/M secondary"
305 * ["no ipv6 address X:X::X:X/M"]
309 zsend_interface_address (int cmd
, struct zserv
*client
,
310 struct interface
*ifp
, struct connected
*ifc
)
316 /* Check this client need interface information. */
317 if (! client
->ifinfo
)
323 /* Place holder for size. */
326 stream_putc (s
, cmd
);
327 stream_putl (s
, ifp
->ifindex
);
329 /* Interface address flag. */
330 stream_putc (s
, ifc
->flags
);
332 /* Prefix information. */
334 stream_putc (s
, p
->family
);
335 blen
= prefix_blen (p
);
336 stream_put (s
, &p
->u
.prefix
, blen
);
339 * XXX gnu version does not send prefixlen for ZEBRA_INTERFACE_ADDRESS_DELETE
340 * but zebra_interface_address_delete_read() in the gnu version
343 stream_putc (s
, p
->prefixlen
);
346 p
= ifc
->destination
;
348 stream_put (s
, &p
->u
.prefix
, blen
);
350 stream_put (s
, NULL
, blen
);
352 /* Write packet size. */
353 stream_putw_at (s
, 0, stream_get_endp (s
));
355 zebra_server_send_message (client
->sock
, s
->data
, stream_get_endp (s
));
361 * The cmd passed to zsend_interface_update may be ZEBRA_INTERFACE_UP or
362 * ZEBRA_INTERFACE_DOWN.
364 * The ZEBRA_INTERFACE_UP message is sent from the zebra server to
365 * the clients in one of 2 situations:
366 * - an if_up is detected e.g., as a result of an RTM_IFINFO message
367 * - a vty command modifying the bandwidth of an interface is received.
368 * The ZEBRA_INTERFACE_DOWN message is sent when an if_down is detected.
371 zsend_interface_update (int cmd
, struct zserv
*client
, struct interface
*ifp
)
375 /* Check this client need interface information. */
376 if (! client
->ifinfo
)
382 /* Place holder for size. */
386 stream_putc (s
, cmd
);
388 /* Interface information. */
389 stream_put (s
, ifp
->name
, INTERFACE_NAMSIZ
);
390 stream_putl (s
, ifp
->ifindex
);
391 stream_putc (s
, ifp
->status
);
392 stream_putl (s
, ifp
->flags
);
393 stream_putl (s
, ifp
->metric
);
394 stream_putl (s
, ifp
->mtu
);
395 stream_putl (s
, ifp
->mtu6
);
396 stream_putl (s
, ifp
->bandwidth
);
398 /* Write packet size. */
399 stream_putw_at (s
, 0, stream_get_endp (s
));
401 zebra_server_send_message (client
->sock
, s
->data
, stream_get_endp (s
));
407 * The zebra server sends the clients a ZEBRA_IPV4_ROUTE_ADD or a
408 * ZEBRA_IPV6_ROUTE_ADD via zsend_route_multipath in the following
410 * - when the client starts up, and requests default information
411 * by sending a ZEBRA_REDISTRIBUTE_DEFAULT_ADD to the zebra server, in the
412 * - case of rip, ripngd, ospfd and ospf6d, when the client sends a
413 * ZEBRA_REDISTRIBUTE_ADD as a result of the "redistribute" vty cmd,
414 * - when the zebra server redistributes routes after it updates its rib
416 * The zebra server sends clients a ZEBRA_IPV4_ROUTE_DELETE or a
417 * ZEBRA_IPV6_ROUTE_DELETE via zsend_route_multipath when:
418 * - a "ip route" or "ipv6 route" vty command is issued, a prefix is
419 * - deleted from zebra's rib, and this info
420 * has to be redistributed to the clients
422 * XXX The ZEBRA_IPV*_ROUTE_ADD message is also sent by the client to the
423 * zebra server when the client wants to tell the zebra server to add a
424 * route to the kernel (zapi_ipv4_add etc. ). Since it's essentially the
425 * same message being sent back and forth, this function and
426 * zapi_ipv{4,6}_{add, delete} should be re-written to avoid code
430 zsend_route_multipath (int cmd
, struct zserv
*client
, struct prefix
*p
,
435 struct nexthop
*nexthop
;
436 unsigned long nhnummark
= 0;
438 u_char zapi_flags
= ZAPI_MESSAGE_NEXTHOP
| ZAPI_MESSAGE_IFINDEX
;
443 /* Place holder for size. */
446 /* Put command, type and nexthop. */
447 stream_putc (s
, cmd
);
448 stream_putc (s
, rib
->type
);
449 stream_putc (s
, rib
->flags
);
452 * XXX no need to set ZAPI_MESSAGE_NEXTHOP if we are going to
453 * send empty nexthop?
455 if (cmd
== ZEBRA_IPV4_ROUTE_ADD
|| ZEBRA_IPV6_ROUTE_ADD
)
456 zapi_flags
|= ZAPI_MESSAGE_METRIC
;
458 stream_putc (s
, zapi_flags
);
461 psize
= PSIZE (p
->prefixlen
);
462 stream_putc (s
, p
->prefixlen
);
463 stream_write (s
, (u_char
*) & p
->u
.prefix
, psize
);
466 * XXX The message format sent by zebra below does not match the format
467 * of the corresponding message expected by the zebra server
468 * itself (e.g., see zread_ipv4_add). The nexthop_num is not set correctly,
469 * (is there a bug on the client side if more than one segment is sent?)
470 * nexthop ZEBRA_NEXTHOP_IPV4 is never set, ZEBRA_NEXTHOP_IFINDEX
474 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
476 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
478 nhnummark
= stream_get_putp (s
);
479 stream_putc (s
, 1); /* placeholder */
482 switch(nexthop
->type
)
484 case NEXTHOP_TYPE_IPV4
:
485 case NEXTHOP_TYPE_IPV4_IFINDEX
:
486 stream_put_in_addr (s
, &nexthop
->gate
.ipv4
);
489 case NEXTHOP_TYPE_IPV6
:
490 case NEXTHOP_TYPE_IPV6_IFINDEX
:
491 case NEXTHOP_TYPE_IPV6_IFNAME
:
492 stream_write (s
, (u_char
*) &nexthop
->gate
.ipv6
, 16);
496 if (cmd
== ZEBRA_IPV4_ROUTE_ADD
497 || cmd
== ZEBRA_IPV4_ROUTE_DELETE
)
499 struct in_addr empty
;
500 memset (&empty
, 0, sizeof (struct in6_addr
));
501 stream_write (s
, (u_char
*) &empty
, IPV4_MAX_BYTELEN
);
505 struct in6_addr empty
;
506 memset (&empty
, 0, sizeof (struct in6_addr
));
507 stream_write (s
, (u_char
*) &empty
, IPV6_MAX_BYTELEN
);
511 /* Interface index. */
513 stream_putl (s
, nexthop
->ifindex
);
520 stream_putl (s
, rib
->metric
);
522 /* Write next-hop number */
524 stream_putw_at (s
, nhnummark
, nhnum
);
526 /* Write packet size. */
527 stream_putw_at (s
, 0, stream_get_endp (s
));
529 zebra_server_send_message (client
->sock
, s
->data
, stream_get_endp (s
));
536 zsend_ipv6_nexthop_lookup (struct zserv
*client
, struct in6_addr
*addr
)
542 struct nexthop
*nexthop
;
544 /* Lookup nexthop. */
545 rib
= rib_match_ipv6 (addr
);
547 /* Get output stream. */
551 /* Fill in result. */
553 stream_putc (s
, ZEBRA_IPV6_NEXTHOP_LOOKUP
);
554 stream_put (s
, &addr
, 16);
558 stream_putl (s
, rib
->metric
);
562 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
563 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
565 stream_putc (s
, nexthop
->type
);
566 switch (nexthop
->type
)
568 case ZEBRA_NEXTHOP_IPV6
:
569 stream_put (s
, &nexthop
->gate
.ipv6
, 16);
571 case ZEBRA_NEXTHOP_IPV6_IFINDEX
:
572 case ZEBRA_NEXTHOP_IPV6_IFNAME
:
573 stream_put (s
, &nexthop
->gate
.ipv6
, 16);
574 stream_putl (s
, nexthop
->ifindex
);
576 case ZEBRA_NEXTHOP_IFINDEX
:
577 case ZEBRA_NEXTHOP_IFNAME
:
578 stream_putl (s
, nexthop
->ifindex
);
586 stream_putc_at (s
, nump
, num
);
594 stream_putw_at (s
, 0, stream_get_endp (s
));
596 zebra_server_send_message (client
->sock
, s
->data
, stream_get_endp (s
));
600 #endif /* HAVE_IPV6 */
603 zsend_ipv4_nexthop_lookup (struct zserv
*client
, struct in_addr addr
)
609 struct nexthop
*nexthop
;
611 /* Lookup nexthop. */
612 rib
= rib_match_ipv4 (addr
);
614 /* Get output stream. */
618 /* Fill in result. */
620 stream_putc (s
, ZEBRA_IPV4_NEXTHOP_LOOKUP
);
621 stream_put_in_addr (s
, &addr
);
625 stream_putl (s
, rib
->metric
);
629 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
630 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
632 stream_putc (s
, nexthop
->type
);
633 switch (nexthop
->type
)
635 case ZEBRA_NEXTHOP_IPV4
:
636 stream_put_in_addr (s
, &nexthop
->gate
.ipv4
);
638 case ZEBRA_NEXTHOP_IFINDEX
:
639 case ZEBRA_NEXTHOP_IFNAME
:
640 stream_putl (s
, nexthop
->ifindex
);
648 stream_putc_at (s
, nump
, num
);
656 stream_putw_at (s
, 0, stream_get_endp (s
));
658 zebra_server_send_message (client
->sock
, s
->data
, stream_get_endp (s
));
664 zsend_ipv4_import_lookup (struct zserv
*client
, struct prefix_ipv4
*p
)
670 struct nexthop
*nexthop
;
672 /* Lookup nexthop. */
673 rib
= rib_lookup_ipv4 (p
);
675 /* Get output stream. */
679 /* Fill in result. */
681 stream_putc (s
, ZEBRA_IPV4_IMPORT_LOOKUP
);
682 stream_put_in_addr (s
, &p
->prefix
);
686 stream_putl (s
, rib
->metric
);
690 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
691 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
693 stream_putc (s
, nexthop
->type
);
694 switch (nexthop
->type
)
696 case ZEBRA_NEXTHOP_IPV4
:
697 stream_put_in_addr (s
, &nexthop
->gate
.ipv4
);
699 case ZEBRA_NEXTHOP_IFINDEX
:
700 case ZEBRA_NEXTHOP_IFNAME
:
701 stream_putl (s
, nexthop
->ifindex
);
709 stream_putc_at (s
, nump
, num
);
717 stream_putw_at (s
, 0, stream_get_endp (s
));
719 zebra_server_send_message (client
->sock
, s
->data
, stream_get_endp (s
));
724 /* Register zebra server interface information. Send current all
725 interface and address information. */
727 zread_interface_add (struct zserv
*client
, u_short length
)
731 struct interface
*ifp
;
734 /* Interface information is needed. */
737 for (ifnode
= listhead (iflist
); ifnode
; ifnode
= nextnode (ifnode
))
739 ifp
= getdata (ifnode
);
741 /* Skip pseudo interface. */
742 if (! CHECK_FLAG (ifp
->status
, ZEBRA_INTERFACE_ACTIVE
))
745 zsend_interface_add (client
, ifp
);
747 for (cnode
= listhead (ifp
->connected
); cnode
; nextnode (cnode
))
750 if (CHECK_FLAG (c
->conf
, ZEBRA_IFC_REAL
))
751 zsend_interface_address (ZEBRA_INTERFACE_ADDRESS_ADD
, client
,
757 /* Unregister zebra server interface information. */
759 zread_interface_delete (struct zserv
*client
, u_short length
)
764 /* This function support multiple nexthop. */
766 * Parse the ZEBRA_IPV4_ROUTE_ADD sent from client. Update rib and
770 zread_ipv4_add (struct zserv
*client
, u_short length
)
774 struct prefix_ipv4 p
;
776 struct in_addr nexthop
;
780 unsigned int ifindex
;
783 /* Get input stream. */
786 /* Allocate new rib. */
787 rib
= XMALLOC (MTYPE_RIB
, sizeof (struct rib
));
788 memset (rib
, 0, sizeof (struct rib
));
790 /* Type, flags, message. */
791 rib
->type
= stream_getc (s
);
792 rib
->flags
= stream_getc (s
);
793 message
= stream_getc (s
);
794 rib
->uptime
= time (NULL
);
797 memset (&p
, 0, sizeof (struct prefix_ipv4
));
799 p
.prefixlen
= stream_getc (s
);
800 stream_get (&p
.prefix
, s
, PSIZE (p
.prefixlen
));
803 if (CHECK_FLAG (message
, ZAPI_MESSAGE_NEXTHOP
))
805 nexthop_num
= stream_getc (s
);
807 for (i
= 0; i
< nexthop_num
; i
++)
809 nexthop_type
= stream_getc (s
);
811 switch (nexthop_type
)
813 case ZEBRA_NEXTHOP_IFINDEX
:
814 ifindex
= stream_getl (s
);
815 nexthop_ifindex_add (rib
, ifindex
);
817 case ZEBRA_NEXTHOP_IFNAME
:
818 ifname_len
= stream_getc (s
);
819 stream_forward (s
, ifname_len
);
821 case ZEBRA_NEXTHOP_IPV4
:
822 nexthop
.s_addr
= stream_get_ipv4 (s
);
823 nexthop_ipv4_add (rib
, &nexthop
);
825 case ZEBRA_NEXTHOP_IPV6
:
826 stream_forward (s
, IPV6_MAX_BYTELEN
);
828 case ZEBRA_NEXTHOP_BLACKHOLE
:
829 nexthop_blackhole_add (rib
);
836 if (CHECK_FLAG (message
, ZAPI_MESSAGE_DISTANCE
))
837 rib
->distance
= stream_getc (s
);
840 if (CHECK_FLAG (message
, ZAPI_MESSAGE_METRIC
))
841 rib
->metric
= stream_getl (s
);
843 rib_add_ipv4_multipath (&p
, rib
);
846 /* Zebra server IPv4 prefix delete function. */
848 zread_ipv4_delete (struct zserv
*client
, u_short length
)
852 struct zapi_ipv4 api
;
853 struct in_addr nexthop
;
854 unsigned long ifindex
;
855 struct prefix_ipv4 p
;
864 /* Type, flags, message. */
865 api
.type
= stream_getc (s
);
866 api
.flags
= stream_getc (s
);
867 api
.message
= stream_getc (s
);
870 memset (&p
, 0, sizeof (struct prefix_ipv4
));
872 p
.prefixlen
= stream_getc (s
);
873 stream_get (&p
.prefix
, s
, PSIZE (p
.prefixlen
));
875 /* Nexthop, ifindex, distance, metric. */
876 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
))
878 nexthop_num
= stream_getc (s
);
880 for (i
= 0; i
< nexthop_num
; i
++)
882 nexthop_type
= stream_getc (s
);
884 switch (nexthop_type
)
886 case ZEBRA_NEXTHOP_IFINDEX
:
887 ifindex
= stream_getl (s
);
889 case ZEBRA_NEXTHOP_IFNAME
:
890 ifname_len
= stream_getc (s
);
891 stream_forward (s
, ifname_len
);
893 case ZEBRA_NEXTHOP_IPV4
:
894 nexthop
.s_addr
= stream_get_ipv4 (s
);
896 case ZEBRA_NEXTHOP_IPV6
:
897 stream_forward (s
, IPV6_MAX_BYTELEN
);
904 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_DISTANCE
))
905 api
.distance
= stream_getc (s
);
910 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_METRIC
))
911 api
.metric
= stream_getl (s
);
915 rib_delete_ipv4 (api
.type
, api
.flags
, &p
, &nexthop
, ifindex
,
919 /* Nexthop lookup for IPv4. */
921 zread_ipv4_nexthop_lookup (struct zserv
*client
, u_short length
)
925 addr
.s_addr
= stream_get_ipv4 (client
->ibuf
);
926 zsend_ipv4_nexthop_lookup (client
, addr
);
929 /* Nexthop lookup for IPv4. */
931 zread_ipv4_import_lookup (struct zserv
*client
, u_short length
)
933 struct prefix_ipv4 p
;
936 p
.prefixlen
= stream_getc (client
->ibuf
);
937 p
.prefix
.s_addr
= stream_get_ipv4 (client
->ibuf
);
939 zsend_ipv4_import_lookup (client
, &p
);
943 /* Zebra server IPv6 prefix add function. */
945 zread_ipv6_add (struct zserv
*client
, u_short length
)
949 struct zapi_ipv6 api
;
950 struct in6_addr nexthop
;
951 unsigned long ifindex
;
952 struct prefix_ipv6 p
;
956 memset (&nexthop
, 0, sizeof (struct in6_addr
));
958 /* Type, flags, message. */
959 api
.type
= stream_getc (s
);
960 api
.flags
= stream_getc (s
);
961 api
.message
= stream_getc (s
);
964 memset (&p
, 0, sizeof (struct prefix_ipv6
));
966 p
.prefixlen
= stream_getc (s
);
967 stream_get (&p
.prefix
, s
, PSIZE (p
.prefixlen
));
969 /* Nexthop, ifindex, distance, metric. */
970 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
))
974 api
.nexthop_num
= stream_getc (s
);
975 for (i
= 0; i
< api
.nexthop_num
; i
++)
977 nexthop_type
= stream_getc (s
);
979 switch (nexthop_type
)
981 case ZEBRA_NEXTHOP_IPV6
:
982 stream_get (&nexthop
, s
, 16);
984 case ZEBRA_NEXTHOP_IFINDEX
:
985 ifindex
= stream_getl (s
);
991 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_DISTANCE
))
992 api
.distance
= stream_getc (s
);
996 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_METRIC
))
997 api
.metric
= stream_getl (s
);
1001 if (IN6_IS_ADDR_UNSPECIFIED (&nexthop
))
1002 rib_add_ipv6 (api
.type
, api
.flags
, &p
, NULL
, ifindex
, 0);
1004 rib_add_ipv6 (api
.type
, api
.flags
, &p
, &nexthop
, ifindex
, 0);
1007 /* Zebra server IPv6 prefix delete function. */
1009 zread_ipv6_delete (struct zserv
*client
, u_short length
)
1013 struct zapi_ipv6 api
;
1014 struct in6_addr nexthop
;
1015 unsigned long ifindex
;
1016 struct prefix_ipv6 p
;
1020 memset (&nexthop
, 0, sizeof (struct in6_addr
));
1022 /* Type, flags, message. */
1023 api
.type
= stream_getc (s
);
1024 api
.flags
= stream_getc (s
);
1025 api
.message
= stream_getc (s
);
1028 memset (&p
, 0, sizeof (struct prefix_ipv6
));
1029 p
.family
= AF_INET6
;
1030 p
.prefixlen
= stream_getc (s
);
1031 stream_get (&p
.prefix
, s
, PSIZE (p
.prefixlen
));
1033 /* Nexthop, ifindex, distance, metric. */
1034 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
))
1036 u_char nexthop_type
;
1038 api
.nexthop_num
= stream_getc (s
);
1039 for (i
= 0; i
< api
.nexthop_num
; i
++)
1041 nexthop_type
= stream_getc (s
);
1043 switch (nexthop_type
)
1045 case ZEBRA_NEXTHOP_IPV6
:
1046 stream_get (&nexthop
, s
, 16);
1048 case ZEBRA_NEXTHOP_IFINDEX
:
1049 ifindex
= stream_getl (s
);
1055 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_DISTANCE
))
1056 api
.distance
= stream_getc (s
);
1059 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_METRIC
))
1060 api
.metric
= stream_getl (s
);
1064 if (IN6_IS_ADDR_UNSPECIFIED (&nexthop
))
1065 rib_delete_ipv6 (api
.type
, api
.flags
, &p
, NULL
, ifindex
, 0);
1067 rib_delete_ipv6 (api
.type
, api
.flags
, &p
, &nexthop
, ifindex
, 0);
1071 zebra_read_ipv6 (int command
, struct zserv
*client
, u_short length
)
1075 struct in6_addr nexthop
, *gate
;
1078 unsigned int ifindex
;
1080 pnt
= stream_pnt (client
->ibuf
);
1083 type
= stream_getc (client
->ibuf
);
1084 flags
= stream_getc (client
->ibuf
);
1085 stream_get (&nexthop
, client
->ibuf
, sizeof (struct in6_addr
));
1087 while (stream_pnt (client
->ibuf
) < lim
)
1090 struct prefix_ipv6 p
;
1092 ifindex
= stream_getl (client
->ibuf
);
1094 memset (&p
, 0, sizeof (struct prefix_ipv6
));
1095 p
.family
= AF_INET6
;
1096 p
.prefixlen
= stream_getc (client
->ibuf
);
1097 size
= PSIZE(p
.prefixlen
);
1098 stream_get (&p
.prefix
, client
->ibuf
, size
);
1100 if (IN6_IS_ADDR_UNSPECIFIED (&nexthop
))
1105 if (command
== ZEBRA_IPV6_ROUTE_ADD
)
1106 rib_add_ipv6 (type
, flags
, &p
, gate
, ifindex
, 0);
1108 rib_delete_ipv6 (type
, flags
, &p
, gate
, ifindex
, 0);
1113 zread_ipv6_nexthop_lookup (struct zserv
*client
, u_short length
)
1115 struct in6_addr addr
;
1118 stream_get (&addr
, client
->ibuf
, 16);
1119 printf ("DEBUG %s\n", inet_ntop (AF_INET6
, &addr
, buf
, BUFSIZ
));
1121 zsend_ipv6_nexthop_lookup (client
, &addr
);
1123 #endif /* HAVE_IPV6 */
1125 /* Close zebra client. */
1127 zebra_client_close (struct zserv
*client
)
1129 /* Close file descriptor. */
1132 close (client
->sock
);
1136 /* Free stream buffers. */
1138 stream_free (client
->ibuf
);
1140 stream_free (client
->obuf
);
1142 /* Release threads. */
1144 thread_cancel (client
->t_read
);
1145 if (client
->t_write
)
1146 thread_cancel (client
->t_write
);
1148 /* Free client structure. */
1149 listnode_delete (zebrad
.client_list
, client
);
1153 /* Make new client. */
1155 zebra_client_create (int sock
)
1157 struct zserv
*client
;
1159 client
= XCALLOC (0, sizeof (struct zserv
));
1161 /* Make client input/output buffer. */
1162 client
->sock
= sock
;
1163 client
->ibuf
= stream_new (ZEBRA_MAX_PACKET_SIZ
);
1164 client
->obuf
= stream_new (ZEBRA_MAX_PACKET_SIZ
);
1166 /* Set table number. */
1167 client
->rtm_table
= zebrad
.rtm_table_default
;
1169 /* Add this client to linked list. */
1170 listnode_add (zebrad
.client_list
, client
);
1172 /* Make new read thread. */
1173 zebra_event (ZEBRA_READ
, sock
, client
);
1176 /* Handler of zebra service request. */
1178 zebra_client_read (struct thread
*thread
)
1181 struct zserv
*client
;
1186 /* Get thread data. Reset reading thread because I'm running. */
1187 sock
= THREAD_FD (thread
);
1188 client
= THREAD_ARG (thread
);
1189 client
->t_read
= NULL
;
1191 /* Read length and command. */
1192 nbyte
= stream_read (client
->ibuf
, sock
, 3);
1195 if (IS_ZEBRA_DEBUG_EVENT
)
1196 zlog_info ("connection closed socket [%d]", sock
);
1197 zebra_client_close (client
);
1200 length
= stream_getw (client
->ibuf
);
1201 command
= stream_getc (client
->ibuf
);
1205 if (IS_ZEBRA_DEBUG_EVENT
)
1206 zlog_info ("length %d is less than 3 ", length
);
1207 zebra_client_close (client
);
1213 /* Read rest of data. */
1216 nbyte
= stream_read (client
->ibuf
, sock
, length
);
1219 if (IS_ZEBRA_DEBUG_EVENT
)
1220 zlog_info ("connection closed [%d] when reading zebra data", sock
);
1221 zebra_client_close (client
);
1226 /* Debug packet information. */
1227 if (IS_ZEBRA_DEBUG_EVENT
)
1228 zlog_info ("zebra message comes from socket [%d]", sock
);
1230 if (IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
)
1231 zlog_info ("zebra message received [%s] %d",
1232 zebra_command_str
[command
], length
);
1236 case ZEBRA_INTERFACE_ADD
:
1237 zread_interface_add (client
, length
);
1239 case ZEBRA_INTERFACE_DELETE
:
1240 zread_interface_delete (client
, length
);
1242 case ZEBRA_IPV4_ROUTE_ADD
:
1243 zread_ipv4_add (client
, length
);
1245 case ZEBRA_IPV4_ROUTE_DELETE
:
1246 zread_ipv4_delete (client
, length
);
1249 case ZEBRA_IPV6_ROUTE_ADD
:
1250 zread_ipv6_add (client
, length
);
1252 case ZEBRA_IPV6_ROUTE_DELETE
:
1253 zread_ipv6_delete (client
, length
);
1255 #endif /* HAVE_IPV6 */
1256 case ZEBRA_REDISTRIBUTE_ADD
:
1257 zebra_redistribute_add (command
, client
, length
);
1259 case ZEBRA_REDISTRIBUTE_DELETE
:
1260 zebra_redistribute_delete (command
, client
, length
);
1262 case ZEBRA_REDISTRIBUTE_DEFAULT_ADD
:
1263 zebra_redistribute_default_add (command
, client
, length
);
1265 case ZEBRA_REDISTRIBUTE_DEFAULT_DELETE
:
1266 zebra_redistribute_default_delete (command
, client
, length
);
1268 case ZEBRA_IPV4_NEXTHOP_LOOKUP
:
1269 zread_ipv4_nexthop_lookup (client
, length
);
1272 case ZEBRA_IPV6_NEXTHOP_LOOKUP
:
1273 zread_ipv6_nexthop_lookup (client
, length
);
1275 #endif /* HAVE_IPV6 */
1276 case ZEBRA_IPV4_IMPORT_LOOKUP
:
1277 zread_ipv4_import_lookup (client
, length
);
1280 zlog_info ("Zebra received unknown command %d", command
);
1284 stream_reset (client
->ibuf
);
1285 zebra_event (ZEBRA_READ
, sock
, client
);
1291 /* Accept code of zebra server socket. */
1293 zebra_accept (struct thread
*thread
)
1298 struct sockaddr_in client
;
1301 accept_sock
= THREAD_FD (thread
);
1303 len
= sizeof (struct sockaddr_in
);
1304 client_sock
= accept (accept_sock
, (struct sockaddr
*) &client
, &len
);
1306 if (client_sock
< 0)
1308 zlog_warn ("Can't accept zebra socket: %s", strerror (errno
));
1312 /* Make client socket non-blocking. */
1314 val
= fcntl (client_sock
, F_GETFL
, 0);
1315 fcntl (client_sock
, F_SETFL
, (val
| O_NONBLOCK
));
1317 /* Create new zebra client. */
1318 zebra_client_create (client_sock
);
1320 /* Register myself. */
1321 zebra_event (ZEBRA_SERV
, accept_sock
, NULL
);
1326 #ifdef HAVE_TCP_ZEBRA
1327 /* Make zebra's server socket. */
1333 struct sockaddr_in addr
;
1335 accept_sock
= socket (AF_INET
, SOCK_STREAM
, 0);
1337 if (accept_sock
< 0)
1339 zlog_warn ("Can't bind to socket: %s", strerror (errno
));
1340 zlog_warn ("zebra can't provice full functionality due to above error");
1344 memset (&addr
, 0, sizeof (struct sockaddr_in
));
1345 addr
.sin_family
= AF_INET
;
1346 addr
.sin_port
= htons (ZEBRA_PORT
);
1348 addr
.sin_len
= sizeof (struct sockaddr_in
);
1349 #endif /* HAVE_SIN_LEN */
1350 addr
.sin_addr
.s_addr
= htonl (INADDR_LOOPBACK
);
1352 sockopt_reuseaddr (accept_sock
);
1353 sockopt_reuseport (accept_sock
);
1355 if ( zserv_privs
.change(ZPRIVS_RAISE
) )
1356 zlog (NULL
, LOG_ERR
, "Can't raise privileges");
1358 ret
= bind (accept_sock
, (struct sockaddr
*)&addr
,
1359 sizeof (struct sockaddr_in
));
1362 zlog_warn ("Can't bind to socket: %s", strerror (errno
));
1363 zlog_warn ("zebra can't provice full functionality due to above error");
1364 close (accept_sock
); /* Avoid sd leak. */
1368 if ( zserv_privs
.change(ZPRIVS_LOWER
) )
1369 zlog (NULL
, LOG_ERR
, "Can't lower privileges");
1371 ret
= listen (accept_sock
, 1);
1374 zlog_warn ("Can't listen to socket: %s", strerror (errno
));
1375 zlog_warn ("zebra can't provice full functionality due to above error");
1376 close (accept_sock
); /* Avoid sd leak. */
1380 zebra_event (ZEBRA_SERV
, accept_sock
, NULL
);
1382 #endif /* HAVE_TCP_ZEBRA */
1384 /* For sockaddr_un. */
1387 /* zebra server UNIX domain socket. */
1389 zebra_serv_un (char *path
)
1393 struct sockaddr_un serv
;
1396 /* First of all, unlink existing socket */
1400 old_mask
= umask (0077);
1402 /* Make UNIX domain socket. */
1403 sock
= socket (AF_UNIX
, SOCK_STREAM
, 0);
1410 /* Make server socket. */
1411 memset (&serv
, 0, sizeof (struct sockaddr_un
));
1412 serv
.sun_family
= AF_UNIX
;
1413 strncpy (serv
.sun_path
, path
, strlen (path
));
1415 len
= serv
.sun_len
= SUN_LEN(&serv
);
1417 len
= sizeof (serv
.sun_family
) + strlen (serv
.sun_path
);
1418 #endif /* HAVE_SUN_LEN */
1420 ret
= bind (sock
, (struct sockaddr
*) &serv
, len
);
1428 ret
= listen (sock
, 5);
1438 zebra_event (ZEBRA_SERV
, sock
, NULL
);
1443 zebra_event (enum event event
, int sock
, struct zserv
*client
)
1448 thread_add_read (zebrad
.master
, zebra_accept
, client
, sock
);
1452 thread_add_read (zebrad
.master
, zebra_client_read
, client
, sock
);
1460 /* Display default rtm_table for all clients. */
1465 "default routing table to use for all clients\n")
1467 vty_out (vty
, "table %d%s", zebrad
.rtm_table_default
,
1472 DEFUN (config_table
,
1475 "Configure target kernel routing table\n"
1478 zebrad
.rtm_table_default
= strtol (argv
[0], (char**)0, 10);
1482 DEFUN (ip_forwarding
,
1486 "Turn on IP forwarding")
1494 vty_out (vty
, "IP forwarding is already on%s", VTY_NEWLINE
);
1495 return CMD_ERR_NOTHING_TODO
;
1498 ret
= ipforward_on ();
1501 vty_out (vty
, "Can't turn on IP forwarding%s", VTY_NEWLINE
);
1508 DEFUN (no_ip_forwarding
,
1509 no_ip_forwarding_cmd
,
1513 "Turn off IP forwarding")
1521 vty_out (vty
, "IP forwarding is already off%s", VTY_NEWLINE
);
1522 return CMD_ERR_NOTHING_TODO
;
1525 ret
= ipforward_off ();
1528 vty_out (vty
, "Can't turn off IP forwarding%s", VTY_NEWLINE
);
1535 /* This command is for debugging purpose. */
1536 DEFUN (show_zebra_client
,
1537 show_zebra_client_cmd
,
1538 "show zebra client",
1541 "Client information")
1544 struct zserv
*client
;
1546 for (node
= listhead (zebrad
.client_list
); node
; nextnode (node
))
1548 client
= getdata (node
);
1549 vty_out (vty
, "Client fd %d%s", client
->sock
, VTY_NEWLINE
);
1554 /* Table configuration write function. */
1556 config_write_table (struct vty
*vty
)
1558 if (zebrad
.rtm_table_default
)
1559 vty_out (vty
, "table %d%s", zebrad
.rtm_table_default
,
1564 /* table node for routing tables. */
1565 struct cmd_node table_node
=
1568 "", /* This node has no interface. */
1572 /* Only display ip forwarding is enabled or not. */
1573 DEFUN (show_ip_forwarding
,
1574 show_ip_forwarding_cmd
,
1575 "show ip forwarding",
1578 "IP forwarding status\n")
1585 vty_out (vty
, "IP forwarding is off%s", VTY_NEWLINE
);
1587 vty_out (vty
, "IP forwarding is on%s", VTY_NEWLINE
);
1592 /* Only display ipv6 forwarding is enabled or not. */
1593 DEFUN (show_ipv6_forwarding
,
1594 show_ipv6_forwarding_cmd
,
1595 "show ipv6 forwarding",
1597 "IPv6 information\n"
1598 "Forwarding status\n")
1602 ret
= ipforward_ipv6 ();
1607 vty_out (vty
, "ipv6 forwarding is unknown%s", VTY_NEWLINE
);
1610 vty_out (vty
, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE
);
1613 vty_out (vty
, "ipv6 forwarding is %s%s", "on", VTY_NEWLINE
);
1616 vty_out (vty
, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE
);
1622 DEFUN (ipv6_forwarding
,
1623 ipv6_forwarding_cmd
,
1626 "Turn on IPv6 forwarding")
1630 ret
= ipforward_ipv6 ();
1633 vty_out (vty
, "IPv6 forwarding is already on%s", VTY_NEWLINE
);
1634 return CMD_ERR_NOTHING_TODO
;
1637 ret
= ipforward_ipv6_on ();
1640 vty_out (vty
, "Can't turn on IPv6 forwarding%s", VTY_NEWLINE
);
1647 DEFUN (no_ipv6_forwarding
,
1648 no_ipv6_forwarding_cmd
,
1649 "no ipv6 forwarding",
1652 "Turn off IPv6 forwarding")
1656 ret
= ipforward_ipv6 ();
1659 vty_out (vty
, "IP forwarding is already off%s", VTY_NEWLINE
);
1660 return CMD_ERR_NOTHING_TODO
;
1663 ret
= ipforward_ipv6_off ();
1666 vty_out (vty
, "Can't turn off IPv6 forwarding%s", VTY_NEWLINE
);
1673 #endif /* HAVE_IPV6 */
1675 /* IPForwarding configuration write function. */
1677 config_write_forwarding (struct vty
*vty
)
1680 vty_out (vty
, "no ip forwarding%s", VTY_NEWLINE
);
1682 if (! ipforward_ipv6 ())
1683 vty_out (vty
, "no ipv6 forwarding%s", VTY_NEWLINE
);
1684 #endif /* HAVE_IPV6 */
1685 vty_out (vty
, "!%s", VTY_NEWLINE
);
1689 /* table node for routing tables. */
1690 struct cmd_node forwarding_node
=
1693 "", /* This node has no interface. */
1698 /* Initialisation of zebra and installation of commands. */
1702 /* Client list init. */
1703 zebrad
.client_list
= list_new ();
1705 /* Forwarding is on by default. */
1708 ipforward_ipv6_on ();
1709 #endif /* HAVE_IPV6 */
1711 /* Make zebra server socket. */
1712 #ifdef HAVE_TCP_ZEBRA
1715 zebra_serv_un (ZEBRA_SERV_PATH
);
1716 #endif /* HAVE_TCP_ZEBRA */
1718 /* Install configuration write function. */
1719 install_node (&table_node
, config_write_table
);
1720 install_node (&forwarding_node
, config_write_forwarding
);
1722 install_element (VIEW_NODE
, &show_ip_forwarding_cmd
);
1723 install_element (ENABLE_NODE
, &show_ip_forwarding_cmd
);
1724 install_element (CONFIG_NODE
, &ip_forwarding_cmd
);
1725 install_element (CONFIG_NODE
, &no_ip_forwarding_cmd
);
1726 install_element (ENABLE_NODE
, &show_zebra_client_cmd
);
1729 install_element (VIEW_NODE
, &show_table_cmd
);
1730 install_element (ENABLE_NODE
, &show_table_cmd
);
1731 install_element (CONFIG_NODE
, &config_table_cmd
);
1732 #endif /* HAVE_NETLINK */
1735 install_element (VIEW_NODE
, &show_ipv6_forwarding_cmd
);
1736 install_element (ENABLE_NODE
, &show_ipv6_forwarding_cmd
);
1737 install_element (CONFIG_NODE
, &ipv6_forwarding_cmd
);
1738 install_element (CONFIG_NODE
, &no_ipv6_forwarding_cmd
);
1739 #endif /* HAVE_IPV6 */
1741 FIFO_INIT(&message_queue
);