blob: 34d80b4aaa048364ed5db14d6583a94f897ac583 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * IPv6 Address [auto]configuration
3 * Linux INET6 implementation
4 *
5 * Authors:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09006 * Pedro Roque <roque@di.fc.ul.pt>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * 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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/net.h>
48#include <linux/in6.h>
49#include <linux/netdevice.h>
Thomas Graf18237302006-08-04 23:04:54 -070050#include <linux/if_addr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/if_arp.h>
52#include <linux/if_arcnet.h>
53#include <linux/if_infiniband.h>
54#include <linux/route.h>
55#include <linux/inetdevice.h>
56#include <linux/init.h>
57#ifdef CONFIG_SYSCTL
58#include <linux/sysctl.h>
59#endif
Randy Dunlap4fc268d2006-01-11 12:17:47 -080060#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <linux/delay.h>
62#include <linux/notifier.h>
Paulo Marques543537b2005-06-23 00:09:02 -070063#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65#include <net/sock.h>
66#include <net/snmp.h>
67
68#include <net/ipv6.h>
69#include <net/protocol.h>
70#include <net/ndisc.h>
71#include <net/ip6_route.h>
72#include <net/addrconf.h>
73#include <net/tcp.h>
74#include <net/ip.h>
Thomas Graf5d620262006-08-15 00:35:02 -070075#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070076#include <linux/if_tunnel.h>
77#include <linux/rtnetlink.h>
78
79#ifdef CONFIG_IPV6_PRIVACY
80#include <linux/random.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070081#endif
82
83#include <asm/uaccess.h>
84
85#include <linux/proc_fs.h>
86#include <linux/seq_file.h>
87
88/* Set to 3 to get tracing... */
89#define ACONF_DEBUG 2
90
91#if ACONF_DEBUG >= 3
92#define ADBG(x) printk x
93#else
94#define ADBG(x)
95#endif
96
97#define INFINITY_LIFE_TIME 0xFFFFFFFF
98#define TIME_DELTA(a,b) ((unsigned long)((long)(a) - (long)(b)))
99
100#ifdef CONFIG_SYSCTL
101static void addrconf_sysctl_register(struct inet6_dev *idev, struct ipv6_devconf *p);
102static void addrconf_sysctl_unregister(struct ipv6_devconf *p);
103#endif
104
105#ifdef CONFIG_IPV6_PRIVACY
106static int __ipv6_regen_rndid(struct inet6_dev *idev);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900107static int __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108static void ipv6_regen_rndid(unsigned long data);
109
110static int desync_factor = MAX_DESYNC_FACTOR * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111#endif
112
113static int ipv6_count_addresses(struct inet6_dev *idev);
114
115/*
116 * Configured unicast address hash table
117 */
118static struct inet6_ifaddr *inet6_addr_lst[IN6_ADDR_HSIZE];
119static DEFINE_RWLOCK(addrconf_hash_lock);
120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121static void addrconf_verify(unsigned long);
122
Ingo Molnar8d06afa2005-09-09 13:10:40 -0700123static DEFINE_TIMER(addr_chk_timer, addrconf_verify, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124static DEFINE_SPINLOCK(addrconf_verify_lock);
125
126static void addrconf_join_anycast(struct inet6_ifaddr *ifp);
127static void addrconf_leave_anycast(struct inet6_ifaddr *ifp);
128
129static int addrconf_ifdown(struct net_device *dev, int how);
130
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -0700131static void addrconf_dad_start(struct inet6_ifaddr *ifp, u32 flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132static void addrconf_dad_timer(unsigned long data);
133static void addrconf_dad_completed(struct inet6_ifaddr *ifp);
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +0900134static void addrconf_dad_run(struct inet6_dev *idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135static void addrconf_rs_timer(unsigned long data);
136static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
137static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
138
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900139static void inet6_prefix_notify(int event, struct inet6_dev *idev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 struct prefix_info *pinfo);
141static int ipv6_chk_same_addr(const struct in6_addr *addr, struct net_device *dev);
142
Alan Sterne041c682006-03-27 01:16:30 -0800143static ATOMIC_NOTIFIER_HEAD(inet6addr_chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Brian Haleyab32ea52006-09-22 14:15:41 -0700145struct ipv6_devconf ipv6_devconf __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 .forwarding = 0,
147 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
148 .mtu6 = IPV6_MIN_MTU,
149 .accept_ra = 1,
150 .accept_redirects = 1,
151 .autoconf = 1,
152 .force_mld_version = 0,
153 .dad_transmits = 1,
154 .rtr_solicits = MAX_RTR_SOLICITATIONS,
155 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
156 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
157#ifdef CONFIG_IPV6_PRIVACY
158 .use_tempaddr = 0,
159 .temp_valid_lft = TEMP_VALID_LIFETIME,
160 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
161 .regen_max_retry = REGEN_MAX_RETRY,
162 .max_desync_factor = MAX_DESYNC_FACTOR,
163#endif
164 .max_addresses = IPV6_MAX_ADDRESSES,
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -0800165 .accept_ra_defrtr = 1,
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -0800166 .accept_ra_pinfo = 1,
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800167#ifdef CONFIG_IPV6_ROUTER_PREF
168 .accept_ra_rtr_pref = 1,
YOSHIFUJI Hideaki52e163562006-03-20 17:05:47 -0800169 .rtr_probe_interval = 60 * HZ,
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -0800170#ifdef CONFIG_IPV6_ROUTE_INFO
171 .accept_ra_rt_info_max_plen = 0,
172#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800173#endif
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -0700174 .proxy_ndp = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175};
176
Brian Haleyab32ea52006-09-22 14:15:41 -0700177static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 .forwarding = 0,
179 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
180 .mtu6 = IPV6_MIN_MTU,
181 .accept_ra = 1,
182 .accept_redirects = 1,
183 .autoconf = 1,
184 .dad_transmits = 1,
185 .rtr_solicits = MAX_RTR_SOLICITATIONS,
186 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
187 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
188#ifdef CONFIG_IPV6_PRIVACY
189 .use_tempaddr = 0,
190 .temp_valid_lft = TEMP_VALID_LIFETIME,
191 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
192 .regen_max_retry = REGEN_MAX_RETRY,
193 .max_desync_factor = MAX_DESYNC_FACTOR,
194#endif
195 .max_addresses = IPV6_MAX_ADDRESSES,
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -0800196 .accept_ra_defrtr = 1,
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -0800197 .accept_ra_pinfo = 1,
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800198#ifdef CONFIG_IPV6_ROUTER_PREF
199 .accept_ra_rtr_pref = 1,
YOSHIFUJI Hideaki52e163562006-03-20 17:05:47 -0800200 .rtr_probe_interval = 60 * HZ,
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -0800201#ifdef CONFIG_IPV6_ROUTE_INFO
202 .accept_ra_rt_info_max_plen = 0,
203#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800204#endif
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -0700205 .proxy_ndp = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206};
207
208/* IPv6 Wildcard Address and Loopback Address defined by RFC2553 */
209#if 0
210const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
211#endif
212const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
213
YOSHIFUJI Hideakib1cacb62005-11-08 09:38:12 -0800214#define IPV6_ADDR_SCOPE_TYPE(scope) ((scope) << 16)
215
216static inline unsigned ipv6_addr_scope2type(unsigned scope)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217{
YOSHIFUJI Hideakib1cacb62005-11-08 09:38:12 -0800218 switch(scope) {
219 case IPV6_ADDR_SCOPE_NODELOCAL:
220 return (IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_NODELOCAL) |
221 IPV6_ADDR_LOOPBACK);
222 case IPV6_ADDR_SCOPE_LINKLOCAL:
223 return (IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_LINKLOCAL) |
224 IPV6_ADDR_LINKLOCAL);
225 case IPV6_ADDR_SCOPE_SITELOCAL:
226 return (IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_SITELOCAL) |
227 IPV6_ADDR_SITELOCAL);
228 }
229 return IPV6_ADDR_SCOPE_TYPE(scope);
230}
231
232int __ipv6_addr_type(const struct in6_addr *addr)
233{
Al Viroe69a4ad2006-11-14 20:56:00 -0800234 __be32 st;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236 st = addr->s6_addr32[0];
237
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 /* Consider all addresses with the first three bits different of
YOSHIFUJI Hideakib1cacb62005-11-08 09:38:12 -0800239 000 and 111 as unicasts.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 */
241 if ((st & htonl(0xE0000000)) != htonl(0x00000000) &&
242 (st & htonl(0xE0000000)) != htonl(0xE0000000))
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900243 return (IPV6_ADDR_UNICAST |
YOSHIFUJI Hideakib1cacb62005-11-08 09:38:12 -0800244 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
YOSHIFUJI Hideakib1cacb62005-11-08 09:38:12 -0800246 if ((st & htonl(0xFF000000)) == htonl(0xFF000000)) {
247 /* multicast */
248 /* addr-select 3.1 */
249 return (IPV6_ADDR_MULTICAST |
250 ipv6_addr_scope2type(IPV6_ADDR_MC_SCOPE(addr)));
251 }
252
253 if ((st & htonl(0xFFC00000)) == htonl(0xFE800000))
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900254 return (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_UNICAST |
YOSHIFUJI Hideakib1cacb62005-11-08 09:38:12 -0800255 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_LINKLOCAL)); /* addr-select 3.1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 if ((st & htonl(0xFFC00000)) == htonl(0xFEC00000))
YOSHIFUJI Hideakib1cacb62005-11-08 09:38:12 -0800257 return (IPV6_ADDR_SITELOCAL | IPV6_ADDR_UNICAST |
258 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_SITELOCAL)); /* addr-select 3.1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
260 if ((addr->s6_addr32[0] | addr->s6_addr32[1]) == 0) {
261 if (addr->s6_addr32[2] == 0) {
262 if (addr->s6_addr32[3] == 0)
263 return IPV6_ADDR_ANY;
264
265 if (addr->s6_addr32[3] == htonl(0x00000001))
YOSHIFUJI Hideakib1cacb62005-11-08 09:38:12 -0800266 return (IPV6_ADDR_LOOPBACK | IPV6_ADDR_UNICAST |
267 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_LINKLOCAL)); /* addr-select 3.4 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
YOSHIFUJI Hideakib1cacb62005-11-08 09:38:12 -0800269 return (IPV6_ADDR_COMPATv4 | IPV6_ADDR_UNICAST |
270 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL)); /* addr-select 3.3 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 }
272
273 if (addr->s6_addr32[2] == htonl(0x0000ffff))
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900274 return (IPV6_ADDR_MAPPED |
YOSHIFUJI Hideakib1cacb62005-11-08 09:38:12 -0800275 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL)); /* addr-select 3.3 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 }
277
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900278 return (IPV6_ADDR_RESERVED |
YOSHIFUJI Hideakib1cacb62005-11-08 09:38:12 -0800279 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL)); /* addr-select 3.4 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280}
281
282static void addrconf_del_timer(struct inet6_ifaddr *ifp)
283{
284 if (del_timer(&ifp->timer))
285 __in6_ifa_put(ifp);
286}
287
288enum addrconf_timer_t
289{
290 AC_NONE,
291 AC_DAD,
292 AC_RS,
293};
294
295static void addrconf_mod_timer(struct inet6_ifaddr *ifp,
296 enum addrconf_timer_t what,
297 unsigned long when)
298{
299 if (!del_timer(&ifp->timer))
300 in6_ifa_hold(ifp);
301
302 switch (what) {
303 case AC_DAD:
304 ifp->timer.function = addrconf_dad_timer;
305 break;
306 case AC_RS:
307 ifp->timer.function = addrconf_rs_timer;
308 break;
309 default:;
310 }
311 ifp->timer.expires = jiffies + when;
312 add_timer(&ifp->timer);
313}
314
315/* Nobody refers to this device, we may destroy it. */
316
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -0700317static void in6_dev_finish_destroy_rcu(struct rcu_head *head)
318{
319 struct inet6_dev *idev = container_of(head, struct inet6_dev, rcu);
320 kfree(idev);
321}
322
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323void in6_dev_finish_destroy(struct inet6_dev *idev)
324{
325 struct net_device *dev = idev->dev;
326 BUG_TRAP(idev->addr_list==NULL);
327 BUG_TRAP(idev->mc_list==NULL);
328#ifdef NET_REFCNT_DEBUG
329 printk(KERN_DEBUG "in6_dev_finish_destroy: %s\n", dev ? dev->name : "NIL");
330#endif
331 dev_put(dev);
332 if (!idev->dead) {
333 printk("Freeing alive inet6 device %p\n", idev);
334 return;
335 }
336 snmp6_free_dev(idev);
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -0700337 call_rcu(&idev->rcu, in6_dev_finish_destroy_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338}
339
340static struct inet6_dev * ipv6_add_dev(struct net_device *dev)
341{
342 struct inet6_dev *ndev;
YOSHIFUJI Hideakid88ae4c2007-01-14 21:48:40 -0800343 struct in6_addr maddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
345 ASSERT_RTNL();
346
347 if (dev->mtu < IPV6_MIN_MTU)
348 return NULL;
349
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900350 ndev = kzalloc(sizeof(struct inet6_dev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900352 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
Ingo Oeser322f74a2006-03-20 23:01:47 -0800416
417 ipv6_mc_init_dev(ndev);
418 ndev->tstamp = jiffies;
419#ifdef CONFIG_SYSCTL
420 neigh_sysctl_register(dev, ndev->nd_parms, NET_IPV6,
421 NET_IPV6_NEIGH, "ipv6",
422 &ndisc_ifinfo_sysctl_change,
423 NULL);
424 addrconf_sysctl_register(ndev, &ndev->cnf);
425#endif
David L Stevens30c4cf52007-01-04 12:31:14 -0800426 /* protected by rtnl_lock */
427 rcu_assign_pointer(dev->ip6_ptr, ndev);
YOSHIFUJI Hideakid88ae4c2007-01-14 21:48:40 -0800428
429 /* Join all-node multicast group */
430 ipv6_addr_all_nodes(&maddr);
431 ipv6_dev_mc_inc(dev, &maddr);
432
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 return ndev;
434}
435
436static struct inet6_dev * ipv6_find_idev(struct net_device *dev)
437{
438 struct inet6_dev *idev;
439
440 ASSERT_RTNL();
441
442 if ((idev = __in6_dev_get(dev)) == NULL) {
443 if ((idev = ipv6_add_dev(dev)) == NULL)
444 return NULL;
445 }
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +0900446
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 if (dev->flags&IFF_UP)
448 ipv6_mc_up(idev);
449 return idev;
450}
451
452#ifdef CONFIG_SYSCTL
453static void dev_forward_change(struct inet6_dev *idev)
454{
455 struct net_device *dev;
456 struct inet6_ifaddr *ifa;
457 struct in6_addr addr;
458
459 if (!idev)
460 return;
461 dev = idev->dev;
462 if (dev && (dev->flags & IFF_MULTICAST)) {
463 ipv6_addr_all_routers(&addr);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900464
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 if (idev->cnf.forwarding)
466 ipv6_dev_mc_inc(dev, &addr);
467 else
468 ipv6_dev_mc_dec(dev, &addr);
469 }
470 for (ifa=idev->addr_list; ifa; ifa=ifa->if_next) {
471 if (idev->cnf.forwarding)
472 addrconf_join_anycast(ifa);
473 else
474 addrconf_leave_anycast(ifa);
475 }
476}
477
478
479static void addrconf_forward_change(void)
480{
481 struct net_device *dev;
482 struct inet6_dev *idev;
483
484 read_lock(&dev_base_lock);
485 for (dev=dev_base; dev; dev=dev->next) {
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -0700486 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 idev = __in6_dev_get(dev);
488 if (idev) {
489 int changed = (!idev->cnf.forwarding) ^ (!ipv6_devconf.forwarding);
490 idev->cnf.forwarding = ipv6_devconf.forwarding;
491 if (changed)
492 dev_forward_change(idev);
493 }
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -0700494 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 }
496 read_unlock(&dev_base_lock);
497}
498#endif
499
500/* Nobody refers to this ifaddr, destroy it */
501
502void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp)
503{
504 BUG_TRAP(ifp->if_next==NULL);
505 BUG_TRAP(ifp->lst_next==NULL);
506#ifdef NET_REFCNT_DEBUG
507 printk(KERN_DEBUG "inet6_ifa_finish_destroy\n");
508#endif
509
510 in6_dev_put(ifp->idev);
511
512 if (del_timer(&ifp->timer))
513 printk("Timer is still running, when freeing ifa=%p\n", ifp);
514
515 if (!ifp->dead) {
516 printk("Freeing alive inet6 address %p\n", ifp);
517 return;
518 }
519 dst_release(&ifp->rt->u.dst);
520
521 kfree(ifp);
522}
523
Brian Haleye55ffac2006-07-10 15:25:51 -0700524static void
525ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp)
526{
527 struct inet6_ifaddr *ifa, **ifap;
YOSHIFUJI Hideaki8a6ce0c2006-07-11 13:05:30 -0700528 int ifp_scope = ipv6_addr_src_scope(&ifp->addr);
Brian Haleye55ffac2006-07-10 15:25:51 -0700529
530 /*
531 * Each device address list is sorted in order of scope -
532 * global before linklocal.
533 */
534 for (ifap = &idev->addr_list; (ifa = *ifap) != NULL;
535 ifap = &ifa->if_next) {
YOSHIFUJI Hideaki8a6ce0c2006-07-11 13:05:30 -0700536 if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr))
Brian Haleye55ffac2006-07-10 15:25:51 -0700537 break;
538 }
539
540 ifp->if_next = *ifap;
541 *ifap = ifp;
542}
543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544/* On success it returns ifp with increased reference count */
545
546static struct inet6_ifaddr *
547ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr, int pfxlen,
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -0700548 int scope, u32 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549{
550 struct inet6_ifaddr *ifa = NULL;
551 struct rt6_info *rt;
552 int hash;
553 int err = 0;
554
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -0700555 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 if (idev->dead) {
557 err = -ENODEV; /*XXX*/
558 goto out2;
559 }
560
561 write_lock(&addrconf_hash_lock);
562
563 /* Ignore adding duplicate addresses on an interface */
564 if (ipv6_chk_same_addr(addr, idev->dev)) {
565 ADBG(("ipv6_add_addr: already assigned\n"));
566 err = -EEXIST;
567 goto out;
568 }
569
Ingo Oeser322f74a2006-03-20 23:01:47 -0800570 ifa = kzalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
572 if (ifa == NULL) {
573 ADBG(("ipv6_add_addr: malloc failed\n"));
574 err = -ENOBUFS;
575 goto out;
576 }
577
578 rt = addrconf_dst_alloc(idev, addr, 0);
579 if (IS_ERR(rt)) {
580 err = PTR_ERR(rt);
581 goto out;
582 }
583
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 ipv6_addr_copy(&ifa->addr, addr);
585
586 spin_lock_init(&ifa->lock);
587 init_timer(&ifa->timer);
588 ifa->timer.data = (unsigned long) ifa;
589 ifa->scope = scope;
590 ifa->prefix_len = pfxlen;
591 ifa->flags = flags | IFA_F_TENTATIVE;
592 ifa->cstamp = ifa->tstamp = jiffies;
593
Keir Fraser57f5f542006-08-29 02:43:49 -0700594 ifa->rt = rt;
595
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 ifa->idev = idev;
597 in6_dev_hold(idev);
598 /* For caller */
599 in6_ifa_hold(ifa);
600
601 /* Add to big hash table */
602 hash = ipv6_addr_hash(addr);
603
604 ifa->lst_next = inet6_addr_lst[hash];
605 inet6_addr_lst[hash] = ifa;
606 in6_ifa_hold(ifa);
607 write_unlock(&addrconf_hash_lock);
608
609 write_lock(&idev->lock);
610 /* Add to inet6_dev unicast addr list. */
Brian Haleye55ffac2006-07-10 15:25:51 -0700611 ipv6_link_dev_addr(idev, ifa);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
613#ifdef CONFIG_IPV6_PRIVACY
614 if (ifa->flags&IFA_F_TEMPORARY) {
615 ifa->tmp_next = idev->tempaddr_list;
616 idev->tempaddr_list = ifa;
617 in6_ifa_hold(ifa);
618 }
619#endif
620
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 in6_ifa_hold(ifa);
622 write_unlock(&idev->lock);
623out2:
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -0700624 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
YOSHIFUJI Hideakifd928332005-04-19 22:27:09 -0700626 if (likely(err == 0))
Alan Sterne041c682006-03-27 01:16:30 -0800627 atomic_notifier_call_chain(&inet6addr_chain, NETDEV_UP, ifa);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 else {
629 kfree(ifa);
630 ifa = ERR_PTR(err);
631 }
632
633 return ifa;
634out:
635 write_unlock(&addrconf_hash_lock);
636 goto out2;
637}
638
639/* This function wants to get referenced ifp and releases it before return */
640
641static void ipv6_del_addr(struct inet6_ifaddr *ifp)
642{
643 struct inet6_ifaddr *ifa, **ifap;
644 struct inet6_dev *idev = ifp->idev;
645 int hash;
646 int deleted = 0, onlink = 0;
647 unsigned long expires = jiffies;
648
649 hash = ipv6_addr_hash(&ifp->addr);
650
651 ifp->dead = 1;
652
653 write_lock_bh(&addrconf_hash_lock);
654 for (ifap = &inet6_addr_lst[hash]; (ifa=*ifap) != NULL;
655 ifap = &ifa->lst_next) {
656 if (ifa == ifp) {
657 *ifap = ifa->lst_next;
658 __in6_ifa_put(ifp);
659 ifa->lst_next = NULL;
660 break;
661 }
662 }
663 write_unlock_bh(&addrconf_hash_lock);
664
665 write_lock_bh(&idev->lock);
666#ifdef CONFIG_IPV6_PRIVACY
667 if (ifp->flags&IFA_F_TEMPORARY) {
668 for (ifap = &idev->tempaddr_list; (ifa=*ifap) != NULL;
669 ifap = &ifa->tmp_next) {
670 if (ifa == ifp) {
671 *ifap = ifa->tmp_next;
672 if (ifp->ifpub) {
673 in6_ifa_put(ifp->ifpub);
674 ifp->ifpub = NULL;
675 }
676 __in6_ifa_put(ifp);
677 ifa->tmp_next = NULL;
678 break;
679 }
680 }
681 }
682#endif
683
Kristian Slavov1d142802005-12-21 18:47:24 -0800684 for (ifap = &idev->addr_list; (ifa=*ifap) != NULL;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 if (ifa == ifp) {
686 *ifap = ifa->if_next;
687 __in6_ifa_put(ifp);
688 ifa->if_next = NULL;
689 if (!(ifp->flags & IFA_F_PERMANENT) || onlink > 0)
690 break;
691 deleted = 1;
Kristian Slavov1d142802005-12-21 18:47:24 -0800692 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 } else if (ifp->flags & IFA_F_PERMANENT) {
694 if (ipv6_prefix_equal(&ifa->addr, &ifp->addr,
695 ifp->prefix_len)) {
696 if (ifa->flags & IFA_F_PERMANENT) {
697 onlink = 1;
698 if (deleted)
699 break;
700 } else {
701 unsigned long lifetime;
702
703 if (!onlink)
704 onlink = -1;
705
706 spin_lock(&ifa->lock);
707 lifetime = min_t(unsigned long,
708 ifa->valid_lft, 0x7fffffffUL/HZ);
709 if (time_before(expires,
710 ifa->tstamp + lifetime * HZ))
711 expires = ifa->tstamp + lifetime * HZ;
712 spin_unlock(&ifa->lock);
713 }
714 }
715 }
Kristian Slavov1d142802005-12-21 18:47:24 -0800716 ifap = &ifa->if_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 }
718 write_unlock_bh(&idev->lock);
719
720 ipv6_ifa_notify(RTM_DELADDR, ifp);
721
Alan Sterne041c682006-03-27 01:16:30 -0800722 atomic_notifier_call_chain(&inet6addr_chain, NETDEV_DOWN, ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
724 addrconf_del_timer(ifp);
725
726 /*
727 * Purge or update corresponding prefix
728 *
729 * 1) we don't purge prefix here if address was not permanent.
730 * prefix is managed by its own lifetime.
731 * 2) if there're no addresses, delete prefix.
732 * 3) if there're still other permanent address(es),
733 * corresponding prefix is still permanent.
734 * 4) otherwise, update prefix lifetime to the
735 * longest valid lifetime among the corresponding
736 * addresses on the device.
737 * Note: subsequent RA will update lifetime.
738 *
739 * --yoshfuji
740 */
741 if ((ifp->flags & IFA_F_PERMANENT) && onlink < 1) {
742 struct in6_addr prefix;
743 struct rt6_info *rt;
744
745 ipv6_addr_prefix(&prefix, &ifp->addr, ifp->prefix_len);
746 rt = rt6_lookup(&prefix, NULL, ifp->idev->dev->ifindex, 1);
747
748 if (rt && ((rt->rt6i_flags & (RTF_GATEWAY | RTF_DEFAULT)) == 0)) {
749 if (onlink == 0) {
Thomas Grafe0a1ad732006-08-22 00:00:21 -0700750 ip6_del_rt(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 rt = NULL;
752 } else if (!(rt->rt6i_flags & RTF_EXPIRES)) {
753 rt->rt6i_expires = expires;
754 rt->rt6i_flags |= RTF_EXPIRES;
755 }
756 }
757 dst_release(&rt->u.dst);
758 }
759
760 in6_ifa_put(ifp);
761}
762
763#ifdef CONFIG_IPV6_PRIVACY
764static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift)
765{
766 struct inet6_dev *idev = ifp->idev;
767 struct in6_addr addr, *tmpaddr;
768 unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_cstamp, tmp_tstamp;
769 int tmp_plen;
770 int ret = 0;
771 int max_addresses;
772
773 write_lock(&idev->lock);
774 if (ift) {
775 spin_lock_bh(&ift->lock);
776 memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8);
777 spin_unlock_bh(&ift->lock);
778 tmpaddr = &addr;
779 } else {
780 tmpaddr = NULL;
781 }
782retry:
783 in6_dev_hold(idev);
784 if (idev->cnf.use_tempaddr <= 0) {
785 write_unlock(&idev->lock);
786 printk(KERN_INFO
787 "ipv6_create_tempaddr(): use_tempaddr is disabled.\n");
788 in6_dev_put(idev);
789 ret = -1;
790 goto out;
791 }
792 spin_lock_bh(&ifp->lock);
793 if (ifp->regen_count++ >= idev->cnf.regen_max_retry) {
794 idev->cnf.use_tempaddr = -1; /*XXX*/
795 spin_unlock_bh(&ifp->lock);
796 write_unlock(&idev->lock);
797 printk(KERN_WARNING
798 "ipv6_create_tempaddr(): regeneration time exceeded. disabled temporary address support.\n");
799 in6_dev_put(idev);
800 ret = -1;
801 goto out;
802 }
803 in6_ifa_hold(ifp);
804 memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
805 if (__ipv6_try_regen_rndid(idev, tmpaddr) < 0) {
806 spin_unlock_bh(&ifp->lock);
807 write_unlock(&idev->lock);
808 printk(KERN_WARNING
809 "ipv6_create_tempaddr(): regeneration of randomized interface id failed.\n");
810 in6_ifa_put(ifp);
811 in6_dev_put(idev);
812 ret = -1;
813 goto out;
814 }
815 memcpy(&addr.s6_addr[8], idev->rndid, 8);
816 tmp_valid_lft = min_t(__u32,
817 ifp->valid_lft,
818 idev->cnf.temp_valid_lft);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900819 tmp_prefered_lft = min_t(__u32,
820 ifp->prefered_lft,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 idev->cnf.temp_prefered_lft - desync_factor / HZ);
822 tmp_plen = ifp->prefix_len;
823 max_addresses = idev->cnf.max_addresses;
824 tmp_cstamp = ifp->cstamp;
825 tmp_tstamp = ifp->tstamp;
826 spin_unlock_bh(&ifp->lock);
827
828 write_unlock(&idev->lock);
829 ift = !max_addresses ||
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900830 ipv6_count_addresses(idev) < max_addresses ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 ipv6_add_addr(idev, &addr, tmp_plen,
832 ipv6_addr_type(&addr)&IPV6_ADDR_SCOPE_MASK, IFA_F_TEMPORARY) : NULL;
833 if (!ift || IS_ERR(ift)) {
834 in6_ifa_put(ifp);
835 in6_dev_put(idev);
836 printk(KERN_INFO
837 "ipv6_create_tempaddr(): retry temporary address regeneration.\n");
838 tmpaddr = &addr;
839 write_lock(&idev->lock);
840 goto retry;
841 }
842
843 spin_lock_bh(&ift->lock);
844 ift->ifpub = ifp;
845 ift->valid_lft = tmp_valid_lft;
846 ift->prefered_lft = tmp_prefered_lft;
847 ift->cstamp = tmp_cstamp;
848 ift->tstamp = tmp_tstamp;
849 spin_unlock_bh(&ift->lock);
850
851 addrconf_dad_start(ift, 0);
852 in6_ifa_put(ift);
853 in6_dev_put(idev);
854out:
855 return ret;
856}
857#endif
858
859/*
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800860 * Choose an appropriate source address (RFC3484)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 */
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800862struct ipv6_saddr_score {
863 int addr_type;
864 unsigned int attrs;
865 int matchlen;
Brian Haley0d27b422006-03-11 18:50:14 -0800866 int scope;
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800867 unsigned int rule;
868};
869
870#define IPV6_SADDR_SCORE_LOCAL 0x0001
871#define IPV6_SADDR_SCORE_PREFERRED 0x0004
872#define IPV6_SADDR_SCORE_HOA 0x0008
873#define IPV6_SADDR_SCORE_OIF 0x0010
874#define IPV6_SADDR_SCORE_LABEL 0x0020
875#define IPV6_SADDR_SCORE_PRIVACY 0x0040
876
877static int inline ipv6_saddr_preferred(int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878{
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800879 if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|
880 IPV6_ADDR_LOOPBACK|IPV6_ADDR_RESERVED))
881 return 1;
882 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883}
884
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800885/* static matching label */
886static int inline ipv6_saddr_label(const struct in6_addr *addr, int type)
887{
888 /*
889 * prefix (longest match) label
890 * -----------------------------
891 * ::1/128 0
892 * ::/0 1
893 * 2002::/16 2
894 * ::/96 3
895 * ::ffff:0:0/96 4
Łukasz Stelmach102128e2006-06-22 01:37:19 -0700896 * fc00::/7 5
897 * 2001::/32 6
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800898 */
899 if (type & IPV6_ADDR_LOOPBACK)
900 return 0;
901 else if (type & IPV6_ADDR_COMPATv4)
902 return 3;
903 else if (type & IPV6_ADDR_MAPPED)
904 return 4;
Łukasz Stelmach102128e2006-06-22 01:37:19 -0700905 else if (addr->s6_addr32[0] == htonl(0x20010000))
906 return 6;
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800907 else if (addr->s6_addr16[0] == htons(0x2002))
908 return 2;
Łukasz Stelmach102128e2006-06-22 01:37:19 -0700909 else if ((addr->s6_addr[0] & 0xfe) == 0xfc)
910 return 5;
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800911 return 1;
912}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800914int ipv6_dev_get_saddr(struct net_device *daddr_dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 struct in6_addr *daddr, struct in6_addr *saddr)
916{
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800917 struct ipv6_saddr_score hiscore;
918 struct inet6_ifaddr *ifa_result = NULL;
919 int daddr_type = __ipv6_addr_type(daddr);
920 int daddr_scope = __ipv6_addr_src_scope(daddr_type);
921 u32 daddr_label = ipv6_saddr_label(daddr, daddr_type);
922 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800924 memset(&hiscore, 0, sizeof(hiscore));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
926 read_lock(&dev_base_lock);
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -0700927 rcu_read_lock();
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800928
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 for (dev = dev_base; dev; dev=dev->next) {
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800930 struct inet6_dev *idev;
931 struct inet6_ifaddr *ifa;
932
933 /* Rule 0: Candidate Source Address (section 4)
934 * - multicast and link-local destination address,
935 * the set of candidate source address MUST only
936 * include addresses assigned to interfaces
937 * belonging to the same link as the outgoing
938 * interface.
939 * (- For site-local destination addresses, the
940 * set of candidate source addresses MUST only
941 * include addresses assigned to interfaces
942 * belonging to the same site as the outgoing
943 * interface.)
944 */
945 if ((daddr_type & IPV6_ADDR_MULTICAST ||
946 daddr_scope <= IPV6_ADDR_SCOPE_LINKLOCAL) &&
947 daddr_dev && dev != daddr_dev)
948 continue;
949
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 idev = __in6_dev_get(dev);
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800951 if (!idev)
952 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800954 read_lock_bh(&idev->lock);
955 for (ifa = idev->addr_list; ifa; ifa = ifa->if_next) {
956 struct ipv6_saddr_score score;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800958 score.addr_type = __ipv6_addr_type(&ifa->addr);
959
YOSHIFUJI Hideaki6b3ae802005-12-21 22:58:01 +0900960 /* Rule 0:
961 * - Tentative Address (RFC2462 section 5.4)
962 * - A tentative address is not considered
963 * "assigned to an interface" in the traditional
964 * sense.
965 * - Candidate Source Address (section 4)
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800966 * - In any case, anycast addresses, multicast
967 * addresses, and the unspecified address MUST
968 * NOT be included in a candidate set.
969 */
YOSHIFUJI Hideaki6b3ae802005-12-21 22:58:01 +0900970 if (ifa->flags & IFA_F_TENTATIVE)
971 continue;
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800972 if (unlikely(score.addr_type == IPV6_ADDR_ANY ||
973 score.addr_type & IPV6_ADDR_MULTICAST)) {
974 LIMIT_NETDEBUG(KERN_DEBUG
975 "ADDRCONF: unspecified / multicast address"
976 "assigned as unicast address on %s",
977 dev->name);
978 continue;
979 }
980
981 score.attrs = 0;
982 score.matchlen = 0;
983 score.scope = 0;
984 score.rule = 0;
985
986 if (ifa_result == NULL) {
987 /* record it if the first available entry */
988 goto record_it;
989 }
990
991 /* Rule 1: Prefer same address */
992 if (hiscore.rule < 1) {
993 if (ipv6_addr_equal(&ifa_result->addr, daddr))
994 hiscore.attrs |= IPV6_SADDR_SCORE_LOCAL;
995 hiscore.rule++;
996 }
997 if (ipv6_addr_equal(&ifa->addr, daddr)) {
998 score.attrs |= IPV6_SADDR_SCORE_LOCAL;
999 if (!(hiscore.attrs & IPV6_SADDR_SCORE_LOCAL)) {
1000 score.rule = 1;
1001 goto record_it;
1002 }
1003 } else {
1004 if (hiscore.attrs & IPV6_SADDR_SCORE_LOCAL)
1005 continue;
1006 }
1007
1008 /* Rule 2: Prefer appropriate scope */
1009 if (hiscore.rule < 2) {
1010 hiscore.scope = __ipv6_addr_src_scope(hiscore.addr_type);
1011 hiscore.rule++;
1012 }
1013 score.scope = __ipv6_addr_src_scope(score.addr_type);
1014 if (hiscore.scope < score.scope) {
1015 if (hiscore.scope < daddr_scope) {
1016 score.rule = 2;
1017 goto record_it;
1018 } else
1019 continue;
1020 } else if (score.scope < hiscore.scope) {
1021 if (score.scope < daddr_scope)
Brian Haleye55ffac2006-07-10 15:25:51 -07001022 break; /* addresses sorted by scope */
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001023 else {
1024 score.rule = 2;
1025 goto record_it;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 }
1027 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001029 /* Rule 3: Avoid deprecated address */
1030 if (hiscore.rule < 3) {
1031 if (ipv6_saddr_preferred(hiscore.addr_type) ||
1032 !(ifa_result->flags & IFA_F_DEPRECATED))
1033 hiscore.attrs |= IPV6_SADDR_SCORE_PREFERRED;
1034 hiscore.rule++;
1035 }
1036 if (ipv6_saddr_preferred(score.addr_type) ||
1037 !(ifa->flags & IFA_F_DEPRECATED)) {
1038 score.attrs |= IPV6_SADDR_SCORE_PREFERRED;
1039 if (!(hiscore.attrs & IPV6_SADDR_SCORE_PREFERRED)) {
1040 score.rule = 3;
1041 goto record_it;
1042 }
1043 } else {
1044 if (hiscore.attrs & IPV6_SADDR_SCORE_PREFERRED)
1045 continue;
1046 }
1047
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07001048 /* Rule 4: Prefer home address */
1049#ifdef CONFIG_IPV6_MIP6
1050 if (hiscore.rule < 4) {
1051 if (ifa_result->flags & IFA_F_HOMEADDRESS)
1052 hiscore.attrs |= IPV6_SADDR_SCORE_HOA;
1053 hiscore.rule++;
1054 }
1055 if (ifa->flags & IFA_F_HOMEADDRESS) {
1056 score.attrs |= IPV6_SADDR_SCORE_HOA;
1057 if (!(ifa_result->flags & IFA_F_HOMEADDRESS)) {
1058 score.rule = 4;
1059 goto record_it;
1060 }
1061 } else {
1062 if (hiscore.attrs & IPV6_SADDR_SCORE_HOA)
1063 continue;
1064 }
1065#else
YOSHIFUJI Hideaki220bbd72005-11-28 22:27:11 -08001066 if (hiscore.rule < 4)
1067 hiscore.rule++;
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07001068#endif
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001069
1070 /* Rule 5: Prefer outgoing interface */
1071 if (hiscore.rule < 5) {
1072 if (daddr_dev == NULL ||
1073 daddr_dev == ifa_result->idev->dev)
1074 hiscore.attrs |= IPV6_SADDR_SCORE_OIF;
1075 hiscore.rule++;
1076 }
1077 if (daddr_dev == NULL ||
1078 daddr_dev == ifa->idev->dev) {
1079 score.attrs |= IPV6_SADDR_SCORE_OIF;
1080 if (!(hiscore.attrs & IPV6_SADDR_SCORE_OIF)) {
1081 score.rule = 5;
1082 goto record_it;
1083 }
1084 } else {
1085 if (hiscore.attrs & IPV6_SADDR_SCORE_OIF)
1086 continue;
1087 }
1088
1089 /* Rule 6: Prefer matching label */
1090 if (hiscore.rule < 6) {
1091 if (ipv6_saddr_label(&ifa_result->addr, hiscore.addr_type) == daddr_label)
1092 hiscore.attrs |= IPV6_SADDR_SCORE_LABEL;
1093 hiscore.rule++;
1094 }
1095 if (ipv6_saddr_label(&ifa->addr, score.addr_type) == daddr_label) {
1096 score.attrs |= IPV6_SADDR_SCORE_LABEL;
1097 if (!(hiscore.attrs & IPV6_SADDR_SCORE_LABEL)) {
1098 score.rule = 6;
1099 goto record_it;
1100 }
1101 } else {
1102 if (hiscore.attrs & IPV6_SADDR_SCORE_LABEL)
1103 continue;
1104 }
1105
Peter Chubb44fd0262005-11-09 13:05:47 -08001106#ifdef CONFIG_IPV6_PRIVACY
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001107 /* Rule 7: Prefer public address
1108 * Note: prefer temprary address if use_tempaddr >= 2
1109 */
1110 if (hiscore.rule < 7) {
1111 if ((!(ifa_result->flags & IFA_F_TEMPORARY)) ^
1112 (ifa_result->idev->cnf.use_tempaddr >= 2))
1113 hiscore.attrs |= IPV6_SADDR_SCORE_PRIVACY;
1114 hiscore.rule++;
1115 }
1116 if ((!(ifa->flags & IFA_F_TEMPORARY)) ^
1117 (ifa->idev->cnf.use_tempaddr >= 2)) {
1118 score.attrs |= IPV6_SADDR_SCORE_PRIVACY;
1119 if (!(hiscore.attrs & IPV6_SADDR_SCORE_PRIVACY)) {
1120 score.rule = 7;
1121 goto record_it;
1122 }
1123 } else {
1124 if (hiscore.attrs & IPV6_SADDR_SCORE_PRIVACY)
1125 continue;
1126 }
YOSHIFUJI Hideaki5e2707f2006-06-22 01:41:18 -07001127#else
1128 if (hiscore.rule < 7)
1129 hiscore.rule++;
Peter Chubb44fd0262005-11-09 13:05:47 -08001130#endif
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001131 /* Rule 8: Use longest matching prefix */
Yan Zheng12da2a42005-11-14 21:42:46 -08001132 if (hiscore.rule < 8) {
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001133 hiscore.matchlen = ipv6_addr_diff(&ifa_result->addr, daddr);
Yan Zheng12da2a42005-11-14 21:42:46 -08001134 hiscore.rule++;
1135 }
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001136 score.matchlen = ipv6_addr_diff(&ifa->addr, daddr);
1137 if (score.matchlen > hiscore.matchlen) {
1138 score.rule = 8;
1139 goto record_it;
1140 }
1141#if 0
1142 else if (score.matchlen < hiscore.matchlen)
1143 continue;
1144#endif
1145
1146 /* Final Rule: choose first available one */
1147 continue;
1148record_it:
1149 if (ifa_result)
1150 in6_ifa_put(ifa_result);
1151 in6_ifa_hold(ifa);
1152 ifa_result = ifa;
1153 hiscore = score;
1154 }
1155 read_unlock_bh(&idev->lock);
1156 }
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001157 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 read_unlock(&dev_base_lock);
1159
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001160 if (!ifa_result)
1161 return -EADDRNOTAVAIL;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001162
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001163 ipv6_addr_copy(saddr, &ifa_result->addr);
1164 in6_ifa_put(ifa_result);
1165 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166}
1167
1168
1169int ipv6_get_saddr(struct dst_entry *dst,
1170 struct in6_addr *daddr, struct in6_addr *saddr)
1171{
YOSHIFUJI Hideaki7a3025b2006-10-13 16:17:25 +09001172 return ipv6_dev_get_saddr(dst ? ip6_dst_idev(dst)->dev : NULL, daddr, saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173}
1174
1175
1176int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr)
1177{
1178 struct inet6_dev *idev;
1179 int err = -EADDRNOTAVAIL;
1180
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001181 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 if ((idev = __in6_dev_get(dev)) != NULL) {
1183 struct inet6_ifaddr *ifp;
1184
1185 read_lock_bh(&idev->lock);
1186 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
1187 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
1188 ipv6_addr_copy(addr, &ifp->addr);
1189 err = 0;
1190 break;
1191 }
1192 }
1193 read_unlock_bh(&idev->lock);
1194 }
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001195 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 return err;
1197}
1198
1199static int ipv6_count_addresses(struct inet6_dev *idev)
1200{
1201 int cnt = 0;
1202 struct inet6_ifaddr *ifp;
1203
1204 read_lock_bh(&idev->lock);
1205 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next)
1206 cnt++;
1207 read_unlock_bh(&idev->lock);
1208 return cnt;
1209}
1210
1211int ipv6_chk_addr(struct in6_addr *addr, struct net_device *dev, int strict)
1212{
1213 struct inet6_ifaddr * ifp;
1214 u8 hash = ipv6_addr_hash(addr);
1215
1216 read_lock_bh(&addrconf_hash_lock);
1217 for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
1218 if (ipv6_addr_equal(&ifp->addr, addr) &&
1219 !(ifp->flags&IFA_F_TENTATIVE)) {
1220 if (dev == NULL || ifp->idev->dev == dev ||
1221 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))
1222 break;
1223 }
1224 }
1225 read_unlock_bh(&addrconf_hash_lock);
1226 return ifp != NULL;
1227}
1228
1229static
1230int ipv6_chk_same_addr(const struct in6_addr *addr, struct net_device *dev)
1231{
1232 struct inet6_ifaddr * ifp;
1233 u8 hash = ipv6_addr_hash(addr);
1234
1235 for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
1236 if (ipv6_addr_equal(&ifp->addr, addr)) {
1237 if (dev == NULL || ifp->idev->dev == dev)
1238 break;
1239 }
1240 }
1241 return ifp != NULL;
1242}
1243
1244struct inet6_ifaddr * ipv6_get_ifaddr(struct in6_addr *addr, struct net_device *dev, int strict)
1245{
1246 struct inet6_ifaddr * ifp;
1247 u8 hash = ipv6_addr_hash(addr);
1248
1249 read_lock_bh(&addrconf_hash_lock);
1250 for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
1251 if (ipv6_addr_equal(&ifp->addr, addr)) {
1252 if (dev == NULL || ifp->idev->dev == dev ||
1253 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
1254 in6_ifa_hold(ifp);
1255 break;
1256 }
1257 }
1258 }
1259 read_unlock_bh(&addrconf_hash_lock);
1260
1261 return ifp;
1262}
1263
1264int ipv6_rcv_saddr_equal(const struct sock *sk, const struct sock *sk2)
1265{
1266 const struct in6_addr *sk_rcv_saddr6 = &inet6_sk(sk)->rcv_saddr;
Arnaldo Carvalho de Melo0fa1a532005-12-13 23:23:09 -08001267 const struct in6_addr *sk2_rcv_saddr6 = inet6_rcv_saddr(sk2);
Al Viro82103232006-09-27 18:44:10 -07001268 __be32 sk_rcv_saddr = inet_sk(sk)->rcv_saddr;
1269 __be32 sk2_rcv_saddr = inet_rcv_saddr(sk2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 int sk_ipv6only = ipv6_only_sock(sk);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001271 int sk2_ipv6only = inet_v6_ipv6only(sk2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 int addr_type = ipv6_addr_type(sk_rcv_saddr6);
1273 int addr_type2 = sk2_rcv_saddr6 ? ipv6_addr_type(sk2_rcv_saddr6) : IPV6_ADDR_MAPPED;
1274
1275 if (!sk2_rcv_saddr && !sk_ipv6only)
1276 return 1;
1277
1278 if (addr_type2 == IPV6_ADDR_ANY &&
1279 !(sk2_ipv6only && addr_type == IPV6_ADDR_MAPPED))
1280 return 1;
1281
1282 if (addr_type == IPV6_ADDR_ANY &&
1283 !(sk_ipv6only && addr_type2 == IPV6_ADDR_MAPPED))
1284 return 1;
1285
1286 if (sk2_rcv_saddr6 &&
1287 ipv6_addr_equal(sk_rcv_saddr6, sk2_rcv_saddr6))
1288 return 1;
1289
1290 if (addr_type == IPV6_ADDR_MAPPED &&
1291 !sk2_ipv6only &&
1292 (!sk2_rcv_saddr || !sk_rcv_saddr || sk_rcv_saddr == sk2_rcv_saddr))
1293 return 1;
1294
1295 return 0;
1296}
1297
1298/* Gets referenced address, destroys ifaddr */
1299
Adrian Bunk9f5336e2006-01-07 13:24:25 -08001300static void addrconf_dad_stop(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 if (ifp->flags&IFA_F_PERMANENT) {
1303 spin_lock_bh(&ifp->lock);
1304 addrconf_del_timer(ifp);
1305 ifp->flags |= IFA_F_TENTATIVE;
1306 spin_unlock_bh(&ifp->lock);
1307 in6_ifa_put(ifp);
1308#ifdef CONFIG_IPV6_PRIVACY
1309 } else if (ifp->flags&IFA_F_TEMPORARY) {
1310 struct inet6_ifaddr *ifpub;
1311 spin_lock_bh(&ifp->lock);
1312 ifpub = ifp->ifpub;
1313 if (ifpub) {
1314 in6_ifa_hold(ifpub);
1315 spin_unlock_bh(&ifp->lock);
1316 ipv6_create_tempaddr(ifpub, ifp);
1317 in6_ifa_put(ifpub);
1318 } else {
1319 spin_unlock_bh(&ifp->lock);
1320 }
1321 ipv6_del_addr(ifp);
1322#endif
1323 } else
1324 ipv6_del_addr(ifp);
1325}
1326
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09001327void addrconf_dad_failure(struct inet6_ifaddr *ifp)
1328{
1329 if (net_ratelimit())
1330 printk(KERN_INFO "%s: duplicate address detected!\n", ifp->idev->dev->name);
1331 addrconf_dad_stop(ifp);
1332}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
1334/* Join to solicited addr multicast group. */
1335
1336void addrconf_join_solict(struct net_device *dev, struct in6_addr *addr)
1337{
1338 struct in6_addr maddr;
1339
1340 if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1341 return;
1342
1343 addrconf_addr_solict_mult(addr, &maddr);
1344 ipv6_dev_mc_inc(dev, &maddr);
1345}
1346
1347void addrconf_leave_solict(struct inet6_dev *idev, struct in6_addr *addr)
1348{
1349 struct in6_addr maddr;
1350
1351 if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1352 return;
1353
1354 addrconf_addr_solict_mult(addr, &maddr);
1355 __ipv6_dev_mc_dec(idev, &maddr);
1356}
1357
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03001358static void addrconf_join_anycast(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359{
1360 struct in6_addr addr;
1361 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1362 if (ipv6_addr_any(&addr))
1363 return;
1364 ipv6_dev_ac_inc(ifp->idev->dev, &addr);
1365}
1366
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03001367static void addrconf_leave_anycast(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368{
1369 struct in6_addr addr;
1370 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1371 if (ipv6_addr_any(&addr))
1372 return;
1373 __ipv6_dev_ac_dec(ifp->idev, &addr);
1374}
1375
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08001376static int addrconf_ifid_eui48(u8 *eui, struct net_device *dev)
1377{
1378 if (dev->addr_len != ETH_ALEN)
1379 return -1;
1380 memcpy(eui, dev->dev_addr, 3);
1381 memcpy(eui + 5, dev->dev_addr + 3, 3);
1382
1383 /*
1384 * The zSeries OSA network cards can be shared among various
1385 * OS instances, but the OSA cards have only one MAC address.
1386 * This leads to duplicate address conflicts in conjunction
1387 * with IPv6 if more than one instance uses the same card.
1388 *
1389 * The driver for these cards can deliver a unique 16-bit
1390 * identifier for each instance sharing the same card. It is
1391 * placed instead of 0xFFFE in the interface identifier. The
1392 * "u" bit of the interface identifier is not inverted in this
1393 * case. Hence the resulting interface identifier has local
1394 * scope according to RFC2373.
1395 */
1396 if (dev->dev_id) {
1397 eui[3] = (dev->dev_id >> 8) & 0xFF;
1398 eui[4] = dev->dev_id & 0xFF;
1399 } else {
1400 eui[3] = 0xFF;
1401 eui[4] = 0xFE;
1402 eui[0] ^= 2;
1403 }
1404 return 0;
1405}
1406
1407static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev)
1408{
1409 /* XXX: inherit EUI-64 from other interface -- yoshfuji */
1410 if (dev->addr_len != ARCNET_ALEN)
1411 return -1;
1412 memset(eui, 0, 7);
1413 eui[7] = *(u8*)dev->dev_addr;
1414 return 0;
1415}
1416
1417static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev)
1418{
1419 if (dev->addr_len != INFINIBAND_ALEN)
1420 return -1;
1421 memcpy(eui, dev->dev_addr + 12, 8);
1422 eui[0] |= 2;
1423 return 0;
1424}
1425
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
1427{
1428 switch (dev->type) {
1429 case ARPHRD_ETHER:
1430 case ARPHRD_FDDI:
1431 case ARPHRD_IEEE802_TR:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08001432 return addrconf_ifid_eui48(eui, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 case ARPHRD_ARCNET:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08001434 return addrconf_ifid_arcnet(eui, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 case ARPHRD_INFINIBAND:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08001436 return addrconf_ifid_infiniband(eui, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 }
1438 return -1;
1439}
1440
1441static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
1442{
1443 int err = -1;
1444 struct inet6_ifaddr *ifp;
1445
1446 read_lock_bh(&idev->lock);
1447 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
1448 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
1449 memcpy(eui, ifp->addr.s6_addr+8, 8);
1450 err = 0;
1451 break;
1452 }
1453 }
1454 read_unlock_bh(&idev->lock);
1455 return err;
1456}
1457
1458#ifdef CONFIG_IPV6_PRIVACY
1459/* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
1460static int __ipv6_regen_rndid(struct inet6_dev *idev)
1461{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462regen:
YOSHIFUJI Hideaki955189e2006-03-20 16:54:09 -08001463 get_random_bytes(idev->rndid, sizeof(idev->rndid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 idev->rndid[0] &= ~0x02;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465
1466 /*
1467 * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
1468 * check if generated address is not inappropriate
1469 *
1470 * - Reserved subnet anycast (RFC 2526)
1471 * 11111101 11....11 1xxxxxxx
1472 * - ISATAP (draft-ietf-ngtrans-isatap-13.txt) 5.1
1473 * 00-00-5E-FE-xx-xx-xx-xx
1474 * - value 0
1475 * - XXX: already assigned to an address on the device
1476 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001477 if (idev->rndid[0] == 0xfd &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff &&
1479 (idev->rndid[7]&0x80))
1480 goto regen;
1481 if ((idev->rndid[0]|idev->rndid[1]) == 0) {
1482 if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe)
1483 goto regen;
1484 if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00)
1485 goto regen;
1486 }
1487
1488 return 0;
1489}
1490
1491static void ipv6_regen_rndid(unsigned long data)
1492{
1493 struct inet6_dev *idev = (struct inet6_dev *) data;
1494 unsigned long expires;
1495
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001496 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 write_lock_bh(&idev->lock);
1498
1499 if (idev->dead)
1500 goto out;
1501
1502 if (__ipv6_regen_rndid(idev) < 0)
1503 goto out;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001504
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 expires = jiffies +
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001506 idev->cnf.temp_prefered_lft * HZ -
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 idev->cnf.regen_max_retry * idev->cnf.dad_transmits * idev->nd_parms->retrans_time - desync_factor;
1508 if (time_before(expires, jiffies)) {
1509 printk(KERN_WARNING
1510 "ipv6_regen_rndid(): too short regeneration interval; timer disabled for %s.\n",
1511 idev->dev->name);
1512 goto out;
1513 }
1514
1515 if (!mod_timer(&idev->regen_timer, expires))
1516 in6_dev_hold(idev);
1517
1518out:
1519 write_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001520 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 in6_dev_put(idev);
1522}
1523
1524static int __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr) {
1525 int ret = 0;
1526
1527 if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0)
1528 ret = __ipv6_regen_rndid(idev);
1529 return ret;
1530}
1531#endif
1532
1533/*
1534 * Add prefix route.
1535 */
1536
1537static void
1538addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev,
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -07001539 unsigned long expires, u32 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540{
Thomas Graf86872cb2006-08-22 00:01:08 -07001541 struct fib6_config cfg = {
1542 .fc_table = RT6_TABLE_PREFIX,
1543 .fc_metric = IP6_RT_PRIO_ADDRCONF,
1544 .fc_ifindex = dev->ifindex,
1545 .fc_expires = expires,
1546 .fc_dst_len = plen,
1547 .fc_flags = RTF_UP | flags,
1548 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
Thomas Graf86872cb2006-08-22 00:01:08 -07001550 ipv6_addr_copy(&cfg.fc_dst, pfx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551
1552 /* Prevent useless cloning on PtP SIT.
1553 This thing is done here expecting that the whole
1554 class of non-broadcast devices need not cloning.
1555 */
Joerg Roedel0be669b2006-10-10 14:49:53 -07001556#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
Thomas Graf86872cb2006-08-22 00:01:08 -07001557 if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT))
1558 cfg.fc_flags |= RTF_NONEXTHOP;
Joerg Roedel0be669b2006-10-10 14:49:53 -07001559#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560
Thomas Graf86872cb2006-08-22 00:01:08 -07001561 ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562}
1563
1564/* Create "default" multicast route to the interface */
1565
1566static void addrconf_add_mroute(struct net_device *dev)
1567{
Thomas Graf86872cb2006-08-22 00:01:08 -07001568 struct fib6_config cfg = {
1569 .fc_table = RT6_TABLE_LOCAL,
1570 .fc_metric = IP6_RT_PRIO_ADDRCONF,
1571 .fc_ifindex = dev->ifindex,
1572 .fc_dst_len = 8,
1573 .fc_flags = RTF_UP,
1574 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575
Thomas Graf86872cb2006-08-22 00:01:08 -07001576 ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0);
1577
1578 ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579}
1580
Joerg Roedel0be669b2006-10-10 14:49:53 -07001581#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582static void sit_route_add(struct net_device *dev)
1583{
Thomas Graf86872cb2006-08-22 00:01:08 -07001584 struct fib6_config cfg = {
1585 .fc_table = RT6_TABLE_MAIN,
1586 .fc_metric = IP6_RT_PRIO_ADDRCONF,
1587 .fc_ifindex = dev->ifindex,
1588 .fc_dst_len = 96,
1589 .fc_flags = RTF_UP | RTF_NONEXTHOP,
1590 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
1592 /* prefix length - 96 bits "::d.d.d.d" */
Thomas Graf86872cb2006-08-22 00:01:08 -07001593 ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594}
Joerg Roedel0be669b2006-10-10 14:49:53 -07001595#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
1597static void addrconf_add_lroute(struct net_device *dev)
1598{
1599 struct in6_addr addr;
1600
1601 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
1602 addrconf_prefix_route(&addr, 64, dev, 0, 0);
1603}
1604
1605static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
1606{
1607 struct inet6_dev *idev;
1608
1609 ASSERT_RTNL();
1610
1611 if ((idev = ipv6_find_idev(dev)) == NULL)
1612 return NULL;
1613
1614 /* Add default multicast route */
1615 addrconf_add_mroute(dev);
1616
1617 /* Add link local route */
1618 addrconf_add_lroute(dev);
1619 return idev;
1620}
1621
1622void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len)
1623{
1624 struct prefix_info *pinfo;
1625 __u32 valid_lft;
1626 __u32 prefered_lft;
1627 int addr_type;
1628 unsigned long rt_expires;
1629 struct inet6_dev *in6_dev;
1630
1631 pinfo = (struct prefix_info *) opt;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001632
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 if (len < sizeof(struct prefix_info)) {
1634 ADBG(("addrconf: prefix option too short\n"));
1635 return;
1636 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001637
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 /*
1639 * Validation checks ([ADDRCONF], page 19)
1640 */
1641
1642 addr_type = ipv6_addr_type(&pinfo->prefix);
1643
1644 if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
1645 return;
1646
1647 valid_lft = ntohl(pinfo->valid);
1648 prefered_lft = ntohl(pinfo->prefered);
1649
1650 if (prefered_lft > valid_lft) {
1651 if (net_ratelimit())
1652 printk(KERN_WARNING "addrconf: prefix option has invalid lifetime\n");
1653 return;
1654 }
1655
1656 in6_dev = in6_dev_get(dev);
1657
1658 if (in6_dev == NULL) {
1659 if (net_ratelimit())
1660 printk(KERN_DEBUG "addrconf: device %s not configured\n", dev->name);
1661 return;
1662 }
1663
1664 /*
1665 * Two things going on here:
1666 * 1) Add routes for on-link prefixes
1667 * 2) Configure prefixes with the auto flag set
1668 */
1669
1670 /* Avoid arithmetic overflow. Really, we could
1671 save rt_expires in seconds, likely valid_lft,
1672 but it would require division in fib gc, that it
1673 not good.
1674 */
1675 if (valid_lft >= 0x7FFFFFFF/HZ)
YOSHIFUJI Hideaki3dd4bc62005-12-19 14:02:45 -08001676 rt_expires = 0x7FFFFFFF - (0x7FFFFFFF % HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 else
YOSHIFUJI Hideaki3dd4bc62005-12-19 14:02:45 -08001678 rt_expires = valid_lft * HZ;
1679
1680 /*
1681 * We convert this (in jiffies) to clock_t later.
1682 * Avoid arithmetic overflow there as well.
1683 * Overflow can happen only if HZ < USER_HZ.
1684 */
1685 if (HZ < USER_HZ && rt_expires > 0x7FFFFFFF / USER_HZ)
1686 rt_expires = 0x7FFFFFFF / USER_HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687
1688 if (pinfo->onlink) {
1689 struct rt6_info *rt;
1690 rt = rt6_lookup(&pinfo->prefix, NULL, dev->ifindex, 1);
1691
1692 if (rt && ((rt->rt6i_flags & (RTF_GATEWAY | RTF_DEFAULT)) == 0)) {
1693 if (rt->rt6i_flags&RTF_EXPIRES) {
1694 if (valid_lft == 0) {
Thomas Grafe0a1ad732006-08-22 00:00:21 -07001695 ip6_del_rt(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 rt = NULL;
1697 } else {
YOSHIFUJI Hideaki3dd4bc62005-12-19 14:02:45 -08001698 rt->rt6i_expires = jiffies + rt_expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 }
1700 }
1701 } else if (valid_lft) {
1702 addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
YOSHIFUJI Hideaki3dd4bc62005-12-19 14:02:45 -08001703 dev, jiffies_to_clock_t(rt_expires), RTF_ADDRCONF|RTF_EXPIRES|RTF_PREFIX_RT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 }
1705 if (rt)
1706 dst_release(&rt->u.dst);
1707 }
1708
1709 /* Try to figure out our local address for this prefix */
1710
1711 if (pinfo->autoconf && in6_dev->cnf.autoconf) {
1712 struct inet6_ifaddr * ifp;
1713 struct in6_addr addr;
1714 int create = 0, update_lft = 0;
1715
1716 if (pinfo->prefix_len == 64) {
1717 memcpy(&addr, &pinfo->prefix, 8);
1718 if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
1719 ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
1720 in6_dev_put(in6_dev);
1721 return;
1722 }
1723 goto ok;
1724 }
1725 if (net_ratelimit())
1726 printk(KERN_DEBUG "IPv6 addrconf: prefix with wrong length %d\n",
1727 pinfo->prefix_len);
1728 in6_dev_put(in6_dev);
1729 return;
1730
1731ok:
1732
1733 ifp = ipv6_get_ifaddr(&addr, dev, 1);
1734
1735 if (ifp == NULL && valid_lft) {
1736 int max_addresses = in6_dev->cnf.max_addresses;
1737
1738 /* Do not allow to create too much of autoconfigured
1739 * addresses; this would be too easy way to crash kernel.
1740 */
1741 if (!max_addresses ||
1742 ipv6_count_addresses(in6_dev) < max_addresses)
1743 ifp = ipv6_add_addr(in6_dev, &addr, pinfo->prefix_len,
1744 addr_type&IPV6_ADDR_SCOPE_MASK, 0);
1745
1746 if (!ifp || IS_ERR(ifp)) {
1747 in6_dev_put(in6_dev);
1748 return;
1749 }
1750
1751 update_lft = create = 1;
1752 ifp->cstamp = jiffies;
1753 addrconf_dad_start(ifp, RTF_ADDRCONF|RTF_PREFIX_RT);
1754 }
1755
1756 if (ifp) {
1757 int flags;
1758 unsigned long now;
1759#ifdef CONFIG_IPV6_PRIVACY
1760 struct inet6_ifaddr *ift;
1761#endif
1762 u32 stored_lft;
1763
1764 /* update lifetime (RFC2462 5.5.3 e) */
1765 spin_lock(&ifp->lock);
1766 now = jiffies;
1767 if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
1768 stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
1769 else
1770 stored_lft = 0;
1771 if (!update_lft && stored_lft) {
1772 if (valid_lft > MIN_VALID_LIFETIME ||
1773 valid_lft > stored_lft)
1774 update_lft = 1;
1775 else if (stored_lft <= MIN_VALID_LIFETIME) {
1776 /* valid_lft <= stored_lft is always true */
1777 /* XXX: IPsec */
1778 update_lft = 0;
1779 } else {
1780 valid_lft = MIN_VALID_LIFETIME;
1781 if (valid_lft < prefered_lft)
1782 prefered_lft = valid_lft;
1783 update_lft = 1;
1784 }
1785 }
1786
1787 if (update_lft) {
1788 ifp->valid_lft = valid_lft;
1789 ifp->prefered_lft = prefered_lft;
1790 ifp->tstamp = now;
1791 flags = ifp->flags;
1792 ifp->flags &= ~IFA_F_DEPRECATED;
1793 spin_unlock(&ifp->lock);
1794
1795 if (!(flags&IFA_F_TENTATIVE))
1796 ipv6_ifa_notify(0, ifp);
1797 } else
1798 spin_unlock(&ifp->lock);
1799
1800#ifdef CONFIG_IPV6_PRIVACY
1801 read_lock_bh(&in6_dev->lock);
1802 /* update all temporary addresses in the list */
1803 for (ift=in6_dev->tempaddr_list; ift; ift=ift->tmp_next) {
1804 /*
1805 * When adjusting the lifetimes of an existing
1806 * temporary address, only lower the lifetimes.
1807 * Implementations must not increase the
1808 * lifetimes of an existing temporary address
1809 * when processing a Prefix Information Option.
1810 */
1811 spin_lock(&ift->lock);
1812 flags = ift->flags;
1813 if (ift->valid_lft > valid_lft &&
1814 ift->valid_lft - valid_lft > (jiffies - ift->tstamp) / HZ)
1815 ift->valid_lft = valid_lft + (jiffies - ift->tstamp) / HZ;
1816 if (ift->prefered_lft > prefered_lft &&
1817 ift->prefered_lft - prefered_lft > (jiffies - ift->tstamp) / HZ)
1818 ift->prefered_lft = prefered_lft + (jiffies - ift->tstamp) / HZ;
1819 spin_unlock(&ift->lock);
1820 if (!(flags&IFA_F_TENTATIVE))
1821 ipv6_ifa_notify(0, ift);
1822 }
1823
1824 if (create && in6_dev->cnf.use_tempaddr > 0) {
1825 /*
1826 * When a new public address is created as described in [ADDRCONF],
1827 * also create a new temporary address.
1828 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001829 read_unlock_bh(&in6_dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 ipv6_create_tempaddr(ifp, NULL);
1831 } else {
1832 read_unlock_bh(&in6_dev->lock);
1833 }
1834#endif
1835 in6_ifa_put(ifp);
1836 addrconf_verify(0);
1837 }
1838 }
1839 inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
1840 in6_dev_put(in6_dev);
1841}
1842
1843/*
1844 * Set destination address.
1845 * Special case for SIT interfaces where we create a new "virtual"
1846 * device.
1847 */
1848int addrconf_set_dstaddr(void __user *arg)
1849{
1850 struct in6_ifreq ireq;
1851 struct net_device *dev;
1852 int err = -EINVAL;
1853
1854 rtnl_lock();
1855
1856 err = -EFAULT;
1857 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
1858 goto err_exit;
1859
1860 dev = __dev_get_by_index(ireq.ifr6_ifindex);
1861
1862 err = -ENODEV;
1863 if (dev == NULL)
1864 goto err_exit;
1865
Joerg Roedel0be669b2006-10-10 14:49:53 -07001866#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 if (dev->type == ARPHRD_SIT) {
1868 struct ifreq ifr;
1869 mm_segment_t oldfs;
1870 struct ip_tunnel_parm p;
1871
1872 err = -EADDRNOTAVAIL;
1873 if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4))
1874 goto err_exit;
1875
1876 memset(&p, 0, sizeof(p));
1877 p.iph.daddr = ireq.ifr6_addr.s6_addr32[3];
1878 p.iph.saddr = 0;
1879 p.iph.version = 4;
1880 p.iph.ihl = 5;
1881 p.iph.protocol = IPPROTO_IPV6;
1882 p.iph.ttl = 64;
1883 ifr.ifr_ifru.ifru_data = (void __user *)&p;
1884
1885 oldfs = get_fs(); set_fs(KERNEL_DS);
1886 err = dev->do_ioctl(dev, &ifr, SIOCADDTUNNEL);
1887 set_fs(oldfs);
1888
1889 if (err == 0) {
1890 err = -ENOBUFS;
1891 if ((dev = __dev_get_by_name(p.name)) == NULL)
1892 goto err_exit;
1893 err = dev_open(dev);
1894 }
1895 }
Joerg Roedel0be669b2006-10-10 14:49:53 -07001896#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897
1898err_exit:
1899 rtnl_unlock();
1900 return err;
1901}
1902
1903/*
1904 * Manual configuration of address on an interface
1905 */
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09001906static int inet6_addr_add(int ifindex, struct in6_addr *pfx, int plen,
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07001907 __u8 ifa_flags, __u32 prefered_lft, __u32 valid_lft)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908{
1909 struct inet6_ifaddr *ifp;
1910 struct inet6_dev *idev;
1911 struct net_device *dev;
1912 int scope;
1913
1914 ASSERT_RTNL();
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001915
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09001916 /* check the lifetime */
1917 if (!valid_lft || prefered_lft > valid_lft)
1918 return -EINVAL;
1919
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 if ((dev = __dev_get_by_index(ifindex)) == NULL)
1921 return -ENODEV;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001922
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 if ((idev = addrconf_add_dev(dev)) == NULL)
1924 return -ENOBUFS;
1925
1926 scope = ipv6_addr_scope(pfx);
1927
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09001928 if (valid_lft == INFINITY_LIFE_TIME)
1929 ifa_flags |= IFA_F_PERMANENT;
1930 else if (valid_lft >= 0x7FFFFFFF/HZ)
1931 valid_lft = 0x7FFFFFFF/HZ;
1932
1933 if (prefered_lft == 0)
1934 ifa_flags |= IFA_F_DEPRECATED;
1935 else if ((prefered_lft >= 0x7FFFFFFF/HZ) &&
1936 (prefered_lft != INFINITY_LIFE_TIME))
1937 prefered_lft = 0x7FFFFFFF/HZ;
1938
1939 ifp = ipv6_add_addr(idev, pfx, plen, scope, ifa_flags);
1940
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 if (!IS_ERR(ifp)) {
Herbert Xu06aebfb2006-08-09 16:52:04 -07001942 spin_lock_bh(&ifp->lock);
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09001943 ifp->valid_lft = valid_lft;
1944 ifp->prefered_lft = prefered_lft;
1945 ifp->tstamp = jiffies;
Herbert Xu06aebfb2006-08-09 16:52:04 -07001946 spin_unlock_bh(&ifp->lock);
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09001947
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 addrconf_dad_start(ifp, 0);
1949 in6_ifa_put(ifp);
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09001950 addrconf_verify(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 return 0;
1952 }
1953
1954 return PTR_ERR(ifp);
1955}
1956
1957static int inet6_addr_del(int ifindex, struct in6_addr *pfx, int plen)
1958{
1959 struct inet6_ifaddr *ifp;
1960 struct inet6_dev *idev;
1961 struct net_device *dev;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001962
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 if ((dev = __dev_get_by_index(ifindex)) == NULL)
1964 return -ENODEV;
1965
1966 if ((idev = __in6_dev_get(dev)) == NULL)
1967 return -ENXIO;
1968
1969 read_lock_bh(&idev->lock);
1970 for (ifp = idev->addr_list; ifp; ifp=ifp->if_next) {
1971 if (ifp->prefix_len == plen &&
1972 ipv6_addr_equal(pfx, &ifp->addr)) {
1973 in6_ifa_hold(ifp);
1974 read_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001975
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 ipv6_del_addr(ifp);
1977
1978 /* If the last address is deleted administratively,
1979 disable IPv6 on this interface.
1980 */
1981 if (idev->addr_list == NULL)
1982 addrconf_ifdown(idev->dev, 1);
1983 return 0;
1984 }
1985 }
1986 read_unlock_bh(&idev->lock);
1987 return -EADDRNOTAVAIL;
1988}
1989
1990
1991int addrconf_add_ifaddr(void __user *arg)
1992{
1993 struct in6_ifreq ireq;
1994 int err;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001995
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 if (!capable(CAP_NET_ADMIN))
1997 return -EPERM;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001998
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2000 return -EFAULT;
2001
2002 rtnl_lock();
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002003 err = inet6_addr_add(ireq.ifr6_ifindex, &ireq.ifr6_addr, ireq.ifr6_prefixlen,
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07002004 IFA_F_PERMANENT, INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 rtnl_unlock();
2006 return err;
2007}
2008
2009int addrconf_del_ifaddr(void __user *arg)
2010{
2011 struct in6_ifreq ireq;
2012 int err;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002013
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 if (!capable(CAP_NET_ADMIN))
2015 return -EPERM;
2016
2017 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2018 return -EFAULT;
2019
2020 rtnl_lock();
2021 err = inet6_addr_del(ireq.ifr6_ifindex, &ireq.ifr6_addr, ireq.ifr6_prefixlen);
2022 rtnl_unlock();
2023 return err;
2024}
2025
Joerg Roedel0be669b2006-10-10 14:49:53 -07002026#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027static void sit_add_v4_addrs(struct inet6_dev *idev)
2028{
2029 struct inet6_ifaddr * ifp;
2030 struct in6_addr addr;
2031 struct net_device *dev;
2032 int scope;
2033
2034 ASSERT_RTNL();
2035
2036 memset(&addr, 0, sizeof(struct in6_addr));
2037 memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
2038
2039 if (idev->dev->flags&IFF_POINTOPOINT) {
2040 addr.s6_addr32[0] = htonl(0xfe800000);
2041 scope = IFA_LINK;
2042 } else {
2043 scope = IPV6_ADDR_COMPATv4;
2044 }
2045
2046 if (addr.s6_addr32[3]) {
2047 ifp = ipv6_add_addr(idev, &addr, 128, scope, IFA_F_PERMANENT);
2048 if (!IS_ERR(ifp)) {
2049 spin_lock_bh(&ifp->lock);
2050 ifp->flags &= ~IFA_F_TENTATIVE;
2051 spin_unlock_bh(&ifp->lock);
2052 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2053 in6_ifa_put(ifp);
2054 }
2055 return;
2056 }
2057
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002058 for (dev = dev_base; dev != NULL; dev = dev->next) {
Herbert Xue5ed6392005-10-03 14:35:55 -07002059 struct in_device * in_dev = __in_dev_get_rtnl(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 if (in_dev && (dev->flags & IFF_UP)) {
2061 struct in_ifaddr * ifa;
2062
2063 int flag = scope;
2064
2065 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
2066 int plen;
2067
2068 addr.s6_addr32[3] = ifa->ifa_local;
2069
2070 if (ifa->ifa_scope == RT_SCOPE_LINK)
2071 continue;
2072 if (ifa->ifa_scope >= RT_SCOPE_HOST) {
2073 if (idev->dev->flags&IFF_POINTOPOINT)
2074 continue;
2075 flag |= IFA_HOST;
2076 }
2077 if (idev->dev->flags&IFF_POINTOPOINT)
2078 plen = 64;
2079 else
2080 plen = 96;
2081
2082 ifp = ipv6_add_addr(idev, &addr, plen, flag,
2083 IFA_F_PERMANENT);
2084 if (!IS_ERR(ifp)) {
2085 spin_lock_bh(&ifp->lock);
2086 ifp->flags &= ~IFA_F_TENTATIVE;
2087 spin_unlock_bh(&ifp->lock);
2088 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2089 in6_ifa_put(ifp);
2090 }
2091 }
2092 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002093 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094}
Joerg Roedel0be669b2006-10-10 14:49:53 -07002095#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096
2097static void init_loopback(struct net_device *dev)
2098{
2099 struct inet6_dev *idev;
2100 struct inet6_ifaddr * ifp;
2101
2102 /* ::1 */
2103
2104 ASSERT_RTNL();
2105
2106 if ((idev = ipv6_find_idev(dev)) == NULL) {
2107 printk(KERN_DEBUG "init loopback: add_dev failed\n");
2108 return;
2109 }
2110
2111 ifp = ipv6_add_addr(idev, &in6addr_loopback, 128, IFA_HOST, IFA_F_PERMANENT);
2112 if (!IS_ERR(ifp)) {
2113 spin_lock_bh(&ifp->lock);
2114 ifp->flags &= ~IFA_F_TENTATIVE;
2115 spin_unlock_bh(&ifp->lock);
2116 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2117 in6_ifa_put(ifp);
2118 }
2119}
2120
2121static void addrconf_add_linklocal(struct inet6_dev *idev, struct in6_addr *addr)
2122{
2123 struct inet6_ifaddr * ifp;
2124
2125 ifp = ipv6_add_addr(idev, addr, 64, IFA_LINK, IFA_F_PERMANENT);
2126 if (!IS_ERR(ifp)) {
2127 addrconf_dad_start(ifp, 0);
2128 in6_ifa_put(ifp);
2129 }
2130}
2131
2132static void addrconf_dev_config(struct net_device *dev)
2133{
2134 struct in6_addr addr;
2135 struct inet6_dev * idev;
2136
2137 ASSERT_RTNL();
2138
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002139 if ((dev->type != ARPHRD_ETHER) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 (dev->type != ARPHRD_FDDI) &&
2141 (dev->type != ARPHRD_IEEE802_TR) &&
2142 (dev->type != ARPHRD_ARCNET) &&
2143 (dev->type != ARPHRD_INFINIBAND)) {
2144 /* Alas, we support only Ethernet autoconfiguration. */
2145 return;
2146 }
2147
2148 idev = addrconf_add_dev(dev);
2149 if (idev == NULL)
2150 return;
2151
2152 memset(&addr, 0, sizeof(struct in6_addr));
2153 addr.s6_addr32[0] = htonl(0xFE800000);
2154
2155 if (ipv6_generate_eui64(addr.s6_addr + 8, dev) == 0)
2156 addrconf_add_linklocal(idev, &addr);
2157}
2158
Joerg Roedel0be669b2006-10-10 14:49:53 -07002159#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160static void addrconf_sit_config(struct net_device *dev)
2161{
2162 struct inet6_dev *idev;
2163
2164 ASSERT_RTNL();
2165
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002166 /*
2167 * Configure the tunnel with one of our IPv4
2168 * addresses... we should configure all of
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 * our v4 addrs in the tunnel
2170 */
2171
2172 if ((idev = ipv6_find_idev(dev)) == NULL) {
2173 printk(KERN_DEBUG "init sit: add_dev failed\n");
2174 return;
2175 }
2176
2177 sit_add_v4_addrs(idev);
2178
2179 if (dev->flags&IFF_POINTOPOINT) {
2180 addrconf_add_mroute(dev);
2181 addrconf_add_lroute(dev);
2182 } else
2183 sit_route_add(dev);
2184}
Joerg Roedel0be669b2006-10-10 14:49:53 -07002185#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186
2187static inline int
2188ipv6_inherit_linklocal(struct inet6_dev *idev, struct net_device *link_dev)
2189{
2190 struct in6_addr lladdr;
2191
2192 if (!ipv6_get_lladdr(link_dev, &lladdr)) {
2193 addrconf_add_linklocal(idev, &lladdr);
2194 return 0;
2195 }
2196 return -1;
2197}
2198
2199static void ip6_tnl_add_linklocal(struct inet6_dev *idev)
2200{
2201 struct net_device *link_dev;
2202
2203 /* first try to inherit the link-local address from the link device */
2204 if (idev->dev->iflink &&
2205 (link_dev = __dev_get_by_index(idev->dev->iflink))) {
2206 if (!ipv6_inherit_linklocal(idev, link_dev))
2207 return;
2208 }
2209 /* then try to inherit it from any device */
2210 for (link_dev = dev_base; link_dev; link_dev = link_dev->next) {
2211 if (!ipv6_inherit_linklocal(idev, link_dev))
2212 return;
2213 }
2214 printk(KERN_DEBUG "init ip6-ip6: add_linklocal failed\n");
2215}
2216
2217/*
2218 * Autoconfigure tunnel with a link-local address so routing protocols,
2219 * DHCPv6, MLD etc. can be run over the virtual link
2220 */
2221
2222static void addrconf_ip6_tnl_config(struct net_device *dev)
2223{
2224 struct inet6_dev *idev;
2225
2226 ASSERT_RTNL();
2227
2228 if ((idev = addrconf_add_dev(dev)) == NULL) {
2229 printk(KERN_DEBUG "init ip6-ip6: add_dev failed\n");
2230 return;
2231 }
2232 ip6_tnl_add_linklocal(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233}
2234
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002235static int addrconf_notify(struct notifier_block *this, unsigned long event,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 void * data)
2237{
2238 struct net_device *dev = (struct net_device *) data;
2239 struct inet6_dev *idev = __in6_dev_get(dev);
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09002240 int run_pending = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241
2242 switch(event) {
2243 case NETDEV_UP:
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002244 case NETDEV_CHANGE:
2245 if (event == NETDEV_UP) {
2246 if (!netif_carrier_ok(dev)) {
2247 /* device is not ready yet. */
2248 printk(KERN_INFO
2249 "ADDRCONF(NETDEV_UP): %s: "
2250 "link is not ready\n",
2251 dev->name);
2252 break;
2253 }
Kristian Slavov99081042006-02-08 16:10:53 -08002254
2255 if (idev)
2256 idev->if_flags |= IF_READY;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002257 } else {
2258 if (!netif_carrier_ok(dev)) {
2259 /* device is still not ready. */
2260 break;
2261 }
2262
2263 if (idev) {
2264 if (idev->if_flags & IF_READY) {
2265 /* device is already configured. */
2266 break;
2267 }
2268 idev->if_flags |= IF_READY;
2269 }
2270
2271 printk(KERN_INFO
2272 "ADDRCONF(NETDEV_CHANGE): %s: "
2273 "link becomes ready\n",
2274 dev->name);
2275
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09002276 run_pending = 1;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002277 }
2278
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 switch(dev->type) {
Joerg Roedel0be669b2006-10-10 14:49:53 -07002280#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 case ARPHRD_SIT:
2282 addrconf_sit_config(dev);
2283 break;
Joerg Roedel0be669b2006-10-10 14:49:53 -07002284#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 case ARPHRD_TUNNEL6:
2286 addrconf_ip6_tnl_config(dev);
2287 break;
2288 case ARPHRD_LOOPBACK:
2289 init_loopback(dev);
2290 break;
2291
2292 default:
2293 addrconf_dev_config(dev);
2294 break;
2295 };
2296 if (idev) {
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09002297 if (run_pending)
2298 addrconf_dad_run(idev);
2299
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 /* If the MTU changed during the interface down, when the
2301 interface up, the changed MTU must be reflected in the
2302 idev as well as routers.
2303 */
2304 if (idev->cnf.mtu6 != dev->mtu && dev->mtu >= IPV6_MIN_MTU) {
2305 rt6_mtu_change(dev, dev->mtu);
2306 idev->cnf.mtu6 = dev->mtu;
2307 }
2308 idev->tstamp = jiffies;
2309 inet6_ifinfo_notify(RTM_NEWLINK, idev);
2310 /* If the changed mtu during down is lower than IPV6_MIN_MTU
2311 stop IPv6 on this interface.
2312 */
2313 if (dev->mtu < IPV6_MIN_MTU)
2314 addrconf_ifdown(dev, event != NETDEV_DOWN);
2315 }
2316 break;
2317
2318 case NETDEV_CHANGEMTU:
2319 if ( idev && dev->mtu >= IPV6_MIN_MTU) {
2320 rt6_mtu_change(dev, dev->mtu);
2321 idev->cnf.mtu6 = dev->mtu;
2322 break;
2323 }
2324
2325 /* MTU falled under IPV6_MIN_MTU. Stop IPv6 on this interface. */
2326
2327 case NETDEV_DOWN:
2328 case NETDEV_UNREGISTER:
2329 /*
2330 * Remove all addresses from this interface.
2331 */
2332 addrconf_ifdown(dev, event != NETDEV_DOWN);
2333 break;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002334
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 case NETDEV_CHANGENAME:
2336#ifdef CONFIG_SYSCTL
2337 if (idev) {
2338 addrconf_sysctl_unregister(&idev->cnf);
2339 neigh_sysctl_unregister(idev->nd_parms);
2340 neigh_sysctl_register(dev, idev->nd_parms,
2341 NET_IPV6, NET_IPV6_NEIGH, "ipv6",
2342 &ndisc_ifinfo_sysctl_change,
2343 NULL);
2344 addrconf_sysctl_register(idev, &idev->cnf);
2345 }
2346#endif
2347 break;
2348 };
2349
2350 return NOTIFY_OK;
2351}
2352
2353/*
2354 * addrconf module should be notified of a device going up
2355 */
2356static struct notifier_block ipv6_dev_notf = {
2357 .notifier_call = addrconf_notify,
2358 .priority = 0
2359};
2360
2361static int addrconf_ifdown(struct net_device *dev, int how)
2362{
2363 struct inet6_dev *idev;
2364 struct inet6_ifaddr *ifa, **bifa;
2365 int i;
2366
2367 ASSERT_RTNL();
2368
2369 if (dev == &loopback_dev && how == 1)
2370 how = 0;
2371
2372 rt6_ifdown(dev);
2373 neigh_ifdown(&nd_tbl, dev);
2374
2375 idev = __in6_dev_get(dev);
2376 if (idev == NULL)
2377 return -ENODEV;
2378
2379 /* Step 1: remove reference to ipv6 device from parent device.
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002380 Do not dev_put!
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 */
2382 if (how == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 idev->dead = 1;
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07002384
2385 /* protected by rtnl_lock */
2386 rcu_assign_pointer(dev->ip6_ptr, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387
2388 /* Step 1.5: remove snmp6 entry */
2389 snmp6_unregister_dev(idev);
2390
2391 }
2392
2393 /* Step 2: clear hash table */
2394 for (i=0; i<IN6_ADDR_HSIZE; i++) {
2395 bifa = &inet6_addr_lst[i];
2396
2397 write_lock_bh(&addrconf_hash_lock);
2398 while ((ifa = *bifa) != NULL) {
2399 if (ifa->idev == idev) {
2400 *bifa = ifa->lst_next;
2401 ifa->lst_next = NULL;
2402 addrconf_del_timer(ifa);
2403 in6_ifa_put(ifa);
2404 continue;
2405 }
2406 bifa = &ifa->lst_next;
2407 }
2408 write_unlock_bh(&addrconf_hash_lock);
2409 }
2410
2411 write_lock_bh(&idev->lock);
2412
2413 /* Step 3: clear flags for stateless addrconf */
2414 if (how != 1)
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002415 idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416
2417 /* Step 4: clear address list */
2418#ifdef CONFIG_IPV6_PRIVACY
2419 if (how == 1 && del_timer(&idev->regen_timer))
2420 in6_dev_put(idev);
2421
2422 /* clear tempaddr list */
2423 while ((ifa = idev->tempaddr_list) != NULL) {
2424 idev->tempaddr_list = ifa->tmp_next;
2425 ifa->tmp_next = NULL;
2426 ifa->dead = 1;
2427 write_unlock_bh(&idev->lock);
2428 spin_lock_bh(&ifa->lock);
2429
2430 if (ifa->ifpub) {
2431 in6_ifa_put(ifa->ifpub);
2432 ifa->ifpub = NULL;
2433 }
2434 spin_unlock_bh(&ifa->lock);
2435 in6_ifa_put(ifa);
2436 write_lock_bh(&idev->lock);
2437 }
2438#endif
2439 while ((ifa = idev->addr_list) != NULL) {
2440 idev->addr_list = ifa->if_next;
2441 ifa->if_next = NULL;
2442 ifa->dead = 1;
2443 addrconf_del_timer(ifa);
2444 write_unlock_bh(&idev->lock);
2445
2446 __ipv6_ifa_notify(RTM_DELADDR, ifa);
2447 in6_ifa_put(ifa);
2448
2449 write_lock_bh(&idev->lock);
2450 }
2451 write_unlock_bh(&idev->lock);
2452
2453 /* Step 5: Discard multicast list */
2454
2455 if (how == 1)
2456 ipv6_mc_destroy_dev(idev);
2457 else
2458 ipv6_mc_down(idev);
2459
2460 /* Step 5: netlink notification of this interface */
2461 idev->tstamp = jiffies;
Yan Zheng979ad662005-10-14 18:31:15 +08002462 inet6_ifinfo_notify(RTM_DELLINK, idev);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002463
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 /* Shot the device (if unregistered) */
2465
2466 if (how == 1) {
2467#ifdef CONFIG_SYSCTL
2468 addrconf_sysctl_unregister(&idev->cnf);
2469 neigh_sysctl_unregister(idev->nd_parms);
2470#endif
2471 neigh_parms_release(&nd_tbl, idev->nd_parms);
2472 neigh_ifdown(&nd_tbl, dev);
2473 in6_dev_put(idev);
2474 }
2475 return 0;
2476}
2477
2478static void addrconf_rs_timer(unsigned long data)
2479{
2480 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
2481
2482 if (ifp->idev->cnf.forwarding)
2483 goto out;
2484
2485 if (ifp->idev->if_flags & IF_RA_RCVD) {
2486 /*
2487 * Announcement received after solicitation
2488 * was sent
2489 */
2490 goto out;
2491 }
2492
2493 spin_lock(&ifp->lock);
2494 if (ifp->probes++ < ifp->idev->cnf.rtr_solicits) {
2495 struct in6_addr all_routers;
2496
2497 /* The wait after the last probe can be shorter */
2498 addrconf_mod_timer(ifp, AC_RS,
2499 (ifp->probes == ifp->idev->cnf.rtr_solicits) ?
2500 ifp->idev->cnf.rtr_solicit_delay :
2501 ifp->idev->cnf.rtr_solicit_interval);
2502 spin_unlock(&ifp->lock);
2503
2504 ipv6_addr_all_routers(&all_routers);
2505
2506 ndisc_send_rs(ifp->idev->dev, &ifp->addr, &all_routers);
2507 } else {
2508 spin_unlock(&ifp->lock);
2509 /*
2510 * Note: we do not support deprecated "all on-link"
2511 * assumption any longer.
2512 */
2513 printk(KERN_DEBUG "%s: no IPv6 routers present\n",
2514 ifp->idev->dev->name);
2515 }
2516
2517out:
2518 in6_ifa_put(ifp);
2519}
2520
2521/*
2522 * Duplicate Address Detection
2523 */
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002524static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
2525{
2526 unsigned long rand_num;
2527 struct inet6_dev *idev = ifp->idev;
2528
2529 rand_num = net_random() % (idev->cnf.rtr_solicit_delay ? : 1);
2530 ifp->probes = idev->cnf.dad_transmits;
2531 addrconf_mod_timer(ifp, AC_DAD, rand_num);
2532}
2533
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -07002534static void addrconf_dad_start(struct inet6_ifaddr *ifp, u32 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535{
2536 struct inet6_dev *idev = ifp->idev;
2537 struct net_device *dev = idev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538
2539 addrconf_join_solict(dev, &ifp->addr);
2540
2541 if (ifp->prefix_len != 128 && (ifp->flags&IFA_F_PERMANENT))
2542 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev, 0,
2543 flags);
2544
2545 net_srandom(ifp->addr.s6_addr32[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546
2547 read_lock_bh(&idev->lock);
2548 if (ifp->dead)
2549 goto out;
2550 spin_lock_bh(&ifp->lock);
2551
2552 if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07002553 !(ifp->flags&IFA_F_TENTATIVE) ||
2554 ifp->flags & IFA_F_NODAD) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 ifp->flags &= ~IFA_F_TENTATIVE;
2556 spin_unlock_bh(&ifp->lock);
2557 read_unlock_bh(&idev->lock);
2558
2559 addrconf_dad_completed(ifp);
2560 return;
2561 }
2562
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08002563 if (!(idev->if_flags & IF_READY)) {
YOSHIFUJI Hideaki3dd3bf82005-12-23 11:23:21 -08002564 spin_unlock_bh(&ifp->lock);
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08002565 read_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002566 /*
2567 * If the defice is not ready:
2568 * - keep it tentative if it is a permanent address.
2569 * - otherwise, kill it.
2570 */
2571 in6_ifa_hold(ifp);
2572 addrconf_dad_stop(ifp);
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08002573 return;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002574 }
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08002575 addrconf_dad_kick(ifp);
2576 spin_unlock_bh(&ifp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577out:
2578 read_unlock_bh(&idev->lock);
2579}
2580
2581static void addrconf_dad_timer(unsigned long data)
2582{
2583 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
2584 struct inet6_dev *idev = ifp->idev;
2585 struct in6_addr unspec;
2586 struct in6_addr mcaddr;
2587
2588 read_lock_bh(&idev->lock);
2589 if (idev->dead) {
2590 read_unlock_bh(&idev->lock);
2591 goto out;
2592 }
2593 spin_lock_bh(&ifp->lock);
2594 if (ifp->probes == 0) {
2595 /*
2596 * DAD was successful
2597 */
2598
2599 ifp->flags &= ~IFA_F_TENTATIVE;
2600 spin_unlock_bh(&ifp->lock);
2601 read_unlock_bh(&idev->lock);
2602
2603 addrconf_dad_completed(ifp);
2604
2605 goto out;
2606 }
2607
2608 ifp->probes--;
2609 addrconf_mod_timer(ifp, AC_DAD, ifp->idev->nd_parms->retrans_time);
2610 spin_unlock_bh(&ifp->lock);
2611 read_unlock_bh(&idev->lock);
2612
2613 /* send a neighbour solicitation for our addr */
2614 memset(&unspec, 0, sizeof(unspec));
2615 addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
2616 ndisc_send_ns(ifp->idev->dev, NULL, &ifp->addr, &mcaddr, &unspec);
2617out:
2618 in6_ifa_put(ifp);
2619}
2620
2621static void addrconf_dad_completed(struct inet6_ifaddr *ifp)
2622{
2623 struct net_device * dev = ifp->idev->dev;
2624
2625 /*
2626 * Configure the address for reception. Now it is valid.
2627 */
2628
2629 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2630
2631 /* If added prefix is link local and forwarding is off,
2632 start sending router solicitations.
2633 */
2634
2635 if (ifp->idev->cnf.forwarding == 0 &&
2636 ifp->idev->cnf.rtr_solicits > 0 &&
2637 (dev->flags&IFF_LOOPBACK) == 0 &&
2638 (ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL)) {
2639 struct in6_addr all_routers;
2640
2641 ipv6_addr_all_routers(&all_routers);
2642
2643 /*
2644 * If a host as already performed a random delay
2645 * [...] as part of DAD [...] there is no need
2646 * to delay again before sending the first RS
2647 */
2648 ndisc_send_rs(ifp->idev->dev, &ifp->addr, &all_routers);
2649
2650 spin_lock_bh(&ifp->lock);
2651 ifp->probes = 1;
2652 ifp->idev->if_flags |= IF_RS_SENT;
2653 addrconf_mod_timer(ifp, AC_RS, ifp->idev->cnf.rtr_solicit_interval);
2654 spin_unlock_bh(&ifp->lock);
2655 }
2656}
2657
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09002658static void addrconf_dad_run(struct inet6_dev *idev) {
2659 struct inet6_ifaddr *ifp;
2660
2661 read_lock_bh(&idev->lock);
2662 for (ifp = idev->addr_list; ifp; ifp = ifp->if_next) {
2663 spin_lock_bh(&ifp->lock);
2664 if (!(ifp->flags & IFA_F_TENTATIVE)) {
2665 spin_unlock_bh(&ifp->lock);
2666 continue;
2667 }
2668 spin_unlock_bh(&ifp->lock);
2669 addrconf_dad_kick(ifp);
2670 }
2671 read_unlock_bh(&idev->lock);
2672}
2673
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674#ifdef CONFIG_PROC_FS
2675struct if6_iter_state {
2676 int bucket;
2677};
2678
2679static struct inet6_ifaddr *if6_get_first(struct seq_file *seq)
2680{
2681 struct inet6_ifaddr *ifa = NULL;
2682 struct if6_iter_state *state = seq->private;
2683
2684 for (state->bucket = 0; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
2685 ifa = inet6_addr_lst[state->bucket];
2686 if (ifa)
2687 break;
2688 }
2689 return ifa;
2690}
2691
2692static struct inet6_ifaddr *if6_get_next(struct seq_file *seq, struct inet6_ifaddr *ifa)
2693{
2694 struct if6_iter_state *state = seq->private;
2695
2696 ifa = ifa->lst_next;
2697try_again:
2698 if (!ifa && ++state->bucket < IN6_ADDR_HSIZE) {
2699 ifa = inet6_addr_lst[state->bucket];
2700 goto try_again;
2701 }
2702 return ifa;
2703}
2704
2705static struct inet6_ifaddr *if6_get_idx(struct seq_file *seq, loff_t pos)
2706{
2707 struct inet6_ifaddr *ifa = if6_get_first(seq);
2708
2709 if (ifa)
2710 while(pos && (ifa = if6_get_next(seq, ifa)) != NULL)
2711 --pos;
2712 return pos ? NULL : ifa;
2713}
2714
2715static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
2716{
2717 read_lock_bh(&addrconf_hash_lock);
2718 return if6_get_idx(seq, *pos);
2719}
2720
2721static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2722{
2723 struct inet6_ifaddr *ifa;
2724
2725 ifa = if6_get_next(seq, v);
2726 ++*pos;
2727 return ifa;
2728}
2729
2730static void if6_seq_stop(struct seq_file *seq, void *v)
2731{
2732 read_unlock_bh(&addrconf_hash_lock);
2733}
2734
2735static int if6_seq_show(struct seq_file *seq, void *v)
2736{
2737 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
2738 seq_printf(seq,
YOSHIFUJI Hideaki9343e792006-01-17 02:10:53 -08002739 NIP6_SEQFMT " %02x %02x %02x %02x %8s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 NIP6(ifp->addr),
2741 ifp->idev->dev->ifindex,
2742 ifp->prefix_len,
2743 ifp->scope,
2744 ifp->flags,
2745 ifp->idev->dev->name);
2746 return 0;
2747}
2748
2749static struct seq_operations if6_seq_ops = {
2750 .start = if6_seq_start,
2751 .next = if6_seq_next,
2752 .show = if6_seq_show,
2753 .stop = if6_seq_stop,
2754};
2755
2756static int if6_seq_open(struct inode *inode, struct file *file)
2757{
2758 struct seq_file *seq;
2759 int rc = -ENOMEM;
Ingo Oeser322f74a2006-03-20 23:01:47 -08002760 struct if6_iter_state *s = kzalloc(sizeof(*s), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761
2762 if (!s)
2763 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764
2765 rc = seq_open(file, &if6_seq_ops);
2766 if (rc)
2767 goto out_kfree;
2768
2769 seq = file->private_data;
2770 seq->private = s;
2771out:
2772 return rc;
2773out_kfree:
2774 kfree(s);
2775 goto out;
2776}
2777
Arjan van de Ven9a321442007-02-12 00:55:35 -08002778static const struct file_operations if6_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 .owner = THIS_MODULE,
2780 .open = if6_seq_open,
2781 .read = seq_read,
2782 .llseek = seq_lseek,
2783 .release = seq_release_private,
2784};
2785
2786int __init if6_proc_init(void)
2787{
2788 if (!proc_net_fops_create("if_inet6", S_IRUGO, &if6_fops))
2789 return -ENOMEM;
2790 return 0;
2791}
2792
2793void if6_proc_exit(void)
2794{
2795 proc_net_remove("if_inet6");
2796}
2797#endif /* CONFIG_PROC_FS */
2798
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07002799#ifdef CONFIG_IPV6_MIP6
2800/* Check if address is a home address configured on any interface. */
2801int ipv6_chk_home_addr(struct in6_addr *addr)
2802{
2803 int ret = 0;
2804 struct inet6_ifaddr * ifp;
2805 u8 hash = ipv6_addr_hash(addr);
2806 read_lock_bh(&addrconf_hash_lock);
2807 for (ifp = inet6_addr_lst[hash]; ifp; ifp = ifp->lst_next) {
2808 if (ipv6_addr_cmp(&ifp->addr, addr) == 0 &&
2809 (ifp->flags & IFA_F_HOMEADDRESS)) {
2810 ret = 1;
2811 break;
2812 }
2813 }
2814 read_unlock_bh(&addrconf_hash_lock);
2815 return ret;
2816}
2817#endif
2818
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819/*
2820 * Periodic address status verification
2821 */
2822
2823static void addrconf_verify(unsigned long foo)
2824{
2825 struct inet6_ifaddr *ifp;
2826 unsigned long now, next;
2827 int i;
2828
2829 spin_lock_bh(&addrconf_verify_lock);
2830 now = jiffies;
2831 next = now + ADDR_CHECK_FREQUENCY;
2832
2833 del_timer(&addr_chk_timer);
2834
2835 for (i=0; i < IN6_ADDR_HSIZE; i++) {
2836
2837restart:
Yan Zheng5d5780d2005-11-20 13:42:20 -08002838 read_lock(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 for (ifp=inet6_addr_lst[i]; ifp; ifp=ifp->lst_next) {
2840 unsigned long age;
2841#ifdef CONFIG_IPV6_PRIVACY
2842 unsigned long regen_advance;
2843#endif
2844
2845 if (ifp->flags & IFA_F_PERMANENT)
2846 continue;
2847
2848 spin_lock(&ifp->lock);
2849 age = (now - ifp->tstamp) / HZ;
2850
2851#ifdef CONFIG_IPV6_PRIVACY
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002852 regen_advance = ifp->idev->cnf.regen_max_retry *
2853 ifp->idev->cnf.dad_transmits *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 ifp->idev->nd_parms->retrans_time / HZ;
2855#endif
2856
YOSHIFUJI Hideaki8f27ebb2006-07-28 18:12:11 +09002857 if (ifp->valid_lft != INFINITY_LIFE_TIME &&
2858 age >= ifp->valid_lft) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 spin_unlock(&ifp->lock);
2860 in6_ifa_hold(ifp);
Yan Zheng5d5780d2005-11-20 13:42:20 -08002861 read_unlock(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 ipv6_del_addr(ifp);
2863 goto restart;
YOSHIFUJI Hideaki8f27ebb2006-07-28 18:12:11 +09002864 } else if (ifp->prefered_lft == INFINITY_LIFE_TIME) {
2865 spin_unlock(&ifp->lock);
2866 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 } else if (age >= ifp->prefered_lft) {
2868 /* jiffies - ifp->tsamp > age >= ifp->prefered_lft */
2869 int deprecate = 0;
2870
2871 if (!(ifp->flags&IFA_F_DEPRECATED)) {
2872 deprecate = 1;
2873 ifp->flags |= IFA_F_DEPRECATED;
2874 }
2875
2876 if (time_before(ifp->tstamp + ifp->valid_lft * HZ, next))
2877 next = ifp->tstamp + ifp->valid_lft * HZ;
2878
2879 spin_unlock(&ifp->lock);
2880
2881 if (deprecate) {
2882 in6_ifa_hold(ifp);
Yan Zheng5d5780d2005-11-20 13:42:20 -08002883 read_unlock(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884
2885 ipv6_ifa_notify(0, ifp);
2886 in6_ifa_put(ifp);
2887 goto restart;
2888 }
2889#ifdef CONFIG_IPV6_PRIVACY
2890 } else if ((ifp->flags&IFA_F_TEMPORARY) &&
2891 !(ifp->flags&IFA_F_TENTATIVE)) {
2892 if (age >= ifp->prefered_lft - regen_advance) {
2893 struct inet6_ifaddr *ifpub = ifp->ifpub;
2894 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
2895 next = ifp->tstamp + ifp->prefered_lft * HZ;
2896 if (!ifp->regen_count && ifpub) {
2897 ifp->regen_count++;
2898 in6_ifa_hold(ifp);
2899 in6_ifa_hold(ifpub);
2900 spin_unlock(&ifp->lock);
Yan Zheng5d5780d2005-11-20 13:42:20 -08002901 read_unlock(&addrconf_hash_lock);
Hiroyuki YAMAMORI291d8092005-12-23 11:24:05 -08002902 spin_lock(&ifpub->lock);
2903 ifpub->regen_count = 0;
2904 spin_unlock(&ifpub->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 ipv6_create_tempaddr(ifpub, ifp);
2906 in6_ifa_put(ifpub);
2907 in6_ifa_put(ifp);
2908 goto restart;
2909 }
2910 } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
2911 next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
2912 spin_unlock(&ifp->lock);
2913#endif
2914 } else {
2915 /* ifp->prefered_lft <= ifp->valid_lft */
2916 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
2917 next = ifp->tstamp + ifp->prefered_lft * HZ;
2918 spin_unlock(&ifp->lock);
2919 }
2920 }
Yan Zheng5d5780d2005-11-20 13:42:20 -08002921 read_unlock(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 }
2923
2924 addr_chk_timer.expires = time_before(next, jiffies + HZ) ? jiffies + HZ : next;
2925 add_timer(&addr_chk_timer);
2926 spin_unlock_bh(&addrconf_verify_lock);
2927}
2928
Thomas Graf461d8832006-09-18 00:09:49 -07002929static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local)
2930{
2931 struct in6_addr *pfx = NULL;
2932
2933 if (addr)
2934 pfx = nla_data(addr);
2935
2936 if (local) {
2937 if (pfx && nla_memcmp(local, pfx, sizeof(*pfx)))
2938 pfx = NULL;
2939 else
2940 pfx = nla_data(local);
2941 }
2942
2943 return pfx;
2944}
2945
2946static struct nla_policy ifa_ipv6_policy[IFA_MAX+1] __read_mostly = {
2947 [IFA_ADDRESS] = { .len = sizeof(struct in6_addr) },
2948 [IFA_LOCAL] = { .len = sizeof(struct in6_addr) },
2949 [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) },
2950};
2951
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952static int
2953inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
2954{
Thomas Grafb933f712006-09-18 00:10:19 -07002955 struct ifaddrmsg *ifm;
2956 struct nlattr *tb[IFA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 struct in6_addr *pfx;
Thomas Grafb933f712006-09-18 00:10:19 -07002958 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959
Thomas Grafb933f712006-09-18 00:10:19 -07002960 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
2961 if (err < 0)
2962 return err;
2963
2964 ifm = nlmsg_data(nlh);
2965 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 if (pfx == NULL)
2967 return -EINVAL;
2968
2969 return inet6_addr_del(ifm->ifa_index, pfx, ifm->ifa_prefixlen);
2970}
2971
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07002972static int inet6_addr_modify(struct inet6_ifaddr *ifp, u8 ifa_flags,
2973 u32 prefered_lft, u32 valid_lft)
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09002974{
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09002975 if (!valid_lft || (prefered_lft > valid_lft))
2976 return -EINVAL;
2977
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09002978 if (valid_lft == INFINITY_LIFE_TIME)
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07002979 ifa_flags |= IFA_F_PERMANENT;
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09002980 else if (valid_lft >= 0x7FFFFFFF/HZ)
2981 valid_lft = 0x7FFFFFFF/HZ;
2982
2983 if (prefered_lft == 0)
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07002984 ifa_flags |= IFA_F_DEPRECATED;
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09002985 else if ((prefered_lft >= 0x7FFFFFFF/HZ) &&
2986 (prefered_lft != INFINITY_LIFE_TIME))
2987 prefered_lft = 0x7FFFFFFF/HZ;
2988
2989 spin_lock_bh(&ifp->lock);
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07002990 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 +09002991 ifp->tstamp = jiffies;
2992 ifp->valid_lft = valid_lft;
2993 ifp->prefered_lft = prefered_lft;
2994
2995 spin_unlock_bh(&ifp->lock);
2996 if (!(ifp->flags&IFA_F_TENTATIVE))
2997 ipv6_ifa_notify(0, ifp);
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09002998
2999 addrconf_verify(0);
3000
3001 return 0;
3002}
3003
3004static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
3006{
Thomas Graf461d8832006-09-18 00:09:49 -07003007 struct ifaddrmsg *ifm;
3008 struct nlattr *tb[IFA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 struct in6_addr *pfx;
Thomas Graf7198f8c2006-09-18 00:13:46 -07003010 struct inet6_ifaddr *ifa;
3011 struct net_device *dev;
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003012 u32 valid_lft = INFINITY_LIFE_TIME, preferred_lft = INFINITY_LIFE_TIME;
3013 u8 ifa_flags;
Thomas Graf461d8832006-09-18 00:09:49 -07003014 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015
Thomas Graf461d8832006-09-18 00:09:49 -07003016 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
3017 if (err < 0)
3018 return err;
3019
3020 ifm = nlmsg_data(nlh);
3021 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 if (pfx == NULL)
3023 return -EINVAL;
3024
Thomas Graf461d8832006-09-18 00:09:49 -07003025 if (tb[IFA_CACHEINFO]) {
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09003026 struct ifa_cacheinfo *ci;
Thomas Graf461d8832006-09-18 00:09:49 -07003027
3028 ci = nla_data(tb[IFA_CACHEINFO]);
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09003029 valid_lft = ci->ifa_valid;
Thomas Graf461d8832006-09-18 00:09:49 -07003030 preferred_lft = ci->ifa_prefered;
3031 } else {
3032 preferred_lft = INFINITY_LIFE_TIME;
3033 valid_lft = INFINITY_LIFE_TIME;
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09003034 }
3035
Thomas Graf7198f8c2006-09-18 00:13:46 -07003036 dev = __dev_get_by_index(ifm->ifa_index);
3037 if (dev == NULL)
3038 return -ENODEV;
3039
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003040 /* We ignore other flags so far. */
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07003041 ifa_flags = ifm->ifa_flags & (IFA_F_NODAD | IFA_F_HOMEADDRESS);
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003042
Thomas Graf7198f8c2006-09-18 00:13:46 -07003043 ifa = ipv6_get_ifaddr(pfx, dev, 1);
3044 if (ifa == NULL) {
3045 /*
3046 * It would be best to check for !NLM_F_CREATE here but
3047 * userspace alreay relies on not having to provide this.
3048 */
3049 return inet6_addr_add(ifm->ifa_index, pfx, ifm->ifa_prefixlen,
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003050 ifa_flags, preferred_lft, valid_lft);
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09003051 }
3052
Thomas Graf7198f8c2006-09-18 00:13:46 -07003053 if (nlh->nlmsg_flags & NLM_F_EXCL ||
3054 !(nlh->nlmsg_flags & NLM_F_REPLACE))
3055 err = -EEXIST;
3056 else
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003057 err = inet6_addr_modify(ifa, ifa_flags, preferred_lft, valid_lft);
Thomas Graf7198f8c2006-09-18 00:13:46 -07003058
3059 in6_ifa_put(ifa);
3060
3061 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062}
3063
Thomas Graf101bb222006-09-18 00:11:52 -07003064static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u8 flags,
3065 u8 scope, int ifindex)
3066{
3067 struct ifaddrmsg *ifm;
3068
3069 ifm = nlmsg_data(nlh);
3070 ifm->ifa_family = AF_INET6;
3071 ifm->ifa_prefixlen = prefixlen;
3072 ifm->ifa_flags = flags;
3073 ifm->ifa_scope = scope;
3074 ifm->ifa_index = ifindex;
3075}
3076
Thomas Graf85486af2006-09-18 00:11:24 -07003077static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
3078 unsigned long tstamp, u32 preferred, u32 valid)
3079{
3080 struct ifa_cacheinfo ci;
3081
3082 ci.cstamp = (u32)(TIME_DELTA(cstamp, INITIAL_JIFFIES) / HZ * 100
3083 + TIME_DELTA(cstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
3084 ci.tstamp = (u32)(TIME_DELTA(tstamp, INITIAL_JIFFIES) / HZ * 100
3085 + TIME_DELTA(tstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
3086 ci.ifa_prefered = preferred;
3087 ci.ifa_valid = valid;
3088
3089 return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
3090}
3091
Thomas Graf101bb222006-09-18 00:11:52 -07003092static inline int rt_scope(int ifa_scope)
3093{
3094 if (ifa_scope & IFA_HOST)
3095 return RT_SCOPE_HOST;
3096 else if (ifa_scope & IFA_LINK)
3097 return RT_SCOPE_LINK;
3098 else if (ifa_scope & IFA_SITE)
3099 return RT_SCOPE_SITE;
3100 else
3101 return RT_SCOPE_UNIVERSE;
3102}
3103
Thomas Graf0ab68032006-09-18 00:12:35 -07003104static inline int inet6_ifaddr_msgsize(void)
3105{
Thomas Graf339bf982006-11-10 14:10:15 -08003106 return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
3107 + nla_total_size(16) /* IFA_ADDRESS */
3108 + nla_total_size(sizeof(struct ifa_cacheinfo));
Thomas Graf0ab68032006-09-18 00:12:35 -07003109}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07003110
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07003112 u32 pid, u32 seq, int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114 struct nlmsghdr *nlh;
Thomas Graf85486af2006-09-18 00:11:24 -07003115 u32 preferred, valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116
Thomas Graf0ab68032006-09-18 00:12:35 -07003117 nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags);
3118 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08003119 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07003120
Thomas Graf101bb222006-09-18 00:11:52 -07003121 put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope),
3122 ifa->idev->dev->ifindex);
3123
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 if (!(ifa->flags&IFA_F_PERMANENT)) {
Thomas Graf85486af2006-09-18 00:11:24 -07003125 preferred = ifa->prefered_lft;
3126 valid = ifa->valid_lft;
3127 if (preferred != INFINITY_LIFE_TIME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 long tval = (jiffies - ifa->tstamp)/HZ;
Thomas Graf85486af2006-09-18 00:11:24 -07003129 preferred -= tval;
3130 if (valid != INFINITY_LIFE_TIME)
3131 valid -= tval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132 }
3133 } else {
Thomas Graf85486af2006-09-18 00:11:24 -07003134 preferred = INFINITY_LIFE_TIME;
3135 valid = INFINITY_LIFE_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 }
Thomas Graf85486af2006-09-18 00:11:24 -07003137
Thomas Graf0ab68032006-09-18 00:12:35 -07003138 if (nla_put(skb, IFA_ADDRESS, 16, &ifa->addr) < 0 ||
Patrick McHardy26932562007-01-31 23:16:40 -08003139 put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0) {
3140 nlmsg_cancel(skb, nlh);
3141 return -EMSGSIZE;
3142 }
Thomas Graf85486af2006-09-18 00:11:24 -07003143
Thomas Graf0ab68032006-09-18 00:12:35 -07003144 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145}
3146
3147static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -07003148 u32 pid, u32 seq, int event, u16 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 struct nlmsghdr *nlh;
Thomas Graf101bb222006-09-18 00:11:52 -07003151 u8 scope = RT_SCOPE_UNIVERSE;
3152 int ifindex = ifmca->idev->dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153
Thomas Graf101bb222006-09-18 00:11:52 -07003154 if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE)
3155 scope = RT_SCOPE_SITE;
3156
Thomas Graf0ab68032006-09-18 00:12:35 -07003157 nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags);
3158 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08003159 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07003160
Thomas Graf101bb222006-09-18 00:11:52 -07003161 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
Thomas Graf0ab68032006-09-18 00:12:35 -07003162 if (nla_put(skb, IFA_MULTICAST, 16, &ifmca->mca_addr) < 0 ||
3163 put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp,
Patrick McHardy26932562007-01-31 23:16:40 -08003164 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
3165 nlmsg_cancel(skb, nlh);
3166 return -EMSGSIZE;
3167 }
Thomas Graf85486af2006-09-18 00:11:24 -07003168
Thomas Graf0ab68032006-09-18 00:12:35 -07003169 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170}
3171
3172static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07003173 u32 pid, u32 seq, int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175 struct nlmsghdr *nlh;
Thomas Graf101bb222006-09-18 00:11:52 -07003176 u8 scope = RT_SCOPE_UNIVERSE;
3177 int ifindex = ifaca->aca_idev->dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178
Thomas Graf101bb222006-09-18 00:11:52 -07003179 if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE)
3180 scope = RT_SCOPE_SITE;
3181
Thomas Graf0ab68032006-09-18 00:12:35 -07003182 nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags);
3183 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08003184 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07003185
Thomas Graf101bb222006-09-18 00:11:52 -07003186 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
Thomas Graf0ab68032006-09-18 00:12:35 -07003187 if (nla_put(skb, IFA_ANYCAST, 16, &ifaca->aca_addr) < 0 ||
3188 put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp,
Patrick McHardy26932562007-01-31 23:16:40 -08003189 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
3190 nlmsg_cancel(skb, nlh);
3191 return -EMSGSIZE;
3192 }
Thomas Graf85486af2006-09-18 00:11:24 -07003193
Thomas Graf0ab68032006-09-18 00:12:35 -07003194 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195}
3196
3197enum addr_type_t
3198{
3199 UNICAST_ADDR,
3200 MULTICAST_ADDR,
3201 ANYCAST_ADDR,
3202};
3203
3204static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
3205 enum addr_type_t type)
3206{
3207 int idx, ip_idx;
3208 int s_idx, s_ip_idx;
3209 int err = 1;
3210 struct net_device *dev;
3211 struct inet6_dev *idev = NULL;
3212 struct inet6_ifaddr *ifa;
3213 struct ifmcaddr6 *ifmca;
3214 struct ifacaddr6 *ifaca;
3215
3216 s_idx = cb->args[0];
3217 s_ip_idx = ip_idx = cb->args[1];
3218 read_lock(&dev_base_lock);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003219
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 for (dev = dev_base, idx = 0; dev; dev = dev->next, idx++) {
3221 if (idx < s_idx)
3222 continue;
3223 if (idx > s_idx)
3224 s_ip_idx = 0;
3225 ip_idx = 0;
3226 if ((idev = in6_dev_get(dev)) == NULL)
3227 continue;
3228 read_lock_bh(&idev->lock);
3229 switch (type) {
3230 case UNICAST_ADDR:
YOSHIFUJI Hideakiae9cda52005-06-28 13:00:30 -07003231 /* unicast address incl. temp addr */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 for (ifa = idev->addr_list; ifa;
3233 ifa = ifa->if_next, ip_idx++) {
3234 if (ip_idx < s_ip_idx)
3235 continue;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003236 if ((err = inet6_fill_ifaddr(skb, ifa,
3237 NETLINK_CB(cb->skb).pid,
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07003238 cb->nlh->nlmsg_seq, RTM_NEWADDR,
3239 NLM_F_MULTI)) <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 goto done;
3241 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242 break;
3243 case MULTICAST_ADDR:
3244 /* multicast address */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003245 for (ifmca = idev->mc_list; ifmca;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 ifmca = ifmca->next, ip_idx++) {
3247 if (ip_idx < s_ip_idx)
3248 continue;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003249 if ((err = inet6_fill_ifmcaddr(skb, ifmca,
3250 NETLINK_CB(cb->skb).pid,
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07003251 cb->nlh->nlmsg_seq, RTM_GETMULTICAST,
3252 NLM_F_MULTI)) <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 goto done;
3254 }
3255 break;
3256 case ANYCAST_ADDR:
3257 /* anycast address */
3258 for (ifaca = idev->ac_list; ifaca;
3259 ifaca = ifaca->aca_next, ip_idx++) {
3260 if (ip_idx < s_ip_idx)
3261 continue;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003262 if ((err = inet6_fill_ifacaddr(skb, ifaca,
3263 NETLINK_CB(cb->skb).pid,
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07003264 cb->nlh->nlmsg_seq, RTM_GETANYCAST,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003265 NLM_F_MULTI)) <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266 goto done;
3267 }
3268 break;
3269 default:
3270 break;
3271 }
3272 read_unlock_bh(&idev->lock);
3273 in6_dev_put(idev);
3274 }
3275done:
3276 if (err <= 0) {
3277 read_unlock_bh(&idev->lock);
3278 in6_dev_put(idev);
3279 }
3280 read_unlock(&dev_base_lock);
3281 cb->args[0] = idx;
3282 cb->args[1] = ip_idx;
3283 return skb->len;
3284}
3285
3286static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
3287{
3288 enum addr_type_t type = UNICAST_ADDR;
3289 return inet6_dump_addr(skb, cb, type);
3290}
3291
3292static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
3293{
3294 enum addr_type_t type = MULTICAST_ADDR;
3295 return inet6_dump_addr(skb, cb, type);
3296}
3297
3298
3299static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
3300{
3301 enum addr_type_t type = ANYCAST_ADDR;
3302 return inet6_dump_addr(skb, cb, type);
3303}
3304
Thomas Graf1b29fc22006-09-18 00:10:50 -07003305static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr* nlh,
3306 void *arg)
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003307{
Thomas Graf1b29fc22006-09-18 00:10:50 -07003308 struct ifaddrmsg *ifm;
3309 struct nlattr *tb[IFA_MAX+1];
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003310 struct in6_addr *addr = NULL;
3311 struct net_device *dev = NULL;
3312 struct inet6_ifaddr *ifa;
3313 struct sk_buff *skb;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003314 int err;
3315
Thomas Graf1b29fc22006-09-18 00:10:50 -07003316 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
3317 if (err < 0)
3318 goto errout;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003319
Thomas Graf1b29fc22006-09-18 00:10:50 -07003320 addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
3321 if (addr == NULL) {
3322 err = -EINVAL;
3323 goto errout;
3324 }
3325
3326 ifm = nlmsg_data(nlh);
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003327 if (ifm->ifa_index)
3328 dev = __dev_get_by_index(ifm->ifa_index);
3329
Thomas Graf1b29fc22006-09-18 00:10:50 -07003330 if ((ifa = ipv6_get_ifaddr(addr, dev, 1)) == NULL) {
3331 err = -EADDRNOTAVAIL;
3332 goto errout;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003333 }
3334
Thomas Graf0ab68032006-09-18 00:12:35 -07003335 if ((skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL)) == NULL) {
Thomas Graf1b29fc22006-09-18 00:10:50 -07003336 err = -ENOBUFS;
3337 goto errout_ifa;
3338 }
3339
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003340 err = inet6_fill_ifaddr(skb, ifa, NETLINK_CB(in_skb).pid,
3341 nlh->nlmsg_seq, RTM_NEWADDR, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08003342 if (err < 0) {
3343 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
3344 WARN_ON(err == -EMSGSIZE);
3345 kfree_skb(skb);
3346 goto errout_ifa;
3347 }
Thomas Graf2942e902006-08-15 00:30:25 -07003348 err = rtnl_unicast(skb, NETLINK_CB(in_skb).pid);
Thomas Graf1b29fc22006-09-18 00:10:50 -07003349errout_ifa:
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003350 in6_ifa_put(ifa);
Thomas Graf1b29fc22006-09-18 00:10:50 -07003351errout:
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003352 return err;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003353}
3354
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
3356{
3357 struct sk_buff *skb;
Thomas Graf5d620262006-08-15 00:35:02 -07003358 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359
Thomas Graf0ab68032006-09-18 00:12:35 -07003360 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC);
Thomas Graf5d620262006-08-15 00:35:02 -07003361 if (skb == NULL)
3362 goto errout;
3363
3364 err = inet6_fill_ifaddr(skb, ifa, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08003365 if (err < 0) {
3366 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
3367 WARN_ON(err == -EMSGSIZE);
3368 kfree_skb(skb);
3369 goto errout;
3370 }
Thomas Graf5d620262006-08-15 00:35:02 -07003371 err = rtnl_notify(skb, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
3372errout:
3373 if (err < 0)
3374 rtnl_set_sk_err(RTNLGRP_IPV6_IFADDR, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375}
3376
3377static void inline ipv6_store_devconf(struct ipv6_devconf *cnf,
3378 __s32 *array, int bytes)
3379{
Thomas Graf04561c12006-11-14 19:53:58 -08003380 BUG_ON(bytes < (DEVCONF_MAX * 4));
3381
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382 memset(array, 0, bytes);
3383 array[DEVCONF_FORWARDING] = cnf->forwarding;
3384 array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
3385 array[DEVCONF_MTU6] = cnf->mtu6;
3386 array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
3387 array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
3388 array[DEVCONF_AUTOCONF] = cnf->autoconf;
3389 array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
3390 array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
3391 array[DEVCONF_RTR_SOLICIT_INTERVAL] = cnf->rtr_solicit_interval;
3392 array[DEVCONF_RTR_SOLICIT_DELAY] = cnf->rtr_solicit_delay;
3393 array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
3394#ifdef CONFIG_IPV6_PRIVACY
3395 array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
3396 array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
3397 array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
3398 array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
3399 array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
3400#endif
3401 array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -08003402 array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr;
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -08003403 array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo;
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08003404#ifdef CONFIG_IPV6_ROUTER_PREF
3405 array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref;
YOSHIFUJI Hideaki52e163562006-03-20 17:05:47 -08003406 array[DEVCONF_RTR_PROBE_INTERVAL] = cnf->rtr_probe_interval;
Neil Hormanfa03ef32007-01-30 14:30:10 -08003407#ifdef CONFIG_IPV6_ROUTE_INFO
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -08003408 array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen;
3409#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08003410#endif
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -07003411 array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412}
3413
Thomas Graf339bf982006-11-10 14:10:15 -08003414static inline size_t inet6_if_nlmsg_size(void)
3415{
3416 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
3417 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
3418 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
3419 + nla_total_size(4) /* IFLA_MTU */
3420 + nla_total_size(4) /* IFLA_LINK */
3421 + nla_total_size( /* IFLA_PROTINFO */
3422 nla_total_size(4) /* IFLA_INET6_FLAGS */
3423 + nla_total_size(sizeof(struct ifla_cacheinfo))
3424 + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */
3425 );
3426}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07003427
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003428static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07003429 u32 pid, u32 seq, int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430{
Thomas Graf04561c12006-11-14 19:53:58 -08003431 struct net_device *dev = idev->dev;
3432 struct nlattr *conf;
3433 struct ifinfomsg *hdr;
3434 struct nlmsghdr *nlh;
3435 void *protoinfo;
3436 struct ifla_cacheinfo ci;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437
Thomas Graf04561c12006-11-14 19:53:58 -08003438 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*hdr), flags);
3439 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08003440 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441
Thomas Graf04561c12006-11-14 19:53:58 -08003442 hdr = nlmsg_data(nlh);
3443 hdr->ifi_family = AF_INET6;
3444 hdr->__ifi_pad = 0;
3445 hdr->ifi_type = dev->type;
3446 hdr->ifi_index = dev->ifindex;
3447 hdr->ifi_flags = dev_get_flags(dev);
3448 hdr->ifi_change = 0;
3449
3450 NLA_PUT_STRING(skb, IFLA_IFNAME, dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451
3452 if (dev->addr_len)
Thomas Graf04561c12006-11-14 19:53:58 -08003453 NLA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454
Thomas Graf04561c12006-11-14 19:53:58 -08003455 NLA_PUT_U32(skb, IFLA_MTU, dev->mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456 if (dev->ifindex != dev->iflink)
Thomas Graf04561c12006-11-14 19:53:58 -08003457 NLA_PUT_U32(skb, IFLA_LINK, dev->iflink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458
Thomas Graf04561c12006-11-14 19:53:58 -08003459 protoinfo = nla_nest_start(skb, IFLA_PROTINFO);
3460 if (protoinfo == NULL)
3461 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462
Thomas Graf04561c12006-11-14 19:53:58 -08003463 NLA_PUT_U32(skb, IFLA_INET6_FLAGS, idev->if_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465 ci.max_reasm_len = IPV6_MAXPLEN;
3466 ci.tstamp = (__u32)(TIME_DELTA(idev->tstamp, INITIAL_JIFFIES) / HZ * 100
3467 + TIME_DELTA(idev->tstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
3468 ci.reachable_time = idev->nd_parms->reachable_time;
3469 ci.retrans_time = idev->nd_parms->retrans_time;
Thomas Graf04561c12006-11-14 19:53:58 -08003470 NLA_PUT(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci);
3471
3472 conf = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32));
3473 if (conf == NULL)
3474 goto nla_put_failure;
3475 ipv6_store_devconf(&idev->cnf, nla_data(conf), nla_len(conf));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476
3477 /* XXX - Statistics/MC not implemented */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478
Thomas Graf04561c12006-11-14 19:53:58 -08003479 nla_nest_end(skb, protoinfo);
3480 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481
Thomas Graf04561c12006-11-14 19:53:58 -08003482nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08003483 nlmsg_cancel(skb, nlh);
3484 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485}
3486
3487static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
3488{
3489 int idx, err;
3490 int s_idx = cb->args[0];
3491 struct net_device *dev;
3492 struct inet6_dev *idev;
3493
3494 read_lock(&dev_base_lock);
3495 for (dev=dev_base, idx=0; dev; dev = dev->next, idx++) {
3496 if (idx < s_idx)
3497 continue;
3498 if ((idev = in6_dev_get(dev)) == NULL)
3499 continue;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003500 err = inet6_fill_ifinfo(skb, idev, NETLINK_CB(cb->skb).pid,
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07003501 cb->nlh->nlmsg_seq, RTM_NEWLINK, NLM_F_MULTI);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502 in6_dev_put(idev);
3503 if (err <= 0)
3504 break;
3505 }
3506 read_unlock(&dev_base_lock);
3507 cb->args[0] = idx;
3508
3509 return skb->len;
3510}
3511
3512void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
3513{
3514 struct sk_buff *skb;
Thomas Graf8d7a76c2006-08-15 00:35:47 -07003515 int err = -ENOBUFS;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003516
Thomas Graf339bf982006-11-10 14:10:15 -08003517 skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC);
Thomas Graf8d7a76c2006-08-15 00:35:47 -07003518 if (skb == NULL)
3519 goto errout;
3520
3521 err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08003522 if (err < 0) {
3523 /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */
3524 WARN_ON(err == -EMSGSIZE);
3525 kfree_skb(skb);
3526 goto errout;
3527 }
Thomas Graf8d7a76c2006-08-15 00:35:47 -07003528 err = rtnl_notify(skb, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
3529errout:
3530 if (err < 0)
3531 rtnl_set_sk_err(RTNLGRP_IPV6_IFADDR, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532}
3533
Thomas Graf339bf982006-11-10 14:10:15 -08003534static inline size_t inet6_prefix_nlmsg_size(void)
3535{
3536 return NLMSG_ALIGN(sizeof(struct prefixmsg))
3537 + nla_total_size(sizeof(struct in6_addr))
3538 + nla_total_size(sizeof(struct prefix_cacheinfo));
3539}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07003540
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
Thomas Graf6051e2f2006-11-14 19:54:19 -08003542 struct prefix_info *pinfo, u32 pid, u32 seq,
3543 int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544{
Thomas Graf6051e2f2006-11-14 19:54:19 -08003545 struct prefixmsg *pmsg;
3546 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547 struct prefix_cacheinfo ci;
3548
Thomas Graf6051e2f2006-11-14 19:54:19 -08003549 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*pmsg), flags);
3550 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08003551 return -EMSGSIZE;
Thomas Graf6051e2f2006-11-14 19:54:19 -08003552
3553 pmsg = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554 pmsg->prefix_family = AF_INET6;
Patrick McHardy8a470772005-06-28 12:56:45 -07003555 pmsg->prefix_pad1 = 0;
3556 pmsg->prefix_pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557 pmsg->prefix_ifindex = idev->dev->ifindex;
3558 pmsg->prefix_len = pinfo->prefix_len;
3559 pmsg->prefix_type = pinfo->type;
Patrick McHardy8a470772005-06-28 12:56:45 -07003560 pmsg->prefix_pad3 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561 pmsg->prefix_flags = 0;
3562 if (pinfo->onlink)
3563 pmsg->prefix_flags |= IF_PREFIX_ONLINK;
3564 if (pinfo->autoconf)
3565 pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
3566
Thomas Graf6051e2f2006-11-14 19:54:19 -08003567 NLA_PUT(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568
3569 ci.preferred_time = ntohl(pinfo->prefered);
3570 ci.valid_time = ntohl(pinfo->valid);
Thomas Graf6051e2f2006-11-14 19:54:19 -08003571 NLA_PUT(skb, PREFIX_CACHEINFO, sizeof(ci), &ci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572
Thomas Graf6051e2f2006-11-14 19:54:19 -08003573 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574
Thomas Graf6051e2f2006-11-14 19:54:19 -08003575nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08003576 nlmsg_cancel(skb, nlh);
3577 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578}
3579
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003580static void inet6_prefix_notify(int event, struct inet6_dev *idev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581 struct prefix_info *pinfo)
3582{
3583 struct sk_buff *skb;
Thomas Graf8c384bfa2006-08-15 00:36:07 -07003584 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585
Thomas Graf339bf982006-11-10 14:10:15 -08003586 skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC);
Thomas Graf8c384bfa2006-08-15 00:36:07 -07003587 if (skb == NULL)
3588 goto errout;
3589
3590 err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08003591 if (err < 0) {
3592 /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */
3593 WARN_ON(err == -EMSGSIZE);
3594 kfree_skb(skb);
3595 goto errout;
3596 }
Thomas Graf8c384bfa2006-08-15 00:36:07 -07003597 err = rtnl_notify(skb, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC);
3598errout:
3599 if (err < 0)
3600 rtnl_set_sk_err(RTNLGRP_IPV6_PREFIX, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601}
3602
Thomas Grafdb46edc2005-05-03 14:29:39 -07003603static struct rtnetlink_link inet6_rtnetlink_table[RTM_NR_MSGTYPES] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604 [RTM_GETLINK - RTM_BASE] = { .dumpit = inet6_dump_ifinfo, },
3605 [RTM_NEWADDR - RTM_BASE] = { .doit = inet6_rtm_newaddr, },
3606 [RTM_DELADDR - RTM_BASE] = { .doit = inet6_rtm_deladdr, },
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003607 [RTM_GETADDR - RTM_BASE] = { .doit = inet6_rtm_getaddr,
3608 .dumpit = inet6_dump_ifaddr, },
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609 [RTM_GETMULTICAST - RTM_BASE] = { .dumpit = inet6_dump_ifmcaddr, },
3610 [RTM_GETANYCAST - RTM_BASE] = { .dumpit = inet6_dump_ifacaddr, },
3611 [RTM_NEWROUTE - RTM_BASE] = { .doit = inet6_rtm_newroute, },
3612 [RTM_DELROUTE - RTM_BASE] = { .doit = inet6_rtm_delroute, },
3613 [RTM_GETROUTE - RTM_BASE] = { .doit = inet6_rtm_getroute,
3614 .dumpit = inet6_dump_fib, },
David S. Miller8423a9a2006-08-07 21:54:37 -07003615#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Thomas Graf101367c2006-08-04 03:39:02 -07003616 [RTM_GETRULE - RTM_BASE] = { .dumpit = fib6_rules_dump, },
David S. Miller8423a9a2006-08-07 21:54:37 -07003617#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618};
3619
3620static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
3621{
3622 inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
3623
3624 switch (event) {
3625 case RTM_NEWADDR:
Thomas Graf40e22e82006-08-22 00:00:45 -07003626 ip6_ins_rt(ifp->rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627 if (ifp->idev->cnf.forwarding)
3628 addrconf_join_anycast(ifp);
3629 break;
3630 case RTM_DELADDR:
3631 if (ifp->idev->cnf.forwarding)
3632 addrconf_leave_anycast(ifp);
3633 addrconf_leave_solict(ifp->idev, &ifp->addr);
3634 dst_hold(&ifp->rt->u.dst);
Thomas Grafe0a1ad732006-08-22 00:00:21 -07003635 if (ip6_del_rt(ifp->rt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636 dst_free(&ifp->rt->u.dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637 break;
3638 }
3639}
3640
3641static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
3642{
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07003643 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644 if (likely(ifp->idev->dead == 0))
3645 __ipv6_ifa_notify(event, ifp);
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07003646 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647}
3648
3649#ifdef CONFIG_SYSCTL
3650
3651static
3652int addrconf_sysctl_forward(ctl_table *ctl, int write, struct file * filp,
3653 void __user *buffer, size_t *lenp, loff_t *ppos)
3654{
3655 int *valp = ctl->data;
3656 int val = *valp;
3657 int ret;
3658
3659 ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
3660
3661 if (write && valp != &ipv6_devconf_dflt.forwarding) {
3662 if (valp != &ipv6_devconf.forwarding) {
3663 if ((!*valp) ^ (!val)) {
3664 struct inet6_dev *idev = (struct inet6_dev *)ctl->extra1;
3665 if (idev == NULL)
3666 return ret;
3667 dev_forward_change(idev);
3668 }
3669 } else {
3670 ipv6_devconf_dflt.forwarding = ipv6_devconf.forwarding;
3671 addrconf_forward_change();
3672 }
3673 if (*valp)
3674 rt6_purge_dflt_routers();
3675 }
3676
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003677 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678}
3679
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003680static int addrconf_sysctl_forward_strategy(ctl_table *table,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681 int __user *name, int nlen,
3682 void __user *oldval,
3683 size_t __user *oldlenp,
Alexey Dobriyan1f29bcd2006-12-10 02:19:10 -08003684 void __user *newval, size_t newlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685{
3686 int *valp = table->data;
3687 int new;
3688
3689 if (!newval || !newlen)
3690 return 0;
3691 if (newlen != sizeof(int))
3692 return -EINVAL;
3693 if (get_user(new, (int __user *)newval))
3694 return -EFAULT;
3695 if (new == *valp)
3696 return 0;
3697 if (oldval && oldlenp) {
3698 size_t len;
3699 if (get_user(len, oldlenp))
3700 return -EFAULT;
3701 if (len) {
3702 if (len > table->maxlen)
3703 len = table->maxlen;
3704 if (copy_to_user(oldval, valp, len))
3705 return -EFAULT;
3706 if (put_user(len, oldlenp))
3707 return -EFAULT;
3708 }
3709 }
3710
3711 if (valp != &ipv6_devconf_dflt.forwarding) {
3712 if (valp != &ipv6_devconf.forwarding) {
3713 struct inet6_dev *idev = (struct inet6_dev *)table->extra1;
3714 int changed;
3715 if (unlikely(idev == NULL))
3716 return -ENODEV;
3717 changed = (!*valp) ^ (!new);
3718 *valp = new;
3719 if (changed)
3720 dev_forward_change(idev);
3721 } else {
3722 *valp = new;
3723 addrconf_forward_change();
3724 }
3725
3726 if (*valp)
3727 rt6_purge_dflt_routers();
3728 } else
3729 *valp = new;
3730
3731 return 1;
3732}
3733
3734static struct addrconf_sysctl_table
3735{
3736 struct ctl_table_header *sysctl_header;
3737 ctl_table addrconf_vars[__NET_IPV6_MAX];
3738 ctl_table addrconf_dev[2];
3739 ctl_table addrconf_conf_dir[2];
3740 ctl_table addrconf_proto_dir[2];
3741 ctl_table addrconf_root_dir[2];
Brian Haleyab32ea52006-09-22 14:15:41 -07003742} addrconf_sysctl __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743 .sysctl_header = NULL,
3744 .addrconf_vars = {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003745 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003746 .ctl_name = NET_IPV6_FORWARDING,
3747 .procname = "forwarding",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003748 .data = &ipv6_devconf.forwarding,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749 .maxlen = sizeof(int),
3750 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003751 .proc_handler = &addrconf_sysctl_forward,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752 .strategy = &addrconf_sysctl_forward_strategy,
3753 },
3754 {
3755 .ctl_name = NET_IPV6_HOP_LIMIT,
3756 .procname = "hop_limit",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003757 .data = &ipv6_devconf.hop_limit,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758 .maxlen = sizeof(int),
3759 .mode = 0644,
3760 .proc_handler = proc_dointvec,
3761 },
3762 {
3763 .ctl_name = NET_IPV6_MTU,
3764 .procname = "mtu",
3765 .data = &ipv6_devconf.mtu6,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003766 .maxlen = sizeof(int),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003768 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769 },
3770 {
3771 .ctl_name = NET_IPV6_ACCEPT_RA,
3772 .procname = "accept_ra",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003773 .data = &ipv6_devconf.accept_ra,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774 .maxlen = sizeof(int),
3775 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003776 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777 },
3778 {
3779 .ctl_name = NET_IPV6_ACCEPT_REDIRECTS,
3780 .procname = "accept_redirects",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003781 .data = &ipv6_devconf.accept_redirects,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782 .maxlen = sizeof(int),
3783 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003784 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785 },
3786 {
3787 .ctl_name = NET_IPV6_AUTOCONF,
3788 .procname = "autoconf",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003789 .data = &ipv6_devconf.autoconf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790 .maxlen = sizeof(int),
3791 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003792 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793 },
3794 {
3795 .ctl_name = NET_IPV6_DAD_TRANSMITS,
3796 .procname = "dad_transmits",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003797 .data = &ipv6_devconf.dad_transmits,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798 .maxlen = sizeof(int),
3799 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003800 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801 },
3802 {
3803 .ctl_name = NET_IPV6_RTR_SOLICITS,
3804 .procname = "router_solicitations",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003805 .data = &ipv6_devconf.rtr_solicits,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806 .maxlen = sizeof(int),
3807 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003808 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809 },
3810 {
3811 .ctl_name = NET_IPV6_RTR_SOLICIT_INTERVAL,
3812 .procname = "router_solicitation_interval",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003813 .data = &ipv6_devconf.rtr_solicit_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814 .maxlen = sizeof(int),
3815 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003816 .proc_handler = &proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817 .strategy = &sysctl_jiffies,
3818 },
3819 {
3820 .ctl_name = NET_IPV6_RTR_SOLICIT_DELAY,
3821 .procname = "router_solicitation_delay",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003822 .data = &ipv6_devconf.rtr_solicit_delay,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823 .maxlen = sizeof(int),
3824 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003825 .proc_handler = &proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826 .strategy = &sysctl_jiffies,
3827 },
3828 {
3829 .ctl_name = NET_IPV6_FORCE_MLD_VERSION,
3830 .procname = "force_mld_version",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003831 .data = &ipv6_devconf.force_mld_version,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832 .maxlen = sizeof(int),
3833 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003834 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835 },
3836#ifdef CONFIG_IPV6_PRIVACY
3837 {
3838 .ctl_name = NET_IPV6_USE_TEMPADDR,
3839 .procname = "use_tempaddr",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003840 .data = &ipv6_devconf.use_tempaddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003841 .maxlen = sizeof(int),
3842 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003843 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844 },
3845 {
3846 .ctl_name = NET_IPV6_TEMP_VALID_LFT,
3847 .procname = "temp_valid_lft",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003848 .data = &ipv6_devconf.temp_valid_lft,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849 .maxlen = sizeof(int),
3850 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003851 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852 },
3853 {
3854 .ctl_name = NET_IPV6_TEMP_PREFERED_LFT,
3855 .procname = "temp_prefered_lft",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003856 .data = &ipv6_devconf.temp_prefered_lft,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857 .maxlen = sizeof(int),
3858 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003859 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003860 },
3861 {
3862 .ctl_name = NET_IPV6_REGEN_MAX_RETRY,
3863 .procname = "regen_max_retry",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003864 .data = &ipv6_devconf.regen_max_retry,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865 .maxlen = sizeof(int),
3866 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003867 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003868 },
3869 {
3870 .ctl_name = NET_IPV6_MAX_DESYNC_FACTOR,
3871 .procname = "max_desync_factor",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003872 .data = &ipv6_devconf.max_desync_factor,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873 .maxlen = sizeof(int),
3874 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003875 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876 },
3877#endif
3878 {
3879 .ctl_name = NET_IPV6_MAX_ADDRESSES,
3880 .procname = "max_addresses",
3881 .data = &ipv6_devconf.max_addresses,
3882 .maxlen = sizeof(int),
3883 .mode = 0644,
3884 .proc_handler = &proc_dointvec,
3885 },
3886 {
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -08003887 .ctl_name = NET_IPV6_ACCEPT_RA_DEFRTR,
3888 .procname = "accept_ra_defrtr",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003889 .data = &ipv6_devconf.accept_ra_defrtr,
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -08003890 .maxlen = sizeof(int),
3891 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003892 .proc_handler = &proc_dointvec,
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -08003893 },
3894 {
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -08003895 .ctl_name = NET_IPV6_ACCEPT_RA_PINFO,
3896 .procname = "accept_ra_pinfo",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003897 .data = &ipv6_devconf.accept_ra_pinfo,
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -08003898 .maxlen = sizeof(int),
3899 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003900 .proc_handler = &proc_dointvec,
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -08003901 },
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08003902#ifdef CONFIG_IPV6_ROUTER_PREF
3903 {
3904 .ctl_name = NET_IPV6_ACCEPT_RA_RTR_PREF,
3905 .procname = "accept_ra_rtr_pref",
3906 .data = &ipv6_devconf.accept_ra_rtr_pref,
3907 .maxlen = sizeof(int),
3908 .mode = 0644,
3909 .proc_handler = &proc_dointvec,
3910 },
YOSHIFUJI Hideaki52e163562006-03-20 17:05:47 -08003911 {
3912 .ctl_name = NET_IPV6_RTR_PROBE_INTERVAL,
3913 .procname = "router_probe_interval",
3914 .data = &ipv6_devconf.rtr_probe_interval,
3915 .maxlen = sizeof(int),
3916 .mode = 0644,
3917 .proc_handler = &proc_dointvec_jiffies,
3918 .strategy = &sysctl_jiffies,
3919 },
Neil Hormanfa03ef32007-01-30 14:30:10 -08003920#ifdef CONFIG_IPV6_ROUTE_INFO
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -08003921 {
3922 .ctl_name = NET_IPV6_ACCEPT_RA_RT_INFO_MAX_PLEN,
3923 .procname = "accept_ra_rt_info_max_plen",
3924 .data = &ipv6_devconf.accept_ra_rt_info_max_plen,
3925 .maxlen = sizeof(int),
3926 .mode = 0644,
3927 .proc_handler = &proc_dointvec,
3928 },
3929#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08003930#endif
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -08003931 {
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -07003932 .ctl_name = NET_IPV6_PROXY_NDP,
3933 .procname = "proxy_ndp",
3934 .data = &ipv6_devconf.proxy_ndp,
3935 .maxlen = sizeof(int),
3936 .mode = 0644,
3937 .proc_handler = &proc_dointvec,
3938 },
3939 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940 .ctl_name = 0, /* sentinel */
3941 }
3942 },
3943 .addrconf_dev = {
3944 {
3945 .ctl_name = NET_PROTO_CONF_ALL,
3946 .procname = "all",
3947 .mode = 0555,
3948 .child = addrconf_sysctl.addrconf_vars,
3949 },
3950 {
3951 .ctl_name = 0, /* sentinel */
3952 }
3953 },
3954 .addrconf_conf_dir = {
3955 {
3956 .ctl_name = NET_IPV6_CONF,
3957 .procname = "conf",
3958 .mode = 0555,
3959 .child = addrconf_sysctl.addrconf_dev,
3960 },
3961 {
3962 .ctl_name = 0, /* sentinel */
3963 }
3964 },
3965 .addrconf_proto_dir = {
3966 {
3967 .ctl_name = NET_IPV6,
3968 .procname = "ipv6",
3969 .mode = 0555,
3970 .child = addrconf_sysctl.addrconf_conf_dir,
3971 },
3972 {
3973 .ctl_name = 0, /* sentinel */
3974 }
3975 },
3976 .addrconf_root_dir = {
3977 {
3978 .ctl_name = CTL_NET,
3979 .procname = "net",
3980 .mode = 0555,
3981 .child = addrconf_sysctl.addrconf_proto_dir,
3982 },
3983 {
3984 .ctl_name = 0, /* sentinel */
3985 }
3986 },
3987};
3988
3989static void addrconf_sysctl_register(struct inet6_dev *idev, struct ipv6_devconf *p)
3990{
3991 int i;
3992 struct net_device *dev = idev ? idev->dev : NULL;
3993 struct addrconf_sysctl_table *t;
3994 char *dev_name = NULL;
3995
Arnaldo Carvalho de Meloaf879cc2006-11-17 12:14:37 -02003996 t = kmemdup(&addrconf_sysctl, sizeof(*t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997 if (t == NULL)
3998 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003999 for (i=0; t->addrconf_vars[i].data; i++) {
4000 t->addrconf_vars[i].data += (char*)p - (char*)&ipv6_devconf;
4001 t->addrconf_vars[i].de = NULL;
4002 t->addrconf_vars[i].extra1 = idev; /* embedded; no ref */
4003 }
4004 if (dev) {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004005 dev_name = dev->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006 t->addrconf_dev[0].ctl_name = dev->ifindex;
4007 } else {
4008 dev_name = "default";
4009 t->addrconf_dev[0].ctl_name = NET_PROTO_CONF_DEFAULT;
4010 }
4011
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004012 /*
4013 * Make a copy of dev_name, because '.procname' is regarded as const
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014 * by sysctl and we wouldn't want anyone to change it under our feet
4015 * (see SIOCSIFNAME).
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004016 */
Paulo Marques543537b2005-06-23 00:09:02 -07004017 dev_name = kstrdup(dev_name, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018 if (!dev_name)
4019 goto free;
4020
4021 t->addrconf_dev[0].procname = dev_name;
4022
4023 t->addrconf_dev[0].child = t->addrconf_vars;
4024 t->addrconf_dev[0].de = NULL;
4025 t->addrconf_conf_dir[0].child = t->addrconf_dev;
4026 t->addrconf_conf_dir[0].de = NULL;
4027 t->addrconf_proto_dir[0].child = t->addrconf_conf_dir;
4028 t->addrconf_proto_dir[0].de = NULL;
4029 t->addrconf_root_dir[0].child = t->addrconf_proto_dir;
4030 t->addrconf_root_dir[0].de = NULL;
4031
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08004032 t->sysctl_header = register_sysctl_table(t->addrconf_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033 if (t->sysctl_header == NULL)
4034 goto free_procname;
4035 else
4036 p->sysctl = t;
4037 return;
4038
4039 /* error path */
4040 free_procname:
4041 kfree(dev_name);
4042 free:
4043 kfree(t);
4044
4045 return;
4046}
4047
4048static void addrconf_sysctl_unregister(struct ipv6_devconf *p)
4049{
4050 if (p->sysctl) {
4051 struct addrconf_sysctl_table *t = p->sysctl;
4052 p->sysctl = NULL;
4053 unregister_sysctl_table(t->sysctl_header);
4054 kfree(t->addrconf_dev[0].procname);
4055 kfree(t);
4056 }
4057}
4058
4059
4060#endif
4061
4062/*
4063 * Device notifier
4064 */
4065
4066int register_inet6addr_notifier(struct notifier_block *nb)
4067{
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004068 return atomic_notifier_chain_register(&inet6addr_chain, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069}
4070
4071int unregister_inet6addr_notifier(struct notifier_block *nb)
4072{
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004073 return atomic_notifier_chain_unregister(&inet6addr_chain,nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074}
4075
4076/*
4077 * Init / cleanup code
4078 */
4079
4080int __init addrconf_init(void)
4081{
4082 int err = 0;
4083
4084 /* The addrconf netdev notifier requires that loopback_dev
4085 * has it's ipv6 private information allocated and setup
4086 * before it can bring up and give link-local addresses
4087 * to other devices which are up.
4088 *
4089 * Unfortunately, loopback_dev is not necessarily the first
4090 * entry in the global dev_base list of net devices. In fact,
4091 * it is likely to be the very last entry on that list.
4092 * So this causes the notifier registry below to try and
4093 * give link-local addresses to all devices besides loopback_dev
4094 * first, then loopback_dev, which cases all the non-loopback_dev
4095 * devices to fail to get a link-local address.
4096 *
4097 * So, as a temporary fix, allocate the ipv6 structure for
4098 * loopback_dev first by hand.
4099 * Longer term, all of the dependencies ipv6 has upon the loopback
4100 * device and it being up should be removed.
4101 */
4102 rtnl_lock();
4103 if (!ipv6_add_dev(&loopback_dev))
4104 err = -ENOMEM;
4105 rtnl_unlock();
4106 if (err)
4107 return err;
4108
Herbert Xuc62dba92005-09-26 15:10:16 -07004109 ip6_null_entry.rt6i_idev = in6_dev_get(&loopback_dev);
4110
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111 register_netdevice_notifier(&ipv6_dev_notf);
4112
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113 addrconf_verify(0);
4114 rtnetlink_links[PF_INET6] = inet6_rtnetlink_table;
4115#ifdef CONFIG_SYSCTL
4116 addrconf_sysctl.sysctl_header =
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08004117 register_sysctl_table(addrconf_sysctl.addrconf_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118 addrconf_sysctl_register(NULL, &ipv6_devconf_dflt);
4119#endif
4120
4121 return 0;
4122}
4123
4124void __exit addrconf_cleanup(void)
4125{
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004126 struct net_device *dev;
4127 struct inet6_dev *idev;
4128 struct inet6_ifaddr *ifa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129 int i;
4130
4131 unregister_netdevice_notifier(&ipv6_dev_notf);
4132
4133 rtnetlink_links[PF_INET6] = NULL;
4134#ifdef CONFIG_SYSCTL
4135 addrconf_sysctl_unregister(&ipv6_devconf_dflt);
4136 addrconf_sysctl_unregister(&ipv6_devconf);
4137#endif
4138
4139 rtnl_lock();
4140
4141 /*
4142 * clean dev list.
4143 */
4144
4145 for (dev=dev_base; dev; dev=dev->next) {
4146 if ((idev = __in6_dev_get(dev)) == NULL)
4147 continue;
4148 addrconf_ifdown(dev, 1);
4149 }
4150 addrconf_ifdown(&loopback_dev, 2);
4151
4152 /*
4153 * Check hash table.
4154 */
4155
4156 write_lock_bh(&addrconf_hash_lock);
4157 for (i=0; i < IN6_ADDR_HSIZE; i++) {
4158 for (ifa=inet6_addr_lst[i]; ifa; ) {
4159 struct inet6_ifaddr *bifa;
4160
4161 bifa = ifa;
4162 ifa = ifa->lst_next;
4163 printk(KERN_DEBUG "bug: IPv6 address leakage detected: ifa=%p\n", bifa);
4164 /* Do not free it; something is wrong.
4165 Now we can investigate it with debugger.
4166 */
4167 }
4168 }
4169 write_unlock_bh(&addrconf_hash_lock);
4170
4171 del_timer(&addr_chk_timer);
4172
4173 rtnl_unlock();
4174
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175#ifdef CONFIG_PROC_FS
4176 proc_net_remove("if_inet6");
4177#endif
4178}