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

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

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

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