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