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