blob: bde50c686722d68ec8b975f395b1e8dd1de5a949 [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);
152static int ipv6_chk_same_addr(const struct in6_addr *addr, struct net_device *dev);
153
Alan Sterne041c682006-03-27 01:16:30 -0800154static ATOMIC_NOTIFIER_HEAD(inet6addr_chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Brian Haleyab32ea52006-09-22 14:15:41 -0700156struct ipv6_devconf ipv6_devconf __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 .forwarding = 0,
158 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
159 .mtu6 = IPV6_MIN_MTU,
160 .accept_ra = 1,
161 .accept_redirects = 1,
162 .autoconf = 1,
163 .force_mld_version = 0,
164 .dad_transmits = 1,
165 .rtr_solicits = MAX_RTR_SOLICITATIONS,
166 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
167 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
168#ifdef CONFIG_IPV6_PRIVACY
169 .use_tempaddr = 0,
170 .temp_valid_lft = TEMP_VALID_LIFETIME,
171 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
172 .regen_max_retry = REGEN_MAX_RETRY,
173 .max_desync_factor = MAX_DESYNC_FACTOR,
174#endif
175 .max_addresses = IPV6_MAX_ADDRESSES,
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -0800176 .accept_ra_defrtr = 1,
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -0800177 .accept_ra_pinfo = 1,
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800178#ifdef CONFIG_IPV6_ROUTER_PREF
179 .accept_ra_rtr_pref = 1,
YOSHIFUJI Hideaki52e163562006-03-20 17:05:47 -0800180 .rtr_probe_interval = 60 * HZ,
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -0800181#ifdef CONFIG_IPV6_ROUTE_INFO
182 .accept_ra_rt_info_max_plen = 0,
183#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800184#endif
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -0700185 .proxy_ndp = 0,
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -0700186 .accept_source_route = 0, /* we do not accept RH0 by default. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187};
188
Brian Haleyab32ea52006-09-22 14:15:41 -0700189static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 .forwarding = 0,
191 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
192 .mtu6 = IPV6_MIN_MTU,
193 .accept_ra = 1,
194 .accept_redirects = 1,
195 .autoconf = 1,
196 .dad_transmits = 1,
197 .rtr_solicits = MAX_RTR_SOLICITATIONS,
198 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
199 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
200#ifdef CONFIG_IPV6_PRIVACY
201 .use_tempaddr = 0,
202 .temp_valid_lft = TEMP_VALID_LIFETIME,
203 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
204 .regen_max_retry = REGEN_MAX_RETRY,
205 .max_desync_factor = MAX_DESYNC_FACTOR,
206#endif
207 .max_addresses = IPV6_MAX_ADDRESSES,
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -0800208 .accept_ra_defrtr = 1,
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -0800209 .accept_ra_pinfo = 1,
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800210#ifdef CONFIG_IPV6_ROUTER_PREF
211 .accept_ra_rtr_pref = 1,
YOSHIFUJI Hideaki52e163562006-03-20 17:05:47 -0800212 .rtr_probe_interval = 60 * HZ,
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -0800213#ifdef CONFIG_IPV6_ROUTE_INFO
214 .accept_ra_rt_info_max_plen = 0,
215#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800216#endif
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -0700217 .proxy_ndp = 0,
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -0700218 .accept_source_route = 0, /* we do not accept RH0 by default. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219};
220
221/* IPv6 Wildcard Address and Loopback Address defined by RFC2553 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
224
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -0700225/* Check if a valid qdisc is available */
226static inline int addrconf_qdisc_ok(struct net_device *dev)
227{
228 return (dev->qdisc != &noop_qdisc);
229}
230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231static void addrconf_del_timer(struct inet6_ifaddr *ifp)
232{
233 if (del_timer(&ifp->timer))
234 __in6_ifa_put(ifp);
235}
236
237enum addrconf_timer_t
238{
239 AC_NONE,
240 AC_DAD,
241 AC_RS,
242};
243
244static void addrconf_mod_timer(struct inet6_ifaddr *ifp,
245 enum addrconf_timer_t what,
246 unsigned long when)
247{
248 if (!del_timer(&ifp->timer))
249 in6_ifa_hold(ifp);
250
251 switch (what) {
252 case AC_DAD:
253 ifp->timer.function = addrconf_dad_timer;
254 break;
255 case AC_RS:
256 ifp->timer.function = addrconf_rs_timer;
257 break;
258 default:;
259 }
260 ifp->timer.expires = jiffies + when;
261 add_timer(&ifp->timer);
262}
263
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700264static int snmp6_alloc_dev(struct inet6_dev *idev)
265{
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700266 if (snmp_mib_init((void **)idev->stats.ipv6,
YOSHIFUJI Hideakic69bce22008-01-23 22:31:45 -0800267 sizeof(struct ipstats_mib)) < 0)
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700268 goto err_ip;
269 if (snmp_mib_init((void **)idev->stats.icmpv6,
YOSHIFUJI Hideakic69bce22008-01-23 22:31:45 -0800270 sizeof(struct icmpv6_mib)) < 0)
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700271 goto err_icmp;
David L Stevens14878f72007-09-16 16:52:35 -0700272 if (snmp_mib_init((void **)idev->stats.icmpv6msg,
YOSHIFUJI Hideakic69bce22008-01-23 22:31:45 -0800273 sizeof(struct icmpv6msg_mib)) < 0)
David L Stevens14878f72007-09-16 16:52:35 -0700274 goto err_icmpmsg;
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700275
276 return 0;
277
David L Stevens14878f72007-09-16 16:52:35 -0700278err_icmpmsg:
279 snmp_mib_free((void **)idev->stats.icmpv6);
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700280err_icmp:
281 snmp_mib_free((void **)idev->stats.ipv6);
282err_ip:
Pavel Emelyanovaaf70ec2007-10-17 21:25:32 -0700283 return -ENOMEM;
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700284}
285
Pavel Emelyanov16910b92007-10-17 21:23:43 -0700286static void snmp6_free_dev(struct inet6_dev *idev)
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700287{
David L Stevens14878f72007-09-16 16:52:35 -0700288 snmp_mib_free((void **)idev->stats.icmpv6msg);
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700289 snmp_mib_free((void **)idev->stats.icmpv6);
290 snmp_mib_free((void **)idev->stats.ipv6);
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700291}
292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293/* Nobody refers to this device, we may destroy it. */
294
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -0700295static void in6_dev_finish_destroy_rcu(struct rcu_head *head)
296{
297 struct inet6_dev *idev = container_of(head, struct inet6_dev, rcu);
298 kfree(idev);
299}
300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301void in6_dev_finish_destroy(struct inet6_dev *idev)
302{
303 struct net_device *dev = idev->dev;
304 BUG_TRAP(idev->addr_list==NULL);
305 BUG_TRAP(idev->mc_list==NULL);
306#ifdef NET_REFCNT_DEBUG
307 printk(KERN_DEBUG "in6_dev_finish_destroy: %s\n", dev ? dev->name : "NIL");
308#endif
309 dev_put(dev);
310 if (!idev->dead) {
311 printk("Freeing alive inet6 device %p\n", idev);
312 return;
313 }
314 snmp6_free_dev(idev);
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -0700315 call_rcu(&idev->rcu, in6_dev_finish_destroy_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316}
317
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +0900318EXPORT_SYMBOL(in6_dev_finish_destroy);
319
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320static struct inet6_dev * ipv6_add_dev(struct net_device *dev)
321{
322 struct inet6_dev *ndev;
YOSHIFUJI Hideakid88ae4c2007-01-14 21:48:40 -0800323 struct in6_addr maddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
325 ASSERT_RTNL();
326
327 if (dev->mtu < IPV6_MIN_MTU)
328 return NULL;
329
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900330 ndev = kzalloc(sizeof(struct inet6_dev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900332 if (ndev == NULL)
333 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Ingo Oeser322f74a2006-03-20 23:01:47 -0800335 rwlock_init(&ndev->lock);
336 ndev->dev = dev;
337 memcpy(&ndev->cnf, &ipv6_devconf_dflt, sizeof(ndev->cnf));
338 ndev->cnf.mtu6 = dev->mtu;
339 ndev->cnf.sysctl = NULL;
340 ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl);
341 if (ndev->nd_parms == NULL) {
342 kfree(ndev);
343 return NULL;
344 }
345 /* We refer to the device */
346 dev_hold(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
Ingo Oeser322f74a2006-03-20 23:01:47 -0800348 if (snmp6_alloc_dev(ndev) < 0) {
349 ADBG((KERN_WARNING
350 "%s(): cannot allocate memory for statistics; dev=%s.\n",
351 __FUNCTION__, dev->name));
352 neigh_parms_release(&nd_tbl, ndev->nd_parms);
353 ndev->dead = 1;
354 in6_dev_finish_destroy(ndev);
355 return NULL;
356 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
Ingo Oeser322f74a2006-03-20 23:01:47 -0800358 if (snmp6_register_dev(ndev) < 0) {
359 ADBG((KERN_WARNING
360 "%s(): cannot create /proc/net/dev_snmp6/%s\n",
361 __FUNCTION__, dev->name));
362 neigh_parms_release(&nd_tbl, ndev->nd_parms);
363 ndev->dead = 1;
364 in6_dev_finish_destroy(ndev);
365 return NULL;
366 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
Ingo Oeser322f74a2006-03-20 23:01:47 -0800368 /* One reference from device. We must do this before
369 * we invoke __ipv6_regen_rndid().
370 */
371 in6_dev_hold(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
373#ifdef CONFIG_IPV6_PRIVACY
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800374 setup_timer(&ndev->regen_timer, ipv6_regen_rndid, (unsigned long)ndev);
Ingo Oeser322f74a2006-03-20 23:01:47 -0800375 if ((dev->flags&IFF_LOOPBACK) ||
376 dev->type == ARPHRD_TUNNEL ||
Joerg Roedel0be669b2006-10-10 14:49:53 -0700377#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
378 dev->type == ARPHRD_SIT ||
379#endif
380 dev->type == ARPHRD_NONE) {
Ingo Oeser322f74a2006-03-20 23:01:47 -0800381 printk(KERN_INFO
382 "%s: Disabled Privacy Extensions\n",
383 dev->name);
384 ndev->cnf.use_tempaddr = -1;
Fred L. Templinc7dc89c2007-11-29 22:11:40 +1100385
386 if (dev->type == ARPHRD_SIT && (dev->priv_flags & IFF_ISATAP)) {
387 printk(KERN_INFO
388 "%s: Disabled Multicast RS\n",
389 dev->name);
390 ndev->cnf.rtr_solicits = 0;
391 }
Ingo Oeser322f74a2006-03-20 23:01:47 -0800392 } else {
393 in6_dev_hold(ndev);
394 ipv6_regen_rndid((unsigned long) ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 }
Ingo Oeser322f74a2006-03-20 23:01:47 -0800396#endif
397
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -0700398 if (netif_running(dev) && addrconf_qdisc_ok(dev))
Herbert Xu53aadcc2007-03-27 14:31:52 -0700399 ndev->if_flags |= IF_READY;
400
Ingo Oeser322f74a2006-03-20 23:01:47 -0800401 ipv6_mc_init_dev(ndev);
402 ndev->tstamp = jiffies;
Pavel Emelyanovf52295a2007-12-02 00:58:37 +1100403 addrconf_sysctl_register(ndev);
David L Stevens30c4cf52007-01-04 12:31:14 -0800404 /* protected by rtnl_lock */
405 rcu_assign_pointer(dev->ip6_ptr, ndev);
YOSHIFUJI Hideakid88ae4c2007-01-14 21:48:40 -0800406
407 /* Join all-node multicast group */
408 ipv6_addr_all_nodes(&maddr);
409 ipv6_dev_mc_inc(dev, &maddr);
410
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 return ndev;
412}
413
414static struct inet6_dev * ipv6_find_idev(struct net_device *dev)
415{
416 struct inet6_dev *idev;
417
418 ASSERT_RTNL();
419
420 if ((idev = __in6_dev_get(dev)) == NULL) {
421 if ((idev = ipv6_add_dev(dev)) == NULL)
422 return NULL;
423 }
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +0900424
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 if (dev->flags&IFF_UP)
426 ipv6_mc_up(idev);
427 return idev;
428}
429
430#ifdef CONFIG_SYSCTL
431static void dev_forward_change(struct inet6_dev *idev)
432{
433 struct net_device *dev;
434 struct inet6_ifaddr *ifa;
435 struct in6_addr addr;
436
437 if (!idev)
438 return;
439 dev = idev->dev;
440 if (dev && (dev->flags & IFF_MULTICAST)) {
441 ipv6_addr_all_routers(&addr);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900442
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 if (idev->cnf.forwarding)
444 ipv6_dev_mc_inc(dev, &addr);
445 else
446 ipv6_dev_mc_dec(dev, &addr);
447 }
448 for (ifa=idev->addr_list; ifa; ifa=ifa->if_next) {
Michal Wrobel2c12a742007-02-26 15:36:10 -0800449 if (ifa->flags&IFA_F_TENTATIVE)
450 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 if (idev->cnf.forwarding)
452 addrconf_join_anycast(ifa);
453 else
454 addrconf_leave_anycast(ifa);
455 }
456}
457
458
Pavel Emelyanovbff16c22008-01-10 17:42:13 -0800459static void addrconf_forward_change(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{
461 struct net_device *dev;
462 struct inet6_dev *idev;
463
464 read_lock(&dev_base_lock);
Pavel Emelyanovbff16c22008-01-10 17:42:13 -0800465 for_each_netdev(net, dev) {
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -0700466 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 idev = __in6_dev_get(dev);
468 if (idev) {
469 int changed = (!idev->cnf.forwarding) ^ (!ipv6_devconf.forwarding);
470 idev->cnf.forwarding = ipv6_devconf.forwarding;
471 if (changed)
472 dev_forward_change(idev);
473 }
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -0700474 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 }
476 read_unlock(&dev_base_lock);
477}
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800478
479static void addrconf_fixup_forwarding(struct ctl_table *table, int *p, int old)
480{
Pavel Emelyanovbff16c22008-01-10 17:42:13 -0800481 struct net *net;
482
483 net = (struct net *)table->extra2;
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800484 if (p == &ipv6_devconf_dflt.forwarding)
485 return;
486
487 if (p == &ipv6_devconf.forwarding) {
488 ipv6_devconf_dflt.forwarding = ipv6_devconf.forwarding;
Pavel Emelyanovbff16c22008-01-10 17:42:13 -0800489 addrconf_forward_change(net);
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800490 } else if ((!*p) ^ (!old))
491 dev_forward_change((struct inet6_dev *)table->extra1);
492
493 if (*p)
494 rt6_purge_dflt_routers();
495}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496#endif
497
498/* Nobody refers to this ifaddr, destroy it */
499
500void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp)
501{
502 BUG_TRAP(ifp->if_next==NULL);
503 BUG_TRAP(ifp->lst_next==NULL);
504#ifdef NET_REFCNT_DEBUG
505 printk(KERN_DEBUG "inet6_ifa_finish_destroy\n");
506#endif
507
508 in6_dev_put(ifp->idev);
509
510 if (del_timer(&ifp->timer))
511 printk("Timer is still running, when freeing ifa=%p\n", ifp);
512
513 if (!ifp->dead) {
514 printk("Freeing alive inet6 address %p\n", ifp);
515 return;
516 }
517 dst_release(&ifp->rt->u.dst);
518
519 kfree(ifp);
520}
521
Brian Haleye55ffac2006-07-10 15:25:51 -0700522static void
523ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp)
524{
525 struct inet6_ifaddr *ifa, **ifap;
YOSHIFUJI Hideaki8a6ce0c2006-07-11 13:05:30 -0700526 int ifp_scope = ipv6_addr_src_scope(&ifp->addr);
Brian Haleye55ffac2006-07-10 15:25:51 -0700527
528 /*
529 * Each device address list is sorted in order of scope -
530 * global before linklocal.
531 */
532 for (ifap = &idev->addr_list; (ifa = *ifap) != NULL;
533 ifap = &ifa->if_next) {
YOSHIFUJI Hideaki8a6ce0c2006-07-11 13:05:30 -0700534 if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr))
Brian Haleye55ffac2006-07-10 15:25:51 -0700535 break;
536 }
537
538 ifp->if_next = *ifap;
539 *ifap = ifp;
540}
541
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542/* On success it returns ifp with increased reference count */
543
544static struct inet6_ifaddr *
545ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr, int pfxlen,
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -0700546 int scope, u32 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547{
548 struct inet6_ifaddr *ifa = NULL;
549 struct rt6_info *rt;
550 int hash;
551 int err = 0;
552
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -0700553 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 if (idev->dead) {
555 err = -ENODEV; /*XXX*/
556 goto out2;
557 }
558
559 write_lock(&addrconf_hash_lock);
560
561 /* Ignore adding duplicate addresses on an interface */
562 if (ipv6_chk_same_addr(addr, idev->dev)) {
563 ADBG(("ipv6_add_addr: already assigned\n"));
564 err = -EEXIST;
565 goto out;
566 }
567
Ingo Oeser322f74a2006-03-20 23:01:47 -0800568 ifa = kzalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
570 if (ifa == NULL) {
571 ADBG(("ipv6_add_addr: malloc failed\n"));
572 err = -ENOBUFS;
573 goto out;
574 }
575
576 rt = addrconf_dst_alloc(idev, addr, 0);
577 if (IS_ERR(rt)) {
578 err = PTR_ERR(rt);
579 goto out;
580 }
581
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 ipv6_addr_copy(&ifa->addr, addr);
583
584 spin_lock_init(&ifa->lock);
585 init_timer(&ifa->timer);
586 ifa->timer.data = (unsigned long) ifa;
587 ifa->scope = scope;
588 ifa->prefix_len = pfxlen;
589 ifa->flags = flags | IFA_F_TENTATIVE;
590 ifa->cstamp = ifa->tstamp = jiffies;
591
Keir Fraser57f5f542006-08-29 02:43:49 -0700592 ifa->rt = rt;
593
Neil Horman95c385b2007-04-25 17:08:10 -0700594 /*
595 * part one of RFC 4429, section 3.3
596 * We should not configure an address as
597 * optimistic if we do not yet know the link
598 * layer address of our nexhop router
599 */
600
601 if (rt->rt6i_nexthop == NULL)
602 ifa->flags &= ~IFA_F_OPTIMISTIC;
603
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 ifa->idev = idev;
605 in6_dev_hold(idev);
606 /* For caller */
607 in6_ifa_hold(ifa);
608
609 /* Add to big hash table */
610 hash = ipv6_addr_hash(addr);
611
612 ifa->lst_next = inet6_addr_lst[hash];
613 inet6_addr_lst[hash] = ifa;
614 in6_ifa_hold(ifa);
615 write_unlock(&addrconf_hash_lock);
616
617 write_lock(&idev->lock);
618 /* Add to inet6_dev unicast addr list. */
Brian Haleye55ffac2006-07-10 15:25:51 -0700619 ipv6_link_dev_addr(idev, ifa);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
621#ifdef CONFIG_IPV6_PRIVACY
622 if (ifa->flags&IFA_F_TEMPORARY) {
623 ifa->tmp_next = idev->tempaddr_list;
624 idev->tempaddr_list = ifa;
625 in6_ifa_hold(ifa);
626 }
627#endif
628
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 in6_ifa_hold(ifa);
630 write_unlock(&idev->lock);
631out2:
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -0700632 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
YOSHIFUJI Hideakifd928332005-04-19 22:27:09 -0700634 if (likely(err == 0))
Alan Sterne041c682006-03-27 01:16:30 -0800635 atomic_notifier_call_chain(&inet6addr_chain, NETDEV_UP, ifa);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 else {
637 kfree(ifa);
638 ifa = ERR_PTR(err);
639 }
640
641 return ifa;
642out:
643 write_unlock(&addrconf_hash_lock);
644 goto out2;
645}
646
647/* This function wants to get referenced ifp and releases it before return */
648
649static void ipv6_del_addr(struct inet6_ifaddr *ifp)
650{
651 struct inet6_ifaddr *ifa, **ifap;
652 struct inet6_dev *idev = ifp->idev;
653 int hash;
654 int deleted = 0, onlink = 0;
655 unsigned long expires = jiffies;
656
657 hash = ipv6_addr_hash(&ifp->addr);
658
659 ifp->dead = 1;
660
661 write_lock_bh(&addrconf_hash_lock);
662 for (ifap = &inet6_addr_lst[hash]; (ifa=*ifap) != NULL;
663 ifap = &ifa->lst_next) {
664 if (ifa == ifp) {
665 *ifap = ifa->lst_next;
666 __in6_ifa_put(ifp);
667 ifa->lst_next = NULL;
668 break;
669 }
670 }
671 write_unlock_bh(&addrconf_hash_lock);
672
673 write_lock_bh(&idev->lock);
674#ifdef CONFIG_IPV6_PRIVACY
675 if (ifp->flags&IFA_F_TEMPORARY) {
676 for (ifap = &idev->tempaddr_list; (ifa=*ifap) != NULL;
677 ifap = &ifa->tmp_next) {
678 if (ifa == ifp) {
679 *ifap = ifa->tmp_next;
680 if (ifp->ifpub) {
681 in6_ifa_put(ifp->ifpub);
682 ifp->ifpub = NULL;
683 }
684 __in6_ifa_put(ifp);
685 ifa->tmp_next = NULL;
686 break;
687 }
688 }
689 }
690#endif
691
Kristian Slavov1d142802005-12-21 18:47:24 -0800692 for (ifap = &idev->addr_list; (ifa=*ifap) != NULL;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 if (ifa == ifp) {
694 *ifap = ifa->if_next;
695 __in6_ifa_put(ifp);
696 ifa->if_next = NULL;
697 if (!(ifp->flags & IFA_F_PERMANENT) || onlink > 0)
698 break;
699 deleted = 1;
Kristian Slavov1d142802005-12-21 18:47:24 -0800700 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 } else if (ifp->flags & IFA_F_PERMANENT) {
702 if (ipv6_prefix_equal(&ifa->addr, &ifp->addr,
703 ifp->prefix_len)) {
704 if (ifa->flags & IFA_F_PERMANENT) {
705 onlink = 1;
706 if (deleted)
707 break;
708 } else {
709 unsigned long lifetime;
710
711 if (!onlink)
712 onlink = -1;
713
714 spin_lock(&ifa->lock);
715 lifetime = min_t(unsigned long,
716 ifa->valid_lft, 0x7fffffffUL/HZ);
717 if (time_before(expires,
718 ifa->tstamp + lifetime * HZ))
719 expires = ifa->tstamp + lifetime * HZ;
720 spin_unlock(&ifa->lock);
721 }
722 }
723 }
Kristian Slavov1d142802005-12-21 18:47:24 -0800724 ifap = &ifa->if_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 }
726 write_unlock_bh(&idev->lock);
727
728 ipv6_ifa_notify(RTM_DELADDR, ifp);
729
Alan Sterne041c682006-03-27 01:16:30 -0800730 atomic_notifier_call_chain(&inet6addr_chain, NETDEV_DOWN, ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
732 addrconf_del_timer(ifp);
733
734 /*
735 * Purge or update corresponding prefix
736 *
737 * 1) we don't purge prefix here if address was not permanent.
738 * prefix is managed by its own lifetime.
739 * 2) if there're no addresses, delete prefix.
740 * 3) if there're still other permanent address(es),
741 * corresponding prefix is still permanent.
742 * 4) otherwise, update prefix lifetime to the
743 * longest valid lifetime among the corresponding
744 * addresses on the device.
745 * Note: subsequent RA will update lifetime.
746 *
747 * --yoshfuji
748 */
749 if ((ifp->flags & IFA_F_PERMANENT) && onlink < 1) {
750 struct in6_addr prefix;
751 struct rt6_info *rt;
752
753 ipv6_addr_prefix(&prefix, &ifp->addr, ifp->prefix_len);
754 rt = rt6_lookup(&prefix, NULL, ifp->idev->dev->ifindex, 1);
755
756 if (rt && ((rt->rt6i_flags & (RTF_GATEWAY | RTF_DEFAULT)) == 0)) {
757 if (onlink == 0) {
Thomas Grafe0a1ad732006-08-22 00:00:21 -0700758 ip6_del_rt(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 rt = NULL;
760 } else if (!(rt->rt6i_flags & RTF_EXPIRES)) {
761 rt->rt6i_expires = expires;
762 rt->rt6i_flags |= RTF_EXPIRES;
763 }
764 }
765 dst_release(&rt->u.dst);
766 }
767
768 in6_ifa_put(ifp);
769}
770
771#ifdef CONFIG_IPV6_PRIVACY
772static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift)
773{
774 struct inet6_dev *idev = ifp->idev;
775 struct in6_addr addr, *tmpaddr;
776 unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_cstamp, tmp_tstamp;
777 int tmp_plen;
778 int ret = 0;
779 int max_addresses;
Neil Horman95c385b2007-04-25 17:08:10 -0700780 u32 addr_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
782 write_lock(&idev->lock);
783 if (ift) {
784 spin_lock_bh(&ift->lock);
785 memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8);
786 spin_unlock_bh(&ift->lock);
787 tmpaddr = &addr;
788 } else {
789 tmpaddr = NULL;
790 }
791retry:
792 in6_dev_hold(idev);
793 if (idev->cnf.use_tempaddr <= 0) {
794 write_unlock(&idev->lock);
795 printk(KERN_INFO
796 "ipv6_create_tempaddr(): use_tempaddr is disabled.\n");
797 in6_dev_put(idev);
798 ret = -1;
799 goto out;
800 }
801 spin_lock_bh(&ifp->lock);
802 if (ifp->regen_count++ >= idev->cnf.regen_max_retry) {
803 idev->cnf.use_tempaddr = -1; /*XXX*/
804 spin_unlock_bh(&ifp->lock);
805 write_unlock(&idev->lock);
806 printk(KERN_WARNING
807 "ipv6_create_tempaddr(): regeneration time exceeded. disabled temporary address support.\n");
808 in6_dev_put(idev);
809 ret = -1;
810 goto out;
811 }
812 in6_ifa_hold(ifp);
813 memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
814 if (__ipv6_try_regen_rndid(idev, tmpaddr) < 0) {
815 spin_unlock_bh(&ifp->lock);
816 write_unlock(&idev->lock);
817 printk(KERN_WARNING
818 "ipv6_create_tempaddr(): regeneration of randomized interface id failed.\n");
819 in6_ifa_put(ifp);
820 in6_dev_put(idev);
821 ret = -1;
822 goto out;
823 }
824 memcpy(&addr.s6_addr[8], idev->rndid, 8);
825 tmp_valid_lft = min_t(__u32,
826 ifp->valid_lft,
827 idev->cnf.temp_valid_lft);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900828 tmp_prefered_lft = min_t(__u32,
829 ifp->prefered_lft,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 idev->cnf.temp_prefered_lft - desync_factor / HZ);
831 tmp_plen = ifp->prefix_len;
832 max_addresses = idev->cnf.max_addresses;
833 tmp_cstamp = ifp->cstamp;
834 tmp_tstamp = ifp->tstamp;
835 spin_unlock_bh(&ifp->lock);
836
837 write_unlock(&idev->lock);
Neil Horman95c385b2007-04-25 17:08:10 -0700838
839 addr_flags = IFA_F_TEMPORARY;
840 /* set in addrconf_prefix_rcv() */
841 if (ifp->flags & IFA_F_OPTIMISTIC)
842 addr_flags |= IFA_F_OPTIMISTIC;
843
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 ift = !max_addresses ||
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900845 ipv6_count_addresses(idev) < max_addresses ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 ipv6_add_addr(idev, &addr, tmp_plen,
Neil Horman95c385b2007-04-25 17:08:10 -0700847 ipv6_addr_type(&addr)&IPV6_ADDR_SCOPE_MASK,
848 addr_flags) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 if (!ift || IS_ERR(ift)) {
850 in6_ifa_put(ifp);
851 in6_dev_put(idev);
852 printk(KERN_INFO
853 "ipv6_create_tempaddr(): retry temporary address regeneration.\n");
854 tmpaddr = &addr;
855 write_lock(&idev->lock);
856 goto retry;
857 }
858
859 spin_lock_bh(&ift->lock);
860 ift->ifpub = ifp;
861 ift->valid_lft = tmp_valid_lft;
862 ift->prefered_lft = tmp_prefered_lft;
863 ift->cstamp = tmp_cstamp;
864 ift->tstamp = tmp_tstamp;
865 spin_unlock_bh(&ift->lock);
866
867 addrconf_dad_start(ift, 0);
868 in6_ifa_put(ift);
869 in6_dev_put(idev);
870out:
871 return ret;
872}
873#endif
874
875/*
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800876 * Choose an appropriate source address (RFC3484)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 */
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800878struct ipv6_saddr_score {
879 int addr_type;
880 unsigned int attrs;
881 int matchlen;
Brian Haley0d27b422006-03-11 18:50:14 -0800882 int scope;
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800883 unsigned int rule;
884};
885
886#define IPV6_SADDR_SCORE_LOCAL 0x0001
887#define IPV6_SADDR_SCORE_PREFERRED 0x0004
888#define IPV6_SADDR_SCORE_HOA 0x0008
889#define IPV6_SADDR_SCORE_OIF 0x0010
890#define IPV6_SADDR_SCORE_LABEL 0x0020
891#define IPV6_SADDR_SCORE_PRIVACY 0x0040
892
Dave Jonesb6f99a22007-03-22 12:27:49 -0700893static inline int ipv6_saddr_preferred(int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894{
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800895 if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|
896 IPV6_ADDR_LOOPBACK|IPV6_ADDR_RESERVED))
897 return 1;
898 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899}
900
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800901int ipv6_dev_get_saddr(struct net_device *daddr_dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 struct in6_addr *daddr, struct in6_addr *saddr)
903{
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800904 struct ipv6_saddr_score hiscore;
905 struct inet6_ifaddr *ifa_result = NULL;
906 int daddr_type = __ipv6_addr_type(daddr);
907 int daddr_scope = __ipv6_addr_src_scope(daddr_type);
YOSHIFUJI Hideaki303065a2007-11-14 15:56:15 +0900908 int daddr_ifindex = daddr_dev ? daddr_dev->ifindex : 0;
909 u32 daddr_label = ipv6_addr_label(daddr, daddr_type, daddr_ifindex);
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800910 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800912 memset(&hiscore, 0, sizeof(hiscore));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
914 read_lock(&dev_base_lock);
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -0700915 rcu_read_lock();
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800916
Eric W. Biederman881d9662007-09-17 11:56:21 -0700917 for_each_netdev(&init_net, dev) {
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800918 struct inet6_dev *idev;
919 struct inet6_ifaddr *ifa;
920
921 /* Rule 0: Candidate Source Address (section 4)
922 * - multicast and link-local destination address,
923 * the set of candidate source address MUST only
924 * include addresses assigned to interfaces
925 * belonging to the same link as the outgoing
926 * interface.
927 * (- For site-local destination addresses, the
928 * set of candidate source addresses MUST only
929 * include addresses assigned to interfaces
930 * belonging to the same site as the outgoing
931 * interface.)
932 */
933 if ((daddr_type & IPV6_ADDR_MULTICAST ||
934 daddr_scope <= IPV6_ADDR_SCOPE_LINKLOCAL) &&
935 daddr_dev && dev != daddr_dev)
936 continue;
937
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 idev = __in6_dev_get(dev);
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800939 if (!idev)
940 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800942 read_lock_bh(&idev->lock);
943 for (ifa = idev->addr_list; ifa; ifa = ifa->if_next) {
944 struct ipv6_saddr_score score;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800946 score.addr_type = __ipv6_addr_type(&ifa->addr);
947
YOSHIFUJI Hideaki6b3ae802005-12-21 22:58:01 +0900948 /* Rule 0:
949 * - Tentative Address (RFC2462 section 5.4)
950 * - A tentative address is not considered
951 * "assigned to an interface" in the traditional
Neil Horman95c385b2007-04-25 17:08:10 -0700952 * sense, unless it is also flagged as optimistic.
YOSHIFUJI Hideaki6b3ae802005-12-21 22:58:01 +0900953 * - Candidate Source Address (section 4)
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800954 * - In any case, anycast addresses, multicast
955 * addresses, and the unspecified address MUST
956 * NOT be included in a candidate set.
957 */
Neil Horman95c385b2007-04-25 17:08:10 -0700958 if ((ifa->flags & IFA_F_TENTATIVE) &&
959 (!(ifa->flags & IFA_F_OPTIMISTIC)))
YOSHIFUJI Hideaki6b3ae802005-12-21 22:58:01 +0900960 continue;
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800961 if (unlikely(score.addr_type == IPV6_ADDR_ANY ||
962 score.addr_type & IPV6_ADDR_MULTICAST)) {
963 LIMIT_NETDEBUG(KERN_DEBUG
Joe Perches3b6d821c2007-11-19 23:47:25 -0800964 "ADDRCONF: unspecified / multicast address "
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800965 "assigned as unicast address on %s",
966 dev->name);
967 continue;
968 }
969
970 score.attrs = 0;
971 score.matchlen = 0;
972 score.scope = 0;
973 score.rule = 0;
974
975 if (ifa_result == NULL) {
976 /* record it if the first available entry */
977 goto record_it;
978 }
979
980 /* Rule 1: Prefer same address */
981 if (hiscore.rule < 1) {
982 if (ipv6_addr_equal(&ifa_result->addr, daddr))
983 hiscore.attrs |= IPV6_SADDR_SCORE_LOCAL;
984 hiscore.rule++;
985 }
986 if (ipv6_addr_equal(&ifa->addr, daddr)) {
987 score.attrs |= IPV6_SADDR_SCORE_LOCAL;
988 if (!(hiscore.attrs & IPV6_SADDR_SCORE_LOCAL)) {
989 score.rule = 1;
990 goto record_it;
991 }
992 } else {
993 if (hiscore.attrs & IPV6_SADDR_SCORE_LOCAL)
994 continue;
995 }
996
997 /* Rule 2: Prefer appropriate scope */
998 if (hiscore.rule < 2) {
999 hiscore.scope = __ipv6_addr_src_scope(hiscore.addr_type);
1000 hiscore.rule++;
1001 }
1002 score.scope = __ipv6_addr_src_scope(score.addr_type);
1003 if (hiscore.scope < score.scope) {
1004 if (hiscore.scope < daddr_scope) {
1005 score.rule = 2;
1006 goto record_it;
1007 } else
1008 continue;
1009 } else if (score.scope < hiscore.scope) {
1010 if (score.scope < daddr_scope)
Brian Haleye55ffac2006-07-10 15:25:51 -07001011 break; /* addresses sorted by scope */
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001012 else {
1013 score.rule = 2;
1014 goto record_it;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 }
1016 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
Neil Horman95c385b2007-04-25 17:08:10 -07001018 /* Rule 3: Avoid deprecated and optimistic addresses */
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001019 if (hiscore.rule < 3) {
1020 if (ipv6_saddr_preferred(hiscore.addr_type) ||
Neil Horman95c385b2007-04-25 17:08:10 -07001021 (((ifa_result->flags &
1022 (IFA_F_DEPRECATED|IFA_F_OPTIMISTIC)) == 0)))
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001023 hiscore.attrs |= IPV6_SADDR_SCORE_PREFERRED;
1024 hiscore.rule++;
1025 }
1026 if (ipv6_saddr_preferred(score.addr_type) ||
Jiri Kosina6ae5f982007-09-16 14:48:21 -07001027 (((ifa->flags &
Neil Horman95c385b2007-04-25 17:08:10 -07001028 (IFA_F_DEPRECATED|IFA_F_OPTIMISTIC)) == 0))) {
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001029 score.attrs |= IPV6_SADDR_SCORE_PREFERRED;
1030 if (!(hiscore.attrs & IPV6_SADDR_SCORE_PREFERRED)) {
1031 score.rule = 3;
1032 goto record_it;
1033 }
1034 } else {
1035 if (hiscore.attrs & IPV6_SADDR_SCORE_PREFERRED)
1036 continue;
1037 }
1038
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07001039 /* Rule 4: Prefer home address */
Masahide NAKAMURA59fbb3a62007-06-26 23:56:32 -07001040#if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07001041 if (hiscore.rule < 4) {
1042 if (ifa_result->flags & IFA_F_HOMEADDRESS)
1043 hiscore.attrs |= IPV6_SADDR_SCORE_HOA;
1044 hiscore.rule++;
1045 }
1046 if (ifa->flags & IFA_F_HOMEADDRESS) {
1047 score.attrs |= IPV6_SADDR_SCORE_HOA;
1048 if (!(ifa_result->flags & IFA_F_HOMEADDRESS)) {
1049 score.rule = 4;
1050 goto record_it;
1051 }
1052 } else {
1053 if (hiscore.attrs & IPV6_SADDR_SCORE_HOA)
1054 continue;
1055 }
1056#else
YOSHIFUJI Hideaki220bbd72005-11-28 22:27:11 -08001057 if (hiscore.rule < 4)
1058 hiscore.rule++;
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07001059#endif
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001060
1061 /* Rule 5: Prefer outgoing interface */
1062 if (hiscore.rule < 5) {
1063 if (daddr_dev == NULL ||
1064 daddr_dev == ifa_result->idev->dev)
1065 hiscore.attrs |= IPV6_SADDR_SCORE_OIF;
1066 hiscore.rule++;
1067 }
1068 if (daddr_dev == NULL ||
1069 daddr_dev == ifa->idev->dev) {
1070 score.attrs |= IPV6_SADDR_SCORE_OIF;
1071 if (!(hiscore.attrs & IPV6_SADDR_SCORE_OIF)) {
1072 score.rule = 5;
1073 goto record_it;
1074 }
1075 } else {
1076 if (hiscore.attrs & IPV6_SADDR_SCORE_OIF)
1077 continue;
1078 }
1079
1080 /* Rule 6: Prefer matching label */
1081 if (hiscore.rule < 6) {
YOSHIFUJI Hideakic1ee6562007-11-14 15:55:29 +09001082 if (ipv6_addr_label(&ifa_result->addr,
YOSHIFUJI Hideaki303065a2007-11-14 15:56:15 +09001083 hiscore.addr_type,
1084 ifa_result->idev->dev->ifindex) == daddr_label)
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001085 hiscore.attrs |= IPV6_SADDR_SCORE_LABEL;
1086 hiscore.rule++;
1087 }
YOSHIFUJI Hideakic1ee6562007-11-14 15:55:29 +09001088 if (ipv6_addr_label(&ifa->addr,
YOSHIFUJI Hideaki303065a2007-11-14 15:56:15 +09001089 score.addr_type,
1090 ifa->idev->dev->ifindex) == daddr_label) {
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001091 score.attrs |= IPV6_SADDR_SCORE_LABEL;
1092 if (!(hiscore.attrs & IPV6_SADDR_SCORE_LABEL)) {
1093 score.rule = 6;
1094 goto record_it;
1095 }
1096 } else {
1097 if (hiscore.attrs & IPV6_SADDR_SCORE_LABEL)
1098 continue;
1099 }
1100
Peter Chubb44fd0262005-11-09 13:05:47 -08001101#ifdef CONFIG_IPV6_PRIVACY
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001102 /* Rule 7: Prefer public address
1103 * Note: prefer temprary address if use_tempaddr >= 2
1104 */
1105 if (hiscore.rule < 7) {
1106 if ((!(ifa_result->flags & IFA_F_TEMPORARY)) ^
1107 (ifa_result->idev->cnf.use_tempaddr >= 2))
1108 hiscore.attrs |= IPV6_SADDR_SCORE_PRIVACY;
1109 hiscore.rule++;
1110 }
1111 if ((!(ifa->flags & IFA_F_TEMPORARY)) ^
1112 (ifa->idev->cnf.use_tempaddr >= 2)) {
1113 score.attrs |= IPV6_SADDR_SCORE_PRIVACY;
1114 if (!(hiscore.attrs & IPV6_SADDR_SCORE_PRIVACY)) {
1115 score.rule = 7;
1116 goto record_it;
1117 }
1118 } else {
1119 if (hiscore.attrs & IPV6_SADDR_SCORE_PRIVACY)
1120 continue;
1121 }
YOSHIFUJI Hideaki5e2707f2006-06-22 01:41:18 -07001122#else
1123 if (hiscore.rule < 7)
1124 hiscore.rule++;
Peter Chubb44fd0262005-11-09 13:05:47 -08001125#endif
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001126 /* Rule 8: Use longest matching prefix */
Yan Zheng12da2a42005-11-14 21:42:46 -08001127 if (hiscore.rule < 8) {
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001128 hiscore.matchlen = ipv6_addr_diff(&ifa_result->addr, daddr);
Yan Zheng12da2a42005-11-14 21:42:46 -08001129 hiscore.rule++;
1130 }
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001131 score.matchlen = ipv6_addr_diff(&ifa->addr, daddr);
1132 if (score.matchlen > hiscore.matchlen) {
1133 score.rule = 8;
1134 goto record_it;
1135 }
1136#if 0
1137 else if (score.matchlen < hiscore.matchlen)
1138 continue;
1139#endif
1140
1141 /* Final Rule: choose first available one */
1142 continue;
1143record_it:
1144 if (ifa_result)
1145 in6_ifa_put(ifa_result);
1146 in6_ifa_hold(ifa);
1147 ifa_result = ifa;
1148 hiscore = score;
1149 }
1150 read_unlock_bh(&idev->lock);
1151 }
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001152 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 read_unlock(&dev_base_lock);
1154
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001155 if (!ifa_result)
1156 return -EADDRNOTAVAIL;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001157
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001158 ipv6_addr_copy(saddr, &ifa_result->addr);
1159 in6_ifa_put(ifa_result);
1160 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161}
1162
1163
1164int ipv6_get_saddr(struct dst_entry *dst,
1165 struct in6_addr *daddr, struct in6_addr *saddr)
1166{
YOSHIFUJI Hideaki7a3025b2006-10-13 16:17:25 +09001167 return ipv6_dev_get_saddr(dst ? ip6_dst_idev(dst)->dev : NULL, daddr, saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168}
1169
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +09001170EXPORT_SYMBOL(ipv6_get_saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
Neil Horman95c385b2007-04-25 17:08:10 -07001172int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr,
1173 unsigned char banned_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174{
1175 struct inet6_dev *idev;
1176 int err = -EADDRNOTAVAIL;
1177
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001178 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 if ((idev = __in6_dev_get(dev)) != NULL) {
1180 struct inet6_ifaddr *ifp;
1181
1182 read_lock_bh(&idev->lock);
1183 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
Neil Horman95c385b2007-04-25 17:08:10 -07001184 if (ifp->scope == IFA_LINK && !(ifp->flags & banned_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 ipv6_addr_copy(addr, &ifp->addr);
1186 err = 0;
1187 break;
1188 }
1189 }
1190 read_unlock_bh(&idev->lock);
1191 }
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001192 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 return err;
1194}
1195
1196static int ipv6_count_addresses(struct inet6_dev *idev)
1197{
1198 int cnt = 0;
1199 struct inet6_ifaddr *ifp;
1200
1201 read_lock_bh(&idev->lock);
1202 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next)
1203 cnt++;
1204 read_unlock_bh(&idev->lock);
1205 return cnt;
1206}
1207
1208int ipv6_chk_addr(struct in6_addr *addr, struct net_device *dev, int strict)
1209{
1210 struct inet6_ifaddr * ifp;
1211 u8 hash = ipv6_addr_hash(addr);
1212
1213 read_lock_bh(&addrconf_hash_lock);
1214 for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
1215 if (ipv6_addr_equal(&ifp->addr, addr) &&
1216 !(ifp->flags&IFA_F_TENTATIVE)) {
1217 if (dev == NULL || ifp->idev->dev == dev ||
1218 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))
1219 break;
1220 }
1221 }
1222 read_unlock_bh(&addrconf_hash_lock);
1223 return ifp != NULL;
1224}
1225
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +09001226EXPORT_SYMBOL(ipv6_chk_addr);
1227
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228static
1229int ipv6_chk_same_addr(const struct in6_addr *addr, struct net_device *dev)
1230{
1231 struct inet6_ifaddr * ifp;
1232 u8 hash = ipv6_addr_hash(addr);
1233
1234 for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
1235 if (ipv6_addr_equal(&ifp->addr, addr)) {
1236 if (dev == NULL || ifp->idev->dev == dev)
1237 break;
1238 }
1239 }
1240 return ifp != NULL;
1241}
1242
1243struct inet6_ifaddr * ipv6_get_ifaddr(struct in6_addr *addr, struct net_device *dev, int strict)
1244{
1245 struct inet6_ifaddr * ifp;
1246 u8 hash = ipv6_addr_hash(addr);
1247
1248 read_lock_bh(&addrconf_hash_lock);
1249 for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
1250 if (ipv6_addr_equal(&ifp->addr, addr)) {
1251 if (dev == NULL || ifp->idev->dev == dev ||
1252 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
1253 in6_ifa_hold(ifp);
1254 break;
1255 }
1256 }
1257 }
1258 read_unlock_bh(&addrconf_hash_lock);
1259
1260 return ifp;
1261}
1262
1263int ipv6_rcv_saddr_equal(const struct sock *sk, const struct sock *sk2)
1264{
1265 const struct in6_addr *sk_rcv_saddr6 = &inet6_sk(sk)->rcv_saddr;
Arnaldo Carvalho de Melo0fa1a532005-12-13 23:23:09 -08001266 const struct in6_addr *sk2_rcv_saddr6 = inet6_rcv_saddr(sk2);
Al Viro82103232006-09-27 18:44:10 -07001267 __be32 sk_rcv_saddr = inet_sk(sk)->rcv_saddr;
1268 __be32 sk2_rcv_saddr = inet_rcv_saddr(sk2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 int sk_ipv6only = ipv6_only_sock(sk);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001270 int sk2_ipv6only = inet_v6_ipv6only(sk2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 int addr_type = ipv6_addr_type(sk_rcv_saddr6);
1272 int addr_type2 = sk2_rcv_saddr6 ? ipv6_addr_type(sk2_rcv_saddr6) : IPV6_ADDR_MAPPED;
1273
1274 if (!sk2_rcv_saddr && !sk_ipv6only)
1275 return 1;
1276
1277 if (addr_type2 == IPV6_ADDR_ANY &&
1278 !(sk2_ipv6only && addr_type == IPV6_ADDR_MAPPED))
1279 return 1;
1280
1281 if (addr_type == IPV6_ADDR_ANY &&
1282 !(sk_ipv6only && addr_type2 == IPV6_ADDR_MAPPED))
1283 return 1;
1284
1285 if (sk2_rcv_saddr6 &&
1286 ipv6_addr_equal(sk_rcv_saddr6, sk2_rcv_saddr6))
1287 return 1;
1288
1289 if (addr_type == IPV6_ADDR_MAPPED &&
1290 !sk2_ipv6only &&
1291 (!sk2_rcv_saddr || !sk_rcv_saddr || sk_rcv_saddr == sk2_rcv_saddr))
1292 return 1;
1293
1294 return 0;
1295}
1296
1297/* Gets referenced address, destroys ifaddr */
1298
Adrian Bunk9f5336e2006-01-07 13:24:25 -08001299static void addrconf_dad_stop(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 if (ifp->flags&IFA_F_PERMANENT) {
1302 spin_lock_bh(&ifp->lock);
1303 addrconf_del_timer(ifp);
1304 ifp->flags |= IFA_F_TENTATIVE;
1305 spin_unlock_bh(&ifp->lock);
1306 in6_ifa_put(ifp);
1307#ifdef CONFIG_IPV6_PRIVACY
1308 } else if (ifp->flags&IFA_F_TEMPORARY) {
1309 struct inet6_ifaddr *ifpub;
1310 spin_lock_bh(&ifp->lock);
1311 ifpub = ifp->ifpub;
1312 if (ifpub) {
1313 in6_ifa_hold(ifpub);
1314 spin_unlock_bh(&ifp->lock);
1315 ipv6_create_tempaddr(ifpub, ifp);
1316 in6_ifa_put(ifpub);
1317 } else {
1318 spin_unlock_bh(&ifp->lock);
1319 }
1320 ipv6_del_addr(ifp);
1321#endif
1322 } else
1323 ipv6_del_addr(ifp);
1324}
1325
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09001326void addrconf_dad_failure(struct inet6_ifaddr *ifp)
1327{
1328 if (net_ratelimit())
1329 printk(KERN_INFO "%s: duplicate address detected!\n", ifp->idev->dev->name);
1330 addrconf_dad_stop(ifp);
1331}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
1333/* Join to solicited addr multicast group. */
1334
1335void addrconf_join_solict(struct net_device *dev, struct in6_addr *addr)
1336{
1337 struct in6_addr maddr;
1338
1339 if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1340 return;
1341
1342 addrconf_addr_solict_mult(addr, &maddr);
1343 ipv6_dev_mc_inc(dev, &maddr);
1344}
1345
1346void addrconf_leave_solict(struct inet6_dev *idev, struct in6_addr *addr)
1347{
1348 struct in6_addr maddr;
1349
1350 if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1351 return;
1352
1353 addrconf_addr_solict_mult(addr, &maddr);
1354 __ipv6_dev_mc_dec(idev, &maddr);
1355}
1356
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03001357static void addrconf_join_anycast(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358{
1359 struct in6_addr addr;
1360 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1361 if (ipv6_addr_any(&addr))
1362 return;
1363 ipv6_dev_ac_inc(ifp->idev->dev, &addr);
1364}
1365
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03001366static void addrconf_leave_anycast(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367{
1368 struct in6_addr addr;
1369 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1370 if (ipv6_addr_any(&addr))
1371 return;
1372 __ipv6_dev_ac_dec(ifp->idev, &addr);
1373}
1374
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08001375static int addrconf_ifid_eui48(u8 *eui, struct net_device *dev)
1376{
1377 if (dev->addr_len != ETH_ALEN)
1378 return -1;
1379 memcpy(eui, dev->dev_addr, 3);
1380 memcpy(eui + 5, dev->dev_addr + 3, 3);
1381
1382 /*
1383 * The zSeries OSA network cards can be shared among various
1384 * OS instances, but the OSA cards have only one MAC address.
1385 * This leads to duplicate address conflicts in conjunction
1386 * with IPv6 if more than one instance uses the same card.
1387 *
1388 * The driver for these cards can deliver a unique 16-bit
1389 * identifier for each instance sharing the same card. It is
1390 * placed instead of 0xFFFE in the interface identifier. The
1391 * "u" bit of the interface identifier is not inverted in this
1392 * case. Hence the resulting interface identifier has local
1393 * scope according to RFC2373.
1394 */
1395 if (dev->dev_id) {
1396 eui[3] = (dev->dev_id >> 8) & 0xFF;
1397 eui[4] = dev->dev_id & 0xFF;
1398 } else {
1399 eui[3] = 0xFF;
1400 eui[4] = 0xFE;
1401 eui[0] ^= 2;
1402 }
1403 return 0;
1404}
1405
1406static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev)
1407{
1408 /* XXX: inherit EUI-64 from other interface -- yoshfuji */
1409 if (dev->addr_len != ARCNET_ALEN)
1410 return -1;
1411 memset(eui, 0, 7);
1412 eui[7] = *(u8*)dev->dev_addr;
1413 return 0;
1414}
1415
1416static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev)
1417{
1418 if (dev->addr_len != INFINIBAND_ALEN)
1419 return -1;
1420 memcpy(eui, dev->dev_addr + 12, 8);
1421 eui[0] |= 2;
1422 return 0;
1423}
1424
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
1426{
1427 switch (dev->type) {
1428 case ARPHRD_ETHER:
1429 case ARPHRD_FDDI:
1430 case ARPHRD_IEEE802_TR:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08001431 return addrconf_ifid_eui48(eui, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 case ARPHRD_ARCNET:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08001433 return addrconf_ifid_arcnet(eui, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 case ARPHRD_INFINIBAND:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08001435 return addrconf_ifid_infiniband(eui, dev);
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11001436 case ARPHRD_SIT:
1437 if (dev->priv_flags & IFF_ISATAP)
1438 return ipv6_isatap_eui64(eui, *(__be32 *)dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 }
1440 return -1;
1441}
1442
1443static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
1444{
1445 int err = -1;
1446 struct inet6_ifaddr *ifp;
1447
1448 read_lock_bh(&idev->lock);
1449 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
1450 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
1451 memcpy(eui, ifp->addr.s6_addr+8, 8);
1452 err = 0;
1453 break;
1454 }
1455 }
1456 read_unlock_bh(&idev->lock);
1457 return err;
1458}
1459
1460#ifdef CONFIG_IPV6_PRIVACY
1461/* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
1462static int __ipv6_regen_rndid(struct inet6_dev *idev)
1463{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464regen:
YOSHIFUJI Hideaki955189e2006-03-20 16:54:09 -08001465 get_random_bytes(idev->rndid, sizeof(idev->rndid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 idev->rndid[0] &= ~0x02;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
1468 /*
1469 * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
1470 * check if generated address is not inappropriate
1471 *
1472 * - Reserved subnet anycast (RFC 2526)
1473 * 11111101 11....11 1xxxxxxx
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11001474 * - ISATAP (RFC4214) 6.1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 * 00-00-5E-FE-xx-xx-xx-xx
1476 * - value 0
1477 * - XXX: already assigned to an address on the device
1478 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001479 if (idev->rndid[0] == 0xfd &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff &&
1481 (idev->rndid[7]&0x80))
1482 goto regen;
1483 if ((idev->rndid[0]|idev->rndid[1]) == 0) {
1484 if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe)
1485 goto regen;
1486 if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00)
1487 goto regen;
1488 }
1489
1490 return 0;
1491}
1492
1493static void ipv6_regen_rndid(unsigned long data)
1494{
1495 struct inet6_dev *idev = (struct inet6_dev *) data;
1496 unsigned long expires;
1497
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001498 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 write_lock_bh(&idev->lock);
1500
1501 if (idev->dead)
1502 goto out;
1503
1504 if (__ipv6_regen_rndid(idev) < 0)
1505 goto out;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001506
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 expires = jiffies +
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001508 idev->cnf.temp_prefered_lft * HZ -
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 idev->cnf.regen_max_retry * idev->cnf.dad_transmits * idev->nd_parms->retrans_time - desync_factor;
1510 if (time_before(expires, jiffies)) {
1511 printk(KERN_WARNING
1512 "ipv6_regen_rndid(): too short regeneration interval; timer disabled for %s.\n",
1513 idev->dev->name);
1514 goto out;
1515 }
1516
1517 if (!mod_timer(&idev->regen_timer, expires))
1518 in6_dev_hold(idev);
1519
1520out:
1521 write_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001522 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 in6_dev_put(idev);
1524}
1525
1526static int __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr) {
1527 int ret = 0;
1528
1529 if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0)
1530 ret = __ipv6_regen_rndid(idev);
1531 return ret;
1532}
1533#endif
1534
1535/*
1536 * Add prefix route.
1537 */
1538
1539static void
1540addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev,
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -07001541 unsigned long expires, u32 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542{
Thomas Graf86872cb2006-08-22 00:01:08 -07001543 struct fib6_config cfg = {
1544 .fc_table = RT6_TABLE_PREFIX,
1545 .fc_metric = IP6_RT_PRIO_ADDRCONF,
1546 .fc_ifindex = dev->ifindex,
1547 .fc_expires = expires,
1548 .fc_dst_len = plen,
1549 .fc_flags = RTF_UP | flags,
1550 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551
Thomas Graf86872cb2006-08-22 00:01:08 -07001552 ipv6_addr_copy(&cfg.fc_dst, pfx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553
1554 /* Prevent useless cloning on PtP SIT.
1555 This thing is done here expecting that the whole
1556 class of non-broadcast devices need not cloning.
1557 */
Joerg Roedel0be669b2006-10-10 14:49:53 -07001558#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
Thomas Graf86872cb2006-08-22 00:01:08 -07001559 if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT))
1560 cfg.fc_flags |= RTF_NONEXTHOP;
Joerg Roedel0be669b2006-10-10 14:49:53 -07001561#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562
Thomas Graf86872cb2006-08-22 00:01:08 -07001563 ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564}
1565
1566/* Create "default" multicast route to the interface */
1567
1568static void addrconf_add_mroute(struct net_device *dev)
1569{
Thomas Graf86872cb2006-08-22 00:01:08 -07001570 struct fib6_config cfg = {
1571 .fc_table = RT6_TABLE_LOCAL,
1572 .fc_metric = IP6_RT_PRIO_ADDRCONF,
1573 .fc_ifindex = dev->ifindex,
1574 .fc_dst_len = 8,
1575 .fc_flags = RTF_UP,
1576 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577
Thomas Graf86872cb2006-08-22 00:01:08 -07001578 ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0);
1579
1580 ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581}
1582
Joerg Roedel0be669b2006-10-10 14:49:53 -07001583#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584static void sit_route_add(struct net_device *dev)
1585{
Thomas Graf86872cb2006-08-22 00:01:08 -07001586 struct fib6_config cfg = {
1587 .fc_table = RT6_TABLE_MAIN,
1588 .fc_metric = IP6_RT_PRIO_ADDRCONF,
1589 .fc_ifindex = dev->ifindex,
1590 .fc_dst_len = 96,
1591 .fc_flags = RTF_UP | RTF_NONEXTHOP,
1592 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593
1594 /* prefix length - 96 bits "::d.d.d.d" */
Thomas Graf86872cb2006-08-22 00:01:08 -07001595 ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596}
Joerg Roedel0be669b2006-10-10 14:49:53 -07001597#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598
1599static void addrconf_add_lroute(struct net_device *dev)
1600{
1601 struct in6_addr addr;
1602
1603 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
1604 addrconf_prefix_route(&addr, 64, dev, 0, 0);
1605}
1606
1607static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
1608{
1609 struct inet6_dev *idev;
1610
1611 ASSERT_RTNL();
1612
1613 if ((idev = ipv6_find_idev(dev)) == NULL)
1614 return NULL;
1615
1616 /* Add default multicast route */
1617 addrconf_add_mroute(dev);
1618
1619 /* Add link local route */
1620 addrconf_add_lroute(dev);
1621 return idev;
1622}
1623
1624void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len)
1625{
1626 struct prefix_info *pinfo;
1627 __u32 valid_lft;
1628 __u32 prefered_lft;
1629 int addr_type;
1630 unsigned long rt_expires;
1631 struct inet6_dev *in6_dev;
1632
1633 pinfo = (struct prefix_info *) opt;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001634
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 if (len < sizeof(struct prefix_info)) {
1636 ADBG(("addrconf: prefix option too short\n"));
1637 return;
1638 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001639
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 /*
1641 * Validation checks ([ADDRCONF], page 19)
1642 */
1643
1644 addr_type = ipv6_addr_type(&pinfo->prefix);
1645
1646 if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
1647 return;
1648
1649 valid_lft = ntohl(pinfo->valid);
1650 prefered_lft = ntohl(pinfo->prefered);
1651
1652 if (prefered_lft > valid_lft) {
1653 if (net_ratelimit())
1654 printk(KERN_WARNING "addrconf: prefix option has invalid lifetime\n");
1655 return;
1656 }
1657
1658 in6_dev = in6_dev_get(dev);
1659
1660 if (in6_dev == NULL) {
1661 if (net_ratelimit())
1662 printk(KERN_DEBUG "addrconf: device %s not configured\n", dev->name);
1663 return;
1664 }
1665
1666 /*
1667 * Two things going on here:
1668 * 1) Add routes for on-link prefixes
1669 * 2) Configure prefixes with the auto flag set
1670 */
1671
1672 /* Avoid arithmetic overflow. Really, we could
1673 save rt_expires in seconds, likely valid_lft,
1674 but it would require division in fib gc, that it
1675 not good.
1676 */
1677 if (valid_lft >= 0x7FFFFFFF/HZ)
YOSHIFUJI Hideaki3dd4bc62005-12-19 14:02:45 -08001678 rt_expires = 0x7FFFFFFF - (0x7FFFFFFF % HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 else
YOSHIFUJI Hideaki3dd4bc62005-12-19 14:02:45 -08001680 rt_expires = valid_lft * HZ;
1681
1682 /*
1683 * We convert this (in jiffies) to clock_t later.
1684 * Avoid arithmetic overflow there as well.
1685 * Overflow can happen only if HZ < USER_HZ.
1686 */
1687 if (HZ < USER_HZ && rt_expires > 0x7FFFFFFF / USER_HZ)
1688 rt_expires = 0x7FFFFFFF / USER_HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689
1690 if (pinfo->onlink) {
1691 struct rt6_info *rt;
1692 rt = rt6_lookup(&pinfo->prefix, NULL, dev->ifindex, 1);
1693
1694 if (rt && ((rt->rt6i_flags & (RTF_GATEWAY | RTF_DEFAULT)) == 0)) {
1695 if (rt->rt6i_flags&RTF_EXPIRES) {
1696 if (valid_lft == 0) {
Thomas Grafe0a1ad732006-08-22 00:00:21 -07001697 ip6_del_rt(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 rt = NULL;
1699 } else {
YOSHIFUJI Hideaki3dd4bc62005-12-19 14:02:45 -08001700 rt->rt6i_expires = jiffies + rt_expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 }
1702 }
1703 } else if (valid_lft) {
1704 addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
YOSHIFUJI Hideaki3dd4bc62005-12-19 14:02:45 -08001705 dev, jiffies_to_clock_t(rt_expires), RTF_ADDRCONF|RTF_EXPIRES|RTF_PREFIX_RT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 }
1707 if (rt)
1708 dst_release(&rt->u.dst);
1709 }
1710
1711 /* Try to figure out our local address for this prefix */
1712
1713 if (pinfo->autoconf && in6_dev->cnf.autoconf) {
1714 struct inet6_ifaddr * ifp;
1715 struct in6_addr addr;
1716 int create = 0, update_lft = 0;
1717
1718 if (pinfo->prefix_len == 64) {
1719 memcpy(&addr, &pinfo->prefix, 8);
1720 if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
1721 ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
1722 in6_dev_put(in6_dev);
1723 return;
1724 }
1725 goto ok;
1726 }
1727 if (net_ratelimit())
1728 printk(KERN_DEBUG "IPv6 addrconf: prefix with wrong length %d\n",
1729 pinfo->prefix_len);
1730 in6_dev_put(in6_dev);
1731 return;
1732
1733ok:
1734
1735 ifp = ipv6_get_ifaddr(&addr, dev, 1);
1736
1737 if (ifp == NULL && valid_lft) {
1738 int max_addresses = in6_dev->cnf.max_addresses;
Neil Horman95c385b2007-04-25 17:08:10 -07001739 u32 addr_flags = 0;
1740
1741#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1742 if (in6_dev->cnf.optimistic_dad &&
1743 !ipv6_devconf.forwarding)
1744 addr_flags = IFA_F_OPTIMISTIC;
1745#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746
1747 /* Do not allow to create too much of autoconfigured
1748 * addresses; this would be too easy way to crash kernel.
1749 */
1750 if (!max_addresses ||
1751 ipv6_count_addresses(in6_dev) < max_addresses)
1752 ifp = ipv6_add_addr(in6_dev, &addr, pinfo->prefix_len,
Neil Horman95c385b2007-04-25 17:08:10 -07001753 addr_type&IPV6_ADDR_SCOPE_MASK,
1754 addr_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755
1756 if (!ifp || IS_ERR(ifp)) {
1757 in6_dev_put(in6_dev);
1758 return;
1759 }
1760
1761 update_lft = create = 1;
1762 ifp->cstamp = jiffies;
1763 addrconf_dad_start(ifp, RTF_ADDRCONF|RTF_PREFIX_RT);
1764 }
1765
1766 if (ifp) {
1767 int flags;
1768 unsigned long now;
1769#ifdef CONFIG_IPV6_PRIVACY
1770 struct inet6_ifaddr *ift;
1771#endif
1772 u32 stored_lft;
1773
1774 /* update lifetime (RFC2462 5.5.3 e) */
1775 spin_lock(&ifp->lock);
1776 now = jiffies;
1777 if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
1778 stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
1779 else
1780 stored_lft = 0;
1781 if (!update_lft && stored_lft) {
1782 if (valid_lft > MIN_VALID_LIFETIME ||
1783 valid_lft > stored_lft)
1784 update_lft = 1;
1785 else if (stored_lft <= MIN_VALID_LIFETIME) {
1786 /* valid_lft <= stored_lft is always true */
1787 /* XXX: IPsec */
1788 update_lft = 0;
1789 } else {
1790 valid_lft = MIN_VALID_LIFETIME;
1791 if (valid_lft < prefered_lft)
1792 prefered_lft = valid_lft;
1793 update_lft = 1;
1794 }
1795 }
1796
1797 if (update_lft) {
1798 ifp->valid_lft = valid_lft;
1799 ifp->prefered_lft = prefered_lft;
1800 ifp->tstamp = now;
1801 flags = ifp->flags;
1802 ifp->flags &= ~IFA_F_DEPRECATED;
1803 spin_unlock(&ifp->lock);
1804
1805 if (!(flags&IFA_F_TENTATIVE))
1806 ipv6_ifa_notify(0, ifp);
1807 } else
1808 spin_unlock(&ifp->lock);
1809
1810#ifdef CONFIG_IPV6_PRIVACY
1811 read_lock_bh(&in6_dev->lock);
1812 /* update all temporary addresses in the list */
1813 for (ift=in6_dev->tempaddr_list; ift; ift=ift->tmp_next) {
1814 /*
1815 * When adjusting the lifetimes of an existing
1816 * temporary address, only lower the lifetimes.
1817 * Implementations must not increase the
1818 * lifetimes of an existing temporary address
1819 * when processing a Prefix Information Option.
1820 */
1821 spin_lock(&ift->lock);
1822 flags = ift->flags;
1823 if (ift->valid_lft > valid_lft &&
1824 ift->valid_lft - valid_lft > (jiffies - ift->tstamp) / HZ)
1825 ift->valid_lft = valid_lft + (jiffies - ift->tstamp) / HZ;
1826 if (ift->prefered_lft > prefered_lft &&
1827 ift->prefered_lft - prefered_lft > (jiffies - ift->tstamp) / HZ)
1828 ift->prefered_lft = prefered_lft + (jiffies - ift->tstamp) / HZ;
1829 spin_unlock(&ift->lock);
1830 if (!(flags&IFA_F_TENTATIVE))
1831 ipv6_ifa_notify(0, ift);
1832 }
1833
1834 if (create && in6_dev->cnf.use_tempaddr > 0) {
1835 /*
1836 * When a new public address is created as described in [ADDRCONF],
1837 * also create a new temporary address.
1838 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001839 read_unlock_bh(&in6_dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 ipv6_create_tempaddr(ifp, NULL);
1841 } else {
1842 read_unlock_bh(&in6_dev->lock);
1843 }
1844#endif
1845 in6_ifa_put(ifp);
1846 addrconf_verify(0);
1847 }
1848 }
1849 inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
1850 in6_dev_put(in6_dev);
1851}
1852
1853/*
1854 * Set destination address.
1855 * Special case for SIT interfaces where we create a new "virtual"
1856 * device.
1857 */
1858int addrconf_set_dstaddr(void __user *arg)
1859{
1860 struct in6_ifreq ireq;
1861 struct net_device *dev;
1862 int err = -EINVAL;
1863
1864 rtnl_lock();
1865
1866 err = -EFAULT;
1867 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
1868 goto err_exit;
1869
Eric W. Biederman881d9662007-09-17 11:56:21 -07001870 dev = __dev_get_by_index(&init_net, ireq.ifr6_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871
1872 err = -ENODEV;
1873 if (dev == NULL)
1874 goto err_exit;
1875
Joerg Roedel0be669b2006-10-10 14:49:53 -07001876#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 if (dev->type == ARPHRD_SIT) {
1878 struct ifreq ifr;
1879 mm_segment_t oldfs;
1880 struct ip_tunnel_parm p;
1881
1882 err = -EADDRNOTAVAIL;
1883 if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4))
1884 goto err_exit;
1885
1886 memset(&p, 0, sizeof(p));
1887 p.iph.daddr = ireq.ifr6_addr.s6_addr32[3];
1888 p.iph.saddr = 0;
1889 p.iph.version = 4;
1890 p.iph.ihl = 5;
1891 p.iph.protocol = IPPROTO_IPV6;
1892 p.iph.ttl = 64;
1893 ifr.ifr_ifru.ifru_data = (void __user *)&p;
1894
1895 oldfs = get_fs(); set_fs(KERNEL_DS);
1896 err = dev->do_ioctl(dev, &ifr, SIOCADDTUNNEL);
1897 set_fs(oldfs);
1898
1899 if (err == 0) {
1900 err = -ENOBUFS;
Eric W. Biederman881d9662007-09-17 11:56:21 -07001901 if ((dev = __dev_get_by_name(&init_net, p.name)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 goto err_exit;
1903 err = dev_open(dev);
1904 }
1905 }
Joerg Roedel0be669b2006-10-10 14:49:53 -07001906#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907
1908err_exit:
1909 rtnl_unlock();
1910 return err;
1911}
1912
1913/*
1914 * Manual configuration of address on an interface
1915 */
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09001916static int inet6_addr_add(int ifindex, struct in6_addr *pfx, int plen,
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07001917 __u8 ifa_flags, __u32 prefered_lft, __u32 valid_lft)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918{
1919 struct inet6_ifaddr *ifp;
1920 struct inet6_dev *idev;
1921 struct net_device *dev;
1922 int scope;
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09001923 u32 flags = RTF_EXPIRES;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924
1925 ASSERT_RTNL();
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001926
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09001927 /* check the lifetime */
1928 if (!valid_lft || prefered_lft > valid_lft)
1929 return -EINVAL;
1930
Eric W. Biederman881d9662007-09-17 11:56:21 -07001931 if ((dev = __dev_get_by_index(&init_net, ifindex)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 return -ENODEV;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001933
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 if ((idev = addrconf_add_dev(dev)) == NULL)
1935 return -ENOBUFS;
1936
1937 scope = ipv6_addr_scope(pfx);
1938
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09001939 if (valid_lft == INFINITY_LIFE_TIME) {
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09001940 ifa_flags |= IFA_F_PERMANENT;
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09001941 flags = 0;
1942 } else if (valid_lft >= 0x7FFFFFFF/HZ)
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09001943 valid_lft = 0x7FFFFFFF/HZ;
1944
1945 if (prefered_lft == 0)
1946 ifa_flags |= IFA_F_DEPRECATED;
1947 else if ((prefered_lft >= 0x7FFFFFFF/HZ) &&
1948 (prefered_lft != INFINITY_LIFE_TIME))
1949 prefered_lft = 0x7FFFFFFF/HZ;
1950
1951 ifp = ipv6_add_addr(idev, pfx, plen, scope, ifa_flags);
1952
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 if (!IS_ERR(ifp)) {
Herbert Xu06aebfb2006-08-09 16:52:04 -07001954 spin_lock_bh(&ifp->lock);
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09001955 ifp->valid_lft = valid_lft;
1956 ifp->prefered_lft = prefered_lft;
1957 ifp->tstamp = jiffies;
Herbert Xu06aebfb2006-08-09 16:52:04 -07001958 spin_unlock_bh(&ifp->lock);
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09001959
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09001960 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev,
1961 jiffies_to_clock_t(valid_lft * HZ), flags);
Neil Horman95c385b2007-04-25 17:08:10 -07001962 /*
1963 * Note that section 3.1 of RFC 4429 indicates
1964 * that the Optimistic flag should not be set for
1965 * manually configured addresses
1966 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 addrconf_dad_start(ifp, 0);
1968 in6_ifa_put(ifp);
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09001969 addrconf_verify(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 return 0;
1971 }
1972
1973 return PTR_ERR(ifp);
1974}
1975
1976static int inet6_addr_del(int ifindex, struct in6_addr *pfx, int plen)
1977{
1978 struct inet6_ifaddr *ifp;
1979 struct inet6_dev *idev;
1980 struct net_device *dev;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001981
Eric W. Biederman881d9662007-09-17 11:56:21 -07001982 if ((dev = __dev_get_by_index(&init_net, ifindex)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 return -ENODEV;
1984
1985 if ((idev = __in6_dev_get(dev)) == NULL)
1986 return -ENXIO;
1987
1988 read_lock_bh(&idev->lock);
1989 for (ifp = idev->addr_list; ifp; ifp=ifp->if_next) {
1990 if (ifp->prefix_len == plen &&
1991 ipv6_addr_equal(pfx, &ifp->addr)) {
1992 in6_ifa_hold(ifp);
1993 read_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001994
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 ipv6_del_addr(ifp);
1996
1997 /* If the last address is deleted administratively,
1998 disable IPv6 on this interface.
1999 */
2000 if (idev->addr_list == NULL)
2001 addrconf_ifdown(idev->dev, 1);
2002 return 0;
2003 }
2004 }
2005 read_unlock_bh(&idev->lock);
2006 return -EADDRNOTAVAIL;
2007}
2008
2009
2010int addrconf_add_ifaddr(void __user *arg)
2011{
2012 struct in6_ifreq ireq;
2013 int err;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002014
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 if (!capable(CAP_NET_ADMIN))
2016 return -EPERM;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002017
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2019 return -EFAULT;
2020
2021 rtnl_lock();
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002022 err = inet6_addr_add(ireq.ifr6_ifindex, &ireq.ifr6_addr, ireq.ifr6_prefixlen,
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07002023 IFA_F_PERMANENT, INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 rtnl_unlock();
2025 return err;
2026}
2027
2028int addrconf_del_ifaddr(void __user *arg)
2029{
2030 struct in6_ifreq ireq;
2031 int err;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002032
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 if (!capable(CAP_NET_ADMIN))
2034 return -EPERM;
2035
2036 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2037 return -EFAULT;
2038
2039 rtnl_lock();
2040 err = inet6_addr_del(ireq.ifr6_ifindex, &ireq.ifr6_addr, ireq.ifr6_prefixlen);
2041 rtnl_unlock();
2042 return err;
2043}
2044
Joerg Roedel0be669b2006-10-10 14:49:53 -07002045#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046static void sit_add_v4_addrs(struct inet6_dev *idev)
2047{
2048 struct inet6_ifaddr * ifp;
2049 struct in6_addr addr;
2050 struct net_device *dev;
2051 int scope;
2052
2053 ASSERT_RTNL();
2054
2055 memset(&addr, 0, sizeof(struct in6_addr));
2056 memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
2057
2058 if (idev->dev->flags&IFF_POINTOPOINT) {
2059 addr.s6_addr32[0] = htonl(0xfe800000);
2060 scope = IFA_LINK;
2061 } else {
2062 scope = IPV6_ADDR_COMPATv4;
2063 }
2064
2065 if (addr.s6_addr32[3]) {
2066 ifp = ipv6_add_addr(idev, &addr, 128, scope, IFA_F_PERMANENT);
2067 if (!IS_ERR(ifp)) {
2068 spin_lock_bh(&ifp->lock);
2069 ifp->flags &= ~IFA_F_TENTATIVE;
2070 spin_unlock_bh(&ifp->lock);
2071 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2072 in6_ifa_put(ifp);
2073 }
2074 return;
2075 }
2076
Eric W. Biederman881d9662007-09-17 11:56:21 -07002077 for_each_netdev(&init_net, dev) {
Herbert Xue5ed6392005-10-03 14:35:55 -07002078 struct in_device * in_dev = __in_dev_get_rtnl(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 if (in_dev && (dev->flags & IFF_UP)) {
2080 struct in_ifaddr * ifa;
2081
2082 int flag = scope;
2083
2084 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
2085 int plen;
2086
2087 addr.s6_addr32[3] = ifa->ifa_local;
2088
2089 if (ifa->ifa_scope == RT_SCOPE_LINK)
2090 continue;
2091 if (ifa->ifa_scope >= RT_SCOPE_HOST) {
2092 if (idev->dev->flags&IFF_POINTOPOINT)
2093 continue;
2094 flag |= IFA_HOST;
2095 }
2096 if (idev->dev->flags&IFF_POINTOPOINT)
2097 plen = 64;
2098 else
2099 plen = 96;
2100
2101 ifp = ipv6_add_addr(idev, &addr, plen, flag,
2102 IFA_F_PERMANENT);
2103 if (!IS_ERR(ifp)) {
2104 spin_lock_bh(&ifp->lock);
2105 ifp->flags &= ~IFA_F_TENTATIVE;
2106 spin_unlock_bh(&ifp->lock);
2107 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2108 in6_ifa_put(ifp);
2109 }
2110 }
2111 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002112 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113}
Joerg Roedel0be669b2006-10-10 14:49:53 -07002114#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115
2116static void init_loopback(struct net_device *dev)
2117{
2118 struct inet6_dev *idev;
2119 struct inet6_ifaddr * ifp;
2120
2121 /* ::1 */
2122
2123 ASSERT_RTNL();
2124
2125 if ((idev = ipv6_find_idev(dev)) == NULL) {
2126 printk(KERN_DEBUG "init loopback: add_dev failed\n");
2127 return;
2128 }
2129
2130 ifp = ipv6_add_addr(idev, &in6addr_loopback, 128, IFA_HOST, IFA_F_PERMANENT);
2131 if (!IS_ERR(ifp)) {
2132 spin_lock_bh(&ifp->lock);
2133 ifp->flags &= ~IFA_F_TENTATIVE;
2134 spin_unlock_bh(&ifp->lock);
2135 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2136 in6_ifa_put(ifp);
2137 }
2138}
2139
2140static void addrconf_add_linklocal(struct inet6_dev *idev, struct in6_addr *addr)
2141{
2142 struct inet6_ifaddr * ifp;
Neil Horman95c385b2007-04-25 17:08:10 -07002143 u32 addr_flags = IFA_F_PERMANENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144
Neil Horman95c385b2007-04-25 17:08:10 -07002145#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
2146 if (idev->cnf.optimistic_dad &&
2147 !ipv6_devconf.forwarding)
2148 addr_flags |= IFA_F_OPTIMISTIC;
2149#endif
2150
2151
2152 ifp = ipv6_add_addr(idev, addr, 64, IFA_LINK, addr_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 if (!IS_ERR(ifp)) {
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09002154 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, idev->dev, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 addrconf_dad_start(ifp, 0);
2156 in6_ifa_put(ifp);
2157 }
2158}
2159
2160static void addrconf_dev_config(struct net_device *dev)
2161{
2162 struct in6_addr addr;
2163 struct inet6_dev * idev;
2164
2165 ASSERT_RTNL();
2166
Herbert Xu74235a22007-06-14 13:02:55 -07002167 if ((dev->type != ARPHRD_ETHER) &&
2168 (dev->type != ARPHRD_FDDI) &&
2169 (dev->type != ARPHRD_IEEE802_TR) &&
2170 (dev->type != ARPHRD_ARCNET) &&
2171 (dev->type != ARPHRD_INFINIBAND)) {
2172 /* Alas, we support only Ethernet autoconfiguration. */
2173 return;
2174 }
2175
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 idev = addrconf_add_dev(dev);
2177 if (idev == NULL)
2178 return;
2179
2180 memset(&addr, 0, sizeof(struct in6_addr));
2181 addr.s6_addr32[0] = htonl(0xFE800000);
2182
2183 if (ipv6_generate_eui64(addr.s6_addr + 8, dev) == 0)
2184 addrconf_add_linklocal(idev, &addr);
2185}
2186
Joerg Roedel0be669b2006-10-10 14:49:53 -07002187#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188static void addrconf_sit_config(struct net_device *dev)
2189{
2190 struct inet6_dev *idev;
2191
2192 ASSERT_RTNL();
2193
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002194 /*
2195 * Configure the tunnel with one of our IPv4
2196 * addresses... we should configure all of
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 * our v4 addrs in the tunnel
2198 */
2199
2200 if ((idev = ipv6_find_idev(dev)) == NULL) {
2201 printk(KERN_DEBUG "init sit: add_dev failed\n");
2202 return;
2203 }
2204
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11002205 if (dev->priv_flags & IFF_ISATAP) {
2206 struct in6_addr addr;
2207
2208 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
2209 addrconf_prefix_route(&addr, 64, dev, 0, 0);
2210 if (!ipv6_generate_eui64(addr.s6_addr + 8, dev))
2211 addrconf_add_linklocal(idev, &addr);
2212 return;
2213 }
2214
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 sit_add_v4_addrs(idev);
2216
2217 if (dev->flags&IFF_POINTOPOINT) {
2218 addrconf_add_mroute(dev);
2219 addrconf_add_lroute(dev);
2220 } else
2221 sit_route_add(dev);
2222}
Joerg Roedel0be669b2006-10-10 14:49:53 -07002223#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224
2225static inline int
2226ipv6_inherit_linklocal(struct inet6_dev *idev, struct net_device *link_dev)
2227{
2228 struct in6_addr lladdr;
2229
Neil Horman95c385b2007-04-25 17:08:10 -07002230 if (!ipv6_get_lladdr(link_dev, &lladdr, IFA_F_TENTATIVE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 addrconf_add_linklocal(idev, &lladdr);
2232 return 0;
2233 }
2234 return -1;
2235}
2236
2237static void ip6_tnl_add_linklocal(struct inet6_dev *idev)
2238{
2239 struct net_device *link_dev;
2240
2241 /* first try to inherit the link-local address from the link device */
2242 if (idev->dev->iflink &&
Eric W. Biederman881d9662007-09-17 11:56:21 -07002243 (link_dev = __dev_get_by_index(&init_net, idev->dev->iflink))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 if (!ipv6_inherit_linklocal(idev, link_dev))
2245 return;
2246 }
2247 /* then try to inherit it from any device */
Eric W. Biederman881d9662007-09-17 11:56:21 -07002248 for_each_netdev(&init_net, link_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 if (!ipv6_inherit_linklocal(idev, link_dev))
2250 return;
2251 }
2252 printk(KERN_DEBUG "init ip6-ip6: add_linklocal failed\n");
2253}
2254
2255/*
2256 * Autoconfigure tunnel with a link-local address so routing protocols,
2257 * DHCPv6, MLD etc. can be run over the virtual link
2258 */
2259
2260static void addrconf_ip6_tnl_config(struct net_device *dev)
2261{
2262 struct inet6_dev *idev;
2263
2264 ASSERT_RTNL();
2265
2266 if ((idev = addrconf_add_dev(dev)) == NULL) {
2267 printk(KERN_DEBUG "init ip6-ip6: add_dev failed\n");
2268 return;
2269 }
2270 ip6_tnl_add_linklocal(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271}
2272
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002273static int addrconf_notify(struct notifier_block *this, unsigned long event,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 void * data)
2275{
2276 struct net_device *dev = (struct net_device *) data;
Herbert Xu74235a22007-06-14 13:02:55 -07002277 struct inet6_dev *idev = __in6_dev_get(dev);
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09002278 int run_pending = 0;
Herbert Xub217d612007-07-30 17:04:52 -07002279 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02002281 if (dev->nd_net != &init_net)
2282 return NOTIFY_DONE;
2283
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 switch(event) {
YOSHIFUJI Hideaki45ba9dd2007-02-15 02:07:27 +09002285 case NETDEV_REGISTER:
Herbert Xu74235a22007-06-14 13:02:55 -07002286 if (!idev && dev->mtu >= IPV6_MIN_MTU) {
YOSHIFUJI Hideaki45ba9dd2007-02-15 02:07:27 +09002287 idev = ipv6_add_dev(dev);
2288 if (!idev)
Herbert Xub217d612007-07-30 17:04:52 -07002289 return notifier_from_errno(-ENOMEM);
YOSHIFUJI Hideaki45ba9dd2007-02-15 02:07:27 +09002290 }
2291 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 case NETDEV_UP:
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002293 case NETDEV_CHANGE:
Jay Vosburghc2edacf2007-07-09 10:42:47 -07002294 if (dev->flags & IFF_SLAVE)
2295 break;
2296
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002297 if (event == NETDEV_UP) {
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -07002298 if (!addrconf_qdisc_ok(dev)) {
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002299 /* device is not ready yet. */
2300 printk(KERN_INFO
2301 "ADDRCONF(NETDEV_UP): %s: "
2302 "link is not ready\n",
2303 dev->name);
2304 break;
2305 }
Kristian Slavov99081042006-02-08 16:10:53 -08002306
Evgeniy Polyakovd31c7b82007-11-30 23:36:08 +11002307 if (!idev && dev->mtu >= IPV6_MIN_MTU)
2308 idev = ipv6_add_dev(dev);
2309
Kristian Slavov99081042006-02-08 16:10:53 -08002310 if (idev)
2311 idev->if_flags |= IF_READY;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002312 } else {
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -07002313 if (!addrconf_qdisc_ok(dev)) {
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002314 /* device is still not ready. */
2315 break;
2316 }
2317
2318 if (idev) {
2319 if (idev->if_flags & IF_READY) {
2320 /* device is already configured. */
2321 break;
2322 }
2323 idev->if_flags |= IF_READY;
2324 }
2325
2326 printk(KERN_INFO
2327 "ADDRCONF(NETDEV_CHANGE): %s: "
2328 "link becomes ready\n",
2329 dev->name);
2330
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09002331 run_pending = 1;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002332 }
2333
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 switch(dev->type) {
Joerg Roedel0be669b2006-10-10 14:49:53 -07002335#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 case ARPHRD_SIT:
2337 addrconf_sit_config(dev);
2338 break;
Joerg Roedel0be669b2006-10-10 14:49:53 -07002339#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 case ARPHRD_TUNNEL6:
2341 addrconf_ip6_tnl_config(dev);
2342 break;
2343 case ARPHRD_LOOPBACK:
2344 init_loopback(dev);
2345 break;
2346
2347 default:
2348 addrconf_dev_config(dev);
2349 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002350 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 if (idev) {
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09002352 if (run_pending)
2353 addrconf_dad_run(idev);
2354
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 /* If the MTU changed during the interface down, when the
2356 interface up, the changed MTU must be reflected in the
2357 idev as well as routers.
2358 */
2359 if (idev->cnf.mtu6 != dev->mtu && dev->mtu >= IPV6_MIN_MTU) {
2360 rt6_mtu_change(dev, dev->mtu);
2361 idev->cnf.mtu6 = dev->mtu;
2362 }
2363 idev->tstamp = jiffies;
2364 inet6_ifinfo_notify(RTM_NEWLINK, idev);
2365 /* If the changed mtu during down is lower than IPV6_MIN_MTU
2366 stop IPv6 on this interface.
2367 */
2368 if (dev->mtu < IPV6_MIN_MTU)
2369 addrconf_ifdown(dev, event != NETDEV_DOWN);
2370 }
2371 break;
2372
2373 case NETDEV_CHANGEMTU:
Evgeniy Polyakovd31c7b82007-11-30 23:36:08 +11002374 if (idev && dev->mtu >= IPV6_MIN_MTU) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 rt6_mtu_change(dev, dev->mtu);
2376 idev->cnf.mtu6 = dev->mtu;
2377 break;
2378 }
2379
Evgeniy Polyakovd31c7b82007-11-30 23:36:08 +11002380 if (!idev && dev->mtu >= IPV6_MIN_MTU) {
2381 idev = ipv6_add_dev(dev);
2382 if (idev)
2383 break;
2384 }
2385
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 /* MTU falled under IPV6_MIN_MTU. Stop IPv6 on this interface. */
2387
2388 case NETDEV_DOWN:
2389 case NETDEV_UNREGISTER:
2390 /*
2391 * Remove all addresses from this interface.
2392 */
2393 addrconf_ifdown(dev, event != NETDEV_DOWN);
2394 break;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002395
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 case NETDEV_CHANGENAME:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 if (idev) {
Stephen Hemminger5632c512007-04-28 21:16:39 -07002398 snmp6_unregister_dev(idev);
Pavel Emelyanov408c4762008-01-10 17:41:21 -08002399 addrconf_sysctl_unregister(idev);
Pavel Emelyanovf52295a2007-12-02 00:58:37 +11002400 addrconf_sysctl_register(idev);
Herbert Xub217d612007-07-30 17:04:52 -07002401 err = snmp6_register_dev(idev);
2402 if (err)
2403 return notifier_from_errno(err);
Stephen Hemminger5632c512007-04-28 21:16:39 -07002404 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002406 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407
2408 return NOTIFY_OK;
2409}
2410
2411/*
2412 * addrconf module should be notified of a device going up
2413 */
2414static struct notifier_block ipv6_dev_notf = {
2415 .notifier_call = addrconf_notify,
2416 .priority = 0
2417};
2418
2419static int addrconf_ifdown(struct net_device *dev, int how)
2420{
2421 struct inet6_dev *idev;
2422 struct inet6_ifaddr *ifa, **bifa;
2423 int i;
2424
2425 ASSERT_RTNL();
2426
Eric W. Biederman2774c7a2007-09-26 22:10:56 -07002427 if (dev == init_net.loopback_dev && how == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 how = 0;
2429
2430 rt6_ifdown(dev);
2431 neigh_ifdown(&nd_tbl, dev);
2432
2433 idev = __in6_dev_get(dev);
2434 if (idev == NULL)
2435 return -ENODEV;
2436
2437 /* Step 1: remove reference to ipv6 device from parent device.
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002438 Do not dev_put!
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 */
2440 if (how == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 idev->dead = 1;
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07002442
2443 /* protected by rtnl_lock */
2444 rcu_assign_pointer(dev->ip6_ptr, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445
2446 /* Step 1.5: remove snmp6 entry */
2447 snmp6_unregister_dev(idev);
2448
2449 }
2450
2451 /* Step 2: clear hash table */
2452 for (i=0; i<IN6_ADDR_HSIZE; i++) {
2453 bifa = &inet6_addr_lst[i];
2454
2455 write_lock_bh(&addrconf_hash_lock);
2456 while ((ifa = *bifa) != NULL) {
2457 if (ifa->idev == idev) {
2458 *bifa = ifa->lst_next;
2459 ifa->lst_next = NULL;
2460 addrconf_del_timer(ifa);
2461 in6_ifa_put(ifa);
2462 continue;
2463 }
2464 bifa = &ifa->lst_next;
2465 }
2466 write_unlock_bh(&addrconf_hash_lock);
2467 }
2468
2469 write_lock_bh(&idev->lock);
2470
2471 /* Step 3: clear flags for stateless addrconf */
2472 if (how != 1)
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002473 idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474
2475 /* Step 4: clear address list */
2476#ifdef CONFIG_IPV6_PRIVACY
2477 if (how == 1 && del_timer(&idev->regen_timer))
2478 in6_dev_put(idev);
2479
2480 /* clear tempaddr list */
2481 while ((ifa = idev->tempaddr_list) != NULL) {
2482 idev->tempaddr_list = ifa->tmp_next;
2483 ifa->tmp_next = NULL;
2484 ifa->dead = 1;
2485 write_unlock_bh(&idev->lock);
2486 spin_lock_bh(&ifa->lock);
2487
2488 if (ifa->ifpub) {
2489 in6_ifa_put(ifa->ifpub);
2490 ifa->ifpub = NULL;
2491 }
2492 spin_unlock_bh(&ifa->lock);
2493 in6_ifa_put(ifa);
2494 write_lock_bh(&idev->lock);
2495 }
2496#endif
2497 while ((ifa = idev->addr_list) != NULL) {
2498 idev->addr_list = ifa->if_next;
2499 ifa->if_next = NULL;
2500 ifa->dead = 1;
2501 addrconf_del_timer(ifa);
2502 write_unlock_bh(&idev->lock);
2503
2504 __ipv6_ifa_notify(RTM_DELADDR, ifa);
Vlad Yasevich063ed362007-07-15 00:16:35 -07002505 atomic_notifier_call_chain(&inet6addr_chain, NETDEV_DOWN, ifa);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 in6_ifa_put(ifa);
2507
2508 write_lock_bh(&idev->lock);
2509 }
2510 write_unlock_bh(&idev->lock);
2511
2512 /* Step 5: Discard multicast list */
2513
2514 if (how == 1)
2515 ipv6_mc_destroy_dev(idev);
2516 else
2517 ipv6_mc_down(idev);
2518
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 idev->tstamp = jiffies;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002520
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 /* Shot the device (if unregistered) */
2522
2523 if (how == 1) {
Pavel Emelyanov408c4762008-01-10 17:41:21 -08002524 addrconf_sysctl_unregister(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 neigh_parms_release(&nd_tbl, idev->nd_parms);
2526 neigh_ifdown(&nd_tbl, dev);
2527 in6_dev_put(idev);
2528 }
2529 return 0;
2530}
2531
2532static void addrconf_rs_timer(unsigned long data)
2533{
2534 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
2535
2536 if (ifp->idev->cnf.forwarding)
2537 goto out;
2538
2539 if (ifp->idev->if_flags & IF_RA_RCVD) {
2540 /*
2541 * Announcement received after solicitation
2542 * was sent
2543 */
2544 goto out;
2545 }
2546
2547 spin_lock(&ifp->lock);
2548 if (ifp->probes++ < ifp->idev->cnf.rtr_solicits) {
2549 struct in6_addr all_routers;
2550
2551 /* The wait after the last probe can be shorter */
2552 addrconf_mod_timer(ifp, AC_RS,
2553 (ifp->probes == ifp->idev->cnf.rtr_solicits) ?
2554 ifp->idev->cnf.rtr_solicit_delay :
2555 ifp->idev->cnf.rtr_solicit_interval);
2556 spin_unlock(&ifp->lock);
2557
2558 ipv6_addr_all_routers(&all_routers);
2559
2560 ndisc_send_rs(ifp->idev->dev, &ifp->addr, &all_routers);
2561 } else {
2562 spin_unlock(&ifp->lock);
2563 /*
2564 * Note: we do not support deprecated "all on-link"
2565 * assumption any longer.
2566 */
2567 printk(KERN_DEBUG "%s: no IPv6 routers present\n",
2568 ifp->idev->dev->name);
2569 }
2570
2571out:
2572 in6_ifa_put(ifp);
2573}
2574
2575/*
2576 * Duplicate Address Detection
2577 */
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002578static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
2579{
2580 unsigned long rand_num;
2581 struct inet6_dev *idev = ifp->idev;
2582
Neil Horman95c385b2007-04-25 17:08:10 -07002583 if (ifp->flags & IFA_F_OPTIMISTIC)
2584 rand_num = 0;
2585 else
2586 rand_num = net_random() % (idev->cnf.rtr_solicit_delay ? : 1);
2587
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002588 ifp->probes = idev->cnf.dad_transmits;
2589 addrconf_mod_timer(ifp, AC_DAD, rand_num);
2590}
2591
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -07002592static void addrconf_dad_start(struct inet6_ifaddr *ifp, u32 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593{
2594 struct inet6_dev *idev = ifp->idev;
2595 struct net_device *dev = idev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596
2597 addrconf_join_solict(dev, &ifp->addr);
2598
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 net_srandom(ifp->addr.s6_addr32[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600
2601 read_lock_bh(&idev->lock);
2602 if (ifp->dead)
2603 goto out;
2604 spin_lock_bh(&ifp->lock);
2605
2606 if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07002607 !(ifp->flags&IFA_F_TENTATIVE) ||
2608 ifp->flags & IFA_F_NODAD) {
Neil Horman95c385b2007-04-25 17:08:10 -07002609 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 spin_unlock_bh(&ifp->lock);
2611 read_unlock_bh(&idev->lock);
2612
2613 addrconf_dad_completed(ifp);
2614 return;
2615 }
2616
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08002617 if (!(idev->if_flags & IF_READY)) {
YOSHIFUJI Hideaki3dd3bf82005-12-23 11:23:21 -08002618 spin_unlock_bh(&ifp->lock);
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08002619 read_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002620 /*
2621 * If the defice is not ready:
2622 * - keep it tentative if it is a permanent address.
2623 * - otherwise, kill it.
2624 */
2625 in6_ifa_hold(ifp);
2626 addrconf_dad_stop(ifp);
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08002627 return;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002628 }
Neil Horman95c385b2007-04-25 17:08:10 -07002629
2630 /*
2631 * Optimistic nodes can start receiving
2632 * Frames right away
2633 */
2634 if(ifp->flags & IFA_F_OPTIMISTIC)
2635 ip6_ins_rt(ifp->rt);
2636
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08002637 addrconf_dad_kick(ifp);
2638 spin_unlock_bh(&ifp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639out:
2640 read_unlock_bh(&idev->lock);
2641}
2642
2643static void addrconf_dad_timer(unsigned long data)
2644{
2645 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
2646 struct inet6_dev *idev = ifp->idev;
2647 struct in6_addr unspec;
2648 struct in6_addr mcaddr;
2649
2650 read_lock_bh(&idev->lock);
2651 if (idev->dead) {
2652 read_unlock_bh(&idev->lock);
2653 goto out;
2654 }
2655 spin_lock_bh(&ifp->lock);
2656 if (ifp->probes == 0) {
2657 /*
2658 * DAD was successful
2659 */
2660
Neil Horman95c385b2007-04-25 17:08:10 -07002661 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 spin_unlock_bh(&ifp->lock);
2663 read_unlock_bh(&idev->lock);
2664
2665 addrconf_dad_completed(ifp);
2666
2667 goto out;
2668 }
2669
2670 ifp->probes--;
2671 addrconf_mod_timer(ifp, AC_DAD, ifp->idev->nd_parms->retrans_time);
2672 spin_unlock_bh(&ifp->lock);
2673 read_unlock_bh(&idev->lock);
2674
2675 /* send a neighbour solicitation for our addr */
2676 memset(&unspec, 0, sizeof(unspec));
2677 addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
2678 ndisc_send_ns(ifp->idev->dev, NULL, &ifp->addr, &mcaddr, &unspec);
2679out:
2680 in6_ifa_put(ifp);
2681}
2682
2683static void addrconf_dad_completed(struct inet6_ifaddr *ifp)
2684{
2685 struct net_device * dev = ifp->idev->dev;
2686
2687 /*
2688 * Configure the address for reception. Now it is valid.
2689 */
2690
2691 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2692
2693 /* If added prefix is link local and forwarding is off,
2694 start sending router solicitations.
2695 */
2696
2697 if (ifp->idev->cnf.forwarding == 0 &&
2698 ifp->idev->cnf.rtr_solicits > 0 &&
2699 (dev->flags&IFF_LOOPBACK) == 0 &&
2700 (ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL)) {
2701 struct in6_addr all_routers;
2702
2703 ipv6_addr_all_routers(&all_routers);
2704
2705 /*
2706 * If a host as already performed a random delay
2707 * [...] as part of DAD [...] there is no need
2708 * to delay again before sending the first RS
2709 */
2710 ndisc_send_rs(ifp->idev->dev, &ifp->addr, &all_routers);
2711
2712 spin_lock_bh(&ifp->lock);
2713 ifp->probes = 1;
2714 ifp->idev->if_flags |= IF_RS_SENT;
2715 addrconf_mod_timer(ifp, AC_RS, ifp->idev->cnf.rtr_solicit_interval);
2716 spin_unlock_bh(&ifp->lock);
2717 }
2718}
2719
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09002720static void addrconf_dad_run(struct inet6_dev *idev) {
2721 struct inet6_ifaddr *ifp;
2722
2723 read_lock_bh(&idev->lock);
2724 for (ifp = idev->addr_list; ifp; ifp = ifp->if_next) {
2725 spin_lock_bh(&ifp->lock);
2726 if (!(ifp->flags & IFA_F_TENTATIVE)) {
2727 spin_unlock_bh(&ifp->lock);
2728 continue;
2729 }
2730 spin_unlock_bh(&ifp->lock);
2731 addrconf_dad_kick(ifp);
2732 }
2733 read_unlock_bh(&idev->lock);
2734}
2735
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736#ifdef CONFIG_PROC_FS
2737struct if6_iter_state {
2738 int bucket;
2739};
2740
2741static struct inet6_ifaddr *if6_get_first(struct seq_file *seq)
2742{
2743 struct inet6_ifaddr *ifa = NULL;
2744 struct if6_iter_state *state = seq->private;
2745
2746 for (state->bucket = 0; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
2747 ifa = inet6_addr_lst[state->bucket];
2748 if (ifa)
2749 break;
2750 }
2751 return ifa;
2752}
2753
2754static struct inet6_ifaddr *if6_get_next(struct seq_file *seq, struct inet6_ifaddr *ifa)
2755{
2756 struct if6_iter_state *state = seq->private;
2757
2758 ifa = ifa->lst_next;
2759try_again:
2760 if (!ifa && ++state->bucket < IN6_ADDR_HSIZE) {
2761 ifa = inet6_addr_lst[state->bucket];
2762 goto try_again;
2763 }
2764 return ifa;
2765}
2766
2767static struct inet6_ifaddr *if6_get_idx(struct seq_file *seq, loff_t pos)
2768{
2769 struct inet6_ifaddr *ifa = if6_get_first(seq);
2770
2771 if (ifa)
2772 while(pos && (ifa = if6_get_next(seq, ifa)) != NULL)
2773 --pos;
2774 return pos ? NULL : ifa;
2775}
2776
2777static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
2778{
2779 read_lock_bh(&addrconf_hash_lock);
2780 return if6_get_idx(seq, *pos);
2781}
2782
2783static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2784{
2785 struct inet6_ifaddr *ifa;
2786
2787 ifa = if6_get_next(seq, v);
2788 ++*pos;
2789 return ifa;
2790}
2791
2792static void if6_seq_stop(struct seq_file *seq, void *v)
2793{
2794 read_unlock_bh(&addrconf_hash_lock);
2795}
2796
2797static int if6_seq_show(struct seq_file *seq, void *v)
2798{
2799 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
2800 seq_printf(seq,
YOSHIFUJI Hideaki9343e792006-01-17 02:10:53 -08002801 NIP6_SEQFMT " %02x %02x %02x %02x %8s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 NIP6(ifp->addr),
2803 ifp->idev->dev->ifindex,
2804 ifp->prefix_len,
2805 ifp->scope,
2806 ifp->flags,
2807 ifp->idev->dev->name);
2808 return 0;
2809}
2810
Philippe De Muyter56b3d972007-07-10 23:07:31 -07002811static const struct seq_operations if6_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 .start = if6_seq_start,
2813 .next = if6_seq_next,
2814 .show = if6_seq_show,
2815 .stop = if6_seq_stop,
2816};
2817
2818static int if6_seq_open(struct inode *inode, struct file *file)
2819{
Pavel Emelyanovcf7732e2007-10-10 02:29:29 -07002820 return seq_open_private(file, &if6_seq_ops,
2821 sizeof(struct if6_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822}
2823
Arjan van de Ven9a321442007-02-12 00:55:35 -08002824static const struct file_operations if6_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 .owner = THIS_MODULE,
2826 .open = if6_seq_open,
2827 .read = seq_read,
2828 .llseek = seq_lseek,
2829 .release = seq_release_private,
2830};
2831
2832int __init if6_proc_init(void)
2833{
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02002834 if (!proc_net_fops_create(&init_net, "if_inet6", S_IRUGO, &if6_fops))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 return -ENOMEM;
2836 return 0;
2837}
2838
2839void if6_proc_exit(void)
2840{
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02002841 proc_net_remove(&init_net, "if_inet6");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842}
2843#endif /* CONFIG_PROC_FS */
2844
Masahide NAKAMURA59fbb3a62007-06-26 23:56:32 -07002845#if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07002846/* Check if address is a home address configured on any interface. */
2847int ipv6_chk_home_addr(struct in6_addr *addr)
2848{
2849 int ret = 0;
2850 struct inet6_ifaddr * ifp;
2851 u8 hash = ipv6_addr_hash(addr);
2852 read_lock_bh(&addrconf_hash_lock);
2853 for (ifp = inet6_addr_lst[hash]; ifp; ifp = ifp->lst_next) {
2854 if (ipv6_addr_cmp(&ifp->addr, addr) == 0 &&
2855 (ifp->flags & IFA_F_HOMEADDRESS)) {
2856 ret = 1;
2857 break;
2858 }
2859 }
2860 read_unlock_bh(&addrconf_hash_lock);
2861 return ret;
2862}
2863#endif
2864
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865/*
2866 * Periodic address status verification
2867 */
2868
2869static void addrconf_verify(unsigned long foo)
2870{
2871 struct inet6_ifaddr *ifp;
2872 unsigned long now, next;
2873 int i;
2874
2875 spin_lock_bh(&addrconf_verify_lock);
2876 now = jiffies;
2877 next = now + ADDR_CHECK_FREQUENCY;
2878
2879 del_timer(&addr_chk_timer);
2880
2881 for (i=0; i < IN6_ADDR_HSIZE; i++) {
2882
2883restart:
Yan Zheng5d5780d2005-11-20 13:42:20 -08002884 read_lock(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 for (ifp=inet6_addr_lst[i]; ifp; ifp=ifp->lst_next) {
2886 unsigned long age;
2887#ifdef CONFIG_IPV6_PRIVACY
2888 unsigned long regen_advance;
2889#endif
2890
2891 if (ifp->flags & IFA_F_PERMANENT)
2892 continue;
2893
2894 spin_lock(&ifp->lock);
2895 age = (now - ifp->tstamp) / HZ;
2896
2897#ifdef CONFIG_IPV6_PRIVACY
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002898 regen_advance = ifp->idev->cnf.regen_max_retry *
2899 ifp->idev->cnf.dad_transmits *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 ifp->idev->nd_parms->retrans_time / HZ;
2901#endif
2902
YOSHIFUJI Hideaki8f27ebb2006-07-28 18:12:11 +09002903 if (ifp->valid_lft != INFINITY_LIFE_TIME &&
2904 age >= ifp->valid_lft) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 spin_unlock(&ifp->lock);
2906 in6_ifa_hold(ifp);
Yan Zheng5d5780d2005-11-20 13:42:20 -08002907 read_unlock(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 ipv6_del_addr(ifp);
2909 goto restart;
YOSHIFUJI Hideaki8f27ebb2006-07-28 18:12:11 +09002910 } else if (ifp->prefered_lft == INFINITY_LIFE_TIME) {
2911 spin_unlock(&ifp->lock);
2912 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 } else if (age >= ifp->prefered_lft) {
2914 /* jiffies - ifp->tsamp > age >= ifp->prefered_lft */
2915 int deprecate = 0;
2916
2917 if (!(ifp->flags&IFA_F_DEPRECATED)) {
2918 deprecate = 1;
2919 ifp->flags |= IFA_F_DEPRECATED;
2920 }
2921
2922 if (time_before(ifp->tstamp + ifp->valid_lft * HZ, next))
2923 next = ifp->tstamp + ifp->valid_lft * HZ;
2924
2925 spin_unlock(&ifp->lock);
2926
2927 if (deprecate) {
2928 in6_ifa_hold(ifp);
Yan Zheng5d5780d2005-11-20 13:42:20 -08002929 read_unlock(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930
2931 ipv6_ifa_notify(0, ifp);
2932 in6_ifa_put(ifp);
2933 goto restart;
2934 }
2935#ifdef CONFIG_IPV6_PRIVACY
2936 } else if ((ifp->flags&IFA_F_TEMPORARY) &&
2937 !(ifp->flags&IFA_F_TENTATIVE)) {
2938 if (age >= ifp->prefered_lft - regen_advance) {
2939 struct inet6_ifaddr *ifpub = ifp->ifpub;
2940 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
2941 next = ifp->tstamp + ifp->prefered_lft * HZ;
2942 if (!ifp->regen_count && ifpub) {
2943 ifp->regen_count++;
2944 in6_ifa_hold(ifp);
2945 in6_ifa_hold(ifpub);
2946 spin_unlock(&ifp->lock);
Yan Zheng5d5780d2005-11-20 13:42:20 -08002947 read_unlock(&addrconf_hash_lock);
Hiroyuki YAMAMORI291d8092005-12-23 11:24:05 -08002948 spin_lock(&ifpub->lock);
2949 ifpub->regen_count = 0;
2950 spin_unlock(&ifpub->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 ipv6_create_tempaddr(ifpub, ifp);
2952 in6_ifa_put(ifpub);
2953 in6_ifa_put(ifp);
2954 goto restart;
2955 }
2956 } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
2957 next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
2958 spin_unlock(&ifp->lock);
2959#endif
2960 } else {
2961 /* ifp->prefered_lft <= ifp->valid_lft */
2962 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
2963 next = ifp->tstamp + ifp->prefered_lft * HZ;
2964 spin_unlock(&ifp->lock);
2965 }
2966 }
Yan Zheng5d5780d2005-11-20 13:42:20 -08002967 read_unlock(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 }
2969
2970 addr_chk_timer.expires = time_before(next, jiffies + HZ) ? jiffies + HZ : next;
2971 add_timer(&addr_chk_timer);
2972 spin_unlock_bh(&addrconf_verify_lock);
2973}
2974
Thomas Graf461d8832006-09-18 00:09:49 -07002975static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local)
2976{
2977 struct in6_addr *pfx = NULL;
2978
2979 if (addr)
2980 pfx = nla_data(addr);
2981
2982 if (local) {
2983 if (pfx && nla_memcmp(local, pfx, sizeof(*pfx)))
2984 pfx = NULL;
2985 else
2986 pfx = nla_data(local);
2987 }
2988
2989 return pfx;
2990}
2991
Patrick McHardyef7c79e2007-06-05 12:38:30 -07002992static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = {
Thomas Graf461d8832006-09-18 00:09:49 -07002993 [IFA_ADDRESS] = { .len = sizeof(struct in6_addr) },
2994 [IFA_LOCAL] = { .len = sizeof(struct in6_addr) },
2995 [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) },
2996};
2997
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998static int
2999inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
3000{
Denis V. Lunevb8542722007-12-01 00:21:31 +11003001 struct net *net = skb->sk->sk_net;
Thomas Grafb933f712006-09-18 00:10:19 -07003002 struct ifaddrmsg *ifm;
3003 struct nlattr *tb[IFA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 struct in6_addr *pfx;
Thomas Grafb933f712006-09-18 00:10:19 -07003005 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006
Denis V. Lunevb8542722007-12-01 00:21:31 +11003007 if (net != &init_net)
3008 return -EINVAL;
3009
Thomas Grafb933f712006-09-18 00:10:19 -07003010 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
3011 if (err < 0)
3012 return err;
3013
3014 ifm = nlmsg_data(nlh);
3015 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 if (pfx == NULL)
3017 return -EINVAL;
3018
3019 return inet6_addr_del(ifm->ifa_index, pfx, ifm->ifa_prefixlen);
3020}
3021
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003022static int inet6_addr_modify(struct inet6_ifaddr *ifp, u8 ifa_flags,
3023 u32 prefered_lft, u32 valid_lft)
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09003024{
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09003025 u32 flags = RTF_EXPIRES;
3026
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09003027 if (!valid_lft || (prefered_lft > valid_lft))
3028 return -EINVAL;
3029
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09003030 if (valid_lft == INFINITY_LIFE_TIME) {
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003031 ifa_flags |= IFA_F_PERMANENT;
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09003032 flags = 0;
3033 } else if (valid_lft >= 0x7FFFFFFF/HZ)
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09003034 valid_lft = 0x7FFFFFFF/HZ;
3035
3036 if (prefered_lft == 0)
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003037 ifa_flags |= IFA_F_DEPRECATED;
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09003038 else if ((prefered_lft >= 0x7FFFFFFF/HZ) &&
3039 (prefered_lft != INFINITY_LIFE_TIME))
3040 prefered_lft = 0x7FFFFFFF/HZ;
3041
3042 spin_lock_bh(&ifp->lock);
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07003043 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 +09003044 ifp->tstamp = jiffies;
3045 ifp->valid_lft = valid_lft;
3046 ifp->prefered_lft = prefered_lft;
3047
3048 spin_unlock_bh(&ifp->lock);
3049 if (!(ifp->flags&IFA_F_TENTATIVE))
3050 ipv6_ifa_notify(0, ifp);
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09003051
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09003052 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, ifp->idev->dev,
3053 jiffies_to_clock_t(valid_lft * HZ), flags);
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09003054 addrconf_verify(0);
3055
3056 return 0;
3057}
3058
3059static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
3061{
Denis V. Lunevb8542722007-12-01 00:21:31 +11003062 struct net *net = skb->sk->sk_net;
Thomas Graf461d8832006-09-18 00:09:49 -07003063 struct ifaddrmsg *ifm;
3064 struct nlattr *tb[IFA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 struct in6_addr *pfx;
Thomas Graf7198f8c2006-09-18 00:13:46 -07003066 struct inet6_ifaddr *ifa;
3067 struct net_device *dev;
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003068 u32 valid_lft = INFINITY_LIFE_TIME, preferred_lft = INFINITY_LIFE_TIME;
3069 u8 ifa_flags;
Thomas Graf461d8832006-09-18 00:09:49 -07003070 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071
Denis V. Lunevb8542722007-12-01 00:21:31 +11003072 if (net != &init_net)
3073 return -EINVAL;
3074
Thomas Graf461d8832006-09-18 00:09:49 -07003075 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
3076 if (err < 0)
3077 return err;
3078
3079 ifm = nlmsg_data(nlh);
3080 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 if (pfx == NULL)
3082 return -EINVAL;
3083
Thomas Graf461d8832006-09-18 00:09:49 -07003084 if (tb[IFA_CACHEINFO]) {
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09003085 struct ifa_cacheinfo *ci;
Thomas Graf461d8832006-09-18 00:09:49 -07003086
3087 ci = nla_data(tb[IFA_CACHEINFO]);
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09003088 valid_lft = ci->ifa_valid;
Thomas Graf461d8832006-09-18 00:09:49 -07003089 preferred_lft = ci->ifa_prefered;
3090 } else {
3091 preferred_lft = INFINITY_LIFE_TIME;
3092 valid_lft = INFINITY_LIFE_TIME;
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09003093 }
3094
Eric W. Biederman881d9662007-09-17 11:56:21 -07003095 dev = __dev_get_by_index(&init_net, ifm->ifa_index);
Thomas Graf7198f8c2006-09-18 00:13:46 -07003096 if (dev == NULL)
3097 return -ENODEV;
3098
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003099 /* We ignore other flags so far. */
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07003100 ifa_flags = ifm->ifa_flags & (IFA_F_NODAD | IFA_F_HOMEADDRESS);
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003101
Thomas Graf7198f8c2006-09-18 00:13:46 -07003102 ifa = ipv6_get_ifaddr(pfx, dev, 1);
3103 if (ifa == NULL) {
3104 /*
3105 * It would be best to check for !NLM_F_CREATE here but
3106 * userspace alreay relies on not having to provide this.
3107 */
3108 return inet6_addr_add(ifm->ifa_index, pfx, ifm->ifa_prefixlen,
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003109 ifa_flags, preferred_lft, valid_lft);
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09003110 }
3111
Thomas Graf7198f8c2006-09-18 00:13:46 -07003112 if (nlh->nlmsg_flags & NLM_F_EXCL ||
3113 !(nlh->nlmsg_flags & NLM_F_REPLACE))
3114 err = -EEXIST;
3115 else
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003116 err = inet6_addr_modify(ifa, ifa_flags, preferred_lft, valid_lft);
Thomas Graf7198f8c2006-09-18 00:13:46 -07003117
3118 in6_ifa_put(ifa);
3119
3120 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121}
3122
Thomas Graf101bb222006-09-18 00:11:52 -07003123static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u8 flags,
3124 u8 scope, int ifindex)
3125{
3126 struct ifaddrmsg *ifm;
3127
3128 ifm = nlmsg_data(nlh);
3129 ifm->ifa_family = AF_INET6;
3130 ifm->ifa_prefixlen = prefixlen;
3131 ifm->ifa_flags = flags;
3132 ifm->ifa_scope = scope;
3133 ifm->ifa_index = ifindex;
3134}
3135
Thomas Graf85486af2006-09-18 00:11:24 -07003136static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
3137 unsigned long tstamp, u32 preferred, u32 valid)
3138{
3139 struct ifa_cacheinfo ci;
3140
3141 ci.cstamp = (u32)(TIME_DELTA(cstamp, INITIAL_JIFFIES) / HZ * 100
3142 + TIME_DELTA(cstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
3143 ci.tstamp = (u32)(TIME_DELTA(tstamp, INITIAL_JIFFIES) / HZ * 100
3144 + TIME_DELTA(tstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
3145 ci.ifa_prefered = preferred;
3146 ci.ifa_valid = valid;
3147
3148 return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
3149}
3150
Thomas Graf101bb222006-09-18 00:11:52 -07003151static inline int rt_scope(int ifa_scope)
3152{
3153 if (ifa_scope & IFA_HOST)
3154 return RT_SCOPE_HOST;
3155 else if (ifa_scope & IFA_LINK)
3156 return RT_SCOPE_LINK;
3157 else if (ifa_scope & IFA_SITE)
3158 return RT_SCOPE_SITE;
3159 else
3160 return RT_SCOPE_UNIVERSE;
3161}
3162
Thomas Graf0ab68032006-09-18 00:12:35 -07003163static inline int inet6_ifaddr_msgsize(void)
3164{
Thomas Graf339bf982006-11-10 14:10:15 -08003165 return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
3166 + nla_total_size(16) /* IFA_ADDRESS */
3167 + nla_total_size(sizeof(struct ifa_cacheinfo));
Thomas Graf0ab68032006-09-18 00:12:35 -07003168}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07003169
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07003171 u32 pid, u32 seq, int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 struct nlmsghdr *nlh;
Thomas Graf85486af2006-09-18 00:11:24 -07003174 u32 preferred, valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175
Thomas Graf0ab68032006-09-18 00:12:35 -07003176 nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags);
3177 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08003178 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07003179
Thomas Graf101bb222006-09-18 00:11:52 -07003180 put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope),
3181 ifa->idev->dev->ifindex);
3182
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 if (!(ifa->flags&IFA_F_PERMANENT)) {
Thomas Graf85486af2006-09-18 00:11:24 -07003184 preferred = ifa->prefered_lft;
3185 valid = ifa->valid_lft;
3186 if (preferred != INFINITY_LIFE_TIME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 long tval = (jiffies - ifa->tstamp)/HZ;
Thomas Graf85486af2006-09-18 00:11:24 -07003188 preferred -= tval;
3189 if (valid != INFINITY_LIFE_TIME)
3190 valid -= tval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 }
3192 } else {
Thomas Graf85486af2006-09-18 00:11:24 -07003193 preferred = INFINITY_LIFE_TIME;
3194 valid = INFINITY_LIFE_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 }
Thomas Graf85486af2006-09-18 00:11:24 -07003196
Thomas Graf0ab68032006-09-18 00:12:35 -07003197 if (nla_put(skb, IFA_ADDRESS, 16, &ifa->addr) < 0 ||
Patrick McHardy26932562007-01-31 23:16:40 -08003198 put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0) {
3199 nlmsg_cancel(skb, nlh);
3200 return -EMSGSIZE;
3201 }
Thomas Graf85486af2006-09-18 00:11:24 -07003202
Thomas Graf0ab68032006-09-18 00:12:35 -07003203 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204}
3205
3206static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -07003207 u32 pid, u32 seq, int event, u16 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 struct nlmsghdr *nlh;
Thomas Graf101bb222006-09-18 00:11:52 -07003210 u8 scope = RT_SCOPE_UNIVERSE;
3211 int ifindex = ifmca->idev->dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212
Thomas Graf101bb222006-09-18 00:11:52 -07003213 if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE)
3214 scope = RT_SCOPE_SITE;
3215
Thomas Graf0ab68032006-09-18 00:12:35 -07003216 nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags);
3217 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08003218 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07003219
Thomas Graf101bb222006-09-18 00:11:52 -07003220 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
Thomas Graf0ab68032006-09-18 00:12:35 -07003221 if (nla_put(skb, IFA_MULTICAST, 16, &ifmca->mca_addr) < 0 ||
3222 put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp,
Patrick McHardy26932562007-01-31 23:16:40 -08003223 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
3224 nlmsg_cancel(skb, nlh);
3225 return -EMSGSIZE;
3226 }
Thomas Graf85486af2006-09-18 00:11:24 -07003227
Thomas Graf0ab68032006-09-18 00:12:35 -07003228 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229}
3230
3231static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07003232 u32 pid, u32 seq, int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 struct nlmsghdr *nlh;
Thomas Graf101bb222006-09-18 00:11:52 -07003235 u8 scope = RT_SCOPE_UNIVERSE;
3236 int ifindex = ifaca->aca_idev->dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237
Thomas Graf101bb222006-09-18 00:11:52 -07003238 if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE)
3239 scope = RT_SCOPE_SITE;
3240
Thomas Graf0ab68032006-09-18 00:12:35 -07003241 nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags);
3242 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08003243 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07003244
Thomas Graf101bb222006-09-18 00:11:52 -07003245 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
Thomas Graf0ab68032006-09-18 00:12:35 -07003246 if (nla_put(skb, IFA_ANYCAST, 16, &ifaca->aca_addr) < 0 ||
3247 put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp,
Patrick McHardy26932562007-01-31 23:16:40 -08003248 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
3249 nlmsg_cancel(skb, nlh);
3250 return -EMSGSIZE;
3251 }
Thomas Graf85486af2006-09-18 00:11:24 -07003252
Thomas Graf0ab68032006-09-18 00:12:35 -07003253 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254}
3255
3256enum addr_type_t
3257{
3258 UNICAST_ADDR,
3259 MULTICAST_ADDR,
3260 ANYCAST_ADDR,
3261};
3262
3263static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
3264 enum addr_type_t type)
3265{
3266 int idx, ip_idx;
3267 int s_idx, s_ip_idx;
3268 int err = 1;
3269 struct net_device *dev;
3270 struct inet6_dev *idev = NULL;
3271 struct inet6_ifaddr *ifa;
3272 struct ifmcaddr6 *ifmca;
3273 struct ifacaddr6 *ifaca;
3274
3275 s_idx = cb->args[0];
3276 s_ip_idx = ip_idx = cb->args[1];
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003277
Pavel Emelianov7562f872007-05-03 15:13:45 -07003278 idx = 0;
Eric W. Biederman881d9662007-09-17 11:56:21 -07003279 for_each_netdev(&init_net, dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 if (idx < s_idx)
Pavel Emelianov7562f872007-05-03 15:13:45 -07003281 goto cont;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 if (idx > s_idx)
3283 s_ip_idx = 0;
3284 ip_idx = 0;
3285 if ((idev = in6_dev_get(dev)) == NULL)
Pavel Emelianov7562f872007-05-03 15:13:45 -07003286 goto cont;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 read_lock_bh(&idev->lock);
3288 switch (type) {
3289 case UNICAST_ADDR:
YOSHIFUJI Hideakiae9cda52005-06-28 13:00:30 -07003290 /* unicast address incl. temp addr */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291 for (ifa = idev->addr_list; ifa;
3292 ifa = ifa->if_next, ip_idx++) {
3293 if (ip_idx < s_ip_idx)
3294 continue;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003295 if ((err = inet6_fill_ifaddr(skb, ifa,
3296 NETLINK_CB(cb->skb).pid,
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07003297 cb->nlh->nlmsg_seq, RTM_NEWADDR,
3298 NLM_F_MULTI)) <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 goto done;
3300 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301 break;
3302 case MULTICAST_ADDR:
3303 /* multicast address */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003304 for (ifmca = idev->mc_list; ifmca;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305 ifmca = ifmca->next, ip_idx++) {
3306 if (ip_idx < s_ip_idx)
3307 continue;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003308 if ((err = inet6_fill_ifmcaddr(skb, ifmca,
3309 NETLINK_CB(cb->skb).pid,
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07003310 cb->nlh->nlmsg_seq, RTM_GETMULTICAST,
3311 NLM_F_MULTI)) <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312 goto done;
3313 }
3314 break;
3315 case ANYCAST_ADDR:
3316 /* anycast address */
3317 for (ifaca = idev->ac_list; ifaca;
3318 ifaca = ifaca->aca_next, ip_idx++) {
3319 if (ip_idx < s_ip_idx)
3320 continue;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003321 if ((err = inet6_fill_ifacaddr(skb, ifaca,
3322 NETLINK_CB(cb->skb).pid,
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07003323 cb->nlh->nlmsg_seq, RTM_GETANYCAST,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003324 NLM_F_MULTI)) <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325 goto done;
3326 }
3327 break;
3328 default:
3329 break;
3330 }
3331 read_unlock_bh(&idev->lock);
3332 in6_dev_put(idev);
Pavel Emelianov7562f872007-05-03 15:13:45 -07003333cont:
3334 idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335 }
3336done:
3337 if (err <= 0) {
3338 read_unlock_bh(&idev->lock);
3339 in6_dev_put(idev);
3340 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341 cb->args[0] = idx;
3342 cb->args[1] = ip_idx;
3343 return skb->len;
3344}
3345
3346static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
3347{
Denis V. Lunevb8542722007-12-01 00:21:31 +11003348 struct net *net = skb->sk->sk_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349 enum addr_type_t type = UNICAST_ADDR;
Denis V. Lunevb8542722007-12-01 00:21:31 +11003350
3351 if (net != &init_net)
3352 return 0;
3353
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354 return inet6_dump_addr(skb, cb, type);
3355}
3356
3357static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
3358{
Denis V. Lunevb8542722007-12-01 00:21:31 +11003359 struct net *net = skb->sk->sk_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 enum addr_type_t type = MULTICAST_ADDR;
Denis V. Lunevb8542722007-12-01 00:21:31 +11003361
3362 if (net != &init_net)
3363 return 0;
3364
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365 return inet6_dump_addr(skb, cb, type);
3366}
3367
3368
3369static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
3370{
Denis V. Lunevb8542722007-12-01 00:21:31 +11003371 struct net *net = skb->sk->sk_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372 enum addr_type_t type = ANYCAST_ADDR;
Denis V. Lunevb8542722007-12-01 00:21:31 +11003373
3374 if (net != &init_net)
3375 return 0;
3376
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 return inet6_dump_addr(skb, cb, type);
3378}
3379
Thomas Graf1b29fc22006-09-18 00:10:50 -07003380static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr* nlh,
3381 void *arg)
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003382{
Denis V. Lunevb8542722007-12-01 00:21:31 +11003383 struct net *net = in_skb->sk->sk_net;
Thomas Graf1b29fc22006-09-18 00:10:50 -07003384 struct ifaddrmsg *ifm;
3385 struct nlattr *tb[IFA_MAX+1];
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003386 struct in6_addr *addr = NULL;
3387 struct net_device *dev = NULL;
3388 struct inet6_ifaddr *ifa;
3389 struct sk_buff *skb;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003390 int err;
3391
Denis V. Lunevb8542722007-12-01 00:21:31 +11003392 if (net != &init_net)
3393 return -EINVAL;
3394
Thomas Graf1b29fc22006-09-18 00:10:50 -07003395 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
3396 if (err < 0)
3397 goto errout;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003398
Thomas Graf1b29fc22006-09-18 00:10:50 -07003399 addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
3400 if (addr == NULL) {
3401 err = -EINVAL;
3402 goto errout;
3403 }
3404
3405 ifm = nlmsg_data(nlh);
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003406 if (ifm->ifa_index)
Eric W. Biederman881d9662007-09-17 11:56:21 -07003407 dev = __dev_get_by_index(&init_net, ifm->ifa_index);
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003408
Thomas Graf1b29fc22006-09-18 00:10:50 -07003409 if ((ifa = ipv6_get_ifaddr(addr, dev, 1)) == NULL) {
3410 err = -EADDRNOTAVAIL;
3411 goto errout;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003412 }
3413
Thomas Graf0ab68032006-09-18 00:12:35 -07003414 if ((skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL)) == NULL) {
Thomas Graf1b29fc22006-09-18 00:10:50 -07003415 err = -ENOBUFS;
3416 goto errout_ifa;
3417 }
3418
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003419 err = inet6_fill_ifaddr(skb, ifa, NETLINK_CB(in_skb).pid,
3420 nlh->nlmsg_seq, RTM_NEWADDR, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08003421 if (err < 0) {
3422 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
3423 WARN_ON(err == -EMSGSIZE);
3424 kfree_skb(skb);
3425 goto errout_ifa;
3426 }
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003427 err = rtnl_unicast(skb, &init_net, NETLINK_CB(in_skb).pid);
Thomas Graf1b29fc22006-09-18 00:10:50 -07003428errout_ifa:
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003429 in6_ifa_put(ifa);
Thomas Graf1b29fc22006-09-18 00:10:50 -07003430errout:
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003431 return err;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003432}
3433
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
3435{
3436 struct sk_buff *skb;
Thomas Graf5d620262006-08-15 00:35:02 -07003437 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438
Thomas Graf0ab68032006-09-18 00:12:35 -07003439 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC);
Thomas Graf5d620262006-08-15 00:35:02 -07003440 if (skb == NULL)
3441 goto errout;
3442
3443 err = inet6_fill_ifaddr(skb, ifa, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08003444 if (err < 0) {
3445 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
3446 WARN_ON(err == -EMSGSIZE);
3447 kfree_skb(skb);
3448 goto errout;
3449 }
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003450 err = rtnl_notify(skb, &init_net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
Thomas Graf5d620262006-08-15 00:35:02 -07003451errout:
3452 if (err < 0)
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003453 rtnl_set_sk_err(&init_net, RTNLGRP_IPV6_IFADDR, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454}
3455
Dave Jonesb6f99a22007-03-22 12:27:49 -07003456static inline void ipv6_store_devconf(struct ipv6_devconf *cnf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457 __s32 *array, int bytes)
3458{
Thomas Graf04561c12006-11-14 19:53:58 -08003459 BUG_ON(bytes < (DEVCONF_MAX * 4));
3460
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461 memset(array, 0, bytes);
3462 array[DEVCONF_FORWARDING] = cnf->forwarding;
3463 array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
3464 array[DEVCONF_MTU6] = cnf->mtu6;
3465 array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
3466 array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
3467 array[DEVCONF_AUTOCONF] = cnf->autoconf;
3468 array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
3469 array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
3470 array[DEVCONF_RTR_SOLICIT_INTERVAL] = cnf->rtr_solicit_interval;
3471 array[DEVCONF_RTR_SOLICIT_DELAY] = cnf->rtr_solicit_delay;
3472 array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
3473#ifdef CONFIG_IPV6_PRIVACY
3474 array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
3475 array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
3476 array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
3477 array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
3478 array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
3479#endif
3480 array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -08003481 array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr;
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -08003482 array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo;
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08003483#ifdef CONFIG_IPV6_ROUTER_PREF
3484 array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref;
YOSHIFUJI Hideaki52e163562006-03-20 17:05:47 -08003485 array[DEVCONF_RTR_PROBE_INTERVAL] = cnf->rtr_probe_interval;
Neil Hormanfa03ef32007-01-30 14:30:10 -08003486#ifdef CONFIG_IPV6_ROUTE_INFO
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -08003487 array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen;
3488#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08003489#endif
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -07003490 array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp;
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -07003491 array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route;
Neil Horman95c385b2007-04-25 17:08:10 -07003492#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
3493 array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad;
3494#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495}
3496
Thomas Graf339bf982006-11-10 14:10:15 -08003497static inline size_t inet6_if_nlmsg_size(void)
3498{
3499 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
3500 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
3501 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
3502 + nla_total_size(4) /* IFLA_MTU */
3503 + nla_total_size(4) /* IFLA_LINK */
3504 + nla_total_size( /* IFLA_PROTINFO */
3505 nla_total_size(4) /* IFLA_INET6_FLAGS */
3506 + nla_total_size(sizeof(struct ifla_cacheinfo))
3507 + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07003508 + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */
3509 + nla_total_size(ICMP6_MIB_MAX * 8) /* IFLA_INET6_ICMP6STATS */
Thomas Graf339bf982006-11-10 14:10:15 -08003510 );
3511}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07003512
Herbert Xu7f7d9a62007-04-24 21:54:09 -07003513static inline void __snmp6_fill_stats(u64 *stats, void **mib, int items,
3514 int bytes)
3515{
3516 int i;
3517 int pad = bytes - sizeof(u64) * items;
3518 BUG_ON(pad < 0);
3519
3520 /* Use put_unaligned() because stats may not be aligned for u64. */
3521 put_unaligned(items, &stats[0]);
3522 for (i = 1; i < items; i++)
3523 put_unaligned(snmp_fold_field(mib, i), &stats[i]);
3524
3525 memset(&stats[items], 0, pad);
3526}
3527
3528static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype,
3529 int bytes)
3530{
3531 switch(attrtype) {
3532 case IFLA_INET6_STATS:
3533 __snmp6_fill_stats(stats, (void **)idev->stats.ipv6, IPSTATS_MIB_MAX, bytes);
3534 break;
3535 case IFLA_INET6_ICMP6STATS:
3536 __snmp6_fill_stats(stats, (void **)idev->stats.icmpv6, ICMP6_MIB_MAX, bytes);
3537 break;
3538 }
3539}
3540
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003541static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07003542 u32 pid, u32 seq, int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543{
Thomas Graf04561c12006-11-14 19:53:58 -08003544 struct net_device *dev = idev->dev;
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07003545 struct nlattr *nla;
Thomas Graf04561c12006-11-14 19:53:58 -08003546 struct ifinfomsg *hdr;
3547 struct nlmsghdr *nlh;
3548 void *protoinfo;
3549 struct ifla_cacheinfo ci;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550
Thomas Graf04561c12006-11-14 19:53:58 -08003551 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*hdr), flags);
3552 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08003553 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554
Thomas Graf04561c12006-11-14 19:53:58 -08003555 hdr = nlmsg_data(nlh);
3556 hdr->ifi_family = AF_INET6;
3557 hdr->__ifi_pad = 0;
3558 hdr->ifi_type = dev->type;
3559 hdr->ifi_index = dev->ifindex;
3560 hdr->ifi_flags = dev_get_flags(dev);
3561 hdr->ifi_change = 0;
3562
3563 NLA_PUT_STRING(skb, IFLA_IFNAME, dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564
3565 if (dev->addr_len)
Thomas Graf04561c12006-11-14 19:53:58 -08003566 NLA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567
Thomas Graf04561c12006-11-14 19:53:58 -08003568 NLA_PUT_U32(skb, IFLA_MTU, dev->mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569 if (dev->ifindex != dev->iflink)
Thomas Graf04561c12006-11-14 19:53:58 -08003570 NLA_PUT_U32(skb, IFLA_LINK, dev->iflink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571
Thomas Graf04561c12006-11-14 19:53:58 -08003572 protoinfo = nla_nest_start(skb, IFLA_PROTINFO);
3573 if (protoinfo == NULL)
3574 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575
Thomas Graf04561c12006-11-14 19:53:58 -08003576 NLA_PUT_U32(skb, IFLA_INET6_FLAGS, idev->if_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578 ci.max_reasm_len = IPV6_MAXPLEN;
3579 ci.tstamp = (__u32)(TIME_DELTA(idev->tstamp, INITIAL_JIFFIES) / HZ * 100
3580 + TIME_DELTA(idev->tstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
3581 ci.reachable_time = idev->nd_parms->reachable_time;
3582 ci.retrans_time = idev->nd_parms->retrans_time;
Thomas Graf04561c12006-11-14 19:53:58 -08003583 NLA_PUT(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci);
3584
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07003585 nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32));
3586 if (nla == NULL)
Thomas Graf04561c12006-11-14 19:53:58 -08003587 goto nla_put_failure;
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07003588 ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07003590 /* XXX - MC not implemented */
3591
3592 nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64));
3593 if (nla == NULL)
3594 goto nla_put_failure;
3595 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla));
3596
3597 nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64));
3598 if (nla == NULL)
3599 goto nla_put_failure;
3600 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601
Thomas Graf04561c12006-11-14 19:53:58 -08003602 nla_nest_end(skb, protoinfo);
3603 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604
Thomas Graf04561c12006-11-14 19:53:58 -08003605nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08003606 nlmsg_cancel(skb, nlh);
3607 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608}
3609
3610static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
3611{
Denis V. Lunevb8542722007-12-01 00:21:31 +11003612 struct net *net = skb->sk->sk_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613 int idx, err;
3614 int s_idx = cb->args[0];
3615 struct net_device *dev;
3616 struct inet6_dev *idev;
3617
Denis V. Lunevb8542722007-12-01 00:21:31 +11003618 if (net != &init_net)
3619 return 0;
3620
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621 read_lock(&dev_base_lock);
Pavel Emelianov7562f872007-05-03 15:13:45 -07003622 idx = 0;
Eric W. Biederman881d9662007-09-17 11:56:21 -07003623 for_each_netdev(&init_net, dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624 if (idx < s_idx)
Pavel Emelianov7562f872007-05-03 15:13:45 -07003625 goto cont;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626 if ((idev = in6_dev_get(dev)) == NULL)
Pavel Emelianov7562f872007-05-03 15:13:45 -07003627 goto cont;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003628 err = inet6_fill_ifinfo(skb, idev, NETLINK_CB(cb->skb).pid,
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07003629 cb->nlh->nlmsg_seq, RTM_NEWLINK, NLM_F_MULTI);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630 in6_dev_put(idev);
3631 if (err <= 0)
3632 break;
Pavel Emelianov7562f872007-05-03 15:13:45 -07003633cont:
3634 idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635 }
3636 read_unlock(&dev_base_lock);
3637 cb->args[0] = idx;
3638
3639 return skb->len;
3640}
3641
3642void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
3643{
3644 struct sk_buff *skb;
Thomas Graf8d7a76c2006-08-15 00:35:47 -07003645 int err = -ENOBUFS;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003646
Thomas Graf339bf982006-11-10 14:10:15 -08003647 skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC);
Thomas Graf8d7a76c2006-08-15 00:35:47 -07003648 if (skb == NULL)
3649 goto errout;
3650
3651 err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08003652 if (err < 0) {
3653 /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */
3654 WARN_ON(err == -EMSGSIZE);
3655 kfree_skb(skb);
3656 goto errout;
3657 }
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003658 err = rtnl_notify(skb, &init_net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
Thomas Graf8d7a76c2006-08-15 00:35:47 -07003659errout:
3660 if (err < 0)
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003661 rtnl_set_sk_err(&init_net, RTNLGRP_IPV6_IFADDR, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662}
3663
Thomas Graf339bf982006-11-10 14:10:15 -08003664static inline size_t inet6_prefix_nlmsg_size(void)
3665{
3666 return NLMSG_ALIGN(sizeof(struct prefixmsg))
3667 + nla_total_size(sizeof(struct in6_addr))
3668 + nla_total_size(sizeof(struct prefix_cacheinfo));
3669}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07003670
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
Thomas Graf6051e2f2006-11-14 19:54:19 -08003672 struct prefix_info *pinfo, u32 pid, u32 seq,
3673 int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674{
Thomas Graf6051e2f2006-11-14 19:54:19 -08003675 struct prefixmsg *pmsg;
3676 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677 struct prefix_cacheinfo ci;
3678
Thomas Graf6051e2f2006-11-14 19:54:19 -08003679 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*pmsg), flags);
3680 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08003681 return -EMSGSIZE;
Thomas Graf6051e2f2006-11-14 19:54:19 -08003682
3683 pmsg = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684 pmsg->prefix_family = AF_INET6;
Patrick McHardy8a470772005-06-28 12:56:45 -07003685 pmsg->prefix_pad1 = 0;
3686 pmsg->prefix_pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687 pmsg->prefix_ifindex = idev->dev->ifindex;
3688 pmsg->prefix_len = pinfo->prefix_len;
3689 pmsg->prefix_type = pinfo->type;
Patrick McHardy8a470772005-06-28 12:56:45 -07003690 pmsg->prefix_pad3 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691 pmsg->prefix_flags = 0;
3692 if (pinfo->onlink)
3693 pmsg->prefix_flags |= IF_PREFIX_ONLINK;
3694 if (pinfo->autoconf)
3695 pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
3696
Thomas Graf6051e2f2006-11-14 19:54:19 -08003697 NLA_PUT(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698
3699 ci.preferred_time = ntohl(pinfo->prefered);
3700 ci.valid_time = ntohl(pinfo->valid);
Thomas Graf6051e2f2006-11-14 19:54:19 -08003701 NLA_PUT(skb, PREFIX_CACHEINFO, sizeof(ci), &ci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702
Thomas Graf6051e2f2006-11-14 19:54:19 -08003703 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704
Thomas Graf6051e2f2006-11-14 19:54:19 -08003705nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08003706 nlmsg_cancel(skb, nlh);
3707 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708}
3709
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003710static void inet6_prefix_notify(int event, struct inet6_dev *idev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711 struct prefix_info *pinfo)
3712{
3713 struct sk_buff *skb;
Thomas Graf8c384bfa2006-08-15 00:36:07 -07003714 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715
Thomas Graf339bf982006-11-10 14:10:15 -08003716 skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC);
Thomas Graf8c384bfa2006-08-15 00:36:07 -07003717 if (skb == NULL)
3718 goto errout;
3719
3720 err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08003721 if (err < 0) {
3722 /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */
3723 WARN_ON(err == -EMSGSIZE);
3724 kfree_skb(skb);
3725 goto errout;
3726 }
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003727 err = rtnl_notify(skb, &init_net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC);
Thomas Graf8c384bfa2006-08-15 00:36:07 -07003728errout:
3729 if (err < 0)
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003730 rtnl_set_sk_err(&init_net, RTNLGRP_IPV6_PREFIX, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731}
3732
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
3734{
3735 inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
3736
3737 switch (event) {
3738 case RTM_NEWADDR:
Neil Horman95c385b2007-04-25 17:08:10 -07003739 /*
3740 * If the address was optimistic
3741 * we inserted the route at the start of
3742 * our DAD process, so we don't need
3743 * to do it again
3744 */
3745 if (!(ifp->rt->rt6i_node))
3746 ip6_ins_rt(ifp->rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747 if (ifp->idev->cnf.forwarding)
3748 addrconf_join_anycast(ifp);
3749 break;
3750 case RTM_DELADDR:
3751 if (ifp->idev->cnf.forwarding)
3752 addrconf_leave_anycast(ifp);
3753 addrconf_leave_solict(ifp->idev, &ifp->addr);
3754 dst_hold(&ifp->rt->u.dst);
Thomas Grafe0a1ad732006-08-22 00:00:21 -07003755 if (ip6_del_rt(ifp->rt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756 dst_free(&ifp->rt->u.dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757 break;
3758 }
3759}
3760
3761static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
3762{
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07003763 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764 if (likely(ifp->idev->dead == 0))
3765 __ipv6_ifa_notify(event, ifp);
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07003766 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767}
3768
3769#ifdef CONFIG_SYSCTL
3770
3771static
3772int addrconf_sysctl_forward(ctl_table *ctl, int write, struct file * filp,
3773 void __user *buffer, size_t *lenp, loff_t *ppos)
3774{
3775 int *valp = ctl->data;
3776 int val = *valp;
3777 int ret;
3778
3779 ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
3780
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -08003781 if (write)
3782 addrconf_fixup_forwarding(ctl, valp, val);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003783 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784}
3785
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003786static int addrconf_sysctl_forward_strategy(ctl_table *table,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787 int __user *name, int nlen,
3788 void __user *oldval,
3789 size_t __user *oldlenp,
Alexey Dobriyan1f29bcd2006-12-10 02:19:10 -08003790 void __user *newval, size_t newlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791{
3792 int *valp = table->data;
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -08003793 int val = *valp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794 int new;
3795
3796 if (!newval || !newlen)
3797 return 0;
3798 if (newlen != sizeof(int))
3799 return -EINVAL;
3800 if (get_user(new, (int __user *)newval))
3801 return -EFAULT;
3802 if (new == *valp)
3803 return 0;
3804 if (oldval && oldlenp) {
3805 size_t len;
3806 if (get_user(len, oldlenp))
3807 return -EFAULT;
3808 if (len) {
3809 if (len > table->maxlen)
3810 len = table->maxlen;
3811 if (copy_to_user(oldval, valp, len))
3812 return -EFAULT;
3813 if (put_user(len, oldlenp))
3814 return -EFAULT;
3815 }
3816 }
3817
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -08003818 *valp = new;
3819 addrconf_fixup_forwarding(table, valp, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820 return 1;
3821}
3822
3823static struct addrconf_sysctl_table
3824{
3825 struct ctl_table_header *sysctl_header;
3826 ctl_table addrconf_vars[__NET_IPV6_MAX];
Pavel Emelyanov1dab6222007-12-02 00:59:38 +11003827 char *dev_name;
Brian Haleyab32ea52006-09-22 14:15:41 -07003828} addrconf_sysctl __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829 .sysctl_header = NULL,
3830 .addrconf_vars = {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003831 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832 .ctl_name = NET_IPV6_FORWARDING,
3833 .procname = "forwarding",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003834 .data = &ipv6_devconf.forwarding,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835 .maxlen = sizeof(int),
3836 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003837 .proc_handler = &addrconf_sysctl_forward,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838 .strategy = &addrconf_sysctl_forward_strategy,
3839 },
3840 {
3841 .ctl_name = NET_IPV6_HOP_LIMIT,
3842 .procname = "hop_limit",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003843 .data = &ipv6_devconf.hop_limit,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844 .maxlen = sizeof(int),
3845 .mode = 0644,
3846 .proc_handler = proc_dointvec,
3847 },
3848 {
3849 .ctl_name = NET_IPV6_MTU,
3850 .procname = "mtu",
3851 .data = &ipv6_devconf.mtu6,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003852 .maxlen = sizeof(int),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003854 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855 },
3856 {
3857 .ctl_name = NET_IPV6_ACCEPT_RA,
3858 .procname = "accept_ra",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003859 .data = &ipv6_devconf.accept_ra,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003860 .maxlen = sizeof(int),
3861 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003862 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003863 },
3864 {
3865 .ctl_name = NET_IPV6_ACCEPT_REDIRECTS,
3866 .procname = "accept_redirects",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003867 .data = &ipv6_devconf.accept_redirects,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003868 .maxlen = sizeof(int),
3869 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003870 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871 },
3872 {
3873 .ctl_name = NET_IPV6_AUTOCONF,
3874 .procname = "autoconf",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003875 .data = &ipv6_devconf.autoconf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876 .maxlen = sizeof(int),
3877 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003878 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879 },
3880 {
3881 .ctl_name = NET_IPV6_DAD_TRANSMITS,
3882 .procname = "dad_transmits",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003883 .data = &ipv6_devconf.dad_transmits,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884 .maxlen = sizeof(int),
3885 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003886 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003887 },
3888 {
3889 .ctl_name = NET_IPV6_RTR_SOLICITS,
3890 .procname = "router_solicitations",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003891 .data = &ipv6_devconf.rtr_solicits,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892 .maxlen = sizeof(int),
3893 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003894 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895 },
3896 {
3897 .ctl_name = NET_IPV6_RTR_SOLICIT_INTERVAL,
3898 .procname = "router_solicitation_interval",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003899 .data = &ipv6_devconf.rtr_solicit_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900 .maxlen = sizeof(int),
3901 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003902 .proc_handler = &proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903 .strategy = &sysctl_jiffies,
3904 },
3905 {
3906 .ctl_name = NET_IPV6_RTR_SOLICIT_DELAY,
3907 .procname = "router_solicitation_delay",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003908 .data = &ipv6_devconf.rtr_solicit_delay,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909 .maxlen = sizeof(int),
3910 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003911 .proc_handler = &proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912 .strategy = &sysctl_jiffies,
3913 },
3914 {
3915 .ctl_name = NET_IPV6_FORCE_MLD_VERSION,
3916 .procname = "force_mld_version",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003917 .data = &ipv6_devconf.force_mld_version,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918 .maxlen = sizeof(int),
3919 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003920 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003921 },
3922#ifdef CONFIG_IPV6_PRIVACY
3923 {
3924 .ctl_name = NET_IPV6_USE_TEMPADDR,
3925 .procname = "use_tempaddr",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003926 .data = &ipv6_devconf.use_tempaddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927 .maxlen = sizeof(int),
3928 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003929 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930 },
3931 {
3932 .ctl_name = NET_IPV6_TEMP_VALID_LFT,
3933 .procname = "temp_valid_lft",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003934 .data = &ipv6_devconf.temp_valid_lft,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935 .maxlen = sizeof(int),
3936 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003937 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938 },
3939 {
3940 .ctl_name = NET_IPV6_TEMP_PREFERED_LFT,
3941 .procname = "temp_prefered_lft",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003942 .data = &ipv6_devconf.temp_prefered_lft,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003943 .maxlen = sizeof(int),
3944 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003945 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946 },
3947 {
3948 .ctl_name = NET_IPV6_REGEN_MAX_RETRY,
3949 .procname = "regen_max_retry",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003950 .data = &ipv6_devconf.regen_max_retry,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951 .maxlen = sizeof(int),
3952 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003953 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954 },
3955 {
3956 .ctl_name = NET_IPV6_MAX_DESYNC_FACTOR,
3957 .procname = "max_desync_factor",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003958 .data = &ipv6_devconf.max_desync_factor,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959 .maxlen = sizeof(int),
3960 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003961 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962 },
3963#endif
3964 {
3965 .ctl_name = NET_IPV6_MAX_ADDRESSES,
3966 .procname = "max_addresses",
3967 .data = &ipv6_devconf.max_addresses,
3968 .maxlen = sizeof(int),
3969 .mode = 0644,
3970 .proc_handler = &proc_dointvec,
3971 },
3972 {
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -08003973 .ctl_name = NET_IPV6_ACCEPT_RA_DEFRTR,
3974 .procname = "accept_ra_defrtr",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003975 .data = &ipv6_devconf.accept_ra_defrtr,
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -08003976 .maxlen = sizeof(int),
3977 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003978 .proc_handler = &proc_dointvec,
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -08003979 },
3980 {
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -08003981 .ctl_name = NET_IPV6_ACCEPT_RA_PINFO,
3982 .procname = "accept_ra_pinfo",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003983 .data = &ipv6_devconf.accept_ra_pinfo,
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -08003984 .maxlen = sizeof(int),
3985 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003986 .proc_handler = &proc_dointvec,
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -08003987 },
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08003988#ifdef CONFIG_IPV6_ROUTER_PREF
3989 {
3990 .ctl_name = NET_IPV6_ACCEPT_RA_RTR_PREF,
3991 .procname = "accept_ra_rtr_pref",
3992 .data = &ipv6_devconf.accept_ra_rtr_pref,
3993 .maxlen = sizeof(int),
3994 .mode = 0644,
3995 .proc_handler = &proc_dointvec,
3996 },
YOSHIFUJI Hideaki52e163562006-03-20 17:05:47 -08003997 {
3998 .ctl_name = NET_IPV6_RTR_PROBE_INTERVAL,
3999 .procname = "router_probe_interval",
4000 .data = &ipv6_devconf.rtr_probe_interval,
4001 .maxlen = sizeof(int),
4002 .mode = 0644,
4003 .proc_handler = &proc_dointvec_jiffies,
4004 .strategy = &sysctl_jiffies,
4005 },
Neil Hormanfa03ef32007-01-30 14:30:10 -08004006#ifdef CONFIG_IPV6_ROUTE_INFO
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -08004007 {
4008 .ctl_name = NET_IPV6_ACCEPT_RA_RT_INFO_MAX_PLEN,
4009 .procname = "accept_ra_rt_info_max_plen",
4010 .data = &ipv6_devconf.accept_ra_rt_info_max_plen,
4011 .maxlen = sizeof(int),
4012 .mode = 0644,
4013 .proc_handler = &proc_dointvec,
4014 },
4015#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08004016#endif
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -08004017 {
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -07004018 .ctl_name = NET_IPV6_PROXY_NDP,
4019 .procname = "proxy_ndp",
4020 .data = &ipv6_devconf.proxy_ndp,
4021 .maxlen = sizeof(int),
4022 .mode = 0644,
4023 .proc_handler = &proc_dointvec,
4024 },
4025 {
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -07004026 .ctl_name = NET_IPV6_ACCEPT_SOURCE_ROUTE,
4027 .procname = "accept_source_route",
4028 .data = &ipv6_devconf.accept_source_route,
4029 .maxlen = sizeof(int),
4030 .mode = 0644,
4031 .proc_handler = &proc_dointvec,
4032 },
Neil Horman95c385b2007-04-25 17:08:10 -07004033#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
4034 {
4035 .ctl_name = CTL_UNNUMBERED,
4036 .procname = "optimistic_dad",
4037 .data = &ipv6_devconf.optimistic_dad,
4038 .maxlen = sizeof(int),
4039 .mode = 0644,
4040 .proc_handler = &proc_dointvec,
4041
4042 },
4043#endif
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -07004044 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045 .ctl_name = 0, /* sentinel */
4046 }
4047 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048};
4049
Pavel Emelyanovbff16c22008-01-10 17:42:13 -08004050static int __addrconf_sysctl_register(struct net *net, char *dev_name,
4051 int ctl_name, struct inet6_dev *idev, struct ipv6_devconf *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052{
4053 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054 struct addrconf_sysctl_table *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055
Pavel Emelyanov1dab6222007-12-02 00:59:38 +11004056#define ADDRCONF_CTL_PATH_DEV 3
4057
4058 struct ctl_path addrconf_ctl_path[] = {
4059 { .procname = "net", .ctl_name = CTL_NET, },
4060 { .procname = "ipv6", .ctl_name = NET_IPV6, },
4061 { .procname = "conf", .ctl_name = NET_IPV6_CONF, },
4062 { /* to be set */ },
4063 { },
4064 };
4065
4066
Arnaldo Carvalho de Meloaf879cc2006-11-17 12:14:37 -02004067 t = kmemdup(&addrconf_sysctl, sizeof(*t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068 if (t == NULL)
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11004069 goto out;
4070
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071 for (i=0; t->addrconf_vars[i].data; i++) {
4072 t->addrconf_vars[i].data += (char*)p - (char*)&ipv6_devconf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073 t->addrconf_vars[i].extra1 = idev; /* embedded; no ref */
Pavel Emelyanovbff16c22008-01-10 17:42:13 -08004074 t->addrconf_vars[i].extra2 = net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004077 /*
4078 * Make a copy of dev_name, because '.procname' is regarded as const
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079 * by sysctl and we wouldn't want anyone to change it under our feet
4080 * (see SIOCSIFNAME).
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004081 */
Pavel Emelyanov1dab6222007-12-02 00:59:38 +11004082 t->dev_name = kstrdup(dev_name, GFP_KERNEL);
4083 if (!t->dev_name)
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11004084 goto free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085
Pavel Emelyanov1dab6222007-12-02 00:59:38 +11004086 addrconf_ctl_path[ADDRCONF_CTL_PATH_DEV].procname = t->dev_name;
4087 addrconf_ctl_path[ADDRCONF_CTL_PATH_DEV].ctl_name = ctl_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088
Pavel Emelyanovbff16c22008-01-10 17:42:13 -08004089 t->sysctl_header = register_net_sysctl_table(net, addrconf_ctl_path,
Pavel Emelyanov1dab6222007-12-02 00:59:38 +11004090 t->addrconf_vars);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091 if (t->sysctl_header == NULL)
4092 goto free_procname;
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11004093
4094 p->sysctl = t;
Pavel Emelyanov95897312008-01-10 17:41:45 -08004095 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11004097free_procname:
Pavel Emelyanov1dab6222007-12-02 00:59:38 +11004098 kfree(t->dev_name);
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11004099free:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100 kfree(t);
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11004101out:
Pavel Emelyanov95897312008-01-10 17:41:45 -08004102 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004103}
4104
Pavel Emelyanov408c4762008-01-10 17:41:21 -08004105static void __addrconf_sysctl_unregister(struct ipv6_devconf *p)
4106{
4107 struct addrconf_sysctl_table *t;
4108
4109 if (p->sysctl == NULL)
4110 return;
4111
4112 t = p->sysctl;
4113 p->sysctl = NULL;
4114 unregister_sysctl_table(t->sysctl_header);
4115 kfree(t->dev_name);
4116 kfree(t);
4117}
4118
Pavel Emelyanovf52295a2007-12-02 00:58:37 +11004119static void addrconf_sysctl_register(struct inet6_dev *idev)
4120{
Pavel Emelyanov408c4762008-01-10 17:41:21 -08004121 neigh_sysctl_register(idev->dev, idev->nd_parms, NET_IPV6,
4122 NET_IPV6_NEIGH, "ipv6",
4123 &ndisc_ifinfo_sysctl_change,
4124 NULL);
Pavel Emelyanovbff16c22008-01-10 17:42:13 -08004125 __addrconf_sysctl_register(idev->dev->nd_net, idev->dev->name,
4126 idev->dev->ifindex, idev, &idev->cnf);
Pavel Emelyanovf52295a2007-12-02 00:58:37 +11004127}
4128
Pavel Emelyanov408c4762008-01-10 17:41:21 -08004129static void addrconf_sysctl_unregister(struct inet6_dev *idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130{
Pavel Emelyanov408c4762008-01-10 17:41:21 -08004131 __addrconf_sysctl_unregister(&idev->cnf);
4132 neigh_sysctl_unregister(idev->nd_parms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133}
4134
4135
4136#endif
4137
4138/*
4139 * Device notifier
4140 */
4141
4142int register_inet6addr_notifier(struct notifier_block *nb)
4143{
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004144 return atomic_notifier_chain_register(&inet6addr_chain, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145}
4146
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +09004147EXPORT_SYMBOL(register_inet6addr_notifier);
4148
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149int unregister_inet6addr_notifier(struct notifier_block *nb)
4150{
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004151 return atomic_notifier_chain_unregister(&inet6addr_chain,nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152}
4153
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +09004154EXPORT_SYMBOL(unregister_inet6addr_notifier);
4155
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156/*
4157 * Init / cleanup code
4158 */
4159
4160int __init addrconf_init(void)
4161{
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +09004162 int err;
4163
4164 if ((err = ipv6_addr_label_init()) < 0) {
4165 printk(KERN_CRIT "IPv6 Addrconf: cannot initialize default policy table: %d.\n",
4166 err);
4167 return err;
4168 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169
4170 /* The addrconf netdev notifier requires that loopback_dev
4171 * has it's ipv6 private information allocated and setup
4172 * before it can bring up and give link-local addresses
4173 * to other devices which are up.
4174 *
4175 * Unfortunately, loopback_dev is not necessarily the first
4176 * entry in the global dev_base list of net devices. In fact,
4177 * it is likely to be the very last entry on that list.
4178 * So this causes the notifier registry below to try and
4179 * give link-local addresses to all devices besides loopback_dev
4180 * first, then loopback_dev, which cases all the non-loopback_dev
4181 * devices to fail to get a link-local address.
4182 *
4183 * So, as a temporary fix, allocate the ipv6 structure for
4184 * loopback_dev first by hand.
4185 * Longer term, all of the dependencies ipv6 has upon the loopback
4186 * device and it being up should be removed.
4187 */
4188 rtnl_lock();
Eric W. Biederman2774c7a2007-09-26 22:10:56 -07004189 if (!ipv6_add_dev(init_net.loopback_dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190 err = -ENOMEM;
4191 rtnl_unlock();
4192 if (err)
4193 return err;
4194
Eric W. Biederman2774c7a2007-09-26 22:10:56 -07004195 ip6_null_entry.u.dst.dev = init_net.loopback_dev;
4196 ip6_null_entry.rt6i_idev = in6_dev_get(init_net.loopback_dev);
YOSHIFUJI Hideaki9a6bf6f2007-05-09 14:03:28 -07004197#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Eric W. Biederman2774c7a2007-09-26 22:10:56 -07004198 ip6_prohibit_entry.u.dst.dev = init_net.loopback_dev;
4199 ip6_prohibit_entry.rt6i_idev = in6_dev_get(init_net.loopback_dev);
4200 ip6_blk_hole_entry.u.dst.dev = init_net.loopback_dev;
4201 ip6_blk_hole_entry.rt6i_idev = in6_dev_get(init_net.loopback_dev);
YOSHIFUJI Hideaki9a6bf6f2007-05-09 14:03:28 -07004202#endif
Herbert Xuc62dba92005-09-26 15:10:16 -07004203
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204 register_netdevice_notifier(&ipv6_dev_notf);
4205
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206 addrconf_verify(0);
Thomas Grafc127ea22007-03-22 11:58:32 -07004207
4208 err = __rtnl_register(PF_INET6, RTM_GETLINK, NULL, inet6_dump_ifinfo);
4209 if (err < 0)
4210 goto errout;
4211
4212 /* Only the first call to __rtnl_register can fail */
4213 __rtnl_register(PF_INET6, RTM_NEWADDR, inet6_rtm_newaddr, NULL);
4214 __rtnl_register(PF_INET6, RTM_DELADDR, inet6_rtm_deladdr, NULL);
4215 __rtnl_register(PF_INET6, RTM_GETADDR, inet6_rtm_getaddr, inet6_dump_ifaddr);
4216 __rtnl_register(PF_INET6, RTM_GETMULTICAST, NULL, inet6_dump_ifmcaddr);
4217 __rtnl_register(PF_INET6, RTM_GETANYCAST, NULL, inet6_dump_ifacaddr);
4218
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +09004219 ipv6_addr_label_rtnl_register();
4220
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221#ifdef CONFIG_SYSCTL
Pavel Emelyanovbff16c22008-01-10 17:42:13 -08004222 __addrconf_sysctl_register(&init_net, "all", NET_PROTO_CONF_ALL,
Pavel Emelyanovf52295a2007-12-02 00:58:37 +11004223 NULL, &ipv6_devconf);
Pavel Emelyanovbff16c22008-01-10 17:42:13 -08004224 __addrconf_sysctl_register(&init_net, "default", NET_PROTO_CONF_DEFAULT,
Pavel Emelyanovf52295a2007-12-02 00:58:37 +11004225 NULL, &ipv6_devconf_dflt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226#endif
4227
4228 return 0;
Thomas Grafc127ea22007-03-22 11:58:32 -07004229errout:
4230 unregister_netdevice_notifier(&ipv6_dev_notf);
4231
4232 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233}
4234
Daniel Lezcano09f77092007-12-13 05:34:58 -08004235void addrconf_cleanup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236{
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004237 struct net_device *dev;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004238 struct inet6_ifaddr *ifa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239 int i;
4240
4241 unregister_netdevice_notifier(&ipv6_dev_notf);
4242
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243#ifdef CONFIG_SYSCTL
Pavel Emelyanov408c4762008-01-10 17:41:21 -08004244 __addrconf_sysctl_unregister(&ipv6_devconf_dflt);
4245 __addrconf_sysctl_unregister(&ipv6_devconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246#endif
4247
4248 rtnl_lock();
4249
4250 /*
4251 * clean dev list.
4252 */
4253
Eric W. Biederman881d9662007-09-17 11:56:21 -07004254 for_each_netdev(&init_net, dev) {
Micah Gruberdffe4f02007-07-10 23:04:19 -07004255 if (__in6_dev_get(dev) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256 continue;
4257 addrconf_ifdown(dev, 1);
4258 }
Eric W. Biederman2774c7a2007-09-26 22:10:56 -07004259 addrconf_ifdown(init_net.loopback_dev, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260
4261 /*
4262 * Check hash table.
4263 */
4264
4265 write_lock_bh(&addrconf_hash_lock);
4266 for (i=0; i < IN6_ADDR_HSIZE; i++) {
4267 for (ifa=inet6_addr_lst[i]; ifa; ) {
4268 struct inet6_ifaddr *bifa;
4269
4270 bifa = ifa;
4271 ifa = ifa->lst_next;
4272 printk(KERN_DEBUG "bug: IPv6 address leakage detected: ifa=%p\n", bifa);
4273 /* Do not free it; something is wrong.
4274 Now we can investigate it with debugger.
4275 */
4276 }
4277 }
4278 write_unlock_bh(&addrconf_hash_lock);
4279
4280 del_timer(&addr_chk_timer);
4281
4282 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283}