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