]> git.proxmox.com Git - mirror_frr.git/blame - zebra/zserv.c
No warnings here any more.
[mirror_frr.git] / zebra / zserv.c
CommitLineData
718e3744 1/* 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"
edd7c245 36#include "privs.h"
718e3744 37
38#include "zebra/zserv.h"
18a6dce6 39#include "zebra/router-id.h"
718e3744 40#include "zebra/redistribute.h"
41#include "zebra/debug.h"
42#include "zebra/ipforward.h"
43\f
44/* Event list of zebra. */
45enum event { ZEBRA_SERV, ZEBRA_READ, ZEBRA_WRITE };
46
b21b19c5 47extern struct zebra_t zebrad;
718e3744 48
b9df2d25 49static void zebra_event (enum event event, int sock, struct zserv *client);
ccf3557b 50
edd7c245 51extern struct zebra_privs_t zserv_privs;
718e3744 52\f
53/* For logging of zebra meesages. */
b9df2d25 54static char *zebra_command_str [] =
718e3744 55{
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",
18a6dce6 74 "ZEBRA_IPV6_IMPORT_LOOKUP",
75 "ZEBRA_ROUTER_ID_ADD",
76 "ZEBRA_ROUTER_ID_DELETE",
77 "ZEBRA_ROUTER_ID_UPDATE"
718e3744 78};
79\f
ccf3557b 80struct 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))
b21b19c5 127 THREAD_WRITE_ON (zebrad.master, t_write, zebra_server_dequeue,
128 NULL, sock);
ccf3557b 129
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
b21b19c5 148 THREAD_WRITE_ON (zebrad.master, t_write, zebra_server_dequeue, NULL, sock);
ccf3557b 149}
150
151int
152zebra_server_send_message (int sock, u_char *buf, unsigned long length)
153{
154 int nbytes;
155
156 if (FIFO_TOP (&message_queue))
157 {
158 zebra_server_enqueue (sock, buf, length, 0);
159 return 0;
160 }
161
162 /* Send message. */
163 nbytes = write (sock, buf, length);
164
165 if (nbytes <= 0)
166 {
167 if (errno == EAGAIN)
168 zebra_server_enqueue (sock, buf, length, 0);
169 else
170 return -1;
171 }
172 else if (nbytes != length)
173 zebra_server_enqueue (sock, buf, length, nbytes);
174
175 return 0;
176}
177
718e3744 178/* Interface is added. Send ZEBRA_INTERFACE_ADD to client. */
b9df2d25 179/*
180 * This function is called in the following situations:
181 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
182 * from the client.
183 * - at startup, when zebra figures out the available interfaces
184 * - when an interface is added (where support for
185 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
186 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
187 * received)
188 */
718e3744 189int
190zsend_interface_add (struct zserv *client, struct interface *ifp)
191{
192 struct stream *s;
193
194 /* Check this client need interface information. */
195 if (! client->ifinfo)
196 return -1;
197
198 s = client->obuf;
199 stream_reset (s);
200
201 /* Place holder for size. */
202 stream_putw (s, 0);
203
204 /* Message type. */
205 stream_putc (s, ZEBRA_INTERFACE_ADD);
206
207 /* Interface information. */
208 stream_put (s, ifp->name, INTERFACE_NAMSIZ);
209 stream_putl (s, ifp->ifindex);
2e3b2e47 210 stream_putc (s, ifp->status);
718e3744 211 stream_putl (s, ifp->flags);
212 stream_putl (s, ifp->metric);
213 stream_putl (s, ifp->mtu);
b9df2d25 214 stream_putl (s, ifp->mtu6);
718e3744 215 stream_putl (s, ifp->bandwidth);
216#ifdef HAVE_SOCKADDR_DL
217 stream_put (s, &ifp->sdl, sizeof (ifp->sdl));
218#else
219 stream_putl (s, ifp->hw_addr_len);
220 if (ifp->hw_addr_len)
221 stream_put (s, ifp->hw_addr, ifp->hw_addr_len);
222#endif /* HAVE_SOCKADDR_DL */
223
224 /* Write packet size. */
225 stream_putw_at (s, 0, stream_get_endp (s));
226
ccf3557b 227 zebra_server_send_message (client->sock, s->data, stream_get_endp (s));
228
229 return 0;
718e3744 230}
231
232/* Interface deletion from zebra daemon. */
b9df2d25 233/*
234 * This function is only called when support for
235 * RTM_IFANNOUNCE or AF_NETLINK sockets (RTM_DELLINK message)
236 * is available. It is not called on Solaris.
237 */
238#if (defined(RTM_IFANNOUNCE) || defined(HAVE_NETLINK))
718e3744 239int
240zsend_interface_delete (struct zserv *client, struct interface *ifp)
241{
242 struct stream *s;
243
244 /* Check this client need interface information. */
245 if (! client->ifinfo)
246 return -1;
247
248 s = client->obuf;
249 stream_reset (s);
250
251 /* Packet length placeholder. */
252 stream_putw (s, 0);
253
254 /* Interface information. */
255 stream_putc (s, ZEBRA_INTERFACE_DELETE);
256 stream_put (s, ifp->name, INTERFACE_NAMSIZ);
257 stream_putl (s, ifp->ifindex);
2e3b2e47 258 stream_putc (s, ifp->status);
718e3744 259 stream_putl (s, ifp->flags);
260 stream_putl (s, ifp->metric);
261 stream_putl (s, ifp->mtu);
b9df2d25 262 stream_putl (s, ifp->mtu6);
718e3744 263 stream_putl (s, ifp->bandwidth);
264
265 /* Write packet length. */
266 stream_putw_at (s, 0, stream_get_endp (s));
267
ccf3557b 268 zebra_server_send_message (client->sock, s->data, stream_get_endp (s));
269
270 return 0;
718e3744 271}
b9df2d25 272#endif /* (defined(RTM_IFANNOUNCE) || defined(HAVE_LINUX_RTNETLINK_H)) */
718e3744 273
b9df2d25 274/* Interface address is added/deleted. Send ZEBRA_INTERFACE_ADDRESS_ADD or
275 * ZEBRA_INTERFACE_ADDRESS_DELETE to the client.
276 *
277 * A ZEBRA_INTERFACE_ADDRESS_ADD is sent in the following situations:
278 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
279 * from the client, after the ZEBRA_INTERFACE_ADD has been
280 * sent from zebra to the client
281 * - redistribute new address info to all clients in the following situations
282 * - at startup, when zebra figures out the available interfaces
283 * - when an interface is added (where support for
284 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
285 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
286 * received)
287 * - for the vty commands "ip address A.B.C.D/M [<secondary>|<label LINE>]"
288 * and "no bandwidth <1-10000000>", "ipv6 address X:X::X:X/M"
289 * - when an RTM_NEWADDR message is received from the kernel,
290 *
291 * The call tree that triggers ZEBRA_INTERFACE_ADDRESS_DELETE:
292 *
293 * zsend_interface_address(DELETE)
294 * ^
295 * |
296 * zebra_interface_address_delete_update
297 * ^ ^ ^
298 * | | if_delete_update (not called on
299 * | | Solaris)
300 * ip_address_uninstall connected_delete_ipv4
301 * [ipv6_addresss_uninstall] [connected_delete_ipv6]
302 * ^ ^
303 * | |
304 * | RTM_NEWADDR on routing/netlink socket
305 * |
306 * vty commands:
307 * "no ip address A.B.C.D/M [label LINE]"
308 * "no ip address A.B.C.D/M secondary"
309 * ["no ipv6 address X:X::X:X/M"]
310 *
311 */
718e3744 312int
b9df2d25 313zsend_interface_address (int cmd, struct zserv *client,
314 struct interface *ifp, struct connected *ifc)
718e3744 315{
316 int blen;
317 struct stream *s;
318 struct prefix *p;
319
320 /* Check this client need interface information. */
321 if (! client->ifinfo)
322 return -1;
323
324 s = client->obuf;
325 stream_reset (s);
326
327 /* Place holder for size. */
328 stream_putw (s, 0);
329
b9df2d25 330 stream_putc (s, cmd);
718e3744 331 stream_putl (s, ifp->ifindex);
332
333 /* Interface address flag. */
334 stream_putc (s, ifc->flags);
335
336 /* Prefix information. */
337 p = ifc->address;
338 stream_putc (s, p->family);
339 blen = prefix_blen (p);
340 stream_put (s, &p->u.prefix, blen);
b9df2d25 341
342 /*
343 * XXX gnu version does not send prefixlen for ZEBRA_INTERFACE_ADDRESS_DELETE
344 * but zebra_interface_address_delete_read() in the gnu version
345 * expects to find it
346 */
718e3744 347 stream_putc (s, p->prefixlen);
348
349 /* Destination. */
350 p = ifc->destination;
351 if (p)
352 stream_put (s, &p->u.prefix, blen);
353 else
354 stream_put (s, NULL, blen);
355
356 /* Write packet size. */
357 stream_putw_at (s, 0, stream_get_endp (s));
358
ccf3557b 359 zebra_server_send_message (client->sock, s->data, stream_get_endp (s));
360
361 return 0;
718e3744 362}
363
b9df2d25 364/*
365 * The cmd passed to zsend_interface_update may be ZEBRA_INTERFACE_UP or
366 * ZEBRA_INTERFACE_DOWN.
367 *
368 * The ZEBRA_INTERFACE_UP message is sent from the zebra server to
369 * the clients in one of 2 situations:
370 * - an if_up is detected e.g., as a result of an RTM_IFINFO message
371 * - a vty command modifying the bandwidth of an interface is received.
372 * The ZEBRA_INTERFACE_DOWN message is sent when an if_down is detected.
373 */
718e3744 374int
b9df2d25 375zsend_interface_update (int cmd, struct zserv *client, struct interface *ifp)
718e3744 376{
377 struct stream *s;
378
379 /* Check this client need interface information. */
380 if (! client->ifinfo)
381 return -1;
382
383 s = client->obuf;
384 stream_reset (s);
385
386 /* Place holder for size. */
387 stream_putw (s, 0);
388
389 /* Zebra command. */
b9df2d25 390 stream_putc (s, cmd);
718e3744 391
392 /* Interface information. */
393 stream_put (s, ifp->name, INTERFACE_NAMSIZ);
394 stream_putl (s, ifp->ifindex);
2e3b2e47 395 stream_putc (s, ifp->status);
718e3744 396 stream_putl (s, ifp->flags);
397 stream_putl (s, ifp->metric);
398 stream_putl (s, ifp->mtu);
b9df2d25 399 stream_putl (s, ifp->mtu6);
718e3744 400 stream_putl (s, ifp->bandwidth);
401
402 /* Write packet size. */
403 stream_putw_at (s, 0, stream_get_endp (s));
404
ccf3557b 405 zebra_server_send_message (client->sock, s->data, stream_get_endp (s));
406
407 return 0;
718e3744 408}
409
b9df2d25 410/*
411 * The zebra server sends the clients a ZEBRA_IPV4_ROUTE_ADD or a
412 * ZEBRA_IPV6_ROUTE_ADD via zsend_route_multipath in the following
413 * situations:
414 * - when the client starts up, and requests default information
415 * by sending a ZEBRA_REDISTRIBUTE_DEFAULT_ADD to the zebra server, in the
416 * - case of rip, ripngd, ospfd and ospf6d, when the client sends a
417 * ZEBRA_REDISTRIBUTE_ADD as a result of the "redistribute" vty cmd,
418 * - when the zebra server redistributes routes after it updates its rib
419 *
420 * The zebra server sends clients a ZEBRA_IPV4_ROUTE_DELETE or a
421 * ZEBRA_IPV6_ROUTE_DELETE via zsend_route_multipath when:
422 * - a "ip route" or "ipv6 route" vty command is issued, a prefix is
423 * - deleted from zebra's rib, and this info
424 * has to be redistributed to the clients
425 *
426 * XXX The ZEBRA_IPV*_ROUTE_ADD message is also sent by the client to the
427 * zebra server when the client wants to tell the zebra server to add a
428 * route to the kernel (zapi_ipv4_add etc. ). Since it's essentially the
429 * same message being sent back and forth, this function and
430 * zapi_ipv{4,6}_{add, delete} should be re-written to avoid code
431 * duplication.
432 */
718e3744 433int
b9df2d25 434zsend_route_multipath (int cmd, struct zserv *client, struct prefix *p,
435 struct rib *rib)
718e3744 436{
437 int psize;
438 struct stream *s;
439 struct nexthop *nexthop;
b9df2d25 440 unsigned long nhnummark = 0;
441 int nhnum = 0;
442 u_char zapi_flags = ZAPI_MESSAGE_NEXTHOP | ZAPI_MESSAGE_IFINDEX;
443
718e3744 444 s = client->obuf;
445 stream_reset (s);
446
447 /* Place holder for size. */
448 stream_putw (s, 0);
449
450 /* Put command, type and nexthop. */
b9df2d25 451 stream_putc (s, cmd);
718e3744 452 stream_putc (s, rib->type);
453 stream_putc (s, rib->flags);
718e3744 454
b9df2d25 455 /*
456 * XXX no need to set ZAPI_MESSAGE_NEXTHOP if we are going to
457 * send empty nexthop?
458 */
459 if (cmd == ZEBRA_IPV4_ROUTE_ADD || ZEBRA_IPV6_ROUTE_ADD)
460 zapi_flags |= ZAPI_MESSAGE_METRIC;
718e3744 461
b9df2d25 462 stream_putc (s, zapi_flags);
718e3744 463
464 /* Prefix. */
465 psize = PSIZE (p->prefixlen);
466 stream_putc (s, p->prefixlen);
b9df2d25 467 stream_write (s, (u_char *) & p->u.prefix, psize);
468
469 /*
470 * XXX The message format sent by zebra below does not match the format
471 * of the corresponding message expected by the zebra server
472 * itself (e.g., see zread_ipv4_add). The nexthop_num is not set correctly,
473 * (is there a bug on the client side if more than one segment is sent?)
474 * nexthop ZEBRA_NEXTHOP_IPV4 is never set, ZEBRA_NEXTHOP_IFINDEX
475 * is hard-coded.
476 */
718e3744 477 /* Nexthop */
478 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
479 {
480 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB))
b9df2d25 481 {
482 nhnummark = stream_get_putp (s);
483 stream_putc (s, 1); /* placeholder */
484 nhnum++;
485
486 switch(nexthop->type)
487 {
488 case NEXTHOP_TYPE_IPV4:
489 case NEXTHOP_TYPE_IPV4_IFINDEX:
490 stream_put_in_addr (s, &nexthop->gate.ipv4);
491 break;
718e3744 492#ifdef HAVE_IPV6
b9df2d25 493 case NEXTHOP_TYPE_IPV6:
494 case NEXTHOP_TYPE_IPV6_IFINDEX:
495 case NEXTHOP_TYPE_IPV6_IFNAME:
496 stream_write (s, (u_char *) &nexthop->gate.ipv6, 16);
497 break;
498#endif
499 default:
500 if (cmd == ZEBRA_IPV4_ROUTE_ADD
501 || cmd == ZEBRA_IPV4_ROUTE_DELETE)
502 {
503 struct in_addr empty;
44983cf8 504 memset (&empty, 0, sizeof (struct in_addr));
b9df2d25 505 stream_write (s, (u_char *) &empty, IPV4_MAX_BYTELEN);
506 }
507 else
508 {
509 struct in6_addr empty;
510 memset (&empty, 0, sizeof (struct in6_addr));
511 stream_write (s, (u_char *) &empty, IPV6_MAX_BYTELEN);
512 }
513 }
514
515 /* Interface index. */
516 stream_putc (s, 1);
517 stream_putl (s, nexthop->ifindex);
518
519 break;
520 }
718e3744 521 }
522
523 /* Metric */
524 stream_putl (s, rib->metric);
525
b9df2d25 526 /* Write next-hop number */
527 if (nhnummark)
528 stream_putw_at (s, nhnummark, nhnum);
529
718e3744 530 /* Write packet size. */
531 stream_putw_at (s, 0, stream_get_endp (s));
532
ccf3557b 533 zebra_server_send_message (client->sock, s->data, stream_get_endp (s));
534
535 return 0;
718e3744 536}
537
b9df2d25 538#ifdef HAVE_IPV6
718e3744 539int
540zsend_ipv6_nexthop_lookup (struct zserv *client, struct in6_addr *addr)
541{
542 struct stream *s;
543 struct rib *rib;
544 unsigned long nump;
545 u_char num;
546 struct nexthop *nexthop;
547
548 /* Lookup nexthop. */
549 rib = rib_match_ipv6 (addr);
550
551 /* Get output stream. */
552 s = client->obuf;
553 stream_reset (s);
554
555 /* Fill in result. */
556 stream_putw (s, 0);
557 stream_putc (s, ZEBRA_IPV6_NEXTHOP_LOOKUP);
558 stream_put (s, &addr, 16);
559
560 if (rib)
561 {
562 stream_putl (s, rib->metric);
563 num = 0;
564 nump = s->putp;
565 stream_putc (s, 0);
566 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
567 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB))
568 {
569 stream_putc (s, nexthop->type);
570 switch (nexthop->type)
571 {
572 case ZEBRA_NEXTHOP_IPV6:
573 stream_put (s, &nexthop->gate.ipv6, 16);
574 break;
575 case ZEBRA_NEXTHOP_IPV6_IFINDEX:
576 case ZEBRA_NEXTHOP_IPV6_IFNAME:
577 stream_put (s, &nexthop->gate.ipv6, 16);
578 stream_putl (s, nexthop->ifindex);
579 break;
580 case ZEBRA_NEXTHOP_IFINDEX:
581 case ZEBRA_NEXTHOP_IFNAME:
582 stream_putl (s, nexthop->ifindex);
583 break;
fa2b17e3 584 default:
585 /* do nothing */
586 break;
718e3744 587 }
588 num++;
589 }
590 stream_putc_at (s, nump, num);
591 }
592 else
593 {
594 stream_putl (s, 0);
595 stream_putc (s, 0);
596 }
597
598 stream_putw_at (s, 0, stream_get_endp (s));
599
ccf3557b 600 zebra_server_send_message (client->sock, s->data, stream_get_endp (s));
601
602 return 0;
718e3744 603}
604#endif /* HAVE_IPV6 */
605
b9df2d25 606static int
718e3744 607zsend_ipv4_nexthop_lookup (struct zserv *client, struct in_addr addr)
608{
609 struct stream *s;
610 struct rib *rib;
611 unsigned long nump;
612 u_char num;
613 struct nexthop *nexthop;
614
615 /* Lookup nexthop. */
616 rib = rib_match_ipv4 (addr);
617
618 /* Get output stream. */
619 s = client->obuf;
620 stream_reset (s);
621
622 /* Fill in result. */
623 stream_putw (s, 0);
624 stream_putc (s, ZEBRA_IPV4_NEXTHOP_LOOKUP);
625 stream_put_in_addr (s, &addr);
626
627 if (rib)
628 {
629 stream_putl (s, rib->metric);
630 num = 0;
631 nump = s->putp;
632 stream_putc (s, 0);
633 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
634 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB))
635 {
636 stream_putc (s, nexthop->type);
637 switch (nexthop->type)
638 {
639 case ZEBRA_NEXTHOP_IPV4:
640 stream_put_in_addr (s, &nexthop->gate.ipv4);
641 break;
642 case ZEBRA_NEXTHOP_IFINDEX:
643 case ZEBRA_NEXTHOP_IFNAME:
644 stream_putl (s, nexthop->ifindex);
645 break;
fa2b17e3 646 default:
647 /* do nothing */
648 break;
718e3744 649 }
650 num++;
651 }
652 stream_putc_at (s, nump, num);
653 }
654 else
655 {
656 stream_putl (s, 0);
657 stream_putc (s, 0);
658 }
659
660 stream_putw_at (s, 0, stream_get_endp (s));
661
ccf3557b 662 zebra_server_send_message (client->sock, s->data, stream_get_endp (s));
663
664 return 0;
718e3744 665}
666
b9df2d25 667static int
718e3744 668zsend_ipv4_import_lookup (struct zserv *client, struct prefix_ipv4 *p)
669{
670 struct stream *s;
671 struct rib *rib;
672 unsigned long nump;
673 u_char num;
674 struct nexthop *nexthop;
675
676 /* Lookup nexthop. */
677 rib = rib_lookup_ipv4 (p);
678
679 /* Get output stream. */
680 s = client->obuf;
681 stream_reset (s);
682
683 /* Fill in result. */
684 stream_putw (s, 0);
685 stream_putc (s, ZEBRA_IPV4_IMPORT_LOOKUP);
686 stream_put_in_addr (s, &p->prefix);
687
688 if (rib)
689 {
690 stream_putl (s, rib->metric);
691 num = 0;
692 nump = s->putp;
693 stream_putc (s, 0);
694 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
695 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB))
696 {
697 stream_putc (s, nexthop->type);
698 switch (nexthop->type)
699 {
700 case ZEBRA_NEXTHOP_IPV4:
701 stream_put_in_addr (s, &nexthop->gate.ipv4);
702 break;
703 case ZEBRA_NEXTHOP_IFINDEX:
704 case ZEBRA_NEXTHOP_IFNAME:
705 stream_putl (s, nexthop->ifindex);
706 break;
fa2b17e3 707 default:
708 /* do nothing */
709 break;
718e3744 710 }
711 num++;
712 }
713 stream_putc_at (s, nump, num);
714 }
715 else
716 {
717 stream_putl (s, 0);
718 stream_putc (s, 0);
719 }
720
721 stream_putw_at (s, 0, stream_get_endp (s));
722
ccf3557b 723 zebra_server_send_message (client->sock, s->data, stream_get_endp (s));
724
725 return 0;
718e3744 726}
727\f
18a6dce6 728/* Router-id is updated. Send ZEBRA_ROUTER_ID_ADD to client. */
729int
730zsend_router_id_update (struct zserv *client, struct prefix *p)
731{
732 struct stream *s;
733 int blen;
734
735 /* Check this client need interface information. */
736 if (!client->ridinfo)
737 return -1;
738
739 s = client->obuf;
740 stream_reset (s);
741
742 /* Place holder for size. */
743 stream_putw (s, 0);
744
745 /* Message type. */
746 stream_putc (s, ZEBRA_ROUTER_ID_UPDATE);
747
748 /* Prefix information. */
749 stream_putc (s, p->family);
750 blen = prefix_blen (p);
751 stream_put (s, &p->u.prefix, blen);
752 stream_putc (s, p->prefixlen);
753
754 /* Write packet size. */
755 stream_putw_at (s, 0, stream_get_endp (s));
756
757 return writen (client->sock, s->data, stream_get_endp (s));
758}
759\f
718e3744 760/* Register zebra server interface information. Send current all
761 interface and address information. */
b9df2d25 762static void
718e3744 763zread_interface_add (struct zserv *client, u_short length)
764{
52dc7ee6 765 struct listnode *ifnode;
766 struct listnode *cnode;
718e3744 767 struct interface *ifp;
768 struct connected *c;
769
770 /* Interface information is needed. */
771 client->ifinfo = 1;
772
773 for (ifnode = listhead (iflist); ifnode; ifnode = nextnode (ifnode))
774 {
775 ifp = getdata (ifnode);
776
777 /* Skip pseudo interface. */
778 if (! CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE))
779 continue;
780
781 zsend_interface_add (client, ifp);
782
783 for (cnode = listhead (ifp->connected); cnode; nextnode (cnode))
784 {
785 c = getdata (cnode);
786 if (CHECK_FLAG (c->conf, ZEBRA_IFC_REAL))
b9df2d25 787 zsend_interface_address (ZEBRA_INTERFACE_ADDRESS_ADD, client,
788 ifp, c);
718e3744 789 }
790 }
791}
792
793/* Unregister zebra server interface information. */
b9df2d25 794static void
718e3744 795zread_interface_delete (struct zserv *client, u_short length)
796{
797 client->ifinfo = 0;
798}
799
800/* This function support multiple nexthop. */
b9df2d25 801/*
802 * Parse the ZEBRA_IPV4_ROUTE_ADD sent from client. Update rib and
803 * add kernel route.
804 */
805static void
718e3744 806zread_ipv4_add (struct zserv *client, u_short length)
807{
808 int i;
809 struct rib *rib;
810 struct prefix_ipv4 p;
811 u_char message;
812 struct in_addr nexthop;
813 u_char nexthop_num;
814 u_char nexthop_type;
815 struct stream *s;
816 unsigned int ifindex;
817 u_char ifname_len;
818
819 /* Get input stream. */
820 s = client->ibuf;
821
822 /* Allocate new rib. */
823 rib = XMALLOC (MTYPE_RIB, sizeof (struct rib));
824 memset (rib, 0, sizeof (struct rib));
825
826 /* Type, flags, message. */
827 rib->type = stream_getc (s);
828 rib->flags = stream_getc (s);
b9df2d25 829 message = stream_getc (s);
718e3744 830 rib->uptime = time (NULL);
831
832 /* IPv4 prefix. */
833 memset (&p, 0, sizeof (struct prefix_ipv4));
834 p.family = AF_INET;
835 p.prefixlen = stream_getc (s);
836 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
837
838 /* Nexthop parse. */
839 if (CHECK_FLAG (message, ZAPI_MESSAGE_NEXTHOP))
840 {
841 nexthop_num = stream_getc (s);
842
843 for (i = 0; i < nexthop_num; i++)
844 {
845 nexthop_type = stream_getc (s);
846
847 switch (nexthop_type)
848 {
849 case ZEBRA_NEXTHOP_IFINDEX:
850 ifindex = stream_getl (s);
851 nexthop_ifindex_add (rib, ifindex);
852 break;
853 case ZEBRA_NEXTHOP_IFNAME:
854 ifname_len = stream_getc (s);
855 stream_forward (s, ifname_len);
856 break;
857 case ZEBRA_NEXTHOP_IPV4:
858 nexthop.s_addr = stream_get_ipv4 (s);
859 nexthop_ipv4_add (rib, &nexthop);
860 break;
861 case ZEBRA_NEXTHOP_IPV6:
862 stream_forward (s, IPV6_MAX_BYTELEN);
863 break;
595db7f1 864 case ZEBRA_NEXTHOP_BLACKHOLE:
865 nexthop_blackhole_add (rib);
866 break;
718e3744 867 }
868 }
869 }
870
871 /* Distance. */
872 if (CHECK_FLAG (message, ZAPI_MESSAGE_DISTANCE))
873 rib->distance = stream_getc (s);
874
875 /* Metric. */
876 if (CHECK_FLAG (message, ZAPI_MESSAGE_METRIC))
877 rib->metric = stream_getl (s);
878
879 rib_add_ipv4_multipath (&p, rib);
880}
881
882/* Zebra server IPv4 prefix delete function. */
b9df2d25 883static void
718e3744 884zread_ipv4_delete (struct zserv *client, u_short length)
885{
886 int i;
887 struct stream *s;
888 struct zapi_ipv4 api;
889 struct in_addr nexthop;
890 unsigned long ifindex;
891 struct prefix_ipv4 p;
892 u_char nexthop_num;
893 u_char nexthop_type;
894 u_char ifname_len;
895
896 s = client->ibuf;
897 ifindex = 0;
898 nexthop.s_addr = 0;
899
900 /* Type, flags, message. */
901 api.type = stream_getc (s);
902 api.flags = stream_getc (s);
903 api.message = stream_getc (s);
904
905 /* IPv4 prefix. */
906 memset (&p, 0, sizeof (struct prefix_ipv4));
907 p.family = AF_INET;
908 p.prefixlen = stream_getc (s);
909 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
910
911 /* Nexthop, ifindex, distance, metric. */
912 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
913 {
914 nexthop_num = stream_getc (s);
915
916 for (i = 0; i < nexthop_num; i++)
917 {
918 nexthop_type = stream_getc (s);
919
920 switch (nexthop_type)
921 {
922 case ZEBRA_NEXTHOP_IFINDEX:
923 ifindex = stream_getl (s);
924 break;
925 case ZEBRA_NEXTHOP_IFNAME:
926 ifname_len = stream_getc (s);
927 stream_forward (s, ifname_len);
928 break;
929 case ZEBRA_NEXTHOP_IPV4:
930 nexthop.s_addr = stream_get_ipv4 (s);
931 break;
932 case ZEBRA_NEXTHOP_IPV6:
933 stream_forward (s, IPV6_MAX_BYTELEN);
934 break;
935 }
936 }
937 }
938
939 /* Distance. */
940 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
941 api.distance = stream_getc (s);
942 else
943 api.distance = 0;
944
945 /* Metric. */
946 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
947 api.metric = stream_getl (s);
948 else
949 api.metric = 0;
950
951 rib_delete_ipv4 (api.type, api.flags, &p, &nexthop, ifindex,
952 client->rtm_table);
953}
954
955/* Nexthop lookup for IPv4. */
b9df2d25 956static void
718e3744 957zread_ipv4_nexthop_lookup (struct zserv *client, u_short length)
958{
959 struct in_addr addr;
960
961 addr.s_addr = stream_get_ipv4 (client->ibuf);
962 zsend_ipv4_nexthop_lookup (client, addr);
963}
964
965/* Nexthop lookup for IPv4. */
b9df2d25 966static void
718e3744 967zread_ipv4_import_lookup (struct zserv *client, u_short length)
968{
969 struct prefix_ipv4 p;
970
971 p.family = AF_INET;
972 p.prefixlen = stream_getc (client->ibuf);
973 p.prefix.s_addr = stream_get_ipv4 (client->ibuf);
974
975 zsend_ipv4_import_lookup (client, &p);
976}
977
978#ifdef HAVE_IPV6
979/* Zebra server IPv6 prefix add function. */
b9df2d25 980static void
718e3744 981zread_ipv6_add (struct zserv *client, u_short length)
982{
983 int i;
984 struct stream *s;
985 struct zapi_ipv6 api;
986 struct in6_addr nexthop;
987 unsigned long ifindex;
988 struct prefix_ipv6 p;
989
990 s = client->ibuf;
991 ifindex = 0;
992 memset (&nexthop, 0, sizeof (struct in6_addr));
993
994 /* Type, flags, message. */
995 api.type = stream_getc (s);
996 api.flags = stream_getc (s);
997 api.message = stream_getc (s);
998
999 /* IPv4 prefix. */
1000 memset (&p, 0, sizeof (struct prefix_ipv6));
1001 p.family = AF_INET6;
1002 p.prefixlen = stream_getc (s);
1003 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
1004
1005 /* Nexthop, ifindex, distance, metric. */
1006 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
1007 {
1008 u_char nexthop_type;
1009
1010 api.nexthop_num = stream_getc (s);
1011 for (i = 0; i < api.nexthop_num; i++)
1012 {
1013 nexthop_type = stream_getc (s);
1014
1015 switch (nexthop_type)
1016 {
1017 case ZEBRA_NEXTHOP_IPV6:
1018 stream_get (&nexthop, s, 16);
1019 break;
1020 case ZEBRA_NEXTHOP_IFINDEX:
1021 ifindex = stream_getl (s);
1022 break;
1023 }
1024 }
1025 }
1026
1027 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
1028 api.distance = stream_getc (s);
1029 else
1030 api.distance = 0;
1031
1032 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
1033 api.metric = stream_getl (s);
1034 else
1035 api.metric = 0;
1036
1037 if (IN6_IS_ADDR_UNSPECIFIED (&nexthop))
1038 rib_add_ipv6 (api.type, api.flags, &p, NULL, ifindex, 0);
1039 else
1040 rib_add_ipv6 (api.type, api.flags, &p, &nexthop, ifindex, 0);
1041}
1042
1043/* Zebra server IPv6 prefix delete function. */
b9df2d25 1044static void
718e3744 1045zread_ipv6_delete (struct zserv *client, u_short length)
1046{
1047 int i;
1048 struct stream *s;
1049 struct zapi_ipv6 api;
1050 struct in6_addr nexthop;
1051 unsigned long ifindex;
1052 struct prefix_ipv6 p;
1053
1054 s = client->ibuf;
1055 ifindex = 0;
1056 memset (&nexthop, 0, sizeof (struct in6_addr));
1057
1058 /* Type, flags, message. */
1059 api.type = stream_getc (s);
1060 api.flags = stream_getc (s);
1061 api.message = stream_getc (s);
1062
1063 /* IPv4 prefix. */
1064 memset (&p, 0, sizeof (struct prefix_ipv6));
1065 p.family = AF_INET6;
1066 p.prefixlen = stream_getc (s);
1067 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
1068
1069 /* Nexthop, ifindex, distance, metric. */
1070 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
1071 {
1072 u_char nexthop_type;
1073
1074 api.nexthop_num = stream_getc (s);
1075 for (i = 0; i < api.nexthop_num; i++)
1076 {
1077 nexthop_type = stream_getc (s);
1078
1079 switch (nexthop_type)
1080 {
1081 case ZEBRA_NEXTHOP_IPV6:
1082 stream_get (&nexthop, s, 16);
1083 break;
1084 case ZEBRA_NEXTHOP_IFINDEX:
1085 ifindex = stream_getl (s);
1086 break;
1087 }
1088 }
1089 }
1090
1091 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
1092 api.distance = stream_getc (s);
1093 else
1094 api.distance = 0;
1095 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
1096 api.metric = stream_getl (s);
1097 else
1098 api.metric = 0;
1099
1100 if (IN6_IS_ADDR_UNSPECIFIED (&nexthop))
1101 rib_delete_ipv6 (api.type, api.flags, &p, NULL, ifindex, 0);
1102 else
1103 rib_delete_ipv6 (api.type, api.flags, &p, &nexthop, ifindex, 0);
1104}
1105
1106void
1107zebra_read_ipv6 (int command, struct zserv *client, u_short length)
1108{
1109 u_char type;
1110 u_char flags;
1111 struct in6_addr nexthop, *gate;
1112 u_char *lim;
1113 u_char *pnt;
1114 unsigned int ifindex;
1115
1116 pnt = stream_pnt (client->ibuf);
1117 lim = pnt + length;
1118
1119 type = stream_getc (client->ibuf);
1120 flags = stream_getc (client->ibuf);
1121 stream_get (&nexthop, client->ibuf, sizeof (struct in6_addr));
1122
1123 while (stream_pnt (client->ibuf) < lim)
1124 {
1125 int size;
1126 struct prefix_ipv6 p;
1127
1128 ifindex = stream_getl (client->ibuf);
1129
1130 memset (&p, 0, sizeof (struct prefix_ipv6));
1131 p.family = AF_INET6;
1132 p.prefixlen = stream_getc (client->ibuf);
1133 size = PSIZE(p.prefixlen);
1134 stream_get (&p.prefix, client->ibuf, size);
1135
1136 if (IN6_IS_ADDR_UNSPECIFIED (&nexthop))
1137 gate = NULL;
1138 else
1139 gate = &nexthop;
1140
1141 if (command == ZEBRA_IPV6_ROUTE_ADD)
b9df2d25 1142 rib_add_ipv6 (type, flags, &p, gate, ifindex, 0);
718e3744 1143 else
b9df2d25 1144 rib_delete_ipv6 (type, flags, &p, gate, ifindex, 0);
718e3744 1145 }
1146}
1147
b9df2d25 1148static void
718e3744 1149zread_ipv6_nexthop_lookup (struct zserv *client, u_short length)
1150{
1151 struct in6_addr addr;
1152 char buf[BUFSIZ];
1153
1154 stream_get (&addr, client->ibuf, 16);
1155 printf ("DEBUG %s\n", inet_ntop (AF_INET6, &addr, buf, BUFSIZ));
1156
1157 zsend_ipv6_nexthop_lookup (client, &addr);
1158}
1159#endif /* HAVE_IPV6 */
1160
18a6dce6 1161/* Register zebra server router-id information. Send current router-id */
1162void
1163zread_router_id_add (struct zserv *client, u_short length)
1164{
1165 struct prefix p;
1166
1167 /* Router-id information is needed. */
1168 client->ridinfo = 1;
1169
1170 router_id_get (&p);
1171
1172 zsend_router_id_update (client,&p);
1173}
1174
1175/* Unregister zebra server router-id information. */
1176void
1177zread_router_id_delete (struct zserv *client, u_short length)
1178{
1179 client->ridinfo = 0;
1180}
1181
718e3744 1182/* Close zebra client. */
b9df2d25 1183static void
718e3744 1184zebra_client_close (struct zserv *client)
1185{
1186 /* Close file descriptor. */
1187 if (client->sock)
1188 {
1189 close (client->sock);
1190 client->sock = -1;
1191 }
1192
1193 /* Free stream buffers. */
1194 if (client->ibuf)
1195 stream_free (client->ibuf);
1196 if (client->obuf)
1197 stream_free (client->obuf);
1198
1199 /* Release threads. */
1200 if (client->t_read)
1201 thread_cancel (client->t_read);
1202 if (client->t_write)
1203 thread_cancel (client->t_write);
1204
1205 /* Free client structure. */
b21b19c5 1206 listnode_delete (zebrad.client_list, client);
718e3744 1207 XFREE (0, client);
1208}
1209
1210/* Make new client. */
b9df2d25 1211static void
718e3744 1212zebra_client_create (int sock)
1213{
1214 struct zserv *client;
1215
1216 client = XCALLOC (0, sizeof (struct zserv));
1217
1218 /* Make client input/output buffer. */
1219 client->sock = sock;
1220 client->ibuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
1221 client->obuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
1222
1223 /* Set table number. */
b21b19c5 1224 client->rtm_table = zebrad.rtm_table_default;
718e3744 1225
1226 /* Add this client to linked list. */
b21b19c5 1227 listnode_add (zebrad.client_list, client);
718e3744 1228
1229 /* Make new read thread. */
1230 zebra_event (ZEBRA_READ, sock, client);
1231}
1232
1233/* Handler of zebra service request. */
b9df2d25 1234static int
718e3744 1235zebra_client_read (struct thread *thread)
1236{
1237 int sock;
1238 struct zserv *client;
1239 int nbyte;
1240 u_short length;
1241 u_char command;
1242
1243 /* Get thread data. Reset reading thread because I'm running. */
1244 sock = THREAD_FD (thread);
1245 client = THREAD_ARG (thread);
1246 client->t_read = NULL;
1247
1248 /* Read length and command. */
1249 nbyte = stream_read (client->ibuf, sock, 3);
1250 if (nbyte <= 0)
1251 {
1252 if (IS_ZEBRA_DEBUG_EVENT)
1253 zlog_info ("connection closed socket [%d]", sock);
1254 zebra_client_close (client);
1255 return -1;
1256 }
1257 length = stream_getw (client->ibuf);
1258 command = stream_getc (client->ibuf);
1259
1260 if (length < 3)
1261 {
1262 if (IS_ZEBRA_DEBUG_EVENT)
1263 zlog_info ("length %d is less than 3 ", length);
1264 zebra_client_close (client);
1265 return -1;
1266 }
1267
1268 length -= 3;
1269
1270 /* Read rest of data. */
1271 if (length)
1272 {
1273 nbyte = stream_read (client->ibuf, sock, length);
1274 if (nbyte <= 0)
1275 {
1276 if (IS_ZEBRA_DEBUG_EVENT)
1277 zlog_info ("connection closed [%d] when reading zebra data", sock);
1278 zebra_client_close (client);
1279 return -1;
1280 }
1281 }
1282
1283 /* Debug packet information. */
1284 if (IS_ZEBRA_DEBUG_EVENT)
1285 zlog_info ("zebra message comes from socket [%d]", sock);
1286
1287 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
1288 zlog_info ("zebra message received [%s] %d",
1289 zebra_command_str[command], length);
1290
1291 switch (command)
1292 {
18a6dce6 1293 case ZEBRA_ROUTER_ID_ADD:
1294 zread_router_id_add (client, length);
1295 break;
1296 case ZEBRA_ROUTER_ID_DELETE:
1297 zread_router_id_delete (client, length);
1298 break;
718e3744 1299 case ZEBRA_INTERFACE_ADD:
1300 zread_interface_add (client, length);
1301 break;
1302 case ZEBRA_INTERFACE_DELETE:
1303 zread_interface_delete (client, length);
1304 break;
1305 case ZEBRA_IPV4_ROUTE_ADD:
1306 zread_ipv4_add (client, length);
1307 break;
1308 case ZEBRA_IPV4_ROUTE_DELETE:
1309 zread_ipv4_delete (client, length);
1310 break;
1311#ifdef HAVE_IPV6
1312 case ZEBRA_IPV6_ROUTE_ADD:
1313 zread_ipv6_add (client, length);
1314 break;
1315 case ZEBRA_IPV6_ROUTE_DELETE:
1316 zread_ipv6_delete (client, length);
1317 break;
1318#endif /* HAVE_IPV6 */
1319 case ZEBRA_REDISTRIBUTE_ADD:
1320 zebra_redistribute_add (command, client, length);
1321 break;
1322 case ZEBRA_REDISTRIBUTE_DELETE:
1323 zebra_redistribute_delete (command, client, length);
1324 break;
1325 case ZEBRA_REDISTRIBUTE_DEFAULT_ADD:
1326 zebra_redistribute_default_add (command, client, length);
1327 break;
1328 case ZEBRA_REDISTRIBUTE_DEFAULT_DELETE:
1329 zebra_redistribute_default_delete (command, client, length);
1330 break;
1331 case ZEBRA_IPV4_NEXTHOP_LOOKUP:
1332 zread_ipv4_nexthop_lookup (client, length);
1333 break;
1334#ifdef HAVE_IPV6
1335 case ZEBRA_IPV6_NEXTHOP_LOOKUP:
1336 zread_ipv6_nexthop_lookup (client, length);
1337 break;
1338#endif /* HAVE_IPV6 */
1339 case ZEBRA_IPV4_IMPORT_LOOKUP:
1340 zread_ipv4_import_lookup (client, length);
1341 break;
1342 default:
1343 zlog_info ("Zebra received unknown command %d", command);
1344 break;
1345 }
1346
1347 stream_reset (client->ibuf);
1348 zebra_event (ZEBRA_READ, sock, client);
1349
1350 return 0;
1351}
1352
718e3744 1353
1354/* Accept code of zebra server socket. */
b9df2d25 1355static int
718e3744 1356zebra_accept (struct thread *thread)
1357{
ccf3557b 1358 int val;
718e3744 1359 int accept_sock;
1360 int client_sock;
1361 struct sockaddr_in client;
1362 socklen_t len;
1363
1364 accept_sock = THREAD_FD (thread);
1365
1366 len = sizeof (struct sockaddr_in);
1367 client_sock = accept (accept_sock, (struct sockaddr *) &client, &len);
1368
1369 if (client_sock < 0)
1370 {
1371 zlog_warn ("Can't accept zebra socket: %s", strerror (errno));
1372 return -1;
1373 }
1374
ccf3557b 1375 /* Make client socket non-blocking. */
1376
1377 val = fcntl (client_sock, F_GETFL, 0);
1378 fcntl (client_sock, F_SETFL, (val | O_NONBLOCK));
1379
718e3744 1380 /* 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
b9df2d25 1389#ifdef HAVE_TCP_ZEBRA
718e3744 1390/* Make zebra's server socket. */
b9df2d25 1391static void
718e3744 1392zebra_serv ()
1393{
1394 int ret;
1395 int accept_sock;
1396 struct sockaddr_in addr;
1397
1398 accept_sock = socket (AF_INET, SOCK_STREAM, 0);
1399
1400 if (accept_sock < 0)
1401 {
1402 zlog_warn ("Can't bind to socket: %s", strerror (errno));
1403 zlog_warn ("zebra can't provice full functionality due to above error");
1404 return;
1405 }
1406
1407 memset (&addr, 0, sizeof (struct sockaddr_in));
1408 addr.sin_family = AF_INET;
1409 addr.sin_port = htons (ZEBRA_PORT);
1410#ifdef HAVE_SIN_LEN
1411 addr.sin_len = sizeof (struct sockaddr_in);
1412#endif /* HAVE_SIN_LEN */
1413 addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
1414
1415 sockopt_reuseaddr (accept_sock);
1416 sockopt_reuseport (accept_sock);
1417
edd7c245 1418 if ( zserv_privs.change(ZPRIVS_RAISE) )
1419 zlog (NULL, LOG_ERR, "Can't raise privileges");
1420
718e3744 1421 ret = bind (accept_sock, (struct sockaddr *)&addr,
1422 sizeof (struct sockaddr_in));
1423 if (ret < 0)
1424 {
1425 zlog_warn ("Can't bind to socket: %s", strerror (errno));
1426 zlog_warn ("zebra can't provice full functionality due to above error");
1427 close (accept_sock); /* Avoid sd leak. */
1428 return;
1429 }
edd7c245 1430
1431 if ( zserv_privs.change(ZPRIVS_LOWER) )
1432 zlog (NULL, LOG_ERR, "Can't lower privileges");
718e3744 1433
1434 ret = listen (accept_sock, 1);
1435 if (ret < 0)
1436 {
1437 zlog_warn ("Can't listen to socket: %s", strerror (errno));
1438 zlog_warn ("zebra can't provice full functionality due to above error");
1439 close (accept_sock); /* Avoid sd leak. */
1440 return;
1441 }
1442
1443 zebra_event (ZEBRA_SERV, accept_sock, NULL);
1444}
b9df2d25 1445#endif /* HAVE_TCP_ZEBRA */
718e3744 1446
1447/* For sockaddr_un. */
1448#include <sys/un.h>
1449
1450/* zebra server UNIX domain socket. */
b9df2d25 1451static void
718e3744 1452zebra_serv_un (char *path)
1453{
1454 int ret;
1455 int sock, len;
1456 struct sockaddr_un serv;
1457 mode_t old_mask;
1458
1459 /* First of all, unlink existing socket */
1460 unlink (path);
1461
1462 /* Set umask */
1463 old_mask = umask (0077);
1464
1465 /* Make UNIX domain socket. */
1466 sock = socket (AF_UNIX, SOCK_STREAM, 0);
1467 if (sock < 0)
1468 {
1469 perror ("sock");
1470 return;
1471 }
1472
1473 /* Make server socket. */
1474 memset (&serv, 0, sizeof (struct sockaddr_un));
1475 serv.sun_family = AF_UNIX;
1476 strncpy (serv.sun_path, path, strlen (path));
1477#ifdef HAVE_SUN_LEN
1478 len = serv.sun_len = SUN_LEN(&serv);
1479#else
1480 len = sizeof (serv.sun_family) + strlen (serv.sun_path);
1481#endif /* HAVE_SUN_LEN */
1482
1483 ret = bind (sock, (struct sockaddr *) &serv, len);
1484 if (ret < 0)
1485 {
1486 perror ("bind");
1487 close (sock);
1488 return;
1489 }
1490
1491 ret = listen (sock, 5);
1492 if (ret < 0)
1493 {
1494 perror ("listen");
1495 close (sock);
1496 return;
1497 }
1498
1499 umask (old_mask);
1500
1501 zebra_event (ZEBRA_SERV, sock, NULL);
1502}
1503\f
718e3744 1504
b9df2d25 1505static void
718e3744 1506zebra_event (enum event event, int sock, struct zserv *client)
1507{
1508 switch (event)
1509 {
1510 case ZEBRA_SERV:
b21b19c5 1511 thread_add_read (zebrad.master, zebra_accept, client, sock);
718e3744 1512 break;
1513 case ZEBRA_READ:
1514 client->t_read =
b21b19c5 1515 thread_add_read (zebrad.master, zebra_client_read, client, sock);
718e3744 1516 break;
1517 case ZEBRA_WRITE:
1518 /**/
1519 break;
1520 }
1521}
1522\f
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{
b21b19c5 1530 vty_out (vty, "table %d%s", zebrad.rtm_table_default,
718e3744 1531 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{
b21b19c5 1541 zebrad.rtm_table_default = strtol (argv[0], (char**)0, 10);
718e3744 1542 return CMD_SUCCESS;
1543}
1544
647e4f1f 1545DEFUN (ip_forwarding,
1546 ip_forwarding_cmd,
1547 "ip forwarding",
1548 IP_STR
1549 "Turn on IP forwarding")
1550{
1551 int ret;
1552
1553 ret = ipforward ();
1554
1555 if (ret != 0)
1556 {
1557 vty_out (vty, "IP forwarding is already on%s", VTY_NEWLINE);
1558 return CMD_ERR_NOTHING_TODO;
1559 }
1560
1561 ret = ipforward_on ();
1562 if (ret == 0)
1563 {
1564 vty_out (vty, "Can't turn on IP forwarding%s", VTY_NEWLINE);
1565 return CMD_WARNING;
1566 }
1567
1568 return CMD_SUCCESS;
1569}
1570
718e3744 1571DEFUN (no_ip_forwarding,
1572 no_ip_forwarding_cmd,
1573 "no ip forwarding",
1574 NO_STR
1575 IP_STR
1576 "Turn off IP forwarding")
1577{
1578 int ret;
1579
1580 ret = ipforward ();
1581
1582 if (ret == 0)
1583 {
1584 vty_out (vty, "IP forwarding is already off%s", VTY_NEWLINE);
1585 return CMD_ERR_NOTHING_TODO;
1586 }
1587
1588 ret = ipforward_off ();
1589 if (ret != 0)
1590 {
1591 vty_out (vty, "Can't turn off IP forwarding%s", VTY_NEWLINE);
1592 return CMD_WARNING;
1593 }
1594
1595 return CMD_SUCCESS;
1596}
1597
1598/* This command is for debugging purpose. */
1599DEFUN (show_zebra_client,
1600 show_zebra_client_cmd,
1601 "show zebra client",
1602 SHOW_STR
1603 "Zebra information"
1604 "Client information")
1605{
52dc7ee6 1606 struct listnode *node;
718e3744 1607 struct zserv *client;
1608
b21b19c5 1609 for (node = listhead (zebrad.client_list); node; nextnode (node))
718e3744 1610 {
1611 client = getdata (node);
1612 vty_out (vty, "Client fd %d%s", client->sock, VTY_NEWLINE);
1613 }
1614 return CMD_SUCCESS;
1615}
1616
1617/* Table configuration write function. */
b9df2d25 1618static int
718e3744 1619config_write_table (struct vty *vty)
1620{
b21b19c5 1621 if (zebrad.rtm_table_default)
1622 vty_out (vty, "table %d%s", zebrad.rtm_table_default,
718e3744 1623 VTY_NEWLINE);
1624 return 0;
1625}
1626
1627/* table node for routing tables. */
1628struct cmd_node table_node =
1629{
1630 TABLE_NODE,
1631 "", /* This node has no interface. */
1632 1
1633};
1634\f
1635/* Only display ip forwarding is enabled or not. */
1636DEFUN (show_ip_forwarding,
1637 show_ip_forwarding_cmd,
1638 "show ip forwarding",
1639 SHOW_STR
1640 IP_STR
1641 "IP forwarding status\n")
1642{
1643 int ret;
1644
1645 ret = ipforward ();
1646
1647 if (ret == 0)
1648 vty_out (vty, "IP forwarding is off%s", VTY_NEWLINE);
1649 else
1650 vty_out (vty, "IP forwarding is on%s", VTY_NEWLINE);
1651 return CMD_SUCCESS;
1652}
1653
1654#ifdef HAVE_IPV6
1655/* Only display ipv6 forwarding is enabled or not. */
1656DEFUN (show_ipv6_forwarding,
1657 show_ipv6_forwarding_cmd,
1658 "show ipv6 forwarding",
1659 SHOW_STR
1660 "IPv6 information\n"
1661 "Forwarding status\n")
1662{
1663 int ret;
1664
1665 ret = ipforward_ipv6 ();
1666
1667 switch (ret)
1668 {
1669 case -1:
1670 vty_out (vty, "ipv6 forwarding is unknown%s", VTY_NEWLINE);
1671 break;
1672 case 0:
1673 vty_out (vty, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE);
1674 break;
1675 case 1:
1676 vty_out (vty, "ipv6 forwarding is %s%s", "on", VTY_NEWLINE);
1677 break;
1678 default:
1679 vty_out (vty, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE);
1680 break;
1681 }
1682 return CMD_SUCCESS;
1683}
1684
55906724 1685DEFUN (ipv6_forwarding,
1686 ipv6_forwarding_cmd,
1687 "ipv6 forwarding",
1688 IPV6_STR
1689 "Turn on IPv6 forwarding")
1690{
1691 int ret;
1692
41d3fc96 1693 ret = ipforward_ipv6 ();
55906724 1694 if (ret != 0)
41d3fc96 1695 {
1696 vty_out (vty, "IPv6 forwarding is already on%s", VTY_NEWLINE);
1697 return CMD_ERR_NOTHING_TODO;
1698 }
1699
1700 ret = ipforward_ipv6_on ();
1701 if (ret == 0)
55906724 1702 {
1703 vty_out (vty, "Can't turn on IPv6 forwarding%s", VTY_NEWLINE);
1704 return CMD_WARNING;
1705 }
1706
1707 return CMD_SUCCESS;
1708}
1709
718e3744 1710DEFUN (no_ipv6_forwarding,
1711 no_ipv6_forwarding_cmd,
1712 "no ipv6 forwarding",
1713 NO_STR
55906724 1714 IPV6_STR
1715 "Turn off IPv6 forwarding")
718e3744 1716{
1717 int ret;
1718
41d3fc96 1719 ret = ipforward_ipv6 ();
1720 if (ret == 0)
1721 {
1722 vty_out (vty, "IP forwarding is already off%s", VTY_NEWLINE);
1723 return CMD_ERR_NOTHING_TODO;
1724 }
1725
718e3744 1726 ret = ipforward_ipv6_off ();
1727 if (ret != 0)
1728 {
1729 vty_out (vty, "Can't turn off IPv6 forwarding%s", VTY_NEWLINE);
1730 return CMD_WARNING;
1731 }
1732
1733 return CMD_SUCCESS;
1734}
1735
1736#endif /* HAVE_IPV6 */
1737
1738/* IPForwarding configuration write function. */
1739int
1740config_write_forwarding (struct vty *vty)
1741{
18a6dce6 1742 /* FIXME: Find better place for that. */
1743 router_id_write (vty);
1744
3e0b3a56 1745 if (ipforward ())
1746 vty_out (vty, "ip forwarding%s", VTY_NEWLINE);
718e3744 1747#ifdef HAVE_IPV6
3e0b3a56 1748 if (ipforward_ipv6 ())
1749 vty_out (vty, "ipv6 forwarding%s", VTY_NEWLINE);
718e3744 1750#endif /* HAVE_IPV6 */
1751 vty_out (vty, "!%s", VTY_NEWLINE);
1752 return 0;
1753}
1754
1755/* table node for routing tables. */
1756struct cmd_node forwarding_node =
1757{
1758 FORWARDING_NODE,
1759 "", /* This node has no interface. */
1760 1
1761};
1762
1763\f
1764/* Initialisation of zebra and installation of commands. */
1765void
1766zebra_init ()
1767{
1768 /* Client list init. */
b21b19c5 1769 zebrad.client_list = list_new ();
718e3744 1770
718e3744 1771 /* Make zebra server socket. */
1772#ifdef HAVE_TCP_ZEBRA
1773 zebra_serv ();
1774#else
1775 zebra_serv_un (ZEBRA_SERV_PATH);
1776#endif /* HAVE_TCP_ZEBRA */
1777
1778 /* Install configuration write function. */
1779 install_node (&table_node, config_write_table);
1780 install_node (&forwarding_node, config_write_forwarding);
1781
1782 install_element (VIEW_NODE, &show_ip_forwarding_cmd);
1783 install_element (ENABLE_NODE, &show_ip_forwarding_cmd);
647e4f1f 1784 install_element (CONFIG_NODE, &ip_forwarding_cmd);
718e3744 1785 install_element (CONFIG_NODE, &no_ip_forwarding_cmd);
1786 install_element (ENABLE_NODE, &show_zebra_client_cmd);
1787
1788#ifdef HAVE_NETLINK
1789 install_element (VIEW_NODE, &show_table_cmd);
1790 install_element (ENABLE_NODE, &show_table_cmd);
1791 install_element (CONFIG_NODE, &config_table_cmd);
1792#endif /* HAVE_NETLINK */
1793
1794#ifdef HAVE_IPV6
1795 install_element (VIEW_NODE, &show_ipv6_forwarding_cmd);
1796 install_element (ENABLE_NODE, &show_ipv6_forwarding_cmd);
55906724 1797 install_element (CONFIG_NODE, &ipv6_forwarding_cmd);
718e3744 1798 install_element (CONFIG_NODE, &no_ipv6_forwarding_cmd);
1799#endif /* HAVE_IPV6 */
ccf3557b 1800
1801 FIFO_INIT(&message_queue);
1802 t_write = NULL;
718e3744 1803}