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