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