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