blob: 9dcee8ea9df6dafe8ce0a3df497f562b1b903669 [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;
433 ret = setsockopt_ipv6_checksum (sock, 2);
434 if (ret < 0)
435 return ret;
436 ret = setsockopt_ipv6_multicast_loop (sock, 0);
437 if (ret < 0)
438 return ret;
439 ret = setsockopt_ipv6_unicast_hops (sock, 255);
440 if (ret < 0)
441 return ret;
442 ret = setsockopt_ipv6_multicast_hops (sock, 255);
443 if (ret < 0)
444 return ret;
445 ret = setsockopt_ipv6_hoplimit (sock, 1);
446 if (ret < 0)
447 return ret;
448
449 ICMP6_FILTER_SETBLOCKALL(&filter);
450 ICMP6_FILTER_SETPASS (ND_ROUTER_SOLICIT, &filter);
451 ICMP6_FILTER_SETPASS (ND_ROUTER_ADVERT, &filter);
452
453 ret = setsockopt (sock, IPPROTO_ICMPV6, ICMP6_FILTER, &filter,
454 sizeof (struct icmp6_filter));
455 if (ret < 0)
456 {
457 zlog_info ("ICMP6_FILTER set fail: %s", strerror (errno));
458 return ret;
459 }
460
461 return sock;
462}
463
464struct rtadv_prefix *
465rtadv_prefix_new ()
466{
467 struct rtadv_prefix *new;
468
469 new = XMALLOC (MTYPE_RTADV_PREFIX, sizeof (struct rtadv_prefix));
470 memset (new, 0, sizeof (struct rtadv_prefix));
471
472 return new;
473}
474
475void
476rtadv_prefix_free (struct rtadv_prefix *rtadv_prefix)
477{
478 XFREE (MTYPE_RTADV_PREFIX, rtadv_prefix);
479}
480
481struct rtadv_prefix *
482rtadv_prefix_lookup (list rplist, struct prefix *p)
483{
484 listnode node;
485 struct rtadv_prefix *rprefix;
486
487 for (node = listhead (rplist); node; node = nextnode (node))
488 {
489 rprefix = getdata (node);
490 if (prefix_same (&rprefix->prefix, p))
491 return rprefix;
492 }
493 return NULL;
494}
495
496struct rtadv_prefix *
497rtadv_prefix_get (list rplist, struct prefix *p)
498{
499 struct rtadv_prefix *rprefix;
500
501 rprefix = rtadv_prefix_lookup (rplist, p);
502 if (rprefix)
503 return rprefix;
504
505 rprefix = rtadv_prefix_new ();
506 memcpy (&rprefix->prefix, p, sizeof (struct prefix));
507 listnode_add (rplist, rprefix);
508
509 return rprefix;
510}
511
512void
513rtadv_prefix_set (struct zebra_if *zif, struct rtadv_prefix *rp)
514{
515 struct rtadv_prefix *rprefix;
516
517 rprefix = rtadv_prefix_get (zif->rtadv.AdvPrefixList, &rp->prefix);
518
519 /* Set parameters. */
520 rprefix->AdvValidLifetime = rp->AdvValidLifetime;
521 rprefix->AdvPreferredLifetime = rp->AdvPreferredLifetime;
522 rprefix->AdvOnLinkFlag = rp->AdvOnLinkFlag;
523 rprefix->AdvAutonomousFlag = rp->AdvAutonomousFlag;
524}
525
526int
527rtadv_prefix_reset (struct zebra_if *zif, struct rtadv_prefix *rp)
528{
529 struct rtadv_prefix *rprefix;
530
531 rprefix = rtadv_prefix_lookup (zif->rtadv.AdvPrefixList, &rp->prefix);
532 if (rprefix != NULL)
533 {
534 listnode_delete (zif->rtadv.AdvPrefixList, (void *) rprefix);
535 rtadv_prefix_free (rprefix);
536 return 1;
537 }
538 else
539 return 0;
540}
541
542DEFUN (ipv6_nd_suppress_ra,
543 ipv6_nd_suppress_ra_cmd,
544 "ipv6 nd suppress-ra",
545 IP_STR
546 "Neighbor discovery\n"
547 "Suppress Router Advertisement\n")
548{
549 struct interface *ifp;
550 struct zebra_if *zif;
551
552 ifp = vty->index;
553 zif = ifp->info;
554
555 if (if_is_loopback (ifp))
556 {
557 vty_out (vty, "Invalid interface%s", VTY_NEWLINE);
558 return CMD_WARNING;
559 }
560
561 if (zif->rtadv.AdvSendAdvertisements)
562 {
563 zif->rtadv.AdvSendAdvertisements = 0;
564 zif->rtadv.AdvIntervalTimer = 0;
565 rtadv->adv_if_count--;
566
567 if_leave_all_router (rtadv->sock, ifp);
568
569 if (rtadv->adv_if_count == 0)
570 rtadv_event (RTADV_STOP, 0);
571 }
572
573 return CMD_SUCCESS;
574}
575
576ALIAS (ipv6_nd_suppress_ra,
577 no_ipv6_nd_send_ra_cmd,
578 "no ipv6 nd send-ra",
579 NO_STR
580 IP_STR
581 "Neighbor discovery\n"
582 "Send Router Advertisement\n")
583
584DEFUN (no_ipv6_nd_suppress_ra,
585 no_ipv6_nd_suppress_ra_cmd,
586 "no ipv6 nd suppress-ra",
587 NO_STR
588 IP_STR
589 "Neighbor discovery\n"
590 "Suppress Router Advertisement\n")
591{
592 struct interface *ifp;
593 struct zebra_if *zif;
594
595 ifp = vty->index;
596 zif = ifp->info;
597
598 if (if_is_loopback (ifp))
599 {
600 vty_out (vty, "Invalid interface%s", VTY_NEWLINE);
601 return CMD_WARNING;
602 }
603
604 if (! zif->rtadv.AdvSendAdvertisements)
605 {
606 zif->rtadv.AdvSendAdvertisements = 1;
607 zif->rtadv.AdvIntervalTimer = 0;
608 rtadv->adv_if_count++;
609
610 if_join_all_router (rtadv->sock, ifp);
611
612 if (rtadv->adv_if_count == 1)
613 rtadv_event (RTADV_START, rtadv->sock);
614 }
615
616 return CMD_SUCCESS;
617}
618
619ALIAS (no_ipv6_nd_suppress_ra,
620 ipv6_nd_send_ra_cmd,
621 "ipv6 nd send-ra",
622 IP_STR
623 "Neighbor discovery\n"
624 "Send Router Advertisement\n")
625
626DEFUN (ipv6_nd_ra_interval,
627 ipv6_nd_ra_interval_cmd,
628 "ipv6 nd ra-interval SECONDS",
629 IP_STR
630 "Neighbor discovery\n"
631 "Router Advertisement interval\n"
632 "Router Advertisement interval in seconds\n")
633{
634 int interval;
635 struct interface *ifp;
636 struct zebra_if *zif;
637
638 ifp = (struct interface *) vty->index;
639 zif = ifp->info;
640
641 interval = atoi (argv[0]);
642
643 if (interval < 0)
644 {
645 vty_out (vty, "Invalid Router Advertisement Interval%s", VTY_NEWLINE);
646 return CMD_WARNING;
647 }
648
649 zif->rtadv.MaxRtrAdvInterval = interval;
650 zif->rtadv.MinRtrAdvInterval = 0.33 * interval;
651 zif->rtadv.AdvIntervalTimer = 0;
652
653 return CMD_SUCCESS;
654}
655
656DEFUN (no_ipv6_nd_ra_interval,
657 no_ipv6_nd_ra_interval_cmd,
658 "no ipv6 nd ra-interval",
659 NO_STR
660 IP_STR
661 "Neighbor discovery\n"
662 "Router Advertisement interval\n")
663{
664 struct interface *ifp;
665 struct zebra_if *zif;
666
667 ifp = (struct interface *) vty->index;
668 zif = ifp->info;
669
670 zif->rtadv.MaxRtrAdvInterval = RTADV_MAX_RTR_ADV_INTERVAL;
671 zif->rtadv.MinRtrAdvInterval = RTADV_MIN_RTR_ADV_INTERVAL;
672 zif->rtadv.AdvIntervalTimer = zif->rtadv.MaxRtrAdvInterval;
673
674 return CMD_SUCCESS;
675}
676
677DEFUN (ipv6_nd_ra_lifetime,
678 ipv6_nd_ra_lifetime_cmd,
679 "ipv6 nd ra-lifetime SECONDS",
680 IP_STR
681 "Neighbor discovery\n"
682 "Router lifetime\n"
683 "Router lifetime in seconds\n")
684{
685 int lifetime;
686 struct interface *ifp;
687 struct zebra_if *zif;
688
689 ifp = (struct interface *) vty->index;
690 zif = ifp->info;
691
692 lifetime = atoi (argv[0]);
693
694 if (lifetime < 0 || lifetime > 0xffff)
695 {
696 vty_out (vty, "Invalid Router Lifetime%s", VTY_NEWLINE);
697 return CMD_WARNING;
698 }
699
700 zif->rtadv.AdvDefaultLifetime = lifetime;
701
702 return CMD_SUCCESS;
703}
704
705DEFUN (no_ipv6_nd_ra_lifetime,
706 no_ipv6_nd_ra_lifetime_cmd,
707 "no ipv6 nd ra-lifetime",
708 NO_STR
709 IP_STR
710 "Neighbor discovery\n"
711 "Router lifetime\n")
712{
713 struct interface *ifp;
714 struct zebra_if *zif;
715
716 ifp = (struct interface *) vty->index;
717 zif = ifp->info;
718
719 zif->rtadv.AdvDefaultLifetime = RTADV_ADV_DEFAULT_LIFETIME;
720
721 return CMD_SUCCESS;
722}
723
724DEFUN (ipv6_nd_reachable_time,
725 ipv6_nd_reachable_time_cmd,
726 "ipv6 nd reachable-time MILLISECONDS",
727 IP_STR
728 "Neighbor discovery\n"
729 "Reachable time\n"
730 "Reachable time in milliseconds\n")
731{
732 u_int32_t rtime;
733 struct interface *ifp;
734 struct zebra_if *zif;
735
736 ifp = (struct interface *) vty->index;
737 zif = ifp->info;
738
739 rtime = (u_int32_t) atol (argv[0]);
740
741 if (rtime > RTADV_MAX_REACHABLE_TIME)
742 {
743 vty_out (vty, "Invalid Reachable time%s", VTY_NEWLINE);
744 return CMD_WARNING;
745 }
746
747 zif->rtadv.AdvReachableTime = rtime;
748
749 return CMD_SUCCESS;
750}
751
752DEFUN (no_ipv6_nd_reachable_time,
753 no_ipv6_nd_reachable_time_cmd,
754 "no ipv6 nd reachable-time",
755 NO_STR
756 IP_STR
757 "Neighbor discovery\n"
758 "Reachable time\n")
759{
760 struct interface *ifp;
761 struct zebra_if *zif;
762
763 ifp = (struct interface *) vty->index;
764 zif = ifp->info;
765
766 zif->rtadv.AdvReachableTime = 0;
767
768 return CMD_SUCCESS;
769}
770
771DEFUN (ipv6_nd_managed_config_flag,
772 ipv6_nd_managed_config_flag_cmd,
773 "ipv6 nd managed-config-flag",
774 IP_STR
775 "Neighbor discovery\n"
776 "Managed address configuration flag\n")
777{
778 struct interface *ifp;
779 struct zebra_if *zif;
780
781 ifp = (struct interface *) vty->index;
782 zif = ifp->info;
783
784 zif->rtadv.AdvManagedFlag = 1;
785
786 return CMD_SUCCESS;
787}
788
789DEFUN (no_ipv6_nd_managed_config_flag,
790 no_ipv6_nd_managed_config_flag_cmd,
791 "no ipv6 nd managed-config-flag",
792 NO_STR
793 IP_STR
794 "Neighbor discovery\n"
795 "Managed address configuration flag\n")
796{
797 struct interface *ifp;
798 struct zebra_if *zif;
799
800 ifp = (struct interface *) vty->index;
801 zif = ifp->info;
802
803 zif->rtadv.AdvManagedFlag = 0;
804
805 return CMD_SUCCESS;
806}
807
808DEFUN (ipv6_nd_other_config_flag,
809 ipv6_nd_other_config_flag_cmd,
810 "ipv6 nd other-config-flag",
811 IP_STR
812 "Neighbor discovery\n"
813 "Other statefull configuration flag\n")
814{
815 struct interface *ifp;
816 struct zebra_if *zif;
817
818 ifp = (struct interface *) vty->index;
819 zif = ifp->info;
820
821 zif->rtadv.AdvOtherConfigFlag = 1;
822
823 return CMD_SUCCESS;
824}
825
826DEFUN (no_ipv6_nd_other_config_flag,
827 no_ipv6_nd_other_config_flag_cmd,
828 "no ipv6 nd other-config-flag",
829 NO_STR
830 IP_STR
831 "Neighbor discovery\n"
832 "Other statefull configuration flag\n")
833{
834 struct interface *ifp;
835 struct zebra_if *zif;
836
837 ifp = (struct interface *) vty->index;
838 zif = ifp->info;
839
840 zif->rtadv.AdvOtherConfigFlag = 0;
841
842 return CMD_SUCCESS;
843}
844
845DEFUN (ipv6_nd_prefix_advertisement,
846 ipv6_nd_prefix_advertisement_cmd,
847 "ipv6 nd prefix-advertisement IPV6PREFIX VALID PREFERRED [onlink] [autoconfig]",
848 IP_STR
849 "Neighbor discovery\n"
850 "Prefix information\n"
851 "IPv6 prefix\n"
852 "Valid lifetime in seconds\n"
853 "Preferred lifetime in seconds\n"
854 "On link flag\n"
855 "Autonomous address-configuration flag\n")
856{
857 int i;
858 int ret;
859 struct interface *ifp;
860 struct zebra_if *zebra_if;
861 struct rtadv_prefix rp;
862
863 ifp = (struct interface *) vty->index;
864 zebra_if = ifp->info;
865
866 ret = str2prefix_ipv6 (argv[0], (struct prefix_ipv6 *) &rp.prefix);
867 if (!ret)
868 {
869 vty_out (vty, "Malformed IPv6 prefix%s", VTY_NEWLINE);
870 return CMD_WARNING;
871 }
872
873 if (argc == 1)
874 {
875 rp.AdvValidLifetime = RTADV_VALID_LIFETIME;
876 rp.AdvPreferredLifetime = RTADV_PREFERRED_LIFETIME;
877 rp.AdvOnLinkFlag = 1;
878 rp.AdvAutonomousFlag = 1;
879 }
880 else
881 {
882 rp.AdvValidLifetime = (u_int32_t) atol (argv[1]);
883 rp.AdvPreferredLifetime = (u_int32_t) atol (argv[2]);
884 if (rp.AdvPreferredLifetime > rp.AdvValidLifetime)
885 {
886 vty_out (vty, "Invalid preferred lifetime%s", VTY_NEWLINE);
887 return CMD_WARNING;
888 }
889
890 rp.AdvOnLinkFlag = 0;
891 rp.AdvAutonomousFlag = 0;
892 for (i = 3; i < argc; i++)
893 {
894 if (! strcmp (argv[i], "onlink"))
895 rp.AdvOnLinkFlag = 1;
896 else if (! strcmp (argv[i], "autoconfig"))
897 rp.AdvAutonomousFlag = 1;
898 }
899 }
900
901 rtadv_prefix_set (zebra_if, &rp);
902
903 return CMD_SUCCESS;
904}
905
906ALIAS (ipv6_nd_prefix_advertisement,
907 ipv6_nd_prefix_advertisement_no_val_cmd,
908 "ipv6 nd prefix-advertisement IPV6PREFIX",
909 IP_STR
910 "Neighbor discovery\n"
911 "Prefix information\n"
912 "IPv6 prefix\n")
913
914DEFUN (no_ipv6_nd_prefix_advertisement,
915 no_ipv6_nd_prefix_advertisement_cmd,
916 "no ipv6 nd prefix-advertisement IPV6PREFIX",
917 NO_STR
918 IP_STR
919 "Neighbor discovery\n"
920 "Prefix information\n"
921 "IPv6 prefix\n")
922{
923 int ret;
924 struct interface *ifp;
925 struct zebra_if *zebra_if;
926 struct rtadv_prefix rp;
927
928 ifp = (struct interface *) vty->index;
929 zebra_if = ifp->info;
930
931 ret = str2prefix_ipv6 (argv[0], (struct prefix_ipv6 *) &rp.prefix);
932 if (!ret)
933 {
934 vty_out (vty, "Malformed IPv6 prefix%s", VTY_NEWLINE);
935 return CMD_WARNING;
936 }
937
938 ret = rtadv_prefix_reset (zebra_if, &rp);
939 if (!ret)
940 {
941 vty_out (vty, "Non-exist IPv6 prefix%s", VTY_NEWLINE);
942 return CMD_WARNING;
943 }
944
945 return CMD_SUCCESS;
946}
947
948/* Write configuration about router advertisement. */
949void
950rtadv_config_write (struct vty *vty, struct interface *ifp)
951{
952 struct zebra_if *zif;
953 listnode node;
954 struct rtadv_prefix *rprefix;
955 u_char buf[INET6_ADDRSTRLEN];
956
957 if (! rtadv)
958 return;
959
960 zif = ifp->info;
961
962 if (! if_is_loopback (ifp))
963 {
964 if (zif->rtadv.AdvSendAdvertisements)
965 vty_out (vty, " no ipv6 nd suppress-ra%s", VTY_NEWLINE);
966 else
967 vty_out (vty, " ipv6 nd suppress-ra%s", VTY_NEWLINE);
968 }
969
970 if (zif->rtadv.MaxRtrAdvInterval != RTADV_MAX_RTR_ADV_INTERVAL)
971 vty_out (vty, " ipv6 nd ra-interval %d%s", zif->rtadv.MaxRtrAdvInterval,
972 VTY_NEWLINE);
973
974 if (zif->rtadv.AdvDefaultLifetime != RTADV_ADV_DEFAULT_LIFETIME)
975 vty_out (vty, " ipv6 nd ra-lifetime %d%s", zif->rtadv.AdvDefaultLifetime,
976 VTY_NEWLINE);
977
978 if (zif->rtadv.AdvReachableTime)
979 vty_out (vty, " ipv6 nd reachable-time %d%s", zif->rtadv.AdvReachableTime,
980 VTY_NEWLINE);
981
982 if (zif->rtadv.AdvManagedFlag)
983 vty_out (vty, " ipv6 nd managed-config-flag%s", VTY_NEWLINE);
984
985 if (zif->rtadv.AdvOtherConfigFlag)
986 vty_out (vty, " ipv6 nd other-config-flag%s", VTY_NEWLINE);
987
988 for (node = listhead(zif->rtadv.AdvPrefixList); node; node = nextnode (node))
989 {
990 rprefix = getdata (node);
991 vty_out (vty, " ipv6 nd prefix-advertisement %s/%d %d %d",
992 inet_ntop (AF_INET6, &rprefix->prefix.u.prefix6,
993 buf, INET6_ADDRSTRLEN),
994 rprefix->prefix.prefixlen,
995 rprefix->AdvValidLifetime,
996 rprefix->AdvPreferredLifetime);
997 if (rprefix->AdvOnLinkFlag)
998 vty_out (vty, " onlink");
999 if (rprefix->AdvAutonomousFlag)
1000 vty_out (vty, " autoconfig");
1001 vty_out (vty, "%s", VTY_NEWLINE);
1002 }
1003}
1004
1005extern struct thread_master *master;
1006
1007void
1008rtadv_event (enum rtadv_event event, int val)
1009{
1010 switch (event)
1011 {
1012 case RTADV_START:
1013 if (! rtadv->ra_read)
1014 rtadv->ra_read = thread_add_read (master, rtadv_read, NULL, val);
1015 if (! rtadv->ra_timer)
1016 rtadv->ra_timer = thread_add_event (master, rtadv_timer, NULL, 0);
1017 break;
1018 case RTADV_STOP:
1019 if (rtadv->ra_timer)
1020 {
1021 thread_cancel (rtadv->ra_timer);
1022 rtadv->ra_timer = NULL;
1023 }
1024 if (rtadv->ra_read)
1025 {
1026 thread_cancel (rtadv->ra_read);
1027 rtadv->ra_read = NULL;
1028 }
1029 break;
1030 case RTADV_TIMER:
1031 if (! rtadv->ra_timer)
1032 rtadv->ra_timer = thread_add_timer (master, rtadv_timer, NULL, val);
1033 break;
1034 case RTADV_READ:
1035 if (! rtadv->ra_read)
1036 rtadv->ra_read = thread_add_read (master, rtadv_read, NULL, val);
1037 break;
1038 default:
1039 break;
1040 }
1041 return;
1042}
1043
1044void
1045rtadv_init ()
1046{
1047 int sock;
1048
1049 sock = rtadv_make_socket ();
1050 if (sock < 0)
1051 return;
1052
1053 rtadv = rtadv_new ();
1054 rtadv->sock = sock;
1055
1056 install_element (INTERFACE_NODE, &ipv6_nd_suppress_ra_cmd);
1057 install_element (INTERFACE_NODE, &no_ipv6_nd_suppress_ra_cmd);
1058 install_element (INTERFACE_NODE, &ipv6_nd_send_ra_cmd);
1059 install_element (INTERFACE_NODE, &no_ipv6_nd_send_ra_cmd);
1060 install_element (INTERFACE_NODE, &ipv6_nd_ra_interval_cmd);
1061 install_element (INTERFACE_NODE, &no_ipv6_nd_ra_interval_cmd);
1062 install_element (INTERFACE_NODE, &ipv6_nd_ra_lifetime_cmd);
1063 install_element (INTERFACE_NODE, &no_ipv6_nd_ra_lifetime_cmd);
1064 install_element (INTERFACE_NODE, &ipv6_nd_reachable_time_cmd);
1065 install_element (INTERFACE_NODE, &no_ipv6_nd_reachable_time_cmd);
1066 install_element (INTERFACE_NODE, &ipv6_nd_managed_config_flag_cmd);
1067 install_element (INTERFACE_NODE, &no_ipv6_nd_managed_config_flag_cmd);
1068 install_element (INTERFACE_NODE, &ipv6_nd_other_config_flag_cmd);
1069 install_element (INTERFACE_NODE, &no_ipv6_nd_other_config_flag_cmd);
1070 install_element (INTERFACE_NODE, &ipv6_nd_prefix_advertisement_cmd);
1071 install_element (INTERFACE_NODE, &ipv6_nd_prefix_advertisement_no_val_cmd);
1072 install_element (INTERFACE_NODE, &no_ipv6_nd_prefix_advertisement_cmd);
1073}
1074
1075int
1076if_join_all_router (int sock, struct interface *ifp)
1077{
1078 int ret;
1079
1080 struct ipv6_mreq mreq;
1081
1082 memset (&mreq, 0, sizeof (struct ipv6_mreq));
1083 inet_pton (AF_INET6, ALLROUTER, &mreq.ipv6mr_multiaddr);
1084 mreq.ipv6mr_interface = ifp->ifindex;
1085
1086 ret = setsockopt (sock, IPPROTO_IPV6, IPV6_JOIN_GROUP,
1087 (char *) &mreq, sizeof mreq);
1088 if (ret < 0)
1089 zlog_warn ("can't setsockopt IPV6_JOIN_GROUP: %s", strerror (errno));
1090
1091 zlog_info ("rtadv: %s join to all-routers multicast group", ifp->name);
1092
1093 return 0;
1094}
1095
1096int
1097if_leave_all_router (int sock, struct interface *ifp)
1098{
1099 int ret;
1100
1101 struct ipv6_mreq mreq;
1102
1103 memset (&mreq, 0, sizeof (struct ipv6_mreq));
1104 inet_pton (AF_INET6, ALLROUTER, &mreq.ipv6mr_multiaddr);
1105 mreq.ipv6mr_interface = ifp->ifindex;
1106
1107 ret = setsockopt (sock, IPPROTO_IPV6, IPV6_LEAVE_GROUP,
1108 (char *) &mreq, sizeof mreq);
1109 if (ret < 0)
1110 zlog_warn ("can't setsockopt IPV6_LEAVE_GROUP: %s", strerror (errno));
1111
1112 zlog_info ("rtadv: %s leave from all-routers multicast group", ifp->name);
1113
1114 return 0;
1115}
1116
1117#else
1118void
1119rtadv_init ()
1120{
1121 /* Empty.*/;
1122}
1123#endif /* RTADV && HAVE_IPV6 */