]> git.proxmox.com Git - mirror_frr.git/blob - zebra/zserv.c
Merge pull request #1854 from qlyoung/integer-standards-compliance
[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 stream_free(s);
227 return 0;
228 }
229
230 if (!ifp->link_params) {
231 stream_free(s);
232 return 0;
233 }
234
235 zclient_create_header(s, ZEBRA_INTERFACE_LINK_PARAMS, ifp->vrf_id);
236
237 /* Add Interface Index */
238 stream_putl(s, ifp->ifindex);
239
240 /* Then TE Link Parameters */
241 if (zebra_interface_link_params_write(s, ifp) == 0) {
242 stream_free(s);
243 return 0;
244 }
245
246 /* Write packet size. */
247 stream_putw_at(s, 0, stream_get_endp(s));
248
249 return zebra_server_send_message(client, s);
250 }
251
252 /* Interface address is added/deleted. Send ZEBRA_INTERFACE_ADDRESS_ADD or
253 * ZEBRA_INTERFACE_ADDRESS_DELETE to the client.
254 *
255 * A ZEBRA_INTERFACE_ADDRESS_ADD is sent in the following situations:
256 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
257 * from the client, after the ZEBRA_INTERFACE_ADD has been
258 * sent from zebra to the client
259 * - redistribute new address info to all clients in the following situations
260 * - at startup, when zebra figures out the available interfaces
261 * - when an interface is added (where support for
262 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
263 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
264 * received)
265 * - for the vty commands "ip address A.B.C.D/M [<secondary>|<label LINE>]"
266 * and "no bandwidth <1-10000000>", "ipv6 address X:X::X:X/M"
267 * - when an RTM_NEWADDR message is received from the kernel,
268 *
269 * The call tree that triggers ZEBRA_INTERFACE_ADDRESS_DELETE:
270 *
271 * zsend_interface_address(DELETE)
272 * ^
273 * |
274 * zebra_interface_address_delete_update
275 * ^ ^ ^
276 * | | if_delete_update
277 * | |
278 * ip_address_uninstall connected_delete_ipv4
279 * [ipv6_addresss_uninstall] [connected_delete_ipv6]
280 * ^ ^
281 * | |
282 * | RTM_NEWADDR on routing/netlink socket
283 * |
284 * vty commands:
285 * "no ip address A.B.C.D/M [label LINE]"
286 * "no ip address A.B.C.D/M secondary"
287 * ["no ipv6 address X:X::X:X/M"]
288 *
289 */
290 int zsend_interface_address(int cmd, struct zserv *client,
291 struct interface *ifp, struct connected *ifc)
292 {
293 int blen;
294 struct prefix *p;
295 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
296
297 zclient_create_header(s, cmd, ifp->vrf_id);
298 stream_putl(s, ifp->ifindex);
299
300 /* Interface address flag. */
301 stream_putc(s, ifc->flags);
302
303 /* Prefix information. */
304 p = ifc->address;
305 stream_putc(s, p->family);
306 blen = prefix_blen(p);
307 stream_put(s, &p->u.prefix, blen);
308
309 /*
310 * XXX gnu version does not send prefixlen for
311 * ZEBRA_INTERFACE_ADDRESS_DELETE
312 * but zebra_interface_address_delete_read() in the gnu version
313 * expects to find it
314 */
315 stream_putc(s, p->prefixlen);
316
317 /* Destination. */
318 p = ifc->destination;
319 if (p)
320 stream_put(s, &p->u.prefix, blen);
321 else
322 stream_put(s, NULL, blen);
323
324 /* Write packet size. */
325 stream_putw_at(s, 0, stream_get_endp(s));
326
327 client->connected_rt_add_cnt++;
328 return zebra_server_send_message(client, s);
329 }
330
331 static int zsend_interface_nbr_address(int cmd, struct zserv *client,
332 struct interface *ifp,
333 struct nbr_connected *ifc)
334 {
335 int blen;
336 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
337 struct prefix *p;
338
339 zclient_create_header(s, cmd, ifp->vrf_id);
340 stream_putl(s, ifp->ifindex);
341
342 /* Prefix information. */
343 p = ifc->address;
344 stream_putc(s, p->family);
345 blen = prefix_blen(p);
346 stream_put(s, &p->u.prefix, blen);
347
348 /*
349 * XXX gnu version does not send prefixlen for
350 * ZEBRA_INTERFACE_ADDRESS_DELETE
351 * but zebra_interface_address_delete_read() in the gnu version
352 * expects to find it
353 */
354 stream_putc(s, p->prefixlen);
355
356 /* Write packet size. */
357 stream_putw_at(s, 0, stream_get_endp(s));
358
359 return zebra_server_send_message(client, s);
360 }
361
362 /* Interface address addition. */
363 static void zebra_interface_nbr_address_add_update(struct interface *ifp,
364 struct nbr_connected *ifc)
365 {
366 struct listnode *node, *nnode;
367 struct zserv *client;
368 struct prefix *p;
369
370 if (IS_ZEBRA_DEBUG_EVENT) {
371 char buf[INET6_ADDRSTRLEN];
372
373 p = ifc->address;
374 zlog_debug(
375 "MESSAGE: ZEBRA_INTERFACE_NBR_ADDRESS_ADD %s/%d on %s",
376 inet_ntop(p->family, &p->u.prefix, buf,
377 INET6_ADDRSTRLEN),
378 p->prefixlen, ifc->ifp->name);
379 }
380
381 for (ALL_LIST_ELEMENTS(zebrad.client_list, node, nnode, client))
382 zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_ADD,
383 client, ifp, ifc);
384 }
385
386 /* Interface address deletion. */
387 static void zebra_interface_nbr_address_delete_update(struct interface *ifp,
388 struct nbr_connected *ifc)
389 {
390 struct listnode *node, *nnode;
391 struct zserv *client;
392 struct prefix *p;
393
394 if (IS_ZEBRA_DEBUG_EVENT) {
395 char buf[INET6_ADDRSTRLEN];
396
397 p = ifc->address;
398 zlog_debug(
399 "MESSAGE: ZEBRA_INTERFACE_NBR_ADDRESS_DELETE %s/%d on %s",
400 inet_ntop(p->family, &p->u.prefix, buf,
401 INET6_ADDRSTRLEN),
402 p->prefixlen, ifc->ifp->name);
403 }
404
405 for (ALL_LIST_ELEMENTS(zebrad.client_list, node, nnode, client))
406 zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_DELETE,
407 client, ifp, ifc);
408 }
409
410 /* Send addresses on interface to client */
411 int zsend_interface_addresses(struct zserv *client, struct interface *ifp)
412 {
413 struct listnode *cnode, *cnnode;
414 struct connected *c;
415 struct nbr_connected *nc;
416
417 /* Send interface addresses. */
418 for (ALL_LIST_ELEMENTS(ifp->connected, cnode, cnnode, c)) {
419 if (!CHECK_FLAG(c->conf, ZEBRA_IFC_REAL))
420 continue;
421
422 if (zsend_interface_address(ZEBRA_INTERFACE_ADDRESS_ADD, client,
423 ifp, c)
424 < 0)
425 return -1;
426 }
427
428 /* Send interface neighbors. */
429 for (ALL_LIST_ELEMENTS(ifp->nbr_connected, cnode, cnnode, nc)) {
430 if (zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_ADD,
431 client, ifp, nc)
432 < 0)
433 return -1;
434 }
435
436 return 0;
437 }
438
439 /* Notify client about interface moving from one VRF to another.
440 * Whether client is interested in old and new VRF is checked by caller.
441 */
442 int zsend_interface_vrf_update(struct zserv *client, struct interface *ifp,
443 vrf_id_t vrf_id)
444 {
445 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
446
447 zclient_create_header(s, ZEBRA_INTERFACE_VRF_UPDATE, ifp->vrf_id);
448
449 /* Fill in the ifIndex of the interface and its new VRF (id) */
450 stream_putl(s, ifp->ifindex);
451 stream_putl(s, vrf_id);
452
453 /* Write packet size. */
454 stream_putw_at(s, 0, stream_get_endp(s));
455
456 client->if_vrfchg_cnt++;
457 return zebra_server_send_message(client, s);
458 }
459
460 /* Add new nbr connected IPv6 address */
461 void nbr_connected_add_ipv6(struct interface *ifp, struct in6_addr *address)
462 {
463 struct nbr_connected *ifc;
464 struct prefix p;
465
466 p.family = AF_INET6;
467 IPV6_ADDR_COPY(&p.u.prefix, address);
468 p.prefixlen = IPV6_MAX_PREFIXLEN;
469
470 if (!(ifc = listnode_head(ifp->nbr_connected))) {
471 /* new addition */
472 ifc = nbr_connected_new();
473 ifc->address = prefix_new();
474 ifc->ifp = ifp;
475 listnode_add(ifp->nbr_connected, ifc);
476 }
477
478 prefix_copy(ifc->address, &p);
479
480 zebra_interface_nbr_address_add_update(ifp, ifc);
481
482 if_nbr_ipv6ll_to_ipv4ll_neigh_update(ifp, address, 1);
483 }
484
485 void nbr_connected_delete_ipv6(struct interface *ifp, struct in6_addr *address)
486 {
487 struct nbr_connected *ifc;
488 struct prefix p;
489
490 p.family = AF_INET6;
491 IPV6_ADDR_COPY(&p.u.prefix, address);
492 p.prefixlen = IPV6_MAX_PREFIXLEN;
493
494 ifc = nbr_connected_check(ifp, &p);
495 if (!ifc)
496 return;
497
498 listnode_delete(ifp->nbr_connected, ifc);
499
500 zebra_interface_nbr_address_delete_update(ifp, ifc);
501
502 if_nbr_ipv6ll_to_ipv4ll_neigh_update(ifp, address, 0);
503
504 nbr_connected_free(ifc);
505 }
506
507 /*
508 * The cmd passed to zsend_interface_update may be ZEBRA_INTERFACE_UP or
509 * ZEBRA_INTERFACE_DOWN.
510 *
511 * The ZEBRA_INTERFACE_UP message is sent from the zebra server to
512 * the clients in one of 2 situations:
513 * - an if_up is detected e.g., as a result of an RTM_IFINFO message
514 * - a vty command modifying the bandwidth of an interface is received.
515 * The ZEBRA_INTERFACE_DOWN message is sent when an if_down is detected.
516 */
517 int zsend_interface_update(int cmd, struct zserv *client, struct interface *ifp)
518 {
519 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
520
521 zclient_create_header(s, cmd, ifp->vrf_id);
522 zserv_encode_interface(s, ifp);
523
524 if (cmd == ZEBRA_INTERFACE_UP)
525 client->ifup_cnt++;
526 else
527 client->ifdown_cnt++;
528
529 return zebra_server_send_message(client, s);
530 }
531
532 int zsend_redistribute_route(int cmd, struct zserv *client, struct prefix *p,
533 struct prefix *src_p, struct route_entry *re)
534 {
535 struct zapi_route api;
536 struct zapi_nexthop *api_nh;
537 struct nexthop *nexthop;
538 int count = 0;
539
540 memset(&api, 0, sizeof(api));
541 api.vrf_id = re->vrf_id;
542 api.type = re->type;
543 api.instance = re->instance;
544 api.flags = re->flags;
545
546 /* Prefix. */
547 api.prefix = *p;
548 if (src_p) {
549 SET_FLAG(api.message, ZAPI_MESSAGE_SRCPFX);
550 memcpy(&api.src_prefix, src_p, sizeof(api.src_prefix));
551 }
552
553 /* Nexthops. */
554 if (re->nexthop_active_num) {
555 SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP);
556 api.nexthop_num = re->nexthop_active_num;
557 }
558 for (nexthop = re->ng.nexthop; nexthop; nexthop = nexthop->next) {
559 if (!CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE))
560 continue;
561
562 api_nh = &api.nexthops[count];
563 api_nh->vrf_id = nexthop->vrf_id;
564 api_nh->type = nexthop->type;
565 switch (nexthop->type) {
566 case NEXTHOP_TYPE_BLACKHOLE:
567 api_nh->bh_type = nexthop->bh_type;
568 break;
569 case NEXTHOP_TYPE_IPV4:
570 api_nh->gate.ipv4 = nexthop->gate.ipv4;
571 break;
572 case NEXTHOP_TYPE_IPV4_IFINDEX:
573 api_nh->gate.ipv4 = nexthop->gate.ipv4;
574 api_nh->ifindex = nexthop->ifindex;
575 break;
576 case NEXTHOP_TYPE_IFINDEX:
577 api_nh->ifindex = nexthop->ifindex;
578 break;
579 case NEXTHOP_TYPE_IPV6:
580 api_nh->gate.ipv6 = nexthop->gate.ipv6;
581 break;
582 case NEXTHOP_TYPE_IPV6_IFINDEX:
583 api_nh->gate.ipv6 = nexthop->gate.ipv6;
584 api_nh->ifindex = nexthop->ifindex;
585 }
586 count++;
587 }
588
589 /* Attributes. */
590 SET_FLAG(api.message, ZAPI_MESSAGE_DISTANCE);
591 api.distance = re->distance;
592 SET_FLAG(api.message, ZAPI_MESSAGE_METRIC);
593 api.metric = re->metric;
594 if (re->tag) {
595 SET_FLAG(api.message, ZAPI_MESSAGE_TAG);
596 api.tag = re->tag;
597 }
598 SET_FLAG(api.message, ZAPI_MESSAGE_MTU);
599 api.mtu = re->mtu;
600
601 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
602
603 /* Encode route and send. */
604 if (zapi_route_encode(cmd, s, &api) < 0) {
605 stream_free(s);
606 return -1;
607 }
608
609 if (IS_ZEBRA_DEBUG_SEND) {
610 char buf_prefix[PREFIX_STRLEN];
611 prefix2str(&api.prefix, buf_prefix, sizeof(buf_prefix));
612
613 zlog_debug("%s: %s to client %s: type %s, vrf_id %d, p %s",
614 __func__, zserv_command_string(cmd),
615 zebra_route_string(client->proto),
616 zebra_route_string(api.type), api.vrf_id,
617 buf_prefix);
618 }
619 return zebra_server_send_message(client, s);
620 }
621
622 /*
623 * Modified version of zsend_ipv4_nexthop_lookup(): Query unicast rib if
624 * nexthop is not found on mrib. Returns both route metric and protocol
625 * distance.
626 */
627 static int zsend_ipv4_nexthop_lookup_mrib(struct zserv *client,
628 struct in_addr addr,
629 struct route_entry *re,
630 struct zebra_vrf *zvrf)
631 {
632 struct stream *s;
633 unsigned long nump;
634 uint8_t num;
635 struct nexthop *nexthop;
636
637 /* Get output stream. */
638 s = stream_new(ZEBRA_MAX_PACKET_SIZ);
639 stream_reset(s);
640
641 /* Fill in result. */
642 zclient_create_header(s, ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB, zvrf_id(zvrf));
643 stream_put_in_addr(s, &addr);
644
645 if (re) {
646 stream_putc(s, re->distance);
647 stream_putl(s, re->metric);
648 num = 0;
649 nump = stream_get_endp(
650 s); /* remember position for nexthop_num */
651 stream_putc(s, 0); /* reserve room for nexthop_num */
652 /* Only non-recursive routes are elegible to resolve the nexthop
653 * we
654 * are looking up. Therefore, we will just iterate over the top
655 * chain of nexthops. */
656 for (nexthop = re->ng.nexthop; nexthop; nexthop = nexthop->next)
657 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE))
658 num += zserv_encode_nexthop(s, nexthop);
659
660 stream_putc_at(s, nump, num); /* store nexthop_num */
661 } else {
662 stream_putc(s, 0); /* distance */
663 stream_putl(s, 0); /* metric */
664 stream_putc(s, 0); /* nexthop_num */
665 }
666
667 stream_putw_at(s, 0, stream_get_endp(s));
668
669 return zebra_server_send_message(client, s);
670 }
671
672 int zsend_route_notify_owner(struct route_entry *re, struct prefix *p,
673 enum zapi_route_notify_owner note)
674 {
675 struct zserv *client;
676 struct stream *s;
677 uint8_t blen;
678
679 client = zebra_find_client(re->type, re->instance);
680 if (!client || !client->notify_owner) {
681 if (IS_ZEBRA_DEBUG_PACKET) {
682 char buff[PREFIX_STRLEN];
683
684 zlog_debug(
685 "Not Notifying Owner: %u about prefix %s(%u) %d vrf: %u",
686 re->type, prefix2str(p, buff, sizeof(buff)),
687 re->table, note, re->vrf_id);
688 }
689 return 0;
690 }
691
692 if (IS_ZEBRA_DEBUG_PACKET) {
693 char buff[PREFIX_STRLEN];
694
695 zlog_debug("Notifying Owner: %u about prefix %s(%u) %d vrf: %u",
696 re->type, prefix2str(p, buff, sizeof(buff)),
697 re->table, note, re->vrf_id);
698 }
699
700 s = stream_new(ZEBRA_MAX_PACKET_SIZ);
701 stream_reset(s);
702
703 zclient_create_header(s, ZEBRA_ROUTE_NOTIFY_OWNER, re->vrf_id);
704
705 stream_put(s, &note, sizeof(note));
706
707 stream_putc(s, p->family);
708
709 blen = prefix_blen(p);
710 stream_putc(s, p->prefixlen);
711 stream_put(s, &p->u.prefix, blen);
712
713 stream_putl(s, re->table);
714
715 stream_putw_at(s, 0, stream_get_endp(s));
716
717 return zebra_server_send_message(client, s);
718 }
719
720 void zsend_rule_notify_owner(struct zebra_pbr_rule *rule,
721 enum zapi_rule_notify_owner note)
722 {
723 struct listnode *node;
724 struct zserv *client;
725 struct stream *s;
726
727 if (IS_ZEBRA_DEBUG_PACKET) {
728 zlog_debug("%s: Notifying %u", __PRETTY_FUNCTION__,
729 rule->unique);
730 }
731
732 for (ALL_LIST_ELEMENTS_RO(zebrad.client_list, node, client)) {
733 if (rule->sock == client->sock)
734 break;
735 }
736
737 if (!client)
738 return;
739
740 s = stream_new(ZEBRA_MAX_PACKET_SIZ);
741 stream_reset(s);
742
743 zclient_create_header(s, ZEBRA_RULE_NOTIFY_OWNER, VRF_DEFAULT);
744 stream_put(s, &note, sizeof(note));
745 stream_putl(s, rule->seq);
746 stream_putl(s, rule->priority);
747 stream_putl(s, rule->unique);
748 if (rule->ifp)
749 stream_putl(s, rule->ifp->ifindex);
750 else
751 stream_putl(s, 0);
752
753 stream_putw_at(s, 0, stream_get_endp(s));
754
755 zebra_server_send_message(client, s);
756 }
757
758 /* Router-id is updated. Send ZEBRA_ROUTER_ID_ADD to client. */
759 int zsend_router_id_update(struct zserv *client, struct prefix *p,
760 vrf_id_t vrf_id)
761 {
762 int blen;
763
764 /* Check this client need interface information. */
765 if (!vrf_bitmap_check(client->ridinfo, vrf_id))
766 return 0;
767
768 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
769
770 /* Message type. */
771 zclient_create_header(s, ZEBRA_ROUTER_ID_UPDATE, vrf_id);
772
773 /* Prefix information. */
774 stream_putc(s, p->family);
775 blen = prefix_blen(p);
776 stream_put(s, &p->u.prefix, blen);
777 stream_putc(s, p->prefixlen);
778
779 /* Write packet size. */
780 stream_putw_at(s, 0, stream_get_endp(s));
781
782 return zebra_server_send_message(client, s);
783 }
784
785 /*
786 * Function used by Zebra to send a PW status update to LDP daemon
787 */
788 int zsend_pw_update(struct zserv *client, struct zebra_pw *pw)
789 {
790 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
791
792 zclient_create_header(s, ZEBRA_PW_STATUS_UPDATE, pw->vrf_id);
793 stream_write(s, pw->ifname, IF_NAMESIZE);
794 stream_putl(s, pw->ifindex);
795 stream_putl(s, pw->status);
796
797 /* Put length at the first point of the stream. */
798 stream_putw_at(s, 0, stream_get_endp(s));
799
800 return zebra_server_send_message(client, s);
801 }
802
803 /* Send response to a get label chunk request to client */
804 static int zsend_assign_label_chunk_response(struct zserv *client,
805 vrf_id_t vrf_id,
806 struct label_manager_chunk *lmc)
807 {
808 int ret;
809 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
810
811 zclient_create_header(s, ZEBRA_GET_LABEL_CHUNK, vrf_id);
812
813 if (lmc) {
814 /* keep */
815 stream_putc(s, lmc->keep);
816 /* start and end labels */
817 stream_putl(s, lmc->start);
818 stream_putl(s, lmc->end);
819 }
820
821 /* Write packet size. */
822 stream_putw_at(s, 0, stream_get_endp(s));
823
824 ret = writen(client->sock, s->data, stream_get_endp(s));
825 stream_free(s);
826 return ret;
827 }
828
829 /* Send response to a label manager connect request to client */
830 static int zsend_label_manager_connect_response(struct zserv *client,
831 vrf_id_t vrf_id,
832 unsigned short result)
833 {
834 int ret;
835 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
836
837 zclient_create_header(s, ZEBRA_LABEL_MANAGER_CONNECT, vrf_id);
838
839 /* result */
840 stream_putc(s, result);
841
842 /* Write packet size. */
843 stream_putw_at(s, 0, stream_get_endp(s));
844
845 ret = writen(client->sock, s->data, stream_get_endp(s));
846 stream_free(s);
847
848 return ret;
849 }
850
851 /* Inbound message handling ------------------------------------------------ */
852
853 int cmd2type[] = {
854 [ZEBRA_NEXTHOP_REGISTER] = RNH_NEXTHOP_TYPE,
855 [ZEBRA_NEXTHOP_UNREGISTER] = RNH_NEXTHOP_TYPE,
856 [ZEBRA_IMPORT_ROUTE_REGISTER] = RNH_IMPORT_CHECK_TYPE,
857 [ZEBRA_IMPORT_ROUTE_UNREGISTER] = RNH_IMPORT_CHECK_TYPE,
858 };
859
860 /* Nexthop register */
861 static void zread_rnh_register(ZAPI_HANDLER_ARGS)
862 {
863 struct rnh *rnh;
864 struct stream *s;
865 struct prefix p;
866 unsigned short l = 0;
867 uint8_t flags = 0;
868 uint16_t type = cmd2type[hdr->command];
869
870 if (IS_ZEBRA_DEBUG_NHT)
871 zlog_debug(
872 "rnh_register msg from client %s: hdr->length=%d, type=%s vrf=%u\n",
873 zebra_route_string(client->proto), hdr->length,
874 (type == RNH_NEXTHOP_TYPE) ? "nexthop" : "route",
875 zvrf->vrf->vrf_id);
876
877 s = msg;
878
879 client->nh_reg_time = monotime(NULL);
880
881 while (l < hdr->length) {
882 STREAM_GETC(s, flags);
883 STREAM_GETW(s, p.family);
884 STREAM_GETC(s, p.prefixlen);
885 l += 4;
886 if (p.family == AF_INET) {
887 if (p.prefixlen > IPV4_MAX_BITLEN) {
888 zlog_warn(
889 "%s: Specified prefix hdr->length %d is too large for a v4 address",
890 __PRETTY_FUNCTION__, p.prefixlen);
891 return;
892 }
893 STREAM_GET(&p.u.prefix4.s_addr, s, IPV4_MAX_BYTELEN);
894 l += IPV4_MAX_BYTELEN;
895 } else if (p.family == AF_INET6) {
896 if (p.prefixlen > IPV6_MAX_BITLEN) {
897 zlog_warn(
898 "%s: Specified prefix hdr->length %d is to large for a v6 address",
899 __PRETTY_FUNCTION__, p.prefixlen);
900 return;
901 }
902 STREAM_GET(&p.u.prefix6, s, IPV6_MAX_BYTELEN);
903 l += IPV6_MAX_BYTELEN;
904 } else {
905 zlog_err(
906 "rnh_register: Received unknown family type %d\n",
907 p.family);
908 return;
909 }
910 rnh = zebra_add_rnh(&p, zvrf_id(zvrf), type);
911 if (type == RNH_NEXTHOP_TYPE) {
912 if (flags
913 && !CHECK_FLAG(rnh->flags, ZEBRA_NHT_CONNECTED))
914 SET_FLAG(rnh->flags, ZEBRA_NHT_CONNECTED);
915 else if (!flags
916 && CHECK_FLAG(rnh->flags, ZEBRA_NHT_CONNECTED))
917 UNSET_FLAG(rnh->flags, ZEBRA_NHT_CONNECTED);
918 } else if (type == RNH_IMPORT_CHECK_TYPE) {
919 if (flags
920 && !CHECK_FLAG(rnh->flags, ZEBRA_NHT_EXACT_MATCH))
921 SET_FLAG(rnh->flags, ZEBRA_NHT_EXACT_MATCH);
922 else if (!flags
923 && CHECK_FLAG(rnh->flags,
924 ZEBRA_NHT_EXACT_MATCH))
925 UNSET_FLAG(rnh->flags, ZEBRA_NHT_EXACT_MATCH);
926 }
927
928 zebra_add_rnh_client(rnh, client, type, zvrf_id(zvrf));
929 /* Anything not AF_INET/INET6 has been filtered out above */
930 zebra_evaluate_rnh(zvrf_id(zvrf), p.family, 1, type, &p);
931 }
932
933 stream_failure:
934 return;
935 }
936
937 /* Nexthop register */
938 static void zread_rnh_unregister(ZAPI_HANDLER_ARGS)
939 {
940 struct rnh *rnh;
941 struct stream *s;
942 struct prefix p;
943 unsigned short l = 0;
944 uint16_t type = cmd2type[hdr->command];
945
946 if (IS_ZEBRA_DEBUG_NHT)
947 zlog_debug(
948 "rnh_unregister msg from client %s: hdr->length=%d vrf: %u\n",
949 zebra_route_string(client->proto), hdr->length,
950 zvrf->vrf->vrf_id);
951
952 s = msg;
953
954 while (l < hdr->length) {
955 uint8_t flags;
956
957 STREAM_GETC(s, flags);
958 if (flags != 0)
959 goto stream_failure;
960
961 STREAM_GETW(s, p.family);
962 STREAM_GETC(s, p.prefixlen);
963 l += 4;
964 if (p.family == AF_INET) {
965 if (p.prefixlen > IPV4_MAX_BITLEN) {
966 zlog_warn(
967 "%s: Specified prefix hdr->length %d is to large for a v4 address",
968 __PRETTY_FUNCTION__, p.prefixlen);
969 return;
970 }
971 STREAM_GET(&p.u.prefix4.s_addr, s, IPV4_MAX_BYTELEN);
972 l += IPV4_MAX_BYTELEN;
973 } else if (p.family == AF_INET6) {
974 if (p.prefixlen > IPV6_MAX_BITLEN) {
975 zlog_warn(
976 "%s: Specified prefix hdr->length %d is to large for a v6 address",
977 __PRETTY_FUNCTION__, p.prefixlen);
978 return;
979 }
980 STREAM_GET(&p.u.prefix6, s, IPV6_MAX_BYTELEN);
981 l += IPV6_MAX_BYTELEN;
982 } else {
983 zlog_err(
984 "rnh_register: Received unknown family type %d\n",
985 p.family);
986 return;
987 }
988 rnh = zebra_lookup_rnh(&p, zvrf_id(zvrf), type);
989 if (rnh) {
990 client->nh_dereg_time = monotime(NULL);
991 zebra_remove_rnh_client(rnh, client, type);
992 }
993 }
994 stream_failure:
995 return;
996 }
997
998 #define ZEBRA_MIN_FEC_LENGTH 5
999
1000 /* FEC register */
1001 static void zread_fec_register(ZAPI_HANDLER_ARGS)
1002 {
1003 struct stream *s;
1004 unsigned short l = 0;
1005 struct prefix p;
1006 uint16_t flags;
1007 uint32_t label_index = MPLS_INVALID_LABEL_INDEX;
1008
1009 s = msg;
1010 zvrf = vrf_info_lookup(VRF_DEFAULT);
1011 if (!zvrf)
1012 return; // unexpected
1013
1014 /*
1015 * The minimum amount of data that can be sent for one fec
1016 * registration
1017 */
1018 if (hdr->length < ZEBRA_MIN_FEC_LENGTH) {
1019 zlog_err(
1020 "fec_register: Received a fec register of hdr->length %d, it is of insufficient size to properly decode",
1021 hdr->length);
1022 return;
1023 }
1024
1025 while (l < hdr->length) {
1026 STREAM_GETW(s, flags);
1027 memset(&p, 0, sizeof(p));
1028 STREAM_GETW(s, p.family);
1029 if (p.family != AF_INET && p.family != AF_INET6) {
1030 zlog_err(
1031 "fec_register: Received unknown family type %d\n",
1032 p.family);
1033 return;
1034 }
1035 STREAM_GETC(s, p.prefixlen);
1036 if ((p.family == AF_INET && p.prefixlen > IPV4_MAX_BITLEN)
1037 || (p.family == AF_INET6
1038 && p.prefixlen > IPV6_MAX_BITLEN)) {
1039 zlog_warn(
1040 "%s: Specified prefix hdr->length: %d is to long for %d",
1041 __PRETTY_FUNCTION__, p.prefixlen, p.family);
1042 return;
1043 }
1044 l += 5;
1045 STREAM_GET(&p.u.prefix, s, PSIZE(p.prefixlen));
1046 l += PSIZE(p.prefixlen);
1047 if (flags & ZEBRA_FEC_REGISTER_LABEL_INDEX) {
1048 STREAM_GETL(s, label_index);
1049 l += 4;
1050 } else
1051 label_index = MPLS_INVALID_LABEL_INDEX;
1052 zebra_mpls_fec_register(zvrf, &p, label_index, client);
1053 }
1054
1055 stream_failure:
1056 return;
1057 }
1058
1059 /* FEC unregister */
1060 static void zread_fec_unregister(ZAPI_HANDLER_ARGS)
1061 {
1062 struct stream *s;
1063 unsigned short l = 0;
1064 struct prefix p;
1065 uint16_t flags;
1066
1067 s = msg;
1068 zvrf = vrf_info_lookup(VRF_DEFAULT);
1069 if (!zvrf)
1070 return; // unexpected
1071
1072 /*
1073 * The minimum amount of data that can be sent for one
1074 * fec unregistration
1075 */
1076 if (hdr->length < ZEBRA_MIN_FEC_LENGTH) {
1077 zlog_err(
1078 "fec_unregister: Received a fec unregister of hdr->length %d, it is of insufficient size to properly decode",
1079 hdr->length);
1080 return;
1081 }
1082
1083 while (l < hdr->length) {
1084 STREAM_GETW(s, flags);
1085 if (flags != 0)
1086 goto stream_failure;
1087
1088 memset(&p, 0, sizeof(p));
1089 STREAM_GETW(s, p.family);
1090 if (p.family != AF_INET && p.family != AF_INET6) {
1091 zlog_err(
1092 "fec_unregister: Received unknown family type %d\n",
1093 p.family);
1094 return;
1095 }
1096 STREAM_GETC(s, p.prefixlen);
1097 if ((p.family == AF_INET && p.prefixlen > IPV4_MAX_BITLEN)
1098 || (p.family == AF_INET6
1099 && p.prefixlen > IPV6_MAX_BITLEN)) {
1100 zlog_warn(
1101 "%s: Received prefix hdr->length %d which is greater than %d can support",
1102 __PRETTY_FUNCTION__, p.prefixlen, p.family);
1103 return;
1104 }
1105 l += 5;
1106 STREAM_GET(&p.u.prefix, s, PSIZE(p.prefixlen));
1107 l += PSIZE(p.prefixlen);
1108 zebra_mpls_fec_unregister(zvrf, &p, client);
1109 }
1110
1111 stream_failure:
1112 return;
1113 }
1114
1115
1116 /*
1117 * Register zebra server interface information.
1118 * Send current all interface and address information.
1119 */
1120 static void zread_interface_add(ZAPI_HANDLER_ARGS)
1121 {
1122 struct vrf *vrf;
1123 struct interface *ifp;
1124
1125 /* Interface information is needed. */
1126 vrf_bitmap_set(client->ifinfo, zvrf_id(zvrf));
1127
1128 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) {
1129 FOR_ALL_INTERFACES (vrf, ifp) {
1130 /* Skip pseudo interface. */
1131 if (!CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE))
1132 continue;
1133
1134 zsend_interface_add(client, ifp);
1135 zsend_interface_addresses(client, ifp);
1136 }
1137 }
1138 }
1139
1140 /* Unregister zebra server interface information. */
1141 static void zread_interface_delete(ZAPI_HANDLER_ARGS)
1142 {
1143 vrf_bitmap_unset(client->ifinfo, zvrf_id(zvrf));
1144 }
1145
1146 void zserv_nexthop_num_warn(const char *caller, const struct prefix *p,
1147 const unsigned int nexthop_num)
1148 {
1149 if (nexthop_num > multipath_num) {
1150 char buff[PREFIX2STR_BUFFER];
1151 prefix2str(p, buff, sizeof(buff));
1152 zlog_warn(
1153 "%s: Prefix %s has %d nexthops, but we can only use the first %d",
1154 caller, buff, nexthop_num, multipath_num);
1155 }
1156 }
1157
1158 static void zread_route_add(ZAPI_HANDLER_ARGS)
1159 {
1160 struct stream *s;
1161 struct zapi_route api;
1162 struct zapi_nexthop *api_nh;
1163 afi_t afi;
1164 struct prefix_ipv6 *src_p = NULL;
1165 struct route_entry *re;
1166 struct nexthop *nexthop = NULL;
1167 int i, ret;
1168 vrf_id_t vrf_id = 0;
1169 struct ipaddr vtep_ip;
1170
1171 s = msg;
1172 if (zapi_route_decode(s, &api) < 0) {
1173 if (IS_ZEBRA_DEBUG_RECV)
1174 zlog_debug("%s: Unable to decode zapi_route sent",
1175 __PRETTY_FUNCTION__);
1176 return;
1177 }
1178
1179 if (IS_ZEBRA_DEBUG_RECV) {
1180 char buf_prefix[PREFIX_STRLEN];
1181 prefix2str(&api.prefix, buf_prefix, sizeof(buf_prefix));
1182 zlog_debug("%s: p=%s, ZAPI_MESSAGE_LABEL: %sset, flags=0x%x",
1183 __func__, buf_prefix,
1184 (CHECK_FLAG(api.message, ZAPI_MESSAGE_LABEL) ? ""
1185 : "un"),
1186 api.flags);
1187 }
1188
1189 /* Allocate new route. */
1190 vrf_id = zvrf_id(zvrf);
1191 re = XCALLOC(MTYPE_RE, sizeof(struct route_entry));
1192 re->type = api.type;
1193 re->instance = api.instance;
1194 re->flags = api.flags;
1195 re->uptime = time(NULL);
1196 re->vrf_id = vrf_id;
1197 if (api.tableid && vrf_id == VRF_DEFAULT)
1198 re->table = api.tableid;
1199 else
1200 re->table = zvrf->table_id;
1201
1202 /*
1203 * TBD should _all_ of the nexthop add operations use
1204 * api_nh->vrf_id instead of re->vrf_id ? I only changed
1205 * for cases NEXTHOP_TYPE_IPV4 and NEXTHOP_TYPE_IPV6.
1206 */
1207 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP)) {
1208 for (i = 0; i < api.nexthop_num; i++) {
1209 api_nh = &api.nexthops[i];
1210 ifindex_t ifindex = 0;
1211
1212 if (IS_ZEBRA_DEBUG_RECV) {
1213 zlog_debug("nh type %d", api_nh->type);
1214 }
1215
1216 switch (api_nh->type) {
1217 case NEXTHOP_TYPE_IFINDEX:
1218 nexthop = route_entry_nexthop_ifindex_add(
1219 re, api_nh->ifindex, api_nh->vrf_id);
1220 break;
1221 case NEXTHOP_TYPE_IPV4:
1222 if (IS_ZEBRA_DEBUG_RECV) {
1223 char nhbuf[INET6_ADDRSTRLEN] = {0};
1224 inet_ntop(AF_INET, &api_nh->gate.ipv4,
1225 nhbuf, INET6_ADDRSTRLEN);
1226 zlog_debug("%s: nh=%s, vrf_id=%d",
1227 __func__, nhbuf,
1228 api_nh->vrf_id);
1229 }
1230 nexthop = route_entry_nexthop_ipv4_add(
1231 re, &api_nh->gate.ipv4, NULL,
1232 api_nh->vrf_id);
1233 break;
1234 case NEXTHOP_TYPE_IPV4_IFINDEX:
1235
1236 memset(&vtep_ip, 0, sizeof(struct ipaddr));
1237 if (CHECK_FLAG(api.flags,
1238 ZEBRA_FLAG_EVPN_ROUTE)) {
1239 ifindex = get_l3vni_svi_ifindex(vrf_id);
1240 } else {
1241 ifindex = api_nh->ifindex;
1242 }
1243
1244 if (IS_ZEBRA_DEBUG_RECV) {
1245 char nhbuf[INET6_ADDRSTRLEN] = {0};
1246 inet_ntop(AF_INET, &api_nh->gate.ipv4,
1247 nhbuf, INET6_ADDRSTRLEN);
1248 zlog_debug(
1249 "%s: nh=%s, vrf_id=%d (re->vrf_id=%d), ifindex=%d",
1250 __func__, nhbuf, api_nh->vrf_id,
1251 re->vrf_id, ifindex);
1252 }
1253 nexthop = route_entry_nexthop_ipv4_ifindex_add(
1254 re, &api_nh->gate.ipv4, NULL, ifindex,
1255 api_nh->vrf_id);
1256
1257 /* if this an EVPN route entry,
1258 * program the nh as neigh
1259 */
1260 if (CHECK_FLAG(api.flags,
1261 ZEBRA_FLAG_EVPN_ROUTE)) {
1262 SET_FLAG(nexthop->flags,
1263 NEXTHOP_FLAG_EVPN_RVTEP);
1264 vtep_ip.ipa_type = IPADDR_V4;
1265 memcpy(&(vtep_ip.ipaddr_v4),
1266 &(api_nh->gate.ipv4),
1267 sizeof(struct in_addr));
1268 zebra_vxlan_evpn_vrf_route_add(
1269 vrf_id, &api.rmac, &vtep_ip,
1270 &api.prefix);
1271 }
1272 break;
1273 case NEXTHOP_TYPE_IPV6:
1274 nexthop = route_entry_nexthop_ipv6_add(
1275 re, &api_nh->gate.ipv6, api_nh->vrf_id);
1276 break;
1277 case NEXTHOP_TYPE_IPV6_IFINDEX:
1278 memset(&vtep_ip, 0, sizeof(struct ipaddr));
1279 if (CHECK_FLAG(api.flags,
1280 ZEBRA_FLAG_EVPN_ROUTE)) {
1281 ifindex =
1282 get_l3vni_svi_ifindex(vrf_id);
1283 } else {
1284 ifindex = api_nh->ifindex;
1285 }
1286
1287 nexthop = route_entry_nexthop_ipv6_ifindex_add(
1288 re, &api_nh->gate.ipv6, ifindex,
1289 api_nh->vrf_id);
1290
1291 /* if this an EVPN route entry,
1292 * program the nh as neigh
1293 */
1294 if (CHECK_FLAG(api.flags,
1295 ZEBRA_FLAG_EVPN_ROUTE)) {
1296 SET_FLAG(nexthop->flags,
1297 NEXTHOP_FLAG_EVPN_RVTEP);
1298 vtep_ip.ipa_type = IPADDR_V6;
1299 memcpy(&vtep_ip.ipaddr_v6,
1300 &(api_nh->gate.ipv6),
1301 sizeof(struct in6_addr));
1302 zebra_vxlan_evpn_vrf_route_add(
1303 vrf_id,
1304 &api.rmac,
1305 &vtep_ip,
1306 &api.prefix);
1307 }
1308 break;
1309 case NEXTHOP_TYPE_BLACKHOLE:
1310 nexthop = route_entry_nexthop_blackhole_add(
1311 re, api_nh->bh_type);
1312 break;
1313 }
1314
1315 if (!nexthop) {
1316 zlog_warn(
1317 "%s: Nexthops Specified: %d but we failed to properly create one",
1318 __PRETTY_FUNCTION__, api.nexthop_num);
1319 nexthops_free(re->ng.nexthop);
1320 XFREE(MTYPE_RE, re);
1321 return;
1322 }
1323 /* MPLS labels for BGP-LU or Segment Routing */
1324 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_LABEL)
1325 && api_nh->type != NEXTHOP_TYPE_IFINDEX
1326 && api_nh->type != NEXTHOP_TYPE_BLACKHOLE) {
1327 enum lsp_types_t label_type;
1328
1329 label_type =
1330 lsp_type_from_re_type(client->proto);
1331
1332 if (IS_ZEBRA_DEBUG_RECV) {
1333 zlog_debug(
1334 "%s: adding %d labels of type %d (1st=%u)",
1335 __func__, api_nh->label_num,
1336 label_type, api_nh->labels[0]);
1337 }
1338
1339 nexthop_add_labels(nexthop, label_type,
1340 api_nh->label_num,
1341 &api_nh->labels[0]);
1342 }
1343 }
1344 }
1345
1346 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_DISTANCE))
1347 re->distance = api.distance;
1348 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_METRIC))
1349 re->metric = api.metric;
1350 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_TAG))
1351 re->tag = api.tag;
1352 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_MTU))
1353 re->mtu = api.mtu;
1354
1355 afi = family2afi(api.prefix.family);
1356 if (afi != AFI_IP6 && CHECK_FLAG(api.message, ZAPI_MESSAGE_SRCPFX)) {
1357 zlog_warn("%s: Received SRC Prefix but afi is not v6",
1358 __PRETTY_FUNCTION__);
1359 nexthops_free(re->ng.nexthop);
1360 XFREE(MTYPE_RE, re);
1361 return;
1362 }
1363 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_SRCPFX))
1364 src_p = &api.src_prefix;
1365
1366 ret = rib_add_multipath(afi, api.safi, &api.prefix, src_p, re);
1367
1368 /* Stats */
1369 switch (api.prefix.family) {
1370 case AF_INET:
1371 if (ret > 0)
1372 client->v4_route_add_cnt++;
1373 else if (ret < 0)
1374 client->v4_route_upd8_cnt++;
1375 break;
1376 case AF_INET6:
1377 if (ret > 0)
1378 client->v6_route_add_cnt++;
1379 else if (ret < 0)
1380 client->v6_route_upd8_cnt++;
1381 break;
1382 }
1383 }
1384
1385 static void zread_route_del(ZAPI_HANDLER_ARGS)
1386 {
1387 struct stream *s;
1388 struct zapi_route api;
1389 afi_t afi;
1390 struct prefix_ipv6 *src_p = NULL;
1391 uint32_t table_id;
1392
1393 s = msg;
1394 if (zapi_route_decode(s, &api) < 0)
1395 return;
1396
1397 afi = family2afi(api.prefix.family);
1398 if (afi != AFI_IP6 && CHECK_FLAG(api.message, ZAPI_MESSAGE_SRCPFX)) {
1399 zlog_warn("%s: Received a src prefix while afi is not v6",
1400 __PRETTY_FUNCTION__);
1401 return;
1402 }
1403 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_SRCPFX))
1404 src_p = &api.src_prefix;
1405
1406 if (api.vrf_id == VRF_DEFAULT && api.tableid != 0)
1407 table_id = api.tableid;
1408 else
1409 table_id = zvrf->table_id;
1410
1411 rib_delete(afi, api.safi, zvrf_id(zvrf), api.type, api.instance,
1412 api.flags, &api.prefix, src_p, NULL, table_id,
1413 api.metric, false, &api.rmac);
1414
1415 /* Stats */
1416 switch (api.prefix.family) {
1417 case AF_INET:
1418 client->v4_route_del_cnt++;
1419 break;
1420 case AF_INET6:
1421 client->v6_route_del_cnt++;
1422 break;
1423 }
1424 }
1425
1426 /* This function support multiple nexthop. */
1427 /*
1428 * Parse the ZEBRA_IPV4_ROUTE_ADD sent from client. Update re and
1429 * add kernel route.
1430 */
1431 static void zread_ipv4_add(ZAPI_HANDLER_ARGS)
1432 {
1433 int i;
1434 struct route_entry *re;
1435 struct prefix p;
1436 uint8_t message;
1437 struct in_addr nhop_addr;
1438 uint8_t nexthop_num;
1439 uint8_t nexthop_type;
1440 struct stream *s;
1441 ifindex_t ifindex;
1442 safi_t safi;
1443 int ret;
1444 enum lsp_types_t label_type = ZEBRA_LSP_NONE;
1445 mpls_label_t label;
1446 struct nexthop *nexthop;
1447 enum blackhole_type bh_type = BLACKHOLE_NULL;
1448
1449 /* Get input stream. */
1450 s = msg;
1451
1452 /* Allocate new re. */
1453 re = XCALLOC(MTYPE_RE, sizeof(struct route_entry));
1454
1455 /* Type, flags, message. */
1456 STREAM_GETC(s, re->type);
1457 if (re->type > ZEBRA_ROUTE_MAX) {
1458 zlog_warn("%s: Specified route type %d is not a legal value\n",
1459 __PRETTY_FUNCTION__, re->type);
1460 XFREE(MTYPE_RE, re);
1461 return;
1462 }
1463 STREAM_GETW(s, re->instance);
1464 STREAM_GETL(s, re->flags);
1465 STREAM_GETC(s, message);
1466 STREAM_GETW(s, safi);
1467 re->uptime = time(NULL);
1468
1469 /* IPv4 prefix. */
1470 memset(&p, 0, sizeof(struct prefix_ipv4));
1471 p.family = AF_INET;
1472 STREAM_GETC(s, p.prefixlen);
1473 if (p.prefixlen > IPV4_MAX_BITLEN) {
1474 zlog_warn(
1475 "%s: Specified prefix length %d is greater than what v4 can be",
1476 __PRETTY_FUNCTION__, p.prefixlen);
1477 XFREE(MTYPE_RE, re);
1478 return;
1479 }
1480 STREAM_GET(&p.u.prefix4, s, PSIZE(p.prefixlen));
1481
1482 /* VRF ID */
1483 re->vrf_id = zvrf_id(zvrf);
1484
1485 /* Nexthop parse. */
1486 if (CHECK_FLAG(message, ZAPI_MESSAGE_NEXTHOP)) {
1487 STREAM_GETC(s, nexthop_num);
1488 zserv_nexthop_num_warn(__func__, (const struct prefix *)&p,
1489 nexthop_num);
1490
1491 if (CHECK_FLAG(message, ZAPI_MESSAGE_LABEL))
1492 label_type = lsp_type_from_re_type(client->proto);
1493
1494 for (i = 0; i < nexthop_num; i++) {
1495 STREAM_GETC(s, nexthop_type);
1496
1497 switch (nexthop_type) {
1498 case NEXTHOP_TYPE_IFINDEX:
1499 STREAM_GETL(s, ifindex);
1500 route_entry_nexthop_ifindex_add(re, ifindex,
1501 re->vrf_id);
1502 break;
1503 case NEXTHOP_TYPE_IPV4:
1504 STREAM_GET(&nhop_addr.s_addr, s,
1505 IPV4_MAX_BYTELEN);
1506 nexthop = route_entry_nexthop_ipv4_add(
1507 re, &nhop_addr, NULL, re->vrf_id);
1508 /* For labeled-unicast, each nexthop is followed
1509 * by label. */
1510 if (CHECK_FLAG(message, ZAPI_MESSAGE_LABEL)) {
1511 STREAM_GETL(s, label);
1512 nexthop_add_labels(nexthop, label_type,
1513 1, &label);
1514 }
1515 break;
1516 case NEXTHOP_TYPE_IPV4_IFINDEX:
1517 STREAM_GET(&nhop_addr.s_addr, s,
1518 IPV4_MAX_BYTELEN);
1519 STREAM_GETL(s, ifindex);
1520 route_entry_nexthop_ipv4_ifindex_add(
1521 re, &nhop_addr, NULL, ifindex,
1522 re->vrf_id);
1523 break;
1524 case NEXTHOP_TYPE_IPV6:
1525 zlog_warn(
1526 "%s: Please use ZEBRA_ROUTE_ADD if you want to pass v6 nexthops",
1527 __PRETTY_FUNCTION__);
1528 nexthops_free(re->ng.nexthop);
1529 XFREE(MTYPE_RE, re);
1530 return;
1531 break;
1532 case NEXTHOP_TYPE_BLACKHOLE:
1533 route_entry_nexthop_blackhole_add(re, bh_type);
1534 break;
1535 default:
1536 zlog_warn(
1537 "%s: Specified nexthop type: %d does not exist",
1538 __PRETTY_FUNCTION__, nexthop_type);
1539 nexthops_free(re->ng.nexthop);
1540 XFREE(MTYPE_RE, re);
1541 return;
1542 }
1543 }
1544 }
1545
1546 /* Distance. */
1547 if (CHECK_FLAG(message, ZAPI_MESSAGE_DISTANCE))
1548 STREAM_GETC(s, re->distance);
1549
1550 /* Metric. */
1551 if (CHECK_FLAG(message, ZAPI_MESSAGE_METRIC))
1552 STREAM_GETL(s, re->metric);
1553
1554 /* Tag */
1555 if (CHECK_FLAG(message, ZAPI_MESSAGE_TAG))
1556 STREAM_GETL(s, re->tag);
1557 else
1558 re->tag = 0;
1559
1560 if (CHECK_FLAG(message, ZAPI_MESSAGE_MTU))
1561 STREAM_GETL(s, re->mtu);
1562 else
1563 re->mtu = 0;
1564
1565 /* Table */
1566 re->table = zvrf->table_id;
1567
1568 ret = rib_add_multipath(AFI_IP, safi, &p, NULL, re);
1569
1570 /* Stats */
1571 if (ret > 0)
1572 client->v4_route_add_cnt++;
1573 else if (ret < 0)
1574 client->v4_route_upd8_cnt++;
1575
1576 return;
1577
1578 stream_failure:
1579 nexthops_free(re->ng.nexthop);
1580 XFREE(MTYPE_RE, re);
1581 }
1582
1583 /* Zebra server IPv4 prefix delete function. */
1584 static void zread_ipv4_delete(ZAPI_HANDLER_ARGS)
1585 {
1586 struct stream *s;
1587 struct zapi_ipv4 api;
1588 struct prefix p;
1589 uint32_t table_id;
1590
1591 s = msg;
1592
1593 /* Type, flags, message. */
1594 STREAM_GETC(s, api.type);
1595 STREAM_GETW(s, api.instance);
1596 STREAM_GETL(s, api.flags);
1597 STREAM_GETC(s, api.message);
1598 STREAM_GETW(s, api.safi);
1599
1600 /* IPv4 prefix. */
1601 memset(&p, 0, sizeof(struct prefix));
1602 p.family = AF_INET;
1603 STREAM_GETC(s, p.prefixlen);
1604 if (p.prefixlen > IPV4_MAX_BITLEN) {
1605 zlog_warn("%s: Passed in prefixlen %d is impossible",
1606 __PRETTY_FUNCTION__, p.prefixlen);
1607 return;
1608 }
1609 STREAM_GET(&p.u.prefix4, s, PSIZE(p.prefixlen));
1610
1611 table_id = zvrf->table_id;
1612
1613 rib_delete(AFI_IP, api.safi, zvrf_id(zvrf), api.type, api.instance,
1614 api.flags, &p, NULL, NULL, table_id, 0, false, NULL);
1615 client->v4_route_del_cnt++;
1616
1617 stream_failure:
1618 return;
1619 }
1620
1621 /* MRIB Nexthop lookup for IPv4. */
1622 static void zread_ipv4_nexthop_lookup_mrib(ZAPI_HANDLER_ARGS)
1623 {
1624 struct in_addr addr;
1625 struct route_entry *re;
1626
1627 STREAM_GET(&addr.s_addr, msg, IPV4_MAX_BYTELEN);
1628 re = rib_match_ipv4_multicast(zvrf_id(zvrf), addr, NULL);
1629 zsend_ipv4_nexthop_lookup_mrib(client, addr, re, zvrf);
1630
1631 stream_failure:
1632 return;
1633 }
1634
1635 /* Zebra server IPv6 prefix add function. */
1636 static void zread_ipv4_route_ipv6_nexthop_add(ZAPI_HANDLER_ARGS)
1637 {
1638 unsigned int i;
1639 struct stream *s;
1640 struct in6_addr nhop_addr;
1641 struct route_entry *re;
1642 uint8_t message;
1643 uint8_t nexthop_num;
1644 uint8_t nexthop_type;
1645 struct prefix p;
1646 safi_t safi;
1647 static struct in6_addr nexthops[MULTIPATH_NUM];
1648 static unsigned int ifindices[MULTIPATH_NUM];
1649 int ret;
1650 static mpls_label_t labels[MULTIPATH_NUM];
1651 enum lsp_types_t label_type = ZEBRA_LSP_NONE;
1652 mpls_label_t label;
1653 struct nexthop *nexthop;
1654 enum blackhole_type bh_type = BLACKHOLE_NULL;
1655
1656 /* Get input stream. */
1657 s = msg;
1658
1659 memset(&nhop_addr, 0, sizeof(struct in6_addr));
1660
1661 /* Allocate new re. */
1662 re = XCALLOC(MTYPE_RE, sizeof(struct route_entry));
1663
1664 /* Type, flags, message. */
1665 STREAM_GETC(s, re->type);
1666 if (re->type > ZEBRA_ROUTE_MAX) {
1667 zlog_warn("%s: Specified route type: %d is not a legal value\n",
1668 __PRETTY_FUNCTION__, re->type);
1669 XFREE(MTYPE_RE, re);
1670 return;
1671 }
1672 STREAM_GETW(s, re->instance);
1673 STREAM_GETL(s, re->flags);
1674 STREAM_GETC(s, message);
1675 STREAM_GETW(s, safi);
1676 re->uptime = time(NULL);
1677
1678 /* IPv4 prefix. */
1679 memset(&p, 0, sizeof(struct prefix_ipv4));
1680 p.family = AF_INET;
1681 STREAM_GETC(s, p.prefixlen);
1682 if (p.prefixlen > IPV4_MAX_BITLEN) {
1683 zlog_warn(
1684 "%s: Prefix Length %d is greater than what a v4 address can use",
1685 __PRETTY_FUNCTION__, p.prefixlen);
1686 XFREE(MTYPE_RE, re);
1687 return;
1688 }
1689 STREAM_GET(&p.u.prefix4, s, PSIZE(p.prefixlen));
1690
1691 /* VRF ID */
1692 re->vrf_id = zvrf_id(zvrf);
1693
1694 /* We need to give nh-addr, nh-ifindex with the same next-hop object
1695 * to the re to ensure that IPv6 multipathing works; need to coalesce
1696 * these. Clients should send the same number of paired set of
1697 * next-hop-addr/next-hop-ifindices. */
1698 if (CHECK_FLAG(message, ZAPI_MESSAGE_NEXTHOP)) {
1699 unsigned int nh_count = 0;
1700 unsigned int if_count = 0;
1701 unsigned int max_nh_if = 0;
1702
1703 STREAM_GETC(s, nexthop_num);
1704 zserv_nexthop_num_warn(__func__, (const struct prefix *)&p,
1705 nexthop_num);
1706
1707 if (CHECK_FLAG(message, ZAPI_MESSAGE_LABEL))
1708 label_type = lsp_type_from_re_type(client->proto);
1709
1710 for (i = 0; i < nexthop_num; i++) {
1711 STREAM_GETC(s, nexthop_type);
1712
1713 switch (nexthop_type) {
1714 case NEXTHOP_TYPE_IPV6:
1715 STREAM_GET(&nhop_addr, s, 16);
1716 if (nh_count < MULTIPATH_NUM) {
1717 /* For labeled-unicast, each nexthop is
1718 * followed by label. */
1719 if (CHECK_FLAG(message,
1720 ZAPI_MESSAGE_LABEL)) {
1721 STREAM_GETL(s, label);
1722 labels[nh_count] = label;
1723 }
1724 nexthops[nh_count] = nhop_addr;
1725 nh_count++;
1726 }
1727 break;
1728 case NEXTHOP_TYPE_IFINDEX:
1729 if (if_count < multipath_num) {
1730 STREAM_GETL(s, ifindices[if_count++]);
1731 }
1732 break;
1733 case NEXTHOP_TYPE_BLACKHOLE:
1734 route_entry_nexthop_blackhole_add(re, bh_type);
1735 break;
1736 default:
1737 zlog_warn(
1738 "%s: Please use ZEBRA_ROUTE_ADD if you want to pass non v6 nexthops",
1739 __PRETTY_FUNCTION__);
1740 nexthops_free(re->ng.nexthop);
1741 XFREE(MTYPE_RE, re);
1742 return;
1743 }
1744 }
1745
1746 max_nh_if = (nh_count > if_count) ? nh_count : if_count;
1747 for (i = 0; i < max_nh_if; i++) {
1748 if ((i < nh_count)
1749 && !IN6_IS_ADDR_UNSPECIFIED(&nexthops[i])) {
1750 if ((i < if_count) && ifindices[i])
1751 nexthop =
1752 route_entry_nexthop_ipv6_ifindex_add(
1753 re, &nexthops[i],
1754 ifindices[i],
1755 re->vrf_id);
1756 else
1757 nexthop = route_entry_nexthop_ipv6_add(
1758 re, &nexthops[i], re->vrf_id);
1759
1760 if (CHECK_FLAG(message, ZAPI_MESSAGE_LABEL))
1761 nexthop_add_labels(nexthop, label_type,
1762 1, &labels[i]);
1763 } else {
1764 if ((i < if_count) && ifindices[i])
1765 route_entry_nexthop_ifindex_add(
1766 re, ifindices[i], re->vrf_id);
1767 }
1768 }
1769 }
1770
1771 /* Distance. */
1772 if (CHECK_FLAG(message, ZAPI_MESSAGE_DISTANCE))
1773 STREAM_GETC(s, re->distance);
1774
1775 /* Metric. */
1776 if (CHECK_FLAG(message, ZAPI_MESSAGE_METRIC))
1777 STREAM_GETL(s, re->metric);
1778
1779 /* Tag */
1780 if (CHECK_FLAG(message, ZAPI_MESSAGE_TAG))
1781 STREAM_GETL(s, re->tag);
1782 else
1783 re->tag = 0;
1784
1785 if (CHECK_FLAG(message, ZAPI_MESSAGE_MTU))
1786 STREAM_GETL(s, re->mtu);
1787 else
1788 re->mtu = 0;
1789
1790 /* Table */
1791 re->table = zvrf->table_id;
1792
1793 ret = rib_add_multipath(AFI_IP6, safi, &p, NULL, re);
1794 /* Stats */
1795 if (ret > 0)
1796 client->v4_route_add_cnt++;
1797 else if (ret < 0)
1798 client->v4_route_upd8_cnt++;
1799
1800 return;
1801
1802 stream_failure:
1803 nexthops_free(re->ng.nexthop);
1804 XFREE(MTYPE_RE, re);
1805 }
1806
1807 static void zread_ipv6_add(ZAPI_HANDLER_ARGS)
1808 {
1809 unsigned int i;
1810 struct stream *s;
1811 struct in6_addr nhop_addr;
1812 ifindex_t ifindex;
1813 struct route_entry *re;
1814 uint8_t message;
1815 uint8_t nexthop_num;
1816 uint8_t nexthop_type;
1817 struct prefix p;
1818 struct prefix_ipv6 src_p, *src_pp;
1819 safi_t safi;
1820 static struct in6_addr nexthops[MULTIPATH_NUM];
1821 static unsigned int ifindices[MULTIPATH_NUM];
1822 int ret;
1823 static mpls_label_t labels[MULTIPATH_NUM];
1824 enum lsp_types_t label_type = ZEBRA_LSP_NONE;
1825 mpls_label_t label;
1826 struct nexthop *nexthop;
1827 enum blackhole_type bh_type = BLACKHOLE_NULL;
1828
1829 /* Get input stream. */
1830 s = msg;
1831
1832 memset(&nhop_addr, 0, sizeof(struct in6_addr));
1833
1834 /* Allocate new re. */
1835 re = XCALLOC(MTYPE_RE, sizeof(struct route_entry));
1836
1837 /* Type, flags, message. */
1838 STREAM_GETC(s, re->type);
1839 if (re->type > ZEBRA_ROUTE_MAX) {
1840 zlog_warn("%s: Specified route type: %d is not a legal value\n",
1841 __PRETTY_FUNCTION__, re->type);
1842 XFREE(MTYPE_RE, re);
1843 return;
1844 }
1845 STREAM_GETW(s, re->instance);
1846 STREAM_GETL(s, re->flags);
1847 STREAM_GETC(s, message);
1848 STREAM_GETW(s, safi);
1849 re->uptime = time(NULL);
1850
1851 /* IPv6 prefix. */
1852 memset(&p, 0, sizeof(p));
1853 p.family = AF_INET6;
1854 STREAM_GETC(s, p.prefixlen);
1855 if (p.prefixlen > IPV6_MAX_BITLEN) {
1856 zlog_warn(
1857 "%s: Specified prefix length %d is to large for v6 prefix",
1858 __PRETTY_FUNCTION__, p.prefixlen);
1859 XFREE(MTYPE_RE, re);
1860 return;
1861 }
1862 STREAM_GET(&p.u.prefix6, s, PSIZE(p.prefixlen));
1863
1864 if (CHECK_FLAG(message, ZAPI_MESSAGE_SRCPFX)) {
1865 memset(&src_p, 0, sizeof(src_p));
1866 src_p.family = AF_INET6;
1867 STREAM_GETC(s, src_p.prefixlen);
1868 if (src_p.prefixlen > IPV6_MAX_BITLEN) {
1869 zlog_warn(
1870 "%s: Specified src prefix length %d is to large for v6 prefix",
1871 __PRETTY_FUNCTION__, src_p.prefixlen);
1872 XFREE(MTYPE_RE, re);
1873 return;
1874 }
1875 STREAM_GET(&src_p.prefix, s, PSIZE(src_p.prefixlen));
1876 src_pp = &src_p;
1877 } else
1878 src_pp = NULL;
1879
1880 /* VRF ID */
1881 re->vrf_id = zvrf_id(zvrf);
1882
1883 /* We need to give nh-addr, nh-ifindex with the same next-hop object
1884 * to the re to ensure that IPv6 multipathing works; need to coalesce
1885 * these. Clients should send the same number of paired set of
1886 * next-hop-addr/next-hop-ifindices. */
1887 if (CHECK_FLAG(message, ZAPI_MESSAGE_NEXTHOP)) {
1888 unsigned int nh_count = 0;
1889 unsigned int if_count = 0;
1890 unsigned int max_nh_if = 0;
1891
1892 STREAM_GETC(s, nexthop_num);
1893 zserv_nexthop_num_warn(__func__, (const struct prefix *)&p,
1894 nexthop_num);
1895
1896 if (CHECK_FLAG(message, ZAPI_MESSAGE_LABEL))
1897 label_type = lsp_type_from_re_type(client->proto);
1898
1899 for (i = 0; i < nexthop_num; i++) {
1900 STREAM_GETC(s, nexthop_type);
1901
1902 switch (nexthop_type) {
1903 case NEXTHOP_TYPE_IPV6:
1904 STREAM_GET(&nhop_addr, s, 16);
1905 if (nh_count < MULTIPATH_NUM) {
1906 /* For labeled-unicast, each nexthop is
1907 * followed by label. */
1908 if (CHECK_FLAG(message,
1909 ZAPI_MESSAGE_LABEL)) {
1910 STREAM_GETL(s, label);
1911 labels[nh_count] = label;
1912 }
1913 nexthops[nh_count++] = nhop_addr;
1914 }
1915 break;
1916 case NEXTHOP_TYPE_IPV6_IFINDEX:
1917 STREAM_GET(&nhop_addr, s, 16);
1918 STREAM_GETL(s, ifindex);
1919 route_entry_nexthop_ipv6_ifindex_add(
1920 re, &nhop_addr, ifindex, re->vrf_id);
1921 break;
1922 case NEXTHOP_TYPE_IFINDEX:
1923 if (if_count < multipath_num) {
1924 STREAM_GETL(s, ifindices[if_count++]);
1925 }
1926 break;
1927 case NEXTHOP_TYPE_BLACKHOLE:
1928 route_entry_nexthop_blackhole_add(re, bh_type);
1929 break;
1930 default:
1931 zlog_warn(
1932 "%s: Please use ZEBRA_ROUTE_ADD if you want to pass non v6 nexthops",
1933 __PRETTY_FUNCTION__);
1934 nexthops_free(re->ng.nexthop);
1935 XFREE(MTYPE_RE, re);
1936 return;
1937 }
1938 }
1939
1940 max_nh_if = (nh_count > if_count) ? nh_count : if_count;
1941 for (i = 0; i < max_nh_if; i++) {
1942 if ((i < nh_count)
1943 && !IN6_IS_ADDR_UNSPECIFIED(&nexthops[i])) {
1944 if ((i < if_count) && ifindices[i])
1945 nexthop =
1946 route_entry_nexthop_ipv6_ifindex_add(
1947 re, &nexthops[i],
1948 ifindices[i],
1949 re->vrf_id);
1950 else
1951 nexthop = route_entry_nexthop_ipv6_add(
1952 re, &nexthops[i], re->vrf_id);
1953 if (CHECK_FLAG(message, ZAPI_MESSAGE_LABEL))
1954 nexthop_add_labels(nexthop, label_type,
1955 1, &labels[i]);
1956 } else {
1957 if ((i < if_count) && ifindices[i])
1958 route_entry_nexthop_ifindex_add(
1959 re, ifindices[i], re->vrf_id);
1960 }
1961 }
1962 }
1963
1964 /* Distance. */
1965 if (CHECK_FLAG(message, ZAPI_MESSAGE_DISTANCE))
1966 STREAM_GETC(s, re->distance);
1967
1968 /* Metric. */
1969 if (CHECK_FLAG(message, ZAPI_MESSAGE_METRIC))
1970 STREAM_GETL(s, re->metric);
1971
1972 /* Tag */
1973 if (CHECK_FLAG(message, ZAPI_MESSAGE_TAG))
1974 STREAM_GETL(s, re->tag);
1975 else
1976 re->tag = 0;
1977
1978 if (CHECK_FLAG(message, ZAPI_MESSAGE_MTU))
1979 STREAM_GETL(s, re->mtu);
1980 else
1981 re->mtu = 0;
1982
1983 re->table = zvrf->table_id;
1984
1985 ret = rib_add_multipath(AFI_IP6, safi, &p, src_pp, re);
1986 /* Stats */
1987 if (ret > 0)
1988 client->v6_route_add_cnt++;
1989 else if (ret < 0)
1990 client->v6_route_upd8_cnt++;
1991
1992 return;
1993
1994 stream_failure:
1995 nexthops_free(re->ng.nexthop);
1996 XFREE(MTYPE_RE, re);
1997 }
1998
1999 /* Zebra server IPv6 prefix delete function. */
2000 static void zread_ipv6_delete(ZAPI_HANDLER_ARGS)
2001 {
2002 struct stream *s;
2003 struct zapi_ipv6 api;
2004 struct prefix p;
2005 struct prefix_ipv6 src_p, *src_pp;
2006
2007 s = msg;
2008
2009 /* Type, flags, message. */
2010 STREAM_GETC(s, api.type);
2011 STREAM_GETW(s, api.instance);
2012 STREAM_GETL(s, api.flags);
2013 STREAM_GETC(s, api.message);
2014 STREAM_GETW(s, api.safi);
2015
2016 /* IPv4 prefix. */
2017 memset(&p, 0, sizeof(struct prefix));
2018 p.family = AF_INET6;
2019 STREAM_GETC(s, p.prefixlen);
2020 STREAM_GET(&p.u.prefix6, s, PSIZE(p.prefixlen));
2021
2022 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_SRCPFX)) {
2023 memset(&src_p, 0, sizeof(struct prefix_ipv6));
2024 src_p.family = AF_INET6;
2025 STREAM_GETC(s, src_p.prefixlen);
2026 STREAM_GET(&src_p.prefix, s, PSIZE(src_p.prefixlen));
2027 src_pp = &src_p;
2028 } else
2029 src_pp = NULL;
2030
2031 rib_delete(AFI_IP6, api.safi, zvrf_id(zvrf), api.type, api.instance,
2032 api.flags, &p, src_pp, NULL, client->rtm_table, 0, false,
2033 NULL);
2034
2035 client->v6_route_del_cnt++;
2036
2037 stream_failure:
2038 return;
2039 }
2040
2041 /* Register zebra server router-id information. Send current router-id */
2042 static void zread_router_id_add(ZAPI_HANDLER_ARGS)
2043 {
2044 struct prefix p;
2045
2046 /* Router-id information is needed. */
2047 vrf_bitmap_set(client->ridinfo, zvrf_id(zvrf));
2048
2049 router_id_get(&p, zvrf_id(zvrf));
2050
2051 zsend_router_id_update(client, &p, zvrf_id(zvrf));
2052 }
2053
2054 /* Unregister zebra server router-id information. */
2055 static void zread_router_id_delete(ZAPI_HANDLER_ARGS)
2056 {
2057 vrf_bitmap_unset(client->ridinfo, zvrf_id(zvrf));
2058 }
2059
2060 /* Tie up route-type and client->sock */
2061 static void zread_hello(ZAPI_HANDLER_ARGS)
2062 {
2063 /* type of protocol (lib/zebra.h) */
2064 uint8_t proto;
2065 unsigned short instance;
2066 uint8_t notify;
2067
2068 STREAM_GETC(msg, proto);
2069 STREAM_GETW(msg, instance);
2070 STREAM_GETC(msg, notify);
2071 if (notify)
2072 client->notify_owner = true;
2073
2074 /* accept only dynamic routing protocols */
2075 if ((proto < ZEBRA_ROUTE_MAX) && (proto > ZEBRA_ROUTE_STATIC)) {
2076 zlog_notice(
2077 "client %d says hello and bids fair to announce only %s routes vrf=%u",
2078 client->sock, zebra_route_string(proto),
2079 zvrf->vrf->vrf_id);
2080 if (instance)
2081 zlog_notice("client protocol instance %d", instance);
2082
2083 client->proto = proto;
2084 client->instance = instance;
2085 }
2086
2087 stream_failure:
2088 return;
2089 }
2090
2091 /* Unregister all information in a VRF. */
2092 static void zread_vrf_unregister(ZAPI_HANDLER_ARGS)
2093 {
2094 int i;
2095 afi_t afi;
2096
2097 for (afi = AFI_IP; afi < AFI_MAX; afi++)
2098 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
2099 vrf_bitmap_unset(client->redist[afi][i], zvrf_id(zvrf));
2100 vrf_bitmap_unset(client->redist_default, zvrf_id(zvrf));
2101 vrf_bitmap_unset(client->ifinfo, zvrf_id(zvrf));
2102 vrf_bitmap_unset(client->ridinfo, zvrf_id(zvrf));
2103 }
2104
2105 static void zread_mpls_labels(ZAPI_HANDLER_ARGS)
2106 {
2107 struct stream *s;
2108 enum lsp_types_t type;
2109 struct prefix prefix;
2110 enum nexthop_types_t gtype;
2111 union g_addr gate;
2112 ifindex_t ifindex;
2113 mpls_label_t in_label, out_label;
2114 uint8_t distance;
2115
2116 /* Get input stream. */
2117 s = msg;
2118
2119 /* Get data. */
2120 STREAM_GETC(s, type);
2121 STREAM_GETL(s, prefix.family);
2122 switch (prefix.family) {
2123 case AF_INET:
2124 STREAM_GET(&prefix.u.prefix4.s_addr, s, IPV4_MAX_BYTELEN);
2125 STREAM_GETC(s, prefix.prefixlen);
2126 if (prefix.prefixlen > IPV4_MAX_BITLEN) {
2127 zlog_warn(
2128 "%s: Specified prefix length %d is greater than a v4 address can support",
2129 __PRETTY_FUNCTION__, prefix.prefixlen);
2130 return;
2131 }
2132 STREAM_GET(&gate.ipv4.s_addr, s, IPV4_MAX_BYTELEN);
2133 break;
2134 case AF_INET6:
2135 STREAM_GET(&prefix.u.prefix6, s, 16);
2136 STREAM_GETC(s, prefix.prefixlen);
2137 if (prefix.prefixlen > IPV6_MAX_BITLEN) {
2138 zlog_warn(
2139 "%s: Specified prefix length %d is greater than a v6 address can support",
2140 __PRETTY_FUNCTION__, prefix.prefixlen);
2141 return;
2142 }
2143 STREAM_GET(&gate.ipv6, s, 16);
2144 break;
2145 default:
2146 zlog_warn("%s: Specified AF %d is not supported for this call",
2147 __PRETTY_FUNCTION__, prefix.family);
2148 return;
2149 }
2150 STREAM_GETL(s, ifindex);
2151 STREAM_GETC(s, distance);
2152 STREAM_GETL(s, in_label);
2153 STREAM_GETL(s, out_label);
2154
2155 switch (prefix.family) {
2156 case AF_INET:
2157 if (ifindex)
2158 gtype = NEXTHOP_TYPE_IPV4_IFINDEX;
2159 else
2160 gtype = NEXTHOP_TYPE_IPV4;
2161 break;
2162 case AF_INET6:
2163 if (ifindex)
2164 gtype = NEXTHOP_TYPE_IPV6_IFINDEX;
2165 else
2166 gtype = NEXTHOP_TYPE_IPV6;
2167 break;
2168 default:
2169 return;
2170 }
2171
2172 if (!mpls_enabled)
2173 return;
2174
2175 if (hdr->command == ZEBRA_MPLS_LABELS_ADD) {
2176 mpls_lsp_install(zvrf, type, in_label, out_label, gtype, &gate,
2177 ifindex);
2178 mpls_ftn_update(1, zvrf, type, &prefix, gtype, &gate, ifindex,
2179 distance, out_label);
2180 } else if (hdr->command == ZEBRA_MPLS_LABELS_DELETE) {
2181 mpls_lsp_uninstall(zvrf, type, in_label, gtype, &gate, ifindex);
2182 mpls_ftn_update(0, zvrf, type, &prefix, gtype, &gate, ifindex,
2183 distance, out_label);
2184 }
2185 stream_failure:
2186 return;
2187 }
2188
2189 static void zread_label_manager_connect(struct zserv *client,
2190 struct stream *msg, vrf_id_t vrf_id)
2191 {
2192 struct stream *s;
2193 /* type of protocol (lib/zebra.h) */
2194 uint8_t proto;
2195 unsigned short instance;
2196
2197 /* Get input stream. */
2198 s = msg;
2199
2200 /* Get data. */
2201 STREAM_GETC(s, proto);
2202 STREAM_GETW(s, instance);
2203
2204 /* accept only dynamic routing protocols */
2205 if ((proto >= ZEBRA_ROUTE_MAX) || (proto <= ZEBRA_ROUTE_STATIC)) {
2206 zlog_err("client %d has wrong protocol %s", client->sock,
2207 zebra_route_string(proto));
2208 zsend_label_manager_connect_response(client, vrf_id, 1);
2209 return;
2210 }
2211 zlog_notice("client %d with vrf %u instance %u connected as %s",
2212 client->sock, vrf_id, instance, zebra_route_string(proto));
2213 client->proto = proto;
2214 client->instance = instance;
2215
2216 /*
2217 Release previous labels of same protocol and instance.
2218 This is done in case it restarted from an unexpected shutdown.
2219 */
2220 release_daemon_chunks(proto, instance);
2221
2222 zlog_debug(
2223 " Label Manager client connected: sock %d, proto %s, vrf %u instance %u",
2224 client->sock, zebra_route_string(proto), vrf_id, instance);
2225 /* send response back */
2226 zsend_label_manager_connect_response(client, vrf_id, 0);
2227
2228 stream_failure:
2229 return;
2230 }
2231
2232 static void zread_get_label_chunk(struct zserv *client, struct stream *msg,
2233 vrf_id_t vrf_id)
2234 {
2235 struct stream *s;
2236 uint8_t keep;
2237 uint32_t size;
2238 struct label_manager_chunk *lmc;
2239
2240 /* Get input stream. */
2241 s = msg;
2242
2243 /* Get data. */
2244 STREAM_GETC(s, keep);
2245 STREAM_GETL(s, size);
2246
2247 lmc = assign_label_chunk(client->proto, client->instance, keep, size);
2248 if (!lmc)
2249 zlog_err("%s: Unable to assign Label Chunk of size %u",
2250 __func__, size);
2251 else
2252 zlog_debug("Assigned Label Chunk %u - %u to %u", lmc->start,
2253 lmc->end, keep);
2254 /* send response back */
2255 zsend_assign_label_chunk_response(client, vrf_id, lmc);
2256
2257 stream_failure:
2258 return;
2259 }
2260
2261 static void zread_release_label_chunk(struct zserv *client, struct stream *msg)
2262 {
2263 struct stream *s;
2264 uint32_t start, end;
2265
2266 /* Get input stream. */
2267 s = msg;
2268
2269 /* Get data. */
2270 STREAM_GETL(s, start);
2271 STREAM_GETL(s, end);
2272
2273 release_label_chunk(client->proto, client->instance, start, end);
2274
2275 stream_failure:
2276 return;
2277 }
2278 static void zread_label_manager_request(ZAPI_HANDLER_ARGS)
2279 {
2280 /* to avoid sending other messages like ZERBA_INTERFACE_UP */
2281 if (hdr->command == ZEBRA_LABEL_MANAGER_CONNECT)
2282 client->is_synchronous = 1;
2283
2284 /* external label manager */
2285 if (lm_is_external)
2286 zread_relay_label_manager_request(hdr->command, client,
2287 zvrf_id(zvrf));
2288 /* this is a label manager */
2289 else {
2290 if (hdr->command == ZEBRA_LABEL_MANAGER_CONNECT)
2291 zread_label_manager_connect(client, msg, zvrf_id(zvrf));
2292 else {
2293 /* Sanity: don't allow 'unidentified' requests */
2294 if (!client->proto) {
2295 zlog_err(
2296 "Got label request from an unidentified client");
2297 return;
2298 }
2299 if (hdr->command == ZEBRA_GET_LABEL_CHUNK)
2300 zread_get_label_chunk(client, msg,
2301 zvrf_id(zvrf));
2302 else if (hdr->command == ZEBRA_RELEASE_LABEL_CHUNK)
2303 zread_release_label_chunk(client, msg);
2304 }
2305 }
2306 }
2307
2308 static void zread_pseudowire(ZAPI_HANDLER_ARGS)
2309 {
2310 struct stream *s;
2311 char ifname[IF_NAMESIZE];
2312 ifindex_t ifindex;
2313 int type;
2314 int af;
2315 union g_addr nexthop;
2316 uint32_t local_label;
2317 uint32_t remote_label;
2318 uint8_t flags;
2319 union pw_protocol_fields data;
2320 uint8_t protocol;
2321 struct zebra_pw *pw;
2322
2323 /* Get input stream. */
2324 s = msg;
2325
2326 /* Get data. */
2327 STREAM_GET(ifname, s, IF_NAMESIZE);
2328 STREAM_GETL(s, ifindex);
2329 STREAM_GETL(s, type);
2330 STREAM_GETL(s, af);
2331 switch (af) {
2332 case AF_INET:
2333 STREAM_GET(&nexthop.ipv4.s_addr, s, IPV4_MAX_BYTELEN);
2334 break;
2335 case AF_INET6:
2336 STREAM_GET(&nexthop.ipv6, s, 16);
2337 break;
2338 default:
2339 return;
2340 }
2341 STREAM_GETL(s, local_label);
2342 STREAM_GETL(s, remote_label);
2343 STREAM_GETC(s, flags);
2344 STREAM_GET(&data, s, sizeof(data));
2345 protocol = client->proto;
2346
2347 pw = zebra_pw_find(zvrf, ifname);
2348 switch (hdr->command) {
2349 case ZEBRA_PW_ADD:
2350 if (pw) {
2351 zlog_warn("%s: pseudowire %s already exists [%s]",
2352 __func__, ifname,
2353 zserv_command_string(hdr->command));
2354 return;
2355 }
2356
2357 zebra_pw_add(zvrf, ifname, protocol, client);
2358 break;
2359 case ZEBRA_PW_DELETE:
2360 if (!pw) {
2361 zlog_warn("%s: pseudowire %s not found [%s]", __func__,
2362 ifname, zserv_command_string(hdr->command));
2363 return;
2364 }
2365
2366 zebra_pw_del(zvrf, pw);
2367 break;
2368 case ZEBRA_PW_SET:
2369 case ZEBRA_PW_UNSET:
2370 if (!pw) {
2371 zlog_warn("%s: pseudowire %s not found [%s]", __func__,
2372 ifname, zserv_command_string(hdr->command));
2373 return;
2374 }
2375
2376 switch (hdr->command) {
2377 case ZEBRA_PW_SET:
2378 pw->enabled = 1;
2379 break;
2380 case ZEBRA_PW_UNSET:
2381 pw->enabled = 0;
2382 break;
2383 }
2384
2385 zebra_pw_change(pw, ifindex, type, af, &nexthop, local_label,
2386 remote_label, flags, &data);
2387 break;
2388 }
2389
2390 stream_failure:
2391 return;
2392 }
2393
2394 /* Cleanup registered nexthops (across VRFs) upon client disconnect. */
2395 static void zebra_client_close_cleanup_rnh(struct zserv *client)
2396 {
2397 struct vrf *vrf;
2398 struct zebra_vrf *zvrf;
2399
2400 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) {
2401 if ((zvrf = vrf->info) != NULL) {
2402 zebra_cleanup_rnh_client(zvrf_id(zvrf), AF_INET, client,
2403 RNH_NEXTHOP_TYPE);
2404 zebra_cleanup_rnh_client(zvrf_id(zvrf), AF_INET6,
2405 client, RNH_NEXTHOP_TYPE);
2406 zebra_cleanup_rnh_client(zvrf_id(zvrf), AF_INET, client,
2407 RNH_IMPORT_CHECK_TYPE);
2408 zebra_cleanup_rnh_client(zvrf_id(zvrf), AF_INET6,
2409 client, RNH_IMPORT_CHECK_TYPE);
2410 if (client->proto == ZEBRA_ROUTE_LDP) {
2411 hash_iterate(zvrf->lsp_table,
2412 mpls_ldp_lsp_uninstall_all,
2413 zvrf->lsp_table);
2414 mpls_ldp_ftn_uninstall_all(zvrf, AFI_IP);
2415 mpls_ldp_ftn_uninstall_all(zvrf, AFI_IP6);
2416 }
2417 }
2418 }
2419 }
2420
2421 static void zread_interface_set_master(ZAPI_HANDLER_ARGS)
2422 {
2423 struct interface *master;
2424 struct interface *slave;
2425 struct stream *s = msg;
2426 int ifindex;
2427 vrf_id_t vrf_id;
2428
2429 STREAM_GETL(s, vrf_id);
2430 STREAM_GETL(s, ifindex);
2431 master = if_lookup_by_index(ifindex, vrf_id);
2432
2433 STREAM_GETL(s, vrf_id);
2434 STREAM_GETL(s, ifindex);
2435 slave = if_lookup_by_index(ifindex, vrf_id);
2436
2437 if (!master || !slave)
2438 return;
2439
2440 kernel_interface_set_master(master, slave);
2441
2442 stream_failure:
2443 return;
2444 }
2445
2446
2447 static void zread_vrf_label(ZAPI_HANDLER_ARGS)
2448 {
2449 struct interface *ifp;
2450 mpls_label_t nlabel;
2451 afi_t afi;
2452 struct stream *s;
2453 struct zebra_vrf *def_zvrf;
2454 enum lsp_types_t ltype;
2455
2456 s = msg;
2457 STREAM_GETL(s, nlabel);
2458 STREAM_GETC(s, afi);
2459 if (nlabel == zvrf->label[afi]) {
2460 /*
2461 * Nothing to do here move along
2462 */
2463 return;
2464 }
2465
2466 STREAM_GETC(s, ltype);
2467
2468 if (zvrf->vrf->vrf_id != VRF_DEFAULT)
2469 ifp = if_lookup_by_name(zvrf->vrf->name, zvrf->vrf->vrf_id);
2470 else
2471 ifp = if_lookup_by_name("lo", VRF_DEFAULT);
2472
2473 if (!ifp) {
2474 zlog_debug("Unable to find specified Interface for %s",
2475 zvrf->vrf->name);
2476 return;
2477 }
2478
2479 def_zvrf = zebra_vrf_lookup_by_id(VRF_DEFAULT);
2480
2481 if (zvrf->label[afi] != MPLS_LABEL_NONE) {
2482 afi_t scrubber;
2483 bool really_remove;
2484
2485 really_remove = true;
2486 for (scrubber = AFI_IP; scrubber < AFI_MAX; scrubber++) {
2487 if (scrubber == afi)
2488 continue;
2489
2490 if (zvrf->label[scrubber] == MPLS_LABEL_NONE)
2491 continue;
2492
2493 if (zvrf->label[afi] == zvrf->label[scrubber]) {
2494 really_remove = false;
2495 break;
2496 }
2497 }
2498
2499 if (really_remove)
2500 mpls_lsp_uninstall(def_zvrf, ltype, zvrf->label[afi],
2501 NEXTHOP_TYPE_IFINDEX, NULL,
2502 ifp->ifindex);
2503 }
2504
2505 if (nlabel != MPLS_LABEL_NONE)
2506 mpls_lsp_install(def_zvrf, ltype, nlabel,
2507 MPLS_LABEL_IMPLICIT_NULL, NEXTHOP_TYPE_IFINDEX,
2508 NULL, ifp->ifindex);
2509
2510 zvrf->label[afi] = nlabel;
2511 stream_failure:
2512 return;
2513 }
2514
2515 static inline void zread_rule(ZAPI_HANDLER_ARGS)
2516 {
2517 struct zebra_pbr_rule zpr;
2518 struct stream *s;
2519 uint32_t total, i;
2520 ifindex_t ifindex;
2521
2522 s = msg;
2523 STREAM_GETL(s, total);
2524
2525 for (i = 0; i < total; i++) {
2526 memset(&zpr, 0, sizeof(zpr));
2527
2528 zpr.sock = client->sock;
2529 STREAM_GETL(s, zpr.seq);
2530 STREAM_GETL(s, zpr.priority);
2531 STREAM_GETL(s, zpr.unique);
2532 STREAM_GETC(s, zpr.filter.src_ip.family);
2533 STREAM_GETC(s, zpr.filter.src_ip.prefixlen);
2534 STREAM_GET(&zpr.filter.src_ip.u.prefix, s,
2535 prefix_blen(&zpr.filter.src_ip));
2536 STREAM_GETW(s, zpr.filter.src_port);
2537 STREAM_GETC(s, zpr.filter.dst_ip.family);
2538 STREAM_GETC(s, zpr.filter.dst_ip.prefixlen);
2539 STREAM_GET(&zpr.filter.dst_ip.u.prefix, s,
2540 prefix_blen(&zpr.filter.dst_ip));
2541 STREAM_GETW(s, zpr.filter.dst_port);
2542 STREAM_GETL(s, zpr.action.table);
2543 STREAM_GETL(s, ifindex);
2544
2545 zpr.ifp = if_lookup_by_index(ifindex, VRF_UNKNOWN);
2546 if (!zpr.ifp) {
2547 zlog_debug("FAiled to lookup ifindex: %u", ifindex);
2548 return;
2549 }
2550
2551 if (!is_default_prefix(&zpr.filter.src_ip))
2552 zpr.filter.filter_bm |= PBR_FILTER_SRC_IP;
2553
2554 if (!is_default_prefix(&zpr.filter.dst_ip))
2555 zpr.filter.filter_bm |= PBR_FILTER_DST_IP;
2556
2557 if (zpr.filter.src_port)
2558 zpr.filter.filter_bm |= PBR_FILTER_SRC_PORT;
2559
2560 if (zpr.filter.dst_port)
2561 zpr.filter.filter_bm |= PBR_FILTER_DST_PORT;
2562
2563 if (hdr->command == ZEBRA_RULE_ADD)
2564 zebra_pbr_add_rule(zvrf->zns, &zpr);
2565 else
2566 zebra_pbr_del_rule(zvrf->zns, &zpr);
2567 }
2568
2569 stream_failure:
2570 return;
2571 }
2572
2573 void (*zserv_handlers[])(ZAPI_HANDLER_ARGS) = {
2574 [ZEBRA_ROUTER_ID_ADD] = zread_router_id_add,
2575 [ZEBRA_ROUTER_ID_DELETE] = zread_router_id_delete,
2576 [ZEBRA_INTERFACE_ADD] = zread_interface_add,
2577 [ZEBRA_INTERFACE_DELETE] = zread_interface_delete,
2578 [ZEBRA_ROUTE_ADD] = zread_route_add,
2579 [ZEBRA_ROUTE_DELETE] = zread_route_del,
2580 [ZEBRA_IPV4_ROUTE_ADD] = zread_ipv4_add,
2581 [ZEBRA_IPV4_ROUTE_DELETE] = zread_ipv4_delete,
2582 [ZEBRA_IPV4_ROUTE_IPV6_NEXTHOP_ADD] = zread_ipv4_route_ipv6_nexthop_add,
2583 [ZEBRA_IPV6_ROUTE_ADD] = zread_ipv6_add,
2584 [ZEBRA_IPV6_ROUTE_DELETE] = zread_ipv6_delete,
2585 [ZEBRA_REDISTRIBUTE_ADD] = zebra_redistribute_add,
2586 [ZEBRA_REDISTRIBUTE_DELETE] = zebra_redistribute_delete,
2587 [ZEBRA_REDISTRIBUTE_DEFAULT_ADD] = zebra_redistribute_default_add,
2588 [ZEBRA_REDISTRIBUTE_DEFAULT_DELETE] = zebra_redistribute_default_delete,
2589 [ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB] = zread_ipv4_nexthop_lookup_mrib,
2590 [ZEBRA_HELLO] = zread_hello,
2591 [ZEBRA_NEXTHOP_REGISTER] = zread_rnh_register,
2592 [ZEBRA_NEXTHOP_UNREGISTER] = zread_rnh_unregister,
2593 [ZEBRA_IMPORT_ROUTE_REGISTER] = zread_rnh_register,
2594 [ZEBRA_IMPORT_ROUTE_UNREGISTER] = zread_rnh_unregister,
2595 [ZEBRA_BFD_DEST_UPDATE] = zebra_ptm_bfd_dst_register,
2596 [ZEBRA_BFD_DEST_REGISTER] = zebra_ptm_bfd_dst_register,
2597 [ZEBRA_BFD_DEST_DEREGISTER] = zebra_ptm_bfd_dst_deregister,
2598 [ZEBRA_VRF_UNREGISTER] = zread_vrf_unregister,
2599 [ZEBRA_VRF_LABEL] = zread_vrf_label,
2600 [ZEBRA_BFD_CLIENT_REGISTER] = zebra_ptm_bfd_client_register,
2601 #if defined(HAVE_RTADV)
2602 [ZEBRA_INTERFACE_ENABLE_RADV] = zebra_interface_radv_enable,
2603 [ZEBRA_INTERFACE_DISABLE_RADV] = zebra_interface_radv_disable,
2604 #else
2605 [ZEBRA_INTERFACE_ENABLE_RADV] = NULL,
2606 [ZEBRA_INTERFACE_DISABLE_RADV] = NULL,
2607 #endif
2608 [ZEBRA_MPLS_LABELS_ADD] = zread_mpls_labels,
2609 [ZEBRA_MPLS_LABELS_DELETE] = zread_mpls_labels,
2610 [ZEBRA_IPMR_ROUTE_STATS] = zebra_ipmr_route_stats,
2611 [ZEBRA_LABEL_MANAGER_CONNECT] = zread_label_manager_request,
2612 [ZEBRA_GET_LABEL_CHUNK] = zread_label_manager_request,
2613 [ZEBRA_RELEASE_LABEL_CHUNK] = zread_label_manager_request,
2614 [ZEBRA_FEC_REGISTER] = zread_fec_register,
2615 [ZEBRA_FEC_UNREGISTER] = zread_fec_unregister,
2616 [ZEBRA_ADVERTISE_DEFAULT_GW] = zebra_vxlan_advertise_gw_macip,
2617 [ZEBRA_ADVERTISE_SUBNET] = zebra_vxlan_advertise_subnet,
2618 [ZEBRA_ADVERTISE_ALL_VNI] = zebra_vxlan_advertise_all_vni,
2619 [ZEBRA_REMOTE_VTEP_ADD] = zebra_vxlan_remote_vtep_add,
2620 [ZEBRA_REMOTE_VTEP_DEL] = zebra_vxlan_remote_vtep_del,
2621 [ZEBRA_REMOTE_MACIP_ADD] = zebra_vxlan_remote_macip_add,
2622 [ZEBRA_REMOTE_MACIP_DEL] = zebra_vxlan_remote_macip_del,
2623 [ZEBRA_INTERFACE_SET_MASTER] = zread_interface_set_master,
2624 [ZEBRA_PW_ADD] = zread_pseudowire,
2625 [ZEBRA_PW_DELETE] = zread_pseudowire,
2626 [ZEBRA_PW_SET] = zread_pseudowire,
2627 [ZEBRA_PW_UNSET] = zread_pseudowire,
2628 [ZEBRA_RULE_ADD] = zread_rule,
2629 [ZEBRA_RULE_DELETE] = zread_rule,
2630 };
2631
2632 static inline void zserv_handle_commands(struct zserv *client,
2633 struct zmsghdr *hdr,
2634 struct stream *msg,
2635 struct zebra_vrf *zvrf)
2636 {
2637 if (hdr->command > array_size(zserv_handlers)
2638 || zserv_handlers[hdr->command] == NULL)
2639 zlog_info("Zebra received unknown command %d", hdr->command);
2640 else
2641 zserv_handlers[hdr->command](client, hdr, msg, zvrf);
2642
2643 stream_free(msg);
2644 }
2645
2646 /* Lifecycle ---------------------------------------------------------------- */
2647
2648 /* free zebra client information. */
2649 static void zebra_client_free(struct zserv *client)
2650 {
2651 /* Send client de-registration to BFD */
2652 zebra_ptm_bfd_client_deregister(client->proto);
2653
2654 /* Cleanup any rules installed from this client */
2655 zebra_pbr_client_close_cleanup(client->sock);
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(uint8_t proto, unsigned 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 }