2 * Neighbour Discovery for IPv6
3 * Linux INET6 implementation
6 * Pedro Roque <roque@di.fc.ul.pt>
7 * Mike Shaver <shaver@ingenia.com>
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version
12 * 2 of the License, or (at your option) any later version.
18 * Pierre Ynard : export userland ND options
19 * through netlink (RDNSS support)
20 * Lars Fenneberg : fixed MTU setting on receipt
22 * Janos Farkas : kmalloc failure checks
23 * Alexey Kuznetsov : state machine reworked
24 * and moved to net/core.
25 * Pekka Savola : RFC2461 validation
26 * YOSHIFUJI Hideaki @USAGI : Verify ND options properly
29 /* Set to 3 to get tracing... */
32 #define ND_PRINTK(fmt, args...) do { if (net_ratelimit()) { printk(fmt, ## args); } } while(0)
33 #define ND_NOPRINTK(x...) do { ; } while(0)
34 #define ND_PRINTK0 ND_PRINTK
35 #define ND_PRINTK1 ND_NOPRINTK
36 #define ND_PRINTK2 ND_NOPRINTK
37 #define ND_PRINTK3 ND_NOPRINTK
40 #define ND_PRINTK1 ND_PRINTK
44 #define ND_PRINTK2 ND_PRINTK
48 #define ND_PRINTK3 ND_PRINTK
51 #include <linux/module.h>
52 #include <linux/errno.h>
53 #include <linux/types.h>
54 #include <linux/socket.h>
55 #include <linux/sockios.h>
56 #include <linux/sched.h>
57 #include <linux/net.h>
58 #include <linux/in6.h>
59 #include <linux/route.h>
60 #include <linux/init.h>
61 #include <linux/rcupdate.h>
62 #include <linux/slab.h>
64 #include <linux/sysctl.h>
67 #include <linux/if_addr.h>
68 #include <linux/if_arp.h>
69 #include <linux/ipv6.h>
70 #include <linux/icmpv6.h>
71 #include <linux/jhash.h>
77 #include <net/protocol.h>
78 #include <net/ndisc.h>
79 #include <net/ip6_route.h>
80 #include <net/addrconf.h>
83 #include <net/netlink.h>
84 #include <linux/rtnetlink.h>
87 #include <net/ip6_checksum.h>
88 #include <net/inet_common.h>
89 #include <linux/proc_fs.h>
91 #include <linux/netfilter.h>
92 #include <linux/netfilter_ipv6.h>
94 static u32 ndisc_hash(const void *pkey,
95 const struct net_device *dev,
97 static int ndisc_constructor(struct neighbour *neigh);
98 static void ndisc_solicit(struct neighbour *neigh, struct sk_buff *skb);
99 static void ndisc_error_report(struct neighbour *neigh, struct sk_buff *skb);
100 static int pndisc_constructor(struct pneigh_entry *n);
101 static void pndisc_destructor(struct pneigh_entry *n);
102 static void pndisc_redo(struct sk_buff *skb);
104 static const struct neigh_ops ndisc_generic_ops = {
106 .solicit = ndisc_solicit,
107 .error_report = ndisc_error_report,
108 .output = neigh_resolve_output,
109 .connected_output = neigh_connected_output,
110 .hh_output = dev_queue_xmit,
111 .queue_xmit = dev_queue_xmit,
114 static const struct neigh_ops ndisc_hh_ops = {
116 .solicit = ndisc_solicit,
117 .error_report = ndisc_error_report,
118 .output = neigh_resolve_output,
119 .connected_output = neigh_resolve_output,
120 .hh_output = dev_queue_xmit,
121 .queue_xmit = dev_queue_xmit,
125 static const struct neigh_ops ndisc_direct_ops = {
127 .output = dev_queue_xmit,
128 .connected_output = dev_queue_xmit,
129 .hh_output = dev_queue_xmit,
130 .queue_xmit = dev_queue_xmit,
133 struct neigh_table nd_tbl = {
135 .entry_size = sizeof(struct neighbour) + sizeof(struct in6_addr),
136 .key_len = sizeof(struct in6_addr),
138 .constructor = ndisc_constructor,
139 .pconstructor = pndisc_constructor,
140 .pdestructor = pndisc_destructor,
141 .proxy_redo = pndisc_redo,
145 .base_reachable_time = 30 * HZ,
146 .retrans_time = 1 * HZ,
147 .gc_staletime = 60 * HZ,
148 .reachable_time = 30 * HZ,
149 .delay_probe_time = 5 * HZ,
153 .anycast_delay = 1 * HZ,
154 .proxy_delay = (8 * HZ) / 10,
157 .gc_interval = 30 * HZ,
164 struct ndisc_options {
165 struct nd_opt_hdr *nd_opt_array[__ND_OPT_ARRAY_MAX];
166 #ifdef CONFIG_IPV6_ROUTE_INFO
167 struct nd_opt_hdr *nd_opts_ri;
168 struct nd_opt_hdr *nd_opts_ri_end;
170 struct nd_opt_hdr *nd_useropts;
171 struct nd_opt_hdr *nd_useropts_end;
174 #define nd_opts_src_lladdr nd_opt_array[ND_OPT_SOURCE_LL_ADDR]
175 #define nd_opts_tgt_lladdr nd_opt_array[ND_OPT_TARGET_LL_ADDR]
176 #define nd_opts_pi nd_opt_array[ND_OPT_PREFIX_INFO]
177 #define nd_opts_pi_end nd_opt_array[__ND_OPT_PREFIX_INFO_END]
178 #define nd_opts_rh nd_opt_array[ND_OPT_REDIRECT_HDR]
179 #define nd_opts_mtu nd_opt_array[ND_OPT_MTU]
181 #define NDISC_OPT_SPACE(len) (((len)+2+7)&~7)
184 * Return the padding between the option length and the start of the
185 * link addr. Currently only IP-over-InfiniBand needs this, although
186 * if RFC 3831 IPv6-over-Fibre Channel is ever implemented it may
187 * also need a pad of 2.
189 static int ndisc_addr_option_pad(unsigned short type)
192 case ARPHRD_INFINIBAND: return 2;
197 static inline int ndisc_opt_addr_space(struct net_device *dev)
199 return NDISC_OPT_SPACE(dev->addr_len + ndisc_addr_option_pad(dev->type));
202 static u8 *ndisc_fill_addr_option(u8 *opt, int type, void *data, int data_len,
203 unsigned short addr_type)
205 int space = NDISC_OPT_SPACE(data_len);
206 int pad = ndisc_addr_option_pad(addr_type);
211 memset(opt + 2, 0, pad);
215 memcpy(opt+2, data, data_len);
218 if ((space -= data_len) > 0)
219 memset(opt, 0, space);
223 static struct nd_opt_hdr *ndisc_next_option(struct nd_opt_hdr *cur,
224 struct nd_opt_hdr *end)
227 if (!cur || !end || cur >= end)
229 type = cur->nd_opt_type;
231 cur = ((void *)cur) + (cur->nd_opt_len << 3);
232 } while(cur < end && cur->nd_opt_type != type);
233 return cur <= end && cur->nd_opt_type == type ? cur : NULL;
236 static inline int ndisc_is_useropt(struct nd_opt_hdr *opt)
238 return opt->nd_opt_type == ND_OPT_RDNSS;
241 static struct nd_opt_hdr *ndisc_next_useropt(struct nd_opt_hdr *cur,
242 struct nd_opt_hdr *end)
244 if (!cur || !end || cur >= end)
247 cur = ((void *)cur) + (cur->nd_opt_len << 3);
248 } while(cur < end && !ndisc_is_useropt(cur));
249 return cur <= end && ndisc_is_useropt(cur) ? cur : NULL;
252 static struct ndisc_options *ndisc_parse_options(u8 *opt, int opt_len,
253 struct ndisc_options *ndopts)
255 struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)opt;
257 if (!nd_opt || opt_len < 0 || !ndopts)
259 memset(ndopts, 0, sizeof(*ndopts));
262 if (opt_len < sizeof(struct nd_opt_hdr))
264 l = nd_opt->nd_opt_len << 3;
265 if (opt_len < l || l == 0)
267 switch (nd_opt->nd_opt_type) {
268 case ND_OPT_SOURCE_LL_ADDR:
269 case ND_OPT_TARGET_LL_ADDR:
271 case ND_OPT_REDIRECT_HDR:
272 if (ndopts->nd_opt_array[nd_opt->nd_opt_type]) {
273 ND_PRINTK2(KERN_WARNING
274 "%s(): duplicated ND6 option found: type=%d\n",
276 nd_opt->nd_opt_type);
278 ndopts->nd_opt_array[nd_opt->nd_opt_type] = nd_opt;
281 case ND_OPT_PREFIX_INFO:
282 ndopts->nd_opts_pi_end = nd_opt;
283 if (!ndopts->nd_opt_array[nd_opt->nd_opt_type])
284 ndopts->nd_opt_array[nd_opt->nd_opt_type] = nd_opt;
286 #ifdef CONFIG_IPV6_ROUTE_INFO
287 case ND_OPT_ROUTE_INFO:
288 ndopts->nd_opts_ri_end = nd_opt;
289 if (!ndopts->nd_opts_ri)
290 ndopts->nd_opts_ri = nd_opt;
294 if (ndisc_is_useropt(nd_opt)) {
295 ndopts->nd_useropts_end = nd_opt;
296 if (!ndopts->nd_useropts)
297 ndopts->nd_useropts = nd_opt;
300 * Unknown options must be silently ignored,
301 * to accommodate future extension to the
304 ND_PRINTK2(KERN_NOTICE
305 "%s(): ignored unsupported option; type=%d, len=%d\n",
307 nd_opt->nd_opt_type, nd_opt->nd_opt_len);
311 nd_opt = ((void *)nd_opt) + l;
316 static inline u8 *ndisc_opt_addr_data(struct nd_opt_hdr *p,
317 struct net_device *dev)
319 u8 *lladdr = (u8 *)(p + 1);
320 int lladdrlen = p->nd_opt_len << 3;
321 int prepad = ndisc_addr_option_pad(dev->type);
322 if (lladdrlen != NDISC_OPT_SPACE(dev->addr_len + prepad))
324 return lladdr + prepad;
327 int ndisc_mc_map(struct in6_addr *addr, char *buf, struct net_device *dev, int dir)
331 case ARPHRD_IEEE802: /* Not sure. Check it later. --ANK */
333 ipv6_eth_mc_map(addr, buf);
335 case ARPHRD_IEEE802_TR:
336 ipv6_tr_mc_map(addr,buf);
339 ipv6_arcnet_mc_map(addr, buf);
341 case ARPHRD_INFINIBAND:
342 ipv6_ib_mc_map(addr, dev->broadcast, buf);
346 memcpy(buf, dev->broadcast, dev->addr_len);
353 EXPORT_SYMBOL(ndisc_mc_map);
355 static u32 ndisc_hash(const void *pkey,
356 const struct net_device *dev,
359 const u32 *p32 = pkey;
363 for (i = 0; i < (sizeof(struct in6_addr) / sizeof(u32)); i++)
366 return jhash_2words(addr_hash, dev->ifindex, hash_rnd);
369 static int ndisc_constructor(struct neighbour *neigh)
371 struct in6_addr *addr = (struct in6_addr*)&neigh->primary_key;
372 struct net_device *dev = neigh->dev;
373 struct inet6_dev *in6_dev;
374 struct neigh_parms *parms;
375 int is_multicast = ipv6_addr_is_multicast(addr);
378 in6_dev = in6_dev_get(dev);
379 if (in6_dev == NULL) {
384 parms = in6_dev->nd_parms;
385 __neigh_parms_put(neigh->parms);
386 neigh->parms = neigh_parms_clone(parms);
389 neigh->type = is_multicast ? RTN_MULTICAST : RTN_UNICAST;
390 if (!dev->header_ops) {
391 neigh->nud_state = NUD_NOARP;
392 neigh->ops = &ndisc_direct_ops;
393 neigh->output = neigh->ops->queue_xmit;
396 neigh->nud_state = NUD_NOARP;
397 ndisc_mc_map(addr, neigh->ha, dev, 1);
398 } else if (dev->flags&(IFF_NOARP|IFF_LOOPBACK)) {
399 neigh->nud_state = NUD_NOARP;
400 memcpy(neigh->ha, dev->dev_addr, dev->addr_len);
401 if (dev->flags&IFF_LOOPBACK)
402 neigh->type = RTN_LOCAL;
403 } else if (dev->flags&IFF_POINTOPOINT) {
404 neigh->nud_state = NUD_NOARP;
405 memcpy(neigh->ha, dev->broadcast, dev->addr_len);
407 if (dev->header_ops->cache)
408 neigh->ops = &ndisc_hh_ops;
410 neigh->ops = &ndisc_generic_ops;
411 if (neigh->nud_state&NUD_VALID)
412 neigh->output = neigh->ops->connected_output;
414 neigh->output = neigh->ops->output;
416 in6_dev_put(in6_dev);
420 static int pndisc_constructor(struct pneigh_entry *n)
422 struct in6_addr *addr = (struct in6_addr*)&n->key;
423 struct in6_addr maddr;
424 struct net_device *dev = n->dev;
426 if (dev == NULL || __in6_dev_get(dev) == NULL)
428 addrconf_addr_solict_mult(addr, &maddr);
429 ipv6_dev_mc_inc(dev, &maddr);
433 static void pndisc_destructor(struct pneigh_entry *n)
435 struct in6_addr *addr = (struct in6_addr*)&n->key;
436 struct in6_addr maddr;
437 struct net_device *dev = n->dev;
439 if (dev == NULL || __in6_dev_get(dev) == NULL)
441 addrconf_addr_solict_mult(addr, &maddr);
442 ipv6_dev_mc_dec(dev, &maddr);
445 struct sk_buff *ndisc_build_skb(struct net_device *dev,
446 const struct in6_addr *daddr,
447 const struct in6_addr *saddr,
448 struct icmp6hdr *icmp6h,
449 const struct in6_addr *target,
452 struct net *net = dev_net(dev);
453 struct sock *sk = net->ipv6.ndisc_sk;
455 struct icmp6hdr *hdr;
463 len = sizeof(struct icmp6hdr) + (target ? sizeof(*target) : 0);
465 len += ndisc_opt_addr_space(dev);
467 skb = sock_alloc_send_skb(sk,
468 (MAX_HEADER + sizeof(struct ipv6hdr) +
469 len + LL_ALLOCATED_SPACE(dev)),
473 "ICMPv6 ND: %s() failed to allocate an skb, err=%d.\n",
478 skb_reserve(skb, LL_RESERVED_SPACE(dev));
479 ip6_nd_hdr(sk, skb, dev, saddr, daddr, IPPROTO_ICMPV6, len);
481 skb->transport_header = skb->tail;
484 hdr = (struct icmp6hdr *)skb_transport_header(skb);
485 memcpy(hdr, icmp6h, sizeof(*hdr));
487 opt = skb_transport_header(skb) + sizeof(struct icmp6hdr);
489 ipv6_addr_copy((struct in6_addr *)opt, target);
490 opt += sizeof(*target);
494 ndisc_fill_addr_option(opt, llinfo, dev->dev_addr,
495 dev->addr_len, dev->type);
497 hdr->icmp6_cksum = csum_ipv6_magic(saddr, daddr, len,
505 EXPORT_SYMBOL(ndisc_build_skb);
507 void ndisc_send_skb(struct sk_buff *skb,
508 struct net_device *dev,
509 struct neighbour *neigh,
510 const struct in6_addr *daddr,
511 const struct in6_addr *saddr,
512 struct icmp6hdr *icmp6h)
515 struct dst_entry *dst;
516 struct net *net = dev_net(dev);
517 struct sock *sk = net->ipv6.ndisc_sk;
518 struct inet6_dev *idev;
522 type = icmp6h->icmp6_type;
524 icmpv6_flow_init(sk, &fl, type, saddr, daddr, dev->ifindex);
526 dst = icmp6_dst_alloc(dev, neigh, daddr);
532 err = xfrm_lookup(net, &dst, &fl, NULL, 0);
538 skb_dst_set(skb, dst);
540 idev = in6_dev_get(dst->dev);
541 IP6_UPD_PO_STATS(net, idev, IPSTATS_MIB_OUT, skb->len);
543 err = NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT, skb, NULL, dst->dev,
546 ICMP6MSGOUT_INC_STATS(net, idev, type);
547 ICMP6_INC_STATS(net, idev, ICMP6_MIB_OUTMSGS);
550 if (likely(idev != NULL))
554 EXPORT_SYMBOL(ndisc_send_skb);
557 * Send a Neighbour Discover packet
559 static void __ndisc_send(struct net_device *dev,
560 struct neighbour *neigh,
561 const struct in6_addr *daddr,
562 const struct in6_addr *saddr,
563 struct icmp6hdr *icmp6h, const struct in6_addr *target,
568 skb = ndisc_build_skb(dev, daddr, saddr, icmp6h, target, llinfo);
572 ndisc_send_skb(skb, dev, neigh, daddr, saddr, icmp6h);
575 static void ndisc_send_na(struct net_device *dev, struct neighbour *neigh,
576 const struct in6_addr *daddr,
577 const struct in6_addr *solicited_addr,
578 int router, int solicited, int override, int inc_opt)
580 struct in6_addr tmpaddr;
581 struct inet6_ifaddr *ifp;
582 const struct in6_addr *src_addr;
583 struct icmp6hdr icmp6h = {
584 .icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT,
587 /* for anycast or proxy, solicited_addr != src_addr */
588 ifp = ipv6_get_ifaddr(dev_net(dev), solicited_addr, dev, 1);
590 src_addr = solicited_addr;
591 if (ifp->flags & IFA_F_OPTIMISTIC)
593 inc_opt |= ifp->idev->cnf.force_tllao;
596 if (ipv6_dev_get_saddr(dev_net(dev), dev, daddr,
597 inet6_sk(dev_net(dev)->ipv6.ndisc_sk)->srcprefs,
603 icmp6h.icmp6_router = router;
604 icmp6h.icmp6_solicited = solicited;
605 icmp6h.icmp6_override = override;
607 __ndisc_send(dev, neigh, daddr, src_addr,
608 &icmp6h, solicited_addr,
609 inc_opt ? ND_OPT_TARGET_LL_ADDR : 0);
612 void ndisc_send_ns(struct net_device *dev, struct neighbour *neigh,
613 const struct in6_addr *solicit,
614 const struct in6_addr *daddr, const struct in6_addr *saddr)
616 struct in6_addr addr_buf;
617 struct icmp6hdr icmp6h = {
618 .icmp6_type = NDISC_NEIGHBOUR_SOLICITATION,
622 if (ipv6_get_lladdr(dev, &addr_buf,
623 (IFA_F_TENTATIVE|IFA_F_OPTIMISTIC)))
628 __ndisc_send(dev, neigh, daddr, saddr,
630 !ipv6_addr_any(saddr) ? ND_OPT_SOURCE_LL_ADDR : 0);
633 void ndisc_send_rs(struct net_device *dev, const struct in6_addr *saddr,
634 const struct in6_addr *daddr)
636 struct icmp6hdr icmp6h = {
637 .icmp6_type = NDISC_ROUTER_SOLICITATION,
639 int send_sllao = dev->addr_len;
641 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
643 * According to section 2.2 of RFC 4429, we must not
644 * send router solicitations with a sllao from
645 * optimistic addresses, but we may send the solicitation
646 * if we don't include the sllao. So here we check
647 * if our address is optimistic, and if so, we
648 * suppress the inclusion of the sllao.
651 struct inet6_ifaddr *ifp = ipv6_get_ifaddr(dev_net(dev), saddr,
654 if (ifp->flags & IFA_F_OPTIMISTIC) {
663 __ndisc_send(dev, NULL, daddr, saddr,
665 send_sllao ? ND_OPT_SOURCE_LL_ADDR : 0);
669 static void ndisc_error_report(struct neighbour *neigh, struct sk_buff *skb)
672 * "The sender MUST return an ICMP
673 * destination unreachable"
675 dst_link_failure(skb);
679 /* Called with locked neigh: either read or both */
681 static void ndisc_solicit(struct neighbour *neigh, struct sk_buff *skb)
683 struct in6_addr *saddr = NULL;
684 struct in6_addr mcaddr;
685 struct net_device *dev = neigh->dev;
686 struct in6_addr *target = (struct in6_addr *)&neigh->primary_key;
687 int probes = atomic_read(&neigh->probes);
689 if (skb && ipv6_chk_addr(dev_net(dev), &ipv6_hdr(skb)->saddr, dev, 1))
690 saddr = &ipv6_hdr(skb)->saddr;
692 if ((probes -= neigh->parms->ucast_probes) < 0) {
693 if (!(neigh->nud_state & NUD_VALID)) {
694 ND_PRINTK1(KERN_DEBUG "%s(): trying to ucast probe in NUD_INVALID: %pI6\n",
697 ndisc_send_ns(dev, neigh, target, target, saddr);
698 } else if ((probes -= neigh->parms->app_probes) < 0) {
703 addrconf_addr_solict_mult(target, &mcaddr);
704 ndisc_send_ns(dev, NULL, target, &mcaddr, saddr);
708 static int pndisc_is_router(const void *pkey,
709 struct net_device *dev)
711 struct pneigh_entry *n;
714 read_lock_bh(&nd_tbl.lock);
715 n = __pneigh_lookup(&nd_tbl, dev_net(dev), pkey, dev);
717 ret = !!(n->flags & NTF_ROUTER);
718 read_unlock_bh(&nd_tbl.lock);
723 static void ndisc_recv_ns(struct sk_buff *skb)
725 struct nd_msg *msg = (struct nd_msg *)skb_transport_header(skb);
726 struct in6_addr *saddr = &ipv6_hdr(skb)->saddr;
727 struct in6_addr *daddr = &ipv6_hdr(skb)->daddr;
729 u32 ndoptlen = skb->tail - (skb->transport_header +
730 offsetof(struct nd_msg, opt));
731 struct ndisc_options ndopts;
732 struct net_device *dev = skb->dev;
733 struct inet6_ifaddr *ifp;
734 struct inet6_dev *idev = NULL;
735 struct neighbour *neigh;
736 int dad = ipv6_addr_any(saddr);
740 if (ipv6_addr_is_multicast(&msg->target)) {
741 ND_PRINTK2(KERN_WARNING
742 "ICMPv6 NS: multicast target address");
748 * DAD has to be destined for solicited node multicast address.
751 !(daddr->s6_addr32[0] == htonl(0xff020000) &&
752 daddr->s6_addr32[1] == htonl(0x00000000) &&
753 daddr->s6_addr32[2] == htonl(0x00000001) &&
754 daddr->s6_addr [12] == 0xff )) {
755 ND_PRINTK2(KERN_WARNING
756 "ICMPv6 NS: bad DAD packet (wrong destination)\n");
760 if (!ndisc_parse_options(msg->opt, ndoptlen, &ndopts)) {
761 ND_PRINTK2(KERN_WARNING
762 "ICMPv6 NS: invalid ND options\n");
766 if (ndopts.nd_opts_src_lladdr) {
767 lladdr = ndisc_opt_addr_data(ndopts.nd_opts_src_lladdr, dev);
769 ND_PRINTK2(KERN_WARNING
770 "ICMPv6 NS: invalid link-layer address length\n");
775 * If the IP source address is the unspecified address,
776 * there MUST NOT be source link-layer address option
780 ND_PRINTK2(KERN_WARNING
781 "ICMPv6 NS: bad DAD packet (link-layer address option)\n");
786 inc = ipv6_addr_is_multicast(daddr);
788 ifp = ipv6_get_ifaddr(dev_net(dev), &msg->target, dev, 1);
791 if (ifp->flags & (IFA_F_TENTATIVE|IFA_F_OPTIMISTIC)) {
793 if (dev->type == ARPHRD_IEEE802_TR) {
794 const unsigned char *sadr;
795 sadr = skb_mac_header(skb);
796 if (((sadr[8] ^ dev->dev_addr[0]) & 0x7f) == 0 &&
797 sadr[9] == dev->dev_addr[1] &&
798 sadr[10] == dev->dev_addr[2] &&
799 sadr[11] == dev->dev_addr[3] &&
800 sadr[12] == dev->dev_addr[4] &&
801 sadr[13] == dev->dev_addr[5]) {
802 /* looped-back to us */
808 * We are colliding with another node
810 * so fail our DAD process
812 addrconf_dad_failure(ifp);
816 * This is not a dad solicitation.
817 * If we are an optimistic node,
819 * Otherwise, we should ignore it.
821 if (!(ifp->flags & IFA_F_OPTIMISTIC))
828 struct net *net = dev_net(dev);
830 idev = in6_dev_get(dev);
832 /* XXX: count this drop? */
836 if (ipv6_chk_acast_addr(net, dev, &msg->target) ||
837 (idev->cnf.forwarding &&
838 (net->ipv6.devconf_all->proxy_ndp || idev->cnf.proxy_ndp) &&
839 (is_router = pndisc_is_router(&msg->target, dev)) >= 0)) {
840 if (!(NEIGH_CB(skb)->flags & LOCALLY_ENQUEUED) &&
841 skb->pkt_type != PACKET_HOST &&
843 idev->nd_parms->proxy_delay != 0) {
845 * for anycast or proxy,
846 * sender should delay its response
847 * by a random time between 0 and
848 * MAX_ANYCAST_DELAY_TIME seconds.
849 * (RFC2461) -- yoshfuji
851 struct sk_buff *n = skb_clone(skb, GFP_ATOMIC);
853 pneigh_enqueue(&nd_tbl, idev->nd_parms, n);
861 is_router = !!idev->cnf.forwarding;
864 ndisc_send_na(dev, NULL, &in6addr_linklocal_allnodes, &msg->target,
865 is_router, 0, (ifp != NULL), 1);
870 NEIGH_CACHE_STAT_INC(&nd_tbl, rcv_probes_mcast);
872 NEIGH_CACHE_STAT_INC(&nd_tbl, rcv_probes_ucast);
875 * update / create cache entry
876 * for the source address
878 neigh = __neigh_lookup(&nd_tbl, saddr, dev,
879 !inc || lladdr || !dev->addr_len);
881 neigh_update(neigh, lladdr, NUD_STALE,
882 NEIGH_UPDATE_F_WEAK_OVERRIDE|
883 NEIGH_UPDATE_F_OVERRIDE);
884 if (neigh || !dev->header_ops) {
885 ndisc_send_na(dev, neigh, saddr, &msg->target,
887 1, (ifp != NULL && inc), inc);
889 neigh_release(neigh);
899 static void ndisc_recv_na(struct sk_buff *skb)
901 struct nd_msg *msg = (struct nd_msg *)skb_transport_header(skb);
902 struct in6_addr *saddr = &ipv6_hdr(skb)->saddr;
903 struct in6_addr *daddr = &ipv6_hdr(skb)->daddr;
905 u32 ndoptlen = skb->tail - (skb->transport_header +
906 offsetof(struct nd_msg, opt));
907 struct ndisc_options ndopts;
908 struct net_device *dev = skb->dev;
909 struct inet6_ifaddr *ifp;
910 struct neighbour *neigh;
912 if (skb->len < sizeof(struct nd_msg)) {
913 ND_PRINTK2(KERN_WARNING
914 "ICMPv6 NA: packet too short\n");
918 if (ipv6_addr_is_multicast(&msg->target)) {
919 ND_PRINTK2(KERN_WARNING
920 "ICMPv6 NA: target address is multicast.\n");
924 if (ipv6_addr_is_multicast(daddr) &&
925 msg->icmph.icmp6_solicited) {
926 ND_PRINTK2(KERN_WARNING
927 "ICMPv6 NA: solicited NA is multicasted.\n");
931 if (!ndisc_parse_options(msg->opt, ndoptlen, &ndopts)) {
932 ND_PRINTK2(KERN_WARNING
933 "ICMPv6 NS: invalid ND option\n");
936 if (ndopts.nd_opts_tgt_lladdr) {
937 lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr, dev);
939 ND_PRINTK2(KERN_WARNING
940 "ICMPv6 NA: invalid link-layer address length\n");
944 ifp = ipv6_get_ifaddr(dev_net(dev), &msg->target, dev, 1);
946 if (ifp->flags & IFA_F_TENTATIVE) {
947 addrconf_dad_failure(ifp);
950 /* What should we make now? The advertisement
951 is invalid, but ndisc specs say nothing
952 about it. It could be misconfiguration, or
953 an smart proxy agent tries to help us :-)
955 We should not print the error if NA has been
956 received from loopback - it is just our own
957 unsolicited advertisement.
959 if (skb->pkt_type != PACKET_LOOPBACK)
960 ND_PRINTK1(KERN_WARNING
961 "ICMPv6 NA: someone advertises our address %pI6 on %s!\n",
962 &ifp->addr, ifp->idev->dev->name);
966 neigh = neigh_lookup(&nd_tbl, &msg->target, dev);
969 u8 old_flags = neigh->flags;
970 struct net *net = dev_net(dev);
972 if (neigh->nud_state & NUD_FAILED)
976 * Don't update the neighbor cache entry on a proxy NA from
977 * ourselves because either the proxied node is off link or it
978 * has already sent a NA to us.
980 if (lladdr && !memcmp(lladdr, dev->dev_addr, dev->addr_len) &&
981 net->ipv6.devconf_all->forwarding && net->ipv6.devconf_all->proxy_ndp &&
982 pneigh_lookup(&nd_tbl, net, &msg->target, dev, 0)) {
983 /* XXX: idev->cnf.prixy_ndp */
987 neigh_update(neigh, lladdr,
988 msg->icmph.icmp6_solicited ? NUD_REACHABLE : NUD_STALE,
989 NEIGH_UPDATE_F_WEAK_OVERRIDE|
990 (msg->icmph.icmp6_override ? NEIGH_UPDATE_F_OVERRIDE : 0)|
991 NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
992 (msg->icmph.icmp6_router ? NEIGH_UPDATE_F_ISROUTER : 0));
994 if ((old_flags & ~neigh->flags) & NTF_ROUTER) {
996 * Change: router to host
999 rt = rt6_get_dflt_router(saddr, dev);
1005 neigh_release(neigh);
1009 static void ndisc_recv_rs(struct sk_buff *skb)
1011 struct rs_msg *rs_msg = (struct rs_msg *)skb_transport_header(skb);
1012 unsigned long ndoptlen = skb->len - sizeof(*rs_msg);
1013 struct neighbour *neigh;
1014 struct inet6_dev *idev;
1015 struct in6_addr *saddr = &ipv6_hdr(skb)->saddr;
1016 struct ndisc_options ndopts;
1019 if (skb->len < sizeof(*rs_msg))
1022 idev = in6_dev_get(skb->dev);
1024 if (net_ratelimit())
1025 ND_PRINTK1("ICMP6 RS: can't find in6 device\n");
1029 /* Don't accept RS if we're not in router mode */
1030 if (!idev->cnf.forwarding)
1034 * Don't update NCE if src = ::;
1035 * this implies that the source node has no ip address assigned yet.
1037 if (ipv6_addr_any(saddr))
1040 /* Parse ND options */
1041 if (!ndisc_parse_options(rs_msg->opt, ndoptlen, &ndopts)) {
1042 if (net_ratelimit())
1043 ND_PRINTK2("ICMP6 NS: invalid ND option, ignored\n");
1047 if (ndopts.nd_opts_src_lladdr) {
1048 lladdr = ndisc_opt_addr_data(ndopts.nd_opts_src_lladdr,
1054 neigh = __neigh_lookup(&nd_tbl, saddr, skb->dev, 1);
1056 neigh_update(neigh, lladdr, NUD_STALE,
1057 NEIGH_UPDATE_F_WEAK_OVERRIDE|
1058 NEIGH_UPDATE_F_OVERRIDE|
1059 NEIGH_UPDATE_F_OVERRIDE_ISROUTER);
1060 neigh_release(neigh);
1066 static void ndisc_ra_useropt(struct sk_buff *ra, struct nd_opt_hdr *opt)
1068 struct icmp6hdr *icmp6h = (struct icmp6hdr *)skb_transport_header(ra);
1069 struct sk_buff *skb;
1070 struct nlmsghdr *nlh;
1071 struct nduseroptmsg *ndmsg;
1072 struct net *net = dev_net(ra->dev);
1074 int base_size = NLMSG_ALIGN(sizeof(struct nduseroptmsg)
1075 + (opt->nd_opt_len << 3));
1076 size_t msg_size = base_size + nla_total_size(sizeof(struct in6_addr));
1078 skb = nlmsg_new(msg_size, GFP_ATOMIC);
1084 nlh = nlmsg_put(skb, 0, 0, RTM_NEWNDUSEROPT, base_size, 0);
1086 goto nla_put_failure;
1089 ndmsg = nlmsg_data(nlh);
1090 ndmsg->nduseropt_family = AF_INET6;
1091 ndmsg->nduseropt_ifindex = ra->dev->ifindex;
1092 ndmsg->nduseropt_icmp_type = icmp6h->icmp6_type;
1093 ndmsg->nduseropt_icmp_code = icmp6h->icmp6_code;
1094 ndmsg->nduseropt_opts_len = opt->nd_opt_len << 3;
1096 memcpy(ndmsg + 1, opt, opt->nd_opt_len << 3);
1098 NLA_PUT(skb, NDUSEROPT_SRCADDR, sizeof(struct in6_addr),
1099 &ipv6_hdr(ra)->saddr);
1100 nlmsg_end(skb, nlh);
1102 rtnl_notify(skb, net, 0, RTNLGRP_ND_USEROPT, NULL, GFP_ATOMIC);
1109 rtnl_set_sk_err(net, RTNLGRP_ND_USEROPT, err);
1112 static inline int accept_ra(struct inet6_dev *in6_dev)
1115 * If forwarding is enabled, RA are not accepted unless the special
1116 * hybrid mode (accept_ra=2) is enabled.
1118 if (in6_dev->cnf.forwarding && in6_dev->cnf.accept_ra < 2)
1121 return in6_dev->cnf.accept_ra;
1124 static void ndisc_router_discovery(struct sk_buff *skb)
1126 struct ra_msg *ra_msg = (struct ra_msg *)skb_transport_header(skb);
1127 struct neighbour *neigh = NULL;
1128 struct inet6_dev *in6_dev;
1129 struct rt6_info *rt = NULL;
1131 struct ndisc_options ndopts;
1133 unsigned int pref = 0;
1135 __u8 * opt = (__u8 *)(ra_msg + 1);
1137 optlen = (skb->tail - skb->transport_header) - sizeof(struct ra_msg);
1139 if (!(ipv6_addr_type(&ipv6_hdr(skb)->saddr) & IPV6_ADDR_LINKLOCAL)) {
1140 ND_PRINTK2(KERN_WARNING
1141 "ICMPv6 RA: source address is not link-local.\n");
1145 ND_PRINTK2(KERN_WARNING
1146 "ICMPv6 RA: packet too short\n");
1150 #ifdef CONFIG_IPV6_NDISC_NODETYPE
1151 if (skb->ndisc_nodetype == NDISC_NODETYPE_HOST) {
1152 ND_PRINTK2(KERN_WARNING
1153 "ICMPv6 RA: from host or unauthorized router\n");
1159 * set the RA_RECV flag in the interface
1162 in6_dev = in6_dev_get(skb->dev);
1163 if (in6_dev == NULL) {
1165 "ICMPv6 RA: can't find inet6 device for %s.\n",
1170 if (!ndisc_parse_options(opt, optlen, &ndopts)) {
1171 in6_dev_put(in6_dev);
1172 ND_PRINTK2(KERN_WARNING
1173 "ICMP6 RA: invalid ND options\n");
1177 if (!accept_ra(in6_dev))
1178 goto skip_linkparms;
1180 #ifdef CONFIG_IPV6_NDISC_NODETYPE
1181 /* skip link-specific parameters from interior routers */
1182 if (skb->ndisc_nodetype == NDISC_NODETYPE_NODEFAULT)
1183 goto skip_linkparms;
1186 if (in6_dev->if_flags & IF_RS_SENT) {
1188 * flag that an RA was received after an RS was sent
1189 * out on this interface.
1191 in6_dev->if_flags |= IF_RA_RCVD;
1195 * Remember the managed/otherconf flags from most recently
1196 * received RA message (RFC 2462) -- yoshfuji
1198 in6_dev->if_flags = (in6_dev->if_flags & ~(IF_RA_MANAGED |
1200 (ra_msg->icmph.icmp6_addrconf_managed ?
1201 IF_RA_MANAGED : 0) |
1202 (ra_msg->icmph.icmp6_addrconf_other ?
1203 IF_RA_OTHERCONF : 0);
1205 if (!in6_dev->cnf.accept_ra_defrtr)
1208 lifetime = ntohs(ra_msg->icmph.icmp6_rt_lifetime);
1210 #ifdef CONFIG_IPV6_ROUTER_PREF
1211 pref = ra_msg->icmph.icmp6_router_pref;
1212 /* 10b is handled as if it were 00b (medium) */
1213 if (pref == ICMPV6_ROUTER_PREF_INVALID ||
1214 !in6_dev->cnf.accept_ra_rtr_pref)
1215 pref = ICMPV6_ROUTER_PREF_MEDIUM;
1218 rt = rt6_get_dflt_router(&ipv6_hdr(skb)->saddr, skb->dev);
1221 neigh = rt->rt6i_nexthop;
1223 if (rt && lifetime == 0) {
1229 if (rt == NULL && lifetime) {
1230 ND_PRINTK3(KERN_DEBUG
1231 "ICMPv6 RA: adding default router.\n");
1233 rt = rt6_add_dflt_router(&ipv6_hdr(skb)->saddr, skb->dev, pref);
1236 "ICMPv6 RA: %s() failed to add default route.\n",
1238 in6_dev_put(in6_dev);
1242 neigh = rt->rt6i_nexthop;
1243 if (neigh == NULL) {
1245 "ICMPv6 RA: %s() got default router without neighbour.\n",
1247 dst_release(&rt->dst);
1248 in6_dev_put(in6_dev);
1251 neigh->flags |= NTF_ROUTER;
1253 rt->rt6i_flags = (rt->rt6i_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
1257 rt->rt6i_expires = jiffies + (HZ * lifetime);
1259 if (ra_msg->icmph.icmp6_hop_limit) {
1260 in6_dev->cnf.hop_limit = ra_msg->icmph.icmp6_hop_limit;
1262 rt->dst.metrics[RTAX_HOPLIMIT-1] = ra_msg->icmph.icmp6_hop_limit;
1268 * Update Reachable Time and Retrans Timer
1271 if (in6_dev->nd_parms) {
1272 unsigned long rtime = ntohl(ra_msg->retrans_timer);
1274 if (rtime && rtime/1000 < MAX_SCHEDULE_TIMEOUT/HZ) {
1275 rtime = (rtime*HZ)/1000;
1278 in6_dev->nd_parms->retrans_time = rtime;
1279 in6_dev->tstamp = jiffies;
1280 inet6_ifinfo_notify(RTM_NEWLINK, in6_dev);
1283 rtime = ntohl(ra_msg->reachable_time);
1284 if (rtime && rtime/1000 < MAX_SCHEDULE_TIMEOUT/(3*HZ)) {
1285 rtime = (rtime*HZ)/1000;
1290 if (rtime != in6_dev->nd_parms->base_reachable_time) {
1291 in6_dev->nd_parms->base_reachable_time = rtime;
1292 in6_dev->nd_parms->gc_staletime = 3 * rtime;
1293 in6_dev->nd_parms->reachable_time = neigh_rand_reach_time(rtime);
1294 in6_dev->tstamp = jiffies;
1295 inet6_ifinfo_notify(RTM_NEWLINK, in6_dev);
1307 neigh = __neigh_lookup(&nd_tbl, &ipv6_hdr(skb)->saddr,
1311 if (ndopts.nd_opts_src_lladdr) {
1312 lladdr = ndisc_opt_addr_data(ndopts.nd_opts_src_lladdr,
1315 ND_PRINTK2(KERN_WARNING
1316 "ICMPv6 RA: invalid link-layer address length\n");
1320 neigh_update(neigh, lladdr, NUD_STALE,
1321 NEIGH_UPDATE_F_WEAK_OVERRIDE|
1322 NEIGH_UPDATE_F_OVERRIDE|
1323 NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
1324 NEIGH_UPDATE_F_ISROUTER);
1327 if (!accept_ra(in6_dev))
1330 #ifdef CONFIG_IPV6_ROUTE_INFO
1331 if (in6_dev->cnf.accept_ra_rtr_pref && ndopts.nd_opts_ri) {
1332 struct nd_opt_hdr *p;
1333 for (p = ndopts.nd_opts_ri;
1335 p = ndisc_next_option(p, ndopts.nd_opts_ri_end)) {
1336 struct route_info *ri = (struct route_info *)p;
1337 #ifdef CONFIG_IPV6_NDISC_NODETYPE
1338 if (skb->ndisc_nodetype == NDISC_NODETYPE_NODEFAULT &&
1339 ri->prefix_len == 0)
1342 if (ri->prefix_len > in6_dev->cnf.accept_ra_rt_info_max_plen)
1344 rt6_route_rcv(skb->dev, (u8*)p, (p->nd_opt_len) << 3,
1345 &ipv6_hdr(skb)->saddr);
1350 #ifdef CONFIG_IPV6_NDISC_NODETYPE
1351 /* skip link-specific ndopts from interior routers */
1352 if (skb->ndisc_nodetype == NDISC_NODETYPE_NODEFAULT)
1356 if (in6_dev->cnf.accept_ra_pinfo && ndopts.nd_opts_pi) {
1357 struct nd_opt_hdr *p;
1358 for (p = ndopts.nd_opts_pi;
1360 p = ndisc_next_option(p, ndopts.nd_opts_pi_end)) {
1361 addrconf_prefix_rcv(skb->dev, (u8*)p, (p->nd_opt_len) << 3);
1365 if (ndopts.nd_opts_mtu) {
1369 memcpy(&n, ((u8*)(ndopts.nd_opts_mtu+1))+2, sizeof(mtu));
1372 if (mtu < IPV6_MIN_MTU || mtu > skb->dev->mtu) {
1373 ND_PRINTK2(KERN_WARNING
1374 "ICMPv6 RA: invalid mtu: %d\n",
1376 } else if (in6_dev->cnf.mtu6 != mtu) {
1377 in6_dev->cnf.mtu6 = mtu;
1380 rt->dst.metrics[RTAX_MTU-1] = mtu;
1382 rt6_mtu_change(skb->dev, mtu);
1386 if (ndopts.nd_useropts) {
1387 struct nd_opt_hdr *p;
1388 for (p = ndopts.nd_useropts;
1390 p = ndisc_next_useropt(p, ndopts.nd_useropts_end)) {
1391 ndisc_ra_useropt(skb, p);
1395 if (ndopts.nd_opts_tgt_lladdr || ndopts.nd_opts_rh) {
1396 ND_PRINTK2(KERN_WARNING
1397 "ICMPv6 RA: invalid RA options");
1401 dst_release(&rt->dst);
1403 neigh_release(neigh);
1404 in6_dev_put(in6_dev);
1407 static void ndisc_redirect_rcv(struct sk_buff *skb)
1409 struct inet6_dev *in6_dev;
1410 struct icmp6hdr *icmph;
1411 struct in6_addr *dest;
1412 struct in6_addr *target; /* new first hop to destination */
1413 struct neighbour *neigh;
1415 struct ndisc_options ndopts;
1419 #ifdef CONFIG_IPV6_NDISC_NODETYPE
1420 switch (skb->ndisc_nodetype) {
1421 case NDISC_NODETYPE_HOST:
1422 case NDISC_NODETYPE_NODEFAULT:
1423 ND_PRINTK2(KERN_WARNING
1424 "ICMPv6 Redirect: from host or unauthorized router\n");
1429 if (!(ipv6_addr_type(&ipv6_hdr(skb)->saddr) & IPV6_ADDR_LINKLOCAL)) {
1430 ND_PRINTK2(KERN_WARNING
1431 "ICMPv6 Redirect: source address is not link-local.\n");
1435 optlen = skb->tail - skb->transport_header;
1436 optlen -= sizeof(struct icmp6hdr) + 2 * sizeof(struct in6_addr);
1439 ND_PRINTK2(KERN_WARNING
1440 "ICMPv6 Redirect: packet too short\n");
1444 icmph = icmp6_hdr(skb);
1445 target = (struct in6_addr *) (icmph + 1);
1448 if (ipv6_addr_is_multicast(dest)) {
1449 ND_PRINTK2(KERN_WARNING
1450 "ICMPv6 Redirect: destination address is multicast.\n");
1454 if (ipv6_addr_equal(dest, target)) {
1456 } else if (ipv6_addr_type(target) !=
1457 (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) {
1458 ND_PRINTK2(KERN_WARNING
1459 "ICMPv6 Redirect: target address is not link-local unicast.\n");
1463 in6_dev = in6_dev_get(skb->dev);
1466 if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects) {
1467 in6_dev_put(in6_dev);
1472 * The IP source address of the Redirect MUST be the same as the current
1473 * first-hop router for the specified ICMP Destination Address.
1476 if (!ndisc_parse_options((u8*)(dest + 1), optlen, &ndopts)) {
1477 ND_PRINTK2(KERN_WARNING
1478 "ICMPv6 Redirect: invalid ND options\n");
1479 in6_dev_put(in6_dev);
1482 if (ndopts.nd_opts_tgt_lladdr) {
1483 lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr,
1486 ND_PRINTK2(KERN_WARNING
1487 "ICMPv6 Redirect: invalid link-layer address length\n");
1488 in6_dev_put(in6_dev);
1493 neigh = __neigh_lookup(&nd_tbl, target, skb->dev, 1);
1495 rt6_redirect(dest, &ipv6_hdr(skb)->daddr,
1496 &ipv6_hdr(skb)->saddr, neigh, lladdr,
1498 neigh_release(neigh);
1500 in6_dev_put(in6_dev);
1503 void ndisc_send_redirect(struct sk_buff *skb, struct neighbour *neigh,
1504 const struct in6_addr *target)
1506 struct net_device *dev = skb->dev;
1507 struct net *net = dev_net(dev);
1508 struct sock *sk = net->ipv6.ndisc_sk;
1509 int len = sizeof(struct icmp6hdr) + 2 * sizeof(struct in6_addr);
1510 struct sk_buff *buff;
1511 struct icmp6hdr *icmph;
1512 struct in6_addr saddr_buf;
1513 struct in6_addr *addrp;
1514 struct rt6_info *rt;
1515 struct dst_entry *dst;
1516 struct inet6_dev *idev;
1521 u8 ha_buf[MAX_ADDR_LEN], *ha = NULL;
1523 if (ipv6_get_lladdr(dev, &saddr_buf, IFA_F_TENTATIVE)) {
1524 ND_PRINTK2(KERN_WARNING
1525 "ICMPv6 Redirect: no link-local address on %s\n",
1530 if (!ipv6_addr_equal(&ipv6_hdr(skb)->daddr, target) &&
1531 ipv6_addr_type(target) != (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) {
1532 ND_PRINTK2(KERN_WARNING
1533 "ICMPv6 Redirect: target address is not link-local unicast.\n");
1537 icmpv6_flow_init(sk, &fl, NDISC_REDIRECT,
1538 &saddr_buf, &ipv6_hdr(skb)->saddr, dev->ifindex);
1540 dst = ip6_route_output(net, NULL, &fl);
1544 err = xfrm_lookup(net, &dst, &fl, NULL, 0);
1548 rt = (struct rt6_info *) dst;
1550 if (rt->rt6i_flags & RTF_GATEWAY) {
1551 ND_PRINTK2(KERN_WARNING
1552 "ICMPv6 Redirect: destination is not a neighbour.\n");
1555 if (!xrlim_allow(dst, 1*HZ))
1558 if (dev->addr_len) {
1559 read_lock_bh(&neigh->lock);
1560 if (neigh->nud_state & NUD_VALID) {
1561 memcpy(ha_buf, neigh->ha, dev->addr_len);
1562 read_unlock_bh(&neigh->lock);
1564 len += ndisc_opt_addr_space(dev);
1566 read_unlock_bh(&neigh->lock);
1569 rd_len = min_t(unsigned int,
1570 IPV6_MIN_MTU-sizeof(struct ipv6hdr)-len, skb->len + 8);
1574 buff = sock_alloc_send_skb(sk,
1575 (MAX_HEADER + sizeof(struct ipv6hdr) +
1576 len + LL_ALLOCATED_SPACE(dev)),
1580 "ICMPv6 Redirect: %s() failed to allocate an skb, err=%d.\n",
1585 skb_reserve(buff, LL_RESERVED_SPACE(dev));
1586 ip6_nd_hdr(sk, buff, dev, &saddr_buf, &ipv6_hdr(skb)->saddr,
1587 IPPROTO_ICMPV6, len);
1589 skb_set_transport_header(buff, skb_tail_pointer(buff) - buff->data);
1591 icmph = icmp6_hdr(buff);
1593 memset(icmph, 0, sizeof(struct icmp6hdr));
1594 icmph->icmp6_type = NDISC_REDIRECT;
1597 * copy target and destination addresses
1600 addrp = (struct in6_addr *)(icmph + 1);
1601 ipv6_addr_copy(addrp, target);
1603 ipv6_addr_copy(addrp, &ipv6_hdr(skb)->daddr);
1605 opt = (u8*) (addrp + 1);
1608 * include target_address option
1612 opt = ndisc_fill_addr_option(opt, ND_OPT_TARGET_LL_ADDR, ha,
1613 dev->addr_len, dev->type);
1616 * build redirect option and copy skb over to the new packet.
1620 *(opt++) = ND_OPT_REDIRECT_HDR;
1621 *(opt++) = (rd_len >> 3);
1624 memcpy(opt, ipv6_hdr(skb), rd_len - 8);
1626 icmph->icmp6_cksum = csum_ipv6_magic(&saddr_buf, &ipv6_hdr(skb)->saddr,
1627 len, IPPROTO_ICMPV6,
1628 csum_partial(icmph, len, 0));
1630 skb_dst_set(buff, dst);
1631 idev = in6_dev_get(dst->dev);
1632 IP6_UPD_PO_STATS(net, idev, IPSTATS_MIB_OUT, skb->len);
1633 err = NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT, buff, NULL, dst->dev,
1636 ICMP6MSGOUT_INC_STATS(net, idev, NDISC_REDIRECT);
1637 ICMP6_INC_STATS(net, idev, ICMP6_MIB_OUTMSGS);
1640 if (likely(idev != NULL))
1648 static void pndisc_redo(struct sk_buff *skb)
1654 int ndisc_rcv(struct sk_buff *skb)
1658 if (!pskb_may_pull(skb, skb->len))
1661 msg = (struct nd_msg *)skb_transport_header(skb);
1663 __skb_push(skb, skb->data - skb_transport_header(skb));
1665 if (ipv6_hdr(skb)->hop_limit != 255) {
1666 ND_PRINTK2(KERN_WARNING
1667 "ICMPv6 NDISC: invalid hop-limit: %d\n",
1668 ipv6_hdr(skb)->hop_limit);
1672 if (msg->icmph.icmp6_code != 0) {
1673 ND_PRINTK2(KERN_WARNING
1674 "ICMPv6 NDISC: invalid ICMPv6 code: %d\n",
1675 msg->icmph.icmp6_code);
1679 memset(NEIGH_CB(skb), 0, sizeof(struct neighbour_cb));
1681 switch (msg->icmph.icmp6_type) {
1682 case NDISC_NEIGHBOUR_SOLICITATION:
1686 case NDISC_NEIGHBOUR_ADVERTISEMENT:
1690 case NDISC_ROUTER_SOLICITATION:
1694 case NDISC_ROUTER_ADVERTISEMENT:
1695 ndisc_router_discovery(skb);
1698 case NDISC_REDIRECT:
1699 ndisc_redirect_rcv(skb);
1706 static int ndisc_netdev_event(struct notifier_block *this, unsigned long event, void *ptr)
1708 struct net_device *dev = ptr;
1709 struct net *net = dev_net(dev);
1712 case NETDEV_CHANGEADDR:
1713 neigh_changeaddr(&nd_tbl, dev);
1714 fib6_run_gc(~0UL, net);
1717 neigh_ifdown(&nd_tbl, dev);
1718 fib6_run_gc(~0UL, net);
1727 static struct notifier_block ndisc_netdev_notifier = {
1728 .notifier_call = ndisc_netdev_event,
1731 #ifdef CONFIG_SYSCTL
1732 static void ndisc_warn_deprecated_sysctl(struct ctl_table *ctl,
1733 const char *func, const char *dev_name)
1735 static char warncomm[TASK_COMM_LEN];
1737 if (strcmp(warncomm, current->comm) && warned < 5) {
1738 strcpy(warncomm, current->comm);
1740 "process `%s' is using deprecated sysctl (%s) "
1741 "net.ipv6.neigh.%s.%s; "
1742 "Use net.ipv6.neigh.%s.%s_ms "
1745 dev_name, ctl->procname,
1746 dev_name, ctl->procname);
1751 int ndisc_ifinfo_sysctl_change(struct ctl_table *ctl, int write, void __user *buffer, size_t *lenp, loff_t *ppos)
1753 struct net_device *dev = ctl->extra1;
1754 struct inet6_dev *idev;
1757 if ((strcmp(ctl->procname, "retrans_time") == 0) ||
1758 (strcmp(ctl->procname, "base_reachable_time") == 0))
1759 ndisc_warn_deprecated_sysctl(ctl, "syscall", dev ? dev->name : "default");
1761 if (strcmp(ctl->procname, "retrans_time") == 0)
1762 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
1764 else if (strcmp(ctl->procname, "base_reachable_time") == 0)
1765 ret = proc_dointvec_jiffies(ctl, write,
1766 buffer, lenp, ppos);
1768 else if ((strcmp(ctl->procname, "retrans_time_ms") == 0) ||
1769 (strcmp(ctl->procname, "base_reachable_time_ms") == 0))
1770 ret = proc_dointvec_ms_jiffies(ctl, write,
1771 buffer, lenp, ppos);
1775 if (write && ret == 0 && dev && (idev = in6_dev_get(dev)) != NULL) {
1776 if (ctl->data == &idev->nd_parms->base_reachable_time)
1777 idev->nd_parms->reachable_time = neigh_rand_reach_time(idev->nd_parms->base_reachable_time);
1778 idev->tstamp = jiffies;
1779 inet6_ifinfo_notify(RTM_NEWLINK, idev);
1788 static int __net_init ndisc_net_init(struct net *net)
1790 struct ipv6_pinfo *np;
1794 err = inet_ctl_sock_create(&sk, PF_INET6,
1795 SOCK_RAW, IPPROTO_ICMPV6, net);
1798 "ICMPv6 NDISC: Failed to initialize the control socket (err %d).\n",
1803 net->ipv6.ndisc_sk = sk;
1806 np->hop_limit = 255;
1807 /* Do not loopback ndisc messages */
1813 static void __net_exit ndisc_net_exit(struct net *net)
1815 inet_ctl_sock_destroy(net->ipv6.ndisc_sk);
1818 static struct pernet_operations ndisc_net_ops = {
1819 .init = ndisc_net_init,
1820 .exit = ndisc_net_exit,
1823 int __init ndisc_init(void)
1827 err = register_pernet_subsys(&ndisc_net_ops);
1831 * Initialize the neighbour table
1833 neigh_table_init(&nd_tbl);
1835 #ifdef CONFIG_SYSCTL
1836 err = neigh_sysctl_register(NULL, &nd_tbl.parms, "ipv6",
1837 &ndisc_ifinfo_sysctl_change);
1839 goto out_unregister_pernet;
1841 err = register_netdevice_notifier(&ndisc_netdev_notifier);
1843 goto out_unregister_sysctl;
1847 out_unregister_sysctl:
1848 #ifdef CONFIG_SYSCTL
1849 neigh_sysctl_unregister(&nd_tbl.parms);
1850 out_unregister_pernet:
1852 unregister_pernet_subsys(&ndisc_net_ops);
1856 void ndisc_cleanup(void)
1858 unregister_netdevice_notifier(&ndisc_netdev_notifier);
1859 #ifdef CONFIG_SYSCTL
1860 neigh_sysctl_unregister(&nd_tbl.parms);
1862 neigh_table_clear(&nd_tbl);
1863 unregister_pernet_subsys(&ndisc_net_ops);