]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - net/ipv6/addrconf.c
Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/s390/linux
[karo-tx-linux.git] / net / ipv6 / addrconf.c
1 /*
2  *      IPv6 Address [auto]configuration
3  *      Linux INET6 implementation
4  *
5  *      Authors:
6  *      Pedro Roque             <roque@di.fc.ul.pt>
7  *      Alexey Kuznetsov        <kuznet@ms2.inr.ac.ru>
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  *      Janos Farkas                    :       delete timer on ifdown
19  *      <chexum@bankinf.banki.hu>
20  *      Andi Kleen                      :       kill double kfree on module
21  *                                              unload.
22  *      Maciej W. Rozycki               :       FDDI support
23  *      sekiya@USAGI                    :       Don't send too many RS
24  *                                              packets.
25  *      yoshfuji@USAGI                  :       Fixed interval between DAD
26  *                                              packets.
27  *      YOSHIFUJI Hideaki @USAGI        :       improved accuracy of
28  *                                              address validation timer.
29  *      YOSHIFUJI Hideaki @USAGI        :       Privacy Extensions (RFC3041)
30  *                                              support.
31  *      Yuji SEKIYA @USAGI              :       Don't assign a same IPv6
32  *                                              address on a same interface.
33  *      YOSHIFUJI Hideaki @USAGI        :       ARCnet support
34  *      YOSHIFUJI Hideaki @USAGI        :       convert /proc/net/if_inet6 to
35  *                                              seq_file.
36  *      YOSHIFUJI Hideaki @USAGI        :       improved source address
37  *                                              selection; consider scope,
38  *                                              status etc.
39  */
40
41 #define pr_fmt(fmt) "IPv6: " fmt
42
43 #include <linux/errno.h>
44 #include <linux/types.h>
45 #include <linux/kernel.h>
46 #include <linux/socket.h>
47 #include <linux/sockios.h>
48 #include <linux/net.h>
49 #include <linux/in6.h>
50 #include <linux/netdevice.h>
51 #include <linux/if_addr.h>
52 #include <linux/if_arp.h>
53 #include <linux/if_arcnet.h>
54 #include <linux/if_infiniband.h>
55 #include <linux/route.h>
56 #include <linux/inetdevice.h>
57 #include <linux/init.h>
58 #include <linux/slab.h>
59 #ifdef CONFIG_SYSCTL
60 #include <linux/sysctl.h>
61 #endif
62 #include <linux/capability.h>
63 #include <linux/delay.h>
64 #include <linux/notifier.h>
65 #include <linux/string.h>
66 #include <linux/hash.h>
67
68 #include <net/net_namespace.h>
69 #include <net/sock.h>
70 #include <net/snmp.h>
71
72 #include <net/af_ieee802154.h>
73 #include <net/firewire.h>
74 #include <net/ipv6.h>
75 #include <net/protocol.h>
76 #include <net/ndisc.h>
77 #include <net/ip6_route.h>
78 #include <net/addrconf.h>
79 #include <net/tcp.h>
80 #include <net/ip.h>
81 #include <net/netlink.h>
82 #include <net/pkt_sched.h>
83 #include <linux/if_tunnel.h>
84 #include <linux/rtnetlink.h>
85 #include <linux/netconf.h>
86
87 #ifdef CONFIG_IPV6_PRIVACY
88 #include <linux/random.h>
89 #endif
90
91 #include <linux/uaccess.h>
92 #include <asm/unaligned.h>
93
94 #include <linux/proc_fs.h>
95 #include <linux/seq_file.h>
96 #include <linux/export.h>
97
98 /* Set to 3 to get tracing... */
99 #define ACONF_DEBUG 2
100
101 #if ACONF_DEBUG >= 3
102 #define ADBG(x) printk x
103 #else
104 #define ADBG(x)
105 #endif
106
107 #define INFINITY_LIFE_TIME      0xFFFFFFFF
108
109 static inline u32 cstamp_delta(unsigned long cstamp)
110 {
111         return (cstamp - INITIAL_JIFFIES) * 100UL / HZ;
112 }
113
114 #ifdef CONFIG_SYSCTL
115 static void addrconf_sysctl_register(struct inet6_dev *idev);
116 static void addrconf_sysctl_unregister(struct inet6_dev *idev);
117 #else
118 static inline void addrconf_sysctl_register(struct inet6_dev *idev)
119 {
120 }
121
122 static inline void addrconf_sysctl_unregister(struct inet6_dev *idev)
123 {
124 }
125 #endif
126
127 #ifdef CONFIG_IPV6_PRIVACY
128 static void __ipv6_regen_rndid(struct inet6_dev *idev);
129 static void __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr);
130 static void ipv6_regen_rndid(unsigned long data);
131 #endif
132
133 static int ipv6_generate_eui64(u8 *eui, struct net_device *dev);
134 static int ipv6_count_addresses(struct inet6_dev *idev);
135
136 /*
137  *      Configured unicast address hash table
138  */
139 static struct hlist_head inet6_addr_lst[IN6_ADDR_HSIZE];
140 static DEFINE_SPINLOCK(addrconf_hash_lock);
141
142 static void addrconf_verify(unsigned long);
143
144 static DEFINE_TIMER(addr_chk_timer, addrconf_verify, 0, 0);
145 static DEFINE_SPINLOCK(addrconf_verify_lock);
146
147 static void addrconf_join_anycast(struct inet6_ifaddr *ifp);
148 static void addrconf_leave_anycast(struct inet6_ifaddr *ifp);
149
150 static void addrconf_type_change(struct net_device *dev,
151                                  unsigned long event);
152 static int addrconf_ifdown(struct net_device *dev, int how);
153
154 static struct rt6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
155                                                   int plen,
156                                                   const struct net_device *dev,
157                                                   u32 flags, u32 noflags);
158
159 static void addrconf_dad_start(struct inet6_ifaddr *ifp);
160 static void addrconf_dad_timer(unsigned long data);
161 static void addrconf_dad_completed(struct inet6_ifaddr *ifp);
162 static void addrconf_dad_run(struct inet6_dev *idev);
163 static void addrconf_rs_timer(unsigned long data);
164 static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
165 static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
166
167 static void inet6_prefix_notify(int event, struct inet6_dev *idev,
168                                 struct prefix_info *pinfo);
169 static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
170                                struct net_device *dev);
171
172 static struct ipv6_devconf ipv6_devconf __read_mostly = {
173         .forwarding             = 0,
174         .hop_limit              = IPV6_DEFAULT_HOPLIMIT,
175         .mtu6                   = IPV6_MIN_MTU,
176         .accept_ra              = 1,
177         .accept_redirects       = 1,
178         .autoconf               = 1,
179         .force_mld_version      = 0,
180         .dad_transmits          = 1,
181         .rtr_solicits           = MAX_RTR_SOLICITATIONS,
182         .rtr_solicit_interval   = RTR_SOLICITATION_INTERVAL,
183         .rtr_solicit_delay      = MAX_RTR_SOLICITATION_DELAY,
184 #ifdef CONFIG_IPV6_PRIVACY
185         .use_tempaddr           = 0,
186         .temp_valid_lft         = TEMP_VALID_LIFETIME,
187         .temp_prefered_lft      = TEMP_PREFERRED_LIFETIME,
188         .regen_max_retry        = REGEN_MAX_RETRY,
189         .max_desync_factor      = MAX_DESYNC_FACTOR,
190 #endif
191         .max_addresses          = IPV6_MAX_ADDRESSES,
192         .accept_ra_defrtr       = 1,
193         .accept_ra_pinfo        = 1,
194 #ifdef CONFIG_IPV6_ROUTER_PREF
195         .accept_ra_rtr_pref     = 1,
196         .rtr_probe_interval     = 60 * HZ,
197 #ifdef CONFIG_IPV6_ROUTE_INFO
198         .accept_ra_rt_info_max_plen = 0,
199 #endif
200 #endif
201         .proxy_ndp              = 0,
202         .accept_source_route    = 0,    /* we do not accept RH0 by default. */
203         .disable_ipv6           = 0,
204         .accept_dad             = 1,
205 };
206
207 static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = {
208         .forwarding             = 0,
209         .hop_limit              = IPV6_DEFAULT_HOPLIMIT,
210         .mtu6                   = IPV6_MIN_MTU,
211         .accept_ra              = 1,
212         .accept_redirects       = 1,
213         .autoconf               = 1,
214         .dad_transmits          = 1,
215         .rtr_solicits           = MAX_RTR_SOLICITATIONS,
216         .rtr_solicit_interval   = RTR_SOLICITATION_INTERVAL,
217         .rtr_solicit_delay      = MAX_RTR_SOLICITATION_DELAY,
218 #ifdef CONFIG_IPV6_PRIVACY
219         .use_tempaddr           = 0,
220         .temp_valid_lft         = TEMP_VALID_LIFETIME,
221         .temp_prefered_lft      = TEMP_PREFERRED_LIFETIME,
222         .regen_max_retry        = REGEN_MAX_RETRY,
223         .max_desync_factor      = MAX_DESYNC_FACTOR,
224 #endif
225         .max_addresses          = IPV6_MAX_ADDRESSES,
226         .accept_ra_defrtr       = 1,
227         .accept_ra_pinfo        = 1,
228 #ifdef CONFIG_IPV6_ROUTER_PREF
229         .accept_ra_rtr_pref     = 1,
230         .rtr_probe_interval     = 60 * HZ,
231 #ifdef CONFIG_IPV6_ROUTE_INFO
232         .accept_ra_rt_info_max_plen = 0,
233 #endif
234 #endif
235         .proxy_ndp              = 0,
236         .accept_source_route    = 0,    /* we do not accept RH0 by default. */
237         .disable_ipv6           = 0,
238         .accept_dad             = 1,
239 };
240
241 /* IPv6 Wildcard Address and Loopback Address defined by RFC2553 */
242 const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
243 const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
244 const struct in6_addr in6addr_linklocal_allnodes = IN6ADDR_LINKLOCAL_ALLNODES_INIT;
245 const struct in6_addr in6addr_linklocal_allrouters = IN6ADDR_LINKLOCAL_ALLROUTERS_INIT;
246 const struct in6_addr in6addr_interfacelocal_allnodes = IN6ADDR_INTERFACELOCAL_ALLNODES_INIT;
247 const struct in6_addr in6addr_interfacelocal_allrouters = IN6ADDR_INTERFACELOCAL_ALLROUTERS_INIT;
248 const struct in6_addr in6addr_sitelocal_allrouters = IN6ADDR_SITELOCAL_ALLROUTERS_INIT;
249
250 /* Check if a valid qdisc is available */
251 static inline bool addrconf_qdisc_ok(const struct net_device *dev)
252 {
253         return !qdisc_tx_is_noop(dev);
254 }
255
256 static void addrconf_del_rs_timer(struct inet6_dev *idev)
257 {
258         if (del_timer(&idev->rs_timer))
259                 __in6_dev_put(idev);
260 }
261
262 static void addrconf_del_dad_timer(struct inet6_ifaddr *ifp)
263 {
264         if (del_timer(&ifp->dad_timer))
265                 __in6_ifa_put(ifp);
266 }
267
268 static void addrconf_mod_rs_timer(struct inet6_dev *idev,
269                                   unsigned long when)
270 {
271         if (!timer_pending(&idev->rs_timer))
272                 in6_dev_hold(idev);
273         mod_timer(&idev->rs_timer, jiffies + when);
274 }
275
276 static void addrconf_mod_dad_timer(struct inet6_ifaddr *ifp,
277                                    unsigned long when)
278 {
279         if (!timer_pending(&ifp->dad_timer))
280                 in6_ifa_hold(ifp);
281         mod_timer(&ifp->dad_timer, jiffies + when);
282 }
283
284 static int snmp6_alloc_dev(struct inet6_dev *idev)
285 {
286         if (snmp_mib_init((void __percpu **)idev->stats.ipv6,
287                           sizeof(struct ipstats_mib),
288                           __alignof__(struct ipstats_mib)) < 0)
289                 goto err_ip;
290         idev->stats.icmpv6dev = kzalloc(sizeof(struct icmpv6_mib_device),
291                                         GFP_KERNEL);
292         if (!idev->stats.icmpv6dev)
293                 goto err_icmp;
294         idev->stats.icmpv6msgdev = kzalloc(sizeof(struct icmpv6msg_mib_device),
295                                            GFP_KERNEL);
296         if (!idev->stats.icmpv6msgdev)
297                 goto err_icmpmsg;
298
299         return 0;
300
301 err_icmpmsg:
302         kfree(idev->stats.icmpv6dev);
303 err_icmp:
304         snmp_mib_free((void __percpu **)idev->stats.ipv6);
305 err_ip:
306         return -ENOMEM;
307 }
308
309 static void snmp6_free_dev(struct inet6_dev *idev)
310 {
311         kfree(idev->stats.icmpv6msgdev);
312         kfree(idev->stats.icmpv6dev);
313         snmp_mib_free((void __percpu **)idev->stats.ipv6);
314 }
315
316 /* Nobody refers to this device, we may destroy it. */
317
318 void in6_dev_finish_destroy(struct inet6_dev *idev)
319 {
320         struct net_device *dev = idev->dev;
321
322         WARN_ON(!list_empty(&idev->addr_list));
323         WARN_ON(idev->mc_list != NULL);
324         WARN_ON(timer_pending(&idev->rs_timer));
325
326 #ifdef NET_REFCNT_DEBUG
327         pr_debug("%s: %s\n", __func__, dev ? dev->name : "NIL");
328 #endif
329         dev_put(dev);
330         if (!idev->dead) {
331                 pr_warn("Freeing alive inet6 device %p\n", idev);
332                 return;
333         }
334         snmp6_free_dev(idev);
335         kfree_rcu(idev, rcu);
336 }
337 EXPORT_SYMBOL(in6_dev_finish_destroy);
338
339 static struct inet6_dev *ipv6_add_dev(struct net_device *dev)
340 {
341         struct inet6_dev *ndev;
342
343         ASSERT_RTNL();
344
345         if (dev->mtu < IPV6_MIN_MTU)
346                 return NULL;
347
348         ndev = kzalloc(sizeof(struct inet6_dev), GFP_KERNEL);
349
350         if (ndev == NULL)
351                 return NULL;
352
353         rwlock_init(&ndev->lock);
354         ndev->dev = dev;
355         INIT_LIST_HEAD(&ndev->addr_list);
356         setup_timer(&ndev->rs_timer, addrconf_rs_timer,
357                     (unsigned long)ndev);
358         memcpy(&ndev->cnf, dev_net(dev)->ipv6.devconf_dflt, sizeof(ndev->cnf));
359         ndev->cnf.mtu6 = dev->mtu;
360         ndev->cnf.sysctl = NULL;
361         ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl);
362         if (ndev->nd_parms == NULL) {
363                 kfree(ndev);
364                 return NULL;
365         }
366         if (ndev->cnf.forwarding)
367                 dev_disable_lro(dev);
368         /* We refer to the device */
369         dev_hold(dev);
370
371         if (snmp6_alloc_dev(ndev) < 0) {
372                 ADBG((KERN_WARNING
373                         "%s: cannot allocate memory for statistics; dev=%s.\n",
374                         __func__, dev->name));
375                 neigh_parms_release(&nd_tbl, ndev->nd_parms);
376                 dev_put(dev);
377                 kfree(ndev);
378                 return NULL;
379         }
380
381         if (snmp6_register_dev(ndev) < 0) {
382                 ADBG((KERN_WARNING
383                         "%s: cannot create /proc/net/dev_snmp6/%s\n",
384                         __func__, dev->name));
385                 neigh_parms_release(&nd_tbl, ndev->nd_parms);
386                 ndev->dead = 1;
387                 in6_dev_finish_destroy(ndev);
388                 return NULL;
389         }
390
391         /* One reference from device.  We must do this before
392          * we invoke __ipv6_regen_rndid().
393          */
394         in6_dev_hold(ndev);
395
396         if (dev->flags & (IFF_NOARP | IFF_LOOPBACK))
397                 ndev->cnf.accept_dad = -1;
398
399 #if IS_ENABLED(CONFIG_IPV6_SIT)
400         if (dev->type == ARPHRD_SIT && (dev->priv_flags & IFF_ISATAP)) {
401                 pr_info("%s: Disabled Multicast RS\n", dev->name);
402                 ndev->cnf.rtr_solicits = 0;
403         }
404 #endif
405
406 #ifdef CONFIG_IPV6_PRIVACY
407         INIT_LIST_HEAD(&ndev->tempaddr_list);
408         setup_timer(&ndev->regen_timer, ipv6_regen_rndid, (unsigned long)ndev);
409         if ((dev->flags&IFF_LOOPBACK) ||
410             dev->type == ARPHRD_TUNNEL ||
411             dev->type == ARPHRD_TUNNEL6 ||
412             dev->type == ARPHRD_SIT ||
413             dev->type == ARPHRD_NONE) {
414                 ndev->cnf.use_tempaddr = -1;
415         } else {
416                 in6_dev_hold(ndev);
417                 ipv6_regen_rndid((unsigned long) ndev);
418         }
419 #endif
420         ndev->token = in6addr_any;
421
422         if (netif_running(dev) && addrconf_qdisc_ok(dev))
423                 ndev->if_flags |= IF_READY;
424
425         ipv6_mc_init_dev(ndev);
426         ndev->tstamp = jiffies;
427         addrconf_sysctl_register(ndev);
428         /* protected by rtnl_lock */
429         rcu_assign_pointer(dev->ip6_ptr, ndev);
430
431         /* Join interface-local all-node multicast group */
432         ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allnodes);
433
434         /* Join all-node multicast group */
435         ipv6_dev_mc_inc(dev, &in6addr_linklocal_allnodes);
436
437         /* Join all-router multicast group if forwarding is set */
438         if (ndev->cnf.forwarding && (dev->flags & IFF_MULTICAST))
439                 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
440
441         return ndev;
442 }
443
444 static struct inet6_dev *ipv6_find_idev(struct net_device *dev)
445 {
446         struct inet6_dev *idev;
447
448         ASSERT_RTNL();
449
450         idev = __in6_dev_get(dev);
451         if (!idev) {
452                 idev = ipv6_add_dev(dev);
453                 if (!idev)
454                         return NULL;
455         }
456
457         if (dev->flags&IFF_UP)
458                 ipv6_mc_up(idev);
459         return idev;
460 }
461
462 static int inet6_netconf_msgsize_devconf(int type)
463 {
464         int size =  NLMSG_ALIGN(sizeof(struct netconfmsg))
465                     + nla_total_size(4);        /* NETCONFA_IFINDEX */
466
467         /* type -1 is used for ALL */
468         if (type == -1 || type == NETCONFA_FORWARDING)
469                 size += nla_total_size(4);
470 #ifdef CONFIG_IPV6_MROUTE
471         if (type == -1 || type == NETCONFA_MC_FORWARDING)
472                 size += nla_total_size(4);
473 #endif
474
475         return size;
476 }
477
478 static int inet6_netconf_fill_devconf(struct sk_buff *skb, int ifindex,
479                                       struct ipv6_devconf *devconf, u32 portid,
480                                       u32 seq, int event, unsigned int flags,
481                                       int type)
482 {
483         struct nlmsghdr  *nlh;
484         struct netconfmsg *ncm;
485
486         nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct netconfmsg),
487                         flags);
488         if (nlh == NULL)
489                 return -EMSGSIZE;
490
491         ncm = nlmsg_data(nlh);
492         ncm->ncm_family = AF_INET6;
493
494         if (nla_put_s32(skb, NETCONFA_IFINDEX, ifindex) < 0)
495                 goto nla_put_failure;
496
497         /* type -1 is used for ALL */
498         if ((type == -1 || type == NETCONFA_FORWARDING) &&
499             nla_put_s32(skb, NETCONFA_FORWARDING, devconf->forwarding) < 0)
500                 goto nla_put_failure;
501 #ifdef CONFIG_IPV6_MROUTE
502         if ((type == -1 || type == NETCONFA_MC_FORWARDING) &&
503             nla_put_s32(skb, NETCONFA_MC_FORWARDING,
504                         devconf->mc_forwarding) < 0)
505                 goto nla_put_failure;
506 #endif
507         return nlmsg_end(skb, nlh);
508
509 nla_put_failure:
510         nlmsg_cancel(skb, nlh);
511         return -EMSGSIZE;
512 }
513
514 void inet6_netconf_notify_devconf(struct net *net, int type, int ifindex,
515                                   struct ipv6_devconf *devconf)
516 {
517         struct sk_buff *skb;
518         int err = -ENOBUFS;
519
520         skb = nlmsg_new(inet6_netconf_msgsize_devconf(type), GFP_ATOMIC);
521         if (skb == NULL)
522                 goto errout;
523
524         err = inet6_netconf_fill_devconf(skb, ifindex, devconf, 0, 0,
525                                          RTM_NEWNETCONF, 0, type);
526         if (err < 0) {
527                 /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
528                 WARN_ON(err == -EMSGSIZE);
529                 kfree_skb(skb);
530                 goto errout;
531         }
532         rtnl_notify(skb, net, 0, RTNLGRP_IPV6_NETCONF, NULL, GFP_ATOMIC);
533         return;
534 errout:
535         rtnl_set_sk_err(net, RTNLGRP_IPV6_NETCONF, err);
536 }
537
538 static const struct nla_policy devconf_ipv6_policy[NETCONFA_MAX+1] = {
539         [NETCONFA_IFINDEX]      = { .len = sizeof(int) },
540         [NETCONFA_FORWARDING]   = { .len = sizeof(int) },
541 };
542
543 static int inet6_netconf_get_devconf(struct sk_buff *in_skb,
544                                      struct nlmsghdr *nlh)
545 {
546         struct net *net = sock_net(in_skb->sk);
547         struct nlattr *tb[NETCONFA_MAX+1];
548         struct netconfmsg *ncm;
549         struct sk_buff *skb;
550         struct ipv6_devconf *devconf;
551         struct inet6_dev *in6_dev;
552         struct net_device *dev;
553         int ifindex;
554         int err;
555
556         err = nlmsg_parse(nlh, sizeof(*ncm), tb, NETCONFA_MAX,
557                           devconf_ipv6_policy);
558         if (err < 0)
559                 goto errout;
560
561         err = EINVAL;
562         if (!tb[NETCONFA_IFINDEX])
563                 goto errout;
564
565         ifindex = nla_get_s32(tb[NETCONFA_IFINDEX]);
566         switch (ifindex) {
567         case NETCONFA_IFINDEX_ALL:
568                 devconf = net->ipv6.devconf_all;
569                 break;
570         case NETCONFA_IFINDEX_DEFAULT:
571                 devconf = net->ipv6.devconf_dflt;
572                 break;
573         default:
574                 dev = __dev_get_by_index(net, ifindex);
575                 if (dev == NULL)
576                         goto errout;
577                 in6_dev = __in6_dev_get(dev);
578                 if (in6_dev == NULL)
579                         goto errout;
580                 devconf = &in6_dev->cnf;
581                 break;
582         }
583
584         err = -ENOBUFS;
585         skb = nlmsg_new(inet6_netconf_msgsize_devconf(-1), GFP_ATOMIC);
586         if (skb == NULL)
587                 goto errout;
588
589         err = inet6_netconf_fill_devconf(skb, ifindex, devconf,
590                                          NETLINK_CB(in_skb).portid,
591                                          nlh->nlmsg_seq, RTM_NEWNETCONF, 0,
592                                          -1);
593         if (err < 0) {
594                 /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
595                 WARN_ON(err == -EMSGSIZE);
596                 kfree_skb(skb);
597                 goto errout;
598         }
599         err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
600 errout:
601         return err;
602 }
603
604 static int inet6_netconf_dump_devconf(struct sk_buff *skb,
605                                       struct netlink_callback *cb)
606 {
607         struct net *net = sock_net(skb->sk);
608         int h, s_h;
609         int idx, s_idx;
610         struct net_device *dev;
611         struct inet6_dev *idev;
612         struct hlist_head *head;
613
614         s_h = cb->args[0];
615         s_idx = idx = cb->args[1];
616
617         for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
618                 idx = 0;
619                 head = &net->dev_index_head[h];
620                 rcu_read_lock();
621                 cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^
622                           net->dev_base_seq;
623                 hlist_for_each_entry_rcu(dev, head, index_hlist) {
624                         if (idx < s_idx)
625                                 goto cont;
626                         idev = __in6_dev_get(dev);
627                         if (!idev)
628                                 goto cont;
629
630                         if (inet6_netconf_fill_devconf(skb, dev->ifindex,
631                                                        &idev->cnf,
632                                                        NETLINK_CB(cb->skb).portid,
633                                                        cb->nlh->nlmsg_seq,
634                                                        RTM_NEWNETCONF,
635                                                        NLM_F_MULTI,
636                                                        -1) <= 0) {
637                                 rcu_read_unlock();
638                                 goto done;
639                         }
640                         nl_dump_check_consistent(cb, nlmsg_hdr(skb));
641 cont:
642                         idx++;
643                 }
644                 rcu_read_unlock();
645         }
646         if (h == NETDEV_HASHENTRIES) {
647                 if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_ALL,
648                                                net->ipv6.devconf_all,
649                                                NETLINK_CB(cb->skb).portid,
650                                                cb->nlh->nlmsg_seq,
651                                                RTM_NEWNETCONF, NLM_F_MULTI,
652                                                -1) <= 0)
653                         goto done;
654                 else
655                         h++;
656         }
657         if (h == NETDEV_HASHENTRIES + 1) {
658                 if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_DEFAULT,
659                                                net->ipv6.devconf_dflt,
660                                                NETLINK_CB(cb->skb).portid,
661                                                cb->nlh->nlmsg_seq,
662                                                RTM_NEWNETCONF, NLM_F_MULTI,
663                                                -1) <= 0)
664                         goto done;
665                 else
666                         h++;
667         }
668 done:
669         cb->args[0] = h;
670         cb->args[1] = idx;
671
672         return skb->len;
673 }
674
675 #ifdef CONFIG_SYSCTL
676 static void dev_forward_change(struct inet6_dev *idev)
677 {
678         struct net_device *dev;
679         struct inet6_ifaddr *ifa;
680
681         if (!idev)
682                 return;
683         dev = idev->dev;
684         if (idev->cnf.forwarding)
685                 dev_disable_lro(dev);
686         if (dev->flags & IFF_MULTICAST) {
687                 if (idev->cnf.forwarding) {
688                         ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
689                         ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allrouters);
690                         ipv6_dev_mc_inc(dev, &in6addr_sitelocal_allrouters);
691                 } else {
692                         ipv6_dev_mc_dec(dev, &in6addr_linklocal_allrouters);
693                         ipv6_dev_mc_dec(dev, &in6addr_interfacelocal_allrouters);
694                         ipv6_dev_mc_dec(dev, &in6addr_sitelocal_allrouters);
695                 }
696         }
697
698         list_for_each_entry(ifa, &idev->addr_list, if_list) {
699                 if (ifa->flags&IFA_F_TENTATIVE)
700                         continue;
701                 if (idev->cnf.forwarding)
702                         addrconf_join_anycast(ifa);
703                 else
704                         addrconf_leave_anycast(ifa);
705         }
706         inet6_netconf_notify_devconf(dev_net(dev), NETCONFA_FORWARDING,
707                                      dev->ifindex, &idev->cnf);
708 }
709
710
711 static void addrconf_forward_change(struct net *net, __s32 newf)
712 {
713         struct net_device *dev;
714         struct inet6_dev *idev;
715
716         for_each_netdev(net, dev) {
717                 idev = __in6_dev_get(dev);
718                 if (idev) {
719                         int changed = (!idev->cnf.forwarding) ^ (!newf);
720                         idev->cnf.forwarding = newf;
721                         if (changed)
722                                 dev_forward_change(idev);
723                 }
724         }
725 }
726
727 static int addrconf_fixup_forwarding(struct ctl_table *table, int *p, int newf)
728 {
729         struct net *net;
730         int old;
731
732         if (!rtnl_trylock())
733                 return restart_syscall();
734
735         net = (struct net *)table->extra2;
736         old = *p;
737         *p = newf;
738
739         if (p == &net->ipv6.devconf_dflt->forwarding) {
740                 if ((!newf) ^ (!old))
741                         inet6_netconf_notify_devconf(net, NETCONFA_FORWARDING,
742                                                      NETCONFA_IFINDEX_DEFAULT,
743                                                      net->ipv6.devconf_dflt);
744                 rtnl_unlock();
745                 return 0;
746         }
747
748         if (p == &net->ipv6.devconf_all->forwarding) {
749                 net->ipv6.devconf_dflt->forwarding = newf;
750                 addrconf_forward_change(net, newf);
751                 if ((!newf) ^ (!old))
752                         inet6_netconf_notify_devconf(net, NETCONFA_FORWARDING,
753                                                      NETCONFA_IFINDEX_ALL,
754                                                      net->ipv6.devconf_all);
755         } else if ((!newf) ^ (!old))
756                 dev_forward_change((struct inet6_dev *)table->extra1);
757         rtnl_unlock();
758
759         if (newf)
760                 rt6_purge_dflt_routers(net);
761         return 1;
762 }
763 #endif
764
765 /* Nobody refers to this ifaddr, destroy it */
766 void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp)
767 {
768         WARN_ON(!hlist_unhashed(&ifp->addr_lst));
769
770 #ifdef NET_REFCNT_DEBUG
771         pr_debug("%s\n", __func__);
772 #endif
773
774         in6_dev_put(ifp->idev);
775
776         if (del_timer(&ifp->dad_timer))
777                 pr_notice("Timer is still running, when freeing ifa=%p\n", ifp);
778
779         if (ifp->state != INET6_IFADDR_STATE_DEAD) {
780                 pr_warn("Freeing alive inet6 address %p\n", ifp);
781                 return;
782         }
783         ip6_rt_put(ifp->rt);
784
785         kfree_rcu(ifp, rcu);
786 }
787
788 static void
789 ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp)
790 {
791         struct list_head *p;
792         int ifp_scope = ipv6_addr_src_scope(&ifp->addr);
793
794         /*
795          * Each device address list is sorted in order of scope -
796          * global before linklocal.
797          */
798         list_for_each(p, &idev->addr_list) {
799                 struct inet6_ifaddr *ifa
800                         = list_entry(p, struct inet6_ifaddr, if_list);
801                 if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr))
802                         break;
803         }
804
805         list_add_tail(&ifp->if_list, p);
806 }
807
808 static u32 inet6_addr_hash(const struct in6_addr *addr)
809 {
810         return hash_32(ipv6_addr_hash(addr), IN6_ADDR_HSIZE_SHIFT);
811 }
812
813 /* On success it returns ifp with increased reference count */
814
815 static struct inet6_ifaddr *
816 ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
817               const struct in6_addr *peer_addr, int pfxlen,
818               int scope, u32 flags, u32 valid_lft, u32 prefered_lft)
819 {
820         struct inet6_ifaddr *ifa = NULL;
821         struct rt6_info *rt;
822         unsigned int hash;
823         int err = 0;
824         int addr_type = ipv6_addr_type(addr);
825
826         if (addr_type == IPV6_ADDR_ANY ||
827             addr_type & IPV6_ADDR_MULTICAST ||
828             (!(idev->dev->flags & IFF_LOOPBACK) &&
829              addr_type & IPV6_ADDR_LOOPBACK))
830                 return ERR_PTR(-EADDRNOTAVAIL);
831
832         rcu_read_lock_bh();
833         if (idev->dead) {
834                 err = -ENODEV;                  /*XXX*/
835                 goto out2;
836         }
837
838         if (idev->cnf.disable_ipv6) {
839                 err = -EACCES;
840                 goto out2;
841         }
842
843         spin_lock(&addrconf_hash_lock);
844
845         /* Ignore adding duplicate addresses on an interface */
846         if (ipv6_chk_same_addr(dev_net(idev->dev), addr, idev->dev)) {
847                 ADBG(("ipv6_add_addr: already assigned\n"));
848                 err = -EEXIST;
849                 goto out;
850         }
851
852         ifa = kzalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC);
853
854         if (ifa == NULL) {
855                 ADBG(("ipv6_add_addr: malloc failed\n"));
856                 err = -ENOBUFS;
857                 goto out;
858         }
859
860         rt = addrconf_dst_alloc(idev, addr, false);
861         if (IS_ERR(rt)) {
862                 err = PTR_ERR(rt);
863                 goto out;
864         }
865
866         ifa->addr = *addr;
867         if (peer_addr)
868                 ifa->peer_addr = *peer_addr;
869
870         spin_lock_init(&ifa->lock);
871         spin_lock_init(&ifa->state_lock);
872         setup_timer(&ifa->dad_timer, addrconf_dad_timer,
873                     (unsigned long)ifa);
874         INIT_HLIST_NODE(&ifa->addr_lst);
875         ifa->scope = scope;
876         ifa->prefix_len = pfxlen;
877         ifa->flags = flags | IFA_F_TENTATIVE;
878         ifa->valid_lft = valid_lft;
879         ifa->prefered_lft = prefered_lft;
880         ifa->cstamp = ifa->tstamp = jiffies;
881         ifa->tokenized = false;
882
883         ifa->rt = rt;
884
885         ifa->idev = idev;
886         in6_dev_hold(idev);
887         /* For caller */
888         in6_ifa_hold(ifa);
889
890         /* Add to big hash table */
891         hash = inet6_addr_hash(addr);
892
893         hlist_add_head_rcu(&ifa->addr_lst, &inet6_addr_lst[hash]);
894         spin_unlock(&addrconf_hash_lock);
895
896         write_lock(&idev->lock);
897         /* Add to inet6_dev unicast addr list. */
898         ipv6_link_dev_addr(idev, ifa);
899
900 #ifdef CONFIG_IPV6_PRIVACY
901         if (ifa->flags&IFA_F_TEMPORARY) {
902                 list_add(&ifa->tmp_list, &idev->tempaddr_list);
903                 in6_ifa_hold(ifa);
904         }
905 #endif
906
907         in6_ifa_hold(ifa);
908         write_unlock(&idev->lock);
909 out2:
910         rcu_read_unlock_bh();
911
912         if (likely(err == 0))
913                 inet6addr_notifier_call_chain(NETDEV_UP, ifa);
914         else {
915                 kfree(ifa);
916                 ifa = ERR_PTR(err);
917         }
918
919         return ifa;
920 out:
921         spin_unlock(&addrconf_hash_lock);
922         goto out2;
923 }
924
925 /* This function wants to get referenced ifp and releases it before return */
926
927 static void ipv6_del_addr(struct inet6_ifaddr *ifp)
928 {
929         struct inet6_ifaddr *ifa, *ifn;
930         struct inet6_dev *idev = ifp->idev;
931         int state;
932         int deleted = 0, onlink = 0;
933         unsigned long expires = jiffies;
934
935         spin_lock_bh(&ifp->state_lock);
936         state = ifp->state;
937         ifp->state = INET6_IFADDR_STATE_DEAD;
938         spin_unlock_bh(&ifp->state_lock);
939
940         if (state == INET6_IFADDR_STATE_DEAD)
941                 goto out;
942
943         spin_lock_bh(&addrconf_hash_lock);
944         hlist_del_init_rcu(&ifp->addr_lst);
945         spin_unlock_bh(&addrconf_hash_lock);
946
947         write_lock_bh(&idev->lock);
948 #ifdef CONFIG_IPV6_PRIVACY
949         if (ifp->flags&IFA_F_TEMPORARY) {
950                 list_del(&ifp->tmp_list);
951                 if (ifp->ifpub) {
952                         in6_ifa_put(ifp->ifpub);
953                         ifp->ifpub = NULL;
954                 }
955                 __in6_ifa_put(ifp);
956         }
957 #endif
958
959         list_for_each_entry_safe(ifa, ifn, &idev->addr_list, if_list) {
960                 if (ifa == ifp) {
961                         list_del_init(&ifp->if_list);
962                         __in6_ifa_put(ifp);
963
964                         if (!(ifp->flags & IFA_F_PERMANENT) || onlink > 0)
965                                 break;
966                         deleted = 1;
967                         continue;
968                 } else if (ifp->flags & IFA_F_PERMANENT) {
969                         if (ipv6_prefix_equal(&ifa->addr, &ifp->addr,
970                                               ifp->prefix_len)) {
971                                 if (ifa->flags & IFA_F_PERMANENT) {
972                                         onlink = 1;
973                                         if (deleted)
974                                                 break;
975                                 } else {
976                                         unsigned long lifetime;
977
978                                         if (!onlink)
979                                                 onlink = -1;
980
981                                         spin_lock(&ifa->lock);
982
983                                         lifetime = addrconf_timeout_fixup(ifa->valid_lft, HZ);
984                                         /*
985                                          * Note: Because this address is
986                                          * not permanent, lifetime <
987                                          * LONG_MAX / HZ here.
988                                          */
989                                         if (time_before(expires,
990                                                         ifa->tstamp + lifetime * HZ))
991                                                 expires = ifa->tstamp + lifetime * HZ;
992                                         spin_unlock(&ifa->lock);
993                                 }
994                         }
995                 }
996         }
997         write_unlock_bh(&idev->lock);
998
999         addrconf_del_dad_timer(ifp);
1000
1001         ipv6_ifa_notify(RTM_DELADDR, ifp);
1002
1003         inet6addr_notifier_call_chain(NETDEV_DOWN, ifp);
1004
1005         /*
1006          * Purge or update corresponding prefix
1007          *
1008          * 1) we don't purge prefix here if address was not permanent.
1009          *    prefix is managed by its own lifetime.
1010          * 2) if there're no addresses, delete prefix.
1011          * 3) if there're still other permanent address(es),
1012          *    corresponding prefix is still permanent.
1013          * 4) otherwise, update prefix lifetime to the
1014          *    longest valid lifetime among the corresponding
1015          *    addresses on the device.
1016          *    Note: subsequent RA will update lifetime.
1017          *
1018          * --yoshfuji
1019          */
1020         if ((ifp->flags & IFA_F_PERMANENT) && onlink < 1) {
1021                 struct in6_addr prefix;
1022                 struct rt6_info *rt;
1023
1024                 ipv6_addr_prefix(&prefix, &ifp->addr, ifp->prefix_len);
1025
1026                 rt = addrconf_get_prefix_route(&prefix,
1027                                                ifp->prefix_len,
1028                                                ifp->idev->dev,
1029                                                0, RTF_GATEWAY | RTF_DEFAULT);
1030
1031                 if (rt) {
1032                         if (onlink == 0) {
1033                                 ip6_del_rt(rt);
1034                                 rt = NULL;
1035                         } else if (!(rt->rt6i_flags & RTF_EXPIRES)) {
1036                                 rt6_set_expires(rt, expires);
1037                         }
1038                 }
1039                 ip6_rt_put(rt);
1040         }
1041
1042         /* clean up prefsrc entries */
1043         rt6_remove_prefsrc(ifp);
1044 out:
1045         in6_ifa_put(ifp);
1046 }
1047
1048 #ifdef CONFIG_IPV6_PRIVACY
1049 static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift)
1050 {
1051         struct inet6_dev *idev = ifp->idev;
1052         struct in6_addr addr, *tmpaddr;
1053         unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_tstamp, age;
1054         unsigned long regen_advance;
1055         int tmp_plen;
1056         int ret = 0;
1057         int max_addresses;
1058         u32 addr_flags;
1059         unsigned long now = jiffies;
1060
1061         write_lock(&idev->lock);
1062         if (ift) {
1063                 spin_lock_bh(&ift->lock);
1064                 memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8);
1065                 spin_unlock_bh(&ift->lock);
1066                 tmpaddr = &addr;
1067         } else {
1068                 tmpaddr = NULL;
1069         }
1070 retry:
1071         in6_dev_hold(idev);
1072         if (idev->cnf.use_tempaddr <= 0) {
1073                 write_unlock(&idev->lock);
1074                 pr_info("%s: use_tempaddr is disabled\n", __func__);
1075                 in6_dev_put(idev);
1076                 ret = -1;
1077                 goto out;
1078         }
1079         spin_lock_bh(&ifp->lock);
1080         if (ifp->regen_count++ >= idev->cnf.regen_max_retry) {
1081                 idev->cnf.use_tempaddr = -1;    /*XXX*/
1082                 spin_unlock_bh(&ifp->lock);
1083                 write_unlock(&idev->lock);
1084                 pr_warn("%s: regeneration time exceeded - disabled temporary address support\n",
1085                         __func__);
1086                 in6_dev_put(idev);
1087                 ret = -1;
1088                 goto out;
1089         }
1090         in6_ifa_hold(ifp);
1091         memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
1092         __ipv6_try_regen_rndid(idev, tmpaddr);
1093         memcpy(&addr.s6_addr[8], idev->rndid, 8);
1094         age = (now - ifp->tstamp) / HZ;
1095         tmp_valid_lft = min_t(__u32,
1096                               ifp->valid_lft,
1097                               idev->cnf.temp_valid_lft + age);
1098         tmp_prefered_lft = min_t(__u32,
1099                                  ifp->prefered_lft,
1100                                  idev->cnf.temp_prefered_lft + age -
1101                                  idev->cnf.max_desync_factor);
1102         tmp_plen = ifp->prefix_len;
1103         max_addresses = idev->cnf.max_addresses;
1104         tmp_tstamp = ifp->tstamp;
1105         spin_unlock_bh(&ifp->lock);
1106
1107         regen_advance = idev->cnf.regen_max_retry *
1108                         idev->cnf.dad_transmits *
1109                         idev->nd_parms->retrans_time / HZ;
1110         write_unlock(&idev->lock);
1111
1112         /* A temporary address is created only if this calculated Preferred
1113          * Lifetime is greater than REGEN_ADVANCE time units.  In particular,
1114          * an implementation must not create a temporary address with a zero
1115          * Preferred Lifetime.
1116          */
1117         if (tmp_prefered_lft <= regen_advance) {
1118                 in6_ifa_put(ifp);
1119                 in6_dev_put(idev);
1120                 ret = -1;
1121                 goto out;
1122         }
1123
1124         addr_flags = IFA_F_TEMPORARY;
1125         /* set in addrconf_prefix_rcv() */
1126         if (ifp->flags & IFA_F_OPTIMISTIC)
1127                 addr_flags |= IFA_F_OPTIMISTIC;
1128
1129         ift = !max_addresses ||
1130               ipv6_count_addresses(idev) < max_addresses ?
1131                 ipv6_add_addr(idev, &addr, NULL, tmp_plen,
1132                               ipv6_addr_scope(&addr), addr_flags,
1133                               tmp_valid_lft, tmp_prefered_lft) : NULL;
1134         if (IS_ERR_OR_NULL(ift)) {
1135                 in6_ifa_put(ifp);
1136                 in6_dev_put(idev);
1137                 pr_info("%s: retry temporary address regeneration\n", __func__);
1138                 tmpaddr = &addr;
1139                 write_lock(&idev->lock);
1140                 goto retry;
1141         }
1142
1143         spin_lock_bh(&ift->lock);
1144         ift->ifpub = ifp;
1145         ift->cstamp = now;
1146         ift->tstamp = tmp_tstamp;
1147         spin_unlock_bh(&ift->lock);
1148
1149         addrconf_dad_start(ift);
1150         in6_ifa_put(ift);
1151         in6_dev_put(idev);
1152 out:
1153         return ret;
1154 }
1155 #endif
1156
1157 /*
1158  *      Choose an appropriate source address (RFC3484)
1159  */
1160 enum {
1161         IPV6_SADDR_RULE_INIT = 0,
1162         IPV6_SADDR_RULE_LOCAL,
1163         IPV6_SADDR_RULE_SCOPE,
1164         IPV6_SADDR_RULE_PREFERRED,
1165 #ifdef CONFIG_IPV6_MIP6
1166         IPV6_SADDR_RULE_HOA,
1167 #endif
1168         IPV6_SADDR_RULE_OIF,
1169         IPV6_SADDR_RULE_LABEL,
1170 #ifdef CONFIG_IPV6_PRIVACY
1171         IPV6_SADDR_RULE_PRIVACY,
1172 #endif
1173         IPV6_SADDR_RULE_ORCHID,
1174         IPV6_SADDR_RULE_PREFIX,
1175         IPV6_SADDR_RULE_MAX
1176 };
1177
1178 struct ipv6_saddr_score {
1179         int                     rule;
1180         int                     addr_type;
1181         struct inet6_ifaddr     *ifa;
1182         DECLARE_BITMAP(scorebits, IPV6_SADDR_RULE_MAX);
1183         int                     scopedist;
1184         int                     matchlen;
1185 };
1186
1187 struct ipv6_saddr_dst {
1188         const struct in6_addr *addr;
1189         int ifindex;
1190         int scope;
1191         int label;
1192         unsigned int prefs;
1193 };
1194
1195 static inline int ipv6_saddr_preferred(int type)
1196 {
1197         if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|IPV6_ADDR_LOOPBACK))
1198                 return 1;
1199         return 0;
1200 }
1201
1202 static int ipv6_get_saddr_eval(struct net *net,
1203                                struct ipv6_saddr_score *score,
1204                                struct ipv6_saddr_dst *dst,
1205                                int i)
1206 {
1207         int ret;
1208
1209         if (i <= score->rule) {
1210                 switch (i) {
1211                 case IPV6_SADDR_RULE_SCOPE:
1212                         ret = score->scopedist;
1213                         break;
1214                 case IPV6_SADDR_RULE_PREFIX:
1215                         ret = score->matchlen;
1216                         break;
1217                 default:
1218                         ret = !!test_bit(i, score->scorebits);
1219                 }
1220                 goto out;
1221         }
1222
1223         switch (i) {
1224         case IPV6_SADDR_RULE_INIT:
1225                 /* Rule 0: remember if hiscore is not ready yet */
1226                 ret = !!score->ifa;
1227                 break;
1228         case IPV6_SADDR_RULE_LOCAL:
1229                 /* Rule 1: Prefer same address */
1230                 ret = ipv6_addr_equal(&score->ifa->addr, dst->addr);
1231                 break;
1232         case IPV6_SADDR_RULE_SCOPE:
1233                 /* Rule 2: Prefer appropriate scope
1234                  *
1235                  *      ret
1236                  *       ^
1237                  *    -1 |  d 15
1238                  *    ---+--+-+---> scope
1239                  *       |
1240                  *       |             d is scope of the destination.
1241                  *  B-d  |  \
1242                  *       |   \      <- smaller scope is better if
1243                  *  B-15 |    \        if scope is enough for destinaion.
1244                  *       |             ret = B - scope (-1 <= scope >= d <= 15).
1245                  * d-C-1 | /
1246                  *       |/         <- greater is better
1247                  *   -C  /             if scope is not enough for destination.
1248                  *      /|             ret = scope - C (-1 <= d < scope <= 15).
1249                  *
1250                  * d - C - 1 < B -15 (for all -1 <= d <= 15).
1251                  * C > d + 14 - B >= 15 + 14 - B = 29 - B.
1252                  * Assume B = 0 and we get C > 29.
1253                  */
1254                 ret = __ipv6_addr_src_scope(score->addr_type);
1255                 if (ret >= dst->scope)
1256                         ret = -ret;
1257                 else
1258                         ret -= 128;     /* 30 is enough */
1259                 score->scopedist = ret;
1260                 break;
1261         case IPV6_SADDR_RULE_PREFERRED:
1262                 /* Rule 3: Avoid deprecated and optimistic addresses */
1263                 ret = ipv6_saddr_preferred(score->addr_type) ||
1264                       !(score->ifa->flags & (IFA_F_DEPRECATED|IFA_F_OPTIMISTIC));
1265                 break;
1266 #ifdef CONFIG_IPV6_MIP6
1267         case IPV6_SADDR_RULE_HOA:
1268             {
1269                 /* Rule 4: Prefer home address */
1270                 int prefhome = !(dst->prefs & IPV6_PREFER_SRC_COA);
1271                 ret = !(score->ifa->flags & IFA_F_HOMEADDRESS) ^ prefhome;
1272                 break;
1273             }
1274 #endif
1275         case IPV6_SADDR_RULE_OIF:
1276                 /* Rule 5: Prefer outgoing interface */
1277                 ret = (!dst->ifindex ||
1278                        dst->ifindex == score->ifa->idev->dev->ifindex);
1279                 break;
1280         case IPV6_SADDR_RULE_LABEL:
1281                 /* Rule 6: Prefer matching label */
1282                 ret = ipv6_addr_label(net,
1283                                       &score->ifa->addr, score->addr_type,
1284                                       score->ifa->idev->dev->ifindex) == dst->label;
1285                 break;
1286 #ifdef CONFIG_IPV6_PRIVACY
1287         case IPV6_SADDR_RULE_PRIVACY:
1288             {
1289                 /* Rule 7: Prefer public address
1290                  * Note: prefer temporary address if use_tempaddr >= 2
1291                  */
1292                 int preftmp = dst->prefs & (IPV6_PREFER_SRC_PUBLIC|IPV6_PREFER_SRC_TMP) ?
1293                                 !!(dst->prefs & IPV6_PREFER_SRC_TMP) :
1294                                 score->ifa->idev->cnf.use_tempaddr >= 2;
1295                 ret = (!(score->ifa->flags & IFA_F_TEMPORARY)) ^ preftmp;
1296                 break;
1297             }
1298 #endif
1299         case IPV6_SADDR_RULE_ORCHID:
1300                 /* Rule 8-: Prefer ORCHID vs ORCHID or
1301                  *          non-ORCHID vs non-ORCHID
1302                  */
1303                 ret = !(ipv6_addr_orchid(&score->ifa->addr) ^
1304                         ipv6_addr_orchid(dst->addr));
1305                 break;
1306         case IPV6_SADDR_RULE_PREFIX:
1307                 /* Rule 8: Use longest matching prefix */
1308                 ret = ipv6_addr_diff(&score->ifa->addr, dst->addr);
1309                 if (ret > score->ifa->prefix_len)
1310                         ret = score->ifa->prefix_len;
1311                 score->matchlen = ret;
1312                 break;
1313         default:
1314                 ret = 0;
1315         }
1316
1317         if (ret)
1318                 __set_bit(i, score->scorebits);
1319         score->rule = i;
1320 out:
1321         return ret;
1322 }
1323
1324 int ipv6_dev_get_saddr(struct net *net, const struct net_device *dst_dev,
1325                        const struct in6_addr *daddr, unsigned int prefs,
1326                        struct in6_addr *saddr)
1327 {
1328         struct ipv6_saddr_score scores[2],
1329                                 *score = &scores[0], *hiscore = &scores[1];
1330         struct ipv6_saddr_dst dst;
1331         struct net_device *dev;
1332         int dst_type;
1333
1334         dst_type = __ipv6_addr_type(daddr);
1335         dst.addr = daddr;
1336         dst.ifindex = dst_dev ? dst_dev->ifindex : 0;
1337         dst.scope = __ipv6_addr_src_scope(dst_type);
1338         dst.label = ipv6_addr_label(net, daddr, dst_type, dst.ifindex);
1339         dst.prefs = prefs;
1340
1341         hiscore->rule = -1;
1342         hiscore->ifa = NULL;
1343
1344         rcu_read_lock();
1345
1346         for_each_netdev_rcu(net, dev) {
1347                 struct inet6_dev *idev;
1348
1349                 /* Candidate Source Address (section 4)
1350                  *  - multicast and link-local destination address,
1351                  *    the set of candidate source address MUST only
1352                  *    include addresses assigned to interfaces
1353                  *    belonging to the same link as the outgoing
1354                  *    interface.
1355                  * (- For site-local destination addresses, the
1356                  *    set of candidate source addresses MUST only
1357                  *    include addresses assigned to interfaces
1358                  *    belonging to the same site as the outgoing
1359                  *    interface.)
1360                  */
1361                 if (((dst_type & IPV6_ADDR_MULTICAST) ||
1362                      dst.scope <= IPV6_ADDR_SCOPE_LINKLOCAL) &&
1363                     dst.ifindex && dev->ifindex != dst.ifindex)
1364                         continue;
1365
1366                 idev = __in6_dev_get(dev);
1367                 if (!idev)
1368                         continue;
1369
1370                 read_lock_bh(&idev->lock);
1371                 list_for_each_entry(score->ifa, &idev->addr_list, if_list) {
1372                         int i;
1373
1374                         /*
1375                          * - Tentative Address (RFC2462 section 5.4)
1376                          *  - A tentative address is not considered
1377                          *    "assigned to an interface" in the traditional
1378                          *    sense, unless it is also flagged as optimistic.
1379                          * - Candidate Source Address (section 4)
1380                          *  - In any case, anycast addresses, multicast
1381                          *    addresses, and the unspecified address MUST
1382                          *    NOT be included in a candidate set.
1383                          */
1384                         if ((score->ifa->flags & IFA_F_TENTATIVE) &&
1385                             (!(score->ifa->flags & IFA_F_OPTIMISTIC)))
1386                                 continue;
1387
1388                         score->addr_type = __ipv6_addr_type(&score->ifa->addr);
1389
1390                         if (unlikely(score->addr_type == IPV6_ADDR_ANY ||
1391                                      score->addr_type & IPV6_ADDR_MULTICAST)) {
1392                                 LIMIT_NETDEBUG(KERN_DEBUG
1393                                                "ADDRCONF: unspecified / multicast address "
1394                                                "assigned as unicast address on %s",
1395                                                dev->name);
1396                                 continue;
1397                         }
1398
1399                         score->rule = -1;
1400                         bitmap_zero(score->scorebits, IPV6_SADDR_RULE_MAX);
1401
1402                         for (i = 0; i < IPV6_SADDR_RULE_MAX; i++) {
1403                                 int minihiscore, miniscore;
1404
1405                                 minihiscore = ipv6_get_saddr_eval(net, hiscore, &dst, i);
1406                                 miniscore = ipv6_get_saddr_eval(net, score, &dst, i);
1407
1408                                 if (minihiscore > miniscore) {
1409                                         if (i == IPV6_SADDR_RULE_SCOPE &&
1410                                             score->scopedist > 0) {
1411                                                 /*
1412                                                  * special case:
1413                                                  * each remaining entry
1414                                                  * has too small (not enough)
1415                                                  * scope, because ifa entries
1416                                                  * are sorted by their scope
1417                                                  * values.
1418                                                  */
1419                                                 goto try_nextdev;
1420                                         }
1421                                         break;
1422                                 } else if (minihiscore < miniscore) {
1423                                         if (hiscore->ifa)
1424                                                 in6_ifa_put(hiscore->ifa);
1425
1426                                         in6_ifa_hold(score->ifa);
1427
1428                                         swap(hiscore, score);
1429
1430                                         /* restore our iterator */
1431                                         score->ifa = hiscore->ifa;
1432
1433                                         break;
1434                                 }
1435                         }
1436                 }
1437 try_nextdev:
1438                 read_unlock_bh(&idev->lock);
1439         }
1440         rcu_read_unlock();
1441
1442         if (!hiscore->ifa)
1443                 return -EADDRNOTAVAIL;
1444
1445         *saddr = hiscore->ifa->addr;
1446         in6_ifa_put(hiscore->ifa);
1447         return 0;
1448 }
1449 EXPORT_SYMBOL(ipv6_dev_get_saddr);
1450
1451 int __ipv6_get_lladdr(struct inet6_dev *idev, struct in6_addr *addr,
1452                       unsigned char banned_flags)
1453 {
1454         struct inet6_ifaddr *ifp;
1455         int err = -EADDRNOTAVAIL;
1456
1457         list_for_each_entry(ifp, &idev->addr_list, if_list) {
1458                 if (ifp->scope == IFA_LINK &&
1459                     !(ifp->flags & banned_flags)) {
1460                         *addr = ifp->addr;
1461                         err = 0;
1462                         break;
1463                 }
1464         }
1465         return err;
1466 }
1467
1468 int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr,
1469                     unsigned char banned_flags)
1470 {
1471         struct inet6_dev *idev;
1472         int err = -EADDRNOTAVAIL;
1473
1474         rcu_read_lock();
1475         idev = __in6_dev_get(dev);
1476         if (idev) {
1477                 read_lock_bh(&idev->lock);
1478                 err = __ipv6_get_lladdr(idev, addr, banned_flags);
1479                 read_unlock_bh(&idev->lock);
1480         }
1481         rcu_read_unlock();
1482         return err;
1483 }
1484
1485 static int ipv6_count_addresses(struct inet6_dev *idev)
1486 {
1487         int cnt = 0;
1488         struct inet6_ifaddr *ifp;
1489
1490         read_lock_bh(&idev->lock);
1491         list_for_each_entry(ifp, &idev->addr_list, if_list)
1492                 cnt++;
1493         read_unlock_bh(&idev->lock);
1494         return cnt;
1495 }
1496
1497 int ipv6_chk_addr(struct net *net, const struct in6_addr *addr,
1498                   const struct net_device *dev, int strict)
1499 {
1500         struct inet6_ifaddr *ifp;
1501         unsigned int hash = inet6_addr_hash(addr);
1502
1503         rcu_read_lock_bh();
1504         hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) {
1505                 if (!net_eq(dev_net(ifp->idev->dev), net))
1506                         continue;
1507                 if (ipv6_addr_equal(&ifp->addr, addr) &&
1508                     !(ifp->flags&IFA_F_TENTATIVE) &&
1509                     (dev == NULL || ifp->idev->dev == dev ||
1510                      !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))) {
1511                         rcu_read_unlock_bh();
1512                         return 1;
1513                 }
1514         }
1515
1516         rcu_read_unlock_bh();
1517         return 0;
1518 }
1519 EXPORT_SYMBOL(ipv6_chk_addr);
1520
1521 static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
1522                                struct net_device *dev)
1523 {
1524         unsigned int hash = inet6_addr_hash(addr);
1525         struct inet6_ifaddr *ifp;
1526
1527         hlist_for_each_entry(ifp, &inet6_addr_lst[hash], addr_lst) {
1528                 if (!net_eq(dev_net(ifp->idev->dev), net))
1529                         continue;
1530                 if (ipv6_addr_equal(&ifp->addr, addr)) {
1531                         if (dev == NULL || ifp->idev->dev == dev)
1532                                 return true;
1533                 }
1534         }
1535         return false;
1536 }
1537
1538 int ipv6_chk_prefix(const struct in6_addr *addr, struct net_device *dev)
1539 {
1540         struct inet6_dev *idev;
1541         struct inet6_ifaddr *ifa;
1542         int     onlink;
1543
1544         onlink = 0;
1545         rcu_read_lock();
1546         idev = __in6_dev_get(dev);
1547         if (idev) {
1548                 read_lock_bh(&idev->lock);
1549                 list_for_each_entry(ifa, &idev->addr_list, if_list) {
1550                         onlink = ipv6_prefix_equal(addr, &ifa->addr,
1551                                                    ifa->prefix_len);
1552                         if (onlink)
1553                                 break;
1554                 }
1555                 read_unlock_bh(&idev->lock);
1556         }
1557         rcu_read_unlock();
1558         return onlink;
1559 }
1560 EXPORT_SYMBOL(ipv6_chk_prefix);
1561
1562 struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, const struct in6_addr *addr,
1563                                      struct net_device *dev, int strict)
1564 {
1565         struct inet6_ifaddr *ifp, *result = NULL;
1566         unsigned int hash = inet6_addr_hash(addr);
1567
1568         rcu_read_lock_bh();
1569         hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) {
1570                 if (!net_eq(dev_net(ifp->idev->dev), net))
1571                         continue;
1572                 if (ipv6_addr_equal(&ifp->addr, addr)) {
1573                         if (dev == NULL || ifp->idev->dev == dev ||
1574                             !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
1575                                 result = ifp;
1576                                 in6_ifa_hold(ifp);
1577                                 break;
1578                         }
1579                 }
1580         }
1581         rcu_read_unlock_bh();
1582
1583         return result;
1584 }
1585
1586 /* Gets referenced address, destroys ifaddr */
1587
1588 static void addrconf_dad_stop(struct inet6_ifaddr *ifp, int dad_failed)
1589 {
1590         if (ifp->flags&IFA_F_PERMANENT) {
1591                 spin_lock_bh(&ifp->lock);
1592                 addrconf_del_dad_timer(ifp);
1593                 ifp->flags |= IFA_F_TENTATIVE;
1594                 if (dad_failed)
1595                         ifp->flags |= IFA_F_DADFAILED;
1596                 spin_unlock_bh(&ifp->lock);
1597                 if (dad_failed)
1598                         ipv6_ifa_notify(0, ifp);
1599                 in6_ifa_put(ifp);
1600 #ifdef CONFIG_IPV6_PRIVACY
1601         } else if (ifp->flags&IFA_F_TEMPORARY) {
1602                 struct inet6_ifaddr *ifpub;
1603                 spin_lock_bh(&ifp->lock);
1604                 ifpub = ifp->ifpub;
1605                 if (ifpub) {
1606                         in6_ifa_hold(ifpub);
1607                         spin_unlock_bh(&ifp->lock);
1608                         ipv6_create_tempaddr(ifpub, ifp);
1609                         in6_ifa_put(ifpub);
1610                 } else {
1611                         spin_unlock_bh(&ifp->lock);
1612                 }
1613                 ipv6_del_addr(ifp);
1614 #endif
1615         } else
1616                 ipv6_del_addr(ifp);
1617 }
1618
1619 static int addrconf_dad_end(struct inet6_ifaddr *ifp)
1620 {
1621         int err = -ENOENT;
1622
1623         spin_lock(&ifp->state_lock);
1624         if (ifp->state == INET6_IFADDR_STATE_DAD) {
1625                 ifp->state = INET6_IFADDR_STATE_POSTDAD;
1626                 err = 0;
1627         }
1628         spin_unlock(&ifp->state_lock);
1629
1630         return err;
1631 }
1632
1633 void addrconf_dad_failure(struct inet6_ifaddr *ifp)
1634 {
1635         struct inet6_dev *idev = ifp->idev;
1636
1637         if (addrconf_dad_end(ifp)) {
1638                 in6_ifa_put(ifp);
1639                 return;
1640         }
1641
1642         net_info_ratelimited("%s: IPv6 duplicate address %pI6c detected!\n",
1643                              ifp->idev->dev->name, &ifp->addr);
1644
1645         if (idev->cnf.accept_dad > 1 && !idev->cnf.disable_ipv6) {
1646                 struct in6_addr addr;
1647
1648                 addr.s6_addr32[0] = htonl(0xfe800000);
1649                 addr.s6_addr32[1] = 0;
1650
1651                 if (!ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) &&
1652                     ipv6_addr_equal(&ifp->addr, &addr)) {
1653                         /* DAD failed for link-local based on MAC address */
1654                         idev->cnf.disable_ipv6 = 1;
1655
1656                         pr_info("%s: IPv6 being disabled!\n",
1657                                 ifp->idev->dev->name);
1658                 }
1659         }
1660
1661         addrconf_dad_stop(ifp, 1);
1662 }
1663
1664 /* Join to solicited addr multicast group. */
1665
1666 void addrconf_join_solict(struct net_device *dev, const struct in6_addr *addr)
1667 {
1668         struct in6_addr maddr;
1669
1670         if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1671                 return;
1672
1673         addrconf_addr_solict_mult(addr, &maddr);
1674         ipv6_dev_mc_inc(dev, &maddr);
1675 }
1676
1677 void addrconf_leave_solict(struct inet6_dev *idev, const struct in6_addr *addr)
1678 {
1679         struct in6_addr maddr;
1680
1681         if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1682                 return;
1683
1684         addrconf_addr_solict_mult(addr, &maddr);
1685         __ipv6_dev_mc_dec(idev, &maddr);
1686 }
1687
1688 static void addrconf_join_anycast(struct inet6_ifaddr *ifp)
1689 {
1690         struct in6_addr addr;
1691         if (ifp->prefix_len == 127) /* RFC 6164 */
1692                 return;
1693         ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1694         if (ipv6_addr_any(&addr))
1695                 return;
1696         ipv6_dev_ac_inc(ifp->idev->dev, &addr);
1697 }
1698
1699 static void addrconf_leave_anycast(struct inet6_ifaddr *ifp)
1700 {
1701         struct in6_addr addr;
1702         if (ifp->prefix_len == 127) /* RFC 6164 */
1703                 return;
1704         ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1705         if (ipv6_addr_any(&addr))
1706                 return;
1707         __ipv6_dev_ac_dec(ifp->idev, &addr);
1708 }
1709
1710 static int addrconf_ifid_eui48(u8 *eui, struct net_device *dev)
1711 {
1712         if (dev->addr_len != ETH_ALEN)
1713                 return -1;
1714         memcpy(eui, dev->dev_addr, 3);
1715         memcpy(eui + 5, dev->dev_addr + 3, 3);
1716
1717         /*
1718          * The zSeries OSA network cards can be shared among various
1719          * OS instances, but the OSA cards have only one MAC address.
1720          * This leads to duplicate address conflicts in conjunction
1721          * with IPv6 if more than one instance uses the same card.
1722          *
1723          * The driver for these cards can deliver a unique 16-bit
1724          * identifier for each instance sharing the same card.  It is
1725          * placed instead of 0xFFFE in the interface identifier.  The
1726          * "u" bit of the interface identifier is not inverted in this
1727          * case.  Hence the resulting interface identifier has local
1728          * scope according to RFC2373.
1729          */
1730         if (dev->dev_id) {
1731                 eui[3] = (dev->dev_id >> 8) & 0xFF;
1732                 eui[4] = dev->dev_id & 0xFF;
1733         } else {
1734                 eui[3] = 0xFF;
1735                 eui[4] = 0xFE;
1736                 eui[0] ^= 2;
1737         }
1738         return 0;
1739 }
1740
1741 static int addrconf_ifid_eui64(u8 *eui, struct net_device *dev)
1742 {
1743         if (dev->addr_len != IEEE802154_ADDR_LEN)
1744                 return -1;
1745         memcpy(eui, dev->dev_addr, 8);
1746         eui[0] ^= 2;
1747         return 0;
1748 }
1749
1750 static int addrconf_ifid_ieee1394(u8 *eui, struct net_device *dev)
1751 {
1752         union fwnet_hwaddr *ha;
1753
1754         if (dev->addr_len != FWNET_ALEN)
1755                 return -1;
1756
1757         ha = (union fwnet_hwaddr *)dev->dev_addr;
1758
1759         memcpy(eui, &ha->uc.uniq_id, sizeof(ha->uc.uniq_id));
1760         eui[0] ^= 2;
1761         return 0;
1762 }
1763
1764 static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev)
1765 {
1766         /* XXX: inherit EUI-64 from other interface -- yoshfuji */
1767         if (dev->addr_len != ARCNET_ALEN)
1768                 return -1;
1769         memset(eui, 0, 7);
1770         eui[7] = *(u8 *)dev->dev_addr;
1771         return 0;
1772 }
1773
1774 static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev)
1775 {
1776         if (dev->addr_len != INFINIBAND_ALEN)
1777                 return -1;
1778         memcpy(eui, dev->dev_addr + 12, 8);
1779         eui[0] |= 2;
1780         return 0;
1781 }
1782
1783 static int __ipv6_isatap_ifid(u8 *eui, __be32 addr)
1784 {
1785         if (addr == 0)
1786                 return -1;
1787         eui[0] = (ipv4_is_zeronet(addr) || ipv4_is_private_10(addr) ||
1788                   ipv4_is_loopback(addr) || ipv4_is_linklocal_169(addr) ||
1789                   ipv4_is_private_172(addr) || ipv4_is_test_192(addr) ||
1790                   ipv4_is_anycast_6to4(addr) || ipv4_is_private_192(addr) ||
1791                   ipv4_is_test_198(addr) || ipv4_is_multicast(addr) ||
1792                   ipv4_is_lbcast(addr)) ? 0x00 : 0x02;
1793         eui[1] = 0;
1794         eui[2] = 0x5E;
1795         eui[3] = 0xFE;
1796         memcpy(eui + 4, &addr, 4);
1797         return 0;
1798 }
1799
1800 static int addrconf_ifid_sit(u8 *eui, struct net_device *dev)
1801 {
1802         if (dev->priv_flags & IFF_ISATAP)
1803                 return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
1804         return -1;
1805 }
1806
1807 static int addrconf_ifid_gre(u8 *eui, struct net_device *dev)
1808 {
1809         return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
1810 }
1811
1812 static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
1813 {
1814         switch (dev->type) {
1815         case ARPHRD_ETHER:
1816         case ARPHRD_FDDI:
1817                 return addrconf_ifid_eui48(eui, dev);
1818         case ARPHRD_ARCNET:
1819                 return addrconf_ifid_arcnet(eui, dev);
1820         case ARPHRD_INFINIBAND:
1821                 return addrconf_ifid_infiniband(eui, dev);
1822         case ARPHRD_SIT:
1823                 return addrconf_ifid_sit(eui, dev);
1824         case ARPHRD_IPGRE:
1825                 return addrconf_ifid_gre(eui, dev);
1826         case ARPHRD_IEEE802154:
1827                 return addrconf_ifid_eui64(eui, dev);
1828         case ARPHRD_IEEE1394:
1829                 return addrconf_ifid_ieee1394(eui, dev);
1830         }
1831         return -1;
1832 }
1833
1834 static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
1835 {
1836         int err = -1;
1837         struct inet6_ifaddr *ifp;
1838
1839         read_lock_bh(&idev->lock);
1840         list_for_each_entry(ifp, &idev->addr_list, if_list) {
1841                 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
1842                         memcpy(eui, ifp->addr.s6_addr+8, 8);
1843                         err = 0;
1844                         break;
1845                 }
1846         }
1847         read_unlock_bh(&idev->lock);
1848         return err;
1849 }
1850
1851 #ifdef CONFIG_IPV6_PRIVACY
1852 /* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
1853 static void __ipv6_regen_rndid(struct inet6_dev *idev)
1854 {
1855 regen:
1856         get_random_bytes(idev->rndid, sizeof(idev->rndid));
1857         idev->rndid[0] &= ~0x02;
1858
1859         /*
1860          * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
1861          * check if generated address is not inappropriate
1862          *
1863          *  - Reserved subnet anycast (RFC 2526)
1864          *      11111101 11....11 1xxxxxxx
1865          *  - ISATAP (RFC4214) 6.1
1866          *      00-00-5E-FE-xx-xx-xx-xx
1867          *  - value 0
1868          *  - XXX: already assigned to an address on the device
1869          */
1870         if (idev->rndid[0] == 0xfd &&
1871             (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff &&
1872             (idev->rndid[7]&0x80))
1873                 goto regen;
1874         if ((idev->rndid[0]|idev->rndid[1]) == 0) {
1875                 if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe)
1876                         goto regen;
1877                 if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00)
1878                         goto regen;
1879         }
1880 }
1881
1882 static void ipv6_regen_rndid(unsigned long data)
1883 {
1884         struct inet6_dev *idev = (struct inet6_dev *) data;
1885         unsigned long expires;
1886
1887         rcu_read_lock_bh();
1888         write_lock_bh(&idev->lock);
1889
1890         if (idev->dead)
1891                 goto out;
1892
1893         __ipv6_regen_rndid(idev);
1894
1895         expires = jiffies +
1896                 idev->cnf.temp_prefered_lft * HZ -
1897                 idev->cnf.regen_max_retry * idev->cnf.dad_transmits * idev->nd_parms->retrans_time -
1898                 idev->cnf.max_desync_factor * HZ;
1899         if (time_before(expires, jiffies)) {
1900                 pr_warn("%s: too short regeneration interval; timer disabled for %s\n",
1901                         __func__, idev->dev->name);
1902                 goto out;
1903         }
1904
1905         if (!mod_timer(&idev->regen_timer, expires))
1906                 in6_dev_hold(idev);
1907
1908 out:
1909         write_unlock_bh(&idev->lock);
1910         rcu_read_unlock_bh();
1911         in6_dev_put(idev);
1912 }
1913
1914 static void  __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr)
1915 {
1916         if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0)
1917                 __ipv6_regen_rndid(idev);
1918 }
1919 #endif
1920
1921 /*
1922  *      Add prefix route.
1923  */
1924
1925 static void
1926 addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev,
1927                       unsigned long expires, u32 flags)
1928 {
1929         struct fib6_config cfg = {
1930                 .fc_table = RT6_TABLE_PREFIX,
1931                 .fc_metric = IP6_RT_PRIO_ADDRCONF,
1932                 .fc_ifindex = dev->ifindex,
1933                 .fc_expires = expires,
1934                 .fc_dst_len = plen,
1935                 .fc_flags = RTF_UP | flags,
1936                 .fc_nlinfo.nl_net = dev_net(dev),
1937                 .fc_protocol = RTPROT_KERNEL,
1938         };
1939
1940         cfg.fc_dst = *pfx;
1941
1942         /* Prevent useless cloning on PtP SIT.
1943            This thing is done here expecting that the whole
1944            class of non-broadcast devices need not cloning.
1945          */
1946 #if IS_ENABLED(CONFIG_IPV6_SIT)
1947         if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT))
1948                 cfg.fc_flags |= RTF_NONEXTHOP;
1949 #endif
1950
1951         ip6_route_add(&cfg);
1952 }
1953
1954
1955 static struct rt6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
1956                                                   int plen,
1957                                                   const struct net_device *dev,
1958                                                   u32 flags, u32 noflags)
1959 {
1960         struct fib6_node *fn;
1961         struct rt6_info *rt = NULL;
1962         struct fib6_table *table;
1963
1964         table = fib6_get_table(dev_net(dev), RT6_TABLE_PREFIX);
1965         if (table == NULL)
1966                 return NULL;
1967
1968         read_lock_bh(&table->tb6_lock);
1969         fn = fib6_locate(&table->tb6_root, pfx, plen, NULL, 0);
1970         if (!fn)
1971                 goto out;
1972         for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
1973                 if (rt->dst.dev->ifindex != dev->ifindex)
1974                         continue;
1975                 if ((rt->rt6i_flags & flags) != flags)
1976                         continue;
1977                 if ((rt->rt6i_flags & noflags) != 0)
1978                         continue;
1979                 dst_hold(&rt->dst);
1980                 break;
1981         }
1982 out:
1983         read_unlock_bh(&table->tb6_lock);
1984         return rt;
1985 }
1986
1987
1988 /* Create "default" multicast route to the interface */
1989
1990 static void addrconf_add_mroute(struct net_device *dev)
1991 {
1992         struct fib6_config cfg = {
1993                 .fc_table = RT6_TABLE_LOCAL,
1994                 .fc_metric = IP6_RT_PRIO_ADDRCONF,
1995                 .fc_ifindex = dev->ifindex,
1996                 .fc_dst_len = 8,
1997                 .fc_flags = RTF_UP,
1998                 .fc_nlinfo.nl_net = dev_net(dev),
1999         };
2000
2001         ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0);
2002
2003         ip6_route_add(&cfg);
2004 }
2005
2006 #if IS_ENABLED(CONFIG_IPV6_SIT)
2007 static void sit_route_add(struct net_device *dev)
2008 {
2009         struct fib6_config cfg = {
2010                 .fc_table = RT6_TABLE_MAIN,
2011                 .fc_metric = IP6_RT_PRIO_ADDRCONF,
2012                 .fc_ifindex = dev->ifindex,
2013                 .fc_dst_len = 96,
2014                 .fc_flags = RTF_UP | RTF_NONEXTHOP,
2015                 .fc_nlinfo.nl_net = dev_net(dev),
2016         };
2017
2018         /* prefix length - 96 bits "::d.d.d.d" */
2019         ip6_route_add(&cfg);
2020 }
2021 #endif
2022
2023 static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
2024 {
2025         struct inet6_dev *idev;
2026
2027         ASSERT_RTNL();
2028
2029         idev = ipv6_find_idev(dev);
2030         if (!idev)
2031                 return ERR_PTR(-ENOBUFS);
2032
2033         if (idev->cnf.disable_ipv6)
2034                 return ERR_PTR(-EACCES);
2035
2036         /* Add default multicast route */
2037         if (!(dev->flags & IFF_LOOPBACK))
2038                 addrconf_add_mroute(dev);
2039
2040         return idev;
2041 }
2042
2043 void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len, bool sllao)
2044 {
2045         struct prefix_info *pinfo;
2046         __u32 valid_lft;
2047         __u32 prefered_lft;
2048         int addr_type;
2049         struct inet6_dev *in6_dev;
2050         struct net *net = dev_net(dev);
2051
2052         pinfo = (struct prefix_info *) opt;
2053
2054         if (len < sizeof(struct prefix_info)) {
2055                 ADBG(("addrconf: prefix option too short\n"));
2056                 return;
2057         }
2058
2059         /*
2060          *      Validation checks ([ADDRCONF], page 19)
2061          */
2062
2063         addr_type = ipv6_addr_type(&pinfo->prefix);
2064
2065         if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
2066                 return;
2067
2068         valid_lft = ntohl(pinfo->valid);
2069         prefered_lft = ntohl(pinfo->prefered);
2070
2071         if (prefered_lft > valid_lft) {
2072                 net_warn_ratelimited("addrconf: prefix option has invalid lifetime\n");
2073                 return;
2074         }
2075
2076         in6_dev = in6_dev_get(dev);
2077
2078         if (in6_dev == NULL) {
2079                 net_dbg_ratelimited("addrconf: device %s not configured\n",
2080                                     dev->name);
2081                 return;
2082         }
2083
2084         /*
2085          *      Two things going on here:
2086          *      1) Add routes for on-link prefixes
2087          *      2) Configure prefixes with the auto flag set
2088          */
2089
2090         if (pinfo->onlink) {
2091                 struct rt6_info *rt;
2092                 unsigned long rt_expires;
2093
2094                 /* Avoid arithmetic overflow. Really, we could
2095                  * save rt_expires in seconds, likely valid_lft,
2096                  * but it would require division in fib gc, that it
2097                  * not good.
2098                  */
2099                 if (HZ > USER_HZ)
2100                         rt_expires = addrconf_timeout_fixup(valid_lft, HZ);
2101                 else
2102                         rt_expires = addrconf_timeout_fixup(valid_lft, USER_HZ);
2103
2104                 if (addrconf_finite_timeout(rt_expires))
2105                         rt_expires *= HZ;
2106
2107                 rt = addrconf_get_prefix_route(&pinfo->prefix,
2108                                                pinfo->prefix_len,
2109                                                dev,
2110                                                RTF_ADDRCONF | RTF_PREFIX_RT,
2111                                                RTF_GATEWAY | RTF_DEFAULT);
2112
2113                 if (rt) {
2114                         /* Autoconf prefix route */
2115                         if (valid_lft == 0) {
2116                                 ip6_del_rt(rt);
2117                                 rt = NULL;
2118                         } else if (addrconf_finite_timeout(rt_expires)) {
2119                                 /* not infinity */
2120                                 rt6_set_expires(rt, jiffies + rt_expires);
2121                         } else {
2122                                 rt6_clean_expires(rt);
2123                         }
2124                 } else if (valid_lft) {
2125                         clock_t expires = 0;
2126                         int flags = RTF_ADDRCONF | RTF_PREFIX_RT;
2127                         if (addrconf_finite_timeout(rt_expires)) {
2128                                 /* not infinity */
2129                                 flags |= RTF_EXPIRES;
2130                                 expires = jiffies_to_clock_t(rt_expires);
2131                         }
2132                         addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
2133                                               dev, expires, flags);
2134                 }
2135                 ip6_rt_put(rt);
2136         }
2137
2138         /* Try to figure out our local address for this prefix */
2139
2140         if (pinfo->autoconf && in6_dev->cnf.autoconf) {
2141                 struct inet6_ifaddr *ifp;
2142                 struct in6_addr addr;
2143                 int create = 0, update_lft = 0;
2144                 bool tokenized = false;
2145
2146                 if (pinfo->prefix_len == 64) {
2147                         memcpy(&addr, &pinfo->prefix, 8);
2148
2149                         if (!ipv6_addr_any(&in6_dev->token)) {
2150                                 read_lock_bh(&in6_dev->lock);
2151                                 memcpy(addr.s6_addr + 8,
2152                                        in6_dev->token.s6_addr + 8, 8);
2153                                 read_unlock_bh(&in6_dev->lock);
2154                                 tokenized = true;
2155                         } else if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
2156                                    ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
2157                                 in6_dev_put(in6_dev);
2158                                 return;
2159                         }
2160                         goto ok;
2161                 }
2162                 net_dbg_ratelimited("IPv6 addrconf: prefix with wrong length %d\n",
2163                                     pinfo->prefix_len);
2164                 in6_dev_put(in6_dev);
2165                 return;
2166
2167 ok:
2168
2169                 ifp = ipv6_get_ifaddr(net, &addr, dev, 1);
2170
2171                 if (ifp == NULL && valid_lft) {
2172                         int max_addresses = in6_dev->cnf.max_addresses;
2173                         u32 addr_flags = 0;
2174
2175 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
2176                         if (in6_dev->cnf.optimistic_dad &&
2177                             !net->ipv6.devconf_all->forwarding && sllao)
2178                                 addr_flags = IFA_F_OPTIMISTIC;
2179 #endif
2180
2181                         /* Do not allow to create too much of autoconfigured
2182                          * addresses; this would be too easy way to crash kernel.
2183                          */
2184                         if (!max_addresses ||
2185                             ipv6_count_addresses(in6_dev) < max_addresses)
2186                                 ifp = ipv6_add_addr(in6_dev, &addr, NULL,
2187                                                     pinfo->prefix_len,
2188                                                     addr_type&IPV6_ADDR_SCOPE_MASK,
2189                                                     addr_flags, valid_lft,
2190                                                     prefered_lft);
2191
2192                         if (IS_ERR_OR_NULL(ifp)) {
2193                                 in6_dev_put(in6_dev);
2194                                 return;
2195                         }
2196
2197                         update_lft = 0;
2198                         create = 1;
2199                         ifp->cstamp = jiffies;
2200                         ifp->tokenized = tokenized;
2201                         addrconf_dad_start(ifp);
2202                 }
2203
2204                 if (ifp) {
2205                         int flags;
2206                         unsigned long now;
2207 #ifdef CONFIG_IPV6_PRIVACY
2208                         struct inet6_ifaddr *ift;
2209 #endif
2210                         u32 stored_lft;
2211
2212                         /* update lifetime (RFC2462 5.5.3 e) */
2213                         spin_lock(&ifp->lock);
2214                         now = jiffies;
2215                         if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
2216                                 stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
2217                         else
2218                                 stored_lft = 0;
2219                         if (!update_lft && !create && stored_lft) {
2220                                 if (valid_lft > MIN_VALID_LIFETIME ||
2221                                     valid_lft > stored_lft)
2222                                         update_lft = 1;
2223                                 else if (stored_lft <= MIN_VALID_LIFETIME) {
2224                                         /* valid_lft <= stored_lft is always true */
2225                                         /*
2226                                          * RFC 4862 Section 5.5.3e:
2227                                          * "Note that the preferred lifetime of
2228                                          *  the corresponding address is always
2229                                          *  reset to the Preferred Lifetime in
2230                                          *  the received Prefix Information
2231                                          *  option, regardless of whether the
2232                                          *  valid lifetime is also reset or
2233                                          *  ignored."
2234                                          *
2235                                          *  So if the preferred lifetime in
2236                                          *  this advertisement is different
2237                                          *  than what we have stored, but the
2238                                          *  valid lifetime is invalid, just
2239                                          *  reset prefered_lft.
2240                                          *
2241                                          *  We must set the valid lifetime
2242                                          *  to the stored lifetime since we'll
2243                                          *  be updating the timestamp below,
2244                                          *  else we'll set it back to the
2245                                          *  minimum.
2246                                          */
2247                                         if (prefered_lft != ifp->prefered_lft) {
2248                                                 valid_lft = stored_lft;
2249                                                 update_lft = 1;
2250                                         }
2251                                 } else {
2252                                         valid_lft = MIN_VALID_LIFETIME;
2253                                         if (valid_lft < prefered_lft)
2254                                                 prefered_lft = valid_lft;
2255                                         update_lft = 1;
2256                                 }
2257                         }
2258
2259                         if (update_lft) {
2260                                 ifp->valid_lft = valid_lft;
2261                                 ifp->prefered_lft = prefered_lft;
2262                                 ifp->tstamp = now;
2263                                 flags = ifp->flags;
2264                                 ifp->flags &= ~IFA_F_DEPRECATED;
2265                                 spin_unlock(&ifp->lock);
2266
2267                                 if (!(flags&IFA_F_TENTATIVE))
2268                                         ipv6_ifa_notify(0, ifp);
2269                         } else
2270                                 spin_unlock(&ifp->lock);
2271
2272 #ifdef CONFIG_IPV6_PRIVACY
2273                         read_lock_bh(&in6_dev->lock);
2274                         /* update all temporary addresses in the list */
2275                         list_for_each_entry(ift, &in6_dev->tempaddr_list,
2276                                             tmp_list) {
2277                                 int age, max_valid, max_prefered;
2278
2279                                 if (ifp != ift->ifpub)
2280                                         continue;
2281
2282                                 /*
2283                                  * RFC 4941 section 3.3:
2284                                  * If a received option will extend the lifetime
2285                                  * of a public address, the lifetimes of
2286                                  * temporary addresses should be extended,
2287                                  * subject to the overall constraint that no
2288                                  * temporary addresses should ever remain
2289                                  * "valid" or "preferred" for a time longer than
2290                                  * (TEMP_VALID_LIFETIME) or
2291                                  * (TEMP_PREFERRED_LIFETIME - DESYNC_FACTOR),
2292                                  * respectively.
2293                                  */
2294                                 age = (now - ift->cstamp) / HZ;
2295                                 max_valid = in6_dev->cnf.temp_valid_lft - age;
2296                                 if (max_valid < 0)
2297                                         max_valid = 0;
2298
2299                                 max_prefered = in6_dev->cnf.temp_prefered_lft -
2300                                                in6_dev->cnf.max_desync_factor -
2301                                                age;
2302                                 if (max_prefered < 0)
2303                                         max_prefered = 0;
2304
2305                                 if (valid_lft > max_valid)
2306                                         valid_lft = max_valid;
2307
2308                                 if (prefered_lft > max_prefered)
2309                                         prefered_lft = max_prefered;
2310
2311                                 spin_lock(&ift->lock);
2312                                 flags = ift->flags;
2313                                 ift->valid_lft = valid_lft;
2314                                 ift->prefered_lft = prefered_lft;
2315                                 ift->tstamp = now;
2316                                 if (prefered_lft > 0)
2317                                         ift->flags &= ~IFA_F_DEPRECATED;
2318
2319                                 spin_unlock(&ift->lock);
2320                                 if (!(flags&IFA_F_TENTATIVE))
2321                                         ipv6_ifa_notify(0, ift);
2322                         }
2323
2324                         if ((create || list_empty(&in6_dev->tempaddr_list)) && in6_dev->cnf.use_tempaddr > 0) {
2325                                 /*
2326                                  * When a new public address is created as
2327                                  * described in [ADDRCONF], also create a new
2328                                  * temporary address. Also create a temporary
2329                                  * address if it's enabled but no temporary
2330                                  * address currently exists.
2331                                  */
2332                                 read_unlock_bh(&in6_dev->lock);
2333                                 ipv6_create_tempaddr(ifp, NULL);
2334                         } else {
2335                                 read_unlock_bh(&in6_dev->lock);
2336                         }
2337 #endif
2338                         in6_ifa_put(ifp);
2339                         addrconf_verify(0);
2340                 }
2341         }
2342         inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
2343         in6_dev_put(in6_dev);
2344 }
2345
2346 /*
2347  *      Set destination address.
2348  *      Special case for SIT interfaces where we create a new "virtual"
2349  *      device.
2350  */
2351 int addrconf_set_dstaddr(struct net *net, void __user *arg)
2352 {
2353         struct in6_ifreq ireq;
2354         struct net_device *dev;
2355         int err = -EINVAL;
2356
2357         rtnl_lock();
2358
2359         err = -EFAULT;
2360         if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2361                 goto err_exit;
2362
2363         dev = __dev_get_by_index(net, ireq.ifr6_ifindex);
2364
2365         err = -ENODEV;
2366         if (dev == NULL)
2367                 goto err_exit;
2368
2369 #if IS_ENABLED(CONFIG_IPV6_SIT)
2370         if (dev->type == ARPHRD_SIT) {
2371                 const struct net_device_ops *ops = dev->netdev_ops;
2372                 struct ifreq ifr;
2373                 struct ip_tunnel_parm p;
2374
2375                 err = -EADDRNOTAVAIL;
2376                 if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4))
2377                         goto err_exit;
2378
2379                 memset(&p, 0, sizeof(p));
2380                 p.iph.daddr = ireq.ifr6_addr.s6_addr32[3];
2381                 p.iph.saddr = 0;
2382                 p.iph.version = 4;
2383                 p.iph.ihl = 5;
2384                 p.iph.protocol = IPPROTO_IPV6;
2385                 p.iph.ttl = 64;
2386                 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
2387
2388                 if (ops->ndo_do_ioctl) {
2389                         mm_segment_t oldfs = get_fs();
2390
2391                         set_fs(KERNEL_DS);
2392                         err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
2393                         set_fs(oldfs);
2394                 } else
2395                         err = -EOPNOTSUPP;
2396
2397                 if (err == 0) {
2398                         err = -ENOBUFS;
2399                         dev = __dev_get_by_name(net, p.name);
2400                         if (!dev)
2401                                 goto err_exit;
2402                         err = dev_open(dev);
2403                 }
2404         }
2405 #endif
2406
2407 err_exit:
2408         rtnl_unlock();
2409         return err;
2410 }
2411
2412 /*
2413  *      Manual configuration of address on an interface
2414  */
2415 static int inet6_addr_add(struct net *net, int ifindex, const struct in6_addr *pfx,
2416                           const struct in6_addr *peer_pfx,
2417                           unsigned int plen, __u8 ifa_flags, __u32 prefered_lft,
2418                           __u32 valid_lft)
2419 {
2420         struct inet6_ifaddr *ifp;
2421         struct inet6_dev *idev;
2422         struct net_device *dev;
2423         int scope;
2424         u32 flags;
2425         clock_t expires;
2426         unsigned long timeout;
2427
2428         ASSERT_RTNL();
2429
2430         if (plen > 128)
2431                 return -EINVAL;
2432
2433         /* check the lifetime */
2434         if (!valid_lft || prefered_lft > valid_lft)
2435                 return -EINVAL;
2436
2437         dev = __dev_get_by_index(net, ifindex);
2438         if (!dev)
2439                 return -ENODEV;
2440
2441         idev = addrconf_add_dev(dev);
2442         if (IS_ERR(idev))
2443                 return PTR_ERR(idev);
2444
2445         scope = ipv6_addr_scope(pfx);
2446
2447         timeout = addrconf_timeout_fixup(valid_lft, HZ);
2448         if (addrconf_finite_timeout(timeout)) {
2449                 expires = jiffies_to_clock_t(timeout * HZ);
2450                 valid_lft = timeout;
2451                 flags = RTF_EXPIRES;
2452         } else {
2453                 expires = 0;
2454                 flags = 0;
2455                 ifa_flags |= IFA_F_PERMANENT;
2456         }
2457
2458         timeout = addrconf_timeout_fixup(prefered_lft, HZ);
2459         if (addrconf_finite_timeout(timeout)) {
2460                 if (timeout == 0)
2461                         ifa_flags |= IFA_F_DEPRECATED;
2462                 prefered_lft = timeout;
2463         }
2464
2465         ifp = ipv6_add_addr(idev, pfx, peer_pfx, plen, scope, ifa_flags,
2466                             valid_lft, prefered_lft);
2467
2468         if (!IS_ERR(ifp)) {
2469                 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev,
2470                                       expires, flags);
2471                 /*
2472                  * Note that section 3.1 of RFC 4429 indicates
2473                  * that the Optimistic flag should not be set for
2474                  * manually configured addresses
2475                  */
2476                 addrconf_dad_start(ifp);
2477                 in6_ifa_put(ifp);
2478                 addrconf_verify(0);
2479                 return 0;
2480         }
2481
2482         return PTR_ERR(ifp);
2483 }
2484
2485 static int inet6_addr_del(struct net *net, int ifindex, const struct in6_addr *pfx,
2486                           unsigned int plen)
2487 {
2488         struct inet6_ifaddr *ifp;
2489         struct inet6_dev *idev;
2490         struct net_device *dev;
2491
2492         if (plen > 128)
2493                 return -EINVAL;
2494
2495         dev = __dev_get_by_index(net, ifindex);
2496         if (!dev)
2497                 return -ENODEV;
2498
2499         if ((idev = __in6_dev_get(dev)) == NULL)
2500                 return -ENXIO;
2501
2502         read_lock_bh(&idev->lock);
2503         list_for_each_entry(ifp, &idev->addr_list, if_list) {
2504                 if (ifp->prefix_len == plen &&
2505                     ipv6_addr_equal(pfx, &ifp->addr)) {
2506                         in6_ifa_hold(ifp);
2507                         read_unlock_bh(&idev->lock);
2508
2509                         ipv6_del_addr(ifp);
2510                         return 0;
2511                 }
2512         }
2513         read_unlock_bh(&idev->lock);
2514         return -EADDRNOTAVAIL;
2515 }
2516
2517
2518 int addrconf_add_ifaddr(struct net *net, void __user *arg)
2519 {
2520         struct in6_ifreq ireq;
2521         int err;
2522
2523         if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
2524                 return -EPERM;
2525
2526         if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2527                 return -EFAULT;
2528
2529         rtnl_lock();
2530         err = inet6_addr_add(net, ireq.ifr6_ifindex, &ireq.ifr6_addr, NULL,
2531                              ireq.ifr6_prefixlen, IFA_F_PERMANENT,
2532                              INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
2533         rtnl_unlock();
2534         return err;
2535 }
2536
2537 int addrconf_del_ifaddr(struct net *net, void __user *arg)
2538 {
2539         struct in6_ifreq ireq;
2540         int err;
2541
2542         if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
2543                 return -EPERM;
2544
2545         if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2546                 return -EFAULT;
2547
2548         rtnl_lock();
2549         err = inet6_addr_del(net, ireq.ifr6_ifindex, &ireq.ifr6_addr,
2550                              ireq.ifr6_prefixlen);
2551         rtnl_unlock();
2552         return err;
2553 }
2554
2555 static void add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
2556                      int plen, int scope)
2557 {
2558         struct inet6_ifaddr *ifp;
2559
2560         ifp = ipv6_add_addr(idev, addr, NULL, plen,
2561                             scope, IFA_F_PERMANENT, 0, 0);
2562         if (!IS_ERR(ifp)) {
2563                 spin_lock_bh(&ifp->lock);
2564                 ifp->flags &= ~IFA_F_TENTATIVE;
2565                 spin_unlock_bh(&ifp->lock);
2566                 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2567                 in6_ifa_put(ifp);
2568         }
2569 }
2570
2571 #if IS_ENABLED(CONFIG_IPV6_SIT)
2572 static void sit_add_v4_addrs(struct inet6_dev *idev)
2573 {
2574         struct in6_addr addr;
2575         struct net_device *dev;
2576         struct net *net = dev_net(idev->dev);
2577         int scope;
2578
2579         ASSERT_RTNL();
2580
2581         memset(&addr, 0, sizeof(struct in6_addr));
2582         memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
2583
2584         if (idev->dev->flags&IFF_POINTOPOINT) {
2585                 addr.s6_addr32[0] = htonl(0xfe800000);
2586                 scope = IFA_LINK;
2587         } else {
2588                 scope = IPV6_ADDR_COMPATv4;
2589         }
2590
2591         if (addr.s6_addr32[3]) {
2592                 add_addr(idev, &addr, 128, scope);
2593                 return;
2594         }
2595
2596         for_each_netdev(net, dev) {
2597                 struct in_device *in_dev = __in_dev_get_rtnl(dev);
2598                 if (in_dev && (dev->flags & IFF_UP)) {
2599                         struct in_ifaddr *ifa;
2600
2601                         int flag = scope;
2602
2603                         for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
2604                                 int plen;
2605
2606                                 addr.s6_addr32[3] = ifa->ifa_local;
2607
2608                                 if (ifa->ifa_scope == RT_SCOPE_LINK)
2609                                         continue;
2610                                 if (ifa->ifa_scope >= RT_SCOPE_HOST) {
2611                                         if (idev->dev->flags&IFF_POINTOPOINT)
2612                                                 continue;
2613                                         flag |= IFA_HOST;
2614                                 }
2615                                 if (idev->dev->flags&IFF_POINTOPOINT)
2616                                         plen = 64;
2617                                 else
2618                                         plen = 96;
2619
2620                                 add_addr(idev, &addr, plen, flag);
2621                         }
2622                 }
2623         }
2624 }
2625 #endif
2626
2627 static void init_loopback(struct net_device *dev)
2628 {
2629         struct inet6_dev  *idev;
2630         struct net_device *sp_dev;
2631         struct inet6_ifaddr *sp_ifa;
2632         struct rt6_info *sp_rt;
2633
2634         /* ::1 */
2635
2636         ASSERT_RTNL();
2637
2638         if ((idev = ipv6_find_idev(dev)) == NULL) {
2639                 pr_debug("%s: add_dev failed\n", __func__);
2640                 return;
2641         }
2642
2643         add_addr(idev, &in6addr_loopback, 128, IFA_HOST);
2644
2645         /* Add routes to other interface's IPv6 addresses */
2646         for_each_netdev(dev_net(dev), sp_dev) {
2647                 if (!strcmp(sp_dev->name, dev->name))
2648                         continue;
2649
2650                 idev = __in6_dev_get(sp_dev);
2651                 if (!idev)
2652                         continue;
2653
2654                 read_lock_bh(&idev->lock);
2655                 list_for_each_entry(sp_ifa, &idev->addr_list, if_list) {
2656
2657                         if (sp_ifa->flags & (IFA_F_DADFAILED | IFA_F_TENTATIVE))
2658                                 continue;
2659
2660                         if (sp_ifa->rt)
2661                                 continue;
2662
2663                         sp_rt = addrconf_dst_alloc(idev, &sp_ifa->addr, 0);
2664
2665                         /* Failure cases are ignored */
2666                         if (!IS_ERR(sp_rt)) {
2667                                 sp_ifa->rt = sp_rt;
2668                                 ip6_ins_rt(sp_rt);
2669                         }
2670                 }
2671                 read_unlock_bh(&idev->lock);
2672         }
2673 }
2674
2675 static void addrconf_add_linklocal(struct inet6_dev *idev, const struct in6_addr *addr)
2676 {
2677         struct inet6_ifaddr *ifp;
2678         u32 addr_flags = IFA_F_PERMANENT;
2679
2680 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
2681         if (idev->cnf.optimistic_dad &&
2682             !dev_net(idev->dev)->ipv6.devconf_all->forwarding)
2683                 addr_flags |= IFA_F_OPTIMISTIC;
2684 #endif
2685
2686
2687         ifp = ipv6_add_addr(idev, addr, NULL, 64, IFA_LINK, addr_flags, 0, 0);
2688         if (!IS_ERR(ifp)) {
2689                 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, idev->dev, 0, 0);
2690                 addrconf_dad_start(ifp);
2691                 in6_ifa_put(ifp);
2692         }
2693 }
2694
2695 static void addrconf_dev_config(struct net_device *dev)
2696 {
2697         struct in6_addr addr;
2698         struct inet6_dev *idev;
2699
2700         ASSERT_RTNL();
2701
2702         if ((dev->type != ARPHRD_ETHER) &&
2703             (dev->type != ARPHRD_FDDI) &&
2704             (dev->type != ARPHRD_ARCNET) &&
2705             (dev->type != ARPHRD_INFINIBAND) &&
2706             (dev->type != ARPHRD_IEEE802154) &&
2707             (dev->type != ARPHRD_IEEE1394)) {
2708                 /* Alas, we support only Ethernet autoconfiguration. */
2709                 return;
2710         }
2711
2712         idev = addrconf_add_dev(dev);
2713         if (IS_ERR(idev))
2714                 return;
2715
2716         memset(&addr, 0, sizeof(struct in6_addr));
2717         addr.s6_addr32[0] = htonl(0xFE800000);
2718
2719         if (ipv6_generate_eui64(addr.s6_addr + 8, dev) == 0)
2720                 addrconf_add_linklocal(idev, &addr);
2721 }
2722
2723 #if IS_ENABLED(CONFIG_IPV6_SIT)
2724 static void addrconf_sit_config(struct net_device *dev)
2725 {
2726         struct inet6_dev *idev;
2727
2728         ASSERT_RTNL();
2729
2730         /*
2731          * Configure the tunnel with one of our IPv4
2732          * addresses... we should configure all of
2733          * our v4 addrs in the tunnel
2734          */
2735
2736         if ((idev = ipv6_find_idev(dev)) == NULL) {
2737                 pr_debug("%s: add_dev failed\n", __func__);
2738                 return;
2739         }
2740
2741         if (dev->priv_flags & IFF_ISATAP) {
2742                 struct in6_addr addr;
2743
2744                 ipv6_addr_set(&addr,  htonl(0xFE800000), 0, 0, 0);
2745                 addrconf_prefix_route(&addr, 64, dev, 0, 0);
2746                 if (!ipv6_generate_eui64(addr.s6_addr + 8, dev))
2747                         addrconf_add_linklocal(idev, &addr);
2748                 return;
2749         }
2750
2751         sit_add_v4_addrs(idev);
2752
2753         if (dev->flags&IFF_POINTOPOINT)
2754                 addrconf_add_mroute(dev);
2755         else
2756                 sit_route_add(dev);
2757 }
2758 #endif
2759
2760 #if IS_ENABLED(CONFIG_NET_IPGRE)
2761 static void addrconf_gre_config(struct net_device *dev)
2762 {
2763         struct inet6_dev *idev;
2764         struct in6_addr addr;
2765
2766         ASSERT_RTNL();
2767
2768         if ((idev = ipv6_find_idev(dev)) == NULL) {
2769                 pr_debug("%s: add_dev failed\n", __func__);
2770                 return;
2771         }
2772
2773         ipv6_addr_set(&addr,  htonl(0xFE800000), 0, 0, 0);
2774         addrconf_prefix_route(&addr, 64, dev, 0, 0);
2775
2776         if (!ipv6_generate_eui64(addr.s6_addr + 8, dev))
2777                 addrconf_add_linklocal(idev, &addr);
2778 }
2779 #endif
2780
2781 static inline int
2782 ipv6_inherit_linklocal(struct inet6_dev *idev, struct net_device *link_dev)
2783 {
2784         struct in6_addr lladdr;
2785
2786         if (!ipv6_get_lladdr(link_dev, &lladdr, IFA_F_TENTATIVE)) {
2787                 addrconf_add_linklocal(idev, &lladdr);
2788                 return 0;
2789         }
2790         return -1;
2791 }
2792
2793 static void ip6_tnl_add_linklocal(struct inet6_dev *idev)
2794 {
2795         struct net_device *link_dev;
2796         struct net *net = dev_net(idev->dev);
2797
2798         /* first try to inherit the link-local address from the link device */
2799         if (idev->dev->iflink &&
2800             (link_dev = __dev_get_by_index(net, idev->dev->iflink))) {
2801                 if (!ipv6_inherit_linklocal(idev, link_dev))
2802                         return;
2803         }
2804         /* then try to inherit it from any device */
2805         for_each_netdev(net, link_dev) {
2806                 if (!ipv6_inherit_linklocal(idev, link_dev))
2807                         return;
2808         }
2809         pr_debug("init ip6-ip6: add_linklocal failed\n");
2810 }
2811
2812 /*
2813  * Autoconfigure tunnel with a link-local address so routing protocols,
2814  * DHCPv6, MLD etc. can be run over the virtual link
2815  */
2816
2817 static void addrconf_ip6_tnl_config(struct net_device *dev)
2818 {
2819         struct inet6_dev *idev;
2820
2821         ASSERT_RTNL();
2822
2823         idev = addrconf_add_dev(dev);
2824         if (IS_ERR(idev)) {
2825                 pr_debug("init ip6-ip6: add_dev failed\n");
2826                 return;
2827         }
2828         ip6_tnl_add_linklocal(idev);
2829 }
2830
2831 static int addrconf_notify(struct notifier_block *this, unsigned long event,
2832                            void *ptr)
2833 {
2834         struct net_device *dev = netdev_notifier_info_to_dev(ptr);
2835         struct inet6_dev *idev = __in6_dev_get(dev);
2836         int run_pending = 0;
2837         int err;
2838
2839         switch (event) {
2840         case NETDEV_REGISTER:
2841                 if (!idev && dev->mtu >= IPV6_MIN_MTU) {
2842                         idev = ipv6_add_dev(dev);
2843                         if (!idev)
2844                                 return notifier_from_errno(-ENOMEM);
2845                 }
2846                 break;
2847
2848         case NETDEV_UP:
2849         case NETDEV_CHANGE:
2850                 if (dev->flags & IFF_SLAVE)
2851                         break;
2852
2853                 if (event == NETDEV_UP) {
2854                         if (!addrconf_qdisc_ok(dev)) {
2855                                 /* device is not ready yet. */
2856                                 pr_info("ADDRCONF(NETDEV_UP): %s: link is not ready\n",
2857                                         dev->name);
2858                                 break;
2859                         }
2860
2861                         if (!idev && dev->mtu >= IPV6_MIN_MTU)
2862                                 idev = ipv6_add_dev(dev);
2863
2864                         if (idev) {
2865                                 idev->if_flags |= IF_READY;
2866                                 run_pending = 1;
2867                         }
2868                 } else {
2869                         if (!addrconf_qdisc_ok(dev)) {
2870                                 /* device is still not ready. */
2871                                 break;
2872                         }
2873
2874                         if (idev) {
2875                                 if (idev->if_flags & IF_READY)
2876                                         /* device is already configured. */
2877                                         break;
2878                                 idev->if_flags |= IF_READY;
2879                         }
2880
2881                         pr_info("ADDRCONF(NETDEV_CHANGE): %s: link becomes ready\n",
2882                                 dev->name);
2883
2884                         run_pending = 1;
2885                 }
2886
2887                 switch (dev->type) {
2888 #if IS_ENABLED(CONFIG_IPV6_SIT)
2889                 case ARPHRD_SIT:
2890                         addrconf_sit_config(dev);
2891                         break;
2892 #endif
2893 #if IS_ENABLED(CONFIG_NET_IPGRE)
2894                 case ARPHRD_IPGRE:
2895                         addrconf_gre_config(dev);
2896                         break;
2897 #endif
2898                 case ARPHRD_TUNNEL6:
2899                         addrconf_ip6_tnl_config(dev);
2900                         break;
2901                 case ARPHRD_LOOPBACK:
2902                         init_loopback(dev);
2903                         break;
2904
2905                 default:
2906                         addrconf_dev_config(dev);
2907                         break;
2908                 }
2909
2910                 if (idev) {
2911                         if (run_pending)
2912                                 addrconf_dad_run(idev);
2913
2914                         /*
2915                          * If the MTU changed during the interface down,
2916                          * when the interface up, the changed MTU must be
2917                          * reflected in the idev as well as routers.
2918                          */
2919                         if (idev->cnf.mtu6 != dev->mtu &&
2920                             dev->mtu >= IPV6_MIN_MTU) {
2921                                 rt6_mtu_change(dev, dev->mtu);
2922                                 idev->cnf.mtu6 = dev->mtu;
2923                         }
2924                         idev->tstamp = jiffies;
2925                         inet6_ifinfo_notify(RTM_NEWLINK, idev);
2926
2927                         /*
2928                          * If the changed mtu during down is lower than
2929                          * IPV6_MIN_MTU stop IPv6 on this interface.
2930                          */
2931                         if (dev->mtu < IPV6_MIN_MTU)
2932                                 addrconf_ifdown(dev, 1);
2933                 }
2934                 break;
2935
2936         case NETDEV_CHANGEMTU:
2937                 if (idev && dev->mtu >= IPV6_MIN_MTU) {
2938                         rt6_mtu_change(dev, dev->mtu);
2939                         idev->cnf.mtu6 = dev->mtu;
2940                         break;
2941                 }
2942
2943                 if (!idev && dev->mtu >= IPV6_MIN_MTU) {
2944                         idev = ipv6_add_dev(dev);
2945                         if (idev)
2946                                 break;
2947                 }
2948
2949                 /*
2950                  * MTU falled under IPV6_MIN_MTU.
2951                  * Stop IPv6 on this interface.
2952                  */
2953
2954         case NETDEV_DOWN:
2955         case NETDEV_UNREGISTER:
2956                 /*
2957                  *      Remove all addresses from this interface.
2958                  */
2959                 addrconf_ifdown(dev, event != NETDEV_DOWN);
2960                 break;
2961
2962         case NETDEV_CHANGENAME:
2963                 if (idev) {
2964                         snmp6_unregister_dev(idev);
2965                         addrconf_sysctl_unregister(idev);
2966                         addrconf_sysctl_register(idev);
2967                         err = snmp6_register_dev(idev);
2968                         if (err)
2969                                 return notifier_from_errno(err);
2970                 }
2971                 break;
2972
2973         case NETDEV_PRE_TYPE_CHANGE:
2974         case NETDEV_POST_TYPE_CHANGE:
2975                 addrconf_type_change(dev, event);
2976                 break;
2977         }
2978
2979         return NOTIFY_OK;
2980 }
2981
2982 /*
2983  *      addrconf module should be notified of a device going up
2984  */
2985 static struct notifier_block ipv6_dev_notf = {
2986         .notifier_call = addrconf_notify,
2987 };
2988
2989 static void addrconf_type_change(struct net_device *dev, unsigned long event)
2990 {
2991         struct inet6_dev *idev;
2992         ASSERT_RTNL();
2993
2994         idev = __in6_dev_get(dev);
2995
2996         if (event == NETDEV_POST_TYPE_CHANGE)
2997                 ipv6_mc_remap(idev);
2998         else if (event == NETDEV_PRE_TYPE_CHANGE)
2999                 ipv6_mc_unmap(idev);
3000 }
3001
3002 static int addrconf_ifdown(struct net_device *dev, int how)
3003 {
3004         struct net *net = dev_net(dev);
3005         struct inet6_dev *idev;
3006         struct inet6_ifaddr *ifa;
3007         int state, i;
3008
3009         ASSERT_RTNL();
3010
3011         rt6_ifdown(net, dev);
3012         neigh_ifdown(&nd_tbl, dev);
3013
3014         idev = __in6_dev_get(dev);
3015         if (idev == NULL)
3016                 return -ENODEV;
3017
3018         /*
3019          * Step 1: remove reference to ipv6 device from parent device.
3020          *         Do not dev_put!
3021          */
3022         if (how) {
3023                 idev->dead = 1;
3024
3025                 /* protected by rtnl_lock */
3026                 RCU_INIT_POINTER(dev->ip6_ptr, NULL);
3027
3028                 /* Step 1.5: remove snmp6 entry */
3029                 snmp6_unregister_dev(idev);
3030
3031         }
3032
3033         /* Step 2: clear hash table */
3034         for (i = 0; i < IN6_ADDR_HSIZE; i++) {
3035                 struct hlist_head *h = &inet6_addr_lst[i];
3036
3037                 spin_lock_bh(&addrconf_hash_lock);
3038         restart:
3039                 hlist_for_each_entry_rcu(ifa, h, addr_lst) {
3040                         if (ifa->idev == idev) {
3041                                 hlist_del_init_rcu(&ifa->addr_lst);
3042                                 addrconf_del_dad_timer(ifa);
3043                                 goto restart;
3044                         }
3045                 }
3046                 spin_unlock_bh(&addrconf_hash_lock);
3047         }
3048
3049         write_lock_bh(&idev->lock);
3050
3051         addrconf_del_rs_timer(idev);
3052
3053         /* Step 2: clear flags for stateless addrconf */
3054         if (!how)
3055                 idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
3056
3057 #ifdef CONFIG_IPV6_PRIVACY
3058         if (how && del_timer(&idev->regen_timer))
3059                 in6_dev_put(idev);
3060
3061         /* Step 3: clear tempaddr list */
3062         while (!list_empty(&idev->tempaddr_list)) {
3063                 ifa = list_first_entry(&idev->tempaddr_list,
3064                                        struct inet6_ifaddr, tmp_list);
3065                 list_del(&ifa->tmp_list);
3066                 write_unlock_bh(&idev->lock);
3067                 spin_lock_bh(&ifa->lock);
3068
3069                 if (ifa->ifpub) {
3070                         in6_ifa_put(ifa->ifpub);
3071                         ifa->ifpub = NULL;
3072                 }
3073                 spin_unlock_bh(&ifa->lock);
3074                 in6_ifa_put(ifa);
3075                 write_lock_bh(&idev->lock);
3076         }
3077 #endif
3078
3079         while (!list_empty(&idev->addr_list)) {
3080                 ifa = list_first_entry(&idev->addr_list,
3081                                        struct inet6_ifaddr, if_list);
3082                 addrconf_del_dad_timer(ifa);
3083
3084                 list_del(&ifa->if_list);
3085
3086                 write_unlock_bh(&idev->lock);
3087
3088                 spin_lock_bh(&ifa->state_lock);
3089                 state = ifa->state;
3090                 ifa->state = INET6_IFADDR_STATE_DEAD;
3091                 spin_unlock_bh(&ifa->state_lock);
3092
3093                 if (state != INET6_IFADDR_STATE_DEAD) {
3094                         __ipv6_ifa_notify(RTM_DELADDR, ifa);
3095                         inet6addr_notifier_call_chain(NETDEV_DOWN, ifa);
3096                 }
3097                 in6_ifa_put(ifa);
3098
3099                 write_lock_bh(&idev->lock);
3100         }
3101
3102         write_unlock_bh(&idev->lock);
3103
3104         /* Step 5: Discard multicast list */
3105         if (how)
3106                 ipv6_mc_destroy_dev(idev);
3107         else
3108                 ipv6_mc_down(idev);
3109
3110         idev->tstamp = jiffies;
3111
3112         /* Last: Shot the device (if unregistered) */
3113         if (how) {
3114                 addrconf_sysctl_unregister(idev);
3115                 neigh_parms_release(&nd_tbl, idev->nd_parms);
3116                 neigh_ifdown(&nd_tbl, dev);
3117                 in6_dev_put(idev);
3118         }
3119         return 0;
3120 }
3121
3122 static void addrconf_rs_timer(unsigned long data)
3123 {
3124         struct inet6_dev *idev = (struct inet6_dev *)data;
3125         struct in6_addr lladdr;
3126
3127         write_lock(&idev->lock);
3128         if (idev->dead || !(idev->if_flags & IF_READY))
3129                 goto out;
3130
3131         if (!ipv6_accept_ra(idev))
3132                 goto out;
3133
3134         /* Announcement received after solicitation was sent */
3135         if (idev->if_flags & IF_RA_RCVD)
3136                 goto out;
3137
3138         if (idev->rs_probes++ < idev->cnf.rtr_solicits) {
3139                 if (!__ipv6_get_lladdr(idev, &lladdr, IFA_F_TENTATIVE))
3140                         ndisc_send_rs(idev->dev, &lladdr,
3141                                       &in6addr_linklocal_allrouters);
3142                 else
3143                         goto out;
3144
3145                 /* The wait after the last probe can be shorter */
3146                 addrconf_mod_rs_timer(idev, (idev->rs_probes ==
3147                                              idev->cnf.rtr_solicits) ?
3148                                       idev->cnf.rtr_solicit_delay :
3149                                       idev->cnf.rtr_solicit_interval);
3150         } else {
3151                 /*
3152                  * Note: we do not support deprecated "all on-link"
3153                  * assumption any longer.
3154                  */
3155                 pr_debug("%s: no IPv6 routers present\n", idev->dev->name);
3156         }
3157
3158 out:
3159         write_unlock(&idev->lock);
3160         in6_dev_put(idev);
3161 }
3162
3163 /*
3164  *      Duplicate Address Detection
3165  */
3166 static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
3167 {
3168         unsigned long rand_num;
3169         struct inet6_dev *idev = ifp->idev;
3170
3171         if (ifp->flags & IFA_F_OPTIMISTIC)
3172                 rand_num = 0;
3173         else
3174                 rand_num = net_random() % (idev->cnf.rtr_solicit_delay ? : 1);
3175
3176         ifp->dad_probes = idev->cnf.dad_transmits;
3177         addrconf_mod_dad_timer(ifp, rand_num);
3178 }
3179
3180 static void addrconf_dad_start(struct inet6_ifaddr *ifp)
3181 {
3182         struct inet6_dev *idev = ifp->idev;
3183         struct net_device *dev = idev->dev;
3184
3185         addrconf_join_solict(dev, &ifp->addr);
3186
3187         net_srandom(ifp->addr.s6_addr32[3]);
3188
3189         read_lock_bh(&idev->lock);
3190         spin_lock(&ifp->lock);
3191         if (ifp->state == INET6_IFADDR_STATE_DEAD)
3192                 goto out;
3193
3194         if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
3195             idev->cnf.accept_dad < 1 ||
3196             !(ifp->flags&IFA_F_TENTATIVE) ||
3197             ifp->flags & IFA_F_NODAD) {
3198                 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
3199                 spin_unlock(&ifp->lock);
3200                 read_unlock_bh(&idev->lock);
3201
3202                 addrconf_dad_completed(ifp);
3203                 return;
3204         }
3205
3206         if (!(idev->if_flags & IF_READY)) {
3207                 spin_unlock(&ifp->lock);
3208                 read_unlock_bh(&idev->lock);
3209                 /*
3210                  * If the device is not ready:
3211                  * - keep it tentative if it is a permanent address.
3212                  * - otherwise, kill it.
3213                  */
3214                 in6_ifa_hold(ifp);
3215                 addrconf_dad_stop(ifp, 0);
3216                 return;
3217         }
3218
3219         /*
3220          * Optimistic nodes can start receiving
3221          * Frames right away
3222          */
3223         if (ifp->flags & IFA_F_OPTIMISTIC)
3224                 ip6_ins_rt(ifp->rt);
3225
3226         addrconf_dad_kick(ifp);
3227 out:
3228         spin_unlock(&ifp->lock);
3229         read_unlock_bh(&idev->lock);
3230 }
3231
3232 static void addrconf_dad_timer(unsigned long data)
3233 {
3234         struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
3235         struct inet6_dev *idev = ifp->idev;
3236         struct in6_addr mcaddr;
3237
3238         if (!ifp->dad_probes && addrconf_dad_end(ifp))
3239                 goto out;
3240
3241         write_lock(&idev->lock);
3242         if (idev->dead || !(idev->if_flags & IF_READY)) {
3243                 write_unlock(&idev->lock);
3244                 goto out;
3245         }
3246
3247         spin_lock(&ifp->lock);
3248         if (ifp->state == INET6_IFADDR_STATE_DEAD) {
3249                 spin_unlock(&ifp->lock);
3250                 write_unlock(&idev->lock);
3251                 goto out;
3252         }
3253
3254         if (ifp->dad_probes == 0) {
3255                 /*
3256                  * DAD was successful
3257                  */
3258
3259                 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
3260                 spin_unlock(&ifp->lock);
3261                 write_unlock(&idev->lock);
3262
3263                 addrconf_dad_completed(ifp);
3264
3265                 goto out;
3266         }
3267
3268         ifp->dad_probes--;
3269         addrconf_mod_dad_timer(ifp, ifp->idev->nd_parms->retrans_time);
3270         spin_unlock(&ifp->lock);
3271         write_unlock(&idev->lock);
3272
3273         /* send a neighbour solicitation for our addr */
3274         addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
3275         ndisc_send_ns(ifp->idev->dev, NULL, &ifp->addr, &mcaddr, &in6addr_any);
3276 out:
3277         in6_ifa_put(ifp);
3278 }
3279
3280 static void addrconf_dad_completed(struct inet6_ifaddr *ifp)
3281 {
3282         struct net_device *dev = ifp->idev->dev;
3283         struct in6_addr lladdr;
3284         bool send_rs, send_mld;
3285
3286         addrconf_del_dad_timer(ifp);
3287
3288         /*
3289          *      Configure the address for reception. Now it is valid.
3290          */
3291
3292         ipv6_ifa_notify(RTM_NEWADDR, ifp);
3293
3294         /* If added prefix is link local and we are prepared to process
3295            router advertisements, start sending router solicitations.
3296          */
3297
3298         read_lock_bh(&ifp->idev->lock);
3299         spin_lock(&ifp->lock);
3300         send_mld = ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL &&
3301                    ifp->idev->valid_ll_addr_cnt == 1;
3302         send_rs = send_mld &&
3303                   ipv6_accept_ra(ifp->idev) &&
3304                   ifp->idev->cnf.rtr_solicits > 0 &&
3305                   (dev->flags&IFF_LOOPBACK) == 0;
3306         spin_unlock(&ifp->lock);
3307         read_unlock_bh(&ifp->idev->lock);
3308
3309         /* While dad is in progress mld report's source address is in6_addrany.
3310          * Resend with proper ll now.
3311          */
3312         if (send_mld)
3313                 ipv6_mc_dad_complete(ifp->idev);
3314
3315         if (send_rs) {
3316                 /*
3317                  *      If a host as already performed a random delay
3318                  *      [...] as part of DAD [...] there is no need
3319                  *      to delay again before sending the first RS
3320                  */
3321                 if (ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
3322                         return;
3323                 ndisc_send_rs(dev, &lladdr, &in6addr_linklocal_allrouters);
3324
3325                 write_lock_bh(&ifp->idev->lock);
3326                 spin_lock(&ifp->lock);
3327                 ifp->idev->rs_probes = 1;
3328                 ifp->idev->if_flags |= IF_RS_SENT;
3329                 addrconf_mod_rs_timer(ifp->idev,
3330                                       ifp->idev->cnf.rtr_solicit_interval);
3331                 spin_unlock(&ifp->lock);
3332                 write_unlock_bh(&ifp->idev->lock);
3333         }
3334 }
3335
3336 static void addrconf_dad_run(struct inet6_dev *idev)
3337 {
3338         struct inet6_ifaddr *ifp;
3339
3340         read_lock_bh(&idev->lock);
3341         list_for_each_entry(ifp, &idev->addr_list, if_list) {
3342                 spin_lock(&ifp->lock);
3343                 if (ifp->flags & IFA_F_TENTATIVE &&
3344                     ifp->state == INET6_IFADDR_STATE_DAD)
3345                         addrconf_dad_kick(ifp);
3346                 spin_unlock(&ifp->lock);
3347         }
3348         read_unlock_bh(&idev->lock);
3349 }
3350
3351 #ifdef CONFIG_PROC_FS
3352 struct if6_iter_state {
3353         struct seq_net_private p;
3354         int bucket;
3355         int offset;
3356 };
3357
3358 static struct inet6_ifaddr *if6_get_first(struct seq_file *seq, loff_t pos)
3359 {
3360         struct inet6_ifaddr *ifa = NULL;
3361         struct if6_iter_state *state = seq->private;
3362         struct net *net = seq_file_net(seq);
3363         int p = 0;
3364
3365         /* initial bucket if pos is 0 */
3366         if (pos == 0) {
3367                 state->bucket = 0;
3368                 state->offset = 0;
3369         }
3370
3371         for (; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
3372                 hlist_for_each_entry_rcu_bh(ifa, &inet6_addr_lst[state->bucket],
3373                                          addr_lst) {
3374                         if (!net_eq(dev_net(ifa->idev->dev), net))
3375                                 continue;
3376                         /* sync with offset */
3377                         if (p < state->offset) {
3378                                 p++;
3379                                 continue;
3380                         }
3381                         state->offset++;
3382                         return ifa;
3383                 }
3384
3385                 /* prepare for next bucket */
3386                 state->offset = 0;
3387                 p = 0;
3388         }
3389         return NULL;
3390 }
3391
3392 static struct inet6_ifaddr *if6_get_next(struct seq_file *seq,
3393                                          struct inet6_ifaddr *ifa)
3394 {
3395         struct if6_iter_state *state = seq->private;
3396         struct net *net = seq_file_net(seq);
3397
3398         hlist_for_each_entry_continue_rcu_bh(ifa, addr_lst) {
3399                 if (!net_eq(dev_net(ifa->idev->dev), net))
3400                         continue;
3401                 state->offset++;
3402                 return ifa;
3403         }
3404
3405         while (++state->bucket < IN6_ADDR_HSIZE) {
3406                 state->offset = 0;
3407                 hlist_for_each_entry_rcu_bh(ifa,
3408                                      &inet6_addr_lst[state->bucket], addr_lst) {
3409                         if (!net_eq(dev_net(ifa->idev->dev), net))
3410                                 continue;
3411                         state->offset++;
3412                         return ifa;
3413                 }
3414         }
3415
3416         return NULL;
3417 }
3418
3419 static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
3420         __acquires(rcu_bh)
3421 {
3422         rcu_read_lock_bh();
3423         return if6_get_first(seq, *pos);
3424 }
3425
3426 static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3427 {
3428         struct inet6_ifaddr *ifa;
3429
3430         ifa = if6_get_next(seq, v);
3431         ++*pos;
3432         return ifa;
3433 }
3434
3435 static void if6_seq_stop(struct seq_file *seq, void *v)
3436         __releases(rcu_bh)
3437 {
3438         rcu_read_unlock_bh();
3439 }
3440
3441 static int if6_seq_show(struct seq_file *seq, void *v)
3442 {
3443         struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
3444         seq_printf(seq, "%pi6 %02x %02x %02x %02x %8s\n",
3445                    &ifp->addr,
3446                    ifp->idev->dev->ifindex,
3447                    ifp->prefix_len,
3448                    ifp->scope,
3449                    ifp->flags,
3450                    ifp->idev->dev->name);
3451         return 0;
3452 }
3453
3454 static const struct seq_operations if6_seq_ops = {
3455         .start  = if6_seq_start,
3456         .next   = if6_seq_next,
3457         .show   = if6_seq_show,
3458         .stop   = if6_seq_stop,
3459 };
3460
3461 static int if6_seq_open(struct inode *inode, struct file *file)
3462 {
3463         return seq_open_net(inode, file, &if6_seq_ops,
3464                             sizeof(struct if6_iter_state));
3465 }
3466
3467 static const struct file_operations if6_fops = {
3468         .owner          = THIS_MODULE,
3469         .open           = if6_seq_open,
3470         .read           = seq_read,
3471         .llseek         = seq_lseek,
3472         .release        = seq_release_net,
3473 };
3474
3475 static int __net_init if6_proc_net_init(struct net *net)
3476 {
3477         if (!proc_create("if_inet6", S_IRUGO, net->proc_net, &if6_fops))
3478                 return -ENOMEM;
3479         return 0;
3480 }
3481
3482 static void __net_exit if6_proc_net_exit(struct net *net)
3483 {
3484         remove_proc_entry("if_inet6", net->proc_net);
3485 }
3486
3487 static struct pernet_operations if6_proc_net_ops = {
3488        .init = if6_proc_net_init,
3489        .exit = if6_proc_net_exit,
3490 };
3491
3492 int __init if6_proc_init(void)
3493 {
3494         return register_pernet_subsys(&if6_proc_net_ops);
3495 }
3496
3497 void if6_proc_exit(void)
3498 {
3499         unregister_pernet_subsys(&if6_proc_net_ops);
3500 }
3501 #endif  /* CONFIG_PROC_FS */
3502
3503 #if IS_ENABLED(CONFIG_IPV6_MIP6)
3504 /* Check if address is a home address configured on any interface. */
3505 int ipv6_chk_home_addr(struct net *net, const struct in6_addr *addr)
3506 {
3507         int ret = 0;
3508         struct inet6_ifaddr *ifp = NULL;
3509         unsigned int hash = inet6_addr_hash(addr);
3510
3511         rcu_read_lock_bh();
3512         hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) {
3513                 if (!net_eq(dev_net(ifp->idev->dev), net))
3514                         continue;
3515                 if (ipv6_addr_equal(&ifp->addr, addr) &&
3516                     (ifp->flags & IFA_F_HOMEADDRESS)) {
3517                         ret = 1;
3518                         break;
3519                 }
3520         }
3521         rcu_read_unlock_bh();
3522         return ret;
3523 }
3524 #endif
3525
3526 /*
3527  *      Periodic address status verification
3528  */
3529
3530 static void addrconf_verify(unsigned long foo)
3531 {
3532         unsigned long now, next, next_sec, next_sched;
3533         struct inet6_ifaddr *ifp;
3534         int i;
3535
3536         rcu_read_lock_bh();
3537         spin_lock(&addrconf_verify_lock);
3538         now = jiffies;
3539         next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY);
3540
3541         del_timer(&addr_chk_timer);
3542
3543         for (i = 0; i < IN6_ADDR_HSIZE; i++) {
3544 restart:
3545                 hlist_for_each_entry_rcu_bh(ifp,
3546                                          &inet6_addr_lst[i], addr_lst) {
3547                         unsigned long age;
3548
3549                         if (ifp->flags & IFA_F_PERMANENT)
3550                                 continue;
3551
3552                         spin_lock(&ifp->lock);
3553                         /* We try to batch several events at once. */
3554                         age = (now - ifp->tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
3555
3556                         if (ifp->valid_lft != INFINITY_LIFE_TIME &&
3557                             age >= ifp->valid_lft) {
3558                                 spin_unlock(&ifp->lock);
3559                                 in6_ifa_hold(ifp);
3560                                 ipv6_del_addr(ifp);
3561                                 goto restart;
3562                         } else if (ifp->prefered_lft == INFINITY_LIFE_TIME) {
3563                                 spin_unlock(&ifp->lock);
3564                                 continue;
3565                         } else if (age >= ifp->prefered_lft) {
3566                                 /* jiffies - ifp->tstamp > age >= ifp->prefered_lft */
3567                                 int deprecate = 0;
3568
3569                                 if (!(ifp->flags&IFA_F_DEPRECATED)) {
3570                                         deprecate = 1;
3571                                         ifp->flags |= IFA_F_DEPRECATED;
3572                                 }
3573
3574                                 if (time_before(ifp->tstamp + ifp->valid_lft * HZ, next))
3575                                         next = ifp->tstamp + ifp->valid_lft * HZ;
3576
3577                                 spin_unlock(&ifp->lock);
3578
3579                                 if (deprecate) {
3580                                         in6_ifa_hold(ifp);
3581
3582                                         ipv6_ifa_notify(0, ifp);
3583                                         in6_ifa_put(ifp);
3584                                         goto restart;
3585                                 }
3586 #ifdef CONFIG_IPV6_PRIVACY
3587                         } else if ((ifp->flags&IFA_F_TEMPORARY) &&
3588                                    !(ifp->flags&IFA_F_TENTATIVE)) {
3589                                 unsigned long regen_advance = ifp->idev->cnf.regen_max_retry *
3590                                         ifp->idev->cnf.dad_transmits *
3591                                         ifp->idev->nd_parms->retrans_time / HZ;
3592
3593                                 if (age >= ifp->prefered_lft - regen_advance) {
3594                                         struct inet6_ifaddr *ifpub = ifp->ifpub;
3595                                         if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
3596                                                 next = ifp->tstamp + ifp->prefered_lft * HZ;
3597                                         if (!ifp->regen_count && ifpub) {
3598                                                 ifp->regen_count++;
3599                                                 in6_ifa_hold(ifp);
3600                                                 in6_ifa_hold(ifpub);
3601                                                 spin_unlock(&ifp->lock);
3602
3603                                                 spin_lock(&ifpub->lock);
3604                                                 ifpub->regen_count = 0;
3605                                                 spin_unlock(&ifpub->lock);
3606                                                 ipv6_create_tempaddr(ifpub, ifp);
3607                                                 in6_ifa_put(ifpub);
3608                                                 in6_ifa_put(ifp);
3609                                                 goto restart;
3610                                         }
3611                                 } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
3612                                         next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
3613                                 spin_unlock(&ifp->lock);
3614 #endif
3615                         } else {
3616                                 /* ifp->prefered_lft <= ifp->valid_lft */
3617                                 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
3618                                         next = ifp->tstamp + ifp->prefered_lft * HZ;
3619                                 spin_unlock(&ifp->lock);
3620                         }
3621                 }
3622         }
3623
3624         next_sec = round_jiffies_up(next);
3625         next_sched = next;
3626
3627         /* If rounded timeout is accurate enough, accept it. */
3628         if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ))
3629                 next_sched = next_sec;
3630
3631         /* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */
3632         if (time_before(next_sched, jiffies + ADDRCONF_TIMER_FUZZ_MAX))
3633                 next_sched = jiffies + ADDRCONF_TIMER_FUZZ_MAX;
3634
3635         ADBG((KERN_DEBUG "now = %lu, schedule = %lu, rounded schedule = %lu => %lu\n",
3636               now, next, next_sec, next_sched));
3637
3638         addr_chk_timer.expires = next_sched;
3639         add_timer(&addr_chk_timer);
3640         spin_unlock(&addrconf_verify_lock);
3641         rcu_read_unlock_bh();
3642 }
3643
3644 static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local,
3645                                      struct in6_addr **peer_pfx)
3646 {
3647         struct in6_addr *pfx = NULL;
3648
3649         *peer_pfx = NULL;
3650
3651         if (addr)
3652                 pfx = nla_data(addr);
3653
3654         if (local) {
3655                 if (pfx && nla_memcmp(local, pfx, sizeof(*pfx)))
3656                         *peer_pfx = pfx;
3657                 pfx = nla_data(local);
3658         }
3659
3660         return pfx;
3661 }
3662
3663 static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = {
3664         [IFA_ADDRESS]           = { .len = sizeof(struct in6_addr) },
3665         [IFA_LOCAL]             = { .len = sizeof(struct in6_addr) },
3666         [IFA_CACHEINFO]         = { .len = sizeof(struct ifa_cacheinfo) },
3667 };
3668
3669 static int
3670 inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh)
3671 {
3672         struct net *net = sock_net(skb->sk);
3673         struct ifaddrmsg *ifm;
3674         struct nlattr *tb[IFA_MAX+1];
3675         struct in6_addr *pfx, *peer_pfx;
3676         int err;
3677
3678         err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
3679         if (err < 0)
3680                 return err;
3681
3682         ifm = nlmsg_data(nlh);
3683         pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
3684         if (pfx == NULL)
3685                 return -EINVAL;
3686
3687         return inet6_addr_del(net, ifm->ifa_index, pfx, ifm->ifa_prefixlen);
3688 }
3689
3690 static int inet6_addr_modify(struct inet6_ifaddr *ifp, u8 ifa_flags,
3691                              u32 prefered_lft, u32 valid_lft)
3692 {
3693         u32 flags;
3694         clock_t expires;
3695         unsigned long timeout;
3696
3697         if (!valid_lft || (prefered_lft > valid_lft))
3698                 return -EINVAL;
3699
3700         timeout = addrconf_timeout_fixup(valid_lft, HZ);
3701         if (addrconf_finite_timeout(timeout)) {
3702                 expires = jiffies_to_clock_t(timeout * HZ);
3703                 valid_lft = timeout;
3704                 flags = RTF_EXPIRES;
3705         } else {
3706                 expires = 0;
3707                 flags = 0;
3708                 ifa_flags |= IFA_F_PERMANENT;
3709         }
3710
3711         timeout = addrconf_timeout_fixup(prefered_lft, HZ);
3712         if (addrconf_finite_timeout(timeout)) {
3713                 if (timeout == 0)
3714                         ifa_flags |= IFA_F_DEPRECATED;
3715                 prefered_lft = timeout;
3716         }
3717
3718         spin_lock_bh(&ifp->lock);
3719         ifp->flags = (ifp->flags & ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD | IFA_F_HOMEADDRESS)) | ifa_flags;
3720         ifp->tstamp = jiffies;
3721         ifp->valid_lft = valid_lft;
3722         ifp->prefered_lft = prefered_lft;
3723
3724         spin_unlock_bh(&ifp->lock);
3725         if (!(ifp->flags&IFA_F_TENTATIVE))
3726                 ipv6_ifa_notify(0, ifp);
3727
3728         addrconf_prefix_route(&ifp->addr, ifp->prefix_len, ifp->idev->dev,
3729                               expires, flags);
3730         addrconf_verify(0);
3731
3732         return 0;
3733 }
3734
3735 static int
3736 inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh)
3737 {
3738         struct net *net = sock_net(skb->sk);
3739         struct ifaddrmsg *ifm;
3740         struct nlattr *tb[IFA_MAX+1];
3741         struct in6_addr *pfx, *peer_pfx;
3742         struct inet6_ifaddr *ifa;
3743         struct net_device *dev;
3744         u32 valid_lft = INFINITY_LIFE_TIME, preferred_lft = INFINITY_LIFE_TIME;
3745         u8 ifa_flags;
3746         int err;
3747
3748         err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
3749         if (err < 0)
3750                 return err;
3751
3752         ifm = nlmsg_data(nlh);
3753         pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
3754         if (pfx == NULL)
3755                 return -EINVAL;
3756
3757         if (tb[IFA_CACHEINFO]) {
3758                 struct ifa_cacheinfo *ci;
3759
3760                 ci = nla_data(tb[IFA_CACHEINFO]);
3761                 valid_lft = ci->ifa_valid;
3762                 preferred_lft = ci->ifa_prefered;
3763         } else {
3764                 preferred_lft = INFINITY_LIFE_TIME;
3765                 valid_lft = INFINITY_LIFE_TIME;
3766         }
3767
3768         dev =  __dev_get_by_index(net, ifm->ifa_index);
3769         if (dev == NULL)
3770                 return -ENODEV;
3771
3772         /* We ignore other flags so far. */
3773         ifa_flags = ifm->ifa_flags & (IFA_F_NODAD | IFA_F_HOMEADDRESS);
3774
3775         ifa = ipv6_get_ifaddr(net, pfx, dev, 1);
3776         if (ifa == NULL) {
3777                 /*
3778                  * It would be best to check for !NLM_F_CREATE here but
3779                  * userspace alreay relies on not having to provide this.
3780                  */
3781                 return inet6_addr_add(net, ifm->ifa_index, pfx, peer_pfx,
3782                                       ifm->ifa_prefixlen, ifa_flags,
3783                                       preferred_lft, valid_lft);
3784         }
3785
3786         if (nlh->nlmsg_flags & NLM_F_EXCL ||
3787             !(nlh->nlmsg_flags & NLM_F_REPLACE))
3788                 err = -EEXIST;
3789         else
3790                 err = inet6_addr_modify(ifa, ifa_flags, preferred_lft, valid_lft);
3791
3792         in6_ifa_put(ifa);
3793
3794         return err;
3795 }
3796
3797 static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u8 flags,
3798                           u8 scope, int ifindex)
3799 {
3800         struct ifaddrmsg *ifm;
3801
3802         ifm = nlmsg_data(nlh);
3803         ifm->ifa_family = AF_INET6;
3804         ifm->ifa_prefixlen = prefixlen;
3805         ifm->ifa_flags = flags;
3806         ifm->ifa_scope = scope;
3807         ifm->ifa_index = ifindex;
3808 }
3809
3810 static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
3811                          unsigned long tstamp, u32 preferred, u32 valid)
3812 {
3813         struct ifa_cacheinfo ci;
3814
3815         ci.cstamp = cstamp_delta(cstamp);
3816         ci.tstamp = cstamp_delta(tstamp);
3817         ci.ifa_prefered = preferred;
3818         ci.ifa_valid = valid;
3819
3820         return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
3821 }
3822
3823 static inline int rt_scope(int ifa_scope)
3824 {
3825         if (ifa_scope & IFA_HOST)
3826                 return RT_SCOPE_HOST;
3827         else if (ifa_scope & IFA_LINK)
3828                 return RT_SCOPE_LINK;
3829         else if (ifa_scope & IFA_SITE)
3830                 return RT_SCOPE_SITE;
3831         else
3832                 return RT_SCOPE_UNIVERSE;
3833 }
3834
3835 static inline int inet6_ifaddr_msgsize(void)
3836 {
3837         return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
3838                + nla_total_size(16) /* IFA_LOCAL */
3839                + nla_total_size(16) /* IFA_ADDRESS */
3840                + nla_total_size(sizeof(struct ifa_cacheinfo));
3841 }
3842
3843 static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
3844                              u32 portid, u32 seq, int event, unsigned int flags)
3845 {
3846         struct nlmsghdr  *nlh;
3847         u32 preferred, valid;
3848
3849         nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
3850         if (nlh == NULL)
3851                 return -EMSGSIZE;
3852
3853         put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope),
3854                       ifa->idev->dev->ifindex);
3855
3856         if (!(ifa->flags&IFA_F_PERMANENT)) {
3857                 preferred = ifa->prefered_lft;
3858                 valid = ifa->valid_lft;
3859                 if (preferred != INFINITY_LIFE_TIME) {
3860                         long tval = (jiffies - ifa->tstamp)/HZ;
3861                         if (preferred > tval)
3862                                 preferred -= tval;
3863                         else
3864                                 preferred = 0;
3865                         if (valid != INFINITY_LIFE_TIME) {
3866                                 if (valid > tval)
3867                                         valid -= tval;
3868                                 else
3869                                         valid = 0;
3870                         }
3871                 }
3872         } else {
3873                 preferred = INFINITY_LIFE_TIME;
3874                 valid = INFINITY_LIFE_TIME;
3875         }
3876
3877         if (!ipv6_addr_any(&ifa->peer_addr)) {
3878                 if (nla_put(skb, IFA_LOCAL, 16, &ifa->addr) < 0 ||
3879                     nla_put(skb, IFA_ADDRESS, 16, &ifa->peer_addr) < 0)
3880                         goto error;
3881         } else
3882                 if (nla_put(skb, IFA_ADDRESS, 16, &ifa->addr) < 0)
3883                         goto error;
3884
3885         if (put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0)
3886                 goto error;
3887
3888         return nlmsg_end(skb, nlh);
3889
3890 error:
3891         nlmsg_cancel(skb, nlh);
3892         return -EMSGSIZE;
3893 }
3894
3895 static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
3896                                 u32 portid, u32 seq, int event, u16 flags)
3897 {
3898         struct nlmsghdr  *nlh;
3899         u8 scope = RT_SCOPE_UNIVERSE;
3900         int ifindex = ifmca->idev->dev->ifindex;
3901
3902         if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE)
3903                 scope = RT_SCOPE_SITE;
3904
3905         nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
3906         if (nlh == NULL)
3907                 return -EMSGSIZE;
3908
3909         put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
3910         if (nla_put(skb, IFA_MULTICAST, 16, &ifmca->mca_addr) < 0 ||
3911             put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp,
3912                           INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
3913                 nlmsg_cancel(skb, nlh);
3914                 return -EMSGSIZE;
3915         }
3916
3917         return nlmsg_end(skb, nlh);
3918 }
3919
3920 static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
3921                                 u32 portid, u32 seq, int event, unsigned int flags)
3922 {
3923         struct nlmsghdr  *nlh;
3924         u8 scope = RT_SCOPE_UNIVERSE;
3925         int ifindex = ifaca->aca_idev->dev->ifindex;
3926
3927         if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE)
3928                 scope = RT_SCOPE_SITE;
3929
3930         nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
3931         if (nlh == NULL)
3932                 return -EMSGSIZE;
3933
3934         put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
3935         if (nla_put(skb, IFA_ANYCAST, 16, &ifaca->aca_addr) < 0 ||
3936             put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp,
3937                           INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
3938                 nlmsg_cancel(skb, nlh);
3939                 return -EMSGSIZE;
3940         }
3941
3942         return nlmsg_end(skb, nlh);
3943 }
3944
3945 enum addr_type_t {
3946         UNICAST_ADDR,
3947         MULTICAST_ADDR,
3948         ANYCAST_ADDR,
3949 };
3950
3951 /* called with rcu_read_lock() */
3952 static int in6_dump_addrs(struct inet6_dev *idev, struct sk_buff *skb,
3953                           struct netlink_callback *cb, enum addr_type_t type,
3954                           int s_ip_idx, int *p_ip_idx)
3955 {
3956         struct ifmcaddr6 *ifmca;
3957         struct ifacaddr6 *ifaca;
3958         int err = 1;
3959         int ip_idx = *p_ip_idx;
3960
3961         read_lock_bh(&idev->lock);
3962         switch (type) {
3963         case UNICAST_ADDR: {
3964                 struct inet6_ifaddr *ifa;
3965
3966                 /* unicast address incl. temp addr */
3967                 list_for_each_entry(ifa, &idev->addr_list, if_list) {
3968                         if (++ip_idx < s_ip_idx)
3969                                 continue;
3970                         err = inet6_fill_ifaddr(skb, ifa,
3971                                                 NETLINK_CB(cb->skb).portid,
3972                                                 cb->nlh->nlmsg_seq,
3973                                                 RTM_NEWADDR,
3974                                                 NLM_F_MULTI);
3975                         if (err <= 0)
3976                                 break;
3977                         nl_dump_check_consistent(cb, nlmsg_hdr(skb));
3978                 }
3979                 break;
3980         }
3981         case MULTICAST_ADDR:
3982                 /* multicast address */
3983                 for (ifmca = idev->mc_list; ifmca;
3984                      ifmca = ifmca->next, ip_idx++) {
3985                         if (ip_idx < s_ip_idx)
3986                                 continue;
3987                         err = inet6_fill_ifmcaddr(skb, ifmca,
3988                                                   NETLINK_CB(cb->skb).portid,
3989                                                   cb->nlh->nlmsg_seq,
3990                                                   RTM_GETMULTICAST,
3991                                                   NLM_F_MULTI);
3992                         if (err <= 0)
3993                                 break;
3994                 }
3995                 break;
3996         case ANYCAST_ADDR:
3997                 /* anycast address */
3998                 for (ifaca = idev->ac_list; ifaca;
3999                      ifaca = ifaca->aca_next, ip_idx++) {
4000                         if (ip_idx < s_ip_idx)
4001                                 continue;
4002                         err = inet6_fill_ifacaddr(skb, ifaca,
4003                                                   NETLINK_CB(cb->skb).portid,
4004                                                   cb->nlh->nlmsg_seq,
4005                                                   RTM_GETANYCAST,
4006                                                   NLM_F_MULTI);
4007                         if (err <= 0)
4008                                 break;
4009                 }
4010                 break;
4011         default:
4012                 break;
4013         }
4014         read_unlock_bh(&idev->lock);
4015         *p_ip_idx = ip_idx;
4016         return err;
4017 }
4018
4019 static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
4020                            enum addr_type_t type)
4021 {
4022         struct net *net = sock_net(skb->sk);
4023         int h, s_h;
4024         int idx, ip_idx;
4025         int s_idx, s_ip_idx;
4026         struct net_device *dev;
4027         struct inet6_dev *idev;
4028         struct hlist_head *head;
4029
4030         s_h = cb->args[0];
4031         s_idx = idx = cb->args[1];
4032         s_ip_idx = ip_idx = cb->args[2];
4033
4034         rcu_read_lock();
4035         cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^ net->dev_base_seq;
4036         for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4037                 idx = 0;
4038                 head = &net->dev_index_head[h];
4039                 hlist_for_each_entry_rcu(dev, head, index_hlist) {
4040                         if (idx < s_idx)
4041                                 goto cont;
4042                         if (h > s_h || idx > s_idx)
4043                                 s_ip_idx = 0;
4044                         ip_idx = 0;
4045                         idev = __in6_dev_get(dev);
4046                         if (!idev)
4047                                 goto cont;
4048
4049                         if (in6_dump_addrs(idev, skb, cb, type,
4050                                            s_ip_idx, &ip_idx) <= 0)
4051                                 goto done;
4052 cont:
4053                         idx++;
4054                 }
4055         }
4056 done:
4057         rcu_read_unlock();
4058         cb->args[0] = h;
4059         cb->args[1] = idx;
4060         cb->args[2] = ip_idx;
4061
4062         return skb->len;
4063 }
4064
4065 static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
4066 {
4067         enum addr_type_t type = UNICAST_ADDR;
4068
4069         return inet6_dump_addr(skb, cb, type);
4070 }
4071
4072 static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
4073 {
4074         enum addr_type_t type = MULTICAST_ADDR;
4075
4076         return inet6_dump_addr(skb, cb, type);
4077 }
4078
4079
4080 static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
4081 {
4082         enum addr_type_t type = ANYCAST_ADDR;
4083
4084         return inet6_dump_addr(skb, cb, type);
4085 }
4086
4087 static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr *nlh)
4088 {
4089         struct net *net = sock_net(in_skb->sk);
4090         struct ifaddrmsg *ifm;
4091         struct nlattr *tb[IFA_MAX+1];
4092         struct in6_addr *addr = NULL, *peer;
4093         struct net_device *dev = NULL;
4094         struct inet6_ifaddr *ifa;
4095         struct sk_buff *skb;
4096         int err;
4097
4098         err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
4099         if (err < 0)
4100                 goto errout;
4101
4102         addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer);
4103         if (addr == NULL) {
4104                 err = -EINVAL;
4105                 goto errout;
4106         }
4107
4108         ifm = nlmsg_data(nlh);
4109         if (ifm->ifa_index)
4110                 dev = __dev_get_by_index(net, ifm->ifa_index);
4111
4112         ifa = ipv6_get_ifaddr(net, addr, dev, 1);
4113         if (!ifa) {
4114                 err = -EADDRNOTAVAIL;
4115                 goto errout;
4116         }
4117
4118         skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL);
4119         if (!skb) {
4120                 err = -ENOBUFS;
4121                 goto errout_ifa;
4122         }
4123
4124         err = inet6_fill_ifaddr(skb, ifa, NETLINK_CB(in_skb).portid,
4125                                 nlh->nlmsg_seq, RTM_NEWADDR, 0);
4126         if (err < 0) {
4127                 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
4128                 WARN_ON(err == -EMSGSIZE);
4129                 kfree_skb(skb);
4130                 goto errout_ifa;
4131         }
4132         err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
4133 errout_ifa:
4134         in6_ifa_put(ifa);
4135 errout:
4136         return err;
4137 }
4138
4139 static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
4140 {
4141         struct sk_buff *skb;
4142         struct net *net = dev_net(ifa->idev->dev);
4143         int err = -ENOBUFS;
4144
4145         skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC);
4146         if (skb == NULL)
4147                 goto errout;
4148
4149         err = inet6_fill_ifaddr(skb, ifa, 0, 0, event, 0);
4150         if (err < 0) {
4151                 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
4152                 WARN_ON(err == -EMSGSIZE);
4153                 kfree_skb(skb);
4154                 goto errout;
4155         }
4156         rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
4157         return;
4158 errout:
4159         if (err < 0)
4160                 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err);
4161 }
4162
4163 static inline void ipv6_store_devconf(struct ipv6_devconf *cnf,
4164                                 __s32 *array, int bytes)
4165 {
4166         BUG_ON(bytes < (DEVCONF_MAX * 4));
4167
4168         memset(array, 0, bytes);
4169         array[DEVCONF_FORWARDING] = cnf->forwarding;
4170         array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
4171         array[DEVCONF_MTU6] = cnf->mtu6;
4172         array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
4173         array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
4174         array[DEVCONF_AUTOCONF] = cnf->autoconf;
4175         array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
4176         array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
4177         array[DEVCONF_RTR_SOLICIT_INTERVAL] =
4178                 jiffies_to_msecs(cnf->rtr_solicit_interval);
4179         array[DEVCONF_RTR_SOLICIT_DELAY] =
4180                 jiffies_to_msecs(cnf->rtr_solicit_delay);
4181         array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
4182 #ifdef CONFIG_IPV6_PRIVACY
4183         array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
4184         array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
4185         array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
4186         array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
4187         array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
4188 #endif
4189         array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
4190         array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr;
4191         array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo;
4192 #ifdef CONFIG_IPV6_ROUTER_PREF
4193         array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref;
4194         array[DEVCONF_RTR_PROBE_INTERVAL] =
4195                 jiffies_to_msecs(cnf->rtr_probe_interval);
4196 #ifdef CONFIG_IPV6_ROUTE_INFO
4197         array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen;
4198 #endif
4199 #endif
4200         array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp;
4201         array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route;
4202 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
4203         array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad;
4204 #endif
4205 #ifdef CONFIG_IPV6_MROUTE
4206         array[DEVCONF_MC_FORWARDING] = cnf->mc_forwarding;
4207 #endif
4208         array[DEVCONF_DISABLE_IPV6] = cnf->disable_ipv6;
4209         array[DEVCONF_ACCEPT_DAD] = cnf->accept_dad;
4210         array[DEVCONF_FORCE_TLLAO] = cnf->force_tllao;
4211         array[DEVCONF_NDISC_NOTIFY] = cnf->ndisc_notify;
4212 }
4213
4214 static inline size_t inet6_ifla6_size(void)
4215 {
4216         return nla_total_size(4) /* IFLA_INET6_FLAGS */
4217              + nla_total_size(sizeof(struct ifla_cacheinfo))
4218              + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */
4219              + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */
4220              + nla_total_size(ICMP6_MIB_MAX * 8) /* IFLA_INET6_ICMP6STATS */
4221              + nla_total_size(sizeof(struct in6_addr)); /* IFLA_INET6_TOKEN */
4222 }
4223
4224 static inline size_t inet6_if_nlmsg_size(void)
4225 {
4226         return NLMSG_ALIGN(sizeof(struct ifinfomsg))
4227                + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
4228                + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
4229                + nla_total_size(4) /* IFLA_MTU */
4230                + nla_total_size(4) /* IFLA_LINK */
4231                + nla_total_size(inet6_ifla6_size()); /* IFLA_PROTINFO */
4232 }
4233
4234 static inline void __snmp6_fill_statsdev(u64 *stats, atomic_long_t *mib,
4235                                       int items, int bytes)
4236 {
4237         int i;
4238         int pad = bytes - sizeof(u64) * items;
4239         BUG_ON(pad < 0);
4240
4241         /* Use put_unaligned() because stats may not be aligned for u64. */
4242         put_unaligned(items, &stats[0]);
4243         for (i = 1; i < items; i++)
4244                 put_unaligned(atomic_long_read(&mib[i]), &stats[i]);
4245
4246         memset(&stats[items], 0, pad);
4247 }
4248
4249 static inline void __snmp6_fill_stats64(u64 *stats, void __percpu **mib,
4250                                       int items, int bytes, size_t syncpoff)
4251 {
4252         int i;
4253         int pad = bytes - sizeof(u64) * items;
4254         BUG_ON(pad < 0);
4255
4256         /* Use put_unaligned() because stats may not be aligned for u64. */
4257         put_unaligned(items, &stats[0]);
4258         for (i = 1; i < items; i++)
4259                 put_unaligned(snmp_fold_field64(mib, i, syncpoff), &stats[i]);
4260
4261         memset(&stats[items], 0, pad);
4262 }
4263
4264 static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype,
4265                              int bytes)
4266 {
4267         switch (attrtype) {
4268         case IFLA_INET6_STATS:
4269                 __snmp6_fill_stats64(stats, (void __percpu **)idev->stats.ipv6,
4270                                      IPSTATS_MIB_MAX, bytes, offsetof(struct ipstats_mib, syncp));
4271                 break;
4272         case IFLA_INET6_ICMP6STATS:
4273                 __snmp6_fill_statsdev(stats, idev->stats.icmpv6dev->mibs, ICMP6_MIB_MAX, bytes);
4274                 break;
4275         }
4276 }
4277
4278 static int inet6_fill_ifla6_attrs(struct sk_buff *skb, struct inet6_dev *idev)
4279 {
4280         struct nlattr *nla;
4281         struct ifla_cacheinfo ci;
4282
4283         if (nla_put_u32(skb, IFLA_INET6_FLAGS, idev->if_flags))
4284                 goto nla_put_failure;
4285         ci.max_reasm_len = IPV6_MAXPLEN;
4286         ci.tstamp = cstamp_delta(idev->tstamp);
4287         ci.reachable_time = jiffies_to_msecs(idev->nd_parms->reachable_time);
4288         ci.retrans_time = jiffies_to_msecs(idev->nd_parms->retrans_time);
4289         if (nla_put(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci))
4290                 goto nla_put_failure;
4291         nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32));
4292         if (nla == NULL)
4293                 goto nla_put_failure;
4294         ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla));
4295
4296         /* XXX - MC not implemented */
4297
4298         nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64));
4299         if (nla == NULL)
4300                 goto nla_put_failure;
4301         snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla));
4302
4303         nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64));
4304         if (nla == NULL)
4305                 goto nla_put_failure;
4306         snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla));
4307
4308         nla = nla_reserve(skb, IFLA_INET6_TOKEN, sizeof(struct in6_addr));
4309         if (nla == NULL)
4310                 goto nla_put_failure;
4311         read_lock_bh(&idev->lock);
4312         memcpy(nla_data(nla), idev->token.s6_addr, nla_len(nla));
4313         read_unlock_bh(&idev->lock);
4314
4315         return 0;
4316
4317 nla_put_failure:
4318         return -EMSGSIZE;
4319 }
4320
4321 static size_t inet6_get_link_af_size(const struct net_device *dev)
4322 {
4323         if (!__in6_dev_get(dev))
4324                 return 0;
4325
4326         return inet6_ifla6_size();
4327 }
4328
4329 static int inet6_fill_link_af(struct sk_buff *skb, const struct net_device *dev)
4330 {
4331         struct inet6_dev *idev = __in6_dev_get(dev);
4332
4333         if (!idev)
4334                 return -ENODATA;
4335
4336         if (inet6_fill_ifla6_attrs(skb, idev) < 0)
4337                 return -EMSGSIZE;
4338
4339         return 0;
4340 }
4341
4342 static int inet6_set_iftoken(struct inet6_dev *idev, struct in6_addr *token)
4343 {
4344         struct inet6_ifaddr *ifp;
4345         struct net_device *dev = idev->dev;
4346         bool update_rs = false;
4347         struct in6_addr ll_addr;
4348
4349         if (token == NULL)
4350                 return -EINVAL;
4351         if (ipv6_addr_any(token))
4352                 return -EINVAL;
4353         if (dev->flags & (IFF_LOOPBACK | IFF_NOARP))
4354                 return -EINVAL;
4355         if (!ipv6_accept_ra(idev))
4356                 return -EINVAL;
4357         if (idev->cnf.rtr_solicits <= 0)
4358                 return -EINVAL;
4359
4360         write_lock_bh(&idev->lock);
4361
4362         BUILD_BUG_ON(sizeof(token->s6_addr) != 16);
4363         memcpy(idev->token.s6_addr + 8, token->s6_addr + 8, 8);
4364
4365         write_unlock_bh(&idev->lock);
4366
4367         if (!idev->dead && (idev->if_flags & IF_READY) &&
4368             !ipv6_get_lladdr(dev, &ll_addr, IFA_F_TENTATIVE |
4369                              IFA_F_OPTIMISTIC)) {
4370
4371                 /* If we're not ready, then normal ifup will take care
4372                  * of this. Otherwise, we need to request our rs here.
4373                  */
4374                 ndisc_send_rs(dev, &ll_addr, &in6addr_linklocal_allrouters);
4375                 update_rs = true;
4376         }
4377
4378         write_lock_bh(&idev->lock);
4379
4380         if (update_rs) {
4381                 idev->if_flags |= IF_RS_SENT;
4382                 idev->rs_probes = 1;
4383                 addrconf_mod_rs_timer(idev, idev->cnf.rtr_solicit_interval);
4384         }
4385
4386         /* Well, that's kinda nasty ... */
4387         list_for_each_entry(ifp, &idev->addr_list, if_list) {
4388                 spin_lock(&ifp->lock);
4389                 if (ifp->tokenized) {
4390                         ifp->valid_lft = 0;
4391                         ifp->prefered_lft = 0;
4392                 }
4393                 spin_unlock(&ifp->lock);
4394         }
4395
4396         write_unlock_bh(&idev->lock);
4397         addrconf_verify(0);
4398         return 0;
4399 }
4400
4401 static int inet6_set_link_af(struct net_device *dev, const struct nlattr *nla)
4402 {
4403         int err = -EINVAL;
4404         struct inet6_dev *idev = __in6_dev_get(dev);
4405         struct nlattr *tb[IFLA_INET6_MAX + 1];
4406
4407         if (!idev)
4408                 return -EAFNOSUPPORT;
4409
4410         if (nla_parse_nested(tb, IFLA_INET6_MAX, nla, NULL) < 0)
4411                 BUG();
4412
4413         if (tb[IFLA_INET6_TOKEN])
4414                 err = inet6_set_iftoken(idev, nla_data(tb[IFLA_INET6_TOKEN]));
4415
4416         return err;
4417 }
4418
4419 static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
4420                              u32 portid, u32 seq, int event, unsigned int flags)
4421 {
4422         struct net_device *dev = idev->dev;
4423         struct ifinfomsg *hdr;
4424         struct nlmsghdr *nlh;
4425         void *protoinfo;
4426
4427         nlh = nlmsg_put(skb, portid, seq, event, sizeof(*hdr), flags);
4428         if (nlh == NULL)
4429                 return -EMSGSIZE;
4430
4431         hdr = nlmsg_data(nlh);
4432         hdr->ifi_family = AF_INET6;
4433         hdr->__ifi_pad = 0;
4434         hdr->ifi_type = dev->type;
4435         hdr->ifi_index = dev->ifindex;
4436         hdr->ifi_flags = dev_get_flags(dev);
4437         hdr->ifi_change = 0;
4438
4439         if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
4440             (dev->addr_len &&
4441              nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
4442             nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
4443             (dev->ifindex != dev->iflink &&
4444              nla_put_u32(skb, IFLA_LINK, dev->iflink)))
4445                 goto nla_put_failure;
4446         protoinfo = nla_nest_start(skb, IFLA_PROTINFO);
4447         if (protoinfo == NULL)
4448                 goto nla_put_failure;
4449
4450         if (inet6_fill_ifla6_attrs(skb, idev) < 0)
4451                 goto nla_put_failure;
4452
4453         nla_nest_end(skb, protoinfo);
4454         return nlmsg_end(skb, nlh);
4455
4456 nla_put_failure:
4457         nlmsg_cancel(skb, nlh);
4458         return -EMSGSIZE;
4459 }
4460
4461 static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
4462 {
4463         struct net *net = sock_net(skb->sk);
4464         int h, s_h;
4465         int idx = 0, s_idx;
4466         struct net_device *dev;
4467         struct inet6_dev *idev;
4468         struct hlist_head *head;
4469
4470         s_h = cb->args[0];
4471         s_idx = cb->args[1];
4472
4473         rcu_read_lock();
4474         for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4475                 idx = 0;
4476                 head = &net->dev_index_head[h];
4477                 hlist_for_each_entry_rcu(dev, head, index_hlist) {
4478                         if (idx < s_idx)
4479                                 goto cont;
4480                         idev = __in6_dev_get(dev);
4481                         if (!idev)
4482                                 goto cont;
4483                         if (inet6_fill_ifinfo(skb, idev,
4484                                               NETLINK_CB(cb->skb).portid,
4485                                               cb->nlh->nlmsg_seq,
4486                                               RTM_NEWLINK, NLM_F_MULTI) <= 0)
4487                                 goto out;
4488 cont:
4489                         idx++;
4490                 }
4491         }
4492 out:
4493         rcu_read_unlock();
4494         cb->args[1] = idx;
4495         cb->args[0] = h;
4496
4497         return skb->len;
4498 }
4499
4500 void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
4501 {
4502         struct sk_buff *skb;
4503         struct net *net = dev_net(idev->dev);
4504         int err = -ENOBUFS;
4505
4506         skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC);
4507         if (skb == NULL)
4508                 goto errout;
4509
4510         err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0);
4511         if (err < 0) {
4512                 /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */
4513                 WARN_ON(err == -EMSGSIZE);
4514                 kfree_skb(skb);
4515                 goto errout;
4516         }
4517         rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFINFO, NULL, GFP_ATOMIC);
4518         return;
4519 errout:
4520         if (err < 0)
4521                 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFINFO, err);
4522 }
4523
4524 static inline size_t inet6_prefix_nlmsg_size(void)
4525 {
4526         return NLMSG_ALIGN(sizeof(struct prefixmsg))
4527                + nla_total_size(sizeof(struct in6_addr))
4528                + nla_total_size(sizeof(struct prefix_cacheinfo));
4529 }
4530
4531 static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
4532                              struct prefix_info *pinfo, u32 portid, u32 seq,
4533                              int event, unsigned int flags)
4534 {
4535         struct prefixmsg *pmsg;
4536         struct nlmsghdr *nlh;
4537         struct prefix_cacheinfo ci;
4538
4539         nlh = nlmsg_put(skb, portid, seq, event, sizeof(*pmsg), flags);
4540         if (nlh == NULL)
4541                 return -EMSGSIZE;
4542
4543         pmsg = nlmsg_data(nlh);
4544         pmsg->prefix_family = AF_INET6;
4545         pmsg->prefix_pad1 = 0;
4546         pmsg->prefix_pad2 = 0;
4547         pmsg->prefix_ifindex = idev->dev->ifindex;
4548         pmsg->prefix_len = pinfo->prefix_len;
4549         pmsg->prefix_type = pinfo->type;
4550         pmsg->prefix_pad3 = 0;
4551         pmsg->prefix_flags = 0;
4552         if (pinfo->onlink)
4553                 pmsg->prefix_flags |= IF_PREFIX_ONLINK;
4554         if (pinfo->autoconf)
4555                 pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
4556
4557         if (nla_put(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix))
4558                 goto nla_put_failure;
4559         ci.preferred_time = ntohl(pinfo->prefered);
4560         ci.valid_time = ntohl(pinfo->valid);
4561         if (nla_put(skb, PREFIX_CACHEINFO, sizeof(ci), &ci))
4562                 goto nla_put_failure;
4563         return nlmsg_end(skb, nlh);
4564
4565 nla_put_failure:
4566         nlmsg_cancel(skb, nlh);
4567         return -EMSGSIZE;
4568 }
4569
4570 static void inet6_prefix_notify(int event, struct inet6_dev *idev,
4571                          struct prefix_info *pinfo)
4572 {
4573         struct sk_buff *skb;
4574         struct net *net = dev_net(idev->dev);
4575         int err = -ENOBUFS;
4576
4577         skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC);
4578         if (skb == NULL)
4579                 goto errout;
4580
4581         err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0);
4582         if (err < 0) {
4583                 /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */
4584                 WARN_ON(err == -EMSGSIZE);
4585                 kfree_skb(skb);
4586                 goto errout;
4587         }
4588         rtnl_notify(skb, net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC);
4589         return;
4590 errout:
4591         if (err < 0)
4592                 rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err);
4593 }
4594
4595 static void update_valid_ll_addr_cnt(struct inet6_ifaddr *ifp, int count)
4596 {
4597         write_lock_bh(&ifp->idev->lock);
4598         spin_lock(&ifp->lock);
4599         if (((ifp->flags & (IFA_F_PERMANENT|IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|
4600                             IFA_F_DADFAILED)) == IFA_F_PERMANENT) &&
4601             (ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL))
4602                 ifp->idev->valid_ll_addr_cnt += count;
4603         WARN_ON(ifp->idev->valid_ll_addr_cnt < 0);
4604         spin_unlock(&ifp->lock);
4605         write_unlock_bh(&ifp->idev->lock);
4606 }
4607
4608 static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
4609 {
4610         struct net *net = dev_net(ifp->idev->dev);
4611
4612         inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
4613
4614         switch (event) {
4615         case RTM_NEWADDR:
4616                 update_valid_ll_addr_cnt(ifp, 1);
4617
4618                 /*
4619                  * If the address was optimistic
4620                  * we inserted the route at the start of
4621                  * our DAD process, so we don't need
4622                  * to do it again
4623                  */
4624                 if (!(ifp->rt->rt6i_node))
4625                         ip6_ins_rt(ifp->rt);
4626                 if (ifp->idev->cnf.forwarding)
4627                         addrconf_join_anycast(ifp);
4628                 if (!ipv6_addr_any(&ifp->peer_addr))
4629                         addrconf_prefix_route(&ifp->peer_addr, 128,
4630                                               ifp->idev->dev, 0, 0);
4631                 break;
4632         case RTM_DELADDR:
4633                 update_valid_ll_addr_cnt(ifp, -1);
4634
4635                 if (ifp->idev->cnf.forwarding)
4636                         addrconf_leave_anycast(ifp);
4637                 addrconf_leave_solict(ifp->idev, &ifp->addr);
4638                 if (!ipv6_addr_any(&ifp->peer_addr)) {
4639                         struct rt6_info *rt;
4640                         struct net_device *dev = ifp->idev->dev;
4641
4642                         rt = rt6_lookup(dev_net(dev), &ifp->peer_addr, NULL,
4643                                         dev->ifindex, 1);
4644                         if (rt) {
4645                                 dst_hold(&rt->dst);
4646                                 if (ip6_del_rt(rt))
4647                                         dst_free(&rt->dst);
4648                         }
4649                 }
4650                 dst_hold(&ifp->rt->dst);
4651
4652                 if (ip6_del_rt(ifp->rt))
4653                         dst_free(&ifp->rt->dst);
4654                 break;
4655         }
4656         atomic_inc(&net->ipv6.dev_addr_genid);
4657 }
4658
4659 static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
4660 {
4661         rcu_read_lock_bh();
4662         if (likely(ifp->idev->dead == 0))
4663                 __ipv6_ifa_notify(event, ifp);
4664         rcu_read_unlock_bh();
4665 }
4666
4667 #ifdef CONFIG_SYSCTL
4668
4669 static
4670 int addrconf_sysctl_forward(struct ctl_table *ctl, int write,
4671                            void __user *buffer, size_t *lenp, loff_t *ppos)
4672 {
4673         int *valp = ctl->data;
4674         int val = *valp;
4675         loff_t pos = *ppos;
4676         struct ctl_table lctl;
4677         int ret;
4678
4679         /*
4680          * ctl->data points to idev->cnf.forwarding, we should
4681          * not modify it until we get the rtnl lock.
4682          */
4683         lctl = *ctl;
4684         lctl.data = &val;
4685
4686         ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
4687
4688         if (write)
4689                 ret = addrconf_fixup_forwarding(ctl, valp, val);
4690         if (ret)
4691                 *ppos = pos;
4692         return ret;
4693 }
4694
4695 static void dev_disable_change(struct inet6_dev *idev)
4696 {
4697         struct netdev_notifier_info info;
4698
4699         if (!idev || !idev->dev)
4700                 return;
4701
4702         netdev_notifier_info_init(&info, idev->dev);
4703         if (idev->cnf.disable_ipv6)
4704                 addrconf_notify(NULL, NETDEV_DOWN, &info);
4705         else
4706                 addrconf_notify(NULL, NETDEV_UP, &info);
4707 }
4708
4709 static void addrconf_disable_change(struct net *net, __s32 newf)
4710 {
4711         struct net_device *dev;
4712         struct inet6_dev *idev;
4713
4714         rcu_read_lock();
4715         for_each_netdev_rcu(net, dev) {
4716                 idev = __in6_dev_get(dev);
4717                 if (idev) {
4718                         int changed = (!idev->cnf.disable_ipv6) ^ (!newf);
4719                         idev->cnf.disable_ipv6 = newf;
4720                         if (changed)
4721                                 dev_disable_change(idev);
4722                 }
4723         }
4724         rcu_read_unlock();
4725 }
4726
4727 static int addrconf_disable_ipv6(struct ctl_table *table, int *p, int newf)
4728 {
4729         struct net *net;
4730         int old;
4731
4732         if (!rtnl_trylock())
4733                 return restart_syscall();
4734
4735         net = (struct net *)table->extra2;
4736         old = *p;
4737         *p = newf;
4738
4739         if (p == &net->ipv6.devconf_dflt->disable_ipv6) {
4740                 rtnl_unlock();
4741                 return 0;
4742         }
4743
4744         if (p == &net->ipv6.devconf_all->disable_ipv6) {
4745                 net->ipv6.devconf_dflt->disable_ipv6 = newf;
4746                 addrconf_disable_change(net, newf);
4747         } else if ((!newf) ^ (!old))
4748                 dev_disable_change((struct inet6_dev *)table->extra1);
4749
4750         rtnl_unlock();
4751         return 0;
4752 }
4753
4754 static
4755 int addrconf_sysctl_disable(struct ctl_table *ctl, int write,
4756                             void __user *buffer, size_t *lenp, loff_t *ppos)
4757 {
4758         int *valp = ctl->data;
4759         int val = *valp;
4760         loff_t pos = *ppos;
4761         struct ctl_table lctl;
4762         int ret;
4763
4764         /*
4765          * ctl->data points to idev->cnf.disable_ipv6, we should
4766          * not modify it until we get the rtnl lock.
4767          */
4768         lctl = *ctl;
4769         lctl.data = &val;
4770
4771         ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
4772
4773         if (write)
4774                 ret = addrconf_disable_ipv6(ctl, valp, val);
4775         if (ret)
4776                 *ppos = pos;
4777         return ret;
4778 }
4779
4780 static struct addrconf_sysctl_table
4781 {
4782         struct ctl_table_header *sysctl_header;
4783         struct ctl_table addrconf_vars[DEVCONF_MAX+1];
4784 } addrconf_sysctl __read_mostly = {
4785         .sysctl_header = NULL,
4786         .addrconf_vars = {
4787                 {
4788                         .procname       = "forwarding",
4789                         .data           = &ipv6_devconf.forwarding,
4790                         .maxlen         = sizeof(int),
4791                         .mode           = 0644,
4792                         .proc_handler   = addrconf_sysctl_forward,
4793                 },
4794                 {
4795                         .procname       = "hop_limit",
4796                         .data           = &ipv6_devconf.hop_limit,
4797                         .maxlen         = sizeof(int),
4798                         .mode           = 0644,
4799                         .proc_handler   = proc_dointvec,
4800                 },
4801                 {
4802                         .procname       = "mtu",
4803                         .data           = &ipv6_devconf.mtu6,
4804                         .maxlen         = sizeof(int),
4805                         .mode           = 0644,
4806                         .proc_handler   = proc_dointvec,
4807                 },
4808                 {
4809                         .procname       = "accept_ra",
4810                         .data           = &ipv6_devconf.accept_ra,
4811                         .maxlen         = sizeof(int),
4812                         .mode           = 0644,
4813                         .proc_handler   = proc_dointvec,
4814                 },
4815                 {
4816                         .procname       = "accept_redirects",
4817                         .data           = &ipv6_devconf.accept_redirects,
4818                         .maxlen         = sizeof(int),
4819                         .mode           = 0644,
4820                         .proc_handler   = proc_dointvec,
4821                 },
4822                 {
4823                         .procname       = "autoconf",
4824                         .data           = &ipv6_devconf.autoconf,
4825                         .maxlen         = sizeof(int),
4826                         .mode           = 0644,
4827                         .proc_handler   = proc_dointvec,
4828                 },
4829                 {
4830                         .procname       = "dad_transmits",
4831                         .data           = &ipv6_devconf.dad_transmits,
4832                         .maxlen         = sizeof(int),
4833                         .mode           = 0644,
4834                         .proc_handler   = proc_dointvec,
4835                 },
4836                 {
4837                         .procname       = "router_solicitations",
4838                         .data           = &ipv6_devconf.rtr_solicits,
4839                         .maxlen         = sizeof(int),
4840                         .mode           = 0644,
4841                         .proc_handler   = proc_dointvec,
4842                 },
4843                 {
4844                         .procname       = "router_solicitation_interval",
4845                         .data           = &ipv6_devconf.rtr_solicit_interval,
4846                         .maxlen         = sizeof(int),
4847                         .mode           = 0644,
4848                         .proc_handler   = proc_dointvec_jiffies,
4849                 },
4850                 {
4851                         .procname       = "router_solicitation_delay",
4852                         .data           = &ipv6_devconf.rtr_solicit_delay,
4853                         .maxlen         = sizeof(int),
4854                         .mode           = 0644,
4855                         .proc_handler   = proc_dointvec_jiffies,
4856                 },
4857                 {
4858                         .procname       = "force_mld_version",
4859                         .data           = &ipv6_devconf.force_mld_version,
4860                         .maxlen         = sizeof(int),
4861                         .mode           = 0644,
4862                         .proc_handler   = proc_dointvec,
4863                 },
4864 #ifdef CONFIG_IPV6_PRIVACY
4865                 {
4866                         .procname       = "use_tempaddr",
4867                         .data           = &ipv6_devconf.use_tempaddr,
4868                         .maxlen         = sizeof(int),
4869                         .mode           = 0644,
4870                         .proc_handler   = proc_dointvec,
4871                 },
4872                 {
4873                         .procname       = "temp_valid_lft",
4874                         .data           = &ipv6_devconf.temp_valid_lft,
4875                         .maxlen         = sizeof(int),
4876                         .mode           = 0644,
4877                         .proc_handler   = proc_dointvec,
4878                 },
4879                 {
4880                         .procname       = "temp_prefered_lft",
4881                         .data           = &ipv6_devconf.temp_prefered_lft,
4882                         .maxlen         = sizeof(int),
4883                         .mode           = 0644,
4884                         .proc_handler   = proc_dointvec,
4885                 },
4886                 {
4887                         .procname       = "regen_max_retry",
4888                         .data           = &ipv6_devconf.regen_max_retry,
4889                         .maxlen         = sizeof(int),
4890                         .mode           = 0644,
4891                         .proc_handler   = proc_dointvec,
4892                 },
4893                 {
4894                         .procname       = "max_desync_factor",
4895                         .data           = &ipv6_devconf.max_desync_factor,
4896                         .maxlen         = sizeof(int),
4897                         .mode           = 0644,
4898                         .proc_handler   = proc_dointvec,
4899                 },
4900 #endif
4901                 {
4902                         .procname       = "max_addresses",
4903                         .data           = &ipv6_devconf.max_addresses,
4904                         .maxlen         = sizeof(int),
4905                         .mode           = 0644,
4906                         .proc_handler   = proc_dointvec,
4907                 },
4908                 {
4909                         .procname       = "accept_ra_defrtr",
4910                         .data           = &ipv6_devconf.accept_ra_defrtr,
4911                         .maxlen         = sizeof(int),
4912                         .mode           = 0644,
4913                         .proc_handler   = proc_dointvec,
4914                 },
4915                 {
4916                         .procname       = "accept_ra_pinfo",
4917                         .data           = &ipv6_devconf.accept_ra_pinfo,
4918                         .maxlen         = sizeof(int),
4919                         .mode           = 0644,
4920                         .proc_handler   = proc_dointvec,
4921                 },
4922 #ifdef CONFIG_IPV6_ROUTER_PREF
4923                 {
4924                         .procname       = "accept_ra_rtr_pref",
4925                         .data           = &ipv6_devconf.accept_ra_rtr_pref,
4926                         .maxlen         = sizeof(int),
4927                         .mode           = 0644,
4928                         .proc_handler   = proc_dointvec,
4929                 },
4930                 {
4931                         .procname       = "router_probe_interval",
4932                         .data           = &ipv6_devconf.rtr_probe_interval,
4933                         .maxlen         = sizeof(int),
4934                         .mode           = 0644,
4935                         .proc_handler   = proc_dointvec_jiffies,
4936                 },
4937 #ifdef CONFIG_IPV6_ROUTE_INFO
4938                 {
4939                         .procname       = "accept_ra_rt_info_max_plen",
4940                         .data           = &ipv6_devconf.accept_ra_rt_info_max_plen,
4941                         .maxlen         = sizeof(int),
4942                         .mode           = 0644,
4943                         .proc_handler   = proc_dointvec,
4944                 },
4945 #endif
4946 #endif
4947                 {
4948                         .procname       = "proxy_ndp",
4949                         .data           = &ipv6_devconf.proxy_ndp,
4950                         .maxlen         = sizeof(int),
4951                         .mode           = 0644,
4952                         .proc_handler   = proc_dointvec,
4953                 },
4954                 {
4955                         .procname       = "accept_source_route",
4956                         .data           = &ipv6_devconf.accept_source_route,
4957                         .maxlen         = sizeof(int),
4958                         .mode           = 0644,
4959                         .proc_handler   = proc_dointvec,
4960                 },
4961 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
4962                 {
4963                         .procname       = "optimistic_dad",
4964                         .data           = &ipv6_devconf.optimistic_dad,
4965                         .maxlen         = sizeof(int),
4966                         .mode           = 0644,
4967                         .proc_handler   = proc_dointvec,
4968
4969                 },
4970 #endif
4971 #ifdef CONFIG_IPV6_MROUTE
4972                 {
4973                         .procname       = "mc_forwarding",
4974                         .data           = &ipv6_devconf.mc_forwarding,
4975                         .maxlen         = sizeof(int),
4976                         .mode           = 0444,
4977                         .proc_handler   = proc_dointvec,
4978                 },
4979 #endif
4980                 {
4981                         .procname       = "disable_ipv6",
4982                         .data           = &ipv6_devconf.disable_ipv6,
4983                         .maxlen         = sizeof(int),
4984                         .mode           = 0644,
4985                         .proc_handler   = addrconf_sysctl_disable,
4986                 },
4987                 {
4988                         .procname       = "accept_dad",
4989                         .data           = &ipv6_devconf.accept_dad,
4990                         .maxlen         = sizeof(int),
4991                         .mode           = 0644,
4992                         .proc_handler   = proc_dointvec,
4993                 },
4994                 {
4995                         .procname       = "force_tllao",
4996                         .data           = &ipv6_devconf.force_tllao,
4997                         .maxlen         = sizeof(int),
4998                         .mode           = 0644,
4999                         .proc_handler   = proc_dointvec
5000                 },
5001                 {
5002                         .procname       = "ndisc_notify",
5003                         .data           = &ipv6_devconf.ndisc_notify,
5004                         .maxlen         = sizeof(int),
5005                         .mode           = 0644,
5006                         .proc_handler   = proc_dointvec
5007                 },
5008                 {
5009                         /* sentinel */
5010                 }
5011         },
5012 };
5013
5014 static int __addrconf_sysctl_register(struct net *net, char *dev_name,
5015                 struct inet6_dev *idev, struct ipv6_devconf *p)
5016 {
5017         int i;
5018         struct addrconf_sysctl_table *t;
5019         char path[sizeof("net/ipv6/conf/") + IFNAMSIZ];
5020
5021         t = kmemdup(&addrconf_sysctl, sizeof(*t), GFP_KERNEL);
5022         if (t == NULL)
5023                 goto out;
5024
5025         for (i = 0; t->addrconf_vars[i].data; i++) {
5026                 t->addrconf_vars[i].data += (char *)p - (char *)&ipv6_devconf;
5027                 t->addrconf_vars[i].extra1 = idev; /* embedded; no ref */
5028                 t->addrconf_vars[i].extra2 = net;
5029         }
5030
5031         snprintf(path, sizeof(path), "net/ipv6/conf/%s", dev_name);
5032
5033         t->sysctl_header = register_net_sysctl(net, path, t->addrconf_vars);
5034         if (t->sysctl_header == NULL)
5035                 goto free;
5036
5037         p->sysctl = t;
5038         return 0;
5039
5040 free:
5041         kfree(t);
5042 out:
5043         return -ENOBUFS;
5044 }
5045
5046 static void __addrconf_sysctl_unregister(struct ipv6_devconf *p)
5047 {
5048         struct addrconf_sysctl_table *t;
5049
5050         if (p->sysctl == NULL)
5051                 return;
5052
5053         t = p->sysctl;
5054         p->sysctl = NULL;
5055         unregister_net_sysctl_table(t->sysctl_header);
5056         kfree(t);
5057 }
5058
5059 static void addrconf_sysctl_register(struct inet6_dev *idev)
5060 {
5061         neigh_sysctl_register(idev->dev, idev->nd_parms, "ipv6",
5062                               &ndisc_ifinfo_sysctl_change);
5063         __addrconf_sysctl_register(dev_net(idev->dev), idev->dev->name,
5064                                         idev, &idev->cnf);
5065 }
5066
5067 static void addrconf_sysctl_unregister(struct inet6_dev *idev)
5068 {
5069         __addrconf_sysctl_unregister(&idev->cnf);
5070         neigh_sysctl_unregister(idev->nd_parms);
5071 }
5072
5073
5074 #endif
5075
5076 static int __net_init addrconf_init_net(struct net *net)
5077 {
5078         int err = -ENOMEM;
5079         struct ipv6_devconf *all, *dflt;
5080
5081         all = kmemdup(&ipv6_devconf, sizeof(ipv6_devconf), GFP_KERNEL);
5082         if (all == NULL)
5083                 goto err_alloc_all;
5084
5085         dflt = kmemdup(&ipv6_devconf_dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL);
5086         if (dflt == NULL)
5087                 goto err_alloc_dflt;
5088
5089         /* these will be inherited by all namespaces */
5090         dflt->autoconf = ipv6_defaults.autoconf;
5091         dflt->disable_ipv6 = ipv6_defaults.disable_ipv6;
5092
5093         net->ipv6.devconf_all = all;
5094         net->ipv6.devconf_dflt = dflt;
5095
5096 #ifdef CONFIG_SYSCTL
5097         err = __addrconf_sysctl_register(net, "all", NULL, all);
5098         if (err < 0)
5099                 goto err_reg_all;
5100
5101         err = __addrconf_sysctl_register(net, "default", NULL, dflt);
5102         if (err < 0)
5103                 goto err_reg_dflt;
5104 #endif
5105         return 0;
5106
5107 #ifdef CONFIG_SYSCTL
5108 err_reg_dflt:
5109         __addrconf_sysctl_unregister(all);
5110 err_reg_all:
5111         kfree(dflt);
5112 #endif
5113 err_alloc_dflt:
5114         kfree(all);
5115 err_alloc_all:
5116         return err;
5117 }
5118
5119 static void __net_exit addrconf_exit_net(struct net *net)
5120 {
5121 #ifdef CONFIG_SYSCTL
5122         __addrconf_sysctl_unregister(net->ipv6.devconf_dflt);
5123         __addrconf_sysctl_unregister(net->ipv6.devconf_all);
5124 #endif
5125         if (!net_eq(net, &init_net)) {
5126                 kfree(net->ipv6.devconf_dflt);
5127                 kfree(net->ipv6.devconf_all);
5128         }
5129 }
5130
5131 static struct pernet_operations addrconf_ops = {
5132         .init = addrconf_init_net,
5133         .exit = addrconf_exit_net,
5134 };
5135
5136 static struct rtnl_af_ops inet6_ops = {
5137         .family           = AF_INET6,
5138         .fill_link_af     = inet6_fill_link_af,
5139         .get_link_af_size = inet6_get_link_af_size,
5140         .set_link_af      = inet6_set_link_af,
5141 };
5142
5143 /*
5144  *      Init / cleanup code
5145  */
5146
5147 int __init addrconf_init(void)
5148 {
5149         int i, err;
5150
5151         err = ipv6_addr_label_init();
5152         if (err < 0) {
5153                 pr_crit("%s: cannot initialize default policy table: %d\n",
5154                         __func__, err);
5155                 goto out;
5156         }
5157
5158         err = register_pernet_subsys(&addrconf_ops);
5159         if (err < 0)
5160                 goto out_addrlabel;
5161
5162         /* The addrconf netdev notifier requires that loopback_dev
5163          * has it's ipv6 private information allocated and setup
5164          * before it can bring up and give link-local addresses
5165          * to other devices which are up.
5166          *
5167          * Unfortunately, loopback_dev is not necessarily the first
5168          * entry in the global dev_base list of net devices.  In fact,
5169          * it is likely to be the very last entry on that list.
5170          * So this causes the notifier registry below to try and
5171          * give link-local addresses to all devices besides loopback_dev
5172          * first, then loopback_dev, which cases all the non-loopback_dev
5173          * devices to fail to get a link-local address.
5174          *
5175          * So, as a temporary fix, allocate the ipv6 structure for
5176          * loopback_dev first by hand.
5177          * Longer term, all of the dependencies ipv6 has upon the loopback
5178          * device and it being up should be removed.
5179          */
5180         rtnl_lock();
5181         if (!ipv6_add_dev(init_net.loopback_dev))
5182                 err = -ENOMEM;
5183         rtnl_unlock();
5184         if (err)
5185                 goto errlo;
5186
5187         for (i = 0; i < IN6_ADDR_HSIZE; i++)
5188                 INIT_HLIST_HEAD(&inet6_addr_lst[i]);
5189
5190         register_netdevice_notifier(&ipv6_dev_notf);
5191
5192         addrconf_verify(0);
5193
5194         err = rtnl_af_register(&inet6_ops);
5195         if (err < 0)
5196                 goto errout_af;
5197
5198         err = __rtnl_register(PF_INET6, RTM_GETLINK, NULL, inet6_dump_ifinfo,
5199                               NULL);
5200         if (err < 0)
5201                 goto errout;
5202
5203         /* Only the first call to __rtnl_register can fail */
5204         __rtnl_register(PF_INET6, RTM_NEWADDR, inet6_rtm_newaddr, NULL, NULL);
5205         __rtnl_register(PF_INET6, RTM_DELADDR, inet6_rtm_deladdr, NULL, NULL);
5206         __rtnl_register(PF_INET6, RTM_GETADDR, inet6_rtm_getaddr,
5207                         inet6_dump_ifaddr, NULL);
5208         __rtnl_register(PF_INET6, RTM_GETMULTICAST, NULL,
5209                         inet6_dump_ifmcaddr, NULL);
5210         __rtnl_register(PF_INET6, RTM_GETANYCAST, NULL,
5211                         inet6_dump_ifacaddr, NULL);
5212         __rtnl_register(PF_INET6, RTM_GETNETCONF, inet6_netconf_get_devconf,
5213                         inet6_netconf_dump_devconf, NULL);
5214
5215         ipv6_addr_label_rtnl_register();
5216
5217         return 0;
5218 errout:
5219         rtnl_af_unregister(&inet6_ops);
5220 errout_af:
5221         unregister_netdevice_notifier(&ipv6_dev_notf);
5222 errlo:
5223         unregister_pernet_subsys(&addrconf_ops);
5224 out_addrlabel:
5225         ipv6_addr_label_cleanup();
5226 out:
5227         return err;
5228 }
5229
5230 void addrconf_cleanup(void)
5231 {
5232         struct net_device *dev;
5233         int i;
5234
5235         unregister_netdevice_notifier(&ipv6_dev_notf);
5236         unregister_pernet_subsys(&addrconf_ops);
5237         ipv6_addr_label_cleanup();
5238
5239         rtnl_lock();
5240
5241         __rtnl_af_unregister(&inet6_ops);
5242
5243         /* clean dev list */
5244         for_each_netdev(&init_net, dev) {
5245                 if (__in6_dev_get(dev) == NULL)
5246                         continue;
5247                 addrconf_ifdown(dev, 1);
5248         }
5249         addrconf_ifdown(init_net.loopback_dev, 2);
5250
5251         /*
5252          *      Check hash table.
5253          */
5254         spin_lock_bh(&addrconf_hash_lock);
5255         for (i = 0; i < IN6_ADDR_HSIZE; i++)
5256                 WARN_ON(!hlist_empty(&inet6_addr_lst[i]));
5257         spin_unlock_bh(&addrconf_hash_lock);
5258
5259         del_timer(&addr_chk_timer);
5260         rtnl_unlock();
5261 }