blob: 4779b64307cce69df8b9dc860799d0c1877a89d8 [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"
39#include "zebra/redistribute.h"
40#include "zebra/debug.h"
41#include "zebra/ipforward.h"
42
43/* Event list of zebra. */
44enum event { ZEBRA_SERV, ZEBRA_READ, ZEBRA_WRITE };
45
paulb21b19c2003-06-15 01:28:29 +000046extern struct zebra_t zebrad;
paul718e3742002-12-13 20:15:29 +000047
paulb9df2d22004-05-09 09:09:59 +000048static void zebra_event (enum event event, int sock, struct zserv *client);
paulccf35572003-03-01 11:42:20 +000049
pauledd7c242003-06-04 13:59:38 +000050extern struct zebra_privs_t zserv_privs;
paul718e3742002-12-13 20:15:29 +000051
52/* For logging of zebra meesages. */
paulb9df2d22004-05-09 09:09:59 +000053static char *zebra_command_str [] =
paul718e3742002-12-13 20:15:29 +000054{
55 "NULL",
56 "ZEBRA_INTERFACE_ADD",
57 "ZEBRA_INTERFACE_DELETE",
58 "ZEBRA_INTERFACE_ADDRESS_ADD",
59 "ZEBRA_INTERFACE_ADDRESS_DELETE",
60 "ZEBRA_INTERFACE_UP",
61 "ZEBRA_INTERFACE_DOWN",
62 "ZEBRA_IPV4_ROUTE_ADD",
63 "ZEBRA_IPV4_ROUTE_DELETE",
64 "ZEBRA_IPV6_ROUTE_ADD",
65 "ZEBRA_IPV6_ROUTE_DELETE",
66 "ZEBRA_REDISTRIBUTE_ADD",
67 "ZEBRA_REDISTRIBUTE_DELETE",
68 "ZEBRA_REDISTRIBUTE_DEFAULT_ADD",
69 "ZEBRA_REDISTRIBUTE_DEFAULT_DELETE",
70 "ZEBRA_IPV4_NEXTHOP_LOOKUP",
71 "ZEBRA_IPV6_NEXTHOP_LOOKUP",
72 "ZEBRA_IPV4_IMPORT_LOOKUP",
73 "ZEBRA_IPV6_IMPORT_LOOKUP"
74};
75
paulccf35572003-03-01 11:42:20 +000076struct zebra_message_queue
77{
78 struct nsm_message_queue *next;
79 struct nsm_message_queue *prev;
80
81 u_char *buf;
82 u_int16_t length;
83 u_int16_t written;
84};
85
86struct thread *t_write;
87struct fifo message_queue;
88
89int
90zebra_server_dequeue (struct thread *t)
91{
92 int sock;
93 int nbytes;
94 struct zebra_message_queue *queue;
95
96 sock = THREAD_FD (t);
97 t_write = NULL;
98
99 queue = (struct zebra_message_queue *) FIFO_HEAD (&message_queue);
100 if (queue)
101 {
102 nbytes = write (sock, queue->buf + queue->written,
103 queue->length - queue->written);
104
105 if (nbytes <= 0)
106 {
107 if (errno != EAGAIN)
108 return -1;
109 }
110 else if (nbytes != (queue->length - queue->written))
111 {
112 queue->written += nbytes;
113 }
114 else
115 {
116 FIFO_DEL (queue);
117 XFREE (MTYPE_TMP, queue->buf);
118 XFREE (MTYPE_TMP, queue);
119 }
120 }
121
122 if (FIFO_TOP (&message_queue))
paulb21b19c2003-06-15 01:28:29 +0000123 THREAD_WRITE_ON (zebrad.master, t_write, zebra_server_dequeue,
124 NULL, sock);
paulccf35572003-03-01 11:42:20 +0000125
126 return 0;
127}
128
129/* Enqueu message. */
130void
131zebra_server_enqueue (int sock, u_char *buf, unsigned long length,
132 unsigned long written)
133{
134 struct zebra_message_queue *queue;
135
136 queue = XCALLOC (MTYPE_TMP, sizeof (struct zebra_message_queue));
137 queue->buf = XMALLOC (MTYPE_TMP, length);
138 memcpy (queue->buf, buf, length);
139 queue->length = length;
140 queue->written = written;
141
142 FIFO_ADD (&message_queue, queue);
143
paulb21b19c2003-06-15 01:28:29 +0000144 THREAD_WRITE_ON (zebrad.master, t_write, zebra_server_dequeue, NULL, sock);
paulccf35572003-03-01 11:42:20 +0000145}
146
147int
148zebra_server_send_message (int sock, u_char *buf, unsigned long length)
149{
150 int nbytes;
151
152 if (FIFO_TOP (&message_queue))
153 {
154 zebra_server_enqueue (sock, buf, length, 0);
155 return 0;
156 }
157
158 /* Send message. */
159 nbytes = write (sock, buf, length);
160
161 if (nbytes <= 0)
162 {
163 if (errno == EAGAIN)
164 zebra_server_enqueue (sock, buf, length, 0);
165 else
166 return -1;
167 }
168 else if (nbytes != length)
169 zebra_server_enqueue (sock, buf, length, nbytes);
170
171 return 0;
172}
173
paul718e3742002-12-13 20:15:29 +0000174/* Interface is added. Send ZEBRA_INTERFACE_ADD to client. */
paulb9df2d22004-05-09 09:09:59 +0000175/*
176 * This function is called in the following situations:
177 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
178 * from the client.
179 * - at startup, when zebra figures out the available interfaces
180 * - when an interface is added (where support for
181 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
182 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
183 * received)
184 */
paul718e3742002-12-13 20:15:29 +0000185int
186zsend_interface_add (struct zserv *client, struct interface *ifp)
187{
188 struct stream *s;
189
190 /* Check this client need interface information. */
191 if (! client->ifinfo)
192 return -1;
193
194 s = client->obuf;
195 stream_reset (s);
196
197 /* Place holder for size. */
198 stream_putw (s, 0);
199
200 /* Message type. */
201 stream_putc (s, ZEBRA_INTERFACE_ADD);
202
203 /* Interface information. */
204 stream_put (s, ifp->name, INTERFACE_NAMSIZ);
205 stream_putl (s, ifp->ifindex);
paul2e3b2e42002-12-13 21:03:13 +0000206 stream_putc (s, ifp->status);
paul718e3742002-12-13 20:15:29 +0000207 stream_putl (s, ifp->flags);
208 stream_putl (s, ifp->metric);
209 stream_putl (s, ifp->mtu);
paulb9df2d22004-05-09 09:09:59 +0000210 stream_putl (s, ifp->mtu6);
paul718e3742002-12-13 20:15:29 +0000211 stream_putl (s, ifp->bandwidth);
212#ifdef HAVE_SOCKADDR_DL
213 stream_put (s, &ifp->sdl, sizeof (ifp->sdl));
214#else
215 stream_putl (s, ifp->hw_addr_len);
216 if (ifp->hw_addr_len)
217 stream_put (s, ifp->hw_addr, ifp->hw_addr_len);
218#endif /* HAVE_SOCKADDR_DL */
219
220 /* Write packet size. */
221 stream_putw_at (s, 0, stream_get_endp (s));
222
paulccf35572003-03-01 11:42:20 +0000223 zebra_server_send_message (client->sock, s->data, stream_get_endp (s));
224
225 return 0;
paul718e3742002-12-13 20:15:29 +0000226}
227
228/* Interface deletion from zebra daemon. */
paulb9df2d22004-05-09 09:09:59 +0000229/*
230 * This function is only called when support for
231 * RTM_IFANNOUNCE or AF_NETLINK sockets (RTM_DELLINK message)
232 * is available. It is not called on Solaris.
233 */
234#if (defined(RTM_IFANNOUNCE) || defined(HAVE_NETLINK))
paul718e3742002-12-13 20:15:29 +0000235int
236zsend_interface_delete (struct zserv *client, struct interface *ifp)
237{
238 struct stream *s;
239
240 /* Check this client need interface information. */
241 if (! client->ifinfo)
242 return -1;
243
244 s = client->obuf;
245 stream_reset (s);
246
247 /* Packet length placeholder. */
248 stream_putw (s, 0);
249
250 /* Interface information. */
251 stream_putc (s, ZEBRA_INTERFACE_DELETE);
252 stream_put (s, ifp->name, INTERFACE_NAMSIZ);
253 stream_putl (s, ifp->ifindex);
paul2e3b2e42002-12-13 21:03:13 +0000254 stream_putc (s, ifp->status);
paul718e3742002-12-13 20:15:29 +0000255 stream_putl (s, ifp->flags);
256 stream_putl (s, ifp->metric);
257 stream_putl (s, ifp->mtu);
paulb9df2d22004-05-09 09:09:59 +0000258 stream_putl (s, ifp->mtu6);
paul718e3742002-12-13 20:15:29 +0000259 stream_putl (s, ifp->bandwidth);
260
261 /* Write packet length. */
262 stream_putw_at (s, 0, stream_get_endp (s));
263
paulccf35572003-03-01 11:42:20 +0000264 zebra_server_send_message (client->sock, s->data, stream_get_endp (s));
265
266 return 0;
paul718e3742002-12-13 20:15:29 +0000267}
paulb9df2d22004-05-09 09:09:59 +0000268#endif /* (defined(RTM_IFANNOUNCE) || defined(HAVE_LINUX_RTNETLINK_H)) */
paul718e3742002-12-13 20:15:29 +0000269
paulb9df2d22004-05-09 09:09:59 +0000270/* Interface address is added/deleted. Send ZEBRA_INTERFACE_ADDRESS_ADD or
271 * ZEBRA_INTERFACE_ADDRESS_DELETE to the client.
272 *
273 * A ZEBRA_INTERFACE_ADDRESS_ADD is sent in the following situations:
274 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
275 * from the client, after the ZEBRA_INTERFACE_ADD has been
276 * sent from zebra to the client
277 * - redistribute new address info to all clients in the following situations
278 * - at startup, when zebra figures out the available interfaces
279 * - when an interface is added (where support for
280 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
281 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
282 * received)
283 * - for the vty commands "ip address A.B.C.D/M [<secondary>|<label LINE>]"
284 * and "no bandwidth <1-10000000>", "ipv6 address X:X::X:X/M"
285 * - when an RTM_NEWADDR message is received from the kernel,
286 *
287 * The call tree that triggers ZEBRA_INTERFACE_ADDRESS_DELETE:
288 *
289 * zsend_interface_address(DELETE)
290 * ^
291 * |
292 * zebra_interface_address_delete_update
293 * ^ ^ ^
294 * | | if_delete_update (not called on
295 * | | Solaris)
296 * ip_address_uninstall connected_delete_ipv4
297 * [ipv6_addresss_uninstall] [connected_delete_ipv6]
298 * ^ ^
299 * | |
300 * | RTM_NEWADDR on routing/netlink socket
301 * |
302 * vty commands:
303 * "no ip address A.B.C.D/M [label LINE]"
304 * "no ip address A.B.C.D/M secondary"
305 * ["no ipv6 address X:X::X:X/M"]
306 *
307 */
paul718e3742002-12-13 20:15:29 +0000308int
paulb9df2d22004-05-09 09:09:59 +0000309zsend_interface_address (int cmd, struct zserv *client,
310 struct interface *ifp, struct connected *ifc)
paul718e3742002-12-13 20:15:29 +0000311{
312 int blen;
313 struct stream *s;
314 struct prefix *p;
315
316 /* Check this client need interface information. */
317 if (! client->ifinfo)
318 return -1;
319
320 s = client->obuf;
321 stream_reset (s);
322
323 /* Place holder for size. */
324 stream_putw (s, 0);
325
paulb9df2d22004-05-09 09:09:59 +0000326 stream_putc (s, cmd);
paul718e3742002-12-13 20:15:29 +0000327 stream_putl (s, ifp->ifindex);
328
329 /* Interface address flag. */
330 stream_putc (s, ifc->flags);
331
332 /* Prefix information. */
333 p = ifc->address;
334 stream_putc (s, p->family);
335 blen = prefix_blen (p);
336 stream_put (s, &p->u.prefix, blen);
paulb9df2d22004-05-09 09:09:59 +0000337
338 /*
339 * XXX gnu version does not send prefixlen for ZEBRA_INTERFACE_ADDRESS_DELETE
340 * but zebra_interface_address_delete_read() in the gnu version
341 * expects to find it
342 */
paul718e3742002-12-13 20:15:29 +0000343 stream_putc (s, p->prefixlen);
344
345 /* Destination. */
346 p = ifc->destination;
347 if (p)
348 stream_put (s, &p->u.prefix, blen);
349 else
350 stream_put (s, NULL, blen);
351
352 /* Write packet size. */
353 stream_putw_at (s, 0, stream_get_endp (s));
354
paulccf35572003-03-01 11:42:20 +0000355 zebra_server_send_message (client->sock, s->data, stream_get_endp (s));
356
357 return 0;
paul718e3742002-12-13 20:15:29 +0000358}
359
paulb9df2d22004-05-09 09:09:59 +0000360/*
361 * The cmd passed to zsend_interface_update may be ZEBRA_INTERFACE_UP or
362 * ZEBRA_INTERFACE_DOWN.
363 *
364 * The ZEBRA_INTERFACE_UP message is sent from the zebra server to
365 * the clients in one of 2 situations:
366 * - an if_up is detected e.g., as a result of an RTM_IFINFO message
367 * - a vty command modifying the bandwidth of an interface is received.
368 * The ZEBRA_INTERFACE_DOWN message is sent when an if_down is detected.
369 */
paul718e3742002-12-13 20:15:29 +0000370int
paulb9df2d22004-05-09 09:09:59 +0000371zsend_interface_update (int cmd, struct zserv *client, struct interface *ifp)
paul718e3742002-12-13 20:15:29 +0000372{
373 struct stream *s;
374
375 /* Check this client need interface information. */
376 if (! client->ifinfo)
377 return -1;
378
379 s = client->obuf;
380 stream_reset (s);
381
382 /* Place holder for size. */
383 stream_putw (s, 0);
384
385 /* Zebra command. */
paulb9df2d22004-05-09 09:09:59 +0000386 stream_putc (s, cmd);
paul718e3742002-12-13 20:15:29 +0000387
388 /* Interface information. */
389 stream_put (s, ifp->name, INTERFACE_NAMSIZ);
390 stream_putl (s, ifp->ifindex);
paul2e3b2e42002-12-13 21:03:13 +0000391 stream_putc (s, ifp->status);
paul718e3742002-12-13 20:15:29 +0000392 stream_putl (s, ifp->flags);
393 stream_putl (s, ifp->metric);
394 stream_putl (s, ifp->mtu);
paulb9df2d22004-05-09 09:09:59 +0000395 stream_putl (s, ifp->mtu6);
paul718e3742002-12-13 20:15:29 +0000396 stream_putl (s, ifp->bandwidth);
397
398 /* Write packet size. */
399 stream_putw_at (s, 0, stream_get_endp (s));
400
paulccf35572003-03-01 11:42:20 +0000401 zebra_server_send_message (client->sock, s->data, stream_get_endp (s));
402
403 return 0;
paul718e3742002-12-13 20:15:29 +0000404}
405
paulb9df2d22004-05-09 09:09:59 +0000406/*
407 * The zebra server sends the clients a ZEBRA_IPV4_ROUTE_ADD or a
408 * ZEBRA_IPV6_ROUTE_ADD via zsend_route_multipath in the following
409 * situations:
410 * - when the client starts up, and requests default information
411 * by sending a ZEBRA_REDISTRIBUTE_DEFAULT_ADD to the zebra server, in the
412 * - case of rip, ripngd, ospfd and ospf6d, when the client sends a
413 * ZEBRA_REDISTRIBUTE_ADD as a result of the "redistribute" vty cmd,
414 * - when the zebra server redistributes routes after it updates its rib
415 *
416 * The zebra server sends clients a ZEBRA_IPV4_ROUTE_DELETE or a
417 * ZEBRA_IPV6_ROUTE_DELETE via zsend_route_multipath when:
418 * - a "ip route" or "ipv6 route" vty command is issued, a prefix is
419 * - deleted from zebra's rib, and this info
420 * has to be redistributed to the clients
421 *
422 * XXX The ZEBRA_IPV*_ROUTE_ADD message is also sent by the client to the
423 * zebra server when the client wants to tell the zebra server to add a
424 * route to the kernel (zapi_ipv4_add etc. ). Since it's essentially the
425 * same message being sent back and forth, this function and
426 * zapi_ipv{4,6}_{add, delete} should be re-written to avoid code
427 * duplication.
428 */
paul718e3742002-12-13 20:15:29 +0000429int
paulb9df2d22004-05-09 09:09:59 +0000430zsend_route_multipath (int cmd, struct zserv *client, struct prefix *p,
431 struct rib *rib)
paul718e3742002-12-13 20:15:29 +0000432{
433 int psize;
434 struct stream *s;
435 struct nexthop *nexthop;
paulb9df2d22004-05-09 09:09:59 +0000436 unsigned long nhnummark = 0;
437 int nhnum = 0;
438 u_char zapi_flags = ZAPI_MESSAGE_NEXTHOP | ZAPI_MESSAGE_IFINDEX;
439
paul718e3742002-12-13 20:15:29 +0000440 s = client->obuf;
441 stream_reset (s);
442
443 /* Place holder for size. */
444 stream_putw (s, 0);
445
446 /* Put command, type and nexthop. */
paulb9df2d22004-05-09 09:09:59 +0000447 stream_putc (s, cmd);
paul718e3742002-12-13 20:15:29 +0000448 stream_putc (s, rib->type);
449 stream_putc (s, rib->flags);
paulb9df2d22004-05-09 09:09:59 +0000450
451 /*
452 * XXX no need to set ZAPI_MESSAGE_NEXTHOP if we are going to
453 * send empty nexthop?
454 */
455 if (cmd == ZEBRA_IPV4_ROUTE_ADD || ZEBRA_IPV6_ROUTE_ADD)
456 zapi_flags |= ZAPI_MESSAGE_METRIC;
457
458 stream_putc (s, zapi_flags);
paul718e3742002-12-13 20:15:29 +0000459
460 /* Prefix. */
461 psize = PSIZE (p->prefixlen);
462 stream_putc (s, p->prefixlen);
paulb9df2d22004-05-09 09:09:59 +0000463 stream_write (s, (u_char *) & p->u.prefix, psize);
paul718e3742002-12-13 20:15:29 +0000464
paulb9df2d22004-05-09 09:09:59 +0000465 /*
466 * XXX The message format sent by zebra below does not match the format
467 * of the corresponding message expected by the zebra server
468 * itself (e.g., see zread_ipv4_add). The nexthop_num is not set correctly,
469 * (is there a bug on the client side if more than one segment is sent?)
470 * nexthop ZEBRA_NEXTHOP_IPV4 is never set, ZEBRA_NEXTHOP_IFINDEX
471 * is hard-coded.
472 */
paul718e3742002-12-13 20:15:29 +0000473 /* Nexthop */
474 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
475 {
476 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB))
paulb9df2d22004-05-09 09:09:59 +0000477 {
478 nhnummark = stream_get_putp (s);
479 stream_putc (s, 1); /* placeholder */
480 nhnum++;
paul718e3742002-12-13 20:15:29 +0000481
paulb9df2d22004-05-09 09:09:59 +0000482 switch(nexthop->type)
483 {
484 case NEXTHOP_TYPE_IPV4:
485 case NEXTHOP_TYPE_IPV4_IFINDEX:
486 stream_put_in_addr (s, &nexthop->gate.ipv4);
487 break;
488#ifdef HAVE_IPV6
489 case NEXTHOP_TYPE_IPV6:
490 case NEXTHOP_TYPE_IPV6_IFINDEX:
491 case NEXTHOP_TYPE_IPV6_IFNAME:
492 stream_write (s, (u_char *) &nexthop->gate.ipv6, 16);
493 break;
494#endif
495 default:
496 if (cmd == ZEBRA_IPV4_ROUTE_ADD
497 || cmd == ZEBRA_IPV4_ROUTE_DELETE)
498 {
499 struct in_addr empty;
500 memset (&empty, 0, sizeof (struct in6_addr));
501 stream_write (s, (u_char *) &empty, IPV4_MAX_BYTELEN);
502 }
503 else
504 {
505 struct in6_addr empty;
506 memset (&empty, 0, sizeof (struct in6_addr));
507 stream_write (s, (u_char *) &empty, IPV6_MAX_BYTELEN);
508 }
509 }
paul718e3742002-12-13 20:15:29 +0000510
paulb9df2d22004-05-09 09:09:59 +0000511 /* Interface index. */
512 stream_putc (s, 1);
513 stream_putl (s, nexthop->ifindex);
paul718e3742002-12-13 20:15:29 +0000514
paulb9df2d22004-05-09 09:09:59 +0000515 break;
516 }
paul718e3742002-12-13 20:15:29 +0000517 }
518
519 /* Metric */
520 stream_putl (s, rib->metric);
521
paulb9df2d22004-05-09 09:09:59 +0000522 /* Write next-hop number */
523 if (nhnummark)
524 stream_putw_at (s, nhnummark, nhnum);
525
paul718e3742002-12-13 20:15:29 +0000526 /* Write packet size. */
527 stream_putw_at (s, 0, stream_get_endp (s));
528
paulccf35572003-03-01 11:42:20 +0000529 zebra_server_send_message (client->sock, s->data, stream_get_endp (s));
530
531 return 0;
paul718e3742002-12-13 20:15:29 +0000532}
533
paul718e3742002-12-13 20:15:29 +0000534#ifdef HAVE_IPV6
paul718e3742002-12-13 20:15:29 +0000535int
536zsend_ipv6_nexthop_lookup (struct zserv *client, struct in6_addr *addr)
537{
538 struct stream *s;
539 struct rib *rib;
540 unsigned long nump;
541 u_char num;
542 struct nexthop *nexthop;
543
544 /* Lookup nexthop. */
545 rib = rib_match_ipv6 (addr);
546
547 /* Get output stream. */
548 s = client->obuf;
549 stream_reset (s);
550
551 /* Fill in result. */
552 stream_putw (s, 0);
553 stream_putc (s, ZEBRA_IPV6_NEXTHOP_LOOKUP);
554 stream_put (s, &addr, 16);
555
556 if (rib)
557 {
558 stream_putl (s, rib->metric);
559 num = 0;
560 nump = s->putp;
561 stream_putc (s, 0);
562 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
563 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB))
564 {
565 stream_putc (s, nexthop->type);
566 switch (nexthop->type)
567 {
568 case ZEBRA_NEXTHOP_IPV6:
569 stream_put (s, &nexthop->gate.ipv6, 16);
570 break;
571 case ZEBRA_NEXTHOP_IPV6_IFINDEX:
572 case ZEBRA_NEXTHOP_IPV6_IFNAME:
573 stream_put (s, &nexthop->gate.ipv6, 16);
574 stream_putl (s, nexthop->ifindex);
575 break;
576 case ZEBRA_NEXTHOP_IFINDEX:
577 case ZEBRA_NEXTHOP_IFNAME:
578 stream_putl (s, nexthop->ifindex);
579 break;
hassofa2b17e2004-03-04 17:45:00 +0000580 default:
581 /* do nothing */
582 break;
paul718e3742002-12-13 20:15:29 +0000583 }
584 num++;
585 }
586 stream_putc_at (s, nump, num);
587 }
588 else
589 {
590 stream_putl (s, 0);
591 stream_putc (s, 0);
592 }
593
594 stream_putw_at (s, 0, stream_get_endp (s));
595
paulccf35572003-03-01 11:42:20 +0000596 zebra_server_send_message (client->sock, s->data, stream_get_endp (s));
597
598 return 0;
paul718e3742002-12-13 20:15:29 +0000599}
600#endif /* HAVE_IPV6 */
601
paulb9df2d22004-05-09 09:09:59 +0000602static int
paul718e3742002-12-13 20:15:29 +0000603zsend_ipv4_nexthop_lookup (struct zserv *client, struct in_addr addr)
604{
605 struct stream *s;
606 struct rib *rib;
607 unsigned long nump;
608 u_char num;
609 struct nexthop *nexthop;
610
611 /* Lookup nexthop. */
612 rib = rib_match_ipv4 (addr);
613
614 /* Get output stream. */
615 s = client->obuf;
616 stream_reset (s);
617
618 /* Fill in result. */
619 stream_putw (s, 0);
620 stream_putc (s, ZEBRA_IPV4_NEXTHOP_LOOKUP);
621 stream_put_in_addr (s, &addr);
622
623 if (rib)
624 {
625 stream_putl (s, rib->metric);
626 num = 0;
627 nump = s->putp;
628 stream_putc (s, 0);
629 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
630 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB))
631 {
632 stream_putc (s, nexthop->type);
633 switch (nexthop->type)
634 {
635 case ZEBRA_NEXTHOP_IPV4:
636 stream_put_in_addr (s, &nexthop->gate.ipv4);
637 break;
638 case ZEBRA_NEXTHOP_IFINDEX:
639 case ZEBRA_NEXTHOP_IFNAME:
640 stream_putl (s, nexthop->ifindex);
641 break;
hassofa2b17e2004-03-04 17:45:00 +0000642 default:
643 /* do nothing */
644 break;
paul718e3742002-12-13 20:15:29 +0000645 }
646 num++;
647 }
648 stream_putc_at (s, nump, num);
649 }
650 else
651 {
652 stream_putl (s, 0);
653 stream_putc (s, 0);
654 }
655
656 stream_putw_at (s, 0, stream_get_endp (s));
657
paulccf35572003-03-01 11:42:20 +0000658 zebra_server_send_message (client->sock, s->data, stream_get_endp (s));
659
660 return 0;
paul718e3742002-12-13 20:15:29 +0000661}
662
paulb9df2d22004-05-09 09:09:59 +0000663static int
paul718e3742002-12-13 20:15:29 +0000664zsend_ipv4_import_lookup (struct zserv *client, struct prefix_ipv4 *p)
665{
666 struct stream *s;
667 struct rib *rib;
668 unsigned long nump;
669 u_char num;
670 struct nexthop *nexthop;
671
672 /* Lookup nexthop. */
673 rib = rib_lookup_ipv4 (p);
674
675 /* Get output stream. */
676 s = client->obuf;
677 stream_reset (s);
678
679 /* Fill in result. */
680 stream_putw (s, 0);
681 stream_putc (s, ZEBRA_IPV4_IMPORT_LOOKUP);
682 stream_put_in_addr (s, &p->prefix);
683
684 if (rib)
685 {
686 stream_putl (s, rib->metric);
687 num = 0;
688 nump = s->putp;
689 stream_putc (s, 0);
690 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
691 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB))
692 {
693 stream_putc (s, nexthop->type);
694 switch (nexthop->type)
695 {
696 case ZEBRA_NEXTHOP_IPV4:
697 stream_put_in_addr (s, &nexthop->gate.ipv4);
698 break;
699 case ZEBRA_NEXTHOP_IFINDEX:
700 case ZEBRA_NEXTHOP_IFNAME:
701 stream_putl (s, nexthop->ifindex);
702 break;
hassofa2b17e2004-03-04 17:45:00 +0000703 default:
704 /* do nothing */
705 break;
paul718e3742002-12-13 20:15:29 +0000706 }
707 num++;
708 }
709 stream_putc_at (s, nump, num);
710 }
711 else
712 {
713 stream_putl (s, 0);
714 stream_putc (s, 0);
715 }
716
717 stream_putw_at (s, 0, stream_get_endp (s));
718
paulccf35572003-03-01 11:42:20 +0000719 zebra_server_send_message (client->sock, s->data, stream_get_endp (s));
720
721 return 0;
paul718e3742002-12-13 20:15:29 +0000722}
723
724/* Register zebra server interface information. Send current all
725 interface and address information. */
paulb9df2d22004-05-09 09:09:59 +0000726static void
paul718e3742002-12-13 20:15:29 +0000727zread_interface_add (struct zserv *client, u_short length)
728{
729 listnode ifnode;
730 listnode cnode;
731 struct interface *ifp;
732 struct connected *c;
733
734 /* Interface information is needed. */
735 client->ifinfo = 1;
736
737 for (ifnode = listhead (iflist); ifnode; ifnode = nextnode (ifnode))
738 {
739 ifp = getdata (ifnode);
740
741 /* Skip pseudo interface. */
742 if (! CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE))
743 continue;
744
745 zsend_interface_add (client, ifp);
746
747 for (cnode = listhead (ifp->connected); cnode; nextnode (cnode))
748 {
749 c = getdata (cnode);
750 if (CHECK_FLAG (c->conf, ZEBRA_IFC_REAL))
paulb9df2d22004-05-09 09:09:59 +0000751 zsend_interface_address (ZEBRA_INTERFACE_ADDRESS_ADD, client,
752 ifp, c);
paul718e3742002-12-13 20:15:29 +0000753 }
754 }
755}
756
757/* Unregister zebra server interface information. */
paulb9df2d22004-05-09 09:09:59 +0000758static void
paul718e3742002-12-13 20:15:29 +0000759zread_interface_delete (struct zserv *client, u_short length)
760{
761 client->ifinfo = 0;
762}
763
764/* This function support multiple nexthop. */
paulb9df2d22004-05-09 09:09:59 +0000765/*
766 * Parse the ZEBRA_IPV4_ROUTE_ADD sent from client. Update rib and
767 * add kernel route.
768 */
769static void
paul718e3742002-12-13 20:15:29 +0000770zread_ipv4_add (struct zserv *client, u_short length)
771{
772 int i;
773 struct rib *rib;
774 struct prefix_ipv4 p;
775 u_char message;
776 struct in_addr nexthop;
777 u_char nexthop_num;
778 u_char nexthop_type;
779 struct stream *s;
780 unsigned int ifindex;
781 u_char ifname_len;
782
783 /* Get input stream. */
784 s = client->ibuf;
785
786 /* Allocate new rib. */
787 rib = XMALLOC (MTYPE_RIB, sizeof (struct rib));
788 memset (rib, 0, sizeof (struct rib));
789
790 /* Type, flags, message. */
791 rib->type = stream_getc (s);
792 rib->flags = stream_getc (s);
paulb9df2d22004-05-09 09:09:59 +0000793 message = stream_getc (s);
paul718e3742002-12-13 20:15:29 +0000794 rib->uptime = time (NULL);
795
796 /* IPv4 prefix. */
797 memset (&p, 0, sizeof (struct prefix_ipv4));
798 p.family = AF_INET;
799 p.prefixlen = stream_getc (s);
800 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
801
802 /* Nexthop parse. */
803 if (CHECK_FLAG (message, ZAPI_MESSAGE_NEXTHOP))
804 {
805 nexthop_num = stream_getc (s);
806
807 for (i = 0; i < nexthop_num; i++)
808 {
809 nexthop_type = stream_getc (s);
810
811 switch (nexthop_type)
812 {
813 case ZEBRA_NEXTHOP_IFINDEX:
814 ifindex = stream_getl (s);
815 nexthop_ifindex_add (rib, ifindex);
816 break;
817 case ZEBRA_NEXTHOP_IFNAME:
818 ifname_len = stream_getc (s);
819 stream_forward (s, ifname_len);
820 break;
821 case ZEBRA_NEXTHOP_IPV4:
822 nexthop.s_addr = stream_get_ipv4 (s);
823 nexthop_ipv4_add (rib, &nexthop);
824 break;
825 case ZEBRA_NEXTHOP_IPV6:
826 stream_forward (s, IPV6_MAX_BYTELEN);
827 break;
paul595db7f2003-05-25 21:35:06 +0000828 case ZEBRA_NEXTHOP_BLACKHOLE:
829 nexthop_blackhole_add (rib);
830 break;
paul718e3742002-12-13 20:15:29 +0000831 }
832 }
833 }
834
835 /* Distance. */
836 if (CHECK_FLAG (message, ZAPI_MESSAGE_DISTANCE))
837 rib->distance = stream_getc (s);
838
839 /* Metric. */
840 if (CHECK_FLAG (message, ZAPI_MESSAGE_METRIC))
841 rib->metric = stream_getl (s);
842
843 rib_add_ipv4_multipath (&p, rib);
844}
845
846/* Zebra server IPv4 prefix delete function. */
paulb9df2d22004-05-09 09:09:59 +0000847static void
paul718e3742002-12-13 20:15:29 +0000848zread_ipv4_delete (struct zserv *client, u_short length)
849{
850 int i;
851 struct stream *s;
852 struct zapi_ipv4 api;
853 struct in_addr nexthop;
854 unsigned long ifindex;
855 struct prefix_ipv4 p;
856 u_char nexthop_num;
857 u_char nexthop_type;
858 u_char ifname_len;
859
860 s = client->ibuf;
861 ifindex = 0;
862 nexthop.s_addr = 0;
863
864 /* Type, flags, message. */
865 api.type = stream_getc (s);
866 api.flags = stream_getc (s);
867 api.message = stream_getc (s);
868
869 /* IPv4 prefix. */
870 memset (&p, 0, sizeof (struct prefix_ipv4));
871 p.family = AF_INET;
872 p.prefixlen = stream_getc (s);
873 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
874
875 /* Nexthop, ifindex, distance, metric. */
876 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
877 {
878 nexthop_num = stream_getc (s);
879
880 for (i = 0; i < nexthop_num; i++)
881 {
882 nexthop_type = stream_getc (s);
883
884 switch (nexthop_type)
885 {
886 case ZEBRA_NEXTHOP_IFINDEX:
887 ifindex = stream_getl (s);
888 break;
889 case ZEBRA_NEXTHOP_IFNAME:
890 ifname_len = stream_getc (s);
891 stream_forward (s, ifname_len);
892 break;
893 case ZEBRA_NEXTHOP_IPV4:
894 nexthop.s_addr = stream_get_ipv4 (s);
895 break;
896 case ZEBRA_NEXTHOP_IPV6:
897 stream_forward (s, IPV6_MAX_BYTELEN);
898 break;
899 }
900 }
901 }
902
903 /* Distance. */
904 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
905 api.distance = stream_getc (s);
906 else
907 api.distance = 0;
908
909 /* Metric. */
910 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
911 api.metric = stream_getl (s);
912 else
913 api.metric = 0;
914
915 rib_delete_ipv4 (api.type, api.flags, &p, &nexthop, ifindex,
916 client->rtm_table);
917}
918
919/* Nexthop lookup for IPv4. */
paulb9df2d22004-05-09 09:09:59 +0000920static void
paul718e3742002-12-13 20:15:29 +0000921zread_ipv4_nexthop_lookup (struct zserv *client, u_short length)
922{
923 struct in_addr addr;
924
925 addr.s_addr = stream_get_ipv4 (client->ibuf);
926 zsend_ipv4_nexthop_lookup (client, addr);
927}
928
929/* Nexthop lookup for IPv4. */
paulb9df2d22004-05-09 09:09:59 +0000930static void
paul718e3742002-12-13 20:15:29 +0000931zread_ipv4_import_lookup (struct zserv *client, u_short length)
932{
933 struct prefix_ipv4 p;
934
935 p.family = AF_INET;
936 p.prefixlen = stream_getc (client->ibuf);
937 p.prefix.s_addr = stream_get_ipv4 (client->ibuf);
938
939 zsend_ipv4_import_lookup (client, &p);
940}
941
942#ifdef HAVE_IPV6
943/* Zebra server IPv6 prefix add function. */
paulb9df2d22004-05-09 09:09:59 +0000944static void
paul718e3742002-12-13 20:15:29 +0000945zread_ipv6_add (struct zserv *client, u_short length)
946{
947 int i;
948 struct stream *s;
949 struct zapi_ipv6 api;
950 struct in6_addr nexthop;
951 unsigned long ifindex;
952 struct prefix_ipv6 p;
953
954 s = client->ibuf;
955 ifindex = 0;
956 memset (&nexthop, 0, sizeof (struct in6_addr));
957
958 /* Type, flags, message. */
959 api.type = stream_getc (s);
960 api.flags = stream_getc (s);
961 api.message = stream_getc (s);
962
963 /* IPv4 prefix. */
964 memset (&p, 0, sizeof (struct prefix_ipv6));
965 p.family = AF_INET6;
966 p.prefixlen = stream_getc (s);
967 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
968
969 /* Nexthop, ifindex, distance, metric. */
970 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
971 {
972 u_char nexthop_type;
973
974 api.nexthop_num = stream_getc (s);
975 for (i = 0; i < api.nexthop_num; i++)
976 {
977 nexthop_type = stream_getc (s);
978
979 switch (nexthop_type)
980 {
981 case ZEBRA_NEXTHOP_IPV6:
982 stream_get (&nexthop, s, 16);
983 break;
984 case ZEBRA_NEXTHOP_IFINDEX:
985 ifindex = stream_getl (s);
986 break;
987 }
988 }
989 }
990
991 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
992 api.distance = stream_getc (s);
993 else
994 api.distance = 0;
995
996 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
997 api.metric = stream_getl (s);
998 else
999 api.metric = 0;
1000
1001 if (IN6_IS_ADDR_UNSPECIFIED (&nexthop))
1002 rib_add_ipv6 (api.type, api.flags, &p, NULL, ifindex, 0);
1003 else
1004 rib_add_ipv6 (api.type, api.flags, &p, &nexthop, ifindex, 0);
1005}
1006
1007/* Zebra server IPv6 prefix delete function. */
paulb9df2d22004-05-09 09:09:59 +00001008static void
paul718e3742002-12-13 20:15:29 +00001009zread_ipv6_delete (struct zserv *client, u_short length)
1010{
1011 int i;
1012 struct stream *s;
1013 struct zapi_ipv6 api;
1014 struct in6_addr nexthop;
1015 unsigned long ifindex;
1016 struct prefix_ipv6 p;
1017
1018 s = client->ibuf;
1019 ifindex = 0;
1020 memset (&nexthop, 0, sizeof (struct in6_addr));
1021
1022 /* Type, flags, message. */
1023 api.type = stream_getc (s);
1024 api.flags = stream_getc (s);
1025 api.message = stream_getc (s);
1026
1027 /* IPv4 prefix. */
1028 memset (&p, 0, sizeof (struct prefix_ipv6));
1029 p.family = AF_INET6;
1030 p.prefixlen = stream_getc (s);
1031 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
1032
1033 /* Nexthop, ifindex, distance, metric. */
1034 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
1035 {
1036 u_char nexthop_type;
1037
1038 api.nexthop_num = stream_getc (s);
1039 for (i = 0; i < api.nexthop_num; i++)
1040 {
1041 nexthop_type = stream_getc (s);
1042
1043 switch (nexthop_type)
1044 {
1045 case ZEBRA_NEXTHOP_IPV6:
1046 stream_get (&nexthop, s, 16);
1047 break;
1048 case ZEBRA_NEXTHOP_IFINDEX:
1049 ifindex = stream_getl (s);
1050 break;
1051 }
1052 }
1053 }
1054
1055 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
1056 api.distance = stream_getc (s);
1057 else
1058 api.distance = 0;
1059 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
1060 api.metric = stream_getl (s);
1061 else
1062 api.metric = 0;
1063
1064 if (IN6_IS_ADDR_UNSPECIFIED (&nexthop))
1065 rib_delete_ipv6 (api.type, api.flags, &p, NULL, ifindex, 0);
1066 else
1067 rib_delete_ipv6 (api.type, api.flags, &p, &nexthop, ifindex, 0);
1068}
1069
1070void
1071zebra_read_ipv6 (int command, struct zserv *client, u_short length)
1072{
1073 u_char type;
1074 u_char flags;
1075 struct in6_addr nexthop, *gate;
1076 u_char *lim;
1077 u_char *pnt;
1078 unsigned int ifindex;
1079
1080 pnt = stream_pnt (client->ibuf);
1081 lim = pnt + length;
1082
1083 type = stream_getc (client->ibuf);
1084 flags = stream_getc (client->ibuf);
1085 stream_get (&nexthop, client->ibuf, sizeof (struct in6_addr));
1086
1087 while (stream_pnt (client->ibuf) < lim)
1088 {
1089 int size;
1090 struct prefix_ipv6 p;
1091
1092 ifindex = stream_getl (client->ibuf);
1093
1094 memset (&p, 0, sizeof (struct prefix_ipv6));
1095 p.family = AF_INET6;
1096 p.prefixlen = stream_getc (client->ibuf);
1097 size = PSIZE(p.prefixlen);
1098 stream_get (&p.prefix, client->ibuf, size);
1099
1100 if (IN6_IS_ADDR_UNSPECIFIED (&nexthop))
1101 gate = NULL;
1102 else
1103 gate = &nexthop;
1104
1105 if (command == ZEBRA_IPV6_ROUTE_ADD)
paulb9df2d22004-05-09 09:09:59 +00001106 rib_add_ipv6 (type, flags, &p, gate, ifindex, 0);
paul718e3742002-12-13 20:15:29 +00001107 else
paulb9df2d22004-05-09 09:09:59 +00001108 rib_delete_ipv6 (type, flags, &p, gate, ifindex, 0);
paul718e3742002-12-13 20:15:29 +00001109 }
1110}
1111
paulb9df2d22004-05-09 09:09:59 +00001112static void
paul718e3742002-12-13 20:15:29 +00001113zread_ipv6_nexthop_lookup (struct zserv *client, u_short length)
1114{
1115 struct in6_addr addr;
1116 char buf[BUFSIZ];
1117
1118 stream_get (&addr, client->ibuf, 16);
1119 printf ("DEBUG %s\n", inet_ntop (AF_INET6, &addr, buf, BUFSIZ));
1120
1121 zsend_ipv6_nexthop_lookup (client, &addr);
1122}
1123#endif /* HAVE_IPV6 */
1124
1125/* Close zebra client. */
paulb9df2d22004-05-09 09:09:59 +00001126static void
paul718e3742002-12-13 20:15:29 +00001127zebra_client_close (struct zserv *client)
1128{
1129 /* Close file descriptor. */
1130 if (client->sock)
1131 {
1132 close (client->sock);
1133 client->sock = -1;
1134 }
1135
1136 /* Free stream buffers. */
1137 if (client->ibuf)
1138 stream_free (client->ibuf);
1139 if (client->obuf)
1140 stream_free (client->obuf);
1141
1142 /* Release threads. */
1143 if (client->t_read)
1144 thread_cancel (client->t_read);
1145 if (client->t_write)
1146 thread_cancel (client->t_write);
1147
1148 /* Free client structure. */
paulb21b19c2003-06-15 01:28:29 +00001149 listnode_delete (zebrad.client_list, client);
paul718e3742002-12-13 20:15:29 +00001150 XFREE (0, client);
1151}
1152
1153/* Make new client. */
paulb9df2d22004-05-09 09:09:59 +00001154static void
paul718e3742002-12-13 20:15:29 +00001155zebra_client_create (int sock)
1156{
1157 struct zserv *client;
1158
1159 client = XCALLOC (0, sizeof (struct zserv));
1160
1161 /* Make client input/output buffer. */
1162 client->sock = sock;
1163 client->ibuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
1164 client->obuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
1165
1166 /* Set table number. */
paulb21b19c2003-06-15 01:28:29 +00001167 client->rtm_table = zebrad.rtm_table_default;
paul718e3742002-12-13 20:15:29 +00001168
1169 /* Add this client to linked list. */
paulb21b19c2003-06-15 01:28:29 +00001170 listnode_add (zebrad.client_list, client);
paul718e3742002-12-13 20:15:29 +00001171
1172 /* Make new read thread. */
1173 zebra_event (ZEBRA_READ, sock, client);
1174}
1175
1176/* Handler of zebra service request. */
paulb9df2d22004-05-09 09:09:59 +00001177static int
paul718e3742002-12-13 20:15:29 +00001178zebra_client_read (struct thread *thread)
1179{
1180 int sock;
1181 struct zserv *client;
1182 int nbyte;
1183 u_short length;
1184 u_char command;
1185
1186 /* Get thread data. Reset reading thread because I'm running. */
1187 sock = THREAD_FD (thread);
1188 client = THREAD_ARG (thread);
1189 client->t_read = NULL;
1190
1191 /* Read length and command. */
1192 nbyte = stream_read (client->ibuf, sock, 3);
1193 if (nbyte <= 0)
1194 {
1195 if (IS_ZEBRA_DEBUG_EVENT)
1196 zlog_info ("connection closed socket [%d]", sock);
1197 zebra_client_close (client);
1198 return -1;
1199 }
1200 length = stream_getw (client->ibuf);
1201 command = stream_getc (client->ibuf);
1202
1203 if (length < 3)
1204 {
1205 if (IS_ZEBRA_DEBUG_EVENT)
1206 zlog_info ("length %d is less than 3 ", length);
1207 zebra_client_close (client);
1208 return -1;
1209 }
1210
1211 length -= 3;
1212
1213 /* Read rest of data. */
1214 if (length)
1215 {
1216 nbyte = stream_read (client->ibuf, sock, length);
1217 if (nbyte <= 0)
1218 {
1219 if (IS_ZEBRA_DEBUG_EVENT)
1220 zlog_info ("connection closed [%d] when reading zebra data", sock);
1221 zebra_client_close (client);
1222 return -1;
1223 }
1224 }
1225
1226 /* Debug packet information. */
1227 if (IS_ZEBRA_DEBUG_EVENT)
1228 zlog_info ("zebra message comes from socket [%d]", sock);
1229
1230 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
1231 zlog_info ("zebra message received [%s] %d",
1232 zebra_command_str[command], length);
1233
1234 switch (command)
1235 {
1236 case ZEBRA_INTERFACE_ADD:
1237 zread_interface_add (client, length);
1238 break;
1239 case ZEBRA_INTERFACE_DELETE:
1240 zread_interface_delete (client, length);
1241 break;
1242 case ZEBRA_IPV4_ROUTE_ADD:
1243 zread_ipv4_add (client, length);
1244 break;
1245 case ZEBRA_IPV4_ROUTE_DELETE:
1246 zread_ipv4_delete (client, length);
1247 break;
1248#ifdef HAVE_IPV6
1249 case ZEBRA_IPV6_ROUTE_ADD:
1250 zread_ipv6_add (client, length);
1251 break;
1252 case ZEBRA_IPV6_ROUTE_DELETE:
1253 zread_ipv6_delete (client, length);
1254 break;
1255#endif /* HAVE_IPV6 */
1256 case ZEBRA_REDISTRIBUTE_ADD:
1257 zebra_redistribute_add (command, client, length);
1258 break;
1259 case ZEBRA_REDISTRIBUTE_DELETE:
1260 zebra_redistribute_delete (command, client, length);
1261 break;
1262 case ZEBRA_REDISTRIBUTE_DEFAULT_ADD:
1263 zebra_redistribute_default_add (command, client, length);
1264 break;
1265 case ZEBRA_REDISTRIBUTE_DEFAULT_DELETE:
1266 zebra_redistribute_default_delete (command, client, length);
1267 break;
1268 case ZEBRA_IPV4_NEXTHOP_LOOKUP:
1269 zread_ipv4_nexthop_lookup (client, length);
1270 break;
1271#ifdef HAVE_IPV6
1272 case ZEBRA_IPV6_NEXTHOP_LOOKUP:
1273 zread_ipv6_nexthop_lookup (client, length);
1274 break;
1275#endif /* HAVE_IPV6 */
1276 case ZEBRA_IPV4_IMPORT_LOOKUP:
1277 zread_ipv4_import_lookup (client, length);
1278 break;
1279 default:
1280 zlog_info ("Zebra received unknown command %d", command);
1281 break;
1282 }
1283
1284 stream_reset (client->ibuf);
1285 zebra_event (ZEBRA_READ, sock, client);
1286
1287 return 0;
1288}
1289
paul718e3742002-12-13 20:15:29 +00001290
1291/* Accept code of zebra server socket. */
paulb9df2d22004-05-09 09:09:59 +00001292static int
paul718e3742002-12-13 20:15:29 +00001293zebra_accept (struct thread *thread)
1294{
paulccf35572003-03-01 11:42:20 +00001295 int val;
paul718e3742002-12-13 20:15:29 +00001296 int accept_sock;
1297 int client_sock;
1298 struct sockaddr_in client;
1299 socklen_t len;
1300
1301 accept_sock = THREAD_FD (thread);
1302
1303 len = sizeof (struct sockaddr_in);
1304 client_sock = accept (accept_sock, (struct sockaddr *) &client, &len);
1305
1306 if (client_sock < 0)
1307 {
1308 zlog_warn ("Can't accept zebra socket: %s", strerror (errno));
1309 return -1;
1310 }
1311
paulccf35572003-03-01 11:42:20 +00001312 /* Make client socket non-blocking. */
1313
1314 val = fcntl (client_sock, F_GETFL, 0);
1315 fcntl (client_sock, F_SETFL, (val | O_NONBLOCK));
1316
paul718e3742002-12-13 20:15:29 +00001317 /* Create new zebra client. */
1318 zebra_client_create (client_sock);
1319
1320 /* Register myself. */
1321 zebra_event (ZEBRA_SERV, accept_sock, NULL);
1322
1323 return 0;
1324}
1325
paulb9df2d22004-05-09 09:09:59 +00001326#ifdef HAVE_TCP_ZEBRA
paul718e3742002-12-13 20:15:29 +00001327/* Make zebra's server socket. */
paulb9df2d22004-05-09 09:09:59 +00001328static void
paul718e3742002-12-13 20:15:29 +00001329zebra_serv ()
1330{
1331 int ret;
1332 int accept_sock;
1333 struct sockaddr_in addr;
1334
1335 accept_sock = socket (AF_INET, SOCK_STREAM, 0);
1336
1337 if (accept_sock < 0)
1338 {
1339 zlog_warn ("Can't bind to socket: %s", strerror (errno));
1340 zlog_warn ("zebra can't provice full functionality due to above error");
1341 return;
1342 }
1343
1344 memset (&addr, 0, sizeof (struct sockaddr_in));
1345 addr.sin_family = AF_INET;
1346 addr.sin_port = htons (ZEBRA_PORT);
1347#ifdef HAVE_SIN_LEN
1348 addr.sin_len = sizeof (struct sockaddr_in);
1349#endif /* HAVE_SIN_LEN */
1350 addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
1351
1352 sockopt_reuseaddr (accept_sock);
1353 sockopt_reuseport (accept_sock);
1354
pauledd7c242003-06-04 13:59:38 +00001355 if ( zserv_privs.change(ZPRIVS_RAISE) )
1356 zlog (NULL, LOG_ERR, "Can't raise privileges");
1357
paul718e3742002-12-13 20:15:29 +00001358 ret = bind (accept_sock, (struct sockaddr *)&addr,
1359 sizeof (struct sockaddr_in));
1360 if (ret < 0)
1361 {
1362 zlog_warn ("Can't bind to socket: %s", strerror (errno));
1363 zlog_warn ("zebra can't provice full functionality due to above error");
1364 close (accept_sock); /* Avoid sd leak. */
1365 return;
1366 }
pauledd7c242003-06-04 13:59:38 +00001367
1368 if ( zserv_privs.change(ZPRIVS_LOWER) )
1369 zlog (NULL, LOG_ERR, "Can't lower privileges");
paul718e3742002-12-13 20:15:29 +00001370
1371 ret = listen (accept_sock, 1);
1372 if (ret < 0)
1373 {
1374 zlog_warn ("Can't listen to socket: %s", strerror (errno));
1375 zlog_warn ("zebra can't provice full functionality due to above error");
1376 close (accept_sock); /* Avoid sd leak. */
1377 return;
1378 }
1379
1380 zebra_event (ZEBRA_SERV, accept_sock, NULL);
1381}
paulb9df2d22004-05-09 09:09:59 +00001382#endif /* HAVE_TCP_ZEBRA */
paul718e3742002-12-13 20:15:29 +00001383
1384/* For sockaddr_un. */
1385#include <sys/un.h>
1386
1387/* zebra server UNIX domain socket. */
paulb9df2d22004-05-09 09:09:59 +00001388static void
paul718e3742002-12-13 20:15:29 +00001389zebra_serv_un (char *path)
1390{
1391 int ret;
1392 int sock, len;
1393 struct sockaddr_un serv;
1394 mode_t old_mask;
1395
1396 /* First of all, unlink existing socket */
1397 unlink (path);
1398
1399 /* Set umask */
1400 old_mask = umask (0077);
1401
1402 /* Make UNIX domain socket. */
1403 sock = socket (AF_UNIX, SOCK_STREAM, 0);
1404 if (sock < 0)
1405 {
1406 perror ("sock");
1407 return;
1408 }
1409
1410 /* Make server socket. */
1411 memset (&serv, 0, sizeof (struct sockaddr_un));
1412 serv.sun_family = AF_UNIX;
1413 strncpy (serv.sun_path, path, strlen (path));
1414#ifdef HAVE_SUN_LEN
1415 len = serv.sun_len = SUN_LEN(&serv);
1416#else
1417 len = sizeof (serv.sun_family) + strlen (serv.sun_path);
1418#endif /* HAVE_SUN_LEN */
1419
1420 ret = bind (sock, (struct sockaddr *) &serv, len);
1421 if (ret < 0)
1422 {
1423 perror ("bind");
1424 close (sock);
1425 return;
1426 }
1427
1428 ret = listen (sock, 5);
1429 if (ret < 0)
1430 {
1431 perror ("listen");
1432 close (sock);
1433 return;
1434 }
1435
1436 umask (old_mask);
1437
1438 zebra_event (ZEBRA_SERV, sock, NULL);
1439}
1440
paul718e3742002-12-13 20:15:29 +00001441
paulb9df2d22004-05-09 09:09:59 +00001442static void
paul718e3742002-12-13 20:15:29 +00001443zebra_event (enum event event, int sock, struct zserv *client)
1444{
1445 switch (event)
1446 {
1447 case ZEBRA_SERV:
paulb21b19c2003-06-15 01:28:29 +00001448 thread_add_read (zebrad.master, zebra_accept, client, sock);
paul718e3742002-12-13 20:15:29 +00001449 break;
1450 case ZEBRA_READ:
1451 client->t_read =
paulb21b19c2003-06-15 01:28:29 +00001452 thread_add_read (zebrad.master, zebra_client_read, client, sock);
paul718e3742002-12-13 20:15:29 +00001453 break;
1454 case ZEBRA_WRITE:
1455 /**/
1456 break;
1457 }
1458}
1459
1460/* Display default rtm_table for all clients. */
1461DEFUN (show_table,
1462 show_table_cmd,
1463 "show table",
1464 SHOW_STR
1465 "default routing table to use for all clients\n")
1466{
paulb21b19c2003-06-15 01:28:29 +00001467 vty_out (vty, "table %d%s", zebrad.rtm_table_default,
paul718e3742002-12-13 20:15:29 +00001468 VTY_NEWLINE);
1469 return CMD_SUCCESS;
1470}
1471
1472DEFUN (config_table,
1473 config_table_cmd,
1474 "table TABLENO",
1475 "Configure target kernel routing table\n"
1476 "TABLE integer\n")
1477{
paulb21b19c2003-06-15 01:28:29 +00001478 zebrad.rtm_table_default = strtol (argv[0], (char**)0, 10);
paul718e3742002-12-13 20:15:29 +00001479 return CMD_SUCCESS;
1480}
1481
hasso647e4f12003-05-25 11:43:52 +00001482DEFUN (ip_forwarding,
1483 ip_forwarding_cmd,
1484 "ip forwarding",
1485 IP_STR
1486 "Turn on IP forwarding")
1487{
1488 int ret;
1489
1490 ret = ipforward ();
1491
1492 if (ret != 0)
1493 {
1494 vty_out (vty, "IP forwarding is already on%s", VTY_NEWLINE);
1495 return CMD_ERR_NOTHING_TODO;
1496 }
1497
1498 ret = ipforward_on ();
1499 if (ret == 0)
1500 {
1501 vty_out (vty, "Can't turn on IP forwarding%s", VTY_NEWLINE);
1502 return CMD_WARNING;
1503 }
1504
1505 return CMD_SUCCESS;
1506}
1507
paul718e3742002-12-13 20:15:29 +00001508DEFUN (no_ip_forwarding,
1509 no_ip_forwarding_cmd,
1510 "no ip forwarding",
1511 NO_STR
1512 IP_STR
1513 "Turn off IP forwarding")
1514{
1515 int ret;
1516
1517 ret = ipforward ();
1518
1519 if (ret == 0)
1520 {
1521 vty_out (vty, "IP forwarding is already off%s", VTY_NEWLINE);
1522 return CMD_ERR_NOTHING_TODO;
1523 }
1524
1525 ret = ipforward_off ();
1526 if (ret != 0)
1527 {
1528 vty_out (vty, "Can't turn off IP forwarding%s", VTY_NEWLINE);
1529 return CMD_WARNING;
1530 }
1531
1532 return CMD_SUCCESS;
1533}
1534
1535/* This command is for debugging purpose. */
1536DEFUN (show_zebra_client,
1537 show_zebra_client_cmd,
1538 "show zebra client",
1539 SHOW_STR
1540 "Zebra information"
1541 "Client information")
1542{
1543 listnode node;
1544 struct zserv *client;
1545
paulb21b19c2003-06-15 01:28:29 +00001546 for (node = listhead (zebrad.client_list); node; nextnode (node))
paul718e3742002-12-13 20:15:29 +00001547 {
1548 client = getdata (node);
1549 vty_out (vty, "Client fd %d%s", client->sock, VTY_NEWLINE);
1550 }
1551 return CMD_SUCCESS;
1552}
1553
1554/* Table configuration write function. */
paulb9df2d22004-05-09 09:09:59 +00001555static int
paul718e3742002-12-13 20:15:29 +00001556config_write_table (struct vty *vty)
1557{
paulb21b19c2003-06-15 01:28:29 +00001558 if (zebrad.rtm_table_default)
1559 vty_out (vty, "table %d%s", zebrad.rtm_table_default,
paul718e3742002-12-13 20:15:29 +00001560 VTY_NEWLINE);
1561 return 0;
1562}
1563
1564/* table node for routing tables. */
1565struct cmd_node table_node =
1566{
1567 TABLE_NODE,
1568 "", /* This node has no interface. */
1569 1
1570};
1571
1572/* Only display ip forwarding is enabled or not. */
1573DEFUN (show_ip_forwarding,
1574 show_ip_forwarding_cmd,
1575 "show ip forwarding",
1576 SHOW_STR
1577 IP_STR
1578 "IP forwarding status\n")
1579{
1580 int ret;
1581
1582 ret = ipforward ();
1583
1584 if (ret == 0)
1585 vty_out (vty, "IP forwarding is off%s", VTY_NEWLINE);
1586 else
1587 vty_out (vty, "IP forwarding is on%s", VTY_NEWLINE);
1588 return CMD_SUCCESS;
1589}
1590
1591#ifdef HAVE_IPV6
1592/* Only display ipv6 forwarding is enabled or not. */
1593DEFUN (show_ipv6_forwarding,
1594 show_ipv6_forwarding_cmd,
1595 "show ipv6 forwarding",
1596 SHOW_STR
1597 "IPv6 information\n"
1598 "Forwarding status\n")
1599{
1600 int ret;
1601
1602 ret = ipforward_ipv6 ();
1603
1604 switch (ret)
1605 {
1606 case -1:
1607 vty_out (vty, "ipv6 forwarding is unknown%s", VTY_NEWLINE);
1608 break;
1609 case 0:
1610 vty_out (vty, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE);
1611 break;
1612 case 1:
1613 vty_out (vty, "ipv6 forwarding is %s%s", "on", VTY_NEWLINE);
1614 break;
1615 default:
1616 vty_out (vty, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE);
1617 break;
1618 }
1619 return CMD_SUCCESS;
1620}
1621
hasso55906722004-02-11 22:42:16 +00001622DEFUN (ipv6_forwarding,
1623 ipv6_forwarding_cmd,
1624 "ipv6 forwarding",
1625 IPV6_STR
1626 "Turn on IPv6 forwarding")
1627{
1628 int ret;
1629
hasso41d3fc92004-04-06 11:59:00 +00001630 ret = ipforward_ipv6 ();
hasso55906722004-02-11 22:42:16 +00001631 if (ret != 0)
1632 {
hasso41d3fc92004-04-06 11:59:00 +00001633 vty_out (vty, "IPv6 forwarding is already on%s", VTY_NEWLINE);
1634 return CMD_ERR_NOTHING_TODO;
1635 }
1636
1637 ret = ipforward_ipv6_on ();
1638 if (ret == 0)
1639 {
hasso55906722004-02-11 22:42:16 +00001640 vty_out (vty, "Can't turn on IPv6 forwarding%s", VTY_NEWLINE);
1641 return CMD_WARNING;
1642 }
1643
1644 return CMD_SUCCESS;
1645}
1646
paul718e3742002-12-13 20:15:29 +00001647DEFUN (no_ipv6_forwarding,
1648 no_ipv6_forwarding_cmd,
1649 "no ipv6 forwarding",
1650 NO_STR
hasso55906722004-02-11 22:42:16 +00001651 IPV6_STR
1652 "Turn off IPv6 forwarding")
paul718e3742002-12-13 20:15:29 +00001653{
1654 int ret;
1655
hasso41d3fc92004-04-06 11:59:00 +00001656 ret = ipforward_ipv6 ();
1657 if (ret == 0)
1658 {
1659 vty_out (vty, "IP forwarding is already off%s", VTY_NEWLINE);
1660 return CMD_ERR_NOTHING_TODO;
1661 }
1662
paul718e3742002-12-13 20:15:29 +00001663 ret = ipforward_ipv6_off ();
1664 if (ret != 0)
1665 {
1666 vty_out (vty, "Can't turn off IPv6 forwarding%s", VTY_NEWLINE);
1667 return CMD_WARNING;
1668 }
1669
1670 return CMD_SUCCESS;
1671}
1672
1673#endif /* HAVE_IPV6 */
1674
1675/* IPForwarding configuration write function. */
1676int
1677config_write_forwarding (struct vty *vty)
1678{
paul3e0b3a52004-08-23 18:58:32 +00001679 if (ipforward ())
1680 vty_out (vty, "ip forwarding%s", VTY_NEWLINE);
paul718e3742002-12-13 20:15:29 +00001681#ifdef HAVE_IPV6
paul3e0b3a52004-08-23 18:58:32 +00001682 if (ipforward_ipv6 ())
1683 vty_out (vty, "ipv6 forwarding%s", VTY_NEWLINE);
paul718e3742002-12-13 20:15:29 +00001684#endif /* HAVE_IPV6 */
1685 vty_out (vty, "!%s", VTY_NEWLINE);
1686 return 0;
1687}
1688
1689/* table node for routing tables. */
1690struct cmd_node forwarding_node =
1691{
1692 FORWARDING_NODE,
1693 "", /* This node has no interface. */
1694 1
1695};
1696
1697
1698/* Initialisation of zebra and installation of commands. */
1699void
1700zebra_init ()
1701{
1702 /* Client list init. */
paulb21b19c2003-06-15 01:28:29 +00001703 zebrad.client_list = list_new ();
paul718e3742002-12-13 20:15:29 +00001704
paul718e3742002-12-13 20:15:29 +00001705 /* Make zebra server socket. */
1706#ifdef HAVE_TCP_ZEBRA
1707 zebra_serv ();
1708#else
1709 zebra_serv_un (ZEBRA_SERV_PATH);
1710#endif /* HAVE_TCP_ZEBRA */
1711
1712 /* Install configuration write function. */
1713 install_node (&table_node, config_write_table);
1714 install_node (&forwarding_node, config_write_forwarding);
1715
1716 install_element (VIEW_NODE, &show_ip_forwarding_cmd);
1717 install_element (ENABLE_NODE, &show_ip_forwarding_cmd);
hasso647e4f12003-05-25 11:43:52 +00001718 install_element (CONFIG_NODE, &ip_forwarding_cmd);
paul718e3742002-12-13 20:15:29 +00001719 install_element (CONFIG_NODE, &no_ip_forwarding_cmd);
1720 install_element (ENABLE_NODE, &show_zebra_client_cmd);
1721
1722#ifdef HAVE_NETLINK
1723 install_element (VIEW_NODE, &show_table_cmd);
1724 install_element (ENABLE_NODE, &show_table_cmd);
1725 install_element (CONFIG_NODE, &config_table_cmd);
1726#endif /* HAVE_NETLINK */
1727
1728#ifdef HAVE_IPV6
1729 install_element (VIEW_NODE, &show_ipv6_forwarding_cmd);
1730 install_element (ENABLE_NODE, &show_ipv6_forwarding_cmd);
hasso55906722004-02-11 22:42:16 +00001731 install_element (CONFIG_NODE, &ipv6_forwarding_cmd);
paul718e3742002-12-13 20:15:29 +00001732 install_element (CONFIG_NODE, &no_ipv6_forwarding_cmd);
1733#endif /* HAVE_IPV6 */
paulccf35572003-03-01 11:42:20 +00001734
1735 FIFO_INIT(&message_queue);
1736 t_write = NULL;
paul718e3742002-12-13 20:15:29 +00001737}