GNU Linux-libre 4.14.332-gnu1
[releases.git] / net / ipv6 / ndisc.c
1 /*
2  *      Neighbour Discovery for IPv6
3  *      Linux INET6 implementation
4  *
5  *      Authors:
6  *      Pedro Roque             <roque@di.fc.ul.pt>
7  *      Mike Shaver             <shaver@ingenia.com>
8  *
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.
13  */
14
15 /*
16  *      Changes:
17  *
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
22  *                                              of an RA.
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
28  */
29
30 #define pr_fmt(fmt) "ICMPv6: " fmt
31
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>
44 #ifdef CONFIG_SYSCTL
45 #include <linux/sysctl.h>
46 #endif
47
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>
53
54 #include <net/sock.h>
55 #include <net/snmp.h>
56
57 #include <net/ipv6.h>
58 #include <net/protocol.h>
59 #include <net/ndisc.h>
60 #include <net/ip6_route.h>
61 #include <net/addrconf.h>
62 #include <net/icmp.h>
63
64 #include <net/netlink.h>
65 #include <linux/rtnetlink.h>
66
67 #include <net/flow.h>
68 #include <net/ip6_checksum.h>
69 #include <net/inet_common.h>
70 #include <linux/proc_fs.h>
71
72 #include <linux/netfilter.h>
73 #include <linux/netfilter_ipv6.h>
74
75 static u32 ndisc_hash(const void *pkey,
76                       const struct net_device *dev,
77                       __u32 *hash_rnd);
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);
85
86 static const struct neigh_ops ndisc_generic_ops = {
87         .family =               AF_INET6,
88         .solicit =              ndisc_solicit,
89         .error_report =         ndisc_error_report,
90         .output =               neigh_resolve_output,
91         .connected_output =     neigh_connected_output,
92 };
93
94 static const struct neigh_ops ndisc_hh_ops = {
95         .family =               AF_INET6,
96         .solicit =              ndisc_solicit,
97         .error_report =         ndisc_error_report,
98         .output =               neigh_resolve_output,
99         .connected_output =     neigh_resolve_output,
100 };
101
102
103 static const struct neigh_ops ndisc_direct_ops = {
104         .family =               AF_INET6,
105         .output =               neigh_direct_output,
106         .connected_output =     neigh_direct_output,
107 };
108
109 struct neigh_table nd_tbl = {
110         .family =       AF_INET6,
111         .key_len =      sizeof(struct in6_addr),
112         .protocol =     cpu_to_be16(ETH_P_IPV6),
113         .hash =         ndisc_hash,
114         .key_eq =       ndisc_key_eq,
115         .constructor =  ndisc_constructor,
116         .pconstructor = pndisc_constructor,
117         .pdestructor =  pndisc_destructor,
118         .proxy_redo =   pndisc_redo,
119         .id =           "ndisc_cache",
120         .parms = {
121                 .tbl                    = &nd_tbl,
122                 .reachable_time         = ND_REACHABLE_TIME,
123                 .data = {
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,
134                 },
135         },
136         .gc_interval =    30 * HZ,
137         .gc_thresh1 =    128,
138         .gc_thresh2 =    512,
139         .gc_thresh3 =   1024,
140 };
141 EXPORT_SYMBOL_GPL(nd_tbl);
142
143 void __ndisc_fill_addr_option(struct sk_buff *skb, int type, void *data,
144                               int data_len, int pad)
145 {
146         int space = __ndisc_opt_addr_space(data_len, pad);
147         u8 *opt = skb_put(skb, space);
148
149         opt[0] = type;
150         opt[1] = space>>3;
151
152         memset(opt + 2, 0, pad);
153         opt   += pad;
154         space -= pad;
155
156         memcpy(opt+2, data, data_len);
157         data_len += 2;
158         opt += data_len;
159         space -= data_len;
160         if (space > 0)
161                 memset(opt, 0, space);
162 }
163 EXPORT_SYMBOL_GPL(__ndisc_fill_addr_option);
164
165 static inline void ndisc_fill_addr_option(struct sk_buff *skb, int type,
166                                           void *data, u8 icmp6_type)
167 {
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);
171 }
172
173 static inline void ndisc_fill_redirect_addr_option(struct sk_buff *skb,
174                                                    void *ha,
175                                                    const u8 *ops_data)
176 {
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);
179 }
180
181 static struct nd_opt_hdr *ndisc_next_option(struct nd_opt_hdr *cur,
182                                             struct nd_opt_hdr *end)
183 {
184         int type;
185         if (!cur || !end || cur >= end)
186                 return NULL;
187         type = cur->nd_opt_type;
188         do {
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;
192 }
193
194 static inline int ndisc_is_useropt(const struct net_device *dev,
195                                    struct nd_opt_hdr *opt)
196 {
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);
201 }
202
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)
206 {
207         if (!cur || !end || cur >= end)
208                 return NULL;
209         do {
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;
213 }
214
215 struct ndisc_options *ndisc_parse_options(const struct net_device *dev,
216                                           u8 *opt, int opt_len,
217                                           struct ndisc_options *ndopts)
218 {
219         struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)opt;
220
221         if (!nd_opt || opt_len < 0 || !ndopts)
222                 return NULL;
223         memset(ndopts, 0, sizeof(*ndopts));
224         while (opt_len) {
225                 int l;
226                 if (opt_len < sizeof(struct nd_opt_hdr))
227                         return NULL;
228                 l = nd_opt->nd_opt_len << 3;
229                 if (opt_len < l || l == 0)
230                         return NULL;
231                 if (ndisc_ops_parse_options(dev, nd_opt, ndopts))
232                         goto next_opt;
233                 switch (nd_opt->nd_opt_type) {
234                 case ND_OPT_SOURCE_LL_ADDR:
235                 case ND_OPT_TARGET_LL_ADDR:
236                 case ND_OPT_MTU:
237                 case ND_OPT_NONCE:
238                 case ND_OPT_REDIRECT_HDR:
239                         if (ndopts->nd_opt_array[nd_opt->nd_opt_type]) {
240                                 ND_PRINTK(2, warn,
241                                           "%s: duplicated ND6 option found: type=%d\n",
242                                           __func__, nd_opt->nd_opt_type);
243                         } else {
244                                 ndopts->nd_opt_array[nd_opt->nd_opt_type] = nd_opt;
245                         }
246                         break;
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;
251                         break;
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;
257                         break;
258 #endif
259                 default:
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;
264                         } else {
265                                 /*
266                                  * Unknown options must be silently ignored,
267                                  * to accommodate future extension to the
268                                  * protocol.
269                                  */
270                                 ND_PRINTK(2, notice,
271                                           "%s: ignored unsupported option; type=%d, len=%d\n",
272                                           __func__,
273                                           nd_opt->nd_opt_type,
274                                           nd_opt->nd_opt_len);
275                         }
276                 }
277 next_opt:
278                 opt_len -= l;
279                 nd_opt = ((void *)nd_opt) + l;
280         }
281         return ndopts;
282 }
283
284 int ndisc_mc_map(const struct in6_addr *addr, char *buf, struct net_device *dev, int dir)
285 {
286         switch (dev->type) {
287         case ARPHRD_ETHER:
288         case ARPHRD_IEEE802:    /* Not sure. Check it later. --ANK */
289         case ARPHRD_FDDI:
290                 ipv6_eth_mc_map(addr, buf);
291                 return 0;
292         case ARPHRD_ARCNET:
293                 ipv6_arcnet_mc_map(addr, buf);
294                 return 0;
295         case ARPHRD_INFINIBAND:
296                 ipv6_ib_mc_map(addr, dev->broadcast, buf);
297                 return 0;
298         case ARPHRD_IPGRE:
299                 return ipv6_ipgre_mc_map(addr, dev->broadcast, buf);
300         default:
301                 if (dir) {
302                         memcpy(buf, dev->broadcast, dev->addr_len);
303                         return 0;
304                 }
305         }
306         return -EINVAL;
307 }
308 EXPORT_SYMBOL(ndisc_mc_map);
309
310 static u32 ndisc_hash(const void *pkey,
311                       const struct net_device *dev,
312                       __u32 *hash_rnd)
313 {
314         return ndisc_hashfn(pkey, dev, hash_rnd);
315 }
316
317 static bool ndisc_key_eq(const struct neighbour *n, const void *pkey)
318 {
319         return neigh_key_eq128(n, pkey);
320 }
321
322 static int ndisc_constructor(struct neighbour *neigh)
323 {
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);
329
330         in6_dev = in6_dev_get(dev);
331         if (!in6_dev) {
332                 return -EINVAL;
333         }
334
335         parms = in6_dev->nd_parms;
336         __neigh_parms_put(neigh->parms);
337         neigh->parms = neigh_parms_clone(parms);
338
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;
344         } else {
345                 if (is_multicast) {
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);
356                 }
357                 if (dev->header_ops->cache)
358                         neigh->ops = &ndisc_hh_ops;
359                 else
360                         neigh->ops = &ndisc_generic_ops;
361                 if (neigh->nud_state&NUD_VALID)
362                         neigh->output = neigh->ops->connected_output;
363                 else
364                         neigh->output = neigh->ops->output;
365         }
366         in6_dev_put(in6_dev);
367         return 0;
368 }
369
370 static int pndisc_constructor(struct pneigh_entry *n)
371 {
372         struct in6_addr *addr = (struct in6_addr *)&n->key;
373         struct in6_addr maddr;
374         struct net_device *dev = n->dev;
375
376         if (!dev || !__in6_dev_get(dev))
377                 return -EINVAL;
378         addrconf_addr_solict_mult(addr, &maddr);
379         ipv6_dev_mc_inc(dev, &maddr);
380         return 0;
381 }
382
383 static void pndisc_destructor(struct pneigh_entry *n)
384 {
385         struct in6_addr *addr = (struct in6_addr *)&n->key;
386         struct in6_addr maddr;
387         struct net_device *dev = n->dev;
388
389         if (!dev || !__in6_dev_get(dev))
390                 return;
391         addrconf_addr_solict_mult(addr, &maddr);
392         ipv6_dev_mc_dec(dev, &maddr);
393 }
394
395 static struct sk_buff *ndisc_alloc_skb(struct net_device *dev,
396                                        int len)
397 {
398         int hlen = LL_RESERVED_SPACE(dev);
399         int tlen = dev->needed_tailroom;
400         struct sock *sk = dev_net(dev)->ipv6.ndisc_sk;
401         struct sk_buff *skb;
402
403         skb = alloc_skb(hlen + sizeof(struct ipv6hdr) + len + tlen, GFP_ATOMIC);
404         if (!skb) {
405                 ND_PRINTK(0, err, "ndisc: %s failed to allocate an skb\n",
406                           __func__);
407                 return NULL;
408         }
409
410         skb->protocol = htons(ETH_P_IPV6);
411         skb->dev = dev;
412
413         skb_reserve(skb, hlen + sizeof(struct ipv6hdr));
414         skb_reset_transport_header(skb);
415
416         /* Manually assign socket ownership as we avoid calling
417          * sock_alloc_send_pskb() to bypass wmem buffer limits
418          */
419         skb_set_owner_w(skb, sk);
420
421         return skb;
422 }
423
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)
428 {
429         struct ipv6hdr *hdr;
430
431         skb_push(skb, sizeof(*hdr));
432         skb_reset_network_header(skb);
433         hdr = ipv6_hdr(skb);
434
435         ip6_flow_hdr(hdr, 0, 0);
436
437         hdr->payload_len = htons(len);
438         hdr->nexthdr = IPPROTO_ICMPV6;
439         hdr->hop_limit = hop_limit;
440
441         hdr->saddr = *saddr;
442         hdr->daddr = *daddr;
443 }
444
445 static void ndisc_send_skb(struct sk_buff *skb,
446                            const struct in6_addr *daddr,
447                            const struct in6_addr *saddr)
448 {
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;
453         int err;
454         struct icmp6hdr *icmp6h = icmp6_hdr(skb);
455         u8 type;
456
457         type = icmp6h->icmp6_type;
458
459         if (!dst) {
460                 struct flowi6 fl6;
461                 int oif = skb->dev->ifindex;
462
463                 icmpv6_flow_init(sk, &fl6, type, saddr, daddr, oif);
464                 dst = icmp6_dst_alloc(skb->dev, &fl6);
465                 if (IS_ERR(dst)) {
466                         kfree_skb(skb);
467                         return;
468                 }
469
470                 skb_dst_set(skb, dst);
471         }
472
473         icmp6h->icmp6_cksum = csum_ipv6_magic(saddr, daddr, skb->len,
474                                               IPPROTO_ICMPV6,
475                                               csum_partial(icmp6h,
476                                                            skb->len, 0));
477
478         ip6_nd_hdr(skb, saddr, daddr, inet6_sk(sk)->hop_limit, skb->len);
479
480         rcu_read_lock();
481         idev = __in6_dev_get(dst->dev);
482         IP6_UPD_PO_STATS(net, idev, IPSTATS_MIB_OUT, skb->len);
483
484         err = NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT,
485                       net, sk, skb, NULL, dst->dev,
486                       dst_output);
487         if (!err) {
488                 ICMP6MSGOUT_INC_STATS(net, idev, type);
489                 ICMP6_INC_STATS(net, idev, ICMP6_MIB_OUTMSGS);
490         }
491
492         rcu_read_unlock();
493 }
494
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)
498 {
499         struct sk_buff *skb;
500         struct in6_addr tmpaddr;
501         struct inet6_ifaddr *ifp;
502         const struct in6_addr *src_addr;
503         struct nd_msg *msg;
504         int optlen = 0;
505
506         /* for anycast or proxy, solicited_addr != src_addr */
507         ifp = ipv6_get_ifaddr(dev_net(dev), solicited_addr, dev, 1);
508         if (ifp) {
509                 src_addr = solicited_addr;
510                 if (ifp->flags & IFA_F_OPTIMISTIC)
511                         override = false;
512                 inc_opt |= ifp->idev->cnf.force_tllao;
513                 in6_ifa_put(ifp);
514         } else {
515                 if (ipv6_dev_get_saddr(dev_net(dev), dev, daddr,
516                                        inet6_sk(dev_net(dev)->ipv6.ndisc_sk)->srcprefs,
517                                        &tmpaddr))
518                         return;
519                 src_addr = &tmpaddr;
520         }
521
522         if (!dev->addr_len)
523                 inc_opt = 0;
524         if (inc_opt)
525                 optlen += ndisc_opt_addr_space(dev,
526                                                NDISC_NEIGHBOUR_ADVERTISEMENT);
527
528         skb = ndisc_alloc_skb(dev, sizeof(*msg) + optlen);
529         if (!skb)
530                 return;
531
532         msg = skb_put(skb, sizeof(*msg));
533         *msg = (struct nd_msg) {
534                 .icmph = {
535                         .icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT,
536                         .icmp6_router = router,
537                         .icmp6_solicited = solicited,
538                         .icmp6_override = override,
539                 },
540                 .target = *solicited_addr,
541         };
542
543         if (inc_opt)
544                 ndisc_fill_addr_option(skb, ND_OPT_TARGET_LL_ADDR,
545                                        dev->dev_addr,
546                                        NDISC_NEIGHBOUR_ADVERTISEMENT);
547
548         ndisc_send_skb(skb, daddr, src_addr);
549 }
550
551 static void ndisc_send_unsol_na(struct net_device *dev)
552 {
553         struct inet6_dev *idev;
554         struct inet6_ifaddr *ifa;
555
556         idev = in6_dev_get(dev);
557         if (!idev)
558                 return;
559
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))
565                         continue;
566
567                 ndisc_send_na(dev, &in6addr_linklocal_allnodes, &ifa->addr,
568                               /*router=*/ !!idev->cnf.forwarding,
569                               /*solicited=*/ false, /*override=*/ true,
570                               /*inc_opt=*/ true);
571         }
572         read_unlock_bh(&idev->lock);
573
574         in6_dev_put(idev);
575 }
576
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,
579                    u64 nonce)
580 {
581         struct sk_buff *skb;
582         struct in6_addr addr_buf;
583         int inc_opt = dev->addr_len;
584         int optlen = 0;
585         struct nd_msg *msg;
586
587         if (!saddr) {
588                 if (ipv6_get_lladdr(dev, &addr_buf,
589                                    (IFA_F_TENTATIVE|IFA_F_OPTIMISTIC)))
590                         return;
591                 saddr = &addr_buf;
592         }
593
594         if (ipv6_addr_any(saddr))
595                 inc_opt = false;
596         if (inc_opt)
597                 optlen += ndisc_opt_addr_space(dev,
598                                                NDISC_NEIGHBOUR_SOLICITATION);
599         if (nonce != 0)
600                 optlen += 8;
601
602         skb = ndisc_alloc_skb(dev, sizeof(*msg) + optlen);
603         if (!skb)
604                 return;
605
606         msg = skb_put(skb, sizeof(*msg));
607         *msg = (struct nd_msg) {
608                 .icmph = {
609                         .icmp6_type = NDISC_NEIGHBOUR_SOLICITATION,
610                 },
611                 .target = *solicit,
612         };
613
614         if (inc_opt)
615                 ndisc_fill_addr_option(skb, ND_OPT_SOURCE_LL_ADDR,
616                                        dev->dev_addr,
617                                        NDISC_NEIGHBOUR_SOLICITATION);
618         if (nonce != 0) {
619                 u8 *opt = skb_put(skb, 8);
620
621                 opt[0] = ND_OPT_NONCE;
622                 opt[1] = 8 >> 3;
623                 memcpy(opt + 2, &nonce, 6);
624         }
625
626         ndisc_send_skb(skb, daddr, saddr);
627 }
628
629 void ndisc_send_rs(struct net_device *dev, const struct in6_addr *saddr,
630                    const struct in6_addr *daddr)
631 {
632         struct sk_buff *skb;
633         struct rs_msg *msg;
634         int send_sllao = dev->addr_len;
635         int optlen = 0;
636
637 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
638         /*
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.
645          */
646         if (send_sllao) {
647                 struct inet6_ifaddr *ifp = ipv6_get_ifaddr(dev_net(dev), saddr,
648                                                            dev, 1);
649                 if (ifp) {
650                         if (ifp->flags & IFA_F_OPTIMISTIC)  {
651                                 send_sllao = 0;
652                         }
653                         in6_ifa_put(ifp);
654                 } else {
655                         send_sllao = 0;
656                 }
657         }
658 #endif
659         if (send_sllao)
660                 optlen += ndisc_opt_addr_space(dev, NDISC_ROUTER_SOLICITATION);
661
662         skb = ndisc_alloc_skb(dev, sizeof(*msg) + optlen);
663         if (!skb)
664                 return;
665
666         msg = skb_put(skb, sizeof(*msg));
667         *msg = (struct rs_msg) {
668                 .icmph = {
669                         .icmp6_type = NDISC_ROUTER_SOLICITATION,
670                 },
671         };
672
673         if (send_sllao)
674                 ndisc_fill_addr_option(skb, ND_OPT_SOURCE_LL_ADDR,
675                                        dev->dev_addr,
676                                        NDISC_ROUTER_SOLICITATION);
677
678         ndisc_send_skb(skb, daddr, saddr);
679 }
680
681
682 static void ndisc_error_report(struct neighbour *neigh, struct sk_buff *skb)
683 {
684         /*
685          *      "The sender MUST return an ICMP
686          *       destination unreachable"
687          */
688         dst_link_failure(skb);
689         kfree_skb(skb);
690 }
691
692 /* Called with locked neigh: either read or both */
693
694 static void ndisc_solicit(struct neighbour *neigh, struct sk_buff *skb)
695 {
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);
701
702         if (skb && ipv6_chk_addr_and_flags(dev_net(dev), &ipv6_hdr(skb)->saddr,
703                                            dev, 1,
704                                            IFA_F_TENTATIVE|IFA_F_OPTIMISTIC))
705                 saddr = &ipv6_hdr(skb)->saddr;
706         probes -= NEIGH_VAR(neigh->parms, UCAST_PROBES);
707         if (probes < 0) {
708                 if (!(neigh->nud_state & NUD_VALID)) {
709                         ND_PRINTK(1, dbg,
710                                   "%s: trying to ucast probe in NUD_INVALID: %pI6\n",
711                                   __func__, target);
712                 }
713                 ndisc_send_ns(dev, target, target, saddr, 0);
714         } else if ((probes -= NEIGH_VAR(neigh->parms, APP_PROBES)) < 0) {
715                 neigh_app_ns(neigh);
716         } else {
717                 addrconf_addr_solict_mult(target, &mcaddr);
718                 ndisc_send_ns(dev, target, &mcaddr, saddr, 0);
719         }
720 }
721
722 static int pndisc_is_router(const void *pkey,
723                             struct net_device *dev)
724 {
725         struct pneigh_entry *n;
726         int ret = -1;
727
728         read_lock_bh(&nd_tbl.lock);
729         n = __pneigh_lookup(&nd_tbl, dev_net(dev), pkey, dev);
730         if (n)
731                 ret = !!(n->flags & NTF_ROUTER);
732         read_unlock_bh(&nd_tbl.lock);
733
734         return ret;
735 }
736
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)
740 {
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);
744 }
745
746 static void ndisc_recv_ns(struct sk_buff *skb)
747 {
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;
751         u8 *lladdr = NULL;
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);
760         bool inc;
761         int is_router = -1;
762         u64 nonce = 0;
763
764         if (skb->len < sizeof(struct nd_msg)) {
765                 ND_PRINTK(2, warn, "NS: packet too short\n");
766                 return;
767         }
768
769         if (ipv6_addr_is_multicast(&msg->target)) {
770                 ND_PRINTK(2, warn, "NS: multicast target address\n");
771                 return;
772         }
773
774         /*
775          * RFC2461 7.1.1:
776          * DAD has to be destined for solicited node multicast address.
777          */
778         if (dad && !ipv6_addr_is_solict_mult(daddr)) {
779                 ND_PRINTK(2, warn, "NS: bad DAD packet (wrong destination)\n");
780                 return;
781         }
782
783         if (!ndisc_parse_options(dev, msg->opt, ndoptlen, &ndopts)) {
784                 ND_PRINTK(2, warn, "NS: invalid ND options\n");
785                 return;
786         }
787
788         if (ndopts.nd_opts_src_lladdr) {
789                 lladdr = ndisc_opt_addr_data(ndopts.nd_opts_src_lladdr, dev);
790                 if (!lladdr) {
791                         ND_PRINTK(2, warn,
792                                   "NS: invalid link-layer address length\n");
793                         return;
794                 }
795
796                 /* RFC2461 7.1.1:
797                  *      If the IP source address is the unspecified address,
798                  *      there MUST NOT be source link-layer address option
799                  *      in the message.
800                  */
801                 if (dad) {
802                         ND_PRINTK(2, warn,
803                                   "NS: bad DAD packet (link-layer address option)\n");
804                         return;
805                 }
806         }
807         if (ndopts.nd_opts_nonce && ndopts.nd_opts_nonce->nd_opt_len == 1)
808                 memcpy(&nonce, (u8 *)(ndopts.nd_opts_nonce + 1), 6);
809
810         inc = ipv6_addr_is_multicast(daddr);
811
812         ifp = ipv6_get_ifaddr(dev_net(dev), &msg->target, dev, 1);
813         if (ifp) {
814 have_ifp:
815                 if (ifp->flags & (IFA_F_TENTATIVE|IFA_F_OPTIMISTIC)) {
816                         if (dad) {
817                                 if (nonce != 0 && ifp->dad_nonce == nonce) {
818                                         u8 *np = (u8 *)&nonce;
819                                         /* Matching nonce if looped back */
820                                         ND_PRINTK(2, notice,
821                                                   "%s: IPv6 DAD loopback for address %pI6c nonce %pM ignored\n",
822                                                   ifp->idev->dev->name,
823                                                   &ifp->addr, np);
824                                         goto out;
825                                 }
826                                 /*
827                                  * We are colliding with another node
828                                  * who is doing DAD
829                                  * so fail our DAD process
830                                  */
831                                 addrconf_dad_failure(ifp);
832                                 return;
833                         } else {
834                                 /*
835                                  * This is not a dad solicitation.
836                                  * If we are an optimistic node,
837                                  * we should respond.
838                                  * Otherwise, we should ignore it.
839                                  */
840                                 if (!(ifp->flags & IFA_F_OPTIMISTIC))
841                                         goto out;
842                         }
843                 }
844
845                 idev = ifp->idev;
846         } else {
847                 struct net *net = dev_net(dev);
848
849                 /* perhaps an address on the master device */
850                 if (netif_is_l3_slave(dev)) {
851                         struct net_device *mdev;
852
853                         mdev = netdev_master_upper_dev_get_rcu(dev);
854                         if (mdev) {
855                                 ifp = ipv6_get_ifaddr(net, &msg->target, mdev, 1);
856                                 if (ifp)
857                                         goto have_ifp;
858                         }
859                 }
860
861                 idev = in6_dev_get(dev);
862                 if (!idev) {
863                         /* XXX: count this drop? */
864                         return;
865                 }
866
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 &&
873                             inc &&
874                             NEIGH_VAR(idev->nd_parms, PROXY_DELAY) != 0) {
875                                 /*
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
881                                  */
882                                 struct sk_buff *n = skb_clone(skb, GFP_ATOMIC);
883                                 if (n)
884                                         pneigh_enqueue(&nd_tbl, idev->nd_parms, n);
885                                 goto out;
886                         }
887                 } else
888                         goto out;
889         }
890
891         if (is_router < 0)
892                 is_router = idev->cnf.forwarding;
893
894         if (dad) {
895                 ndisc_send_na(dev, &in6addr_linklocal_allnodes, &msg->target,
896                               !!is_router, false, (ifp != NULL), true);
897                 goto out;
898         }
899
900         if (inc)
901                 NEIGH_CACHE_STAT_INC(&nd_tbl, rcv_probes_mcast);
902         else
903                 NEIGH_CACHE_STAT_INC(&nd_tbl, rcv_probes_ucast);
904
905         /*
906          *      update / create cache entry
907          *      for the source address
908          */
909         neigh = __neigh_lookup(&nd_tbl, saddr, dev,
910                                !inc || lladdr || !dev->addr_len);
911         if (neigh)
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);
919                 if (neigh)
920                         neigh_release(neigh);
921         }
922
923 out:
924         if (ifp)
925                 in6_ifa_put(ifp);
926         else
927                 in6_dev_put(idev);
928 }
929
930 static void ndisc_recv_na(struct sk_buff *skb)
931 {
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;
935         u8 *lladdr = NULL;
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;
943
944         if (skb->len < sizeof(struct nd_msg)) {
945                 ND_PRINTK(2, warn, "NA: packet too short\n");
946                 return;
947         }
948
949         if (ipv6_addr_is_multicast(&msg->target)) {
950                 ND_PRINTK(2, warn, "NA: target address is multicast\n");
951                 return;
952         }
953
954         if (ipv6_addr_is_multicast(daddr) &&
955             msg->icmph.icmp6_solicited) {
956                 ND_PRINTK(2, warn, "NA: solicited NA is multicasted\n");
957                 return;
958         }
959
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.
963          */
964         if (!msg->icmph.icmp6_solicited && idev &&
965             idev->cnf.drop_unsolicited_na)
966                 return;
967
968         if (!ndisc_parse_options(dev, msg->opt, ndoptlen, &ndopts)) {
969                 ND_PRINTK(2, warn, "NS: invalid ND option\n");
970                 return;
971         }
972         if (ndopts.nd_opts_tgt_lladdr) {
973                 lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr, dev);
974                 if (!lladdr) {
975                         ND_PRINTK(2, warn,
976                                   "NA: invalid link-layer address length\n");
977                         return;
978                 }
979         }
980         ifp = ipv6_get_ifaddr(dev_net(dev), &msg->target, dev, 1);
981         if (ifp) {
982                 if (skb->pkt_type != PACKET_LOOPBACK
983                     && (ifp->flags & IFA_F_TENTATIVE)) {
984                                 addrconf_dad_failure(ifp);
985                                 return;
986                 }
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 :-)
991
992                    We should not print the error if NA has been
993                    received from loopback - it is just our own
994                    unsolicited advertisement.
995                  */
996                 if (skb->pkt_type != PACKET_LOOPBACK)
997                         ND_PRINTK(1, warn,
998                                   "NA: someone advertises our address %pI6 on %s!\n",
999                                   &ifp->addr, ifp->idev->dev->name);
1000                 in6_ifa_put(ifp);
1001                 return;
1002         }
1003         neigh = neigh_lookup(&nd_tbl, &msg->target, dev);
1004
1005         if (neigh) {
1006                 u8 old_flags = neigh->flags;
1007                 struct net *net = dev_net(dev);
1008
1009                 if (neigh->nud_state & NUD_FAILED)
1010                         goto out;
1011
1012                 /*
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.
1016                  */
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 */
1021                         goto out;
1022                 }
1023
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);
1031
1032                 if ((old_flags & ~neigh->flags) & NTF_ROUTER) {
1033                         /*
1034                          * Change: router to host
1035                          */
1036                         rt6_clean_tohost(dev_net(dev),  saddr);
1037                 }
1038
1039 out:
1040                 neigh_release(neigh);
1041         }
1042 }
1043
1044 static void ndisc_recv_rs(struct sk_buff *skb)
1045 {
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;
1052         u8 *lladdr = NULL;
1053
1054         if (skb->len < sizeof(*rs_msg))
1055                 return;
1056
1057         idev = __in6_dev_get(skb->dev);
1058         if (!idev) {
1059                 ND_PRINTK(1, err, "RS: can't find in6 device\n");
1060                 return;
1061         }
1062
1063         /* Don't accept RS if we're not in router mode */
1064         if (!idev->cnf.forwarding)
1065                 goto out;
1066
1067         /*
1068          * Don't update NCE if src = ::;
1069          * this implies that the source node has no ip address assigned yet.
1070          */
1071         if (ipv6_addr_any(saddr))
1072                 goto out;
1073
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");
1077                 goto out;
1078         }
1079
1080         if (ndopts.nd_opts_src_lladdr) {
1081                 lladdr = ndisc_opt_addr_data(ndopts.nd_opts_src_lladdr,
1082                                              skb->dev);
1083                 if (!lladdr)
1084                         goto out;
1085         }
1086
1087         neigh = __neigh_lookup(&nd_tbl, saddr, skb->dev, 1);
1088         if (neigh) {
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);
1095         }
1096 out:
1097         return;
1098 }
1099
1100 static void ndisc_ra_useropt(struct sk_buff *ra, struct nd_opt_hdr *opt)
1101 {
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);
1107         int err;
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));
1111
1112         skb = nlmsg_new(msg_size, GFP_ATOMIC);
1113         if (!skb) {
1114                 err = -ENOBUFS;
1115                 goto errout;
1116         }
1117
1118         nlh = nlmsg_put(skb, 0, 0, RTM_NEWNDUSEROPT, base_size, 0);
1119         if (!nlh) {
1120                 goto nla_put_failure;
1121         }
1122
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;
1129
1130         memcpy(ndmsg + 1, opt, opt->nd_opt_len << 3);
1131
1132         if (nla_put_in6_addr(skb, NDUSEROPT_SRCADDR, &ipv6_hdr(ra)->saddr))
1133                 goto nla_put_failure;
1134         nlmsg_end(skb, nlh);
1135
1136         rtnl_notify(skb, net, 0, RTNLGRP_ND_USEROPT, NULL, GFP_ATOMIC);
1137         return;
1138
1139 nla_put_failure:
1140         nlmsg_free(skb);
1141         err = -EMSGSIZE;
1142 errout:
1143         rtnl_set_sk_err(net, RTNLGRP_ND_USEROPT, err);
1144 }
1145
1146 static void ndisc_router_discovery(struct sk_buff *skb)
1147 {
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;
1152         int lifetime;
1153         struct ndisc_options ndopts;
1154         int optlen;
1155         unsigned int pref = 0;
1156         __u32 old_if_flags;
1157         bool send_ifinfo_notify = false;
1158
1159         __u8 *opt = (__u8 *)(ra_msg + 1);
1160
1161         optlen = (skb_tail_pointer(skb) - skb_transport_header(skb)) -
1162                 sizeof(struct ra_msg);
1163
1164         ND_PRINTK(2, info,
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");
1169                 return;
1170         }
1171         if (optlen < 0) {
1172                 ND_PRINTK(2, warn, "RA: packet too short\n");
1173                 return;
1174         }
1175
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");
1179                 return;
1180         }
1181 #endif
1182
1183         /*
1184          *      set the RA_RECV flag in the interface
1185          */
1186
1187         in6_dev = __in6_dev_get(skb->dev);
1188         if (!in6_dev) {
1189                 ND_PRINTK(0, err, "RA: can't find inet6 device for %s\n",
1190                           skb->dev->name);
1191                 return;
1192         }
1193
1194         if (!ndisc_parse_options(skb->dev, opt, optlen, &ndopts)) {
1195                 ND_PRINTK(2, warn, "RA: invalid ND options\n");
1196                 return;
1197         }
1198
1199         if (!ipv6_accept_ra(in6_dev)) {
1200                 ND_PRINTK(2, info,
1201                           "RA: %s, did not accept ra for dev: %s\n",
1202                           __func__, skb->dev->name);
1203                 goto skip_linkparms;
1204         }
1205
1206 #ifdef CONFIG_IPV6_NDISC_NODETYPE
1207         /* skip link-specific parameters from interior routers */
1208         if (skb->ndisc_nodetype == NDISC_NODETYPE_NODEFAULT) {
1209                 ND_PRINTK(2, info,
1210                           "RA: %s, nodetype is NODEFAULT, dev: %s\n",
1211                           __func__, skb->dev->name);
1212                 goto skip_linkparms;
1213         }
1214 #endif
1215
1216         if (in6_dev->if_flags & IF_RS_SENT) {
1217                 /*
1218                  *      flag that an RA was received after an RS was sent
1219                  *      out on this interface.
1220                  */
1221                 in6_dev->if_flags |= IF_RA_RCVD;
1222         }
1223
1224         /*
1225          * Remember the managed/otherconf flags from most recently
1226          * received RA message (RFC 2462) -- yoshfuji
1227          */
1228         old_if_flags = in6_dev->if_flags;
1229         in6_dev->if_flags = (in6_dev->if_flags & ~(IF_RA_MANAGED |
1230                                 IF_RA_OTHERCONF)) |
1231                                 (ra_msg->icmph.icmp6_addrconf_managed ?
1232                                         IF_RA_MANAGED : 0) |
1233                                 (ra_msg->icmph.icmp6_addrconf_other ?
1234                                         IF_RA_OTHERCONF : 0);
1235
1236         if (old_if_flags != in6_dev->if_flags)
1237                 send_ifinfo_notify = true;
1238
1239         if (!in6_dev->cnf.accept_ra_defrtr) {
1240                 ND_PRINTK(2, info,
1241                           "RA: %s, defrtr is false for dev: %s\n",
1242                           __func__, skb->dev->name);
1243                 goto skip_defrtr;
1244         }
1245
1246         /* Do not accept RA with source-addr found on local machine unless
1247          * accept_ra_from_local is set to true.
1248          */
1249         if (!in6_dev->cnf.accept_ra_from_local &&
1250             ipv6_chk_addr(dev_net(in6_dev->dev), &ipv6_hdr(skb)->saddr,
1251                           in6_dev->dev, 0)) {
1252                 ND_PRINTK(2, info,
1253                           "RA from local address detected on dev: %s: default router ignored\n",
1254                           skb->dev->name);
1255                 goto skip_defrtr;
1256         }
1257
1258         lifetime = ntohs(ra_msg->icmph.icmp6_rt_lifetime);
1259
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;
1266 #endif
1267
1268         rt = rt6_get_dflt_router(&ipv6_hdr(skb)->saddr, skb->dev);
1269
1270         if (rt) {
1271                 neigh = dst_neigh_lookup(&rt->dst, &ipv6_hdr(skb)->saddr);
1272                 if (!neigh) {
1273                         ND_PRINTK(0, err,
1274                                   "RA: %s got default router without neighbour\n",
1275                                   __func__);
1276                         ip6_rt_put(rt);
1277                         return;
1278                 }
1279         }
1280         if (rt && lifetime == 0) {
1281                 ip6_del_rt(rt);
1282                 rt = NULL;
1283         }
1284
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");
1289
1290                 rt = rt6_add_dflt_router(&ipv6_hdr(skb)->saddr, skb->dev, pref);
1291                 if (!rt) {
1292                         ND_PRINTK(0, err,
1293                                   "RA: %s failed to add default route\n",
1294                                   __func__);
1295                         return;
1296                 }
1297
1298                 neigh = dst_neigh_lookup(&rt->dst, &ipv6_hdr(skb)->saddr);
1299                 if (!neigh) {
1300                         ND_PRINTK(0, err,
1301                                   "RA: %s got default router without neighbour\n",
1302                                   __func__);
1303                         ip6_rt_put(rt);
1304                         return;
1305                 }
1306                 neigh->flags |= NTF_ROUTER;
1307         } else if (rt) {
1308                 rt->rt6i_flags = (rt->rt6i_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
1309         }
1310
1311         if (rt)
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;
1317                         if (rt)
1318                                 dst_metric_set(&rt->dst, RTAX_HOPLIMIT,
1319                                                ra_msg->icmph.icmp6_hop_limit);
1320                 } else {
1321                         ND_PRINTK(2, warn, "RA: Got route advertisement with lower hop_limit than minimum\n");
1322                 }
1323         }
1324
1325 skip_defrtr:
1326
1327         /*
1328          *      Update Reachable Time and Retrans Timer
1329          */
1330
1331         if (in6_dev->nd_parms) {
1332                 unsigned long rtime = ntohl(ra_msg->retrans_timer);
1333
1334                 if (rtime && rtime/1000 < MAX_SCHEDULE_TIMEOUT/HZ) {
1335                         rtime = (rtime*HZ)/1000;
1336                         if (rtime < HZ/10)
1337                                 rtime = HZ/10;
1338                         NEIGH_VAR_SET(in6_dev->nd_parms, RETRANS_TIME, rtime);
1339                         in6_dev->tstamp = jiffies;
1340                         send_ifinfo_notify = true;
1341                 }
1342
1343                 rtime = ntohl(ra_msg->reachable_time);
1344                 if (rtime && rtime/1000 < MAX_SCHEDULE_TIMEOUT/(3*HZ)) {
1345                         rtime = (rtime*HZ)/1000;
1346
1347                         if (rtime < HZ/10)
1348                                 rtime = HZ/10;
1349
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;
1358                         }
1359                 }
1360         }
1361
1362         /*
1363          *      Send a notify if RA changed managed/otherconf flags or timer settings
1364          */
1365         if (send_ifinfo_notify)
1366                 inet6_ifinfo_notify(RTM_NEWLINK, in6_dev);
1367
1368 skip_linkparms:
1369
1370         /*
1371          *      Process options.
1372          */
1373
1374         if (!neigh)
1375                 neigh = __neigh_lookup(&nd_tbl, &ipv6_hdr(skb)->saddr,
1376                                        skb->dev, 1);
1377         if (neigh) {
1378                 u8 *lladdr = NULL;
1379                 if (ndopts.nd_opts_src_lladdr) {
1380                         lladdr = ndisc_opt_addr_data(ndopts.nd_opts_src_lladdr,
1381                                                      skb->dev);
1382                         if (!lladdr) {
1383                                 ND_PRINTK(2, warn,
1384                                           "RA: invalid link-layer address length\n");
1385                                 goto out;
1386                         }
1387                 }
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);
1394         }
1395
1396         if (!ipv6_accept_ra(in6_dev)) {
1397                 ND_PRINTK(2, info,
1398                           "RA: %s, accept_ra is false for dev: %s\n",
1399                           __func__, skb->dev->name);
1400                 goto out;
1401         }
1402
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,
1406                           in6_dev->dev, 0)) {
1407                 ND_PRINTK(2, info,
1408                           "RA from local address detected on dev: %s: router info ignored.\n",
1409                           skb->dev->name);
1410                 goto skip_routeinfo;
1411         }
1412
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;
1416                      p;
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)
1422                                 continue;
1423 #endif
1424                         if (ri->prefix_len == 0 &&
1425                             !in6_dev->cnf.accept_ra_defrtr)
1426                                 continue;
1427                         if (ri->prefix_len < in6_dev->cnf.accept_ra_rt_info_min_plen)
1428                                 continue;
1429                         if (ri->prefix_len > in6_dev->cnf.accept_ra_rt_info_max_plen)
1430                                 continue;
1431                         rt6_route_rcv(skb->dev, (u8 *)p, (p->nd_opt_len) << 3,
1432                                       &ipv6_hdr(skb)->saddr);
1433                 }
1434         }
1435
1436 skip_routeinfo:
1437 #endif
1438
1439 #ifdef CONFIG_IPV6_NDISC_NODETYPE
1440         /* skip link-specific ndopts from interior routers */
1441         if (skb->ndisc_nodetype == NDISC_NODETYPE_NODEFAULT) {
1442                 ND_PRINTK(2, info,
1443                           "RA: %s, nodetype is NODEFAULT (interior routes), dev: %s\n",
1444                           __func__, skb->dev->name);
1445                 goto out;
1446         }
1447 #endif
1448
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;
1452                      p;
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);
1457                 }
1458         }
1459
1460         if (ndopts.nd_opts_mtu && in6_dev->cnf.accept_ra_mtu) {
1461                 __be32 n;
1462                 u32 mtu;
1463
1464                 memcpy(&n, ((u8 *)(ndopts.nd_opts_mtu+1))+2, sizeof(mtu));
1465                 mtu = ntohl(n);
1466
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;
1471
1472                         if (rt)
1473                                 dst_metric_set(&rt->dst, RTAX_MTU, mtu);
1474
1475                         rt6_mtu_change(skb->dev, mtu);
1476                 }
1477         }
1478
1479         if (ndopts.nd_useropts) {
1480                 struct nd_opt_hdr *p;
1481                 for (p = ndopts.nd_useropts;
1482                      p;
1483                      p = ndisc_next_useropt(skb->dev, p,
1484                                             ndopts.nd_useropts_end)) {
1485                         ndisc_ra_useropt(skb, p);
1486                 }
1487         }
1488
1489         if (ndopts.nd_opts_tgt_lladdr || ndopts.nd_opts_rh) {
1490                 ND_PRINTK(2, warn, "RA: invalid RA options\n");
1491         }
1492 out:
1493         ip6_rt_put(rt);
1494         if (neigh)
1495                 neigh_release(neigh);
1496 }
1497
1498 static void ndisc_redirect_rcv(struct sk_buff *skb)
1499 {
1500         u8 *hdr;
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));
1505
1506 #ifdef CONFIG_IPV6_NDISC_NODETYPE
1507         switch (skb->ndisc_nodetype) {
1508         case NDISC_NODETYPE_HOST:
1509         case NDISC_NODETYPE_NODEFAULT:
1510                 ND_PRINTK(2, warn,
1511                           "Redirect: from host or unauthorized router\n");
1512                 return;
1513         }
1514 #endif
1515
1516         if (!(ipv6_addr_type(&ipv6_hdr(skb)->saddr) & IPV6_ADDR_LINKLOCAL)) {
1517                 ND_PRINTK(2, warn,
1518                           "Redirect: source address is not link-local\n");
1519                 return;
1520         }
1521
1522         if (!ndisc_parse_options(skb->dev, msg->opt, ndoptlen, &ndopts))
1523                 return;
1524
1525         if (!ndopts.nd_opts_rh) {
1526                 ip6_redirect_no_header(skb, dev_net(skb->dev),
1527                                         skb->dev->ifindex, 0);
1528                 return;
1529         }
1530
1531         hdr = (u8 *)ndopts.nd_opts_rh;
1532         hdr += 8;
1533         if (!pskb_pull(skb, hdr - skb_transport_header(skb)))
1534                 return;
1535
1536         icmpv6_notify(skb, NDISC_REDIRECT, 0, 0);
1537 }
1538
1539 static void ndisc_fill_redirect_hdr_option(struct sk_buff *skb,
1540                                            struct sk_buff *orig_skb,
1541                                            int rd_len)
1542 {
1543         u8 *opt = skb_put(skb, rd_len);
1544
1545         memset(opt, 0, 8);
1546         *(opt++) = ND_OPT_REDIRECT_HDR;
1547         *(opt++) = (rd_len >> 3);
1548         opt += 6;
1549
1550         skb_copy_bits(orig_skb, skb_network_offset(orig_skb), opt,
1551                       rd_len - 8);
1552 }
1553
1554 void ndisc_send_redirect(struct sk_buff *skb, const struct in6_addr *target)
1555 {
1556         struct net_device *dev = skb->dev;
1557         struct net *net = dev_net(dev);
1558         struct sock *sk = net->ipv6.ndisc_sk;
1559         int optlen = 0;
1560         struct inet_peer *peer;
1561         struct sk_buff *buff;
1562         struct rd_msg *msg;
1563         struct in6_addr saddr_buf;
1564         struct rt6_info *rt;
1565         struct dst_entry *dst;
1566         struct flowi6 fl6;
1567         int rd_len;
1568         u8 ha_buf[MAX_ADDR_LEN], *ha = NULL,
1569            ops_data_buf[NDISC_OPS_REDIRECT_DATA_SPACE], *ops_data = NULL;
1570         bool ret;
1571
1572         if (netif_is_l3_master(skb->dev)) {
1573                 dev = __dev_get_by_index(dev_net(skb->dev), IPCB(skb)->iif);
1574                 if (!dev)
1575                         return;
1576         }
1577
1578         if (ipv6_get_lladdr(dev, &saddr_buf, IFA_F_TENTATIVE)) {
1579                 ND_PRINTK(2, warn, "Redirect: no link-local address on %s\n",
1580                           dev->name);
1581                 return;
1582         }
1583
1584         if (!ipv6_addr_equal(&ipv6_hdr(skb)->daddr, target) &&
1585             ipv6_addr_type(target) != (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) {
1586                 ND_PRINTK(2, warn,
1587                           "Redirect: target address is not link-local unicast\n");
1588                 return;
1589         }
1590
1591         icmpv6_flow_init(sk, &fl6, NDISC_REDIRECT,
1592                          &saddr_buf, &ipv6_hdr(skb)->saddr, dev->ifindex);
1593
1594         dst = ip6_route_output(net, NULL, &fl6);
1595         if (dst->error) {
1596                 dst_release(dst);
1597                 return;
1598         }
1599         dst = xfrm_lookup(net, dst, flowi6_to_flowi(&fl6), NULL, 0);
1600         if (IS_ERR(dst))
1601                 return;
1602
1603         rt = (struct rt6_info *) dst;
1604
1605         if (rt->rt6i_flags & RTF_GATEWAY) {
1606                 ND_PRINTK(2, warn,
1607                           "Redirect: destination is not a neighbour\n");
1608                 goto release;
1609         }
1610         peer = inet_getpeer_v6(net->ipv6.peers, &ipv6_hdr(skb)->saddr, 1);
1611         ret = inet_peer_xrlim_allow(peer, 1*HZ);
1612         if (peer)
1613                 inet_putpeer(peer);
1614         if (!ret)
1615                 goto release;
1616
1617         if (dev->addr_len) {
1618                 struct neighbour *neigh = dst_neigh_lookup(skb_dst(skb), target);
1619                 if (!neigh) {
1620                         ND_PRINTK(2, warn,
1621                                   "Redirect: no neigh for target address\n");
1622                         goto release;
1623                 }
1624
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);
1629                         ha = ha_buf;
1630                         optlen += ndisc_redirect_opt_addr_space(dev, neigh,
1631                                                                 ops_data_buf,
1632                                                                 &ops_data);
1633                 } else
1634                         read_unlock_bh(&neigh->lock);
1635
1636                 neigh_release(neigh);
1637         }
1638
1639         rd_len = min_t(unsigned int,
1640                        IPV6_MIN_MTU - sizeof(struct ipv6hdr) - sizeof(*msg) - optlen,
1641                        skb->len + 8);
1642         rd_len &= ~0x7;
1643         optlen += rd_len;
1644
1645         buff = ndisc_alloc_skb(dev, sizeof(*msg) + optlen);
1646         if (!buff)
1647                 goto release;
1648
1649         msg = skb_put(buff, sizeof(*msg));
1650         *msg = (struct rd_msg) {
1651                 .icmph = {
1652                         .icmp6_type = NDISC_REDIRECT,
1653                 },
1654                 .target = *target,
1655                 .dest = ipv6_hdr(skb)->daddr,
1656         };
1657
1658         /*
1659          *      include target_address option
1660          */
1661
1662         if (ha)
1663                 ndisc_fill_redirect_addr_option(buff, ha, ops_data);
1664
1665         /*
1666          *      build redirect option and copy skb over to the new packet.
1667          */
1668
1669         if (rd_len)
1670                 ndisc_fill_redirect_hdr_option(buff, skb, rd_len);
1671
1672         skb_dst_set(buff, dst);
1673         ndisc_send_skb(buff, &ipv6_hdr(skb)->saddr, &saddr_buf);
1674         return;
1675
1676 release:
1677         dst_release(dst);
1678 }
1679
1680 static void pndisc_redo(struct sk_buff *skb)
1681 {
1682         ndisc_recv_ns(skb);
1683         kfree_skb(skb);
1684 }
1685
1686 static bool ndisc_suppress_frag_ndisc(struct sk_buff *skb)
1687 {
1688         struct inet6_dev *idev = __in6_dev_get(skb->dev);
1689
1690         if (!idev)
1691                 return true;
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");
1695                 return true;
1696         }
1697         return false;
1698 }
1699
1700 int ndisc_rcv(struct sk_buff *skb)
1701 {
1702         struct nd_msg *msg;
1703
1704         if (ndisc_suppress_frag_ndisc(skb))
1705                 return 0;
1706
1707         if (skb_linearize(skb))
1708                 return 0;
1709
1710         msg = (struct nd_msg *)skb_transport_header(skb);
1711
1712         __skb_push(skb, skb->data - skb_transport_header(skb));
1713
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);
1717                 return 0;
1718         }
1719
1720         if (msg->icmph.icmp6_code != 0) {
1721                 ND_PRINTK(2, warn, "NDISC: invalid ICMPv6 code: %d\n",
1722                           msg->icmph.icmp6_code);
1723                 return 0;
1724         }
1725
1726         switch (msg->icmph.icmp6_type) {
1727         case NDISC_NEIGHBOUR_SOLICITATION:
1728                 memset(NEIGH_CB(skb), 0, sizeof(struct neighbour_cb));
1729                 ndisc_recv_ns(skb);
1730                 break;
1731
1732         case NDISC_NEIGHBOUR_ADVERTISEMENT:
1733                 ndisc_recv_na(skb);
1734                 break;
1735
1736         case NDISC_ROUTER_SOLICITATION:
1737                 ndisc_recv_rs(skb);
1738                 break;
1739
1740         case NDISC_ROUTER_ADVERTISEMENT:
1741                 ndisc_router_discovery(skb);
1742                 break;
1743
1744         case NDISC_REDIRECT:
1745                 ndisc_redirect_rcv(skb);
1746                 break;
1747         }
1748
1749         return 0;
1750 }
1751
1752 static int ndisc_netdev_event(struct notifier_block *this, unsigned long event, void *ptr)
1753 {
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;
1758
1759         switch (event) {
1760         case NETDEV_CHANGEADDR:
1761                 neigh_changeaddr(&nd_tbl, dev);
1762                 fib6_run_gc(0, net, false);
1763                 /* fallthrough */
1764         case NETDEV_UP:
1765                 idev = in6_dev_get(dev);
1766                 if (!idev)
1767                         break;
1768                 if (idev->cnf.ndisc_notify ||
1769                     net->ipv6.devconf_all->ndisc_notify)
1770                         ndisc_send_unsol_na(dev);
1771                 in6_dev_put(idev);
1772                 break;
1773         case NETDEV_CHANGE:
1774                 change_info = ptr;
1775                 if (change_info->flags_changed & IFF_NOARP)
1776                         neigh_changeaddr(&nd_tbl, dev);
1777                 break;
1778         case NETDEV_DOWN:
1779                 neigh_ifdown(&nd_tbl, dev);
1780                 fib6_run_gc(0, net, false);
1781                 break;
1782         case NETDEV_NOTIFY_PEERS:
1783                 ndisc_send_unsol_na(dev);
1784                 break;
1785         default:
1786                 break;
1787         }
1788
1789         return NOTIFY_DONE;
1790 }
1791
1792 static struct notifier_block ndisc_netdev_notifier = {
1793         .notifier_call = ndisc_netdev_event,
1794         .priority = ADDRCONF_NOTIFY_PRIORITY - 5,
1795 };
1796
1797 #ifdef CONFIG_SYSCTL
1798 static void ndisc_warn_deprecated_sysctl(struct ctl_table *ctl,
1799                                          const char *func, const char *dev_name)
1800 {
1801         static char warncomm[TASK_COMM_LEN];
1802         static int warned;
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",
1806                         warncomm, func,
1807                         dev_name, ctl->procname,
1808                         dev_name, ctl->procname);
1809                 warned++;
1810         }
1811 }
1812
1813 int ndisc_ifinfo_sysctl_change(struct ctl_table *ctl, int write, void __user *buffer, size_t *lenp, loff_t *ppos)
1814 {
1815         struct net_device *dev = ctl->extra1;
1816         struct inet6_dev *idev;
1817         int ret;
1818
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");
1822
1823         if (strcmp(ctl->procname, "retrans_time") == 0)
1824                 ret = neigh_proc_dointvec(ctl, write, buffer, lenp, ppos);
1825
1826         else if (strcmp(ctl->procname, "base_reachable_time") == 0)
1827                 ret = neigh_proc_dointvec_jiffies(ctl, write,
1828                                                   buffer, lenp, ppos);
1829
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);
1834         else
1835                 ret = -1;
1836
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);
1843                 in6_dev_put(idev);
1844         }
1845         return ret;
1846 }
1847
1848
1849 #endif
1850
1851 static int __net_init ndisc_net_init(struct net *net)
1852 {
1853         struct ipv6_pinfo *np;
1854         struct sock *sk;
1855         int err;
1856
1857         err = inet_ctl_sock_create(&sk, PF_INET6,
1858                                    SOCK_RAW, IPPROTO_ICMPV6, net);
1859         if (err < 0) {
1860                 ND_PRINTK(0, err,
1861                           "NDISC: Failed to initialize the control socket (err %d)\n",
1862                           err);
1863                 return err;
1864         }
1865
1866         net->ipv6.ndisc_sk = sk;
1867
1868         np = inet6_sk(sk);
1869         np->hop_limit = 255;
1870         /* Do not loopback ndisc messages */
1871         np->mc_loop = 0;
1872
1873         return 0;
1874 }
1875
1876 static void __net_exit ndisc_net_exit(struct net *net)
1877 {
1878         inet_ctl_sock_destroy(net->ipv6.ndisc_sk);
1879 }
1880
1881 static struct pernet_operations ndisc_net_ops = {
1882         .init = ndisc_net_init,
1883         .exit = ndisc_net_exit,
1884 };
1885
1886 int __init ndisc_init(void)
1887 {
1888         int err;
1889
1890         err = register_pernet_subsys(&ndisc_net_ops);
1891         if (err)
1892                 return err;
1893         /*
1894          * Initialize the neighbour table
1895          */
1896         neigh_table_init(NEIGH_ND_TABLE, &nd_tbl);
1897
1898 #ifdef CONFIG_SYSCTL
1899         err = neigh_sysctl_register(NULL, &nd_tbl.parms,
1900                                     ndisc_ifinfo_sysctl_change);
1901         if (err)
1902                 goto out_unregister_pernet;
1903 out:
1904 #endif
1905         return err;
1906
1907 #ifdef CONFIG_SYSCTL
1908 out_unregister_pernet:
1909         unregister_pernet_subsys(&ndisc_net_ops);
1910         goto out;
1911 #endif
1912 }
1913
1914 int __init ndisc_late_init(void)
1915 {
1916         return register_netdevice_notifier(&ndisc_netdev_notifier);
1917 }
1918
1919 void ndisc_late_cleanup(void)
1920 {
1921         unregister_netdevice_notifier(&ndisc_netdev_notifier);
1922 }
1923
1924 void ndisc_cleanup(void)
1925 {
1926 #ifdef CONFIG_SYSCTL
1927         neigh_sysctl_unregister(&nd_tbl.parms);
1928 #endif
1929         neigh_table_clear(NEIGH_ND_TABLE, &nd_tbl);
1930         unregister_pernet_subsys(&ndisc_net_ops);
1931 }