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