]> git.proxmox.com Git - mirror_frr.git/blob - zebra/zserv.c
Merge branch 'master' into PIM_VRF
[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 /* Register zebra server interface information. Send current all
1089 interface and address information. */
1090 static int zread_interface_add(struct zserv *client, u_short length,
1091 struct zebra_vrf *zvrf)
1092 {
1093 struct vrf *vrf;
1094 struct listnode *ifnode, *ifnnode;
1095 struct interface *ifp;
1096
1097 /* Interface information is needed. */
1098 vrf_bitmap_set(client->ifinfo, zvrf_id(zvrf));
1099
1100 RB_FOREACH(vrf, vrf_id_head, &vrfs_by_id)
1101 {
1102 for (ALL_LIST_ELEMENTS(vrf->iflist, ifnode, ifnnode, ifp)) {
1103 /* Skip pseudo interface. */
1104 if (!CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE))
1105 continue;
1106
1107 if (zsend_interface_add(client, ifp) < 0)
1108 return -1;
1109
1110 if (zsend_interface_addresses(client, ifp) < 0)
1111 return -1;
1112 }
1113 }
1114 return 0;
1115 }
1116
1117 /* Unregister zebra server interface information. */
1118 static int zread_interface_delete(struct zserv *client, u_short length,
1119 struct zebra_vrf *zvrf)
1120 {
1121 vrf_bitmap_unset(client->ifinfo, zvrf_id(zvrf));
1122 return 0;
1123 }
1124
1125 void zserv_nexthop_num_warn(const char *caller, const struct prefix *p,
1126 const unsigned int nexthop_num)
1127 {
1128 if (nexthop_num > multipath_num) {
1129 char buff[PREFIX2STR_BUFFER];
1130 prefix2str(p, buff, sizeof(buff));
1131 zlog_warn(
1132 "%s: Prefix %s has %d nexthops, but we can only use the first %d",
1133 caller, buff, nexthop_num, multipath_num);
1134 }
1135 }
1136
1137 /* This function support multiple nexthop. */
1138 /*
1139 * Parse the ZEBRA_IPV4_ROUTE_ADD sent from client. Update re and
1140 * add kernel route.
1141 */
1142 static int zread_ipv4_add(struct zserv *client, u_short length,
1143 struct zebra_vrf *zvrf)
1144 {
1145 int i;
1146 struct route_entry *re;
1147 struct prefix p;
1148 u_char message;
1149 struct in_addr nhop_addr;
1150 u_char nexthop_num;
1151 u_char nexthop_type;
1152 struct stream *s;
1153 ifindex_t ifindex;
1154 safi_t safi;
1155 int ret;
1156 mpls_label_t label;
1157 struct nexthop *nexthop;
1158
1159 /* Get input stream. */
1160 s = client->ibuf;
1161
1162 /* Allocate new re. */
1163 re = XCALLOC(MTYPE_RE, sizeof(struct route_entry));
1164
1165 /* Type, flags, message. */
1166 re->type = stream_getc(s);
1167 re->instance = stream_getw(s);
1168 re->flags = stream_getl(s);
1169 message = stream_getc(s);
1170 safi = stream_getw(s);
1171 re->uptime = time(NULL);
1172
1173 /* IPv4 prefix. */
1174 memset(&p, 0, sizeof(struct prefix_ipv4));
1175 p.family = AF_INET;
1176 p.prefixlen = stream_getc(s);
1177 stream_get(&p.u.prefix4, s, PSIZE(p.prefixlen));
1178
1179 /* VRF ID */
1180 re->vrf_id = zvrf_id(zvrf);
1181
1182 /* Nexthop parse. */
1183 if (CHECK_FLAG(message, ZAPI_MESSAGE_NEXTHOP)) {
1184 nexthop_num = stream_getc(s);
1185 zserv_nexthop_num_warn(__func__, (const struct prefix *)&p,
1186 nexthop_num);
1187
1188 for (i = 0; i < nexthop_num; i++) {
1189 nexthop_type = stream_getc(s);
1190
1191 switch (nexthop_type) {
1192 case NEXTHOP_TYPE_IFINDEX:
1193 ifindex = stream_getl(s);
1194 route_entry_nexthop_ifindex_add(re, ifindex);
1195 break;
1196 case NEXTHOP_TYPE_IPV4:
1197 nhop_addr.s_addr = stream_get_ipv4(s);
1198 nexthop = route_entry_nexthop_ipv4_add(
1199 re, &nhop_addr, NULL);
1200 /* For labeled-unicast, each nexthop is followed
1201 * by label. */
1202 if (CHECK_FLAG(message, ZAPI_MESSAGE_LABEL)) {
1203 label = (mpls_label_t)stream_getl(s);
1204 nexthop_add_labels(
1205 nexthop, nexthop->nh_label_type,
1206 1, &label);
1207 }
1208 break;
1209 case NEXTHOP_TYPE_IPV4_IFINDEX:
1210 nhop_addr.s_addr = stream_get_ipv4(s);
1211 ifindex = stream_getl(s);
1212 route_entry_nexthop_ipv4_ifindex_add(
1213 re, &nhop_addr, NULL, ifindex);
1214 break;
1215 case NEXTHOP_TYPE_IPV6:
1216 stream_forward_getp(s, IPV6_MAX_BYTELEN);
1217 break;
1218 case NEXTHOP_TYPE_BLACKHOLE:
1219 route_entry_nexthop_blackhole_add(re);
1220 break;
1221 }
1222 }
1223 }
1224
1225 /* Distance. */
1226 if (CHECK_FLAG(message, ZAPI_MESSAGE_DISTANCE))
1227 re->distance = stream_getc(s);
1228
1229 /* Metric. */
1230 if (CHECK_FLAG(message, ZAPI_MESSAGE_METRIC))
1231 re->metric = stream_getl(s);
1232
1233 /* Tag */
1234 if (CHECK_FLAG(message, ZAPI_MESSAGE_TAG))
1235 re->tag = stream_getl(s);
1236 else
1237 re->tag = 0;
1238
1239 if (CHECK_FLAG(message, ZAPI_MESSAGE_MTU))
1240 re->mtu = stream_getl(s);
1241 else
1242 re->mtu = 0;
1243
1244 /* Table */
1245 re->table = zvrf->table_id;
1246
1247 ret = rib_add_multipath(AFI_IP, safi, &p, NULL, re);
1248
1249 /* Stats */
1250 if (ret > 0)
1251 client->v4_route_add_cnt++;
1252 else if (ret < 0)
1253 client->v4_route_upd8_cnt++;
1254 return 0;
1255 }
1256
1257 /* Zebra server IPv4 prefix delete function. */
1258 static int zread_ipv4_delete(struct zserv *client, u_short length,
1259 struct zebra_vrf *zvrf)
1260 {
1261 int i;
1262 struct stream *s;
1263 struct zapi_ipv4 api;
1264 struct in_addr nexthop;
1265 union g_addr *nexthop_p;
1266 unsigned long ifindex;
1267 struct prefix p;
1268 u_char nexthop_num;
1269 u_char nexthop_type;
1270 u_int32_t table_id;
1271
1272 s = client->ibuf;
1273 ifindex = 0;
1274 nexthop.s_addr = 0;
1275 nexthop_p = NULL;
1276
1277 /* Type, flags, message. */
1278 api.type = stream_getc(s);
1279 api.instance = stream_getw(s);
1280 api.flags = stream_getl(s);
1281 api.message = stream_getc(s);
1282 api.safi = stream_getw(s);
1283
1284 /* IPv4 prefix. */
1285 memset(&p, 0, sizeof(struct prefix));
1286 p.family = AF_INET;
1287 p.prefixlen = stream_getc(s);
1288 stream_get(&p.u.prefix4, s, PSIZE(p.prefixlen));
1289
1290 /* Nexthop, ifindex, distance, metric. */
1291 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP)) {
1292 nexthop_num = stream_getc(s);
1293
1294 for (i = 0; i < nexthop_num; i++) {
1295 nexthop_type = stream_getc(s);
1296
1297 switch (nexthop_type) {
1298 case NEXTHOP_TYPE_IFINDEX:
1299 ifindex = stream_getl(s);
1300 break;
1301 case NEXTHOP_TYPE_IPV4:
1302 nexthop.s_addr = stream_get_ipv4(s);
1303 /* For labeled-unicast, each nexthop is followed
1304 * by label, but
1305 * we don't care for delete.
1306 */
1307 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_LABEL))
1308 stream_forward_getp(s,
1309 sizeof(u_int32_t));
1310 nexthop_p = (union g_addr *)&nexthop;
1311 break;
1312 case NEXTHOP_TYPE_IPV4_IFINDEX:
1313 nexthop.s_addr = stream_get_ipv4(s);
1314 nexthop_p = (union g_addr *)&nexthop;
1315 ifindex = stream_getl(s);
1316 break;
1317 case NEXTHOP_TYPE_IPV6:
1318 stream_forward_getp(s, IPV6_MAX_BYTELEN);
1319 break;
1320 }
1321 }
1322 }
1323
1324 /* Distance. */
1325 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_DISTANCE))
1326 api.distance = stream_getc(s);
1327 else
1328 api.distance = 0;
1329
1330 /* Metric. */
1331 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_METRIC))
1332 api.metric = stream_getl(s);
1333 else
1334 api.metric = 0;
1335
1336 /* tag */
1337 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_TAG))
1338 api.tag = stream_getl(s);
1339 else
1340 api.tag = 0;
1341
1342 table_id = zvrf->table_id;
1343
1344 rib_delete(AFI_IP, api.safi, zvrf_id(zvrf), api.type, api.instance,
1345 api.flags, &p, NULL, nexthop_p, ifindex, table_id);
1346 client->v4_route_del_cnt++;
1347 return 0;
1348 }
1349
1350 /* MRIB Nexthop lookup for IPv4. */
1351 static int zread_ipv4_nexthop_lookup_mrib(struct zserv *client, u_short length,
1352 struct zebra_vrf *zvrf)
1353 {
1354 struct in_addr addr;
1355 struct route_entry *re;
1356
1357 addr.s_addr = stream_get_ipv4(client->ibuf);
1358 re = rib_match_ipv4_multicast(zvrf_id(zvrf), addr, NULL);
1359 return zsend_ipv4_nexthop_lookup_mrib(client, addr, re, zvrf);
1360 }
1361
1362 /* Zebra server IPv6 prefix add function. */
1363 static int zread_ipv4_route_ipv6_nexthop_add(struct zserv *client,
1364 u_short length,
1365 struct zebra_vrf *zvrf)
1366 {
1367 unsigned int i;
1368 struct stream *s;
1369 struct in6_addr nhop_addr;
1370 struct route_entry *re;
1371 u_char message;
1372 u_char nexthop_num;
1373 u_char nexthop_type;
1374 struct prefix p;
1375 safi_t safi;
1376 static struct in6_addr nexthops[MULTIPATH_NUM];
1377 static unsigned int ifindices[MULTIPATH_NUM];
1378 int ret;
1379 static mpls_label_t labels[MULTIPATH_NUM];
1380 mpls_label_t label;
1381 struct nexthop *nexthop;
1382
1383 /* Get input stream. */
1384 s = client->ibuf;
1385
1386 memset(&nhop_addr, 0, sizeof(struct in6_addr));
1387
1388 /* Allocate new re. */
1389 re = XCALLOC(MTYPE_RE, sizeof(struct route_entry));
1390
1391 /* Type, flags, message. */
1392 re->type = stream_getc(s);
1393 re->instance = stream_getw(s);
1394 re->flags = stream_getl(s);
1395 message = stream_getc(s);
1396 safi = stream_getw(s);
1397 re->uptime = time(NULL);
1398
1399 /* IPv4 prefix. */
1400 memset(&p, 0, sizeof(struct prefix_ipv4));
1401 p.family = AF_INET;
1402 p.prefixlen = stream_getc(s);
1403 stream_get(&p.u.prefix4, s, PSIZE(p.prefixlen));
1404
1405 /* VRF ID */
1406 re->vrf_id = zvrf_id(zvrf);
1407
1408 /* We need to give nh-addr, nh-ifindex with the same next-hop object
1409 * to the re to ensure that IPv6 multipathing works; need to coalesce
1410 * these. Clients should send the same number of paired set of
1411 * next-hop-addr/next-hop-ifindices. */
1412 if (CHECK_FLAG(message, ZAPI_MESSAGE_NEXTHOP)) {
1413 unsigned int nh_count = 0;
1414 unsigned int if_count = 0;
1415 unsigned int max_nh_if = 0;
1416
1417 nexthop_num = stream_getc(s);
1418 zserv_nexthop_num_warn(__func__, (const struct prefix *)&p,
1419 nexthop_num);
1420 for (i = 0; i < nexthop_num; i++) {
1421 nexthop_type = stream_getc(s);
1422
1423 switch (nexthop_type) {
1424 case NEXTHOP_TYPE_IPV6:
1425 stream_get(&nhop_addr, s, 16);
1426 if (nh_count < MULTIPATH_NUM) {
1427 /* For labeled-unicast, each nexthop is
1428 * followed by label. */
1429 if (CHECK_FLAG(message,
1430 ZAPI_MESSAGE_LABEL)) {
1431 label = (mpls_label_t)
1432 stream_getl(s);
1433 labels[nh_count] = label;
1434 }
1435 nexthops[nh_count] = nhop_addr;
1436 nh_count++;
1437 }
1438 break;
1439 case NEXTHOP_TYPE_IFINDEX:
1440 if (if_count < multipath_num) {
1441 ifindices[if_count++] = stream_getl(s);
1442 }
1443 break;
1444 case NEXTHOP_TYPE_BLACKHOLE:
1445 route_entry_nexthop_blackhole_add(re);
1446 break;
1447 }
1448 }
1449
1450 max_nh_if = (nh_count > if_count) ? nh_count : if_count;
1451 for (i = 0; i < max_nh_if; i++) {
1452 if ((i < nh_count)
1453 && !IN6_IS_ADDR_UNSPECIFIED(&nexthops[i])) {
1454 if ((i < if_count) && ifindices[i])
1455 nexthop =
1456 route_entry_nexthop_ipv6_ifindex_add(
1457 re, &nexthops[i],
1458 ifindices[i]);
1459 else
1460 nexthop = route_entry_nexthop_ipv6_add(
1461 re, &nexthops[i]);
1462
1463 if (CHECK_FLAG(message, ZAPI_MESSAGE_LABEL))
1464 nexthop_add_labels(
1465 nexthop, nexthop->nh_label_type,
1466 1, &labels[i]);
1467 } else {
1468 if ((i < if_count) && ifindices[i])
1469 route_entry_nexthop_ifindex_add(
1470 re, ifindices[i]);
1471 }
1472 }
1473 }
1474
1475 /* Distance. */
1476 if (CHECK_FLAG(message, ZAPI_MESSAGE_DISTANCE))
1477 re->distance = stream_getc(s);
1478
1479 /* Metric. */
1480 if (CHECK_FLAG(message, ZAPI_MESSAGE_METRIC))
1481 re->metric = stream_getl(s);
1482
1483 /* Tag */
1484 if (CHECK_FLAG(message, ZAPI_MESSAGE_TAG))
1485 re->tag = stream_getl(s);
1486 else
1487 re->tag = 0;
1488
1489 if (CHECK_FLAG(message, ZAPI_MESSAGE_MTU))
1490 re->mtu = stream_getl(s);
1491 else
1492 re->mtu = 0;
1493
1494 /* Table */
1495 re->table = zvrf->table_id;
1496
1497 ret = rib_add_multipath(AFI_IP6, safi, &p, NULL, re);
1498 /* Stats */
1499 if (ret > 0)
1500 client->v4_route_add_cnt++;
1501 else if (ret < 0)
1502 client->v4_route_upd8_cnt++;
1503
1504 return 0;
1505 }
1506
1507 static int zread_ipv6_add(struct zserv *client, u_short length,
1508 struct zebra_vrf *zvrf)
1509 {
1510 unsigned int i;
1511 struct stream *s;
1512 struct in6_addr nhop_addr;
1513 struct route_entry *re;
1514 u_char message;
1515 u_char nexthop_num;
1516 u_char nexthop_type;
1517 struct prefix p;
1518 struct prefix_ipv6 src_p, *src_pp;
1519 safi_t safi;
1520 static struct in6_addr nexthops[MULTIPATH_NUM];
1521 static unsigned int ifindices[MULTIPATH_NUM];
1522 int ret;
1523 static mpls_label_t labels[MULTIPATH_NUM];
1524 mpls_label_t label;
1525 struct nexthop *nexthop;
1526
1527 /* Get input stream. */
1528 s = client->ibuf;
1529
1530 memset(&nhop_addr, 0, sizeof(struct in6_addr));
1531
1532 /* Allocate new re. */
1533 re = XCALLOC(MTYPE_RE, sizeof(struct route_entry));
1534
1535 /* Type, flags, message. */
1536 re->type = stream_getc(s);
1537 re->instance = stream_getw(s);
1538 re->flags = stream_getl(s);
1539 message = stream_getc(s);
1540 safi = stream_getw(s);
1541 re->uptime = time(NULL);
1542
1543 /* IPv6 prefix. */
1544 memset(&p, 0, sizeof(struct prefix_ipv6));
1545 p.family = AF_INET6;
1546 p.prefixlen = stream_getc(s);
1547 stream_get(&p.u.prefix6, s, PSIZE(p.prefixlen));
1548
1549 if (CHECK_FLAG(message, ZAPI_MESSAGE_SRCPFX)) {
1550 memset(&src_p, 0, sizeof(struct prefix_ipv6));
1551 src_p.family = AF_INET6;
1552 src_p.prefixlen = stream_getc(s);
1553 stream_get(&src_p.prefix, s, PSIZE(src_p.prefixlen));
1554 src_pp = &src_p;
1555 } else
1556 src_pp = NULL;
1557
1558 /* We need to give nh-addr, nh-ifindex with the same next-hop object
1559 * to the re to ensure that IPv6 multipathing works; need to coalesce
1560 * these. Clients should send the same number of paired set of
1561 * next-hop-addr/next-hop-ifindices. */
1562 if (CHECK_FLAG(message, ZAPI_MESSAGE_NEXTHOP)) {
1563 unsigned int nh_count = 0;
1564 unsigned int if_count = 0;
1565 unsigned int max_nh_if = 0;
1566
1567 nexthop_num = stream_getc(s);
1568 zserv_nexthop_num_warn(__func__, (const struct prefix *)&p,
1569 nexthop_num);
1570 for (i = 0; i < nexthop_num; i++) {
1571 nexthop_type = stream_getc(s);
1572
1573 switch (nexthop_type) {
1574 case NEXTHOP_TYPE_IPV6:
1575 stream_get(&nhop_addr, s, 16);
1576 if (nh_count < MULTIPATH_NUM) {
1577 /* For labeled-unicast, each nexthop is
1578 * followed by label. */
1579 if (CHECK_FLAG(message,
1580 ZAPI_MESSAGE_LABEL)) {
1581 label = (mpls_label_t)
1582 stream_getl(s);
1583 labels[nh_count] = label;
1584 }
1585 nexthops[nh_count++] = nhop_addr;
1586 }
1587 break;
1588 case NEXTHOP_TYPE_IFINDEX:
1589 if (if_count < multipath_num) {
1590 ifindices[if_count++] = stream_getl(s);
1591 }
1592 break;
1593 case NEXTHOP_TYPE_BLACKHOLE:
1594 route_entry_nexthop_blackhole_add(re);
1595 break;
1596 }
1597 }
1598
1599 max_nh_if = (nh_count > if_count) ? nh_count : if_count;
1600 for (i = 0; i < max_nh_if; i++) {
1601 if ((i < nh_count)
1602 && !IN6_IS_ADDR_UNSPECIFIED(&nexthops[i])) {
1603 if ((i < if_count) && ifindices[i])
1604 nexthop =
1605 route_entry_nexthop_ipv6_ifindex_add(
1606 re, &nexthops[i],
1607 ifindices[i]);
1608 else
1609 nexthop = route_entry_nexthop_ipv6_add(
1610 re, &nexthops[i]);
1611 if (CHECK_FLAG(message, ZAPI_MESSAGE_LABEL))
1612 nexthop_add_labels(
1613 nexthop, nexthop->nh_label_type,
1614 1, &labels[i]);
1615 } else {
1616 if ((i < if_count) && ifindices[i])
1617 route_entry_nexthop_ifindex_add(
1618 re, ifindices[i]);
1619 }
1620 }
1621 }
1622
1623 /* Distance. */
1624 if (CHECK_FLAG(message, ZAPI_MESSAGE_DISTANCE))
1625 re->distance = stream_getc(s);
1626
1627 /* Metric. */
1628 if (CHECK_FLAG(message, ZAPI_MESSAGE_METRIC))
1629 re->metric = stream_getl(s);
1630
1631 /* Tag */
1632 if (CHECK_FLAG(message, ZAPI_MESSAGE_TAG))
1633 re->tag = stream_getl(s);
1634 else
1635 re->tag = 0;
1636
1637 if (CHECK_FLAG(message, ZAPI_MESSAGE_MTU))
1638 re->mtu = stream_getl(s);
1639 else
1640 re->mtu = 0;
1641
1642 /* VRF ID */
1643 re->vrf_id = zvrf_id(zvrf);
1644 re->table = zvrf->table_id;
1645
1646 ret = rib_add_multipath(AFI_IP6, safi, &p, src_pp, re);
1647 /* Stats */
1648 if (ret > 0)
1649 client->v6_route_add_cnt++;
1650 else if (ret < 0)
1651 client->v6_route_upd8_cnt++;
1652
1653 return 0;
1654 }
1655
1656 /* Zebra server IPv6 prefix delete function. */
1657 static int zread_ipv6_delete(struct zserv *client, u_short length,
1658 struct zebra_vrf *zvrf)
1659 {
1660 int i;
1661 struct stream *s;
1662 struct zapi_ipv6 api;
1663 struct in6_addr nexthop;
1664 union g_addr *pnexthop = NULL;
1665 unsigned long ifindex;
1666 struct prefix p;
1667 struct prefix_ipv6 src_p, *src_pp;
1668
1669 s = client->ibuf;
1670 ifindex = 0;
1671 memset(&nexthop, 0, sizeof(struct in6_addr));
1672
1673 /* Type, flags, message. */
1674 api.type = stream_getc(s);
1675 api.instance = stream_getw(s);
1676 api.flags = stream_getl(s);
1677 api.message = stream_getc(s);
1678 api.safi = stream_getw(s);
1679
1680 /* IPv4 prefix. */
1681 memset(&p, 0, sizeof(struct prefix_ipv6));
1682 p.family = AF_INET6;
1683 p.prefixlen = stream_getc(s);
1684 stream_get(&p.u.prefix6, s, PSIZE(p.prefixlen));
1685
1686 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_SRCPFX)) {
1687 memset(&src_p, 0, sizeof(struct prefix_ipv6));
1688 src_p.family = AF_INET6;
1689 src_p.prefixlen = stream_getc(s);
1690 stream_get(&src_p.prefix, s, PSIZE(src_p.prefixlen));
1691 src_pp = &src_p;
1692 } else
1693 src_pp = NULL;
1694
1695 /* Nexthop, ifindex, distance, metric. */
1696 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP)) {
1697 u_char nexthop_type;
1698
1699 api.nexthop_num = stream_getc(s);
1700 for (i = 0; i < api.nexthop_num; i++) {
1701 nexthop_type = stream_getc(s);
1702
1703 switch (nexthop_type) {
1704 case NEXTHOP_TYPE_IPV6:
1705 stream_get(&nexthop, s, 16);
1706 /* For labeled-unicast, each nexthop is followed
1707 * by label, but
1708 * we don't care for delete.
1709 */
1710 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_LABEL))
1711 stream_forward_getp(s,
1712 sizeof(u_int32_t));
1713 pnexthop = (union g_addr *)&nexthop;
1714 break;
1715 case NEXTHOP_TYPE_IFINDEX:
1716 ifindex = stream_getl(s);
1717 break;
1718 }
1719 }
1720 }
1721
1722 /* Distance. */
1723 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_DISTANCE))
1724 api.distance = stream_getc(s);
1725 else
1726 api.distance = 0;
1727
1728 /* Metric. */
1729 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_METRIC))
1730 api.metric = stream_getl(s);
1731 else
1732 api.metric = 0;
1733
1734 /* tag */
1735 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_TAG))
1736 api.tag = stream_getl(s);
1737 else
1738 api.tag = 0;
1739
1740 if (IN6_IS_ADDR_UNSPECIFIED(&nexthop))
1741 rib_delete(AFI_IP6, api.safi, zvrf_id(zvrf), api.type,
1742 api.instance, api.flags, &p, src_pp, NULL, ifindex,
1743 client->rtm_table);
1744 else
1745 rib_delete(AFI_IP6, api.safi, zvrf_id(zvrf), api.type,
1746 api.instance, api.flags, &p, src_pp, pnexthop,
1747 ifindex, client->rtm_table);
1748
1749 client->v6_route_del_cnt++;
1750 return 0;
1751 }
1752
1753 /* Register zebra server router-id information. Send current router-id */
1754 static int zread_router_id_add(struct zserv *client, u_short length,
1755 struct zebra_vrf *zvrf)
1756 {
1757 struct prefix p;
1758
1759 /* Router-id information is needed. */
1760 vrf_bitmap_set(client->ridinfo, zvrf_id(zvrf));
1761
1762 router_id_get(&p, zvrf_id(zvrf));
1763
1764 return zsend_router_id_update(client, &p, zvrf_id(zvrf));
1765 }
1766
1767 /* Unregister zebra server router-id information. */
1768 static int zread_router_id_delete(struct zserv *client, u_short length,
1769 struct zebra_vrf *zvrf)
1770 {
1771 vrf_bitmap_unset(client->ridinfo, zvrf_id(zvrf));
1772 return 0;
1773 }
1774
1775 /* Tie up route-type and client->sock */
1776 static void zread_hello(struct zserv *client)
1777 {
1778 /* type of protocol (lib/zebra.h) */
1779 u_char proto;
1780 u_short instance;
1781
1782 proto = stream_getc(client->ibuf);
1783 instance = stream_getw(client->ibuf);
1784
1785 /* accept only dynamic routing protocols */
1786 if ((proto < ZEBRA_ROUTE_MAX) && (proto > ZEBRA_ROUTE_STATIC)) {
1787 zlog_notice(
1788 "client %d says hello and bids fair to announce only %s routes",
1789 client->sock, zebra_route_string(proto));
1790 if (instance)
1791 zlog_notice("client protocol instance %d", instance);
1792
1793 client->proto = proto;
1794 client->instance = instance;
1795 }
1796 }
1797
1798 /* Unregister all information in a VRF. */
1799 static int zread_vrf_unregister(struct zserv *client, u_short length,
1800 struct zebra_vrf *zvrf)
1801 {
1802 int i;
1803 afi_t afi;
1804
1805 for (afi = AFI_IP; afi < AFI_MAX; afi++)
1806 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
1807 vrf_bitmap_unset(client->redist[afi][i], zvrf_id(zvrf));
1808 vrf_bitmap_unset(client->redist_default, zvrf_id(zvrf));
1809 vrf_bitmap_unset(client->ifinfo, zvrf_id(zvrf));
1810 vrf_bitmap_unset(client->ridinfo, zvrf_id(zvrf));
1811
1812 return 0;
1813 }
1814
1815 static void zread_mpls_labels(int command, struct zserv *client, u_short length,
1816 vrf_id_t vrf_id)
1817 {
1818 struct stream *s;
1819 enum lsp_types_t type;
1820 struct prefix prefix;
1821 enum nexthop_types_t gtype;
1822 union g_addr gate;
1823 ifindex_t ifindex;
1824 mpls_label_t in_label, out_label;
1825 u_int8_t distance;
1826 struct zebra_vrf *zvrf;
1827
1828 zvrf = vrf_info_lookup(vrf_id);
1829 if (!zvrf)
1830 return;
1831
1832 /* Get input stream. */
1833 s = client->ibuf;
1834
1835 /* Get data. */
1836 type = stream_getc(s);
1837 prefix.family = stream_getl(s);
1838 switch (prefix.family) {
1839 case AF_INET:
1840 prefix.u.prefix4.s_addr = stream_get_ipv4(s);
1841 prefix.prefixlen = stream_getc(s);
1842 gate.ipv4.s_addr = stream_get_ipv4(s);
1843 break;
1844 case AF_INET6:
1845 stream_get(&prefix.u.prefix6, s, 16);
1846 prefix.prefixlen = stream_getc(s);
1847 stream_get(&gate.ipv6, s, 16);
1848 break;
1849 default:
1850 return;
1851 }
1852 ifindex = stream_getl(s);
1853 distance = stream_getc(s);
1854 in_label = stream_getl(s);
1855 out_label = stream_getl(s);
1856
1857 switch (prefix.family) {
1858 case AF_INET:
1859 if (ifindex)
1860 gtype = NEXTHOP_TYPE_IPV4_IFINDEX;
1861 else
1862 gtype = NEXTHOP_TYPE_IPV4;
1863 break;
1864 case AF_INET6:
1865 if (ifindex)
1866 gtype = NEXTHOP_TYPE_IPV6_IFINDEX;
1867 else
1868 gtype = NEXTHOP_TYPE_IPV6;
1869 break;
1870 default:
1871 return;
1872 }
1873
1874 if (!mpls_enabled)
1875 return;
1876
1877 if (command == ZEBRA_MPLS_LABELS_ADD) {
1878 mpls_lsp_install(zvrf, type, in_label, out_label, gtype, &gate,
1879 ifindex);
1880 if (out_label != MPLS_IMP_NULL_LABEL)
1881 mpls_ftn_update(1, zvrf, type, &prefix, gtype, &gate,
1882 ifindex, distance, out_label);
1883 } else if (command == ZEBRA_MPLS_LABELS_DELETE) {
1884 mpls_lsp_uninstall(zvrf, type, in_label, gtype, &gate, ifindex);
1885 if (out_label != MPLS_IMP_NULL_LABEL)
1886 mpls_ftn_update(0, zvrf, type, &prefix, gtype, &gate,
1887 ifindex, distance, out_label);
1888 }
1889 }
1890 /* Send response to a label manager connect request to client */
1891 static int zsend_label_manager_connect_response(struct zserv *client,
1892 vrf_id_t vrf_id, u_short result)
1893 {
1894 struct stream *s;
1895
1896 s = client->obuf;
1897 stream_reset(s);
1898
1899 zserv_create_header(s, ZEBRA_LABEL_MANAGER_CONNECT, vrf_id);
1900
1901 /* result */
1902 stream_putc(s, result);
1903
1904 /* Write packet size. */
1905 stream_putw_at(s, 0, stream_get_endp(s));
1906
1907 return writen(client->sock, s->data, stream_get_endp(s));
1908 }
1909
1910 static void zread_label_manager_connect(struct zserv *client, vrf_id_t vrf_id)
1911 {
1912 struct stream *s;
1913 /* type of protocol (lib/zebra.h) */
1914 u_char proto;
1915 u_short instance;
1916
1917 /* Get input stream. */
1918 s = client->ibuf;
1919
1920 /* Get data. */
1921 proto = stream_getc(s);
1922 instance = stream_getw(s);
1923
1924 /* accept only dynamic routing protocols */
1925 if ((proto >= ZEBRA_ROUTE_MAX) || (proto <= ZEBRA_ROUTE_STATIC)) {
1926 zlog_err("client %d has wrong protocol %s", client->sock,
1927 zebra_route_string(proto));
1928 zsend_label_manager_connect_response(client, vrf_id, 1);
1929 return;
1930 }
1931 zlog_notice("client %d with instance %u connected as %s", client->sock,
1932 instance, zebra_route_string(proto));
1933 client->proto = proto;
1934 client->instance = instance;
1935
1936 /*
1937 Release previous labels of same protocol and instance.
1938 This is done in case it restarted from an unexpected shutdown.
1939 */
1940 release_daemon_chunks(proto, instance);
1941
1942 zlog_debug(
1943 " Label Manager client connected: sock %d, proto %s, instance %u",
1944 client->sock, zebra_route_string(proto), instance);
1945 /* send response back */
1946 zsend_label_manager_connect_response(client, vrf_id, 0);
1947 }
1948 /* Send response to a get label chunk request to client */
1949 static int zsend_assign_label_chunk_response(struct zserv *client,
1950 vrf_id_t vrf_id,
1951 struct label_manager_chunk *lmc)
1952 {
1953 struct stream *s;
1954
1955 s = client->obuf;
1956 stream_reset(s);
1957
1958 zserv_create_header(s, ZEBRA_GET_LABEL_CHUNK, vrf_id);
1959
1960 if (lmc) {
1961 /* keep */
1962 stream_putc(s, lmc->keep);
1963 /* start and end labels */
1964 stream_putl(s, lmc->start);
1965 stream_putl(s, lmc->end);
1966 }
1967
1968 /* Write packet size. */
1969 stream_putw_at(s, 0, stream_get_endp(s));
1970
1971 return writen(client->sock, s->data, stream_get_endp(s));
1972 }
1973
1974 static void zread_get_label_chunk(struct zserv *client, vrf_id_t vrf_id)
1975 {
1976 struct stream *s;
1977 u_char keep;
1978 uint32_t size;
1979 struct label_manager_chunk *lmc;
1980
1981 /* Get input stream. */
1982 s = client->ibuf;
1983
1984 /* Get data. */
1985 keep = stream_getc(s);
1986 size = stream_getl(s);
1987
1988 lmc = assign_label_chunk(client->proto, client->instance, keep, size);
1989 if (!lmc)
1990 zlog_err("%s: Unable to assign Label Chunk of size %u",
1991 __func__, size);
1992 else
1993 zlog_debug("Assigned Label Chunk %u - %u to %u", lmc->start,
1994 lmc->end, keep);
1995 /* send response back */
1996 zsend_assign_label_chunk_response(client, vrf_id, lmc);
1997 }
1998
1999 static void zread_release_label_chunk(struct zserv *client)
2000 {
2001 struct stream *s;
2002 uint32_t start, end;
2003
2004 /* Get input stream. */
2005 s = client->ibuf;
2006
2007 /* Get data. */
2008 start = stream_getl(s);
2009 end = stream_getl(s);
2010
2011 release_label_chunk(client->proto, client->instance, start, end);
2012 }
2013 static void zread_label_manager_request(int cmd, struct zserv *client,
2014 vrf_id_t vrf_id)
2015 {
2016 /* to avoid sending other messages like ZERBA_INTERFACE_UP */
2017 if (cmd == ZEBRA_LABEL_MANAGER_CONNECT)
2018 client->is_synchronous = 1;
2019
2020 /* external label manager */
2021 if (lm_is_external)
2022 zread_relay_label_manager_request(cmd, client, vrf_id);
2023 /* this is a label manager */
2024 else {
2025 if (cmd == ZEBRA_LABEL_MANAGER_CONNECT)
2026 zread_label_manager_connect(client, vrf_id);
2027 else {
2028 /* Sanity: don't allow 'unidentified' requests */
2029 if (!client->proto) {
2030 zlog_err(
2031 "Got label request from an unidentified client");
2032 return;
2033 }
2034 if (cmd == ZEBRA_GET_LABEL_CHUNK)
2035 zread_get_label_chunk(client, vrf_id);
2036 else if (cmd == ZEBRA_RELEASE_LABEL_CHUNK)
2037 zread_release_label_chunk(client);
2038 }
2039 }
2040 }
2041
2042 /* Cleanup registered nexthops (across VRFs) upon client disconnect. */
2043 static void zebra_client_close_cleanup_rnh(struct zserv *client)
2044 {
2045 struct vrf *vrf;
2046 struct zebra_vrf *zvrf;
2047
2048 RB_FOREACH(vrf, vrf_id_head, &vrfs_by_id)
2049 {
2050 if ((zvrf = vrf->info) != NULL) {
2051 zebra_cleanup_rnh_client(zvrf_id(zvrf), AF_INET, client,
2052 RNH_NEXTHOP_TYPE);
2053 zebra_cleanup_rnh_client(zvrf_id(zvrf), AF_INET6,
2054 client, RNH_NEXTHOP_TYPE);
2055 zebra_cleanup_rnh_client(zvrf_id(zvrf), AF_INET, client,
2056 RNH_IMPORT_CHECK_TYPE);
2057 zebra_cleanup_rnh_client(zvrf_id(zvrf), AF_INET6,
2058 client, RNH_IMPORT_CHECK_TYPE);
2059 if (client->proto == ZEBRA_ROUTE_LDP) {
2060 hash_iterate(zvrf->lsp_table,
2061 mpls_ldp_lsp_uninstall_all,
2062 zvrf->lsp_table);
2063 mpls_ldp_ftn_uninstall_all(zvrf, AFI_IP);
2064 mpls_ldp_ftn_uninstall_all(zvrf, AFI_IP6);
2065 }
2066 }
2067 }
2068 }
2069
2070 /* Close zebra client. */
2071 static void zebra_client_close(struct zserv *client)
2072 {
2073 /* Send client de-registration to BFD */
2074 zebra_ptm_bfd_client_deregister(client->proto);
2075
2076 /* Cleanup any registered nexthops - across all VRFs. */
2077 zebra_client_close_cleanup_rnh(client);
2078
2079 /* Release Label Manager chunks */
2080 release_daemon_chunks(client->proto, client->instance);
2081
2082 /* Cleanup any FECs registered by this client. */
2083 zebra_mpls_cleanup_fecs_for_client(vrf_info_lookup(VRF_DEFAULT),
2084 client);
2085
2086 /* Close file descriptor. */
2087 if (client->sock) {
2088 unsigned long nroutes;
2089
2090 close(client->sock);
2091 nroutes = rib_score_proto(client->proto, client->instance);
2092 zlog_notice(
2093 "client %d disconnected. %lu %s routes removed from the rib",
2094 client->sock, nroutes,
2095 zebra_route_string(client->proto));
2096 client->sock = -1;
2097 }
2098
2099 /* Free stream buffers. */
2100 if (client->ibuf)
2101 stream_free(client->ibuf);
2102 if (client->obuf)
2103 stream_free(client->obuf);
2104 if (client->wb)
2105 buffer_free(client->wb);
2106
2107 /* Release threads. */
2108 if (client->t_read)
2109 thread_cancel(client->t_read);
2110 if (client->t_write)
2111 thread_cancel(client->t_write);
2112 if (client->t_suicide)
2113 thread_cancel(client->t_suicide);
2114
2115 /* Free bitmaps. */
2116 for (afi_t afi = AFI_IP; afi < AFI_MAX; afi++)
2117 for (int i = 0; i < ZEBRA_ROUTE_MAX; i++)
2118 vrf_bitmap_free(client->redist[afi][i]);
2119
2120 vrf_bitmap_free(client->redist_default);
2121 vrf_bitmap_free(client->ifinfo);
2122 vrf_bitmap_free(client->ridinfo);
2123
2124 /* Free client structure. */
2125 listnode_delete(zebrad.client_list, client);
2126 XFREE(MTYPE_TMP, client);
2127 }
2128
2129 /* Make new client. */
2130 static void zebra_client_create(int sock)
2131 {
2132 struct zserv *client;
2133 int i;
2134 afi_t afi;
2135
2136 client = XCALLOC(MTYPE_TMP, sizeof(struct zserv));
2137
2138 /* Make client input/output buffer. */
2139 client->sock = sock;
2140 client->ibuf = stream_new(ZEBRA_MAX_PACKET_SIZ);
2141 client->obuf = stream_new(ZEBRA_MAX_PACKET_SIZ);
2142 client->wb = buffer_new(0);
2143
2144 /* Set table number. */
2145 client->rtm_table = zebrad.rtm_table_default;
2146
2147 client->connect_time = monotime(NULL);
2148 /* Initialize flags */
2149 for (afi = AFI_IP; afi < AFI_MAX; afi++)
2150 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
2151 client->redist[afi][i] = vrf_bitmap_init();
2152 client->redist_default = vrf_bitmap_init();
2153 client->ifinfo = vrf_bitmap_init();
2154 client->ridinfo = vrf_bitmap_init();
2155
2156 /* by default, it's not a synchronous client */
2157 client->is_synchronous = 0;
2158
2159 /* Add this client to linked list. */
2160 listnode_add(zebrad.client_list, client);
2161
2162 /* Make new read thread. */
2163 zebra_event(ZEBRA_READ, sock, client);
2164
2165 zebra_vrf_update_all(client);
2166 }
2167
2168 static int zread_interface_set_master(struct zserv *client, int sock,
2169 u_short length)
2170 {
2171 struct interface *master;
2172 struct interface *slave;
2173 struct stream *s = client->ibuf;
2174 int ifindex;
2175 vrf_id_t vrf_id;
2176
2177 vrf_id = stream_getw(s);
2178 ifindex = stream_getl(s);
2179 master = if_lookup_by_index(ifindex, vrf_id);
2180
2181 vrf_id = stream_getw(s);
2182 ifindex = stream_getl(s);
2183 slave = if_lookup_by_index(ifindex, vrf_id);
2184
2185 if (!master || !slave)
2186 return 0;
2187
2188 kernel_interface_set_master(master, slave);
2189
2190 return 1;
2191 }
2192
2193 /* Handler of zebra service request. */
2194 static int zebra_client_read(struct thread *thread)
2195 {
2196 int sock;
2197 struct zserv *client;
2198 size_t already;
2199 uint16_t length, command;
2200 uint8_t marker, version;
2201 vrf_id_t vrf_id;
2202 struct zebra_vrf *zvrf;
2203
2204 /* Get thread data. Reset reading thread because I'm running. */
2205 sock = THREAD_FD(thread);
2206 client = THREAD_ARG(thread);
2207 client->t_read = NULL;
2208
2209 if (client->t_suicide) {
2210 zebra_client_close(client);
2211 return -1;
2212 }
2213
2214 /* Read length and command (if we don't have it already). */
2215 if ((already = stream_get_endp(client->ibuf)) < ZEBRA_HEADER_SIZE) {
2216 ssize_t nbyte;
2217 if (((nbyte = stream_read_try(client->ibuf, sock,
2218 ZEBRA_HEADER_SIZE - already))
2219 == 0)
2220 || (nbyte == -1)) {
2221 if (IS_ZEBRA_DEBUG_EVENT)
2222 zlog_debug("connection closed socket [%d]",
2223 sock);
2224 zebra_client_close(client);
2225 return -1;
2226 }
2227 if (nbyte != (ssize_t)(ZEBRA_HEADER_SIZE - already)) {
2228 /* Try again later. */
2229 zebra_event(ZEBRA_READ, sock, client);
2230 return 0;
2231 }
2232 already = ZEBRA_HEADER_SIZE;
2233 }
2234
2235 /* Reset to read from the beginning of the incoming packet. */
2236 stream_set_getp(client->ibuf, 0);
2237
2238 /* Fetch header values */
2239 length = stream_getw(client->ibuf);
2240 marker = stream_getc(client->ibuf);
2241 version = stream_getc(client->ibuf);
2242 vrf_id = stream_getw(client->ibuf);
2243 command = stream_getw(client->ibuf);
2244
2245 if (marker != ZEBRA_HEADER_MARKER || version != ZSERV_VERSION) {
2246 zlog_err(
2247 "%s: socket %d version mismatch, marker %d, version %d",
2248 __func__, sock, marker, version);
2249 zebra_client_close(client);
2250 return -1;
2251 }
2252 if (length < ZEBRA_HEADER_SIZE) {
2253 zlog_warn(
2254 "%s: socket %d message length %u is less than header size %d",
2255 __func__, sock, length, ZEBRA_HEADER_SIZE);
2256 zebra_client_close(client);
2257 return -1;
2258 }
2259 if (length > STREAM_SIZE(client->ibuf)) {
2260 zlog_warn(
2261 "%s: socket %d message length %u exceeds buffer size %lu",
2262 __func__, sock, length,
2263 (u_long)STREAM_SIZE(client->ibuf));
2264 zebra_client_close(client);
2265 return -1;
2266 }
2267
2268 /* Read rest of data. */
2269 if (already < length) {
2270 ssize_t nbyte;
2271 if (((nbyte = stream_read_try(client->ibuf, sock,
2272 length - already))
2273 == 0)
2274 || (nbyte == -1)) {
2275 if (IS_ZEBRA_DEBUG_EVENT)
2276 zlog_debug(
2277 "connection closed [%d] when reading zebra data",
2278 sock);
2279 zebra_client_close(client);
2280 return -1;
2281 }
2282 if (nbyte != (ssize_t)(length - already)) {
2283 /* Try again later. */
2284 zebra_event(ZEBRA_READ, sock, client);
2285 return 0;
2286 }
2287 }
2288
2289 length -= ZEBRA_HEADER_SIZE;
2290
2291 /* Debug packet information. */
2292 if (IS_ZEBRA_DEBUG_EVENT)
2293 zlog_debug("zebra message comes from socket [%d]", sock);
2294
2295 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
2296 zlog_debug("zebra message received [%s] %d in VRF %u",
2297 zserv_command_string(command), length, vrf_id);
2298
2299 client->last_read_time = monotime(NULL);
2300 client->last_read_cmd = command;
2301
2302 zvrf = zebra_vrf_lookup_by_id(vrf_id);
2303 if (!zvrf) {
2304 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
2305 zlog_debug("zebra received unknown VRF[%u]", vrf_id);
2306 goto zclient_read_out;
2307 }
2308
2309 switch (command) {
2310 case ZEBRA_ROUTER_ID_ADD:
2311 zread_router_id_add(client, length, zvrf);
2312 break;
2313 case ZEBRA_ROUTER_ID_DELETE:
2314 zread_router_id_delete(client, length, zvrf);
2315 break;
2316 case ZEBRA_INTERFACE_ADD:
2317 zread_interface_add(client, length, zvrf);
2318 break;
2319 case ZEBRA_INTERFACE_DELETE:
2320 zread_interface_delete(client, length, zvrf);
2321 break;
2322 case ZEBRA_IPV4_ROUTE_ADD:
2323 zread_ipv4_add(client, length, zvrf);
2324 break;
2325 case ZEBRA_IPV4_ROUTE_DELETE:
2326 zread_ipv4_delete(client, length, zvrf);
2327 break;
2328 case ZEBRA_IPV4_ROUTE_IPV6_NEXTHOP_ADD:
2329 zread_ipv4_route_ipv6_nexthop_add(client, length, zvrf);
2330 break;
2331 case ZEBRA_IPV4_NEXTHOP_ADD:
2332 zread_ipv4_add(client, length,
2333 zvrf); /* LB: r1.0 merge - id was 1 */
2334 break;
2335 case ZEBRA_IPV4_NEXTHOP_DELETE:
2336 zread_ipv4_delete(client, length,
2337 zvrf); /* LB: r1.0 merge - id was 1 */
2338 break;
2339 case ZEBRA_IPV6_ROUTE_ADD:
2340 zread_ipv6_add(client, length, zvrf);
2341 break;
2342 case ZEBRA_IPV6_ROUTE_DELETE:
2343 zread_ipv6_delete(client, length, zvrf);
2344 break;
2345 case ZEBRA_REDISTRIBUTE_ADD:
2346 zebra_redistribute_add(command, client, length, zvrf);
2347 break;
2348 case ZEBRA_REDISTRIBUTE_DELETE:
2349 zebra_redistribute_delete(command, client, length, zvrf);
2350 break;
2351 case ZEBRA_REDISTRIBUTE_DEFAULT_ADD:
2352 zebra_redistribute_default_add(command, client, length, zvrf);
2353 break;
2354 case ZEBRA_REDISTRIBUTE_DEFAULT_DELETE:
2355 zebra_redistribute_default_delete(command, client, length,
2356 zvrf);
2357 break;
2358 case ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB:
2359 zread_ipv4_nexthop_lookup_mrib(client, length, zvrf);
2360 break;
2361 case ZEBRA_HELLO:
2362 zread_hello(client);
2363 break;
2364 case ZEBRA_NEXTHOP_REGISTER:
2365 zserv_rnh_register(client, sock, length, RNH_NEXTHOP_TYPE,
2366 zvrf);
2367 break;
2368 case ZEBRA_NEXTHOP_UNREGISTER:
2369 zserv_rnh_unregister(client, sock, length, RNH_NEXTHOP_TYPE,
2370 zvrf);
2371 break;
2372 case ZEBRA_IMPORT_ROUTE_REGISTER:
2373 zserv_rnh_register(client, sock, length, RNH_IMPORT_CHECK_TYPE,
2374 zvrf);
2375 break;
2376 case ZEBRA_IMPORT_ROUTE_UNREGISTER:
2377 zserv_rnh_unregister(client, sock, length,
2378 RNH_IMPORT_CHECK_TYPE, zvrf);
2379 break;
2380 case ZEBRA_BFD_DEST_UPDATE:
2381 case ZEBRA_BFD_DEST_REGISTER:
2382 zebra_ptm_bfd_dst_register(client, sock, length, command, zvrf);
2383 break;
2384 case ZEBRA_BFD_DEST_DEREGISTER:
2385 zebra_ptm_bfd_dst_deregister(client, sock, length, zvrf);
2386 break;
2387 case ZEBRA_VRF_UNREGISTER:
2388 zread_vrf_unregister(client, length, zvrf);
2389 break;
2390 case ZEBRA_BFD_CLIENT_REGISTER:
2391 zebra_ptm_bfd_client_register(client, sock, length);
2392 break;
2393 case ZEBRA_INTERFACE_ENABLE_RADV:
2394 #if defined(HAVE_RTADV)
2395 zebra_interface_radv_set(client, sock, length, zvrf, 1);
2396 #endif
2397 break;
2398 case ZEBRA_INTERFACE_DISABLE_RADV:
2399 #if defined(HAVE_RTADV)
2400 zebra_interface_radv_set(client, sock, length, zvrf, 0);
2401 #endif
2402 break;
2403 case ZEBRA_MPLS_LABELS_ADD:
2404 case ZEBRA_MPLS_LABELS_DELETE:
2405 zread_mpls_labels(command, client, length, vrf_id);
2406 break;
2407 case ZEBRA_IPMR_ROUTE_STATS:
2408 zebra_ipmr_route_stats(client, sock, length, zvrf);
2409 break;
2410 case ZEBRA_LABEL_MANAGER_CONNECT:
2411 case ZEBRA_GET_LABEL_CHUNK:
2412 case ZEBRA_RELEASE_LABEL_CHUNK:
2413 zread_label_manager_request(command, client, vrf_id);
2414 break;
2415 case ZEBRA_FEC_REGISTER:
2416 zserv_fec_register(client, sock, length);
2417 break;
2418 case ZEBRA_FEC_UNREGISTER:
2419 zserv_fec_unregister(client, sock, length);
2420 break;
2421 case ZEBRA_ADVERTISE_ALL_VNI:
2422 zebra_vxlan_advertise_all_vni(client, sock, length, zvrf);
2423 break;
2424 case ZEBRA_REMOTE_VTEP_ADD:
2425 zebra_vxlan_remote_vtep_add(client, sock, length, zvrf);
2426 break;
2427 case ZEBRA_REMOTE_VTEP_DEL:
2428 zebra_vxlan_remote_vtep_del(client, sock, length, zvrf);
2429 break;
2430 case ZEBRA_REMOTE_MACIP_ADD:
2431 zebra_vxlan_remote_macip_add(client, sock, length, zvrf);
2432 break;
2433 case ZEBRA_REMOTE_MACIP_DEL:
2434 zebra_vxlan_remote_macip_del(client, sock, length, zvrf);
2435 case ZEBRA_INTERFACE_SET_MASTER:
2436 zread_interface_set_master(client, sock, length);
2437 break;
2438 default:
2439 zlog_info("Zebra received unknown command %d", command);
2440 break;
2441 }
2442
2443 if (client->t_suicide) {
2444 /* No need to wait for thread callback, just kill immediately.
2445 */
2446 zebra_client_close(client);
2447 return -1;
2448 }
2449
2450 zclient_read_out:
2451 stream_reset(client->ibuf);
2452 zebra_event(ZEBRA_READ, sock, client);
2453 return 0;
2454 }
2455
2456
2457 /* Accept code of zebra server socket. */
2458 static int zebra_accept(struct thread *thread)
2459 {
2460 int accept_sock;
2461 int client_sock;
2462 struct sockaddr_in client;
2463 socklen_t len;
2464
2465 accept_sock = THREAD_FD(thread);
2466
2467 /* Reregister myself. */
2468 zebra_event(ZEBRA_SERV, accept_sock, NULL);
2469
2470 len = sizeof(struct sockaddr_in);
2471 client_sock = accept(accept_sock, (struct sockaddr *)&client, &len);
2472
2473 if (client_sock < 0) {
2474 zlog_warn("Can't accept zebra socket: %s",
2475 safe_strerror(errno));
2476 return -1;
2477 }
2478
2479 /* Make client socket non-blocking. */
2480 set_nonblocking(client_sock);
2481
2482 /* Create new zebra client. */
2483 zebra_client_create(client_sock);
2484
2485 return 0;
2486 }
2487
2488 #ifdef HAVE_TCP_ZEBRA
2489 /* Make zebra's server socket. */
2490 static void zebra_serv()
2491 {
2492 int ret;
2493 int accept_sock;
2494 struct sockaddr_in addr;
2495
2496 accept_sock = socket(AF_INET, SOCK_STREAM, 0);
2497
2498 if (accept_sock < 0) {
2499 zlog_warn("Can't create zserv stream socket: %s",
2500 safe_strerror(errno));
2501 zlog_warn(
2502 "zebra can't provice full functionality due to above error");
2503 return;
2504 }
2505
2506 memset(&addr, 0, sizeof(struct sockaddr_in));
2507 addr.sin_family = AF_INET;
2508 addr.sin_port = htons(ZEBRA_PORT);
2509 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
2510 addr.sin_len = sizeof(struct sockaddr_in);
2511 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
2512 addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
2513
2514 sockopt_reuseaddr(accept_sock);
2515 sockopt_reuseport(accept_sock);
2516
2517 if (zserv_privs.change(ZPRIVS_RAISE))
2518 zlog_err("Can't raise privileges");
2519
2520 ret = bind(accept_sock, (struct sockaddr *)&addr,
2521 sizeof(struct sockaddr_in));
2522 if (ret < 0) {
2523 zlog_warn("Can't bind to stream socket: %s",
2524 safe_strerror(errno));
2525 zlog_warn(
2526 "zebra can't provice full functionality due to above error");
2527 close(accept_sock); /* Avoid sd leak. */
2528 return;
2529 }
2530
2531 if (zserv_privs.change(ZPRIVS_LOWER))
2532 zlog_err("Can't lower privileges");
2533
2534 ret = listen(accept_sock, 1);
2535 if (ret < 0) {
2536 zlog_warn("Can't listen to stream socket: %s",
2537 safe_strerror(errno));
2538 zlog_warn(
2539 "zebra can't provice full functionality due to above error");
2540 close(accept_sock); /* Avoid sd leak. */
2541 return;
2542 }
2543
2544 zebra_event(ZEBRA_SERV, accept_sock, NULL);
2545 }
2546 #else /* HAVE_TCP_ZEBRA */
2547
2548 /* For sockaddr_un. */
2549 #include <sys/un.h>
2550
2551 /* zebra server UNIX domain socket. */
2552 static void zebra_serv_un(const char *path)
2553 {
2554 int ret;
2555 int sock, len;
2556 struct sockaddr_un serv;
2557 mode_t old_mask;
2558
2559 /* First of all, unlink existing socket */
2560 unlink(path);
2561
2562 /* Set umask */
2563 old_mask = umask(0077);
2564
2565 /* Make UNIX domain socket. */
2566 sock = socket(AF_UNIX, SOCK_STREAM, 0);
2567 if (sock < 0) {
2568 zlog_warn("Can't create zserv unix socket: %s",
2569 safe_strerror(errno));
2570 zlog_warn(
2571 "zebra can't provide full functionality due to above error");
2572 return;
2573 }
2574
2575 /* Make server socket. */
2576 memset(&serv, 0, sizeof(struct sockaddr_un));
2577 serv.sun_family = AF_UNIX;
2578 strncpy(serv.sun_path, path, strlen(path));
2579 #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
2580 len = serv.sun_len = SUN_LEN(&serv);
2581 #else
2582 len = sizeof(serv.sun_family) + strlen(serv.sun_path);
2583 #endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
2584
2585 ret = bind(sock, (struct sockaddr *)&serv, len);
2586 if (ret < 0) {
2587 zlog_warn("Can't bind to unix socket %s: %s", path,
2588 safe_strerror(errno));
2589 zlog_warn(
2590 "zebra can't provide full functionality due to above error");
2591 close(sock);
2592 return;
2593 }
2594
2595 ret = listen(sock, 5);
2596 if (ret < 0) {
2597 zlog_warn("Can't listen to unix socket %s: %s", path,
2598 safe_strerror(errno));
2599 zlog_warn(
2600 "zebra can't provide full functionality due to above error");
2601 close(sock);
2602 return;
2603 }
2604
2605 umask(old_mask);
2606
2607 zebra_event(ZEBRA_SERV, sock, NULL);
2608 }
2609 #endif /* HAVE_TCP_ZEBRA */
2610
2611
2612 static void zebra_event(enum event event, int sock, struct zserv *client)
2613 {
2614 switch (event) {
2615 case ZEBRA_SERV:
2616 thread_add_read(zebrad.master, zebra_accept, client, sock,
2617 NULL);
2618 break;
2619 case ZEBRA_READ:
2620 client->t_read = NULL;
2621 thread_add_read(zebrad.master, zebra_client_read, client, sock,
2622 &client->t_read);
2623 break;
2624 case ZEBRA_WRITE:
2625 /**/
2626 break;
2627 }
2628 }
2629
2630 #define ZEBRA_TIME_BUF 32
2631 static char *zserv_time_buf(time_t *time1, char *buf, int buflen)
2632 {
2633 struct tm *tm;
2634 time_t now;
2635
2636 assert(buf != NULL);
2637 assert(buflen >= ZEBRA_TIME_BUF);
2638 assert(time1 != NULL);
2639
2640 if (!*time1) {
2641 snprintf(buf, buflen, "never ");
2642 return (buf);
2643 }
2644
2645 now = monotime(NULL);
2646 now -= *time1;
2647 tm = gmtime(&now);
2648
2649 /* Making formatted timer strings. */
2650 #define ONE_DAY_SECOND 60*60*24
2651 #define ONE_WEEK_SECOND 60*60*24*7
2652
2653 if (now < ONE_DAY_SECOND)
2654 snprintf(buf, buflen, "%02d:%02d:%02d", tm->tm_hour, tm->tm_min,
2655 tm->tm_sec);
2656 else if (now < ONE_WEEK_SECOND)
2657 snprintf(buf, buflen, "%dd%02dh%02dm", tm->tm_yday, tm->tm_hour,
2658 tm->tm_min);
2659 else
2660 snprintf(buf, buflen, "%02dw%dd%02dh", tm->tm_yday / 7,
2661 tm->tm_yday - ((tm->tm_yday / 7) * 7), tm->tm_hour);
2662 return buf;
2663 }
2664
2665 static void zebra_show_client_detail(struct vty *vty, struct zserv *client)
2666 {
2667 char cbuf[ZEBRA_TIME_BUF], rbuf[ZEBRA_TIME_BUF];
2668 char wbuf[ZEBRA_TIME_BUF], nhbuf[ZEBRA_TIME_BUF], mbuf[ZEBRA_TIME_BUF];
2669
2670 vty_out(vty, "Client: %s", zebra_route_string(client->proto));
2671 if (client->instance)
2672 vty_out(vty, " Instance: %d", client->instance);
2673 vty_out(vty, "\n");
2674
2675 vty_out(vty, "------------------------ \n");
2676 vty_out(vty, "FD: %d \n", client->sock);
2677 vty_out(vty, "Route Table ID: %d \n", client->rtm_table);
2678
2679 vty_out(vty, "Connect Time: %s \n",
2680 zserv_time_buf(&client->connect_time, cbuf, ZEBRA_TIME_BUF));
2681 if (client->nh_reg_time) {
2682 vty_out(vty, "Nexthop Registry Time: %s \n",
2683 zserv_time_buf(&client->nh_reg_time, nhbuf,
2684 ZEBRA_TIME_BUF));
2685 if (client->nh_last_upd_time)
2686 vty_out(vty, "Nexthop Last Update Time: %s \n",
2687 zserv_time_buf(&client->nh_last_upd_time, mbuf,
2688 ZEBRA_TIME_BUF));
2689 else
2690 vty_out(vty, "No Nexthop Update sent\n");
2691 } else
2692 vty_out(vty, "Not registered for Nexthop Updates\n");
2693
2694 vty_out(vty, "Last Msg Rx Time: %s \n",
2695 zserv_time_buf(&client->last_read_time, rbuf, ZEBRA_TIME_BUF));
2696 vty_out(vty, "Last Msg Tx Time: %s \n",
2697 zserv_time_buf(&client->last_write_time, wbuf, ZEBRA_TIME_BUF));
2698 if (client->last_read_time)
2699 vty_out(vty, "Last Rcvd Cmd: %s \n",
2700 zserv_command_string(client->last_read_cmd));
2701 if (client->last_write_time)
2702 vty_out(vty, "Last Sent Cmd: %s \n",
2703 zserv_command_string(client->last_write_cmd));
2704 vty_out(vty, "\n");
2705
2706 vty_out(vty, "Type Add Update Del \n");
2707 vty_out(vty, "================================================== \n");
2708 vty_out(vty, "IPv4 %-12d%-12d%-12d\n", client->v4_route_add_cnt,
2709 client->v4_route_upd8_cnt, client->v4_route_del_cnt);
2710 vty_out(vty, "IPv6 %-12d%-12d%-12d\n", client->v6_route_add_cnt,
2711 client->v6_route_upd8_cnt, client->v6_route_del_cnt);
2712 vty_out(vty, "Redist:v4 %-12d%-12d%-12d\n", client->redist_v4_add_cnt,
2713 0, client->redist_v4_del_cnt);
2714 vty_out(vty, "Redist:v6 %-12d%-12d%-12d\n", client->redist_v6_add_cnt,
2715 0, client->redist_v6_del_cnt);
2716 vty_out(vty, "Connected %-12d%-12d%-12d\n", client->ifadd_cnt, 0,
2717 client->ifdel_cnt);
2718 vty_out(vty, "BFD peer %-12d%-12d%-12d\n", client->bfd_peer_add_cnt,
2719 client->bfd_peer_upd8_cnt, client->bfd_peer_del_cnt);
2720 vty_out(vty, "Interface Up Notifications: %d\n", client->ifup_cnt);
2721 vty_out(vty, "Interface Down Notifications: %d\n", client->ifdown_cnt);
2722 vty_out(vty, "VNI add notifications: %d\n", client->vniadd_cnt);
2723 vty_out(vty, "VNI delete notifications: %d\n", client->vnidel_cnt);
2724 vty_out(vty, "MAC-IP add notifications: %d\n", client->macipadd_cnt);
2725 vty_out(vty, "MAC-IP delete notifications: %d\n", client->macipdel_cnt);
2726
2727 vty_out(vty, "\n");
2728 return;
2729 }
2730
2731 static void zebra_show_client_brief(struct vty *vty, struct zserv *client)
2732 {
2733 char cbuf[ZEBRA_TIME_BUF], rbuf[ZEBRA_TIME_BUF];
2734 char wbuf[ZEBRA_TIME_BUF];
2735
2736 vty_out(vty, "%-8s%12s %12s%12s%8d/%-8d%8d/%-8d\n",
2737 zebra_route_string(client->proto),
2738 zserv_time_buf(&client->connect_time, cbuf, ZEBRA_TIME_BUF),
2739 zserv_time_buf(&client->last_read_time, rbuf, ZEBRA_TIME_BUF),
2740 zserv_time_buf(&client->last_write_time, wbuf, ZEBRA_TIME_BUF),
2741 client->v4_route_add_cnt + client->v4_route_upd8_cnt,
2742 client->v4_route_del_cnt,
2743 client->v6_route_add_cnt + client->v6_route_upd8_cnt,
2744 client->v6_route_del_cnt);
2745 }
2746
2747 struct zserv *zebra_find_client(u_char proto)
2748 {
2749 struct listnode *node, *nnode;
2750 struct zserv *client;
2751
2752 for (ALL_LIST_ELEMENTS(zebrad.client_list, node, nnode, client)) {
2753 if (client->proto == proto)
2754 return client;
2755 }
2756
2757 return NULL;
2758 }
2759
2760 #ifdef HAVE_NETLINK
2761 /* Display default rtm_table for all clients. */
2762 DEFUN (show_table,
2763 show_table_cmd,
2764 "show table",
2765 SHOW_STR
2766 "default routing table to use for all clients\n")
2767 {
2768 vty_out(vty, "table %d\n", zebrad.rtm_table_default);
2769 return CMD_SUCCESS;
2770 }
2771
2772 DEFUN (config_table,
2773 config_table_cmd,
2774 "table TABLENO",
2775 "Configure target kernel routing table\n"
2776 "TABLE integer\n")
2777 {
2778 zebrad.rtm_table_default = strtol(argv[1]->arg, (char **)0, 10);
2779 return CMD_SUCCESS;
2780 }
2781
2782 DEFUN (no_config_table,
2783 no_config_table_cmd,
2784 "no table [TABLENO]",
2785 NO_STR
2786 "Configure target kernel routing table\n"
2787 "TABLE integer\n")
2788 {
2789 zebrad.rtm_table_default = 0;
2790 return CMD_SUCCESS;
2791 }
2792 #endif
2793
2794 DEFUN (ip_forwarding,
2795 ip_forwarding_cmd,
2796 "ip forwarding",
2797 IP_STR
2798 "Turn on IP forwarding")
2799 {
2800 int ret;
2801
2802 ret = ipforward();
2803 if (ret == 0)
2804 ret = ipforward_on();
2805
2806 if (ret == 0) {
2807 vty_out(vty, "Can't turn on IP forwarding\n");
2808 return CMD_WARNING_CONFIG_FAILED;
2809 }
2810
2811 return CMD_SUCCESS;
2812 }
2813
2814 DEFUN (no_ip_forwarding,
2815 no_ip_forwarding_cmd,
2816 "no ip forwarding",
2817 NO_STR
2818 IP_STR
2819 "Turn off IP forwarding")
2820 {
2821 int ret;
2822
2823 ret = ipforward();
2824 if (ret != 0)
2825 ret = ipforward_off();
2826
2827 if (ret != 0) {
2828 vty_out(vty, "Can't turn off IP forwarding\n");
2829 return CMD_WARNING_CONFIG_FAILED;
2830 }
2831
2832 return CMD_SUCCESS;
2833 }
2834
2835 DEFUN (show_zebra,
2836 show_zebra_cmd,
2837 "show zebra",
2838 SHOW_STR
2839 "Zebra information\n")
2840 {
2841 struct vrf *vrf;
2842
2843 vty_out(vty,
2844 " Route Route Neighbor LSP LSP\n");
2845 vty_out(vty,
2846 "VRF Installs Removals Updates Installs Removals\n");
2847 RB_FOREACH(vrf, vrf_name_head, &vrfs_by_name)
2848 {
2849 struct zebra_vrf *zvrf = vrf->info;
2850 vty_out(vty, "%-25s %10" PRIu64 " %10" PRIu64 " %10" PRIu64
2851 " %10" PRIu64 " %10" PRIu64 "\n",
2852 vrf->name, zvrf->installs, zvrf->removals,
2853 zvrf->neigh_updates, zvrf->lsp_installs,
2854 zvrf->lsp_removals);
2855 }
2856
2857 return CMD_SUCCESS;
2858 }
2859
2860 /* This command is for debugging purpose. */
2861 DEFUN (show_zebra_client,
2862 show_zebra_client_cmd,
2863 "show zebra client",
2864 SHOW_STR
2865 "Zebra information\n"
2866 "Client information\n")
2867 {
2868 struct listnode *node;
2869 struct zserv *client;
2870
2871 for (ALL_LIST_ELEMENTS_RO(zebrad.client_list, node, client))
2872 zebra_show_client_detail(vty, client);
2873
2874 return CMD_SUCCESS;
2875 }
2876
2877 /* This command is for debugging purpose. */
2878 DEFUN (show_zebra_client_summary,
2879 show_zebra_client_summary_cmd,
2880 "show zebra client summary",
2881 SHOW_STR
2882 "Zebra information brief\n"
2883 "Client information brief\n"
2884 "Brief Summary\n")
2885 {
2886 struct listnode *node;
2887 struct zserv *client;
2888
2889 vty_out(vty,
2890 "Name Connect Time Last Read Last Write IPv4 Routes IPv6 Routes \n");
2891 vty_out(vty,
2892 "--------------------------------------------------------------------------------\n");
2893
2894 for (ALL_LIST_ELEMENTS_RO(zebrad.client_list, node, client))
2895 zebra_show_client_brief(vty, client);
2896
2897 vty_out(vty, "Routes column shows (added+updated)/deleted\n");
2898 return CMD_SUCCESS;
2899 }
2900
2901 /* Table configuration write function. */
2902 static int config_write_table(struct vty *vty)
2903 {
2904 if (zebrad.rtm_table_default)
2905 vty_out(vty, "table %d\n", zebrad.rtm_table_default);
2906 return 0;
2907 }
2908
2909 /* table node for routing tables. */
2910 static struct cmd_node table_node = {TABLE_NODE,
2911 "", /* This node has no interface. */
2912 1};
2913
2914 /* Only display ip forwarding is enabled or not. */
2915 DEFUN (show_ip_forwarding,
2916 show_ip_forwarding_cmd,
2917 "show ip forwarding",
2918 SHOW_STR
2919 IP_STR
2920 "IP forwarding status\n")
2921 {
2922 int ret;
2923
2924 ret = ipforward();
2925
2926 if (ret == 0)
2927 vty_out(vty, "IP forwarding is off\n");
2928 else
2929 vty_out(vty, "IP forwarding is on\n");
2930 return CMD_SUCCESS;
2931 }
2932
2933 /* Only display ipv6 forwarding is enabled or not. */
2934 DEFUN (show_ipv6_forwarding,
2935 show_ipv6_forwarding_cmd,
2936 "show ipv6 forwarding",
2937 SHOW_STR
2938 "IPv6 information\n"
2939 "Forwarding status\n")
2940 {
2941 int ret;
2942
2943 ret = ipforward_ipv6();
2944
2945 switch (ret) {
2946 case -1:
2947 vty_out(vty, "ipv6 forwarding is unknown\n");
2948 break;
2949 case 0:
2950 vty_out(vty, "ipv6 forwarding is %s\n", "off");
2951 break;
2952 case 1:
2953 vty_out(vty, "ipv6 forwarding is %s\n", "on");
2954 break;
2955 default:
2956 vty_out(vty, "ipv6 forwarding is %s\n", "off");
2957 break;
2958 }
2959 return CMD_SUCCESS;
2960 }
2961
2962 DEFUN (ipv6_forwarding,
2963 ipv6_forwarding_cmd,
2964 "ipv6 forwarding",
2965 IPV6_STR
2966 "Turn on IPv6 forwarding")
2967 {
2968 int ret;
2969
2970 ret = ipforward_ipv6();
2971 if (ret == 0)
2972 ret = ipforward_ipv6_on();
2973
2974 if (ret == 0) {
2975 vty_out(vty, "Can't turn on IPv6 forwarding\n");
2976 return CMD_WARNING_CONFIG_FAILED;
2977 }
2978
2979 return CMD_SUCCESS;
2980 }
2981
2982 DEFUN (no_ipv6_forwarding,
2983 no_ipv6_forwarding_cmd,
2984 "no ipv6 forwarding",
2985 NO_STR
2986 IPV6_STR
2987 "Turn off IPv6 forwarding")
2988 {
2989 int ret;
2990
2991 ret = ipforward_ipv6();
2992 if (ret != 0)
2993 ret = ipforward_ipv6_off();
2994
2995 if (ret != 0) {
2996 vty_out(vty, "Can't turn off IPv6 forwarding\n");
2997 return CMD_WARNING_CONFIG_FAILED;
2998 }
2999
3000 return CMD_SUCCESS;
3001 }
3002
3003 /* IPForwarding configuration write function. */
3004 static int config_write_forwarding(struct vty *vty)
3005 {
3006 /* FIXME: Find better place for that. */
3007 router_id_write(vty);
3008
3009 if (!ipforward())
3010 vty_out(vty, "no ip forwarding\n");
3011 if (!ipforward_ipv6())
3012 vty_out(vty, "no ipv6 forwarding\n");
3013 vty_out(vty, "!\n");
3014 return 0;
3015 }
3016
3017 /* table node for routing tables. */
3018 static struct cmd_node forwarding_node = {FORWARDING_NODE,
3019 "", /* This node has no interface. */
3020 1};
3021
3022 /* Initialisation of zebra and installation of commands. */
3023 void zebra_init(void)
3024 {
3025 /* Client list init. */
3026 zebrad.client_list = list_new();
3027
3028 /* Install configuration write function. */
3029 install_node(&table_node, config_write_table);
3030 install_node(&forwarding_node, config_write_forwarding);
3031
3032 install_element(VIEW_NODE, &show_ip_forwarding_cmd);
3033 install_element(CONFIG_NODE, &ip_forwarding_cmd);
3034 install_element(CONFIG_NODE, &no_ip_forwarding_cmd);
3035 install_element(ENABLE_NODE, &show_zebra_cmd);
3036 install_element(ENABLE_NODE, &show_zebra_client_cmd);
3037 install_element(ENABLE_NODE, &show_zebra_client_summary_cmd);
3038
3039 #ifdef HAVE_NETLINK
3040 install_element(VIEW_NODE, &show_table_cmd);
3041 install_element(CONFIG_NODE, &config_table_cmd);
3042 install_element(CONFIG_NODE, &no_config_table_cmd);
3043 #endif /* HAVE_NETLINK */
3044
3045 install_element(VIEW_NODE, &show_ipv6_forwarding_cmd);
3046 install_element(CONFIG_NODE, &ipv6_forwarding_cmd);
3047 install_element(CONFIG_NODE, &no_ipv6_forwarding_cmd);
3048
3049 /* Route-map */
3050 zebra_route_map_init();
3051 }
3052
3053 /* Make zebra server socket, wiping any existing one (see bug #403). */
3054 void zebra_zserv_socket_init(char *path)
3055 {
3056 #ifdef HAVE_TCP_ZEBRA
3057 zebra_serv();
3058 #else
3059 zebra_serv_un(path ? path : ZEBRA_SERV_PATH);
3060 #endif /* HAVE_TCP_ZEBRA */
3061 }