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