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