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