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