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