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