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