blob: 9b33f5de59cfcd3d110c0debc3abe668edc21591 [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
Lorenzo Colitti653be182014-03-26 19:35:41 +0900198 .accept_ra_rt_table = 0,
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -0700199 .proxy_ndp = 0,
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -0700200 .accept_source_route = 0, /* we do not accept RH0 by default. */
YOSHIFUJI Hideaki778d80b2008-06-28 14:17:11 +0900201 .disable_ipv6 = 0,
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +0900202 .accept_dad = 1,
Harout Hedeshian8b2414d2012-12-21 12:38:38 -0800203 .accept_ra_prefix_route = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204};
205
Brian Haleyab32ea52006-09-22 14:15:41 -0700206static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 .forwarding = 0,
208 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
209 .mtu6 = IPV6_MIN_MTU,
210 .accept_ra = 1,
211 .accept_redirects = 1,
212 .autoconf = 1,
213 .dad_transmits = 1,
214 .rtr_solicits = MAX_RTR_SOLICITATIONS,
215 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
216 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
217#ifdef CONFIG_IPV6_PRIVACY
218 .use_tempaddr = 0,
219 .temp_valid_lft = TEMP_VALID_LIFETIME,
220 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
221 .regen_max_retry = REGEN_MAX_RETRY,
222 .max_desync_factor = MAX_DESYNC_FACTOR,
223#endif
224 .max_addresses = IPV6_MAX_ADDRESSES,
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -0800225 .accept_ra_defrtr = 1,
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -0800226 .accept_ra_pinfo = 1,
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800227#ifdef CONFIG_IPV6_ROUTER_PREF
228 .accept_ra_rtr_pref = 1,
YOSHIFUJI Hideaki52e16352006-03-20 17:05:47 -0800229 .rtr_probe_interval = 60 * HZ,
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -0800230#ifdef CONFIG_IPV6_ROUTE_INFO
231 .accept_ra_rt_info_max_plen = 0,
232#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800233#endif
Lorenzo Colitti653be182014-03-26 19:35:41 +0900234 .accept_ra_rt_table = 0,
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -0700235 .proxy_ndp = 0,
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -0700236 .accept_source_route = 0, /* we do not accept RH0 by default. */
YOSHIFUJI Hideaki778d80b2008-06-28 14:17:11 +0900237 .disable_ipv6 = 0,
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +0900238 .accept_dad = 1,
Harout Hedeshian8b2414d2012-12-21 12:38:38 -0800239 .accept_ra_prefix_route = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240};
241
242/* IPv6 Wildcard Address and Loopback Address defined by RFC2553 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +0900245const struct in6_addr in6addr_linklocal_allnodes = IN6ADDR_LINKLOCAL_ALLNODES_INIT;
246const struct in6_addr in6addr_linklocal_allrouters = IN6ADDR_LINKLOCAL_ALLROUTERS_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -0700248/* Check if a valid qdisc is available */
David S. Miller05297942008-07-08 23:01:27 -0700249static inline bool addrconf_qdisc_ok(const struct net_device *dev)
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -0700250{
David S. Miller05297942008-07-08 23:01:27 -0700251 return !qdisc_tx_is_noop(dev);
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -0700252}
253
YOSHIFUJI Hideaki2b5ead42008-05-13 01:16:24 +0900254/* Check if a route is valid prefix route */
255static inline int addrconf_is_prefix_route(const struct rt6_info *rt)
256{
Eric Dumazeta02cec22010-09-22 20:43:57 +0000257 return (rt->rt6i_flags & (RTF_GATEWAY | RTF_DEFAULT)) == 0;
YOSHIFUJI Hideaki2b5ead42008-05-13 01:16:24 +0900258}
259
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260static void addrconf_del_timer(struct inet6_ifaddr *ifp)
261{
262 if (del_timer(&ifp->timer))
263 __in6_ifa_put(ifp);
264}
265
Stephen Hemmingere21e8462010-03-20 16:09:01 -0700266enum addrconf_timer_t {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 AC_NONE,
268 AC_DAD,
269 AC_RS,
270};
271
272static void addrconf_mod_timer(struct inet6_ifaddr *ifp,
273 enum addrconf_timer_t what,
274 unsigned long when)
275{
276 if (!del_timer(&ifp->timer))
277 in6_ifa_hold(ifp);
278
279 switch (what) {
280 case AC_DAD:
281 ifp->timer.function = addrconf_dad_timer;
282 break;
283 case AC_RS:
284 ifp->timer.function = addrconf_rs_timer;
285 break;
Stephen Hemmingere21e8462010-03-20 16:09:01 -0700286 default:
287 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 }
289 ifp->timer.expires = jiffies + when;
290 add_timer(&ifp->timer);
291}
292
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700293static int snmp6_alloc_dev(struct inet6_dev *idev)
294{
Tejun Heo7d720c32010-02-16 15:20:26 +0000295 if (snmp_mib_init((void __percpu **)idev->stats.ipv6,
Eric Dumazet1823e4c82010-06-22 20:58:41 +0000296 sizeof(struct ipstats_mib),
297 __alignof__(struct ipstats_mib)) < 0)
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700298 goto err_ip;
Eric Dumazetbe281e52011-05-19 01:14:23 +0000299 idev->stats.icmpv6dev = kzalloc(sizeof(struct icmpv6_mib_device),
300 GFP_KERNEL);
301 if (!idev->stats.icmpv6dev)
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700302 goto err_icmp;
Eric Dumazetbe281e52011-05-19 01:14:23 +0000303 idev->stats.icmpv6msgdev = kzalloc(sizeof(struct icmpv6msg_mib_device),
304 GFP_KERNEL);
305 if (!idev->stats.icmpv6msgdev)
David L Stevens14878f72007-09-16 16:52:35 -0700306 goto err_icmpmsg;
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700307
308 return 0;
309
David L Stevens14878f72007-09-16 16:52:35 -0700310err_icmpmsg:
Eric Dumazetbe281e52011-05-19 01:14:23 +0000311 kfree(idev->stats.icmpv6dev);
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700312err_icmp:
Tejun Heo7d720c32010-02-16 15:20:26 +0000313 snmp_mib_free((void __percpu **)idev->stats.ipv6);
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700314err_ip:
Pavel Emelyanovaaf70ec2007-10-17 21:25:32 -0700315 return -ENOMEM;
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700316}
317
Pavel Emelyanov16910b92007-10-17 21:23:43 -0700318static void snmp6_free_dev(struct inet6_dev *idev)
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700319{
Eric Dumazetbe281e52011-05-19 01:14:23 +0000320 kfree(idev->stats.icmpv6msgdev);
321 kfree(idev->stats.icmpv6dev);
Tejun Heo7d720c32010-02-16 15:20:26 +0000322 snmp_mib_free((void __percpu **)idev->stats.ipv6);
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700323}
324
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325/* Nobody refers to this device, we may destroy it. */
326
327void in6_dev_finish_destroy(struct inet6_dev *idev)
328{
329 struct net_device *dev = idev->dev;
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700330
stephen hemminger502a2ff2010-03-17 20:31:13 +0000331 WARN_ON(!list_empty(&idev->addr_list));
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700332 WARN_ON(idev->mc_list != NULL);
333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334#ifdef NET_REFCNT_DEBUG
335 printk(KERN_DEBUG "in6_dev_finish_destroy: %s\n", dev ? dev->name : "NIL");
336#endif
337 dev_put(dev);
338 if (!idev->dead) {
Stephen Hemmingere21e8462010-03-20 16:09:01 -0700339 pr_warning("Freeing alive inet6 device %p\n", idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 return;
341 }
342 snmp6_free_dev(idev);
Lai Jiangshan38f57d12011-03-15 17:59:14 +0800343 kfree_rcu(idev, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344}
345
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +0900346EXPORT_SYMBOL(in6_dev_finish_destroy);
347
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348static struct inet6_dev * ipv6_add_dev(struct net_device *dev)
349{
350 struct inet6_dev *ndev;
351
352 ASSERT_RTNL();
353
354 if (dev->mtu < IPV6_MIN_MTU)
355 return NULL;
356
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900357 ndev = kzalloc(sizeof(struct inet6_dev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900359 if (ndev == NULL)
360 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
Ingo Oeser322f74a2006-03-20 23:01:47 -0800362 rwlock_init(&ndev->lock);
363 ndev->dev = dev;
stephen hemminger502a2ff2010-03-17 20:31:13 +0000364 INIT_LIST_HEAD(&ndev->addr_list);
365
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900366 memcpy(&ndev->cnf, dev_net(dev)->ipv6.devconf_dflt, sizeof(ndev->cnf));
Ingo Oeser322f74a2006-03-20 23:01:47 -0800367 ndev->cnf.mtu6 = dev->mtu;
368 ndev->cnf.sysctl = NULL;
369 ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl);
370 if (ndev->nd_parms == NULL) {
371 kfree(ndev);
372 return NULL;
373 }
Ben Hutchings0187bdf2008-06-19 16:15:47 -0700374 if (ndev->cnf.forwarding)
375 dev_disable_lro(dev);
Ingo Oeser322f74a2006-03-20 23:01:47 -0800376 /* We refer to the device */
377 dev_hold(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Ingo Oeser322f74a2006-03-20 23:01:47 -0800379 if (snmp6_alloc_dev(ndev) < 0) {
380 ADBG((KERN_WARNING
381 "%s(): cannot allocate memory for statistics; dev=%s.\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800382 __func__, dev->name));
Ingo Oeser322f74a2006-03-20 23:01:47 -0800383 neigh_parms_release(&nd_tbl, ndev->nd_parms);
Roy Li8603e332011-09-20 15:10:16 -0400384 dev_put(dev);
385 kfree(ndev);
Ingo Oeser322f74a2006-03-20 23:01:47 -0800386 return NULL;
387 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Ingo Oeser322f74a2006-03-20 23:01:47 -0800389 if (snmp6_register_dev(ndev) < 0) {
390 ADBG((KERN_WARNING
391 "%s(): cannot create /proc/net/dev_snmp6/%s\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800392 __func__, dev->name));
Ingo Oeser322f74a2006-03-20 23:01:47 -0800393 neigh_parms_release(&nd_tbl, ndev->nd_parms);
394 ndev->dead = 1;
395 in6_dev_finish_destroy(ndev);
396 return NULL;
397 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Ingo Oeser322f74a2006-03-20 23:01:47 -0800399 /* One reference from device. We must do this before
400 * we invoke __ipv6_regen_rndid().
401 */
402 in6_dev_hold(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +0900404 if (dev->flags & (IFF_NOARP | IFF_LOOPBACK))
405 ndev->cnf.accept_dad = -1;
406
YOSHIFUJI Hideakib077d7a2008-04-13 23:42:18 -0700407#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
408 if (dev->type == ARPHRD_SIT && (dev->priv_flags & IFF_ISATAP)) {
409 printk(KERN_INFO
410 "%s: Disabled Multicast RS\n",
411 dev->name);
412 ndev->cnf.rtr_solicits = 0;
413 }
414#endif
415
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416#ifdef CONFIG_IPV6_PRIVACY
stephen hemminger372e6c82010-03-17 20:31:09 +0000417 INIT_LIST_HEAD(&ndev->tempaddr_list);
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800418 setup_timer(&ndev->regen_timer, ipv6_regen_rndid, (unsigned long)ndev);
Ingo Oeser322f74a2006-03-20 23:01:47 -0800419 if ((dev->flags&IFF_LOOPBACK) ||
420 dev->type == ARPHRD_TUNNEL ||
YOSHIFUJI Hideaki9625ed72008-04-13 23:47:11 -0700421 dev->type == ARPHRD_TUNNEL6 ||
Joerg Roedel0be669b2006-10-10 14:49:53 -0700422 dev->type == ARPHRD_SIT ||
Joerg Roedel0be669b2006-10-10 14:49:53 -0700423 dev->type == ARPHRD_NONE) {
Ingo Oeser322f74a2006-03-20 23:01:47 -0800424 ndev->cnf.use_tempaddr = -1;
425 } else {
426 in6_dev_hold(ndev);
427 ipv6_regen_rndid((unsigned long) ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 }
Ingo Oeser322f74a2006-03-20 23:01:47 -0800429#endif
430
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -0700431 if (netif_running(dev) && addrconf_qdisc_ok(dev))
Herbert Xu53aadcc2007-03-27 14:31:52 -0700432 ndev->if_flags |= IF_READY;
433
Ingo Oeser322f74a2006-03-20 23:01:47 -0800434 ipv6_mc_init_dev(ndev);
435 ndev->tstamp = jiffies;
Pavel Emelyanovf52295a2007-12-02 00:58:37 +1100436 addrconf_sysctl_register(ndev);
David L Stevens30c4cf52007-01-04 12:31:14 -0800437 /* protected by rtnl_lock */
Eric Dumazetcf778b02012-01-12 04:41:32 +0000438 rcu_assign_pointer(dev->ip6_ptr, ndev);
YOSHIFUJI Hideakid88ae4c2007-01-14 21:48:40 -0800439
440 /* Join all-node multicast group */
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +0900441 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allnodes);
YOSHIFUJI Hideakid88ae4c2007-01-14 21:48:40 -0800442
Li Weid6ddef92012-03-05 14:45:17 +0000443 /* Join all-router multicast group if forwarding is set */
Li Wei8b2aaed2012-03-07 14:58:07 +0000444 if (ndev->cnf.forwarding && (dev->flags & IFF_MULTICAST))
Li Weid6ddef92012-03-05 14:45:17 +0000445 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
446
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 return ndev;
448}
449
YOSHIFUJI Hideakicee89472008-04-13 23:21:16 -0700450static struct inet6_dev * ipv6_find_idev(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
452 struct inet6_dev *idev;
453
454 ASSERT_RTNL();
455
Stephen Hemmingere21e8462010-03-20 16:09:01 -0700456 idev = __in6_dev_get(dev);
457 if (!idev) {
458 idev = ipv6_add_dev(dev);
459 if (!idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 return NULL;
461 }
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +0900462
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 if (dev->flags&IFF_UP)
464 ipv6_mc_up(idev);
465 return idev;
466}
467
468#ifdef CONFIG_SYSCTL
469static void dev_forward_change(struct inet6_dev *idev)
470{
471 struct net_device *dev;
472 struct inet6_ifaddr *ifa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
474 if (!idev)
475 return;
476 dev = idev->dev;
Ben Hutchings0187bdf2008-06-19 16:15:47 -0700477 if (idev->cnf.forwarding)
478 dev_disable_lro(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 if (dev && (dev->flags & IFF_MULTICAST)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 if (idev->cnf.forwarding)
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +0900481 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 else
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +0900483 ipv6_dev_mc_dec(dev, &in6addr_linklocal_allrouters);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 }
stephen hemminger502a2ff2010-03-17 20:31:13 +0000485
486 list_for_each_entry(ifa, &idev->addr_list, if_list) {
Michal Wrobel2c12a742007-02-26 15:36:10 -0800487 if (ifa->flags&IFA_F_TENTATIVE)
488 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 if (idev->cnf.forwarding)
490 addrconf_join_anycast(ifa);
491 else
492 addrconf_leave_anycast(ifa);
493 }
494}
495
496
Pavel Emelyanove1869322008-01-10 17:43:50 -0800497static void addrconf_forward_change(struct net *net, __s32 newf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
499 struct net_device *dev;
500 struct inet6_dev *idev;
501
Ben Hutchings84a2d3c2012-08-14 08:54:51 +0000502 for_each_netdev(net, dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 idev = __in6_dev_get(dev);
504 if (idev) {
Pavel Emelyanove1869322008-01-10 17:43:50 -0800505 int changed = (!idev->cnf.forwarding) ^ (!newf);
506 idev->cnf.forwarding = newf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 if (changed)
508 dev_forward_change(idev);
509 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511}
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800512
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000513static int addrconf_fixup_forwarding(struct ctl_table *table, int *p, int newf)
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800514{
Pavel Emelyanovbff16c22008-01-10 17:42:13 -0800515 struct net *net;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000516 int old;
517
518 if (!rtnl_trylock())
519 return restart_syscall();
Pavel Emelyanovbff16c22008-01-10 17:42:13 -0800520
521 net = (struct net *)table->extra2;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000522 old = *p;
523 *p = newf;
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800524
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000525 if (p == &net->ipv6.devconf_dflt->forwarding) {
526 rtnl_unlock();
527 return 0;
Eric W. Biederman88af1822010-02-19 13:22:59 +0000528 }
Stephen Hemmingerb325fdd2009-02-26 06:55:31 +0000529
Pavel Emelyanove1869322008-01-10 17:43:50 -0800530 if (p == &net->ipv6.devconf_all->forwarding) {
Pavel Emelyanove1869322008-01-10 17:43:50 -0800531 net->ipv6.devconf_dflt->forwarding = newf;
532 addrconf_forward_change(net, newf);
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000533 } else if ((!newf) ^ (!old))
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800534 dev_forward_change((struct inet6_dev *)table->extra1);
Ben Hutchings0187bdf2008-06-19 16:15:47 -0700535 rtnl_unlock();
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800536
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000537 if (newf)
Daniel Lezcano7b4da532008-03-04 13:47:14 -0800538 rt6_purge_dflt_routers(net);
Stephen Hemmingerb325fdd2009-02-26 06:55:31 +0000539 return 1;
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800540}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541#endif
542
stephen hemminger5c578ae2010-03-17 20:31:11 +0000543/* Nobody refers to this ifaddr, destroy it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp)
545{
stephen hemmingerc2e21292010-03-17 20:31:10 +0000546 WARN_ON(!hlist_unhashed(&ifp->addr_lst));
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700547
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548#ifdef NET_REFCNT_DEBUG
549 printk(KERN_DEBUG "inet6_ifa_finish_destroy\n");
550#endif
551
552 in6_dev_put(ifp->idev);
553
554 if (del_timer(&ifp->timer))
Stephen Hemmingere21e8462010-03-20 16:09:01 -0700555 pr_notice("Timer is still running, when freeing ifa=%p\n", ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Herbert Xue9d3e082010-05-18 15:36:06 -0700557 if (ifp->state != INET6_IFADDR_STATE_DEAD) {
Stephen Hemmingere21e8462010-03-20 16:09:01 -0700558 pr_warning("Freeing alive inet6 address %p\n", ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 return;
560 }
Changli Gaod8d1f302010-06-10 23:31:35 -0700561 dst_release(&ifp->rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
Lai Jiangshane5785982011-03-15 18:00:14 +0800563 kfree_rcu(ifp, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564}
565
Brian Haleye55ffac2006-07-10 15:25:51 -0700566static void
567ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp)
568{
stephen hemminger502a2ff2010-03-17 20:31:13 +0000569 struct list_head *p;
YOSHIFUJI Hideaki8a6ce0c2006-07-11 13:05:30 -0700570 int ifp_scope = ipv6_addr_src_scope(&ifp->addr);
Brian Haleye55ffac2006-07-10 15:25:51 -0700571
572 /*
573 * Each device address list is sorted in order of scope -
574 * global before linklocal.
575 */
stephen hemminger502a2ff2010-03-17 20:31:13 +0000576 list_for_each(p, &idev->addr_list) {
577 struct inet6_ifaddr *ifa
578 = list_entry(p, struct inet6_ifaddr, if_list);
YOSHIFUJI Hideaki8a6ce0c2006-07-11 13:05:30 -0700579 if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr))
Brian Haleye55ffac2006-07-10 15:25:51 -0700580 break;
581 }
582
David S. Millerb54c9b92010-03-25 21:25:30 -0700583 list_add_tail(&ifp->if_list, p);
Brian Haleye55ffac2006-07-10 15:25:51 -0700584}
585
stephen hemminger3a88a812010-03-17 20:31:12 +0000586static u32 ipv6_addr_hash(const struct in6_addr *addr)
YOSHIFUJI Hideaki3eb84f42008-04-10 15:42:08 +0900587{
YOSHIFUJI Hideaki3eb84f42008-04-10 15:42:08 +0900588 /*
589 * We perform the hash function over the last 64 bits of the address
590 * This will include the IEEE address token on links that support it.
591 */
Eric Dumazet0eae88f2010-04-20 19:06:52 -0700592 return jhash_2words((__force u32)addr->s6_addr32[2],
593 (__force u32)addr->s6_addr32[3], 0)
stephen hemminger3a88a812010-03-17 20:31:12 +0000594 & (IN6_ADDR_HSIZE - 1);
YOSHIFUJI Hideaki3eb84f42008-04-10 15:42:08 +0900595}
596
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597/* On success it returns ifp with increased reference count */
598
599static struct inet6_ifaddr *
600ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr, int pfxlen,
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -0700601 int scope, u32 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602{
603 struct inet6_ifaddr *ifa = NULL;
604 struct rt6_info *rt;
stephen hemminger3a88a812010-03-17 20:31:12 +0000605 unsigned int hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 int err = 0;
YOSHIFUJI Hideakid68b8272008-06-25 16:26:47 +0900607 int addr_type = ipv6_addr_type(addr);
608
609 if (addr_type == IPV6_ADDR_ANY ||
610 addr_type & IPV6_ADDR_MULTICAST ||
611 (!(idev->dev->flags & IFF_LOOPBACK) &&
612 addr_type & IPV6_ADDR_LOOPBACK))
613 return ERR_PTR(-EADDRNOTAVAIL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -0700615 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 if (idev->dead) {
617 err = -ENODEV; /*XXX*/
618 goto out2;
619 }
620
Brian Haley56d417b2009-06-01 03:07:33 -0700621 if (idev->cnf.disable_ipv6) {
Brian Haley9bdd8d42009-03-18 18:22:48 -0700622 err = -EACCES;
623 goto out2;
624 }
625
stephen hemminger5c578ae2010-03-17 20:31:11 +0000626 spin_lock(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
628 /* Ignore adding duplicate addresses on an interface */
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900629 if (ipv6_chk_same_addr(dev_net(idev->dev), addr, idev->dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 ADBG(("ipv6_add_addr: already assigned\n"));
631 err = -EEXIST;
632 goto out;
633 }
634
Ingo Oeser322f74a2006-03-20 23:01:47 -0800635 ifa = kzalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
637 if (ifa == NULL) {
638 ADBG(("ipv6_add_addr: malloc failed\n"));
639 err = -ENOBUFS;
640 goto out;
641 }
642
David S. Miller8f031512011-12-06 16:48:14 -0500643 rt = addrconf_dst_alloc(idev, addr, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 if (IS_ERR(rt)) {
645 err = PTR_ERR(rt);
646 goto out;
647 }
648
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000649 ifa->addr = *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
651 spin_lock_init(&ifa->lock);
Herbert Xue9d3e082010-05-18 15:36:06 -0700652 spin_lock_init(&ifa->state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 init_timer(&ifa->timer);
stephen hemmingerc2e21292010-03-17 20:31:10 +0000654 INIT_HLIST_NODE(&ifa->addr_lst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 ifa->timer.data = (unsigned long) ifa;
656 ifa->scope = scope;
657 ifa->prefix_len = pfxlen;
658 ifa->flags = flags | IFA_F_TENTATIVE;
659 ifa->cstamp = ifa->tstamp = jiffies;
660
Keir Fraser57f5f542006-08-29 02:43:49 -0700661 ifa->rt = rt;
662
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 ifa->idev = idev;
664 in6_dev_hold(idev);
665 /* For caller */
666 in6_ifa_hold(ifa);
667
668 /* Add to big hash table */
669 hash = ipv6_addr_hash(addr);
670
stephen hemminger5c578ae2010-03-17 20:31:11 +0000671 hlist_add_head_rcu(&ifa->addr_lst, &inet6_addr_lst[hash]);
stephen hemminger5c578ae2010-03-17 20:31:11 +0000672 spin_unlock(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
674 write_lock(&idev->lock);
675 /* Add to inet6_dev unicast addr list. */
Brian Haleye55ffac2006-07-10 15:25:51 -0700676 ipv6_link_dev_addr(idev, ifa);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
678#ifdef CONFIG_IPV6_PRIVACY
679 if (ifa->flags&IFA_F_TEMPORARY) {
stephen hemminger372e6c82010-03-17 20:31:09 +0000680 list_add(&ifa->tmp_list, &idev->tempaddr_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 in6_ifa_hold(ifa);
682 }
683#endif
684
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 in6_ifa_hold(ifa);
686 write_unlock(&idev->lock);
687out2:
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -0700688 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
YOSHIFUJI Hideakifd928332005-04-19 22:27:09 -0700690 if (likely(err == 0))
Alan Sterne041c682006-03-27 01:16:30 -0800691 atomic_notifier_call_chain(&inet6addr_chain, NETDEV_UP, ifa);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 else {
693 kfree(ifa);
694 ifa = ERR_PTR(err);
695 }
696
697 return ifa;
698out:
stephen hemminger5c578ae2010-03-17 20:31:11 +0000699 spin_unlock(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 goto out2;
701}
702
703/* This function wants to get referenced ifp and releases it before return */
704
705static void ipv6_del_addr(struct inet6_ifaddr *ifp)
706{
stephen hemminger502a2ff2010-03-17 20:31:13 +0000707 struct inet6_ifaddr *ifa, *ifn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 struct inet6_dev *idev = ifp->idev;
Herbert Xu4c5ff6a2010-05-18 15:54:18 -0700709 int state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 int deleted = 0, onlink = 0;
711 unsigned long expires = jiffies;
712
Herbert Xu4c5ff6a2010-05-18 15:54:18 -0700713 spin_lock_bh(&ifp->state_lock);
714 state = ifp->state;
Herbert Xue9d3e082010-05-18 15:36:06 -0700715 ifp->state = INET6_IFADDR_STATE_DEAD;
Herbert Xu4c5ff6a2010-05-18 15:54:18 -0700716 spin_unlock_bh(&ifp->state_lock);
717
718 if (state == INET6_IFADDR_STATE_DEAD)
719 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
stephen hemminger5c578ae2010-03-17 20:31:11 +0000721 spin_lock_bh(&addrconf_hash_lock);
722 hlist_del_init_rcu(&ifp->addr_lst);
stephen hemminger5c578ae2010-03-17 20:31:11 +0000723 spin_unlock_bh(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
725 write_lock_bh(&idev->lock);
726#ifdef CONFIG_IPV6_PRIVACY
727 if (ifp->flags&IFA_F_TEMPORARY) {
stephen hemminger372e6c82010-03-17 20:31:09 +0000728 list_del(&ifp->tmp_list);
729 if (ifp->ifpub) {
730 in6_ifa_put(ifp->ifpub);
731 ifp->ifpub = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 }
stephen hemminger372e6c82010-03-17 20:31:09 +0000733 __in6_ifa_put(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 }
735#endif
736
stephen hemminger502a2ff2010-03-17 20:31:13 +0000737 list_for_each_entry_safe(ifa, ifn, &idev->addr_list, if_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 if (ifa == ifp) {
stephen hemminger502a2ff2010-03-17 20:31:13 +0000739 list_del_init(&ifp->if_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 __in6_ifa_put(ifp);
stephen hemminger502a2ff2010-03-17 20:31:13 +0000741
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 if (!(ifp->flags & IFA_F_PERMANENT) || onlink > 0)
743 break;
744 deleted = 1;
Kristian Slavov1d142802005-12-21 18:47:24 -0800745 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 } else if (ifp->flags & IFA_F_PERMANENT) {
747 if (ipv6_prefix_equal(&ifa->addr, &ifp->addr,
748 ifp->prefix_len)) {
749 if (ifa->flags & IFA_F_PERMANENT) {
750 onlink = 1;
751 if (deleted)
752 break;
753 } else {
754 unsigned long lifetime;
755
756 if (!onlink)
757 onlink = -1;
758
759 spin_lock(&ifa->lock);
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +0900760
761 lifetime = addrconf_timeout_fixup(ifa->valid_lft, HZ);
762 /*
763 * Note: Because this address is
764 * not permanent, lifetime <
765 * LONG_MAX / HZ here.
766 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 if (time_before(expires,
768 ifa->tstamp + lifetime * HZ))
769 expires = ifa->tstamp + lifetime * HZ;
770 spin_unlock(&ifa->lock);
771 }
772 }
773 }
774 }
775 write_unlock_bh(&idev->lock);
776
Andrey Vaginb2238562008-07-08 15:13:31 -0700777 addrconf_del_timer(ifp);
778
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 ipv6_ifa_notify(RTM_DELADDR, ifp);
780
Alan Sterne041c682006-03-27 01:16:30 -0800781 atomic_notifier_call_chain(&inet6addr_chain, NETDEV_DOWN, ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 /*
784 * Purge or update corresponding prefix
785 *
786 * 1) we don't purge prefix here if address was not permanent.
787 * prefix is managed by its own lifetime.
788 * 2) if there're no addresses, delete prefix.
789 * 3) if there're still other permanent address(es),
790 * corresponding prefix is still permanent.
791 * 4) otherwise, update prefix lifetime to the
792 * longest valid lifetime among the corresponding
793 * addresses on the device.
794 * Note: subsequent RA will update lifetime.
795 *
796 * --yoshfuji
797 */
798 if ((ifp->flags & IFA_F_PERMANENT) && onlink < 1) {
799 struct in6_addr prefix;
800 struct rt6_info *rt;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900801 struct net *net = dev_net(ifp->idev->dev);
Nicolas Dichteld5e36b02012-09-26 00:04:55 +0000802 struct flowi6 fl6 = {};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
Nicolas Dichteld5e36b02012-09-26 00:04:55 +0000804 ipv6_addr_prefix(&prefix, &ifp->addr, ifp->prefix_len);
805 fl6.flowi6_oif = ifp->idev->dev->ifindex;
806 fl6.daddr = prefix;
807 rt = (struct rt6_info *)ip6_route_lookup(net, &fl6,
808 RT6_LOOKUP_F_IFACE);
809
810 if (rt != net->ipv6.ip6_null_entry &&
811 addrconf_is_prefix_route(rt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 if (onlink == 0) {
Thomas Grafe0a1ad732006-08-22 00:00:21 -0700813 ip6_del_rt(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 rt = NULL;
815 } else if (!(rt->rt6i_flags & RTF_EXPIRES)) {
Gao feng1716a962012-04-06 00:13:10 +0000816 rt6_set_expires(rt, expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 }
818 }
Changli Gaod8d1f302010-06-10 23:31:35 -0700819 dst_release(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 }
821
Daniel Walterc3968a82011-04-13 21:10:57 +0000822 /* clean up prefsrc entries */
823 rt6_remove_prefsrc(ifp);
Herbert Xu4c5ff6a2010-05-18 15:54:18 -0700824out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 in6_ifa_put(ifp);
826}
827
828#ifdef CONFIG_IPV6_PRIVACY
829static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift)
830{
831 struct inet6_dev *idev = ifp->idev;
832 struct in6_addr addr, *tmpaddr;
Lorenzo Colitti76f793e2011-07-26 13:50:49 +0000833 unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_tstamp, age;
Benoit Boissinoteac55bf2008-04-02 00:01:35 -0700834 unsigned long regen_advance;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 int tmp_plen;
836 int ret = 0;
837 int max_addresses;
Neil Horman95c385b2007-04-25 17:08:10 -0700838 u32 addr_flags;
Lorenzo Colitti76f793e2011-07-26 13:50:49 +0000839 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
841 write_lock(&idev->lock);
842 if (ift) {
843 spin_lock_bh(&ift->lock);
844 memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8);
845 spin_unlock_bh(&ift->lock);
846 tmpaddr = &addr;
847 } else {
848 tmpaddr = NULL;
849 }
850retry:
851 in6_dev_hold(idev);
852 if (idev->cnf.use_tempaddr <= 0) {
853 write_unlock(&idev->lock);
854 printk(KERN_INFO
855 "ipv6_create_tempaddr(): use_tempaddr is disabled.\n");
856 in6_dev_put(idev);
857 ret = -1;
858 goto out;
859 }
860 spin_lock_bh(&ifp->lock);
861 if (ifp->regen_count++ >= idev->cnf.regen_max_retry) {
862 idev->cnf.use_tempaddr = -1; /*XXX*/
863 spin_unlock_bh(&ifp->lock);
864 write_unlock(&idev->lock);
865 printk(KERN_WARNING
866 "ipv6_create_tempaddr(): regeneration time exceeded. disabled temporary address support.\n");
867 in6_dev_put(idev);
868 ret = -1;
869 goto out;
870 }
871 in6_ifa_hold(ifp);
872 memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
873 if (__ipv6_try_regen_rndid(idev, tmpaddr) < 0) {
874 spin_unlock_bh(&ifp->lock);
875 write_unlock(&idev->lock);
876 printk(KERN_WARNING
877 "ipv6_create_tempaddr(): regeneration of randomized interface id failed.\n");
878 in6_ifa_put(ifp);
879 in6_dev_put(idev);
880 ret = -1;
881 goto out;
882 }
883 memcpy(&addr.s6_addr[8], idev->rndid, 8);
Lorenzo Colitti76f793e2011-07-26 13:50:49 +0000884 age = (now - ifp->tstamp) / HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 tmp_valid_lft = min_t(__u32,
886 ifp->valid_lft,
Glenn Wurster7a876b02010-09-27 07:10:10 +0000887 idev->cnf.temp_valid_lft + age);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900888 tmp_prefered_lft = min_t(__u32,
889 ifp->prefered_lft,
Glenn Wurster7a876b02010-09-27 07:10:10 +0000890 idev->cnf.temp_prefered_lft + age -
Ben Hutchings784e2712010-06-26 11:42:55 +0000891 idev->cnf.max_desync_factor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 tmp_plen = ifp->prefix_len;
893 max_addresses = idev->cnf.max_addresses;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 tmp_tstamp = ifp->tstamp;
895 spin_unlock_bh(&ifp->lock);
896
Benoit Boissinoteac55bf2008-04-02 00:01:35 -0700897 regen_advance = idev->cnf.regen_max_retry *
898 idev->cnf.dad_transmits *
899 idev->nd_parms->retrans_time / HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 write_unlock(&idev->lock);
Neil Horman95c385b2007-04-25 17:08:10 -0700901
Benoit Boissinoteac55bf2008-04-02 00:01:35 -0700902 /* A temporary address is created only if this calculated Preferred
903 * Lifetime is greater than REGEN_ADVANCE time units. In particular,
904 * an implementation must not create a temporary address with a zero
905 * Preferred Lifetime.
906 */
907 if (tmp_prefered_lft <= regen_advance) {
908 in6_ifa_put(ifp);
909 in6_dev_put(idev);
910 ret = -1;
911 goto out;
912 }
913
Neil Horman95c385b2007-04-25 17:08:10 -0700914 addr_flags = IFA_F_TEMPORARY;
915 /* set in addrconf_prefix_rcv() */
916 if (ifp->flags & IFA_F_OPTIMISTIC)
917 addr_flags |= IFA_F_OPTIMISTIC;
918
Hannes Frederic Sowaf4cb8372013-08-16 13:02:27 +0200919 ift = ipv6_add_addr(idev, &addr, tmp_plen,
920 ipv6_addr_type(&addr)&IPV6_ADDR_SCOPE_MASK,
921 addr_flags);
922 if (IS_ERR(ift)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 in6_ifa_put(ifp);
924 in6_dev_put(idev);
925 printk(KERN_INFO
926 "ipv6_create_tempaddr(): retry temporary address regeneration.\n");
927 tmpaddr = &addr;
928 write_lock(&idev->lock);
929 goto retry;
930 }
931
932 spin_lock_bh(&ift->lock);
933 ift->ifpub = ifp;
934 ift->valid_lft = tmp_valid_lft;
935 ift->prefered_lft = tmp_prefered_lft;
Lorenzo Colitti76f793e2011-07-26 13:50:49 +0000936 ift->cstamp = now;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 ift->tstamp = tmp_tstamp;
938 spin_unlock_bh(&ift->lock);
939
940 addrconf_dad_start(ift, 0);
941 in6_ifa_put(ift);
942 in6_dev_put(idev);
943out:
944 return ret;
945}
946#endif
947
948/*
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800949 * Choose an appropriate source address (RFC3484)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 */
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +0900951enum {
952 IPV6_SADDR_RULE_INIT = 0,
953 IPV6_SADDR_RULE_LOCAL,
954 IPV6_SADDR_RULE_SCOPE,
955 IPV6_SADDR_RULE_PREFERRED,
956#ifdef CONFIG_IPV6_MIP6
957 IPV6_SADDR_RULE_HOA,
958#endif
959 IPV6_SADDR_RULE_OIF,
960 IPV6_SADDR_RULE_LABEL,
961#ifdef CONFIG_IPV6_PRIVACY
962 IPV6_SADDR_RULE_PRIVACY,
963#endif
964 IPV6_SADDR_RULE_ORCHID,
965 IPV6_SADDR_RULE_PREFIX,
966 IPV6_SADDR_RULE_MAX
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800967};
968
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +0900969struct ipv6_saddr_score {
970 int rule;
971 int addr_type;
972 struct inet6_ifaddr *ifa;
973 DECLARE_BITMAP(scorebits, IPV6_SADDR_RULE_MAX);
974 int scopedist;
975 int matchlen;
976};
977
978struct ipv6_saddr_dst {
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +0900979 const struct in6_addr *addr;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +0900980 int ifindex;
981 int scope;
982 int label;
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +0900983 unsigned int prefs;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +0900984};
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800985
Dave Jonesb6f99a22007-03-22 12:27:49 -0700986static inline int ipv6_saddr_preferred(int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987{
Ulrich Weber45bb0062010-02-25 23:28:58 +0000988 if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|IPV6_ADDR_LOOPBACK))
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800989 return 1;
990 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991}
992
Benjamin Thery3de23252008-05-28 14:51:24 +0200993static int ipv6_get_saddr_eval(struct net *net,
994 struct ipv6_saddr_score *score,
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +0900995 struct ipv6_saddr_dst *dst,
996 int i)
997{
998 int ret;
999
1000 if (i <= score->rule) {
1001 switch (i) {
1002 case IPV6_SADDR_RULE_SCOPE:
1003 ret = score->scopedist;
1004 break;
1005 case IPV6_SADDR_RULE_PREFIX:
1006 ret = score->matchlen;
1007 break;
1008 default:
1009 ret = !!test_bit(i, score->scorebits);
1010 }
1011 goto out;
1012 }
1013
1014 switch (i) {
1015 case IPV6_SADDR_RULE_INIT:
1016 /* Rule 0: remember if hiscore is not ready yet */
1017 ret = !!score->ifa;
1018 break;
1019 case IPV6_SADDR_RULE_LOCAL:
1020 /* Rule 1: Prefer same address */
1021 ret = ipv6_addr_equal(&score->ifa->addr, dst->addr);
1022 break;
1023 case IPV6_SADDR_RULE_SCOPE:
1024 /* Rule 2: Prefer appropriate scope
1025 *
1026 * ret
1027 * ^
1028 * -1 | d 15
1029 * ---+--+-+---> scope
1030 * |
1031 * | d is scope of the destination.
1032 * B-d | \
1033 * | \ <- smaller scope is better if
1034 * B-15 | \ if scope is enough for destinaion.
1035 * | ret = B - scope (-1 <= scope >= d <= 15).
1036 * d-C-1 | /
1037 * |/ <- greater is better
1038 * -C / if scope is not enough for destination.
1039 * /| ret = scope - C (-1 <= d < scope <= 15).
1040 *
1041 * d - C - 1 < B -15 (for all -1 <= d <= 15).
1042 * C > d + 14 - B >= 15 + 14 - B = 29 - B.
1043 * Assume B = 0 and we get C > 29.
1044 */
1045 ret = __ipv6_addr_src_scope(score->addr_type);
1046 if (ret >= dst->scope)
1047 ret = -ret;
1048 else
1049 ret -= 128; /* 30 is enough */
1050 score->scopedist = ret;
1051 break;
1052 case IPV6_SADDR_RULE_PREFERRED:
1053 /* Rule 3: Avoid deprecated and optimistic addresses */
1054 ret = ipv6_saddr_preferred(score->addr_type) ||
1055 !(score->ifa->flags & (IFA_F_DEPRECATED|IFA_F_OPTIMISTIC));
1056 break;
1057#ifdef CONFIG_IPV6_MIP6
1058 case IPV6_SADDR_RULE_HOA:
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001059 {
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001060 /* Rule 4: Prefer home address */
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001061 int prefhome = !(dst->prefs & IPV6_PREFER_SRC_COA);
1062 ret = !(score->ifa->flags & IFA_F_HOMEADDRESS) ^ prefhome;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001063 break;
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001064 }
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001065#endif
1066 case IPV6_SADDR_RULE_OIF:
1067 /* Rule 5: Prefer outgoing interface */
1068 ret = (!dst->ifindex ||
1069 dst->ifindex == score->ifa->idev->dev->ifindex);
1070 break;
1071 case IPV6_SADDR_RULE_LABEL:
1072 /* Rule 6: Prefer matching label */
Benjamin Thery3de23252008-05-28 14:51:24 +02001073 ret = ipv6_addr_label(net,
1074 &score->ifa->addr, score->addr_type,
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001075 score->ifa->idev->dev->ifindex) == dst->label;
1076 break;
1077#ifdef CONFIG_IPV6_PRIVACY
1078 case IPV6_SADDR_RULE_PRIVACY:
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001079 {
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001080 /* Rule 7: Prefer public address
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001081 * Note: prefer temporary address if use_tempaddr >= 2
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001082 */
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001083 int preftmp = dst->prefs & (IPV6_PREFER_SRC_PUBLIC|IPV6_PREFER_SRC_TMP) ?
1084 !!(dst->prefs & IPV6_PREFER_SRC_TMP) :
1085 score->ifa->idev->cnf.use_tempaddr >= 2;
1086 ret = (!(score->ifa->flags & IFA_F_TEMPORARY)) ^ preftmp;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001087 break;
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001088 }
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001089#endif
1090 case IPV6_SADDR_RULE_ORCHID:
1091 /* Rule 8-: Prefer ORCHID vs ORCHID or
1092 * non-ORCHID vs non-ORCHID
1093 */
1094 ret = !(ipv6_addr_orchid(&score->ifa->addr) ^
1095 ipv6_addr_orchid(dst->addr));
1096 break;
1097 case IPV6_SADDR_RULE_PREFIX:
1098 /* Rule 8: Use longest matching prefix */
1099 score->matchlen = ret = ipv6_addr_diff(&score->ifa->addr,
1100 dst->addr);
1101 break;
1102 default:
1103 ret = 0;
1104 }
1105
1106 if (ret)
1107 __set_bit(i, score->scorebits);
1108 score->rule = i;
1109out:
1110 return ret;
1111}
1112
Brian Haley191cd582008-08-14 15:33:21 -07001113int ipv6_dev_get_saddr(struct net *net, struct net_device *dst_dev,
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +09001114 const struct in6_addr *daddr, unsigned int prefs,
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001115 struct in6_addr *saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116{
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001117 struct ipv6_saddr_score scores[2],
1118 *score = &scores[0], *hiscore = &scores[1];
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001119 struct ipv6_saddr_dst dst;
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001120 struct net_device *dev;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001121 int dst_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001123 dst_type = __ipv6_addr_type(daddr);
1124 dst.addr = daddr;
1125 dst.ifindex = dst_dev ? dst_dev->ifindex : 0;
1126 dst.scope = __ipv6_addr_src_scope(dst_type);
Benjamin Thery3de23252008-05-28 14:51:24 +02001127 dst.label = ipv6_addr_label(net, daddr, dst_type, dst.ifindex);
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001128 dst.prefs = prefs;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001129
1130 hiscore->rule = -1;
1131 hiscore->ifa = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001133 rcu_read_lock();
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001134
Eric Dumazetc6d14c82009-11-04 05:43:23 -08001135 for_each_netdev_rcu(net, dev) {
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001136 struct inet6_dev *idev;
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001137
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001138 /* Candidate Source Address (section 4)
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001139 * - multicast and link-local destination address,
1140 * the set of candidate source address MUST only
1141 * include addresses assigned to interfaces
1142 * belonging to the same link as the outgoing
1143 * interface.
1144 * (- For site-local destination addresses, the
1145 * set of candidate source addresses MUST only
1146 * include addresses assigned to interfaces
1147 * belonging to the same site as the outgoing
1148 * interface.)
1149 */
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001150 if (((dst_type & IPV6_ADDR_MULTICAST) ||
1151 dst.scope <= IPV6_ADDR_SCOPE_LINKLOCAL) &&
1152 dst.ifindex && dev->ifindex != dst.ifindex)
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001153 continue;
1154
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 idev = __in6_dev_get(dev);
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001156 if (!idev)
1157 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001159 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00001160 list_for_each_entry(score->ifa, &idev->addr_list, if_list) {
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001161 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001163 /*
YOSHIFUJI Hideaki6b3ae802005-12-21 22:58:01 +09001164 * - Tentative Address (RFC2462 section 5.4)
1165 * - A tentative address is not considered
1166 * "assigned to an interface" in the traditional
Neil Horman95c385b2007-04-25 17:08:10 -07001167 * sense, unless it is also flagged as optimistic.
YOSHIFUJI Hideaki6b3ae802005-12-21 22:58:01 +09001168 * - Candidate Source Address (section 4)
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001169 * - In any case, anycast addresses, multicast
1170 * addresses, and the unspecified address MUST
1171 * NOT be included in a candidate set.
1172 */
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001173 if ((score->ifa->flags & IFA_F_TENTATIVE) &&
1174 (!(score->ifa->flags & IFA_F_OPTIMISTIC)))
YOSHIFUJI Hideaki6b3ae802005-12-21 22:58:01 +09001175 continue;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001176
1177 score->addr_type = __ipv6_addr_type(&score->ifa->addr);
1178
1179 if (unlikely(score->addr_type == IPV6_ADDR_ANY ||
1180 score->addr_type & IPV6_ADDR_MULTICAST)) {
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001181 LIMIT_NETDEBUG(KERN_DEBUG
Joe Perches3b6d8212007-11-19 23:47:25 -08001182 "ADDRCONF: unspecified / multicast address "
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001183 "assigned as unicast address on %s",
1184 dev->name);
1185 continue;
1186 }
1187
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001188 score->rule = -1;
1189 bitmap_zero(score->scorebits, IPV6_SADDR_RULE_MAX);
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001190
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001191 for (i = 0; i < IPV6_SADDR_RULE_MAX; i++) {
1192 int minihiscore, miniscore;
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001193
Benjamin Thery3de23252008-05-28 14:51:24 +02001194 minihiscore = ipv6_get_saddr_eval(net, hiscore, &dst, i);
1195 miniscore = ipv6_get_saddr_eval(net, score, &dst, i);
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001196
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001197 if (minihiscore > miniscore) {
1198 if (i == IPV6_SADDR_RULE_SCOPE &&
1199 score->scopedist > 0) {
1200 /*
1201 * special case:
1202 * each remaining entry
1203 * has too small (not enough)
1204 * scope, because ifa entries
1205 * are sorted by their scope
1206 * values.
1207 */
1208 goto try_nextdev;
1209 }
1210 break;
1211 } else if (minihiscore < miniscore) {
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001212 if (hiscore->ifa)
1213 in6_ifa_put(hiscore->ifa);
1214
1215 in6_ifa_hold(score->ifa);
1216
Ilpo Järvinena0bffff2009-03-21 13:36:17 -07001217 swap(hiscore, score);
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001218
1219 /* restore our iterator */
1220 score->ifa = hiscore->ifa;
1221
1222 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 }
1224 }
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001225 }
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001226try_nextdev:
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001227 read_unlock_bh(&idev->lock);
1228 }
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001229 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001231 if (!hiscore->ifa)
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001232 return -EADDRNOTAVAIL;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001233
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001234 *saddr = hiscore->ifa->addr;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001235 in6_ifa_put(hiscore->ifa);
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001236 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237}
YOSHIFUJI Hideaki5e5f3f02008-03-03 21:44:34 +09001238EXPORT_SYMBOL(ipv6_dev_get_saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
Amerigo Wang3ef208a2013-06-29 21:30:49 +08001240int __ipv6_get_lladdr(struct inet6_dev *idev, struct in6_addr *addr,
1241 unsigned char banned_flags)
1242{
1243 struct inet6_ifaddr *ifp;
1244 int err = -EADDRNOTAVAIL;
1245
1246 list_for_each_entry(ifp, &idev->addr_list, if_list) {
1247 if (ifp->scope == IFA_LINK &&
1248 !(ifp->flags & banned_flags)) {
1249 *addr = ifp->addr;
1250 err = 0;
1251 break;
1252 }
1253 }
1254 return err;
1255}
1256
Neil Horman95c385b2007-04-25 17:08:10 -07001257int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr,
1258 unsigned char banned_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259{
1260 struct inet6_dev *idev;
1261 int err = -EADDRNOTAVAIL;
1262
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001263 rcu_read_lock();
Stephen Hemmingere21e8462010-03-20 16:09:01 -07001264 idev = __in6_dev_get(dev);
1265 if (idev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 read_lock_bh(&idev->lock);
Amerigo Wang3ef208a2013-06-29 21:30:49 +08001267 err = __ipv6_get_lladdr(idev, addr, banned_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 read_unlock_bh(&idev->lock);
1269 }
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001270 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 return err;
1272}
1273
1274static int ipv6_count_addresses(struct inet6_dev *idev)
1275{
1276 int cnt = 0;
1277 struct inet6_ifaddr *ifp;
1278
1279 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00001280 list_for_each_entry(ifp, &idev->addr_list, if_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 cnt++;
1282 read_unlock_bh(&idev->lock);
1283 return cnt;
1284}
1285
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001286int ipv6_chk_addr(struct net *net, const struct in6_addr *addr,
Daniel Lezcanobfeade02008-01-10 22:43:18 -08001287 struct net_device *dev, int strict)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288{
Stephen Hemmingereedf0422010-05-17 22:27:12 -07001289 struct inet6_ifaddr *ifp;
stephen hemmingerc2e21292010-03-17 20:31:10 +00001290 struct hlist_node *node;
stephen hemminger3a88a812010-03-17 20:31:12 +00001291 unsigned int hash = ipv6_addr_hash(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
stephen hemminger5c578ae2010-03-17 20:31:11 +00001293 rcu_read_lock_bh();
1294 hlist_for_each_entry_rcu(ifp, node, &inet6_addr_lst[hash], addr_lst) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09001295 if (!net_eq(dev_net(ifp->idev->dev), net))
Daniel Lezcanobfeade02008-01-10 22:43:18 -08001296 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 if (ipv6_addr_equal(&ifp->addr, addr) &&
Stephen Hemmingereedf0422010-05-17 22:27:12 -07001298 !(ifp->flags&IFA_F_TENTATIVE) &&
1299 (dev == NULL || ifp->idev->dev == dev ||
1300 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))) {
1301 rcu_read_unlock_bh();
1302 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 }
1304 }
stephen hemminger5c578ae2010-03-17 20:31:11 +00001305
Stephen Hemmingereedf0422010-05-17 22:27:12 -07001306 rcu_read_unlock_bh();
1307 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308}
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +09001309EXPORT_SYMBOL(ipv6_chk_addr);
1310
David S. Miller3e81c6d2010-03-20 16:18:00 -07001311static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
1312 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313{
David S. Miller3e81c6d2010-03-20 16:18:00 -07001314 unsigned int hash = ipv6_addr_hash(addr);
stephen hemmingerc2e21292010-03-17 20:31:10 +00001315 struct inet6_ifaddr *ifp;
1316 struct hlist_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
stephen hemmingerc2e21292010-03-17 20:31:10 +00001318 hlist_for_each_entry(ifp, node, &inet6_addr_lst[hash], addr_lst) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09001319 if (!net_eq(dev_net(ifp->idev->dev), net))
Daniel Lezcano06bfe652008-01-10 22:43:42 -08001320 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 if (ipv6_addr_equal(&ifp->addr, addr)) {
1322 if (dev == NULL || ifp->idev->dev == dev)
David S. Miller3e81c6d2010-03-20 16:18:00 -07001323 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 }
1325 }
David S. Miller3e81c6d2010-03-20 16:18:00 -07001326 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327}
1328
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001329int ipv6_chk_prefix(const struct in6_addr *addr, struct net_device *dev)
YOSHIFUJI Hideaki52eeeb82008-03-15 22:54:23 -04001330{
1331 struct inet6_dev *idev;
1332 struct inet6_ifaddr *ifa;
1333 int onlink;
1334
1335 onlink = 0;
1336 rcu_read_lock();
1337 idev = __in6_dev_get(dev);
1338 if (idev) {
1339 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00001340 list_for_each_entry(ifa, &idev->addr_list, if_list) {
YOSHIFUJI Hideaki52eeeb82008-03-15 22:54:23 -04001341 onlink = ipv6_prefix_equal(addr, &ifa->addr,
1342 ifa->prefix_len);
1343 if (onlink)
1344 break;
1345 }
1346 read_unlock_bh(&idev->lock);
1347 }
1348 rcu_read_unlock();
1349 return onlink;
1350}
1351
1352EXPORT_SYMBOL(ipv6_chk_prefix);
1353
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +09001354struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, const struct in6_addr *addr,
Daniel Lezcano1cab3da2008-01-10 22:44:09 -08001355 struct net_device *dev, int strict)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356{
David S. Millerb79d1d52010-03-25 21:39:21 -07001357 struct inet6_ifaddr *ifp, *result = NULL;
stephen hemminger3a88a812010-03-17 20:31:12 +00001358 unsigned int hash = ipv6_addr_hash(addr);
David S. Millerb79d1d52010-03-25 21:39:21 -07001359 struct hlist_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
stephen hemminger5c578ae2010-03-17 20:31:11 +00001361 rcu_read_lock_bh();
Eric Dumazet4f70ecc2010-05-03 10:50:14 +00001362 hlist_for_each_entry_rcu_bh(ifp, node, &inet6_addr_lst[hash], addr_lst) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09001363 if (!net_eq(dev_net(ifp->idev->dev), net))
Daniel Lezcano1cab3da2008-01-10 22:44:09 -08001364 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 if (ipv6_addr_equal(&ifp->addr, addr)) {
1366 if (dev == NULL || ifp->idev->dev == dev ||
1367 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
David S. Millerb79d1d52010-03-25 21:39:21 -07001368 result = ifp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 in6_ifa_hold(ifp);
1370 break;
1371 }
1372 }
1373 }
stephen hemminger5c578ae2010-03-17 20:31:11 +00001374 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
David S. Millerb79d1d52010-03-25 21:39:21 -07001376 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377}
1378
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379/* Gets referenced address, destroys ifaddr */
1380
Brian Haleycc411d02009-09-09 14:41:32 +00001381static void addrconf_dad_stop(struct inet6_ifaddr *ifp, int dad_failed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 if (ifp->flags&IFA_F_PERMANENT) {
1384 spin_lock_bh(&ifp->lock);
1385 addrconf_del_timer(ifp);
1386 ifp->flags |= IFA_F_TENTATIVE;
Brian Haleycc411d02009-09-09 14:41:32 +00001387 if (dad_failed)
1388 ifp->flags |= IFA_F_DADFAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 spin_unlock_bh(&ifp->lock);
Herbert Xue2577a02010-03-13 12:23:29 -08001390 if (dad_failed)
1391 ipv6_ifa_notify(0, ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 in6_ifa_put(ifp);
1393#ifdef CONFIG_IPV6_PRIVACY
1394 } else if (ifp->flags&IFA_F_TEMPORARY) {
1395 struct inet6_ifaddr *ifpub;
1396 spin_lock_bh(&ifp->lock);
1397 ifpub = ifp->ifpub;
1398 if (ifpub) {
1399 in6_ifa_hold(ifpub);
1400 spin_unlock_bh(&ifp->lock);
1401 ipv6_create_tempaddr(ifpub, ifp);
1402 in6_ifa_put(ifpub);
1403 } else {
1404 spin_unlock_bh(&ifp->lock);
1405 }
1406 ipv6_del_addr(ifp);
1407#endif
1408 } else
1409 ipv6_del_addr(ifp);
1410}
1411
Herbert Xuf2344a12010-05-18 15:55:27 -07001412static int addrconf_dad_end(struct inet6_ifaddr *ifp)
1413{
1414 int err = -ENOENT;
1415
1416 spin_lock(&ifp->state_lock);
1417 if (ifp->state == INET6_IFADDR_STATE_DAD) {
1418 ifp->state = INET6_IFADDR_STATE_POSTDAD;
1419 err = 0;
1420 }
1421 spin_unlock(&ifp->state_lock);
1422
1423 return err;
1424}
1425
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09001426void addrconf_dad_failure(struct inet6_ifaddr *ifp)
1427{
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09001428 struct inet6_dev *idev = ifp->idev;
Brian Haley9bdd8d42009-03-18 18:22:48 -07001429
Ursula Braun853dc2e2010-10-24 23:06:43 +00001430 if (addrconf_dad_end(ifp)) {
1431 in6_ifa_put(ifp);
Herbert Xuf2344a12010-05-18 15:55:27 -07001432 return;
Ursula Braun853dc2e2010-10-24 23:06:43 +00001433 }
Herbert Xuf2344a12010-05-18 15:55:27 -07001434
Brian Haley9bdd8d42009-03-18 18:22:48 -07001435 if (net_ratelimit())
Jens Rosenboom0522fea2009-09-17 10:24:24 -07001436 printk(KERN_INFO "%s: IPv6 duplicate address %pI6c detected!\n",
1437 ifp->idev->dev->name, &ifp->addr);
Brian Haley9bdd8d42009-03-18 18:22:48 -07001438
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09001439 if (idev->cnf.accept_dad > 1 && !idev->cnf.disable_ipv6) {
1440 struct in6_addr addr;
1441
1442 addr.s6_addr32[0] = htonl(0xfe800000);
1443 addr.s6_addr32[1] = 0;
1444
1445 if (!ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) &&
1446 ipv6_addr_equal(&ifp->addr, &addr)) {
1447 /* DAD failed for link-local based on MAC address */
1448 idev->cnf.disable_ipv6 = 1;
Brian Haley9bdd8d42009-03-18 18:22:48 -07001449
1450 printk(KERN_INFO "%s: IPv6 being disabled!\n",
1451 ifp->idev->dev->name);
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09001452 }
1453 }
1454
Brian Haleycc411d02009-09-09 14:41:32 +00001455 addrconf_dad_stop(ifp, 1);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09001456}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457
1458/* Join to solicited addr multicast group. */
1459
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001460void addrconf_join_solict(struct net_device *dev, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461{
1462 struct in6_addr maddr;
1463
1464 if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1465 return;
1466
1467 addrconf_addr_solict_mult(addr, &maddr);
1468 ipv6_dev_mc_inc(dev, &maddr);
1469}
1470
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001471void addrconf_leave_solict(struct inet6_dev *idev, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472{
1473 struct in6_addr maddr;
1474
1475 if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1476 return;
1477
1478 addrconf_addr_solict_mult(addr, &maddr);
1479 __ipv6_dev_mc_dec(idev, &maddr);
1480}
1481
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03001482static void addrconf_join_anycast(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483{
1484 struct in6_addr addr;
Bjørn Mork2bda8a02011-07-05 23:04:13 +00001485 if (ifp->prefix_len == 127) /* RFC 6164 */
1486 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1488 if (ipv6_addr_any(&addr))
1489 return;
1490 ipv6_dev_ac_inc(ifp->idev->dev, &addr);
1491}
1492
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03001493static void addrconf_leave_anycast(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494{
1495 struct in6_addr addr;
YOSHIFUJI Hideaki32019e62011-07-24 11:44:34 +00001496 if (ifp->prefix_len == 127) /* RFC 6164 */
1497 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1499 if (ipv6_addr_any(&addr))
1500 return;
1501 __ipv6_dev_ac_dec(ifp->idev, &addr);
1502}
1503
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08001504static int addrconf_ifid_eui48(u8 *eui, struct net_device *dev)
1505{
1506 if (dev->addr_len != ETH_ALEN)
1507 return -1;
1508 memcpy(eui, dev->dev_addr, 3);
1509 memcpy(eui + 5, dev->dev_addr + 3, 3);
1510
1511 /*
1512 * The zSeries OSA network cards can be shared among various
1513 * OS instances, but the OSA cards have only one MAC address.
1514 * This leads to duplicate address conflicts in conjunction
1515 * with IPv6 if more than one instance uses the same card.
1516 *
1517 * The driver for these cards can deliver a unique 16-bit
1518 * identifier for each instance sharing the same card. It is
1519 * placed instead of 0xFFFE in the interface identifier. The
1520 * "u" bit of the interface identifier is not inverted in this
1521 * case. Hence the resulting interface identifier has local
1522 * scope according to RFC2373.
1523 */
1524 if (dev->dev_id) {
1525 eui[3] = (dev->dev_id >> 8) & 0xFF;
1526 eui[4] = dev->dev_id & 0xFF;
1527 } else {
1528 eui[3] = 0xFF;
1529 eui[4] = 0xFE;
1530 eui[0] ^= 2;
1531 }
1532 return 0;
1533}
1534
1535static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev)
1536{
1537 /* XXX: inherit EUI-64 from other interface -- yoshfuji */
1538 if (dev->addr_len != ARCNET_ALEN)
1539 return -1;
1540 memset(eui, 0, 7);
1541 eui[7] = *(u8*)dev->dev_addr;
1542 return 0;
1543}
1544
1545static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev)
1546{
1547 if (dev->addr_len != INFINIBAND_ALEN)
1548 return -1;
1549 memcpy(eui, dev->dev_addr + 12, 8);
1550 eui[0] |= 2;
1551 return 0;
1552}
1553
stephen hemmingerc61393e2010-10-04 20:17:53 +00001554static int __ipv6_isatap_ifid(u8 *eui, __be32 addr)
YOSHIFUJI Hideakidfd982b2008-04-10 15:42:09 +09001555{
Sascha Hlusiak9af28512009-05-19 12:56:51 +00001556 if (addr == 0)
1557 return -1;
YOSHIFUJI Hideakidfd982b2008-04-10 15:42:09 +09001558 eui[0] = (ipv4_is_zeronet(addr) || ipv4_is_private_10(addr) ||
1559 ipv4_is_loopback(addr) || ipv4_is_linklocal_169(addr) ||
1560 ipv4_is_private_172(addr) || ipv4_is_test_192(addr) ||
1561 ipv4_is_anycast_6to4(addr) || ipv4_is_private_192(addr) ||
1562 ipv4_is_test_198(addr) || ipv4_is_multicast(addr) ||
1563 ipv4_is_lbcast(addr)) ? 0x00 : 0x02;
1564 eui[1] = 0;
1565 eui[2] = 0x5E;
1566 eui[3] = 0xFE;
1567 memcpy(eui + 4, &addr, 4);
1568 return 0;
1569}
YOSHIFUJI Hideakidfd982b2008-04-10 15:42:09 +09001570
1571static int addrconf_ifid_sit(u8 *eui, struct net_device *dev)
1572{
1573 if (dev->priv_flags & IFF_ISATAP)
1574 return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
1575 return -1;
1576}
1577
stephen hemmingeraee80b52011-06-08 10:44:30 +00001578static int addrconf_ifid_gre(u8 *eui, struct net_device *dev)
1579{
1580 return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
1581}
1582
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
1584{
1585 switch (dev->type) {
1586 case ARPHRD_ETHER:
1587 case ARPHRD_FDDI:
1588 case ARPHRD_IEEE802_TR:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08001589 return addrconf_ifid_eui48(eui, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 case ARPHRD_ARCNET:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08001591 return addrconf_ifid_arcnet(eui, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 case ARPHRD_INFINIBAND:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08001593 return addrconf_ifid_infiniband(eui, dev);
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11001594 case ARPHRD_SIT:
YOSHIFUJI Hideakidfd982b2008-04-10 15:42:09 +09001595 return addrconf_ifid_sit(eui, dev);
stephen hemmingeraee80b52011-06-08 10:44:30 +00001596 case ARPHRD_IPGRE:
1597 return addrconf_ifid_gre(eui, dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001598 case ARPHRD_RAWIP: {
1599 struct in6_addr lladdr;
1600
1601 if (ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
1602 get_random_bytes(eui, 8);
1603 else
1604 memcpy(eui, lladdr.s6_addr + 8, 8);
1605
1606 return 0;
1607 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 }
1609 return -1;
1610}
1611
1612static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
1613{
1614 int err = -1;
1615 struct inet6_ifaddr *ifp;
1616
1617 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00001618 list_for_each_entry(ifp, &idev->addr_list, if_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
1620 memcpy(eui, ifp->addr.s6_addr+8, 8);
1621 err = 0;
1622 break;
1623 }
1624 }
1625 read_unlock_bh(&idev->lock);
1626 return err;
1627}
1628
1629#ifdef CONFIG_IPV6_PRIVACY
1630/* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
1631static int __ipv6_regen_rndid(struct inet6_dev *idev)
1632{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633regen:
YOSHIFUJI Hideaki955189e2006-03-20 16:54:09 -08001634 get_random_bytes(idev->rndid, sizeof(idev->rndid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 idev->rndid[0] &= ~0x02;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636
1637 /*
1638 * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
1639 * check if generated address is not inappropriate
1640 *
1641 * - Reserved subnet anycast (RFC 2526)
1642 * 11111101 11....11 1xxxxxxx
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11001643 * - ISATAP (RFC4214) 6.1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 * 00-00-5E-FE-xx-xx-xx-xx
1645 * - value 0
1646 * - XXX: already assigned to an address on the device
1647 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001648 if (idev->rndid[0] == 0xfd &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff &&
1650 (idev->rndid[7]&0x80))
1651 goto regen;
1652 if ((idev->rndid[0]|idev->rndid[1]) == 0) {
1653 if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe)
1654 goto regen;
1655 if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00)
1656 goto regen;
1657 }
1658
1659 return 0;
1660}
1661
1662static void ipv6_regen_rndid(unsigned long data)
1663{
1664 struct inet6_dev *idev = (struct inet6_dev *) data;
1665 unsigned long expires;
1666
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001667 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 write_lock_bh(&idev->lock);
1669
1670 if (idev->dead)
1671 goto out;
1672
1673 if (__ipv6_regen_rndid(idev) < 0)
1674 goto out;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001675
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 expires = jiffies +
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001677 idev->cnf.temp_prefered_lft * HZ -
Ben Hutchings784e2712010-06-26 11:42:55 +00001678 idev->cnf.regen_max_retry * idev->cnf.dad_transmits * idev->nd_parms->retrans_time -
1679 idev->cnf.max_desync_factor * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 if (time_before(expires, jiffies)) {
1681 printk(KERN_WARNING
1682 "ipv6_regen_rndid(): too short regeneration interval; timer disabled for %s.\n",
1683 idev->dev->name);
1684 goto out;
1685 }
1686
1687 if (!mod_timer(&idev->regen_timer, expires))
1688 in6_dev_hold(idev);
1689
1690out:
1691 write_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001692 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 in6_dev_put(idev);
1694}
1695
1696static int __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr) {
1697 int ret = 0;
1698
1699 if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0)
1700 ret = __ipv6_regen_rndid(idev);
1701 return ret;
1702}
1703#endif
1704
Lorenzo Colitti653be182014-03-26 19:35:41 +09001705u32 addrconf_rt_table(const struct net_device *dev, u32 default_table) {
1706 /* Determines into what table to put autoconf PIO/RIO/default routes
1707 * learned on this device.
1708 *
1709 * - If 0, use the same table for every device. This puts routes into
1710 * one of RT_TABLE_{PREFIX,INFO,DFLT} depending on the type of route
1711 * (but note that these three are currently all equal to
1712 * RT6_TABLE_MAIN).
1713 * - If > 0, use the specified table.
1714 * - If < 0, put routes into table dev->ifindex + (-rt_table).
1715 */
1716 struct inet6_dev *idev = in6_dev_get(dev);
1717 u32 table;
1718 int sysctl = idev->cnf.accept_ra_rt_table;
1719 if (sysctl == 0) {
1720 table = default_table;
1721 } else if (sysctl > 0) {
1722 table = (u32) sysctl;
1723 } else {
1724 table = (unsigned) dev->ifindex + (-sysctl);
1725 }
1726 in6_dev_put(idev);
1727 return table;
1728}
1729
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730/*
1731 * Add prefix route.
1732 */
1733
1734static void
1735addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev,
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -07001736 unsigned long expires, u32 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737{
Thomas Graf86872cb2006-08-22 00:01:08 -07001738 struct fib6_config cfg = {
Lorenzo Colitti653be182014-03-26 19:35:41 +09001739 .fc_table = addrconf_rt_table(dev, RT6_TABLE_PREFIX),
Thomas Graf86872cb2006-08-22 00:01:08 -07001740 .fc_metric = IP6_RT_PRIO_ADDRCONF,
1741 .fc_ifindex = dev->ifindex,
1742 .fc_expires = expires,
1743 .fc_dst_len = plen,
1744 .fc_flags = RTF_UP | flags,
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001745 .fc_nlinfo.nl_net = dev_net(dev),
Stephen Hemmingerf410a1f2008-08-23 05:16:46 -07001746 .fc_protocol = RTPROT_KERNEL,
Thomas Graf86872cb2006-08-22 00:01:08 -07001747 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001749 cfg.fc_dst = *pfx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
1751 /* Prevent useless cloning on PtP SIT.
1752 This thing is done here expecting that the whole
1753 class of non-broadcast devices need not cloning.
1754 */
Joerg Roedel0be669b2006-10-10 14:49:53 -07001755#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
Thomas Graf86872cb2006-08-22 00:01:08 -07001756 if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT))
1757 cfg.fc_flags |= RTF_NONEXTHOP;
Joerg Roedel0be669b2006-10-10 14:49:53 -07001758#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759
Thomas Graf86872cb2006-08-22 00:01:08 -07001760 ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761}
1762
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00001763
1764static struct rt6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
1765 int plen,
1766 const struct net_device *dev,
1767 u32 flags, u32 noflags)
1768{
1769 struct fib6_node *fn;
1770 struct rt6_info *rt = NULL;
1771 struct fib6_table *table;
1772
Lorenzo Colitti653be182014-03-26 19:35:41 +09001773 table = fib6_get_table(dev_net(dev),
1774 addrconf_rt_table(dev, RT6_TABLE_PREFIX));
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00001775 if (table == NULL)
1776 return NULL;
1777
1778 write_lock_bh(&table->tb6_lock);
1779 fn = fib6_locate(&table->tb6_root, pfx, plen, NULL, 0);
1780 if (!fn)
1781 goto out;
1782 for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
David S. Millerd1918542011-12-28 20:19:20 -05001783 if (rt->dst.dev->ifindex != dev->ifindex)
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00001784 continue;
1785 if ((rt->rt6i_flags & flags) != flags)
1786 continue;
Romain Kuntz3b690552013-01-09 15:02:26 +01001787 if ((rt->rt6i_flags & noflags) != 0)
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00001788 continue;
1789 dst_hold(&rt->dst);
1790 break;
1791 }
1792out:
1793 write_unlock_bh(&table->tb6_lock);
1794 return rt;
1795}
1796
1797
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798/* Create "default" multicast route to the interface */
1799
1800static void addrconf_add_mroute(struct net_device *dev)
1801{
Thomas Graf86872cb2006-08-22 00:01:08 -07001802 struct fib6_config cfg = {
1803 .fc_table = RT6_TABLE_LOCAL,
1804 .fc_metric = IP6_RT_PRIO_ADDRCONF,
1805 .fc_ifindex = dev->ifindex,
1806 .fc_dst_len = 8,
1807 .fc_flags = RTF_UP,
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001808 .fc_nlinfo.nl_net = dev_net(dev),
Thomas Graf86872cb2006-08-22 00:01:08 -07001809 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810
Thomas Graf86872cb2006-08-22 00:01:08 -07001811 ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0);
1812
1813 ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814}
1815
Joerg Roedel0be669b2006-10-10 14:49:53 -07001816#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817static void sit_route_add(struct net_device *dev)
1818{
Thomas Graf86872cb2006-08-22 00:01:08 -07001819 struct fib6_config cfg = {
1820 .fc_table = RT6_TABLE_MAIN,
1821 .fc_metric = IP6_RT_PRIO_ADDRCONF,
1822 .fc_ifindex = dev->ifindex,
1823 .fc_dst_len = 96,
1824 .fc_flags = RTF_UP | RTF_NONEXTHOP,
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001825 .fc_nlinfo.nl_net = dev_net(dev),
Thomas Graf86872cb2006-08-22 00:01:08 -07001826 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827
1828 /* prefix length - 96 bits "::d.d.d.d" */
Thomas Graf86872cb2006-08-22 00:01:08 -07001829 ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830}
Joerg Roedel0be669b2006-10-10 14:49:53 -07001831#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832
1833static void addrconf_add_lroute(struct net_device *dev)
1834{
1835 struct in6_addr addr;
1836
1837 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
1838 addrconf_prefix_route(&addr, 64, dev, 0, 0);
1839}
1840
1841static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
1842{
1843 struct inet6_dev *idev;
1844
1845 ASSERT_RTNL();
1846
Stephen Hemmingere21e8462010-03-20 16:09:01 -07001847 idev = ipv6_find_idev(dev);
1848 if (!idev)
Brian Haley64e724f2010-07-20 10:34:30 +00001849 return ERR_PTR(-ENOBUFS);
1850
1851 if (idev->cnf.disable_ipv6)
1852 return ERR_PTR(-EACCES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853
1854 /* Add default multicast route */
Li Wei4af04ab2011-12-06 21:23:45 +00001855 if (!(dev->flags & IFF_LOOPBACK))
1856 addrconf_add_mroute(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857
1858 /* Add link local route */
1859 addrconf_add_lroute(dev);
1860 return idev;
1861}
1862
Neil Hormane6bff992012-01-04 10:49:15 +00001863void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len, bool sllao)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864{
1865 struct prefix_info *pinfo;
1866 __u32 valid_lft;
1867 __u32 prefered_lft;
1868 int addr_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 struct inet6_dev *in6_dev;
Brian Haley56d417b2009-06-01 03:07:33 -07001870 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871
1872 pinfo = (struct prefix_info *) opt;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001873
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 if (len < sizeof(struct prefix_info)) {
1875 ADBG(("addrconf: prefix option too short\n"));
1876 return;
1877 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001878
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 /*
1880 * Validation checks ([ADDRCONF], page 19)
1881 */
1882
1883 addr_type = ipv6_addr_type(&pinfo->prefix);
1884
1885 if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
1886 return;
1887
1888 valid_lft = ntohl(pinfo->valid);
1889 prefered_lft = ntohl(pinfo->prefered);
1890
1891 if (prefered_lft > valid_lft) {
1892 if (net_ratelimit())
1893 printk(KERN_WARNING "addrconf: prefix option has invalid lifetime\n");
1894 return;
1895 }
1896
1897 in6_dev = in6_dev_get(dev);
1898
1899 if (in6_dev == NULL) {
1900 if (net_ratelimit())
1901 printk(KERN_DEBUG "addrconf: device %s not configured\n", dev->name);
1902 return;
1903 }
1904
1905 /*
1906 * Two things going on here:
1907 * 1) Add routes for on-link prefixes
1908 * 2) Configure prefixes with the auto flag set
1909 */
1910
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09001911 if (pinfo->onlink) {
1912 struct rt6_info *rt;
1913 unsigned long rt_expires;
1914
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07001915 /* Avoid arithmetic overflow. Really, we could
1916 * save rt_expires in seconds, likely valid_lft,
1917 * but it would require division in fib gc, that it
1918 * not good.
1919 */
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09001920 if (HZ > USER_HZ)
1921 rt_expires = addrconf_timeout_fixup(valid_lft, HZ);
1922 else
1923 rt_expires = addrconf_timeout_fixup(valid_lft, USER_HZ);
YOSHIFUJI Hideaki3dd4bc62005-12-19 14:02:45 -08001924
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09001925 if (addrconf_finite_timeout(rt_expires))
1926 rt_expires *= HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00001928 rt = addrconf_get_prefix_route(&pinfo->prefix,
1929 pinfo->prefix_len,
1930 dev,
1931 RTF_ADDRCONF | RTF_PREFIX_RT,
1932 RTF_GATEWAY | RTF_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00001934 if (rt) {
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07001935 /* Autoconf prefix route */
1936 if (valid_lft == 0) {
1937 ip6_del_rt(rt);
1938 rt = NULL;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09001939 } else if (addrconf_finite_timeout(rt_expires)) {
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07001940 /* not infinity */
Gao feng1716a962012-04-06 00:13:10 +00001941 rt6_set_expires(rt, jiffies + rt_expires);
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07001942 } else {
Gao feng1716a962012-04-06 00:13:10 +00001943 rt6_clean_expires(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 }
1945 } else if (valid_lft) {
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07001946 clock_t expires = 0;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09001947 int flags = RTF_ADDRCONF | RTF_PREFIX_RT;
1948 if (addrconf_finite_timeout(rt_expires)) {
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07001949 /* not infinity */
1950 flags |= RTF_EXPIRES;
1951 expires = jiffies_to_clock_t(rt_expires);
1952 }
Harout Hedeshian8b2414d2012-12-21 12:38:38 -08001953 if (dev->ip6_ptr->cnf.accept_ra_prefix_route) {
1954 addrconf_prefix_route(&pinfo->prefix,
1955 pinfo->prefix_len, dev, expires, flags);
1956 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 }
1958 if (rt)
Changli Gaod8d1f302010-06-10 23:31:35 -07001959 dst_release(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 }
1961
1962 /* Try to figure out our local address for this prefix */
1963
1964 if (pinfo->autoconf && in6_dev->cnf.autoconf) {
1965 struct inet6_ifaddr * ifp;
1966 struct in6_addr addr;
1967 int create = 0, update_lft = 0;
1968
1969 if (pinfo->prefix_len == 64) {
1970 memcpy(&addr, &pinfo->prefix, 8);
1971 if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
1972 ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
1973 in6_dev_put(in6_dev);
1974 return;
1975 }
1976 goto ok;
1977 }
1978 if (net_ratelimit())
1979 printk(KERN_DEBUG "IPv6 addrconf: prefix with wrong length %d\n",
1980 pinfo->prefix_len);
1981 in6_dev_put(in6_dev);
1982 return;
1983
1984ok:
1985
YOSHIFUJI Hideaki53b79972008-07-19 22:35:03 -07001986 ifp = ipv6_get_ifaddr(net, &addr, dev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987
1988 if (ifp == NULL && valid_lft) {
1989 int max_addresses = in6_dev->cnf.max_addresses;
Neil Horman95c385b2007-04-25 17:08:10 -07001990 u32 addr_flags = 0;
1991
1992#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1993 if (in6_dev->cnf.optimistic_dad &&
Neil Hormane6bff992012-01-04 10:49:15 +00001994 !net->ipv6.devconf_all->forwarding && sllao)
Neil Horman95c385b2007-04-25 17:08:10 -07001995 addr_flags = IFA_F_OPTIMISTIC;
1996#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997
1998 /* Do not allow to create too much of autoconfigured
1999 * addresses; this would be too easy way to crash kernel.
2000 */
2001 if (!max_addresses ||
2002 ipv6_count_addresses(in6_dev) < max_addresses)
2003 ifp = ipv6_add_addr(in6_dev, &addr, pinfo->prefix_len,
Neil Horman95c385b2007-04-25 17:08:10 -07002004 addr_type&IPV6_ADDR_SCOPE_MASK,
2005 addr_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006
2007 if (!ifp || IS_ERR(ifp)) {
2008 in6_dev_put(in6_dev);
2009 return;
2010 }
2011
2012 update_lft = create = 1;
2013 ifp->cstamp = jiffies;
2014 addrconf_dad_start(ifp, RTF_ADDRCONF|RTF_PREFIX_RT);
2015 }
2016
2017 if (ifp) {
2018 int flags;
2019 unsigned long now;
2020#ifdef CONFIG_IPV6_PRIVACY
2021 struct inet6_ifaddr *ift;
2022#endif
2023 u32 stored_lft;
2024
2025 /* update lifetime (RFC2462 5.5.3 e) */
2026 spin_lock(&ifp->lock);
2027 now = jiffies;
2028 if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
2029 stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
2030 else
2031 stored_lft = 0;
2032 if (!update_lft && stored_lft) {
2033 if (valid_lft > MIN_VALID_LIFETIME ||
2034 valid_lft > stored_lft)
2035 update_lft = 1;
2036 else if (stored_lft <= MIN_VALID_LIFETIME) {
2037 /* valid_lft <= stored_lft is always true */
Brian Haleya1ed0522009-07-02 07:10:52 +00002038 /*
2039 * RFC 4862 Section 5.5.3e:
2040 * "Note that the preferred lifetime of
2041 * the corresponding address is always
2042 * reset to the Preferred Lifetime in
2043 * the received Prefix Information
2044 * option, regardless of whether the
2045 * valid lifetime is also reset or
2046 * ignored."
2047 *
2048 * So if the preferred lifetime in
2049 * this advertisement is different
2050 * than what we have stored, but the
2051 * valid lifetime is invalid, just
2052 * reset prefered_lft.
2053 *
2054 * We must set the valid lifetime
2055 * to the stored lifetime since we'll
2056 * be updating the timestamp below,
2057 * else we'll set it back to the
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002058 * minimum.
Brian Haleya1ed0522009-07-02 07:10:52 +00002059 */
2060 if (prefered_lft != ifp->prefered_lft) {
2061 valid_lft = stored_lft;
2062 update_lft = 1;
2063 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 } else {
2065 valid_lft = MIN_VALID_LIFETIME;
2066 if (valid_lft < prefered_lft)
2067 prefered_lft = valid_lft;
2068 update_lft = 1;
2069 }
2070 }
2071
2072 if (update_lft) {
2073 ifp->valid_lft = valid_lft;
2074 ifp->prefered_lft = prefered_lft;
2075 ifp->tstamp = now;
2076 flags = ifp->flags;
2077 ifp->flags &= ~IFA_F_DEPRECATED;
2078 spin_unlock(&ifp->lock);
2079
2080 if (!(flags&IFA_F_TENTATIVE))
2081 ipv6_ifa_notify(0, ifp);
2082 } else
2083 spin_unlock(&ifp->lock);
2084
2085#ifdef CONFIG_IPV6_PRIVACY
2086 read_lock_bh(&in6_dev->lock);
2087 /* update all temporary addresses in the list */
Lorenzo Colitti76f793e2011-07-26 13:50:49 +00002088 list_for_each_entry(ift, &in6_dev->tempaddr_list,
2089 tmp_list) {
2090 int age, max_valid, max_prefered;
2091
Benoit Boissinotc6fbfac2008-04-02 00:00:58 -07002092 if (ifp != ift->ifpub)
2093 continue;
2094
Lorenzo Colitti76f793e2011-07-26 13:50:49 +00002095 /*
2096 * RFC 4941 section 3.3:
2097 * If a received option will extend the lifetime
2098 * of a public address, the lifetimes of
2099 * temporary addresses should be extended,
2100 * subject to the overall constraint that no
2101 * temporary addresses should ever remain
2102 * "valid" or "preferred" for a time longer than
2103 * (TEMP_VALID_LIFETIME) or
2104 * (TEMP_PREFERRED_LIFETIME - DESYNC_FACTOR),
2105 * respectively.
2106 */
2107 age = (now - ift->cstamp) / HZ;
2108 max_valid = in6_dev->cnf.temp_valid_lft - age;
2109 if (max_valid < 0)
2110 max_valid = 0;
2111
2112 max_prefered = in6_dev->cnf.temp_prefered_lft -
2113 in6_dev->cnf.max_desync_factor -
2114 age;
2115 if (max_prefered < 0)
2116 max_prefered = 0;
2117
2118 if (valid_lft > max_valid)
2119 valid_lft = max_valid;
2120
2121 if (prefered_lft > max_prefered)
2122 prefered_lft = max_prefered;
2123
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 spin_lock(&ift->lock);
2125 flags = ift->flags;
Lorenzo Colitti76f793e2011-07-26 13:50:49 +00002126 ift->valid_lft = valid_lft;
2127 ift->prefered_lft = prefered_lft;
2128 ift->tstamp = now;
2129 if (prefered_lft > 0)
2130 ift->flags &= ~IFA_F_DEPRECATED;
2131
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 spin_unlock(&ift->lock);
2133 if (!(flags&IFA_F_TENTATIVE))
2134 ipv6_ifa_notify(0, ift);
2135 }
2136
Glenn Wursteraed65502010-09-27 07:04:30 +00002137 if ((create || list_empty(&in6_dev->tempaddr_list)) && in6_dev->cnf.use_tempaddr > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 /*
Lorenzo Colitti76f793e2011-07-26 13:50:49 +00002139 * When a new public address is created as
2140 * described in [ADDRCONF], also create a new
2141 * temporary address. Also create a temporary
2142 * address if it's enabled but no temporary
2143 * address currently exists.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002145 read_unlock_bh(&in6_dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 ipv6_create_tempaddr(ifp, NULL);
2147 } else {
2148 read_unlock_bh(&in6_dev->lock);
2149 }
2150#endif
2151 in6_ifa_put(ifp);
2152 addrconf_verify(0);
2153 }
2154 }
2155 inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
2156 in6_dev_put(in6_dev);
2157}
2158
2159/*
2160 * Set destination address.
2161 * Special case for SIT interfaces where we create a new "virtual"
2162 * device.
2163 */
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002164int addrconf_set_dstaddr(struct net *net, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165{
2166 struct in6_ifreq ireq;
2167 struct net_device *dev;
2168 int err = -EINVAL;
2169
2170 rtnl_lock();
2171
2172 err = -EFAULT;
2173 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2174 goto err_exit;
2175
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002176 dev = __dev_get_by_index(net, ireq.ifr6_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177
2178 err = -ENODEV;
2179 if (dev == NULL)
2180 goto err_exit;
2181
Joerg Roedel0be669b2006-10-10 14:49:53 -07002182#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 if (dev->type == ARPHRD_SIT) {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -08002184 const struct net_device_ops *ops = dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 struct ifreq ifr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 struct ip_tunnel_parm p;
2187
2188 err = -EADDRNOTAVAIL;
2189 if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4))
2190 goto err_exit;
2191
2192 memset(&p, 0, sizeof(p));
2193 p.iph.daddr = ireq.ifr6_addr.s6_addr32[3];
2194 p.iph.saddr = 0;
2195 p.iph.version = 4;
2196 p.iph.ihl = 5;
2197 p.iph.protocol = IPPROTO_IPV6;
2198 p.iph.ttl = 64;
Stephen Hemmingerd20b3102008-01-21 00:48:43 -08002199 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -08002201 if (ops->ndo_do_ioctl) {
2202 mm_segment_t oldfs = get_fs();
2203
2204 set_fs(KERNEL_DS);
2205 err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
2206 set_fs(oldfs);
2207 } else
2208 err = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209
2210 if (err == 0) {
2211 err = -ENOBUFS;
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002212 dev = __dev_get_by_name(net, p.name);
2213 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 goto err_exit;
2215 err = dev_open(dev);
2216 }
2217 }
Joerg Roedel0be669b2006-10-10 14:49:53 -07002218#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219
2220err_exit:
2221 rtnl_unlock();
2222 return err;
2223}
2224
2225/*
2226 * Manual configuration of address on an interface
2227 */
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002228static int inet6_addr_add(struct net *net, int ifindex, const struct in6_addr *pfx,
Thomas Graf24ef0da2008-05-28 16:54:22 +02002229 unsigned int plen, __u8 ifa_flags, __u32 prefered_lft,
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002230 __u32 valid_lft)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231{
2232 struct inet6_ifaddr *ifp;
2233 struct inet6_dev *idev;
2234 struct net_device *dev;
2235 int scope;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002236 u32 flags;
2237 clock_t expires;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002238 unsigned long timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239
2240 ASSERT_RTNL();
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002241
Thomas Graf24ef0da2008-05-28 16:54:22 +02002242 if (plen > 128)
2243 return -EINVAL;
2244
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002245 /* check the lifetime */
2246 if (!valid_lft || prefered_lft > valid_lft)
2247 return -EINVAL;
2248
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002249 dev = __dev_get_by_index(net, ifindex);
2250 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 return -ENODEV;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002252
Brian Haley64e724f2010-07-20 10:34:30 +00002253 idev = addrconf_add_dev(dev);
2254 if (IS_ERR(idev))
2255 return PTR_ERR(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256
2257 scope = ipv6_addr_scope(pfx);
2258
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002259 timeout = addrconf_timeout_fixup(valid_lft, HZ);
2260 if (addrconf_finite_timeout(timeout)) {
2261 expires = jiffies_to_clock_t(timeout * HZ);
2262 valid_lft = timeout;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002263 flags = RTF_EXPIRES;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002264 } else {
2265 expires = 0;
2266 flags = 0;
2267 ifa_flags |= IFA_F_PERMANENT;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002268 }
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002269
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002270 timeout = addrconf_timeout_fixup(prefered_lft, HZ);
2271 if (addrconf_finite_timeout(timeout)) {
2272 if (timeout == 0)
2273 ifa_flags |= IFA_F_DEPRECATED;
2274 prefered_lft = timeout;
2275 }
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002276
2277 ifp = ipv6_add_addr(idev, pfx, plen, scope, ifa_flags);
2278
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 if (!IS_ERR(ifp)) {
Herbert Xu06aebfb2006-08-09 16:52:04 -07002280 spin_lock_bh(&ifp->lock);
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002281 ifp->valid_lft = valid_lft;
2282 ifp->prefered_lft = prefered_lft;
2283 ifp->tstamp = jiffies;
Herbert Xu06aebfb2006-08-09 16:52:04 -07002284 spin_unlock_bh(&ifp->lock);
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002285
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09002286 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev,
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002287 expires, flags);
Neil Horman95c385b2007-04-25 17:08:10 -07002288 /*
2289 * Note that section 3.1 of RFC 4429 indicates
2290 * that the Optimistic flag should not be set for
2291 * manually configured addresses
2292 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 addrconf_dad_start(ifp, 0);
2294 in6_ifa_put(ifp);
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002295 addrconf_verify(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 return 0;
2297 }
2298
2299 return PTR_ERR(ifp);
2300}
2301
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002302static int inet6_addr_del(struct net *net, int ifindex, const struct in6_addr *pfx,
Thomas Graf24ef0da2008-05-28 16:54:22 +02002303 unsigned int plen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304{
2305 struct inet6_ifaddr *ifp;
2306 struct inet6_dev *idev;
2307 struct net_device *dev;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002308
Thomas Graf24ef0da2008-05-28 16:54:22 +02002309 if (plen > 128)
2310 return -EINVAL;
2311
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002312 dev = __dev_get_by_index(net, ifindex);
2313 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 return -ENODEV;
2315
2316 if ((idev = __in6_dev_get(dev)) == NULL)
2317 return -ENXIO;
2318
2319 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00002320 list_for_each_entry(ifp, &idev->addr_list, if_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 if (ifp->prefix_len == plen &&
2322 ipv6_addr_equal(pfx, &ifp->addr)) {
2323 in6_ifa_hold(ifp);
2324 read_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002325
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 ipv6_del_addr(ifp);
2327
2328 /* If the last address is deleted administratively,
2329 disable IPv6 on this interface.
2330 */
stephen hemminger502a2ff2010-03-17 20:31:13 +00002331 if (list_empty(&idev->addr_list))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 addrconf_ifdown(idev->dev, 1);
2333 return 0;
2334 }
2335 }
2336 read_unlock_bh(&idev->lock);
2337 return -EADDRNOTAVAIL;
2338}
2339
2340
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002341int addrconf_add_ifaddr(struct net *net, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342{
2343 struct in6_ifreq ireq;
2344 int err;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002345
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 if (!capable(CAP_NET_ADMIN))
2347 return -EPERM;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002348
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2350 return -EFAULT;
2351
2352 rtnl_lock();
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002353 err = inet6_addr_add(net, ireq.ifr6_ifindex, &ireq.ifr6_addr,
2354 ireq.ifr6_prefixlen, IFA_F_PERMANENT,
2355 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 rtnl_unlock();
2357 return err;
2358}
2359
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002360int addrconf_del_ifaddr(struct net *net, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361{
2362 struct in6_ifreq ireq;
2363 int err;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002364
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 if (!capable(CAP_NET_ADMIN))
2366 return -EPERM;
2367
2368 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2369 return -EFAULT;
2370
2371 rtnl_lock();
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002372 err = inet6_addr_del(net, ireq.ifr6_ifindex, &ireq.ifr6_addr,
2373 ireq.ifr6_prefixlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 rtnl_unlock();
2375 return err;
2376}
2377
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08002378static void add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
2379 int plen, int scope)
2380{
2381 struct inet6_ifaddr *ifp;
2382
2383 ifp = ipv6_add_addr(idev, addr, plen, scope, IFA_F_PERMANENT);
2384 if (!IS_ERR(ifp)) {
2385 spin_lock_bh(&ifp->lock);
2386 ifp->flags &= ~IFA_F_TENTATIVE;
2387 spin_unlock_bh(&ifp->lock);
2388 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2389 in6_ifa_put(ifp);
2390 }
2391}
2392
Joerg Roedel0be669b2006-10-10 14:49:53 -07002393#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394static void sit_add_v4_addrs(struct inet6_dev *idev)
2395{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 struct in6_addr addr;
2397 struct net_device *dev;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002398 struct net *net = dev_net(idev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 int scope;
2400
2401 ASSERT_RTNL();
2402
2403 memset(&addr, 0, sizeof(struct in6_addr));
2404 memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
2405
2406 if (idev->dev->flags&IFF_POINTOPOINT) {
2407 addr.s6_addr32[0] = htonl(0xfe800000);
2408 scope = IFA_LINK;
2409 } else {
2410 scope = IPV6_ADDR_COMPATv4;
2411 }
2412
2413 if (addr.s6_addr32[3]) {
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08002414 add_addr(idev, &addr, 128, scope);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 return;
2416 }
2417
Benjamin Thery6fda7352008-03-05 10:47:47 -08002418 for_each_netdev(net, dev) {
Herbert Xue5ed6392005-10-03 14:35:55 -07002419 struct in_device * in_dev = __in_dev_get_rtnl(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 if (in_dev && (dev->flags & IFF_UP)) {
2421 struct in_ifaddr * ifa;
2422
2423 int flag = scope;
2424
2425 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
2426 int plen;
2427
2428 addr.s6_addr32[3] = ifa->ifa_local;
2429
2430 if (ifa->ifa_scope == RT_SCOPE_LINK)
2431 continue;
2432 if (ifa->ifa_scope >= RT_SCOPE_HOST) {
2433 if (idev->dev->flags&IFF_POINTOPOINT)
2434 continue;
2435 flag |= IFA_HOST;
2436 }
2437 if (idev->dev->flags&IFF_POINTOPOINT)
2438 plen = 64;
2439 else
2440 plen = 96;
2441
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08002442 add_addr(idev, &addr, plen, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 }
2444 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002445 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446}
Joerg Roedel0be669b2006-10-10 14:49:53 -07002447#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448
2449static void init_loopback(struct net_device *dev)
2450{
2451 struct inet6_dev *idev;
Balakumaran Kannanef95e3d2013-04-02 16:15:05 +05302452 struct net_device *sp_dev;
2453 struct inet6_ifaddr *sp_ifa;
2454 struct rt6_info *sp_rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455
2456 /* ::1 */
2457
2458 ASSERT_RTNL();
2459
2460 if ((idev = ipv6_find_idev(dev)) == NULL) {
2461 printk(KERN_DEBUG "init loopback: add_dev failed\n");
2462 return;
2463 }
2464
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08002465 add_addr(idev, &in6addr_loopback, 128, IFA_HOST);
Balakumaran Kannanef95e3d2013-04-02 16:15:05 +05302466
2467 /* Add routes to other interface's IPv6 addresses */
2468 for_each_netdev(dev_net(dev), sp_dev) {
2469 if (!strcmp(sp_dev->name, dev->name))
2470 continue;
2471
2472 idev = __in6_dev_get(sp_dev);
2473 if (!idev)
2474 continue;
2475
2476 read_lock_bh(&idev->lock);
2477 list_for_each_entry(sp_ifa, &idev->addr_list, if_list) {
2478
2479 if (sp_ifa->flags & (IFA_F_DADFAILED | IFA_F_TENTATIVE))
2480 continue;
2481
Gao fengdabed302013-06-16 11:14:30 +08002482 if (sp_ifa->rt)
2483 continue;
2484
Balakumaran Kannanef95e3d2013-04-02 16:15:05 +05302485 sp_rt = addrconf_dst_alloc(idev, &sp_ifa->addr, 0);
2486
2487 /* Failure cases are ignored */
Gao feng2f4997c2013-06-02 22:16:21 +00002488 if (!IS_ERR(sp_rt)) {
2489 sp_ifa->rt = sp_rt;
Balakumaran Kannanef95e3d2013-04-02 16:15:05 +05302490 ip6_ins_rt(sp_rt);
Gao feng2f4997c2013-06-02 22:16:21 +00002491 }
Balakumaran Kannanef95e3d2013-04-02 16:15:05 +05302492 }
2493 read_unlock_bh(&idev->lock);
2494 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495}
2496
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002497static void addrconf_add_linklocal(struct inet6_dev *idev, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498{
2499 struct inet6_ifaddr * ifp;
Neil Horman95c385b2007-04-25 17:08:10 -07002500 u32 addr_flags = IFA_F_PERMANENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501
Neil Horman95c385b2007-04-25 17:08:10 -07002502#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
2503 if (idev->cnf.optimistic_dad &&
David Miller702beb82008-07-20 18:17:02 -07002504 !dev_net(idev->dev)->ipv6.devconf_all->forwarding)
Neil Horman95c385b2007-04-25 17:08:10 -07002505 addr_flags |= IFA_F_OPTIMISTIC;
2506#endif
2507
2508
2509 ifp = ipv6_add_addr(idev, addr, 64, IFA_LINK, addr_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 if (!IS_ERR(ifp)) {
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09002511 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, idev->dev, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 addrconf_dad_start(ifp, 0);
2513 in6_ifa_put(ifp);
2514 }
2515}
2516
2517static void addrconf_dev_config(struct net_device *dev)
2518{
2519 struct in6_addr addr;
2520 struct inet6_dev * idev;
2521
2522 ASSERT_RTNL();
2523
Herbert Xu74235a22007-06-14 13:02:55 -07002524 if ((dev->type != ARPHRD_ETHER) &&
2525 (dev->type != ARPHRD_FDDI) &&
2526 (dev->type != ARPHRD_IEEE802_TR) &&
2527 (dev->type != ARPHRD_ARCNET) &&
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002528 (dev->type != ARPHRD_RAWIP) &&
Herbert Xu74235a22007-06-14 13:02:55 -07002529 (dev->type != ARPHRD_INFINIBAND)) {
2530 /* Alas, we support only Ethernet autoconfiguration. */
2531 return;
2532 }
2533
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 idev = addrconf_add_dev(dev);
Brian Haley64e724f2010-07-20 10:34:30 +00002535 if (IS_ERR(idev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 return;
2537
2538 memset(&addr, 0, sizeof(struct in6_addr));
2539 addr.s6_addr32[0] = htonl(0xFE800000);
2540
2541 if (ipv6_generate_eui64(addr.s6_addr + 8, dev) == 0)
2542 addrconf_add_linklocal(idev, &addr);
2543}
2544
Joerg Roedel0be669b2006-10-10 14:49:53 -07002545#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546static void addrconf_sit_config(struct net_device *dev)
2547{
2548 struct inet6_dev *idev;
2549
2550 ASSERT_RTNL();
2551
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002552 /*
2553 * Configure the tunnel with one of our IPv4
2554 * addresses... we should configure all of
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 * our v4 addrs in the tunnel
2556 */
2557
2558 if ((idev = ipv6_find_idev(dev)) == NULL) {
2559 printk(KERN_DEBUG "init sit: add_dev failed\n");
2560 return;
2561 }
2562
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11002563 if (dev->priv_flags & IFF_ISATAP) {
2564 struct in6_addr addr;
2565
2566 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
2567 addrconf_prefix_route(&addr, 64, dev, 0, 0);
2568 if (!ipv6_generate_eui64(addr.s6_addr + 8, dev))
2569 addrconf_add_linklocal(idev, &addr);
2570 return;
2571 }
2572
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 sit_add_v4_addrs(idev);
2574
2575 if (dev->flags&IFF_POINTOPOINT) {
2576 addrconf_add_mroute(dev);
2577 addrconf_add_lroute(dev);
2578 } else
2579 sit_route_add(dev);
2580}
Joerg Roedel0be669b2006-10-10 14:49:53 -07002581#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582
stephen hemmingeraee80b52011-06-08 10:44:30 +00002583#if defined(CONFIG_NET_IPGRE) || defined(CONFIG_NET_IPGRE_MODULE)
2584static void addrconf_gre_config(struct net_device *dev)
2585{
2586 struct inet6_dev *idev;
2587 struct in6_addr addr;
2588
2589 pr_info("ipv6: addrconf_gre_config(%s)\n", dev->name);
2590
2591 ASSERT_RTNL();
2592
2593 if ((idev = ipv6_find_idev(dev)) == NULL) {
2594 printk(KERN_DEBUG "init gre: add_dev failed\n");
2595 return;
2596 }
2597
2598 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
2599 addrconf_prefix_route(&addr, 64, dev, 0, 0);
2600
2601 if (!ipv6_generate_eui64(addr.s6_addr + 8, dev))
2602 addrconf_add_linklocal(idev, &addr);
2603}
2604#endif
2605
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606static inline int
2607ipv6_inherit_linklocal(struct inet6_dev *idev, struct net_device *link_dev)
2608{
2609 struct in6_addr lladdr;
2610
Neil Horman95c385b2007-04-25 17:08:10 -07002611 if (!ipv6_get_lladdr(link_dev, &lladdr, IFA_F_TENTATIVE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 addrconf_add_linklocal(idev, &lladdr);
2613 return 0;
2614 }
2615 return -1;
2616}
2617
2618static void ip6_tnl_add_linklocal(struct inet6_dev *idev)
2619{
2620 struct net_device *link_dev;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002621 struct net *net = dev_net(idev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622
2623 /* first try to inherit the link-local address from the link device */
2624 if (idev->dev->iflink &&
Benjamin Thery6fda7352008-03-05 10:47:47 -08002625 (link_dev = __dev_get_by_index(net, idev->dev->iflink))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 if (!ipv6_inherit_linklocal(idev, link_dev))
2627 return;
2628 }
2629 /* then try to inherit it from any device */
Benjamin Thery6fda7352008-03-05 10:47:47 -08002630 for_each_netdev(net, link_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 if (!ipv6_inherit_linklocal(idev, link_dev))
2632 return;
2633 }
2634 printk(KERN_DEBUG "init ip6-ip6: add_linklocal failed\n");
2635}
2636
2637/*
2638 * Autoconfigure tunnel with a link-local address so routing protocols,
2639 * DHCPv6, MLD etc. can be run over the virtual link
2640 */
2641
2642static void addrconf_ip6_tnl_config(struct net_device *dev)
2643{
2644 struct inet6_dev *idev;
2645
2646 ASSERT_RTNL();
2647
Stephen Hemmingere21e8462010-03-20 16:09:01 -07002648 idev = addrconf_add_dev(dev);
Brian Haley64e724f2010-07-20 10:34:30 +00002649 if (IS_ERR(idev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 printk(KERN_DEBUG "init ip6-ip6: add_dev failed\n");
2651 return;
2652 }
2653 ip6_tnl_add_linklocal(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654}
2655
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002656static int addrconf_notify(struct notifier_block *this, unsigned long event,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 void * data)
2658{
2659 struct net_device *dev = (struct net_device *) data;
Herbert Xu74235a22007-06-14 13:02:55 -07002660 struct inet6_dev *idev = __in6_dev_get(dev);
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09002661 int run_pending = 0;
Herbert Xub217d612007-07-30 17:04:52 -07002662 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663
Stephen Hemmingere21e8462010-03-20 16:09:01 -07002664 switch (event) {
YOSHIFUJI Hideaki45ba9dd2007-02-15 02:07:27 +09002665 case NETDEV_REGISTER:
Herbert Xu74235a22007-06-14 13:02:55 -07002666 if (!idev && dev->mtu >= IPV6_MIN_MTU) {
YOSHIFUJI Hideaki45ba9dd2007-02-15 02:07:27 +09002667 idev = ipv6_add_dev(dev);
2668 if (!idev)
Herbert Xub217d612007-07-30 17:04:52 -07002669 return notifier_from_errno(-ENOMEM);
YOSHIFUJI Hideaki45ba9dd2007-02-15 02:07:27 +09002670 }
2671 break;
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07002672
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 case NETDEV_UP:
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002674 case NETDEV_CHANGE:
Jay Vosburghc2edacf2007-07-09 10:42:47 -07002675 if (dev->flags & IFF_SLAVE)
2676 break;
2677
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002678 if (event == NETDEV_UP) {
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -07002679 if (!addrconf_qdisc_ok(dev)) {
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002680 /* device is not ready yet. */
2681 printk(KERN_INFO
2682 "ADDRCONF(NETDEV_UP): %s: "
2683 "link is not ready\n",
2684 dev->name);
2685 break;
2686 }
Kristian Slavov99081042006-02-08 16:10:53 -08002687
Evgeniy Polyakovd31c7b82007-11-30 23:36:08 +11002688 if (!idev && dev->mtu >= IPV6_MIN_MTU)
2689 idev = ipv6_add_dev(dev);
2690
Benjamin Therye3ec6cf2008-11-05 01:43:57 -08002691 if (idev) {
Kristian Slavov99081042006-02-08 16:10:53 -08002692 idev->if_flags |= IF_READY;
Benjamin Therye3ec6cf2008-11-05 01:43:57 -08002693 run_pending = 1;
2694 }
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002695 } else {
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -07002696 if (!addrconf_qdisc_ok(dev)) {
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002697 /* device is still not ready. */
2698 break;
2699 }
2700
2701 if (idev) {
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07002702 if (idev->if_flags & IF_READY)
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002703 /* device is already configured. */
2704 break;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002705 idev->if_flags |= IF_READY;
2706 }
2707
2708 printk(KERN_INFO
2709 "ADDRCONF(NETDEV_CHANGE): %s: "
2710 "link becomes ready\n",
2711 dev->name);
2712
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09002713 run_pending = 1;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002714 }
2715
Stephen Hemmingere21e8462010-03-20 16:09:01 -07002716 switch (dev->type) {
Joerg Roedel0be669b2006-10-10 14:49:53 -07002717#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 case ARPHRD_SIT:
2719 addrconf_sit_config(dev);
2720 break;
Joerg Roedel0be669b2006-10-10 14:49:53 -07002721#endif
stephen hemmingeraee80b52011-06-08 10:44:30 +00002722#if defined(CONFIG_NET_IPGRE) || defined(CONFIG_NET_IPGRE_MODULE)
2723 case ARPHRD_IPGRE:
2724 addrconf_gre_config(dev);
2725 break;
2726#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 case ARPHRD_TUNNEL6:
2728 addrconf_ip6_tnl_config(dev);
2729 break;
2730 case ARPHRD_LOOPBACK:
2731 init_loopback(dev);
2732 break;
2733
2734 default:
2735 addrconf_dev_config(dev);
2736 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002737 }
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07002738
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 if (idev) {
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09002740 if (run_pending)
2741 addrconf_dad_run(idev);
2742
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07002743 /*
2744 * If the MTU changed during the interface down,
2745 * when the interface up, the changed MTU must be
2746 * reflected in the idev as well as routers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 */
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07002748 if (idev->cnf.mtu6 != dev->mtu &&
2749 dev->mtu >= IPV6_MIN_MTU) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 rt6_mtu_change(dev, dev->mtu);
2751 idev->cnf.mtu6 = dev->mtu;
2752 }
2753 idev->tstamp = jiffies;
2754 inet6_ifinfo_notify(RTM_NEWLINK, idev);
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07002755
2756 /*
2757 * If the changed mtu during down is lower than
2758 * IPV6_MIN_MTU stop IPv6 on this interface.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 */
2760 if (dev->mtu < IPV6_MIN_MTU)
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07002761 addrconf_ifdown(dev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 }
2763 break;
2764
2765 case NETDEV_CHANGEMTU:
Evgeniy Polyakovd31c7b82007-11-30 23:36:08 +11002766 if (idev && dev->mtu >= IPV6_MIN_MTU) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 rt6_mtu_change(dev, dev->mtu);
2768 idev->cnf.mtu6 = dev->mtu;
2769 break;
2770 }
2771
Evgeniy Polyakovd31c7b82007-11-30 23:36:08 +11002772 if (!idev && dev->mtu >= IPV6_MIN_MTU) {
2773 idev = ipv6_add_dev(dev);
2774 if (idev)
2775 break;
2776 }
2777
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07002778 /*
2779 * MTU falled under IPV6_MIN_MTU.
2780 * Stop IPv6 on this interface.
2781 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782
2783 case NETDEV_DOWN:
2784 case NETDEV_UNREGISTER:
2785 /*
2786 * Remove all addresses from this interface.
2787 */
2788 addrconf_ifdown(dev, event != NETDEV_DOWN);
2789 break;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002790
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 case NETDEV_CHANGENAME:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 if (idev) {
Stephen Hemminger5632c512007-04-28 21:16:39 -07002793 snmp6_unregister_dev(idev);
Pavel Emelyanov408c4762008-01-10 17:41:21 -08002794 addrconf_sysctl_unregister(idev);
Pavel Emelyanovf52295a2007-12-02 00:58:37 +11002795 addrconf_sysctl_register(idev);
Herbert Xub217d612007-07-30 17:04:52 -07002796 err = snmp6_register_dev(idev);
2797 if (err)
2798 return notifier_from_errno(err);
Stephen Hemminger5632c512007-04-28 21:16:39 -07002799 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 break;
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07002801
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00002802 case NETDEV_PRE_TYPE_CHANGE:
2803 case NETDEV_POST_TYPE_CHANGE:
2804 addrconf_type_change(dev, event);
Moni Shoua75c78502009-09-15 02:37:40 -07002805 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002806 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807
2808 return NOTIFY_OK;
2809}
2810
2811/*
2812 * addrconf module should be notified of a device going up
2813 */
2814static struct notifier_block ipv6_dev_notf = {
2815 .notifier_call = addrconf_notify,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816};
2817
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00002818static void addrconf_type_change(struct net_device *dev, unsigned long event)
Moni Shoua75c78502009-09-15 02:37:40 -07002819{
2820 struct inet6_dev *idev;
2821 ASSERT_RTNL();
2822
2823 idev = __in6_dev_get(dev);
2824
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00002825 if (event == NETDEV_POST_TYPE_CHANGE)
Moni Shoua75c78502009-09-15 02:37:40 -07002826 ipv6_mc_remap(idev);
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00002827 else if (event == NETDEV_PRE_TYPE_CHANGE)
Moni Shoua75c78502009-09-15 02:37:40 -07002828 ipv6_mc_unmap(idev);
2829}
2830
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831static int addrconf_ifdown(struct net_device *dev, int how)
2832{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002833 struct net *net = dev_net(dev);
stephen hemminger502a2ff2010-03-17 20:31:13 +00002834 struct inet6_dev *idev;
2835 struct inet6_ifaddr *ifa;
David S. Miller73a8bd72011-01-23 23:27:15 -08002836 int state, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837
2838 ASSERT_RTNL();
2839
David S. Miller73a8bd72011-01-23 23:27:15 -08002840 rt6_ifdown(net, dev);
2841 neigh_ifdown(&nd_tbl, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842
2843 idev = __in6_dev_get(dev);
2844 if (idev == NULL)
2845 return -ENODEV;
2846
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07002847 /*
2848 * Step 1: remove reference to ipv6 device from parent device.
2849 * Do not dev_put!
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 */
Denis V. Lunev439e2382008-04-03 13:30:17 -07002851 if (how) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 idev->dead = 1;
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07002853
2854 /* protected by rtnl_lock */
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00002855 RCU_INIT_POINTER(dev->ip6_ptr, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856
2857 /* Step 1.5: remove snmp6 entry */
2858 snmp6_unregister_dev(idev);
2859
2860 }
2861
David S. Miller73a8bd72011-01-23 23:27:15 -08002862 /* Step 2: clear hash table */
2863 for (i = 0; i < IN6_ADDR_HSIZE; i++) {
2864 struct hlist_head *h = &inet6_addr_lst[i];
2865 struct hlist_node *n;
2866
2867 spin_lock_bh(&addrconf_hash_lock);
2868 restart:
2869 hlist_for_each_entry_rcu(ifa, n, h, addr_lst) {
2870 if (ifa->idev == idev) {
2871 hlist_del_init_rcu(&ifa->addr_lst);
2872 addrconf_del_timer(ifa);
2873 goto restart;
2874 }
2875 }
2876 spin_unlock_bh(&addrconf_hash_lock);
2877 }
2878
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 write_lock_bh(&idev->lock);
2880
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07002881 /* Step 2: clear flags for stateless addrconf */
Denis V. Lunev439e2382008-04-03 13:30:17 -07002882 if (!how)
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002883 idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885#ifdef CONFIG_IPV6_PRIVACY
Denis V. Lunev439e2382008-04-03 13:30:17 -07002886 if (how && del_timer(&idev->regen_timer))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 in6_dev_put(idev);
2888
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07002889 /* Step 3: clear tempaddr list */
stephen hemminger372e6c82010-03-17 20:31:09 +00002890 while (!list_empty(&idev->tempaddr_list)) {
2891 ifa = list_first_entry(&idev->tempaddr_list,
2892 struct inet6_ifaddr, tmp_list);
2893 list_del(&ifa->tmp_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 write_unlock_bh(&idev->lock);
2895 spin_lock_bh(&ifa->lock);
2896
2897 if (ifa->ifpub) {
2898 in6_ifa_put(ifa->ifpub);
2899 ifa->ifpub = NULL;
2900 }
2901 spin_unlock_bh(&ifa->lock);
2902 in6_ifa_put(ifa);
2903 write_lock_bh(&idev->lock);
2904 }
2905#endif
stephen hemminger8f37ada2010-03-03 08:19:59 +00002906
stephen hemminger502a2ff2010-03-17 20:31:13 +00002907 while (!list_empty(&idev->addr_list)) {
2908 ifa = list_first_entry(&idev->addr_list,
2909 struct inet6_ifaddr, if_list);
stephen hemminger84e8b802010-03-02 13:32:46 +00002910 addrconf_del_timer(ifa);
2911
David S. Miller73a8bd72011-01-23 23:27:15 -08002912 list_del(&ifa->if_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913
David S. Miller73a8bd72011-01-23 23:27:15 -08002914 write_unlock_bh(&idev->lock);
stephen hemminger84e8b802010-03-02 13:32:46 +00002915
David S. Miller73a8bd72011-01-23 23:27:15 -08002916 spin_lock_bh(&ifa->state_lock);
2917 state = ifa->state;
2918 ifa->state = INET6_IFADDR_STATE_DEAD;
2919 spin_unlock_bh(&ifa->state_lock);
stephen hemminger84e8b802010-03-02 13:32:46 +00002920
David S. Miller73a8bd72011-01-23 23:27:15 -08002921 if (state != INET6_IFADDR_STATE_DEAD) {
2922 __ipv6_ifa_notify(RTM_DELADDR, ifa);
2923 atomic_notifier_call_chain(&inet6addr_chain, NETDEV_DOWN, ifa);
stephen hemminger27bdb2a2010-04-12 05:41:32 +00002924 }
David S. Miller73a8bd72011-01-23 23:27:15 -08002925 in6_ifa_put(ifa);
stephen hemminger8f37ada2010-03-03 08:19:59 +00002926
David S. Miller73a8bd72011-01-23 23:27:15 -08002927 write_lock_bh(&idev->lock);
2928 }
stephen hemminger8f37ada2010-03-03 08:19:59 +00002929
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 write_unlock_bh(&idev->lock);
2931
2932 /* Step 5: Discard multicast list */
Denis V. Lunev439e2382008-04-03 13:30:17 -07002933 if (how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 ipv6_mc_destroy_dev(idev);
2935 else
2936 ipv6_mc_down(idev);
2937
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 idev->tstamp = jiffies;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002939
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07002940 /* Last: Shot the device (if unregistered) */
Denis V. Lunev439e2382008-04-03 13:30:17 -07002941 if (how) {
Pavel Emelyanov408c4762008-01-10 17:41:21 -08002942 addrconf_sysctl_unregister(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 neigh_parms_release(&nd_tbl, idev->nd_parms);
2944 neigh_ifdown(&nd_tbl, dev);
2945 in6_dev_put(idev);
2946 }
2947 return 0;
2948}
2949
2950static void addrconf_rs_timer(unsigned long data)
2951{
2952 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
stephen hemminger5b2a1952010-03-02 13:32:45 +00002953 struct inet6_dev *idev = ifp->idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954
stephen hemminger5b2a1952010-03-02 13:32:45 +00002955 read_lock(&idev->lock);
2956 if (idev->dead || !(idev->if_flags & IF_READY))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 goto out;
2958
stephen hemminger5b2a1952010-03-02 13:32:45 +00002959 if (idev->cnf.forwarding)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 goto out;
stephen hemminger5b2a1952010-03-02 13:32:45 +00002961
2962 /* Announcement received after solicitation was sent */
2963 if (idev->if_flags & IF_RA_RCVD)
2964 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965
2966 spin_lock(&ifp->lock);
stephen hemminger5b2a1952010-03-02 13:32:45 +00002967 if (ifp->probes++ < idev->cnf.rtr_solicits) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 /* The wait after the last probe can be shorter */
2969 addrconf_mod_timer(ifp, AC_RS,
stephen hemminger5b2a1952010-03-02 13:32:45 +00002970 (ifp->probes == idev->cnf.rtr_solicits) ?
2971 idev->cnf.rtr_solicit_delay :
2972 idev->cnf.rtr_solicit_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 spin_unlock(&ifp->lock);
2974
stephen hemminger5b2a1952010-03-02 13:32:45 +00002975 ndisc_send_rs(idev->dev, &ifp->addr, &in6addr_linklocal_allrouters);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 } else {
2977 spin_unlock(&ifp->lock);
2978 /*
2979 * Note: we do not support deprecated "all on-link"
2980 * assumption any longer.
2981 */
2982 printk(KERN_DEBUG "%s: no IPv6 routers present\n",
stephen hemminger5b2a1952010-03-02 13:32:45 +00002983 idev->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 }
2985
2986out:
stephen hemminger5b2a1952010-03-02 13:32:45 +00002987 read_unlock(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 in6_ifa_put(ifp);
2989}
2990
2991/*
2992 * Duplicate Address Detection
2993 */
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002994static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
2995{
2996 unsigned long rand_num;
2997 struct inet6_dev *idev = ifp->idev;
2998
Neil Horman95c385b2007-04-25 17:08:10 -07002999 if (ifp->flags & IFA_F_OPTIMISTIC)
3000 rand_num = 0;
3001 else
3002 rand_num = net_random() % (idev->cnf.rtr_solicit_delay ? : 1);
3003
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003004 ifp->probes = idev->cnf.dad_transmits;
3005 addrconf_mod_timer(ifp, AC_DAD, rand_num);
3006}
3007
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -07003008static void addrconf_dad_start(struct inet6_ifaddr *ifp, u32 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009{
3010 struct inet6_dev *idev = ifp->idev;
3011 struct net_device *dev = idev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012
3013 addrconf_join_solict(dev, &ifp->addr);
3014
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 net_srandom(ifp->addr.s6_addr32[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016
3017 read_lock_bh(&idev->lock);
Herbert Xu622ccdf2010-05-18 15:56:06 -07003018 spin_lock(&ifp->lock);
Herbert Xue9d3e082010-05-18 15:36:06 -07003019 if (ifp->state == INET6_IFADDR_STATE_DEAD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021
3022 if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09003023 idev->cnf.accept_dad < 1 ||
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003024 !(ifp->flags&IFA_F_TENTATIVE) ||
3025 ifp->flags & IFA_F_NODAD) {
Brian Haleycc411d02009-09-09 14:41:32 +00003026 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
stephen hemminger21809fa2010-02-08 19:48:52 +00003027 spin_unlock(&ifp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 read_unlock_bh(&idev->lock);
3029
3030 addrconf_dad_completed(ifp);
3031 return;
3032 }
3033
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08003034 if (!(idev->if_flags & IF_READY)) {
stephen hemminger21809fa2010-02-08 19:48:52 +00003035 spin_unlock(&ifp->lock);
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08003036 read_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003037 /*
Masatake YAMATO590a9882009-06-09 10:41:12 +09003038 * If the device is not ready:
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003039 * - keep it tentative if it is a permanent address.
3040 * - otherwise, kill it.
3041 */
3042 in6_ifa_hold(ifp);
Brian Haleycc411d02009-09-09 14:41:32 +00003043 addrconf_dad_stop(ifp, 0);
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08003044 return;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003045 }
Neil Horman95c385b2007-04-25 17:08:10 -07003046
3047 /*
3048 * Optimistic nodes can start receiving
3049 * Frames right away
3050 */
Stephen Hemmingere21e8462010-03-20 16:09:01 -07003051 if (ifp->flags & IFA_F_OPTIMISTIC)
Neil Horman95c385b2007-04-25 17:08:10 -07003052 ip6_ins_rt(ifp->rt);
3053
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08003054 addrconf_dad_kick(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055out:
Herbert Xu622ccdf2010-05-18 15:56:06 -07003056 spin_unlock(&ifp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 read_unlock_bh(&idev->lock);
3058}
3059
3060static void addrconf_dad_timer(unsigned long data)
3061{
3062 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
3063 struct inet6_dev *idev = ifp->idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 struct in6_addr mcaddr;
3065
Herbert Xuf2344a12010-05-18 15:55:27 -07003066 if (!ifp->probes && addrconf_dad_end(ifp))
3067 goto out;
3068
stephen hemminger122e4512010-03-02 13:32:44 +00003069 read_lock(&idev->lock);
3070 if (idev->dead || !(idev->if_flags & IF_READY)) {
3071 read_unlock(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 goto out;
3073 }
stephen hemminger21809fa2010-02-08 19:48:52 +00003074
3075 spin_lock(&ifp->lock);
Herbert Xu622ccdf2010-05-18 15:56:06 -07003076 if (ifp->state == INET6_IFADDR_STATE_DEAD) {
3077 spin_unlock(&ifp->lock);
3078 read_unlock(&idev->lock);
3079 goto out;
3080 }
3081
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 if (ifp->probes == 0) {
3083 /*
3084 * DAD was successful
3085 */
3086
Brian Haleycc411d02009-09-09 14:41:32 +00003087 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
stephen hemminger21809fa2010-02-08 19:48:52 +00003088 spin_unlock(&ifp->lock);
stephen hemminger122e4512010-03-02 13:32:44 +00003089 read_unlock(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090
3091 addrconf_dad_completed(ifp);
3092
3093 goto out;
3094 }
3095
3096 ifp->probes--;
3097 addrconf_mod_timer(ifp, AC_DAD, ifp->idev->nd_parms->retrans_time);
stephen hemminger21809fa2010-02-08 19:48:52 +00003098 spin_unlock(&ifp->lock);
stephen hemminger122e4512010-03-02 13:32:44 +00003099 read_unlock(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100
3101 /* send a neighbour solicitation for our addr */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
YOSHIFUJI Hideakid7aabf22008-04-10 15:42:11 +09003103 ndisc_send_ns(ifp->idev->dev, NULL, &ifp->addr, &mcaddr, &in6addr_any);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104out:
3105 in6_ifa_put(ifp);
3106}
3107
3108static void addrconf_dad_completed(struct inet6_ifaddr *ifp)
3109{
Stephen Hemmingere21e8462010-03-20 16:09:01 -07003110 struct net_device *dev = ifp->idev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111
3112 /*
3113 * Configure the address for reception. Now it is valid.
3114 */
3115
3116 ipv6_ifa_notify(RTM_NEWADDR, ifp);
3117
Tore Anderson026359b2011-08-28 23:47:33 +00003118 /* If added prefix is link local and we are prepared to process
3119 router advertisements, start sending router solicitations.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 */
3121
Tore Anderson026359b2011-08-28 23:47:33 +00003122 if (((ifp->idev->cnf.accept_ra == 1 && !ifp->idev->cnf.forwarding) ||
3123 ifp->idev->cnf.accept_ra == 2) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 ifp->idev->cnf.rtr_solicits > 0 &&
3125 (dev->flags&IFF_LOOPBACK) == 0 &&
3126 (ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 /*
3128 * If a host as already performed a random delay
3129 * [...] as part of DAD [...] there is no need
3130 * to delay again before sending the first RS
3131 */
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +09003132 ndisc_send_rs(ifp->idev->dev, &ifp->addr, &in6addr_linklocal_allrouters);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133
3134 spin_lock_bh(&ifp->lock);
3135 ifp->probes = 1;
3136 ifp->idev->if_flags |= IF_RS_SENT;
3137 addrconf_mod_timer(ifp, AC_RS, ifp->idev->cnf.rtr_solicit_interval);
3138 spin_unlock_bh(&ifp->lock);
3139 }
3140}
3141
Stephen Hemmingere21e8462010-03-20 16:09:01 -07003142static void addrconf_dad_run(struct inet6_dev *idev)
3143{
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09003144 struct inet6_ifaddr *ifp;
3145
3146 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00003147 list_for_each_entry(ifp, &idev->addr_list, if_list) {
stephen hemminger21809fa2010-02-08 19:48:52 +00003148 spin_lock(&ifp->lock);
Herbert Xuf2344a12010-05-18 15:55:27 -07003149 if (ifp->flags & IFA_F_TENTATIVE &&
3150 ifp->state == INET6_IFADDR_STATE_DAD)
3151 addrconf_dad_kick(ifp);
stephen hemminger21809fa2010-02-08 19:48:52 +00003152 spin_unlock(&ifp->lock);
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09003153 }
3154 read_unlock_bh(&idev->lock);
3155}
3156
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157#ifdef CONFIG_PROC_FS
3158struct if6_iter_state {
Daniel Lezcano3c400902008-01-10 22:42:49 -08003159 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160 int bucket;
Mihai Maruseac1d578302012-01-03 23:31:35 +00003161 int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162};
3163
Mihai Maruseac1d578302012-01-03 23:31:35 +00003164static struct inet6_ifaddr *if6_get_first(struct seq_file *seq, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165{
3166 struct inet6_ifaddr *ifa = NULL;
3167 struct if6_iter_state *state = seq->private;
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09003168 struct net *net = seq_file_net(seq);
Mihai Maruseac1d578302012-01-03 23:31:35 +00003169 int p = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170
Mihai Maruseac1d578302012-01-03 23:31:35 +00003171 /* initial bucket if pos is 0 */
3172 if (pos == 0) {
3173 state->bucket = 0;
3174 state->offset = 0;
3175 }
3176
3177 for (; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
stephen hemmingerc2e21292010-03-17 20:31:10 +00003178 struct hlist_node *n;
Eric Dumazet4f70ecc2010-05-03 10:50:14 +00003179 hlist_for_each_entry_rcu_bh(ifa, n, &inet6_addr_lst[state->bucket],
Mihai Maruseac1d578302012-01-03 23:31:35 +00003180 addr_lst) {
Eric Dumazetdd2c50e2012-10-16 07:37:27 +00003181 if (!net_eq(dev_net(ifa->idev->dev), net))
3182 continue;
Mihai Maruseac1d578302012-01-03 23:31:35 +00003183 /* sync with offset */
3184 if (p < state->offset) {
3185 p++;
3186 continue;
3187 }
3188 state->offset++;
Eric Dumazetdd2c50e2012-10-16 07:37:27 +00003189 return ifa;
Mihai Maruseac1d578302012-01-03 23:31:35 +00003190 }
3191
3192 /* prepare for next bucket */
3193 state->offset = 0;
3194 p = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 }
stephen hemmingerc2e21292010-03-17 20:31:10 +00003196 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197}
3198
stephen hemmingerc2e21292010-03-17 20:31:10 +00003199static struct inet6_ifaddr *if6_get_next(struct seq_file *seq,
3200 struct inet6_ifaddr *ifa)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201{
3202 struct if6_iter_state *state = seq->private;
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09003203 struct net *net = seq_file_net(seq);
stephen hemmingerc2e21292010-03-17 20:31:10 +00003204 struct hlist_node *n = &ifa->addr_lst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205
Mihai Maruseac1d578302012-01-03 23:31:35 +00003206 hlist_for_each_entry_continue_rcu_bh(ifa, n, addr_lst) {
Eric Dumazetdd2c50e2012-10-16 07:37:27 +00003207 if (!net_eq(dev_net(ifa->idev->dev), net))
3208 continue;
Mihai Maruseac1d578302012-01-03 23:31:35 +00003209 state->offset++;
Eric Dumazetdd2c50e2012-10-16 07:37:27 +00003210 return ifa;
Mihai Maruseac1d578302012-01-03 23:31:35 +00003211 }
stephen hemmingerc2e21292010-03-17 20:31:10 +00003212
3213 while (++state->bucket < IN6_ADDR_HSIZE) {
Mihai Maruseac1d578302012-01-03 23:31:35 +00003214 state->offset = 0;
Eric Dumazet4f70ecc2010-05-03 10:50:14 +00003215 hlist_for_each_entry_rcu_bh(ifa, n,
stephen hemmingerc2e21292010-03-17 20:31:10 +00003216 &inet6_addr_lst[state->bucket], addr_lst) {
Eric Dumazetdd2c50e2012-10-16 07:37:27 +00003217 if (!net_eq(dev_net(ifa->idev->dev), net))
3218 continue;
Mihai Maruseac1d578302012-01-03 23:31:35 +00003219 state->offset++;
Eric Dumazetdd2c50e2012-10-16 07:37:27 +00003220 return ifa;
Daniel Lezcano3c400902008-01-10 22:42:49 -08003221 }
3222 }
3223
stephen hemmingerc2e21292010-03-17 20:31:10 +00003224 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225}
3226
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
Eric Dumazet4f70ecc2010-05-03 10:50:14 +00003228 __acquires(rcu_bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229{
stephen hemminger5c578ae2010-03-17 20:31:11 +00003230 rcu_read_lock_bh();
Mihai Maruseac1d578302012-01-03 23:31:35 +00003231 return if6_get_first(seq, *pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232}
3233
3234static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3235{
3236 struct inet6_ifaddr *ifa;
3237
3238 ifa = if6_get_next(seq, v);
3239 ++*pos;
3240 return ifa;
3241}
3242
3243static void if6_seq_stop(struct seq_file *seq, void *v)
Eric Dumazet4f70ecc2010-05-03 10:50:14 +00003244 __releases(rcu_bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245{
stephen hemminger5c578ae2010-03-17 20:31:11 +00003246 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247}
3248
3249static int if6_seq_show(struct seq_file *seq, void *v)
3250{
3251 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
Harvey Harrison4b7a4272008-10-29 12:50:24 -07003252 seq_printf(seq, "%pi6 %02x %02x %02x %02x %8s\n",
Harvey Harrisonb0711952008-10-28 16:05:40 -07003253 &ifp->addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 ifp->idev->dev->ifindex,
3255 ifp->prefix_len,
3256 ifp->scope,
3257 ifp->flags,
3258 ifp->idev->dev->name);
3259 return 0;
3260}
3261
Philippe De Muyter56b3d972007-07-10 23:07:31 -07003262static const struct seq_operations if6_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 .start = if6_seq_start,
3264 .next = if6_seq_next,
3265 .show = if6_seq_show,
3266 .stop = if6_seq_stop,
3267};
3268
3269static int if6_seq_open(struct inode *inode, struct file *file)
3270{
Daniel Lezcano3c400902008-01-10 22:42:49 -08003271 return seq_open_net(inode, file, &if6_seq_ops,
3272 sizeof(struct if6_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273}
3274
Arjan van de Ven9a321442007-02-12 00:55:35 -08003275static const struct file_operations if6_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 .owner = THIS_MODULE,
3277 .open = if6_seq_open,
3278 .read = seq_read,
3279 .llseek = seq_lseek,
Daniel Lezcano3c400902008-01-10 22:42:49 -08003280 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281};
3282
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003283static int __net_init if6_proc_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284{
Daniel Lezcano3c400902008-01-10 22:42:49 -08003285 if (!proc_net_fops_create(net, "if_inet6", S_IRUGO, &if6_fops))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286 return -ENOMEM;
3287 return 0;
3288}
3289
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003290static void __net_exit if6_proc_net_exit(struct net *net)
Daniel Lezcano3c400902008-01-10 22:42:49 -08003291{
3292 proc_net_remove(net, "if_inet6");
3293}
3294
3295static struct pernet_operations if6_proc_net_ops = {
3296 .init = if6_proc_net_init,
3297 .exit = if6_proc_net_exit,
3298};
3299
3300int __init if6_proc_init(void)
3301{
3302 return register_pernet_subsys(&if6_proc_net_ops);
3303}
3304
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305void if6_proc_exit(void)
3306{
Daniel Lezcano3c400902008-01-10 22:42:49 -08003307 unregister_pernet_subsys(&if6_proc_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308}
3309#endif /* CONFIG_PROC_FS */
3310
Masahide NAKAMURA59fbb3a2007-06-26 23:56:32 -07003311#if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07003312/* Check if address is a home address configured on any interface. */
Eric Dumazetb71d1d42011-04-22 04:53:02 +00003313int ipv6_chk_home_addr(struct net *net, const struct in6_addr *addr)
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07003314{
3315 int ret = 0;
stephen hemmingerc2e21292010-03-17 20:31:10 +00003316 struct inet6_ifaddr *ifp = NULL;
3317 struct hlist_node *n;
stephen hemminger3a88a812010-03-17 20:31:12 +00003318 unsigned int hash = ipv6_addr_hash(addr);
stephen hemmingerc2e21292010-03-17 20:31:10 +00003319
stephen hemminger5c578ae2010-03-17 20:31:11 +00003320 rcu_read_lock_bh();
Eric Dumazet4f70ecc2010-05-03 10:50:14 +00003321 hlist_for_each_entry_rcu_bh(ifp, n, &inet6_addr_lst[hash], addr_lst) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09003322 if (!net_eq(dev_net(ifp->idev->dev), net))
Daniel Lezcano389f6612008-01-10 22:44:40 -08003323 continue;
YOSHIFUJI Hideakicaad2952008-04-10 15:42:07 +09003324 if (ipv6_addr_equal(&ifp->addr, addr) &&
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07003325 (ifp->flags & IFA_F_HOMEADDRESS)) {
3326 ret = 1;
3327 break;
3328 }
3329 }
stephen hemminger5c578ae2010-03-17 20:31:11 +00003330 rcu_read_unlock_bh();
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07003331 return ret;
3332}
3333#endif
3334
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335/*
3336 * Periodic address status verification
3337 */
3338
3339static void addrconf_verify(unsigned long foo)
3340{
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -07003341 unsigned long now, next, next_sec, next_sched;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342 struct inet6_ifaddr *ifp;
stephen hemmingerc2e21292010-03-17 20:31:10 +00003343 struct hlist_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 int i;
3345
stephen hemminger5c578ae2010-03-17 20:31:11 +00003346 rcu_read_lock_bh();
3347 spin_lock(&addrconf_verify_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348 now = jiffies;
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -07003349 next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350
3351 del_timer(&addr_chk_timer);
3352
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003353 for (i = 0; i < IN6_ADDR_HSIZE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354restart:
Eric Dumazet4f70ecc2010-05-03 10:50:14 +00003355 hlist_for_each_entry_rcu_bh(ifp, node,
stephen hemminger5c578ae2010-03-17 20:31:11 +00003356 &inet6_addr_lst[i], addr_lst) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 unsigned long age;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358
3359 if (ifp->flags & IFA_F_PERMANENT)
3360 continue;
3361
3362 spin_lock(&ifp->lock);
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -07003363 /* We try to batch several events at once. */
3364 age = (now - ifp->tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365
YOSHIFUJI Hideaki8f27ebb2006-07-28 18:12:11 +09003366 if (ifp->valid_lft != INFINITY_LIFE_TIME &&
3367 age >= ifp->valid_lft) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368 spin_unlock(&ifp->lock);
3369 in6_ifa_hold(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370 ipv6_del_addr(ifp);
3371 goto restart;
YOSHIFUJI Hideaki8f27ebb2006-07-28 18:12:11 +09003372 } else if (ifp->prefered_lft == INFINITY_LIFE_TIME) {
3373 spin_unlock(&ifp->lock);
3374 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 } else if (age >= ifp->prefered_lft) {
Brian Haleya1ed0522009-07-02 07:10:52 +00003376 /* jiffies - ifp->tstamp > age >= ifp->prefered_lft */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 int deprecate = 0;
3378
3379 if (!(ifp->flags&IFA_F_DEPRECATED)) {
3380 deprecate = 1;
3381 ifp->flags |= IFA_F_DEPRECATED;
3382 }
3383
3384 if (time_before(ifp->tstamp + ifp->valid_lft * HZ, next))
3385 next = ifp->tstamp + ifp->valid_lft * HZ;
3386
3387 spin_unlock(&ifp->lock);
3388
3389 if (deprecate) {
3390 in6_ifa_hold(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391
3392 ipv6_ifa_notify(0, ifp);
3393 in6_ifa_put(ifp);
3394 goto restart;
3395 }
3396#ifdef CONFIG_IPV6_PRIVACY
3397 } else if ((ifp->flags&IFA_F_TEMPORARY) &&
3398 !(ifp->flags&IFA_F_TENTATIVE)) {
stephen hemminger88949cf2010-03-17 20:31:17 +00003399 unsigned long regen_advance = ifp->idev->cnf.regen_max_retry *
3400 ifp->idev->cnf.dad_transmits *
3401 ifp->idev->nd_parms->retrans_time / HZ;
3402
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403 if (age >= ifp->prefered_lft - regen_advance) {
3404 struct inet6_ifaddr *ifpub = ifp->ifpub;
3405 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
3406 next = ifp->tstamp + ifp->prefered_lft * HZ;
3407 if (!ifp->regen_count && ifpub) {
3408 ifp->regen_count++;
3409 in6_ifa_hold(ifp);
3410 in6_ifa_hold(ifpub);
3411 spin_unlock(&ifp->lock);
stephen hemminger5c578ae2010-03-17 20:31:11 +00003412
Hiroyuki YAMAMORI291d8092005-12-23 11:24:05 -08003413 spin_lock(&ifpub->lock);
3414 ifpub->regen_count = 0;
3415 spin_unlock(&ifpub->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416 ipv6_create_tempaddr(ifpub, ifp);
3417 in6_ifa_put(ifpub);
3418 in6_ifa_put(ifp);
3419 goto restart;
3420 }
3421 } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
3422 next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
3423 spin_unlock(&ifp->lock);
3424#endif
3425 } else {
3426 /* ifp->prefered_lft <= ifp->valid_lft */
3427 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
3428 next = ifp->tstamp + ifp->prefered_lft * HZ;
3429 spin_unlock(&ifp->lock);
3430 }
3431 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432 }
3433
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -07003434 next_sec = round_jiffies_up(next);
3435 next_sched = next;
3436
3437 /* If rounded timeout is accurate enough, accept it. */
3438 if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ))
3439 next_sched = next_sec;
3440
3441 /* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */
3442 if (time_before(next_sched, jiffies + ADDRCONF_TIMER_FUZZ_MAX))
3443 next_sched = jiffies + ADDRCONF_TIMER_FUZZ_MAX;
3444
3445 ADBG((KERN_DEBUG "now = %lu, schedule = %lu, rounded schedule = %lu => %lu\n",
3446 now, next, next_sec, next_sched));
3447
3448 addr_chk_timer.expires = next_sched;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449 add_timer(&addr_chk_timer);
stephen hemminger5c578ae2010-03-17 20:31:11 +00003450 spin_unlock(&addrconf_verify_lock);
3451 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452}
3453
Thomas Graf461d8832006-09-18 00:09:49 -07003454static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local)
3455{
3456 struct in6_addr *pfx = NULL;
3457
3458 if (addr)
3459 pfx = nla_data(addr);
3460
3461 if (local) {
3462 if (pfx && nla_memcmp(local, pfx, sizeof(*pfx)))
3463 pfx = NULL;
3464 else
3465 pfx = nla_data(local);
3466 }
3467
3468 return pfx;
3469}
3470
Patrick McHardyef7c79e2007-06-05 12:38:30 -07003471static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = {
Thomas Graf461d8832006-09-18 00:09:49 -07003472 [IFA_ADDRESS] = { .len = sizeof(struct in6_addr) },
3473 [IFA_LOCAL] = { .len = sizeof(struct in6_addr) },
3474 [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) },
3475};
3476
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477static int
3478inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
3479{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09003480 struct net *net = sock_net(skb->sk);
Thomas Grafb933f712006-09-18 00:10:19 -07003481 struct ifaddrmsg *ifm;
3482 struct nlattr *tb[IFA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483 struct in6_addr *pfx;
Thomas Grafb933f712006-09-18 00:10:19 -07003484 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485
Thomas Grafb933f712006-09-18 00:10:19 -07003486 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
3487 if (err < 0)
3488 return err;
3489
3490 ifm = nlmsg_data(nlh);
3491 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492 if (pfx == NULL)
3493 return -EINVAL;
3494
Daniel Lezcanoaf284932008-03-05 10:46:57 -08003495 return inet6_addr_del(net, ifm->ifa_index, pfx, ifm->ifa_prefixlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496}
3497
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003498static int inet6_addr_modify(struct inet6_ifaddr *ifp, u8 ifa_flags,
3499 u32 prefered_lft, u32 valid_lft)
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09003500{
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07003501 u32 flags;
3502 clock_t expires;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09003503 unsigned long timeout;
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09003504
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09003505 if (!valid_lft || (prefered_lft > valid_lft))
3506 return -EINVAL;
3507
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09003508 timeout = addrconf_timeout_fixup(valid_lft, HZ);
3509 if (addrconf_finite_timeout(timeout)) {
3510 expires = jiffies_to_clock_t(timeout * HZ);
3511 valid_lft = timeout;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07003512 flags = RTF_EXPIRES;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09003513 } else {
3514 expires = 0;
3515 flags = 0;
3516 ifa_flags |= IFA_F_PERMANENT;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07003517 }
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09003518
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09003519 timeout = addrconf_timeout_fixup(prefered_lft, HZ);
3520 if (addrconf_finite_timeout(timeout)) {
3521 if (timeout == 0)
3522 ifa_flags |= IFA_F_DEPRECATED;
3523 prefered_lft = timeout;
3524 }
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09003525
3526 spin_lock_bh(&ifp->lock);
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07003527 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 +09003528 ifp->tstamp = jiffies;
3529 ifp->valid_lft = valid_lft;
3530 ifp->prefered_lft = prefered_lft;
3531
3532 spin_unlock_bh(&ifp->lock);
3533 if (!(ifp->flags&IFA_F_TENTATIVE))
3534 ipv6_ifa_notify(0, ifp);
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09003535
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09003536 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, ifp->idev->dev,
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07003537 expires, flags);
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09003538 addrconf_verify(0);
3539
3540 return 0;
3541}
3542
3543static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
3545{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09003546 struct net *net = sock_net(skb->sk);
Thomas Graf461d8832006-09-18 00:09:49 -07003547 struct ifaddrmsg *ifm;
3548 struct nlattr *tb[IFA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549 struct in6_addr *pfx;
Thomas Graf7198f8c2006-09-18 00:13:46 -07003550 struct inet6_ifaddr *ifa;
3551 struct net_device *dev;
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003552 u32 valid_lft = INFINITY_LIFE_TIME, preferred_lft = INFINITY_LIFE_TIME;
3553 u8 ifa_flags;
Thomas Graf461d8832006-09-18 00:09:49 -07003554 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555
Thomas Graf461d8832006-09-18 00:09:49 -07003556 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
3557 if (err < 0)
3558 return err;
3559
3560 ifm = nlmsg_data(nlh);
3561 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562 if (pfx == NULL)
3563 return -EINVAL;
3564
Thomas Graf461d8832006-09-18 00:09:49 -07003565 if (tb[IFA_CACHEINFO]) {
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09003566 struct ifa_cacheinfo *ci;
Thomas Graf461d8832006-09-18 00:09:49 -07003567
3568 ci = nla_data(tb[IFA_CACHEINFO]);
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09003569 valid_lft = ci->ifa_valid;
Thomas Graf461d8832006-09-18 00:09:49 -07003570 preferred_lft = ci->ifa_prefered;
3571 } else {
3572 preferred_lft = INFINITY_LIFE_TIME;
3573 valid_lft = INFINITY_LIFE_TIME;
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09003574 }
3575
Daniel Lezcanoaf284932008-03-05 10:46:57 -08003576 dev = __dev_get_by_index(net, ifm->ifa_index);
Thomas Graf7198f8c2006-09-18 00:13:46 -07003577 if (dev == NULL)
3578 return -ENODEV;
3579
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003580 /* We ignore other flags so far. */
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07003581 ifa_flags = ifm->ifa_flags & (IFA_F_NODAD | IFA_F_HOMEADDRESS);
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003582
Daniel Lezcano1cab3da2008-01-10 22:44:09 -08003583 ifa = ipv6_get_ifaddr(net, pfx, dev, 1);
Thomas Graf7198f8c2006-09-18 00:13:46 -07003584 if (ifa == NULL) {
3585 /*
3586 * It would be best to check for !NLM_F_CREATE here but
3587 * userspace alreay relies on not having to provide this.
3588 */
Daniel Lezcanoaf284932008-03-05 10:46:57 -08003589 return inet6_addr_add(net, ifm->ifa_index, pfx,
3590 ifm->ifa_prefixlen, ifa_flags,
3591 preferred_lft, valid_lft);
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09003592 }
3593
Thomas Graf7198f8c2006-09-18 00:13:46 -07003594 if (nlh->nlmsg_flags & NLM_F_EXCL ||
3595 !(nlh->nlmsg_flags & NLM_F_REPLACE))
3596 err = -EEXIST;
3597 else
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003598 err = inet6_addr_modify(ifa, ifa_flags, preferred_lft, valid_lft);
Thomas Graf7198f8c2006-09-18 00:13:46 -07003599
3600 in6_ifa_put(ifa);
3601
3602 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603}
3604
Thomas Graf101bb222006-09-18 00:11:52 -07003605static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u8 flags,
3606 u8 scope, int ifindex)
3607{
3608 struct ifaddrmsg *ifm;
3609
3610 ifm = nlmsg_data(nlh);
3611 ifm->ifa_family = AF_INET6;
3612 ifm->ifa_prefixlen = prefixlen;
3613 ifm->ifa_flags = flags;
3614 ifm->ifa_scope = scope;
3615 ifm->ifa_index = ifindex;
3616}
3617
Thomas Graf85486af2006-09-18 00:11:24 -07003618static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
3619 unsigned long tstamp, u32 preferred, u32 valid)
3620{
3621 struct ifa_cacheinfo ci;
3622
Thomas Graf18a31e12010-11-17 04:12:02 +00003623 ci.cstamp = cstamp_delta(cstamp);
3624 ci.tstamp = cstamp_delta(tstamp);
Thomas Graf85486af2006-09-18 00:11:24 -07003625 ci.ifa_prefered = preferred;
3626 ci.ifa_valid = valid;
3627
3628 return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
3629}
3630
Thomas Graf101bb222006-09-18 00:11:52 -07003631static inline int rt_scope(int ifa_scope)
3632{
3633 if (ifa_scope & IFA_HOST)
3634 return RT_SCOPE_HOST;
3635 else if (ifa_scope & IFA_LINK)
3636 return RT_SCOPE_LINK;
3637 else if (ifa_scope & IFA_SITE)
3638 return RT_SCOPE_SITE;
3639 else
3640 return RT_SCOPE_UNIVERSE;
3641}
3642
Thomas Graf0ab68032006-09-18 00:12:35 -07003643static inline int inet6_ifaddr_msgsize(void)
3644{
Thomas Graf339bf982006-11-10 14:10:15 -08003645 return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
3646 + nla_total_size(16) /* IFA_ADDRESS */
3647 + nla_total_size(sizeof(struct ifa_cacheinfo));
Thomas Graf0ab68032006-09-18 00:12:35 -07003648}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07003649
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07003651 u32 pid, u32 seq, int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653 struct nlmsghdr *nlh;
Thomas Graf85486af2006-09-18 00:11:24 -07003654 u32 preferred, valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655
Thomas Graf0ab68032006-09-18 00:12:35 -07003656 nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags);
3657 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08003658 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07003659
Thomas Graf101bb222006-09-18 00:11:52 -07003660 put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope),
3661 ifa->idev->dev->ifindex);
3662
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663 if (!(ifa->flags&IFA_F_PERMANENT)) {
Thomas Graf85486af2006-09-18 00:11:24 -07003664 preferred = ifa->prefered_lft;
3665 valid = ifa->valid_lft;
3666 if (preferred != INFINITY_LIFE_TIME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667 long tval = (jiffies - ifa->tstamp)/HZ;
Jens Rosenbooma1faa692009-06-25 04:55:50 +00003668 if (preferred > tval)
3669 preferred -= tval;
3670 else
3671 preferred = 0;
Ben Hutchingsf56619f2010-06-26 11:37:47 +00003672 if (valid != INFINITY_LIFE_TIME) {
3673 if (valid > tval)
3674 valid -= tval;
3675 else
3676 valid = 0;
3677 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678 }
3679 } else {
Thomas Graf85486af2006-09-18 00:11:24 -07003680 preferred = INFINITY_LIFE_TIME;
3681 valid = INFINITY_LIFE_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682 }
Thomas Graf85486af2006-09-18 00:11:24 -07003683
Thomas Graf0ab68032006-09-18 00:12:35 -07003684 if (nla_put(skb, IFA_ADDRESS, 16, &ifa->addr) < 0 ||
Patrick McHardy26932562007-01-31 23:16:40 -08003685 put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0) {
3686 nlmsg_cancel(skb, nlh);
3687 return -EMSGSIZE;
3688 }
Thomas Graf85486af2006-09-18 00:11:24 -07003689
Thomas Graf0ab68032006-09-18 00:12:35 -07003690 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691}
3692
3693static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -07003694 u32 pid, u32 seq, int event, u16 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696 struct nlmsghdr *nlh;
Thomas Graf101bb222006-09-18 00:11:52 -07003697 u8 scope = RT_SCOPE_UNIVERSE;
3698 int ifindex = ifmca->idev->dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699
Thomas Graf101bb222006-09-18 00:11:52 -07003700 if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE)
3701 scope = RT_SCOPE_SITE;
3702
Thomas Graf0ab68032006-09-18 00:12:35 -07003703 nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags);
3704 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08003705 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07003706
Thomas Graf101bb222006-09-18 00:11:52 -07003707 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
Thomas Graf0ab68032006-09-18 00:12:35 -07003708 if (nla_put(skb, IFA_MULTICAST, 16, &ifmca->mca_addr) < 0 ||
3709 put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp,
Patrick McHardy26932562007-01-31 23:16:40 -08003710 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
3711 nlmsg_cancel(skb, nlh);
3712 return -EMSGSIZE;
3713 }
Thomas Graf85486af2006-09-18 00:11:24 -07003714
Thomas Graf0ab68032006-09-18 00:12:35 -07003715 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716}
3717
3718static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07003719 u32 pid, u32 seq, int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721 struct nlmsghdr *nlh;
Thomas Graf101bb222006-09-18 00:11:52 -07003722 u8 scope = RT_SCOPE_UNIVERSE;
3723 int ifindex = ifaca->aca_idev->dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724
Thomas Graf101bb222006-09-18 00:11:52 -07003725 if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE)
3726 scope = RT_SCOPE_SITE;
3727
Thomas Graf0ab68032006-09-18 00:12:35 -07003728 nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags);
3729 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08003730 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07003731
Thomas Graf101bb222006-09-18 00:11:52 -07003732 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
Thomas Graf0ab68032006-09-18 00:12:35 -07003733 if (nla_put(skb, IFA_ANYCAST, 16, &ifaca->aca_addr) < 0 ||
3734 put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp,
Patrick McHardy26932562007-01-31 23:16:40 -08003735 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
3736 nlmsg_cancel(skb, nlh);
3737 return -EMSGSIZE;
3738 }
Thomas Graf85486af2006-09-18 00:11:24 -07003739
Thomas Graf0ab68032006-09-18 00:12:35 -07003740 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741}
3742
Stephen Hemmingere21e8462010-03-20 16:09:01 -07003743enum addr_type_t {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744 UNICAST_ADDR,
3745 MULTICAST_ADDR,
3746 ANYCAST_ADDR,
3747};
3748
Eric Dumazet234b27c2009-11-12 04:11:50 +00003749/* called with rcu_read_lock() */
3750static int in6_dump_addrs(struct inet6_dev *idev, struct sk_buff *skb,
3751 struct netlink_callback *cb, enum addr_type_t type,
3752 int s_ip_idx, int *p_ip_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754 struct ifmcaddr6 *ifmca;
3755 struct ifacaddr6 *ifaca;
Eric Dumazet234b27c2009-11-12 04:11:50 +00003756 int err = 1;
3757 int ip_idx = *p_ip_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758
Eric Dumazet234b27c2009-11-12 04:11:50 +00003759 read_lock_bh(&idev->lock);
3760 switch (type) {
stephen hemminger502a2ff2010-03-17 20:31:13 +00003761 case UNICAST_ADDR: {
3762 struct inet6_ifaddr *ifa;
3763
Eric Dumazet234b27c2009-11-12 04:11:50 +00003764 /* unicast address incl. temp addr */
stephen hemminger502a2ff2010-03-17 20:31:13 +00003765 list_for_each_entry(ifa, &idev->addr_list, if_list) {
3766 if (++ip_idx < s_ip_idx)
Eric Dumazet234b27c2009-11-12 04:11:50 +00003767 continue;
3768 err = inet6_fill_ifaddr(skb, ifa,
3769 NETLINK_CB(cb->skb).pid,
3770 cb->nlh->nlmsg_seq,
3771 RTM_NEWADDR,
3772 NLM_F_MULTI);
3773 if (err <= 0)
3774 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775 }
Eric Dumazet234b27c2009-11-12 04:11:50 +00003776 break;
stephen hemminger502a2ff2010-03-17 20:31:13 +00003777 }
Eric Dumazet234b27c2009-11-12 04:11:50 +00003778 case MULTICAST_ADDR:
3779 /* multicast address */
3780 for (ifmca = idev->mc_list; ifmca;
3781 ifmca = ifmca->next, ip_idx++) {
3782 if (ip_idx < s_ip_idx)
3783 continue;
3784 err = inet6_fill_ifmcaddr(skb, ifmca,
3785 NETLINK_CB(cb->skb).pid,
3786 cb->nlh->nlmsg_seq,
3787 RTM_GETMULTICAST,
3788 NLM_F_MULTI);
3789 if (err <= 0)
3790 break;
3791 }
3792 break;
3793 case ANYCAST_ADDR:
3794 /* anycast address */
3795 for (ifaca = idev->ac_list; ifaca;
3796 ifaca = ifaca->aca_next, ip_idx++) {
3797 if (ip_idx < s_ip_idx)
3798 continue;
3799 err = inet6_fill_ifacaddr(skb, ifaca,
3800 NETLINK_CB(cb->skb).pid,
3801 cb->nlh->nlmsg_seq,
3802 RTM_GETANYCAST,
3803 NLM_F_MULTI);
3804 if (err <= 0)
3805 break;
3806 }
3807 break;
3808 default:
3809 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810 }
Eric Dumazet234b27c2009-11-12 04:11:50 +00003811 read_unlock_bh(&idev->lock);
3812 *p_ip_idx = ip_idx;
3813 return err;
3814}
3815
3816static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
3817 enum addr_type_t type)
3818{
3819 struct net *net = sock_net(skb->sk);
3820 int h, s_h;
3821 int idx, ip_idx;
3822 int s_idx, s_ip_idx;
3823 struct net_device *dev;
3824 struct inet6_dev *idev;
3825 struct hlist_head *head;
3826 struct hlist_node *node;
3827
3828 s_h = cb->args[0];
3829 s_idx = idx = cb->args[1];
3830 s_ip_idx = ip_idx = cb->args[2];
3831
3832 rcu_read_lock();
3833 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
3834 idx = 0;
3835 head = &net->dev_index_head[h];
3836 hlist_for_each_entry_rcu(dev, node, head, index_hlist) {
3837 if (idx < s_idx)
3838 goto cont;
Patrick McHardy4b97efd2010-03-26 20:27:49 -07003839 if (h > s_h || idx > s_idx)
Eric Dumazet234b27c2009-11-12 04:11:50 +00003840 s_ip_idx = 0;
3841 ip_idx = 0;
Stephen Hemmingere21e8462010-03-20 16:09:01 -07003842 idev = __in6_dev_get(dev);
3843 if (!idev)
Eric Dumazet234b27c2009-11-12 04:11:50 +00003844 goto cont;
3845
3846 if (in6_dump_addrs(idev, skb, cb, type,
3847 s_ip_idx, &ip_idx) <= 0)
3848 goto done;
3849cont:
3850 idx++;
3851 }
3852 }
3853done:
3854 rcu_read_unlock();
3855 cb->args[0] = h;
3856 cb->args[1] = idx;
3857 cb->args[2] = ip_idx;
3858
Linus Torvalds1da177e2005-04-16 15:20:36 -07003859 return skb->len;
3860}
3861
3862static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
3863{
3864 enum addr_type_t type = UNICAST_ADDR;
Denis V. Lunevb8542722007-12-01 00:21:31 +11003865
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866 return inet6_dump_addr(skb, cb, type);
3867}
3868
3869static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
3870{
3871 enum addr_type_t type = MULTICAST_ADDR;
Denis V. Lunevb8542722007-12-01 00:21:31 +11003872
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873 return inet6_dump_addr(skb, cb, type);
3874}
3875
3876
3877static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
3878{
3879 enum addr_type_t type = ANYCAST_ADDR;
Denis V. Lunevb8542722007-12-01 00:21:31 +11003880
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881 return inet6_dump_addr(skb, cb, type);
3882}
3883
Thomas Graf1b29fc22006-09-18 00:10:50 -07003884static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr* nlh,
3885 void *arg)
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003886{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09003887 struct net *net = sock_net(in_skb->sk);
Thomas Graf1b29fc22006-09-18 00:10:50 -07003888 struct ifaddrmsg *ifm;
3889 struct nlattr *tb[IFA_MAX+1];
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003890 struct in6_addr *addr = NULL;
3891 struct net_device *dev = NULL;
3892 struct inet6_ifaddr *ifa;
3893 struct sk_buff *skb;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003894 int err;
3895
Thomas Graf1b29fc22006-09-18 00:10:50 -07003896 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
3897 if (err < 0)
3898 goto errout;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003899
Thomas Graf1b29fc22006-09-18 00:10:50 -07003900 addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
3901 if (addr == NULL) {
3902 err = -EINVAL;
3903 goto errout;
3904 }
3905
3906 ifm = nlmsg_data(nlh);
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003907 if (ifm->ifa_index)
Benjamin Thery6fda7352008-03-05 10:47:47 -08003908 dev = __dev_get_by_index(net, ifm->ifa_index);
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003909
Stephen Hemmingere21e8462010-03-20 16:09:01 -07003910 ifa = ipv6_get_ifaddr(net, addr, dev, 1);
3911 if (!ifa) {
Thomas Graf1b29fc22006-09-18 00:10:50 -07003912 err = -EADDRNOTAVAIL;
3913 goto errout;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003914 }
3915
Stephen Hemmingere21e8462010-03-20 16:09:01 -07003916 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL);
3917 if (!skb) {
Thomas Graf1b29fc22006-09-18 00:10:50 -07003918 err = -ENOBUFS;
3919 goto errout_ifa;
3920 }
3921
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003922 err = inet6_fill_ifaddr(skb, ifa, NETLINK_CB(in_skb).pid,
3923 nlh->nlmsg_seq, RTM_NEWADDR, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08003924 if (err < 0) {
3925 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
3926 WARN_ON(err == -EMSGSIZE);
3927 kfree_skb(skb);
3928 goto errout_ifa;
3929 }
Benjamin Thery6fda7352008-03-05 10:47:47 -08003930 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).pid);
Thomas Graf1b29fc22006-09-18 00:10:50 -07003931errout_ifa:
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003932 in6_ifa_put(ifa);
Thomas Graf1b29fc22006-09-18 00:10:50 -07003933errout:
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003934 return err;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003935}
3936
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
3938{
3939 struct sk_buff *skb;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09003940 struct net *net = dev_net(ifa->idev->dev);
Thomas Graf5d620262006-08-15 00:35:02 -07003941 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942
Thomas Graf0ab68032006-09-18 00:12:35 -07003943 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC);
Thomas Graf5d620262006-08-15 00:35:02 -07003944 if (skb == NULL)
3945 goto errout;
3946
3947 err = inet6_fill_ifaddr(skb, ifa, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08003948 if (err < 0) {
3949 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
3950 WARN_ON(err == -EMSGSIZE);
3951 kfree_skb(skb);
3952 goto errout;
3953 }
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08003954 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
3955 return;
Thomas Graf5d620262006-08-15 00:35:02 -07003956errout:
3957 if (err < 0)
Benjamin Thery6fda7352008-03-05 10:47:47 -08003958 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959}
3960
Dave Jonesb6f99a22007-03-22 12:27:49 -07003961static inline void ipv6_store_devconf(struct ipv6_devconf *cnf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962 __s32 *array, int bytes)
3963{
Thomas Graf04561c12006-11-14 19:53:58 -08003964 BUG_ON(bytes < (DEVCONF_MAX * 4));
3965
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966 memset(array, 0, bytes);
3967 array[DEVCONF_FORWARDING] = cnf->forwarding;
3968 array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
3969 array[DEVCONF_MTU6] = cnf->mtu6;
3970 array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
3971 array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
3972 array[DEVCONF_AUTOCONF] = cnf->autoconf;
3973 array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
3974 array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
Thomas Graf93908d12010-11-17 01:44:24 +00003975 array[DEVCONF_RTR_SOLICIT_INTERVAL] =
3976 jiffies_to_msecs(cnf->rtr_solicit_interval);
3977 array[DEVCONF_RTR_SOLICIT_DELAY] =
3978 jiffies_to_msecs(cnf->rtr_solicit_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979 array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
3980#ifdef CONFIG_IPV6_PRIVACY
3981 array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
3982 array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
3983 array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
3984 array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
3985 array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
3986#endif
3987 array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -08003988 array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr;
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -08003989 array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo;
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08003990#ifdef CONFIG_IPV6_ROUTER_PREF
3991 array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref;
Thomas Graf93908d12010-11-17 01:44:24 +00003992 array[DEVCONF_RTR_PROBE_INTERVAL] =
3993 jiffies_to_msecs(cnf->rtr_probe_interval);
Neil Hormanfa03ef32007-01-30 14:30:10 -08003994#ifdef CONFIG_IPV6_ROUTE_INFO
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -08003995 array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen;
3996#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08003997#endif
Lorenzo Colitti653be182014-03-26 19:35:41 +09003998 array[DEVCONF_ACCEPT_RA_RT_TABLE] = cnf->accept_ra_rt_table;
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -07003999 array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp;
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -07004000 array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route;
Neil Horman95c385b2007-04-25 17:08:10 -07004001#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
4002 array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad;
4003#endif
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09004004#ifdef CONFIG_IPV6_MROUTE
4005 array[DEVCONF_MC_FORWARDING] = cnf->mc_forwarding;
4006#endif
YOSHIFUJI Hideaki778d80b2008-06-28 14:17:11 +09004007 array[DEVCONF_DISABLE_IPV6] = cnf->disable_ipv6;
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09004008 array[DEVCONF_ACCEPT_DAD] = cnf->accept_dad;
Cosmin Ratiuc3faca02009-10-09 03:11:14 +00004009 array[DEVCONF_FORCE_TLLAO] = cnf->force_tllao;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010}
4011
Thomas Grafb382b192010-11-16 04:33:57 +00004012static inline size_t inet6_ifla6_size(void)
4013{
4014 return nla_total_size(4) /* IFLA_INET6_FLAGS */
4015 + nla_total_size(sizeof(struct ifla_cacheinfo))
4016 + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */
4017 + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */
4018 + nla_total_size(ICMP6_MIB_MAX * 8); /* IFLA_INET6_ICMP6STATS */
4019}
4020
Thomas Graf339bf982006-11-10 14:10:15 -08004021static inline size_t inet6_if_nlmsg_size(void)
4022{
4023 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
4024 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
4025 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
4026 + nla_total_size(4) /* IFLA_MTU */
4027 + nla_total_size(4) /* IFLA_LINK */
Thomas Grafb382b192010-11-16 04:33:57 +00004028 + nla_total_size(inet6_ifla6_size()); /* IFLA_PROTINFO */
Thomas Graf339bf982006-11-10 14:10:15 -08004029}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07004030
Eric Dumazetbe281e52011-05-19 01:14:23 +00004031static inline void __snmp6_fill_statsdev(u64 *stats, atomic_long_t *mib,
Tejun Heo7d720c32010-02-16 15:20:26 +00004032 int items, int bytes)
Herbert Xu7f7d9a62007-04-24 21:54:09 -07004033{
4034 int i;
4035 int pad = bytes - sizeof(u64) * items;
4036 BUG_ON(pad < 0);
4037
4038 /* Use put_unaligned() because stats may not be aligned for u64. */
4039 put_unaligned(items, &stats[0]);
4040 for (i = 1; i < items; i++)
Eric Dumazetbe281e52011-05-19 01:14:23 +00004041 put_unaligned(atomic_long_read(&mib[i]), &stats[i]);
Herbert Xu7f7d9a62007-04-24 21:54:09 -07004042
4043 memset(&stats[items], 0, pad);
4044}
4045
Eric Dumazet4ce3c182010-06-30 13:31:19 -07004046static inline void __snmp6_fill_stats64(u64 *stats, void __percpu **mib,
4047 int items, int bytes, size_t syncpoff)
4048{
4049 int i;
4050 int pad = bytes - sizeof(u64) * items;
4051 BUG_ON(pad < 0);
4052
4053 /* Use put_unaligned() because stats may not be aligned for u64. */
4054 put_unaligned(items, &stats[0]);
4055 for (i = 1; i < items; i++)
4056 put_unaligned(snmp_fold_field64(mib, i, syncpoff), &stats[i]);
4057
4058 memset(&stats[items], 0, pad);
4059}
4060
Herbert Xu7f7d9a62007-04-24 21:54:09 -07004061static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype,
4062 int bytes)
4063{
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004064 switch (attrtype) {
Herbert Xu7f7d9a62007-04-24 21:54:09 -07004065 case IFLA_INET6_STATS:
Eric Dumazet4ce3c182010-06-30 13:31:19 -07004066 __snmp6_fill_stats64(stats, (void __percpu **)idev->stats.ipv6,
4067 IPSTATS_MIB_MAX, bytes, offsetof(struct ipstats_mib, syncp));
Herbert Xu7f7d9a62007-04-24 21:54:09 -07004068 break;
4069 case IFLA_INET6_ICMP6STATS:
Eric Dumazetbe281e52011-05-19 01:14:23 +00004070 __snmp6_fill_statsdev(stats, idev->stats.icmpv6dev->mibs, ICMP6_MIB_MAX, bytes);
Herbert Xu7f7d9a62007-04-24 21:54:09 -07004071 break;
4072 }
4073}
4074
Thomas Grafb382b192010-11-16 04:33:57 +00004075static int inet6_fill_ifla6_attrs(struct sk_buff *skb, struct inet6_dev *idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076{
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07004077 struct nlattr *nla;
Thomas Graf04561c12006-11-14 19:53:58 -08004078 struct ifla_cacheinfo ci;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079
Thomas Graf04561c12006-11-14 19:53:58 -08004080 NLA_PUT_U32(skb, IFLA_INET6_FLAGS, idev->if_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081
Linus Torvalds1da177e2005-04-16 15:20:36 -07004082 ci.max_reasm_len = IPV6_MAXPLEN;
David S. Miller24912422010-11-19 13:13:47 -08004083 ci.tstamp = cstamp_delta(idev->tstamp);
4084 ci.reachable_time = jiffies_to_msecs(idev->nd_parms->reachable_time);
4085 ci.retrans_time = jiffies_to_msecs(idev->nd_parms->retrans_time);
Thomas Graf04561c12006-11-14 19:53:58 -08004086 NLA_PUT(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci);
4087
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07004088 nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32));
4089 if (nla == NULL)
Thomas Graf04561c12006-11-14 19:53:58 -08004090 goto nla_put_failure;
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07004091 ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07004093 /* XXX - MC not implemented */
4094
4095 nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64));
4096 if (nla == NULL)
4097 goto nla_put_failure;
4098 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla));
4099
4100 nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64));
4101 if (nla == NULL)
4102 goto nla_put_failure;
4103 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104
Thomas Grafb382b192010-11-16 04:33:57 +00004105 return 0;
4106
4107nla_put_failure:
4108 return -EMSGSIZE;
4109}
4110
4111static size_t inet6_get_link_af_size(const struct net_device *dev)
4112{
4113 if (!__in6_dev_get(dev))
4114 return 0;
4115
4116 return inet6_ifla6_size();
4117}
4118
4119static int inet6_fill_link_af(struct sk_buff *skb, const struct net_device *dev)
4120{
4121 struct inet6_dev *idev = __in6_dev_get(dev);
4122
4123 if (!idev)
4124 return -ENODATA;
4125
4126 if (inet6_fill_ifla6_attrs(skb, idev) < 0)
4127 return -EMSGSIZE;
4128
4129 return 0;
4130}
4131
Thomas Grafb382b192010-11-16 04:33:57 +00004132static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
4133 u32 pid, u32 seq, int event, unsigned int flags)
4134{
4135 struct net_device *dev = idev->dev;
4136 struct ifinfomsg *hdr;
4137 struct nlmsghdr *nlh;
4138 void *protoinfo;
4139
4140 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*hdr), flags);
4141 if (nlh == NULL)
4142 return -EMSGSIZE;
4143
4144 hdr = nlmsg_data(nlh);
4145 hdr->ifi_family = AF_INET6;
4146 hdr->__ifi_pad = 0;
4147 hdr->ifi_type = dev->type;
4148 hdr->ifi_index = dev->ifindex;
4149 hdr->ifi_flags = dev_get_flags(dev);
4150 hdr->ifi_change = 0;
4151
4152 NLA_PUT_STRING(skb, IFLA_IFNAME, dev->name);
4153
4154 if (dev->addr_len)
4155 NLA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr);
4156
4157 NLA_PUT_U32(skb, IFLA_MTU, dev->mtu);
4158 if (dev->ifindex != dev->iflink)
4159 NLA_PUT_U32(skb, IFLA_LINK, dev->iflink);
4160
4161 protoinfo = nla_nest_start(skb, IFLA_PROTINFO);
4162 if (protoinfo == NULL)
4163 goto nla_put_failure;
4164
4165 if (inet6_fill_ifla6_attrs(skb, idev) < 0)
4166 goto nla_put_failure;
4167
Thomas Graf04561c12006-11-14 19:53:58 -08004168 nla_nest_end(skb, protoinfo);
4169 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170
Thomas Graf04561c12006-11-14 19:53:58 -08004171nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08004172 nlmsg_cancel(skb, nlh);
4173 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174}
4175
4176static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
4177{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09004178 struct net *net = sock_net(skb->sk);
Eric Dumazet84d26972009-11-09 12:11:28 +00004179 int h, s_h;
David S. Miller434a8a52009-11-11 18:53:00 -08004180 int idx = 0, s_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181 struct net_device *dev;
4182 struct inet6_dev *idev;
Eric Dumazet84d26972009-11-09 12:11:28 +00004183 struct hlist_head *head;
4184 struct hlist_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185
Eric Dumazet84d26972009-11-09 12:11:28 +00004186 s_h = cb->args[0];
4187 s_idx = cb->args[1];
4188
4189 rcu_read_lock();
4190 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4191 idx = 0;
4192 head = &net->dev_index_head[h];
4193 hlist_for_each_entry_rcu(dev, node, head, index_hlist) {
4194 if (idx < s_idx)
4195 goto cont;
4196 idev = __in6_dev_get(dev);
4197 if (!idev)
4198 goto cont;
4199 if (inet6_fill_ifinfo(skb, idev,
4200 NETLINK_CB(cb->skb).pid,
4201 cb->nlh->nlmsg_seq,
4202 RTM_NEWLINK, NLM_F_MULTI) <= 0)
4203 goto out;
Pavel Emelianov7562f872007-05-03 15:13:45 -07004204cont:
Eric Dumazet84d26972009-11-09 12:11:28 +00004205 idx++;
4206 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004207 }
Eric Dumazet84d26972009-11-09 12:11:28 +00004208out:
4209 rcu_read_unlock();
4210 cb->args[1] = idx;
4211 cb->args[0] = h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212
4213 return skb->len;
4214}
4215
4216void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
4217{
4218 struct sk_buff *skb;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09004219 struct net *net = dev_net(idev->dev);
Thomas Graf8d7a76c2006-08-15 00:35:47 -07004220 int err = -ENOBUFS;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004221
Thomas Graf339bf982006-11-10 14:10:15 -08004222 skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC);
Thomas Graf8d7a76c2006-08-15 00:35:47 -07004223 if (skb == NULL)
4224 goto errout;
4225
4226 err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08004227 if (err < 0) {
4228 /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */
4229 WARN_ON(err == -EMSGSIZE);
4230 kfree_skb(skb);
4231 goto errout;
4232 }
Nicolas Dichtel5f75a102010-12-07 23:38:31 +00004233 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFINFO, NULL, GFP_ATOMIC);
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08004234 return;
Thomas Graf8d7a76c2006-08-15 00:35:47 -07004235errout:
4236 if (err < 0)
Nicolas Dichtel5f75a102010-12-07 23:38:31 +00004237 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFINFO, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238}
4239
Thomas Graf339bf982006-11-10 14:10:15 -08004240static inline size_t inet6_prefix_nlmsg_size(void)
4241{
4242 return NLMSG_ALIGN(sizeof(struct prefixmsg))
4243 + nla_total_size(sizeof(struct in6_addr))
4244 + nla_total_size(sizeof(struct prefix_cacheinfo));
4245}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07004246
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
Thomas Graf6051e2f2006-11-14 19:54:19 -08004248 struct prefix_info *pinfo, u32 pid, u32 seq,
4249 int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250{
Thomas Graf6051e2f2006-11-14 19:54:19 -08004251 struct prefixmsg *pmsg;
4252 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253 struct prefix_cacheinfo ci;
4254
Thomas Graf6051e2f2006-11-14 19:54:19 -08004255 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*pmsg), flags);
4256 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08004257 return -EMSGSIZE;
Thomas Graf6051e2f2006-11-14 19:54:19 -08004258
4259 pmsg = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260 pmsg->prefix_family = AF_INET6;
Patrick McHardy8a470772005-06-28 12:56:45 -07004261 pmsg->prefix_pad1 = 0;
4262 pmsg->prefix_pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263 pmsg->prefix_ifindex = idev->dev->ifindex;
4264 pmsg->prefix_len = pinfo->prefix_len;
4265 pmsg->prefix_type = pinfo->type;
Patrick McHardy8a470772005-06-28 12:56:45 -07004266 pmsg->prefix_pad3 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004267 pmsg->prefix_flags = 0;
4268 if (pinfo->onlink)
4269 pmsg->prefix_flags |= IF_PREFIX_ONLINK;
4270 if (pinfo->autoconf)
4271 pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
4272
Thomas Graf6051e2f2006-11-14 19:54:19 -08004273 NLA_PUT(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274
4275 ci.preferred_time = ntohl(pinfo->prefered);
4276 ci.valid_time = ntohl(pinfo->valid);
Thomas Graf6051e2f2006-11-14 19:54:19 -08004277 NLA_PUT(skb, PREFIX_CACHEINFO, sizeof(ci), &ci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278
Thomas Graf6051e2f2006-11-14 19:54:19 -08004279 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280
Thomas Graf6051e2f2006-11-14 19:54:19 -08004281nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08004282 nlmsg_cancel(skb, nlh);
4283 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284}
4285
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004286static void inet6_prefix_notify(int event, struct inet6_dev *idev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287 struct prefix_info *pinfo)
4288{
4289 struct sk_buff *skb;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09004290 struct net *net = dev_net(idev->dev);
Thomas Graf8c384bfa2006-08-15 00:36:07 -07004291 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292
Thomas Graf339bf982006-11-10 14:10:15 -08004293 skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC);
Thomas Graf8c384bfa2006-08-15 00:36:07 -07004294 if (skb == NULL)
4295 goto errout;
4296
4297 err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08004298 if (err < 0) {
4299 /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */
4300 WARN_ON(err == -EMSGSIZE);
4301 kfree_skb(skb);
4302 goto errout;
4303 }
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08004304 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC);
4305 return;
Thomas Graf8c384bfa2006-08-15 00:36:07 -07004306errout:
4307 if (err < 0)
Benjamin Thery6fda7352008-03-05 10:47:47 -08004308 rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309}
4310
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
4312{
4313 inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
4314
4315 switch (event) {
4316 case RTM_NEWADDR:
Neil Horman95c385b2007-04-25 17:08:10 -07004317 /*
4318 * If the address was optimistic
4319 * we inserted the route at the start of
4320 * our DAD process, so we don't need
4321 * to do it again
4322 */
4323 if (!(ifp->rt->rt6i_node))
4324 ip6_ins_rt(ifp->rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325 if (ifp->idev->cnf.forwarding)
4326 addrconf_join_anycast(ifp);
4327 break;
4328 case RTM_DELADDR:
4329 if (ifp->idev->cnf.forwarding)
4330 addrconf_leave_anycast(ifp);
4331 addrconf_leave_solict(ifp->idev, &ifp->addr);
Changli Gaod8d1f302010-06-10 23:31:35 -07004332 dst_hold(&ifp->rt->dst);
stephen hemminger93fa1592010-04-12 05:41:31 +00004333
David S. Miller73a8bd72011-01-23 23:27:15 -08004334 if (ip6_del_rt(ifp->rt))
Changli Gaod8d1f302010-06-10 23:31:35 -07004335 dst_free(&ifp->rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336 break;
4337 }
4338}
4339
4340static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
4341{
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07004342 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343 if (likely(ifp->idev->dead == 0))
4344 __ipv6_ifa_notify(event, ifp);
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07004345 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346}
4347
4348#ifdef CONFIG_SYSCTL
4349
4350static
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07004351int addrconf_sysctl_forward(ctl_table *ctl, int write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352 void __user *buffer, size_t *lenp, loff_t *ppos)
4353{
4354 int *valp = ctl->data;
4355 int val = *valp;
Eric W. Biederman88af1822010-02-19 13:22:59 +00004356 loff_t pos = *ppos;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00004357 ctl_table lctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004358 int ret;
4359
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00004360 /*
4361 * ctl->data points to idev->cnf.forwarding, we should
4362 * not modify it until we get the rtnl lock.
4363 */
4364 lctl = *ctl;
4365 lctl.data = &val;
4366
4367 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -08004369 if (write)
Stephen Hemmingerb325fdd2009-02-26 06:55:31 +00004370 ret = addrconf_fixup_forwarding(ctl, valp, val);
Eric W. Biederman88af1822010-02-19 13:22:59 +00004371 if (ret)
4372 *ppos = pos;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004373 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374}
4375
Brian Haley56d417b2009-06-01 03:07:33 -07004376static void dev_disable_change(struct inet6_dev *idev)
4377{
4378 if (!idev || !idev->dev)
4379 return;
4380
4381 if (idev->cnf.disable_ipv6)
4382 addrconf_notify(NULL, NETDEV_DOWN, idev->dev);
4383 else
4384 addrconf_notify(NULL, NETDEV_UP, idev->dev);
4385}
4386
4387static void addrconf_disable_change(struct net *net, __s32 newf)
4388{
4389 struct net_device *dev;
4390 struct inet6_dev *idev;
4391
Eric Dumazetc6d14c82009-11-04 05:43:23 -08004392 rcu_read_lock();
4393 for_each_netdev_rcu(net, dev) {
Brian Haley56d417b2009-06-01 03:07:33 -07004394 idev = __in6_dev_get(dev);
4395 if (idev) {
4396 int changed = (!idev->cnf.disable_ipv6) ^ (!newf);
4397 idev->cnf.disable_ipv6 = newf;
4398 if (changed)
4399 dev_disable_change(idev);
4400 }
Brian Haley56d417b2009-06-01 03:07:33 -07004401 }
Eric Dumazetc6d14c82009-11-04 05:43:23 -08004402 rcu_read_unlock();
Brian Haley56d417b2009-06-01 03:07:33 -07004403}
4404
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00004405static int addrconf_disable_ipv6(struct ctl_table *table, int *p, int newf)
Brian Haley56d417b2009-06-01 03:07:33 -07004406{
4407 struct net *net;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00004408 int old;
4409
4410 if (!rtnl_trylock())
4411 return restart_syscall();
Brian Haley56d417b2009-06-01 03:07:33 -07004412
4413 net = (struct net *)table->extra2;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00004414 old = *p;
4415 *p = newf;
Brian Haley56d417b2009-06-01 03:07:33 -07004416
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00004417 if (p == &net->ipv6.devconf_dflt->disable_ipv6) {
4418 rtnl_unlock();
Brian Haley56d417b2009-06-01 03:07:33 -07004419 return 0;
Eric W. Biederman88af1822010-02-19 13:22:59 +00004420 }
Brian Haley56d417b2009-06-01 03:07:33 -07004421
4422 if (p == &net->ipv6.devconf_all->disable_ipv6) {
Brian Haley56d417b2009-06-01 03:07:33 -07004423 net->ipv6.devconf_dflt->disable_ipv6 = newf;
4424 addrconf_disable_change(net, newf);
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00004425 } else if ((!newf) ^ (!old))
Brian Haley56d417b2009-06-01 03:07:33 -07004426 dev_disable_change((struct inet6_dev *)table->extra1);
4427
4428 rtnl_unlock();
4429 return 0;
4430}
4431
4432static
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07004433int addrconf_sysctl_disable(ctl_table *ctl, int write,
Brian Haley56d417b2009-06-01 03:07:33 -07004434 void __user *buffer, size_t *lenp, loff_t *ppos)
4435{
4436 int *valp = ctl->data;
4437 int val = *valp;
Eric W. Biederman88af1822010-02-19 13:22:59 +00004438 loff_t pos = *ppos;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00004439 ctl_table lctl;
Brian Haley56d417b2009-06-01 03:07:33 -07004440 int ret;
4441
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00004442 /*
4443 * ctl->data points to idev->cnf.disable_ipv6, we should
4444 * not modify it until we get the rtnl lock.
4445 */
4446 lctl = *ctl;
4447 lctl.data = &val;
4448
4449 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
Brian Haley56d417b2009-06-01 03:07:33 -07004450
4451 if (write)
4452 ret = addrconf_disable_ipv6(ctl, valp, val);
Eric W. Biederman88af1822010-02-19 13:22:59 +00004453 if (ret)
4454 *ppos = pos;
Brian Haley56d417b2009-06-01 03:07:33 -07004455 return ret;
4456}
4457
Linus Torvalds1da177e2005-04-16 15:20:36 -07004458static struct addrconf_sysctl_table
4459{
4460 struct ctl_table_header *sysctl_header;
YOSHIFUJI Hideakif6a07b22008-03-25 00:25:11 +09004461 ctl_table addrconf_vars[DEVCONF_MAX+1];
Pavel Emelyanov1dab6222007-12-02 00:59:38 +11004462 char *dev_name;
Brian Haleyab32ea52006-09-22 14:15:41 -07004463} addrconf_sysctl __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004464 .sysctl_header = NULL,
4465 .addrconf_vars = {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004466 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004467 .procname = "forwarding",
4468 .data = &ipv6_devconf.forwarding,
4469 .maxlen = sizeof(int),
4470 .mode = 0644,
4471 .proc_handler = addrconf_sysctl_forward,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004472 },
4473 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004474 .procname = "hop_limit",
4475 .data = &ipv6_devconf.hop_limit,
4476 .maxlen = sizeof(int),
4477 .mode = 0644,
4478 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479 },
4480 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004481 .procname = "mtu",
4482 .data = &ipv6_devconf.mtu6,
4483 .maxlen = sizeof(int),
4484 .mode = 0644,
4485 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486 },
4487 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004488 .procname = "accept_ra",
4489 .data = &ipv6_devconf.accept_ra,
4490 .maxlen = sizeof(int),
4491 .mode = 0644,
4492 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493 },
4494 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004495 .procname = "accept_redirects",
4496 .data = &ipv6_devconf.accept_redirects,
4497 .maxlen = sizeof(int),
4498 .mode = 0644,
4499 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500 },
4501 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004502 .procname = "autoconf",
4503 .data = &ipv6_devconf.autoconf,
4504 .maxlen = sizeof(int),
4505 .mode = 0644,
4506 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507 },
4508 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004509 .procname = "dad_transmits",
4510 .data = &ipv6_devconf.dad_transmits,
4511 .maxlen = sizeof(int),
4512 .mode = 0644,
4513 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514 },
4515 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004516 .procname = "router_solicitations",
4517 .data = &ipv6_devconf.rtr_solicits,
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 = "router_solicitation_interval",
4524 .data = &ipv6_devconf.rtr_solicit_interval,
4525 .maxlen = sizeof(int),
4526 .mode = 0644,
4527 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528 },
4529 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004530 .procname = "router_solicitation_delay",
4531 .data = &ipv6_devconf.rtr_solicit_delay,
4532 .maxlen = sizeof(int),
4533 .mode = 0644,
4534 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535 },
4536 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004537 .procname = "force_mld_version",
4538 .data = &ipv6_devconf.force_mld_version,
4539 .maxlen = sizeof(int),
4540 .mode = 0644,
4541 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004542 },
4543#ifdef CONFIG_IPV6_PRIVACY
4544 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004545 .procname = "use_tempaddr",
4546 .data = &ipv6_devconf.use_tempaddr,
4547 .maxlen = sizeof(int),
4548 .mode = 0644,
4549 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004550 },
4551 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004552 .procname = "temp_valid_lft",
4553 .data = &ipv6_devconf.temp_valid_lft,
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 = "temp_prefered_lft",
4560 .data = &ipv6_devconf.temp_prefered_lft,
4561 .maxlen = sizeof(int),
4562 .mode = 0644,
4563 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564 },
4565 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004566 .procname = "regen_max_retry",
4567 .data = &ipv6_devconf.regen_max_retry,
4568 .maxlen = sizeof(int),
4569 .mode = 0644,
4570 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004571 },
4572 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004573 .procname = "max_desync_factor",
4574 .data = &ipv6_devconf.max_desync_factor,
4575 .maxlen = sizeof(int),
4576 .mode = 0644,
4577 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578 },
4579#endif
4580 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004581 .procname = "max_addresses",
4582 .data = &ipv6_devconf.max_addresses,
4583 .maxlen = sizeof(int),
4584 .mode = 0644,
4585 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586 },
4587 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004588 .procname = "accept_ra_defrtr",
4589 .data = &ipv6_devconf.accept_ra_defrtr,
4590 .maxlen = sizeof(int),
4591 .mode = 0644,
4592 .proc_handler = proc_dointvec,
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -08004593 },
4594 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004595 .procname = "accept_ra_pinfo",
4596 .data = &ipv6_devconf.accept_ra_pinfo,
4597 .maxlen = sizeof(int),
4598 .mode = 0644,
4599 .proc_handler = proc_dointvec,
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -08004600 },
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08004601#ifdef CONFIG_IPV6_ROUTER_PREF
4602 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004603 .procname = "accept_ra_rtr_pref",
4604 .data = &ipv6_devconf.accept_ra_rtr_pref,
4605 .maxlen = sizeof(int),
4606 .mode = 0644,
4607 .proc_handler = proc_dointvec,
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08004608 },
YOSHIFUJI Hideaki52e16352006-03-20 17:05:47 -08004609 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004610 .procname = "router_probe_interval",
4611 .data = &ipv6_devconf.rtr_probe_interval,
4612 .maxlen = sizeof(int),
4613 .mode = 0644,
4614 .proc_handler = proc_dointvec_jiffies,
YOSHIFUJI Hideaki52e16352006-03-20 17:05:47 -08004615 },
Neil Hormanfa03ef32007-01-30 14:30:10 -08004616#ifdef CONFIG_IPV6_ROUTE_INFO
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -08004617 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004618 .procname = "accept_ra_rt_info_max_plen",
4619 .data = &ipv6_devconf.accept_ra_rt_info_max_plen,
4620 .maxlen = sizeof(int),
4621 .mode = 0644,
4622 .proc_handler = proc_dointvec,
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -08004623 },
4624#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08004625#endif
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -08004626 {
Lorenzo Colitti653be182014-03-26 19:35:41 +09004627 .procname = "accept_ra_rt_table",
4628 .data = &ipv6_devconf.accept_ra_rt_table,
4629 .maxlen = sizeof(int),
4630 .mode = 0644,
4631 .proc_handler = proc_dointvec,
4632 },
4633 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004634 .procname = "proxy_ndp",
4635 .data = &ipv6_devconf.proxy_ndp,
4636 .maxlen = sizeof(int),
4637 .mode = 0644,
4638 .proc_handler = proc_dointvec,
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -07004639 },
4640 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004641 .procname = "accept_source_route",
4642 .data = &ipv6_devconf.accept_source_route,
4643 .maxlen = sizeof(int),
4644 .mode = 0644,
4645 .proc_handler = proc_dointvec,
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -07004646 },
Neil Horman95c385b2007-04-25 17:08:10 -07004647#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
4648 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004649 .procname = "optimistic_dad",
4650 .data = &ipv6_devconf.optimistic_dad,
4651 .maxlen = sizeof(int),
4652 .mode = 0644,
4653 .proc_handler = proc_dointvec,
Neil Horman95c385b2007-04-25 17:08:10 -07004654
4655 },
4656#endif
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09004657#ifdef CONFIG_IPV6_MROUTE
4658 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004659 .procname = "mc_forwarding",
4660 .data = &ipv6_devconf.mc_forwarding,
4661 .maxlen = sizeof(int),
4662 .mode = 0444,
4663 .proc_handler = proc_dointvec,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09004664 },
4665#endif
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -07004666 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004667 .procname = "disable_ipv6",
4668 .data = &ipv6_devconf.disable_ipv6,
4669 .maxlen = sizeof(int),
4670 .mode = 0644,
4671 .proc_handler = addrconf_sysctl_disable,
YOSHIFUJI Hideaki778d80b2008-06-28 14:17:11 +09004672 },
4673 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004674 .procname = "accept_dad",
4675 .data = &ipv6_devconf.accept_dad,
4676 .maxlen = sizeof(int),
4677 .mode = 0644,
4678 .proc_handler = proc_dointvec,
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09004679 },
4680 {
Octavian Purdilaf7734fd2009-10-02 11:39:15 +00004681 .procname = "force_tllao",
4682 .data = &ipv6_devconf.force_tllao,
4683 .maxlen = sizeof(int),
4684 .mode = 0644,
4685 .proc_handler = proc_dointvec
4686 },
4687 {
Harout Hedeshian8b2414d2012-12-21 12:38:38 -08004688 .procname = "accept_ra_prefix_route",
4689 .data = &ipv6_devconf.accept_ra_prefix_route,
4690 .maxlen = sizeof(int),
4691 .mode = 0644,
4692 .proc_handler = proc_dointvec,
4693 },
4694 {
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08004695 /* sentinel */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696 }
4697 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07004698};
4699
Pavel Emelyanovbff16c22008-01-10 17:42:13 -08004700static int __addrconf_sysctl_register(struct net *net, char *dev_name,
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08004701 struct inet6_dev *idev, struct ipv6_devconf *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004702{
4703 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004704 struct addrconf_sysctl_table *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004705
Pavel Emelyanov1dab6222007-12-02 00:59:38 +11004706#define ADDRCONF_CTL_PATH_DEV 3
4707
4708 struct ctl_path addrconf_ctl_path[] = {
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08004709 { .procname = "net", },
4710 { .procname = "ipv6", },
4711 { .procname = "conf", },
Pavel Emelyanov1dab6222007-12-02 00:59:38 +11004712 { /* to be set */ },
4713 { },
4714 };
4715
4716
Arnaldo Carvalho de Meloaf879cc2006-11-17 12:14:37 -02004717 t = kmemdup(&addrconf_sysctl, sizeof(*t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004718 if (t == NULL)
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11004719 goto out;
4720
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07004721 for (i = 0; t->addrconf_vars[i].data; i++) {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004722 t->addrconf_vars[i].data += (char *)p - (char *)&ipv6_devconf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004723 t->addrconf_vars[i].extra1 = idev; /* embedded; no ref */
Pavel Emelyanovbff16c22008-01-10 17:42:13 -08004724 t->addrconf_vars[i].extra2 = net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004725 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004726
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004727 /*
4728 * Make a copy of dev_name, because '.procname' is regarded as const
Linus Torvalds1da177e2005-04-16 15:20:36 -07004729 * by sysctl and we wouldn't want anyone to change it under our feet
4730 * (see SIOCSIFNAME).
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004731 */
Pavel Emelyanov1dab6222007-12-02 00:59:38 +11004732 t->dev_name = kstrdup(dev_name, GFP_KERNEL);
4733 if (!t->dev_name)
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11004734 goto free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004735
Pavel Emelyanov1dab6222007-12-02 00:59:38 +11004736 addrconf_ctl_path[ADDRCONF_CTL_PATH_DEV].procname = t->dev_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004737
Pavel Emelyanovbff16c22008-01-10 17:42:13 -08004738 t->sysctl_header = register_net_sysctl_table(net, addrconf_ctl_path,
Pavel Emelyanov1dab6222007-12-02 00:59:38 +11004739 t->addrconf_vars);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740 if (t->sysctl_header == NULL)
4741 goto free_procname;
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11004742
4743 p->sysctl = t;
Pavel Emelyanov95897312008-01-10 17:41:45 -08004744 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004745
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11004746free_procname:
Pavel Emelyanov1dab6222007-12-02 00:59:38 +11004747 kfree(t->dev_name);
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11004748free:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004749 kfree(t);
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11004750out:
Pavel Emelyanov95897312008-01-10 17:41:45 -08004751 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004752}
4753
Pavel Emelyanov408c4762008-01-10 17:41:21 -08004754static void __addrconf_sysctl_unregister(struct ipv6_devconf *p)
4755{
4756 struct addrconf_sysctl_table *t;
4757
4758 if (p->sysctl == NULL)
4759 return;
4760
4761 t = p->sysctl;
4762 p->sysctl = NULL;
Lucian Adrian Grijincuff538812011-05-01 01:44:01 +00004763 unregister_net_sysctl_table(t->sysctl_header);
Pavel Emelyanov408c4762008-01-10 17:41:21 -08004764 kfree(t->dev_name);
4765 kfree(t);
4766}
4767
Pavel Emelyanovf52295a2007-12-02 00:58:37 +11004768static void addrconf_sysctl_register(struct inet6_dev *idev)
4769{
Eric W. Biederman54716e32010-02-14 03:27:03 +00004770 neigh_sysctl_register(idev->dev, idev->nd_parms, "ipv6",
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08004771 &ndisc_ifinfo_sysctl_change);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09004772 __addrconf_sysctl_register(dev_net(idev->dev), idev->dev->name,
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08004773 idev, &idev->cnf);
Pavel Emelyanovf52295a2007-12-02 00:58:37 +11004774}
4775
Pavel Emelyanov408c4762008-01-10 17:41:21 -08004776static void addrconf_sysctl_unregister(struct inet6_dev *idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777{
Pavel Emelyanov408c4762008-01-10 17:41:21 -08004778 __addrconf_sysctl_unregister(&idev->cnf);
4779 neigh_sysctl_unregister(idev->nd_parms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004780}
4781
4782
4783#endif
4784
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004785static int __net_init addrconf_init_net(struct net *net)
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08004786{
Hong Zhiguo6a519972013-03-26 01:52:45 +08004787 int err = -ENOMEM;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08004788 struct ipv6_devconf *all, *dflt;
4789
Hong Zhiguo6a519972013-03-26 01:52:45 +08004790 all = kmemdup(&ipv6_devconf, sizeof(ipv6_devconf), GFP_KERNEL);
4791 if (all == NULL)
4792 goto err_alloc_all;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08004793
Hong Zhiguo6a519972013-03-26 01:52:45 +08004794 dflt = kmemdup(&ipv6_devconf_dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL);
4795 if (dflt == NULL)
4796 goto err_alloc_dflt;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08004797
Hong Zhiguo6a519972013-03-26 01:52:45 +08004798 /* these will be inherited by all namespaces */
4799 dflt->autoconf = ipv6_defaults.autoconf;
4800 dflt->disable_ipv6 = ipv6_defaults.disable_ipv6;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08004801
4802 net->ipv6.devconf_all = all;
4803 net->ipv6.devconf_dflt = dflt;
4804
4805#ifdef CONFIG_SYSCTL
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08004806 err = __addrconf_sysctl_register(net, "all", NULL, all);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08004807 if (err < 0)
4808 goto err_reg_all;
4809
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08004810 err = __addrconf_sysctl_register(net, "default", NULL, dflt);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08004811 if (err < 0)
4812 goto err_reg_dflt;
4813#endif
4814 return 0;
4815
4816#ifdef CONFIG_SYSCTL
4817err_reg_dflt:
4818 __addrconf_sysctl_unregister(all);
4819err_reg_all:
4820 kfree(dflt);
4821#endif
4822err_alloc_dflt:
4823 kfree(all);
4824err_alloc_all:
4825 return err;
4826}
4827
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004828static void __net_exit addrconf_exit_net(struct net *net)
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08004829{
4830#ifdef CONFIG_SYSCTL
4831 __addrconf_sysctl_unregister(net->ipv6.devconf_dflt);
4832 __addrconf_sysctl_unregister(net->ipv6.devconf_all);
4833#endif
Octavian Purdila09ad9bc2009-11-25 15:14:13 -08004834 if (!net_eq(net, &init_net)) {
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08004835 kfree(net->ipv6.devconf_dflt);
4836 kfree(net->ipv6.devconf_all);
4837 }
4838}
4839
4840static struct pernet_operations addrconf_ops = {
4841 .init = addrconf_init_net,
4842 .exit = addrconf_exit_net,
4843};
4844
Linus Torvalds1da177e2005-04-16 15:20:36 -07004845/*
4846 * Device notifier
4847 */
4848
4849int register_inet6addr_notifier(struct notifier_block *nb)
4850{
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004851 return atomic_notifier_chain_register(&inet6addr_chain, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852}
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +09004853EXPORT_SYMBOL(register_inet6addr_notifier);
4854
Linus Torvalds1da177e2005-04-16 15:20:36 -07004855int unregister_inet6addr_notifier(struct notifier_block *nb)
4856{
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004857 return atomic_notifier_chain_unregister(&inet6addr_chain, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004858}
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +09004859EXPORT_SYMBOL(unregister_inet6addr_notifier);
4860
Thomas Grafb382b192010-11-16 04:33:57 +00004861static struct rtnl_af_ops inet6_ops = {
4862 .family = AF_INET6,
4863 .fill_link_af = inet6_fill_link_af,
4864 .get_link_af_size = inet6_get_link_af_size,
Thomas Grafb382b192010-11-16 04:33:57 +00004865};
4866
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867/*
4868 * Init / cleanup code
4869 */
4870
4871int __init addrconf_init(void)
4872{
stephen hemmingerc2e21292010-03-17 20:31:10 +00004873 int i, err;
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +09004874
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004875 err = ipv6_addr_label_init();
4876 if (err < 0) {
4877 printk(KERN_CRIT "IPv6 Addrconf:"
4878 " cannot initialize default policy table: %d.\n", err);
Neil Horman2cc6d2b2010-09-24 09:55:52 +00004879 goto out;
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +09004880 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004881
Neil Horman2cc6d2b2010-09-24 09:55:52 +00004882 err = register_pernet_subsys(&addrconf_ops);
4883 if (err < 0)
4884 goto out_addrlabel;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08004885
Linus Torvalds1da177e2005-04-16 15:20:36 -07004886 /* The addrconf netdev notifier requires that loopback_dev
4887 * has it's ipv6 private information allocated and setup
4888 * before it can bring up and give link-local addresses
4889 * to other devices which are up.
4890 *
4891 * Unfortunately, loopback_dev is not necessarily the first
4892 * entry in the global dev_base list of net devices. In fact,
4893 * it is likely to be the very last entry on that list.
4894 * So this causes the notifier registry below to try and
4895 * give link-local addresses to all devices besides loopback_dev
4896 * first, then loopback_dev, which cases all the non-loopback_dev
4897 * devices to fail to get a link-local address.
4898 *
4899 * So, as a temporary fix, allocate the ipv6 structure for
4900 * loopback_dev first by hand.
4901 * Longer term, all of the dependencies ipv6 has upon the loopback
4902 * device and it being up should be removed.
4903 */
4904 rtnl_lock();
Eric W. Biederman2774c7a2007-09-26 22:10:56 -07004905 if (!ipv6_add_dev(init_net.loopback_dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004906 err = -ENOMEM;
4907 rtnl_unlock();
4908 if (err)
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08004909 goto errlo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004910
stephen hemmingerc2e21292010-03-17 20:31:10 +00004911 for (i = 0; i < IN6_ADDR_HSIZE; i++)
4912 INIT_HLIST_HEAD(&inet6_addr_lst[i]);
4913
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914 register_netdevice_notifier(&ipv6_dev_notf);
4915
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916 addrconf_verify(0);
Thomas Grafc127ea22007-03-22 11:58:32 -07004917
Thomas Grafb382b192010-11-16 04:33:57 +00004918 err = rtnl_af_register(&inet6_ops);
4919 if (err < 0)
4920 goto errout_af;
4921
Greg Rosec7ac8672011-06-10 01:27:09 +00004922 err = __rtnl_register(PF_INET6, RTM_GETLINK, NULL, inet6_dump_ifinfo,
4923 NULL);
Thomas Grafc127ea22007-03-22 11:58:32 -07004924 if (err < 0)
4925 goto errout;
4926
4927 /* Only the first call to __rtnl_register can fail */
Greg Rosec7ac8672011-06-10 01:27:09 +00004928 __rtnl_register(PF_INET6, RTM_NEWADDR, inet6_rtm_newaddr, NULL, NULL);
4929 __rtnl_register(PF_INET6, RTM_DELADDR, inet6_rtm_deladdr, NULL, NULL);
4930 __rtnl_register(PF_INET6, RTM_GETADDR, inet6_rtm_getaddr,
4931 inet6_dump_ifaddr, NULL);
4932 __rtnl_register(PF_INET6, RTM_GETMULTICAST, NULL,
4933 inet6_dump_ifmcaddr, NULL);
4934 __rtnl_register(PF_INET6, RTM_GETANYCAST, NULL,
4935 inet6_dump_ifacaddr, NULL);
Thomas Grafc127ea22007-03-22 11:58:32 -07004936
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +09004937 ipv6_addr_label_rtnl_register();
4938
Linus Torvalds1da177e2005-04-16 15:20:36 -07004939 return 0;
Thomas Grafc127ea22007-03-22 11:58:32 -07004940errout:
Thomas Grafb382b192010-11-16 04:33:57 +00004941 rtnl_af_unregister(&inet6_ops);
4942errout_af:
Thomas Grafc127ea22007-03-22 11:58:32 -07004943 unregister_netdevice_notifier(&ipv6_dev_notf);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08004944errlo:
4945 unregister_pernet_subsys(&addrconf_ops);
Neil Horman2cc6d2b2010-09-24 09:55:52 +00004946out_addrlabel:
4947 ipv6_addr_label_cleanup();
4948out:
Thomas Grafc127ea22007-03-22 11:58:32 -07004949 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004950}
4951
Daniel Lezcano09f77092007-12-13 05:34:58 -08004952void addrconf_cleanup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004953{
Daniel Lezcano176c39a2009-03-03 01:06:45 -08004954 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955 int i;
4956
4957 unregister_netdevice_notifier(&ipv6_dev_notf);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08004958 unregister_pernet_subsys(&addrconf_ops);
Neil Horman2cc6d2b2010-09-24 09:55:52 +00004959 ipv6_addr_label_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004960
4961 rtnl_lock();
4962
Thomas Grafb382b192010-11-16 04:33:57 +00004963 __rtnl_af_unregister(&inet6_ops);
4964
Daniel Lezcano176c39a2009-03-03 01:06:45 -08004965 /* clean dev list */
4966 for_each_netdev(&init_net, dev) {
4967 if (__in6_dev_get(dev) == NULL)
4968 continue;
4969 addrconf_ifdown(dev, 1);
4970 }
4971 addrconf_ifdown(init_net.loopback_dev, 2);
4972
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004974 * Check hash table.
4975 */
stephen hemminger5c578ae2010-03-17 20:31:11 +00004976 spin_lock_bh(&addrconf_hash_lock);
stephen hemmingerc2e21292010-03-17 20:31:10 +00004977 for (i = 0; i < IN6_ADDR_HSIZE; i++)
4978 WARN_ON(!hlist_empty(&inet6_addr_lst[i]));
stephen hemminger5c578ae2010-03-17 20:31:11 +00004979 spin_unlock_bh(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004980
4981 del_timer(&addr_chk_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004982 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983}