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