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