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