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