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