blob: e74248b19afcf23c1f64c4aa21e5d3ec5c0c1ece [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. */
hassofce954f2004-10-07 20:29:24 +000054static const 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 }
hassofce954f2004-10-07 20:29:24 +0000172 /* It's clear that nbytes is positive at this point. */
173 else if ((unsigned) nbytes != length)
paulccf35572003-03-01 11:42:20 +0000174 zebra_server_enqueue (sock, buf, length, nbytes);
175
176 return 0;
177}
178
paul718e3742002-12-13 20:15:29 +0000179/* Interface is added. Send ZEBRA_INTERFACE_ADD to client. */
paulb9df2d22004-05-09 09:09:59 +0000180/*
181 * This function is called in the following situations:
182 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
183 * from the client.
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
188 * received)
189 */
paul718e3742002-12-13 20:15:29 +0000190int
191zsend_interface_add (struct zserv *client, struct interface *ifp)
192{
193 struct stream *s;
194
195 /* Check this client need interface information. */
196 if (! client->ifinfo)
197 return -1;
198
199 s = client->obuf;
200 stream_reset (s);
201
202 /* Place holder for size. */
203 stream_putw (s, 0);
204
205 /* Message type. */
206 stream_putc (s, ZEBRA_INTERFACE_ADD);
207
208 /* Interface information. */
209 stream_put (s, ifp->name, INTERFACE_NAMSIZ);
210 stream_putl (s, ifp->ifindex);
paul2e3b2e42002-12-13 21:03:13 +0000211 stream_putc (s, ifp->status);
paul718e3742002-12-13 20:15:29 +0000212 stream_putl (s, ifp->flags);
213 stream_putl (s, ifp->metric);
214 stream_putl (s, ifp->mtu);
paulb9df2d22004-05-09 09:09:59 +0000215 stream_putl (s, ifp->mtu6);
paul718e3742002-12-13 20:15:29 +0000216 stream_putl (s, ifp->bandwidth);
217#ifdef HAVE_SOCKADDR_DL
218 stream_put (s, &ifp->sdl, sizeof (ifp->sdl));
219#else
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 */
224
225 /* Write packet size. */
226 stream_putw_at (s, 0, stream_get_endp (s));
227
paulccf35572003-03-01 11:42:20 +0000228 zebra_server_send_message (client->sock, s->data, stream_get_endp (s));
229
230 return 0;
paul718e3742002-12-13 20:15:29 +0000231}
232
233/* Interface deletion from zebra daemon. */
paulb9df2d22004-05-09 09:09:59 +0000234/*
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.
238 */
239#if (defined(RTM_IFANNOUNCE) || defined(HAVE_NETLINK))
paul718e3742002-12-13 20:15:29 +0000240int
241zsend_interface_delete (struct zserv *client, struct interface *ifp)
242{
243 struct stream *s;
244
245 /* Check this client need interface information. */
246 if (! client->ifinfo)
247 return -1;
248
249 s = client->obuf;
250 stream_reset (s);
251
252 /* Packet length placeholder. */
253 stream_putw (s, 0);
254
255 /* Interface information. */
256 stream_putc (s, ZEBRA_INTERFACE_DELETE);
257 stream_put (s, ifp->name, INTERFACE_NAMSIZ);
258 stream_putl (s, ifp->ifindex);
paul2e3b2e42002-12-13 21:03:13 +0000259 stream_putc (s, ifp->status);
paul718e3742002-12-13 20:15:29 +0000260 stream_putl (s, ifp->flags);
261 stream_putl (s, ifp->metric);
262 stream_putl (s, ifp->mtu);
paulb9df2d22004-05-09 09:09:59 +0000263 stream_putl (s, ifp->mtu6);
paul718e3742002-12-13 20:15:29 +0000264 stream_putl (s, ifp->bandwidth);
265
266 /* Write packet length. */
267 stream_putw_at (s, 0, stream_get_endp (s));
268
paulccf35572003-03-01 11:42:20 +0000269 zebra_server_send_message (client->sock, s->data, stream_get_endp (s));
270
271 return 0;
paul718e3742002-12-13 20:15:29 +0000272}
paulb9df2d22004-05-09 09:09:59 +0000273#endif /* (defined(RTM_IFANNOUNCE) || defined(HAVE_LINUX_RTNETLINK_H)) */
paul718e3742002-12-13 20:15:29 +0000274
paulb9df2d22004-05-09 09:09:59 +0000275/* Interface address is added/deleted. Send ZEBRA_INTERFACE_ADDRESS_ADD or
276 * ZEBRA_INTERFACE_ADDRESS_DELETE to the client.
277 *
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
287 * received)
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,
291 *
292 * The call tree that triggers ZEBRA_INTERFACE_ADDRESS_DELETE:
293 *
294 * zsend_interface_address(DELETE)
295 * ^
296 * |
297 * zebra_interface_address_delete_update
298 * ^ ^ ^
299 * | | if_delete_update (not called on
300 * | | Solaris)
301 * ip_address_uninstall connected_delete_ipv4
302 * [ipv6_addresss_uninstall] [connected_delete_ipv6]
303 * ^ ^
304 * | |
305 * | RTM_NEWADDR on routing/netlink socket
306 * |
307 * vty commands:
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"]
311 *
312 */
paul718e3742002-12-13 20:15:29 +0000313int
paulb9df2d22004-05-09 09:09:59 +0000314zsend_interface_address (int cmd, struct zserv *client,
315 struct interface *ifp, struct connected *ifc)
paul718e3742002-12-13 20:15:29 +0000316{
317 int blen;
318 struct stream *s;
319 struct prefix *p;
320
321 /* Check this client need interface information. */
322 if (! client->ifinfo)
323 return -1;
324
325 s = client->obuf;
326 stream_reset (s);
327
328 /* Place holder for size. */
329 stream_putw (s, 0);
330
paulb9df2d22004-05-09 09:09:59 +0000331 stream_putc (s, cmd);
paul718e3742002-12-13 20:15:29 +0000332 stream_putl (s, ifp->ifindex);
333
334 /* Interface address flag. */
335 stream_putc (s, ifc->flags);
336
337 /* Prefix information. */
338 p = ifc->address;
339 stream_putc (s, p->family);
340 blen = prefix_blen (p);
341 stream_put (s, &p->u.prefix, blen);
paulb9df2d22004-05-09 09:09:59 +0000342
343 /*
344 * XXX gnu version does not send prefixlen for ZEBRA_INTERFACE_ADDRESS_DELETE
345 * but zebra_interface_address_delete_read() in the gnu version
346 * expects to find it
347 */
paul718e3742002-12-13 20:15:29 +0000348 stream_putc (s, p->prefixlen);
349
350 /* Destination. */
351 p = ifc->destination;
352 if (p)
353 stream_put (s, &p->u.prefix, blen);
354 else
355 stream_put (s, NULL, blen);
356
357 /* Write packet size. */
358 stream_putw_at (s, 0, stream_get_endp (s));
359
paulccf35572003-03-01 11:42:20 +0000360 zebra_server_send_message (client->sock, s->data, stream_get_endp (s));
361
362 return 0;
paul718e3742002-12-13 20:15:29 +0000363}
364
paulb9df2d22004-05-09 09:09:59 +0000365/*
366 * The cmd passed to zsend_interface_update may be ZEBRA_INTERFACE_UP or
367 * ZEBRA_INTERFACE_DOWN.
368 *
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.
374 */
paul718e3742002-12-13 20:15:29 +0000375int
paulb9df2d22004-05-09 09:09:59 +0000376zsend_interface_update (int cmd, struct zserv *client, struct interface *ifp)
paul718e3742002-12-13 20:15:29 +0000377{
378 struct stream *s;
379
380 /* Check this client need interface information. */
381 if (! client->ifinfo)
382 return -1;
383
384 s = client->obuf;
385 stream_reset (s);
386
387 /* Place holder for size. */
388 stream_putw (s, 0);
389
390 /* Zebra command. */
paulb9df2d22004-05-09 09:09:59 +0000391 stream_putc (s, cmd);
paul718e3742002-12-13 20:15:29 +0000392
393 /* Interface information. */
394 stream_put (s, ifp->name, INTERFACE_NAMSIZ);
395 stream_putl (s, ifp->ifindex);
paul2e3b2e42002-12-13 21:03:13 +0000396 stream_putc (s, ifp->status);
paul718e3742002-12-13 20:15:29 +0000397 stream_putl (s, ifp->flags);
398 stream_putl (s, ifp->metric);
399 stream_putl (s, ifp->mtu);
paulb9df2d22004-05-09 09:09:59 +0000400 stream_putl (s, ifp->mtu6);
paul718e3742002-12-13 20:15:29 +0000401 stream_putl (s, ifp->bandwidth);
402
403 /* Write packet size. */
404 stream_putw_at (s, 0, stream_get_endp (s));
405
paulccf35572003-03-01 11:42:20 +0000406 zebra_server_send_message (client->sock, s->data, stream_get_endp (s));
407
408 return 0;
paul718e3742002-12-13 20:15:29 +0000409}
410
paulb9df2d22004-05-09 09:09:59 +0000411/*
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
414 * situations:
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
420 *
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
426 *
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
432 * duplication.
433 */
paul718e3742002-12-13 20:15:29 +0000434int
paulb9df2d22004-05-09 09:09:59 +0000435zsend_route_multipath (int cmd, struct zserv *client, struct prefix *p,
436 struct rib *rib)
paul718e3742002-12-13 20:15:29 +0000437{
438 int psize;
439 struct stream *s;
440 struct nexthop *nexthop;
paulb9df2d22004-05-09 09:09:59 +0000441 unsigned long nhnummark = 0;
442 int nhnum = 0;
443 u_char zapi_flags = ZAPI_MESSAGE_NEXTHOP | ZAPI_MESSAGE_IFINDEX;
444
paul718e3742002-12-13 20:15:29 +0000445 s = client->obuf;
446 stream_reset (s);
447
448 /* Place holder for size. */
449 stream_putw (s, 0);
450
451 /* Put command, type and nexthop. */
paulb9df2d22004-05-09 09:09:59 +0000452 stream_putc (s, cmd);
paul718e3742002-12-13 20:15:29 +0000453 stream_putc (s, rib->type);
454 stream_putc (s, rib->flags);
paulb9df2d22004-05-09 09:09:59 +0000455
456 /*
457 * XXX no need to set ZAPI_MESSAGE_NEXTHOP if we are going to
458 * send empty nexthop?
459 */
460 if (cmd == ZEBRA_IPV4_ROUTE_ADD || ZEBRA_IPV6_ROUTE_ADD)
461 zapi_flags |= ZAPI_MESSAGE_METRIC;
462
463 stream_putc (s, zapi_flags);
paul718e3742002-12-13 20:15:29 +0000464
465 /* Prefix. */
466 psize = PSIZE (p->prefixlen);
467 stream_putc (s, p->prefixlen);
paulb9df2d22004-05-09 09:09:59 +0000468 stream_write (s, (u_char *) & p->u.prefix, psize);
paul718e3742002-12-13 20:15:29 +0000469
paulb9df2d22004-05-09 09:09:59 +0000470 /*
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
476 * is hard-coded.
477 */
paul718e3742002-12-13 20:15:29 +0000478 /* Nexthop */
479 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
480 {
481 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB))
paulb9df2d22004-05-09 09:09:59 +0000482 {
paul9985f832005-02-09 15:51:56 +0000483 nhnummark = stream_get_endp (s);
paulb9df2d22004-05-09 09:09:59 +0000484 stream_putc (s, 1); /* placeholder */
485 nhnum++;
paul718e3742002-12-13 20:15:29 +0000486
paulb9df2d22004-05-09 09:09:59 +0000487 switch(nexthop->type)
488 {
489 case NEXTHOP_TYPE_IPV4:
490 case NEXTHOP_TYPE_IPV4_IFINDEX:
491 stream_put_in_addr (s, &nexthop->gate.ipv4);
492 break;
493#ifdef HAVE_IPV6
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);
498 break;
499#endif
500 default:
501 if (cmd == ZEBRA_IPV4_ROUTE_ADD
502 || cmd == ZEBRA_IPV4_ROUTE_DELETE)
503 {
504 struct in_addr empty;
paul44983cf2004-09-22 13:15:58 +0000505 memset (&empty, 0, sizeof (struct in_addr));
paulb9df2d22004-05-09 09:09:59 +0000506 stream_write (s, (u_char *) &empty, IPV4_MAX_BYTELEN);
507 }
508 else
509 {
510 struct in6_addr empty;
511 memset (&empty, 0, sizeof (struct in6_addr));
512 stream_write (s, (u_char *) &empty, IPV6_MAX_BYTELEN);
513 }
514 }
paul718e3742002-12-13 20:15:29 +0000515
paulb9df2d22004-05-09 09:09:59 +0000516 /* Interface index. */
517 stream_putc (s, 1);
518 stream_putl (s, nexthop->ifindex);
paul718e3742002-12-13 20:15:29 +0000519
paulb9df2d22004-05-09 09:09:59 +0000520 break;
521 }
paul718e3742002-12-13 20:15:29 +0000522 }
523
524 /* Metric */
525 stream_putl (s, rib->metric);
526
paulb9df2d22004-05-09 09:09:59 +0000527 /* Write next-hop number */
528 if (nhnummark)
hassoc1eaa442004-10-19 06:26:01 +0000529 stream_putc_at (s, nhnummark, nhnum);
paulb9df2d22004-05-09 09:09:59 +0000530
paul718e3742002-12-13 20:15:29 +0000531 /* Write packet size. */
532 stream_putw_at (s, 0, stream_get_endp (s));
533
paulccf35572003-03-01 11:42:20 +0000534 zebra_server_send_message (client->sock, s->data, stream_get_endp (s));
535
536 return 0;
paul718e3742002-12-13 20:15:29 +0000537}
538
paul718e3742002-12-13 20:15:29 +0000539#ifdef HAVE_IPV6
paul718e3742002-12-13 20:15:29 +0000540int
541zsend_ipv6_nexthop_lookup (struct zserv *client, struct in6_addr *addr)
542{
543 struct stream *s;
544 struct rib *rib;
545 unsigned long nump;
546 u_char num;
547 struct nexthop *nexthop;
548
549 /* Lookup nexthop. */
550 rib = rib_match_ipv6 (addr);
551
552 /* Get output stream. */
553 s = client->obuf;
554 stream_reset (s);
555
556 /* Fill in result. */
557 stream_putw (s, 0);
558 stream_putc (s, ZEBRA_IPV6_NEXTHOP_LOOKUP);
559 stream_put (s, &addr, 16);
560
561 if (rib)
562 {
563 stream_putl (s, rib->metric);
564 num = 0;
paul9985f832005-02-09 15:51:56 +0000565 nump = stream_get_endp(s);
paul718e3742002-12-13 20:15:29 +0000566 stream_putc (s, 0);
567 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
568 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB))
569 {
570 stream_putc (s, nexthop->type);
571 switch (nexthop->type)
572 {
573 case ZEBRA_NEXTHOP_IPV6:
574 stream_put (s, &nexthop->gate.ipv6, 16);
575 break;
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);
580 break;
581 case ZEBRA_NEXTHOP_IFINDEX:
582 case ZEBRA_NEXTHOP_IFNAME:
583 stream_putl (s, nexthop->ifindex);
584 break;
hassofa2b17e2004-03-04 17:45:00 +0000585 default:
586 /* do nothing */
587 break;
paul718e3742002-12-13 20:15:29 +0000588 }
589 num++;
590 }
591 stream_putc_at (s, nump, num);
592 }
593 else
594 {
595 stream_putl (s, 0);
596 stream_putc (s, 0);
597 }
598
599 stream_putw_at (s, 0, stream_get_endp (s));
600
paulccf35572003-03-01 11:42:20 +0000601 zebra_server_send_message (client->sock, s->data, stream_get_endp (s));
602
603 return 0;
paul718e3742002-12-13 20:15:29 +0000604}
605#endif /* HAVE_IPV6 */
606
paulb9df2d22004-05-09 09:09:59 +0000607static int
paul718e3742002-12-13 20:15:29 +0000608zsend_ipv4_nexthop_lookup (struct zserv *client, struct in_addr addr)
609{
610 struct stream *s;
611 struct rib *rib;
612 unsigned long nump;
613 u_char num;
614 struct nexthop *nexthop;
615
616 /* Lookup nexthop. */
617 rib = rib_match_ipv4 (addr);
618
619 /* Get output stream. */
620 s = client->obuf;
621 stream_reset (s);
622
623 /* Fill in result. */
624 stream_putw (s, 0);
625 stream_putc (s, ZEBRA_IPV4_NEXTHOP_LOOKUP);
626 stream_put_in_addr (s, &addr);
627
628 if (rib)
629 {
630 stream_putl (s, rib->metric);
631 num = 0;
paul9985f832005-02-09 15:51:56 +0000632 nump = stream_get_endp(s);
paul718e3742002-12-13 20:15:29 +0000633 stream_putc (s, 0);
634 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
635 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB))
636 {
637 stream_putc (s, nexthop->type);
638 switch (nexthop->type)
639 {
640 case ZEBRA_NEXTHOP_IPV4:
641 stream_put_in_addr (s, &nexthop->gate.ipv4);
642 break;
643 case ZEBRA_NEXTHOP_IFINDEX:
644 case ZEBRA_NEXTHOP_IFNAME:
645 stream_putl (s, nexthop->ifindex);
646 break;
hassofa2b17e2004-03-04 17:45:00 +0000647 default:
648 /* do nothing */
649 break;
paul718e3742002-12-13 20:15:29 +0000650 }
651 num++;
652 }
653 stream_putc_at (s, nump, num);
654 }
655 else
656 {
657 stream_putl (s, 0);
658 stream_putc (s, 0);
659 }
660
661 stream_putw_at (s, 0, stream_get_endp (s));
662
paulccf35572003-03-01 11:42:20 +0000663 zebra_server_send_message (client->sock, s->data, stream_get_endp (s));
664
665 return 0;
paul718e3742002-12-13 20:15:29 +0000666}
667
paulb9df2d22004-05-09 09:09:59 +0000668static int
paul718e3742002-12-13 20:15:29 +0000669zsend_ipv4_import_lookup (struct zserv *client, struct prefix_ipv4 *p)
670{
671 struct stream *s;
672 struct rib *rib;
673 unsigned long nump;
674 u_char num;
675 struct nexthop *nexthop;
676
677 /* Lookup nexthop. */
678 rib = rib_lookup_ipv4 (p);
679
680 /* Get output stream. */
681 s = client->obuf;
682 stream_reset (s);
683
684 /* Fill in result. */
685 stream_putw (s, 0);
686 stream_putc (s, ZEBRA_IPV4_IMPORT_LOOKUP);
687 stream_put_in_addr (s, &p->prefix);
688
689 if (rib)
690 {
691 stream_putl (s, rib->metric);
692 num = 0;
paul9985f832005-02-09 15:51:56 +0000693 nump = stream_get_endp(s);
paul718e3742002-12-13 20:15:29 +0000694 stream_putc (s, 0);
695 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
696 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB))
697 {
698 stream_putc (s, nexthop->type);
699 switch (nexthop->type)
700 {
701 case ZEBRA_NEXTHOP_IPV4:
702 stream_put_in_addr (s, &nexthop->gate.ipv4);
703 break;
704 case ZEBRA_NEXTHOP_IFINDEX:
705 case ZEBRA_NEXTHOP_IFNAME:
706 stream_putl (s, nexthop->ifindex);
707 break;
hassofa2b17e2004-03-04 17:45:00 +0000708 default:
709 /* do nothing */
710 break;
paul718e3742002-12-13 20:15:29 +0000711 }
712 num++;
713 }
714 stream_putc_at (s, nump, num);
715 }
716 else
717 {
718 stream_putl (s, 0);
719 stream_putc (s, 0);
720 }
721
722 stream_putw_at (s, 0, stream_get_endp (s));
723
paulccf35572003-03-01 11:42:20 +0000724 zebra_server_send_message (client->sock, s->data, stream_get_endp (s));
725
726 return 0;
paul718e3742002-12-13 20:15:29 +0000727}
728
hasso18a6dce2004-10-03 18:18:34 +0000729/* Router-id is updated. Send ZEBRA_ROUTER_ID_ADD to client. */
730int
731zsend_router_id_update (struct zserv *client, struct prefix *p)
732{
733 struct stream *s;
734 int blen;
735
736 /* Check this client need interface information. */
737 if (!client->ridinfo)
738 return -1;
739
740 s = client->obuf;
741 stream_reset (s);
742
743 /* Place holder for size. */
744 stream_putw (s, 0);
745
746 /* Message type. */
747 stream_putc (s, ZEBRA_ROUTER_ID_UPDATE);
748
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);
754
755 /* Write packet size. */
756 stream_putw_at (s, 0, stream_get_endp (s));
757
758 return writen (client->sock, s->data, stream_get_endp (s));
759}
760
paul718e3742002-12-13 20:15:29 +0000761/* Register zebra server interface information. Send current all
762 interface and address information. */
paulb9df2d22004-05-09 09:09:59 +0000763static void
paul718e3742002-12-13 20:15:29 +0000764zread_interface_add (struct zserv *client, u_short length)
765{
hasso52dc7ee2004-09-23 19:18:23 +0000766 struct listnode *ifnode;
767 struct listnode *cnode;
paul718e3742002-12-13 20:15:29 +0000768 struct interface *ifp;
769 struct connected *c;
770
771 /* Interface information is needed. */
772 client->ifinfo = 1;
773
774 for (ifnode = listhead (iflist); ifnode; ifnode = nextnode (ifnode))
775 {
776 ifp = getdata (ifnode);
777
778 /* Skip pseudo interface. */
779 if (! CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE))
780 continue;
781
782 zsend_interface_add (client, ifp);
783
784 for (cnode = listhead (ifp->connected); cnode; nextnode (cnode))
785 {
786 c = getdata (cnode);
787 if (CHECK_FLAG (c->conf, ZEBRA_IFC_REAL))
paulb9df2d22004-05-09 09:09:59 +0000788 zsend_interface_address (ZEBRA_INTERFACE_ADDRESS_ADD, client,
789 ifp, c);
paul718e3742002-12-13 20:15:29 +0000790 }
791 }
792}
793
794/* Unregister zebra server interface information. */
paulb9df2d22004-05-09 09:09:59 +0000795static void
paul718e3742002-12-13 20:15:29 +0000796zread_interface_delete (struct zserv *client, u_short length)
797{
798 client->ifinfo = 0;
799}
800
801/* This function support multiple nexthop. */
paulb9df2d22004-05-09 09:09:59 +0000802/*
803 * Parse the ZEBRA_IPV4_ROUTE_ADD sent from client. Update rib and
804 * add kernel route.
805 */
806static void
paul718e3742002-12-13 20:15:29 +0000807zread_ipv4_add (struct zserv *client, u_short length)
808{
809 int i;
810 struct rib *rib;
811 struct prefix_ipv4 p;
812 u_char message;
813 struct in_addr nexthop;
814 u_char nexthop_num;
815 u_char nexthop_type;
816 struct stream *s;
817 unsigned int ifindex;
818 u_char ifname_len;
819
820 /* Get input stream. */
821 s = client->ibuf;
822
823 /* Allocate new rib. */
824 rib = XMALLOC (MTYPE_RIB, sizeof (struct rib));
825 memset (rib, 0, sizeof (struct rib));
826
827 /* Type, flags, message. */
828 rib->type = stream_getc (s);
829 rib->flags = stream_getc (s);
paulb9df2d22004-05-09 09:09:59 +0000830 message = stream_getc (s);
paul718e3742002-12-13 20:15:29 +0000831 rib->uptime = time (NULL);
832
833 /* IPv4 prefix. */
834 memset (&p, 0, sizeof (struct prefix_ipv4));
835 p.family = AF_INET;
836 p.prefixlen = stream_getc (s);
837 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
838
839 /* Nexthop parse. */
840 if (CHECK_FLAG (message, ZAPI_MESSAGE_NEXTHOP))
841 {
842 nexthop_num = stream_getc (s);
843
844 for (i = 0; i < nexthop_num; i++)
845 {
846 nexthop_type = stream_getc (s);
847
848 switch (nexthop_type)
849 {
850 case ZEBRA_NEXTHOP_IFINDEX:
851 ifindex = stream_getl (s);
852 nexthop_ifindex_add (rib, ifindex);
853 break;
854 case ZEBRA_NEXTHOP_IFNAME:
855 ifname_len = stream_getc (s);
paul9985f832005-02-09 15:51:56 +0000856 stream_forward_getp (s, ifname_len);
paul718e3742002-12-13 20:15:29 +0000857 break;
858 case ZEBRA_NEXTHOP_IPV4:
859 nexthop.s_addr = stream_get_ipv4 (s);
860 nexthop_ipv4_add (rib, &nexthop);
861 break;
862 case ZEBRA_NEXTHOP_IPV6:
paul9985f832005-02-09 15:51:56 +0000863 stream_forward_getp (s, IPV6_MAX_BYTELEN);
paul718e3742002-12-13 20:15:29 +0000864 break;
paul595db7f2003-05-25 21:35:06 +0000865 case ZEBRA_NEXTHOP_BLACKHOLE:
866 nexthop_blackhole_add (rib);
867 break;
paul718e3742002-12-13 20:15:29 +0000868 }
869 }
870 }
871
872 /* Distance. */
873 if (CHECK_FLAG (message, ZAPI_MESSAGE_DISTANCE))
874 rib->distance = stream_getc (s);
875
876 /* Metric. */
877 if (CHECK_FLAG (message, ZAPI_MESSAGE_METRIC))
878 rib->metric = stream_getl (s);
879
880 rib_add_ipv4_multipath (&p, rib);
881}
882
883/* Zebra server IPv4 prefix delete function. */
paulb9df2d22004-05-09 09:09:59 +0000884static void
paul718e3742002-12-13 20:15:29 +0000885zread_ipv4_delete (struct zserv *client, u_short length)
886{
887 int i;
888 struct stream *s;
889 struct zapi_ipv4 api;
890 struct in_addr nexthop;
891 unsigned long ifindex;
892 struct prefix_ipv4 p;
893 u_char nexthop_num;
894 u_char nexthop_type;
895 u_char ifname_len;
896
897 s = client->ibuf;
898 ifindex = 0;
899 nexthop.s_addr = 0;
900
901 /* Type, flags, message. */
902 api.type = stream_getc (s);
903 api.flags = stream_getc (s);
904 api.message = stream_getc (s);
905
906 /* IPv4 prefix. */
907 memset (&p, 0, sizeof (struct prefix_ipv4));
908 p.family = AF_INET;
909 p.prefixlen = stream_getc (s);
910 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
911
912 /* Nexthop, ifindex, distance, metric. */
913 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
914 {
915 nexthop_num = stream_getc (s);
916
917 for (i = 0; i < nexthop_num; i++)
918 {
919 nexthop_type = stream_getc (s);
920
921 switch (nexthop_type)
922 {
923 case ZEBRA_NEXTHOP_IFINDEX:
924 ifindex = stream_getl (s);
925 break;
926 case ZEBRA_NEXTHOP_IFNAME:
927 ifname_len = stream_getc (s);
paul9985f832005-02-09 15:51:56 +0000928 stream_forward_getp (s, ifname_len);
paul718e3742002-12-13 20:15:29 +0000929 break;
930 case ZEBRA_NEXTHOP_IPV4:
931 nexthop.s_addr = stream_get_ipv4 (s);
932 break;
933 case ZEBRA_NEXTHOP_IPV6:
paul9985f832005-02-09 15:51:56 +0000934 stream_forward_getp (s, IPV6_MAX_BYTELEN);
paul718e3742002-12-13 20:15:29 +0000935 break;
936 }
937 }
938 }
939
940 /* Distance. */
941 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
942 api.distance = stream_getc (s);
943 else
944 api.distance = 0;
945
946 /* Metric. */
947 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
948 api.metric = stream_getl (s);
949 else
950 api.metric = 0;
951
952 rib_delete_ipv4 (api.type, api.flags, &p, &nexthop, ifindex,
953 client->rtm_table);
954}
955
956/* Nexthop lookup for IPv4. */
paulb9df2d22004-05-09 09:09:59 +0000957static void
paul718e3742002-12-13 20:15:29 +0000958zread_ipv4_nexthop_lookup (struct zserv *client, u_short length)
959{
960 struct in_addr addr;
961
962 addr.s_addr = stream_get_ipv4 (client->ibuf);
963 zsend_ipv4_nexthop_lookup (client, addr);
964}
965
966/* Nexthop lookup for IPv4. */
paulb9df2d22004-05-09 09:09:59 +0000967static void
paul718e3742002-12-13 20:15:29 +0000968zread_ipv4_import_lookup (struct zserv *client, u_short length)
969{
970 struct prefix_ipv4 p;
971
972 p.family = AF_INET;
973 p.prefixlen = stream_getc (client->ibuf);
974 p.prefix.s_addr = stream_get_ipv4 (client->ibuf);
975
976 zsend_ipv4_import_lookup (client, &p);
977}
978
979#ifdef HAVE_IPV6
980/* Zebra server IPv6 prefix add function. */
paulb9df2d22004-05-09 09:09:59 +0000981static void
paul718e3742002-12-13 20:15:29 +0000982zread_ipv6_add (struct zserv *client, u_short length)
983{
984 int i;
985 struct stream *s;
986 struct zapi_ipv6 api;
987 struct in6_addr nexthop;
988 unsigned long ifindex;
989 struct prefix_ipv6 p;
990
991 s = client->ibuf;
992 ifindex = 0;
993 memset (&nexthop, 0, sizeof (struct in6_addr));
994
995 /* Type, flags, message. */
996 api.type = stream_getc (s);
997 api.flags = stream_getc (s);
998 api.message = stream_getc (s);
999
1000 /* IPv4 prefix. */
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));
1005
1006 /* Nexthop, ifindex, distance, metric. */
1007 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
1008 {
1009 u_char nexthop_type;
1010
1011 api.nexthop_num = stream_getc (s);
1012 for (i = 0; i < api.nexthop_num; i++)
1013 {
1014 nexthop_type = stream_getc (s);
1015
1016 switch (nexthop_type)
1017 {
1018 case ZEBRA_NEXTHOP_IPV6:
1019 stream_get (&nexthop, s, 16);
1020 break;
1021 case ZEBRA_NEXTHOP_IFINDEX:
1022 ifindex = stream_getl (s);
1023 break;
1024 }
1025 }
1026 }
1027
1028 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
1029 api.distance = stream_getc (s);
1030 else
1031 api.distance = 0;
1032
1033 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
1034 api.metric = stream_getl (s);
1035 else
1036 api.metric = 0;
1037
1038 if (IN6_IS_ADDR_UNSPECIFIED (&nexthop))
1039 rib_add_ipv6 (api.type, api.flags, &p, NULL, ifindex, 0);
1040 else
1041 rib_add_ipv6 (api.type, api.flags, &p, &nexthop, ifindex, 0);
1042}
1043
1044/* Zebra server IPv6 prefix delete function. */
paulb9df2d22004-05-09 09:09:59 +00001045static void
paul718e3742002-12-13 20:15:29 +00001046zread_ipv6_delete (struct zserv *client, u_short length)
1047{
1048 int i;
1049 struct stream *s;
1050 struct zapi_ipv6 api;
1051 struct in6_addr nexthop;
1052 unsigned long ifindex;
1053 struct prefix_ipv6 p;
1054
1055 s = client->ibuf;
1056 ifindex = 0;
1057 memset (&nexthop, 0, sizeof (struct in6_addr));
1058
1059 /* Type, flags, message. */
1060 api.type = stream_getc (s);
1061 api.flags = stream_getc (s);
1062 api.message = stream_getc (s);
1063
1064 /* IPv4 prefix. */
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));
1069
1070 /* Nexthop, ifindex, distance, metric. */
1071 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
1072 {
1073 u_char nexthop_type;
1074
1075 api.nexthop_num = stream_getc (s);
1076 for (i = 0; i < api.nexthop_num; i++)
1077 {
1078 nexthop_type = stream_getc (s);
1079
1080 switch (nexthop_type)
1081 {
1082 case ZEBRA_NEXTHOP_IPV6:
1083 stream_get (&nexthop, s, 16);
1084 break;
1085 case ZEBRA_NEXTHOP_IFINDEX:
1086 ifindex = stream_getl (s);
1087 break;
1088 }
1089 }
1090 }
1091
1092 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
1093 api.distance = stream_getc (s);
1094 else
1095 api.distance = 0;
1096 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
1097 api.metric = stream_getl (s);
1098 else
1099 api.metric = 0;
1100
1101 if (IN6_IS_ADDR_UNSPECIFIED (&nexthop))
1102 rib_delete_ipv6 (api.type, api.flags, &p, NULL, ifindex, 0);
1103 else
1104 rib_delete_ipv6 (api.type, api.flags, &p, &nexthop, ifindex, 0);
1105}
1106
1107void
1108zebra_read_ipv6 (int command, struct zserv *client, u_short length)
1109{
1110 u_char type;
1111 u_char flags;
1112 struct in6_addr nexthop, *gate;
paul718e3742002-12-13 20:15:29 +00001113 unsigned int ifindex;
1114
paul718e3742002-12-13 20:15:29 +00001115 type = stream_getc (client->ibuf);
1116 flags = stream_getc (client->ibuf);
1117 stream_get (&nexthop, client->ibuf, sizeof (struct in6_addr));
1118
paule31f2292005-02-19 02:00:26 +00001119 while (STREAM_READABLE (client->ibuf))
paul718e3742002-12-13 20:15:29 +00001120 {
1121 int size;
1122 struct prefix_ipv6 p;
1123
1124 ifindex = stream_getl (client->ibuf);
1125
1126 memset (&p, 0, sizeof (struct prefix_ipv6));
1127 p.family = AF_INET6;
1128 p.prefixlen = stream_getc (client->ibuf);
1129 size = PSIZE(p.prefixlen);
1130 stream_get (&p.prefix, client->ibuf, size);
1131
1132 if (IN6_IS_ADDR_UNSPECIFIED (&nexthop))
1133 gate = NULL;
1134 else
1135 gate = &nexthop;
1136
1137 if (command == ZEBRA_IPV6_ROUTE_ADD)
paulb9df2d22004-05-09 09:09:59 +00001138 rib_add_ipv6 (type, flags, &p, gate, ifindex, 0);
paul718e3742002-12-13 20:15:29 +00001139 else
paulb9df2d22004-05-09 09:09:59 +00001140 rib_delete_ipv6 (type, flags, &p, gate, ifindex, 0);
paul718e3742002-12-13 20:15:29 +00001141 }
1142}
1143
paulb9df2d22004-05-09 09:09:59 +00001144static void
paul718e3742002-12-13 20:15:29 +00001145zread_ipv6_nexthop_lookup (struct zserv *client, u_short length)
1146{
1147 struct in6_addr addr;
1148 char buf[BUFSIZ];
1149
1150 stream_get (&addr, client->ibuf, 16);
1151 printf ("DEBUG %s\n", inet_ntop (AF_INET6, &addr, buf, BUFSIZ));
1152
1153 zsend_ipv6_nexthop_lookup (client, &addr);
1154}
1155#endif /* HAVE_IPV6 */
1156
hasso18a6dce2004-10-03 18:18:34 +00001157/* Register zebra server router-id information. Send current router-id */
1158void
1159zread_router_id_add (struct zserv *client, u_short length)
1160{
1161 struct prefix p;
1162
1163 /* Router-id information is needed. */
1164 client->ridinfo = 1;
1165
1166 router_id_get (&p);
1167
1168 zsend_router_id_update (client,&p);
1169}
1170
1171/* Unregister zebra server router-id information. */
1172void
1173zread_router_id_delete (struct zserv *client, u_short length)
1174{
1175 client->ridinfo = 0;
1176}
1177
paul718e3742002-12-13 20:15:29 +00001178/* Close zebra client. */
paulb9df2d22004-05-09 09:09:59 +00001179static void
paul718e3742002-12-13 20:15:29 +00001180zebra_client_close (struct zserv *client)
1181{
1182 /* Close file descriptor. */
1183 if (client->sock)
1184 {
1185 close (client->sock);
1186 client->sock = -1;
1187 }
1188
1189 /* Free stream buffers. */
1190 if (client->ibuf)
1191 stream_free (client->ibuf);
1192 if (client->obuf)
1193 stream_free (client->obuf);
1194
1195 /* Release threads. */
1196 if (client->t_read)
1197 thread_cancel (client->t_read);
1198 if (client->t_write)
1199 thread_cancel (client->t_write);
1200
1201 /* Free client structure. */
paulb21b19c2003-06-15 01:28:29 +00001202 listnode_delete (zebrad.client_list, client);
paul718e3742002-12-13 20:15:29 +00001203 XFREE (0, client);
1204}
1205
1206/* Make new client. */
paulb9df2d22004-05-09 09:09:59 +00001207static void
paul718e3742002-12-13 20:15:29 +00001208zebra_client_create (int sock)
1209{
1210 struct zserv *client;
1211
1212 client = XCALLOC (0, sizeof (struct zserv));
1213
1214 /* Make client input/output buffer. */
1215 client->sock = sock;
1216 client->ibuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
1217 client->obuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
1218
1219 /* Set table number. */
paulb21b19c2003-06-15 01:28:29 +00001220 client->rtm_table = zebrad.rtm_table_default;
paul718e3742002-12-13 20:15:29 +00001221
1222 /* Add this client to linked list. */
paulb21b19c2003-06-15 01:28:29 +00001223 listnode_add (zebrad.client_list, client);
paul718e3742002-12-13 20:15:29 +00001224
1225 /* Make new read thread. */
1226 zebra_event (ZEBRA_READ, sock, client);
1227}
1228
1229/* Handler of zebra service request. */
paulb9df2d22004-05-09 09:09:59 +00001230static int
paul718e3742002-12-13 20:15:29 +00001231zebra_client_read (struct thread *thread)
1232{
1233 int sock;
1234 struct zserv *client;
1235 int nbyte;
1236 u_short length;
1237 u_char command;
1238
1239 /* Get thread data. Reset reading thread because I'm running. */
1240 sock = THREAD_FD (thread);
1241 client = THREAD_ARG (thread);
1242 client->t_read = NULL;
1243
1244 /* Read length and command. */
1245 nbyte = stream_read (client->ibuf, sock, 3);
1246 if (nbyte <= 0)
1247 {
1248 if (IS_ZEBRA_DEBUG_EVENT)
ajsb6178002004-12-07 21:12:56 +00001249 zlog_debug ("connection closed socket [%d]", sock);
paul718e3742002-12-13 20:15:29 +00001250 zebra_client_close (client);
1251 return -1;
1252 }
1253 length = stream_getw (client->ibuf);
1254 command = stream_getc (client->ibuf);
1255
1256 if (length < 3)
1257 {
1258 if (IS_ZEBRA_DEBUG_EVENT)
ajsb6178002004-12-07 21:12:56 +00001259 zlog_debug ("length %d is less than 3 ", length);
paul718e3742002-12-13 20:15:29 +00001260 zebra_client_close (client);
1261 return -1;
1262 }
1263
1264 length -= 3;
1265
1266 /* Read rest of data. */
1267 if (length)
1268 {
1269 nbyte = stream_read (client->ibuf, sock, length);
1270 if (nbyte <= 0)
1271 {
1272 if (IS_ZEBRA_DEBUG_EVENT)
ajsb6178002004-12-07 21:12:56 +00001273 zlog_debug ("connection closed [%d] when reading zebra data", sock);
paul718e3742002-12-13 20:15:29 +00001274 zebra_client_close (client);
1275 return -1;
1276 }
1277 }
1278
1279 /* Debug packet information. */
1280 if (IS_ZEBRA_DEBUG_EVENT)
ajsb6178002004-12-07 21:12:56 +00001281 zlog_debug ("zebra message comes from socket [%d]", sock);
paul718e3742002-12-13 20:15:29 +00001282
1283 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
ajsb6178002004-12-07 21:12:56 +00001284 zlog_debug ("zebra message received [%s] %d",
paul718e3742002-12-13 20:15:29 +00001285 zebra_command_str[command], length);
1286
1287 switch (command)
1288 {
hasso18a6dce2004-10-03 18:18:34 +00001289 case ZEBRA_ROUTER_ID_ADD:
1290 zread_router_id_add (client, length);
1291 break;
1292 case ZEBRA_ROUTER_ID_DELETE:
1293 zread_router_id_delete (client, length);
1294 break;
paul718e3742002-12-13 20:15:29 +00001295 case ZEBRA_INTERFACE_ADD:
1296 zread_interface_add (client, length);
1297 break;
1298 case ZEBRA_INTERFACE_DELETE:
1299 zread_interface_delete (client, length);
1300 break;
1301 case ZEBRA_IPV4_ROUTE_ADD:
1302 zread_ipv4_add (client, length);
1303 break;
1304 case ZEBRA_IPV4_ROUTE_DELETE:
1305 zread_ipv4_delete (client, length);
1306 break;
1307#ifdef HAVE_IPV6
1308 case ZEBRA_IPV6_ROUTE_ADD:
1309 zread_ipv6_add (client, length);
1310 break;
1311 case ZEBRA_IPV6_ROUTE_DELETE:
1312 zread_ipv6_delete (client, length);
1313 break;
1314#endif /* HAVE_IPV6 */
1315 case ZEBRA_REDISTRIBUTE_ADD:
1316 zebra_redistribute_add (command, client, length);
1317 break;
1318 case ZEBRA_REDISTRIBUTE_DELETE:
1319 zebra_redistribute_delete (command, client, length);
1320 break;
1321 case ZEBRA_REDISTRIBUTE_DEFAULT_ADD:
1322 zebra_redistribute_default_add (command, client, length);
1323 break;
1324 case ZEBRA_REDISTRIBUTE_DEFAULT_DELETE:
1325 zebra_redistribute_default_delete (command, client, length);
1326 break;
1327 case ZEBRA_IPV4_NEXTHOP_LOOKUP:
1328 zread_ipv4_nexthop_lookup (client, length);
1329 break;
1330#ifdef HAVE_IPV6
1331 case ZEBRA_IPV6_NEXTHOP_LOOKUP:
1332 zread_ipv6_nexthop_lookup (client, length);
1333 break;
1334#endif /* HAVE_IPV6 */
1335 case ZEBRA_IPV4_IMPORT_LOOKUP:
1336 zread_ipv4_import_lookup (client, length);
1337 break;
1338 default:
1339 zlog_info ("Zebra received unknown command %d", command);
1340 break;
1341 }
1342
1343 stream_reset (client->ibuf);
1344 zebra_event (ZEBRA_READ, sock, client);
1345
1346 return 0;
1347}
1348
paul718e3742002-12-13 20:15:29 +00001349
1350/* Accept code of zebra server socket. */
paulb9df2d22004-05-09 09:09:59 +00001351static int
paul718e3742002-12-13 20:15:29 +00001352zebra_accept (struct thread *thread)
1353{
paulccf35572003-03-01 11:42:20 +00001354 int val;
paul718e3742002-12-13 20:15:29 +00001355 int accept_sock;
1356 int client_sock;
1357 struct sockaddr_in client;
1358 socklen_t len;
1359
1360 accept_sock = THREAD_FD (thread);
1361
1362 len = sizeof (struct sockaddr_in);
1363 client_sock = accept (accept_sock, (struct sockaddr *) &client, &len);
1364
1365 if (client_sock < 0)
1366 {
ajs6099b3b2004-11-20 02:06:59 +00001367 zlog_warn ("Can't accept zebra socket: %s", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001368 return -1;
1369 }
1370
paulccf35572003-03-01 11:42:20 +00001371 /* Make client socket non-blocking. */
paul865b8522005-01-05 08:30:35 +00001372 /* XXX: We dont requeue failed writes, so this leads to inconsistencies.
1373 * for now socket must remain blocking, regardless of risk of deadlocks.
1374 */
1375 /*
paulccf35572003-03-01 11:42:20 +00001376 val = fcntl (client_sock, F_GETFL, 0);
1377 fcntl (client_sock, F_SETFL, (val | O_NONBLOCK));
paul865b8522005-01-05 08:30:35 +00001378 */
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 {
ajs6099b3b2004-11-20 02:06:59 +00001402 zlog_warn ("Can't bind to socket: %s", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001403 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 {
ajs6099b3b2004-11-20 02:06:59 +00001425 zlog_warn ("Can't bind to socket: %s", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001426 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 {
ajs6099b3b2004-11-20 02:06:59 +00001437 zlog_warn ("Can't listen to socket: %s", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001438 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
hassofce954f2004-10-07 20:29:24 +00001452zebra_serv_un (const char *path)
paul718e3742002-12-13 20:15:29 +00001453{
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 ();
hassob71f00f2004-10-13 12:20:35 +00001554 if (ret == 0)
1555 ret = ipforward_on ();
hasso647e4f12003-05-25 11:43:52 +00001556
hasso647e4f12003-05-25 11:43:52 +00001557 if (ret == 0)
1558 {
1559 vty_out (vty, "Can't turn on IP forwarding%s", VTY_NEWLINE);
1560 return CMD_WARNING;
1561 }
1562
1563 return CMD_SUCCESS;
1564}
1565
paul718e3742002-12-13 20:15:29 +00001566DEFUN (no_ip_forwarding,
1567 no_ip_forwarding_cmd,
1568 "no ip forwarding",
1569 NO_STR
1570 IP_STR
1571 "Turn off IP forwarding")
1572{
1573 int ret;
1574
1575 ret = ipforward ();
hassob71f00f2004-10-13 12:20:35 +00001576 if (ret != 0)
1577 ret = ipforward_off ();
paul718e3742002-12-13 20:15:29 +00001578
paul718e3742002-12-13 20:15:29 +00001579 if (ret != 0)
1580 {
1581 vty_out (vty, "Can't turn off IP forwarding%s", VTY_NEWLINE);
1582 return CMD_WARNING;
1583 }
1584
1585 return CMD_SUCCESS;
1586}
1587
1588/* This command is for debugging purpose. */
1589DEFUN (show_zebra_client,
1590 show_zebra_client_cmd,
1591 "show zebra client",
1592 SHOW_STR
1593 "Zebra information"
1594 "Client information")
1595{
hasso52dc7ee2004-09-23 19:18:23 +00001596 struct listnode *node;
paul718e3742002-12-13 20:15:29 +00001597 struct zserv *client;
1598
paulb21b19c2003-06-15 01:28:29 +00001599 for (node = listhead (zebrad.client_list); node; nextnode (node))
paul718e3742002-12-13 20:15:29 +00001600 {
1601 client = getdata (node);
1602 vty_out (vty, "Client fd %d%s", client->sock, VTY_NEWLINE);
1603 }
1604 return CMD_SUCCESS;
1605}
1606
1607/* Table configuration write function. */
paulb9df2d22004-05-09 09:09:59 +00001608static int
paul718e3742002-12-13 20:15:29 +00001609config_write_table (struct vty *vty)
1610{
paulb21b19c2003-06-15 01:28:29 +00001611 if (zebrad.rtm_table_default)
1612 vty_out (vty, "table %d%s", zebrad.rtm_table_default,
paul718e3742002-12-13 20:15:29 +00001613 VTY_NEWLINE);
1614 return 0;
1615}
1616
1617/* table node for routing tables. */
1618struct cmd_node table_node =
1619{
1620 TABLE_NODE,
1621 "", /* This node has no interface. */
1622 1
1623};
1624
1625/* Only display ip forwarding is enabled or not. */
1626DEFUN (show_ip_forwarding,
1627 show_ip_forwarding_cmd,
1628 "show ip forwarding",
1629 SHOW_STR
1630 IP_STR
1631 "IP forwarding status\n")
1632{
1633 int ret;
1634
1635 ret = ipforward ();
1636
1637 if (ret == 0)
1638 vty_out (vty, "IP forwarding is off%s", VTY_NEWLINE);
1639 else
1640 vty_out (vty, "IP forwarding is on%s", VTY_NEWLINE);
1641 return CMD_SUCCESS;
1642}
1643
1644#ifdef HAVE_IPV6
1645/* Only display ipv6 forwarding is enabled or not. */
1646DEFUN (show_ipv6_forwarding,
1647 show_ipv6_forwarding_cmd,
1648 "show ipv6 forwarding",
1649 SHOW_STR
1650 "IPv6 information\n"
1651 "Forwarding status\n")
1652{
1653 int ret;
1654
1655 ret = ipforward_ipv6 ();
1656
1657 switch (ret)
1658 {
1659 case -1:
1660 vty_out (vty, "ipv6 forwarding is unknown%s", VTY_NEWLINE);
1661 break;
1662 case 0:
1663 vty_out (vty, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE);
1664 break;
1665 case 1:
1666 vty_out (vty, "ipv6 forwarding is %s%s", "on", VTY_NEWLINE);
1667 break;
1668 default:
1669 vty_out (vty, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE);
1670 break;
1671 }
1672 return CMD_SUCCESS;
1673}
1674
hasso55906722004-02-11 22:42:16 +00001675DEFUN (ipv6_forwarding,
1676 ipv6_forwarding_cmd,
1677 "ipv6 forwarding",
1678 IPV6_STR
1679 "Turn on IPv6 forwarding")
1680{
1681 int ret;
1682
hasso41d3fc92004-04-06 11:59:00 +00001683 ret = ipforward_ipv6 ();
hassob71f00f2004-10-13 12:20:35 +00001684 if (ret == 0)
1685 ret = ipforward_ipv6_on ();
hasso41d3fc92004-04-06 11:59:00 +00001686
hasso41d3fc92004-04-06 11:59:00 +00001687 if (ret == 0)
1688 {
hasso55906722004-02-11 22:42:16 +00001689 vty_out (vty, "Can't turn on IPv6 forwarding%s", VTY_NEWLINE);
1690 return CMD_WARNING;
1691 }
1692
1693 return CMD_SUCCESS;
1694}
1695
paul718e3742002-12-13 20:15:29 +00001696DEFUN (no_ipv6_forwarding,
1697 no_ipv6_forwarding_cmd,
1698 "no ipv6 forwarding",
1699 NO_STR
hasso55906722004-02-11 22:42:16 +00001700 IPV6_STR
1701 "Turn off IPv6 forwarding")
paul718e3742002-12-13 20:15:29 +00001702{
1703 int ret;
1704
hasso41d3fc92004-04-06 11:59:00 +00001705 ret = ipforward_ipv6 ();
hassob71f00f2004-10-13 12:20:35 +00001706 if (ret != 0)
1707 ret = ipforward_ipv6_off ();
hasso41d3fc92004-04-06 11:59:00 +00001708
paul718e3742002-12-13 20:15:29 +00001709 if (ret != 0)
1710 {
1711 vty_out (vty, "Can't turn off IPv6 forwarding%s", VTY_NEWLINE);
1712 return CMD_WARNING;
1713 }
1714
1715 return CMD_SUCCESS;
1716}
1717
1718#endif /* HAVE_IPV6 */
1719
1720/* IPForwarding configuration write function. */
1721int
1722config_write_forwarding (struct vty *vty)
1723{
hasso18a6dce2004-10-03 18:18:34 +00001724 /* FIXME: Find better place for that. */
1725 router_id_write (vty);
1726
paul3e0b3a52004-08-23 18:58:32 +00001727 if (ipforward ())
1728 vty_out (vty, "ip forwarding%s", VTY_NEWLINE);
paul718e3742002-12-13 20:15:29 +00001729#ifdef HAVE_IPV6
paul3e0b3a52004-08-23 18:58:32 +00001730 if (ipforward_ipv6 ())
1731 vty_out (vty, "ipv6 forwarding%s", VTY_NEWLINE);
paul718e3742002-12-13 20:15:29 +00001732#endif /* HAVE_IPV6 */
1733 vty_out (vty, "!%s", VTY_NEWLINE);
1734 return 0;
1735}
1736
1737/* table node for routing tables. */
1738struct cmd_node forwarding_node =
1739{
1740 FORWARDING_NODE,
1741 "", /* This node has no interface. */
1742 1
1743};
1744
1745
1746/* Initialisation of zebra and installation of commands. */
1747void
1748zebra_init ()
1749{
1750 /* Client list init. */
paulb21b19c2003-06-15 01:28:29 +00001751 zebrad.client_list = list_new ();
paul718e3742002-12-13 20:15:29 +00001752
paul718e3742002-12-13 20:15:29 +00001753 /* Make zebra server socket. */
1754#ifdef HAVE_TCP_ZEBRA
1755 zebra_serv ();
1756#else
1757 zebra_serv_un (ZEBRA_SERV_PATH);
1758#endif /* HAVE_TCP_ZEBRA */
1759
1760 /* Install configuration write function. */
1761 install_node (&table_node, config_write_table);
1762 install_node (&forwarding_node, config_write_forwarding);
1763
1764 install_element (VIEW_NODE, &show_ip_forwarding_cmd);
1765 install_element (ENABLE_NODE, &show_ip_forwarding_cmd);
hasso647e4f12003-05-25 11:43:52 +00001766 install_element (CONFIG_NODE, &ip_forwarding_cmd);
paul718e3742002-12-13 20:15:29 +00001767 install_element (CONFIG_NODE, &no_ip_forwarding_cmd);
1768 install_element (ENABLE_NODE, &show_zebra_client_cmd);
1769
1770#ifdef HAVE_NETLINK
1771 install_element (VIEW_NODE, &show_table_cmd);
1772 install_element (ENABLE_NODE, &show_table_cmd);
1773 install_element (CONFIG_NODE, &config_table_cmd);
1774#endif /* HAVE_NETLINK */
1775
1776#ifdef HAVE_IPV6
1777 install_element (VIEW_NODE, &show_ipv6_forwarding_cmd);
1778 install_element (ENABLE_NODE, &show_ipv6_forwarding_cmd);
hasso55906722004-02-11 22:42:16 +00001779 install_element (CONFIG_NODE, &ipv6_forwarding_cmd);
paul718e3742002-12-13 20:15:29 +00001780 install_element (CONFIG_NODE, &no_ipv6_forwarding_cmd);
1781#endif /* HAVE_IPV6 */
paulccf35572003-03-01 11:42:20 +00001782
1783 FIFO_INIT(&message_queue);
1784 t_write = NULL;
paul718e3742002-12-13 20:15:29 +00001785}