]> git.proxmox.com Git - mirror_frr.git/blob - zebra/zserv.c
bgpd, zebra: rfc-5549-generic.patch
[mirror_frr.git] / zebra / zserv.c
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"
36 #include "privs.h"
37 #include "network.h"
38 #include "buffer.h"
39 #include "nexthop.h"
40
41 #include "zebra/zserv.h"
42 #include "zebra/router-id.h"
43 #include "zebra/redistribute.h"
44 #include "zebra/debug.h"
45 #include "zebra/ipforward.h"
46 #include "zebra/zebra_rnh.h"
47
48 /* Event list of zebra. */
49 enum event { ZEBRA_SERV, ZEBRA_READ, ZEBRA_WRITE };
50
51 extern struct zebra_t zebrad;
52
53 static void zebra_event (enum event event, int sock, struct zserv *client);
54
55 extern struct zebra_privs_t zserv_privs;
56
57 static void zebra_client_close (struct zserv *client);
58
59 static int
60 zserv_delayed_close(struct thread *thread)
61 {
62 struct zserv *client = THREAD_ARG(thread);
63
64 client->t_suicide = NULL;
65 zebra_client_close(client);
66 return 0;
67 }
68
69 static int
70 zserv_flush_data(struct thread *thread)
71 {
72 struct zserv *client = THREAD_ARG(thread);
73
74 client->t_write = NULL;
75 if (client->t_suicide)
76 {
77 zebra_client_close(client);
78 return -1;
79 }
80 switch (buffer_flush_available(client->wb, client->sock))
81 {
82 case BUFFER_ERROR:
83 zlog_warn("%s: buffer_flush_available failed on zserv client fd %d, "
84 "closing", __func__, client->sock);
85 zebra_client_close(client);
86 break;
87 case BUFFER_PENDING:
88 client->t_write = thread_add_write(zebrad.master, zserv_flush_data,
89 client, client->sock);
90 break;
91 case BUFFER_EMPTY:
92 break;
93 }
94
95 client->last_write_time = quagga_time(NULL);
96 return 0;
97 }
98
99 int
100 zebra_server_send_message(struct zserv *client)
101 {
102 if (client->t_suicide)
103 return -1;
104
105 stream_set_getp(client->obuf, 0);
106 client->last_write_cmd = stream_getw_from(client->obuf, 4);
107 switch (buffer_write(client->wb, client->sock, STREAM_DATA(client->obuf),
108 stream_get_endp(client->obuf)))
109 {
110 case BUFFER_ERROR:
111 zlog_warn("%s: buffer_write failed to zserv client fd %d, closing",
112 __func__, client->sock);
113 /* Schedule a delayed close since many of the functions that call this
114 one do not check the return code. They do not allow for the
115 possibility that an I/O error may have caused the client to be
116 deleted. */
117 client->t_suicide = thread_add_event(zebrad.master, zserv_delayed_close,
118 client, 0);
119 return -1;
120 case BUFFER_EMPTY:
121 THREAD_OFF(client->t_write);
122 break;
123 case BUFFER_PENDING:
124 THREAD_WRITE_ON(zebrad.master, client->t_write,
125 zserv_flush_data, client, client->sock);
126 break;
127 }
128
129 client->last_write_time = quagga_time(NULL);
130 return 0;
131 }
132
133 void
134 zserv_create_header (struct stream *s, uint16_t cmd)
135 {
136 /* length placeholder, caller can update */
137 stream_putw (s, ZEBRA_HEADER_SIZE);
138 stream_putc (s, ZEBRA_HEADER_MARKER);
139 stream_putc (s, ZSERV_VERSION);
140 stream_putw (s, cmd);
141 }
142
143 static void
144 zserv_encode_interface (struct stream *s, struct interface *ifp)
145 {
146 /* Interface information. */
147 stream_put (s, ifp->name, INTERFACE_NAMSIZ);
148 stream_putl (s, ifp->ifindex);
149 stream_putc (s, ifp->status);
150 stream_putq (s, ifp->flags);
151 stream_putc (s, ifp->ptm_enable);
152 stream_putc (s, ifp->ptm_status);
153 stream_putl (s, ifp->metric);
154 stream_putl (s, ifp->mtu);
155 stream_putl (s, ifp->mtu6);
156 stream_putl (s, ifp->bandwidth);
157 #ifdef HAVE_STRUCT_SOCKADDR_DL
158 stream_put (s, &ifp->sdl, sizeof (ifp->sdl_storage));
159 #else
160 stream_putl (s, ifp->hw_addr_len);
161 if (ifp->hw_addr_len)
162 stream_put (s, ifp->hw_addr, ifp->hw_addr_len);
163 #endif /* HAVE_STRUCT_SOCKADDR_DL */
164
165 /* Write packet size. */
166 stream_putw_at (s, 0, stream_get_endp (s));
167 }
168
169 /* Interface is added. Send ZEBRA_INTERFACE_ADD to client. */
170 /*
171 * This function is called in the following situations:
172 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
173 * from the client.
174 * - at startup, when zebra figures out the available interfaces
175 * - when an interface is added (where support for
176 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
177 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
178 * received)
179 */
180 int
181 zsend_interface_add (struct zserv *client, struct interface *ifp)
182 {
183 struct stream *s;
184
185 /* Check this client need interface information. */
186 if (! client->ifinfo)
187 return 0;
188
189 s = client->obuf;
190 stream_reset (s);
191
192 zserv_create_header (s, ZEBRA_INTERFACE_ADD);
193 zserv_encode_interface (s, ifp);
194
195 client->ifadd_cnt++;
196 return zebra_server_send_message(client);
197 }
198
199 /* Interface deletion from zebra daemon. */
200 int
201 zsend_interface_delete (struct zserv *client, struct interface *ifp)
202 {
203 struct stream *s;
204
205 /* Check this client need interface information. */
206 if (! client->ifinfo)
207 return 0;
208
209 s = client->obuf;
210 stream_reset (s);
211
212 zserv_create_header (s, ZEBRA_INTERFACE_DELETE);
213 zserv_encode_interface (s, ifp);
214
215 client->ifdel_cnt++;
216 return zebra_server_send_message (client);
217 }
218
219 /* Interface address is added/deleted. Send ZEBRA_INTERFACE_ADDRESS_ADD or
220 * ZEBRA_INTERFACE_ADDRESS_DELETE to the client.
221 *
222 * A ZEBRA_INTERFACE_ADDRESS_ADD is sent in the following situations:
223 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
224 * from the client, after the ZEBRA_INTERFACE_ADD has been
225 * sent from zebra to the client
226 * - redistribute new address info to all clients in the following situations
227 * - at startup, when zebra figures out the available interfaces
228 * - when an interface is added (where support for
229 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
230 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
231 * received)
232 * - for the vty commands "ip address A.B.C.D/M [<secondary>|<label LINE>]"
233 * and "no bandwidth <1-10000000>", "ipv6 address X:X::X:X/M"
234 * - when an RTM_NEWADDR message is received from the kernel,
235 *
236 * The call tree that triggers ZEBRA_INTERFACE_ADDRESS_DELETE:
237 *
238 * zsend_interface_address(DELETE)
239 * ^
240 * |
241 * zebra_interface_address_delete_update
242 * ^ ^ ^
243 * | | if_delete_update
244 * | |
245 * ip_address_uninstall connected_delete_ipv4
246 * [ipv6_addresss_uninstall] [connected_delete_ipv6]
247 * ^ ^
248 * | |
249 * | RTM_NEWADDR on routing/netlink socket
250 * |
251 * vty commands:
252 * "no ip address A.B.C.D/M [label LINE]"
253 * "no ip address A.B.C.D/M secondary"
254 * ["no ipv6 address X:X::X:X/M"]
255 *
256 */
257 int
258 zsend_interface_address (int cmd, struct zserv *client,
259 struct interface *ifp, struct connected *ifc)
260 {
261 int blen;
262 struct stream *s;
263 struct prefix *p;
264
265 /* Check this client need interface information. */
266 if (! client->ifinfo)
267 return 0;
268
269 s = client->obuf;
270 stream_reset (s);
271
272 zserv_create_header (s, cmd);
273 stream_putl (s, ifp->ifindex);
274
275 /* Interface address flag. */
276 stream_putc (s, ifc->flags);
277
278 /* Prefix information. */
279 p = ifc->address;
280 stream_putc (s, p->family);
281 blen = prefix_blen (p);
282 stream_put (s, &p->u.prefix, blen);
283
284 /*
285 * XXX gnu version does not send prefixlen for ZEBRA_INTERFACE_ADDRESS_DELETE
286 * but zebra_interface_address_delete_read() in the gnu version
287 * expects to find it
288 */
289 stream_putc (s, p->prefixlen);
290
291 /* Destination. */
292 p = ifc->destination;
293 if (p)
294 stream_put (s, &p->u.prefix, blen);
295 else
296 stream_put (s, NULL, blen);
297
298 /* Write packet size. */
299 stream_putw_at (s, 0, stream_get_endp (s));
300
301 client->connected_rt_add_cnt++;
302 return zebra_server_send_message(client);
303 }
304
305 static int
306 zsend_interface_nbr_address (int cmd, struct zserv *client,
307 struct interface *ifp, struct nbr_connected *ifc)
308 {
309 int blen;
310 struct stream *s;
311 struct prefix *p;
312
313 /* Check this client need interface information. */
314 if (! client->ifinfo)
315 return 0;
316
317 s = client->obuf;
318 stream_reset (s);
319
320 zserv_create_header (s, cmd);
321 stream_putl (s, ifp->ifindex);
322
323 /* Prefix information. */
324 p = ifc->address;
325 stream_putc (s, p->family);
326 blen = prefix_blen (p);
327 stream_put (s, &p->u.prefix, blen);
328
329 /*
330 * XXX gnu version does not send prefixlen for ZEBRA_INTERFACE_ADDRESS_DELETE
331 * but zebra_interface_address_delete_read() in the gnu version
332 * expects to find it
333 */
334 stream_putc (s, p->prefixlen);
335
336 /* Write packet size. */
337 stream_putw_at (s, 0, stream_get_endp (s));
338
339 return zebra_server_send_message(client);
340 }
341
342 /* Interface address addition. */
343 static void
344 zebra_interface_nbr_address_add_update (struct interface *ifp,
345 struct nbr_connected *ifc)
346 {
347 struct listnode *node, *nnode;
348 struct zserv *client;
349 struct prefix *p;
350
351 if (IS_ZEBRA_DEBUG_EVENT)
352 {
353 char buf[INET6_ADDRSTRLEN];
354
355 p = ifc->address;
356 zlog_debug ("MESSAGE: ZEBRA_INTERFACE_NBR_ADDRESS_ADD %s/%d on %s",
357 inet_ntop (p->family, &p->u.prefix, buf, INET6_ADDRSTRLEN),
358 p->prefixlen, ifc->ifp->name);
359 }
360
361 for (ALL_LIST_ELEMENTS (zebrad.client_list, node, nnode, client))
362 if (client->ifinfo)
363 zsend_interface_nbr_address (ZEBRA_INTERFACE_NBR_ADDRESS_ADD, client, ifp, ifc);
364 }
365
366 /* Interface address deletion. */
367 static void
368 zebra_interface_nbr_address_delete_update (struct interface *ifp,
369 struct nbr_connected *ifc)
370 {
371 struct listnode *node, *nnode;
372 struct zserv *client;
373 struct prefix *p;
374
375 if (IS_ZEBRA_DEBUG_EVENT)
376 {
377 char buf[INET6_ADDRSTRLEN];
378
379 p = ifc->address;
380 zlog_debug ("MESSAGE: ZEBRA_INTERFACE_NBR_ADDRESS_DELETE %s/%d on %s",
381 inet_ntop (p->family, &p->u.prefix, buf, INET6_ADDRSTRLEN),
382 p->prefixlen, ifc->ifp->name);
383 }
384
385 for (ALL_LIST_ELEMENTS (zebrad.client_list, node, nnode, client))
386 if (client->ifinfo)
387 zsend_interface_nbr_address (ZEBRA_INTERFACE_NBR_ADDRESS_DELETE, client, ifp, ifc);
388 }
389
390 /* Add new nbr connected IPv6 address if none exists already, or replace the
391 existing one if an ifc entry is found on the interface. */
392 void
393 nbr_connected_replacement_add_ipv6 (struct interface *ifp, struct in6_addr *address,
394 u_char prefixlen)
395 {
396 struct nbr_connected *ifc;
397 struct prefix p;
398
399 p.family = AF_INET6;
400 IPV6_ADDR_COPY (&p.u.prefix, address);
401 p.prefixlen = prefixlen;
402
403 if (nbr_connected_check(ifp, &p))
404 return;
405
406 if (!(ifc = listnode_head(ifp->nbr_connected)))
407 {
408 /* new addition */
409 ifc = nbr_connected_new ();
410 ifc->address = prefix_new();
411 ifc->ifp = ifp;
412 listnode_add (ifp->nbr_connected, ifc);
413 }
414
415 prefix_copy(ifc->address, &p);
416
417 zebra_interface_nbr_address_add_update (ifp, ifc);
418 }
419
420 void
421 nbr_connected_delete_ipv6 (struct interface *ifp, struct in6_addr *address,
422 u_char prefixlen)
423 {
424 struct nbr_connected *ifc;
425 struct prefix p;
426
427 p.family = AF_INET6;
428 IPV6_ADDR_COPY (&p.u.prefix, address);
429 p.prefixlen = prefixlen;
430
431 ifc = nbr_connected_check(ifp, &p);
432 if (!ifc)
433 return;
434
435 listnode_delete (ifp->nbr_connected, ifc);
436
437 zebra_interface_nbr_address_delete_update (ifp, ifc);
438
439 nbr_connected_free (ifc);
440 }
441
442 /*
443 * The cmd passed to zsend_interface_update may be ZEBRA_INTERFACE_UP or
444 * ZEBRA_INTERFACE_DOWN.
445 *
446 * The ZEBRA_INTERFACE_UP message is sent from the zebra server to
447 * the clients in one of 2 situations:
448 * - an if_up is detected e.g., as a result of an RTM_IFINFO message
449 * - a vty command modifying the bandwidth of an interface is received.
450 * The ZEBRA_INTERFACE_DOWN message is sent when an if_down is detected.
451 */
452 int
453 zsend_interface_update (int cmd, struct zserv *client, struct interface *ifp)
454 {
455 struct stream *s;
456
457 /* Check this client need interface information. */
458 if (! client->ifinfo)
459 return 0;
460
461 s = client->obuf;
462 stream_reset (s);
463
464 zserv_create_header (s, cmd);
465 zserv_encode_interface (s, ifp);
466
467 if (cmd == ZEBRA_INTERFACE_UP)
468 client->ifup_cnt++;
469 else
470 client->ifdown_cnt++;
471
472 return zebra_server_send_message(client);
473 }
474
475 int
476 zsend_interface_bfd_update (int cmd, struct zserv *client,
477 struct interface *ifp, struct prefix *p)
478 {
479 int blen;
480 struct stream *s;
481
482 /* Check this client need interface information. */
483 if (! client->ifinfo)
484 return 0;
485
486 s = client->obuf;
487 stream_reset (s);
488
489 zserv_create_header (s, cmd);
490 stream_putl (s, ifp->ifindex);
491
492 /* BFD destination prefix information. */
493 stream_putc (s, p->family);
494 blen = prefix_blen (p);
495 stream_put (s, &p->u.prefix, blen);
496 stream_putc (s, p->prefixlen);
497
498 /* Write packet size. */
499 stream_putw_at (s, 0, stream_get_endp (s));
500
501 client->if_bfd_cnt++;
502 return zebra_server_send_message(client);
503 }
504
505 /*
506 * The zebra server sends the clients a ZEBRA_IPV4_ROUTE_ADD or a
507 * ZEBRA_IPV6_ROUTE_ADD via zsend_route_multipath in the following
508 * situations:
509 * - when the client starts up, and requests default information
510 * by sending a ZEBRA_REDISTRIBUTE_DEFAULT_ADD to the zebra server, in the
511 * - case of rip, ripngd, ospfd and ospf6d, when the client sends a
512 * ZEBRA_REDISTRIBUTE_ADD as a result of the "redistribute" vty cmd,
513 * - when the zebra server redistributes routes after it updates its rib
514 *
515 * The zebra server sends clients a ZEBRA_IPV4_ROUTE_DELETE or a
516 * ZEBRA_IPV6_ROUTE_DELETE via zsend_route_multipath when:
517 * - a "ip route" or "ipv6 route" vty command is issued, a prefix is
518 * - deleted from zebra's rib, and this info
519 * has to be redistributed to the clients
520 *
521 * XXX The ZEBRA_IPV*_ROUTE_ADD message is also sent by the client to the
522 * zebra server when the client wants to tell the zebra server to add a
523 * route to the kernel (zapi_ipv4_add etc. ). Since it's essentially the
524 * same message being sent back and forth, this function and
525 * zapi_ipv{4,6}_{add, delete} should be re-written to avoid code
526 * duplication.
527 */
528 int
529 zsend_route_multipath (int cmd, struct zserv *client, struct prefix *p,
530 struct rib *rib)
531 {
532 int psize;
533 struct stream *s;
534 struct nexthop *nexthop;
535 unsigned long nhnummark = 0, messmark = 0;
536 int nhnum = 0;
537 u_char zapi_flags = 0;
538
539 s = client->obuf;
540 stream_reset (s);
541
542 zserv_create_header (s, cmd);
543
544 /* Put type and nexthop. */
545 stream_putc (s, rib->type);
546 stream_putw (s, rib->instance);
547 stream_putc (s, rib->flags);
548
549 /* marker for message flags field */
550 messmark = stream_get_endp (s);
551 stream_putc (s, 0);
552
553 /* Prefix. */
554 psize = PSIZE (p->prefixlen);
555 stream_putc (s, p->prefixlen);
556 stream_write (s, (u_char *) & p->u.prefix, psize);
557
558 /*
559 * XXX The message format sent by zebra below does not match the format
560 * of the corresponding message expected by the zebra server
561 * itself (e.g., see zread_ipv4_add). The nexthop_num is not set correctly,
562 * (is there a bug on the client side if more than one segment is sent?)
563 * nexthop ZEBRA_NEXTHOP_IPV4 is never set, ZEBRA_NEXTHOP_IFINDEX
564 * is hard-coded.
565 */
566 /* Nexthop */
567
568 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
569 {
570 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB)
571 || nexthop_has_fib_child(nexthop))
572 {
573 SET_FLAG (zapi_flags, ZAPI_MESSAGE_NEXTHOP);
574 SET_FLAG (zapi_flags, ZAPI_MESSAGE_IFINDEX);
575
576 if (nhnummark == 0)
577 {
578 nhnummark = stream_get_endp (s);
579 stream_putc (s, 1); /* placeholder */
580 }
581
582 nhnum++;
583
584 switch(nexthop->type)
585 {
586 case NEXTHOP_TYPE_IPV4:
587 case NEXTHOP_TYPE_IPV4_IFINDEX:
588 stream_put_in_addr (s, &nexthop->gate.ipv4);
589 break;
590 #ifdef HAVE_IPV6
591 case NEXTHOP_TYPE_IPV6:
592 case NEXTHOP_TYPE_IPV6_IFINDEX:
593 case NEXTHOP_TYPE_IPV6_IFNAME:
594 stream_write (s, (u_char *) &nexthop->gate.ipv6, 16);
595 break;
596 #endif
597 default:
598 if (cmd == ZEBRA_IPV4_ROUTE_ADD
599 || cmd == ZEBRA_IPV4_ROUTE_DELETE)
600 {
601 struct in_addr empty;
602 memset (&empty, 0, sizeof (struct in_addr));
603 stream_write (s, (u_char *) &empty, IPV4_MAX_BYTELEN);
604 }
605 else
606 {
607 struct in6_addr empty;
608 memset (&empty, 0, sizeof (struct in6_addr));
609 stream_write (s, (u_char *) &empty, IPV6_MAX_BYTELEN);
610 }
611 }
612
613 /* Interface index. */
614 stream_putc (s, 1);
615 stream_putl (s, nexthop->ifindex);
616
617 break;
618 }
619 }
620
621 /* Metric */
622 if (cmd == ZEBRA_IPV4_ROUTE_ADD || cmd == ZEBRA_IPV6_ROUTE_ADD)
623 {
624 SET_FLAG (zapi_flags, ZAPI_MESSAGE_DISTANCE);
625 stream_putc (s, rib->distance);
626 SET_FLAG (zapi_flags, ZAPI_MESSAGE_METRIC);
627 stream_putl (s, rib->metric);
628
629 /* tag */
630 if (rib->tag)
631 {
632 SET_FLAG(zapi_flags, ZAPI_MESSAGE_TAG);
633 stream_putw(s, rib->tag);
634 }
635 }
636
637 /* write real message flags value */
638 stream_putc_at (s, messmark, zapi_flags);
639
640 /* Write next-hop number */
641 if (nhnummark)
642 stream_putc_at (s, nhnummark, nhnum);
643
644 /* Write packet size. */
645 stream_putw_at (s, 0, stream_get_endp (s));
646
647 return zebra_server_send_message(client);
648 }
649
650 #ifdef HAVE_IPV6
651 static int
652 zsend_ipv6_nexthop_lookup (struct zserv *client, struct in6_addr *addr)
653 {
654 struct stream *s;
655 struct rib *rib;
656 unsigned long nump;
657 u_char num;
658 struct nexthop *nexthop;
659
660 /* Lookup nexthop. */
661 rib = rib_match_ipv6 (addr);
662
663 /* Get output stream. */
664 s = client->obuf;
665 stream_reset (s);
666
667 /* Fill in result. */
668 zserv_create_header (s, ZEBRA_IPV6_NEXTHOP_LOOKUP);
669 stream_put (s, &addr, 16);
670
671 if (rib)
672 {
673 stream_putl (s, rib->metric);
674 num = 0;
675 nump = stream_get_endp(s);
676 stream_putc (s, 0);
677 /* Only non-recursive routes are elegible to resolve nexthop we
678 * are looking up. Therefore, we will just iterate over the top
679 * chain of nexthops. */
680 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
681 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB))
682 {
683 stream_putc (s, nexthop->type);
684 switch (nexthop->type)
685 {
686 case ZEBRA_NEXTHOP_IPV6:
687 stream_put (s, &nexthop->gate.ipv6, 16);
688 break;
689 case ZEBRA_NEXTHOP_IPV6_IFINDEX:
690 case ZEBRA_NEXTHOP_IPV6_IFNAME:
691 stream_put (s, &nexthop->gate.ipv6, 16);
692 stream_putl (s, nexthop->ifindex);
693 break;
694 case ZEBRA_NEXTHOP_IFINDEX:
695 case ZEBRA_NEXTHOP_IFNAME:
696 stream_putl (s, nexthop->ifindex);
697 break;
698 default:
699 /* do nothing */
700 break;
701 }
702 num++;
703 }
704 stream_putc_at (s, nump, num);
705 }
706 else
707 {
708 stream_putl (s, 0);
709 stream_putc (s, 0);
710 }
711
712 stream_putw_at (s, 0, stream_get_endp (s));
713
714 return zebra_server_send_message(client);
715 }
716 #endif /* HAVE_IPV6 */
717
718 static int
719 zsend_ipv4_nexthop_lookup (struct zserv *client, struct in_addr addr)
720 {
721 struct stream *s;
722 struct rib *rib;
723 unsigned long nump;
724 u_char num;
725 struct nexthop *nexthop;
726
727 /* Lookup nexthop. */
728 rib = rib_match_ipv4 (addr);
729
730 /* Get output stream. */
731 s = client->obuf;
732 stream_reset (s);
733
734 /* Fill in result. */
735 zserv_create_header (s, ZEBRA_IPV4_NEXTHOP_LOOKUP);
736 stream_put_in_addr (s, &addr);
737
738 if (rib)
739 {
740 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
741 zlog_debug("%s: Matching rib entry found.", __func__);
742 stream_putl (s, rib->metric);
743 num = 0;
744 nump = stream_get_endp(s);
745 stream_putc (s, 0);
746 /* Only non-recursive routes are elegible to resolve the nexthop we
747 * are looking up. Therefore, we will just iterate over the top
748 * chain of nexthops. */
749 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
750 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB))
751 {
752 stream_putc (s, nexthop->type);
753 switch (nexthop->type)
754 {
755 case ZEBRA_NEXTHOP_IPV4:
756 stream_put_in_addr (s, &nexthop->gate.ipv4);
757 break;
758 case ZEBRA_NEXTHOP_IPV4_IFINDEX:
759 stream_put_in_addr (s, &nexthop->gate.ipv4);
760 stream_putl (s, nexthop->ifindex);
761 break;
762 case ZEBRA_NEXTHOP_IFINDEX:
763 case ZEBRA_NEXTHOP_IFNAME:
764 stream_putl (s, nexthop->ifindex);
765 break;
766 default:
767 /* do nothing */
768 break;
769 }
770 num++;
771 }
772 stream_putc_at (s, nump, num);
773 }
774 else
775 {
776 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
777 zlog_debug("%s: No matching rib entry found.", __func__);
778 stream_putl (s, 0);
779 stream_putc (s, 0);
780 }
781
782 stream_putw_at (s, 0, stream_get_endp (s));
783
784 return zebra_server_send_message(client);
785 }
786
787 /* Nexthop register */
788 static int
789 zserv_rnh_register (struct zserv *client, int sock, u_short length,
790 rnh_type_t type)
791 {
792 struct rnh *rnh;
793 struct stream *s;
794 struct prefix p;
795 u_short l = 0;
796 u_char flags = 0;
797
798 if (IS_ZEBRA_DEBUG_NHT)
799 zlog_debug("rnh_register msg from client %s: length=%d, type=%s\n",
800 zebra_route_string(client->proto), length,
801 (type == RNH_NEXTHOP_TYPE) ? "nexthop" : "route");
802
803 s = client->ibuf;
804
805 client->nh_reg_time = quagga_time(NULL);
806
807 while (l < length)
808 {
809 flags = stream_getc(s);
810 p.family = stream_getw(s);
811 p.prefixlen = stream_getc(s);
812 l += 4;
813 if (p.family == AF_INET)
814 {
815 p.u.prefix4.s_addr = stream_get_ipv4(s);
816 l += IPV4_MAX_BYTELEN;
817 }
818 else if (p.family == AF_INET6)
819 {
820 stream_get(&p.u.prefix6, s, IPV6_MAX_BYTELEN);
821 l += IPV6_MAX_BYTELEN;
822 }
823 else
824 {
825 zlog_err("rnh_register: Received unknown family type %d\n",
826 p.family);
827 return -1;
828 }
829 rnh = zebra_add_rnh(&p, 0, type);
830 if (type == RNH_NEXTHOP_TYPE)
831 {
832 if (flags && !CHECK_FLAG(rnh->flags, ZEBRA_NHT_CONNECTED))
833 SET_FLAG(rnh->flags, ZEBRA_NHT_CONNECTED);
834 else if (!flags && CHECK_FLAG(rnh->flags, ZEBRA_NHT_CONNECTED))
835 UNSET_FLAG(rnh->flags, ZEBRA_NHT_CONNECTED);
836 }
837 else if (type == RNH_IMPORT_CHECK_TYPE)
838 {
839 if (flags && !CHECK_FLAG(rnh->flags, ZEBRA_NHT_EXACT_MATCH))
840 SET_FLAG(rnh->flags, ZEBRA_NHT_EXACT_MATCH);
841 else if (!flags && CHECK_FLAG(rnh->flags, ZEBRA_NHT_EXACT_MATCH))
842 UNSET_FLAG(rnh->flags, ZEBRA_NHT_EXACT_MATCH);
843 }
844
845 zebra_add_rnh_client(rnh, client, type);
846 /* Anything not AF_INET/INET6 has been filtered out above */
847 zebra_evaluate_rnh(0, p.family, 1, type, &p);
848 }
849 return 0;
850 }
851
852 /* Nexthop register */
853 static int
854 zserv_rnh_unregister (struct zserv *client, int sock, u_short length,
855 rnh_type_t type)
856 {
857 struct rnh *rnh;
858 struct stream *s;
859 struct prefix p;
860 u_short l = 0;
861
862 if (IS_ZEBRA_DEBUG_NHT)
863 zlog_debug("rnh_unregister msg from client %s: length=%d\n",
864 zebra_route_string(client->proto), length);
865
866 s = client->ibuf;
867
868 while (l < length)
869 {
870 (void)stream_getc(s); //Connected or not. Not used in this function
871 p.family = stream_getw(s);
872 p.prefixlen = stream_getc(s);
873 l += 4;
874 if (p.family == AF_INET)
875 {
876 p.u.prefix4.s_addr = stream_get_ipv4(s);
877 l += IPV4_MAX_BYTELEN;
878 }
879 else if (p.family == AF_INET6)
880 {
881 stream_get(&p.u.prefix6, s, IPV6_MAX_BYTELEN);
882 l += IPV6_MAX_BYTELEN;
883 }
884 else
885 {
886 zlog_err("rnh_register: Received unknown family type %d\n",
887 p.family);
888 return -1;
889 }
890 rnh = zebra_lookup_rnh(&p, 0, type);
891 if (rnh)
892 {
893 client->nh_dereg_time = quagga_time(NULL);
894 zebra_remove_rnh_client(rnh, client, type);
895 }
896 }
897 return 0;
898 }
899
900 static int
901 zsend_ipv4_import_lookup (struct zserv *client, struct prefix_ipv4 *p)
902 {
903 struct stream *s;
904 struct rib *rib;
905 unsigned long nump;
906 u_char num;
907 struct nexthop *nexthop;
908
909 /* Lookup nexthop. */
910 rib = rib_lookup_ipv4 (p);
911
912 /* Get output stream. */
913 s = client->obuf;
914 stream_reset (s);
915
916 /* Fill in result. */
917 zserv_create_header (s, ZEBRA_IPV4_IMPORT_LOOKUP);
918 stream_put_in_addr (s, &p->prefix);
919
920 if (rib)
921 {
922 stream_putl (s, rib->metric);
923 num = 0;
924 nump = stream_get_endp(s);
925 stream_putc (s, 0);
926 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
927 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB)
928 || nexthop_has_fib_child(nexthop))
929 {
930 stream_putc (s, nexthop->type);
931 switch (nexthop->type)
932 {
933 case ZEBRA_NEXTHOP_IPV4:
934 stream_put_in_addr (s, &nexthop->gate.ipv4);
935 break;
936 case ZEBRA_NEXTHOP_IPV4_IFINDEX:
937 stream_put_in_addr (s, &nexthop->gate.ipv4);
938 stream_putl (s, nexthop->ifindex);
939 break;
940 case ZEBRA_NEXTHOP_IFINDEX:
941 case ZEBRA_NEXTHOP_IFNAME:
942 stream_putl (s, nexthop->ifindex);
943 break;
944 default:
945 /* do nothing */
946 break;
947 }
948 num++;
949 }
950 stream_putc_at (s, nump, num);
951 }
952 else
953 {
954 stream_putl (s, 0);
955 stream_putc (s, 0);
956 }
957
958 stream_putw_at (s, 0, stream_get_endp (s));
959
960 return zebra_server_send_message(client);
961 }
962
963 /* Router-id is updated. Send ZEBRA_ROUTER_ID_ADD to client. */
964 int
965 zsend_router_id_update (struct zserv *client, struct prefix *p)
966 {
967 struct stream *s;
968 int blen;
969
970 /* Check this client need interface information. */
971 if (!client->ridinfo)
972 return 0;
973
974 s = client->obuf;
975 stream_reset (s);
976
977 /* Message type. */
978 zserv_create_header (s, ZEBRA_ROUTER_ID_UPDATE);
979
980 /* Prefix information. */
981 stream_putc (s, p->family);
982 blen = prefix_blen (p);
983 stream_put (s, &p->u.prefix, blen);
984 stream_putc (s, p->prefixlen);
985
986 /* Write packet size. */
987 stream_putw_at (s, 0, stream_get_endp (s));
988
989 return zebra_server_send_message(client);
990 }
991
992 /* Register zebra server interface information. Send current all
993 interface and address information. */
994 static int
995 zread_interface_add (struct zserv *client, u_short length)
996 {
997 struct listnode *ifnode, *ifnnode;
998 struct listnode *cnode, *cnnode;
999 struct interface *ifp;
1000 struct connected *c;
1001 struct nbr_connected *nc;
1002
1003 /* Interface information is needed. */
1004 client->ifinfo = 1;
1005
1006 for (ALL_LIST_ELEMENTS (iflist, ifnode, ifnnode, ifp))
1007 {
1008 /* Skip pseudo interface. */
1009 if (! CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE))
1010 continue;
1011
1012 if (zsend_interface_add (client, ifp) < 0)
1013 return -1;
1014
1015 for (ALL_LIST_ELEMENTS (ifp->connected, cnode, cnnode, c))
1016 {
1017 if (CHECK_FLAG (c->conf, ZEBRA_IFC_REAL) &&
1018 (zsend_interface_address (ZEBRA_INTERFACE_ADDRESS_ADD, client,
1019 ifp, c) < 0))
1020 return -1;
1021 }
1022 for (ALL_LIST_ELEMENTS (ifp->nbr_connected, cnode, cnnode, nc))
1023 {
1024 if (zsend_interface_nbr_address (ZEBRA_INTERFACE_NBR_ADDRESS_ADD, client,
1025 ifp, nc) < 0)
1026 return -1;
1027 }
1028
1029 }
1030 return 0;
1031 }
1032
1033 /* Unregister zebra server interface information. */
1034 static int
1035 zread_interface_delete (struct zserv *client, u_short length)
1036 {
1037 client->ifinfo = 0;
1038 return 0;
1039 }
1040
1041 /* This function support multiple nexthop. */
1042 /*
1043 * Parse the ZEBRA_IPV4_ROUTE_ADD sent from client. Update rib and
1044 * add kernel route.
1045 */
1046 static int
1047 zread_ipv4_add (struct zserv *client, u_short length)
1048 {
1049 int i;
1050 struct rib *rib;
1051 struct prefix_ipv4 p;
1052 u_char message;
1053 struct in_addr nexthop;
1054 u_char nexthop_num;
1055 u_char nexthop_type;
1056 struct stream *s;
1057 unsigned int ifindex;
1058 u_char ifname_len;
1059 safi_t safi;
1060 int ret;
1061
1062 /* Get input stream. */
1063 s = client->ibuf;
1064
1065 /* Allocate new rib. */
1066 rib = XCALLOC (MTYPE_RIB, sizeof (struct rib));
1067
1068 /* Type, flags, message. */
1069 rib->type = stream_getc (s);
1070 rib->instance = stream_getw (s);
1071 rib->flags = stream_getc (s);
1072 message = stream_getc (s);
1073 safi = stream_getw (s);
1074 rib->uptime = time (NULL);
1075
1076 /* IPv4 prefix. */
1077 memset (&p, 0, sizeof (struct prefix_ipv4));
1078 p.family = AF_INET;
1079 p.prefixlen = stream_getc (s);
1080 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
1081
1082 /* Nexthop parse. */
1083 if (CHECK_FLAG (message, ZAPI_MESSAGE_NEXTHOP))
1084 {
1085 nexthop_num = stream_getc (s);
1086
1087 for (i = 0; i < nexthop_num; i++)
1088 {
1089 nexthop_type = stream_getc (s);
1090
1091 switch (nexthop_type)
1092 {
1093 case ZEBRA_NEXTHOP_IFINDEX:
1094 ifindex = stream_getl (s);
1095 nexthop_ifindex_add (rib, ifindex);
1096 break;
1097 case ZEBRA_NEXTHOP_IFNAME:
1098 ifname_len = stream_getc (s);
1099 stream_forward_getp (s, ifname_len);
1100 break;
1101 case ZEBRA_NEXTHOP_IPV4:
1102 nexthop.s_addr = stream_get_ipv4 (s);
1103 nexthop_ipv4_add (rib, &nexthop, NULL);
1104 break;
1105 case ZEBRA_NEXTHOP_IPV4_IFINDEX:
1106 nexthop.s_addr = stream_get_ipv4 (s);
1107 ifindex = stream_getl (s);
1108 nexthop_ipv4_ifindex_add (rib, &nexthop, NULL, ifindex);
1109 break;
1110 case ZEBRA_NEXTHOP_IPV6:
1111 stream_forward_getp (s, IPV6_MAX_BYTELEN);
1112 break;
1113 case ZEBRA_NEXTHOP_BLACKHOLE:
1114 nexthop_blackhole_add (rib);
1115 break;
1116 case ZEBRA_NEXTHOP_IPV4_ONLINK:
1117 nexthop.s_addr = stream_get_ipv4 (s);
1118 ifindex = stream_getl (s);
1119 nexthop_ipv4_ifindex_ol_add (rib, &nexthop, NULL, ifindex);
1120 break;
1121 }
1122 }
1123 }
1124
1125 /* Distance. */
1126 if (CHECK_FLAG (message, ZAPI_MESSAGE_DISTANCE))
1127 rib->distance = stream_getc (s);
1128
1129 /* Metric. */
1130 if (CHECK_FLAG (message, ZAPI_MESSAGE_METRIC))
1131 rib->metric = stream_getl (s);
1132
1133 /* Tag */
1134 if (CHECK_FLAG (message, ZAPI_MESSAGE_TAG))
1135 rib->tag = stream_getw (s);
1136 else
1137 rib->tag = 0;
1138
1139 /* Table */
1140 rib->table=zebrad.rtm_table_default;
1141 ret = rib_add_ipv4_multipath (&p, rib, safi);
1142
1143 /* Stats */
1144 if (ret > 0)
1145 client->v4_route_add_cnt++;
1146 else if (ret < 0)
1147 client->v4_route_upd8_cnt++;
1148 return 0;
1149 }
1150
1151 /* Zebra server IPv4 prefix delete function. */
1152 static int
1153 zread_ipv4_delete (struct zserv *client, u_short length)
1154 {
1155 int i;
1156 struct stream *s;
1157 struct zapi_ipv4 api;
1158 struct in_addr nexthop, *nexthop_p;
1159 unsigned long ifindex;
1160 struct prefix_ipv4 p;
1161 u_char nexthop_num;
1162 u_char nexthop_type;
1163 u_char ifname_len;
1164
1165 s = client->ibuf;
1166 ifindex = 0;
1167 nexthop.s_addr = 0;
1168 nexthop_p = NULL;
1169
1170 /* Type, flags, message. */
1171 api.type = stream_getc (s);
1172 api.instance = stream_getw (s);
1173 api.flags = stream_getc (s);
1174 api.message = stream_getc (s);
1175 api.safi = stream_getw (s);
1176
1177 /* IPv4 prefix. */
1178 memset (&p, 0, sizeof (struct prefix_ipv4));
1179 p.family = AF_INET;
1180 p.prefixlen = stream_getc (s);
1181 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
1182
1183 /* Nexthop, ifindex, distance, metric. */
1184 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
1185 {
1186 nexthop_num = stream_getc (s);
1187
1188 for (i = 0; i < nexthop_num; i++)
1189 {
1190 nexthop_type = stream_getc (s);
1191
1192 switch (nexthop_type)
1193 {
1194 case ZEBRA_NEXTHOP_IFINDEX:
1195 ifindex = stream_getl (s);
1196 break;
1197 case ZEBRA_NEXTHOP_IFNAME:
1198 ifname_len = stream_getc (s);
1199 stream_forward_getp (s, ifname_len);
1200 break;
1201 case ZEBRA_NEXTHOP_IPV4:
1202 nexthop.s_addr = stream_get_ipv4 (s);
1203 nexthop_p = &nexthop;
1204 break;
1205 case ZEBRA_NEXTHOP_IPV4_IFINDEX:
1206 nexthop.s_addr = stream_get_ipv4 (s);
1207 nexthop_p = &nexthop;
1208 ifindex = stream_getl (s);
1209 break;
1210 case ZEBRA_NEXTHOP_IPV6:
1211 stream_forward_getp (s, IPV6_MAX_BYTELEN);
1212 break;
1213 }
1214 }
1215 }
1216
1217 /* Distance. */
1218 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
1219 api.distance = stream_getc (s);
1220 else
1221 api.distance = 0;
1222
1223 /* Metric. */
1224 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
1225 api.metric = stream_getl (s);
1226 else
1227 api.metric = 0;
1228
1229 /* tag */
1230 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_TAG))
1231 api.tag = stream_getw (s);
1232 else
1233 api.tag = 0;
1234
1235 rib_delete_ipv4 (api.type, api.instance, api.flags, &p, nexthop_p, ifindex,
1236 client->rtm_table, api.safi);
1237 client->v4_route_del_cnt++;
1238 return 0;
1239 }
1240
1241 /* Nexthop lookup for IPv4. */
1242 static int
1243 zread_ipv4_nexthop_lookup (struct zserv *client, u_short length)
1244 {
1245 struct in_addr addr;
1246 char buf[BUFSIZ];
1247
1248 addr.s_addr = stream_get_ipv4 (client->ibuf);
1249 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
1250 zlog_debug("%s: looking up %s", __func__,
1251 inet_ntop (AF_INET, &addr, buf, BUFSIZ));
1252 return zsend_ipv4_nexthop_lookup (client, addr);
1253 }
1254
1255 /* Nexthop lookup for IPv4. */
1256 static int
1257 zread_ipv4_import_lookup (struct zserv *client, u_short length)
1258 {
1259 struct prefix_ipv4 p;
1260
1261 p.family = AF_INET;
1262 p.prefixlen = stream_getc (client->ibuf);
1263 p.prefix.s_addr = stream_get_ipv4 (client->ibuf);
1264
1265 return zsend_ipv4_import_lookup (client, &p);
1266 }
1267
1268 #ifdef HAVE_IPV6
1269 /* Zebra server IPv6 prefix add function. */
1270 static int
1271 zread_ipv4_route_ipv6_nexthop_add (struct zserv *client, u_short length)
1272 {
1273 int i;
1274 struct stream *s;
1275 struct in6_addr nexthop;
1276 struct rib *rib;
1277 u_char message;
1278 u_char nexthop_num;
1279 u_char nexthop_type;
1280 unsigned long ifindex;
1281 struct prefix_ipv4 p;
1282 u_char ifname_len;
1283 safi_t safi;
1284 static struct in6_addr nexthops[MULTIPATH_NUM];
1285 static unsigned int ifindices[MULTIPATH_NUM];
1286 int ret;
1287
1288 /* Get input stream. */
1289 s = client->ibuf;
1290
1291 ifindex = 0;
1292 memset (&nexthop, 0, sizeof (struct in6_addr));
1293
1294 /* Allocate new rib. */
1295 rib = XCALLOC (MTYPE_RIB, sizeof (struct rib));
1296
1297 /* Type, flags, message. */
1298 rib->type = stream_getc (s);
1299 rib->instance = stream_getw (s);
1300 rib->flags = stream_getc (s);
1301 message = stream_getc (s);
1302 safi = stream_getw (s);
1303 rib->uptime = time (NULL);
1304
1305 /* IPv4 prefix. */
1306 memset (&p, 0, sizeof (struct prefix_ipv4));
1307 p.family = AF_INET;
1308 p.prefixlen = stream_getc (s);
1309 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
1310
1311 /* We need to give nh-addr, nh-ifindex with the same next-hop object
1312 * to the rib to ensure that IPv6 multipathing works; need to coalesce
1313 * these. Clients should send the same number of paired set of
1314 * next-hop-addr/next-hop-ifindices. */
1315 if (CHECK_FLAG (message, ZAPI_MESSAGE_NEXTHOP))
1316 {
1317 int nh_count = 0;
1318 int if_count = 0;
1319 int max_nh_if = 0;
1320
1321 nexthop_num = stream_getc (s);
1322 for (i = 0; i < nexthop_num; i++)
1323 {
1324 nexthop_type = stream_getc (s);
1325
1326 switch (nexthop_type)
1327 {
1328 case ZEBRA_NEXTHOP_IPV6:
1329 stream_get (&nexthop, s, 16);
1330 if (nh_count < MULTIPATH_NUM) {
1331 nexthops[nh_count++] = nexthop;
1332 }
1333 break;
1334 case ZEBRA_NEXTHOP_IFINDEX:
1335 if (if_count < MULTIPATH_NUM) {
1336 ifindices[if_count++] = stream_getl (s);
1337 }
1338 break;
1339 case ZEBRA_NEXTHOP_BLACKHOLE:
1340 nexthop_blackhole_add (rib);
1341 break;
1342 }
1343 }
1344
1345 max_nh_if = (nh_count > if_count) ? nh_count : if_count;
1346 for (i = 0; i < max_nh_if; i++)
1347 {
1348 if ((i < nh_count) && !IN6_IS_ADDR_UNSPECIFIED (&nexthops[i])) {
1349 if ((i < if_count) && ifindices[i]) {
1350 nexthop_ipv6_ifindex_add (rib, &nexthops[i], ifindices[i]);
1351 }
1352 else {
1353 nexthop_ipv6_add (rib, &nexthops[i]);
1354 }
1355 }
1356 else {
1357 if ((i < if_count) && ifindices[i]) {
1358 nexthop_ifindex_add (rib, ifindices[i]);
1359 }
1360 }
1361 }
1362 }
1363
1364 /* Distance. */
1365 if (CHECK_FLAG (message, ZAPI_MESSAGE_DISTANCE))
1366 rib->distance = stream_getc (s);
1367
1368 /* Metric. */
1369 if (CHECK_FLAG (message, ZAPI_MESSAGE_METRIC))
1370 rib->metric = stream_getl (s);
1371
1372 /* Tag */
1373 if (CHECK_FLAG (message, ZAPI_MESSAGE_TAG))
1374 rib->tag = stream_getw (s);
1375 else
1376 rib->tag = 0;
1377
1378 /* Table */
1379 rib->table=zebrad.rtm_table_default;
1380 ret = rib_add_ipv6_multipath ((struct prefix *)&p, rib, safi, ifindex);
1381 /* Stats */
1382 if (ret > 0)
1383 client->v4_route_add_cnt++;
1384 else if (ret < 0)
1385 client->v4_route_upd8_cnt++;
1386
1387 return 0;
1388 }
1389
1390 /* Zebra server IPv6 prefix add function. */
1391 static int
1392 zread_ipv6_add (struct zserv *client, u_short length)
1393 {
1394 int i;
1395 struct stream *s;
1396 struct in6_addr nexthop;
1397 struct rib *rib;
1398 u_char message;
1399 u_char nexthop_num;
1400 u_char nexthop_type;
1401 unsigned long ifindex;
1402 struct prefix_ipv6 p;
1403 safi_t safi;
1404 static struct in6_addr nexthops[MULTIPATH_NUM];
1405 static unsigned int ifindices[MULTIPATH_NUM];
1406 int ret;
1407
1408 /* Get input stream. */
1409 s = client->ibuf;
1410
1411 ifindex = 0;
1412 memset (&nexthop, 0, sizeof (struct in6_addr));
1413
1414 /* Allocate new rib. */
1415 rib = XCALLOC (MTYPE_RIB, sizeof (struct rib));
1416
1417 /* Type, flags, message. */
1418 rib->type = stream_getc (s);
1419 rib->instance = stream_getw (s);
1420 rib->flags = stream_getc (s);
1421 message = stream_getc (s);
1422 safi = stream_getw (s);
1423 rib->uptime = time (NULL);
1424
1425 /* IPv6 prefix. */
1426 memset (&p, 0, sizeof (struct prefix_ipv6));
1427 p.family = AF_INET6;
1428 p.prefixlen = stream_getc (s);
1429 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
1430
1431 /* We need to give nh-addr, nh-ifindex with the same next-hop object
1432 * to the rib to ensure that IPv6 multipathing works; need to coalesce
1433 * these. Clients should send the same number of paired set of
1434 * next-hop-addr/next-hop-ifindices. */
1435 if (CHECK_FLAG (message, ZAPI_MESSAGE_NEXTHOP))
1436 {
1437 int nh_count = 0;
1438 int if_count = 0;
1439 int max_nh_if = 0;
1440
1441 nexthop_num = stream_getc (s);
1442 for (i = 0; i < nexthop_num; i++)
1443 {
1444 nexthop_type = stream_getc (s);
1445
1446 switch (nexthop_type)
1447 {
1448 case ZEBRA_NEXTHOP_IPV6:
1449 stream_get (&nexthop, s, 16);
1450 if (nh_count < MULTIPATH_NUM) {
1451 nexthops[nh_count++] = nexthop;
1452 }
1453 break;
1454 case ZEBRA_NEXTHOP_IFINDEX:
1455 if (if_count < MULTIPATH_NUM) {
1456 ifindices[if_count++] = stream_getl (s);
1457 }
1458 break;
1459 case ZEBRA_NEXTHOP_BLACKHOLE:
1460 nexthop_blackhole_add (rib);
1461 break;
1462 }
1463 }
1464
1465 max_nh_if = (nh_count > if_count) ? nh_count : if_count;
1466 for (i = 0; i < max_nh_if; i++)
1467 {
1468 if ((i < nh_count) && !IN6_IS_ADDR_UNSPECIFIED (&nexthops[i])) {
1469 if ((i < if_count) && ifindices[i]) {
1470 if (rib_bogus_ipv6 (rib->type, &p, &nexthops[i], ifindices[i], 0)) {
1471 continue;
1472 }
1473 nexthop_ipv6_ifindex_add (rib, &nexthops[i], ifindices[i]);
1474 }
1475 else {
1476 if (rib_bogus_ipv6 (rib->type, &p, &nexthops[i], 0, 0)) {
1477 continue;
1478 }
1479 nexthop_ipv6_add (rib, &nexthops[i]);
1480 }
1481 }
1482 else {
1483 if ((i < if_count) && ifindices[i]) {
1484 if (rib_bogus_ipv6 (rib->type, &p, NULL, ifindices[i], 0)) {
1485 continue;
1486 }
1487 nexthop_ifindex_add (rib, ifindices[i]);
1488 }
1489 }
1490 }
1491 }
1492
1493 /* Distance. */
1494 if (CHECK_FLAG (message, ZAPI_MESSAGE_DISTANCE))
1495 rib->distance = stream_getc (s);
1496
1497 /* Metric. */
1498 if (CHECK_FLAG (message, ZAPI_MESSAGE_METRIC))
1499 rib->metric = stream_getl (s);
1500
1501 /* Tag */
1502 if (CHECK_FLAG (message, ZAPI_MESSAGE_TAG))
1503 rib->tag = stream_getw (s);
1504 else
1505 rib->tag = 0;
1506
1507 /* Table */
1508 rib->table=zebrad.rtm_table_default;
1509 ret = rib_add_ipv6_multipath ((struct prefix *)&p, rib, safi, ifindex);
1510 /* Stats */
1511 if (ret > 0)
1512 client->v6_route_add_cnt++;
1513 else if (ret < 0)
1514 client->v6_route_upd8_cnt++;
1515
1516 return 0;
1517 }
1518
1519 /* Zebra server IPv6 prefix delete function. */
1520 static int
1521 zread_ipv6_delete (struct zserv *client, u_short length)
1522 {
1523 int i;
1524 struct stream *s;
1525 struct zapi_ipv6 api;
1526 struct in6_addr nexthop;
1527 unsigned long ifindex;
1528 struct prefix_ipv6 p;
1529
1530 s = client->ibuf;
1531 ifindex = 0;
1532 memset (&nexthop, 0, sizeof (struct in6_addr));
1533
1534 /* Type, flags, message. */
1535 api.type = stream_getc (s);
1536 api.instance = stream_getw (s);
1537 api.flags = stream_getc (s);
1538 api.message = stream_getc (s);
1539 api.safi = stream_getw (s);
1540
1541 /* IPv4 prefix. */
1542 memset (&p, 0, sizeof (struct prefix_ipv6));
1543 p.family = AF_INET6;
1544 p.prefixlen = stream_getc (s);
1545 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
1546
1547 /* Nexthop, ifindex, distance, metric. */
1548 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
1549 {
1550 u_char nexthop_type;
1551
1552 api.nexthop_num = stream_getc (s);
1553 for (i = 0; i < api.nexthop_num; i++)
1554 {
1555 nexthop_type = stream_getc (s);
1556
1557 switch (nexthop_type)
1558 {
1559 case ZEBRA_NEXTHOP_IPV6:
1560 stream_get (&nexthop, s, 16);
1561 break;
1562 case ZEBRA_NEXTHOP_IFINDEX:
1563 ifindex = stream_getl (s);
1564 break;
1565 }
1566 }
1567 }
1568
1569 /* Distance. */
1570 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
1571 api.distance = stream_getc (s);
1572 else
1573 api.distance = 0;
1574
1575 /* Metric. */
1576 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
1577 api.metric = stream_getl (s);
1578 else
1579 api.metric = 0;
1580
1581 /* tag */
1582 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_TAG))
1583 api.tag = stream_getw (s);
1584 else
1585 api.tag = 0;
1586
1587 if (IN6_IS_ADDR_UNSPECIFIED (&nexthop))
1588 rib_delete_ipv6 (api.type, api.instance, api.flags, &p, NULL, ifindex, client->rtm_table, api.safi);
1589 else
1590 rib_delete_ipv6 (api.type, api.instance, api.flags, &p, &nexthop, ifindex, client->rtm_table, api.safi);
1591
1592 client->v6_route_del_cnt++;
1593 return 0;
1594 }
1595
1596 static int
1597 zread_ipv6_nexthop_lookup (struct zserv *client, u_short length)
1598 {
1599 struct in6_addr addr;
1600 char buf[BUFSIZ];
1601
1602 stream_get (&addr, client->ibuf, 16);
1603 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
1604 zlog_debug("%s: looking up %s", __func__,
1605 inet_ntop (AF_INET6, &addr, buf, BUFSIZ));
1606
1607 return zsend_ipv6_nexthop_lookup (client, &addr);
1608 }
1609 #endif /* HAVE_IPV6 */
1610
1611 /* Register zebra server router-id information. Send current router-id */
1612 static int
1613 zread_router_id_add (struct zserv *client, u_short length)
1614 {
1615 struct prefix p;
1616
1617 /* Router-id information is needed. */
1618 client->ridinfo = 1;
1619
1620 router_id_get (&p);
1621
1622 return zsend_router_id_update (client,&p);
1623 }
1624
1625 /* Unregister zebra server router-id information. */
1626 static int
1627 zread_router_id_delete (struct zserv *client, u_short length)
1628 {
1629 client->ridinfo = 0;
1630 return 0;
1631 }
1632
1633 /* Tie up route-type and client->sock */
1634 static void
1635 zread_hello (struct zserv *client)
1636 {
1637 /* type of protocol (lib/zebra.h) */
1638 u_char proto;
1639 u_short instance;
1640
1641 proto = stream_getc (client->ibuf);
1642 instance = stream_getw (client->ibuf);
1643
1644 /* accept only dynamic routing protocols */
1645 if ((proto < ZEBRA_ROUTE_MAX)
1646 && (proto > ZEBRA_ROUTE_STATIC))
1647 {
1648 zlog_notice ("client %d says hello and bids fair to announce only %s routes",
1649 client->sock, zebra_route_string(proto));
1650 if (instance)
1651 zlog_notice ("client protocol instance %d", instance);
1652
1653 client->proto = proto;
1654 client->instance = instance;
1655 }
1656 }
1657
1658 /* Close zebra client. */
1659 static void
1660 zebra_client_close (struct zserv *client)
1661 {
1662 zebra_cleanup_rnh_client(0, AF_INET, client, RNH_NEXTHOP_TYPE);
1663 zebra_cleanup_rnh_client(0, AF_INET6, client, RNH_NEXTHOP_TYPE);
1664 zebra_cleanup_rnh_client(0, AF_INET, client, RNH_IMPORT_CHECK_TYPE);
1665 zebra_cleanup_rnh_client(0, AF_INET6, client, RNH_IMPORT_CHECK_TYPE);
1666
1667 /* Close file descriptor. */
1668 if (client->sock)
1669 {
1670 unsigned long nroutes;
1671
1672 close (client->sock);
1673 nroutes = rib_score_proto (client->proto, client->instance);
1674 zlog_notice ("client %d disconnected. %lu %s routes removed from the rib",
1675 client->sock, nroutes, zebra_route_string (client->proto));
1676 client->sock = -1;
1677 }
1678
1679 /* Free stream buffers. */
1680 if (client->ibuf)
1681 stream_free (client->ibuf);
1682 if (client->obuf)
1683 stream_free (client->obuf);
1684 if (client->wb)
1685 buffer_free(client->wb);
1686
1687 /* Release threads. */
1688 if (client->t_read)
1689 thread_cancel (client->t_read);
1690 if (client->t_write)
1691 thread_cancel (client->t_write);
1692 if (client->t_suicide)
1693 thread_cancel (client->t_suicide);
1694
1695 /* Free client structure. */
1696 listnode_delete (zebrad.client_list, client);
1697 XFREE (0, client);
1698 }
1699
1700 /* Make new client. */
1701 static void
1702 zebra_client_create (int sock)
1703 {
1704 struct zserv *client;
1705
1706 client = XCALLOC (0, sizeof (struct zserv));
1707
1708 /* Make client input/output buffer. */
1709 client->sock = sock;
1710 client->ibuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
1711 client->obuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
1712 client->wb = buffer_new(0);
1713
1714 /* Set table number. */
1715 client->rtm_table = zebrad.rtm_table_default;
1716
1717 client->connect_time = quagga_time(NULL);
1718
1719 /* Add this client to linked list. */
1720 listnode_add (zebrad.client_list, client);
1721
1722 /* Make new read thread. */
1723 zebra_event (ZEBRA_READ, sock, client);
1724 }
1725
1726 /* Handler of zebra service request. */
1727 static int
1728 zebra_client_read (struct thread *thread)
1729 {
1730 int sock;
1731 struct zserv *client;
1732 size_t already;
1733 uint16_t length, command;
1734 uint8_t marker, version;
1735
1736 /* Get thread data. Reset reading thread because I'm running. */
1737 sock = THREAD_FD (thread);
1738 client = THREAD_ARG (thread);
1739 client->t_read = NULL;
1740
1741 if (client->t_suicide)
1742 {
1743 zebra_client_close(client);
1744 return -1;
1745 }
1746
1747 /* Read length and command (if we don't have it already). */
1748 if ((already = stream_get_endp(client->ibuf)) < ZEBRA_HEADER_SIZE)
1749 {
1750 ssize_t nbyte;
1751 if (((nbyte = stream_read_try (client->ibuf, sock,
1752 ZEBRA_HEADER_SIZE-already)) == 0) ||
1753 (nbyte == -1))
1754 {
1755 if (IS_ZEBRA_DEBUG_EVENT)
1756 zlog_debug ("connection closed socket [%d]", sock);
1757 zebra_client_close (client);
1758 return -1;
1759 }
1760 if (nbyte != (ssize_t)(ZEBRA_HEADER_SIZE-already))
1761 {
1762 /* Try again later. */
1763 zebra_event (ZEBRA_READ, sock, client);
1764 return 0;
1765 }
1766 already = ZEBRA_HEADER_SIZE;
1767 }
1768
1769 /* Reset to read from the beginning of the incoming packet. */
1770 stream_set_getp(client->ibuf, 0);
1771
1772 /* Fetch header values */
1773 length = stream_getw (client->ibuf);
1774 marker = stream_getc (client->ibuf);
1775 version = stream_getc (client->ibuf);
1776 command = stream_getw (client->ibuf);
1777
1778 if (marker != ZEBRA_HEADER_MARKER || version != ZSERV_VERSION)
1779 {
1780 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
1781 __func__, sock, marker, version);
1782 zebra_client_close (client);
1783 return -1;
1784 }
1785 if (length < ZEBRA_HEADER_SIZE)
1786 {
1787 zlog_warn("%s: socket %d message length %u is less than header size %d",
1788 __func__, sock, length, ZEBRA_HEADER_SIZE);
1789 zebra_client_close (client);
1790 return -1;
1791 }
1792 if (length > STREAM_SIZE(client->ibuf))
1793 {
1794 zlog_warn("%s: socket %d message length %u exceeds buffer size %lu",
1795 __func__, sock, length, (u_long)STREAM_SIZE(client->ibuf));
1796 zebra_client_close (client);
1797 return -1;
1798 }
1799
1800 /* Read rest of data. */
1801 if (already < length)
1802 {
1803 ssize_t nbyte;
1804 if (((nbyte = stream_read_try (client->ibuf, sock,
1805 length-already)) == 0) ||
1806 (nbyte == -1))
1807 {
1808 if (IS_ZEBRA_DEBUG_EVENT)
1809 zlog_debug ("connection closed [%d] when reading zebra data", sock);
1810 zebra_client_close (client);
1811 return -1;
1812 }
1813 if (nbyte != (ssize_t)(length-already))
1814 {
1815 /* Try again later. */
1816 zebra_event (ZEBRA_READ, sock, client);
1817 return 0;
1818 }
1819 }
1820
1821 length -= ZEBRA_HEADER_SIZE;
1822
1823 /* Debug packet information. */
1824 if (IS_ZEBRA_DEBUG_EVENT)
1825 zlog_debug ("zebra message comes from socket [%d]", sock);
1826
1827 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
1828 zlog_debug ("zebra message received [%s] %d",
1829 zserv_command_string (command), length);
1830
1831 client->last_read_time = quagga_time(NULL);
1832 client->last_read_cmd = command;
1833
1834 switch (command)
1835 {
1836 case ZEBRA_ROUTER_ID_ADD:
1837 zread_router_id_add (client, length);
1838 break;
1839 case ZEBRA_ROUTER_ID_DELETE:
1840 zread_router_id_delete (client, length);
1841 break;
1842 case ZEBRA_INTERFACE_ADD:
1843 zread_interface_add (client, length);
1844 break;
1845 case ZEBRA_INTERFACE_DELETE:
1846 zread_interface_delete (client, length);
1847 break;
1848 case ZEBRA_IPV4_ROUTE_ADD:
1849 zread_ipv4_add (client, length);
1850 break;
1851 case ZEBRA_IPV4_ROUTE_DELETE:
1852 zread_ipv4_delete (client, length);
1853 break;
1854 #ifdef HAVE_IPV6
1855 case ZEBRA_IPV4_ROUTE_IPV6_NEXTHOP_ADD:
1856 zread_ipv4_route_ipv6_nexthop_add (client, length);
1857 break;
1858 case ZEBRA_IPV6_ROUTE_ADD:
1859 zread_ipv6_add (client, length);
1860 break;
1861 case ZEBRA_IPV6_ROUTE_DELETE:
1862 zread_ipv6_delete (client, length);
1863 break;
1864 #endif /* HAVE_IPV6 */
1865 case ZEBRA_REDISTRIBUTE_ADD:
1866 zebra_redistribute_add (command, client, length);
1867 break;
1868 case ZEBRA_REDISTRIBUTE_DELETE:
1869 zebra_redistribute_delete (command, client, length);
1870 break;
1871 case ZEBRA_REDISTRIBUTE_DEFAULT_ADD:
1872 zebra_redistribute_default_add (command, client, length);
1873 break;
1874 case ZEBRA_REDISTRIBUTE_DEFAULT_DELETE:
1875 zebra_redistribute_default_delete (command, client, length);
1876 break;
1877 case ZEBRA_IPV4_NEXTHOP_LOOKUP:
1878 zread_ipv4_nexthop_lookup (client, length);
1879 break;
1880 #ifdef HAVE_IPV6
1881 case ZEBRA_IPV6_NEXTHOP_LOOKUP:
1882 zread_ipv6_nexthop_lookup (client, length);
1883 break;
1884 #endif /* HAVE_IPV6 */
1885 case ZEBRA_IPV4_IMPORT_LOOKUP:
1886 zread_ipv4_import_lookup (client, length);
1887 break;
1888 case ZEBRA_HELLO:
1889 zread_hello (client);
1890 break;
1891 case ZEBRA_NEXTHOP_REGISTER:
1892 zserv_rnh_register(client, sock, length, RNH_NEXTHOP_TYPE);
1893 break;
1894 case ZEBRA_NEXTHOP_UNREGISTER:
1895 zserv_rnh_unregister(client, sock, length, RNH_NEXTHOP_TYPE);
1896 break;
1897 case ZEBRA_IMPORT_ROUTE_REGISTER:
1898 zserv_rnh_register(client, sock, length, RNH_IMPORT_CHECK_TYPE);
1899 break;
1900 case ZEBRA_IMPORT_ROUTE_UNREGISTER:
1901 zserv_rnh_unregister(client, sock, length, RNH_IMPORT_CHECK_TYPE);
1902 break;
1903 default:
1904 zlog_info ("Zebra received unknown command %d", command);
1905 break;
1906 }
1907
1908 if (client->t_suicide)
1909 {
1910 /* No need to wait for thread callback, just kill immediately. */
1911 zebra_client_close(client);
1912 return -1;
1913 }
1914
1915 stream_reset (client->ibuf);
1916 zebra_event (ZEBRA_READ, sock, client);
1917 return 0;
1918 }
1919
1920
1921 /* Accept code of zebra server socket. */
1922 static int
1923 zebra_accept (struct thread *thread)
1924 {
1925 int accept_sock;
1926 int client_sock;
1927 struct sockaddr_in client;
1928 socklen_t len;
1929
1930 accept_sock = THREAD_FD (thread);
1931
1932 /* Reregister myself. */
1933 zebra_event (ZEBRA_SERV, accept_sock, NULL);
1934
1935 len = sizeof (struct sockaddr_in);
1936 client_sock = accept (accept_sock, (struct sockaddr *) &client, &len);
1937
1938 if (client_sock < 0)
1939 {
1940 zlog_warn ("Can't accept zebra socket: %s", safe_strerror (errno));
1941 return -1;
1942 }
1943
1944 /* Make client socket non-blocking. */
1945 set_nonblocking(client_sock);
1946
1947 /* Create new zebra client. */
1948 zebra_client_create (client_sock);
1949
1950 return 0;
1951 }
1952
1953 #ifdef HAVE_TCP_ZEBRA
1954 /* Make zebra's server socket. */
1955 static void
1956 zebra_serv ()
1957 {
1958 int ret;
1959 int accept_sock;
1960 struct sockaddr_in addr;
1961
1962 accept_sock = socket (AF_INET, SOCK_STREAM, 0);
1963
1964 if (accept_sock < 0)
1965 {
1966 zlog_warn ("Can't create zserv stream socket: %s",
1967 safe_strerror (errno));
1968 zlog_warn ("zebra can't provice full functionality due to above error");
1969 return;
1970 }
1971
1972 memset (&addr, 0, sizeof (struct sockaddr_in));
1973 addr.sin_family = AF_INET;
1974 addr.sin_port = htons (ZEBRA_PORT);
1975 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
1976 addr.sin_len = sizeof (struct sockaddr_in);
1977 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
1978 addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
1979
1980 sockopt_reuseaddr (accept_sock);
1981 sockopt_reuseport (accept_sock);
1982
1983 if ( zserv_privs.change(ZPRIVS_RAISE) )
1984 zlog (NULL, LOG_ERR, "Can't raise privileges");
1985
1986 ret = bind (accept_sock, (struct sockaddr *)&addr,
1987 sizeof (struct sockaddr_in));
1988 if (ret < 0)
1989 {
1990 zlog_warn ("Can't bind to stream socket: %s",
1991 safe_strerror (errno));
1992 zlog_warn ("zebra can't provice full functionality due to above error");
1993 close (accept_sock); /* Avoid sd leak. */
1994 return;
1995 }
1996
1997 if ( zserv_privs.change(ZPRIVS_LOWER) )
1998 zlog (NULL, LOG_ERR, "Can't lower privileges");
1999
2000 ret = listen (accept_sock, 1);
2001 if (ret < 0)
2002 {
2003 zlog_warn ("Can't listen to stream socket: %s",
2004 safe_strerror (errno));
2005 zlog_warn ("zebra can't provice full functionality due to above error");
2006 close (accept_sock); /* Avoid sd leak. */
2007 return;
2008 }
2009
2010 zebra_event (ZEBRA_SERV, accept_sock, NULL);
2011 }
2012 #endif /* HAVE_TCP_ZEBRA */
2013
2014 /* For sockaddr_un. */
2015 #include <sys/un.h>
2016
2017 /* zebra server UNIX domain socket. */
2018 static void
2019 zebra_serv_un (const char *path)
2020 {
2021 int ret;
2022 int sock, len;
2023 struct sockaddr_un serv;
2024 mode_t old_mask;
2025
2026 /* First of all, unlink existing socket */
2027 unlink (path);
2028
2029 /* Set umask */
2030 old_mask = umask (0077);
2031
2032 /* Make UNIX domain socket. */
2033 sock = socket (AF_UNIX, SOCK_STREAM, 0);
2034 if (sock < 0)
2035 {
2036 zlog_warn ("Can't create zserv unix socket: %s",
2037 safe_strerror (errno));
2038 zlog_warn ("zebra can't provide full functionality due to above error");
2039 return;
2040 }
2041
2042 /* Make server socket. */
2043 memset (&serv, 0, sizeof (struct sockaddr_un));
2044 serv.sun_family = AF_UNIX;
2045 strncpy (serv.sun_path, path, strlen (path));
2046 #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
2047 len = serv.sun_len = SUN_LEN(&serv);
2048 #else
2049 len = sizeof (serv.sun_family) + strlen (serv.sun_path);
2050 #endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
2051
2052 ret = bind (sock, (struct sockaddr *) &serv, len);
2053 if (ret < 0)
2054 {
2055 zlog_warn ("Can't bind to unix socket %s: %s",
2056 path, safe_strerror (errno));
2057 zlog_warn ("zebra can't provide full functionality due to above error");
2058 close (sock);
2059 return;
2060 }
2061
2062 ret = listen (sock, 5);
2063 if (ret < 0)
2064 {
2065 zlog_warn ("Can't listen to unix socket %s: %s",
2066 path, safe_strerror (errno));
2067 zlog_warn ("zebra can't provide full functionality due to above error");
2068 close (sock);
2069 return;
2070 }
2071
2072 umask (old_mask);
2073
2074 zebra_event (ZEBRA_SERV, sock, NULL);
2075 }
2076
2077
2078 static void
2079 zebra_event (enum event event, int sock, struct zserv *client)
2080 {
2081 switch (event)
2082 {
2083 case ZEBRA_SERV:
2084 thread_add_read (zebrad.master, zebra_accept, client, sock);
2085 break;
2086 case ZEBRA_READ:
2087 client->t_read =
2088 thread_add_read (zebrad.master, zebra_client_read, client, sock);
2089 break;
2090 case ZEBRA_WRITE:
2091 /**/
2092 break;
2093 }
2094 }
2095
2096 #define ZEBRA_TIME_BUF 32
2097 static char *
2098 zserv_time_buf(time_t *time1, char *buf, int buflen)
2099 {
2100 struct tm *tm;
2101 time_t now;
2102
2103 assert (buf != NULL);
2104 assert (buflen >= ZEBRA_TIME_BUF);
2105 assert (time1 != NULL);
2106
2107 if (!*time1)
2108 {
2109 snprintf(buf, buflen, "never ");
2110 return (buf);
2111 }
2112
2113 now = quagga_time(NULL);
2114 now -= *time1;
2115 tm = gmtime(&now);
2116
2117 /* Making formatted timer strings. */
2118 #define ONE_DAY_SECOND 60*60*24
2119 #define ONE_WEEK_SECOND 60*60*24*7
2120
2121 if (now < ONE_DAY_SECOND)
2122 snprintf (buf, buflen, "%02d:%02d:%02d",
2123 tm->tm_hour, tm->tm_min, tm->tm_sec);
2124 else if (now < ONE_WEEK_SECOND)
2125 snprintf (buf, buflen, "%dd%02dh%02dm",
2126 tm->tm_yday, tm->tm_hour, tm->tm_min);
2127 else
2128 snprintf (buf, buflen, "%02dw%dd%02dh",
2129 tm->tm_yday/7, tm->tm_yday - ((tm->tm_yday/7) * 7), tm->tm_hour);
2130 return buf;
2131 }
2132
2133 static void
2134 zebra_show_client_detail (struct vty *vty, struct zserv *client)
2135 {
2136 char cbuf[ZEBRA_TIME_BUF], rbuf[ZEBRA_TIME_BUF];
2137 char wbuf[ZEBRA_TIME_BUF], nhbuf[ZEBRA_TIME_BUF], mbuf[ZEBRA_TIME_BUF];
2138
2139 vty_out (vty, "Client: %s", zebra_route_string(client->proto));
2140 if (client->instance)
2141 vty_out (vty, " Instance: %d", client->instance);
2142 vty_out (vty, "%s", VTY_NEWLINE);
2143
2144 vty_out (vty, "------------------------ %s", VTY_NEWLINE);
2145 vty_out (vty, "FD: %d %s", client->sock, VTY_NEWLINE);
2146 vty_out (vty, "Route Table ID: %d %s", client->rtm_table, VTY_NEWLINE);
2147
2148 vty_out (vty, "Connect Time: %s %s",
2149 zserv_time_buf(&client->connect_time, cbuf, ZEBRA_TIME_BUF),
2150 VTY_NEWLINE);
2151 if (client->nh_reg_time)
2152 {
2153 vty_out (vty, "Nexthop Registry Time: %s %s",
2154 zserv_time_buf(&client->nh_reg_time, nhbuf, ZEBRA_TIME_BUF),
2155 VTY_NEWLINE);
2156 if (client->nh_last_upd_time)
2157 vty_out (vty, "Nexthop Last Update Time: %s %s",
2158 zserv_time_buf(&client->nh_last_upd_time, mbuf, ZEBRA_TIME_BUF),
2159 VTY_NEWLINE);
2160 else
2161 vty_out (vty, "No Nexthop Update sent%s", VTY_NEWLINE);
2162 }
2163 else
2164 vty_out (vty, "Not registered for Nexthop Updates%s", VTY_NEWLINE);
2165
2166 vty_out (vty, "Last Msg Rx Time: %s %s",
2167 zserv_time_buf(&client->last_read_time, rbuf, ZEBRA_TIME_BUF),
2168 VTY_NEWLINE);
2169 vty_out (vty, "Last Msg Tx Time: %s %s",
2170 zserv_time_buf(&client->last_write_time, wbuf, ZEBRA_TIME_BUF),
2171 VTY_NEWLINE);
2172 if (client->last_read_time)
2173 vty_out (vty, "Last Rcvd Cmd: %s %s",
2174 zserv_command_string(client->last_read_cmd), VTY_NEWLINE);
2175 if (client->last_write_time)
2176 vty_out (vty, "Last Sent Cmd: %s %s",
2177 zserv_command_string(client->last_write_cmd), VTY_NEWLINE);
2178 vty_out (vty, "%s", VTY_NEWLINE);
2179
2180 vty_out (vty, "Type Add Update Del %s", VTY_NEWLINE);
2181 vty_out (vty, "================================================== %s", VTY_NEWLINE);
2182 vty_out (vty, "IPv4 %-12d%-12d%-12d%s", client->v4_route_add_cnt,
2183 client->v4_route_upd8_cnt, client->v4_route_del_cnt, VTY_NEWLINE);
2184 vty_out (vty, "IPv6 %-12d%-12d%-12d%s", client->v6_route_add_cnt,
2185 client->v6_route_upd8_cnt, client->v6_route_del_cnt, VTY_NEWLINE);
2186 vty_out (vty, "Redist:v4 %-12d%-12d%-12d%s", client->redist_v4_add_cnt, 0,
2187 client->redist_v4_del_cnt, VTY_NEWLINE);
2188 vty_out (vty, "Redist:v6 %-12d%-12d%-12d%s", client->redist_v6_add_cnt, 0,
2189 client->redist_v6_del_cnt, VTY_NEWLINE);
2190 vty_out (vty, "Connected %-12d%-12d%-12d%s", client->ifadd_cnt, 0,
2191 client->ifdel_cnt, VTY_NEWLINE);
2192 vty_out (vty, "Interface Up Notifications: %d%s", client->ifup_cnt,
2193 VTY_NEWLINE);
2194 vty_out (vty, "Interface Down Notifications: %d%s", client->ifdown_cnt,
2195 VTY_NEWLINE);
2196
2197 vty_out (vty, "%s", VTY_NEWLINE);
2198 return;
2199 }
2200
2201 static void
2202 zebra_show_client_brief (struct vty *vty, struct zserv *client)
2203 {
2204 char cbuf[ZEBRA_TIME_BUF], rbuf[ZEBRA_TIME_BUF];
2205 char wbuf[ZEBRA_TIME_BUF];
2206
2207 vty_out (vty, "%-8s%12s %12s%12s%8d/%-8d%8d/%-8d%s",
2208 zebra_route_string(client->proto),
2209 zserv_time_buf(&client->connect_time, cbuf, ZEBRA_TIME_BUF),
2210 zserv_time_buf(&client->last_read_time, rbuf, ZEBRA_TIME_BUF),
2211 zserv_time_buf(&client->last_write_time, wbuf, ZEBRA_TIME_BUF),
2212 client->v4_route_add_cnt+client->v4_route_upd8_cnt,
2213 client->v4_route_del_cnt,
2214 client->v6_route_add_cnt+client->v6_route_upd8_cnt,
2215 client->v6_route_del_cnt, VTY_NEWLINE);
2216
2217 }
2218
2219
2220 /* Display default rtm_table for all clients. */
2221 DEFUN (show_table,
2222 show_table_cmd,
2223 "show table",
2224 SHOW_STR
2225 "default routing table to use for all clients\n")
2226 {
2227 vty_out (vty, "table %d%s", zebrad.rtm_table_default,
2228 VTY_NEWLINE);
2229 return CMD_SUCCESS;
2230 }
2231
2232 DEFUN (config_table,
2233 config_table_cmd,
2234 "table TABLENO",
2235 "Configure target kernel routing table\n"
2236 "TABLE integer\n")
2237 {
2238 zebrad.rtm_table_default = strtol (argv[0], (char**)0, 10);
2239 return CMD_SUCCESS;
2240 }
2241
2242 DEFUN (ip_forwarding,
2243 ip_forwarding_cmd,
2244 "ip forwarding",
2245 IP_STR
2246 "Turn on IP forwarding")
2247 {
2248 int ret;
2249
2250 ret = ipforward ();
2251 if (ret == 0)
2252 ret = ipforward_on ();
2253
2254 if (ret == 0)
2255 {
2256 vty_out (vty, "Can't turn on IP forwarding%s", VTY_NEWLINE);
2257 return CMD_WARNING;
2258 }
2259
2260 return CMD_SUCCESS;
2261 }
2262
2263 DEFUN (no_ip_forwarding,
2264 no_ip_forwarding_cmd,
2265 "no ip forwarding",
2266 NO_STR
2267 IP_STR
2268 "Turn off IP forwarding")
2269 {
2270 int ret;
2271
2272 ret = ipforward ();
2273 if (ret != 0)
2274 ret = ipforward_off ();
2275
2276 if (ret != 0)
2277 {
2278 vty_out (vty, "Can't turn off IP forwarding%s", VTY_NEWLINE);
2279 return CMD_WARNING;
2280 }
2281
2282 return CMD_SUCCESS;
2283 }
2284
2285 /* This command is for debugging purpose. */
2286 DEFUN (show_zebra_client,
2287 show_zebra_client_cmd,
2288 "show zebra client",
2289 SHOW_STR
2290 "Zebra information"
2291 "Client information")
2292 {
2293 struct listnode *node;
2294 struct zserv *client;
2295
2296 for (ALL_LIST_ELEMENTS_RO (zebrad.client_list, node, client))
2297 zebra_show_client_detail(vty, client);
2298
2299 return CMD_SUCCESS;
2300 }
2301
2302 /* This command is for debugging purpose. */
2303 DEFUN (show_zebra_client_summary,
2304 show_zebra_client_summary_cmd,
2305 "show zebra client summary",
2306 SHOW_STR
2307 "Zebra information brief"
2308 "Client information brief")
2309 {
2310 struct listnode *node;
2311 struct zserv *client;
2312
2313 vty_out (vty, "Name Connect Time Last Read Last Write IPv4 Routes IPv6 Routes %s",
2314 VTY_NEWLINE);
2315 vty_out (vty,"--------------------------------------------------------------------------------%s",
2316 VTY_NEWLINE);
2317
2318 for (ALL_LIST_ELEMENTS_RO (zebrad.client_list, node, client))
2319 zebra_show_client_brief(vty, client);
2320
2321 vty_out (vty, "Routes column shows (added+updated)/deleted%s", VTY_NEWLINE);
2322 return CMD_SUCCESS;
2323 }
2324
2325 /* Table configuration write function. */
2326 static int
2327 config_write_table (struct vty *vty)
2328 {
2329 if (zebrad.rtm_table_default)
2330 vty_out (vty, "table %d%s", zebrad.rtm_table_default,
2331 VTY_NEWLINE);
2332 return 0;
2333 }
2334
2335 /* table node for routing tables. */
2336 static struct cmd_node table_node =
2337 {
2338 TABLE_NODE,
2339 "", /* This node has no interface. */
2340 1
2341 };
2342
2343 /* Only display ip forwarding is enabled or not. */
2344 DEFUN (show_ip_forwarding,
2345 show_ip_forwarding_cmd,
2346 "show ip forwarding",
2347 SHOW_STR
2348 IP_STR
2349 "IP forwarding status\n")
2350 {
2351 int ret;
2352
2353 ret = ipforward ();
2354
2355 if (ret == 0)
2356 vty_out (vty, "IP forwarding is off%s", VTY_NEWLINE);
2357 else
2358 vty_out (vty, "IP forwarding is on%s", VTY_NEWLINE);
2359 return CMD_SUCCESS;
2360 }
2361
2362 #ifdef HAVE_IPV6
2363 /* Only display ipv6 forwarding is enabled or not. */
2364 DEFUN (show_ipv6_forwarding,
2365 show_ipv6_forwarding_cmd,
2366 "show ipv6 forwarding",
2367 SHOW_STR
2368 "IPv6 information\n"
2369 "Forwarding status\n")
2370 {
2371 int ret;
2372
2373 ret = ipforward_ipv6 ();
2374
2375 switch (ret)
2376 {
2377 case -1:
2378 vty_out (vty, "ipv6 forwarding is unknown%s", VTY_NEWLINE);
2379 break;
2380 case 0:
2381 vty_out (vty, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE);
2382 break;
2383 case 1:
2384 vty_out (vty, "ipv6 forwarding is %s%s", "on", VTY_NEWLINE);
2385 break;
2386 default:
2387 vty_out (vty, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE);
2388 break;
2389 }
2390 return CMD_SUCCESS;
2391 }
2392
2393 DEFUN (ipv6_forwarding,
2394 ipv6_forwarding_cmd,
2395 "ipv6 forwarding",
2396 IPV6_STR
2397 "Turn on IPv6 forwarding")
2398 {
2399 int ret;
2400
2401 ret = ipforward_ipv6 ();
2402 if (ret == 0)
2403 ret = ipforward_ipv6_on ();
2404
2405 if (ret == 0)
2406 {
2407 vty_out (vty, "Can't turn on IPv6 forwarding%s", VTY_NEWLINE);
2408 return CMD_WARNING;
2409 }
2410
2411 return CMD_SUCCESS;
2412 }
2413
2414 DEFUN (no_ipv6_forwarding,
2415 no_ipv6_forwarding_cmd,
2416 "no ipv6 forwarding",
2417 NO_STR
2418 IPV6_STR
2419 "Turn off IPv6 forwarding")
2420 {
2421 int ret;
2422
2423 ret = ipforward_ipv6 ();
2424 if (ret != 0)
2425 ret = ipforward_ipv6_off ();
2426
2427 if (ret != 0)
2428 {
2429 vty_out (vty, "Can't turn off IPv6 forwarding%s", VTY_NEWLINE);
2430 return CMD_WARNING;
2431 }
2432
2433 return CMD_SUCCESS;
2434 }
2435
2436 #endif /* HAVE_IPV6 */
2437
2438 /* IPForwarding configuration write function. */
2439 static int
2440 config_write_forwarding (struct vty *vty)
2441 {
2442 /* FIXME: Find better place for that. */
2443 router_id_write (vty);
2444
2445 if (ipforward ())
2446 vty_out (vty, "ip forwarding%s", VTY_NEWLINE);
2447 #ifdef HAVE_IPV6
2448 if (ipforward_ipv6 ())
2449 vty_out (vty, "ipv6 forwarding%s", VTY_NEWLINE);
2450 #endif /* HAVE_IPV6 */
2451 vty_out (vty, "!%s", VTY_NEWLINE);
2452 return 0;
2453 }
2454
2455 /* table node for routing tables. */
2456 static struct cmd_node forwarding_node =
2457 {
2458 FORWARDING_NODE,
2459 "", /* This node has no interface. */
2460 1
2461 };
2462
2463
2464 /* Initialisation of zebra and installation of commands. */
2465 void
2466 zebra_init (void)
2467 {
2468 /* Client list init. */
2469 zebrad.client_list = list_new ();
2470
2471 /* Install configuration write function. */
2472 install_node (&table_node, config_write_table);
2473 install_node (&forwarding_node, config_write_forwarding);
2474
2475 install_element (VIEW_NODE, &show_ip_forwarding_cmd);
2476 install_element (ENABLE_NODE, &show_ip_forwarding_cmd);
2477 install_element (CONFIG_NODE, &ip_forwarding_cmd);
2478 install_element (CONFIG_NODE, &no_ip_forwarding_cmd);
2479 install_element (ENABLE_NODE, &show_zebra_client_cmd);
2480 install_element (ENABLE_NODE, &show_zebra_client_summary_cmd);
2481
2482 #ifdef HAVE_NETLINK
2483 install_element (VIEW_NODE, &show_table_cmd);
2484 install_element (ENABLE_NODE, &show_table_cmd);
2485 install_element (CONFIG_NODE, &config_table_cmd);
2486 #endif /* HAVE_NETLINK */
2487
2488 #ifdef HAVE_IPV6
2489 install_element (VIEW_NODE, &show_ipv6_forwarding_cmd);
2490 install_element (ENABLE_NODE, &show_ipv6_forwarding_cmd);
2491 install_element (CONFIG_NODE, &ipv6_forwarding_cmd);
2492 install_element (CONFIG_NODE, &no_ipv6_forwarding_cmd);
2493 #endif /* HAVE_IPV6 */
2494
2495 /* Route-map */
2496 zebra_route_map_init ();
2497 }
2498
2499 /* Make zebra server socket, wiping any existing one (see bug #403). */
2500 void
2501 zebra_zserv_socket_init (char *path)
2502 {
2503 #ifdef HAVE_TCP_ZEBRA
2504 zebra_serv ();
2505 #else
2506 zebra_serv_un (path ? path : ZEBRA_SERV_PATH);
2507 #endif /* HAVE_TCP_ZEBRA */
2508 }