blob: a5e8d207a51b72eb1c3ae4a65bd1f825f4d7100f [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>
Thomas Graf18237302006-08-04 23:04:54 -070051#include <linux/if_addr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <linux/if_arp.h>
53#include <linux/if_arcnet.h>
54#include <linux/if_infiniband.h>
55#include <linux/route.h>
56#include <linux/inetdevice.h>
57#include <linux/init.h>
58#ifdef CONFIG_SYSCTL
59#include <linux/sysctl.h>
60#endif
Randy Dunlap4fc268d2006-01-11 12:17:47 -080061#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#include <linux/delay.h>
63#include <linux/notifier.h>
Paulo Marques543537b2005-06-23 00:09:02 -070064#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66#include <net/sock.h>
67#include <net/snmp.h>
68
69#include <net/ipv6.h>
70#include <net/protocol.h>
71#include <net/ndisc.h>
72#include <net/ip6_route.h>
73#include <net/addrconf.h>
74#include <net/tcp.h>
75#include <net/ip.h>
Thomas Graf5d620262006-08-15 00:35:02 -070076#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070077#include <linux/if_tunnel.h>
78#include <linux/rtnetlink.h>
79
80#ifdef CONFIG_IPV6_PRIVACY
81#include <linux/random.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070082#endif
83
84#include <asm/uaccess.h>
85
86#include <linux/proc_fs.h>
87#include <linux/seq_file.h>
88
89/* Set to 3 to get tracing... */
90#define ACONF_DEBUG 2
91
92#if ACONF_DEBUG >= 3
93#define ADBG(x) printk x
94#else
95#define ADBG(x)
96#endif
97
98#define INFINITY_LIFE_TIME 0xFFFFFFFF
99#define TIME_DELTA(a,b) ((unsigned long)((long)(a) - (long)(b)))
100
101#ifdef CONFIG_SYSCTL
102static void addrconf_sysctl_register(struct inet6_dev *idev, struct ipv6_devconf *p);
103static void addrconf_sysctl_unregister(struct ipv6_devconf *p);
104#endif
105
106#ifdef CONFIG_IPV6_PRIVACY
107static int __ipv6_regen_rndid(struct inet6_dev *idev);
108static int __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr);
109static void ipv6_regen_rndid(unsigned long data);
110
111static int desync_factor = MAX_DESYNC_FACTOR * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112#endif
113
114static int ipv6_count_addresses(struct inet6_dev *idev);
115
116/*
117 * Configured unicast address hash table
118 */
119static struct inet6_ifaddr *inet6_addr_lst[IN6_ADDR_HSIZE];
120static DEFINE_RWLOCK(addrconf_hash_lock);
121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122static void addrconf_verify(unsigned long);
123
Ingo Molnar8d06afa2005-09-09 13:10:40 -0700124static DEFINE_TIMER(addr_chk_timer, addrconf_verify, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125static DEFINE_SPINLOCK(addrconf_verify_lock);
126
127static void addrconf_join_anycast(struct inet6_ifaddr *ifp);
128static void addrconf_leave_anycast(struct inet6_ifaddr *ifp);
129
130static int addrconf_ifdown(struct net_device *dev, int how);
131
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -0700132static void addrconf_dad_start(struct inet6_ifaddr *ifp, u32 flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133static void addrconf_dad_timer(unsigned long data);
134static void addrconf_dad_completed(struct inet6_ifaddr *ifp);
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +0900135static void addrconf_dad_run(struct inet6_dev *idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136static void addrconf_rs_timer(unsigned long data);
137static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
138static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
139
140static void inet6_prefix_notify(int event, struct inet6_dev *idev,
141 struct prefix_info *pinfo);
142static int ipv6_chk_same_addr(const struct in6_addr *addr, struct net_device *dev);
143
Alan Sterne041c682006-03-27 01:16:30 -0800144static ATOMIC_NOTIFIER_HEAD(inet6addr_chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Brian Haleyab32ea52006-09-22 14:15:41 -0700146struct ipv6_devconf ipv6_devconf __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 .forwarding = 0,
148 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
149 .mtu6 = IPV6_MIN_MTU,
150 .accept_ra = 1,
151 .accept_redirects = 1,
152 .autoconf = 1,
153 .force_mld_version = 0,
154 .dad_transmits = 1,
155 .rtr_solicits = MAX_RTR_SOLICITATIONS,
156 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
157 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
158#ifdef CONFIG_IPV6_PRIVACY
159 .use_tempaddr = 0,
160 .temp_valid_lft = TEMP_VALID_LIFETIME,
161 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
162 .regen_max_retry = REGEN_MAX_RETRY,
163 .max_desync_factor = MAX_DESYNC_FACTOR,
164#endif
165 .max_addresses = IPV6_MAX_ADDRESSES,
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -0800166 .accept_ra_defrtr = 1,
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -0800167 .accept_ra_pinfo = 1,
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800168#ifdef CONFIG_IPV6_ROUTER_PREF
169 .accept_ra_rtr_pref = 1,
YOSHIFUJI Hideaki52e16352006-03-20 17:05:47 -0800170 .rtr_probe_interval = 60 * HZ,
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -0800171#ifdef CONFIG_IPV6_ROUTE_INFO
172 .accept_ra_rt_info_max_plen = 0,
173#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800174#endif
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -0700175 .proxy_ndp = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176};
177
Brian Haleyab32ea52006-09-22 14:15:41 -0700178static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 .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
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -0700206 .proxy_ndp = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207};
208
209/* IPv6 Wildcard Address and Loopback Address defined by RFC2553 */
210#if 0
211const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
212#endif
213const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
214
YOSHIFUJI Hideakib1cacb62005-11-08 09:38:12 -0800215#define IPV6_ADDR_SCOPE_TYPE(scope) ((scope) << 16)
216
217static inline unsigned ipv6_addr_scope2type(unsigned scope)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
YOSHIFUJI Hideakib1cacb62005-11-08 09:38:12 -0800219 switch(scope) {
220 case IPV6_ADDR_SCOPE_NODELOCAL:
221 return (IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_NODELOCAL) |
222 IPV6_ADDR_LOOPBACK);
223 case IPV6_ADDR_SCOPE_LINKLOCAL:
224 return (IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_LINKLOCAL) |
225 IPV6_ADDR_LINKLOCAL);
226 case IPV6_ADDR_SCOPE_SITELOCAL:
227 return (IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_SITELOCAL) |
228 IPV6_ADDR_SITELOCAL);
229 }
230 return IPV6_ADDR_SCOPE_TYPE(scope);
231}
232
233int __ipv6_addr_type(const struct in6_addr *addr)
234{
Al Viroe69a4ad2006-11-14 20:56:00 -0800235 __be32 st;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
237 st = addr->s6_addr32[0];
238
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 /* Consider all addresses with the first three bits different of
YOSHIFUJI Hideakib1cacb62005-11-08 09:38:12 -0800240 000 and 111 as unicasts.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 */
242 if ((st & htonl(0xE0000000)) != htonl(0x00000000) &&
243 (st & htonl(0xE0000000)) != htonl(0xE0000000))
YOSHIFUJI Hideakib1cacb62005-11-08 09:38:12 -0800244 return (IPV6_ADDR_UNICAST |
245 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
YOSHIFUJI Hideakib1cacb62005-11-08 09:38:12 -0800247 if ((st & htonl(0xFF000000)) == htonl(0xFF000000)) {
248 /* multicast */
249 /* addr-select 3.1 */
250 return (IPV6_ADDR_MULTICAST |
251 ipv6_addr_scope2type(IPV6_ADDR_MC_SCOPE(addr)));
252 }
253
254 if ((st & htonl(0xFFC00000)) == htonl(0xFE800000))
255 return (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_UNICAST |
256 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_LINKLOCAL)); /* addr-select 3.1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 if ((st & htonl(0xFFC00000)) == htonl(0xFEC00000))
YOSHIFUJI Hideakib1cacb62005-11-08 09:38:12 -0800258 return (IPV6_ADDR_SITELOCAL | IPV6_ADDR_UNICAST |
259 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_SITELOCAL)); /* addr-select 3.1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
261 if ((addr->s6_addr32[0] | addr->s6_addr32[1]) == 0) {
262 if (addr->s6_addr32[2] == 0) {
263 if (addr->s6_addr32[3] == 0)
264 return IPV6_ADDR_ANY;
265
266 if (addr->s6_addr32[3] == htonl(0x00000001))
YOSHIFUJI Hideakib1cacb62005-11-08 09:38:12 -0800267 return (IPV6_ADDR_LOOPBACK | IPV6_ADDR_UNICAST |
268 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_LINKLOCAL)); /* addr-select 3.4 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
YOSHIFUJI Hideakib1cacb62005-11-08 09:38:12 -0800270 return (IPV6_ADDR_COMPATv4 | IPV6_ADDR_UNICAST |
271 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL)); /* addr-select 3.3 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 }
273
274 if (addr->s6_addr32[2] == htonl(0x0000ffff))
YOSHIFUJI Hideakib1cacb62005-11-08 09:38:12 -0800275 return (IPV6_ADDR_MAPPED |
276 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL)); /* addr-select 3.3 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 }
278
YOSHIFUJI Hideakib1cacb62005-11-08 09:38:12 -0800279 return (IPV6_ADDR_RESERVED |
280 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL)); /* addr-select 3.4 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281}
282
283static void addrconf_del_timer(struct inet6_ifaddr *ifp)
284{
285 if (del_timer(&ifp->timer))
286 __in6_ifa_put(ifp);
287}
288
289enum addrconf_timer_t
290{
291 AC_NONE,
292 AC_DAD,
293 AC_RS,
294};
295
296static void addrconf_mod_timer(struct inet6_ifaddr *ifp,
297 enum addrconf_timer_t what,
298 unsigned long when)
299{
300 if (!del_timer(&ifp->timer))
301 in6_ifa_hold(ifp);
302
303 switch (what) {
304 case AC_DAD:
305 ifp->timer.function = addrconf_dad_timer;
306 break;
307 case AC_RS:
308 ifp->timer.function = addrconf_rs_timer;
309 break;
310 default:;
311 }
312 ifp->timer.expires = jiffies + when;
313 add_timer(&ifp->timer);
314}
315
316/* Nobody refers to this device, we may destroy it. */
317
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -0700318static void in6_dev_finish_destroy_rcu(struct rcu_head *head)
319{
320 struct inet6_dev *idev = container_of(head, struct inet6_dev, rcu);
321 kfree(idev);
322}
323
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324void in6_dev_finish_destroy(struct inet6_dev *idev)
325{
326 struct net_device *dev = idev->dev;
327 BUG_TRAP(idev->addr_list==NULL);
328 BUG_TRAP(idev->mc_list==NULL);
329#ifdef NET_REFCNT_DEBUG
330 printk(KERN_DEBUG "in6_dev_finish_destroy: %s\n", dev ? dev->name : "NIL");
331#endif
332 dev_put(dev);
333 if (!idev->dead) {
334 printk("Freeing alive inet6 device %p\n", idev);
335 return;
336 }
337 snmp6_free_dev(idev);
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -0700338 call_rcu(&idev->rcu, in6_dev_finish_destroy_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339}
340
341static struct inet6_dev * ipv6_add_dev(struct net_device *dev)
342{
343 struct inet6_dev *ndev;
344
345 ASSERT_RTNL();
346
347 if (dev->mtu < IPV6_MIN_MTU)
348 return NULL;
349
Ingo Oeser322f74a2006-03-20 23:01:47 -0800350 ndev = kzalloc(sizeof(struct inet6_dev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Ingo Oeser322f74a2006-03-20 23:01:47 -0800352 if (ndev == NULL)
353 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
Ingo Oeser322f74a2006-03-20 23:01:47 -0800355 rwlock_init(&ndev->lock);
356 ndev->dev = dev;
357 memcpy(&ndev->cnf, &ipv6_devconf_dflt, sizeof(ndev->cnf));
358 ndev->cnf.mtu6 = dev->mtu;
359 ndev->cnf.sysctl = NULL;
360 ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl);
361 if (ndev->nd_parms == NULL) {
362 kfree(ndev);
363 return NULL;
364 }
365 /* We refer to the device */
366 dev_hold(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
Ingo Oeser322f74a2006-03-20 23:01:47 -0800368 if (snmp6_alloc_dev(ndev) < 0) {
369 ADBG((KERN_WARNING
370 "%s(): cannot allocate memory for statistics; dev=%s.\n",
371 __FUNCTION__, dev->name));
372 neigh_parms_release(&nd_tbl, ndev->nd_parms);
373 ndev->dead = 1;
374 in6_dev_finish_destroy(ndev);
375 return NULL;
376 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
Ingo Oeser322f74a2006-03-20 23:01:47 -0800378 if (snmp6_register_dev(ndev) < 0) {
379 ADBG((KERN_WARNING
380 "%s(): cannot create /proc/net/dev_snmp6/%s\n",
381 __FUNCTION__, dev->name));
382 neigh_parms_release(&nd_tbl, ndev->nd_parms);
383 ndev->dead = 1;
384 in6_dev_finish_destroy(ndev);
385 return NULL;
386 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
Ingo Oeser322f74a2006-03-20 23:01:47 -0800388 /* One reference from device. We must do this before
389 * we invoke __ipv6_regen_rndid().
390 */
391 in6_dev_hold(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
393#ifdef CONFIG_IPV6_PRIVACY
Ingo Oeser322f74a2006-03-20 23:01:47 -0800394 init_timer(&ndev->regen_timer);
395 ndev->regen_timer.function = ipv6_regen_rndid;
396 ndev->regen_timer.data = (unsigned long) ndev;
397 if ((dev->flags&IFF_LOOPBACK) ||
398 dev->type == ARPHRD_TUNNEL ||
Joerg Roedel0be669b2006-10-10 14:49:53 -0700399#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
400 dev->type == ARPHRD_SIT ||
401#endif
402 dev->type == ARPHRD_NONE) {
Ingo Oeser322f74a2006-03-20 23:01:47 -0800403 printk(KERN_INFO
404 "%s: Disabled Privacy Extensions\n",
405 dev->name);
406 ndev->cnf.use_tempaddr = -1;
407 } else {
408 in6_dev_hold(ndev);
409 ipv6_regen_rndid((unsigned long) ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 }
Ingo Oeser322f74a2006-03-20 23:01:47 -0800411#endif
412
413 if (netif_carrier_ok(dev))
414 ndev->if_flags |= IF_READY;
415
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -0700416 /* protected by rtnl_lock */
417 rcu_assign_pointer(dev->ip6_ptr, ndev);
Ingo Oeser322f74a2006-03-20 23:01:47 -0800418
419 ipv6_mc_init_dev(ndev);
420 ndev->tstamp = jiffies;
421#ifdef CONFIG_SYSCTL
422 neigh_sysctl_register(dev, ndev->nd_parms, NET_IPV6,
423 NET_IPV6_NEIGH, "ipv6",
424 &ndisc_ifinfo_sysctl_change,
425 NULL);
426 addrconf_sysctl_register(ndev, &ndev->cnf);
427#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 return ndev;
429}
430
431static struct inet6_dev * ipv6_find_idev(struct net_device *dev)
432{
433 struct inet6_dev *idev;
434
435 ASSERT_RTNL();
436
437 if ((idev = __in6_dev_get(dev)) == NULL) {
438 if ((idev = ipv6_add_dev(dev)) == NULL)
439 return NULL;
440 }
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +0900441
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 if (dev->flags&IFF_UP)
443 ipv6_mc_up(idev);
444 return idev;
445}
446
447#ifdef CONFIG_SYSCTL
448static void dev_forward_change(struct inet6_dev *idev)
449{
450 struct net_device *dev;
451 struct inet6_ifaddr *ifa;
452 struct in6_addr addr;
453
454 if (!idev)
455 return;
456 dev = idev->dev;
457 if (dev && (dev->flags & IFF_MULTICAST)) {
458 ipv6_addr_all_routers(&addr);
459
460 if (idev->cnf.forwarding)
461 ipv6_dev_mc_inc(dev, &addr);
462 else
463 ipv6_dev_mc_dec(dev, &addr);
464 }
465 for (ifa=idev->addr_list; ifa; ifa=ifa->if_next) {
466 if (idev->cnf.forwarding)
467 addrconf_join_anycast(ifa);
468 else
469 addrconf_leave_anycast(ifa);
470 }
471}
472
473
474static void addrconf_forward_change(void)
475{
476 struct net_device *dev;
477 struct inet6_dev *idev;
478
479 read_lock(&dev_base_lock);
480 for (dev=dev_base; dev; dev=dev->next) {
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -0700481 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 idev = __in6_dev_get(dev);
483 if (idev) {
484 int changed = (!idev->cnf.forwarding) ^ (!ipv6_devconf.forwarding);
485 idev->cnf.forwarding = ipv6_devconf.forwarding;
486 if (changed)
487 dev_forward_change(idev);
488 }
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -0700489 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 }
491 read_unlock(&dev_base_lock);
492}
493#endif
494
495/* Nobody refers to this ifaddr, destroy it */
496
497void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp)
498{
499 BUG_TRAP(ifp->if_next==NULL);
500 BUG_TRAP(ifp->lst_next==NULL);
501#ifdef NET_REFCNT_DEBUG
502 printk(KERN_DEBUG "inet6_ifa_finish_destroy\n");
503#endif
504
505 in6_dev_put(ifp->idev);
506
507 if (del_timer(&ifp->timer))
508 printk("Timer is still running, when freeing ifa=%p\n", ifp);
509
510 if (!ifp->dead) {
511 printk("Freeing alive inet6 address %p\n", ifp);
512 return;
513 }
514 dst_release(&ifp->rt->u.dst);
515
516 kfree(ifp);
517}
518
Brian Haleye55ffac2006-07-10 15:25:51 -0700519static void
520ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp)
521{
522 struct inet6_ifaddr *ifa, **ifap;
YOSHIFUJI Hideaki8a6ce0c2006-07-11 13:05:30 -0700523 int ifp_scope = ipv6_addr_src_scope(&ifp->addr);
Brian Haleye55ffac2006-07-10 15:25:51 -0700524
525 /*
526 * Each device address list is sorted in order of scope -
527 * global before linklocal.
528 */
529 for (ifap = &idev->addr_list; (ifa = *ifap) != NULL;
530 ifap = &ifa->if_next) {
YOSHIFUJI Hideaki8a6ce0c2006-07-11 13:05:30 -0700531 if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr))
Brian Haleye55ffac2006-07-10 15:25:51 -0700532 break;
533 }
534
535 ifp->if_next = *ifap;
536 *ifap = ifp;
537}
538
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539/* On success it returns ifp with increased reference count */
540
541static struct inet6_ifaddr *
542ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr, int pfxlen,
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -0700543 int scope, u32 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544{
545 struct inet6_ifaddr *ifa = NULL;
546 struct rt6_info *rt;
547 int hash;
548 int err = 0;
549
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -0700550 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 if (idev->dead) {
552 err = -ENODEV; /*XXX*/
553 goto out2;
554 }
555
556 write_lock(&addrconf_hash_lock);
557
558 /* Ignore adding duplicate addresses on an interface */
559 if (ipv6_chk_same_addr(addr, idev->dev)) {
560 ADBG(("ipv6_add_addr: already assigned\n"));
561 err = -EEXIST;
562 goto out;
563 }
564
Ingo Oeser322f74a2006-03-20 23:01:47 -0800565 ifa = kzalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
567 if (ifa == NULL) {
568 ADBG(("ipv6_add_addr: malloc failed\n"));
569 err = -ENOBUFS;
570 goto out;
571 }
572
573 rt = addrconf_dst_alloc(idev, addr, 0);
574 if (IS_ERR(rt)) {
575 err = PTR_ERR(rt);
576 goto out;
577 }
578
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 ipv6_addr_copy(&ifa->addr, addr);
580
581 spin_lock_init(&ifa->lock);
582 init_timer(&ifa->timer);
583 ifa->timer.data = (unsigned long) ifa;
584 ifa->scope = scope;
585 ifa->prefix_len = pfxlen;
586 ifa->flags = flags | IFA_F_TENTATIVE;
587 ifa->cstamp = ifa->tstamp = jiffies;
588
Keir Fraser57f5f542006-08-29 02:43:49 -0700589 ifa->rt = rt;
590
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 ifa->idev = idev;
592 in6_dev_hold(idev);
593 /* For caller */
594 in6_ifa_hold(ifa);
595
596 /* Add to big hash table */
597 hash = ipv6_addr_hash(addr);
598
599 ifa->lst_next = inet6_addr_lst[hash];
600 inet6_addr_lst[hash] = ifa;
601 in6_ifa_hold(ifa);
602 write_unlock(&addrconf_hash_lock);
603
604 write_lock(&idev->lock);
605 /* Add to inet6_dev unicast addr list. */
Brian Haleye55ffac2006-07-10 15:25:51 -0700606 ipv6_link_dev_addr(idev, ifa);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
608#ifdef CONFIG_IPV6_PRIVACY
609 if (ifa->flags&IFA_F_TEMPORARY) {
610 ifa->tmp_next = idev->tempaddr_list;
611 idev->tempaddr_list = ifa;
612 in6_ifa_hold(ifa);
613 }
614#endif
615
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 in6_ifa_hold(ifa);
617 write_unlock(&idev->lock);
618out2:
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -0700619 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
YOSHIFUJI Hideakifd928332005-04-19 22:27:09 -0700621 if (likely(err == 0))
Alan Sterne041c682006-03-27 01:16:30 -0800622 atomic_notifier_call_chain(&inet6addr_chain, NETDEV_UP, ifa);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 else {
624 kfree(ifa);
625 ifa = ERR_PTR(err);
626 }
627
628 return ifa;
629out:
630 write_unlock(&addrconf_hash_lock);
631 goto out2;
632}
633
634/* This function wants to get referenced ifp and releases it before return */
635
636static void ipv6_del_addr(struct inet6_ifaddr *ifp)
637{
638 struct inet6_ifaddr *ifa, **ifap;
639 struct inet6_dev *idev = ifp->idev;
640 int hash;
641 int deleted = 0, onlink = 0;
642 unsigned long expires = jiffies;
643
644 hash = ipv6_addr_hash(&ifp->addr);
645
646 ifp->dead = 1;
647
648 write_lock_bh(&addrconf_hash_lock);
649 for (ifap = &inet6_addr_lst[hash]; (ifa=*ifap) != NULL;
650 ifap = &ifa->lst_next) {
651 if (ifa == ifp) {
652 *ifap = ifa->lst_next;
653 __in6_ifa_put(ifp);
654 ifa->lst_next = NULL;
655 break;
656 }
657 }
658 write_unlock_bh(&addrconf_hash_lock);
659
660 write_lock_bh(&idev->lock);
661#ifdef CONFIG_IPV6_PRIVACY
662 if (ifp->flags&IFA_F_TEMPORARY) {
663 for (ifap = &idev->tempaddr_list; (ifa=*ifap) != NULL;
664 ifap = &ifa->tmp_next) {
665 if (ifa == ifp) {
666 *ifap = ifa->tmp_next;
667 if (ifp->ifpub) {
668 in6_ifa_put(ifp->ifpub);
669 ifp->ifpub = NULL;
670 }
671 __in6_ifa_put(ifp);
672 ifa->tmp_next = NULL;
673 break;
674 }
675 }
676 }
677#endif
678
Kristian Slavov1d142802005-12-21 18:47:24 -0800679 for (ifap = &idev->addr_list; (ifa=*ifap) != NULL;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 if (ifa == ifp) {
681 *ifap = ifa->if_next;
682 __in6_ifa_put(ifp);
683 ifa->if_next = NULL;
684 if (!(ifp->flags & IFA_F_PERMANENT) || onlink > 0)
685 break;
686 deleted = 1;
Kristian Slavov1d142802005-12-21 18:47:24 -0800687 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 } else if (ifp->flags & IFA_F_PERMANENT) {
689 if (ipv6_prefix_equal(&ifa->addr, &ifp->addr,
690 ifp->prefix_len)) {
691 if (ifa->flags & IFA_F_PERMANENT) {
692 onlink = 1;
693 if (deleted)
694 break;
695 } else {
696 unsigned long lifetime;
697
698 if (!onlink)
699 onlink = -1;
700
701 spin_lock(&ifa->lock);
702 lifetime = min_t(unsigned long,
703 ifa->valid_lft, 0x7fffffffUL/HZ);
704 if (time_before(expires,
705 ifa->tstamp + lifetime * HZ))
706 expires = ifa->tstamp + lifetime * HZ;
707 spin_unlock(&ifa->lock);
708 }
709 }
710 }
Kristian Slavov1d142802005-12-21 18:47:24 -0800711 ifap = &ifa->if_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 }
713 write_unlock_bh(&idev->lock);
714
715 ipv6_ifa_notify(RTM_DELADDR, ifp);
716
Alan Sterne041c682006-03-27 01:16:30 -0800717 atomic_notifier_call_chain(&inet6addr_chain, NETDEV_DOWN, ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
719 addrconf_del_timer(ifp);
720
721 /*
722 * Purge or update corresponding prefix
723 *
724 * 1) we don't purge prefix here if address was not permanent.
725 * prefix is managed by its own lifetime.
726 * 2) if there're no addresses, delete prefix.
727 * 3) if there're still other permanent address(es),
728 * corresponding prefix is still permanent.
729 * 4) otherwise, update prefix lifetime to the
730 * longest valid lifetime among the corresponding
731 * addresses on the device.
732 * Note: subsequent RA will update lifetime.
733 *
734 * --yoshfuji
735 */
736 if ((ifp->flags & IFA_F_PERMANENT) && onlink < 1) {
737 struct in6_addr prefix;
738 struct rt6_info *rt;
739
740 ipv6_addr_prefix(&prefix, &ifp->addr, ifp->prefix_len);
741 rt = rt6_lookup(&prefix, NULL, ifp->idev->dev->ifindex, 1);
742
743 if (rt && ((rt->rt6i_flags & (RTF_GATEWAY | RTF_DEFAULT)) == 0)) {
744 if (onlink == 0) {
Thomas Grafe0a1ad732006-08-22 00:00:21 -0700745 ip6_del_rt(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 rt = NULL;
747 } else if (!(rt->rt6i_flags & RTF_EXPIRES)) {
748 rt->rt6i_expires = expires;
749 rt->rt6i_flags |= RTF_EXPIRES;
750 }
751 }
752 dst_release(&rt->u.dst);
753 }
754
755 in6_ifa_put(ifp);
756}
757
758#ifdef CONFIG_IPV6_PRIVACY
759static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift)
760{
761 struct inet6_dev *idev = ifp->idev;
762 struct in6_addr addr, *tmpaddr;
763 unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_cstamp, tmp_tstamp;
764 int tmp_plen;
765 int ret = 0;
766 int max_addresses;
767
768 write_lock(&idev->lock);
769 if (ift) {
770 spin_lock_bh(&ift->lock);
771 memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8);
772 spin_unlock_bh(&ift->lock);
773 tmpaddr = &addr;
774 } else {
775 tmpaddr = NULL;
776 }
777retry:
778 in6_dev_hold(idev);
779 if (idev->cnf.use_tempaddr <= 0) {
780 write_unlock(&idev->lock);
781 printk(KERN_INFO
782 "ipv6_create_tempaddr(): use_tempaddr is disabled.\n");
783 in6_dev_put(idev);
784 ret = -1;
785 goto out;
786 }
787 spin_lock_bh(&ifp->lock);
788 if (ifp->regen_count++ >= idev->cnf.regen_max_retry) {
789 idev->cnf.use_tempaddr = -1; /*XXX*/
790 spin_unlock_bh(&ifp->lock);
791 write_unlock(&idev->lock);
792 printk(KERN_WARNING
793 "ipv6_create_tempaddr(): regeneration time exceeded. disabled temporary address support.\n");
794 in6_dev_put(idev);
795 ret = -1;
796 goto out;
797 }
798 in6_ifa_hold(ifp);
799 memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
800 if (__ipv6_try_regen_rndid(idev, tmpaddr) < 0) {
801 spin_unlock_bh(&ifp->lock);
802 write_unlock(&idev->lock);
803 printk(KERN_WARNING
804 "ipv6_create_tempaddr(): regeneration of randomized interface id failed.\n");
805 in6_ifa_put(ifp);
806 in6_dev_put(idev);
807 ret = -1;
808 goto out;
809 }
810 memcpy(&addr.s6_addr[8], idev->rndid, 8);
811 tmp_valid_lft = min_t(__u32,
812 ifp->valid_lft,
813 idev->cnf.temp_valid_lft);
814 tmp_prefered_lft = min_t(__u32,
815 ifp->prefered_lft,
816 idev->cnf.temp_prefered_lft - desync_factor / HZ);
817 tmp_plen = ifp->prefix_len;
818 max_addresses = idev->cnf.max_addresses;
819 tmp_cstamp = ifp->cstamp;
820 tmp_tstamp = ifp->tstamp;
821 spin_unlock_bh(&ifp->lock);
822
823 write_unlock(&idev->lock);
824 ift = !max_addresses ||
825 ipv6_count_addresses(idev) < max_addresses ?
826 ipv6_add_addr(idev, &addr, tmp_plen,
827 ipv6_addr_type(&addr)&IPV6_ADDR_SCOPE_MASK, IFA_F_TEMPORARY) : NULL;
828 if (!ift || IS_ERR(ift)) {
829 in6_ifa_put(ifp);
830 in6_dev_put(idev);
831 printk(KERN_INFO
832 "ipv6_create_tempaddr(): retry temporary address regeneration.\n");
833 tmpaddr = &addr;
834 write_lock(&idev->lock);
835 goto retry;
836 }
837
838 spin_lock_bh(&ift->lock);
839 ift->ifpub = ifp;
840 ift->valid_lft = tmp_valid_lft;
841 ift->prefered_lft = tmp_prefered_lft;
842 ift->cstamp = tmp_cstamp;
843 ift->tstamp = tmp_tstamp;
844 spin_unlock_bh(&ift->lock);
845
846 addrconf_dad_start(ift, 0);
847 in6_ifa_put(ift);
848 in6_dev_put(idev);
849out:
850 return ret;
851}
852#endif
853
854/*
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800855 * Choose an appropriate source address (RFC3484)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 */
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800857struct ipv6_saddr_score {
858 int addr_type;
859 unsigned int attrs;
860 int matchlen;
Brian Haley0d27b422006-03-11 18:50:14 -0800861 int scope;
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800862 unsigned int rule;
863};
864
865#define IPV6_SADDR_SCORE_LOCAL 0x0001
866#define IPV6_SADDR_SCORE_PREFERRED 0x0004
867#define IPV6_SADDR_SCORE_HOA 0x0008
868#define IPV6_SADDR_SCORE_OIF 0x0010
869#define IPV6_SADDR_SCORE_LABEL 0x0020
870#define IPV6_SADDR_SCORE_PRIVACY 0x0040
871
872static int inline ipv6_saddr_preferred(int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873{
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800874 if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|
875 IPV6_ADDR_LOOPBACK|IPV6_ADDR_RESERVED))
876 return 1;
877 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878}
879
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800880/* static matching label */
881static int inline ipv6_saddr_label(const struct in6_addr *addr, int type)
882{
883 /*
884 * prefix (longest match) label
885 * -----------------------------
886 * ::1/128 0
887 * ::/0 1
888 * 2002::/16 2
889 * ::/96 3
890 * ::ffff:0:0/96 4
Łukasz Stelmach102128e2006-06-22 01:37:19 -0700891 * fc00::/7 5
892 * 2001::/32 6
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800893 */
894 if (type & IPV6_ADDR_LOOPBACK)
895 return 0;
896 else if (type & IPV6_ADDR_COMPATv4)
897 return 3;
898 else if (type & IPV6_ADDR_MAPPED)
899 return 4;
Łukasz Stelmach102128e2006-06-22 01:37:19 -0700900 else if (addr->s6_addr32[0] == htonl(0x20010000))
901 return 6;
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800902 else if (addr->s6_addr16[0] == htons(0x2002))
903 return 2;
Łukasz Stelmach102128e2006-06-22 01:37:19 -0700904 else if ((addr->s6_addr[0] & 0xfe) == 0xfc)
905 return 5;
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800906 return 1;
907}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800909int ipv6_dev_get_saddr(struct net_device *daddr_dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 struct in6_addr *daddr, struct in6_addr *saddr)
911{
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800912 struct ipv6_saddr_score hiscore;
913 struct inet6_ifaddr *ifa_result = NULL;
914 int daddr_type = __ipv6_addr_type(daddr);
915 int daddr_scope = __ipv6_addr_src_scope(daddr_type);
916 u32 daddr_label = ipv6_saddr_label(daddr, daddr_type);
917 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800919 memset(&hiscore, 0, sizeof(hiscore));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
921 read_lock(&dev_base_lock);
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -0700922 rcu_read_lock();
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800923
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 for (dev = dev_base; dev; dev=dev->next) {
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800925 struct inet6_dev *idev;
926 struct inet6_ifaddr *ifa;
927
928 /* Rule 0: Candidate Source Address (section 4)
929 * - multicast and link-local destination address,
930 * the set of candidate source address MUST only
931 * include addresses assigned to interfaces
932 * belonging to the same link as the outgoing
933 * interface.
934 * (- For site-local destination addresses, the
935 * set of candidate source addresses MUST only
936 * include addresses assigned to interfaces
937 * belonging to the same site as the outgoing
938 * interface.)
939 */
940 if ((daddr_type & IPV6_ADDR_MULTICAST ||
941 daddr_scope <= IPV6_ADDR_SCOPE_LINKLOCAL) &&
942 daddr_dev && dev != daddr_dev)
943 continue;
944
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 idev = __in6_dev_get(dev);
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800946 if (!idev)
947 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800949 read_lock_bh(&idev->lock);
950 for (ifa = idev->addr_list; ifa; ifa = ifa->if_next) {
951 struct ipv6_saddr_score score;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800953 score.addr_type = __ipv6_addr_type(&ifa->addr);
954
YOSHIFUJI Hideaki6b3ae802005-12-21 22:58:01 +0900955 /* Rule 0:
956 * - Tentative Address (RFC2462 section 5.4)
957 * - A tentative address is not considered
958 * "assigned to an interface" in the traditional
959 * sense.
960 * - Candidate Source Address (section 4)
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800961 * - In any case, anycast addresses, multicast
962 * addresses, and the unspecified address MUST
963 * NOT be included in a candidate set.
964 */
YOSHIFUJI Hideaki6b3ae802005-12-21 22:58:01 +0900965 if (ifa->flags & IFA_F_TENTATIVE)
966 continue;
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800967 if (unlikely(score.addr_type == IPV6_ADDR_ANY ||
968 score.addr_type & IPV6_ADDR_MULTICAST)) {
969 LIMIT_NETDEBUG(KERN_DEBUG
970 "ADDRCONF: unspecified / multicast address"
971 "assigned as unicast address on %s",
972 dev->name);
973 continue;
974 }
975
976 score.attrs = 0;
977 score.matchlen = 0;
978 score.scope = 0;
979 score.rule = 0;
980
981 if (ifa_result == NULL) {
982 /* record it if the first available entry */
983 goto record_it;
984 }
985
986 /* Rule 1: Prefer same address */
987 if (hiscore.rule < 1) {
988 if (ipv6_addr_equal(&ifa_result->addr, daddr))
989 hiscore.attrs |= IPV6_SADDR_SCORE_LOCAL;
990 hiscore.rule++;
991 }
992 if (ipv6_addr_equal(&ifa->addr, daddr)) {
993 score.attrs |= IPV6_SADDR_SCORE_LOCAL;
994 if (!(hiscore.attrs & IPV6_SADDR_SCORE_LOCAL)) {
995 score.rule = 1;
996 goto record_it;
997 }
998 } else {
999 if (hiscore.attrs & IPV6_SADDR_SCORE_LOCAL)
1000 continue;
1001 }
1002
1003 /* Rule 2: Prefer appropriate scope */
1004 if (hiscore.rule < 2) {
1005 hiscore.scope = __ipv6_addr_src_scope(hiscore.addr_type);
1006 hiscore.rule++;
1007 }
1008 score.scope = __ipv6_addr_src_scope(score.addr_type);
1009 if (hiscore.scope < score.scope) {
1010 if (hiscore.scope < daddr_scope) {
1011 score.rule = 2;
1012 goto record_it;
1013 } else
1014 continue;
1015 } else if (score.scope < hiscore.scope) {
1016 if (score.scope < daddr_scope)
Brian Haleye55ffac2006-07-10 15:25:51 -07001017 break; /* addresses sorted by scope */
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001018 else {
1019 score.rule = 2;
1020 goto record_it;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 }
1022 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001024 /* Rule 3: Avoid deprecated address */
1025 if (hiscore.rule < 3) {
1026 if (ipv6_saddr_preferred(hiscore.addr_type) ||
1027 !(ifa_result->flags & IFA_F_DEPRECATED))
1028 hiscore.attrs |= IPV6_SADDR_SCORE_PREFERRED;
1029 hiscore.rule++;
1030 }
1031 if (ipv6_saddr_preferred(score.addr_type) ||
1032 !(ifa->flags & IFA_F_DEPRECATED)) {
1033 score.attrs |= IPV6_SADDR_SCORE_PREFERRED;
1034 if (!(hiscore.attrs & IPV6_SADDR_SCORE_PREFERRED)) {
1035 score.rule = 3;
1036 goto record_it;
1037 }
1038 } else {
1039 if (hiscore.attrs & IPV6_SADDR_SCORE_PREFERRED)
1040 continue;
1041 }
1042
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07001043 /* Rule 4: Prefer home address */
1044#ifdef CONFIG_IPV6_MIP6
1045 if (hiscore.rule < 4) {
1046 if (ifa_result->flags & IFA_F_HOMEADDRESS)
1047 hiscore.attrs |= IPV6_SADDR_SCORE_HOA;
1048 hiscore.rule++;
1049 }
1050 if (ifa->flags & IFA_F_HOMEADDRESS) {
1051 score.attrs |= IPV6_SADDR_SCORE_HOA;
1052 if (!(ifa_result->flags & IFA_F_HOMEADDRESS)) {
1053 score.rule = 4;
1054 goto record_it;
1055 }
1056 } else {
1057 if (hiscore.attrs & IPV6_SADDR_SCORE_HOA)
1058 continue;
1059 }
1060#else
YOSHIFUJI Hideaki220bbd72005-11-28 22:27:11 -08001061 if (hiscore.rule < 4)
1062 hiscore.rule++;
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07001063#endif
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001064
1065 /* Rule 5: Prefer outgoing interface */
1066 if (hiscore.rule < 5) {
1067 if (daddr_dev == NULL ||
1068 daddr_dev == ifa_result->idev->dev)
1069 hiscore.attrs |= IPV6_SADDR_SCORE_OIF;
1070 hiscore.rule++;
1071 }
1072 if (daddr_dev == NULL ||
1073 daddr_dev == ifa->idev->dev) {
1074 score.attrs |= IPV6_SADDR_SCORE_OIF;
1075 if (!(hiscore.attrs & IPV6_SADDR_SCORE_OIF)) {
1076 score.rule = 5;
1077 goto record_it;
1078 }
1079 } else {
1080 if (hiscore.attrs & IPV6_SADDR_SCORE_OIF)
1081 continue;
1082 }
1083
1084 /* Rule 6: Prefer matching label */
1085 if (hiscore.rule < 6) {
1086 if (ipv6_saddr_label(&ifa_result->addr, hiscore.addr_type) == daddr_label)
1087 hiscore.attrs |= IPV6_SADDR_SCORE_LABEL;
1088 hiscore.rule++;
1089 }
1090 if (ipv6_saddr_label(&ifa->addr, score.addr_type) == daddr_label) {
1091 score.attrs |= IPV6_SADDR_SCORE_LABEL;
1092 if (!(hiscore.attrs & IPV6_SADDR_SCORE_LABEL)) {
1093 score.rule = 6;
1094 goto record_it;
1095 }
1096 } else {
1097 if (hiscore.attrs & IPV6_SADDR_SCORE_LABEL)
1098 continue;
1099 }
1100
Peter Chubb44fd0262005-11-09 13:05:47 -08001101#ifdef CONFIG_IPV6_PRIVACY
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001102 /* Rule 7: Prefer public address
1103 * Note: prefer temprary address if use_tempaddr >= 2
1104 */
1105 if (hiscore.rule < 7) {
1106 if ((!(ifa_result->flags & IFA_F_TEMPORARY)) ^
1107 (ifa_result->idev->cnf.use_tempaddr >= 2))
1108 hiscore.attrs |= IPV6_SADDR_SCORE_PRIVACY;
1109 hiscore.rule++;
1110 }
1111 if ((!(ifa->flags & IFA_F_TEMPORARY)) ^
1112 (ifa->idev->cnf.use_tempaddr >= 2)) {
1113 score.attrs |= IPV6_SADDR_SCORE_PRIVACY;
1114 if (!(hiscore.attrs & IPV6_SADDR_SCORE_PRIVACY)) {
1115 score.rule = 7;
1116 goto record_it;
1117 }
1118 } else {
1119 if (hiscore.attrs & IPV6_SADDR_SCORE_PRIVACY)
1120 continue;
1121 }
YOSHIFUJI Hideaki5e2707f2006-06-22 01:41:18 -07001122#else
1123 if (hiscore.rule < 7)
1124 hiscore.rule++;
Peter Chubb44fd0262005-11-09 13:05:47 -08001125#endif
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001126 /* Rule 8: Use longest matching prefix */
Yan Zheng12da2a42005-11-14 21:42:46 -08001127 if (hiscore.rule < 8) {
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001128 hiscore.matchlen = ipv6_addr_diff(&ifa_result->addr, daddr);
Yan Zheng12da2a42005-11-14 21:42:46 -08001129 hiscore.rule++;
1130 }
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001131 score.matchlen = ipv6_addr_diff(&ifa->addr, daddr);
1132 if (score.matchlen > hiscore.matchlen) {
1133 score.rule = 8;
1134 goto record_it;
1135 }
1136#if 0
1137 else if (score.matchlen < hiscore.matchlen)
1138 continue;
1139#endif
1140
1141 /* Final Rule: choose first available one */
1142 continue;
1143record_it:
1144 if (ifa_result)
1145 in6_ifa_put(ifa_result);
1146 in6_ifa_hold(ifa);
1147 ifa_result = ifa;
1148 hiscore = score;
1149 }
1150 read_unlock_bh(&idev->lock);
1151 }
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001152 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 read_unlock(&dev_base_lock);
1154
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001155 if (!ifa_result)
1156 return -EADDRNOTAVAIL;
1157
1158 ipv6_addr_copy(saddr, &ifa_result->addr);
1159 in6_ifa_put(ifa_result);
1160 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161}
1162
1163
1164int ipv6_get_saddr(struct dst_entry *dst,
1165 struct in6_addr *daddr, struct in6_addr *saddr)
1166{
YOSHIFUJI Hideaki7a3025b2006-10-13 16:17:25 +09001167 return ipv6_dev_get_saddr(dst ? ip6_dst_idev(dst)->dev : NULL, daddr, saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168}
1169
1170
1171int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr)
1172{
1173 struct inet6_dev *idev;
1174 int err = -EADDRNOTAVAIL;
1175
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001176 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 if ((idev = __in6_dev_get(dev)) != NULL) {
1178 struct inet6_ifaddr *ifp;
1179
1180 read_lock_bh(&idev->lock);
1181 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
1182 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
1183 ipv6_addr_copy(addr, &ifp->addr);
1184 err = 0;
1185 break;
1186 }
1187 }
1188 read_unlock_bh(&idev->lock);
1189 }
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001190 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 return err;
1192}
1193
1194static int ipv6_count_addresses(struct inet6_dev *idev)
1195{
1196 int cnt = 0;
1197 struct inet6_ifaddr *ifp;
1198
1199 read_lock_bh(&idev->lock);
1200 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next)
1201 cnt++;
1202 read_unlock_bh(&idev->lock);
1203 return cnt;
1204}
1205
1206int ipv6_chk_addr(struct in6_addr *addr, struct net_device *dev, int strict)
1207{
1208 struct inet6_ifaddr * ifp;
1209 u8 hash = ipv6_addr_hash(addr);
1210
1211 read_lock_bh(&addrconf_hash_lock);
1212 for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
1213 if (ipv6_addr_equal(&ifp->addr, addr) &&
1214 !(ifp->flags&IFA_F_TENTATIVE)) {
1215 if (dev == NULL || ifp->idev->dev == dev ||
1216 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))
1217 break;
1218 }
1219 }
1220 read_unlock_bh(&addrconf_hash_lock);
1221 return ifp != NULL;
1222}
1223
1224static
1225int ipv6_chk_same_addr(const struct in6_addr *addr, struct net_device *dev)
1226{
1227 struct inet6_ifaddr * ifp;
1228 u8 hash = ipv6_addr_hash(addr);
1229
1230 for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
1231 if (ipv6_addr_equal(&ifp->addr, addr)) {
1232 if (dev == NULL || ifp->idev->dev == dev)
1233 break;
1234 }
1235 }
1236 return ifp != NULL;
1237}
1238
1239struct inet6_ifaddr * ipv6_get_ifaddr(struct in6_addr *addr, struct net_device *dev, int strict)
1240{
1241 struct inet6_ifaddr * ifp;
1242 u8 hash = ipv6_addr_hash(addr);
1243
1244 read_lock_bh(&addrconf_hash_lock);
1245 for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
1246 if (ipv6_addr_equal(&ifp->addr, addr)) {
1247 if (dev == NULL || ifp->idev->dev == dev ||
1248 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
1249 in6_ifa_hold(ifp);
1250 break;
1251 }
1252 }
1253 }
1254 read_unlock_bh(&addrconf_hash_lock);
1255
1256 return ifp;
1257}
1258
1259int ipv6_rcv_saddr_equal(const struct sock *sk, const struct sock *sk2)
1260{
1261 const struct in6_addr *sk_rcv_saddr6 = &inet6_sk(sk)->rcv_saddr;
Arnaldo Carvalho de Melo0fa1a532005-12-13 23:23:09 -08001262 const struct in6_addr *sk2_rcv_saddr6 = inet6_rcv_saddr(sk2);
Al Viro82103232006-09-27 18:44:10 -07001263 __be32 sk_rcv_saddr = inet_sk(sk)->rcv_saddr;
1264 __be32 sk2_rcv_saddr = inet_rcv_saddr(sk2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 int sk_ipv6only = ipv6_only_sock(sk);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001266 int sk2_ipv6only = inet_v6_ipv6only(sk2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 int addr_type = ipv6_addr_type(sk_rcv_saddr6);
1268 int addr_type2 = sk2_rcv_saddr6 ? ipv6_addr_type(sk2_rcv_saddr6) : IPV6_ADDR_MAPPED;
1269
1270 if (!sk2_rcv_saddr && !sk_ipv6only)
1271 return 1;
1272
1273 if (addr_type2 == IPV6_ADDR_ANY &&
1274 !(sk2_ipv6only && addr_type == IPV6_ADDR_MAPPED))
1275 return 1;
1276
1277 if (addr_type == IPV6_ADDR_ANY &&
1278 !(sk_ipv6only && addr_type2 == IPV6_ADDR_MAPPED))
1279 return 1;
1280
1281 if (sk2_rcv_saddr6 &&
1282 ipv6_addr_equal(sk_rcv_saddr6, sk2_rcv_saddr6))
1283 return 1;
1284
1285 if (addr_type == IPV6_ADDR_MAPPED &&
1286 !sk2_ipv6only &&
1287 (!sk2_rcv_saddr || !sk_rcv_saddr || sk_rcv_saddr == sk2_rcv_saddr))
1288 return 1;
1289
1290 return 0;
1291}
1292
1293/* Gets referenced address, destroys ifaddr */
1294
Adrian Bunk9f5336e2006-01-07 13:24:25 -08001295static void addrconf_dad_stop(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 if (ifp->flags&IFA_F_PERMANENT) {
1298 spin_lock_bh(&ifp->lock);
1299 addrconf_del_timer(ifp);
1300 ifp->flags |= IFA_F_TENTATIVE;
1301 spin_unlock_bh(&ifp->lock);
1302 in6_ifa_put(ifp);
1303#ifdef CONFIG_IPV6_PRIVACY
1304 } else if (ifp->flags&IFA_F_TEMPORARY) {
1305 struct inet6_ifaddr *ifpub;
1306 spin_lock_bh(&ifp->lock);
1307 ifpub = ifp->ifpub;
1308 if (ifpub) {
1309 in6_ifa_hold(ifpub);
1310 spin_unlock_bh(&ifp->lock);
1311 ipv6_create_tempaddr(ifpub, ifp);
1312 in6_ifa_put(ifpub);
1313 } else {
1314 spin_unlock_bh(&ifp->lock);
1315 }
1316 ipv6_del_addr(ifp);
1317#endif
1318 } else
1319 ipv6_del_addr(ifp);
1320}
1321
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09001322void addrconf_dad_failure(struct inet6_ifaddr *ifp)
1323{
1324 if (net_ratelimit())
1325 printk(KERN_INFO "%s: duplicate address detected!\n", ifp->idev->dev->name);
1326 addrconf_dad_stop(ifp);
1327}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
1329/* Join to solicited addr multicast group. */
1330
1331void addrconf_join_solict(struct net_device *dev, struct in6_addr *addr)
1332{
1333 struct in6_addr maddr;
1334
1335 if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1336 return;
1337
1338 addrconf_addr_solict_mult(addr, &maddr);
1339 ipv6_dev_mc_inc(dev, &maddr);
1340}
1341
1342void addrconf_leave_solict(struct inet6_dev *idev, struct in6_addr *addr)
1343{
1344 struct in6_addr maddr;
1345
1346 if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1347 return;
1348
1349 addrconf_addr_solict_mult(addr, &maddr);
1350 __ipv6_dev_mc_dec(idev, &maddr);
1351}
1352
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03001353static void addrconf_join_anycast(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354{
1355 struct in6_addr addr;
1356 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1357 if (ipv6_addr_any(&addr))
1358 return;
1359 ipv6_dev_ac_inc(ifp->idev->dev, &addr);
1360}
1361
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03001362static void addrconf_leave_anycast(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363{
1364 struct in6_addr addr;
1365 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1366 if (ipv6_addr_any(&addr))
1367 return;
1368 __ipv6_dev_ac_dec(ifp->idev, &addr);
1369}
1370
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08001371static int addrconf_ifid_eui48(u8 *eui, struct net_device *dev)
1372{
1373 if (dev->addr_len != ETH_ALEN)
1374 return -1;
1375 memcpy(eui, dev->dev_addr, 3);
1376 memcpy(eui + 5, dev->dev_addr + 3, 3);
1377
1378 /*
1379 * The zSeries OSA network cards can be shared among various
1380 * OS instances, but the OSA cards have only one MAC address.
1381 * This leads to duplicate address conflicts in conjunction
1382 * with IPv6 if more than one instance uses the same card.
1383 *
1384 * The driver for these cards can deliver a unique 16-bit
1385 * identifier for each instance sharing the same card. It is
1386 * placed instead of 0xFFFE in the interface identifier. The
1387 * "u" bit of the interface identifier is not inverted in this
1388 * case. Hence the resulting interface identifier has local
1389 * scope according to RFC2373.
1390 */
1391 if (dev->dev_id) {
1392 eui[3] = (dev->dev_id >> 8) & 0xFF;
1393 eui[4] = dev->dev_id & 0xFF;
1394 } else {
1395 eui[3] = 0xFF;
1396 eui[4] = 0xFE;
1397 eui[0] ^= 2;
1398 }
1399 return 0;
1400}
1401
1402static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev)
1403{
1404 /* XXX: inherit EUI-64 from other interface -- yoshfuji */
1405 if (dev->addr_len != ARCNET_ALEN)
1406 return -1;
1407 memset(eui, 0, 7);
1408 eui[7] = *(u8*)dev->dev_addr;
1409 return 0;
1410}
1411
1412static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev)
1413{
1414 if (dev->addr_len != INFINIBAND_ALEN)
1415 return -1;
1416 memcpy(eui, dev->dev_addr + 12, 8);
1417 eui[0] |= 2;
1418 return 0;
1419}
1420
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
1422{
1423 switch (dev->type) {
1424 case ARPHRD_ETHER:
1425 case ARPHRD_FDDI:
1426 case ARPHRD_IEEE802_TR:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08001427 return addrconf_ifid_eui48(eui, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 case ARPHRD_ARCNET:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08001429 return addrconf_ifid_arcnet(eui, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 case ARPHRD_INFINIBAND:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08001431 return addrconf_ifid_infiniband(eui, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 }
1433 return -1;
1434}
1435
1436static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
1437{
1438 int err = -1;
1439 struct inet6_ifaddr *ifp;
1440
1441 read_lock_bh(&idev->lock);
1442 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
1443 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
1444 memcpy(eui, ifp->addr.s6_addr+8, 8);
1445 err = 0;
1446 break;
1447 }
1448 }
1449 read_unlock_bh(&idev->lock);
1450 return err;
1451}
1452
1453#ifdef CONFIG_IPV6_PRIVACY
1454/* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
1455static int __ipv6_regen_rndid(struct inet6_dev *idev)
1456{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457regen:
YOSHIFUJI Hideaki955189e2006-03-20 16:54:09 -08001458 get_random_bytes(idev->rndid, sizeof(idev->rndid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 idev->rndid[0] &= ~0x02;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
1461 /*
1462 * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
1463 * check if generated address is not inappropriate
1464 *
1465 * - Reserved subnet anycast (RFC 2526)
1466 * 11111101 11....11 1xxxxxxx
1467 * - ISATAP (draft-ietf-ngtrans-isatap-13.txt) 5.1
1468 * 00-00-5E-FE-xx-xx-xx-xx
1469 * - value 0
1470 * - XXX: already assigned to an address on the device
1471 */
1472 if (idev->rndid[0] == 0xfd &&
1473 (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff &&
1474 (idev->rndid[7]&0x80))
1475 goto regen;
1476 if ((idev->rndid[0]|idev->rndid[1]) == 0) {
1477 if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe)
1478 goto regen;
1479 if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00)
1480 goto regen;
1481 }
1482
1483 return 0;
1484}
1485
1486static void ipv6_regen_rndid(unsigned long data)
1487{
1488 struct inet6_dev *idev = (struct inet6_dev *) data;
1489 unsigned long expires;
1490
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001491 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 write_lock_bh(&idev->lock);
1493
1494 if (idev->dead)
1495 goto out;
1496
1497 if (__ipv6_regen_rndid(idev) < 0)
1498 goto out;
1499
1500 expires = jiffies +
1501 idev->cnf.temp_prefered_lft * HZ -
1502 idev->cnf.regen_max_retry * idev->cnf.dad_transmits * idev->nd_parms->retrans_time - desync_factor;
1503 if (time_before(expires, jiffies)) {
1504 printk(KERN_WARNING
1505 "ipv6_regen_rndid(): too short regeneration interval; timer disabled for %s.\n",
1506 idev->dev->name);
1507 goto out;
1508 }
1509
1510 if (!mod_timer(&idev->regen_timer, expires))
1511 in6_dev_hold(idev);
1512
1513out:
1514 write_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001515 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 in6_dev_put(idev);
1517}
1518
1519static int __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr) {
1520 int ret = 0;
1521
1522 if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0)
1523 ret = __ipv6_regen_rndid(idev);
1524 return ret;
1525}
1526#endif
1527
1528/*
1529 * Add prefix route.
1530 */
1531
1532static void
1533addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev,
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -07001534 unsigned long expires, u32 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535{
Thomas Graf86872cb2006-08-22 00:01:08 -07001536 struct fib6_config cfg = {
1537 .fc_table = RT6_TABLE_PREFIX,
1538 .fc_metric = IP6_RT_PRIO_ADDRCONF,
1539 .fc_ifindex = dev->ifindex,
1540 .fc_expires = expires,
1541 .fc_dst_len = plen,
1542 .fc_flags = RTF_UP | flags,
1543 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544
Thomas Graf86872cb2006-08-22 00:01:08 -07001545 ipv6_addr_copy(&cfg.fc_dst, pfx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546
1547 /* Prevent useless cloning on PtP SIT.
1548 This thing is done here expecting that the whole
1549 class of non-broadcast devices need not cloning.
1550 */
Joerg Roedel0be669b2006-10-10 14:49:53 -07001551#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
Thomas Graf86872cb2006-08-22 00:01:08 -07001552 if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT))
1553 cfg.fc_flags |= RTF_NONEXTHOP;
Joerg Roedel0be669b2006-10-10 14:49:53 -07001554#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555
Thomas Graf86872cb2006-08-22 00:01:08 -07001556 ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557}
1558
1559/* Create "default" multicast route to the interface */
1560
1561static void addrconf_add_mroute(struct net_device *dev)
1562{
Thomas Graf86872cb2006-08-22 00:01:08 -07001563 struct fib6_config cfg = {
1564 .fc_table = RT6_TABLE_LOCAL,
1565 .fc_metric = IP6_RT_PRIO_ADDRCONF,
1566 .fc_ifindex = dev->ifindex,
1567 .fc_dst_len = 8,
1568 .fc_flags = RTF_UP,
1569 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570
Thomas Graf86872cb2006-08-22 00:01:08 -07001571 ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0);
1572
1573 ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574}
1575
Joerg Roedel0be669b2006-10-10 14:49:53 -07001576#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577static void sit_route_add(struct net_device *dev)
1578{
Thomas Graf86872cb2006-08-22 00:01:08 -07001579 struct fib6_config cfg = {
1580 .fc_table = RT6_TABLE_MAIN,
1581 .fc_metric = IP6_RT_PRIO_ADDRCONF,
1582 .fc_ifindex = dev->ifindex,
1583 .fc_dst_len = 96,
1584 .fc_flags = RTF_UP | RTF_NONEXTHOP,
1585 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
1587 /* prefix length - 96 bits "::d.d.d.d" */
Thomas Graf86872cb2006-08-22 00:01:08 -07001588 ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589}
Joerg Roedel0be669b2006-10-10 14:49:53 -07001590#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
1592static void addrconf_add_lroute(struct net_device *dev)
1593{
1594 struct in6_addr addr;
1595
1596 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
1597 addrconf_prefix_route(&addr, 64, dev, 0, 0);
1598}
1599
1600static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
1601{
1602 struct inet6_dev *idev;
1603
1604 ASSERT_RTNL();
1605
1606 if ((idev = ipv6_find_idev(dev)) == NULL)
1607 return NULL;
1608
1609 /* Add default multicast route */
1610 addrconf_add_mroute(dev);
1611
1612 /* Add link local route */
1613 addrconf_add_lroute(dev);
1614 return idev;
1615}
1616
1617void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len)
1618{
1619 struct prefix_info *pinfo;
1620 __u32 valid_lft;
1621 __u32 prefered_lft;
1622 int addr_type;
1623 unsigned long rt_expires;
1624 struct inet6_dev *in6_dev;
1625
1626 pinfo = (struct prefix_info *) opt;
1627
1628 if (len < sizeof(struct prefix_info)) {
1629 ADBG(("addrconf: prefix option too short\n"));
1630 return;
1631 }
1632
1633 /*
1634 * Validation checks ([ADDRCONF], page 19)
1635 */
1636
1637 addr_type = ipv6_addr_type(&pinfo->prefix);
1638
1639 if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
1640 return;
1641
1642 valid_lft = ntohl(pinfo->valid);
1643 prefered_lft = ntohl(pinfo->prefered);
1644
1645 if (prefered_lft > valid_lft) {
1646 if (net_ratelimit())
1647 printk(KERN_WARNING "addrconf: prefix option has invalid lifetime\n");
1648 return;
1649 }
1650
1651 in6_dev = in6_dev_get(dev);
1652
1653 if (in6_dev == NULL) {
1654 if (net_ratelimit())
1655 printk(KERN_DEBUG "addrconf: device %s not configured\n", dev->name);
1656 return;
1657 }
1658
1659 /*
1660 * Two things going on here:
1661 * 1) Add routes for on-link prefixes
1662 * 2) Configure prefixes with the auto flag set
1663 */
1664
1665 /* Avoid arithmetic overflow. Really, we could
1666 save rt_expires in seconds, likely valid_lft,
1667 but it would require division in fib gc, that it
1668 not good.
1669 */
1670 if (valid_lft >= 0x7FFFFFFF/HZ)
YOSHIFUJI Hideaki3dd4bc62005-12-19 14:02:45 -08001671 rt_expires = 0x7FFFFFFF - (0x7FFFFFFF % HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 else
YOSHIFUJI Hideaki3dd4bc62005-12-19 14:02:45 -08001673 rt_expires = valid_lft * HZ;
1674
1675 /*
1676 * We convert this (in jiffies) to clock_t later.
1677 * Avoid arithmetic overflow there as well.
1678 * Overflow can happen only if HZ < USER_HZ.
1679 */
1680 if (HZ < USER_HZ && rt_expires > 0x7FFFFFFF / USER_HZ)
1681 rt_expires = 0x7FFFFFFF / USER_HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682
1683 if (pinfo->onlink) {
1684 struct rt6_info *rt;
1685 rt = rt6_lookup(&pinfo->prefix, NULL, dev->ifindex, 1);
1686
1687 if (rt && ((rt->rt6i_flags & (RTF_GATEWAY | RTF_DEFAULT)) == 0)) {
1688 if (rt->rt6i_flags&RTF_EXPIRES) {
1689 if (valid_lft == 0) {
Thomas Grafe0a1ad732006-08-22 00:00:21 -07001690 ip6_del_rt(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 rt = NULL;
1692 } else {
YOSHIFUJI Hideaki3dd4bc62005-12-19 14:02:45 -08001693 rt->rt6i_expires = jiffies + rt_expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 }
1695 }
1696 } else if (valid_lft) {
1697 addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
YOSHIFUJI Hideaki3dd4bc62005-12-19 14:02:45 -08001698 dev, jiffies_to_clock_t(rt_expires), RTF_ADDRCONF|RTF_EXPIRES|RTF_PREFIX_RT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 }
1700 if (rt)
1701 dst_release(&rt->u.dst);
1702 }
1703
1704 /* Try to figure out our local address for this prefix */
1705
1706 if (pinfo->autoconf && in6_dev->cnf.autoconf) {
1707 struct inet6_ifaddr * ifp;
1708 struct in6_addr addr;
1709 int create = 0, update_lft = 0;
1710
1711 if (pinfo->prefix_len == 64) {
1712 memcpy(&addr, &pinfo->prefix, 8);
1713 if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
1714 ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
1715 in6_dev_put(in6_dev);
1716 return;
1717 }
1718 goto ok;
1719 }
1720 if (net_ratelimit())
1721 printk(KERN_DEBUG "IPv6 addrconf: prefix with wrong length %d\n",
1722 pinfo->prefix_len);
1723 in6_dev_put(in6_dev);
1724 return;
1725
1726ok:
1727
1728 ifp = ipv6_get_ifaddr(&addr, dev, 1);
1729
1730 if (ifp == NULL && valid_lft) {
1731 int max_addresses = in6_dev->cnf.max_addresses;
1732
1733 /* Do not allow to create too much of autoconfigured
1734 * addresses; this would be too easy way to crash kernel.
1735 */
1736 if (!max_addresses ||
1737 ipv6_count_addresses(in6_dev) < max_addresses)
1738 ifp = ipv6_add_addr(in6_dev, &addr, pinfo->prefix_len,
1739 addr_type&IPV6_ADDR_SCOPE_MASK, 0);
1740
1741 if (!ifp || IS_ERR(ifp)) {
1742 in6_dev_put(in6_dev);
1743 return;
1744 }
1745
1746 update_lft = create = 1;
1747 ifp->cstamp = jiffies;
1748 addrconf_dad_start(ifp, RTF_ADDRCONF|RTF_PREFIX_RT);
1749 }
1750
1751 if (ifp) {
1752 int flags;
1753 unsigned long now;
1754#ifdef CONFIG_IPV6_PRIVACY
1755 struct inet6_ifaddr *ift;
1756#endif
1757 u32 stored_lft;
1758
1759 /* update lifetime (RFC2462 5.5.3 e) */
1760 spin_lock(&ifp->lock);
1761 now = jiffies;
1762 if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
1763 stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
1764 else
1765 stored_lft = 0;
1766 if (!update_lft && stored_lft) {
1767 if (valid_lft > MIN_VALID_LIFETIME ||
1768 valid_lft > stored_lft)
1769 update_lft = 1;
1770 else if (stored_lft <= MIN_VALID_LIFETIME) {
1771 /* valid_lft <= stored_lft is always true */
1772 /* XXX: IPsec */
1773 update_lft = 0;
1774 } else {
1775 valid_lft = MIN_VALID_LIFETIME;
1776 if (valid_lft < prefered_lft)
1777 prefered_lft = valid_lft;
1778 update_lft = 1;
1779 }
1780 }
1781
1782 if (update_lft) {
1783 ifp->valid_lft = valid_lft;
1784 ifp->prefered_lft = prefered_lft;
1785 ifp->tstamp = now;
1786 flags = ifp->flags;
1787 ifp->flags &= ~IFA_F_DEPRECATED;
1788 spin_unlock(&ifp->lock);
1789
1790 if (!(flags&IFA_F_TENTATIVE))
1791 ipv6_ifa_notify(0, ifp);
1792 } else
1793 spin_unlock(&ifp->lock);
1794
1795#ifdef CONFIG_IPV6_PRIVACY
1796 read_lock_bh(&in6_dev->lock);
1797 /* update all temporary addresses in the list */
1798 for (ift=in6_dev->tempaddr_list; ift; ift=ift->tmp_next) {
1799 /*
1800 * When adjusting the lifetimes of an existing
1801 * temporary address, only lower the lifetimes.
1802 * Implementations must not increase the
1803 * lifetimes of an existing temporary address
1804 * when processing a Prefix Information Option.
1805 */
1806 spin_lock(&ift->lock);
1807 flags = ift->flags;
1808 if (ift->valid_lft > valid_lft &&
1809 ift->valid_lft - valid_lft > (jiffies - ift->tstamp) / HZ)
1810 ift->valid_lft = valid_lft + (jiffies - ift->tstamp) / HZ;
1811 if (ift->prefered_lft > prefered_lft &&
1812 ift->prefered_lft - prefered_lft > (jiffies - ift->tstamp) / HZ)
1813 ift->prefered_lft = prefered_lft + (jiffies - ift->tstamp) / HZ;
1814 spin_unlock(&ift->lock);
1815 if (!(flags&IFA_F_TENTATIVE))
1816 ipv6_ifa_notify(0, ift);
1817 }
1818
1819 if (create && in6_dev->cnf.use_tempaddr > 0) {
1820 /*
1821 * When a new public address is created as described in [ADDRCONF],
1822 * also create a new temporary address.
1823 */
1824 read_unlock_bh(&in6_dev->lock);
1825 ipv6_create_tempaddr(ifp, NULL);
1826 } else {
1827 read_unlock_bh(&in6_dev->lock);
1828 }
1829#endif
1830 in6_ifa_put(ifp);
1831 addrconf_verify(0);
1832 }
1833 }
1834 inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
1835 in6_dev_put(in6_dev);
1836}
1837
1838/*
1839 * Set destination address.
1840 * Special case for SIT interfaces where we create a new "virtual"
1841 * device.
1842 */
1843int addrconf_set_dstaddr(void __user *arg)
1844{
1845 struct in6_ifreq ireq;
1846 struct net_device *dev;
1847 int err = -EINVAL;
1848
1849 rtnl_lock();
1850
1851 err = -EFAULT;
1852 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
1853 goto err_exit;
1854
1855 dev = __dev_get_by_index(ireq.ifr6_ifindex);
1856
1857 err = -ENODEV;
1858 if (dev == NULL)
1859 goto err_exit;
1860
Joerg Roedel0be669b2006-10-10 14:49:53 -07001861#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 if (dev->type == ARPHRD_SIT) {
1863 struct ifreq ifr;
1864 mm_segment_t oldfs;
1865 struct ip_tunnel_parm p;
1866
1867 err = -EADDRNOTAVAIL;
1868 if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4))
1869 goto err_exit;
1870
1871 memset(&p, 0, sizeof(p));
1872 p.iph.daddr = ireq.ifr6_addr.s6_addr32[3];
1873 p.iph.saddr = 0;
1874 p.iph.version = 4;
1875 p.iph.ihl = 5;
1876 p.iph.protocol = IPPROTO_IPV6;
1877 p.iph.ttl = 64;
1878 ifr.ifr_ifru.ifru_data = (void __user *)&p;
1879
1880 oldfs = get_fs(); set_fs(KERNEL_DS);
1881 err = dev->do_ioctl(dev, &ifr, SIOCADDTUNNEL);
1882 set_fs(oldfs);
1883
1884 if (err == 0) {
1885 err = -ENOBUFS;
1886 if ((dev = __dev_get_by_name(p.name)) == NULL)
1887 goto err_exit;
1888 err = dev_open(dev);
1889 }
1890 }
Joerg Roedel0be669b2006-10-10 14:49:53 -07001891#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892
1893err_exit:
1894 rtnl_unlock();
1895 return err;
1896}
1897
1898/*
1899 * Manual configuration of address on an interface
1900 */
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09001901static int inet6_addr_add(int ifindex, struct in6_addr *pfx, int plen,
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07001902 __u8 ifa_flags, __u32 prefered_lft, __u32 valid_lft)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903{
1904 struct inet6_ifaddr *ifp;
1905 struct inet6_dev *idev;
1906 struct net_device *dev;
1907 int scope;
1908
1909 ASSERT_RTNL();
1910
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09001911 /* check the lifetime */
1912 if (!valid_lft || prefered_lft > valid_lft)
1913 return -EINVAL;
1914
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 if ((dev = __dev_get_by_index(ifindex)) == NULL)
1916 return -ENODEV;
1917
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 if ((idev = addrconf_add_dev(dev)) == NULL)
1919 return -ENOBUFS;
1920
1921 scope = ipv6_addr_scope(pfx);
1922
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09001923 if (valid_lft == INFINITY_LIFE_TIME)
1924 ifa_flags |= IFA_F_PERMANENT;
1925 else if (valid_lft >= 0x7FFFFFFF/HZ)
1926 valid_lft = 0x7FFFFFFF/HZ;
1927
1928 if (prefered_lft == 0)
1929 ifa_flags |= IFA_F_DEPRECATED;
1930 else if ((prefered_lft >= 0x7FFFFFFF/HZ) &&
1931 (prefered_lft != INFINITY_LIFE_TIME))
1932 prefered_lft = 0x7FFFFFFF/HZ;
1933
1934 ifp = ipv6_add_addr(idev, pfx, plen, scope, ifa_flags);
1935
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 if (!IS_ERR(ifp)) {
Herbert Xu06aebfb2006-08-09 16:52:04 -07001937 spin_lock_bh(&ifp->lock);
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09001938 ifp->valid_lft = valid_lft;
1939 ifp->prefered_lft = prefered_lft;
1940 ifp->tstamp = jiffies;
Herbert Xu06aebfb2006-08-09 16:52:04 -07001941 spin_unlock_bh(&ifp->lock);
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09001942
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 addrconf_dad_start(ifp, 0);
1944 in6_ifa_put(ifp);
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09001945 addrconf_verify(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 return 0;
1947 }
1948
1949 return PTR_ERR(ifp);
1950}
1951
1952static int inet6_addr_del(int ifindex, struct in6_addr *pfx, int plen)
1953{
1954 struct inet6_ifaddr *ifp;
1955 struct inet6_dev *idev;
1956 struct net_device *dev;
1957
1958 if ((dev = __dev_get_by_index(ifindex)) == NULL)
1959 return -ENODEV;
1960
1961 if ((idev = __in6_dev_get(dev)) == NULL)
1962 return -ENXIO;
1963
1964 read_lock_bh(&idev->lock);
1965 for (ifp = idev->addr_list; ifp; ifp=ifp->if_next) {
1966 if (ifp->prefix_len == plen &&
1967 ipv6_addr_equal(pfx, &ifp->addr)) {
1968 in6_ifa_hold(ifp);
1969 read_unlock_bh(&idev->lock);
1970
1971 ipv6_del_addr(ifp);
1972
1973 /* If the last address is deleted administratively,
1974 disable IPv6 on this interface.
1975 */
1976 if (idev->addr_list == NULL)
1977 addrconf_ifdown(idev->dev, 1);
1978 return 0;
1979 }
1980 }
1981 read_unlock_bh(&idev->lock);
1982 return -EADDRNOTAVAIL;
1983}
1984
1985
1986int addrconf_add_ifaddr(void __user *arg)
1987{
1988 struct in6_ifreq ireq;
1989 int err;
1990
1991 if (!capable(CAP_NET_ADMIN))
1992 return -EPERM;
1993
1994 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
1995 return -EFAULT;
1996
1997 rtnl_lock();
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09001998 err = inet6_addr_add(ireq.ifr6_ifindex, &ireq.ifr6_addr, ireq.ifr6_prefixlen,
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07001999 IFA_F_PERMANENT, INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 rtnl_unlock();
2001 return err;
2002}
2003
2004int addrconf_del_ifaddr(void __user *arg)
2005{
2006 struct in6_ifreq ireq;
2007 int err;
2008
2009 if (!capable(CAP_NET_ADMIN))
2010 return -EPERM;
2011
2012 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2013 return -EFAULT;
2014
2015 rtnl_lock();
2016 err = inet6_addr_del(ireq.ifr6_ifindex, &ireq.ifr6_addr, ireq.ifr6_prefixlen);
2017 rtnl_unlock();
2018 return err;
2019}
2020
Joerg Roedel0be669b2006-10-10 14:49:53 -07002021#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022static void sit_add_v4_addrs(struct inet6_dev *idev)
2023{
2024 struct inet6_ifaddr * ifp;
2025 struct in6_addr addr;
2026 struct net_device *dev;
2027 int scope;
2028
2029 ASSERT_RTNL();
2030
2031 memset(&addr, 0, sizeof(struct in6_addr));
2032 memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
2033
2034 if (idev->dev->flags&IFF_POINTOPOINT) {
2035 addr.s6_addr32[0] = htonl(0xfe800000);
2036 scope = IFA_LINK;
2037 } else {
2038 scope = IPV6_ADDR_COMPATv4;
2039 }
2040
2041 if (addr.s6_addr32[3]) {
2042 ifp = ipv6_add_addr(idev, &addr, 128, scope, IFA_F_PERMANENT);
2043 if (!IS_ERR(ifp)) {
2044 spin_lock_bh(&ifp->lock);
2045 ifp->flags &= ~IFA_F_TENTATIVE;
2046 spin_unlock_bh(&ifp->lock);
2047 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2048 in6_ifa_put(ifp);
2049 }
2050 return;
2051 }
2052
2053 for (dev = dev_base; dev != NULL; dev = dev->next) {
Herbert Xue5ed6392005-10-03 14:35:55 -07002054 struct in_device * in_dev = __in_dev_get_rtnl(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 if (in_dev && (dev->flags & IFF_UP)) {
2056 struct in_ifaddr * ifa;
2057
2058 int flag = scope;
2059
2060 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
2061 int plen;
2062
2063 addr.s6_addr32[3] = ifa->ifa_local;
2064
2065 if (ifa->ifa_scope == RT_SCOPE_LINK)
2066 continue;
2067 if (ifa->ifa_scope >= RT_SCOPE_HOST) {
2068 if (idev->dev->flags&IFF_POINTOPOINT)
2069 continue;
2070 flag |= IFA_HOST;
2071 }
2072 if (idev->dev->flags&IFF_POINTOPOINT)
2073 plen = 64;
2074 else
2075 plen = 96;
2076
2077 ifp = ipv6_add_addr(idev, &addr, plen, flag,
2078 IFA_F_PERMANENT);
2079 if (!IS_ERR(ifp)) {
2080 spin_lock_bh(&ifp->lock);
2081 ifp->flags &= ~IFA_F_TENTATIVE;
2082 spin_unlock_bh(&ifp->lock);
2083 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2084 in6_ifa_put(ifp);
2085 }
2086 }
2087 }
2088 }
2089}
Joerg Roedel0be669b2006-10-10 14:49:53 -07002090#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091
2092static void init_loopback(struct net_device *dev)
2093{
2094 struct inet6_dev *idev;
2095 struct inet6_ifaddr * ifp;
2096
2097 /* ::1 */
2098
2099 ASSERT_RTNL();
2100
2101 if ((idev = ipv6_find_idev(dev)) == NULL) {
2102 printk(KERN_DEBUG "init loopback: add_dev failed\n");
2103 return;
2104 }
2105
2106 ifp = ipv6_add_addr(idev, &in6addr_loopback, 128, IFA_HOST, IFA_F_PERMANENT);
2107 if (!IS_ERR(ifp)) {
2108 spin_lock_bh(&ifp->lock);
2109 ifp->flags &= ~IFA_F_TENTATIVE;
2110 spin_unlock_bh(&ifp->lock);
2111 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2112 in6_ifa_put(ifp);
2113 }
2114}
2115
2116static void addrconf_add_linklocal(struct inet6_dev *idev, struct in6_addr *addr)
2117{
2118 struct inet6_ifaddr * ifp;
2119
2120 ifp = ipv6_add_addr(idev, addr, 64, IFA_LINK, IFA_F_PERMANENT);
2121 if (!IS_ERR(ifp)) {
2122 addrconf_dad_start(ifp, 0);
2123 in6_ifa_put(ifp);
2124 }
2125}
2126
2127static void addrconf_dev_config(struct net_device *dev)
2128{
2129 struct in6_addr addr;
2130 struct inet6_dev * idev;
2131
2132 ASSERT_RTNL();
2133
2134 if ((dev->type != ARPHRD_ETHER) &&
2135 (dev->type != ARPHRD_FDDI) &&
2136 (dev->type != ARPHRD_IEEE802_TR) &&
2137 (dev->type != ARPHRD_ARCNET) &&
2138 (dev->type != ARPHRD_INFINIBAND)) {
2139 /* Alas, we support only Ethernet autoconfiguration. */
2140 return;
2141 }
2142
2143 idev = addrconf_add_dev(dev);
2144 if (idev == NULL)
2145 return;
2146
2147 memset(&addr, 0, sizeof(struct in6_addr));
2148 addr.s6_addr32[0] = htonl(0xFE800000);
2149
2150 if (ipv6_generate_eui64(addr.s6_addr + 8, dev) == 0)
2151 addrconf_add_linklocal(idev, &addr);
2152}
2153
Joerg Roedel0be669b2006-10-10 14:49:53 -07002154#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155static void addrconf_sit_config(struct net_device *dev)
2156{
2157 struct inet6_dev *idev;
2158
2159 ASSERT_RTNL();
2160
2161 /*
2162 * Configure the tunnel with one of our IPv4
2163 * addresses... we should configure all of
2164 * our v4 addrs in the tunnel
2165 */
2166
2167 if ((idev = ipv6_find_idev(dev)) == NULL) {
2168 printk(KERN_DEBUG "init sit: add_dev failed\n");
2169 return;
2170 }
2171
2172 sit_add_v4_addrs(idev);
2173
2174 if (dev->flags&IFF_POINTOPOINT) {
2175 addrconf_add_mroute(dev);
2176 addrconf_add_lroute(dev);
2177 } else
2178 sit_route_add(dev);
2179}
Joerg Roedel0be669b2006-10-10 14:49:53 -07002180#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181
2182static inline int
2183ipv6_inherit_linklocal(struct inet6_dev *idev, struct net_device *link_dev)
2184{
2185 struct in6_addr lladdr;
2186
2187 if (!ipv6_get_lladdr(link_dev, &lladdr)) {
2188 addrconf_add_linklocal(idev, &lladdr);
2189 return 0;
2190 }
2191 return -1;
2192}
2193
2194static void ip6_tnl_add_linklocal(struct inet6_dev *idev)
2195{
2196 struct net_device *link_dev;
2197
2198 /* first try to inherit the link-local address from the link device */
2199 if (idev->dev->iflink &&
2200 (link_dev = __dev_get_by_index(idev->dev->iflink))) {
2201 if (!ipv6_inherit_linklocal(idev, link_dev))
2202 return;
2203 }
2204 /* then try to inherit it from any device */
2205 for (link_dev = dev_base; link_dev; link_dev = link_dev->next) {
2206 if (!ipv6_inherit_linklocal(idev, link_dev))
2207 return;
2208 }
2209 printk(KERN_DEBUG "init ip6-ip6: add_linklocal failed\n");
2210}
2211
2212/*
2213 * Autoconfigure tunnel with a link-local address so routing protocols,
2214 * DHCPv6, MLD etc. can be run over the virtual link
2215 */
2216
2217static void addrconf_ip6_tnl_config(struct net_device *dev)
2218{
2219 struct inet6_dev *idev;
2220
2221 ASSERT_RTNL();
2222
2223 if ((idev = addrconf_add_dev(dev)) == NULL) {
2224 printk(KERN_DEBUG "init ip6-ip6: add_dev failed\n");
2225 return;
2226 }
2227 ip6_tnl_add_linklocal(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228}
2229
2230static int addrconf_notify(struct notifier_block *this, unsigned long event,
2231 void * data)
2232{
2233 struct net_device *dev = (struct net_device *) data;
2234 struct inet6_dev *idev = __in6_dev_get(dev);
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09002235 int run_pending = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236
2237 switch(event) {
2238 case NETDEV_UP:
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002239 case NETDEV_CHANGE:
2240 if (event == NETDEV_UP) {
2241 if (!netif_carrier_ok(dev)) {
2242 /* device is not ready yet. */
2243 printk(KERN_INFO
2244 "ADDRCONF(NETDEV_UP): %s: "
2245 "link is not ready\n",
2246 dev->name);
2247 break;
2248 }
Kristian Slavov99081042006-02-08 16:10:53 -08002249
2250 if (idev)
2251 idev->if_flags |= IF_READY;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002252 } else {
2253 if (!netif_carrier_ok(dev)) {
2254 /* device is still not ready. */
2255 break;
2256 }
2257
2258 if (idev) {
2259 if (idev->if_flags & IF_READY) {
2260 /* device is already configured. */
2261 break;
2262 }
2263 idev->if_flags |= IF_READY;
2264 }
2265
2266 printk(KERN_INFO
2267 "ADDRCONF(NETDEV_CHANGE): %s: "
2268 "link becomes ready\n",
2269 dev->name);
2270
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09002271 run_pending = 1;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002272 }
2273
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 switch(dev->type) {
Joerg Roedel0be669b2006-10-10 14:49:53 -07002275#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 case ARPHRD_SIT:
2277 addrconf_sit_config(dev);
2278 break;
Joerg Roedel0be669b2006-10-10 14:49:53 -07002279#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 case ARPHRD_TUNNEL6:
2281 addrconf_ip6_tnl_config(dev);
2282 break;
2283 case ARPHRD_LOOPBACK:
2284 init_loopback(dev);
2285 break;
2286
2287 default:
2288 addrconf_dev_config(dev);
2289 break;
2290 };
2291 if (idev) {
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09002292 if (run_pending)
2293 addrconf_dad_run(idev);
2294
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 /* If the MTU changed during the interface down, when the
2296 interface up, the changed MTU must be reflected in the
2297 idev as well as routers.
2298 */
2299 if (idev->cnf.mtu6 != dev->mtu && dev->mtu >= IPV6_MIN_MTU) {
2300 rt6_mtu_change(dev, dev->mtu);
2301 idev->cnf.mtu6 = dev->mtu;
2302 }
2303 idev->tstamp = jiffies;
2304 inet6_ifinfo_notify(RTM_NEWLINK, idev);
2305 /* If the changed mtu during down is lower than IPV6_MIN_MTU
2306 stop IPv6 on this interface.
2307 */
2308 if (dev->mtu < IPV6_MIN_MTU)
2309 addrconf_ifdown(dev, event != NETDEV_DOWN);
2310 }
2311 break;
2312
2313 case NETDEV_CHANGEMTU:
2314 if ( idev && dev->mtu >= IPV6_MIN_MTU) {
2315 rt6_mtu_change(dev, dev->mtu);
2316 idev->cnf.mtu6 = dev->mtu;
2317 break;
2318 }
2319
2320 /* MTU falled under IPV6_MIN_MTU. Stop IPv6 on this interface. */
2321
2322 case NETDEV_DOWN:
2323 case NETDEV_UNREGISTER:
2324 /*
2325 * Remove all addresses from this interface.
2326 */
2327 addrconf_ifdown(dev, event != NETDEV_DOWN);
2328 break;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002329
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 case NETDEV_CHANGENAME:
2331#ifdef CONFIG_SYSCTL
2332 if (idev) {
2333 addrconf_sysctl_unregister(&idev->cnf);
2334 neigh_sysctl_unregister(idev->nd_parms);
2335 neigh_sysctl_register(dev, idev->nd_parms,
2336 NET_IPV6, NET_IPV6_NEIGH, "ipv6",
2337 &ndisc_ifinfo_sysctl_change,
2338 NULL);
2339 addrconf_sysctl_register(idev, &idev->cnf);
2340 }
2341#endif
2342 break;
2343 };
2344
2345 return NOTIFY_OK;
2346}
2347
2348/*
2349 * addrconf module should be notified of a device going up
2350 */
2351static struct notifier_block ipv6_dev_notf = {
2352 .notifier_call = addrconf_notify,
2353 .priority = 0
2354};
2355
2356static int addrconf_ifdown(struct net_device *dev, int how)
2357{
2358 struct inet6_dev *idev;
2359 struct inet6_ifaddr *ifa, **bifa;
2360 int i;
2361
2362 ASSERT_RTNL();
2363
2364 if (dev == &loopback_dev && how == 1)
2365 how = 0;
2366
2367 rt6_ifdown(dev);
2368 neigh_ifdown(&nd_tbl, dev);
2369
2370 idev = __in6_dev_get(dev);
2371 if (idev == NULL)
2372 return -ENODEV;
2373
2374 /* Step 1: remove reference to ipv6 device from parent device.
2375 Do not dev_put!
2376 */
2377 if (how == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 idev->dead = 1;
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07002379
2380 /* protected by rtnl_lock */
2381 rcu_assign_pointer(dev->ip6_ptr, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382
2383 /* Step 1.5: remove snmp6 entry */
2384 snmp6_unregister_dev(idev);
2385
2386 }
2387
2388 /* Step 2: clear hash table */
2389 for (i=0; i<IN6_ADDR_HSIZE; i++) {
2390 bifa = &inet6_addr_lst[i];
2391
2392 write_lock_bh(&addrconf_hash_lock);
2393 while ((ifa = *bifa) != NULL) {
2394 if (ifa->idev == idev) {
2395 *bifa = ifa->lst_next;
2396 ifa->lst_next = NULL;
2397 addrconf_del_timer(ifa);
2398 in6_ifa_put(ifa);
2399 continue;
2400 }
2401 bifa = &ifa->lst_next;
2402 }
2403 write_unlock_bh(&addrconf_hash_lock);
2404 }
2405
2406 write_lock_bh(&idev->lock);
2407
2408 /* Step 3: clear flags for stateless addrconf */
2409 if (how != 1)
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002410 idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411
2412 /* Step 4: clear address list */
2413#ifdef CONFIG_IPV6_PRIVACY
2414 if (how == 1 && del_timer(&idev->regen_timer))
2415 in6_dev_put(idev);
2416
2417 /* clear tempaddr list */
2418 while ((ifa = idev->tempaddr_list) != NULL) {
2419 idev->tempaddr_list = ifa->tmp_next;
2420 ifa->tmp_next = NULL;
2421 ifa->dead = 1;
2422 write_unlock_bh(&idev->lock);
2423 spin_lock_bh(&ifa->lock);
2424
2425 if (ifa->ifpub) {
2426 in6_ifa_put(ifa->ifpub);
2427 ifa->ifpub = NULL;
2428 }
2429 spin_unlock_bh(&ifa->lock);
2430 in6_ifa_put(ifa);
2431 write_lock_bh(&idev->lock);
2432 }
2433#endif
2434 while ((ifa = idev->addr_list) != NULL) {
2435 idev->addr_list = ifa->if_next;
2436 ifa->if_next = NULL;
2437 ifa->dead = 1;
2438 addrconf_del_timer(ifa);
2439 write_unlock_bh(&idev->lock);
2440
2441 __ipv6_ifa_notify(RTM_DELADDR, ifa);
2442 in6_ifa_put(ifa);
2443
2444 write_lock_bh(&idev->lock);
2445 }
2446 write_unlock_bh(&idev->lock);
2447
2448 /* Step 5: Discard multicast list */
2449
2450 if (how == 1)
2451 ipv6_mc_destroy_dev(idev);
2452 else
2453 ipv6_mc_down(idev);
2454
2455 /* Step 5: netlink notification of this interface */
2456 idev->tstamp = jiffies;
Yan Zheng979ad662005-10-14 18:31:15 +08002457 inet6_ifinfo_notify(RTM_DELLINK, idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458
2459 /* Shot the device (if unregistered) */
2460
2461 if (how == 1) {
2462#ifdef CONFIG_SYSCTL
2463 addrconf_sysctl_unregister(&idev->cnf);
2464 neigh_sysctl_unregister(idev->nd_parms);
2465#endif
2466 neigh_parms_release(&nd_tbl, idev->nd_parms);
2467 neigh_ifdown(&nd_tbl, dev);
2468 in6_dev_put(idev);
2469 }
2470 return 0;
2471}
2472
2473static void addrconf_rs_timer(unsigned long data)
2474{
2475 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
2476
2477 if (ifp->idev->cnf.forwarding)
2478 goto out;
2479
2480 if (ifp->idev->if_flags & IF_RA_RCVD) {
2481 /*
2482 * Announcement received after solicitation
2483 * was sent
2484 */
2485 goto out;
2486 }
2487
2488 spin_lock(&ifp->lock);
2489 if (ifp->probes++ < ifp->idev->cnf.rtr_solicits) {
2490 struct in6_addr all_routers;
2491
2492 /* The wait after the last probe can be shorter */
2493 addrconf_mod_timer(ifp, AC_RS,
2494 (ifp->probes == ifp->idev->cnf.rtr_solicits) ?
2495 ifp->idev->cnf.rtr_solicit_delay :
2496 ifp->idev->cnf.rtr_solicit_interval);
2497 spin_unlock(&ifp->lock);
2498
2499 ipv6_addr_all_routers(&all_routers);
2500
2501 ndisc_send_rs(ifp->idev->dev, &ifp->addr, &all_routers);
2502 } else {
2503 spin_unlock(&ifp->lock);
2504 /*
2505 * Note: we do not support deprecated "all on-link"
2506 * assumption any longer.
2507 */
2508 printk(KERN_DEBUG "%s: no IPv6 routers present\n",
2509 ifp->idev->dev->name);
2510 }
2511
2512out:
2513 in6_ifa_put(ifp);
2514}
2515
2516/*
2517 * Duplicate Address Detection
2518 */
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002519static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
2520{
2521 unsigned long rand_num;
2522 struct inet6_dev *idev = ifp->idev;
2523
2524 rand_num = net_random() % (idev->cnf.rtr_solicit_delay ? : 1);
2525 ifp->probes = idev->cnf.dad_transmits;
2526 addrconf_mod_timer(ifp, AC_DAD, rand_num);
2527}
2528
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -07002529static void addrconf_dad_start(struct inet6_ifaddr *ifp, u32 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530{
2531 struct inet6_dev *idev = ifp->idev;
2532 struct net_device *dev = idev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533
2534 addrconf_join_solict(dev, &ifp->addr);
2535
2536 if (ifp->prefix_len != 128 && (ifp->flags&IFA_F_PERMANENT))
2537 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev, 0,
2538 flags);
2539
2540 net_srandom(ifp->addr.s6_addr32[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541
2542 read_lock_bh(&idev->lock);
2543 if (ifp->dead)
2544 goto out;
2545 spin_lock_bh(&ifp->lock);
2546
2547 if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07002548 !(ifp->flags&IFA_F_TENTATIVE) ||
2549 ifp->flags & IFA_F_NODAD) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 ifp->flags &= ~IFA_F_TENTATIVE;
2551 spin_unlock_bh(&ifp->lock);
2552 read_unlock_bh(&idev->lock);
2553
2554 addrconf_dad_completed(ifp);
2555 return;
2556 }
2557
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08002558 if (!(idev->if_flags & IF_READY)) {
YOSHIFUJI Hideaki3dd3bf82005-12-23 11:23:21 -08002559 spin_unlock_bh(&ifp->lock);
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08002560 read_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002561 /*
2562 * If the defice is not ready:
2563 * - keep it tentative if it is a permanent address.
2564 * - otherwise, kill it.
2565 */
2566 in6_ifa_hold(ifp);
2567 addrconf_dad_stop(ifp);
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08002568 return;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002569 }
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08002570 addrconf_dad_kick(ifp);
2571 spin_unlock_bh(&ifp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572out:
2573 read_unlock_bh(&idev->lock);
2574}
2575
2576static void addrconf_dad_timer(unsigned long data)
2577{
2578 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
2579 struct inet6_dev *idev = ifp->idev;
2580 struct in6_addr unspec;
2581 struct in6_addr mcaddr;
2582
2583 read_lock_bh(&idev->lock);
2584 if (idev->dead) {
2585 read_unlock_bh(&idev->lock);
2586 goto out;
2587 }
2588 spin_lock_bh(&ifp->lock);
2589 if (ifp->probes == 0) {
2590 /*
2591 * DAD was successful
2592 */
2593
2594 ifp->flags &= ~IFA_F_TENTATIVE;
2595 spin_unlock_bh(&ifp->lock);
2596 read_unlock_bh(&idev->lock);
2597
2598 addrconf_dad_completed(ifp);
2599
2600 goto out;
2601 }
2602
2603 ifp->probes--;
2604 addrconf_mod_timer(ifp, AC_DAD, ifp->idev->nd_parms->retrans_time);
2605 spin_unlock_bh(&ifp->lock);
2606 read_unlock_bh(&idev->lock);
2607
2608 /* send a neighbour solicitation for our addr */
2609 memset(&unspec, 0, sizeof(unspec));
2610 addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
2611 ndisc_send_ns(ifp->idev->dev, NULL, &ifp->addr, &mcaddr, &unspec);
2612out:
2613 in6_ifa_put(ifp);
2614}
2615
2616static void addrconf_dad_completed(struct inet6_ifaddr *ifp)
2617{
2618 struct net_device * dev = ifp->idev->dev;
2619
2620 /*
2621 * Configure the address for reception. Now it is valid.
2622 */
2623
2624 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2625
2626 /* If added prefix is link local and forwarding is off,
2627 start sending router solicitations.
2628 */
2629
2630 if (ifp->idev->cnf.forwarding == 0 &&
2631 ifp->idev->cnf.rtr_solicits > 0 &&
2632 (dev->flags&IFF_LOOPBACK) == 0 &&
2633 (ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL)) {
2634 struct in6_addr all_routers;
2635
2636 ipv6_addr_all_routers(&all_routers);
2637
2638 /*
2639 * If a host as already performed a random delay
2640 * [...] as part of DAD [...] there is no need
2641 * to delay again before sending the first RS
2642 */
2643 ndisc_send_rs(ifp->idev->dev, &ifp->addr, &all_routers);
2644
2645 spin_lock_bh(&ifp->lock);
2646 ifp->probes = 1;
2647 ifp->idev->if_flags |= IF_RS_SENT;
2648 addrconf_mod_timer(ifp, AC_RS, ifp->idev->cnf.rtr_solicit_interval);
2649 spin_unlock_bh(&ifp->lock);
2650 }
2651}
2652
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09002653static void addrconf_dad_run(struct inet6_dev *idev) {
2654 struct inet6_ifaddr *ifp;
2655
2656 read_lock_bh(&idev->lock);
2657 for (ifp = idev->addr_list; ifp; ifp = ifp->if_next) {
2658 spin_lock_bh(&ifp->lock);
2659 if (!(ifp->flags & IFA_F_TENTATIVE)) {
2660 spin_unlock_bh(&ifp->lock);
2661 continue;
2662 }
2663 spin_unlock_bh(&ifp->lock);
2664 addrconf_dad_kick(ifp);
2665 }
2666 read_unlock_bh(&idev->lock);
2667}
2668
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669#ifdef CONFIG_PROC_FS
2670struct if6_iter_state {
2671 int bucket;
2672};
2673
2674static struct inet6_ifaddr *if6_get_first(struct seq_file *seq)
2675{
2676 struct inet6_ifaddr *ifa = NULL;
2677 struct if6_iter_state *state = seq->private;
2678
2679 for (state->bucket = 0; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
2680 ifa = inet6_addr_lst[state->bucket];
2681 if (ifa)
2682 break;
2683 }
2684 return ifa;
2685}
2686
2687static struct inet6_ifaddr *if6_get_next(struct seq_file *seq, struct inet6_ifaddr *ifa)
2688{
2689 struct if6_iter_state *state = seq->private;
2690
2691 ifa = ifa->lst_next;
2692try_again:
2693 if (!ifa && ++state->bucket < IN6_ADDR_HSIZE) {
2694 ifa = inet6_addr_lst[state->bucket];
2695 goto try_again;
2696 }
2697 return ifa;
2698}
2699
2700static struct inet6_ifaddr *if6_get_idx(struct seq_file *seq, loff_t pos)
2701{
2702 struct inet6_ifaddr *ifa = if6_get_first(seq);
2703
2704 if (ifa)
2705 while(pos && (ifa = if6_get_next(seq, ifa)) != NULL)
2706 --pos;
2707 return pos ? NULL : ifa;
2708}
2709
2710static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
2711{
2712 read_lock_bh(&addrconf_hash_lock);
2713 return if6_get_idx(seq, *pos);
2714}
2715
2716static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2717{
2718 struct inet6_ifaddr *ifa;
2719
2720 ifa = if6_get_next(seq, v);
2721 ++*pos;
2722 return ifa;
2723}
2724
2725static void if6_seq_stop(struct seq_file *seq, void *v)
2726{
2727 read_unlock_bh(&addrconf_hash_lock);
2728}
2729
2730static int if6_seq_show(struct seq_file *seq, void *v)
2731{
2732 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
2733 seq_printf(seq,
YOSHIFUJI Hideaki9343e792006-01-17 02:10:53 -08002734 NIP6_SEQFMT " %02x %02x %02x %02x %8s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 NIP6(ifp->addr),
2736 ifp->idev->dev->ifindex,
2737 ifp->prefix_len,
2738 ifp->scope,
2739 ifp->flags,
2740 ifp->idev->dev->name);
2741 return 0;
2742}
2743
2744static struct seq_operations if6_seq_ops = {
2745 .start = if6_seq_start,
2746 .next = if6_seq_next,
2747 .show = if6_seq_show,
2748 .stop = if6_seq_stop,
2749};
2750
2751static int if6_seq_open(struct inode *inode, struct file *file)
2752{
2753 struct seq_file *seq;
2754 int rc = -ENOMEM;
Ingo Oeser322f74a2006-03-20 23:01:47 -08002755 struct if6_iter_state *s = kzalloc(sizeof(*s), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756
2757 if (!s)
2758 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759
2760 rc = seq_open(file, &if6_seq_ops);
2761 if (rc)
2762 goto out_kfree;
2763
2764 seq = file->private_data;
2765 seq->private = s;
2766out:
2767 return rc;
2768out_kfree:
2769 kfree(s);
2770 goto out;
2771}
2772
2773static struct file_operations if6_fops = {
2774 .owner = THIS_MODULE,
2775 .open = if6_seq_open,
2776 .read = seq_read,
2777 .llseek = seq_lseek,
2778 .release = seq_release_private,
2779};
2780
2781int __init if6_proc_init(void)
2782{
2783 if (!proc_net_fops_create("if_inet6", S_IRUGO, &if6_fops))
2784 return -ENOMEM;
2785 return 0;
2786}
2787
2788void if6_proc_exit(void)
2789{
2790 proc_net_remove("if_inet6");
2791}
2792#endif /* CONFIG_PROC_FS */
2793
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07002794#ifdef CONFIG_IPV6_MIP6
2795/* Check if address is a home address configured on any interface. */
2796int ipv6_chk_home_addr(struct in6_addr *addr)
2797{
2798 int ret = 0;
2799 struct inet6_ifaddr * ifp;
2800 u8 hash = ipv6_addr_hash(addr);
2801 read_lock_bh(&addrconf_hash_lock);
2802 for (ifp = inet6_addr_lst[hash]; ifp; ifp = ifp->lst_next) {
2803 if (ipv6_addr_cmp(&ifp->addr, addr) == 0 &&
2804 (ifp->flags & IFA_F_HOMEADDRESS)) {
2805 ret = 1;
2806 break;
2807 }
2808 }
2809 read_unlock_bh(&addrconf_hash_lock);
2810 return ret;
2811}
2812#endif
2813
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814/*
2815 * Periodic address status verification
2816 */
2817
2818static void addrconf_verify(unsigned long foo)
2819{
2820 struct inet6_ifaddr *ifp;
2821 unsigned long now, next;
2822 int i;
2823
2824 spin_lock_bh(&addrconf_verify_lock);
2825 now = jiffies;
2826 next = now + ADDR_CHECK_FREQUENCY;
2827
2828 del_timer(&addr_chk_timer);
2829
2830 for (i=0; i < IN6_ADDR_HSIZE; i++) {
2831
2832restart:
Yan Zheng5d5780d2005-11-20 13:42:20 -08002833 read_lock(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 for (ifp=inet6_addr_lst[i]; ifp; ifp=ifp->lst_next) {
2835 unsigned long age;
2836#ifdef CONFIG_IPV6_PRIVACY
2837 unsigned long regen_advance;
2838#endif
2839
2840 if (ifp->flags & IFA_F_PERMANENT)
2841 continue;
2842
2843 spin_lock(&ifp->lock);
2844 age = (now - ifp->tstamp) / HZ;
2845
2846#ifdef CONFIG_IPV6_PRIVACY
2847 regen_advance = ifp->idev->cnf.regen_max_retry *
2848 ifp->idev->cnf.dad_transmits *
2849 ifp->idev->nd_parms->retrans_time / HZ;
2850#endif
2851
YOSHIFUJI Hideaki8f27ebb2006-07-28 18:12:11 +09002852 if (ifp->valid_lft != INFINITY_LIFE_TIME &&
2853 age >= ifp->valid_lft) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 spin_unlock(&ifp->lock);
2855 in6_ifa_hold(ifp);
Yan Zheng5d5780d2005-11-20 13:42:20 -08002856 read_unlock(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 ipv6_del_addr(ifp);
2858 goto restart;
YOSHIFUJI Hideaki8f27ebb2006-07-28 18:12:11 +09002859 } else if (ifp->prefered_lft == INFINITY_LIFE_TIME) {
2860 spin_unlock(&ifp->lock);
2861 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 } else if (age >= ifp->prefered_lft) {
2863 /* jiffies - ifp->tsamp > age >= ifp->prefered_lft */
2864 int deprecate = 0;
2865
2866 if (!(ifp->flags&IFA_F_DEPRECATED)) {
2867 deprecate = 1;
2868 ifp->flags |= IFA_F_DEPRECATED;
2869 }
2870
2871 if (time_before(ifp->tstamp + ifp->valid_lft * HZ, next))
2872 next = ifp->tstamp + ifp->valid_lft * HZ;
2873
2874 spin_unlock(&ifp->lock);
2875
2876 if (deprecate) {
2877 in6_ifa_hold(ifp);
Yan Zheng5d5780d2005-11-20 13:42:20 -08002878 read_unlock(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879
2880 ipv6_ifa_notify(0, ifp);
2881 in6_ifa_put(ifp);
2882 goto restart;
2883 }
2884#ifdef CONFIG_IPV6_PRIVACY
2885 } else if ((ifp->flags&IFA_F_TEMPORARY) &&
2886 !(ifp->flags&IFA_F_TENTATIVE)) {
2887 if (age >= ifp->prefered_lft - regen_advance) {
2888 struct inet6_ifaddr *ifpub = ifp->ifpub;
2889 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
2890 next = ifp->tstamp + ifp->prefered_lft * HZ;
2891 if (!ifp->regen_count && ifpub) {
2892 ifp->regen_count++;
2893 in6_ifa_hold(ifp);
2894 in6_ifa_hold(ifpub);
2895 spin_unlock(&ifp->lock);
Yan Zheng5d5780d2005-11-20 13:42:20 -08002896 read_unlock(&addrconf_hash_lock);
Hiroyuki YAMAMORI291d8092005-12-23 11:24:05 -08002897 spin_lock(&ifpub->lock);
2898 ifpub->regen_count = 0;
2899 spin_unlock(&ifpub->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 ipv6_create_tempaddr(ifpub, ifp);
2901 in6_ifa_put(ifpub);
2902 in6_ifa_put(ifp);
2903 goto restart;
2904 }
2905 } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
2906 next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
2907 spin_unlock(&ifp->lock);
2908#endif
2909 } else {
2910 /* ifp->prefered_lft <= ifp->valid_lft */
2911 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
2912 next = ifp->tstamp + ifp->prefered_lft * HZ;
2913 spin_unlock(&ifp->lock);
2914 }
2915 }
Yan Zheng5d5780d2005-11-20 13:42:20 -08002916 read_unlock(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 }
2918
2919 addr_chk_timer.expires = time_before(next, jiffies + HZ) ? jiffies + HZ : next;
2920 add_timer(&addr_chk_timer);
2921 spin_unlock_bh(&addrconf_verify_lock);
2922}
2923
Thomas Graf461d8832006-09-18 00:09:49 -07002924static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local)
2925{
2926 struct in6_addr *pfx = NULL;
2927
2928 if (addr)
2929 pfx = nla_data(addr);
2930
2931 if (local) {
2932 if (pfx && nla_memcmp(local, pfx, sizeof(*pfx)))
2933 pfx = NULL;
2934 else
2935 pfx = nla_data(local);
2936 }
2937
2938 return pfx;
2939}
2940
2941static struct nla_policy ifa_ipv6_policy[IFA_MAX+1] __read_mostly = {
2942 [IFA_ADDRESS] = { .len = sizeof(struct in6_addr) },
2943 [IFA_LOCAL] = { .len = sizeof(struct in6_addr) },
2944 [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) },
2945};
2946
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947static int
2948inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
2949{
Thomas Grafb933f712006-09-18 00:10:19 -07002950 struct ifaddrmsg *ifm;
2951 struct nlattr *tb[IFA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 struct in6_addr *pfx;
Thomas Grafb933f712006-09-18 00:10:19 -07002953 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954
Thomas Grafb933f712006-09-18 00:10:19 -07002955 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
2956 if (err < 0)
2957 return err;
2958
2959 ifm = nlmsg_data(nlh);
2960 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 if (pfx == NULL)
2962 return -EINVAL;
2963
2964 return inet6_addr_del(ifm->ifa_index, pfx, ifm->ifa_prefixlen);
2965}
2966
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07002967static int inet6_addr_modify(struct inet6_ifaddr *ifp, u8 ifa_flags,
2968 u32 prefered_lft, u32 valid_lft)
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09002969{
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09002970 if (!valid_lft || (prefered_lft > valid_lft))
2971 return -EINVAL;
2972
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09002973 if (valid_lft == INFINITY_LIFE_TIME)
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07002974 ifa_flags |= IFA_F_PERMANENT;
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09002975 else if (valid_lft >= 0x7FFFFFFF/HZ)
2976 valid_lft = 0x7FFFFFFF/HZ;
2977
2978 if (prefered_lft == 0)
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07002979 ifa_flags |= IFA_F_DEPRECATED;
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09002980 else if ((prefered_lft >= 0x7FFFFFFF/HZ) &&
2981 (prefered_lft != INFINITY_LIFE_TIME))
2982 prefered_lft = 0x7FFFFFFF/HZ;
2983
2984 spin_lock_bh(&ifp->lock);
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07002985 ifp->flags = (ifp->flags & ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD | IFA_F_HOMEADDRESS)) | ifa_flags;
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09002986 ifp->tstamp = jiffies;
2987 ifp->valid_lft = valid_lft;
2988 ifp->prefered_lft = prefered_lft;
2989
2990 spin_unlock_bh(&ifp->lock);
2991 if (!(ifp->flags&IFA_F_TENTATIVE))
2992 ipv6_ifa_notify(0, ifp);
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09002993
2994 addrconf_verify(0);
2995
2996 return 0;
2997}
2998
2999static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
3001{
Thomas Graf461d8832006-09-18 00:09:49 -07003002 struct ifaddrmsg *ifm;
3003 struct nlattr *tb[IFA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 struct in6_addr *pfx;
Thomas Graf7198f8c2006-09-18 00:13:46 -07003005 struct inet6_ifaddr *ifa;
3006 struct net_device *dev;
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003007 u32 valid_lft = INFINITY_LIFE_TIME, preferred_lft = INFINITY_LIFE_TIME;
3008 u8 ifa_flags;
Thomas Graf461d8832006-09-18 00:09:49 -07003009 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010
Thomas Graf461d8832006-09-18 00:09:49 -07003011 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
3012 if (err < 0)
3013 return err;
3014
3015 ifm = nlmsg_data(nlh);
3016 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 if (pfx == NULL)
3018 return -EINVAL;
3019
Thomas Graf461d8832006-09-18 00:09:49 -07003020 if (tb[IFA_CACHEINFO]) {
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09003021 struct ifa_cacheinfo *ci;
Thomas Graf461d8832006-09-18 00:09:49 -07003022
3023 ci = nla_data(tb[IFA_CACHEINFO]);
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09003024 valid_lft = ci->ifa_valid;
Thomas Graf461d8832006-09-18 00:09:49 -07003025 preferred_lft = ci->ifa_prefered;
3026 } else {
3027 preferred_lft = INFINITY_LIFE_TIME;
3028 valid_lft = INFINITY_LIFE_TIME;
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09003029 }
3030
Thomas Graf7198f8c2006-09-18 00:13:46 -07003031 dev = __dev_get_by_index(ifm->ifa_index);
3032 if (dev == NULL)
3033 return -ENODEV;
3034
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003035 /* We ignore other flags so far. */
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07003036 ifa_flags = ifm->ifa_flags & (IFA_F_NODAD | IFA_F_HOMEADDRESS);
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003037
Thomas Graf7198f8c2006-09-18 00:13:46 -07003038 ifa = ipv6_get_ifaddr(pfx, dev, 1);
3039 if (ifa == NULL) {
3040 /*
3041 * It would be best to check for !NLM_F_CREATE here but
3042 * userspace alreay relies on not having to provide this.
3043 */
3044 return inet6_addr_add(ifm->ifa_index, pfx, ifm->ifa_prefixlen,
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003045 ifa_flags, preferred_lft, valid_lft);
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09003046 }
3047
Thomas Graf7198f8c2006-09-18 00:13:46 -07003048 if (nlh->nlmsg_flags & NLM_F_EXCL ||
3049 !(nlh->nlmsg_flags & NLM_F_REPLACE))
3050 err = -EEXIST;
3051 else
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003052 err = inet6_addr_modify(ifa, ifa_flags, preferred_lft, valid_lft);
Thomas Graf7198f8c2006-09-18 00:13:46 -07003053
3054 in6_ifa_put(ifa);
3055
3056 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057}
3058
Thomas Graf101bb222006-09-18 00:11:52 -07003059static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u8 flags,
3060 u8 scope, int ifindex)
3061{
3062 struct ifaddrmsg *ifm;
3063
3064 ifm = nlmsg_data(nlh);
3065 ifm->ifa_family = AF_INET6;
3066 ifm->ifa_prefixlen = prefixlen;
3067 ifm->ifa_flags = flags;
3068 ifm->ifa_scope = scope;
3069 ifm->ifa_index = ifindex;
3070}
3071
Thomas Graf85486af2006-09-18 00:11:24 -07003072static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
3073 unsigned long tstamp, u32 preferred, u32 valid)
3074{
3075 struct ifa_cacheinfo ci;
3076
3077 ci.cstamp = (u32)(TIME_DELTA(cstamp, INITIAL_JIFFIES) / HZ * 100
3078 + TIME_DELTA(cstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
3079 ci.tstamp = (u32)(TIME_DELTA(tstamp, INITIAL_JIFFIES) / HZ * 100
3080 + TIME_DELTA(tstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
3081 ci.ifa_prefered = preferred;
3082 ci.ifa_valid = valid;
3083
3084 return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
3085}
3086
Thomas Graf101bb222006-09-18 00:11:52 -07003087static inline int rt_scope(int ifa_scope)
3088{
3089 if (ifa_scope & IFA_HOST)
3090 return RT_SCOPE_HOST;
3091 else if (ifa_scope & IFA_LINK)
3092 return RT_SCOPE_LINK;
3093 else if (ifa_scope & IFA_SITE)
3094 return RT_SCOPE_SITE;
3095 else
3096 return RT_SCOPE_UNIVERSE;
3097}
3098
Thomas Graf0ab68032006-09-18 00:12:35 -07003099static inline int inet6_ifaddr_msgsize(void)
3100{
Thomas Graf339bf982006-11-10 14:10:15 -08003101 return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
3102 + nla_total_size(16) /* IFA_ADDRESS */
3103 + nla_total_size(sizeof(struct ifa_cacheinfo));
Thomas Graf0ab68032006-09-18 00:12:35 -07003104}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07003105
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07003107 u32 pid, u32 seq, int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 struct nlmsghdr *nlh;
Thomas Graf85486af2006-09-18 00:11:24 -07003110 u32 preferred, valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111
Thomas Graf0ab68032006-09-18 00:12:35 -07003112 nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags);
3113 if (nlh == NULL)
3114 return -ENOBUFS;
3115
Thomas Graf101bb222006-09-18 00:11:52 -07003116 put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope),
3117 ifa->idev->dev->ifindex);
3118
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 if (!(ifa->flags&IFA_F_PERMANENT)) {
Thomas Graf85486af2006-09-18 00:11:24 -07003120 preferred = ifa->prefered_lft;
3121 valid = ifa->valid_lft;
3122 if (preferred != INFINITY_LIFE_TIME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 long tval = (jiffies - ifa->tstamp)/HZ;
Thomas Graf85486af2006-09-18 00:11:24 -07003124 preferred -= tval;
3125 if (valid != INFINITY_LIFE_TIME)
3126 valid -= tval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 }
3128 } else {
Thomas Graf85486af2006-09-18 00:11:24 -07003129 preferred = INFINITY_LIFE_TIME;
3130 valid = INFINITY_LIFE_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 }
Thomas Graf85486af2006-09-18 00:11:24 -07003132
Thomas Graf0ab68032006-09-18 00:12:35 -07003133 if (nla_put(skb, IFA_ADDRESS, 16, &ifa->addr) < 0 ||
3134 put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0)
3135 return nlmsg_cancel(skb, nlh);
Thomas Graf85486af2006-09-18 00:11:24 -07003136
Thomas Graf0ab68032006-09-18 00:12:35 -07003137 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138}
3139
3140static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -07003141 u32 pid, u32 seq, int event, u16 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143 struct nlmsghdr *nlh;
Thomas Graf101bb222006-09-18 00:11:52 -07003144 u8 scope = RT_SCOPE_UNIVERSE;
3145 int ifindex = ifmca->idev->dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146
Thomas Graf101bb222006-09-18 00:11:52 -07003147 if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE)
3148 scope = RT_SCOPE_SITE;
3149
Thomas Graf0ab68032006-09-18 00:12:35 -07003150 nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags);
3151 if (nlh == NULL)
3152 return -ENOBUFS;
3153
Thomas Graf101bb222006-09-18 00:11:52 -07003154 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
Thomas Graf0ab68032006-09-18 00:12:35 -07003155 if (nla_put(skb, IFA_MULTICAST, 16, &ifmca->mca_addr) < 0 ||
3156 put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp,
Thomas Graf85486af2006-09-18 00:11:24 -07003157 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0)
Thomas Graf0ab68032006-09-18 00:12:35 -07003158 return nlmsg_cancel(skb, nlh);
Thomas Graf85486af2006-09-18 00:11:24 -07003159
Thomas Graf0ab68032006-09-18 00:12:35 -07003160 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161}
3162
3163static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07003164 u32 pid, u32 seq, int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166 struct nlmsghdr *nlh;
Thomas Graf101bb222006-09-18 00:11:52 -07003167 u8 scope = RT_SCOPE_UNIVERSE;
3168 int ifindex = ifaca->aca_idev->dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169
Thomas Graf101bb222006-09-18 00:11:52 -07003170 if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE)
3171 scope = RT_SCOPE_SITE;
3172
Thomas Graf0ab68032006-09-18 00:12:35 -07003173 nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags);
3174 if (nlh == NULL)
3175 return -ENOBUFS;
3176
Thomas Graf101bb222006-09-18 00:11:52 -07003177 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
Thomas Graf0ab68032006-09-18 00:12:35 -07003178 if (nla_put(skb, IFA_ANYCAST, 16, &ifaca->aca_addr) < 0 ||
3179 put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp,
Thomas Graf85486af2006-09-18 00:11:24 -07003180 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0)
Thomas Graf0ab68032006-09-18 00:12:35 -07003181 return nlmsg_cancel(skb, nlh);
Thomas Graf85486af2006-09-18 00:11:24 -07003182
Thomas Graf0ab68032006-09-18 00:12:35 -07003183 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184}
3185
3186enum addr_type_t
3187{
3188 UNICAST_ADDR,
3189 MULTICAST_ADDR,
3190 ANYCAST_ADDR,
3191};
3192
3193static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
3194 enum addr_type_t type)
3195{
3196 int idx, ip_idx;
3197 int s_idx, s_ip_idx;
3198 int err = 1;
3199 struct net_device *dev;
3200 struct inet6_dev *idev = NULL;
3201 struct inet6_ifaddr *ifa;
3202 struct ifmcaddr6 *ifmca;
3203 struct ifacaddr6 *ifaca;
3204
3205 s_idx = cb->args[0];
3206 s_ip_idx = ip_idx = cb->args[1];
3207 read_lock(&dev_base_lock);
3208
3209 for (dev = dev_base, idx = 0; dev; dev = dev->next, idx++) {
3210 if (idx < s_idx)
3211 continue;
3212 if (idx > s_idx)
3213 s_ip_idx = 0;
3214 ip_idx = 0;
3215 if ((idev = in6_dev_get(dev)) == NULL)
3216 continue;
3217 read_lock_bh(&idev->lock);
3218 switch (type) {
3219 case UNICAST_ADDR:
YOSHIFUJI Hideakiae9cda52005-06-28 13:00:30 -07003220 /* unicast address incl. temp addr */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 for (ifa = idev->addr_list; ifa;
3222 ifa = ifa->if_next, ip_idx++) {
3223 if (ip_idx < s_ip_idx)
3224 continue;
3225 if ((err = inet6_fill_ifaddr(skb, ifa,
3226 NETLINK_CB(cb->skb).pid,
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07003227 cb->nlh->nlmsg_seq, RTM_NEWADDR,
3228 NLM_F_MULTI)) <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 goto done;
3230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231 break;
3232 case MULTICAST_ADDR:
3233 /* multicast address */
3234 for (ifmca = idev->mc_list; ifmca;
3235 ifmca = ifmca->next, ip_idx++) {
3236 if (ip_idx < s_ip_idx)
3237 continue;
3238 if ((err = inet6_fill_ifmcaddr(skb, ifmca,
3239 NETLINK_CB(cb->skb).pid,
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07003240 cb->nlh->nlmsg_seq, RTM_GETMULTICAST,
3241 NLM_F_MULTI)) <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242 goto done;
3243 }
3244 break;
3245 case ANYCAST_ADDR:
3246 /* anycast address */
3247 for (ifaca = idev->ac_list; ifaca;
3248 ifaca = ifaca->aca_next, ip_idx++) {
3249 if (ip_idx < s_ip_idx)
3250 continue;
3251 if ((err = inet6_fill_ifacaddr(skb, ifaca,
3252 NETLINK_CB(cb->skb).pid,
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07003253 cb->nlh->nlmsg_seq, RTM_GETANYCAST,
3254 NLM_F_MULTI)) <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255 goto done;
3256 }
3257 break;
3258 default:
3259 break;
3260 }
3261 read_unlock_bh(&idev->lock);
3262 in6_dev_put(idev);
3263 }
3264done:
3265 if (err <= 0) {
3266 read_unlock_bh(&idev->lock);
3267 in6_dev_put(idev);
3268 }
3269 read_unlock(&dev_base_lock);
3270 cb->args[0] = idx;
3271 cb->args[1] = ip_idx;
3272 return skb->len;
3273}
3274
3275static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
3276{
3277 enum addr_type_t type = UNICAST_ADDR;
3278 return inet6_dump_addr(skb, cb, type);
3279}
3280
3281static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
3282{
3283 enum addr_type_t type = MULTICAST_ADDR;
3284 return inet6_dump_addr(skb, cb, type);
3285}
3286
3287
3288static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
3289{
3290 enum addr_type_t type = ANYCAST_ADDR;
3291 return inet6_dump_addr(skb, cb, type);
3292}
3293
Thomas Graf1b29fc22006-09-18 00:10:50 -07003294static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr* nlh,
3295 void *arg)
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003296{
Thomas Graf1b29fc22006-09-18 00:10:50 -07003297 struct ifaddrmsg *ifm;
3298 struct nlattr *tb[IFA_MAX+1];
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003299 struct in6_addr *addr = NULL;
3300 struct net_device *dev = NULL;
3301 struct inet6_ifaddr *ifa;
3302 struct sk_buff *skb;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003303 int err;
3304
Thomas Graf1b29fc22006-09-18 00:10:50 -07003305 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
3306 if (err < 0)
3307 goto errout;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003308
Thomas Graf1b29fc22006-09-18 00:10:50 -07003309 addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
3310 if (addr == NULL) {
3311 err = -EINVAL;
3312 goto errout;
3313 }
3314
3315 ifm = nlmsg_data(nlh);
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003316 if (ifm->ifa_index)
3317 dev = __dev_get_by_index(ifm->ifa_index);
3318
Thomas Graf1b29fc22006-09-18 00:10:50 -07003319 if ((ifa = ipv6_get_ifaddr(addr, dev, 1)) == NULL) {
3320 err = -EADDRNOTAVAIL;
3321 goto errout;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003322 }
3323
Thomas Graf0ab68032006-09-18 00:12:35 -07003324 if ((skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL)) == NULL) {
Thomas Graf1b29fc22006-09-18 00:10:50 -07003325 err = -ENOBUFS;
3326 goto errout_ifa;
3327 }
3328
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003329 err = inet6_fill_ifaddr(skb, ifa, NETLINK_CB(in_skb).pid,
3330 nlh->nlmsg_seq, RTM_NEWADDR, 0);
Thomas Graf339bf982006-11-10 14:10:15 -08003331 /* failure implies BUG in inet6_ifaddr_msgsize() */
3332 BUG_ON(err < 0);
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003333
Thomas Graf2942e902006-08-15 00:30:25 -07003334 err = rtnl_unicast(skb, NETLINK_CB(in_skb).pid);
Thomas Graf1b29fc22006-09-18 00:10:50 -07003335errout_ifa:
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003336 in6_ifa_put(ifa);
Thomas Graf1b29fc22006-09-18 00:10:50 -07003337errout:
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003338 return err;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003339}
3340
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
3342{
3343 struct sk_buff *skb;
Thomas Graf5d620262006-08-15 00:35:02 -07003344 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345
Thomas Graf0ab68032006-09-18 00:12:35 -07003346 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC);
Thomas Graf5d620262006-08-15 00:35:02 -07003347 if (skb == NULL)
3348 goto errout;
3349
3350 err = inet6_fill_ifaddr(skb, ifa, 0, 0, event, 0);
Thomas Graf339bf982006-11-10 14:10:15 -08003351 /* failure implies BUG in inet6_ifaddr_msgsize() */
3352 BUG_ON(err < 0);
Thomas Graf5d620262006-08-15 00:35:02 -07003353
3354 err = rtnl_notify(skb, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
3355errout:
3356 if (err < 0)
3357 rtnl_set_sk_err(RTNLGRP_IPV6_IFADDR, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358}
3359
3360static void inline ipv6_store_devconf(struct ipv6_devconf *cnf,
3361 __s32 *array, int bytes)
3362{
Thomas Graf04561c12006-11-14 19:53:58 -08003363 BUG_ON(bytes < (DEVCONF_MAX * 4));
3364
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365 memset(array, 0, bytes);
3366 array[DEVCONF_FORWARDING] = cnf->forwarding;
3367 array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
3368 array[DEVCONF_MTU6] = cnf->mtu6;
3369 array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
3370 array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
3371 array[DEVCONF_AUTOCONF] = cnf->autoconf;
3372 array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
3373 array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
3374 array[DEVCONF_RTR_SOLICIT_INTERVAL] = cnf->rtr_solicit_interval;
3375 array[DEVCONF_RTR_SOLICIT_DELAY] = cnf->rtr_solicit_delay;
3376 array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
3377#ifdef CONFIG_IPV6_PRIVACY
3378 array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
3379 array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
3380 array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
3381 array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
3382 array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
3383#endif
3384 array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -08003385 array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr;
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -08003386 array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo;
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08003387#ifdef CONFIG_IPV6_ROUTER_PREF
3388 array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref;
YOSHIFUJI Hideaki52e16352006-03-20 17:05:47 -08003389 array[DEVCONF_RTR_PROBE_INTERVAL] = cnf->rtr_probe_interval;
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -08003390#ifdef CONFIV_IPV6_ROUTE_INFO
3391 array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen;
3392#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08003393#endif
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -07003394 array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395}
3396
Thomas Graf339bf982006-11-10 14:10:15 -08003397static inline size_t inet6_if_nlmsg_size(void)
3398{
3399 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
3400 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
3401 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
3402 + nla_total_size(4) /* IFLA_MTU */
3403 + nla_total_size(4) /* IFLA_LINK */
3404 + nla_total_size( /* IFLA_PROTINFO */
3405 nla_total_size(4) /* IFLA_INET6_FLAGS */
3406 + nla_total_size(sizeof(struct ifla_cacheinfo))
3407 + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */
3408 );
3409}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07003410
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07003412 u32 pid, u32 seq, int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413{
Thomas Graf04561c12006-11-14 19:53:58 -08003414 struct net_device *dev = idev->dev;
3415 struct nlattr *conf;
3416 struct ifinfomsg *hdr;
3417 struct nlmsghdr *nlh;
3418 void *protoinfo;
3419 struct ifla_cacheinfo ci;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420
Thomas Graf04561c12006-11-14 19:53:58 -08003421 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*hdr), flags);
3422 if (nlh == NULL)
3423 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424
Thomas Graf04561c12006-11-14 19:53:58 -08003425 hdr = nlmsg_data(nlh);
3426 hdr->ifi_family = AF_INET6;
3427 hdr->__ifi_pad = 0;
3428 hdr->ifi_type = dev->type;
3429 hdr->ifi_index = dev->ifindex;
3430 hdr->ifi_flags = dev_get_flags(dev);
3431 hdr->ifi_change = 0;
3432
3433 NLA_PUT_STRING(skb, IFLA_IFNAME, dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434
3435 if (dev->addr_len)
Thomas Graf04561c12006-11-14 19:53:58 -08003436 NLA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437
Thomas Graf04561c12006-11-14 19:53:58 -08003438 NLA_PUT_U32(skb, IFLA_MTU, dev->mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439 if (dev->ifindex != dev->iflink)
Thomas Graf04561c12006-11-14 19:53:58 -08003440 NLA_PUT_U32(skb, IFLA_LINK, dev->iflink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441
Thomas Graf04561c12006-11-14 19:53:58 -08003442 protoinfo = nla_nest_start(skb, IFLA_PROTINFO);
3443 if (protoinfo == NULL)
3444 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445
Thomas Graf04561c12006-11-14 19:53:58 -08003446 NLA_PUT_U32(skb, IFLA_INET6_FLAGS, idev->if_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448 ci.max_reasm_len = IPV6_MAXPLEN;
3449 ci.tstamp = (__u32)(TIME_DELTA(idev->tstamp, INITIAL_JIFFIES) / HZ * 100
3450 + TIME_DELTA(idev->tstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
3451 ci.reachable_time = idev->nd_parms->reachable_time;
3452 ci.retrans_time = idev->nd_parms->retrans_time;
Thomas Graf04561c12006-11-14 19:53:58 -08003453 NLA_PUT(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci);
3454
3455 conf = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32));
3456 if (conf == NULL)
3457 goto nla_put_failure;
3458 ipv6_store_devconf(&idev->cnf, nla_data(conf), nla_len(conf));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459
3460 /* XXX - Statistics/MC not implemented */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461
Thomas Graf04561c12006-11-14 19:53:58 -08003462 nla_nest_end(skb, protoinfo);
3463 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464
Thomas Graf04561c12006-11-14 19:53:58 -08003465nla_put_failure:
3466 return nlmsg_cancel(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467}
3468
3469static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
3470{
3471 int idx, err;
3472 int s_idx = cb->args[0];
3473 struct net_device *dev;
3474 struct inet6_dev *idev;
3475
3476 read_lock(&dev_base_lock);
3477 for (dev=dev_base, idx=0; dev; dev = dev->next, idx++) {
3478 if (idx < s_idx)
3479 continue;
3480 if ((idev = in6_dev_get(dev)) == NULL)
3481 continue;
3482 err = inet6_fill_ifinfo(skb, idev, NETLINK_CB(cb->skb).pid,
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07003483 cb->nlh->nlmsg_seq, RTM_NEWLINK, NLM_F_MULTI);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484 in6_dev_put(idev);
3485 if (err <= 0)
3486 break;
3487 }
3488 read_unlock(&dev_base_lock);
3489 cb->args[0] = idx;
3490
3491 return skb->len;
3492}
3493
3494void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
3495{
3496 struct sk_buff *skb;
Thomas Graf8d7a76c2006-08-15 00:35:47 -07003497 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498
Thomas Graf339bf982006-11-10 14:10:15 -08003499 skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC);
Thomas Graf8d7a76c2006-08-15 00:35:47 -07003500 if (skb == NULL)
3501 goto errout;
3502
3503 err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0);
Thomas Graf339bf982006-11-10 14:10:15 -08003504 /* failure implies BUG in inet6_if_nlmsg_size() */
3505 BUG_ON(err < 0);
Thomas Graf8d7a76c2006-08-15 00:35:47 -07003506
3507 err = rtnl_notify(skb, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
3508errout:
3509 if (err < 0)
3510 rtnl_set_sk_err(RTNLGRP_IPV6_IFADDR, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511}
3512
Thomas Graf339bf982006-11-10 14:10:15 -08003513static inline size_t inet6_prefix_nlmsg_size(void)
3514{
3515 return NLMSG_ALIGN(sizeof(struct prefixmsg))
3516 + nla_total_size(sizeof(struct in6_addr))
3517 + nla_total_size(sizeof(struct prefix_cacheinfo));
3518}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07003519
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
Thomas Graf6051e2f2006-11-14 19:54:19 -08003521 struct prefix_info *pinfo, u32 pid, u32 seq,
3522 int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523{
Thomas Graf6051e2f2006-11-14 19:54:19 -08003524 struct prefixmsg *pmsg;
3525 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526 struct prefix_cacheinfo ci;
3527
Thomas Graf6051e2f2006-11-14 19:54:19 -08003528 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*pmsg), flags);
3529 if (nlh == NULL)
3530 return -ENOBUFS;
3531
3532 pmsg = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533 pmsg->prefix_family = AF_INET6;
Patrick McHardy8a470772005-06-28 12:56:45 -07003534 pmsg->prefix_pad1 = 0;
3535 pmsg->prefix_pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536 pmsg->prefix_ifindex = idev->dev->ifindex;
3537 pmsg->prefix_len = pinfo->prefix_len;
3538 pmsg->prefix_type = pinfo->type;
Patrick McHardy8a470772005-06-28 12:56:45 -07003539 pmsg->prefix_pad3 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540 pmsg->prefix_flags = 0;
3541 if (pinfo->onlink)
3542 pmsg->prefix_flags |= IF_PREFIX_ONLINK;
3543 if (pinfo->autoconf)
3544 pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
3545
Thomas Graf6051e2f2006-11-14 19:54:19 -08003546 NLA_PUT(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547
3548 ci.preferred_time = ntohl(pinfo->prefered);
3549 ci.valid_time = ntohl(pinfo->valid);
Thomas Graf6051e2f2006-11-14 19:54:19 -08003550 NLA_PUT(skb, PREFIX_CACHEINFO, sizeof(ci), &ci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551
Thomas Graf6051e2f2006-11-14 19:54:19 -08003552 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553
Thomas Graf6051e2f2006-11-14 19:54:19 -08003554nla_put_failure:
3555 return nlmsg_cancel(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556}
3557
3558static void inet6_prefix_notify(int event, struct inet6_dev *idev,
3559 struct prefix_info *pinfo)
3560{
3561 struct sk_buff *skb;
Thomas Graf8c384bfa2006-08-15 00:36:07 -07003562 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563
Thomas Graf339bf982006-11-10 14:10:15 -08003564 skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC);
Thomas Graf8c384bfa2006-08-15 00:36:07 -07003565 if (skb == NULL)
3566 goto errout;
3567
3568 err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0);
Thomas Graf339bf982006-11-10 14:10:15 -08003569 /* failure implies BUG in inet6_prefix_nlmsg_size() */
3570 BUG_ON(err < 0);
Thomas Graf8c384bfa2006-08-15 00:36:07 -07003571
3572 err = rtnl_notify(skb, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC);
3573errout:
3574 if (err < 0)
3575 rtnl_set_sk_err(RTNLGRP_IPV6_PREFIX, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576}
3577
Thomas Grafdb46edc2005-05-03 14:29:39 -07003578static struct rtnetlink_link inet6_rtnetlink_table[RTM_NR_MSGTYPES] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579 [RTM_GETLINK - RTM_BASE] = { .dumpit = inet6_dump_ifinfo, },
3580 [RTM_NEWADDR - RTM_BASE] = { .doit = inet6_rtm_newaddr, },
3581 [RTM_DELADDR - RTM_BASE] = { .doit = inet6_rtm_deladdr, },
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003582 [RTM_GETADDR - RTM_BASE] = { .doit = inet6_rtm_getaddr,
3583 .dumpit = inet6_dump_ifaddr, },
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584 [RTM_GETMULTICAST - RTM_BASE] = { .dumpit = inet6_dump_ifmcaddr, },
3585 [RTM_GETANYCAST - RTM_BASE] = { .dumpit = inet6_dump_ifacaddr, },
3586 [RTM_NEWROUTE - RTM_BASE] = { .doit = inet6_rtm_newroute, },
3587 [RTM_DELROUTE - RTM_BASE] = { .doit = inet6_rtm_delroute, },
3588 [RTM_GETROUTE - RTM_BASE] = { .doit = inet6_rtm_getroute,
3589 .dumpit = inet6_dump_fib, },
David S. Miller8423a9a2006-08-07 21:54:37 -07003590#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Thomas Graf101367c2006-08-04 03:39:02 -07003591 [RTM_GETRULE - RTM_BASE] = { .dumpit = fib6_rules_dump, },
David S. Miller8423a9a2006-08-07 21:54:37 -07003592#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593};
3594
3595static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
3596{
3597 inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
3598
3599 switch (event) {
3600 case RTM_NEWADDR:
Thomas Graf40e22e82006-08-22 00:00:45 -07003601 ip6_ins_rt(ifp->rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602 if (ifp->idev->cnf.forwarding)
3603 addrconf_join_anycast(ifp);
3604 break;
3605 case RTM_DELADDR:
3606 if (ifp->idev->cnf.forwarding)
3607 addrconf_leave_anycast(ifp);
3608 addrconf_leave_solict(ifp->idev, &ifp->addr);
3609 dst_hold(&ifp->rt->u.dst);
Thomas Grafe0a1ad732006-08-22 00:00:21 -07003610 if (ip6_del_rt(ifp->rt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611 dst_free(&ifp->rt->u.dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612 break;
3613 }
3614}
3615
3616static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
3617{
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07003618 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619 if (likely(ifp->idev->dead == 0))
3620 __ipv6_ifa_notify(event, ifp);
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07003621 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622}
3623
3624#ifdef CONFIG_SYSCTL
3625
3626static
3627int addrconf_sysctl_forward(ctl_table *ctl, int write, struct file * filp,
3628 void __user *buffer, size_t *lenp, loff_t *ppos)
3629{
3630 int *valp = ctl->data;
3631 int val = *valp;
3632 int ret;
3633
3634 ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
3635
3636 if (write && valp != &ipv6_devconf_dflt.forwarding) {
3637 if (valp != &ipv6_devconf.forwarding) {
3638 if ((!*valp) ^ (!val)) {
3639 struct inet6_dev *idev = (struct inet6_dev *)ctl->extra1;
3640 if (idev == NULL)
3641 return ret;
3642 dev_forward_change(idev);
3643 }
3644 } else {
3645 ipv6_devconf_dflt.forwarding = ipv6_devconf.forwarding;
3646 addrconf_forward_change();
3647 }
3648 if (*valp)
3649 rt6_purge_dflt_routers();
3650 }
3651
3652 return ret;
3653}
3654
3655static int addrconf_sysctl_forward_strategy(ctl_table *table,
3656 int __user *name, int nlen,
3657 void __user *oldval,
3658 size_t __user *oldlenp,
3659 void __user *newval, size_t newlen,
3660 void **context)
3661{
3662 int *valp = table->data;
3663 int new;
3664
3665 if (!newval || !newlen)
3666 return 0;
3667 if (newlen != sizeof(int))
3668 return -EINVAL;
3669 if (get_user(new, (int __user *)newval))
3670 return -EFAULT;
3671 if (new == *valp)
3672 return 0;
3673 if (oldval && oldlenp) {
3674 size_t len;
3675 if (get_user(len, oldlenp))
3676 return -EFAULT;
3677 if (len) {
3678 if (len > table->maxlen)
3679 len = table->maxlen;
3680 if (copy_to_user(oldval, valp, len))
3681 return -EFAULT;
3682 if (put_user(len, oldlenp))
3683 return -EFAULT;
3684 }
3685 }
3686
3687 if (valp != &ipv6_devconf_dflt.forwarding) {
3688 if (valp != &ipv6_devconf.forwarding) {
3689 struct inet6_dev *idev = (struct inet6_dev *)table->extra1;
3690 int changed;
3691 if (unlikely(idev == NULL))
3692 return -ENODEV;
3693 changed = (!*valp) ^ (!new);
3694 *valp = new;
3695 if (changed)
3696 dev_forward_change(idev);
3697 } else {
3698 *valp = new;
3699 addrconf_forward_change();
3700 }
3701
3702 if (*valp)
3703 rt6_purge_dflt_routers();
3704 } else
3705 *valp = new;
3706
3707 return 1;
3708}
3709
3710static struct addrconf_sysctl_table
3711{
3712 struct ctl_table_header *sysctl_header;
3713 ctl_table addrconf_vars[__NET_IPV6_MAX];
3714 ctl_table addrconf_dev[2];
3715 ctl_table addrconf_conf_dir[2];
3716 ctl_table addrconf_proto_dir[2];
3717 ctl_table addrconf_root_dir[2];
Brian Haleyab32ea52006-09-22 14:15:41 -07003718} addrconf_sysctl __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719 .sysctl_header = NULL,
3720 .addrconf_vars = {
3721 {
3722 .ctl_name = NET_IPV6_FORWARDING,
3723 .procname = "forwarding",
3724 .data = &ipv6_devconf.forwarding,
3725 .maxlen = sizeof(int),
3726 .mode = 0644,
3727 .proc_handler = &addrconf_sysctl_forward,
3728 .strategy = &addrconf_sysctl_forward_strategy,
3729 },
3730 {
3731 .ctl_name = NET_IPV6_HOP_LIMIT,
3732 .procname = "hop_limit",
3733 .data = &ipv6_devconf.hop_limit,
3734 .maxlen = sizeof(int),
3735 .mode = 0644,
3736 .proc_handler = proc_dointvec,
3737 },
3738 {
3739 .ctl_name = NET_IPV6_MTU,
3740 .procname = "mtu",
3741 .data = &ipv6_devconf.mtu6,
3742 .maxlen = sizeof(int),
3743 .mode = 0644,
3744 .proc_handler = &proc_dointvec,
3745 },
3746 {
3747 .ctl_name = NET_IPV6_ACCEPT_RA,
3748 .procname = "accept_ra",
3749 .data = &ipv6_devconf.accept_ra,
3750 .maxlen = sizeof(int),
3751 .mode = 0644,
3752 .proc_handler = &proc_dointvec,
3753 },
3754 {
3755 .ctl_name = NET_IPV6_ACCEPT_REDIRECTS,
3756 .procname = "accept_redirects",
3757 .data = &ipv6_devconf.accept_redirects,
3758 .maxlen = sizeof(int),
3759 .mode = 0644,
3760 .proc_handler = &proc_dointvec,
3761 },
3762 {
3763 .ctl_name = NET_IPV6_AUTOCONF,
3764 .procname = "autoconf",
3765 .data = &ipv6_devconf.autoconf,
3766 .maxlen = sizeof(int),
3767 .mode = 0644,
3768 .proc_handler = &proc_dointvec,
3769 },
3770 {
3771 .ctl_name = NET_IPV6_DAD_TRANSMITS,
3772 .procname = "dad_transmits",
3773 .data = &ipv6_devconf.dad_transmits,
3774 .maxlen = sizeof(int),
3775 .mode = 0644,
3776 .proc_handler = &proc_dointvec,
3777 },
3778 {
3779 .ctl_name = NET_IPV6_RTR_SOLICITS,
3780 .procname = "router_solicitations",
3781 .data = &ipv6_devconf.rtr_solicits,
3782 .maxlen = sizeof(int),
3783 .mode = 0644,
3784 .proc_handler = &proc_dointvec,
3785 },
3786 {
3787 .ctl_name = NET_IPV6_RTR_SOLICIT_INTERVAL,
3788 .procname = "router_solicitation_interval",
3789 .data = &ipv6_devconf.rtr_solicit_interval,
3790 .maxlen = sizeof(int),
3791 .mode = 0644,
3792 .proc_handler = &proc_dointvec_jiffies,
3793 .strategy = &sysctl_jiffies,
3794 },
3795 {
3796 .ctl_name = NET_IPV6_RTR_SOLICIT_DELAY,
3797 .procname = "router_solicitation_delay",
3798 .data = &ipv6_devconf.rtr_solicit_delay,
3799 .maxlen = sizeof(int),
3800 .mode = 0644,
3801 .proc_handler = &proc_dointvec_jiffies,
3802 .strategy = &sysctl_jiffies,
3803 },
3804 {
3805 .ctl_name = NET_IPV6_FORCE_MLD_VERSION,
3806 .procname = "force_mld_version",
3807 .data = &ipv6_devconf.force_mld_version,
3808 .maxlen = sizeof(int),
3809 .mode = 0644,
3810 .proc_handler = &proc_dointvec,
3811 },
3812#ifdef CONFIG_IPV6_PRIVACY
3813 {
3814 .ctl_name = NET_IPV6_USE_TEMPADDR,
3815 .procname = "use_tempaddr",
3816 .data = &ipv6_devconf.use_tempaddr,
3817 .maxlen = sizeof(int),
3818 .mode = 0644,
3819 .proc_handler = &proc_dointvec,
3820 },
3821 {
3822 .ctl_name = NET_IPV6_TEMP_VALID_LFT,
3823 .procname = "temp_valid_lft",
3824 .data = &ipv6_devconf.temp_valid_lft,
3825 .maxlen = sizeof(int),
3826 .mode = 0644,
3827 .proc_handler = &proc_dointvec,
3828 },
3829 {
3830 .ctl_name = NET_IPV6_TEMP_PREFERED_LFT,
3831 .procname = "temp_prefered_lft",
3832 .data = &ipv6_devconf.temp_prefered_lft,
3833 .maxlen = sizeof(int),
3834 .mode = 0644,
3835 .proc_handler = &proc_dointvec,
3836 },
3837 {
3838 .ctl_name = NET_IPV6_REGEN_MAX_RETRY,
3839 .procname = "regen_max_retry",
3840 .data = &ipv6_devconf.regen_max_retry,
3841 .maxlen = sizeof(int),
3842 .mode = 0644,
3843 .proc_handler = &proc_dointvec,
3844 },
3845 {
3846 .ctl_name = NET_IPV6_MAX_DESYNC_FACTOR,
3847 .procname = "max_desync_factor",
3848 .data = &ipv6_devconf.max_desync_factor,
3849 .maxlen = sizeof(int),
3850 .mode = 0644,
3851 .proc_handler = &proc_dointvec,
3852 },
3853#endif
3854 {
3855 .ctl_name = NET_IPV6_MAX_ADDRESSES,
3856 .procname = "max_addresses",
3857 .data = &ipv6_devconf.max_addresses,
3858 .maxlen = sizeof(int),
3859 .mode = 0644,
3860 .proc_handler = &proc_dointvec,
3861 },
3862 {
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -08003863 .ctl_name = NET_IPV6_ACCEPT_RA_DEFRTR,
3864 .procname = "accept_ra_defrtr",
3865 .data = &ipv6_devconf.accept_ra_defrtr,
3866 .maxlen = sizeof(int),
3867 .mode = 0644,
3868 .proc_handler = &proc_dointvec,
3869 },
3870 {
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -08003871 .ctl_name = NET_IPV6_ACCEPT_RA_PINFO,
3872 .procname = "accept_ra_pinfo",
3873 .data = &ipv6_devconf.accept_ra_pinfo,
3874 .maxlen = sizeof(int),
3875 .mode = 0644,
3876 .proc_handler = &proc_dointvec,
3877 },
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08003878#ifdef CONFIG_IPV6_ROUTER_PREF
3879 {
3880 .ctl_name = NET_IPV6_ACCEPT_RA_RTR_PREF,
3881 .procname = "accept_ra_rtr_pref",
3882 .data = &ipv6_devconf.accept_ra_rtr_pref,
3883 .maxlen = sizeof(int),
3884 .mode = 0644,
3885 .proc_handler = &proc_dointvec,
3886 },
YOSHIFUJI Hideaki52e16352006-03-20 17:05:47 -08003887 {
3888 .ctl_name = NET_IPV6_RTR_PROBE_INTERVAL,
3889 .procname = "router_probe_interval",
3890 .data = &ipv6_devconf.rtr_probe_interval,
3891 .maxlen = sizeof(int),
3892 .mode = 0644,
3893 .proc_handler = &proc_dointvec_jiffies,
3894 .strategy = &sysctl_jiffies,
3895 },
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -08003896#ifdef CONFIV_IPV6_ROUTE_INFO
3897 {
3898 .ctl_name = NET_IPV6_ACCEPT_RA_RT_INFO_MAX_PLEN,
3899 .procname = "accept_ra_rt_info_max_plen",
3900 .data = &ipv6_devconf.accept_ra_rt_info_max_plen,
3901 .maxlen = sizeof(int),
3902 .mode = 0644,
3903 .proc_handler = &proc_dointvec,
3904 },
3905#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08003906#endif
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -08003907 {
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -07003908 .ctl_name = NET_IPV6_PROXY_NDP,
3909 .procname = "proxy_ndp",
3910 .data = &ipv6_devconf.proxy_ndp,
3911 .maxlen = sizeof(int),
3912 .mode = 0644,
3913 .proc_handler = &proc_dointvec,
3914 },
3915 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916 .ctl_name = 0, /* sentinel */
3917 }
3918 },
3919 .addrconf_dev = {
3920 {
3921 .ctl_name = NET_PROTO_CONF_ALL,
3922 .procname = "all",
3923 .mode = 0555,
3924 .child = addrconf_sysctl.addrconf_vars,
3925 },
3926 {
3927 .ctl_name = 0, /* sentinel */
3928 }
3929 },
3930 .addrconf_conf_dir = {
3931 {
3932 .ctl_name = NET_IPV6_CONF,
3933 .procname = "conf",
3934 .mode = 0555,
3935 .child = addrconf_sysctl.addrconf_dev,
3936 },
3937 {
3938 .ctl_name = 0, /* sentinel */
3939 }
3940 },
3941 .addrconf_proto_dir = {
3942 {
3943 .ctl_name = NET_IPV6,
3944 .procname = "ipv6",
3945 .mode = 0555,
3946 .child = addrconf_sysctl.addrconf_conf_dir,
3947 },
3948 {
3949 .ctl_name = 0, /* sentinel */
3950 }
3951 },
3952 .addrconf_root_dir = {
3953 {
3954 .ctl_name = CTL_NET,
3955 .procname = "net",
3956 .mode = 0555,
3957 .child = addrconf_sysctl.addrconf_proto_dir,
3958 },
3959 {
3960 .ctl_name = 0, /* sentinel */
3961 }
3962 },
3963};
3964
3965static void addrconf_sysctl_register(struct inet6_dev *idev, struct ipv6_devconf *p)
3966{
3967 int i;
3968 struct net_device *dev = idev ? idev->dev : NULL;
3969 struct addrconf_sysctl_table *t;
3970 char *dev_name = NULL;
3971
Arnaldo Carvalho de Meloaf879cc2006-11-17 12:14:37 -02003972 t = kmemdup(&addrconf_sysctl, sizeof(*t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973 if (t == NULL)
3974 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975 for (i=0; t->addrconf_vars[i].data; i++) {
3976 t->addrconf_vars[i].data += (char*)p - (char*)&ipv6_devconf;
3977 t->addrconf_vars[i].de = NULL;
3978 t->addrconf_vars[i].extra1 = idev; /* embedded; no ref */
3979 }
3980 if (dev) {
3981 dev_name = dev->name;
3982 t->addrconf_dev[0].ctl_name = dev->ifindex;
3983 } else {
3984 dev_name = "default";
3985 t->addrconf_dev[0].ctl_name = NET_PROTO_CONF_DEFAULT;
3986 }
3987
3988 /*
3989 * Make a copy of dev_name, because '.procname' is regarded as const
3990 * by sysctl and we wouldn't want anyone to change it under our feet
3991 * (see SIOCSIFNAME).
3992 */
Paulo Marques543537b2005-06-23 00:09:02 -07003993 dev_name = kstrdup(dev_name, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994 if (!dev_name)
3995 goto free;
3996
3997 t->addrconf_dev[0].procname = dev_name;
3998
3999 t->addrconf_dev[0].child = t->addrconf_vars;
4000 t->addrconf_dev[0].de = NULL;
4001 t->addrconf_conf_dir[0].child = t->addrconf_dev;
4002 t->addrconf_conf_dir[0].de = NULL;
4003 t->addrconf_proto_dir[0].child = t->addrconf_conf_dir;
4004 t->addrconf_proto_dir[0].de = NULL;
4005 t->addrconf_root_dir[0].child = t->addrconf_proto_dir;
4006 t->addrconf_root_dir[0].de = NULL;
4007
4008 t->sysctl_header = register_sysctl_table(t->addrconf_root_dir, 0);
4009 if (t->sysctl_header == NULL)
4010 goto free_procname;
4011 else
4012 p->sysctl = t;
4013 return;
4014
4015 /* error path */
4016 free_procname:
4017 kfree(dev_name);
4018 free:
4019 kfree(t);
4020
4021 return;
4022}
4023
4024static void addrconf_sysctl_unregister(struct ipv6_devconf *p)
4025{
4026 if (p->sysctl) {
4027 struct addrconf_sysctl_table *t = p->sysctl;
4028 p->sysctl = NULL;
4029 unregister_sysctl_table(t->sysctl_header);
4030 kfree(t->addrconf_dev[0].procname);
4031 kfree(t);
4032 }
4033}
4034
4035
4036#endif
4037
4038/*
4039 * Device notifier
4040 */
4041
4042int register_inet6addr_notifier(struct notifier_block *nb)
4043{
Alan Sterne041c682006-03-27 01:16:30 -08004044 return atomic_notifier_chain_register(&inet6addr_chain, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045}
4046
4047int unregister_inet6addr_notifier(struct notifier_block *nb)
4048{
Alan Sterne041c682006-03-27 01:16:30 -08004049 return atomic_notifier_chain_unregister(&inet6addr_chain,nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050}
4051
4052/*
4053 * Init / cleanup code
4054 */
4055
4056int __init addrconf_init(void)
4057{
4058 int err = 0;
4059
4060 /* The addrconf netdev notifier requires that loopback_dev
4061 * has it's ipv6 private information allocated and setup
4062 * before it can bring up and give link-local addresses
4063 * to other devices which are up.
4064 *
4065 * Unfortunately, loopback_dev is not necessarily the first
4066 * entry in the global dev_base list of net devices. In fact,
4067 * it is likely to be the very last entry on that list.
4068 * So this causes the notifier registry below to try and
4069 * give link-local addresses to all devices besides loopback_dev
4070 * first, then loopback_dev, which cases all the non-loopback_dev
4071 * devices to fail to get a link-local address.
4072 *
4073 * So, as a temporary fix, allocate the ipv6 structure for
4074 * loopback_dev first by hand.
4075 * Longer term, all of the dependencies ipv6 has upon the loopback
4076 * device and it being up should be removed.
4077 */
4078 rtnl_lock();
4079 if (!ipv6_add_dev(&loopback_dev))
4080 err = -ENOMEM;
4081 rtnl_unlock();
4082 if (err)
4083 return err;
4084
Herbert Xuc62dba92005-09-26 15:10:16 -07004085 ip6_null_entry.rt6i_idev = in6_dev_get(&loopback_dev);
4086
Linus Torvalds1da177e2005-04-16 15:20:36 -07004087 register_netdevice_notifier(&ipv6_dev_notf);
4088
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089 addrconf_verify(0);
4090 rtnetlink_links[PF_INET6] = inet6_rtnetlink_table;
4091#ifdef CONFIG_SYSCTL
4092 addrconf_sysctl.sysctl_header =
4093 register_sysctl_table(addrconf_sysctl.addrconf_root_dir, 0);
4094 addrconf_sysctl_register(NULL, &ipv6_devconf_dflt);
4095#endif
4096
4097 return 0;
4098}
4099
4100void __exit addrconf_cleanup(void)
4101{
4102 struct net_device *dev;
4103 struct inet6_dev *idev;
4104 struct inet6_ifaddr *ifa;
4105 int i;
4106
4107 unregister_netdevice_notifier(&ipv6_dev_notf);
4108
4109 rtnetlink_links[PF_INET6] = NULL;
4110#ifdef CONFIG_SYSCTL
4111 addrconf_sysctl_unregister(&ipv6_devconf_dflt);
4112 addrconf_sysctl_unregister(&ipv6_devconf);
4113#endif
4114
4115 rtnl_lock();
4116
4117 /*
4118 * clean dev list.
4119 */
4120
4121 for (dev=dev_base; dev; dev=dev->next) {
4122 if ((idev = __in6_dev_get(dev)) == NULL)
4123 continue;
4124 addrconf_ifdown(dev, 1);
4125 }
4126 addrconf_ifdown(&loopback_dev, 2);
4127
4128 /*
4129 * Check hash table.
4130 */
4131
4132 write_lock_bh(&addrconf_hash_lock);
4133 for (i=0; i < IN6_ADDR_HSIZE; i++) {
4134 for (ifa=inet6_addr_lst[i]; ifa; ) {
4135 struct inet6_ifaddr *bifa;
4136
4137 bifa = ifa;
4138 ifa = ifa->lst_next;
4139 printk(KERN_DEBUG "bug: IPv6 address leakage detected: ifa=%p\n", bifa);
4140 /* Do not free it; something is wrong.
4141 Now we can investigate it with debugger.
4142 */
4143 }
4144 }
4145 write_unlock_bh(&addrconf_hash_lock);
4146
4147 del_timer(&addr_chk_timer);
4148
4149 rtnl_unlock();
4150
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151#ifdef CONFIG_PROC_FS
4152 proc_net_remove("if_inet6");
4153#endif
4154}