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