~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/net/ipv6/addrconf.c

Version: ~ [ linux-5.14-rc3 ] ~ [ linux-5.13.5 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.53 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.135 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.198 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.240 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.276 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.276 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.18.140 ] ~ [ linux-3.16.85 ] ~ [ linux-3.14.79 ] ~ [ linux-3.12.74 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp