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