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

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

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

~ [ 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