blob: 77e56f2b1af295de8d87f5dbfec9b02508c75826 [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>
47#include <linux/sched.h>
48#include <linux/net.h>
49#include <linux/in6.h>
50#include <linux/netdevice.h>
Thomas Graf18237302006-08-04 23:04:54 -070051#include <linux/if_addr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <linux/if_arp.h>
53#include <linux/if_arcnet.h>
54#include <linux/if_infiniband.h>
55#include <linux/route.h>
56#include <linux/inetdevice.h>
57#include <linux/init.h>
58#ifdef CONFIG_SYSCTL
59#include <linux/sysctl.h>
60#endif
Randy Dunlap4fc268d2006-01-11 12:17:47 -080061#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#include <linux/delay.h>
63#include <linux/notifier.h>
Paulo Marques543537b2005-06-23 00:09:02 -070064#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66#include <net/sock.h>
67#include <net/snmp.h>
68
69#include <net/ipv6.h>
70#include <net/protocol.h>
71#include <net/ndisc.h>
72#include <net/ip6_route.h>
73#include <net/addrconf.h>
74#include <net/tcp.h>
75#include <net/ip.h>
Thomas Graf5d620262006-08-15 00:35:02 -070076#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070077#include <linux/if_tunnel.h>
78#include <linux/rtnetlink.h>
79
80#ifdef CONFIG_IPV6_PRIVACY
81#include <linux/random.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070082#endif
83
84#include <asm/uaccess.h>
85
86#include <linux/proc_fs.h>
87#include <linux/seq_file.h>
88
89/* Set to 3 to get tracing... */
90#define ACONF_DEBUG 2
91
92#if ACONF_DEBUG >= 3
93#define ADBG(x) printk x
94#else
95#define ADBG(x)
96#endif
97
98#define INFINITY_LIFE_TIME 0xFFFFFFFF
99#define TIME_DELTA(a,b) ((unsigned long)((long)(a) - (long)(b)))
100
101#ifdef CONFIG_SYSCTL
102static void addrconf_sysctl_register(struct inet6_dev *idev, struct ipv6_devconf *p);
103static void addrconf_sysctl_unregister(struct ipv6_devconf *p);
104#endif
105
106#ifdef CONFIG_IPV6_PRIVACY
107static int __ipv6_regen_rndid(struct inet6_dev *idev);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900108static int __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109static void ipv6_regen_rndid(unsigned long data);
110
111static int desync_factor = MAX_DESYNC_FACTOR * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112#endif
113
114static int ipv6_count_addresses(struct inet6_dev *idev);
115
116/*
117 * Configured unicast address hash table
118 */
119static struct inet6_ifaddr *inet6_addr_lst[IN6_ADDR_HSIZE];
120static DEFINE_RWLOCK(addrconf_hash_lock);
121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122static void addrconf_verify(unsigned long);
123
Ingo Molnar8d06afa2005-09-09 13:10:40 -0700124static DEFINE_TIMER(addr_chk_timer, addrconf_verify, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125static DEFINE_SPINLOCK(addrconf_verify_lock);
126
127static void addrconf_join_anycast(struct inet6_ifaddr *ifp);
128static void addrconf_leave_anycast(struct inet6_ifaddr *ifp);
129
130static int addrconf_ifdown(struct net_device *dev, int how);
131
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -0700132static void addrconf_dad_start(struct inet6_ifaddr *ifp, u32 flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133static void addrconf_dad_timer(unsigned long data);
134static void addrconf_dad_completed(struct inet6_ifaddr *ifp);
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +0900135static void addrconf_dad_run(struct inet6_dev *idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136static void addrconf_rs_timer(unsigned long data);
137static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
138static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
139
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900140static void inet6_prefix_notify(int event, struct inet6_dev *idev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 struct prefix_info *pinfo);
142static int ipv6_chk_same_addr(const struct in6_addr *addr, struct net_device *dev);
143
Alan Sterne041c682006-03-27 01:16:30 -0800144static ATOMIC_NOTIFIER_HEAD(inet6addr_chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Brian Haleyab32ea52006-09-22 14:15:41 -0700146struct ipv6_devconf ipv6_devconf __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 .forwarding = 0,
148 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
149 .mtu6 = IPV6_MIN_MTU,
150 .accept_ra = 1,
151 .accept_redirects = 1,
152 .autoconf = 1,
153 .force_mld_version = 0,
154 .dad_transmits = 1,
155 .rtr_solicits = MAX_RTR_SOLICITATIONS,
156 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
157 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
158#ifdef CONFIG_IPV6_PRIVACY
159 .use_tempaddr = 0,
160 .temp_valid_lft = TEMP_VALID_LIFETIME,
161 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
162 .regen_max_retry = REGEN_MAX_RETRY,
163 .max_desync_factor = MAX_DESYNC_FACTOR,
164#endif
165 .max_addresses = IPV6_MAX_ADDRESSES,
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -0800166 .accept_ra_defrtr = 1,
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -0800167 .accept_ra_pinfo = 1,
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800168#ifdef CONFIG_IPV6_ROUTER_PREF
169 .accept_ra_rtr_pref = 1,
YOSHIFUJI Hideaki52e16352006-03-20 17:05:47 -0800170 .rtr_probe_interval = 60 * HZ,
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -0800171#ifdef CONFIG_IPV6_ROUTE_INFO
172 .accept_ra_rt_info_max_plen = 0,
173#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800174#endif
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -0700175 .proxy_ndp = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176};
177
Brian Haleyab32ea52006-09-22 14:15:41 -0700178static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 .forwarding = 0,
180 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
181 .mtu6 = IPV6_MIN_MTU,
182 .accept_ra = 1,
183 .accept_redirects = 1,
184 .autoconf = 1,
185 .dad_transmits = 1,
186 .rtr_solicits = MAX_RTR_SOLICITATIONS,
187 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
188 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
189#ifdef CONFIG_IPV6_PRIVACY
190 .use_tempaddr = 0,
191 .temp_valid_lft = TEMP_VALID_LIFETIME,
192 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
193 .regen_max_retry = REGEN_MAX_RETRY,
194 .max_desync_factor = MAX_DESYNC_FACTOR,
195#endif
196 .max_addresses = IPV6_MAX_ADDRESSES,
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -0800197 .accept_ra_defrtr = 1,
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -0800198 .accept_ra_pinfo = 1,
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800199#ifdef CONFIG_IPV6_ROUTER_PREF
200 .accept_ra_rtr_pref = 1,
YOSHIFUJI Hideaki52e16352006-03-20 17:05:47 -0800201 .rtr_probe_interval = 60 * HZ,
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -0800202#ifdef CONFIG_IPV6_ROUTE_INFO
203 .accept_ra_rt_info_max_plen = 0,
204#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800205#endif
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -0700206 .proxy_ndp = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207};
208
209/* IPv6 Wildcard Address and Loopback Address defined by RFC2553 */
210#if 0
211const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
212#endif
213const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
214
YOSHIFUJI Hideakib1cacb62005-11-08 09:38:12 -0800215#define IPV6_ADDR_SCOPE_TYPE(scope) ((scope) << 16)
216
217static inline unsigned ipv6_addr_scope2type(unsigned scope)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
YOSHIFUJI Hideakib1cacb62005-11-08 09:38:12 -0800219 switch(scope) {
220 case IPV6_ADDR_SCOPE_NODELOCAL:
221 return (IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_NODELOCAL) |
222 IPV6_ADDR_LOOPBACK);
223 case IPV6_ADDR_SCOPE_LINKLOCAL:
224 return (IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_LINKLOCAL) |
225 IPV6_ADDR_LINKLOCAL);
226 case IPV6_ADDR_SCOPE_SITELOCAL:
227 return (IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_SITELOCAL) |
228 IPV6_ADDR_SITELOCAL);
229 }
230 return IPV6_ADDR_SCOPE_TYPE(scope);
231}
232
233int __ipv6_addr_type(const struct in6_addr *addr)
234{
Al Viroe69a4ad2006-11-14 20:56:00 -0800235 __be32 st;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
237 st = addr->s6_addr32[0];
238
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 /* Consider all addresses with the first three bits different of
YOSHIFUJI Hideakib1cacb62005-11-08 09:38:12 -0800240 000 and 111 as unicasts.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 */
242 if ((st & htonl(0xE0000000)) != htonl(0x00000000) &&
243 (st & htonl(0xE0000000)) != htonl(0xE0000000))
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900244 return (IPV6_ADDR_UNICAST |
YOSHIFUJI Hideakib1cacb62005-11-08 09:38:12 -0800245 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
YOSHIFUJI Hideakib1cacb62005-11-08 09:38:12 -0800247 if ((st & htonl(0xFF000000)) == htonl(0xFF000000)) {
248 /* multicast */
249 /* addr-select 3.1 */
250 return (IPV6_ADDR_MULTICAST |
251 ipv6_addr_scope2type(IPV6_ADDR_MC_SCOPE(addr)));
252 }
253
254 if ((st & htonl(0xFFC00000)) == htonl(0xFE800000))
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900255 return (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_UNICAST |
YOSHIFUJI Hideakib1cacb62005-11-08 09:38:12 -0800256 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_LINKLOCAL)); /* addr-select 3.1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 if ((st & htonl(0xFFC00000)) == htonl(0xFEC00000))
YOSHIFUJI Hideakib1cacb62005-11-08 09:38:12 -0800258 return (IPV6_ADDR_SITELOCAL | IPV6_ADDR_UNICAST |
259 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_SITELOCAL)); /* addr-select 3.1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
261 if ((addr->s6_addr32[0] | addr->s6_addr32[1]) == 0) {
262 if (addr->s6_addr32[2] == 0) {
263 if (addr->s6_addr32[3] == 0)
264 return IPV6_ADDR_ANY;
265
266 if (addr->s6_addr32[3] == htonl(0x00000001))
YOSHIFUJI Hideakib1cacb62005-11-08 09:38:12 -0800267 return (IPV6_ADDR_LOOPBACK | IPV6_ADDR_UNICAST |
268 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_LINKLOCAL)); /* addr-select 3.4 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
YOSHIFUJI Hideakib1cacb62005-11-08 09:38:12 -0800270 return (IPV6_ADDR_COMPATv4 | IPV6_ADDR_UNICAST |
271 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL)); /* addr-select 3.3 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 }
273
274 if (addr->s6_addr32[2] == htonl(0x0000ffff))
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900275 return (IPV6_ADDR_MAPPED |
YOSHIFUJI Hideakib1cacb62005-11-08 09:38:12 -0800276 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL)); /* addr-select 3.3 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 }
278
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900279 return (IPV6_ADDR_RESERVED |
YOSHIFUJI Hideakib1cacb62005-11-08 09:38:12 -0800280 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL)); /* addr-select 3.4 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281}
282
283static void addrconf_del_timer(struct inet6_ifaddr *ifp)
284{
285 if (del_timer(&ifp->timer))
286 __in6_ifa_put(ifp);
287}
288
289enum addrconf_timer_t
290{
291 AC_NONE,
292 AC_DAD,
293 AC_RS,
294};
295
296static void addrconf_mod_timer(struct inet6_ifaddr *ifp,
297 enum addrconf_timer_t what,
298 unsigned long when)
299{
300 if (!del_timer(&ifp->timer))
301 in6_ifa_hold(ifp);
302
303 switch (what) {
304 case AC_DAD:
305 ifp->timer.function = addrconf_dad_timer;
306 break;
307 case AC_RS:
308 ifp->timer.function = addrconf_rs_timer;
309 break;
310 default:;
311 }
312 ifp->timer.expires = jiffies + when;
313 add_timer(&ifp->timer);
314}
315
316/* Nobody refers to this device, we may destroy it. */
317
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -0700318static void in6_dev_finish_destroy_rcu(struct rcu_head *head)
319{
320 struct inet6_dev *idev = container_of(head, struct inet6_dev, rcu);
321 kfree(idev);
322}
323
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324void in6_dev_finish_destroy(struct inet6_dev *idev)
325{
326 struct net_device *dev = idev->dev;
327 BUG_TRAP(idev->addr_list==NULL);
328 BUG_TRAP(idev->mc_list==NULL);
329#ifdef NET_REFCNT_DEBUG
330 printk(KERN_DEBUG "in6_dev_finish_destroy: %s\n", dev ? dev->name : "NIL");
331#endif
332 dev_put(dev);
333 if (!idev->dead) {
334 printk("Freeing alive inet6 device %p\n", idev);
335 return;
336 }
337 snmp6_free_dev(idev);
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -0700338 call_rcu(&idev->rcu, in6_dev_finish_destroy_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339}
340
341static struct inet6_dev * ipv6_add_dev(struct net_device *dev)
342{
343 struct inet6_dev *ndev;
YOSHIFUJI Hideakid88ae4c2007-01-14 21:48:40 -0800344 struct in6_addr maddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
346 ASSERT_RTNL();
347
348 if (dev->mtu < IPV6_MIN_MTU)
349 return NULL;
350
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900351 ndev = kzalloc(sizeof(struct inet6_dev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900353 if (ndev == NULL)
354 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
Ingo Oeser322f74a2006-03-20 23:01:47 -0800356 rwlock_init(&ndev->lock);
357 ndev->dev = dev;
358 memcpy(&ndev->cnf, &ipv6_devconf_dflt, sizeof(ndev->cnf));
359 ndev->cnf.mtu6 = dev->mtu;
360 ndev->cnf.sysctl = NULL;
361 ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl);
362 if (ndev->nd_parms == NULL) {
363 kfree(ndev);
364 return NULL;
365 }
366 /* We refer to the device */
367 dev_hold(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
Ingo Oeser322f74a2006-03-20 23:01:47 -0800369 if (snmp6_alloc_dev(ndev) < 0) {
370 ADBG((KERN_WARNING
371 "%s(): cannot allocate memory for statistics; dev=%s.\n",
372 __FUNCTION__, dev->name));
373 neigh_parms_release(&nd_tbl, ndev->nd_parms);
374 ndev->dead = 1;
375 in6_dev_finish_destroy(ndev);
376 return NULL;
377 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Ingo Oeser322f74a2006-03-20 23:01:47 -0800379 if (snmp6_register_dev(ndev) < 0) {
380 ADBG((KERN_WARNING
381 "%s(): cannot create /proc/net/dev_snmp6/%s\n",
382 __FUNCTION__, dev->name));
383 neigh_parms_release(&nd_tbl, ndev->nd_parms);
384 ndev->dead = 1;
385 in6_dev_finish_destroy(ndev);
386 return NULL;
387 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Ingo Oeser322f74a2006-03-20 23:01:47 -0800389 /* One reference from device. We must do this before
390 * we invoke __ipv6_regen_rndid().
391 */
392 in6_dev_hold(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
394#ifdef CONFIG_IPV6_PRIVACY
Ingo Oeser322f74a2006-03-20 23:01:47 -0800395 init_timer(&ndev->regen_timer);
396 ndev->regen_timer.function = ipv6_regen_rndid;
397 ndev->regen_timer.data = (unsigned long) ndev;
398 if ((dev->flags&IFF_LOOPBACK) ||
399 dev->type == ARPHRD_TUNNEL ||
Joerg Roedel0be669b2006-10-10 14:49:53 -0700400#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
401 dev->type == ARPHRD_SIT ||
402#endif
403 dev->type == ARPHRD_NONE) {
Ingo Oeser322f74a2006-03-20 23:01:47 -0800404 printk(KERN_INFO
405 "%s: Disabled Privacy Extensions\n",
406 dev->name);
407 ndev->cnf.use_tempaddr = -1;
408 } else {
409 in6_dev_hold(ndev);
410 ipv6_regen_rndid((unsigned long) ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 }
Ingo Oeser322f74a2006-03-20 23:01:47 -0800412#endif
413
414 if (netif_carrier_ok(dev))
415 ndev->if_flags |= IF_READY;
416
Ingo Oeser322f74a2006-03-20 23:01:47 -0800417
418 ipv6_mc_init_dev(ndev);
419 ndev->tstamp = jiffies;
420#ifdef CONFIG_SYSCTL
421 neigh_sysctl_register(dev, ndev->nd_parms, NET_IPV6,
422 NET_IPV6_NEIGH, "ipv6",
423 &ndisc_ifinfo_sysctl_change,
424 NULL);
425 addrconf_sysctl_register(ndev, &ndev->cnf);
426#endif
David L Stevens30c4cf52007-01-04 12:31:14 -0800427 /* protected by rtnl_lock */
428 rcu_assign_pointer(dev->ip6_ptr, ndev);
YOSHIFUJI Hideakid88ae4c2007-01-14 21:48:40 -0800429
430 /* Join all-node multicast group */
431 ipv6_addr_all_nodes(&maddr);
432 ipv6_dev_mc_inc(dev, &maddr);
433
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 return ndev;
435}
436
437static struct inet6_dev * ipv6_find_idev(struct net_device *dev)
438{
439 struct inet6_dev *idev;
440
441 ASSERT_RTNL();
442
443 if ((idev = __in6_dev_get(dev)) == NULL) {
444 if ((idev = ipv6_add_dev(dev)) == NULL)
445 return NULL;
446 }
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +0900447
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 if (dev->flags&IFF_UP)
449 ipv6_mc_up(idev);
450 return idev;
451}
452
453#ifdef CONFIG_SYSCTL
454static void dev_forward_change(struct inet6_dev *idev)
455{
456 struct net_device *dev;
457 struct inet6_ifaddr *ifa;
458 struct in6_addr addr;
459
460 if (!idev)
461 return;
462 dev = idev->dev;
463 if (dev && (dev->flags & IFF_MULTICAST)) {
464 ipv6_addr_all_routers(&addr);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900465
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 if (idev->cnf.forwarding)
467 ipv6_dev_mc_inc(dev, &addr);
468 else
469 ipv6_dev_mc_dec(dev, &addr);
470 }
471 for (ifa=idev->addr_list; ifa; ifa=ifa->if_next) {
472 if (idev->cnf.forwarding)
473 addrconf_join_anycast(ifa);
474 else
475 addrconf_leave_anycast(ifa);
476 }
477}
478
479
480static void addrconf_forward_change(void)
481{
482 struct net_device *dev;
483 struct inet6_dev *idev;
484
485 read_lock(&dev_base_lock);
486 for (dev=dev_base; dev; dev=dev->next) {
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -0700487 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 idev = __in6_dev_get(dev);
489 if (idev) {
490 int changed = (!idev->cnf.forwarding) ^ (!ipv6_devconf.forwarding);
491 idev->cnf.forwarding = ipv6_devconf.forwarding;
492 if (changed)
493 dev_forward_change(idev);
494 }
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -0700495 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 }
497 read_unlock(&dev_base_lock);
498}
499#endif
500
501/* Nobody refers to this ifaddr, destroy it */
502
503void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp)
504{
505 BUG_TRAP(ifp->if_next==NULL);
506 BUG_TRAP(ifp->lst_next==NULL);
507#ifdef NET_REFCNT_DEBUG
508 printk(KERN_DEBUG "inet6_ifa_finish_destroy\n");
509#endif
510
511 in6_dev_put(ifp->idev);
512
513 if (del_timer(&ifp->timer))
514 printk("Timer is still running, when freeing ifa=%p\n", ifp);
515
516 if (!ifp->dead) {
517 printk("Freeing alive inet6 address %p\n", ifp);
518 return;
519 }
520 dst_release(&ifp->rt->u.dst);
521
522 kfree(ifp);
523}
524
Brian Haleye55ffac2006-07-10 15:25:51 -0700525static void
526ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp)
527{
528 struct inet6_ifaddr *ifa, **ifap;
YOSHIFUJI Hideaki8a6ce0c2006-07-11 13:05:30 -0700529 int ifp_scope = ipv6_addr_src_scope(&ifp->addr);
Brian Haleye55ffac2006-07-10 15:25:51 -0700530
531 /*
532 * Each device address list is sorted in order of scope -
533 * global before linklocal.
534 */
535 for (ifap = &idev->addr_list; (ifa = *ifap) != NULL;
536 ifap = &ifa->if_next) {
YOSHIFUJI Hideaki8a6ce0c2006-07-11 13:05:30 -0700537 if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr))
Brian Haleye55ffac2006-07-10 15:25:51 -0700538 break;
539 }
540
541 ifp->if_next = *ifap;
542 *ifap = ifp;
543}
544
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545/* On success it returns ifp with increased reference count */
546
547static struct inet6_ifaddr *
548ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr, int pfxlen,
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -0700549 int scope, u32 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550{
551 struct inet6_ifaddr *ifa = NULL;
552 struct rt6_info *rt;
553 int hash;
554 int err = 0;
555
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -0700556 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 if (idev->dead) {
558 err = -ENODEV; /*XXX*/
559 goto out2;
560 }
561
562 write_lock(&addrconf_hash_lock);
563
564 /* Ignore adding duplicate addresses on an interface */
565 if (ipv6_chk_same_addr(addr, idev->dev)) {
566 ADBG(("ipv6_add_addr: already assigned\n"));
567 err = -EEXIST;
568 goto out;
569 }
570
Ingo Oeser322f74a2006-03-20 23:01:47 -0800571 ifa = kzalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
573 if (ifa == NULL) {
574 ADBG(("ipv6_add_addr: malloc failed\n"));
575 err = -ENOBUFS;
576 goto out;
577 }
578
579 rt = addrconf_dst_alloc(idev, addr, 0);
580 if (IS_ERR(rt)) {
581 err = PTR_ERR(rt);
582 goto out;
583 }
584
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 ipv6_addr_copy(&ifa->addr, addr);
586
587 spin_lock_init(&ifa->lock);
588 init_timer(&ifa->timer);
589 ifa->timer.data = (unsigned long) ifa;
590 ifa->scope = scope;
591 ifa->prefix_len = pfxlen;
592 ifa->flags = flags | IFA_F_TENTATIVE;
593 ifa->cstamp = ifa->tstamp = jiffies;
594
Keir Fraser57f5f542006-08-29 02:43:49 -0700595 ifa->rt = rt;
596
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 ifa->idev = idev;
598 in6_dev_hold(idev);
599 /* For caller */
600 in6_ifa_hold(ifa);
601
602 /* Add to big hash table */
603 hash = ipv6_addr_hash(addr);
604
605 ifa->lst_next = inet6_addr_lst[hash];
606 inet6_addr_lst[hash] = ifa;
607 in6_ifa_hold(ifa);
608 write_unlock(&addrconf_hash_lock);
609
610 write_lock(&idev->lock);
611 /* Add to inet6_dev unicast addr list. */
Brian Haleye55ffac2006-07-10 15:25:51 -0700612 ipv6_link_dev_addr(idev, ifa);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
614#ifdef CONFIG_IPV6_PRIVACY
615 if (ifa->flags&IFA_F_TEMPORARY) {
616 ifa->tmp_next = idev->tempaddr_list;
617 idev->tempaddr_list = ifa;
618 in6_ifa_hold(ifa);
619 }
620#endif
621
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 in6_ifa_hold(ifa);
623 write_unlock(&idev->lock);
624out2:
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -0700625 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
YOSHIFUJI Hideakifd928332005-04-19 22:27:09 -0700627 if (likely(err == 0))
Alan Sterne041c682006-03-27 01:16:30 -0800628 atomic_notifier_call_chain(&inet6addr_chain, NETDEV_UP, ifa);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 else {
630 kfree(ifa);
631 ifa = ERR_PTR(err);
632 }
633
634 return ifa;
635out:
636 write_unlock(&addrconf_hash_lock);
637 goto out2;
638}
639
640/* This function wants to get referenced ifp and releases it before return */
641
642static void ipv6_del_addr(struct inet6_ifaddr *ifp)
643{
644 struct inet6_ifaddr *ifa, **ifap;
645 struct inet6_dev *idev = ifp->idev;
646 int hash;
647 int deleted = 0, onlink = 0;
648 unsigned long expires = jiffies;
649
650 hash = ipv6_addr_hash(&ifp->addr);
651
652 ifp->dead = 1;
653
654 write_lock_bh(&addrconf_hash_lock);
655 for (ifap = &inet6_addr_lst[hash]; (ifa=*ifap) != NULL;
656 ifap = &ifa->lst_next) {
657 if (ifa == ifp) {
658 *ifap = ifa->lst_next;
659 __in6_ifa_put(ifp);
660 ifa->lst_next = NULL;
661 break;
662 }
663 }
664 write_unlock_bh(&addrconf_hash_lock);
665
666 write_lock_bh(&idev->lock);
667#ifdef CONFIG_IPV6_PRIVACY
668 if (ifp->flags&IFA_F_TEMPORARY) {
669 for (ifap = &idev->tempaddr_list; (ifa=*ifap) != NULL;
670 ifap = &ifa->tmp_next) {
671 if (ifa == ifp) {
672 *ifap = ifa->tmp_next;
673 if (ifp->ifpub) {
674 in6_ifa_put(ifp->ifpub);
675 ifp->ifpub = NULL;
676 }
677 __in6_ifa_put(ifp);
678 ifa->tmp_next = NULL;
679 break;
680 }
681 }
682 }
683#endif
684
Kristian Slavov1d142802005-12-21 18:47:24 -0800685 for (ifap = &idev->addr_list; (ifa=*ifap) != NULL;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 if (ifa == ifp) {
687 *ifap = ifa->if_next;
688 __in6_ifa_put(ifp);
689 ifa->if_next = NULL;
690 if (!(ifp->flags & IFA_F_PERMANENT) || onlink > 0)
691 break;
692 deleted = 1;
Kristian Slavov1d142802005-12-21 18:47:24 -0800693 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 } else if (ifp->flags & IFA_F_PERMANENT) {
695 if (ipv6_prefix_equal(&ifa->addr, &ifp->addr,
696 ifp->prefix_len)) {
697 if (ifa->flags & IFA_F_PERMANENT) {
698 onlink = 1;
699 if (deleted)
700 break;
701 } else {
702 unsigned long lifetime;
703
704 if (!onlink)
705 onlink = -1;
706
707 spin_lock(&ifa->lock);
708 lifetime = min_t(unsigned long,
709 ifa->valid_lft, 0x7fffffffUL/HZ);
710 if (time_before(expires,
711 ifa->tstamp + lifetime * HZ))
712 expires = ifa->tstamp + lifetime * HZ;
713 spin_unlock(&ifa->lock);
714 }
715 }
716 }
Kristian Slavov1d142802005-12-21 18:47:24 -0800717 ifap = &ifa->if_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 }
719 write_unlock_bh(&idev->lock);
720
721 ipv6_ifa_notify(RTM_DELADDR, ifp);
722
Alan Sterne041c682006-03-27 01:16:30 -0800723 atomic_notifier_call_chain(&inet6addr_chain, NETDEV_DOWN, ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
725 addrconf_del_timer(ifp);
726
727 /*
728 * Purge or update corresponding prefix
729 *
730 * 1) we don't purge prefix here if address was not permanent.
731 * prefix is managed by its own lifetime.
732 * 2) if there're no addresses, delete prefix.
733 * 3) if there're still other permanent address(es),
734 * corresponding prefix is still permanent.
735 * 4) otherwise, update prefix lifetime to the
736 * longest valid lifetime among the corresponding
737 * addresses on the device.
738 * Note: subsequent RA will update lifetime.
739 *
740 * --yoshfuji
741 */
742 if ((ifp->flags & IFA_F_PERMANENT) && onlink < 1) {
743 struct in6_addr prefix;
744 struct rt6_info *rt;
745
746 ipv6_addr_prefix(&prefix, &ifp->addr, ifp->prefix_len);
747 rt = rt6_lookup(&prefix, NULL, ifp->idev->dev->ifindex, 1);
748
749 if (rt && ((rt->rt6i_flags & (RTF_GATEWAY | RTF_DEFAULT)) == 0)) {
750 if (onlink == 0) {
Thomas Grafe0a1ad732006-08-22 00:00:21 -0700751 ip6_del_rt(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 rt = NULL;
753 } else if (!(rt->rt6i_flags & RTF_EXPIRES)) {
754 rt->rt6i_expires = expires;
755 rt->rt6i_flags |= RTF_EXPIRES;
756 }
757 }
758 dst_release(&rt->u.dst);
759 }
760
761 in6_ifa_put(ifp);
762}
763
764#ifdef CONFIG_IPV6_PRIVACY
765static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift)
766{
767 struct inet6_dev *idev = ifp->idev;
768 struct in6_addr addr, *tmpaddr;
769 unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_cstamp, tmp_tstamp;
770 int tmp_plen;
771 int ret = 0;
772 int max_addresses;
773
774 write_lock(&idev->lock);
775 if (ift) {
776 spin_lock_bh(&ift->lock);
777 memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8);
778 spin_unlock_bh(&ift->lock);
779 tmpaddr = &addr;
780 } else {
781 tmpaddr = NULL;
782 }
783retry:
784 in6_dev_hold(idev);
785 if (idev->cnf.use_tempaddr <= 0) {
786 write_unlock(&idev->lock);
787 printk(KERN_INFO
788 "ipv6_create_tempaddr(): use_tempaddr is disabled.\n");
789 in6_dev_put(idev);
790 ret = -1;
791 goto out;
792 }
793 spin_lock_bh(&ifp->lock);
794 if (ifp->regen_count++ >= idev->cnf.regen_max_retry) {
795 idev->cnf.use_tempaddr = -1; /*XXX*/
796 spin_unlock_bh(&ifp->lock);
797 write_unlock(&idev->lock);
798 printk(KERN_WARNING
799 "ipv6_create_tempaddr(): regeneration time exceeded. disabled temporary address support.\n");
800 in6_dev_put(idev);
801 ret = -1;
802 goto out;
803 }
804 in6_ifa_hold(ifp);
805 memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
806 if (__ipv6_try_regen_rndid(idev, tmpaddr) < 0) {
807 spin_unlock_bh(&ifp->lock);
808 write_unlock(&idev->lock);
809 printk(KERN_WARNING
810 "ipv6_create_tempaddr(): regeneration of randomized interface id failed.\n");
811 in6_ifa_put(ifp);
812 in6_dev_put(idev);
813 ret = -1;
814 goto out;
815 }
816 memcpy(&addr.s6_addr[8], idev->rndid, 8);
817 tmp_valid_lft = min_t(__u32,
818 ifp->valid_lft,
819 idev->cnf.temp_valid_lft);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900820 tmp_prefered_lft = min_t(__u32,
821 ifp->prefered_lft,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 idev->cnf.temp_prefered_lft - desync_factor / HZ);
823 tmp_plen = ifp->prefix_len;
824 max_addresses = idev->cnf.max_addresses;
825 tmp_cstamp = ifp->cstamp;
826 tmp_tstamp = ifp->tstamp;
827 spin_unlock_bh(&ifp->lock);
828
829 write_unlock(&idev->lock);
830 ift = !max_addresses ||
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900831 ipv6_count_addresses(idev) < max_addresses ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 ipv6_add_addr(idev, &addr, tmp_plen,
833 ipv6_addr_type(&addr)&IPV6_ADDR_SCOPE_MASK, IFA_F_TEMPORARY) : NULL;
834 if (!ift || IS_ERR(ift)) {
835 in6_ifa_put(ifp);
836 in6_dev_put(idev);
837 printk(KERN_INFO
838 "ipv6_create_tempaddr(): retry temporary address regeneration.\n");
839 tmpaddr = &addr;
840 write_lock(&idev->lock);
841 goto retry;
842 }
843
844 spin_lock_bh(&ift->lock);
845 ift->ifpub = ifp;
846 ift->valid_lft = tmp_valid_lft;
847 ift->prefered_lft = tmp_prefered_lft;
848 ift->cstamp = tmp_cstamp;
849 ift->tstamp = tmp_tstamp;
850 spin_unlock_bh(&ift->lock);
851
852 addrconf_dad_start(ift, 0);
853 in6_ifa_put(ift);
854 in6_dev_put(idev);
855out:
856 return ret;
857}
858#endif
859
860/*
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800861 * Choose an appropriate source address (RFC3484)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 */
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800863struct ipv6_saddr_score {
864 int addr_type;
865 unsigned int attrs;
866 int matchlen;
Brian Haley0d27b422006-03-11 18:50:14 -0800867 int scope;
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800868 unsigned int rule;
869};
870
871#define IPV6_SADDR_SCORE_LOCAL 0x0001
872#define IPV6_SADDR_SCORE_PREFERRED 0x0004
873#define IPV6_SADDR_SCORE_HOA 0x0008
874#define IPV6_SADDR_SCORE_OIF 0x0010
875#define IPV6_SADDR_SCORE_LABEL 0x0020
876#define IPV6_SADDR_SCORE_PRIVACY 0x0040
877
878static int inline ipv6_saddr_preferred(int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879{
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800880 if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|
881 IPV6_ADDR_LOOPBACK|IPV6_ADDR_RESERVED))
882 return 1;
883 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884}
885
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800886/* static matching label */
887static int inline ipv6_saddr_label(const struct in6_addr *addr, int type)
888{
889 /*
890 * prefix (longest match) label
891 * -----------------------------
892 * ::1/128 0
893 * ::/0 1
894 * 2002::/16 2
895 * ::/96 3
896 * ::ffff:0:0/96 4
Łukasz Stelmach102128e2006-06-22 01:37:19 -0700897 * fc00::/7 5
898 * 2001::/32 6
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800899 */
900 if (type & IPV6_ADDR_LOOPBACK)
901 return 0;
902 else if (type & IPV6_ADDR_COMPATv4)
903 return 3;
904 else if (type & IPV6_ADDR_MAPPED)
905 return 4;
Łukasz Stelmach102128e2006-06-22 01:37:19 -0700906 else if (addr->s6_addr32[0] == htonl(0x20010000))
907 return 6;
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800908 else if (addr->s6_addr16[0] == htons(0x2002))
909 return 2;
Łukasz Stelmach102128e2006-06-22 01:37:19 -0700910 else if ((addr->s6_addr[0] & 0xfe) == 0xfc)
911 return 5;
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800912 return 1;
913}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800915int ipv6_dev_get_saddr(struct net_device *daddr_dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 struct in6_addr *daddr, struct in6_addr *saddr)
917{
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800918 struct ipv6_saddr_score hiscore;
919 struct inet6_ifaddr *ifa_result = NULL;
920 int daddr_type = __ipv6_addr_type(daddr);
921 int daddr_scope = __ipv6_addr_src_scope(daddr_type);
922 u32 daddr_label = ipv6_saddr_label(daddr, daddr_type);
923 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800925 memset(&hiscore, 0, sizeof(hiscore));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
927 read_lock(&dev_base_lock);
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -0700928 rcu_read_lock();
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800929
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 for (dev = dev_base; dev; dev=dev->next) {
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800931 struct inet6_dev *idev;
932 struct inet6_ifaddr *ifa;
933
934 /* Rule 0: Candidate Source Address (section 4)
935 * - multicast and link-local destination address,
936 * the set of candidate source address MUST only
937 * include addresses assigned to interfaces
938 * belonging to the same link as the outgoing
939 * interface.
940 * (- For site-local destination addresses, the
941 * set of candidate source addresses MUST only
942 * include addresses assigned to interfaces
943 * belonging to the same site as the outgoing
944 * interface.)
945 */
946 if ((daddr_type & IPV6_ADDR_MULTICAST ||
947 daddr_scope <= IPV6_ADDR_SCOPE_LINKLOCAL) &&
948 daddr_dev && dev != daddr_dev)
949 continue;
950
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 idev = __in6_dev_get(dev);
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800952 if (!idev)
953 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800955 read_lock_bh(&idev->lock);
956 for (ifa = idev->addr_list; ifa; ifa = ifa->if_next) {
957 struct ipv6_saddr_score score;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800959 score.addr_type = __ipv6_addr_type(&ifa->addr);
960
YOSHIFUJI Hideaki6b3ae802005-12-21 22:58:01 +0900961 /* Rule 0:
962 * - Tentative Address (RFC2462 section 5.4)
963 * - A tentative address is not considered
964 * "assigned to an interface" in the traditional
965 * sense.
966 * - Candidate Source Address (section 4)
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800967 * - In any case, anycast addresses, multicast
968 * addresses, and the unspecified address MUST
969 * NOT be included in a candidate set.
970 */
YOSHIFUJI Hideaki6b3ae802005-12-21 22:58:01 +0900971 if (ifa->flags & IFA_F_TENTATIVE)
972 continue;
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800973 if (unlikely(score.addr_type == IPV6_ADDR_ANY ||
974 score.addr_type & IPV6_ADDR_MULTICAST)) {
975 LIMIT_NETDEBUG(KERN_DEBUG
976 "ADDRCONF: unspecified / multicast address"
977 "assigned as unicast address on %s",
978 dev->name);
979 continue;
980 }
981
982 score.attrs = 0;
983 score.matchlen = 0;
984 score.scope = 0;
985 score.rule = 0;
986
987 if (ifa_result == NULL) {
988 /* record it if the first available entry */
989 goto record_it;
990 }
991
992 /* Rule 1: Prefer same address */
993 if (hiscore.rule < 1) {
994 if (ipv6_addr_equal(&ifa_result->addr, daddr))
995 hiscore.attrs |= IPV6_SADDR_SCORE_LOCAL;
996 hiscore.rule++;
997 }
998 if (ipv6_addr_equal(&ifa->addr, daddr)) {
999 score.attrs |= IPV6_SADDR_SCORE_LOCAL;
1000 if (!(hiscore.attrs & IPV6_SADDR_SCORE_LOCAL)) {
1001 score.rule = 1;
1002 goto record_it;
1003 }
1004 } else {
1005 if (hiscore.attrs & IPV6_SADDR_SCORE_LOCAL)
1006 continue;
1007 }
1008
1009 /* Rule 2: Prefer appropriate scope */
1010 if (hiscore.rule < 2) {
1011 hiscore.scope = __ipv6_addr_src_scope(hiscore.addr_type);
1012 hiscore.rule++;
1013 }
1014 score.scope = __ipv6_addr_src_scope(score.addr_type);
1015 if (hiscore.scope < score.scope) {
1016 if (hiscore.scope < daddr_scope) {
1017 score.rule = 2;
1018 goto record_it;
1019 } else
1020 continue;
1021 } else if (score.scope < hiscore.scope) {
1022 if (score.scope < daddr_scope)
Brian Haleye55ffac2006-07-10 15:25:51 -07001023 break; /* addresses sorted by scope */
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001024 else {
1025 score.rule = 2;
1026 goto record_it;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 }
1028 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001030 /* Rule 3: Avoid deprecated address */
1031 if (hiscore.rule < 3) {
1032 if (ipv6_saddr_preferred(hiscore.addr_type) ||
1033 !(ifa_result->flags & IFA_F_DEPRECATED))
1034 hiscore.attrs |= IPV6_SADDR_SCORE_PREFERRED;
1035 hiscore.rule++;
1036 }
1037 if (ipv6_saddr_preferred(score.addr_type) ||
1038 !(ifa->flags & IFA_F_DEPRECATED)) {
1039 score.attrs |= IPV6_SADDR_SCORE_PREFERRED;
1040 if (!(hiscore.attrs & IPV6_SADDR_SCORE_PREFERRED)) {
1041 score.rule = 3;
1042 goto record_it;
1043 }
1044 } else {
1045 if (hiscore.attrs & IPV6_SADDR_SCORE_PREFERRED)
1046 continue;
1047 }
1048
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07001049 /* Rule 4: Prefer home address */
1050#ifdef CONFIG_IPV6_MIP6
1051 if (hiscore.rule < 4) {
1052 if (ifa_result->flags & IFA_F_HOMEADDRESS)
1053 hiscore.attrs |= IPV6_SADDR_SCORE_HOA;
1054 hiscore.rule++;
1055 }
1056 if (ifa->flags & IFA_F_HOMEADDRESS) {
1057 score.attrs |= IPV6_SADDR_SCORE_HOA;
1058 if (!(ifa_result->flags & IFA_F_HOMEADDRESS)) {
1059 score.rule = 4;
1060 goto record_it;
1061 }
1062 } else {
1063 if (hiscore.attrs & IPV6_SADDR_SCORE_HOA)
1064 continue;
1065 }
1066#else
YOSHIFUJI Hideaki220bbd72005-11-28 22:27:11 -08001067 if (hiscore.rule < 4)
1068 hiscore.rule++;
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07001069#endif
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001070
1071 /* Rule 5: Prefer outgoing interface */
1072 if (hiscore.rule < 5) {
1073 if (daddr_dev == NULL ||
1074 daddr_dev == ifa_result->idev->dev)
1075 hiscore.attrs |= IPV6_SADDR_SCORE_OIF;
1076 hiscore.rule++;
1077 }
1078 if (daddr_dev == NULL ||
1079 daddr_dev == ifa->idev->dev) {
1080 score.attrs |= IPV6_SADDR_SCORE_OIF;
1081 if (!(hiscore.attrs & IPV6_SADDR_SCORE_OIF)) {
1082 score.rule = 5;
1083 goto record_it;
1084 }
1085 } else {
1086 if (hiscore.attrs & IPV6_SADDR_SCORE_OIF)
1087 continue;
1088 }
1089
1090 /* Rule 6: Prefer matching label */
1091 if (hiscore.rule < 6) {
1092 if (ipv6_saddr_label(&ifa_result->addr, hiscore.addr_type) == daddr_label)
1093 hiscore.attrs |= IPV6_SADDR_SCORE_LABEL;
1094 hiscore.rule++;
1095 }
1096 if (ipv6_saddr_label(&ifa->addr, score.addr_type) == daddr_label) {
1097 score.attrs |= IPV6_SADDR_SCORE_LABEL;
1098 if (!(hiscore.attrs & IPV6_SADDR_SCORE_LABEL)) {
1099 score.rule = 6;
1100 goto record_it;
1101 }
1102 } else {
1103 if (hiscore.attrs & IPV6_SADDR_SCORE_LABEL)
1104 continue;
1105 }
1106
Peter Chubb44fd0262005-11-09 13:05:47 -08001107#ifdef CONFIG_IPV6_PRIVACY
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001108 /* Rule 7: Prefer public address
1109 * Note: prefer temprary address if use_tempaddr >= 2
1110 */
1111 if (hiscore.rule < 7) {
1112 if ((!(ifa_result->flags & IFA_F_TEMPORARY)) ^
1113 (ifa_result->idev->cnf.use_tempaddr >= 2))
1114 hiscore.attrs |= IPV6_SADDR_SCORE_PRIVACY;
1115 hiscore.rule++;
1116 }
1117 if ((!(ifa->flags & IFA_F_TEMPORARY)) ^
1118 (ifa->idev->cnf.use_tempaddr >= 2)) {
1119 score.attrs |= IPV6_SADDR_SCORE_PRIVACY;
1120 if (!(hiscore.attrs & IPV6_SADDR_SCORE_PRIVACY)) {
1121 score.rule = 7;
1122 goto record_it;
1123 }
1124 } else {
1125 if (hiscore.attrs & IPV6_SADDR_SCORE_PRIVACY)
1126 continue;
1127 }
YOSHIFUJI Hideaki5e2707f2006-06-22 01:41:18 -07001128#else
1129 if (hiscore.rule < 7)
1130 hiscore.rule++;
Peter Chubb44fd0262005-11-09 13:05:47 -08001131#endif
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001132 /* Rule 8: Use longest matching prefix */
Yan Zheng12da2a42005-11-14 21:42:46 -08001133 if (hiscore.rule < 8) {
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001134 hiscore.matchlen = ipv6_addr_diff(&ifa_result->addr, daddr);
Yan Zheng12da2a42005-11-14 21:42:46 -08001135 hiscore.rule++;
1136 }
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001137 score.matchlen = ipv6_addr_diff(&ifa->addr, daddr);
1138 if (score.matchlen > hiscore.matchlen) {
1139 score.rule = 8;
1140 goto record_it;
1141 }
1142#if 0
1143 else if (score.matchlen < hiscore.matchlen)
1144 continue;
1145#endif
1146
1147 /* Final Rule: choose first available one */
1148 continue;
1149record_it:
1150 if (ifa_result)
1151 in6_ifa_put(ifa_result);
1152 in6_ifa_hold(ifa);
1153 ifa_result = ifa;
1154 hiscore = score;
1155 }
1156 read_unlock_bh(&idev->lock);
1157 }
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001158 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 read_unlock(&dev_base_lock);
1160
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001161 if (!ifa_result)
1162 return -EADDRNOTAVAIL;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001163
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001164 ipv6_addr_copy(saddr, &ifa_result->addr);
1165 in6_ifa_put(ifa_result);
1166 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167}
1168
1169
1170int ipv6_get_saddr(struct dst_entry *dst,
1171 struct in6_addr *daddr, struct in6_addr *saddr)
1172{
YOSHIFUJI Hideaki7a3025b2006-10-13 16:17:25 +09001173 return ipv6_dev_get_saddr(dst ? ip6_dst_idev(dst)->dev : NULL, daddr, saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174}
1175
1176
1177int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr)
1178{
1179 struct inet6_dev *idev;
1180 int err = -EADDRNOTAVAIL;
1181
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001182 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 if ((idev = __in6_dev_get(dev)) != NULL) {
1184 struct inet6_ifaddr *ifp;
1185
1186 read_lock_bh(&idev->lock);
1187 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
1188 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
1189 ipv6_addr_copy(addr, &ifp->addr);
1190 err = 0;
1191 break;
1192 }
1193 }
1194 read_unlock_bh(&idev->lock);
1195 }
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001196 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 return err;
1198}
1199
1200static int ipv6_count_addresses(struct inet6_dev *idev)
1201{
1202 int cnt = 0;
1203 struct inet6_ifaddr *ifp;
1204
1205 read_lock_bh(&idev->lock);
1206 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next)
1207 cnt++;
1208 read_unlock_bh(&idev->lock);
1209 return cnt;
1210}
1211
1212int ipv6_chk_addr(struct in6_addr *addr, struct net_device *dev, int strict)
1213{
1214 struct inet6_ifaddr * ifp;
1215 u8 hash = ipv6_addr_hash(addr);
1216
1217 read_lock_bh(&addrconf_hash_lock);
1218 for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
1219 if (ipv6_addr_equal(&ifp->addr, addr) &&
1220 !(ifp->flags&IFA_F_TENTATIVE)) {
1221 if (dev == NULL || ifp->idev->dev == dev ||
1222 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))
1223 break;
1224 }
1225 }
1226 read_unlock_bh(&addrconf_hash_lock);
1227 return ifp != NULL;
1228}
1229
1230static
1231int ipv6_chk_same_addr(const struct in6_addr *addr, struct net_device *dev)
1232{
1233 struct inet6_ifaddr * ifp;
1234 u8 hash = ipv6_addr_hash(addr);
1235
1236 for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
1237 if (ipv6_addr_equal(&ifp->addr, addr)) {
1238 if (dev == NULL || ifp->idev->dev == dev)
1239 break;
1240 }
1241 }
1242 return ifp != NULL;
1243}
1244
1245struct inet6_ifaddr * ipv6_get_ifaddr(struct in6_addr *addr, struct net_device *dev, int strict)
1246{
1247 struct inet6_ifaddr * ifp;
1248 u8 hash = ipv6_addr_hash(addr);
1249
1250 read_lock_bh(&addrconf_hash_lock);
1251 for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
1252 if (ipv6_addr_equal(&ifp->addr, addr)) {
1253 if (dev == NULL || ifp->idev->dev == dev ||
1254 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
1255 in6_ifa_hold(ifp);
1256 break;
1257 }
1258 }
1259 }
1260 read_unlock_bh(&addrconf_hash_lock);
1261
1262 return ifp;
1263}
1264
1265int ipv6_rcv_saddr_equal(const struct sock *sk, const struct sock *sk2)
1266{
1267 const struct in6_addr *sk_rcv_saddr6 = &inet6_sk(sk)->rcv_saddr;
Arnaldo Carvalho de Melo0fa1a532005-12-13 23:23:09 -08001268 const struct in6_addr *sk2_rcv_saddr6 = inet6_rcv_saddr(sk2);
Al Viro82103232006-09-27 18:44:10 -07001269 __be32 sk_rcv_saddr = inet_sk(sk)->rcv_saddr;
1270 __be32 sk2_rcv_saddr = inet_rcv_saddr(sk2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 int sk_ipv6only = ipv6_only_sock(sk);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001272 int sk2_ipv6only = inet_v6_ipv6only(sk2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 int addr_type = ipv6_addr_type(sk_rcv_saddr6);
1274 int addr_type2 = sk2_rcv_saddr6 ? ipv6_addr_type(sk2_rcv_saddr6) : IPV6_ADDR_MAPPED;
1275
1276 if (!sk2_rcv_saddr && !sk_ipv6only)
1277 return 1;
1278
1279 if (addr_type2 == IPV6_ADDR_ANY &&
1280 !(sk2_ipv6only && addr_type == IPV6_ADDR_MAPPED))
1281 return 1;
1282
1283 if (addr_type == IPV6_ADDR_ANY &&
1284 !(sk_ipv6only && addr_type2 == IPV6_ADDR_MAPPED))
1285 return 1;
1286
1287 if (sk2_rcv_saddr6 &&
1288 ipv6_addr_equal(sk_rcv_saddr6, sk2_rcv_saddr6))
1289 return 1;
1290
1291 if (addr_type == IPV6_ADDR_MAPPED &&
1292 !sk2_ipv6only &&
1293 (!sk2_rcv_saddr || !sk_rcv_saddr || sk_rcv_saddr == sk2_rcv_saddr))
1294 return 1;
1295
1296 return 0;
1297}
1298
1299/* Gets referenced address, destroys ifaddr */
1300
Adrian Bunk9f5336e2006-01-07 13:24:25 -08001301static void addrconf_dad_stop(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 if (ifp->flags&IFA_F_PERMANENT) {
1304 spin_lock_bh(&ifp->lock);
1305 addrconf_del_timer(ifp);
1306 ifp->flags |= IFA_F_TENTATIVE;
1307 spin_unlock_bh(&ifp->lock);
1308 in6_ifa_put(ifp);
1309#ifdef CONFIG_IPV6_PRIVACY
1310 } else if (ifp->flags&IFA_F_TEMPORARY) {
1311 struct inet6_ifaddr *ifpub;
1312 spin_lock_bh(&ifp->lock);
1313 ifpub = ifp->ifpub;
1314 if (ifpub) {
1315 in6_ifa_hold(ifpub);
1316 spin_unlock_bh(&ifp->lock);
1317 ipv6_create_tempaddr(ifpub, ifp);
1318 in6_ifa_put(ifpub);
1319 } else {
1320 spin_unlock_bh(&ifp->lock);
1321 }
1322 ipv6_del_addr(ifp);
1323#endif
1324 } else
1325 ipv6_del_addr(ifp);
1326}
1327
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09001328void addrconf_dad_failure(struct inet6_ifaddr *ifp)
1329{
1330 if (net_ratelimit())
1331 printk(KERN_INFO "%s: duplicate address detected!\n", ifp->idev->dev->name);
1332 addrconf_dad_stop(ifp);
1333}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
1335/* Join to solicited addr multicast group. */
1336
1337void addrconf_join_solict(struct net_device *dev, struct in6_addr *addr)
1338{
1339 struct in6_addr maddr;
1340
1341 if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1342 return;
1343
1344 addrconf_addr_solict_mult(addr, &maddr);
1345 ipv6_dev_mc_inc(dev, &maddr);
1346}
1347
1348void addrconf_leave_solict(struct inet6_dev *idev, struct in6_addr *addr)
1349{
1350 struct in6_addr maddr;
1351
1352 if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1353 return;
1354
1355 addrconf_addr_solict_mult(addr, &maddr);
1356 __ipv6_dev_mc_dec(idev, &maddr);
1357}
1358
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03001359static void addrconf_join_anycast(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360{
1361 struct in6_addr addr;
1362 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1363 if (ipv6_addr_any(&addr))
1364 return;
1365 ipv6_dev_ac_inc(ifp->idev->dev, &addr);
1366}
1367
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03001368static void addrconf_leave_anycast(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369{
1370 struct in6_addr addr;
1371 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1372 if (ipv6_addr_any(&addr))
1373 return;
1374 __ipv6_dev_ac_dec(ifp->idev, &addr);
1375}
1376
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08001377static int addrconf_ifid_eui48(u8 *eui, struct net_device *dev)
1378{
1379 if (dev->addr_len != ETH_ALEN)
1380 return -1;
1381 memcpy(eui, dev->dev_addr, 3);
1382 memcpy(eui + 5, dev->dev_addr + 3, 3);
1383
1384 /*
1385 * The zSeries OSA network cards can be shared among various
1386 * OS instances, but the OSA cards have only one MAC address.
1387 * This leads to duplicate address conflicts in conjunction
1388 * with IPv6 if more than one instance uses the same card.
1389 *
1390 * The driver for these cards can deliver a unique 16-bit
1391 * identifier for each instance sharing the same card. It is
1392 * placed instead of 0xFFFE in the interface identifier. The
1393 * "u" bit of the interface identifier is not inverted in this
1394 * case. Hence the resulting interface identifier has local
1395 * scope according to RFC2373.
1396 */
1397 if (dev->dev_id) {
1398 eui[3] = (dev->dev_id >> 8) & 0xFF;
1399 eui[4] = dev->dev_id & 0xFF;
1400 } else {
1401 eui[3] = 0xFF;
1402 eui[4] = 0xFE;
1403 eui[0] ^= 2;
1404 }
1405 return 0;
1406}
1407
1408static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev)
1409{
1410 /* XXX: inherit EUI-64 from other interface -- yoshfuji */
1411 if (dev->addr_len != ARCNET_ALEN)
1412 return -1;
1413 memset(eui, 0, 7);
1414 eui[7] = *(u8*)dev->dev_addr;
1415 return 0;
1416}
1417
1418static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev)
1419{
1420 if (dev->addr_len != INFINIBAND_ALEN)
1421 return -1;
1422 memcpy(eui, dev->dev_addr + 12, 8);
1423 eui[0] |= 2;
1424 return 0;
1425}
1426
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
1428{
1429 switch (dev->type) {
1430 case ARPHRD_ETHER:
1431 case ARPHRD_FDDI:
1432 case ARPHRD_IEEE802_TR:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08001433 return addrconf_ifid_eui48(eui, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 case ARPHRD_ARCNET:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08001435 return addrconf_ifid_arcnet(eui, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 case ARPHRD_INFINIBAND:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08001437 return addrconf_ifid_infiniband(eui, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 }
1439 return -1;
1440}
1441
1442static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
1443{
1444 int err = -1;
1445 struct inet6_ifaddr *ifp;
1446
1447 read_lock_bh(&idev->lock);
1448 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
1449 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
1450 memcpy(eui, ifp->addr.s6_addr+8, 8);
1451 err = 0;
1452 break;
1453 }
1454 }
1455 read_unlock_bh(&idev->lock);
1456 return err;
1457}
1458
1459#ifdef CONFIG_IPV6_PRIVACY
1460/* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
1461static int __ipv6_regen_rndid(struct inet6_dev *idev)
1462{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463regen:
YOSHIFUJI Hideaki955189e2006-03-20 16:54:09 -08001464 get_random_bytes(idev->rndid, sizeof(idev->rndid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 idev->rndid[0] &= ~0x02;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466
1467 /*
1468 * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
1469 * check if generated address is not inappropriate
1470 *
1471 * - Reserved subnet anycast (RFC 2526)
1472 * 11111101 11....11 1xxxxxxx
1473 * - ISATAP (draft-ietf-ngtrans-isatap-13.txt) 5.1
1474 * 00-00-5E-FE-xx-xx-xx-xx
1475 * - value 0
1476 * - XXX: already assigned to an address on the device
1477 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001478 if (idev->rndid[0] == 0xfd &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff &&
1480 (idev->rndid[7]&0x80))
1481 goto regen;
1482 if ((idev->rndid[0]|idev->rndid[1]) == 0) {
1483 if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe)
1484 goto regen;
1485 if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00)
1486 goto regen;
1487 }
1488
1489 return 0;
1490}
1491
1492static void ipv6_regen_rndid(unsigned long data)
1493{
1494 struct inet6_dev *idev = (struct inet6_dev *) data;
1495 unsigned long expires;
1496
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001497 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 write_lock_bh(&idev->lock);
1499
1500 if (idev->dead)
1501 goto out;
1502
1503 if (__ipv6_regen_rndid(idev) < 0)
1504 goto out;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001505
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 expires = jiffies +
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001507 idev->cnf.temp_prefered_lft * HZ -
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 idev->cnf.regen_max_retry * idev->cnf.dad_transmits * idev->nd_parms->retrans_time - desync_factor;
1509 if (time_before(expires, jiffies)) {
1510 printk(KERN_WARNING
1511 "ipv6_regen_rndid(): too short regeneration interval; timer disabled for %s.\n",
1512 idev->dev->name);
1513 goto out;
1514 }
1515
1516 if (!mod_timer(&idev->regen_timer, expires))
1517 in6_dev_hold(idev);
1518
1519out:
1520 write_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001521 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 in6_dev_put(idev);
1523}
1524
1525static int __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr) {
1526 int ret = 0;
1527
1528 if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0)
1529 ret = __ipv6_regen_rndid(idev);
1530 return ret;
1531}
1532#endif
1533
1534/*
1535 * Add prefix route.
1536 */
1537
1538static void
1539addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev,
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -07001540 unsigned long expires, u32 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541{
Thomas Graf86872cb2006-08-22 00:01:08 -07001542 struct fib6_config cfg = {
1543 .fc_table = RT6_TABLE_PREFIX,
1544 .fc_metric = IP6_RT_PRIO_ADDRCONF,
1545 .fc_ifindex = dev->ifindex,
1546 .fc_expires = expires,
1547 .fc_dst_len = plen,
1548 .fc_flags = RTF_UP | flags,
1549 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
Thomas Graf86872cb2006-08-22 00:01:08 -07001551 ipv6_addr_copy(&cfg.fc_dst, pfx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552
1553 /* Prevent useless cloning on PtP SIT.
1554 This thing is done here expecting that the whole
1555 class of non-broadcast devices need not cloning.
1556 */
Joerg Roedel0be669b2006-10-10 14:49:53 -07001557#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
Thomas Graf86872cb2006-08-22 00:01:08 -07001558 if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT))
1559 cfg.fc_flags |= RTF_NONEXTHOP;
Joerg Roedel0be669b2006-10-10 14:49:53 -07001560#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
Thomas Graf86872cb2006-08-22 00:01:08 -07001562 ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563}
1564
1565/* Create "default" multicast route to the interface */
1566
1567static void addrconf_add_mroute(struct net_device *dev)
1568{
Thomas Graf86872cb2006-08-22 00:01:08 -07001569 struct fib6_config cfg = {
1570 .fc_table = RT6_TABLE_LOCAL,
1571 .fc_metric = IP6_RT_PRIO_ADDRCONF,
1572 .fc_ifindex = dev->ifindex,
1573 .fc_dst_len = 8,
1574 .fc_flags = RTF_UP,
1575 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576
Thomas Graf86872cb2006-08-22 00:01:08 -07001577 ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0);
1578
1579 ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580}
1581
Joerg Roedel0be669b2006-10-10 14:49:53 -07001582#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583static void sit_route_add(struct net_device *dev)
1584{
Thomas Graf86872cb2006-08-22 00:01:08 -07001585 struct fib6_config cfg = {
1586 .fc_table = RT6_TABLE_MAIN,
1587 .fc_metric = IP6_RT_PRIO_ADDRCONF,
1588 .fc_ifindex = dev->ifindex,
1589 .fc_dst_len = 96,
1590 .fc_flags = RTF_UP | RTF_NONEXTHOP,
1591 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592
1593 /* prefix length - 96 bits "::d.d.d.d" */
Thomas Graf86872cb2006-08-22 00:01:08 -07001594 ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595}
Joerg Roedel0be669b2006-10-10 14:49:53 -07001596#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597
1598static void addrconf_add_lroute(struct net_device *dev)
1599{
1600 struct in6_addr addr;
1601
1602 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
1603 addrconf_prefix_route(&addr, 64, dev, 0, 0);
1604}
1605
1606static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
1607{
1608 struct inet6_dev *idev;
1609
1610 ASSERT_RTNL();
1611
1612 if ((idev = ipv6_find_idev(dev)) == NULL)
1613 return NULL;
1614
1615 /* Add default multicast route */
1616 addrconf_add_mroute(dev);
1617
1618 /* Add link local route */
1619 addrconf_add_lroute(dev);
1620 return idev;
1621}
1622
1623void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len)
1624{
1625 struct prefix_info *pinfo;
1626 __u32 valid_lft;
1627 __u32 prefered_lft;
1628 int addr_type;
1629 unsigned long rt_expires;
1630 struct inet6_dev *in6_dev;
1631
1632 pinfo = (struct prefix_info *) opt;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001633
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 if (len < sizeof(struct prefix_info)) {
1635 ADBG(("addrconf: prefix option too short\n"));
1636 return;
1637 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001638
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 /*
1640 * Validation checks ([ADDRCONF], page 19)
1641 */
1642
1643 addr_type = ipv6_addr_type(&pinfo->prefix);
1644
1645 if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
1646 return;
1647
1648 valid_lft = ntohl(pinfo->valid);
1649 prefered_lft = ntohl(pinfo->prefered);
1650
1651 if (prefered_lft > valid_lft) {
1652 if (net_ratelimit())
1653 printk(KERN_WARNING "addrconf: prefix option has invalid lifetime\n");
1654 return;
1655 }
1656
1657 in6_dev = in6_dev_get(dev);
1658
1659 if (in6_dev == NULL) {
1660 if (net_ratelimit())
1661 printk(KERN_DEBUG "addrconf: device %s not configured\n", dev->name);
1662 return;
1663 }
1664
1665 /*
1666 * Two things going on here:
1667 * 1) Add routes for on-link prefixes
1668 * 2) Configure prefixes with the auto flag set
1669 */
1670
1671 /* Avoid arithmetic overflow. Really, we could
1672 save rt_expires in seconds, likely valid_lft,
1673 but it would require division in fib gc, that it
1674 not good.
1675 */
1676 if (valid_lft >= 0x7FFFFFFF/HZ)
YOSHIFUJI Hideaki3dd4bc62005-12-19 14:02:45 -08001677 rt_expires = 0x7FFFFFFF - (0x7FFFFFFF % HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 else
YOSHIFUJI Hideaki3dd4bc62005-12-19 14:02:45 -08001679 rt_expires = valid_lft * HZ;
1680
1681 /*
1682 * We convert this (in jiffies) to clock_t later.
1683 * Avoid arithmetic overflow there as well.
1684 * Overflow can happen only if HZ < USER_HZ.
1685 */
1686 if (HZ < USER_HZ && rt_expires > 0x7FFFFFFF / USER_HZ)
1687 rt_expires = 0x7FFFFFFF / USER_HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688
1689 if (pinfo->onlink) {
1690 struct rt6_info *rt;
1691 rt = rt6_lookup(&pinfo->prefix, NULL, dev->ifindex, 1);
1692
1693 if (rt && ((rt->rt6i_flags & (RTF_GATEWAY | RTF_DEFAULT)) == 0)) {
1694 if (rt->rt6i_flags&RTF_EXPIRES) {
1695 if (valid_lft == 0) {
Thomas Grafe0a1ad732006-08-22 00:00:21 -07001696 ip6_del_rt(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 rt = NULL;
1698 } else {
YOSHIFUJI Hideaki3dd4bc62005-12-19 14:02:45 -08001699 rt->rt6i_expires = jiffies + rt_expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 }
1701 }
1702 } else if (valid_lft) {
1703 addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
YOSHIFUJI Hideaki3dd4bc62005-12-19 14:02:45 -08001704 dev, jiffies_to_clock_t(rt_expires), RTF_ADDRCONF|RTF_EXPIRES|RTF_PREFIX_RT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 }
1706 if (rt)
1707 dst_release(&rt->u.dst);
1708 }
1709
1710 /* Try to figure out our local address for this prefix */
1711
1712 if (pinfo->autoconf && in6_dev->cnf.autoconf) {
1713 struct inet6_ifaddr * ifp;
1714 struct in6_addr addr;
1715 int create = 0, update_lft = 0;
1716
1717 if (pinfo->prefix_len == 64) {
1718 memcpy(&addr, &pinfo->prefix, 8);
1719 if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
1720 ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
1721 in6_dev_put(in6_dev);
1722 return;
1723 }
1724 goto ok;
1725 }
1726 if (net_ratelimit())
1727 printk(KERN_DEBUG "IPv6 addrconf: prefix with wrong length %d\n",
1728 pinfo->prefix_len);
1729 in6_dev_put(in6_dev);
1730 return;
1731
1732ok:
1733
1734 ifp = ipv6_get_ifaddr(&addr, dev, 1);
1735
1736 if (ifp == NULL && valid_lft) {
1737 int max_addresses = in6_dev->cnf.max_addresses;
1738
1739 /* Do not allow to create too much of autoconfigured
1740 * addresses; this would be too easy way to crash kernel.
1741 */
1742 if (!max_addresses ||
1743 ipv6_count_addresses(in6_dev) < max_addresses)
1744 ifp = ipv6_add_addr(in6_dev, &addr, pinfo->prefix_len,
1745 addr_type&IPV6_ADDR_SCOPE_MASK, 0);
1746
1747 if (!ifp || IS_ERR(ifp)) {
1748 in6_dev_put(in6_dev);
1749 return;
1750 }
1751
1752 update_lft = create = 1;
1753 ifp->cstamp = jiffies;
1754 addrconf_dad_start(ifp, RTF_ADDRCONF|RTF_PREFIX_RT);
1755 }
1756
1757 if (ifp) {
1758 int flags;
1759 unsigned long now;
1760#ifdef CONFIG_IPV6_PRIVACY
1761 struct inet6_ifaddr *ift;
1762#endif
1763 u32 stored_lft;
1764
1765 /* update lifetime (RFC2462 5.5.3 e) */
1766 spin_lock(&ifp->lock);
1767 now = jiffies;
1768 if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
1769 stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
1770 else
1771 stored_lft = 0;
1772 if (!update_lft && stored_lft) {
1773 if (valid_lft > MIN_VALID_LIFETIME ||
1774 valid_lft > stored_lft)
1775 update_lft = 1;
1776 else if (stored_lft <= MIN_VALID_LIFETIME) {
1777 /* valid_lft <= stored_lft is always true */
1778 /* XXX: IPsec */
1779 update_lft = 0;
1780 } else {
1781 valid_lft = MIN_VALID_LIFETIME;
1782 if (valid_lft < prefered_lft)
1783 prefered_lft = valid_lft;
1784 update_lft = 1;
1785 }
1786 }
1787
1788 if (update_lft) {
1789 ifp->valid_lft = valid_lft;
1790 ifp->prefered_lft = prefered_lft;
1791 ifp->tstamp = now;
1792 flags = ifp->flags;
1793 ifp->flags &= ~IFA_F_DEPRECATED;
1794 spin_unlock(&ifp->lock);
1795
1796 if (!(flags&IFA_F_TENTATIVE))
1797 ipv6_ifa_notify(0, ifp);
1798 } else
1799 spin_unlock(&ifp->lock);
1800
1801#ifdef CONFIG_IPV6_PRIVACY
1802 read_lock_bh(&in6_dev->lock);
1803 /* update all temporary addresses in the list */
1804 for (ift=in6_dev->tempaddr_list; ift; ift=ift->tmp_next) {
1805 /*
1806 * When adjusting the lifetimes of an existing
1807 * temporary address, only lower the lifetimes.
1808 * Implementations must not increase the
1809 * lifetimes of an existing temporary address
1810 * when processing a Prefix Information Option.
1811 */
1812 spin_lock(&ift->lock);
1813 flags = ift->flags;
1814 if (ift->valid_lft > valid_lft &&
1815 ift->valid_lft - valid_lft > (jiffies - ift->tstamp) / HZ)
1816 ift->valid_lft = valid_lft + (jiffies - ift->tstamp) / HZ;
1817 if (ift->prefered_lft > prefered_lft &&
1818 ift->prefered_lft - prefered_lft > (jiffies - ift->tstamp) / HZ)
1819 ift->prefered_lft = prefered_lft + (jiffies - ift->tstamp) / HZ;
1820 spin_unlock(&ift->lock);
1821 if (!(flags&IFA_F_TENTATIVE))
1822 ipv6_ifa_notify(0, ift);
1823 }
1824
1825 if (create && in6_dev->cnf.use_tempaddr > 0) {
1826 /*
1827 * When a new public address is created as described in [ADDRCONF],
1828 * also create a new temporary address.
1829 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001830 read_unlock_bh(&in6_dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 ipv6_create_tempaddr(ifp, NULL);
1832 } else {
1833 read_unlock_bh(&in6_dev->lock);
1834 }
1835#endif
1836 in6_ifa_put(ifp);
1837 addrconf_verify(0);
1838 }
1839 }
1840 inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
1841 in6_dev_put(in6_dev);
1842}
1843
1844/*
1845 * Set destination address.
1846 * Special case for SIT interfaces where we create a new "virtual"
1847 * device.
1848 */
1849int addrconf_set_dstaddr(void __user *arg)
1850{
1851 struct in6_ifreq ireq;
1852 struct net_device *dev;
1853 int err = -EINVAL;
1854
1855 rtnl_lock();
1856
1857 err = -EFAULT;
1858 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
1859 goto err_exit;
1860
1861 dev = __dev_get_by_index(ireq.ifr6_ifindex);
1862
1863 err = -ENODEV;
1864 if (dev == NULL)
1865 goto err_exit;
1866
Joerg Roedel0be669b2006-10-10 14:49:53 -07001867#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 if (dev->type == ARPHRD_SIT) {
1869 struct ifreq ifr;
1870 mm_segment_t oldfs;
1871 struct ip_tunnel_parm p;
1872
1873 err = -EADDRNOTAVAIL;
1874 if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4))
1875 goto err_exit;
1876
1877 memset(&p, 0, sizeof(p));
1878 p.iph.daddr = ireq.ifr6_addr.s6_addr32[3];
1879 p.iph.saddr = 0;
1880 p.iph.version = 4;
1881 p.iph.ihl = 5;
1882 p.iph.protocol = IPPROTO_IPV6;
1883 p.iph.ttl = 64;
1884 ifr.ifr_ifru.ifru_data = (void __user *)&p;
1885
1886 oldfs = get_fs(); set_fs(KERNEL_DS);
1887 err = dev->do_ioctl(dev, &ifr, SIOCADDTUNNEL);
1888 set_fs(oldfs);
1889
1890 if (err == 0) {
1891 err = -ENOBUFS;
1892 if ((dev = __dev_get_by_name(p.name)) == NULL)
1893 goto err_exit;
1894 err = dev_open(dev);
1895 }
1896 }
Joerg Roedel0be669b2006-10-10 14:49:53 -07001897#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898
1899err_exit:
1900 rtnl_unlock();
1901 return err;
1902}
1903
1904/*
1905 * Manual configuration of address on an interface
1906 */
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09001907static int inet6_addr_add(int ifindex, struct in6_addr *pfx, int plen,
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07001908 __u8 ifa_flags, __u32 prefered_lft, __u32 valid_lft)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909{
1910 struct inet6_ifaddr *ifp;
1911 struct inet6_dev *idev;
1912 struct net_device *dev;
1913 int scope;
1914
1915 ASSERT_RTNL();
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001916
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09001917 /* check the lifetime */
1918 if (!valid_lft || prefered_lft > valid_lft)
1919 return -EINVAL;
1920
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 if ((dev = __dev_get_by_index(ifindex)) == NULL)
1922 return -ENODEV;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001923
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 if ((idev = addrconf_add_dev(dev)) == NULL)
1925 return -ENOBUFS;
1926
1927 scope = ipv6_addr_scope(pfx);
1928
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09001929 if (valid_lft == INFINITY_LIFE_TIME)
1930 ifa_flags |= IFA_F_PERMANENT;
1931 else if (valid_lft >= 0x7FFFFFFF/HZ)
1932 valid_lft = 0x7FFFFFFF/HZ;
1933
1934 if (prefered_lft == 0)
1935 ifa_flags |= IFA_F_DEPRECATED;
1936 else if ((prefered_lft >= 0x7FFFFFFF/HZ) &&
1937 (prefered_lft != INFINITY_LIFE_TIME))
1938 prefered_lft = 0x7FFFFFFF/HZ;
1939
1940 ifp = ipv6_add_addr(idev, pfx, plen, scope, ifa_flags);
1941
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 if (!IS_ERR(ifp)) {
Herbert Xu06aebfb2006-08-09 16:52:04 -07001943 spin_lock_bh(&ifp->lock);
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09001944 ifp->valid_lft = valid_lft;
1945 ifp->prefered_lft = prefered_lft;
1946 ifp->tstamp = jiffies;
Herbert Xu06aebfb2006-08-09 16:52:04 -07001947 spin_unlock_bh(&ifp->lock);
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09001948
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 addrconf_dad_start(ifp, 0);
1950 in6_ifa_put(ifp);
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09001951 addrconf_verify(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 return 0;
1953 }
1954
1955 return PTR_ERR(ifp);
1956}
1957
1958static int inet6_addr_del(int ifindex, struct in6_addr *pfx, int plen)
1959{
1960 struct inet6_ifaddr *ifp;
1961 struct inet6_dev *idev;
1962 struct net_device *dev;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001963
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 if ((dev = __dev_get_by_index(ifindex)) == NULL)
1965 return -ENODEV;
1966
1967 if ((idev = __in6_dev_get(dev)) == NULL)
1968 return -ENXIO;
1969
1970 read_lock_bh(&idev->lock);
1971 for (ifp = idev->addr_list; ifp; ifp=ifp->if_next) {
1972 if (ifp->prefix_len == plen &&
1973 ipv6_addr_equal(pfx, &ifp->addr)) {
1974 in6_ifa_hold(ifp);
1975 read_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001976
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 ipv6_del_addr(ifp);
1978
1979 /* If the last address is deleted administratively,
1980 disable IPv6 on this interface.
1981 */
1982 if (idev->addr_list == NULL)
1983 addrconf_ifdown(idev->dev, 1);
1984 return 0;
1985 }
1986 }
1987 read_unlock_bh(&idev->lock);
1988 return -EADDRNOTAVAIL;
1989}
1990
1991
1992int addrconf_add_ifaddr(void __user *arg)
1993{
1994 struct in6_ifreq ireq;
1995 int err;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001996
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 if (!capable(CAP_NET_ADMIN))
1998 return -EPERM;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001999
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2001 return -EFAULT;
2002
2003 rtnl_lock();
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002004 err = inet6_addr_add(ireq.ifr6_ifindex, &ireq.ifr6_addr, ireq.ifr6_prefixlen,
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07002005 IFA_F_PERMANENT, INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 rtnl_unlock();
2007 return err;
2008}
2009
2010int addrconf_del_ifaddr(void __user *arg)
2011{
2012 struct in6_ifreq ireq;
2013 int err;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002014
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 if (!capable(CAP_NET_ADMIN))
2016 return -EPERM;
2017
2018 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2019 return -EFAULT;
2020
2021 rtnl_lock();
2022 err = inet6_addr_del(ireq.ifr6_ifindex, &ireq.ifr6_addr, ireq.ifr6_prefixlen);
2023 rtnl_unlock();
2024 return err;
2025}
2026
Joerg Roedel0be669b2006-10-10 14:49:53 -07002027#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028static void sit_add_v4_addrs(struct inet6_dev *idev)
2029{
2030 struct inet6_ifaddr * ifp;
2031 struct in6_addr addr;
2032 struct net_device *dev;
2033 int scope;
2034
2035 ASSERT_RTNL();
2036
2037 memset(&addr, 0, sizeof(struct in6_addr));
2038 memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
2039
2040 if (idev->dev->flags&IFF_POINTOPOINT) {
2041 addr.s6_addr32[0] = htonl(0xfe800000);
2042 scope = IFA_LINK;
2043 } else {
2044 scope = IPV6_ADDR_COMPATv4;
2045 }
2046
2047 if (addr.s6_addr32[3]) {
2048 ifp = ipv6_add_addr(idev, &addr, 128, scope, IFA_F_PERMANENT);
2049 if (!IS_ERR(ifp)) {
2050 spin_lock_bh(&ifp->lock);
2051 ifp->flags &= ~IFA_F_TENTATIVE;
2052 spin_unlock_bh(&ifp->lock);
2053 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2054 in6_ifa_put(ifp);
2055 }
2056 return;
2057 }
2058
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002059 for (dev = dev_base; dev != NULL; dev = dev->next) {
Herbert Xue5ed6392005-10-03 14:35:55 -07002060 struct in_device * in_dev = __in_dev_get_rtnl(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 if (in_dev && (dev->flags & IFF_UP)) {
2062 struct in_ifaddr * ifa;
2063
2064 int flag = scope;
2065
2066 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
2067 int plen;
2068
2069 addr.s6_addr32[3] = ifa->ifa_local;
2070
2071 if (ifa->ifa_scope == RT_SCOPE_LINK)
2072 continue;
2073 if (ifa->ifa_scope >= RT_SCOPE_HOST) {
2074 if (idev->dev->flags&IFF_POINTOPOINT)
2075 continue;
2076 flag |= IFA_HOST;
2077 }
2078 if (idev->dev->flags&IFF_POINTOPOINT)
2079 plen = 64;
2080 else
2081 plen = 96;
2082
2083 ifp = ipv6_add_addr(idev, &addr, plen, flag,
2084 IFA_F_PERMANENT);
2085 if (!IS_ERR(ifp)) {
2086 spin_lock_bh(&ifp->lock);
2087 ifp->flags &= ~IFA_F_TENTATIVE;
2088 spin_unlock_bh(&ifp->lock);
2089 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2090 in6_ifa_put(ifp);
2091 }
2092 }
2093 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002094 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095}
Joerg Roedel0be669b2006-10-10 14:49:53 -07002096#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097
2098static void init_loopback(struct net_device *dev)
2099{
2100 struct inet6_dev *idev;
2101 struct inet6_ifaddr * ifp;
2102
2103 /* ::1 */
2104
2105 ASSERT_RTNL();
2106
2107 if ((idev = ipv6_find_idev(dev)) == NULL) {
2108 printk(KERN_DEBUG "init loopback: add_dev failed\n");
2109 return;
2110 }
2111
2112 ifp = ipv6_add_addr(idev, &in6addr_loopback, 128, IFA_HOST, IFA_F_PERMANENT);
2113 if (!IS_ERR(ifp)) {
2114 spin_lock_bh(&ifp->lock);
2115 ifp->flags &= ~IFA_F_TENTATIVE;
2116 spin_unlock_bh(&ifp->lock);
2117 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2118 in6_ifa_put(ifp);
2119 }
2120}
2121
2122static void addrconf_add_linklocal(struct inet6_dev *idev, struct in6_addr *addr)
2123{
2124 struct inet6_ifaddr * ifp;
2125
2126 ifp = ipv6_add_addr(idev, addr, 64, IFA_LINK, IFA_F_PERMANENT);
2127 if (!IS_ERR(ifp)) {
2128 addrconf_dad_start(ifp, 0);
2129 in6_ifa_put(ifp);
2130 }
2131}
2132
2133static void addrconf_dev_config(struct net_device *dev)
2134{
2135 struct in6_addr addr;
2136 struct inet6_dev * idev;
2137
2138 ASSERT_RTNL();
2139
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002140 if ((dev->type != ARPHRD_ETHER) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 (dev->type != ARPHRD_FDDI) &&
2142 (dev->type != ARPHRD_IEEE802_TR) &&
2143 (dev->type != ARPHRD_ARCNET) &&
2144 (dev->type != ARPHRD_INFINIBAND)) {
2145 /* Alas, we support only Ethernet autoconfiguration. */
2146 return;
2147 }
2148
2149 idev = addrconf_add_dev(dev);
2150 if (idev == NULL)
2151 return;
2152
2153 memset(&addr, 0, sizeof(struct in6_addr));
2154 addr.s6_addr32[0] = htonl(0xFE800000);
2155
2156 if (ipv6_generate_eui64(addr.s6_addr + 8, dev) == 0)
2157 addrconf_add_linklocal(idev, &addr);
2158}
2159
Joerg Roedel0be669b2006-10-10 14:49:53 -07002160#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161static void addrconf_sit_config(struct net_device *dev)
2162{
2163 struct inet6_dev *idev;
2164
2165 ASSERT_RTNL();
2166
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002167 /*
2168 * Configure the tunnel with one of our IPv4
2169 * addresses... we should configure all of
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 * our v4 addrs in the tunnel
2171 */
2172
2173 if ((idev = ipv6_find_idev(dev)) == NULL) {
2174 printk(KERN_DEBUG "init sit: add_dev failed\n");
2175 return;
2176 }
2177
2178 sit_add_v4_addrs(idev);
2179
2180 if (dev->flags&IFF_POINTOPOINT) {
2181 addrconf_add_mroute(dev);
2182 addrconf_add_lroute(dev);
2183 } else
2184 sit_route_add(dev);
2185}
Joerg Roedel0be669b2006-10-10 14:49:53 -07002186#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187
2188static inline int
2189ipv6_inherit_linklocal(struct inet6_dev *idev, struct net_device *link_dev)
2190{
2191 struct in6_addr lladdr;
2192
2193 if (!ipv6_get_lladdr(link_dev, &lladdr)) {
2194 addrconf_add_linklocal(idev, &lladdr);
2195 return 0;
2196 }
2197 return -1;
2198}
2199
2200static void ip6_tnl_add_linklocal(struct inet6_dev *idev)
2201{
2202 struct net_device *link_dev;
2203
2204 /* first try to inherit the link-local address from the link device */
2205 if (idev->dev->iflink &&
2206 (link_dev = __dev_get_by_index(idev->dev->iflink))) {
2207 if (!ipv6_inherit_linklocal(idev, link_dev))
2208 return;
2209 }
2210 /* then try to inherit it from any device */
2211 for (link_dev = dev_base; link_dev; link_dev = link_dev->next) {
2212 if (!ipv6_inherit_linklocal(idev, link_dev))
2213 return;
2214 }
2215 printk(KERN_DEBUG "init ip6-ip6: add_linklocal failed\n");
2216}
2217
2218/*
2219 * Autoconfigure tunnel with a link-local address so routing protocols,
2220 * DHCPv6, MLD etc. can be run over the virtual link
2221 */
2222
2223static void addrconf_ip6_tnl_config(struct net_device *dev)
2224{
2225 struct inet6_dev *idev;
2226
2227 ASSERT_RTNL();
2228
2229 if ((idev = addrconf_add_dev(dev)) == NULL) {
2230 printk(KERN_DEBUG "init ip6-ip6: add_dev failed\n");
2231 return;
2232 }
2233 ip6_tnl_add_linklocal(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234}
2235
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002236static int addrconf_notify(struct notifier_block *this, unsigned long event,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 void * data)
2238{
2239 struct net_device *dev = (struct net_device *) data;
2240 struct inet6_dev *idev = __in6_dev_get(dev);
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09002241 int run_pending = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242
2243 switch(event) {
2244 case NETDEV_UP:
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002245 case NETDEV_CHANGE:
2246 if (event == NETDEV_UP) {
2247 if (!netif_carrier_ok(dev)) {
2248 /* device is not ready yet. */
2249 printk(KERN_INFO
2250 "ADDRCONF(NETDEV_UP): %s: "
2251 "link is not ready\n",
2252 dev->name);
2253 break;
2254 }
Kristian Slavov99081042006-02-08 16:10:53 -08002255
2256 if (idev)
2257 idev->if_flags |= IF_READY;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002258 } else {
2259 if (!netif_carrier_ok(dev)) {
2260 /* device is still not ready. */
2261 break;
2262 }
2263
2264 if (idev) {
2265 if (idev->if_flags & IF_READY) {
2266 /* device is already configured. */
2267 break;
2268 }
2269 idev->if_flags |= IF_READY;
2270 }
2271
2272 printk(KERN_INFO
2273 "ADDRCONF(NETDEV_CHANGE): %s: "
2274 "link becomes ready\n",
2275 dev->name);
2276
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09002277 run_pending = 1;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002278 }
2279
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 switch(dev->type) {
Joerg Roedel0be669b2006-10-10 14:49:53 -07002281#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 case ARPHRD_SIT:
2283 addrconf_sit_config(dev);
2284 break;
Joerg Roedel0be669b2006-10-10 14:49:53 -07002285#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 case ARPHRD_TUNNEL6:
2287 addrconf_ip6_tnl_config(dev);
2288 break;
2289 case ARPHRD_LOOPBACK:
2290 init_loopback(dev);
2291 break;
2292
2293 default:
2294 addrconf_dev_config(dev);
2295 break;
2296 };
2297 if (idev) {
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09002298 if (run_pending)
2299 addrconf_dad_run(idev);
2300
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 /* If the MTU changed during the interface down, when the
2302 interface up, the changed MTU must be reflected in the
2303 idev as well as routers.
2304 */
2305 if (idev->cnf.mtu6 != dev->mtu && dev->mtu >= IPV6_MIN_MTU) {
2306 rt6_mtu_change(dev, dev->mtu);
2307 idev->cnf.mtu6 = dev->mtu;
2308 }
2309 idev->tstamp = jiffies;
2310 inet6_ifinfo_notify(RTM_NEWLINK, idev);
2311 /* If the changed mtu during down is lower than IPV6_MIN_MTU
2312 stop IPv6 on this interface.
2313 */
2314 if (dev->mtu < IPV6_MIN_MTU)
2315 addrconf_ifdown(dev, event != NETDEV_DOWN);
2316 }
2317 break;
2318
2319 case NETDEV_CHANGEMTU:
2320 if ( idev && dev->mtu >= IPV6_MIN_MTU) {
2321 rt6_mtu_change(dev, dev->mtu);
2322 idev->cnf.mtu6 = dev->mtu;
2323 break;
2324 }
2325
2326 /* MTU falled under IPV6_MIN_MTU. Stop IPv6 on this interface. */
2327
2328 case NETDEV_DOWN:
2329 case NETDEV_UNREGISTER:
2330 /*
2331 * Remove all addresses from this interface.
2332 */
2333 addrconf_ifdown(dev, event != NETDEV_DOWN);
2334 break;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002335
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 case NETDEV_CHANGENAME:
2337#ifdef CONFIG_SYSCTL
2338 if (idev) {
2339 addrconf_sysctl_unregister(&idev->cnf);
2340 neigh_sysctl_unregister(idev->nd_parms);
2341 neigh_sysctl_register(dev, idev->nd_parms,
2342 NET_IPV6, NET_IPV6_NEIGH, "ipv6",
2343 &ndisc_ifinfo_sysctl_change,
2344 NULL);
2345 addrconf_sysctl_register(idev, &idev->cnf);
2346 }
2347#endif
2348 break;
2349 };
2350
2351 return NOTIFY_OK;
2352}
2353
2354/*
2355 * addrconf module should be notified of a device going up
2356 */
2357static struct notifier_block ipv6_dev_notf = {
2358 .notifier_call = addrconf_notify,
2359 .priority = 0
2360};
2361
2362static int addrconf_ifdown(struct net_device *dev, int how)
2363{
2364 struct inet6_dev *idev;
2365 struct inet6_ifaddr *ifa, **bifa;
2366 int i;
2367
2368 ASSERT_RTNL();
2369
2370 if (dev == &loopback_dev && how == 1)
2371 how = 0;
2372
2373 rt6_ifdown(dev);
2374 neigh_ifdown(&nd_tbl, dev);
2375
2376 idev = __in6_dev_get(dev);
2377 if (idev == NULL)
2378 return -ENODEV;
2379
2380 /* Step 1: remove reference to ipv6 device from parent device.
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002381 Do not dev_put!
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 */
2383 if (how == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 idev->dead = 1;
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07002385
2386 /* protected by rtnl_lock */
2387 rcu_assign_pointer(dev->ip6_ptr, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388
2389 /* Step 1.5: remove snmp6 entry */
2390 snmp6_unregister_dev(idev);
2391
2392 }
2393
2394 /* Step 2: clear hash table */
2395 for (i=0; i<IN6_ADDR_HSIZE; i++) {
2396 bifa = &inet6_addr_lst[i];
2397
2398 write_lock_bh(&addrconf_hash_lock);
2399 while ((ifa = *bifa) != NULL) {
2400 if (ifa->idev == idev) {
2401 *bifa = ifa->lst_next;
2402 ifa->lst_next = NULL;
2403 addrconf_del_timer(ifa);
2404 in6_ifa_put(ifa);
2405 continue;
2406 }
2407 bifa = &ifa->lst_next;
2408 }
2409 write_unlock_bh(&addrconf_hash_lock);
2410 }
2411
2412 write_lock_bh(&idev->lock);
2413
2414 /* Step 3: clear flags for stateless addrconf */
2415 if (how != 1)
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002416 idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417
2418 /* Step 4: clear address list */
2419#ifdef CONFIG_IPV6_PRIVACY
2420 if (how == 1 && del_timer(&idev->regen_timer))
2421 in6_dev_put(idev);
2422
2423 /* clear tempaddr list */
2424 while ((ifa = idev->tempaddr_list) != NULL) {
2425 idev->tempaddr_list = ifa->tmp_next;
2426 ifa->tmp_next = NULL;
2427 ifa->dead = 1;
2428 write_unlock_bh(&idev->lock);
2429 spin_lock_bh(&ifa->lock);
2430
2431 if (ifa->ifpub) {
2432 in6_ifa_put(ifa->ifpub);
2433 ifa->ifpub = NULL;
2434 }
2435 spin_unlock_bh(&ifa->lock);
2436 in6_ifa_put(ifa);
2437 write_lock_bh(&idev->lock);
2438 }
2439#endif
2440 while ((ifa = idev->addr_list) != NULL) {
2441 idev->addr_list = ifa->if_next;
2442 ifa->if_next = NULL;
2443 ifa->dead = 1;
2444 addrconf_del_timer(ifa);
2445 write_unlock_bh(&idev->lock);
2446
2447 __ipv6_ifa_notify(RTM_DELADDR, ifa);
2448 in6_ifa_put(ifa);
2449
2450 write_lock_bh(&idev->lock);
2451 }
2452 write_unlock_bh(&idev->lock);
2453
2454 /* Step 5: Discard multicast list */
2455
2456 if (how == 1)
2457 ipv6_mc_destroy_dev(idev);
2458 else
2459 ipv6_mc_down(idev);
2460
2461 /* Step 5: netlink notification of this interface */
2462 idev->tstamp = jiffies;
Yan Zheng979ad662005-10-14 18:31:15 +08002463 inet6_ifinfo_notify(RTM_DELLINK, idev);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002464
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 /* Shot the device (if unregistered) */
2466
2467 if (how == 1) {
2468#ifdef CONFIG_SYSCTL
2469 addrconf_sysctl_unregister(&idev->cnf);
2470 neigh_sysctl_unregister(idev->nd_parms);
2471#endif
2472 neigh_parms_release(&nd_tbl, idev->nd_parms);
2473 neigh_ifdown(&nd_tbl, dev);
2474 in6_dev_put(idev);
2475 }
2476 return 0;
2477}
2478
2479static void addrconf_rs_timer(unsigned long data)
2480{
2481 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
2482
2483 if (ifp->idev->cnf.forwarding)
2484 goto out;
2485
2486 if (ifp->idev->if_flags & IF_RA_RCVD) {
2487 /*
2488 * Announcement received after solicitation
2489 * was sent
2490 */
2491 goto out;
2492 }
2493
2494 spin_lock(&ifp->lock);
2495 if (ifp->probes++ < ifp->idev->cnf.rtr_solicits) {
2496 struct in6_addr all_routers;
2497
2498 /* The wait after the last probe can be shorter */
2499 addrconf_mod_timer(ifp, AC_RS,
2500 (ifp->probes == ifp->idev->cnf.rtr_solicits) ?
2501 ifp->idev->cnf.rtr_solicit_delay :
2502 ifp->idev->cnf.rtr_solicit_interval);
2503 spin_unlock(&ifp->lock);
2504
2505 ipv6_addr_all_routers(&all_routers);
2506
2507 ndisc_send_rs(ifp->idev->dev, &ifp->addr, &all_routers);
2508 } else {
2509 spin_unlock(&ifp->lock);
2510 /*
2511 * Note: we do not support deprecated "all on-link"
2512 * assumption any longer.
2513 */
2514 printk(KERN_DEBUG "%s: no IPv6 routers present\n",
2515 ifp->idev->dev->name);
2516 }
2517
2518out:
2519 in6_ifa_put(ifp);
2520}
2521
2522/*
2523 * Duplicate Address Detection
2524 */
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002525static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
2526{
2527 unsigned long rand_num;
2528 struct inet6_dev *idev = ifp->idev;
2529
2530 rand_num = net_random() % (idev->cnf.rtr_solicit_delay ? : 1);
2531 ifp->probes = idev->cnf.dad_transmits;
2532 addrconf_mod_timer(ifp, AC_DAD, rand_num);
2533}
2534
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -07002535static void addrconf_dad_start(struct inet6_ifaddr *ifp, u32 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536{
2537 struct inet6_dev *idev = ifp->idev;
2538 struct net_device *dev = idev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539
2540 addrconf_join_solict(dev, &ifp->addr);
2541
2542 if (ifp->prefix_len != 128 && (ifp->flags&IFA_F_PERMANENT))
2543 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev, 0,
2544 flags);
2545
2546 net_srandom(ifp->addr.s6_addr32[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547
2548 read_lock_bh(&idev->lock);
2549 if (ifp->dead)
2550 goto out;
2551 spin_lock_bh(&ifp->lock);
2552
2553 if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07002554 !(ifp->flags&IFA_F_TENTATIVE) ||
2555 ifp->flags & IFA_F_NODAD) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 ifp->flags &= ~IFA_F_TENTATIVE;
2557 spin_unlock_bh(&ifp->lock);
2558 read_unlock_bh(&idev->lock);
2559
2560 addrconf_dad_completed(ifp);
2561 return;
2562 }
2563
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08002564 if (!(idev->if_flags & IF_READY)) {
YOSHIFUJI Hideaki3dd3bf82005-12-23 11:23:21 -08002565 spin_unlock_bh(&ifp->lock);
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08002566 read_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002567 /*
2568 * If the defice is not ready:
2569 * - keep it tentative if it is a permanent address.
2570 * - otherwise, kill it.
2571 */
2572 in6_ifa_hold(ifp);
2573 addrconf_dad_stop(ifp);
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08002574 return;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002575 }
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08002576 addrconf_dad_kick(ifp);
2577 spin_unlock_bh(&ifp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578out:
2579 read_unlock_bh(&idev->lock);
2580}
2581
2582static void addrconf_dad_timer(unsigned long data)
2583{
2584 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
2585 struct inet6_dev *idev = ifp->idev;
2586 struct in6_addr unspec;
2587 struct in6_addr mcaddr;
2588
2589 read_lock_bh(&idev->lock);
2590 if (idev->dead) {
2591 read_unlock_bh(&idev->lock);
2592 goto out;
2593 }
2594 spin_lock_bh(&ifp->lock);
2595 if (ifp->probes == 0) {
2596 /*
2597 * DAD was successful
2598 */
2599
2600 ifp->flags &= ~IFA_F_TENTATIVE;
2601 spin_unlock_bh(&ifp->lock);
2602 read_unlock_bh(&idev->lock);
2603
2604 addrconf_dad_completed(ifp);
2605
2606 goto out;
2607 }
2608
2609 ifp->probes--;
2610 addrconf_mod_timer(ifp, AC_DAD, ifp->idev->nd_parms->retrans_time);
2611 spin_unlock_bh(&ifp->lock);
2612 read_unlock_bh(&idev->lock);
2613
2614 /* send a neighbour solicitation for our addr */
2615 memset(&unspec, 0, sizeof(unspec));
2616 addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
2617 ndisc_send_ns(ifp->idev->dev, NULL, &ifp->addr, &mcaddr, &unspec);
2618out:
2619 in6_ifa_put(ifp);
2620}
2621
2622static void addrconf_dad_completed(struct inet6_ifaddr *ifp)
2623{
2624 struct net_device * dev = ifp->idev->dev;
2625
2626 /*
2627 * Configure the address for reception. Now it is valid.
2628 */
2629
2630 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2631
2632 /* If added prefix is link local and forwarding is off,
2633 start sending router solicitations.
2634 */
2635
2636 if (ifp->idev->cnf.forwarding == 0 &&
2637 ifp->idev->cnf.rtr_solicits > 0 &&
2638 (dev->flags&IFF_LOOPBACK) == 0 &&
2639 (ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL)) {
2640 struct in6_addr all_routers;
2641
2642 ipv6_addr_all_routers(&all_routers);
2643
2644 /*
2645 * If a host as already performed a random delay
2646 * [...] as part of DAD [...] there is no need
2647 * to delay again before sending the first RS
2648 */
2649 ndisc_send_rs(ifp->idev->dev, &ifp->addr, &all_routers);
2650
2651 spin_lock_bh(&ifp->lock);
2652 ifp->probes = 1;
2653 ifp->idev->if_flags |= IF_RS_SENT;
2654 addrconf_mod_timer(ifp, AC_RS, ifp->idev->cnf.rtr_solicit_interval);
2655 spin_unlock_bh(&ifp->lock);
2656 }
2657}
2658
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09002659static void addrconf_dad_run(struct inet6_dev *idev) {
2660 struct inet6_ifaddr *ifp;
2661
2662 read_lock_bh(&idev->lock);
2663 for (ifp = idev->addr_list; ifp; ifp = ifp->if_next) {
2664 spin_lock_bh(&ifp->lock);
2665 if (!(ifp->flags & IFA_F_TENTATIVE)) {
2666 spin_unlock_bh(&ifp->lock);
2667 continue;
2668 }
2669 spin_unlock_bh(&ifp->lock);
2670 addrconf_dad_kick(ifp);
2671 }
2672 read_unlock_bh(&idev->lock);
2673}
2674
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675#ifdef CONFIG_PROC_FS
2676struct if6_iter_state {
2677 int bucket;
2678};
2679
2680static struct inet6_ifaddr *if6_get_first(struct seq_file *seq)
2681{
2682 struct inet6_ifaddr *ifa = NULL;
2683 struct if6_iter_state *state = seq->private;
2684
2685 for (state->bucket = 0; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
2686 ifa = inet6_addr_lst[state->bucket];
2687 if (ifa)
2688 break;
2689 }
2690 return ifa;
2691}
2692
2693static struct inet6_ifaddr *if6_get_next(struct seq_file *seq, struct inet6_ifaddr *ifa)
2694{
2695 struct if6_iter_state *state = seq->private;
2696
2697 ifa = ifa->lst_next;
2698try_again:
2699 if (!ifa && ++state->bucket < IN6_ADDR_HSIZE) {
2700 ifa = inet6_addr_lst[state->bucket];
2701 goto try_again;
2702 }
2703 return ifa;
2704}
2705
2706static struct inet6_ifaddr *if6_get_idx(struct seq_file *seq, loff_t pos)
2707{
2708 struct inet6_ifaddr *ifa = if6_get_first(seq);
2709
2710 if (ifa)
2711 while(pos && (ifa = if6_get_next(seq, ifa)) != NULL)
2712 --pos;
2713 return pos ? NULL : ifa;
2714}
2715
2716static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
2717{
2718 read_lock_bh(&addrconf_hash_lock);
2719 return if6_get_idx(seq, *pos);
2720}
2721
2722static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2723{
2724 struct inet6_ifaddr *ifa;
2725
2726 ifa = if6_get_next(seq, v);
2727 ++*pos;
2728 return ifa;
2729}
2730
2731static void if6_seq_stop(struct seq_file *seq, void *v)
2732{
2733 read_unlock_bh(&addrconf_hash_lock);
2734}
2735
2736static int if6_seq_show(struct seq_file *seq, void *v)
2737{
2738 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
2739 seq_printf(seq,
YOSHIFUJI Hideaki9343e792006-01-17 02:10:53 -08002740 NIP6_SEQFMT " %02x %02x %02x %02x %8s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 NIP6(ifp->addr),
2742 ifp->idev->dev->ifindex,
2743 ifp->prefix_len,
2744 ifp->scope,
2745 ifp->flags,
2746 ifp->idev->dev->name);
2747 return 0;
2748}
2749
2750static struct seq_operations if6_seq_ops = {
2751 .start = if6_seq_start,
2752 .next = if6_seq_next,
2753 .show = if6_seq_show,
2754 .stop = if6_seq_stop,
2755};
2756
2757static int if6_seq_open(struct inode *inode, struct file *file)
2758{
2759 struct seq_file *seq;
2760 int rc = -ENOMEM;
Ingo Oeser322f74a2006-03-20 23:01:47 -08002761 struct if6_iter_state *s = kzalloc(sizeof(*s), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762
2763 if (!s)
2764 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765
2766 rc = seq_open(file, &if6_seq_ops);
2767 if (rc)
2768 goto out_kfree;
2769
2770 seq = file->private_data;
2771 seq->private = s;
2772out:
2773 return rc;
2774out_kfree:
2775 kfree(s);
2776 goto out;
2777}
2778
2779static struct file_operations if6_fops = {
2780 .owner = THIS_MODULE,
2781 .open = if6_seq_open,
2782 .read = seq_read,
2783 .llseek = seq_lseek,
2784 .release = seq_release_private,
2785};
2786
2787int __init if6_proc_init(void)
2788{
2789 if (!proc_net_fops_create("if_inet6", S_IRUGO, &if6_fops))
2790 return -ENOMEM;
2791 return 0;
2792}
2793
2794void if6_proc_exit(void)
2795{
2796 proc_net_remove("if_inet6");
2797}
2798#endif /* CONFIG_PROC_FS */
2799
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07002800#ifdef CONFIG_IPV6_MIP6
2801/* Check if address is a home address configured on any interface. */
2802int ipv6_chk_home_addr(struct in6_addr *addr)
2803{
2804 int ret = 0;
2805 struct inet6_ifaddr * ifp;
2806 u8 hash = ipv6_addr_hash(addr);
2807 read_lock_bh(&addrconf_hash_lock);
2808 for (ifp = inet6_addr_lst[hash]; ifp; ifp = ifp->lst_next) {
2809 if (ipv6_addr_cmp(&ifp->addr, addr) == 0 &&
2810 (ifp->flags & IFA_F_HOMEADDRESS)) {
2811 ret = 1;
2812 break;
2813 }
2814 }
2815 read_unlock_bh(&addrconf_hash_lock);
2816 return ret;
2817}
2818#endif
2819
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820/*
2821 * Periodic address status verification
2822 */
2823
2824static void addrconf_verify(unsigned long foo)
2825{
2826 struct inet6_ifaddr *ifp;
2827 unsigned long now, next;
2828 int i;
2829
2830 spin_lock_bh(&addrconf_verify_lock);
2831 now = jiffies;
2832 next = now + ADDR_CHECK_FREQUENCY;
2833
2834 del_timer(&addr_chk_timer);
2835
2836 for (i=0; i < IN6_ADDR_HSIZE; i++) {
2837
2838restart:
Yan Zheng5d5780d2005-11-20 13:42:20 -08002839 read_lock(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 for (ifp=inet6_addr_lst[i]; ifp; ifp=ifp->lst_next) {
2841 unsigned long age;
2842#ifdef CONFIG_IPV6_PRIVACY
2843 unsigned long regen_advance;
2844#endif
2845
2846 if (ifp->flags & IFA_F_PERMANENT)
2847 continue;
2848
2849 spin_lock(&ifp->lock);
2850 age = (now - ifp->tstamp) / HZ;
2851
2852#ifdef CONFIG_IPV6_PRIVACY
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002853 regen_advance = ifp->idev->cnf.regen_max_retry *
2854 ifp->idev->cnf.dad_transmits *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 ifp->idev->nd_parms->retrans_time / HZ;
2856#endif
2857
YOSHIFUJI Hideaki8f27ebb2006-07-28 18:12:11 +09002858 if (ifp->valid_lft != INFINITY_LIFE_TIME &&
2859 age >= ifp->valid_lft) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 spin_unlock(&ifp->lock);
2861 in6_ifa_hold(ifp);
Yan Zheng5d5780d2005-11-20 13:42:20 -08002862 read_unlock(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 ipv6_del_addr(ifp);
2864 goto restart;
YOSHIFUJI Hideaki8f27ebb2006-07-28 18:12:11 +09002865 } else if (ifp->prefered_lft == INFINITY_LIFE_TIME) {
2866 spin_unlock(&ifp->lock);
2867 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 } else if (age >= ifp->prefered_lft) {
2869 /* jiffies - ifp->tsamp > age >= ifp->prefered_lft */
2870 int deprecate = 0;
2871
2872 if (!(ifp->flags&IFA_F_DEPRECATED)) {
2873 deprecate = 1;
2874 ifp->flags |= IFA_F_DEPRECATED;
2875 }
2876
2877 if (time_before(ifp->tstamp + ifp->valid_lft * HZ, next))
2878 next = ifp->tstamp + ifp->valid_lft * HZ;
2879
2880 spin_unlock(&ifp->lock);
2881
2882 if (deprecate) {
2883 in6_ifa_hold(ifp);
Yan Zheng5d5780d2005-11-20 13:42:20 -08002884 read_unlock(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885
2886 ipv6_ifa_notify(0, ifp);
2887 in6_ifa_put(ifp);
2888 goto restart;
2889 }
2890#ifdef CONFIG_IPV6_PRIVACY
2891 } else if ((ifp->flags&IFA_F_TEMPORARY) &&
2892 !(ifp->flags&IFA_F_TENTATIVE)) {
2893 if (age >= ifp->prefered_lft - regen_advance) {
2894 struct inet6_ifaddr *ifpub = ifp->ifpub;
2895 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
2896 next = ifp->tstamp + ifp->prefered_lft * HZ;
2897 if (!ifp->regen_count && ifpub) {
2898 ifp->regen_count++;
2899 in6_ifa_hold(ifp);
2900 in6_ifa_hold(ifpub);
2901 spin_unlock(&ifp->lock);
Yan Zheng5d5780d2005-11-20 13:42:20 -08002902 read_unlock(&addrconf_hash_lock);
Hiroyuki YAMAMORI291d8092005-12-23 11:24:05 -08002903 spin_lock(&ifpub->lock);
2904 ifpub->regen_count = 0;
2905 spin_unlock(&ifpub->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 ipv6_create_tempaddr(ifpub, ifp);
2907 in6_ifa_put(ifpub);
2908 in6_ifa_put(ifp);
2909 goto restart;
2910 }
2911 } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
2912 next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
2913 spin_unlock(&ifp->lock);
2914#endif
2915 } else {
2916 /* ifp->prefered_lft <= ifp->valid_lft */
2917 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
2918 next = ifp->tstamp + ifp->prefered_lft * HZ;
2919 spin_unlock(&ifp->lock);
2920 }
2921 }
Yan Zheng5d5780d2005-11-20 13:42:20 -08002922 read_unlock(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 }
2924
2925 addr_chk_timer.expires = time_before(next, jiffies + HZ) ? jiffies + HZ : next;
2926 add_timer(&addr_chk_timer);
2927 spin_unlock_bh(&addrconf_verify_lock);
2928}
2929
Thomas Graf461d8832006-09-18 00:09:49 -07002930static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local)
2931{
2932 struct in6_addr *pfx = NULL;
2933
2934 if (addr)
2935 pfx = nla_data(addr);
2936
2937 if (local) {
2938 if (pfx && nla_memcmp(local, pfx, sizeof(*pfx)))
2939 pfx = NULL;
2940 else
2941 pfx = nla_data(local);
2942 }
2943
2944 return pfx;
2945}
2946
2947static struct nla_policy ifa_ipv6_policy[IFA_MAX+1] __read_mostly = {
2948 [IFA_ADDRESS] = { .len = sizeof(struct in6_addr) },
2949 [IFA_LOCAL] = { .len = sizeof(struct in6_addr) },
2950 [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) },
2951};
2952
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953static int
2954inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
2955{
Thomas Grafb933f712006-09-18 00:10:19 -07002956 struct ifaddrmsg *ifm;
2957 struct nlattr *tb[IFA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 struct in6_addr *pfx;
Thomas Grafb933f712006-09-18 00:10:19 -07002959 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960
Thomas Grafb933f712006-09-18 00:10:19 -07002961 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
2962 if (err < 0)
2963 return err;
2964
2965 ifm = nlmsg_data(nlh);
2966 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 if (pfx == NULL)
2968 return -EINVAL;
2969
2970 return inet6_addr_del(ifm->ifa_index, pfx, ifm->ifa_prefixlen);
2971}
2972
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07002973static int inet6_addr_modify(struct inet6_ifaddr *ifp, u8 ifa_flags,
2974 u32 prefered_lft, u32 valid_lft)
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09002975{
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09002976 if (!valid_lft || (prefered_lft > valid_lft))
2977 return -EINVAL;
2978
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09002979 if (valid_lft == INFINITY_LIFE_TIME)
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07002980 ifa_flags |= IFA_F_PERMANENT;
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09002981 else if (valid_lft >= 0x7FFFFFFF/HZ)
2982 valid_lft = 0x7FFFFFFF/HZ;
2983
2984 if (prefered_lft == 0)
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07002985 ifa_flags |= IFA_F_DEPRECATED;
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09002986 else if ((prefered_lft >= 0x7FFFFFFF/HZ) &&
2987 (prefered_lft != INFINITY_LIFE_TIME))
2988 prefered_lft = 0x7FFFFFFF/HZ;
2989
2990 spin_lock_bh(&ifp->lock);
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07002991 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 +09002992 ifp->tstamp = jiffies;
2993 ifp->valid_lft = valid_lft;
2994 ifp->prefered_lft = prefered_lft;
2995
2996 spin_unlock_bh(&ifp->lock);
2997 if (!(ifp->flags&IFA_F_TENTATIVE))
2998 ipv6_ifa_notify(0, ifp);
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09002999
3000 addrconf_verify(0);
3001
3002 return 0;
3003}
3004
3005static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
3007{
Thomas Graf461d8832006-09-18 00:09:49 -07003008 struct ifaddrmsg *ifm;
3009 struct nlattr *tb[IFA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 struct in6_addr *pfx;
Thomas Graf7198f8c2006-09-18 00:13:46 -07003011 struct inet6_ifaddr *ifa;
3012 struct net_device *dev;
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003013 u32 valid_lft = INFINITY_LIFE_TIME, preferred_lft = INFINITY_LIFE_TIME;
3014 u8 ifa_flags;
Thomas Graf461d8832006-09-18 00:09:49 -07003015 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016
Thomas Graf461d8832006-09-18 00:09:49 -07003017 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
3018 if (err < 0)
3019 return err;
3020
3021 ifm = nlmsg_data(nlh);
3022 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023 if (pfx == NULL)
3024 return -EINVAL;
3025
Thomas Graf461d8832006-09-18 00:09:49 -07003026 if (tb[IFA_CACHEINFO]) {
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09003027 struct ifa_cacheinfo *ci;
Thomas Graf461d8832006-09-18 00:09:49 -07003028
3029 ci = nla_data(tb[IFA_CACHEINFO]);
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09003030 valid_lft = ci->ifa_valid;
Thomas Graf461d8832006-09-18 00:09:49 -07003031 preferred_lft = ci->ifa_prefered;
3032 } else {
3033 preferred_lft = INFINITY_LIFE_TIME;
3034 valid_lft = INFINITY_LIFE_TIME;
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09003035 }
3036
Thomas Graf7198f8c2006-09-18 00:13:46 -07003037 dev = __dev_get_by_index(ifm->ifa_index);
3038 if (dev == NULL)
3039 return -ENODEV;
3040
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003041 /* We ignore other flags so far. */
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07003042 ifa_flags = ifm->ifa_flags & (IFA_F_NODAD | IFA_F_HOMEADDRESS);
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003043
Thomas Graf7198f8c2006-09-18 00:13:46 -07003044 ifa = ipv6_get_ifaddr(pfx, dev, 1);
3045 if (ifa == NULL) {
3046 /*
3047 * It would be best to check for !NLM_F_CREATE here but
3048 * userspace alreay relies on not having to provide this.
3049 */
3050 return inet6_addr_add(ifm->ifa_index, pfx, ifm->ifa_prefixlen,
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003051 ifa_flags, preferred_lft, valid_lft);
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09003052 }
3053
Thomas Graf7198f8c2006-09-18 00:13:46 -07003054 if (nlh->nlmsg_flags & NLM_F_EXCL ||
3055 !(nlh->nlmsg_flags & NLM_F_REPLACE))
3056 err = -EEXIST;
3057 else
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003058 err = inet6_addr_modify(ifa, ifa_flags, preferred_lft, valid_lft);
Thomas Graf7198f8c2006-09-18 00:13:46 -07003059
3060 in6_ifa_put(ifa);
3061
3062 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063}
3064
Thomas Graf101bb222006-09-18 00:11:52 -07003065static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u8 flags,
3066 u8 scope, int ifindex)
3067{
3068 struct ifaddrmsg *ifm;
3069
3070 ifm = nlmsg_data(nlh);
3071 ifm->ifa_family = AF_INET6;
3072 ifm->ifa_prefixlen = prefixlen;
3073 ifm->ifa_flags = flags;
3074 ifm->ifa_scope = scope;
3075 ifm->ifa_index = ifindex;
3076}
3077
Thomas Graf85486af2006-09-18 00:11:24 -07003078static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
3079 unsigned long tstamp, u32 preferred, u32 valid)
3080{
3081 struct ifa_cacheinfo ci;
3082
3083 ci.cstamp = (u32)(TIME_DELTA(cstamp, INITIAL_JIFFIES) / HZ * 100
3084 + TIME_DELTA(cstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
3085 ci.tstamp = (u32)(TIME_DELTA(tstamp, INITIAL_JIFFIES) / HZ * 100
3086 + TIME_DELTA(tstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
3087 ci.ifa_prefered = preferred;
3088 ci.ifa_valid = valid;
3089
3090 return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
3091}
3092
Thomas Graf101bb222006-09-18 00:11:52 -07003093static inline int rt_scope(int ifa_scope)
3094{
3095 if (ifa_scope & IFA_HOST)
3096 return RT_SCOPE_HOST;
3097 else if (ifa_scope & IFA_LINK)
3098 return RT_SCOPE_LINK;
3099 else if (ifa_scope & IFA_SITE)
3100 return RT_SCOPE_SITE;
3101 else
3102 return RT_SCOPE_UNIVERSE;
3103}
3104
Thomas Graf0ab68032006-09-18 00:12:35 -07003105static inline int inet6_ifaddr_msgsize(void)
3106{
Thomas Graf339bf982006-11-10 14:10:15 -08003107 return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
3108 + nla_total_size(16) /* IFA_ADDRESS */
3109 + nla_total_size(sizeof(struct ifa_cacheinfo));
Thomas Graf0ab68032006-09-18 00:12:35 -07003110}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07003111
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07003113 u32 pid, u32 seq, int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 struct nlmsghdr *nlh;
Thomas Graf85486af2006-09-18 00:11:24 -07003116 u32 preferred, valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117
Thomas Graf0ab68032006-09-18 00:12:35 -07003118 nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags);
3119 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08003120 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07003121
Thomas Graf101bb222006-09-18 00:11:52 -07003122 put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope),
3123 ifa->idev->dev->ifindex);
3124
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 if (!(ifa->flags&IFA_F_PERMANENT)) {
Thomas Graf85486af2006-09-18 00:11:24 -07003126 preferred = ifa->prefered_lft;
3127 valid = ifa->valid_lft;
3128 if (preferred != INFINITY_LIFE_TIME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 long tval = (jiffies - ifa->tstamp)/HZ;
Thomas Graf85486af2006-09-18 00:11:24 -07003130 preferred -= tval;
3131 if (valid != INFINITY_LIFE_TIME)
3132 valid -= tval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 }
3134 } else {
Thomas Graf85486af2006-09-18 00:11:24 -07003135 preferred = INFINITY_LIFE_TIME;
3136 valid = INFINITY_LIFE_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137 }
Thomas Graf85486af2006-09-18 00:11:24 -07003138
Thomas Graf0ab68032006-09-18 00:12:35 -07003139 if (nla_put(skb, IFA_ADDRESS, 16, &ifa->addr) < 0 ||
Patrick McHardy26932562007-01-31 23:16:40 -08003140 put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0) {
3141 nlmsg_cancel(skb, nlh);
3142 return -EMSGSIZE;
3143 }
Thomas Graf85486af2006-09-18 00:11:24 -07003144
Thomas Graf0ab68032006-09-18 00:12:35 -07003145 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146}
3147
3148static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -07003149 u32 pid, u32 seq, int event, u16 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 struct nlmsghdr *nlh;
Thomas Graf101bb222006-09-18 00:11:52 -07003152 u8 scope = RT_SCOPE_UNIVERSE;
3153 int ifindex = ifmca->idev->dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154
Thomas Graf101bb222006-09-18 00:11:52 -07003155 if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE)
3156 scope = RT_SCOPE_SITE;
3157
Thomas Graf0ab68032006-09-18 00:12:35 -07003158 nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags);
3159 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08003160 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07003161
Thomas Graf101bb222006-09-18 00:11:52 -07003162 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
Thomas Graf0ab68032006-09-18 00:12:35 -07003163 if (nla_put(skb, IFA_MULTICAST, 16, &ifmca->mca_addr) < 0 ||
3164 put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp,
Patrick McHardy26932562007-01-31 23:16:40 -08003165 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
3166 nlmsg_cancel(skb, nlh);
3167 return -EMSGSIZE;
3168 }
Thomas Graf85486af2006-09-18 00:11:24 -07003169
Thomas Graf0ab68032006-09-18 00:12:35 -07003170 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171}
3172
3173static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07003174 u32 pid, u32 seq, int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176 struct nlmsghdr *nlh;
Thomas Graf101bb222006-09-18 00:11:52 -07003177 u8 scope = RT_SCOPE_UNIVERSE;
3178 int ifindex = ifaca->aca_idev->dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179
Thomas Graf101bb222006-09-18 00:11:52 -07003180 if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE)
3181 scope = RT_SCOPE_SITE;
3182
Thomas Graf0ab68032006-09-18 00:12:35 -07003183 nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags);
3184 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08003185 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07003186
Thomas Graf101bb222006-09-18 00:11:52 -07003187 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
Thomas Graf0ab68032006-09-18 00:12:35 -07003188 if (nla_put(skb, IFA_ANYCAST, 16, &ifaca->aca_addr) < 0 ||
3189 put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp,
Patrick McHardy26932562007-01-31 23:16:40 -08003190 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
3191 nlmsg_cancel(skb, nlh);
3192 return -EMSGSIZE;
3193 }
Thomas Graf85486af2006-09-18 00:11:24 -07003194
Thomas Graf0ab68032006-09-18 00:12:35 -07003195 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196}
3197
3198enum addr_type_t
3199{
3200 UNICAST_ADDR,
3201 MULTICAST_ADDR,
3202 ANYCAST_ADDR,
3203};
3204
3205static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
3206 enum addr_type_t type)
3207{
3208 int idx, ip_idx;
3209 int s_idx, s_ip_idx;
3210 int err = 1;
3211 struct net_device *dev;
3212 struct inet6_dev *idev = NULL;
3213 struct inet6_ifaddr *ifa;
3214 struct ifmcaddr6 *ifmca;
3215 struct ifacaddr6 *ifaca;
3216
3217 s_idx = cb->args[0];
3218 s_ip_idx = ip_idx = cb->args[1];
3219 read_lock(&dev_base_lock);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003220
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 for (dev = dev_base, idx = 0; dev; dev = dev->next, idx++) {
3222 if (idx < s_idx)
3223 continue;
3224 if (idx > s_idx)
3225 s_ip_idx = 0;
3226 ip_idx = 0;
3227 if ((idev = in6_dev_get(dev)) == NULL)
3228 continue;
3229 read_lock_bh(&idev->lock);
3230 switch (type) {
3231 case UNICAST_ADDR:
YOSHIFUJI Hideakiae9cda52005-06-28 13:00:30 -07003232 /* unicast address incl. temp addr */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233 for (ifa = idev->addr_list; ifa;
3234 ifa = ifa->if_next, ip_idx++) {
3235 if (ip_idx < s_ip_idx)
3236 continue;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003237 if ((err = inet6_fill_ifaddr(skb, ifa,
3238 NETLINK_CB(cb->skb).pid,
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07003239 cb->nlh->nlmsg_seq, RTM_NEWADDR,
3240 NLM_F_MULTI)) <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 goto done;
3242 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243 break;
3244 case MULTICAST_ADDR:
3245 /* multicast address */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003246 for (ifmca = idev->mc_list; ifmca;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247 ifmca = ifmca->next, ip_idx++) {
3248 if (ip_idx < s_ip_idx)
3249 continue;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003250 if ((err = inet6_fill_ifmcaddr(skb, ifmca,
3251 NETLINK_CB(cb->skb).pid,
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07003252 cb->nlh->nlmsg_seq, RTM_GETMULTICAST,
3253 NLM_F_MULTI)) <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 goto done;
3255 }
3256 break;
3257 case ANYCAST_ADDR:
3258 /* anycast address */
3259 for (ifaca = idev->ac_list; ifaca;
3260 ifaca = ifaca->aca_next, ip_idx++) {
3261 if (ip_idx < s_ip_idx)
3262 continue;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003263 if ((err = inet6_fill_ifacaddr(skb, ifaca,
3264 NETLINK_CB(cb->skb).pid,
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07003265 cb->nlh->nlmsg_seq, RTM_GETANYCAST,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003266 NLM_F_MULTI)) <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267 goto done;
3268 }
3269 break;
3270 default:
3271 break;
3272 }
3273 read_unlock_bh(&idev->lock);
3274 in6_dev_put(idev);
3275 }
3276done:
3277 if (err <= 0) {
3278 read_unlock_bh(&idev->lock);
3279 in6_dev_put(idev);
3280 }
3281 read_unlock(&dev_base_lock);
3282 cb->args[0] = idx;
3283 cb->args[1] = ip_idx;
3284 return skb->len;
3285}
3286
3287static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
3288{
3289 enum addr_type_t type = UNICAST_ADDR;
3290 return inet6_dump_addr(skb, cb, type);
3291}
3292
3293static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
3294{
3295 enum addr_type_t type = MULTICAST_ADDR;
3296 return inet6_dump_addr(skb, cb, type);
3297}
3298
3299
3300static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
3301{
3302 enum addr_type_t type = ANYCAST_ADDR;
3303 return inet6_dump_addr(skb, cb, type);
3304}
3305
Thomas Graf1b29fc22006-09-18 00:10:50 -07003306static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr* nlh,
3307 void *arg)
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003308{
Thomas Graf1b29fc22006-09-18 00:10:50 -07003309 struct ifaddrmsg *ifm;
3310 struct nlattr *tb[IFA_MAX+1];
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003311 struct in6_addr *addr = NULL;
3312 struct net_device *dev = NULL;
3313 struct inet6_ifaddr *ifa;
3314 struct sk_buff *skb;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003315 int err;
3316
Thomas Graf1b29fc22006-09-18 00:10:50 -07003317 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
3318 if (err < 0)
3319 goto errout;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003320
Thomas Graf1b29fc22006-09-18 00:10:50 -07003321 addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
3322 if (addr == NULL) {
3323 err = -EINVAL;
3324 goto errout;
3325 }
3326
3327 ifm = nlmsg_data(nlh);
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003328 if (ifm->ifa_index)
3329 dev = __dev_get_by_index(ifm->ifa_index);
3330
Thomas Graf1b29fc22006-09-18 00:10:50 -07003331 if ((ifa = ipv6_get_ifaddr(addr, dev, 1)) == NULL) {
3332 err = -EADDRNOTAVAIL;
3333 goto errout;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003334 }
3335
Thomas Graf0ab68032006-09-18 00:12:35 -07003336 if ((skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL)) == NULL) {
Thomas Graf1b29fc22006-09-18 00:10:50 -07003337 err = -ENOBUFS;
3338 goto errout_ifa;
3339 }
3340
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003341 err = inet6_fill_ifaddr(skb, ifa, NETLINK_CB(in_skb).pid,
3342 nlh->nlmsg_seq, RTM_NEWADDR, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08003343 if (err < 0) {
3344 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
3345 WARN_ON(err == -EMSGSIZE);
3346 kfree_skb(skb);
3347 goto errout_ifa;
3348 }
Thomas Graf2942e902006-08-15 00:30:25 -07003349 err = rtnl_unicast(skb, NETLINK_CB(in_skb).pid);
Thomas Graf1b29fc22006-09-18 00:10:50 -07003350errout_ifa:
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003351 in6_ifa_put(ifa);
Thomas Graf1b29fc22006-09-18 00:10:50 -07003352errout:
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003353 return err;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003354}
3355
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
3357{
3358 struct sk_buff *skb;
Thomas Graf5d620262006-08-15 00:35:02 -07003359 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360
Thomas Graf0ab68032006-09-18 00:12:35 -07003361 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC);
Thomas Graf5d620262006-08-15 00:35:02 -07003362 if (skb == NULL)
3363 goto errout;
3364
3365 err = inet6_fill_ifaddr(skb, ifa, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08003366 if (err < 0) {
3367 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
3368 WARN_ON(err == -EMSGSIZE);
3369 kfree_skb(skb);
3370 goto errout;
3371 }
Thomas Graf5d620262006-08-15 00:35:02 -07003372 err = rtnl_notify(skb, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
3373errout:
3374 if (err < 0)
3375 rtnl_set_sk_err(RTNLGRP_IPV6_IFADDR, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376}
3377
3378static void inline ipv6_store_devconf(struct ipv6_devconf *cnf,
3379 __s32 *array, int bytes)
3380{
Thomas Graf04561c12006-11-14 19:53:58 -08003381 BUG_ON(bytes < (DEVCONF_MAX * 4));
3382
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383 memset(array, 0, bytes);
3384 array[DEVCONF_FORWARDING] = cnf->forwarding;
3385 array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
3386 array[DEVCONF_MTU6] = cnf->mtu6;
3387 array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
3388 array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
3389 array[DEVCONF_AUTOCONF] = cnf->autoconf;
3390 array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
3391 array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
3392 array[DEVCONF_RTR_SOLICIT_INTERVAL] = cnf->rtr_solicit_interval;
3393 array[DEVCONF_RTR_SOLICIT_DELAY] = cnf->rtr_solicit_delay;
3394 array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
3395#ifdef CONFIG_IPV6_PRIVACY
3396 array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
3397 array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
3398 array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
3399 array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
3400 array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
3401#endif
3402 array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -08003403 array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr;
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -08003404 array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo;
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08003405#ifdef CONFIG_IPV6_ROUTER_PREF
3406 array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref;
YOSHIFUJI Hideaki52e16352006-03-20 17:05:47 -08003407 array[DEVCONF_RTR_PROBE_INTERVAL] = cnf->rtr_probe_interval;
Neil Hormanfa03ef32007-01-30 14:30:10 -08003408#ifdef CONFIG_IPV6_ROUTE_INFO
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -08003409 array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen;
3410#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08003411#endif
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -07003412 array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413}
3414
Thomas Graf339bf982006-11-10 14:10:15 -08003415static inline size_t inet6_if_nlmsg_size(void)
3416{
3417 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
3418 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
3419 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
3420 + nla_total_size(4) /* IFLA_MTU */
3421 + nla_total_size(4) /* IFLA_LINK */
3422 + nla_total_size( /* IFLA_PROTINFO */
3423 nla_total_size(4) /* IFLA_INET6_FLAGS */
3424 + nla_total_size(sizeof(struct ifla_cacheinfo))
3425 + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */
3426 );
3427}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07003428
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003429static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07003430 u32 pid, u32 seq, int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431{
Thomas Graf04561c12006-11-14 19:53:58 -08003432 struct net_device *dev = idev->dev;
3433 struct nlattr *conf;
3434 struct ifinfomsg *hdr;
3435 struct nlmsghdr *nlh;
3436 void *protoinfo;
3437 struct ifla_cacheinfo ci;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438
Thomas Graf04561c12006-11-14 19:53:58 -08003439 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*hdr), flags);
3440 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08003441 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442
Thomas Graf04561c12006-11-14 19:53:58 -08003443 hdr = nlmsg_data(nlh);
3444 hdr->ifi_family = AF_INET6;
3445 hdr->__ifi_pad = 0;
3446 hdr->ifi_type = dev->type;
3447 hdr->ifi_index = dev->ifindex;
3448 hdr->ifi_flags = dev_get_flags(dev);
3449 hdr->ifi_change = 0;
3450
3451 NLA_PUT_STRING(skb, IFLA_IFNAME, dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452
3453 if (dev->addr_len)
Thomas Graf04561c12006-11-14 19:53:58 -08003454 NLA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455
Thomas Graf04561c12006-11-14 19:53:58 -08003456 NLA_PUT_U32(skb, IFLA_MTU, dev->mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457 if (dev->ifindex != dev->iflink)
Thomas Graf04561c12006-11-14 19:53:58 -08003458 NLA_PUT_U32(skb, IFLA_LINK, dev->iflink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459
Thomas Graf04561c12006-11-14 19:53:58 -08003460 protoinfo = nla_nest_start(skb, IFLA_PROTINFO);
3461 if (protoinfo == NULL)
3462 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463
Thomas Graf04561c12006-11-14 19:53:58 -08003464 NLA_PUT_U32(skb, IFLA_INET6_FLAGS, idev->if_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466 ci.max_reasm_len = IPV6_MAXPLEN;
3467 ci.tstamp = (__u32)(TIME_DELTA(idev->tstamp, INITIAL_JIFFIES) / HZ * 100
3468 + TIME_DELTA(idev->tstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
3469 ci.reachable_time = idev->nd_parms->reachable_time;
3470 ci.retrans_time = idev->nd_parms->retrans_time;
Thomas Graf04561c12006-11-14 19:53:58 -08003471 NLA_PUT(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci);
3472
3473 conf = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32));
3474 if (conf == NULL)
3475 goto nla_put_failure;
3476 ipv6_store_devconf(&idev->cnf, nla_data(conf), nla_len(conf));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477
3478 /* XXX - Statistics/MC not implemented */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479
Thomas Graf04561c12006-11-14 19:53:58 -08003480 nla_nest_end(skb, protoinfo);
3481 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482
Thomas Graf04561c12006-11-14 19:53:58 -08003483nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08003484 nlmsg_cancel(skb, nlh);
3485 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486}
3487
3488static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
3489{
3490 int idx, err;
3491 int s_idx = cb->args[0];
3492 struct net_device *dev;
3493 struct inet6_dev *idev;
3494
3495 read_lock(&dev_base_lock);
3496 for (dev=dev_base, idx=0; dev; dev = dev->next, idx++) {
3497 if (idx < s_idx)
3498 continue;
3499 if ((idev = in6_dev_get(dev)) == NULL)
3500 continue;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003501 err = inet6_fill_ifinfo(skb, idev, NETLINK_CB(cb->skb).pid,
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07003502 cb->nlh->nlmsg_seq, RTM_NEWLINK, NLM_F_MULTI);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503 in6_dev_put(idev);
3504 if (err <= 0)
3505 break;
3506 }
3507 read_unlock(&dev_base_lock);
3508 cb->args[0] = idx;
3509
3510 return skb->len;
3511}
3512
3513void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
3514{
3515 struct sk_buff *skb;
Thomas Graf8d7a76c2006-08-15 00:35:47 -07003516 int err = -ENOBUFS;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003517
Thomas Graf339bf982006-11-10 14:10:15 -08003518 skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC);
Thomas Graf8d7a76c2006-08-15 00:35:47 -07003519 if (skb == NULL)
3520 goto errout;
3521
3522 err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08003523 if (err < 0) {
3524 /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */
3525 WARN_ON(err == -EMSGSIZE);
3526 kfree_skb(skb);
3527 goto errout;
3528 }
Thomas Graf8d7a76c2006-08-15 00:35:47 -07003529 err = rtnl_notify(skb, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
3530errout:
3531 if (err < 0)
3532 rtnl_set_sk_err(RTNLGRP_IPV6_IFADDR, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533}
3534
Thomas Graf339bf982006-11-10 14:10:15 -08003535static inline size_t inet6_prefix_nlmsg_size(void)
3536{
3537 return NLMSG_ALIGN(sizeof(struct prefixmsg))
3538 + nla_total_size(sizeof(struct in6_addr))
3539 + nla_total_size(sizeof(struct prefix_cacheinfo));
3540}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07003541
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
Thomas Graf6051e2f2006-11-14 19:54:19 -08003543 struct prefix_info *pinfo, u32 pid, u32 seq,
3544 int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545{
Thomas Graf6051e2f2006-11-14 19:54:19 -08003546 struct prefixmsg *pmsg;
3547 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548 struct prefix_cacheinfo ci;
3549
Thomas Graf6051e2f2006-11-14 19:54:19 -08003550 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*pmsg), flags);
3551 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08003552 return -EMSGSIZE;
Thomas Graf6051e2f2006-11-14 19:54:19 -08003553
3554 pmsg = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555 pmsg->prefix_family = AF_INET6;
Patrick McHardy8a470772005-06-28 12:56:45 -07003556 pmsg->prefix_pad1 = 0;
3557 pmsg->prefix_pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558 pmsg->prefix_ifindex = idev->dev->ifindex;
3559 pmsg->prefix_len = pinfo->prefix_len;
3560 pmsg->prefix_type = pinfo->type;
Patrick McHardy8a470772005-06-28 12:56:45 -07003561 pmsg->prefix_pad3 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562 pmsg->prefix_flags = 0;
3563 if (pinfo->onlink)
3564 pmsg->prefix_flags |= IF_PREFIX_ONLINK;
3565 if (pinfo->autoconf)
3566 pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
3567
Thomas Graf6051e2f2006-11-14 19:54:19 -08003568 NLA_PUT(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569
3570 ci.preferred_time = ntohl(pinfo->prefered);
3571 ci.valid_time = ntohl(pinfo->valid);
Thomas Graf6051e2f2006-11-14 19:54:19 -08003572 NLA_PUT(skb, PREFIX_CACHEINFO, sizeof(ci), &ci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573
Thomas Graf6051e2f2006-11-14 19:54:19 -08003574 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575
Thomas Graf6051e2f2006-11-14 19:54:19 -08003576nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08003577 nlmsg_cancel(skb, nlh);
3578 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579}
3580
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003581static void inet6_prefix_notify(int event, struct inet6_dev *idev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582 struct prefix_info *pinfo)
3583{
3584 struct sk_buff *skb;
Thomas Graf8c384bfa2006-08-15 00:36:07 -07003585 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586
Thomas Graf339bf982006-11-10 14:10:15 -08003587 skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC);
Thomas Graf8c384bfa2006-08-15 00:36:07 -07003588 if (skb == NULL)
3589 goto errout;
3590
3591 err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08003592 if (err < 0) {
3593 /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */
3594 WARN_ON(err == -EMSGSIZE);
3595 kfree_skb(skb);
3596 goto errout;
3597 }
Thomas Graf8c384bfa2006-08-15 00:36:07 -07003598 err = rtnl_notify(skb, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC);
3599errout:
3600 if (err < 0)
3601 rtnl_set_sk_err(RTNLGRP_IPV6_PREFIX, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602}
3603
Thomas Grafdb46edc2005-05-03 14:29:39 -07003604static struct rtnetlink_link inet6_rtnetlink_table[RTM_NR_MSGTYPES] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605 [RTM_GETLINK - RTM_BASE] = { .dumpit = inet6_dump_ifinfo, },
3606 [RTM_NEWADDR - RTM_BASE] = { .doit = inet6_rtm_newaddr, },
3607 [RTM_DELADDR - RTM_BASE] = { .doit = inet6_rtm_deladdr, },
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003608 [RTM_GETADDR - RTM_BASE] = { .doit = inet6_rtm_getaddr,
3609 .dumpit = inet6_dump_ifaddr, },
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610 [RTM_GETMULTICAST - RTM_BASE] = { .dumpit = inet6_dump_ifmcaddr, },
3611 [RTM_GETANYCAST - RTM_BASE] = { .dumpit = inet6_dump_ifacaddr, },
3612 [RTM_NEWROUTE - RTM_BASE] = { .doit = inet6_rtm_newroute, },
3613 [RTM_DELROUTE - RTM_BASE] = { .doit = inet6_rtm_delroute, },
3614 [RTM_GETROUTE - RTM_BASE] = { .doit = inet6_rtm_getroute,
3615 .dumpit = inet6_dump_fib, },
David S. Miller8423a9a2006-08-07 21:54:37 -07003616#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Thomas Graf101367c2006-08-04 03:39:02 -07003617 [RTM_GETRULE - RTM_BASE] = { .dumpit = fib6_rules_dump, },
David S. Miller8423a9a2006-08-07 21:54:37 -07003618#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619};
3620
3621static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
3622{
3623 inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
3624
3625 switch (event) {
3626 case RTM_NEWADDR:
Thomas Graf40e22e82006-08-22 00:00:45 -07003627 ip6_ins_rt(ifp->rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628 if (ifp->idev->cnf.forwarding)
3629 addrconf_join_anycast(ifp);
3630 break;
3631 case RTM_DELADDR:
3632 if (ifp->idev->cnf.forwarding)
3633 addrconf_leave_anycast(ifp);
3634 addrconf_leave_solict(ifp->idev, &ifp->addr);
3635 dst_hold(&ifp->rt->u.dst);
Thomas Grafe0a1ad732006-08-22 00:00:21 -07003636 if (ip6_del_rt(ifp->rt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637 dst_free(&ifp->rt->u.dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638 break;
3639 }
3640}
3641
3642static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
3643{
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07003644 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645 if (likely(ifp->idev->dead == 0))
3646 __ipv6_ifa_notify(event, ifp);
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07003647 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648}
3649
3650#ifdef CONFIG_SYSCTL
3651
3652static
3653int addrconf_sysctl_forward(ctl_table *ctl, int write, struct file * filp,
3654 void __user *buffer, size_t *lenp, loff_t *ppos)
3655{
3656 int *valp = ctl->data;
3657 int val = *valp;
3658 int ret;
3659
3660 ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
3661
3662 if (write && valp != &ipv6_devconf_dflt.forwarding) {
3663 if (valp != &ipv6_devconf.forwarding) {
3664 if ((!*valp) ^ (!val)) {
3665 struct inet6_dev *idev = (struct inet6_dev *)ctl->extra1;
3666 if (idev == NULL)
3667 return ret;
3668 dev_forward_change(idev);
3669 }
3670 } else {
3671 ipv6_devconf_dflt.forwarding = ipv6_devconf.forwarding;
3672 addrconf_forward_change();
3673 }
3674 if (*valp)
3675 rt6_purge_dflt_routers();
3676 }
3677
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003678 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679}
3680
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003681static int addrconf_sysctl_forward_strategy(ctl_table *table,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682 int __user *name, int nlen,
3683 void __user *oldval,
3684 size_t __user *oldlenp,
Alexey Dobriyan1f29bcd2006-12-10 02:19:10 -08003685 void __user *newval, size_t newlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686{
3687 int *valp = table->data;
3688 int new;
3689
3690 if (!newval || !newlen)
3691 return 0;
3692 if (newlen != sizeof(int))
3693 return -EINVAL;
3694 if (get_user(new, (int __user *)newval))
3695 return -EFAULT;
3696 if (new == *valp)
3697 return 0;
3698 if (oldval && oldlenp) {
3699 size_t len;
3700 if (get_user(len, oldlenp))
3701 return -EFAULT;
3702 if (len) {
3703 if (len > table->maxlen)
3704 len = table->maxlen;
3705 if (copy_to_user(oldval, valp, len))
3706 return -EFAULT;
3707 if (put_user(len, oldlenp))
3708 return -EFAULT;
3709 }
3710 }
3711
3712 if (valp != &ipv6_devconf_dflt.forwarding) {
3713 if (valp != &ipv6_devconf.forwarding) {
3714 struct inet6_dev *idev = (struct inet6_dev *)table->extra1;
3715 int changed;
3716 if (unlikely(idev == NULL))
3717 return -ENODEV;
3718 changed = (!*valp) ^ (!new);
3719 *valp = new;
3720 if (changed)
3721 dev_forward_change(idev);
3722 } else {
3723 *valp = new;
3724 addrconf_forward_change();
3725 }
3726
3727 if (*valp)
3728 rt6_purge_dflt_routers();
3729 } else
3730 *valp = new;
3731
3732 return 1;
3733}
3734
3735static struct addrconf_sysctl_table
3736{
3737 struct ctl_table_header *sysctl_header;
3738 ctl_table addrconf_vars[__NET_IPV6_MAX];
3739 ctl_table addrconf_dev[2];
3740 ctl_table addrconf_conf_dir[2];
3741 ctl_table addrconf_proto_dir[2];
3742 ctl_table addrconf_root_dir[2];
Brian Haleyab32ea52006-09-22 14:15:41 -07003743} addrconf_sysctl __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744 .sysctl_header = NULL,
3745 .addrconf_vars = {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003746 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747 .ctl_name = NET_IPV6_FORWARDING,
3748 .procname = "forwarding",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003749 .data = &ipv6_devconf.forwarding,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750 .maxlen = sizeof(int),
3751 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003752 .proc_handler = &addrconf_sysctl_forward,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753 .strategy = &addrconf_sysctl_forward_strategy,
3754 },
3755 {
3756 .ctl_name = NET_IPV6_HOP_LIMIT,
3757 .procname = "hop_limit",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003758 .data = &ipv6_devconf.hop_limit,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759 .maxlen = sizeof(int),
3760 .mode = 0644,
3761 .proc_handler = proc_dointvec,
3762 },
3763 {
3764 .ctl_name = NET_IPV6_MTU,
3765 .procname = "mtu",
3766 .data = &ipv6_devconf.mtu6,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003767 .maxlen = sizeof(int),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003769 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770 },
3771 {
3772 .ctl_name = NET_IPV6_ACCEPT_RA,
3773 .procname = "accept_ra",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003774 .data = &ipv6_devconf.accept_ra,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775 .maxlen = sizeof(int),
3776 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003777 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778 },
3779 {
3780 .ctl_name = NET_IPV6_ACCEPT_REDIRECTS,
3781 .procname = "accept_redirects",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003782 .data = &ipv6_devconf.accept_redirects,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783 .maxlen = sizeof(int),
3784 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003785 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786 },
3787 {
3788 .ctl_name = NET_IPV6_AUTOCONF,
3789 .procname = "autoconf",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003790 .data = &ipv6_devconf.autoconf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791 .maxlen = sizeof(int),
3792 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003793 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794 },
3795 {
3796 .ctl_name = NET_IPV6_DAD_TRANSMITS,
3797 .procname = "dad_transmits",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003798 .data = &ipv6_devconf.dad_transmits,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799 .maxlen = sizeof(int),
3800 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003801 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802 },
3803 {
3804 .ctl_name = NET_IPV6_RTR_SOLICITS,
3805 .procname = "router_solicitations",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003806 .data = &ipv6_devconf.rtr_solicits,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807 .maxlen = sizeof(int),
3808 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003809 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810 },
3811 {
3812 .ctl_name = NET_IPV6_RTR_SOLICIT_INTERVAL,
3813 .procname = "router_solicitation_interval",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003814 .data = &ipv6_devconf.rtr_solicit_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815 .maxlen = sizeof(int),
3816 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003817 .proc_handler = &proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818 .strategy = &sysctl_jiffies,
3819 },
3820 {
3821 .ctl_name = NET_IPV6_RTR_SOLICIT_DELAY,
3822 .procname = "router_solicitation_delay",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003823 .data = &ipv6_devconf.rtr_solicit_delay,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824 .maxlen = sizeof(int),
3825 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003826 .proc_handler = &proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827 .strategy = &sysctl_jiffies,
3828 },
3829 {
3830 .ctl_name = NET_IPV6_FORCE_MLD_VERSION,
3831 .procname = "force_mld_version",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003832 .data = &ipv6_devconf.force_mld_version,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833 .maxlen = sizeof(int),
3834 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003835 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836 },
3837#ifdef CONFIG_IPV6_PRIVACY
3838 {
3839 .ctl_name = NET_IPV6_USE_TEMPADDR,
3840 .procname = "use_tempaddr",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003841 .data = &ipv6_devconf.use_tempaddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003842 .maxlen = sizeof(int),
3843 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003844 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845 },
3846 {
3847 .ctl_name = NET_IPV6_TEMP_VALID_LFT,
3848 .procname = "temp_valid_lft",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003849 .data = &ipv6_devconf.temp_valid_lft,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850 .maxlen = sizeof(int),
3851 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003852 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853 },
3854 {
3855 .ctl_name = NET_IPV6_TEMP_PREFERED_LFT,
3856 .procname = "temp_prefered_lft",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003857 .data = &ipv6_devconf.temp_prefered_lft,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858 .maxlen = sizeof(int),
3859 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003860 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861 },
3862 {
3863 .ctl_name = NET_IPV6_REGEN_MAX_RETRY,
3864 .procname = "regen_max_retry",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003865 .data = &ipv6_devconf.regen_max_retry,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866 .maxlen = sizeof(int),
3867 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003868 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003869 },
3870 {
3871 .ctl_name = NET_IPV6_MAX_DESYNC_FACTOR,
3872 .procname = "max_desync_factor",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003873 .data = &ipv6_devconf.max_desync_factor,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874 .maxlen = sizeof(int),
3875 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003876 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877 },
3878#endif
3879 {
3880 .ctl_name = NET_IPV6_MAX_ADDRESSES,
3881 .procname = "max_addresses",
3882 .data = &ipv6_devconf.max_addresses,
3883 .maxlen = sizeof(int),
3884 .mode = 0644,
3885 .proc_handler = &proc_dointvec,
3886 },
3887 {
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -08003888 .ctl_name = NET_IPV6_ACCEPT_RA_DEFRTR,
3889 .procname = "accept_ra_defrtr",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003890 .data = &ipv6_devconf.accept_ra_defrtr,
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -08003891 .maxlen = sizeof(int),
3892 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003893 .proc_handler = &proc_dointvec,
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -08003894 },
3895 {
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -08003896 .ctl_name = NET_IPV6_ACCEPT_RA_PINFO,
3897 .procname = "accept_ra_pinfo",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003898 .data = &ipv6_devconf.accept_ra_pinfo,
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -08003899 .maxlen = sizeof(int),
3900 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003901 .proc_handler = &proc_dointvec,
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -08003902 },
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08003903#ifdef CONFIG_IPV6_ROUTER_PREF
3904 {
3905 .ctl_name = NET_IPV6_ACCEPT_RA_RTR_PREF,
3906 .procname = "accept_ra_rtr_pref",
3907 .data = &ipv6_devconf.accept_ra_rtr_pref,
3908 .maxlen = sizeof(int),
3909 .mode = 0644,
3910 .proc_handler = &proc_dointvec,
3911 },
YOSHIFUJI Hideaki52e16352006-03-20 17:05:47 -08003912 {
3913 .ctl_name = NET_IPV6_RTR_PROBE_INTERVAL,
3914 .procname = "router_probe_interval",
3915 .data = &ipv6_devconf.rtr_probe_interval,
3916 .maxlen = sizeof(int),
3917 .mode = 0644,
3918 .proc_handler = &proc_dointvec_jiffies,
3919 .strategy = &sysctl_jiffies,
3920 },
Neil Hormanfa03ef32007-01-30 14:30:10 -08003921#ifdef CONFIG_IPV6_ROUTE_INFO
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -08003922 {
3923 .ctl_name = NET_IPV6_ACCEPT_RA_RT_INFO_MAX_PLEN,
3924 .procname = "accept_ra_rt_info_max_plen",
3925 .data = &ipv6_devconf.accept_ra_rt_info_max_plen,
3926 .maxlen = sizeof(int),
3927 .mode = 0644,
3928 .proc_handler = &proc_dointvec,
3929 },
3930#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08003931#endif
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -08003932 {
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -07003933 .ctl_name = NET_IPV6_PROXY_NDP,
3934 .procname = "proxy_ndp",
3935 .data = &ipv6_devconf.proxy_ndp,
3936 .maxlen = sizeof(int),
3937 .mode = 0644,
3938 .proc_handler = &proc_dointvec,
3939 },
3940 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941 .ctl_name = 0, /* sentinel */
3942 }
3943 },
3944 .addrconf_dev = {
3945 {
3946 .ctl_name = NET_PROTO_CONF_ALL,
3947 .procname = "all",
3948 .mode = 0555,
3949 .child = addrconf_sysctl.addrconf_vars,
3950 },
3951 {
3952 .ctl_name = 0, /* sentinel */
3953 }
3954 },
3955 .addrconf_conf_dir = {
3956 {
3957 .ctl_name = NET_IPV6_CONF,
3958 .procname = "conf",
3959 .mode = 0555,
3960 .child = addrconf_sysctl.addrconf_dev,
3961 },
3962 {
3963 .ctl_name = 0, /* sentinel */
3964 }
3965 },
3966 .addrconf_proto_dir = {
3967 {
3968 .ctl_name = NET_IPV6,
3969 .procname = "ipv6",
3970 .mode = 0555,
3971 .child = addrconf_sysctl.addrconf_conf_dir,
3972 },
3973 {
3974 .ctl_name = 0, /* sentinel */
3975 }
3976 },
3977 .addrconf_root_dir = {
3978 {
3979 .ctl_name = CTL_NET,
3980 .procname = "net",
3981 .mode = 0555,
3982 .child = addrconf_sysctl.addrconf_proto_dir,
3983 },
3984 {
3985 .ctl_name = 0, /* sentinel */
3986 }
3987 },
3988};
3989
3990static void addrconf_sysctl_register(struct inet6_dev *idev, struct ipv6_devconf *p)
3991{
3992 int i;
3993 struct net_device *dev = idev ? idev->dev : NULL;
3994 struct addrconf_sysctl_table *t;
3995 char *dev_name = NULL;
3996
Arnaldo Carvalho de Meloaf879cc2006-11-17 12:14:37 -02003997 t = kmemdup(&addrconf_sysctl, sizeof(*t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998 if (t == NULL)
3999 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000 for (i=0; t->addrconf_vars[i].data; i++) {
4001 t->addrconf_vars[i].data += (char*)p - (char*)&ipv6_devconf;
4002 t->addrconf_vars[i].de = NULL;
4003 t->addrconf_vars[i].extra1 = idev; /* embedded; no ref */
4004 }
4005 if (dev) {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004006 dev_name = dev->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007 t->addrconf_dev[0].ctl_name = dev->ifindex;
4008 } else {
4009 dev_name = "default";
4010 t->addrconf_dev[0].ctl_name = NET_PROTO_CONF_DEFAULT;
4011 }
4012
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004013 /*
4014 * Make a copy of dev_name, because '.procname' is regarded as const
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015 * by sysctl and we wouldn't want anyone to change it under our feet
4016 * (see SIOCSIFNAME).
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004017 */
Paulo Marques543537b2005-06-23 00:09:02 -07004018 dev_name = kstrdup(dev_name, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019 if (!dev_name)
4020 goto free;
4021
4022 t->addrconf_dev[0].procname = dev_name;
4023
4024 t->addrconf_dev[0].child = t->addrconf_vars;
4025 t->addrconf_dev[0].de = NULL;
4026 t->addrconf_conf_dir[0].child = t->addrconf_dev;
4027 t->addrconf_conf_dir[0].de = NULL;
4028 t->addrconf_proto_dir[0].child = t->addrconf_conf_dir;
4029 t->addrconf_proto_dir[0].de = NULL;
4030 t->addrconf_root_dir[0].child = t->addrconf_proto_dir;
4031 t->addrconf_root_dir[0].de = NULL;
4032
4033 t->sysctl_header = register_sysctl_table(t->addrconf_root_dir, 0);
4034 if (t->sysctl_header == NULL)
4035 goto free_procname;
4036 else
4037 p->sysctl = t;
4038 return;
4039
4040 /* error path */
4041 free_procname:
4042 kfree(dev_name);
4043 free:
4044 kfree(t);
4045
4046 return;
4047}
4048
4049static void addrconf_sysctl_unregister(struct ipv6_devconf *p)
4050{
4051 if (p->sysctl) {
4052 struct addrconf_sysctl_table *t = p->sysctl;
4053 p->sysctl = NULL;
4054 unregister_sysctl_table(t->sysctl_header);
4055 kfree(t->addrconf_dev[0].procname);
4056 kfree(t);
4057 }
4058}
4059
4060
4061#endif
4062
4063/*
4064 * Device notifier
4065 */
4066
4067int register_inet6addr_notifier(struct notifier_block *nb)
4068{
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004069 return atomic_notifier_chain_register(&inet6addr_chain, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070}
4071
4072int unregister_inet6addr_notifier(struct notifier_block *nb)
4073{
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004074 return atomic_notifier_chain_unregister(&inet6addr_chain,nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075}
4076
4077/*
4078 * Init / cleanup code
4079 */
4080
4081int __init addrconf_init(void)
4082{
4083 int err = 0;
4084
4085 /* The addrconf netdev notifier requires that loopback_dev
4086 * has it's ipv6 private information allocated and setup
4087 * before it can bring up and give link-local addresses
4088 * to other devices which are up.
4089 *
4090 * Unfortunately, loopback_dev is not necessarily the first
4091 * entry in the global dev_base list of net devices. In fact,
4092 * it is likely to be the very last entry on that list.
4093 * So this causes the notifier registry below to try and
4094 * give link-local addresses to all devices besides loopback_dev
4095 * first, then loopback_dev, which cases all the non-loopback_dev
4096 * devices to fail to get a link-local address.
4097 *
4098 * So, as a temporary fix, allocate the ipv6 structure for
4099 * loopback_dev first by hand.
4100 * Longer term, all of the dependencies ipv6 has upon the loopback
4101 * device and it being up should be removed.
4102 */
4103 rtnl_lock();
4104 if (!ipv6_add_dev(&loopback_dev))
4105 err = -ENOMEM;
4106 rtnl_unlock();
4107 if (err)
4108 return err;
4109
Herbert Xuc62dba92005-09-26 15:10:16 -07004110 ip6_null_entry.rt6i_idev = in6_dev_get(&loopback_dev);
4111
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112 register_netdevice_notifier(&ipv6_dev_notf);
4113
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114 addrconf_verify(0);
4115 rtnetlink_links[PF_INET6] = inet6_rtnetlink_table;
4116#ifdef CONFIG_SYSCTL
4117 addrconf_sysctl.sysctl_header =
4118 register_sysctl_table(addrconf_sysctl.addrconf_root_dir, 0);
4119 addrconf_sysctl_register(NULL, &ipv6_devconf_dflt);
4120#endif
4121
4122 return 0;
4123}
4124
4125void __exit addrconf_cleanup(void)
4126{
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004127 struct net_device *dev;
4128 struct inet6_dev *idev;
4129 struct inet6_ifaddr *ifa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130 int i;
4131
4132 unregister_netdevice_notifier(&ipv6_dev_notf);
4133
4134 rtnetlink_links[PF_INET6] = NULL;
4135#ifdef CONFIG_SYSCTL
4136 addrconf_sysctl_unregister(&ipv6_devconf_dflt);
4137 addrconf_sysctl_unregister(&ipv6_devconf);
4138#endif
4139
4140 rtnl_lock();
4141
4142 /*
4143 * clean dev list.
4144 */
4145
4146 for (dev=dev_base; dev; dev=dev->next) {
4147 if ((idev = __in6_dev_get(dev)) == NULL)
4148 continue;
4149 addrconf_ifdown(dev, 1);
4150 }
4151 addrconf_ifdown(&loopback_dev, 2);
4152
4153 /*
4154 * Check hash table.
4155 */
4156
4157 write_lock_bh(&addrconf_hash_lock);
4158 for (i=0; i < IN6_ADDR_HSIZE; i++) {
4159 for (ifa=inet6_addr_lst[i]; ifa; ) {
4160 struct inet6_ifaddr *bifa;
4161
4162 bifa = ifa;
4163 ifa = ifa->lst_next;
4164 printk(KERN_DEBUG "bug: IPv6 address leakage detected: ifa=%p\n", bifa);
4165 /* Do not free it; something is wrong.
4166 Now we can investigate it with debugger.
4167 */
4168 }
4169 }
4170 write_unlock_bh(&addrconf_hash_lock);
4171
4172 del_timer(&addr_chk_timer);
4173
4174 rtnl_unlock();
4175
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176#ifdef CONFIG_PROC_FS
4177 proc_net_remove("if_inet6");
4178#endif
4179}