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