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