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