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