blob: c3b5e1952db185330cd6bb564ec1a9d9b9150a69 [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 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version
12 * 2 of the License, or (at your option) any later version.
13 */
14
15/*
16 * Changes:
17 *
18 * Janos Farkas : delete timer on ifdown
19 * <chexum@bankinf.banki.hu>
20 * Andi Kleen : kill double kfree on module
21 * unload.
22 * Maciej W. Rozycki : FDDI support
23 * sekiya@USAGI : Don't send too many RS
24 * packets.
25 * yoshfuji@USAGI : Fixed interval between DAD
26 * packets.
27 * YOSHIFUJI Hideaki @USAGI : improved accuracy of
28 * address validation timer.
29 * YOSHIFUJI Hideaki @USAGI : Privacy Extensions (RFC3041)
30 * support.
31 * Yuji SEKIYA @USAGI : Don't assign a same IPv6
32 * address on a same interface.
33 * YOSHIFUJI Hideaki @USAGI : ARCnet support
34 * YOSHIFUJI Hideaki @USAGI : convert /proc/net/if_inet6 to
35 * seq_file.
YOSHIFUJI Hideakib1cacb62005-11-08 09:38:12 -080036 * YOSHIFUJI Hideaki @USAGI : improved source address
37 * selection; consider scope,
38 * status etc.
Harout Hedeshian8b2414d2012-12-21 12:38:38 -080039 * Harout S. Hedeshian : procfs flag to toggle automatic
40 * addition of prefix route
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>
Ilpo Järvinena0bffff2009-03-21 13:36:17 -070045#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <linux/socket.h>
47#include <linux/sockios.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/net.h>
49#include <linux/in6.h>
50#include <linux/netdevice.h>
Thomas Graf18237302006-08-04 23:04:54 -070051#include <linux/if_addr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <linux/if_arp.h>
53#include <linux/if_arcnet.h>
54#include <linux/if_infiniband.h>
55#include <linux/route.h>
56#include <linux/inetdevice.h>
57#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090058#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#ifdef CONFIG_SYSCTL
60#include <linux/sysctl.h>
61#endif
Randy Dunlap4fc268d2006-01-11 12:17:47 -080062#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#include <linux/delay.h>
64#include <linux/notifier.h>
Paulo Marques543537b2005-06-23 00:09:02 -070065#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020067#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#include <net/sock.h>
69#include <net/snmp.h>
70
71#include <net/ipv6.h>
72#include <net/protocol.h>
73#include <net/ndisc.h>
74#include <net/ip6_route.h>
75#include <net/addrconf.h>
76#include <net/tcp.h>
77#include <net/ip.h>
Thomas Graf5d620262006-08-15 00:35:02 -070078#include <net/netlink.h>
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -070079#include <net/pkt_sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070080#include <linux/if_tunnel.h>
81#include <linux/rtnetlink.h>
82
83#ifdef CONFIG_IPV6_PRIVACY
84#include <linux/random.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070085#endif
86
Stephen Hemmingere21e8462010-03-20 16:09:01 -070087#include <linux/uaccess.h>
Herbert Xu7f7d9a62007-04-24 21:54:09 -070088#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90#include <linux/proc_fs.h>
91#include <linux/seq_file.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040092#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
94/* Set to 3 to get tracing... */
95#define ACONF_DEBUG 2
96
97#if ACONF_DEBUG >= 3
98#define ADBG(x) printk x
99#else
100#define ADBG(x)
101#endif
102
103#define INFINITY_LIFE_TIME 0xFFFFFFFF
Thomas Graf18a31e12010-11-17 04:12:02 +0000104
105static inline u32 cstamp_delta(unsigned long cstamp)
106{
107 return (cstamp - INITIAL_JIFFIES) * 100UL / HZ;
108}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -0700110#define ADDRCONF_TIMER_FUZZ_MINUS (HZ > 50 ? HZ/50 : 1)
111#define ADDRCONF_TIMER_FUZZ (HZ / 4)
112#define ADDRCONF_TIMER_FUZZ_MAX (HZ)
113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114#ifdef CONFIG_SYSCTL
Pavel Emelyanovf52295a2007-12-02 00:58:37 +1100115static void addrconf_sysctl_register(struct inet6_dev *idev);
Pavel Emelyanov408c4762008-01-10 17:41:21 -0800116static void addrconf_sysctl_unregister(struct inet6_dev *idev);
117#else
118static inline void addrconf_sysctl_register(struct inet6_dev *idev)
119{
120}
121
122static inline void addrconf_sysctl_unregister(struct inet6_dev *idev)
123{
124}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125#endif
126
127#ifdef CONFIG_IPV6_PRIVACY
128static int __ipv6_regen_rndid(struct inet6_dev *idev);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900129static int __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130static void ipv6_regen_rndid(unsigned long data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131#endif
132
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +0900133static int ipv6_generate_eui64(u8 *eui, struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134static int ipv6_count_addresses(struct inet6_dev *idev);
135
136/*
137 * Configured unicast address hash table
138 */
stephen hemmingerc2e21292010-03-17 20:31:10 +0000139static struct hlist_head inet6_addr_lst[IN6_ADDR_HSIZE];
stephen hemminger5c578ae2010-03-17 20:31:11 +0000140static DEFINE_SPINLOCK(addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142static void addrconf_verify(unsigned long);
143
Ingo Molnar8d06afa2005-09-09 13:10:40 -0700144static DEFINE_TIMER(addr_chk_timer, addrconf_verify, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145static DEFINE_SPINLOCK(addrconf_verify_lock);
146
147static void addrconf_join_anycast(struct inet6_ifaddr *ifp);
148static void addrconf_leave_anycast(struct inet6_ifaddr *ifp);
149
Jiri Pirko93d9b7d2010-03-10 10:28:56 +0000150static void addrconf_type_change(struct net_device *dev,
151 unsigned long event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152static int addrconf_ifdown(struct net_device *dev, int how);
153
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -0700154static void addrconf_dad_start(struct inet6_ifaddr *ifp, u32 flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155static void addrconf_dad_timer(unsigned long data);
156static void addrconf_dad_completed(struct inet6_ifaddr *ifp);
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +0900157static void addrconf_dad_run(struct inet6_dev *idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158static void addrconf_rs_timer(unsigned long data);
159static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
160static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
161
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900162static void inet6_prefix_notify(int event, struct inet6_dev *idev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 struct prefix_info *pinfo);
David S. Miller3e81c6d2010-03-20 16:18:00 -0700164static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
165 struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Alan Sterne041c682006-03-27 01:16:30 -0800167static ATOMIC_NOTIFIER_HEAD(inet6addr_chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Adrian Bunk888c8482008-07-22 14:21:58 -0700169static struct ipv6_devconf ipv6_devconf __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 .forwarding = 0,
171 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
172 .mtu6 = IPV6_MIN_MTU,
173 .accept_ra = 1,
174 .accept_redirects = 1,
175 .autoconf = 1,
176 .force_mld_version = 0,
177 .dad_transmits = 1,
178 .rtr_solicits = MAX_RTR_SOLICITATIONS,
179 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
180 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
181#ifdef CONFIG_IPV6_PRIVACY
182 .use_tempaddr = 0,
183 .temp_valid_lft = TEMP_VALID_LIFETIME,
184 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
185 .regen_max_retry = REGEN_MAX_RETRY,
186 .max_desync_factor = MAX_DESYNC_FACTOR,
187#endif
188 .max_addresses = IPV6_MAX_ADDRESSES,
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -0800189 .accept_ra_defrtr = 1,
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -0800190 .accept_ra_pinfo = 1,
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800191#ifdef CONFIG_IPV6_ROUTER_PREF
192 .accept_ra_rtr_pref = 1,
YOSHIFUJI Hideaki52e16352006-03-20 17:05:47 -0800193 .rtr_probe_interval = 60 * HZ,
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -0800194#ifdef CONFIG_IPV6_ROUTE_INFO
195 .accept_ra_rt_info_max_plen = 0,
196#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800197#endif
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -0700198 .proxy_ndp = 0,
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -0700199 .accept_source_route = 0, /* we do not accept RH0 by default. */
YOSHIFUJI Hideaki778d80b2008-06-28 14:17:11 +0900200 .disable_ipv6 = 0,
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +0900201 .accept_dad = 1,
Harout Hedeshian8b2414d2012-12-21 12:38:38 -0800202 .accept_ra_prefix_route = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203};
204
Brian Haleyab32ea52006-09-22 14:15:41 -0700205static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 .forwarding = 0,
207 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
208 .mtu6 = IPV6_MIN_MTU,
209 .accept_ra = 1,
210 .accept_redirects = 1,
211 .autoconf = 1,
212 .dad_transmits = 1,
213 .rtr_solicits = MAX_RTR_SOLICITATIONS,
214 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
215 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
216#ifdef CONFIG_IPV6_PRIVACY
217 .use_tempaddr = 0,
218 .temp_valid_lft = TEMP_VALID_LIFETIME,
219 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
220 .regen_max_retry = REGEN_MAX_RETRY,
221 .max_desync_factor = MAX_DESYNC_FACTOR,
222#endif
223 .max_addresses = IPV6_MAX_ADDRESSES,
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -0800224 .accept_ra_defrtr = 1,
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -0800225 .accept_ra_pinfo = 1,
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800226#ifdef CONFIG_IPV6_ROUTER_PREF
227 .accept_ra_rtr_pref = 1,
YOSHIFUJI Hideaki52e16352006-03-20 17:05:47 -0800228 .rtr_probe_interval = 60 * HZ,
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -0800229#ifdef CONFIG_IPV6_ROUTE_INFO
230 .accept_ra_rt_info_max_plen = 0,
231#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800232#endif
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -0700233 .proxy_ndp = 0,
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -0700234 .accept_source_route = 0, /* we do not accept RH0 by default. */
YOSHIFUJI Hideaki778d80b2008-06-28 14:17:11 +0900235 .disable_ipv6 = 0,
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +0900236 .accept_dad = 1,
Harout Hedeshian8b2414d2012-12-21 12:38:38 -0800237 .accept_ra_prefix_route = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238};
239
240/* IPv6 Wildcard Address and Loopback Address defined by RFC2553 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +0900243const struct in6_addr in6addr_linklocal_allnodes = IN6ADDR_LINKLOCAL_ALLNODES_INIT;
244const struct in6_addr in6addr_linklocal_allrouters = IN6ADDR_LINKLOCAL_ALLROUTERS_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -0700246/* Check if a valid qdisc is available */
David S. Miller05297942008-07-08 23:01:27 -0700247static inline bool addrconf_qdisc_ok(const struct net_device *dev)
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -0700248{
David S. Miller05297942008-07-08 23:01:27 -0700249 return !qdisc_tx_is_noop(dev);
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -0700250}
251
YOSHIFUJI Hideaki2b5ead42008-05-13 01:16:24 +0900252/* Check if a route is valid prefix route */
253static inline int addrconf_is_prefix_route(const struct rt6_info *rt)
254{
Eric Dumazeta02cec22010-09-22 20:43:57 +0000255 return (rt->rt6i_flags & (RTF_GATEWAY | RTF_DEFAULT)) == 0;
YOSHIFUJI Hideaki2b5ead42008-05-13 01:16:24 +0900256}
257
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258static void addrconf_del_timer(struct inet6_ifaddr *ifp)
259{
260 if (del_timer(&ifp->timer))
261 __in6_ifa_put(ifp);
262}
263
Stephen Hemmingere21e8462010-03-20 16:09:01 -0700264enum addrconf_timer_t {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 AC_NONE,
266 AC_DAD,
267 AC_RS,
268};
269
270static void addrconf_mod_timer(struct inet6_ifaddr *ifp,
271 enum addrconf_timer_t what,
272 unsigned long when)
273{
274 if (!del_timer(&ifp->timer))
275 in6_ifa_hold(ifp);
276
277 switch (what) {
278 case AC_DAD:
279 ifp->timer.function = addrconf_dad_timer;
280 break;
281 case AC_RS:
282 ifp->timer.function = addrconf_rs_timer;
283 break;
Stephen Hemmingere21e8462010-03-20 16:09:01 -0700284 default:
285 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 }
287 ifp->timer.expires = jiffies + when;
288 add_timer(&ifp->timer);
289}
290
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700291static int snmp6_alloc_dev(struct inet6_dev *idev)
292{
Tejun Heo7d720c32010-02-16 15:20:26 +0000293 if (snmp_mib_init((void __percpu **)idev->stats.ipv6,
Eric Dumazet1823e4c82010-06-22 20:58:41 +0000294 sizeof(struct ipstats_mib),
295 __alignof__(struct ipstats_mib)) < 0)
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700296 goto err_ip;
Eric Dumazetbe281e52011-05-19 01:14:23 +0000297 idev->stats.icmpv6dev = kzalloc(sizeof(struct icmpv6_mib_device),
298 GFP_KERNEL);
299 if (!idev->stats.icmpv6dev)
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700300 goto err_icmp;
Eric Dumazetbe281e52011-05-19 01:14:23 +0000301 idev->stats.icmpv6msgdev = kzalloc(sizeof(struct icmpv6msg_mib_device),
302 GFP_KERNEL);
303 if (!idev->stats.icmpv6msgdev)
David L Stevens14878f72007-09-16 16:52:35 -0700304 goto err_icmpmsg;
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700305
306 return 0;
307
David L Stevens14878f72007-09-16 16:52:35 -0700308err_icmpmsg:
Eric Dumazetbe281e52011-05-19 01:14:23 +0000309 kfree(idev->stats.icmpv6dev);
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700310err_icmp:
Tejun Heo7d720c32010-02-16 15:20:26 +0000311 snmp_mib_free((void __percpu **)idev->stats.ipv6);
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700312err_ip:
Pavel Emelyanovaaf70ec2007-10-17 21:25:32 -0700313 return -ENOMEM;
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700314}
315
Pavel Emelyanov16910b92007-10-17 21:23:43 -0700316static void snmp6_free_dev(struct inet6_dev *idev)
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700317{
Eric Dumazetbe281e52011-05-19 01:14:23 +0000318 kfree(idev->stats.icmpv6msgdev);
319 kfree(idev->stats.icmpv6dev);
Tejun Heo7d720c32010-02-16 15:20:26 +0000320 snmp_mib_free((void __percpu **)idev->stats.ipv6);
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700321}
322
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323/* Nobody refers to this device, we may destroy it. */
324
325void in6_dev_finish_destroy(struct inet6_dev *idev)
326{
327 struct net_device *dev = idev->dev;
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700328
stephen hemminger502a2ff2010-03-17 20:31:13 +0000329 WARN_ON(!list_empty(&idev->addr_list));
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700330 WARN_ON(idev->mc_list != NULL);
331
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332#ifdef NET_REFCNT_DEBUG
333 printk(KERN_DEBUG "in6_dev_finish_destroy: %s\n", dev ? dev->name : "NIL");
334#endif
335 dev_put(dev);
336 if (!idev->dead) {
Stephen Hemmingere21e8462010-03-20 16:09:01 -0700337 pr_warning("Freeing alive inet6 device %p\n", idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 return;
339 }
340 snmp6_free_dev(idev);
Lai Jiangshan38f57d12011-03-15 17:59:14 +0800341 kfree_rcu(idev, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342}
343
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +0900344EXPORT_SYMBOL(in6_dev_finish_destroy);
345
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346static struct inet6_dev * ipv6_add_dev(struct net_device *dev)
347{
348 struct inet6_dev *ndev;
349
350 ASSERT_RTNL();
351
352 if (dev->mtu < IPV6_MIN_MTU)
353 return NULL;
354
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900355 ndev = kzalloc(sizeof(struct inet6_dev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900357 if (ndev == NULL)
358 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
Ingo Oeser322f74a2006-03-20 23:01:47 -0800360 rwlock_init(&ndev->lock);
361 ndev->dev = dev;
stephen hemminger502a2ff2010-03-17 20:31:13 +0000362 INIT_LIST_HEAD(&ndev->addr_list);
363
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900364 memcpy(&ndev->cnf, dev_net(dev)->ipv6.devconf_dflt, sizeof(ndev->cnf));
Ingo Oeser322f74a2006-03-20 23:01:47 -0800365 ndev->cnf.mtu6 = dev->mtu;
366 ndev->cnf.sysctl = NULL;
367 ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl);
368 if (ndev->nd_parms == NULL) {
369 kfree(ndev);
370 return NULL;
371 }
Ben Hutchings0187bdf2008-06-19 16:15:47 -0700372 if (ndev->cnf.forwarding)
373 dev_disable_lro(dev);
Ingo Oeser322f74a2006-03-20 23:01:47 -0800374 /* We refer to the device */
375 dev_hold(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Ingo Oeser322f74a2006-03-20 23:01:47 -0800377 if (snmp6_alloc_dev(ndev) < 0) {
378 ADBG((KERN_WARNING
379 "%s(): cannot allocate memory for statistics; dev=%s.\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800380 __func__, dev->name));
Ingo Oeser322f74a2006-03-20 23:01:47 -0800381 neigh_parms_release(&nd_tbl, ndev->nd_parms);
Roy Li8603e332011-09-20 15:10:16 -0400382 dev_put(dev);
383 kfree(ndev);
Ingo Oeser322f74a2006-03-20 23:01:47 -0800384 return NULL;
385 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Ingo Oeser322f74a2006-03-20 23:01:47 -0800387 if (snmp6_register_dev(ndev) < 0) {
388 ADBG((KERN_WARNING
389 "%s(): cannot create /proc/net/dev_snmp6/%s\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800390 __func__, dev->name));
Ingo Oeser322f74a2006-03-20 23:01:47 -0800391 neigh_parms_release(&nd_tbl, ndev->nd_parms);
392 ndev->dead = 1;
393 in6_dev_finish_destroy(ndev);
394 return NULL;
395 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
Ingo Oeser322f74a2006-03-20 23:01:47 -0800397 /* One reference from device. We must do this before
398 * we invoke __ipv6_regen_rndid().
399 */
400 in6_dev_hold(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +0900402 if (dev->flags & (IFF_NOARP | IFF_LOOPBACK))
403 ndev->cnf.accept_dad = -1;
404
YOSHIFUJI Hideakib077d7a2008-04-13 23:42:18 -0700405#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
406 if (dev->type == ARPHRD_SIT && (dev->priv_flags & IFF_ISATAP)) {
407 printk(KERN_INFO
408 "%s: Disabled Multicast RS\n",
409 dev->name);
410 ndev->cnf.rtr_solicits = 0;
411 }
412#endif
413
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414#ifdef CONFIG_IPV6_PRIVACY
stephen hemminger372e6c82010-03-17 20:31:09 +0000415 INIT_LIST_HEAD(&ndev->tempaddr_list);
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800416 setup_timer(&ndev->regen_timer, ipv6_regen_rndid, (unsigned long)ndev);
Ingo Oeser322f74a2006-03-20 23:01:47 -0800417 if ((dev->flags&IFF_LOOPBACK) ||
418 dev->type == ARPHRD_TUNNEL ||
YOSHIFUJI Hideaki9625ed72008-04-13 23:47:11 -0700419 dev->type == ARPHRD_TUNNEL6 ||
Joerg Roedel0be669b2006-10-10 14:49:53 -0700420 dev->type == ARPHRD_SIT ||
Joerg Roedel0be669b2006-10-10 14:49:53 -0700421 dev->type == ARPHRD_NONE) {
Ingo Oeser322f74a2006-03-20 23:01:47 -0800422 ndev->cnf.use_tempaddr = -1;
423 } else {
424 in6_dev_hold(ndev);
425 ipv6_regen_rndid((unsigned long) ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 }
Ingo Oeser322f74a2006-03-20 23:01:47 -0800427#endif
428
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -0700429 if (netif_running(dev) && addrconf_qdisc_ok(dev))
Herbert Xu53aadcc2007-03-27 14:31:52 -0700430 ndev->if_flags |= IF_READY;
431
Ingo Oeser322f74a2006-03-20 23:01:47 -0800432 ipv6_mc_init_dev(ndev);
433 ndev->tstamp = jiffies;
Pavel Emelyanovf52295a2007-12-02 00:58:37 +1100434 addrconf_sysctl_register(ndev);
David L Stevens30c4cf52007-01-04 12:31:14 -0800435 /* protected by rtnl_lock */
Eric Dumazetcf778b02012-01-12 04:41:32 +0000436 rcu_assign_pointer(dev->ip6_ptr, ndev);
YOSHIFUJI Hideakid88ae4c2007-01-14 21:48:40 -0800437
438 /* Join all-node multicast group */
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +0900439 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allnodes);
YOSHIFUJI Hideakid88ae4c2007-01-14 21:48:40 -0800440
Li Weid6ddef92012-03-05 14:45:17 +0000441 /* Join all-router multicast group if forwarding is set */
Li Wei8b2aaed2012-03-07 14:58:07 +0000442 if (ndev->cnf.forwarding && (dev->flags & IFF_MULTICAST))
Li Weid6ddef92012-03-05 14:45:17 +0000443 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
444
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 return ndev;
446}
447
YOSHIFUJI Hideakicee89472008-04-13 23:21:16 -0700448static struct inet6_dev * ipv6_find_idev(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
450 struct inet6_dev *idev;
451
452 ASSERT_RTNL();
453
Stephen Hemmingere21e8462010-03-20 16:09:01 -0700454 idev = __in6_dev_get(dev);
455 if (!idev) {
456 idev = ipv6_add_dev(dev);
457 if (!idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 return NULL;
459 }
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +0900460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 if (dev->flags&IFF_UP)
462 ipv6_mc_up(idev);
463 return idev;
464}
465
466#ifdef CONFIG_SYSCTL
467static void dev_forward_change(struct inet6_dev *idev)
468{
469 struct net_device *dev;
470 struct inet6_ifaddr *ifa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
472 if (!idev)
473 return;
474 dev = idev->dev;
Ben Hutchings0187bdf2008-06-19 16:15:47 -0700475 if (idev->cnf.forwarding)
476 dev_disable_lro(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 if (dev && (dev->flags & IFF_MULTICAST)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 if (idev->cnf.forwarding)
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +0900479 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 else
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +0900481 ipv6_dev_mc_dec(dev, &in6addr_linklocal_allrouters);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 }
stephen hemminger502a2ff2010-03-17 20:31:13 +0000483
484 list_for_each_entry(ifa, &idev->addr_list, if_list) {
Michal Wrobel2c12a742007-02-26 15:36:10 -0800485 if (ifa->flags&IFA_F_TENTATIVE)
486 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 if (idev->cnf.forwarding)
488 addrconf_join_anycast(ifa);
489 else
490 addrconf_leave_anycast(ifa);
491 }
492}
493
494
Pavel Emelyanove1869322008-01-10 17:43:50 -0800495static void addrconf_forward_change(struct net *net, __s32 newf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496{
497 struct net_device *dev;
498 struct inet6_dev *idev;
499
Ben Hutchings84a2d3c2012-08-14 08:54:51 +0000500 for_each_netdev(net, dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 idev = __in6_dev_get(dev);
502 if (idev) {
Pavel Emelyanove1869322008-01-10 17:43:50 -0800503 int changed = (!idev->cnf.forwarding) ^ (!newf);
504 idev->cnf.forwarding = newf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 if (changed)
506 dev_forward_change(idev);
507 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509}
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800510
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000511static int addrconf_fixup_forwarding(struct ctl_table *table, int *p, int newf)
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800512{
Pavel Emelyanovbff16c22008-01-10 17:42:13 -0800513 struct net *net;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000514 int old;
515
516 if (!rtnl_trylock())
517 return restart_syscall();
Pavel Emelyanovbff16c22008-01-10 17:42:13 -0800518
519 net = (struct net *)table->extra2;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000520 old = *p;
521 *p = newf;
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800522
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000523 if (p == &net->ipv6.devconf_dflt->forwarding) {
524 rtnl_unlock();
525 return 0;
Eric W. Biederman88af1822010-02-19 13:22:59 +0000526 }
Stephen Hemmingerb325fdd2009-02-26 06:55:31 +0000527
Pavel Emelyanove1869322008-01-10 17:43:50 -0800528 if (p == &net->ipv6.devconf_all->forwarding) {
Pavel Emelyanove1869322008-01-10 17:43:50 -0800529 net->ipv6.devconf_dflt->forwarding = newf;
530 addrconf_forward_change(net, newf);
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000531 } else if ((!newf) ^ (!old))
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800532 dev_forward_change((struct inet6_dev *)table->extra1);
Ben Hutchings0187bdf2008-06-19 16:15:47 -0700533 rtnl_unlock();
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800534
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000535 if (newf)
Daniel Lezcano7b4da532008-03-04 13:47:14 -0800536 rt6_purge_dflt_routers(net);
Stephen Hemmingerb325fdd2009-02-26 06:55:31 +0000537 return 1;
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800538}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539#endif
540
stephen hemminger5c578ae2010-03-17 20:31:11 +0000541/* Nobody refers to this ifaddr, destroy it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp)
543{
stephen hemmingerc2e21292010-03-17 20:31:10 +0000544 WARN_ON(!hlist_unhashed(&ifp->addr_lst));
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700545
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546#ifdef NET_REFCNT_DEBUG
547 printk(KERN_DEBUG "inet6_ifa_finish_destroy\n");
548#endif
549
550 in6_dev_put(ifp->idev);
551
552 if (del_timer(&ifp->timer))
Stephen Hemmingere21e8462010-03-20 16:09:01 -0700553 pr_notice("Timer is still running, when freeing ifa=%p\n", ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Herbert Xue9d3e082010-05-18 15:36:06 -0700555 if (ifp->state != INET6_IFADDR_STATE_DEAD) {
Stephen Hemmingere21e8462010-03-20 16:09:01 -0700556 pr_warning("Freeing alive inet6 address %p\n", ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 return;
558 }
Changli Gaod8d1f302010-06-10 23:31:35 -0700559 dst_release(&ifp->rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Lai Jiangshane5785982011-03-15 18:00:14 +0800561 kfree_rcu(ifp, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562}
563
Brian Haleye55ffac2006-07-10 15:25:51 -0700564static void
565ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp)
566{
stephen hemminger502a2ff2010-03-17 20:31:13 +0000567 struct list_head *p;
YOSHIFUJI Hideaki8a6ce0c2006-07-11 13:05:30 -0700568 int ifp_scope = ipv6_addr_src_scope(&ifp->addr);
Brian Haleye55ffac2006-07-10 15:25:51 -0700569
570 /*
571 * Each device address list is sorted in order of scope -
572 * global before linklocal.
573 */
stephen hemminger502a2ff2010-03-17 20:31:13 +0000574 list_for_each(p, &idev->addr_list) {
575 struct inet6_ifaddr *ifa
576 = list_entry(p, struct inet6_ifaddr, if_list);
YOSHIFUJI Hideaki8a6ce0c2006-07-11 13:05:30 -0700577 if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr))
Brian Haleye55ffac2006-07-10 15:25:51 -0700578 break;
579 }
580
David S. Millerb54c9b92010-03-25 21:25:30 -0700581 list_add_tail(&ifp->if_list, p);
Brian Haleye55ffac2006-07-10 15:25:51 -0700582}
583
stephen hemminger3a88a812010-03-17 20:31:12 +0000584static u32 ipv6_addr_hash(const struct in6_addr *addr)
YOSHIFUJI Hideaki3eb84f42008-04-10 15:42:08 +0900585{
YOSHIFUJI Hideaki3eb84f42008-04-10 15:42:08 +0900586 /*
587 * We perform the hash function over the last 64 bits of the address
588 * This will include the IEEE address token on links that support it.
589 */
Eric Dumazet0eae88f2010-04-20 19:06:52 -0700590 return jhash_2words((__force u32)addr->s6_addr32[2],
591 (__force u32)addr->s6_addr32[3], 0)
stephen hemminger3a88a812010-03-17 20:31:12 +0000592 & (IN6_ADDR_HSIZE - 1);
YOSHIFUJI Hideaki3eb84f42008-04-10 15:42:08 +0900593}
594
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595/* On success it returns ifp with increased reference count */
596
597static struct inet6_ifaddr *
598ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr, int pfxlen,
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -0700599 int scope, u32 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600{
601 struct inet6_ifaddr *ifa = NULL;
602 struct rt6_info *rt;
stephen hemminger3a88a812010-03-17 20:31:12 +0000603 unsigned int hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 int err = 0;
YOSHIFUJI Hideakid68b8272008-06-25 16:26:47 +0900605 int addr_type = ipv6_addr_type(addr);
606
607 if (addr_type == IPV6_ADDR_ANY ||
608 addr_type & IPV6_ADDR_MULTICAST ||
609 (!(idev->dev->flags & IFF_LOOPBACK) &&
610 addr_type & IPV6_ADDR_LOOPBACK))
611 return ERR_PTR(-EADDRNOTAVAIL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -0700613 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 if (idev->dead) {
615 err = -ENODEV; /*XXX*/
616 goto out2;
617 }
618
Brian Haley56d417b2009-06-01 03:07:33 -0700619 if (idev->cnf.disable_ipv6) {
Brian Haley9bdd8d42009-03-18 18:22:48 -0700620 err = -EACCES;
621 goto out2;
622 }
623
stephen hemminger5c578ae2010-03-17 20:31:11 +0000624 spin_lock(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
626 /* Ignore adding duplicate addresses on an interface */
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900627 if (ipv6_chk_same_addr(dev_net(idev->dev), addr, idev->dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 ADBG(("ipv6_add_addr: already assigned\n"));
629 err = -EEXIST;
630 goto out;
631 }
632
Ingo Oeser322f74a2006-03-20 23:01:47 -0800633 ifa = kzalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
635 if (ifa == NULL) {
636 ADBG(("ipv6_add_addr: malloc failed\n"));
637 err = -ENOBUFS;
638 goto out;
639 }
640
David S. Miller8f031512011-12-06 16:48:14 -0500641 rt = addrconf_dst_alloc(idev, addr, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 if (IS_ERR(rt)) {
643 err = PTR_ERR(rt);
644 goto out;
645 }
646
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000647 ifa->addr = *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
649 spin_lock_init(&ifa->lock);
Herbert Xue9d3e082010-05-18 15:36:06 -0700650 spin_lock_init(&ifa->state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 init_timer(&ifa->timer);
stephen hemmingerc2e21292010-03-17 20:31:10 +0000652 INIT_HLIST_NODE(&ifa->addr_lst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 ifa->timer.data = (unsigned long) ifa;
654 ifa->scope = scope;
655 ifa->prefix_len = pfxlen;
656 ifa->flags = flags | IFA_F_TENTATIVE;
657 ifa->cstamp = ifa->tstamp = jiffies;
658
Keir Fraser57f5f542006-08-29 02:43:49 -0700659 ifa->rt = rt;
660
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 ifa->idev = idev;
662 in6_dev_hold(idev);
663 /* For caller */
664 in6_ifa_hold(ifa);
665
666 /* Add to big hash table */
667 hash = ipv6_addr_hash(addr);
668
stephen hemminger5c578ae2010-03-17 20:31:11 +0000669 hlist_add_head_rcu(&ifa->addr_lst, &inet6_addr_lst[hash]);
stephen hemminger5c578ae2010-03-17 20:31:11 +0000670 spin_unlock(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
672 write_lock(&idev->lock);
673 /* Add to inet6_dev unicast addr list. */
Brian Haleye55ffac2006-07-10 15:25:51 -0700674 ipv6_link_dev_addr(idev, ifa);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
676#ifdef CONFIG_IPV6_PRIVACY
677 if (ifa->flags&IFA_F_TEMPORARY) {
stephen hemminger372e6c82010-03-17 20:31:09 +0000678 list_add(&ifa->tmp_list, &idev->tempaddr_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 in6_ifa_hold(ifa);
680 }
681#endif
682
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 in6_ifa_hold(ifa);
684 write_unlock(&idev->lock);
685out2:
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -0700686 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
YOSHIFUJI Hideakifd928332005-04-19 22:27:09 -0700688 if (likely(err == 0))
Alan Sterne041c682006-03-27 01:16:30 -0800689 atomic_notifier_call_chain(&inet6addr_chain, NETDEV_UP, ifa);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 else {
691 kfree(ifa);
692 ifa = ERR_PTR(err);
693 }
694
695 return ifa;
696out:
stephen hemminger5c578ae2010-03-17 20:31:11 +0000697 spin_unlock(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 goto out2;
699}
700
701/* This function wants to get referenced ifp and releases it before return */
702
703static void ipv6_del_addr(struct inet6_ifaddr *ifp)
704{
stephen hemminger502a2ff2010-03-17 20:31:13 +0000705 struct inet6_ifaddr *ifa, *ifn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 struct inet6_dev *idev = ifp->idev;
Herbert Xu4c5ff6a2010-05-18 15:54:18 -0700707 int state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 int deleted = 0, onlink = 0;
709 unsigned long expires = jiffies;
710
Herbert Xu4c5ff6a2010-05-18 15:54:18 -0700711 spin_lock_bh(&ifp->state_lock);
712 state = ifp->state;
Herbert Xue9d3e082010-05-18 15:36:06 -0700713 ifp->state = INET6_IFADDR_STATE_DEAD;
Herbert Xu4c5ff6a2010-05-18 15:54:18 -0700714 spin_unlock_bh(&ifp->state_lock);
715
716 if (state == INET6_IFADDR_STATE_DEAD)
717 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
stephen hemminger5c578ae2010-03-17 20:31:11 +0000719 spin_lock_bh(&addrconf_hash_lock);
720 hlist_del_init_rcu(&ifp->addr_lst);
stephen hemminger5c578ae2010-03-17 20:31:11 +0000721 spin_unlock_bh(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
723 write_lock_bh(&idev->lock);
724#ifdef CONFIG_IPV6_PRIVACY
725 if (ifp->flags&IFA_F_TEMPORARY) {
stephen hemminger372e6c82010-03-17 20:31:09 +0000726 list_del(&ifp->tmp_list);
727 if (ifp->ifpub) {
728 in6_ifa_put(ifp->ifpub);
729 ifp->ifpub = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 }
stephen hemminger372e6c82010-03-17 20:31:09 +0000731 __in6_ifa_put(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 }
733#endif
734
stephen hemminger502a2ff2010-03-17 20:31:13 +0000735 list_for_each_entry_safe(ifa, ifn, &idev->addr_list, if_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 if (ifa == ifp) {
stephen hemminger502a2ff2010-03-17 20:31:13 +0000737 list_del_init(&ifp->if_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 __in6_ifa_put(ifp);
stephen hemminger502a2ff2010-03-17 20:31:13 +0000739
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 if (!(ifp->flags & IFA_F_PERMANENT) || onlink > 0)
741 break;
742 deleted = 1;
Kristian Slavov1d142802005-12-21 18:47:24 -0800743 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 } else if (ifp->flags & IFA_F_PERMANENT) {
745 if (ipv6_prefix_equal(&ifa->addr, &ifp->addr,
746 ifp->prefix_len)) {
747 if (ifa->flags & IFA_F_PERMANENT) {
748 onlink = 1;
749 if (deleted)
750 break;
751 } else {
752 unsigned long lifetime;
753
754 if (!onlink)
755 onlink = -1;
756
757 spin_lock(&ifa->lock);
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +0900758
759 lifetime = addrconf_timeout_fixup(ifa->valid_lft, HZ);
760 /*
761 * Note: Because this address is
762 * not permanent, lifetime <
763 * LONG_MAX / HZ here.
764 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 if (time_before(expires,
766 ifa->tstamp + lifetime * HZ))
767 expires = ifa->tstamp + lifetime * HZ;
768 spin_unlock(&ifa->lock);
769 }
770 }
771 }
772 }
773 write_unlock_bh(&idev->lock);
774
Andrey Vaginb2238562008-07-08 15:13:31 -0700775 addrconf_del_timer(ifp);
776
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 ipv6_ifa_notify(RTM_DELADDR, ifp);
778
Alan Sterne041c682006-03-27 01:16:30 -0800779 atomic_notifier_call_chain(&inet6addr_chain, NETDEV_DOWN, ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 /*
782 * Purge or update corresponding prefix
783 *
784 * 1) we don't purge prefix here if address was not permanent.
785 * prefix is managed by its own lifetime.
786 * 2) if there're no addresses, delete prefix.
787 * 3) if there're still other permanent address(es),
788 * corresponding prefix is still permanent.
789 * 4) otherwise, update prefix lifetime to the
790 * longest valid lifetime among the corresponding
791 * addresses on the device.
792 * Note: subsequent RA will update lifetime.
793 *
794 * --yoshfuji
795 */
796 if ((ifp->flags & IFA_F_PERMANENT) && onlink < 1) {
797 struct in6_addr prefix;
798 struct rt6_info *rt;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900799 struct net *net = dev_net(ifp->idev->dev);
Nicolas Dichteld5e36b02012-09-26 00:04:55 +0000800 struct flowi6 fl6 = {};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
Nicolas Dichteld5e36b02012-09-26 00:04:55 +0000802 ipv6_addr_prefix(&prefix, &ifp->addr, ifp->prefix_len);
803 fl6.flowi6_oif = ifp->idev->dev->ifindex;
804 fl6.daddr = prefix;
805 rt = (struct rt6_info *)ip6_route_lookup(net, &fl6,
806 RT6_LOOKUP_F_IFACE);
807
808 if (rt != net->ipv6.ip6_null_entry &&
809 addrconf_is_prefix_route(rt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 if (onlink == 0) {
Thomas Grafe0a1ad732006-08-22 00:00:21 -0700811 ip6_del_rt(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 rt = NULL;
813 } else if (!(rt->rt6i_flags & RTF_EXPIRES)) {
Gao feng1716a962012-04-06 00:13:10 +0000814 rt6_set_expires(rt, expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 }
816 }
Changli Gaod8d1f302010-06-10 23:31:35 -0700817 dst_release(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 }
819
Daniel Walterc3968a82011-04-13 21:10:57 +0000820 /* clean up prefsrc entries */
821 rt6_remove_prefsrc(ifp);
Herbert Xu4c5ff6a2010-05-18 15:54:18 -0700822out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 in6_ifa_put(ifp);
824}
825
826#ifdef CONFIG_IPV6_PRIVACY
827static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift)
828{
829 struct inet6_dev *idev = ifp->idev;
830 struct in6_addr addr, *tmpaddr;
Lorenzo Colitti76f793e2011-07-26 13:50:49 +0000831 unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_tstamp, age;
Benoit Boissinoteac55bf2008-04-02 00:01:35 -0700832 unsigned long regen_advance;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 int tmp_plen;
834 int ret = 0;
835 int max_addresses;
Neil Horman95c385b2007-04-25 17:08:10 -0700836 u32 addr_flags;
Lorenzo Colitti76f793e2011-07-26 13:50:49 +0000837 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
839 write_lock(&idev->lock);
840 if (ift) {
841 spin_lock_bh(&ift->lock);
842 memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8);
843 spin_unlock_bh(&ift->lock);
844 tmpaddr = &addr;
845 } else {
846 tmpaddr = NULL;
847 }
848retry:
849 in6_dev_hold(idev);
850 if (idev->cnf.use_tempaddr <= 0) {
851 write_unlock(&idev->lock);
852 printk(KERN_INFO
853 "ipv6_create_tempaddr(): use_tempaddr is disabled.\n");
854 in6_dev_put(idev);
855 ret = -1;
856 goto out;
857 }
858 spin_lock_bh(&ifp->lock);
859 if (ifp->regen_count++ >= idev->cnf.regen_max_retry) {
860 idev->cnf.use_tempaddr = -1; /*XXX*/
861 spin_unlock_bh(&ifp->lock);
862 write_unlock(&idev->lock);
863 printk(KERN_WARNING
864 "ipv6_create_tempaddr(): regeneration time exceeded. disabled temporary address support.\n");
865 in6_dev_put(idev);
866 ret = -1;
867 goto out;
868 }
869 in6_ifa_hold(ifp);
870 memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
871 if (__ipv6_try_regen_rndid(idev, tmpaddr) < 0) {
872 spin_unlock_bh(&ifp->lock);
873 write_unlock(&idev->lock);
874 printk(KERN_WARNING
875 "ipv6_create_tempaddr(): regeneration of randomized interface id failed.\n");
876 in6_ifa_put(ifp);
877 in6_dev_put(idev);
878 ret = -1;
879 goto out;
880 }
881 memcpy(&addr.s6_addr[8], idev->rndid, 8);
Lorenzo Colitti76f793e2011-07-26 13:50:49 +0000882 age = (now - ifp->tstamp) / HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 tmp_valid_lft = min_t(__u32,
884 ifp->valid_lft,
Glenn Wurster7a876b02010-09-27 07:10:10 +0000885 idev->cnf.temp_valid_lft + age);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900886 tmp_prefered_lft = min_t(__u32,
887 ifp->prefered_lft,
Glenn Wurster7a876b02010-09-27 07:10:10 +0000888 idev->cnf.temp_prefered_lft + age -
Ben Hutchings784e2712010-06-26 11:42:55 +0000889 idev->cnf.max_desync_factor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 tmp_plen = ifp->prefix_len;
891 max_addresses = idev->cnf.max_addresses;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 tmp_tstamp = ifp->tstamp;
893 spin_unlock_bh(&ifp->lock);
894
Benoit Boissinoteac55bf2008-04-02 00:01:35 -0700895 regen_advance = idev->cnf.regen_max_retry *
896 idev->cnf.dad_transmits *
897 idev->nd_parms->retrans_time / HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 write_unlock(&idev->lock);
Neil Horman95c385b2007-04-25 17:08:10 -0700899
Benoit Boissinoteac55bf2008-04-02 00:01:35 -0700900 /* A temporary address is created only if this calculated Preferred
901 * Lifetime is greater than REGEN_ADVANCE time units. In particular,
902 * an implementation must not create a temporary address with a zero
903 * Preferred Lifetime.
904 */
905 if (tmp_prefered_lft <= regen_advance) {
906 in6_ifa_put(ifp);
907 in6_dev_put(idev);
908 ret = -1;
909 goto out;
910 }
911
Neil Horman95c385b2007-04-25 17:08:10 -0700912 addr_flags = IFA_F_TEMPORARY;
913 /* set in addrconf_prefix_rcv() */
914 if (ifp->flags & IFA_F_OPTIMISTIC)
915 addr_flags |= IFA_F_OPTIMISTIC;
916
Hannes Frederic Sowaf4cb8372013-08-16 13:02:27 +0200917 ift = ipv6_add_addr(idev, &addr, tmp_plen,
918 ipv6_addr_type(&addr)&IPV6_ADDR_SCOPE_MASK,
919 addr_flags);
920 if (IS_ERR(ift)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 in6_ifa_put(ifp);
922 in6_dev_put(idev);
923 printk(KERN_INFO
924 "ipv6_create_tempaddr(): retry temporary address regeneration.\n");
925 tmpaddr = &addr;
926 write_lock(&idev->lock);
927 goto retry;
928 }
929
930 spin_lock_bh(&ift->lock);
931 ift->ifpub = ifp;
932 ift->valid_lft = tmp_valid_lft;
933 ift->prefered_lft = tmp_prefered_lft;
Lorenzo Colitti76f793e2011-07-26 13:50:49 +0000934 ift->cstamp = now;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 ift->tstamp = tmp_tstamp;
936 spin_unlock_bh(&ift->lock);
937
938 addrconf_dad_start(ift, 0);
939 in6_ifa_put(ift);
940 in6_dev_put(idev);
941out:
942 return ret;
943}
944#endif
945
946/*
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800947 * Choose an appropriate source address (RFC3484)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 */
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +0900949enum {
950 IPV6_SADDR_RULE_INIT = 0,
951 IPV6_SADDR_RULE_LOCAL,
952 IPV6_SADDR_RULE_SCOPE,
953 IPV6_SADDR_RULE_PREFERRED,
954#ifdef CONFIG_IPV6_MIP6
955 IPV6_SADDR_RULE_HOA,
956#endif
957 IPV6_SADDR_RULE_OIF,
958 IPV6_SADDR_RULE_LABEL,
959#ifdef CONFIG_IPV6_PRIVACY
960 IPV6_SADDR_RULE_PRIVACY,
961#endif
962 IPV6_SADDR_RULE_ORCHID,
963 IPV6_SADDR_RULE_PREFIX,
964 IPV6_SADDR_RULE_MAX
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800965};
966
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +0900967struct ipv6_saddr_score {
968 int rule;
969 int addr_type;
970 struct inet6_ifaddr *ifa;
971 DECLARE_BITMAP(scorebits, IPV6_SADDR_RULE_MAX);
972 int scopedist;
973 int matchlen;
974};
975
976struct ipv6_saddr_dst {
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +0900977 const struct in6_addr *addr;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +0900978 int ifindex;
979 int scope;
980 int label;
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +0900981 unsigned int prefs;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +0900982};
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800983
Dave Jonesb6f99a22007-03-22 12:27:49 -0700984static inline int ipv6_saddr_preferred(int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985{
Ulrich Weber45bb0062010-02-25 23:28:58 +0000986 if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|IPV6_ADDR_LOOPBACK))
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800987 return 1;
988 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989}
990
Benjamin Thery3de23252008-05-28 14:51:24 +0200991static int ipv6_get_saddr_eval(struct net *net,
992 struct ipv6_saddr_score *score,
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +0900993 struct ipv6_saddr_dst *dst,
994 int i)
995{
996 int ret;
997
998 if (i <= score->rule) {
999 switch (i) {
1000 case IPV6_SADDR_RULE_SCOPE:
1001 ret = score->scopedist;
1002 break;
1003 case IPV6_SADDR_RULE_PREFIX:
1004 ret = score->matchlen;
1005 break;
1006 default:
1007 ret = !!test_bit(i, score->scorebits);
1008 }
1009 goto out;
1010 }
1011
1012 switch (i) {
1013 case IPV6_SADDR_RULE_INIT:
1014 /* Rule 0: remember if hiscore is not ready yet */
1015 ret = !!score->ifa;
1016 break;
1017 case IPV6_SADDR_RULE_LOCAL:
1018 /* Rule 1: Prefer same address */
1019 ret = ipv6_addr_equal(&score->ifa->addr, dst->addr);
1020 break;
1021 case IPV6_SADDR_RULE_SCOPE:
1022 /* Rule 2: Prefer appropriate scope
1023 *
1024 * ret
1025 * ^
1026 * -1 | d 15
1027 * ---+--+-+---> scope
1028 * |
1029 * | d is scope of the destination.
1030 * B-d | \
1031 * | \ <- smaller scope is better if
1032 * B-15 | \ if scope is enough for destinaion.
1033 * | ret = B - scope (-1 <= scope >= d <= 15).
1034 * d-C-1 | /
1035 * |/ <- greater is better
1036 * -C / if scope is not enough for destination.
1037 * /| ret = scope - C (-1 <= d < scope <= 15).
1038 *
1039 * d - C - 1 < B -15 (for all -1 <= d <= 15).
1040 * C > d + 14 - B >= 15 + 14 - B = 29 - B.
1041 * Assume B = 0 and we get C > 29.
1042 */
1043 ret = __ipv6_addr_src_scope(score->addr_type);
1044 if (ret >= dst->scope)
1045 ret = -ret;
1046 else
1047 ret -= 128; /* 30 is enough */
1048 score->scopedist = ret;
1049 break;
1050 case IPV6_SADDR_RULE_PREFERRED:
1051 /* Rule 3: Avoid deprecated and optimistic addresses */
1052 ret = ipv6_saddr_preferred(score->addr_type) ||
1053 !(score->ifa->flags & (IFA_F_DEPRECATED|IFA_F_OPTIMISTIC));
1054 break;
1055#ifdef CONFIG_IPV6_MIP6
1056 case IPV6_SADDR_RULE_HOA:
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001057 {
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001058 /* Rule 4: Prefer home address */
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001059 int prefhome = !(dst->prefs & IPV6_PREFER_SRC_COA);
1060 ret = !(score->ifa->flags & IFA_F_HOMEADDRESS) ^ prefhome;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001061 break;
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001062 }
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001063#endif
1064 case IPV6_SADDR_RULE_OIF:
1065 /* Rule 5: Prefer outgoing interface */
1066 ret = (!dst->ifindex ||
1067 dst->ifindex == score->ifa->idev->dev->ifindex);
1068 break;
1069 case IPV6_SADDR_RULE_LABEL:
1070 /* Rule 6: Prefer matching label */
Benjamin Thery3de23252008-05-28 14:51:24 +02001071 ret = ipv6_addr_label(net,
1072 &score->ifa->addr, score->addr_type,
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001073 score->ifa->idev->dev->ifindex) == dst->label;
1074 break;
1075#ifdef CONFIG_IPV6_PRIVACY
1076 case IPV6_SADDR_RULE_PRIVACY:
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001077 {
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001078 /* Rule 7: Prefer public address
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001079 * Note: prefer temporary address if use_tempaddr >= 2
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001080 */
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001081 int preftmp = dst->prefs & (IPV6_PREFER_SRC_PUBLIC|IPV6_PREFER_SRC_TMP) ?
1082 !!(dst->prefs & IPV6_PREFER_SRC_TMP) :
1083 score->ifa->idev->cnf.use_tempaddr >= 2;
1084 ret = (!(score->ifa->flags & IFA_F_TEMPORARY)) ^ preftmp;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001085 break;
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001086 }
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001087#endif
1088 case IPV6_SADDR_RULE_ORCHID:
1089 /* Rule 8-: Prefer ORCHID vs ORCHID or
1090 * non-ORCHID vs non-ORCHID
1091 */
1092 ret = !(ipv6_addr_orchid(&score->ifa->addr) ^
1093 ipv6_addr_orchid(dst->addr));
1094 break;
1095 case IPV6_SADDR_RULE_PREFIX:
1096 /* Rule 8: Use longest matching prefix */
1097 score->matchlen = ret = ipv6_addr_diff(&score->ifa->addr,
1098 dst->addr);
1099 break;
1100 default:
1101 ret = 0;
1102 }
1103
1104 if (ret)
1105 __set_bit(i, score->scorebits);
1106 score->rule = i;
1107out:
1108 return ret;
1109}
1110
Brian Haley191cd582008-08-14 15:33:21 -07001111int ipv6_dev_get_saddr(struct net *net, struct net_device *dst_dev,
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +09001112 const struct in6_addr *daddr, unsigned int prefs,
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001113 struct in6_addr *saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114{
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001115 struct ipv6_saddr_score scores[2],
1116 *score = &scores[0], *hiscore = &scores[1];
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001117 struct ipv6_saddr_dst dst;
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001118 struct net_device *dev;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001119 int dst_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001121 dst_type = __ipv6_addr_type(daddr);
1122 dst.addr = daddr;
1123 dst.ifindex = dst_dev ? dst_dev->ifindex : 0;
1124 dst.scope = __ipv6_addr_src_scope(dst_type);
Benjamin Thery3de23252008-05-28 14:51:24 +02001125 dst.label = ipv6_addr_label(net, daddr, dst_type, dst.ifindex);
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001126 dst.prefs = prefs;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001127
1128 hiscore->rule = -1;
1129 hiscore->ifa = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001131 rcu_read_lock();
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001132
Eric Dumazetc6d14c82009-11-04 05:43:23 -08001133 for_each_netdev_rcu(net, dev) {
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001134 struct inet6_dev *idev;
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001135
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001136 /* Candidate Source Address (section 4)
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001137 * - multicast and link-local destination address,
1138 * the set of candidate source address MUST only
1139 * include addresses assigned to interfaces
1140 * belonging to the same link as the outgoing
1141 * interface.
1142 * (- For site-local destination addresses, the
1143 * set of candidate source addresses MUST only
1144 * include addresses assigned to interfaces
1145 * belonging to the same site as the outgoing
1146 * interface.)
1147 */
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001148 if (((dst_type & IPV6_ADDR_MULTICAST) ||
1149 dst.scope <= IPV6_ADDR_SCOPE_LINKLOCAL) &&
1150 dst.ifindex && dev->ifindex != dst.ifindex)
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001151 continue;
1152
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 idev = __in6_dev_get(dev);
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001154 if (!idev)
1155 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001157 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00001158 list_for_each_entry(score->ifa, &idev->addr_list, if_list) {
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001159 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001161 /*
YOSHIFUJI Hideaki6b3ae802005-12-21 22:58:01 +09001162 * - Tentative Address (RFC2462 section 5.4)
1163 * - A tentative address is not considered
1164 * "assigned to an interface" in the traditional
Neil Horman95c385b2007-04-25 17:08:10 -07001165 * sense, unless it is also flagged as optimistic.
YOSHIFUJI Hideaki6b3ae802005-12-21 22:58:01 +09001166 * - Candidate Source Address (section 4)
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001167 * - In any case, anycast addresses, multicast
1168 * addresses, and the unspecified address MUST
1169 * NOT be included in a candidate set.
1170 */
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001171 if ((score->ifa->flags & IFA_F_TENTATIVE) &&
1172 (!(score->ifa->flags & IFA_F_OPTIMISTIC)))
YOSHIFUJI Hideaki6b3ae802005-12-21 22:58:01 +09001173 continue;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001174
1175 score->addr_type = __ipv6_addr_type(&score->ifa->addr);
1176
1177 if (unlikely(score->addr_type == IPV6_ADDR_ANY ||
1178 score->addr_type & IPV6_ADDR_MULTICAST)) {
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001179 LIMIT_NETDEBUG(KERN_DEBUG
Joe Perches3b6d8212007-11-19 23:47:25 -08001180 "ADDRCONF: unspecified / multicast address "
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001181 "assigned as unicast address on %s",
1182 dev->name);
1183 continue;
1184 }
1185
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001186 score->rule = -1;
1187 bitmap_zero(score->scorebits, IPV6_SADDR_RULE_MAX);
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001188
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001189 for (i = 0; i < IPV6_SADDR_RULE_MAX; i++) {
1190 int minihiscore, miniscore;
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001191
Benjamin Thery3de23252008-05-28 14:51:24 +02001192 minihiscore = ipv6_get_saddr_eval(net, hiscore, &dst, i);
1193 miniscore = ipv6_get_saddr_eval(net, score, &dst, i);
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001194
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001195 if (minihiscore > miniscore) {
1196 if (i == IPV6_SADDR_RULE_SCOPE &&
1197 score->scopedist > 0) {
1198 /*
1199 * special case:
1200 * each remaining entry
1201 * has too small (not enough)
1202 * scope, because ifa entries
1203 * are sorted by their scope
1204 * values.
1205 */
1206 goto try_nextdev;
1207 }
1208 break;
1209 } else if (minihiscore < miniscore) {
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001210 if (hiscore->ifa)
1211 in6_ifa_put(hiscore->ifa);
1212
1213 in6_ifa_hold(score->ifa);
1214
Ilpo Järvinena0bffff2009-03-21 13:36:17 -07001215 swap(hiscore, score);
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001216
1217 /* restore our iterator */
1218 score->ifa = hiscore->ifa;
1219
1220 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 }
1222 }
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001223 }
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001224try_nextdev:
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001225 read_unlock_bh(&idev->lock);
1226 }
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001227 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001229 if (!hiscore->ifa)
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001230 return -EADDRNOTAVAIL;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001231
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001232 *saddr = hiscore->ifa->addr;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001233 in6_ifa_put(hiscore->ifa);
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001234 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235}
YOSHIFUJI Hideaki5e5f3f02008-03-03 21:44:34 +09001236EXPORT_SYMBOL(ipv6_dev_get_saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
Amerigo Wang3ef208a2013-06-29 21:30:49 +08001238int __ipv6_get_lladdr(struct inet6_dev *idev, struct in6_addr *addr,
1239 unsigned char banned_flags)
1240{
1241 struct inet6_ifaddr *ifp;
1242 int err = -EADDRNOTAVAIL;
1243
1244 list_for_each_entry(ifp, &idev->addr_list, if_list) {
1245 if (ifp->scope == IFA_LINK &&
1246 !(ifp->flags & banned_flags)) {
1247 *addr = ifp->addr;
1248 err = 0;
1249 break;
1250 }
1251 }
1252 return err;
1253}
1254
Neil Horman95c385b2007-04-25 17:08:10 -07001255int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr,
1256 unsigned char banned_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257{
1258 struct inet6_dev *idev;
1259 int err = -EADDRNOTAVAIL;
1260
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001261 rcu_read_lock();
Stephen Hemmingere21e8462010-03-20 16:09:01 -07001262 idev = __in6_dev_get(dev);
1263 if (idev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 read_lock_bh(&idev->lock);
Amerigo Wang3ef208a2013-06-29 21:30:49 +08001265 err = __ipv6_get_lladdr(idev, addr, banned_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 read_unlock_bh(&idev->lock);
1267 }
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001268 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 return err;
1270}
1271
1272static int ipv6_count_addresses(struct inet6_dev *idev)
1273{
1274 int cnt = 0;
1275 struct inet6_ifaddr *ifp;
1276
1277 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00001278 list_for_each_entry(ifp, &idev->addr_list, if_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 cnt++;
1280 read_unlock_bh(&idev->lock);
1281 return cnt;
1282}
1283
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001284int ipv6_chk_addr(struct net *net, const struct in6_addr *addr,
Daniel Lezcanobfeade02008-01-10 22:43:18 -08001285 struct net_device *dev, int strict)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286{
Stephen Hemmingereedf0422010-05-17 22:27:12 -07001287 struct inet6_ifaddr *ifp;
stephen hemmingerc2e21292010-03-17 20:31:10 +00001288 struct hlist_node *node;
stephen hemminger3a88a812010-03-17 20:31:12 +00001289 unsigned int hash = ipv6_addr_hash(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
stephen hemminger5c578ae2010-03-17 20:31:11 +00001291 rcu_read_lock_bh();
1292 hlist_for_each_entry_rcu(ifp, node, &inet6_addr_lst[hash], addr_lst) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09001293 if (!net_eq(dev_net(ifp->idev->dev), net))
Daniel Lezcanobfeade02008-01-10 22:43:18 -08001294 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 if (ipv6_addr_equal(&ifp->addr, addr) &&
Stephen Hemmingereedf0422010-05-17 22:27:12 -07001296 !(ifp->flags&IFA_F_TENTATIVE) &&
1297 (dev == NULL || ifp->idev->dev == dev ||
1298 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))) {
1299 rcu_read_unlock_bh();
1300 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 }
1302 }
stephen hemminger5c578ae2010-03-17 20:31:11 +00001303
Stephen Hemmingereedf0422010-05-17 22:27:12 -07001304 rcu_read_unlock_bh();
1305 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306}
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +09001307EXPORT_SYMBOL(ipv6_chk_addr);
1308
David S. Miller3e81c6d2010-03-20 16:18:00 -07001309static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
1310 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311{
David S. Miller3e81c6d2010-03-20 16:18:00 -07001312 unsigned int hash = ipv6_addr_hash(addr);
stephen hemmingerc2e21292010-03-17 20:31:10 +00001313 struct inet6_ifaddr *ifp;
1314 struct hlist_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
stephen hemmingerc2e21292010-03-17 20:31:10 +00001316 hlist_for_each_entry(ifp, node, &inet6_addr_lst[hash], addr_lst) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09001317 if (!net_eq(dev_net(ifp->idev->dev), net))
Daniel Lezcano06bfe652008-01-10 22:43:42 -08001318 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 if (ipv6_addr_equal(&ifp->addr, addr)) {
1320 if (dev == NULL || ifp->idev->dev == dev)
David S. Miller3e81c6d2010-03-20 16:18:00 -07001321 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 }
1323 }
David S. Miller3e81c6d2010-03-20 16:18:00 -07001324 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325}
1326
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001327int ipv6_chk_prefix(const struct in6_addr *addr, struct net_device *dev)
YOSHIFUJI Hideaki52eeeb82008-03-15 22:54:23 -04001328{
1329 struct inet6_dev *idev;
1330 struct inet6_ifaddr *ifa;
1331 int onlink;
1332
1333 onlink = 0;
1334 rcu_read_lock();
1335 idev = __in6_dev_get(dev);
1336 if (idev) {
1337 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00001338 list_for_each_entry(ifa, &idev->addr_list, if_list) {
YOSHIFUJI Hideaki52eeeb82008-03-15 22:54:23 -04001339 onlink = ipv6_prefix_equal(addr, &ifa->addr,
1340 ifa->prefix_len);
1341 if (onlink)
1342 break;
1343 }
1344 read_unlock_bh(&idev->lock);
1345 }
1346 rcu_read_unlock();
1347 return onlink;
1348}
1349
1350EXPORT_SYMBOL(ipv6_chk_prefix);
1351
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +09001352struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, const struct in6_addr *addr,
Daniel Lezcano1cab3da2008-01-10 22:44:09 -08001353 struct net_device *dev, int strict)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354{
David S. Millerb79d1d52010-03-25 21:39:21 -07001355 struct inet6_ifaddr *ifp, *result = NULL;
stephen hemminger3a88a812010-03-17 20:31:12 +00001356 unsigned int hash = ipv6_addr_hash(addr);
David S. Millerb79d1d52010-03-25 21:39:21 -07001357 struct hlist_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
stephen hemminger5c578ae2010-03-17 20:31:11 +00001359 rcu_read_lock_bh();
Eric Dumazet4f70ecc2010-05-03 10:50:14 +00001360 hlist_for_each_entry_rcu_bh(ifp, node, &inet6_addr_lst[hash], addr_lst) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09001361 if (!net_eq(dev_net(ifp->idev->dev), net))
Daniel Lezcano1cab3da2008-01-10 22:44:09 -08001362 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 if (ipv6_addr_equal(&ifp->addr, addr)) {
1364 if (dev == NULL || ifp->idev->dev == dev ||
1365 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
David S. Millerb79d1d52010-03-25 21:39:21 -07001366 result = ifp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 in6_ifa_hold(ifp);
1368 break;
1369 }
1370 }
1371 }
stephen hemminger5c578ae2010-03-17 20:31:11 +00001372 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
David S. Millerb79d1d52010-03-25 21:39:21 -07001374 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375}
1376
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377/* Gets referenced address, destroys ifaddr */
1378
Brian Haleycc411d02009-09-09 14:41:32 +00001379static void addrconf_dad_stop(struct inet6_ifaddr *ifp, int dad_failed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 if (ifp->flags&IFA_F_PERMANENT) {
1382 spin_lock_bh(&ifp->lock);
1383 addrconf_del_timer(ifp);
1384 ifp->flags |= IFA_F_TENTATIVE;
Brian Haleycc411d02009-09-09 14:41:32 +00001385 if (dad_failed)
1386 ifp->flags |= IFA_F_DADFAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 spin_unlock_bh(&ifp->lock);
Herbert Xue2577a02010-03-13 12:23:29 -08001388 if (dad_failed)
1389 ipv6_ifa_notify(0, ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 in6_ifa_put(ifp);
1391#ifdef CONFIG_IPV6_PRIVACY
1392 } else if (ifp->flags&IFA_F_TEMPORARY) {
1393 struct inet6_ifaddr *ifpub;
1394 spin_lock_bh(&ifp->lock);
1395 ifpub = ifp->ifpub;
1396 if (ifpub) {
1397 in6_ifa_hold(ifpub);
1398 spin_unlock_bh(&ifp->lock);
1399 ipv6_create_tempaddr(ifpub, ifp);
1400 in6_ifa_put(ifpub);
1401 } else {
1402 spin_unlock_bh(&ifp->lock);
1403 }
1404 ipv6_del_addr(ifp);
1405#endif
1406 } else
1407 ipv6_del_addr(ifp);
1408}
1409
Herbert Xuf2344a12010-05-18 15:55:27 -07001410static int addrconf_dad_end(struct inet6_ifaddr *ifp)
1411{
1412 int err = -ENOENT;
1413
1414 spin_lock(&ifp->state_lock);
1415 if (ifp->state == INET6_IFADDR_STATE_DAD) {
1416 ifp->state = INET6_IFADDR_STATE_POSTDAD;
1417 err = 0;
1418 }
1419 spin_unlock(&ifp->state_lock);
1420
1421 return err;
1422}
1423
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09001424void addrconf_dad_failure(struct inet6_ifaddr *ifp)
1425{
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09001426 struct inet6_dev *idev = ifp->idev;
Brian Haley9bdd8d42009-03-18 18:22:48 -07001427
Ursula Braun853dc2e2010-10-24 23:06:43 +00001428 if (addrconf_dad_end(ifp)) {
1429 in6_ifa_put(ifp);
Herbert Xuf2344a12010-05-18 15:55:27 -07001430 return;
Ursula Braun853dc2e2010-10-24 23:06:43 +00001431 }
Herbert Xuf2344a12010-05-18 15:55:27 -07001432
Brian Haley9bdd8d42009-03-18 18:22:48 -07001433 if (net_ratelimit())
Jens Rosenboom0522fea2009-09-17 10:24:24 -07001434 printk(KERN_INFO "%s: IPv6 duplicate address %pI6c detected!\n",
1435 ifp->idev->dev->name, &ifp->addr);
Brian Haley9bdd8d42009-03-18 18:22:48 -07001436
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09001437 if (idev->cnf.accept_dad > 1 && !idev->cnf.disable_ipv6) {
1438 struct in6_addr addr;
1439
1440 addr.s6_addr32[0] = htonl(0xfe800000);
1441 addr.s6_addr32[1] = 0;
1442
1443 if (!ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) &&
1444 ipv6_addr_equal(&ifp->addr, &addr)) {
1445 /* DAD failed for link-local based on MAC address */
1446 idev->cnf.disable_ipv6 = 1;
Brian Haley9bdd8d42009-03-18 18:22:48 -07001447
1448 printk(KERN_INFO "%s: IPv6 being disabled!\n",
1449 ifp->idev->dev->name);
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09001450 }
1451 }
1452
Brian Haleycc411d02009-09-09 14:41:32 +00001453 addrconf_dad_stop(ifp, 1);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09001454}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
1456/* Join to solicited addr multicast group. */
1457
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001458void addrconf_join_solict(struct net_device *dev, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459{
1460 struct in6_addr maddr;
1461
1462 if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1463 return;
1464
1465 addrconf_addr_solict_mult(addr, &maddr);
1466 ipv6_dev_mc_inc(dev, &maddr);
1467}
1468
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001469void addrconf_leave_solict(struct inet6_dev *idev, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470{
1471 struct in6_addr maddr;
1472
1473 if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1474 return;
1475
1476 addrconf_addr_solict_mult(addr, &maddr);
1477 __ipv6_dev_mc_dec(idev, &maddr);
1478}
1479
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03001480static void addrconf_join_anycast(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481{
1482 struct in6_addr addr;
Bjørn Mork2bda8a02011-07-05 23:04:13 +00001483 if (ifp->prefix_len == 127) /* RFC 6164 */
1484 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1486 if (ipv6_addr_any(&addr))
1487 return;
1488 ipv6_dev_ac_inc(ifp->idev->dev, &addr);
1489}
1490
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03001491static void addrconf_leave_anycast(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492{
1493 struct in6_addr addr;
YOSHIFUJI Hideaki32019e62011-07-24 11:44:34 +00001494 if (ifp->prefix_len == 127) /* RFC 6164 */
1495 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1497 if (ipv6_addr_any(&addr))
1498 return;
1499 __ipv6_dev_ac_dec(ifp->idev, &addr);
1500}
1501
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08001502static int addrconf_ifid_eui48(u8 *eui, struct net_device *dev)
1503{
1504 if (dev->addr_len != ETH_ALEN)
1505 return -1;
1506 memcpy(eui, dev->dev_addr, 3);
1507 memcpy(eui + 5, dev->dev_addr + 3, 3);
1508
1509 /*
1510 * The zSeries OSA network cards can be shared among various
1511 * OS instances, but the OSA cards have only one MAC address.
1512 * This leads to duplicate address conflicts in conjunction
1513 * with IPv6 if more than one instance uses the same card.
1514 *
1515 * The driver for these cards can deliver a unique 16-bit
1516 * identifier for each instance sharing the same card. It is
1517 * placed instead of 0xFFFE in the interface identifier. The
1518 * "u" bit of the interface identifier is not inverted in this
1519 * case. Hence the resulting interface identifier has local
1520 * scope according to RFC2373.
1521 */
1522 if (dev->dev_id) {
1523 eui[3] = (dev->dev_id >> 8) & 0xFF;
1524 eui[4] = dev->dev_id & 0xFF;
1525 } else {
1526 eui[3] = 0xFF;
1527 eui[4] = 0xFE;
1528 eui[0] ^= 2;
1529 }
1530 return 0;
1531}
1532
1533static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev)
1534{
1535 /* XXX: inherit EUI-64 from other interface -- yoshfuji */
1536 if (dev->addr_len != ARCNET_ALEN)
1537 return -1;
1538 memset(eui, 0, 7);
1539 eui[7] = *(u8*)dev->dev_addr;
1540 return 0;
1541}
1542
1543static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev)
1544{
1545 if (dev->addr_len != INFINIBAND_ALEN)
1546 return -1;
1547 memcpy(eui, dev->dev_addr + 12, 8);
1548 eui[0] |= 2;
1549 return 0;
1550}
1551
stephen hemmingerc61393e2010-10-04 20:17:53 +00001552static int __ipv6_isatap_ifid(u8 *eui, __be32 addr)
YOSHIFUJI Hideakidfd982b2008-04-10 15:42:09 +09001553{
Sascha Hlusiak9af28512009-05-19 12:56:51 +00001554 if (addr == 0)
1555 return -1;
YOSHIFUJI Hideakidfd982b2008-04-10 15:42:09 +09001556 eui[0] = (ipv4_is_zeronet(addr) || ipv4_is_private_10(addr) ||
1557 ipv4_is_loopback(addr) || ipv4_is_linklocal_169(addr) ||
1558 ipv4_is_private_172(addr) || ipv4_is_test_192(addr) ||
1559 ipv4_is_anycast_6to4(addr) || ipv4_is_private_192(addr) ||
1560 ipv4_is_test_198(addr) || ipv4_is_multicast(addr) ||
1561 ipv4_is_lbcast(addr)) ? 0x00 : 0x02;
1562 eui[1] = 0;
1563 eui[2] = 0x5E;
1564 eui[3] = 0xFE;
1565 memcpy(eui + 4, &addr, 4);
1566 return 0;
1567}
YOSHIFUJI Hideakidfd982b2008-04-10 15:42:09 +09001568
1569static int addrconf_ifid_sit(u8 *eui, struct net_device *dev)
1570{
1571 if (dev->priv_flags & IFF_ISATAP)
1572 return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
1573 return -1;
1574}
1575
stephen hemmingeraee80b52011-06-08 10:44:30 +00001576static int addrconf_ifid_gre(u8 *eui, struct net_device *dev)
1577{
1578 return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
1579}
1580
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
1582{
1583 switch (dev->type) {
1584 case ARPHRD_ETHER:
1585 case ARPHRD_FDDI:
1586 case ARPHRD_IEEE802_TR:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08001587 return addrconf_ifid_eui48(eui, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 case ARPHRD_ARCNET:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08001589 return addrconf_ifid_arcnet(eui, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 case ARPHRD_INFINIBAND:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08001591 return addrconf_ifid_infiniband(eui, dev);
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11001592 case ARPHRD_SIT:
YOSHIFUJI Hideakidfd982b2008-04-10 15:42:09 +09001593 return addrconf_ifid_sit(eui, dev);
stephen hemmingeraee80b52011-06-08 10:44:30 +00001594 case ARPHRD_IPGRE:
1595 return addrconf_ifid_gre(eui, dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001596 case ARPHRD_RAWIP: {
1597 struct in6_addr lladdr;
1598
1599 if (ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
1600 get_random_bytes(eui, 8);
1601 else
1602 memcpy(eui, lladdr.s6_addr + 8, 8);
1603
1604 return 0;
1605 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 }
1607 return -1;
1608}
1609
1610static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
1611{
1612 int err = -1;
1613 struct inet6_ifaddr *ifp;
1614
1615 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00001616 list_for_each_entry(ifp, &idev->addr_list, if_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
1618 memcpy(eui, ifp->addr.s6_addr+8, 8);
1619 err = 0;
1620 break;
1621 }
1622 }
1623 read_unlock_bh(&idev->lock);
1624 return err;
1625}
1626
1627#ifdef CONFIG_IPV6_PRIVACY
1628/* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
1629static int __ipv6_regen_rndid(struct inet6_dev *idev)
1630{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631regen:
YOSHIFUJI Hideaki955189e2006-03-20 16:54:09 -08001632 get_random_bytes(idev->rndid, sizeof(idev->rndid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 idev->rndid[0] &= ~0x02;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634
1635 /*
1636 * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
1637 * check if generated address is not inappropriate
1638 *
1639 * - Reserved subnet anycast (RFC 2526)
1640 * 11111101 11....11 1xxxxxxx
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11001641 * - ISATAP (RFC4214) 6.1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 * 00-00-5E-FE-xx-xx-xx-xx
1643 * - value 0
1644 * - XXX: already assigned to an address on the device
1645 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001646 if (idev->rndid[0] == 0xfd &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff &&
1648 (idev->rndid[7]&0x80))
1649 goto regen;
1650 if ((idev->rndid[0]|idev->rndid[1]) == 0) {
1651 if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe)
1652 goto regen;
1653 if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00)
1654 goto regen;
1655 }
1656
1657 return 0;
1658}
1659
1660static void ipv6_regen_rndid(unsigned long data)
1661{
1662 struct inet6_dev *idev = (struct inet6_dev *) data;
1663 unsigned long expires;
1664
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001665 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 write_lock_bh(&idev->lock);
1667
1668 if (idev->dead)
1669 goto out;
1670
1671 if (__ipv6_regen_rndid(idev) < 0)
1672 goto out;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001673
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 expires = jiffies +
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001675 idev->cnf.temp_prefered_lft * HZ -
Ben Hutchings784e2712010-06-26 11:42:55 +00001676 idev->cnf.regen_max_retry * idev->cnf.dad_transmits * idev->nd_parms->retrans_time -
1677 idev->cnf.max_desync_factor * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 if (time_before(expires, jiffies)) {
1679 printk(KERN_WARNING
1680 "ipv6_regen_rndid(): too short regeneration interval; timer disabled for %s.\n",
1681 idev->dev->name);
1682 goto out;
1683 }
1684
1685 if (!mod_timer(&idev->regen_timer, expires))
1686 in6_dev_hold(idev);
1687
1688out:
1689 write_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001690 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 in6_dev_put(idev);
1692}
1693
1694static int __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr) {
1695 int ret = 0;
1696
1697 if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0)
1698 ret = __ipv6_regen_rndid(idev);
1699 return ret;
1700}
1701#endif
1702
1703/*
1704 * Add prefix route.
1705 */
1706
1707static void
1708addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev,
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -07001709 unsigned long expires, u32 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710{
Thomas Graf86872cb2006-08-22 00:01:08 -07001711 struct fib6_config cfg = {
1712 .fc_table = RT6_TABLE_PREFIX,
1713 .fc_metric = IP6_RT_PRIO_ADDRCONF,
1714 .fc_ifindex = dev->ifindex,
1715 .fc_expires = expires,
1716 .fc_dst_len = plen,
1717 .fc_flags = RTF_UP | flags,
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001718 .fc_nlinfo.nl_net = dev_net(dev),
Stephen Hemmingerf410a1f2008-08-23 05:16:46 -07001719 .fc_protocol = RTPROT_KERNEL,
Thomas Graf86872cb2006-08-22 00:01:08 -07001720 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001722 cfg.fc_dst = *pfx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723
1724 /* Prevent useless cloning on PtP SIT.
1725 This thing is done here expecting that the whole
1726 class of non-broadcast devices need not cloning.
1727 */
Joerg Roedel0be669b2006-10-10 14:49:53 -07001728#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
Thomas Graf86872cb2006-08-22 00:01:08 -07001729 if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT))
1730 cfg.fc_flags |= RTF_NONEXTHOP;
Joerg Roedel0be669b2006-10-10 14:49:53 -07001731#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732
Thomas Graf86872cb2006-08-22 00:01:08 -07001733 ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734}
1735
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00001736
1737static struct rt6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
1738 int plen,
1739 const struct net_device *dev,
1740 u32 flags, u32 noflags)
1741{
1742 struct fib6_node *fn;
1743 struct rt6_info *rt = NULL;
1744 struct fib6_table *table;
1745
1746 table = fib6_get_table(dev_net(dev), RT6_TABLE_PREFIX);
1747 if (table == NULL)
1748 return NULL;
1749
1750 write_lock_bh(&table->tb6_lock);
1751 fn = fib6_locate(&table->tb6_root, pfx, plen, NULL, 0);
1752 if (!fn)
1753 goto out;
1754 for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
David S. Millerd1918542011-12-28 20:19:20 -05001755 if (rt->dst.dev->ifindex != dev->ifindex)
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00001756 continue;
1757 if ((rt->rt6i_flags & flags) != flags)
1758 continue;
Romain Kuntz3b690552013-01-09 15:02:26 +01001759 if ((rt->rt6i_flags & noflags) != 0)
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00001760 continue;
1761 dst_hold(&rt->dst);
1762 break;
1763 }
1764out:
1765 write_unlock_bh(&table->tb6_lock);
1766 return rt;
1767}
1768
1769
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770/* Create "default" multicast route to the interface */
1771
1772static void addrconf_add_mroute(struct net_device *dev)
1773{
Thomas Graf86872cb2006-08-22 00:01:08 -07001774 struct fib6_config cfg = {
1775 .fc_table = RT6_TABLE_LOCAL,
1776 .fc_metric = IP6_RT_PRIO_ADDRCONF,
1777 .fc_ifindex = dev->ifindex,
1778 .fc_dst_len = 8,
1779 .fc_flags = RTF_UP,
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001780 .fc_nlinfo.nl_net = dev_net(dev),
Thomas Graf86872cb2006-08-22 00:01:08 -07001781 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782
Thomas Graf86872cb2006-08-22 00:01:08 -07001783 ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0);
1784
1785 ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786}
1787
Joerg Roedel0be669b2006-10-10 14:49:53 -07001788#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789static void sit_route_add(struct net_device *dev)
1790{
Thomas Graf86872cb2006-08-22 00:01:08 -07001791 struct fib6_config cfg = {
1792 .fc_table = RT6_TABLE_MAIN,
1793 .fc_metric = IP6_RT_PRIO_ADDRCONF,
1794 .fc_ifindex = dev->ifindex,
1795 .fc_dst_len = 96,
1796 .fc_flags = RTF_UP | RTF_NONEXTHOP,
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001797 .fc_nlinfo.nl_net = dev_net(dev),
Thomas Graf86872cb2006-08-22 00:01:08 -07001798 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799
1800 /* prefix length - 96 bits "::d.d.d.d" */
Thomas Graf86872cb2006-08-22 00:01:08 -07001801 ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802}
Joerg Roedel0be669b2006-10-10 14:49:53 -07001803#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804
1805static void addrconf_add_lroute(struct net_device *dev)
1806{
1807 struct in6_addr addr;
1808
1809 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
1810 addrconf_prefix_route(&addr, 64, dev, 0, 0);
1811}
1812
1813static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
1814{
1815 struct inet6_dev *idev;
1816
1817 ASSERT_RTNL();
1818
Stephen Hemmingere21e8462010-03-20 16:09:01 -07001819 idev = ipv6_find_idev(dev);
1820 if (!idev)
Brian Haley64e724f2010-07-20 10:34:30 +00001821 return ERR_PTR(-ENOBUFS);
1822
1823 if (idev->cnf.disable_ipv6)
1824 return ERR_PTR(-EACCES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825
1826 /* Add default multicast route */
Li Wei4af04ab2011-12-06 21:23:45 +00001827 if (!(dev->flags & IFF_LOOPBACK))
1828 addrconf_add_mroute(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829
1830 /* Add link local route */
1831 addrconf_add_lroute(dev);
1832 return idev;
1833}
1834
Neil Hormane6bff992012-01-04 10:49:15 +00001835void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len, bool sllao)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836{
1837 struct prefix_info *pinfo;
1838 __u32 valid_lft;
1839 __u32 prefered_lft;
1840 int addr_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 struct inet6_dev *in6_dev;
Brian Haley56d417b2009-06-01 03:07:33 -07001842 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843
1844 pinfo = (struct prefix_info *) opt;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001845
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 if (len < sizeof(struct prefix_info)) {
1847 ADBG(("addrconf: prefix option too short\n"));
1848 return;
1849 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001850
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 /*
1852 * Validation checks ([ADDRCONF], page 19)
1853 */
1854
1855 addr_type = ipv6_addr_type(&pinfo->prefix);
1856
1857 if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
1858 return;
1859
1860 valid_lft = ntohl(pinfo->valid);
1861 prefered_lft = ntohl(pinfo->prefered);
1862
1863 if (prefered_lft > valid_lft) {
1864 if (net_ratelimit())
1865 printk(KERN_WARNING "addrconf: prefix option has invalid lifetime\n");
1866 return;
1867 }
1868
1869 in6_dev = in6_dev_get(dev);
1870
1871 if (in6_dev == NULL) {
1872 if (net_ratelimit())
1873 printk(KERN_DEBUG "addrconf: device %s not configured\n", dev->name);
1874 return;
1875 }
1876
1877 /*
1878 * Two things going on here:
1879 * 1) Add routes for on-link prefixes
1880 * 2) Configure prefixes with the auto flag set
1881 */
1882
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09001883 if (pinfo->onlink) {
1884 struct rt6_info *rt;
1885 unsigned long rt_expires;
1886
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07001887 /* Avoid arithmetic overflow. Really, we could
1888 * save rt_expires in seconds, likely valid_lft,
1889 * but it would require division in fib gc, that it
1890 * not good.
1891 */
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09001892 if (HZ > USER_HZ)
1893 rt_expires = addrconf_timeout_fixup(valid_lft, HZ);
1894 else
1895 rt_expires = addrconf_timeout_fixup(valid_lft, USER_HZ);
YOSHIFUJI Hideaki3dd4bc62005-12-19 14:02:45 -08001896
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09001897 if (addrconf_finite_timeout(rt_expires))
1898 rt_expires *= HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00001900 rt = addrconf_get_prefix_route(&pinfo->prefix,
1901 pinfo->prefix_len,
1902 dev,
1903 RTF_ADDRCONF | RTF_PREFIX_RT,
1904 RTF_GATEWAY | RTF_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00001906 if (rt) {
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07001907 /* Autoconf prefix route */
1908 if (valid_lft == 0) {
1909 ip6_del_rt(rt);
1910 rt = NULL;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09001911 } else if (addrconf_finite_timeout(rt_expires)) {
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07001912 /* not infinity */
Gao feng1716a962012-04-06 00:13:10 +00001913 rt6_set_expires(rt, jiffies + rt_expires);
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07001914 } else {
Gao feng1716a962012-04-06 00:13:10 +00001915 rt6_clean_expires(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 }
1917 } else if (valid_lft) {
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07001918 clock_t expires = 0;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09001919 int flags = RTF_ADDRCONF | RTF_PREFIX_RT;
1920 if (addrconf_finite_timeout(rt_expires)) {
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07001921 /* not infinity */
1922 flags |= RTF_EXPIRES;
1923 expires = jiffies_to_clock_t(rt_expires);
1924 }
Harout Hedeshian8b2414d2012-12-21 12:38:38 -08001925 if (dev->ip6_ptr->cnf.accept_ra_prefix_route) {
1926 addrconf_prefix_route(&pinfo->prefix,
1927 pinfo->prefix_len, dev, expires, flags);
1928 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 }
1930 if (rt)
Changli Gaod8d1f302010-06-10 23:31:35 -07001931 dst_release(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 }
1933
1934 /* Try to figure out our local address for this prefix */
1935
1936 if (pinfo->autoconf && in6_dev->cnf.autoconf) {
1937 struct inet6_ifaddr * ifp;
1938 struct in6_addr addr;
1939 int create = 0, update_lft = 0;
1940
1941 if (pinfo->prefix_len == 64) {
1942 memcpy(&addr, &pinfo->prefix, 8);
1943 if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
1944 ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
1945 in6_dev_put(in6_dev);
1946 return;
1947 }
1948 goto ok;
1949 }
1950 if (net_ratelimit())
1951 printk(KERN_DEBUG "IPv6 addrconf: prefix with wrong length %d\n",
1952 pinfo->prefix_len);
1953 in6_dev_put(in6_dev);
1954 return;
1955
1956ok:
1957
YOSHIFUJI Hideaki53b79972008-07-19 22:35:03 -07001958 ifp = ipv6_get_ifaddr(net, &addr, dev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959
1960 if (ifp == NULL && valid_lft) {
1961 int max_addresses = in6_dev->cnf.max_addresses;
Neil Horman95c385b2007-04-25 17:08:10 -07001962 u32 addr_flags = 0;
1963
1964#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1965 if (in6_dev->cnf.optimistic_dad &&
Neil Hormane6bff992012-01-04 10:49:15 +00001966 !net->ipv6.devconf_all->forwarding && sllao)
Neil Horman95c385b2007-04-25 17:08:10 -07001967 addr_flags = IFA_F_OPTIMISTIC;
1968#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969
1970 /* Do not allow to create too much of autoconfigured
1971 * addresses; this would be too easy way to crash kernel.
1972 */
1973 if (!max_addresses ||
1974 ipv6_count_addresses(in6_dev) < max_addresses)
1975 ifp = ipv6_add_addr(in6_dev, &addr, pinfo->prefix_len,
Neil Horman95c385b2007-04-25 17:08:10 -07001976 addr_type&IPV6_ADDR_SCOPE_MASK,
1977 addr_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978
1979 if (!ifp || IS_ERR(ifp)) {
1980 in6_dev_put(in6_dev);
1981 return;
1982 }
1983
1984 update_lft = create = 1;
1985 ifp->cstamp = jiffies;
1986 addrconf_dad_start(ifp, RTF_ADDRCONF|RTF_PREFIX_RT);
1987 }
1988
1989 if (ifp) {
1990 int flags;
1991 unsigned long now;
1992#ifdef CONFIG_IPV6_PRIVACY
1993 struct inet6_ifaddr *ift;
1994#endif
1995 u32 stored_lft;
1996
1997 /* update lifetime (RFC2462 5.5.3 e) */
1998 spin_lock(&ifp->lock);
1999 now = jiffies;
2000 if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
2001 stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
2002 else
2003 stored_lft = 0;
2004 if (!update_lft && stored_lft) {
2005 if (valid_lft > MIN_VALID_LIFETIME ||
2006 valid_lft > stored_lft)
2007 update_lft = 1;
2008 else if (stored_lft <= MIN_VALID_LIFETIME) {
2009 /* valid_lft <= stored_lft is always true */
Brian Haleya1ed0522009-07-02 07:10:52 +00002010 /*
2011 * RFC 4862 Section 5.5.3e:
2012 * "Note that the preferred lifetime of
2013 * the corresponding address is always
2014 * reset to the Preferred Lifetime in
2015 * the received Prefix Information
2016 * option, regardless of whether the
2017 * valid lifetime is also reset or
2018 * ignored."
2019 *
2020 * So if the preferred lifetime in
2021 * this advertisement is different
2022 * than what we have stored, but the
2023 * valid lifetime is invalid, just
2024 * reset prefered_lft.
2025 *
2026 * We must set the valid lifetime
2027 * to the stored lifetime since we'll
2028 * be updating the timestamp below,
2029 * else we'll set it back to the
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002030 * minimum.
Brian Haleya1ed0522009-07-02 07:10:52 +00002031 */
2032 if (prefered_lft != ifp->prefered_lft) {
2033 valid_lft = stored_lft;
2034 update_lft = 1;
2035 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 } else {
2037 valid_lft = MIN_VALID_LIFETIME;
2038 if (valid_lft < prefered_lft)
2039 prefered_lft = valid_lft;
2040 update_lft = 1;
2041 }
2042 }
2043
2044 if (update_lft) {
2045 ifp->valid_lft = valid_lft;
2046 ifp->prefered_lft = prefered_lft;
2047 ifp->tstamp = now;
2048 flags = ifp->flags;
2049 ifp->flags &= ~IFA_F_DEPRECATED;
2050 spin_unlock(&ifp->lock);
2051
2052 if (!(flags&IFA_F_TENTATIVE))
2053 ipv6_ifa_notify(0, ifp);
2054 } else
2055 spin_unlock(&ifp->lock);
2056
2057#ifdef CONFIG_IPV6_PRIVACY
2058 read_lock_bh(&in6_dev->lock);
2059 /* update all temporary addresses in the list */
Lorenzo Colitti76f793e2011-07-26 13:50:49 +00002060 list_for_each_entry(ift, &in6_dev->tempaddr_list,
2061 tmp_list) {
2062 int age, max_valid, max_prefered;
2063
Benoit Boissinotc6fbfac2008-04-02 00:00:58 -07002064 if (ifp != ift->ifpub)
2065 continue;
2066
Lorenzo Colitti76f793e2011-07-26 13:50:49 +00002067 /*
2068 * RFC 4941 section 3.3:
2069 * If a received option will extend the lifetime
2070 * of a public address, the lifetimes of
2071 * temporary addresses should be extended,
2072 * subject to the overall constraint that no
2073 * temporary addresses should ever remain
2074 * "valid" or "preferred" for a time longer than
2075 * (TEMP_VALID_LIFETIME) or
2076 * (TEMP_PREFERRED_LIFETIME - DESYNC_FACTOR),
2077 * respectively.
2078 */
2079 age = (now - ift->cstamp) / HZ;
2080 max_valid = in6_dev->cnf.temp_valid_lft - age;
2081 if (max_valid < 0)
2082 max_valid = 0;
2083
2084 max_prefered = in6_dev->cnf.temp_prefered_lft -
2085 in6_dev->cnf.max_desync_factor -
2086 age;
2087 if (max_prefered < 0)
2088 max_prefered = 0;
2089
2090 if (valid_lft > max_valid)
2091 valid_lft = max_valid;
2092
2093 if (prefered_lft > max_prefered)
2094 prefered_lft = max_prefered;
2095
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 spin_lock(&ift->lock);
2097 flags = ift->flags;
Lorenzo Colitti76f793e2011-07-26 13:50:49 +00002098 ift->valid_lft = valid_lft;
2099 ift->prefered_lft = prefered_lft;
2100 ift->tstamp = now;
2101 if (prefered_lft > 0)
2102 ift->flags &= ~IFA_F_DEPRECATED;
2103
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 spin_unlock(&ift->lock);
2105 if (!(flags&IFA_F_TENTATIVE))
2106 ipv6_ifa_notify(0, ift);
2107 }
2108
Glenn Wursteraed65502010-09-27 07:04:30 +00002109 if ((create || list_empty(&in6_dev->tempaddr_list)) && in6_dev->cnf.use_tempaddr > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 /*
Lorenzo Colitti76f793e2011-07-26 13:50:49 +00002111 * When a new public address is created as
2112 * described in [ADDRCONF], also create a new
2113 * temporary address. Also create a temporary
2114 * address if it's enabled but no temporary
2115 * address currently exists.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002117 read_unlock_bh(&in6_dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 ipv6_create_tempaddr(ifp, NULL);
2119 } else {
2120 read_unlock_bh(&in6_dev->lock);
2121 }
2122#endif
2123 in6_ifa_put(ifp);
2124 addrconf_verify(0);
2125 }
2126 }
2127 inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
2128 in6_dev_put(in6_dev);
2129}
2130
2131/*
2132 * Set destination address.
2133 * Special case for SIT interfaces where we create a new "virtual"
2134 * device.
2135 */
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002136int addrconf_set_dstaddr(struct net *net, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137{
2138 struct in6_ifreq ireq;
2139 struct net_device *dev;
2140 int err = -EINVAL;
2141
2142 rtnl_lock();
2143
2144 err = -EFAULT;
2145 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2146 goto err_exit;
2147
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002148 dev = __dev_get_by_index(net, ireq.ifr6_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149
2150 err = -ENODEV;
2151 if (dev == NULL)
2152 goto err_exit;
2153
Joerg Roedel0be669b2006-10-10 14:49:53 -07002154#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 if (dev->type == ARPHRD_SIT) {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -08002156 const struct net_device_ops *ops = dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 struct ifreq ifr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 struct ip_tunnel_parm p;
2159
2160 err = -EADDRNOTAVAIL;
2161 if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4))
2162 goto err_exit;
2163
2164 memset(&p, 0, sizeof(p));
2165 p.iph.daddr = ireq.ifr6_addr.s6_addr32[3];
2166 p.iph.saddr = 0;
2167 p.iph.version = 4;
2168 p.iph.ihl = 5;
2169 p.iph.protocol = IPPROTO_IPV6;
2170 p.iph.ttl = 64;
Stephen Hemmingerd20b3102008-01-21 00:48:43 -08002171 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -08002173 if (ops->ndo_do_ioctl) {
2174 mm_segment_t oldfs = get_fs();
2175
2176 set_fs(KERNEL_DS);
2177 err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
2178 set_fs(oldfs);
2179 } else
2180 err = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181
2182 if (err == 0) {
2183 err = -ENOBUFS;
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002184 dev = __dev_get_by_name(net, p.name);
2185 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 goto err_exit;
2187 err = dev_open(dev);
2188 }
2189 }
Joerg Roedel0be669b2006-10-10 14:49:53 -07002190#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191
2192err_exit:
2193 rtnl_unlock();
2194 return err;
2195}
2196
2197/*
2198 * Manual configuration of address on an interface
2199 */
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002200static int inet6_addr_add(struct net *net, int ifindex, const struct in6_addr *pfx,
Thomas Graf24ef0da2008-05-28 16:54:22 +02002201 unsigned int plen, __u8 ifa_flags, __u32 prefered_lft,
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002202 __u32 valid_lft)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203{
2204 struct inet6_ifaddr *ifp;
2205 struct inet6_dev *idev;
2206 struct net_device *dev;
2207 int scope;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002208 u32 flags;
2209 clock_t expires;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002210 unsigned long timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211
2212 ASSERT_RTNL();
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002213
Thomas Graf24ef0da2008-05-28 16:54:22 +02002214 if (plen > 128)
2215 return -EINVAL;
2216
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002217 /* check the lifetime */
2218 if (!valid_lft || prefered_lft > valid_lft)
2219 return -EINVAL;
2220
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002221 dev = __dev_get_by_index(net, ifindex);
2222 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 return -ENODEV;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002224
Brian Haley64e724f2010-07-20 10:34:30 +00002225 idev = addrconf_add_dev(dev);
2226 if (IS_ERR(idev))
2227 return PTR_ERR(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228
2229 scope = ipv6_addr_scope(pfx);
2230
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002231 timeout = addrconf_timeout_fixup(valid_lft, HZ);
2232 if (addrconf_finite_timeout(timeout)) {
2233 expires = jiffies_to_clock_t(timeout * HZ);
2234 valid_lft = timeout;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002235 flags = RTF_EXPIRES;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002236 } else {
2237 expires = 0;
2238 flags = 0;
2239 ifa_flags |= IFA_F_PERMANENT;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002240 }
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002241
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002242 timeout = addrconf_timeout_fixup(prefered_lft, HZ);
2243 if (addrconf_finite_timeout(timeout)) {
2244 if (timeout == 0)
2245 ifa_flags |= IFA_F_DEPRECATED;
2246 prefered_lft = timeout;
2247 }
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002248
2249 ifp = ipv6_add_addr(idev, pfx, plen, scope, ifa_flags);
2250
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 if (!IS_ERR(ifp)) {
Herbert Xu06aebfb2006-08-09 16:52:04 -07002252 spin_lock_bh(&ifp->lock);
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002253 ifp->valid_lft = valid_lft;
2254 ifp->prefered_lft = prefered_lft;
2255 ifp->tstamp = jiffies;
Herbert Xu06aebfb2006-08-09 16:52:04 -07002256 spin_unlock_bh(&ifp->lock);
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002257
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09002258 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev,
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002259 expires, flags);
Neil Horman95c385b2007-04-25 17:08:10 -07002260 /*
2261 * Note that section 3.1 of RFC 4429 indicates
2262 * that the Optimistic flag should not be set for
2263 * manually configured addresses
2264 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 addrconf_dad_start(ifp, 0);
2266 in6_ifa_put(ifp);
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002267 addrconf_verify(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 return 0;
2269 }
2270
2271 return PTR_ERR(ifp);
2272}
2273
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002274static int inet6_addr_del(struct net *net, int ifindex, const struct in6_addr *pfx,
Thomas Graf24ef0da2008-05-28 16:54:22 +02002275 unsigned int plen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276{
2277 struct inet6_ifaddr *ifp;
2278 struct inet6_dev *idev;
2279 struct net_device *dev;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002280
Thomas Graf24ef0da2008-05-28 16:54:22 +02002281 if (plen > 128)
2282 return -EINVAL;
2283
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002284 dev = __dev_get_by_index(net, ifindex);
2285 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 return -ENODEV;
2287
2288 if ((idev = __in6_dev_get(dev)) == NULL)
2289 return -ENXIO;
2290
2291 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00002292 list_for_each_entry(ifp, &idev->addr_list, if_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 if (ifp->prefix_len == plen &&
2294 ipv6_addr_equal(pfx, &ifp->addr)) {
2295 in6_ifa_hold(ifp);
2296 read_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002297
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 ipv6_del_addr(ifp);
2299
2300 /* If the last address is deleted administratively,
2301 disable IPv6 on this interface.
2302 */
stephen hemminger502a2ff2010-03-17 20:31:13 +00002303 if (list_empty(&idev->addr_list))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 addrconf_ifdown(idev->dev, 1);
2305 return 0;
2306 }
2307 }
2308 read_unlock_bh(&idev->lock);
2309 return -EADDRNOTAVAIL;
2310}
2311
2312
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002313int addrconf_add_ifaddr(struct net *net, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314{
2315 struct in6_ifreq ireq;
2316 int err;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002317
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 if (!capable(CAP_NET_ADMIN))
2319 return -EPERM;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002320
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2322 return -EFAULT;
2323
2324 rtnl_lock();
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002325 err = inet6_addr_add(net, ireq.ifr6_ifindex, &ireq.ifr6_addr,
2326 ireq.ifr6_prefixlen, IFA_F_PERMANENT,
2327 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 rtnl_unlock();
2329 return err;
2330}
2331
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002332int addrconf_del_ifaddr(struct net *net, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333{
2334 struct in6_ifreq ireq;
2335 int err;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002336
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 if (!capable(CAP_NET_ADMIN))
2338 return -EPERM;
2339
2340 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2341 return -EFAULT;
2342
2343 rtnl_lock();
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002344 err = inet6_addr_del(net, ireq.ifr6_ifindex, &ireq.ifr6_addr,
2345 ireq.ifr6_prefixlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 rtnl_unlock();
2347 return err;
2348}
2349
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08002350static void add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
2351 int plen, int scope)
2352{
2353 struct inet6_ifaddr *ifp;
2354
2355 ifp = ipv6_add_addr(idev, addr, plen, scope, IFA_F_PERMANENT);
2356 if (!IS_ERR(ifp)) {
2357 spin_lock_bh(&ifp->lock);
2358 ifp->flags &= ~IFA_F_TENTATIVE;
2359 spin_unlock_bh(&ifp->lock);
2360 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2361 in6_ifa_put(ifp);
2362 }
2363}
2364
Joerg Roedel0be669b2006-10-10 14:49:53 -07002365#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366static void sit_add_v4_addrs(struct inet6_dev *idev)
2367{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 struct in6_addr addr;
2369 struct net_device *dev;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002370 struct net *net = dev_net(idev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 int scope;
2372
2373 ASSERT_RTNL();
2374
2375 memset(&addr, 0, sizeof(struct in6_addr));
2376 memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
2377
2378 if (idev->dev->flags&IFF_POINTOPOINT) {
2379 addr.s6_addr32[0] = htonl(0xfe800000);
2380 scope = IFA_LINK;
2381 } else {
2382 scope = IPV6_ADDR_COMPATv4;
2383 }
2384
2385 if (addr.s6_addr32[3]) {
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08002386 add_addr(idev, &addr, 128, scope);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 return;
2388 }
2389
Benjamin Thery6fda7352008-03-05 10:47:47 -08002390 for_each_netdev(net, dev) {
Herbert Xue5ed6392005-10-03 14:35:55 -07002391 struct in_device * in_dev = __in_dev_get_rtnl(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 if (in_dev && (dev->flags & IFF_UP)) {
2393 struct in_ifaddr * ifa;
2394
2395 int flag = scope;
2396
2397 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
2398 int plen;
2399
2400 addr.s6_addr32[3] = ifa->ifa_local;
2401
2402 if (ifa->ifa_scope == RT_SCOPE_LINK)
2403 continue;
2404 if (ifa->ifa_scope >= RT_SCOPE_HOST) {
2405 if (idev->dev->flags&IFF_POINTOPOINT)
2406 continue;
2407 flag |= IFA_HOST;
2408 }
2409 if (idev->dev->flags&IFF_POINTOPOINT)
2410 plen = 64;
2411 else
2412 plen = 96;
2413
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08002414 add_addr(idev, &addr, plen, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 }
2416 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002417 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418}
Joerg Roedel0be669b2006-10-10 14:49:53 -07002419#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420
2421static void init_loopback(struct net_device *dev)
2422{
2423 struct inet6_dev *idev;
Balakumaran Kannanef95e3d2013-04-02 16:15:05 +05302424 struct net_device *sp_dev;
2425 struct inet6_ifaddr *sp_ifa;
2426 struct rt6_info *sp_rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427
2428 /* ::1 */
2429
2430 ASSERT_RTNL();
2431
2432 if ((idev = ipv6_find_idev(dev)) == NULL) {
2433 printk(KERN_DEBUG "init loopback: add_dev failed\n");
2434 return;
2435 }
2436
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08002437 add_addr(idev, &in6addr_loopback, 128, IFA_HOST);
Balakumaran Kannanef95e3d2013-04-02 16:15:05 +05302438
2439 /* Add routes to other interface's IPv6 addresses */
2440 for_each_netdev(dev_net(dev), sp_dev) {
2441 if (!strcmp(sp_dev->name, dev->name))
2442 continue;
2443
2444 idev = __in6_dev_get(sp_dev);
2445 if (!idev)
2446 continue;
2447
2448 read_lock_bh(&idev->lock);
2449 list_for_each_entry(sp_ifa, &idev->addr_list, if_list) {
2450
2451 if (sp_ifa->flags & (IFA_F_DADFAILED | IFA_F_TENTATIVE))
2452 continue;
2453
Gao fengdabed302013-06-16 11:14:30 +08002454 if (sp_ifa->rt)
2455 continue;
2456
Balakumaran Kannanef95e3d2013-04-02 16:15:05 +05302457 sp_rt = addrconf_dst_alloc(idev, &sp_ifa->addr, 0);
2458
2459 /* Failure cases are ignored */
Gao feng2f4997c2013-06-02 22:16:21 +00002460 if (!IS_ERR(sp_rt)) {
2461 sp_ifa->rt = sp_rt;
Balakumaran Kannanef95e3d2013-04-02 16:15:05 +05302462 ip6_ins_rt(sp_rt);
Gao feng2f4997c2013-06-02 22:16:21 +00002463 }
Balakumaran Kannanef95e3d2013-04-02 16:15:05 +05302464 }
2465 read_unlock_bh(&idev->lock);
2466 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467}
2468
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002469static void addrconf_add_linklocal(struct inet6_dev *idev, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470{
2471 struct inet6_ifaddr * ifp;
Neil Horman95c385b2007-04-25 17:08:10 -07002472 u32 addr_flags = IFA_F_PERMANENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473
Neil Horman95c385b2007-04-25 17:08:10 -07002474#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
2475 if (idev->cnf.optimistic_dad &&
David Miller702beb82008-07-20 18:17:02 -07002476 !dev_net(idev->dev)->ipv6.devconf_all->forwarding)
Neil Horman95c385b2007-04-25 17:08:10 -07002477 addr_flags |= IFA_F_OPTIMISTIC;
2478#endif
2479
2480
2481 ifp = ipv6_add_addr(idev, addr, 64, IFA_LINK, addr_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 if (!IS_ERR(ifp)) {
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09002483 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, idev->dev, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 addrconf_dad_start(ifp, 0);
2485 in6_ifa_put(ifp);
2486 }
2487}
2488
2489static void addrconf_dev_config(struct net_device *dev)
2490{
2491 struct in6_addr addr;
2492 struct inet6_dev * idev;
2493
2494 ASSERT_RTNL();
2495
Herbert Xu74235a22007-06-14 13:02:55 -07002496 if ((dev->type != ARPHRD_ETHER) &&
2497 (dev->type != ARPHRD_FDDI) &&
2498 (dev->type != ARPHRD_IEEE802_TR) &&
2499 (dev->type != ARPHRD_ARCNET) &&
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002500 (dev->type != ARPHRD_RAWIP) &&
Herbert Xu74235a22007-06-14 13:02:55 -07002501 (dev->type != ARPHRD_INFINIBAND)) {
2502 /* Alas, we support only Ethernet autoconfiguration. */
2503 return;
2504 }
2505
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 idev = addrconf_add_dev(dev);
Brian Haley64e724f2010-07-20 10:34:30 +00002507 if (IS_ERR(idev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 return;
2509
2510 memset(&addr, 0, sizeof(struct in6_addr));
2511 addr.s6_addr32[0] = htonl(0xFE800000);
2512
2513 if (ipv6_generate_eui64(addr.s6_addr + 8, dev) == 0)
2514 addrconf_add_linklocal(idev, &addr);
2515}
2516
Joerg Roedel0be669b2006-10-10 14:49:53 -07002517#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518static void addrconf_sit_config(struct net_device *dev)
2519{
2520 struct inet6_dev *idev;
2521
2522 ASSERT_RTNL();
2523
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002524 /*
2525 * Configure the tunnel with one of our IPv4
2526 * addresses... we should configure all of
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 * our v4 addrs in the tunnel
2528 */
2529
2530 if ((idev = ipv6_find_idev(dev)) == NULL) {
2531 printk(KERN_DEBUG "init sit: add_dev failed\n");
2532 return;
2533 }
2534
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11002535 if (dev->priv_flags & IFF_ISATAP) {
2536 struct in6_addr addr;
2537
2538 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
2539 addrconf_prefix_route(&addr, 64, dev, 0, 0);
2540 if (!ipv6_generate_eui64(addr.s6_addr + 8, dev))
2541 addrconf_add_linklocal(idev, &addr);
2542 return;
2543 }
2544
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 sit_add_v4_addrs(idev);
2546
2547 if (dev->flags&IFF_POINTOPOINT) {
2548 addrconf_add_mroute(dev);
2549 addrconf_add_lroute(dev);
2550 } else
2551 sit_route_add(dev);
2552}
Joerg Roedel0be669b2006-10-10 14:49:53 -07002553#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554
stephen hemmingeraee80b52011-06-08 10:44:30 +00002555#if defined(CONFIG_NET_IPGRE) || defined(CONFIG_NET_IPGRE_MODULE)
2556static void addrconf_gre_config(struct net_device *dev)
2557{
2558 struct inet6_dev *idev;
2559 struct in6_addr addr;
2560
2561 pr_info("ipv6: addrconf_gre_config(%s)\n", dev->name);
2562
2563 ASSERT_RTNL();
2564
2565 if ((idev = ipv6_find_idev(dev)) == NULL) {
2566 printk(KERN_DEBUG "init gre: add_dev failed\n");
2567 return;
2568 }
2569
2570 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
2571 addrconf_prefix_route(&addr, 64, dev, 0, 0);
2572
2573 if (!ipv6_generate_eui64(addr.s6_addr + 8, dev))
2574 addrconf_add_linklocal(idev, &addr);
2575}
2576#endif
2577
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578static inline int
2579ipv6_inherit_linklocal(struct inet6_dev *idev, struct net_device *link_dev)
2580{
2581 struct in6_addr lladdr;
2582
Neil Horman95c385b2007-04-25 17:08:10 -07002583 if (!ipv6_get_lladdr(link_dev, &lladdr, IFA_F_TENTATIVE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 addrconf_add_linklocal(idev, &lladdr);
2585 return 0;
2586 }
2587 return -1;
2588}
2589
2590static void ip6_tnl_add_linklocal(struct inet6_dev *idev)
2591{
2592 struct net_device *link_dev;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002593 struct net *net = dev_net(idev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594
2595 /* first try to inherit the link-local address from the link device */
2596 if (idev->dev->iflink &&
Benjamin Thery6fda7352008-03-05 10:47:47 -08002597 (link_dev = __dev_get_by_index(net, idev->dev->iflink))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 if (!ipv6_inherit_linklocal(idev, link_dev))
2599 return;
2600 }
2601 /* then try to inherit it from any device */
Benjamin Thery6fda7352008-03-05 10:47:47 -08002602 for_each_netdev(net, link_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 if (!ipv6_inherit_linklocal(idev, link_dev))
2604 return;
2605 }
2606 printk(KERN_DEBUG "init ip6-ip6: add_linklocal failed\n");
2607}
2608
2609/*
2610 * Autoconfigure tunnel with a link-local address so routing protocols,
2611 * DHCPv6, MLD etc. can be run over the virtual link
2612 */
2613
2614static void addrconf_ip6_tnl_config(struct net_device *dev)
2615{
2616 struct inet6_dev *idev;
2617
2618 ASSERT_RTNL();
2619
Stephen Hemmingere21e8462010-03-20 16:09:01 -07002620 idev = addrconf_add_dev(dev);
Brian Haley64e724f2010-07-20 10:34:30 +00002621 if (IS_ERR(idev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 printk(KERN_DEBUG "init ip6-ip6: add_dev failed\n");
2623 return;
2624 }
2625 ip6_tnl_add_linklocal(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626}
2627
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002628static int addrconf_notify(struct notifier_block *this, unsigned long event,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 void * data)
2630{
2631 struct net_device *dev = (struct net_device *) data;
Herbert Xu74235a22007-06-14 13:02:55 -07002632 struct inet6_dev *idev = __in6_dev_get(dev);
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09002633 int run_pending = 0;
Herbert Xub217d612007-07-30 17:04:52 -07002634 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635
Stephen Hemmingere21e8462010-03-20 16:09:01 -07002636 switch (event) {
YOSHIFUJI Hideaki45ba9dd2007-02-15 02:07:27 +09002637 case NETDEV_REGISTER:
Herbert Xu74235a22007-06-14 13:02:55 -07002638 if (!idev && dev->mtu >= IPV6_MIN_MTU) {
YOSHIFUJI Hideaki45ba9dd2007-02-15 02:07:27 +09002639 idev = ipv6_add_dev(dev);
2640 if (!idev)
Herbert Xub217d612007-07-30 17:04:52 -07002641 return notifier_from_errno(-ENOMEM);
YOSHIFUJI Hideaki45ba9dd2007-02-15 02:07:27 +09002642 }
2643 break;
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07002644
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 case NETDEV_UP:
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002646 case NETDEV_CHANGE:
Jay Vosburghc2edacf2007-07-09 10:42:47 -07002647 if (dev->flags & IFF_SLAVE)
2648 break;
2649
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002650 if (event == NETDEV_UP) {
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -07002651 if (!addrconf_qdisc_ok(dev)) {
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002652 /* device is not ready yet. */
2653 printk(KERN_INFO
2654 "ADDRCONF(NETDEV_UP): %s: "
2655 "link is not ready\n",
2656 dev->name);
2657 break;
2658 }
Kristian Slavov99081042006-02-08 16:10:53 -08002659
Evgeniy Polyakovd31c7b82007-11-30 23:36:08 +11002660 if (!idev && dev->mtu >= IPV6_MIN_MTU)
2661 idev = ipv6_add_dev(dev);
2662
Benjamin Therye3ec6cf2008-11-05 01:43:57 -08002663 if (idev) {
Kristian Slavov99081042006-02-08 16:10:53 -08002664 idev->if_flags |= IF_READY;
Benjamin Therye3ec6cf2008-11-05 01:43:57 -08002665 run_pending = 1;
2666 }
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002667 } else {
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -07002668 if (!addrconf_qdisc_ok(dev)) {
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002669 /* device is still not ready. */
2670 break;
2671 }
2672
2673 if (idev) {
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07002674 if (idev->if_flags & IF_READY)
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002675 /* device is already configured. */
2676 break;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002677 idev->if_flags |= IF_READY;
2678 }
2679
2680 printk(KERN_INFO
2681 "ADDRCONF(NETDEV_CHANGE): %s: "
2682 "link becomes ready\n",
2683 dev->name);
2684
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09002685 run_pending = 1;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002686 }
2687
Stephen Hemmingere21e8462010-03-20 16:09:01 -07002688 switch (dev->type) {
Joerg Roedel0be669b2006-10-10 14:49:53 -07002689#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 case ARPHRD_SIT:
2691 addrconf_sit_config(dev);
2692 break;
Joerg Roedel0be669b2006-10-10 14:49:53 -07002693#endif
stephen hemmingeraee80b52011-06-08 10:44:30 +00002694#if defined(CONFIG_NET_IPGRE) || defined(CONFIG_NET_IPGRE_MODULE)
2695 case ARPHRD_IPGRE:
2696 addrconf_gre_config(dev);
2697 break;
2698#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 case ARPHRD_TUNNEL6:
2700 addrconf_ip6_tnl_config(dev);
2701 break;
2702 case ARPHRD_LOOPBACK:
2703 init_loopback(dev);
2704 break;
2705
2706 default:
2707 addrconf_dev_config(dev);
2708 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002709 }
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07002710
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 if (idev) {
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09002712 if (run_pending)
2713 addrconf_dad_run(idev);
2714
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07002715 /*
2716 * If the MTU changed during the interface down,
2717 * when the interface up, the changed MTU must be
2718 * reflected in the idev as well as routers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 */
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07002720 if (idev->cnf.mtu6 != dev->mtu &&
2721 dev->mtu >= IPV6_MIN_MTU) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 rt6_mtu_change(dev, dev->mtu);
2723 idev->cnf.mtu6 = dev->mtu;
2724 }
2725 idev->tstamp = jiffies;
2726 inet6_ifinfo_notify(RTM_NEWLINK, idev);
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07002727
2728 /*
2729 * If the changed mtu during down is lower than
2730 * IPV6_MIN_MTU stop IPv6 on this interface.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 */
2732 if (dev->mtu < IPV6_MIN_MTU)
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07002733 addrconf_ifdown(dev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 }
2735 break;
2736
2737 case NETDEV_CHANGEMTU:
Evgeniy Polyakovd31c7b82007-11-30 23:36:08 +11002738 if (idev && dev->mtu >= IPV6_MIN_MTU) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 rt6_mtu_change(dev, dev->mtu);
2740 idev->cnf.mtu6 = dev->mtu;
2741 break;
2742 }
2743
Evgeniy Polyakovd31c7b82007-11-30 23:36:08 +11002744 if (!idev && dev->mtu >= IPV6_MIN_MTU) {
2745 idev = ipv6_add_dev(dev);
2746 if (idev)
2747 break;
2748 }
2749
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07002750 /*
2751 * MTU falled under IPV6_MIN_MTU.
2752 * Stop IPv6 on this interface.
2753 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754
2755 case NETDEV_DOWN:
2756 case NETDEV_UNREGISTER:
2757 /*
2758 * Remove all addresses from this interface.
2759 */
2760 addrconf_ifdown(dev, event != NETDEV_DOWN);
2761 break;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002762
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 case NETDEV_CHANGENAME:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 if (idev) {
Stephen Hemminger5632c512007-04-28 21:16:39 -07002765 snmp6_unregister_dev(idev);
Pavel Emelyanov408c4762008-01-10 17:41:21 -08002766 addrconf_sysctl_unregister(idev);
Pavel Emelyanovf52295a2007-12-02 00:58:37 +11002767 addrconf_sysctl_register(idev);
Herbert Xub217d612007-07-30 17:04:52 -07002768 err = snmp6_register_dev(idev);
2769 if (err)
2770 return notifier_from_errno(err);
Stephen Hemminger5632c512007-04-28 21:16:39 -07002771 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 break;
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07002773
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00002774 case NETDEV_PRE_TYPE_CHANGE:
2775 case NETDEV_POST_TYPE_CHANGE:
2776 addrconf_type_change(dev, event);
Moni Shoua75c78502009-09-15 02:37:40 -07002777 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002778 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779
2780 return NOTIFY_OK;
2781}
2782
2783/*
2784 * addrconf module should be notified of a device going up
2785 */
2786static struct notifier_block ipv6_dev_notf = {
2787 .notifier_call = addrconf_notify,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788};
2789
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00002790static void addrconf_type_change(struct net_device *dev, unsigned long event)
Moni Shoua75c78502009-09-15 02:37:40 -07002791{
2792 struct inet6_dev *idev;
2793 ASSERT_RTNL();
2794
2795 idev = __in6_dev_get(dev);
2796
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00002797 if (event == NETDEV_POST_TYPE_CHANGE)
Moni Shoua75c78502009-09-15 02:37:40 -07002798 ipv6_mc_remap(idev);
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00002799 else if (event == NETDEV_PRE_TYPE_CHANGE)
Moni Shoua75c78502009-09-15 02:37:40 -07002800 ipv6_mc_unmap(idev);
2801}
2802
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803static int addrconf_ifdown(struct net_device *dev, int how)
2804{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002805 struct net *net = dev_net(dev);
stephen hemminger502a2ff2010-03-17 20:31:13 +00002806 struct inet6_dev *idev;
2807 struct inet6_ifaddr *ifa;
David S. Miller73a8bd72011-01-23 23:27:15 -08002808 int state, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809
2810 ASSERT_RTNL();
2811
David S. Miller73a8bd72011-01-23 23:27:15 -08002812 rt6_ifdown(net, dev);
2813 neigh_ifdown(&nd_tbl, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814
2815 idev = __in6_dev_get(dev);
2816 if (idev == NULL)
2817 return -ENODEV;
2818
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07002819 /*
2820 * Step 1: remove reference to ipv6 device from parent device.
2821 * Do not dev_put!
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 */
Denis V. Lunev439e2382008-04-03 13:30:17 -07002823 if (how) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 idev->dead = 1;
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07002825
2826 /* protected by rtnl_lock */
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00002827 RCU_INIT_POINTER(dev->ip6_ptr, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828
2829 /* Step 1.5: remove snmp6 entry */
2830 snmp6_unregister_dev(idev);
2831
2832 }
2833
David S. Miller73a8bd72011-01-23 23:27:15 -08002834 /* Step 2: clear hash table */
2835 for (i = 0; i < IN6_ADDR_HSIZE; i++) {
2836 struct hlist_head *h = &inet6_addr_lst[i];
2837 struct hlist_node *n;
2838
2839 spin_lock_bh(&addrconf_hash_lock);
2840 restart:
2841 hlist_for_each_entry_rcu(ifa, n, h, addr_lst) {
2842 if (ifa->idev == idev) {
2843 hlist_del_init_rcu(&ifa->addr_lst);
2844 addrconf_del_timer(ifa);
2845 goto restart;
2846 }
2847 }
2848 spin_unlock_bh(&addrconf_hash_lock);
2849 }
2850
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 write_lock_bh(&idev->lock);
2852
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07002853 /* Step 2: clear flags for stateless addrconf */
Denis V. Lunev439e2382008-04-03 13:30:17 -07002854 if (!how)
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002855 idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857#ifdef CONFIG_IPV6_PRIVACY
Denis V. Lunev439e2382008-04-03 13:30:17 -07002858 if (how && del_timer(&idev->regen_timer))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 in6_dev_put(idev);
2860
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07002861 /* Step 3: clear tempaddr list */
stephen hemminger372e6c82010-03-17 20:31:09 +00002862 while (!list_empty(&idev->tempaddr_list)) {
2863 ifa = list_first_entry(&idev->tempaddr_list,
2864 struct inet6_ifaddr, tmp_list);
2865 list_del(&ifa->tmp_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 write_unlock_bh(&idev->lock);
2867 spin_lock_bh(&ifa->lock);
2868
2869 if (ifa->ifpub) {
2870 in6_ifa_put(ifa->ifpub);
2871 ifa->ifpub = NULL;
2872 }
2873 spin_unlock_bh(&ifa->lock);
2874 in6_ifa_put(ifa);
2875 write_lock_bh(&idev->lock);
2876 }
2877#endif
stephen hemminger8f37ada2010-03-03 08:19:59 +00002878
stephen hemminger502a2ff2010-03-17 20:31:13 +00002879 while (!list_empty(&idev->addr_list)) {
2880 ifa = list_first_entry(&idev->addr_list,
2881 struct inet6_ifaddr, if_list);
stephen hemminger84e8b802010-03-02 13:32:46 +00002882 addrconf_del_timer(ifa);
2883
David S. Miller73a8bd72011-01-23 23:27:15 -08002884 list_del(&ifa->if_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885
David S. Miller73a8bd72011-01-23 23:27:15 -08002886 write_unlock_bh(&idev->lock);
stephen hemminger84e8b802010-03-02 13:32:46 +00002887
David S. Miller73a8bd72011-01-23 23:27:15 -08002888 spin_lock_bh(&ifa->state_lock);
2889 state = ifa->state;
2890 ifa->state = INET6_IFADDR_STATE_DEAD;
2891 spin_unlock_bh(&ifa->state_lock);
stephen hemminger84e8b802010-03-02 13:32:46 +00002892
David S. Miller73a8bd72011-01-23 23:27:15 -08002893 if (state != INET6_IFADDR_STATE_DEAD) {
2894 __ipv6_ifa_notify(RTM_DELADDR, ifa);
2895 atomic_notifier_call_chain(&inet6addr_chain, NETDEV_DOWN, ifa);
stephen hemminger27bdb2a2010-04-12 05:41:32 +00002896 }
David S. Miller73a8bd72011-01-23 23:27:15 -08002897 in6_ifa_put(ifa);
stephen hemminger8f37ada2010-03-03 08:19:59 +00002898
David S. Miller73a8bd72011-01-23 23:27:15 -08002899 write_lock_bh(&idev->lock);
2900 }
stephen hemminger8f37ada2010-03-03 08:19:59 +00002901
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 write_unlock_bh(&idev->lock);
2903
2904 /* Step 5: Discard multicast list */
Denis V. Lunev439e2382008-04-03 13:30:17 -07002905 if (how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 ipv6_mc_destroy_dev(idev);
2907 else
2908 ipv6_mc_down(idev);
2909
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 idev->tstamp = jiffies;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002911
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07002912 /* Last: Shot the device (if unregistered) */
Denis V. Lunev439e2382008-04-03 13:30:17 -07002913 if (how) {
Pavel Emelyanov408c4762008-01-10 17:41:21 -08002914 addrconf_sysctl_unregister(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915 neigh_parms_release(&nd_tbl, idev->nd_parms);
2916 neigh_ifdown(&nd_tbl, dev);
2917 in6_dev_put(idev);
2918 }
2919 return 0;
2920}
2921
2922static void addrconf_rs_timer(unsigned long data)
2923{
2924 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
stephen hemminger5b2a1952010-03-02 13:32:45 +00002925 struct inet6_dev *idev = ifp->idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926
stephen hemminger5b2a1952010-03-02 13:32:45 +00002927 read_lock(&idev->lock);
2928 if (idev->dead || !(idev->if_flags & IF_READY))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 goto out;
2930
stephen hemminger5b2a1952010-03-02 13:32:45 +00002931 if (idev->cnf.forwarding)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 goto out;
stephen hemminger5b2a1952010-03-02 13:32:45 +00002933
2934 /* Announcement received after solicitation was sent */
2935 if (idev->if_flags & IF_RA_RCVD)
2936 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937
2938 spin_lock(&ifp->lock);
stephen hemminger5b2a1952010-03-02 13:32:45 +00002939 if (ifp->probes++ < idev->cnf.rtr_solicits) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 /* The wait after the last probe can be shorter */
2941 addrconf_mod_timer(ifp, AC_RS,
stephen hemminger5b2a1952010-03-02 13:32:45 +00002942 (ifp->probes == idev->cnf.rtr_solicits) ?
2943 idev->cnf.rtr_solicit_delay :
2944 idev->cnf.rtr_solicit_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 spin_unlock(&ifp->lock);
2946
stephen hemminger5b2a1952010-03-02 13:32:45 +00002947 ndisc_send_rs(idev->dev, &ifp->addr, &in6addr_linklocal_allrouters);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 } else {
2949 spin_unlock(&ifp->lock);
2950 /*
2951 * Note: we do not support deprecated "all on-link"
2952 * assumption any longer.
2953 */
2954 printk(KERN_DEBUG "%s: no IPv6 routers present\n",
stephen hemminger5b2a1952010-03-02 13:32:45 +00002955 idev->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 }
2957
2958out:
stephen hemminger5b2a1952010-03-02 13:32:45 +00002959 read_unlock(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 in6_ifa_put(ifp);
2961}
2962
2963/*
2964 * Duplicate Address Detection
2965 */
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002966static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
2967{
2968 unsigned long rand_num;
2969 struct inet6_dev *idev = ifp->idev;
2970
Neil Horman95c385b2007-04-25 17:08:10 -07002971 if (ifp->flags & IFA_F_OPTIMISTIC)
2972 rand_num = 0;
2973 else
2974 rand_num = net_random() % (idev->cnf.rtr_solicit_delay ? : 1);
2975
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002976 ifp->probes = idev->cnf.dad_transmits;
2977 addrconf_mod_timer(ifp, AC_DAD, rand_num);
2978}
2979
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -07002980static void addrconf_dad_start(struct inet6_ifaddr *ifp, u32 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981{
2982 struct inet6_dev *idev = ifp->idev;
2983 struct net_device *dev = idev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984
2985 addrconf_join_solict(dev, &ifp->addr);
2986
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 net_srandom(ifp->addr.s6_addr32[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988
2989 read_lock_bh(&idev->lock);
Herbert Xu622ccdf2010-05-18 15:56:06 -07002990 spin_lock(&ifp->lock);
Herbert Xue9d3e082010-05-18 15:36:06 -07002991 if (ifp->state == INET6_IFADDR_STATE_DEAD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993
2994 if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09002995 idev->cnf.accept_dad < 1 ||
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07002996 !(ifp->flags&IFA_F_TENTATIVE) ||
2997 ifp->flags & IFA_F_NODAD) {
Brian Haleycc411d02009-09-09 14:41:32 +00002998 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
stephen hemminger21809fa2010-02-08 19:48:52 +00002999 spin_unlock(&ifp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 read_unlock_bh(&idev->lock);
3001
3002 addrconf_dad_completed(ifp);
3003 return;
3004 }
3005
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08003006 if (!(idev->if_flags & IF_READY)) {
stephen hemminger21809fa2010-02-08 19:48:52 +00003007 spin_unlock(&ifp->lock);
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08003008 read_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003009 /*
Masatake YAMATO590a9882009-06-09 10:41:12 +09003010 * If the device is not ready:
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003011 * - keep it tentative if it is a permanent address.
3012 * - otherwise, kill it.
3013 */
3014 in6_ifa_hold(ifp);
Brian Haleycc411d02009-09-09 14:41:32 +00003015 addrconf_dad_stop(ifp, 0);
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08003016 return;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003017 }
Neil Horman95c385b2007-04-25 17:08:10 -07003018
3019 /*
3020 * Optimistic nodes can start receiving
3021 * Frames right away
3022 */
Stephen Hemmingere21e8462010-03-20 16:09:01 -07003023 if (ifp->flags & IFA_F_OPTIMISTIC)
Neil Horman95c385b2007-04-25 17:08:10 -07003024 ip6_ins_rt(ifp->rt);
3025
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08003026 addrconf_dad_kick(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027out:
Herbert Xu622ccdf2010-05-18 15:56:06 -07003028 spin_unlock(&ifp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 read_unlock_bh(&idev->lock);
3030}
3031
3032static void addrconf_dad_timer(unsigned long data)
3033{
3034 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
3035 struct inet6_dev *idev = ifp->idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 struct in6_addr mcaddr;
3037
Herbert Xuf2344a12010-05-18 15:55:27 -07003038 if (!ifp->probes && addrconf_dad_end(ifp))
3039 goto out;
3040
stephen hemminger122e4512010-03-02 13:32:44 +00003041 read_lock(&idev->lock);
3042 if (idev->dead || !(idev->if_flags & IF_READY)) {
3043 read_unlock(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 goto out;
3045 }
stephen hemminger21809fa2010-02-08 19:48:52 +00003046
3047 spin_lock(&ifp->lock);
Herbert Xu622ccdf2010-05-18 15:56:06 -07003048 if (ifp->state == INET6_IFADDR_STATE_DEAD) {
3049 spin_unlock(&ifp->lock);
3050 read_unlock(&idev->lock);
3051 goto out;
3052 }
3053
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 if (ifp->probes == 0) {
3055 /*
3056 * DAD was successful
3057 */
3058
Brian Haleycc411d02009-09-09 14:41:32 +00003059 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
stephen hemminger21809fa2010-02-08 19:48:52 +00003060 spin_unlock(&ifp->lock);
stephen hemminger122e4512010-03-02 13:32:44 +00003061 read_unlock(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062
3063 addrconf_dad_completed(ifp);
3064
3065 goto out;
3066 }
3067
3068 ifp->probes--;
3069 addrconf_mod_timer(ifp, AC_DAD, ifp->idev->nd_parms->retrans_time);
stephen hemminger21809fa2010-02-08 19:48:52 +00003070 spin_unlock(&ifp->lock);
stephen hemminger122e4512010-03-02 13:32:44 +00003071 read_unlock(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072
3073 /* send a neighbour solicitation for our addr */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
YOSHIFUJI Hideakid7aabf22008-04-10 15:42:11 +09003075 ndisc_send_ns(ifp->idev->dev, NULL, &ifp->addr, &mcaddr, &in6addr_any);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076out:
3077 in6_ifa_put(ifp);
3078}
3079
3080static void addrconf_dad_completed(struct inet6_ifaddr *ifp)
3081{
Stephen Hemmingere21e8462010-03-20 16:09:01 -07003082 struct net_device *dev = ifp->idev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083
3084 /*
3085 * Configure the address for reception. Now it is valid.
3086 */
3087
3088 ipv6_ifa_notify(RTM_NEWADDR, ifp);
3089
Tore Anderson026359b2011-08-28 23:47:33 +00003090 /* If added prefix is link local and we are prepared to process
3091 router advertisements, start sending router solicitations.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 */
3093
Tore Anderson026359b2011-08-28 23:47:33 +00003094 if (((ifp->idev->cnf.accept_ra == 1 && !ifp->idev->cnf.forwarding) ||
3095 ifp->idev->cnf.accept_ra == 2) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 ifp->idev->cnf.rtr_solicits > 0 &&
3097 (dev->flags&IFF_LOOPBACK) == 0 &&
3098 (ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 /*
3100 * If a host as already performed a random delay
3101 * [...] as part of DAD [...] there is no need
3102 * to delay again before sending the first RS
3103 */
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +09003104 ndisc_send_rs(ifp->idev->dev, &ifp->addr, &in6addr_linklocal_allrouters);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105
3106 spin_lock_bh(&ifp->lock);
3107 ifp->probes = 1;
3108 ifp->idev->if_flags |= IF_RS_SENT;
3109 addrconf_mod_timer(ifp, AC_RS, ifp->idev->cnf.rtr_solicit_interval);
3110 spin_unlock_bh(&ifp->lock);
3111 }
3112}
3113
Stephen Hemmingere21e8462010-03-20 16:09:01 -07003114static void addrconf_dad_run(struct inet6_dev *idev)
3115{
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09003116 struct inet6_ifaddr *ifp;
3117
3118 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00003119 list_for_each_entry(ifp, &idev->addr_list, if_list) {
stephen hemminger21809fa2010-02-08 19:48:52 +00003120 spin_lock(&ifp->lock);
Herbert Xuf2344a12010-05-18 15:55:27 -07003121 if (ifp->flags & IFA_F_TENTATIVE &&
3122 ifp->state == INET6_IFADDR_STATE_DAD)
3123 addrconf_dad_kick(ifp);
stephen hemminger21809fa2010-02-08 19:48:52 +00003124 spin_unlock(&ifp->lock);
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09003125 }
3126 read_unlock_bh(&idev->lock);
3127}
3128
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129#ifdef CONFIG_PROC_FS
3130struct if6_iter_state {
Daniel Lezcano3c400902008-01-10 22:42:49 -08003131 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132 int bucket;
Mihai Maruseac1d578302012-01-03 23:31:35 +00003133 int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134};
3135
Mihai Maruseac1d578302012-01-03 23:31:35 +00003136static struct inet6_ifaddr *if6_get_first(struct seq_file *seq, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137{
3138 struct inet6_ifaddr *ifa = NULL;
3139 struct if6_iter_state *state = seq->private;
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09003140 struct net *net = seq_file_net(seq);
Mihai Maruseac1d578302012-01-03 23:31:35 +00003141 int p = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142
Mihai Maruseac1d578302012-01-03 23:31:35 +00003143 /* initial bucket if pos is 0 */
3144 if (pos == 0) {
3145 state->bucket = 0;
3146 state->offset = 0;
3147 }
3148
3149 for (; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
stephen hemmingerc2e21292010-03-17 20:31:10 +00003150 struct hlist_node *n;
Eric Dumazet4f70ecc2010-05-03 10:50:14 +00003151 hlist_for_each_entry_rcu_bh(ifa, n, &inet6_addr_lst[state->bucket],
Mihai Maruseac1d578302012-01-03 23:31:35 +00003152 addr_lst) {
Eric Dumazetdd2c50e2012-10-16 07:37:27 +00003153 if (!net_eq(dev_net(ifa->idev->dev), net))
3154 continue;
Mihai Maruseac1d578302012-01-03 23:31:35 +00003155 /* sync with offset */
3156 if (p < state->offset) {
3157 p++;
3158 continue;
3159 }
3160 state->offset++;
Eric Dumazetdd2c50e2012-10-16 07:37:27 +00003161 return ifa;
Mihai Maruseac1d578302012-01-03 23:31:35 +00003162 }
3163
3164 /* prepare for next bucket */
3165 state->offset = 0;
3166 p = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 }
stephen hemmingerc2e21292010-03-17 20:31:10 +00003168 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169}
3170
stephen hemmingerc2e21292010-03-17 20:31:10 +00003171static struct inet6_ifaddr *if6_get_next(struct seq_file *seq,
3172 struct inet6_ifaddr *ifa)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173{
3174 struct if6_iter_state *state = seq->private;
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09003175 struct net *net = seq_file_net(seq);
stephen hemmingerc2e21292010-03-17 20:31:10 +00003176 struct hlist_node *n = &ifa->addr_lst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177
Mihai Maruseac1d578302012-01-03 23:31:35 +00003178 hlist_for_each_entry_continue_rcu_bh(ifa, n, addr_lst) {
Eric Dumazetdd2c50e2012-10-16 07:37:27 +00003179 if (!net_eq(dev_net(ifa->idev->dev), net))
3180 continue;
Mihai Maruseac1d578302012-01-03 23:31:35 +00003181 state->offset++;
Eric Dumazetdd2c50e2012-10-16 07:37:27 +00003182 return ifa;
Mihai Maruseac1d578302012-01-03 23:31:35 +00003183 }
stephen hemmingerc2e21292010-03-17 20:31:10 +00003184
3185 while (++state->bucket < IN6_ADDR_HSIZE) {
Mihai Maruseac1d578302012-01-03 23:31:35 +00003186 state->offset = 0;
Eric Dumazet4f70ecc2010-05-03 10:50:14 +00003187 hlist_for_each_entry_rcu_bh(ifa, n,
stephen hemmingerc2e21292010-03-17 20:31:10 +00003188 &inet6_addr_lst[state->bucket], addr_lst) {
Eric Dumazetdd2c50e2012-10-16 07:37:27 +00003189 if (!net_eq(dev_net(ifa->idev->dev), net))
3190 continue;
Mihai Maruseac1d578302012-01-03 23:31:35 +00003191 state->offset++;
Eric Dumazetdd2c50e2012-10-16 07:37:27 +00003192 return ifa;
Daniel Lezcano3c400902008-01-10 22:42:49 -08003193 }
3194 }
3195
stephen hemmingerc2e21292010-03-17 20:31:10 +00003196 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197}
3198
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
Eric Dumazet4f70ecc2010-05-03 10:50:14 +00003200 __acquires(rcu_bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201{
stephen hemminger5c578ae2010-03-17 20:31:11 +00003202 rcu_read_lock_bh();
Mihai Maruseac1d578302012-01-03 23:31:35 +00003203 return if6_get_first(seq, *pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204}
3205
3206static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3207{
3208 struct inet6_ifaddr *ifa;
3209
3210 ifa = if6_get_next(seq, v);
3211 ++*pos;
3212 return ifa;
3213}
3214
3215static void if6_seq_stop(struct seq_file *seq, void *v)
Eric Dumazet4f70ecc2010-05-03 10:50:14 +00003216 __releases(rcu_bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217{
stephen hemminger5c578ae2010-03-17 20:31:11 +00003218 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219}
3220
3221static int if6_seq_show(struct seq_file *seq, void *v)
3222{
3223 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
Harvey Harrison4b7a4272008-10-29 12:50:24 -07003224 seq_printf(seq, "%pi6 %02x %02x %02x %02x %8s\n",
Harvey Harrisonb0711952008-10-28 16:05:40 -07003225 &ifp->addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226 ifp->idev->dev->ifindex,
3227 ifp->prefix_len,
3228 ifp->scope,
3229 ifp->flags,
3230 ifp->idev->dev->name);
3231 return 0;
3232}
3233
Philippe De Muyter56b3d972007-07-10 23:07:31 -07003234static const struct seq_operations if6_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235 .start = if6_seq_start,
3236 .next = if6_seq_next,
3237 .show = if6_seq_show,
3238 .stop = if6_seq_stop,
3239};
3240
3241static int if6_seq_open(struct inode *inode, struct file *file)
3242{
Daniel Lezcano3c400902008-01-10 22:42:49 -08003243 return seq_open_net(inode, file, &if6_seq_ops,
3244 sizeof(struct if6_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245}
3246
Arjan van de Ven9a321442007-02-12 00:55:35 -08003247static const struct file_operations if6_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 .owner = THIS_MODULE,
3249 .open = if6_seq_open,
3250 .read = seq_read,
3251 .llseek = seq_lseek,
Daniel Lezcano3c400902008-01-10 22:42:49 -08003252 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253};
3254
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003255static int __net_init if6_proc_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256{
Daniel Lezcano3c400902008-01-10 22:42:49 -08003257 if (!proc_net_fops_create(net, "if_inet6", S_IRUGO, &if6_fops))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258 return -ENOMEM;
3259 return 0;
3260}
3261
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003262static void __net_exit if6_proc_net_exit(struct net *net)
Daniel Lezcano3c400902008-01-10 22:42:49 -08003263{
3264 proc_net_remove(net, "if_inet6");
3265}
3266
3267static struct pernet_operations if6_proc_net_ops = {
3268 .init = if6_proc_net_init,
3269 .exit = if6_proc_net_exit,
3270};
3271
3272int __init if6_proc_init(void)
3273{
3274 return register_pernet_subsys(&if6_proc_net_ops);
3275}
3276
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277void if6_proc_exit(void)
3278{
Daniel Lezcano3c400902008-01-10 22:42:49 -08003279 unregister_pernet_subsys(&if6_proc_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280}
3281#endif /* CONFIG_PROC_FS */
3282
Masahide NAKAMURA59fbb3a2007-06-26 23:56:32 -07003283#if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07003284/* Check if address is a home address configured on any interface. */
Eric Dumazetb71d1d42011-04-22 04:53:02 +00003285int ipv6_chk_home_addr(struct net *net, const struct in6_addr *addr)
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07003286{
3287 int ret = 0;
stephen hemmingerc2e21292010-03-17 20:31:10 +00003288 struct inet6_ifaddr *ifp = NULL;
3289 struct hlist_node *n;
stephen hemminger3a88a812010-03-17 20:31:12 +00003290 unsigned int hash = ipv6_addr_hash(addr);
stephen hemmingerc2e21292010-03-17 20:31:10 +00003291
stephen hemminger5c578ae2010-03-17 20:31:11 +00003292 rcu_read_lock_bh();
Eric Dumazet4f70ecc2010-05-03 10:50:14 +00003293 hlist_for_each_entry_rcu_bh(ifp, n, &inet6_addr_lst[hash], addr_lst) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09003294 if (!net_eq(dev_net(ifp->idev->dev), net))
Daniel Lezcano389f6612008-01-10 22:44:40 -08003295 continue;
YOSHIFUJI Hideakicaad2952008-04-10 15:42:07 +09003296 if (ipv6_addr_equal(&ifp->addr, addr) &&
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07003297 (ifp->flags & IFA_F_HOMEADDRESS)) {
3298 ret = 1;
3299 break;
3300 }
3301 }
stephen hemminger5c578ae2010-03-17 20:31:11 +00003302 rcu_read_unlock_bh();
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07003303 return ret;
3304}
3305#endif
3306
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307/*
3308 * Periodic address status verification
3309 */
3310
3311static void addrconf_verify(unsigned long foo)
3312{
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -07003313 unsigned long now, next, next_sec, next_sched;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314 struct inet6_ifaddr *ifp;
stephen hemmingerc2e21292010-03-17 20:31:10 +00003315 struct hlist_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 int i;
3317
stephen hemminger5c578ae2010-03-17 20:31:11 +00003318 rcu_read_lock_bh();
3319 spin_lock(&addrconf_verify_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320 now = jiffies;
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -07003321 next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322
3323 del_timer(&addr_chk_timer);
3324
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003325 for (i = 0; i < IN6_ADDR_HSIZE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326restart:
Eric Dumazet4f70ecc2010-05-03 10:50:14 +00003327 hlist_for_each_entry_rcu_bh(ifp, node,
stephen hemminger5c578ae2010-03-17 20:31:11 +00003328 &inet6_addr_lst[i], addr_lst) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329 unsigned long age;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330
3331 if (ifp->flags & IFA_F_PERMANENT)
3332 continue;
3333
3334 spin_lock(&ifp->lock);
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -07003335 /* We try to batch several events at once. */
3336 age = (now - ifp->tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337
YOSHIFUJI Hideaki8f27ebb2006-07-28 18:12:11 +09003338 if (ifp->valid_lft != INFINITY_LIFE_TIME &&
3339 age >= ifp->valid_lft) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 spin_unlock(&ifp->lock);
3341 in6_ifa_hold(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342 ipv6_del_addr(ifp);
3343 goto restart;
YOSHIFUJI Hideaki8f27ebb2006-07-28 18:12:11 +09003344 } else if (ifp->prefered_lft == INFINITY_LIFE_TIME) {
3345 spin_unlock(&ifp->lock);
3346 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347 } else if (age >= ifp->prefered_lft) {
Brian Haleya1ed0522009-07-02 07:10:52 +00003348 /* jiffies - ifp->tstamp > age >= ifp->prefered_lft */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349 int deprecate = 0;
3350
3351 if (!(ifp->flags&IFA_F_DEPRECATED)) {
3352 deprecate = 1;
3353 ifp->flags |= IFA_F_DEPRECATED;
3354 }
3355
3356 if (time_before(ifp->tstamp + ifp->valid_lft * HZ, next))
3357 next = ifp->tstamp + ifp->valid_lft * HZ;
3358
3359 spin_unlock(&ifp->lock);
3360
3361 if (deprecate) {
3362 in6_ifa_hold(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363
3364 ipv6_ifa_notify(0, ifp);
3365 in6_ifa_put(ifp);
3366 goto restart;
3367 }
3368#ifdef CONFIG_IPV6_PRIVACY
3369 } else if ((ifp->flags&IFA_F_TEMPORARY) &&
3370 !(ifp->flags&IFA_F_TENTATIVE)) {
stephen hemminger88949cf2010-03-17 20:31:17 +00003371 unsigned long regen_advance = ifp->idev->cnf.regen_max_retry *
3372 ifp->idev->cnf.dad_transmits *
3373 ifp->idev->nd_parms->retrans_time / HZ;
3374
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 if (age >= ifp->prefered_lft - regen_advance) {
3376 struct inet6_ifaddr *ifpub = ifp->ifpub;
3377 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
3378 next = ifp->tstamp + ifp->prefered_lft * HZ;
3379 if (!ifp->regen_count && ifpub) {
3380 ifp->regen_count++;
3381 in6_ifa_hold(ifp);
3382 in6_ifa_hold(ifpub);
3383 spin_unlock(&ifp->lock);
stephen hemminger5c578ae2010-03-17 20:31:11 +00003384
Hiroyuki YAMAMORI291d8092005-12-23 11:24:05 -08003385 spin_lock(&ifpub->lock);
3386 ifpub->regen_count = 0;
3387 spin_unlock(&ifpub->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388 ipv6_create_tempaddr(ifpub, ifp);
3389 in6_ifa_put(ifpub);
3390 in6_ifa_put(ifp);
3391 goto restart;
3392 }
3393 } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
3394 next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
3395 spin_unlock(&ifp->lock);
3396#endif
3397 } else {
3398 /* ifp->prefered_lft <= ifp->valid_lft */
3399 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
3400 next = ifp->tstamp + ifp->prefered_lft * HZ;
3401 spin_unlock(&ifp->lock);
3402 }
3403 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404 }
3405
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -07003406 next_sec = round_jiffies_up(next);
3407 next_sched = next;
3408
3409 /* If rounded timeout is accurate enough, accept it. */
3410 if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ))
3411 next_sched = next_sec;
3412
3413 /* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */
3414 if (time_before(next_sched, jiffies + ADDRCONF_TIMER_FUZZ_MAX))
3415 next_sched = jiffies + ADDRCONF_TIMER_FUZZ_MAX;
3416
3417 ADBG((KERN_DEBUG "now = %lu, schedule = %lu, rounded schedule = %lu => %lu\n",
3418 now, next, next_sec, next_sched));
3419
3420 addr_chk_timer.expires = next_sched;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 add_timer(&addr_chk_timer);
stephen hemminger5c578ae2010-03-17 20:31:11 +00003422 spin_unlock(&addrconf_verify_lock);
3423 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424}
3425
Thomas Graf461d8832006-09-18 00:09:49 -07003426static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local)
3427{
3428 struct in6_addr *pfx = NULL;
3429
3430 if (addr)
3431 pfx = nla_data(addr);
3432
3433 if (local) {
3434 if (pfx && nla_memcmp(local, pfx, sizeof(*pfx)))
3435 pfx = NULL;
3436 else
3437 pfx = nla_data(local);
3438 }
3439
3440 return pfx;
3441}
3442
Patrick McHardyef7c79e2007-06-05 12:38:30 -07003443static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = {
Thomas Graf461d8832006-09-18 00:09:49 -07003444 [IFA_ADDRESS] = { .len = sizeof(struct in6_addr) },
3445 [IFA_LOCAL] = { .len = sizeof(struct in6_addr) },
3446 [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) },
3447};
3448
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449static int
3450inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
3451{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09003452 struct net *net = sock_net(skb->sk);
Thomas Grafb933f712006-09-18 00:10:19 -07003453 struct ifaddrmsg *ifm;
3454 struct nlattr *tb[IFA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455 struct in6_addr *pfx;
Thomas Grafb933f712006-09-18 00:10:19 -07003456 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457
Thomas Grafb933f712006-09-18 00:10:19 -07003458 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
3459 if (err < 0)
3460 return err;
3461
3462 ifm = nlmsg_data(nlh);
3463 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464 if (pfx == NULL)
3465 return -EINVAL;
3466
Daniel Lezcanoaf284932008-03-05 10:46:57 -08003467 return inet6_addr_del(net, ifm->ifa_index, pfx, ifm->ifa_prefixlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468}
3469
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003470static int inet6_addr_modify(struct inet6_ifaddr *ifp, u8 ifa_flags,
3471 u32 prefered_lft, u32 valid_lft)
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09003472{
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07003473 u32 flags;
3474 clock_t expires;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09003475 unsigned long timeout;
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09003476
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09003477 if (!valid_lft || (prefered_lft > valid_lft))
3478 return -EINVAL;
3479
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09003480 timeout = addrconf_timeout_fixup(valid_lft, HZ);
3481 if (addrconf_finite_timeout(timeout)) {
3482 expires = jiffies_to_clock_t(timeout * HZ);
3483 valid_lft = timeout;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07003484 flags = RTF_EXPIRES;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09003485 } else {
3486 expires = 0;
3487 flags = 0;
3488 ifa_flags |= IFA_F_PERMANENT;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07003489 }
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09003490
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09003491 timeout = addrconf_timeout_fixup(prefered_lft, HZ);
3492 if (addrconf_finite_timeout(timeout)) {
3493 if (timeout == 0)
3494 ifa_flags |= IFA_F_DEPRECATED;
3495 prefered_lft = timeout;
3496 }
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09003497
3498 spin_lock_bh(&ifp->lock);
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07003499 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 +09003500 ifp->tstamp = jiffies;
3501 ifp->valid_lft = valid_lft;
3502 ifp->prefered_lft = prefered_lft;
3503
3504 spin_unlock_bh(&ifp->lock);
3505 if (!(ifp->flags&IFA_F_TENTATIVE))
3506 ipv6_ifa_notify(0, ifp);
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09003507
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09003508 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, ifp->idev->dev,
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07003509 expires, flags);
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09003510 addrconf_verify(0);
3511
3512 return 0;
3513}
3514
3515static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
3517{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09003518 struct net *net = sock_net(skb->sk);
Thomas Graf461d8832006-09-18 00:09:49 -07003519 struct ifaddrmsg *ifm;
3520 struct nlattr *tb[IFA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521 struct in6_addr *pfx;
Thomas Graf7198f8c2006-09-18 00:13:46 -07003522 struct inet6_ifaddr *ifa;
3523 struct net_device *dev;
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003524 u32 valid_lft = INFINITY_LIFE_TIME, preferred_lft = INFINITY_LIFE_TIME;
3525 u8 ifa_flags;
Thomas Graf461d8832006-09-18 00:09:49 -07003526 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527
Thomas Graf461d8832006-09-18 00:09:49 -07003528 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
3529 if (err < 0)
3530 return err;
3531
3532 ifm = nlmsg_data(nlh);
3533 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534 if (pfx == NULL)
3535 return -EINVAL;
3536
Thomas Graf461d8832006-09-18 00:09:49 -07003537 if (tb[IFA_CACHEINFO]) {
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09003538 struct ifa_cacheinfo *ci;
Thomas Graf461d8832006-09-18 00:09:49 -07003539
3540 ci = nla_data(tb[IFA_CACHEINFO]);
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09003541 valid_lft = ci->ifa_valid;
Thomas Graf461d8832006-09-18 00:09:49 -07003542 preferred_lft = ci->ifa_prefered;
3543 } else {
3544 preferred_lft = INFINITY_LIFE_TIME;
3545 valid_lft = INFINITY_LIFE_TIME;
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09003546 }
3547
Daniel Lezcanoaf284932008-03-05 10:46:57 -08003548 dev = __dev_get_by_index(net, ifm->ifa_index);
Thomas Graf7198f8c2006-09-18 00:13:46 -07003549 if (dev == NULL)
3550 return -ENODEV;
3551
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003552 /* We ignore other flags so far. */
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07003553 ifa_flags = ifm->ifa_flags & (IFA_F_NODAD | IFA_F_HOMEADDRESS);
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003554
Daniel Lezcano1cab3da2008-01-10 22:44:09 -08003555 ifa = ipv6_get_ifaddr(net, pfx, dev, 1);
Thomas Graf7198f8c2006-09-18 00:13:46 -07003556 if (ifa == NULL) {
3557 /*
3558 * It would be best to check for !NLM_F_CREATE here but
3559 * userspace alreay relies on not having to provide this.
3560 */
Daniel Lezcanoaf284932008-03-05 10:46:57 -08003561 return inet6_addr_add(net, ifm->ifa_index, pfx,
3562 ifm->ifa_prefixlen, ifa_flags,
3563 preferred_lft, valid_lft);
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09003564 }
3565
Thomas Graf7198f8c2006-09-18 00:13:46 -07003566 if (nlh->nlmsg_flags & NLM_F_EXCL ||
3567 !(nlh->nlmsg_flags & NLM_F_REPLACE))
3568 err = -EEXIST;
3569 else
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003570 err = inet6_addr_modify(ifa, ifa_flags, preferred_lft, valid_lft);
Thomas Graf7198f8c2006-09-18 00:13:46 -07003571
3572 in6_ifa_put(ifa);
3573
3574 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575}
3576
Thomas Graf101bb222006-09-18 00:11:52 -07003577static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u8 flags,
3578 u8 scope, int ifindex)
3579{
3580 struct ifaddrmsg *ifm;
3581
3582 ifm = nlmsg_data(nlh);
3583 ifm->ifa_family = AF_INET6;
3584 ifm->ifa_prefixlen = prefixlen;
3585 ifm->ifa_flags = flags;
3586 ifm->ifa_scope = scope;
3587 ifm->ifa_index = ifindex;
3588}
3589
Thomas Graf85486af2006-09-18 00:11:24 -07003590static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
3591 unsigned long tstamp, u32 preferred, u32 valid)
3592{
3593 struct ifa_cacheinfo ci;
3594
Thomas Graf18a31e12010-11-17 04:12:02 +00003595 ci.cstamp = cstamp_delta(cstamp);
3596 ci.tstamp = cstamp_delta(tstamp);
Thomas Graf85486af2006-09-18 00:11:24 -07003597 ci.ifa_prefered = preferred;
3598 ci.ifa_valid = valid;
3599
3600 return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
3601}
3602
Thomas Graf101bb222006-09-18 00:11:52 -07003603static inline int rt_scope(int ifa_scope)
3604{
3605 if (ifa_scope & IFA_HOST)
3606 return RT_SCOPE_HOST;
3607 else if (ifa_scope & IFA_LINK)
3608 return RT_SCOPE_LINK;
3609 else if (ifa_scope & IFA_SITE)
3610 return RT_SCOPE_SITE;
3611 else
3612 return RT_SCOPE_UNIVERSE;
3613}
3614
Thomas Graf0ab68032006-09-18 00:12:35 -07003615static inline int inet6_ifaddr_msgsize(void)
3616{
Thomas Graf339bf982006-11-10 14:10:15 -08003617 return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
3618 + nla_total_size(16) /* IFA_ADDRESS */
3619 + nla_total_size(sizeof(struct ifa_cacheinfo));
Thomas Graf0ab68032006-09-18 00:12:35 -07003620}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07003621
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07003623 u32 pid, u32 seq, int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625 struct nlmsghdr *nlh;
Thomas Graf85486af2006-09-18 00:11:24 -07003626 u32 preferred, valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627
Thomas Graf0ab68032006-09-18 00:12:35 -07003628 nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags);
3629 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08003630 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07003631
Thomas Graf101bb222006-09-18 00:11:52 -07003632 put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope),
3633 ifa->idev->dev->ifindex);
3634
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635 if (!(ifa->flags&IFA_F_PERMANENT)) {
Thomas Graf85486af2006-09-18 00:11:24 -07003636 preferred = ifa->prefered_lft;
3637 valid = ifa->valid_lft;
3638 if (preferred != INFINITY_LIFE_TIME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639 long tval = (jiffies - ifa->tstamp)/HZ;
Jens Rosenbooma1faa692009-06-25 04:55:50 +00003640 if (preferred > tval)
3641 preferred -= tval;
3642 else
3643 preferred = 0;
Ben Hutchingsf56619f2010-06-26 11:37:47 +00003644 if (valid != INFINITY_LIFE_TIME) {
3645 if (valid > tval)
3646 valid -= tval;
3647 else
3648 valid = 0;
3649 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650 }
3651 } else {
Thomas Graf85486af2006-09-18 00:11:24 -07003652 preferred = INFINITY_LIFE_TIME;
3653 valid = INFINITY_LIFE_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654 }
Thomas Graf85486af2006-09-18 00:11:24 -07003655
Thomas Graf0ab68032006-09-18 00:12:35 -07003656 if (nla_put(skb, IFA_ADDRESS, 16, &ifa->addr) < 0 ||
Patrick McHardy26932562007-01-31 23:16:40 -08003657 put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0) {
3658 nlmsg_cancel(skb, nlh);
3659 return -EMSGSIZE;
3660 }
Thomas Graf85486af2006-09-18 00:11:24 -07003661
Thomas Graf0ab68032006-09-18 00:12:35 -07003662 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663}
3664
3665static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -07003666 u32 pid, u32 seq, int event, u16 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668 struct nlmsghdr *nlh;
Thomas Graf101bb222006-09-18 00:11:52 -07003669 u8 scope = RT_SCOPE_UNIVERSE;
3670 int ifindex = ifmca->idev->dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671
Thomas Graf101bb222006-09-18 00:11:52 -07003672 if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE)
3673 scope = RT_SCOPE_SITE;
3674
Thomas Graf0ab68032006-09-18 00:12:35 -07003675 nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags);
3676 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08003677 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07003678
Thomas Graf101bb222006-09-18 00:11:52 -07003679 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
Thomas Graf0ab68032006-09-18 00:12:35 -07003680 if (nla_put(skb, IFA_MULTICAST, 16, &ifmca->mca_addr) < 0 ||
3681 put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp,
Patrick McHardy26932562007-01-31 23:16:40 -08003682 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
3683 nlmsg_cancel(skb, nlh);
3684 return -EMSGSIZE;
3685 }
Thomas Graf85486af2006-09-18 00:11:24 -07003686
Thomas Graf0ab68032006-09-18 00:12:35 -07003687 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688}
3689
3690static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07003691 u32 pid, u32 seq, int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693 struct nlmsghdr *nlh;
Thomas Graf101bb222006-09-18 00:11:52 -07003694 u8 scope = RT_SCOPE_UNIVERSE;
3695 int ifindex = ifaca->aca_idev->dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696
Thomas Graf101bb222006-09-18 00:11:52 -07003697 if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE)
3698 scope = RT_SCOPE_SITE;
3699
Thomas Graf0ab68032006-09-18 00:12:35 -07003700 nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags);
3701 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08003702 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07003703
Thomas Graf101bb222006-09-18 00:11:52 -07003704 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
Thomas Graf0ab68032006-09-18 00:12:35 -07003705 if (nla_put(skb, IFA_ANYCAST, 16, &ifaca->aca_addr) < 0 ||
3706 put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp,
Patrick McHardy26932562007-01-31 23:16:40 -08003707 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
3708 nlmsg_cancel(skb, nlh);
3709 return -EMSGSIZE;
3710 }
Thomas Graf85486af2006-09-18 00:11:24 -07003711
Thomas Graf0ab68032006-09-18 00:12:35 -07003712 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713}
3714
Stephen Hemmingere21e8462010-03-20 16:09:01 -07003715enum addr_type_t {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716 UNICAST_ADDR,
3717 MULTICAST_ADDR,
3718 ANYCAST_ADDR,
3719};
3720
Eric Dumazet234b27c2009-11-12 04:11:50 +00003721/* called with rcu_read_lock() */
3722static int in6_dump_addrs(struct inet6_dev *idev, struct sk_buff *skb,
3723 struct netlink_callback *cb, enum addr_type_t type,
3724 int s_ip_idx, int *p_ip_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726 struct ifmcaddr6 *ifmca;
3727 struct ifacaddr6 *ifaca;
Eric Dumazet234b27c2009-11-12 04:11:50 +00003728 int err = 1;
3729 int ip_idx = *p_ip_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730
Eric Dumazet234b27c2009-11-12 04:11:50 +00003731 read_lock_bh(&idev->lock);
3732 switch (type) {
stephen hemminger502a2ff2010-03-17 20:31:13 +00003733 case UNICAST_ADDR: {
3734 struct inet6_ifaddr *ifa;
3735
Eric Dumazet234b27c2009-11-12 04:11:50 +00003736 /* unicast address incl. temp addr */
stephen hemminger502a2ff2010-03-17 20:31:13 +00003737 list_for_each_entry(ifa, &idev->addr_list, if_list) {
3738 if (++ip_idx < s_ip_idx)
Eric Dumazet234b27c2009-11-12 04:11:50 +00003739 continue;
3740 err = inet6_fill_ifaddr(skb, ifa,
3741 NETLINK_CB(cb->skb).pid,
3742 cb->nlh->nlmsg_seq,
3743 RTM_NEWADDR,
3744 NLM_F_MULTI);
3745 if (err <= 0)
3746 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747 }
Eric Dumazet234b27c2009-11-12 04:11:50 +00003748 break;
stephen hemminger502a2ff2010-03-17 20:31:13 +00003749 }
Eric Dumazet234b27c2009-11-12 04:11:50 +00003750 case MULTICAST_ADDR:
3751 /* multicast address */
3752 for (ifmca = idev->mc_list; ifmca;
3753 ifmca = ifmca->next, ip_idx++) {
3754 if (ip_idx < s_ip_idx)
3755 continue;
3756 err = inet6_fill_ifmcaddr(skb, ifmca,
3757 NETLINK_CB(cb->skb).pid,
3758 cb->nlh->nlmsg_seq,
3759 RTM_GETMULTICAST,
3760 NLM_F_MULTI);
3761 if (err <= 0)
3762 break;
3763 }
3764 break;
3765 case ANYCAST_ADDR:
3766 /* anycast address */
3767 for (ifaca = idev->ac_list; ifaca;
3768 ifaca = ifaca->aca_next, ip_idx++) {
3769 if (ip_idx < s_ip_idx)
3770 continue;
3771 err = inet6_fill_ifacaddr(skb, ifaca,
3772 NETLINK_CB(cb->skb).pid,
3773 cb->nlh->nlmsg_seq,
3774 RTM_GETANYCAST,
3775 NLM_F_MULTI);
3776 if (err <= 0)
3777 break;
3778 }
3779 break;
3780 default:
3781 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782 }
Eric Dumazet234b27c2009-11-12 04:11:50 +00003783 read_unlock_bh(&idev->lock);
3784 *p_ip_idx = ip_idx;
3785 return err;
3786}
3787
3788static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
3789 enum addr_type_t type)
3790{
3791 struct net *net = sock_net(skb->sk);
3792 int h, s_h;
3793 int idx, ip_idx;
3794 int s_idx, s_ip_idx;
3795 struct net_device *dev;
3796 struct inet6_dev *idev;
3797 struct hlist_head *head;
3798 struct hlist_node *node;
3799
3800 s_h = cb->args[0];
3801 s_idx = idx = cb->args[1];
3802 s_ip_idx = ip_idx = cb->args[2];
3803
3804 rcu_read_lock();
3805 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
3806 idx = 0;
3807 head = &net->dev_index_head[h];
3808 hlist_for_each_entry_rcu(dev, node, head, index_hlist) {
3809 if (idx < s_idx)
3810 goto cont;
Patrick McHardy4b97efd2010-03-26 20:27:49 -07003811 if (h > s_h || idx > s_idx)
Eric Dumazet234b27c2009-11-12 04:11:50 +00003812 s_ip_idx = 0;
3813 ip_idx = 0;
Stephen Hemmingere21e8462010-03-20 16:09:01 -07003814 idev = __in6_dev_get(dev);
3815 if (!idev)
Eric Dumazet234b27c2009-11-12 04:11:50 +00003816 goto cont;
3817
3818 if (in6_dump_addrs(idev, skb, cb, type,
3819 s_ip_idx, &ip_idx) <= 0)
3820 goto done;
3821cont:
3822 idx++;
3823 }
3824 }
3825done:
3826 rcu_read_unlock();
3827 cb->args[0] = h;
3828 cb->args[1] = idx;
3829 cb->args[2] = ip_idx;
3830
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831 return skb->len;
3832}
3833
3834static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
3835{
3836 enum addr_type_t type = UNICAST_ADDR;
Denis V. Lunevb8542722007-12-01 00:21:31 +11003837
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838 return inet6_dump_addr(skb, cb, type);
3839}
3840
3841static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
3842{
3843 enum addr_type_t type = MULTICAST_ADDR;
Denis V. Lunevb8542722007-12-01 00:21:31 +11003844
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845 return inet6_dump_addr(skb, cb, type);
3846}
3847
3848
3849static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
3850{
3851 enum addr_type_t type = ANYCAST_ADDR;
Denis V. Lunevb8542722007-12-01 00:21:31 +11003852
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853 return inet6_dump_addr(skb, cb, type);
3854}
3855
Thomas Graf1b29fc22006-09-18 00:10:50 -07003856static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr* nlh,
3857 void *arg)
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003858{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09003859 struct net *net = sock_net(in_skb->sk);
Thomas Graf1b29fc22006-09-18 00:10:50 -07003860 struct ifaddrmsg *ifm;
3861 struct nlattr *tb[IFA_MAX+1];
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003862 struct in6_addr *addr = NULL;
3863 struct net_device *dev = NULL;
3864 struct inet6_ifaddr *ifa;
3865 struct sk_buff *skb;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003866 int err;
3867
Thomas Graf1b29fc22006-09-18 00:10:50 -07003868 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
3869 if (err < 0)
3870 goto errout;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003871
Thomas Graf1b29fc22006-09-18 00:10:50 -07003872 addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
3873 if (addr == NULL) {
3874 err = -EINVAL;
3875 goto errout;
3876 }
3877
3878 ifm = nlmsg_data(nlh);
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003879 if (ifm->ifa_index)
Benjamin Thery6fda7352008-03-05 10:47:47 -08003880 dev = __dev_get_by_index(net, ifm->ifa_index);
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003881
Stephen Hemmingere21e8462010-03-20 16:09:01 -07003882 ifa = ipv6_get_ifaddr(net, addr, dev, 1);
3883 if (!ifa) {
Thomas Graf1b29fc22006-09-18 00:10:50 -07003884 err = -EADDRNOTAVAIL;
3885 goto errout;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003886 }
3887
Stephen Hemmingere21e8462010-03-20 16:09:01 -07003888 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL);
3889 if (!skb) {
Thomas Graf1b29fc22006-09-18 00:10:50 -07003890 err = -ENOBUFS;
3891 goto errout_ifa;
3892 }
3893
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003894 err = inet6_fill_ifaddr(skb, ifa, NETLINK_CB(in_skb).pid,
3895 nlh->nlmsg_seq, RTM_NEWADDR, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08003896 if (err < 0) {
3897 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
3898 WARN_ON(err == -EMSGSIZE);
3899 kfree_skb(skb);
3900 goto errout_ifa;
3901 }
Benjamin Thery6fda7352008-03-05 10:47:47 -08003902 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).pid);
Thomas Graf1b29fc22006-09-18 00:10:50 -07003903errout_ifa:
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003904 in6_ifa_put(ifa);
Thomas Graf1b29fc22006-09-18 00:10:50 -07003905errout:
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003906 return err;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003907}
3908
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
3910{
3911 struct sk_buff *skb;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09003912 struct net *net = dev_net(ifa->idev->dev);
Thomas Graf5d620262006-08-15 00:35:02 -07003913 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914
Thomas Graf0ab68032006-09-18 00:12:35 -07003915 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC);
Thomas Graf5d620262006-08-15 00:35:02 -07003916 if (skb == NULL)
3917 goto errout;
3918
3919 err = inet6_fill_ifaddr(skb, ifa, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08003920 if (err < 0) {
3921 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
3922 WARN_ON(err == -EMSGSIZE);
3923 kfree_skb(skb);
3924 goto errout;
3925 }
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08003926 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
3927 return;
Thomas Graf5d620262006-08-15 00:35:02 -07003928errout:
3929 if (err < 0)
Benjamin Thery6fda7352008-03-05 10:47:47 -08003930 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931}
3932
Dave Jonesb6f99a22007-03-22 12:27:49 -07003933static inline void ipv6_store_devconf(struct ipv6_devconf *cnf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934 __s32 *array, int bytes)
3935{
Thomas Graf04561c12006-11-14 19:53:58 -08003936 BUG_ON(bytes < (DEVCONF_MAX * 4));
3937
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938 memset(array, 0, bytes);
3939 array[DEVCONF_FORWARDING] = cnf->forwarding;
3940 array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
3941 array[DEVCONF_MTU6] = cnf->mtu6;
3942 array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
3943 array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
3944 array[DEVCONF_AUTOCONF] = cnf->autoconf;
3945 array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
3946 array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
Thomas Graf93908d12010-11-17 01:44:24 +00003947 array[DEVCONF_RTR_SOLICIT_INTERVAL] =
3948 jiffies_to_msecs(cnf->rtr_solicit_interval);
3949 array[DEVCONF_RTR_SOLICIT_DELAY] =
3950 jiffies_to_msecs(cnf->rtr_solicit_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951 array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
3952#ifdef CONFIG_IPV6_PRIVACY
3953 array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
3954 array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
3955 array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
3956 array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
3957 array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
3958#endif
3959 array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -08003960 array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr;
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -08003961 array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo;
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08003962#ifdef CONFIG_IPV6_ROUTER_PREF
3963 array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref;
Thomas Graf93908d12010-11-17 01:44:24 +00003964 array[DEVCONF_RTR_PROBE_INTERVAL] =
3965 jiffies_to_msecs(cnf->rtr_probe_interval);
Neil Hormanfa03ef32007-01-30 14:30:10 -08003966#ifdef CONFIG_IPV6_ROUTE_INFO
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -08003967 array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen;
3968#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08003969#endif
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -07003970 array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp;
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -07003971 array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route;
Neil Horman95c385b2007-04-25 17:08:10 -07003972#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
3973 array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad;
3974#endif
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09003975#ifdef CONFIG_IPV6_MROUTE
3976 array[DEVCONF_MC_FORWARDING] = cnf->mc_forwarding;
3977#endif
YOSHIFUJI Hideaki778d80b2008-06-28 14:17:11 +09003978 array[DEVCONF_DISABLE_IPV6] = cnf->disable_ipv6;
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09003979 array[DEVCONF_ACCEPT_DAD] = cnf->accept_dad;
Cosmin Ratiuc3faca02009-10-09 03:11:14 +00003980 array[DEVCONF_FORCE_TLLAO] = cnf->force_tllao;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981}
3982
Thomas Grafb382b192010-11-16 04:33:57 +00003983static inline size_t inet6_ifla6_size(void)
3984{
3985 return nla_total_size(4) /* IFLA_INET6_FLAGS */
3986 + nla_total_size(sizeof(struct ifla_cacheinfo))
3987 + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */
3988 + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */
3989 + nla_total_size(ICMP6_MIB_MAX * 8); /* IFLA_INET6_ICMP6STATS */
3990}
3991
Thomas Graf339bf982006-11-10 14:10:15 -08003992static inline size_t inet6_if_nlmsg_size(void)
3993{
3994 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
3995 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
3996 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
3997 + nla_total_size(4) /* IFLA_MTU */
3998 + nla_total_size(4) /* IFLA_LINK */
Thomas Grafb382b192010-11-16 04:33:57 +00003999 + nla_total_size(inet6_ifla6_size()); /* IFLA_PROTINFO */
Thomas Graf339bf982006-11-10 14:10:15 -08004000}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07004001
Eric Dumazetbe281e52011-05-19 01:14:23 +00004002static inline void __snmp6_fill_statsdev(u64 *stats, atomic_long_t *mib,
Tejun Heo7d720c32010-02-16 15:20:26 +00004003 int items, int bytes)
Herbert Xu7f7d9a62007-04-24 21:54:09 -07004004{
4005 int i;
4006 int pad = bytes - sizeof(u64) * items;
4007 BUG_ON(pad < 0);
4008
4009 /* Use put_unaligned() because stats may not be aligned for u64. */
4010 put_unaligned(items, &stats[0]);
4011 for (i = 1; i < items; i++)
Eric Dumazetbe281e52011-05-19 01:14:23 +00004012 put_unaligned(atomic_long_read(&mib[i]), &stats[i]);
Herbert Xu7f7d9a62007-04-24 21:54:09 -07004013
4014 memset(&stats[items], 0, pad);
4015}
4016
Eric Dumazet4ce3c182010-06-30 13:31:19 -07004017static inline void __snmp6_fill_stats64(u64 *stats, void __percpu **mib,
4018 int items, int bytes, size_t syncpoff)
4019{
4020 int i;
4021 int pad = bytes - sizeof(u64) * items;
4022 BUG_ON(pad < 0);
4023
4024 /* Use put_unaligned() because stats may not be aligned for u64. */
4025 put_unaligned(items, &stats[0]);
4026 for (i = 1; i < items; i++)
4027 put_unaligned(snmp_fold_field64(mib, i, syncpoff), &stats[i]);
4028
4029 memset(&stats[items], 0, pad);
4030}
4031
Herbert Xu7f7d9a62007-04-24 21:54:09 -07004032static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype,
4033 int bytes)
4034{
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004035 switch (attrtype) {
Herbert Xu7f7d9a62007-04-24 21:54:09 -07004036 case IFLA_INET6_STATS:
Eric Dumazet4ce3c182010-06-30 13:31:19 -07004037 __snmp6_fill_stats64(stats, (void __percpu **)idev->stats.ipv6,
4038 IPSTATS_MIB_MAX, bytes, offsetof(struct ipstats_mib, syncp));
Herbert Xu7f7d9a62007-04-24 21:54:09 -07004039 break;
4040 case IFLA_INET6_ICMP6STATS:
Eric Dumazetbe281e52011-05-19 01:14:23 +00004041 __snmp6_fill_statsdev(stats, idev->stats.icmpv6dev->mibs, ICMP6_MIB_MAX, bytes);
Herbert Xu7f7d9a62007-04-24 21:54:09 -07004042 break;
4043 }
4044}
4045
Thomas Grafb382b192010-11-16 04:33:57 +00004046static int inet6_fill_ifla6_attrs(struct sk_buff *skb, struct inet6_dev *idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047{
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07004048 struct nlattr *nla;
Thomas Graf04561c12006-11-14 19:53:58 -08004049 struct ifla_cacheinfo ci;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050
Thomas Graf04561c12006-11-14 19:53:58 -08004051 NLA_PUT_U32(skb, IFLA_INET6_FLAGS, idev->if_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053 ci.max_reasm_len = IPV6_MAXPLEN;
David S. Miller24912422010-11-19 13:13:47 -08004054 ci.tstamp = cstamp_delta(idev->tstamp);
4055 ci.reachable_time = jiffies_to_msecs(idev->nd_parms->reachable_time);
4056 ci.retrans_time = jiffies_to_msecs(idev->nd_parms->retrans_time);
Thomas Graf04561c12006-11-14 19:53:58 -08004057 NLA_PUT(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci);
4058
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07004059 nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32));
4060 if (nla == NULL)
Thomas Graf04561c12006-11-14 19:53:58 -08004061 goto nla_put_failure;
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07004062 ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07004064 /* XXX - MC not implemented */
4065
4066 nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64));
4067 if (nla == NULL)
4068 goto nla_put_failure;
4069 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla));
4070
4071 nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64));
4072 if (nla == NULL)
4073 goto nla_put_failure;
4074 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075
Thomas Grafb382b192010-11-16 04:33:57 +00004076 return 0;
4077
4078nla_put_failure:
4079 return -EMSGSIZE;
4080}
4081
4082static size_t inet6_get_link_af_size(const struct net_device *dev)
4083{
4084 if (!__in6_dev_get(dev))
4085 return 0;
4086
4087 return inet6_ifla6_size();
4088}
4089
4090static int inet6_fill_link_af(struct sk_buff *skb, const struct net_device *dev)
4091{
4092 struct inet6_dev *idev = __in6_dev_get(dev);
4093
4094 if (!idev)
4095 return -ENODATA;
4096
4097 if (inet6_fill_ifla6_attrs(skb, idev) < 0)
4098 return -EMSGSIZE;
4099
4100 return 0;
4101}
4102
Thomas Grafb382b192010-11-16 04:33:57 +00004103static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
4104 u32 pid, u32 seq, int event, unsigned int flags)
4105{
4106 struct net_device *dev = idev->dev;
4107 struct ifinfomsg *hdr;
4108 struct nlmsghdr *nlh;
4109 void *protoinfo;
4110
4111 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*hdr), flags);
4112 if (nlh == NULL)
4113 return -EMSGSIZE;
4114
4115 hdr = nlmsg_data(nlh);
4116 hdr->ifi_family = AF_INET6;
4117 hdr->__ifi_pad = 0;
4118 hdr->ifi_type = dev->type;
4119 hdr->ifi_index = dev->ifindex;
4120 hdr->ifi_flags = dev_get_flags(dev);
4121 hdr->ifi_change = 0;
4122
4123 NLA_PUT_STRING(skb, IFLA_IFNAME, dev->name);
4124
4125 if (dev->addr_len)
4126 NLA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr);
4127
4128 NLA_PUT_U32(skb, IFLA_MTU, dev->mtu);
4129 if (dev->ifindex != dev->iflink)
4130 NLA_PUT_U32(skb, IFLA_LINK, dev->iflink);
4131
4132 protoinfo = nla_nest_start(skb, IFLA_PROTINFO);
4133 if (protoinfo == NULL)
4134 goto nla_put_failure;
4135
4136 if (inet6_fill_ifla6_attrs(skb, idev) < 0)
4137 goto nla_put_failure;
4138
Thomas Graf04561c12006-11-14 19:53:58 -08004139 nla_nest_end(skb, protoinfo);
4140 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141
Thomas Graf04561c12006-11-14 19:53:58 -08004142nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08004143 nlmsg_cancel(skb, nlh);
4144 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145}
4146
4147static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
4148{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09004149 struct net *net = sock_net(skb->sk);
Eric Dumazet84d26972009-11-09 12:11:28 +00004150 int h, s_h;
David S. Miller434a8a52009-11-11 18:53:00 -08004151 int idx = 0, s_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152 struct net_device *dev;
4153 struct inet6_dev *idev;
Eric Dumazet84d26972009-11-09 12:11:28 +00004154 struct hlist_head *head;
4155 struct hlist_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156
Eric Dumazet84d26972009-11-09 12:11:28 +00004157 s_h = cb->args[0];
4158 s_idx = cb->args[1];
4159
4160 rcu_read_lock();
4161 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4162 idx = 0;
4163 head = &net->dev_index_head[h];
4164 hlist_for_each_entry_rcu(dev, node, head, index_hlist) {
4165 if (idx < s_idx)
4166 goto cont;
4167 idev = __in6_dev_get(dev);
4168 if (!idev)
4169 goto cont;
4170 if (inet6_fill_ifinfo(skb, idev,
4171 NETLINK_CB(cb->skb).pid,
4172 cb->nlh->nlmsg_seq,
4173 RTM_NEWLINK, NLM_F_MULTI) <= 0)
4174 goto out;
Pavel Emelianov7562f872007-05-03 15:13:45 -07004175cont:
Eric Dumazet84d26972009-11-09 12:11:28 +00004176 idx++;
4177 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178 }
Eric Dumazet84d26972009-11-09 12:11:28 +00004179out:
4180 rcu_read_unlock();
4181 cb->args[1] = idx;
4182 cb->args[0] = h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183
4184 return skb->len;
4185}
4186
4187void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
4188{
4189 struct sk_buff *skb;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09004190 struct net *net = dev_net(idev->dev);
Thomas Graf8d7a76c2006-08-15 00:35:47 -07004191 int err = -ENOBUFS;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004192
Thomas Graf339bf982006-11-10 14:10:15 -08004193 skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC);
Thomas Graf8d7a76c2006-08-15 00:35:47 -07004194 if (skb == NULL)
4195 goto errout;
4196
4197 err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08004198 if (err < 0) {
4199 /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */
4200 WARN_ON(err == -EMSGSIZE);
4201 kfree_skb(skb);
4202 goto errout;
4203 }
Nicolas Dichtel5f75a102010-12-07 23:38:31 +00004204 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFINFO, NULL, GFP_ATOMIC);
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08004205 return;
Thomas Graf8d7a76c2006-08-15 00:35:47 -07004206errout:
4207 if (err < 0)
Nicolas Dichtel5f75a102010-12-07 23:38:31 +00004208 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFINFO, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209}
4210
Thomas Graf339bf982006-11-10 14:10:15 -08004211static inline size_t inet6_prefix_nlmsg_size(void)
4212{
4213 return NLMSG_ALIGN(sizeof(struct prefixmsg))
4214 + nla_total_size(sizeof(struct in6_addr))
4215 + nla_total_size(sizeof(struct prefix_cacheinfo));
4216}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07004217
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
Thomas Graf6051e2f2006-11-14 19:54:19 -08004219 struct prefix_info *pinfo, u32 pid, u32 seq,
4220 int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221{
Thomas Graf6051e2f2006-11-14 19:54:19 -08004222 struct prefixmsg *pmsg;
4223 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224 struct prefix_cacheinfo ci;
4225
Thomas Graf6051e2f2006-11-14 19:54:19 -08004226 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*pmsg), flags);
4227 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08004228 return -EMSGSIZE;
Thomas Graf6051e2f2006-11-14 19:54:19 -08004229
4230 pmsg = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231 pmsg->prefix_family = AF_INET6;
Patrick McHardy8a470772005-06-28 12:56:45 -07004232 pmsg->prefix_pad1 = 0;
4233 pmsg->prefix_pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234 pmsg->prefix_ifindex = idev->dev->ifindex;
4235 pmsg->prefix_len = pinfo->prefix_len;
4236 pmsg->prefix_type = pinfo->type;
Patrick McHardy8a470772005-06-28 12:56:45 -07004237 pmsg->prefix_pad3 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238 pmsg->prefix_flags = 0;
4239 if (pinfo->onlink)
4240 pmsg->prefix_flags |= IF_PREFIX_ONLINK;
4241 if (pinfo->autoconf)
4242 pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
4243
Thomas Graf6051e2f2006-11-14 19:54:19 -08004244 NLA_PUT(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245
4246 ci.preferred_time = ntohl(pinfo->prefered);
4247 ci.valid_time = ntohl(pinfo->valid);
Thomas Graf6051e2f2006-11-14 19:54:19 -08004248 NLA_PUT(skb, PREFIX_CACHEINFO, sizeof(ci), &ci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249
Thomas Graf6051e2f2006-11-14 19:54:19 -08004250 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251
Thomas Graf6051e2f2006-11-14 19:54:19 -08004252nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08004253 nlmsg_cancel(skb, nlh);
4254 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255}
4256
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004257static void inet6_prefix_notify(int event, struct inet6_dev *idev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258 struct prefix_info *pinfo)
4259{
4260 struct sk_buff *skb;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09004261 struct net *net = dev_net(idev->dev);
Thomas Graf8c384bfa2006-08-15 00:36:07 -07004262 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263
Thomas Graf339bf982006-11-10 14:10:15 -08004264 skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC);
Thomas Graf8c384bfa2006-08-15 00:36:07 -07004265 if (skb == NULL)
4266 goto errout;
4267
4268 err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08004269 if (err < 0) {
4270 /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */
4271 WARN_ON(err == -EMSGSIZE);
4272 kfree_skb(skb);
4273 goto errout;
4274 }
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08004275 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC);
4276 return;
Thomas Graf8c384bfa2006-08-15 00:36:07 -07004277errout:
4278 if (err < 0)
Benjamin Thery6fda7352008-03-05 10:47:47 -08004279 rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280}
4281
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
4283{
4284 inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
4285
4286 switch (event) {
4287 case RTM_NEWADDR:
Neil Horman95c385b2007-04-25 17:08:10 -07004288 /*
4289 * If the address was optimistic
4290 * we inserted the route at the start of
4291 * our DAD process, so we don't need
4292 * to do it again
4293 */
4294 if (!(ifp->rt->rt6i_node))
4295 ip6_ins_rt(ifp->rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296 if (ifp->idev->cnf.forwarding)
4297 addrconf_join_anycast(ifp);
4298 break;
4299 case RTM_DELADDR:
4300 if (ifp->idev->cnf.forwarding)
4301 addrconf_leave_anycast(ifp);
4302 addrconf_leave_solict(ifp->idev, &ifp->addr);
Changli Gaod8d1f302010-06-10 23:31:35 -07004303 dst_hold(&ifp->rt->dst);
stephen hemminger93fa1592010-04-12 05:41:31 +00004304
David S. Miller73a8bd72011-01-23 23:27:15 -08004305 if (ip6_del_rt(ifp->rt))
Changli Gaod8d1f302010-06-10 23:31:35 -07004306 dst_free(&ifp->rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307 break;
4308 }
4309}
4310
4311static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
4312{
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07004313 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314 if (likely(ifp->idev->dead == 0))
4315 __ipv6_ifa_notify(event, ifp);
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07004316 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317}
4318
4319#ifdef CONFIG_SYSCTL
4320
4321static
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07004322int addrconf_sysctl_forward(ctl_table *ctl, int write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323 void __user *buffer, size_t *lenp, loff_t *ppos)
4324{
4325 int *valp = ctl->data;
4326 int val = *valp;
Eric W. Biederman88af1822010-02-19 13:22:59 +00004327 loff_t pos = *ppos;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00004328 ctl_table lctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329 int ret;
4330
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00004331 /*
4332 * ctl->data points to idev->cnf.forwarding, we should
4333 * not modify it until we get the rtnl lock.
4334 */
4335 lctl = *ctl;
4336 lctl.data = &val;
4337
4338 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -08004340 if (write)
Stephen Hemmingerb325fdd2009-02-26 06:55:31 +00004341 ret = addrconf_fixup_forwarding(ctl, valp, val);
Eric W. Biederman88af1822010-02-19 13:22:59 +00004342 if (ret)
4343 *ppos = pos;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004344 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345}
4346
Brian Haley56d417b2009-06-01 03:07:33 -07004347static void dev_disable_change(struct inet6_dev *idev)
4348{
4349 if (!idev || !idev->dev)
4350 return;
4351
4352 if (idev->cnf.disable_ipv6)
4353 addrconf_notify(NULL, NETDEV_DOWN, idev->dev);
4354 else
4355 addrconf_notify(NULL, NETDEV_UP, idev->dev);
4356}
4357
4358static void addrconf_disable_change(struct net *net, __s32 newf)
4359{
4360 struct net_device *dev;
4361 struct inet6_dev *idev;
4362
Eric Dumazetc6d14c82009-11-04 05:43:23 -08004363 rcu_read_lock();
4364 for_each_netdev_rcu(net, dev) {
Brian Haley56d417b2009-06-01 03:07:33 -07004365 idev = __in6_dev_get(dev);
4366 if (idev) {
4367 int changed = (!idev->cnf.disable_ipv6) ^ (!newf);
4368 idev->cnf.disable_ipv6 = newf;
4369 if (changed)
4370 dev_disable_change(idev);
4371 }
Brian Haley56d417b2009-06-01 03:07:33 -07004372 }
Eric Dumazetc6d14c82009-11-04 05:43:23 -08004373 rcu_read_unlock();
Brian Haley56d417b2009-06-01 03:07:33 -07004374}
4375
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00004376static int addrconf_disable_ipv6(struct ctl_table *table, int *p, int newf)
Brian Haley56d417b2009-06-01 03:07:33 -07004377{
4378 struct net *net;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00004379 int old;
4380
4381 if (!rtnl_trylock())
4382 return restart_syscall();
Brian Haley56d417b2009-06-01 03:07:33 -07004383
4384 net = (struct net *)table->extra2;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00004385 old = *p;
4386 *p = newf;
Brian Haley56d417b2009-06-01 03:07:33 -07004387
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00004388 if (p == &net->ipv6.devconf_dflt->disable_ipv6) {
4389 rtnl_unlock();
Brian Haley56d417b2009-06-01 03:07:33 -07004390 return 0;
Eric W. Biederman88af1822010-02-19 13:22:59 +00004391 }
Brian Haley56d417b2009-06-01 03:07:33 -07004392
4393 if (p == &net->ipv6.devconf_all->disable_ipv6) {
Brian Haley56d417b2009-06-01 03:07:33 -07004394 net->ipv6.devconf_dflt->disable_ipv6 = newf;
4395 addrconf_disable_change(net, newf);
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00004396 } else if ((!newf) ^ (!old))
Brian Haley56d417b2009-06-01 03:07:33 -07004397 dev_disable_change((struct inet6_dev *)table->extra1);
4398
4399 rtnl_unlock();
4400 return 0;
4401}
4402
4403static
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07004404int addrconf_sysctl_disable(ctl_table *ctl, int write,
Brian Haley56d417b2009-06-01 03:07:33 -07004405 void __user *buffer, size_t *lenp, loff_t *ppos)
4406{
4407 int *valp = ctl->data;
4408 int val = *valp;
Eric W. Biederman88af1822010-02-19 13:22:59 +00004409 loff_t pos = *ppos;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00004410 ctl_table lctl;
Brian Haley56d417b2009-06-01 03:07:33 -07004411 int ret;
4412
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00004413 /*
4414 * ctl->data points to idev->cnf.disable_ipv6, we should
4415 * not modify it until we get the rtnl lock.
4416 */
4417 lctl = *ctl;
4418 lctl.data = &val;
4419
4420 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
Brian Haley56d417b2009-06-01 03:07:33 -07004421
4422 if (write)
4423 ret = addrconf_disable_ipv6(ctl, valp, val);
Eric W. Biederman88af1822010-02-19 13:22:59 +00004424 if (ret)
4425 *ppos = pos;
Brian Haley56d417b2009-06-01 03:07:33 -07004426 return ret;
4427}
4428
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429static struct addrconf_sysctl_table
4430{
4431 struct ctl_table_header *sysctl_header;
YOSHIFUJI Hideakif6a07b22008-03-25 00:25:11 +09004432 ctl_table addrconf_vars[DEVCONF_MAX+1];
Pavel Emelyanov1dab6222007-12-02 00:59:38 +11004433 char *dev_name;
Brian Haleyab32ea52006-09-22 14:15:41 -07004434} addrconf_sysctl __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435 .sysctl_header = NULL,
4436 .addrconf_vars = {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004437 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004438 .procname = "forwarding",
4439 .data = &ipv6_devconf.forwarding,
4440 .maxlen = sizeof(int),
4441 .mode = 0644,
4442 .proc_handler = addrconf_sysctl_forward,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443 },
4444 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004445 .procname = "hop_limit",
4446 .data = &ipv6_devconf.hop_limit,
4447 .maxlen = sizeof(int),
4448 .mode = 0644,
4449 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450 },
4451 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004452 .procname = "mtu",
4453 .data = &ipv6_devconf.mtu6,
4454 .maxlen = sizeof(int),
4455 .mode = 0644,
4456 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457 },
4458 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004459 .procname = "accept_ra",
4460 .data = &ipv6_devconf.accept_ra,
4461 .maxlen = sizeof(int),
4462 .mode = 0644,
4463 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004464 },
4465 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004466 .procname = "accept_redirects",
4467 .data = &ipv6_devconf.accept_redirects,
4468 .maxlen = sizeof(int),
4469 .mode = 0644,
4470 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471 },
4472 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004473 .procname = "autoconf",
4474 .data = &ipv6_devconf.autoconf,
4475 .maxlen = sizeof(int),
4476 .mode = 0644,
4477 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478 },
4479 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004480 .procname = "dad_transmits",
4481 .data = &ipv6_devconf.dad_transmits,
4482 .maxlen = sizeof(int),
4483 .mode = 0644,
4484 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004485 },
4486 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004487 .procname = "router_solicitations",
4488 .data = &ipv6_devconf.rtr_solicits,
4489 .maxlen = sizeof(int),
4490 .mode = 0644,
4491 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492 },
4493 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004494 .procname = "router_solicitation_interval",
4495 .data = &ipv6_devconf.rtr_solicit_interval,
4496 .maxlen = sizeof(int),
4497 .mode = 0644,
4498 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004499 },
4500 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004501 .procname = "router_solicitation_delay",
4502 .data = &ipv6_devconf.rtr_solicit_delay,
4503 .maxlen = sizeof(int),
4504 .mode = 0644,
4505 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004506 },
4507 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004508 .procname = "force_mld_version",
4509 .data = &ipv6_devconf.force_mld_version,
4510 .maxlen = sizeof(int),
4511 .mode = 0644,
4512 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513 },
4514#ifdef CONFIG_IPV6_PRIVACY
4515 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004516 .procname = "use_tempaddr",
4517 .data = &ipv6_devconf.use_tempaddr,
4518 .maxlen = sizeof(int),
4519 .mode = 0644,
4520 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521 },
4522 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004523 .procname = "temp_valid_lft",
4524 .data = &ipv6_devconf.temp_valid_lft,
4525 .maxlen = sizeof(int),
4526 .mode = 0644,
4527 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528 },
4529 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004530 .procname = "temp_prefered_lft",
4531 .data = &ipv6_devconf.temp_prefered_lft,
4532 .maxlen = sizeof(int),
4533 .mode = 0644,
4534 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535 },
4536 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004537 .procname = "regen_max_retry",
4538 .data = &ipv6_devconf.regen_max_retry,
4539 .maxlen = sizeof(int),
4540 .mode = 0644,
4541 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004542 },
4543 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004544 .procname = "max_desync_factor",
4545 .data = &ipv6_devconf.max_desync_factor,
4546 .maxlen = sizeof(int),
4547 .mode = 0644,
4548 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549 },
4550#endif
4551 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004552 .procname = "max_addresses",
4553 .data = &ipv6_devconf.max_addresses,
4554 .maxlen = sizeof(int),
4555 .mode = 0644,
4556 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557 },
4558 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004559 .procname = "accept_ra_defrtr",
4560 .data = &ipv6_devconf.accept_ra_defrtr,
4561 .maxlen = sizeof(int),
4562 .mode = 0644,
4563 .proc_handler = proc_dointvec,
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -08004564 },
4565 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004566 .procname = "accept_ra_pinfo",
4567 .data = &ipv6_devconf.accept_ra_pinfo,
4568 .maxlen = sizeof(int),
4569 .mode = 0644,
4570 .proc_handler = proc_dointvec,
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -08004571 },
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08004572#ifdef CONFIG_IPV6_ROUTER_PREF
4573 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004574 .procname = "accept_ra_rtr_pref",
4575 .data = &ipv6_devconf.accept_ra_rtr_pref,
4576 .maxlen = sizeof(int),
4577 .mode = 0644,
4578 .proc_handler = proc_dointvec,
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08004579 },
YOSHIFUJI Hideaki52e16352006-03-20 17:05:47 -08004580 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004581 .procname = "router_probe_interval",
4582 .data = &ipv6_devconf.rtr_probe_interval,
4583 .maxlen = sizeof(int),
4584 .mode = 0644,
4585 .proc_handler = proc_dointvec_jiffies,
YOSHIFUJI Hideaki52e16352006-03-20 17:05:47 -08004586 },
Neil Hormanfa03ef32007-01-30 14:30:10 -08004587#ifdef CONFIG_IPV6_ROUTE_INFO
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -08004588 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004589 .procname = "accept_ra_rt_info_max_plen",
4590 .data = &ipv6_devconf.accept_ra_rt_info_max_plen,
4591 .maxlen = sizeof(int),
4592 .mode = 0644,
4593 .proc_handler = proc_dointvec,
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -08004594 },
4595#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08004596#endif
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -08004597 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004598 .procname = "proxy_ndp",
4599 .data = &ipv6_devconf.proxy_ndp,
4600 .maxlen = sizeof(int),
4601 .mode = 0644,
4602 .proc_handler = proc_dointvec,
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -07004603 },
4604 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004605 .procname = "accept_source_route",
4606 .data = &ipv6_devconf.accept_source_route,
4607 .maxlen = sizeof(int),
4608 .mode = 0644,
4609 .proc_handler = proc_dointvec,
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -07004610 },
Neil Horman95c385b2007-04-25 17:08:10 -07004611#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
4612 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004613 .procname = "optimistic_dad",
4614 .data = &ipv6_devconf.optimistic_dad,
4615 .maxlen = sizeof(int),
4616 .mode = 0644,
4617 .proc_handler = proc_dointvec,
Neil Horman95c385b2007-04-25 17:08:10 -07004618
4619 },
4620#endif
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09004621#ifdef CONFIG_IPV6_MROUTE
4622 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004623 .procname = "mc_forwarding",
4624 .data = &ipv6_devconf.mc_forwarding,
4625 .maxlen = sizeof(int),
4626 .mode = 0444,
4627 .proc_handler = proc_dointvec,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09004628 },
4629#endif
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -07004630 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004631 .procname = "disable_ipv6",
4632 .data = &ipv6_devconf.disable_ipv6,
4633 .maxlen = sizeof(int),
4634 .mode = 0644,
4635 .proc_handler = addrconf_sysctl_disable,
YOSHIFUJI Hideaki778d80b2008-06-28 14:17:11 +09004636 },
4637 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004638 .procname = "accept_dad",
4639 .data = &ipv6_devconf.accept_dad,
4640 .maxlen = sizeof(int),
4641 .mode = 0644,
4642 .proc_handler = proc_dointvec,
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09004643 },
4644 {
Octavian Purdilaf7734fd2009-10-02 11:39:15 +00004645 .procname = "force_tllao",
4646 .data = &ipv6_devconf.force_tllao,
4647 .maxlen = sizeof(int),
4648 .mode = 0644,
4649 .proc_handler = proc_dointvec
4650 },
4651 {
Harout Hedeshian8b2414d2012-12-21 12:38:38 -08004652 .procname = "accept_ra_prefix_route",
4653 .data = &ipv6_devconf.accept_ra_prefix_route,
4654 .maxlen = sizeof(int),
4655 .mode = 0644,
4656 .proc_handler = proc_dointvec,
4657 },
4658 {
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08004659 /* sentinel */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004660 }
4661 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662};
4663
Pavel Emelyanovbff16c22008-01-10 17:42:13 -08004664static int __addrconf_sysctl_register(struct net *net, char *dev_name,
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08004665 struct inet6_dev *idev, struct ipv6_devconf *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666{
4667 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668 struct addrconf_sysctl_table *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669
Pavel Emelyanov1dab6222007-12-02 00:59:38 +11004670#define ADDRCONF_CTL_PATH_DEV 3
4671
4672 struct ctl_path addrconf_ctl_path[] = {
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08004673 { .procname = "net", },
4674 { .procname = "ipv6", },
4675 { .procname = "conf", },
Pavel Emelyanov1dab6222007-12-02 00:59:38 +11004676 { /* to be set */ },
4677 { },
4678 };
4679
4680
Arnaldo Carvalho de Meloaf879cc2006-11-17 12:14:37 -02004681 t = kmemdup(&addrconf_sysctl, sizeof(*t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004682 if (t == NULL)
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11004683 goto out;
4684
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07004685 for (i = 0; t->addrconf_vars[i].data; i++) {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004686 t->addrconf_vars[i].data += (char *)p - (char *)&ipv6_devconf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687 t->addrconf_vars[i].extra1 = idev; /* embedded; no ref */
Pavel Emelyanovbff16c22008-01-10 17:42:13 -08004688 t->addrconf_vars[i].extra2 = net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004690
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004691 /*
4692 * Make a copy of dev_name, because '.procname' is regarded as const
Linus Torvalds1da177e2005-04-16 15:20:36 -07004693 * by sysctl and we wouldn't want anyone to change it under our feet
4694 * (see SIOCSIFNAME).
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004695 */
Pavel Emelyanov1dab6222007-12-02 00:59:38 +11004696 t->dev_name = kstrdup(dev_name, GFP_KERNEL);
4697 if (!t->dev_name)
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11004698 goto free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004699
Pavel Emelyanov1dab6222007-12-02 00:59:38 +11004700 addrconf_ctl_path[ADDRCONF_CTL_PATH_DEV].procname = t->dev_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004701
Pavel Emelyanovbff16c22008-01-10 17:42:13 -08004702 t->sysctl_header = register_net_sysctl_table(net, addrconf_ctl_path,
Pavel Emelyanov1dab6222007-12-02 00:59:38 +11004703 t->addrconf_vars);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004704 if (t->sysctl_header == NULL)
4705 goto free_procname;
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11004706
4707 p->sysctl = t;
Pavel Emelyanov95897312008-01-10 17:41:45 -08004708 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11004710free_procname:
Pavel Emelyanov1dab6222007-12-02 00:59:38 +11004711 kfree(t->dev_name);
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11004712free:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004713 kfree(t);
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11004714out:
Pavel Emelyanov95897312008-01-10 17:41:45 -08004715 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004716}
4717
Pavel Emelyanov408c4762008-01-10 17:41:21 -08004718static void __addrconf_sysctl_unregister(struct ipv6_devconf *p)
4719{
4720 struct addrconf_sysctl_table *t;
4721
4722 if (p->sysctl == NULL)
4723 return;
4724
4725 t = p->sysctl;
4726 p->sysctl = NULL;
Lucian Adrian Grijincuff538812011-05-01 01:44:01 +00004727 unregister_net_sysctl_table(t->sysctl_header);
Pavel Emelyanov408c4762008-01-10 17:41:21 -08004728 kfree(t->dev_name);
4729 kfree(t);
4730}
4731
Pavel Emelyanovf52295a2007-12-02 00:58:37 +11004732static void addrconf_sysctl_register(struct inet6_dev *idev)
4733{
Eric W. Biederman54716e32010-02-14 03:27:03 +00004734 neigh_sysctl_register(idev->dev, idev->nd_parms, "ipv6",
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08004735 &ndisc_ifinfo_sysctl_change);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09004736 __addrconf_sysctl_register(dev_net(idev->dev), idev->dev->name,
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08004737 idev, &idev->cnf);
Pavel Emelyanovf52295a2007-12-02 00:58:37 +11004738}
4739
Pavel Emelyanov408c4762008-01-10 17:41:21 -08004740static void addrconf_sysctl_unregister(struct inet6_dev *idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741{
Pavel Emelyanov408c4762008-01-10 17:41:21 -08004742 __addrconf_sysctl_unregister(&idev->cnf);
4743 neigh_sysctl_unregister(idev->nd_parms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744}
4745
4746
4747#endif
4748
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004749static int __net_init addrconf_init_net(struct net *net)
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08004750{
Hong Zhiguo6a519972013-03-26 01:52:45 +08004751 int err = -ENOMEM;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08004752 struct ipv6_devconf *all, *dflt;
4753
Hong Zhiguo6a519972013-03-26 01:52:45 +08004754 all = kmemdup(&ipv6_devconf, sizeof(ipv6_devconf), GFP_KERNEL);
4755 if (all == NULL)
4756 goto err_alloc_all;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08004757
Hong Zhiguo6a519972013-03-26 01:52:45 +08004758 dflt = kmemdup(&ipv6_devconf_dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL);
4759 if (dflt == NULL)
4760 goto err_alloc_dflt;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08004761
Hong Zhiguo6a519972013-03-26 01:52:45 +08004762 /* these will be inherited by all namespaces */
4763 dflt->autoconf = ipv6_defaults.autoconf;
4764 dflt->disable_ipv6 = ipv6_defaults.disable_ipv6;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08004765
4766 net->ipv6.devconf_all = all;
4767 net->ipv6.devconf_dflt = dflt;
4768
4769#ifdef CONFIG_SYSCTL
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08004770 err = __addrconf_sysctl_register(net, "all", NULL, all);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08004771 if (err < 0)
4772 goto err_reg_all;
4773
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08004774 err = __addrconf_sysctl_register(net, "default", NULL, dflt);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08004775 if (err < 0)
4776 goto err_reg_dflt;
4777#endif
4778 return 0;
4779
4780#ifdef CONFIG_SYSCTL
4781err_reg_dflt:
4782 __addrconf_sysctl_unregister(all);
4783err_reg_all:
4784 kfree(dflt);
4785#endif
4786err_alloc_dflt:
4787 kfree(all);
4788err_alloc_all:
4789 return err;
4790}
4791
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004792static void __net_exit addrconf_exit_net(struct net *net)
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08004793{
4794#ifdef CONFIG_SYSCTL
4795 __addrconf_sysctl_unregister(net->ipv6.devconf_dflt);
4796 __addrconf_sysctl_unregister(net->ipv6.devconf_all);
4797#endif
Octavian Purdila09ad9bc2009-11-25 15:14:13 -08004798 if (!net_eq(net, &init_net)) {
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08004799 kfree(net->ipv6.devconf_dflt);
4800 kfree(net->ipv6.devconf_all);
4801 }
4802}
4803
4804static struct pernet_operations addrconf_ops = {
4805 .init = addrconf_init_net,
4806 .exit = addrconf_exit_net,
4807};
4808
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809/*
4810 * Device notifier
4811 */
4812
4813int register_inet6addr_notifier(struct notifier_block *nb)
4814{
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004815 return atomic_notifier_chain_register(&inet6addr_chain, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004816}
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +09004817EXPORT_SYMBOL(register_inet6addr_notifier);
4818
Linus Torvalds1da177e2005-04-16 15:20:36 -07004819int unregister_inet6addr_notifier(struct notifier_block *nb)
4820{
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004821 return atomic_notifier_chain_unregister(&inet6addr_chain, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004822}
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +09004823EXPORT_SYMBOL(unregister_inet6addr_notifier);
4824
Thomas Grafb382b192010-11-16 04:33:57 +00004825static struct rtnl_af_ops inet6_ops = {
4826 .family = AF_INET6,
4827 .fill_link_af = inet6_fill_link_af,
4828 .get_link_af_size = inet6_get_link_af_size,
Thomas Grafb382b192010-11-16 04:33:57 +00004829};
4830
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831/*
4832 * Init / cleanup code
4833 */
4834
4835int __init addrconf_init(void)
4836{
stephen hemmingerc2e21292010-03-17 20:31:10 +00004837 int i, err;
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +09004838
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004839 err = ipv6_addr_label_init();
4840 if (err < 0) {
4841 printk(KERN_CRIT "IPv6 Addrconf:"
4842 " cannot initialize default policy table: %d.\n", err);
Neil Horman2cc6d2b2010-09-24 09:55:52 +00004843 goto out;
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +09004844 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004845
Neil Horman2cc6d2b2010-09-24 09:55:52 +00004846 err = register_pernet_subsys(&addrconf_ops);
4847 if (err < 0)
4848 goto out_addrlabel;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08004849
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850 /* The addrconf netdev notifier requires that loopback_dev
4851 * has it's ipv6 private information allocated and setup
4852 * before it can bring up and give link-local addresses
4853 * to other devices which are up.
4854 *
4855 * Unfortunately, loopback_dev is not necessarily the first
4856 * entry in the global dev_base list of net devices. In fact,
4857 * it is likely to be the very last entry on that list.
4858 * So this causes the notifier registry below to try and
4859 * give link-local addresses to all devices besides loopback_dev
4860 * first, then loopback_dev, which cases all the non-loopback_dev
4861 * devices to fail to get a link-local address.
4862 *
4863 * So, as a temporary fix, allocate the ipv6 structure for
4864 * loopback_dev first by hand.
4865 * Longer term, all of the dependencies ipv6 has upon the loopback
4866 * device and it being up should be removed.
4867 */
4868 rtnl_lock();
Eric W. Biederman2774c7a2007-09-26 22:10:56 -07004869 if (!ipv6_add_dev(init_net.loopback_dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004870 err = -ENOMEM;
4871 rtnl_unlock();
4872 if (err)
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08004873 goto errlo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004874
stephen hemmingerc2e21292010-03-17 20:31:10 +00004875 for (i = 0; i < IN6_ADDR_HSIZE; i++)
4876 INIT_HLIST_HEAD(&inet6_addr_lst[i]);
4877
Linus Torvalds1da177e2005-04-16 15:20:36 -07004878 register_netdevice_notifier(&ipv6_dev_notf);
4879
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880 addrconf_verify(0);
Thomas Grafc127ea22007-03-22 11:58:32 -07004881
Thomas Grafb382b192010-11-16 04:33:57 +00004882 err = rtnl_af_register(&inet6_ops);
4883 if (err < 0)
4884 goto errout_af;
4885
Greg Rosec7ac8672011-06-10 01:27:09 +00004886 err = __rtnl_register(PF_INET6, RTM_GETLINK, NULL, inet6_dump_ifinfo,
4887 NULL);
Thomas Grafc127ea22007-03-22 11:58:32 -07004888 if (err < 0)
4889 goto errout;
4890
4891 /* Only the first call to __rtnl_register can fail */
Greg Rosec7ac8672011-06-10 01:27:09 +00004892 __rtnl_register(PF_INET6, RTM_NEWADDR, inet6_rtm_newaddr, NULL, NULL);
4893 __rtnl_register(PF_INET6, RTM_DELADDR, inet6_rtm_deladdr, NULL, NULL);
4894 __rtnl_register(PF_INET6, RTM_GETADDR, inet6_rtm_getaddr,
4895 inet6_dump_ifaddr, NULL);
4896 __rtnl_register(PF_INET6, RTM_GETMULTICAST, NULL,
4897 inet6_dump_ifmcaddr, NULL);
4898 __rtnl_register(PF_INET6, RTM_GETANYCAST, NULL,
4899 inet6_dump_ifacaddr, NULL);
Thomas Grafc127ea22007-03-22 11:58:32 -07004900
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +09004901 ipv6_addr_label_rtnl_register();
4902
Linus Torvalds1da177e2005-04-16 15:20:36 -07004903 return 0;
Thomas Grafc127ea22007-03-22 11:58:32 -07004904errout:
Thomas Grafb382b192010-11-16 04:33:57 +00004905 rtnl_af_unregister(&inet6_ops);
4906errout_af:
Thomas Grafc127ea22007-03-22 11:58:32 -07004907 unregister_netdevice_notifier(&ipv6_dev_notf);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08004908errlo:
4909 unregister_pernet_subsys(&addrconf_ops);
Neil Horman2cc6d2b2010-09-24 09:55:52 +00004910out_addrlabel:
4911 ipv6_addr_label_cleanup();
4912out:
Thomas Grafc127ea22007-03-22 11:58:32 -07004913 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914}
4915
Daniel Lezcano09f77092007-12-13 05:34:58 -08004916void addrconf_cleanup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004917{
Daniel Lezcano176c39a2009-03-03 01:06:45 -08004918 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004919 int i;
4920
4921 unregister_netdevice_notifier(&ipv6_dev_notf);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08004922 unregister_pernet_subsys(&addrconf_ops);
Neil Horman2cc6d2b2010-09-24 09:55:52 +00004923 ipv6_addr_label_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924
4925 rtnl_lock();
4926
Thomas Grafb382b192010-11-16 04:33:57 +00004927 __rtnl_af_unregister(&inet6_ops);
4928
Daniel Lezcano176c39a2009-03-03 01:06:45 -08004929 /* clean dev list */
4930 for_each_netdev(&init_net, dev) {
4931 if (__in6_dev_get(dev) == NULL)
4932 continue;
4933 addrconf_ifdown(dev, 1);
4934 }
4935 addrconf_ifdown(init_net.loopback_dev, 2);
4936
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004938 * Check hash table.
4939 */
stephen hemminger5c578ae2010-03-17 20:31:11 +00004940 spin_lock_bh(&addrconf_hash_lock);
stephen hemmingerc2e21292010-03-17 20:31:10 +00004941 for (i = 0; i < IN6_ADDR_HSIZE; i++)
4942 WARN_ON(!hlist_empty(&inet6_addr_lst[i]));
stephen hemminger5c578ae2010-03-17 20:31:11 +00004943 spin_unlock_bh(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944
4945 del_timer(&addr_chk_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004947}