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