blob: e08955baedffc491a6fafaae378e71adb50d9e23 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * IPv6 Address [auto]configuration
3 * Linux INET6 implementation
4 *
5 * Authors:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09006 * Pedro Roque <roque@di.fc.ul.pt>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
8 *
9 * $Id: addrconf.c,v 1.69 2001/10/31 21:55:54 davem Exp $
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version
14 * 2 of the License, or (at your option) any later version.
15 */
16
17/*
18 * Changes:
19 *
20 * Janos Farkas : delete timer on ifdown
21 * <chexum@bankinf.banki.hu>
22 * Andi Kleen : kill double kfree on module
23 * unload.
24 * Maciej W. Rozycki : FDDI support
25 * sekiya@USAGI : Don't send too many RS
26 * packets.
27 * yoshfuji@USAGI : Fixed interval between DAD
28 * packets.
29 * YOSHIFUJI Hideaki @USAGI : improved accuracy of
30 * address validation timer.
31 * YOSHIFUJI Hideaki @USAGI : Privacy Extensions (RFC3041)
32 * support.
33 * Yuji SEKIYA @USAGI : Don't assign a same IPv6
34 * address on a same interface.
35 * YOSHIFUJI Hideaki @USAGI : ARCnet support
36 * YOSHIFUJI Hideaki @USAGI : convert /proc/net/if_inet6 to
37 * seq_file.
YOSHIFUJI Hideakib1cacb62005-11-08 09:38:12 -080038 * YOSHIFUJI Hideaki @USAGI : improved source address
39 * selection; consider scope,
40 * status etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 */
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/errno.h>
44#include <linux/types.h>
45#include <linux/socket.h>
46#include <linux/sockios.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/net.h>
48#include <linux/in6.h>
49#include <linux/netdevice.h>
Thomas Graf18237302006-08-04 23:04:54 -070050#include <linux/if_addr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/if_arp.h>
52#include <linux/if_arcnet.h>
53#include <linux/if_infiniband.h>
54#include <linux/route.h>
55#include <linux/inetdevice.h>
56#include <linux/init.h>
57#ifdef CONFIG_SYSCTL
58#include <linux/sysctl.h>
59#endif
Randy Dunlap4fc268d2006-01-11 12:17:47 -080060#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <linux/delay.h>
62#include <linux/notifier.h>
Paulo Marques543537b2005-06-23 00:09:02 -070063#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020065#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#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>
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -070077#include <net/pkt_sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070078#include <linux/if_tunnel.h>
79#include <linux/rtnetlink.h>
80
81#ifdef CONFIG_IPV6_PRIVACY
82#include <linux/random.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070083#endif
84
85#include <asm/uaccess.h>
Herbert Xu7f7d9a62007-04-24 21:54:09 -070086#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88#include <linux/proc_fs.h>
89#include <linux/seq_file.h>
90
91/* Set to 3 to get tracing... */
92#define ACONF_DEBUG 2
93
94#if ACONF_DEBUG >= 3
95#define ADBG(x) printk x
96#else
97#define ADBG(x)
98#endif
99
100#define INFINITY_LIFE_TIME 0xFFFFFFFF
101#define TIME_DELTA(a,b) ((unsigned long)((long)(a) - (long)(b)))
102
103#ifdef CONFIG_SYSCTL
Pavel Emelyanovf52295a2007-12-02 00:58:37 +1100104static void addrconf_sysctl_register(struct inet6_dev *idev);
Pavel Emelyanov408c4762008-01-10 17:41:21 -0800105static void addrconf_sysctl_unregister(struct inet6_dev *idev);
106#else
107static inline void addrconf_sysctl_register(struct inet6_dev *idev)
108{
109}
110
111static inline void addrconf_sysctl_unregister(struct inet6_dev *idev)
112{
113}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114#endif
115
116#ifdef CONFIG_IPV6_PRIVACY
117static int __ipv6_regen_rndid(struct inet6_dev *idev);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900118static int __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119static void ipv6_regen_rndid(unsigned long data);
120
121static int desync_factor = MAX_DESYNC_FACTOR * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122#endif
123
124static int ipv6_count_addresses(struct inet6_dev *idev);
125
126/*
127 * Configured unicast address hash table
128 */
129static struct inet6_ifaddr *inet6_addr_lst[IN6_ADDR_HSIZE];
130static DEFINE_RWLOCK(addrconf_hash_lock);
131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132static void addrconf_verify(unsigned long);
133
Ingo Molnar8d06afa2005-09-09 13:10:40 -0700134static DEFINE_TIMER(addr_chk_timer, addrconf_verify, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135static DEFINE_SPINLOCK(addrconf_verify_lock);
136
137static void addrconf_join_anycast(struct inet6_ifaddr *ifp);
138static void addrconf_leave_anycast(struct inet6_ifaddr *ifp);
139
140static int addrconf_ifdown(struct net_device *dev, int how);
141
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -0700142static void addrconf_dad_start(struct inet6_ifaddr *ifp, u32 flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143static void addrconf_dad_timer(unsigned long data);
144static void addrconf_dad_completed(struct inet6_ifaddr *ifp);
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +0900145static void addrconf_dad_run(struct inet6_dev *idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146static void addrconf_rs_timer(unsigned long data);
147static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
148static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
149
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900150static void inet6_prefix_notify(int event, struct inet6_dev *idev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 struct prefix_info *pinfo);
Daniel Lezcano06bfe652008-01-10 22:43:42 -0800152static int ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
153 struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Alan Sterne041c682006-03-27 01:16:30 -0800155static ATOMIC_NOTIFIER_HEAD(inet6addr_chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Brian Haleyab32ea52006-09-22 14:15:41 -0700157struct ipv6_devconf ipv6_devconf __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 .forwarding = 0,
159 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
160 .mtu6 = IPV6_MIN_MTU,
161 .accept_ra = 1,
162 .accept_redirects = 1,
163 .autoconf = 1,
164 .force_mld_version = 0,
165 .dad_transmits = 1,
166 .rtr_solicits = MAX_RTR_SOLICITATIONS,
167 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
168 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
169#ifdef CONFIG_IPV6_PRIVACY
170 .use_tempaddr = 0,
171 .temp_valid_lft = TEMP_VALID_LIFETIME,
172 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
173 .regen_max_retry = REGEN_MAX_RETRY,
174 .max_desync_factor = MAX_DESYNC_FACTOR,
175#endif
176 .max_addresses = IPV6_MAX_ADDRESSES,
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -0800177 .accept_ra_defrtr = 1,
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -0800178 .accept_ra_pinfo = 1,
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800179#ifdef CONFIG_IPV6_ROUTER_PREF
180 .accept_ra_rtr_pref = 1,
YOSHIFUJI Hideaki52e16352006-03-20 17:05:47 -0800181 .rtr_probe_interval = 60 * HZ,
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -0800182#ifdef CONFIG_IPV6_ROUTE_INFO
183 .accept_ra_rt_info_max_plen = 0,
184#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800185#endif
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -0700186 .proxy_ndp = 0,
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -0700187 .accept_source_route = 0, /* we do not accept RH0 by default. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188};
189
Brian Haleyab32ea52006-09-22 14:15:41 -0700190static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 .forwarding = 0,
192 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
193 .mtu6 = IPV6_MIN_MTU,
194 .accept_ra = 1,
195 .accept_redirects = 1,
196 .autoconf = 1,
197 .dad_transmits = 1,
198 .rtr_solicits = MAX_RTR_SOLICITATIONS,
199 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
200 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
201#ifdef CONFIG_IPV6_PRIVACY
202 .use_tempaddr = 0,
203 .temp_valid_lft = TEMP_VALID_LIFETIME,
204 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
205 .regen_max_retry = REGEN_MAX_RETRY,
206 .max_desync_factor = MAX_DESYNC_FACTOR,
207#endif
208 .max_addresses = IPV6_MAX_ADDRESSES,
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -0800209 .accept_ra_defrtr = 1,
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -0800210 .accept_ra_pinfo = 1,
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800211#ifdef CONFIG_IPV6_ROUTER_PREF
212 .accept_ra_rtr_pref = 1,
YOSHIFUJI Hideaki52e16352006-03-20 17:05:47 -0800213 .rtr_probe_interval = 60 * HZ,
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -0800214#ifdef CONFIG_IPV6_ROUTE_INFO
215 .accept_ra_rt_info_max_plen = 0,
216#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800217#endif
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -0700218 .proxy_ndp = 0,
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -0700219 .accept_source_route = 0, /* we do not accept RH0 by default. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220};
221
222/* IPv6 Wildcard Address and Loopback Address defined by RFC2553 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
225
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -0700226/* Check if a valid qdisc is available */
227static inline int addrconf_qdisc_ok(struct net_device *dev)
228{
229 return (dev->qdisc != &noop_qdisc);
230}
231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232static void addrconf_del_timer(struct inet6_ifaddr *ifp)
233{
234 if (del_timer(&ifp->timer))
235 __in6_ifa_put(ifp);
236}
237
238enum addrconf_timer_t
239{
240 AC_NONE,
241 AC_DAD,
242 AC_RS,
243};
244
245static void addrconf_mod_timer(struct inet6_ifaddr *ifp,
246 enum addrconf_timer_t what,
247 unsigned long when)
248{
249 if (!del_timer(&ifp->timer))
250 in6_ifa_hold(ifp);
251
252 switch (what) {
253 case AC_DAD:
254 ifp->timer.function = addrconf_dad_timer;
255 break;
256 case AC_RS:
257 ifp->timer.function = addrconf_rs_timer;
258 break;
259 default:;
260 }
261 ifp->timer.expires = jiffies + when;
262 add_timer(&ifp->timer);
263}
264
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700265static int snmp6_alloc_dev(struct inet6_dev *idev)
266{
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700267 if (snmp_mib_init((void **)idev->stats.ipv6,
YOSHIFUJI Hideakic69bce22008-01-23 22:31:45 -0800268 sizeof(struct ipstats_mib)) < 0)
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700269 goto err_ip;
270 if (snmp_mib_init((void **)idev->stats.icmpv6,
YOSHIFUJI Hideakic69bce22008-01-23 22:31:45 -0800271 sizeof(struct icmpv6_mib)) < 0)
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700272 goto err_icmp;
David L Stevens14878f72007-09-16 16:52:35 -0700273 if (snmp_mib_init((void **)idev->stats.icmpv6msg,
YOSHIFUJI Hideakic69bce22008-01-23 22:31:45 -0800274 sizeof(struct icmpv6msg_mib)) < 0)
David L Stevens14878f72007-09-16 16:52:35 -0700275 goto err_icmpmsg;
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700276
277 return 0;
278
David L Stevens14878f72007-09-16 16:52:35 -0700279err_icmpmsg:
280 snmp_mib_free((void **)idev->stats.icmpv6);
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700281err_icmp:
282 snmp_mib_free((void **)idev->stats.ipv6);
283err_ip:
Pavel Emelyanovaaf70ec2007-10-17 21:25:32 -0700284 return -ENOMEM;
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700285}
286
Pavel Emelyanov16910b92007-10-17 21:23:43 -0700287static void snmp6_free_dev(struct inet6_dev *idev)
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700288{
David L Stevens14878f72007-09-16 16:52:35 -0700289 snmp_mib_free((void **)idev->stats.icmpv6msg);
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700290 snmp_mib_free((void **)idev->stats.icmpv6);
291 snmp_mib_free((void **)idev->stats.ipv6);
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700292}
293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294/* Nobody refers to this device, we may destroy it. */
295
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -0700296static void in6_dev_finish_destroy_rcu(struct rcu_head *head)
297{
298 struct inet6_dev *idev = container_of(head, struct inet6_dev, rcu);
299 kfree(idev);
300}
301
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302void in6_dev_finish_destroy(struct inet6_dev *idev)
303{
304 struct net_device *dev = idev->dev;
305 BUG_TRAP(idev->addr_list==NULL);
306 BUG_TRAP(idev->mc_list==NULL);
307#ifdef NET_REFCNT_DEBUG
308 printk(KERN_DEBUG "in6_dev_finish_destroy: %s\n", dev ? dev->name : "NIL");
309#endif
310 dev_put(dev);
311 if (!idev->dead) {
312 printk("Freeing alive inet6 device %p\n", idev);
313 return;
314 }
315 snmp6_free_dev(idev);
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -0700316 call_rcu(&idev->rcu, in6_dev_finish_destroy_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317}
318
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +0900319EXPORT_SYMBOL(in6_dev_finish_destroy);
320
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321static struct inet6_dev * ipv6_add_dev(struct net_device *dev)
322{
323 struct inet6_dev *ndev;
YOSHIFUJI Hideakid88ae4c2007-01-14 21:48:40 -0800324 struct in6_addr maddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
326 ASSERT_RTNL();
327
328 if (dev->mtu < IPV6_MIN_MTU)
329 return NULL;
330
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900331 ndev = kzalloc(sizeof(struct inet6_dev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900333 if (ndev == NULL)
334 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Ingo Oeser322f74a2006-03-20 23:01:47 -0800336 rwlock_init(&ndev->lock);
337 ndev->dev = dev;
Pavel Emelyanov441fc2a2008-01-10 17:43:22 -0800338 memcpy(&ndev->cnf, dev->nd_net->ipv6.devconf_dflt, sizeof(ndev->cnf));
Ingo Oeser322f74a2006-03-20 23:01:47 -0800339 ndev->cnf.mtu6 = dev->mtu;
340 ndev->cnf.sysctl = NULL;
341 ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl);
342 if (ndev->nd_parms == NULL) {
343 kfree(ndev);
344 return NULL;
345 }
346 /* We refer to the device */
347 dev_hold(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Ingo Oeser322f74a2006-03-20 23:01:47 -0800349 if (snmp6_alloc_dev(ndev) < 0) {
350 ADBG((KERN_WARNING
351 "%s(): cannot allocate memory for statistics; dev=%s.\n",
352 __FUNCTION__, dev->name));
353 neigh_parms_release(&nd_tbl, ndev->nd_parms);
354 ndev->dead = 1;
355 in6_dev_finish_destroy(ndev);
356 return NULL;
357 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Ingo Oeser322f74a2006-03-20 23:01:47 -0800359 if (snmp6_register_dev(ndev) < 0) {
360 ADBG((KERN_WARNING
361 "%s(): cannot create /proc/net/dev_snmp6/%s\n",
362 __FUNCTION__, dev->name));
363 neigh_parms_release(&nd_tbl, ndev->nd_parms);
364 ndev->dead = 1;
365 in6_dev_finish_destroy(ndev);
366 return NULL;
367 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
Ingo Oeser322f74a2006-03-20 23:01:47 -0800369 /* One reference from device. We must do this before
370 * we invoke __ipv6_regen_rndid().
371 */
372 in6_dev_hold(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
YOSHIFUJI Hideakib077d7a2008-04-13 23:42:18 -0700374#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
375 if (dev->type == ARPHRD_SIT && (dev->priv_flags & IFF_ISATAP)) {
376 printk(KERN_INFO
377 "%s: Disabled Multicast RS\n",
378 dev->name);
379 ndev->cnf.rtr_solicits = 0;
380 }
381#endif
382
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383#ifdef CONFIG_IPV6_PRIVACY
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800384 setup_timer(&ndev->regen_timer, ipv6_regen_rndid, (unsigned long)ndev);
Ingo Oeser322f74a2006-03-20 23:01:47 -0800385 if ((dev->flags&IFF_LOOPBACK) ||
386 dev->type == ARPHRD_TUNNEL ||
YOSHIFUJI Hideaki9625ed72008-04-13 23:47:11 -0700387 dev->type == ARPHRD_TUNNEL6 ||
Joerg Roedel0be669b2006-10-10 14:49:53 -0700388 dev->type == ARPHRD_SIT ||
Joerg Roedel0be669b2006-10-10 14:49:53 -0700389 dev->type == ARPHRD_NONE) {
Ingo Oeser322f74a2006-03-20 23:01:47 -0800390 printk(KERN_INFO
391 "%s: Disabled Privacy Extensions\n",
392 dev->name);
393 ndev->cnf.use_tempaddr = -1;
394 } else {
395 in6_dev_hold(ndev);
396 ipv6_regen_rndid((unsigned long) ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 }
Ingo Oeser322f74a2006-03-20 23:01:47 -0800398#endif
399
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -0700400 if (netif_running(dev) && addrconf_qdisc_ok(dev))
Herbert Xu53aadcc2007-03-27 14:31:52 -0700401 ndev->if_flags |= IF_READY;
402
Ingo Oeser322f74a2006-03-20 23:01:47 -0800403 ipv6_mc_init_dev(ndev);
404 ndev->tstamp = jiffies;
Pavel Emelyanovf52295a2007-12-02 00:58:37 +1100405 addrconf_sysctl_register(ndev);
David L Stevens30c4cf52007-01-04 12:31:14 -0800406 /* protected by rtnl_lock */
407 rcu_assign_pointer(dev->ip6_ptr, ndev);
YOSHIFUJI Hideakid88ae4c2007-01-14 21:48:40 -0800408
409 /* Join all-node multicast group */
410 ipv6_addr_all_nodes(&maddr);
411 ipv6_dev_mc_inc(dev, &maddr);
412
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 return ndev;
414}
415
416static struct inet6_dev * ipv6_find_idev(struct net_device *dev)
417{
418 struct inet6_dev *idev;
419
420 ASSERT_RTNL();
421
422 if ((idev = __in6_dev_get(dev)) == NULL) {
423 if ((idev = ipv6_add_dev(dev)) == NULL)
424 return NULL;
425 }
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +0900426
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 if (dev->flags&IFF_UP)
428 ipv6_mc_up(idev);
429 return idev;
430}
431
432#ifdef CONFIG_SYSCTL
433static void dev_forward_change(struct inet6_dev *idev)
434{
435 struct net_device *dev;
436 struct inet6_ifaddr *ifa;
437 struct in6_addr addr;
438
439 if (!idev)
440 return;
441 dev = idev->dev;
442 if (dev && (dev->flags & IFF_MULTICAST)) {
443 ipv6_addr_all_routers(&addr);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900444
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 if (idev->cnf.forwarding)
446 ipv6_dev_mc_inc(dev, &addr);
447 else
448 ipv6_dev_mc_dec(dev, &addr);
449 }
450 for (ifa=idev->addr_list; ifa; ifa=ifa->if_next) {
Michal Wrobel2c12a742007-02-26 15:36:10 -0800451 if (ifa->flags&IFA_F_TENTATIVE)
452 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 if (idev->cnf.forwarding)
454 addrconf_join_anycast(ifa);
455 else
456 addrconf_leave_anycast(ifa);
457 }
458}
459
460
Pavel Emelyanove1869322008-01-10 17:43:50 -0800461static void addrconf_forward_change(struct net *net, __s32 newf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462{
463 struct net_device *dev;
464 struct inet6_dev *idev;
465
466 read_lock(&dev_base_lock);
Pavel Emelyanovbff16c22008-01-10 17:42:13 -0800467 for_each_netdev(net, dev) {
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -0700468 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 idev = __in6_dev_get(dev);
470 if (idev) {
Pavel Emelyanove1869322008-01-10 17:43:50 -0800471 int changed = (!idev->cnf.forwarding) ^ (!newf);
472 idev->cnf.forwarding = newf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 if (changed)
474 dev_forward_change(idev);
475 }
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -0700476 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 }
478 read_unlock(&dev_base_lock);
479}
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800480
481static void addrconf_fixup_forwarding(struct ctl_table *table, int *p, int old)
482{
Pavel Emelyanovbff16c22008-01-10 17:42:13 -0800483 struct net *net;
484
485 net = (struct net *)table->extra2;
Pavel Emelyanov441fc2a2008-01-10 17:43:22 -0800486 if (p == &net->ipv6.devconf_dflt->forwarding)
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800487 return;
488
Pavel Emelyanove1869322008-01-10 17:43:50 -0800489 if (p == &net->ipv6.devconf_all->forwarding) {
490 __s32 newf = net->ipv6.devconf_all->forwarding;
491 net->ipv6.devconf_dflt->forwarding = newf;
492 addrconf_forward_change(net, newf);
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800493 } else if ((!*p) ^ (!old))
494 dev_forward_change((struct inet6_dev *)table->extra1);
495
496 if (*p)
497 rt6_purge_dflt_routers();
498}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499#endif
500
501/* Nobody refers to this ifaddr, destroy it */
502
503void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp)
504{
505 BUG_TRAP(ifp->if_next==NULL);
506 BUG_TRAP(ifp->lst_next==NULL);
507#ifdef NET_REFCNT_DEBUG
508 printk(KERN_DEBUG "inet6_ifa_finish_destroy\n");
509#endif
510
511 in6_dev_put(ifp->idev);
512
513 if (del_timer(&ifp->timer))
514 printk("Timer is still running, when freeing ifa=%p\n", ifp);
515
516 if (!ifp->dead) {
517 printk("Freeing alive inet6 address %p\n", ifp);
518 return;
519 }
520 dst_release(&ifp->rt->u.dst);
521
522 kfree(ifp);
523}
524
Brian Haleye55ffac2006-07-10 15:25:51 -0700525static void
526ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp)
527{
528 struct inet6_ifaddr *ifa, **ifap;
YOSHIFUJI Hideaki8a6ce0c2006-07-11 13:05:30 -0700529 int ifp_scope = ipv6_addr_src_scope(&ifp->addr);
Brian Haleye55ffac2006-07-10 15:25:51 -0700530
531 /*
532 * Each device address list is sorted in order of scope -
533 * global before linklocal.
534 */
535 for (ifap = &idev->addr_list; (ifa = *ifap) != NULL;
536 ifap = &ifa->if_next) {
YOSHIFUJI Hideaki8a6ce0c2006-07-11 13:05:30 -0700537 if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr))
Brian Haleye55ffac2006-07-10 15:25:51 -0700538 break;
539 }
540
541 ifp->if_next = *ifap;
542 *ifap = ifp;
543}
544
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545/* On success it returns ifp with increased reference count */
546
547static struct inet6_ifaddr *
548ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr, int pfxlen,
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -0700549 int scope, u32 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550{
551 struct inet6_ifaddr *ifa = NULL;
552 struct rt6_info *rt;
553 int hash;
554 int err = 0;
555
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -0700556 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 if (idev->dead) {
558 err = -ENODEV; /*XXX*/
559 goto out2;
560 }
561
562 write_lock(&addrconf_hash_lock);
563
564 /* Ignore adding duplicate addresses on an interface */
Daniel Lezcano06bfe652008-01-10 22:43:42 -0800565 if (ipv6_chk_same_addr(&init_net, addr, idev->dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 ADBG(("ipv6_add_addr: already assigned\n"));
567 err = -EEXIST;
568 goto out;
569 }
570
Ingo Oeser322f74a2006-03-20 23:01:47 -0800571 ifa = kzalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
573 if (ifa == NULL) {
574 ADBG(("ipv6_add_addr: malloc failed\n"));
575 err = -ENOBUFS;
576 goto out;
577 }
578
579 rt = addrconf_dst_alloc(idev, addr, 0);
580 if (IS_ERR(rt)) {
581 err = PTR_ERR(rt);
582 goto out;
583 }
584
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 ipv6_addr_copy(&ifa->addr, addr);
586
587 spin_lock_init(&ifa->lock);
588 init_timer(&ifa->timer);
589 ifa->timer.data = (unsigned long) ifa;
590 ifa->scope = scope;
591 ifa->prefix_len = pfxlen;
592 ifa->flags = flags | IFA_F_TENTATIVE;
593 ifa->cstamp = ifa->tstamp = jiffies;
594
Keir Fraser57f5f542006-08-29 02:43:49 -0700595 ifa->rt = rt;
596
Neil Horman95c385b2007-04-25 17:08:10 -0700597 /*
598 * part one of RFC 4429, section 3.3
599 * We should not configure an address as
600 * optimistic if we do not yet know the link
601 * layer address of our nexhop router
602 */
603
604 if (rt->rt6i_nexthop == NULL)
605 ifa->flags &= ~IFA_F_OPTIMISTIC;
606
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 ifa->idev = idev;
608 in6_dev_hold(idev);
609 /* For caller */
610 in6_ifa_hold(ifa);
611
612 /* Add to big hash table */
613 hash = ipv6_addr_hash(addr);
614
615 ifa->lst_next = inet6_addr_lst[hash];
616 inet6_addr_lst[hash] = ifa;
617 in6_ifa_hold(ifa);
618 write_unlock(&addrconf_hash_lock);
619
620 write_lock(&idev->lock);
621 /* Add to inet6_dev unicast addr list. */
Brian Haleye55ffac2006-07-10 15:25:51 -0700622 ipv6_link_dev_addr(idev, ifa);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
624#ifdef CONFIG_IPV6_PRIVACY
625 if (ifa->flags&IFA_F_TEMPORARY) {
626 ifa->tmp_next = idev->tempaddr_list;
627 idev->tempaddr_list = ifa;
628 in6_ifa_hold(ifa);
629 }
630#endif
631
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 in6_ifa_hold(ifa);
633 write_unlock(&idev->lock);
634out2:
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -0700635 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
YOSHIFUJI Hideakifd928332005-04-19 22:27:09 -0700637 if (likely(err == 0))
Alan Sterne041c682006-03-27 01:16:30 -0800638 atomic_notifier_call_chain(&inet6addr_chain, NETDEV_UP, ifa);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 else {
640 kfree(ifa);
641 ifa = ERR_PTR(err);
642 }
643
644 return ifa;
645out:
646 write_unlock(&addrconf_hash_lock);
647 goto out2;
648}
649
650/* This function wants to get referenced ifp and releases it before return */
651
652static void ipv6_del_addr(struct inet6_ifaddr *ifp)
653{
654 struct inet6_ifaddr *ifa, **ifap;
655 struct inet6_dev *idev = ifp->idev;
656 int hash;
657 int deleted = 0, onlink = 0;
658 unsigned long expires = jiffies;
659
660 hash = ipv6_addr_hash(&ifp->addr);
661
662 ifp->dead = 1;
663
664 write_lock_bh(&addrconf_hash_lock);
665 for (ifap = &inet6_addr_lst[hash]; (ifa=*ifap) != NULL;
666 ifap = &ifa->lst_next) {
667 if (ifa == ifp) {
668 *ifap = ifa->lst_next;
669 __in6_ifa_put(ifp);
670 ifa->lst_next = NULL;
671 break;
672 }
673 }
674 write_unlock_bh(&addrconf_hash_lock);
675
676 write_lock_bh(&idev->lock);
677#ifdef CONFIG_IPV6_PRIVACY
678 if (ifp->flags&IFA_F_TEMPORARY) {
679 for (ifap = &idev->tempaddr_list; (ifa=*ifap) != NULL;
680 ifap = &ifa->tmp_next) {
681 if (ifa == ifp) {
682 *ifap = ifa->tmp_next;
683 if (ifp->ifpub) {
684 in6_ifa_put(ifp->ifpub);
685 ifp->ifpub = NULL;
686 }
687 __in6_ifa_put(ifp);
688 ifa->tmp_next = NULL;
689 break;
690 }
691 }
692 }
693#endif
694
Kristian Slavov1d142802005-12-21 18:47:24 -0800695 for (ifap = &idev->addr_list; (ifa=*ifap) != NULL;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 if (ifa == ifp) {
697 *ifap = ifa->if_next;
698 __in6_ifa_put(ifp);
699 ifa->if_next = NULL;
700 if (!(ifp->flags & IFA_F_PERMANENT) || onlink > 0)
701 break;
702 deleted = 1;
Kristian Slavov1d142802005-12-21 18:47:24 -0800703 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 } else if (ifp->flags & IFA_F_PERMANENT) {
705 if (ipv6_prefix_equal(&ifa->addr, &ifp->addr,
706 ifp->prefix_len)) {
707 if (ifa->flags & IFA_F_PERMANENT) {
708 onlink = 1;
709 if (deleted)
710 break;
711 } else {
712 unsigned long lifetime;
713
714 if (!onlink)
715 onlink = -1;
716
717 spin_lock(&ifa->lock);
718 lifetime = min_t(unsigned long,
719 ifa->valid_lft, 0x7fffffffUL/HZ);
720 if (time_before(expires,
721 ifa->tstamp + lifetime * HZ))
722 expires = ifa->tstamp + lifetime * HZ;
723 spin_unlock(&ifa->lock);
724 }
725 }
726 }
Kristian Slavov1d142802005-12-21 18:47:24 -0800727 ifap = &ifa->if_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 }
729 write_unlock_bh(&idev->lock);
730
731 ipv6_ifa_notify(RTM_DELADDR, ifp);
732
Alan Sterne041c682006-03-27 01:16:30 -0800733 atomic_notifier_call_chain(&inet6addr_chain, NETDEV_DOWN, ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
735 addrconf_del_timer(ifp);
736
737 /*
738 * Purge or update corresponding prefix
739 *
740 * 1) we don't purge prefix here if address was not permanent.
741 * prefix is managed by its own lifetime.
742 * 2) if there're no addresses, delete prefix.
743 * 3) if there're still other permanent address(es),
744 * corresponding prefix is still permanent.
745 * 4) otherwise, update prefix lifetime to the
746 * longest valid lifetime among the corresponding
747 * addresses on the device.
748 * Note: subsequent RA will update lifetime.
749 *
750 * --yoshfuji
751 */
752 if ((ifp->flags & IFA_F_PERMANENT) && onlink < 1) {
753 struct in6_addr prefix;
754 struct rt6_info *rt;
755
756 ipv6_addr_prefix(&prefix, &ifp->addr, ifp->prefix_len);
757 rt = rt6_lookup(&prefix, NULL, ifp->idev->dev->ifindex, 1);
758
759 if (rt && ((rt->rt6i_flags & (RTF_GATEWAY | RTF_DEFAULT)) == 0)) {
760 if (onlink == 0) {
Thomas Grafe0a1ad732006-08-22 00:00:21 -0700761 ip6_del_rt(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 rt = NULL;
763 } else if (!(rt->rt6i_flags & RTF_EXPIRES)) {
764 rt->rt6i_expires = expires;
765 rt->rt6i_flags |= RTF_EXPIRES;
766 }
767 }
768 dst_release(&rt->u.dst);
769 }
770
771 in6_ifa_put(ifp);
772}
773
774#ifdef CONFIG_IPV6_PRIVACY
775static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift)
776{
777 struct inet6_dev *idev = ifp->idev;
778 struct in6_addr addr, *tmpaddr;
779 unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_cstamp, tmp_tstamp;
Benoit Boissinoteac55bf2008-04-02 00:01:35 -0700780 unsigned long regen_advance;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 int tmp_plen;
782 int ret = 0;
783 int max_addresses;
Neil Horman95c385b2007-04-25 17:08:10 -0700784 u32 addr_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
786 write_lock(&idev->lock);
787 if (ift) {
788 spin_lock_bh(&ift->lock);
789 memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8);
790 spin_unlock_bh(&ift->lock);
791 tmpaddr = &addr;
792 } else {
793 tmpaddr = NULL;
794 }
795retry:
796 in6_dev_hold(idev);
797 if (idev->cnf.use_tempaddr <= 0) {
798 write_unlock(&idev->lock);
799 printk(KERN_INFO
800 "ipv6_create_tempaddr(): use_tempaddr is disabled.\n");
801 in6_dev_put(idev);
802 ret = -1;
803 goto out;
804 }
805 spin_lock_bh(&ifp->lock);
806 if (ifp->regen_count++ >= idev->cnf.regen_max_retry) {
807 idev->cnf.use_tempaddr = -1; /*XXX*/
808 spin_unlock_bh(&ifp->lock);
809 write_unlock(&idev->lock);
810 printk(KERN_WARNING
811 "ipv6_create_tempaddr(): regeneration time exceeded. disabled temporary address support.\n");
812 in6_dev_put(idev);
813 ret = -1;
814 goto out;
815 }
816 in6_ifa_hold(ifp);
817 memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
818 if (__ipv6_try_regen_rndid(idev, tmpaddr) < 0) {
819 spin_unlock_bh(&ifp->lock);
820 write_unlock(&idev->lock);
821 printk(KERN_WARNING
822 "ipv6_create_tempaddr(): regeneration of randomized interface id failed.\n");
823 in6_ifa_put(ifp);
824 in6_dev_put(idev);
825 ret = -1;
826 goto out;
827 }
828 memcpy(&addr.s6_addr[8], idev->rndid, 8);
829 tmp_valid_lft = min_t(__u32,
830 ifp->valid_lft,
831 idev->cnf.temp_valid_lft);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900832 tmp_prefered_lft = min_t(__u32,
833 ifp->prefered_lft,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 idev->cnf.temp_prefered_lft - desync_factor / HZ);
835 tmp_plen = ifp->prefix_len;
836 max_addresses = idev->cnf.max_addresses;
837 tmp_cstamp = ifp->cstamp;
838 tmp_tstamp = ifp->tstamp;
839 spin_unlock_bh(&ifp->lock);
840
Benoit Boissinoteac55bf2008-04-02 00:01:35 -0700841 regen_advance = idev->cnf.regen_max_retry *
842 idev->cnf.dad_transmits *
843 idev->nd_parms->retrans_time / HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 write_unlock(&idev->lock);
Neil Horman95c385b2007-04-25 17:08:10 -0700845
Benoit Boissinoteac55bf2008-04-02 00:01:35 -0700846 /* A temporary address is created only if this calculated Preferred
847 * Lifetime is greater than REGEN_ADVANCE time units. In particular,
848 * an implementation must not create a temporary address with a zero
849 * Preferred Lifetime.
850 */
851 if (tmp_prefered_lft <= regen_advance) {
852 in6_ifa_put(ifp);
853 in6_dev_put(idev);
854 ret = -1;
855 goto out;
856 }
857
Neil Horman95c385b2007-04-25 17:08:10 -0700858 addr_flags = IFA_F_TEMPORARY;
859 /* set in addrconf_prefix_rcv() */
860 if (ifp->flags & IFA_F_OPTIMISTIC)
861 addr_flags |= IFA_F_OPTIMISTIC;
862
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 ift = !max_addresses ||
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900864 ipv6_count_addresses(idev) < max_addresses ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 ipv6_add_addr(idev, &addr, tmp_plen,
Neil Horman95c385b2007-04-25 17:08:10 -0700866 ipv6_addr_type(&addr)&IPV6_ADDR_SCOPE_MASK,
867 addr_flags) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 if (!ift || IS_ERR(ift)) {
869 in6_ifa_put(ifp);
870 in6_dev_put(idev);
871 printk(KERN_INFO
872 "ipv6_create_tempaddr(): retry temporary address regeneration.\n");
873 tmpaddr = &addr;
874 write_lock(&idev->lock);
875 goto retry;
876 }
877
878 spin_lock_bh(&ift->lock);
879 ift->ifpub = ifp;
880 ift->valid_lft = tmp_valid_lft;
881 ift->prefered_lft = tmp_prefered_lft;
882 ift->cstamp = tmp_cstamp;
883 ift->tstamp = tmp_tstamp;
884 spin_unlock_bh(&ift->lock);
885
886 addrconf_dad_start(ift, 0);
887 in6_ifa_put(ift);
888 in6_dev_put(idev);
889out:
890 return ret;
891}
892#endif
893
894/*
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800895 * Choose an appropriate source address (RFC3484)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 */
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800897struct ipv6_saddr_score {
898 int addr_type;
899 unsigned int attrs;
900 int matchlen;
Brian Haley0d27b422006-03-11 18:50:14 -0800901 int scope;
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800902 unsigned int rule;
903};
904
905#define IPV6_SADDR_SCORE_LOCAL 0x0001
906#define IPV6_SADDR_SCORE_PREFERRED 0x0004
907#define IPV6_SADDR_SCORE_HOA 0x0008
908#define IPV6_SADDR_SCORE_OIF 0x0010
909#define IPV6_SADDR_SCORE_LABEL 0x0020
910#define IPV6_SADDR_SCORE_PRIVACY 0x0040
911
Dave Jonesb6f99a22007-03-22 12:27:49 -0700912static inline int ipv6_saddr_preferred(int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913{
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800914 if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|
915 IPV6_ADDR_LOOPBACK|IPV6_ADDR_RESERVED))
916 return 1;
917 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918}
919
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800920int ipv6_dev_get_saddr(struct net_device *daddr_dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 struct in6_addr *daddr, struct in6_addr *saddr)
922{
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800923 struct ipv6_saddr_score hiscore;
924 struct inet6_ifaddr *ifa_result = NULL;
925 int daddr_type = __ipv6_addr_type(daddr);
926 int daddr_scope = __ipv6_addr_src_scope(daddr_type);
YOSHIFUJI Hideaki303065a2007-11-14 15:56:15 +0900927 int daddr_ifindex = daddr_dev ? daddr_dev->ifindex : 0;
928 u32 daddr_label = ipv6_addr_label(daddr, daddr_type, daddr_ifindex);
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800929 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800931 memset(&hiscore, 0, sizeof(hiscore));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
933 read_lock(&dev_base_lock);
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -0700934 rcu_read_lock();
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800935
Eric W. Biederman881d9662007-09-17 11:56:21 -0700936 for_each_netdev(&init_net, dev) {
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800937 struct inet6_dev *idev;
938 struct inet6_ifaddr *ifa;
939
940 /* Rule 0: Candidate Source Address (section 4)
941 * - multicast and link-local destination address,
942 * the set of candidate source address MUST only
943 * include addresses assigned to interfaces
944 * belonging to the same link as the outgoing
945 * interface.
946 * (- For site-local destination addresses, the
947 * set of candidate source addresses MUST only
948 * include addresses assigned to interfaces
949 * belonging to the same site as the outgoing
950 * interface.)
951 */
952 if ((daddr_type & IPV6_ADDR_MULTICAST ||
953 daddr_scope <= IPV6_ADDR_SCOPE_LINKLOCAL) &&
954 daddr_dev && dev != daddr_dev)
955 continue;
956
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 idev = __in6_dev_get(dev);
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800958 if (!idev)
959 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800961 read_lock_bh(&idev->lock);
962 for (ifa = idev->addr_list; ifa; ifa = ifa->if_next) {
963 struct ipv6_saddr_score score;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800965 score.addr_type = __ipv6_addr_type(&ifa->addr);
966
YOSHIFUJI Hideaki6b3ae802005-12-21 22:58:01 +0900967 /* Rule 0:
968 * - Tentative Address (RFC2462 section 5.4)
969 * - A tentative address is not considered
970 * "assigned to an interface" in the traditional
Neil Horman95c385b2007-04-25 17:08:10 -0700971 * sense, unless it is also flagged as optimistic.
YOSHIFUJI Hideaki6b3ae802005-12-21 22:58:01 +0900972 * - Candidate Source Address (section 4)
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800973 * - In any case, anycast addresses, multicast
974 * addresses, and the unspecified address MUST
975 * NOT be included in a candidate set.
976 */
Neil Horman95c385b2007-04-25 17:08:10 -0700977 if ((ifa->flags & IFA_F_TENTATIVE) &&
978 (!(ifa->flags & IFA_F_OPTIMISTIC)))
YOSHIFUJI Hideaki6b3ae802005-12-21 22:58:01 +0900979 continue;
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800980 if (unlikely(score.addr_type == IPV6_ADDR_ANY ||
981 score.addr_type & IPV6_ADDR_MULTICAST)) {
982 LIMIT_NETDEBUG(KERN_DEBUG
Joe Perches3b6d821c2007-11-19 23:47:25 -0800983 "ADDRCONF: unspecified / multicast address "
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800984 "assigned as unicast address on %s",
985 dev->name);
986 continue;
987 }
988
989 score.attrs = 0;
990 score.matchlen = 0;
991 score.scope = 0;
992 score.rule = 0;
993
994 if (ifa_result == NULL) {
995 /* record it if the first available entry */
996 goto record_it;
997 }
998
999 /* Rule 1: Prefer same address */
1000 if (hiscore.rule < 1) {
1001 if (ipv6_addr_equal(&ifa_result->addr, daddr))
1002 hiscore.attrs |= IPV6_SADDR_SCORE_LOCAL;
1003 hiscore.rule++;
1004 }
1005 if (ipv6_addr_equal(&ifa->addr, daddr)) {
1006 score.attrs |= IPV6_SADDR_SCORE_LOCAL;
1007 if (!(hiscore.attrs & IPV6_SADDR_SCORE_LOCAL)) {
1008 score.rule = 1;
1009 goto record_it;
1010 }
1011 } else {
1012 if (hiscore.attrs & IPV6_SADDR_SCORE_LOCAL)
1013 continue;
1014 }
1015
1016 /* Rule 2: Prefer appropriate scope */
1017 if (hiscore.rule < 2) {
1018 hiscore.scope = __ipv6_addr_src_scope(hiscore.addr_type);
1019 hiscore.rule++;
1020 }
1021 score.scope = __ipv6_addr_src_scope(score.addr_type);
1022 if (hiscore.scope < score.scope) {
1023 if (hiscore.scope < daddr_scope) {
1024 score.rule = 2;
1025 goto record_it;
1026 } else
1027 continue;
1028 } else if (score.scope < hiscore.scope) {
1029 if (score.scope < daddr_scope)
Brian Haleye55ffac2006-07-10 15:25:51 -07001030 break; /* addresses sorted by scope */
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001031 else {
1032 score.rule = 2;
1033 goto record_it;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 }
1035 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
Neil Horman95c385b2007-04-25 17:08:10 -07001037 /* Rule 3: Avoid deprecated and optimistic addresses */
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001038 if (hiscore.rule < 3) {
1039 if (ipv6_saddr_preferred(hiscore.addr_type) ||
Neil Horman95c385b2007-04-25 17:08:10 -07001040 (((ifa_result->flags &
1041 (IFA_F_DEPRECATED|IFA_F_OPTIMISTIC)) == 0)))
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001042 hiscore.attrs |= IPV6_SADDR_SCORE_PREFERRED;
1043 hiscore.rule++;
1044 }
1045 if (ipv6_saddr_preferred(score.addr_type) ||
Jiri Kosina6ae5f982007-09-16 14:48:21 -07001046 (((ifa->flags &
Neil Horman95c385b2007-04-25 17:08:10 -07001047 (IFA_F_DEPRECATED|IFA_F_OPTIMISTIC)) == 0))) {
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001048 score.attrs |= IPV6_SADDR_SCORE_PREFERRED;
1049 if (!(hiscore.attrs & IPV6_SADDR_SCORE_PREFERRED)) {
1050 score.rule = 3;
1051 goto record_it;
1052 }
1053 } else {
1054 if (hiscore.attrs & IPV6_SADDR_SCORE_PREFERRED)
1055 continue;
1056 }
1057
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07001058 /* Rule 4: Prefer home address */
Masahide NAKAMURA59fbb3a62007-06-26 23:56:32 -07001059#if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07001060 if (hiscore.rule < 4) {
1061 if (ifa_result->flags & IFA_F_HOMEADDRESS)
1062 hiscore.attrs |= IPV6_SADDR_SCORE_HOA;
1063 hiscore.rule++;
1064 }
1065 if (ifa->flags & IFA_F_HOMEADDRESS) {
1066 score.attrs |= IPV6_SADDR_SCORE_HOA;
1067 if (!(ifa_result->flags & IFA_F_HOMEADDRESS)) {
1068 score.rule = 4;
1069 goto record_it;
1070 }
1071 } else {
1072 if (hiscore.attrs & IPV6_SADDR_SCORE_HOA)
1073 continue;
1074 }
1075#else
YOSHIFUJI Hideaki220bbd72005-11-28 22:27:11 -08001076 if (hiscore.rule < 4)
1077 hiscore.rule++;
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07001078#endif
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001079
1080 /* Rule 5: Prefer outgoing interface */
1081 if (hiscore.rule < 5) {
1082 if (daddr_dev == NULL ||
1083 daddr_dev == ifa_result->idev->dev)
1084 hiscore.attrs |= IPV6_SADDR_SCORE_OIF;
1085 hiscore.rule++;
1086 }
1087 if (daddr_dev == NULL ||
1088 daddr_dev == ifa->idev->dev) {
1089 score.attrs |= IPV6_SADDR_SCORE_OIF;
1090 if (!(hiscore.attrs & IPV6_SADDR_SCORE_OIF)) {
1091 score.rule = 5;
1092 goto record_it;
1093 }
1094 } else {
1095 if (hiscore.attrs & IPV6_SADDR_SCORE_OIF)
1096 continue;
1097 }
1098
1099 /* Rule 6: Prefer matching label */
1100 if (hiscore.rule < 6) {
YOSHIFUJI Hideakic1ee6562007-11-14 15:55:29 +09001101 if (ipv6_addr_label(&ifa_result->addr,
YOSHIFUJI Hideaki303065a2007-11-14 15:56:15 +09001102 hiscore.addr_type,
1103 ifa_result->idev->dev->ifindex) == daddr_label)
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001104 hiscore.attrs |= IPV6_SADDR_SCORE_LABEL;
1105 hiscore.rule++;
1106 }
YOSHIFUJI Hideakic1ee6562007-11-14 15:55:29 +09001107 if (ipv6_addr_label(&ifa->addr,
YOSHIFUJI Hideaki303065a2007-11-14 15:56:15 +09001108 score.addr_type,
1109 ifa->idev->dev->ifindex) == daddr_label) {
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001110 score.attrs |= IPV6_SADDR_SCORE_LABEL;
1111 if (!(hiscore.attrs & IPV6_SADDR_SCORE_LABEL)) {
1112 score.rule = 6;
1113 goto record_it;
1114 }
1115 } else {
1116 if (hiscore.attrs & IPV6_SADDR_SCORE_LABEL)
1117 continue;
1118 }
1119
Peter Chubb44fd0262005-11-09 13:05:47 -08001120#ifdef CONFIG_IPV6_PRIVACY
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001121 /* Rule 7: Prefer public address
1122 * Note: prefer temprary address if use_tempaddr >= 2
1123 */
1124 if (hiscore.rule < 7) {
1125 if ((!(ifa_result->flags & IFA_F_TEMPORARY)) ^
1126 (ifa_result->idev->cnf.use_tempaddr >= 2))
1127 hiscore.attrs |= IPV6_SADDR_SCORE_PRIVACY;
1128 hiscore.rule++;
1129 }
1130 if ((!(ifa->flags & IFA_F_TEMPORARY)) ^
1131 (ifa->idev->cnf.use_tempaddr >= 2)) {
1132 score.attrs |= IPV6_SADDR_SCORE_PRIVACY;
1133 if (!(hiscore.attrs & IPV6_SADDR_SCORE_PRIVACY)) {
1134 score.rule = 7;
1135 goto record_it;
1136 }
1137 } else {
1138 if (hiscore.attrs & IPV6_SADDR_SCORE_PRIVACY)
1139 continue;
1140 }
YOSHIFUJI Hideaki5e2707f2006-06-22 01:41:18 -07001141#else
1142 if (hiscore.rule < 7)
1143 hiscore.rule++;
Peter Chubb44fd0262005-11-09 13:05:47 -08001144#endif
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001145 /* Rule 8: Use longest matching prefix */
Yan Zheng12da2a42005-11-14 21:42:46 -08001146 if (hiscore.rule < 8) {
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001147 hiscore.matchlen = ipv6_addr_diff(&ifa_result->addr, daddr);
Yan Zheng12da2a42005-11-14 21:42:46 -08001148 hiscore.rule++;
1149 }
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001150 score.matchlen = ipv6_addr_diff(&ifa->addr, daddr);
1151 if (score.matchlen > hiscore.matchlen) {
1152 score.rule = 8;
1153 goto record_it;
1154 }
1155#if 0
1156 else if (score.matchlen < hiscore.matchlen)
1157 continue;
1158#endif
1159
1160 /* Final Rule: choose first available one */
1161 continue;
1162record_it:
1163 if (ifa_result)
1164 in6_ifa_put(ifa_result);
1165 in6_ifa_hold(ifa);
1166 ifa_result = ifa;
1167 hiscore = score;
1168 }
1169 read_unlock_bh(&idev->lock);
1170 }
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001171 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 read_unlock(&dev_base_lock);
1173
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001174 if (!ifa_result)
1175 return -EADDRNOTAVAIL;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001176
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001177 ipv6_addr_copy(saddr, &ifa_result->addr);
1178 in6_ifa_put(ifa_result);
1179 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180}
1181
1182
1183int ipv6_get_saddr(struct dst_entry *dst,
1184 struct in6_addr *daddr, struct in6_addr *saddr)
1185{
YOSHIFUJI Hideaki7a3025b2006-10-13 16:17:25 +09001186 return ipv6_dev_get_saddr(dst ? ip6_dst_idev(dst)->dev : NULL, daddr, saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187}
1188
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +09001189EXPORT_SYMBOL(ipv6_get_saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
Neil Horman95c385b2007-04-25 17:08:10 -07001191int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr,
1192 unsigned char banned_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193{
1194 struct inet6_dev *idev;
1195 int err = -EADDRNOTAVAIL;
1196
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001197 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 if ((idev = __in6_dev_get(dev)) != NULL) {
1199 struct inet6_ifaddr *ifp;
1200
1201 read_lock_bh(&idev->lock);
1202 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
Neil Horman95c385b2007-04-25 17:08:10 -07001203 if (ifp->scope == IFA_LINK && !(ifp->flags & banned_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 ipv6_addr_copy(addr, &ifp->addr);
1205 err = 0;
1206 break;
1207 }
1208 }
1209 read_unlock_bh(&idev->lock);
1210 }
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001211 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 return err;
1213}
1214
1215static int ipv6_count_addresses(struct inet6_dev *idev)
1216{
1217 int cnt = 0;
1218 struct inet6_ifaddr *ifp;
1219
1220 read_lock_bh(&idev->lock);
1221 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next)
1222 cnt++;
1223 read_unlock_bh(&idev->lock);
1224 return cnt;
1225}
1226
Daniel Lezcanobfeade02008-01-10 22:43:18 -08001227int ipv6_chk_addr(struct net *net, struct in6_addr *addr,
1228 struct net_device *dev, int strict)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229{
1230 struct inet6_ifaddr * ifp;
1231 u8 hash = ipv6_addr_hash(addr);
1232
1233 read_lock_bh(&addrconf_hash_lock);
1234 for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
Daniel Lezcanobfeade02008-01-10 22:43:18 -08001235 if (ifp->idev->dev->nd_net != net)
1236 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 if (ipv6_addr_equal(&ifp->addr, addr) &&
1238 !(ifp->flags&IFA_F_TENTATIVE)) {
1239 if (dev == NULL || ifp->idev->dev == dev ||
1240 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))
1241 break;
1242 }
1243 }
1244 read_unlock_bh(&addrconf_hash_lock);
1245 return ifp != NULL;
1246}
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +09001247EXPORT_SYMBOL(ipv6_chk_addr);
1248
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249static
Daniel Lezcano06bfe652008-01-10 22:43:42 -08001250int ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
1251 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252{
1253 struct inet6_ifaddr * ifp;
1254 u8 hash = ipv6_addr_hash(addr);
1255
1256 for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
Daniel Lezcano06bfe652008-01-10 22:43:42 -08001257 if (ifp->idev->dev->nd_net != net)
1258 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 if (ipv6_addr_equal(&ifp->addr, addr)) {
1260 if (dev == NULL || ifp->idev->dev == dev)
1261 break;
1262 }
1263 }
1264 return ifp != NULL;
1265}
1266
Daniel Lezcano1cab3da2008-01-10 22:44:09 -08001267struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, struct in6_addr *addr,
1268 struct net_device *dev, int strict)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269{
1270 struct inet6_ifaddr * ifp;
1271 u8 hash = ipv6_addr_hash(addr);
1272
1273 read_lock_bh(&addrconf_hash_lock);
1274 for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
Daniel Lezcano1cab3da2008-01-10 22:44:09 -08001275 if (ifp->idev->dev->nd_net != net)
1276 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 if (ipv6_addr_equal(&ifp->addr, addr)) {
1278 if (dev == NULL || ifp->idev->dev == dev ||
1279 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
1280 in6_ifa_hold(ifp);
1281 break;
1282 }
1283 }
1284 }
1285 read_unlock_bh(&addrconf_hash_lock);
1286
1287 return ifp;
1288}
1289
1290int ipv6_rcv_saddr_equal(const struct sock *sk, const struct sock *sk2)
1291{
1292 const struct in6_addr *sk_rcv_saddr6 = &inet6_sk(sk)->rcv_saddr;
Arnaldo Carvalho de Melo0fa1a532005-12-13 23:23:09 -08001293 const struct in6_addr *sk2_rcv_saddr6 = inet6_rcv_saddr(sk2);
Al Viro82103232006-09-27 18:44:10 -07001294 __be32 sk_rcv_saddr = inet_sk(sk)->rcv_saddr;
1295 __be32 sk2_rcv_saddr = inet_rcv_saddr(sk2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 int sk_ipv6only = ipv6_only_sock(sk);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001297 int sk2_ipv6only = inet_v6_ipv6only(sk2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 int addr_type = ipv6_addr_type(sk_rcv_saddr6);
1299 int addr_type2 = sk2_rcv_saddr6 ? ipv6_addr_type(sk2_rcv_saddr6) : IPV6_ADDR_MAPPED;
1300
1301 if (!sk2_rcv_saddr && !sk_ipv6only)
1302 return 1;
1303
1304 if (addr_type2 == IPV6_ADDR_ANY &&
1305 !(sk2_ipv6only && addr_type == IPV6_ADDR_MAPPED))
1306 return 1;
1307
1308 if (addr_type == IPV6_ADDR_ANY &&
1309 !(sk_ipv6only && addr_type2 == IPV6_ADDR_MAPPED))
1310 return 1;
1311
1312 if (sk2_rcv_saddr6 &&
1313 ipv6_addr_equal(sk_rcv_saddr6, sk2_rcv_saddr6))
1314 return 1;
1315
1316 if (addr_type == IPV6_ADDR_MAPPED &&
1317 !sk2_ipv6only &&
1318 (!sk2_rcv_saddr || !sk_rcv_saddr || sk_rcv_saddr == sk2_rcv_saddr))
1319 return 1;
1320
1321 return 0;
1322}
1323
1324/* Gets referenced address, destroys ifaddr */
1325
Adrian Bunk9f5336e2006-01-07 13:24:25 -08001326static void addrconf_dad_stop(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 if (ifp->flags&IFA_F_PERMANENT) {
1329 spin_lock_bh(&ifp->lock);
1330 addrconf_del_timer(ifp);
1331 ifp->flags |= IFA_F_TENTATIVE;
1332 spin_unlock_bh(&ifp->lock);
1333 in6_ifa_put(ifp);
1334#ifdef CONFIG_IPV6_PRIVACY
1335 } else if (ifp->flags&IFA_F_TEMPORARY) {
1336 struct inet6_ifaddr *ifpub;
1337 spin_lock_bh(&ifp->lock);
1338 ifpub = ifp->ifpub;
1339 if (ifpub) {
1340 in6_ifa_hold(ifpub);
1341 spin_unlock_bh(&ifp->lock);
1342 ipv6_create_tempaddr(ifpub, ifp);
1343 in6_ifa_put(ifpub);
1344 } else {
1345 spin_unlock_bh(&ifp->lock);
1346 }
1347 ipv6_del_addr(ifp);
1348#endif
1349 } else
1350 ipv6_del_addr(ifp);
1351}
1352
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09001353void addrconf_dad_failure(struct inet6_ifaddr *ifp)
1354{
1355 if (net_ratelimit())
1356 printk(KERN_INFO "%s: duplicate address detected!\n", ifp->idev->dev->name);
1357 addrconf_dad_stop(ifp);
1358}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
1360/* Join to solicited addr multicast group. */
1361
1362void addrconf_join_solict(struct net_device *dev, struct in6_addr *addr)
1363{
1364 struct in6_addr maddr;
1365
1366 if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1367 return;
1368
1369 addrconf_addr_solict_mult(addr, &maddr);
1370 ipv6_dev_mc_inc(dev, &maddr);
1371}
1372
1373void addrconf_leave_solict(struct inet6_dev *idev, struct in6_addr *addr)
1374{
1375 struct in6_addr maddr;
1376
1377 if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1378 return;
1379
1380 addrconf_addr_solict_mult(addr, &maddr);
1381 __ipv6_dev_mc_dec(idev, &maddr);
1382}
1383
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03001384static void addrconf_join_anycast(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385{
1386 struct in6_addr addr;
1387 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1388 if (ipv6_addr_any(&addr))
1389 return;
1390 ipv6_dev_ac_inc(ifp->idev->dev, &addr);
1391}
1392
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03001393static void addrconf_leave_anycast(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394{
1395 struct in6_addr addr;
1396 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1397 if (ipv6_addr_any(&addr))
1398 return;
1399 __ipv6_dev_ac_dec(ifp->idev, &addr);
1400}
1401
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08001402static int addrconf_ifid_eui48(u8 *eui, struct net_device *dev)
1403{
1404 if (dev->addr_len != ETH_ALEN)
1405 return -1;
1406 memcpy(eui, dev->dev_addr, 3);
1407 memcpy(eui + 5, dev->dev_addr + 3, 3);
1408
1409 /*
1410 * The zSeries OSA network cards can be shared among various
1411 * OS instances, but the OSA cards have only one MAC address.
1412 * This leads to duplicate address conflicts in conjunction
1413 * with IPv6 if more than one instance uses the same card.
1414 *
1415 * The driver for these cards can deliver a unique 16-bit
1416 * identifier for each instance sharing the same card. It is
1417 * placed instead of 0xFFFE in the interface identifier. The
1418 * "u" bit of the interface identifier is not inverted in this
1419 * case. Hence the resulting interface identifier has local
1420 * scope according to RFC2373.
1421 */
1422 if (dev->dev_id) {
1423 eui[3] = (dev->dev_id >> 8) & 0xFF;
1424 eui[4] = dev->dev_id & 0xFF;
1425 } else {
1426 eui[3] = 0xFF;
1427 eui[4] = 0xFE;
1428 eui[0] ^= 2;
1429 }
1430 return 0;
1431}
1432
1433static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev)
1434{
1435 /* XXX: inherit EUI-64 from other interface -- yoshfuji */
1436 if (dev->addr_len != ARCNET_ALEN)
1437 return -1;
1438 memset(eui, 0, 7);
1439 eui[7] = *(u8*)dev->dev_addr;
1440 return 0;
1441}
1442
1443static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev)
1444{
1445 if (dev->addr_len != INFINIBAND_ALEN)
1446 return -1;
1447 memcpy(eui, dev->dev_addr + 12, 8);
1448 eui[0] |= 2;
1449 return 0;
1450}
1451
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
1453{
1454 switch (dev->type) {
1455 case ARPHRD_ETHER:
1456 case ARPHRD_FDDI:
1457 case ARPHRD_IEEE802_TR:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08001458 return addrconf_ifid_eui48(eui, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 case ARPHRD_ARCNET:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08001460 return addrconf_ifid_arcnet(eui, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 case ARPHRD_INFINIBAND:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08001462 return addrconf_ifid_infiniband(eui, dev);
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11001463 case ARPHRD_SIT:
1464 if (dev->priv_flags & IFF_ISATAP)
1465 return ipv6_isatap_eui64(eui, *(__be32 *)dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 }
1467 return -1;
1468}
1469
1470static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
1471{
1472 int err = -1;
1473 struct inet6_ifaddr *ifp;
1474
1475 read_lock_bh(&idev->lock);
1476 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
1477 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
1478 memcpy(eui, ifp->addr.s6_addr+8, 8);
1479 err = 0;
1480 break;
1481 }
1482 }
1483 read_unlock_bh(&idev->lock);
1484 return err;
1485}
1486
1487#ifdef CONFIG_IPV6_PRIVACY
1488/* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
1489static int __ipv6_regen_rndid(struct inet6_dev *idev)
1490{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491regen:
YOSHIFUJI Hideaki955189e2006-03-20 16:54:09 -08001492 get_random_bytes(idev->rndid, sizeof(idev->rndid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 idev->rndid[0] &= ~0x02;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
1495 /*
1496 * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
1497 * check if generated address is not inappropriate
1498 *
1499 * - Reserved subnet anycast (RFC 2526)
1500 * 11111101 11....11 1xxxxxxx
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11001501 * - ISATAP (RFC4214) 6.1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 * 00-00-5E-FE-xx-xx-xx-xx
1503 * - value 0
1504 * - XXX: already assigned to an address on the device
1505 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001506 if (idev->rndid[0] == 0xfd &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff &&
1508 (idev->rndid[7]&0x80))
1509 goto regen;
1510 if ((idev->rndid[0]|idev->rndid[1]) == 0) {
1511 if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe)
1512 goto regen;
1513 if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00)
1514 goto regen;
1515 }
1516
1517 return 0;
1518}
1519
1520static void ipv6_regen_rndid(unsigned long data)
1521{
1522 struct inet6_dev *idev = (struct inet6_dev *) data;
1523 unsigned long expires;
1524
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001525 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 write_lock_bh(&idev->lock);
1527
1528 if (idev->dead)
1529 goto out;
1530
1531 if (__ipv6_regen_rndid(idev) < 0)
1532 goto out;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001533
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 expires = jiffies +
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001535 idev->cnf.temp_prefered_lft * HZ -
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 idev->cnf.regen_max_retry * idev->cnf.dad_transmits * idev->nd_parms->retrans_time - desync_factor;
1537 if (time_before(expires, jiffies)) {
1538 printk(KERN_WARNING
1539 "ipv6_regen_rndid(): too short regeneration interval; timer disabled for %s.\n",
1540 idev->dev->name);
1541 goto out;
1542 }
1543
1544 if (!mod_timer(&idev->regen_timer, expires))
1545 in6_dev_hold(idev);
1546
1547out:
1548 write_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001549 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 in6_dev_put(idev);
1551}
1552
1553static int __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr) {
1554 int ret = 0;
1555
1556 if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0)
1557 ret = __ipv6_regen_rndid(idev);
1558 return ret;
1559}
1560#endif
1561
1562/*
1563 * Add prefix route.
1564 */
1565
1566static void
1567addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev,
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -07001568 unsigned long expires, u32 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569{
Thomas Graf86872cb2006-08-22 00:01:08 -07001570 struct fib6_config cfg = {
1571 .fc_table = RT6_TABLE_PREFIX,
1572 .fc_metric = IP6_RT_PRIO_ADDRCONF,
1573 .fc_ifindex = dev->ifindex,
1574 .fc_expires = expires,
1575 .fc_dst_len = plen,
1576 .fc_flags = RTF_UP | flags,
Benjamin Theryf1243c22008-02-26 18:10:03 -08001577 .fc_nlinfo.nl_net = &init_net,
Thomas Graf86872cb2006-08-22 00:01:08 -07001578 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
Thomas Graf86872cb2006-08-22 00:01:08 -07001580 ipv6_addr_copy(&cfg.fc_dst, pfx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581
1582 /* Prevent useless cloning on PtP SIT.
1583 This thing is done here expecting that the whole
1584 class of non-broadcast devices need not cloning.
1585 */
Joerg Roedel0be669b2006-10-10 14:49:53 -07001586#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
Thomas Graf86872cb2006-08-22 00:01:08 -07001587 if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT))
1588 cfg.fc_flags |= RTF_NONEXTHOP;
Joerg Roedel0be669b2006-10-10 14:49:53 -07001589#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
Thomas Graf86872cb2006-08-22 00:01:08 -07001591 ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592}
1593
1594/* Create "default" multicast route to the interface */
1595
1596static void addrconf_add_mroute(struct net_device *dev)
1597{
Thomas Graf86872cb2006-08-22 00:01:08 -07001598 struct fib6_config cfg = {
1599 .fc_table = RT6_TABLE_LOCAL,
1600 .fc_metric = IP6_RT_PRIO_ADDRCONF,
1601 .fc_ifindex = dev->ifindex,
1602 .fc_dst_len = 8,
1603 .fc_flags = RTF_UP,
Benjamin Theryf1243c22008-02-26 18:10:03 -08001604 .fc_nlinfo.nl_net = &init_net,
Thomas Graf86872cb2006-08-22 00:01:08 -07001605 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606
Thomas Graf86872cb2006-08-22 00:01:08 -07001607 ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0);
1608
1609 ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610}
1611
Joerg Roedel0be669b2006-10-10 14:49:53 -07001612#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613static void sit_route_add(struct net_device *dev)
1614{
Thomas Graf86872cb2006-08-22 00:01:08 -07001615 struct fib6_config cfg = {
1616 .fc_table = RT6_TABLE_MAIN,
1617 .fc_metric = IP6_RT_PRIO_ADDRCONF,
1618 .fc_ifindex = dev->ifindex,
1619 .fc_dst_len = 96,
1620 .fc_flags = RTF_UP | RTF_NONEXTHOP,
Benjamin Theryf1243c22008-02-26 18:10:03 -08001621 .fc_nlinfo.nl_net = &init_net,
Thomas Graf86872cb2006-08-22 00:01:08 -07001622 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
1624 /* prefix length - 96 bits "::d.d.d.d" */
Thomas Graf86872cb2006-08-22 00:01:08 -07001625 ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626}
Joerg Roedel0be669b2006-10-10 14:49:53 -07001627#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628
1629static void addrconf_add_lroute(struct net_device *dev)
1630{
1631 struct in6_addr addr;
1632
1633 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
1634 addrconf_prefix_route(&addr, 64, dev, 0, 0);
1635}
1636
1637static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
1638{
1639 struct inet6_dev *idev;
1640
1641 ASSERT_RTNL();
1642
1643 if ((idev = ipv6_find_idev(dev)) == NULL)
1644 return NULL;
1645
1646 /* Add default multicast route */
1647 addrconf_add_mroute(dev);
1648
1649 /* Add link local route */
1650 addrconf_add_lroute(dev);
1651 return idev;
1652}
1653
1654void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len)
1655{
1656 struct prefix_info *pinfo;
1657 __u32 valid_lft;
1658 __u32 prefered_lft;
1659 int addr_type;
1660 unsigned long rt_expires;
1661 struct inet6_dev *in6_dev;
1662
1663 pinfo = (struct prefix_info *) opt;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001664
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 if (len < sizeof(struct prefix_info)) {
1666 ADBG(("addrconf: prefix option too short\n"));
1667 return;
1668 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001669
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 /*
1671 * Validation checks ([ADDRCONF], page 19)
1672 */
1673
1674 addr_type = ipv6_addr_type(&pinfo->prefix);
1675
1676 if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
1677 return;
1678
1679 valid_lft = ntohl(pinfo->valid);
1680 prefered_lft = ntohl(pinfo->prefered);
1681
1682 if (prefered_lft > valid_lft) {
1683 if (net_ratelimit())
1684 printk(KERN_WARNING "addrconf: prefix option has invalid lifetime\n");
1685 return;
1686 }
1687
1688 in6_dev = in6_dev_get(dev);
1689
1690 if (in6_dev == NULL) {
1691 if (net_ratelimit())
1692 printk(KERN_DEBUG "addrconf: device %s not configured\n", dev->name);
1693 return;
1694 }
1695
1696 /*
1697 * Two things going on here:
1698 * 1) Add routes for on-link prefixes
1699 * 2) Configure prefixes with the auto flag set
1700 */
1701
1702 /* Avoid arithmetic overflow. Really, we could
1703 save rt_expires in seconds, likely valid_lft,
1704 but it would require division in fib gc, that it
1705 not good.
1706 */
1707 if (valid_lft >= 0x7FFFFFFF/HZ)
YOSHIFUJI Hideaki3dd4bc62005-12-19 14:02:45 -08001708 rt_expires = 0x7FFFFFFF - (0x7FFFFFFF % HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 else
YOSHIFUJI Hideaki3dd4bc62005-12-19 14:02:45 -08001710 rt_expires = valid_lft * HZ;
1711
1712 /*
1713 * We convert this (in jiffies) to clock_t later.
1714 * Avoid arithmetic overflow there as well.
1715 * Overflow can happen only if HZ < USER_HZ.
1716 */
1717 if (HZ < USER_HZ && rt_expires > 0x7FFFFFFF / USER_HZ)
1718 rt_expires = 0x7FFFFFFF / USER_HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719
1720 if (pinfo->onlink) {
1721 struct rt6_info *rt;
1722 rt = rt6_lookup(&pinfo->prefix, NULL, dev->ifindex, 1);
1723
1724 if (rt && ((rt->rt6i_flags & (RTF_GATEWAY | RTF_DEFAULT)) == 0)) {
1725 if (rt->rt6i_flags&RTF_EXPIRES) {
1726 if (valid_lft == 0) {
Thomas Grafe0a1ad732006-08-22 00:00:21 -07001727 ip6_del_rt(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 rt = NULL;
1729 } else {
YOSHIFUJI Hideaki3dd4bc62005-12-19 14:02:45 -08001730 rt->rt6i_expires = jiffies + rt_expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 }
1732 }
1733 } else if (valid_lft) {
1734 addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
YOSHIFUJI Hideaki3dd4bc62005-12-19 14:02:45 -08001735 dev, jiffies_to_clock_t(rt_expires), RTF_ADDRCONF|RTF_EXPIRES|RTF_PREFIX_RT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 }
1737 if (rt)
1738 dst_release(&rt->u.dst);
1739 }
1740
1741 /* Try to figure out our local address for this prefix */
1742
1743 if (pinfo->autoconf && in6_dev->cnf.autoconf) {
1744 struct inet6_ifaddr * ifp;
1745 struct in6_addr addr;
1746 int create = 0, update_lft = 0;
1747
1748 if (pinfo->prefix_len == 64) {
1749 memcpy(&addr, &pinfo->prefix, 8);
1750 if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
1751 ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
1752 in6_dev_put(in6_dev);
1753 return;
1754 }
1755 goto ok;
1756 }
1757 if (net_ratelimit())
1758 printk(KERN_DEBUG "IPv6 addrconf: prefix with wrong length %d\n",
1759 pinfo->prefix_len);
1760 in6_dev_put(in6_dev);
1761 return;
1762
1763ok:
1764
Daniel Lezcano1cab3da2008-01-10 22:44:09 -08001765 ifp = ipv6_get_ifaddr(&init_net, &addr, dev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766
1767 if (ifp == NULL && valid_lft) {
1768 int max_addresses = in6_dev->cnf.max_addresses;
Neil Horman95c385b2007-04-25 17:08:10 -07001769 u32 addr_flags = 0;
1770
1771#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1772 if (in6_dev->cnf.optimistic_dad &&
1773 !ipv6_devconf.forwarding)
1774 addr_flags = IFA_F_OPTIMISTIC;
1775#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776
1777 /* Do not allow to create too much of autoconfigured
1778 * addresses; this would be too easy way to crash kernel.
1779 */
1780 if (!max_addresses ||
1781 ipv6_count_addresses(in6_dev) < max_addresses)
1782 ifp = ipv6_add_addr(in6_dev, &addr, pinfo->prefix_len,
Neil Horman95c385b2007-04-25 17:08:10 -07001783 addr_type&IPV6_ADDR_SCOPE_MASK,
1784 addr_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785
1786 if (!ifp || IS_ERR(ifp)) {
1787 in6_dev_put(in6_dev);
1788 return;
1789 }
1790
1791 update_lft = create = 1;
1792 ifp->cstamp = jiffies;
1793 addrconf_dad_start(ifp, RTF_ADDRCONF|RTF_PREFIX_RT);
1794 }
1795
1796 if (ifp) {
1797 int flags;
1798 unsigned long now;
1799#ifdef CONFIG_IPV6_PRIVACY
1800 struct inet6_ifaddr *ift;
1801#endif
1802 u32 stored_lft;
1803
1804 /* update lifetime (RFC2462 5.5.3 e) */
1805 spin_lock(&ifp->lock);
1806 now = jiffies;
1807 if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
1808 stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
1809 else
1810 stored_lft = 0;
1811 if (!update_lft && stored_lft) {
1812 if (valid_lft > MIN_VALID_LIFETIME ||
1813 valid_lft > stored_lft)
1814 update_lft = 1;
1815 else if (stored_lft <= MIN_VALID_LIFETIME) {
1816 /* valid_lft <= stored_lft is always true */
1817 /* XXX: IPsec */
1818 update_lft = 0;
1819 } else {
1820 valid_lft = MIN_VALID_LIFETIME;
1821 if (valid_lft < prefered_lft)
1822 prefered_lft = valid_lft;
1823 update_lft = 1;
1824 }
1825 }
1826
1827 if (update_lft) {
1828 ifp->valid_lft = valid_lft;
1829 ifp->prefered_lft = prefered_lft;
1830 ifp->tstamp = now;
1831 flags = ifp->flags;
1832 ifp->flags &= ~IFA_F_DEPRECATED;
1833 spin_unlock(&ifp->lock);
1834
1835 if (!(flags&IFA_F_TENTATIVE))
1836 ipv6_ifa_notify(0, ifp);
1837 } else
1838 spin_unlock(&ifp->lock);
1839
1840#ifdef CONFIG_IPV6_PRIVACY
1841 read_lock_bh(&in6_dev->lock);
1842 /* update all temporary addresses in the list */
1843 for (ift=in6_dev->tempaddr_list; ift; ift=ift->tmp_next) {
1844 /*
1845 * When adjusting the lifetimes of an existing
1846 * temporary address, only lower the lifetimes.
1847 * Implementations must not increase the
1848 * lifetimes of an existing temporary address
1849 * when processing a Prefix Information Option.
1850 */
Benoit Boissinotc6fbfac2008-04-02 00:00:58 -07001851 if (ifp != ift->ifpub)
1852 continue;
1853
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 spin_lock(&ift->lock);
1855 flags = ift->flags;
1856 if (ift->valid_lft > valid_lft &&
1857 ift->valid_lft - valid_lft > (jiffies - ift->tstamp) / HZ)
1858 ift->valid_lft = valid_lft + (jiffies - ift->tstamp) / HZ;
1859 if (ift->prefered_lft > prefered_lft &&
1860 ift->prefered_lft - prefered_lft > (jiffies - ift->tstamp) / HZ)
1861 ift->prefered_lft = prefered_lft + (jiffies - ift->tstamp) / HZ;
1862 spin_unlock(&ift->lock);
1863 if (!(flags&IFA_F_TENTATIVE))
1864 ipv6_ifa_notify(0, ift);
1865 }
1866
1867 if (create && in6_dev->cnf.use_tempaddr > 0) {
1868 /*
1869 * When a new public address is created as described in [ADDRCONF],
1870 * also create a new temporary address.
1871 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001872 read_unlock_bh(&in6_dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 ipv6_create_tempaddr(ifp, NULL);
1874 } else {
1875 read_unlock_bh(&in6_dev->lock);
1876 }
1877#endif
1878 in6_ifa_put(ifp);
1879 addrconf_verify(0);
1880 }
1881 }
1882 inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
1883 in6_dev_put(in6_dev);
1884}
1885
1886/*
1887 * Set destination address.
1888 * Special case for SIT interfaces where we create a new "virtual"
1889 * device.
1890 */
1891int addrconf_set_dstaddr(void __user *arg)
1892{
1893 struct in6_ifreq ireq;
1894 struct net_device *dev;
1895 int err = -EINVAL;
1896
1897 rtnl_lock();
1898
1899 err = -EFAULT;
1900 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
1901 goto err_exit;
1902
Eric W. Biederman881d9662007-09-17 11:56:21 -07001903 dev = __dev_get_by_index(&init_net, ireq.ifr6_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904
1905 err = -ENODEV;
1906 if (dev == NULL)
1907 goto err_exit;
1908
Joerg Roedel0be669b2006-10-10 14:49:53 -07001909#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 if (dev->type == ARPHRD_SIT) {
1911 struct ifreq ifr;
1912 mm_segment_t oldfs;
1913 struct ip_tunnel_parm p;
1914
1915 err = -EADDRNOTAVAIL;
1916 if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4))
1917 goto err_exit;
1918
1919 memset(&p, 0, sizeof(p));
1920 p.iph.daddr = ireq.ifr6_addr.s6_addr32[3];
1921 p.iph.saddr = 0;
1922 p.iph.version = 4;
1923 p.iph.ihl = 5;
1924 p.iph.protocol = IPPROTO_IPV6;
1925 p.iph.ttl = 64;
Stephen Hemmingerd20b3102008-01-21 00:48:43 -08001926 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927
1928 oldfs = get_fs(); set_fs(KERNEL_DS);
1929 err = dev->do_ioctl(dev, &ifr, SIOCADDTUNNEL);
1930 set_fs(oldfs);
1931
1932 if (err == 0) {
1933 err = -ENOBUFS;
Eric W. Biederman881d9662007-09-17 11:56:21 -07001934 if ((dev = __dev_get_by_name(&init_net, p.name)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 goto err_exit;
1936 err = dev_open(dev);
1937 }
1938 }
Joerg Roedel0be669b2006-10-10 14:49:53 -07001939#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940
1941err_exit:
1942 rtnl_unlock();
1943 return err;
1944}
1945
1946/*
1947 * Manual configuration of address on an interface
1948 */
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09001949static int inet6_addr_add(int ifindex, struct in6_addr *pfx, int plen,
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07001950 __u8 ifa_flags, __u32 prefered_lft, __u32 valid_lft)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951{
1952 struct inet6_ifaddr *ifp;
1953 struct inet6_dev *idev;
1954 struct net_device *dev;
1955 int scope;
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09001956 u32 flags = RTF_EXPIRES;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957
1958 ASSERT_RTNL();
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001959
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09001960 /* check the lifetime */
1961 if (!valid_lft || prefered_lft > valid_lft)
1962 return -EINVAL;
1963
Eric W. Biederman881d9662007-09-17 11:56:21 -07001964 if ((dev = __dev_get_by_index(&init_net, ifindex)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 return -ENODEV;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001966
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 if ((idev = addrconf_add_dev(dev)) == NULL)
1968 return -ENOBUFS;
1969
1970 scope = ipv6_addr_scope(pfx);
1971
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09001972 if (valid_lft == INFINITY_LIFE_TIME) {
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09001973 ifa_flags |= IFA_F_PERMANENT;
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09001974 flags = 0;
1975 } else if (valid_lft >= 0x7FFFFFFF/HZ)
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09001976 valid_lft = 0x7FFFFFFF/HZ;
1977
1978 if (prefered_lft == 0)
1979 ifa_flags |= IFA_F_DEPRECATED;
1980 else if ((prefered_lft >= 0x7FFFFFFF/HZ) &&
1981 (prefered_lft != INFINITY_LIFE_TIME))
1982 prefered_lft = 0x7FFFFFFF/HZ;
1983
1984 ifp = ipv6_add_addr(idev, pfx, plen, scope, ifa_flags);
1985
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 if (!IS_ERR(ifp)) {
Herbert Xu06aebfb2006-08-09 16:52:04 -07001987 spin_lock_bh(&ifp->lock);
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09001988 ifp->valid_lft = valid_lft;
1989 ifp->prefered_lft = prefered_lft;
1990 ifp->tstamp = jiffies;
Herbert Xu06aebfb2006-08-09 16:52:04 -07001991 spin_unlock_bh(&ifp->lock);
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09001992
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09001993 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev,
1994 jiffies_to_clock_t(valid_lft * HZ), flags);
Neil Horman95c385b2007-04-25 17:08:10 -07001995 /*
1996 * Note that section 3.1 of RFC 4429 indicates
1997 * that the Optimistic flag should not be set for
1998 * manually configured addresses
1999 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 addrconf_dad_start(ifp, 0);
2001 in6_ifa_put(ifp);
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002002 addrconf_verify(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 return 0;
2004 }
2005
2006 return PTR_ERR(ifp);
2007}
2008
2009static int inet6_addr_del(int ifindex, struct in6_addr *pfx, int plen)
2010{
2011 struct inet6_ifaddr *ifp;
2012 struct inet6_dev *idev;
2013 struct net_device *dev;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002014
Eric W. Biederman881d9662007-09-17 11:56:21 -07002015 if ((dev = __dev_get_by_index(&init_net, ifindex)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 return -ENODEV;
2017
2018 if ((idev = __in6_dev_get(dev)) == NULL)
2019 return -ENXIO;
2020
2021 read_lock_bh(&idev->lock);
2022 for (ifp = idev->addr_list; ifp; ifp=ifp->if_next) {
2023 if (ifp->prefix_len == plen &&
2024 ipv6_addr_equal(pfx, &ifp->addr)) {
2025 in6_ifa_hold(ifp);
2026 read_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002027
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 ipv6_del_addr(ifp);
2029
2030 /* If the last address is deleted administratively,
2031 disable IPv6 on this interface.
2032 */
2033 if (idev->addr_list == NULL)
2034 addrconf_ifdown(idev->dev, 1);
2035 return 0;
2036 }
2037 }
2038 read_unlock_bh(&idev->lock);
2039 return -EADDRNOTAVAIL;
2040}
2041
2042
2043int addrconf_add_ifaddr(void __user *arg)
2044{
2045 struct in6_ifreq ireq;
2046 int err;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002047
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 if (!capable(CAP_NET_ADMIN))
2049 return -EPERM;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002050
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2052 return -EFAULT;
2053
2054 rtnl_lock();
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002055 err = inet6_addr_add(ireq.ifr6_ifindex, &ireq.ifr6_addr, ireq.ifr6_prefixlen,
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07002056 IFA_F_PERMANENT, INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 rtnl_unlock();
2058 return err;
2059}
2060
2061int addrconf_del_ifaddr(void __user *arg)
2062{
2063 struct in6_ifreq ireq;
2064 int err;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002065
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 if (!capable(CAP_NET_ADMIN))
2067 return -EPERM;
2068
2069 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2070 return -EFAULT;
2071
2072 rtnl_lock();
2073 err = inet6_addr_del(ireq.ifr6_ifindex, &ireq.ifr6_addr, ireq.ifr6_prefixlen);
2074 rtnl_unlock();
2075 return err;
2076}
2077
Joerg Roedel0be669b2006-10-10 14:49:53 -07002078#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079static void sit_add_v4_addrs(struct inet6_dev *idev)
2080{
2081 struct inet6_ifaddr * ifp;
2082 struct in6_addr addr;
2083 struct net_device *dev;
2084 int scope;
2085
2086 ASSERT_RTNL();
2087
2088 memset(&addr, 0, sizeof(struct in6_addr));
2089 memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
2090
2091 if (idev->dev->flags&IFF_POINTOPOINT) {
2092 addr.s6_addr32[0] = htonl(0xfe800000);
2093 scope = IFA_LINK;
2094 } else {
2095 scope = IPV6_ADDR_COMPATv4;
2096 }
2097
2098 if (addr.s6_addr32[3]) {
2099 ifp = ipv6_add_addr(idev, &addr, 128, scope, IFA_F_PERMANENT);
2100 if (!IS_ERR(ifp)) {
2101 spin_lock_bh(&ifp->lock);
2102 ifp->flags &= ~IFA_F_TENTATIVE;
2103 spin_unlock_bh(&ifp->lock);
2104 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2105 in6_ifa_put(ifp);
2106 }
2107 return;
2108 }
2109
Eric W. Biederman881d9662007-09-17 11:56:21 -07002110 for_each_netdev(&init_net, dev) {
Herbert Xue5ed6392005-10-03 14:35:55 -07002111 struct in_device * in_dev = __in_dev_get_rtnl(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 if (in_dev && (dev->flags & IFF_UP)) {
2113 struct in_ifaddr * ifa;
2114
2115 int flag = scope;
2116
2117 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
2118 int plen;
2119
2120 addr.s6_addr32[3] = ifa->ifa_local;
2121
2122 if (ifa->ifa_scope == RT_SCOPE_LINK)
2123 continue;
2124 if (ifa->ifa_scope >= RT_SCOPE_HOST) {
2125 if (idev->dev->flags&IFF_POINTOPOINT)
2126 continue;
2127 flag |= IFA_HOST;
2128 }
2129 if (idev->dev->flags&IFF_POINTOPOINT)
2130 plen = 64;
2131 else
2132 plen = 96;
2133
2134 ifp = ipv6_add_addr(idev, &addr, plen, flag,
2135 IFA_F_PERMANENT);
2136 if (!IS_ERR(ifp)) {
2137 spin_lock_bh(&ifp->lock);
2138 ifp->flags &= ~IFA_F_TENTATIVE;
2139 spin_unlock_bh(&ifp->lock);
2140 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2141 in6_ifa_put(ifp);
2142 }
2143 }
2144 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002145 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146}
Joerg Roedel0be669b2006-10-10 14:49:53 -07002147#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148
2149static void init_loopback(struct net_device *dev)
2150{
2151 struct inet6_dev *idev;
2152 struct inet6_ifaddr * ifp;
2153
2154 /* ::1 */
2155
2156 ASSERT_RTNL();
2157
2158 if ((idev = ipv6_find_idev(dev)) == NULL) {
2159 printk(KERN_DEBUG "init loopback: add_dev failed\n");
2160 return;
2161 }
2162
2163 ifp = ipv6_add_addr(idev, &in6addr_loopback, 128, IFA_HOST, IFA_F_PERMANENT);
2164 if (!IS_ERR(ifp)) {
2165 spin_lock_bh(&ifp->lock);
2166 ifp->flags &= ~IFA_F_TENTATIVE;
2167 spin_unlock_bh(&ifp->lock);
2168 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2169 in6_ifa_put(ifp);
2170 }
2171}
2172
2173static void addrconf_add_linklocal(struct inet6_dev *idev, struct in6_addr *addr)
2174{
2175 struct inet6_ifaddr * ifp;
Neil Horman95c385b2007-04-25 17:08:10 -07002176 u32 addr_flags = IFA_F_PERMANENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177
Neil Horman95c385b2007-04-25 17:08:10 -07002178#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
2179 if (idev->cnf.optimistic_dad &&
2180 !ipv6_devconf.forwarding)
2181 addr_flags |= IFA_F_OPTIMISTIC;
2182#endif
2183
2184
2185 ifp = ipv6_add_addr(idev, addr, 64, IFA_LINK, addr_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 if (!IS_ERR(ifp)) {
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09002187 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, idev->dev, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 addrconf_dad_start(ifp, 0);
2189 in6_ifa_put(ifp);
2190 }
2191}
2192
2193static void addrconf_dev_config(struct net_device *dev)
2194{
2195 struct in6_addr addr;
2196 struct inet6_dev * idev;
2197
2198 ASSERT_RTNL();
2199
Herbert Xu74235a22007-06-14 13:02:55 -07002200 if ((dev->type != ARPHRD_ETHER) &&
2201 (dev->type != ARPHRD_FDDI) &&
2202 (dev->type != ARPHRD_IEEE802_TR) &&
2203 (dev->type != ARPHRD_ARCNET) &&
2204 (dev->type != ARPHRD_INFINIBAND)) {
2205 /* Alas, we support only Ethernet autoconfiguration. */
2206 return;
2207 }
2208
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 idev = addrconf_add_dev(dev);
2210 if (idev == NULL)
2211 return;
2212
2213 memset(&addr, 0, sizeof(struct in6_addr));
2214 addr.s6_addr32[0] = htonl(0xFE800000);
2215
2216 if (ipv6_generate_eui64(addr.s6_addr + 8, dev) == 0)
2217 addrconf_add_linklocal(idev, &addr);
2218}
2219
Joerg Roedel0be669b2006-10-10 14:49:53 -07002220#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221static void addrconf_sit_config(struct net_device *dev)
2222{
2223 struct inet6_dev *idev;
2224
2225 ASSERT_RTNL();
2226
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002227 /*
2228 * Configure the tunnel with one of our IPv4
2229 * addresses... we should configure all of
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 * our v4 addrs in the tunnel
2231 */
2232
2233 if ((idev = ipv6_find_idev(dev)) == NULL) {
2234 printk(KERN_DEBUG "init sit: add_dev failed\n");
2235 return;
2236 }
2237
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11002238 if (dev->priv_flags & IFF_ISATAP) {
2239 struct in6_addr addr;
2240
2241 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
2242 addrconf_prefix_route(&addr, 64, dev, 0, 0);
2243 if (!ipv6_generate_eui64(addr.s6_addr + 8, dev))
2244 addrconf_add_linklocal(idev, &addr);
2245 return;
2246 }
2247
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 sit_add_v4_addrs(idev);
2249
2250 if (dev->flags&IFF_POINTOPOINT) {
2251 addrconf_add_mroute(dev);
2252 addrconf_add_lroute(dev);
2253 } else
2254 sit_route_add(dev);
2255}
Joerg Roedel0be669b2006-10-10 14:49:53 -07002256#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257
2258static inline int
2259ipv6_inherit_linklocal(struct inet6_dev *idev, struct net_device *link_dev)
2260{
2261 struct in6_addr lladdr;
2262
Neil Horman95c385b2007-04-25 17:08:10 -07002263 if (!ipv6_get_lladdr(link_dev, &lladdr, IFA_F_TENTATIVE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 addrconf_add_linklocal(idev, &lladdr);
2265 return 0;
2266 }
2267 return -1;
2268}
2269
2270static void ip6_tnl_add_linklocal(struct inet6_dev *idev)
2271{
2272 struct net_device *link_dev;
2273
2274 /* first try to inherit the link-local address from the link device */
2275 if (idev->dev->iflink &&
Eric W. Biederman881d9662007-09-17 11:56:21 -07002276 (link_dev = __dev_get_by_index(&init_net, idev->dev->iflink))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 if (!ipv6_inherit_linklocal(idev, link_dev))
2278 return;
2279 }
2280 /* then try to inherit it from any device */
Eric W. Biederman881d9662007-09-17 11:56:21 -07002281 for_each_netdev(&init_net, link_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 if (!ipv6_inherit_linklocal(idev, link_dev))
2283 return;
2284 }
2285 printk(KERN_DEBUG "init ip6-ip6: add_linklocal failed\n");
2286}
2287
2288/*
2289 * Autoconfigure tunnel with a link-local address so routing protocols,
2290 * DHCPv6, MLD etc. can be run over the virtual link
2291 */
2292
2293static void addrconf_ip6_tnl_config(struct net_device *dev)
2294{
2295 struct inet6_dev *idev;
2296
2297 ASSERT_RTNL();
2298
2299 if ((idev = addrconf_add_dev(dev)) == NULL) {
2300 printk(KERN_DEBUG "init ip6-ip6: add_dev failed\n");
2301 return;
2302 }
2303 ip6_tnl_add_linklocal(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304}
2305
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002306static int addrconf_notify(struct notifier_block *this, unsigned long event,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 void * data)
2308{
2309 struct net_device *dev = (struct net_device *) data;
Herbert Xu74235a22007-06-14 13:02:55 -07002310 struct inet6_dev *idev = __in6_dev_get(dev);
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09002311 int run_pending = 0;
Herbert Xub217d612007-07-30 17:04:52 -07002312 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02002314 if (dev->nd_net != &init_net)
2315 return NOTIFY_DONE;
2316
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 switch(event) {
YOSHIFUJI Hideaki45ba9dd2007-02-15 02:07:27 +09002318 case NETDEV_REGISTER:
Herbert Xu74235a22007-06-14 13:02:55 -07002319 if (!idev && dev->mtu >= IPV6_MIN_MTU) {
YOSHIFUJI Hideaki45ba9dd2007-02-15 02:07:27 +09002320 idev = ipv6_add_dev(dev);
2321 if (!idev)
Herbert Xub217d612007-07-30 17:04:52 -07002322 return notifier_from_errno(-ENOMEM);
YOSHIFUJI Hideaki45ba9dd2007-02-15 02:07:27 +09002323 }
2324 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 case NETDEV_UP:
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002326 case NETDEV_CHANGE:
Jay Vosburghc2edacf2007-07-09 10:42:47 -07002327 if (dev->flags & IFF_SLAVE)
2328 break;
2329
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002330 if (event == NETDEV_UP) {
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -07002331 if (!addrconf_qdisc_ok(dev)) {
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002332 /* device is not ready yet. */
2333 printk(KERN_INFO
2334 "ADDRCONF(NETDEV_UP): %s: "
2335 "link is not ready\n",
2336 dev->name);
2337 break;
2338 }
Kristian Slavov99081042006-02-08 16:10:53 -08002339
Evgeniy Polyakovd31c7b82007-11-30 23:36:08 +11002340 if (!idev && dev->mtu >= IPV6_MIN_MTU)
2341 idev = ipv6_add_dev(dev);
2342
Kristian Slavov99081042006-02-08 16:10:53 -08002343 if (idev)
2344 idev->if_flags |= IF_READY;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002345 } else {
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -07002346 if (!addrconf_qdisc_ok(dev)) {
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002347 /* device is still not ready. */
2348 break;
2349 }
2350
2351 if (idev) {
2352 if (idev->if_flags & IF_READY) {
2353 /* device is already configured. */
2354 break;
2355 }
2356 idev->if_flags |= IF_READY;
2357 }
2358
2359 printk(KERN_INFO
2360 "ADDRCONF(NETDEV_CHANGE): %s: "
2361 "link becomes ready\n",
2362 dev->name);
2363
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09002364 run_pending = 1;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002365 }
2366
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 switch(dev->type) {
Joerg Roedel0be669b2006-10-10 14:49:53 -07002368#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 case ARPHRD_SIT:
2370 addrconf_sit_config(dev);
2371 break;
Joerg Roedel0be669b2006-10-10 14:49:53 -07002372#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 case ARPHRD_TUNNEL6:
2374 addrconf_ip6_tnl_config(dev);
2375 break;
2376 case ARPHRD_LOOPBACK:
2377 init_loopback(dev);
2378 break;
2379
2380 default:
2381 addrconf_dev_config(dev);
2382 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002383 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 if (idev) {
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09002385 if (run_pending)
2386 addrconf_dad_run(idev);
2387
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 /* If the MTU changed during the interface down, when the
2389 interface up, the changed MTU must be reflected in the
2390 idev as well as routers.
2391 */
2392 if (idev->cnf.mtu6 != dev->mtu && dev->mtu >= IPV6_MIN_MTU) {
2393 rt6_mtu_change(dev, dev->mtu);
2394 idev->cnf.mtu6 = dev->mtu;
2395 }
2396 idev->tstamp = jiffies;
2397 inet6_ifinfo_notify(RTM_NEWLINK, idev);
2398 /* If the changed mtu during down is lower than IPV6_MIN_MTU
2399 stop IPv6 on this interface.
2400 */
2401 if (dev->mtu < IPV6_MIN_MTU)
2402 addrconf_ifdown(dev, event != NETDEV_DOWN);
2403 }
2404 break;
2405
2406 case NETDEV_CHANGEMTU:
Evgeniy Polyakovd31c7b82007-11-30 23:36:08 +11002407 if (idev && dev->mtu >= IPV6_MIN_MTU) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 rt6_mtu_change(dev, dev->mtu);
2409 idev->cnf.mtu6 = dev->mtu;
2410 break;
2411 }
2412
Evgeniy Polyakovd31c7b82007-11-30 23:36:08 +11002413 if (!idev && dev->mtu >= IPV6_MIN_MTU) {
2414 idev = ipv6_add_dev(dev);
2415 if (idev)
2416 break;
2417 }
2418
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 /* MTU falled under IPV6_MIN_MTU. Stop IPv6 on this interface. */
2420
2421 case NETDEV_DOWN:
2422 case NETDEV_UNREGISTER:
2423 /*
2424 * Remove all addresses from this interface.
2425 */
2426 addrconf_ifdown(dev, event != NETDEV_DOWN);
2427 break;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002428
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 case NETDEV_CHANGENAME:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 if (idev) {
Stephen Hemminger5632c512007-04-28 21:16:39 -07002431 snmp6_unregister_dev(idev);
Pavel Emelyanov408c4762008-01-10 17:41:21 -08002432 addrconf_sysctl_unregister(idev);
Pavel Emelyanovf52295a2007-12-02 00:58:37 +11002433 addrconf_sysctl_register(idev);
Herbert Xub217d612007-07-30 17:04:52 -07002434 err = snmp6_register_dev(idev);
2435 if (err)
2436 return notifier_from_errno(err);
Stephen Hemminger5632c512007-04-28 21:16:39 -07002437 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002439 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440
2441 return NOTIFY_OK;
2442}
2443
2444/*
2445 * addrconf module should be notified of a device going up
2446 */
2447static struct notifier_block ipv6_dev_notf = {
2448 .notifier_call = addrconf_notify,
2449 .priority = 0
2450};
2451
2452static int addrconf_ifdown(struct net_device *dev, int how)
2453{
2454 struct inet6_dev *idev;
2455 struct inet6_ifaddr *ifa, **bifa;
2456 int i;
2457
2458 ASSERT_RTNL();
2459
Denis V. Luneveb867572008-04-03 13:31:53 -07002460 if ((dev->flags & IFF_LOOPBACK) && how == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 how = 0;
2462
2463 rt6_ifdown(dev);
2464 neigh_ifdown(&nd_tbl, dev);
2465
2466 idev = __in6_dev_get(dev);
2467 if (idev == NULL)
2468 return -ENODEV;
2469
2470 /* Step 1: remove reference to ipv6 device from parent device.
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002471 Do not dev_put!
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 */
Denis V. Lunev439e2382008-04-03 13:30:17 -07002473 if (how) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 idev->dead = 1;
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07002475
2476 /* protected by rtnl_lock */
2477 rcu_assign_pointer(dev->ip6_ptr, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478
2479 /* Step 1.5: remove snmp6 entry */
2480 snmp6_unregister_dev(idev);
2481
2482 }
2483
2484 /* Step 2: clear hash table */
2485 for (i=0; i<IN6_ADDR_HSIZE; i++) {
2486 bifa = &inet6_addr_lst[i];
2487
2488 write_lock_bh(&addrconf_hash_lock);
2489 while ((ifa = *bifa) != NULL) {
2490 if (ifa->idev == idev) {
2491 *bifa = ifa->lst_next;
2492 ifa->lst_next = NULL;
2493 addrconf_del_timer(ifa);
2494 in6_ifa_put(ifa);
2495 continue;
2496 }
2497 bifa = &ifa->lst_next;
2498 }
2499 write_unlock_bh(&addrconf_hash_lock);
2500 }
2501
2502 write_lock_bh(&idev->lock);
2503
2504 /* Step 3: clear flags for stateless addrconf */
Denis V. Lunev439e2382008-04-03 13:30:17 -07002505 if (!how)
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002506 idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507
2508 /* Step 4: clear address list */
2509#ifdef CONFIG_IPV6_PRIVACY
Denis V. Lunev439e2382008-04-03 13:30:17 -07002510 if (how && del_timer(&idev->regen_timer))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 in6_dev_put(idev);
2512
2513 /* clear tempaddr list */
2514 while ((ifa = idev->tempaddr_list) != NULL) {
2515 idev->tempaddr_list = ifa->tmp_next;
2516 ifa->tmp_next = NULL;
2517 ifa->dead = 1;
2518 write_unlock_bh(&idev->lock);
2519 spin_lock_bh(&ifa->lock);
2520
2521 if (ifa->ifpub) {
2522 in6_ifa_put(ifa->ifpub);
2523 ifa->ifpub = NULL;
2524 }
2525 spin_unlock_bh(&ifa->lock);
2526 in6_ifa_put(ifa);
2527 write_lock_bh(&idev->lock);
2528 }
2529#endif
2530 while ((ifa = idev->addr_list) != NULL) {
2531 idev->addr_list = ifa->if_next;
2532 ifa->if_next = NULL;
2533 ifa->dead = 1;
2534 addrconf_del_timer(ifa);
2535 write_unlock_bh(&idev->lock);
2536
2537 __ipv6_ifa_notify(RTM_DELADDR, ifa);
Vlad Yasevich063ed362007-07-15 00:16:35 -07002538 atomic_notifier_call_chain(&inet6addr_chain, NETDEV_DOWN, ifa);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 in6_ifa_put(ifa);
2540
2541 write_lock_bh(&idev->lock);
2542 }
2543 write_unlock_bh(&idev->lock);
2544
2545 /* Step 5: Discard multicast list */
2546
Denis V. Lunev439e2382008-04-03 13:30:17 -07002547 if (how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 ipv6_mc_destroy_dev(idev);
2549 else
2550 ipv6_mc_down(idev);
2551
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 idev->tstamp = jiffies;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002553
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 /* Shot the device (if unregistered) */
2555
Denis V. Lunev439e2382008-04-03 13:30:17 -07002556 if (how) {
Pavel Emelyanov408c4762008-01-10 17:41:21 -08002557 addrconf_sysctl_unregister(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 neigh_parms_release(&nd_tbl, idev->nd_parms);
2559 neigh_ifdown(&nd_tbl, dev);
2560 in6_dev_put(idev);
2561 }
2562 return 0;
2563}
2564
2565static void addrconf_rs_timer(unsigned long data)
2566{
2567 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
2568
2569 if (ifp->idev->cnf.forwarding)
2570 goto out;
2571
2572 if (ifp->idev->if_flags & IF_RA_RCVD) {
2573 /*
2574 * Announcement received after solicitation
2575 * was sent
2576 */
2577 goto out;
2578 }
2579
2580 spin_lock(&ifp->lock);
2581 if (ifp->probes++ < ifp->idev->cnf.rtr_solicits) {
2582 struct in6_addr all_routers;
2583
2584 /* The wait after the last probe can be shorter */
2585 addrconf_mod_timer(ifp, AC_RS,
2586 (ifp->probes == ifp->idev->cnf.rtr_solicits) ?
2587 ifp->idev->cnf.rtr_solicit_delay :
2588 ifp->idev->cnf.rtr_solicit_interval);
2589 spin_unlock(&ifp->lock);
2590
2591 ipv6_addr_all_routers(&all_routers);
2592
2593 ndisc_send_rs(ifp->idev->dev, &ifp->addr, &all_routers);
2594 } else {
2595 spin_unlock(&ifp->lock);
2596 /*
2597 * Note: we do not support deprecated "all on-link"
2598 * assumption any longer.
2599 */
2600 printk(KERN_DEBUG "%s: no IPv6 routers present\n",
2601 ifp->idev->dev->name);
2602 }
2603
2604out:
2605 in6_ifa_put(ifp);
2606}
2607
2608/*
2609 * Duplicate Address Detection
2610 */
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002611static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
2612{
2613 unsigned long rand_num;
2614 struct inet6_dev *idev = ifp->idev;
2615
Neil Horman95c385b2007-04-25 17:08:10 -07002616 if (ifp->flags & IFA_F_OPTIMISTIC)
2617 rand_num = 0;
2618 else
2619 rand_num = net_random() % (idev->cnf.rtr_solicit_delay ? : 1);
2620
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002621 ifp->probes = idev->cnf.dad_transmits;
2622 addrconf_mod_timer(ifp, AC_DAD, rand_num);
2623}
2624
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -07002625static void addrconf_dad_start(struct inet6_ifaddr *ifp, u32 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626{
2627 struct inet6_dev *idev = ifp->idev;
2628 struct net_device *dev = idev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629
2630 addrconf_join_solict(dev, &ifp->addr);
2631
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 net_srandom(ifp->addr.s6_addr32[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633
2634 read_lock_bh(&idev->lock);
2635 if (ifp->dead)
2636 goto out;
2637 spin_lock_bh(&ifp->lock);
2638
2639 if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07002640 !(ifp->flags&IFA_F_TENTATIVE) ||
2641 ifp->flags & IFA_F_NODAD) {
Neil Horman95c385b2007-04-25 17:08:10 -07002642 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 spin_unlock_bh(&ifp->lock);
2644 read_unlock_bh(&idev->lock);
2645
2646 addrconf_dad_completed(ifp);
2647 return;
2648 }
2649
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08002650 if (!(idev->if_flags & IF_READY)) {
YOSHIFUJI Hideaki3dd3bf82005-12-23 11:23:21 -08002651 spin_unlock_bh(&ifp->lock);
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08002652 read_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002653 /*
2654 * If the defice is not ready:
2655 * - keep it tentative if it is a permanent address.
2656 * - otherwise, kill it.
2657 */
2658 in6_ifa_hold(ifp);
2659 addrconf_dad_stop(ifp);
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08002660 return;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002661 }
Neil Horman95c385b2007-04-25 17:08:10 -07002662
2663 /*
2664 * Optimistic nodes can start receiving
2665 * Frames right away
2666 */
2667 if(ifp->flags & IFA_F_OPTIMISTIC)
2668 ip6_ins_rt(ifp->rt);
2669
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08002670 addrconf_dad_kick(ifp);
2671 spin_unlock_bh(&ifp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672out:
2673 read_unlock_bh(&idev->lock);
2674}
2675
2676static void addrconf_dad_timer(unsigned long data)
2677{
2678 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
2679 struct inet6_dev *idev = ifp->idev;
2680 struct in6_addr unspec;
2681 struct in6_addr mcaddr;
2682
2683 read_lock_bh(&idev->lock);
2684 if (idev->dead) {
2685 read_unlock_bh(&idev->lock);
2686 goto out;
2687 }
2688 spin_lock_bh(&ifp->lock);
2689 if (ifp->probes == 0) {
2690 /*
2691 * DAD was successful
2692 */
2693
Neil Horman95c385b2007-04-25 17:08:10 -07002694 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 spin_unlock_bh(&ifp->lock);
2696 read_unlock_bh(&idev->lock);
2697
2698 addrconf_dad_completed(ifp);
2699
2700 goto out;
2701 }
2702
2703 ifp->probes--;
2704 addrconf_mod_timer(ifp, AC_DAD, ifp->idev->nd_parms->retrans_time);
2705 spin_unlock_bh(&ifp->lock);
2706 read_unlock_bh(&idev->lock);
2707
2708 /* send a neighbour solicitation for our addr */
2709 memset(&unspec, 0, sizeof(unspec));
2710 addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
2711 ndisc_send_ns(ifp->idev->dev, NULL, &ifp->addr, &mcaddr, &unspec);
2712out:
2713 in6_ifa_put(ifp);
2714}
2715
2716static void addrconf_dad_completed(struct inet6_ifaddr *ifp)
2717{
2718 struct net_device * dev = ifp->idev->dev;
2719
2720 /*
2721 * Configure the address for reception. Now it is valid.
2722 */
2723
2724 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2725
2726 /* If added prefix is link local and forwarding is off,
2727 start sending router solicitations.
2728 */
2729
2730 if (ifp->idev->cnf.forwarding == 0 &&
2731 ifp->idev->cnf.rtr_solicits > 0 &&
2732 (dev->flags&IFF_LOOPBACK) == 0 &&
2733 (ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL)) {
2734 struct in6_addr all_routers;
2735
2736 ipv6_addr_all_routers(&all_routers);
2737
2738 /*
2739 * If a host as already performed a random delay
2740 * [...] as part of DAD [...] there is no need
2741 * to delay again before sending the first RS
2742 */
2743 ndisc_send_rs(ifp->idev->dev, &ifp->addr, &all_routers);
2744
2745 spin_lock_bh(&ifp->lock);
2746 ifp->probes = 1;
2747 ifp->idev->if_flags |= IF_RS_SENT;
2748 addrconf_mod_timer(ifp, AC_RS, ifp->idev->cnf.rtr_solicit_interval);
2749 spin_unlock_bh(&ifp->lock);
2750 }
2751}
2752
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09002753static void addrconf_dad_run(struct inet6_dev *idev) {
2754 struct inet6_ifaddr *ifp;
2755
2756 read_lock_bh(&idev->lock);
2757 for (ifp = idev->addr_list; ifp; ifp = ifp->if_next) {
2758 spin_lock_bh(&ifp->lock);
2759 if (!(ifp->flags & IFA_F_TENTATIVE)) {
2760 spin_unlock_bh(&ifp->lock);
2761 continue;
2762 }
2763 spin_unlock_bh(&ifp->lock);
2764 addrconf_dad_kick(ifp);
2765 }
2766 read_unlock_bh(&idev->lock);
2767}
2768
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769#ifdef CONFIG_PROC_FS
2770struct if6_iter_state {
Daniel Lezcano3c400902008-01-10 22:42:49 -08002771 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 int bucket;
2773};
2774
2775static struct inet6_ifaddr *if6_get_first(struct seq_file *seq)
2776{
2777 struct inet6_ifaddr *ifa = NULL;
2778 struct if6_iter_state *state = seq->private;
Daniel Lezcano3c400902008-01-10 22:42:49 -08002779 struct net *net = state->p.net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780
2781 for (state->bucket = 0; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
2782 ifa = inet6_addr_lst[state->bucket];
Daniel Lezcano3c400902008-01-10 22:42:49 -08002783
2784 while (ifa && ifa->idev->dev->nd_net != net)
2785 ifa = ifa->lst_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 if (ifa)
2787 break;
2788 }
2789 return ifa;
2790}
2791
2792static struct inet6_ifaddr *if6_get_next(struct seq_file *seq, struct inet6_ifaddr *ifa)
2793{
2794 struct if6_iter_state *state = seq->private;
Daniel Lezcano3c400902008-01-10 22:42:49 -08002795 struct net *net = state->p.net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796
2797 ifa = ifa->lst_next;
2798try_again:
Daniel Lezcano3c400902008-01-10 22:42:49 -08002799 if (ifa) {
2800 if (ifa->idev->dev->nd_net != net) {
2801 ifa = ifa->lst_next;
2802 goto try_again;
2803 }
2804 }
2805
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 if (!ifa && ++state->bucket < IN6_ADDR_HSIZE) {
2807 ifa = inet6_addr_lst[state->bucket];
2808 goto try_again;
2809 }
Daniel Lezcano3c400902008-01-10 22:42:49 -08002810
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 return ifa;
2812}
2813
2814static struct inet6_ifaddr *if6_get_idx(struct seq_file *seq, loff_t pos)
2815{
2816 struct inet6_ifaddr *ifa = if6_get_first(seq);
2817
2818 if (ifa)
2819 while(pos && (ifa = if6_get_next(seq, ifa)) != NULL)
2820 --pos;
2821 return pos ? NULL : ifa;
2822}
2823
2824static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
Stephen Hemmingerd20b3102008-01-21 00:48:43 -08002825 __acquires(addrconf_hash_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826{
2827 read_lock_bh(&addrconf_hash_lock);
2828 return if6_get_idx(seq, *pos);
2829}
2830
2831static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2832{
2833 struct inet6_ifaddr *ifa;
2834
2835 ifa = if6_get_next(seq, v);
2836 ++*pos;
2837 return ifa;
2838}
2839
2840static void if6_seq_stop(struct seq_file *seq, void *v)
Stephen Hemmingerd20b3102008-01-21 00:48:43 -08002841 __releases(addrconf_hash_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842{
2843 read_unlock_bh(&addrconf_hash_lock);
2844}
2845
2846static int if6_seq_show(struct seq_file *seq, void *v)
2847{
2848 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
2849 seq_printf(seq,
YOSHIFUJI Hideaki9343e792006-01-17 02:10:53 -08002850 NIP6_SEQFMT " %02x %02x %02x %02x %8s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 NIP6(ifp->addr),
2852 ifp->idev->dev->ifindex,
2853 ifp->prefix_len,
2854 ifp->scope,
2855 ifp->flags,
2856 ifp->idev->dev->name);
2857 return 0;
2858}
2859
Philippe De Muyter56b3d972007-07-10 23:07:31 -07002860static const struct seq_operations if6_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 .start = if6_seq_start,
2862 .next = if6_seq_next,
2863 .show = if6_seq_show,
2864 .stop = if6_seq_stop,
2865};
2866
2867static int if6_seq_open(struct inode *inode, struct file *file)
2868{
Daniel Lezcano3c400902008-01-10 22:42:49 -08002869 return seq_open_net(inode, file, &if6_seq_ops,
2870 sizeof(struct if6_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871}
2872
Arjan van de Ven9a321442007-02-12 00:55:35 -08002873static const struct file_operations if6_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 .owner = THIS_MODULE,
2875 .open = if6_seq_open,
2876 .read = seq_read,
2877 .llseek = seq_lseek,
Daniel Lezcano3c400902008-01-10 22:42:49 -08002878 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879};
2880
Daniel Lezcano3c400902008-01-10 22:42:49 -08002881static int if6_proc_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882{
Daniel Lezcano3c400902008-01-10 22:42:49 -08002883 if (!proc_net_fops_create(net, "if_inet6", S_IRUGO, &if6_fops))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 return -ENOMEM;
2885 return 0;
2886}
2887
Daniel Lezcano3c400902008-01-10 22:42:49 -08002888static void if6_proc_net_exit(struct net *net)
2889{
2890 proc_net_remove(net, "if_inet6");
2891}
2892
2893static struct pernet_operations if6_proc_net_ops = {
2894 .init = if6_proc_net_init,
2895 .exit = if6_proc_net_exit,
2896};
2897
2898int __init if6_proc_init(void)
2899{
2900 return register_pernet_subsys(&if6_proc_net_ops);
2901}
2902
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903void if6_proc_exit(void)
2904{
Daniel Lezcano3c400902008-01-10 22:42:49 -08002905 unregister_pernet_subsys(&if6_proc_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906}
2907#endif /* CONFIG_PROC_FS */
2908
Masahide NAKAMURA59fbb3a62007-06-26 23:56:32 -07002909#if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07002910/* Check if address is a home address configured on any interface. */
Daniel Lezcano389f6612008-01-10 22:44:40 -08002911int ipv6_chk_home_addr(struct net *net, struct in6_addr *addr)
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07002912{
2913 int ret = 0;
2914 struct inet6_ifaddr * ifp;
2915 u8 hash = ipv6_addr_hash(addr);
2916 read_lock_bh(&addrconf_hash_lock);
2917 for (ifp = inet6_addr_lst[hash]; ifp; ifp = ifp->lst_next) {
Daniel Lezcano389f6612008-01-10 22:44:40 -08002918 if (ifp->idev->dev->nd_net != net)
2919 continue;
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07002920 if (ipv6_addr_cmp(&ifp->addr, addr) == 0 &&
2921 (ifp->flags & IFA_F_HOMEADDRESS)) {
2922 ret = 1;
2923 break;
2924 }
2925 }
2926 read_unlock_bh(&addrconf_hash_lock);
2927 return ret;
2928}
2929#endif
2930
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931/*
2932 * Periodic address status verification
2933 */
2934
2935static void addrconf_verify(unsigned long foo)
2936{
2937 struct inet6_ifaddr *ifp;
2938 unsigned long now, next;
2939 int i;
2940
2941 spin_lock_bh(&addrconf_verify_lock);
2942 now = jiffies;
2943 next = now + ADDR_CHECK_FREQUENCY;
2944
2945 del_timer(&addr_chk_timer);
2946
2947 for (i=0; i < IN6_ADDR_HSIZE; i++) {
2948
2949restart:
Yan Zheng5d5780d2005-11-20 13:42:20 -08002950 read_lock(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 for (ifp=inet6_addr_lst[i]; ifp; ifp=ifp->lst_next) {
2952 unsigned long age;
2953#ifdef CONFIG_IPV6_PRIVACY
2954 unsigned long regen_advance;
2955#endif
2956
2957 if (ifp->flags & IFA_F_PERMANENT)
2958 continue;
2959
2960 spin_lock(&ifp->lock);
2961 age = (now - ifp->tstamp) / HZ;
2962
2963#ifdef CONFIG_IPV6_PRIVACY
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002964 regen_advance = ifp->idev->cnf.regen_max_retry *
2965 ifp->idev->cnf.dad_transmits *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 ifp->idev->nd_parms->retrans_time / HZ;
2967#endif
2968
YOSHIFUJI Hideaki8f27ebb2006-07-28 18:12:11 +09002969 if (ifp->valid_lft != INFINITY_LIFE_TIME &&
2970 age >= ifp->valid_lft) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 spin_unlock(&ifp->lock);
2972 in6_ifa_hold(ifp);
Yan Zheng5d5780d2005-11-20 13:42:20 -08002973 read_unlock(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 ipv6_del_addr(ifp);
2975 goto restart;
YOSHIFUJI Hideaki8f27ebb2006-07-28 18:12:11 +09002976 } else if (ifp->prefered_lft == INFINITY_LIFE_TIME) {
2977 spin_unlock(&ifp->lock);
2978 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 } else if (age >= ifp->prefered_lft) {
2980 /* jiffies - ifp->tsamp > age >= ifp->prefered_lft */
2981 int deprecate = 0;
2982
2983 if (!(ifp->flags&IFA_F_DEPRECATED)) {
2984 deprecate = 1;
2985 ifp->flags |= IFA_F_DEPRECATED;
2986 }
2987
2988 if (time_before(ifp->tstamp + ifp->valid_lft * HZ, next))
2989 next = ifp->tstamp + ifp->valid_lft * HZ;
2990
2991 spin_unlock(&ifp->lock);
2992
2993 if (deprecate) {
2994 in6_ifa_hold(ifp);
Yan Zheng5d5780d2005-11-20 13:42:20 -08002995 read_unlock(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996
2997 ipv6_ifa_notify(0, ifp);
2998 in6_ifa_put(ifp);
2999 goto restart;
3000 }
3001#ifdef CONFIG_IPV6_PRIVACY
3002 } else if ((ifp->flags&IFA_F_TEMPORARY) &&
3003 !(ifp->flags&IFA_F_TENTATIVE)) {
3004 if (age >= ifp->prefered_lft - regen_advance) {
3005 struct inet6_ifaddr *ifpub = ifp->ifpub;
3006 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
3007 next = ifp->tstamp + ifp->prefered_lft * HZ;
3008 if (!ifp->regen_count && ifpub) {
3009 ifp->regen_count++;
3010 in6_ifa_hold(ifp);
3011 in6_ifa_hold(ifpub);
3012 spin_unlock(&ifp->lock);
Yan Zheng5d5780d2005-11-20 13:42:20 -08003013 read_unlock(&addrconf_hash_lock);
Hiroyuki YAMAMORI291d8092005-12-23 11:24:05 -08003014 spin_lock(&ifpub->lock);
3015 ifpub->regen_count = 0;
3016 spin_unlock(&ifpub->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 ipv6_create_tempaddr(ifpub, ifp);
3018 in6_ifa_put(ifpub);
3019 in6_ifa_put(ifp);
3020 goto restart;
3021 }
3022 } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
3023 next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
3024 spin_unlock(&ifp->lock);
3025#endif
3026 } else {
3027 /* ifp->prefered_lft <= ifp->valid_lft */
3028 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
3029 next = ifp->tstamp + ifp->prefered_lft * HZ;
3030 spin_unlock(&ifp->lock);
3031 }
3032 }
Yan Zheng5d5780d2005-11-20 13:42:20 -08003033 read_unlock(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 }
3035
3036 addr_chk_timer.expires = time_before(next, jiffies + HZ) ? jiffies + HZ : next;
3037 add_timer(&addr_chk_timer);
3038 spin_unlock_bh(&addrconf_verify_lock);
3039}
3040
Thomas Graf461d8832006-09-18 00:09:49 -07003041static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local)
3042{
3043 struct in6_addr *pfx = NULL;
3044
3045 if (addr)
3046 pfx = nla_data(addr);
3047
3048 if (local) {
3049 if (pfx && nla_memcmp(local, pfx, sizeof(*pfx)))
3050 pfx = NULL;
3051 else
3052 pfx = nla_data(local);
3053 }
3054
3055 return pfx;
3056}
3057
Patrick McHardyef7c79e2007-06-05 12:38:30 -07003058static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = {
Thomas Graf461d8832006-09-18 00:09:49 -07003059 [IFA_ADDRESS] = { .len = sizeof(struct in6_addr) },
3060 [IFA_LOCAL] = { .len = sizeof(struct in6_addr) },
3061 [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) },
3062};
3063
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064static int
3065inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
3066{
Denis V. Lunevb8542722007-12-01 00:21:31 +11003067 struct net *net = skb->sk->sk_net;
Thomas Grafb933f712006-09-18 00:10:19 -07003068 struct ifaddrmsg *ifm;
3069 struct nlattr *tb[IFA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 struct in6_addr *pfx;
Thomas Grafb933f712006-09-18 00:10:19 -07003071 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072
Denis V. Lunevb8542722007-12-01 00:21:31 +11003073 if (net != &init_net)
3074 return -EINVAL;
3075
Thomas Grafb933f712006-09-18 00:10:19 -07003076 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
3077 if (err < 0)
3078 return err;
3079
3080 ifm = nlmsg_data(nlh);
3081 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 if (pfx == NULL)
3083 return -EINVAL;
3084
3085 return inet6_addr_del(ifm->ifa_index, pfx, ifm->ifa_prefixlen);
3086}
3087
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003088static int inet6_addr_modify(struct inet6_ifaddr *ifp, u8 ifa_flags,
3089 u32 prefered_lft, u32 valid_lft)
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09003090{
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09003091 u32 flags = RTF_EXPIRES;
3092
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09003093 if (!valid_lft || (prefered_lft > valid_lft))
3094 return -EINVAL;
3095
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09003096 if (valid_lft == INFINITY_LIFE_TIME) {
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003097 ifa_flags |= IFA_F_PERMANENT;
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09003098 flags = 0;
3099 } else if (valid_lft >= 0x7FFFFFFF/HZ)
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09003100 valid_lft = 0x7FFFFFFF/HZ;
3101
3102 if (prefered_lft == 0)
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003103 ifa_flags |= IFA_F_DEPRECATED;
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09003104 else if ((prefered_lft >= 0x7FFFFFFF/HZ) &&
3105 (prefered_lft != INFINITY_LIFE_TIME))
3106 prefered_lft = 0x7FFFFFFF/HZ;
3107
3108 spin_lock_bh(&ifp->lock);
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07003109 ifp->flags = (ifp->flags & ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD | IFA_F_HOMEADDRESS)) | ifa_flags;
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09003110 ifp->tstamp = jiffies;
3111 ifp->valid_lft = valid_lft;
3112 ifp->prefered_lft = prefered_lft;
3113
3114 spin_unlock_bh(&ifp->lock);
3115 if (!(ifp->flags&IFA_F_TENTATIVE))
3116 ipv6_ifa_notify(0, ifp);
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09003117
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09003118 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, ifp->idev->dev,
3119 jiffies_to_clock_t(valid_lft * HZ), flags);
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09003120 addrconf_verify(0);
3121
3122 return 0;
3123}
3124
3125static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
3127{
Denis V. Lunevb8542722007-12-01 00:21:31 +11003128 struct net *net = skb->sk->sk_net;
Thomas Graf461d8832006-09-18 00:09:49 -07003129 struct ifaddrmsg *ifm;
3130 struct nlattr *tb[IFA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 struct in6_addr *pfx;
Thomas Graf7198f8c2006-09-18 00:13:46 -07003132 struct inet6_ifaddr *ifa;
3133 struct net_device *dev;
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003134 u32 valid_lft = INFINITY_LIFE_TIME, preferred_lft = INFINITY_LIFE_TIME;
3135 u8 ifa_flags;
Thomas Graf461d8832006-09-18 00:09:49 -07003136 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137
Denis V. Lunevb8542722007-12-01 00:21:31 +11003138 if (net != &init_net)
3139 return -EINVAL;
3140
Thomas Graf461d8832006-09-18 00:09:49 -07003141 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
3142 if (err < 0)
3143 return err;
3144
3145 ifm = nlmsg_data(nlh);
3146 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 if (pfx == NULL)
3148 return -EINVAL;
3149
Thomas Graf461d8832006-09-18 00:09:49 -07003150 if (tb[IFA_CACHEINFO]) {
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09003151 struct ifa_cacheinfo *ci;
Thomas Graf461d8832006-09-18 00:09:49 -07003152
3153 ci = nla_data(tb[IFA_CACHEINFO]);
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09003154 valid_lft = ci->ifa_valid;
Thomas Graf461d8832006-09-18 00:09:49 -07003155 preferred_lft = ci->ifa_prefered;
3156 } else {
3157 preferred_lft = INFINITY_LIFE_TIME;
3158 valid_lft = INFINITY_LIFE_TIME;
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09003159 }
3160
Eric W. Biederman881d9662007-09-17 11:56:21 -07003161 dev = __dev_get_by_index(&init_net, ifm->ifa_index);
Thomas Graf7198f8c2006-09-18 00:13:46 -07003162 if (dev == NULL)
3163 return -ENODEV;
3164
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003165 /* We ignore other flags so far. */
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07003166 ifa_flags = ifm->ifa_flags & (IFA_F_NODAD | IFA_F_HOMEADDRESS);
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003167
Daniel Lezcano1cab3da2008-01-10 22:44:09 -08003168 ifa = ipv6_get_ifaddr(net, pfx, dev, 1);
Thomas Graf7198f8c2006-09-18 00:13:46 -07003169 if (ifa == NULL) {
3170 /*
3171 * It would be best to check for !NLM_F_CREATE here but
3172 * userspace alreay relies on not having to provide this.
3173 */
3174 return inet6_addr_add(ifm->ifa_index, pfx, ifm->ifa_prefixlen,
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003175 ifa_flags, preferred_lft, valid_lft);
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09003176 }
3177
Thomas Graf7198f8c2006-09-18 00:13:46 -07003178 if (nlh->nlmsg_flags & NLM_F_EXCL ||
3179 !(nlh->nlmsg_flags & NLM_F_REPLACE))
3180 err = -EEXIST;
3181 else
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003182 err = inet6_addr_modify(ifa, ifa_flags, preferred_lft, valid_lft);
Thomas Graf7198f8c2006-09-18 00:13:46 -07003183
3184 in6_ifa_put(ifa);
3185
3186 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187}
3188
Thomas Graf101bb222006-09-18 00:11:52 -07003189static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u8 flags,
3190 u8 scope, int ifindex)
3191{
3192 struct ifaddrmsg *ifm;
3193
3194 ifm = nlmsg_data(nlh);
3195 ifm->ifa_family = AF_INET6;
3196 ifm->ifa_prefixlen = prefixlen;
3197 ifm->ifa_flags = flags;
3198 ifm->ifa_scope = scope;
3199 ifm->ifa_index = ifindex;
3200}
3201
Thomas Graf85486af2006-09-18 00:11:24 -07003202static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
3203 unsigned long tstamp, u32 preferred, u32 valid)
3204{
3205 struct ifa_cacheinfo ci;
3206
3207 ci.cstamp = (u32)(TIME_DELTA(cstamp, INITIAL_JIFFIES) / HZ * 100
3208 + TIME_DELTA(cstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
3209 ci.tstamp = (u32)(TIME_DELTA(tstamp, INITIAL_JIFFIES) / HZ * 100
3210 + TIME_DELTA(tstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
3211 ci.ifa_prefered = preferred;
3212 ci.ifa_valid = valid;
3213
3214 return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
3215}
3216
Thomas Graf101bb222006-09-18 00:11:52 -07003217static inline int rt_scope(int ifa_scope)
3218{
3219 if (ifa_scope & IFA_HOST)
3220 return RT_SCOPE_HOST;
3221 else if (ifa_scope & IFA_LINK)
3222 return RT_SCOPE_LINK;
3223 else if (ifa_scope & IFA_SITE)
3224 return RT_SCOPE_SITE;
3225 else
3226 return RT_SCOPE_UNIVERSE;
3227}
3228
Thomas Graf0ab68032006-09-18 00:12:35 -07003229static inline int inet6_ifaddr_msgsize(void)
3230{
Thomas Graf339bf982006-11-10 14:10:15 -08003231 return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
3232 + nla_total_size(16) /* IFA_ADDRESS */
3233 + nla_total_size(sizeof(struct ifa_cacheinfo));
Thomas Graf0ab68032006-09-18 00:12:35 -07003234}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07003235
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07003237 u32 pid, u32 seq, int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239 struct nlmsghdr *nlh;
Thomas Graf85486af2006-09-18 00:11:24 -07003240 u32 preferred, valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241
Thomas Graf0ab68032006-09-18 00:12:35 -07003242 nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags);
3243 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08003244 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07003245
Thomas Graf101bb222006-09-18 00:11:52 -07003246 put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope),
3247 ifa->idev->dev->ifindex);
3248
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249 if (!(ifa->flags&IFA_F_PERMANENT)) {
Thomas Graf85486af2006-09-18 00:11:24 -07003250 preferred = ifa->prefered_lft;
3251 valid = ifa->valid_lft;
3252 if (preferred != INFINITY_LIFE_TIME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 long tval = (jiffies - ifa->tstamp)/HZ;
Thomas Graf85486af2006-09-18 00:11:24 -07003254 preferred -= tval;
3255 if (valid != INFINITY_LIFE_TIME)
3256 valid -= tval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 }
3258 } else {
Thomas Graf85486af2006-09-18 00:11:24 -07003259 preferred = INFINITY_LIFE_TIME;
3260 valid = INFINITY_LIFE_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 }
Thomas Graf85486af2006-09-18 00:11:24 -07003262
Thomas Graf0ab68032006-09-18 00:12:35 -07003263 if (nla_put(skb, IFA_ADDRESS, 16, &ifa->addr) < 0 ||
Patrick McHardy26932562007-01-31 23:16:40 -08003264 put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0) {
3265 nlmsg_cancel(skb, nlh);
3266 return -EMSGSIZE;
3267 }
Thomas Graf85486af2006-09-18 00:11:24 -07003268
Thomas Graf0ab68032006-09-18 00:12:35 -07003269 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270}
3271
3272static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -07003273 u32 pid, u32 seq, int event, u16 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 struct nlmsghdr *nlh;
Thomas Graf101bb222006-09-18 00:11:52 -07003276 u8 scope = RT_SCOPE_UNIVERSE;
3277 int ifindex = ifmca->idev->dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278
Thomas Graf101bb222006-09-18 00:11:52 -07003279 if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE)
3280 scope = RT_SCOPE_SITE;
3281
Thomas Graf0ab68032006-09-18 00:12:35 -07003282 nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags);
3283 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08003284 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07003285
Thomas Graf101bb222006-09-18 00:11:52 -07003286 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
Thomas Graf0ab68032006-09-18 00:12:35 -07003287 if (nla_put(skb, IFA_MULTICAST, 16, &ifmca->mca_addr) < 0 ||
3288 put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp,
Patrick McHardy26932562007-01-31 23:16:40 -08003289 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
3290 nlmsg_cancel(skb, nlh);
3291 return -EMSGSIZE;
3292 }
Thomas Graf85486af2006-09-18 00:11:24 -07003293
Thomas Graf0ab68032006-09-18 00:12:35 -07003294 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295}
3296
3297static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07003298 u32 pid, u32 seq, int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 struct nlmsghdr *nlh;
Thomas Graf101bb222006-09-18 00:11:52 -07003301 u8 scope = RT_SCOPE_UNIVERSE;
3302 int ifindex = ifaca->aca_idev->dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303
Thomas Graf101bb222006-09-18 00:11:52 -07003304 if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE)
3305 scope = RT_SCOPE_SITE;
3306
Thomas Graf0ab68032006-09-18 00:12:35 -07003307 nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags);
3308 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08003309 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07003310
Thomas Graf101bb222006-09-18 00:11:52 -07003311 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
Thomas Graf0ab68032006-09-18 00:12:35 -07003312 if (nla_put(skb, IFA_ANYCAST, 16, &ifaca->aca_addr) < 0 ||
3313 put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp,
Patrick McHardy26932562007-01-31 23:16:40 -08003314 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
3315 nlmsg_cancel(skb, nlh);
3316 return -EMSGSIZE;
3317 }
Thomas Graf85486af2006-09-18 00:11:24 -07003318
Thomas Graf0ab68032006-09-18 00:12:35 -07003319 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320}
3321
3322enum addr_type_t
3323{
3324 UNICAST_ADDR,
3325 MULTICAST_ADDR,
3326 ANYCAST_ADDR,
3327};
3328
3329static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
3330 enum addr_type_t type)
3331{
3332 int idx, ip_idx;
3333 int s_idx, s_ip_idx;
3334 int err = 1;
3335 struct net_device *dev;
3336 struct inet6_dev *idev = NULL;
3337 struct inet6_ifaddr *ifa;
3338 struct ifmcaddr6 *ifmca;
3339 struct ifacaddr6 *ifaca;
3340
3341 s_idx = cb->args[0];
3342 s_ip_idx = ip_idx = cb->args[1];
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003343
Pavel Emelianov7562f872007-05-03 15:13:45 -07003344 idx = 0;
Eric W. Biederman881d9662007-09-17 11:56:21 -07003345 for_each_netdev(&init_net, dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346 if (idx < s_idx)
Pavel Emelianov7562f872007-05-03 15:13:45 -07003347 goto cont;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348 if (idx > s_idx)
3349 s_ip_idx = 0;
3350 ip_idx = 0;
3351 if ((idev = in6_dev_get(dev)) == NULL)
Pavel Emelianov7562f872007-05-03 15:13:45 -07003352 goto cont;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353 read_lock_bh(&idev->lock);
3354 switch (type) {
3355 case UNICAST_ADDR:
YOSHIFUJI Hideakiae9cda52005-06-28 13:00:30 -07003356 /* unicast address incl. temp addr */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 for (ifa = idev->addr_list; ifa;
3358 ifa = ifa->if_next, ip_idx++) {
3359 if (ip_idx < s_ip_idx)
3360 continue;
YOSHIFUJI Hideaki5d5619b2008-01-22 17:29:40 +09003361 err = inet6_fill_ifaddr(skb, ifa,
3362 NETLINK_CB(cb->skb).pid,
3363 cb->nlh->nlmsg_seq,
3364 RTM_NEWADDR,
3365 NLM_F_MULTI);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367 break;
3368 case MULTICAST_ADDR:
3369 /* multicast address */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003370 for (ifmca = idev->mc_list; ifmca;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371 ifmca = ifmca->next, ip_idx++) {
3372 if (ip_idx < s_ip_idx)
3373 continue;
YOSHIFUJI Hideaki5d5619b2008-01-22 17:29:40 +09003374 err = inet6_fill_ifmcaddr(skb, ifmca,
3375 NETLINK_CB(cb->skb).pid,
3376 cb->nlh->nlmsg_seq,
3377 RTM_GETMULTICAST,
3378 NLM_F_MULTI);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 }
3380 break;
3381 case ANYCAST_ADDR:
3382 /* anycast address */
3383 for (ifaca = idev->ac_list; ifaca;
3384 ifaca = ifaca->aca_next, ip_idx++) {
3385 if (ip_idx < s_ip_idx)
3386 continue;
YOSHIFUJI Hideaki5d5619b2008-01-22 17:29:40 +09003387 err = inet6_fill_ifacaddr(skb, ifaca,
3388 NETLINK_CB(cb->skb).pid,
3389 cb->nlh->nlmsg_seq,
3390 RTM_GETANYCAST,
3391 NLM_F_MULTI);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392 }
3393 break;
3394 default:
3395 break;
3396 }
3397 read_unlock_bh(&idev->lock);
3398 in6_dev_put(idev);
YOSHIFUJI Hideaki5d5619b2008-01-22 17:29:40 +09003399
3400 if (err <= 0)
3401 break;
Pavel Emelianov7562f872007-05-03 15:13:45 -07003402cont:
3403 idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405 cb->args[0] = idx;
3406 cb->args[1] = ip_idx;
3407 return skb->len;
3408}
3409
3410static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
3411{
Denis V. Lunevb8542722007-12-01 00:21:31 +11003412 struct net *net = skb->sk->sk_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413 enum addr_type_t type = UNICAST_ADDR;
Denis V. Lunevb8542722007-12-01 00:21:31 +11003414
3415 if (net != &init_net)
3416 return 0;
3417
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418 return inet6_dump_addr(skb, cb, type);
3419}
3420
3421static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
3422{
Denis V. Lunevb8542722007-12-01 00:21:31 +11003423 struct net *net = skb->sk->sk_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424 enum addr_type_t type = MULTICAST_ADDR;
Denis V. Lunevb8542722007-12-01 00:21:31 +11003425
3426 if (net != &init_net)
3427 return 0;
3428
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429 return inet6_dump_addr(skb, cb, type);
3430}
3431
3432
3433static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
3434{
Denis V. Lunevb8542722007-12-01 00:21:31 +11003435 struct net *net = skb->sk->sk_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436 enum addr_type_t type = ANYCAST_ADDR;
Denis V. Lunevb8542722007-12-01 00:21:31 +11003437
3438 if (net != &init_net)
3439 return 0;
3440
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441 return inet6_dump_addr(skb, cb, type);
3442}
3443
Thomas Graf1b29fc22006-09-18 00:10:50 -07003444static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr* nlh,
3445 void *arg)
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003446{
Denis V. Lunevb8542722007-12-01 00:21:31 +11003447 struct net *net = in_skb->sk->sk_net;
Thomas Graf1b29fc22006-09-18 00:10:50 -07003448 struct ifaddrmsg *ifm;
3449 struct nlattr *tb[IFA_MAX+1];
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003450 struct in6_addr *addr = NULL;
3451 struct net_device *dev = NULL;
3452 struct inet6_ifaddr *ifa;
3453 struct sk_buff *skb;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003454 int err;
3455
Denis V. Lunevb8542722007-12-01 00:21:31 +11003456 if (net != &init_net)
3457 return -EINVAL;
3458
Thomas Graf1b29fc22006-09-18 00:10:50 -07003459 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
3460 if (err < 0)
3461 goto errout;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003462
Thomas Graf1b29fc22006-09-18 00:10:50 -07003463 addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
3464 if (addr == NULL) {
3465 err = -EINVAL;
3466 goto errout;
3467 }
3468
3469 ifm = nlmsg_data(nlh);
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003470 if (ifm->ifa_index)
Eric W. Biederman881d9662007-09-17 11:56:21 -07003471 dev = __dev_get_by_index(&init_net, ifm->ifa_index);
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003472
Daniel Lezcano1cab3da2008-01-10 22:44:09 -08003473 if ((ifa = ipv6_get_ifaddr(net, addr, dev, 1)) == NULL) {
Thomas Graf1b29fc22006-09-18 00:10:50 -07003474 err = -EADDRNOTAVAIL;
3475 goto errout;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003476 }
3477
Thomas Graf0ab68032006-09-18 00:12:35 -07003478 if ((skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL)) == NULL) {
Thomas Graf1b29fc22006-09-18 00:10:50 -07003479 err = -ENOBUFS;
3480 goto errout_ifa;
3481 }
3482
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003483 err = inet6_fill_ifaddr(skb, ifa, NETLINK_CB(in_skb).pid,
3484 nlh->nlmsg_seq, RTM_NEWADDR, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08003485 if (err < 0) {
3486 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
3487 WARN_ON(err == -EMSGSIZE);
3488 kfree_skb(skb);
3489 goto errout_ifa;
3490 }
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003491 err = rtnl_unicast(skb, &init_net, NETLINK_CB(in_skb).pid);
Thomas Graf1b29fc22006-09-18 00:10:50 -07003492errout_ifa:
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003493 in6_ifa_put(ifa);
Thomas Graf1b29fc22006-09-18 00:10:50 -07003494errout:
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003495 return err;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003496}
3497
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
3499{
3500 struct sk_buff *skb;
Thomas Graf5d620262006-08-15 00:35:02 -07003501 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502
Thomas Graf0ab68032006-09-18 00:12:35 -07003503 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC);
Thomas Graf5d620262006-08-15 00:35:02 -07003504 if (skb == NULL)
3505 goto errout;
3506
3507 err = inet6_fill_ifaddr(skb, ifa, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08003508 if (err < 0) {
3509 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
3510 WARN_ON(err == -EMSGSIZE);
3511 kfree_skb(skb);
3512 goto errout;
3513 }
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003514 err = rtnl_notify(skb, &init_net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
Thomas Graf5d620262006-08-15 00:35:02 -07003515errout:
3516 if (err < 0)
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003517 rtnl_set_sk_err(&init_net, RTNLGRP_IPV6_IFADDR, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518}
3519
Dave Jonesb6f99a22007-03-22 12:27:49 -07003520static inline void ipv6_store_devconf(struct ipv6_devconf *cnf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521 __s32 *array, int bytes)
3522{
Thomas Graf04561c12006-11-14 19:53:58 -08003523 BUG_ON(bytes < (DEVCONF_MAX * 4));
3524
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525 memset(array, 0, bytes);
3526 array[DEVCONF_FORWARDING] = cnf->forwarding;
3527 array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
3528 array[DEVCONF_MTU6] = cnf->mtu6;
3529 array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
3530 array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
3531 array[DEVCONF_AUTOCONF] = cnf->autoconf;
3532 array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
3533 array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
3534 array[DEVCONF_RTR_SOLICIT_INTERVAL] = cnf->rtr_solicit_interval;
3535 array[DEVCONF_RTR_SOLICIT_DELAY] = cnf->rtr_solicit_delay;
3536 array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
3537#ifdef CONFIG_IPV6_PRIVACY
3538 array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
3539 array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
3540 array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
3541 array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
3542 array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
3543#endif
3544 array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -08003545 array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr;
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -08003546 array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo;
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08003547#ifdef CONFIG_IPV6_ROUTER_PREF
3548 array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref;
YOSHIFUJI Hideaki52e16352006-03-20 17:05:47 -08003549 array[DEVCONF_RTR_PROBE_INTERVAL] = cnf->rtr_probe_interval;
Neil Hormanfa03ef32007-01-30 14:30:10 -08003550#ifdef CONFIG_IPV6_ROUTE_INFO
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -08003551 array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen;
3552#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08003553#endif
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -07003554 array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp;
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -07003555 array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route;
Neil Horman95c385b2007-04-25 17:08:10 -07003556#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
3557 array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad;
3558#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559}
3560
Thomas Graf339bf982006-11-10 14:10:15 -08003561static inline size_t inet6_if_nlmsg_size(void)
3562{
3563 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
3564 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
3565 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
3566 + nla_total_size(4) /* IFLA_MTU */
3567 + nla_total_size(4) /* IFLA_LINK */
3568 + nla_total_size( /* IFLA_PROTINFO */
3569 nla_total_size(4) /* IFLA_INET6_FLAGS */
3570 + nla_total_size(sizeof(struct ifla_cacheinfo))
3571 + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07003572 + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */
3573 + nla_total_size(ICMP6_MIB_MAX * 8) /* IFLA_INET6_ICMP6STATS */
Thomas Graf339bf982006-11-10 14:10:15 -08003574 );
3575}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07003576
Herbert Xu7f7d9a62007-04-24 21:54:09 -07003577static inline void __snmp6_fill_stats(u64 *stats, void **mib, int items,
3578 int bytes)
3579{
3580 int i;
3581 int pad = bytes - sizeof(u64) * items;
3582 BUG_ON(pad < 0);
3583
3584 /* Use put_unaligned() because stats may not be aligned for u64. */
3585 put_unaligned(items, &stats[0]);
3586 for (i = 1; i < items; i++)
3587 put_unaligned(snmp_fold_field(mib, i), &stats[i]);
3588
3589 memset(&stats[items], 0, pad);
3590}
3591
3592static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype,
3593 int bytes)
3594{
3595 switch(attrtype) {
3596 case IFLA_INET6_STATS:
3597 __snmp6_fill_stats(stats, (void **)idev->stats.ipv6, IPSTATS_MIB_MAX, bytes);
3598 break;
3599 case IFLA_INET6_ICMP6STATS:
3600 __snmp6_fill_stats(stats, (void **)idev->stats.icmpv6, ICMP6_MIB_MAX, bytes);
3601 break;
3602 }
3603}
3604
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003605static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07003606 u32 pid, u32 seq, int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607{
Thomas Graf04561c12006-11-14 19:53:58 -08003608 struct net_device *dev = idev->dev;
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07003609 struct nlattr *nla;
Thomas Graf04561c12006-11-14 19:53:58 -08003610 struct ifinfomsg *hdr;
3611 struct nlmsghdr *nlh;
3612 void *protoinfo;
3613 struct ifla_cacheinfo ci;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614
Thomas Graf04561c12006-11-14 19:53:58 -08003615 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*hdr), flags);
3616 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08003617 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618
Thomas Graf04561c12006-11-14 19:53:58 -08003619 hdr = nlmsg_data(nlh);
3620 hdr->ifi_family = AF_INET6;
3621 hdr->__ifi_pad = 0;
3622 hdr->ifi_type = dev->type;
3623 hdr->ifi_index = dev->ifindex;
3624 hdr->ifi_flags = dev_get_flags(dev);
3625 hdr->ifi_change = 0;
3626
3627 NLA_PUT_STRING(skb, IFLA_IFNAME, dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628
3629 if (dev->addr_len)
Thomas Graf04561c12006-11-14 19:53:58 -08003630 NLA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631
Thomas Graf04561c12006-11-14 19:53:58 -08003632 NLA_PUT_U32(skb, IFLA_MTU, dev->mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633 if (dev->ifindex != dev->iflink)
Thomas Graf04561c12006-11-14 19:53:58 -08003634 NLA_PUT_U32(skb, IFLA_LINK, dev->iflink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635
Thomas Graf04561c12006-11-14 19:53:58 -08003636 protoinfo = nla_nest_start(skb, IFLA_PROTINFO);
3637 if (protoinfo == NULL)
3638 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639
Thomas Graf04561c12006-11-14 19:53:58 -08003640 NLA_PUT_U32(skb, IFLA_INET6_FLAGS, idev->if_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642 ci.max_reasm_len = IPV6_MAXPLEN;
3643 ci.tstamp = (__u32)(TIME_DELTA(idev->tstamp, INITIAL_JIFFIES) / HZ * 100
3644 + TIME_DELTA(idev->tstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
3645 ci.reachable_time = idev->nd_parms->reachable_time;
3646 ci.retrans_time = idev->nd_parms->retrans_time;
Thomas Graf04561c12006-11-14 19:53:58 -08003647 NLA_PUT(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci);
3648
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07003649 nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32));
3650 if (nla == NULL)
Thomas Graf04561c12006-11-14 19:53:58 -08003651 goto nla_put_failure;
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07003652 ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07003654 /* XXX - MC not implemented */
3655
3656 nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64));
3657 if (nla == NULL)
3658 goto nla_put_failure;
3659 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla));
3660
3661 nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64));
3662 if (nla == NULL)
3663 goto nla_put_failure;
3664 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665
Thomas Graf04561c12006-11-14 19:53:58 -08003666 nla_nest_end(skb, protoinfo);
3667 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668
Thomas Graf04561c12006-11-14 19:53:58 -08003669nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08003670 nlmsg_cancel(skb, nlh);
3671 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672}
3673
3674static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
3675{
Denis V. Lunevb8542722007-12-01 00:21:31 +11003676 struct net *net = skb->sk->sk_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677 int idx, err;
3678 int s_idx = cb->args[0];
3679 struct net_device *dev;
3680 struct inet6_dev *idev;
3681
Denis V. Lunevb8542722007-12-01 00:21:31 +11003682 if (net != &init_net)
3683 return 0;
3684
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685 read_lock(&dev_base_lock);
Pavel Emelianov7562f872007-05-03 15:13:45 -07003686 idx = 0;
Eric W. Biederman881d9662007-09-17 11:56:21 -07003687 for_each_netdev(&init_net, dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688 if (idx < s_idx)
Pavel Emelianov7562f872007-05-03 15:13:45 -07003689 goto cont;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690 if ((idev = in6_dev_get(dev)) == NULL)
Pavel Emelianov7562f872007-05-03 15:13:45 -07003691 goto cont;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003692 err = inet6_fill_ifinfo(skb, idev, NETLINK_CB(cb->skb).pid,
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07003693 cb->nlh->nlmsg_seq, RTM_NEWLINK, NLM_F_MULTI);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694 in6_dev_put(idev);
3695 if (err <= 0)
3696 break;
Pavel Emelianov7562f872007-05-03 15:13:45 -07003697cont:
3698 idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699 }
3700 read_unlock(&dev_base_lock);
3701 cb->args[0] = idx;
3702
3703 return skb->len;
3704}
3705
3706void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
3707{
3708 struct sk_buff *skb;
Thomas Graf8d7a76c2006-08-15 00:35:47 -07003709 int err = -ENOBUFS;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003710
Thomas Graf339bf982006-11-10 14:10:15 -08003711 skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC);
Thomas Graf8d7a76c2006-08-15 00:35:47 -07003712 if (skb == NULL)
3713 goto errout;
3714
3715 err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08003716 if (err < 0) {
3717 /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */
3718 WARN_ON(err == -EMSGSIZE);
3719 kfree_skb(skb);
3720 goto errout;
3721 }
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003722 err = rtnl_notify(skb, &init_net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
Thomas Graf8d7a76c2006-08-15 00:35:47 -07003723errout:
3724 if (err < 0)
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003725 rtnl_set_sk_err(&init_net, RTNLGRP_IPV6_IFADDR, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726}
3727
Thomas Graf339bf982006-11-10 14:10:15 -08003728static inline size_t inet6_prefix_nlmsg_size(void)
3729{
3730 return NLMSG_ALIGN(sizeof(struct prefixmsg))
3731 + nla_total_size(sizeof(struct in6_addr))
3732 + nla_total_size(sizeof(struct prefix_cacheinfo));
3733}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07003734
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
Thomas Graf6051e2f2006-11-14 19:54:19 -08003736 struct prefix_info *pinfo, u32 pid, u32 seq,
3737 int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738{
Thomas Graf6051e2f2006-11-14 19:54:19 -08003739 struct prefixmsg *pmsg;
3740 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741 struct prefix_cacheinfo ci;
3742
Thomas Graf6051e2f2006-11-14 19:54:19 -08003743 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*pmsg), flags);
3744 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08003745 return -EMSGSIZE;
Thomas Graf6051e2f2006-11-14 19:54:19 -08003746
3747 pmsg = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748 pmsg->prefix_family = AF_INET6;
Patrick McHardy8a470772005-06-28 12:56:45 -07003749 pmsg->prefix_pad1 = 0;
3750 pmsg->prefix_pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751 pmsg->prefix_ifindex = idev->dev->ifindex;
3752 pmsg->prefix_len = pinfo->prefix_len;
3753 pmsg->prefix_type = pinfo->type;
Patrick McHardy8a470772005-06-28 12:56:45 -07003754 pmsg->prefix_pad3 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755 pmsg->prefix_flags = 0;
3756 if (pinfo->onlink)
3757 pmsg->prefix_flags |= IF_PREFIX_ONLINK;
3758 if (pinfo->autoconf)
3759 pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
3760
Thomas Graf6051e2f2006-11-14 19:54:19 -08003761 NLA_PUT(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762
3763 ci.preferred_time = ntohl(pinfo->prefered);
3764 ci.valid_time = ntohl(pinfo->valid);
Thomas Graf6051e2f2006-11-14 19:54:19 -08003765 NLA_PUT(skb, PREFIX_CACHEINFO, sizeof(ci), &ci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766
Thomas Graf6051e2f2006-11-14 19:54:19 -08003767 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768
Thomas Graf6051e2f2006-11-14 19:54:19 -08003769nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08003770 nlmsg_cancel(skb, nlh);
3771 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772}
3773
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003774static void inet6_prefix_notify(int event, struct inet6_dev *idev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775 struct prefix_info *pinfo)
3776{
3777 struct sk_buff *skb;
Thomas Graf8c384bfa2006-08-15 00:36:07 -07003778 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779
Thomas Graf339bf982006-11-10 14:10:15 -08003780 skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC);
Thomas Graf8c384bfa2006-08-15 00:36:07 -07003781 if (skb == NULL)
3782 goto errout;
3783
3784 err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08003785 if (err < 0) {
3786 /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */
3787 WARN_ON(err == -EMSGSIZE);
3788 kfree_skb(skb);
3789 goto errout;
3790 }
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003791 err = rtnl_notify(skb, &init_net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC);
Thomas Graf8c384bfa2006-08-15 00:36:07 -07003792errout:
3793 if (err < 0)
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003794 rtnl_set_sk_err(&init_net, RTNLGRP_IPV6_PREFIX, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795}
3796
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
3798{
3799 inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
3800
3801 switch (event) {
3802 case RTM_NEWADDR:
Neil Horman95c385b2007-04-25 17:08:10 -07003803 /*
3804 * If the address was optimistic
3805 * we inserted the route at the start of
3806 * our DAD process, so we don't need
3807 * to do it again
3808 */
3809 if (!(ifp->rt->rt6i_node))
3810 ip6_ins_rt(ifp->rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811 if (ifp->idev->cnf.forwarding)
3812 addrconf_join_anycast(ifp);
3813 break;
3814 case RTM_DELADDR:
3815 if (ifp->idev->cnf.forwarding)
3816 addrconf_leave_anycast(ifp);
3817 addrconf_leave_solict(ifp->idev, &ifp->addr);
3818 dst_hold(&ifp->rt->u.dst);
Thomas Grafe0a1ad732006-08-22 00:00:21 -07003819 if (ip6_del_rt(ifp->rt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820 dst_free(&ifp->rt->u.dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821 break;
3822 }
3823}
3824
3825static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
3826{
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07003827 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828 if (likely(ifp->idev->dead == 0))
3829 __ipv6_ifa_notify(event, ifp);
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07003830 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831}
3832
3833#ifdef CONFIG_SYSCTL
3834
3835static
3836int addrconf_sysctl_forward(ctl_table *ctl, int write, struct file * filp,
3837 void __user *buffer, size_t *lenp, loff_t *ppos)
3838{
3839 int *valp = ctl->data;
3840 int val = *valp;
3841 int ret;
3842
3843 ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
3844
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -08003845 if (write)
3846 addrconf_fixup_forwarding(ctl, valp, val);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003847 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848}
3849
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003850static int addrconf_sysctl_forward_strategy(ctl_table *table,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851 int __user *name, int nlen,
3852 void __user *oldval,
3853 size_t __user *oldlenp,
Alexey Dobriyan1f29bcd2006-12-10 02:19:10 -08003854 void __user *newval, size_t newlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855{
3856 int *valp = table->data;
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -08003857 int val = *valp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858 int new;
3859
3860 if (!newval || !newlen)
3861 return 0;
3862 if (newlen != sizeof(int))
3863 return -EINVAL;
3864 if (get_user(new, (int __user *)newval))
3865 return -EFAULT;
3866 if (new == *valp)
3867 return 0;
3868 if (oldval && oldlenp) {
3869 size_t len;
3870 if (get_user(len, oldlenp))
3871 return -EFAULT;
3872 if (len) {
3873 if (len > table->maxlen)
3874 len = table->maxlen;
3875 if (copy_to_user(oldval, valp, len))
3876 return -EFAULT;
3877 if (put_user(len, oldlenp))
3878 return -EFAULT;
3879 }
3880 }
3881
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -08003882 *valp = new;
3883 addrconf_fixup_forwarding(table, valp, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884 return 1;
3885}
3886
3887static struct addrconf_sysctl_table
3888{
3889 struct ctl_table_header *sysctl_header;
3890 ctl_table addrconf_vars[__NET_IPV6_MAX];
Pavel Emelyanov1dab6222007-12-02 00:59:38 +11003891 char *dev_name;
Brian Haleyab32ea52006-09-22 14:15:41 -07003892} addrconf_sysctl __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893 .sysctl_header = NULL,
3894 .addrconf_vars = {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003895 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896 .ctl_name = NET_IPV6_FORWARDING,
3897 .procname = "forwarding",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003898 .data = &ipv6_devconf.forwarding,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899 .maxlen = sizeof(int),
3900 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003901 .proc_handler = &addrconf_sysctl_forward,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902 .strategy = &addrconf_sysctl_forward_strategy,
3903 },
3904 {
3905 .ctl_name = NET_IPV6_HOP_LIMIT,
3906 .procname = "hop_limit",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003907 .data = &ipv6_devconf.hop_limit,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908 .maxlen = sizeof(int),
3909 .mode = 0644,
3910 .proc_handler = proc_dointvec,
3911 },
3912 {
3913 .ctl_name = NET_IPV6_MTU,
3914 .procname = "mtu",
3915 .data = &ipv6_devconf.mtu6,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003916 .maxlen = sizeof(int),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003918 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919 },
3920 {
3921 .ctl_name = NET_IPV6_ACCEPT_RA,
3922 .procname = "accept_ra",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003923 .data = &ipv6_devconf.accept_ra,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924 .maxlen = sizeof(int),
3925 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003926 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927 },
3928 {
3929 .ctl_name = NET_IPV6_ACCEPT_REDIRECTS,
3930 .procname = "accept_redirects",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003931 .data = &ipv6_devconf.accept_redirects,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932 .maxlen = sizeof(int),
3933 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003934 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935 },
3936 {
3937 .ctl_name = NET_IPV6_AUTOCONF,
3938 .procname = "autoconf",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003939 .data = &ipv6_devconf.autoconf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940 .maxlen = sizeof(int),
3941 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003942 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003943 },
3944 {
3945 .ctl_name = NET_IPV6_DAD_TRANSMITS,
3946 .procname = "dad_transmits",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003947 .data = &ipv6_devconf.dad_transmits,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948 .maxlen = sizeof(int),
3949 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003950 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951 },
3952 {
3953 .ctl_name = NET_IPV6_RTR_SOLICITS,
3954 .procname = "router_solicitations",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003955 .data = &ipv6_devconf.rtr_solicits,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956 .maxlen = sizeof(int),
3957 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003958 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959 },
3960 {
3961 .ctl_name = NET_IPV6_RTR_SOLICIT_INTERVAL,
3962 .procname = "router_solicitation_interval",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003963 .data = &ipv6_devconf.rtr_solicit_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964 .maxlen = sizeof(int),
3965 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003966 .proc_handler = &proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967 .strategy = &sysctl_jiffies,
3968 },
3969 {
3970 .ctl_name = NET_IPV6_RTR_SOLICIT_DELAY,
3971 .procname = "router_solicitation_delay",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003972 .data = &ipv6_devconf.rtr_solicit_delay,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973 .maxlen = sizeof(int),
3974 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003975 .proc_handler = &proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976 .strategy = &sysctl_jiffies,
3977 },
3978 {
3979 .ctl_name = NET_IPV6_FORCE_MLD_VERSION,
3980 .procname = "force_mld_version",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003981 .data = &ipv6_devconf.force_mld_version,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982 .maxlen = sizeof(int),
3983 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003984 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985 },
3986#ifdef CONFIG_IPV6_PRIVACY
3987 {
3988 .ctl_name = NET_IPV6_USE_TEMPADDR,
3989 .procname = "use_tempaddr",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003990 .data = &ipv6_devconf.use_tempaddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991 .maxlen = sizeof(int),
3992 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003993 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994 },
3995 {
3996 .ctl_name = NET_IPV6_TEMP_VALID_LFT,
3997 .procname = "temp_valid_lft",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003998 .data = &ipv6_devconf.temp_valid_lft,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003999 .maxlen = sizeof(int),
4000 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004001 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002 },
4003 {
4004 .ctl_name = NET_IPV6_TEMP_PREFERED_LFT,
4005 .procname = "temp_prefered_lft",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004006 .data = &ipv6_devconf.temp_prefered_lft,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007 .maxlen = sizeof(int),
4008 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004009 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010 },
4011 {
4012 .ctl_name = NET_IPV6_REGEN_MAX_RETRY,
4013 .procname = "regen_max_retry",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004014 .data = &ipv6_devconf.regen_max_retry,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015 .maxlen = sizeof(int),
4016 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004017 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018 },
4019 {
4020 .ctl_name = NET_IPV6_MAX_DESYNC_FACTOR,
4021 .procname = "max_desync_factor",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004022 .data = &ipv6_devconf.max_desync_factor,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023 .maxlen = sizeof(int),
4024 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004025 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026 },
4027#endif
4028 {
4029 .ctl_name = NET_IPV6_MAX_ADDRESSES,
4030 .procname = "max_addresses",
4031 .data = &ipv6_devconf.max_addresses,
4032 .maxlen = sizeof(int),
4033 .mode = 0644,
4034 .proc_handler = &proc_dointvec,
4035 },
4036 {
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -08004037 .ctl_name = NET_IPV6_ACCEPT_RA_DEFRTR,
4038 .procname = "accept_ra_defrtr",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004039 .data = &ipv6_devconf.accept_ra_defrtr,
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -08004040 .maxlen = sizeof(int),
4041 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004042 .proc_handler = &proc_dointvec,
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -08004043 },
4044 {
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -08004045 .ctl_name = NET_IPV6_ACCEPT_RA_PINFO,
4046 .procname = "accept_ra_pinfo",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004047 .data = &ipv6_devconf.accept_ra_pinfo,
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -08004048 .maxlen = sizeof(int),
4049 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004050 .proc_handler = &proc_dointvec,
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -08004051 },
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08004052#ifdef CONFIG_IPV6_ROUTER_PREF
4053 {
4054 .ctl_name = NET_IPV6_ACCEPT_RA_RTR_PREF,
4055 .procname = "accept_ra_rtr_pref",
4056 .data = &ipv6_devconf.accept_ra_rtr_pref,
4057 .maxlen = sizeof(int),
4058 .mode = 0644,
4059 .proc_handler = &proc_dointvec,
4060 },
YOSHIFUJI Hideaki52e16352006-03-20 17:05:47 -08004061 {
4062 .ctl_name = NET_IPV6_RTR_PROBE_INTERVAL,
4063 .procname = "router_probe_interval",
4064 .data = &ipv6_devconf.rtr_probe_interval,
4065 .maxlen = sizeof(int),
4066 .mode = 0644,
4067 .proc_handler = &proc_dointvec_jiffies,
4068 .strategy = &sysctl_jiffies,
4069 },
Neil Hormanfa03ef32007-01-30 14:30:10 -08004070#ifdef CONFIG_IPV6_ROUTE_INFO
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -08004071 {
4072 .ctl_name = NET_IPV6_ACCEPT_RA_RT_INFO_MAX_PLEN,
4073 .procname = "accept_ra_rt_info_max_plen",
4074 .data = &ipv6_devconf.accept_ra_rt_info_max_plen,
4075 .maxlen = sizeof(int),
4076 .mode = 0644,
4077 .proc_handler = &proc_dointvec,
4078 },
4079#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08004080#endif
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -08004081 {
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -07004082 .ctl_name = NET_IPV6_PROXY_NDP,
4083 .procname = "proxy_ndp",
4084 .data = &ipv6_devconf.proxy_ndp,
4085 .maxlen = sizeof(int),
4086 .mode = 0644,
4087 .proc_handler = &proc_dointvec,
4088 },
4089 {
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -07004090 .ctl_name = NET_IPV6_ACCEPT_SOURCE_ROUTE,
4091 .procname = "accept_source_route",
4092 .data = &ipv6_devconf.accept_source_route,
4093 .maxlen = sizeof(int),
4094 .mode = 0644,
4095 .proc_handler = &proc_dointvec,
4096 },
Neil Horman95c385b2007-04-25 17:08:10 -07004097#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
4098 {
4099 .ctl_name = CTL_UNNUMBERED,
4100 .procname = "optimistic_dad",
4101 .data = &ipv6_devconf.optimistic_dad,
4102 .maxlen = sizeof(int),
4103 .mode = 0644,
4104 .proc_handler = &proc_dointvec,
4105
4106 },
4107#endif
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -07004108 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109 .ctl_name = 0, /* sentinel */
4110 }
4111 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112};
4113
Pavel Emelyanovbff16c22008-01-10 17:42:13 -08004114static int __addrconf_sysctl_register(struct net *net, char *dev_name,
4115 int ctl_name, struct inet6_dev *idev, struct ipv6_devconf *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116{
4117 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118 struct addrconf_sysctl_table *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119
Pavel Emelyanov1dab6222007-12-02 00:59:38 +11004120#define ADDRCONF_CTL_PATH_DEV 3
4121
4122 struct ctl_path addrconf_ctl_path[] = {
4123 { .procname = "net", .ctl_name = CTL_NET, },
4124 { .procname = "ipv6", .ctl_name = NET_IPV6, },
4125 { .procname = "conf", .ctl_name = NET_IPV6_CONF, },
4126 { /* to be set */ },
4127 { },
4128 };
4129
4130
Arnaldo Carvalho de Meloaf879cc2006-11-17 12:14:37 -02004131 t = kmemdup(&addrconf_sysctl, sizeof(*t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132 if (t == NULL)
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11004133 goto out;
4134
Linus Torvalds1da177e2005-04-16 15:20:36 -07004135 for (i=0; t->addrconf_vars[i].data; i++) {
4136 t->addrconf_vars[i].data += (char*)p - (char*)&ipv6_devconf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137 t->addrconf_vars[i].extra1 = idev; /* embedded; no ref */
Pavel Emelyanovbff16c22008-01-10 17:42:13 -08004138 t->addrconf_vars[i].extra2 = net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004141 /*
4142 * Make a copy of dev_name, because '.procname' is regarded as const
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143 * by sysctl and we wouldn't want anyone to change it under our feet
4144 * (see SIOCSIFNAME).
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004145 */
Pavel Emelyanov1dab6222007-12-02 00:59:38 +11004146 t->dev_name = kstrdup(dev_name, GFP_KERNEL);
4147 if (!t->dev_name)
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11004148 goto free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149
Pavel Emelyanov1dab6222007-12-02 00:59:38 +11004150 addrconf_ctl_path[ADDRCONF_CTL_PATH_DEV].procname = t->dev_name;
4151 addrconf_ctl_path[ADDRCONF_CTL_PATH_DEV].ctl_name = ctl_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152
Pavel Emelyanovbff16c22008-01-10 17:42:13 -08004153 t->sysctl_header = register_net_sysctl_table(net, addrconf_ctl_path,
Pavel Emelyanov1dab6222007-12-02 00:59:38 +11004154 t->addrconf_vars);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155 if (t->sysctl_header == NULL)
4156 goto free_procname;
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11004157
4158 p->sysctl = t;
Pavel Emelyanov95897312008-01-10 17:41:45 -08004159 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11004161free_procname:
Pavel Emelyanov1dab6222007-12-02 00:59:38 +11004162 kfree(t->dev_name);
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11004163free:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164 kfree(t);
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11004165out:
Pavel Emelyanov95897312008-01-10 17:41:45 -08004166 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167}
4168
Pavel Emelyanov408c4762008-01-10 17:41:21 -08004169static void __addrconf_sysctl_unregister(struct ipv6_devconf *p)
4170{
4171 struct addrconf_sysctl_table *t;
4172
4173 if (p->sysctl == NULL)
4174 return;
4175
4176 t = p->sysctl;
4177 p->sysctl = NULL;
4178 unregister_sysctl_table(t->sysctl_header);
4179 kfree(t->dev_name);
4180 kfree(t);
4181}
4182
Pavel Emelyanovf52295a2007-12-02 00:58:37 +11004183static void addrconf_sysctl_register(struct inet6_dev *idev)
4184{
Pavel Emelyanov408c4762008-01-10 17:41:21 -08004185 neigh_sysctl_register(idev->dev, idev->nd_parms, NET_IPV6,
4186 NET_IPV6_NEIGH, "ipv6",
4187 &ndisc_ifinfo_sysctl_change,
4188 NULL);
Pavel Emelyanovbff16c22008-01-10 17:42:13 -08004189 __addrconf_sysctl_register(idev->dev->nd_net, idev->dev->name,
4190 idev->dev->ifindex, idev, &idev->cnf);
Pavel Emelyanovf52295a2007-12-02 00:58:37 +11004191}
4192
Pavel Emelyanov408c4762008-01-10 17:41:21 -08004193static void addrconf_sysctl_unregister(struct inet6_dev *idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194{
Pavel Emelyanov408c4762008-01-10 17:41:21 -08004195 __addrconf_sysctl_unregister(&idev->cnf);
4196 neigh_sysctl_unregister(idev->nd_parms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197}
4198
4199
4200#endif
4201
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08004202static int addrconf_init_net(struct net *net)
4203{
4204 int err;
4205 struct ipv6_devconf *all, *dflt;
4206
4207 err = -ENOMEM;
4208 all = &ipv6_devconf;
4209 dflt = &ipv6_devconf_dflt;
4210
4211 if (net != &init_net) {
4212 all = kmemdup(all, sizeof(ipv6_devconf), GFP_KERNEL);
4213 if (all == NULL)
4214 goto err_alloc_all;
4215
4216 dflt = kmemdup(dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL);
4217 if (dflt == NULL)
4218 goto err_alloc_dflt;
4219 }
4220
4221 net->ipv6.devconf_all = all;
4222 net->ipv6.devconf_dflt = dflt;
4223
4224#ifdef CONFIG_SYSCTL
4225 err = __addrconf_sysctl_register(net, "all", NET_PROTO_CONF_ALL,
4226 NULL, all);
4227 if (err < 0)
4228 goto err_reg_all;
4229
4230 err = __addrconf_sysctl_register(net, "default", NET_PROTO_CONF_DEFAULT,
4231 NULL, dflt);
4232 if (err < 0)
4233 goto err_reg_dflt;
4234#endif
4235 return 0;
4236
4237#ifdef CONFIG_SYSCTL
4238err_reg_dflt:
4239 __addrconf_sysctl_unregister(all);
4240err_reg_all:
4241 kfree(dflt);
4242#endif
4243err_alloc_dflt:
4244 kfree(all);
4245err_alloc_all:
4246 return err;
4247}
4248
4249static void addrconf_exit_net(struct net *net)
4250{
4251#ifdef CONFIG_SYSCTL
4252 __addrconf_sysctl_unregister(net->ipv6.devconf_dflt);
4253 __addrconf_sysctl_unregister(net->ipv6.devconf_all);
4254#endif
4255 if (net != &init_net) {
4256 kfree(net->ipv6.devconf_dflt);
4257 kfree(net->ipv6.devconf_all);
4258 }
4259}
4260
4261static struct pernet_operations addrconf_ops = {
4262 .init = addrconf_init_net,
4263 .exit = addrconf_exit_net,
4264};
4265
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266/*
4267 * Device notifier
4268 */
4269
4270int register_inet6addr_notifier(struct notifier_block *nb)
4271{
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004272 return atomic_notifier_chain_register(&inet6addr_chain, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273}
4274
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +09004275EXPORT_SYMBOL(register_inet6addr_notifier);
4276
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277int unregister_inet6addr_notifier(struct notifier_block *nb)
4278{
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004279 return atomic_notifier_chain_unregister(&inet6addr_chain,nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280}
4281
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +09004282EXPORT_SYMBOL(unregister_inet6addr_notifier);
4283
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284/*
4285 * Init / cleanup code
4286 */
4287
4288int __init addrconf_init(void)
4289{
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +09004290 int err;
4291
4292 if ((err = ipv6_addr_label_init()) < 0) {
4293 printk(KERN_CRIT "IPv6 Addrconf: cannot initialize default policy table: %d.\n",
4294 err);
4295 return err;
4296 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08004298 register_pernet_subsys(&addrconf_ops);
4299
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300 /* The addrconf netdev notifier requires that loopback_dev
4301 * has it's ipv6 private information allocated and setup
4302 * before it can bring up and give link-local addresses
4303 * to other devices which are up.
4304 *
4305 * Unfortunately, loopback_dev is not necessarily the first
4306 * entry in the global dev_base list of net devices. In fact,
4307 * it is likely to be the very last entry on that list.
4308 * So this causes the notifier registry below to try and
4309 * give link-local addresses to all devices besides loopback_dev
4310 * first, then loopback_dev, which cases all the non-loopback_dev
4311 * devices to fail to get a link-local address.
4312 *
4313 * So, as a temporary fix, allocate the ipv6 structure for
4314 * loopback_dev first by hand.
4315 * Longer term, all of the dependencies ipv6 has upon the loopback
4316 * device and it being up should be removed.
4317 */
4318 rtnl_lock();
Eric W. Biederman2774c7a2007-09-26 22:10:56 -07004319 if (!ipv6_add_dev(init_net.loopback_dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320 err = -ENOMEM;
4321 rtnl_unlock();
4322 if (err)
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08004323 goto errlo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324
Eric W. Biederman2774c7a2007-09-26 22:10:56 -07004325 ip6_null_entry.u.dst.dev = init_net.loopback_dev;
4326 ip6_null_entry.rt6i_idev = in6_dev_get(init_net.loopback_dev);
YOSHIFUJI Hideaki9a6bf6f2007-05-09 14:03:28 -07004327#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Eric W. Biederman2774c7a2007-09-26 22:10:56 -07004328 ip6_prohibit_entry.u.dst.dev = init_net.loopback_dev;
4329 ip6_prohibit_entry.rt6i_idev = in6_dev_get(init_net.loopback_dev);
4330 ip6_blk_hole_entry.u.dst.dev = init_net.loopback_dev;
4331 ip6_blk_hole_entry.rt6i_idev = in6_dev_get(init_net.loopback_dev);
YOSHIFUJI Hideaki9a6bf6f2007-05-09 14:03:28 -07004332#endif
Herbert Xuc62dba92005-09-26 15:10:16 -07004333
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334 register_netdevice_notifier(&ipv6_dev_notf);
4335
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336 addrconf_verify(0);
Thomas Grafc127ea22007-03-22 11:58:32 -07004337
4338 err = __rtnl_register(PF_INET6, RTM_GETLINK, NULL, inet6_dump_ifinfo);
4339 if (err < 0)
4340 goto errout;
4341
4342 /* Only the first call to __rtnl_register can fail */
4343 __rtnl_register(PF_INET6, RTM_NEWADDR, inet6_rtm_newaddr, NULL);
4344 __rtnl_register(PF_INET6, RTM_DELADDR, inet6_rtm_deladdr, NULL);
4345 __rtnl_register(PF_INET6, RTM_GETADDR, inet6_rtm_getaddr, inet6_dump_ifaddr);
4346 __rtnl_register(PF_INET6, RTM_GETMULTICAST, NULL, inet6_dump_ifmcaddr);
4347 __rtnl_register(PF_INET6, RTM_GETANYCAST, NULL, inet6_dump_ifacaddr);
4348
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +09004349 ipv6_addr_label_rtnl_register();
4350
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351 return 0;
Thomas Grafc127ea22007-03-22 11:58:32 -07004352errout:
4353 unregister_netdevice_notifier(&ipv6_dev_notf);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08004354errlo:
4355 unregister_pernet_subsys(&addrconf_ops);
Thomas Grafc127ea22007-03-22 11:58:32 -07004356
4357 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004358}
4359
Daniel Lezcano09f77092007-12-13 05:34:58 -08004360void addrconf_cleanup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361{
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004362 struct net_device *dev;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004363 struct inet6_ifaddr *ifa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004364 int i;
4365
4366 unregister_netdevice_notifier(&ipv6_dev_notf);
4367
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08004368 unregister_pernet_subsys(&addrconf_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004369
4370 rtnl_lock();
4371
4372 /*
4373 * clean dev list.
4374 */
4375
Eric W. Biederman881d9662007-09-17 11:56:21 -07004376 for_each_netdev(&init_net, dev) {
Micah Gruberdffe4f02007-07-10 23:04:19 -07004377 if (__in6_dev_get(dev) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378 continue;
4379 addrconf_ifdown(dev, 1);
4380 }
Eric W. Biederman2774c7a2007-09-26 22:10:56 -07004381 addrconf_ifdown(init_net.loopback_dev, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382
4383 /*
4384 * Check hash table.
4385 */
4386
4387 write_lock_bh(&addrconf_hash_lock);
4388 for (i=0; i < IN6_ADDR_HSIZE; i++) {
4389 for (ifa=inet6_addr_lst[i]; ifa; ) {
4390 struct inet6_ifaddr *bifa;
4391
4392 bifa = ifa;
4393 ifa = ifa->lst_next;
4394 printk(KERN_DEBUG "bug: IPv6 address leakage detected: ifa=%p\n", bifa);
4395 /* Do not free it; something is wrong.
4396 Now we can investigate it with debugger.
4397 */
4398 }
4399 }
4400 write_unlock_bh(&addrconf_hash_lock);
4401
4402 del_timer(&addr_chk_timer);
4403
4404 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405}