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