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 * Alexey I. Froloff : RFC6106 (DNSSL) support
19 * Pierre Ynard : export userland ND options
20 * through netlink (RDNSS support)
21 * Lars Fenneberg : fixed MTU setting on receipt
23 * Janos Farkas : kmalloc failure checks
24 * Alexey Kuznetsov : state machine reworked
25 * and moved to net/core.
26 * Pekka Savola : RFC2461 validation
27 * YOSHIFUJI Hideaki @USAGI : Verify ND options properly
30 #define pr_fmt(fmt) "ICMPv6: " fmt
32 #include <linux/module.h>
33 #include <linux/errno.h>
34 #include <linux/types.h>
35 #include <linux/socket.h>
36 #include <linux/sockios.h>
37 #include <linux/sched.h>
38 #include <linux/net.h>
39 #include <linux/in6.h>
40 #include <linux/route.h>
41 #include <linux/init.h>
42 #include <linux/rcupdate.h>
43 #include <linux/slab.h>
45 #include <linux/sysctl.h>
48 #include <linux/if_addr.h>
49 #include <linux/if_arp.h>
50 #include <linux/ipv6.h>
51 #include <linux/icmpv6.h>
52 #include <linux/jhash.h>
58 #include <net/protocol.h>
59 #include <net/ndisc.h>
60 #include <net/ip6_route.h>
61 #include <net/addrconf.h>
64 #include <net/netlink.h>
65 #include <linux/rtnetlink.h>
68 #include <net/ip6_checksum.h>
69 #include <net/inet_common.h>
70 #include <linux/proc_fs.h>
72 #include <linux/netfilter.h>
73 #include <linux/netfilter_ipv6.h>
75 static u32 ndisc_hash(const void *pkey,
76 const struct net_device *dev,
78 static bool ndisc_key_eq(const struct neighbour *neigh, const void *pkey);
79 static int ndisc_constructor(struct neighbour *neigh);
80 static void ndisc_solicit(struct neighbour *neigh, struct sk_buff *skb);
81 static void ndisc_error_report(struct neighbour *neigh, struct sk_buff *skb);
82 static int pndisc_constructor(struct pneigh_entry *n);
83 static void pndisc_destructor(struct pneigh_entry *n);
84 static void pndisc_redo(struct sk_buff *skb);
86 static const struct neigh_ops ndisc_generic_ops = {
88 .solicit = ndisc_solicit,
89 .error_report = ndisc_error_report,
90 .output = neigh_resolve_output,
91 .connected_output = neigh_connected_output,
94 static const struct neigh_ops ndisc_hh_ops = {
96 .solicit = ndisc_solicit,
97 .error_report = ndisc_error_report,
98 .output = neigh_resolve_output,
99 .connected_output = neigh_resolve_output,
103 static const struct neigh_ops ndisc_direct_ops = {
105 .output = neigh_direct_output,
106 .connected_output = neigh_direct_output,
109 struct neigh_table nd_tbl = {
111 .key_len = sizeof(struct in6_addr),
112 .protocol = cpu_to_be16(ETH_P_IPV6),
114 .key_eq = ndisc_key_eq,
115 .constructor = ndisc_constructor,
116 .pconstructor = pndisc_constructor,
117 .pdestructor = pndisc_destructor,
118 .proxy_redo = pndisc_redo,
122 .reachable_time = ND_REACHABLE_TIME,
124 [NEIGH_VAR_MCAST_PROBES] = 3,
125 [NEIGH_VAR_UCAST_PROBES] = 3,
126 [NEIGH_VAR_RETRANS_TIME] = ND_RETRANS_TIMER,
127 [NEIGH_VAR_BASE_REACHABLE_TIME] = ND_REACHABLE_TIME,
128 [NEIGH_VAR_DELAY_PROBE_TIME] = 5 * HZ,
129 [NEIGH_VAR_GC_STALETIME] = 60 * HZ,
130 [NEIGH_VAR_QUEUE_LEN_BYTES] = SK_WMEM_MAX,
131 [NEIGH_VAR_PROXY_QLEN] = 64,
132 [NEIGH_VAR_ANYCAST_DELAY] = 1 * HZ,
133 [NEIGH_VAR_PROXY_DELAY] = (8 * HZ) / 10,
136 .gc_interval = 30 * HZ,
141 EXPORT_SYMBOL_GPL(nd_tbl);
143 void __ndisc_fill_addr_option(struct sk_buff *skb, int type, void *data,
144 int data_len, int pad)
146 int space = __ndisc_opt_addr_space(data_len, pad);
147 u8 *opt = skb_put(skb, space);
152 memset(opt + 2, 0, pad);
156 memcpy(opt+2, data, data_len);
161 memset(opt, 0, space);
163 EXPORT_SYMBOL_GPL(__ndisc_fill_addr_option);
165 static inline void ndisc_fill_addr_option(struct sk_buff *skb, int type,
166 void *data, u8 icmp6_type)
168 __ndisc_fill_addr_option(skb, type, data, skb->dev->addr_len,
169 ndisc_addr_option_pad(skb->dev->type));
170 ndisc_ops_fill_addr_option(skb->dev, skb, icmp6_type);
173 static inline void ndisc_fill_redirect_addr_option(struct sk_buff *skb,
177 ndisc_fill_addr_option(skb, ND_OPT_TARGET_LL_ADDR, ha, NDISC_REDIRECT);
178 ndisc_ops_fill_redirect_addr_option(skb->dev, skb, ops_data);
181 static struct nd_opt_hdr *ndisc_next_option(struct nd_opt_hdr *cur,
182 struct nd_opt_hdr *end)
185 if (!cur || !end || cur >= end)
187 type = cur->nd_opt_type;
189 cur = ((void *)cur) + (cur->nd_opt_len << 3);
190 } while (cur < end && cur->nd_opt_type != type);
191 return cur <= end && cur->nd_opt_type == type ? cur : NULL;
194 static inline int ndisc_is_useropt(const struct net_device *dev,
195 struct nd_opt_hdr *opt)
197 return opt->nd_opt_type == ND_OPT_PREFIX_INFO ||
198 opt->nd_opt_type == ND_OPT_RDNSS ||
199 opt->nd_opt_type == ND_OPT_DNSSL ||
200 ndisc_ops_is_useropt(dev, opt->nd_opt_type);
203 static struct nd_opt_hdr *ndisc_next_useropt(const struct net_device *dev,
204 struct nd_opt_hdr *cur,
205 struct nd_opt_hdr *end)
207 if (!cur || !end || cur >= end)
210 cur = ((void *)cur) + (cur->nd_opt_len << 3);
211 } while (cur < end && !ndisc_is_useropt(dev, cur));
212 return cur <= end && ndisc_is_useropt(dev, cur) ? cur : NULL;
215 struct ndisc_options *ndisc_parse_options(const struct net_device *dev,
216 u8 *opt, int opt_len,
217 struct ndisc_options *ndopts)
219 struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)opt;
221 if (!nd_opt || opt_len < 0 || !ndopts)
223 memset(ndopts, 0, sizeof(*ndopts));
226 if (opt_len < sizeof(struct nd_opt_hdr))
228 l = nd_opt->nd_opt_len << 3;
229 if (opt_len < l || l == 0)
231 if (ndisc_ops_parse_options(dev, nd_opt, ndopts))
233 switch (nd_opt->nd_opt_type) {
234 case ND_OPT_SOURCE_LL_ADDR:
235 case ND_OPT_TARGET_LL_ADDR:
238 case ND_OPT_REDIRECT_HDR:
239 if (ndopts->nd_opt_array[nd_opt->nd_opt_type]) {
241 "%s: duplicated ND6 option found: type=%d\n",
242 __func__, nd_opt->nd_opt_type);
244 ndopts->nd_opt_array[nd_opt->nd_opt_type] = nd_opt;
247 case ND_OPT_PREFIX_INFO:
248 ndopts->nd_opts_pi_end = nd_opt;
249 if (!ndopts->nd_opt_array[nd_opt->nd_opt_type])
250 ndopts->nd_opt_array[nd_opt->nd_opt_type] = nd_opt;
252 #ifdef CONFIG_IPV6_ROUTE_INFO
253 case ND_OPT_ROUTE_INFO:
254 ndopts->nd_opts_ri_end = nd_opt;
255 if (!ndopts->nd_opts_ri)
256 ndopts->nd_opts_ri = nd_opt;
260 if (ndisc_is_useropt(dev, nd_opt)) {
261 ndopts->nd_useropts_end = nd_opt;
262 if (!ndopts->nd_useropts)
263 ndopts->nd_useropts = nd_opt;
266 * Unknown options must be silently ignored,
267 * to accommodate future extension to the
271 "%s: ignored unsupported option; type=%d, len=%d\n",
279 nd_opt = ((void *)nd_opt) + l;
284 int ndisc_mc_map(const struct in6_addr *addr, char *buf, struct net_device *dev, int dir)
288 case ARPHRD_IEEE802: /* Not sure. Check it later. --ANK */
290 ipv6_eth_mc_map(addr, buf);
293 ipv6_arcnet_mc_map(addr, buf);
295 case ARPHRD_INFINIBAND:
296 ipv6_ib_mc_map(addr, dev->broadcast, buf);
299 return ipv6_ipgre_mc_map(addr, dev->broadcast, buf);
302 memcpy(buf, dev->broadcast, dev->addr_len);
308 EXPORT_SYMBOL(ndisc_mc_map);
310 static u32 ndisc_hash(const void *pkey,
311 const struct net_device *dev,
314 return ndisc_hashfn(pkey, dev, hash_rnd);
317 static bool ndisc_key_eq(const struct neighbour *n, const void *pkey)
319 return neigh_key_eq128(n, pkey);
322 static int ndisc_constructor(struct neighbour *neigh)
324 struct in6_addr *addr = (struct in6_addr *)&neigh->primary_key;
325 struct net_device *dev = neigh->dev;
326 struct inet6_dev *in6_dev;
327 struct neigh_parms *parms;
328 bool is_multicast = ipv6_addr_is_multicast(addr);
330 in6_dev = in6_dev_get(dev);
335 parms = in6_dev->nd_parms;
336 __neigh_parms_put(neigh->parms);
337 neigh->parms = neigh_parms_clone(parms);
339 neigh->type = is_multicast ? RTN_MULTICAST : RTN_UNICAST;
340 if (!dev->header_ops) {
341 neigh->nud_state = NUD_NOARP;
342 neigh->ops = &ndisc_direct_ops;
343 neigh->output = neigh_direct_output;
346 neigh->nud_state = NUD_NOARP;
347 ndisc_mc_map(addr, neigh->ha, dev, 1);
348 } else if (dev->flags&(IFF_NOARP|IFF_LOOPBACK)) {
349 neigh->nud_state = NUD_NOARP;
350 memcpy(neigh->ha, dev->dev_addr, dev->addr_len);
351 if (dev->flags&IFF_LOOPBACK)
352 neigh->type = RTN_LOCAL;
353 } else if (dev->flags&IFF_POINTOPOINT) {
354 neigh->nud_state = NUD_NOARP;
355 memcpy(neigh->ha, dev->broadcast, dev->addr_len);
357 if (dev->header_ops->cache)
358 neigh->ops = &ndisc_hh_ops;
360 neigh->ops = &ndisc_generic_ops;
361 if (neigh->nud_state&NUD_VALID)
362 neigh->output = neigh->ops->connected_output;
364 neigh->output = neigh->ops->output;
366 in6_dev_put(in6_dev);
370 static int pndisc_constructor(struct pneigh_entry *n)
372 struct in6_addr *addr = (struct in6_addr *)&n->key;
373 struct in6_addr maddr;
374 struct net_device *dev = n->dev;
376 if (!dev || !__in6_dev_get(dev))
378 addrconf_addr_solict_mult(addr, &maddr);
379 ipv6_dev_mc_inc(dev, &maddr);
383 static void pndisc_destructor(struct pneigh_entry *n)
385 struct in6_addr *addr = (struct in6_addr *)&n->key;
386 struct in6_addr maddr;
387 struct net_device *dev = n->dev;
389 if (!dev || !__in6_dev_get(dev))
391 addrconf_addr_solict_mult(addr, &maddr);
392 ipv6_dev_mc_dec(dev, &maddr);
395 static struct sk_buff *ndisc_alloc_skb(struct net_device *dev,
398 int hlen = LL_RESERVED_SPACE(dev);
399 int tlen = dev->needed_tailroom;
400 struct sock *sk = dev_net(dev)->ipv6.ndisc_sk;
403 skb = alloc_skb(hlen + sizeof(struct ipv6hdr) + len + tlen, GFP_ATOMIC);
405 ND_PRINTK(0, err, "ndisc: %s failed to allocate an skb\n",
410 skb->protocol = htons(ETH_P_IPV6);
413 skb_reserve(skb, hlen + sizeof(struct ipv6hdr));
414 skb_reset_transport_header(skb);
416 /* Manually assign socket ownership as we avoid calling
417 * sock_alloc_send_pskb() to bypass wmem buffer limits
419 skb_set_owner_w(skb, sk);
424 static void ip6_nd_hdr(struct sk_buff *skb,
425 const struct in6_addr *saddr,
426 const struct in6_addr *daddr,
427 int hop_limit, int len)
431 skb_push(skb, sizeof(*hdr));
432 skb_reset_network_header(skb);
435 ip6_flow_hdr(hdr, 0, 0);
437 hdr->payload_len = htons(len);
438 hdr->nexthdr = IPPROTO_ICMPV6;
439 hdr->hop_limit = hop_limit;
445 static void ndisc_send_skb(struct sk_buff *skb,
446 const struct in6_addr *daddr,
447 const struct in6_addr *saddr)
449 struct dst_entry *dst = skb_dst(skb);
450 struct net *net = dev_net(skb->dev);
451 struct sock *sk = net->ipv6.ndisc_sk;
452 struct inet6_dev *idev;
454 struct icmp6hdr *icmp6h = icmp6_hdr(skb);
457 type = icmp6h->icmp6_type;
461 int oif = skb->dev->ifindex;
463 icmpv6_flow_init(sk, &fl6, type, saddr, daddr, oif);
464 dst = icmp6_dst_alloc(skb->dev, &fl6);
470 skb_dst_set(skb, dst);
473 icmp6h->icmp6_cksum = csum_ipv6_magic(saddr, daddr, skb->len,
478 ip6_nd_hdr(skb, saddr, daddr, inet6_sk(sk)->hop_limit, skb->len);
481 idev = __in6_dev_get(dst->dev);
482 IP6_UPD_PO_STATS(net, idev, IPSTATS_MIB_OUT, skb->len);
484 err = NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT,
485 net, sk, skb, NULL, dst->dev,
488 ICMP6MSGOUT_INC_STATS(net, idev, type);
489 ICMP6_INC_STATS(net, idev, ICMP6_MIB_OUTMSGS);
495 void ndisc_send_na(struct net_device *dev, const struct in6_addr *daddr,
496 const struct in6_addr *solicited_addr,
497 bool router, bool solicited, bool override, bool inc_opt)
500 struct in6_addr tmpaddr;
501 struct inet6_ifaddr *ifp;
502 const struct in6_addr *src_addr;
506 /* for anycast or proxy, solicited_addr != src_addr */
507 ifp = ipv6_get_ifaddr(dev_net(dev), solicited_addr, dev, 1);
509 src_addr = solicited_addr;
510 if (ifp->flags & IFA_F_OPTIMISTIC)
512 inc_opt |= ifp->idev->cnf.force_tllao;
515 if (ipv6_dev_get_saddr(dev_net(dev), dev, daddr,
516 inet6_sk(dev_net(dev)->ipv6.ndisc_sk)->srcprefs,
525 optlen += ndisc_opt_addr_space(dev,
526 NDISC_NEIGHBOUR_ADVERTISEMENT);
528 skb = ndisc_alloc_skb(dev, sizeof(*msg) + optlen);
532 msg = skb_put(skb, sizeof(*msg));
533 *msg = (struct nd_msg) {
535 .icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT,
536 .icmp6_router = router,
537 .icmp6_solicited = solicited,
538 .icmp6_override = override,
540 .target = *solicited_addr,
544 ndisc_fill_addr_option(skb, ND_OPT_TARGET_LL_ADDR,
546 NDISC_NEIGHBOUR_ADVERTISEMENT);
548 ndisc_send_skb(skb, daddr, src_addr);
551 static void ndisc_send_unsol_na(struct net_device *dev)
553 struct inet6_dev *idev;
554 struct inet6_ifaddr *ifa;
556 idev = in6_dev_get(dev);
560 read_lock_bh(&idev->lock);
561 list_for_each_entry(ifa, &idev->addr_list, if_list) {
562 /* skip tentative addresses until dad completes */
563 if (ifa->flags & IFA_F_TENTATIVE &&
564 !(ifa->flags & IFA_F_OPTIMISTIC))
567 ndisc_send_na(dev, &in6addr_linklocal_allnodes, &ifa->addr,
568 /*router=*/ !!idev->cnf.forwarding,
569 /*solicited=*/ false, /*override=*/ true,
572 read_unlock_bh(&idev->lock);
577 void ndisc_send_ns(struct net_device *dev, const struct in6_addr *solicit,
578 const struct in6_addr *daddr, const struct in6_addr *saddr,
582 struct in6_addr addr_buf;
583 int inc_opt = dev->addr_len;
588 if (ipv6_get_lladdr(dev, &addr_buf,
589 (IFA_F_TENTATIVE|IFA_F_OPTIMISTIC)))
594 if (ipv6_addr_any(saddr))
597 optlen += ndisc_opt_addr_space(dev,
598 NDISC_NEIGHBOUR_SOLICITATION);
602 skb = ndisc_alloc_skb(dev, sizeof(*msg) + optlen);
606 msg = skb_put(skb, sizeof(*msg));
607 *msg = (struct nd_msg) {
609 .icmp6_type = NDISC_NEIGHBOUR_SOLICITATION,
615 ndisc_fill_addr_option(skb, ND_OPT_SOURCE_LL_ADDR,
617 NDISC_NEIGHBOUR_SOLICITATION);
619 u8 *opt = skb_put(skb, 8);
621 opt[0] = ND_OPT_NONCE;
623 memcpy(opt + 2, &nonce, 6);
626 ndisc_send_skb(skb, daddr, saddr);
629 void ndisc_send_rs(struct net_device *dev, const struct in6_addr *saddr,
630 const struct in6_addr *daddr)
634 int send_sllao = dev->addr_len;
637 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
639 * According to section 2.2 of RFC 4429, we must not
640 * send router solicitations with a sllao from
641 * optimistic addresses, but we may send the solicitation
642 * if we don't include the sllao. So here we check
643 * if our address is optimistic, and if so, we
644 * suppress the inclusion of the sllao.
647 struct inet6_ifaddr *ifp = ipv6_get_ifaddr(dev_net(dev), saddr,
650 if (ifp->flags & IFA_F_OPTIMISTIC) {
660 optlen += ndisc_opt_addr_space(dev, NDISC_ROUTER_SOLICITATION);
662 skb = ndisc_alloc_skb(dev, sizeof(*msg) + optlen);
666 msg = skb_put(skb, sizeof(*msg));
667 *msg = (struct rs_msg) {
669 .icmp6_type = NDISC_ROUTER_SOLICITATION,
674 ndisc_fill_addr_option(skb, ND_OPT_SOURCE_LL_ADDR,
676 NDISC_ROUTER_SOLICITATION);
678 ndisc_send_skb(skb, daddr, saddr);
682 static void ndisc_error_report(struct neighbour *neigh, struct sk_buff *skb)
685 * "The sender MUST return an ICMP
686 * destination unreachable"
688 dst_link_failure(skb);
692 /* Called with locked neigh: either read or both */
694 static void ndisc_solicit(struct neighbour *neigh, struct sk_buff *skb)
696 struct in6_addr *saddr = NULL;
697 struct in6_addr mcaddr;
698 struct net_device *dev = neigh->dev;
699 struct in6_addr *target = (struct in6_addr *)&neigh->primary_key;
700 int probes = atomic_read(&neigh->probes);
702 if (skb && ipv6_chk_addr_and_flags(dev_net(dev), &ipv6_hdr(skb)->saddr,
704 IFA_F_TENTATIVE|IFA_F_OPTIMISTIC))
705 saddr = &ipv6_hdr(skb)->saddr;
706 probes -= NEIGH_VAR(neigh->parms, UCAST_PROBES);
708 if (!(neigh->nud_state & NUD_VALID)) {
710 "%s: trying to ucast probe in NUD_INVALID: %pI6\n",
713 ndisc_send_ns(dev, target, target, saddr, 0);
714 } else if ((probes -= NEIGH_VAR(neigh->parms, APP_PROBES)) < 0) {
717 addrconf_addr_solict_mult(target, &mcaddr);
718 ndisc_send_ns(dev, target, &mcaddr, saddr, 0);
722 static int pndisc_is_router(const void *pkey,
723 struct net_device *dev)
725 struct pneigh_entry *n;
728 read_lock_bh(&nd_tbl.lock);
729 n = __pneigh_lookup(&nd_tbl, dev_net(dev), pkey, dev);
731 ret = !!(n->flags & NTF_ROUTER);
732 read_unlock_bh(&nd_tbl.lock);
737 void ndisc_update(const struct net_device *dev, struct neighbour *neigh,
738 const u8 *lladdr, u8 new, u32 flags, u8 icmp6_type,
739 struct ndisc_options *ndopts)
741 neigh_update(neigh, lladdr, new, flags, 0);
742 /* report ndisc ops about neighbour update */
743 ndisc_ops_update(dev, neigh, flags, icmp6_type, ndopts);
746 static void ndisc_recv_ns(struct sk_buff *skb)
748 struct nd_msg *msg = (struct nd_msg *)skb_transport_header(skb);
749 const struct in6_addr *saddr = &ipv6_hdr(skb)->saddr;
750 const struct in6_addr *daddr = &ipv6_hdr(skb)->daddr;
752 u32 ndoptlen = skb_tail_pointer(skb) - (skb_transport_header(skb) +
753 offsetof(struct nd_msg, opt));
754 struct ndisc_options ndopts;
755 struct net_device *dev = skb->dev;
756 struct inet6_ifaddr *ifp;
757 struct inet6_dev *idev = NULL;
758 struct neighbour *neigh;
759 int dad = ipv6_addr_any(saddr);
764 if (skb->len < sizeof(struct nd_msg)) {
765 ND_PRINTK(2, warn, "NS: packet too short\n");
769 if (ipv6_addr_is_multicast(&msg->target)) {
770 ND_PRINTK(2, warn, "NS: multicast target address\n");
776 * DAD has to be destined for solicited node multicast address.
778 if (dad && !ipv6_addr_is_solict_mult(daddr)) {
779 ND_PRINTK(2, warn, "NS: bad DAD packet (wrong destination)\n");
783 if (!ndisc_parse_options(dev, msg->opt, ndoptlen, &ndopts)) {
784 ND_PRINTK(2, warn, "NS: invalid ND options\n");
788 if (ndopts.nd_opts_src_lladdr) {
789 lladdr = ndisc_opt_addr_data(ndopts.nd_opts_src_lladdr, dev);
792 "NS: invalid link-layer address length\n");
797 * If the IP source address is the unspecified address,
798 * there MUST NOT be source link-layer address option
803 "NS: bad DAD packet (link-layer address option)\n");
807 if (ndopts.nd_opts_nonce && ndopts.nd_opts_nonce->nd_opt_len == 1)
808 memcpy(&nonce, (u8 *)(ndopts.nd_opts_nonce + 1), 6);
810 inc = ipv6_addr_is_multicast(daddr);
812 ifp = ipv6_get_ifaddr(dev_net(dev), &msg->target, dev, 1);
815 if (ifp->flags & (IFA_F_TENTATIVE|IFA_F_OPTIMISTIC)) {
817 if (nonce != 0 && ifp->dad_nonce == nonce) {
818 u8 *np = (u8 *)&nonce;
819 /* Matching nonce if looped back */
821 "%s: IPv6 DAD loopback for address %pI6c nonce %pM ignored\n",
822 ifp->idev->dev->name,
827 * We are colliding with another node
829 * so fail our DAD process
831 addrconf_dad_failure(ifp);
835 * This is not a dad solicitation.
836 * If we are an optimistic node,
838 * Otherwise, we should ignore it.
840 if (!(ifp->flags & IFA_F_OPTIMISTIC))
847 struct net *net = dev_net(dev);
849 /* perhaps an address on the master device */
850 if (netif_is_l3_slave(dev)) {
851 struct net_device *mdev;
853 mdev = netdev_master_upper_dev_get_rcu(dev);
855 ifp = ipv6_get_ifaddr(net, &msg->target, mdev, 1);
861 idev = in6_dev_get(dev);
863 /* XXX: count this drop? */
867 if (ipv6_chk_acast_addr(net, dev, &msg->target) ||
868 (idev->cnf.forwarding &&
869 (net->ipv6.devconf_all->proxy_ndp || idev->cnf.proxy_ndp) &&
870 (is_router = pndisc_is_router(&msg->target, dev)) >= 0)) {
871 if (!(NEIGH_CB(skb)->flags & LOCALLY_ENQUEUED) &&
872 skb->pkt_type != PACKET_HOST &&
874 NEIGH_VAR(idev->nd_parms, PROXY_DELAY) != 0) {
876 * for anycast or proxy,
877 * sender should delay its response
878 * by a random time between 0 and
879 * MAX_ANYCAST_DELAY_TIME seconds.
880 * (RFC2461) -- yoshfuji
882 struct sk_buff *n = skb_clone(skb, GFP_ATOMIC);
884 pneigh_enqueue(&nd_tbl, idev->nd_parms, n);
892 is_router = idev->cnf.forwarding;
895 ndisc_send_na(dev, &in6addr_linklocal_allnodes, &msg->target,
896 !!is_router, false, (ifp != NULL), true);
901 NEIGH_CACHE_STAT_INC(&nd_tbl, rcv_probes_mcast);
903 NEIGH_CACHE_STAT_INC(&nd_tbl, rcv_probes_ucast);
906 * update / create cache entry
907 * for the source address
909 neigh = __neigh_lookup(&nd_tbl, saddr, dev,
910 !inc || lladdr || !dev->addr_len);
912 ndisc_update(dev, neigh, lladdr, NUD_STALE,
913 NEIGH_UPDATE_F_WEAK_OVERRIDE|
914 NEIGH_UPDATE_F_OVERRIDE,
915 NDISC_NEIGHBOUR_SOLICITATION, &ndopts);
916 if (neigh || !dev->header_ops) {
917 ndisc_send_na(dev, saddr, &msg->target, !!is_router,
918 true, (ifp != NULL && inc), inc);
920 neigh_release(neigh);
930 static void ndisc_recv_na(struct sk_buff *skb)
932 struct nd_msg *msg = (struct nd_msg *)skb_transport_header(skb);
933 struct in6_addr *saddr = &ipv6_hdr(skb)->saddr;
934 const struct in6_addr *daddr = &ipv6_hdr(skb)->daddr;
936 u32 ndoptlen = skb_tail_pointer(skb) - (skb_transport_header(skb) +
937 offsetof(struct nd_msg, opt));
938 struct ndisc_options ndopts;
939 struct net_device *dev = skb->dev;
940 struct inet6_dev *idev = __in6_dev_get(dev);
941 struct inet6_ifaddr *ifp;
942 struct neighbour *neigh;
944 if (skb->len < sizeof(struct nd_msg)) {
945 ND_PRINTK(2, warn, "NA: packet too short\n");
949 if (ipv6_addr_is_multicast(&msg->target)) {
950 ND_PRINTK(2, warn, "NA: target address is multicast\n");
954 if (ipv6_addr_is_multicast(daddr) &&
955 msg->icmph.icmp6_solicited) {
956 ND_PRINTK(2, warn, "NA: solicited NA is multicasted\n");
960 /* For some 802.11 wireless deployments (and possibly other networks),
961 * there will be a NA proxy and unsolicitd packets are attacks
962 * and thus should not be accepted.
964 if (!msg->icmph.icmp6_solicited && idev &&
965 idev->cnf.drop_unsolicited_na)
968 if (!ndisc_parse_options(dev, msg->opt, ndoptlen, &ndopts)) {
969 ND_PRINTK(2, warn, "NS: invalid ND option\n");
972 if (ndopts.nd_opts_tgt_lladdr) {
973 lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr, dev);
976 "NA: invalid link-layer address length\n");
980 ifp = ipv6_get_ifaddr(dev_net(dev), &msg->target, dev, 1);
982 if (skb->pkt_type != PACKET_LOOPBACK
983 && (ifp->flags & IFA_F_TENTATIVE)) {
984 addrconf_dad_failure(ifp);
987 /* What should we make now? The advertisement
988 is invalid, but ndisc specs say nothing
989 about it. It could be misconfiguration, or
990 an smart proxy agent tries to help us :-)
992 We should not print the error if NA has been
993 received from loopback - it is just our own
994 unsolicited advertisement.
996 if (skb->pkt_type != PACKET_LOOPBACK)
998 "NA: someone advertises our address %pI6 on %s!\n",
999 &ifp->addr, ifp->idev->dev->name);
1003 neigh = neigh_lookup(&nd_tbl, &msg->target, dev);
1006 u8 old_flags = neigh->flags;
1007 struct net *net = dev_net(dev);
1009 if (neigh->nud_state & NUD_FAILED)
1013 * Don't update the neighbor cache entry on a proxy NA from
1014 * ourselves because either the proxied node is off link or it
1015 * has already sent a NA to us.
1017 if (lladdr && !memcmp(lladdr, dev->dev_addr, dev->addr_len) &&
1018 net->ipv6.devconf_all->forwarding && net->ipv6.devconf_all->proxy_ndp &&
1019 pneigh_lookup(&nd_tbl, net, &msg->target, dev, 0)) {
1020 /* XXX: idev->cnf.proxy_ndp */
1024 ndisc_update(dev, neigh, lladdr,
1025 msg->icmph.icmp6_solicited ? NUD_REACHABLE : NUD_STALE,
1026 NEIGH_UPDATE_F_WEAK_OVERRIDE|
1027 (msg->icmph.icmp6_override ? NEIGH_UPDATE_F_OVERRIDE : 0)|
1028 NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
1029 (msg->icmph.icmp6_router ? NEIGH_UPDATE_F_ISROUTER : 0),
1030 NDISC_NEIGHBOUR_ADVERTISEMENT, &ndopts);
1032 if ((old_flags & ~neigh->flags) & NTF_ROUTER) {
1034 * Change: router to host
1036 rt6_clean_tohost(dev_net(dev), saddr);
1040 neigh_release(neigh);
1044 static void ndisc_recv_rs(struct sk_buff *skb)
1046 struct rs_msg *rs_msg = (struct rs_msg *)skb_transport_header(skb);
1047 unsigned long ndoptlen = skb->len - sizeof(*rs_msg);
1048 struct neighbour *neigh;
1049 struct inet6_dev *idev;
1050 const struct in6_addr *saddr = &ipv6_hdr(skb)->saddr;
1051 struct ndisc_options ndopts;
1054 if (skb->len < sizeof(*rs_msg))
1057 idev = __in6_dev_get(skb->dev);
1059 ND_PRINTK(1, err, "RS: can't find in6 device\n");
1063 /* Don't accept RS if we're not in router mode */
1064 if (!idev->cnf.forwarding)
1068 * Don't update NCE if src = ::;
1069 * this implies that the source node has no ip address assigned yet.
1071 if (ipv6_addr_any(saddr))
1074 /* Parse ND options */
1075 if (!ndisc_parse_options(skb->dev, rs_msg->opt, ndoptlen, &ndopts)) {
1076 ND_PRINTK(2, notice, "NS: invalid ND option, ignored\n");
1080 if (ndopts.nd_opts_src_lladdr) {
1081 lladdr = ndisc_opt_addr_data(ndopts.nd_opts_src_lladdr,
1087 neigh = __neigh_lookup(&nd_tbl, saddr, skb->dev, 1);
1089 ndisc_update(skb->dev, neigh, lladdr, NUD_STALE,
1090 NEIGH_UPDATE_F_WEAK_OVERRIDE|
1091 NEIGH_UPDATE_F_OVERRIDE|
1092 NEIGH_UPDATE_F_OVERRIDE_ISROUTER,
1093 NDISC_ROUTER_SOLICITATION, &ndopts);
1094 neigh_release(neigh);
1100 static void ndisc_ra_useropt(struct sk_buff *ra, struct nd_opt_hdr *opt)
1102 struct icmp6hdr *icmp6h = (struct icmp6hdr *)skb_transport_header(ra);
1103 struct sk_buff *skb;
1104 struct nlmsghdr *nlh;
1105 struct nduseroptmsg *ndmsg;
1106 struct net *net = dev_net(ra->dev);
1108 int base_size = NLMSG_ALIGN(sizeof(struct nduseroptmsg)
1109 + (opt->nd_opt_len << 3));
1110 size_t msg_size = base_size + nla_total_size(sizeof(struct in6_addr));
1112 skb = nlmsg_new(msg_size, GFP_ATOMIC);
1118 nlh = nlmsg_put(skb, 0, 0, RTM_NEWNDUSEROPT, base_size, 0);
1120 goto nla_put_failure;
1123 ndmsg = nlmsg_data(nlh);
1124 ndmsg->nduseropt_family = AF_INET6;
1125 ndmsg->nduseropt_ifindex = ra->dev->ifindex;
1126 ndmsg->nduseropt_icmp_type = icmp6h->icmp6_type;
1127 ndmsg->nduseropt_icmp_code = icmp6h->icmp6_code;
1128 ndmsg->nduseropt_opts_len = opt->nd_opt_len << 3;
1130 memcpy(ndmsg + 1, opt, opt->nd_opt_len << 3);
1132 if (nla_put_in6_addr(skb, NDUSEROPT_SRCADDR, &ipv6_hdr(ra)->saddr))
1133 goto nla_put_failure;
1134 nlmsg_end(skb, nlh);
1136 rtnl_notify(skb, net, 0, RTNLGRP_ND_USEROPT, NULL, GFP_ATOMIC);
1143 rtnl_set_sk_err(net, RTNLGRP_ND_USEROPT, err);
1146 static void ndisc_router_discovery(struct sk_buff *skb)
1148 struct ra_msg *ra_msg = (struct ra_msg *)skb_transport_header(skb);
1149 struct neighbour *neigh = NULL;
1150 struct inet6_dev *in6_dev;
1151 struct rt6_info *rt = NULL;
1153 struct ndisc_options ndopts;
1155 unsigned int pref = 0;
1157 bool send_ifinfo_notify = false;
1159 __u8 *opt = (__u8 *)(ra_msg + 1);
1161 optlen = (skb_tail_pointer(skb) - skb_transport_header(skb)) -
1162 sizeof(struct ra_msg);
1165 "RA: %s, dev: %s\n",
1166 __func__, skb->dev->name);
1167 if (!(ipv6_addr_type(&ipv6_hdr(skb)->saddr) & IPV6_ADDR_LINKLOCAL)) {
1168 ND_PRINTK(2, warn, "RA: source address is not link-local\n");
1172 ND_PRINTK(2, warn, "RA: packet too short\n");
1176 #ifdef CONFIG_IPV6_NDISC_NODETYPE
1177 if (skb->ndisc_nodetype == NDISC_NODETYPE_HOST) {
1178 ND_PRINTK(2, warn, "RA: from host or unauthorized router\n");
1184 * set the RA_RECV flag in the interface
1187 in6_dev = __in6_dev_get(skb->dev);
1189 ND_PRINTK(0, err, "RA: can't find inet6 device for %s\n",
1194 if (!ndisc_parse_options(skb->dev, opt, optlen, &ndopts)) {
1195 ND_PRINTK(2, warn, "RA: invalid ND options\n");
1199 if (!ipv6_accept_ra(in6_dev)) {
1201 "RA: %s, did not accept ra for dev: %s\n",
1202 __func__, skb->dev->name);
1203 goto skip_linkparms;
1206 #ifdef CONFIG_IPV6_NDISC_NODETYPE
1207 /* skip link-specific parameters from interior routers */
1208 if (skb->ndisc_nodetype == NDISC_NODETYPE_NODEFAULT) {
1210 "RA: %s, nodetype is NODEFAULT, dev: %s\n",
1211 __func__, skb->dev->name);
1212 goto skip_linkparms;
1216 if (in6_dev->if_flags & IF_RS_SENT) {
1218 * flag that an RA was received after an RS was sent
1219 * out on this interface.
1221 in6_dev->if_flags |= IF_RA_RCVD;
1225 * Remember the managed/otherconf flags from most recently
1226 * received RA message (RFC 2462) -- yoshfuji
1228 old_if_flags = in6_dev->if_flags;
1229 in6_dev->if_flags = (in6_dev->if_flags & ~(IF_RA_MANAGED |
1231 (ra_msg->icmph.icmp6_addrconf_managed ?
1232 IF_RA_MANAGED : 0) |
1233 (ra_msg->icmph.icmp6_addrconf_other ?
1234 IF_RA_OTHERCONF : 0);
1236 if (old_if_flags != in6_dev->if_flags)
1237 send_ifinfo_notify = true;
1239 if (!in6_dev->cnf.accept_ra_defrtr) {
1241 "RA: %s, defrtr is false for dev: %s\n",
1242 __func__, skb->dev->name);
1246 /* Do not accept RA with source-addr found on local machine unless
1247 * accept_ra_from_local is set to true.
1249 if (!in6_dev->cnf.accept_ra_from_local &&
1250 ipv6_chk_addr(dev_net(in6_dev->dev), &ipv6_hdr(skb)->saddr,
1253 "RA from local address detected on dev: %s: default router ignored\n",
1258 lifetime = ntohs(ra_msg->icmph.icmp6_rt_lifetime);
1260 #ifdef CONFIG_IPV6_ROUTER_PREF
1261 pref = ra_msg->icmph.icmp6_router_pref;
1262 /* 10b is handled as if it were 00b (medium) */
1263 if (pref == ICMPV6_ROUTER_PREF_INVALID ||
1264 !in6_dev->cnf.accept_ra_rtr_pref)
1265 pref = ICMPV6_ROUTER_PREF_MEDIUM;
1268 rt = rt6_get_dflt_router(&ipv6_hdr(skb)->saddr, skb->dev);
1271 neigh = dst_neigh_lookup(&rt->dst, &ipv6_hdr(skb)->saddr);
1274 "RA: %s got default router without neighbour\n",
1280 if (rt && lifetime == 0) {
1285 ND_PRINTK(3, info, "RA: rt: %p lifetime: %d, for dev: %s\n",
1286 rt, lifetime, skb->dev->name);
1287 if (!rt && lifetime) {
1288 ND_PRINTK(3, info, "RA: adding default router\n");
1290 rt = rt6_add_dflt_router(&ipv6_hdr(skb)->saddr, skb->dev, pref);
1293 "RA: %s failed to add default route\n",
1298 neigh = dst_neigh_lookup(&rt->dst, &ipv6_hdr(skb)->saddr);
1301 "RA: %s got default router without neighbour\n",
1306 neigh->flags |= NTF_ROUTER;
1308 rt->rt6i_flags = (rt->rt6i_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
1312 rt6_set_expires(rt, jiffies + (HZ * lifetime));
1313 if (in6_dev->cnf.accept_ra_min_hop_limit < 256 &&
1314 ra_msg->icmph.icmp6_hop_limit) {
1315 if (in6_dev->cnf.accept_ra_min_hop_limit <= ra_msg->icmph.icmp6_hop_limit) {
1316 in6_dev->cnf.hop_limit = ra_msg->icmph.icmp6_hop_limit;
1318 dst_metric_set(&rt->dst, RTAX_HOPLIMIT,
1319 ra_msg->icmph.icmp6_hop_limit);
1321 ND_PRINTK(2, warn, "RA: Got route advertisement with lower hop_limit than minimum\n");
1328 * Update Reachable Time and Retrans Timer
1331 if (in6_dev->nd_parms) {
1332 unsigned long rtime = ntohl(ra_msg->retrans_timer);
1334 if (rtime && rtime/1000 < MAX_SCHEDULE_TIMEOUT/HZ) {
1335 rtime = (rtime*HZ)/1000;
1338 NEIGH_VAR_SET(in6_dev->nd_parms, RETRANS_TIME, rtime);
1339 in6_dev->tstamp = jiffies;
1340 send_ifinfo_notify = true;
1343 rtime = ntohl(ra_msg->reachable_time);
1344 if (rtime && rtime/1000 < MAX_SCHEDULE_TIMEOUT/(3*HZ)) {
1345 rtime = (rtime*HZ)/1000;
1350 if (rtime != NEIGH_VAR(in6_dev->nd_parms, BASE_REACHABLE_TIME)) {
1351 NEIGH_VAR_SET(in6_dev->nd_parms,
1352 BASE_REACHABLE_TIME, rtime);
1353 NEIGH_VAR_SET(in6_dev->nd_parms,
1354 GC_STALETIME, 3 * rtime);
1355 in6_dev->nd_parms->reachable_time = neigh_rand_reach_time(rtime);
1356 in6_dev->tstamp = jiffies;
1357 send_ifinfo_notify = true;
1363 * Send a notify if RA changed managed/otherconf flags or timer settings
1365 if (send_ifinfo_notify)
1366 inet6_ifinfo_notify(RTM_NEWLINK, in6_dev);
1375 neigh = __neigh_lookup(&nd_tbl, &ipv6_hdr(skb)->saddr,
1379 if (ndopts.nd_opts_src_lladdr) {
1380 lladdr = ndisc_opt_addr_data(ndopts.nd_opts_src_lladdr,
1384 "RA: invalid link-layer address length\n");
1388 ndisc_update(skb->dev, neigh, lladdr, NUD_STALE,
1389 NEIGH_UPDATE_F_WEAK_OVERRIDE|
1390 NEIGH_UPDATE_F_OVERRIDE|
1391 NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
1392 NEIGH_UPDATE_F_ISROUTER,
1393 NDISC_ROUTER_ADVERTISEMENT, &ndopts);
1396 if (!ipv6_accept_ra(in6_dev)) {
1398 "RA: %s, accept_ra is false for dev: %s\n",
1399 __func__, skb->dev->name);
1403 #ifdef CONFIG_IPV6_ROUTE_INFO
1404 if (!in6_dev->cnf.accept_ra_from_local &&
1405 ipv6_chk_addr(dev_net(in6_dev->dev), &ipv6_hdr(skb)->saddr,
1408 "RA from local address detected on dev: %s: router info ignored.\n",
1410 goto skip_routeinfo;
1413 if (in6_dev->cnf.accept_ra_rtr_pref && ndopts.nd_opts_ri) {
1414 struct nd_opt_hdr *p;
1415 for (p = ndopts.nd_opts_ri;
1417 p = ndisc_next_option(p, ndopts.nd_opts_ri_end)) {
1418 struct route_info *ri = (struct route_info *)p;
1419 #ifdef CONFIG_IPV6_NDISC_NODETYPE
1420 if (skb->ndisc_nodetype == NDISC_NODETYPE_NODEFAULT &&
1421 ri->prefix_len == 0)
1424 if (ri->prefix_len == 0 &&
1425 !in6_dev->cnf.accept_ra_defrtr)
1427 if (ri->prefix_len < in6_dev->cnf.accept_ra_rt_info_min_plen)
1429 if (ri->prefix_len > in6_dev->cnf.accept_ra_rt_info_max_plen)
1431 rt6_route_rcv(skb->dev, (u8 *)p, (p->nd_opt_len) << 3,
1432 &ipv6_hdr(skb)->saddr);
1439 #ifdef CONFIG_IPV6_NDISC_NODETYPE
1440 /* skip link-specific ndopts from interior routers */
1441 if (skb->ndisc_nodetype == NDISC_NODETYPE_NODEFAULT) {
1443 "RA: %s, nodetype is NODEFAULT (interior routes), dev: %s\n",
1444 __func__, skb->dev->name);
1449 if (in6_dev->cnf.accept_ra_pinfo && ndopts.nd_opts_pi) {
1450 struct nd_opt_hdr *p;
1451 for (p = ndopts.nd_opts_pi;
1453 p = ndisc_next_option(p, ndopts.nd_opts_pi_end)) {
1454 addrconf_prefix_rcv(skb->dev, (u8 *)p,
1455 (p->nd_opt_len) << 3,
1456 ndopts.nd_opts_src_lladdr != NULL);
1460 if (ndopts.nd_opts_mtu && in6_dev->cnf.accept_ra_mtu) {
1464 memcpy(&n, ((u8 *)(ndopts.nd_opts_mtu+1))+2, sizeof(mtu));
1467 if (mtu < IPV6_MIN_MTU || mtu > skb->dev->mtu) {
1468 ND_PRINTK(2, warn, "RA: invalid mtu: %d\n", mtu);
1469 } else if (in6_dev->cnf.mtu6 != mtu) {
1470 in6_dev->cnf.mtu6 = mtu;
1473 dst_metric_set(&rt->dst, RTAX_MTU, mtu);
1475 rt6_mtu_change(skb->dev, mtu);
1479 if (ndopts.nd_useropts) {
1480 struct nd_opt_hdr *p;
1481 for (p = ndopts.nd_useropts;
1483 p = ndisc_next_useropt(skb->dev, p,
1484 ndopts.nd_useropts_end)) {
1485 ndisc_ra_useropt(skb, p);
1489 if (ndopts.nd_opts_tgt_lladdr || ndopts.nd_opts_rh) {
1490 ND_PRINTK(2, warn, "RA: invalid RA options\n");
1495 neigh_release(neigh);
1498 static void ndisc_redirect_rcv(struct sk_buff *skb)
1501 struct ndisc_options ndopts;
1502 struct rd_msg *msg = (struct rd_msg *)skb_transport_header(skb);
1503 u32 ndoptlen = skb_tail_pointer(skb) - (skb_transport_header(skb) +
1504 offsetof(struct rd_msg, opt));
1506 #ifdef CONFIG_IPV6_NDISC_NODETYPE
1507 switch (skb->ndisc_nodetype) {
1508 case NDISC_NODETYPE_HOST:
1509 case NDISC_NODETYPE_NODEFAULT:
1511 "Redirect: from host or unauthorized router\n");
1516 if (!(ipv6_addr_type(&ipv6_hdr(skb)->saddr) & IPV6_ADDR_LINKLOCAL)) {
1518 "Redirect: source address is not link-local\n");
1522 if (!ndisc_parse_options(skb->dev, msg->opt, ndoptlen, &ndopts))
1525 if (!ndopts.nd_opts_rh) {
1526 ip6_redirect_no_header(skb, dev_net(skb->dev),
1527 skb->dev->ifindex, 0);
1531 hdr = (u8 *)ndopts.nd_opts_rh;
1533 if (!pskb_pull(skb, hdr - skb_transport_header(skb)))
1536 icmpv6_notify(skb, NDISC_REDIRECT, 0, 0);
1539 static void ndisc_fill_redirect_hdr_option(struct sk_buff *skb,
1540 struct sk_buff *orig_skb,
1543 u8 *opt = skb_put(skb, rd_len);
1546 *(opt++) = ND_OPT_REDIRECT_HDR;
1547 *(opt++) = (rd_len >> 3);
1550 skb_copy_bits(orig_skb, skb_network_offset(orig_skb), opt,
1554 void ndisc_send_redirect(struct sk_buff *skb, const struct in6_addr *target)
1556 struct net_device *dev = skb->dev;
1557 struct net *net = dev_net(dev);
1558 struct sock *sk = net->ipv6.ndisc_sk;
1560 struct inet_peer *peer;
1561 struct sk_buff *buff;
1563 struct in6_addr saddr_buf;
1564 struct rt6_info *rt;
1565 struct dst_entry *dst;
1568 u8 ha_buf[MAX_ADDR_LEN], *ha = NULL,
1569 ops_data_buf[NDISC_OPS_REDIRECT_DATA_SPACE], *ops_data = NULL;
1572 if (netif_is_l3_master(skb->dev)) {
1573 dev = __dev_get_by_index(dev_net(skb->dev), IPCB(skb)->iif);
1578 if (ipv6_get_lladdr(dev, &saddr_buf, IFA_F_TENTATIVE)) {
1579 ND_PRINTK(2, warn, "Redirect: no link-local address on %s\n",
1584 if (!ipv6_addr_equal(&ipv6_hdr(skb)->daddr, target) &&
1585 ipv6_addr_type(target) != (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) {
1587 "Redirect: target address is not link-local unicast\n");
1591 icmpv6_flow_init(sk, &fl6, NDISC_REDIRECT,
1592 &saddr_buf, &ipv6_hdr(skb)->saddr, dev->ifindex);
1594 dst = ip6_route_output(net, NULL, &fl6);
1599 dst = xfrm_lookup(net, dst, flowi6_to_flowi(&fl6), NULL, 0);
1603 rt = (struct rt6_info *) dst;
1605 if (rt->rt6i_flags & RTF_GATEWAY) {
1607 "Redirect: destination is not a neighbour\n");
1610 peer = inet_getpeer_v6(net->ipv6.peers, &ipv6_hdr(skb)->saddr, 1);
1611 ret = inet_peer_xrlim_allow(peer, 1*HZ);
1617 if (dev->addr_len) {
1618 struct neighbour *neigh = dst_neigh_lookup(skb_dst(skb), target);
1621 "Redirect: no neigh for target address\n");
1625 read_lock_bh(&neigh->lock);
1626 if (neigh->nud_state & NUD_VALID) {
1627 memcpy(ha_buf, neigh->ha, dev->addr_len);
1628 read_unlock_bh(&neigh->lock);
1630 optlen += ndisc_redirect_opt_addr_space(dev, neigh,
1634 read_unlock_bh(&neigh->lock);
1636 neigh_release(neigh);
1639 rd_len = min_t(unsigned int,
1640 IPV6_MIN_MTU - sizeof(struct ipv6hdr) - sizeof(*msg) - optlen,
1645 buff = ndisc_alloc_skb(dev, sizeof(*msg) + optlen);
1649 msg = skb_put(buff, sizeof(*msg));
1650 *msg = (struct rd_msg) {
1652 .icmp6_type = NDISC_REDIRECT,
1655 .dest = ipv6_hdr(skb)->daddr,
1659 * include target_address option
1663 ndisc_fill_redirect_addr_option(buff, ha, ops_data);
1666 * build redirect option and copy skb over to the new packet.
1670 ndisc_fill_redirect_hdr_option(buff, skb, rd_len);
1672 skb_dst_set(buff, dst);
1673 ndisc_send_skb(buff, &ipv6_hdr(skb)->saddr, &saddr_buf);
1680 static void pndisc_redo(struct sk_buff *skb)
1686 static bool ndisc_suppress_frag_ndisc(struct sk_buff *skb)
1688 struct inet6_dev *idev = __in6_dev_get(skb->dev);
1692 if (IP6CB(skb)->flags & IP6SKB_FRAGMENTED &&
1693 idev->cnf.suppress_frag_ndisc) {
1694 net_warn_ratelimited("Received fragmented ndisc packet. Carefully consider disabling suppress_frag_ndisc.\n");
1700 int ndisc_rcv(struct sk_buff *skb)
1704 if (ndisc_suppress_frag_ndisc(skb))
1707 if (skb_linearize(skb))
1710 msg = (struct nd_msg *)skb_transport_header(skb);
1712 __skb_push(skb, skb->data - skb_transport_header(skb));
1714 if (ipv6_hdr(skb)->hop_limit != 255) {
1715 ND_PRINTK(2, warn, "NDISC: invalid hop-limit: %d\n",
1716 ipv6_hdr(skb)->hop_limit);
1720 if (msg->icmph.icmp6_code != 0) {
1721 ND_PRINTK(2, warn, "NDISC: invalid ICMPv6 code: %d\n",
1722 msg->icmph.icmp6_code);
1726 switch (msg->icmph.icmp6_type) {
1727 case NDISC_NEIGHBOUR_SOLICITATION:
1728 memset(NEIGH_CB(skb), 0, sizeof(struct neighbour_cb));
1732 case NDISC_NEIGHBOUR_ADVERTISEMENT:
1736 case NDISC_ROUTER_SOLICITATION:
1740 case NDISC_ROUTER_ADVERTISEMENT:
1741 ndisc_router_discovery(skb);
1744 case NDISC_REDIRECT:
1745 ndisc_redirect_rcv(skb);
1752 static int ndisc_netdev_event(struct notifier_block *this, unsigned long event, void *ptr)
1754 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1755 struct netdev_notifier_change_info *change_info;
1756 struct net *net = dev_net(dev);
1757 struct inet6_dev *idev;
1760 case NETDEV_CHANGEADDR:
1761 neigh_changeaddr(&nd_tbl, dev);
1762 fib6_run_gc(0, net, false);
1765 idev = in6_dev_get(dev);
1768 if (idev->cnf.ndisc_notify ||
1769 net->ipv6.devconf_all->ndisc_notify)
1770 ndisc_send_unsol_na(dev);
1775 if (change_info->flags_changed & IFF_NOARP)
1776 neigh_changeaddr(&nd_tbl, dev);
1779 neigh_ifdown(&nd_tbl, dev);
1780 fib6_run_gc(0, net, false);
1782 case NETDEV_NOTIFY_PEERS:
1783 ndisc_send_unsol_na(dev);
1792 static struct notifier_block ndisc_netdev_notifier = {
1793 .notifier_call = ndisc_netdev_event,
1794 .priority = ADDRCONF_NOTIFY_PRIORITY - 5,
1797 #ifdef CONFIG_SYSCTL
1798 static void ndisc_warn_deprecated_sysctl(struct ctl_table *ctl,
1799 const char *func, const char *dev_name)
1801 static char warncomm[TASK_COMM_LEN];
1803 if (strcmp(warncomm, current->comm) && warned < 5) {
1804 strcpy(warncomm, current->comm);
1805 pr_warn("process `%s' is using deprecated sysctl (%s) net.ipv6.neigh.%s.%s - use net.ipv6.neigh.%s.%s_ms instead\n",
1807 dev_name, ctl->procname,
1808 dev_name, ctl->procname);
1813 int ndisc_ifinfo_sysctl_change(struct ctl_table *ctl, int write, void __user *buffer, size_t *lenp, loff_t *ppos)
1815 struct net_device *dev = ctl->extra1;
1816 struct inet6_dev *idev;
1819 if ((strcmp(ctl->procname, "retrans_time") == 0) ||
1820 (strcmp(ctl->procname, "base_reachable_time") == 0))
1821 ndisc_warn_deprecated_sysctl(ctl, "syscall", dev ? dev->name : "default");
1823 if (strcmp(ctl->procname, "retrans_time") == 0)
1824 ret = neigh_proc_dointvec(ctl, write, buffer, lenp, ppos);
1826 else if (strcmp(ctl->procname, "base_reachable_time") == 0)
1827 ret = neigh_proc_dointvec_jiffies(ctl, write,
1828 buffer, lenp, ppos);
1830 else if ((strcmp(ctl->procname, "retrans_time_ms") == 0) ||
1831 (strcmp(ctl->procname, "base_reachable_time_ms") == 0))
1832 ret = neigh_proc_dointvec_ms_jiffies(ctl, write,
1833 buffer, lenp, ppos);
1837 if (write && ret == 0 && dev && (idev = in6_dev_get(dev)) != NULL) {
1838 if (ctl->data == &NEIGH_VAR(idev->nd_parms, BASE_REACHABLE_TIME))
1839 idev->nd_parms->reachable_time =
1840 neigh_rand_reach_time(NEIGH_VAR(idev->nd_parms, BASE_REACHABLE_TIME));
1841 idev->tstamp = jiffies;
1842 inet6_ifinfo_notify(RTM_NEWLINK, idev);
1851 static int __net_init ndisc_net_init(struct net *net)
1853 struct ipv6_pinfo *np;
1857 err = inet_ctl_sock_create(&sk, PF_INET6,
1858 SOCK_RAW, IPPROTO_ICMPV6, net);
1861 "NDISC: Failed to initialize the control socket (err %d)\n",
1866 net->ipv6.ndisc_sk = sk;
1869 np->hop_limit = 255;
1870 /* Do not loopback ndisc messages */
1876 static void __net_exit ndisc_net_exit(struct net *net)
1878 inet_ctl_sock_destroy(net->ipv6.ndisc_sk);
1881 static struct pernet_operations ndisc_net_ops = {
1882 .init = ndisc_net_init,
1883 .exit = ndisc_net_exit,
1886 int __init ndisc_init(void)
1890 err = register_pernet_subsys(&ndisc_net_ops);
1894 * Initialize the neighbour table
1896 neigh_table_init(NEIGH_ND_TABLE, &nd_tbl);
1898 #ifdef CONFIG_SYSCTL
1899 err = neigh_sysctl_register(NULL, &nd_tbl.parms,
1900 ndisc_ifinfo_sysctl_change);
1902 goto out_unregister_pernet;
1907 #ifdef CONFIG_SYSCTL
1908 out_unregister_pernet:
1909 unregister_pernet_subsys(&ndisc_net_ops);
1914 int __init ndisc_late_init(void)
1916 return register_netdevice_notifier(&ndisc_netdev_notifier);
1919 void ndisc_late_cleanup(void)
1921 unregister_netdevice_notifier(&ndisc_netdev_notifier);
1924 void ndisc_cleanup(void)
1926 #ifdef CONFIG_SYSCTL
1927 neigh_sysctl_unregister(&nd_tbl.parms);
1929 neigh_table_clear(NEIGH_ND_TABLE, &nd_tbl);
1930 unregister_pernet_subsys(&ndisc_net_ops);