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