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