blob: ab0d49f0f79511d3de52a13020752d66c827f3af [file] [log] [blame]
paul718e3742002-12-13 20:15:29 +00001/* Router advertisement
2 * Copyright (C) 1999 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 Free
18 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 * 02111-1307, USA.
20 */
21
22#include <zebra.h>
23
24#include "memory.h"
25#include "sockopt.h"
26#include "thread.h"
27#include "if.h"
28#include "log.h"
29#include "prefix.h"
30#include "linklist.h"
31#include "command.h"
pauledd7c242003-06-04 13:59:38 +000032#include "privs.h"
paul718e3742002-12-13 20:15:29 +000033
34#include "zebra/interface.h"
35#include "zebra/rtadv.h"
36#include "zebra/debug.h"
37
pauledd7c242003-06-04 13:59:38 +000038extern struct zebra_privs_t zserv_privs;
39
paul718e3742002-12-13 20:15:29 +000040#if defined (HAVE_IPV6) && defined (RTADV)
41
42/* If RFC2133 definition is used. */
43#ifndef IPV6_JOIN_GROUP
44#define IPV6_JOIN_GROUP IPV6_ADD_MEMBERSHIP
45#endif
46#ifndef IPV6_LEAVE_GROUP
47#define IPV6_LEAVE_GROUP IPV6_DROP_MEMBERSHIP
48#endif
49
50#define ALLNODE "ff02::1"
51#define ALLROUTER "ff02::2"
52
53enum rtadv_event {RTADV_START, RTADV_STOP, RTADV_TIMER, RTADV_READ};
54
55void rtadv_event (enum rtadv_event, int);
56
57int if_join_all_router (int, struct interface *);
58int if_leave_all_router (int, struct interface *);
59
60/* Structure which hold status of router advertisement. */
61struct rtadv
62{
63 int sock;
64
65 int adv_if_count;
66
67 struct thread *ra_read;
68 struct thread *ra_timer;
69};
70
71struct rtadv *rtadv = NULL;
72
73struct rtadv *
74rtadv_new ()
75{
76 struct rtadv *new;
77 new = XMALLOC (MTYPE_TMP, sizeof (struct rtadv));
78 memset (new, 0, sizeof (struct rtadv));
79 return new;
80}
81
82void
83rtadv_free (struct rtadv *rtadv)
84{
85 XFREE (MTYPE_TMP, rtadv);
86}
87
88int
89rtadv_recv_packet (int sock, u_char *buf, int buflen,
90 struct sockaddr_in6 *from, unsigned int *ifindex,
91 int *hoplimit)
92{
93 int ret;
94 struct msghdr msg;
95 struct iovec iov;
96 struct cmsghdr *cmsgptr;
97 struct in6_addr dst;
98
99 char adata[1024];
100
101 /* Fill in message and iovec. */
102 msg.msg_name = (void *) from;
103 msg.msg_namelen = sizeof (struct sockaddr_in6);
104 msg.msg_iov = &iov;
105 msg.msg_iovlen = 1;
106 msg.msg_control = (void *) adata;
107 msg.msg_controllen = sizeof adata;
108 iov.iov_base = buf;
109 iov.iov_len = buflen;
110
111 /* If recvmsg fail return minus value. */
112 ret = recvmsg (sock, &msg, 0);
113 if (ret < 0)
114 return ret;
115
116 for (cmsgptr = CMSG_FIRSTHDR(&msg); cmsgptr != NULL;
117 cmsgptr = CMSG_NXTHDR(&msg, cmsgptr))
118 {
119 /* I want interface index which this packet comes from. */
120 if (cmsgptr->cmsg_level == IPPROTO_IPV6 &&
121 cmsgptr->cmsg_type == IPV6_PKTINFO)
122 {
123 struct in6_pktinfo *ptr;
124
125 ptr = (struct in6_pktinfo *) CMSG_DATA (cmsgptr);
126 *ifindex = ptr->ipi6_ifindex;
127 memcpy(&dst, &ptr->ipi6_addr, sizeof(ptr->ipi6_addr));
128 }
129
130 /* Incoming packet's hop limit. */
131 if (cmsgptr->cmsg_level == IPPROTO_IPV6 &&
132 cmsgptr->cmsg_type == IPV6_HOPLIMIT)
133 *hoplimit = *((int *) CMSG_DATA (cmsgptr));
134 }
135 return ret;
136}
137
138#define RTADV_MSG_SIZE 4096
139
140/* Send router advertisement packet. */
141void
142rtadv_send_packet (int sock, struct interface *ifp)
143{
144 struct msghdr msg;
145 struct iovec iov;
146 struct cmsghdr *cmsgptr;
147 struct in6_pktinfo *pkt;
148 struct sockaddr_in6 addr;
pauledd7c242003-06-04 13:59:38 +0000149#ifdef HAVE_SOCKADDR_DL
paul718e3742002-12-13 20:15:29 +0000150 struct sockaddr_dl *sdl;
151#endif /* HAVE_SOCKADDR_DL */
152 char adata [sizeof (struct cmsghdr) + sizeof (struct in6_pktinfo)];
153 unsigned char buf[RTADV_MSG_SIZE];
154 struct nd_router_advert *rtadv;
155 int ret;
156 int len = 0;
157 struct zebra_if *zif;
158 u_char all_nodes_addr[] = {0xff,0x02,0,0,0,0,0,0,0,0,0,0,0,0,0,1};
159 listnode node;
160
161 /* Logging of packet. */
162 if (IS_ZEBRA_DEBUG_PACKET)
163 zlog_info ("Router advertisement send to %s", ifp->name);
164
165 /* Fill in sockaddr_in6. */
166 memset (&addr, 0, sizeof (struct sockaddr_in6));
167 addr.sin6_family = AF_INET6;
168#ifdef SIN6_LEN
169 addr.sin6_len = sizeof (struct sockaddr_in6);
170#endif /* SIN6_LEN */
171 addr.sin6_port = htons (IPPROTO_ICMPV6);
172 memcpy (&addr.sin6_addr, all_nodes_addr, sizeof (struct in6_addr));
173
174 /* Fetch interface information. */
175 zif = ifp->info;
176
177 /* Make router advertisement message. */
178 rtadv = (struct nd_router_advert *) buf;
179
180 rtadv->nd_ra_type = ND_ROUTER_ADVERT;
181 rtadv->nd_ra_code = 0;
182 rtadv->nd_ra_cksum = 0;
183
184 rtadv->nd_ra_curhoplimit = 64;
185 rtadv->nd_ra_flags_reserved = 0;
186 if (zif->rtadv.AdvManagedFlag)
187 rtadv->nd_ra_flags_reserved |= ND_RA_FLAG_MANAGED;
188 if (zif->rtadv.AdvOtherConfigFlag)
189 rtadv->nd_ra_flags_reserved |= ND_RA_FLAG_OTHER;
190 rtadv->nd_ra_router_lifetime = htons (zif->rtadv.AdvDefaultLifetime);
191 rtadv->nd_ra_reachable = htonl (zif->rtadv.AdvReachableTime);
192 rtadv->nd_ra_retransmit = htonl (0);
193
194 len = sizeof (struct nd_router_advert);
195
196 /* Fill in prefix. */
197 for (node = listhead (zif->rtadv.AdvPrefixList); node; node = nextnode (node))
198 {
199 struct nd_opt_prefix_info *pinfo;
200 struct rtadv_prefix *rprefix;
201
202 rprefix = getdata (node);
203
204 pinfo = (struct nd_opt_prefix_info *) (buf + len);
205
206 pinfo->nd_opt_pi_type = ND_OPT_PREFIX_INFORMATION;
207 pinfo->nd_opt_pi_len = 4;
208 pinfo->nd_opt_pi_prefix_len = rprefix->prefix.prefixlen;
209
210 pinfo->nd_opt_pi_flags_reserved = 0;
211 if (rprefix->AdvOnLinkFlag)
212 pinfo->nd_opt_pi_flags_reserved |= ND_OPT_PI_FLAG_ONLINK;
213 if (rprefix->AdvAutonomousFlag)
214 pinfo->nd_opt_pi_flags_reserved |= ND_OPT_PI_FLAG_AUTO;
215
216 pinfo->nd_opt_pi_valid_time = htonl (rprefix->AdvValidLifetime);
217 pinfo->nd_opt_pi_preferred_time = htonl (rprefix->AdvPreferredLifetime);
218 pinfo->nd_opt_pi_reserved2 = 0;
219
220 memcpy (&pinfo->nd_opt_pi_prefix, &rprefix->prefix.u.prefix6,
221 sizeof (struct in6_addr));
222
223#ifdef DEBUG
224 {
225 u_char buf[INET6_ADDRSTRLEN];
226
227 zlog_info ("DEBUG %s", inet_ntop (AF_INET6, &pinfo->nd_opt_pi_prefix, buf, INET6_ADDRSTRLEN));
228
229 }
230#endif /* DEBUG */
231
232 len += sizeof (struct nd_opt_prefix_info);
233 }
234
235 /* Hardware address. */
236#ifdef HAVE_SOCKADDR_DL
237 sdl = &ifp->sdl;
238 if (sdl != NULL && sdl->sdl_alen != 0)
239 {
240 buf[len++] = ND_OPT_SOURCE_LINKADDR;
241 buf[len++] = (sdl->sdl_alen + 2) >> 3;
242
243 memcpy (buf + len, LLADDR (sdl), sdl->sdl_alen);
244 len += sdl->sdl_alen;
245 }
246#else
247 if (ifp->hw_addr_len != 0)
248 {
249 buf[len++] = ND_OPT_SOURCE_LINKADDR;
250 buf[len++] = (ifp->hw_addr_len + 2) >> 3;
251
252 memcpy (buf + len, ifp->hw_addr, ifp->hw_addr_len);
253 len += ifp->hw_addr_len;
254 }
255#endif /* HAVE_SOCKADDR_DL */
256
257 msg.msg_name = (void *) &addr;
258 msg.msg_namelen = sizeof (struct sockaddr_in6);
259 msg.msg_iov = &iov;
260 msg.msg_iovlen = 1;
261 msg.msg_control = (void *) adata;
262 msg.msg_controllen = sizeof adata;
263 iov.iov_base = buf;
264 iov.iov_len = len;
265
266 cmsgptr = (struct cmsghdr *)adata;
267 cmsgptr->cmsg_len = sizeof adata;
268 cmsgptr->cmsg_level = IPPROTO_IPV6;
269 cmsgptr->cmsg_type = IPV6_PKTINFO;
270 pkt = (struct in6_pktinfo *) CMSG_DATA (cmsgptr);
271 memset (&pkt->ipi6_addr, 0, sizeof (struct in6_addr));
272 pkt->ipi6_ifindex = ifp->ifindex;
273
274 ret = sendmsg (sock, &msg, 0);
275 if (ret <0)
276 perror ("sendmsg");
277}
278
279int
280rtadv_timer (struct thread *thread)
281{
282 listnode node;
283 struct interface *ifp;
284 struct zebra_if *zif;
285
286 rtadv->ra_timer = NULL;
287 rtadv_event (RTADV_TIMER, 1);
288
289 for (node = listhead (iflist); node; nextnode (node))
290 {
291 ifp = getdata (node);
292
293 if (if_is_loopback (ifp))
294 continue;
295
296 zif = ifp->info;
297
298 if (zif->rtadv.AdvSendAdvertisements)
299 if (--zif->rtadv.AdvIntervalTimer <= 0)
300 {
301 zif->rtadv.AdvIntervalTimer = zif->rtadv.MaxRtrAdvInterval;
302 rtadv_send_packet (rtadv->sock, ifp);
303 }
304 }
305 return 0;
306}
307
308void
309rtadv_process_solicit (struct interface *ifp)
310{
311 zlog_info ("Router solicitation received on %s", ifp->name);
312
313 rtadv_send_packet (rtadv->sock, ifp);
314}
315
316void
317rtadv_process_advert ()
318{
319 zlog_info ("Router advertisement received");
320}
321
322void
323rtadv_process_packet (u_char *buf, int len, unsigned int ifindex, int hoplimit)
324{
325 struct icmp6_hdr *icmph;
326 struct interface *ifp;
327 struct zebra_if *zif;
328
329 /* Interface search. */
330 ifp = if_lookup_by_index (ifindex);
331 if (ifp == NULL)
332 {
333 zlog_warn ("Unknown interface index: %d", ifindex);
334 return;
335 }
336
337 if (if_is_loopback (ifp))
338 return;
339
340 /* Check interface configuration. */
341 zif = ifp->info;
342 if (! zif->rtadv.AdvSendAdvertisements)
343 return;
344
345 /* ICMP message length check. */
346 if (len < sizeof (struct icmp6_hdr))
347 {
348 zlog_warn ("Invalid ICMPV6 packet length: %d", len);
349 return;
350 }
351
352 icmph = (struct icmp6_hdr *) buf;
353
354 /* ICMP message type check. */
355 if (icmph->icmp6_type != ND_ROUTER_SOLICIT &&
356 icmph->icmp6_type != ND_ROUTER_ADVERT)
357 {
358 zlog_warn ("Unwanted ICMPV6 message type: %d", icmph->icmp6_type);
359 return;
360 }
361
362 /* Hoplimit check. */
363 if (hoplimit >= 0 && hoplimit != 255)
364 {
365 zlog_warn ("Invalid hoplimit %d for router advertisement ICMP packet",
366 hoplimit);
367 return;
368 }
369
370 /* Check ICMP message type. */
371 if (icmph->icmp6_type == ND_ROUTER_SOLICIT)
372 rtadv_process_solicit (ifp);
373 else if (icmph->icmp6_type == ND_ROUTER_ADVERT)
374 rtadv_process_advert ();
375
376 return;
377}
378
379int
380rtadv_read (struct thread *thread)
381{
382 int sock;
383 int len;
384 u_char buf[RTADV_MSG_SIZE];
385 struct sockaddr_in6 from;
386 unsigned int ifindex;
387 int hoplimit = -1;
388
389 sock = THREAD_FD (thread);
390 rtadv->ra_read = NULL;
391
392 /* Register myself. */
393 rtadv_event (RTADV_READ, sock);
394
395 len = rtadv_recv_packet (sock, buf, BUFSIZ, &from, &ifindex, &hoplimit);
396
397 if (len < 0)
398 {
399 zlog_warn ("router solicitation recv failed: %s.", strerror (errno));
400 return len;
401 }
402
403 rtadv_process_packet (buf, len, ifindex, hoplimit);
404
405 return 0;
406}
407
408int
409rtadv_make_socket (void)
410{
411 int sock;
412 int ret;
413 struct icmp6_filter filter;
414
pauledd7c242003-06-04 13:59:38 +0000415 if ( zserv_privs.change (ZPRIVS_RAISE) )
416 zlog_err ("rtadv_make_socket: could not raise privs, %s",
417 strerror (errno) );
418
paul718e3742002-12-13 20:15:29 +0000419 sock = socket (AF_INET6, SOCK_RAW, IPPROTO_ICMPV6);
420
pauledd7c242003-06-04 13:59:38 +0000421 if ( zserv_privs.change (ZPRIVS_LOWER) )
422 zlog_err ("rtadv_make_socket: could not lower privs, %s",
423 strerror (errno) );
424
paul718e3742002-12-13 20:15:29 +0000425 /* When we can't make ICMPV6 socket simply back. Router
426 advertisement feature will not be supported. */
427 if (sock < 0)
428 return -1;
429
430 ret = setsockopt_ipv6_pktinfo (sock, 1);
431 if (ret < 0)
432 return ret;
paul718e3742002-12-13 20:15:29 +0000433 ret = setsockopt_ipv6_multicast_loop (sock, 0);
434 if (ret < 0)
435 return ret;
436 ret = setsockopt_ipv6_unicast_hops (sock, 255);
437 if (ret < 0)
438 return ret;
439 ret = setsockopt_ipv6_multicast_hops (sock, 255);
440 if (ret < 0)
441 return ret;
442 ret = setsockopt_ipv6_hoplimit (sock, 1);
443 if (ret < 0)
444 return ret;
445
446 ICMP6_FILTER_SETBLOCKALL(&filter);
447 ICMP6_FILTER_SETPASS (ND_ROUTER_SOLICIT, &filter);
448 ICMP6_FILTER_SETPASS (ND_ROUTER_ADVERT, &filter);
449
450 ret = setsockopt (sock, IPPROTO_ICMPV6, ICMP6_FILTER, &filter,
451 sizeof (struct icmp6_filter));
452 if (ret < 0)
453 {
454 zlog_info ("ICMP6_FILTER set fail: %s", strerror (errno));
455 return ret;
456 }
457
458 return sock;
459}
460
461struct rtadv_prefix *
462rtadv_prefix_new ()
463{
464 struct rtadv_prefix *new;
465
466 new = XMALLOC (MTYPE_RTADV_PREFIX, sizeof (struct rtadv_prefix));
467 memset (new, 0, sizeof (struct rtadv_prefix));
468
469 return new;
470}
471
472void
473rtadv_prefix_free (struct rtadv_prefix *rtadv_prefix)
474{
475 XFREE (MTYPE_RTADV_PREFIX, rtadv_prefix);
476}
477
478struct rtadv_prefix *
479rtadv_prefix_lookup (list rplist, struct prefix *p)
480{
481 listnode node;
482 struct rtadv_prefix *rprefix;
483
484 for (node = listhead (rplist); node; node = nextnode (node))
485 {
486 rprefix = getdata (node);
487 if (prefix_same (&rprefix->prefix, p))
488 return rprefix;
489 }
490 return NULL;
491}
492
493struct rtadv_prefix *
494rtadv_prefix_get (list rplist, struct prefix *p)
495{
496 struct rtadv_prefix *rprefix;
497
498 rprefix = rtadv_prefix_lookup (rplist, p);
499 if (rprefix)
500 return rprefix;
501
502 rprefix = rtadv_prefix_new ();
503 memcpy (&rprefix->prefix, p, sizeof (struct prefix));
504 listnode_add (rplist, rprefix);
505
506 return rprefix;
507}
508
509void
510rtadv_prefix_set (struct zebra_if *zif, struct rtadv_prefix *rp)
511{
512 struct rtadv_prefix *rprefix;
513
514 rprefix = rtadv_prefix_get (zif->rtadv.AdvPrefixList, &rp->prefix);
515
516 /* Set parameters. */
517 rprefix->AdvValidLifetime = rp->AdvValidLifetime;
518 rprefix->AdvPreferredLifetime = rp->AdvPreferredLifetime;
519 rprefix->AdvOnLinkFlag = rp->AdvOnLinkFlag;
520 rprefix->AdvAutonomousFlag = rp->AdvAutonomousFlag;
521}
522
523int
524rtadv_prefix_reset (struct zebra_if *zif, struct rtadv_prefix *rp)
525{
526 struct rtadv_prefix *rprefix;
527
528 rprefix = rtadv_prefix_lookup (zif->rtadv.AdvPrefixList, &rp->prefix);
529 if (rprefix != NULL)
530 {
531 listnode_delete (zif->rtadv.AdvPrefixList, (void *) rprefix);
532 rtadv_prefix_free (rprefix);
533 return 1;
534 }
535 else
536 return 0;
537}
538
539DEFUN (ipv6_nd_suppress_ra,
540 ipv6_nd_suppress_ra_cmd,
541 "ipv6 nd suppress-ra",
542 IP_STR
543 "Neighbor discovery\n"
544 "Suppress Router Advertisement\n")
545{
546 struct interface *ifp;
547 struct zebra_if *zif;
548
549 ifp = vty->index;
550 zif = ifp->info;
551
552 if (if_is_loopback (ifp))
553 {
554 vty_out (vty, "Invalid interface%s", VTY_NEWLINE);
555 return CMD_WARNING;
556 }
557
558 if (zif->rtadv.AdvSendAdvertisements)
559 {
560 zif->rtadv.AdvSendAdvertisements = 0;
561 zif->rtadv.AdvIntervalTimer = 0;
562 rtadv->adv_if_count--;
563
564 if_leave_all_router (rtadv->sock, ifp);
565
566 if (rtadv->adv_if_count == 0)
567 rtadv_event (RTADV_STOP, 0);
568 }
569
570 return CMD_SUCCESS;
571}
572
573ALIAS (ipv6_nd_suppress_ra,
574 no_ipv6_nd_send_ra_cmd,
575 "no ipv6 nd send-ra",
576 NO_STR
577 IP_STR
578 "Neighbor discovery\n"
579 "Send Router Advertisement\n")
580
581DEFUN (no_ipv6_nd_suppress_ra,
582 no_ipv6_nd_suppress_ra_cmd,
583 "no ipv6 nd suppress-ra",
584 NO_STR
585 IP_STR
586 "Neighbor discovery\n"
587 "Suppress Router Advertisement\n")
588{
589 struct interface *ifp;
590 struct zebra_if *zif;
591
592 ifp = vty->index;
593 zif = ifp->info;
594
595 if (if_is_loopback (ifp))
596 {
597 vty_out (vty, "Invalid interface%s", VTY_NEWLINE);
598 return CMD_WARNING;
599 }
600
601 if (! zif->rtadv.AdvSendAdvertisements)
602 {
603 zif->rtadv.AdvSendAdvertisements = 1;
604 zif->rtadv.AdvIntervalTimer = 0;
605 rtadv->adv_if_count++;
606
607 if_join_all_router (rtadv->sock, ifp);
608
609 if (rtadv->adv_if_count == 1)
610 rtadv_event (RTADV_START, rtadv->sock);
611 }
612
613 return CMD_SUCCESS;
614}
615
616ALIAS (no_ipv6_nd_suppress_ra,
617 ipv6_nd_send_ra_cmd,
618 "ipv6 nd send-ra",
619 IP_STR
620 "Neighbor discovery\n"
621 "Send Router Advertisement\n")
622
623DEFUN (ipv6_nd_ra_interval,
624 ipv6_nd_ra_interval_cmd,
625 "ipv6 nd ra-interval SECONDS",
626 IP_STR
627 "Neighbor discovery\n"
628 "Router Advertisement interval\n"
629 "Router Advertisement interval in seconds\n")
630{
631 int interval;
632 struct interface *ifp;
633 struct zebra_if *zif;
634
635 ifp = (struct interface *) vty->index;
636 zif = ifp->info;
637
638 interval = atoi (argv[0]);
639
640 if (interval < 0)
641 {
642 vty_out (vty, "Invalid Router Advertisement Interval%s", VTY_NEWLINE);
643 return CMD_WARNING;
644 }
645
646 zif->rtadv.MaxRtrAdvInterval = interval;
647 zif->rtadv.MinRtrAdvInterval = 0.33 * interval;
648 zif->rtadv.AdvIntervalTimer = 0;
649
650 return CMD_SUCCESS;
651}
652
653DEFUN (no_ipv6_nd_ra_interval,
654 no_ipv6_nd_ra_interval_cmd,
655 "no ipv6 nd ra-interval",
656 NO_STR
657 IP_STR
658 "Neighbor discovery\n"
659 "Router Advertisement interval\n")
660{
661 struct interface *ifp;
662 struct zebra_if *zif;
663
664 ifp = (struct interface *) vty->index;
665 zif = ifp->info;
666
667 zif->rtadv.MaxRtrAdvInterval = RTADV_MAX_RTR_ADV_INTERVAL;
668 zif->rtadv.MinRtrAdvInterval = RTADV_MIN_RTR_ADV_INTERVAL;
669 zif->rtadv.AdvIntervalTimer = zif->rtadv.MaxRtrAdvInterval;
670
671 return CMD_SUCCESS;
672}
673
674DEFUN (ipv6_nd_ra_lifetime,
675 ipv6_nd_ra_lifetime_cmd,
676 "ipv6 nd ra-lifetime SECONDS",
677 IP_STR
678 "Neighbor discovery\n"
679 "Router lifetime\n"
680 "Router lifetime in seconds\n")
681{
682 int lifetime;
683 struct interface *ifp;
684 struct zebra_if *zif;
685
686 ifp = (struct interface *) vty->index;
687 zif = ifp->info;
688
689 lifetime = atoi (argv[0]);
690
691 if (lifetime < 0 || lifetime > 0xffff)
692 {
693 vty_out (vty, "Invalid Router Lifetime%s", VTY_NEWLINE);
694 return CMD_WARNING;
695 }
696
697 zif->rtadv.AdvDefaultLifetime = lifetime;
698
699 return CMD_SUCCESS;
700}
701
702DEFUN (no_ipv6_nd_ra_lifetime,
703 no_ipv6_nd_ra_lifetime_cmd,
704 "no ipv6 nd ra-lifetime",
705 NO_STR
706 IP_STR
707 "Neighbor discovery\n"
708 "Router lifetime\n")
709{
710 struct interface *ifp;
711 struct zebra_if *zif;
712
713 ifp = (struct interface *) vty->index;
714 zif = ifp->info;
715
716 zif->rtadv.AdvDefaultLifetime = RTADV_ADV_DEFAULT_LIFETIME;
717
718 return CMD_SUCCESS;
719}
720
721DEFUN (ipv6_nd_reachable_time,
722 ipv6_nd_reachable_time_cmd,
723 "ipv6 nd reachable-time MILLISECONDS",
724 IP_STR
725 "Neighbor discovery\n"
726 "Reachable time\n"
727 "Reachable time in milliseconds\n")
728{
729 u_int32_t rtime;
730 struct interface *ifp;
731 struct zebra_if *zif;
732
733 ifp = (struct interface *) vty->index;
734 zif = ifp->info;
735
736 rtime = (u_int32_t) atol (argv[0]);
737
738 if (rtime > RTADV_MAX_REACHABLE_TIME)
739 {
740 vty_out (vty, "Invalid Reachable time%s", VTY_NEWLINE);
741 return CMD_WARNING;
742 }
743
744 zif->rtadv.AdvReachableTime = rtime;
745
746 return CMD_SUCCESS;
747}
748
749DEFUN (no_ipv6_nd_reachable_time,
750 no_ipv6_nd_reachable_time_cmd,
751 "no ipv6 nd reachable-time",
752 NO_STR
753 IP_STR
754 "Neighbor discovery\n"
755 "Reachable time\n")
756{
757 struct interface *ifp;
758 struct zebra_if *zif;
759
760 ifp = (struct interface *) vty->index;
761 zif = ifp->info;
762
763 zif->rtadv.AdvReachableTime = 0;
764
765 return CMD_SUCCESS;
766}
767
768DEFUN (ipv6_nd_managed_config_flag,
769 ipv6_nd_managed_config_flag_cmd,
770 "ipv6 nd managed-config-flag",
771 IP_STR
772 "Neighbor discovery\n"
773 "Managed address configuration flag\n")
774{
775 struct interface *ifp;
776 struct zebra_if *zif;
777
778 ifp = (struct interface *) vty->index;
779 zif = ifp->info;
780
781 zif->rtadv.AdvManagedFlag = 1;
782
783 return CMD_SUCCESS;
784}
785
786DEFUN (no_ipv6_nd_managed_config_flag,
787 no_ipv6_nd_managed_config_flag_cmd,
788 "no ipv6 nd managed-config-flag",
789 NO_STR
790 IP_STR
791 "Neighbor discovery\n"
792 "Managed address configuration flag\n")
793{
794 struct interface *ifp;
795 struct zebra_if *zif;
796
797 ifp = (struct interface *) vty->index;
798 zif = ifp->info;
799
800 zif->rtadv.AdvManagedFlag = 0;
801
802 return CMD_SUCCESS;
803}
804
805DEFUN (ipv6_nd_other_config_flag,
806 ipv6_nd_other_config_flag_cmd,
807 "ipv6 nd other-config-flag",
808 IP_STR
809 "Neighbor discovery\n"
810 "Other statefull configuration flag\n")
811{
812 struct interface *ifp;
813 struct zebra_if *zif;
814
815 ifp = (struct interface *) vty->index;
816 zif = ifp->info;
817
818 zif->rtadv.AdvOtherConfigFlag = 1;
819
820 return CMD_SUCCESS;
821}
822
823DEFUN (no_ipv6_nd_other_config_flag,
824 no_ipv6_nd_other_config_flag_cmd,
825 "no ipv6 nd other-config-flag",
826 NO_STR
827 IP_STR
828 "Neighbor discovery\n"
829 "Other statefull configuration flag\n")
830{
831 struct interface *ifp;
832 struct zebra_if *zif;
833
834 ifp = (struct interface *) vty->index;
835 zif = ifp->info;
836
837 zif->rtadv.AdvOtherConfigFlag = 0;
838
839 return CMD_SUCCESS;
840}
841
842DEFUN (ipv6_nd_prefix_advertisement,
843 ipv6_nd_prefix_advertisement_cmd,
844 "ipv6 nd prefix-advertisement IPV6PREFIX VALID PREFERRED [onlink] [autoconfig]",
845 IP_STR
846 "Neighbor discovery\n"
847 "Prefix information\n"
848 "IPv6 prefix\n"
849 "Valid lifetime in seconds\n"
850 "Preferred lifetime in seconds\n"
851 "On link flag\n"
852 "Autonomous address-configuration flag\n")
853{
854 int i;
855 int ret;
856 struct interface *ifp;
857 struct zebra_if *zebra_if;
858 struct rtadv_prefix rp;
859
860 ifp = (struct interface *) vty->index;
861 zebra_if = ifp->info;
862
863 ret = str2prefix_ipv6 (argv[0], (struct prefix_ipv6 *) &rp.prefix);
864 if (!ret)
865 {
866 vty_out (vty, "Malformed IPv6 prefix%s", VTY_NEWLINE);
867 return CMD_WARNING;
868 }
869
870 if (argc == 1)
871 {
872 rp.AdvValidLifetime = RTADV_VALID_LIFETIME;
873 rp.AdvPreferredLifetime = RTADV_PREFERRED_LIFETIME;
874 rp.AdvOnLinkFlag = 1;
875 rp.AdvAutonomousFlag = 1;
876 }
877 else
878 {
879 rp.AdvValidLifetime = (u_int32_t) atol (argv[1]);
880 rp.AdvPreferredLifetime = (u_int32_t) atol (argv[2]);
881 if (rp.AdvPreferredLifetime > rp.AdvValidLifetime)
882 {
883 vty_out (vty, "Invalid preferred lifetime%s", VTY_NEWLINE);
884 return CMD_WARNING;
885 }
886
887 rp.AdvOnLinkFlag = 0;
888 rp.AdvAutonomousFlag = 0;
889 for (i = 3; i < argc; i++)
890 {
891 if (! strcmp (argv[i], "onlink"))
892 rp.AdvOnLinkFlag = 1;
893 else if (! strcmp (argv[i], "autoconfig"))
894 rp.AdvAutonomousFlag = 1;
895 }
896 }
897
898 rtadv_prefix_set (zebra_if, &rp);
899
900 return CMD_SUCCESS;
901}
902
903ALIAS (ipv6_nd_prefix_advertisement,
904 ipv6_nd_prefix_advertisement_no_val_cmd,
905 "ipv6 nd prefix-advertisement IPV6PREFIX",
906 IP_STR
907 "Neighbor discovery\n"
908 "Prefix information\n"
909 "IPv6 prefix\n")
910
911DEFUN (no_ipv6_nd_prefix_advertisement,
912 no_ipv6_nd_prefix_advertisement_cmd,
913 "no ipv6 nd prefix-advertisement IPV6PREFIX",
914 NO_STR
915 IP_STR
916 "Neighbor discovery\n"
917 "Prefix information\n"
918 "IPv6 prefix\n")
919{
920 int ret;
921 struct interface *ifp;
922 struct zebra_if *zebra_if;
923 struct rtadv_prefix rp;
924
925 ifp = (struct interface *) vty->index;
926 zebra_if = ifp->info;
927
928 ret = str2prefix_ipv6 (argv[0], (struct prefix_ipv6 *) &rp.prefix);
929 if (!ret)
930 {
931 vty_out (vty, "Malformed IPv6 prefix%s", VTY_NEWLINE);
932 return CMD_WARNING;
933 }
934
935 ret = rtadv_prefix_reset (zebra_if, &rp);
936 if (!ret)
937 {
938 vty_out (vty, "Non-exist IPv6 prefix%s", VTY_NEWLINE);
939 return CMD_WARNING;
940 }
941
942 return CMD_SUCCESS;
943}
944
945/* Write configuration about router advertisement. */
946void
947rtadv_config_write (struct vty *vty, struct interface *ifp)
948{
949 struct zebra_if *zif;
950 listnode node;
951 struct rtadv_prefix *rprefix;
952 u_char buf[INET6_ADDRSTRLEN];
953
954 if (! rtadv)
955 return;
956
957 zif = ifp->info;
958
959 if (! if_is_loopback (ifp))
960 {
961 if (zif->rtadv.AdvSendAdvertisements)
962 vty_out (vty, " no ipv6 nd suppress-ra%s", VTY_NEWLINE);
963 else
964 vty_out (vty, " ipv6 nd suppress-ra%s", VTY_NEWLINE);
965 }
966
967 if (zif->rtadv.MaxRtrAdvInterval != RTADV_MAX_RTR_ADV_INTERVAL)
968 vty_out (vty, " ipv6 nd ra-interval %d%s", zif->rtadv.MaxRtrAdvInterval,
969 VTY_NEWLINE);
970
971 if (zif->rtadv.AdvDefaultLifetime != RTADV_ADV_DEFAULT_LIFETIME)
972 vty_out (vty, " ipv6 nd ra-lifetime %d%s", zif->rtadv.AdvDefaultLifetime,
973 VTY_NEWLINE);
974
975 if (zif->rtadv.AdvReachableTime)
976 vty_out (vty, " ipv6 nd reachable-time %d%s", zif->rtadv.AdvReachableTime,
977 VTY_NEWLINE);
978
979 if (zif->rtadv.AdvManagedFlag)
980 vty_out (vty, " ipv6 nd managed-config-flag%s", VTY_NEWLINE);
981
982 if (zif->rtadv.AdvOtherConfigFlag)
983 vty_out (vty, " ipv6 nd other-config-flag%s", VTY_NEWLINE);
984
985 for (node = listhead(zif->rtadv.AdvPrefixList); node; node = nextnode (node))
986 {
987 rprefix = getdata (node);
988 vty_out (vty, " ipv6 nd prefix-advertisement %s/%d %d %d",
989 inet_ntop (AF_INET6, &rprefix->prefix.u.prefix6,
990 buf, INET6_ADDRSTRLEN),
991 rprefix->prefix.prefixlen,
992 rprefix->AdvValidLifetime,
993 rprefix->AdvPreferredLifetime);
994 if (rprefix->AdvOnLinkFlag)
995 vty_out (vty, " onlink");
996 if (rprefix->AdvAutonomousFlag)
997 vty_out (vty, " autoconfig");
998 vty_out (vty, "%s", VTY_NEWLINE);
999 }
1000}
1001
1002extern struct thread_master *master;
1003
1004void
1005rtadv_event (enum rtadv_event event, int val)
1006{
1007 switch (event)
1008 {
1009 case RTADV_START:
1010 if (! rtadv->ra_read)
1011 rtadv->ra_read = thread_add_read (master, rtadv_read, NULL, val);
1012 if (! rtadv->ra_timer)
1013 rtadv->ra_timer = thread_add_event (master, rtadv_timer, NULL, 0);
1014 break;
1015 case RTADV_STOP:
1016 if (rtadv->ra_timer)
1017 {
1018 thread_cancel (rtadv->ra_timer);
1019 rtadv->ra_timer = NULL;
1020 }
1021 if (rtadv->ra_read)
1022 {
1023 thread_cancel (rtadv->ra_read);
1024 rtadv->ra_read = NULL;
1025 }
1026 break;
1027 case RTADV_TIMER:
1028 if (! rtadv->ra_timer)
1029 rtadv->ra_timer = thread_add_timer (master, rtadv_timer, NULL, val);
1030 break;
1031 case RTADV_READ:
1032 if (! rtadv->ra_read)
1033 rtadv->ra_read = thread_add_read (master, rtadv_read, NULL, val);
1034 break;
1035 default:
1036 break;
1037 }
1038 return;
1039}
1040
1041void
1042rtadv_init ()
1043{
1044 int sock;
1045
1046 sock = rtadv_make_socket ();
1047 if (sock < 0)
1048 return;
1049
1050 rtadv = rtadv_new ();
1051 rtadv->sock = sock;
1052
1053 install_element (INTERFACE_NODE, &ipv6_nd_suppress_ra_cmd);
1054 install_element (INTERFACE_NODE, &no_ipv6_nd_suppress_ra_cmd);
1055 install_element (INTERFACE_NODE, &ipv6_nd_send_ra_cmd);
1056 install_element (INTERFACE_NODE, &no_ipv6_nd_send_ra_cmd);
1057 install_element (INTERFACE_NODE, &ipv6_nd_ra_interval_cmd);
1058 install_element (INTERFACE_NODE, &no_ipv6_nd_ra_interval_cmd);
1059 install_element (INTERFACE_NODE, &ipv6_nd_ra_lifetime_cmd);
1060 install_element (INTERFACE_NODE, &no_ipv6_nd_ra_lifetime_cmd);
1061 install_element (INTERFACE_NODE, &ipv6_nd_reachable_time_cmd);
1062 install_element (INTERFACE_NODE, &no_ipv6_nd_reachable_time_cmd);
1063 install_element (INTERFACE_NODE, &ipv6_nd_managed_config_flag_cmd);
1064 install_element (INTERFACE_NODE, &no_ipv6_nd_managed_config_flag_cmd);
1065 install_element (INTERFACE_NODE, &ipv6_nd_other_config_flag_cmd);
1066 install_element (INTERFACE_NODE, &no_ipv6_nd_other_config_flag_cmd);
1067 install_element (INTERFACE_NODE, &ipv6_nd_prefix_advertisement_cmd);
1068 install_element (INTERFACE_NODE, &ipv6_nd_prefix_advertisement_no_val_cmd);
1069 install_element (INTERFACE_NODE, &no_ipv6_nd_prefix_advertisement_cmd);
1070}
1071
1072int
1073if_join_all_router (int sock, struct interface *ifp)
1074{
1075 int ret;
1076
1077 struct ipv6_mreq mreq;
1078
1079 memset (&mreq, 0, sizeof (struct ipv6_mreq));
1080 inet_pton (AF_INET6, ALLROUTER, &mreq.ipv6mr_multiaddr);
1081 mreq.ipv6mr_interface = ifp->ifindex;
1082
1083 ret = setsockopt (sock, IPPROTO_IPV6, IPV6_JOIN_GROUP,
1084 (char *) &mreq, sizeof mreq);
1085 if (ret < 0)
1086 zlog_warn ("can't setsockopt IPV6_JOIN_GROUP: %s", strerror (errno));
1087
1088 zlog_info ("rtadv: %s join to all-routers multicast group", ifp->name);
1089
1090 return 0;
1091}
1092
1093int
1094if_leave_all_router (int sock, struct interface *ifp)
1095{
1096 int ret;
1097
1098 struct ipv6_mreq mreq;
1099
1100 memset (&mreq, 0, sizeof (struct ipv6_mreq));
1101 inet_pton (AF_INET6, ALLROUTER, &mreq.ipv6mr_multiaddr);
1102 mreq.ipv6mr_interface = ifp->ifindex;
1103
1104 ret = setsockopt (sock, IPPROTO_IPV6, IPV6_LEAVE_GROUP,
1105 (char *) &mreq, sizeof mreq);
1106 if (ret < 0)
1107 zlog_warn ("can't setsockopt IPV6_LEAVE_GROUP: %s", strerror (errno));
1108
1109 zlog_info ("rtadv: %s leave from all-routers multicast group", ifp->name);
1110
1111 return 0;
1112}
1113
1114#else
1115void
1116rtadv_init ()
1117{
1118 /* Empty.*/;
1119}
1120#endif /* RTADV && HAVE_IPV6 */