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