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

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

Version: ~ [ linux-5.6 ] ~ [ linux-5.5.13 ] ~ [ linux-5.4.28 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.113 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.174 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.217 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.217 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.82 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

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