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