blob: 6a48bb88f46dfbef744b06bba899838adcaaabfe [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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105static void addrconf_sysctl_unregister(struct ipv6_devconf *p);
106#endif
107
108#ifdef CONFIG_IPV6_PRIVACY
109static int __ipv6_regen_rndid(struct inet6_dev *idev);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900110static int __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111static void ipv6_regen_rndid(unsigned long data);
112
113static int desync_factor = MAX_DESYNC_FACTOR * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114#endif
115
116static int ipv6_count_addresses(struct inet6_dev *idev);
117
118/*
119 * Configured unicast address hash table
120 */
121static struct inet6_ifaddr *inet6_addr_lst[IN6_ADDR_HSIZE];
122static DEFINE_RWLOCK(addrconf_hash_lock);
123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124static void addrconf_verify(unsigned long);
125
Ingo Molnar8d06afa2005-09-09 13:10:40 -0700126static DEFINE_TIMER(addr_chk_timer, addrconf_verify, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127static DEFINE_SPINLOCK(addrconf_verify_lock);
128
129static void addrconf_join_anycast(struct inet6_ifaddr *ifp);
130static void addrconf_leave_anycast(struct inet6_ifaddr *ifp);
131
132static int addrconf_ifdown(struct net_device *dev, int how);
133
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -0700134static void addrconf_dad_start(struct inet6_ifaddr *ifp, u32 flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135static void addrconf_dad_timer(unsigned long data);
136static void addrconf_dad_completed(struct inet6_ifaddr *ifp);
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +0900137static void addrconf_dad_run(struct inet6_dev *idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138static void addrconf_rs_timer(unsigned long data);
139static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
140static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
141
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900142static void inet6_prefix_notify(int event, struct inet6_dev *idev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 struct prefix_info *pinfo);
144static int ipv6_chk_same_addr(const struct in6_addr *addr, struct net_device *dev);
145
Alan Sterne041c682006-03-27 01:16:30 -0800146static ATOMIC_NOTIFIER_HEAD(inet6addr_chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Brian Haleyab32ea52006-09-22 14:15:41 -0700148struct ipv6_devconf ipv6_devconf __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 .forwarding = 0,
150 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
151 .mtu6 = IPV6_MIN_MTU,
152 .accept_ra = 1,
153 .accept_redirects = 1,
154 .autoconf = 1,
155 .force_mld_version = 0,
156 .dad_transmits = 1,
157 .rtr_solicits = MAX_RTR_SOLICITATIONS,
158 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
159 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
160#ifdef CONFIG_IPV6_PRIVACY
161 .use_tempaddr = 0,
162 .temp_valid_lft = TEMP_VALID_LIFETIME,
163 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
164 .regen_max_retry = REGEN_MAX_RETRY,
165 .max_desync_factor = MAX_DESYNC_FACTOR,
166#endif
167 .max_addresses = IPV6_MAX_ADDRESSES,
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -0800168 .accept_ra_defrtr = 1,
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -0800169 .accept_ra_pinfo = 1,
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800170#ifdef CONFIG_IPV6_ROUTER_PREF
171 .accept_ra_rtr_pref = 1,
YOSHIFUJI Hideaki52e16352006-03-20 17:05:47 -0800172 .rtr_probe_interval = 60 * HZ,
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -0800173#ifdef CONFIG_IPV6_ROUTE_INFO
174 .accept_ra_rt_info_max_plen = 0,
175#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800176#endif
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -0700177 .proxy_ndp = 0,
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -0700178 .accept_source_route = 0, /* we do not accept RH0 by default. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179};
180
Brian Haleyab32ea52006-09-22 14:15:41 -0700181static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 .forwarding = 0,
183 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
184 .mtu6 = IPV6_MIN_MTU,
185 .accept_ra = 1,
186 .accept_redirects = 1,
187 .autoconf = 1,
188 .dad_transmits = 1,
189 .rtr_solicits = MAX_RTR_SOLICITATIONS,
190 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
191 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
192#ifdef CONFIG_IPV6_PRIVACY
193 .use_tempaddr = 0,
194 .temp_valid_lft = TEMP_VALID_LIFETIME,
195 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
196 .regen_max_retry = REGEN_MAX_RETRY,
197 .max_desync_factor = MAX_DESYNC_FACTOR,
198#endif
199 .max_addresses = IPV6_MAX_ADDRESSES,
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -0800200 .accept_ra_defrtr = 1,
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -0800201 .accept_ra_pinfo = 1,
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800202#ifdef CONFIG_IPV6_ROUTER_PREF
203 .accept_ra_rtr_pref = 1,
YOSHIFUJI Hideaki52e16352006-03-20 17:05:47 -0800204 .rtr_probe_interval = 60 * HZ,
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -0800205#ifdef CONFIG_IPV6_ROUTE_INFO
206 .accept_ra_rt_info_max_plen = 0,
207#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800208#endif
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -0700209 .proxy_ndp = 0,
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -0700210 .accept_source_route = 0, /* we do not accept RH0 by default. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211};
212
213/* IPv6 Wildcard Address and Loopback Address defined by RFC2553 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
216
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -0700217/* Check if a valid qdisc is available */
218static inline int addrconf_qdisc_ok(struct net_device *dev)
219{
220 return (dev->qdisc != &noop_qdisc);
221}
222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223static void addrconf_del_timer(struct inet6_ifaddr *ifp)
224{
225 if (del_timer(&ifp->timer))
226 __in6_ifa_put(ifp);
227}
228
229enum addrconf_timer_t
230{
231 AC_NONE,
232 AC_DAD,
233 AC_RS,
234};
235
236static void addrconf_mod_timer(struct inet6_ifaddr *ifp,
237 enum addrconf_timer_t what,
238 unsigned long when)
239{
240 if (!del_timer(&ifp->timer))
241 in6_ifa_hold(ifp);
242
243 switch (what) {
244 case AC_DAD:
245 ifp->timer.function = addrconf_dad_timer;
246 break;
247 case AC_RS:
248 ifp->timer.function = addrconf_rs_timer;
249 break;
250 default:;
251 }
252 ifp->timer.expires = jiffies + when;
253 add_timer(&ifp->timer);
254}
255
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700256static int snmp6_alloc_dev(struct inet6_dev *idev)
257{
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700258 if (snmp_mib_init((void **)idev->stats.ipv6,
YOSHIFUJI Hideakic69bce22008-01-23 22:31:45 -0800259 sizeof(struct ipstats_mib)) < 0)
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700260 goto err_ip;
261 if (snmp_mib_init((void **)idev->stats.icmpv6,
YOSHIFUJI Hideakic69bce22008-01-23 22:31:45 -0800262 sizeof(struct icmpv6_mib)) < 0)
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700263 goto err_icmp;
David L Stevens14878f72007-09-16 16:52:35 -0700264 if (snmp_mib_init((void **)idev->stats.icmpv6msg,
YOSHIFUJI Hideakic69bce22008-01-23 22:31:45 -0800265 sizeof(struct icmpv6msg_mib)) < 0)
David L Stevens14878f72007-09-16 16:52:35 -0700266 goto err_icmpmsg;
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700267
268 return 0;
269
David L Stevens14878f72007-09-16 16:52:35 -0700270err_icmpmsg:
271 snmp_mib_free((void **)idev->stats.icmpv6);
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700272err_icmp:
273 snmp_mib_free((void **)idev->stats.ipv6);
274err_ip:
Pavel Emelyanovaaf70ec2007-10-17 21:25:32 -0700275 return -ENOMEM;
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700276}
277
Pavel Emelyanov16910b92007-10-17 21:23:43 -0700278static void snmp6_free_dev(struct inet6_dev *idev)
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700279{
David L Stevens14878f72007-09-16 16:52:35 -0700280 snmp_mib_free((void **)idev->stats.icmpv6msg);
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700281 snmp_mib_free((void **)idev->stats.icmpv6);
282 snmp_mib_free((void **)idev->stats.ipv6);
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700283}
284
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285/* Nobody refers to this device, we may destroy it. */
286
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -0700287static void in6_dev_finish_destroy_rcu(struct rcu_head *head)
288{
289 struct inet6_dev *idev = container_of(head, struct inet6_dev, rcu);
290 kfree(idev);
291}
292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293void in6_dev_finish_destroy(struct inet6_dev *idev)
294{
295 struct net_device *dev = idev->dev;
296 BUG_TRAP(idev->addr_list==NULL);
297 BUG_TRAP(idev->mc_list==NULL);
298#ifdef NET_REFCNT_DEBUG
299 printk(KERN_DEBUG "in6_dev_finish_destroy: %s\n", dev ? dev->name : "NIL");
300#endif
301 dev_put(dev);
302 if (!idev->dead) {
303 printk("Freeing alive inet6 device %p\n", idev);
304 return;
305 }
306 snmp6_free_dev(idev);
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -0700307 call_rcu(&idev->rcu, in6_dev_finish_destroy_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308}
309
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +0900310EXPORT_SYMBOL(in6_dev_finish_destroy);
311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312static struct inet6_dev * ipv6_add_dev(struct net_device *dev)
313{
314 struct inet6_dev *ndev;
YOSHIFUJI Hideakid88ae4c2007-01-14 21:48:40 -0800315 struct in6_addr maddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
317 ASSERT_RTNL();
318
319 if (dev->mtu < IPV6_MIN_MTU)
320 return NULL;
321
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900322 ndev = kzalloc(sizeof(struct inet6_dev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900324 if (ndev == NULL)
325 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
Ingo Oeser322f74a2006-03-20 23:01:47 -0800327 rwlock_init(&ndev->lock);
328 ndev->dev = dev;
329 memcpy(&ndev->cnf, &ipv6_devconf_dflt, sizeof(ndev->cnf));
330 ndev->cnf.mtu6 = dev->mtu;
331 ndev->cnf.sysctl = NULL;
332 ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl);
333 if (ndev->nd_parms == NULL) {
334 kfree(ndev);
335 return NULL;
336 }
337 /* We refer to the device */
338 dev_hold(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Ingo Oeser322f74a2006-03-20 23:01:47 -0800340 if (snmp6_alloc_dev(ndev) < 0) {
341 ADBG((KERN_WARNING
342 "%s(): cannot allocate memory for statistics; dev=%s.\n",
343 __FUNCTION__, dev->name));
344 neigh_parms_release(&nd_tbl, ndev->nd_parms);
345 ndev->dead = 1;
346 in6_dev_finish_destroy(ndev);
347 return NULL;
348 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Ingo Oeser322f74a2006-03-20 23:01:47 -0800350 if (snmp6_register_dev(ndev) < 0) {
351 ADBG((KERN_WARNING
352 "%s(): cannot create /proc/net/dev_snmp6/%s\n",
353 __FUNCTION__, dev->name));
354 neigh_parms_release(&nd_tbl, ndev->nd_parms);
355 ndev->dead = 1;
356 in6_dev_finish_destroy(ndev);
357 return NULL;
358 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
Ingo Oeser322f74a2006-03-20 23:01:47 -0800360 /* One reference from device. We must do this before
361 * we invoke __ipv6_regen_rndid().
362 */
363 in6_dev_hold(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
365#ifdef CONFIG_IPV6_PRIVACY
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800366 setup_timer(&ndev->regen_timer, ipv6_regen_rndid, (unsigned long)ndev);
Ingo Oeser322f74a2006-03-20 23:01:47 -0800367 if ((dev->flags&IFF_LOOPBACK) ||
368 dev->type == ARPHRD_TUNNEL ||
Joerg Roedel0be669b2006-10-10 14:49:53 -0700369#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
370 dev->type == ARPHRD_SIT ||
371#endif
372 dev->type == ARPHRD_NONE) {
Ingo Oeser322f74a2006-03-20 23:01:47 -0800373 printk(KERN_INFO
374 "%s: Disabled Privacy Extensions\n",
375 dev->name);
376 ndev->cnf.use_tempaddr = -1;
Fred L. Templinc7dc89c2007-11-29 22:11:40 +1100377
378 if (dev->type == ARPHRD_SIT && (dev->priv_flags & IFF_ISATAP)) {
379 printk(KERN_INFO
380 "%s: Disabled Multicast RS\n",
381 dev->name);
382 ndev->cnf.rtr_solicits = 0;
383 }
Ingo Oeser322f74a2006-03-20 23:01:47 -0800384 } else {
385 in6_dev_hold(ndev);
386 ipv6_regen_rndid((unsigned long) ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 }
Ingo Oeser322f74a2006-03-20 23:01:47 -0800388#endif
389
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -0700390 if (netif_running(dev) && addrconf_qdisc_ok(dev))
Herbert Xu53aadcc2007-03-27 14:31:52 -0700391 ndev->if_flags |= IF_READY;
392
Ingo Oeser322f74a2006-03-20 23:01:47 -0800393 ipv6_mc_init_dev(ndev);
394 ndev->tstamp = jiffies;
395#ifdef CONFIG_SYSCTL
396 neigh_sysctl_register(dev, ndev->nd_parms, NET_IPV6,
397 NET_IPV6_NEIGH, "ipv6",
398 &ndisc_ifinfo_sysctl_change,
399 NULL);
Pavel Emelyanovf52295a2007-12-02 00:58:37 +1100400 addrconf_sysctl_register(ndev);
Ingo Oeser322f74a2006-03-20 23:01:47 -0800401#endif
David L Stevens30c4cf52007-01-04 12:31:14 -0800402 /* protected by rtnl_lock */
403 rcu_assign_pointer(dev->ip6_ptr, ndev);
YOSHIFUJI Hideakid88ae4c2007-01-14 21:48:40 -0800404
405 /* Join all-node multicast group */
406 ipv6_addr_all_nodes(&maddr);
407 ipv6_dev_mc_inc(dev, &maddr);
408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 return ndev;
410}
411
412static struct inet6_dev * ipv6_find_idev(struct net_device *dev)
413{
414 struct inet6_dev *idev;
415
416 ASSERT_RTNL();
417
418 if ((idev = __in6_dev_get(dev)) == NULL) {
419 if ((idev = ipv6_add_dev(dev)) == NULL)
420 return NULL;
421 }
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +0900422
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 if (dev->flags&IFF_UP)
424 ipv6_mc_up(idev);
425 return idev;
426}
427
428#ifdef CONFIG_SYSCTL
429static void dev_forward_change(struct inet6_dev *idev)
430{
431 struct net_device *dev;
432 struct inet6_ifaddr *ifa;
433 struct in6_addr addr;
434
435 if (!idev)
436 return;
437 dev = idev->dev;
438 if (dev && (dev->flags & IFF_MULTICAST)) {
439 ipv6_addr_all_routers(&addr);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 if (idev->cnf.forwarding)
442 ipv6_dev_mc_inc(dev, &addr);
443 else
444 ipv6_dev_mc_dec(dev, &addr);
445 }
446 for (ifa=idev->addr_list; ifa; ifa=ifa->if_next) {
Michal Wrobel2c12a742007-02-26 15:36:10 -0800447 if (ifa->flags&IFA_F_TENTATIVE)
448 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 if (idev->cnf.forwarding)
450 addrconf_join_anycast(ifa);
451 else
452 addrconf_leave_anycast(ifa);
453 }
454}
455
456
457static void addrconf_forward_change(void)
458{
459 struct net_device *dev;
460 struct inet6_dev *idev;
461
462 read_lock(&dev_base_lock);
Eric W. Biederman881d9662007-09-17 11:56:21 -0700463 for_each_netdev(&init_net, dev) {
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -0700464 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 idev = __in6_dev_get(dev);
466 if (idev) {
467 int changed = (!idev->cnf.forwarding) ^ (!ipv6_devconf.forwarding);
468 idev->cnf.forwarding = ipv6_devconf.forwarding;
469 if (changed)
470 dev_forward_change(idev);
471 }
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -0700472 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 }
474 read_unlock(&dev_base_lock);
475}
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800476
477static void addrconf_fixup_forwarding(struct ctl_table *table, int *p, int old)
478{
479 if (p == &ipv6_devconf_dflt.forwarding)
480 return;
481
482 if (p == &ipv6_devconf.forwarding) {
483 ipv6_devconf_dflt.forwarding = ipv6_devconf.forwarding;
484 addrconf_forward_change();
485 } else if ((!*p) ^ (!old))
486 dev_forward_change((struct inet6_dev *)table->extra1);
487
488 if (*p)
489 rt6_purge_dflt_routers();
490}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491#endif
492
493/* Nobody refers to this ifaddr, destroy it */
494
495void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp)
496{
497 BUG_TRAP(ifp->if_next==NULL);
498 BUG_TRAP(ifp->lst_next==NULL);
499#ifdef NET_REFCNT_DEBUG
500 printk(KERN_DEBUG "inet6_ifa_finish_destroy\n");
501#endif
502
503 in6_dev_put(ifp->idev);
504
505 if (del_timer(&ifp->timer))
506 printk("Timer is still running, when freeing ifa=%p\n", ifp);
507
508 if (!ifp->dead) {
509 printk("Freeing alive inet6 address %p\n", ifp);
510 return;
511 }
512 dst_release(&ifp->rt->u.dst);
513
514 kfree(ifp);
515}
516
Brian Haleye55ffac2006-07-10 15:25:51 -0700517static void
518ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp)
519{
520 struct inet6_ifaddr *ifa, **ifap;
YOSHIFUJI Hideaki8a6ce0c2006-07-11 13:05:30 -0700521 int ifp_scope = ipv6_addr_src_scope(&ifp->addr);
Brian Haleye55ffac2006-07-10 15:25:51 -0700522
523 /*
524 * Each device address list is sorted in order of scope -
525 * global before linklocal.
526 */
527 for (ifap = &idev->addr_list; (ifa = *ifap) != NULL;
528 ifap = &ifa->if_next) {
YOSHIFUJI Hideaki8a6ce0c2006-07-11 13:05:30 -0700529 if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr))
Brian Haleye55ffac2006-07-10 15:25:51 -0700530 break;
531 }
532
533 ifp->if_next = *ifap;
534 *ifap = ifp;
535}
536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537/* On success it returns ifp with increased reference count */
538
539static struct inet6_ifaddr *
540ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr, int pfxlen,
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -0700541 int scope, u32 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542{
543 struct inet6_ifaddr *ifa = NULL;
544 struct rt6_info *rt;
545 int hash;
546 int err = 0;
547
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -0700548 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 if (idev->dead) {
550 err = -ENODEV; /*XXX*/
551 goto out2;
552 }
553
554 write_lock(&addrconf_hash_lock);
555
556 /* Ignore adding duplicate addresses on an interface */
557 if (ipv6_chk_same_addr(addr, idev->dev)) {
558 ADBG(("ipv6_add_addr: already assigned\n"));
559 err = -EEXIST;
560 goto out;
561 }
562
Ingo Oeser322f74a2006-03-20 23:01:47 -0800563 ifa = kzalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
565 if (ifa == NULL) {
566 ADBG(("ipv6_add_addr: malloc failed\n"));
567 err = -ENOBUFS;
568 goto out;
569 }
570
571 rt = addrconf_dst_alloc(idev, addr, 0);
572 if (IS_ERR(rt)) {
573 err = PTR_ERR(rt);
574 goto out;
575 }
576
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 ipv6_addr_copy(&ifa->addr, addr);
578
579 spin_lock_init(&ifa->lock);
580 init_timer(&ifa->timer);
581 ifa->timer.data = (unsigned long) ifa;
582 ifa->scope = scope;
583 ifa->prefix_len = pfxlen;
584 ifa->flags = flags | IFA_F_TENTATIVE;
585 ifa->cstamp = ifa->tstamp = jiffies;
586
Keir Fraser57f5f542006-08-29 02:43:49 -0700587 ifa->rt = rt;
588
Neil Horman95c385b2007-04-25 17:08:10 -0700589 /*
590 * part one of RFC 4429, section 3.3
591 * We should not configure an address as
592 * optimistic if we do not yet know the link
593 * layer address of our nexhop router
594 */
595
596 if (rt->rt6i_nexthop == NULL)
597 ifa->flags &= ~IFA_F_OPTIMISTIC;
598
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 ifa->idev = idev;
600 in6_dev_hold(idev);
601 /* For caller */
602 in6_ifa_hold(ifa);
603
604 /* Add to big hash table */
605 hash = ipv6_addr_hash(addr);
606
607 ifa->lst_next = inet6_addr_lst[hash];
608 inet6_addr_lst[hash] = ifa;
609 in6_ifa_hold(ifa);
610 write_unlock(&addrconf_hash_lock);
611
612 write_lock(&idev->lock);
613 /* Add to inet6_dev unicast addr list. */
Brian Haleye55ffac2006-07-10 15:25:51 -0700614 ipv6_link_dev_addr(idev, ifa);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
616#ifdef CONFIG_IPV6_PRIVACY
617 if (ifa->flags&IFA_F_TEMPORARY) {
618 ifa->tmp_next = idev->tempaddr_list;
619 idev->tempaddr_list = ifa;
620 in6_ifa_hold(ifa);
621 }
622#endif
623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 in6_ifa_hold(ifa);
625 write_unlock(&idev->lock);
626out2:
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -0700627 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
YOSHIFUJI Hideakifd928332005-04-19 22:27:09 -0700629 if (likely(err == 0))
Alan Sterne041c682006-03-27 01:16:30 -0800630 atomic_notifier_call_chain(&inet6addr_chain, NETDEV_UP, ifa);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 else {
632 kfree(ifa);
633 ifa = ERR_PTR(err);
634 }
635
636 return ifa;
637out:
638 write_unlock(&addrconf_hash_lock);
639 goto out2;
640}
641
642/* This function wants to get referenced ifp and releases it before return */
643
644static void ipv6_del_addr(struct inet6_ifaddr *ifp)
645{
646 struct inet6_ifaddr *ifa, **ifap;
647 struct inet6_dev *idev = ifp->idev;
648 int hash;
649 int deleted = 0, onlink = 0;
650 unsigned long expires = jiffies;
651
652 hash = ipv6_addr_hash(&ifp->addr);
653
654 ifp->dead = 1;
655
656 write_lock_bh(&addrconf_hash_lock);
657 for (ifap = &inet6_addr_lst[hash]; (ifa=*ifap) != NULL;
658 ifap = &ifa->lst_next) {
659 if (ifa == ifp) {
660 *ifap = ifa->lst_next;
661 __in6_ifa_put(ifp);
662 ifa->lst_next = NULL;
663 break;
664 }
665 }
666 write_unlock_bh(&addrconf_hash_lock);
667
668 write_lock_bh(&idev->lock);
669#ifdef CONFIG_IPV6_PRIVACY
670 if (ifp->flags&IFA_F_TEMPORARY) {
671 for (ifap = &idev->tempaddr_list; (ifa=*ifap) != NULL;
672 ifap = &ifa->tmp_next) {
673 if (ifa == ifp) {
674 *ifap = ifa->tmp_next;
675 if (ifp->ifpub) {
676 in6_ifa_put(ifp->ifpub);
677 ifp->ifpub = NULL;
678 }
679 __in6_ifa_put(ifp);
680 ifa->tmp_next = NULL;
681 break;
682 }
683 }
684 }
685#endif
686
Kristian Slavov1d142802005-12-21 18:47:24 -0800687 for (ifap = &idev->addr_list; (ifa=*ifap) != NULL;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 if (ifa == ifp) {
689 *ifap = ifa->if_next;
690 __in6_ifa_put(ifp);
691 ifa->if_next = NULL;
692 if (!(ifp->flags & IFA_F_PERMANENT) || onlink > 0)
693 break;
694 deleted = 1;
Kristian Slavov1d142802005-12-21 18:47:24 -0800695 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 } else if (ifp->flags & IFA_F_PERMANENT) {
697 if (ipv6_prefix_equal(&ifa->addr, &ifp->addr,
698 ifp->prefix_len)) {
699 if (ifa->flags & IFA_F_PERMANENT) {
700 onlink = 1;
701 if (deleted)
702 break;
703 } else {
704 unsigned long lifetime;
705
706 if (!onlink)
707 onlink = -1;
708
709 spin_lock(&ifa->lock);
710 lifetime = min_t(unsigned long,
711 ifa->valid_lft, 0x7fffffffUL/HZ);
712 if (time_before(expires,
713 ifa->tstamp + lifetime * HZ))
714 expires = ifa->tstamp + lifetime * HZ;
715 spin_unlock(&ifa->lock);
716 }
717 }
718 }
Kristian Slavov1d142802005-12-21 18:47:24 -0800719 ifap = &ifa->if_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 }
721 write_unlock_bh(&idev->lock);
722
723 ipv6_ifa_notify(RTM_DELADDR, ifp);
724
Alan Sterne041c682006-03-27 01:16:30 -0800725 atomic_notifier_call_chain(&inet6addr_chain, NETDEV_DOWN, ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
727 addrconf_del_timer(ifp);
728
729 /*
730 * Purge or update corresponding prefix
731 *
732 * 1) we don't purge prefix here if address was not permanent.
733 * prefix is managed by its own lifetime.
734 * 2) if there're no addresses, delete prefix.
735 * 3) if there're still other permanent address(es),
736 * corresponding prefix is still permanent.
737 * 4) otherwise, update prefix lifetime to the
738 * longest valid lifetime among the corresponding
739 * addresses on the device.
740 * Note: subsequent RA will update lifetime.
741 *
742 * --yoshfuji
743 */
744 if ((ifp->flags & IFA_F_PERMANENT) && onlink < 1) {
745 struct in6_addr prefix;
746 struct rt6_info *rt;
747
748 ipv6_addr_prefix(&prefix, &ifp->addr, ifp->prefix_len);
749 rt = rt6_lookup(&prefix, NULL, ifp->idev->dev->ifindex, 1);
750
751 if (rt && ((rt->rt6i_flags & (RTF_GATEWAY | RTF_DEFAULT)) == 0)) {
752 if (onlink == 0) {
Thomas Grafe0a1ad732006-08-22 00:00:21 -0700753 ip6_del_rt(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 rt = NULL;
755 } else if (!(rt->rt6i_flags & RTF_EXPIRES)) {
756 rt->rt6i_expires = expires;
757 rt->rt6i_flags |= RTF_EXPIRES;
758 }
759 }
760 dst_release(&rt->u.dst);
761 }
762
763 in6_ifa_put(ifp);
764}
765
766#ifdef CONFIG_IPV6_PRIVACY
767static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift)
768{
769 struct inet6_dev *idev = ifp->idev;
770 struct in6_addr addr, *tmpaddr;
771 unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_cstamp, tmp_tstamp;
772 int tmp_plen;
773 int ret = 0;
774 int max_addresses;
Neil Horman95c385b2007-04-25 17:08:10 -0700775 u32 addr_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
777 write_lock(&idev->lock);
778 if (ift) {
779 spin_lock_bh(&ift->lock);
780 memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8);
781 spin_unlock_bh(&ift->lock);
782 tmpaddr = &addr;
783 } else {
784 tmpaddr = NULL;
785 }
786retry:
787 in6_dev_hold(idev);
788 if (idev->cnf.use_tempaddr <= 0) {
789 write_unlock(&idev->lock);
790 printk(KERN_INFO
791 "ipv6_create_tempaddr(): use_tempaddr is disabled.\n");
792 in6_dev_put(idev);
793 ret = -1;
794 goto out;
795 }
796 spin_lock_bh(&ifp->lock);
797 if (ifp->regen_count++ >= idev->cnf.regen_max_retry) {
798 idev->cnf.use_tempaddr = -1; /*XXX*/
799 spin_unlock_bh(&ifp->lock);
800 write_unlock(&idev->lock);
801 printk(KERN_WARNING
802 "ipv6_create_tempaddr(): regeneration time exceeded. disabled temporary address support.\n");
803 in6_dev_put(idev);
804 ret = -1;
805 goto out;
806 }
807 in6_ifa_hold(ifp);
808 memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
809 if (__ipv6_try_regen_rndid(idev, tmpaddr) < 0) {
810 spin_unlock_bh(&ifp->lock);
811 write_unlock(&idev->lock);
812 printk(KERN_WARNING
813 "ipv6_create_tempaddr(): regeneration of randomized interface id failed.\n");
814 in6_ifa_put(ifp);
815 in6_dev_put(idev);
816 ret = -1;
817 goto out;
818 }
819 memcpy(&addr.s6_addr[8], idev->rndid, 8);
820 tmp_valid_lft = min_t(__u32,
821 ifp->valid_lft,
822 idev->cnf.temp_valid_lft);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900823 tmp_prefered_lft = min_t(__u32,
824 ifp->prefered_lft,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 idev->cnf.temp_prefered_lft - desync_factor / HZ);
826 tmp_plen = ifp->prefix_len;
827 max_addresses = idev->cnf.max_addresses;
828 tmp_cstamp = ifp->cstamp;
829 tmp_tstamp = ifp->tstamp;
830 spin_unlock_bh(&ifp->lock);
831
832 write_unlock(&idev->lock);
Neil Horman95c385b2007-04-25 17:08:10 -0700833
834 addr_flags = IFA_F_TEMPORARY;
835 /* set in addrconf_prefix_rcv() */
836 if (ifp->flags & IFA_F_OPTIMISTIC)
837 addr_flags |= IFA_F_OPTIMISTIC;
838
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 ift = !max_addresses ||
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900840 ipv6_count_addresses(idev) < max_addresses ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 ipv6_add_addr(idev, &addr, tmp_plen,
Neil Horman95c385b2007-04-25 17:08:10 -0700842 ipv6_addr_type(&addr)&IPV6_ADDR_SCOPE_MASK,
843 addr_flags) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 if (!ift || IS_ERR(ift)) {
845 in6_ifa_put(ifp);
846 in6_dev_put(idev);
847 printk(KERN_INFO
848 "ipv6_create_tempaddr(): retry temporary address regeneration.\n");
849 tmpaddr = &addr;
850 write_lock(&idev->lock);
851 goto retry;
852 }
853
854 spin_lock_bh(&ift->lock);
855 ift->ifpub = ifp;
856 ift->valid_lft = tmp_valid_lft;
857 ift->prefered_lft = tmp_prefered_lft;
858 ift->cstamp = tmp_cstamp;
859 ift->tstamp = tmp_tstamp;
860 spin_unlock_bh(&ift->lock);
861
862 addrconf_dad_start(ift, 0);
863 in6_ifa_put(ift);
864 in6_dev_put(idev);
865out:
866 return ret;
867}
868#endif
869
870/*
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800871 * Choose an appropriate source address (RFC3484)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 */
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800873struct ipv6_saddr_score {
874 int addr_type;
875 unsigned int attrs;
876 int matchlen;
Brian Haley0d27b422006-03-11 18:50:14 -0800877 int scope;
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800878 unsigned int rule;
879};
880
881#define IPV6_SADDR_SCORE_LOCAL 0x0001
882#define IPV6_SADDR_SCORE_PREFERRED 0x0004
883#define IPV6_SADDR_SCORE_HOA 0x0008
884#define IPV6_SADDR_SCORE_OIF 0x0010
885#define IPV6_SADDR_SCORE_LABEL 0x0020
886#define IPV6_SADDR_SCORE_PRIVACY 0x0040
887
Dave Jonesb6f99a22007-03-22 12:27:49 -0700888static inline int ipv6_saddr_preferred(int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889{
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800890 if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|
891 IPV6_ADDR_LOOPBACK|IPV6_ADDR_RESERVED))
892 return 1;
893 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894}
895
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800896int ipv6_dev_get_saddr(struct net_device *daddr_dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 struct in6_addr *daddr, struct in6_addr *saddr)
898{
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800899 struct ipv6_saddr_score hiscore;
900 struct inet6_ifaddr *ifa_result = NULL;
901 int daddr_type = __ipv6_addr_type(daddr);
902 int daddr_scope = __ipv6_addr_src_scope(daddr_type);
YOSHIFUJI Hideaki303065a2007-11-14 15:56:15 +0900903 int daddr_ifindex = daddr_dev ? daddr_dev->ifindex : 0;
904 u32 daddr_label = ipv6_addr_label(daddr, daddr_type, daddr_ifindex);
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800905 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800907 memset(&hiscore, 0, sizeof(hiscore));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
909 read_lock(&dev_base_lock);
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -0700910 rcu_read_lock();
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800911
Eric W. Biederman881d9662007-09-17 11:56:21 -0700912 for_each_netdev(&init_net, dev) {
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800913 struct inet6_dev *idev;
914 struct inet6_ifaddr *ifa;
915
916 /* Rule 0: Candidate Source Address (section 4)
917 * - multicast and link-local destination address,
918 * the set of candidate source address MUST only
919 * include addresses assigned to interfaces
920 * belonging to the same link as the outgoing
921 * interface.
922 * (- For site-local destination addresses, the
923 * set of candidate source addresses MUST only
924 * include addresses assigned to interfaces
925 * belonging to the same site as the outgoing
926 * interface.)
927 */
928 if ((daddr_type & IPV6_ADDR_MULTICAST ||
929 daddr_scope <= IPV6_ADDR_SCOPE_LINKLOCAL) &&
930 daddr_dev && dev != daddr_dev)
931 continue;
932
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 idev = __in6_dev_get(dev);
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800934 if (!idev)
935 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800937 read_lock_bh(&idev->lock);
938 for (ifa = idev->addr_list; ifa; ifa = ifa->if_next) {
939 struct ipv6_saddr_score score;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800941 score.addr_type = __ipv6_addr_type(&ifa->addr);
942
YOSHIFUJI Hideaki6b3ae802005-12-21 22:58:01 +0900943 /* Rule 0:
944 * - Tentative Address (RFC2462 section 5.4)
945 * - A tentative address is not considered
946 * "assigned to an interface" in the traditional
Neil Horman95c385b2007-04-25 17:08:10 -0700947 * sense, unless it is also flagged as optimistic.
YOSHIFUJI Hideaki6b3ae802005-12-21 22:58:01 +0900948 * - Candidate Source Address (section 4)
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800949 * - In any case, anycast addresses, multicast
950 * addresses, and the unspecified address MUST
951 * NOT be included in a candidate set.
952 */
Neil Horman95c385b2007-04-25 17:08:10 -0700953 if ((ifa->flags & IFA_F_TENTATIVE) &&
954 (!(ifa->flags & IFA_F_OPTIMISTIC)))
YOSHIFUJI Hideaki6b3ae802005-12-21 22:58:01 +0900955 continue;
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800956 if (unlikely(score.addr_type == IPV6_ADDR_ANY ||
957 score.addr_type & IPV6_ADDR_MULTICAST)) {
958 LIMIT_NETDEBUG(KERN_DEBUG
Joe Perches3b6d8212007-11-19 23:47:25 -0800959 "ADDRCONF: unspecified / multicast address "
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800960 "assigned as unicast address on %s",
961 dev->name);
962 continue;
963 }
964
965 score.attrs = 0;
966 score.matchlen = 0;
967 score.scope = 0;
968 score.rule = 0;
969
970 if (ifa_result == NULL) {
971 /* record it if the first available entry */
972 goto record_it;
973 }
974
975 /* Rule 1: Prefer same address */
976 if (hiscore.rule < 1) {
977 if (ipv6_addr_equal(&ifa_result->addr, daddr))
978 hiscore.attrs |= IPV6_SADDR_SCORE_LOCAL;
979 hiscore.rule++;
980 }
981 if (ipv6_addr_equal(&ifa->addr, daddr)) {
982 score.attrs |= IPV6_SADDR_SCORE_LOCAL;
983 if (!(hiscore.attrs & IPV6_SADDR_SCORE_LOCAL)) {
984 score.rule = 1;
985 goto record_it;
986 }
987 } else {
988 if (hiscore.attrs & IPV6_SADDR_SCORE_LOCAL)
989 continue;
990 }
991
992 /* Rule 2: Prefer appropriate scope */
993 if (hiscore.rule < 2) {
994 hiscore.scope = __ipv6_addr_src_scope(hiscore.addr_type);
995 hiscore.rule++;
996 }
997 score.scope = __ipv6_addr_src_scope(score.addr_type);
998 if (hiscore.scope < score.scope) {
999 if (hiscore.scope < daddr_scope) {
1000 score.rule = 2;
1001 goto record_it;
1002 } else
1003 continue;
1004 } else if (score.scope < hiscore.scope) {
1005 if (score.scope < daddr_scope)
Brian Haleye55ffac2006-07-10 15:25:51 -07001006 break; /* addresses sorted by scope */
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001007 else {
1008 score.rule = 2;
1009 goto record_it;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 }
1011 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
Neil Horman95c385b2007-04-25 17:08:10 -07001013 /* Rule 3: Avoid deprecated and optimistic addresses */
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001014 if (hiscore.rule < 3) {
1015 if (ipv6_saddr_preferred(hiscore.addr_type) ||
Neil Horman95c385b2007-04-25 17:08:10 -07001016 (((ifa_result->flags &
1017 (IFA_F_DEPRECATED|IFA_F_OPTIMISTIC)) == 0)))
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001018 hiscore.attrs |= IPV6_SADDR_SCORE_PREFERRED;
1019 hiscore.rule++;
1020 }
1021 if (ipv6_saddr_preferred(score.addr_type) ||
Jiri Kosina6ae5f982007-09-16 14:48:21 -07001022 (((ifa->flags &
Neil Horman95c385b2007-04-25 17:08:10 -07001023 (IFA_F_DEPRECATED|IFA_F_OPTIMISTIC)) == 0))) {
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001024 score.attrs |= IPV6_SADDR_SCORE_PREFERRED;
1025 if (!(hiscore.attrs & IPV6_SADDR_SCORE_PREFERRED)) {
1026 score.rule = 3;
1027 goto record_it;
1028 }
1029 } else {
1030 if (hiscore.attrs & IPV6_SADDR_SCORE_PREFERRED)
1031 continue;
1032 }
1033
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07001034 /* Rule 4: Prefer home address */
Masahide NAKAMURA59fbb3a2007-06-26 23:56:32 -07001035#if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07001036 if (hiscore.rule < 4) {
1037 if (ifa_result->flags & IFA_F_HOMEADDRESS)
1038 hiscore.attrs |= IPV6_SADDR_SCORE_HOA;
1039 hiscore.rule++;
1040 }
1041 if (ifa->flags & IFA_F_HOMEADDRESS) {
1042 score.attrs |= IPV6_SADDR_SCORE_HOA;
1043 if (!(ifa_result->flags & IFA_F_HOMEADDRESS)) {
1044 score.rule = 4;
1045 goto record_it;
1046 }
1047 } else {
1048 if (hiscore.attrs & IPV6_SADDR_SCORE_HOA)
1049 continue;
1050 }
1051#else
YOSHIFUJI Hideaki220bbd72005-11-28 22:27:11 -08001052 if (hiscore.rule < 4)
1053 hiscore.rule++;
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07001054#endif
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001055
1056 /* Rule 5: Prefer outgoing interface */
1057 if (hiscore.rule < 5) {
1058 if (daddr_dev == NULL ||
1059 daddr_dev == ifa_result->idev->dev)
1060 hiscore.attrs |= IPV6_SADDR_SCORE_OIF;
1061 hiscore.rule++;
1062 }
1063 if (daddr_dev == NULL ||
1064 daddr_dev == ifa->idev->dev) {
1065 score.attrs |= IPV6_SADDR_SCORE_OIF;
1066 if (!(hiscore.attrs & IPV6_SADDR_SCORE_OIF)) {
1067 score.rule = 5;
1068 goto record_it;
1069 }
1070 } else {
1071 if (hiscore.attrs & IPV6_SADDR_SCORE_OIF)
1072 continue;
1073 }
1074
1075 /* Rule 6: Prefer matching label */
1076 if (hiscore.rule < 6) {
YOSHIFUJI Hideakic1ee6562007-11-14 15:55:29 +09001077 if (ipv6_addr_label(&ifa_result->addr,
YOSHIFUJI Hideaki303065a2007-11-14 15:56:15 +09001078 hiscore.addr_type,
1079 ifa_result->idev->dev->ifindex) == daddr_label)
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001080 hiscore.attrs |= IPV6_SADDR_SCORE_LABEL;
1081 hiscore.rule++;
1082 }
YOSHIFUJI Hideakic1ee6562007-11-14 15:55:29 +09001083 if (ipv6_addr_label(&ifa->addr,
YOSHIFUJI Hideaki303065a2007-11-14 15:56:15 +09001084 score.addr_type,
1085 ifa->idev->dev->ifindex) == daddr_label) {
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001086 score.attrs |= IPV6_SADDR_SCORE_LABEL;
1087 if (!(hiscore.attrs & IPV6_SADDR_SCORE_LABEL)) {
1088 score.rule = 6;
1089 goto record_it;
1090 }
1091 } else {
1092 if (hiscore.attrs & IPV6_SADDR_SCORE_LABEL)
1093 continue;
1094 }
1095
Peter Chubb44fd0262005-11-09 13:05:47 -08001096#ifdef CONFIG_IPV6_PRIVACY
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001097 /* Rule 7: Prefer public address
1098 * Note: prefer temprary address if use_tempaddr >= 2
1099 */
1100 if (hiscore.rule < 7) {
1101 if ((!(ifa_result->flags & IFA_F_TEMPORARY)) ^
1102 (ifa_result->idev->cnf.use_tempaddr >= 2))
1103 hiscore.attrs |= IPV6_SADDR_SCORE_PRIVACY;
1104 hiscore.rule++;
1105 }
1106 if ((!(ifa->flags & IFA_F_TEMPORARY)) ^
1107 (ifa->idev->cnf.use_tempaddr >= 2)) {
1108 score.attrs |= IPV6_SADDR_SCORE_PRIVACY;
1109 if (!(hiscore.attrs & IPV6_SADDR_SCORE_PRIVACY)) {
1110 score.rule = 7;
1111 goto record_it;
1112 }
1113 } else {
1114 if (hiscore.attrs & IPV6_SADDR_SCORE_PRIVACY)
1115 continue;
1116 }
YOSHIFUJI Hideaki5e2707f2006-06-22 01:41:18 -07001117#else
1118 if (hiscore.rule < 7)
1119 hiscore.rule++;
Peter Chubb44fd0262005-11-09 13:05:47 -08001120#endif
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001121 /* Rule 8: Use longest matching prefix */
Yan Zheng12da2a42005-11-14 21:42:46 -08001122 if (hiscore.rule < 8) {
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001123 hiscore.matchlen = ipv6_addr_diff(&ifa_result->addr, daddr);
Yan Zheng12da2a42005-11-14 21:42:46 -08001124 hiscore.rule++;
1125 }
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001126 score.matchlen = ipv6_addr_diff(&ifa->addr, daddr);
1127 if (score.matchlen > hiscore.matchlen) {
1128 score.rule = 8;
1129 goto record_it;
1130 }
1131#if 0
1132 else if (score.matchlen < hiscore.matchlen)
1133 continue;
1134#endif
1135
1136 /* Final Rule: choose first available one */
1137 continue;
1138record_it:
1139 if (ifa_result)
1140 in6_ifa_put(ifa_result);
1141 in6_ifa_hold(ifa);
1142 ifa_result = ifa;
1143 hiscore = score;
1144 }
1145 read_unlock_bh(&idev->lock);
1146 }
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001147 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 read_unlock(&dev_base_lock);
1149
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001150 if (!ifa_result)
1151 return -EADDRNOTAVAIL;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001152
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001153 ipv6_addr_copy(saddr, &ifa_result->addr);
1154 in6_ifa_put(ifa_result);
1155 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156}
1157
1158
1159int ipv6_get_saddr(struct dst_entry *dst,
1160 struct in6_addr *daddr, struct in6_addr *saddr)
1161{
YOSHIFUJI Hideaki7a3025b2006-10-13 16:17:25 +09001162 return ipv6_dev_get_saddr(dst ? ip6_dst_idev(dst)->dev : NULL, daddr, saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163}
1164
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +09001165EXPORT_SYMBOL(ipv6_get_saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
Neil Horman95c385b2007-04-25 17:08:10 -07001167int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr,
1168 unsigned char banned_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169{
1170 struct inet6_dev *idev;
1171 int err = -EADDRNOTAVAIL;
1172
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001173 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 if ((idev = __in6_dev_get(dev)) != NULL) {
1175 struct inet6_ifaddr *ifp;
1176
1177 read_lock_bh(&idev->lock);
1178 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
Neil Horman95c385b2007-04-25 17:08:10 -07001179 if (ifp->scope == IFA_LINK && !(ifp->flags & banned_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 ipv6_addr_copy(addr, &ifp->addr);
1181 err = 0;
1182 break;
1183 }
1184 }
1185 read_unlock_bh(&idev->lock);
1186 }
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001187 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 return err;
1189}
1190
1191static int ipv6_count_addresses(struct inet6_dev *idev)
1192{
1193 int cnt = 0;
1194 struct inet6_ifaddr *ifp;
1195
1196 read_lock_bh(&idev->lock);
1197 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next)
1198 cnt++;
1199 read_unlock_bh(&idev->lock);
1200 return cnt;
1201}
1202
1203int ipv6_chk_addr(struct in6_addr *addr, struct net_device *dev, int strict)
1204{
1205 struct inet6_ifaddr * ifp;
1206 u8 hash = ipv6_addr_hash(addr);
1207
1208 read_lock_bh(&addrconf_hash_lock);
1209 for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
1210 if (ipv6_addr_equal(&ifp->addr, addr) &&
1211 !(ifp->flags&IFA_F_TENTATIVE)) {
1212 if (dev == NULL || ifp->idev->dev == dev ||
1213 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))
1214 break;
1215 }
1216 }
1217 read_unlock_bh(&addrconf_hash_lock);
1218 return ifp != NULL;
1219}
1220
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +09001221EXPORT_SYMBOL(ipv6_chk_addr);
1222
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223static
1224int ipv6_chk_same_addr(const struct in6_addr *addr, struct net_device *dev)
1225{
1226 struct inet6_ifaddr * ifp;
1227 u8 hash = ipv6_addr_hash(addr);
1228
1229 for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
1230 if (ipv6_addr_equal(&ifp->addr, addr)) {
1231 if (dev == NULL || ifp->idev->dev == dev)
1232 break;
1233 }
1234 }
1235 return ifp != NULL;
1236}
1237
1238struct inet6_ifaddr * ipv6_get_ifaddr(struct in6_addr *addr, struct net_device *dev, int strict)
1239{
1240 struct inet6_ifaddr * ifp;
1241 u8 hash = ipv6_addr_hash(addr);
1242
1243 read_lock_bh(&addrconf_hash_lock);
1244 for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
1245 if (ipv6_addr_equal(&ifp->addr, addr)) {
1246 if (dev == NULL || ifp->idev->dev == dev ||
1247 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
1248 in6_ifa_hold(ifp);
1249 break;
1250 }
1251 }
1252 }
1253 read_unlock_bh(&addrconf_hash_lock);
1254
1255 return ifp;
1256}
1257
1258int ipv6_rcv_saddr_equal(const struct sock *sk, const struct sock *sk2)
1259{
1260 const struct in6_addr *sk_rcv_saddr6 = &inet6_sk(sk)->rcv_saddr;
Arnaldo Carvalho de Melo0fa1a532005-12-13 23:23:09 -08001261 const struct in6_addr *sk2_rcv_saddr6 = inet6_rcv_saddr(sk2);
Al Viro82103232006-09-27 18:44:10 -07001262 __be32 sk_rcv_saddr = inet_sk(sk)->rcv_saddr;
1263 __be32 sk2_rcv_saddr = inet_rcv_saddr(sk2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 int sk_ipv6only = ipv6_only_sock(sk);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001265 int sk2_ipv6only = inet_v6_ipv6only(sk2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 int addr_type = ipv6_addr_type(sk_rcv_saddr6);
1267 int addr_type2 = sk2_rcv_saddr6 ? ipv6_addr_type(sk2_rcv_saddr6) : IPV6_ADDR_MAPPED;
1268
1269 if (!sk2_rcv_saddr && !sk_ipv6only)
1270 return 1;
1271
1272 if (addr_type2 == IPV6_ADDR_ANY &&
1273 !(sk2_ipv6only && addr_type == IPV6_ADDR_MAPPED))
1274 return 1;
1275
1276 if (addr_type == IPV6_ADDR_ANY &&
1277 !(sk_ipv6only && addr_type2 == IPV6_ADDR_MAPPED))
1278 return 1;
1279
1280 if (sk2_rcv_saddr6 &&
1281 ipv6_addr_equal(sk_rcv_saddr6, sk2_rcv_saddr6))
1282 return 1;
1283
1284 if (addr_type == IPV6_ADDR_MAPPED &&
1285 !sk2_ipv6only &&
1286 (!sk2_rcv_saddr || !sk_rcv_saddr || sk_rcv_saddr == sk2_rcv_saddr))
1287 return 1;
1288
1289 return 0;
1290}
1291
1292/* Gets referenced address, destroys ifaddr */
1293
Adrian Bunk9f5336e2006-01-07 13:24:25 -08001294static void addrconf_dad_stop(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 if (ifp->flags&IFA_F_PERMANENT) {
1297 spin_lock_bh(&ifp->lock);
1298 addrconf_del_timer(ifp);
1299 ifp->flags |= IFA_F_TENTATIVE;
1300 spin_unlock_bh(&ifp->lock);
1301 in6_ifa_put(ifp);
1302#ifdef CONFIG_IPV6_PRIVACY
1303 } else if (ifp->flags&IFA_F_TEMPORARY) {
1304 struct inet6_ifaddr *ifpub;
1305 spin_lock_bh(&ifp->lock);
1306 ifpub = ifp->ifpub;
1307 if (ifpub) {
1308 in6_ifa_hold(ifpub);
1309 spin_unlock_bh(&ifp->lock);
1310 ipv6_create_tempaddr(ifpub, ifp);
1311 in6_ifa_put(ifpub);
1312 } else {
1313 spin_unlock_bh(&ifp->lock);
1314 }
1315 ipv6_del_addr(ifp);
1316#endif
1317 } else
1318 ipv6_del_addr(ifp);
1319}
1320
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09001321void addrconf_dad_failure(struct inet6_ifaddr *ifp)
1322{
1323 if (net_ratelimit())
1324 printk(KERN_INFO "%s: duplicate address detected!\n", ifp->idev->dev->name);
1325 addrconf_dad_stop(ifp);
1326}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
1328/* Join to solicited addr multicast group. */
1329
1330void addrconf_join_solict(struct net_device *dev, struct in6_addr *addr)
1331{
1332 struct in6_addr maddr;
1333
1334 if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1335 return;
1336
1337 addrconf_addr_solict_mult(addr, &maddr);
1338 ipv6_dev_mc_inc(dev, &maddr);
1339}
1340
1341void addrconf_leave_solict(struct inet6_dev *idev, struct in6_addr *addr)
1342{
1343 struct in6_addr maddr;
1344
1345 if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1346 return;
1347
1348 addrconf_addr_solict_mult(addr, &maddr);
1349 __ipv6_dev_mc_dec(idev, &maddr);
1350}
1351
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03001352static void addrconf_join_anycast(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353{
1354 struct in6_addr addr;
1355 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1356 if (ipv6_addr_any(&addr))
1357 return;
1358 ipv6_dev_ac_inc(ifp->idev->dev, &addr);
1359}
1360
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03001361static void addrconf_leave_anycast(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362{
1363 struct in6_addr addr;
1364 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1365 if (ipv6_addr_any(&addr))
1366 return;
1367 __ipv6_dev_ac_dec(ifp->idev, &addr);
1368}
1369
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08001370static int addrconf_ifid_eui48(u8 *eui, struct net_device *dev)
1371{
1372 if (dev->addr_len != ETH_ALEN)
1373 return -1;
1374 memcpy(eui, dev->dev_addr, 3);
1375 memcpy(eui + 5, dev->dev_addr + 3, 3);
1376
1377 /*
1378 * The zSeries OSA network cards can be shared among various
1379 * OS instances, but the OSA cards have only one MAC address.
1380 * This leads to duplicate address conflicts in conjunction
1381 * with IPv6 if more than one instance uses the same card.
1382 *
1383 * The driver for these cards can deliver a unique 16-bit
1384 * identifier for each instance sharing the same card. It is
1385 * placed instead of 0xFFFE in the interface identifier. The
1386 * "u" bit of the interface identifier is not inverted in this
1387 * case. Hence the resulting interface identifier has local
1388 * scope according to RFC2373.
1389 */
1390 if (dev->dev_id) {
1391 eui[3] = (dev->dev_id >> 8) & 0xFF;
1392 eui[4] = dev->dev_id & 0xFF;
1393 } else {
1394 eui[3] = 0xFF;
1395 eui[4] = 0xFE;
1396 eui[0] ^= 2;
1397 }
1398 return 0;
1399}
1400
1401static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev)
1402{
1403 /* XXX: inherit EUI-64 from other interface -- yoshfuji */
1404 if (dev->addr_len != ARCNET_ALEN)
1405 return -1;
1406 memset(eui, 0, 7);
1407 eui[7] = *(u8*)dev->dev_addr;
1408 return 0;
1409}
1410
1411static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev)
1412{
1413 if (dev->addr_len != INFINIBAND_ALEN)
1414 return -1;
1415 memcpy(eui, dev->dev_addr + 12, 8);
1416 eui[0] |= 2;
1417 return 0;
1418}
1419
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
1421{
1422 switch (dev->type) {
1423 case ARPHRD_ETHER:
1424 case ARPHRD_FDDI:
1425 case ARPHRD_IEEE802_TR:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08001426 return addrconf_ifid_eui48(eui, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 case ARPHRD_ARCNET:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08001428 return addrconf_ifid_arcnet(eui, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 case ARPHRD_INFINIBAND:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08001430 return addrconf_ifid_infiniband(eui, dev);
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11001431 case ARPHRD_SIT:
1432 if (dev->priv_flags & IFF_ISATAP)
1433 return ipv6_isatap_eui64(eui, *(__be32 *)dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 }
1435 return -1;
1436}
1437
1438static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
1439{
1440 int err = -1;
1441 struct inet6_ifaddr *ifp;
1442
1443 read_lock_bh(&idev->lock);
1444 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
1445 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
1446 memcpy(eui, ifp->addr.s6_addr+8, 8);
1447 err = 0;
1448 break;
1449 }
1450 }
1451 read_unlock_bh(&idev->lock);
1452 return err;
1453}
1454
1455#ifdef CONFIG_IPV6_PRIVACY
1456/* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
1457static int __ipv6_regen_rndid(struct inet6_dev *idev)
1458{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459regen:
YOSHIFUJI Hideaki955189e2006-03-20 16:54:09 -08001460 get_random_bytes(idev->rndid, sizeof(idev->rndid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 idev->rndid[0] &= ~0x02;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
1463 /*
1464 * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
1465 * check if generated address is not inappropriate
1466 *
1467 * - Reserved subnet anycast (RFC 2526)
1468 * 11111101 11....11 1xxxxxxx
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11001469 * - ISATAP (RFC4214) 6.1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 * 00-00-5E-FE-xx-xx-xx-xx
1471 * - value 0
1472 * - XXX: already assigned to an address on the device
1473 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001474 if (idev->rndid[0] == 0xfd &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff &&
1476 (idev->rndid[7]&0x80))
1477 goto regen;
1478 if ((idev->rndid[0]|idev->rndid[1]) == 0) {
1479 if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe)
1480 goto regen;
1481 if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00)
1482 goto regen;
1483 }
1484
1485 return 0;
1486}
1487
1488static void ipv6_regen_rndid(unsigned long data)
1489{
1490 struct inet6_dev *idev = (struct inet6_dev *) data;
1491 unsigned long expires;
1492
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001493 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 write_lock_bh(&idev->lock);
1495
1496 if (idev->dead)
1497 goto out;
1498
1499 if (__ipv6_regen_rndid(idev) < 0)
1500 goto out;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001501
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 expires = jiffies +
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001503 idev->cnf.temp_prefered_lft * HZ -
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 idev->cnf.regen_max_retry * idev->cnf.dad_transmits * idev->nd_parms->retrans_time - desync_factor;
1505 if (time_before(expires, jiffies)) {
1506 printk(KERN_WARNING
1507 "ipv6_regen_rndid(): too short regeneration interval; timer disabled for %s.\n",
1508 idev->dev->name);
1509 goto out;
1510 }
1511
1512 if (!mod_timer(&idev->regen_timer, expires))
1513 in6_dev_hold(idev);
1514
1515out:
1516 write_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001517 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 in6_dev_put(idev);
1519}
1520
1521static int __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr) {
1522 int ret = 0;
1523
1524 if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0)
1525 ret = __ipv6_regen_rndid(idev);
1526 return ret;
1527}
1528#endif
1529
1530/*
1531 * Add prefix route.
1532 */
1533
1534static void
1535addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev,
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -07001536 unsigned long expires, u32 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537{
Thomas Graf86872cb2006-08-22 00:01:08 -07001538 struct fib6_config cfg = {
1539 .fc_table = RT6_TABLE_PREFIX,
1540 .fc_metric = IP6_RT_PRIO_ADDRCONF,
1541 .fc_ifindex = dev->ifindex,
1542 .fc_expires = expires,
1543 .fc_dst_len = plen,
1544 .fc_flags = RTF_UP | flags,
1545 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546
Thomas Graf86872cb2006-08-22 00:01:08 -07001547 ipv6_addr_copy(&cfg.fc_dst, pfx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548
1549 /* Prevent useless cloning on PtP SIT.
1550 This thing is done here expecting that the whole
1551 class of non-broadcast devices need not cloning.
1552 */
Joerg Roedel0be669b2006-10-10 14:49:53 -07001553#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
Thomas Graf86872cb2006-08-22 00:01:08 -07001554 if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT))
1555 cfg.fc_flags |= RTF_NONEXTHOP;
Joerg Roedel0be669b2006-10-10 14:49:53 -07001556#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
Thomas Graf86872cb2006-08-22 00:01:08 -07001558 ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559}
1560
1561/* Create "default" multicast route to the interface */
1562
1563static void addrconf_add_mroute(struct net_device *dev)
1564{
Thomas Graf86872cb2006-08-22 00:01:08 -07001565 struct fib6_config cfg = {
1566 .fc_table = RT6_TABLE_LOCAL,
1567 .fc_metric = IP6_RT_PRIO_ADDRCONF,
1568 .fc_ifindex = dev->ifindex,
1569 .fc_dst_len = 8,
1570 .fc_flags = RTF_UP,
1571 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
Thomas Graf86872cb2006-08-22 00:01:08 -07001573 ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0);
1574
1575 ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576}
1577
Joerg Roedel0be669b2006-10-10 14:49:53 -07001578#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579static void sit_route_add(struct net_device *dev)
1580{
Thomas Graf86872cb2006-08-22 00:01:08 -07001581 struct fib6_config cfg = {
1582 .fc_table = RT6_TABLE_MAIN,
1583 .fc_metric = IP6_RT_PRIO_ADDRCONF,
1584 .fc_ifindex = dev->ifindex,
1585 .fc_dst_len = 96,
1586 .fc_flags = RTF_UP | RTF_NONEXTHOP,
1587 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588
1589 /* prefix length - 96 bits "::d.d.d.d" */
Thomas Graf86872cb2006-08-22 00:01:08 -07001590 ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591}
Joerg Roedel0be669b2006-10-10 14:49:53 -07001592#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593
1594static void addrconf_add_lroute(struct net_device *dev)
1595{
1596 struct in6_addr addr;
1597
1598 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
1599 addrconf_prefix_route(&addr, 64, dev, 0, 0);
1600}
1601
1602static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
1603{
1604 struct inet6_dev *idev;
1605
1606 ASSERT_RTNL();
1607
1608 if ((idev = ipv6_find_idev(dev)) == NULL)
1609 return NULL;
1610
1611 /* Add default multicast route */
1612 addrconf_add_mroute(dev);
1613
1614 /* Add link local route */
1615 addrconf_add_lroute(dev);
1616 return idev;
1617}
1618
1619void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len)
1620{
1621 struct prefix_info *pinfo;
1622 __u32 valid_lft;
1623 __u32 prefered_lft;
1624 int addr_type;
1625 unsigned long rt_expires;
1626 struct inet6_dev *in6_dev;
1627
1628 pinfo = (struct prefix_info *) opt;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001629
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 if (len < sizeof(struct prefix_info)) {
1631 ADBG(("addrconf: prefix option too short\n"));
1632 return;
1633 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001634
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 /*
1636 * Validation checks ([ADDRCONF], page 19)
1637 */
1638
1639 addr_type = ipv6_addr_type(&pinfo->prefix);
1640
1641 if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
1642 return;
1643
1644 valid_lft = ntohl(pinfo->valid);
1645 prefered_lft = ntohl(pinfo->prefered);
1646
1647 if (prefered_lft > valid_lft) {
1648 if (net_ratelimit())
1649 printk(KERN_WARNING "addrconf: prefix option has invalid lifetime\n");
1650 return;
1651 }
1652
1653 in6_dev = in6_dev_get(dev);
1654
1655 if (in6_dev == NULL) {
1656 if (net_ratelimit())
1657 printk(KERN_DEBUG "addrconf: device %s not configured\n", dev->name);
1658 return;
1659 }
1660
1661 /*
1662 * Two things going on here:
1663 * 1) Add routes for on-link prefixes
1664 * 2) Configure prefixes with the auto flag set
1665 */
1666
1667 /* Avoid arithmetic overflow. Really, we could
1668 save rt_expires in seconds, likely valid_lft,
1669 but it would require division in fib gc, that it
1670 not good.
1671 */
1672 if (valid_lft >= 0x7FFFFFFF/HZ)
YOSHIFUJI Hideaki3dd4bc62005-12-19 14:02:45 -08001673 rt_expires = 0x7FFFFFFF - (0x7FFFFFFF % HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 else
YOSHIFUJI Hideaki3dd4bc62005-12-19 14:02:45 -08001675 rt_expires = valid_lft * HZ;
1676
1677 /*
1678 * We convert this (in jiffies) to clock_t later.
1679 * Avoid arithmetic overflow there as well.
1680 * Overflow can happen only if HZ < USER_HZ.
1681 */
1682 if (HZ < USER_HZ && rt_expires > 0x7FFFFFFF / USER_HZ)
1683 rt_expires = 0x7FFFFFFF / USER_HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684
1685 if (pinfo->onlink) {
1686 struct rt6_info *rt;
1687 rt = rt6_lookup(&pinfo->prefix, NULL, dev->ifindex, 1);
1688
1689 if (rt && ((rt->rt6i_flags & (RTF_GATEWAY | RTF_DEFAULT)) == 0)) {
1690 if (rt->rt6i_flags&RTF_EXPIRES) {
1691 if (valid_lft == 0) {
Thomas Grafe0a1ad732006-08-22 00:00:21 -07001692 ip6_del_rt(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 rt = NULL;
1694 } else {
YOSHIFUJI Hideaki3dd4bc62005-12-19 14:02:45 -08001695 rt->rt6i_expires = jiffies + rt_expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 }
1697 }
1698 } else if (valid_lft) {
1699 addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
YOSHIFUJI Hideaki3dd4bc62005-12-19 14:02:45 -08001700 dev, jiffies_to_clock_t(rt_expires), RTF_ADDRCONF|RTF_EXPIRES|RTF_PREFIX_RT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 }
1702 if (rt)
1703 dst_release(&rt->u.dst);
1704 }
1705
1706 /* Try to figure out our local address for this prefix */
1707
1708 if (pinfo->autoconf && in6_dev->cnf.autoconf) {
1709 struct inet6_ifaddr * ifp;
1710 struct in6_addr addr;
1711 int create = 0, update_lft = 0;
1712
1713 if (pinfo->prefix_len == 64) {
1714 memcpy(&addr, &pinfo->prefix, 8);
1715 if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
1716 ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
1717 in6_dev_put(in6_dev);
1718 return;
1719 }
1720 goto ok;
1721 }
1722 if (net_ratelimit())
1723 printk(KERN_DEBUG "IPv6 addrconf: prefix with wrong length %d\n",
1724 pinfo->prefix_len);
1725 in6_dev_put(in6_dev);
1726 return;
1727
1728ok:
1729
1730 ifp = ipv6_get_ifaddr(&addr, dev, 1);
1731
1732 if (ifp == NULL && valid_lft) {
1733 int max_addresses = in6_dev->cnf.max_addresses;
Neil Horman95c385b2007-04-25 17:08:10 -07001734 u32 addr_flags = 0;
1735
1736#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1737 if (in6_dev->cnf.optimistic_dad &&
1738 !ipv6_devconf.forwarding)
1739 addr_flags = IFA_F_OPTIMISTIC;
1740#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741
1742 /* Do not allow to create too much of autoconfigured
1743 * addresses; this would be too easy way to crash kernel.
1744 */
1745 if (!max_addresses ||
1746 ipv6_count_addresses(in6_dev) < max_addresses)
1747 ifp = ipv6_add_addr(in6_dev, &addr, pinfo->prefix_len,
Neil Horman95c385b2007-04-25 17:08:10 -07001748 addr_type&IPV6_ADDR_SCOPE_MASK,
1749 addr_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
1751 if (!ifp || IS_ERR(ifp)) {
1752 in6_dev_put(in6_dev);
1753 return;
1754 }
1755
1756 update_lft = create = 1;
1757 ifp->cstamp = jiffies;
1758 addrconf_dad_start(ifp, RTF_ADDRCONF|RTF_PREFIX_RT);
1759 }
1760
1761 if (ifp) {
1762 int flags;
1763 unsigned long now;
1764#ifdef CONFIG_IPV6_PRIVACY
1765 struct inet6_ifaddr *ift;
1766#endif
1767 u32 stored_lft;
1768
1769 /* update lifetime (RFC2462 5.5.3 e) */
1770 spin_lock(&ifp->lock);
1771 now = jiffies;
1772 if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
1773 stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
1774 else
1775 stored_lft = 0;
1776 if (!update_lft && stored_lft) {
1777 if (valid_lft > MIN_VALID_LIFETIME ||
1778 valid_lft > stored_lft)
1779 update_lft = 1;
1780 else if (stored_lft <= MIN_VALID_LIFETIME) {
1781 /* valid_lft <= stored_lft is always true */
1782 /* XXX: IPsec */
1783 update_lft = 0;
1784 } else {
1785 valid_lft = MIN_VALID_LIFETIME;
1786 if (valid_lft < prefered_lft)
1787 prefered_lft = valid_lft;
1788 update_lft = 1;
1789 }
1790 }
1791
1792 if (update_lft) {
1793 ifp->valid_lft = valid_lft;
1794 ifp->prefered_lft = prefered_lft;
1795 ifp->tstamp = now;
1796 flags = ifp->flags;
1797 ifp->flags &= ~IFA_F_DEPRECATED;
1798 spin_unlock(&ifp->lock);
1799
1800 if (!(flags&IFA_F_TENTATIVE))
1801 ipv6_ifa_notify(0, ifp);
1802 } else
1803 spin_unlock(&ifp->lock);
1804
1805#ifdef CONFIG_IPV6_PRIVACY
1806 read_lock_bh(&in6_dev->lock);
1807 /* update all temporary addresses in the list */
1808 for (ift=in6_dev->tempaddr_list; ift; ift=ift->tmp_next) {
1809 /*
1810 * When adjusting the lifetimes of an existing
1811 * temporary address, only lower the lifetimes.
1812 * Implementations must not increase the
1813 * lifetimes of an existing temporary address
1814 * when processing a Prefix Information Option.
1815 */
1816 spin_lock(&ift->lock);
1817 flags = ift->flags;
1818 if (ift->valid_lft > valid_lft &&
1819 ift->valid_lft - valid_lft > (jiffies - ift->tstamp) / HZ)
1820 ift->valid_lft = valid_lft + (jiffies - ift->tstamp) / HZ;
1821 if (ift->prefered_lft > prefered_lft &&
1822 ift->prefered_lft - prefered_lft > (jiffies - ift->tstamp) / HZ)
1823 ift->prefered_lft = prefered_lft + (jiffies - ift->tstamp) / HZ;
1824 spin_unlock(&ift->lock);
1825 if (!(flags&IFA_F_TENTATIVE))
1826 ipv6_ifa_notify(0, ift);
1827 }
1828
1829 if (create && in6_dev->cnf.use_tempaddr > 0) {
1830 /*
1831 * When a new public address is created as described in [ADDRCONF],
1832 * also create a new temporary address.
1833 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001834 read_unlock_bh(&in6_dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 ipv6_create_tempaddr(ifp, NULL);
1836 } else {
1837 read_unlock_bh(&in6_dev->lock);
1838 }
1839#endif
1840 in6_ifa_put(ifp);
1841 addrconf_verify(0);
1842 }
1843 }
1844 inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
1845 in6_dev_put(in6_dev);
1846}
1847
1848/*
1849 * Set destination address.
1850 * Special case for SIT interfaces where we create a new "virtual"
1851 * device.
1852 */
1853int addrconf_set_dstaddr(void __user *arg)
1854{
1855 struct in6_ifreq ireq;
1856 struct net_device *dev;
1857 int err = -EINVAL;
1858
1859 rtnl_lock();
1860
1861 err = -EFAULT;
1862 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
1863 goto err_exit;
1864
Eric W. Biederman881d9662007-09-17 11:56:21 -07001865 dev = __dev_get_by_index(&init_net, ireq.ifr6_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866
1867 err = -ENODEV;
1868 if (dev == NULL)
1869 goto err_exit;
1870
Joerg Roedel0be669b2006-10-10 14:49:53 -07001871#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 if (dev->type == ARPHRD_SIT) {
1873 struct ifreq ifr;
1874 mm_segment_t oldfs;
1875 struct ip_tunnel_parm p;
1876
1877 err = -EADDRNOTAVAIL;
1878 if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4))
1879 goto err_exit;
1880
1881 memset(&p, 0, sizeof(p));
1882 p.iph.daddr = ireq.ifr6_addr.s6_addr32[3];
1883 p.iph.saddr = 0;
1884 p.iph.version = 4;
1885 p.iph.ihl = 5;
1886 p.iph.protocol = IPPROTO_IPV6;
1887 p.iph.ttl = 64;
1888 ifr.ifr_ifru.ifru_data = (void __user *)&p;
1889
1890 oldfs = get_fs(); set_fs(KERNEL_DS);
1891 err = dev->do_ioctl(dev, &ifr, SIOCADDTUNNEL);
1892 set_fs(oldfs);
1893
1894 if (err == 0) {
1895 err = -ENOBUFS;
Eric W. Biederman881d9662007-09-17 11:56:21 -07001896 if ((dev = __dev_get_by_name(&init_net, p.name)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 goto err_exit;
1898 err = dev_open(dev);
1899 }
1900 }
Joerg Roedel0be669b2006-10-10 14:49:53 -07001901#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902
1903err_exit:
1904 rtnl_unlock();
1905 return err;
1906}
1907
1908/*
1909 * Manual configuration of address on an interface
1910 */
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09001911static int inet6_addr_add(int ifindex, struct in6_addr *pfx, int plen,
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07001912 __u8 ifa_flags, __u32 prefered_lft, __u32 valid_lft)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913{
1914 struct inet6_ifaddr *ifp;
1915 struct inet6_dev *idev;
1916 struct net_device *dev;
1917 int scope;
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09001918 u32 flags = RTF_EXPIRES;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919
1920 ASSERT_RTNL();
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001921
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09001922 /* check the lifetime */
1923 if (!valid_lft || prefered_lft > valid_lft)
1924 return -EINVAL;
1925
Eric W. Biederman881d9662007-09-17 11:56:21 -07001926 if ((dev = __dev_get_by_index(&init_net, ifindex)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 return -ENODEV;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001928
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 if ((idev = addrconf_add_dev(dev)) == NULL)
1930 return -ENOBUFS;
1931
1932 scope = ipv6_addr_scope(pfx);
1933
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09001934 if (valid_lft == INFINITY_LIFE_TIME) {
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09001935 ifa_flags |= IFA_F_PERMANENT;
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09001936 flags = 0;
1937 } else if (valid_lft >= 0x7FFFFFFF/HZ)
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09001938 valid_lft = 0x7FFFFFFF/HZ;
1939
1940 if (prefered_lft == 0)
1941 ifa_flags |= IFA_F_DEPRECATED;
1942 else if ((prefered_lft >= 0x7FFFFFFF/HZ) &&
1943 (prefered_lft != INFINITY_LIFE_TIME))
1944 prefered_lft = 0x7FFFFFFF/HZ;
1945
1946 ifp = ipv6_add_addr(idev, pfx, plen, scope, ifa_flags);
1947
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 if (!IS_ERR(ifp)) {
Herbert Xu06aebfb2006-08-09 16:52:04 -07001949 spin_lock_bh(&ifp->lock);
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09001950 ifp->valid_lft = valid_lft;
1951 ifp->prefered_lft = prefered_lft;
1952 ifp->tstamp = jiffies;
Herbert Xu06aebfb2006-08-09 16:52:04 -07001953 spin_unlock_bh(&ifp->lock);
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09001954
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09001955 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev,
1956 jiffies_to_clock_t(valid_lft * HZ), flags);
Neil Horman95c385b2007-04-25 17:08:10 -07001957 /*
1958 * Note that section 3.1 of RFC 4429 indicates
1959 * that the Optimistic flag should not be set for
1960 * manually configured addresses
1961 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 addrconf_dad_start(ifp, 0);
1963 in6_ifa_put(ifp);
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09001964 addrconf_verify(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 return 0;
1966 }
1967
1968 return PTR_ERR(ifp);
1969}
1970
1971static int inet6_addr_del(int ifindex, struct in6_addr *pfx, int plen)
1972{
1973 struct inet6_ifaddr *ifp;
1974 struct inet6_dev *idev;
1975 struct net_device *dev;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001976
Eric W. Biederman881d9662007-09-17 11:56:21 -07001977 if ((dev = __dev_get_by_index(&init_net, ifindex)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 return -ENODEV;
1979
1980 if ((idev = __in6_dev_get(dev)) == NULL)
1981 return -ENXIO;
1982
1983 read_lock_bh(&idev->lock);
1984 for (ifp = idev->addr_list; ifp; ifp=ifp->if_next) {
1985 if (ifp->prefix_len == plen &&
1986 ipv6_addr_equal(pfx, &ifp->addr)) {
1987 in6_ifa_hold(ifp);
1988 read_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001989
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 ipv6_del_addr(ifp);
1991
1992 /* If the last address is deleted administratively,
1993 disable IPv6 on this interface.
1994 */
1995 if (idev->addr_list == NULL)
1996 addrconf_ifdown(idev->dev, 1);
1997 return 0;
1998 }
1999 }
2000 read_unlock_bh(&idev->lock);
2001 return -EADDRNOTAVAIL;
2002}
2003
2004
2005int addrconf_add_ifaddr(void __user *arg)
2006{
2007 struct in6_ifreq ireq;
2008 int err;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002009
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 if (!capable(CAP_NET_ADMIN))
2011 return -EPERM;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002012
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2014 return -EFAULT;
2015
2016 rtnl_lock();
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002017 err = inet6_addr_add(ireq.ifr6_ifindex, &ireq.ifr6_addr, ireq.ifr6_prefixlen,
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07002018 IFA_F_PERMANENT, INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 rtnl_unlock();
2020 return err;
2021}
2022
2023int addrconf_del_ifaddr(void __user *arg)
2024{
2025 struct in6_ifreq ireq;
2026 int err;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002027
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 if (!capable(CAP_NET_ADMIN))
2029 return -EPERM;
2030
2031 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2032 return -EFAULT;
2033
2034 rtnl_lock();
2035 err = inet6_addr_del(ireq.ifr6_ifindex, &ireq.ifr6_addr, ireq.ifr6_prefixlen);
2036 rtnl_unlock();
2037 return err;
2038}
2039
Joerg Roedel0be669b2006-10-10 14:49:53 -07002040#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041static void sit_add_v4_addrs(struct inet6_dev *idev)
2042{
2043 struct inet6_ifaddr * ifp;
2044 struct in6_addr addr;
2045 struct net_device *dev;
2046 int scope;
2047
2048 ASSERT_RTNL();
2049
2050 memset(&addr, 0, sizeof(struct in6_addr));
2051 memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
2052
2053 if (idev->dev->flags&IFF_POINTOPOINT) {
2054 addr.s6_addr32[0] = htonl(0xfe800000);
2055 scope = IFA_LINK;
2056 } else {
2057 scope = IPV6_ADDR_COMPATv4;
2058 }
2059
2060 if (addr.s6_addr32[3]) {
2061 ifp = ipv6_add_addr(idev, &addr, 128, scope, IFA_F_PERMANENT);
2062 if (!IS_ERR(ifp)) {
2063 spin_lock_bh(&ifp->lock);
2064 ifp->flags &= ~IFA_F_TENTATIVE;
2065 spin_unlock_bh(&ifp->lock);
2066 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2067 in6_ifa_put(ifp);
2068 }
2069 return;
2070 }
2071
Eric W. Biederman881d9662007-09-17 11:56:21 -07002072 for_each_netdev(&init_net, dev) {
Herbert Xue5ed6392005-10-03 14:35:55 -07002073 struct in_device * in_dev = __in_dev_get_rtnl(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 if (in_dev && (dev->flags & IFF_UP)) {
2075 struct in_ifaddr * ifa;
2076
2077 int flag = scope;
2078
2079 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
2080 int plen;
2081
2082 addr.s6_addr32[3] = ifa->ifa_local;
2083
2084 if (ifa->ifa_scope == RT_SCOPE_LINK)
2085 continue;
2086 if (ifa->ifa_scope >= RT_SCOPE_HOST) {
2087 if (idev->dev->flags&IFF_POINTOPOINT)
2088 continue;
2089 flag |= IFA_HOST;
2090 }
2091 if (idev->dev->flags&IFF_POINTOPOINT)
2092 plen = 64;
2093 else
2094 plen = 96;
2095
2096 ifp = ipv6_add_addr(idev, &addr, plen, flag,
2097 IFA_F_PERMANENT);
2098 if (!IS_ERR(ifp)) {
2099 spin_lock_bh(&ifp->lock);
2100 ifp->flags &= ~IFA_F_TENTATIVE;
2101 spin_unlock_bh(&ifp->lock);
2102 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2103 in6_ifa_put(ifp);
2104 }
2105 }
2106 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002107 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108}
Joerg Roedel0be669b2006-10-10 14:49:53 -07002109#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110
2111static void init_loopback(struct net_device *dev)
2112{
2113 struct inet6_dev *idev;
2114 struct inet6_ifaddr * ifp;
2115
2116 /* ::1 */
2117
2118 ASSERT_RTNL();
2119
2120 if ((idev = ipv6_find_idev(dev)) == NULL) {
2121 printk(KERN_DEBUG "init loopback: add_dev failed\n");
2122 return;
2123 }
2124
2125 ifp = ipv6_add_addr(idev, &in6addr_loopback, 128, IFA_HOST, IFA_F_PERMANENT);
2126 if (!IS_ERR(ifp)) {
2127 spin_lock_bh(&ifp->lock);
2128 ifp->flags &= ~IFA_F_TENTATIVE;
2129 spin_unlock_bh(&ifp->lock);
2130 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2131 in6_ifa_put(ifp);
2132 }
2133}
2134
2135static void addrconf_add_linklocal(struct inet6_dev *idev, struct in6_addr *addr)
2136{
2137 struct inet6_ifaddr * ifp;
Neil Horman95c385b2007-04-25 17:08:10 -07002138 u32 addr_flags = IFA_F_PERMANENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139
Neil Horman95c385b2007-04-25 17:08:10 -07002140#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
2141 if (idev->cnf.optimistic_dad &&
2142 !ipv6_devconf.forwarding)
2143 addr_flags |= IFA_F_OPTIMISTIC;
2144#endif
2145
2146
2147 ifp = ipv6_add_addr(idev, addr, 64, IFA_LINK, addr_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 if (!IS_ERR(ifp)) {
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09002149 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, idev->dev, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 addrconf_dad_start(ifp, 0);
2151 in6_ifa_put(ifp);
2152 }
2153}
2154
2155static void addrconf_dev_config(struct net_device *dev)
2156{
2157 struct in6_addr addr;
2158 struct inet6_dev * idev;
2159
2160 ASSERT_RTNL();
2161
Herbert Xu74235a22007-06-14 13:02:55 -07002162 if ((dev->type != ARPHRD_ETHER) &&
2163 (dev->type != ARPHRD_FDDI) &&
2164 (dev->type != ARPHRD_IEEE802_TR) &&
2165 (dev->type != ARPHRD_ARCNET) &&
2166 (dev->type != ARPHRD_INFINIBAND)) {
2167 /* Alas, we support only Ethernet autoconfiguration. */
2168 return;
2169 }
2170
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 idev = addrconf_add_dev(dev);
2172 if (idev == NULL)
2173 return;
2174
2175 memset(&addr, 0, sizeof(struct in6_addr));
2176 addr.s6_addr32[0] = htonl(0xFE800000);
2177
2178 if (ipv6_generate_eui64(addr.s6_addr + 8, dev) == 0)
2179 addrconf_add_linklocal(idev, &addr);
2180}
2181
Joerg Roedel0be669b2006-10-10 14:49:53 -07002182#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183static void addrconf_sit_config(struct net_device *dev)
2184{
2185 struct inet6_dev *idev;
2186
2187 ASSERT_RTNL();
2188
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002189 /*
2190 * Configure the tunnel with one of our IPv4
2191 * addresses... we should configure all of
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 * our v4 addrs in the tunnel
2193 */
2194
2195 if ((idev = ipv6_find_idev(dev)) == NULL) {
2196 printk(KERN_DEBUG "init sit: add_dev failed\n");
2197 return;
2198 }
2199
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11002200 if (dev->priv_flags & IFF_ISATAP) {
2201 struct in6_addr addr;
2202
2203 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
2204 addrconf_prefix_route(&addr, 64, dev, 0, 0);
2205 if (!ipv6_generate_eui64(addr.s6_addr + 8, dev))
2206 addrconf_add_linklocal(idev, &addr);
2207 return;
2208 }
2209
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 sit_add_v4_addrs(idev);
2211
2212 if (dev->flags&IFF_POINTOPOINT) {
2213 addrconf_add_mroute(dev);
2214 addrconf_add_lroute(dev);
2215 } else
2216 sit_route_add(dev);
2217}
Joerg Roedel0be669b2006-10-10 14:49:53 -07002218#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219
2220static inline int
2221ipv6_inherit_linklocal(struct inet6_dev *idev, struct net_device *link_dev)
2222{
2223 struct in6_addr lladdr;
2224
Neil Horman95c385b2007-04-25 17:08:10 -07002225 if (!ipv6_get_lladdr(link_dev, &lladdr, IFA_F_TENTATIVE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 addrconf_add_linklocal(idev, &lladdr);
2227 return 0;
2228 }
2229 return -1;
2230}
2231
2232static void ip6_tnl_add_linklocal(struct inet6_dev *idev)
2233{
2234 struct net_device *link_dev;
2235
2236 /* first try to inherit the link-local address from the link device */
2237 if (idev->dev->iflink &&
Eric W. Biederman881d9662007-09-17 11:56:21 -07002238 (link_dev = __dev_get_by_index(&init_net, idev->dev->iflink))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 if (!ipv6_inherit_linklocal(idev, link_dev))
2240 return;
2241 }
2242 /* then try to inherit it from any device */
Eric W. Biederman881d9662007-09-17 11:56:21 -07002243 for_each_netdev(&init_net, link_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 if (!ipv6_inherit_linklocal(idev, link_dev))
2245 return;
2246 }
2247 printk(KERN_DEBUG "init ip6-ip6: add_linklocal failed\n");
2248}
2249
2250/*
2251 * Autoconfigure tunnel with a link-local address so routing protocols,
2252 * DHCPv6, MLD etc. can be run over the virtual link
2253 */
2254
2255static void addrconf_ip6_tnl_config(struct net_device *dev)
2256{
2257 struct inet6_dev *idev;
2258
2259 ASSERT_RTNL();
2260
2261 if ((idev = addrconf_add_dev(dev)) == NULL) {
2262 printk(KERN_DEBUG "init ip6-ip6: add_dev failed\n");
2263 return;
2264 }
2265 ip6_tnl_add_linklocal(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266}
2267
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002268static int addrconf_notify(struct notifier_block *this, unsigned long event,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 void * data)
2270{
2271 struct net_device *dev = (struct net_device *) data;
Herbert Xu74235a22007-06-14 13:02:55 -07002272 struct inet6_dev *idev = __in6_dev_get(dev);
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09002273 int run_pending = 0;
Herbert Xub217d612007-07-30 17:04:52 -07002274 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02002276 if (dev->nd_net != &init_net)
2277 return NOTIFY_DONE;
2278
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 switch(event) {
YOSHIFUJI Hideaki45ba9dd2007-02-15 02:07:27 +09002280 case NETDEV_REGISTER:
Herbert Xu74235a22007-06-14 13:02:55 -07002281 if (!idev && dev->mtu >= IPV6_MIN_MTU) {
YOSHIFUJI Hideaki45ba9dd2007-02-15 02:07:27 +09002282 idev = ipv6_add_dev(dev);
2283 if (!idev)
Herbert Xub217d612007-07-30 17:04:52 -07002284 return notifier_from_errno(-ENOMEM);
YOSHIFUJI Hideaki45ba9dd2007-02-15 02:07:27 +09002285 }
2286 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 case NETDEV_UP:
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002288 case NETDEV_CHANGE:
Jay Vosburghc2edacf2007-07-09 10:42:47 -07002289 if (dev->flags & IFF_SLAVE)
2290 break;
2291
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002292 if (event == NETDEV_UP) {
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -07002293 if (!addrconf_qdisc_ok(dev)) {
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002294 /* device is not ready yet. */
2295 printk(KERN_INFO
2296 "ADDRCONF(NETDEV_UP): %s: "
2297 "link is not ready\n",
2298 dev->name);
2299 break;
2300 }
Kristian Slavov99081042006-02-08 16:10:53 -08002301
Evgeniy Polyakovd31c7b82007-11-30 23:36:08 +11002302 if (!idev && dev->mtu >= IPV6_MIN_MTU)
2303 idev = ipv6_add_dev(dev);
2304
Kristian Slavov99081042006-02-08 16:10:53 -08002305 if (idev)
2306 idev->if_flags |= IF_READY;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002307 } else {
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -07002308 if (!addrconf_qdisc_ok(dev)) {
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002309 /* device is still not ready. */
2310 break;
2311 }
2312
2313 if (idev) {
2314 if (idev->if_flags & IF_READY) {
2315 /* device is already configured. */
2316 break;
2317 }
2318 idev->if_flags |= IF_READY;
2319 }
2320
2321 printk(KERN_INFO
2322 "ADDRCONF(NETDEV_CHANGE): %s: "
2323 "link becomes ready\n",
2324 dev->name);
2325
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09002326 run_pending = 1;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002327 }
2328
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 switch(dev->type) {
Joerg Roedel0be669b2006-10-10 14:49:53 -07002330#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 case ARPHRD_SIT:
2332 addrconf_sit_config(dev);
2333 break;
Joerg Roedel0be669b2006-10-10 14:49:53 -07002334#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 case ARPHRD_TUNNEL6:
2336 addrconf_ip6_tnl_config(dev);
2337 break;
2338 case ARPHRD_LOOPBACK:
2339 init_loopback(dev);
2340 break;
2341
2342 default:
2343 addrconf_dev_config(dev);
2344 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002345 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 if (idev) {
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09002347 if (run_pending)
2348 addrconf_dad_run(idev);
2349
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 /* If the MTU changed during the interface down, when the
2351 interface up, the changed MTU must be reflected in the
2352 idev as well as routers.
2353 */
2354 if (idev->cnf.mtu6 != dev->mtu && dev->mtu >= IPV6_MIN_MTU) {
2355 rt6_mtu_change(dev, dev->mtu);
2356 idev->cnf.mtu6 = dev->mtu;
2357 }
2358 idev->tstamp = jiffies;
2359 inet6_ifinfo_notify(RTM_NEWLINK, idev);
2360 /* If the changed mtu during down is lower than IPV6_MIN_MTU
2361 stop IPv6 on this interface.
2362 */
2363 if (dev->mtu < IPV6_MIN_MTU)
2364 addrconf_ifdown(dev, event != NETDEV_DOWN);
2365 }
2366 break;
2367
2368 case NETDEV_CHANGEMTU:
Evgeniy Polyakovd31c7b82007-11-30 23:36:08 +11002369 if (idev && dev->mtu >= IPV6_MIN_MTU) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 rt6_mtu_change(dev, dev->mtu);
2371 idev->cnf.mtu6 = dev->mtu;
2372 break;
2373 }
2374
Evgeniy Polyakovd31c7b82007-11-30 23:36:08 +11002375 if (!idev && dev->mtu >= IPV6_MIN_MTU) {
2376 idev = ipv6_add_dev(dev);
2377 if (idev)
2378 break;
2379 }
2380
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 /* MTU falled under IPV6_MIN_MTU. Stop IPv6 on this interface. */
2382
2383 case NETDEV_DOWN:
2384 case NETDEV_UNREGISTER:
2385 /*
2386 * Remove all addresses from this interface.
2387 */
2388 addrconf_ifdown(dev, event != NETDEV_DOWN);
2389 break;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002390
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 case NETDEV_CHANGENAME:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 if (idev) {
Stephen Hemminger5632c512007-04-28 21:16:39 -07002393 snmp6_unregister_dev(idev);
2394#ifdef CONFIG_SYSCTL
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 addrconf_sysctl_unregister(&idev->cnf);
2396 neigh_sysctl_unregister(idev->nd_parms);
2397 neigh_sysctl_register(dev, idev->nd_parms,
2398 NET_IPV6, NET_IPV6_NEIGH, "ipv6",
2399 &ndisc_ifinfo_sysctl_change,
2400 NULL);
Pavel Emelyanovf52295a2007-12-02 00:58:37 +11002401 addrconf_sysctl_register(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402#endif
Herbert Xub217d612007-07-30 17:04:52 -07002403 err = snmp6_register_dev(idev);
2404 if (err)
2405 return notifier_from_errno(err);
Stephen Hemminger5632c512007-04-28 21:16:39 -07002406 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002408 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409
2410 return NOTIFY_OK;
2411}
2412
2413/*
2414 * addrconf module should be notified of a device going up
2415 */
2416static struct notifier_block ipv6_dev_notf = {
2417 .notifier_call = addrconf_notify,
2418 .priority = 0
2419};
2420
2421static int addrconf_ifdown(struct net_device *dev, int how)
2422{
2423 struct inet6_dev *idev;
2424 struct inet6_ifaddr *ifa, **bifa;
2425 int i;
2426
2427 ASSERT_RTNL();
2428
Eric W. Biederman2774c7a2007-09-26 22:10:56 -07002429 if (dev == init_net.loopback_dev && how == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 how = 0;
2431
2432 rt6_ifdown(dev);
2433 neigh_ifdown(&nd_tbl, dev);
2434
2435 idev = __in6_dev_get(dev);
2436 if (idev == NULL)
2437 return -ENODEV;
2438
2439 /* Step 1: remove reference to ipv6 device from parent device.
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002440 Do not dev_put!
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 */
2442 if (how == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 idev->dead = 1;
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07002444
2445 /* protected by rtnl_lock */
2446 rcu_assign_pointer(dev->ip6_ptr, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447
2448 /* Step 1.5: remove snmp6 entry */
2449 snmp6_unregister_dev(idev);
2450
2451 }
2452
2453 /* Step 2: clear hash table */
2454 for (i=0; i<IN6_ADDR_HSIZE; i++) {
2455 bifa = &inet6_addr_lst[i];
2456
2457 write_lock_bh(&addrconf_hash_lock);
2458 while ((ifa = *bifa) != NULL) {
2459 if (ifa->idev == idev) {
2460 *bifa = ifa->lst_next;
2461 ifa->lst_next = NULL;
2462 addrconf_del_timer(ifa);
2463 in6_ifa_put(ifa);
2464 continue;
2465 }
2466 bifa = &ifa->lst_next;
2467 }
2468 write_unlock_bh(&addrconf_hash_lock);
2469 }
2470
2471 write_lock_bh(&idev->lock);
2472
2473 /* Step 3: clear flags for stateless addrconf */
2474 if (how != 1)
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002475 idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476
2477 /* Step 4: clear address list */
2478#ifdef CONFIG_IPV6_PRIVACY
2479 if (how == 1 && del_timer(&idev->regen_timer))
2480 in6_dev_put(idev);
2481
2482 /* clear tempaddr list */
2483 while ((ifa = idev->tempaddr_list) != NULL) {
2484 idev->tempaddr_list = ifa->tmp_next;
2485 ifa->tmp_next = NULL;
2486 ifa->dead = 1;
2487 write_unlock_bh(&idev->lock);
2488 spin_lock_bh(&ifa->lock);
2489
2490 if (ifa->ifpub) {
2491 in6_ifa_put(ifa->ifpub);
2492 ifa->ifpub = NULL;
2493 }
2494 spin_unlock_bh(&ifa->lock);
2495 in6_ifa_put(ifa);
2496 write_lock_bh(&idev->lock);
2497 }
2498#endif
2499 while ((ifa = idev->addr_list) != NULL) {
2500 idev->addr_list = ifa->if_next;
2501 ifa->if_next = NULL;
2502 ifa->dead = 1;
2503 addrconf_del_timer(ifa);
2504 write_unlock_bh(&idev->lock);
2505
2506 __ipv6_ifa_notify(RTM_DELADDR, ifa);
Vlad Yasevich063ed362007-07-15 00:16:35 -07002507 atomic_notifier_call_chain(&inet6addr_chain, NETDEV_DOWN, ifa);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 in6_ifa_put(ifa);
2509
2510 write_lock_bh(&idev->lock);
2511 }
2512 write_unlock_bh(&idev->lock);
2513
2514 /* Step 5: Discard multicast list */
2515
2516 if (how == 1)
2517 ipv6_mc_destroy_dev(idev);
2518 else
2519 ipv6_mc_down(idev);
2520
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 idev->tstamp = jiffies;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002522
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 /* Shot the device (if unregistered) */
2524
2525 if (how == 1) {
2526#ifdef CONFIG_SYSCTL
2527 addrconf_sysctl_unregister(&idev->cnf);
2528 neigh_sysctl_unregister(idev->nd_parms);
2529#endif
2530 neigh_parms_release(&nd_tbl, idev->nd_parms);
2531 neigh_ifdown(&nd_tbl, dev);
2532 in6_dev_put(idev);
2533 }
2534 return 0;
2535}
2536
2537static void addrconf_rs_timer(unsigned long data)
2538{
2539 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
2540
2541 if (ifp->idev->cnf.forwarding)
2542 goto out;
2543
2544 if (ifp->idev->if_flags & IF_RA_RCVD) {
2545 /*
2546 * Announcement received after solicitation
2547 * was sent
2548 */
2549 goto out;
2550 }
2551
2552 spin_lock(&ifp->lock);
2553 if (ifp->probes++ < ifp->idev->cnf.rtr_solicits) {
2554 struct in6_addr all_routers;
2555
2556 /* The wait after the last probe can be shorter */
2557 addrconf_mod_timer(ifp, AC_RS,
2558 (ifp->probes == ifp->idev->cnf.rtr_solicits) ?
2559 ifp->idev->cnf.rtr_solicit_delay :
2560 ifp->idev->cnf.rtr_solicit_interval);
2561 spin_unlock(&ifp->lock);
2562
2563 ipv6_addr_all_routers(&all_routers);
2564
2565 ndisc_send_rs(ifp->idev->dev, &ifp->addr, &all_routers);
2566 } else {
2567 spin_unlock(&ifp->lock);
2568 /*
2569 * Note: we do not support deprecated "all on-link"
2570 * assumption any longer.
2571 */
2572 printk(KERN_DEBUG "%s: no IPv6 routers present\n",
2573 ifp->idev->dev->name);
2574 }
2575
2576out:
2577 in6_ifa_put(ifp);
2578}
2579
2580/*
2581 * Duplicate Address Detection
2582 */
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002583static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
2584{
2585 unsigned long rand_num;
2586 struct inet6_dev *idev = ifp->idev;
2587
Neil Horman95c385b2007-04-25 17:08:10 -07002588 if (ifp->flags & IFA_F_OPTIMISTIC)
2589 rand_num = 0;
2590 else
2591 rand_num = net_random() % (idev->cnf.rtr_solicit_delay ? : 1);
2592
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002593 ifp->probes = idev->cnf.dad_transmits;
2594 addrconf_mod_timer(ifp, AC_DAD, rand_num);
2595}
2596
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -07002597static void addrconf_dad_start(struct inet6_ifaddr *ifp, u32 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598{
2599 struct inet6_dev *idev = ifp->idev;
2600 struct net_device *dev = idev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601
2602 addrconf_join_solict(dev, &ifp->addr);
2603
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 net_srandom(ifp->addr.s6_addr32[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605
2606 read_lock_bh(&idev->lock);
2607 if (ifp->dead)
2608 goto out;
2609 spin_lock_bh(&ifp->lock);
2610
2611 if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07002612 !(ifp->flags&IFA_F_TENTATIVE) ||
2613 ifp->flags & IFA_F_NODAD) {
Neil Horman95c385b2007-04-25 17:08:10 -07002614 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 spin_unlock_bh(&ifp->lock);
2616 read_unlock_bh(&idev->lock);
2617
2618 addrconf_dad_completed(ifp);
2619 return;
2620 }
2621
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08002622 if (!(idev->if_flags & IF_READY)) {
YOSHIFUJI Hideaki3dd3bf82005-12-23 11:23:21 -08002623 spin_unlock_bh(&ifp->lock);
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08002624 read_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002625 /*
2626 * If the defice is not ready:
2627 * - keep it tentative if it is a permanent address.
2628 * - otherwise, kill it.
2629 */
2630 in6_ifa_hold(ifp);
2631 addrconf_dad_stop(ifp);
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08002632 return;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002633 }
Neil Horman95c385b2007-04-25 17:08:10 -07002634
2635 /*
2636 * Optimistic nodes can start receiving
2637 * Frames right away
2638 */
2639 if(ifp->flags & IFA_F_OPTIMISTIC)
2640 ip6_ins_rt(ifp->rt);
2641
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08002642 addrconf_dad_kick(ifp);
2643 spin_unlock_bh(&ifp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644out:
2645 read_unlock_bh(&idev->lock);
2646}
2647
2648static void addrconf_dad_timer(unsigned long data)
2649{
2650 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
2651 struct inet6_dev *idev = ifp->idev;
2652 struct in6_addr unspec;
2653 struct in6_addr mcaddr;
2654
2655 read_lock_bh(&idev->lock);
2656 if (idev->dead) {
2657 read_unlock_bh(&idev->lock);
2658 goto out;
2659 }
2660 spin_lock_bh(&ifp->lock);
2661 if (ifp->probes == 0) {
2662 /*
2663 * DAD was successful
2664 */
2665
Neil Horman95c385b2007-04-25 17:08:10 -07002666 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 spin_unlock_bh(&ifp->lock);
2668 read_unlock_bh(&idev->lock);
2669
2670 addrconf_dad_completed(ifp);
2671
2672 goto out;
2673 }
2674
2675 ifp->probes--;
2676 addrconf_mod_timer(ifp, AC_DAD, ifp->idev->nd_parms->retrans_time);
2677 spin_unlock_bh(&ifp->lock);
2678 read_unlock_bh(&idev->lock);
2679
2680 /* send a neighbour solicitation for our addr */
2681 memset(&unspec, 0, sizeof(unspec));
2682 addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
2683 ndisc_send_ns(ifp->idev->dev, NULL, &ifp->addr, &mcaddr, &unspec);
2684out:
2685 in6_ifa_put(ifp);
2686}
2687
2688static void addrconf_dad_completed(struct inet6_ifaddr *ifp)
2689{
2690 struct net_device * dev = ifp->idev->dev;
2691
2692 /*
2693 * Configure the address for reception. Now it is valid.
2694 */
2695
2696 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2697
2698 /* If added prefix is link local and forwarding is off,
2699 start sending router solicitations.
2700 */
2701
2702 if (ifp->idev->cnf.forwarding == 0 &&
2703 ifp->idev->cnf.rtr_solicits > 0 &&
2704 (dev->flags&IFF_LOOPBACK) == 0 &&
2705 (ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL)) {
2706 struct in6_addr all_routers;
2707
2708 ipv6_addr_all_routers(&all_routers);
2709
2710 /*
2711 * If a host as already performed a random delay
2712 * [...] as part of DAD [...] there is no need
2713 * to delay again before sending the first RS
2714 */
2715 ndisc_send_rs(ifp->idev->dev, &ifp->addr, &all_routers);
2716
2717 spin_lock_bh(&ifp->lock);
2718 ifp->probes = 1;
2719 ifp->idev->if_flags |= IF_RS_SENT;
2720 addrconf_mod_timer(ifp, AC_RS, ifp->idev->cnf.rtr_solicit_interval);
2721 spin_unlock_bh(&ifp->lock);
2722 }
2723}
2724
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09002725static void addrconf_dad_run(struct inet6_dev *idev) {
2726 struct inet6_ifaddr *ifp;
2727
2728 read_lock_bh(&idev->lock);
2729 for (ifp = idev->addr_list; ifp; ifp = ifp->if_next) {
2730 spin_lock_bh(&ifp->lock);
2731 if (!(ifp->flags & IFA_F_TENTATIVE)) {
2732 spin_unlock_bh(&ifp->lock);
2733 continue;
2734 }
2735 spin_unlock_bh(&ifp->lock);
2736 addrconf_dad_kick(ifp);
2737 }
2738 read_unlock_bh(&idev->lock);
2739}
2740
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741#ifdef CONFIG_PROC_FS
2742struct if6_iter_state {
2743 int bucket;
2744};
2745
2746static struct inet6_ifaddr *if6_get_first(struct seq_file *seq)
2747{
2748 struct inet6_ifaddr *ifa = NULL;
2749 struct if6_iter_state *state = seq->private;
2750
2751 for (state->bucket = 0; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
2752 ifa = inet6_addr_lst[state->bucket];
2753 if (ifa)
2754 break;
2755 }
2756 return ifa;
2757}
2758
2759static struct inet6_ifaddr *if6_get_next(struct seq_file *seq, struct inet6_ifaddr *ifa)
2760{
2761 struct if6_iter_state *state = seq->private;
2762
2763 ifa = ifa->lst_next;
2764try_again:
2765 if (!ifa && ++state->bucket < IN6_ADDR_HSIZE) {
2766 ifa = inet6_addr_lst[state->bucket];
2767 goto try_again;
2768 }
2769 return ifa;
2770}
2771
2772static struct inet6_ifaddr *if6_get_idx(struct seq_file *seq, loff_t pos)
2773{
2774 struct inet6_ifaddr *ifa = if6_get_first(seq);
2775
2776 if (ifa)
2777 while(pos && (ifa = if6_get_next(seq, ifa)) != NULL)
2778 --pos;
2779 return pos ? NULL : ifa;
2780}
2781
2782static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
2783{
2784 read_lock_bh(&addrconf_hash_lock);
2785 return if6_get_idx(seq, *pos);
2786}
2787
2788static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2789{
2790 struct inet6_ifaddr *ifa;
2791
2792 ifa = if6_get_next(seq, v);
2793 ++*pos;
2794 return ifa;
2795}
2796
2797static void if6_seq_stop(struct seq_file *seq, void *v)
2798{
2799 read_unlock_bh(&addrconf_hash_lock);
2800}
2801
2802static int if6_seq_show(struct seq_file *seq, void *v)
2803{
2804 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
2805 seq_printf(seq,
YOSHIFUJI Hideaki9343e792006-01-17 02:10:53 -08002806 NIP6_SEQFMT " %02x %02x %02x %02x %8s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 NIP6(ifp->addr),
2808 ifp->idev->dev->ifindex,
2809 ifp->prefix_len,
2810 ifp->scope,
2811 ifp->flags,
2812 ifp->idev->dev->name);
2813 return 0;
2814}
2815
Philippe De Muyter56b3d972007-07-10 23:07:31 -07002816static const struct seq_operations if6_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 .start = if6_seq_start,
2818 .next = if6_seq_next,
2819 .show = if6_seq_show,
2820 .stop = if6_seq_stop,
2821};
2822
2823static int if6_seq_open(struct inode *inode, struct file *file)
2824{
Pavel Emelyanovcf7732e2007-10-10 02:29:29 -07002825 return seq_open_private(file, &if6_seq_ops,
2826 sizeof(struct if6_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827}
2828
Arjan van de Ven9a321442007-02-12 00:55:35 -08002829static const struct file_operations if6_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 .owner = THIS_MODULE,
2831 .open = if6_seq_open,
2832 .read = seq_read,
2833 .llseek = seq_lseek,
2834 .release = seq_release_private,
2835};
2836
2837int __init if6_proc_init(void)
2838{
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02002839 if (!proc_net_fops_create(&init_net, "if_inet6", S_IRUGO, &if6_fops))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 return -ENOMEM;
2841 return 0;
2842}
2843
2844void if6_proc_exit(void)
2845{
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02002846 proc_net_remove(&init_net, "if_inet6");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847}
2848#endif /* CONFIG_PROC_FS */
2849
Masahide NAKAMURA59fbb3a2007-06-26 23:56:32 -07002850#if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07002851/* Check if address is a home address configured on any interface. */
2852int ipv6_chk_home_addr(struct in6_addr *addr)
2853{
2854 int ret = 0;
2855 struct inet6_ifaddr * ifp;
2856 u8 hash = ipv6_addr_hash(addr);
2857 read_lock_bh(&addrconf_hash_lock);
2858 for (ifp = inet6_addr_lst[hash]; ifp; ifp = ifp->lst_next) {
2859 if (ipv6_addr_cmp(&ifp->addr, addr) == 0 &&
2860 (ifp->flags & IFA_F_HOMEADDRESS)) {
2861 ret = 1;
2862 break;
2863 }
2864 }
2865 read_unlock_bh(&addrconf_hash_lock);
2866 return ret;
2867}
2868#endif
2869
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870/*
2871 * Periodic address status verification
2872 */
2873
2874static void addrconf_verify(unsigned long foo)
2875{
2876 struct inet6_ifaddr *ifp;
2877 unsigned long now, next;
2878 int i;
2879
2880 spin_lock_bh(&addrconf_verify_lock);
2881 now = jiffies;
2882 next = now + ADDR_CHECK_FREQUENCY;
2883
2884 del_timer(&addr_chk_timer);
2885
2886 for (i=0; i < IN6_ADDR_HSIZE; i++) {
2887
2888restart:
Yan Zheng5d5780d2005-11-20 13:42:20 -08002889 read_lock(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 for (ifp=inet6_addr_lst[i]; ifp; ifp=ifp->lst_next) {
2891 unsigned long age;
2892#ifdef CONFIG_IPV6_PRIVACY
2893 unsigned long regen_advance;
2894#endif
2895
2896 if (ifp->flags & IFA_F_PERMANENT)
2897 continue;
2898
2899 spin_lock(&ifp->lock);
2900 age = (now - ifp->tstamp) / HZ;
2901
2902#ifdef CONFIG_IPV6_PRIVACY
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002903 regen_advance = ifp->idev->cnf.regen_max_retry *
2904 ifp->idev->cnf.dad_transmits *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 ifp->idev->nd_parms->retrans_time / HZ;
2906#endif
2907
YOSHIFUJI Hideaki8f27ebb2006-07-28 18:12:11 +09002908 if (ifp->valid_lft != INFINITY_LIFE_TIME &&
2909 age >= ifp->valid_lft) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 spin_unlock(&ifp->lock);
2911 in6_ifa_hold(ifp);
Yan Zheng5d5780d2005-11-20 13:42:20 -08002912 read_unlock(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 ipv6_del_addr(ifp);
2914 goto restart;
YOSHIFUJI Hideaki8f27ebb2006-07-28 18:12:11 +09002915 } else if (ifp->prefered_lft == INFINITY_LIFE_TIME) {
2916 spin_unlock(&ifp->lock);
2917 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 } else if (age >= ifp->prefered_lft) {
2919 /* jiffies - ifp->tsamp > age >= ifp->prefered_lft */
2920 int deprecate = 0;
2921
2922 if (!(ifp->flags&IFA_F_DEPRECATED)) {
2923 deprecate = 1;
2924 ifp->flags |= IFA_F_DEPRECATED;
2925 }
2926
2927 if (time_before(ifp->tstamp + ifp->valid_lft * HZ, next))
2928 next = ifp->tstamp + ifp->valid_lft * HZ;
2929
2930 spin_unlock(&ifp->lock);
2931
2932 if (deprecate) {
2933 in6_ifa_hold(ifp);
Yan Zheng5d5780d2005-11-20 13:42:20 -08002934 read_unlock(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935
2936 ipv6_ifa_notify(0, ifp);
2937 in6_ifa_put(ifp);
2938 goto restart;
2939 }
2940#ifdef CONFIG_IPV6_PRIVACY
2941 } else if ((ifp->flags&IFA_F_TEMPORARY) &&
2942 !(ifp->flags&IFA_F_TENTATIVE)) {
2943 if (age >= ifp->prefered_lft - regen_advance) {
2944 struct inet6_ifaddr *ifpub = ifp->ifpub;
2945 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
2946 next = ifp->tstamp + ifp->prefered_lft * HZ;
2947 if (!ifp->regen_count && ifpub) {
2948 ifp->regen_count++;
2949 in6_ifa_hold(ifp);
2950 in6_ifa_hold(ifpub);
2951 spin_unlock(&ifp->lock);
Yan Zheng5d5780d2005-11-20 13:42:20 -08002952 read_unlock(&addrconf_hash_lock);
Hiroyuki YAMAMORI291d8092005-12-23 11:24:05 -08002953 spin_lock(&ifpub->lock);
2954 ifpub->regen_count = 0;
2955 spin_unlock(&ifpub->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 ipv6_create_tempaddr(ifpub, ifp);
2957 in6_ifa_put(ifpub);
2958 in6_ifa_put(ifp);
2959 goto restart;
2960 }
2961 } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
2962 next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
2963 spin_unlock(&ifp->lock);
2964#endif
2965 } else {
2966 /* ifp->prefered_lft <= ifp->valid_lft */
2967 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
2968 next = ifp->tstamp + ifp->prefered_lft * HZ;
2969 spin_unlock(&ifp->lock);
2970 }
2971 }
Yan Zheng5d5780d2005-11-20 13:42:20 -08002972 read_unlock(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 }
2974
2975 addr_chk_timer.expires = time_before(next, jiffies + HZ) ? jiffies + HZ : next;
2976 add_timer(&addr_chk_timer);
2977 spin_unlock_bh(&addrconf_verify_lock);
2978}
2979
Thomas Graf461d8832006-09-18 00:09:49 -07002980static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local)
2981{
2982 struct in6_addr *pfx = NULL;
2983
2984 if (addr)
2985 pfx = nla_data(addr);
2986
2987 if (local) {
2988 if (pfx && nla_memcmp(local, pfx, sizeof(*pfx)))
2989 pfx = NULL;
2990 else
2991 pfx = nla_data(local);
2992 }
2993
2994 return pfx;
2995}
2996
Patrick McHardyef7c79e2007-06-05 12:38:30 -07002997static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = {
Thomas Graf461d8832006-09-18 00:09:49 -07002998 [IFA_ADDRESS] = { .len = sizeof(struct in6_addr) },
2999 [IFA_LOCAL] = { .len = sizeof(struct in6_addr) },
3000 [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) },
3001};
3002
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003static int
3004inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
3005{
Denis V. Lunevb8542722007-12-01 00:21:31 +11003006 struct net *net = skb->sk->sk_net;
Thomas Grafb933f712006-09-18 00:10:19 -07003007 struct ifaddrmsg *ifm;
3008 struct nlattr *tb[IFA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 struct in6_addr *pfx;
Thomas Grafb933f712006-09-18 00:10:19 -07003010 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011
Denis V. Lunevb8542722007-12-01 00:21:31 +11003012 if (net != &init_net)
3013 return -EINVAL;
3014
Thomas Grafb933f712006-09-18 00:10:19 -07003015 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
3016 if (err < 0)
3017 return err;
3018
3019 ifm = nlmsg_data(nlh);
3020 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 if (pfx == NULL)
3022 return -EINVAL;
3023
3024 return inet6_addr_del(ifm->ifa_index, pfx, ifm->ifa_prefixlen);
3025}
3026
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003027static int inet6_addr_modify(struct inet6_ifaddr *ifp, u8 ifa_flags,
3028 u32 prefered_lft, u32 valid_lft)
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09003029{
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09003030 u32 flags = RTF_EXPIRES;
3031
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09003032 if (!valid_lft || (prefered_lft > valid_lft))
3033 return -EINVAL;
3034
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09003035 if (valid_lft == INFINITY_LIFE_TIME) {
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003036 ifa_flags |= IFA_F_PERMANENT;
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09003037 flags = 0;
3038 } else if (valid_lft >= 0x7FFFFFFF/HZ)
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09003039 valid_lft = 0x7FFFFFFF/HZ;
3040
3041 if (prefered_lft == 0)
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003042 ifa_flags |= IFA_F_DEPRECATED;
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09003043 else if ((prefered_lft >= 0x7FFFFFFF/HZ) &&
3044 (prefered_lft != INFINITY_LIFE_TIME))
3045 prefered_lft = 0x7FFFFFFF/HZ;
3046
3047 spin_lock_bh(&ifp->lock);
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07003048 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 +09003049 ifp->tstamp = jiffies;
3050 ifp->valid_lft = valid_lft;
3051 ifp->prefered_lft = prefered_lft;
3052
3053 spin_unlock_bh(&ifp->lock);
3054 if (!(ifp->flags&IFA_F_TENTATIVE))
3055 ipv6_ifa_notify(0, ifp);
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09003056
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09003057 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, ifp->idev->dev,
3058 jiffies_to_clock_t(valid_lft * HZ), flags);
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09003059 addrconf_verify(0);
3060
3061 return 0;
3062}
3063
3064static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
3066{
Denis V. Lunevb8542722007-12-01 00:21:31 +11003067 struct net *net = skb->sk->sk_net;
Thomas Graf461d8832006-09-18 00:09:49 -07003068 struct ifaddrmsg *ifm;
3069 struct nlattr *tb[IFA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 struct in6_addr *pfx;
Thomas Graf7198f8c2006-09-18 00:13:46 -07003071 struct inet6_ifaddr *ifa;
3072 struct net_device *dev;
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003073 u32 valid_lft = INFINITY_LIFE_TIME, preferred_lft = INFINITY_LIFE_TIME;
3074 u8 ifa_flags;
Thomas Graf461d8832006-09-18 00:09:49 -07003075 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076
Denis V. Lunevb8542722007-12-01 00:21:31 +11003077 if (net != &init_net)
3078 return -EINVAL;
3079
Thomas Graf461d8832006-09-18 00:09:49 -07003080 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
3081 if (err < 0)
3082 return err;
3083
3084 ifm = nlmsg_data(nlh);
3085 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086 if (pfx == NULL)
3087 return -EINVAL;
3088
Thomas Graf461d8832006-09-18 00:09:49 -07003089 if (tb[IFA_CACHEINFO]) {
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09003090 struct ifa_cacheinfo *ci;
Thomas Graf461d8832006-09-18 00:09:49 -07003091
3092 ci = nla_data(tb[IFA_CACHEINFO]);
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09003093 valid_lft = ci->ifa_valid;
Thomas Graf461d8832006-09-18 00:09:49 -07003094 preferred_lft = ci->ifa_prefered;
3095 } else {
3096 preferred_lft = INFINITY_LIFE_TIME;
3097 valid_lft = INFINITY_LIFE_TIME;
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09003098 }
3099
Eric W. Biederman881d9662007-09-17 11:56:21 -07003100 dev = __dev_get_by_index(&init_net, ifm->ifa_index);
Thomas Graf7198f8c2006-09-18 00:13:46 -07003101 if (dev == NULL)
3102 return -ENODEV;
3103
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003104 /* We ignore other flags so far. */
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07003105 ifa_flags = ifm->ifa_flags & (IFA_F_NODAD | IFA_F_HOMEADDRESS);
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003106
Thomas Graf7198f8c2006-09-18 00:13:46 -07003107 ifa = ipv6_get_ifaddr(pfx, dev, 1);
3108 if (ifa == NULL) {
3109 /*
3110 * It would be best to check for !NLM_F_CREATE here but
3111 * userspace alreay relies on not having to provide this.
3112 */
3113 return inet6_addr_add(ifm->ifa_index, pfx, ifm->ifa_prefixlen,
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003114 ifa_flags, preferred_lft, valid_lft);
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09003115 }
3116
Thomas Graf7198f8c2006-09-18 00:13:46 -07003117 if (nlh->nlmsg_flags & NLM_F_EXCL ||
3118 !(nlh->nlmsg_flags & NLM_F_REPLACE))
3119 err = -EEXIST;
3120 else
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003121 err = inet6_addr_modify(ifa, ifa_flags, preferred_lft, valid_lft);
Thomas Graf7198f8c2006-09-18 00:13:46 -07003122
3123 in6_ifa_put(ifa);
3124
3125 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126}
3127
Thomas Graf101bb222006-09-18 00:11:52 -07003128static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u8 flags,
3129 u8 scope, int ifindex)
3130{
3131 struct ifaddrmsg *ifm;
3132
3133 ifm = nlmsg_data(nlh);
3134 ifm->ifa_family = AF_INET6;
3135 ifm->ifa_prefixlen = prefixlen;
3136 ifm->ifa_flags = flags;
3137 ifm->ifa_scope = scope;
3138 ifm->ifa_index = ifindex;
3139}
3140
Thomas Graf85486af2006-09-18 00:11:24 -07003141static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
3142 unsigned long tstamp, u32 preferred, u32 valid)
3143{
3144 struct ifa_cacheinfo ci;
3145
3146 ci.cstamp = (u32)(TIME_DELTA(cstamp, INITIAL_JIFFIES) / HZ * 100
3147 + TIME_DELTA(cstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
3148 ci.tstamp = (u32)(TIME_DELTA(tstamp, INITIAL_JIFFIES) / HZ * 100
3149 + TIME_DELTA(tstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
3150 ci.ifa_prefered = preferred;
3151 ci.ifa_valid = valid;
3152
3153 return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
3154}
3155
Thomas Graf101bb222006-09-18 00:11:52 -07003156static inline int rt_scope(int ifa_scope)
3157{
3158 if (ifa_scope & IFA_HOST)
3159 return RT_SCOPE_HOST;
3160 else if (ifa_scope & IFA_LINK)
3161 return RT_SCOPE_LINK;
3162 else if (ifa_scope & IFA_SITE)
3163 return RT_SCOPE_SITE;
3164 else
3165 return RT_SCOPE_UNIVERSE;
3166}
3167
Thomas Graf0ab68032006-09-18 00:12:35 -07003168static inline int inet6_ifaddr_msgsize(void)
3169{
Thomas Graf339bf982006-11-10 14:10:15 -08003170 return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
3171 + nla_total_size(16) /* IFA_ADDRESS */
3172 + nla_total_size(sizeof(struct ifa_cacheinfo));
Thomas Graf0ab68032006-09-18 00:12:35 -07003173}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07003174
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07003176 u32 pid, u32 seq, int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178 struct nlmsghdr *nlh;
Thomas Graf85486af2006-09-18 00:11:24 -07003179 u32 preferred, valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180
Thomas Graf0ab68032006-09-18 00:12:35 -07003181 nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags);
3182 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08003183 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07003184
Thomas Graf101bb222006-09-18 00:11:52 -07003185 put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope),
3186 ifa->idev->dev->ifindex);
3187
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188 if (!(ifa->flags&IFA_F_PERMANENT)) {
Thomas Graf85486af2006-09-18 00:11:24 -07003189 preferred = ifa->prefered_lft;
3190 valid = ifa->valid_lft;
3191 if (preferred != INFINITY_LIFE_TIME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192 long tval = (jiffies - ifa->tstamp)/HZ;
Thomas Graf85486af2006-09-18 00:11:24 -07003193 preferred -= tval;
3194 if (valid != INFINITY_LIFE_TIME)
3195 valid -= tval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196 }
3197 } else {
Thomas Graf85486af2006-09-18 00:11:24 -07003198 preferred = INFINITY_LIFE_TIME;
3199 valid = INFINITY_LIFE_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 }
Thomas Graf85486af2006-09-18 00:11:24 -07003201
Thomas Graf0ab68032006-09-18 00:12:35 -07003202 if (nla_put(skb, IFA_ADDRESS, 16, &ifa->addr) < 0 ||
Patrick McHardy26932562007-01-31 23:16:40 -08003203 put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0) {
3204 nlmsg_cancel(skb, nlh);
3205 return -EMSGSIZE;
3206 }
Thomas Graf85486af2006-09-18 00:11:24 -07003207
Thomas Graf0ab68032006-09-18 00:12:35 -07003208 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209}
3210
3211static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -07003212 u32 pid, u32 seq, int event, u16 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 struct nlmsghdr *nlh;
Thomas Graf101bb222006-09-18 00:11:52 -07003215 u8 scope = RT_SCOPE_UNIVERSE;
3216 int ifindex = ifmca->idev->dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217
Thomas Graf101bb222006-09-18 00:11:52 -07003218 if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE)
3219 scope = RT_SCOPE_SITE;
3220
Thomas Graf0ab68032006-09-18 00:12:35 -07003221 nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags);
3222 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08003223 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07003224
Thomas Graf101bb222006-09-18 00:11:52 -07003225 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
Thomas Graf0ab68032006-09-18 00:12:35 -07003226 if (nla_put(skb, IFA_MULTICAST, 16, &ifmca->mca_addr) < 0 ||
3227 put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp,
Patrick McHardy26932562007-01-31 23:16:40 -08003228 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
3229 nlmsg_cancel(skb, nlh);
3230 return -EMSGSIZE;
3231 }
Thomas Graf85486af2006-09-18 00:11:24 -07003232
Thomas Graf0ab68032006-09-18 00:12:35 -07003233 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234}
3235
3236static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07003237 u32 pid, u32 seq, int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239 struct nlmsghdr *nlh;
Thomas Graf101bb222006-09-18 00:11:52 -07003240 u8 scope = RT_SCOPE_UNIVERSE;
3241 int ifindex = ifaca->aca_idev->dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242
Thomas Graf101bb222006-09-18 00:11:52 -07003243 if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE)
3244 scope = RT_SCOPE_SITE;
3245
Thomas Graf0ab68032006-09-18 00:12:35 -07003246 nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags);
3247 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08003248 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07003249
Thomas Graf101bb222006-09-18 00:11:52 -07003250 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
Thomas Graf0ab68032006-09-18 00:12:35 -07003251 if (nla_put(skb, IFA_ANYCAST, 16, &ifaca->aca_addr) < 0 ||
3252 put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp,
Patrick McHardy26932562007-01-31 23:16:40 -08003253 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
3254 nlmsg_cancel(skb, nlh);
3255 return -EMSGSIZE;
3256 }
Thomas Graf85486af2006-09-18 00:11:24 -07003257
Thomas Graf0ab68032006-09-18 00:12:35 -07003258 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259}
3260
3261enum addr_type_t
3262{
3263 UNICAST_ADDR,
3264 MULTICAST_ADDR,
3265 ANYCAST_ADDR,
3266};
3267
3268static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
3269 enum addr_type_t type)
3270{
3271 int idx, ip_idx;
3272 int s_idx, s_ip_idx;
3273 int err = 1;
3274 struct net_device *dev;
3275 struct inet6_dev *idev = NULL;
3276 struct inet6_ifaddr *ifa;
3277 struct ifmcaddr6 *ifmca;
3278 struct ifacaddr6 *ifaca;
3279
3280 s_idx = cb->args[0];
3281 s_ip_idx = ip_idx = cb->args[1];
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003282
Pavel Emelianov7562f872007-05-03 15:13:45 -07003283 idx = 0;
Eric W. Biederman881d9662007-09-17 11:56:21 -07003284 for_each_netdev(&init_net, dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285 if (idx < s_idx)
Pavel Emelianov7562f872007-05-03 15:13:45 -07003286 goto cont;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 if (idx > s_idx)
3288 s_ip_idx = 0;
3289 ip_idx = 0;
3290 if ((idev = in6_dev_get(dev)) == NULL)
Pavel Emelianov7562f872007-05-03 15:13:45 -07003291 goto cont;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292 read_lock_bh(&idev->lock);
3293 switch (type) {
3294 case UNICAST_ADDR:
YOSHIFUJI Hideakiae9cda52005-06-28 13:00:30 -07003295 /* unicast address incl. temp addr */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296 for (ifa = idev->addr_list; ifa;
3297 ifa = ifa->if_next, ip_idx++) {
3298 if (ip_idx < s_ip_idx)
3299 continue;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003300 if ((err = inet6_fill_ifaddr(skb, ifa,
3301 NETLINK_CB(cb->skb).pid,
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07003302 cb->nlh->nlmsg_seq, RTM_NEWADDR,
3303 NLM_F_MULTI)) <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304 goto done;
3305 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306 break;
3307 case MULTICAST_ADDR:
3308 /* multicast address */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003309 for (ifmca = idev->mc_list; ifmca;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310 ifmca = ifmca->next, ip_idx++) {
3311 if (ip_idx < s_ip_idx)
3312 continue;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003313 if ((err = inet6_fill_ifmcaddr(skb, ifmca,
3314 NETLINK_CB(cb->skb).pid,
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07003315 cb->nlh->nlmsg_seq, RTM_GETMULTICAST,
3316 NLM_F_MULTI)) <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317 goto done;
3318 }
3319 break;
3320 case ANYCAST_ADDR:
3321 /* anycast address */
3322 for (ifaca = idev->ac_list; ifaca;
3323 ifaca = ifaca->aca_next, ip_idx++) {
3324 if (ip_idx < s_ip_idx)
3325 continue;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003326 if ((err = inet6_fill_ifacaddr(skb, ifaca,
3327 NETLINK_CB(cb->skb).pid,
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07003328 cb->nlh->nlmsg_seq, RTM_GETANYCAST,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003329 NLM_F_MULTI)) <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330 goto done;
3331 }
3332 break;
3333 default:
3334 break;
3335 }
3336 read_unlock_bh(&idev->lock);
3337 in6_dev_put(idev);
Pavel Emelianov7562f872007-05-03 15:13:45 -07003338cont:
3339 idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 }
3341done:
3342 if (err <= 0) {
3343 read_unlock_bh(&idev->lock);
3344 in6_dev_put(idev);
3345 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346 cb->args[0] = idx;
3347 cb->args[1] = ip_idx;
3348 return skb->len;
3349}
3350
3351static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
3352{
Denis V. Lunevb8542722007-12-01 00:21:31 +11003353 struct net *net = skb->sk->sk_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354 enum addr_type_t type = UNICAST_ADDR;
Denis V. Lunevb8542722007-12-01 00:21:31 +11003355
3356 if (net != &init_net)
3357 return 0;
3358
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359 return inet6_dump_addr(skb, cb, type);
3360}
3361
3362static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
3363{
Denis V. Lunevb8542722007-12-01 00:21:31 +11003364 struct net *net = skb->sk->sk_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365 enum addr_type_t type = MULTICAST_ADDR;
Denis V. Lunevb8542722007-12-01 00:21:31 +11003366
3367 if (net != &init_net)
3368 return 0;
3369
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370 return inet6_dump_addr(skb, cb, type);
3371}
3372
3373
3374static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
3375{
Denis V. Lunevb8542722007-12-01 00:21:31 +11003376 struct net *net = skb->sk->sk_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 enum addr_type_t type = ANYCAST_ADDR;
Denis V. Lunevb8542722007-12-01 00:21:31 +11003378
3379 if (net != &init_net)
3380 return 0;
3381
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382 return inet6_dump_addr(skb, cb, type);
3383}
3384
Thomas Graf1b29fc22006-09-18 00:10:50 -07003385static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr* nlh,
3386 void *arg)
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003387{
Denis V. Lunevb8542722007-12-01 00:21:31 +11003388 struct net *net = in_skb->sk->sk_net;
Thomas Graf1b29fc22006-09-18 00:10:50 -07003389 struct ifaddrmsg *ifm;
3390 struct nlattr *tb[IFA_MAX+1];
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003391 struct in6_addr *addr = NULL;
3392 struct net_device *dev = NULL;
3393 struct inet6_ifaddr *ifa;
3394 struct sk_buff *skb;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003395 int err;
3396
Denis V. Lunevb8542722007-12-01 00:21:31 +11003397 if (net != &init_net)
3398 return -EINVAL;
3399
Thomas Graf1b29fc22006-09-18 00:10:50 -07003400 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
3401 if (err < 0)
3402 goto errout;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003403
Thomas Graf1b29fc22006-09-18 00:10:50 -07003404 addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
3405 if (addr == NULL) {
3406 err = -EINVAL;
3407 goto errout;
3408 }
3409
3410 ifm = nlmsg_data(nlh);
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003411 if (ifm->ifa_index)
Eric W. Biederman881d9662007-09-17 11:56:21 -07003412 dev = __dev_get_by_index(&init_net, ifm->ifa_index);
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003413
Thomas Graf1b29fc22006-09-18 00:10:50 -07003414 if ((ifa = ipv6_get_ifaddr(addr, dev, 1)) == NULL) {
3415 err = -EADDRNOTAVAIL;
3416 goto errout;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003417 }
3418
Thomas Graf0ab68032006-09-18 00:12:35 -07003419 if ((skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL)) == NULL) {
Thomas Graf1b29fc22006-09-18 00:10:50 -07003420 err = -ENOBUFS;
3421 goto errout_ifa;
3422 }
3423
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003424 err = inet6_fill_ifaddr(skb, ifa, NETLINK_CB(in_skb).pid,
3425 nlh->nlmsg_seq, RTM_NEWADDR, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08003426 if (err < 0) {
3427 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
3428 WARN_ON(err == -EMSGSIZE);
3429 kfree_skb(skb);
3430 goto errout_ifa;
3431 }
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003432 err = rtnl_unicast(skb, &init_net, NETLINK_CB(in_skb).pid);
Thomas Graf1b29fc22006-09-18 00:10:50 -07003433errout_ifa:
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003434 in6_ifa_put(ifa);
Thomas Graf1b29fc22006-09-18 00:10:50 -07003435errout:
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003436 return err;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003437}
3438
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
3440{
3441 struct sk_buff *skb;
Thomas Graf5d620262006-08-15 00:35:02 -07003442 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443
Thomas Graf0ab68032006-09-18 00:12:35 -07003444 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC);
Thomas Graf5d620262006-08-15 00:35:02 -07003445 if (skb == NULL)
3446 goto errout;
3447
3448 err = inet6_fill_ifaddr(skb, ifa, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08003449 if (err < 0) {
3450 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
3451 WARN_ON(err == -EMSGSIZE);
3452 kfree_skb(skb);
3453 goto errout;
3454 }
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003455 err = rtnl_notify(skb, &init_net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
Thomas Graf5d620262006-08-15 00:35:02 -07003456errout:
3457 if (err < 0)
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003458 rtnl_set_sk_err(&init_net, RTNLGRP_IPV6_IFADDR, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459}
3460
Dave Jonesb6f99a22007-03-22 12:27:49 -07003461static inline void ipv6_store_devconf(struct ipv6_devconf *cnf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462 __s32 *array, int bytes)
3463{
Thomas Graf04561c12006-11-14 19:53:58 -08003464 BUG_ON(bytes < (DEVCONF_MAX * 4));
3465
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466 memset(array, 0, bytes);
3467 array[DEVCONF_FORWARDING] = cnf->forwarding;
3468 array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
3469 array[DEVCONF_MTU6] = cnf->mtu6;
3470 array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
3471 array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
3472 array[DEVCONF_AUTOCONF] = cnf->autoconf;
3473 array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
3474 array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
3475 array[DEVCONF_RTR_SOLICIT_INTERVAL] = cnf->rtr_solicit_interval;
3476 array[DEVCONF_RTR_SOLICIT_DELAY] = cnf->rtr_solicit_delay;
3477 array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
3478#ifdef CONFIG_IPV6_PRIVACY
3479 array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
3480 array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
3481 array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
3482 array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
3483 array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
3484#endif
3485 array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -08003486 array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr;
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -08003487 array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo;
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08003488#ifdef CONFIG_IPV6_ROUTER_PREF
3489 array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref;
YOSHIFUJI Hideaki52e16352006-03-20 17:05:47 -08003490 array[DEVCONF_RTR_PROBE_INTERVAL] = cnf->rtr_probe_interval;
Neil Hormanfa03ef32007-01-30 14:30:10 -08003491#ifdef CONFIG_IPV6_ROUTE_INFO
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -08003492 array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen;
3493#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08003494#endif
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -07003495 array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp;
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -07003496 array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route;
Neil Horman95c385b2007-04-25 17:08:10 -07003497#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
3498 array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad;
3499#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500}
3501
Thomas Graf339bf982006-11-10 14:10:15 -08003502static inline size_t inet6_if_nlmsg_size(void)
3503{
3504 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
3505 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
3506 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
3507 + nla_total_size(4) /* IFLA_MTU */
3508 + nla_total_size(4) /* IFLA_LINK */
3509 + nla_total_size( /* IFLA_PROTINFO */
3510 nla_total_size(4) /* IFLA_INET6_FLAGS */
3511 + nla_total_size(sizeof(struct ifla_cacheinfo))
3512 + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07003513 + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */
3514 + nla_total_size(ICMP6_MIB_MAX * 8) /* IFLA_INET6_ICMP6STATS */
Thomas Graf339bf982006-11-10 14:10:15 -08003515 );
3516}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07003517
Herbert Xu7f7d9a62007-04-24 21:54:09 -07003518static inline void __snmp6_fill_stats(u64 *stats, void **mib, int items,
3519 int bytes)
3520{
3521 int i;
3522 int pad = bytes - sizeof(u64) * items;
3523 BUG_ON(pad < 0);
3524
3525 /* Use put_unaligned() because stats may not be aligned for u64. */
3526 put_unaligned(items, &stats[0]);
3527 for (i = 1; i < items; i++)
3528 put_unaligned(snmp_fold_field(mib, i), &stats[i]);
3529
3530 memset(&stats[items], 0, pad);
3531}
3532
3533static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype,
3534 int bytes)
3535{
3536 switch(attrtype) {
3537 case IFLA_INET6_STATS:
3538 __snmp6_fill_stats(stats, (void **)idev->stats.ipv6, IPSTATS_MIB_MAX, bytes);
3539 break;
3540 case IFLA_INET6_ICMP6STATS:
3541 __snmp6_fill_stats(stats, (void **)idev->stats.icmpv6, ICMP6_MIB_MAX, bytes);
3542 break;
3543 }
3544}
3545
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003546static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07003547 u32 pid, u32 seq, int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548{
Thomas Graf04561c12006-11-14 19:53:58 -08003549 struct net_device *dev = idev->dev;
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07003550 struct nlattr *nla;
Thomas Graf04561c12006-11-14 19:53:58 -08003551 struct ifinfomsg *hdr;
3552 struct nlmsghdr *nlh;
3553 void *protoinfo;
3554 struct ifla_cacheinfo ci;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555
Thomas Graf04561c12006-11-14 19:53:58 -08003556 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*hdr), flags);
3557 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08003558 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559
Thomas Graf04561c12006-11-14 19:53:58 -08003560 hdr = nlmsg_data(nlh);
3561 hdr->ifi_family = AF_INET6;
3562 hdr->__ifi_pad = 0;
3563 hdr->ifi_type = dev->type;
3564 hdr->ifi_index = dev->ifindex;
3565 hdr->ifi_flags = dev_get_flags(dev);
3566 hdr->ifi_change = 0;
3567
3568 NLA_PUT_STRING(skb, IFLA_IFNAME, dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569
3570 if (dev->addr_len)
Thomas Graf04561c12006-11-14 19:53:58 -08003571 NLA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572
Thomas Graf04561c12006-11-14 19:53:58 -08003573 NLA_PUT_U32(skb, IFLA_MTU, dev->mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574 if (dev->ifindex != dev->iflink)
Thomas Graf04561c12006-11-14 19:53:58 -08003575 NLA_PUT_U32(skb, IFLA_LINK, dev->iflink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576
Thomas Graf04561c12006-11-14 19:53:58 -08003577 protoinfo = nla_nest_start(skb, IFLA_PROTINFO);
3578 if (protoinfo == NULL)
3579 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580
Thomas Graf04561c12006-11-14 19:53:58 -08003581 NLA_PUT_U32(skb, IFLA_INET6_FLAGS, idev->if_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583 ci.max_reasm_len = IPV6_MAXPLEN;
3584 ci.tstamp = (__u32)(TIME_DELTA(idev->tstamp, INITIAL_JIFFIES) / HZ * 100
3585 + TIME_DELTA(idev->tstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
3586 ci.reachable_time = idev->nd_parms->reachable_time;
3587 ci.retrans_time = idev->nd_parms->retrans_time;
Thomas Graf04561c12006-11-14 19:53:58 -08003588 NLA_PUT(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci);
3589
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07003590 nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32));
3591 if (nla == NULL)
Thomas Graf04561c12006-11-14 19:53:58 -08003592 goto nla_put_failure;
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07003593 ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07003595 /* XXX - MC not implemented */
3596
3597 nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64));
3598 if (nla == NULL)
3599 goto nla_put_failure;
3600 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla));
3601
3602 nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64));
3603 if (nla == NULL)
3604 goto nla_put_failure;
3605 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606
Thomas Graf04561c12006-11-14 19:53:58 -08003607 nla_nest_end(skb, protoinfo);
3608 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609
Thomas Graf04561c12006-11-14 19:53:58 -08003610nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08003611 nlmsg_cancel(skb, nlh);
3612 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613}
3614
3615static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
3616{
Denis V. Lunevb8542722007-12-01 00:21:31 +11003617 struct net *net = skb->sk->sk_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618 int idx, err;
3619 int s_idx = cb->args[0];
3620 struct net_device *dev;
3621 struct inet6_dev *idev;
3622
Denis V. Lunevb8542722007-12-01 00:21:31 +11003623 if (net != &init_net)
3624 return 0;
3625
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626 read_lock(&dev_base_lock);
Pavel Emelianov7562f872007-05-03 15:13:45 -07003627 idx = 0;
Eric W. Biederman881d9662007-09-17 11:56:21 -07003628 for_each_netdev(&init_net, dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629 if (idx < s_idx)
Pavel Emelianov7562f872007-05-03 15:13:45 -07003630 goto cont;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631 if ((idev = in6_dev_get(dev)) == NULL)
Pavel Emelianov7562f872007-05-03 15:13:45 -07003632 goto cont;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003633 err = inet6_fill_ifinfo(skb, idev, NETLINK_CB(cb->skb).pid,
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07003634 cb->nlh->nlmsg_seq, RTM_NEWLINK, NLM_F_MULTI);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635 in6_dev_put(idev);
3636 if (err <= 0)
3637 break;
Pavel Emelianov7562f872007-05-03 15:13:45 -07003638cont:
3639 idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640 }
3641 read_unlock(&dev_base_lock);
3642 cb->args[0] = idx;
3643
3644 return skb->len;
3645}
3646
3647void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
3648{
3649 struct sk_buff *skb;
Thomas Graf8d7a76c2006-08-15 00:35:47 -07003650 int err = -ENOBUFS;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003651
Thomas Graf339bf982006-11-10 14:10:15 -08003652 skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC);
Thomas Graf8d7a76c2006-08-15 00:35:47 -07003653 if (skb == NULL)
3654 goto errout;
3655
3656 err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08003657 if (err < 0) {
3658 /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */
3659 WARN_ON(err == -EMSGSIZE);
3660 kfree_skb(skb);
3661 goto errout;
3662 }
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003663 err = rtnl_notify(skb, &init_net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
Thomas Graf8d7a76c2006-08-15 00:35:47 -07003664errout:
3665 if (err < 0)
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003666 rtnl_set_sk_err(&init_net, RTNLGRP_IPV6_IFADDR, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667}
3668
Thomas Graf339bf982006-11-10 14:10:15 -08003669static inline size_t inet6_prefix_nlmsg_size(void)
3670{
3671 return NLMSG_ALIGN(sizeof(struct prefixmsg))
3672 + nla_total_size(sizeof(struct in6_addr))
3673 + nla_total_size(sizeof(struct prefix_cacheinfo));
3674}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07003675
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
Thomas Graf6051e2f2006-11-14 19:54:19 -08003677 struct prefix_info *pinfo, u32 pid, u32 seq,
3678 int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679{
Thomas Graf6051e2f2006-11-14 19:54:19 -08003680 struct prefixmsg *pmsg;
3681 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682 struct prefix_cacheinfo ci;
3683
Thomas Graf6051e2f2006-11-14 19:54:19 -08003684 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*pmsg), flags);
3685 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08003686 return -EMSGSIZE;
Thomas Graf6051e2f2006-11-14 19:54:19 -08003687
3688 pmsg = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689 pmsg->prefix_family = AF_INET6;
Patrick McHardy8a470772005-06-28 12:56:45 -07003690 pmsg->prefix_pad1 = 0;
3691 pmsg->prefix_pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692 pmsg->prefix_ifindex = idev->dev->ifindex;
3693 pmsg->prefix_len = pinfo->prefix_len;
3694 pmsg->prefix_type = pinfo->type;
Patrick McHardy8a470772005-06-28 12:56:45 -07003695 pmsg->prefix_pad3 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696 pmsg->prefix_flags = 0;
3697 if (pinfo->onlink)
3698 pmsg->prefix_flags |= IF_PREFIX_ONLINK;
3699 if (pinfo->autoconf)
3700 pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
3701
Thomas Graf6051e2f2006-11-14 19:54:19 -08003702 NLA_PUT(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703
3704 ci.preferred_time = ntohl(pinfo->prefered);
3705 ci.valid_time = ntohl(pinfo->valid);
Thomas Graf6051e2f2006-11-14 19:54:19 -08003706 NLA_PUT(skb, PREFIX_CACHEINFO, sizeof(ci), &ci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707
Thomas Graf6051e2f2006-11-14 19:54:19 -08003708 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709
Thomas Graf6051e2f2006-11-14 19:54:19 -08003710nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08003711 nlmsg_cancel(skb, nlh);
3712 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713}
3714
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003715static void inet6_prefix_notify(int event, struct inet6_dev *idev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716 struct prefix_info *pinfo)
3717{
3718 struct sk_buff *skb;
Thomas Graf8c384bfa2006-08-15 00:36:07 -07003719 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720
Thomas Graf339bf982006-11-10 14:10:15 -08003721 skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC);
Thomas Graf8c384bfa2006-08-15 00:36:07 -07003722 if (skb == NULL)
3723 goto errout;
3724
3725 err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08003726 if (err < 0) {
3727 /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */
3728 WARN_ON(err == -EMSGSIZE);
3729 kfree_skb(skb);
3730 goto errout;
3731 }
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003732 err = rtnl_notify(skb, &init_net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC);
Thomas Graf8c384bfa2006-08-15 00:36:07 -07003733errout:
3734 if (err < 0)
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003735 rtnl_set_sk_err(&init_net, RTNLGRP_IPV6_PREFIX, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736}
3737
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
3739{
3740 inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
3741
3742 switch (event) {
3743 case RTM_NEWADDR:
Neil Horman95c385b2007-04-25 17:08:10 -07003744 /*
3745 * If the address was optimistic
3746 * we inserted the route at the start of
3747 * our DAD process, so we don't need
3748 * to do it again
3749 */
3750 if (!(ifp->rt->rt6i_node))
3751 ip6_ins_rt(ifp->rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752 if (ifp->idev->cnf.forwarding)
3753 addrconf_join_anycast(ifp);
3754 break;
3755 case RTM_DELADDR:
3756 if (ifp->idev->cnf.forwarding)
3757 addrconf_leave_anycast(ifp);
3758 addrconf_leave_solict(ifp->idev, &ifp->addr);
3759 dst_hold(&ifp->rt->u.dst);
Thomas Grafe0a1ad732006-08-22 00:00:21 -07003760 if (ip6_del_rt(ifp->rt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761 dst_free(&ifp->rt->u.dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762 break;
3763 }
3764}
3765
3766static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
3767{
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07003768 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769 if (likely(ifp->idev->dead == 0))
3770 __ipv6_ifa_notify(event, ifp);
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07003771 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772}
3773
3774#ifdef CONFIG_SYSCTL
3775
3776static
3777int addrconf_sysctl_forward(ctl_table *ctl, int write, struct file * filp,
3778 void __user *buffer, size_t *lenp, loff_t *ppos)
3779{
3780 int *valp = ctl->data;
3781 int val = *valp;
3782 int ret;
3783
3784 ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
3785
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -08003786 if (write)
3787 addrconf_fixup_forwarding(ctl, valp, val);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003788 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789}
3790
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003791static int addrconf_sysctl_forward_strategy(ctl_table *table,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792 int __user *name, int nlen,
3793 void __user *oldval,
3794 size_t __user *oldlenp,
Alexey Dobriyan1f29bcd2006-12-10 02:19:10 -08003795 void __user *newval, size_t newlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796{
3797 int *valp = table->data;
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -08003798 int val = *valp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799 int new;
3800
3801 if (!newval || !newlen)
3802 return 0;
3803 if (newlen != sizeof(int))
3804 return -EINVAL;
3805 if (get_user(new, (int __user *)newval))
3806 return -EFAULT;
3807 if (new == *valp)
3808 return 0;
3809 if (oldval && oldlenp) {
3810 size_t len;
3811 if (get_user(len, oldlenp))
3812 return -EFAULT;
3813 if (len) {
3814 if (len > table->maxlen)
3815 len = table->maxlen;
3816 if (copy_to_user(oldval, valp, len))
3817 return -EFAULT;
3818 if (put_user(len, oldlenp))
3819 return -EFAULT;
3820 }
3821 }
3822
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -08003823 *valp = new;
3824 addrconf_fixup_forwarding(table, valp, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825 return 1;
3826}
3827
3828static struct addrconf_sysctl_table
3829{
3830 struct ctl_table_header *sysctl_header;
3831 ctl_table addrconf_vars[__NET_IPV6_MAX];
Pavel Emelyanov1dab6222007-12-02 00:59:38 +11003832 char *dev_name;
Brian Haleyab32ea52006-09-22 14:15:41 -07003833} addrconf_sysctl __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834 .sysctl_header = NULL,
3835 .addrconf_vars = {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003836 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837 .ctl_name = NET_IPV6_FORWARDING,
3838 .procname = "forwarding",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003839 .data = &ipv6_devconf.forwarding,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003840 .maxlen = sizeof(int),
3841 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003842 .proc_handler = &addrconf_sysctl_forward,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843 .strategy = &addrconf_sysctl_forward_strategy,
3844 },
3845 {
3846 .ctl_name = NET_IPV6_HOP_LIMIT,
3847 .procname = "hop_limit",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003848 .data = &ipv6_devconf.hop_limit,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849 .maxlen = sizeof(int),
3850 .mode = 0644,
3851 .proc_handler = proc_dointvec,
3852 },
3853 {
3854 .ctl_name = NET_IPV6_MTU,
3855 .procname = "mtu",
3856 .data = &ipv6_devconf.mtu6,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003857 .maxlen = sizeof(int),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003859 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003860 },
3861 {
3862 .ctl_name = NET_IPV6_ACCEPT_RA,
3863 .procname = "accept_ra",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003864 .data = &ipv6_devconf.accept_ra,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865 .maxlen = sizeof(int),
3866 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003867 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003868 },
3869 {
3870 .ctl_name = NET_IPV6_ACCEPT_REDIRECTS,
3871 .procname = "accept_redirects",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003872 .data = &ipv6_devconf.accept_redirects,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873 .maxlen = sizeof(int),
3874 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003875 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876 },
3877 {
3878 .ctl_name = NET_IPV6_AUTOCONF,
3879 .procname = "autoconf",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003880 .data = &ipv6_devconf.autoconf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881 .maxlen = sizeof(int),
3882 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003883 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884 },
3885 {
3886 .ctl_name = NET_IPV6_DAD_TRANSMITS,
3887 .procname = "dad_transmits",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003888 .data = &ipv6_devconf.dad_transmits,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889 .maxlen = sizeof(int),
3890 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003891 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892 },
3893 {
3894 .ctl_name = NET_IPV6_RTR_SOLICITS,
3895 .procname = "router_solicitations",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003896 .data = &ipv6_devconf.rtr_solicits,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003897 .maxlen = sizeof(int),
3898 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003899 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900 },
3901 {
3902 .ctl_name = NET_IPV6_RTR_SOLICIT_INTERVAL,
3903 .procname = "router_solicitation_interval",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003904 .data = &ipv6_devconf.rtr_solicit_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003905 .maxlen = sizeof(int),
3906 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003907 .proc_handler = &proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908 .strategy = &sysctl_jiffies,
3909 },
3910 {
3911 .ctl_name = NET_IPV6_RTR_SOLICIT_DELAY,
3912 .procname = "router_solicitation_delay",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003913 .data = &ipv6_devconf.rtr_solicit_delay,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914 .maxlen = sizeof(int),
3915 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003916 .proc_handler = &proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917 .strategy = &sysctl_jiffies,
3918 },
3919 {
3920 .ctl_name = NET_IPV6_FORCE_MLD_VERSION,
3921 .procname = "force_mld_version",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003922 .data = &ipv6_devconf.force_mld_version,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923 .maxlen = sizeof(int),
3924 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003925 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926 },
3927#ifdef CONFIG_IPV6_PRIVACY
3928 {
3929 .ctl_name = NET_IPV6_USE_TEMPADDR,
3930 .procname = "use_tempaddr",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003931 .data = &ipv6_devconf.use_tempaddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932 .maxlen = sizeof(int),
3933 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003934 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935 },
3936 {
3937 .ctl_name = NET_IPV6_TEMP_VALID_LFT,
3938 .procname = "temp_valid_lft",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003939 .data = &ipv6_devconf.temp_valid_lft,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940 .maxlen = sizeof(int),
3941 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003942 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003943 },
3944 {
3945 .ctl_name = NET_IPV6_TEMP_PREFERED_LFT,
3946 .procname = "temp_prefered_lft",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003947 .data = &ipv6_devconf.temp_prefered_lft,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948 .maxlen = sizeof(int),
3949 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003950 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951 },
3952 {
3953 .ctl_name = NET_IPV6_REGEN_MAX_RETRY,
3954 .procname = "regen_max_retry",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003955 .data = &ipv6_devconf.regen_max_retry,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956 .maxlen = sizeof(int),
3957 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003958 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959 },
3960 {
3961 .ctl_name = NET_IPV6_MAX_DESYNC_FACTOR,
3962 .procname = "max_desync_factor",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003963 .data = &ipv6_devconf.max_desync_factor,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964 .maxlen = sizeof(int),
3965 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003966 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967 },
3968#endif
3969 {
3970 .ctl_name = NET_IPV6_MAX_ADDRESSES,
3971 .procname = "max_addresses",
3972 .data = &ipv6_devconf.max_addresses,
3973 .maxlen = sizeof(int),
3974 .mode = 0644,
3975 .proc_handler = &proc_dointvec,
3976 },
3977 {
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -08003978 .ctl_name = NET_IPV6_ACCEPT_RA_DEFRTR,
3979 .procname = "accept_ra_defrtr",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003980 .data = &ipv6_devconf.accept_ra_defrtr,
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -08003981 .maxlen = sizeof(int),
3982 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003983 .proc_handler = &proc_dointvec,
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -08003984 },
3985 {
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -08003986 .ctl_name = NET_IPV6_ACCEPT_RA_PINFO,
3987 .procname = "accept_ra_pinfo",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003988 .data = &ipv6_devconf.accept_ra_pinfo,
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -08003989 .maxlen = sizeof(int),
3990 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003991 .proc_handler = &proc_dointvec,
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -08003992 },
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08003993#ifdef CONFIG_IPV6_ROUTER_PREF
3994 {
3995 .ctl_name = NET_IPV6_ACCEPT_RA_RTR_PREF,
3996 .procname = "accept_ra_rtr_pref",
3997 .data = &ipv6_devconf.accept_ra_rtr_pref,
3998 .maxlen = sizeof(int),
3999 .mode = 0644,
4000 .proc_handler = &proc_dointvec,
4001 },
YOSHIFUJI Hideaki52e16352006-03-20 17:05:47 -08004002 {
4003 .ctl_name = NET_IPV6_RTR_PROBE_INTERVAL,
4004 .procname = "router_probe_interval",
4005 .data = &ipv6_devconf.rtr_probe_interval,
4006 .maxlen = sizeof(int),
4007 .mode = 0644,
4008 .proc_handler = &proc_dointvec_jiffies,
4009 .strategy = &sysctl_jiffies,
4010 },
Neil Hormanfa03ef32007-01-30 14:30:10 -08004011#ifdef CONFIG_IPV6_ROUTE_INFO
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -08004012 {
4013 .ctl_name = NET_IPV6_ACCEPT_RA_RT_INFO_MAX_PLEN,
4014 .procname = "accept_ra_rt_info_max_plen",
4015 .data = &ipv6_devconf.accept_ra_rt_info_max_plen,
4016 .maxlen = sizeof(int),
4017 .mode = 0644,
4018 .proc_handler = &proc_dointvec,
4019 },
4020#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08004021#endif
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -08004022 {
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -07004023 .ctl_name = NET_IPV6_PROXY_NDP,
4024 .procname = "proxy_ndp",
4025 .data = &ipv6_devconf.proxy_ndp,
4026 .maxlen = sizeof(int),
4027 .mode = 0644,
4028 .proc_handler = &proc_dointvec,
4029 },
4030 {
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -07004031 .ctl_name = NET_IPV6_ACCEPT_SOURCE_ROUTE,
4032 .procname = "accept_source_route",
4033 .data = &ipv6_devconf.accept_source_route,
4034 .maxlen = sizeof(int),
4035 .mode = 0644,
4036 .proc_handler = &proc_dointvec,
4037 },
Neil Horman95c385b2007-04-25 17:08:10 -07004038#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
4039 {
4040 .ctl_name = CTL_UNNUMBERED,
4041 .procname = "optimistic_dad",
4042 .data = &ipv6_devconf.optimistic_dad,
4043 .maxlen = sizeof(int),
4044 .mode = 0644,
4045 .proc_handler = &proc_dointvec,
4046
4047 },
4048#endif
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -07004049 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050 .ctl_name = 0, /* sentinel */
4051 }
4052 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053};
4054
Pavel Emelyanovf52295a2007-12-02 00:58:37 +11004055static void __addrconf_sysctl_register(char *dev_name, int ctl_name,
4056 struct inet6_dev *idev, struct ipv6_devconf *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057{
4058 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059 struct addrconf_sysctl_table *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060
Pavel Emelyanov1dab6222007-12-02 00:59:38 +11004061#define ADDRCONF_CTL_PATH_DEV 3
4062
4063 struct ctl_path addrconf_ctl_path[] = {
4064 { .procname = "net", .ctl_name = CTL_NET, },
4065 { .procname = "ipv6", .ctl_name = NET_IPV6, },
4066 { .procname = "conf", .ctl_name = NET_IPV6_CONF, },
4067 { /* to be set */ },
4068 { },
4069 };
4070
4071
Arnaldo Carvalho de Meloaf879cc2006-11-17 12:14:37 -02004072 t = kmemdup(&addrconf_sysctl, sizeof(*t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073 if (t == NULL)
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11004074 goto out;
4075
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076 for (i=0; t->addrconf_vars[i].data; i++) {
4077 t->addrconf_vars[i].data += (char*)p - (char*)&ipv6_devconf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078 t->addrconf_vars[i].extra1 = idev; /* embedded; no ref */
4079 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004081 /*
4082 * Make a copy of dev_name, because '.procname' is regarded as const
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083 * by sysctl and we wouldn't want anyone to change it under our feet
4084 * (see SIOCSIFNAME).
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004085 */
Pavel Emelyanov1dab6222007-12-02 00:59:38 +11004086 t->dev_name = kstrdup(dev_name, GFP_KERNEL);
4087 if (!t->dev_name)
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11004088 goto free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089
Pavel Emelyanov1dab6222007-12-02 00:59:38 +11004090 addrconf_ctl_path[ADDRCONF_CTL_PATH_DEV].procname = t->dev_name;
4091 addrconf_ctl_path[ADDRCONF_CTL_PATH_DEV].ctl_name = ctl_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092
Pavel Emelyanov1dab6222007-12-02 00:59:38 +11004093 t->sysctl_header = register_sysctl_paths(addrconf_ctl_path,
4094 t->addrconf_vars);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095 if (t->sysctl_header == NULL)
4096 goto free_procname;
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11004097
4098 p->sysctl = t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099 return;
4100
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11004101free_procname:
Pavel Emelyanov1dab6222007-12-02 00:59:38 +11004102 kfree(t->dev_name);
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11004103free:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104 kfree(t);
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11004105out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106 return;
4107}
4108
Pavel Emelyanovf52295a2007-12-02 00:58:37 +11004109static void addrconf_sysctl_register(struct inet6_dev *idev)
4110{
4111 __addrconf_sysctl_register(idev->dev->name, idev->dev->ifindex,
4112 idev, &idev->cnf);
4113}
4114
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115static void addrconf_sysctl_unregister(struct ipv6_devconf *p)
4116{
4117 if (p->sysctl) {
4118 struct addrconf_sysctl_table *t = p->sysctl;
4119 p->sysctl = NULL;
4120 unregister_sysctl_table(t->sysctl_header);
Pavel Emelyanov1dab6222007-12-02 00:59:38 +11004121 kfree(t->dev_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122 kfree(t);
4123 }
4124}
4125
4126
4127#endif
4128
4129/*
4130 * Device notifier
4131 */
4132
4133int register_inet6addr_notifier(struct notifier_block *nb)
4134{
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004135 return atomic_notifier_chain_register(&inet6addr_chain, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136}
4137
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +09004138EXPORT_SYMBOL(register_inet6addr_notifier);
4139
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140int unregister_inet6addr_notifier(struct notifier_block *nb)
4141{
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004142 return atomic_notifier_chain_unregister(&inet6addr_chain,nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143}
4144
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +09004145EXPORT_SYMBOL(unregister_inet6addr_notifier);
4146
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147/*
4148 * Init / cleanup code
4149 */
4150
4151int __init addrconf_init(void)
4152{
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +09004153 int err;
4154
4155 if ((err = ipv6_addr_label_init()) < 0) {
4156 printk(KERN_CRIT "IPv6 Addrconf: cannot initialize default policy table: %d.\n",
4157 err);
4158 return err;
4159 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160
4161 /* The addrconf netdev notifier requires that loopback_dev
4162 * has it's ipv6 private information allocated and setup
4163 * before it can bring up and give link-local addresses
4164 * to other devices which are up.
4165 *
4166 * Unfortunately, loopback_dev is not necessarily the first
4167 * entry in the global dev_base list of net devices. In fact,
4168 * it is likely to be the very last entry on that list.
4169 * So this causes the notifier registry below to try and
4170 * give link-local addresses to all devices besides loopback_dev
4171 * first, then loopback_dev, which cases all the non-loopback_dev
4172 * devices to fail to get a link-local address.
4173 *
4174 * So, as a temporary fix, allocate the ipv6 structure for
4175 * loopback_dev first by hand.
4176 * Longer term, all of the dependencies ipv6 has upon the loopback
4177 * device and it being up should be removed.
4178 */
4179 rtnl_lock();
Eric W. Biederman2774c7a2007-09-26 22:10:56 -07004180 if (!ipv6_add_dev(init_net.loopback_dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181 err = -ENOMEM;
4182 rtnl_unlock();
4183 if (err)
4184 return err;
4185
Eric W. Biederman2774c7a2007-09-26 22:10:56 -07004186 ip6_null_entry.u.dst.dev = init_net.loopback_dev;
4187 ip6_null_entry.rt6i_idev = in6_dev_get(init_net.loopback_dev);
YOSHIFUJI Hideaki9a6bf6f2007-05-09 14:03:28 -07004188#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Eric W. Biederman2774c7a2007-09-26 22:10:56 -07004189 ip6_prohibit_entry.u.dst.dev = init_net.loopback_dev;
4190 ip6_prohibit_entry.rt6i_idev = in6_dev_get(init_net.loopback_dev);
4191 ip6_blk_hole_entry.u.dst.dev = init_net.loopback_dev;
4192 ip6_blk_hole_entry.rt6i_idev = in6_dev_get(init_net.loopback_dev);
YOSHIFUJI Hideaki9a6bf6f2007-05-09 14:03:28 -07004193#endif
Herbert Xuc62dba92005-09-26 15:10:16 -07004194
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195 register_netdevice_notifier(&ipv6_dev_notf);
4196
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197 addrconf_verify(0);
Thomas Grafc127ea22007-03-22 11:58:32 -07004198
4199 err = __rtnl_register(PF_INET6, RTM_GETLINK, NULL, inet6_dump_ifinfo);
4200 if (err < 0)
4201 goto errout;
4202
4203 /* Only the first call to __rtnl_register can fail */
4204 __rtnl_register(PF_INET6, RTM_NEWADDR, inet6_rtm_newaddr, NULL);
4205 __rtnl_register(PF_INET6, RTM_DELADDR, inet6_rtm_deladdr, NULL);
4206 __rtnl_register(PF_INET6, RTM_GETADDR, inet6_rtm_getaddr, inet6_dump_ifaddr);
4207 __rtnl_register(PF_INET6, RTM_GETMULTICAST, NULL, inet6_dump_ifmcaddr);
4208 __rtnl_register(PF_INET6, RTM_GETANYCAST, NULL, inet6_dump_ifacaddr);
4209
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +09004210 ipv6_addr_label_rtnl_register();
4211
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212#ifdef CONFIG_SYSCTL
Pavel Emelyanovf52295a2007-12-02 00:58:37 +11004213 __addrconf_sysctl_register("all", NET_PROTO_CONF_ALL,
4214 NULL, &ipv6_devconf);
4215 __addrconf_sysctl_register("default", NET_PROTO_CONF_DEFAULT,
4216 NULL, &ipv6_devconf_dflt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217#endif
4218
4219 return 0;
Thomas Grafc127ea22007-03-22 11:58:32 -07004220errout:
4221 unregister_netdevice_notifier(&ipv6_dev_notf);
4222
4223 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224}
4225
Daniel Lezcano09f77092007-12-13 05:34:58 -08004226void addrconf_cleanup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227{
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004228 struct net_device *dev;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004229 struct inet6_ifaddr *ifa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230 int i;
4231
4232 unregister_netdevice_notifier(&ipv6_dev_notf);
4233
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234#ifdef CONFIG_SYSCTL
4235 addrconf_sysctl_unregister(&ipv6_devconf_dflt);
4236 addrconf_sysctl_unregister(&ipv6_devconf);
4237#endif
4238
4239 rtnl_lock();
4240
4241 /*
4242 * clean dev list.
4243 */
4244
Eric W. Biederman881d9662007-09-17 11:56:21 -07004245 for_each_netdev(&init_net, dev) {
Micah Gruberdffe4f02007-07-10 23:04:19 -07004246 if (__in6_dev_get(dev) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247 continue;
4248 addrconf_ifdown(dev, 1);
4249 }
Eric W. Biederman2774c7a2007-09-26 22:10:56 -07004250 addrconf_ifdown(init_net.loopback_dev, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251
4252 /*
4253 * Check hash table.
4254 */
4255
4256 write_lock_bh(&addrconf_hash_lock);
4257 for (i=0; i < IN6_ADDR_HSIZE; i++) {
4258 for (ifa=inet6_addr_lst[i]; ifa; ) {
4259 struct inet6_ifaddr *bifa;
4260
4261 bifa = ifa;
4262 ifa = ifa->lst_next;
4263 printk(KERN_DEBUG "bug: IPv6 address leakage detected: ifa=%p\n", bifa);
4264 /* Do not free it; something is wrong.
4265 Now we can investigate it with debugger.
4266 */
4267 }
4268 }
4269 write_unlock_bh(&addrconf_hash_lock);
4270
4271 del_timer(&addr_chk_timer);
4272
4273 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274}