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