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