]> git.proxmox.com Git - mirror_frr.git/blob - zebra/rtadv.c
2003-06-15 Paul Jakma <paul@dishone.st>
[mirror_frr.git] / zebra / rtadv.c
1 /* 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"
32 #include "privs.h"
33
34 #include "zebra/interface.h"
35 #include "zebra/rtadv.h"
36 #include "zebra/debug.h"
37 #include "zebra/zserv.h"
38
39 extern struct zebra_privs_t zserv_privs;
40
41 #if defined (HAVE_IPV6) && defined (RTADV)
42
43 /* If RFC2133 definition is used. */
44 #ifndef IPV6_JOIN_GROUP
45 #define IPV6_JOIN_GROUP IPV6_ADD_MEMBERSHIP
46 #endif
47 #ifndef IPV6_LEAVE_GROUP
48 #define IPV6_LEAVE_GROUP IPV6_DROP_MEMBERSHIP
49 #endif
50
51 #define ALLNODE "ff02::1"
52 #define ALLROUTER "ff02::2"
53
54 extern struct zebra_t zebrad;
55
56 enum rtadv_event {RTADV_START, RTADV_STOP, RTADV_TIMER, RTADV_READ};
57
58 void rtadv_event (enum rtadv_event, int);
59
60 int if_join_all_router (int, struct interface *);
61 int if_leave_all_router (int, struct interface *);
62 \f
63 /* Structure which hold status of router advertisement. */
64 struct rtadv
65 {
66 int sock;
67
68 int adv_if_count;
69
70 struct thread *ra_read;
71 struct thread *ra_timer;
72 };
73
74 struct rtadv *rtadv = NULL;
75 \f
76 struct rtadv *
77 rtadv_new ()
78 {
79 struct rtadv *new;
80 new = XMALLOC (MTYPE_TMP, sizeof (struct rtadv));
81 memset (new, 0, sizeof (struct rtadv));
82 return new;
83 }
84
85 void
86 rtadv_free (struct rtadv *rtadv)
87 {
88 XFREE (MTYPE_TMP, rtadv);
89 }
90
91 int
92 rtadv_recv_packet (int sock, u_char *buf, int buflen,
93 struct sockaddr_in6 *from, unsigned int *ifindex,
94 int *hoplimit)
95 {
96 int ret;
97 struct msghdr msg;
98 struct iovec iov;
99 struct cmsghdr *cmsgptr;
100 struct in6_addr dst;
101
102 char adata[1024];
103
104 /* Fill in message and iovec. */
105 msg.msg_name = (void *) from;
106 msg.msg_namelen = sizeof (struct sockaddr_in6);
107 msg.msg_iov = &iov;
108 msg.msg_iovlen = 1;
109 msg.msg_control = (void *) adata;
110 msg.msg_controllen = sizeof adata;
111 iov.iov_base = buf;
112 iov.iov_len = buflen;
113
114 /* If recvmsg fail return minus value. */
115 ret = recvmsg (sock, &msg, 0);
116 if (ret < 0)
117 return ret;
118
119 for (cmsgptr = CMSG_FIRSTHDR(&msg); cmsgptr != NULL;
120 cmsgptr = CMSG_NXTHDR(&msg, cmsgptr))
121 {
122 /* I want interface index which this packet comes from. */
123 if (cmsgptr->cmsg_level == IPPROTO_IPV6 &&
124 cmsgptr->cmsg_type == IPV6_PKTINFO)
125 {
126 struct in6_pktinfo *ptr;
127
128 ptr = (struct in6_pktinfo *) CMSG_DATA (cmsgptr);
129 *ifindex = ptr->ipi6_ifindex;
130 memcpy(&dst, &ptr->ipi6_addr, sizeof(ptr->ipi6_addr));
131 }
132
133 /* Incoming packet's hop limit. */
134 if (cmsgptr->cmsg_level == IPPROTO_IPV6 &&
135 cmsgptr->cmsg_type == IPV6_HOPLIMIT)
136 *hoplimit = *((int *) CMSG_DATA (cmsgptr));
137 }
138 return ret;
139 }
140
141 #define RTADV_MSG_SIZE 4096
142
143 /* Send router advertisement packet. */
144 void
145 rtadv_send_packet (int sock, struct interface *ifp)
146 {
147 struct msghdr msg;
148 struct iovec iov;
149 struct cmsghdr *cmsgptr;
150 struct in6_pktinfo *pkt;
151 struct sockaddr_in6 addr;
152 #ifdef HAVE_SOCKADDR_DL
153 struct sockaddr_dl *sdl;
154 #endif /* HAVE_SOCKADDR_DL */
155 char adata [sizeof (struct cmsghdr) + sizeof (struct in6_pktinfo)];
156 unsigned char buf[RTADV_MSG_SIZE];
157 struct nd_router_advert *rtadv;
158 int ret;
159 int len = 0;
160 struct zebra_if *zif;
161 u_char all_nodes_addr[] = {0xff,0x02,0,0,0,0,0,0,0,0,0,0,0,0,0,1};
162 listnode node;
163
164 /* Logging of packet. */
165 if (IS_ZEBRA_DEBUG_PACKET)
166 zlog_info ("Router advertisement send to %s", ifp->name);
167
168 /* Fill in sockaddr_in6. */
169 memset (&addr, 0, sizeof (struct sockaddr_in6));
170 addr.sin6_family = AF_INET6;
171 #ifdef SIN6_LEN
172 addr.sin6_len = sizeof (struct sockaddr_in6);
173 #endif /* SIN6_LEN */
174 addr.sin6_port = htons (IPPROTO_ICMPV6);
175 memcpy (&addr.sin6_addr, all_nodes_addr, sizeof (struct in6_addr));
176
177 /* Fetch interface information. */
178 zif = ifp->info;
179
180 /* Make router advertisement message. */
181 rtadv = (struct nd_router_advert *) buf;
182
183 rtadv->nd_ra_type = ND_ROUTER_ADVERT;
184 rtadv->nd_ra_code = 0;
185 rtadv->nd_ra_cksum = 0;
186
187 rtadv->nd_ra_curhoplimit = 64;
188 rtadv->nd_ra_flags_reserved = 0;
189 if (zif->rtadv.AdvManagedFlag)
190 rtadv->nd_ra_flags_reserved |= ND_RA_FLAG_MANAGED;
191 if (zif->rtadv.AdvOtherConfigFlag)
192 rtadv->nd_ra_flags_reserved |= ND_RA_FLAG_OTHER;
193 rtadv->nd_ra_router_lifetime = htons (zif->rtadv.AdvDefaultLifetime);
194 rtadv->nd_ra_reachable = htonl (zif->rtadv.AdvReachableTime);
195 rtadv->nd_ra_retransmit = htonl (0);
196
197 len = sizeof (struct nd_router_advert);
198
199 /* Fill in prefix. */
200 for (node = listhead (zif->rtadv.AdvPrefixList); node; node = nextnode (node))
201 {
202 struct nd_opt_prefix_info *pinfo;
203 struct rtadv_prefix *rprefix;
204
205 rprefix = getdata (node);
206
207 pinfo = (struct nd_opt_prefix_info *) (buf + len);
208
209 pinfo->nd_opt_pi_type = ND_OPT_PREFIX_INFORMATION;
210 pinfo->nd_opt_pi_len = 4;
211 pinfo->nd_opt_pi_prefix_len = rprefix->prefix.prefixlen;
212
213 pinfo->nd_opt_pi_flags_reserved = 0;
214 if (rprefix->AdvOnLinkFlag)
215 pinfo->nd_opt_pi_flags_reserved |= ND_OPT_PI_FLAG_ONLINK;
216 if (rprefix->AdvAutonomousFlag)
217 pinfo->nd_opt_pi_flags_reserved |= ND_OPT_PI_FLAG_AUTO;
218
219 pinfo->nd_opt_pi_valid_time = htonl (rprefix->AdvValidLifetime);
220 pinfo->nd_opt_pi_preferred_time = htonl (rprefix->AdvPreferredLifetime);
221 pinfo->nd_opt_pi_reserved2 = 0;
222
223 memcpy (&pinfo->nd_opt_pi_prefix, &rprefix->prefix.u.prefix6,
224 sizeof (struct in6_addr));
225
226 #ifdef DEBUG
227 {
228 u_char buf[INET6_ADDRSTRLEN];
229
230 zlog_info ("DEBUG %s", inet_ntop (AF_INET6, &pinfo->nd_opt_pi_prefix, buf, INET6_ADDRSTRLEN));
231
232 }
233 #endif /* DEBUG */
234
235 len += sizeof (struct nd_opt_prefix_info);
236 }
237
238 /* Hardware address. */
239 #ifdef HAVE_SOCKADDR_DL
240 sdl = &ifp->sdl;
241 if (sdl != NULL && sdl->sdl_alen != 0)
242 {
243 buf[len++] = ND_OPT_SOURCE_LINKADDR;
244 buf[len++] = (sdl->sdl_alen + 2) >> 3;
245
246 memcpy (buf + len, LLADDR (sdl), sdl->sdl_alen);
247 len += sdl->sdl_alen;
248 }
249 #else
250 if (ifp->hw_addr_len != 0)
251 {
252 buf[len++] = ND_OPT_SOURCE_LINKADDR;
253 buf[len++] = (ifp->hw_addr_len + 2) >> 3;
254
255 memcpy (buf + len, ifp->hw_addr, ifp->hw_addr_len);
256 len += ifp->hw_addr_len;
257 }
258 #endif /* HAVE_SOCKADDR_DL */
259
260 msg.msg_name = (void *) &addr;
261 msg.msg_namelen = sizeof (struct sockaddr_in6);
262 msg.msg_iov = &iov;
263 msg.msg_iovlen = 1;
264 msg.msg_control = (void *) adata;
265 msg.msg_controllen = sizeof adata;
266 iov.iov_base = buf;
267 iov.iov_len = len;
268
269 cmsgptr = (struct cmsghdr *)adata;
270 cmsgptr->cmsg_len = sizeof adata;
271 cmsgptr->cmsg_level = IPPROTO_IPV6;
272 cmsgptr->cmsg_type = IPV6_PKTINFO;
273 pkt = (struct in6_pktinfo *) CMSG_DATA (cmsgptr);
274 memset (&pkt->ipi6_addr, 0, sizeof (struct in6_addr));
275 pkt->ipi6_ifindex = ifp->ifindex;
276
277 ret = sendmsg (sock, &msg, 0);
278 if (ret <0)
279 perror ("sendmsg");
280 }
281
282 int
283 rtadv_timer (struct thread *thread)
284 {
285 listnode node;
286 struct interface *ifp;
287 struct zebra_if *zif;
288
289 rtadv->ra_timer = NULL;
290 rtadv_event (RTADV_TIMER, 1);
291
292 for (node = listhead (iflist); node; nextnode (node))
293 {
294 ifp = getdata (node);
295
296 if (if_is_loopback (ifp))
297 continue;
298
299 zif = ifp->info;
300
301 if (zif->rtadv.AdvSendAdvertisements)
302 if (--zif->rtadv.AdvIntervalTimer <= 0)
303 {
304 zif->rtadv.AdvIntervalTimer = zif->rtadv.MaxRtrAdvInterval;
305 rtadv_send_packet (rtadv->sock, ifp);
306 }
307 }
308 return 0;
309 }
310
311 void
312 rtadv_process_solicit (struct interface *ifp)
313 {
314 zlog_info ("Router solicitation received on %s", ifp->name);
315
316 rtadv_send_packet (rtadv->sock, ifp);
317 }
318
319 void
320 rtadv_process_advert ()
321 {
322 zlog_info ("Router advertisement received");
323 }
324
325 void
326 rtadv_process_packet (u_char *buf, int len, unsigned int ifindex, int hoplimit)
327 {
328 struct icmp6_hdr *icmph;
329 struct interface *ifp;
330 struct zebra_if *zif;
331
332 /* Interface search. */
333 ifp = if_lookup_by_index (ifindex);
334 if (ifp == NULL)
335 {
336 zlog_warn ("Unknown interface index: %d", ifindex);
337 return;
338 }
339
340 if (if_is_loopback (ifp))
341 return;
342
343 /* Check interface configuration. */
344 zif = ifp->info;
345 if (! zif->rtadv.AdvSendAdvertisements)
346 return;
347
348 /* ICMP message length check. */
349 if (len < sizeof (struct icmp6_hdr))
350 {
351 zlog_warn ("Invalid ICMPV6 packet length: %d", len);
352 return;
353 }
354
355 icmph = (struct icmp6_hdr *) buf;
356
357 /* ICMP message type check. */
358 if (icmph->icmp6_type != ND_ROUTER_SOLICIT &&
359 icmph->icmp6_type != ND_ROUTER_ADVERT)
360 {
361 zlog_warn ("Unwanted ICMPV6 message type: %d", icmph->icmp6_type);
362 return;
363 }
364
365 /* Hoplimit check. */
366 if (hoplimit >= 0 && hoplimit != 255)
367 {
368 zlog_warn ("Invalid hoplimit %d for router advertisement ICMP packet",
369 hoplimit);
370 return;
371 }
372
373 /* Check ICMP message type. */
374 if (icmph->icmp6_type == ND_ROUTER_SOLICIT)
375 rtadv_process_solicit (ifp);
376 else if (icmph->icmp6_type == ND_ROUTER_ADVERT)
377 rtadv_process_advert ();
378
379 return;
380 }
381
382 int
383 rtadv_read (struct thread *thread)
384 {
385 int sock;
386 int len;
387 u_char buf[RTADV_MSG_SIZE];
388 struct sockaddr_in6 from;
389 unsigned int ifindex;
390 int hoplimit = -1;
391
392 sock = THREAD_FD (thread);
393 rtadv->ra_read = NULL;
394
395 /* Register myself. */
396 rtadv_event (RTADV_READ, sock);
397
398 len = rtadv_recv_packet (sock, buf, BUFSIZ, &from, &ifindex, &hoplimit);
399
400 if (len < 0)
401 {
402 zlog_warn ("router solicitation recv failed: %s.", strerror (errno));
403 return len;
404 }
405
406 rtadv_process_packet (buf, len, ifindex, hoplimit);
407
408 return 0;
409 }
410
411 int
412 rtadv_make_socket (void)
413 {
414 int sock;
415 int ret;
416 struct icmp6_filter filter;
417
418 if ( zserv_privs.change (ZPRIVS_RAISE) )
419 zlog_err ("rtadv_make_socket: could not raise privs, %s",
420 strerror (errno) );
421
422 sock = socket (AF_INET6, SOCK_RAW, IPPROTO_ICMPV6);
423
424 if ( zserv_privs.change (ZPRIVS_LOWER) )
425 zlog_err ("rtadv_make_socket: could not lower privs, %s",
426 strerror (errno) );
427
428 /* When we can't make ICMPV6 socket simply back. Router
429 advertisement feature will not be supported. */
430 if (sock < 0)
431 return -1;
432
433 ret = setsockopt_ipv6_pktinfo (sock, 1);
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 \f
464 struct rtadv_prefix *
465 rtadv_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
475 void
476 rtadv_prefix_free (struct rtadv_prefix *rtadv_prefix)
477 {
478 XFREE (MTYPE_RTADV_PREFIX, rtadv_prefix);
479 }
480
481 struct rtadv_prefix *
482 rtadv_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
496 struct rtadv_prefix *
497 rtadv_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
512 void
513 rtadv_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
526 int
527 rtadv_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
542 DEFUN (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
576 ALIAS (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
584 DEFUN (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
619 ALIAS (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
626 DEFUN (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
656 DEFUN (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
677 DEFUN (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
705 DEFUN (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
724 DEFUN (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
752 DEFUN (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
771 DEFUN (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
789 DEFUN (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
808 DEFUN (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
826 DEFUN (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
845 DEFUN (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
906 ALIAS (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
914 DEFUN (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. */
949 void
950 rtadv_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
1005
1006 void
1007 rtadv_event (enum rtadv_event event, int val)
1008 {
1009 switch (event)
1010 {
1011 case RTADV_START:
1012 if (! rtadv->ra_read)
1013 rtadv->ra_read = thread_add_read (zebrad.master, rtadv_read, NULL, val);
1014 if (! rtadv->ra_timer)
1015 rtadv->ra_timer = thread_add_event (zebrad.master, rtadv_timer, NULL, 0);
1016 break;
1017 case RTADV_STOP:
1018 if (rtadv->ra_timer)
1019 {
1020 thread_cancel (rtadv->ra_timer);
1021 rtadv->ra_timer = NULL;
1022 }
1023 if (rtadv->ra_read)
1024 {
1025 thread_cancel (rtadv->ra_read);
1026 rtadv->ra_read = NULL;
1027 }
1028 break;
1029 case RTADV_TIMER:
1030 if (! rtadv->ra_timer)
1031 rtadv->ra_timer = thread_add_timer (zebrad.master, rtadv_timer, NULL, val);
1032 break;
1033 case RTADV_READ:
1034 if (! rtadv->ra_read)
1035 rtadv->ra_read = thread_add_read (zebrad.master, rtadv_read, NULL, val);
1036 break;
1037 default:
1038 break;
1039 }
1040 return;
1041 }
1042
1043 void
1044 rtadv_init ()
1045 {
1046 int sock;
1047
1048 sock = rtadv_make_socket ();
1049 if (sock < 0)
1050 return;
1051
1052 rtadv = rtadv_new ();
1053 rtadv->sock = sock;
1054
1055 install_element (INTERFACE_NODE, &ipv6_nd_suppress_ra_cmd);
1056 install_element (INTERFACE_NODE, &no_ipv6_nd_suppress_ra_cmd);
1057 install_element (INTERFACE_NODE, &ipv6_nd_send_ra_cmd);
1058 install_element (INTERFACE_NODE, &no_ipv6_nd_send_ra_cmd);
1059 install_element (INTERFACE_NODE, &ipv6_nd_ra_interval_cmd);
1060 install_element (INTERFACE_NODE, &no_ipv6_nd_ra_interval_cmd);
1061 install_element (INTERFACE_NODE, &ipv6_nd_ra_lifetime_cmd);
1062 install_element (INTERFACE_NODE, &no_ipv6_nd_ra_lifetime_cmd);
1063 install_element (INTERFACE_NODE, &ipv6_nd_reachable_time_cmd);
1064 install_element (INTERFACE_NODE, &no_ipv6_nd_reachable_time_cmd);
1065 install_element (INTERFACE_NODE, &ipv6_nd_managed_config_flag_cmd);
1066 install_element (INTERFACE_NODE, &no_ipv6_nd_managed_config_flag_cmd);
1067 install_element (INTERFACE_NODE, &ipv6_nd_other_config_flag_cmd);
1068 install_element (INTERFACE_NODE, &no_ipv6_nd_other_config_flag_cmd);
1069 install_element (INTERFACE_NODE, &ipv6_nd_prefix_advertisement_cmd);
1070 install_element (INTERFACE_NODE, &ipv6_nd_prefix_advertisement_no_val_cmd);
1071 install_element (INTERFACE_NODE, &no_ipv6_nd_prefix_advertisement_cmd);
1072 }
1073
1074 int
1075 if_join_all_router (int sock, struct interface *ifp)
1076 {
1077 int ret;
1078
1079 struct ipv6_mreq mreq;
1080
1081 memset (&mreq, 0, sizeof (struct ipv6_mreq));
1082 inet_pton (AF_INET6, ALLROUTER, &mreq.ipv6mr_multiaddr);
1083 mreq.ipv6mr_interface = ifp->ifindex;
1084
1085 ret = setsockopt (sock, IPPROTO_IPV6, IPV6_JOIN_GROUP,
1086 (char *) &mreq, sizeof mreq);
1087 if (ret < 0)
1088 zlog_warn ("can't setsockopt IPV6_JOIN_GROUP: %s", strerror (errno));
1089
1090 zlog_info ("rtadv: %s join to all-routers multicast group", ifp->name);
1091
1092 return 0;
1093 }
1094
1095 int
1096 if_leave_all_router (int sock, struct interface *ifp)
1097 {
1098 int ret;
1099
1100 struct ipv6_mreq mreq;
1101
1102 memset (&mreq, 0, sizeof (struct ipv6_mreq));
1103 inet_pton (AF_INET6, ALLROUTER, &mreq.ipv6mr_multiaddr);
1104 mreq.ipv6mr_interface = ifp->ifindex;
1105
1106 ret = setsockopt (sock, IPPROTO_IPV6, IPV6_LEAVE_GROUP,
1107 (char *) &mreq, sizeof mreq);
1108 if (ret < 0)
1109 zlog_warn ("can't setsockopt IPV6_LEAVE_GROUP: %s", strerror (errno));
1110
1111 zlog_info ("rtadv: %s leave from all-routers multicast group", ifp->name);
1112
1113 return 0;
1114 }
1115
1116 #else
1117 void
1118 rtadv_init ()
1119 {
1120 /* Empty.*/;
1121 }
1122 #endif /* RTADV && HAVE_IPV6 */