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