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