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/router-id.h"
40 #include "zebra/redistribute.h"
41 #include "zebra/debug.h"
42 #include "zebra/ipforward.h"
44 /* Event list of zebra. */
45 enum event
{ ZEBRA_SERV
, ZEBRA_READ
, ZEBRA_WRITE
};
47 extern struct zebra_t zebrad
;
49 static void zebra_event (enum event event
, int sock
, struct zserv
*client
);
51 extern struct zebra_privs_t zserv_privs
;
53 /* For logging of zebra meesages. */
54 static const char *zebra_command_str
[] =
57 "ZEBRA_INTERFACE_ADD",
58 "ZEBRA_INTERFACE_DELETE",
59 "ZEBRA_INTERFACE_ADDRESS_ADD",
60 "ZEBRA_INTERFACE_ADDRESS_DELETE",
62 "ZEBRA_INTERFACE_DOWN",
63 "ZEBRA_IPV4_ROUTE_ADD",
64 "ZEBRA_IPV4_ROUTE_DELETE",
65 "ZEBRA_IPV6_ROUTE_ADD",
66 "ZEBRA_IPV6_ROUTE_DELETE",
67 "ZEBRA_REDISTRIBUTE_ADD",
68 "ZEBRA_REDISTRIBUTE_DELETE",
69 "ZEBRA_REDISTRIBUTE_DEFAULT_ADD",
70 "ZEBRA_REDISTRIBUTE_DEFAULT_DELETE",
71 "ZEBRA_IPV4_NEXTHOP_LOOKUP",
72 "ZEBRA_IPV6_NEXTHOP_LOOKUP",
73 "ZEBRA_IPV4_IMPORT_LOOKUP",
74 "ZEBRA_IPV6_IMPORT_LOOKUP",
75 "ZEBRA_ROUTER_ID_ADD",
76 "ZEBRA_ROUTER_ID_DELETE",
77 "ZEBRA_ROUTER_ID_UPDATE"
80 struct zebra_message_queue
82 struct nsm_message_queue
*next
;
83 struct nsm_message_queue
*prev
;
90 struct thread
*t_write
;
91 struct fifo message_queue
;
94 zebra_server_dequeue (struct thread
*t
)
98 struct zebra_message_queue
*queue
;
100 sock
= THREAD_FD (t
);
103 queue
= (struct zebra_message_queue
*) FIFO_HEAD (&message_queue
);
106 nbytes
= write (sock
, queue
->buf
+ queue
->written
,
107 queue
->length
- queue
->written
);
114 else if (nbytes
!= (queue
->length
- queue
->written
))
116 queue
->written
+= nbytes
;
121 XFREE (MTYPE_TMP
, queue
->buf
);
122 XFREE (MTYPE_TMP
, queue
);
126 if (FIFO_TOP (&message_queue
))
127 THREAD_WRITE_ON (zebrad
.master
, t_write
, zebra_server_dequeue
,
133 /* Enqueu message. */
135 zebra_server_enqueue (int sock
, u_char
*buf
, unsigned long length
,
136 unsigned long written
)
138 struct zebra_message_queue
*queue
;
140 queue
= XCALLOC (MTYPE_TMP
, sizeof (struct zebra_message_queue
));
141 queue
->buf
= XMALLOC (MTYPE_TMP
, length
);
142 memcpy (queue
->buf
, buf
, length
);
143 queue
->length
= length
;
144 queue
->written
= written
;
146 FIFO_ADD (&message_queue
, queue
);
148 THREAD_WRITE_ON (zebrad
.master
, t_write
, zebra_server_dequeue
, NULL
, sock
);
152 zebra_server_send_message (int sock
, u_char
*buf
, unsigned long length
)
156 if (FIFO_TOP (&message_queue
))
158 zebra_server_enqueue (sock
, buf
, length
, 0);
163 nbytes
= write (sock
, buf
, length
);
168 zebra_server_enqueue (sock
, buf
, length
, 0);
172 /* It's clear that nbytes is positive at this point. */
173 else if ((unsigned) nbytes
!= length
)
174 zebra_server_enqueue (sock
, buf
, length
, nbytes
);
179 /* Interface is added. Send ZEBRA_INTERFACE_ADD to client. */
181 * This function is called in the following situations:
182 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
184 * - at startup, when zebra figures out the available interfaces
185 * - when an interface is added (where support for
186 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
187 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
191 zsend_interface_add (struct zserv
*client
, struct interface
*ifp
)
195 /* Check this client need interface information. */
196 if (! client
->ifinfo
)
202 /* Place holder for size. */
206 stream_putc (s
, ZEBRA_INTERFACE_ADD
);
208 /* Interface information. */
209 stream_put (s
, ifp
->name
, INTERFACE_NAMSIZ
);
210 stream_putl (s
, ifp
->ifindex
);
211 stream_putc (s
, ifp
->status
);
212 stream_putl (s
, ifp
->flags
);
213 stream_putl (s
, ifp
->metric
);
214 stream_putl (s
, ifp
->mtu
);
215 stream_putl (s
, ifp
->mtu6
);
216 stream_putl (s
, ifp
->bandwidth
);
217 #ifdef HAVE_SOCKADDR_DL
218 stream_put (s
, &ifp
->sdl
, sizeof (ifp
->sdl
));
220 stream_putl (s
, ifp
->hw_addr_len
);
221 if (ifp
->hw_addr_len
)
222 stream_put (s
, ifp
->hw_addr
, ifp
->hw_addr_len
);
223 #endif /* HAVE_SOCKADDR_DL */
225 /* Write packet size. */
226 stream_putw_at (s
, 0, stream_get_endp (s
));
228 zebra_server_send_message (client
->sock
, s
->data
, stream_get_endp (s
));
233 /* Interface deletion from zebra daemon. */
235 * This function is only called when support for
236 * RTM_IFANNOUNCE or AF_NETLINK sockets (RTM_DELLINK message)
237 * is available. It is not called on Solaris.
239 #if (defined(RTM_IFANNOUNCE) || defined(HAVE_NETLINK))
241 zsend_interface_delete (struct zserv
*client
, struct interface
*ifp
)
245 /* Check this client need interface information. */
246 if (! client
->ifinfo
)
252 /* Packet length placeholder. */
255 /* Interface information. */
256 stream_putc (s
, ZEBRA_INTERFACE_DELETE
);
257 stream_put (s
, ifp
->name
, INTERFACE_NAMSIZ
);
258 stream_putl (s
, ifp
->ifindex
);
259 stream_putc (s
, ifp
->status
);
260 stream_putl (s
, ifp
->flags
);
261 stream_putl (s
, ifp
->metric
);
262 stream_putl (s
, ifp
->mtu
);
263 stream_putl (s
, ifp
->mtu6
);
264 stream_putl (s
, ifp
->bandwidth
);
266 /* Write packet length. */
267 stream_putw_at (s
, 0, stream_get_endp (s
));
269 zebra_server_send_message (client
->sock
, s
->data
, stream_get_endp (s
));
273 #endif /* (defined(RTM_IFANNOUNCE) || defined(HAVE_LINUX_RTNETLINK_H)) */
275 /* Interface address is added/deleted. Send ZEBRA_INTERFACE_ADDRESS_ADD or
276 * ZEBRA_INTERFACE_ADDRESS_DELETE to the client.
278 * A ZEBRA_INTERFACE_ADDRESS_ADD is sent in the following situations:
279 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
280 * from the client, after the ZEBRA_INTERFACE_ADD has been
281 * sent from zebra to the client
282 * - redistribute new address info to all clients in the following situations
283 * - at startup, when zebra figures out the available interfaces
284 * - when an interface is added (where support for
285 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
286 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
288 * - for the vty commands "ip address A.B.C.D/M [<secondary>|<label LINE>]"
289 * and "no bandwidth <1-10000000>", "ipv6 address X:X::X:X/M"
290 * - when an RTM_NEWADDR message is received from the kernel,
292 * The call tree that triggers ZEBRA_INTERFACE_ADDRESS_DELETE:
294 * zsend_interface_address(DELETE)
297 * zebra_interface_address_delete_update
299 * | | if_delete_update (not called on
301 * ip_address_uninstall connected_delete_ipv4
302 * [ipv6_addresss_uninstall] [connected_delete_ipv6]
305 * | RTM_NEWADDR on routing/netlink socket
308 * "no ip address A.B.C.D/M [label LINE]"
309 * "no ip address A.B.C.D/M secondary"
310 * ["no ipv6 address X:X::X:X/M"]
314 zsend_interface_address (int cmd
, struct zserv
*client
,
315 struct interface
*ifp
, struct connected
*ifc
)
321 /* Check this client need interface information. */
322 if (! client
->ifinfo
)
328 /* Place holder for size. */
331 stream_putc (s
, cmd
);
332 stream_putl (s
, ifp
->ifindex
);
334 /* Interface address flag. */
335 stream_putc (s
, ifc
->flags
);
337 /* Prefix information. */
339 stream_putc (s
, p
->family
);
340 blen
= prefix_blen (p
);
341 stream_put (s
, &p
->u
.prefix
, blen
);
344 * XXX gnu version does not send prefixlen for ZEBRA_INTERFACE_ADDRESS_DELETE
345 * but zebra_interface_address_delete_read() in the gnu version
348 stream_putc (s
, p
->prefixlen
);
351 p
= ifc
->destination
;
353 stream_put (s
, &p
->u
.prefix
, blen
);
355 stream_put (s
, NULL
, blen
);
357 /* Write packet size. */
358 stream_putw_at (s
, 0, stream_get_endp (s
));
360 zebra_server_send_message (client
->sock
, s
->data
, stream_get_endp (s
));
366 * The cmd passed to zsend_interface_update may be ZEBRA_INTERFACE_UP or
367 * ZEBRA_INTERFACE_DOWN.
369 * The ZEBRA_INTERFACE_UP message is sent from the zebra server to
370 * the clients in one of 2 situations:
371 * - an if_up is detected e.g., as a result of an RTM_IFINFO message
372 * - a vty command modifying the bandwidth of an interface is received.
373 * The ZEBRA_INTERFACE_DOWN message is sent when an if_down is detected.
376 zsend_interface_update (int cmd
, struct zserv
*client
, struct interface
*ifp
)
380 /* Check this client need interface information. */
381 if (! client
->ifinfo
)
387 /* Place holder for size. */
391 stream_putc (s
, cmd
);
393 /* Interface information. */
394 stream_put (s
, ifp
->name
, INTERFACE_NAMSIZ
);
395 stream_putl (s
, ifp
->ifindex
);
396 stream_putc (s
, ifp
->status
);
397 stream_putl (s
, ifp
->flags
);
398 stream_putl (s
, ifp
->metric
);
399 stream_putl (s
, ifp
->mtu
);
400 stream_putl (s
, ifp
->mtu6
);
401 stream_putl (s
, ifp
->bandwidth
);
403 /* Write packet size. */
404 stream_putw_at (s
, 0, stream_get_endp (s
));
406 zebra_server_send_message (client
->sock
, s
->data
, stream_get_endp (s
));
412 * The zebra server sends the clients a ZEBRA_IPV4_ROUTE_ADD or a
413 * ZEBRA_IPV6_ROUTE_ADD via zsend_route_multipath in the following
415 * - when the client starts up, and requests default information
416 * by sending a ZEBRA_REDISTRIBUTE_DEFAULT_ADD to the zebra server, in the
417 * - case of rip, ripngd, ospfd and ospf6d, when the client sends a
418 * ZEBRA_REDISTRIBUTE_ADD as a result of the "redistribute" vty cmd,
419 * - when the zebra server redistributes routes after it updates its rib
421 * The zebra server sends clients a ZEBRA_IPV4_ROUTE_DELETE or a
422 * ZEBRA_IPV6_ROUTE_DELETE via zsend_route_multipath when:
423 * - a "ip route" or "ipv6 route" vty command is issued, a prefix is
424 * - deleted from zebra's rib, and this info
425 * has to be redistributed to the clients
427 * XXX The ZEBRA_IPV*_ROUTE_ADD message is also sent by the client to the
428 * zebra server when the client wants to tell the zebra server to add a
429 * route to the kernel (zapi_ipv4_add etc. ). Since it's essentially the
430 * same message being sent back and forth, this function and
431 * zapi_ipv{4,6}_{add, delete} should be re-written to avoid code
435 zsend_route_multipath (int cmd
, struct zserv
*client
, struct prefix
*p
,
440 struct nexthop
*nexthop
;
441 unsigned long nhnummark
= 0;
443 u_char zapi_flags
= ZAPI_MESSAGE_NEXTHOP
| ZAPI_MESSAGE_IFINDEX
;
448 /* Place holder for size. */
451 /* Put command, type and nexthop. */
452 stream_putc (s
, cmd
);
453 stream_putc (s
, rib
->type
);
454 stream_putc (s
, rib
->flags
);
457 * XXX no need to set ZAPI_MESSAGE_NEXTHOP if we are going to
458 * send empty nexthop?
460 if (cmd
== ZEBRA_IPV4_ROUTE_ADD
|| ZEBRA_IPV6_ROUTE_ADD
)
461 zapi_flags
|= ZAPI_MESSAGE_METRIC
;
463 stream_putc (s
, zapi_flags
);
466 psize
= PSIZE (p
->prefixlen
);
467 stream_putc (s
, p
->prefixlen
);
468 stream_write (s
, (u_char
*) & p
->u
.prefix
, psize
);
471 * XXX The message format sent by zebra below does not match the format
472 * of the corresponding message expected by the zebra server
473 * itself (e.g., see zread_ipv4_add). The nexthop_num is not set correctly,
474 * (is there a bug on the client side if more than one segment is sent?)
475 * nexthop ZEBRA_NEXTHOP_IPV4 is never set, ZEBRA_NEXTHOP_IFINDEX
479 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
481 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
483 nhnummark
= stream_get_putp (s
);
484 stream_putc (s
, 1); /* placeholder */
487 switch(nexthop
->type
)
489 case NEXTHOP_TYPE_IPV4
:
490 case NEXTHOP_TYPE_IPV4_IFINDEX
:
491 stream_put_in_addr (s
, &nexthop
->gate
.ipv4
);
494 case NEXTHOP_TYPE_IPV6
:
495 case NEXTHOP_TYPE_IPV6_IFINDEX
:
496 case NEXTHOP_TYPE_IPV6_IFNAME
:
497 stream_write (s
, (u_char
*) &nexthop
->gate
.ipv6
, 16);
501 if (cmd
== ZEBRA_IPV4_ROUTE_ADD
502 || cmd
== ZEBRA_IPV4_ROUTE_DELETE
)
504 struct in_addr empty
;
505 memset (&empty
, 0, sizeof (struct in_addr
));
506 stream_write (s
, (u_char
*) &empty
, IPV4_MAX_BYTELEN
);
510 struct in6_addr empty
;
511 memset (&empty
, 0, sizeof (struct in6_addr
));
512 stream_write (s
, (u_char
*) &empty
, IPV6_MAX_BYTELEN
);
516 /* Interface index. */
518 stream_putl (s
, nexthop
->ifindex
);
525 stream_putl (s
, rib
->metric
);
527 /* Write next-hop number */
529 stream_putw_at (s
, nhnummark
, nhnum
);
531 /* Write packet size. */
532 stream_putw_at (s
, 0, stream_get_endp (s
));
534 zebra_server_send_message (client
->sock
, s
->data
, stream_get_endp (s
));
541 zsend_ipv6_nexthop_lookup (struct zserv
*client
, struct in6_addr
*addr
)
547 struct nexthop
*nexthop
;
549 /* Lookup nexthop. */
550 rib
= rib_match_ipv6 (addr
);
552 /* Get output stream. */
556 /* Fill in result. */
558 stream_putc (s
, ZEBRA_IPV6_NEXTHOP_LOOKUP
);
559 stream_put (s
, &addr
, 16);
563 stream_putl (s
, rib
->metric
);
567 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
568 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
570 stream_putc (s
, nexthop
->type
);
571 switch (nexthop
->type
)
573 case ZEBRA_NEXTHOP_IPV6
:
574 stream_put (s
, &nexthop
->gate
.ipv6
, 16);
576 case ZEBRA_NEXTHOP_IPV6_IFINDEX
:
577 case ZEBRA_NEXTHOP_IPV6_IFNAME
:
578 stream_put (s
, &nexthop
->gate
.ipv6
, 16);
579 stream_putl (s
, nexthop
->ifindex
);
581 case ZEBRA_NEXTHOP_IFINDEX
:
582 case ZEBRA_NEXTHOP_IFNAME
:
583 stream_putl (s
, nexthop
->ifindex
);
591 stream_putc_at (s
, nump
, num
);
599 stream_putw_at (s
, 0, stream_get_endp (s
));
601 zebra_server_send_message (client
->sock
, s
->data
, stream_get_endp (s
));
605 #endif /* HAVE_IPV6 */
608 zsend_ipv4_nexthop_lookup (struct zserv
*client
, struct in_addr addr
)
614 struct nexthop
*nexthop
;
616 /* Lookup nexthop. */
617 rib
= rib_match_ipv4 (addr
);
619 /* Get output stream. */
623 /* Fill in result. */
625 stream_putc (s
, ZEBRA_IPV4_NEXTHOP_LOOKUP
);
626 stream_put_in_addr (s
, &addr
);
630 stream_putl (s
, rib
->metric
);
634 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
635 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
637 stream_putc (s
, nexthop
->type
);
638 switch (nexthop
->type
)
640 case ZEBRA_NEXTHOP_IPV4
:
641 stream_put_in_addr (s
, &nexthop
->gate
.ipv4
);
643 case ZEBRA_NEXTHOP_IFINDEX
:
644 case ZEBRA_NEXTHOP_IFNAME
:
645 stream_putl (s
, nexthop
->ifindex
);
653 stream_putc_at (s
, nump
, num
);
661 stream_putw_at (s
, 0, stream_get_endp (s
));
663 zebra_server_send_message (client
->sock
, s
->data
, stream_get_endp (s
));
669 zsend_ipv4_import_lookup (struct zserv
*client
, struct prefix_ipv4
*p
)
675 struct nexthop
*nexthop
;
677 /* Lookup nexthop. */
678 rib
= rib_lookup_ipv4 (p
);
680 /* Get output stream. */
684 /* Fill in result. */
686 stream_putc (s
, ZEBRA_IPV4_IMPORT_LOOKUP
);
687 stream_put_in_addr (s
, &p
->prefix
);
691 stream_putl (s
, rib
->metric
);
695 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
696 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
698 stream_putc (s
, nexthop
->type
);
699 switch (nexthop
->type
)
701 case ZEBRA_NEXTHOP_IPV4
:
702 stream_put_in_addr (s
, &nexthop
->gate
.ipv4
);
704 case ZEBRA_NEXTHOP_IFINDEX
:
705 case ZEBRA_NEXTHOP_IFNAME
:
706 stream_putl (s
, nexthop
->ifindex
);
714 stream_putc_at (s
, nump
, num
);
722 stream_putw_at (s
, 0, stream_get_endp (s
));
724 zebra_server_send_message (client
->sock
, s
->data
, stream_get_endp (s
));
729 /* Router-id is updated. Send ZEBRA_ROUTER_ID_ADD to client. */
731 zsend_router_id_update (struct zserv
*client
, struct prefix
*p
)
736 /* Check this client need interface information. */
737 if (!client
->ridinfo
)
743 /* Place holder for size. */
747 stream_putc (s
, ZEBRA_ROUTER_ID_UPDATE
);
749 /* Prefix information. */
750 stream_putc (s
, p
->family
);
751 blen
= prefix_blen (p
);
752 stream_put (s
, &p
->u
.prefix
, blen
);
753 stream_putc (s
, p
->prefixlen
);
755 /* Write packet size. */
756 stream_putw_at (s
, 0, stream_get_endp (s
));
758 return writen (client
->sock
, s
->data
, stream_get_endp (s
));
761 /* Register zebra server interface information. Send current all
762 interface and address information. */
764 zread_interface_add (struct zserv
*client
, u_short length
)
766 struct listnode
*ifnode
;
767 struct listnode
*cnode
;
768 struct interface
*ifp
;
771 /* Interface information is needed. */
774 for (ifnode
= listhead (iflist
); ifnode
; ifnode
= nextnode (ifnode
))
776 ifp
= getdata (ifnode
);
778 /* Skip pseudo interface. */
779 if (! CHECK_FLAG (ifp
->status
, ZEBRA_INTERFACE_ACTIVE
))
782 zsend_interface_add (client
, ifp
);
784 for (cnode
= listhead (ifp
->connected
); cnode
; nextnode (cnode
))
787 if (CHECK_FLAG (c
->conf
, ZEBRA_IFC_REAL
))
788 zsend_interface_address (ZEBRA_INTERFACE_ADDRESS_ADD
, client
,
794 /* Unregister zebra server interface information. */
796 zread_interface_delete (struct zserv
*client
, u_short length
)
801 /* This function support multiple nexthop. */
803 * Parse the ZEBRA_IPV4_ROUTE_ADD sent from client. Update rib and
807 zread_ipv4_add (struct zserv
*client
, u_short length
)
811 struct prefix_ipv4 p
;
813 struct in_addr nexthop
;
817 unsigned int ifindex
;
820 /* Get input stream. */
823 /* Allocate new rib. */
824 rib
= XMALLOC (MTYPE_RIB
, sizeof (struct rib
));
825 memset (rib
, 0, sizeof (struct rib
));
827 /* Type, flags, message. */
828 rib
->type
= stream_getc (s
);
829 rib
->flags
= stream_getc (s
);
830 message
= stream_getc (s
);
831 rib
->uptime
= time (NULL
);
834 memset (&p
, 0, sizeof (struct prefix_ipv4
));
836 p
.prefixlen
= stream_getc (s
);
837 stream_get (&p
.prefix
, s
, PSIZE (p
.prefixlen
));
840 if (CHECK_FLAG (message
, ZAPI_MESSAGE_NEXTHOP
))
842 nexthop_num
= stream_getc (s
);
844 for (i
= 0; i
< nexthop_num
; i
++)
846 nexthop_type
= stream_getc (s
);
848 switch (nexthop_type
)
850 case ZEBRA_NEXTHOP_IFINDEX
:
851 ifindex
= stream_getl (s
);
852 nexthop_ifindex_add (rib
, ifindex
);
854 case ZEBRA_NEXTHOP_IFNAME
:
855 ifname_len
= stream_getc (s
);
856 stream_forward (s
, ifname_len
);
858 case ZEBRA_NEXTHOP_IPV4
:
859 nexthop
.s_addr
= stream_get_ipv4 (s
);
860 nexthop_ipv4_add (rib
, &nexthop
);
862 case ZEBRA_NEXTHOP_IPV6
:
863 stream_forward (s
, IPV6_MAX_BYTELEN
);
865 case ZEBRA_NEXTHOP_BLACKHOLE
:
866 nexthop_blackhole_add (rib
);
873 if (CHECK_FLAG (message
, ZAPI_MESSAGE_DISTANCE
))
874 rib
->distance
= stream_getc (s
);
877 if (CHECK_FLAG (message
, ZAPI_MESSAGE_METRIC
))
878 rib
->metric
= stream_getl (s
);
880 rib_add_ipv4_multipath (&p
, rib
);
883 /* Zebra server IPv4 prefix delete function. */
885 zread_ipv4_delete (struct zserv
*client
, u_short length
)
889 struct zapi_ipv4 api
;
890 struct in_addr nexthop
;
891 unsigned long ifindex
;
892 struct prefix_ipv4 p
;
901 /* Type, flags, message. */
902 api
.type
= stream_getc (s
);
903 api
.flags
= stream_getc (s
);
904 api
.message
= stream_getc (s
);
907 memset (&p
, 0, sizeof (struct prefix_ipv4
));
909 p
.prefixlen
= stream_getc (s
);
910 stream_get (&p
.prefix
, s
, PSIZE (p
.prefixlen
));
912 /* Nexthop, ifindex, distance, metric. */
913 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
))
915 nexthop_num
= stream_getc (s
);
917 for (i
= 0; i
< nexthop_num
; i
++)
919 nexthop_type
= stream_getc (s
);
921 switch (nexthop_type
)
923 case ZEBRA_NEXTHOP_IFINDEX
:
924 ifindex
= stream_getl (s
);
926 case ZEBRA_NEXTHOP_IFNAME
:
927 ifname_len
= stream_getc (s
);
928 stream_forward (s
, ifname_len
);
930 case ZEBRA_NEXTHOP_IPV4
:
931 nexthop
.s_addr
= stream_get_ipv4 (s
);
933 case ZEBRA_NEXTHOP_IPV6
:
934 stream_forward (s
, IPV6_MAX_BYTELEN
);
941 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_DISTANCE
))
942 api
.distance
= stream_getc (s
);
947 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_METRIC
))
948 api
.metric
= stream_getl (s
);
952 rib_delete_ipv4 (api
.type
, api
.flags
, &p
, &nexthop
, ifindex
,
956 /* Nexthop lookup for IPv4. */
958 zread_ipv4_nexthop_lookup (struct zserv
*client
, u_short length
)
962 addr
.s_addr
= stream_get_ipv4 (client
->ibuf
);
963 zsend_ipv4_nexthop_lookup (client
, addr
);
966 /* Nexthop lookup for IPv4. */
968 zread_ipv4_import_lookup (struct zserv
*client
, u_short length
)
970 struct prefix_ipv4 p
;
973 p
.prefixlen
= stream_getc (client
->ibuf
);
974 p
.prefix
.s_addr
= stream_get_ipv4 (client
->ibuf
);
976 zsend_ipv4_import_lookup (client
, &p
);
980 /* Zebra server IPv6 prefix add function. */
982 zread_ipv6_add (struct zserv
*client
, u_short length
)
986 struct zapi_ipv6 api
;
987 struct in6_addr nexthop
;
988 unsigned long ifindex
;
989 struct prefix_ipv6 p
;
993 memset (&nexthop
, 0, sizeof (struct in6_addr
));
995 /* Type, flags, message. */
996 api
.type
= stream_getc (s
);
997 api
.flags
= stream_getc (s
);
998 api
.message
= stream_getc (s
);
1001 memset (&p
, 0, sizeof (struct prefix_ipv6
));
1002 p
.family
= AF_INET6
;
1003 p
.prefixlen
= stream_getc (s
);
1004 stream_get (&p
.prefix
, s
, PSIZE (p
.prefixlen
));
1006 /* Nexthop, ifindex, distance, metric. */
1007 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
))
1009 u_char nexthop_type
;
1011 api
.nexthop_num
= stream_getc (s
);
1012 for (i
= 0; i
< api
.nexthop_num
; i
++)
1014 nexthop_type
= stream_getc (s
);
1016 switch (nexthop_type
)
1018 case ZEBRA_NEXTHOP_IPV6
:
1019 stream_get (&nexthop
, s
, 16);
1021 case ZEBRA_NEXTHOP_IFINDEX
:
1022 ifindex
= stream_getl (s
);
1028 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_DISTANCE
))
1029 api
.distance
= stream_getc (s
);
1033 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_METRIC
))
1034 api
.metric
= stream_getl (s
);
1038 if (IN6_IS_ADDR_UNSPECIFIED (&nexthop
))
1039 rib_add_ipv6 (api
.type
, api
.flags
, &p
, NULL
, ifindex
, 0);
1041 rib_add_ipv6 (api
.type
, api
.flags
, &p
, &nexthop
, ifindex
, 0);
1044 /* Zebra server IPv6 prefix delete function. */
1046 zread_ipv6_delete (struct zserv
*client
, u_short length
)
1050 struct zapi_ipv6 api
;
1051 struct in6_addr nexthop
;
1052 unsigned long ifindex
;
1053 struct prefix_ipv6 p
;
1057 memset (&nexthop
, 0, sizeof (struct in6_addr
));
1059 /* Type, flags, message. */
1060 api
.type
= stream_getc (s
);
1061 api
.flags
= stream_getc (s
);
1062 api
.message
= stream_getc (s
);
1065 memset (&p
, 0, sizeof (struct prefix_ipv6
));
1066 p
.family
= AF_INET6
;
1067 p
.prefixlen
= stream_getc (s
);
1068 stream_get (&p
.prefix
, s
, PSIZE (p
.prefixlen
));
1070 /* Nexthop, ifindex, distance, metric. */
1071 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
))
1073 u_char nexthop_type
;
1075 api
.nexthop_num
= stream_getc (s
);
1076 for (i
= 0; i
< api
.nexthop_num
; i
++)
1078 nexthop_type
= stream_getc (s
);
1080 switch (nexthop_type
)
1082 case ZEBRA_NEXTHOP_IPV6
:
1083 stream_get (&nexthop
, s
, 16);
1085 case ZEBRA_NEXTHOP_IFINDEX
:
1086 ifindex
= stream_getl (s
);
1092 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_DISTANCE
))
1093 api
.distance
= stream_getc (s
);
1096 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_METRIC
))
1097 api
.metric
= stream_getl (s
);
1101 if (IN6_IS_ADDR_UNSPECIFIED (&nexthop
))
1102 rib_delete_ipv6 (api
.type
, api
.flags
, &p
, NULL
, ifindex
, 0);
1104 rib_delete_ipv6 (api
.type
, api
.flags
, &p
, &nexthop
, ifindex
, 0);
1108 zebra_read_ipv6 (int command
, struct zserv
*client
, u_short length
)
1112 struct in6_addr nexthop
, *gate
;
1115 unsigned int ifindex
;
1117 pnt
= stream_pnt (client
->ibuf
);
1120 type
= stream_getc (client
->ibuf
);
1121 flags
= stream_getc (client
->ibuf
);
1122 stream_get (&nexthop
, client
->ibuf
, sizeof (struct in6_addr
));
1124 while (stream_pnt (client
->ibuf
) < lim
)
1127 struct prefix_ipv6 p
;
1129 ifindex
= stream_getl (client
->ibuf
);
1131 memset (&p
, 0, sizeof (struct prefix_ipv6
));
1132 p
.family
= AF_INET6
;
1133 p
.prefixlen
= stream_getc (client
->ibuf
);
1134 size
= PSIZE(p
.prefixlen
);
1135 stream_get (&p
.prefix
, client
->ibuf
, size
);
1137 if (IN6_IS_ADDR_UNSPECIFIED (&nexthop
))
1142 if (command
== ZEBRA_IPV6_ROUTE_ADD
)
1143 rib_add_ipv6 (type
, flags
, &p
, gate
, ifindex
, 0);
1145 rib_delete_ipv6 (type
, flags
, &p
, gate
, ifindex
, 0);
1150 zread_ipv6_nexthop_lookup (struct zserv
*client
, u_short length
)
1152 struct in6_addr addr
;
1155 stream_get (&addr
, client
->ibuf
, 16);
1156 printf ("DEBUG %s\n", inet_ntop (AF_INET6
, &addr
, buf
, BUFSIZ
));
1158 zsend_ipv6_nexthop_lookup (client
, &addr
);
1160 #endif /* HAVE_IPV6 */
1162 /* Register zebra server router-id information. Send current router-id */
1164 zread_router_id_add (struct zserv
*client
, u_short length
)
1168 /* Router-id information is needed. */
1169 client
->ridinfo
= 1;
1173 zsend_router_id_update (client
,&p
);
1176 /* Unregister zebra server router-id information. */
1178 zread_router_id_delete (struct zserv
*client
, u_short length
)
1180 client
->ridinfo
= 0;
1183 /* Close zebra client. */
1185 zebra_client_close (struct zserv
*client
)
1187 /* Close file descriptor. */
1190 close (client
->sock
);
1194 /* Free stream buffers. */
1196 stream_free (client
->ibuf
);
1198 stream_free (client
->obuf
);
1200 /* Release threads. */
1202 thread_cancel (client
->t_read
);
1203 if (client
->t_write
)
1204 thread_cancel (client
->t_write
);
1206 /* Free client structure. */
1207 listnode_delete (zebrad
.client_list
, client
);
1211 /* Make new client. */
1213 zebra_client_create (int sock
)
1215 struct zserv
*client
;
1217 client
= XCALLOC (0, sizeof (struct zserv
));
1219 /* Make client input/output buffer. */
1220 client
->sock
= sock
;
1221 client
->ibuf
= stream_new (ZEBRA_MAX_PACKET_SIZ
);
1222 client
->obuf
= stream_new (ZEBRA_MAX_PACKET_SIZ
);
1224 /* Set table number. */
1225 client
->rtm_table
= zebrad
.rtm_table_default
;
1227 /* Add this client to linked list. */
1228 listnode_add (zebrad
.client_list
, client
);
1230 /* Make new read thread. */
1231 zebra_event (ZEBRA_READ
, sock
, client
);
1234 /* Handler of zebra service request. */
1236 zebra_client_read (struct thread
*thread
)
1239 struct zserv
*client
;
1244 /* Get thread data. Reset reading thread because I'm running. */
1245 sock
= THREAD_FD (thread
);
1246 client
= THREAD_ARG (thread
);
1247 client
->t_read
= NULL
;
1249 /* Read length and command. */
1250 nbyte
= stream_read (client
->ibuf
, sock
, 3);
1253 if (IS_ZEBRA_DEBUG_EVENT
)
1254 zlog_info ("connection closed socket [%d]", sock
);
1255 zebra_client_close (client
);
1258 length
= stream_getw (client
->ibuf
);
1259 command
= stream_getc (client
->ibuf
);
1263 if (IS_ZEBRA_DEBUG_EVENT
)
1264 zlog_info ("length %d is less than 3 ", length
);
1265 zebra_client_close (client
);
1271 /* Read rest of data. */
1274 nbyte
= stream_read (client
->ibuf
, sock
, length
);
1277 if (IS_ZEBRA_DEBUG_EVENT
)
1278 zlog_info ("connection closed [%d] when reading zebra data", sock
);
1279 zebra_client_close (client
);
1284 /* Debug packet information. */
1285 if (IS_ZEBRA_DEBUG_EVENT
)
1286 zlog_info ("zebra message comes from socket [%d]", sock
);
1288 if (IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
)
1289 zlog_info ("zebra message received [%s] %d",
1290 zebra_command_str
[command
], length
);
1294 case ZEBRA_ROUTER_ID_ADD
:
1295 zread_router_id_add (client
, length
);
1297 case ZEBRA_ROUTER_ID_DELETE
:
1298 zread_router_id_delete (client
, length
);
1300 case ZEBRA_INTERFACE_ADD
:
1301 zread_interface_add (client
, length
);
1303 case ZEBRA_INTERFACE_DELETE
:
1304 zread_interface_delete (client
, length
);
1306 case ZEBRA_IPV4_ROUTE_ADD
:
1307 zread_ipv4_add (client
, length
);
1309 case ZEBRA_IPV4_ROUTE_DELETE
:
1310 zread_ipv4_delete (client
, length
);
1313 case ZEBRA_IPV6_ROUTE_ADD
:
1314 zread_ipv6_add (client
, length
);
1316 case ZEBRA_IPV6_ROUTE_DELETE
:
1317 zread_ipv6_delete (client
, length
);
1319 #endif /* HAVE_IPV6 */
1320 case ZEBRA_REDISTRIBUTE_ADD
:
1321 zebra_redistribute_add (command
, client
, length
);
1323 case ZEBRA_REDISTRIBUTE_DELETE
:
1324 zebra_redistribute_delete (command
, client
, length
);
1326 case ZEBRA_REDISTRIBUTE_DEFAULT_ADD
:
1327 zebra_redistribute_default_add (command
, client
, length
);
1329 case ZEBRA_REDISTRIBUTE_DEFAULT_DELETE
:
1330 zebra_redistribute_default_delete (command
, client
, length
);
1332 case ZEBRA_IPV4_NEXTHOP_LOOKUP
:
1333 zread_ipv4_nexthop_lookup (client
, length
);
1336 case ZEBRA_IPV6_NEXTHOP_LOOKUP
:
1337 zread_ipv6_nexthop_lookup (client
, length
);
1339 #endif /* HAVE_IPV6 */
1340 case ZEBRA_IPV4_IMPORT_LOOKUP
:
1341 zread_ipv4_import_lookup (client
, length
);
1344 zlog_info ("Zebra received unknown command %d", command
);
1348 stream_reset (client
->ibuf
);
1349 zebra_event (ZEBRA_READ
, sock
, client
);
1355 /* Accept code of zebra server socket. */
1357 zebra_accept (struct thread
*thread
)
1362 struct sockaddr_in client
;
1365 accept_sock
= THREAD_FD (thread
);
1367 len
= sizeof (struct sockaddr_in
);
1368 client_sock
= accept (accept_sock
, (struct sockaddr
*) &client
, &len
);
1370 if (client_sock
< 0)
1372 zlog_warn ("Can't accept zebra socket: %s", strerror (errno
));
1376 /* Make client socket non-blocking. */
1378 val
= fcntl (client_sock
, F_GETFL
, 0);
1379 fcntl (client_sock
, F_SETFL
, (val
| O_NONBLOCK
));
1381 /* Create new zebra client. */
1382 zebra_client_create (client_sock
);
1384 /* Register myself. */
1385 zebra_event (ZEBRA_SERV
, accept_sock
, NULL
);
1390 #ifdef HAVE_TCP_ZEBRA
1391 /* Make zebra's server socket. */
1397 struct sockaddr_in addr
;
1399 accept_sock
= socket (AF_INET
, SOCK_STREAM
, 0);
1401 if (accept_sock
< 0)
1403 zlog_warn ("Can't bind to socket: %s", strerror (errno
));
1404 zlog_warn ("zebra can't provice full functionality due to above error");
1408 memset (&addr
, 0, sizeof (struct sockaddr_in
));
1409 addr
.sin_family
= AF_INET
;
1410 addr
.sin_port
= htons (ZEBRA_PORT
);
1412 addr
.sin_len
= sizeof (struct sockaddr_in
);
1413 #endif /* HAVE_SIN_LEN */
1414 addr
.sin_addr
.s_addr
= htonl (INADDR_LOOPBACK
);
1416 sockopt_reuseaddr (accept_sock
);
1417 sockopt_reuseport (accept_sock
);
1419 if ( zserv_privs
.change(ZPRIVS_RAISE
) )
1420 zlog (NULL
, LOG_ERR
, "Can't raise privileges");
1422 ret
= bind (accept_sock
, (struct sockaddr
*)&addr
,
1423 sizeof (struct sockaddr_in
));
1426 zlog_warn ("Can't bind to socket: %s", strerror (errno
));
1427 zlog_warn ("zebra can't provice full functionality due to above error");
1428 close (accept_sock
); /* Avoid sd leak. */
1432 if ( zserv_privs
.change(ZPRIVS_LOWER
) )
1433 zlog (NULL
, LOG_ERR
, "Can't lower privileges");
1435 ret
= listen (accept_sock
, 1);
1438 zlog_warn ("Can't listen to socket: %s", strerror (errno
));
1439 zlog_warn ("zebra can't provice full functionality due to above error");
1440 close (accept_sock
); /* Avoid sd leak. */
1444 zebra_event (ZEBRA_SERV
, accept_sock
, NULL
);
1446 #endif /* HAVE_TCP_ZEBRA */
1448 /* For sockaddr_un. */
1451 /* zebra server UNIX domain socket. */
1453 zebra_serv_un (const char *path
)
1457 struct sockaddr_un serv
;
1460 /* First of all, unlink existing socket */
1464 old_mask
= umask (0077);
1466 /* Make UNIX domain socket. */
1467 sock
= socket (AF_UNIX
, SOCK_STREAM
, 0);
1474 /* Make server socket. */
1475 memset (&serv
, 0, sizeof (struct sockaddr_un
));
1476 serv
.sun_family
= AF_UNIX
;
1477 strncpy (serv
.sun_path
, path
, strlen (path
));
1479 len
= serv
.sun_len
= SUN_LEN(&serv
);
1481 len
= sizeof (serv
.sun_family
) + strlen (serv
.sun_path
);
1482 #endif /* HAVE_SUN_LEN */
1484 ret
= bind (sock
, (struct sockaddr
*) &serv
, len
);
1492 ret
= listen (sock
, 5);
1502 zebra_event (ZEBRA_SERV
, sock
, NULL
);
1507 zebra_event (enum event event
, int sock
, struct zserv
*client
)
1512 thread_add_read (zebrad
.master
, zebra_accept
, client
, sock
);
1516 thread_add_read (zebrad
.master
, zebra_client_read
, client
, sock
);
1524 /* Display default rtm_table for all clients. */
1529 "default routing table to use for all clients\n")
1531 vty_out (vty
, "table %d%s", zebrad
.rtm_table_default
,
1536 DEFUN (config_table
,
1539 "Configure target kernel routing table\n"
1542 zebrad
.rtm_table_default
= strtol (argv
[0], (char**)0, 10);
1546 DEFUN (ip_forwarding
,
1550 "Turn on IP forwarding")
1556 ret
= ipforward_on ();
1560 vty_out (vty
, "Can't turn on IP forwarding%s", VTY_NEWLINE
);
1567 DEFUN (no_ip_forwarding
,
1568 no_ip_forwarding_cmd
,
1572 "Turn off IP forwarding")
1578 ret
= ipforward_off ();
1582 vty_out (vty
, "Can't turn off IP forwarding%s", VTY_NEWLINE
);
1589 /* This command is for debugging purpose. */
1590 DEFUN (show_zebra_client
,
1591 show_zebra_client_cmd
,
1592 "show zebra client",
1595 "Client information")
1597 struct listnode
*node
;
1598 struct zserv
*client
;
1600 for (node
= listhead (zebrad
.client_list
); node
; nextnode (node
))
1602 client
= getdata (node
);
1603 vty_out (vty
, "Client fd %d%s", client
->sock
, VTY_NEWLINE
);
1608 /* Table configuration write function. */
1610 config_write_table (struct vty
*vty
)
1612 if (zebrad
.rtm_table_default
)
1613 vty_out (vty
, "table %d%s", zebrad
.rtm_table_default
,
1618 /* table node for routing tables. */
1619 struct cmd_node table_node
=
1622 "", /* This node has no interface. */
1626 /* Only display ip forwarding is enabled or not. */
1627 DEFUN (show_ip_forwarding
,
1628 show_ip_forwarding_cmd
,
1629 "show ip forwarding",
1632 "IP forwarding status\n")
1639 vty_out (vty
, "IP forwarding is off%s", VTY_NEWLINE
);
1641 vty_out (vty
, "IP forwarding is on%s", VTY_NEWLINE
);
1646 /* Only display ipv6 forwarding is enabled or not. */
1647 DEFUN (show_ipv6_forwarding
,
1648 show_ipv6_forwarding_cmd
,
1649 "show ipv6 forwarding",
1651 "IPv6 information\n"
1652 "Forwarding status\n")
1656 ret
= ipforward_ipv6 ();
1661 vty_out (vty
, "ipv6 forwarding is unknown%s", VTY_NEWLINE
);
1664 vty_out (vty
, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE
);
1667 vty_out (vty
, "ipv6 forwarding is %s%s", "on", VTY_NEWLINE
);
1670 vty_out (vty
, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE
);
1676 DEFUN (ipv6_forwarding
,
1677 ipv6_forwarding_cmd
,
1680 "Turn on IPv6 forwarding")
1684 ret
= ipforward_ipv6 ();
1686 ret
= ipforward_ipv6_on ();
1690 vty_out (vty
, "Can't turn on IPv6 forwarding%s", VTY_NEWLINE
);
1697 DEFUN (no_ipv6_forwarding
,
1698 no_ipv6_forwarding_cmd
,
1699 "no ipv6 forwarding",
1702 "Turn off IPv6 forwarding")
1706 ret
= ipforward_ipv6 ();
1708 ret
= ipforward_ipv6_off ();
1712 vty_out (vty
, "Can't turn off IPv6 forwarding%s", VTY_NEWLINE
);
1719 #endif /* HAVE_IPV6 */
1721 /* IPForwarding configuration write function. */
1723 config_write_forwarding (struct vty
*vty
)
1725 /* FIXME: Find better place for that. */
1726 router_id_write (vty
);
1729 vty_out (vty
, "ip forwarding%s", VTY_NEWLINE
);
1731 if (ipforward_ipv6 ())
1732 vty_out (vty
, "ipv6 forwarding%s", VTY_NEWLINE
);
1733 #endif /* HAVE_IPV6 */
1734 vty_out (vty
, "!%s", VTY_NEWLINE
);
1738 /* table node for routing tables. */
1739 struct cmd_node forwarding_node
=
1742 "", /* This node has no interface. */
1747 /* Initialisation of zebra and installation of commands. */
1751 /* Client list init. */
1752 zebrad
.client_list
= list_new ();
1754 /* Make zebra server socket. */
1755 #ifdef HAVE_TCP_ZEBRA
1758 zebra_serv_un (ZEBRA_SERV_PATH
);
1759 #endif /* HAVE_TCP_ZEBRA */
1761 /* Install configuration write function. */
1762 install_node (&table_node
, config_write_table
);
1763 install_node (&forwarding_node
, config_write_forwarding
);
1765 install_element (VIEW_NODE
, &show_ip_forwarding_cmd
);
1766 install_element (ENABLE_NODE
, &show_ip_forwarding_cmd
);
1767 install_element (CONFIG_NODE
, &ip_forwarding_cmd
);
1768 install_element (CONFIG_NODE
, &no_ip_forwarding_cmd
);
1769 install_element (ENABLE_NODE
, &show_zebra_client_cmd
);
1772 install_element (VIEW_NODE
, &show_table_cmd
);
1773 install_element (ENABLE_NODE
, &show_table_cmd
);
1774 install_element (CONFIG_NODE
, &config_table_cmd
);
1775 #endif /* HAVE_NETLINK */
1778 install_element (VIEW_NODE
, &show_ipv6_forwarding_cmd
);
1779 install_element (ENABLE_NODE
, &show_ipv6_forwarding_cmd
);
1780 install_element (CONFIG_NODE
, &ipv6_forwarding_cmd
);
1781 install_element (CONFIG_NODE
, &no_ipv6_forwarding_cmd
);
1782 #endif /* HAVE_IPV6 */
1784 FIFO_INIT(&message_queue
);