]> git.proxmox.com Git - mirror_frr.git/blob - zebra/zserv.c
Merge pull request #2086 from donaldsharp/zebra_capabilities
[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 /* for basename */
24 #include <libgen.h>
25
26 #include "prefix.h"
27 #include "command.h"
28 #include "if.h"
29 #include "thread.h"
30 #include "stream.h"
31 #include "memory.h"
32 #include "zebra_memory.h"
33 #include "table.h"
34 #include "rib.h"
35 #include "network.h"
36 #include "sockunion.h"
37 #include "log.h"
38 #include "zclient.h"
39 #include "privs.h"
40 #include "network.h"
41 #include "buffer.h"
42 #include "nexthop.h"
43 #include "vrf.h"
44 #include "libfrr.h"
45 #include "sockopt.h"
46
47 #include "zebra/zserv.h"
48 #include "zebra/zebra_ns.h"
49 #include "zebra/zebra_vrf.h"
50 #include "zebra/router-id.h"
51 #include "zebra/redistribute.h"
52 #include "zebra/debug.h"
53 #include "zebra/zebra_rnh.h"
54 #include "zebra/rt_netlink.h"
55 #include "zebra/interface.h"
56 #include "zebra/zebra_ptm.h"
57 #include "zebra/rtadv.h"
58 #include "zebra/zebra_mpls.h"
59 #include "zebra/zebra_mroute.h"
60 #include "zebra/label_manager.h"
61 #include "zebra/zebra_vxlan.h"
62 #include "zebra/rt.h"
63 #include "zebra/zebra_pbr.h"
64 #include "zebra/table_manager.h"
65
66 /* Event list of zebra. */
67 enum event { ZEBRA_READ, ZEBRA_WRITE };
68 /* privileges */
69 extern struct zebra_privs_t zserv_privs;
70 /* post event into client */
71 static void zebra_event(struct zserv *client, enum event event);
72
73
74 /* Public interface ======================================================== */
75
76 int zebra_server_send_message(struct zserv *client, struct stream *msg)
77 {
78 stream_fifo_push(client->obuf_fifo, msg);
79 zebra_event(client, ZEBRA_WRITE);
80 return 0;
81 }
82
83 /* Encoding helpers -------------------------------------------------------- */
84
85 static void zserv_encode_interface(struct stream *s, struct interface *ifp)
86 {
87 /* Interface information. */
88 stream_put(s, ifp->name, INTERFACE_NAMSIZ);
89 stream_putl(s, ifp->ifindex);
90 stream_putc(s, ifp->status);
91 stream_putq(s, ifp->flags);
92 stream_putc(s, ifp->ptm_enable);
93 stream_putc(s, ifp->ptm_status);
94 stream_putl(s, ifp->metric);
95 stream_putl(s, ifp->speed);
96 stream_putl(s, ifp->mtu);
97 stream_putl(s, ifp->mtu6);
98 stream_putl(s, ifp->bandwidth);
99 stream_putl(s, ifp->ll_type);
100 stream_putl(s, ifp->hw_addr_len);
101 if (ifp->hw_addr_len)
102 stream_put(s, ifp->hw_addr, ifp->hw_addr_len);
103
104 /* Then, Traffic Engineering parameters if any */
105 if (HAS_LINK_PARAMS(ifp) && IS_LINK_PARAMS_SET(ifp->link_params)) {
106 stream_putc(s, 1);
107 zebra_interface_link_params_write(s, ifp);
108 } else
109 stream_putc(s, 0);
110
111 /* Write packet size. */
112 stream_putw_at(s, 0, stream_get_endp(s));
113 }
114
115 static void zserv_encode_vrf(struct stream *s, struct zebra_vrf *zvrf)
116 {
117 struct vrf_data data;
118 const char *netns_name = zvrf_ns_name(zvrf);
119
120 data.l.table_id = zvrf->table_id;
121
122 if (netns_name)
123 strlcpy(data.l.netns_name, basename((char *)netns_name),
124 NS_NAMSIZ);
125 else
126 memset(data.l.netns_name, 0, NS_NAMSIZ);
127 /* Pass the tableid and the netns NAME */
128 stream_put(s, &data, sizeof(struct vrf_data));
129 /* Interface information. */
130 stream_put(s, zvrf_name(zvrf), VRF_NAMSIZ);
131 /* Write packet size. */
132 stream_putw_at(s, 0, stream_get_endp(s));
133 }
134
135 static int zserv_encode_nexthop(struct stream *s, struct nexthop *nexthop)
136 {
137 stream_putc(s, nexthop->type);
138 switch (nexthop->type) {
139 case NEXTHOP_TYPE_IPV4:
140 case NEXTHOP_TYPE_IPV4_IFINDEX:
141 stream_put_in_addr(s, &nexthop->gate.ipv4);
142 stream_putl(s, nexthop->ifindex);
143 break;
144 case NEXTHOP_TYPE_IPV6:
145 stream_put(s, &nexthop->gate.ipv6, 16);
146 break;
147 case NEXTHOP_TYPE_IPV6_IFINDEX:
148 stream_put(s, &nexthop->gate.ipv6, 16);
149 stream_putl(s, nexthop->ifindex);
150 break;
151 case NEXTHOP_TYPE_IFINDEX:
152 stream_putl(s, nexthop->ifindex);
153 break;
154 default:
155 /* do nothing */
156 break;
157 }
158 return 1;
159 }
160
161 /* Send handlers ----------------------------------------------------------- */
162
163 /* Interface is added. Send ZEBRA_INTERFACE_ADD to client. */
164 /*
165 * This function is called in the following situations:
166 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
167 * from the client.
168 * - at startup, when zebra figures out the available interfaces
169 * - when an interface is added (where support for
170 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
171 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
172 * received)
173 */
174 int zsend_interface_add(struct zserv *client, struct interface *ifp)
175 {
176 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
177
178 zclient_create_header(s, ZEBRA_INTERFACE_ADD, ifp->vrf_id);
179 zserv_encode_interface(s, ifp);
180
181 client->ifadd_cnt++;
182 return zebra_server_send_message(client, s);
183 }
184
185 /* Interface deletion from zebra daemon. */
186 int zsend_interface_delete(struct zserv *client, struct interface *ifp)
187 {
188 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
189
190 zclient_create_header(s, ZEBRA_INTERFACE_DELETE, ifp->vrf_id);
191 zserv_encode_interface(s, ifp);
192
193 client->ifdel_cnt++;
194 return zebra_server_send_message(client, s);
195 }
196
197 int zsend_vrf_add(struct zserv *client, struct zebra_vrf *zvrf)
198 {
199 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
200
201 zclient_create_header(s, ZEBRA_VRF_ADD, zvrf_id(zvrf));
202 zserv_encode_vrf(s, zvrf);
203
204 client->vrfadd_cnt++;
205 return zebra_server_send_message(client, s);
206 }
207
208 /* VRF deletion from zebra daemon. */
209 int zsend_vrf_delete(struct zserv *client, struct zebra_vrf *zvrf)
210
211 {
212 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
213
214 zclient_create_header(s, ZEBRA_VRF_DELETE, zvrf_id(zvrf));
215 zserv_encode_vrf(s, zvrf);
216
217 client->vrfdel_cnt++;
218 return zebra_server_send_message(client, s);
219 }
220
221 int zsend_interface_link_params(struct zserv *client, struct interface *ifp)
222 {
223 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
224
225 /* Check this client need interface information. */
226 if (!client->ifinfo) {
227 stream_free(s);
228 return 0;
229 }
230
231 if (!ifp->link_params) {
232 stream_free(s);
233 return 0;
234 }
235
236 zclient_create_header(s, ZEBRA_INTERFACE_LINK_PARAMS, ifp->vrf_id);
237
238 /* Add Interface Index */
239 stream_putl(s, ifp->ifindex);
240
241 /* Then TE Link Parameters */
242 if (zebra_interface_link_params_write(s, ifp) == 0) {
243 stream_free(s);
244 return 0;
245 }
246
247 /* Write packet size. */
248 stream_putw_at(s, 0, stream_get_endp(s));
249
250 return zebra_server_send_message(client, s);
251 }
252
253 /* Interface address is added/deleted. Send ZEBRA_INTERFACE_ADDRESS_ADD or
254 * ZEBRA_INTERFACE_ADDRESS_DELETE to the client.
255 *
256 * A ZEBRA_INTERFACE_ADDRESS_ADD is sent in the following situations:
257 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
258 * from the client, after the ZEBRA_INTERFACE_ADD has been
259 * sent from zebra to the client
260 * - redistribute new address info to all clients in the following situations
261 * - at startup, when zebra figures out the available interfaces
262 * - when an interface is added (where support for
263 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
264 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
265 * received)
266 * - for the vty commands "ip address A.B.C.D/M [<secondary>|<label LINE>]"
267 * and "no bandwidth <1-10000000>", "ipv6 address X:X::X:X/M"
268 * - when an RTM_NEWADDR message is received from the kernel,
269 *
270 * The call tree that triggers ZEBRA_INTERFACE_ADDRESS_DELETE:
271 *
272 * zsend_interface_address(DELETE)
273 * ^
274 * |
275 * zebra_interface_address_delete_update
276 * ^ ^ ^
277 * | | if_delete_update
278 * | |
279 * ip_address_uninstall connected_delete_ipv4
280 * [ipv6_addresss_uninstall] [connected_delete_ipv6]
281 * ^ ^
282 * | |
283 * | RTM_NEWADDR on routing/netlink socket
284 * |
285 * vty commands:
286 * "no ip address A.B.C.D/M [label LINE]"
287 * "no ip address A.B.C.D/M secondary"
288 * ["no ipv6 address X:X::X:X/M"]
289 *
290 */
291 int zsend_interface_address(int cmd, struct zserv *client,
292 struct interface *ifp, struct connected *ifc)
293 {
294 int blen;
295 struct prefix *p;
296 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
297
298 zclient_create_header(s, cmd, ifp->vrf_id);
299 stream_putl(s, ifp->ifindex);
300
301 /* Interface address flag. */
302 stream_putc(s, ifc->flags);
303
304 /* Prefix information. */
305 p = ifc->address;
306 stream_putc(s, p->family);
307 blen = prefix_blen(p);
308 stream_put(s, &p->u.prefix, blen);
309
310 /*
311 * XXX gnu version does not send prefixlen for
312 * ZEBRA_INTERFACE_ADDRESS_DELETE
313 * but zebra_interface_address_delete_read() in the gnu version
314 * expects to find it
315 */
316 stream_putc(s, p->prefixlen);
317
318 /* Destination. */
319 p = ifc->destination;
320 if (p)
321 stream_put(s, &p->u.prefix, blen);
322 else
323 stream_put(s, NULL, blen);
324
325 /* Write packet size. */
326 stream_putw_at(s, 0, stream_get_endp(s));
327
328 client->connected_rt_add_cnt++;
329 return zebra_server_send_message(client, s);
330 }
331
332 static int zsend_interface_nbr_address(int cmd, struct zserv *client,
333 struct interface *ifp,
334 struct nbr_connected *ifc)
335 {
336 int blen;
337 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
338 struct prefix *p;
339
340 zclient_create_header(s, cmd, ifp->vrf_id);
341 stream_putl(s, ifp->ifindex);
342
343 /* Prefix information. */
344 p = ifc->address;
345 stream_putc(s, p->family);
346 blen = prefix_blen(p);
347 stream_put(s, &p->u.prefix, blen);
348
349 /*
350 * XXX gnu version does not send prefixlen for
351 * ZEBRA_INTERFACE_ADDRESS_DELETE
352 * but zebra_interface_address_delete_read() in the gnu version
353 * expects to find it
354 */
355 stream_putc(s, p->prefixlen);
356
357 /* Write packet size. */
358 stream_putw_at(s, 0, stream_get_endp(s));
359
360 return zebra_server_send_message(client, s);
361 }
362
363 /* Interface address addition. */
364 static void zebra_interface_nbr_address_add_update(struct interface *ifp,
365 struct nbr_connected *ifc)
366 {
367 struct listnode *node, *nnode;
368 struct zserv *client;
369 struct prefix *p;
370
371 if (IS_ZEBRA_DEBUG_EVENT) {
372 char buf[INET6_ADDRSTRLEN];
373
374 p = ifc->address;
375 zlog_debug(
376 "MESSAGE: ZEBRA_INTERFACE_NBR_ADDRESS_ADD %s/%d on %s",
377 inet_ntop(p->family, &p->u.prefix, buf,
378 INET6_ADDRSTRLEN),
379 p->prefixlen, ifc->ifp->name);
380 }
381
382 for (ALL_LIST_ELEMENTS(zebrad.client_list, node, nnode, client))
383 zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_ADD,
384 client, ifp, ifc);
385 }
386
387 /* Interface address deletion. */
388 static void zebra_interface_nbr_address_delete_update(struct interface *ifp,
389 struct nbr_connected *ifc)
390 {
391 struct listnode *node, *nnode;
392 struct zserv *client;
393 struct prefix *p;
394
395 if (IS_ZEBRA_DEBUG_EVENT) {
396 char buf[INET6_ADDRSTRLEN];
397
398 p = ifc->address;
399 zlog_debug(
400 "MESSAGE: ZEBRA_INTERFACE_NBR_ADDRESS_DELETE %s/%d on %s",
401 inet_ntop(p->family, &p->u.prefix, buf,
402 INET6_ADDRSTRLEN),
403 p->prefixlen, ifc->ifp->name);
404 }
405
406 for (ALL_LIST_ELEMENTS(zebrad.client_list, node, nnode, client))
407 zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_DELETE,
408 client, ifp, ifc);
409 }
410
411 /* Send addresses on interface to client */
412 int zsend_interface_addresses(struct zserv *client, struct interface *ifp)
413 {
414 struct listnode *cnode, *cnnode;
415 struct connected *c;
416 struct nbr_connected *nc;
417
418 /* Send interface addresses. */
419 for (ALL_LIST_ELEMENTS(ifp->connected, cnode, cnnode, c)) {
420 if (!CHECK_FLAG(c->conf, ZEBRA_IFC_REAL))
421 continue;
422
423 if (zsend_interface_address(ZEBRA_INTERFACE_ADDRESS_ADD, client,
424 ifp, c)
425 < 0)
426 return -1;
427 }
428
429 /* Send interface neighbors. */
430 for (ALL_LIST_ELEMENTS(ifp->nbr_connected, cnode, cnnode, nc)) {
431 if (zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_ADD,
432 client, ifp, nc)
433 < 0)
434 return -1;
435 }
436
437 return 0;
438 }
439
440 /* Notify client about interface moving from one VRF to another.
441 * Whether client is interested in old and new VRF is checked by caller.
442 */
443 int zsend_interface_vrf_update(struct zserv *client, struct interface *ifp,
444 vrf_id_t vrf_id)
445 {
446 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
447
448 zclient_create_header(s, ZEBRA_INTERFACE_VRF_UPDATE, ifp->vrf_id);
449
450 /* Fill in the ifIndex of the interface and its new VRF (id) */
451 stream_putl(s, ifp->ifindex);
452 stream_putl(s, vrf_id);
453
454 /* Write packet size. */
455 stream_putw_at(s, 0, stream_get_endp(s));
456
457 client->if_vrfchg_cnt++;
458 return zebra_server_send_message(client, s);
459 }
460
461 /* Add new nbr connected IPv6 address */
462 void nbr_connected_add_ipv6(struct interface *ifp, struct in6_addr *address)
463 {
464 struct nbr_connected *ifc;
465 struct prefix p;
466
467 p.family = AF_INET6;
468 IPV6_ADDR_COPY(&p.u.prefix, address);
469 p.prefixlen = IPV6_MAX_PREFIXLEN;
470
471 if (!(ifc = listnode_head(ifp->nbr_connected))) {
472 /* new addition */
473 ifc = nbr_connected_new();
474 ifc->address = prefix_new();
475 ifc->ifp = ifp;
476 listnode_add(ifp->nbr_connected, ifc);
477 }
478
479 prefix_copy(ifc->address, &p);
480
481 zebra_interface_nbr_address_add_update(ifp, ifc);
482
483 if_nbr_ipv6ll_to_ipv4ll_neigh_update(ifp, address, 1);
484 }
485
486 void nbr_connected_delete_ipv6(struct interface *ifp, struct in6_addr *address)
487 {
488 struct nbr_connected *ifc;
489 struct prefix p;
490
491 p.family = AF_INET6;
492 IPV6_ADDR_COPY(&p.u.prefix, address);
493 p.prefixlen = IPV6_MAX_PREFIXLEN;
494
495 ifc = nbr_connected_check(ifp, &p);
496 if (!ifc)
497 return;
498
499 listnode_delete(ifp->nbr_connected, ifc);
500
501 zebra_interface_nbr_address_delete_update(ifp, ifc);
502
503 if_nbr_ipv6ll_to_ipv4ll_neigh_update(ifp, address, 0);
504
505 nbr_connected_free(ifc);
506 }
507
508 /*
509 * The cmd passed to zsend_interface_update may be ZEBRA_INTERFACE_UP or
510 * ZEBRA_INTERFACE_DOWN.
511 *
512 * The ZEBRA_INTERFACE_UP message is sent from the zebra server to
513 * the clients in one of 2 situations:
514 * - an if_up is detected e.g., as a result of an RTM_IFINFO message
515 * - a vty command modifying the bandwidth of an interface is received.
516 * The ZEBRA_INTERFACE_DOWN message is sent when an if_down is detected.
517 */
518 int zsend_interface_update(int cmd, struct zserv *client, struct interface *ifp)
519 {
520 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
521
522 zclient_create_header(s, cmd, ifp->vrf_id);
523 zserv_encode_interface(s, ifp);
524
525 if (cmd == ZEBRA_INTERFACE_UP)
526 client->ifup_cnt++;
527 else
528 client->ifdown_cnt++;
529
530 return zebra_server_send_message(client, s);
531 }
532
533 int zsend_redistribute_route(int cmd, struct zserv *client, struct prefix *p,
534 struct prefix *src_p, struct route_entry *re)
535 {
536 struct zapi_route api;
537 struct zapi_nexthop *api_nh;
538 struct nexthop *nexthop;
539 int count = 0;
540
541 memset(&api, 0, sizeof(api));
542 api.vrf_id = re->vrf_id;
543 api.type = re->type;
544 api.instance = re->instance;
545 api.flags = re->flags;
546
547 /* Prefix. */
548 api.prefix = *p;
549 if (src_p) {
550 SET_FLAG(api.message, ZAPI_MESSAGE_SRCPFX);
551 memcpy(&api.src_prefix, src_p, sizeof(api.src_prefix));
552 }
553
554 /* Nexthops. */
555 if (re->nexthop_active_num) {
556 SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP);
557 api.nexthop_num = re->nexthop_active_num;
558 }
559 for (nexthop = re->ng.nexthop; nexthop; nexthop = nexthop->next) {
560 if (!CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE))
561 continue;
562
563 api_nh = &api.nexthops[count];
564 api_nh->vrf_id = nexthop->vrf_id;
565 api_nh->type = nexthop->type;
566 switch (nexthop->type) {
567 case NEXTHOP_TYPE_BLACKHOLE:
568 api_nh->bh_type = nexthop->bh_type;
569 break;
570 case NEXTHOP_TYPE_IPV4:
571 api_nh->gate.ipv4 = nexthop->gate.ipv4;
572 break;
573 case NEXTHOP_TYPE_IPV4_IFINDEX:
574 api_nh->gate.ipv4 = nexthop->gate.ipv4;
575 api_nh->ifindex = nexthop->ifindex;
576 break;
577 case NEXTHOP_TYPE_IFINDEX:
578 api_nh->ifindex = nexthop->ifindex;
579 break;
580 case NEXTHOP_TYPE_IPV6:
581 api_nh->gate.ipv6 = nexthop->gate.ipv6;
582 break;
583 case NEXTHOP_TYPE_IPV6_IFINDEX:
584 api_nh->gate.ipv6 = nexthop->gate.ipv6;
585 api_nh->ifindex = nexthop->ifindex;
586 }
587 count++;
588 }
589
590 /* Attributes. */
591 SET_FLAG(api.message, ZAPI_MESSAGE_DISTANCE);
592 api.distance = re->distance;
593 SET_FLAG(api.message, ZAPI_MESSAGE_METRIC);
594 api.metric = re->metric;
595 if (re->tag) {
596 SET_FLAG(api.message, ZAPI_MESSAGE_TAG);
597 api.tag = re->tag;
598 }
599 SET_FLAG(api.message, ZAPI_MESSAGE_MTU);
600 api.mtu = re->mtu;
601
602 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
603
604 /* Encode route and send. */
605 if (zapi_route_encode(cmd, s, &api) < 0) {
606 stream_free(s);
607 return -1;
608 }
609
610 if (IS_ZEBRA_DEBUG_SEND) {
611 char buf_prefix[PREFIX_STRLEN];
612 prefix2str(&api.prefix, buf_prefix, sizeof(buf_prefix));
613
614 zlog_debug("%s: %s to client %s: type %s, vrf_id %d, p %s",
615 __func__, zserv_command_string(cmd),
616 zebra_route_string(client->proto),
617 zebra_route_string(api.type), api.vrf_id,
618 buf_prefix);
619 }
620 return zebra_server_send_message(client, s);
621 }
622
623 /*
624 * Modified version of zsend_ipv4_nexthop_lookup(): Query unicast rib if
625 * nexthop is not found on mrib. Returns both route metric and protocol
626 * distance.
627 */
628 static int zsend_ipv4_nexthop_lookup_mrib(struct zserv *client,
629 struct in_addr addr,
630 struct route_entry *re,
631 struct zebra_vrf *zvrf)
632 {
633 struct stream *s;
634 unsigned long nump;
635 uint8_t num;
636 struct nexthop *nexthop;
637
638 /* Get output stream. */
639 s = stream_new(ZEBRA_MAX_PACKET_SIZ);
640 stream_reset(s);
641
642 /* Fill in result. */
643 zclient_create_header(s, ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB, zvrf_id(zvrf));
644 stream_put_in_addr(s, &addr);
645
646 if (re) {
647 stream_putc(s, re->distance);
648 stream_putl(s, re->metric);
649 num = 0;
650 nump = stream_get_endp(
651 s); /* remember position for nexthop_num */
652 stream_putc(s, 0); /* reserve room for nexthop_num */
653 /* Only non-recursive routes are elegible to resolve the nexthop
654 * we
655 * are looking up. Therefore, we will just iterate over the top
656 * chain of nexthops. */
657 for (nexthop = re->ng.nexthop; nexthop; nexthop = nexthop->next)
658 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE))
659 num += zserv_encode_nexthop(s, nexthop);
660
661 stream_putc_at(s, nump, num); /* store nexthop_num */
662 } else {
663 stream_putc(s, 0); /* distance */
664 stream_putl(s, 0); /* metric */
665 stream_putc(s, 0); /* nexthop_num */
666 }
667
668 stream_putw_at(s, 0, stream_get_endp(s));
669
670 return zebra_server_send_message(client, s);
671 }
672
673 int zsend_route_notify_owner(struct route_entry *re, struct prefix *p,
674 enum zapi_route_notify_owner note)
675 {
676 struct zserv *client;
677 struct stream *s;
678 uint8_t blen;
679
680 client = zebra_find_client(re->type, re->instance);
681 if (!client || !client->notify_owner) {
682 if (IS_ZEBRA_DEBUG_PACKET) {
683 char buff[PREFIX_STRLEN];
684
685 zlog_debug(
686 "Not Notifying Owner: %u about prefix %s(%u) %d vrf: %u",
687 re->type, prefix2str(p, buff, sizeof(buff)),
688 re->table, note, re->vrf_id);
689 }
690 return 0;
691 }
692
693 if (IS_ZEBRA_DEBUG_PACKET) {
694 char buff[PREFIX_STRLEN];
695
696 zlog_debug("Notifying Owner: %u about prefix %s(%u) %d vrf: %u",
697 re->type, prefix2str(p, buff, sizeof(buff)),
698 re->table, note, re->vrf_id);
699 }
700
701 s = stream_new(ZEBRA_MAX_PACKET_SIZ);
702 stream_reset(s);
703
704 zclient_create_header(s, ZEBRA_ROUTE_NOTIFY_OWNER, re->vrf_id);
705
706 stream_put(s, &note, sizeof(note));
707
708 stream_putc(s, p->family);
709
710 blen = prefix_blen(p);
711 stream_putc(s, p->prefixlen);
712 stream_put(s, &p->u.prefix, blen);
713
714 stream_putl(s, re->table);
715
716 stream_putw_at(s, 0, stream_get_endp(s));
717
718 return zebra_server_send_message(client, s);
719 }
720
721 void zsend_rule_notify_owner(struct zebra_pbr_rule *rule,
722 enum zapi_rule_notify_owner note)
723 {
724 struct listnode *node;
725 struct zserv *client;
726 struct stream *s;
727
728 if (IS_ZEBRA_DEBUG_PACKET)
729 zlog_debug("%s: Notifying %u", __PRETTY_FUNCTION__,
730 rule->rule.unique);
731
732 for (ALL_LIST_ELEMENTS_RO(zebrad.client_list, node, client)) {
733 if (rule->sock == client->sock)
734 break;
735 }
736
737 if (!client)
738 return;
739
740 s = stream_new(ZEBRA_MAX_PACKET_SIZ);
741
742 zclient_create_header(s, ZEBRA_RULE_NOTIFY_OWNER, VRF_DEFAULT);
743 stream_put(s, &note, sizeof(note));
744 stream_putl(s, rule->rule.seq);
745 stream_putl(s, rule->rule.priority);
746 stream_putl(s, rule->rule.unique);
747 if (rule->ifp)
748 stream_putl(s, rule->ifp->ifindex);
749 else
750 stream_putl(s, 0);
751
752 stream_putw_at(s, 0, stream_get_endp(s));
753
754 zebra_server_send_message(client, s);
755 }
756
757 void zsend_ipset_notify_owner(struct zebra_pbr_ipset *ipset,
758 enum zapi_ipset_notify_owner note)
759 {
760 struct listnode *node;
761 struct zserv *client;
762 struct stream *s;
763
764 if (IS_ZEBRA_DEBUG_PACKET)
765 zlog_debug("%s: Notifying %u", __PRETTY_FUNCTION__,
766 ipset->unique);
767
768 for (ALL_LIST_ELEMENTS_RO(zebrad.client_list, node, client)) {
769 if (ipset->sock == client->sock)
770 break;
771 }
772
773 if (!client)
774 return;
775
776 s = stream_new(ZEBRA_MAX_PACKET_SIZ);
777
778 zclient_create_header(s, ZEBRA_IPSET_NOTIFY_OWNER, VRF_DEFAULT);
779 stream_put(s, &note, sizeof(note));
780 stream_putl(s, ipset->unique);
781 stream_put(s, ipset->ipset_name, ZEBRA_IPSET_NAME_SIZE);
782 stream_putw_at(s, 0, stream_get_endp(s));
783
784 zebra_server_send_message(client, s);
785 }
786
787 void zsend_ipset_entry_notify_owner(
788 struct zebra_pbr_ipset_entry *ipset,
789 enum zapi_ipset_entry_notify_owner note)
790 {
791 struct listnode *node;
792 struct zserv *client;
793 struct stream *s;
794
795 if (IS_ZEBRA_DEBUG_PACKET)
796 zlog_debug("%s: Notifying %u", __PRETTY_FUNCTION__,
797 ipset->unique);
798
799 for (ALL_LIST_ELEMENTS_RO(zebrad.client_list, node, client)) {
800 if (ipset->sock == client->sock)
801 break;
802 }
803
804 if (!client)
805 return;
806
807 s = stream_new(ZEBRA_MAX_PACKET_SIZ);
808
809 zclient_create_header(s, ZEBRA_IPSET_ENTRY_NOTIFY_OWNER,
810 VRF_DEFAULT);
811 stream_put(s, &note, sizeof(note));
812 stream_putl(s, ipset->unique);
813 stream_put(s, ipset->backpointer->ipset_name,
814 ZEBRA_IPSET_NAME_SIZE);
815 stream_putw_at(s, 0, stream_get_endp(s));
816
817 zebra_server_send_message(client, s);
818 }
819
820 void zsend_iptable_notify_owner(struct zebra_pbr_iptable *iptable,
821 enum zapi_iptable_notify_owner note)
822 {
823 struct listnode *node;
824 struct zserv *client;
825 struct stream *s;
826
827 if (IS_ZEBRA_DEBUG_PACKET)
828 zlog_debug("%s: Notifying %u", __PRETTY_FUNCTION__,
829 iptable->unique);
830
831 for (ALL_LIST_ELEMENTS_RO(zebrad.client_list, node, client)) {
832 if (iptable->sock == client->sock)
833 break;
834 }
835
836 if (!client)
837 return;
838
839 s = stream_new(ZEBRA_MAX_PACKET_SIZ);
840
841 zclient_create_header(s, ZEBRA_IPTABLE_NOTIFY_OWNER, VRF_DEFAULT);
842 stream_put(s, &note, sizeof(note));
843 stream_putl(s, iptable->unique);
844 stream_putw_at(s, 0, stream_get_endp(s));
845
846 zebra_server_send_message(client, s);
847 }
848
849 /* Router-id is updated. Send ZEBRA_ROUTER_ID_ADD to client. */
850 int zsend_router_id_update(struct zserv *client, struct prefix *p,
851 vrf_id_t vrf_id)
852 {
853 int blen;
854
855 /* Check this client need interface information. */
856 if (!vrf_bitmap_check(client->ridinfo, vrf_id))
857 return 0;
858
859 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
860
861 /* Message type. */
862 zclient_create_header(s, ZEBRA_ROUTER_ID_UPDATE, vrf_id);
863
864 /* Prefix information. */
865 stream_putc(s, p->family);
866 blen = prefix_blen(p);
867 stream_put(s, &p->u.prefix, blen);
868 stream_putc(s, p->prefixlen);
869
870 /* Write packet size. */
871 stream_putw_at(s, 0, stream_get_endp(s));
872
873 return zebra_server_send_message(client, s);
874 }
875
876 /*
877 * Function used by Zebra to send a PW status update to LDP daemon
878 */
879 int zsend_pw_update(struct zserv *client, struct zebra_pw *pw)
880 {
881 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
882
883 zclient_create_header(s, ZEBRA_PW_STATUS_UPDATE, pw->vrf_id);
884 stream_write(s, pw->ifname, IF_NAMESIZE);
885 stream_putl(s, pw->ifindex);
886 stream_putl(s, pw->status);
887
888 /* Put length at the first point of the stream. */
889 stream_putw_at(s, 0, stream_get_endp(s));
890
891 return zebra_server_send_message(client, s);
892 }
893
894 /* Send response to a get label chunk request to client */
895 static int zsend_assign_label_chunk_response(struct zserv *client,
896 vrf_id_t vrf_id,
897 struct label_manager_chunk *lmc)
898 {
899 int ret;
900 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
901
902 zclient_create_header(s, ZEBRA_GET_LABEL_CHUNK, vrf_id);
903
904 if (lmc) {
905 /* keep */
906 stream_putc(s, lmc->keep);
907 /* start and end labels */
908 stream_putl(s, lmc->start);
909 stream_putl(s, lmc->end);
910 }
911
912 /* Write packet size. */
913 stream_putw_at(s, 0, stream_get_endp(s));
914
915 ret = writen(client->sock, s->data, stream_get_endp(s));
916 stream_free(s);
917 return ret;
918 }
919
920 /* Send response to a label manager connect request to client */
921 static int zsend_label_manager_connect_response(struct zserv *client,
922 vrf_id_t vrf_id,
923 unsigned short result)
924 {
925 int ret;
926 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
927
928 zclient_create_header(s, ZEBRA_LABEL_MANAGER_CONNECT, vrf_id);
929
930 /* result */
931 stream_putc(s, result);
932
933 /* Write packet size. */
934 stream_putw_at(s, 0, stream_get_endp(s));
935
936 ret = writen(client->sock, s->data, stream_get_endp(s));
937 stream_free(s);
938
939 return ret;
940 }
941
942 /* Inbound message handling ------------------------------------------------ */
943
944 int cmd2type[] = {
945 [ZEBRA_NEXTHOP_REGISTER] = RNH_NEXTHOP_TYPE,
946 [ZEBRA_NEXTHOP_UNREGISTER] = RNH_NEXTHOP_TYPE,
947 [ZEBRA_IMPORT_ROUTE_REGISTER] = RNH_IMPORT_CHECK_TYPE,
948 [ZEBRA_IMPORT_ROUTE_UNREGISTER] = RNH_IMPORT_CHECK_TYPE,
949 };
950
951 /* Nexthop register */
952 static void zread_rnh_register(ZAPI_HANDLER_ARGS)
953 {
954 struct rnh *rnh;
955 struct stream *s;
956 struct prefix p;
957 unsigned short l = 0;
958 uint8_t flags = 0;
959 uint16_t type = cmd2type[hdr->command];
960
961 if (IS_ZEBRA_DEBUG_NHT)
962 zlog_debug(
963 "rnh_register msg from client %s: hdr->length=%d, type=%s vrf=%u\n",
964 zebra_route_string(client->proto), hdr->length,
965 (type == RNH_NEXTHOP_TYPE) ? "nexthop" : "route",
966 zvrf->vrf->vrf_id);
967
968 s = msg;
969
970 client->nh_reg_time = monotime(NULL);
971
972 while (l < hdr->length) {
973 STREAM_GETC(s, flags);
974 STREAM_GETW(s, p.family);
975 STREAM_GETC(s, p.prefixlen);
976 l += 4;
977 if (p.family == AF_INET) {
978 if (p.prefixlen > IPV4_MAX_BITLEN) {
979 zlog_warn(
980 "%s: Specified prefix hdr->length %d is too large for a v4 address",
981 __PRETTY_FUNCTION__, p.prefixlen);
982 return;
983 }
984 STREAM_GET(&p.u.prefix4.s_addr, s, IPV4_MAX_BYTELEN);
985 l += IPV4_MAX_BYTELEN;
986 } else if (p.family == AF_INET6) {
987 if (p.prefixlen > IPV6_MAX_BITLEN) {
988 zlog_warn(
989 "%s: Specified prefix hdr->length %d is to large for a v6 address",
990 __PRETTY_FUNCTION__, p.prefixlen);
991 return;
992 }
993 STREAM_GET(&p.u.prefix6, s, IPV6_MAX_BYTELEN);
994 l += IPV6_MAX_BYTELEN;
995 } else {
996 zlog_err(
997 "rnh_register: Received unknown family type %d\n",
998 p.family);
999 return;
1000 }
1001 rnh = zebra_add_rnh(&p, zvrf_id(zvrf), type);
1002 if (type == RNH_NEXTHOP_TYPE) {
1003 if (flags
1004 && !CHECK_FLAG(rnh->flags, ZEBRA_NHT_CONNECTED))
1005 SET_FLAG(rnh->flags, ZEBRA_NHT_CONNECTED);
1006 else if (!flags
1007 && CHECK_FLAG(rnh->flags, ZEBRA_NHT_CONNECTED))
1008 UNSET_FLAG(rnh->flags, ZEBRA_NHT_CONNECTED);
1009 } else if (type == RNH_IMPORT_CHECK_TYPE) {
1010 if (flags
1011 && !CHECK_FLAG(rnh->flags, ZEBRA_NHT_EXACT_MATCH))
1012 SET_FLAG(rnh->flags, ZEBRA_NHT_EXACT_MATCH);
1013 else if (!flags
1014 && CHECK_FLAG(rnh->flags,
1015 ZEBRA_NHT_EXACT_MATCH))
1016 UNSET_FLAG(rnh->flags, ZEBRA_NHT_EXACT_MATCH);
1017 }
1018
1019 zebra_add_rnh_client(rnh, client, type, zvrf_id(zvrf));
1020 /* Anything not AF_INET/INET6 has been filtered out above */
1021 zebra_evaluate_rnh(zvrf_id(zvrf), p.family, 1, type, &p);
1022 }
1023
1024 stream_failure:
1025 return;
1026 }
1027
1028 /* Nexthop register */
1029 static void zread_rnh_unregister(ZAPI_HANDLER_ARGS)
1030 {
1031 struct rnh *rnh;
1032 struct stream *s;
1033 struct prefix p;
1034 unsigned short l = 0;
1035 uint16_t type = cmd2type[hdr->command];
1036
1037 if (IS_ZEBRA_DEBUG_NHT)
1038 zlog_debug(
1039 "rnh_unregister msg from client %s: hdr->length=%d vrf: %u\n",
1040 zebra_route_string(client->proto), hdr->length,
1041 zvrf->vrf->vrf_id);
1042
1043 s = msg;
1044
1045 while (l < hdr->length) {
1046 uint8_t flags;
1047
1048 STREAM_GETC(s, flags);
1049 if (flags != 0)
1050 goto stream_failure;
1051
1052 STREAM_GETW(s, p.family);
1053 STREAM_GETC(s, p.prefixlen);
1054 l += 4;
1055 if (p.family == AF_INET) {
1056 if (p.prefixlen > IPV4_MAX_BITLEN) {
1057 zlog_warn(
1058 "%s: Specified prefix hdr->length %d is to large for a v4 address",
1059 __PRETTY_FUNCTION__, p.prefixlen);
1060 return;
1061 }
1062 STREAM_GET(&p.u.prefix4.s_addr, s, IPV4_MAX_BYTELEN);
1063 l += IPV4_MAX_BYTELEN;
1064 } else if (p.family == AF_INET6) {
1065 if (p.prefixlen > IPV6_MAX_BITLEN) {
1066 zlog_warn(
1067 "%s: Specified prefix hdr->length %d is to large for a v6 address",
1068 __PRETTY_FUNCTION__, p.prefixlen);
1069 return;
1070 }
1071 STREAM_GET(&p.u.prefix6, s, IPV6_MAX_BYTELEN);
1072 l += IPV6_MAX_BYTELEN;
1073 } else {
1074 zlog_err(
1075 "rnh_register: Received unknown family type %d\n",
1076 p.family);
1077 return;
1078 }
1079 rnh = zebra_lookup_rnh(&p, zvrf_id(zvrf), type);
1080 if (rnh) {
1081 client->nh_dereg_time = monotime(NULL);
1082 zebra_remove_rnh_client(rnh, client, type);
1083 }
1084 }
1085 stream_failure:
1086 return;
1087 }
1088
1089 #define ZEBRA_MIN_FEC_LENGTH 5
1090
1091 /* FEC register */
1092 static void zread_fec_register(ZAPI_HANDLER_ARGS)
1093 {
1094 struct stream *s;
1095 unsigned short l = 0;
1096 struct prefix p;
1097 uint16_t flags;
1098 uint32_t label_index = MPLS_INVALID_LABEL_INDEX;
1099
1100 s = msg;
1101 zvrf = vrf_info_lookup(VRF_DEFAULT);
1102 if (!zvrf)
1103 return; // unexpected
1104
1105 /*
1106 * The minimum amount of data that can be sent for one fec
1107 * registration
1108 */
1109 if (hdr->length < ZEBRA_MIN_FEC_LENGTH) {
1110 zlog_err(
1111 "fec_register: Received a fec register of hdr->length %d, it is of insufficient size to properly decode",
1112 hdr->length);
1113 return;
1114 }
1115
1116 while (l < hdr->length) {
1117 STREAM_GETW(s, flags);
1118 memset(&p, 0, sizeof(p));
1119 STREAM_GETW(s, p.family);
1120 if (p.family != AF_INET && p.family != AF_INET6) {
1121 zlog_err(
1122 "fec_register: Received unknown family type %d\n",
1123 p.family);
1124 return;
1125 }
1126 STREAM_GETC(s, p.prefixlen);
1127 if ((p.family == AF_INET && p.prefixlen > IPV4_MAX_BITLEN)
1128 || (p.family == AF_INET6
1129 && p.prefixlen > IPV6_MAX_BITLEN)) {
1130 zlog_warn(
1131 "%s: Specified prefix hdr->length: %d is to long for %d",
1132 __PRETTY_FUNCTION__, p.prefixlen, p.family);
1133 return;
1134 }
1135 l += 5;
1136 STREAM_GET(&p.u.prefix, s, PSIZE(p.prefixlen));
1137 l += PSIZE(p.prefixlen);
1138 if (flags & ZEBRA_FEC_REGISTER_LABEL_INDEX) {
1139 STREAM_GETL(s, label_index);
1140 l += 4;
1141 } else
1142 label_index = MPLS_INVALID_LABEL_INDEX;
1143 zebra_mpls_fec_register(zvrf, &p, label_index, client);
1144 }
1145
1146 stream_failure:
1147 return;
1148 }
1149
1150 /* FEC unregister */
1151 static void zread_fec_unregister(ZAPI_HANDLER_ARGS)
1152 {
1153 struct stream *s;
1154 unsigned short l = 0;
1155 struct prefix p;
1156 uint16_t flags;
1157
1158 s = msg;
1159 zvrf = vrf_info_lookup(VRF_DEFAULT);
1160 if (!zvrf)
1161 return; // unexpected
1162
1163 /*
1164 * The minimum amount of data that can be sent for one
1165 * fec unregistration
1166 */
1167 if (hdr->length < ZEBRA_MIN_FEC_LENGTH) {
1168 zlog_err(
1169 "fec_unregister: Received a fec unregister of hdr->length %d, it is of insufficient size to properly decode",
1170 hdr->length);
1171 return;
1172 }
1173
1174 while (l < hdr->length) {
1175 STREAM_GETW(s, flags);
1176 if (flags != 0)
1177 goto stream_failure;
1178
1179 memset(&p, 0, sizeof(p));
1180 STREAM_GETW(s, p.family);
1181 if (p.family != AF_INET && p.family != AF_INET6) {
1182 zlog_err(
1183 "fec_unregister: Received unknown family type %d\n",
1184 p.family);
1185 return;
1186 }
1187 STREAM_GETC(s, p.prefixlen);
1188 if ((p.family == AF_INET && p.prefixlen > IPV4_MAX_BITLEN)
1189 || (p.family == AF_INET6
1190 && p.prefixlen > IPV6_MAX_BITLEN)) {
1191 zlog_warn(
1192 "%s: Received prefix hdr->length %d which is greater than %d can support",
1193 __PRETTY_FUNCTION__, p.prefixlen, p.family);
1194 return;
1195 }
1196 l += 5;
1197 STREAM_GET(&p.u.prefix, s, PSIZE(p.prefixlen));
1198 l += PSIZE(p.prefixlen);
1199 zebra_mpls_fec_unregister(zvrf, &p, client);
1200 }
1201
1202 stream_failure:
1203 return;
1204 }
1205
1206
1207 /*
1208 * Register zebra server interface information.
1209 * Send current all interface and address information.
1210 */
1211 static void zread_interface_add(ZAPI_HANDLER_ARGS)
1212 {
1213 struct vrf *vrf;
1214 struct interface *ifp;
1215
1216 /* Interface information is needed. */
1217 vrf_bitmap_set(client->ifinfo, zvrf_id(zvrf));
1218
1219 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) {
1220 FOR_ALL_INTERFACES (vrf, ifp) {
1221 /* Skip pseudo interface. */
1222 if (!CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE))
1223 continue;
1224
1225 zsend_interface_add(client, ifp);
1226 zsend_interface_addresses(client, ifp);
1227 }
1228 }
1229 }
1230
1231 /* Unregister zebra server interface information. */
1232 static void zread_interface_delete(ZAPI_HANDLER_ARGS)
1233 {
1234 vrf_bitmap_unset(client->ifinfo, zvrf_id(zvrf));
1235 }
1236
1237 void zserv_nexthop_num_warn(const char *caller, const struct prefix *p,
1238 const unsigned int nexthop_num)
1239 {
1240 if (nexthop_num > multipath_num) {
1241 char buff[PREFIX2STR_BUFFER];
1242 prefix2str(p, buff, sizeof(buff));
1243 zlog_warn(
1244 "%s: Prefix %s has %d nexthops, but we can only use the first %d",
1245 caller, buff, nexthop_num, multipath_num);
1246 }
1247 }
1248
1249 static void zread_route_add(ZAPI_HANDLER_ARGS)
1250 {
1251 struct stream *s;
1252 struct zapi_route api;
1253 struct zapi_nexthop *api_nh;
1254 afi_t afi;
1255 struct prefix_ipv6 *src_p = NULL;
1256 struct route_entry *re;
1257 struct nexthop *nexthop = NULL;
1258 int i, ret;
1259 vrf_id_t vrf_id = 0;
1260 struct ipaddr vtep_ip;
1261
1262 s = msg;
1263 if (zapi_route_decode(s, &api) < 0) {
1264 if (IS_ZEBRA_DEBUG_RECV)
1265 zlog_debug("%s: Unable to decode zapi_route sent",
1266 __PRETTY_FUNCTION__);
1267 return;
1268 }
1269
1270 if (IS_ZEBRA_DEBUG_RECV) {
1271 char buf_prefix[PREFIX_STRLEN];
1272 prefix2str(&api.prefix, buf_prefix, sizeof(buf_prefix));
1273 zlog_debug("%s: p=%s, ZAPI_MESSAGE_LABEL: %sset, flags=0x%x",
1274 __func__, buf_prefix,
1275 (CHECK_FLAG(api.message, ZAPI_MESSAGE_LABEL) ? ""
1276 : "un"),
1277 api.flags);
1278 }
1279
1280 /* Allocate new route. */
1281 vrf_id = zvrf_id(zvrf);
1282 re = XCALLOC(MTYPE_RE, sizeof(struct route_entry));
1283 re->type = api.type;
1284 re->instance = api.instance;
1285 re->flags = api.flags;
1286 re->uptime = time(NULL);
1287 re->vrf_id = vrf_id;
1288 if (api.tableid && vrf_id == VRF_DEFAULT)
1289 re->table = api.tableid;
1290 else
1291 re->table = zvrf->table_id;
1292
1293 /*
1294 * TBD should _all_ of the nexthop add operations use
1295 * api_nh->vrf_id instead of re->vrf_id ? I only changed
1296 * for cases NEXTHOP_TYPE_IPV4 and NEXTHOP_TYPE_IPV6.
1297 */
1298 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP)) {
1299 for (i = 0; i < api.nexthop_num; i++) {
1300 api_nh = &api.nexthops[i];
1301 ifindex_t ifindex = 0;
1302
1303 if (IS_ZEBRA_DEBUG_RECV) {
1304 zlog_debug("nh type %d", api_nh->type);
1305 }
1306
1307 switch (api_nh->type) {
1308 case NEXTHOP_TYPE_IFINDEX:
1309 nexthop = route_entry_nexthop_ifindex_add(
1310 re, api_nh->ifindex, api_nh->vrf_id);
1311 break;
1312 case NEXTHOP_TYPE_IPV4:
1313 if (IS_ZEBRA_DEBUG_RECV) {
1314 char nhbuf[INET6_ADDRSTRLEN] = {0};
1315 inet_ntop(AF_INET, &api_nh->gate.ipv4,
1316 nhbuf, INET6_ADDRSTRLEN);
1317 zlog_debug("%s: nh=%s, vrf_id=%d",
1318 __func__, nhbuf,
1319 api_nh->vrf_id);
1320 }
1321 nexthop = route_entry_nexthop_ipv4_add(
1322 re, &api_nh->gate.ipv4, NULL,
1323 api_nh->vrf_id);
1324 break;
1325 case NEXTHOP_TYPE_IPV4_IFINDEX:
1326
1327 memset(&vtep_ip, 0, sizeof(struct ipaddr));
1328 if (CHECK_FLAG(api.flags,
1329 ZEBRA_FLAG_EVPN_ROUTE)) {
1330 ifindex = get_l3vni_svi_ifindex(vrf_id);
1331 } else {
1332 ifindex = api_nh->ifindex;
1333 }
1334
1335 if (IS_ZEBRA_DEBUG_RECV) {
1336 char nhbuf[INET6_ADDRSTRLEN] = {0};
1337 inet_ntop(AF_INET, &api_nh->gate.ipv4,
1338 nhbuf, INET6_ADDRSTRLEN);
1339 zlog_debug(
1340 "%s: nh=%s, vrf_id=%d (re->vrf_id=%d), ifindex=%d",
1341 __func__, nhbuf, api_nh->vrf_id,
1342 re->vrf_id, ifindex);
1343 }
1344 nexthop = route_entry_nexthop_ipv4_ifindex_add(
1345 re, &api_nh->gate.ipv4, NULL, ifindex,
1346 api_nh->vrf_id);
1347
1348 /* if this an EVPN route entry,
1349 * program the nh as neigh
1350 */
1351 if (CHECK_FLAG(api.flags,
1352 ZEBRA_FLAG_EVPN_ROUTE)) {
1353 SET_FLAG(nexthop->flags,
1354 NEXTHOP_FLAG_EVPN_RVTEP);
1355 vtep_ip.ipa_type = IPADDR_V4;
1356 memcpy(&(vtep_ip.ipaddr_v4),
1357 &(api_nh->gate.ipv4),
1358 sizeof(struct in_addr));
1359 zebra_vxlan_evpn_vrf_route_add(
1360 vrf_id, &api.rmac, &vtep_ip,
1361 &api.prefix);
1362 }
1363 break;
1364 case NEXTHOP_TYPE_IPV6:
1365 nexthop = route_entry_nexthop_ipv6_add(
1366 re, &api_nh->gate.ipv6, api_nh->vrf_id);
1367 break;
1368 case NEXTHOP_TYPE_IPV6_IFINDEX:
1369 memset(&vtep_ip, 0, sizeof(struct ipaddr));
1370 if (CHECK_FLAG(api.flags,
1371 ZEBRA_FLAG_EVPN_ROUTE)) {
1372 ifindex =
1373 get_l3vni_svi_ifindex(vrf_id);
1374 } else {
1375 ifindex = api_nh->ifindex;
1376 }
1377
1378 nexthop = route_entry_nexthop_ipv6_ifindex_add(
1379 re, &api_nh->gate.ipv6, ifindex,
1380 api_nh->vrf_id);
1381
1382 /* if this an EVPN route entry,
1383 * program the nh as neigh
1384 */
1385 if (CHECK_FLAG(api.flags,
1386 ZEBRA_FLAG_EVPN_ROUTE)) {
1387 SET_FLAG(nexthop->flags,
1388 NEXTHOP_FLAG_EVPN_RVTEP);
1389 vtep_ip.ipa_type = IPADDR_V6;
1390 memcpy(&vtep_ip.ipaddr_v6,
1391 &(api_nh->gate.ipv6),
1392 sizeof(struct in6_addr));
1393 zebra_vxlan_evpn_vrf_route_add(
1394 vrf_id,
1395 &api.rmac,
1396 &vtep_ip,
1397 &api.prefix);
1398 }
1399 break;
1400 case NEXTHOP_TYPE_BLACKHOLE:
1401 nexthop = route_entry_nexthop_blackhole_add(
1402 re, api_nh->bh_type);
1403 break;
1404 }
1405
1406 if (!nexthop) {
1407 zlog_warn(
1408 "%s: Nexthops Specified: %d but we failed to properly create one",
1409 __PRETTY_FUNCTION__, api.nexthop_num);
1410 nexthops_free(re->ng.nexthop);
1411 XFREE(MTYPE_RE, re);
1412 return;
1413 }
1414 /* MPLS labels for BGP-LU or Segment Routing */
1415 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_LABEL)
1416 && api_nh->type != NEXTHOP_TYPE_IFINDEX
1417 && api_nh->type != NEXTHOP_TYPE_BLACKHOLE) {
1418 enum lsp_types_t label_type;
1419
1420 label_type =
1421 lsp_type_from_re_type(client->proto);
1422
1423 if (IS_ZEBRA_DEBUG_RECV) {
1424 zlog_debug(
1425 "%s: adding %d labels of type %d (1st=%u)",
1426 __func__, api_nh->label_num,
1427 label_type, api_nh->labels[0]);
1428 }
1429
1430 nexthop_add_labels(nexthop, label_type,
1431 api_nh->label_num,
1432 &api_nh->labels[0]);
1433 }
1434 }
1435 }
1436
1437 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_DISTANCE))
1438 re->distance = api.distance;
1439 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_METRIC))
1440 re->metric = api.metric;
1441 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_TAG))
1442 re->tag = api.tag;
1443 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_MTU))
1444 re->mtu = api.mtu;
1445
1446 afi = family2afi(api.prefix.family);
1447 if (afi != AFI_IP6 && CHECK_FLAG(api.message, ZAPI_MESSAGE_SRCPFX)) {
1448 zlog_warn("%s: Received SRC Prefix but afi is not v6",
1449 __PRETTY_FUNCTION__);
1450 nexthops_free(re->ng.nexthop);
1451 XFREE(MTYPE_RE, re);
1452 return;
1453 }
1454 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_SRCPFX))
1455 src_p = &api.src_prefix;
1456
1457 ret = rib_add_multipath(afi, api.safi, &api.prefix, src_p, re);
1458
1459 /* Stats */
1460 switch (api.prefix.family) {
1461 case AF_INET:
1462 if (ret > 0)
1463 client->v4_route_add_cnt++;
1464 else if (ret < 0)
1465 client->v4_route_upd8_cnt++;
1466 break;
1467 case AF_INET6:
1468 if (ret > 0)
1469 client->v6_route_add_cnt++;
1470 else if (ret < 0)
1471 client->v6_route_upd8_cnt++;
1472 break;
1473 }
1474 }
1475
1476 static void zread_route_del(ZAPI_HANDLER_ARGS)
1477 {
1478 struct stream *s;
1479 struct zapi_route api;
1480 afi_t afi;
1481 struct prefix_ipv6 *src_p = NULL;
1482 uint32_t table_id;
1483
1484 s = msg;
1485 if (zapi_route_decode(s, &api) < 0)
1486 return;
1487
1488 afi = family2afi(api.prefix.family);
1489 if (afi != AFI_IP6 && CHECK_FLAG(api.message, ZAPI_MESSAGE_SRCPFX)) {
1490 zlog_warn("%s: Received a src prefix while afi is not v6",
1491 __PRETTY_FUNCTION__);
1492 return;
1493 }
1494 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_SRCPFX))
1495 src_p = &api.src_prefix;
1496
1497 if (api.vrf_id == VRF_DEFAULT && api.tableid != 0)
1498 table_id = api.tableid;
1499 else
1500 table_id = zvrf->table_id;
1501
1502 rib_delete(afi, api.safi, zvrf_id(zvrf), api.type, api.instance,
1503 api.flags, &api.prefix, src_p, NULL, table_id,
1504 api.metric, false, &api.rmac);
1505
1506 /* Stats */
1507 switch (api.prefix.family) {
1508 case AF_INET:
1509 client->v4_route_del_cnt++;
1510 break;
1511 case AF_INET6:
1512 client->v6_route_del_cnt++;
1513 break;
1514 }
1515 }
1516
1517 /* This function support multiple nexthop. */
1518 /*
1519 * Parse the ZEBRA_IPV4_ROUTE_ADD sent from client. Update re and
1520 * add kernel route.
1521 */
1522 static void zread_ipv4_add(ZAPI_HANDLER_ARGS)
1523 {
1524 int i;
1525 struct route_entry *re;
1526 struct prefix p;
1527 uint8_t message;
1528 struct in_addr nhop_addr;
1529 uint8_t nexthop_num;
1530 uint8_t nexthop_type;
1531 struct stream *s;
1532 ifindex_t ifindex;
1533 safi_t safi;
1534 int ret;
1535 enum lsp_types_t label_type = ZEBRA_LSP_NONE;
1536 mpls_label_t label;
1537 struct nexthop *nexthop;
1538 enum blackhole_type bh_type = BLACKHOLE_NULL;
1539
1540 /* Get input stream. */
1541 s = msg;
1542
1543 /* Allocate new re. */
1544 re = XCALLOC(MTYPE_RE, sizeof(struct route_entry));
1545
1546 /* Type, flags, message. */
1547 STREAM_GETC(s, re->type);
1548 if (re->type > ZEBRA_ROUTE_MAX) {
1549 zlog_warn("%s: Specified route type %d is not a legal value\n",
1550 __PRETTY_FUNCTION__, re->type);
1551 XFREE(MTYPE_RE, re);
1552 return;
1553 }
1554 STREAM_GETW(s, re->instance);
1555 STREAM_GETL(s, re->flags);
1556 STREAM_GETC(s, message);
1557 STREAM_GETW(s, safi);
1558 re->uptime = time(NULL);
1559
1560 /* IPv4 prefix. */
1561 memset(&p, 0, sizeof(struct prefix_ipv4));
1562 p.family = AF_INET;
1563 STREAM_GETC(s, p.prefixlen);
1564 if (p.prefixlen > IPV4_MAX_BITLEN) {
1565 zlog_warn(
1566 "%s: Specified prefix length %d is greater than what v4 can be",
1567 __PRETTY_FUNCTION__, p.prefixlen);
1568 XFREE(MTYPE_RE, re);
1569 return;
1570 }
1571 STREAM_GET(&p.u.prefix4, s, PSIZE(p.prefixlen));
1572
1573 /* VRF ID */
1574 re->vrf_id = zvrf_id(zvrf);
1575
1576 /* Nexthop parse. */
1577 if (CHECK_FLAG(message, ZAPI_MESSAGE_NEXTHOP)) {
1578 STREAM_GETC(s, nexthop_num);
1579 zserv_nexthop_num_warn(__func__, (const struct prefix *)&p,
1580 nexthop_num);
1581
1582 if (CHECK_FLAG(message, ZAPI_MESSAGE_LABEL))
1583 label_type = lsp_type_from_re_type(client->proto);
1584
1585 for (i = 0; i < nexthop_num; i++) {
1586 STREAM_GETC(s, nexthop_type);
1587
1588 switch (nexthop_type) {
1589 case NEXTHOP_TYPE_IFINDEX:
1590 STREAM_GETL(s, ifindex);
1591 route_entry_nexthop_ifindex_add(re, ifindex,
1592 re->vrf_id);
1593 break;
1594 case NEXTHOP_TYPE_IPV4:
1595 STREAM_GET(&nhop_addr.s_addr, s,
1596 IPV4_MAX_BYTELEN);
1597 nexthop = route_entry_nexthop_ipv4_add(
1598 re, &nhop_addr, NULL, re->vrf_id);
1599 /* For labeled-unicast, each nexthop is followed
1600 * by label. */
1601 if (CHECK_FLAG(message, ZAPI_MESSAGE_LABEL)) {
1602 STREAM_GETL(s, label);
1603 nexthop_add_labels(nexthop, label_type,
1604 1, &label);
1605 }
1606 break;
1607 case NEXTHOP_TYPE_IPV4_IFINDEX:
1608 STREAM_GET(&nhop_addr.s_addr, s,
1609 IPV4_MAX_BYTELEN);
1610 STREAM_GETL(s, ifindex);
1611 route_entry_nexthop_ipv4_ifindex_add(
1612 re, &nhop_addr, NULL, ifindex,
1613 re->vrf_id);
1614 break;
1615 case NEXTHOP_TYPE_IPV6:
1616 zlog_warn(
1617 "%s: Please use ZEBRA_ROUTE_ADD if you want to pass v6 nexthops",
1618 __PRETTY_FUNCTION__);
1619 nexthops_free(re->ng.nexthop);
1620 XFREE(MTYPE_RE, re);
1621 return;
1622 break;
1623 case NEXTHOP_TYPE_BLACKHOLE:
1624 route_entry_nexthop_blackhole_add(re, bh_type);
1625 break;
1626 default:
1627 zlog_warn(
1628 "%s: Specified nexthop type: %d does not exist",
1629 __PRETTY_FUNCTION__, nexthop_type);
1630 nexthops_free(re->ng.nexthop);
1631 XFREE(MTYPE_RE, re);
1632 return;
1633 }
1634 }
1635 }
1636
1637 /* Distance. */
1638 if (CHECK_FLAG(message, ZAPI_MESSAGE_DISTANCE))
1639 STREAM_GETC(s, re->distance);
1640
1641 /* Metric. */
1642 if (CHECK_FLAG(message, ZAPI_MESSAGE_METRIC))
1643 STREAM_GETL(s, re->metric);
1644
1645 /* Tag */
1646 if (CHECK_FLAG(message, ZAPI_MESSAGE_TAG))
1647 STREAM_GETL(s, re->tag);
1648 else
1649 re->tag = 0;
1650
1651 if (CHECK_FLAG(message, ZAPI_MESSAGE_MTU))
1652 STREAM_GETL(s, re->mtu);
1653 else
1654 re->mtu = 0;
1655
1656 /* Table */
1657 re->table = zvrf->table_id;
1658
1659 ret = rib_add_multipath(AFI_IP, safi, &p, NULL, re);
1660
1661 /* Stats */
1662 if (ret > 0)
1663 client->v4_route_add_cnt++;
1664 else if (ret < 0)
1665 client->v4_route_upd8_cnt++;
1666
1667 return;
1668
1669 stream_failure:
1670 nexthops_free(re->ng.nexthop);
1671 XFREE(MTYPE_RE, re);
1672 }
1673
1674 /* Zebra server IPv4 prefix delete function. */
1675 static void zread_ipv4_delete(ZAPI_HANDLER_ARGS)
1676 {
1677 struct stream *s;
1678 struct zapi_ipv4 api;
1679 struct prefix p;
1680 uint32_t table_id;
1681
1682 s = msg;
1683
1684 /* Type, flags, message. */
1685 STREAM_GETC(s, api.type);
1686 STREAM_GETW(s, api.instance);
1687 STREAM_GETL(s, api.flags);
1688 STREAM_GETC(s, api.message);
1689 STREAM_GETW(s, api.safi);
1690
1691 /* IPv4 prefix. */
1692 memset(&p, 0, sizeof(struct prefix));
1693 p.family = AF_INET;
1694 STREAM_GETC(s, p.prefixlen);
1695 if (p.prefixlen > IPV4_MAX_BITLEN) {
1696 zlog_warn("%s: Passed in prefixlen %d is impossible",
1697 __PRETTY_FUNCTION__, p.prefixlen);
1698 return;
1699 }
1700 STREAM_GET(&p.u.prefix4, s, PSIZE(p.prefixlen));
1701
1702 table_id = zvrf->table_id;
1703
1704 rib_delete(AFI_IP, api.safi, zvrf_id(zvrf), api.type, api.instance,
1705 api.flags, &p, NULL, NULL, table_id, 0, false, NULL);
1706 client->v4_route_del_cnt++;
1707
1708 stream_failure:
1709 return;
1710 }
1711
1712 /* MRIB Nexthop lookup for IPv4. */
1713 static void zread_ipv4_nexthop_lookup_mrib(ZAPI_HANDLER_ARGS)
1714 {
1715 struct in_addr addr;
1716 struct route_entry *re;
1717
1718 STREAM_GET(&addr.s_addr, msg, IPV4_MAX_BYTELEN);
1719 re = rib_match_ipv4_multicast(zvrf_id(zvrf), addr, NULL);
1720 zsend_ipv4_nexthop_lookup_mrib(client, addr, re, zvrf);
1721
1722 stream_failure:
1723 return;
1724 }
1725
1726 /* Zebra server IPv6 prefix add function. */
1727 static void zread_ipv4_route_ipv6_nexthop_add(ZAPI_HANDLER_ARGS)
1728 {
1729 unsigned int i;
1730 struct stream *s;
1731 struct in6_addr nhop_addr;
1732 struct route_entry *re;
1733 uint8_t message;
1734 uint8_t nexthop_num;
1735 uint8_t nexthop_type;
1736 struct prefix p;
1737 safi_t safi;
1738 static struct in6_addr nexthops[MULTIPATH_NUM];
1739 static unsigned int ifindices[MULTIPATH_NUM];
1740 int ret;
1741 static mpls_label_t labels[MULTIPATH_NUM];
1742 enum lsp_types_t label_type = ZEBRA_LSP_NONE;
1743 mpls_label_t label;
1744 struct nexthop *nexthop;
1745 enum blackhole_type bh_type = BLACKHOLE_NULL;
1746
1747 /* Get input stream. */
1748 s = msg;
1749
1750 memset(&nhop_addr, 0, sizeof(struct in6_addr));
1751
1752 /* Allocate new re. */
1753 re = XCALLOC(MTYPE_RE, sizeof(struct route_entry));
1754
1755 /* Type, flags, message. */
1756 STREAM_GETC(s, re->type);
1757 if (re->type > ZEBRA_ROUTE_MAX) {
1758 zlog_warn("%s: Specified route type: %d is not a legal value\n",
1759 __PRETTY_FUNCTION__, re->type);
1760 XFREE(MTYPE_RE, re);
1761 return;
1762 }
1763 STREAM_GETW(s, re->instance);
1764 STREAM_GETL(s, re->flags);
1765 STREAM_GETC(s, message);
1766 STREAM_GETW(s, safi);
1767 re->uptime = time(NULL);
1768
1769 /* IPv4 prefix. */
1770 memset(&p, 0, sizeof(struct prefix_ipv4));
1771 p.family = AF_INET;
1772 STREAM_GETC(s, p.prefixlen);
1773 if (p.prefixlen > IPV4_MAX_BITLEN) {
1774 zlog_warn(
1775 "%s: Prefix Length %d is greater than what a v4 address can use",
1776 __PRETTY_FUNCTION__, p.prefixlen);
1777 XFREE(MTYPE_RE, re);
1778 return;
1779 }
1780 STREAM_GET(&p.u.prefix4, s, PSIZE(p.prefixlen));
1781
1782 /* VRF ID */
1783 re->vrf_id = zvrf_id(zvrf);
1784
1785 /* We need to give nh-addr, nh-ifindex with the same next-hop object
1786 * to the re to ensure that IPv6 multipathing works; need to coalesce
1787 * these. Clients should send the same number of paired set of
1788 * next-hop-addr/next-hop-ifindices. */
1789 if (CHECK_FLAG(message, ZAPI_MESSAGE_NEXTHOP)) {
1790 unsigned int nh_count = 0;
1791 unsigned int if_count = 0;
1792 unsigned int max_nh_if = 0;
1793
1794 STREAM_GETC(s, nexthop_num);
1795 zserv_nexthop_num_warn(__func__, (const struct prefix *)&p,
1796 nexthop_num);
1797
1798 if (CHECK_FLAG(message, ZAPI_MESSAGE_LABEL))
1799 label_type = lsp_type_from_re_type(client->proto);
1800
1801 for (i = 0; i < nexthop_num; i++) {
1802 STREAM_GETC(s, nexthop_type);
1803
1804 switch (nexthop_type) {
1805 case NEXTHOP_TYPE_IPV6:
1806 STREAM_GET(&nhop_addr, s, 16);
1807 if (nh_count < MULTIPATH_NUM) {
1808 /* For labeled-unicast, each nexthop is
1809 * followed by label. */
1810 if (CHECK_FLAG(message,
1811 ZAPI_MESSAGE_LABEL)) {
1812 STREAM_GETL(s, label);
1813 labels[nh_count] = label;
1814 }
1815 nexthops[nh_count] = nhop_addr;
1816 nh_count++;
1817 }
1818 break;
1819 case NEXTHOP_TYPE_IFINDEX:
1820 if (if_count < multipath_num) {
1821 STREAM_GETL(s, ifindices[if_count++]);
1822 }
1823 break;
1824 case NEXTHOP_TYPE_BLACKHOLE:
1825 route_entry_nexthop_blackhole_add(re, bh_type);
1826 break;
1827 default:
1828 zlog_warn(
1829 "%s: Please use ZEBRA_ROUTE_ADD if you want to pass non v6 nexthops",
1830 __PRETTY_FUNCTION__);
1831 nexthops_free(re->ng.nexthop);
1832 XFREE(MTYPE_RE, re);
1833 return;
1834 }
1835 }
1836
1837 max_nh_if = (nh_count > if_count) ? nh_count : if_count;
1838 for (i = 0; i < max_nh_if; i++) {
1839 if ((i < nh_count)
1840 && !IN6_IS_ADDR_UNSPECIFIED(&nexthops[i])) {
1841 if ((i < if_count) && ifindices[i])
1842 nexthop =
1843 route_entry_nexthop_ipv6_ifindex_add(
1844 re, &nexthops[i],
1845 ifindices[i],
1846 re->vrf_id);
1847 else
1848 nexthop = route_entry_nexthop_ipv6_add(
1849 re, &nexthops[i], re->vrf_id);
1850
1851 if (CHECK_FLAG(message, ZAPI_MESSAGE_LABEL))
1852 nexthop_add_labels(nexthop, label_type,
1853 1, &labels[i]);
1854 } else {
1855 if ((i < if_count) && ifindices[i])
1856 route_entry_nexthop_ifindex_add(
1857 re, ifindices[i], re->vrf_id);
1858 }
1859 }
1860 }
1861
1862 /* Distance. */
1863 if (CHECK_FLAG(message, ZAPI_MESSAGE_DISTANCE))
1864 STREAM_GETC(s, re->distance);
1865
1866 /* Metric. */
1867 if (CHECK_FLAG(message, ZAPI_MESSAGE_METRIC))
1868 STREAM_GETL(s, re->metric);
1869
1870 /* Tag */
1871 if (CHECK_FLAG(message, ZAPI_MESSAGE_TAG))
1872 STREAM_GETL(s, re->tag);
1873 else
1874 re->tag = 0;
1875
1876 if (CHECK_FLAG(message, ZAPI_MESSAGE_MTU))
1877 STREAM_GETL(s, re->mtu);
1878 else
1879 re->mtu = 0;
1880
1881 /* Table */
1882 re->table = zvrf->table_id;
1883
1884 ret = rib_add_multipath(AFI_IP6, safi, &p, NULL, re);
1885 /* Stats */
1886 if (ret > 0)
1887 client->v4_route_add_cnt++;
1888 else if (ret < 0)
1889 client->v4_route_upd8_cnt++;
1890
1891 return;
1892
1893 stream_failure:
1894 nexthops_free(re->ng.nexthop);
1895 XFREE(MTYPE_RE, re);
1896 }
1897
1898 static void zread_ipv6_add(ZAPI_HANDLER_ARGS)
1899 {
1900 unsigned int i;
1901 struct stream *s;
1902 struct in6_addr nhop_addr;
1903 ifindex_t ifindex;
1904 struct route_entry *re;
1905 uint8_t message;
1906 uint8_t nexthop_num;
1907 uint8_t nexthop_type;
1908 struct prefix p;
1909 struct prefix_ipv6 src_p, *src_pp;
1910 safi_t safi;
1911 static struct in6_addr nexthops[MULTIPATH_NUM];
1912 static unsigned int ifindices[MULTIPATH_NUM];
1913 int ret;
1914 static mpls_label_t labels[MULTIPATH_NUM];
1915 enum lsp_types_t label_type = ZEBRA_LSP_NONE;
1916 mpls_label_t label;
1917 struct nexthop *nexthop;
1918 enum blackhole_type bh_type = BLACKHOLE_NULL;
1919
1920 /* Get input stream. */
1921 s = msg;
1922
1923 memset(&nhop_addr, 0, sizeof(struct in6_addr));
1924
1925 /* Allocate new re. */
1926 re = XCALLOC(MTYPE_RE, sizeof(struct route_entry));
1927
1928 /* Type, flags, message. */
1929 STREAM_GETC(s, re->type);
1930 if (re->type > ZEBRA_ROUTE_MAX) {
1931 zlog_warn("%s: Specified route type: %d is not a legal value\n",
1932 __PRETTY_FUNCTION__, re->type);
1933 XFREE(MTYPE_RE, re);
1934 return;
1935 }
1936 STREAM_GETW(s, re->instance);
1937 STREAM_GETL(s, re->flags);
1938 STREAM_GETC(s, message);
1939 STREAM_GETW(s, safi);
1940 re->uptime = time(NULL);
1941
1942 /* IPv6 prefix. */
1943 memset(&p, 0, sizeof(p));
1944 p.family = AF_INET6;
1945 STREAM_GETC(s, p.prefixlen);
1946 if (p.prefixlen > IPV6_MAX_BITLEN) {
1947 zlog_warn(
1948 "%s: Specified prefix length %d is to large for v6 prefix",
1949 __PRETTY_FUNCTION__, p.prefixlen);
1950 XFREE(MTYPE_RE, re);
1951 return;
1952 }
1953 STREAM_GET(&p.u.prefix6, s, PSIZE(p.prefixlen));
1954
1955 if (CHECK_FLAG(message, ZAPI_MESSAGE_SRCPFX)) {
1956 memset(&src_p, 0, sizeof(src_p));
1957 src_p.family = AF_INET6;
1958 STREAM_GETC(s, src_p.prefixlen);
1959 if (src_p.prefixlen > IPV6_MAX_BITLEN) {
1960 zlog_warn(
1961 "%s: Specified src prefix length %d is to large for v6 prefix",
1962 __PRETTY_FUNCTION__, src_p.prefixlen);
1963 XFREE(MTYPE_RE, re);
1964 return;
1965 }
1966 STREAM_GET(&src_p.prefix, s, PSIZE(src_p.prefixlen));
1967 src_pp = &src_p;
1968 } else
1969 src_pp = NULL;
1970
1971 /* VRF ID */
1972 re->vrf_id = zvrf_id(zvrf);
1973
1974 /* We need to give nh-addr, nh-ifindex with the same next-hop object
1975 * to the re to ensure that IPv6 multipathing works; need to coalesce
1976 * these. Clients should send the same number of paired set of
1977 * next-hop-addr/next-hop-ifindices. */
1978 if (CHECK_FLAG(message, ZAPI_MESSAGE_NEXTHOP)) {
1979 unsigned int nh_count = 0;
1980 unsigned int if_count = 0;
1981 unsigned int max_nh_if = 0;
1982
1983 STREAM_GETC(s, nexthop_num);
1984 zserv_nexthop_num_warn(__func__, (const struct prefix *)&p,
1985 nexthop_num);
1986
1987 if (CHECK_FLAG(message, ZAPI_MESSAGE_LABEL))
1988 label_type = lsp_type_from_re_type(client->proto);
1989
1990 for (i = 0; i < nexthop_num; i++) {
1991 STREAM_GETC(s, nexthop_type);
1992
1993 switch (nexthop_type) {
1994 case NEXTHOP_TYPE_IPV6:
1995 STREAM_GET(&nhop_addr, s, 16);
1996 if (nh_count < MULTIPATH_NUM) {
1997 /* For labeled-unicast, each nexthop is
1998 * followed by label. */
1999 if (CHECK_FLAG(message,
2000 ZAPI_MESSAGE_LABEL)) {
2001 STREAM_GETL(s, label);
2002 labels[nh_count] = label;
2003 }
2004 nexthops[nh_count++] = nhop_addr;
2005 }
2006 break;
2007 case NEXTHOP_TYPE_IPV6_IFINDEX:
2008 STREAM_GET(&nhop_addr, s, 16);
2009 STREAM_GETL(s, ifindex);
2010 route_entry_nexthop_ipv6_ifindex_add(
2011 re, &nhop_addr, ifindex, re->vrf_id);
2012 break;
2013 case NEXTHOP_TYPE_IFINDEX:
2014 if (if_count < multipath_num) {
2015 STREAM_GETL(s, ifindices[if_count++]);
2016 }
2017 break;
2018 case NEXTHOP_TYPE_BLACKHOLE:
2019 route_entry_nexthop_blackhole_add(re, bh_type);
2020 break;
2021 default:
2022 zlog_warn(
2023 "%s: Please use ZEBRA_ROUTE_ADD if you want to pass non v6 nexthops",
2024 __PRETTY_FUNCTION__);
2025 nexthops_free(re->ng.nexthop);
2026 XFREE(MTYPE_RE, re);
2027 return;
2028 }
2029 }
2030
2031 max_nh_if = (nh_count > if_count) ? nh_count : if_count;
2032 for (i = 0; i < max_nh_if; i++) {
2033 if ((i < nh_count)
2034 && !IN6_IS_ADDR_UNSPECIFIED(&nexthops[i])) {
2035 if ((i < if_count) && ifindices[i])
2036 nexthop =
2037 route_entry_nexthop_ipv6_ifindex_add(
2038 re, &nexthops[i],
2039 ifindices[i],
2040 re->vrf_id);
2041 else
2042 nexthop = route_entry_nexthop_ipv6_add(
2043 re, &nexthops[i], re->vrf_id);
2044 if (CHECK_FLAG(message, ZAPI_MESSAGE_LABEL))
2045 nexthop_add_labels(nexthop, label_type,
2046 1, &labels[i]);
2047 } else {
2048 if ((i < if_count) && ifindices[i])
2049 route_entry_nexthop_ifindex_add(
2050 re, ifindices[i], re->vrf_id);
2051 }
2052 }
2053 }
2054
2055 /* Distance. */
2056 if (CHECK_FLAG(message, ZAPI_MESSAGE_DISTANCE))
2057 STREAM_GETC(s, re->distance);
2058
2059 /* Metric. */
2060 if (CHECK_FLAG(message, ZAPI_MESSAGE_METRIC))
2061 STREAM_GETL(s, re->metric);
2062
2063 /* Tag */
2064 if (CHECK_FLAG(message, ZAPI_MESSAGE_TAG))
2065 STREAM_GETL(s, re->tag);
2066 else
2067 re->tag = 0;
2068
2069 if (CHECK_FLAG(message, ZAPI_MESSAGE_MTU))
2070 STREAM_GETL(s, re->mtu);
2071 else
2072 re->mtu = 0;
2073
2074 re->table = zvrf->table_id;
2075
2076 ret = rib_add_multipath(AFI_IP6, safi, &p, src_pp, re);
2077 /* Stats */
2078 if (ret > 0)
2079 client->v6_route_add_cnt++;
2080 else if (ret < 0)
2081 client->v6_route_upd8_cnt++;
2082
2083 return;
2084
2085 stream_failure:
2086 nexthops_free(re->ng.nexthop);
2087 XFREE(MTYPE_RE, re);
2088 }
2089
2090 /* Zebra server IPv6 prefix delete function. */
2091 static void zread_ipv6_delete(ZAPI_HANDLER_ARGS)
2092 {
2093 struct stream *s;
2094 struct zapi_ipv6 api;
2095 struct prefix p;
2096 struct prefix_ipv6 src_p, *src_pp;
2097
2098 s = msg;
2099
2100 /* Type, flags, message. */
2101 STREAM_GETC(s, api.type);
2102 STREAM_GETW(s, api.instance);
2103 STREAM_GETL(s, api.flags);
2104 STREAM_GETC(s, api.message);
2105 STREAM_GETW(s, api.safi);
2106
2107 /* IPv4 prefix. */
2108 memset(&p, 0, sizeof(struct prefix));
2109 p.family = AF_INET6;
2110 STREAM_GETC(s, p.prefixlen);
2111 STREAM_GET(&p.u.prefix6, s, PSIZE(p.prefixlen));
2112
2113 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_SRCPFX)) {
2114 memset(&src_p, 0, sizeof(struct prefix_ipv6));
2115 src_p.family = AF_INET6;
2116 STREAM_GETC(s, src_p.prefixlen);
2117 STREAM_GET(&src_p.prefix, s, PSIZE(src_p.prefixlen));
2118 src_pp = &src_p;
2119 } else
2120 src_pp = NULL;
2121
2122 rib_delete(AFI_IP6, api.safi, zvrf_id(zvrf), api.type, api.instance,
2123 api.flags, &p, src_pp, NULL, client->rtm_table, 0, false,
2124 NULL);
2125
2126 client->v6_route_del_cnt++;
2127
2128 stream_failure:
2129 return;
2130 }
2131
2132 /* Register zebra server router-id information. Send current router-id */
2133 static void zread_router_id_add(ZAPI_HANDLER_ARGS)
2134 {
2135 struct prefix p;
2136
2137 /* Router-id information is needed. */
2138 vrf_bitmap_set(client->ridinfo, zvrf_id(zvrf));
2139
2140 router_id_get(&p, zvrf_id(zvrf));
2141
2142 zsend_router_id_update(client, &p, zvrf_id(zvrf));
2143 }
2144
2145 /* Unregister zebra server router-id information. */
2146 static void zread_router_id_delete(ZAPI_HANDLER_ARGS)
2147 {
2148 vrf_bitmap_unset(client->ridinfo, zvrf_id(zvrf));
2149 }
2150
2151 static void zsend_capabilities(struct zserv *client, struct zebra_vrf *zvrf)
2152 {
2153 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
2154
2155 zclient_create_header(s, ZEBRA_CAPABILITIES, zvrf->vrf->vrf_id);
2156 stream_putc(s, mpls_enabled);
2157 stream_putl(s, multipath_num);
2158
2159 stream_putw_at(s, 0, stream_get_endp(s));
2160 zebra_server_send_message(client, s);
2161 }
2162
2163 /* Tie up route-type and client->sock */
2164 static void zread_hello(ZAPI_HANDLER_ARGS)
2165 {
2166 /* type of protocol (lib/zebra.h) */
2167 uint8_t proto;
2168 unsigned short instance;
2169 uint8_t notify;
2170
2171 STREAM_GETC(msg, proto);
2172 STREAM_GETW(msg, instance);
2173 STREAM_GETC(msg, notify);
2174 if (notify)
2175 client->notify_owner = true;
2176
2177 /* accept only dynamic routing protocols */
2178 if ((proto < ZEBRA_ROUTE_MAX) && (proto > ZEBRA_ROUTE_STATIC)) {
2179 zlog_notice(
2180 "client %d says hello and bids fair to announce only %s routes vrf=%u",
2181 client->sock, zebra_route_string(proto),
2182 zvrf->vrf->vrf_id);
2183 if (instance)
2184 zlog_notice("client protocol instance %d", instance);
2185
2186 client->proto = proto;
2187 client->instance = instance;
2188 }
2189
2190 zsend_capabilities(client, zvrf);
2191 stream_failure:
2192 return;
2193 }
2194
2195 /* Unregister all information in a VRF. */
2196 static void zread_vrf_unregister(ZAPI_HANDLER_ARGS)
2197 {
2198 int i;
2199 afi_t afi;
2200
2201 for (afi = AFI_IP; afi < AFI_MAX; afi++)
2202 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
2203 vrf_bitmap_unset(client->redist[afi][i], zvrf_id(zvrf));
2204 vrf_bitmap_unset(client->redist_default, zvrf_id(zvrf));
2205 vrf_bitmap_unset(client->ifinfo, zvrf_id(zvrf));
2206 vrf_bitmap_unset(client->ridinfo, zvrf_id(zvrf));
2207 }
2208
2209 static void zread_mpls_labels(ZAPI_HANDLER_ARGS)
2210 {
2211 struct stream *s;
2212 enum lsp_types_t type;
2213 struct prefix prefix;
2214 enum nexthop_types_t gtype;
2215 union g_addr gate;
2216 ifindex_t ifindex;
2217 mpls_label_t in_label, out_label;
2218 uint8_t distance;
2219
2220 /* Get input stream. */
2221 s = msg;
2222
2223 /* Get data. */
2224 STREAM_GETC(s, type);
2225 STREAM_GETL(s, prefix.family);
2226 switch (prefix.family) {
2227 case AF_INET:
2228 STREAM_GET(&prefix.u.prefix4.s_addr, s, IPV4_MAX_BYTELEN);
2229 STREAM_GETC(s, prefix.prefixlen);
2230 if (prefix.prefixlen > IPV4_MAX_BITLEN) {
2231 zlog_warn(
2232 "%s: Specified prefix length %d is greater than a v4 address can support",
2233 __PRETTY_FUNCTION__, prefix.prefixlen);
2234 return;
2235 }
2236 STREAM_GET(&gate.ipv4.s_addr, s, IPV4_MAX_BYTELEN);
2237 break;
2238 case AF_INET6:
2239 STREAM_GET(&prefix.u.prefix6, s, 16);
2240 STREAM_GETC(s, prefix.prefixlen);
2241 if (prefix.prefixlen > IPV6_MAX_BITLEN) {
2242 zlog_warn(
2243 "%s: Specified prefix length %d is greater than a v6 address can support",
2244 __PRETTY_FUNCTION__, prefix.prefixlen);
2245 return;
2246 }
2247 STREAM_GET(&gate.ipv6, s, 16);
2248 break;
2249 default:
2250 zlog_warn("%s: Specified AF %d is not supported for this call",
2251 __PRETTY_FUNCTION__, prefix.family);
2252 return;
2253 }
2254 STREAM_GETL(s, ifindex);
2255 STREAM_GETC(s, distance);
2256 STREAM_GETL(s, in_label);
2257 STREAM_GETL(s, out_label);
2258
2259 switch (prefix.family) {
2260 case AF_INET:
2261 if (ifindex)
2262 gtype = NEXTHOP_TYPE_IPV4_IFINDEX;
2263 else
2264 gtype = NEXTHOP_TYPE_IPV4;
2265 break;
2266 case AF_INET6:
2267 if (ifindex)
2268 gtype = NEXTHOP_TYPE_IPV6_IFINDEX;
2269 else
2270 gtype = NEXTHOP_TYPE_IPV6;
2271 break;
2272 default:
2273 return;
2274 }
2275
2276 if (!mpls_enabled)
2277 return;
2278
2279 if (hdr->command == ZEBRA_MPLS_LABELS_ADD) {
2280 mpls_lsp_install(zvrf, type, in_label, out_label, gtype, &gate,
2281 ifindex);
2282 mpls_ftn_update(1, zvrf, type, &prefix, gtype, &gate, ifindex,
2283 distance, out_label);
2284 } else if (hdr->command == ZEBRA_MPLS_LABELS_DELETE) {
2285 mpls_lsp_uninstall(zvrf, type, in_label, gtype, &gate, ifindex);
2286 mpls_ftn_update(0, zvrf, type, &prefix, gtype, &gate, ifindex,
2287 distance, out_label);
2288 }
2289 stream_failure:
2290 return;
2291 }
2292
2293 static int zsend_table_manager_connect_response(struct zserv *client,
2294 vrf_id_t vrf_id, uint16_t result)
2295 {
2296 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
2297
2298 zclient_create_header(s, ZEBRA_TABLE_MANAGER_CONNECT, vrf_id);
2299
2300 /* result */
2301 stream_putc(s, result);
2302
2303 stream_putw_at(s, 0, stream_get_endp(s));
2304
2305 return zebra_server_send_message(client, s);
2306 }
2307
2308 /* Send response to a table manager connect request to client */
2309 static void zread_table_manager_connect(struct zserv *client,
2310 struct stream *msg,
2311 vrf_id_t vrf_id)
2312 {
2313 struct stream *s;
2314 uint8_t proto;
2315 uint16_t instance;
2316
2317 s = msg;
2318
2319 /* Get data. */
2320 STREAM_GETC(s, proto);
2321 STREAM_GETW(s, instance);
2322
2323 /* accept only dynamic routing protocols */
2324 if ((proto >= ZEBRA_ROUTE_MAX) || (proto <= ZEBRA_ROUTE_STATIC)) {
2325 zlog_err("client %d has wrong protocol %s", client->sock,
2326 zebra_route_string(proto));
2327 zsend_table_manager_connect_response(client, vrf_id, 1);
2328 return;
2329 }
2330 zlog_notice("client %d with vrf %u instance %u connected as %s",
2331 client->sock, vrf_id, instance, zebra_route_string(proto));
2332 client->proto = proto;
2333 client->instance = instance;
2334
2335 /*
2336 * Release previous labels of same protocol and instance.
2337 * This is done in case it restarted from an unexpected shutdown.
2338 */
2339 release_daemon_table_chunks(proto, instance);
2340
2341 zsend_table_manager_connect_response(client, vrf_id, 0);
2342
2343 stream_failure:
2344 return;
2345 }
2346
2347 static void zread_label_manager_connect(struct zserv *client,
2348 struct stream *msg, vrf_id_t vrf_id)
2349 {
2350 struct stream *s;
2351 /* type of protocol (lib/zebra.h) */
2352 uint8_t proto;
2353 unsigned short instance;
2354
2355 /* Get input stream. */
2356 s = msg;
2357
2358 /* Get data. */
2359 STREAM_GETC(s, proto);
2360 STREAM_GETW(s, instance);
2361
2362 /* accept only dynamic routing protocols */
2363 if ((proto >= ZEBRA_ROUTE_MAX) || (proto <= ZEBRA_ROUTE_STATIC)) {
2364 zlog_err("client %d has wrong protocol %s", client->sock,
2365 zebra_route_string(proto));
2366 zsend_label_manager_connect_response(client, vrf_id, 1);
2367 return;
2368 }
2369 zlog_notice("client %d with vrf %u instance %u connected as %s",
2370 client->sock, vrf_id, instance, zebra_route_string(proto));
2371 client->proto = proto;
2372 client->instance = instance;
2373
2374 /*
2375 Release previous labels of same protocol and instance.
2376 This is done in case it restarted from an unexpected shutdown.
2377 */
2378 release_daemon_label_chunks(proto, instance);
2379
2380 zlog_debug(
2381 " Label Manager client connected: sock %d, proto %s, vrf %u instance %u",
2382 client->sock, zebra_route_string(proto), vrf_id, instance);
2383 /* send response back */
2384 zsend_label_manager_connect_response(client, vrf_id, 0);
2385
2386 stream_failure:
2387 return;
2388 }
2389
2390 static void zread_get_label_chunk(struct zserv *client, struct stream *msg,
2391 vrf_id_t vrf_id)
2392 {
2393 struct stream *s;
2394 uint8_t keep;
2395 uint32_t size;
2396 struct label_manager_chunk *lmc;
2397
2398 /* Get input stream. */
2399 s = msg;
2400
2401 /* Get data. */
2402 STREAM_GETC(s, keep);
2403 STREAM_GETL(s, size);
2404
2405 lmc = assign_label_chunk(client->proto, client->instance, keep, size);
2406 if (!lmc)
2407 zlog_err("%s: Unable to assign Label Chunk of size %u",
2408 __func__, size);
2409 else
2410 zlog_debug("Assigned Label Chunk %u - %u to %u", lmc->start,
2411 lmc->end, keep);
2412 /* send response back */
2413 zsend_assign_label_chunk_response(client, vrf_id, lmc);
2414
2415 stream_failure:
2416 return;
2417 }
2418
2419 static void zread_release_label_chunk(struct zserv *client, struct stream *msg)
2420 {
2421 struct stream *s;
2422 uint32_t start, end;
2423
2424 /* Get input stream. */
2425 s = msg;
2426
2427 /* Get data. */
2428 STREAM_GETL(s, start);
2429 STREAM_GETL(s, end);
2430
2431 release_label_chunk(client->proto, client->instance, start, end);
2432
2433 stream_failure:
2434 return;
2435 }
2436 static void zread_label_manager_request(ZAPI_HANDLER_ARGS)
2437 {
2438 /* to avoid sending other messages like ZERBA_INTERFACE_UP */
2439 if (hdr->command == ZEBRA_LABEL_MANAGER_CONNECT)
2440 client->is_synchronous = 1;
2441
2442 /* external label manager */
2443 if (lm_is_external)
2444 zread_relay_label_manager_request(hdr->command, client,
2445 zvrf_id(zvrf));
2446 /* this is a label manager */
2447 else {
2448 if (hdr->command == ZEBRA_LABEL_MANAGER_CONNECT)
2449 zread_label_manager_connect(client, msg, zvrf_id(zvrf));
2450 else {
2451 /* Sanity: don't allow 'unidentified' requests */
2452 if (!client->proto) {
2453 zlog_err(
2454 "Got label request from an unidentified client");
2455 return;
2456 }
2457 if (hdr->command == ZEBRA_GET_LABEL_CHUNK)
2458 zread_get_label_chunk(client, msg,
2459 zvrf_id(zvrf));
2460 else if (hdr->command == ZEBRA_RELEASE_LABEL_CHUNK)
2461 zread_release_label_chunk(client, msg);
2462 }
2463 }
2464 }
2465
2466 /* Send response to a get table chunk request to client */
2467 static int zsend_assign_table_chunk_response(struct zserv *client,
2468 vrf_id_t vrf_id,
2469 struct table_manager_chunk *tmc)
2470 {
2471 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
2472
2473 zclient_create_header(s, ZEBRA_GET_TABLE_CHUNK, vrf_id);
2474
2475 if (tmc) {
2476 /* start and end labels */
2477 stream_putl(s, tmc->start);
2478 stream_putl(s, tmc->end);
2479 }
2480
2481 /* Write packet size. */
2482 stream_putw_at(s, 0, stream_get_endp(s));
2483
2484 return zebra_server_send_message(client, s);
2485 }
2486
2487 static void zread_get_table_chunk(struct zserv *client, struct stream *msg,
2488 vrf_id_t vrf_id)
2489 {
2490 struct stream *s;
2491 uint32_t size;
2492 struct table_manager_chunk *tmc;
2493
2494 /* Get input stream. */
2495 s = msg;
2496
2497 /* Get data. */
2498 STREAM_GETL(s, size);
2499
2500 tmc = assign_table_chunk(client->proto, client->instance, size);
2501 if (!tmc)
2502 zlog_err("%s: Unable to assign Table Chunk of size %u",
2503 __func__, size);
2504 else
2505 zlog_debug("Assigned Table Chunk %u - %u", tmc->start,
2506 tmc->end);
2507 /* send response back */
2508 zsend_assign_table_chunk_response(client, vrf_id, tmc);
2509
2510 stream_failure:
2511 return;
2512 }
2513
2514 static void zread_release_table_chunk(struct zserv *client, struct stream *msg)
2515 {
2516 struct stream *s;
2517 uint32_t start, end;
2518
2519 /* Get input stream. */
2520 s = msg;
2521
2522 /* Get data. */
2523 STREAM_GETL(s, start);
2524 STREAM_GETL(s, end);
2525
2526 release_table_chunk(client->proto, client->instance, start, end);
2527
2528 stream_failure:
2529 return;
2530 }
2531
2532 static void zread_table_manager_request(ZAPI_HANDLER_ARGS)
2533 {
2534 /* to avoid sending other messages like ZERBA_INTERFACE_UP */
2535 if (hdr->command == ZEBRA_TABLE_MANAGER_CONNECT)
2536 zread_table_manager_connect(client, msg, zvrf_id(zvrf));
2537 else {
2538 /* Sanity: don't allow 'unidentified' requests */
2539 if (!client->proto) {
2540 zlog_err(
2541 "Got table request from an unidentified client");
2542 return;
2543 }
2544 if (hdr->command == ZEBRA_GET_TABLE_CHUNK)
2545 zread_get_table_chunk(client, msg,
2546 zvrf_id(zvrf));
2547 else if (hdr->command == ZEBRA_RELEASE_TABLE_CHUNK)
2548 zread_release_table_chunk(client, msg);
2549 }
2550 }
2551
2552 static void zread_pseudowire(ZAPI_HANDLER_ARGS)
2553 {
2554 struct stream *s;
2555 char ifname[IF_NAMESIZE];
2556 ifindex_t ifindex;
2557 int type;
2558 int af;
2559 union g_addr nexthop;
2560 uint32_t local_label;
2561 uint32_t remote_label;
2562 uint8_t flags;
2563 union pw_protocol_fields data;
2564 uint8_t protocol;
2565 struct zebra_pw *pw;
2566
2567 /* Get input stream. */
2568 s = msg;
2569
2570 /* Get data. */
2571 STREAM_GET(ifname, s, IF_NAMESIZE);
2572 STREAM_GETL(s, ifindex);
2573 STREAM_GETL(s, type);
2574 STREAM_GETL(s, af);
2575 switch (af) {
2576 case AF_INET:
2577 STREAM_GET(&nexthop.ipv4.s_addr, s, IPV4_MAX_BYTELEN);
2578 break;
2579 case AF_INET6:
2580 STREAM_GET(&nexthop.ipv6, s, 16);
2581 break;
2582 default:
2583 return;
2584 }
2585 STREAM_GETL(s, local_label);
2586 STREAM_GETL(s, remote_label);
2587 STREAM_GETC(s, flags);
2588 STREAM_GET(&data, s, sizeof(data));
2589 protocol = client->proto;
2590
2591 pw = zebra_pw_find(zvrf, ifname);
2592 switch (hdr->command) {
2593 case ZEBRA_PW_ADD:
2594 if (pw) {
2595 zlog_warn("%s: pseudowire %s already exists [%s]",
2596 __func__, ifname,
2597 zserv_command_string(hdr->command));
2598 return;
2599 }
2600
2601 zebra_pw_add(zvrf, ifname, protocol, client);
2602 break;
2603 case ZEBRA_PW_DELETE:
2604 if (!pw) {
2605 zlog_warn("%s: pseudowire %s not found [%s]", __func__,
2606 ifname, zserv_command_string(hdr->command));
2607 return;
2608 }
2609
2610 zebra_pw_del(zvrf, pw);
2611 break;
2612 case ZEBRA_PW_SET:
2613 case ZEBRA_PW_UNSET:
2614 if (!pw) {
2615 zlog_warn("%s: pseudowire %s not found [%s]", __func__,
2616 ifname, zserv_command_string(hdr->command));
2617 return;
2618 }
2619
2620 switch (hdr->command) {
2621 case ZEBRA_PW_SET:
2622 pw->enabled = 1;
2623 break;
2624 case ZEBRA_PW_UNSET:
2625 pw->enabled = 0;
2626 break;
2627 }
2628
2629 zebra_pw_change(pw, ifindex, type, af, &nexthop, local_label,
2630 remote_label, flags, &data);
2631 break;
2632 }
2633
2634 stream_failure:
2635 return;
2636 }
2637
2638 /* Cleanup registered nexthops (across VRFs) upon client disconnect. */
2639 static void zebra_client_close_cleanup_rnh(struct zserv *client)
2640 {
2641 struct vrf *vrf;
2642 struct zebra_vrf *zvrf;
2643
2644 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) {
2645 if ((zvrf = vrf->info) != NULL) {
2646 zebra_cleanup_rnh_client(zvrf_id(zvrf), AF_INET, client,
2647 RNH_NEXTHOP_TYPE);
2648 zebra_cleanup_rnh_client(zvrf_id(zvrf), AF_INET6,
2649 client, RNH_NEXTHOP_TYPE);
2650 zebra_cleanup_rnh_client(zvrf_id(zvrf), AF_INET, client,
2651 RNH_IMPORT_CHECK_TYPE);
2652 zebra_cleanup_rnh_client(zvrf_id(zvrf), AF_INET6,
2653 client, RNH_IMPORT_CHECK_TYPE);
2654 if (client->proto == ZEBRA_ROUTE_LDP) {
2655 hash_iterate(zvrf->lsp_table,
2656 mpls_ldp_lsp_uninstall_all,
2657 zvrf->lsp_table);
2658 mpls_ldp_ftn_uninstall_all(zvrf, AFI_IP);
2659 mpls_ldp_ftn_uninstall_all(zvrf, AFI_IP6);
2660 }
2661 }
2662 }
2663 }
2664
2665 static void zread_interface_set_master(ZAPI_HANDLER_ARGS)
2666 {
2667 struct interface *master;
2668 struct interface *slave;
2669 struct stream *s = msg;
2670 int ifindex;
2671 vrf_id_t vrf_id;
2672
2673 STREAM_GETL(s, vrf_id);
2674 STREAM_GETL(s, ifindex);
2675 master = if_lookup_by_index(ifindex, vrf_id);
2676
2677 STREAM_GETL(s, vrf_id);
2678 STREAM_GETL(s, ifindex);
2679 slave = if_lookup_by_index(ifindex, vrf_id);
2680
2681 if (!master || !slave)
2682 return;
2683
2684 kernel_interface_set_master(master, slave);
2685
2686 stream_failure:
2687 return;
2688 }
2689
2690
2691 static void zread_vrf_label(ZAPI_HANDLER_ARGS)
2692 {
2693 struct interface *ifp;
2694 mpls_label_t nlabel;
2695 afi_t afi;
2696 struct stream *s;
2697 struct zebra_vrf *def_zvrf;
2698 enum lsp_types_t ltype;
2699
2700 s = msg;
2701 STREAM_GETL(s, nlabel);
2702 STREAM_GETC(s, afi);
2703 if (nlabel == zvrf->label[afi]) {
2704 /*
2705 * Nothing to do here move along
2706 */
2707 return;
2708 }
2709
2710 STREAM_GETC(s, ltype);
2711
2712 if (zvrf->vrf->vrf_id != VRF_DEFAULT)
2713 ifp = if_lookup_by_name(zvrf->vrf->name, zvrf->vrf->vrf_id);
2714 else
2715 ifp = if_lookup_by_name("lo", VRF_DEFAULT);
2716
2717 if (!ifp) {
2718 zlog_debug("Unable to find specified Interface for %s",
2719 zvrf->vrf->name);
2720 return;
2721 }
2722
2723 def_zvrf = zebra_vrf_lookup_by_id(VRF_DEFAULT);
2724
2725 if (zvrf->label[afi] != MPLS_LABEL_NONE) {
2726 afi_t scrubber;
2727 bool really_remove;
2728
2729 really_remove = true;
2730 for (scrubber = AFI_IP; scrubber < AFI_MAX; scrubber++) {
2731 if (scrubber == afi)
2732 continue;
2733
2734 if (zvrf->label[scrubber] == MPLS_LABEL_NONE)
2735 continue;
2736
2737 if (zvrf->label[afi] == zvrf->label[scrubber]) {
2738 really_remove = false;
2739 break;
2740 }
2741 }
2742
2743 if (really_remove)
2744 mpls_lsp_uninstall(def_zvrf, ltype, zvrf->label[afi],
2745 NEXTHOP_TYPE_IFINDEX, NULL,
2746 ifp->ifindex);
2747 }
2748
2749 if (nlabel != MPLS_LABEL_NONE)
2750 mpls_lsp_install(def_zvrf, ltype, nlabel,
2751 MPLS_LABEL_IMPLICIT_NULL, NEXTHOP_TYPE_IFINDEX,
2752 NULL, ifp->ifindex);
2753
2754 zvrf->label[afi] = nlabel;
2755 stream_failure:
2756 return;
2757 }
2758
2759 static inline void zread_rule(ZAPI_HANDLER_ARGS)
2760 {
2761 struct zebra_pbr_rule zpr;
2762 struct stream *s;
2763 uint32_t total, i;
2764 ifindex_t ifindex;
2765
2766 s = msg;
2767 STREAM_GETL(s, total);
2768
2769 for (i = 0; i < total; i++) {
2770 memset(&zpr, 0, sizeof(zpr));
2771
2772 zpr.sock = client->sock;
2773 zpr.rule.vrf_id = hdr->vrf_id;
2774 STREAM_GETL(s, zpr.rule.seq);
2775 STREAM_GETL(s, zpr.rule.priority);
2776 STREAM_GETL(s, zpr.rule.unique);
2777 STREAM_GETC(s, zpr.rule.filter.src_ip.family);
2778 STREAM_GETC(s, zpr.rule.filter.src_ip.prefixlen);
2779 STREAM_GET(&zpr.rule.filter.src_ip.u.prefix, s,
2780 prefix_blen(&zpr.rule.filter.src_ip));
2781 STREAM_GETW(s, zpr.rule.filter.src_port);
2782 STREAM_GETC(s, zpr.rule.filter.dst_ip.family);
2783 STREAM_GETC(s, zpr.rule.filter.dst_ip.prefixlen);
2784 STREAM_GET(&zpr.rule.filter.dst_ip.u.prefix, s,
2785 prefix_blen(&zpr.rule.filter.dst_ip));
2786 STREAM_GETW(s, zpr.rule.filter.dst_port);
2787 STREAM_GETL(s, zpr.rule.filter.fwmark);
2788 STREAM_GETL(s, zpr.rule.action.table);
2789 STREAM_GETL(s, ifindex);
2790
2791 if (ifindex) {
2792 zpr.ifp = if_lookup_by_index(ifindex, VRF_UNKNOWN);
2793 if (!zpr.ifp) {
2794 zlog_debug("Failed to lookup ifindex: %u",
2795 ifindex);
2796 return;
2797 }
2798 }
2799
2800 if (!is_default_prefix(&zpr.rule.filter.src_ip))
2801 zpr.rule.filter.filter_bm |= PBR_FILTER_SRC_IP;
2802
2803 if (!is_default_prefix(&zpr.rule.filter.dst_ip))
2804 zpr.rule.filter.filter_bm |= PBR_FILTER_DST_IP;
2805
2806 if (zpr.rule.filter.src_port)
2807 zpr.rule.filter.filter_bm |= PBR_FILTER_SRC_PORT;
2808
2809 if (zpr.rule.filter.dst_port)
2810 zpr.rule.filter.filter_bm |= PBR_FILTER_DST_PORT;
2811
2812 if (zpr.rule.filter.fwmark)
2813 zpr.rule.filter.filter_bm |= PBR_FILTER_FWMARK;
2814
2815 if (hdr->command == ZEBRA_RULE_ADD)
2816 zebra_pbr_add_rule(zvrf->zns, &zpr);
2817 else
2818 zebra_pbr_del_rule(zvrf->zns, &zpr);
2819 }
2820
2821 stream_failure:
2822 return;
2823 }
2824
2825
2826 static inline void zread_ipset(ZAPI_HANDLER_ARGS)
2827 {
2828 struct zebra_pbr_ipset zpi;
2829 struct stream *s;
2830 uint32_t total, i;
2831
2832 s = msg;
2833 STREAM_GETL(s, total);
2834
2835 for (i = 0; i < total; i++) {
2836 memset(&zpi, 0, sizeof(zpi));
2837
2838 zpi.sock = client->sock;
2839 STREAM_GETL(s, zpi.unique);
2840 STREAM_GETL(s, zpi.type);
2841 STREAM_GET(&zpi.ipset_name, s,
2842 ZEBRA_IPSET_NAME_SIZE);
2843
2844 if (hdr->command == ZEBRA_IPSET_CREATE)
2845 zebra_pbr_create_ipset(zvrf->zns, &zpi);
2846 else
2847 zebra_pbr_destroy_ipset(zvrf->zns, &zpi);
2848 }
2849
2850 stream_failure:
2851 return;
2852 }
2853
2854 static inline void zread_ipset_entry(ZAPI_HANDLER_ARGS)
2855 {
2856 struct zebra_pbr_ipset_entry zpi;
2857 struct zebra_pbr_ipset ipset;
2858 struct stream *s;
2859 uint32_t total, i;
2860
2861 s = msg;
2862 STREAM_GETL(s, total);
2863
2864 for (i = 0; i < total; i++) {
2865 memset(&zpi, 0, sizeof(zpi));
2866 memset(&ipset, 0, sizeof(ipset));
2867
2868 zpi.sock = client->sock;
2869 STREAM_GETL(s, zpi.unique);
2870 STREAM_GET(&ipset.ipset_name, s,
2871 ZEBRA_IPSET_NAME_SIZE);
2872 STREAM_GETC(s, zpi.src.family);
2873 STREAM_GETC(s, zpi.src.prefixlen);
2874 STREAM_GET(&zpi.src.u.prefix, s,
2875 prefix_blen(&zpi.src));
2876 STREAM_GETC(s, zpi.dst.family);
2877 STREAM_GETC(s, zpi.dst.prefixlen);
2878 STREAM_GET(&zpi.dst.u.prefix, s,
2879 prefix_blen(&zpi.dst));
2880
2881 if (!is_default_prefix(&zpi.src))
2882 zpi.filter_bm |= PBR_FILTER_SRC_IP;
2883
2884 if (!is_default_prefix(&zpi.dst))
2885 zpi.filter_bm |= PBR_FILTER_DST_IP;
2886
2887 /* calculate backpointer */
2888 zpi.backpointer = zebra_pbr_lookup_ipset_pername(zvrf->zns,
2889 ipset.ipset_name);
2890 if (hdr->command == ZEBRA_IPSET_ENTRY_ADD)
2891 zebra_pbr_add_ipset_entry(zvrf->zns, &zpi);
2892 else
2893 zebra_pbr_del_ipset_entry(zvrf->zns, &zpi);
2894 }
2895
2896 stream_failure:
2897 return;
2898 }
2899
2900 static inline void zread_iptable(ZAPI_HANDLER_ARGS)
2901 {
2902 struct zebra_pbr_iptable zpi;
2903 struct stream *s;
2904
2905 s = msg;
2906
2907 memset(&zpi, 0, sizeof(zpi));
2908
2909 zpi.sock = client->sock;
2910 STREAM_GETL(s, zpi.unique);
2911 STREAM_GETL(s, zpi.type);
2912 STREAM_GETL(s, zpi.filter_bm);
2913 STREAM_GETL(s, zpi.action);
2914 STREAM_GETL(s, zpi.fwmark);
2915 STREAM_GET(&zpi.ipset_name, s,
2916 ZEBRA_IPSET_NAME_SIZE);
2917
2918 if (hdr->command == ZEBRA_IPTABLE_ADD)
2919 zebra_pbr_add_iptable(zvrf->zns, &zpi);
2920 else
2921 zebra_pbr_del_iptable(zvrf->zns, &zpi);
2922 stream_failure:
2923 return;
2924 }
2925
2926 void (*zserv_handlers[])(ZAPI_HANDLER_ARGS) = {
2927 [ZEBRA_ROUTER_ID_ADD] = zread_router_id_add,
2928 [ZEBRA_ROUTER_ID_DELETE] = zread_router_id_delete,
2929 [ZEBRA_INTERFACE_ADD] = zread_interface_add,
2930 [ZEBRA_INTERFACE_DELETE] = zread_interface_delete,
2931 [ZEBRA_ROUTE_ADD] = zread_route_add,
2932 [ZEBRA_ROUTE_DELETE] = zread_route_del,
2933 [ZEBRA_IPV4_ROUTE_ADD] = zread_ipv4_add,
2934 [ZEBRA_IPV4_ROUTE_DELETE] = zread_ipv4_delete,
2935 [ZEBRA_IPV4_ROUTE_IPV6_NEXTHOP_ADD] = zread_ipv4_route_ipv6_nexthop_add,
2936 [ZEBRA_IPV6_ROUTE_ADD] = zread_ipv6_add,
2937 [ZEBRA_IPV6_ROUTE_DELETE] = zread_ipv6_delete,
2938 [ZEBRA_REDISTRIBUTE_ADD] = zebra_redistribute_add,
2939 [ZEBRA_REDISTRIBUTE_DELETE] = zebra_redistribute_delete,
2940 [ZEBRA_REDISTRIBUTE_DEFAULT_ADD] = zebra_redistribute_default_add,
2941 [ZEBRA_REDISTRIBUTE_DEFAULT_DELETE] = zebra_redistribute_default_delete,
2942 [ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB] = zread_ipv4_nexthop_lookup_mrib,
2943 [ZEBRA_HELLO] = zread_hello,
2944 [ZEBRA_NEXTHOP_REGISTER] = zread_rnh_register,
2945 [ZEBRA_NEXTHOP_UNREGISTER] = zread_rnh_unregister,
2946 [ZEBRA_IMPORT_ROUTE_REGISTER] = zread_rnh_register,
2947 [ZEBRA_IMPORT_ROUTE_UNREGISTER] = zread_rnh_unregister,
2948 [ZEBRA_BFD_DEST_UPDATE] = zebra_ptm_bfd_dst_register,
2949 [ZEBRA_BFD_DEST_REGISTER] = zebra_ptm_bfd_dst_register,
2950 [ZEBRA_BFD_DEST_DEREGISTER] = zebra_ptm_bfd_dst_deregister,
2951 [ZEBRA_VRF_UNREGISTER] = zread_vrf_unregister,
2952 [ZEBRA_VRF_LABEL] = zread_vrf_label,
2953 [ZEBRA_BFD_CLIENT_REGISTER] = zebra_ptm_bfd_client_register,
2954 #if defined(HAVE_RTADV)
2955 [ZEBRA_INTERFACE_ENABLE_RADV] = zebra_interface_radv_enable,
2956 [ZEBRA_INTERFACE_DISABLE_RADV] = zebra_interface_radv_disable,
2957 #else
2958 [ZEBRA_INTERFACE_ENABLE_RADV] = NULL,
2959 [ZEBRA_INTERFACE_DISABLE_RADV] = NULL,
2960 #endif
2961 [ZEBRA_MPLS_LABELS_ADD] = zread_mpls_labels,
2962 [ZEBRA_MPLS_LABELS_DELETE] = zread_mpls_labels,
2963 [ZEBRA_IPMR_ROUTE_STATS] = zebra_ipmr_route_stats,
2964 [ZEBRA_LABEL_MANAGER_CONNECT] = zread_label_manager_request,
2965 [ZEBRA_GET_LABEL_CHUNK] = zread_label_manager_request,
2966 [ZEBRA_RELEASE_LABEL_CHUNK] = zread_label_manager_request,
2967 [ZEBRA_FEC_REGISTER] = zread_fec_register,
2968 [ZEBRA_FEC_UNREGISTER] = zread_fec_unregister,
2969 [ZEBRA_ADVERTISE_DEFAULT_GW] = zebra_vxlan_advertise_gw_macip,
2970 [ZEBRA_ADVERTISE_SUBNET] = zebra_vxlan_advertise_subnet,
2971 [ZEBRA_ADVERTISE_ALL_VNI] = zebra_vxlan_advertise_all_vni,
2972 [ZEBRA_REMOTE_VTEP_ADD] = zebra_vxlan_remote_vtep_add,
2973 [ZEBRA_REMOTE_VTEP_DEL] = zebra_vxlan_remote_vtep_del,
2974 [ZEBRA_REMOTE_MACIP_ADD] = zebra_vxlan_remote_macip_add,
2975 [ZEBRA_REMOTE_MACIP_DEL] = zebra_vxlan_remote_macip_del,
2976 [ZEBRA_INTERFACE_SET_MASTER] = zread_interface_set_master,
2977 [ZEBRA_PW_ADD] = zread_pseudowire,
2978 [ZEBRA_PW_DELETE] = zread_pseudowire,
2979 [ZEBRA_PW_SET] = zread_pseudowire,
2980 [ZEBRA_PW_UNSET] = zread_pseudowire,
2981 [ZEBRA_RULE_ADD] = zread_rule,
2982 [ZEBRA_RULE_DELETE] = zread_rule,
2983 [ZEBRA_TABLE_MANAGER_CONNECT] = zread_table_manager_request,
2984 [ZEBRA_GET_TABLE_CHUNK] = zread_table_manager_request,
2985 [ZEBRA_RELEASE_TABLE_CHUNK] = zread_table_manager_request,
2986 [ZEBRA_IPSET_CREATE] = zread_ipset,
2987 [ZEBRA_IPSET_DESTROY] = zread_ipset,
2988 [ZEBRA_IPSET_ENTRY_ADD] = zread_ipset_entry,
2989 [ZEBRA_IPSET_ENTRY_DELETE] = zread_ipset_entry,
2990 [ZEBRA_IPTABLE_ADD] = zread_iptable,
2991 [ZEBRA_IPTABLE_DELETE] = zread_iptable,
2992 };
2993
2994 static inline void zserv_handle_commands(struct zserv *client,
2995 struct zmsghdr *hdr,
2996 struct stream *msg,
2997 struct zebra_vrf *zvrf)
2998 {
2999 if (hdr->command > array_size(zserv_handlers)
3000 || zserv_handlers[hdr->command] == NULL)
3001 zlog_info("Zebra received unknown command %d", hdr->command);
3002 else
3003 zserv_handlers[hdr->command](client, hdr, msg, zvrf);
3004
3005 stream_free(msg);
3006 }
3007
3008 /* Lifecycle ---------------------------------------------------------------- */
3009
3010 /* free zebra client information. */
3011 static void zebra_client_free(struct zserv *client)
3012 {
3013 /* Send client de-registration to BFD */
3014 zebra_ptm_bfd_client_deregister(client->proto);
3015
3016 /* Cleanup any rules installed from this client */
3017 zebra_pbr_client_close_cleanup(client->sock);
3018
3019 /* Cleanup any registered nexthops - across all VRFs. */
3020 zebra_client_close_cleanup_rnh(client);
3021
3022 /* Release Label Manager chunks */
3023 release_daemon_label_chunks(client->proto, client->instance);
3024
3025 /* Release Table Manager chunks */
3026 release_daemon_table_chunks(client->proto, client->instance);
3027
3028 /* Cleanup any FECs registered by this client. */
3029 zebra_mpls_cleanup_fecs_for_client(vrf_info_lookup(VRF_DEFAULT),
3030 client);
3031
3032 /* Remove pseudowires associated with this client */
3033 zebra_pw_client_close(client);
3034
3035 /* Close file descriptor. */
3036 if (client->sock) {
3037 unsigned long nroutes;
3038
3039 close(client->sock);
3040 nroutes = rib_score_proto(client->proto, client->instance);
3041 zlog_notice(
3042 "client %d disconnected. %lu %s routes removed from the rib",
3043 client->sock, nroutes,
3044 zebra_route_string(client->proto));
3045 client->sock = -1;
3046 }
3047
3048 /* Free stream buffers. */
3049 if (client->ibuf_work)
3050 stream_free(client->ibuf_work);
3051 if (client->obuf_work)
3052 stream_free(client->obuf_work);
3053 if (client->ibuf_fifo)
3054 stream_fifo_free(client->ibuf_fifo);
3055 if (client->obuf_fifo)
3056 stream_fifo_free(client->obuf_fifo);
3057 if (client->wb)
3058 buffer_free(client->wb);
3059
3060 /* Release threads. */
3061 if (client->t_read)
3062 thread_cancel(client->t_read);
3063 if (client->t_write)
3064 thread_cancel(client->t_write);
3065 if (client->t_suicide)
3066 thread_cancel(client->t_suicide);
3067
3068 /* Free bitmaps. */
3069 for (afi_t afi = AFI_IP; afi < AFI_MAX; afi++)
3070 for (int i = 0; i < ZEBRA_ROUTE_MAX; i++)
3071 vrf_bitmap_free(client->redist[afi][i]);
3072
3073 vrf_bitmap_free(client->redist_default);
3074 vrf_bitmap_free(client->ifinfo);
3075 vrf_bitmap_free(client->ridinfo);
3076
3077 XFREE(MTYPE_TMP, client);
3078 }
3079
3080 /*
3081 * Called from client thread to terminate itself.
3082 */
3083 static void zebra_client_close(struct zserv *client)
3084 {
3085 listnode_delete(zebrad.client_list, client);
3086 zebra_client_free(client);
3087 }
3088
3089 /* Make new client. */
3090 static void zebra_client_create(int sock)
3091 {
3092 struct zserv *client;
3093 int i;
3094 afi_t afi;
3095
3096 client = XCALLOC(MTYPE_TMP, sizeof(struct zserv));
3097
3098 /* Make client input/output buffer. */
3099 client->sock = sock;
3100 client->ibuf_fifo = stream_fifo_new();
3101 client->obuf_fifo = stream_fifo_new();
3102 client->ibuf_work = stream_new(ZEBRA_MAX_PACKET_SIZ);
3103 client->obuf_work = stream_new(ZEBRA_MAX_PACKET_SIZ);
3104 client->wb = buffer_new(0);
3105
3106 /* Set table number. */
3107 client->rtm_table = zebrad.rtm_table_default;
3108
3109 client->connect_time = monotime(NULL);
3110 /* Initialize flags */
3111 for (afi = AFI_IP; afi < AFI_MAX; afi++)
3112 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
3113 client->redist[afi][i] = vrf_bitmap_init();
3114 client->redist_default = vrf_bitmap_init();
3115 client->ifinfo = vrf_bitmap_init();
3116 client->ridinfo = vrf_bitmap_init();
3117
3118 /* by default, it's not a synchronous client */
3119 client->is_synchronous = 0;
3120
3121 /* Add this client to linked list. */
3122 listnode_add(zebrad.client_list, client);
3123
3124 zebra_vrf_update_all(client);
3125
3126 /* start read loop */
3127 zebra_event(client, ZEBRA_READ);
3128 }
3129
3130 static int zserv_delayed_close(struct thread *thread)
3131 {
3132 struct zserv *client = THREAD_ARG(thread);
3133
3134 client->t_suicide = NULL;
3135 zebra_client_close(client);
3136 return 0;
3137 }
3138
3139 /*
3140 * Log zapi message to zlog.
3141 *
3142 * errmsg (optional)
3143 * Debugging message
3144 *
3145 * msg
3146 * The message
3147 *
3148 * hdr (optional)
3149 * The message header
3150 */
3151 static void zserv_log_message(const char *errmsg, struct stream *msg,
3152 struct zmsghdr *hdr)
3153 {
3154 zlog_debug("Rx'd ZAPI message");
3155 if (errmsg)
3156 zlog_debug("%s", errmsg);
3157 if (hdr) {
3158 zlog_debug(" Length: %d", hdr->length);
3159 zlog_debug("Command: %s", zserv_command_string(hdr->command));
3160 zlog_debug(" VRF: %u", hdr->vrf_id);
3161 }
3162 zlog_hexdump(msg->data, STREAM_READABLE(msg));
3163 }
3164
3165 static int zserv_flush_data(struct thread *thread)
3166 {
3167 struct zserv *client = THREAD_ARG(thread);
3168
3169 client->t_write = NULL;
3170 if (client->t_suicide) {
3171 zebra_client_close(client);
3172 return -1;
3173 }
3174 switch (buffer_flush_available(client->wb, client->sock)) {
3175 case BUFFER_ERROR:
3176 zlog_warn(
3177 "%s: buffer_flush_available failed on zserv client fd %d, closing",
3178 __func__, client->sock);
3179 zebra_client_close(client);
3180 client = NULL;
3181 break;
3182 case BUFFER_PENDING:
3183 client->t_write = NULL;
3184 thread_add_write(zebrad.master, zserv_flush_data, client,
3185 client->sock, &client->t_write);
3186 break;
3187 case BUFFER_EMPTY:
3188 break;
3189 }
3190
3191 if (client)
3192 client->last_write_time = monotime(NULL);
3193 return 0;
3194 }
3195
3196 /*
3197 * Write a single packet.
3198 */
3199 static int zserv_write(struct thread *thread)
3200 {
3201 struct zserv *client = THREAD_ARG(thread);
3202 struct stream *msg;
3203 int writerv;
3204
3205 if (client->t_suicide)
3206 return -1;
3207
3208 if (client->is_synchronous)
3209 return 0;
3210
3211 msg = stream_fifo_pop(client->obuf_fifo);
3212 stream_set_getp(msg, 0);
3213 client->last_write_cmd = stream_getw_from(msg, 6);
3214
3215 writerv = buffer_write(client->wb, client->sock, STREAM_DATA(msg),
3216 stream_get_endp(msg));
3217
3218 stream_free(msg);
3219
3220 switch (writerv) {
3221 case BUFFER_ERROR:
3222 zlog_warn(
3223 "%s: buffer_write failed to zserv client fd %d, closing",
3224 __func__, client->sock);
3225 /*
3226 * Schedule a delayed close since many of the functions that
3227 * call this one do not check the return code. They do not
3228 * allow for the possibility that an I/O error may have caused
3229 * the client to be deleted.
3230 */
3231 client->t_suicide = NULL;
3232 thread_add_event(zebrad.master, zserv_delayed_close, client, 0,
3233 &client->t_suicide);
3234 return -1;
3235 case BUFFER_EMPTY:
3236 THREAD_OFF(client->t_write);
3237 break;
3238 case BUFFER_PENDING:
3239 thread_add_write(zebrad.master, zserv_flush_data, client,
3240 client->sock, &client->t_write);
3241 break;
3242 }
3243
3244 if (client->obuf_fifo->count)
3245 zebra_event(client, ZEBRA_WRITE);
3246
3247 client->last_write_time = monotime(NULL);
3248 return 0;
3249 }
3250
3251 #if defined(HANDLE_ZAPI_FUZZING)
3252 static void zserv_write_incoming(struct stream *orig, uint16_t command)
3253 {
3254 char fname[MAXPATHLEN];
3255 struct stream *copy;
3256 int fd = -1;
3257
3258 copy = stream_dup(orig);
3259 stream_set_getp(copy, 0);
3260
3261 zserv_privs.change(ZPRIVS_RAISE);
3262 snprintf(fname, MAXPATHLEN, "%s/%u", DAEMON_VTY_DIR, command);
3263 fd = open(fname, O_CREAT | O_WRONLY | O_EXCL, 0644);
3264 stream_flush(copy, fd);
3265 close(fd);
3266 zserv_privs.change(ZPRIVS_LOWER);
3267 stream_free(copy);
3268 }
3269 #endif
3270
3271 static int zserv_process_messages(struct thread *thread)
3272 {
3273 struct zserv *client = THREAD_ARG(thread);
3274 struct zebra_vrf *zvrf;
3275 struct zmsghdr hdr;
3276 struct stream *msg;
3277 bool hdrvalid;
3278
3279 do {
3280 msg = stream_fifo_pop(client->ibuf_fifo);
3281
3282 /* break if out of messages */
3283 if (!msg)
3284 continue;
3285
3286 /* read & check header */
3287 hdrvalid = zapi_parse_header(msg, &hdr);
3288 if (!hdrvalid && IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV) {
3289 const char *emsg = "Message has corrupt header";
3290 zserv_log_message(emsg, msg, NULL);
3291 }
3292 if (!hdrvalid)
3293 continue;
3294
3295 hdr.length -= ZEBRA_HEADER_SIZE;
3296 /* lookup vrf */
3297 zvrf = zebra_vrf_lookup_by_id(hdr.vrf_id);
3298 if (!zvrf && IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV) {
3299 const char *emsg = "Message specifies unknown VRF";
3300 zserv_log_message(emsg, msg, &hdr);
3301 }
3302 if (!zvrf)
3303 continue;
3304
3305 /* process commands */
3306 zserv_handle_commands(client, &hdr, msg, zvrf);
3307
3308 } while (msg);
3309
3310 return 0;
3311 }
3312
3313 /* Handler of zebra service request. */
3314 static int zserv_read(struct thread *thread)
3315 {
3316 int sock;
3317 struct zserv *client;
3318 size_t already;
3319 #if defined(HANDLE_ZAPI_FUZZING)
3320 int packets = 1;
3321 #else
3322 int packets = zebrad.packets_to_process;
3323 #endif
3324 /* Get thread data. Reset reading thread because I'm running. */
3325 sock = THREAD_FD(thread);
3326 client = THREAD_ARG(thread);
3327
3328 if (client->t_suicide) {
3329 zebra_client_close(client);
3330 return -1;
3331 }
3332
3333 while (packets) {
3334 struct zmsghdr hdr;
3335 ssize_t nb;
3336 bool hdrvalid;
3337 char errmsg[256];
3338
3339 already = stream_get_endp(client->ibuf_work);
3340
3341 /* Read length and command (if we don't have it already). */
3342 if (already < ZEBRA_HEADER_SIZE) {
3343 nb = stream_read_try(client->ibuf_work, sock,
3344 ZEBRA_HEADER_SIZE - already);
3345 if ((nb == 0 || nb == -1) && IS_ZEBRA_DEBUG_EVENT)
3346 zlog_debug("connection closed socket [%d]",
3347 sock);
3348 if ((nb == 0 || nb == -1))
3349 goto zread_fail;
3350 if (nb != (ssize_t)(ZEBRA_HEADER_SIZE - already)) {
3351 /* Try again later. */
3352 break;
3353 }
3354 already = ZEBRA_HEADER_SIZE;
3355 }
3356
3357 /* Reset to read from the beginning of the incoming packet. */
3358 stream_set_getp(client->ibuf_work, 0);
3359
3360 /* Fetch header values */
3361 hdrvalid = zapi_parse_header(client->ibuf_work, &hdr);
3362
3363 if (!hdrvalid) {
3364 snprintf(errmsg, sizeof(errmsg),
3365 "%s: Message has corrupt header", __func__);
3366 zserv_log_message(errmsg, client->ibuf_work, NULL);
3367 goto zread_fail;
3368 }
3369
3370 /* Validate header */
3371 if (hdr.marker != ZEBRA_HEADER_MARKER
3372 || hdr.version != ZSERV_VERSION) {
3373 snprintf(
3374 errmsg, sizeof(errmsg),
3375 "Message has corrupt header\n%s: socket %d version mismatch, marker %d, version %d",
3376 __func__, sock, hdr.marker, hdr.version);
3377 zserv_log_message(errmsg, client->ibuf_work, &hdr);
3378 goto zread_fail;
3379 }
3380 if (hdr.length < ZEBRA_HEADER_SIZE) {
3381 snprintf(
3382 errmsg, sizeof(errmsg),
3383 "Message has corrupt header\n%s: socket %d message length %u is less than header size %d",
3384 __func__, sock, hdr.length, ZEBRA_HEADER_SIZE);
3385 zserv_log_message(errmsg, client->ibuf_work, &hdr);
3386 goto zread_fail;
3387 }
3388 if (hdr.length > STREAM_SIZE(client->ibuf_work)) {
3389 snprintf(
3390 errmsg, sizeof(errmsg),
3391 "Message has corrupt header\n%s: socket %d message length %u exceeds buffer size %lu",
3392 __func__, sock, hdr.length,
3393 (unsigned long)STREAM_SIZE(client->ibuf_work));
3394 goto zread_fail;
3395 }
3396
3397 /* Read rest of data. */
3398 if (already < hdr.length) {
3399 nb = stream_read_try(client->ibuf_work, sock,
3400 hdr.length - already);
3401 if ((nb == 0 || nb == -1) && IS_ZEBRA_DEBUG_EVENT)
3402 zlog_debug(
3403 "connection closed [%d] when reading zebra data",
3404 sock);
3405 if ((nb == 0 || nb == -1))
3406 goto zread_fail;
3407 if (nb != (ssize_t)(hdr.length - already)) {
3408 /* Try again later. */
3409 break;
3410 }
3411 }
3412
3413 #if defined(HANDLE_ZAPI_FUZZING)
3414 zserv_write_incoming(client->ibuf_work, command);
3415 #endif
3416
3417 /* Debug packet information. */
3418 if (IS_ZEBRA_DEBUG_EVENT)
3419 zlog_debug("zebra message comes from socket [%d]",
3420 sock);
3421
3422 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
3423 zserv_log_message(NULL, client->ibuf_work, &hdr);
3424
3425 client->last_read_time = monotime(NULL);
3426 client->last_read_cmd = hdr.command;
3427
3428 stream_set_getp(client->ibuf_work, 0);
3429 struct stream *msg = stream_dup(client->ibuf_work);
3430
3431 stream_fifo_push(client->ibuf_fifo, msg);
3432
3433 if (client->t_suicide)
3434 goto zread_fail;
3435
3436 --packets;
3437 stream_reset(client->ibuf_work);
3438 }
3439
3440 if (IS_ZEBRA_DEBUG_PACKET)
3441 zlog_debug("Read %d packets",
3442 zebrad.packets_to_process - packets);
3443
3444 /* Schedule job to process those packets */
3445 thread_add_event(zebrad.master, &zserv_process_messages, client, 0,
3446 NULL);
3447
3448 /* Reschedule ourselves */
3449 zebra_event(client, ZEBRA_READ);
3450
3451 return 0;
3452
3453 zread_fail:
3454 zebra_client_close(client);
3455 return -1;
3456 }
3457
3458 static void zebra_event(struct zserv *client, enum event event)
3459 {
3460 switch (event) {
3461 case ZEBRA_READ:
3462 thread_add_read(zebrad.master, zserv_read, client, client->sock,
3463 &client->t_read);
3464 break;
3465 case ZEBRA_WRITE:
3466 thread_add_write(zebrad.master, zserv_write, client,
3467 client->sock, &client->t_write);
3468 break;
3469 }
3470 }
3471
3472 /* Accept code of zebra server socket. */
3473 static int zebra_accept(struct thread *thread)
3474 {
3475 int accept_sock;
3476 int client_sock;
3477 struct sockaddr_in client;
3478 socklen_t len;
3479
3480 accept_sock = THREAD_FD(thread);
3481
3482 /* Reregister myself. */
3483 thread_add_read(zebrad.master, zebra_accept, NULL, accept_sock, NULL);
3484
3485 len = sizeof(struct sockaddr_in);
3486 client_sock = accept(accept_sock, (struct sockaddr *)&client, &len);
3487
3488 if (client_sock < 0) {
3489 zlog_warn("Can't accept zebra socket: %s",
3490 safe_strerror(errno));
3491 return -1;
3492 }
3493
3494 /* Make client socket non-blocking. */
3495 set_nonblocking(client_sock);
3496
3497 /* Create new zebra client. */
3498 zebra_client_create(client_sock);
3499
3500 return 0;
3501 }
3502
3503 /* Make zebra server socket, wiping any existing one (see bug #403). */
3504 void zebra_zserv_socket_init(char *path)
3505 {
3506 int ret;
3507 int sock;
3508 mode_t old_mask;
3509 struct sockaddr_storage sa;
3510 socklen_t sa_len;
3511
3512 if (!frr_zclient_addr(&sa, &sa_len, path))
3513 /* should be caught in zebra main() */
3514 return;
3515
3516 /* Set umask */
3517 old_mask = umask(0077);
3518
3519 /* Make UNIX domain socket. */
3520 sock = socket(sa.ss_family, SOCK_STREAM, 0);
3521 if (sock < 0) {
3522 zlog_warn("Can't create zserv socket: %s",
3523 safe_strerror(errno));
3524 zlog_warn(
3525 "zebra can't provide full functionality due to above error");
3526 return;
3527 }
3528
3529 if (sa.ss_family != AF_UNIX) {
3530 sockopt_reuseaddr(sock);
3531 sockopt_reuseport(sock);
3532 } else {
3533 struct sockaddr_un *suna = (struct sockaddr_un *)&sa;
3534 if (suna->sun_path[0])
3535 unlink(suna->sun_path);
3536 }
3537
3538 zserv_privs.change(ZPRIVS_RAISE);
3539 setsockopt_so_recvbuf(sock, 1048576);
3540 setsockopt_so_sendbuf(sock, 1048576);
3541 zserv_privs.change(ZPRIVS_LOWER);
3542
3543 if (sa.ss_family != AF_UNIX && zserv_privs.change(ZPRIVS_RAISE))
3544 zlog_err("Can't raise privileges");
3545
3546 ret = bind(sock, (struct sockaddr *)&sa, sa_len);
3547 if (ret < 0) {
3548 zlog_warn("Can't bind zserv socket on %s: %s", path,
3549 safe_strerror(errno));
3550 zlog_warn(
3551 "zebra can't provide full functionality due to above error");
3552 close(sock);
3553 return;
3554 }
3555 if (sa.ss_family != AF_UNIX && zserv_privs.change(ZPRIVS_LOWER))
3556 zlog_err("Can't lower privileges");
3557
3558 ret = listen(sock, 5);
3559 if (ret < 0) {
3560 zlog_warn("Can't listen to zserv socket %s: %s", path,
3561 safe_strerror(errno));
3562 zlog_warn(
3563 "zebra can't provide full functionality due to above error");
3564 close(sock);
3565 return;
3566 }
3567
3568 umask(old_mask);
3569
3570 thread_add_read(zebrad.master, zebra_accept, NULL, sock, NULL);
3571 }
3572
3573 #define ZEBRA_TIME_BUF 32
3574 static char *zserv_time_buf(time_t *time1, char *buf, int buflen)
3575 {
3576 struct tm *tm;
3577 time_t now;
3578
3579 assert(buf != NULL);
3580 assert(buflen >= ZEBRA_TIME_BUF);
3581 assert(time1 != NULL);
3582
3583 if (!*time1) {
3584 snprintf(buf, buflen, "never ");
3585 return (buf);
3586 }
3587
3588 now = monotime(NULL);
3589 now -= *time1;
3590 tm = gmtime(&now);
3591
3592 if (now < ONE_DAY_SECOND)
3593 snprintf(buf, buflen, "%02d:%02d:%02d", tm->tm_hour, tm->tm_min,
3594 tm->tm_sec);
3595 else if (now < ONE_WEEK_SECOND)
3596 snprintf(buf, buflen, "%dd%02dh%02dm", tm->tm_yday, tm->tm_hour,
3597 tm->tm_min);
3598 else
3599 snprintf(buf, buflen, "%02dw%dd%02dh", tm->tm_yday / 7,
3600 tm->tm_yday - ((tm->tm_yday / 7) * 7), tm->tm_hour);
3601 return buf;
3602 }
3603
3604 static void zebra_show_client_detail(struct vty *vty, struct zserv *client)
3605 {
3606 char cbuf[ZEBRA_TIME_BUF], rbuf[ZEBRA_TIME_BUF];
3607 char wbuf[ZEBRA_TIME_BUF], nhbuf[ZEBRA_TIME_BUF], mbuf[ZEBRA_TIME_BUF];
3608
3609 vty_out(vty, "Client: %s", zebra_route_string(client->proto));
3610 if (client->instance)
3611 vty_out(vty, " Instance: %d", client->instance);
3612 vty_out(vty, "\n");
3613
3614 vty_out(vty, "------------------------ \n");
3615 vty_out(vty, "FD: %d \n", client->sock);
3616 vty_out(vty, "Route Table ID: %d \n", client->rtm_table);
3617
3618 vty_out(vty, "Connect Time: %s \n",
3619 zserv_time_buf(&client->connect_time, cbuf, ZEBRA_TIME_BUF));
3620 if (client->nh_reg_time) {
3621 vty_out(vty, "Nexthop Registry Time: %s \n",
3622 zserv_time_buf(&client->nh_reg_time, nhbuf,
3623 ZEBRA_TIME_BUF));
3624 if (client->nh_last_upd_time)
3625 vty_out(vty, "Nexthop Last Update Time: %s \n",
3626 zserv_time_buf(&client->nh_last_upd_time, mbuf,
3627 ZEBRA_TIME_BUF));
3628 else
3629 vty_out(vty, "No Nexthop Update sent\n");
3630 } else
3631 vty_out(vty, "Not registered for Nexthop Updates\n");
3632
3633 vty_out(vty, "Last Msg Rx Time: %s \n",
3634 zserv_time_buf(&client->last_read_time, rbuf, ZEBRA_TIME_BUF));
3635 vty_out(vty, "Last Msg Tx Time: %s \n",
3636 zserv_time_buf(&client->last_write_time, wbuf, ZEBRA_TIME_BUF));
3637 if (client->last_read_time)
3638 vty_out(vty, "Last Rcvd Cmd: %s \n",
3639 zserv_command_string(client->last_read_cmd));
3640 if (client->last_write_time)
3641 vty_out(vty, "Last Sent Cmd: %s \n",
3642 zserv_command_string(client->last_write_cmd));
3643 vty_out(vty, "\n");
3644
3645 vty_out(vty, "Type Add Update Del \n");
3646 vty_out(vty, "================================================== \n");
3647 vty_out(vty, "IPv4 %-12d%-12d%-12d\n", client->v4_route_add_cnt,
3648 client->v4_route_upd8_cnt, client->v4_route_del_cnt);
3649 vty_out(vty, "IPv6 %-12d%-12d%-12d\n", client->v6_route_add_cnt,
3650 client->v6_route_upd8_cnt, client->v6_route_del_cnt);
3651 vty_out(vty, "Redist:v4 %-12d%-12d%-12d\n", client->redist_v4_add_cnt,
3652 0, client->redist_v4_del_cnt);
3653 vty_out(vty, "Redist:v6 %-12d%-12d%-12d\n", client->redist_v6_add_cnt,
3654 0, client->redist_v6_del_cnt);
3655 vty_out(vty, "Connected %-12d%-12d%-12d\n", client->ifadd_cnt, 0,
3656 client->ifdel_cnt);
3657 vty_out(vty, "BFD peer %-12d%-12d%-12d\n", client->bfd_peer_add_cnt,
3658 client->bfd_peer_upd8_cnt, client->bfd_peer_del_cnt);
3659 vty_out(vty, "Interface Up Notifications: %d\n", client->ifup_cnt);
3660 vty_out(vty, "Interface Down Notifications: %d\n", client->ifdown_cnt);
3661 vty_out(vty, "VNI add notifications: %d\n", client->vniadd_cnt);
3662 vty_out(vty, "VNI delete notifications: %d\n", client->vnidel_cnt);
3663 vty_out(vty, "L3-VNI add notifications: %d\n", client->l3vniadd_cnt);
3664 vty_out(vty, "L3-VNI delete notifications: %d\n", client->l3vnidel_cnt);
3665 vty_out(vty, "MAC-IP add notifications: %d\n", client->macipadd_cnt);
3666 vty_out(vty, "MAC-IP delete notifications: %d\n", client->macipdel_cnt);
3667
3668 vty_out(vty, "\n");
3669 return;
3670 }
3671
3672 static void zebra_show_client_brief(struct vty *vty, struct zserv *client)
3673 {
3674 char cbuf[ZEBRA_TIME_BUF], rbuf[ZEBRA_TIME_BUF];
3675 char wbuf[ZEBRA_TIME_BUF];
3676
3677 vty_out(vty, "%-8s%12s %12s%12s%8d/%-8d%8d/%-8d\n",
3678 zebra_route_string(client->proto),
3679 zserv_time_buf(&client->connect_time, cbuf, ZEBRA_TIME_BUF),
3680 zserv_time_buf(&client->last_read_time, rbuf, ZEBRA_TIME_BUF),
3681 zserv_time_buf(&client->last_write_time, wbuf, ZEBRA_TIME_BUF),
3682 client->v4_route_add_cnt + client->v4_route_upd8_cnt,
3683 client->v4_route_del_cnt,
3684 client->v6_route_add_cnt + client->v6_route_upd8_cnt,
3685 client->v6_route_del_cnt);
3686 }
3687
3688 struct zserv *zebra_find_client(uint8_t proto, unsigned short instance)
3689 {
3690 struct listnode *node, *nnode;
3691 struct zserv *client;
3692
3693 for (ALL_LIST_ELEMENTS(zebrad.client_list, node, nnode, client)) {
3694 if (client->proto == proto && client->instance == instance)
3695 return client;
3696 }
3697
3698 return NULL;
3699 }
3700
3701 /* This command is for debugging purpose. */
3702 DEFUN (show_zebra_client,
3703 show_zebra_client_cmd,
3704 "show zebra client",
3705 SHOW_STR
3706 ZEBRA_STR
3707 "Client information\n")
3708 {
3709 struct listnode *node;
3710 struct zserv *client;
3711
3712 for (ALL_LIST_ELEMENTS_RO(zebrad.client_list, node, client))
3713 zebra_show_client_detail(vty, client);
3714
3715 return CMD_SUCCESS;
3716 }
3717
3718 /* This command is for debugging purpose. */
3719 DEFUN (show_zebra_client_summary,
3720 show_zebra_client_summary_cmd,
3721 "show zebra client summary",
3722 SHOW_STR
3723 ZEBRA_STR
3724 "Client information brief\n"
3725 "Brief Summary\n")
3726 {
3727 struct listnode *node;
3728 struct zserv *client;
3729
3730 vty_out(vty,
3731 "Name Connect Time Last Read Last Write IPv4 Routes IPv6 Routes \n");
3732 vty_out(vty,
3733 "--------------------------------------------------------------------------------\n");
3734
3735 for (ALL_LIST_ELEMENTS_RO(zebrad.client_list, node, client))
3736 zebra_show_client_brief(vty, client);
3737
3738 vty_out(vty, "Routes column shows (added+updated)/deleted\n");
3739 return CMD_SUCCESS;
3740 }
3741
3742 #if defined(HANDLE_ZAPI_FUZZING)
3743 void zserv_read_file(char *input)
3744 {
3745 int fd;
3746 struct zserv *client = NULL;
3747 struct thread t;
3748
3749 zebra_client_create(-1);
3750 client = zebrad.client_list->head->data;
3751 t.arg = client;
3752
3753 fd = open(input, O_RDONLY | O_NONBLOCK);
3754 t.u.fd = fd;
3755
3756 zebra_client_read(&t);
3757
3758 close(fd);
3759 }
3760 #endif
3761
3762 void zserv_init(void)
3763 {
3764 /* Client list init. */
3765 zebrad.client_list = list_new();
3766 zebrad.client_list->del = (void (*)(void *))zebra_client_free;
3767
3768 install_element(ENABLE_NODE, &show_zebra_client_cmd);
3769 install_element(ENABLE_NODE, &show_zebra_client_summary_cmd);
3770 }