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