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