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