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