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