]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - net/ipv6/addrconf.c
Merge branch 'net-next-2.6-misc-20080612a' of git://git.linux-ipv6.org/gitroot/yoshfu...
[mirror_ubuntu-bionic-kernel.git] / net / ipv6 / addrconf.c
1 /*
2 * IPv6 Address [auto]configuration
3 * Linux INET6 implementation
4 *
5 * Authors:
6 * Pedro Roque <roque@di.fc.ul.pt>
7 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version
12 * 2 of the License, or (at your option) any later version.
13 */
14
15 /*
16 * Changes:
17 *
18 * Janos Farkas : delete timer on ifdown
19 * <chexum@bankinf.banki.hu>
20 * Andi Kleen : kill double kfree on module
21 * unload.
22 * Maciej W. Rozycki : FDDI support
23 * sekiya@USAGI : Don't send too many RS
24 * packets.
25 * yoshfuji@USAGI : Fixed interval between DAD
26 * packets.
27 * YOSHIFUJI Hideaki @USAGI : improved accuracy of
28 * address validation timer.
29 * YOSHIFUJI Hideaki @USAGI : Privacy Extensions (RFC3041)
30 * support.
31 * Yuji SEKIYA @USAGI : Don't assign a same IPv6
32 * address on a same interface.
33 * YOSHIFUJI Hideaki @USAGI : ARCnet support
34 * YOSHIFUJI Hideaki @USAGI : convert /proc/net/if_inet6 to
35 * seq_file.
36 * YOSHIFUJI Hideaki @USAGI : improved source address
37 * selection; consider scope,
38 * status etc.
39 */
40
41 #include <linux/errno.h>
42 #include <linux/types.h>
43 #include <linux/socket.h>
44 #include <linux/sockios.h>
45 #include <linux/net.h>
46 #include <linux/in6.h>
47 #include <linux/netdevice.h>
48 #include <linux/if_addr.h>
49 #include <linux/if_arp.h>
50 #include <linux/if_arcnet.h>
51 #include <linux/if_infiniband.h>
52 #include <linux/route.h>
53 #include <linux/inetdevice.h>
54 #include <linux/init.h>
55 #ifdef CONFIG_SYSCTL
56 #include <linux/sysctl.h>
57 #endif
58 #include <linux/capability.h>
59 #include <linux/delay.h>
60 #include <linux/notifier.h>
61 #include <linux/string.h>
62
63 #include <net/net_namespace.h>
64 #include <net/sock.h>
65 #include <net/snmp.h>
66
67 #include <net/ipv6.h>
68 #include <net/protocol.h>
69 #include <net/ndisc.h>
70 #include <net/ip6_route.h>
71 #include <net/addrconf.h>
72 #include <net/tcp.h>
73 #include <net/ip.h>
74 #include <net/netlink.h>
75 #include <net/pkt_sched.h>
76 #include <linux/if_tunnel.h>
77 #include <linux/rtnetlink.h>
78
79 #ifdef CONFIG_IPV6_PRIVACY
80 #include <linux/random.h>
81 #endif
82
83 #include <asm/uaccess.h>
84 #include <asm/unaligned.h>
85
86 #include <linux/proc_fs.h>
87 #include <linux/seq_file.h>
88
89 /* Set to 3 to get tracing... */
90 #define ACONF_DEBUG 2
91
92 #if ACONF_DEBUG >= 3
93 #define ADBG(x) printk x
94 #else
95 #define ADBG(x)
96 #endif
97
98 #define INFINITY_LIFE_TIME 0xFFFFFFFF
99 #define TIME_DELTA(a,b) ((unsigned long)((long)(a) - (long)(b)))
100
101 #ifdef CONFIG_SYSCTL
102 static void addrconf_sysctl_register(struct inet6_dev *idev);
103 static void addrconf_sysctl_unregister(struct inet6_dev *idev);
104 #else
105 static inline void addrconf_sysctl_register(struct inet6_dev *idev)
106 {
107 }
108
109 static inline void addrconf_sysctl_unregister(struct inet6_dev *idev)
110 {
111 }
112 #endif
113
114 #ifdef CONFIG_IPV6_PRIVACY
115 static int __ipv6_regen_rndid(struct inet6_dev *idev);
116 static int __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr);
117 static void ipv6_regen_rndid(unsigned long data);
118
119 static int desync_factor = MAX_DESYNC_FACTOR * HZ;
120 #endif
121
122 static int ipv6_count_addresses(struct inet6_dev *idev);
123
124 /*
125 * Configured unicast address hash table
126 */
127 static struct inet6_ifaddr *inet6_addr_lst[IN6_ADDR_HSIZE];
128 static DEFINE_RWLOCK(addrconf_hash_lock);
129
130 static void addrconf_verify(unsigned long);
131
132 static DEFINE_TIMER(addr_chk_timer, addrconf_verify, 0, 0);
133 static DEFINE_SPINLOCK(addrconf_verify_lock);
134
135 static void addrconf_join_anycast(struct inet6_ifaddr *ifp);
136 static void addrconf_leave_anycast(struct inet6_ifaddr *ifp);
137
138 static int addrconf_ifdown(struct net_device *dev, int how);
139
140 static void addrconf_dad_start(struct inet6_ifaddr *ifp, u32 flags);
141 static void addrconf_dad_timer(unsigned long data);
142 static void addrconf_dad_completed(struct inet6_ifaddr *ifp);
143 static void addrconf_dad_run(struct inet6_dev *idev);
144 static void addrconf_rs_timer(unsigned long data);
145 static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
146 static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
147
148 static void inet6_prefix_notify(int event, struct inet6_dev *idev,
149 struct prefix_info *pinfo);
150 static int ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
151 struct net_device *dev);
152
153 static ATOMIC_NOTIFIER_HEAD(inet6addr_chain);
154
155 struct ipv6_devconf ipv6_devconf __read_mostly = {
156 .forwarding = 0,
157 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
158 .mtu6 = IPV6_MIN_MTU,
159 .accept_ra = 1,
160 .accept_redirects = 1,
161 .autoconf = 1,
162 .force_mld_version = 0,
163 .dad_transmits = 1,
164 .rtr_solicits = MAX_RTR_SOLICITATIONS,
165 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
166 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
167 #ifdef CONFIG_IPV6_PRIVACY
168 .use_tempaddr = 0,
169 .temp_valid_lft = TEMP_VALID_LIFETIME,
170 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
171 .regen_max_retry = REGEN_MAX_RETRY,
172 .max_desync_factor = MAX_DESYNC_FACTOR,
173 #endif
174 .max_addresses = IPV6_MAX_ADDRESSES,
175 .accept_ra_defrtr = 1,
176 .accept_ra_pinfo = 1,
177 #ifdef CONFIG_IPV6_ROUTER_PREF
178 .accept_ra_rtr_pref = 1,
179 .rtr_probe_interval = 60 * HZ,
180 #ifdef CONFIG_IPV6_ROUTE_INFO
181 .accept_ra_rt_info_max_plen = 0,
182 #endif
183 #endif
184 .proxy_ndp = 0,
185 .accept_source_route = 0, /* we do not accept RH0 by default. */
186 };
187
188 static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = {
189 .forwarding = 0,
190 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
191 .mtu6 = IPV6_MIN_MTU,
192 .accept_ra = 1,
193 .accept_redirects = 1,
194 .autoconf = 1,
195 .dad_transmits = 1,
196 .rtr_solicits = MAX_RTR_SOLICITATIONS,
197 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
198 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
199 #ifdef CONFIG_IPV6_PRIVACY
200 .use_tempaddr = 0,
201 .temp_valid_lft = TEMP_VALID_LIFETIME,
202 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
203 .regen_max_retry = REGEN_MAX_RETRY,
204 .max_desync_factor = MAX_DESYNC_FACTOR,
205 #endif
206 .max_addresses = IPV6_MAX_ADDRESSES,
207 .accept_ra_defrtr = 1,
208 .accept_ra_pinfo = 1,
209 #ifdef CONFIG_IPV6_ROUTER_PREF
210 .accept_ra_rtr_pref = 1,
211 .rtr_probe_interval = 60 * HZ,
212 #ifdef CONFIG_IPV6_ROUTE_INFO
213 .accept_ra_rt_info_max_plen = 0,
214 #endif
215 #endif
216 .proxy_ndp = 0,
217 .accept_source_route = 0, /* we do not accept RH0 by default. */
218 };
219
220 /* IPv6 Wildcard Address and Loopback Address defined by RFC2553 */
221 const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
222 const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
223 const struct in6_addr in6addr_linklocal_allnodes = IN6ADDR_LINKLOCAL_ALLNODES_INIT;
224 const struct in6_addr in6addr_linklocal_allrouters = IN6ADDR_LINKLOCAL_ALLROUTERS_INIT;
225
226 /* Check if a valid qdisc is available */
227 static inline int addrconf_qdisc_ok(struct net_device *dev)
228 {
229 return (dev->qdisc != &noop_qdisc);
230 }
231
232 /* Check if a route is valid prefix route */
233 static inline int addrconf_is_prefix_route(const struct rt6_info *rt)
234 {
235 return ((rt->rt6i_flags & (RTF_GATEWAY | RTF_DEFAULT)) == 0);
236 }
237
238 static void addrconf_del_timer(struct inet6_ifaddr *ifp)
239 {
240 if (del_timer(&ifp->timer))
241 __in6_ifa_put(ifp);
242 }
243
244 enum addrconf_timer_t
245 {
246 AC_NONE,
247 AC_DAD,
248 AC_RS,
249 };
250
251 static void addrconf_mod_timer(struct inet6_ifaddr *ifp,
252 enum addrconf_timer_t what,
253 unsigned long when)
254 {
255 if (!del_timer(&ifp->timer))
256 in6_ifa_hold(ifp);
257
258 switch (what) {
259 case AC_DAD:
260 ifp->timer.function = addrconf_dad_timer;
261 break;
262 case AC_RS:
263 ifp->timer.function = addrconf_rs_timer;
264 break;
265 default:;
266 }
267 ifp->timer.expires = jiffies + when;
268 add_timer(&ifp->timer);
269 }
270
271 static int snmp6_alloc_dev(struct inet6_dev *idev)
272 {
273 if (snmp_mib_init((void **)idev->stats.ipv6,
274 sizeof(struct ipstats_mib)) < 0)
275 goto err_ip;
276 if (snmp_mib_init((void **)idev->stats.icmpv6,
277 sizeof(struct icmpv6_mib)) < 0)
278 goto err_icmp;
279 if (snmp_mib_init((void **)idev->stats.icmpv6msg,
280 sizeof(struct icmpv6msg_mib)) < 0)
281 goto err_icmpmsg;
282
283 return 0;
284
285 err_icmpmsg:
286 snmp_mib_free((void **)idev->stats.icmpv6);
287 err_icmp:
288 snmp_mib_free((void **)idev->stats.ipv6);
289 err_ip:
290 return -ENOMEM;
291 }
292
293 static void snmp6_free_dev(struct inet6_dev *idev)
294 {
295 snmp_mib_free((void **)idev->stats.icmpv6msg);
296 snmp_mib_free((void **)idev->stats.icmpv6);
297 snmp_mib_free((void **)idev->stats.ipv6);
298 }
299
300 /* Nobody refers to this device, we may destroy it. */
301
302 static void in6_dev_finish_destroy_rcu(struct rcu_head *head)
303 {
304 struct inet6_dev *idev = container_of(head, struct inet6_dev, rcu);
305 kfree(idev);
306 }
307
308 void in6_dev_finish_destroy(struct inet6_dev *idev)
309 {
310 struct net_device *dev = idev->dev;
311 BUG_TRAP(idev->addr_list==NULL);
312 BUG_TRAP(idev->mc_list==NULL);
313 #ifdef NET_REFCNT_DEBUG
314 printk(KERN_DEBUG "in6_dev_finish_destroy: %s\n", dev ? dev->name : "NIL");
315 #endif
316 dev_put(dev);
317 if (!idev->dead) {
318 printk("Freeing alive inet6 device %p\n", idev);
319 return;
320 }
321 snmp6_free_dev(idev);
322 call_rcu(&idev->rcu, in6_dev_finish_destroy_rcu);
323 }
324
325 EXPORT_SYMBOL(in6_dev_finish_destroy);
326
327 static struct inet6_dev * ipv6_add_dev(struct net_device *dev)
328 {
329 struct inet6_dev *ndev;
330
331 ASSERT_RTNL();
332
333 if (dev->mtu < IPV6_MIN_MTU)
334 return NULL;
335
336 ndev = kzalloc(sizeof(struct inet6_dev), GFP_KERNEL);
337
338 if (ndev == NULL)
339 return NULL;
340
341 rwlock_init(&ndev->lock);
342 ndev->dev = dev;
343 memcpy(&ndev->cnf, dev_net(dev)->ipv6.devconf_dflt, sizeof(ndev->cnf));
344 ndev->cnf.mtu6 = dev->mtu;
345 ndev->cnf.sysctl = NULL;
346 ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl);
347 if (ndev->nd_parms == NULL) {
348 kfree(ndev);
349 return NULL;
350 }
351 /* We refer to the device */
352 dev_hold(dev);
353
354 if (snmp6_alloc_dev(ndev) < 0) {
355 ADBG((KERN_WARNING
356 "%s(): cannot allocate memory for statistics; dev=%s.\n",
357 __func__, dev->name));
358 neigh_parms_release(&nd_tbl, ndev->nd_parms);
359 ndev->dead = 1;
360 in6_dev_finish_destroy(ndev);
361 return NULL;
362 }
363
364 if (snmp6_register_dev(ndev) < 0) {
365 ADBG((KERN_WARNING
366 "%s(): cannot create /proc/net/dev_snmp6/%s\n",
367 __func__, dev->name));
368 neigh_parms_release(&nd_tbl, ndev->nd_parms);
369 ndev->dead = 1;
370 in6_dev_finish_destroy(ndev);
371 return NULL;
372 }
373
374 /* One reference from device. We must do this before
375 * we invoke __ipv6_regen_rndid().
376 */
377 in6_dev_hold(ndev);
378
379 #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
380 if (dev->type == ARPHRD_SIT && (dev->priv_flags & IFF_ISATAP)) {
381 printk(KERN_INFO
382 "%s: Disabled Multicast RS\n",
383 dev->name);
384 ndev->cnf.rtr_solicits = 0;
385 }
386 #endif
387
388 #ifdef CONFIG_IPV6_PRIVACY
389 setup_timer(&ndev->regen_timer, ipv6_regen_rndid, (unsigned long)ndev);
390 if ((dev->flags&IFF_LOOPBACK) ||
391 dev->type == ARPHRD_TUNNEL ||
392 dev->type == ARPHRD_TUNNEL6 ||
393 dev->type == ARPHRD_SIT ||
394 dev->type == ARPHRD_NONE) {
395 printk(KERN_INFO
396 "%s: Disabled Privacy Extensions\n",
397 dev->name);
398 ndev->cnf.use_tempaddr = -1;
399 } else {
400 in6_dev_hold(ndev);
401 ipv6_regen_rndid((unsigned long) ndev);
402 }
403 #endif
404
405 if (netif_running(dev) && addrconf_qdisc_ok(dev))
406 ndev->if_flags |= IF_READY;
407
408 ipv6_mc_init_dev(ndev);
409 ndev->tstamp = jiffies;
410 addrconf_sysctl_register(ndev);
411 /* protected by rtnl_lock */
412 rcu_assign_pointer(dev->ip6_ptr, ndev);
413
414 /* Join all-node multicast group */
415 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allnodes);
416
417 return ndev;
418 }
419
420 static struct inet6_dev * ipv6_find_idev(struct net_device *dev)
421 {
422 struct inet6_dev *idev;
423
424 ASSERT_RTNL();
425
426 if ((idev = __in6_dev_get(dev)) == NULL) {
427 if ((idev = ipv6_add_dev(dev)) == NULL)
428 return NULL;
429 }
430
431 if (dev->flags&IFF_UP)
432 ipv6_mc_up(idev);
433 return idev;
434 }
435
436 #ifdef CONFIG_SYSCTL
437 static void dev_forward_change(struct inet6_dev *idev)
438 {
439 struct net_device *dev;
440 struct inet6_ifaddr *ifa;
441
442 if (!idev)
443 return;
444 dev = idev->dev;
445 if (dev && (dev->flags & IFF_MULTICAST)) {
446 if (idev->cnf.forwarding)
447 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
448 else
449 ipv6_dev_mc_dec(dev, &in6addr_linklocal_allrouters);
450 }
451 for (ifa=idev->addr_list; ifa; ifa=ifa->if_next) {
452 if (ifa->flags&IFA_F_TENTATIVE)
453 continue;
454 if (idev->cnf.forwarding)
455 addrconf_join_anycast(ifa);
456 else
457 addrconf_leave_anycast(ifa);
458 }
459 }
460
461
462 static void addrconf_forward_change(struct net *net, __s32 newf)
463 {
464 struct net_device *dev;
465 struct inet6_dev *idev;
466
467 read_lock(&dev_base_lock);
468 for_each_netdev(net, dev) {
469 rcu_read_lock();
470 idev = __in6_dev_get(dev);
471 if (idev) {
472 int changed = (!idev->cnf.forwarding) ^ (!newf);
473 idev->cnf.forwarding = newf;
474 if (changed)
475 dev_forward_change(idev);
476 }
477 rcu_read_unlock();
478 }
479 read_unlock(&dev_base_lock);
480 }
481
482 static void addrconf_fixup_forwarding(struct ctl_table *table, int *p, int old)
483 {
484 struct net *net;
485
486 net = (struct net *)table->extra2;
487 if (p == &net->ipv6.devconf_dflt->forwarding)
488 return;
489
490 if (p == &net->ipv6.devconf_all->forwarding) {
491 __s32 newf = net->ipv6.devconf_all->forwarding;
492 net->ipv6.devconf_dflt->forwarding = newf;
493 addrconf_forward_change(net, newf);
494 } else if ((!*p) ^ (!old))
495 dev_forward_change((struct inet6_dev *)table->extra1);
496
497 if (*p)
498 rt6_purge_dflt_routers(net);
499 }
500 #endif
501
502 /* Nobody refers to this ifaddr, destroy it */
503
504 void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp)
505 {
506 BUG_TRAP(ifp->if_next==NULL);
507 BUG_TRAP(ifp->lst_next==NULL);
508 #ifdef NET_REFCNT_DEBUG
509 printk(KERN_DEBUG "inet6_ifa_finish_destroy\n");
510 #endif
511
512 in6_dev_put(ifp->idev);
513
514 if (del_timer(&ifp->timer))
515 printk("Timer is still running, when freeing ifa=%p\n", ifp);
516
517 if (!ifp->dead) {
518 printk("Freeing alive inet6 address %p\n", ifp);
519 return;
520 }
521 dst_release(&ifp->rt->u.dst);
522
523 kfree(ifp);
524 }
525
526 static void
527 ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp)
528 {
529 struct inet6_ifaddr *ifa, **ifap;
530 int ifp_scope = ipv6_addr_src_scope(&ifp->addr);
531
532 /*
533 * Each device address list is sorted in order of scope -
534 * global before linklocal.
535 */
536 for (ifap = &idev->addr_list; (ifa = *ifap) != NULL;
537 ifap = &ifa->if_next) {
538 if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr))
539 break;
540 }
541
542 ifp->if_next = *ifap;
543 *ifap = ifp;
544 }
545
546 /*
547 * Hash function taken from net_alias.c
548 */
549 static u8 ipv6_addr_hash(const struct in6_addr *addr)
550 {
551 __u32 word;
552
553 /*
554 * We perform the hash function over the last 64 bits of the address
555 * This will include the IEEE address token on links that support it.
556 */
557
558 word = (__force u32)(addr->s6_addr32[2] ^ addr->s6_addr32[3]);
559 word ^= (word >> 16);
560 word ^= (word >> 8);
561
562 return ((word ^ (word >> 4)) & 0x0f);
563 }
564
565 /* On success it returns ifp with increased reference count */
566
567 static struct inet6_ifaddr *
568 ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr, int pfxlen,
569 int scope, u32 flags)
570 {
571 struct inet6_ifaddr *ifa = NULL;
572 struct rt6_info *rt;
573 int hash;
574 int err = 0;
575
576 rcu_read_lock_bh();
577 if (idev->dead) {
578 err = -ENODEV; /*XXX*/
579 goto out2;
580 }
581
582 write_lock(&addrconf_hash_lock);
583
584 /* Ignore adding duplicate addresses on an interface */
585 if (ipv6_chk_same_addr(dev_net(idev->dev), addr, idev->dev)) {
586 ADBG(("ipv6_add_addr: already assigned\n"));
587 err = -EEXIST;
588 goto out;
589 }
590
591 ifa = kzalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC);
592
593 if (ifa == NULL) {
594 ADBG(("ipv6_add_addr: malloc failed\n"));
595 err = -ENOBUFS;
596 goto out;
597 }
598
599 rt = addrconf_dst_alloc(idev, addr, 0);
600 if (IS_ERR(rt)) {
601 err = PTR_ERR(rt);
602 goto out;
603 }
604
605 ipv6_addr_copy(&ifa->addr, addr);
606
607 spin_lock_init(&ifa->lock);
608 init_timer(&ifa->timer);
609 ifa->timer.data = (unsigned long) ifa;
610 ifa->scope = scope;
611 ifa->prefix_len = pfxlen;
612 ifa->flags = flags | IFA_F_TENTATIVE;
613 ifa->cstamp = ifa->tstamp = jiffies;
614
615 ifa->rt = rt;
616
617 /*
618 * part one of RFC 4429, section 3.3
619 * We should not configure an address as
620 * optimistic if we do not yet know the link
621 * layer address of our nexhop router
622 */
623
624 if (rt->rt6i_nexthop == NULL)
625 ifa->flags &= ~IFA_F_OPTIMISTIC;
626
627 ifa->idev = idev;
628 in6_dev_hold(idev);
629 /* For caller */
630 in6_ifa_hold(ifa);
631
632 /* Add to big hash table */
633 hash = ipv6_addr_hash(addr);
634
635 ifa->lst_next = inet6_addr_lst[hash];
636 inet6_addr_lst[hash] = ifa;
637 in6_ifa_hold(ifa);
638 write_unlock(&addrconf_hash_lock);
639
640 write_lock(&idev->lock);
641 /* Add to inet6_dev unicast addr list. */
642 ipv6_link_dev_addr(idev, ifa);
643
644 #ifdef CONFIG_IPV6_PRIVACY
645 if (ifa->flags&IFA_F_TEMPORARY) {
646 ifa->tmp_next = idev->tempaddr_list;
647 idev->tempaddr_list = ifa;
648 in6_ifa_hold(ifa);
649 }
650 #endif
651
652 in6_ifa_hold(ifa);
653 write_unlock(&idev->lock);
654 out2:
655 rcu_read_unlock_bh();
656
657 if (likely(err == 0))
658 atomic_notifier_call_chain(&inet6addr_chain, NETDEV_UP, ifa);
659 else {
660 kfree(ifa);
661 ifa = ERR_PTR(err);
662 }
663
664 return ifa;
665 out:
666 write_unlock(&addrconf_hash_lock);
667 goto out2;
668 }
669
670 /* This function wants to get referenced ifp and releases it before return */
671
672 static void ipv6_del_addr(struct inet6_ifaddr *ifp)
673 {
674 struct inet6_ifaddr *ifa, **ifap;
675 struct inet6_dev *idev = ifp->idev;
676 int hash;
677 int deleted = 0, onlink = 0;
678 unsigned long expires = jiffies;
679
680 hash = ipv6_addr_hash(&ifp->addr);
681
682 ifp->dead = 1;
683
684 write_lock_bh(&addrconf_hash_lock);
685 for (ifap = &inet6_addr_lst[hash]; (ifa=*ifap) != NULL;
686 ifap = &ifa->lst_next) {
687 if (ifa == ifp) {
688 *ifap = ifa->lst_next;
689 __in6_ifa_put(ifp);
690 ifa->lst_next = NULL;
691 break;
692 }
693 }
694 write_unlock_bh(&addrconf_hash_lock);
695
696 write_lock_bh(&idev->lock);
697 #ifdef CONFIG_IPV6_PRIVACY
698 if (ifp->flags&IFA_F_TEMPORARY) {
699 for (ifap = &idev->tempaddr_list; (ifa=*ifap) != NULL;
700 ifap = &ifa->tmp_next) {
701 if (ifa == ifp) {
702 *ifap = ifa->tmp_next;
703 if (ifp->ifpub) {
704 in6_ifa_put(ifp->ifpub);
705 ifp->ifpub = NULL;
706 }
707 __in6_ifa_put(ifp);
708 ifa->tmp_next = NULL;
709 break;
710 }
711 }
712 }
713 #endif
714
715 for (ifap = &idev->addr_list; (ifa=*ifap) != NULL;) {
716 if (ifa == ifp) {
717 *ifap = ifa->if_next;
718 __in6_ifa_put(ifp);
719 ifa->if_next = NULL;
720 if (!(ifp->flags & IFA_F_PERMANENT) || onlink > 0)
721 break;
722 deleted = 1;
723 continue;
724 } else if (ifp->flags & IFA_F_PERMANENT) {
725 if (ipv6_prefix_equal(&ifa->addr, &ifp->addr,
726 ifp->prefix_len)) {
727 if (ifa->flags & IFA_F_PERMANENT) {
728 onlink = 1;
729 if (deleted)
730 break;
731 } else {
732 unsigned long lifetime;
733
734 if (!onlink)
735 onlink = -1;
736
737 spin_lock(&ifa->lock);
738
739 lifetime = addrconf_timeout_fixup(ifa->valid_lft, HZ);
740 /*
741 * Note: Because this address is
742 * not permanent, lifetime <
743 * LONG_MAX / HZ here.
744 */
745 if (time_before(expires,
746 ifa->tstamp + lifetime * HZ))
747 expires = ifa->tstamp + lifetime * HZ;
748 spin_unlock(&ifa->lock);
749 }
750 }
751 }
752 ifap = &ifa->if_next;
753 }
754 write_unlock_bh(&idev->lock);
755
756 ipv6_ifa_notify(RTM_DELADDR, ifp);
757
758 atomic_notifier_call_chain(&inet6addr_chain, NETDEV_DOWN, ifp);
759
760 addrconf_del_timer(ifp);
761
762 /*
763 * Purge or update corresponding prefix
764 *
765 * 1) we don't purge prefix here if address was not permanent.
766 * prefix is managed by its own lifetime.
767 * 2) if there're no addresses, delete prefix.
768 * 3) if there're still other permanent address(es),
769 * corresponding prefix is still permanent.
770 * 4) otherwise, update prefix lifetime to the
771 * longest valid lifetime among the corresponding
772 * addresses on the device.
773 * Note: subsequent RA will update lifetime.
774 *
775 * --yoshfuji
776 */
777 if ((ifp->flags & IFA_F_PERMANENT) && onlink < 1) {
778 struct in6_addr prefix;
779 struct rt6_info *rt;
780 struct net *net = dev_net(ifp->idev->dev);
781 ipv6_addr_prefix(&prefix, &ifp->addr, ifp->prefix_len);
782 rt = rt6_lookup(net, &prefix, NULL, ifp->idev->dev->ifindex, 1);
783
784 if (rt && addrconf_is_prefix_route(rt)) {
785 if (onlink == 0) {
786 ip6_del_rt(rt);
787 rt = NULL;
788 } else if (!(rt->rt6i_flags & RTF_EXPIRES)) {
789 rt->rt6i_expires = expires;
790 rt->rt6i_flags |= RTF_EXPIRES;
791 }
792 }
793 dst_release(&rt->u.dst);
794 }
795
796 in6_ifa_put(ifp);
797 }
798
799 #ifdef CONFIG_IPV6_PRIVACY
800 static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift)
801 {
802 struct inet6_dev *idev = ifp->idev;
803 struct in6_addr addr, *tmpaddr;
804 unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_cstamp, tmp_tstamp;
805 unsigned long regen_advance;
806 int tmp_plen;
807 int ret = 0;
808 int max_addresses;
809 u32 addr_flags;
810
811 write_lock(&idev->lock);
812 if (ift) {
813 spin_lock_bh(&ift->lock);
814 memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8);
815 spin_unlock_bh(&ift->lock);
816 tmpaddr = &addr;
817 } else {
818 tmpaddr = NULL;
819 }
820 retry:
821 in6_dev_hold(idev);
822 if (idev->cnf.use_tempaddr <= 0) {
823 write_unlock(&idev->lock);
824 printk(KERN_INFO
825 "ipv6_create_tempaddr(): use_tempaddr is disabled.\n");
826 in6_dev_put(idev);
827 ret = -1;
828 goto out;
829 }
830 spin_lock_bh(&ifp->lock);
831 if (ifp->regen_count++ >= idev->cnf.regen_max_retry) {
832 idev->cnf.use_tempaddr = -1; /*XXX*/
833 spin_unlock_bh(&ifp->lock);
834 write_unlock(&idev->lock);
835 printk(KERN_WARNING
836 "ipv6_create_tempaddr(): regeneration time exceeded. disabled temporary address support.\n");
837 in6_dev_put(idev);
838 ret = -1;
839 goto out;
840 }
841 in6_ifa_hold(ifp);
842 memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
843 if (__ipv6_try_regen_rndid(idev, tmpaddr) < 0) {
844 spin_unlock_bh(&ifp->lock);
845 write_unlock(&idev->lock);
846 printk(KERN_WARNING
847 "ipv6_create_tempaddr(): regeneration of randomized interface id failed.\n");
848 in6_ifa_put(ifp);
849 in6_dev_put(idev);
850 ret = -1;
851 goto out;
852 }
853 memcpy(&addr.s6_addr[8], idev->rndid, 8);
854 tmp_valid_lft = min_t(__u32,
855 ifp->valid_lft,
856 idev->cnf.temp_valid_lft);
857 tmp_prefered_lft = min_t(__u32,
858 ifp->prefered_lft,
859 idev->cnf.temp_prefered_lft - desync_factor / HZ);
860 tmp_plen = ifp->prefix_len;
861 max_addresses = idev->cnf.max_addresses;
862 tmp_cstamp = ifp->cstamp;
863 tmp_tstamp = ifp->tstamp;
864 spin_unlock_bh(&ifp->lock);
865
866 regen_advance = idev->cnf.regen_max_retry *
867 idev->cnf.dad_transmits *
868 idev->nd_parms->retrans_time / HZ;
869 write_unlock(&idev->lock);
870
871 /* A temporary address is created only if this calculated Preferred
872 * Lifetime is greater than REGEN_ADVANCE time units. In particular,
873 * an implementation must not create a temporary address with a zero
874 * Preferred Lifetime.
875 */
876 if (tmp_prefered_lft <= regen_advance) {
877 in6_ifa_put(ifp);
878 in6_dev_put(idev);
879 ret = -1;
880 goto out;
881 }
882
883 addr_flags = IFA_F_TEMPORARY;
884 /* set in addrconf_prefix_rcv() */
885 if (ifp->flags & IFA_F_OPTIMISTIC)
886 addr_flags |= IFA_F_OPTIMISTIC;
887
888 ift = !max_addresses ||
889 ipv6_count_addresses(idev) < max_addresses ?
890 ipv6_add_addr(idev, &addr, tmp_plen,
891 ipv6_addr_type(&addr)&IPV6_ADDR_SCOPE_MASK,
892 addr_flags) : NULL;
893 if (!ift || IS_ERR(ift)) {
894 in6_ifa_put(ifp);
895 in6_dev_put(idev);
896 printk(KERN_INFO
897 "ipv6_create_tempaddr(): retry temporary address regeneration.\n");
898 tmpaddr = &addr;
899 write_lock(&idev->lock);
900 goto retry;
901 }
902
903 spin_lock_bh(&ift->lock);
904 ift->ifpub = ifp;
905 ift->valid_lft = tmp_valid_lft;
906 ift->prefered_lft = tmp_prefered_lft;
907 ift->cstamp = tmp_cstamp;
908 ift->tstamp = tmp_tstamp;
909 spin_unlock_bh(&ift->lock);
910
911 addrconf_dad_start(ift, 0);
912 in6_ifa_put(ift);
913 in6_dev_put(idev);
914 out:
915 return ret;
916 }
917 #endif
918
919 /*
920 * Choose an appropriate source address (RFC3484)
921 */
922 enum {
923 IPV6_SADDR_RULE_INIT = 0,
924 IPV6_SADDR_RULE_LOCAL,
925 IPV6_SADDR_RULE_SCOPE,
926 IPV6_SADDR_RULE_PREFERRED,
927 #ifdef CONFIG_IPV6_MIP6
928 IPV6_SADDR_RULE_HOA,
929 #endif
930 IPV6_SADDR_RULE_OIF,
931 IPV6_SADDR_RULE_LABEL,
932 #ifdef CONFIG_IPV6_PRIVACY
933 IPV6_SADDR_RULE_PRIVACY,
934 #endif
935 IPV6_SADDR_RULE_ORCHID,
936 IPV6_SADDR_RULE_PREFIX,
937 IPV6_SADDR_RULE_MAX
938 };
939
940 struct ipv6_saddr_score {
941 int rule;
942 int addr_type;
943 struct inet6_ifaddr *ifa;
944 DECLARE_BITMAP(scorebits, IPV6_SADDR_RULE_MAX);
945 int scopedist;
946 int matchlen;
947 };
948
949 struct ipv6_saddr_dst {
950 const struct in6_addr *addr;
951 int ifindex;
952 int scope;
953 int label;
954 unsigned int prefs;
955 };
956
957 static inline int ipv6_saddr_preferred(int type)
958 {
959 if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|
960 IPV6_ADDR_LOOPBACK|IPV6_ADDR_RESERVED))
961 return 1;
962 return 0;
963 }
964
965 static int ipv6_get_saddr_eval(struct net *net,
966 struct ipv6_saddr_score *score,
967 struct ipv6_saddr_dst *dst,
968 int i)
969 {
970 int ret;
971
972 if (i <= score->rule) {
973 switch (i) {
974 case IPV6_SADDR_RULE_SCOPE:
975 ret = score->scopedist;
976 break;
977 case IPV6_SADDR_RULE_PREFIX:
978 ret = score->matchlen;
979 break;
980 default:
981 ret = !!test_bit(i, score->scorebits);
982 }
983 goto out;
984 }
985
986 switch (i) {
987 case IPV6_SADDR_RULE_INIT:
988 /* Rule 0: remember if hiscore is not ready yet */
989 ret = !!score->ifa;
990 break;
991 case IPV6_SADDR_RULE_LOCAL:
992 /* Rule 1: Prefer same address */
993 ret = ipv6_addr_equal(&score->ifa->addr, dst->addr);
994 break;
995 case IPV6_SADDR_RULE_SCOPE:
996 /* Rule 2: Prefer appropriate scope
997 *
998 * ret
999 * ^
1000 * -1 | d 15
1001 * ---+--+-+---> scope
1002 * |
1003 * | d is scope of the destination.
1004 * B-d | \
1005 * | \ <- smaller scope is better if
1006 * B-15 | \ if scope is enough for destinaion.
1007 * | ret = B - scope (-1 <= scope >= d <= 15).
1008 * d-C-1 | /
1009 * |/ <- greater is better
1010 * -C / if scope is not enough for destination.
1011 * /| ret = scope - C (-1 <= d < scope <= 15).
1012 *
1013 * d - C - 1 < B -15 (for all -1 <= d <= 15).
1014 * C > d + 14 - B >= 15 + 14 - B = 29 - B.
1015 * Assume B = 0 and we get C > 29.
1016 */
1017 ret = __ipv6_addr_src_scope(score->addr_type);
1018 if (ret >= dst->scope)
1019 ret = -ret;
1020 else
1021 ret -= 128; /* 30 is enough */
1022 score->scopedist = ret;
1023 break;
1024 case IPV6_SADDR_RULE_PREFERRED:
1025 /* Rule 3: Avoid deprecated and optimistic addresses */
1026 ret = ipv6_saddr_preferred(score->addr_type) ||
1027 !(score->ifa->flags & (IFA_F_DEPRECATED|IFA_F_OPTIMISTIC));
1028 break;
1029 #ifdef CONFIG_IPV6_MIP6
1030 case IPV6_SADDR_RULE_HOA:
1031 {
1032 /* Rule 4: Prefer home address */
1033 int prefhome = !(dst->prefs & IPV6_PREFER_SRC_COA);
1034 ret = !(score->ifa->flags & IFA_F_HOMEADDRESS) ^ prefhome;
1035 break;
1036 }
1037 #endif
1038 case IPV6_SADDR_RULE_OIF:
1039 /* Rule 5: Prefer outgoing interface */
1040 ret = (!dst->ifindex ||
1041 dst->ifindex == score->ifa->idev->dev->ifindex);
1042 break;
1043 case IPV6_SADDR_RULE_LABEL:
1044 /* Rule 6: Prefer matching label */
1045 ret = ipv6_addr_label(net,
1046 &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(net, 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(net, hiscore, &dst, i);
1169 miniscore = ipv6_get_saddr_eval(net, 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 }