blob: ca7ecf2f3e82b90228bf0341cf3d1af14e9609c6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * IPv6 Address [auto]configuration
3 * Linux INET6 implementation
4 *
5 * Authors:
6 * Pedro Roque <roque@di.fc.ul.pt>
7 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
8 *
9 * $Id: addrconf.c,v 1.69 2001/10/31 21:55:54 davem Exp $
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version
14 * 2 of the License, or (at your option) any later version.
15 */
16
17/*
18 * Changes:
19 *
20 * Janos Farkas : delete timer on ifdown
21 * <chexum@bankinf.banki.hu>
22 * Andi Kleen : kill double kfree on module
23 * unload.
24 * Maciej W. Rozycki : FDDI support
25 * sekiya@USAGI : Don't send too many RS
26 * packets.
27 * yoshfuji@USAGI : Fixed interval between DAD
28 * packets.
29 * YOSHIFUJI Hideaki @USAGI : improved accuracy of
30 * address validation timer.
31 * YOSHIFUJI Hideaki @USAGI : Privacy Extensions (RFC3041)
32 * support.
33 * Yuji SEKIYA @USAGI : Don't assign a same IPv6
34 * address on a same interface.
35 * YOSHIFUJI Hideaki @USAGI : ARCnet support
36 * YOSHIFUJI Hideaki @USAGI : convert /proc/net/if_inet6 to
37 * seq_file.
YOSHIFUJI Hideakib1cacb62005-11-08 09:38:12 -080038 * YOSHIFUJI Hideaki @USAGI : improved source address
39 * selection; consider scope,
40 * status etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 */
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/errno.h>
44#include <linux/types.h>
45#include <linux/socket.h>
46#include <linux/sockios.h>
47#include <linux/sched.h>
48#include <linux/net.h>
49#include <linux/in6.h>
50#include <linux/netdevice.h>
Thomas Graf18237302006-08-04 23:04:54 -070051#include <linux/if_addr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <linux/if_arp.h>
53#include <linux/if_arcnet.h>
54#include <linux/if_infiniband.h>
55#include <linux/route.h>
56#include <linux/inetdevice.h>
57#include <linux/init.h>
58#ifdef CONFIG_SYSCTL
59#include <linux/sysctl.h>
60#endif
Randy Dunlap4fc268d2006-01-11 12:17:47 -080061#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#include <linux/delay.h>
63#include <linux/notifier.h>
Paulo Marques543537b2005-06-23 00:09:02 -070064#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66#include <net/sock.h>
67#include <net/snmp.h>
68
69#include <net/ipv6.h>
70#include <net/protocol.h>
71#include <net/ndisc.h>
72#include <net/ip6_route.h>
73#include <net/addrconf.h>
74#include <net/tcp.h>
75#include <net/ip.h>
Thomas Graf5d620262006-08-15 00:35:02 -070076#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070077#include <linux/if_tunnel.h>
78#include <linux/rtnetlink.h>
79
80#ifdef CONFIG_IPV6_PRIVACY
81#include <linux/random.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070082#endif
83
84#include <asm/uaccess.h>
85
86#include <linux/proc_fs.h>
87#include <linux/seq_file.h>
88
89/* Set to 3 to get tracing... */
90#define ACONF_DEBUG 2
91
92#if ACONF_DEBUG >= 3
93#define ADBG(x) printk x
94#else
95#define ADBG(x)
96#endif
97
98#define INFINITY_LIFE_TIME 0xFFFFFFFF
99#define TIME_DELTA(a,b) ((unsigned long)((long)(a) - (long)(b)))
100
101#ifdef CONFIG_SYSCTL
102static void addrconf_sysctl_register(struct inet6_dev *idev, struct ipv6_devconf *p);
103static void addrconf_sysctl_unregister(struct ipv6_devconf *p);
104#endif
105
106#ifdef CONFIG_IPV6_PRIVACY
107static int __ipv6_regen_rndid(struct inet6_dev *idev);
108static int __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr);
109static void ipv6_regen_rndid(unsigned long data);
110
111static int desync_factor = MAX_DESYNC_FACTOR * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112#endif
113
114static int ipv6_count_addresses(struct inet6_dev *idev);
115
116/*
117 * Configured unicast address hash table
118 */
119static struct inet6_ifaddr *inet6_addr_lst[IN6_ADDR_HSIZE];
120static DEFINE_RWLOCK(addrconf_hash_lock);
121
122/* Protects inet6 devices */
123DEFINE_RWLOCK(addrconf_lock);
124
125static void addrconf_verify(unsigned long);
126
Ingo Molnar8d06afa2005-09-09 13:10:40 -0700127static DEFINE_TIMER(addr_chk_timer, addrconf_verify, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128static DEFINE_SPINLOCK(addrconf_verify_lock);
129
130static void addrconf_join_anycast(struct inet6_ifaddr *ifp);
131static void addrconf_leave_anycast(struct inet6_ifaddr *ifp);
132
133static int addrconf_ifdown(struct net_device *dev, int how);
134
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -0700135static void addrconf_dad_start(struct inet6_ifaddr *ifp, u32 flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136static void addrconf_dad_timer(unsigned long data);
137static void addrconf_dad_completed(struct inet6_ifaddr *ifp);
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +0900138static void addrconf_dad_run(struct inet6_dev *idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139static void addrconf_rs_timer(unsigned long data);
140static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
141static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
142
143static void inet6_prefix_notify(int event, struct inet6_dev *idev,
144 struct prefix_info *pinfo);
145static int ipv6_chk_same_addr(const struct in6_addr *addr, struct net_device *dev);
146
Alan Sterne041c682006-03-27 01:16:30 -0800147static ATOMIC_NOTIFIER_HEAD(inet6addr_chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Brian Haleyab32ea52006-09-22 14:15:41 -0700149struct ipv6_devconf ipv6_devconf __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 .forwarding = 0,
151 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
152 .mtu6 = IPV6_MIN_MTU,
153 .accept_ra = 1,
154 .accept_redirects = 1,
155 .autoconf = 1,
156 .force_mld_version = 0,
157 .dad_transmits = 1,
158 .rtr_solicits = MAX_RTR_SOLICITATIONS,
159 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
160 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
161#ifdef CONFIG_IPV6_PRIVACY
162 .use_tempaddr = 0,
163 .temp_valid_lft = TEMP_VALID_LIFETIME,
164 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
165 .regen_max_retry = REGEN_MAX_RETRY,
166 .max_desync_factor = MAX_DESYNC_FACTOR,
167#endif
168 .max_addresses = IPV6_MAX_ADDRESSES,
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -0800169 .accept_ra_defrtr = 1,
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -0800170 .accept_ra_pinfo = 1,
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800171#ifdef CONFIG_IPV6_ROUTER_PREF
172 .accept_ra_rtr_pref = 1,
YOSHIFUJI Hideaki52e16352006-03-20 17:05:47 -0800173 .rtr_probe_interval = 60 * HZ,
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -0800174#ifdef CONFIG_IPV6_ROUTE_INFO
175 .accept_ra_rt_info_max_plen = 0,
176#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800177#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178};
179
Brian Haleyab32ea52006-09-22 14:15:41 -0700180static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 .forwarding = 0,
182 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
183 .mtu6 = IPV6_MIN_MTU,
184 .accept_ra = 1,
185 .accept_redirects = 1,
186 .autoconf = 1,
187 .dad_transmits = 1,
188 .rtr_solicits = MAX_RTR_SOLICITATIONS,
189 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
190 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
191#ifdef CONFIG_IPV6_PRIVACY
192 .use_tempaddr = 0,
193 .temp_valid_lft = TEMP_VALID_LIFETIME,
194 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
195 .regen_max_retry = REGEN_MAX_RETRY,
196 .max_desync_factor = MAX_DESYNC_FACTOR,
197#endif
198 .max_addresses = IPV6_MAX_ADDRESSES,
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -0800199 .accept_ra_defrtr = 1,
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -0800200 .accept_ra_pinfo = 1,
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800201#ifdef CONFIG_IPV6_ROUTER_PREF
202 .accept_ra_rtr_pref = 1,
YOSHIFUJI Hideaki52e16352006-03-20 17:05:47 -0800203 .rtr_probe_interval = 60 * HZ,
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -0800204#ifdef CONFIG_IPV6_ROUTE_INFO
205 .accept_ra_rt_info_max_plen = 0,
206#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800207#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208};
209
210/* IPv6 Wildcard Address and Loopback Address defined by RFC2553 */
211#if 0
212const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
213#endif
214const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
215
YOSHIFUJI Hideakib1cacb62005-11-08 09:38:12 -0800216#define IPV6_ADDR_SCOPE_TYPE(scope) ((scope) << 16)
217
218static inline unsigned ipv6_addr_scope2type(unsigned scope)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219{
YOSHIFUJI Hideakib1cacb62005-11-08 09:38:12 -0800220 switch(scope) {
221 case IPV6_ADDR_SCOPE_NODELOCAL:
222 return (IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_NODELOCAL) |
223 IPV6_ADDR_LOOPBACK);
224 case IPV6_ADDR_SCOPE_LINKLOCAL:
225 return (IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_LINKLOCAL) |
226 IPV6_ADDR_LINKLOCAL);
227 case IPV6_ADDR_SCOPE_SITELOCAL:
228 return (IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_SITELOCAL) |
229 IPV6_ADDR_SITELOCAL);
230 }
231 return IPV6_ADDR_SCOPE_TYPE(scope);
232}
233
234int __ipv6_addr_type(const struct in6_addr *addr)
235{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 u32 st;
237
238 st = addr->s6_addr32[0];
239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 /* Consider all addresses with the first three bits different of
YOSHIFUJI Hideakib1cacb62005-11-08 09:38:12 -0800241 000 and 111 as unicasts.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 */
243 if ((st & htonl(0xE0000000)) != htonl(0x00000000) &&
244 (st & htonl(0xE0000000)) != htonl(0xE0000000))
YOSHIFUJI Hideakib1cacb62005-11-08 09:38:12 -0800245 return (IPV6_ADDR_UNICAST |
246 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
YOSHIFUJI Hideakib1cacb62005-11-08 09:38:12 -0800248 if ((st & htonl(0xFF000000)) == htonl(0xFF000000)) {
249 /* multicast */
250 /* addr-select 3.1 */
251 return (IPV6_ADDR_MULTICAST |
252 ipv6_addr_scope2type(IPV6_ADDR_MC_SCOPE(addr)));
253 }
254
255 if ((st & htonl(0xFFC00000)) == htonl(0xFE800000))
256 return (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_UNICAST |
257 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_LINKLOCAL)); /* addr-select 3.1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 if ((st & htonl(0xFFC00000)) == htonl(0xFEC00000))
YOSHIFUJI Hideakib1cacb62005-11-08 09:38:12 -0800259 return (IPV6_ADDR_SITELOCAL | IPV6_ADDR_UNICAST |
260 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_SITELOCAL)); /* addr-select 3.1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
262 if ((addr->s6_addr32[0] | addr->s6_addr32[1]) == 0) {
263 if (addr->s6_addr32[2] == 0) {
264 if (addr->s6_addr32[3] == 0)
265 return IPV6_ADDR_ANY;
266
267 if (addr->s6_addr32[3] == htonl(0x00000001))
YOSHIFUJI Hideakib1cacb62005-11-08 09:38:12 -0800268 return (IPV6_ADDR_LOOPBACK | IPV6_ADDR_UNICAST |
269 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_LINKLOCAL)); /* addr-select 3.4 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
YOSHIFUJI Hideakib1cacb62005-11-08 09:38:12 -0800271 return (IPV6_ADDR_COMPATv4 | IPV6_ADDR_UNICAST |
272 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL)); /* addr-select 3.3 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 }
274
275 if (addr->s6_addr32[2] == htonl(0x0000ffff))
YOSHIFUJI Hideakib1cacb62005-11-08 09:38:12 -0800276 return (IPV6_ADDR_MAPPED |
277 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL)); /* addr-select 3.3 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 }
279
YOSHIFUJI Hideakib1cacb62005-11-08 09:38:12 -0800280 return (IPV6_ADDR_RESERVED |
281 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL)); /* addr-select 3.4 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282}
283
284static void addrconf_del_timer(struct inet6_ifaddr *ifp)
285{
286 if (del_timer(&ifp->timer))
287 __in6_ifa_put(ifp);
288}
289
290enum addrconf_timer_t
291{
292 AC_NONE,
293 AC_DAD,
294 AC_RS,
295};
296
297static void addrconf_mod_timer(struct inet6_ifaddr *ifp,
298 enum addrconf_timer_t what,
299 unsigned long when)
300{
301 if (!del_timer(&ifp->timer))
302 in6_ifa_hold(ifp);
303
304 switch (what) {
305 case AC_DAD:
306 ifp->timer.function = addrconf_dad_timer;
307 break;
308 case AC_RS:
309 ifp->timer.function = addrconf_rs_timer;
310 break;
311 default:;
312 }
313 ifp->timer.expires = jiffies + when;
314 add_timer(&ifp->timer);
315}
316
317/* Nobody refers to this device, we may destroy it. */
318
319void in6_dev_finish_destroy(struct inet6_dev *idev)
320{
321 struct net_device *dev = idev->dev;
322 BUG_TRAP(idev->addr_list==NULL);
323 BUG_TRAP(idev->mc_list==NULL);
324#ifdef NET_REFCNT_DEBUG
325 printk(KERN_DEBUG "in6_dev_finish_destroy: %s\n", dev ? dev->name : "NIL");
326#endif
327 dev_put(dev);
328 if (!idev->dead) {
329 printk("Freeing alive inet6 device %p\n", idev);
330 return;
331 }
332 snmp6_free_dev(idev);
333 kfree(idev);
334}
335
336static struct inet6_dev * ipv6_add_dev(struct net_device *dev)
337{
338 struct inet6_dev *ndev;
339
340 ASSERT_RTNL();
341
342 if (dev->mtu < IPV6_MIN_MTU)
343 return NULL;
344
Ingo Oeser322f74a2006-03-20 23:01:47 -0800345 ndev = kzalloc(sizeof(struct inet6_dev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
Ingo Oeser322f74a2006-03-20 23:01:47 -0800347 if (ndev == NULL)
348 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Ingo Oeser322f74a2006-03-20 23:01:47 -0800350 rwlock_init(&ndev->lock);
351 ndev->dev = dev;
352 memcpy(&ndev->cnf, &ipv6_devconf_dflt, sizeof(ndev->cnf));
353 ndev->cnf.mtu6 = dev->mtu;
354 ndev->cnf.sysctl = NULL;
355 ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl);
356 if (ndev->nd_parms == NULL) {
357 kfree(ndev);
358 return NULL;
359 }
360 /* We refer to the device */
361 dev_hold(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
Ingo Oeser322f74a2006-03-20 23:01:47 -0800363 if (snmp6_alloc_dev(ndev) < 0) {
364 ADBG((KERN_WARNING
365 "%s(): cannot allocate memory for statistics; dev=%s.\n",
366 __FUNCTION__, dev->name));
367 neigh_parms_release(&nd_tbl, ndev->nd_parms);
368 ndev->dead = 1;
369 in6_dev_finish_destroy(ndev);
370 return NULL;
371 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Ingo Oeser322f74a2006-03-20 23:01:47 -0800373 if (snmp6_register_dev(ndev) < 0) {
374 ADBG((KERN_WARNING
375 "%s(): cannot create /proc/net/dev_snmp6/%s\n",
376 __FUNCTION__, dev->name));
377 neigh_parms_release(&nd_tbl, ndev->nd_parms);
378 ndev->dead = 1;
379 in6_dev_finish_destroy(ndev);
380 return NULL;
381 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
Ingo Oeser322f74a2006-03-20 23:01:47 -0800383 /* One reference from device. We must do this before
384 * we invoke __ipv6_regen_rndid().
385 */
386 in6_dev_hold(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
388#ifdef CONFIG_IPV6_PRIVACY
Ingo Oeser322f74a2006-03-20 23:01:47 -0800389 init_timer(&ndev->regen_timer);
390 ndev->regen_timer.function = ipv6_regen_rndid;
391 ndev->regen_timer.data = (unsigned long) ndev;
392 if ((dev->flags&IFF_LOOPBACK) ||
393 dev->type == ARPHRD_TUNNEL ||
394 dev->type == ARPHRD_NONE ||
395 dev->type == ARPHRD_SIT) {
396 printk(KERN_INFO
397 "%s: Disabled Privacy Extensions\n",
398 dev->name);
399 ndev->cnf.use_tempaddr = -1;
400 } else {
401 in6_dev_hold(ndev);
402 ipv6_regen_rndid((unsigned long) ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 }
Ingo Oeser322f74a2006-03-20 23:01:47 -0800404#endif
405
406 if (netif_carrier_ok(dev))
407 ndev->if_flags |= IF_READY;
408
409 write_lock_bh(&addrconf_lock);
410 dev->ip6_ptr = ndev;
411 write_unlock_bh(&addrconf_lock);
412
413 ipv6_mc_init_dev(ndev);
414 ndev->tstamp = jiffies;
415#ifdef CONFIG_SYSCTL
416 neigh_sysctl_register(dev, ndev->nd_parms, NET_IPV6,
417 NET_IPV6_NEIGH, "ipv6",
418 &ndisc_ifinfo_sysctl_change,
419 NULL);
420 addrconf_sysctl_register(ndev, &ndev->cnf);
421#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 return ndev;
423}
424
425static struct inet6_dev * ipv6_find_idev(struct net_device *dev)
426{
427 struct inet6_dev *idev;
428
429 ASSERT_RTNL();
430
431 if ((idev = __in6_dev_get(dev)) == NULL) {
432 if ((idev = ipv6_add_dev(dev)) == NULL)
433 return NULL;
434 }
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +0900435
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 if (dev->flags&IFF_UP)
437 ipv6_mc_up(idev);
438 return idev;
439}
440
441#ifdef CONFIG_SYSCTL
442static void dev_forward_change(struct inet6_dev *idev)
443{
444 struct net_device *dev;
445 struct inet6_ifaddr *ifa;
446 struct in6_addr addr;
447
448 if (!idev)
449 return;
450 dev = idev->dev;
451 if (dev && (dev->flags & IFF_MULTICAST)) {
452 ipv6_addr_all_routers(&addr);
453
454 if (idev->cnf.forwarding)
455 ipv6_dev_mc_inc(dev, &addr);
456 else
457 ipv6_dev_mc_dec(dev, &addr);
458 }
459 for (ifa=idev->addr_list; ifa; ifa=ifa->if_next) {
460 if (idev->cnf.forwarding)
461 addrconf_join_anycast(ifa);
462 else
463 addrconf_leave_anycast(ifa);
464 }
465}
466
467
468static void addrconf_forward_change(void)
469{
470 struct net_device *dev;
471 struct inet6_dev *idev;
472
473 read_lock(&dev_base_lock);
474 for (dev=dev_base; dev; dev=dev->next) {
475 read_lock(&addrconf_lock);
476 idev = __in6_dev_get(dev);
477 if (idev) {
478 int changed = (!idev->cnf.forwarding) ^ (!ipv6_devconf.forwarding);
479 idev->cnf.forwarding = ipv6_devconf.forwarding;
480 if (changed)
481 dev_forward_change(idev);
482 }
483 read_unlock(&addrconf_lock);
484 }
485 read_unlock(&dev_base_lock);
486}
487#endif
488
489/* Nobody refers to this ifaddr, destroy it */
490
491void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp)
492{
493 BUG_TRAP(ifp->if_next==NULL);
494 BUG_TRAP(ifp->lst_next==NULL);
495#ifdef NET_REFCNT_DEBUG
496 printk(KERN_DEBUG "inet6_ifa_finish_destroy\n");
497#endif
498
499 in6_dev_put(ifp->idev);
500
501 if (del_timer(&ifp->timer))
502 printk("Timer is still running, when freeing ifa=%p\n", ifp);
503
504 if (!ifp->dead) {
505 printk("Freeing alive inet6 address %p\n", ifp);
506 return;
507 }
508 dst_release(&ifp->rt->u.dst);
509
510 kfree(ifp);
511}
512
Brian Haleye55ffac2006-07-10 15:25:51 -0700513static void
514ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp)
515{
516 struct inet6_ifaddr *ifa, **ifap;
YOSHIFUJI Hideaki8a6ce0c2006-07-11 13:05:30 -0700517 int ifp_scope = ipv6_addr_src_scope(&ifp->addr);
Brian Haleye55ffac2006-07-10 15:25:51 -0700518
519 /*
520 * Each device address list is sorted in order of scope -
521 * global before linklocal.
522 */
523 for (ifap = &idev->addr_list; (ifa = *ifap) != NULL;
524 ifap = &ifa->if_next) {
YOSHIFUJI Hideaki8a6ce0c2006-07-11 13:05:30 -0700525 if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr))
Brian Haleye55ffac2006-07-10 15:25:51 -0700526 break;
527 }
528
529 ifp->if_next = *ifap;
530 *ifap = ifp;
531}
532
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533/* On success it returns ifp with increased reference count */
534
535static struct inet6_ifaddr *
536ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr, int pfxlen,
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -0700537 int scope, u32 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{
539 struct inet6_ifaddr *ifa = NULL;
540 struct rt6_info *rt;
541 int hash;
542 int err = 0;
543
544 read_lock_bh(&addrconf_lock);
545 if (idev->dead) {
546 err = -ENODEV; /*XXX*/
547 goto out2;
548 }
549
550 write_lock(&addrconf_hash_lock);
551
552 /* Ignore adding duplicate addresses on an interface */
553 if (ipv6_chk_same_addr(addr, idev->dev)) {
554 ADBG(("ipv6_add_addr: already assigned\n"));
555 err = -EEXIST;
556 goto out;
557 }
558
Ingo Oeser322f74a2006-03-20 23:01:47 -0800559 ifa = kzalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
561 if (ifa == NULL) {
562 ADBG(("ipv6_add_addr: malloc failed\n"));
563 err = -ENOBUFS;
564 goto out;
565 }
566
567 rt = addrconf_dst_alloc(idev, addr, 0);
568 if (IS_ERR(rt)) {
569 err = PTR_ERR(rt);
570 goto out;
571 }
572
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 ipv6_addr_copy(&ifa->addr, addr);
574
575 spin_lock_init(&ifa->lock);
576 init_timer(&ifa->timer);
577 ifa->timer.data = (unsigned long) ifa;
578 ifa->scope = scope;
579 ifa->prefix_len = pfxlen;
580 ifa->flags = flags | IFA_F_TENTATIVE;
581 ifa->cstamp = ifa->tstamp = jiffies;
582
Keir Fraser57f5f542006-08-29 02:43:49 -0700583 ifa->rt = rt;
584
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 ifa->idev = idev;
586 in6_dev_hold(idev);
587 /* For caller */
588 in6_ifa_hold(ifa);
589
590 /* Add to big hash table */
591 hash = ipv6_addr_hash(addr);
592
593 ifa->lst_next = inet6_addr_lst[hash];
594 inet6_addr_lst[hash] = ifa;
595 in6_ifa_hold(ifa);
596 write_unlock(&addrconf_hash_lock);
597
598 write_lock(&idev->lock);
599 /* Add to inet6_dev unicast addr list. */
Brian Haleye55ffac2006-07-10 15:25:51 -0700600 ipv6_link_dev_addr(idev, ifa);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
602#ifdef CONFIG_IPV6_PRIVACY
603 if (ifa->flags&IFA_F_TEMPORARY) {
604 ifa->tmp_next = idev->tempaddr_list;
605 idev->tempaddr_list = ifa;
606 in6_ifa_hold(ifa);
607 }
608#endif
609
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 in6_ifa_hold(ifa);
611 write_unlock(&idev->lock);
612out2:
613 read_unlock_bh(&addrconf_lock);
614
YOSHIFUJI Hideakifd928332005-04-19 22:27:09 -0700615 if (likely(err == 0))
Alan Sterne041c682006-03-27 01:16:30 -0800616 atomic_notifier_call_chain(&inet6addr_chain, NETDEV_UP, ifa);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 else {
618 kfree(ifa);
619 ifa = ERR_PTR(err);
620 }
621
622 return ifa;
623out:
624 write_unlock(&addrconf_hash_lock);
625 goto out2;
626}
627
628/* This function wants to get referenced ifp and releases it before return */
629
630static void ipv6_del_addr(struct inet6_ifaddr *ifp)
631{
632 struct inet6_ifaddr *ifa, **ifap;
633 struct inet6_dev *idev = ifp->idev;
634 int hash;
635 int deleted = 0, onlink = 0;
636 unsigned long expires = jiffies;
637
638 hash = ipv6_addr_hash(&ifp->addr);
639
640 ifp->dead = 1;
641
642 write_lock_bh(&addrconf_hash_lock);
643 for (ifap = &inet6_addr_lst[hash]; (ifa=*ifap) != NULL;
644 ifap = &ifa->lst_next) {
645 if (ifa == ifp) {
646 *ifap = ifa->lst_next;
647 __in6_ifa_put(ifp);
648 ifa->lst_next = NULL;
649 break;
650 }
651 }
652 write_unlock_bh(&addrconf_hash_lock);
653
654 write_lock_bh(&idev->lock);
655#ifdef CONFIG_IPV6_PRIVACY
656 if (ifp->flags&IFA_F_TEMPORARY) {
657 for (ifap = &idev->tempaddr_list; (ifa=*ifap) != NULL;
658 ifap = &ifa->tmp_next) {
659 if (ifa == ifp) {
660 *ifap = ifa->tmp_next;
661 if (ifp->ifpub) {
662 in6_ifa_put(ifp->ifpub);
663 ifp->ifpub = NULL;
664 }
665 __in6_ifa_put(ifp);
666 ifa->tmp_next = NULL;
667 break;
668 }
669 }
670 }
671#endif
672
Kristian Slavov1d142802005-12-21 18:47:24 -0800673 for (ifap = &idev->addr_list; (ifa=*ifap) != NULL;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 if (ifa == ifp) {
675 *ifap = ifa->if_next;
676 __in6_ifa_put(ifp);
677 ifa->if_next = NULL;
678 if (!(ifp->flags & IFA_F_PERMANENT) || onlink > 0)
679 break;
680 deleted = 1;
Kristian Slavov1d142802005-12-21 18:47:24 -0800681 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 } else if (ifp->flags & IFA_F_PERMANENT) {
683 if (ipv6_prefix_equal(&ifa->addr, &ifp->addr,
684 ifp->prefix_len)) {
685 if (ifa->flags & IFA_F_PERMANENT) {
686 onlink = 1;
687 if (deleted)
688 break;
689 } else {
690 unsigned long lifetime;
691
692 if (!onlink)
693 onlink = -1;
694
695 spin_lock(&ifa->lock);
696 lifetime = min_t(unsigned long,
697 ifa->valid_lft, 0x7fffffffUL/HZ);
698 if (time_before(expires,
699 ifa->tstamp + lifetime * HZ))
700 expires = ifa->tstamp + lifetime * HZ;
701 spin_unlock(&ifa->lock);
702 }
703 }
704 }
Kristian Slavov1d142802005-12-21 18:47:24 -0800705 ifap = &ifa->if_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 }
707 write_unlock_bh(&idev->lock);
708
709 ipv6_ifa_notify(RTM_DELADDR, ifp);
710
Alan Sterne041c682006-03-27 01:16:30 -0800711 atomic_notifier_call_chain(&inet6addr_chain, NETDEV_DOWN, ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
713 addrconf_del_timer(ifp);
714
715 /*
716 * Purge or update corresponding prefix
717 *
718 * 1) we don't purge prefix here if address was not permanent.
719 * prefix is managed by its own lifetime.
720 * 2) if there're no addresses, delete prefix.
721 * 3) if there're still other permanent address(es),
722 * corresponding prefix is still permanent.
723 * 4) otherwise, update prefix lifetime to the
724 * longest valid lifetime among the corresponding
725 * addresses on the device.
726 * Note: subsequent RA will update lifetime.
727 *
728 * --yoshfuji
729 */
730 if ((ifp->flags & IFA_F_PERMANENT) && onlink < 1) {
731 struct in6_addr prefix;
732 struct rt6_info *rt;
733
734 ipv6_addr_prefix(&prefix, &ifp->addr, ifp->prefix_len);
735 rt = rt6_lookup(&prefix, NULL, ifp->idev->dev->ifindex, 1);
736
737 if (rt && ((rt->rt6i_flags & (RTF_GATEWAY | RTF_DEFAULT)) == 0)) {
738 if (onlink == 0) {
Thomas Grafe0a1ad732006-08-22 00:00:21 -0700739 ip6_del_rt(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 rt = NULL;
741 } else if (!(rt->rt6i_flags & RTF_EXPIRES)) {
742 rt->rt6i_expires = expires;
743 rt->rt6i_flags |= RTF_EXPIRES;
744 }
745 }
746 dst_release(&rt->u.dst);
747 }
748
749 in6_ifa_put(ifp);
750}
751
752#ifdef CONFIG_IPV6_PRIVACY
753static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift)
754{
755 struct inet6_dev *idev = ifp->idev;
756 struct in6_addr addr, *tmpaddr;
757 unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_cstamp, tmp_tstamp;
758 int tmp_plen;
759 int ret = 0;
760 int max_addresses;
761
762 write_lock(&idev->lock);
763 if (ift) {
764 spin_lock_bh(&ift->lock);
765 memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8);
766 spin_unlock_bh(&ift->lock);
767 tmpaddr = &addr;
768 } else {
769 tmpaddr = NULL;
770 }
771retry:
772 in6_dev_hold(idev);
773 if (idev->cnf.use_tempaddr <= 0) {
774 write_unlock(&idev->lock);
775 printk(KERN_INFO
776 "ipv6_create_tempaddr(): use_tempaddr is disabled.\n");
777 in6_dev_put(idev);
778 ret = -1;
779 goto out;
780 }
781 spin_lock_bh(&ifp->lock);
782 if (ifp->regen_count++ >= idev->cnf.regen_max_retry) {
783 idev->cnf.use_tempaddr = -1; /*XXX*/
784 spin_unlock_bh(&ifp->lock);
785 write_unlock(&idev->lock);
786 printk(KERN_WARNING
787 "ipv6_create_tempaddr(): regeneration time exceeded. disabled temporary address support.\n");
788 in6_dev_put(idev);
789 ret = -1;
790 goto out;
791 }
792 in6_ifa_hold(ifp);
793 memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
794 if (__ipv6_try_regen_rndid(idev, tmpaddr) < 0) {
795 spin_unlock_bh(&ifp->lock);
796 write_unlock(&idev->lock);
797 printk(KERN_WARNING
798 "ipv6_create_tempaddr(): regeneration of randomized interface id failed.\n");
799 in6_ifa_put(ifp);
800 in6_dev_put(idev);
801 ret = -1;
802 goto out;
803 }
804 memcpy(&addr.s6_addr[8], idev->rndid, 8);
805 tmp_valid_lft = min_t(__u32,
806 ifp->valid_lft,
807 idev->cnf.temp_valid_lft);
808 tmp_prefered_lft = min_t(__u32,
809 ifp->prefered_lft,
810 idev->cnf.temp_prefered_lft - desync_factor / HZ);
811 tmp_plen = ifp->prefix_len;
812 max_addresses = idev->cnf.max_addresses;
813 tmp_cstamp = ifp->cstamp;
814 tmp_tstamp = ifp->tstamp;
815 spin_unlock_bh(&ifp->lock);
816
817 write_unlock(&idev->lock);
818 ift = !max_addresses ||
819 ipv6_count_addresses(idev) < max_addresses ?
820 ipv6_add_addr(idev, &addr, tmp_plen,
821 ipv6_addr_type(&addr)&IPV6_ADDR_SCOPE_MASK, IFA_F_TEMPORARY) : NULL;
822 if (!ift || IS_ERR(ift)) {
823 in6_ifa_put(ifp);
824 in6_dev_put(idev);
825 printk(KERN_INFO
826 "ipv6_create_tempaddr(): retry temporary address regeneration.\n");
827 tmpaddr = &addr;
828 write_lock(&idev->lock);
829 goto retry;
830 }
831
832 spin_lock_bh(&ift->lock);
833 ift->ifpub = ifp;
834 ift->valid_lft = tmp_valid_lft;
835 ift->prefered_lft = tmp_prefered_lft;
836 ift->cstamp = tmp_cstamp;
837 ift->tstamp = tmp_tstamp;
838 spin_unlock_bh(&ift->lock);
839
840 addrconf_dad_start(ift, 0);
841 in6_ifa_put(ift);
842 in6_dev_put(idev);
843out:
844 return ret;
845}
846#endif
847
848/*
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800849 * Choose an appropriate source address (RFC3484)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 */
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800851struct ipv6_saddr_score {
852 int addr_type;
853 unsigned int attrs;
854 int matchlen;
Brian Haley0d27b422006-03-11 18:50:14 -0800855 int scope;
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800856 unsigned int rule;
857};
858
859#define IPV6_SADDR_SCORE_LOCAL 0x0001
860#define IPV6_SADDR_SCORE_PREFERRED 0x0004
861#define IPV6_SADDR_SCORE_HOA 0x0008
862#define IPV6_SADDR_SCORE_OIF 0x0010
863#define IPV6_SADDR_SCORE_LABEL 0x0020
864#define IPV6_SADDR_SCORE_PRIVACY 0x0040
865
866static int inline ipv6_saddr_preferred(int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867{
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800868 if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|
869 IPV6_ADDR_LOOPBACK|IPV6_ADDR_RESERVED))
870 return 1;
871 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872}
873
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800874/* static matching label */
875static int inline ipv6_saddr_label(const struct in6_addr *addr, int type)
876{
877 /*
878 * prefix (longest match) label
879 * -----------------------------
880 * ::1/128 0
881 * ::/0 1
882 * 2002::/16 2
883 * ::/96 3
884 * ::ffff:0:0/96 4
Łukasz Stelmach102128e2006-06-22 01:37:19 -0700885 * fc00::/7 5
886 * 2001::/32 6
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800887 */
888 if (type & IPV6_ADDR_LOOPBACK)
889 return 0;
890 else if (type & IPV6_ADDR_COMPATv4)
891 return 3;
892 else if (type & IPV6_ADDR_MAPPED)
893 return 4;
Łukasz Stelmach102128e2006-06-22 01:37:19 -0700894 else if (addr->s6_addr32[0] == htonl(0x20010000))
895 return 6;
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800896 else if (addr->s6_addr16[0] == htons(0x2002))
897 return 2;
Łukasz Stelmach102128e2006-06-22 01:37:19 -0700898 else if ((addr->s6_addr[0] & 0xfe) == 0xfc)
899 return 5;
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800900 return 1;
901}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800903int ipv6_dev_get_saddr(struct net_device *daddr_dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 struct in6_addr *daddr, struct in6_addr *saddr)
905{
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800906 struct ipv6_saddr_score hiscore;
907 struct inet6_ifaddr *ifa_result = NULL;
908 int daddr_type = __ipv6_addr_type(daddr);
909 int daddr_scope = __ipv6_addr_src_scope(daddr_type);
910 u32 daddr_label = ipv6_saddr_label(daddr, daddr_type);
911 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800913 memset(&hiscore, 0, sizeof(hiscore));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
915 read_lock(&dev_base_lock);
916 read_lock(&addrconf_lock);
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800917
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 for (dev = dev_base; dev; dev=dev->next) {
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800919 struct inet6_dev *idev;
920 struct inet6_ifaddr *ifa;
921
922 /* Rule 0: Candidate Source Address (section 4)
923 * - multicast and link-local destination address,
924 * the set of candidate source address MUST only
925 * include addresses assigned to interfaces
926 * belonging to the same link as the outgoing
927 * interface.
928 * (- For site-local destination addresses, the
929 * set of candidate source addresses MUST only
930 * include addresses assigned to interfaces
931 * belonging to the same site as the outgoing
932 * interface.)
933 */
934 if ((daddr_type & IPV6_ADDR_MULTICAST ||
935 daddr_scope <= IPV6_ADDR_SCOPE_LINKLOCAL) &&
936 daddr_dev && dev != daddr_dev)
937 continue;
938
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 idev = __in6_dev_get(dev);
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800940 if (!idev)
941 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800943 read_lock_bh(&idev->lock);
944 for (ifa = idev->addr_list; ifa; ifa = ifa->if_next) {
945 struct ipv6_saddr_score score;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800947 score.addr_type = __ipv6_addr_type(&ifa->addr);
948
YOSHIFUJI Hideaki6b3ae802005-12-21 22:58:01 +0900949 /* Rule 0:
950 * - Tentative Address (RFC2462 section 5.4)
951 * - A tentative address is not considered
952 * "assigned to an interface" in the traditional
953 * sense.
954 * - Candidate Source Address (section 4)
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800955 * - In any case, anycast addresses, multicast
956 * addresses, and the unspecified address MUST
957 * NOT be included in a candidate set.
958 */
YOSHIFUJI Hideaki6b3ae802005-12-21 22:58:01 +0900959 if (ifa->flags & IFA_F_TENTATIVE)
960 continue;
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800961 if (unlikely(score.addr_type == IPV6_ADDR_ANY ||
962 score.addr_type & IPV6_ADDR_MULTICAST)) {
963 LIMIT_NETDEBUG(KERN_DEBUG
964 "ADDRCONF: unspecified / multicast address"
965 "assigned as unicast address on %s",
966 dev->name);
967 continue;
968 }
969
970 score.attrs = 0;
971 score.matchlen = 0;
972 score.scope = 0;
973 score.rule = 0;
974
975 if (ifa_result == NULL) {
976 /* record it if the first available entry */
977 goto record_it;
978 }
979
980 /* Rule 1: Prefer same address */
981 if (hiscore.rule < 1) {
982 if (ipv6_addr_equal(&ifa_result->addr, daddr))
983 hiscore.attrs |= IPV6_SADDR_SCORE_LOCAL;
984 hiscore.rule++;
985 }
986 if (ipv6_addr_equal(&ifa->addr, daddr)) {
987 score.attrs |= IPV6_SADDR_SCORE_LOCAL;
988 if (!(hiscore.attrs & IPV6_SADDR_SCORE_LOCAL)) {
989 score.rule = 1;
990 goto record_it;
991 }
992 } else {
993 if (hiscore.attrs & IPV6_SADDR_SCORE_LOCAL)
994 continue;
995 }
996
997 /* Rule 2: Prefer appropriate scope */
998 if (hiscore.rule < 2) {
999 hiscore.scope = __ipv6_addr_src_scope(hiscore.addr_type);
1000 hiscore.rule++;
1001 }
1002 score.scope = __ipv6_addr_src_scope(score.addr_type);
1003 if (hiscore.scope < score.scope) {
1004 if (hiscore.scope < daddr_scope) {
1005 score.rule = 2;
1006 goto record_it;
1007 } else
1008 continue;
1009 } else if (score.scope < hiscore.scope) {
1010 if (score.scope < daddr_scope)
Brian Haleye55ffac2006-07-10 15:25:51 -07001011 break; /* addresses sorted by scope */
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001012 else {
1013 score.rule = 2;
1014 goto record_it;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 }
1016 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001018 /* Rule 3: Avoid deprecated address */
1019 if (hiscore.rule < 3) {
1020 if (ipv6_saddr_preferred(hiscore.addr_type) ||
1021 !(ifa_result->flags & IFA_F_DEPRECATED))
1022 hiscore.attrs |= IPV6_SADDR_SCORE_PREFERRED;
1023 hiscore.rule++;
1024 }
1025 if (ipv6_saddr_preferred(score.addr_type) ||
1026 !(ifa->flags & IFA_F_DEPRECATED)) {
1027 score.attrs |= IPV6_SADDR_SCORE_PREFERRED;
1028 if (!(hiscore.attrs & IPV6_SADDR_SCORE_PREFERRED)) {
1029 score.rule = 3;
1030 goto record_it;
1031 }
1032 } else {
1033 if (hiscore.attrs & IPV6_SADDR_SCORE_PREFERRED)
1034 continue;
1035 }
1036
1037 /* Rule 4: Prefer home address -- not implemented yet */
YOSHIFUJI Hideaki220bbd72005-11-28 22:27:11 -08001038 if (hiscore.rule < 4)
1039 hiscore.rule++;
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001040
1041 /* Rule 5: Prefer outgoing interface */
1042 if (hiscore.rule < 5) {
1043 if (daddr_dev == NULL ||
1044 daddr_dev == ifa_result->idev->dev)
1045 hiscore.attrs |= IPV6_SADDR_SCORE_OIF;
1046 hiscore.rule++;
1047 }
1048 if (daddr_dev == NULL ||
1049 daddr_dev == ifa->idev->dev) {
1050 score.attrs |= IPV6_SADDR_SCORE_OIF;
1051 if (!(hiscore.attrs & IPV6_SADDR_SCORE_OIF)) {
1052 score.rule = 5;
1053 goto record_it;
1054 }
1055 } else {
1056 if (hiscore.attrs & IPV6_SADDR_SCORE_OIF)
1057 continue;
1058 }
1059
1060 /* Rule 6: Prefer matching label */
1061 if (hiscore.rule < 6) {
1062 if (ipv6_saddr_label(&ifa_result->addr, hiscore.addr_type) == daddr_label)
1063 hiscore.attrs |= IPV6_SADDR_SCORE_LABEL;
1064 hiscore.rule++;
1065 }
1066 if (ipv6_saddr_label(&ifa->addr, score.addr_type) == daddr_label) {
1067 score.attrs |= IPV6_SADDR_SCORE_LABEL;
1068 if (!(hiscore.attrs & IPV6_SADDR_SCORE_LABEL)) {
1069 score.rule = 6;
1070 goto record_it;
1071 }
1072 } else {
1073 if (hiscore.attrs & IPV6_SADDR_SCORE_LABEL)
1074 continue;
1075 }
1076
Peter Chubb44fd0262005-11-09 13:05:47 -08001077#ifdef CONFIG_IPV6_PRIVACY
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001078 /* Rule 7: Prefer public address
1079 * Note: prefer temprary address if use_tempaddr >= 2
1080 */
1081 if (hiscore.rule < 7) {
1082 if ((!(ifa_result->flags & IFA_F_TEMPORARY)) ^
1083 (ifa_result->idev->cnf.use_tempaddr >= 2))
1084 hiscore.attrs |= IPV6_SADDR_SCORE_PRIVACY;
1085 hiscore.rule++;
1086 }
1087 if ((!(ifa->flags & IFA_F_TEMPORARY)) ^
1088 (ifa->idev->cnf.use_tempaddr >= 2)) {
1089 score.attrs |= IPV6_SADDR_SCORE_PRIVACY;
1090 if (!(hiscore.attrs & IPV6_SADDR_SCORE_PRIVACY)) {
1091 score.rule = 7;
1092 goto record_it;
1093 }
1094 } else {
1095 if (hiscore.attrs & IPV6_SADDR_SCORE_PRIVACY)
1096 continue;
1097 }
YOSHIFUJI Hideaki5e2707f2006-06-22 01:41:18 -07001098#else
1099 if (hiscore.rule < 7)
1100 hiscore.rule++;
Peter Chubb44fd0262005-11-09 13:05:47 -08001101#endif
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001102 /* Rule 8: Use longest matching prefix */
Yan Zheng12da2a42005-11-14 21:42:46 -08001103 if (hiscore.rule < 8) {
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001104 hiscore.matchlen = ipv6_addr_diff(&ifa_result->addr, daddr);
Yan Zheng12da2a42005-11-14 21:42:46 -08001105 hiscore.rule++;
1106 }
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001107 score.matchlen = ipv6_addr_diff(&ifa->addr, daddr);
1108 if (score.matchlen > hiscore.matchlen) {
1109 score.rule = 8;
1110 goto record_it;
1111 }
1112#if 0
1113 else if (score.matchlen < hiscore.matchlen)
1114 continue;
1115#endif
1116
1117 /* Final Rule: choose first available one */
1118 continue;
1119record_it:
1120 if (ifa_result)
1121 in6_ifa_put(ifa_result);
1122 in6_ifa_hold(ifa);
1123 ifa_result = ifa;
1124 hiscore = score;
1125 }
1126 read_unlock_bh(&idev->lock);
1127 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 read_unlock(&addrconf_lock);
1129 read_unlock(&dev_base_lock);
1130
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001131 if (!ifa_result)
1132 return -EADDRNOTAVAIL;
1133
1134 ipv6_addr_copy(saddr, &ifa_result->addr);
1135 in6_ifa_put(ifa_result);
1136 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137}
1138
1139
1140int ipv6_get_saddr(struct dst_entry *dst,
1141 struct in6_addr *daddr, struct in6_addr *saddr)
1142{
1143 return ipv6_dev_get_saddr(dst ? ((struct rt6_info *)dst)->rt6i_idev->dev : NULL, daddr, saddr);
1144}
1145
1146
1147int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr)
1148{
1149 struct inet6_dev *idev;
1150 int err = -EADDRNOTAVAIL;
1151
1152 read_lock(&addrconf_lock);
1153 if ((idev = __in6_dev_get(dev)) != NULL) {
1154 struct inet6_ifaddr *ifp;
1155
1156 read_lock_bh(&idev->lock);
1157 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
1158 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
1159 ipv6_addr_copy(addr, &ifp->addr);
1160 err = 0;
1161 break;
1162 }
1163 }
1164 read_unlock_bh(&idev->lock);
1165 }
1166 read_unlock(&addrconf_lock);
1167 return err;
1168}
1169
1170static int ipv6_count_addresses(struct inet6_dev *idev)
1171{
1172 int cnt = 0;
1173 struct inet6_ifaddr *ifp;
1174
1175 read_lock_bh(&idev->lock);
1176 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next)
1177 cnt++;
1178 read_unlock_bh(&idev->lock);
1179 return cnt;
1180}
1181
1182int ipv6_chk_addr(struct in6_addr *addr, struct net_device *dev, int strict)
1183{
1184 struct inet6_ifaddr * ifp;
1185 u8 hash = ipv6_addr_hash(addr);
1186
1187 read_lock_bh(&addrconf_hash_lock);
1188 for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
1189 if (ipv6_addr_equal(&ifp->addr, addr) &&
1190 !(ifp->flags&IFA_F_TENTATIVE)) {
1191 if (dev == NULL || ifp->idev->dev == dev ||
1192 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))
1193 break;
1194 }
1195 }
1196 read_unlock_bh(&addrconf_hash_lock);
1197 return ifp != NULL;
1198}
1199
1200static
1201int ipv6_chk_same_addr(const struct in6_addr *addr, struct net_device *dev)
1202{
1203 struct inet6_ifaddr * ifp;
1204 u8 hash = ipv6_addr_hash(addr);
1205
1206 for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
1207 if (ipv6_addr_equal(&ifp->addr, addr)) {
1208 if (dev == NULL || ifp->idev->dev == dev)
1209 break;
1210 }
1211 }
1212 return ifp != NULL;
1213}
1214
1215struct inet6_ifaddr * ipv6_get_ifaddr(struct in6_addr *addr, struct net_device *dev, int strict)
1216{
1217 struct inet6_ifaddr * ifp;
1218 u8 hash = ipv6_addr_hash(addr);
1219
1220 read_lock_bh(&addrconf_hash_lock);
1221 for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
1222 if (ipv6_addr_equal(&ifp->addr, addr)) {
1223 if (dev == NULL || ifp->idev->dev == dev ||
1224 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
1225 in6_ifa_hold(ifp);
1226 break;
1227 }
1228 }
1229 }
1230 read_unlock_bh(&addrconf_hash_lock);
1231
1232 return ifp;
1233}
1234
1235int ipv6_rcv_saddr_equal(const struct sock *sk, const struct sock *sk2)
1236{
1237 const struct in6_addr *sk_rcv_saddr6 = &inet6_sk(sk)->rcv_saddr;
Arnaldo Carvalho de Melo0fa1a532005-12-13 23:23:09 -08001238 const struct in6_addr *sk2_rcv_saddr6 = inet6_rcv_saddr(sk2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 u32 sk_rcv_saddr = inet_sk(sk)->rcv_saddr;
Arnaldo Carvalho de Melo8feaf0c2005-08-09 20:09:30 -07001240 u32 sk2_rcv_saddr = inet_rcv_saddr(sk2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 int sk_ipv6only = ipv6_only_sock(sk);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001242 int sk2_ipv6only = inet_v6_ipv6only(sk2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 int addr_type = ipv6_addr_type(sk_rcv_saddr6);
1244 int addr_type2 = sk2_rcv_saddr6 ? ipv6_addr_type(sk2_rcv_saddr6) : IPV6_ADDR_MAPPED;
1245
1246 if (!sk2_rcv_saddr && !sk_ipv6only)
1247 return 1;
1248
1249 if (addr_type2 == IPV6_ADDR_ANY &&
1250 !(sk2_ipv6only && addr_type == IPV6_ADDR_MAPPED))
1251 return 1;
1252
1253 if (addr_type == IPV6_ADDR_ANY &&
1254 !(sk_ipv6only && addr_type2 == IPV6_ADDR_MAPPED))
1255 return 1;
1256
1257 if (sk2_rcv_saddr6 &&
1258 ipv6_addr_equal(sk_rcv_saddr6, sk2_rcv_saddr6))
1259 return 1;
1260
1261 if (addr_type == IPV6_ADDR_MAPPED &&
1262 !sk2_ipv6only &&
1263 (!sk2_rcv_saddr || !sk_rcv_saddr || sk_rcv_saddr == sk2_rcv_saddr))
1264 return 1;
1265
1266 return 0;
1267}
1268
1269/* Gets referenced address, destroys ifaddr */
1270
Adrian Bunk9f5336e2006-01-07 13:24:25 -08001271static void addrconf_dad_stop(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 if (ifp->flags&IFA_F_PERMANENT) {
1274 spin_lock_bh(&ifp->lock);
1275 addrconf_del_timer(ifp);
1276 ifp->flags |= IFA_F_TENTATIVE;
1277 spin_unlock_bh(&ifp->lock);
1278 in6_ifa_put(ifp);
1279#ifdef CONFIG_IPV6_PRIVACY
1280 } else if (ifp->flags&IFA_F_TEMPORARY) {
1281 struct inet6_ifaddr *ifpub;
1282 spin_lock_bh(&ifp->lock);
1283 ifpub = ifp->ifpub;
1284 if (ifpub) {
1285 in6_ifa_hold(ifpub);
1286 spin_unlock_bh(&ifp->lock);
1287 ipv6_create_tempaddr(ifpub, ifp);
1288 in6_ifa_put(ifpub);
1289 } else {
1290 spin_unlock_bh(&ifp->lock);
1291 }
1292 ipv6_del_addr(ifp);
1293#endif
1294 } else
1295 ipv6_del_addr(ifp);
1296}
1297
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09001298void addrconf_dad_failure(struct inet6_ifaddr *ifp)
1299{
1300 if (net_ratelimit())
1301 printk(KERN_INFO "%s: duplicate address detected!\n", ifp->idev->dev->name);
1302 addrconf_dad_stop(ifp);
1303}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
1305/* Join to solicited addr multicast group. */
1306
1307void addrconf_join_solict(struct net_device *dev, struct in6_addr *addr)
1308{
1309 struct in6_addr maddr;
1310
1311 if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1312 return;
1313
1314 addrconf_addr_solict_mult(addr, &maddr);
1315 ipv6_dev_mc_inc(dev, &maddr);
1316}
1317
1318void addrconf_leave_solict(struct inet6_dev *idev, struct in6_addr *addr)
1319{
1320 struct in6_addr maddr;
1321
1322 if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1323 return;
1324
1325 addrconf_addr_solict_mult(addr, &maddr);
1326 __ipv6_dev_mc_dec(idev, &maddr);
1327}
1328
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03001329static void addrconf_join_anycast(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330{
1331 struct in6_addr addr;
1332 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1333 if (ipv6_addr_any(&addr))
1334 return;
1335 ipv6_dev_ac_inc(ifp->idev->dev, &addr);
1336}
1337
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03001338static void addrconf_leave_anycast(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339{
1340 struct in6_addr addr;
1341 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1342 if (ipv6_addr_any(&addr))
1343 return;
1344 __ipv6_dev_ac_dec(ifp->idev, &addr);
1345}
1346
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08001347static int addrconf_ifid_eui48(u8 *eui, struct net_device *dev)
1348{
1349 if (dev->addr_len != ETH_ALEN)
1350 return -1;
1351 memcpy(eui, dev->dev_addr, 3);
1352 memcpy(eui + 5, dev->dev_addr + 3, 3);
1353
1354 /*
1355 * The zSeries OSA network cards can be shared among various
1356 * OS instances, but the OSA cards have only one MAC address.
1357 * This leads to duplicate address conflicts in conjunction
1358 * with IPv6 if more than one instance uses the same card.
1359 *
1360 * The driver for these cards can deliver a unique 16-bit
1361 * identifier for each instance sharing the same card. It is
1362 * placed instead of 0xFFFE in the interface identifier. The
1363 * "u" bit of the interface identifier is not inverted in this
1364 * case. Hence the resulting interface identifier has local
1365 * scope according to RFC2373.
1366 */
1367 if (dev->dev_id) {
1368 eui[3] = (dev->dev_id >> 8) & 0xFF;
1369 eui[4] = dev->dev_id & 0xFF;
1370 } else {
1371 eui[3] = 0xFF;
1372 eui[4] = 0xFE;
1373 eui[0] ^= 2;
1374 }
1375 return 0;
1376}
1377
1378static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev)
1379{
1380 /* XXX: inherit EUI-64 from other interface -- yoshfuji */
1381 if (dev->addr_len != ARCNET_ALEN)
1382 return -1;
1383 memset(eui, 0, 7);
1384 eui[7] = *(u8*)dev->dev_addr;
1385 return 0;
1386}
1387
1388static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev)
1389{
1390 if (dev->addr_len != INFINIBAND_ALEN)
1391 return -1;
1392 memcpy(eui, dev->dev_addr + 12, 8);
1393 eui[0] |= 2;
1394 return 0;
1395}
1396
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
1398{
1399 switch (dev->type) {
1400 case ARPHRD_ETHER:
1401 case ARPHRD_FDDI:
1402 case ARPHRD_IEEE802_TR:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08001403 return addrconf_ifid_eui48(eui, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 case ARPHRD_ARCNET:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08001405 return addrconf_ifid_arcnet(eui, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 case ARPHRD_INFINIBAND:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08001407 return addrconf_ifid_infiniband(eui, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 }
1409 return -1;
1410}
1411
1412static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
1413{
1414 int err = -1;
1415 struct inet6_ifaddr *ifp;
1416
1417 read_lock_bh(&idev->lock);
1418 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
1419 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
1420 memcpy(eui, ifp->addr.s6_addr+8, 8);
1421 err = 0;
1422 break;
1423 }
1424 }
1425 read_unlock_bh(&idev->lock);
1426 return err;
1427}
1428
1429#ifdef CONFIG_IPV6_PRIVACY
1430/* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
1431static int __ipv6_regen_rndid(struct inet6_dev *idev)
1432{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433regen:
YOSHIFUJI Hideaki955189e2006-03-20 16:54:09 -08001434 get_random_bytes(idev->rndid, sizeof(idev->rndid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 idev->rndid[0] &= ~0x02;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436
1437 /*
1438 * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
1439 * check if generated address is not inappropriate
1440 *
1441 * - Reserved subnet anycast (RFC 2526)
1442 * 11111101 11....11 1xxxxxxx
1443 * - ISATAP (draft-ietf-ngtrans-isatap-13.txt) 5.1
1444 * 00-00-5E-FE-xx-xx-xx-xx
1445 * - value 0
1446 * - XXX: already assigned to an address on the device
1447 */
1448 if (idev->rndid[0] == 0xfd &&
1449 (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff &&
1450 (idev->rndid[7]&0x80))
1451 goto regen;
1452 if ((idev->rndid[0]|idev->rndid[1]) == 0) {
1453 if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe)
1454 goto regen;
1455 if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00)
1456 goto regen;
1457 }
1458
1459 return 0;
1460}
1461
1462static void ipv6_regen_rndid(unsigned long data)
1463{
1464 struct inet6_dev *idev = (struct inet6_dev *) data;
1465 unsigned long expires;
1466
1467 read_lock_bh(&addrconf_lock);
1468 write_lock_bh(&idev->lock);
1469
1470 if (idev->dead)
1471 goto out;
1472
1473 if (__ipv6_regen_rndid(idev) < 0)
1474 goto out;
1475
1476 expires = jiffies +
1477 idev->cnf.temp_prefered_lft * HZ -
1478 idev->cnf.regen_max_retry * idev->cnf.dad_transmits * idev->nd_parms->retrans_time - desync_factor;
1479 if (time_before(expires, jiffies)) {
1480 printk(KERN_WARNING
1481 "ipv6_regen_rndid(): too short regeneration interval; timer disabled for %s.\n",
1482 idev->dev->name);
1483 goto out;
1484 }
1485
1486 if (!mod_timer(&idev->regen_timer, expires))
1487 in6_dev_hold(idev);
1488
1489out:
1490 write_unlock_bh(&idev->lock);
1491 read_unlock_bh(&addrconf_lock);
1492 in6_dev_put(idev);
1493}
1494
1495static int __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr) {
1496 int ret = 0;
1497
1498 if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0)
1499 ret = __ipv6_regen_rndid(idev);
1500 return ret;
1501}
1502#endif
1503
1504/*
1505 * Add prefix route.
1506 */
1507
1508static void
1509addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev,
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -07001510 unsigned long expires, u32 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511{
Thomas Graf86872cb2006-08-22 00:01:08 -07001512 struct fib6_config cfg = {
1513 .fc_table = RT6_TABLE_PREFIX,
1514 .fc_metric = IP6_RT_PRIO_ADDRCONF,
1515 .fc_ifindex = dev->ifindex,
1516 .fc_expires = expires,
1517 .fc_dst_len = plen,
1518 .fc_flags = RTF_UP | flags,
1519 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
Thomas Graf86872cb2006-08-22 00:01:08 -07001521 ipv6_addr_copy(&cfg.fc_dst, pfx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
1523 /* Prevent useless cloning on PtP SIT.
1524 This thing is done here expecting that the whole
1525 class of non-broadcast devices need not cloning.
1526 */
Thomas Graf86872cb2006-08-22 00:01:08 -07001527 if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT))
1528 cfg.fc_flags |= RTF_NONEXTHOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
Thomas Graf86872cb2006-08-22 00:01:08 -07001530 ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531}
1532
1533/* Create "default" multicast route to the interface */
1534
1535static void addrconf_add_mroute(struct net_device *dev)
1536{
Thomas Graf86872cb2006-08-22 00:01:08 -07001537 struct fib6_config cfg = {
1538 .fc_table = RT6_TABLE_LOCAL,
1539 .fc_metric = IP6_RT_PRIO_ADDRCONF,
1540 .fc_ifindex = dev->ifindex,
1541 .fc_dst_len = 8,
1542 .fc_flags = RTF_UP,
1543 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544
Thomas Graf86872cb2006-08-22 00:01:08 -07001545 ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0);
1546
1547 ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548}
1549
1550static void sit_route_add(struct net_device *dev)
1551{
Thomas Graf86872cb2006-08-22 00:01:08 -07001552 struct fib6_config cfg = {
1553 .fc_table = RT6_TABLE_MAIN,
1554 .fc_metric = IP6_RT_PRIO_ADDRCONF,
1555 .fc_ifindex = dev->ifindex,
1556 .fc_dst_len = 96,
1557 .fc_flags = RTF_UP | RTF_NONEXTHOP,
1558 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
1560 /* prefix length - 96 bits "::d.d.d.d" */
Thomas Graf86872cb2006-08-22 00:01:08 -07001561 ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562}
1563
1564static void addrconf_add_lroute(struct net_device *dev)
1565{
1566 struct in6_addr addr;
1567
1568 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
1569 addrconf_prefix_route(&addr, 64, dev, 0, 0);
1570}
1571
1572static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
1573{
1574 struct inet6_dev *idev;
1575
1576 ASSERT_RTNL();
1577
1578 if ((idev = ipv6_find_idev(dev)) == NULL)
1579 return NULL;
1580
1581 /* Add default multicast route */
1582 addrconf_add_mroute(dev);
1583
1584 /* Add link local route */
1585 addrconf_add_lroute(dev);
1586 return idev;
1587}
1588
1589void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len)
1590{
1591 struct prefix_info *pinfo;
1592 __u32 valid_lft;
1593 __u32 prefered_lft;
1594 int addr_type;
1595 unsigned long rt_expires;
1596 struct inet6_dev *in6_dev;
1597
1598 pinfo = (struct prefix_info *) opt;
1599
1600 if (len < sizeof(struct prefix_info)) {
1601 ADBG(("addrconf: prefix option too short\n"));
1602 return;
1603 }
1604
1605 /*
1606 * Validation checks ([ADDRCONF], page 19)
1607 */
1608
1609 addr_type = ipv6_addr_type(&pinfo->prefix);
1610
1611 if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
1612 return;
1613
1614 valid_lft = ntohl(pinfo->valid);
1615 prefered_lft = ntohl(pinfo->prefered);
1616
1617 if (prefered_lft > valid_lft) {
1618 if (net_ratelimit())
1619 printk(KERN_WARNING "addrconf: prefix option has invalid lifetime\n");
1620 return;
1621 }
1622
1623 in6_dev = in6_dev_get(dev);
1624
1625 if (in6_dev == NULL) {
1626 if (net_ratelimit())
1627 printk(KERN_DEBUG "addrconf: device %s not configured\n", dev->name);
1628 return;
1629 }
1630
1631 /*
1632 * Two things going on here:
1633 * 1) Add routes for on-link prefixes
1634 * 2) Configure prefixes with the auto flag set
1635 */
1636
1637 /* Avoid arithmetic overflow. Really, we could
1638 save rt_expires in seconds, likely valid_lft,
1639 but it would require division in fib gc, that it
1640 not good.
1641 */
1642 if (valid_lft >= 0x7FFFFFFF/HZ)
YOSHIFUJI Hideaki3dd4bc62005-12-19 14:02:45 -08001643 rt_expires = 0x7FFFFFFF - (0x7FFFFFFF % HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 else
YOSHIFUJI Hideaki3dd4bc62005-12-19 14:02:45 -08001645 rt_expires = valid_lft * HZ;
1646
1647 /*
1648 * We convert this (in jiffies) to clock_t later.
1649 * Avoid arithmetic overflow there as well.
1650 * Overflow can happen only if HZ < USER_HZ.
1651 */
1652 if (HZ < USER_HZ && rt_expires > 0x7FFFFFFF / USER_HZ)
1653 rt_expires = 0x7FFFFFFF / USER_HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654
1655 if (pinfo->onlink) {
1656 struct rt6_info *rt;
1657 rt = rt6_lookup(&pinfo->prefix, NULL, dev->ifindex, 1);
1658
1659 if (rt && ((rt->rt6i_flags & (RTF_GATEWAY | RTF_DEFAULT)) == 0)) {
1660 if (rt->rt6i_flags&RTF_EXPIRES) {
1661 if (valid_lft == 0) {
Thomas Grafe0a1ad732006-08-22 00:00:21 -07001662 ip6_del_rt(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 rt = NULL;
1664 } else {
YOSHIFUJI Hideaki3dd4bc62005-12-19 14:02:45 -08001665 rt->rt6i_expires = jiffies + rt_expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 }
1667 }
1668 } else if (valid_lft) {
1669 addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
YOSHIFUJI Hideaki3dd4bc62005-12-19 14:02:45 -08001670 dev, jiffies_to_clock_t(rt_expires), RTF_ADDRCONF|RTF_EXPIRES|RTF_PREFIX_RT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 }
1672 if (rt)
1673 dst_release(&rt->u.dst);
1674 }
1675
1676 /* Try to figure out our local address for this prefix */
1677
1678 if (pinfo->autoconf && in6_dev->cnf.autoconf) {
1679 struct inet6_ifaddr * ifp;
1680 struct in6_addr addr;
1681 int create = 0, update_lft = 0;
1682
1683 if (pinfo->prefix_len == 64) {
1684 memcpy(&addr, &pinfo->prefix, 8);
1685 if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
1686 ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
1687 in6_dev_put(in6_dev);
1688 return;
1689 }
1690 goto ok;
1691 }
1692 if (net_ratelimit())
1693 printk(KERN_DEBUG "IPv6 addrconf: prefix with wrong length %d\n",
1694 pinfo->prefix_len);
1695 in6_dev_put(in6_dev);
1696 return;
1697
1698ok:
1699
1700 ifp = ipv6_get_ifaddr(&addr, dev, 1);
1701
1702 if (ifp == NULL && valid_lft) {
1703 int max_addresses = in6_dev->cnf.max_addresses;
1704
1705 /* Do not allow to create too much of autoconfigured
1706 * addresses; this would be too easy way to crash kernel.
1707 */
1708 if (!max_addresses ||
1709 ipv6_count_addresses(in6_dev) < max_addresses)
1710 ifp = ipv6_add_addr(in6_dev, &addr, pinfo->prefix_len,
1711 addr_type&IPV6_ADDR_SCOPE_MASK, 0);
1712
1713 if (!ifp || IS_ERR(ifp)) {
1714 in6_dev_put(in6_dev);
1715 return;
1716 }
1717
1718 update_lft = create = 1;
1719 ifp->cstamp = jiffies;
1720 addrconf_dad_start(ifp, RTF_ADDRCONF|RTF_PREFIX_RT);
1721 }
1722
1723 if (ifp) {
1724 int flags;
1725 unsigned long now;
1726#ifdef CONFIG_IPV6_PRIVACY
1727 struct inet6_ifaddr *ift;
1728#endif
1729 u32 stored_lft;
1730
1731 /* update lifetime (RFC2462 5.5.3 e) */
1732 spin_lock(&ifp->lock);
1733 now = jiffies;
1734 if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
1735 stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
1736 else
1737 stored_lft = 0;
1738 if (!update_lft && stored_lft) {
1739 if (valid_lft > MIN_VALID_LIFETIME ||
1740 valid_lft > stored_lft)
1741 update_lft = 1;
1742 else if (stored_lft <= MIN_VALID_LIFETIME) {
1743 /* valid_lft <= stored_lft is always true */
1744 /* XXX: IPsec */
1745 update_lft = 0;
1746 } else {
1747 valid_lft = MIN_VALID_LIFETIME;
1748 if (valid_lft < prefered_lft)
1749 prefered_lft = valid_lft;
1750 update_lft = 1;
1751 }
1752 }
1753
1754 if (update_lft) {
1755 ifp->valid_lft = valid_lft;
1756 ifp->prefered_lft = prefered_lft;
1757 ifp->tstamp = now;
1758 flags = ifp->flags;
1759 ifp->flags &= ~IFA_F_DEPRECATED;
1760 spin_unlock(&ifp->lock);
1761
1762 if (!(flags&IFA_F_TENTATIVE))
1763 ipv6_ifa_notify(0, ifp);
1764 } else
1765 spin_unlock(&ifp->lock);
1766
1767#ifdef CONFIG_IPV6_PRIVACY
1768 read_lock_bh(&in6_dev->lock);
1769 /* update all temporary addresses in the list */
1770 for (ift=in6_dev->tempaddr_list; ift; ift=ift->tmp_next) {
1771 /*
1772 * When adjusting the lifetimes of an existing
1773 * temporary address, only lower the lifetimes.
1774 * Implementations must not increase the
1775 * lifetimes of an existing temporary address
1776 * when processing a Prefix Information Option.
1777 */
1778 spin_lock(&ift->lock);
1779 flags = ift->flags;
1780 if (ift->valid_lft > valid_lft &&
1781 ift->valid_lft - valid_lft > (jiffies - ift->tstamp) / HZ)
1782 ift->valid_lft = valid_lft + (jiffies - ift->tstamp) / HZ;
1783 if (ift->prefered_lft > prefered_lft &&
1784 ift->prefered_lft - prefered_lft > (jiffies - ift->tstamp) / HZ)
1785 ift->prefered_lft = prefered_lft + (jiffies - ift->tstamp) / HZ;
1786 spin_unlock(&ift->lock);
1787 if (!(flags&IFA_F_TENTATIVE))
1788 ipv6_ifa_notify(0, ift);
1789 }
1790
1791 if (create && in6_dev->cnf.use_tempaddr > 0) {
1792 /*
1793 * When a new public address is created as described in [ADDRCONF],
1794 * also create a new temporary address.
1795 */
1796 read_unlock_bh(&in6_dev->lock);
1797 ipv6_create_tempaddr(ifp, NULL);
1798 } else {
1799 read_unlock_bh(&in6_dev->lock);
1800 }
1801#endif
1802 in6_ifa_put(ifp);
1803 addrconf_verify(0);
1804 }
1805 }
1806 inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
1807 in6_dev_put(in6_dev);
1808}
1809
1810/*
1811 * Set destination address.
1812 * Special case for SIT interfaces where we create a new "virtual"
1813 * device.
1814 */
1815int addrconf_set_dstaddr(void __user *arg)
1816{
1817 struct in6_ifreq ireq;
1818 struct net_device *dev;
1819 int err = -EINVAL;
1820
1821 rtnl_lock();
1822
1823 err = -EFAULT;
1824 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
1825 goto err_exit;
1826
1827 dev = __dev_get_by_index(ireq.ifr6_ifindex);
1828
1829 err = -ENODEV;
1830 if (dev == NULL)
1831 goto err_exit;
1832
1833 if (dev->type == ARPHRD_SIT) {
1834 struct ifreq ifr;
1835 mm_segment_t oldfs;
1836 struct ip_tunnel_parm p;
1837
1838 err = -EADDRNOTAVAIL;
1839 if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4))
1840 goto err_exit;
1841
1842 memset(&p, 0, sizeof(p));
1843 p.iph.daddr = ireq.ifr6_addr.s6_addr32[3];
1844 p.iph.saddr = 0;
1845 p.iph.version = 4;
1846 p.iph.ihl = 5;
1847 p.iph.protocol = IPPROTO_IPV6;
1848 p.iph.ttl = 64;
1849 ifr.ifr_ifru.ifru_data = (void __user *)&p;
1850
1851 oldfs = get_fs(); set_fs(KERNEL_DS);
1852 err = dev->do_ioctl(dev, &ifr, SIOCADDTUNNEL);
1853 set_fs(oldfs);
1854
1855 if (err == 0) {
1856 err = -ENOBUFS;
1857 if ((dev = __dev_get_by_name(p.name)) == NULL)
1858 goto err_exit;
1859 err = dev_open(dev);
1860 }
1861 }
1862
1863err_exit:
1864 rtnl_unlock();
1865 return err;
1866}
1867
1868/*
1869 * Manual configuration of address on an interface
1870 */
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09001871static int inet6_addr_add(int ifindex, struct in6_addr *pfx, int plen,
1872 __u32 prefered_lft, __u32 valid_lft)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873{
1874 struct inet6_ifaddr *ifp;
1875 struct inet6_dev *idev;
1876 struct net_device *dev;
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09001877 __u8 ifa_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 int scope;
1879
1880 ASSERT_RTNL();
1881
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09001882 /* check the lifetime */
1883 if (!valid_lft || prefered_lft > valid_lft)
1884 return -EINVAL;
1885
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 if ((dev = __dev_get_by_index(ifindex)) == NULL)
1887 return -ENODEV;
1888
1889 if (!(dev->flags&IFF_UP))
1890 return -ENETDOWN;
1891
1892 if ((idev = addrconf_add_dev(dev)) == NULL)
1893 return -ENOBUFS;
1894
1895 scope = ipv6_addr_scope(pfx);
1896
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09001897 if (valid_lft == INFINITY_LIFE_TIME)
1898 ifa_flags |= IFA_F_PERMANENT;
1899 else if (valid_lft >= 0x7FFFFFFF/HZ)
1900 valid_lft = 0x7FFFFFFF/HZ;
1901
1902 if (prefered_lft == 0)
1903 ifa_flags |= IFA_F_DEPRECATED;
1904 else if ((prefered_lft >= 0x7FFFFFFF/HZ) &&
1905 (prefered_lft != INFINITY_LIFE_TIME))
1906 prefered_lft = 0x7FFFFFFF/HZ;
1907
1908 ifp = ipv6_add_addr(idev, pfx, plen, scope, ifa_flags);
1909
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 if (!IS_ERR(ifp)) {
Herbert Xu06aebfb2006-08-09 16:52:04 -07001911 spin_lock_bh(&ifp->lock);
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09001912 ifp->valid_lft = valid_lft;
1913 ifp->prefered_lft = prefered_lft;
1914 ifp->tstamp = jiffies;
Herbert Xu06aebfb2006-08-09 16:52:04 -07001915 spin_unlock_bh(&ifp->lock);
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09001916
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 addrconf_dad_start(ifp, 0);
1918 in6_ifa_put(ifp);
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09001919 addrconf_verify(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 return 0;
1921 }
1922
1923 return PTR_ERR(ifp);
1924}
1925
1926static int inet6_addr_del(int ifindex, struct in6_addr *pfx, int plen)
1927{
1928 struct inet6_ifaddr *ifp;
1929 struct inet6_dev *idev;
1930 struct net_device *dev;
1931
1932 if ((dev = __dev_get_by_index(ifindex)) == NULL)
1933 return -ENODEV;
1934
1935 if ((idev = __in6_dev_get(dev)) == NULL)
1936 return -ENXIO;
1937
1938 read_lock_bh(&idev->lock);
1939 for (ifp = idev->addr_list; ifp; ifp=ifp->if_next) {
1940 if (ifp->prefix_len == plen &&
1941 ipv6_addr_equal(pfx, &ifp->addr)) {
1942 in6_ifa_hold(ifp);
1943 read_unlock_bh(&idev->lock);
1944
1945 ipv6_del_addr(ifp);
1946
1947 /* If the last address is deleted administratively,
1948 disable IPv6 on this interface.
1949 */
1950 if (idev->addr_list == NULL)
1951 addrconf_ifdown(idev->dev, 1);
1952 return 0;
1953 }
1954 }
1955 read_unlock_bh(&idev->lock);
1956 return -EADDRNOTAVAIL;
1957}
1958
1959
1960int addrconf_add_ifaddr(void __user *arg)
1961{
1962 struct in6_ifreq ireq;
1963 int err;
1964
1965 if (!capable(CAP_NET_ADMIN))
1966 return -EPERM;
1967
1968 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
1969 return -EFAULT;
1970
1971 rtnl_lock();
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09001972 err = inet6_addr_add(ireq.ifr6_ifindex, &ireq.ifr6_addr, ireq.ifr6_prefixlen,
1973 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 rtnl_unlock();
1975 return err;
1976}
1977
1978int addrconf_del_ifaddr(void __user *arg)
1979{
1980 struct in6_ifreq ireq;
1981 int err;
1982
1983 if (!capable(CAP_NET_ADMIN))
1984 return -EPERM;
1985
1986 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
1987 return -EFAULT;
1988
1989 rtnl_lock();
1990 err = inet6_addr_del(ireq.ifr6_ifindex, &ireq.ifr6_addr, ireq.ifr6_prefixlen);
1991 rtnl_unlock();
1992 return err;
1993}
1994
1995static void sit_add_v4_addrs(struct inet6_dev *idev)
1996{
1997 struct inet6_ifaddr * ifp;
1998 struct in6_addr addr;
1999 struct net_device *dev;
2000 int scope;
2001
2002 ASSERT_RTNL();
2003
2004 memset(&addr, 0, sizeof(struct in6_addr));
2005 memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
2006
2007 if (idev->dev->flags&IFF_POINTOPOINT) {
2008 addr.s6_addr32[0] = htonl(0xfe800000);
2009 scope = IFA_LINK;
2010 } else {
2011 scope = IPV6_ADDR_COMPATv4;
2012 }
2013
2014 if (addr.s6_addr32[3]) {
2015 ifp = ipv6_add_addr(idev, &addr, 128, scope, IFA_F_PERMANENT);
2016 if (!IS_ERR(ifp)) {
2017 spin_lock_bh(&ifp->lock);
2018 ifp->flags &= ~IFA_F_TENTATIVE;
2019 spin_unlock_bh(&ifp->lock);
2020 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2021 in6_ifa_put(ifp);
2022 }
2023 return;
2024 }
2025
2026 for (dev = dev_base; dev != NULL; dev = dev->next) {
Herbert Xue5ed6392005-10-03 14:35:55 -07002027 struct in_device * in_dev = __in_dev_get_rtnl(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 if (in_dev && (dev->flags & IFF_UP)) {
2029 struct in_ifaddr * ifa;
2030
2031 int flag = scope;
2032
2033 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
2034 int plen;
2035
2036 addr.s6_addr32[3] = ifa->ifa_local;
2037
2038 if (ifa->ifa_scope == RT_SCOPE_LINK)
2039 continue;
2040 if (ifa->ifa_scope >= RT_SCOPE_HOST) {
2041 if (idev->dev->flags&IFF_POINTOPOINT)
2042 continue;
2043 flag |= IFA_HOST;
2044 }
2045 if (idev->dev->flags&IFF_POINTOPOINT)
2046 plen = 64;
2047 else
2048 plen = 96;
2049
2050 ifp = ipv6_add_addr(idev, &addr, plen, flag,
2051 IFA_F_PERMANENT);
2052 if (!IS_ERR(ifp)) {
2053 spin_lock_bh(&ifp->lock);
2054 ifp->flags &= ~IFA_F_TENTATIVE;
2055 spin_unlock_bh(&ifp->lock);
2056 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2057 in6_ifa_put(ifp);
2058 }
2059 }
2060 }
2061 }
2062}
2063
2064static void init_loopback(struct net_device *dev)
2065{
2066 struct inet6_dev *idev;
2067 struct inet6_ifaddr * ifp;
2068
2069 /* ::1 */
2070
2071 ASSERT_RTNL();
2072
2073 if ((idev = ipv6_find_idev(dev)) == NULL) {
2074 printk(KERN_DEBUG "init loopback: add_dev failed\n");
2075 return;
2076 }
2077
2078 ifp = ipv6_add_addr(idev, &in6addr_loopback, 128, IFA_HOST, IFA_F_PERMANENT);
2079 if (!IS_ERR(ifp)) {
2080 spin_lock_bh(&ifp->lock);
2081 ifp->flags &= ~IFA_F_TENTATIVE;
2082 spin_unlock_bh(&ifp->lock);
2083 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2084 in6_ifa_put(ifp);
2085 }
2086}
2087
2088static void addrconf_add_linklocal(struct inet6_dev *idev, struct in6_addr *addr)
2089{
2090 struct inet6_ifaddr * ifp;
2091
2092 ifp = ipv6_add_addr(idev, addr, 64, IFA_LINK, IFA_F_PERMANENT);
2093 if (!IS_ERR(ifp)) {
2094 addrconf_dad_start(ifp, 0);
2095 in6_ifa_put(ifp);
2096 }
2097}
2098
2099static void addrconf_dev_config(struct net_device *dev)
2100{
2101 struct in6_addr addr;
2102 struct inet6_dev * idev;
2103
2104 ASSERT_RTNL();
2105
2106 if ((dev->type != ARPHRD_ETHER) &&
2107 (dev->type != ARPHRD_FDDI) &&
2108 (dev->type != ARPHRD_IEEE802_TR) &&
2109 (dev->type != ARPHRD_ARCNET) &&
2110 (dev->type != ARPHRD_INFINIBAND)) {
2111 /* Alas, we support only Ethernet autoconfiguration. */
2112 return;
2113 }
2114
2115 idev = addrconf_add_dev(dev);
2116 if (idev == NULL)
2117 return;
2118
2119 memset(&addr, 0, sizeof(struct in6_addr));
2120 addr.s6_addr32[0] = htonl(0xFE800000);
2121
2122 if (ipv6_generate_eui64(addr.s6_addr + 8, dev) == 0)
2123 addrconf_add_linklocal(idev, &addr);
2124}
2125
2126static void addrconf_sit_config(struct net_device *dev)
2127{
2128 struct inet6_dev *idev;
2129
2130 ASSERT_RTNL();
2131
2132 /*
2133 * Configure the tunnel with one of our IPv4
2134 * addresses... we should configure all of
2135 * our v4 addrs in the tunnel
2136 */
2137
2138 if ((idev = ipv6_find_idev(dev)) == NULL) {
2139 printk(KERN_DEBUG "init sit: add_dev failed\n");
2140 return;
2141 }
2142
2143 sit_add_v4_addrs(idev);
2144
2145 if (dev->flags&IFF_POINTOPOINT) {
2146 addrconf_add_mroute(dev);
2147 addrconf_add_lroute(dev);
2148 } else
2149 sit_route_add(dev);
2150}
2151
2152static inline int
2153ipv6_inherit_linklocal(struct inet6_dev *idev, struct net_device *link_dev)
2154{
2155 struct in6_addr lladdr;
2156
2157 if (!ipv6_get_lladdr(link_dev, &lladdr)) {
2158 addrconf_add_linklocal(idev, &lladdr);
2159 return 0;
2160 }
2161 return -1;
2162}
2163
2164static void ip6_tnl_add_linklocal(struct inet6_dev *idev)
2165{
2166 struct net_device *link_dev;
2167
2168 /* first try to inherit the link-local address from the link device */
2169 if (idev->dev->iflink &&
2170 (link_dev = __dev_get_by_index(idev->dev->iflink))) {
2171 if (!ipv6_inherit_linklocal(idev, link_dev))
2172 return;
2173 }
2174 /* then try to inherit it from any device */
2175 for (link_dev = dev_base; link_dev; link_dev = link_dev->next) {
2176 if (!ipv6_inherit_linklocal(idev, link_dev))
2177 return;
2178 }
2179 printk(KERN_DEBUG "init ip6-ip6: add_linklocal failed\n");
2180}
2181
2182/*
2183 * Autoconfigure tunnel with a link-local address so routing protocols,
2184 * DHCPv6, MLD etc. can be run over the virtual link
2185 */
2186
2187static void addrconf_ip6_tnl_config(struct net_device *dev)
2188{
2189 struct inet6_dev *idev;
2190
2191 ASSERT_RTNL();
2192
2193 if ((idev = addrconf_add_dev(dev)) == NULL) {
2194 printk(KERN_DEBUG "init ip6-ip6: add_dev failed\n");
2195 return;
2196 }
2197 ip6_tnl_add_linklocal(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198}
2199
2200static int addrconf_notify(struct notifier_block *this, unsigned long event,
2201 void * data)
2202{
2203 struct net_device *dev = (struct net_device *) data;
2204 struct inet6_dev *idev = __in6_dev_get(dev);
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09002205 int run_pending = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206
2207 switch(event) {
2208 case NETDEV_UP:
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002209 case NETDEV_CHANGE:
2210 if (event == NETDEV_UP) {
2211 if (!netif_carrier_ok(dev)) {
2212 /* device is not ready yet. */
2213 printk(KERN_INFO
2214 "ADDRCONF(NETDEV_UP): %s: "
2215 "link is not ready\n",
2216 dev->name);
2217 break;
2218 }
Kristian Slavov99081042006-02-08 16:10:53 -08002219
2220 if (idev)
2221 idev->if_flags |= IF_READY;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002222 } else {
2223 if (!netif_carrier_ok(dev)) {
2224 /* device is still not ready. */
2225 break;
2226 }
2227
2228 if (idev) {
2229 if (idev->if_flags & IF_READY) {
2230 /* device is already configured. */
2231 break;
2232 }
2233 idev->if_flags |= IF_READY;
2234 }
2235
2236 printk(KERN_INFO
2237 "ADDRCONF(NETDEV_CHANGE): %s: "
2238 "link becomes ready\n",
2239 dev->name);
2240
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09002241 run_pending = 1;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002242 }
2243
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 switch(dev->type) {
2245 case ARPHRD_SIT:
2246 addrconf_sit_config(dev);
2247 break;
2248 case ARPHRD_TUNNEL6:
2249 addrconf_ip6_tnl_config(dev);
2250 break;
2251 case ARPHRD_LOOPBACK:
2252 init_loopback(dev);
2253 break;
2254
2255 default:
2256 addrconf_dev_config(dev);
2257 break;
2258 };
2259 if (idev) {
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09002260 if (run_pending)
2261 addrconf_dad_run(idev);
2262
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 /* If the MTU changed during the interface down, when the
2264 interface up, the changed MTU must be reflected in the
2265 idev as well as routers.
2266 */
2267 if (idev->cnf.mtu6 != dev->mtu && dev->mtu >= IPV6_MIN_MTU) {
2268 rt6_mtu_change(dev, dev->mtu);
2269 idev->cnf.mtu6 = dev->mtu;
2270 }
2271 idev->tstamp = jiffies;
2272 inet6_ifinfo_notify(RTM_NEWLINK, idev);
2273 /* If the changed mtu during down is lower than IPV6_MIN_MTU
2274 stop IPv6 on this interface.
2275 */
2276 if (dev->mtu < IPV6_MIN_MTU)
2277 addrconf_ifdown(dev, event != NETDEV_DOWN);
2278 }
2279 break;
2280
2281 case NETDEV_CHANGEMTU:
2282 if ( idev && dev->mtu >= IPV6_MIN_MTU) {
2283 rt6_mtu_change(dev, dev->mtu);
2284 idev->cnf.mtu6 = dev->mtu;
2285 break;
2286 }
2287
2288 /* MTU falled under IPV6_MIN_MTU. Stop IPv6 on this interface. */
2289
2290 case NETDEV_DOWN:
2291 case NETDEV_UNREGISTER:
2292 /*
2293 * Remove all addresses from this interface.
2294 */
2295 addrconf_ifdown(dev, event != NETDEV_DOWN);
2296 break;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002297
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 case NETDEV_CHANGENAME:
2299#ifdef CONFIG_SYSCTL
2300 if (idev) {
2301 addrconf_sysctl_unregister(&idev->cnf);
2302 neigh_sysctl_unregister(idev->nd_parms);
2303 neigh_sysctl_register(dev, idev->nd_parms,
2304 NET_IPV6, NET_IPV6_NEIGH, "ipv6",
2305 &ndisc_ifinfo_sysctl_change,
2306 NULL);
2307 addrconf_sysctl_register(idev, &idev->cnf);
2308 }
2309#endif
2310 break;
2311 };
2312
2313 return NOTIFY_OK;
2314}
2315
2316/*
2317 * addrconf module should be notified of a device going up
2318 */
2319static struct notifier_block ipv6_dev_notf = {
2320 .notifier_call = addrconf_notify,
2321 .priority = 0
2322};
2323
2324static int addrconf_ifdown(struct net_device *dev, int how)
2325{
2326 struct inet6_dev *idev;
2327 struct inet6_ifaddr *ifa, **bifa;
2328 int i;
2329
2330 ASSERT_RTNL();
2331
2332 if (dev == &loopback_dev && how == 1)
2333 how = 0;
2334
2335 rt6_ifdown(dev);
2336 neigh_ifdown(&nd_tbl, dev);
2337
2338 idev = __in6_dev_get(dev);
2339 if (idev == NULL)
2340 return -ENODEV;
2341
2342 /* Step 1: remove reference to ipv6 device from parent device.
2343 Do not dev_put!
2344 */
2345 if (how == 1) {
2346 write_lock_bh(&addrconf_lock);
2347 dev->ip6_ptr = NULL;
2348 idev->dead = 1;
2349 write_unlock_bh(&addrconf_lock);
2350
2351 /* Step 1.5: remove snmp6 entry */
2352 snmp6_unregister_dev(idev);
2353
2354 }
2355
2356 /* Step 2: clear hash table */
2357 for (i=0; i<IN6_ADDR_HSIZE; i++) {
2358 bifa = &inet6_addr_lst[i];
2359
2360 write_lock_bh(&addrconf_hash_lock);
2361 while ((ifa = *bifa) != NULL) {
2362 if (ifa->idev == idev) {
2363 *bifa = ifa->lst_next;
2364 ifa->lst_next = NULL;
2365 addrconf_del_timer(ifa);
2366 in6_ifa_put(ifa);
2367 continue;
2368 }
2369 bifa = &ifa->lst_next;
2370 }
2371 write_unlock_bh(&addrconf_hash_lock);
2372 }
2373
2374 write_lock_bh(&idev->lock);
2375
2376 /* Step 3: clear flags for stateless addrconf */
2377 if (how != 1)
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002378 idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379
2380 /* Step 4: clear address list */
2381#ifdef CONFIG_IPV6_PRIVACY
2382 if (how == 1 && del_timer(&idev->regen_timer))
2383 in6_dev_put(idev);
2384
2385 /* clear tempaddr list */
2386 while ((ifa = idev->tempaddr_list) != NULL) {
2387 idev->tempaddr_list = ifa->tmp_next;
2388 ifa->tmp_next = NULL;
2389 ifa->dead = 1;
2390 write_unlock_bh(&idev->lock);
2391 spin_lock_bh(&ifa->lock);
2392
2393 if (ifa->ifpub) {
2394 in6_ifa_put(ifa->ifpub);
2395 ifa->ifpub = NULL;
2396 }
2397 spin_unlock_bh(&ifa->lock);
2398 in6_ifa_put(ifa);
2399 write_lock_bh(&idev->lock);
2400 }
2401#endif
2402 while ((ifa = idev->addr_list) != NULL) {
2403 idev->addr_list = ifa->if_next;
2404 ifa->if_next = NULL;
2405 ifa->dead = 1;
2406 addrconf_del_timer(ifa);
2407 write_unlock_bh(&idev->lock);
2408
2409 __ipv6_ifa_notify(RTM_DELADDR, ifa);
2410 in6_ifa_put(ifa);
2411
2412 write_lock_bh(&idev->lock);
2413 }
2414 write_unlock_bh(&idev->lock);
2415
2416 /* Step 5: Discard multicast list */
2417
2418 if (how == 1)
2419 ipv6_mc_destroy_dev(idev);
2420 else
2421 ipv6_mc_down(idev);
2422
2423 /* Step 5: netlink notification of this interface */
2424 idev->tstamp = jiffies;
Yan Zheng979ad662005-10-14 18:31:15 +08002425 inet6_ifinfo_notify(RTM_DELLINK, idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426
2427 /* Shot the device (if unregistered) */
2428
2429 if (how == 1) {
2430#ifdef CONFIG_SYSCTL
2431 addrconf_sysctl_unregister(&idev->cnf);
2432 neigh_sysctl_unregister(idev->nd_parms);
2433#endif
2434 neigh_parms_release(&nd_tbl, idev->nd_parms);
2435 neigh_ifdown(&nd_tbl, dev);
2436 in6_dev_put(idev);
2437 }
2438 return 0;
2439}
2440
2441static void addrconf_rs_timer(unsigned long data)
2442{
2443 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
2444
2445 if (ifp->idev->cnf.forwarding)
2446 goto out;
2447
2448 if (ifp->idev->if_flags & IF_RA_RCVD) {
2449 /*
2450 * Announcement received after solicitation
2451 * was sent
2452 */
2453 goto out;
2454 }
2455
2456 spin_lock(&ifp->lock);
2457 if (ifp->probes++ < ifp->idev->cnf.rtr_solicits) {
2458 struct in6_addr all_routers;
2459
2460 /* The wait after the last probe can be shorter */
2461 addrconf_mod_timer(ifp, AC_RS,
2462 (ifp->probes == ifp->idev->cnf.rtr_solicits) ?
2463 ifp->idev->cnf.rtr_solicit_delay :
2464 ifp->idev->cnf.rtr_solicit_interval);
2465 spin_unlock(&ifp->lock);
2466
2467 ipv6_addr_all_routers(&all_routers);
2468
2469 ndisc_send_rs(ifp->idev->dev, &ifp->addr, &all_routers);
2470 } else {
2471 spin_unlock(&ifp->lock);
2472 /*
2473 * Note: we do not support deprecated "all on-link"
2474 * assumption any longer.
2475 */
2476 printk(KERN_DEBUG "%s: no IPv6 routers present\n",
2477 ifp->idev->dev->name);
2478 }
2479
2480out:
2481 in6_ifa_put(ifp);
2482}
2483
2484/*
2485 * Duplicate Address Detection
2486 */
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002487static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
2488{
2489 unsigned long rand_num;
2490 struct inet6_dev *idev = ifp->idev;
2491
2492 rand_num = net_random() % (idev->cnf.rtr_solicit_delay ? : 1);
2493 ifp->probes = idev->cnf.dad_transmits;
2494 addrconf_mod_timer(ifp, AC_DAD, rand_num);
2495}
2496
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -07002497static void addrconf_dad_start(struct inet6_ifaddr *ifp, u32 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498{
2499 struct inet6_dev *idev = ifp->idev;
2500 struct net_device *dev = idev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501
2502 addrconf_join_solict(dev, &ifp->addr);
2503
2504 if (ifp->prefix_len != 128 && (ifp->flags&IFA_F_PERMANENT))
2505 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev, 0,
2506 flags);
2507
2508 net_srandom(ifp->addr.s6_addr32[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509
2510 read_lock_bh(&idev->lock);
2511 if (ifp->dead)
2512 goto out;
2513 spin_lock_bh(&ifp->lock);
2514
2515 if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
2516 !(ifp->flags&IFA_F_TENTATIVE)) {
2517 ifp->flags &= ~IFA_F_TENTATIVE;
2518 spin_unlock_bh(&ifp->lock);
2519 read_unlock_bh(&idev->lock);
2520
2521 addrconf_dad_completed(ifp);
2522 return;
2523 }
2524
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08002525 if (!(idev->if_flags & IF_READY)) {
YOSHIFUJI Hideaki3dd3bf82005-12-23 11:23:21 -08002526 spin_unlock_bh(&ifp->lock);
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08002527 read_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002528 /*
2529 * If the defice is not ready:
2530 * - keep it tentative if it is a permanent address.
2531 * - otherwise, kill it.
2532 */
2533 in6_ifa_hold(ifp);
2534 addrconf_dad_stop(ifp);
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08002535 return;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002536 }
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08002537 addrconf_dad_kick(ifp);
2538 spin_unlock_bh(&ifp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539out:
2540 read_unlock_bh(&idev->lock);
2541}
2542
2543static void addrconf_dad_timer(unsigned long data)
2544{
2545 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
2546 struct inet6_dev *idev = ifp->idev;
2547 struct in6_addr unspec;
2548 struct in6_addr mcaddr;
2549
2550 read_lock_bh(&idev->lock);
2551 if (idev->dead) {
2552 read_unlock_bh(&idev->lock);
2553 goto out;
2554 }
2555 spin_lock_bh(&ifp->lock);
2556 if (ifp->probes == 0) {
2557 /*
2558 * DAD was successful
2559 */
2560
2561 ifp->flags &= ~IFA_F_TENTATIVE;
2562 spin_unlock_bh(&ifp->lock);
2563 read_unlock_bh(&idev->lock);
2564
2565 addrconf_dad_completed(ifp);
2566
2567 goto out;
2568 }
2569
2570 ifp->probes--;
2571 addrconf_mod_timer(ifp, AC_DAD, ifp->idev->nd_parms->retrans_time);
2572 spin_unlock_bh(&ifp->lock);
2573 read_unlock_bh(&idev->lock);
2574
2575 /* send a neighbour solicitation for our addr */
2576 memset(&unspec, 0, sizeof(unspec));
2577 addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
2578 ndisc_send_ns(ifp->idev->dev, NULL, &ifp->addr, &mcaddr, &unspec);
2579out:
2580 in6_ifa_put(ifp);
2581}
2582
2583static void addrconf_dad_completed(struct inet6_ifaddr *ifp)
2584{
2585 struct net_device * dev = ifp->idev->dev;
2586
2587 /*
2588 * Configure the address for reception. Now it is valid.
2589 */
2590
2591 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2592
2593 /* If added prefix is link local and forwarding is off,
2594 start sending router solicitations.
2595 */
2596
2597 if (ifp->idev->cnf.forwarding == 0 &&
2598 ifp->idev->cnf.rtr_solicits > 0 &&
2599 (dev->flags&IFF_LOOPBACK) == 0 &&
2600 (ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL)) {
2601 struct in6_addr all_routers;
2602
2603 ipv6_addr_all_routers(&all_routers);
2604
2605 /*
2606 * If a host as already performed a random delay
2607 * [...] as part of DAD [...] there is no need
2608 * to delay again before sending the first RS
2609 */
2610 ndisc_send_rs(ifp->idev->dev, &ifp->addr, &all_routers);
2611
2612 spin_lock_bh(&ifp->lock);
2613 ifp->probes = 1;
2614 ifp->idev->if_flags |= IF_RS_SENT;
2615 addrconf_mod_timer(ifp, AC_RS, ifp->idev->cnf.rtr_solicit_interval);
2616 spin_unlock_bh(&ifp->lock);
2617 }
2618}
2619
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09002620static void addrconf_dad_run(struct inet6_dev *idev) {
2621 struct inet6_ifaddr *ifp;
2622
2623 read_lock_bh(&idev->lock);
2624 for (ifp = idev->addr_list; ifp; ifp = ifp->if_next) {
2625 spin_lock_bh(&ifp->lock);
2626 if (!(ifp->flags & IFA_F_TENTATIVE)) {
2627 spin_unlock_bh(&ifp->lock);
2628 continue;
2629 }
2630 spin_unlock_bh(&ifp->lock);
2631 addrconf_dad_kick(ifp);
2632 }
2633 read_unlock_bh(&idev->lock);
2634}
2635
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636#ifdef CONFIG_PROC_FS
2637struct if6_iter_state {
2638 int bucket;
2639};
2640
2641static struct inet6_ifaddr *if6_get_first(struct seq_file *seq)
2642{
2643 struct inet6_ifaddr *ifa = NULL;
2644 struct if6_iter_state *state = seq->private;
2645
2646 for (state->bucket = 0; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
2647 ifa = inet6_addr_lst[state->bucket];
2648 if (ifa)
2649 break;
2650 }
2651 return ifa;
2652}
2653
2654static struct inet6_ifaddr *if6_get_next(struct seq_file *seq, struct inet6_ifaddr *ifa)
2655{
2656 struct if6_iter_state *state = seq->private;
2657
2658 ifa = ifa->lst_next;
2659try_again:
2660 if (!ifa && ++state->bucket < IN6_ADDR_HSIZE) {
2661 ifa = inet6_addr_lst[state->bucket];
2662 goto try_again;
2663 }
2664 return ifa;
2665}
2666
2667static struct inet6_ifaddr *if6_get_idx(struct seq_file *seq, loff_t pos)
2668{
2669 struct inet6_ifaddr *ifa = if6_get_first(seq);
2670
2671 if (ifa)
2672 while(pos && (ifa = if6_get_next(seq, ifa)) != NULL)
2673 --pos;
2674 return pos ? NULL : ifa;
2675}
2676
2677static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
2678{
2679 read_lock_bh(&addrconf_hash_lock);
2680 return if6_get_idx(seq, *pos);
2681}
2682
2683static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2684{
2685 struct inet6_ifaddr *ifa;
2686
2687 ifa = if6_get_next(seq, v);
2688 ++*pos;
2689 return ifa;
2690}
2691
2692static void if6_seq_stop(struct seq_file *seq, void *v)
2693{
2694 read_unlock_bh(&addrconf_hash_lock);
2695}
2696
2697static int if6_seq_show(struct seq_file *seq, void *v)
2698{
2699 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
2700 seq_printf(seq,
YOSHIFUJI Hideaki9343e792006-01-17 02:10:53 -08002701 NIP6_SEQFMT " %02x %02x %02x %02x %8s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 NIP6(ifp->addr),
2703 ifp->idev->dev->ifindex,
2704 ifp->prefix_len,
2705 ifp->scope,
2706 ifp->flags,
2707 ifp->idev->dev->name);
2708 return 0;
2709}
2710
2711static struct seq_operations if6_seq_ops = {
2712 .start = if6_seq_start,
2713 .next = if6_seq_next,
2714 .show = if6_seq_show,
2715 .stop = if6_seq_stop,
2716};
2717
2718static int if6_seq_open(struct inode *inode, struct file *file)
2719{
2720 struct seq_file *seq;
2721 int rc = -ENOMEM;
Ingo Oeser322f74a2006-03-20 23:01:47 -08002722 struct if6_iter_state *s = kzalloc(sizeof(*s), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723
2724 if (!s)
2725 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726
2727 rc = seq_open(file, &if6_seq_ops);
2728 if (rc)
2729 goto out_kfree;
2730
2731 seq = file->private_data;
2732 seq->private = s;
2733out:
2734 return rc;
2735out_kfree:
2736 kfree(s);
2737 goto out;
2738}
2739
2740static struct file_operations if6_fops = {
2741 .owner = THIS_MODULE,
2742 .open = if6_seq_open,
2743 .read = seq_read,
2744 .llseek = seq_lseek,
2745 .release = seq_release_private,
2746};
2747
2748int __init if6_proc_init(void)
2749{
2750 if (!proc_net_fops_create("if_inet6", S_IRUGO, &if6_fops))
2751 return -ENOMEM;
2752 return 0;
2753}
2754
2755void if6_proc_exit(void)
2756{
2757 proc_net_remove("if_inet6");
2758}
2759#endif /* CONFIG_PROC_FS */
2760
2761/*
2762 * Periodic address status verification
2763 */
2764
2765static void addrconf_verify(unsigned long foo)
2766{
2767 struct inet6_ifaddr *ifp;
2768 unsigned long now, next;
2769 int i;
2770
2771 spin_lock_bh(&addrconf_verify_lock);
2772 now = jiffies;
2773 next = now + ADDR_CHECK_FREQUENCY;
2774
2775 del_timer(&addr_chk_timer);
2776
2777 for (i=0; i < IN6_ADDR_HSIZE; i++) {
2778
2779restart:
Yan Zheng5d5780d2005-11-20 13:42:20 -08002780 read_lock(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 for (ifp=inet6_addr_lst[i]; ifp; ifp=ifp->lst_next) {
2782 unsigned long age;
2783#ifdef CONFIG_IPV6_PRIVACY
2784 unsigned long regen_advance;
2785#endif
2786
2787 if (ifp->flags & IFA_F_PERMANENT)
2788 continue;
2789
2790 spin_lock(&ifp->lock);
2791 age = (now - ifp->tstamp) / HZ;
2792
2793#ifdef CONFIG_IPV6_PRIVACY
2794 regen_advance = ifp->idev->cnf.regen_max_retry *
2795 ifp->idev->cnf.dad_transmits *
2796 ifp->idev->nd_parms->retrans_time / HZ;
2797#endif
2798
YOSHIFUJI Hideaki8f27ebb2006-07-28 18:12:11 +09002799 if (ifp->valid_lft != INFINITY_LIFE_TIME &&
2800 age >= ifp->valid_lft) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 spin_unlock(&ifp->lock);
2802 in6_ifa_hold(ifp);
Yan Zheng5d5780d2005-11-20 13:42:20 -08002803 read_unlock(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 ipv6_del_addr(ifp);
2805 goto restart;
YOSHIFUJI Hideaki8f27ebb2006-07-28 18:12:11 +09002806 } else if (ifp->prefered_lft == INFINITY_LIFE_TIME) {
2807 spin_unlock(&ifp->lock);
2808 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 } else if (age >= ifp->prefered_lft) {
2810 /* jiffies - ifp->tsamp > age >= ifp->prefered_lft */
2811 int deprecate = 0;
2812
2813 if (!(ifp->flags&IFA_F_DEPRECATED)) {
2814 deprecate = 1;
2815 ifp->flags |= IFA_F_DEPRECATED;
2816 }
2817
2818 if (time_before(ifp->tstamp + ifp->valid_lft * HZ, next))
2819 next = ifp->tstamp + ifp->valid_lft * HZ;
2820
2821 spin_unlock(&ifp->lock);
2822
2823 if (deprecate) {
2824 in6_ifa_hold(ifp);
Yan Zheng5d5780d2005-11-20 13:42:20 -08002825 read_unlock(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826
2827 ipv6_ifa_notify(0, ifp);
2828 in6_ifa_put(ifp);
2829 goto restart;
2830 }
2831#ifdef CONFIG_IPV6_PRIVACY
2832 } else if ((ifp->flags&IFA_F_TEMPORARY) &&
2833 !(ifp->flags&IFA_F_TENTATIVE)) {
2834 if (age >= ifp->prefered_lft - regen_advance) {
2835 struct inet6_ifaddr *ifpub = ifp->ifpub;
2836 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
2837 next = ifp->tstamp + ifp->prefered_lft * HZ;
2838 if (!ifp->regen_count && ifpub) {
2839 ifp->regen_count++;
2840 in6_ifa_hold(ifp);
2841 in6_ifa_hold(ifpub);
2842 spin_unlock(&ifp->lock);
Yan Zheng5d5780d2005-11-20 13:42:20 -08002843 read_unlock(&addrconf_hash_lock);
Hiroyuki YAMAMORI291d8092005-12-23 11:24:05 -08002844 spin_lock(&ifpub->lock);
2845 ifpub->regen_count = 0;
2846 spin_unlock(&ifpub->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 ipv6_create_tempaddr(ifpub, ifp);
2848 in6_ifa_put(ifpub);
2849 in6_ifa_put(ifp);
2850 goto restart;
2851 }
2852 } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
2853 next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
2854 spin_unlock(&ifp->lock);
2855#endif
2856 } else {
2857 /* ifp->prefered_lft <= ifp->valid_lft */
2858 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
2859 next = ifp->tstamp + ifp->prefered_lft * HZ;
2860 spin_unlock(&ifp->lock);
2861 }
2862 }
Yan Zheng5d5780d2005-11-20 13:42:20 -08002863 read_unlock(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 }
2865
2866 addr_chk_timer.expires = time_before(next, jiffies + HZ) ? jiffies + HZ : next;
2867 add_timer(&addr_chk_timer);
2868 spin_unlock_bh(&addrconf_verify_lock);
2869}
2870
Thomas Graf461d8832006-09-18 00:09:49 -07002871static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local)
2872{
2873 struct in6_addr *pfx = NULL;
2874
2875 if (addr)
2876 pfx = nla_data(addr);
2877
2878 if (local) {
2879 if (pfx && nla_memcmp(local, pfx, sizeof(*pfx)))
2880 pfx = NULL;
2881 else
2882 pfx = nla_data(local);
2883 }
2884
2885 return pfx;
2886}
2887
2888static struct nla_policy ifa_ipv6_policy[IFA_MAX+1] __read_mostly = {
2889 [IFA_ADDRESS] = { .len = sizeof(struct in6_addr) },
2890 [IFA_LOCAL] = { .len = sizeof(struct in6_addr) },
2891 [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) },
2892};
2893
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894static int
2895inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
2896{
Thomas Grafb933f712006-09-18 00:10:19 -07002897 struct ifaddrmsg *ifm;
2898 struct nlattr *tb[IFA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 struct in6_addr *pfx;
Thomas Grafb933f712006-09-18 00:10:19 -07002900 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901
Thomas Grafb933f712006-09-18 00:10:19 -07002902 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
2903 if (err < 0)
2904 return err;
2905
2906 ifm = nlmsg_data(nlh);
2907 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 if (pfx == NULL)
2909 return -EINVAL;
2910
2911 return inet6_addr_del(ifm->ifa_index, pfx, ifm->ifa_prefixlen);
2912}
2913
2914static int
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09002915inet6_addr_modify(int ifindex, struct in6_addr *pfx,
2916 __u32 prefered_lft, __u32 valid_lft)
2917{
2918 struct inet6_ifaddr *ifp = NULL;
2919 struct net_device *dev;
2920 int ifa_flags = 0;
2921
2922 if ((dev = __dev_get_by_index(ifindex)) == NULL)
2923 return -ENODEV;
2924
2925 if (!(dev->flags&IFF_UP))
2926 return -ENETDOWN;
2927
2928 if (!valid_lft || (prefered_lft > valid_lft))
2929 return -EINVAL;
2930
2931 ifp = ipv6_get_ifaddr(pfx, dev, 1);
2932 if (ifp == NULL)
2933 return -ENOENT;
2934
2935 if (valid_lft == INFINITY_LIFE_TIME)
2936 ifa_flags = IFA_F_PERMANENT;
2937 else if (valid_lft >= 0x7FFFFFFF/HZ)
2938 valid_lft = 0x7FFFFFFF/HZ;
2939
2940 if (prefered_lft == 0)
2941 ifa_flags = IFA_F_DEPRECATED;
2942 else if ((prefered_lft >= 0x7FFFFFFF/HZ) &&
2943 (prefered_lft != INFINITY_LIFE_TIME))
2944 prefered_lft = 0x7FFFFFFF/HZ;
2945
2946 spin_lock_bh(&ifp->lock);
2947 ifp->flags = (ifp->flags & ~(IFA_F_DEPRECATED|IFA_F_PERMANENT)) | ifa_flags;
2948
2949 ifp->tstamp = jiffies;
2950 ifp->valid_lft = valid_lft;
2951 ifp->prefered_lft = prefered_lft;
2952
2953 spin_unlock_bh(&ifp->lock);
2954 if (!(ifp->flags&IFA_F_TENTATIVE))
2955 ipv6_ifa_notify(0, ifp);
2956 in6_ifa_put(ifp);
2957
2958 addrconf_verify(0);
2959
2960 return 0;
2961}
2962
2963static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
2965{
Thomas Graf461d8832006-09-18 00:09:49 -07002966 struct ifaddrmsg *ifm;
2967 struct nlattr *tb[IFA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 struct in6_addr *pfx;
Thomas Graf461d8832006-09-18 00:09:49 -07002969 u32 valid_lft, preferred_lft;
2970 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971
Thomas Graf461d8832006-09-18 00:09:49 -07002972 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
2973 if (err < 0)
2974 return err;
2975
2976 ifm = nlmsg_data(nlh);
2977 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 if (pfx == NULL)
2979 return -EINVAL;
2980
Thomas Graf461d8832006-09-18 00:09:49 -07002981 if (tb[IFA_CACHEINFO]) {
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002982 struct ifa_cacheinfo *ci;
Thomas Graf461d8832006-09-18 00:09:49 -07002983
2984 ci = nla_data(tb[IFA_CACHEINFO]);
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002985 valid_lft = ci->ifa_valid;
Thomas Graf461d8832006-09-18 00:09:49 -07002986 preferred_lft = ci->ifa_prefered;
2987 } else {
2988 preferred_lft = INFINITY_LIFE_TIME;
2989 valid_lft = INFINITY_LIFE_TIME;
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002990 }
2991
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09002992 if (nlh->nlmsg_flags & NLM_F_REPLACE) {
Thomas Graf461d8832006-09-18 00:09:49 -07002993 err = inet6_addr_modify(ifm->ifa_index, pfx,
2994 preferred_lft, valid_lft);
2995 if (err == 0 || !(nlh->nlmsg_flags & NLM_F_CREATE))
2996 return err;
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09002997 }
2998
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002999 return inet6_addr_add(ifm->ifa_index, pfx, ifm->ifa_prefixlen,
Thomas Graf461d8832006-09-18 00:09:49 -07003000 preferred_lft, valid_lft);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001}
3002
Thomas Graf101bb222006-09-18 00:11:52 -07003003static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u8 flags,
3004 u8 scope, int ifindex)
3005{
3006 struct ifaddrmsg *ifm;
3007
3008 ifm = nlmsg_data(nlh);
3009 ifm->ifa_family = AF_INET6;
3010 ifm->ifa_prefixlen = prefixlen;
3011 ifm->ifa_flags = flags;
3012 ifm->ifa_scope = scope;
3013 ifm->ifa_index = ifindex;
3014}
3015
Thomas Graf85486af2006-09-18 00:11:24 -07003016static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
3017 unsigned long tstamp, u32 preferred, u32 valid)
3018{
3019 struct ifa_cacheinfo ci;
3020
3021 ci.cstamp = (u32)(TIME_DELTA(cstamp, INITIAL_JIFFIES) / HZ * 100
3022 + TIME_DELTA(cstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
3023 ci.tstamp = (u32)(TIME_DELTA(tstamp, INITIAL_JIFFIES) / HZ * 100
3024 + TIME_DELTA(tstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
3025 ci.ifa_prefered = preferred;
3026 ci.ifa_valid = valid;
3027
3028 return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
3029}
3030
Thomas Graf101bb222006-09-18 00:11:52 -07003031static inline int rt_scope(int ifa_scope)
3032{
3033 if (ifa_scope & IFA_HOST)
3034 return RT_SCOPE_HOST;
3035 else if (ifa_scope & IFA_LINK)
3036 return RT_SCOPE_LINK;
3037 else if (ifa_scope & IFA_SITE)
3038 return RT_SCOPE_SITE;
3039 else
3040 return RT_SCOPE_UNIVERSE;
3041}
3042
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07003043/* Maximum length of ifa_cacheinfo attributes */
3044#define INET6_IFADDR_RTA_SPACE \
3045 RTA_SPACE(16) /* IFA_ADDRESS */ + \
3046 RTA_SPACE(sizeof(struct ifa_cacheinfo)) /* CACHEINFO */
3047
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07003049 u32 pid, u32 seq, int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052 unsigned char *b = skb->tail;
Thomas Graf85486af2006-09-18 00:11:24 -07003053 u32 preferred, valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054
Thomas Graf101bb222006-09-18 00:11:52 -07003055 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags);
3056 put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope),
3057 ifa->idev->dev->ifindex);
3058
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 RTA_PUT(skb, IFA_ADDRESS, 16, &ifa->addr);
3060 if (!(ifa->flags&IFA_F_PERMANENT)) {
Thomas Graf85486af2006-09-18 00:11:24 -07003061 preferred = ifa->prefered_lft;
3062 valid = ifa->valid_lft;
3063 if (preferred != INFINITY_LIFE_TIME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 long tval = (jiffies - ifa->tstamp)/HZ;
Thomas Graf85486af2006-09-18 00:11:24 -07003065 preferred -= tval;
3066 if (valid != INFINITY_LIFE_TIME)
3067 valid -= tval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 }
3069 } else {
Thomas Graf85486af2006-09-18 00:11:24 -07003070 preferred = INFINITY_LIFE_TIME;
3071 valid = INFINITY_LIFE_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 }
Thomas Graf85486af2006-09-18 00:11:24 -07003073
3074 if (put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0)
3075 goto rtattr_failure;
3076
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 nlh->nlmsg_len = skb->tail - b;
3078 return skb->len;
3079
3080nlmsg_failure:
3081rtattr_failure:
3082 skb_trim(skb, b - skb->data);
3083 return -1;
3084}
3085
3086static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -07003087 u32 pid, u32 seq, int event, u16 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 unsigned char *b = skb->tail;
Thomas Graf101bb222006-09-18 00:11:52 -07003091 u8 scope = RT_SCOPE_UNIVERSE;
3092 int ifindex = ifmca->idev->dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093
Thomas Graf101bb222006-09-18 00:11:52 -07003094 if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE)
3095 scope = RT_SCOPE_SITE;
3096
3097 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags);
3098 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 RTA_PUT(skb, IFA_MULTICAST, 16, &ifmca->mca_addr);
Thomas Graf85486af2006-09-18 00:11:24 -07003100
3101 if (put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp,
3102 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0)
3103 goto rtattr_failure;
3104
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105 nlh->nlmsg_len = skb->tail - b;
3106 return skb->len;
3107
3108nlmsg_failure:
3109rtattr_failure:
3110 skb_trim(skb, b - skb->data);
3111 return -1;
3112}
3113
3114static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07003115 u32 pid, u32 seq, int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118 unsigned char *b = skb->tail;
Thomas Graf101bb222006-09-18 00:11:52 -07003119 u8 scope = RT_SCOPE_UNIVERSE;
3120 int ifindex = ifaca->aca_idev->dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121
Thomas Graf101bb222006-09-18 00:11:52 -07003122 if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE)
3123 scope = RT_SCOPE_SITE;
3124
3125 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags);
3126 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 RTA_PUT(skb, IFA_ANYCAST, 16, &ifaca->aca_addr);
Thomas Graf85486af2006-09-18 00:11:24 -07003128
3129 if (put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp,
3130 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0)
3131 goto rtattr_failure;
3132
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 nlh->nlmsg_len = skb->tail - b;
3134 return skb->len;
3135
3136nlmsg_failure:
3137rtattr_failure:
3138 skb_trim(skb, b - skb->data);
3139 return -1;
3140}
3141
3142enum addr_type_t
3143{
3144 UNICAST_ADDR,
3145 MULTICAST_ADDR,
3146 ANYCAST_ADDR,
3147};
3148
3149static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
3150 enum addr_type_t type)
3151{
3152 int idx, ip_idx;
3153 int s_idx, s_ip_idx;
3154 int err = 1;
3155 struct net_device *dev;
3156 struct inet6_dev *idev = NULL;
3157 struct inet6_ifaddr *ifa;
3158 struct ifmcaddr6 *ifmca;
3159 struct ifacaddr6 *ifaca;
3160
3161 s_idx = cb->args[0];
3162 s_ip_idx = ip_idx = cb->args[1];
3163 read_lock(&dev_base_lock);
3164
3165 for (dev = dev_base, idx = 0; dev; dev = dev->next, idx++) {
3166 if (idx < s_idx)
3167 continue;
3168 if (idx > s_idx)
3169 s_ip_idx = 0;
3170 ip_idx = 0;
3171 if ((idev = in6_dev_get(dev)) == NULL)
3172 continue;
3173 read_lock_bh(&idev->lock);
3174 switch (type) {
3175 case UNICAST_ADDR:
YOSHIFUJI Hideakiae9cda52005-06-28 13:00:30 -07003176 /* unicast address incl. temp addr */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177 for (ifa = idev->addr_list; ifa;
3178 ifa = ifa->if_next, ip_idx++) {
3179 if (ip_idx < s_ip_idx)
3180 continue;
3181 if ((err = inet6_fill_ifaddr(skb, ifa,
3182 NETLINK_CB(cb->skb).pid,
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07003183 cb->nlh->nlmsg_seq, RTM_NEWADDR,
3184 NLM_F_MULTI)) <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185 goto done;
3186 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 break;
3188 case MULTICAST_ADDR:
3189 /* multicast address */
3190 for (ifmca = idev->mc_list; ifmca;
3191 ifmca = ifmca->next, ip_idx++) {
3192 if (ip_idx < s_ip_idx)
3193 continue;
3194 if ((err = inet6_fill_ifmcaddr(skb, ifmca,
3195 NETLINK_CB(cb->skb).pid,
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07003196 cb->nlh->nlmsg_seq, RTM_GETMULTICAST,
3197 NLM_F_MULTI)) <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 goto done;
3199 }
3200 break;
3201 case ANYCAST_ADDR:
3202 /* anycast address */
3203 for (ifaca = idev->ac_list; ifaca;
3204 ifaca = ifaca->aca_next, ip_idx++) {
3205 if (ip_idx < s_ip_idx)
3206 continue;
3207 if ((err = inet6_fill_ifacaddr(skb, ifaca,
3208 NETLINK_CB(cb->skb).pid,
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07003209 cb->nlh->nlmsg_seq, RTM_GETANYCAST,
3210 NLM_F_MULTI)) <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211 goto done;
3212 }
3213 break;
3214 default:
3215 break;
3216 }
3217 read_unlock_bh(&idev->lock);
3218 in6_dev_put(idev);
3219 }
3220done:
3221 if (err <= 0) {
3222 read_unlock_bh(&idev->lock);
3223 in6_dev_put(idev);
3224 }
3225 read_unlock(&dev_base_lock);
3226 cb->args[0] = idx;
3227 cb->args[1] = ip_idx;
3228 return skb->len;
3229}
3230
3231static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
3232{
3233 enum addr_type_t type = UNICAST_ADDR;
3234 return inet6_dump_addr(skb, cb, type);
3235}
3236
3237static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
3238{
3239 enum addr_type_t type = MULTICAST_ADDR;
3240 return inet6_dump_addr(skb, cb, type);
3241}
3242
3243
3244static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
3245{
3246 enum addr_type_t type = ANYCAST_ADDR;
3247 return inet6_dump_addr(skb, cb, type);
3248}
3249
Thomas Graf1b29fc22006-09-18 00:10:50 -07003250static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr* nlh,
3251 void *arg)
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003252{
Thomas Graf1b29fc22006-09-18 00:10:50 -07003253 struct ifaddrmsg *ifm;
3254 struct nlattr *tb[IFA_MAX+1];
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003255 struct in6_addr *addr = NULL;
3256 struct net_device *dev = NULL;
3257 struct inet6_ifaddr *ifa;
3258 struct sk_buff *skb;
Thomas Graf1b29fc22006-09-18 00:10:50 -07003259 int payload = sizeof(struct ifaddrmsg) + INET6_IFADDR_RTA_SPACE;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003260 int err;
3261
Thomas Graf1b29fc22006-09-18 00:10:50 -07003262 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
3263 if (err < 0)
3264 goto errout;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003265
Thomas Graf1b29fc22006-09-18 00:10:50 -07003266 addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
3267 if (addr == NULL) {
3268 err = -EINVAL;
3269 goto errout;
3270 }
3271
3272 ifm = nlmsg_data(nlh);
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003273 if (ifm->ifa_index)
3274 dev = __dev_get_by_index(ifm->ifa_index);
3275
Thomas Graf1b29fc22006-09-18 00:10:50 -07003276 if ((ifa = ipv6_get_ifaddr(addr, dev, 1)) == NULL) {
3277 err = -EADDRNOTAVAIL;
3278 goto errout;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003279 }
3280
Thomas Graf1b29fc22006-09-18 00:10:50 -07003281 if ((skb = nlmsg_new(nlmsg_total_size(payload), GFP_KERNEL)) == NULL) {
3282 err = -ENOBUFS;
3283 goto errout_ifa;
3284 }
3285
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003286 err = inet6_fill_ifaddr(skb, ifa, NETLINK_CB(in_skb).pid,
3287 nlh->nlmsg_seq, RTM_NEWADDR, 0);
3288 if (err < 0) {
Thomas Graf1b29fc22006-09-18 00:10:50 -07003289 kfree_skb(skb);
3290 goto errout_ifa;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003291 }
3292
Thomas Graf2942e902006-08-15 00:30:25 -07003293 err = rtnl_unicast(skb, NETLINK_CB(in_skb).pid);
Thomas Graf1b29fc22006-09-18 00:10:50 -07003294errout_ifa:
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003295 in6_ifa_put(ifa);
Thomas Graf1b29fc22006-09-18 00:10:50 -07003296errout:
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003297 return err;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003298}
3299
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
3301{
3302 struct sk_buff *skb;
Thomas Graf5d620262006-08-15 00:35:02 -07003303 int payload = sizeof(struct ifaddrmsg) + INET6_IFADDR_RTA_SPACE;
3304 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305
Thomas Graf5d620262006-08-15 00:35:02 -07003306 skb = nlmsg_new(nlmsg_total_size(payload), GFP_ATOMIC);
3307 if (skb == NULL)
3308 goto errout;
3309
3310 err = inet6_fill_ifaddr(skb, ifa, 0, 0, event, 0);
3311 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312 kfree_skb(skb);
Thomas Graf5d620262006-08-15 00:35:02 -07003313 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314 }
Thomas Graf5d620262006-08-15 00:35:02 -07003315
3316 err = rtnl_notify(skb, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
3317errout:
3318 if (err < 0)
3319 rtnl_set_sk_err(RTNLGRP_IPV6_IFADDR, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320}
3321
3322static void inline ipv6_store_devconf(struct ipv6_devconf *cnf,
3323 __s32 *array, int bytes)
3324{
3325 memset(array, 0, bytes);
3326 array[DEVCONF_FORWARDING] = cnf->forwarding;
3327 array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
3328 array[DEVCONF_MTU6] = cnf->mtu6;
3329 array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
3330 array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
3331 array[DEVCONF_AUTOCONF] = cnf->autoconf;
3332 array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
3333 array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
3334 array[DEVCONF_RTR_SOLICIT_INTERVAL] = cnf->rtr_solicit_interval;
3335 array[DEVCONF_RTR_SOLICIT_DELAY] = cnf->rtr_solicit_delay;
3336 array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
3337#ifdef CONFIG_IPV6_PRIVACY
3338 array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
3339 array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
3340 array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
3341 array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
3342 array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
3343#endif
3344 array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -08003345 array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr;
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -08003346 array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo;
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08003347#ifdef CONFIG_IPV6_ROUTER_PREF
3348 array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref;
YOSHIFUJI Hideaki52e16352006-03-20 17:05:47 -08003349 array[DEVCONF_RTR_PROBE_INTERVAL] = cnf->rtr_probe_interval;
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -08003350#ifdef CONFIV_IPV6_ROUTE_INFO
3351 array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen;
3352#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08003353#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354}
3355
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07003356/* Maximum length of ifinfomsg attributes */
3357#define INET6_IFINFO_RTA_SPACE \
3358 RTA_SPACE(IFNAMSIZ) /* IFNAME */ + \
3359 RTA_SPACE(MAX_ADDR_LEN) /* ADDRESS */ + \
3360 RTA_SPACE(sizeof(u32)) /* MTU */ + \
3361 RTA_SPACE(sizeof(int)) /* LINK */ + \
3362 RTA_SPACE(0) /* PROTINFO */ + \
3363 RTA_SPACE(sizeof(u32)) /* FLAGS */ + \
3364 RTA_SPACE(sizeof(struct ifla_cacheinfo)) /* CACHEINFO */ + \
3365 RTA_SPACE(sizeof(__s32[DEVCONF_MAX])) /* CONF */
3366
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07003368 u32 pid, u32 seq, int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369{
3370 struct net_device *dev = idev->dev;
3371 __s32 *array = NULL;
3372 struct ifinfomsg *r;
3373 struct nlmsghdr *nlh;
3374 unsigned char *b = skb->tail;
3375 struct rtattr *subattr;
3376 __u32 mtu = dev->mtu;
3377 struct ifla_cacheinfo ci;
3378
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07003379 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*r), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380 r = NLMSG_DATA(nlh);
3381 r->ifi_family = AF_INET6;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -07003382 r->__ifi_pad = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383 r->ifi_type = dev->type;
3384 r->ifi_index = dev->ifindex;
3385 r->ifi_flags = dev_get_flags(dev);
3386 r->ifi_change = 0;
3387
3388 RTA_PUT(skb, IFLA_IFNAME, strlen(dev->name)+1, dev->name);
3389
3390 if (dev->addr_len)
3391 RTA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr);
3392
3393 RTA_PUT(skb, IFLA_MTU, sizeof(mtu), &mtu);
3394 if (dev->ifindex != dev->iflink)
3395 RTA_PUT(skb, IFLA_LINK, sizeof(int), &dev->iflink);
3396
3397 subattr = (struct rtattr*)skb->tail;
3398
3399 RTA_PUT(skb, IFLA_PROTINFO, 0, NULL);
3400
3401 /* return the device flags */
3402 RTA_PUT(skb, IFLA_INET6_FLAGS, sizeof(__u32), &idev->if_flags);
3403
3404 /* return interface cacheinfo */
3405 ci.max_reasm_len = IPV6_MAXPLEN;
3406 ci.tstamp = (__u32)(TIME_DELTA(idev->tstamp, INITIAL_JIFFIES) / HZ * 100
3407 + TIME_DELTA(idev->tstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
3408 ci.reachable_time = idev->nd_parms->reachable_time;
3409 ci.retrans_time = idev->nd_parms->retrans_time;
3410 RTA_PUT(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci);
3411
3412 /* return the device sysctl params */
3413 if ((array = kmalloc(DEVCONF_MAX * sizeof(*array), GFP_ATOMIC)) == NULL)
3414 goto rtattr_failure;
3415 ipv6_store_devconf(&idev->cnf, array, DEVCONF_MAX * sizeof(*array));
3416 RTA_PUT(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(*array), array);
3417
3418 /* XXX - Statistics/MC not implemented */
3419 subattr->rta_len = skb->tail - (u8*)subattr;
3420
3421 nlh->nlmsg_len = skb->tail - b;
3422 kfree(array);
3423 return skb->len;
3424
3425nlmsg_failure:
3426rtattr_failure:
Jesper Juhla51482b2005-11-08 09:41:34 -08003427 kfree(array);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428 skb_trim(skb, b - skb->data);
3429 return -1;
3430}
3431
3432static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
3433{
3434 int idx, err;
3435 int s_idx = cb->args[0];
3436 struct net_device *dev;
3437 struct inet6_dev *idev;
3438
3439 read_lock(&dev_base_lock);
3440 for (dev=dev_base, idx=0; dev; dev = dev->next, idx++) {
3441 if (idx < s_idx)
3442 continue;
3443 if ((idev = in6_dev_get(dev)) == NULL)
3444 continue;
3445 err = inet6_fill_ifinfo(skb, idev, NETLINK_CB(cb->skb).pid,
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07003446 cb->nlh->nlmsg_seq, RTM_NEWLINK, NLM_F_MULTI);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447 in6_dev_put(idev);
3448 if (err <= 0)
3449 break;
3450 }
3451 read_unlock(&dev_base_lock);
3452 cb->args[0] = idx;
3453
3454 return skb->len;
3455}
3456
3457void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
3458{
3459 struct sk_buff *skb;
Thomas Graf8d7a76c2006-08-15 00:35:47 -07003460 int payload = sizeof(struct ifinfomsg) + INET6_IFINFO_RTA_SPACE;
3461 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462
Thomas Graf8d7a76c2006-08-15 00:35:47 -07003463 skb = nlmsg_new(nlmsg_total_size(payload), GFP_ATOMIC);
3464 if (skb == NULL)
3465 goto errout;
3466
3467 err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0);
3468 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469 kfree_skb(skb);
Thomas Graf8d7a76c2006-08-15 00:35:47 -07003470 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471 }
Thomas Graf8d7a76c2006-08-15 00:35:47 -07003472
3473 err = rtnl_notify(skb, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
3474errout:
3475 if (err < 0)
3476 rtnl_set_sk_err(RTNLGRP_IPV6_IFADDR, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477}
3478
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07003479/* Maximum length of prefix_cacheinfo attributes */
3480#define INET6_PREFIX_RTA_SPACE \
3481 RTA_SPACE(sizeof(((struct prefix_info *)NULL)->prefix)) /* ADDRESS */ + \
3482 RTA_SPACE(sizeof(struct prefix_cacheinfo)) /* CACHEINFO */
3483
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07003485 struct prefix_info *pinfo, u32 pid, u32 seq,
3486 int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487{
3488 struct prefixmsg *pmsg;
3489 struct nlmsghdr *nlh;
3490 unsigned char *b = skb->tail;
3491 struct prefix_cacheinfo ci;
3492
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07003493 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*pmsg), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494 pmsg = NLMSG_DATA(nlh);
3495 pmsg->prefix_family = AF_INET6;
Patrick McHardy8a470772005-06-28 12:56:45 -07003496 pmsg->prefix_pad1 = 0;
3497 pmsg->prefix_pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498 pmsg->prefix_ifindex = idev->dev->ifindex;
3499 pmsg->prefix_len = pinfo->prefix_len;
3500 pmsg->prefix_type = pinfo->type;
Patrick McHardy8a470772005-06-28 12:56:45 -07003501 pmsg->prefix_pad3 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502
3503 pmsg->prefix_flags = 0;
3504 if (pinfo->onlink)
3505 pmsg->prefix_flags |= IF_PREFIX_ONLINK;
3506 if (pinfo->autoconf)
3507 pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
3508
3509 RTA_PUT(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix);
3510
3511 ci.preferred_time = ntohl(pinfo->prefered);
3512 ci.valid_time = ntohl(pinfo->valid);
3513 RTA_PUT(skb, PREFIX_CACHEINFO, sizeof(ci), &ci);
3514
3515 nlh->nlmsg_len = skb->tail - b;
3516 return skb->len;
3517
3518nlmsg_failure:
3519rtattr_failure:
3520 skb_trim(skb, b - skb->data);
3521 return -1;
3522}
3523
3524static void inet6_prefix_notify(int event, struct inet6_dev *idev,
3525 struct prefix_info *pinfo)
3526{
3527 struct sk_buff *skb;
Thomas Graf8c384bfa2006-08-15 00:36:07 -07003528 int payload = sizeof(struct prefixmsg) + INET6_PREFIX_RTA_SPACE;
3529 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530
Thomas Graf8c384bfa2006-08-15 00:36:07 -07003531 skb = nlmsg_new(nlmsg_total_size(payload), GFP_ATOMIC);
3532 if (skb == NULL)
3533 goto errout;
3534
3535 err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0);
3536 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537 kfree_skb(skb);
Thomas Graf8c384bfa2006-08-15 00:36:07 -07003538 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539 }
Thomas Graf8c384bfa2006-08-15 00:36:07 -07003540
3541 err = rtnl_notify(skb, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC);
3542errout:
3543 if (err < 0)
3544 rtnl_set_sk_err(RTNLGRP_IPV6_PREFIX, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545}
3546
Thomas Grafdb46edc2005-05-03 14:29:39 -07003547static struct rtnetlink_link inet6_rtnetlink_table[RTM_NR_MSGTYPES] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548 [RTM_GETLINK - RTM_BASE] = { .dumpit = inet6_dump_ifinfo, },
3549 [RTM_NEWADDR - RTM_BASE] = { .doit = inet6_rtm_newaddr, },
3550 [RTM_DELADDR - RTM_BASE] = { .doit = inet6_rtm_deladdr, },
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003551 [RTM_GETADDR - RTM_BASE] = { .doit = inet6_rtm_getaddr,
3552 .dumpit = inet6_dump_ifaddr, },
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553 [RTM_GETMULTICAST - RTM_BASE] = { .dumpit = inet6_dump_ifmcaddr, },
3554 [RTM_GETANYCAST - RTM_BASE] = { .dumpit = inet6_dump_ifacaddr, },
3555 [RTM_NEWROUTE - RTM_BASE] = { .doit = inet6_rtm_newroute, },
3556 [RTM_DELROUTE - RTM_BASE] = { .doit = inet6_rtm_delroute, },
3557 [RTM_GETROUTE - RTM_BASE] = { .doit = inet6_rtm_getroute,
3558 .dumpit = inet6_dump_fib, },
David S. Miller8423a9a2006-08-07 21:54:37 -07003559#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Thomas Graf101367c2006-08-04 03:39:02 -07003560 [RTM_GETRULE - RTM_BASE] = { .dumpit = fib6_rules_dump, },
David S. Miller8423a9a2006-08-07 21:54:37 -07003561#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562};
3563
3564static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
3565{
3566 inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
3567
3568 switch (event) {
3569 case RTM_NEWADDR:
Thomas Graf40e22e82006-08-22 00:00:45 -07003570 ip6_ins_rt(ifp->rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571 if (ifp->idev->cnf.forwarding)
3572 addrconf_join_anycast(ifp);
3573 break;
3574 case RTM_DELADDR:
3575 if (ifp->idev->cnf.forwarding)
3576 addrconf_leave_anycast(ifp);
3577 addrconf_leave_solict(ifp->idev, &ifp->addr);
3578 dst_hold(&ifp->rt->u.dst);
Thomas Grafe0a1ad732006-08-22 00:00:21 -07003579 if (ip6_del_rt(ifp->rt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580 dst_free(&ifp->rt->u.dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581 break;
3582 }
3583}
3584
3585static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
3586{
3587 read_lock_bh(&addrconf_lock);
3588 if (likely(ifp->idev->dead == 0))
3589 __ipv6_ifa_notify(event, ifp);
3590 read_unlock_bh(&addrconf_lock);
3591}
3592
3593#ifdef CONFIG_SYSCTL
3594
3595static
3596int addrconf_sysctl_forward(ctl_table *ctl, int write, struct file * filp,
3597 void __user *buffer, size_t *lenp, loff_t *ppos)
3598{
3599 int *valp = ctl->data;
3600 int val = *valp;
3601 int ret;
3602
3603 ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
3604
3605 if (write && valp != &ipv6_devconf_dflt.forwarding) {
3606 if (valp != &ipv6_devconf.forwarding) {
3607 if ((!*valp) ^ (!val)) {
3608 struct inet6_dev *idev = (struct inet6_dev *)ctl->extra1;
3609 if (idev == NULL)
3610 return ret;
3611 dev_forward_change(idev);
3612 }
3613 } else {
3614 ipv6_devconf_dflt.forwarding = ipv6_devconf.forwarding;
3615 addrconf_forward_change();
3616 }
3617 if (*valp)
3618 rt6_purge_dflt_routers();
3619 }
3620
3621 return ret;
3622}
3623
3624static int addrconf_sysctl_forward_strategy(ctl_table *table,
3625 int __user *name, int nlen,
3626 void __user *oldval,
3627 size_t __user *oldlenp,
3628 void __user *newval, size_t newlen,
3629 void **context)
3630{
3631 int *valp = table->data;
3632 int new;
3633
3634 if (!newval || !newlen)
3635 return 0;
3636 if (newlen != sizeof(int))
3637 return -EINVAL;
3638 if (get_user(new, (int __user *)newval))
3639 return -EFAULT;
3640 if (new == *valp)
3641 return 0;
3642 if (oldval && oldlenp) {
3643 size_t len;
3644 if (get_user(len, oldlenp))
3645 return -EFAULT;
3646 if (len) {
3647 if (len > table->maxlen)
3648 len = table->maxlen;
3649 if (copy_to_user(oldval, valp, len))
3650 return -EFAULT;
3651 if (put_user(len, oldlenp))
3652 return -EFAULT;
3653 }
3654 }
3655
3656 if (valp != &ipv6_devconf_dflt.forwarding) {
3657 if (valp != &ipv6_devconf.forwarding) {
3658 struct inet6_dev *idev = (struct inet6_dev *)table->extra1;
3659 int changed;
3660 if (unlikely(idev == NULL))
3661 return -ENODEV;
3662 changed = (!*valp) ^ (!new);
3663 *valp = new;
3664 if (changed)
3665 dev_forward_change(idev);
3666 } else {
3667 *valp = new;
3668 addrconf_forward_change();
3669 }
3670
3671 if (*valp)
3672 rt6_purge_dflt_routers();
3673 } else
3674 *valp = new;
3675
3676 return 1;
3677}
3678
3679static struct addrconf_sysctl_table
3680{
3681 struct ctl_table_header *sysctl_header;
3682 ctl_table addrconf_vars[__NET_IPV6_MAX];
3683 ctl_table addrconf_dev[2];
3684 ctl_table addrconf_conf_dir[2];
3685 ctl_table addrconf_proto_dir[2];
3686 ctl_table addrconf_root_dir[2];
Brian Haleyab32ea52006-09-22 14:15:41 -07003687} addrconf_sysctl __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688 .sysctl_header = NULL,
3689 .addrconf_vars = {
3690 {
3691 .ctl_name = NET_IPV6_FORWARDING,
3692 .procname = "forwarding",
3693 .data = &ipv6_devconf.forwarding,
3694 .maxlen = sizeof(int),
3695 .mode = 0644,
3696 .proc_handler = &addrconf_sysctl_forward,
3697 .strategy = &addrconf_sysctl_forward_strategy,
3698 },
3699 {
3700 .ctl_name = NET_IPV6_HOP_LIMIT,
3701 .procname = "hop_limit",
3702 .data = &ipv6_devconf.hop_limit,
3703 .maxlen = sizeof(int),
3704 .mode = 0644,
3705 .proc_handler = proc_dointvec,
3706 },
3707 {
3708 .ctl_name = NET_IPV6_MTU,
3709 .procname = "mtu",
3710 .data = &ipv6_devconf.mtu6,
3711 .maxlen = sizeof(int),
3712 .mode = 0644,
3713 .proc_handler = &proc_dointvec,
3714 },
3715 {
3716 .ctl_name = NET_IPV6_ACCEPT_RA,
3717 .procname = "accept_ra",
3718 .data = &ipv6_devconf.accept_ra,
3719 .maxlen = sizeof(int),
3720 .mode = 0644,
3721 .proc_handler = &proc_dointvec,
3722 },
3723 {
3724 .ctl_name = NET_IPV6_ACCEPT_REDIRECTS,
3725 .procname = "accept_redirects",
3726 .data = &ipv6_devconf.accept_redirects,
3727 .maxlen = sizeof(int),
3728 .mode = 0644,
3729 .proc_handler = &proc_dointvec,
3730 },
3731 {
3732 .ctl_name = NET_IPV6_AUTOCONF,
3733 .procname = "autoconf",
3734 .data = &ipv6_devconf.autoconf,
3735 .maxlen = sizeof(int),
3736 .mode = 0644,
3737 .proc_handler = &proc_dointvec,
3738 },
3739 {
3740 .ctl_name = NET_IPV6_DAD_TRANSMITS,
3741 .procname = "dad_transmits",
3742 .data = &ipv6_devconf.dad_transmits,
3743 .maxlen = sizeof(int),
3744 .mode = 0644,
3745 .proc_handler = &proc_dointvec,
3746 },
3747 {
3748 .ctl_name = NET_IPV6_RTR_SOLICITS,
3749 .procname = "router_solicitations",
3750 .data = &ipv6_devconf.rtr_solicits,
3751 .maxlen = sizeof(int),
3752 .mode = 0644,
3753 .proc_handler = &proc_dointvec,
3754 },
3755 {
3756 .ctl_name = NET_IPV6_RTR_SOLICIT_INTERVAL,
3757 .procname = "router_solicitation_interval",
3758 .data = &ipv6_devconf.rtr_solicit_interval,
3759 .maxlen = sizeof(int),
3760 .mode = 0644,
3761 .proc_handler = &proc_dointvec_jiffies,
3762 .strategy = &sysctl_jiffies,
3763 },
3764 {
3765 .ctl_name = NET_IPV6_RTR_SOLICIT_DELAY,
3766 .procname = "router_solicitation_delay",
3767 .data = &ipv6_devconf.rtr_solicit_delay,
3768 .maxlen = sizeof(int),
3769 .mode = 0644,
3770 .proc_handler = &proc_dointvec_jiffies,
3771 .strategy = &sysctl_jiffies,
3772 },
3773 {
3774 .ctl_name = NET_IPV6_FORCE_MLD_VERSION,
3775 .procname = "force_mld_version",
3776 .data = &ipv6_devconf.force_mld_version,
3777 .maxlen = sizeof(int),
3778 .mode = 0644,
3779 .proc_handler = &proc_dointvec,
3780 },
3781#ifdef CONFIG_IPV6_PRIVACY
3782 {
3783 .ctl_name = NET_IPV6_USE_TEMPADDR,
3784 .procname = "use_tempaddr",
3785 .data = &ipv6_devconf.use_tempaddr,
3786 .maxlen = sizeof(int),
3787 .mode = 0644,
3788 .proc_handler = &proc_dointvec,
3789 },
3790 {
3791 .ctl_name = NET_IPV6_TEMP_VALID_LFT,
3792 .procname = "temp_valid_lft",
3793 .data = &ipv6_devconf.temp_valid_lft,
3794 .maxlen = sizeof(int),
3795 .mode = 0644,
3796 .proc_handler = &proc_dointvec,
3797 },
3798 {
3799 .ctl_name = NET_IPV6_TEMP_PREFERED_LFT,
3800 .procname = "temp_prefered_lft",
3801 .data = &ipv6_devconf.temp_prefered_lft,
3802 .maxlen = sizeof(int),
3803 .mode = 0644,
3804 .proc_handler = &proc_dointvec,
3805 },
3806 {
3807 .ctl_name = NET_IPV6_REGEN_MAX_RETRY,
3808 .procname = "regen_max_retry",
3809 .data = &ipv6_devconf.regen_max_retry,
3810 .maxlen = sizeof(int),
3811 .mode = 0644,
3812 .proc_handler = &proc_dointvec,
3813 },
3814 {
3815 .ctl_name = NET_IPV6_MAX_DESYNC_FACTOR,
3816 .procname = "max_desync_factor",
3817 .data = &ipv6_devconf.max_desync_factor,
3818 .maxlen = sizeof(int),
3819 .mode = 0644,
3820 .proc_handler = &proc_dointvec,
3821 },
3822#endif
3823 {
3824 .ctl_name = NET_IPV6_MAX_ADDRESSES,
3825 .procname = "max_addresses",
3826 .data = &ipv6_devconf.max_addresses,
3827 .maxlen = sizeof(int),
3828 .mode = 0644,
3829 .proc_handler = &proc_dointvec,
3830 },
3831 {
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -08003832 .ctl_name = NET_IPV6_ACCEPT_RA_DEFRTR,
3833 .procname = "accept_ra_defrtr",
3834 .data = &ipv6_devconf.accept_ra_defrtr,
3835 .maxlen = sizeof(int),
3836 .mode = 0644,
3837 .proc_handler = &proc_dointvec,
3838 },
3839 {
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -08003840 .ctl_name = NET_IPV6_ACCEPT_RA_PINFO,
3841 .procname = "accept_ra_pinfo",
3842 .data = &ipv6_devconf.accept_ra_pinfo,
3843 .maxlen = sizeof(int),
3844 .mode = 0644,
3845 .proc_handler = &proc_dointvec,
3846 },
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08003847#ifdef CONFIG_IPV6_ROUTER_PREF
3848 {
3849 .ctl_name = NET_IPV6_ACCEPT_RA_RTR_PREF,
3850 .procname = "accept_ra_rtr_pref",
3851 .data = &ipv6_devconf.accept_ra_rtr_pref,
3852 .maxlen = sizeof(int),
3853 .mode = 0644,
3854 .proc_handler = &proc_dointvec,
3855 },
YOSHIFUJI Hideaki52e16352006-03-20 17:05:47 -08003856 {
3857 .ctl_name = NET_IPV6_RTR_PROBE_INTERVAL,
3858 .procname = "router_probe_interval",
3859 .data = &ipv6_devconf.rtr_probe_interval,
3860 .maxlen = sizeof(int),
3861 .mode = 0644,
3862 .proc_handler = &proc_dointvec_jiffies,
3863 .strategy = &sysctl_jiffies,
3864 },
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -08003865#ifdef CONFIV_IPV6_ROUTE_INFO
3866 {
3867 .ctl_name = NET_IPV6_ACCEPT_RA_RT_INFO_MAX_PLEN,
3868 .procname = "accept_ra_rt_info_max_plen",
3869 .data = &ipv6_devconf.accept_ra_rt_info_max_plen,
3870 .maxlen = sizeof(int),
3871 .mode = 0644,
3872 .proc_handler = &proc_dointvec,
3873 },
3874#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08003875#endif
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -08003876 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877 .ctl_name = 0, /* sentinel */
3878 }
3879 },
3880 .addrconf_dev = {
3881 {
3882 .ctl_name = NET_PROTO_CONF_ALL,
3883 .procname = "all",
3884 .mode = 0555,
3885 .child = addrconf_sysctl.addrconf_vars,
3886 },
3887 {
3888 .ctl_name = 0, /* sentinel */
3889 }
3890 },
3891 .addrconf_conf_dir = {
3892 {
3893 .ctl_name = NET_IPV6_CONF,
3894 .procname = "conf",
3895 .mode = 0555,
3896 .child = addrconf_sysctl.addrconf_dev,
3897 },
3898 {
3899 .ctl_name = 0, /* sentinel */
3900 }
3901 },
3902 .addrconf_proto_dir = {
3903 {
3904 .ctl_name = NET_IPV6,
3905 .procname = "ipv6",
3906 .mode = 0555,
3907 .child = addrconf_sysctl.addrconf_conf_dir,
3908 },
3909 {
3910 .ctl_name = 0, /* sentinel */
3911 }
3912 },
3913 .addrconf_root_dir = {
3914 {
3915 .ctl_name = CTL_NET,
3916 .procname = "net",
3917 .mode = 0555,
3918 .child = addrconf_sysctl.addrconf_proto_dir,
3919 },
3920 {
3921 .ctl_name = 0, /* sentinel */
3922 }
3923 },
3924};
3925
3926static void addrconf_sysctl_register(struct inet6_dev *idev, struct ipv6_devconf *p)
3927{
3928 int i;
3929 struct net_device *dev = idev ? idev->dev : NULL;
3930 struct addrconf_sysctl_table *t;
3931 char *dev_name = NULL;
3932
3933 t = kmalloc(sizeof(*t), GFP_KERNEL);
3934 if (t == NULL)
3935 return;
3936 memcpy(t, &addrconf_sysctl, sizeof(*t));
3937 for (i=0; t->addrconf_vars[i].data; i++) {
3938 t->addrconf_vars[i].data += (char*)p - (char*)&ipv6_devconf;
3939 t->addrconf_vars[i].de = NULL;
3940 t->addrconf_vars[i].extra1 = idev; /* embedded; no ref */
3941 }
3942 if (dev) {
3943 dev_name = dev->name;
3944 t->addrconf_dev[0].ctl_name = dev->ifindex;
3945 } else {
3946 dev_name = "default";
3947 t->addrconf_dev[0].ctl_name = NET_PROTO_CONF_DEFAULT;
3948 }
3949
3950 /*
3951 * Make a copy of dev_name, because '.procname' is regarded as const
3952 * by sysctl and we wouldn't want anyone to change it under our feet
3953 * (see SIOCSIFNAME).
3954 */
Paulo Marques543537b2005-06-23 00:09:02 -07003955 dev_name = kstrdup(dev_name, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956 if (!dev_name)
3957 goto free;
3958
3959 t->addrconf_dev[0].procname = dev_name;
3960
3961 t->addrconf_dev[0].child = t->addrconf_vars;
3962 t->addrconf_dev[0].de = NULL;
3963 t->addrconf_conf_dir[0].child = t->addrconf_dev;
3964 t->addrconf_conf_dir[0].de = NULL;
3965 t->addrconf_proto_dir[0].child = t->addrconf_conf_dir;
3966 t->addrconf_proto_dir[0].de = NULL;
3967 t->addrconf_root_dir[0].child = t->addrconf_proto_dir;
3968 t->addrconf_root_dir[0].de = NULL;
3969
3970 t->sysctl_header = register_sysctl_table(t->addrconf_root_dir, 0);
3971 if (t->sysctl_header == NULL)
3972 goto free_procname;
3973 else
3974 p->sysctl = t;
3975 return;
3976
3977 /* error path */
3978 free_procname:
3979 kfree(dev_name);
3980 free:
3981 kfree(t);
3982
3983 return;
3984}
3985
3986static void addrconf_sysctl_unregister(struct ipv6_devconf *p)
3987{
3988 if (p->sysctl) {
3989 struct addrconf_sysctl_table *t = p->sysctl;
3990 p->sysctl = NULL;
3991 unregister_sysctl_table(t->sysctl_header);
3992 kfree(t->addrconf_dev[0].procname);
3993 kfree(t);
3994 }
3995}
3996
3997
3998#endif
3999
4000/*
4001 * Device notifier
4002 */
4003
4004int register_inet6addr_notifier(struct notifier_block *nb)
4005{
Alan Sterne041c682006-03-27 01:16:30 -08004006 return atomic_notifier_chain_register(&inet6addr_chain, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007}
4008
4009int unregister_inet6addr_notifier(struct notifier_block *nb)
4010{
Alan Sterne041c682006-03-27 01:16:30 -08004011 return atomic_notifier_chain_unregister(&inet6addr_chain,nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012}
4013
4014/*
4015 * Init / cleanup code
4016 */
4017
4018int __init addrconf_init(void)
4019{
4020 int err = 0;
4021
4022 /* The addrconf netdev notifier requires that loopback_dev
4023 * has it's ipv6 private information allocated and setup
4024 * before it can bring up and give link-local addresses
4025 * to other devices which are up.
4026 *
4027 * Unfortunately, loopback_dev is not necessarily the first
4028 * entry in the global dev_base list of net devices. In fact,
4029 * it is likely to be the very last entry on that list.
4030 * So this causes the notifier registry below to try and
4031 * give link-local addresses to all devices besides loopback_dev
4032 * first, then loopback_dev, which cases all the non-loopback_dev
4033 * devices to fail to get a link-local address.
4034 *
4035 * So, as a temporary fix, allocate the ipv6 structure for
4036 * loopback_dev first by hand.
4037 * Longer term, all of the dependencies ipv6 has upon the loopback
4038 * device and it being up should be removed.
4039 */
4040 rtnl_lock();
4041 if (!ipv6_add_dev(&loopback_dev))
4042 err = -ENOMEM;
4043 rtnl_unlock();
4044 if (err)
4045 return err;
4046
Herbert Xuc62dba92005-09-26 15:10:16 -07004047 ip6_null_entry.rt6i_idev = in6_dev_get(&loopback_dev);
4048
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049 register_netdevice_notifier(&ipv6_dev_notf);
4050
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051 addrconf_verify(0);
4052 rtnetlink_links[PF_INET6] = inet6_rtnetlink_table;
4053#ifdef CONFIG_SYSCTL
4054 addrconf_sysctl.sysctl_header =
4055 register_sysctl_table(addrconf_sysctl.addrconf_root_dir, 0);
4056 addrconf_sysctl_register(NULL, &ipv6_devconf_dflt);
4057#endif
4058
4059 return 0;
4060}
4061
4062void __exit addrconf_cleanup(void)
4063{
4064 struct net_device *dev;
4065 struct inet6_dev *idev;
4066 struct inet6_ifaddr *ifa;
4067 int i;
4068
4069 unregister_netdevice_notifier(&ipv6_dev_notf);
4070
4071 rtnetlink_links[PF_INET6] = NULL;
4072#ifdef CONFIG_SYSCTL
4073 addrconf_sysctl_unregister(&ipv6_devconf_dflt);
4074 addrconf_sysctl_unregister(&ipv6_devconf);
4075#endif
4076
4077 rtnl_lock();
4078
4079 /*
4080 * clean dev list.
4081 */
4082
4083 for (dev=dev_base; dev; dev=dev->next) {
4084 if ((idev = __in6_dev_get(dev)) == NULL)
4085 continue;
4086 addrconf_ifdown(dev, 1);
4087 }
4088 addrconf_ifdown(&loopback_dev, 2);
4089
4090 /*
4091 * Check hash table.
4092 */
4093
4094 write_lock_bh(&addrconf_hash_lock);
4095 for (i=0; i < IN6_ADDR_HSIZE; i++) {
4096 for (ifa=inet6_addr_lst[i]; ifa; ) {
4097 struct inet6_ifaddr *bifa;
4098
4099 bifa = ifa;
4100 ifa = ifa->lst_next;
4101 printk(KERN_DEBUG "bug: IPv6 address leakage detected: ifa=%p\n", bifa);
4102 /* Do not free it; something is wrong.
4103 Now we can investigate it with debugger.
4104 */
4105 }
4106 }
4107 write_unlock_bh(&addrconf_hash_lock);
4108
4109 del_timer(&addr_chk_timer);
4110
4111 rtnl_unlock();
4112
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113#ifdef CONFIG_PROC_FS
4114 proc_net_remove("if_inet6");
4115#endif
4116}