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