blob: fa43374e85c5aed988adbb03d618dc04d57e2dc1 [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;
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +0900225const struct in6_addr in6addr_linklocal_allnodes = IN6ADDR_LINKLOCAL_ALLNODES_INIT;
226const struct in6_addr in6addr_linklocal_allrouters = IN6ADDR_LINKLOCAL_ALLROUTERS_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -0700228/* Check if a valid qdisc is available */
229static inline int addrconf_qdisc_ok(struct net_device *dev)
230{
231 return (dev->qdisc != &noop_qdisc);
232}
233
YOSHIFUJI Hideaki2b5ead42008-05-13 01:16:24 +0900234/* Check if a route is valid prefix route */
235static inline int addrconf_is_prefix_route(const struct rt6_info *rt)
236{
237 return ((rt->rt6i_flags & (RTF_GATEWAY | RTF_DEFAULT)) == 0);
238}
239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240static void addrconf_del_timer(struct inet6_ifaddr *ifp)
241{
242 if (del_timer(&ifp->timer))
243 __in6_ifa_put(ifp);
244}
245
246enum addrconf_timer_t
247{
248 AC_NONE,
249 AC_DAD,
250 AC_RS,
251};
252
253static void addrconf_mod_timer(struct inet6_ifaddr *ifp,
254 enum addrconf_timer_t what,
255 unsigned long when)
256{
257 if (!del_timer(&ifp->timer))
258 in6_ifa_hold(ifp);
259
260 switch (what) {
261 case AC_DAD:
262 ifp->timer.function = addrconf_dad_timer;
263 break;
264 case AC_RS:
265 ifp->timer.function = addrconf_rs_timer;
266 break;
267 default:;
268 }
269 ifp->timer.expires = jiffies + when;
270 add_timer(&ifp->timer);
271}
272
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700273static int snmp6_alloc_dev(struct inet6_dev *idev)
274{
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700275 if (snmp_mib_init((void **)idev->stats.ipv6,
YOSHIFUJI Hideakic69bce22008-01-23 22:31:45 -0800276 sizeof(struct ipstats_mib)) < 0)
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700277 goto err_ip;
278 if (snmp_mib_init((void **)idev->stats.icmpv6,
YOSHIFUJI Hideakic69bce22008-01-23 22:31:45 -0800279 sizeof(struct icmpv6_mib)) < 0)
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700280 goto err_icmp;
David L Stevens14878f72007-09-16 16:52:35 -0700281 if (snmp_mib_init((void **)idev->stats.icmpv6msg,
YOSHIFUJI Hideakic69bce22008-01-23 22:31:45 -0800282 sizeof(struct icmpv6msg_mib)) < 0)
David L Stevens14878f72007-09-16 16:52:35 -0700283 goto err_icmpmsg;
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700284
285 return 0;
286
David L Stevens14878f72007-09-16 16:52:35 -0700287err_icmpmsg:
288 snmp_mib_free((void **)idev->stats.icmpv6);
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700289err_icmp:
290 snmp_mib_free((void **)idev->stats.ipv6);
291err_ip:
Pavel Emelyanovaaf70ec2007-10-17 21:25:32 -0700292 return -ENOMEM;
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700293}
294
Pavel Emelyanov16910b92007-10-17 21:23:43 -0700295static void snmp6_free_dev(struct inet6_dev *idev)
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700296{
David L Stevens14878f72007-09-16 16:52:35 -0700297 snmp_mib_free((void **)idev->stats.icmpv6msg);
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700298 snmp_mib_free((void **)idev->stats.icmpv6);
299 snmp_mib_free((void **)idev->stats.ipv6);
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700300}
301
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302/* Nobody refers to this device, we may destroy it. */
303
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -0700304static void in6_dev_finish_destroy_rcu(struct rcu_head *head)
305{
306 struct inet6_dev *idev = container_of(head, struct inet6_dev, rcu);
307 kfree(idev);
308}
309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310void in6_dev_finish_destroy(struct inet6_dev *idev)
311{
312 struct net_device *dev = idev->dev;
313 BUG_TRAP(idev->addr_list==NULL);
314 BUG_TRAP(idev->mc_list==NULL);
315#ifdef NET_REFCNT_DEBUG
316 printk(KERN_DEBUG "in6_dev_finish_destroy: %s\n", dev ? dev->name : "NIL");
317#endif
318 dev_put(dev);
319 if (!idev->dead) {
320 printk("Freeing alive inet6 device %p\n", idev);
321 return;
322 }
323 snmp6_free_dev(idev);
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -0700324 call_rcu(&idev->rcu, in6_dev_finish_destroy_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325}
326
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +0900327EXPORT_SYMBOL(in6_dev_finish_destroy);
328
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329static struct inet6_dev * ipv6_add_dev(struct net_device *dev)
330{
331 struct inet6_dev *ndev;
332
333 ASSERT_RTNL();
334
335 if (dev->mtu < IPV6_MIN_MTU)
336 return NULL;
337
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900338 ndev = kzalloc(sizeof(struct inet6_dev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900340 if (ndev == NULL)
341 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Ingo Oeser322f74a2006-03-20 23:01:47 -0800343 rwlock_init(&ndev->lock);
344 ndev->dev = dev;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900345 memcpy(&ndev->cnf, dev_net(dev)->ipv6.devconf_dflt, sizeof(ndev->cnf));
Ingo Oeser322f74a2006-03-20 23:01:47 -0800346 ndev->cnf.mtu6 = dev->mtu;
347 ndev->cnf.sysctl = NULL;
348 ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl);
349 if (ndev->nd_parms == NULL) {
350 kfree(ndev);
351 return NULL;
352 }
353 /* We refer to the device */
354 dev_hold(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
Ingo Oeser322f74a2006-03-20 23:01:47 -0800356 if (snmp6_alloc_dev(ndev) < 0) {
357 ADBG((KERN_WARNING
358 "%s(): cannot allocate memory for statistics; dev=%s.\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800359 __func__, dev->name));
Ingo Oeser322f74a2006-03-20 23:01:47 -0800360 neigh_parms_release(&nd_tbl, ndev->nd_parms);
361 ndev->dead = 1;
362 in6_dev_finish_destroy(ndev);
363 return NULL;
364 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Ingo Oeser322f74a2006-03-20 23:01:47 -0800366 if (snmp6_register_dev(ndev) < 0) {
367 ADBG((KERN_WARNING
368 "%s(): cannot create /proc/net/dev_snmp6/%s\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800369 __func__, dev->name));
Ingo Oeser322f74a2006-03-20 23:01:47 -0800370 neigh_parms_release(&nd_tbl, ndev->nd_parms);
371 ndev->dead = 1;
372 in6_dev_finish_destroy(ndev);
373 return NULL;
374 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Ingo Oeser322f74a2006-03-20 23:01:47 -0800376 /* One reference from device. We must do this before
377 * we invoke __ipv6_regen_rndid().
378 */
379 in6_dev_hold(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
YOSHIFUJI Hideakib077d7a2008-04-13 23:42:18 -0700381#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
382 if (dev->type == ARPHRD_SIT && (dev->priv_flags & IFF_ISATAP)) {
383 printk(KERN_INFO
384 "%s: Disabled Multicast RS\n",
385 dev->name);
386 ndev->cnf.rtr_solicits = 0;
387 }
388#endif
389
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390#ifdef CONFIG_IPV6_PRIVACY
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800391 setup_timer(&ndev->regen_timer, ipv6_regen_rndid, (unsigned long)ndev);
Ingo Oeser322f74a2006-03-20 23:01:47 -0800392 if ((dev->flags&IFF_LOOPBACK) ||
393 dev->type == ARPHRD_TUNNEL ||
YOSHIFUJI Hideaki9625ed72008-04-13 23:47:11 -0700394 dev->type == ARPHRD_TUNNEL6 ||
Joerg Roedel0be669b2006-10-10 14:49:53 -0700395 dev->type == ARPHRD_SIT ||
Joerg Roedel0be669b2006-10-10 14:49:53 -0700396 dev->type == ARPHRD_NONE) {
Ingo Oeser322f74a2006-03-20 23:01:47 -0800397 printk(KERN_INFO
398 "%s: Disabled Privacy Extensions\n",
399 dev->name);
400 ndev->cnf.use_tempaddr = -1;
401 } else {
402 in6_dev_hold(ndev);
403 ipv6_regen_rndid((unsigned long) ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 }
Ingo Oeser322f74a2006-03-20 23:01:47 -0800405#endif
406
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -0700407 if (netif_running(dev) && addrconf_qdisc_ok(dev))
Herbert Xu53aadcc2007-03-27 14:31:52 -0700408 ndev->if_flags |= IF_READY;
409
Ingo Oeser322f74a2006-03-20 23:01:47 -0800410 ipv6_mc_init_dev(ndev);
411 ndev->tstamp = jiffies;
Pavel Emelyanovf52295a2007-12-02 00:58:37 +1100412 addrconf_sysctl_register(ndev);
David L Stevens30c4cf52007-01-04 12:31:14 -0800413 /* protected by rtnl_lock */
414 rcu_assign_pointer(dev->ip6_ptr, ndev);
YOSHIFUJI Hideakid88ae4c2007-01-14 21:48:40 -0800415
416 /* Join all-node multicast group */
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +0900417 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allnodes);
YOSHIFUJI Hideakid88ae4c2007-01-14 21:48:40 -0800418
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 return ndev;
420}
421
YOSHIFUJI Hideakicee89472008-04-13 23:21:16 -0700422static struct inet6_dev * ipv6_find_idev(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423{
424 struct inet6_dev *idev;
425
426 ASSERT_RTNL();
427
428 if ((idev = __in6_dev_get(dev)) == NULL) {
429 if ((idev = ipv6_add_dev(dev)) == NULL)
430 return NULL;
431 }
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +0900432
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 if (dev->flags&IFF_UP)
434 ipv6_mc_up(idev);
435 return idev;
436}
437
438#ifdef CONFIG_SYSCTL
439static void dev_forward_change(struct inet6_dev *idev)
440{
441 struct net_device *dev;
442 struct inet6_ifaddr *ifa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
444 if (!idev)
445 return;
446 dev = idev->dev;
447 if (dev && (dev->flags & IFF_MULTICAST)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 if (idev->cnf.forwarding)
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +0900449 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 else
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +0900451 ipv6_dev_mc_dec(dev, &in6addr_linklocal_allrouters);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 }
453 for (ifa=idev->addr_list; ifa; ifa=ifa->if_next) {
Michal Wrobel2c12a742007-02-26 15:36:10 -0800454 if (ifa->flags&IFA_F_TENTATIVE)
455 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 if (idev->cnf.forwarding)
457 addrconf_join_anycast(ifa);
458 else
459 addrconf_leave_anycast(ifa);
460 }
461}
462
463
Pavel Emelyanove1869322008-01-10 17:43:50 -0800464static void addrconf_forward_change(struct net *net, __s32 newf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465{
466 struct net_device *dev;
467 struct inet6_dev *idev;
468
469 read_lock(&dev_base_lock);
Pavel Emelyanovbff16c22008-01-10 17:42:13 -0800470 for_each_netdev(net, dev) {
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -0700471 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 idev = __in6_dev_get(dev);
473 if (idev) {
Pavel Emelyanove1869322008-01-10 17:43:50 -0800474 int changed = (!idev->cnf.forwarding) ^ (!newf);
475 idev->cnf.forwarding = newf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 if (changed)
477 dev_forward_change(idev);
478 }
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -0700479 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 }
481 read_unlock(&dev_base_lock);
482}
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800483
484static void addrconf_fixup_forwarding(struct ctl_table *table, int *p, int old)
485{
Pavel Emelyanovbff16c22008-01-10 17:42:13 -0800486 struct net *net;
487
488 net = (struct net *)table->extra2;
Pavel Emelyanov441fc2a2008-01-10 17:43:22 -0800489 if (p == &net->ipv6.devconf_dflt->forwarding)
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800490 return;
491
Pavel Emelyanove1869322008-01-10 17:43:50 -0800492 if (p == &net->ipv6.devconf_all->forwarding) {
493 __s32 newf = net->ipv6.devconf_all->forwarding;
494 net->ipv6.devconf_dflt->forwarding = newf;
495 addrconf_forward_change(net, newf);
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800496 } else if ((!*p) ^ (!old))
497 dev_forward_change((struct inet6_dev *)table->extra1);
498
499 if (*p)
Daniel Lezcano7b4da532008-03-04 13:47:14 -0800500 rt6_purge_dflt_routers(net);
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800501}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502#endif
503
504/* Nobody refers to this ifaddr, destroy it */
505
506void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp)
507{
508 BUG_TRAP(ifp->if_next==NULL);
509 BUG_TRAP(ifp->lst_next==NULL);
510#ifdef NET_REFCNT_DEBUG
511 printk(KERN_DEBUG "inet6_ifa_finish_destroy\n");
512#endif
513
514 in6_dev_put(ifp->idev);
515
516 if (del_timer(&ifp->timer))
517 printk("Timer is still running, when freeing ifa=%p\n", ifp);
518
519 if (!ifp->dead) {
520 printk("Freeing alive inet6 address %p\n", ifp);
521 return;
522 }
523 dst_release(&ifp->rt->u.dst);
524
525 kfree(ifp);
526}
527
Brian Haleye55ffac2006-07-10 15:25:51 -0700528static void
529ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp)
530{
531 struct inet6_ifaddr *ifa, **ifap;
YOSHIFUJI Hideaki8a6ce0c2006-07-11 13:05:30 -0700532 int ifp_scope = ipv6_addr_src_scope(&ifp->addr);
Brian Haleye55ffac2006-07-10 15:25:51 -0700533
534 /*
535 * Each device address list is sorted in order of scope -
536 * global before linklocal.
537 */
538 for (ifap = &idev->addr_list; (ifa = *ifap) != NULL;
539 ifap = &ifa->if_next) {
YOSHIFUJI Hideaki8a6ce0c2006-07-11 13:05:30 -0700540 if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr))
Brian Haleye55ffac2006-07-10 15:25:51 -0700541 break;
542 }
543
544 ifp->if_next = *ifap;
545 *ifap = ifp;
546}
547
YOSHIFUJI Hideaki3eb84f42008-04-10 15:42:08 +0900548/*
549 * Hash function taken from net_alias.c
550 */
551static u8 ipv6_addr_hash(const struct in6_addr *addr)
552{
553 __u32 word;
554
555 /*
556 * We perform the hash function over the last 64 bits of the address
557 * This will include the IEEE address token on links that support it.
558 */
559
560 word = (__force u32)(addr->s6_addr32[2] ^ addr->s6_addr32[3]);
561 word ^= (word >> 16);
562 word ^= (word >> 8);
563
564 return ((word ^ (word >> 4)) & 0x0f);
565}
566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567/* On success it returns ifp with increased reference count */
568
569static struct inet6_ifaddr *
570ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr, int pfxlen,
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -0700571 int scope, u32 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572{
573 struct inet6_ifaddr *ifa = NULL;
574 struct rt6_info *rt;
575 int hash;
576 int err = 0;
577
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -0700578 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 if (idev->dead) {
580 err = -ENODEV; /*XXX*/
581 goto out2;
582 }
583
584 write_lock(&addrconf_hash_lock);
585
586 /* Ignore adding duplicate addresses on an interface */
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900587 if (ipv6_chk_same_addr(dev_net(idev->dev), addr, idev->dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 ADBG(("ipv6_add_addr: already assigned\n"));
589 err = -EEXIST;
590 goto out;
591 }
592
Ingo Oeser322f74a2006-03-20 23:01:47 -0800593 ifa = kzalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
595 if (ifa == NULL) {
596 ADBG(("ipv6_add_addr: malloc failed\n"));
597 err = -ENOBUFS;
598 goto out;
599 }
600
601 rt = addrconf_dst_alloc(idev, addr, 0);
602 if (IS_ERR(rt)) {
603 err = PTR_ERR(rt);
604 goto out;
605 }
606
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 ipv6_addr_copy(&ifa->addr, addr);
608
609 spin_lock_init(&ifa->lock);
610 init_timer(&ifa->timer);
611 ifa->timer.data = (unsigned long) ifa;
612 ifa->scope = scope;
613 ifa->prefix_len = pfxlen;
614 ifa->flags = flags | IFA_F_TENTATIVE;
615 ifa->cstamp = ifa->tstamp = jiffies;
616
Keir Fraser57f5f542006-08-29 02:43:49 -0700617 ifa->rt = rt;
618
Neil Horman95c385b2007-04-25 17:08:10 -0700619 /*
620 * part one of RFC 4429, section 3.3
621 * We should not configure an address as
622 * optimistic if we do not yet know the link
623 * layer address of our nexhop router
624 */
625
626 if (rt->rt6i_nexthop == NULL)
627 ifa->flags &= ~IFA_F_OPTIMISTIC;
628
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 ifa->idev = idev;
630 in6_dev_hold(idev);
631 /* For caller */
632 in6_ifa_hold(ifa);
633
634 /* Add to big hash table */
635 hash = ipv6_addr_hash(addr);
636
637 ifa->lst_next = inet6_addr_lst[hash];
638 inet6_addr_lst[hash] = ifa;
639 in6_ifa_hold(ifa);
640 write_unlock(&addrconf_hash_lock);
641
642 write_lock(&idev->lock);
643 /* Add to inet6_dev unicast addr list. */
Brian Haleye55ffac2006-07-10 15:25:51 -0700644 ipv6_link_dev_addr(idev, ifa);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
646#ifdef CONFIG_IPV6_PRIVACY
647 if (ifa->flags&IFA_F_TEMPORARY) {
648 ifa->tmp_next = idev->tempaddr_list;
649 idev->tempaddr_list = ifa;
650 in6_ifa_hold(ifa);
651 }
652#endif
653
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 in6_ifa_hold(ifa);
655 write_unlock(&idev->lock);
656out2:
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -0700657 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
YOSHIFUJI Hideakifd928332005-04-19 22:27:09 -0700659 if (likely(err == 0))
Alan Sterne041c682006-03-27 01:16:30 -0800660 atomic_notifier_call_chain(&inet6addr_chain, NETDEV_UP, ifa);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 else {
662 kfree(ifa);
663 ifa = ERR_PTR(err);
664 }
665
666 return ifa;
667out:
668 write_unlock(&addrconf_hash_lock);
669 goto out2;
670}
671
672/* This function wants to get referenced ifp and releases it before return */
673
674static void ipv6_del_addr(struct inet6_ifaddr *ifp)
675{
676 struct inet6_ifaddr *ifa, **ifap;
677 struct inet6_dev *idev = ifp->idev;
678 int hash;
679 int deleted = 0, onlink = 0;
680 unsigned long expires = jiffies;
681
682 hash = ipv6_addr_hash(&ifp->addr);
683
684 ifp->dead = 1;
685
686 write_lock_bh(&addrconf_hash_lock);
687 for (ifap = &inet6_addr_lst[hash]; (ifa=*ifap) != NULL;
688 ifap = &ifa->lst_next) {
689 if (ifa == ifp) {
690 *ifap = ifa->lst_next;
691 __in6_ifa_put(ifp);
692 ifa->lst_next = NULL;
693 break;
694 }
695 }
696 write_unlock_bh(&addrconf_hash_lock);
697
698 write_lock_bh(&idev->lock);
699#ifdef CONFIG_IPV6_PRIVACY
700 if (ifp->flags&IFA_F_TEMPORARY) {
701 for (ifap = &idev->tempaddr_list; (ifa=*ifap) != NULL;
702 ifap = &ifa->tmp_next) {
703 if (ifa == ifp) {
704 *ifap = ifa->tmp_next;
705 if (ifp->ifpub) {
706 in6_ifa_put(ifp->ifpub);
707 ifp->ifpub = NULL;
708 }
709 __in6_ifa_put(ifp);
710 ifa->tmp_next = NULL;
711 break;
712 }
713 }
714 }
715#endif
716
Kristian Slavov1d142802005-12-21 18:47:24 -0800717 for (ifap = &idev->addr_list; (ifa=*ifap) != NULL;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 if (ifa == ifp) {
719 *ifap = ifa->if_next;
720 __in6_ifa_put(ifp);
721 ifa->if_next = NULL;
722 if (!(ifp->flags & IFA_F_PERMANENT) || onlink > 0)
723 break;
724 deleted = 1;
Kristian Slavov1d142802005-12-21 18:47:24 -0800725 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 } else if (ifp->flags & IFA_F_PERMANENT) {
727 if (ipv6_prefix_equal(&ifa->addr, &ifp->addr,
728 ifp->prefix_len)) {
729 if (ifa->flags & IFA_F_PERMANENT) {
730 onlink = 1;
731 if (deleted)
732 break;
733 } else {
734 unsigned long lifetime;
735
736 if (!onlink)
737 onlink = -1;
738
739 spin_lock(&ifa->lock);
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +0900740
741 lifetime = addrconf_timeout_fixup(ifa->valid_lft, HZ);
742 /*
743 * Note: Because this address is
744 * not permanent, lifetime <
745 * LONG_MAX / HZ here.
746 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 if (time_before(expires,
748 ifa->tstamp + lifetime * HZ))
749 expires = ifa->tstamp + lifetime * HZ;
750 spin_unlock(&ifa->lock);
751 }
752 }
753 }
Kristian Slavov1d142802005-12-21 18:47:24 -0800754 ifap = &ifa->if_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 }
756 write_unlock_bh(&idev->lock);
757
758 ipv6_ifa_notify(RTM_DELADDR, ifp);
759
Alan Sterne041c682006-03-27 01:16:30 -0800760 atomic_notifier_call_chain(&inet6addr_chain, NETDEV_DOWN, ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
762 addrconf_del_timer(ifp);
763
764 /*
765 * Purge or update corresponding prefix
766 *
767 * 1) we don't purge prefix here if address was not permanent.
768 * prefix is managed by its own lifetime.
769 * 2) if there're no addresses, delete prefix.
770 * 3) if there're still other permanent address(es),
771 * corresponding prefix is still permanent.
772 * 4) otherwise, update prefix lifetime to the
773 * longest valid lifetime among the corresponding
774 * addresses on the device.
775 * Note: subsequent RA will update lifetime.
776 *
777 * --yoshfuji
778 */
779 if ((ifp->flags & IFA_F_PERMANENT) && onlink < 1) {
780 struct in6_addr prefix;
781 struct rt6_info *rt;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900782 struct net *net = dev_net(ifp->idev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 ipv6_addr_prefix(&prefix, &ifp->addr, ifp->prefix_len);
Benjamin Thery6fda7352008-03-05 10:47:47 -0800784 rt = rt6_lookup(net, &prefix, NULL, ifp->idev->dev->ifindex, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
YOSHIFUJI Hideaki2b5ead42008-05-13 01:16:24 +0900786 if (rt && addrconf_is_prefix_route(rt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 if (onlink == 0) {
Thomas Grafe0a1ad732006-08-22 00:00:21 -0700788 ip6_del_rt(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 rt = NULL;
790 } else if (!(rt->rt6i_flags & RTF_EXPIRES)) {
791 rt->rt6i_expires = expires;
792 rt->rt6i_flags |= RTF_EXPIRES;
793 }
794 }
795 dst_release(&rt->u.dst);
796 }
797
798 in6_ifa_put(ifp);
799}
800
801#ifdef CONFIG_IPV6_PRIVACY
802static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift)
803{
804 struct inet6_dev *idev = ifp->idev;
805 struct in6_addr addr, *tmpaddr;
806 unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_cstamp, tmp_tstamp;
Benoit Boissinoteac55bf2008-04-02 00:01:35 -0700807 unsigned long regen_advance;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 int tmp_plen;
809 int ret = 0;
810 int max_addresses;
Neil Horman95c385b2007-04-25 17:08:10 -0700811 u32 addr_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
813 write_lock(&idev->lock);
814 if (ift) {
815 spin_lock_bh(&ift->lock);
816 memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8);
817 spin_unlock_bh(&ift->lock);
818 tmpaddr = &addr;
819 } else {
820 tmpaddr = NULL;
821 }
822retry:
823 in6_dev_hold(idev);
824 if (idev->cnf.use_tempaddr <= 0) {
825 write_unlock(&idev->lock);
826 printk(KERN_INFO
827 "ipv6_create_tempaddr(): use_tempaddr is disabled.\n");
828 in6_dev_put(idev);
829 ret = -1;
830 goto out;
831 }
832 spin_lock_bh(&ifp->lock);
833 if (ifp->regen_count++ >= idev->cnf.regen_max_retry) {
834 idev->cnf.use_tempaddr = -1; /*XXX*/
835 spin_unlock_bh(&ifp->lock);
836 write_unlock(&idev->lock);
837 printk(KERN_WARNING
838 "ipv6_create_tempaddr(): regeneration time exceeded. disabled temporary address support.\n");
839 in6_dev_put(idev);
840 ret = -1;
841 goto out;
842 }
843 in6_ifa_hold(ifp);
844 memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
845 if (__ipv6_try_regen_rndid(idev, tmpaddr) < 0) {
846 spin_unlock_bh(&ifp->lock);
847 write_unlock(&idev->lock);
848 printk(KERN_WARNING
849 "ipv6_create_tempaddr(): regeneration of randomized interface id failed.\n");
850 in6_ifa_put(ifp);
851 in6_dev_put(idev);
852 ret = -1;
853 goto out;
854 }
855 memcpy(&addr.s6_addr[8], idev->rndid, 8);
856 tmp_valid_lft = min_t(__u32,
857 ifp->valid_lft,
858 idev->cnf.temp_valid_lft);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900859 tmp_prefered_lft = min_t(__u32,
860 ifp->prefered_lft,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 idev->cnf.temp_prefered_lft - desync_factor / HZ);
862 tmp_plen = ifp->prefix_len;
863 max_addresses = idev->cnf.max_addresses;
864 tmp_cstamp = ifp->cstamp;
865 tmp_tstamp = ifp->tstamp;
866 spin_unlock_bh(&ifp->lock);
867
Benoit Boissinoteac55bf2008-04-02 00:01:35 -0700868 regen_advance = idev->cnf.regen_max_retry *
869 idev->cnf.dad_transmits *
870 idev->nd_parms->retrans_time / HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 write_unlock(&idev->lock);
Neil Horman95c385b2007-04-25 17:08:10 -0700872
Benoit Boissinoteac55bf2008-04-02 00:01:35 -0700873 /* A temporary address is created only if this calculated Preferred
874 * Lifetime is greater than REGEN_ADVANCE time units. In particular,
875 * an implementation must not create a temporary address with a zero
876 * Preferred Lifetime.
877 */
878 if (tmp_prefered_lft <= regen_advance) {
879 in6_ifa_put(ifp);
880 in6_dev_put(idev);
881 ret = -1;
882 goto out;
883 }
884
Neil Horman95c385b2007-04-25 17:08:10 -0700885 addr_flags = IFA_F_TEMPORARY;
886 /* set in addrconf_prefix_rcv() */
887 if (ifp->flags & IFA_F_OPTIMISTIC)
888 addr_flags |= IFA_F_OPTIMISTIC;
889
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 ift = !max_addresses ||
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900891 ipv6_count_addresses(idev) < max_addresses ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 ipv6_add_addr(idev, &addr, tmp_plen,
Neil Horman95c385b2007-04-25 17:08:10 -0700893 ipv6_addr_type(&addr)&IPV6_ADDR_SCOPE_MASK,
894 addr_flags) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 if (!ift || IS_ERR(ift)) {
896 in6_ifa_put(ifp);
897 in6_dev_put(idev);
898 printk(KERN_INFO
899 "ipv6_create_tempaddr(): retry temporary address regeneration.\n");
900 tmpaddr = &addr;
901 write_lock(&idev->lock);
902 goto retry;
903 }
904
905 spin_lock_bh(&ift->lock);
906 ift->ifpub = ifp;
907 ift->valid_lft = tmp_valid_lft;
908 ift->prefered_lft = tmp_prefered_lft;
909 ift->cstamp = tmp_cstamp;
910 ift->tstamp = tmp_tstamp;
911 spin_unlock_bh(&ift->lock);
912
913 addrconf_dad_start(ift, 0);
914 in6_ifa_put(ift);
915 in6_dev_put(idev);
916out:
917 return ret;
918}
919#endif
920
921/*
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800922 * Choose an appropriate source address (RFC3484)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 */
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +0900924enum {
925 IPV6_SADDR_RULE_INIT = 0,
926 IPV6_SADDR_RULE_LOCAL,
927 IPV6_SADDR_RULE_SCOPE,
928 IPV6_SADDR_RULE_PREFERRED,
929#ifdef CONFIG_IPV6_MIP6
930 IPV6_SADDR_RULE_HOA,
931#endif
932 IPV6_SADDR_RULE_OIF,
933 IPV6_SADDR_RULE_LABEL,
934#ifdef CONFIG_IPV6_PRIVACY
935 IPV6_SADDR_RULE_PRIVACY,
936#endif
937 IPV6_SADDR_RULE_ORCHID,
938 IPV6_SADDR_RULE_PREFIX,
939 IPV6_SADDR_RULE_MAX
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800940};
941
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +0900942struct ipv6_saddr_score {
943 int rule;
944 int addr_type;
945 struct inet6_ifaddr *ifa;
946 DECLARE_BITMAP(scorebits, IPV6_SADDR_RULE_MAX);
947 int scopedist;
948 int matchlen;
949};
950
951struct ipv6_saddr_dst {
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +0900952 const struct in6_addr *addr;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +0900953 int ifindex;
954 int scope;
955 int label;
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +0900956 unsigned int prefs;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +0900957};
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800958
Dave Jonesb6f99a22007-03-22 12:27:49 -0700959static inline int ipv6_saddr_preferred(int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960{
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800961 if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|
962 IPV6_ADDR_LOOPBACK|IPV6_ADDR_RESERVED))
963 return 1;
964 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965}
966
Benjamin Thery3de23252008-05-28 14:51:24 +0200967static int ipv6_get_saddr_eval(struct net *net,
968 struct ipv6_saddr_score *score,
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +0900969 struct ipv6_saddr_dst *dst,
970 int i)
971{
972 int ret;
973
974 if (i <= score->rule) {
975 switch (i) {
976 case IPV6_SADDR_RULE_SCOPE:
977 ret = score->scopedist;
978 break;
979 case IPV6_SADDR_RULE_PREFIX:
980 ret = score->matchlen;
981 break;
982 default:
983 ret = !!test_bit(i, score->scorebits);
984 }
985 goto out;
986 }
987
988 switch (i) {
989 case IPV6_SADDR_RULE_INIT:
990 /* Rule 0: remember if hiscore is not ready yet */
991 ret = !!score->ifa;
992 break;
993 case IPV6_SADDR_RULE_LOCAL:
994 /* Rule 1: Prefer same address */
995 ret = ipv6_addr_equal(&score->ifa->addr, dst->addr);
996 break;
997 case IPV6_SADDR_RULE_SCOPE:
998 /* Rule 2: Prefer appropriate scope
999 *
1000 * ret
1001 * ^
1002 * -1 | d 15
1003 * ---+--+-+---> scope
1004 * |
1005 * | d is scope of the destination.
1006 * B-d | \
1007 * | \ <- smaller scope is better if
1008 * B-15 | \ if scope is enough for destinaion.
1009 * | ret = B - scope (-1 <= scope >= d <= 15).
1010 * d-C-1 | /
1011 * |/ <- greater is better
1012 * -C / if scope is not enough for destination.
1013 * /| ret = scope - C (-1 <= d < scope <= 15).
1014 *
1015 * d - C - 1 < B -15 (for all -1 <= d <= 15).
1016 * C > d + 14 - B >= 15 + 14 - B = 29 - B.
1017 * Assume B = 0 and we get C > 29.
1018 */
1019 ret = __ipv6_addr_src_scope(score->addr_type);
1020 if (ret >= dst->scope)
1021 ret = -ret;
1022 else
1023 ret -= 128; /* 30 is enough */
1024 score->scopedist = ret;
1025 break;
1026 case IPV6_SADDR_RULE_PREFERRED:
1027 /* Rule 3: Avoid deprecated and optimistic addresses */
1028 ret = ipv6_saddr_preferred(score->addr_type) ||
1029 !(score->ifa->flags & (IFA_F_DEPRECATED|IFA_F_OPTIMISTIC));
1030 break;
1031#ifdef CONFIG_IPV6_MIP6
1032 case IPV6_SADDR_RULE_HOA:
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001033 {
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001034 /* Rule 4: Prefer home address */
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001035 int prefhome = !(dst->prefs & IPV6_PREFER_SRC_COA);
1036 ret = !(score->ifa->flags & IFA_F_HOMEADDRESS) ^ prefhome;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001037 break;
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001038 }
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001039#endif
1040 case IPV6_SADDR_RULE_OIF:
1041 /* Rule 5: Prefer outgoing interface */
1042 ret = (!dst->ifindex ||
1043 dst->ifindex == score->ifa->idev->dev->ifindex);
1044 break;
1045 case IPV6_SADDR_RULE_LABEL:
1046 /* Rule 6: Prefer matching label */
Benjamin Thery3de23252008-05-28 14:51:24 +02001047 ret = ipv6_addr_label(net,
1048 &score->ifa->addr, score->addr_type,
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001049 score->ifa->idev->dev->ifindex) == dst->label;
1050 break;
1051#ifdef CONFIG_IPV6_PRIVACY
1052 case IPV6_SADDR_RULE_PRIVACY:
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001053 {
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001054 /* Rule 7: Prefer public address
1055 * Note: prefer temprary address if use_tempaddr >= 2
1056 */
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001057 int preftmp = dst->prefs & (IPV6_PREFER_SRC_PUBLIC|IPV6_PREFER_SRC_TMP) ?
1058 !!(dst->prefs & IPV6_PREFER_SRC_TMP) :
1059 score->ifa->idev->cnf.use_tempaddr >= 2;
1060 ret = (!(score->ifa->flags & IFA_F_TEMPORARY)) ^ preftmp;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001061 break;
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001062 }
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001063#endif
1064 case IPV6_SADDR_RULE_ORCHID:
1065 /* Rule 8-: Prefer ORCHID vs ORCHID or
1066 * non-ORCHID vs non-ORCHID
1067 */
1068 ret = !(ipv6_addr_orchid(&score->ifa->addr) ^
1069 ipv6_addr_orchid(dst->addr));
1070 break;
1071 case IPV6_SADDR_RULE_PREFIX:
1072 /* Rule 8: Use longest matching prefix */
1073 score->matchlen = ret = ipv6_addr_diff(&score->ifa->addr,
1074 dst->addr);
1075 break;
1076 default:
1077 ret = 0;
1078 }
1079
1080 if (ret)
1081 __set_bit(i, score->scorebits);
1082 score->rule = i;
1083out:
1084 return ret;
1085}
1086
1087int ipv6_dev_get_saddr(struct net_device *dst_dev,
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +09001088 const struct in6_addr *daddr, unsigned int prefs,
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001089 struct in6_addr *saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090{
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001091 struct ipv6_saddr_score scores[2],
1092 *score = &scores[0], *hiscore = &scores[1];
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001093 struct net *net = dev_net(dst_dev);
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001094 struct ipv6_saddr_dst dst;
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001095 struct net_device *dev;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001096 int dst_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001098 dst_type = __ipv6_addr_type(daddr);
1099 dst.addr = daddr;
1100 dst.ifindex = dst_dev ? dst_dev->ifindex : 0;
1101 dst.scope = __ipv6_addr_src_scope(dst_type);
Benjamin Thery3de23252008-05-28 14:51:24 +02001102 dst.label = ipv6_addr_label(net, daddr, dst_type, dst.ifindex);
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001103 dst.prefs = prefs;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001104
1105 hiscore->rule = -1;
1106 hiscore->ifa = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
1108 read_lock(&dev_base_lock);
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001109 rcu_read_lock();
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001110
Benjamin Thery6fda7352008-03-05 10:47:47 -08001111 for_each_netdev(net, dev) {
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001112 struct inet6_dev *idev;
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001113
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001114 /* Candidate Source Address (section 4)
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001115 * - multicast and link-local destination address,
1116 * the set of candidate source address MUST only
1117 * include addresses assigned to interfaces
1118 * belonging to the same link as the outgoing
1119 * interface.
1120 * (- For site-local destination addresses, the
1121 * set of candidate source addresses MUST only
1122 * include addresses assigned to interfaces
1123 * belonging to the same site as the outgoing
1124 * interface.)
1125 */
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001126 if (((dst_type & IPV6_ADDR_MULTICAST) ||
1127 dst.scope <= IPV6_ADDR_SCOPE_LINKLOCAL) &&
1128 dst.ifindex && dev->ifindex != dst.ifindex)
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001129 continue;
1130
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 idev = __in6_dev_get(dev);
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001132 if (!idev)
1133 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001135 read_lock_bh(&idev->lock);
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001136 for (score->ifa = idev->addr_list; score->ifa; score->ifa = score->ifa->if_next) {
1137 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001139 /*
YOSHIFUJI Hideaki6b3ae802005-12-21 22:58:01 +09001140 * - Tentative Address (RFC2462 section 5.4)
1141 * - A tentative address is not considered
1142 * "assigned to an interface" in the traditional
Neil Horman95c385b2007-04-25 17:08:10 -07001143 * sense, unless it is also flagged as optimistic.
YOSHIFUJI Hideaki6b3ae802005-12-21 22:58:01 +09001144 * - Candidate Source Address (section 4)
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001145 * - In any case, anycast addresses, multicast
1146 * addresses, and the unspecified address MUST
1147 * NOT be included in a candidate set.
1148 */
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001149 if ((score->ifa->flags & IFA_F_TENTATIVE) &&
1150 (!(score->ifa->flags & IFA_F_OPTIMISTIC)))
YOSHIFUJI Hideaki6b3ae802005-12-21 22:58:01 +09001151 continue;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001152
1153 score->addr_type = __ipv6_addr_type(&score->ifa->addr);
1154
1155 if (unlikely(score->addr_type == IPV6_ADDR_ANY ||
1156 score->addr_type & IPV6_ADDR_MULTICAST)) {
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001157 LIMIT_NETDEBUG(KERN_DEBUG
Joe Perches3b6d8212007-11-19 23:47:25 -08001158 "ADDRCONF: unspecified / multicast address "
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001159 "assigned as unicast address on %s",
1160 dev->name);
1161 continue;
1162 }
1163
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001164 score->rule = -1;
1165 bitmap_zero(score->scorebits, IPV6_SADDR_RULE_MAX);
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001166
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001167 for (i = 0; i < IPV6_SADDR_RULE_MAX; i++) {
1168 int minihiscore, miniscore;
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001169
Benjamin Thery3de23252008-05-28 14:51:24 +02001170 minihiscore = ipv6_get_saddr_eval(net, hiscore, &dst, i);
1171 miniscore = ipv6_get_saddr_eval(net, score, &dst, i);
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001172
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001173 if (minihiscore > miniscore) {
1174 if (i == IPV6_SADDR_RULE_SCOPE &&
1175 score->scopedist > 0) {
1176 /*
1177 * special case:
1178 * each remaining entry
1179 * has too small (not enough)
1180 * scope, because ifa entries
1181 * are sorted by their scope
1182 * values.
1183 */
1184 goto try_nextdev;
1185 }
1186 break;
1187 } else if (minihiscore < miniscore) {
1188 struct ipv6_saddr_score *tmp;
1189
1190 if (hiscore->ifa)
1191 in6_ifa_put(hiscore->ifa);
1192
1193 in6_ifa_hold(score->ifa);
1194
1195 tmp = hiscore;
1196 hiscore = score;
1197 score = tmp;
1198
1199 /* restore our iterator */
1200 score->ifa = hiscore->ifa;
1201
1202 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 }
1204 }
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001205 }
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001206try_nextdev:
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001207 read_unlock_bh(&idev->lock);
1208 }
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001209 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 read_unlock(&dev_base_lock);
1211
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001212 if (!hiscore->ifa)
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001213 return -EADDRNOTAVAIL;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001214
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001215 ipv6_addr_copy(saddr, &hiscore->ifa->addr);
1216 in6_ifa_put(hiscore->ifa);
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001217 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218}
1219
YOSHIFUJI Hideaki5e5f3f02008-03-03 21:44:34 +09001220EXPORT_SYMBOL(ipv6_dev_get_saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
Neil Horman95c385b2007-04-25 17:08:10 -07001222int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr,
1223 unsigned char banned_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224{
1225 struct inet6_dev *idev;
1226 int err = -EADDRNOTAVAIL;
1227
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001228 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 if ((idev = __in6_dev_get(dev)) != NULL) {
1230 struct inet6_ifaddr *ifp;
1231
1232 read_lock_bh(&idev->lock);
1233 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
Neil Horman95c385b2007-04-25 17:08:10 -07001234 if (ifp->scope == IFA_LINK && !(ifp->flags & banned_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 ipv6_addr_copy(addr, &ifp->addr);
1236 err = 0;
1237 break;
1238 }
1239 }
1240 read_unlock_bh(&idev->lock);
1241 }
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001242 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 return err;
1244}
1245
1246static int ipv6_count_addresses(struct inet6_dev *idev)
1247{
1248 int cnt = 0;
1249 struct inet6_ifaddr *ifp;
1250
1251 read_lock_bh(&idev->lock);
1252 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next)
1253 cnt++;
1254 read_unlock_bh(&idev->lock);
1255 return cnt;
1256}
1257
Daniel Lezcanobfeade02008-01-10 22:43:18 -08001258int ipv6_chk_addr(struct net *net, struct in6_addr *addr,
1259 struct net_device *dev, int strict)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260{
1261 struct inet6_ifaddr * ifp;
1262 u8 hash = ipv6_addr_hash(addr);
1263
1264 read_lock_bh(&addrconf_hash_lock);
1265 for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09001266 if (!net_eq(dev_net(ifp->idev->dev), net))
Daniel Lezcanobfeade02008-01-10 22:43:18 -08001267 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 if (ipv6_addr_equal(&ifp->addr, addr) &&
1269 !(ifp->flags&IFA_F_TENTATIVE)) {
1270 if (dev == NULL || ifp->idev->dev == dev ||
1271 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))
1272 break;
1273 }
1274 }
1275 read_unlock_bh(&addrconf_hash_lock);
1276 return ifp != NULL;
1277}
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +09001278EXPORT_SYMBOL(ipv6_chk_addr);
1279
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280static
Daniel Lezcano06bfe652008-01-10 22:43:42 -08001281int ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
1282 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283{
1284 struct inet6_ifaddr * ifp;
1285 u8 hash = ipv6_addr_hash(addr);
1286
1287 for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09001288 if (!net_eq(dev_net(ifp->idev->dev), net))
Daniel Lezcano06bfe652008-01-10 22:43:42 -08001289 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 if (ipv6_addr_equal(&ifp->addr, addr)) {
1291 if (dev == NULL || ifp->idev->dev == dev)
1292 break;
1293 }
1294 }
1295 return ifp != NULL;
1296}
1297
YOSHIFUJI Hideaki52eeeb82008-03-15 22:54:23 -04001298int ipv6_chk_prefix(struct in6_addr *addr, struct net_device *dev)
1299{
1300 struct inet6_dev *idev;
1301 struct inet6_ifaddr *ifa;
1302 int onlink;
1303
1304 onlink = 0;
1305 rcu_read_lock();
1306 idev = __in6_dev_get(dev);
1307 if (idev) {
1308 read_lock_bh(&idev->lock);
1309 for (ifa = idev->addr_list; ifa; ifa = ifa->if_next) {
1310 onlink = ipv6_prefix_equal(addr, &ifa->addr,
1311 ifa->prefix_len);
1312 if (onlink)
1313 break;
1314 }
1315 read_unlock_bh(&idev->lock);
1316 }
1317 rcu_read_unlock();
1318 return onlink;
1319}
1320
1321EXPORT_SYMBOL(ipv6_chk_prefix);
1322
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +09001323struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, const struct in6_addr *addr,
Daniel Lezcano1cab3da2008-01-10 22:44:09 -08001324 struct net_device *dev, int strict)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325{
1326 struct inet6_ifaddr * ifp;
1327 u8 hash = ipv6_addr_hash(addr);
1328
1329 read_lock_bh(&addrconf_hash_lock);
1330 for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09001331 if (!net_eq(dev_net(ifp->idev->dev), net))
Daniel Lezcano1cab3da2008-01-10 22:44:09 -08001332 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 if (ipv6_addr_equal(&ifp->addr, addr)) {
1334 if (dev == NULL || ifp->idev->dev == dev ||
1335 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
1336 in6_ifa_hold(ifp);
1337 break;
1338 }
1339 }
1340 }
1341 read_unlock_bh(&addrconf_hash_lock);
1342
1343 return ifp;
1344}
1345
1346int ipv6_rcv_saddr_equal(const struct sock *sk, const struct sock *sk2)
1347{
1348 const struct in6_addr *sk_rcv_saddr6 = &inet6_sk(sk)->rcv_saddr;
Arnaldo Carvalho de Melo0fa1a532005-12-13 23:23:09 -08001349 const struct in6_addr *sk2_rcv_saddr6 = inet6_rcv_saddr(sk2);
Al Viro82103232006-09-27 18:44:10 -07001350 __be32 sk_rcv_saddr = inet_sk(sk)->rcv_saddr;
1351 __be32 sk2_rcv_saddr = inet_rcv_saddr(sk2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 int sk_ipv6only = ipv6_only_sock(sk);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001353 int sk2_ipv6only = inet_v6_ipv6only(sk2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 int addr_type = ipv6_addr_type(sk_rcv_saddr6);
1355 int addr_type2 = sk2_rcv_saddr6 ? ipv6_addr_type(sk2_rcv_saddr6) : IPV6_ADDR_MAPPED;
1356
1357 if (!sk2_rcv_saddr && !sk_ipv6only)
1358 return 1;
1359
1360 if (addr_type2 == IPV6_ADDR_ANY &&
1361 !(sk2_ipv6only && addr_type == IPV6_ADDR_MAPPED))
1362 return 1;
1363
1364 if (addr_type == IPV6_ADDR_ANY &&
1365 !(sk_ipv6only && addr_type2 == IPV6_ADDR_MAPPED))
1366 return 1;
1367
1368 if (sk2_rcv_saddr6 &&
1369 ipv6_addr_equal(sk_rcv_saddr6, sk2_rcv_saddr6))
1370 return 1;
1371
1372 if (addr_type == IPV6_ADDR_MAPPED &&
1373 !sk2_ipv6only &&
1374 (!sk2_rcv_saddr || !sk_rcv_saddr || sk_rcv_saddr == sk2_rcv_saddr))
1375 return 1;
1376
1377 return 0;
1378}
1379
1380/* Gets referenced address, destroys ifaddr */
1381
Adrian Bunk9f5336e2006-01-07 13:24:25 -08001382static void addrconf_dad_stop(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 if (ifp->flags&IFA_F_PERMANENT) {
1385 spin_lock_bh(&ifp->lock);
1386 addrconf_del_timer(ifp);
1387 ifp->flags |= IFA_F_TENTATIVE;
1388 spin_unlock_bh(&ifp->lock);
1389 in6_ifa_put(ifp);
1390#ifdef CONFIG_IPV6_PRIVACY
1391 } else if (ifp->flags&IFA_F_TEMPORARY) {
1392 struct inet6_ifaddr *ifpub;
1393 spin_lock_bh(&ifp->lock);
1394 ifpub = ifp->ifpub;
1395 if (ifpub) {
1396 in6_ifa_hold(ifpub);
1397 spin_unlock_bh(&ifp->lock);
1398 ipv6_create_tempaddr(ifpub, ifp);
1399 in6_ifa_put(ifpub);
1400 } else {
1401 spin_unlock_bh(&ifp->lock);
1402 }
1403 ipv6_del_addr(ifp);
1404#endif
1405 } else
1406 ipv6_del_addr(ifp);
1407}
1408
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09001409void addrconf_dad_failure(struct inet6_ifaddr *ifp)
1410{
1411 if (net_ratelimit())
1412 printk(KERN_INFO "%s: duplicate address detected!\n", ifp->idev->dev->name);
1413 addrconf_dad_stop(ifp);
1414}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
1416/* Join to solicited addr multicast group. */
1417
1418void addrconf_join_solict(struct net_device *dev, struct in6_addr *addr)
1419{
1420 struct in6_addr maddr;
1421
1422 if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1423 return;
1424
1425 addrconf_addr_solict_mult(addr, &maddr);
1426 ipv6_dev_mc_inc(dev, &maddr);
1427}
1428
1429void addrconf_leave_solict(struct inet6_dev *idev, struct in6_addr *addr)
1430{
1431 struct in6_addr maddr;
1432
1433 if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1434 return;
1435
1436 addrconf_addr_solict_mult(addr, &maddr);
1437 __ipv6_dev_mc_dec(idev, &maddr);
1438}
1439
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03001440static void addrconf_join_anycast(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441{
1442 struct in6_addr addr;
1443 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1444 if (ipv6_addr_any(&addr))
1445 return;
1446 ipv6_dev_ac_inc(ifp->idev->dev, &addr);
1447}
1448
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03001449static void addrconf_leave_anycast(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450{
1451 struct in6_addr addr;
1452 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1453 if (ipv6_addr_any(&addr))
1454 return;
1455 __ipv6_dev_ac_dec(ifp->idev, &addr);
1456}
1457
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08001458static int addrconf_ifid_eui48(u8 *eui, struct net_device *dev)
1459{
1460 if (dev->addr_len != ETH_ALEN)
1461 return -1;
1462 memcpy(eui, dev->dev_addr, 3);
1463 memcpy(eui + 5, dev->dev_addr + 3, 3);
1464
1465 /*
1466 * The zSeries OSA network cards can be shared among various
1467 * OS instances, but the OSA cards have only one MAC address.
1468 * This leads to duplicate address conflicts in conjunction
1469 * with IPv6 if more than one instance uses the same card.
1470 *
1471 * The driver for these cards can deliver a unique 16-bit
1472 * identifier for each instance sharing the same card. It is
1473 * placed instead of 0xFFFE in the interface identifier. The
1474 * "u" bit of the interface identifier is not inverted in this
1475 * case. Hence the resulting interface identifier has local
1476 * scope according to RFC2373.
1477 */
1478 if (dev->dev_id) {
1479 eui[3] = (dev->dev_id >> 8) & 0xFF;
1480 eui[4] = dev->dev_id & 0xFF;
1481 } else {
1482 eui[3] = 0xFF;
1483 eui[4] = 0xFE;
1484 eui[0] ^= 2;
1485 }
1486 return 0;
1487}
1488
1489static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev)
1490{
1491 /* XXX: inherit EUI-64 from other interface -- yoshfuji */
1492 if (dev->addr_len != ARCNET_ALEN)
1493 return -1;
1494 memset(eui, 0, 7);
1495 eui[7] = *(u8*)dev->dev_addr;
1496 return 0;
1497}
1498
1499static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev)
1500{
1501 if (dev->addr_len != INFINIBAND_ALEN)
1502 return -1;
1503 memcpy(eui, dev->dev_addr + 12, 8);
1504 eui[0] |= 2;
1505 return 0;
1506}
1507
YOSHIFUJI Hideakidfd982b2008-04-10 15:42:09 +09001508int __ipv6_isatap_ifid(u8 *eui, __be32 addr)
1509{
1510 eui[0] = (ipv4_is_zeronet(addr) || ipv4_is_private_10(addr) ||
1511 ipv4_is_loopback(addr) || ipv4_is_linklocal_169(addr) ||
1512 ipv4_is_private_172(addr) || ipv4_is_test_192(addr) ||
1513 ipv4_is_anycast_6to4(addr) || ipv4_is_private_192(addr) ||
1514 ipv4_is_test_198(addr) || ipv4_is_multicast(addr) ||
1515 ipv4_is_lbcast(addr)) ? 0x00 : 0x02;
1516 eui[1] = 0;
1517 eui[2] = 0x5E;
1518 eui[3] = 0xFE;
1519 memcpy(eui + 4, &addr, 4);
1520 return 0;
1521}
1522EXPORT_SYMBOL(__ipv6_isatap_ifid);
1523
1524static int addrconf_ifid_sit(u8 *eui, struct net_device *dev)
1525{
1526 if (dev->priv_flags & IFF_ISATAP)
1527 return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
1528 return -1;
1529}
1530
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
1532{
1533 switch (dev->type) {
1534 case ARPHRD_ETHER:
1535 case ARPHRD_FDDI:
1536 case ARPHRD_IEEE802_TR:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08001537 return addrconf_ifid_eui48(eui, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 case ARPHRD_ARCNET:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08001539 return addrconf_ifid_arcnet(eui, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 case ARPHRD_INFINIBAND:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08001541 return addrconf_ifid_infiniband(eui, dev);
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11001542 case ARPHRD_SIT:
YOSHIFUJI Hideakidfd982b2008-04-10 15:42:09 +09001543 return addrconf_ifid_sit(eui, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 }
1545 return -1;
1546}
1547
1548static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
1549{
1550 int err = -1;
1551 struct inet6_ifaddr *ifp;
1552
1553 read_lock_bh(&idev->lock);
1554 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
1555 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
1556 memcpy(eui, ifp->addr.s6_addr+8, 8);
1557 err = 0;
1558 break;
1559 }
1560 }
1561 read_unlock_bh(&idev->lock);
1562 return err;
1563}
1564
1565#ifdef CONFIG_IPV6_PRIVACY
1566/* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
1567static int __ipv6_regen_rndid(struct inet6_dev *idev)
1568{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569regen:
YOSHIFUJI Hideaki955189e2006-03-20 16:54:09 -08001570 get_random_bytes(idev->rndid, sizeof(idev->rndid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 idev->rndid[0] &= ~0x02;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
1573 /*
1574 * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
1575 * check if generated address is not inappropriate
1576 *
1577 * - Reserved subnet anycast (RFC 2526)
1578 * 11111101 11....11 1xxxxxxx
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11001579 * - ISATAP (RFC4214) 6.1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 * 00-00-5E-FE-xx-xx-xx-xx
1581 * - value 0
1582 * - XXX: already assigned to an address on the device
1583 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001584 if (idev->rndid[0] == 0xfd &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff &&
1586 (idev->rndid[7]&0x80))
1587 goto regen;
1588 if ((idev->rndid[0]|idev->rndid[1]) == 0) {
1589 if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe)
1590 goto regen;
1591 if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00)
1592 goto regen;
1593 }
1594
1595 return 0;
1596}
1597
1598static void ipv6_regen_rndid(unsigned long data)
1599{
1600 struct inet6_dev *idev = (struct inet6_dev *) data;
1601 unsigned long expires;
1602
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001603 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 write_lock_bh(&idev->lock);
1605
1606 if (idev->dead)
1607 goto out;
1608
1609 if (__ipv6_regen_rndid(idev) < 0)
1610 goto out;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001611
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 expires = jiffies +
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001613 idev->cnf.temp_prefered_lft * HZ -
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 idev->cnf.regen_max_retry * idev->cnf.dad_transmits * idev->nd_parms->retrans_time - desync_factor;
1615 if (time_before(expires, jiffies)) {
1616 printk(KERN_WARNING
1617 "ipv6_regen_rndid(): too short regeneration interval; timer disabled for %s.\n",
1618 idev->dev->name);
1619 goto out;
1620 }
1621
1622 if (!mod_timer(&idev->regen_timer, expires))
1623 in6_dev_hold(idev);
1624
1625out:
1626 write_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001627 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 in6_dev_put(idev);
1629}
1630
1631static int __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr) {
1632 int ret = 0;
1633
1634 if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0)
1635 ret = __ipv6_regen_rndid(idev);
1636 return ret;
1637}
1638#endif
1639
1640/*
1641 * Add prefix route.
1642 */
1643
1644static void
1645addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev,
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -07001646 unsigned long expires, u32 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647{
Thomas Graf86872cb2006-08-22 00:01:08 -07001648 struct fib6_config cfg = {
1649 .fc_table = RT6_TABLE_PREFIX,
1650 .fc_metric = IP6_RT_PRIO_ADDRCONF,
1651 .fc_ifindex = dev->ifindex,
1652 .fc_expires = expires,
1653 .fc_dst_len = plen,
1654 .fc_flags = RTF_UP | flags,
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001655 .fc_nlinfo.nl_net = dev_net(dev),
Thomas Graf86872cb2006-08-22 00:01:08 -07001656 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657
Thomas Graf86872cb2006-08-22 00:01:08 -07001658 ipv6_addr_copy(&cfg.fc_dst, pfx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659
1660 /* Prevent useless cloning on PtP SIT.
1661 This thing is done here expecting that the whole
1662 class of non-broadcast devices need not cloning.
1663 */
Joerg Roedel0be669b2006-10-10 14:49:53 -07001664#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
Thomas Graf86872cb2006-08-22 00:01:08 -07001665 if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT))
1666 cfg.fc_flags |= RTF_NONEXTHOP;
Joerg Roedel0be669b2006-10-10 14:49:53 -07001667#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668
Thomas Graf86872cb2006-08-22 00:01:08 -07001669 ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670}
1671
1672/* Create "default" multicast route to the interface */
1673
1674static void addrconf_add_mroute(struct net_device *dev)
1675{
Thomas Graf86872cb2006-08-22 00:01:08 -07001676 struct fib6_config cfg = {
1677 .fc_table = RT6_TABLE_LOCAL,
1678 .fc_metric = IP6_RT_PRIO_ADDRCONF,
1679 .fc_ifindex = dev->ifindex,
1680 .fc_dst_len = 8,
1681 .fc_flags = RTF_UP,
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001682 .fc_nlinfo.nl_net = dev_net(dev),
Thomas Graf86872cb2006-08-22 00:01:08 -07001683 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684
Thomas Graf86872cb2006-08-22 00:01:08 -07001685 ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0);
1686
1687 ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688}
1689
Joerg Roedel0be669b2006-10-10 14:49:53 -07001690#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691static void sit_route_add(struct net_device *dev)
1692{
Thomas Graf86872cb2006-08-22 00:01:08 -07001693 struct fib6_config cfg = {
1694 .fc_table = RT6_TABLE_MAIN,
1695 .fc_metric = IP6_RT_PRIO_ADDRCONF,
1696 .fc_ifindex = dev->ifindex,
1697 .fc_dst_len = 96,
1698 .fc_flags = RTF_UP | RTF_NONEXTHOP,
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001699 .fc_nlinfo.nl_net = dev_net(dev),
Thomas Graf86872cb2006-08-22 00:01:08 -07001700 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701
1702 /* prefix length - 96 bits "::d.d.d.d" */
Thomas Graf86872cb2006-08-22 00:01:08 -07001703 ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704}
Joerg Roedel0be669b2006-10-10 14:49:53 -07001705#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706
1707static void addrconf_add_lroute(struct net_device *dev)
1708{
1709 struct in6_addr addr;
1710
1711 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
1712 addrconf_prefix_route(&addr, 64, dev, 0, 0);
1713}
1714
1715static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
1716{
1717 struct inet6_dev *idev;
1718
1719 ASSERT_RTNL();
1720
1721 if ((idev = ipv6_find_idev(dev)) == NULL)
1722 return NULL;
1723
1724 /* Add default multicast route */
1725 addrconf_add_mroute(dev);
1726
1727 /* Add link local route */
1728 addrconf_add_lroute(dev);
1729 return idev;
1730}
1731
1732void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len)
1733{
1734 struct prefix_info *pinfo;
1735 __u32 valid_lft;
1736 __u32 prefered_lft;
1737 int addr_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 struct inet6_dev *in6_dev;
1739
1740 pinfo = (struct prefix_info *) opt;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001741
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 if (len < sizeof(struct prefix_info)) {
1743 ADBG(("addrconf: prefix option too short\n"));
1744 return;
1745 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001746
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 /*
1748 * Validation checks ([ADDRCONF], page 19)
1749 */
1750
1751 addr_type = ipv6_addr_type(&pinfo->prefix);
1752
1753 if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
1754 return;
1755
1756 valid_lft = ntohl(pinfo->valid);
1757 prefered_lft = ntohl(pinfo->prefered);
1758
1759 if (prefered_lft > valid_lft) {
1760 if (net_ratelimit())
1761 printk(KERN_WARNING "addrconf: prefix option has invalid lifetime\n");
1762 return;
1763 }
1764
1765 in6_dev = in6_dev_get(dev);
1766
1767 if (in6_dev == NULL) {
1768 if (net_ratelimit())
1769 printk(KERN_DEBUG "addrconf: device %s not configured\n", dev->name);
1770 return;
1771 }
1772
1773 /*
1774 * Two things going on here:
1775 * 1) Add routes for on-link prefixes
1776 * 2) Configure prefixes with the auto flag set
1777 */
1778
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09001779 if (pinfo->onlink) {
1780 struct rt6_info *rt;
1781 unsigned long rt_expires;
1782
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07001783 /* Avoid arithmetic overflow. Really, we could
1784 * save rt_expires in seconds, likely valid_lft,
1785 * but it would require division in fib gc, that it
1786 * not good.
1787 */
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09001788 if (HZ > USER_HZ)
1789 rt_expires = addrconf_timeout_fixup(valid_lft, HZ);
1790 else
1791 rt_expires = addrconf_timeout_fixup(valid_lft, USER_HZ);
YOSHIFUJI Hideaki3dd4bc62005-12-19 14:02:45 -08001792
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09001793 if (addrconf_finite_timeout(rt_expires))
1794 rt_expires *= HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001796 rt = rt6_lookup(dev_net(dev), &pinfo->prefix, NULL,
Benjamin Thery6fda7352008-03-05 10:47:47 -08001797 dev->ifindex, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798
YOSHIFUJI Hideaki2b5ead42008-05-13 01:16:24 +09001799 if (rt && addrconf_is_prefix_route(rt)) {
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07001800 /* Autoconf prefix route */
1801 if (valid_lft == 0) {
1802 ip6_del_rt(rt);
1803 rt = NULL;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09001804 } else if (addrconf_finite_timeout(rt_expires)) {
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07001805 /* not infinity */
1806 rt->rt6i_expires = jiffies + rt_expires;
1807 rt->rt6i_flags |= RTF_EXPIRES;
1808 } else {
1809 rt->rt6i_flags &= ~RTF_EXPIRES;
1810 rt->rt6i_expires = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 }
1812 } else if (valid_lft) {
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07001813 clock_t expires = 0;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09001814 int flags = RTF_ADDRCONF | RTF_PREFIX_RT;
1815 if (addrconf_finite_timeout(rt_expires)) {
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07001816 /* not infinity */
1817 flags |= RTF_EXPIRES;
1818 expires = jiffies_to_clock_t(rt_expires);
1819 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07001821 dev, expires, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 }
1823 if (rt)
1824 dst_release(&rt->u.dst);
1825 }
1826
1827 /* Try to figure out our local address for this prefix */
1828
1829 if (pinfo->autoconf && in6_dev->cnf.autoconf) {
1830 struct inet6_ifaddr * ifp;
1831 struct in6_addr addr;
1832 int create = 0, update_lft = 0;
1833
1834 if (pinfo->prefix_len == 64) {
1835 memcpy(&addr, &pinfo->prefix, 8);
1836 if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
1837 ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
1838 in6_dev_put(in6_dev);
1839 return;
1840 }
1841 goto ok;
1842 }
1843 if (net_ratelimit())
1844 printk(KERN_DEBUG "IPv6 addrconf: prefix with wrong length %d\n",
1845 pinfo->prefix_len);
1846 in6_dev_put(in6_dev);
1847 return;
1848
1849ok:
1850
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001851 ifp = ipv6_get_ifaddr(dev_net(dev), &addr, dev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852
1853 if (ifp == NULL && valid_lft) {
1854 int max_addresses = in6_dev->cnf.max_addresses;
Neil Horman95c385b2007-04-25 17:08:10 -07001855 u32 addr_flags = 0;
1856
1857#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1858 if (in6_dev->cnf.optimistic_dad &&
1859 !ipv6_devconf.forwarding)
1860 addr_flags = IFA_F_OPTIMISTIC;
1861#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862
1863 /* Do not allow to create too much of autoconfigured
1864 * addresses; this would be too easy way to crash kernel.
1865 */
1866 if (!max_addresses ||
1867 ipv6_count_addresses(in6_dev) < max_addresses)
1868 ifp = ipv6_add_addr(in6_dev, &addr, pinfo->prefix_len,
Neil Horman95c385b2007-04-25 17:08:10 -07001869 addr_type&IPV6_ADDR_SCOPE_MASK,
1870 addr_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871
1872 if (!ifp || IS_ERR(ifp)) {
1873 in6_dev_put(in6_dev);
1874 return;
1875 }
1876
1877 update_lft = create = 1;
1878 ifp->cstamp = jiffies;
1879 addrconf_dad_start(ifp, RTF_ADDRCONF|RTF_PREFIX_RT);
1880 }
1881
1882 if (ifp) {
1883 int flags;
1884 unsigned long now;
1885#ifdef CONFIG_IPV6_PRIVACY
1886 struct inet6_ifaddr *ift;
1887#endif
1888 u32 stored_lft;
1889
1890 /* update lifetime (RFC2462 5.5.3 e) */
1891 spin_lock(&ifp->lock);
1892 now = jiffies;
1893 if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
1894 stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
1895 else
1896 stored_lft = 0;
1897 if (!update_lft && stored_lft) {
1898 if (valid_lft > MIN_VALID_LIFETIME ||
1899 valid_lft > stored_lft)
1900 update_lft = 1;
1901 else if (stored_lft <= MIN_VALID_LIFETIME) {
1902 /* valid_lft <= stored_lft is always true */
1903 /* XXX: IPsec */
1904 update_lft = 0;
1905 } else {
1906 valid_lft = MIN_VALID_LIFETIME;
1907 if (valid_lft < prefered_lft)
1908 prefered_lft = valid_lft;
1909 update_lft = 1;
1910 }
1911 }
1912
1913 if (update_lft) {
1914 ifp->valid_lft = valid_lft;
1915 ifp->prefered_lft = prefered_lft;
1916 ifp->tstamp = now;
1917 flags = ifp->flags;
1918 ifp->flags &= ~IFA_F_DEPRECATED;
1919 spin_unlock(&ifp->lock);
1920
1921 if (!(flags&IFA_F_TENTATIVE))
1922 ipv6_ifa_notify(0, ifp);
1923 } else
1924 spin_unlock(&ifp->lock);
1925
1926#ifdef CONFIG_IPV6_PRIVACY
1927 read_lock_bh(&in6_dev->lock);
1928 /* update all temporary addresses in the list */
1929 for (ift=in6_dev->tempaddr_list; ift; ift=ift->tmp_next) {
1930 /*
1931 * When adjusting the lifetimes of an existing
1932 * temporary address, only lower the lifetimes.
1933 * Implementations must not increase the
1934 * lifetimes of an existing temporary address
1935 * when processing a Prefix Information Option.
1936 */
Benoit Boissinotc6fbfac2008-04-02 00:00:58 -07001937 if (ifp != ift->ifpub)
1938 continue;
1939
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 spin_lock(&ift->lock);
1941 flags = ift->flags;
1942 if (ift->valid_lft > valid_lft &&
1943 ift->valid_lft - valid_lft > (jiffies - ift->tstamp) / HZ)
1944 ift->valid_lft = valid_lft + (jiffies - ift->tstamp) / HZ;
1945 if (ift->prefered_lft > prefered_lft &&
1946 ift->prefered_lft - prefered_lft > (jiffies - ift->tstamp) / HZ)
1947 ift->prefered_lft = prefered_lft + (jiffies - ift->tstamp) / HZ;
1948 spin_unlock(&ift->lock);
1949 if (!(flags&IFA_F_TENTATIVE))
1950 ipv6_ifa_notify(0, ift);
1951 }
1952
1953 if (create && in6_dev->cnf.use_tempaddr > 0) {
1954 /*
1955 * When a new public address is created as described in [ADDRCONF],
1956 * also create a new temporary address.
1957 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001958 read_unlock_bh(&in6_dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 ipv6_create_tempaddr(ifp, NULL);
1960 } else {
1961 read_unlock_bh(&in6_dev->lock);
1962 }
1963#endif
1964 in6_ifa_put(ifp);
1965 addrconf_verify(0);
1966 }
1967 }
1968 inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
1969 in6_dev_put(in6_dev);
1970}
1971
1972/*
1973 * Set destination address.
1974 * Special case for SIT interfaces where we create a new "virtual"
1975 * device.
1976 */
Daniel Lezcanoaf284932008-03-05 10:46:57 -08001977int addrconf_set_dstaddr(struct net *net, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978{
1979 struct in6_ifreq ireq;
1980 struct net_device *dev;
1981 int err = -EINVAL;
1982
1983 rtnl_lock();
1984
1985 err = -EFAULT;
1986 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
1987 goto err_exit;
1988
Daniel Lezcanoaf284932008-03-05 10:46:57 -08001989 dev = __dev_get_by_index(net, ireq.ifr6_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990
1991 err = -ENODEV;
1992 if (dev == NULL)
1993 goto err_exit;
1994
Joerg Roedel0be669b2006-10-10 14:49:53 -07001995#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 if (dev->type == ARPHRD_SIT) {
1997 struct ifreq ifr;
1998 mm_segment_t oldfs;
1999 struct ip_tunnel_parm p;
2000
2001 err = -EADDRNOTAVAIL;
2002 if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4))
2003 goto err_exit;
2004
2005 memset(&p, 0, sizeof(p));
2006 p.iph.daddr = ireq.ifr6_addr.s6_addr32[3];
2007 p.iph.saddr = 0;
2008 p.iph.version = 4;
2009 p.iph.ihl = 5;
2010 p.iph.protocol = IPPROTO_IPV6;
2011 p.iph.ttl = 64;
Stephen Hemmingerd20b3102008-01-21 00:48:43 -08002012 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013
2014 oldfs = get_fs(); set_fs(KERNEL_DS);
2015 err = dev->do_ioctl(dev, &ifr, SIOCADDTUNNEL);
2016 set_fs(oldfs);
2017
2018 if (err == 0) {
2019 err = -ENOBUFS;
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002020 dev = __dev_get_by_name(net, p.name);
2021 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 goto err_exit;
2023 err = dev_open(dev);
2024 }
2025 }
Joerg Roedel0be669b2006-10-10 14:49:53 -07002026#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027
2028err_exit:
2029 rtnl_unlock();
2030 return err;
2031}
2032
2033/*
2034 * Manual configuration of address on an interface
2035 */
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002036static int inet6_addr_add(struct net *net, int ifindex, struct in6_addr *pfx,
Thomas Graf24ef0da2008-05-28 16:54:22 +02002037 unsigned int plen, __u8 ifa_flags, __u32 prefered_lft,
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002038 __u32 valid_lft)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039{
2040 struct inet6_ifaddr *ifp;
2041 struct inet6_dev *idev;
2042 struct net_device *dev;
2043 int scope;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002044 u32 flags;
2045 clock_t expires;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002046 unsigned long timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047
2048 ASSERT_RTNL();
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002049
Thomas Graf24ef0da2008-05-28 16:54:22 +02002050 if (plen > 128)
2051 return -EINVAL;
2052
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002053 /* check the lifetime */
2054 if (!valid_lft || prefered_lft > valid_lft)
2055 return -EINVAL;
2056
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002057 dev = __dev_get_by_index(net, ifindex);
2058 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 return -ENODEV;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002060
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 if ((idev = addrconf_add_dev(dev)) == NULL)
2062 return -ENOBUFS;
2063
2064 scope = ipv6_addr_scope(pfx);
2065
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002066 timeout = addrconf_timeout_fixup(valid_lft, HZ);
2067 if (addrconf_finite_timeout(timeout)) {
2068 expires = jiffies_to_clock_t(timeout * HZ);
2069 valid_lft = timeout;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002070 flags = RTF_EXPIRES;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002071 } else {
2072 expires = 0;
2073 flags = 0;
2074 ifa_flags |= IFA_F_PERMANENT;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002075 }
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002076
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002077 timeout = addrconf_timeout_fixup(prefered_lft, HZ);
2078 if (addrconf_finite_timeout(timeout)) {
2079 if (timeout == 0)
2080 ifa_flags |= IFA_F_DEPRECATED;
2081 prefered_lft = timeout;
2082 }
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002083
2084 ifp = ipv6_add_addr(idev, pfx, plen, scope, ifa_flags);
2085
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 if (!IS_ERR(ifp)) {
Herbert Xu06aebfb2006-08-09 16:52:04 -07002087 spin_lock_bh(&ifp->lock);
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002088 ifp->valid_lft = valid_lft;
2089 ifp->prefered_lft = prefered_lft;
2090 ifp->tstamp = jiffies;
Herbert Xu06aebfb2006-08-09 16:52:04 -07002091 spin_unlock_bh(&ifp->lock);
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002092
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09002093 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev,
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002094 expires, flags);
Neil Horman95c385b2007-04-25 17:08:10 -07002095 /*
2096 * Note that section 3.1 of RFC 4429 indicates
2097 * that the Optimistic flag should not be set for
2098 * manually configured addresses
2099 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 addrconf_dad_start(ifp, 0);
2101 in6_ifa_put(ifp);
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002102 addrconf_verify(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 return 0;
2104 }
2105
2106 return PTR_ERR(ifp);
2107}
2108
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002109static int inet6_addr_del(struct net *net, int ifindex, struct in6_addr *pfx,
Thomas Graf24ef0da2008-05-28 16:54:22 +02002110 unsigned int plen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111{
2112 struct inet6_ifaddr *ifp;
2113 struct inet6_dev *idev;
2114 struct net_device *dev;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002115
Thomas Graf24ef0da2008-05-28 16:54:22 +02002116 if (plen > 128)
2117 return -EINVAL;
2118
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002119 dev = __dev_get_by_index(net, ifindex);
2120 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 return -ENODEV;
2122
2123 if ((idev = __in6_dev_get(dev)) == NULL)
2124 return -ENXIO;
2125
2126 read_lock_bh(&idev->lock);
2127 for (ifp = idev->addr_list; ifp; ifp=ifp->if_next) {
2128 if (ifp->prefix_len == plen &&
2129 ipv6_addr_equal(pfx, &ifp->addr)) {
2130 in6_ifa_hold(ifp);
2131 read_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002132
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 ipv6_del_addr(ifp);
2134
2135 /* If the last address is deleted administratively,
2136 disable IPv6 on this interface.
2137 */
2138 if (idev->addr_list == NULL)
2139 addrconf_ifdown(idev->dev, 1);
2140 return 0;
2141 }
2142 }
2143 read_unlock_bh(&idev->lock);
2144 return -EADDRNOTAVAIL;
2145}
2146
2147
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002148int addrconf_add_ifaddr(struct net *net, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149{
2150 struct in6_ifreq ireq;
2151 int err;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002152
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 if (!capable(CAP_NET_ADMIN))
2154 return -EPERM;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002155
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2157 return -EFAULT;
2158
2159 rtnl_lock();
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002160 err = inet6_addr_add(net, ireq.ifr6_ifindex, &ireq.ifr6_addr,
2161 ireq.ifr6_prefixlen, IFA_F_PERMANENT,
2162 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 rtnl_unlock();
2164 return err;
2165}
2166
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002167int addrconf_del_ifaddr(struct net *net, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168{
2169 struct in6_ifreq ireq;
2170 int err;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002171
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 if (!capable(CAP_NET_ADMIN))
2173 return -EPERM;
2174
2175 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2176 return -EFAULT;
2177
2178 rtnl_lock();
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002179 err = inet6_addr_del(net, ireq.ifr6_ifindex, &ireq.ifr6_addr,
2180 ireq.ifr6_prefixlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 rtnl_unlock();
2182 return err;
2183}
2184
Joerg Roedel0be669b2006-10-10 14:49:53 -07002185#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186static void sit_add_v4_addrs(struct inet6_dev *idev)
2187{
2188 struct inet6_ifaddr * ifp;
2189 struct in6_addr addr;
2190 struct net_device *dev;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002191 struct net *net = dev_net(idev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 int scope;
2193
2194 ASSERT_RTNL();
2195
2196 memset(&addr, 0, sizeof(struct in6_addr));
2197 memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
2198
2199 if (idev->dev->flags&IFF_POINTOPOINT) {
2200 addr.s6_addr32[0] = htonl(0xfe800000);
2201 scope = IFA_LINK;
2202 } else {
2203 scope = IPV6_ADDR_COMPATv4;
2204 }
2205
2206 if (addr.s6_addr32[3]) {
2207 ifp = ipv6_add_addr(idev, &addr, 128, scope, IFA_F_PERMANENT);
2208 if (!IS_ERR(ifp)) {
2209 spin_lock_bh(&ifp->lock);
2210 ifp->flags &= ~IFA_F_TENTATIVE;
2211 spin_unlock_bh(&ifp->lock);
2212 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2213 in6_ifa_put(ifp);
2214 }
2215 return;
2216 }
2217
Benjamin Thery6fda7352008-03-05 10:47:47 -08002218 for_each_netdev(net, dev) {
Herbert Xue5ed6392005-10-03 14:35:55 -07002219 struct in_device * in_dev = __in_dev_get_rtnl(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 if (in_dev && (dev->flags & IFF_UP)) {
2221 struct in_ifaddr * ifa;
2222
2223 int flag = scope;
2224
2225 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
2226 int plen;
2227
2228 addr.s6_addr32[3] = ifa->ifa_local;
2229
2230 if (ifa->ifa_scope == RT_SCOPE_LINK)
2231 continue;
2232 if (ifa->ifa_scope >= RT_SCOPE_HOST) {
2233 if (idev->dev->flags&IFF_POINTOPOINT)
2234 continue;
2235 flag |= IFA_HOST;
2236 }
2237 if (idev->dev->flags&IFF_POINTOPOINT)
2238 plen = 64;
2239 else
2240 plen = 96;
2241
2242 ifp = ipv6_add_addr(idev, &addr, plen, flag,
2243 IFA_F_PERMANENT);
2244 if (!IS_ERR(ifp)) {
2245 spin_lock_bh(&ifp->lock);
2246 ifp->flags &= ~IFA_F_TENTATIVE;
2247 spin_unlock_bh(&ifp->lock);
2248 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2249 in6_ifa_put(ifp);
2250 }
2251 }
2252 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002253 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254}
Joerg Roedel0be669b2006-10-10 14:49:53 -07002255#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256
2257static void init_loopback(struct net_device *dev)
2258{
2259 struct inet6_dev *idev;
2260 struct inet6_ifaddr * ifp;
2261
2262 /* ::1 */
2263
2264 ASSERT_RTNL();
2265
2266 if ((idev = ipv6_find_idev(dev)) == NULL) {
2267 printk(KERN_DEBUG "init loopback: add_dev failed\n");
2268 return;
2269 }
2270
2271 ifp = ipv6_add_addr(idev, &in6addr_loopback, 128, IFA_HOST, IFA_F_PERMANENT);
2272 if (!IS_ERR(ifp)) {
2273 spin_lock_bh(&ifp->lock);
2274 ifp->flags &= ~IFA_F_TENTATIVE;
2275 spin_unlock_bh(&ifp->lock);
2276 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2277 in6_ifa_put(ifp);
2278 }
2279}
2280
2281static void addrconf_add_linklocal(struct inet6_dev *idev, struct in6_addr *addr)
2282{
2283 struct inet6_ifaddr * ifp;
Neil Horman95c385b2007-04-25 17:08:10 -07002284 u32 addr_flags = IFA_F_PERMANENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285
Neil Horman95c385b2007-04-25 17:08:10 -07002286#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
2287 if (idev->cnf.optimistic_dad &&
2288 !ipv6_devconf.forwarding)
2289 addr_flags |= IFA_F_OPTIMISTIC;
2290#endif
2291
2292
2293 ifp = ipv6_add_addr(idev, addr, 64, IFA_LINK, addr_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 if (!IS_ERR(ifp)) {
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09002295 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, idev->dev, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 addrconf_dad_start(ifp, 0);
2297 in6_ifa_put(ifp);
2298 }
2299}
2300
2301static void addrconf_dev_config(struct net_device *dev)
2302{
2303 struct in6_addr addr;
2304 struct inet6_dev * idev;
2305
2306 ASSERT_RTNL();
2307
Herbert Xu74235a22007-06-14 13:02:55 -07002308 if ((dev->type != ARPHRD_ETHER) &&
2309 (dev->type != ARPHRD_FDDI) &&
2310 (dev->type != ARPHRD_IEEE802_TR) &&
2311 (dev->type != ARPHRD_ARCNET) &&
2312 (dev->type != ARPHRD_INFINIBAND)) {
2313 /* Alas, we support only Ethernet autoconfiguration. */
2314 return;
2315 }
2316
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 idev = addrconf_add_dev(dev);
2318 if (idev == NULL)
2319 return;
2320
2321 memset(&addr, 0, sizeof(struct in6_addr));
2322 addr.s6_addr32[0] = htonl(0xFE800000);
2323
2324 if (ipv6_generate_eui64(addr.s6_addr + 8, dev) == 0)
2325 addrconf_add_linklocal(idev, &addr);
2326}
2327
Joerg Roedel0be669b2006-10-10 14:49:53 -07002328#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329static void addrconf_sit_config(struct net_device *dev)
2330{
2331 struct inet6_dev *idev;
2332
2333 ASSERT_RTNL();
2334
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002335 /*
2336 * Configure the tunnel with one of our IPv4
2337 * addresses... we should configure all of
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 * our v4 addrs in the tunnel
2339 */
2340
2341 if ((idev = ipv6_find_idev(dev)) == NULL) {
2342 printk(KERN_DEBUG "init sit: add_dev failed\n");
2343 return;
2344 }
2345
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11002346 if (dev->priv_flags & IFF_ISATAP) {
2347 struct in6_addr addr;
2348
2349 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
2350 addrconf_prefix_route(&addr, 64, dev, 0, 0);
2351 if (!ipv6_generate_eui64(addr.s6_addr + 8, dev))
2352 addrconf_add_linklocal(idev, &addr);
2353 return;
2354 }
2355
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 sit_add_v4_addrs(idev);
2357
2358 if (dev->flags&IFF_POINTOPOINT) {
2359 addrconf_add_mroute(dev);
2360 addrconf_add_lroute(dev);
2361 } else
2362 sit_route_add(dev);
2363}
Joerg Roedel0be669b2006-10-10 14:49:53 -07002364#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365
2366static inline int
2367ipv6_inherit_linklocal(struct inet6_dev *idev, struct net_device *link_dev)
2368{
2369 struct in6_addr lladdr;
2370
Neil Horman95c385b2007-04-25 17:08:10 -07002371 if (!ipv6_get_lladdr(link_dev, &lladdr, IFA_F_TENTATIVE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 addrconf_add_linklocal(idev, &lladdr);
2373 return 0;
2374 }
2375 return -1;
2376}
2377
2378static void ip6_tnl_add_linklocal(struct inet6_dev *idev)
2379{
2380 struct net_device *link_dev;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002381 struct net *net = dev_net(idev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382
2383 /* first try to inherit the link-local address from the link device */
2384 if (idev->dev->iflink &&
Benjamin Thery6fda7352008-03-05 10:47:47 -08002385 (link_dev = __dev_get_by_index(net, idev->dev->iflink))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 if (!ipv6_inherit_linklocal(idev, link_dev))
2387 return;
2388 }
2389 /* then try to inherit it from any device */
Benjamin Thery6fda7352008-03-05 10:47:47 -08002390 for_each_netdev(net, link_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 if (!ipv6_inherit_linklocal(idev, link_dev))
2392 return;
2393 }
2394 printk(KERN_DEBUG "init ip6-ip6: add_linklocal failed\n");
2395}
2396
2397/*
2398 * Autoconfigure tunnel with a link-local address so routing protocols,
2399 * DHCPv6, MLD etc. can be run over the virtual link
2400 */
2401
2402static void addrconf_ip6_tnl_config(struct net_device *dev)
2403{
2404 struct inet6_dev *idev;
2405
2406 ASSERT_RTNL();
2407
2408 if ((idev = addrconf_add_dev(dev)) == NULL) {
2409 printk(KERN_DEBUG "init ip6-ip6: add_dev failed\n");
2410 return;
2411 }
2412 ip6_tnl_add_linklocal(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413}
2414
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002415static int addrconf_notify(struct notifier_block *this, unsigned long event,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 void * data)
2417{
2418 struct net_device *dev = (struct net_device *) data;
Herbert Xu74235a22007-06-14 13:02:55 -07002419 struct inet6_dev *idev = __in6_dev_get(dev);
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09002420 int run_pending = 0;
Herbert Xub217d612007-07-30 17:04:52 -07002421 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422
2423 switch(event) {
YOSHIFUJI Hideaki45ba9dd2007-02-15 02:07:27 +09002424 case NETDEV_REGISTER:
Herbert Xu74235a22007-06-14 13:02:55 -07002425 if (!idev && dev->mtu >= IPV6_MIN_MTU) {
YOSHIFUJI Hideaki45ba9dd2007-02-15 02:07:27 +09002426 idev = ipv6_add_dev(dev);
2427 if (!idev)
Herbert Xub217d612007-07-30 17:04:52 -07002428 return notifier_from_errno(-ENOMEM);
YOSHIFUJI Hideaki45ba9dd2007-02-15 02:07:27 +09002429 }
2430 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 case NETDEV_UP:
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002432 case NETDEV_CHANGE:
Jay Vosburghc2edacf2007-07-09 10:42:47 -07002433 if (dev->flags & IFF_SLAVE)
2434 break;
2435
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002436 if (event == NETDEV_UP) {
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -07002437 if (!addrconf_qdisc_ok(dev)) {
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002438 /* device is not ready yet. */
2439 printk(KERN_INFO
2440 "ADDRCONF(NETDEV_UP): %s: "
2441 "link is not ready\n",
2442 dev->name);
2443 break;
2444 }
Kristian Slavov99081042006-02-08 16:10:53 -08002445
Evgeniy Polyakovd31c7b82007-11-30 23:36:08 +11002446 if (!idev && dev->mtu >= IPV6_MIN_MTU)
2447 idev = ipv6_add_dev(dev);
2448
Kristian Slavov99081042006-02-08 16:10:53 -08002449 if (idev)
2450 idev->if_flags |= IF_READY;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002451 } else {
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -07002452 if (!addrconf_qdisc_ok(dev)) {
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002453 /* device is still not ready. */
2454 break;
2455 }
2456
2457 if (idev) {
2458 if (idev->if_flags & IF_READY) {
2459 /* device is already configured. */
2460 break;
2461 }
2462 idev->if_flags |= IF_READY;
2463 }
2464
2465 printk(KERN_INFO
2466 "ADDRCONF(NETDEV_CHANGE): %s: "
2467 "link becomes ready\n",
2468 dev->name);
2469
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09002470 run_pending = 1;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002471 }
2472
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 switch(dev->type) {
Joerg Roedel0be669b2006-10-10 14:49:53 -07002474#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 case ARPHRD_SIT:
2476 addrconf_sit_config(dev);
2477 break;
Joerg Roedel0be669b2006-10-10 14:49:53 -07002478#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 case ARPHRD_TUNNEL6:
2480 addrconf_ip6_tnl_config(dev);
2481 break;
2482 case ARPHRD_LOOPBACK:
2483 init_loopback(dev);
2484 break;
2485
2486 default:
2487 addrconf_dev_config(dev);
2488 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002489 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 if (idev) {
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09002491 if (run_pending)
2492 addrconf_dad_run(idev);
2493
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 /* If the MTU changed during the interface down, when the
2495 interface up, the changed MTU must be reflected in the
2496 idev as well as routers.
2497 */
2498 if (idev->cnf.mtu6 != dev->mtu && dev->mtu >= IPV6_MIN_MTU) {
2499 rt6_mtu_change(dev, dev->mtu);
2500 idev->cnf.mtu6 = dev->mtu;
2501 }
2502 idev->tstamp = jiffies;
2503 inet6_ifinfo_notify(RTM_NEWLINK, idev);
2504 /* If the changed mtu during down is lower than IPV6_MIN_MTU
2505 stop IPv6 on this interface.
2506 */
2507 if (dev->mtu < IPV6_MIN_MTU)
2508 addrconf_ifdown(dev, event != NETDEV_DOWN);
2509 }
2510 break;
2511
2512 case NETDEV_CHANGEMTU:
Evgeniy Polyakovd31c7b82007-11-30 23:36:08 +11002513 if (idev && dev->mtu >= IPV6_MIN_MTU) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 rt6_mtu_change(dev, dev->mtu);
2515 idev->cnf.mtu6 = dev->mtu;
2516 break;
2517 }
2518
Evgeniy Polyakovd31c7b82007-11-30 23:36:08 +11002519 if (!idev && dev->mtu >= IPV6_MIN_MTU) {
2520 idev = ipv6_add_dev(dev);
2521 if (idev)
2522 break;
2523 }
2524
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 /* MTU falled under IPV6_MIN_MTU. Stop IPv6 on this interface. */
2526
2527 case NETDEV_DOWN:
2528 case NETDEV_UNREGISTER:
2529 /*
2530 * Remove all addresses from this interface.
2531 */
2532 addrconf_ifdown(dev, event != NETDEV_DOWN);
2533 break;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002534
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 case NETDEV_CHANGENAME:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 if (idev) {
Stephen Hemminger5632c512007-04-28 21:16:39 -07002537 snmp6_unregister_dev(idev);
Pavel Emelyanov408c4762008-01-10 17:41:21 -08002538 addrconf_sysctl_unregister(idev);
Pavel Emelyanovf52295a2007-12-02 00:58:37 +11002539 addrconf_sysctl_register(idev);
Herbert Xub217d612007-07-30 17:04:52 -07002540 err = snmp6_register_dev(idev);
2541 if (err)
2542 return notifier_from_errno(err);
Stephen Hemminger5632c512007-04-28 21:16:39 -07002543 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002545 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546
2547 return NOTIFY_OK;
2548}
2549
2550/*
2551 * addrconf module should be notified of a device going up
2552 */
2553static struct notifier_block ipv6_dev_notf = {
2554 .notifier_call = addrconf_notify,
2555 .priority = 0
2556};
2557
2558static int addrconf_ifdown(struct net_device *dev, int how)
2559{
2560 struct inet6_dev *idev;
2561 struct inet6_ifaddr *ifa, **bifa;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002562 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 int i;
2564
2565 ASSERT_RTNL();
2566
Denis V. Luneveb867572008-04-03 13:31:53 -07002567 if ((dev->flags & IFF_LOOPBACK) && how == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 how = 0;
2569
Daniel Lezcanof3db4852008-03-03 23:27:06 -08002570 rt6_ifdown(net, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 neigh_ifdown(&nd_tbl, dev);
2572
2573 idev = __in6_dev_get(dev);
2574 if (idev == NULL)
2575 return -ENODEV;
2576
2577 /* Step 1: remove reference to ipv6 device from parent device.
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002578 Do not dev_put!
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 */
Denis V. Lunev439e2382008-04-03 13:30:17 -07002580 if (how) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 idev->dead = 1;
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07002582
2583 /* protected by rtnl_lock */
2584 rcu_assign_pointer(dev->ip6_ptr, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585
2586 /* Step 1.5: remove snmp6 entry */
2587 snmp6_unregister_dev(idev);
2588
2589 }
2590
2591 /* Step 2: clear hash table */
2592 for (i=0; i<IN6_ADDR_HSIZE; i++) {
2593 bifa = &inet6_addr_lst[i];
2594
2595 write_lock_bh(&addrconf_hash_lock);
2596 while ((ifa = *bifa) != NULL) {
2597 if (ifa->idev == idev) {
2598 *bifa = ifa->lst_next;
2599 ifa->lst_next = NULL;
2600 addrconf_del_timer(ifa);
2601 in6_ifa_put(ifa);
2602 continue;
2603 }
2604 bifa = &ifa->lst_next;
2605 }
2606 write_unlock_bh(&addrconf_hash_lock);
2607 }
2608
2609 write_lock_bh(&idev->lock);
2610
2611 /* Step 3: clear flags for stateless addrconf */
Denis V. Lunev439e2382008-04-03 13:30:17 -07002612 if (!how)
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002613 idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614
2615 /* Step 4: clear address list */
2616#ifdef CONFIG_IPV6_PRIVACY
Denis V. Lunev439e2382008-04-03 13:30:17 -07002617 if (how && del_timer(&idev->regen_timer))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 in6_dev_put(idev);
2619
2620 /* clear tempaddr list */
2621 while ((ifa = idev->tempaddr_list) != NULL) {
2622 idev->tempaddr_list = ifa->tmp_next;
2623 ifa->tmp_next = NULL;
2624 ifa->dead = 1;
2625 write_unlock_bh(&idev->lock);
2626 spin_lock_bh(&ifa->lock);
2627
2628 if (ifa->ifpub) {
2629 in6_ifa_put(ifa->ifpub);
2630 ifa->ifpub = NULL;
2631 }
2632 spin_unlock_bh(&ifa->lock);
2633 in6_ifa_put(ifa);
2634 write_lock_bh(&idev->lock);
2635 }
2636#endif
2637 while ((ifa = idev->addr_list) != NULL) {
2638 idev->addr_list = ifa->if_next;
2639 ifa->if_next = NULL;
2640 ifa->dead = 1;
2641 addrconf_del_timer(ifa);
2642 write_unlock_bh(&idev->lock);
2643
2644 __ipv6_ifa_notify(RTM_DELADDR, ifa);
Vlad Yasevich063ed362007-07-15 00:16:35 -07002645 atomic_notifier_call_chain(&inet6addr_chain, NETDEV_DOWN, ifa);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 in6_ifa_put(ifa);
2647
2648 write_lock_bh(&idev->lock);
2649 }
2650 write_unlock_bh(&idev->lock);
2651
2652 /* Step 5: Discard multicast list */
2653
Denis V. Lunev439e2382008-04-03 13:30:17 -07002654 if (how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 ipv6_mc_destroy_dev(idev);
2656 else
2657 ipv6_mc_down(idev);
2658
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 idev->tstamp = jiffies;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002660
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 /* Shot the device (if unregistered) */
2662
Denis V. Lunev439e2382008-04-03 13:30:17 -07002663 if (how) {
Pavel Emelyanov408c4762008-01-10 17:41:21 -08002664 addrconf_sysctl_unregister(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 neigh_parms_release(&nd_tbl, idev->nd_parms);
2666 neigh_ifdown(&nd_tbl, dev);
2667 in6_dev_put(idev);
2668 }
2669 return 0;
2670}
2671
2672static void addrconf_rs_timer(unsigned long data)
2673{
2674 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
2675
2676 if (ifp->idev->cnf.forwarding)
2677 goto out;
2678
2679 if (ifp->idev->if_flags & IF_RA_RCVD) {
2680 /*
2681 * Announcement received after solicitation
2682 * was sent
2683 */
2684 goto out;
2685 }
2686
2687 spin_lock(&ifp->lock);
2688 if (ifp->probes++ < ifp->idev->cnf.rtr_solicits) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 /* The wait after the last probe can be shorter */
2690 addrconf_mod_timer(ifp, AC_RS,
2691 (ifp->probes == ifp->idev->cnf.rtr_solicits) ?
2692 ifp->idev->cnf.rtr_solicit_delay :
2693 ifp->idev->cnf.rtr_solicit_interval);
2694 spin_unlock(&ifp->lock);
2695
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +09002696 ndisc_send_rs(ifp->idev->dev, &ifp->addr, &in6addr_linklocal_allrouters);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 } else {
2698 spin_unlock(&ifp->lock);
2699 /*
2700 * Note: we do not support deprecated "all on-link"
2701 * assumption any longer.
2702 */
2703 printk(KERN_DEBUG "%s: no IPv6 routers present\n",
2704 ifp->idev->dev->name);
2705 }
2706
2707out:
2708 in6_ifa_put(ifp);
2709}
2710
2711/*
2712 * Duplicate Address Detection
2713 */
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002714static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
2715{
2716 unsigned long rand_num;
2717 struct inet6_dev *idev = ifp->idev;
2718
Neil Horman95c385b2007-04-25 17:08:10 -07002719 if (ifp->flags & IFA_F_OPTIMISTIC)
2720 rand_num = 0;
2721 else
2722 rand_num = net_random() % (idev->cnf.rtr_solicit_delay ? : 1);
2723
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002724 ifp->probes = idev->cnf.dad_transmits;
2725 addrconf_mod_timer(ifp, AC_DAD, rand_num);
2726}
2727
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -07002728static void addrconf_dad_start(struct inet6_ifaddr *ifp, u32 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729{
2730 struct inet6_dev *idev = ifp->idev;
2731 struct net_device *dev = idev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732
2733 addrconf_join_solict(dev, &ifp->addr);
2734
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 net_srandom(ifp->addr.s6_addr32[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736
2737 read_lock_bh(&idev->lock);
2738 if (ifp->dead)
2739 goto out;
2740 spin_lock_bh(&ifp->lock);
2741
2742 if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07002743 !(ifp->flags&IFA_F_TENTATIVE) ||
2744 ifp->flags & IFA_F_NODAD) {
Neil Horman95c385b2007-04-25 17:08:10 -07002745 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 spin_unlock_bh(&ifp->lock);
2747 read_unlock_bh(&idev->lock);
2748
2749 addrconf_dad_completed(ifp);
2750 return;
2751 }
2752
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08002753 if (!(idev->if_flags & IF_READY)) {
YOSHIFUJI Hideaki3dd3bf82005-12-23 11:23:21 -08002754 spin_unlock_bh(&ifp->lock);
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08002755 read_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002756 /*
2757 * If the defice is not ready:
2758 * - keep it tentative if it is a permanent address.
2759 * - otherwise, kill it.
2760 */
2761 in6_ifa_hold(ifp);
2762 addrconf_dad_stop(ifp);
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08002763 return;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002764 }
Neil Horman95c385b2007-04-25 17:08:10 -07002765
2766 /*
2767 * Optimistic nodes can start receiving
2768 * Frames right away
2769 */
2770 if(ifp->flags & IFA_F_OPTIMISTIC)
2771 ip6_ins_rt(ifp->rt);
2772
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08002773 addrconf_dad_kick(ifp);
2774 spin_unlock_bh(&ifp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775out:
2776 read_unlock_bh(&idev->lock);
2777}
2778
2779static void addrconf_dad_timer(unsigned long data)
2780{
2781 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
2782 struct inet6_dev *idev = ifp->idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 struct in6_addr mcaddr;
2784
2785 read_lock_bh(&idev->lock);
2786 if (idev->dead) {
2787 read_unlock_bh(&idev->lock);
2788 goto out;
2789 }
2790 spin_lock_bh(&ifp->lock);
2791 if (ifp->probes == 0) {
2792 /*
2793 * DAD was successful
2794 */
2795
Neil Horman95c385b2007-04-25 17:08:10 -07002796 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 spin_unlock_bh(&ifp->lock);
2798 read_unlock_bh(&idev->lock);
2799
2800 addrconf_dad_completed(ifp);
2801
2802 goto out;
2803 }
2804
2805 ifp->probes--;
2806 addrconf_mod_timer(ifp, AC_DAD, ifp->idev->nd_parms->retrans_time);
2807 spin_unlock_bh(&ifp->lock);
2808 read_unlock_bh(&idev->lock);
2809
2810 /* send a neighbour solicitation for our addr */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
YOSHIFUJI Hideakid7aabf22008-04-10 15:42:11 +09002812 ndisc_send_ns(ifp->idev->dev, NULL, &ifp->addr, &mcaddr, &in6addr_any);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813out:
2814 in6_ifa_put(ifp);
2815}
2816
2817static void addrconf_dad_completed(struct inet6_ifaddr *ifp)
2818{
2819 struct net_device * dev = ifp->idev->dev;
2820
2821 /*
2822 * Configure the address for reception. Now it is valid.
2823 */
2824
2825 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2826
2827 /* If added prefix is link local and forwarding is off,
2828 start sending router solicitations.
2829 */
2830
2831 if (ifp->idev->cnf.forwarding == 0 &&
2832 ifp->idev->cnf.rtr_solicits > 0 &&
2833 (dev->flags&IFF_LOOPBACK) == 0 &&
2834 (ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 /*
2836 * If a host as already performed a random delay
2837 * [...] as part of DAD [...] there is no need
2838 * to delay again before sending the first RS
2839 */
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +09002840 ndisc_send_rs(ifp->idev->dev, &ifp->addr, &in6addr_linklocal_allrouters);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841
2842 spin_lock_bh(&ifp->lock);
2843 ifp->probes = 1;
2844 ifp->idev->if_flags |= IF_RS_SENT;
2845 addrconf_mod_timer(ifp, AC_RS, ifp->idev->cnf.rtr_solicit_interval);
2846 spin_unlock_bh(&ifp->lock);
2847 }
2848}
2849
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09002850static void addrconf_dad_run(struct inet6_dev *idev) {
2851 struct inet6_ifaddr *ifp;
2852
2853 read_lock_bh(&idev->lock);
2854 for (ifp = idev->addr_list; ifp; ifp = ifp->if_next) {
2855 spin_lock_bh(&ifp->lock);
2856 if (!(ifp->flags & IFA_F_TENTATIVE)) {
2857 spin_unlock_bh(&ifp->lock);
2858 continue;
2859 }
2860 spin_unlock_bh(&ifp->lock);
2861 addrconf_dad_kick(ifp);
2862 }
2863 read_unlock_bh(&idev->lock);
2864}
2865
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866#ifdef CONFIG_PROC_FS
2867struct if6_iter_state {
Daniel Lezcano3c400902008-01-10 22:42:49 -08002868 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 int bucket;
2870};
2871
2872static struct inet6_ifaddr *if6_get_first(struct seq_file *seq)
2873{
2874 struct inet6_ifaddr *ifa = NULL;
2875 struct if6_iter_state *state = seq->private;
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09002876 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877
2878 for (state->bucket = 0; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
2879 ifa = inet6_addr_lst[state->bucket];
Daniel Lezcano3c400902008-01-10 22:42:49 -08002880
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09002881 while (ifa && !net_eq(dev_net(ifa->idev->dev), net))
Daniel Lezcano3c400902008-01-10 22:42:49 -08002882 ifa = ifa->lst_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 if (ifa)
2884 break;
2885 }
2886 return ifa;
2887}
2888
2889static struct inet6_ifaddr *if6_get_next(struct seq_file *seq, struct inet6_ifaddr *ifa)
2890{
2891 struct if6_iter_state *state = seq->private;
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09002892 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893
2894 ifa = ifa->lst_next;
2895try_again:
Daniel Lezcano3c400902008-01-10 22:42:49 -08002896 if (ifa) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09002897 if (!net_eq(dev_net(ifa->idev->dev), net)) {
Daniel Lezcano3c400902008-01-10 22:42:49 -08002898 ifa = ifa->lst_next;
2899 goto try_again;
2900 }
2901 }
2902
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903 if (!ifa && ++state->bucket < IN6_ADDR_HSIZE) {
2904 ifa = inet6_addr_lst[state->bucket];
2905 goto try_again;
2906 }
Daniel Lezcano3c400902008-01-10 22:42:49 -08002907
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 return ifa;
2909}
2910
2911static struct inet6_ifaddr *if6_get_idx(struct seq_file *seq, loff_t pos)
2912{
2913 struct inet6_ifaddr *ifa = if6_get_first(seq);
2914
2915 if (ifa)
2916 while(pos && (ifa = if6_get_next(seq, ifa)) != NULL)
2917 --pos;
2918 return pos ? NULL : ifa;
2919}
2920
2921static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
Stephen Hemmingerd20b3102008-01-21 00:48:43 -08002922 __acquires(addrconf_hash_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923{
2924 read_lock_bh(&addrconf_hash_lock);
2925 return if6_get_idx(seq, *pos);
2926}
2927
2928static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2929{
2930 struct inet6_ifaddr *ifa;
2931
2932 ifa = if6_get_next(seq, v);
2933 ++*pos;
2934 return ifa;
2935}
2936
2937static void if6_seq_stop(struct seq_file *seq, void *v)
Stephen Hemmingerd20b3102008-01-21 00:48:43 -08002938 __releases(addrconf_hash_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939{
2940 read_unlock_bh(&addrconf_hash_lock);
2941}
2942
2943static int if6_seq_show(struct seq_file *seq, void *v)
2944{
2945 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
2946 seq_printf(seq,
YOSHIFUJI Hideaki9343e792006-01-17 02:10:53 -08002947 NIP6_SEQFMT " %02x %02x %02x %02x %8s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 NIP6(ifp->addr),
2949 ifp->idev->dev->ifindex,
2950 ifp->prefix_len,
2951 ifp->scope,
2952 ifp->flags,
2953 ifp->idev->dev->name);
2954 return 0;
2955}
2956
Philippe De Muyter56b3d972007-07-10 23:07:31 -07002957static const struct seq_operations if6_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 .start = if6_seq_start,
2959 .next = if6_seq_next,
2960 .show = if6_seq_show,
2961 .stop = if6_seq_stop,
2962};
2963
2964static int if6_seq_open(struct inode *inode, struct file *file)
2965{
Daniel Lezcano3c400902008-01-10 22:42:49 -08002966 return seq_open_net(inode, file, &if6_seq_ops,
2967 sizeof(struct if6_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968}
2969
Arjan van de Ven9a321442007-02-12 00:55:35 -08002970static const struct file_operations if6_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 .owner = THIS_MODULE,
2972 .open = if6_seq_open,
2973 .read = seq_read,
2974 .llseek = seq_lseek,
Daniel Lezcano3c400902008-01-10 22:42:49 -08002975 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976};
2977
Daniel Lezcano3c400902008-01-10 22:42:49 -08002978static int if6_proc_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979{
Daniel Lezcano3c400902008-01-10 22:42:49 -08002980 if (!proc_net_fops_create(net, "if_inet6", S_IRUGO, &if6_fops))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 return -ENOMEM;
2982 return 0;
2983}
2984
Daniel Lezcano3c400902008-01-10 22:42:49 -08002985static void if6_proc_net_exit(struct net *net)
2986{
2987 proc_net_remove(net, "if_inet6");
2988}
2989
2990static struct pernet_operations if6_proc_net_ops = {
2991 .init = if6_proc_net_init,
2992 .exit = if6_proc_net_exit,
2993};
2994
2995int __init if6_proc_init(void)
2996{
2997 return register_pernet_subsys(&if6_proc_net_ops);
2998}
2999
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000void if6_proc_exit(void)
3001{
Daniel Lezcano3c400902008-01-10 22:42:49 -08003002 unregister_pernet_subsys(&if6_proc_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003}
3004#endif /* CONFIG_PROC_FS */
3005
Masahide NAKAMURA59fbb3a2007-06-26 23:56:32 -07003006#if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07003007/* Check if address is a home address configured on any interface. */
Daniel Lezcano389f6612008-01-10 22:44:40 -08003008int ipv6_chk_home_addr(struct net *net, struct in6_addr *addr)
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07003009{
3010 int ret = 0;
3011 struct inet6_ifaddr * ifp;
3012 u8 hash = ipv6_addr_hash(addr);
3013 read_lock_bh(&addrconf_hash_lock);
3014 for (ifp = inet6_addr_lst[hash]; ifp; ifp = ifp->lst_next) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09003015 if (!net_eq(dev_net(ifp->idev->dev), net))
Daniel Lezcano389f6612008-01-10 22:44:40 -08003016 continue;
YOSHIFUJI Hideakicaad2952008-04-10 15:42:07 +09003017 if (ipv6_addr_equal(&ifp->addr, addr) &&
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07003018 (ifp->flags & IFA_F_HOMEADDRESS)) {
3019 ret = 1;
3020 break;
3021 }
3022 }
3023 read_unlock_bh(&addrconf_hash_lock);
3024 return ret;
3025}
3026#endif
3027
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028/*
3029 * Periodic address status verification
3030 */
3031
3032static void addrconf_verify(unsigned long foo)
3033{
3034 struct inet6_ifaddr *ifp;
3035 unsigned long now, next;
3036 int i;
3037
3038 spin_lock_bh(&addrconf_verify_lock);
3039 now = jiffies;
3040 next = now + ADDR_CHECK_FREQUENCY;
3041
3042 del_timer(&addr_chk_timer);
3043
3044 for (i=0; i < IN6_ADDR_HSIZE; i++) {
3045
3046restart:
Yan Zheng5d5780d2005-11-20 13:42:20 -08003047 read_lock(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 for (ifp=inet6_addr_lst[i]; ifp; ifp=ifp->lst_next) {
3049 unsigned long age;
3050#ifdef CONFIG_IPV6_PRIVACY
3051 unsigned long regen_advance;
3052#endif
3053
3054 if (ifp->flags & IFA_F_PERMANENT)
3055 continue;
3056
3057 spin_lock(&ifp->lock);
3058 age = (now - ifp->tstamp) / HZ;
3059
3060#ifdef CONFIG_IPV6_PRIVACY
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003061 regen_advance = ifp->idev->cnf.regen_max_retry *
3062 ifp->idev->cnf.dad_transmits *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 ifp->idev->nd_parms->retrans_time / HZ;
3064#endif
3065
YOSHIFUJI Hideaki8f27ebb2006-07-28 18:12:11 +09003066 if (ifp->valid_lft != INFINITY_LIFE_TIME &&
3067 age >= ifp->valid_lft) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 spin_unlock(&ifp->lock);
3069 in6_ifa_hold(ifp);
Yan Zheng5d5780d2005-11-20 13:42:20 -08003070 read_unlock(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 ipv6_del_addr(ifp);
3072 goto restart;
YOSHIFUJI Hideaki8f27ebb2006-07-28 18:12:11 +09003073 } else if (ifp->prefered_lft == INFINITY_LIFE_TIME) {
3074 spin_unlock(&ifp->lock);
3075 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 } else if (age >= ifp->prefered_lft) {
3077 /* jiffies - ifp->tsamp > age >= ifp->prefered_lft */
3078 int deprecate = 0;
3079
3080 if (!(ifp->flags&IFA_F_DEPRECATED)) {
3081 deprecate = 1;
3082 ifp->flags |= IFA_F_DEPRECATED;
3083 }
3084
3085 if (time_before(ifp->tstamp + ifp->valid_lft * HZ, next))
3086 next = ifp->tstamp + ifp->valid_lft * HZ;
3087
3088 spin_unlock(&ifp->lock);
3089
3090 if (deprecate) {
3091 in6_ifa_hold(ifp);
Yan Zheng5d5780d2005-11-20 13:42:20 -08003092 read_unlock(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093
3094 ipv6_ifa_notify(0, ifp);
3095 in6_ifa_put(ifp);
3096 goto restart;
3097 }
3098#ifdef CONFIG_IPV6_PRIVACY
3099 } else if ((ifp->flags&IFA_F_TEMPORARY) &&
3100 !(ifp->flags&IFA_F_TENTATIVE)) {
3101 if (age >= ifp->prefered_lft - regen_advance) {
3102 struct inet6_ifaddr *ifpub = ifp->ifpub;
3103 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
3104 next = ifp->tstamp + ifp->prefered_lft * HZ;
3105 if (!ifp->regen_count && ifpub) {
3106 ifp->regen_count++;
3107 in6_ifa_hold(ifp);
3108 in6_ifa_hold(ifpub);
3109 spin_unlock(&ifp->lock);
Yan Zheng5d5780d2005-11-20 13:42:20 -08003110 read_unlock(&addrconf_hash_lock);
Hiroyuki YAMAMORI291d8092005-12-23 11:24:05 -08003111 spin_lock(&ifpub->lock);
3112 ifpub->regen_count = 0;
3113 spin_unlock(&ifpub->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114 ipv6_create_tempaddr(ifpub, ifp);
3115 in6_ifa_put(ifpub);
3116 in6_ifa_put(ifp);
3117 goto restart;
3118 }
3119 } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
3120 next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
3121 spin_unlock(&ifp->lock);
3122#endif
3123 } else {
3124 /* ifp->prefered_lft <= ifp->valid_lft */
3125 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
3126 next = ifp->tstamp + ifp->prefered_lft * HZ;
3127 spin_unlock(&ifp->lock);
3128 }
3129 }
Yan Zheng5d5780d2005-11-20 13:42:20 -08003130 read_unlock(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 }
3132
3133 addr_chk_timer.expires = time_before(next, jiffies + HZ) ? jiffies + HZ : next;
3134 add_timer(&addr_chk_timer);
3135 spin_unlock_bh(&addrconf_verify_lock);
3136}
3137
Thomas Graf461d8832006-09-18 00:09:49 -07003138static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local)
3139{
3140 struct in6_addr *pfx = NULL;
3141
3142 if (addr)
3143 pfx = nla_data(addr);
3144
3145 if (local) {
3146 if (pfx && nla_memcmp(local, pfx, sizeof(*pfx)))
3147 pfx = NULL;
3148 else
3149 pfx = nla_data(local);
3150 }
3151
3152 return pfx;
3153}
3154
Patrick McHardyef7c79e2007-06-05 12:38:30 -07003155static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = {
Thomas Graf461d8832006-09-18 00:09:49 -07003156 [IFA_ADDRESS] = { .len = sizeof(struct in6_addr) },
3157 [IFA_LOCAL] = { .len = sizeof(struct in6_addr) },
3158 [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) },
3159};
3160
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161static int
3162inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
3163{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09003164 struct net *net = sock_net(skb->sk);
Thomas Grafb933f712006-09-18 00:10:19 -07003165 struct ifaddrmsg *ifm;
3166 struct nlattr *tb[IFA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 struct in6_addr *pfx;
Thomas Grafb933f712006-09-18 00:10:19 -07003168 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169
Thomas Grafb933f712006-09-18 00:10:19 -07003170 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
3171 if (err < 0)
3172 return err;
3173
3174 ifm = nlmsg_data(nlh);
3175 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176 if (pfx == NULL)
3177 return -EINVAL;
3178
Daniel Lezcanoaf284932008-03-05 10:46:57 -08003179 return inet6_addr_del(net, ifm->ifa_index, pfx, ifm->ifa_prefixlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180}
3181
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003182static int inet6_addr_modify(struct inet6_ifaddr *ifp, u8 ifa_flags,
3183 u32 prefered_lft, u32 valid_lft)
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09003184{
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07003185 u32 flags;
3186 clock_t expires;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09003187 unsigned long timeout;
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09003188
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09003189 if (!valid_lft || (prefered_lft > valid_lft))
3190 return -EINVAL;
3191
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09003192 timeout = addrconf_timeout_fixup(valid_lft, HZ);
3193 if (addrconf_finite_timeout(timeout)) {
3194 expires = jiffies_to_clock_t(timeout * HZ);
3195 valid_lft = timeout;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07003196 flags = RTF_EXPIRES;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09003197 } else {
3198 expires = 0;
3199 flags = 0;
3200 ifa_flags |= IFA_F_PERMANENT;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07003201 }
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09003202
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09003203 timeout = addrconf_timeout_fixup(prefered_lft, HZ);
3204 if (addrconf_finite_timeout(timeout)) {
3205 if (timeout == 0)
3206 ifa_flags |= IFA_F_DEPRECATED;
3207 prefered_lft = timeout;
3208 }
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09003209
3210 spin_lock_bh(&ifp->lock);
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07003211 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 +09003212 ifp->tstamp = jiffies;
3213 ifp->valid_lft = valid_lft;
3214 ifp->prefered_lft = prefered_lft;
3215
3216 spin_unlock_bh(&ifp->lock);
3217 if (!(ifp->flags&IFA_F_TENTATIVE))
3218 ipv6_ifa_notify(0, ifp);
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09003219
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09003220 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, ifp->idev->dev,
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07003221 expires, flags);
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09003222 addrconf_verify(0);
3223
3224 return 0;
3225}
3226
3227static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
3229{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09003230 struct net *net = sock_net(skb->sk);
Thomas Graf461d8832006-09-18 00:09:49 -07003231 struct ifaddrmsg *ifm;
3232 struct nlattr *tb[IFA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233 struct in6_addr *pfx;
Thomas Graf7198f8c2006-09-18 00:13:46 -07003234 struct inet6_ifaddr *ifa;
3235 struct net_device *dev;
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003236 u32 valid_lft = INFINITY_LIFE_TIME, preferred_lft = INFINITY_LIFE_TIME;
3237 u8 ifa_flags;
Thomas Graf461d8832006-09-18 00:09:49 -07003238 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239
Thomas Graf461d8832006-09-18 00:09:49 -07003240 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
3241 if (err < 0)
3242 return err;
3243
3244 ifm = nlmsg_data(nlh);
3245 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 if (pfx == NULL)
3247 return -EINVAL;
3248
Thomas Graf461d8832006-09-18 00:09:49 -07003249 if (tb[IFA_CACHEINFO]) {
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09003250 struct ifa_cacheinfo *ci;
Thomas Graf461d8832006-09-18 00:09:49 -07003251
3252 ci = nla_data(tb[IFA_CACHEINFO]);
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09003253 valid_lft = ci->ifa_valid;
Thomas Graf461d8832006-09-18 00:09:49 -07003254 preferred_lft = ci->ifa_prefered;
3255 } else {
3256 preferred_lft = INFINITY_LIFE_TIME;
3257 valid_lft = INFINITY_LIFE_TIME;
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09003258 }
3259
Daniel Lezcanoaf284932008-03-05 10:46:57 -08003260 dev = __dev_get_by_index(net, ifm->ifa_index);
Thomas Graf7198f8c2006-09-18 00:13:46 -07003261 if (dev == NULL)
3262 return -ENODEV;
3263
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003264 /* We ignore other flags so far. */
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07003265 ifa_flags = ifm->ifa_flags & (IFA_F_NODAD | IFA_F_HOMEADDRESS);
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003266
Daniel Lezcano1cab3da2008-01-10 22:44:09 -08003267 ifa = ipv6_get_ifaddr(net, pfx, dev, 1);
Thomas Graf7198f8c2006-09-18 00:13:46 -07003268 if (ifa == NULL) {
3269 /*
3270 * It would be best to check for !NLM_F_CREATE here but
3271 * userspace alreay relies on not having to provide this.
3272 */
Daniel Lezcanoaf284932008-03-05 10:46:57 -08003273 return inet6_addr_add(net, ifm->ifa_index, pfx,
3274 ifm->ifa_prefixlen, ifa_flags,
3275 preferred_lft, valid_lft);
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09003276 }
3277
Thomas Graf7198f8c2006-09-18 00:13:46 -07003278 if (nlh->nlmsg_flags & NLM_F_EXCL ||
3279 !(nlh->nlmsg_flags & NLM_F_REPLACE))
3280 err = -EEXIST;
3281 else
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003282 err = inet6_addr_modify(ifa, ifa_flags, preferred_lft, valid_lft);
Thomas Graf7198f8c2006-09-18 00:13:46 -07003283
3284 in6_ifa_put(ifa);
3285
3286 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287}
3288
Thomas Graf101bb222006-09-18 00:11:52 -07003289static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u8 flags,
3290 u8 scope, int ifindex)
3291{
3292 struct ifaddrmsg *ifm;
3293
3294 ifm = nlmsg_data(nlh);
3295 ifm->ifa_family = AF_INET6;
3296 ifm->ifa_prefixlen = prefixlen;
3297 ifm->ifa_flags = flags;
3298 ifm->ifa_scope = scope;
3299 ifm->ifa_index = ifindex;
3300}
3301
Thomas Graf85486af2006-09-18 00:11:24 -07003302static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
3303 unsigned long tstamp, u32 preferred, u32 valid)
3304{
3305 struct ifa_cacheinfo ci;
3306
3307 ci.cstamp = (u32)(TIME_DELTA(cstamp, INITIAL_JIFFIES) / HZ * 100
3308 + TIME_DELTA(cstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
3309 ci.tstamp = (u32)(TIME_DELTA(tstamp, INITIAL_JIFFIES) / HZ * 100
3310 + TIME_DELTA(tstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
3311 ci.ifa_prefered = preferred;
3312 ci.ifa_valid = valid;
3313
3314 return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
3315}
3316
Thomas Graf101bb222006-09-18 00:11:52 -07003317static inline int rt_scope(int ifa_scope)
3318{
3319 if (ifa_scope & IFA_HOST)
3320 return RT_SCOPE_HOST;
3321 else if (ifa_scope & IFA_LINK)
3322 return RT_SCOPE_LINK;
3323 else if (ifa_scope & IFA_SITE)
3324 return RT_SCOPE_SITE;
3325 else
3326 return RT_SCOPE_UNIVERSE;
3327}
3328
Thomas Graf0ab68032006-09-18 00:12:35 -07003329static inline int inet6_ifaddr_msgsize(void)
3330{
Thomas Graf339bf982006-11-10 14:10:15 -08003331 return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
3332 + nla_total_size(16) /* IFA_ADDRESS */
3333 + nla_total_size(sizeof(struct ifa_cacheinfo));
Thomas Graf0ab68032006-09-18 00:12:35 -07003334}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07003335
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07003337 u32 pid, u32 seq, int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339 struct nlmsghdr *nlh;
Thomas Graf85486af2006-09-18 00:11:24 -07003340 u32 preferred, valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341
Thomas Graf0ab68032006-09-18 00:12:35 -07003342 nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags);
3343 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08003344 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07003345
Thomas Graf101bb222006-09-18 00:11:52 -07003346 put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope),
3347 ifa->idev->dev->ifindex);
3348
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349 if (!(ifa->flags&IFA_F_PERMANENT)) {
Thomas Graf85486af2006-09-18 00:11:24 -07003350 preferred = ifa->prefered_lft;
3351 valid = ifa->valid_lft;
3352 if (preferred != INFINITY_LIFE_TIME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353 long tval = (jiffies - ifa->tstamp)/HZ;
Thomas Graf85486af2006-09-18 00:11:24 -07003354 preferred -= tval;
3355 if (valid != INFINITY_LIFE_TIME)
3356 valid -= tval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 }
3358 } else {
Thomas Graf85486af2006-09-18 00:11:24 -07003359 preferred = INFINITY_LIFE_TIME;
3360 valid = INFINITY_LIFE_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361 }
Thomas Graf85486af2006-09-18 00:11:24 -07003362
Thomas Graf0ab68032006-09-18 00:12:35 -07003363 if (nla_put(skb, IFA_ADDRESS, 16, &ifa->addr) < 0 ||
Patrick McHardy26932562007-01-31 23:16:40 -08003364 put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0) {
3365 nlmsg_cancel(skb, nlh);
3366 return -EMSGSIZE;
3367 }
Thomas Graf85486af2006-09-18 00:11:24 -07003368
Thomas Graf0ab68032006-09-18 00:12:35 -07003369 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370}
3371
3372static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -07003373 u32 pid, u32 seq, int event, u16 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 struct nlmsghdr *nlh;
Thomas Graf101bb222006-09-18 00:11:52 -07003376 u8 scope = RT_SCOPE_UNIVERSE;
3377 int ifindex = ifmca->idev->dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378
Thomas Graf101bb222006-09-18 00:11:52 -07003379 if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE)
3380 scope = RT_SCOPE_SITE;
3381
Thomas Graf0ab68032006-09-18 00:12:35 -07003382 nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags);
3383 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08003384 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07003385
Thomas Graf101bb222006-09-18 00:11:52 -07003386 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
Thomas Graf0ab68032006-09-18 00:12:35 -07003387 if (nla_put(skb, IFA_MULTICAST, 16, &ifmca->mca_addr) < 0 ||
3388 put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp,
Patrick McHardy26932562007-01-31 23:16:40 -08003389 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
3390 nlmsg_cancel(skb, nlh);
3391 return -EMSGSIZE;
3392 }
Thomas Graf85486af2006-09-18 00:11:24 -07003393
Thomas Graf0ab68032006-09-18 00:12:35 -07003394 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395}
3396
3397static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07003398 u32 pid, u32 seq, int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400 struct nlmsghdr *nlh;
Thomas Graf101bb222006-09-18 00:11:52 -07003401 u8 scope = RT_SCOPE_UNIVERSE;
3402 int ifindex = ifaca->aca_idev->dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403
Thomas Graf101bb222006-09-18 00:11:52 -07003404 if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE)
3405 scope = RT_SCOPE_SITE;
3406
Thomas Graf0ab68032006-09-18 00:12:35 -07003407 nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags);
3408 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08003409 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07003410
Thomas Graf101bb222006-09-18 00:11:52 -07003411 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
Thomas Graf0ab68032006-09-18 00:12:35 -07003412 if (nla_put(skb, IFA_ANYCAST, 16, &ifaca->aca_addr) < 0 ||
3413 put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp,
Patrick McHardy26932562007-01-31 23:16:40 -08003414 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
3415 nlmsg_cancel(skb, nlh);
3416 return -EMSGSIZE;
3417 }
Thomas Graf85486af2006-09-18 00:11:24 -07003418
Thomas Graf0ab68032006-09-18 00:12:35 -07003419 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420}
3421
3422enum addr_type_t
3423{
3424 UNICAST_ADDR,
3425 MULTICAST_ADDR,
3426 ANYCAST_ADDR,
3427};
3428
3429static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
3430 enum addr_type_t type)
3431{
3432 int idx, ip_idx;
3433 int s_idx, s_ip_idx;
3434 int err = 1;
3435 struct net_device *dev;
3436 struct inet6_dev *idev = NULL;
3437 struct inet6_ifaddr *ifa;
3438 struct ifmcaddr6 *ifmca;
3439 struct ifacaddr6 *ifaca;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09003440 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441
3442 s_idx = cb->args[0];
3443 s_ip_idx = ip_idx = cb->args[1];
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003444
Pavel Emelianov7562f872007-05-03 15:13:45 -07003445 idx = 0;
Benjamin Thery6fda7352008-03-05 10:47:47 -08003446 for_each_netdev(net, dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447 if (idx < s_idx)
Pavel Emelianov7562f872007-05-03 15:13:45 -07003448 goto cont;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449 if (idx > s_idx)
3450 s_ip_idx = 0;
3451 ip_idx = 0;
3452 if ((idev = in6_dev_get(dev)) == NULL)
Pavel Emelianov7562f872007-05-03 15:13:45 -07003453 goto cont;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454 read_lock_bh(&idev->lock);
3455 switch (type) {
3456 case UNICAST_ADDR:
YOSHIFUJI Hideakiae9cda52005-06-28 13:00:30 -07003457 /* unicast address incl. temp addr */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458 for (ifa = idev->addr_list; ifa;
3459 ifa = ifa->if_next, ip_idx++) {
3460 if (ip_idx < s_ip_idx)
3461 continue;
YOSHIFUJI Hideaki5d5619b2008-01-22 17:29:40 +09003462 err = inet6_fill_ifaddr(skb, ifa,
3463 NETLINK_CB(cb->skb).pid,
3464 cb->nlh->nlmsg_seq,
3465 RTM_NEWADDR,
3466 NLM_F_MULTI);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468 break;
3469 case MULTICAST_ADDR:
3470 /* multicast address */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003471 for (ifmca = idev->mc_list; ifmca;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472 ifmca = ifmca->next, ip_idx++) {
3473 if (ip_idx < s_ip_idx)
3474 continue;
YOSHIFUJI Hideaki5d5619b2008-01-22 17:29:40 +09003475 err = inet6_fill_ifmcaddr(skb, ifmca,
3476 NETLINK_CB(cb->skb).pid,
3477 cb->nlh->nlmsg_seq,
3478 RTM_GETMULTICAST,
3479 NLM_F_MULTI);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480 }
3481 break;
3482 case ANYCAST_ADDR:
3483 /* anycast address */
3484 for (ifaca = idev->ac_list; ifaca;
3485 ifaca = ifaca->aca_next, ip_idx++) {
3486 if (ip_idx < s_ip_idx)
3487 continue;
YOSHIFUJI Hideaki5d5619b2008-01-22 17:29:40 +09003488 err = inet6_fill_ifacaddr(skb, ifaca,
3489 NETLINK_CB(cb->skb).pid,
3490 cb->nlh->nlmsg_seq,
3491 RTM_GETANYCAST,
3492 NLM_F_MULTI);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493 }
3494 break;
3495 default:
3496 break;
3497 }
3498 read_unlock_bh(&idev->lock);
3499 in6_dev_put(idev);
YOSHIFUJI Hideaki5d5619b2008-01-22 17:29:40 +09003500
3501 if (err <= 0)
3502 break;
Pavel Emelianov7562f872007-05-03 15:13:45 -07003503cont:
3504 idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506 cb->args[0] = idx;
3507 cb->args[1] = ip_idx;
3508 return skb->len;
3509}
3510
3511static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
3512{
3513 enum addr_type_t type = UNICAST_ADDR;
Denis V. Lunevb8542722007-12-01 00:21:31 +11003514
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515 return inet6_dump_addr(skb, cb, type);
3516}
3517
3518static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
3519{
3520 enum addr_type_t type = MULTICAST_ADDR;
Denis V. Lunevb8542722007-12-01 00:21:31 +11003521
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522 return inet6_dump_addr(skb, cb, type);
3523}
3524
3525
3526static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
3527{
3528 enum addr_type_t type = ANYCAST_ADDR;
Denis V. Lunevb8542722007-12-01 00:21:31 +11003529
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530 return inet6_dump_addr(skb, cb, type);
3531}
3532
Thomas Graf1b29fc22006-09-18 00:10:50 -07003533static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr* nlh,
3534 void *arg)
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003535{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09003536 struct net *net = sock_net(in_skb->sk);
Thomas Graf1b29fc22006-09-18 00:10:50 -07003537 struct ifaddrmsg *ifm;
3538 struct nlattr *tb[IFA_MAX+1];
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003539 struct in6_addr *addr = NULL;
3540 struct net_device *dev = NULL;
3541 struct inet6_ifaddr *ifa;
3542 struct sk_buff *skb;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003543 int err;
3544
Thomas Graf1b29fc22006-09-18 00:10:50 -07003545 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
3546 if (err < 0)
3547 goto errout;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003548
Thomas Graf1b29fc22006-09-18 00:10:50 -07003549 addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
3550 if (addr == NULL) {
3551 err = -EINVAL;
3552 goto errout;
3553 }
3554
3555 ifm = nlmsg_data(nlh);
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003556 if (ifm->ifa_index)
Benjamin Thery6fda7352008-03-05 10:47:47 -08003557 dev = __dev_get_by_index(net, ifm->ifa_index);
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003558
Daniel Lezcano1cab3da2008-01-10 22:44:09 -08003559 if ((ifa = ipv6_get_ifaddr(net, addr, dev, 1)) == NULL) {
Thomas Graf1b29fc22006-09-18 00:10:50 -07003560 err = -EADDRNOTAVAIL;
3561 goto errout;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003562 }
3563
Thomas Graf0ab68032006-09-18 00:12:35 -07003564 if ((skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL)) == NULL) {
Thomas Graf1b29fc22006-09-18 00:10:50 -07003565 err = -ENOBUFS;
3566 goto errout_ifa;
3567 }
3568
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003569 err = inet6_fill_ifaddr(skb, ifa, NETLINK_CB(in_skb).pid,
3570 nlh->nlmsg_seq, RTM_NEWADDR, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08003571 if (err < 0) {
3572 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
3573 WARN_ON(err == -EMSGSIZE);
3574 kfree_skb(skb);
3575 goto errout_ifa;
3576 }
Benjamin Thery6fda7352008-03-05 10:47:47 -08003577 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).pid);
Thomas Graf1b29fc22006-09-18 00:10:50 -07003578errout_ifa:
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003579 in6_ifa_put(ifa);
Thomas Graf1b29fc22006-09-18 00:10:50 -07003580errout:
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003581 return err;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003582}
3583
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
3585{
3586 struct sk_buff *skb;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09003587 struct net *net = dev_net(ifa->idev->dev);
Thomas Graf5d620262006-08-15 00:35:02 -07003588 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589
Thomas Graf0ab68032006-09-18 00:12:35 -07003590 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC);
Thomas Graf5d620262006-08-15 00:35:02 -07003591 if (skb == NULL)
3592 goto errout;
3593
3594 err = inet6_fill_ifaddr(skb, ifa, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08003595 if (err < 0) {
3596 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
3597 WARN_ON(err == -EMSGSIZE);
3598 kfree_skb(skb);
3599 goto errout;
3600 }
Benjamin Thery6fda7352008-03-05 10:47:47 -08003601 err = rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
Thomas Graf5d620262006-08-15 00:35:02 -07003602errout:
3603 if (err < 0)
Benjamin Thery6fda7352008-03-05 10:47:47 -08003604 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605}
3606
Dave Jonesb6f99a22007-03-22 12:27:49 -07003607static inline void ipv6_store_devconf(struct ipv6_devconf *cnf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608 __s32 *array, int bytes)
3609{
Thomas Graf04561c12006-11-14 19:53:58 -08003610 BUG_ON(bytes < (DEVCONF_MAX * 4));
3611
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612 memset(array, 0, bytes);
3613 array[DEVCONF_FORWARDING] = cnf->forwarding;
3614 array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
3615 array[DEVCONF_MTU6] = cnf->mtu6;
3616 array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
3617 array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
3618 array[DEVCONF_AUTOCONF] = cnf->autoconf;
3619 array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
3620 array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
3621 array[DEVCONF_RTR_SOLICIT_INTERVAL] = cnf->rtr_solicit_interval;
3622 array[DEVCONF_RTR_SOLICIT_DELAY] = cnf->rtr_solicit_delay;
3623 array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
3624#ifdef CONFIG_IPV6_PRIVACY
3625 array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
3626 array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
3627 array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
3628 array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
3629 array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
3630#endif
3631 array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -08003632 array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr;
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -08003633 array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo;
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08003634#ifdef CONFIG_IPV6_ROUTER_PREF
3635 array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref;
YOSHIFUJI Hideaki52e16352006-03-20 17:05:47 -08003636 array[DEVCONF_RTR_PROBE_INTERVAL] = cnf->rtr_probe_interval;
Neil Hormanfa03ef32007-01-30 14:30:10 -08003637#ifdef CONFIG_IPV6_ROUTE_INFO
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -08003638 array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen;
3639#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08003640#endif
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -07003641 array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp;
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -07003642 array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route;
Neil Horman95c385b2007-04-25 17:08:10 -07003643#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
3644 array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad;
3645#endif
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09003646#ifdef CONFIG_IPV6_MROUTE
3647 array[DEVCONF_MC_FORWARDING] = cnf->mc_forwarding;
3648#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649}
3650
Thomas Graf339bf982006-11-10 14:10:15 -08003651static inline size_t inet6_if_nlmsg_size(void)
3652{
3653 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
3654 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
3655 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
3656 + nla_total_size(4) /* IFLA_MTU */
3657 + nla_total_size(4) /* IFLA_LINK */
3658 + nla_total_size( /* IFLA_PROTINFO */
3659 nla_total_size(4) /* IFLA_INET6_FLAGS */
3660 + nla_total_size(sizeof(struct ifla_cacheinfo))
3661 + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07003662 + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */
3663 + nla_total_size(ICMP6_MIB_MAX * 8) /* IFLA_INET6_ICMP6STATS */
Thomas Graf339bf982006-11-10 14:10:15 -08003664 );
3665}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07003666
Herbert Xu7f7d9a62007-04-24 21:54:09 -07003667static inline void __snmp6_fill_stats(u64 *stats, void **mib, int items,
3668 int bytes)
3669{
3670 int i;
3671 int pad = bytes - sizeof(u64) * items;
3672 BUG_ON(pad < 0);
3673
3674 /* Use put_unaligned() because stats may not be aligned for u64. */
3675 put_unaligned(items, &stats[0]);
3676 for (i = 1; i < items; i++)
3677 put_unaligned(snmp_fold_field(mib, i), &stats[i]);
3678
3679 memset(&stats[items], 0, pad);
3680}
3681
3682static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype,
3683 int bytes)
3684{
3685 switch(attrtype) {
3686 case IFLA_INET6_STATS:
3687 __snmp6_fill_stats(stats, (void **)idev->stats.ipv6, IPSTATS_MIB_MAX, bytes);
3688 break;
3689 case IFLA_INET6_ICMP6STATS:
3690 __snmp6_fill_stats(stats, (void **)idev->stats.icmpv6, ICMP6_MIB_MAX, bytes);
3691 break;
3692 }
3693}
3694
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003695static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07003696 u32 pid, u32 seq, int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697{
Thomas Graf04561c12006-11-14 19:53:58 -08003698 struct net_device *dev = idev->dev;
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07003699 struct nlattr *nla;
Thomas Graf04561c12006-11-14 19:53:58 -08003700 struct ifinfomsg *hdr;
3701 struct nlmsghdr *nlh;
3702 void *protoinfo;
3703 struct ifla_cacheinfo ci;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704
Thomas Graf04561c12006-11-14 19:53:58 -08003705 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*hdr), flags);
3706 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08003707 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708
Thomas Graf04561c12006-11-14 19:53:58 -08003709 hdr = nlmsg_data(nlh);
3710 hdr->ifi_family = AF_INET6;
3711 hdr->__ifi_pad = 0;
3712 hdr->ifi_type = dev->type;
3713 hdr->ifi_index = dev->ifindex;
3714 hdr->ifi_flags = dev_get_flags(dev);
3715 hdr->ifi_change = 0;
3716
3717 NLA_PUT_STRING(skb, IFLA_IFNAME, dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718
3719 if (dev->addr_len)
Thomas Graf04561c12006-11-14 19:53:58 -08003720 NLA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721
Thomas Graf04561c12006-11-14 19:53:58 -08003722 NLA_PUT_U32(skb, IFLA_MTU, dev->mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723 if (dev->ifindex != dev->iflink)
Thomas Graf04561c12006-11-14 19:53:58 -08003724 NLA_PUT_U32(skb, IFLA_LINK, dev->iflink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725
Thomas Graf04561c12006-11-14 19:53:58 -08003726 protoinfo = nla_nest_start(skb, IFLA_PROTINFO);
3727 if (protoinfo == NULL)
3728 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729
Thomas Graf04561c12006-11-14 19:53:58 -08003730 NLA_PUT_U32(skb, IFLA_INET6_FLAGS, idev->if_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732 ci.max_reasm_len = IPV6_MAXPLEN;
3733 ci.tstamp = (__u32)(TIME_DELTA(idev->tstamp, INITIAL_JIFFIES) / HZ * 100
3734 + TIME_DELTA(idev->tstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
3735 ci.reachable_time = idev->nd_parms->reachable_time;
3736 ci.retrans_time = idev->nd_parms->retrans_time;
Thomas Graf04561c12006-11-14 19:53:58 -08003737 NLA_PUT(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci);
3738
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07003739 nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32));
3740 if (nla == NULL)
Thomas Graf04561c12006-11-14 19:53:58 -08003741 goto nla_put_failure;
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07003742 ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07003744 /* XXX - MC not implemented */
3745
3746 nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64));
3747 if (nla == NULL)
3748 goto nla_put_failure;
3749 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla));
3750
3751 nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64));
3752 if (nla == NULL)
3753 goto nla_put_failure;
3754 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755
Thomas Graf04561c12006-11-14 19:53:58 -08003756 nla_nest_end(skb, protoinfo);
3757 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758
Thomas Graf04561c12006-11-14 19:53:58 -08003759nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08003760 nlmsg_cancel(skb, nlh);
3761 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762}
3763
3764static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
3765{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09003766 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767 int idx, err;
3768 int s_idx = cb->args[0];
3769 struct net_device *dev;
3770 struct inet6_dev *idev;
3771
3772 read_lock(&dev_base_lock);
Pavel Emelianov7562f872007-05-03 15:13:45 -07003773 idx = 0;
Benjamin Thery6fda7352008-03-05 10:47:47 -08003774 for_each_netdev(net, dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775 if (idx < s_idx)
Pavel Emelianov7562f872007-05-03 15:13:45 -07003776 goto cont;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777 if ((idev = in6_dev_get(dev)) == NULL)
Pavel Emelianov7562f872007-05-03 15:13:45 -07003778 goto cont;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003779 err = inet6_fill_ifinfo(skb, idev, NETLINK_CB(cb->skb).pid,
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07003780 cb->nlh->nlmsg_seq, RTM_NEWLINK, NLM_F_MULTI);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781 in6_dev_put(idev);
3782 if (err <= 0)
3783 break;
Pavel Emelianov7562f872007-05-03 15:13:45 -07003784cont:
3785 idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786 }
3787 read_unlock(&dev_base_lock);
3788 cb->args[0] = idx;
3789
3790 return skb->len;
3791}
3792
3793void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
3794{
3795 struct sk_buff *skb;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09003796 struct net *net = dev_net(idev->dev);
Thomas Graf8d7a76c2006-08-15 00:35:47 -07003797 int err = -ENOBUFS;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003798
Thomas Graf339bf982006-11-10 14:10:15 -08003799 skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC);
Thomas Graf8d7a76c2006-08-15 00:35:47 -07003800 if (skb == NULL)
3801 goto errout;
3802
3803 err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08003804 if (err < 0) {
3805 /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */
3806 WARN_ON(err == -EMSGSIZE);
3807 kfree_skb(skb);
3808 goto errout;
3809 }
Benjamin Thery6fda7352008-03-05 10:47:47 -08003810 err = rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
Thomas Graf8d7a76c2006-08-15 00:35:47 -07003811errout:
3812 if (err < 0)
Benjamin Thery6fda7352008-03-05 10:47:47 -08003813 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814}
3815
Thomas Graf339bf982006-11-10 14:10:15 -08003816static inline size_t inet6_prefix_nlmsg_size(void)
3817{
3818 return NLMSG_ALIGN(sizeof(struct prefixmsg))
3819 + nla_total_size(sizeof(struct in6_addr))
3820 + nla_total_size(sizeof(struct prefix_cacheinfo));
3821}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07003822
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
Thomas Graf6051e2f2006-11-14 19:54:19 -08003824 struct prefix_info *pinfo, u32 pid, u32 seq,
3825 int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826{
Thomas Graf6051e2f2006-11-14 19:54:19 -08003827 struct prefixmsg *pmsg;
3828 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829 struct prefix_cacheinfo ci;
3830
Thomas Graf6051e2f2006-11-14 19:54:19 -08003831 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*pmsg), flags);
3832 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08003833 return -EMSGSIZE;
Thomas Graf6051e2f2006-11-14 19:54:19 -08003834
3835 pmsg = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836 pmsg->prefix_family = AF_INET6;
Patrick McHardy8a470772005-06-28 12:56:45 -07003837 pmsg->prefix_pad1 = 0;
3838 pmsg->prefix_pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839 pmsg->prefix_ifindex = idev->dev->ifindex;
3840 pmsg->prefix_len = pinfo->prefix_len;
3841 pmsg->prefix_type = pinfo->type;
Patrick McHardy8a470772005-06-28 12:56:45 -07003842 pmsg->prefix_pad3 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843 pmsg->prefix_flags = 0;
3844 if (pinfo->onlink)
3845 pmsg->prefix_flags |= IF_PREFIX_ONLINK;
3846 if (pinfo->autoconf)
3847 pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
3848
Thomas Graf6051e2f2006-11-14 19:54:19 -08003849 NLA_PUT(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850
3851 ci.preferred_time = ntohl(pinfo->prefered);
3852 ci.valid_time = ntohl(pinfo->valid);
Thomas Graf6051e2f2006-11-14 19:54:19 -08003853 NLA_PUT(skb, PREFIX_CACHEINFO, sizeof(ci), &ci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003854
Thomas Graf6051e2f2006-11-14 19:54:19 -08003855 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856
Thomas Graf6051e2f2006-11-14 19:54:19 -08003857nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08003858 nlmsg_cancel(skb, nlh);
3859 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003860}
3861
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003862static void inet6_prefix_notify(int event, struct inet6_dev *idev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003863 struct prefix_info *pinfo)
3864{
3865 struct sk_buff *skb;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09003866 struct net *net = dev_net(idev->dev);
Thomas Graf8c384bfa2006-08-15 00:36:07 -07003867 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003868
Thomas Graf339bf982006-11-10 14:10:15 -08003869 skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC);
Thomas Graf8c384bfa2006-08-15 00:36:07 -07003870 if (skb == NULL)
3871 goto errout;
3872
3873 err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08003874 if (err < 0) {
3875 /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */
3876 WARN_ON(err == -EMSGSIZE);
3877 kfree_skb(skb);
3878 goto errout;
3879 }
Benjamin Thery6fda7352008-03-05 10:47:47 -08003880 err = rtnl_notify(skb, net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC);
Thomas Graf8c384bfa2006-08-15 00:36:07 -07003881errout:
3882 if (err < 0)
Benjamin Thery6fda7352008-03-05 10:47:47 -08003883 rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884}
3885
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
3887{
3888 inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
3889
3890 switch (event) {
3891 case RTM_NEWADDR:
Neil Horman95c385b2007-04-25 17:08:10 -07003892 /*
3893 * If the address was optimistic
3894 * we inserted the route at the start of
3895 * our DAD process, so we don't need
3896 * to do it again
3897 */
3898 if (!(ifp->rt->rt6i_node))
3899 ip6_ins_rt(ifp->rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900 if (ifp->idev->cnf.forwarding)
3901 addrconf_join_anycast(ifp);
3902 break;
3903 case RTM_DELADDR:
3904 if (ifp->idev->cnf.forwarding)
3905 addrconf_leave_anycast(ifp);
3906 addrconf_leave_solict(ifp->idev, &ifp->addr);
3907 dst_hold(&ifp->rt->u.dst);
Thomas Grafe0a1ad732006-08-22 00:00:21 -07003908 if (ip6_del_rt(ifp->rt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909 dst_free(&ifp->rt->u.dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910 break;
3911 }
3912}
3913
3914static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
3915{
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07003916 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917 if (likely(ifp->idev->dead == 0))
3918 __ipv6_ifa_notify(event, ifp);
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07003919 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003920}
3921
3922#ifdef CONFIG_SYSCTL
3923
3924static
3925int addrconf_sysctl_forward(ctl_table *ctl, int write, struct file * filp,
3926 void __user *buffer, size_t *lenp, loff_t *ppos)
3927{
3928 int *valp = ctl->data;
3929 int val = *valp;
3930 int ret;
3931
3932 ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
3933
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -08003934 if (write)
3935 addrconf_fixup_forwarding(ctl, valp, val);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003936 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937}
3938
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003939static int addrconf_sysctl_forward_strategy(ctl_table *table,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940 int __user *name, int nlen,
3941 void __user *oldval,
3942 size_t __user *oldlenp,
Alexey Dobriyan1f29bcd2006-12-10 02:19:10 -08003943 void __user *newval, size_t newlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944{
3945 int *valp = table->data;
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -08003946 int val = *valp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947 int new;
3948
3949 if (!newval || !newlen)
3950 return 0;
3951 if (newlen != sizeof(int))
3952 return -EINVAL;
3953 if (get_user(new, (int __user *)newval))
3954 return -EFAULT;
3955 if (new == *valp)
3956 return 0;
3957 if (oldval && oldlenp) {
3958 size_t len;
3959 if (get_user(len, oldlenp))
3960 return -EFAULT;
3961 if (len) {
3962 if (len > table->maxlen)
3963 len = table->maxlen;
3964 if (copy_to_user(oldval, valp, len))
3965 return -EFAULT;
3966 if (put_user(len, oldlenp))
3967 return -EFAULT;
3968 }
3969 }
3970
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -08003971 *valp = new;
3972 addrconf_fixup_forwarding(table, valp, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973 return 1;
3974}
3975
3976static struct addrconf_sysctl_table
3977{
3978 struct ctl_table_header *sysctl_header;
YOSHIFUJI Hideakif6a07b22008-03-25 00:25:11 +09003979 ctl_table addrconf_vars[DEVCONF_MAX+1];
Pavel Emelyanov1dab6222007-12-02 00:59:38 +11003980 char *dev_name;
Brian Haleyab32ea52006-09-22 14:15:41 -07003981} addrconf_sysctl __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982 .sysctl_header = NULL,
3983 .addrconf_vars = {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003984 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985 .ctl_name = NET_IPV6_FORWARDING,
3986 .procname = "forwarding",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003987 .data = &ipv6_devconf.forwarding,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988 .maxlen = sizeof(int),
3989 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003990 .proc_handler = &addrconf_sysctl_forward,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991 .strategy = &addrconf_sysctl_forward_strategy,
3992 },
3993 {
3994 .ctl_name = NET_IPV6_HOP_LIMIT,
3995 .procname = "hop_limit",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003996 .data = &ipv6_devconf.hop_limit,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997 .maxlen = sizeof(int),
3998 .mode = 0644,
3999 .proc_handler = proc_dointvec,
4000 },
4001 {
4002 .ctl_name = NET_IPV6_MTU,
4003 .procname = "mtu",
4004 .data = &ipv6_devconf.mtu6,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004005 .maxlen = sizeof(int),
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004007 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008 },
4009 {
4010 .ctl_name = NET_IPV6_ACCEPT_RA,
4011 .procname = "accept_ra",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004012 .data = &ipv6_devconf.accept_ra,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013 .maxlen = sizeof(int),
4014 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004015 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016 },
4017 {
4018 .ctl_name = NET_IPV6_ACCEPT_REDIRECTS,
4019 .procname = "accept_redirects",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004020 .data = &ipv6_devconf.accept_redirects,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021 .maxlen = sizeof(int),
4022 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004023 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024 },
4025 {
4026 .ctl_name = NET_IPV6_AUTOCONF,
4027 .procname = "autoconf",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004028 .data = &ipv6_devconf.autoconf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029 .maxlen = sizeof(int),
4030 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004031 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004032 },
4033 {
4034 .ctl_name = NET_IPV6_DAD_TRANSMITS,
4035 .procname = "dad_transmits",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004036 .data = &ipv6_devconf.dad_transmits,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004037 .maxlen = sizeof(int),
4038 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004039 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040 },
4041 {
4042 .ctl_name = NET_IPV6_RTR_SOLICITS,
4043 .procname = "router_solicitations",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004044 .data = &ipv6_devconf.rtr_solicits,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045 .maxlen = sizeof(int),
4046 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004047 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048 },
4049 {
4050 .ctl_name = NET_IPV6_RTR_SOLICIT_INTERVAL,
4051 .procname = "router_solicitation_interval",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004052 .data = &ipv6_devconf.rtr_solicit_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053 .maxlen = sizeof(int),
4054 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004055 .proc_handler = &proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056 .strategy = &sysctl_jiffies,
4057 },
4058 {
4059 .ctl_name = NET_IPV6_RTR_SOLICIT_DELAY,
4060 .procname = "router_solicitation_delay",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004061 .data = &ipv6_devconf.rtr_solicit_delay,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062 .maxlen = sizeof(int),
4063 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004064 .proc_handler = &proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065 .strategy = &sysctl_jiffies,
4066 },
4067 {
4068 .ctl_name = NET_IPV6_FORCE_MLD_VERSION,
4069 .procname = "force_mld_version",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004070 .data = &ipv6_devconf.force_mld_version,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071 .maxlen = sizeof(int),
4072 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004073 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074 },
4075#ifdef CONFIG_IPV6_PRIVACY
4076 {
4077 .ctl_name = NET_IPV6_USE_TEMPADDR,
4078 .procname = "use_tempaddr",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004079 .data = &ipv6_devconf.use_tempaddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080 .maxlen = sizeof(int),
4081 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004082 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083 },
4084 {
4085 .ctl_name = NET_IPV6_TEMP_VALID_LFT,
4086 .procname = "temp_valid_lft",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004087 .data = &ipv6_devconf.temp_valid_lft,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088 .maxlen = sizeof(int),
4089 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004090 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091 },
4092 {
4093 .ctl_name = NET_IPV6_TEMP_PREFERED_LFT,
4094 .procname = "temp_prefered_lft",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004095 .data = &ipv6_devconf.temp_prefered_lft,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096 .maxlen = sizeof(int),
4097 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004098 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099 },
4100 {
4101 .ctl_name = NET_IPV6_REGEN_MAX_RETRY,
4102 .procname = "regen_max_retry",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004103 .data = &ipv6_devconf.regen_max_retry,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104 .maxlen = sizeof(int),
4105 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004106 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107 },
4108 {
4109 .ctl_name = NET_IPV6_MAX_DESYNC_FACTOR,
4110 .procname = "max_desync_factor",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004111 .data = &ipv6_devconf.max_desync_factor,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112 .maxlen = sizeof(int),
4113 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004114 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115 },
4116#endif
4117 {
4118 .ctl_name = NET_IPV6_MAX_ADDRESSES,
4119 .procname = "max_addresses",
4120 .data = &ipv6_devconf.max_addresses,
4121 .maxlen = sizeof(int),
4122 .mode = 0644,
4123 .proc_handler = &proc_dointvec,
4124 },
4125 {
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -08004126 .ctl_name = NET_IPV6_ACCEPT_RA_DEFRTR,
4127 .procname = "accept_ra_defrtr",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004128 .data = &ipv6_devconf.accept_ra_defrtr,
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -08004129 .maxlen = sizeof(int),
4130 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004131 .proc_handler = &proc_dointvec,
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -08004132 },
4133 {
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -08004134 .ctl_name = NET_IPV6_ACCEPT_RA_PINFO,
4135 .procname = "accept_ra_pinfo",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004136 .data = &ipv6_devconf.accept_ra_pinfo,
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -08004137 .maxlen = sizeof(int),
4138 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004139 .proc_handler = &proc_dointvec,
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -08004140 },
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08004141#ifdef CONFIG_IPV6_ROUTER_PREF
4142 {
4143 .ctl_name = NET_IPV6_ACCEPT_RA_RTR_PREF,
4144 .procname = "accept_ra_rtr_pref",
4145 .data = &ipv6_devconf.accept_ra_rtr_pref,
4146 .maxlen = sizeof(int),
4147 .mode = 0644,
4148 .proc_handler = &proc_dointvec,
4149 },
YOSHIFUJI Hideaki52e16352006-03-20 17:05:47 -08004150 {
4151 .ctl_name = NET_IPV6_RTR_PROBE_INTERVAL,
4152 .procname = "router_probe_interval",
4153 .data = &ipv6_devconf.rtr_probe_interval,
4154 .maxlen = sizeof(int),
4155 .mode = 0644,
4156 .proc_handler = &proc_dointvec_jiffies,
4157 .strategy = &sysctl_jiffies,
4158 },
Neil Hormanfa03ef32007-01-30 14:30:10 -08004159#ifdef CONFIG_IPV6_ROUTE_INFO
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -08004160 {
4161 .ctl_name = NET_IPV6_ACCEPT_RA_RT_INFO_MAX_PLEN,
4162 .procname = "accept_ra_rt_info_max_plen",
4163 .data = &ipv6_devconf.accept_ra_rt_info_max_plen,
4164 .maxlen = sizeof(int),
4165 .mode = 0644,
4166 .proc_handler = &proc_dointvec,
4167 },
4168#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08004169#endif
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -08004170 {
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -07004171 .ctl_name = NET_IPV6_PROXY_NDP,
4172 .procname = "proxy_ndp",
4173 .data = &ipv6_devconf.proxy_ndp,
4174 .maxlen = sizeof(int),
4175 .mode = 0644,
4176 .proc_handler = &proc_dointvec,
4177 },
4178 {
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -07004179 .ctl_name = NET_IPV6_ACCEPT_SOURCE_ROUTE,
4180 .procname = "accept_source_route",
4181 .data = &ipv6_devconf.accept_source_route,
4182 .maxlen = sizeof(int),
4183 .mode = 0644,
4184 .proc_handler = &proc_dointvec,
4185 },
Neil Horman95c385b2007-04-25 17:08:10 -07004186#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
4187 {
4188 .ctl_name = CTL_UNNUMBERED,
4189 .procname = "optimistic_dad",
4190 .data = &ipv6_devconf.optimistic_dad,
4191 .maxlen = sizeof(int),
4192 .mode = 0644,
4193 .proc_handler = &proc_dointvec,
4194
4195 },
4196#endif
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09004197#ifdef CONFIG_IPV6_MROUTE
4198 {
4199 .ctl_name = CTL_UNNUMBERED,
4200 .procname = "mc_forwarding",
4201 .data = &ipv6_devconf.mc_forwarding,
4202 .maxlen = sizeof(int),
4203 .mode = 0644,
4204 .proc_handler = &proc_dointvec,
4205 },
4206#endif
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -07004207 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208 .ctl_name = 0, /* sentinel */
4209 }
4210 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211};
4212
Pavel Emelyanovbff16c22008-01-10 17:42:13 -08004213static int __addrconf_sysctl_register(struct net *net, char *dev_name,
4214 int ctl_name, struct inet6_dev *idev, struct ipv6_devconf *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215{
4216 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217 struct addrconf_sysctl_table *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218
Pavel Emelyanov1dab6222007-12-02 00:59:38 +11004219#define ADDRCONF_CTL_PATH_DEV 3
4220
4221 struct ctl_path addrconf_ctl_path[] = {
4222 { .procname = "net", .ctl_name = CTL_NET, },
4223 { .procname = "ipv6", .ctl_name = NET_IPV6, },
4224 { .procname = "conf", .ctl_name = NET_IPV6_CONF, },
4225 { /* to be set */ },
4226 { },
4227 };
4228
4229
Arnaldo Carvalho de Meloaf879cc2006-11-17 12:14:37 -02004230 t = kmemdup(&addrconf_sysctl, sizeof(*t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231 if (t == NULL)
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11004232 goto out;
4233
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234 for (i=0; t->addrconf_vars[i].data; i++) {
4235 t->addrconf_vars[i].data += (char*)p - (char*)&ipv6_devconf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236 t->addrconf_vars[i].extra1 = idev; /* embedded; no ref */
Pavel Emelyanovbff16c22008-01-10 17:42:13 -08004237 t->addrconf_vars[i].extra2 = net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004240 /*
4241 * Make a copy of dev_name, because '.procname' is regarded as const
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242 * by sysctl and we wouldn't want anyone to change it under our feet
4243 * (see SIOCSIFNAME).
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004244 */
Pavel Emelyanov1dab6222007-12-02 00:59:38 +11004245 t->dev_name = kstrdup(dev_name, GFP_KERNEL);
4246 if (!t->dev_name)
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11004247 goto free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248
Pavel Emelyanov1dab6222007-12-02 00:59:38 +11004249 addrconf_ctl_path[ADDRCONF_CTL_PATH_DEV].procname = t->dev_name;
4250 addrconf_ctl_path[ADDRCONF_CTL_PATH_DEV].ctl_name = ctl_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251
Pavel Emelyanovbff16c22008-01-10 17:42:13 -08004252 t->sysctl_header = register_net_sysctl_table(net, addrconf_ctl_path,
Pavel Emelyanov1dab6222007-12-02 00:59:38 +11004253 t->addrconf_vars);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254 if (t->sysctl_header == NULL)
4255 goto free_procname;
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11004256
4257 p->sysctl = t;
Pavel Emelyanov95897312008-01-10 17:41:45 -08004258 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11004260free_procname:
Pavel Emelyanov1dab6222007-12-02 00:59:38 +11004261 kfree(t->dev_name);
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11004262free:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263 kfree(t);
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11004264out:
Pavel Emelyanov95897312008-01-10 17:41:45 -08004265 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266}
4267
Pavel Emelyanov408c4762008-01-10 17:41:21 -08004268static void __addrconf_sysctl_unregister(struct ipv6_devconf *p)
4269{
4270 struct addrconf_sysctl_table *t;
4271
4272 if (p->sysctl == NULL)
4273 return;
4274
4275 t = p->sysctl;
4276 p->sysctl = NULL;
4277 unregister_sysctl_table(t->sysctl_header);
4278 kfree(t->dev_name);
4279 kfree(t);
4280}
4281
Pavel Emelyanovf52295a2007-12-02 00:58:37 +11004282static void addrconf_sysctl_register(struct inet6_dev *idev)
4283{
Pavel Emelyanov408c4762008-01-10 17:41:21 -08004284 neigh_sysctl_register(idev->dev, idev->nd_parms, NET_IPV6,
4285 NET_IPV6_NEIGH, "ipv6",
4286 &ndisc_ifinfo_sysctl_change,
YOSHIFUJI Hideaki0686caa2008-05-19 16:25:42 -07004287 ndisc_ifinfo_sysctl_strategy);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09004288 __addrconf_sysctl_register(dev_net(idev->dev), idev->dev->name,
Pavel Emelyanovbff16c22008-01-10 17:42:13 -08004289 idev->dev->ifindex, idev, &idev->cnf);
Pavel Emelyanovf52295a2007-12-02 00:58:37 +11004290}
4291
Pavel Emelyanov408c4762008-01-10 17:41:21 -08004292static void addrconf_sysctl_unregister(struct inet6_dev *idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293{
Pavel Emelyanov408c4762008-01-10 17:41:21 -08004294 __addrconf_sysctl_unregister(&idev->cnf);
4295 neigh_sysctl_unregister(idev->nd_parms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296}
4297
4298
4299#endif
4300
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08004301static int addrconf_init_net(struct net *net)
4302{
4303 int err;
4304 struct ipv6_devconf *all, *dflt;
4305
4306 err = -ENOMEM;
4307 all = &ipv6_devconf;
4308 dflt = &ipv6_devconf_dflt;
4309
4310 if (net != &init_net) {
4311 all = kmemdup(all, sizeof(ipv6_devconf), GFP_KERNEL);
4312 if (all == NULL)
4313 goto err_alloc_all;
4314
4315 dflt = kmemdup(dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL);
4316 if (dflt == NULL)
4317 goto err_alloc_dflt;
4318 }
4319
4320 net->ipv6.devconf_all = all;
4321 net->ipv6.devconf_dflt = dflt;
4322
4323#ifdef CONFIG_SYSCTL
4324 err = __addrconf_sysctl_register(net, "all", NET_PROTO_CONF_ALL,
4325 NULL, all);
4326 if (err < 0)
4327 goto err_reg_all;
4328
4329 err = __addrconf_sysctl_register(net, "default", NET_PROTO_CONF_DEFAULT,
4330 NULL, dflt);
4331 if (err < 0)
4332 goto err_reg_dflt;
4333#endif
4334 return 0;
4335
4336#ifdef CONFIG_SYSCTL
4337err_reg_dflt:
4338 __addrconf_sysctl_unregister(all);
4339err_reg_all:
4340 kfree(dflt);
4341#endif
4342err_alloc_dflt:
4343 kfree(all);
4344err_alloc_all:
4345 return err;
4346}
4347
4348static void addrconf_exit_net(struct net *net)
4349{
4350#ifdef CONFIG_SYSCTL
4351 __addrconf_sysctl_unregister(net->ipv6.devconf_dflt);
4352 __addrconf_sysctl_unregister(net->ipv6.devconf_all);
4353#endif
4354 if (net != &init_net) {
4355 kfree(net->ipv6.devconf_dflt);
4356 kfree(net->ipv6.devconf_all);
4357 }
4358}
4359
4360static struct pernet_operations addrconf_ops = {
4361 .init = addrconf_init_net,
4362 .exit = addrconf_exit_net,
4363};
4364
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365/*
4366 * Device notifier
4367 */
4368
4369int register_inet6addr_notifier(struct notifier_block *nb)
4370{
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004371 return atomic_notifier_chain_register(&inet6addr_chain, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004372}
4373
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +09004374EXPORT_SYMBOL(register_inet6addr_notifier);
4375
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376int unregister_inet6addr_notifier(struct notifier_block *nb)
4377{
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004378 return atomic_notifier_chain_unregister(&inet6addr_chain,nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379}
4380
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +09004381EXPORT_SYMBOL(unregister_inet6addr_notifier);
4382
Daniel Lezcanoaf284932008-03-05 10:46:57 -08004383static void addrconf_net_exit(struct net *net)
4384{
Benjamin Thery6fda7352008-03-05 10:47:47 -08004385 struct net_device *dev;
4386
Benjamin Thery6fda7352008-03-05 10:47:47 -08004387 rtnl_lock();
4388 /* clean dev list */
4389 for_each_netdev(net, dev) {
4390 if (__in6_dev_get(dev) == NULL)
4391 continue;
4392 addrconf_ifdown(dev, 1);
4393 }
4394 addrconf_ifdown(net->loopback_dev, 2);
4395 rtnl_unlock();
Daniel Lezcanoaf284932008-03-05 10:46:57 -08004396}
4397
4398static struct pernet_operations addrconf_net_ops = {
Daniel Lezcanoaf284932008-03-05 10:46:57 -08004399 .exit = addrconf_net_exit,
4400};
4401
Linus Torvalds1da177e2005-04-16 15:20:36 -07004402/*
4403 * Init / cleanup code
4404 */
4405
4406int __init addrconf_init(void)
4407{
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +09004408 int err;
4409
4410 if ((err = ipv6_addr_label_init()) < 0) {
4411 printk(KERN_CRIT "IPv6 Addrconf: cannot initialize default policy table: %d.\n",
4412 err);
4413 return err;
4414 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004415
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08004416 register_pernet_subsys(&addrconf_ops);
4417
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418 /* The addrconf netdev notifier requires that loopback_dev
4419 * has it's ipv6 private information allocated and setup
4420 * before it can bring up and give link-local addresses
4421 * to other devices which are up.
4422 *
4423 * Unfortunately, loopback_dev is not necessarily the first
4424 * entry in the global dev_base list of net devices. In fact,
4425 * it is likely to be the very last entry on that list.
4426 * So this causes the notifier registry below to try and
4427 * give link-local addresses to all devices besides loopback_dev
4428 * first, then loopback_dev, which cases all the non-loopback_dev
4429 * devices to fail to get a link-local address.
4430 *
4431 * So, as a temporary fix, allocate the ipv6 structure for
4432 * loopback_dev first by hand.
4433 * Longer term, all of the dependencies ipv6 has upon the loopback
4434 * device and it being up should be removed.
4435 */
4436 rtnl_lock();
Eric W. Biederman2774c7a2007-09-26 22:10:56 -07004437 if (!ipv6_add_dev(init_net.loopback_dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438 err = -ENOMEM;
4439 rtnl_unlock();
4440 if (err)
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08004441 goto errlo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442
Daniel Lezcanoaf284932008-03-05 10:46:57 -08004443 err = register_pernet_device(&addrconf_net_ops);
4444 if (err)
4445 return err;
4446
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447 register_netdevice_notifier(&ipv6_dev_notf);
4448
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449 addrconf_verify(0);
Thomas Grafc127ea22007-03-22 11:58:32 -07004450
4451 err = __rtnl_register(PF_INET6, RTM_GETLINK, NULL, inet6_dump_ifinfo);
4452 if (err < 0)
4453 goto errout;
4454
4455 /* Only the first call to __rtnl_register can fail */
4456 __rtnl_register(PF_INET6, RTM_NEWADDR, inet6_rtm_newaddr, NULL);
4457 __rtnl_register(PF_INET6, RTM_DELADDR, inet6_rtm_deladdr, NULL);
4458 __rtnl_register(PF_INET6, RTM_GETADDR, inet6_rtm_getaddr, inet6_dump_ifaddr);
4459 __rtnl_register(PF_INET6, RTM_GETMULTICAST, NULL, inet6_dump_ifmcaddr);
4460 __rtnl_register(PF_INET6, RTM_GETANYCAST, NULL, inet6_dump_ifacaddr);
4461
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +09004462 ipv6_addr_label_rtnl_register();
4463
Linus Torvalds1da177e2005-04-16 15:20:36 -07004464 return 0;
Thomas Grafc127ea22007-03-22 11:58:32 -07004465errout:
4466 unregister_netdevice_notifier(&ipv6_dev_notf);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08004467errlo:
4468 unregister_pernet_subsys(&addrconf_ops);
Thomas Grafc127ea22007-03-22 11:58:32 -07004469
4470 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471}
4472
Daniel Lezcano09f77092007-12-13 05:34:58 -08004473void addrconf_cleanup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474{
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004475 struct inet6_ifaddr *ifa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476 int i;
4477
4478 unregister_netdevice_notifier(&ipv6_dev_notf);
Daniel Lezcanoaf284932008-03-05 10:46:57 -08004479 unregister_pernet_device(&addrconf_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08004481 unregister_pernet_subsys(&addrconf_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482
4483 rtnl_lock();
4484
4485 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486 * Check hash table.
4487 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488 write_lock_bh(&addrconf_hash_lock);
4489 for (i=0; i < IN6_ADDR_HSIZE; i++) {
4490 for (ifa=inet6_addr_lst[i]; ifa; ) {
4491 struct inet6_ifaddr *bifa;
4492
4493 bifa = ifa;
4494 ifa = ifa->lst_next;
4495 printk(KERN_DEBUG "bug: IPv6 address leakage detected: ifa=%p\n", bifa);
4496 /* Do not free it; something is wrong.
4497 Now we can investigate it with debugger.
4498 */
4499 }
4500 }
4501 write_unlock_bh(&addrconf_hash_lock);
4502
4503 del_timer(&addr_chk_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004504 rtnl_unlock();
Daniel Lezcanoaf284932008-03-05 10:46:57 -08004505
4506 unregister_pernet_subsys(&addrconf_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507}