[VLAN]: Add two missing checks to vlan_ioctl_handler()
[deliverable/linux.git] / net / ipv6 / addrconf.c
CommitLineData
1da177e4
LT
1/*
2 * IPv6 Address [auto]configuration
3 * Linux INET6 implementation
4 *
5 * Authors:
6 * Pedro Roque <roque@di.fc.ul.pt>
7 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
8 *
9 * $Id: addrconf.c,v 1.69 2001/10/31 21:55:54 davem Exp $
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version
14 * 2 of the License, or (at your option) any later version.
15 */
16
17/*
18 * Changes:
19 *
20 * Janos Farkas : delete timer on ifdown
21 * <chexum@bankinf.banki.hu>
22 * Andi Kleen : kill double kfree on module
23 * unload.
24 * Maciej W. Rozycki : FDDI support
25 * sekiya@USAGI : Don't send too many RS
26 * packets.
27 * yoshfuji@USAGI : Fixed interval between DAD
28 * packets.
29 * YOSHIFUJI Hideaki @USAGI : improved accuracy of
30 * address validation timer.
31 * YOSHIFUJI Hideaki @USAGI : Privacy Extensions (RFC3041)
32 * support.
33 * Yuji SEKIYA @USAGI : Don't assign a same IPv6
34 * address on a same interface.
35 * YOSHIFUJI Hideaki @USAGI : ARCnet support
36 * YOSHIFUJI Hideaki @USAGI : convert /proc/net/if_inet6 to
37 * seq_file.
b1cacb68
YH
38 * YOSHIFUJI Hideaki @USAGI : improved source address
39 * selection; consider scope,
40 * status etc.
1da177e4
LT
41 */
42
43#include <linux/config.h>
44#include <linux/errno.h>
45#include <linux/types.h>
46#include <linux/socket.h>
47#include <linux/sockios.h>
48#include <linux/sched.h>
49#include <linux/net.h>
50#include <linux/in6.h>
51#include <linux/netdevice.h>
52#include <linux/if_arp.h>
53#include <linux/if_arcnet.h>
54#include <linux/if_infiniband.h>
55#include <linux/route.h>
56#include <linux/inetdevice.h>
57#include <linux/init.h>
58#ifdef CONFIG_SYSCTL
59#include <linux/sysctl.h>
60#endif
61#include <linux/delay.h>
62#include <linux/notifier.h>
543537bd 63#include <linux/string.h>
1da177e4
LT
64
65#include <net/sock.h>
66#include <net/snmp.h>
67
68#include <net/ipv6.h>
69#include <net/protocol.h>
70#include <net/ndisc.h>
71#include <net/ip6_route.h>
72#include <net/addrconf.h>
73#include <net/tcp.h>
74#include <net/ip.h>
75#include <linux/if_tunnel.h>
76#include <linux/rtnetlink.h>
77
78#ifdef CONFIG_IPV6_PRIVACY
79#include <linux/random.h>
80#include <linux/crypto.h>
378f058c 81#include <linux/scatterlist.h>
1da177e4
LT
82#endif
83
84#include <asm/uaccess.h>
85
86#include <linux/proc_fs.h>
87#include <linux/seq_file.h>
88
89/* Set to 3 to get tracing... */
90#define ACONF_DEBUG 2
91
92#if ACONF_DEBUG >= 3
93#define ADBG(x) printk x
94#else
95#define ADBG(x)
96#endif
97
98#define INFINITY_LIFE_TIME 0xFFFFFFFF
99#define TIME_DELTA(a,b) ((unsigned long)((long)(a) - (long)(b)))
100
101#ifdef CONFIG_SYSCTL
102static void addrconf_sysctl_register(struct inet6_dev *idev, struct ipv6_devconf *p);
103static void addrconf_sysctl_unregister(struct ipv6_devconf *p);
104#endif
105
106#ifdef CONFIG_IPV6_PRIVACY
107static int __ipv6_regen_rndid(struct inet6_dev *idev);
108static int __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr);
109static void ipv6_regen_rndid(unsigned long data);
110
111static int desync_factor = MAX_DESYNC_FACTOR * HZ;
112static struct crypto_tfm *md5_tfm;
113static DEFINE_SPINLOCK(md5_tfm_lock);
114#endif
115
116static int ipv6_count_addresses(struct inet6_dev *idev);
117
118/*
119 * Configured unicast address hash table
120 */
121static struct inet6_ifaddr *inet6_addr_lst[IN6_ADDR_HSIZE];
122static DEFINE_RWLOCK(addrconf_hash_lock);
123
124/* Protects inet6 devices */
125DEFINE_RWLOCK(addrconf_lock);
126
127static void addrconf_verify(unsigned long);
128
8d06afab 129static DEFINE_TIMER(addr_chk_timer, addrconf_verify, 0, 0);
1da177e4
LT
130static DEFINE_SPINLOCK(addrconf_verify_lock);
131
132static void addrconf_join_anycast(struct inet6_ifaddr *ifp);
133static void addrconf_leave_anycast(struct inet6_ifaddr *ifp);
134
135static int addrconf_ifdown(struct net_device *dev, int how);
136
e431b8c0 137static void addrconf_dad_start(struct inet6_ifaddr *ifp, u32 flags);
1da177e4
LT
138static void addrconf_dad_timer(unsigned long data);
139static void addrconf_dad_completed(struct inet6_ifaddr *ifp);
140static void addrconf_rs_timer(unsigned long data);
141static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
142static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
143
144static void inet6_prefix_notify(int event, struct inet6_dev *idev,
145 struct prefix_info *pinfo);
146static int ipv6_chk_same_addr(const struct in6_addr *addr, struct net_device *dev);
147
148static struct notifier_block *inet6addr_chain;
149
150struct ipv6_devconf ipv6_devconf = {
151 .forwarding = 0,
152 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
153 .mtu6 = IPV6_MIN_MTU,
154 .accept_ra = 1,
155 .accept_redirects = 1,
156 .autoconf = 1,
157 .force_mld_version = 0,
158 .dad_transmits = 1,
159 .rtr_solicits = MAX_RTR_SOLICITATIONS,
160 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
161 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
162#ifdef CONFIG_IPV6_PRIVACY
163 .use_tempaddr = 0,
164 .temp_valid_lft = TEMP_VALID_LIFETIME,
165 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
166 .regen_max_retry = REGEN_MAX_RETRY,
167 .max_desync_factor = MAX_DESYNC_FACTOR,
168#endif
169 .max_addresses = IPV6_MAX_ADDRESSES,
170};
171
172static struct ipv6_devconf ipv6_devconf_dflt = {
173 .forwarding = 0,
174 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
175 .mtu6 = IPV6_MIN_MTU,
176 .accept_ra = 1,
177 .accept_redirects = 1,
178 .autoconf = 1,
179 .dad_transmits = 1,
180 .rtr_solicits = MAX_RTR_SOLICITATIONS,
181 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
182 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
183#ifdef CONFIG_IPV6_PRIVACY
184 .use_tempaddr = 0,
185 .temp_valid_lft = TEMP_VALID_LIFETIME,
186 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
187 .regen_max_retry = REGEN_MAX_RETRY,
188 .max_desync_factor = MAX_DESYNC_FACTOR,
189#endif
190 .max_addresses = IPV6_MAX_ADDRESSES,
191};
192
193/* IPv6 Wildcard Address and Loopback Address defined by RFC2553 */
194#if 0
195const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
196#endif
197const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
198
b1cacb68
YH
199#define IPV6_ADDR_SCOPE_TYPE(scope) ((scope) << 16)
200
201static inline unsigned ipv6_addr_scope2type(unsigned scope)
202{
203 switch(scope) {
204 case IPV6_ADDR_SCOPE_NODELOCAL:
205 return (IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_NODELOCAL) |
206 IPV6_ADDR_LOOPBACK);
207 case IPV6_ADDR_SCOPE_LINKLOCAL:
208 return (IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_LINKLOCAL) |
209 IPV6_ADDR_LINKLOCAL);
210 case IPV6_ADDR_SCOPE_SITELOCAL:
211 return (IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_SITELOCAL) |
212 IPV6_ADDR_SITELOCAL);
213 }
214 return IPV6_ADDR_SCOPE_TYPE(scope);
215}
216
217int __ipv6_addr_type(const struct in6_addr *addr)
1da177e4 218{
1da177e4
LT
219 u32 st;
220
221 st = addr->s6_addr32[0];
222
1da177e4 223 /* Consider all addresses with the first three bits different of
b1cacb68 224 000 and 111 as unicasts.
1da177e4
LT
225 */
226 if ((st & htonl(0xE0000000)) != htonl(0x00000000) &&
227 (st & htonl(0xE0000000)) != htonl(0xE0000000))
b1cacb68
YH
228 return (IPV6_ADDR_UNICAST |
229 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL));
1da177e4 230
b1cacb68
YH
231 if ((st & htonl(0xFF000000)) == htonl(0xFF000000)) {
232 /* multicast */
233 /* addr-select 3.1 */
234 return (IPV6_ADDR_MULTICAST |
235 ipv6_addr_scope2type(IPV6_ADDR_MC_SCOPE(addr)));
236 }
237
238 if ((st & htonl(0xFFC00000)) == htonl(0xFE800000))
239 return (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_UNICAST |
240 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_LINKLOCAL)); /* addr-select 3.1 */
1da177e4 241 if ((st & htonl(0xFFC00000)) == htonl(0xFEC00000))
b1cacb68
YH
242 return (IPV6_ADDR_SITELOCAL | IPV6_ADDR_UNICAST |
243 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_SITELOCAL)); /* addr-select 3.1 */
1da177e4
LT
244
245 if ((addr->s6_addr32[0] | addr->s6_addr32[1]) == 0) {
246 if (addr->s6_addr32[2] == 0) {
247 if (addr->s6_addr32[3] == 0)
248 return IPV6_ADDR_ANY;
249
250 if (addr->s6_addr32[3] == htonl(0x00000001))
b1cacb68
YH
251 return (IPV6_ADDR_LOOPBACK | IPV6_ADDR_UNICAST |
252 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_LINKLOCAL)); /* addr-select 3.4 */
1da177e4 253
b1cacb68
YH
254 return (IPV6_ADDR_COMPATv4 | IPV6_ADDR_UNICAST |
255 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL)); /* addr-select 3.3 */
1da177e4
LT
256 }
257
258 if (addr->s6_addr32[2] == htonl(0x0000ffff))
b1cacb68
YH
259 return (IPV6_ADDR_MAPPED |
260 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL)); /* addr-select 3.3 */
1da177e4
LT
261 }
262
b1cacb68
YH
263 return (IPV6_ADDR_RESERVED |
264 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL)); /* addr-select 3.4 */
1da177e4
LT
265}
266
267static void addrconf_del_timer(struct inet6_ifaddr *ifp)
268{
269 if (del_timer(&ifp->timer))
270 __in6_ifa_put(ifp);
271}
272
273enum addrconf_timer_t
274{
275 AC_NONE,
276 AC_DAD,
277 AC_RS,
278};
279
280static void addrconf_mod_timer(struct inet6_ifaddr *ifp,
281 enum addrconf_timer_t what,
282 unsigned long when)
283{
284 if (!del_timer(&ifp->timer))
285 in6_ifa_hold(ifp);
286
287 switch (what) {
288 case AC_DAD:
289 ifp->timer.function = addrconf_dad_timer;
290 break;
291 case AC_RS:
292 ifp->timer.function = addrconf_rs_timer;
293 break;
294 default:;
295 }
296 ifp->timer.expires = jiffies + when;
297 add_timer(&ifp->timer);
298}
299
300/* Nobody refers to this device, we may destroy it. */
301
302void in6_dev_finish_destroy(struct inet6_dev *idev)
303{
304 struct net_device *dev = idev->dev;
305 BUG_TRAP(idev->addr_list==NULL);
306 BUG_TRAP(idev->mc_list==NULL);
307#ifdef NET_REFCNT_DEBUG
308 printk(KERN_DEBUG "in6_dev_finish_destroy: %s\n", dev ? dev->name : "NIL");
309#endif
310 dev_put(dev);
311 if (!idev->dead) {
312 printk("Freeing alive inet6 device %p\n", idev);
313 return;
314 }
315 snmp6_free_dev(idev);
316 kfree(idev);
317}
318
319static struct inet6_dev * ipv6_add_dev(struct net_device *dev)
320{
321 struct inet6_dev *ndev;
322
323 ASSERT_RTNL();
324
325 if (dev->mtu < IPV6_MIN_MTU)
326 return NULL;
327
328 ndev = kmalloc(sizeof(struct inet6_dev), GFP_KERNEL);
329
330 if (ndev) {
331 memset(ndev, 0, sizeof(struct inet6_dev));
332
333 rwlock_init(&ndev->lock);
334 ndev->dev = dev;
335 memcpy(&ndev->cnf, &ipv6_devconf_dflt, sizeof(ndev->cnf));
336 ndev->cnf.mtu6 = dev->mtu;
337 ndev->cnf.sysctl = NULL;
338 ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl);
339 if (ndev->nd_parms == NULL) {
340 kfree(ndev);
341 return NULL;
342 }
343 /* We refer to the device */
344 dev_hold(dev);
345
346 if (snmp6_alloc_dev(ndev) < 0) {
347 ADBG((KERN_WARNING
348 "%s(): cannot allocate memory for statistics; dev=%s.\n",
349 __FUNCTION__, dev->name));
350 neigh_parms_release(&nd_tbl, ndev->nd_parms);
351 ndev->dead = 1;
352 in6_dev_finish_destroy(ndev);
353 return NULL;
354 }
355
356 if (snmp6_register_dev(ndev) < 0) {
357 ADBG((KERN_WARNING
358 "%s(): cannot create /proc/net/dev_snmp6/%s\n",
359 __FUNCTION__, dev->name));
360 neigh_parms_release(&nd_tbl, ndev->nd_parms);
361 ndev->dead = 1;
362 in6_dev_finish_destroy(ndev);
363 return NULL;
364 }
365
366 /* One reference from device. We must do this before
367 * we invoke __ipv6_regen_rndid().
368 */
369 in6_dev_hold(ndev);
370
371#ifdef CONFIG_IPV6_PRIVACY
372 get_random_bytes(ndev->rndid, sizeof(ndev->rndid));
373 get_random_bytes(ndev->entropy, sizeof(ndev->entropy));
374 init_timer(&ndev->regen_timer);
375 ndev->regen_timer.function = ipv6_regen_rndid;
376 ndev->regen_timer.data = (unsigned long) ndev;
377 if ((dev->flags&IFF_LOOPBACK) ||
378 dev->type == ARPHRD_TUNNEL ||