blob: 537b5429a263435e5721f3c13e8d0997d3e0b45d [file] [log] [blame]
paul718e3742002-12-13 20:15:29 +00001/* Zebra daemon server routine.
2 * Copyright (C) 1997, 98, 99 Kunihiro Ishiguro
3 *
4 * This file is part of GNU Zebra.
5 *
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
9 * later version.
10 *
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.
15 *
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.
20 */
21
22#include <zebra.h>
23
24#include "prefix.h"
25#include "command.h"
26#include "if.h"
27#include "thread.h"
28#include "stream.h"
29#include "memory.h"
30#include "table.h"
31#include "rib.h"
32#include "network.h"
33#include "sockunion.h"
34#include "log.h"
35#include "zclient.h"
pauledd7c242003-06-04 13:59:38 +000036#include "privs.h"
paul718e3742002-12-13 20:15:29 +000037
38#include "zebra/zserv.h"
hasso18a6dce2004-10-03 18:18:34 +000039#include "zebra/router-id.h"
paul718e3742002-12-13 20:15:29 +000040#include "zebra/redistribute.h"
41#include "zebra/debug.h"
42#include "zebra/ipforward.h"
43
44/* Event list of zebra. */
45enum event { ZEBRA_SERV, ZEBRA_READ, ZEBRA_WRITE };
46
paulb21b19c2003-06-15 01:28:29 +000047extern struct zebra_t zebrad;
paul718e3742002-12-13 20:15:29 +000048
paulb9df2d22004-05-09 09:09:59 +000049static void zebra_event (enum event event, int sock, struct zserv *client);
paulccf35572003-03-01 11:42:20 +000050
pauledd7c242003-06-04 13:59:38 +000051extern struct zebra_privs_t zserv_privs;
paul718e3742002-12-13 20:15:29 +000052
53/* For logging of zebra meesages. */
paulb9df2d22004-05-09 09:09:59 +000054static char *zebra_command_str [] =
paul718e3742002-12-13 20:15:29 +000055{
56 "NULL",
57 "ZEBRA_INTERFACE_ADD",
58 "ZEBRA_INTERFACE_DELETE",
59 "ZEBRA_INTERFACE_ADDRESS_ADD",
60 "ZEBRA_INTERFACE_ADDRESS_DELETE",
61 "ZEBRA_INTERFACE_UP",
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",
hasso18a6dce2004-10-03 18:18:34 +000074 "ZEBRA_IPV6_IMPORT_LOOKUP",
75 "ZEBRA_ROUTER_ID_ADD",
76 "ZEBRA_ROUTER_ID_DELETE",
77 "ZEBRA_ROUTER_ID_UPDATE"
paul718e3742002-12-13 20:15:29 +000078};
79
paulccf35572003-03-01 11:42:20 +000080struct zebra_message_queue
81{
82 struct nsm_message_queue *next;
83 struct nsm_message_queue *prev;
84
85 u_char *buf;
86 u_int16_t length;
87 u_int16_t written;
88};
89
90struct thread *t_write;
91struct fifo message_queue;
92
93int
94zebra_server_dequeue (struct thread *t)
95{
96 int sock;
97 int nbytes;
98 struct zebra_message_queue *queue;
99
100 sock = THREAD_FD (t);
101 t_write = NULL;
102
103 queue = (struct zebra_message_queue *) FIFO_HEAD (&message_queue);
104 if (queue)
105 {
106 nbytes = write (sock, queue->buf + queue->written,
107 queue->length - queue->written);
108
109 if (nbytes <= 0)
110 {
111 if (errno != EAGAIN)
112 return -1;
113 }
114 else if (nbytes != (queue->length - queue->written))
115 {
116 queue->written += nbytes;
117 }
118 else
119 {
120 FIFO_DEL (queue);
121 XFREE (MTYPE_TMP, queue->buf);
122 XFREE (MTYPE_TMP, queue);
123 }
124 }
125
126 if (FIFO_TOP (&message_queue))
paulb21b19c2003-06-15 01:28:29 +0000127 THREAD_WRITE_ON (zebrad.master, t_write, zebra_server_dequeue,
128 NULL, sock);
paulccf35572003-03-01 11:42:20 +0000129
130 return 0;
131}
132
133/* Enqueu message. */
134void
135zebra_server_enqueue (int sock, u_char *buf, unsigned long length,
136 unsigned long written)
137{
138 struct zebra_message_queue *queue;
139
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;
145
146 FIFO_ADD (&message_queue, queue);
147
paulb21b19c2003-06-15 01:28:29 +0000148 THREAD_WRITE_ON (zebrad.master, t_write, zebra_server_dequeue, NULL, sock);
paulccf35572003-03-01 11:42:20 +0000149}
150
151int
152zebra_server_send_message (int sock, u_char *buf, unsigned long length)
153{
154 int nbytes;
155
156 if (FIFO_TOP (&message_queue))
157 {
158 zebra_server_enqueue (sock, buf, length, 0);
159 return 0;
160 }
161
162 /* Send message. */
163 nbytes = write (sock, buf, length);
164
165 if (nbytes <= 0)
166 {
167 if (errno == EAGAIN)
168 zebra_server_enqueue (sock, buf, length, 0);
169 else
170 return -1;
171 }
172 else if (nbytes != length)
173 zebra_server_enqueue (sock, buf, length, nbytes);
174
175 return 0;
176}
177
paul718e3742002-12-13 20:15:29 +0000178/* Interface is added. Send ZEBRA_INTERFACE_ADD to client. */
paulb9df2d22004-05-09 09:09:59 +0000179/*
180 * This function is called in the following situations:
181 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
182 * from the client.
183 * - at startup, when zebra figures out the available interfaces
184 * - when an interface is added (where support for
185 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
186 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
187 * received)
188 */
paul718e3742002-12-13 20:15:29 +0000189int
190zsend_interface_add (struct zserv *client, struct interface *ifp)
191{
192 struct stream *s;
193
194 /* Check this client need interface information. */
195 if (! client->ifinfo)
196 return -1;
197
198 s = client->obuf;
199 stream_reset (s);
200
201 /* Place holder for size. */
202 stream_putw (s, 0);
203
204 /* Message type. */
205 stream_putc (s, ZEBRA_INTERFACE_ADD);
206
207 /* Interface information. */
208 stream_put (s, ifp->name, INTERFACE_NAMSIZ);
209 stream_putl (s, ifp->ifindex);
paul2e3b2e42002-12-13 21:03:13 +0000210 stream_putc (s, ifp->status);
paul718e3742002-12-13 20:15:29 +0000211 stream_putl (s, ifp->flags);
212 stream_putl (s, ifp->metric);
213 stream_putl (s, ifp->mtu);
paulb9df2d22004-05-09 09:09:59 +0000214 stream_putl (s, ifp->mtu6);
paul718e3742002-12-13 20:15:29 +0000215 stream_putl (s, ifp->bandwidth);
216#ifdef HAVE_SOCKADDR_DL
217 stream_put (s, &ifp->sdl, sizeof (ifp->sdl));
218#else
219 stream_putl (s, ifp->hw_addr_len);
220 if (ifp->hw_addr_len)
221 stream_put (s, ifp->hw_addr, ifp->hw_addr_len);
222#endif /* HAVE_SOCKADDR_DL */
223
224 /* Write packet size. */
225 stream_putw_at (s, 0, stream_get_endp (s));
226
paulccf35572003-03-01 11:42:20 +0000227 zebra_server_send_message (client->sock, s->data, stream_get_endp (s));
228
229 return 0;
paul718e3742002-12-13 20:15:29 +0000230}
231
232/* Interface deletion from zebra daemon. */
paulb9df2d22004-05-09 09:09:59 +0000233/*
234 * This function is only called when support for
235 * RTM_IFANNOUNCE or AF_NETLINK sockets (RTM_DELLINK message)
236 * is available. It is not called on Solaris.
237 */
238#if (defined(RTM_IFANNOUNCE) || defined(HAVE_NETLINK))
paul718e3742002-12-13 20:15:29 +0000239int
240zsend_interface_delete (struct zserv *client, struct interface *ifp)
241{
242 struct stream *s;
243
244 /* Check this client need interface information. */
245 if (! client->ifinfo)
246 return -1;
247
248 s = client->obuf;
249 stream_reset (s);
250
251 /* Packet length placeholder. */
252 stream_putw (s, 0);
253
254 /* Interface information. */
255 stream_putc (s, ZEBRA_INTERFACE_DELETE);
256 stream_put (s, ifp->name, INTERFACE_NAMSIZ);
257 stream_putl (s, ifp->ifindex);
paul2e3b2e42002-12-13 21:03:13 +0000258 stream_putc (s, ifp->status);
paul718e3742002-12-13 20:15:29 +0000259 stream_putl (s, ifp->flags);
260 stream_putl (s, ifp->metric);
261 stream_putl (s, ifp->mtu);
paulb9df2d22004-05-09 09:09:59 +0000262 stream_putl (s, ifp->mtu6);
paul718e3742002-12-13 20:15:29 +0000263 stream_putl (s, ifp->bandwidth);
264
265 /* Write packet length. */
266 stream_putw_at (s, 0, stream_get_endp (s));
267
paulccf35572003-03-01 11:42:20 +0000268 zebra_server_send_message (client->sock, s->data, stream_get_endp (s));
269
270 return 0;
paul718e3742002-12-13 20:15:29 +0000271}
paulb9df2d22004-05-09 09:09:59 +0000272#endif /* (defined(RTM_IFANNOUNCE) || defined(HAVE_LINUX_RTNETLINK_H)) */
paul718e3742002-12-13 20:15:29 +0000273
paulb9df2d22004-05-09 09:09:59 +0000274/* Interface address is added/deleted. Send ZEBRA_INTERFACE_ADDRESS_ADD or
275 * ZEBRA_INTERFACE_ADDRESS_DELETE to the client.
276 *
277 * A ZEBRA_INTERFACE_ADDRESS_ADD is sent in the following situations:
278 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
279 * from the client, after the ZEBRA_INTERFACE_ADD has been
280 * sent from zebra to the client
281 * - redistribute new address info to all clients in the following situations
282 * - at startup, when zebra figures out the available interfaces
283 * - when an interface is added (where support for
284 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
285 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
286 * received)
287 * - for the vty commands "ip address A.B.C.D/M [<secondary>|<label LINE>]"
288 * and "no bandwidth <1-10000000>", "ipv6 address X:X::X:X/M"
289 * - when an RTM_NEWADDR message is received from the kernel,
290 *
291 * The call tree that triggers ZEBRA_INTERFACE_ADDRESS_DELETE:
292 *
293 * zsend_interface_address(DELETE)
294 * ^
295 * |
296 * zebra_interface_address_delete_update
297 * ^ ^ ^
298 * | | if_delete_update (not called on
299 * | | Solaris)
300 * ip_address_uninstall connected_delete_ipv4
301 * [ipv6_addresss_uninstall] [connected_delete_ipv6]
302 * ^ ^
303 * | |
304 * | RTM_NEWADDR on routing/netlink socket
305 * |
306 * vty commands:
307 * "no ip address A.B.C.D/M [label LINE]"
308 * "no ip address A.B.C.D/M secondary"
309 * ["no ipv6 address X:X::X:X/M"]
310 *
311 */
paul718e3742002-12-13 20:15:29 +0000312int
paulb9df2d22004-05-09 09:09:59 +0000313zsend_interface_address (int cmd, struct zserv *client,
314 struct interface *ifp, struct connected *ifc)
paul718e3742002-12-13 20:15:29 +0000315{
316 int blen;
317 struct stream *s;
318 struct prefix *p;
319
320 /* Check this client need interface information. */
321 if (! client->ifinfo)
322 return -1;
323
324 s = client->obuf;
325 stream_reset (s);
326
327 /* Place holder for size. */
328 stream_putw (s, 0);
329
paulb9df2d22004-05-09 09:09:59 +0000330 stream_putc (s, cmd);
paul718e3742002-12-13 20:15:29 +0000331 stream_putl (s, ifp->ifindex);
332
333 /* Interface address flag. */
334 stream_putc (s, ifc->flags);
335
336 /* Prefix information. */
337 p = ifc->address;
338 stream_putc (s, p->family);
339 blen = prefix_blen (p);
340 stream_put (s, &p->u.prefix, blen);
paulb9df2d22004-05-09 09:09:59 +0000341
342 /*
343 * XXX gnu version does not send prefixlen for ZEBRA_INTERFACE_ADDRESS_DELETE
344 * but zebra_interface_address_delete_read() in the gnu version
345 * expects to find it
346 */
paul718e3742002-12-13 20:15:29 +0000347 stream_putc (s, p->prefixlen);
348
349 /* Destination. */
350 p = ifc->destination;
351 if (p)
352 stream_put (s, &p->u.prefix, blen);
353 else
354 stream_put (s, NULL, blen);
355
356 /* Write packet size. */
357 stream_putw_at (s, 0, stream_get_endp (s));
358
paulccf35572003-03-01 11:42:20 +0000359 zebra_server_send_message (client->sock, s->data, stream_get_endp (s));
360
361 return 0;
paul718e3742002-12-13 20:15:29 +0000362}
363
paulb9df2d22004-05-09 09:09:59 +0000364/*
365 * The cmd passed to zsend_interface_update may be ZEBRA_INTERFACE_UP or
366 * ZEBRA_INTERFACE_DOWN.
367 *
368 * The ZEBRA_INTERFACE_UP message is sent from the zebra server to
369 * the clients in one of 2 situations:
370 * - an if_up is detected e.g., as a result of an RTM_IFINFO message
371 * - a vty command modifying the bandwidth of an interface is received.
372 * The ZEBRA_INTERFACE_DOWN message is sent when an if_down is detected.
373 */
paul718e3742002-12-13 20:15:29 +0000374int
paulb9df2d22004-05-09 09:09:59 +0000375zsend_interface_update (int cmd, struct zserv *client, struct interface *ifp)
paul718e3742002-12-13 20:15:29 +0000376{
377 struct stream *s;
378
379 /* Check this client need interface information. */
380 if (! client->ifinfo)
381 return -1;
382
383 s = client->obuf;
384 stream_reset (s);
385
386 /* Place holder for size. */
387 stream_putw (s, 0);
388
389 /* Zebra command. */
paulb9df2d22004-05-09 09:09:59 +0000390 stream_putc (s, cmd);
paul718e3742002-12-13 20:15:29 +0000391
392 /* Interface information. */
393 stream_put (s, ifp->name, INTERFACE_NAMSIZ);
394 stream_putl (s, ifp->ifindex);
paul2e3b2e42002-12-13 21:03:13 +0000395 stream_putc (s, ifp->status);
paul718e3742002-12-13 20:15:29 +0000396 stream_putl (s, ifp->flags);
397 stream_putl (s, ifp->metric);
398 stream_putl (s, ifp->mtu);
paulb9df2d22004-05-09 09:09:59 +0000399 stream_putl (s, ifp->mtu6);
paul718e3742002-12-13 20:15:29 +0000400 stream_putl (s, ifp->bandwidth);
401
402 /* Write packet size. */
403 stream_putw_at (s, 0, stream_get_endp (s));
404
paulccf35572003-03-01 11:42:20 +0000405 zebra_server_send_message (client->sock, s->data, stream_get_endp (s));
406
407 return 0;
paul718e3742002-12-13 20:15:29 +0000408}
409
paulb9df2d22004-05-09 09:09:59 +0000410/*
411 * The zebra server sends the clients a ZEBRA_IPV4_ROUTE_ADD or a
412 * ZEBRA_IPV6_ROUTE_ADD via zsend_route_multipath in the following
413 * situations:
414 * - when the client starts up, and requests default information
415 * by sending a ZEBRA_REDISTRIBUTE_DEFAULT_ADD to the zebra server, in the
416 * - case of rip, ripngd, ospfd and ospf6d, when the client sends a
417 * ZEBRA_REDISTRIBUTE_ADD as a result of the "redistribute" vty cmd,
418 * - when the zebra server redistributes routes after it updates its rib
419 *
420 * The zebra server sends clients a ZEBRA_IPV4_ROUTE_DELETE or a
421 * ZEBRA_IPV6_ROUTE_DELETE via zsend_route_multipath when:
422 * - a "ip route" or "ipv6 route" vty command is issued, a prefix is
423 * - deleted from zebra's rib, and this info
424 * has to be redistributed to the clients
425 *
426 * XXX The ZEBRA_IPV*_ROUTE_ADD message is also sent by the client to the
427 * zebra server when the client wants to tell the zebra server to add a
428 * route to the kernel (zapi_ipv4_add etc. ). Since it's essentially the
429 * same message being sent back and forth, this function and
430 * zapi_ipv{4,6}_{add, delete} should be re-written to avoid code
431 * duplication.
432 */
paul718e3742002-12-13 20:15:29 +0000433int
paulb9df2d22004-05-09 09:09:59 +0000434zsend_route_multipath (int cmd, struct zserv *client, struct prefix *p,
435 struct rib *rib)
paul718e3742002-12-13 20:15:29 +0000436{
437 int psize;
438 struct stream *s;
439 struct nexthop *nexthop;
paulb9df2d22004-05-09 09:09:59 +0000440 unsigned long nhnummark = 0;
441 int nhnum = 0;
442 u_char zapi_flags = ZAPI_MESSAGE_NEXTHOP | ZAPI_MESSAGE_IFINDEX;
443
paul718e3742002-12-13 20:15:29 +0000444 s = client->obuf;
445 stream_reset (s);
446
447 /* Place holder for size. */
448 stream_putw (s, 0);
449
450 /* Put command, type and nexthop. */
paulb9df2d22004-05-09 09:09:59 +0000451 stream_putc (s, cmd);
paul718e3742002-12-13 20:15:29 +0000452 stream_putc (s, rib->type);
453 stream_putc (s, rib->flags);
paulb9df2d22004-05-09 09:09:59 +0000454
455 /*
456 * XXX no need to set ZAPI_MESSAGE_NEXTHOP if we are going to
457 * send empty nexthop?
458 */
459 if (cmd == ZEBRA_IPV4_ROUTE_ADD || ZEBRA_IPV6_ROUTE_ADD)
460 zapi_flags |= ZAPI_MESSAGE_METRIC;
461
462 stream_putc (s, zapi_flags);
paul718e3742002-12-13 20:15:29 +0000463
464 /* Prefix. */
465 psize = PSIZE (p->prefixlen);
466 stream_putc (s, p->prefixlen);
paulb9df2d22004-05-09 09:09:59 +0000467 stream_write (s, (u_char *) & p->u.prefix, psize);
paul718e3742002-12-13 20:15:29 +0000468
paulb9df2d22004-05-09 09:09:59 +0000469 /*
470 * XXX The message format sent by zebra below does not match the format
471 * of the corresponding message expected by the zebra server
472 * itself (e.g., see zread_ipv4_add). The nexthop_num is not set correctly,
473 * (is there a bug on the client side if more than one segment is sent?)
474 * nexthop ZEBRA_NEXTHOP_IPV4 is never set, ZEBRA_NEXTHOP_IFINDEX
475 * is hard-coded.
476 */
paul718e3742002-12-13 20:15:29 +0000477 /* Nexthop */
478 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
479 {
480 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB))
paulb9df2d22004-05-09 09:09:59 +0000481 {
482 nhnummark = stream_get_putp (s);
483 stream_putc (s, 1); /* placeholder */
484 nhnum++;
paul718e3742002-12-13 20:15:29 +0000485
paulb9df2d22004-05-09 09:09:59 +0000486 switch(nexthop->type)
487 {
488 case NEXTHOP_TYPE_IPV4:
489 case NEXTHOP_TYPE_IPV4_IFINDEX:
490 stream_put_in_addr (s, &nexthop->gate.ipv4);
491 break;
492#ifdef HAVE_IPV6
493 case NEXTHOP_TYPE_IPV6:
494 case NEXTHOP_TYPE_IPV6_IFINDEX:
495 case NEXTHOP_TYPE_IPV6_IFNAME:
496 stream_write (s, (u_char *) &nexthop->gate.ipv6, 16);
497 break;
498#endif
499 default:
500 if (cmd == ZEBRA_IPV4_ROUTE_ADD
501 || cmd == ZEBRA_IPV4_ROUTE_DELETE)
502 {
503 struct in_addr empty;
paul44983cf2004-09-22 13:15:58 +0000504 memset (&empty, 0, sizeof (struct in_addr));
paulb9df2d22004-05-09 09:09:59 +0000505 stream_write (s, (u_char *) &empty, IPV4_MAX_BYTELEN);
506 }
507 else
508 {
509 struct in6_addr empty;
510 memset (&empty, 0, sizeof (struct in6_addr));
511 stream_write (s, (u_char *) &empty, IPV6_MAX_BYTELEN);
512 }
513 }
paul718e3742002-12-13 20:15:29 +0000514
paulb9df2d22004-05-09 09:09:59 +0000515 /* Interface index. */
516 stream_putc (s, 1);
517 stream_putl (s, nexthop->ifindex);
paul718e3742002-12-13 20:15:29 +0000518
paulb9df2d22004-05-09 09:09:59 +0000519 break;
520 }
paul718e3742002-12-13 20:15:29 +0000521 }
522
523 /* Metric */
524 stream_putl (s, rib->metric);
525
paulb9df2d22004-05-09 09:09:59 +0000526 /* Write next-hop number */
527 if (nhnummark)
528 stream_putw_at (s, nhnummark, nhnum);
529
paul718e3742002-12-13 20:15:29 +0000530 /* Write packet size. */
531 stream_putw_at (s, 0, stream_get_endp (s));
532
paulccf35572003-03-01 11:42:20 +0000533 zebra_server_send_message (client->sock, s->data, stream_get_endp (s));
534
535 return 0;
paul718e3742002-12-13 20:15:29 +0000536}
537
paul718e3742002-12-13 20:15:29 +0000538#ifdef HAVE_IPV6
paul718e3742002-12-13 20:15:29 +0000539int
540zsend_ipv6_nexthop_lookup (struct zserv *client, struct in6_addr *addr)
541{
542 struct stream *s;
543 struct rib *rib;
544 unsigned long nump;
545 u_char num;
546 struct nexthop *nexthop;
547
548 /* Lookup nexthop. */
549 rib = rib_match_ipv6 (addr);
550
551 /* Get output stream. */
552 s = client->obuf;
553 stream_reset (s);
554
555 /* Fill in result. */
556 stream_putw (s, 0);
557 stream_putc (s, ZEBRA_IPV6_NEXTHOP_LOOKUP);
558 stream_put (s, &addr, 16);
559
560 if (rib)
561 {
562 stream_putl (s, rib->metric);
563 num = 0;
564 nump = s->putp;
565 stream_putc (s, 0);
566 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
567 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB))
568 {
569 stream_putc (s, nexthop->type);
570 switch (nexthop->type)
571 {
572 case ZEBRA_NEXTHOP_IPV6:
573 stream_put (s, &nexthop->gate.ipv6, 16);
574 break;
575 case ZEBRA_NEXTHOP_IPV6_IFINDEX:
576 case ZEBRA_NEXTHOP_IPV6_IFNAME:
577 stream_put (s, &nexthop->gate.ipv6, 16);
578 stream_putl (s, nexthop->ifindex);
579 break;
580 case ZEBRA_NEXTHOP_IFINDEX:
581 case ZEBRA_NEXTHOP_IFNAME:
582 stream_putl (s, nexthop->ifindex);
583 break;
hassofa2b17e2004-03-04 17:45:00 +0000584 default:
585 /* do nothing */
586 break;
paul718e3742002-12-13 20:15:29 +0000587 }
588 num++;
589 }
590 stream_putc_at (s, nump, num);
591 }
592 else
593 {
594 stream_putl (s, 0);
595 stream_putc (s, 0);
596 }
597
598 stream_putw_at (s, 0, stream_get_endp (s));
599
paulccf35572003-03-01 11:42:20 +0000600 zebra_server_send_message (client->sock, s->data, stream_get_endp (s));
601
602 return 0;
paul718e3742002-12-13 20:15:29 +0000603}
604#endif /* HAVE_IPV6 */
605
paulb9df2d22004-05-09 09:09:59 +0000606static int
paul718e3742002-12-13 20:15:29 +0000607zsend_ipv4_nexthop_lookup (struct zserv *client, struct in_addr addr)
608{
609 struct stream *s;
610 struct rib *rib;
611 unsigned long nump;
612 u_char num;
613 struct nexthop *nexthop;
614
615 /* Lookup nexthop. */
616 rib = rib_match_ipv4 (addr);
617
618 /* Get output stream. */
619 s = client->obuf;
620 stream_reset (s);
621
622 /* Fill in result. */
623 stream_putw (s, 0);
624 stream_putc (s, ZEBRA_IPV4_NEXTHOP_LOOKUP);
625 stream_put_in_addr (s, &addr);
626
627 if (rib)
628 {
629 stream_putl (s, rib->metric);
630 num = 0;
631 nump = s->putp;
632 stream_putc (s, 0);
633 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
634 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB))
635 {
636 stream_putc (s, nexthop->type);
637 switch (nexthop->type)
638 {
639 case ZEBRA_NEXTHOP_IPV4:
640 stream_put_in_addr (s, &nexthop->gate.ipv4);
641 break;
642 case ZEBRA_NEXTHOP_IFINDEX:
643 case ZEBRA_NEXTHOP_IFNAME:
644 stream_putl (s, nexthop->ifindex);
645 break;
hassofa2b17e2004-03-04 17:45:00 +0000646 default:
647 /* do nothing */
648 break;
paul718e3742002-12-13 20:15:29 +0000649 }
650 num++;
651 }
652 stream_putc_at (s, nump, num);
653 }
654 else
655 {
656 stream_putl (s, 0);
657 stream_putc (s, 0);
658 }
659
660 stream_putw_at (s, 0, stream_get_endp (s));
661
paulccf35572003-03-01 11:42:20 +0000662 zebra_server_send_message (client->sock, s->data, stream_get_endp (s));
663
664 return 0;
paul718e3742002-12-13 20:15:29 +0000665}
666
paulb9df2d22004-05-09 09:09:59 +0000667static int
paul718e3742002-12-13 20:15:29 +0000668zsend_ipv4_import_lookup (struct zserv *client, struct prefix_ipv4 *p)
669{
670 struct stream *s;
671 struct rib *rib;
672 unsigned long nump;
673 u_char num;
674 struct nexthop *nexthop;
675
676 /* Lookup nexthop. */
677 rib = rib_lookup_ipv4 (p);
678
679 /* Get output stream. */
680 s = client->obuf;
681 stream_reset (s);
682
683 /* Fill in result. */
684 stream_putw (s, 0);
685 stream_putc (s, ZEBRA_IPV4_IMPORT_LOOKUP);
686 stream_put_in_addr (s, &p->prefix);
687
688 if (rib)
689 {
690 stream_putl (s, rib->metric);
691 num = 0;
692 nump = s->putp;
693 stream_putc (s, 0);
694 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
695 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB))
696 {
697 stream_putc (s, nexthop->type);
698 switch (nexthop->type)
699 {
700 case ZEBRA_NEXTHOP_IPV4:
701 stream_put_in_addr (s, &nexthop->gate.ipv4);
702 break;
703 case ZEBRA_NEXTHOP_IFINDEX:
704 case ZEBRA_NEXTHOP_IFNAME:
705 stream_putl (s, nexthop->ifindex);
706 break;
hassofa2b17e2004-03-04 17:45:00 +0000707 default:
708 /* do nothing */
709 break;
paul718e3742002-12-13 20:15:29 +0000710 }
711 num++;
712 }
713 stream_putc_at (s, nump, num);
714 }
715 else
716 {
717 stream_putl (s, 0);
718 stream_putc (s, 0);
719 }
720
721 stream_putw_at (s, 0, stream_get_endp (s));
722
paulccf35572003-03-01 11:42:20 +0000723 zebra_server_send_message (client->sock, s->data, stream_get_endp (s));
724
725 return 0;
paul718e3742002-12-13 20:15:29 +0000726}
727
hasso18a6dce2004-10-03 18:18:34 +0000728/* Router-id is updated. Send ZEBRA_ROUTER_ID_ADD to client. */
729int
730zsend_router_id_update (struct zserv *client, struct prefix *p)
731{
732 struct stream *s;
733 int blen;
734
735 /* Check this client need interface information. */
736 if (!client->ridinfo)
737 return -1;
738
739 s = client->obuf;
740 stream_reset (s);
741
742 /* Place holder for size. */
743 stream_putw (s, 0);
744
745 /* Message type. */
746 stream_putc (s, ZEBRA_ROUTER_ID_UPDATE);
747
748 /* Prefix information. */
749 stream_putc (s, p->family);
750 blen = prefix_blen (p);
751 stream_put (s, &p->u.prefix, blen);
752 stream_putc (s, p->prefixlen);
753
754 /* Write packet size. */
755 stream_putw_at (s, 0, stream_get_endp (s));
756
757 return writen (client->sock, s->data, stream_get_endp (s));
758}
759
paul718e3742002-12-13 20:15:29 +0000760/* Register zebra server interface information. Send current all
761 interface and address information. */
paulb9df2d22004-05-09 09:09:59 +0000762static void
paul718e3742002-12-13 20:15:29 +0000763zread_interface_add (struct zserv *client, u_short length)
764{
hasso52dc7ee2004-09-23 19:18:23 +0000765 struct listnode *ifnode;
766 struct listnode *cnode;
paul718e3742002-12-13 20:15:29 +0000767 struct interface *ifp;
768 struct connected *c;
769
770 /* Interface information is needed. */
771 client->ifinfo = 1;
772
773 for (ifnode = listhead (iflist); ifnode; ifnode = nextnode (ifnode))
774 {
775 ifp = getdata (ifnode);
776
777 /* Skip pseudo interface. */
778 if (! CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE))
779 continue;
780
781 zsend_interface_add (client, ifp);
782
783 for (cnode = listhead (ifp->connected); cnode; nextnode (cnode))
784 {
785 c = getdata (cnode);
786 if (CHECK_FLAG (c->conf, ZEBRA_IFC_REAL))
paulb9df2d22004-05-09 09:09:59 +0000787 zsend_interface_address (ZEBRA_INTERFACE_ADDRESS_ADD, client,
788 ifp, c);
paul718e3742002-12-13 20:15:29 +0000789 }
790 }
791}
792
793/* Unregister zebra server interface information. */
paulb9df2d22004-05-09 09:09:59 +0000794static void
paul718e3742002-12-13 20:15:29 +0000795zread_interface_delete (struct zserv *client, u_short length)
796{
797 client->ifinfo = 0;
798}
799
800/* This function support multiple nexthop. */
paulb9df2d22004-05-09 09:09:59 +0000801/*
802 * Parse the ZEBRA_IPV4_ROUTE_ADD sent from client. Update rib and
803 * add kernel route.
804 */
805static void
paul718e3742002-12-13 20:15:29 +0000806zread_ipv4_add (struct zserv *client, u_short length)
807{
808 int i;
809 struct rib *rib;
810 struct prefix_ipv4 p;
811 u_char message;
812 struct in_addr nexthop;
813 u_char nexthop_num;
814 u_char nexthop_type;
815 struct stream *s;
816 unsigned int ifindex;
817 u_char ifname_len;
818
819 /* Get input stream. */
820 s = client->ibuf;
821
822 /* Allocate new rib. */
823 rib = XMALLOC (MTYPE_RIB, sizeof (struct rib));
824 memset (rib, 0, sizeof (struct rib));
825
826 /* Type, flags, message. */
827 rib->type = stream_getc (s);
828 rib->flags = stream_getc (s);
paulb9df2d22004-05-09 09:09:59 +0000829 message = stream_getc (s);
paul718e3742002-12-13 20:15:29 +0000830 rib->uptime = time (NULL);
831
832 /* IPv4 prefix. */
833 memset (&p, 0, sizeof (struct prefix_ipv4));
834 p.family = AF_INET;
835 p.prefixlen = stream_getc (s);
836 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
837
838 /* Nexthop parse. */
839 if (CHECK_FLAG (message, ZAPI_MESSAGE_NEXTHOP))
840 {
841 nexthop_num = stream_getc (s);
842
843 for (i = 0; i < nexthop_num; i++)
844 {
845 nexthop_type = stream_getc (s);
846
847 switch (nexthop_type)
848 {
849 case ZEBRA_NEXTHOP_IFINDEX:
850 ifindex = stream_getl (s);
851 nexthop_ifindex_add (rib, ifindex);
852 break;
853 case ZEBRA_NEXTHOP_IFNAME:
854 ifname_len = stream_getc (s);
855 stream_forward (s, ifname_len);
856 break;
857 case ZEBRA_NEXTHOP_IPV4:
858 nexthop.s_addr = stream_get_ipv4 (s);
859 nexthop_ipv4_add (rib, &nexthop);
860 break;
861 case ZEBRA_NEXTHOP_IPV6:
862 stream_forward (s, IPV6_MAX_BYTELEN);
863 break;
paul595db7f2003-05-25 21:35:06 +0000864 case ZEBRA_NEXTHOP_BLACKHOLE:
865 nexthop_blackhole_add (rib);
866 break;
paul718e3742002-12-13 20:15:29 +0000867 }
868 }
869 }
870
871 /* Distance. */
872 if (CHECK_FLAG (message, ZAPI_MESSAGE_DISTANCE))
873 rib->distance = stream_getc (s);
874
875 /* Metric. */
876 if (CHECK_FLAG (message, ZAPI_MESSAGE_METRIC))
877 rib->metric = stream_getl (s);
878
879 rib_add_ipv4_multipath (&p, rib);
880}
881
882/* Zebra server IPv4 prefix delete function. */
paulb9df2d22004-05-09 09:09:59 +0000883static void
paul718e3742002-12-13 20:15:29 +0000884zread_ipv4_delete (struct zserv *client, u_short length)
885{
886 int i;
887 struct stream *s;
888 struct zapi_ipv4 api;
889 struct in_addr nexthop;
890 unsigned long ifindex;
891 struct prefix_ipv4 p;
892 u_char nexthop_num;
893 u_char nexthop_type;
894 u_char ifname_len;
895
896 s = client->ibuf;
897 ifindex = 0;
898 nexthop.s_addr = 0;
899
900 /* Type, flags, message. */
901 api.type = stream_getc (s);
902 api.flags = stream_getc (s);
903 api.message = stream_getc (s);
904
905 /* IPv4 prefix. */
906 memset (&p, 0, sizeof (struct prefix_ipv4));
907 p.family = AF_INET;
908 p.prefixlen = stream_getc (s);
909 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
910
911 /* Nexthop, ifindex, distance, metric. */
912 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
913 {
914 nexthop_num = stream_getc (s);
915
916 for (i = 0; i < nexthop_num; i++)
917 {
918 nexthop_type = stream_getc (s);
919
920 switch (nexthop_type)
921 {
922 case ZEBRA_NEXTHOP_IFINDEX:
923 ifindex = stream_getl (s);
924 break;
925 case ZEBRA_NEXTHOP_IFNAME:
926 ifname_len = stream_getc (s);
927 stream_forward (s, ifname_len);
928 break;
929 case ZEBRA_NEXTHOP_IPV4:
930 nexthop.s_addr = stream_get_ipv4 (s);
931 break;
932 case ZEBRA_NEXTHOP_IPV6:
933 stream_forward (s, IPV6_MAX_BYTELEN);
934 break;
935 }
936 }
937 }
938
939 /* Distance. */
940 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
941 api.distance = stream_getc (s);
942 else
943 api.distance = 0;
944
945 /* Metric. */
946 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
947 api.metric = stream_getl (s);
948 else
949 api.metric = 0;
950
951 rib_delete_ipv4 (api.type, api.flags, &p, &nexthop, ifindex,
952 client->rtm_table);
953}
954
955/* Nexthop lookup for IPv4. */
paulb9df2d22004-05-09 09:09:59 +0000956static void
paul718e3742002-12-13 20:15:29 +0000957zread_ipv4_nexthop_lookup (struct zserv *client, u_short length)
958{
959 struct in_addr addr;
960
961 addr.s_addr = stream_get_ipv4 (client->ibuf);
962 zsend_ipv4_nexthop_lookup (client, addr);
963}
964
965/* Nexthop lookup for IPv4. */
paulb9df2d22004-05-09 09:09:59 +0000966static void
paul718e3742002-12-13 20:15:29 +0000967zread_ipv4_import_lookup (struct zserv *client, u_short length)
968{
969 struct prefix_ipv4 p;
970
971 p.family = AF_INET;
972 p.prefixlen = stream_getc (client->ibuf);
973 p.prefix.s_addr = stream_get_ipv4 (client->ibuf);
974
975 zsend_ipv4_import_lookup (client, &p);
976}
977
978#ifdef HAVE_IPV6
979/* Zebra server IPv6 prefix add function. */
paulb9df2d22004-05-09 09:09:59 +0000980static void
paul718e3742002-12-13 20:15:29 +0000981zread_ipv6_add (struct zserv *client, u_short length)
982{
983 int i;
984 struct stream *s;
985 struct zapi_ipv6 api;
986 struct in6_addr nexthop;
987 unsigned long ifindex;
988 struct prefix_ipv6 p;
989
990 s = client->ibuf;
991 ifindex = 0;
992 memset (&nexthop, 0, sizeof (struct in6_addr));
993
994 /* Type, flags, message. */
995 api.type = stream_getc (s);
996 api.flags = stream_getc (s);
997 api.message = stream_getc (s);
998
999 /* IPv4 prefix. */
1000 memset (&p, 0, sizeof (struct prefix_ipv6));
1001 p.family = AF_INET6;
1002 p.prefixlen = stream_getc (s);
1003 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
1004
1005 /* Nexthop, ifindex, distance, metric. */
1006 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
1007 {
1008 u_char nexthop_type;
1009
1010 api.nexthop_num = stream_getc (s);
1011 for (i = 0; i < api.nexthop_num; i++)
1012 {
1013 nexthop_type = stream_getc (s);
1014
1015 switch (nexthop_type)
1016 {
1017 case ZEBRA_NEXTHOP_IPV6:
1018 stream_get (&nexthop, s, 16);
1019 break;
1020 case ZEBRA_NEXTHOP_IFINDEX:
1021 ifindex = stream_getl (s);
1022 break;
1023 }
1024 }
1025 }
1026
1027 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
1028 api.distance = stream_getc (s);
1029 else
1030 api.distance = 0;
1031
1032 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
1033 api.metric = stream_getl (s);
1034 else
1035 api.metric = 0;
1036
1037 if (IN6_IS_ADDR_UNSPECIFIED (&nexthop))
1038 rib_add_ipv6 (api.type, api.flags, &p, NULL, ifindex, 0);
1039 else
1040 rib_add_ipv6 (api.type, api.flags, &p, &nexthop, ifindex, 0);
1041}
1042
1043/* Zebra server IPv6 prefix delete function. */
paulb9df2d22004-05-09 09:09:59 +00001044static void
paul718e3742002-12-13 20:15:29 +00001045zread_ipv6_delete (struct zserv *client, u_short length)
1046{
1047 int i;
1048 struct stream *s;
1049 struct zapi_ipv6 api;
1050 struct in6_addr nexthop;
1051 unsigned long ifindex;
1052 struct prefix_ipv6 p;
1053
1054 s = client->ibuf;
1055 ifindex = 0;
1056 memset (&nexthop, 0, sizeof (struct in6_addr));
1057
1058 /* Type, flags, message. */
1059 api.type = stream_getc (s);
1060 api.flags = stream_getc (s);
1061 api.message = stream_getc (s);
1062
1063 /* IPv4 prefix. */
1064 memset (&p, 0, sizeof (struct prefix_ipv6));
1065 p.family = AF_INET6;
1066 p.prefixlen = stream_getc (s);
1067 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
1068
1069 /* Nexthop, ifindex, distance, metric. */
1070 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
1071 {
1072 u_char nexthop_type;
1073
1074 api.nexthop_num = stream_getc (s);
1075 for (i = 0; i < api.nexthop_num; i++)
1076 {
1077 nexthop_type = stream_getc (s);
1078
1079 switch (nexthop_type)
1080 {
1081 case ZEBRA_NEXTHOP_IPV6:
1082 stream_get (&nexthop, s, 16);
1083 break;
1084 case ZEBRA_NEXTHOP_IFINDEX:
1085 ifindex = stream_getl (s);
1086 break;
1087 }
1088 }
1089 }
1090
1091 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
1092 api.distance = stream_getc (s);
1093 else
1094 api.distance = 0;
1095 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
1096 api.metric = stream_getl (s);
1097 else
1098 api.metric = 0;
1099
1100 if (IN6_IS_ADDR_UNSPECIFIED (&nexthop))
1101 rib_delete_ipv6 (api.type, api.flags, &p, NULL, ifindex, 0);
1102 else
1103 rib_delete_ipv6 (api.type, api.flags, &p, &nexthop, ifindex, 0);
1104}
1105
1106void
1107zebra_read_ipv6 (int command, struct zserv *client, u_short length)
1108{
1109 u_char type;
1110 u_char flags;
1111 struct in6_addr nexthop, *gate;
1112 u_char *lim;
1113 u_char *pnt;
1114 unsigned int ifindex;
1115
1116 pnt = stream_pnt (client->ibuf);
1117 lim = pnt + length;
1118
1119 type = stream_getc (client->ibuf);
1120 flags = stream_getc (client->ibuf);
1121 stream_get (&nexthop, client->ibuf, sizeof (struct in6_addr));
1122
1123 while (stream_pnt (client->ibuf) < lim)
1124 {
1125 int size;
1126 struct prefix_ipv6 p;
1127
1128 ifindex = stream_getl (client->ibuf);
1129
1130 memset (&p, 0, sizeof (struct prefix_ipv6));
1131 p.family = AF_INET6;
1132 p.prefixlen = stream_getc (client->ibuf);
1133 size = PSIZE(p.prefixlen);
1134 stream_get (&p.prefix, client->ibuf, size);
1135
1136 if (IN6_IS_ADDR_UNSPECIFIED (&nexthop))
1137 gate = NULL;
1138 else
1139 gate = &nexthop;
1140
1141 if (command == ZEBRA_IPV6_ROUTE_ADD)
paulb9df2d22004-05-09 09:09:59 +00001142 rib_add_ipv6 (type, flags, &p, gate, ifindex, 0);
paul718e3742002-12-13 20:15:29 +00001143 else
paulb9df2d22004-05-09 09:09:59 +00001144 rib_delete_ipv6 (type, flags, &p, gate, ifindex, 0);
paul718e3742002-12-13 20:15:29 +00001145 }
1146}
1147
paulb9df2d22004-05-09 09:09:59 +00001148static void
paul718e3742002-12-13 20:15:29 +00001149zread_ipv6_nexthop_lookup (struct zserv *client, u_short length)
1150{
1151 struct in6_addr addr;
1152 char buf[BUFSIZ];
1153
1154 stream_get (&addr, client->ibuf, 16);
1155 printf ("DEBUG %s\n", inet_ntop (AF_INET6, &addr, buf, BUFSIZ));
1156
1157 zsend_ipv6_nexthop_lookup (client, &addr);
1158}
1159#endif /* HAVE_IPV6 */
1160
hasso18a6dce2004-10-03 18:18:34 +00001161/* Register zebra server router-id information. Send current router-id */
1162void
1163zread_router_id_add (struct zserv *client, u_short length)
1164{
1165 struct prefix p;
1166
1167 /* Router-id information is needed. */
1168 client->ridinfo = 1;
1169
1170 router_id_get (&p);
1171
1172 zsend_router_id_update (client,&p);
1173}
1174
1175/* Unregister zebra server router-id information. */
1176void
1177zread_router_id_delete (struct zserv *client, u_short length)
1178{
1179 client->ridinfo = 0;
1180}
1181
paul718e3742002-12-13 20:15:29 +00001182/* Close zebra client. */
paulb9df2d22004-05-09 09:09:59 +00001183static void
paul718e3742002-12-13 20:15:29 +00001184zebra_client_close (struct zserv *client)
1185{
1186 /* Close file descriptor. */
1187 if (client->sock)
1188 {
1189 close (client->sock);
1190 client->sock = -1;
1191 }
1192
1193 /* Free stream buffers. */
1194 if (client->ibuf)
1195 stream_free (client->ibuf);
1196 if (client->obuf)
1197 stream_free (client->obuf);
1198
1199 /* Release threads. */
1200 if (client->t_read)
1201 thread_cancel (client->t_read);
1202 if (client->t_write)
1203 thread_cancel (client->t_write);
1204
1205 /* Free client structure. */
paulb21b19c2003-06-15 01:28:29 +00001206 listnode_delete (zebrad.client_list, client);
paul718e3742002-12-13 20:15:29 +00001207 XFREE (0, client);
1208}
1209
1210/* Make new client. */
paulb9df2d22004-05-09 09:09:59 +00001211static void
paul718e3742002-12-13 20:15:29 +00001212zebra_client_create (int sock)
1213{
1214 struct zserv *client;
1215
1216 client = XCALLOC (0, sizeof (struct zserv));
1217
1218 /* Make client input/output buffer. */
1219 client->sock = sock;
1220 client->ibuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
1221 client->obuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
1222
1223 /* Set table number. */
paulb21b19c2003-06-15 01:28:29 +00001224 client->rtm_table = zebrad.rtm_table_default;
paul718e3742002-12-13 20:15:29 +00001225
1226 /* Add this client to linked list. */
paulb21b19c2003-06-15 01:28:29 +00001227 listnode_add (zebrad.client_list, client);
paul718e3742002-12-13 20:15:29 +00001228
1229 /* Make new read thread. */
1230 zebra_event (ZEBRA_READ, sock, client);
1231}
1232
1233/* Handler of zebra service request. */
paulb9df2d22004-05-09 09:09:59 +00001234static int
paul718e3742002-12-13 20:15:29 +00001235zebra_client_read (struct thread *thread)
1236{
1237 int sock;
1238 struct zserv *client;
1239 int nbyte;
1240 u_short length;
1241 u_char command;
1242
1243 /* Get thread data. Reset reading thread because I'm running. */
1244 sock = THREAD_FD (thread);
1245 client = THREAD_ARG (thread);
1246 client->t_read = NULL;
1247
1248 /* Read length and command. */
1249 nbyte = stream_read (client->ibuf, sock, 3);
1250 if (nbyte <= 0)
1251 {
1252 if (IS_ZEBRA_DEBUG_EVENT)
1253 zlog_info ("connection closed socket [%d]", sock);
1254 zebra_client_close (client);
1255 return -1;
1256 }
1257 length = stream_getw (client->ibuf);
1258 command = stream_getc (client->ibuf);
1259
1260 if (length < 3)
1261 {
1262 if (IS_ZEBRA_DEBUG_EVENT)
1263 zlog_info ("length %d is less than 3 ", length);
1264 zebra_client_close (client);
1265 return -1;
1266 }
1267
1268 length -= 3;
1269
1270 /* Read rest of data. */
1271 if (length)
1272 {
1273 nbyte = stream_read (client->ibuf, sock, length);
1274 if (nbyte <= 0)
1275 {
1276 if (IS_ZEBRA_DEBUG_EVENT)
1277 zlog_info ("connection closed [%d] when reading zebra data", sock);
1278 zebra_client_close (client);
1279 return -1;
1280 }
1281 }
1282
1283 /* Debug packet information. */
1284 if (IS_ZEBRA_DEBUG_EVENT)
1285 zlog_info ("zebra message comes from socket [%d]", sock);
1286
1287 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
1288 zlog_info ("zebra message received [%s] %d",
1289 zebra_command_str[command], length);
1290
1291 switch (command)
1292 {
hasso18a6dce2004-10-03 18:18:34 +00001293 case ZEBRA_ROUTER_ID_ADD:
1294 zread_router_id_add (client, length);
1295 break;
1296 case ZEBRA_ROUTER_ID_DELETE:
1297 zread_router_id_delete (client, length);
1298 break;
paul718e3742002-12-13 20:15:29 +00001299 case ZEBRA_INTERFACE_ADD:
1300 zread_interface_add (client, length);
1301 break;
1302 case ZEBRA_INTERFACE_DELETE:
1303 zread_interface_delete (client, length);
1304 break;
1305 case ZEBRA_IPV4_ROUTE_ADD:
1306 zread_ipv4_add (client, length);
1307 break;
1308 case ZEBRA_IPV4_ROUTE_DELETE:
1309 zread_ipv4_delete (client, length);
1310 break;
1311#ifdef HAVE_IPV6
1312 case ZEBRA_IPV6_ROUTE_ADD:
1313 zread_ipv6_add (client, length);
1314 break;
1315 case ZEBRA_IPV6_ROUTE_DELETE:
1316 zread_ipv6_delete (client, length);
1317 break;
1318#endif /* HAVE_IPV6 */
1319 case ZEBRA_REDISTRIBUTE_ADD:
1320 zebra_redistribute_add (command, client, length);
1321 break;
1322 case ZEBRA_REDISTRIBUTE_DELETE:
1323 zebra_redistribute_delete (command, client, length);
1324 break;
1325 case ZEBRA_REDISTRIBUTE_DEFAULT_ADD:
1326 zebra_redistribute_default_add (command, client, length);
1327 break;
1328 case ZEBRA_REDISTRIBUTE_DEFAULT_DELETE:
1329 zebra_redistribute_default_delete (command, client, length);
1330 break;
1331 case ZEBRA_IPV4_NEXTHOP_LOOKUP:
1332 zread_ipv4_nexthop_lookup (client, length);
1333 break;
1334#ifdef HAVE_IPV6
1335 case ZEBRA_IPV6_NEXTHOP_LOOKUP:
1336 zread_ipv6_nexthop_lookup (client, length);
1337 break;
1338#endif /* HAVE_IPV6 */
1339 case ZEBRA_IPV4_IMPORT_LOOKUP:
1340 zread_ipv4_import_lookup (client, length);
1341 break;
1342 default:
1343 zlog_info ("Zebra received unknown command %d", command);
1344 break;
1345 }
1346
1347 stream_reset (client->ibuf);
1348 zebra_event (ZEBRA_READ, sock, client);
1349
1350 return 0;
1351}
1352
paul718e3742002-12-13 20:15:29 +00001353
1354/* Accept code of zebra server socket. */
paulb9df2d22004-05-09 09:09:59 +00001355static int
paul718e3742002-12-13 20:15:29 +00001356zebra_accept (struct thread *thread)
1357{
paulccf35572003-03-01 11:42:20 +00001358 int val;
paul718e3742002-12-13 20:15:29 +00001359 int accept_sock;
1360 int client_sock;
1361 struct sockaddr_in client;
1362 socklen_t len;
1363
1364 accept_sock = THREAD_FD (thread);
1365
1366 len = sizeof (struct sockaddr_in);
1367 client_sock = accept (accept_sock, (struct sockaddr *) &client, &len);
1368
1369 if (client_sock < 0)
1370 {
1371 zlog_warn ("Can't accept zebra socket: %s", strerror (errno));
1372 return -1;
1373 }
1374
paulccf35572003-03-01 11:42:20 +00001375 /* Make client socket non-blocking. */
1376
1377 val = fcntl (client_sock, F_GETFL, 0);
1378 fcntl (client_sock, F_SETFL, (val | O_NONBLOCK));
1379
paul718e3742002-12-13 20:15:29 +00001380 /* Create new zebra client. */
1381 zebra_client_create (client_sock);
1382
1383 /* Register myself. */
1384 zebra_event (ZEBRA_SERV, accept_sock, NULL);
1385
1386 return 0;
1387}
1388
paulb9df2d22004-05-09 09:09:59 +00001389#ifdef HAVE_TCP_ZEBRA
paul718e3742002-12-13 20:15:29 +00001390/* Make zebra's server socket. */
paulb9df2d22004-05-09 09:09:59 +00001391static void
paul718e3742002-12-13 20:15:29 +00001392zebra_serv ()
1393{
1394 int ret;
1395 int accept_sock;
1396 struct sockaddr_in addr;
1397
1398 accept_sock = socket (AF_INET, SOCK_STREAM, 0);
1399
1400 if (accept_sock < 0)
1401 {
1402 zlog_warn ("Can't bind to socket: %s", strerror (errno));
1403 zlog_warn ("zebra can't provice full functionality due to above error");
1404 return;
1405 }
1406
1407 memset (&addr, 0, sizeof (struct sockaddr_in));
1408 addr.sin_family = AF_INET;
1409 addr.sin_port = htons (ZEBRA_PORT);
1410#ifdef HAVE_SIN_LEN
1411 addr.sin_len = sizeof (struct sockaddr_in);
1412#endif /* HAVE_SIN_LEN */
1413 addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
1414
1415 sockopt_reuseaddr (accept_sock);
1416 sockopt_reuseport (accept_sock);
1417
pauledd7c242003-06-04 13:59:38 +00001418 if ( zserv_privs.change(ZPRIVS_RAISE) )
1419 zlog (NULL, LOG_ERR, "Can't raise privileges");
1420
paul718e3742002-12-13 20:15:29 +00001421 ret = bind (accept_sock, (struct sockaddr *)&addr,
1422 sizeof (struct sockaddr_in));
1423 if (ret < 0)
1424 {
1425 zlog_warn ("Can't bind to socket: %s", strerror (errno));
1426 zlog_warn ("zebra can't provice full functionality due to above error");
1427 close (accept_sock); /* Avoid sd leak. */
1428 return;
1429 }
pauledd7c242003-06-04 13:59:38 +00001430
1431 if ( zserv_privs.change(ZPRIVS_LOWER) )
1432 zlog (NULL, LOG_ERR, "Can't lower privileges");
paul718e3742002-12-13 20:15:29 +00001433
1434 ret = listen (accept_sock, 1);
1435 if (ret < 0)
1436 {
1437 zlog_warn ("Can't listen to socket: %s", strerror (errno));
1438 zlog_warn ("zebra can't provice full functionality due to above error");
1439 close (accept_sock); /* Avoid sd leak. */
1440 return;
1441 }
1442
1443 zebra_event (ZEBRA_SERV, accept_sock, NULL);
1444}
paulb9df2d22004-05-09 09:09:59 +00001445#endif /* HAVE_TCP_ZEBRA */
paul718e3742002-12-13 20:15:29 +00001446
1447/* For sockaddr_un. */
1448#include <sys/un.h>
1449
1450/* zebra server UNIX domain socket. */
paulb9df2d22004-05-09 09:09:59 +00001451static void
paul718e3742002-12-13 20:15:29 +00001452zebra_serv_un (char *path)
1453{
1454 int ret;
1455 int sock, len;
1456 struct sockaddr_un serv;
1457 mode_t old_mask;
1458
1459 /* First of all, unlink existing socket */
1460 unlink (path);
1461
1462 /* Set umask */
1463 old_mask = umask (0077);
1464
1465 /* Make UNIX domain socket. */
1466 sock = socket (AF_UNIX, SOCK_STREAM, 0);
1467 if (sock < 0)
1468 {
1469 perror ("sock");
1470 return;
1471 }
1472
1473 /* Make server socket. */
1474 memset (&serv, 0, sizeof (struct sockaddr_un));
1475 serv.sun_family = AF_UNIX;
1476 strncpy (serv.sun_path, path, strlen (path));
1477#ifdef HAVE_SUN_LEN
1478 len = serv.sun_len = SUN_LEN(&serv);
1479#else
1480 len = sizeof (serv.sun_family) + strlen (serv.sun_path);
1481#endif /* HAVE_SUN_LEN */
1482
1483 ret = bind (sock, (struct sockaddr *) &serv, len);
1484 if (ret < 0)
1485 {
1486 perror ("bind");
1487 close (sock);
1488 return;
1489 }
1490
1491 ret = listen (sock, 5);
1492 if (ret < 0)
1493 {
1494 perror ("listen");
1495 close (sock);
1496 return;
1497 }
1498
1499 umask (old_mask);
1500
1501 zebra_event (ZEBRA_SERV, sock, NULL);
1502}
1503
paul718e3742002-12-13 20:15:29 +00001504
paulb9df2d22004-05-09 09:09:59 +00001505static void
paul718e3742002-12-13 20:15:29 +00001506zebra_event (enum event event, int sock, struct zserv *client)
1507{
1508 switch (event)
1509 {
1510 case ZEBRA_SERV:
paulb21b19c2003-06-15 01:28:29 +00001511 thread_add_read (zebrad.master, zebra_accept, client, sock);
paul718e3742002-12-13 20:15:29 +00001512 break;
1513 case ZEBRA_READ:
1514 client->t_read =
paulb21b19c2003-06-15 01:28:29 +00001515 thread_add_read (zebrad.master, zebra_client_read, client, sock);
paul718e3742002-12-13 20:15:29 +00001516 break;
1517 case ZEBRA_WRITE:
1518 /**/
1519 break;
1520 }
1521}
1522
1523/* Display default rtm_table for all clients. */
1524DEFUN (show_table,
1525 show_table_cmd,
1526 "show table",
1527 SHOW_STR
1528 "default routing table to use for all clients\n")
1529{
paulb21b19c2003-06-15 01:28:29 +00001530 vty_out (vty, "table %d%s", zebrad.rtm_table_default,
paul718e3742002-12-13 20:15:29 +00001531 VTY_NEWLINE);
1532 return CMD_SUCCESS;
1533}
1534
1535DEFUN (config_table,
1536 config_table_cmd,
1537 "table TABLENO",
1538 "Configure target kernel routing table\n"
1539 "TABLE integer\n")
1540{
paulb21b19c2003-06-15 01:28:29 +00001541 zebrad.rtm_table_default = strtol (argv[0], (char**)0, 10);
paul718e3742002-12-13 20:15:29 +00001542 return CMD_SUCCESS;
1543}
1544
hasso647e4f12003-05-25 11:43:52 +00001545DEFUN (ip_forwarding,
1546 ip_forwarding_cmd,
1547 "ip forwarding",
1548 IP_STR
1549 "Turn on IP forwarding")
1550{
1551 int ret;
1552
1553 ret = ipforward ();
1554
1555 if (ret != 0)
1556 {
1557 vty_out (vty, "IP forwarding is already on%s", VTY_NEWLINE);
1558 return CMD_ERR_NOTHING_TODO;
1559 }
1560
1561 ret = ipforward_on ();
1562 if (ret == 0)
1563 {
1564 vty_out (vty, "Can't turn on IP forwarding%s", VTY_NEWLINE);
1565 return CMD_WARNING;
1566 }
1567
1568 return CMD_SUCCESS;
1569}
1570
paul718e3742002-12-13 20:15:29 +00001571DEFUN (no_ip_forwarding,
1572 no_ip_forwarding_cmd,
1573 "no ip forwarding",
1574 NO_STR
1575 IP_STR
1576 "Turn off IP forwarding")
1577{
1578 int ret;
1579
1580 ret = ipforward ();
1581
1582 if (ret == 0)
1583 {
1584 vty_out (vty, "IP forwarding is already off%s", VTY_NEWLINE);
1585 return CMD_ERR_NOTHING_TODO;
1586 }
1587
1588 ret = ipforward_off ();
1589 if (ret != 0)
1590 {
1591 vty_out (vty, "Can't turn off IP forwarding%s", VTY_NEWLINE);
1592 return CMD_WARNING;
1593 }
1594
1595 return CMD_SUCCESS;
1596}
1597
1598/* This command is for debugging purpose. */
1599DEFUN (show_zebra_client,
1600 show_zebra_client_cmd,
1601 "show zebra client",
1602 SHOW_STR
1603 "Zebra information"
1604 "Client information")
1605{
hasso52dc7ee2004-09-23 19:18:23 +00001606 struct listnode *node;
paul718e3742002-12-13 20:15:29 +00001607 struct zserv *client;
1608
paulb21b19c2003-06-15 01:28:29 +00001609 for (node = listhead (zebrad.client_list); node; nextnode (node))
paul718e3742002-12-13 20:15:29 +00001610 {
1611 client = getdata (node);
1612 vty_out (vty, "Client fd %d%s", client->sock, VTY_NEWLINE);
1613 }
1614 return CMD_SUCCESS;
1615}
1616
1617/* Table configuration write function. */
paulb9df2d22004-05-09 09:09:59 +00001618static int
paul718e3742002-12-13 20:15:29 +00001619config_write_table (struct vty *vty)
1620{
paulb21b19c2003-06-15 01:28:29 +00001621 if (zebrad.rtm_table_default)
1622 vty_out (vty, "table %d%s", zebrad.rtm_table_default,
paul718e3742002-12-13 20:15:29 +00001623 VTY_NEWLINE);
1624 return 0;
1625}
1626
1627/* table node for routing tables. */
1628struct cmd_node table_node =
1629{
1630 TABLE_NODE,
1631 "", /* This node has no interface. */
1632 1
1633};
1634
1635/* Only display ip forwarding is enabled or not. */
1636DEFUN (show_ip_forwarding,
1637 show_ip_forwarding_cmd,
1638 "show ip forwarding",
1639 SHOW_STR
1640 IP_STR
1641 "IP forwarding status\n")
1642{
1643 int ret;
1644
1645 ret = ipforward ();
1646
1647 if (ret == 0)
1648 vty_out (vty, "IP forwarding is off%s", VTY_NEWLINE);
1649 else
1650 vty_out (vty, "IP forwarding is on%s", VTY_NEWLINE);
1651 return CMD_SUCCESS;
1652}
1653
1654#ifdef HAVE_IPV6
1655/* Only display ipv6 forwarding is enabled or not. */
1656DEFUN (show_ipv6_forwarding,
1657 show_ipv6_forwarding_cmd,
1658 "show ipv6 forwarding",
1659 SHOW_STR
1660 "IPv6 information\n"
1661 "Forwarding status\n")
1662{
1663 int ret;
1664
1665 ret = ipforward_ipv6 ();
1666
1667 switch (ret)
1668 {
1669 case -1:
1670 vty_out (vty, "ipv6 forwarding is unknown%s", VTY_NEWLINE);
1671 break;
1672 case 0:
1673 vty_out (vty, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE);
1674 break;
1675 case 1:
1676 vty_out (vty, "ipv6 forwarding is %s%s", "on", VTY_NEWLINE);
1677 break;
1678 default:
1679 vty_out (vty, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE);
1680 break;
1681 }
1682 return CMD_SUCCESS;
1683}
1684
hasso55906722004-02-11 22:42:16 +00001685DEFUN (ipv6_forwarding,
1686 ipv6_forwarding_cmd,
1687 "ipv6 forwarding",
1688 IPV6_STR
1689 "Turn on IPv6 forwarding")
1690{
1691 int ret;
1692
hasso41d3fc92004-04-06 11:59:00 +00001693 ret = ipforward_ipv6 ();
hasso55906722004-02-11 22:42:16 +00001694 if (ret != 0)
1695 {
hasso41d3fc92004-04-06 11:59:00 +00001696 vty_out (vty, "IPv6 forwarding is already on%s", VTY_NEWLINE);
1697 return CMD_ERR_NOTHING_TODO;
1698 }
1699
1700 ret = ipforward_ipv6_on ();
1701 if (ret == 0)
1702 {
hasso55906722004-02-11 22:42:16 +00001703 vty_out (vty, "Can't turn on IPv6 forwarding%s", VTY_NEWLINE);
1704 return CMD_WARNING;
1705 }
1706
1707 return CMD_SUCCESS;
1708}
1709
paul718e3742002-12-13 20:15:29 +00001710DEFUN (no_ipv6_forwarding,
1711 no_ipv6_forwarding_cmd,
1712 "no ipv6 forwarding",
1713 NO_STR
hasso55906722004-02-11 22:42:16 +00001714 IPV6_STR
1715 "Turn off IPv6 forwarding")
paul718e3742002-12-13 20:15:29 +00001716{
1717 int ret;
1718
hasso41d3fc92004-04-06 11:59:00 +00001719 ret = ipforward_ipv6 ();
1720 if (ret == 0)
1721 {
1722 vty_out (vty, "IP forwarding is already off%s", VTY_NEWLINE);
1723 return CMD_ERR_NOTHING_TODO;
1724 }
1725
paul718e3742002-12-13 20:15:29 +00001726 ret = ipforward_ipv6_off ();
1727 if (ret != 0)
1728 {
1729 vty_out (vty, "Can't turn off IPv6 forwarding%s", VTY_NEWLINE);
1730 return CMD_WARNING;
1731 }
1732
1733 return CMD_SUCCESS;
1734}
1735
1736#endif /* HAVE_IPV6 */
1737
1738/* IPForwarding configuration write function. */
1739int
1740config_write_forwarding (struct vty *vty)
1741{
hasso18a6dce2004-10-03 18:18:34 +00001742 /* FIXME: Find better place for that. */
1743 router_id_write (vty);
1744
paul3e0b3a52004-08-23 18:58:32 +00001745 if (ipforward ())
1746 vty_out (vty, "ip forwarding%s", VTY_NEWLINE);
paul718e3742002-12-13 20:15:29 +00001747#ifdef HAVE_IPV6
paul3e0b3a52004-08-23 18:58:32 +00001748 if (ipforward_ipv6 ())
1749 vty_out (vty, "ipv6 forwarding%s", VTY_NEWLINE);
paul718e3742002-12-13 20:15:29 +00001750#endif /* HAVE_IPV6 */
1751 vty_out (vty, "!%s", VTY_NEWLINE);
1752 return 0;
1753}
1754
1755/* table node for routing tables. */
1756struct cmd_node forwarding_node =
1757{
1758 FORWARDING_NODE,
1759 "", /* This node has no interface. */
1760 1
1761};
1762
1763
1764/* Initialisation of zebra and installation of commands. */
1765void
1766zebra_init ()
1767{
1768 /* Client list init. */
paulb21b19c2003-06-15 01:28:29 +00001769 zebrad.client_list = list_new ();
paul718e3742002-12-13 20:15:29 +00001770
paul718e3742002-12-13 20:15:29 +00001771 /* Make zebra server socket. */
1772#ifdef HAVE_TCP_ZEBRA
1773 zebra_serv ();
1774#else
1775 zebra_serv_un (ZEBRA_SERV_PATH);
1776#endif /* HAVE_TCP_ZEBRA */
1777
1778 /* Install configuration write function. */
1779 install_node (&table_node, config_write_table);
1780 install_node (&forwarding_node, config_write_forwarding);
1781
1782 install_element (VIEW_NODE, &show_ip_forwarding_cmd);
1783 install_element (ENABLE_NODE, &show_ip_forwarding_cmd);
hasso647e4f12003-05-25 11:43:52 +00001784 install_element (CONFIG_NODE, &ip_forwarding_cmd);
paul718e3742002-12-13 20:15:29 +00001785 install_element (CONFIG_NODE, &no_ip_forwarding_cmd);
1786 install_element (ENABLE_NODE, &show_zebra_client_cmd);
1787
1788#ifdef HAVE_NETLINK
1789 install_element (VIEW_NODE, &show_table_cmd);
1790 install_element (ENABLE_NODE, &show_table_cmd);
1791 install_element (CONFIG_NODE, &config_table_cmd);
1792#endif /* HAVE_NETLINK */
1793
1794#ifdef HAVE_IPV6
1795 install_element (VIEW_NODE, &show_ipv6_forwarding_cmd);
1796 install_element (ENABLE_NODE, &show_ipv6_forwarding_cmd);
hasso55906722004-02-11 22:42:16 +00001797 install_element (CONFIG_NODE, &ipv6_forwarding_cmd);
paul718e3742002-12-13 20:15:29 +00001798 install_element (CONFIG_NODE, &no_ipv6_forwarding_cmd);
1799#endif /* HAVE_IPV6 */
paulccf35572003-03-01 11:42:20 +00001800
1801 FIFO_INIT(&message_queue);
1802 t_write = NULL;
paul718e3742002-12-13 20:15:29 +00001803}