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