blob: 420e563263840442a150c13098148921559c3f2d [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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 */
40
Joe Perchesf3213832012-05-15 14:11:53 +000041#define pr_fmt(fmt) "IPv6: " fmt
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>
Eric Dumazetddbe5032012-07-18 08:11:12 +000066#include <linux/hash.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020068#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#include <net/sock.h>
70#include <net/snmp.h>
71
alex.bluesman.smirnov@gmail.com06a4c1c2012-05-10 03:25:52 +000072#include <net/af_ieee802154.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#include <net/ipv6.h>
74#include <net/protocol.h>
75#include <net/ndisc.h>
76#include <net/ip6_route.h>
77#include <net/addrconf.h>
78#include <net/tcp.h>
79#include <net/ip.h>
Thomas Graf5d620262006-08-15 00:35:02 -070080#include <net/netlink.h>
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -070081#include <net/pkt_sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070082#include <linux/if_tunnel.h>
83#include <linux/rtnetlink.h>
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +000084#include <linux/netconf.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86#ifdef CONFIG_IPV6_PRIVACY
87#include <linux/random.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070088#endif
89
Stephen Hemmingere21e8462010-03-20 16:09:01 -070090#include <linux/uaccess.h>
Herbert Xu7f7d9a62007-04-24 21:54:09 -070091#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
93#include <linux/proc_fs.h>
94#include <linux/seq_file.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040095#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97/* Set to 3 to get tracing... */
98#define ACONF_DEBUG 2
99
100#if ACONF_DEBUG >= 3
101#define ADBG(x) printk x
102#else
103#define ADBG(x)
104#endif
105
106#define INFINITY_LIFE_TIME 0xFFFFFFFF
Thomas Graf18a31e12010-11-17 04:12:02 +0000107
108static inline u32 cstamp_delta(unsigned long cstamp)
109{
110 return (cstamp - INITIAL_JIFFIES) * 100UL / HZ;
111}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -0700113#define ADDRCONF_TIMER_FUZZ_MINUS (HZ > 50 ? HZ/50 : 1)
114#define ADDRCONF_TIMER_FUZZ (HZ / 4)
115#define ADDRCONF_TIMER_FUZZ_MAX (HZ)
116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117#ifdef CONFIG_SYSCTL
Pavel Emelyanovf52295a2007-12-02 00:58:37 +1100118static void addrconf_sysctl_register(struct inet6_dev *idev);
Pavel Emelyanov408c4762008-01-10 17:41:21 -0800119static void addrconf_sysctl_unregister(struct inet6_dev *idev);
120#else
121static inline void addrconf_sysctl_register(struct inet6_dev *idev)
122{
123}
124
125static inline void addrconf_sysctl_unregister(struct inet6_dev *idev)
126{
127}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128#endif
129
130#ifdef CONFIG_IPV6_PRIVACY
Sorin Dumitrueb7e0572012-08-30 02:01:45 +0000131static void __ipv6_regen_rndid(struct inet6_dev *idev);
132static void __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133static void ipv6_regen_rndid(unsigned long data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134#endif
135
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +0900136static int ipv6_generate_eui64(u8 *eui, struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137static int ipv6_count_addresses(struct inet6_dev *idev);
138
139/*
140 * Configured unicast address hash table
141 */
stephen hemmingerc2e21292010-03-17 20:31:10 +0000142static struct hlist_head inet6_addr_lst[IN6_ADDR_HSIZE];
stephen hemminger5c578ae2010-03-17 20:31:11 +0000143static DEFINE_SPINLOCK(addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145static void addrconf_verify(unsigned long);
146
Ingo Molnar8d06afa2005-09-09 13:10:40 -0700147static DEFINE_TIMER(addr_chk_timer, addrconf_verify, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148static DEFINE_SPINLOCK(addrconf_verify_lock);
149
150static void addrconf_join_anycast(struct inet6_ifaddr *ifp);
151static void addrconf_leave_anycast(struct inet6_ifaddr *ifp);
152
Jiri Pirko93d9b7d2010-03-10 10:28:56 +0000153static void addrconf_type_change(struct net_device *dev,
154 unsigned long event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155static int addrconf_ifdown(struct net_device *dev, int how);
156
Romain Kuntz21caa662013-01-09 21:06:03 +0000157static struct rt6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
158 int plen,
159 const struct net_device *dev,
160 u32 flags, u32 noflags);
161
David S. Millercf22f9a2012-04-14 21:37:40 -0400162static void addrconf_dad_start(struct inet6_ifaddr *ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163static void addrconf_dad_timer(unsigned long data);
164static void addrconf_dad_completed(struct inet6_ifaddr *ifp);
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +0900165static void addrconf_dad_run(struct inet6_dev *idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166static void addrconf_rs_timer(unsigned long data);
167static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
168static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
169
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900170static void inet6_prefix_notify(int event, struct inet6_dev *idev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 struct prefix_info *pinfo);
David S. Miller3e81c6d2010-03-20 16:18:00 -0700172static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
173 struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
Alan Sterne041c682006-03-27 01:16:30 -0800175static ATOMIC_NOTIFIER_HEAD(inet6addr_chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Adrian Bunk888c8482008-07-22 14:21:58 -0700177static struct ipv6_devconf ipv6_devconf __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 .forwarding = 0,
179 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
180 .mtu6 = IPV6_MIN_MTU,
181 .accept_ra = 1,
182 .accept_redirects = 1,
183 .autoconf = 1,
184 .force_mld_version = 0,
185 .dad_transmits = 1,
186 .rtr_solicits = MAX_RTR_SOLICITATIONS,
187 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
188 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
189#ifdef CONFIG_IPV6_PRIVACY
190 .use_tempaddr = 0,
191 .temp_valid_lft = TEMP_VALID_LIFETIME,
192 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
193 .regen_max_retry = REGEN_MAX_RETRY,
194 .max_desync_factor = MAX_DESYNC_FACTOR,
195#endif
196 .max_addresses = IPV6_MAX_ADDRESSES,
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -0800197 .accept_ra_defrtr = 1,
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -0800198 .accept_ra_pinfo = 1,
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800199#ifdef CONFIG_IPV6_ROUTER_PREF
200 .accept_ra_rtr_pref = 1,
YOSHIFUJI Hideaki52e16352006-03-20 17:05:47 -0800201 .rtr_probe_interval = 60 * HZ,
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -0800202#ifdef CONFIG_IPV6_ROUTE_INFO
203 .accept_ra_rt_info_max_plen = 0,
204#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800205#endif
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -0700206 .proxy_ndp = 0,
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -0700207 .accept_source_route = 0, /* we do not accept RH0 by default. */
YOSHIFUJI Hideaki778d80b2008-06-28 14:17:11 +0900208 .disable_ipv6 = 0,
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +0900209 .accept_dad = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210};
211
Brian Haleyab32ea52006-09-22 14:15:41 -0700212static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 .forwarding = 0,
214 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
215 .mtu6 = IPV6_MIN_MTU,
216 .accept_ra = 1,
217 .accept_redirects = 1,
218 .autoconf = 1,
219 .dad_transmits = 1,
220 .rtr_solicits = MAX_RTR_SOLICITATIONS,
221 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
222 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
223#ifdef CONFIG_IPV6_PRIVACY
224 .use_tempaddr = 0,
225 .temp_valid_lft = TEMP_VALID_LIFETIME,
226 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
227 .regen_max_retry = REGEN_MAX_RETRY,
228 .max_desync_factor = MAX_DESYNC_FACTOR,
229#endif
230 .max_addresses = IPV6_MAX_ADDRESSES,
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -0800231 .accept_ra_defrtr = 1,
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -0800232 .accept_ra_pinfo = 1,
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800233#ifdef CONFIG_IPV6_ROUTER_PREF
234 .accept_ra_rtr_pref = 1,
YOSHIFUJI Hideaki52e16352006-03-20 17:05:47 -0800235 .rtr_probe_interval = 60 * HZ,
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -0800236#ifdef CONFIG_IPV6_ROUTE_INFO
237 .accept_ra_rt_info_max_plen = 0,
238#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800239#endif
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -0700240 .proxy_ndp = 0,
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -0700241 .accept_source_route = 0, /* we do not accept RH0 by default. */
YOSHIFUJI Hideaki778d80b2008-06-28 14:17:11 +0900242 .disable_ipv6 = 0,
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +0900243 .accept_dad = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244};
245
246/* IPv6 Wildcard Address and Loopback Address defined by RFC2553 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +0900249const struct in6_addr in6addr_linklocal_allnodes = IN6ADDR_LINKLOCAL_ALLNODES_INIT;
250const struct in6_addr in6addr_linklocal_allrouters = IN6ADDR_LINKLOCAL_ALLROUTERS_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -0700252/* Check if a valid qdisc is available */
David S. Miller05297942008-07-08 23:01:27 -0700253static inline bool addrconf_qdisc_ok(const struct net_device *dev)
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -0700254{
David S. Miller05297942008-07-08 23:01:27 -0700255 return !qdisc_tx_is_noop(dev);
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -0700256}
257
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258static void addrconf_del_timer(struct inet6_ifaddr *ifp)
259{
260 if (del_timer(&ifp->timer))
261 __in6_ifa_put(ifp);
262}
263
Stephen Hemmingere21e8462010-03-20 16:09:01 -0700264enum addrconf_timer_t {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 AC_NONE,
266 AC_DAD,
267 AC_RS,
268};
269
270static void addrconf_mod_timer(struct inet6_ifaddr *ifp,
271 enum addrconf_timer_t what,
272 unsigned long when)
273{
274 if (!del_timer(&ifp->timer))
275 in6_ifa_hold(ifp);
276
277 switch (what) {
278 case AC_DAD:
279 ifp->timer.function = addrconf_dad_timer;
280 break;
281 case AC_RS:
282 ifp->timer.function = addrconf_rs_timer;
283 break;
Stephen Hemmingere21e8462010-03-20 16:09:01 -0700284 default:
285 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 }
287 ifp->timer.expires = jiffies + when;
288 add_timer(&ifp->timer);
289}
290
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700291static int snmp6_alloc_dev(struct inet6_dev *idev)
292{
Tejun Heo7d720c32010-02-16 15:20:26 +0000293 if (snmp_mib_init((void __percpu **)idev->stats.ipv6,
Eric Dumazet1823e4c82010-06-22 20:58:41 +0000294 sizeof(struct ipstats_mib),
295 __alignof__(struct ipstats_mib)) < 0)
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700296 goto err_ip;
Eric Dumazetbe281e52011-05-19 01:14:23 +0000297 idev->stats.icmpv6dev = kzalloc(sizeof(struct icmpv6_mib_device),
298 GFP_KERNEL);
299 if (!idev->stats.icmpv6dev)
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700300 goto err_icmp;
Eric Dumazetbe281e52011-05-19 01:14:23 +0000301 idev->stats.icmpv6msgdev = kzalloc(sizeof(struct icmpv6msg_mib_device),
302 GFP_KERNEL);
303 if (!idev->stats.icmpv6msgdev)
David L Stevens14878f72007-09-16 16:52:35 -0700304 goto err_icmpmsg;
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700305
306 return 0;
307
David L Stevens14878f72007-09-16 16:52:35 -0700308err_icmpmsg:
Eric Dumazetbe281e52011-05-19 01:14:23 +0000309 kfree(idev->stats.icmpv6dev);
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700310err_icmp:
Tejun Heo7d720c32010-02-16 15:20:26 +0000311 snmp_mib_free((void __percpu **)idev->stats.ipv6);
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700312err_ip:
Pavel Emelyanovaaf70ec2007-10-17 21:25:32 -0700313 return -ENOMEM;
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700314}
315
Pavel Emelyanov16910b92007-10-17 21:23:43 -0700316static void snmp6_free_dev(struct inet6_dev *idev)
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700317{
Eric Dumazetbe281e52011-05-19 01:14:23 +0000318 kfree(idev->stats.icmpv6msgdev);
319 kfree(idev->stats.icmpv6dev);
Tejun Heo7d720c32010-02-16 15:20:26 +0000320 snmp_mib_free((void __percpu **)idev->stats.ipv6);
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700321}
322
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323/* Nobody refers to this device, we may destroy it. */
324
325void in6_dev_finish_destroy(struct inet6_dev *idev)
326{
327 struct net_device *dev = idev->dev;
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700328
stephen hemminger502a2ff2010-03-17 20:31:13 +0000329 WARN_ON(!list_empty(&idev->addr_list));
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700330 WARN_ON(idev->mc_list != NULL);
331
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332#ifdef NET_REFCNT_DEBUG
Joe Perches91df42b2012-05-15 14:11:54 +0000333 pr_debug("%s: %s\n", __func__, dev ? dev->name : "NIL");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334#endif
335 dev_put(dev);
336 if (!idev->dead) {
Joe Perchesf3213832012-05-15 14:11:53 +0000337 pr_warn("Freeing alive inet6 device %p\n", idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 return;
339 }
340 snmp6_free_dev(idev);
Lai Jiangshan38f57d12011-03-15 17:59:14 +0800341 kfree_rcu(idev, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342}
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +0900343EXPORT_SYMBOL(in6_dev_finish_destroy);
344
Eldad Zack8e5e8f32012-04-01 07:49:08 +0000345static struct inet6_dev *ipv6_add_dev(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346{
347 struct inet6_dev *ndev;
348
349 ASSERT_RTNL();
350
351 if (dev->mtu < IPV6_MIN_MTU)
352 return NULL;
353
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900354 ndev = kzalloc(sizeof(struct inet6_dev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900356 if (ndev == NULL)
357 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Ingo Oeser322f74a2006-03-20 23:01:47 -0800359 rwlock_init(&ndev->lock);
360 ndev->dev = dev;
stephen hemminger502a2ff2010-03-17 20:31:13 +0000361 INIT_LIST_HEAD(&ndev->addr_list);
362
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900363 memcpy(&ndev->cnf, dev_net(dev)->ipv6.devconf_dflt, sizeof(ndev->cnf));
Ingo Oeser322f74a2006-03-20 23:01:47 -0800364 ndev->cnf.mtu6 = dev->mtu;
365 ndev->cnf.sysctl = NULL;
366 ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl);
367 if (ndev->nd_parms == NULL) {
368 kfree(ndev);
369 return NULL;
370 }
Ben Hutchings0187bdf2008-06-19 16:15:47 -0700371 if (ndev->cnf.forwarding)
372 dev_disable_lro(dev);
Ingo Oeser322f74a2006-03-20 23:01:47 -0800373 /* We refer to the device */
374 dev_hold(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Ingo Oeser322f74a2006-03-20 23:01:47 -0800376 if (snmp6_alloc_dev(ndev) < 0) {
377 ADBG((KERN_WARNING
Joe Perchesf3213832012-05-15 14:11:53 +0000378 "%s: cannot allocate memory for statistics; dev=%s.\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800379 __func__, dev->name));
Ingo Oeser322f74a2006-03-20 23:01:47 -0800380 neigh_parms_release(&nd_tbl, ndev->nd_parms);
Roy Li8603e332011-09-20 15:10:16 -0400381 dev_put(dev);
382 kfree(ndev);
Ingo Oeser322f74a2006-03-20 23:01:47 -0800383 return NULL;
384 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
Ingo Oeser322f74a2006-03-20 23:01:47 -0800386 if (snmp6_register_dev(ndev) < 0) {
387 ADBG((KERN_WARNING
Joe Perchesf3213832012-05-15 14:11:53 +0000388 "%s: cannot create /proc/net/dev_snmp6/%s\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800389 __func__, dev->name));
Ingo Oeser322f74a2006-03-20 23:01:47 -0800390 neigh_parms_release(&nd_tbl, ndev->nd_parms);
391 ndev->dead = 1;
392 in6_dev_finish_destroy(ndev);
393 return NULL;
394 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Ingo Oeser322f74a2006-03-20 23:01:47 -0800396 /* One reference from device. We must do this before
397 * we invoke __ipv6_regen_rndid().
398 */
399 in6_dev_hold(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +0900401 if (dev->flags & (IFF_NOARP | IFF_LOOPBACK))
402 ndev->cnf.accept_dad = -1;
403
Amerigo Wang07a93622012-10-29 16:23:10 +0000404#if IS_ENABLED(CONFIG_IPV6_SIT)
YOSHIFUJI Hideakib077d7a2008-04-13 23:42:18 -0700405 if (dev->type == ARPHRD_SIT && (dev->priv_flags & IFF_ISATAP)) {
Joe Perchesf3213832012-05-15 14:11:53 +0000406 pr_info("%s: Disabled Multicast RS\n", dev->name);
YOSHIFUJI Hideakib077d7a2008-04-13 23:42:18 -0700407 ndev->cnf.rtr_solicits = 0;
408 }
409#endif
410
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411#ifdef CONFIG_IPV6_PRIVACY
stephen hemminger372e6c82010-03-17 20:31:09 +0000412 INIT_LIST_HEAD(&ndev->tempaddr_list);
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800413 setup_timer(&ndev->regen_timer, ipv6_regen_rndid, (unsigned long)ndev);
Ingo Oeser322f74a2006-03-20 23:01:47 -0800414 if ((dev->flags&IFF_LOOPBACK) ||
415 dev->type == ARPHRD_TUNNEL ||
YOSHIFUJI Hideaki9625ed72008-04-13 23:47:11 -0700416 dev->type == ARPHRD_TUNNEL6 ||
Joerg Roedel0be669b2006-10-10 14:49:53 -0700417 dev->type == ARPHRD_SIT ||
Joerg Roedel0be669b2006-10-10 14:49:53 -0700418 dev->type == ARPHRD_NONE) {
Ingo Oeser322f74a2006-03-20 23:01:47 -0800419 ndev->cnf.use_tempaddr = -1;
420 } else {
421 in6_dev_hold(ndev);
422 ipv6_regen_rndid((unsigned long) ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 }
Ingo Oeser322f74a2006-03-20 23:01:47 -0800424#endif
425
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -0700426 if (netif_running(dev) && addrconf_qdisc_ok(dev))
Herbert Xu53aadcc2007-03-27 14:31:52 -0700427 ndev->if_flags |= IF_READY;
428
Ingo Oeser322f74a2006-03-20 23:01:47 -0800429 ipv6_mc_init_dev(ndev);
430 ndev->tstamp = jiffies;
Pavel Emelyanovf52295a2007-12-02 00:58:37 +1100431 addrconf_sysctl_register(ndev);
David L Stevens30c4cf52007-01-04 12:31:14 -0800432 /* protected by rtnl_lock */
Eric Dumazetcf778b02012-01-12 04:41:32 +0000433 rcu_assign_pointer(dev->ip6_ptr, ndev);
YOSHIFUJI Hideakid88ae4c2007-01-14 21:48:40 -0800434
435 /* Join all-node multicast group */
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +0900436 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allnodes);
YOSHIFUJI Hideakid88ae4c2007-01-14 21:48:40 -0800437
Li Weid6ddef92012-03-05 14:45:17 +0000438 /* Join all-router multicast group if forwarding is set */
Li Wei8b2aaed2012-03-07 14:58:07 +0000439 if (ndev->cnf.forwarding && (dev->flags & IFF_MULTICAST))
Li Weid6ddef92012-03-05 14:45:17 +0000440 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
441
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 return ndev;
443}
444
Eldad Zack8e5e8f32012-04-01 07:49:08 +0000445static struct inet6_dev *ipv6_find_idev(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446{
447 struct inet6_dev *idev;
448
449 ASSERT_RTNL();
450
Stephen Hemmingere21e8462010-03-20 16:09:01 -0700451 idev = __in6_dev_get(dev);
452 if (!idev) {
453 idev = ipv6_add_dev(dev);
454 if (!idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 return NULL;
456 }
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +0900457
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 if (dev->flags&IFF_UP)
459 ipv6_mc_up(idev);
460 return idev;
461}
462
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000463static int inet6_netconf_msgsize_devconf(int type)
464{
465 int size = NLMSG_ALIGN(sizeof(struct netconfmsg))
466 + nla_total_size(4); /* NETCONFA_IFINDEX */
467
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000468 /* type -1 is used for ALL */
469 if (type == -1 || type == NETCONFA_FORWARDING)
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000470 size += nla_total_size(4);
David S. Millerb1afce92012-12-04 14:46:34 -0500471#ifdef CONFIG_IPV6_MROUTE
Nicolas Dichteld67b8c62012-12-04 01:13:35 +0000472 if (type == -1 || type == NETCONFA_MC_FORWARDING)
473 size += nla_total_size(4);
David S. Millerb1afce92012-12-04 14:46:34 -0500474#endif
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000475
476 return size;
477}
478
479static int inet6_netconf_fill_devconf(struct sk_buff *skb, int ifindex,
480 struct ipv6_devconf *devconf, u32 portid,
481 u32 seq, int event, unsigned int flags,
482 int type)
483{
484 struct nlmsghdr *nlh;
485 struct netconfmsg *ncm;
486
487 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct netconfmsg),
488 flags);
489 if (nlh == NULL)
490 return -EMSGSIZE;
491
492 ncm = nlmsg_data(nlh);
493 ncm->ncm_family = AF_INET6;
494
495 if (nla_put_s32(skb, NETCONFA_IFINDEX, ifindex) < 0)
496 goto nla_put_failure;
497
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000498 /* type -1 is used for ALL */
499 if ((type == -1 || type == NETCONFA_FORWARDING) &&
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000500 nla_put_s32(skb, NETCONFA_FORWARDING, devconf->forwarding) < 0)
501 goto nla_put_failure;
David S. Millerb1afce92012-12-04 14:46:34 -0500502#ifdef CONFIG_IPV6_MROUTE
Nicolas Dichteld67b8c62012-12-04 01:13:35 +0000503 if ((type == -1 || type == NETCONFA_MC_FORWARDING) &&
504 nla_put_s32(skb, NETCONFA_MC_FORWARDING,
505 devconf->mc_forwarding) < 0)
506 goto nla_put_failure;
David S. Millerb1afce92012-12-04 14:46:34 -0500507#endif
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000508 return nlmsg_end(skb, nlh);
509
510nla_put_failure:
511 nlmsg_cancel(skb, nlh);
512 return -EMSGSIZE;
513}
514
Nicolas Dichteld67b8c62012-12-04 01:13:35 +0000515void inet6_netconf_notify_devconf(struct net *net, int type, int ifindex,
516 struct ipv6_devconf *devconf)
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000517{
518 struct sk_buff *skb;
519 int err = -ENOBUFS;
520
521 skb = nlmsg_new(inet6_netconf_msgsize_devconf(type), GFP_ATOMIC);
522 if (skb == NULL)
523 goto errout;
524
525 err = inet6_netconf_fill_devconf(skb, ifindex, devconf, 0, 0,
526 RTM_NEWNETCONF, 0, type);
527 if (err < 0) {
528 /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
529 WARN_ON(err == -EMSGSIZE);
530 kfree_skb(skb);
531 goto errout;
532 }
533 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_NETCONF, NULL, GFP_ATOMIC);
534 return;
535errout:
Cong Dingbd779022012-12-18 12:08:56 +0000536 rtnl_set_sk_err(net, RTNLGRP_IPV6_NETCONF, err);
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000537}
538
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000539static const struct nla_policy devconf_ipv6_policy[NETCONFA_MAX+1] = {
540 [NETCONFA_IFINDEX] = { .len = sizeof(int) },
541 [NETCONFA_FORWARDING] = { .len = sizeof(int) },
542};
543
544static int inet6_netconf_get_devconf(struct sk_buff *in_skb,
545 struct nlmsghdr *nlh,
546 void *arg)
547{
548 struct net *net = sock_net(in_skb->sk);
549 struct nlattr *tb[NETCONFA_MAX+1];
550 struct netconfmsg *ncm;
551 struct sk_buff *skb;
552 struct ipv6_devconf *devconf;
553 struct inet6_dev *in6_dev;
554 struct net_device *dev;
555 int ifindex;
556 int err;
557
558 err = nlmsg_parse(nlh, sizeof(*ncm), tb, NETCONFA_MAX,
559 devconf_ipv6_policy);
560 if (err < 0)
561 goto errout;
562
563 err = EINVAL;
564 if (!tb[NETCONFA_IFINDEX])
565 goto errout;
566
567 ifindex = nla_get_s32(tb[NETCONFA_IFINDEX]);
568 switch (ifindex) {
569 case NETCONFA_IFINDEX_ALL:
570 devconf = net->ipv6.devconf_all;
571 break;
572 case NETCONFA_IFINDEX_DEFAULT:
573 devconf = net->ipv6.devconf_dflt;
574 break;
575 default:
576 dev = __dev_get_by_index(net, ifindex);
577 if (dev == NULL)
578 goto errout;
579 in6_dev = __in6_dev_get(dev);
580 if (in6_dev == NULL)
581 goto errout;
582 devconf = &in6_dev->cnf;
583 break;
584 }
585
586 err = -ENOBUFS;
587 skb = nlmsg_new(inet6_netconf_msgsize_devconf(-1), GFP_ATOMIC);
588 if (skb == NULL)
589 goto errout;
590
591 err = inet6_netconf_fill_devconf(skb, ifindex, devconf,
592 NETLINK_CB(in_skb).portid,
593 nlh->nlmsg_seq, RTM_NEWNETCONF, 0,
594 -1);
595 if (err < 0) {
596 /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
597 WARN_ON(err == -EMSGSIZE);
598 kfree_skb(skb);
599 goto errout;
600 }
601 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
602errout:
603 return err;
604}
605
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606#ifdef CONFIG_SYSCTL
607static void dev_forward_change(struct inet6_dev *idev)
608{
609 struct net_device *dev;
610 struct inet6_ifaddr *ifa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
612 if (!idev)
613 return;
614 dev = idev->dev;
Ben Hutchings0187bdf2008-06-19 16:15:47 -0700615 if (idev->cnf.forwarding)
616 dev_disable_lro(dev);
Amerigo Wang1a940832012-10-28 17:43:53 +0000617 if (dev->flags & IFF_MULTICAST) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 if (idev->cnf.forwarding)
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +0900619 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 else
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +0900621 ipv6_dev_mc_dec(dev, &in6addr_linklocal_allrouters);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 }
stephen hemminger502a2ff2010-03-17 20:31:13 +0000623
624 list_for_each_entry(ifa, &idev->addr_list, if_list) {
Michal Wrobel2c12a742007-02-26 15:36:10 -0800625 if (ifa->flags&IFA_F_TENTATIVE)
626 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 if (idev->cnf.forwarding)
628 addrconf_join_anycast(ifa);
629 else
630 addrconf_leave_anycast(ifa);
631 }
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000632 inet6_netconf_notify_devconf(dev_net(dev), NETCONFA_FORWARDING,
633 dev->ifindex, &idev->cnf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634}
635
636
Pavel Emelyanove1869322008-01-10 17:43:50 -0800637static void addrconf_forward_change(struct net *net, __s32 newf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638{
639 struct net_device *dev;
640 struct inet6_dev *idev;
641
Ben Hutchings4acd4942012-08-14 08:54:51 +0000642 for_each_netdev(net, dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 idev = __in6_dev_get(dev);
644 if (idev) {
Pavel Emelyanove1869322008-01-10 17:43:50 -0800645 int changed = (!idev->cnf.forwarding) ^ (!newf);
646 idev->cnf.forwarding = newf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 if (changed)
648 dev_forward_change(idev);
649 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651}
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800652
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000653static int addrconf_fixup_forwarding(struct ctl_table *table, int *p, int newf)
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800654{
Pavel Emelyanovbff16c22008-01-10 17:42:13 -0800655 struct net *net;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000656 int old;
657
658 if (!rtnl_trylock())
659 return restart_syscall();
Pavel Emelyanovbff16c22008-01-10 17:42:13 -0800660
661 net = (struct net *)table->extra2;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000662 old = *p;
663 *p = newf;
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800664
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000665 if (p == &net->ipv6.devconf_dflt->forwarding) {
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000666 if ((!newf) ^ (!old))
667 inet6_netconf_notify_devconf(net, NETCONFA_FORWARDING,
668 NETCONFA_IFINDEX_DEFAULT,
669 net->ipv6.devconf_dflt);
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000670 rtnl_unlock();
671 return 0;
Eric W. Biederman88af1822010-02-19 13:22:59 +0000672 }
Stephen Hemmingerb325fdd2009-02-26 06:55:31 +0000673
Pavel Emelyanove1869322008-01-10 17:43:50 -0800674 if (p == &net->ipv6.devconf_all->forwarding) {
Pavel Emelyanove1869322008-01-10 17:43:50 -0800675 net->ipv6.devconf_dflt->forwarding = newf;
676 addrconf_forward_change(net, newf);
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000677 if ((!newf) ^ (!old))
678 inet6_netconf_notify_devconf(net, NETCONFA_FORWARDING,
679 NETCONFA_IFINDEX_ALL,
680 net->ipv6.devconf_all);
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000681 } else if ((!newf) ^ (!old))
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800682 dev_forward_change((struct inet6_dev *)table->extra1);
Ben Hutchings0187bdf2008-06-19 16:15:47 -0700683 rtnl_unlock();
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800684
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000685 if (newf)
Daniel Lezcano7b4da532008-03-04 13:47:14 -0800686 rt6_purge_dflt_routers(net);
Stephen Hemmingerb325fdd2009-02-26 06:55:31 +0000687 return 1;
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800688}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689#endif
690
stephen hemminger5c578ae2010-03-17 20:31:11 +0000691/* Nobody refers to this ifaddr, destroy it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp)
693{
stephen hemmingerc2e21292010-03-17 20:31:10 +0000694 WARN_ON(!hlist_unhashed(&ifp->addr_lst));
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700695
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696#ifdef NET_REFCNT_DEBUG
Joe Perches91df42b2012-05-15 14:11:54 +0000697 pr_debug("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698#endif
699
700 in6_dev_put(ifp->idev);
701
702 if (del_timer(&ifp->timer))
Stephen Hemmingere21e8462010-03-20 16:09:01 -0700703 pr_notice("Timer is still running, when freeing ifa=%p\n", ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
Herbert Xue9d3e082010-05-18 15:36:06 -0700705 if (ifp->state != INET6_IFADDR_STATE_DEAD) {
Joe Perchesf3213832012-05-15 14:11:53 +0000706 pr_warn("Freeing alive inet6 address %p\n", ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 return;
708 }
Amerigo Wang94e187c2012-10-29 00:13:19 +0000709 ip6_rt_put(ifp->rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
Lai Jiangshane5785982011-03-15 18:00:14 +0800711 kfree_rcu(ifp, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712}
713
Brian Haleye55ffac2006-07-10 15:25:51 -0700714static void
715ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp)
716{
stephen hemminger502a2ff2010-03-17 20:31:13 +0000717 struct list_head *p;
YOSHIFUJI Hideaki8a6ce0c2006-07-11 13:05:30 -0700718 int ifp_scope = ipv6_addr_src_scope(&ifp->addr);
Brian Haleye55ffac2006-07-10 15:25:51 -0700719
720 /*
721 * Each device address list is sorted in order of scope -
722 * global before linklocal.
723 */
stephen hemminger502a2ff2010-03-17 20:31:13 +0000724 list_for_each(p, &idev->addr_list) {
725 struct inet6_ifaddr *ifa
726 = list_entry(p, struct inet6_ifaddr, if_list);
YOSHIFUJI Hideaki8a6ce0c2006-07-11 13:05:30 -0700727 if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr))
Brian Haleye55ffac2006-07-10 15:25:51 -0700728 break;
729 }
730
David S. Millerb54c9b92010-03-25 21:25:30 -0700731 list_add_tail(&ifp->if_list, p);
Brian Haleye55ffac2006-07-10 15:25:51 -0700732}
733
Eric Dumazetddbe5032012-07-18 08:11:12 +0000734static u32 inet6_addr_hash(const struct in6_addr *addr)
YOSHIFUJI Hideaki3eb84f42008-04-10 15:42:08 +0900735{
Eric Dumazetddbe5032012-07-18 08:11:12 +0000736 return hash_32(ipv6_addr_hash(addr), IN6_ADDR_HSIZE_SHIFT);
YOSHIFUJI Hideaki3eb84f42008-04-10 15:42:08 +0900737}
738
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739/* On success it returns ifp with increased reference count */
740
741static struct inet6_ifaddr *
742ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr, int pfxlen,
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -0700743 int scope, u32 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744{
745 struct inet6_ifaddr *ifa = NULL;
746 struct rt6_info *rt;
stephen hemminger3a88a812010-03-17 20:31:12 +0000747 unsigned int hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 int err = 0;
YOSHIFUJI Hideakid68b8272008-06-25 16:26:47 +0900749 int addr_type = ipv6_addr_type(addr);
750
751 if (addr_type == IPV6_ADDR_ANY ||
752 addr_type & IPV6_ADDR_MULTICAST ||
753 (!(idev->dev->flags & IFF_LOOPBACK) &&
754 addr_type & IPV6_ADDR_LOOPBACK))
755 return ERR_PTR(-EADDRNOTAVAIL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -0700757 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 if (idev->dead) {
759 err = -ENODEV; /*XXX*/
760 goto out2;
761 }
762
Brian Haley56d417b2009-06-01 03:07:33 -0700763 if (idev->cnf.disable_ipv6) {
Brian Haley9bdd8d42009-03-18 18:22:48 -0700764 err = -EACCES;
765 goto out2;
766 }
767
stephen hemminger5c578ae2010-03-17 20:31:11 +0000768 spin_lock(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
770 /* Ignore adding duplicate addresses on an interface */
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900771 if (ipv6_chk_same_addr(dev_net(idev->dev), addr, idev->dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 ADBG(("ipv6_add_addr: already assigned\n"));
773 err = -EEXIST;
774 goto out;
775 }
776
Ingo Oeser322f74a2006-03-20 23:01:47 -0800777 ifa = kzalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
779 if (ifa == NULL) {
780 ADBG(("ipv6_add_addr: malloc failed\n"));
781 err = -ENOBUFS;
782 goto out;
783 }
784
David S. Miller8f031512011-12-06 16:48:14 -0500785 rt = addrconf_dst_alloc(idev, addr, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 if (IS_ERR(rt)) {
787 err = PTR_ERR(rt);
788 goto out;
789 }
790
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000791 ifa->addr = *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
793 spin_lock_init(&ifa->lock);
Herbert Xue9d3e082010-05-18 15:36:06 -0700794 spin_lock_init(&ifa->state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 init_timer(&ifa->timer);
stephen hemmingerc2e21292010-03-17 20:31:10 +0000796 INIT_HLIST_NODE(&ifa->addr_lst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 ifa->timer.data = (unsigned long) ifa;
798 ifa->scope = scope;
799 ifa->prefix_len = pfxlen;
800 ifa->flags = flags | IFA_F_TENTATIVE;
801 ifa->cstamp = ifa->tstamp = jiffies;
802
Keir Fraser57f5f542006-08-29 02:43:49 -0700803 ifa->rt = rt;
804
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 ifa->idev = idev;
806 in6_dev_hold(idev);
807 /* For caller */
808 in6_ifa_hold(ifa);
809
810 /* Add to big hash table */
Eric Dumazetddbe5032012-07-18 08:11:12 +0000811 hash = inet6_addr_hash(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
stephen hemminger5c578ae2010-03-17 20:31:11 +0000813 hlist_add_head_rcu(&ifa->addr_lst, &inet6_addr_lst[hash]);
stephen hemminger5c578ae2010-03-17 20:31:11 +0000814 spin_unlock(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
816 write_lock(&idev->lock);
817 /* Add to inet6_dev unicast addr list. */
Brian Haleye55ffac2006-07-10 15:25:51 -0700818 ipv6_link_dev_addr(idev, ifa);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
820#ifdef CONFIG_IPV6_PRIVACY
821 if (ifa->flags&IFA_F_TEMPORARY) {
stephen hemminger372e6c82010-03-17 20:31:09 +0000822 list_add(&ifa->tmp_list, &idev->tempaddr_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 in6_ifa_hold(ifa);
824 }
825#endif
826
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 in6_ifa_hold(ifa);
828 write_unlock(&idev->lock);
829out2:
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -0700830 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
YOSHIFUJI Hideakifd928332005-04-19 22:27:09 -0700832 if (likely(err == 0))
Alan Sterne041c682006-03-27 01:16:30 -0800833 atomic_notifier_call_chain(&inet6addr_chain, NETDEV_UP, ifa);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 else {
835 kfree(ifa);
836 ifa = ERR_PTR(err);
837 }
838
839 return ifa;
840out:
stephen hemminger5c578ae2010-03-17 20:31:11 +0000841 spin_unlock(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 goto out2;
843}
844
845/* This function wants to get referenced ifp and releases it before return */
846
847static void ipv6_del_addr(struct inet6_ifaddr *ifp)
848{
stephen hemminger502a2ff2010-03-17 20:31:13 +0000849 struct inet6_ifaddr *ifa, *ifn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 struct inet6_dev *idev = ifp->idev;
Herbert Xu4c5ff6a2010-05-18 15:54:18 -0700851 int state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 int deleted = 0, onlink = 0;
853 unsigned long expires = jiffies;
854
Herbert Xu4c5ff6a2010-05-18 15:54:18 -0700855 spin_lock_bh(&ifp->state_lock);
856 state = ifp->state;
Herbert Xue9d3e082010-05-18 15:36:06 -0700857 ifp->state = INET6_IFADDR_STATE_DEAD;
Herbert Xu4c5ff6a2010-05-18 15:54:18 -0700858 spin_unlock_bh(&ifp->state_lock);
859
860 if (state == INET6_IFADDR_STATE_DEAD)
861 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
stephen hemminger5c578ae2010-03-17 20:31:11 +0000863 spin_lock_bh(&addrconf_hash_lock);
864 hlist_del_init_rcu(&ifp->addr_lst);
stephen hemminger5c578ae2010-03-17 20:31:11 +0000865 spin_unlock_bh(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
867 write_lock_bh(&idev->lock);
868#ifdef CONFIG_IPV6_PRIVACY
869 if (ifp->flags&IFA_F_TEMPORARY) {
stephen hemminger372e6c82010-03-17 20:31:09 +0000870 list_del(&ifp->tmp_list);
871 if (ifp->ifpub) {
872 in6_ifa_put(ifp->ifpub);
873 ifp->ifpub = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 }
stephen hemminger372e6c82010-03-17 20:31:09 +0000875 __in6_ifa_put(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 }
877#endif
878
stephen hemminger502a2ff2010-03-17 20:31:13 +0000879 list_for_each_entry_safe(ifa, ifn, &idev->addr_list, if_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 if (ifa == ifp) {
stephen hemminger502a2ff2010-03-17 20:31:13 +0000881 list_del_init(&ifp->if_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 __in6_ifa_put(ifp);
stephen hemminger502a2ff2010-03-17 20:31:13 +0000883
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 if (!(ifp->flags & IFA_F_PERMANENT) || onlink > 0)
885 break;
886 deleted = 1;
Kristian Slavov1d142802005-12-21 18:47:24 -0800887 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 } else if (ifp->flags & IFA_F_PERMANENT) {
889 if (ipv6_prefix_equal(&ifa->addr, &ifp->addr,
890 ifp->prefix_len)) {
891 if (ifa->flags & IFA_F_PERMANENT) {
892 onlink = 1;
893 if (deleted)
894 break;
895 } else {
896 unsigned long lifetime;
897
898 if (!onlink)
899 onlink = -1;
900
901 spin_lock(&ifa->lock);
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +0900902
903 lifetime = addrconf_timeout_fixup(ifa->valid_lft, HZ);
904 /*
905 * Note: Because this address is
906 * not permanent, lifetime <
907 * LONG_MAX / HZ here.
908 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 if (time_before(expires,
910 ifa->tstamp + lifetime * HZ))
911 expires = ifa->tstamp + lifetime * HZ;
912 spin_unlock(&ifa->lock);
913 }
914 }
915 }
916 }
917 write_unlock_bh(&idev->lock);
918
Andrey Vaginb2238562008-07-08 15:13:31 -0700919 addrconf_del_timer(ifp);
920
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 ipv6_ifa_notify(RTM_DELADDR, ifp);
922
Alan Sterne041c682006-03-27 01:16:30 -0800923 atomic_notifier_call_chain(&inet6addr_chain, NETDEV_DOWN, ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 /*
926 * Purge or update corresponding prefix
927 *
928 * 1) we don't purge prefix here if address was not permanent.
929 * prefix is managed by its own lifetime.
930 * 2) if there're no addresses, delete prefix.
931 * 3) if there're still other permanent address(es),
932 * corresponding prefix is still permanent.
933 * 4) otherwise, update prefix lifetime to the
934 * longest valid lifetime among the corresponding
935 * addresses on the device.
936 * Note: subsequent RA will update lifetime.
937 *
938 * --yoshfuji
939 */
940 if ((ifp->flags & IFA_F_PERMANENT) && onlink < 1) {
941 struct in6_addr prefix;
942 struct rt6_info *rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
Nicolas Dichtel64c6d082012-09-26 00:04:55 +0000944 ipv6_addr_prefix(&prefix, &ifp->addr, ifp->prefix_len);
Nicolas Dichtel64c6d082012-09-26 00:04:55 +0000945
Romain Kuntz21caa662013-01-09 21:06:03 +0000946 rt = addrconf_get_prefix_route(&prefix,
947 ifp->prefix_len,
948 ifp->idev->dev,
949 0, RTF_GATEWAY | RTF_DEFAULT);
950
951 if (rt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 if (onlink == 0) {
Thomas Grafe0a1ad732006-08-22 00:00:21 -0700953 ip6_del_rt(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 rt = NULL;
955 } else if (!(rt->rt6i_flags & RTF_EXPIRES)) {
Gao feng1716a962012-04-06 00:13:10 +0000956 rt6_set_expires(rt, expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 }
958 }
Amerigo Wang94e187c2012-10-29 00:13:19 +0000959 ip6_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 }
961
Daniel Walterc3968a82011-04-13 21:10:57 +0000962 /* clean up prefsrc entries */
963 rt6_remove_prefsrc(ifp);
Herbert Xu4c5ff6a2010-05-18 15:54:18 -0700964out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 in6_ifa_put(ifp);
966}
967
968#ifdef CONFIG_IPV6_PRIVACY
969static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift)
970{
971 struct inet6_dev *idev = ifp->idev;
972 struct in6_addr addr, *tmpaddr;
Lorenzo Colitti76f793e2011-07-26 13:50:49 +0000973 unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_tstamp, age;
Benoit Boissinoteac55bf2008-04-02 00:01:35 -0700974 unsigned long regen_advance;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 int tmp_plen;
976 int ret = 0;
977 int max_addresses;
Neil Horman95c385b2007-04-25 17:08:10 -0700978 u32 addr_flags;
Lorenzo Colitti76f793e2011-07-26 13:50:49 +0000979 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
981 write_lock(&idev->lock);
982 if (ift) {
983 spin_lock_bh(&ift->lock);
984 memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8);
985 spin_unlock_bh(&ift->lock);
986 tmpaddr = &addr;
987 } else {
988 tmpaddr = NULL;
989 }
990retry:
991 in6_dev_hold(idev);
992 if (idev->cnf.use_tempaddr <= 0) {
993 write_unlock(&idev->lock);
Joe Perchesf3213832012-05-15 14:11:53 +0000994 pr_info("%s: use_tempaddr is disabled\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 in6_dev_put(idev);
996 ret = -1;
997 goto out;
998 }
999 spin_lock_bh(&ifp->lock);
1000 if (ifp->regen_count++ >= idev->cnf.regen_max_retry) {
1001 idev->cnf.use_tempaddr = -1; /*XXX*/
1002 spin_unlock_bh(&ifp->lock);
1003 write_unlock(&idev->lock);
Joe Perchesf3213832012-05-15 14:11:53 +00001004 pr_warn("%s: regeneration time exceeded - disabled temporary address support\n",
1005 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 in6_dev_put(idev);
1007 ret = -1;
1008 goto out;
1009 }
1010 in6_ifa_hold(ifp);
1011 memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
Sorin Dumitrueb7e0572012-08-30 02:01:45 +00001012 __ipv6_try_regen_rndid(idev, tmpaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 memcpy(&addr.s6_addr[8], idev->rndid, 8);
Lorenzo Colitti76f793e2011-07-26 13:50:49 +00001014 age = (now - ifp->tstamp) / HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 tmp_valid_lft = min_t(__u32,
1016 ifp->valid_lft,
Glenn Wurster7a876b02010-09-27 07:10:10 +00001017 idev->cnf.temp_valid_lft + age);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001018 tmp_prefered_lft = min_t(__u32,
1019 ifp->prefered_lft,
Glenn Wurster7a876b02010-09-27 07:10:10 +00001020 idev->cnf.temp_prefered_lft + age -
Ben Hutchings784e2712010-06-26 11:42:55 +00001021 idev->cnf.max_desync_factor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 tmp_plen = ifp->prefix_len;
1023 max_addresses = idev->cnf.max_addresses;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 tmp_tstamp = ifp->tstamp;
1025 spin_unlock_bh(&ifp->lock);
1026
Benoit Boissinoteac55bf2008-04-02 00:01:35 -07001027 regen_advance = idev->cnf.regen_max_retry *
1028 idev->cnf.dad_transmits *
1029 idev->nd_parms->retrans_time / HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 write_unlock(&idev->lock);
Neil Horman95c385b2007-04-25 17:08:10 -07001031
Benoit Boissinoteac55bf2008-04-02 00:01:35 -07001032 /* A temporary address is created only if this calculated Preferred
1033 * Lifetime is greater than REGEN_ADVANCE time units. In particular,
1034 * an implementation must not create a temporary address with a zero
1035 * Preferred Lifetime.
1036 */
1037 if (tmp_prefered_lft <= regen_advance) {
1038 in6_ifa_put(ifp);
1039 in6_dev_put(idev);
1040 ret = -1;
1041 goto out;
1042 }
1043
Neil Horman95c385b2007-04-25 17:08:10 -07001044 addr_flags = IFA_F_TEMPORARY;
1045 /* set in addrconf_prefix_rcv() */
1046 if (ifp->flags & IFA_F_OPTIMISTIC)
1047 addr_flags |= IFA_F_OPTIMISTIC;
1048
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 ift = !max_addresses ||
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001050 ipv6_count_addresses(idev) < max_addresses ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 ipv6_add_addr(idev, &addr, tmp_plen,
Neil Horman95c385b2007-04-25 17:08:10 -07001052 ipv6_addr_type(&addr)&IPV6_ADDR_SCOPE_MASK,
1053 addr_flags) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 if (!ift || IS_ERR(ift)) {
1055 in6_ifa_put(ifp);
1056 in6_dev_put(idev);
Joe Perchesf3213832012-05-15 14:11:53 +00001057 pr_info("%s: retry temporary address regeneration\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 tmpaddr = &addr;
1059 write_lock(&idev->lock);
1060 goto retry;
1061 }
1062
1063 spin_lock_bh(&ift->lock);
1064 ift->ifpub = ifp;
1065 ift->valid_lft = tmp_valid_lft;
1066 ift->prefered_lft = tmp_prefered_lft;
Lorenzo Colitti76f793e2011-07-26 13:50:49 +00001067 ift->cstamp = now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 ift->tstamp = tmp_tstamp;
1069 spin_unlock_bh(&ift->lock);
1070
David S. Millercf22f9a2012-04-14 21:37:40 -04001071 addrconf_dad_start(ift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 in6_ifa_put(ift);
1073 in6_dev_put(idev);
1074out:
1075 return ret;
1076}
1077#endif
1078
1079/*
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001080 * Choose an appropriate source address (RFC3484)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 */
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001082enum {
1083 IPV6_SADDR_RULE_INIT = 0,
1084 IPV6_SADDR_RULE_LOCAL,
1085 IPV6_SADDR_RULE_SCOPE,
1086 IPV6_SADDR_RULE_PREFERRED,
1087#ifdef CONFIG_IPV6_MIP6
1088 IPV6_SADDR_RULE_HOA,
1089#endif
1090 IPV6_SADDR_RULE_OIF,
1091 IPV6_SADDR_RULE_LABEL,
1092#ifdef CONFIG_IPV6_PRIVACY
1093 IPV6_SADDR_RULE_PRIVACY,
1094#endif
1095 IPV6_SADDR_RULE_ORCHID,
1096 IPV6_SADDR_RULE_PREFIX,
1097 IPV6_SADDR_RULE_MAX
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001098};
1099
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001100struct ipv6_saddr_score {
1101 int rule;
1102 int addr_type;
1103 struct inet6_ifaddr *ifa;
1104 DECLARE_BITMAP(scorebits, IPV6_SADDR_RULE_MAX);
1105 int scopedist;
1106 int matchlen;
1107};
1108
1109struct ipv6_saddr_dst {
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +09001110 const struct in6_addr *addr;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001111 int ifindex;
1112 int scope;
1113 int label;
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001114 unsigned int prefs;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001115};
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001116
Dave Jonesb6f99a22007-03-22 12:27:49 -07001117static inline int ipv6_saddr_preferred(int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118{
Ulrich Weber45bb0062010-02-25 23:28:58 +00001119 if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|IPV6_ADDR_LOOPBACK))
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001120 return 1;
1121 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122}
1123
Benjamin Thery3de23252008-05-28 14:51:24 +02001124static int ipv6_get_saddr_eval(struct net *net,
1125 struct ipv6_saddr_score *score,
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001126 struct ipv6_saddr_dst *dst,
1127 int i)
1128{
1129 int ret;
1130
1131 if (i <= score->rule) {
1132 switch (i) {
1133 case IPV6_SADDR_RULE_SCOPE:
1134 ret = score->scopedist;
1135 break;
1136 case IPV6_SADDR_RULE_PREFIX:
1137 ret = score->matchlen;
1138 break;
1139 default:
1140 ret = !!test_bit(i, score->scorebits);
1141 }
1142 goto out;
1143 }
1144
1145 switch (i) {
1146 case IPV6_SADDR_RULE_INIT:
1147 /* Rule 0: remember if hiscore is not ready yet */
1148 ret = !!score->ifa;
1149 break;
1150 case IPV6_SADDR_RULE_LOCAL:
1151 /* Rule 1: Prefer same address */
1152 ret = ipv6_addr_equal(&score->ifa->addr, dst->addr);
1153 break;
1154 case IPV6_SADDR_RULE_SCOPE:
1155 /* Rule 2: Prefer appropriate scope
1156 *
1157 * ret
1158 * ^
1159 * -1 | d 15
1160 * ---+--+-+---> scope
1161 * |
1162 * | d is scope of the destination.
1163 * B-d | \
1164 * | \ <- smaller scope is better if
1165 * B-15 | \ if scope is enough for destinaion.
1166 * | ret = B - scope (-1 <= scope >= d <= 15).
1167 * d-C-1 | /
1168 * |/ <- greater is better
1169 * -C / if scope is not enough for destination.
1170 * /| ret = scope - C (-1 <= d < scope <= 15).
1171 *
1172 * d - C - 1 < B -15 (for all -1 <= d <= 15).
1173 * C > d + 14 - B >= 15 + 14 - B = 29 - B.
1174 * Assume B = 0 and we get C > 29.
1175 */
1176 ret = __ipv6_addr_src_scope(score->addr_type);
1177 if (ret >= dst->scope)
1178 ret = -ret;
1179 else
1180 ret -= 128; /* 30 is enough */
1181 score->scopedist = ret;
1182 break;
1183 case IPV6_SADDR_RULE_PREFERRED:
1184 /* Rule 3: Avoid deprecated and optimistic addresses */
1185 ret = ipv6_saddr_preferred(score->addr_type) ||
1186 !(score->ifa->flags & (IFA_F_DEPRECATED|IFA_F_OPTIMISTIC));
1187 break;
1188#ifdef CONFIG_IPV6_MIP6
1189 case IPV6_SADDR_RULE_HOA:
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001190 {
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001191 /* Rule 4: Prefer home address */
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001192 int prefhome = !(dst->prefs & IPV6_PREFER_SRC_COA);
1193 ret = !(score->ifa->flags & IFA_F_HOMEADDRESS) ^ prefhome;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001194 break;
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001195 }
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001196#endif
1197 case IPV6_SADDR_RULE_OIF:
1198 /* Rule 5: Prefer outgoing interface */
1199 ret = (!dst->ifindex ||
1200 dst->ifindex == score->ifa->idev->dev->ifindex);
1201 break;
1202 case IPV6_SADDR_RULE_LABEL:
1203 /* Rule 6: Prefer matching label */
Benjamin Thery3de23252008-05-28 14:51:24 +02001204 ret = ipv6_addr_label(net,
1205 &score->ifa->addr, score->addr_type,
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001206 score->ifa->idev->dev->ifindex) == dst->label;
1207 break;
1208#ifdef CONFIG_IPV6_PRIVACY
1209 case IPV6_SADDR_RULE_PRIVACY:
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001210 {
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001211 /* Rule 7: Prefer public address
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001212 * Note: prefer temporary address if use_tempaddr >= 2
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001213 */
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001214 int preftmp = dst->prefs & (IPV6_PREFER_SRC_PUBLIC|IPV6_PREFER_SRC_TMP) ?
1215 !!(dst->prefs & IPV6_PREFER_SRC_TMP) :
1216 score->ifa->idev->cnf.use_tempaddr >= 2;
1217 ret = (!(score->ifa->flags & IFA_F_TEMPORARY)) ^ preftmp;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001218 break;
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001219 }
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001220#endif
1221 case IPV6_SADDR_RULE_ORCHID:
1222 /* Rule 8-: Prefer ORCHID vs ORCHID or
1223 * non-ORCHID vs non-ORCHID
1224 */
1225 ret = !(ipv6_addr_orchid(&score->ifa->addr) ^
1226 ipv6_addr_orchid(dst->addr));
1227 break;
1228 case IPV6_SADDR_RULE_PREFIX:
1229 /* Rule 8: Use longest matching prefix */
YOSHIFUJI Hideaki / 吉藤英明91b4b042012-09-10 18:41:07 +00001230 ret = ipv6_addr_diff(&score->ifa->addr, dst->addr);
1231 if (ret > score->ifa->prefix_len)
1232 ret = score->ifa->prefix_len;
1233 score->matchlen = ret;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001234 break;
1235 default:
1236 ret = 0;
1237 }
1238
1239 if (ret)
1240 __set_bit(i, score->scorebits);
1241 score->rule = i;
1242out:
1243 return ret;
1244}
1245
Patrick McHardyb3f644f2012-08-26 19:14:14 +02001246int ipv6_dev_get_saddr(struct net *net, const struct net_device *dst_dev,
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +09001247 const struct in6_addr *daddr, unsigned int prefs,
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001248 struct in6_addr *saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249{
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001250 struct ipv6_saddr_score scores[2],
1251 *score = &scores[0], *hiscore = &scores[1];
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001252 struct ipv6_saddr_dst dst;
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001253 struct net_device *dev;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001254 int dst_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001256 dst_type = __ipv6_addr_type(daddr);
1257 dst.addr = daddr;
1258 dst.ifindex = dst_dev ? dst_dev->ifindex : 0;
1259 dst.scope = __ipv6_addr_src_scope(dst_type);
Benjamin Thery3de23252008-05-28 14:51:24 +02001260 dst.label = ipv6_addr_label(net, daddr, dst_type, dst.ifindex);
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001261 dst.prefs = prefs;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001262
1263 hiscore->rule = -1;
1264 hiscore->ifa = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001266 rcu_read_lock();
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001267
Eric Dumazetc6d14c82009-11-04 05:43:23 -08001268 for_each_netdev_rcu(net, dev) {
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001269 struct inet6_dev *idev;
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001270
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001271 /* Candidate Source Address (section 4)
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001272 * - multicast and link-local destination address,
1273 * the set of candidate source address MUST only
1274 * include addresses assigned to interfaces
1275 * belonging to the same link as the outgoing
1276 * interface.
1277 * (- For site-local destination addresses, the
1278 * set of candidate source addresses MUST only
1279 * include addresses assigned to interfaces
1280 * belonging to the same site as the outgoing
1281 * interface.)
1282 */
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001283 if (((dst_type & IPV6_ADDR_MULTICAST) ||
1284 dst.scope <= IPV6_ADDR_SCOPE_LINKLOCAL) &&
1285 dst.ifindex && dev->ifindex != dst.ifindex)
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001286 continue;
1287
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 idev = __in6_dev_get(dev);
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001289 if (!idev)
1290 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001292 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00001293 list_for_each_entry(score->ifa, &idev->addr_list, if_list) {
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001294 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001296 /*
YOSHIFUJI Hideaki6b3ae802005-12-21 22:58:01 +09001297 * - Tentative Address (RFC2462 section 5.4)
1298 * - A tentative address is not considered
1299 * "assigned to an interface" in the traditional
Neil Horman95c385b2007-04-25 17:08:10 -07001300 * sense, unless it is also flagged as optimistic.
YOSHIFUJI Hideaki6b3ae802005-12-21 22:58:01 +09001301 * - Candidate Source Address (section 4)
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001302 * - In any case, anycast addresses, multicast
1303 * addresses, and the unspecified address MUST
1304 * NOT be included in a candidate set.
1305 */
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001306 if ((score->ifa->flags & IFA_F_TENTATIVE) &&
1307 (!(score->ifa->flags & IFA_F_OPTIMISTIC)))
YOSHIFUJI Hideaki6b3ae802005-12-21 22:58:01 +09001308 continue;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001309
1310 score->addr_type = __ipv6_addr_type(&score->ifa->addr);
1311
1312 if (unlikely(score->addr_type == IPV6_ADDR_ANY ||
1313 score->addr_type & IPV6_ADDR_MULTICAST)) {
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001314 LIMIT_NETDEBUG(KERN_DEBUG
Joe Perches3b6d821c2007-11-19 23:47:25 -08001315 "ADDRCONF: unspecified / multicast address "
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001316 "assigned as unicast address on %s",
1317 dev->name);
1318 continue;
1319 }
1320
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001321 score->rule = -1;
1322 bitmap_zero(score->scorebits, IPV6_SADDR_RULE_MAX);
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001323
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001324 for (i = 0; i < IPV6_SADDR_RULE_MAX; i++) {
1325 int minihiscore, miniscore;
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001326
Benjamin Thery3de23252008-05-28 14:51:24 +02001327 minihiscore = ipv6_get_saddr_eval(net, hiscore, &dst, i);
1328 miniscore = ipv6_get_saddr_eval(net, score, &dst, i);
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001329
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001330 if (minihiscore > miniscore) {
1331 if (i == IPV6_SADDR_RULE_SCOPE &&
1332 score->scopedist > 0) {
1333 /*
1334 * special case:
1335 * each remaining entry
1336 * has too small (not enough)
1337 * scope, because ifa entries
1338 * are sorted by their scope
1339 * values.
1340 */
1341 goto try_nextdev;
1342 }
1343 break;
1344 } else if (minihiscore < miniscore) {
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001345 if (hiscore->ifa)
1346 in6_ifa_put(hiscore->ifa);
1347
1348 in6_ifa_hold(score->ifa);
1349
Ilpo Järvinena0bffff2009-03-21 13:36:17 -07001350 swap(hiscore, score);
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001351
1352 /* restore our iterator */
1353 score->ifa = hiscore->ifa;
1354
1355 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 }
1357 }
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001358 }
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001359try_nextdev:
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001360 read_unlock_bh(&idev->lock);
1361 }
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001362 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001364 if (!hiscore->ifa)
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001365 return -EADDRNOTAVAIL;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001366
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001367 *saddr = hiscore->ifa->addr;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001368 in6_ifa_put(hiscore->ifa);
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001369 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370}
YOSHIFUJI Hideaki5e5f3f02008-03-03 21:44:34 +09001371EXPORT_SYMBOL(ipv6_dev_get_saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
Neil Horman95c385b2007-04-25 17:08:10 -07001373int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr,
1374 unsigned char banned_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375{
1376 struct inet6_dev *idev;
1377 int err = -EADDRNOTAVAIL;
1378
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001379 rcu_read_lock();
Stephen Hemmingere21e8462010-03-20 16:09:01 -07001380 idev = __in6_dev_get(dev);
1381 if (idev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 struct inet6_ifaddr *ifp;
1383
1384 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00001385 list_for_each_entry(ifp, &idev->addr_list, if_list) {
1386 if (ifp->scope == IFA_LINK &&
1387 !(ifp->flags & banned_flags)) {
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001388 *addr = ifp->addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 err = 0;
1390 break;
1391 }
1392 }
1393 read_unlock_bh(&idev->lock);
1394 }
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001395 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 return err;
1397}
1398
1399static int ipv6_count_addresses(struct inet6_dev *idev)
1400{
1401 int cnt = 0;
1402 struct inet6_ifaddr *ifp;
1403
1404 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00001405 list_for_each_entry(ifp, &idev->addr_list, if_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 cnt++;
1407 read_unlock_bh(&idev->lock);
1408 return cnt;
1409}
1410
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001411int ipv6_chk_addr(struct net *net, const struct in6_addr *addr,
Daniel Lezcanobfeade02008-01-10 22:43:18 -08001412 struct net_device *dev, int strict)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413{
Stephen Hemmingereedf0422010-05-17 22:27:12 -07001414 struct inet6_ifaddr *ifp;
stephen hemmingerc2e21292010-03-17 20:31:10 +00001415 struct hlist_node *node;
Eric Dumazetddbe5032012-07-18 08:11:12 +00001416 unsigned int hash = inet6_addr_hash(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
stephen hemminger5c578ae2010-03-17 20:31:11 +00001418 rcu_read_lock_bh();
1419 hlist_for_each_entry_rcu(ifp, node, &inet6_addr_lst[hash], addr_lst) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09001420 if (!net_eq(dev_net(ifp->idev->dev), net))
Daniel Lezcanobfeade02008-01-10 22:43:18 -08001421 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 if (ipv6_addr_equal(&ifp->addr, addr) &&
Stephen Hemmingereedf0422010-05-17 22:27:12 -07001423 !(ifp->flags&IFA_F_TENTATIVE) &&
1424 (dev == NULL || ifp->idev->dev == dev ||
1425 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))) {
1426 rcu_read_unlock_bh();
1427 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 }
1429 }
stephen hemminger5c578ae2010-03-17 20:31:11 +00001430
Stephen Hemmingereedf0422010-05-17 22:27:12 -07001431 rcu_read_unlock_bh();
1432 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433}
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +09001434EXPORT_SYMBOL(ipv6_chk_addr);
1435
David S. Miller3e81c6d2010-03-20 16:18:00 -07001436static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
1437 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438{
Eric Dumazetddbe5032012-07-18 08:11:12 +00001439 unsigned int hash = inet6_addr_hash(addr);
stephen hemmingerc2e21292010-03-17 20:31:10 +00001440 struct inet6_ifaddr *ifp;
1441 struct hlist_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
stephen hemmingerc2e21292010-03-17 20:31:10 +00001443 hlist_for_each_entry(ifp, node, &inet6_addr_lst[hash], addr_lst) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09001444 if (!net_eq(dev_net(ifp->idev->dev), net))
Daniel Lezcano06bfe652008-01-10 22:43:42 -08001445 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 if (ipv6_addr_equal(&ifp->addr, addr)) {
1447 if (dev == NULL || ifp->idev->dev == dev)
David S. Miller3e81c6d2010-03-20 16:18:00 -07001448 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 }
1450 }
David S. Miller3e81c6d2010-03-20 16:18:00 -07001451 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452}
1453
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001454int ipv6_chk_prefix(const struct in6_addr *addr, struct net_device *dev)
YOSHIFUJI Hideaki52eeeb82008-03-15 22:54:23 -04001455{
1456 struct inet6_dev *idev;
1457 struct inet6_ifaddr *ifa;
1458 int onlink;
1459
1460 onlink = 0;
1461 rcu_read_lock();
1462 idev = __in6_dev_get(dev);
1463 if (idev) {
1464 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00001465 list_for_each_entry(ifa, &idev->addr_list, if_list) {
YOSHIFUJI Hideaki52eeeb82008-03-15 22:54:23 -04001466 onlink = ipv6_prefix_equal(addr, &ifa->addr,
1467 ifa->prefix_len);
1468 if (onlink)
1469 break;
1470 }
1471 read_unlock_bh(&idev->lock);
1472 }
1473 rcu_read_unlock();
1474 return onlink;
1475}
YOSHIFUJI Hideaki52eeeb82008-03-15 22:54:23 -04001476EXPORT_SYMBOL(ipv6_chk_prefix);
1477
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +09001478struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, const struct in6_addr *addr,
Daniel Lezcano1cab3da2008-01-10 22:44:09 -08001479 struct net_device *dev, int strict)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480{
David S. Millerb79d1d52010-03-25 21:39:21 -07001481 struct inet6_ifaddr *ifp, *result = NULL;
Eric Dumazetddbe5032012-07-18 08:11:12 +00001482 unsigned int hash = inet6_addr_hash(addr);
David S. Millerb79d1d52010-03-25 21:39:21 -07001483 struct hlist_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484
stephen hemminger5c578ae2010-03-17 20:31:11 +00001485 rcu_read_lock_bh();
Eric Dumazet4f70ecc2010-05-03 10:50:14 +00001486 hlist_for_each_entry_rcu_bh(ifp, node, &inet6_addr_lst[hash], addr_lst) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09001487 if (!net_eq(dev_net(ifp->idev->dev), net))
Daniel Lezcano1cab3da2008-01-10 22:44:09 -08001488 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 if (ipv6_addr_equal(&ifp->addr, addr)) {
1490 if (dev == NULL || ifp->idev->dev == dev ||
1491 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
David S. Millerb79d1d52010-03-25 21:39:21 -07001492 result = ifp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 in6_ifa_hold(ifp);
1494 break;
1495 }
1496 }
1497 }
stephen hemminger5c578ae2010-03-17 20:31:11 +00001498 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499
David S. Millerb79d1d52010-03-25 21:39:21 -07001500 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501}
1502
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503/* Gets referenced address, destroys ifaddr */
1504
Brian Haleycc411d02009-09-09 14:41:32 +00001505static void addrconf_dad_stop(struct inet6_ifaddr *ifp, int dad_failed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 if (ifp->flags&IFA_F_PERMANENT) {
1508 spin_lock_bh(&ifp->lock);
1509 addrconf_del_timer(ifp);
1510 ifp->flags |= IFA_F_TENTATIVE;
Brian Haleycc411d02009-09-09 14:41:32 +00001511 if (dad_failed)
1512 ifp->flags |= IFA_F_DADFAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 spin_unlock_bh(&ifp->lock);
Herbert Xue2577a02010-03-13 12:23:29 -08001514 if (dad_failed)
1515 ipv6_ifa_notify(0, ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 in6_ifa_put(ifp);
1517#ifdef CONFIG_IPV6_PRIVACY
1518 } else if (ifp->flags&IFA_F_TEMPORARY) {
1519 struct inet6_ifaddr *ifpub;
1520 spin_lock_bh(&ifp->lock);
1521 ifpub = ifp->ifpub;
1522 if (ifpub) {
1523 in6_ifa_hold(ifpub);
1524 spin_unlock_bh(&ifp->lock);
1525 ipv6_create_tempaddr(ifpub, ifp);
1526 in6_ifa_put(ifpub);
1527 } else {
1528 spin_unlock_bh(&ifp->lock);
1529 }
1530 ipv6_del_addr(ifp);
1531#endif
1532 } else
1533 ipv6_del_addr(ifp);
1534}
1535
Herbert Xuf2344a12010-05-18 15:55:27 -07001536static int addrconf_dad_end(struct inet6_ifaddr *ifp)
1537{
1538 int err = -ENOENT;
1539
1540 spin_lock(&ifp->state_lock);
1541 if (ifp->state == INET6_IFADDR_STATE_DAD) {
1542 ifp->state = INET6_IFADDR_STATE_POSTDAD;
1543 err = 0;
1544 }
1545 spin_unlock(&ifp->state_lock);
1546
1547 return err;
1548}
1549
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09001550void addrconf_dad_failure(struct inet6_ifaddr *ifp)
1551{
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09001552 struct inet6_dev *idev = ifp->idev;
Brian Haley9bdd8d42009-03-18 18:22:48 -07001553
Ursula Braun853dc2e2010-10-24 23:06:43 +00001554 if (addrconf_dad_end(ifp)) {
1555 in6_ifa_put(ifp);
Herbert Xuf2344a12010-05-18 15:55:27 -07001556 return;
Ursula Braun853dc2e2010-10-24 23:06:43 +00001557 }
Herbert Xuf2344a12010-05-18 15:55:27 -07001558
Joe Perchese87cc472012-05-13 21:56:26 +00001559 net_info_ratelimited("%s: IPv6 duplicate address %pI6c detected!\n",
1560 ifp->idev->dev->name, &ifp->addr);
Brian Haley9bdd8d42009-03-18 18:22:48 -07001561
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09001562 if (idev->cnf.accept_dad > 1 && !idev->cnf.disable_ipv6) {
1563 struct in6_addr addr;
1564
1565 addr.s6_addr32[0] = htonl(0xfe800000);
1566 addr.s6_addr32[1] = 0;
1567
1568 if (!ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) &&
1569 ipv6_addr_equal(&ifp->addr, &addr)) {
1570 /* DAD failed for link-local based on MAC address */
1571 idev->cnf.disable_ipv6 = 1;
Brian Haley9bdd8d42009-03-18 18:22:48 -07001572
Joe Perchesf3213832012-05-15 14:11:53 +00001573 pr_info("%s: IPv6 being disabled!\n",
Brian Haley9bdd8d42009-03-18 18:22:48 -07001574 ifp->idev->dev->name);
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09001575 }
1576 }
1577
Brian Haleycc411d02009-09-09 14:41:32 +00001578 addrconf_dad_stop(ifp, 1);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09001579}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580
1581/* Join to solicited addr multicast group. */
1582
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001583void addrconf_join_solict(struct net_device *dev, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584{
1585 struct in6_addr maddr;
1586
1587 if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1588 return;
1589
1590 addrconf_addr_solict_mult(addr, &maddr);
1591 ipv6_dev_mc_inc(dev, &maddr);
1592}
1593
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001594void addrconf_leave_solict(struct inet6_dev *idev, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595{
1596 struct in6_addr maddr;
1597
1598 if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1599 return;
1600
1601 addrconf_addr_solict_mult(addr, &maddr);
1602 __ipv6_dev_mc_dec(idev, &maddr);
1603}
1604
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03001605static void addrconf_join_anycast(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606{
1607 struct in6_addr addr;
Bjørn Mork2bda8a02011-07-05 23:04:13 +00001608 if (ifp->prefix_len == 127) /* RFC 6164 */
1609 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1611 if (ipv6_addr_any(&addr))
1612 return;
1613 ipv6_dev_ac_inc(ifp->idev->dev, &addr);
1614}
1615
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03001616static void addrconf_leave_anycast(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617{
1618 struct in6_addr addr;
YOSHIFUJI Hideaki32019e62011-07-24 11:44:34 +00001619 if (ifp->prefix_len == 127) /* RFC 6164 */
1620 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1622 if (ipv6_addr_any(&addr))
1623 return;
1624 __ipv6_dev_ac_dec(ifp->idev, &addr);
1625}
1626
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08001627static int addrconf_ifid_eui48(u8 *eui, struct net_device *dev)
1628{
1629 if (dev->addr_len != ETH_ALEN)
1630 return -1;
1631 memcpy(eui, dev->dev_addr, 3);
1632 memcpy(eui + 5, dev->dev_addr + 3, 3);
1633
1634 /*
1635 * The zSeries OSA network cards can be shared among various
1636 * OS instances, but the OSA cards have only one MAC address.
1637 * This leads to duplicate address conflicts in conjunction
1638 * with IPv6 if more than one instance uses the same card.
1639 *
1640 * The driver for these cards can deliver a unique 16-bit
1641 * identifier for each instance sharing the same card. It is
1642 * placed instead of 0xFFFE in the interface identifier. The
1643 * "u" bit of the interface identifier is not inverted in this
1644 * case. Hence the resulting interface identifier has local
1645 * scope according to RFC2373.
1646 */
1647 if (dev->dev_id) {
1648 eui[3] = (dev->dev_id >> 8) & 0xFF;
1649 eui[4] = dev->dev_id & 0xFF;
1650 } else {
1651 eui[3] = 0xFF;
1652 eui[4] = 0xFE;
1653 eui[0] ^= 2;
1654 }
1655 return 0;
1656}
1657
alex.bluesman.smirnov@gmail.com06a4c1c2012-05-10 03:25:52 +00001658static int addrconf_ifid_eui64(u8 *eui, struct net_device *dev)
1659{
1660 if (dev->addr_len != IEEE802154_ADDR_LEN)
1661 return -1;
1662 memcpy(eui, dev->dev_addr, 8);
1663 return 0;
1664}
1665
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08001666static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev)
1667{
1668 /* XXX: inherit EUI-64 from other interface -- yoshfuji */
1669 if (dev->addr_len != ARCNET_ALEN)
1670 return -1;
1671 memset(eui, 0, 7);
Eldad Zack8e5e8f32012-04-01 07:49:08 +00001672 eui[7] = *(u8 *)dev->dev_addr;
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08001673 return 0;
1674}
1675
1676static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev)
1677{
1678 if (dev->addr_len != INFINIBAND_ALEN)
1679 return -1;
1680 memcpy(eui, dev->dev_addr + 12, 8);
1681 eui[0] |= 2;
1682 return 0;
1683}
1684
stephen hemmingerc61393e2010-10-04 20:17:53 +00001685static int __ipv6_isatap_ifid(u8 *eui, __be32 addr)
YOSHIFUJI Hideakidfd982b2008-04-10 15:42:09 +09001686{
Sascha Hlusiak9af28512009-05-19 12:56:51 +00001687 if (addr == 0)
1688 return -1;
YOSHIFUJI Hideakidfd982b2008-04-10 15:42:09 +09001689 eui[0] = (ipv4_is_zeronet(addr) || ipv4_is_private_10(addr) ||
1690 ipv4_is_loopback(addr) || ipv4_is_linklocal_169(addr) ||
1691 ipv4_is_private_172(addr) || ipv4_is_test_192(addr) ||
1692 ipv4_is_anycast_6to4(addr) || ipv4_is_private_192(addr) ||
1693 ipv4_is_test_198(addr) || ipv4_is_multicast(addr) ||
1694 ipv4_is_lbcast(addr)) ? 0x00 : 0x02;
1695 eui[1] = 0;
1696 eui[2] = 0x5E;
1697 eui[3] = 0xFE;
1698 memcpy(eui + 4, &addr, 4);
1699 return 0;
1700}
YOSHIFUJI Hideakidfd982b2008-04-10 15:42:09 +09001701
1702static int addrconf_ifid_sit(u8 *eui, struct net_device *dev)
1703{
1704 if (dev->priv_flags & IFF_ISATAP)
1705 return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
1706 return -1;
1707}
1708
stephen hemmingeraee80b52011-06-08 10:44:30 +00001709static int addrconf_ifid_gre(u8 *eui, struct net_device *dev)
1710{
1711 return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
1712}
1713
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
1715{
1716 switch (dev->type) {
1717 case ARPHRD_ETHER:
1718 case ARPHRD_FDDI:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08001719 return addrconf_ifid_eui48(eui, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 case ARPHRD_ARCNET:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08001721 return addrconf_ifid_arcnet(eui, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 case ARPHRD_INFINIBAND:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08001723 return addrconf_ifid_infiniband(eui, dev);
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11001724 case ARPHRD_SIT:
YOSHIFUJI Hideakidfd982b2008-04-10 15:42:09 +09001725 return addrconf_ifid_sit(eui, dev);
stephen hemmingeraee80b52011-06-08 10:44:30 +00001726 case ARPHRD_IPGRE:
1727 return addrconf_ifid_gre(eui, dev);
alex.bluesman.smirnov@gmail.com06a4c1c2012-05-10 03:25:52 +00001728 case ARPHRD_IEEE802154:
1729 return addrconf_ifid_eui64(eui, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 }
1731 return -1;
1732}
1733
1734static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
1735{
1736 int err = -1;
1737 struct inet6_ifaddr *ifp;
1738
1739 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00001740 list_for_each_entry(ifp, &idev->addr_list, if_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
1742 memcpy(eui, ifp->addr.s6_addr+8, 8);
1743 err = 0;
1744 break;
1745 }
1746 }
1747 read_unlock_bh(&idev->lock);
1748 return err;
1749}
1750
1751#ifdef CONFIG_IPV6_PRIVACY
1752/* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
Sorin Dumitrueb7e0572012-08-30 02:01:45 +00001753static void __ipv6_regen_rndid(struct inet6_dev *idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755regen:
YOSHIFUJI Hideaki955189e2006-03-20 16:54:09 -08001756 get_random_bytes(idev->rndid, sizeof(idev->rndid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 idev->rndid[0] &= ~0x02;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758
1759 /*
1760 * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
1761 * check if generated address is not inappropriate
1762 *
1763 * - Reserved subnet anycast (RFC 2526)
1764 * 11111101 11....11 1xxxxxxx
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11001765 * - ISATAP (RFC4214) 6.1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 * 00-00-5E-FE-xx-xx-xx-xx
1767 * - value 0
1768 * - XXX: already assigned to an address on the device
1769 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001770 if (idev->rndid[0] == 0xfd &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff &&
1772 (idev->rndid[7]&0x80))
1773 goto regen;
1774 if ((idev->rndid[0]|idev->rndid[1]) == 0) {
1775 if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe)
1776 goto regen;
1777 if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00)
1778 goto regen;
1779 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780}
1781
1782static void ipv6_regen_rndid(unsigned long data)
1783{
1784 struct inet6_dev *idev = (struct inet6_dev *) data;
1785 unsigned long expires;
1786
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001787 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 write_lock_bh(&idev->lock);
1789
1790 if (idev->dead)
1791 goto out;
1792
Sorin Dumitrueb7e0572012-08-30 02:01:45 +00001793 __ipv6_regen_rndid(idev);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001794
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 expires = jiffies +
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001796 idev->cnf.temp_prefered_lft * HZ -
Ben Hutchings784e2712010-06-26 11:42:55 +00001797 idev->cnf.regen_max_retry * idev->cnf.dad_transmits * idev->nd_parms->retrans_time -
1798 idev->cnf.max_desync_factor * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 if (time_before(expires, jiffies)) {
Joe Perchesf3213832012-05-15 14:11:53 +00001800 pr_warn("%s: too short regeneration interval; timer disabled for %s\n",
1801 __func__, idev->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 goto out;
1803 }
1804
1805 if (!mod_timer(&idev->regen_timer, expires))
1806 in6_dev_hold(idev);
1807
1808out:
1809 write_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001810 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 in6_dev_put(idev);
1812}
1813
Sorin Dumitrueb7e0572012-08-30 02:01:45 +00001814static void __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr)
Eldad Zack8e5e8f32012-04-01 07:49:08 +00001815{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0)
Sorin Dumitrueb7e0572012-08-30 02:01:45 +00001817 __ipv6_regen_rndid(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818}
1819#endif
1820
1821/*
1822 * Add prefix route.
1823 */
1824
1825static void
1826addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev,
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -07001827 unsigned long expires, u32 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828{
Thomas Graf86872cb2006-08-22 00:01:08 -07001829 struct fib6_config cfg = {
1830 .fc_table = RT6_TABLE_PREFIX,
1831 .fc_metric = IP6_RT_PRIO_ADDRCONF,
1832 .fc_ifindex = dev->ifindex,
1833 .fc_expires = expires,
1834 .fc_dst_len = plen,
1835 .fc_flags = RTF_UP | flags,
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001836 .fc_nlinfo.nl_net = dev_net(dev),
Stephen Hemmingerf410a1f2008-08-23 05:16:46 -07001837 .fc_protocol = RTPROT_KERNEL,
Thomas Graf86872cb2006-08-22 00:01:08 -07001838 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001840 cfg.fc_dst = *pfx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841
1842 /* Prevent useless cloning on PtP SIT.
1843 This thing is done here expecting that the whole
1844 class of non-broadcast devices need not cloning.
1845 */
Amerigo Wang07a93622012-10-29 16:23:10 +00001846#if IS_ENABLED(CONFIG_IPV6_SIT)
Thomas Graf86872cb2006-08-22 00:01:08 -07001847 if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT))
1848 cfg.fc_flags |= RTF_NONEXTHOP;
Joerg Roedel0be669b2006-10-10 14:49:53 -07001849#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850
Thomas Graf86872cb2006-08-22 00:01:08 -07001851 ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852}
1853
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00001854
1855static struct rt6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
1856 int plen,
1857 const struct net_device *dev,
1858 u32 flags, u32 noflags)
1859{
1860 struct fib6_node *fn;
1861 struct rt6_info *rt = NULL;
1862 struct fib6_table *table;
1863
1864 table = fib6_get_table(dev_net(dev), RT6_TABLE_PREFIX);
1865 if (table == NULL)
1866 return NULL;
1867
Li RongQing5744dd92012-09-11 21:59:01 +00001868 read_lock_bh(&table->tb6_lock);
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00001869 fn = fib6_locate(&table->tb6_root, pfx, plen, NULL, 0);
1870 if (!fn)
1871 goto out;
1872 for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
David S. Millerd1918542011-12-28 20:19:20 -05001873 if (rt->dst.dev->ifindex != dev->ifindex)
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00001874 continue;
1875 if ((rt->rt6i_flags & flags) != flags)
1876 continue;
Romain Kuntz85da53b2013-01-09 15:02:26 +01001877 if ((rt->rt6i_flags & noflags) != 0)
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00001878 continue;
1879 dst_hold(&rt->dst);
1880 break;
1881 }
1882out:
Li RongQing5744dd92012-09-11 21:59:01 +00001883 read_unlock_bh(&table->tb6_lock);
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00001884 return rt;
1885}
1886
1887
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888/* Create "default" multicast route to the interface */
1889
1890static void addrconf_add_mroute(struct net_device *dev)
1891{
Thomas Graf86872cb2006-08-22 00:01:08 -07001892 struct fib6_config cfg = {
1893 .fc_table = RT6_TABLE_LOCAL,
1894 .fc_metric = IP6_RT_PRIO_ADDRCONF,
1895 .fc_ifindex = dev->ifindex,
1896 .fc_dst_len = 8,
1897 .fc_flags = RTF_UP,
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001898 .fc_nlinfo.nl_net = dev_net(dev),
Thomas Graf86872cb2006-08-22 00:01:08 -07001899 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900
Thomas Graf86872cb2006-08-22 00:01:08 -07001901 ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0);
1902
1903 ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904}
1905
Amerigo Wang07a93622012-10-29 16:23:10 +00001906#if IS_ENABLED(CONFIG_IPV6_SIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907static void sit_route_add(struct net_device *dev)
1908{
Thomas Graf86872cb2006-08-22 00:01:08 -07001909 struct fib6_config cfg = {
1910 .fc_table = RT6_TABLE_MAIN,
1911 .fc_metric = IP6_RT_PRIO_ADDRCONF,
1912 .fc_ifindex = dev->ifindex,
1913 .fc_dst_len = 96,
1914 .fc_flags = RTF_UP | RTF_NONEXTHOP,
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001915 .fc_nlinfo.nl_net = dev_net(dev),
Thomas Graf86872cb2006-08-22 00:01:08 -07001916 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917
1918 /* prefix length - 96 bits "::d.d.d.d" */
Thomas Graf86872cb2006-08-22 00:01:08 -07001919 ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920}
Joerg Roedel0be669b2006-10-10 14:49:53 -07001921#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
1924{
1925 struct inet6_dev *idev;
1926
1927 ASSERT_RTNL();
1928
Stephen Hemmingere21e8462010-03-20 16:09:01 -07001929 idev = ipv6_find_idev(dev);
1930 if (!idev)
Brian Haley64e724f2010-07-20 10:34:30 +00001931 return ERR_PTR(-ENOBUFS);
1932
1933 if (idev->cnf.disable_ipv6)
1934 return ERR_PTR(-EACCES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935
1936 /* Add default multicast route */
Li Wei4af04ab2011-12-06 21:23:45 +00001937 if (!(dev->flags & IFF_LOOPBACK))
1938 addrconf_add_mroute(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 return idev;
1941}
1942
Neil Hormane6bff992012-01-04 10:49:15 +00001943void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len, bool sllao)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944{
1945 struct prefix_info *pinfo;
1946 __u32 valid_lft;
1947 __u32 prefered_lft;
1948 int addr_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 struct inet6_dev *in6_dev;
Brian Haley56d417b2009-06-01 03:07:33 -07001950 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951
1952 pinfo = (struct prefix_info *) opt;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001953
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 if (len < sizeof(struct prefix_info)) {
1955 ADBG(("addrconf: prefix option too short\n"));
1956 return;
1957 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001958
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 /*
1960 * Validation checks ([ADDRCONF], page 19)
1961 */
1962
1963 addr_type = ipv6_addr_type(&pinfo->prefix);
1964
1965 if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
1966 return;
1967
1968 valid_lft = ntohl(pinfo->valid);
1969 prefered_lft = ntohl(pinfo->prefered);
1970
1971 if (prefered_lft > valid_lft) {
Joe Perchese87cc472012-05-13 21:56:26 +00001972 net_warn_ratelimited("addrconf: prefix option has invalid lifetime\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 return;
1974 }
1975
1976 in6_dev = in6_dev_get(dev);
1977
1978 if (in6_dev == NULL) {
Joe Perchese87cc472012-05-13 21:56:26 +00001979 net_dbg_ratelimited("addrconf: device %s not configured\n",
1980 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 return;
1982 }
1983
1984 /*
1985 * Two things going on here:
1986 * 1) Add routes for on-link prefixes
1987 * 2) Configure prefixes with the auto flag set
1988 */
1989
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09001990 if (pinfo->onlink) {
1991 struct rt6_info *rt;
1992 unsigned long rt_expires;
1993
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07001994 /* Avoid arithmetic overflow. Really, we could
1995 * save rt_expires in seconds, likely valid_lft,
1996 * but it would require division in fib gc, that it
1997 * not good.
1998 */
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09001999 if (HZ > USER_HZ)
2000 rt_expires = addrconf_timeout_fixup(valid_lft, HZ);
2001 else
2002 rt_expires = addrconf_timeout_fixup(valid_lft, USER_HZ);
YOSHIFUJI Hideaki3dd4bc62005-12-19 14:02:45 -08002003
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002004 if (addrconf_finite_timeout(rt_expires))
2005 rt_expires *= HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002007 rt = addrconf_get_prefix_route(&pinfo->prefix,
2008 pinfo->prefix_len,
2009 dev,
2010 RTF_ADDRCONF | RTF_PREFIX_RT,
2011 RTF_GATEWAY | RTF_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002013 if (rt) {
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002014 /* Autoconf prefix route */
2015 if (valid_lft == 0) {
2016 ip6_del_rt(rt);
2017 rt = NULL;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002018 } else if (addrconf_finite_timeout(rt_expires)) {
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002019 /* not infinity */
Gao feng1716a962012-04-06 00:13:10 +00002020 rt6_set_expires(rt, jiffies + rt_expires);
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002021 } else {
Gao feng1716a962012-04-06 00:13:10 +00002022 rt6_clean_expires(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 }
2024 } else if (valid_lft) {
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002025 clock_t expires = 0;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002026 int flags = RTF_ADDRCONF | RTF_PREFIX_RT;
2027 if (addrconf_finite_timeout(rt_expires)) {
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002028 /* not infinity */
2029 flags |= RTF_EXPIRES;
2030 expires = jiffies_to_clock_t(rt_expires);
2031 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002033 dev, expires, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 }
Amerigo Wang94e187c2012-10-29 00:13:19 +00002035 ip6_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 }
2037
2038 /* Try to figure out our local address for this prefix */
2039
2040 if (pinfo->autoconf && in6_dev->cnf.autoconf) {
Eldad Zack8e5e8f32012-04-01 07:49:08 +00002041 struct inet6_ifaddr *ifp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 struct in6_addr addr;
2043 int create = 0, update_lft = 0;
2044
2045 if (pinfo->prefix_len == 64) {
2046 memcpy(&addr, &pinfo->prefix, 8);
2047 if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
2048 ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
2049 in6_dev_put(in6_dev);
2050 return;
2051 }
2052 goto ok;
2053 }
Joe Perchese87cc472012-05-13 21:56:26 +00002054 net_dbg_ratelimited("IPv6 addrconf: prefix with wrong length %d\n",
2055 pinfo->prefix_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 in6_dev_put(in6_dev);
2057 return;
2058
2059ok:
2060
YOSHIFUJI Hideaki53b79972008-07-19 22:35:03 -07002061 ifp = ipv6_get_ifaddr(net, &addr, dev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062
2063 if (ifp == NULL && valid_lft) {
2064 int max_addresses = in6_dev->cnf.max_addresses;
Neil Horman95c385b2007-04-25 17:08:10 -07002065 u32 addr_flags = 0;
2066
2067#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
2068 if (in6_dev->cnf.optimistic_dad &&
Neil Hormane6bff992012-01-04 10:49:15 +00002069 !net->ipv6.devconf_all->forwarding && sllao)
Neil Horman95c385b2007-04-25 17:08:10 -07002070 addr_flags = IFA_F_OPTIMISTIC;
2071#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072
2073 /* Do not allow to create too much of autoconfigured
2074 * addresses; this would be too easy way to crash kernel.
2075 */
2076 if (!max_addresses ||
2077 ipv6_count_addresses(in6_dev) < max_addresses)
2078 ifp = ipv6_add_addr(in6_dev, &addr, pinfo->prefix_len,
Neil Horman95c385b2007-04-25 17:08:10 -07002079 addr_type&IPV6_ADDR_SCOPE_MASK,
2080 addr_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081
2082 if (!ifp || IS_ERR(ifp)) {
2083 in6_dev_put(in6_dev);
2084 return;
2085 }
2086
2087 update_lft = create = 1;
2088 ifp->cstamp = jiffies;
David S. Millercf22f9a2012-04-14 21:37:40 -04002089 addrconf_dad_start(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 }
2091
2092 if (ifp) {
2093 int flags;
2094 unsigned long now;
2095#ifdef CONFIG_IPV6_PRIVACY
2096 struct inet6_ifaddr *ift;
2097#endif
2098 u32 stored_lft;
2099
2100 /* update lifetime (RFC2462 5.5.3 e) */
2101 spin_lock(&ifp->lock);
2102 now = jiffies;
2103 if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
2104 stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
2105 else
2106 stored_lft = 0;
2107 if (!update_lft && stored_lft) {
2108 if (valid_lft > MIN_VALID_LIFETIME ||
2109 valid_lft > stored_lft)
2110 update_lft = 1;
2111 else if (stored_lft <= MIN_VALID_LIFETIME) {
2112 /* valid_lft <= stored_lft is always true */
Brian Haleya1ed0522009-07-02 07:10:52 +00002113 /*
2114 * RFC 4862 Section 5.5.3e:
2115 * "Note that the preferred lifetime of
2116 * the corresponding address is always
2117 * reset to the Preferred Lifetime in
2118 * the received Prefix Information
2119 * option, regardless of whether the
2120 * valid lifetime is also reset or
2121 * ignored."
2122 *
2123 * So if the preferred lifetime in
2124 * this advertisement is different
2125 * than what we have stored, but the
2126 * valid lifetime is invalid, just
2127 * reset prefered_lft.
2128 *
2129 * We must set the valid lifetime
2130 * to the stored lifetime since we'll
2131 * be updating the timestamp below,
2132 * else we'll set it back to the
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002133 * minimum.
Brian Haleya1ed0522009-07-02 07:10:52 +00002134 */
2135 if (prefered_lft != ifp->prefered_lft) {
2136 valid_lft = stored_lft;
2137 update_lft = 1;
2138 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 } else {
2140 valid_lft = MIN_VALID_LIFETIME;
2141 if (valid_lft < prefered_lft)
2142 prefered_lft = valid_lft;
2143 update_lft = 1;
2144 }
2145 }
2146
2147 if (update_lft) {
2148 ifp->valid_lft = valid_lft;
2149 ifp->prefered_lft = prefered_lft;
2150 ifp->tstamp = now;
2151 flags = ifp->flags;
2152 ifp->flags &= ~IFA_F_DEPRECATED;
2153 spin_unlock(&ifp->lock);
2154
2155 if (!(flags&IFA_F_TENTATIVE))
2156 ipv6_ifa_notify(0, ifp);
2157 } else
2158 spin_unlock(&ifp->lock);
2159
2160#ifdef CONFIG_IPV6_PRIVACY
2161 read_lock_bh(&in6_dev->lock);
2162 /* update all temporary addresses in the list */
Lorenzo Colitti76f793e2011-07-26 13:50:49 +00002163 list_for_each_entry(ift, &in6_dev->tempaddr_list,
2164 tmp_list) {
2165 int age, max_valid, max_prefered;
2166
Benoit Boissinotc6fbfac2008-04-02 00:00:58 -07002167 if (ifp != ift->ifpub)
2168 continue;
2169
Lorenzo Colitti76f793e2011-07-26 13:50:49 +00002170 /*
2171 * RFC 4941 section 3.3:
2172 * If a received option will extend the lifetime
2173 * of a public address, the lifetimes of
2174 * temporary addresses should be extended,
2175 * subject to the overall constraint that no
2176 * temporary addresses should ever remain
2177 * "valid" or "preferred" for a time longer than
2178 * (TEMP_VALID_LIFETIME) or
2179 * (TEMP_PREFERRED_LIFETIME - DESYNC_FACTOR),
2180 * respectively.
2181 */
2182 age = (now - ift->cstamp) / HZ;
2183 max_valid = in6_dev->cnf.temp_valid_lft - age;
2184 if (max_valid < 0)
2185 max_valid = 0;
2186
2187 max_prefered = in6_dev->cnf.temp_prefered_lft -
2188 in6_dev->cnf.max_desync_factor -
2189 age;
2190 if (max_prefered < 0)
2191 max_prefered = 0;
2192
2193 if (valid_lft > max_valid)
2194 valid_lft = max_valid;
2195
2196 if (prefered_lft > max_prefered)
2197 prefered_lft = max_prefered;
2198
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 spin_lock(&ift->lock);
2200 flags = ift->flags;
Lorenzo Colitti76f793e2011-07-26 13:50:49 +00002201 ift->valid_lft = valid_lft;
2202 ift->prefered_lft = prefered_lft;
2203 ift->tstamp = now;
2204 if (prefered_lft > 0)
2205 ift->flags &= ~IFA_F_DEPRECATED;
2206
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 spin_unlock(&ift->lock);
2208 if (!(flags&IFA_F_TENTATIVE))
2209 ipv6_ifa_notify(0, ift);
2210 }
2211
Glenn Wursteraed65502010-09-27 07:04:30 +00002212 if ((create || list_empty(&in6_dev->tempaddr_list)) && in6_dev->cnf.use_tempaddr > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 /*
Lorenzo Colitti76f793e2011-07-26 13:50:49 +00002214 * When a new public address is created as
2215 * described in [ADDRCONF], also create a new
2216 * temporary address. Also create a temporary
2217 * address if it's enabled but no temporary
2218 * address currently exists.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002220 read_unlock_bh(&in6_dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 ipv6_create_tempaddr(ifp, NULL);
2222 } else {
2223 read_unlock_bh(&in6_dev->lock);
2224 }
2225#endif
2226 in6_ifa_put(ifp);
2227 addrconf_verify(0);
2228 }
2229 }
2230 inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
2231 in6_dev_put(in6_dev);
2232}
2233
2234/*
2235 * Set destination address.
2236 * Special case for SIT interfaces where we create a new "virtual"
2237 * device.
2238 */
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002239int addrconf_set_dstaddr(struct net *net, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240{
2241 struct in6_ifreq ireq;
2242 struct net_device *dev;
2243 int err = -EINVAL;
2244
2245 rtnl_lock();
2246
2247 err = -EFAULT;
2248 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2249 goto err_exit;
2250
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002251 dev = __dev_get_by_index(net, ireq.ifr6_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252
2253 err = -ENODEV;
2254 if (dev == NULL)
2255 goto err_exit;
2256
Amerigo Wang07a93622012-10-29 16:23:10 +00002257#if IS_ENABLED(CONFIG_IPV6_SIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 if (dev->type == ARPHRD_SIT) {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -08002259 const struct net_device_ops *ops = dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 struct ifreq ifr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 struct ip_tunnel_parm p;
2262
2263 err = -EADDRNOTAVAIL;
2264 if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4))
2265 goto err_exit;
2266
2267 memset(&p, 0, sizeof(p));
2268 p.iph.daddr = ireq.ifr6_addr.s6_addr32[3];
2269 p.iph.saddr = 0;
2270 p.iph.version = 4;
2271 p.iph.ihl = 5;
2272 p.iph.protocol = IPPROTO_IPV6;
2273 p.iph.ttl = 64;
Stephen Hemmingerd20b3102008-01-21 00:48:43 -08002274 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -08002276 if (ops->ndo_do_ioctl) {
2277 mm_segment_t oldfs = get_fs();
2278
2279 set_fs(KERNEL_DS);
2280 err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
2281 set_fs(oldfs);
2282 } else
2283 err = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284
2285 if (err == 0) {
2286 err = -ENOBUFS;
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002287 dev = __dev_get_by_name(net, p.name);
2288 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 goto err_exit;
2290 err = dev_open(dev);
2291 }
2292 }
Joerg Roedel0be669b2006-10-10 14:49:53 -07002293#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294
2295err_exit:
2296 rtnl_unlock();
2297 return err;
2298}
2299
2300/*
2301 * Manual configuration of address on an interface
2302 */
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002303static int inet6_addr_add(struct net *net, int ifindex, const struct in6_addr *pfx,
Thomas Graf24ef0da2008-05-28 16:54:22 +02002304 unsigned int plen, __u8 ifa_flags, __u32 prefered_lft,
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002305 __u32 valid_lft)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306{
2307 struct inet6_ifaddr *ifp;
2308 struct inet6_dev *idev;
2309 struct net_device *dev;
2310 int scope;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002311 u32 flags;
2312 clock_t expires;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002313 unsigned long timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314
2315 ASSERT_RTNL();
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002316
Thomas Graf24ef0da2008-05-28 16:54:22 +02002317 if (plen > 128)
2318 return -EINVAL;
2319
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002320 /* check the lifetime */
2321 if (!valid_lft || prefered_lft > valid_lft)
2322 return -EINVAL;
2323
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002324 dev = __dev_get_by_index(net, ifindex);
2325 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 return -ENODEV;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002327
Brian Haley64e724f2010-07-20 10:34:30 +00002328 idev = addrconf_add_dev(dev);
2329 if (IS_ERR(idev))
2330 return PTR_ERR(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331
2332 scope = ipv6_addr_scope(pfx);
2333
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002334 timeout = addrconf_timeout_fixup(valid_lft, HZ);
2335 if (addrconf_finite_timeout(timeout)) {
2336 expires = jiffies_to_clock_t(timeout * HZ);
2337 valid_lft = timeout;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002338 flags = RTF_EXPIRES;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002339 } else {
2340 expires = 0;
2341 flags = 0;
2342 ifa_flags |= IFA_F_PERMANENT;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002343 }
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002344
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002345 timeout = addrconf_timeout_fixup(prefered_lft, HZ);
2346 if (addrconf_finite_timeout(timeout)) {
2347 if (timeout == 0)
2348 ifa_flags |= IFA_F_DEPRECATED;
2349 prefered_lft = timeout;
2350 }
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002351
2352 ifp = ipv6_add_addr(idev, pfx, plen, scope, ifa_flags);
2353
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 if (!IS_ERR(ifp)) {
Herbert Xu06aebfb2006-08-09 16:52:04 -07002355 spin_lock_bh(&ifp->lock);
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002356 ifp->valid_lft = valid_lft;
2357 ifp->prefered_lft = prefered_lft;
2358 ifp->tstamp = jiffies;
Herbert Xu06aebfb2006-08-09 16:52:04 -07002359 spin_unlock_bh(&ifp->lock);
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002360
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09002361 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev,
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002362 expires, flags);
Neil Horman95c385b2007-04-25 17:08:10 -07002363 /*
2364 * Note that section 3.1 of RFC 4429 indicates
2365 * that the Optimistic flag should not be set for
2366 * manually configured addresses
2367 */
David S. Millercf22f9a2012-04-14 21:37:40 -04002368 addrconf_dad_start(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 in6_ifa_put(ifp);
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002370 addrconf_verify(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 return 0;
2372 }
2373
2374 return PTR_ERR(ifp);
2375}
2376
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002377static int inet6_addr_del(struct net *net, int ifindex, const struct in6_addr *pfx,
Thomas Graf24ef0da2008-05-28 16:54:22 +02002378 unsigned int plen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379{
2380 struct inet6_ifaddr *ifp;
2381 struct inet6_dev *idev;
2382 struct net_device *dev;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002383
Thomas Graf24ef0da2008-05-28 16:54:22 +02002384 if (plen > 128)
2385 return -EINVAL;
2386
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002387 dev = __dev_get_by_index(net, ifindex);
2388 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 return -ENODEV;
2390
2391 if ((idev = __in6_dev_get(dev)) == NULL)
2392 return -ENXIO;
2393
2394 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00002395 list_for_each_entry(ifp, &idev->addr_list, if_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 if (ifp->prefix_len == plen &&
2397 ipv6_addr_equal(pfx, &ifp->addr)) {
2398 in6_ifa_hold(ifp);
2399 read_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002400
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 ipv6_del_addr(ifp);
2402
2403 /* If the last address is deleted administratively,
2404 disable IPv6 on this interface.
2405 */
stephen hemminger502a2ff2010-03-17 20:31:13 +00002406 if (list_empty(&idev->addr_list))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 addrconf_ifdown(idev->dev, 1);
2408 return 0;
2409 }
2410 }
2411 read_unlock_bh(&idev->lock);
2412 return -EADDRNOTAVAIL;
2413}
2414
2415
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002416int addrconf_add_ifaddr(struct net *net, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417{
2418 struct in6_ifreq ireq;
2419 int err;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002420
Eric W. Biedermanaf31f412012-11-16 03:03:06 +00002421 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 return -EPERM;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002423
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2425 return -EFAULT;
2426
2427 rtnl_lock();
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002428 err = inet6_addr_add(net, ireq.ifr6_ifindex, &ireq.ifr6_addr,
2429 ireq.ifr6_prefixlen, IFA_F_PERMANENT,
2430 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 rtnl_unlock();
2432 return err;
2433}
2434
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002435int addrconf_del_ifaddr(struct net *net, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436{
2437 struct in6_ifreq ireq;
2438 int err;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002439
Eric W. Biedermanaf31f412012-11-16 03:03:06 +00002440 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 return -EPERM;
2442
2443 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2444 return -EFAULT;
2445
2446 rtnl_lock();
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002447 err = inet6_addr_del(net, ireq.ifr6_ifindex, &ireq.ifr6_addr,
2448 ireq.ifr6_prefixlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 rtnl_unlock();
2450 return err;
2451}
2452
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08002453static void add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
2454 int plen, int scope)
2455{
2456 struct inet6_ifaddr *ifp;
2457
2458 ifp = ipv6_add_addr(idev, addr, plen, scope, IFA_F_PERMANENT);
2459 if (!IS_ERR(ifp)) {
2460 spin_lock_bh(&ifp->lock);
2461 ifp->flags &= ~IFA_F_TENTATIVE;
2462 spin_unlock_bh(&ifp->lock);
2463 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2464 in6_ifa_put(ifp);
2465 }
2466}
2467
Amerigo Wang07a93622012-10-29 16:23:10 +00002468#if IS_ENABLED(CONFIG_IPV6_SIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469static void sit_add_v4_addrs(struct inet6_dev *idev)
2470{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 struct in6_addr addr;
2472 struct net_device *dev;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002473 struct net *net = dev_net(idev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 int scope;
2475
2476 ASSERT_RTNL();
2477
2478 memset(&addr, 0, sizeof(struct in6_addr));
2479 memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
2480
2481 if (idev->dev->flags&IFF_POINTOPOINT) {
2482 addr.s6_addr32[0] = htonl(0xfe800000);
2483 scope = IFA_LINK;
2484 } else {
2485 scope = IPV6_ADDR_COMPATv4;
2486 }
2487
2488 if (addr.s6_addr32[3]) {
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08002489 add_addr(idev, &addr, 128, scope);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 return;
2491 }
2492
Benjamin Thery6fda7352008-03-05 10:47:47 -08002493 for_each_netdev(net, dev) {
Eldad Zack8e5e8f32012-04-01 07:49:08 +00002494 struct in_device *in_dev = __in_dev_get_rtnl(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 if (in_dev && (dev->flags & IFF_UP)) {
Eldad Zack8e5e8f32012-04-01 07:49:08 +00002496 struct in_ifaddr *ifa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497
2498 int flag = scope;
2499
2500 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
2501 int plen;
2502
2503 addr.s6_addr32[3] = ifa->ifa_local;
2504
2505 if (ifa->ifa_scope == RT_SCOPE_LINK)
2506 continue;
2507 if (ifa->ifa_scope >= RT_SCOPE_HOST) {
2508 if (idev->dev->flags&IFF_POINTOPOINT)
2509 continue;
2510 flag |= IFA_HOST;
2511 }
2512 if (idev->dev->flags&IFF_POINTOPOINT)
2513 plen = 64;
2514 else
2515 plen = 96;
2516
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08002517 add_addr(idev, &addr, plen, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 }
2519 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002520 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521}
Joerg Roedel0be669b2006-10-10 14:49:53 -07002522#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523
2524static void init_loopback(struct net_device *dev)
2525{
2526 struct inet6_dev *idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527
2528 /* ::1 */
2529
2530 ASSERT_RTNL();
2531
2532 if ((idev = ipv6_find_idev(dev)) == NULL) {
Joe Perches91df42b2012-05-15 14:11:54 +00002533 pr_debug("%s: add_dev failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 return;
2535 }
2536
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08002537 add_addr(idev, &in6addr_loopback, 128, IFA_HOST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538}
2539
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002540static void addrconf_add_linklocal(struct inet6_dev *idev, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541{
Eldad Zack8e5e8f32012-04-01 07:49:08 +00002542 struct inet6_ifaddr *ifp;
Neil Horman95c385b2007-04-25 17:08:10 -07002543 u32 addr_flags = IFA_F_PERMANENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544
Neil Horman95c385b2007-04-25 17:08:10 -07002545#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
2546 if (idev->cnf.optimistic_dad &&
David Miller702beb82008-07-20 18:17:02 -07002547 !dev_net(idev->dev)->ipv6.devconf_all->forwarding)
Neil Horman95c385b2007-04-25 17:08:10 -07002548 addr_flags |= IFA_F_OPTIMISTIC;
2549#endif
2550
2551
2552 ifp = ipv6_add_addr(idev, addr, 64, IFA_LINK, addr_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 if (!IS_ERR(ifp)) {
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09002554 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, idev->dev, 0, 0);
David S. Millercf22f9a2012-04-14 21:37:40 -04002555 addrconf_dad_start(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 in6_ifa_put(ifp);
2557 }
2558}
2559
2560static void addrconf_dev_config(struct net_device *dev)
2561{
2562 struct in6_addr addr;
Eldad Zack8e5e8f32012-04-01 07:49:08 +00002563 struct inet6_dev *idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564
2565 ASSERT_RTNL();
2566
Herbert Xu74235a22007-06-14 13:02:55 -07002567 if ((dev->type != ARPHRD_ETHER) &&
2568 (dev->type != ARPHRD_FDDI) &&
Herbert Xu74235a22007-06-14 13:02:55 -07002569 (dev->type != ARPHRD_ARCNET) &&
alex.bluesman.smirnov@gmail.com06a4c1c2012-05-10 03:25:52 +00002570 (dev->type != ARPHRD_INFINIBAND) &&
2571 (dev->type != ARPHRD_IEEE802154)) {
Herbert Xu74235a22007-06-14 13:02:55 -07002572 /* Alas, we support only Ethernet autoconfiguration. */
2573 return;
2574 }
2575
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 idev = addrconf_add_dev(dev);
Brian Haley64e724f2010-07-20 10:34:30 +00002577 if (IS_ERR(idev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 return;
2579
2580 memset(&addr, 0, sizeof(struct in6_addr));
2581 addr.s6_addr32[0] = htonl(0xFE800000);
2582
2583 if (ipv6_generate_eui64(addr.s6_addr + 8, dev) == 0)
2584 addrconf_add_linklocal(idev, &addr);
2585}
2586
Amerigo Wang07a93622012-10-29 16:23:10 +00002587#if IS_ENABLED(CONFIG_IPV6_SIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588static void addrconf_sit_config(struct net_device *dev)
2589{
2590 struct inet6_dev *idev;
2591
2592 ASSERT_RTNL();
2593
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002594 /*
2595 * Configure the tunnel with one of our IPv4
2596 * addresses... we should configure all of
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 * our v4 addrs in the tunnel
2598 */
2599
2600 if ((idev = ipv6_find_idev(dev)) == NULL) {
Joe Perches91df42b2012-05-15 14:11:54 +00002601 pr_debug("%s: add_dev failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 return;
2603 }
2604
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11002605 if (dev->priv_flags & IFF_ISATAP) {
2606 struct in6_addr addr;
2607
2608 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
2609 addrconf_prefix_route(&addr, 64, dev, 0, 0);
2610 if (!ipv6_generate_eui64(addr.s6_addr + 8, dev))
2611 addrconf_add_linklocal(idev, &addr);
2612 return;
2613 }
2614
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 sit_add_v4_addrs(idev);
2616
Nicolas Dichtel62b54dd2012-10-01 23:19:14 +00002617 if (dev->flags&IFF_POINTOPOINT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 addrconf_add_mroute(dev);
Nicolas Dichtel62b54dd2012-10-01 23:19:14 +00002619 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 sit_route_add(dev);
2621}
Joerg Roedel0be669b2006-10-10 14:49:53 -07002622#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623
Amerigo Wang07a93622012-10-29 16:23:10 +00002624#if IS_ENABLED(CONFIG_NET_IPGRE)
stephen hemmingeraee80b52011-06-08 10:44:30 +00002625static void addrconf_gre_config(struct net_device *dev)
2626{
2627 struct inet6_dev *idev;
2628 struct in6_addr addr;
2629
Joe Perchesf3213832012-05-15 14:11:53 +00002630 pr_info("%s(%s)\n", __func__, dev->name);
stephen hemmingeraee80b52011-06-08 10:44:30 +00002631
2632 ASSERT_RTNL();
2633
2634 if ((idev = ipv6_find_idev(dev)) == NULL) {
Joe Perches91df42b2012-05-15 14:11:54 +00002635 pr_debug("%s: add_dev failed\n", __func__);
stephen hemmingeraee80b52011-06-08 10:44:30 +00002636 return;
2637 }
2638
2639 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
2640 addrconf_prefix_route(&addr, 64, dev, 0, 0);
2641
2642 if (!ipv6_generate_eui64(addr.s6_addr + 8, dev))
2643 addrconf_add_linklocal(idev, &addr);
2644}
2645#endif
2646
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647static inline int
2648ipv6_inherit_linklocal(struct inet6_dev *idev, struct net_device *link_dev)
2649{
2650 struct in6_addr lladdr;
2651
Neil Horman95c385b2007-04-25 17:08:10 -07002652 if (!ipv6_get_lladdr(link_dev, &lladdr, IFA_F_TENTATIVE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 addrconf_add_linklocal(idev, &lladdr);
2654 return 0;
2655 }
2656 return -1;
2657}
2658
2659static void ip6_tnl_add_linklocal(struct inet6_dev *idev)
2660{
2661 struct net_device *link_dev;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002662 struct net *net = dev_net(idev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663
2664 /* first try to inherit the link-local address from the link device */
2665 if (idev->dev->iflink &&
Benjamin Thery6fda7352008-03-05 10:47:47 -08002666 (link_dev = __dev_get_by_index(net, idev->dev->iflink))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 if (!ipv6_inherit_linklocal(idev, link_dev))
2668 return;
2669 }
2670 /* then try to inherit it from any device */
Benjamin Thery6fda7352008-03-05 10:47:47 -08002671 for_each_netdev(net, link_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 if (!ipv6_inherit_linklocal(idev, link_dev))
2673 return;
2674 }
Joe Perches91df42b2012-05-15 14:11:54 +00002675 pr_debug("init ip6-ip6: add_linklocal failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676}
2677
2678/*
2679 * Autoconfigure tunnel with a link-local address so routing protocols,
2680 * DHCPv6, MLD etc. can be run over the virtual link
2681 */
2682
2683static void addrconf_ip6_tnl_config(struct net_device *dev)
2684{
2685 struct inet6_dev *idev;
2686
2687 ASSERT_RTNL();
2688
Stephen Hemmingere21e8462010-03-20 16:09:01 -07002689 idev = addrconf_add_dev(dev);
Brian Haley64e724f2010-07-20 10:34:30 +00002690 if (IS_ERR(idev)) {
Joe Perches91df42b2012-05-15 14:11:54 +00002691 pr_debug("init ip6-ip6: add_dev failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 return;
2693 }
2694 ip6_tnl_add_linklocal(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695}
2696
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002697static int addrconf_notify(struct notifier_block *this, unsigned long event,
Eldad Zack8e5e8f32012-04-01 07:49:08 +00002698 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699{
2700 struct net_device *dev = (struct net_device *) data;
Eric Dumazet748e2d92012-08-22 21:50:59 +00002701 struct inet6_dev *idev = __in6_dev_get(dev);
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09002702 int run_pending = 0;
Herbert Xub217d612007-07-30 17:04:52 -07002703 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704
Stephen Hemmingere21e8462010-03-20 16:09:01 -07002705 switch (event) {
YOSHIFUJI Hideaki45ba9dd2007-02-15 02:07:27 +09002706 case NETDEV_REGISTER:
Herbert Xu74235a22007-06-14 13:02:55 -07002707 if (!idev && dev->mtu >= IPV6_MIN_MTU) {
YOSHIFUJI Hideaki45ba9dd2007-02-15 02:07:27 +09002708 idev = ipv6_add_dev(dev);
2709 if (!idev)
Herbert Xub217d612007-07-30 17:04:52 -07002710 return notifier_from_errno(-ENOMEM);
YOSHIFUJI Hideaki45ba9dd2007-02-15 02:07:27 +09002711 }
2712 break;
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07002713
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 case NETDEV_UP:
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002715 case NETDEV_CHANGE:
Jay Vosburghc2edacf2007-07-09 10:42:47 -07002716 if (dev->flags & IFF_SLAVE)
2717 break;
2718
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002719 if (event == NETDEV_UP) {
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -07002720 if (!addrconf_qdisc_ok(dev)) {
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002721 /* device is not ready yet. */
Joe Perchesf3213832012-05-15 14:11:53 +00002722 pr_info("ADDRCONF(NETDEV_UP): %s: link is not ready\n",
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002723 dev->name);
2724 break;
2725 }
Kristian Slavov99081042006-02-08 16:10:53 -08002726
Evgeniy Polyakovd31c7b82007-11-30 23:36:08 +11002727 if (!idev && dev->mtu >= IPV6_MIN_MTU)
2728 idev = ipv6_add_dev(dev);
2729
Benjamin Therye3ec6cf2008-11-05 01:43:57 -08002730 if (idev) {
Kristian Slavov99081042006-02-08 16:10:53 -08002731 idev->if_flags |= IF_READY;
Benjamin Therye3ec6cf2008-11-05 01:43:57 -08002732 run_pending = 1;
2733 }
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002734 } else {
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -07002735 if (!addrconf_qdisc_ok(dev)) {
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002736 /* device is still not ready. */
2737 break;
2738 }
2739
2740 if (idev) {
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07002741 if (idev->if_flags & IF_READY)
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002742 /* device is already configured. */
2743 break;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002744 idev->if_flags |= IF_READY;
2745 }
2746
Joe Perchesf3213832012-05-15 14:11:53 +00002747 pr_info("ADDRCONF(NETDEV_CHANGE): %s: link becomes ready\n",
2748 dev->name);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002749
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09002750 run_pending = 1;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002751 }
2752
Stephen Hemmingere21e8462010-03-20 16:09:01 -07002753 switch (dev->type) {
Amerigo Wang07a93622012-10-29 16:23:10 +00002754#if IS_ENABLED(CONFIG_IPV6_SIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 case ARPHRD_SIT:
2756 addrconf_sit_config(dev);
2757 break;
Joerg Roedel0be669b2006-10-10 14:49:53 -07002758#endif
Amerigo Wang07a93622012-10-29 16:23:10 +00002759#if IS_ENABLED(CONFIG_NET_IPGRE)
stephen hemmingeraee80b52011-06-08 10:44:30 +00002760 case ARPHRD_IPGRE:
2761 addrconf_gre_config(dev);
2762 break;
2763#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 case ARPHRD_TUNNEL6:
2765 addrconf_ip6_tnl_config(dev);
2766 break;
2767 case ARPHRD_LOOPBACK:
2768 init_loopback(dev);
2769 break;
2770
2771 default:
2772 addrconf_dev_config(dev);
2773 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002774 }
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07002775
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 if (idev) {
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09002777 if (run_pending)
2778 addrconf_dad_run(idev);
2779
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07002780 /*
2781 * If the MTU changed during the interface down,
2782 * when the interface up, the changed MTU must be
2783 * reflected in the idev as well as routers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 */
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07002785 if (idev->cnf.mtu6 != dev->mtu &&
2786 dev->mtu >= IPV6_MIN_MTU) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 rt6_mtu_change(dev, dev->mtu);
2788 idev->cnf.mtu6 = dev->mtu;
2789 }
2790 idev->tstamp = jiffies;
2791 inet6_ifinfo_notify(RTM_NEWLINK, idev);
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07002792
2793 /*
2794 * If the changed mtu during down is lower than
2795 * IPV6_MIN_MTU stop IPv6 on this interface.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 */
2797 if (dev->mtu < IPV6_MIN_MTU)
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07002798 addrconf_ifdown(dev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 }
2800 break;
2801
2802 case NETDEV_CHANGEMTU:
Evgeniy Polyakovd31c7b82007-11-30 23:36:08 +11002803 if (idev && dev->mtu >= IPV6_MIN_MTU) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 rt6_mtu_change(dev, dev->mtu);
2805 idev->cnf.mtu6 = dev->mtu;
2806 break;
2807 }
2808
Evgeniy Polyakovd31c7b82007-11-30 23:36:08 +11002809 if (!idev && dev->mtu >= IPV6_MIN_MTU) {
2810 idev = ipv6_add_dev(dev);
2811 if (idev)
2812 break;
2813 }
2814
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07002815 /*
2816 * MTU falled under IPV6_MIN_MTU.
2817 * Stop IPv6 on this interface.
2818 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819
2820 case NETDEV_DOWN:
2821 case NETDEV_UNREGISTER:
2822 /*
2823 * Remove all addresses from this interface.
2824 */
2825 addrconf_ifdown(dev, event != NETDEV_DOWN);
2826 break;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002827
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 case NETDEV_CHANGENAME:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 if (idev) {
Stephen Hemminger5632c512007-04-28 21:16:39 -07002830 snmp6_unregister_dev(idev);
Pavel Emelyanov408c4762008-01-10 17:41:21 -08002831 addrconf_sysctl_unregister(idev);
Pavel Emelyanovf52295a2007-12-02 00:58:37 +11002832 addrconf_sysctl_register(idev);
Herbert Xub217d612007-07-30 17:04:52 -07002833 err = snmp6_register_dev(idev);
2834 if (err)
2835 return notifier_from_errno(err);
Stephen Hemminger5632c512007-04-28 21:16:39 -07002836 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 break;
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07002838
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00002839 case NETDEV_PRE_TYPE_CHANGE:
2840 case NETDEV_POST_TYPE_CHANGE:
2841 addrconf_type_change(dev, event);
Moni Shoua75c78502009-09-15 02:37:40 -07002842 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002843 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844
2845 return NOTIFY_OK;
2846}
2847
2848/*
2849 * addrconf module should be notified of a device going up
2850 */
2851static struct notifier_block ipv6_dev_notf = {
2852 .notifier_call = addrconf_notify,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853};
2854
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00002855static void addrconf_type_change(struct net_device *dev, unsigned long event)
Moni Shoua75c78502009-09-15 02:37:40 -07002856{
2857 struct inet6_dev *idev;
2858 ASSERT_RTNL();
2859
2860 idev = __in6_dev_get(dev);
2861
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00002862 if (event == NETDEV_POST_TYPE_CHANGE)
Moni Shoua75c78502009-09-15 02:37:40 -07002863 ipv6_mc_remap(idev);
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00002864 else if (event == NETDEV_PRE_TYPE_CHANGE)
Moni Shoua75c78502009-09-15 02:37:40 -07002865 ipv6_mc_unmap(idev);
2866}
2867
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868static int addrconf_ifdown(struct net_device *dev, int how)
2869{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002870 struct net *net = dev_net(dev);
stephen hemminger502a2ff2010-03-17 20:31:13 +00002871 struct inet6_dev *idev;
2872 struct inet6_ifaddr *ifa;
David S. Miller73a8bd72011-01-23 23:27:15 -08002873 int state, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874
2875 ASSERT_RTNL();
2876
David S. Miller73a8bd72011-01-23 23:27:15 -08002877 rt6_ifdown(net, dev);
2878 neigh_ifdown(&nd_tbl, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879
2880 idev = __in6_dev_get(dev);
2881 if (idev == NULL)
2882 return -ENODEV;
2883
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07002884 /*
2885 * Step 1: remove reference to ipv6 device from parent device.
2886 * Do not dev_put!
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 */
Denis V. Lunev439e2382008-04-03 13:30:17 -07002888 if (how) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 idev->dead = 1;
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07002890
2891 /* protected by rtnl_lock */
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00002892 RCU_INIT_POINTER(dev->ip6_ptr, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893
2894 /* Step 1.5: remove snmp6 entry */
2895 snmp6_unregister_dev(idev);
2896
2897 }
2898
David S. Miller73a8bd72011-01-23 23:27:15 -08002899 /* Step 2: clear hash table */
2900 for (i = 0; i < IN6_ADDR_HSIZE; i++) {
2901 struct hlist_head *h = &inet6_addr_lst[i];
2902 struct hlist_node *n;
2903
2904 spin_lock_bh(&addrconf_hash_lock);
2905 restart:
2906 hlist_for_each_entry_rcu(ifa, n, h, addr_lst) {
2907 if (ifa->idev == idev) {
2908 hlist_del_init_rcu(&ifa->addr_lst);
2909 addrconf_del_timer(ifa);
2910 goto restart;
2911 }
2912 }
2913 spin_unlock_bh(&addrconf_hash_lock);
2914 }
2915
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 write_lock_bh(&idev->lock);
2917
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07002918 /* Step 2: clear flags for stateless addrconf */
Denis V. Lunev439e2382008-04-03 13:30:17 -07002919 if (!how)
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002920 idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922#ifdef CONFIG_IPV6_PRIVACY
Denis V. Lunev439e2382008-04-03 13:30:17 -07002923 if (how && del_timer(&idev->regen_timer))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 in6_dev_put(idev);
2925
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07002926 /* Step 3: clear tempaddr list */
stephen hemminger372e6c82010-03-17 20:31:09 +00002927 while (!list_empty(&idev->tempaddr_list)) {
2928 ifa = list_first_entry(&idev->tempaddr_list,
2929 struct inet6_ifaddr, tmp_list);
2930 list_del(&ifa->tmp_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 write_unlock_bh(&idev->lock);
2932 spin_lock_bh(&ifa->lock);
2933
2934 if (ifa->ifpub) {
2935 in6_ifa_put(ifa->ifpub);
2936 ifa->ifpub = NULL;
2937 }
2938 spin_unlock_bh(&ifa->lock);
2939 in6_ifa_put(ifa);
2940 write_lock_bh(&idev->lock);
2941 }
2942#endif
stephen hemminger8f37ada2010-03-03 08:19:59 +00002943
stephen hemminger502a2ff2010-03-17 20:31:13 +00002944 while (!list_empty(&idev->addr_list)) {
2945 ifa = list_first_entry(&idev->addr_list,
2946 struct inet6_ifaddr, if_list);
stephen hemminger84e8b802010-03-02 13:32:46 +00002947 addrconf_del_timer(ifa);
2948
David S. Miller73a8bd72011-01-23 23:27:15 -08002949 list_del(&ifa->if_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950
David S. Miller73a8bd72011-01-23 23:27:15 -08002951 write_unlock_bh(&idev->lock);
stephen hemminger84e8b802010-03-02 13:32:46 +00002952
David S. Miller73a8bd72011-01-23 23:27:15 -08002953 spin_lock_bh(&ifa->state_lock);
2954 state = ifa->state;
2955 ifa->state = INET6_IFADDR_STATE_DEAD;
2956 spin_unlock_bh(&ifa->state_lock);
stephen hemminger84e8b802010-03-02 13:32:46 +00002957
David S. Miller73a8bd72011-01-23 23:27:15 -08002958 if (state != INET6_IFADDR_STATE_DEAD) {
2959 __ipv6_ifa_notify(RTM_DELADDR, ifa);
2960 atomic_notifier_call_chain(&inet6addr_chain, NETDEV_DOWN, ifa);
stephen hemminger27bdb2a2010-04-12 05:41:32 +00002961 }
David S. Miller73a8bd72011-01-23 23:27:15 -08002962 in6_ifa_put(ifa);
stephen hemminger8f37ada2010-03-03 08:19:59 +00002963
David S. Miller73a8bd72011-01-23 23:27:15 -08002964 write_lock_bh(&idev->lock);
2965 }
stephen hemminger8f37ada2010-03-03 08:19:59 +00002966
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 write_unlock_bh(&idev->lock);
2968
2969 /* Step 5: Discard multicast list */
Denis V. Lunev439e2382008-04-03 13:30:17 -07002970 if (how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 ipv6_mc_destroy_dev(idev);
2972 else
2973 ipv6_mc_down(idev);
2974
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 idev->tstamp = jiffies;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002976
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07002977 /* Last: Shot the device (if unregistered) */
Denis V. Lunev439e2382008-04-03 13:30:17 -07002978 if (how) {
Pavel Emelyanov408c4762008-01-10 17:41:21 -08002979 addrconf_sysctl_unregister(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 neigh_parms_release(&nd_tbl, idev->nd_parms);
2981 neigh_ifdown(&nd_tbl, dev);
2982 in6_dev_put(idev);
2983 }
2984 return 0;
2985}
2986
2987static void addrconf_rs_timer(unsigned long data)
2988{
2989 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
stephen hemminger5b2a1952010-03-02 13:32:45 +00002990 struct inet6_dev *idev = ifp->idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991
stephen hemminger5b2a1952010-03-02 13:32:45 +00002992 read_lock(&idev->lock);
2993 if (idev->dead || !(idev->if_flags & IF_READY))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 goto out;
2995
Shmulik Ladkani9ba2add2012-12-02 01:44:53 +00002996 if (!ipv6_accept_ra(idev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 goto out;
stephen hemminger5b2a1952010-03-02 13:32:45 +00002998
2999 /* Announcement received after solicitation was sent */
3000 if (idev->if_flags & IF_RA_RCVD)
3001 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002
3003 spin_lock(&ifp->lock);
stephen hemminger5b2a1952010-03-02 13:32:45 +00003004 if (ifp->probes++ < idev->cnf.rtr_solicits) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 /* The wait after the last probe can be shorter */
3006 addrconf_mod_timer(ifp, AC_RS,
stephen hemminger5b2a1952010-03-02 13:32:45 +00003007 (ifp->probes == idev->cnf.rtr_solicits) ?
3008 idev->cnf.rtr_solicit_delay :
3009 idev->cnf.rtr_solicit_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 spin_unlock(&ifp->lock);
3011
stephen hemminger5b2a1952010-03-02 13:32:45 +00003012 ndisc_send_rs(idev->dev, &ifp->addr, &in6addr_linklocal_allrouters);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 } else {
3014 spin_unlock(&ifp->lock);
3015 /*
3016 * Note: we do not support deprecated "all on-link"
3017 * assumption any longer.
3018 */
Joe Perches91df42b2012-05-15 14:11:54 +00003019 pr_debug("%s: no IPv6 routers present\n", idev->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 }
3021
3022out:
stephen hemminger5b2a1952010-03-02 13:32:45 +00003023 read_unlock(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 in6_ifa_put(ifp);
3025}
3026
3027/*
3028 * Duplicate Address Detection
3029 */
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003030static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
3031{
3032 unsigned long rand_num;
3033 struct inet6_dev *idev = ifp->idev;
3034
Neil Horman95c385b2007-04-25 17:08:10 -07003035 if (ifp->flags & IFA_F_OPTIMISTIC)
3036 rand_num = 0;
3037 else
3038 rand_num = net_random() % (idev->cnf.rtr_solicit_delay ? : 1);
3039
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003040 ifp->probes = idev->cnf.dad_transmits;
3041 addrconf_mod_timer(ifp, AC_DAD, rand_num);
3042}
3043
David S. Millercf22f9a2012-04-14 21:37:40 -04003044static void addrconf_dad_start(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045{
3046 struct inet6_dev *idev = ifp->idev;
3047 struct net_device *dev = idev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048
3049 addrconf_join_solict(dev, &ifp->addr);
3050
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 net_srandom(ifp->addr.s6_addr32[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052
3053 read_lock_bh(&idev->lock);
Herbert Xu622ccdf2010-05-18 15:56:06 -07003054 spin_lock(&ifp->lock);
Herbert Xue9d3e082010-05-18 15:36:06 -07003055 if (ifp->state == INET6_IFADDR_STATE_DEAD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057
3058 if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09003059 idev->cnf.accept_dad < 1 ||
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003060 !(ifp->flags&IFA_F_TENTATIVE) ||
3061 ifp->flags & IFA_F_NODAD) {
Brian Haleycc411d02009-09-09 14:41:32 +00003062 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
stephen hemminger21809fa2010-02-08 19:48:52 +00003063 spin_unlock(&ifp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 read_unlock_bh(&idev->lock);
3065
3066 addrconf_dad_completed(ifp);
3067 return;
3068 }
3069
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08003070 if (!(idev->if_flags & IF_READY)) {
stephen hemminger21809fa2010-02-08 19:48:52 +00003071 spin_unlock(&ifp->lock);
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08003072 read_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003073 /*
Masatake YAMATO590a9882009-06-09 10:41:12 +09003074 * If the device is not ready:
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003075 * - keep it tentative if it is a permanent address.
3076 * - otherwise, kill it.
3077 */
3078 in6_ifa_hold(ifp);
Brian Haleycc411d02009-09-09 14:41:32 +00003079 addrconf_dad_stop(ifp, 0);
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08003080 return;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003081 }
Neil Horman95c385b2007-04-25 17:08:10 -07003082
3083 /*
3084 * Optimistic nodes can start receiving
3085 * Frames right away
3086 */
Stephen Hemmingere21e8462010-03-20 16:09:01 -07003087 if (ifp->flags & IFA_F_OPTIMISTIC)
Neil Horman95c385b2007-04-25 17:08:10 -07003088 ip6_ins_rt(ifp->rt);
3089
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08003090 addrconf_dad_kick(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091out:
Herbert Xu622ccdf2010-05-18 15:56:06 -07003092 spin_unlock(&ifp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 read_unlock_bh(&idev->lock);
3094}
3095
3096static void addrconf_dad_timer(unsigned long data)
3097{
3098 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
3099 struct inet6_dev *idev = ifp->idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100 struct in6_addr mcaddr;
3101
Herbert Xuf2344a12010-05-18 15:55:27 -07003102 if (!ifp->probes && addrconf_dad_end(ifp))
3103 goto out;
3104
stephen hemminger122e4512010-03-02 13:32:44 +00003105 read_lock(&idev->lock);
3106 if (idev->dead || !(idev->if_flags & IF_READY)) {
3107 read_unlock(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 goto out;
3109 }
stephen hemminger21809fa2010-02-08 19:48:52 +00003110
3111 spin_lock(&ifp->lock);
Herbert Xu622ccdf2010-05-18 15:56:06 -07003112 if (ifp->state == INET6_IFADDR_STATE_DEAD) {
3113 spin_unlock(&ifp->lock);
3114 read_unlock(&idev->lock);
3115 goto out;
3116 }
3117
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118 if (ifp->probes == 0) {
3119 /*
3120 * DAD was successful
3121 */
3122
Brian Haleycc411d02009-09-09 14:41:32 +00003123 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
stephen hemminger21809fa2010-02-08 19:48:52 +00003124 spin_unlock(&ifp->lock);
stephen hemminger122e4512010-03-02 13:32:44 +00003125 read_unlock(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126
3127 addrconf_dad_completed(ifp);
3128
3129 goto out;
3130 }
3131
3132 ifp->probes--;
3133 addrconf_mod_timer(ifp, AC_DAD, ifp->idev->nd_parms->retrans_time);
stephen hemminger21809fa2010-02-08 19:48:52 +00003134 spin_unlock(&ifp->lock);
stephen hemminger122e4512010-03-02 13:32:44 +00003135 read_unlock(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136
3137 /* send a neighbour solicitation for our addr */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
YOSHIFUJI Hideakid7aabf22008-04-10 15:42:11 +09003139 ndisc_send_ns(ifp->idev->dev, NULL, &ifp->addr, &mcaddr, &in6addr_any);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140out:
3141 in6_ifa_put(ifp);
3142}
3143
3144static void addrconf_dad_completed(struct inet6_ifaddr *ifp)
3145{
Stephen Hemmingere21e8462010-03-20 16:09:01 -07003146 struct net_device *dev = ifp->idev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147
3148 /*
3149 * Configure the address for reception. Now it is valid.
3150 */
3151
3152 ipv6_ifa_notify(RTM_NEWADDR, ifp);
3153
Tore Anderson026359b2011-08-28 23:47:33 +00003154 /* If added prefix is link local and we are prepared to process
3155 router advertisements, start sending router solicitations.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 */
3157
Shmulik Ladkaniaeaf6e92012-11-30 10:25:59 +00003158 if (ipv6_accept_ra(ifp->idev) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 ifp->idev->cnf.rtr_solicits > 0 &&
3160 (dev->flags&IFF_LOOPBACK) == 0 &&
3161 (ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162 /*
3163 * If a host as already performed a random delay
3164 * [...] as part of DAD [...] there is no need
3165 * to delay again before sending the first RS
3166 */
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +09003167 ndisc_send_rs(ifp->idev->dev, &ifp->addr, &in6addr_linklocal_allrouters);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168
3169 spin_lock_bh(&ifp->lock);
3170 ifp->probes = 1;
3171 ifp->idev->if_flags |= IF_RS_SENT;
3172 addrconf_mod_timer(ifp, AC_RS, ifp->idev->cnf.rtr_solicit_interval);
3173 spin_unlock_bh(&ifp->lock);
3174 }
3175}
3176
Stephen Hemmingere21e8462010-03-20 16:09:01 -07003177static void addrconf_dad_run(struct inet6_dev *idev)
3178{
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09003179 struct inet6_ifaddr *ifp;
3180
3181 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00003182 list_for_each_entry(ifp, &idev->addr_list, if_list) {
stephen hemminger21809fa2010-02-08 19:48:52 +00003183 spin_lock(&ifp->lock);
Herbert Xuf2344a12010-05-18 15:55:27 -07003184 if (ifp->flags & IFA_F_TENTATIVE &&
3185 ifp->state == INET6_IFADDR_STATE_DAD)
3186 addrconf_dad_kick(ifp);
stephen hemminger21809fa2010-02-08 19:48:52 +00003187 spin_unlock(&ifp->lock);
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09003188 }
3189 read_unlock_bh(&idev->lock);
3190}
3191
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192#ifdef CONFIG_PROC_FS
3193struct if6_iter_state {
Daniel Lezcano3c400902008-01-10 22:42:49 -08003194 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 int bucket;
Mihai Maruseac1d578302012-01-03 23:31:35 +00003196 int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197};
3198
Mihai Maruseac1d578302012-01-03 23:31:35 +00003199static struct inet6_ifaddr *if6_get_first(struct seq_file *seq, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200{
3201 struct inet6_ifaddr *ifa = NULL;
3202 struct if6_iter_state *state = seq->private;
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09003203 struct net *net = seq_file_net(seq);
Mihai Maruseac1d578302012-01-03 23:31:35 +00003204 int p = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205
Mihai Maruseac1d578302012-01-03 23:31:35 +00003206 /* initial bucket if pos is 0 */
3207 if (pos == 0) {
3208 state->bucket = 0;
3209 state->offset = 0;
3210 }
3211
3212 for (; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
stephen hemmingerc2e21292010-03-17 20:31:10 +00003213 struct hlist_node *n;
Eric Dumazet4f70ecc2010-05-03 10:50:14 +00003214 hlist_for_each_entry_rcu_bh(ifa, n, &inet6_addr_lst[state->bucket],
Mihai Maruseac1d578302012-01-03 23:31:35 +00003215 addr_lst) {
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00003216 if (!net_eq(dev_net(ifa->idev->dev), net))
3217 continue;
Mihai Maruseac1d578302012-01-03 23:31:35 +00003218 /* sync with offset */
3219 if (p < state->offset) {
3220 p++;
3221 continue;
3222 }
3223 state->offset++;
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00003224 return ifa;
Mihai Maruseac1d578302012-01-03 23:31:35 +00003225 }
3226
3227 /* prepare for next bucket */
3228 state->offset = 0;
3229 p = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230 }
stephen hemmingerc2e21292010-03-17 20:31:10 +00003231 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232}
3233
stephen hemmingerc2e21292010-03-17 20:31:10 +00003234static struct inet6_ifaddr *if6_get_next(struct seq_file *seq,
3235 struct inet6_ifaddr *ifa)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236{
3237 struct if6_iter_state *state = seq->private;
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09003238 struct net *net = seq_file_net(seq);
stephen hemmingerc2e21292010-03-17 20:31:10 +00003239 struct hlist_node *n = &ifa->addr_lst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240
Mihai Maruseac1d578302012-01-03 23:31:35 +00003241 hlist_for_each_entry_continue_rcu_bh(ifa, n, addr_lst) {
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00003242 if (!net_eq(dev_net(ifa->idev->dev), net))
3243 continue;
Mihai Maruseac1d578302012-01-03 23:31:35 +00003244 state->offset++;
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00003245 return ifa;
Mihai Maruseac1d578302012-01-03 23:31:35 +00003246 }
stephen hemmingerc2e21292010-03-17 20:31:10 +00003247
3248 while (++state->bucket < IN6_ADDR_HSIZE) {
Mihai Maruseac1d578302012-01-03 23:31:35 +00003249 state->offset = 0;
Eric Dumazet4f70ecc2010-05-03 10:50:14 +00003250 hlist_for_each_entry_rcu_bh(ifa, n,
stephen hemmingerc2e21292010-03-17 20:31:10 +00003251 &inet6_addr_lst[state->bucket], addr_lst) {
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00003252 if (!net_eq(dev_net(ifa->idev->dev), net))
3253 continue;
Mihai Maruseac1d578302012-01-03 23:31:35 +00003254 state->offset++;
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00003255 return ifa;
Daniel Lezcano3c400902008-01-10 22:42:49 -08003256 }
3257 }
3258
stephen hemmingerc2e21292010-03-17 20:31:10 +00003259 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260}
3261
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
Eric Dumazet4f70ecc2010-05-03 10:50:14 +00003263 __acquires(rcu_bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264{
stephen hemminger5c578ae2010-03-17 20:31:11 +00003265 rcu_read_lock_bh();
Mihai Maruseac1d578302012-01-03 23:31:35 +00003266 return if6_get_first(seq, *pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267}
3268
3269static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3270{
3271 struct inet6_ifaddr *ifa;
3272
3273 ifa = if6_get_next(seq, v);
3274 ++*pos;
3275 return ifa;
3276}
3277
3278static void if6_seq_stop(struct seq_file *seq, void *v)
Eric Dumazet4f70ecc2010-05-03 10:50:14 +00003279 __releases(rcu_bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280{
stephen hemminger5c578ae2010-03-17 20:31:11 +00003281 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282}
3283
3284static int if6_seq_show(struct seq_file *seq, void *v)
3285{
3286 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
Harvey Harrison4b7a4272008-10-29 12:50:24 -07003287 seq_printf(seq, "%pi6 %02x %02x %02x %02x %8s\n",
Harvey Harrisonb0711952008-10-28 16:05:40 -07003288 &ifp->addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289 ifp->idev->dev->ifindex,
3290 ifp->prefix_len,
3291 ifp->scope,
3292 ifp->flags,
3293 ifp->idev->dev->name);
3294 return 0;
3295}
3296
Philippe De Muyter56b3d972007-07-10 23:07:31 -07003297static const struct seq_operations if6_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 .start = if6_seq_start,
3299 .next = if6_seq_next,
3300 .show = if6_seq_show,
3301 .stop = if6_seq_stop,
3302};
3303
3304static int if6_seq_open(struct inode *inode, struct file *file)
3305{
Daniel Lezcano3c400902008-01-10 22:42:49 -08003306 return seq_open_net(inode, file, &if6_seq_ops,
3307 sizeof(struct if6_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308}
3309
Arjan van de Ven9a321442007-02-12 00:55:35 -08003310static const struct file_operations if6_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311 .owner = THIS_MODULE,
3312 .open = if6_seq_open,
3313 .read = seq_read,
3314 .llseek = seq_lseek,
Daniel Lezcano3c400902008-01-10 22:42:49 -08003315 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316};
3317
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003318static int __net_init if6_proc_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319{
Daniel Lezcano3c400902008-01-10 22:42:49 -08003320 if (!proc_net_fops_create(net, "if_inet6", S_IRUGO, &if6_fops))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 return -ENOMEM;
3322 return 0;
3323}
3324
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003325static void __net_exit if6_proc_net_exit(struct net *net)
Daniel Lezcano3c400902008-01-10 22:42:49 -08003326{
3327 proc_net_remove(net, "if_inet6");
3328}
3329
3330static struct pernet_operations if6_proc_net_ops = {
3331 .init = if6_proc_net_init,
3332 .exit = if6_proc_net_exit,
3333};
3334
3335int __init if6_proc_init(void)
3336{
3337 return register_pernet_subsys(&if6_proc_net_ops);
3338}
3339
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340void if6_proc_exit(void)
3341{
Daniel Lezcano3c400902008-01-10 22:42:49 -08003342 unregister_pernet_subsys(&if6_proc_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343}
3344#endif /* CONFIG_PROC_FS */
3345
Amerigo Wang07a93622012-10-29 16:23:10 +00003346#if IS_ENABLED(CONFIG_IPV6_MIP6)
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07003347/* Check if address is a home address configured on any interface. */
Eric Dumazetb71d1d42011-04-22 04:53:02 +00003348int ipv6_chk_home_addr(struct net *net, const struct in6_addr *addr)
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07003349{
3350 int ret = 0;
stephen hemmingerc2e21292010-03-17 20:31:10 +00003351 struct inet6_ifaddr *ifp = NULL;
3352 struct hlist_node *n;
Eric Dumazetddbe5032012-07-18 08:11:12 +00003353 unsigned int hash = inet6_addr_hash(addr);
stephen hemmingerc2e21292010-03-17 20:31:10 +00003354
stephen hemminger5c578ae2010-03-17 20:31:11 +00003355 rcu_read_lock_bh();
Eric Dumazet4f70ecc2010-05-03 10:50:14 +00003356 hlist_for_each_entry_rcu_bh(ifp, n, &inet6_addr_lst[hash], addr_lst) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09003357 if (!net_eq(dev_net(ifp->idev->dev), net))
Daniel Lezcano389f6612008-01-10 22:44:40 -08003358 continue;
YOSHIFUJI Hideakicaad2952008-04-10 15:42:07 +09003359 if (ipv6_addr_equal(&ifp->addr, addr) &&
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07003360 (ifp->flags & IFA_F_HOMEADDRESS)) {
3361 ret = 1;
3362 break;
3363 }
3364 }
stephen hemminger5c578ae2010-03-17 20:31:11 +00003365 rcu_read_unlock_bh();
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07003366 return ret;
3367}
3368#endif
3369
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370/*
3371 * Periodic address status verification
3372 */
3373
3374static void addrconf_verify(unsigned long foo)
3375{
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -07003376 unsigned long now, next, next_sec, next_sched;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 struct inet6_ifaddr *ifp;
stephen hemmingerc2e21292010-03-17 20:31:10 +00003378 struct hlist_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 int i;
3380
stephen hemminger5c578ae2010-03-17 20:31:11 +00003381 rcu_read_lock_bh();
3382 spin_lock(&addrconf_verify_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383 now = jiffies;
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -07003384 next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385
3386 del_timer(&addr_chk_timer);
3387
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003388 for (i = 0; i < IN6_ADDR_HSIZE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389restart:
Eric Dumazet4f70ecc2010-05-03 10:50:14 +00003390 hlist_for_each_entry_rcu_bh(ifp, node,
stephen hemminger5c578ae2010-03-17 20:31:11 +00003391 &inet6_addr_lst[i], addr_lst) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392 unsigned long age;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393
3394 if (ifp->flags & IFA_F_PERMANENT)
3395 continue;
3396
3397 spin_lock(&ifp->lock);
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -07003398 /* We try to batch several events at once. */
3399 age = (now - ifp->tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400
YOSHIFUJI Hideaki8f27ebb2006-07-28 18:12:11 +09003401 if (ifp->valid_lft != INFINITY_LIFE_TIME &&
3402 age >= ifp->valid_lft) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403 spin_unlock(&ifp->lock);
3404 in6_ifa_hold(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405 ipv6_del_addr(ifp);
3406 goto restart;
YOSHIFUJI Hideaki8f27ebb2006-07-28 18:12:11 +09003407 } else if (ifp->prefered_lft == INFINITY_LIFE_TIME) {
3408 spin_unlock(&ifp->lock);
3409 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410 } else if (age >= ifp->prefered_lft) {
Brian Haleya1ed0522009-07-02 07:10:52 +00003411 /* jiffies - ifp->tstamp > age >= ifp->prefered_lft */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412 int deprecate = 0;
3413
3414 if (!(ifp->flags&IFA_F_DEPRECATED)) {
3415 deprecate = 1;
3416 ifp->flags |= IFA_F_DEPRECATED;
3417 }
3418
3419 if (time_before(ifp->tstamp + ifp->valid_lft * HZ, next))
3420 next = ifp->tstamp + ifp->valid_lft * HZ;
3421
3422 spin_unlock(&ifp->lock);
3423
3424 if (deprecate) {
3425 in6_ifa_hold(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426
3427 ipv6_ifa_notify(0, ifp);
3428 in6_ifa_put(ifp);
3429 goto restart;
3430 }
3431#ifdef CONFIG_IPV6_PRIVACY
3432 } else if ((ifp->flags&IFA_F_TEMPORARY) &&
3433 !(ifp->flags&IFA_F_TENTATIVE)) {
stephen hemminger88949cf2010-03-17 20:31:17 +00003434 unsigned long regen_advance = ifp->idev->cnf.regen_max_retry *
3435 ifp->idev->cnf.dad_transmits *
3436 ifp->idev->nd_parms->retrans_time / HZ;
3437
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438 if (age >= ifp->prefered_lft - regen_advance) {
3439 struct inet6_ifaddr *ifpub = ifp->ifpub;
3440 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
3441 next = ifp->tstamp + ifp->prefered_lft * HZ;
3442 if (!ifp->regen_count && ifpub) {
3443 ifp->regen_count++;
3444 in6_ifa_hold(ifp);
3445 in6_ifa_hold(ifpub);
3446 spin_unlock(&ifp->lock);
stephen hemminger5c578ae2010-03-17 20:31:11 +00003447
Hiroyuki YAMAMORI291d8092005-12-23 11:24:05 -08003448 spin_lock(&ifpub->lock);
3449 ifpub->regen_count = 0;
3450 spin_unlock(&ifpub->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451 ipv6_create_tempaddr(ifpub, ifp);
3452 in6_ifa_put(ifpub);
3453 in6_ifa_put(ifp);
3454 goto restart;
3455 }
3456 } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
3457 next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
3458 spin_unlock(&ifp->lock);
3459#endif
3460 } else {
3461 /* ifp->prefered_lft <= ifp->valid_lft */
3462 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
3463 next = ifp->tstamp + ifp->prefered_lft * HZ;
3464 spin_unlock(&ifp->lock);
3465 }
3466 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467 }
3468
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -07003469 next_sec = round_jiffies_up(next);
3470 next_sched = next;
3471
3472 /* If rounded timeout is accurate enough, accept it. */
3473 if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ))
3474 next_sched = next_sec;
3475
3476 /* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */
3477 if (time_before(next_sched, jiffies + ADDRCONF_TIMER_FUZZ_MAX))
3478 next_sched = jiffies + ADDRCONF_TIMER_FUZZ_MAX;
3479
3480 ADBG((KERN_DEBUG "now = %lu, schedule = %lu, rounded schedule = %lu => %lu\n",
3481 now, next, next_sec, next_sched));
3482
3483 addr_chk_timer.expires = next_sched;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484 add_timer(&addr_chk_timer);
stephen hemminger5c578ae2010-03-17 20:31:11 +00003485 spin_unlock(&addrconf_verify_lock);
3486 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487}
3488
Thomas Graf461d8832006-09-18 00:09:49 -07003489static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local)
3490{
3491 struct in6_addr *pfx = NULL;
3492
3493 if (addr)
3494 pfx = nla_data(addr);
3495
3496 if (local) {
3497 if (pfx && nla_memcmp(local, pfx, sizeof(*pfx)))
3498 pfx = NULL;
3499 else
3500 pfx = nla_data(local);
3501 }
3502
3503 return pfx;
3504}
3505
Patrick McHardyef7c79e2007-06-05 12:38:30 -07003506static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = {
Thomas Graf461d8832006-09-18 00:09:49 -07003507 [IFA_ADDRESS] = { .len = sizeof(struct in6_addr) },
3508 [IFA_LOCAL] = { .len = sizeof(struct in6_addr) },
3509 [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) },
3510};
3511
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512static int
3513inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
3514{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09003515 struct net *net = sock_net(skb->sk);
Thomas Grafb933f712006-09-18 00:10:19 -07003516 struct ifaddrmsg *ifm;
3517 struct nlattr *tb[IFA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518 struct in6_addr *pfx;
Thomas Grafb933f712006-09-18 00:10:19 -07003519 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520
Thomas Grafb933f712006-09-18 00:10:19 -07003521 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
3522 if (err < 0)
3523 return err;
3524
3525 ifm = nlmsg_data(nlh);
3526 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527 if (pfx == NULL)
3528 return -EINVAL;
3529
Daniel Lezcanoaf284932008-03-05 10:46:57 -08003530 return inet6_addr_del(net, ifm->ifa_index, pfx, ifm->ifa_prefixlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531}
3532
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003533static int inet6_addr_modify(struct inet6_ifaddr *ifp, u8 ifa_flags,
3534 u32 prefered_lft, u32 valid_lft)
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09003535{
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07003536 u32 flags;
3537 clock_t expires;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09003538 unsigned long timeout;
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09003539
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09003540 if (!valid_lft || (prefered_lft > valid_lft))
3541 return -EINVAL;
3542
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09003543 timeout = addrconf_timeout_fixup(valid_lft, HZ);
3544 if (addrconf_finite_timeout(timeout)) {
3545 expires = jiffies_to_clock_t(timeout * HZ);
3546 valid_lft = timeout;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07003547 flags = RTF_EXPIRES;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09003548 } else {
3549 expires = 0;
3550 flags = 0;
3551 ifa_flags |= IFA_F_PERMANENT;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07003552 }
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09003553
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09003554 timeout = addrconf_timeout_fixup(prefered_lft, HZ);
3555 if (addrconf_finite_timeout(timeout)) {
3556 if (timeout == 0)
3557 ifa_flags |= IFA_F_DEPRECATED;
3558 prefered_lft = timeout;
3559 }
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09003560
3561 spin_lock_bh(&ifp->lock);
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07003562 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 +09003563 ifp->tstamp = jiffies;
3564 ifp->valid_lft = valid_lft;
3565 ifp->prefered_lft = prefered_lft;
3566
3567 spin_unlock_bh(&ifp->lock);
3568 if (!(ifp->flags&IFA_F_TENTATIVE))
3569 ipv6_ifa_notify(0, ifp);
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09003570
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09003571 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, ifp->idev->dev,
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07003572 expires, flags);
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09003573 addrconf_verify(0);
3574
3575 return 0;
3576}
3577
3578static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
3580{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09003581 struct net *net = sock_net(skb->sk);
Thomas Graf461d8832006-09-18 00:09:49 -07003582 struct ifaddrmsg *ifm;
3583 struct nlattr *tb[IFA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584 struct in6_addr *pfx;
Thomas Graf7198f8c2006-09-18 00:13:46 -07003585 struct inet6_ifaddr *ifa;
3586 struct net_device *dev;
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003587 u32 valid_lft = INFINITY_LIFE_TIME, preferred_lft = INFINITY_LIFE_TIME;
3588 u8 ifa_flags;
Thomas Graf461d8832006-09-18 00:09:49 -07003589 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590
Thomas Graf461d8832006-09-18 00:09:49 -07003591 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
3592 if (err < 0)
3593 return err;
3594
3595 ifm = nlmsg_data(nlh);
3596 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597 if (pfx == NULL)
3598 return -EINVAL;
3599
Thomas Graf461d8832006-09-18 00:09:49 -07003600 if (tb[IFA_CACHEINFO]) {
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09003601 struct ifa_cacheinfo *ci;
Thomas Graf461d8832006-09-18 00:09:49 -07003602
3603 ci = nla_data(tb[IFA_CACHEINFO]);
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09003604 valid_lft = ci->ifa_valid;
Thomas Graf461d8832006-09-18 00:09:49 -07003605 preferred_lft = ci->ifa_prefered;
3606 } else {
3607 preferred_lft = INFINITY_LIFE_TIME;
3608 valid_lft = INFINITY_LIFE_TIME;
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09003609 }
3610
Daniel Lezcanoaf284932008-03-05 10:46:57 -08003611 dev = __dev_get_by_index(net, ifm->ifa_index);
Thomas Graf7198f8c2006-09-18 00:13:46 -07003612 if (dev == NULL)
3613 return -ENODEV;
3614
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003615 /* We ignore other flags so far. */
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07003616 ifa_flags = ifm->ifa_flags & (IFA_F_NODAD | IFA_F_HOMEADDRESS);
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003617
Daniel Lezcano1cab3da2008-01-10 22:44:09 -08003618 ifa = ipv6_get_ifaddr(net, pfx, dev, 1);
Thomas Graf7198f8c2006-09-18 00:13:46 -07003619 if (ifa == NULL) {
3620 /*
3621 * It would be best to check for !NLM_F_CREATE here but
3622 * userspace alreay relies on not having to provide this.
3623 */
Daniel Lezcanoaf284932008-03-05 10:46:57 -08003624 return inet6_addr_add(net, ifm->ifa_index, pfx,
3625 ifm->ifa_prefixlen, ifa_flags,
3626 preferred_lft, valid_lft);
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09003627 }
3628
Thomas Graf7198f8c2006-09-18 00:13:46 -07003629 if (nlh->nlmsg_flags & NLM_F_EXCL ||
3630 !(nlh->nlmsg_flags & NLM_F_REPLACE))
3631 err = -EEXIST;
3632 else
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003633 err = inet6_addr_modify(ifa, ifa_flags, preferred_lft, valid_lft);
Thomas Graf7198f8c2006-09-18 00:13:46 -07003634
3635 in6_ifa_put(ifa);
3636
3637 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638}
3639
Thomas Graf101bb222006-09-18 00:11:52 -07003640static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u8 flags,
3641 u8 scope, int ifindex)
3642{
3643 struct ifaddrmsg *ifm;
3644
3645 ifm = nlmsg_data(nlh);
3646 ifm->ifa_family = AF_INET6;
3647 ifm->ifa_prefixlen = prefixlen;
3648 ifm->ifa_flags = flags;
3649 ifm->ifa_scope = scope;
3650 ifm->ifa_index = ifindex;
3651}
3652
Thomas Graf85486af2006-09-18 00:11:24 -07003653static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
3654 unsigned long tstamp, u32 preferred, u32 valid)
3655{
3656 struct ifa_cacheinfo ci;
3657
Thomas Graf18a31e12010-11-17 04:12:02 +00003658 ci.cstamp = cstamp_delta(cstamp);
3659 ci.tstamp = cstamp_delta(tstamp);
Thomas Graf85486af2006-09-18 00:11:24 -07003660 ci.ifa_prefered = preferred;
3661 ci.ifa_valid = valid;
3662
3663 return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
3664}
3665
Thomas Graf101bb222006-09-18 00:11:52 -07003666static inline int rt_scope(int ifa_scope)
3667{
3668 if (ifa_scope & IFA_HOST)
3669 return RT_SCOPE_HOST;
3670 else if (ifa_scope & IFA_LINK)
3671 return RT_SCOPE_LINK;
3672 else if (ifa_scope & IFA_SITE)
3673 return RT_SCOPE_SITE;
3674 else
3675 return RT_SCOPE_UNIVERSE;
3676}
3677
Thomas Graf0ab68032006-09-18 00:12:35 -07003678static inline int inet6_ifaddr_msgsize(void)
3679{
Thomas Graf339bf982006-11-10 14:10:15 -08003680 return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
3681 + nla_total_size(16) /* IFA_ADDRESS */
3682 + nla_total_size(sizeof(struct ifa_cacheinfo));
Thomas Graf0ab68032006-09-18 00:12:35 -07003683}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07003684
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
Eric W. Biederman15e47302012-09-07 20:12:54 +00003686 u32 portid, u32 seq, int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688 struct nlmsghdr *nlh;
Thomas Graf85486af2006-09-18 00:11:24 -07003689 u32 preferred, valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690
Eric W. Biederman15e47302012-09-07 20:12:54 +00003691 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
Thomas Graf0ab68032006-09-18 00:12:35 -07003692 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08003693 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07003694
Thomas Graf101bb222006-09-18 00:11:52 -07003695 put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope),
3696 ifa->idev->dev->ifindex);
3697
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698 if (!(ifa->flags&IFA_F_PERMANENT)) {
Thomas Graf85486af2006-09-18 00:11:24 -07003699 preferred = ifa->prefered_lft;
3700 valid = ifa->valid_lft;
3701 if (preferred != INFINITY_LIFE_TIME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702 long tval = (jiffies - ifa->tstamp)/HZ;
Jens Rosenbooma1faa692009-06-25 04:55:50 +00003703 if (preferred > tval)
3704 preferred -= tval;
3705 else
3706 preferred = 0;
Ben Hutchingsf56619f2010-06-26 11:37:47 +00003707 if (valid != INFINITY_LIFE_TIME) {
3708 if (valid > tval)
3709 valid -= tval;
3710 else
3711 valid = 0;
3712 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713 }
3714 } else {
Thomas Graf85486af2006-09-18 00:11:24 -07003715 preferred = INFINITY_LIFE_TIME;
3716 valid = INFINITY_LIFE_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717 }
Thomas Graf85486af2006-09-18 00:11:24 -07003718
Thomas Graf0ab68032006-09-18 00:12:35 -07003719 if (nla_put(skb, IFA_ADDRESS, 16, &ifa->addr) < 0 ||
Patrick McHardy26932562007-01-31 23:16:40 -08003720 put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0) {
3721 nlmsg_cancel(skb, nlh);
3722 return -EMSGSIZE;
3723 }
Thomas Graf85486af2006-09-18 00:11:24 -07003724
Thomas Graf0ab68032006-09-18 00:12:35 -07003725 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726}
3727
3728static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
Eric W. Biederman15e47302012-09-07 20:12:54 +00003729 u32 portid, u32 seq, int event, u16 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731 struct nlmsghdr *nlh;
Thomas Graf101bb222006-09-18 00:11:52 -07003732 u8 scope = RT_SCOPE_UNIVERSE;
3733 int ifindex = ifmca->idev->dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734
Thomas Graf101bb222006-09-18 00:11:52 -07003735 if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE)
3736 scope = RT_SCOPE_SITE;
3737
Eric W. Biederman15e47302012-09-07 20:12:54 +00003738 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
Thomas Graf0ab68032006-09-18 00:12:35 -07003739 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08003740 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07003741
Thomas Graf101bb222006-09-18 00:11:52 -07003742 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
Thomas Graf0ab68032006-09-18 00:12:35 -07003743 if (nla_put(skb, IFA_MULTICAST, 16, &ifmca->mca_addr) < 0 ||
3744 put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp,
Patrick McHardy26932562007-01-31 23:16:40 -08003745 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
3746 nlmsg_cancel(skb, nlh);
3747 return -EMSGSIZE;
3748 }
Thomas Graf85486af2006-09-18 00:11:24 -07003749
Thomas Graf0ab68032006-09-18 00:12:35 -07003750 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751}
3752
3753static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
Eric W. Biederman15e47302012-09-07 20:12:54 +00003754 u32 portid, u32 seq, int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756 struct nlmsghdr *nlh;
Thomas Graf101bb222006-09-18 00:11:52 -07003757 u8 scope = RT_SCOPE_UNIVERSE;
3758 int ifindex = ifaca->aca_idev->dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759
Thomas Graf101bb222006-09-18 00:11:52 -07003760 if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE)
3761 scope = RT_SCOPE_SITE;
3762
Eric W. Biederman15e47302012-09-07 20:12:54 +00003763 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
Thomas Graf0ab68032006-09-18 00:12:35 -07003764 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08003765 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07003766
Thomas Graf101bb222006-09-18 00:11:52 -07003767 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
Thomas Graf0ab68032006-09-18 00:12:35 -07003768 if (nla_put(skb, IFA_ANYCAST, 16, &ifaca->aca_addr) < 0 ||
3769 put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp,
Patrick McHardy26932562007-01-31 23:16:40 -08003770 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
3771 nlmsg_cancel(skb, nlh);
3772 return -EMSGSIZE;
3773 }
Thomas Graf85486af2006-09-18 00:11:24 -07003774
Thomas Graf0ab68032006-09-18 00:12:35 -07003775 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776}
3777
Stephen Hemmingere21e8462010-03-20 16:09:01 -07003778enum addr_type_t {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779 UNICAST_ADDR,
3780 MULTICAST_ADDR,
3781 ANYCAST_ADDR,
3782};
3783
Eric Dumazet234b27c2009-11-12 04:11:50 +00003784/* called with rcu_read_lock() */
3785static int in6_dump_addrs(struct inet6_dev *idev, struct sk_buff *skb,
3786 struct netlink_callback *cb, enum addr_type_t type,
3787 int s_ip_idx, int *p_ip_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789 struct ifmcaddr6 *ifmca;
3790 struct ifacaddr6 *ifaca;
Eric Dumazet234b27c2009-11-12 04:11:50 +00003791 int err = 1;
3792 int ip_idx = *p_ip_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793
Eric Dumazet234b27c2009-11-12 04:11:50 +00003794 read_lock_bh(&idev->lock);
3795 switch (type) {
stephen hemminger502a2ff2010-03-17 20:31:13 +00003796 case UNICAST_ADDR: {
3797 struct inet6_ifaddr *ifa;
3798
Eric Dumazet234b27c2009-11-12 04:11:50 +00003799 /* unicast address incl. temp addr */
stephen hemminger502a2ff2010-03-17 20:31:13 +00003800 list_for_each_entry(ifa, &idev->addr_list, if_list) {
3801 if (++ip_idx < s_ip_idx)
Eric Dumazet234b27c2009-11-12 04:11:50 +00003802 continue;
3803 err = inet6_fill_ifaddr(skb, ifa,
Eric W. Biederman15e47302012-09-07 20:12:54 +00003804 NETLINK_CB(cb->skb).portid,
Eric Dumazet234b27c2009-11-12 04:11:50 +00003805 cb->nlh->nlmsg_seq,
3806 RTM_NEWADDR,
3807 NLM_F_MULTI);
3808 if (err <= 0)
3809 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810 }
Eric Dumazet234b27c2009-11-12 04:11:50 +00003811 break;
stephen hemminger502a2ff2010-03-17 20:31:13 +00003812 }
Eric Dumazet234b27c2009-11-12 04:11:50 +00003813 case MULTICAST_ADDR:
3814 /* multicast address */
3815 for (ifmca = idev->mc_list; ifmca;
3816 ifmca = ifmca->next, ip_idx++) {
3817 if (ip_idx < s_ip_idx)
3818 continue;
3819 err = inet6_fill_ifmcaddr(skb, ifmca,
Eric W. Biederman15e47302012-09-07 20:12:54 +00003820 NETLINK_CB(cb->skb).portid,
Eric Dumazet234b27c2009-11-12 04:11:50 +00003821 cb->nlh->nlmsg_seq,
3822 RTM_GETMULTICAST,
3823 NLM_F_MULTI);
3824 if (err <= 0)
3825 break;
3826 }
3827 break;
3828 case ANYCAST_ADDR:
3829 /* anycast address */
3830 for (ifaca = idev->ac_list; ifaca;
3831 ifaca = ifaca->aca_next, ip_idx++) {
3832 if (ip_idx < s_ip_idx)
3833 continue;
3834 err = inet6_fill_ifacaddr(skb, ifaca,
Eric W. Biederman15e47302012-09-07 20:12:54 +00003835 NETLINK_CB(cb->skb).portid,
Eric Dumazet234b27c2009-11-12 04:11:50 +00003836 cb->nlh->nlmsg_seq,
3837 RTM_GETANYCAST,
3838 NLM_F_MULTI);
3839 if (err <= 0)
3840 break;
3841 }
3842 break;
3843 default:
3844 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845 }
Eric Dumazet234b27c2009-11-12 04:11:50 +00003846 read_unlock_bh(&idev->lock);
3847 *p_ip_idx = ip_idx;
3848 return err;
3849}
3850
3851static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
3852 enum addr_type_t type)
3853{
3854 struct net *net = sock_net(skb->sk);
3855 int h, s_h;
3856 int idx, ip_idx;
3857 int s_idx, s_ip_idx;
3858 struct net_device *dev;
3859 struct inet6_dev *idev;
3860 struct hlist_head *head;
3861 struct hlist_node *node;
3862
3863 s_h = cb->args[0];
3864 s_idx = idx = cb->args[1];
3865 s_ip_idx = ip_idx = cb->args[2];
3866
3867 rcu_read_lock();
3868 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
3869 idx = 0;
3870 head = &net->dev_index_head[h];
3871 hlist_for_each_entry_rcu(dev, node, head, index_hlist) {
3872 if (idx < s_idx)
3873 goto cont;
Patrick McHardy4b97efd2010-03-26 20:27:49 -07003874 if (h > s_h || idx > s_idx)
Eric Dumazet234b27c2009-11-12 04:11:50 +00003875 s_ip_idx = 0;
3876 ip_idx = 0;
Stephen Hemmingere21e8462010-03-20 16:09:01 -07003877 idev = __in6_dev_get(dev);
3878 if (!idev)
Eric Dumazet234b27c2009-11-12 04:11:50 +00003879 goto cont;
3880
3881 if (in6_dump_addrs(idev, skb, cb, type,
3882 s_ip_idx, &ip_idx) <= 0)
3883 goto done;
3884cont:
3885 idx++;
3886 }
3887 }
3888done:
3889 rcu_read_unlock();
3890 cb->args[0] = h;
3891 cb->args[1] = idx;
3892 cb->args[2] = ip_idx;
3893
Linus Torvalds1da177e2005-04-16 15:20:36 -07003894 return skb->len;
3895}
3896
3897static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
3898{
3899 enum addr_type_t type = UNICAST_ADDR;
Denis V. Lunevb8542722007-12-01 00:21:31 +11003900
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901 return inet6_dump_addr(skb, cb, type);
3902}
3903
3904static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
3905{
3906 enum addr_type_t type = MULTICAST_ADDR;
Denis V. Lunevb8542722007-12-01 00:21:31 +11003907
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908 return inet6_dump_addr(skb, cb, type);
3909}
3910
3911
3912static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
3913{
3914 enum addr_type_t type = ANYCAST_ADDR;
Denis V. Lunevb8542722007-12-01 00:21:31 +11003915
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916 return inet6_dump_addr(skb, cb, type);
3917}
3918
Eldad Zack8e5e8f32012-04-01 07:49:08 +00003919static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr *nlh,
Thomas Graf1b29fc22006-09-18 00:10:50 -07003920 void *arg)
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003921{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09003922 struct net *net = sock_net(in_skb->sk);
Thomas Graf1b29fc22006-09-18 00:10:50 -07003923 struct ifaddrmsg *ifm;
3924 struct nlattr *tb[IFA_MAX+1];
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003925 struct in6_addr *addr = NULL;
3926 struct net_device *dev = NULL;
3927 struct inet6_ifaddr *ifa;
3928 struct sk_buff *skb;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003929 int err;
3930
Thomas Graf1b29fc22006-09-18 00:10:50 -07003931 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
3932 if (err < 0)
3933 goto errout;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003934
Thomas Graf1b29fc22006-09-18 00:10:50 -07003935 addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
3936 if (addr == NULL) {
3937 err = -EINVAL;
3938 goto errout;
3939 }
3940
3941 ifm = nlmsg_data(nlh);
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003942 if (ifm->ifa_index)
Benjamin Thery6fda7352008-03-05 10:47:47 -08003943 dev = __dev_get_by_index(net, ifm->ifa_index);
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003944
Stephen Hemmingere21e8462010-03-20 16:09:01 -07003945 ifa = ipv6_get_ifaddr(net, addr, dev, 1);
3946 if (!ifa) {
Thomas Graf1b29fc22006-09-18 00:10:50 -07003947 err = -EADDRNOTAVAIL;
3948 goto errout;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003949 }
3950
Stephen Hemmingere21e8462010-03-20 16:09:01 -07003951 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL);
3952 if (!skb) {
Thomas Graf1b29fc22006-09-18 00:10:50 -07003953 err = -ENOBUFS;
3954 goto errout_ifa;
3955 }
3956
Eric W. Biederman15e47302012-09-07 20:12:54 +00003957 err = inet6_fill_ifaddr(skb, ifa, NETLINK_CB(in_skb).portid,
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003958 nlh->nlmsg_seq, RTM_NEWADDR, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08003959 if (err < 0) {
3960 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
3961 WARN_ON(err == -EMSGSIZE);
3962 kfree_skb(skb);
3963 goto errout_ifa;
3964 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00003965 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
Thomas Graf1b29fc22006-09-18 00:10:50 -07003966errout_ifa:
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003967 in6_ifa_put(ifa);
Thomas Graf1b29fc22006-09-18 00:10:50 -07003968errout:
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003969 return err;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003970}
3971
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
3973{
3974 struct sk_buff *skb;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09003975 struct net *net = dev_net(ifa->idev->dev);
Thomas Graf5d620262006-08-15 00:35:02 -07003976 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977
Thomas Graf0ab68032006-09-18 00:12:35 -07003978 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC);
Thomas Graf5d620262006-08-15 00:35:02 -07003979 if (skb == NULL)
3980 goto errout;
3981
3982 err = inet6_fill_ifaddr(skb, ifa, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08003983 if (err < 0) {
3984 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
3985 WARN_ON(err == -EMSGSIZE);
3986 kfree_skb(skb);
3987 goto errout;
3988 }
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08003989 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
3990 return;
Thomas Graf5d620262006-08-15 00:35:02 -07003991errout:
3992 if (err < 0)
Benjamin Thery6fda7352008-03-05 10:47:47 -08003993 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994}
3995
Dave Jonesb6f99a22007-03-22 12:27:49 -07003996static inline void ipv6_store_devconf(struct ipv6_devconf *cnf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997 __s32 *array, int bytes)
3998{
Thomas Graf04561c12006-11-14 19:53:58 -08003999 BUG_ON(bytes < (DEVCONF_MAX * 4));
4000
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001 memset(array, 0, bytes);
4002 array[DEVCONF_FORWARDING] = cnf->forwarding;
4003 array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
4004 array[DEVCONF_MTU6] = cnf->mtu6;
4005 array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
4006 array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
4007 array[DEVCONF_AUTOCONF] = cnf->autoconf;
4008 array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
4009 array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
Thomas Graf93908d12010-11-17 01:44:24 +00004010 array[DEVCONF_RTR_SOLICIT_INTERVAL] =
4011 jiffies_to_msecs(cnf->rtr_solicit_interval);
4012 array[DEVCONF_RTR_SOLICIT_DELAY] =
4013 jiffies_to_msecs(cnf->rtr_solicit_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014 array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
4015#ifdef CONFIG_IPV6_PRIVACY
4016 array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
4017 array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
4018 array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
4019 array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
4020 array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
4021#endif
4022 array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -08004023 array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr;
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -08004024 array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo;
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08004025#ifdef CONFIG_IPV6_ROUTER_PREF
4026 array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref;
Thomas Graf93908d12010-11-17 01:44:24 +00004027 array[DEVCONF_RTR_PROBE_INTERVAL] =
4028 jiffies_to_msecs(cnf->rtr_probe_interval);
Neil Hormanfa03ef32007-01-30 14:30:10 -08004029#ifdef CONFIG_IPV6_ROUTE_INFO
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -08004030 array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen;
4031#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08004032#endif
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -07004033 array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp;
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -07004034 array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route;
Neil Horman95c385b2007-04-25 17:08:10 -07004035#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
4036 array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad;
4037#endif
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09004038#ifdef CONFIG_IPV6_MROUTE
4039 array[DEVCONF_MC_FORWARDING] = cnf->mc_forwarding;
4040#endif
YOSHIFUJI Hideaki778d80b2008-06-28 14:17:11 +09004041 array[DEVCONF_DISABLE_IPV6] = cnf->disable_ipv6;
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09004042 array[DEVCONF_ACCEPT_DAD] = cnf->accept_dad;
Cosmin Ratiuc3faca02009-10-09 03:11:14 +00004043 array[DEVCONF_FORCE_TLLAO] = cnf->force_tllao;
Hannes Frederic Sowa5cb04432012-11-06 16:46:20 +00004044 array[DEVCONF_NDISC_NOTIFY] = cnf->ndisc_notify;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045}
4046
Thomas Grafb382b192010-11-16 04:33:57 +00004047static inline size_t inet6_ifla6_size(void)
4048{
4049 return nla_total_size(4) /* IFLA_INET6_FLAGS */
4050 + nla_total_size(sizeof(struct ifla_cacheinfo))
4051 + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */
4052 + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */
4053 + nla_total_size(ICMP6_MIB_MAX * 8); /* IFLA_INET6_ICMP6STATS */
4054}
4055
Thomas Graf339bf982006-11-10 14:10:15 -08004056static inline size_t inet6_if_nlmsg_size(void)
4057{
4058 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
4059 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
4060 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
4061 + nla_total_size(4) /* IFLA_MTU */
4062 + nla_total_size(4) /* IFLA_LINK */
Thomas Grafb382b192010-11-16 04:33:57 +00004063 + nla_total_size(inet6_ifla6_size()); /* IFLA_PROTINFO */
Thomas Graf339bf982006-11-10 14:10:15 -08004064}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07004065
Eric Dumazetbe281e52011-05-19 01:14:23 +00004066static inline void __snmp6_fill_statsdev(u64 *stats, atomic_long_t *mib,
Tejun Heo7d720c32010-02-16 15:20:26 +00004067 int items, int bytes)
Herbert Xu7f7d9a62007-04-24 21:54:09 -07004068{
4069 int i;
4070 int pad = bytes - sizeof(u64) * items;
4071 BUG_ON(pad < 0);
4072
4073 /* Use put_unaligned() because stats may not be aligned for u64. */
4074 put_unaligned(items, &stats[0]);
4075 for (i = 1; i < items; i++)
Eric Dumazetbe281e52011-05-19 01:14:23 +00004076 put_unaligned(atomic_long_read(&mib[i]), &stats[i]);
Herbert Xu7f7d9a62007-04-24 21:54:09 -07004077
4078 memset(&stats[items], 0, pad);
4079}
4080
Eric Dumazet4ce3c182010-06-30 13:31:19 -07004081static inline void __snmp6_fill_stats64(u64 *stats, void __percpu **mib,
4082 int items, int bytes, size_t syncpoff)
4083{
4084 int i;
4085 int pad = bytes - sizeof(u64) * items;
4086 BUG_ON(pad < 0);
4087
4088 /* Use put_unaligned() because stats may not be aligned for u64. */
4089 put_unaligned(items, &stats[0]);
4090 for (i = 1; i < items; i++)
4091 put_unaligned(snmp_fold_field64(mib, i, syncpoff), &stats[i]);
4092
4093 memset(&stats[items], 0, pad);
4094}
4095
Herbert Xu7f7d9a62007-04-24 21:54:09 -07004096static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype,
4097 int bytes)
4098{
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004099 switch (attrtype) {
Herbert Xu7f7d9a62007-04-24 21:54:09 -07004100 case IFLA_INET6_STATS:
Eric Dumazet4ce3c182010-06-30 13:31:19 -07004101 __snmp6_fill_stats64(stats, (void __percpu **)idev->stats.ipv6,
4102 IPSTATS_MIB_MAX, bytes, offsetof(struct ipstats_mib, syncp));
Herbert Xu7f7d9a62007-04-24 21:54:09 -07004103 break;
4104 case IFLA_INET6_ICMP6STATS:
Eric Dumazetbe281e52011-05-19 01:14:23 +00004105 __snmp6_fill_statsdev(stats, idev->stats.icmpv6dev->mibs, ICMP6_MIB_MAX, bytes);
Herbert Xu7f7d9a62007-04-24 21:54:09 -07004106 break;
4107 }
4108}
4109
Thomas Grafb382b192010-11-16 04:33:57 +00004110static int inet6_fill_ifla6_attrs(struct sk_buff *skb, struct inet6_dev *idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111{
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07004112 struct nlattr *nla;
Thomas Graf04561c12006-11-14 19:53:58 -08004113 struct ifla_cacheinfo ci;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114
David S. Millerc78679e2012-04-01 20:27:33 -04004115 if (nla_put_u32(skb, IFLA_INET6_FLAGS, idev->if_flags))
4116 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117 ci.max_reasm_len = IPV6_MAXPLEN;
David S. Miller24912422010-11-19 13:13:47 -08004118 ci.tstamp = cstamp_delta(idev->tstamp);
4119 ci.reachable_time = jiffies_to_msecs(idev->nd_parms->reachable_time);
4120 ci.retrans_time = jiffies_to_msecs(idev->nd_parms->retrans_time);
David S. Millerc78679e2012-04-01 20:27:33 -04004121 if (nla_put(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci))
4122 goto nla_put_failure;
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07004123 nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32));
4124 if (nla == NULL)
Thomas Graf04561c12006-11-14 19:53:58 -08004125 goto nla_put_failure;
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07004126 ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07004128 /* XXX - MC not implemented */
4129
4130 nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64));
4131 if (nla == NULL)
4132 goto nla_put_failure;
4133 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla));
4134
4135 nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64));
4136 if (nla == NULL)
4137 goto nla_put_failure;
4138 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139
Thomas Grafb382b192010-11-16 04:33:57 +00004140 return 0;
4141
4142nla_put_failure:
4143 return -EMSGSIZE;
4144}
4145
4146static size_t inet6_get_link_af_size(const struct net_device *dev)
4147{
4148 if (!__in6_dev_get(dev))
4149 return 0;
4150
4151 return inet6_ifla6_size();
4152}
4153
4154static int inet6_fill_link_af(struct sk_buff *skb, const struct net_device *dev)
4155{
4156 struct inet6_dev *idev = __in6_dev_get(dev);
4157
4158 if (!idev)
4159 return -ENODATA;
4160
4161 if (inet6_fill_ifla6_attrs(skb, idev) < 0)
4162 return -EMSGSIZE;
4163
4164 return 0;
4165}
4166
Thomas Grafb382b192010-11-16 04:33:57 +00004167static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004168 u32 portid, u32 seq, int event, unsigned int flags)
Thomas Grafb382b192010-11-16 04:33:57 +00004169{
4170 struct net_device *dev = idev->dev;
4171 struct ifinfomsg *hdr;
4172 struct nlmsghdr *nlh;
4173 void *protoinfo;
4174
Eric W. Biederman15e47302012-09-07 20:12:54 +00004175 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*hdr), flags);
Thomas Grafb382b192010-11-16 04:33:57 +00004176 if (nlh == NULL)
4177 return -EMSGSIZE;
4178
4179 hdr = nlmsg_data(nlh);
4180 hdr->ifi_family = AF_INET6;
4181 hdr->__ifi_pad = 0;
4182 hdr->ifi_type = dev->type;
4183 hdr->ifi_index = dev->ifindex;
4184 hdr->ifi_flags = dev_get_flags(dev);
4185 hdr->ifi_change = 0;
4186
David S. Millerc78679e2012-04-01 20:27:33 -04004187 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
4188 (dev->addr_len &&
4189 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
4190 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
4191 (dev->ifindex != dev->iflink &&
4192 nla_put_u32(skb, IFLA_LINK, dev->iflink)))
4193 goto nla_put_failure;
Thomas Grafb382b192010-11-16 04:33:57 +00004194 protoinfo = nla_nest_start(skb, IFLA_PROTINFO);
4195 if (protoinfo == NULL)
4196 goto nla_put_failure;
4197
4198 if (inet6_fill_ifla6_attrs(skb, idev) < 0)
4199 goto nla_put_failure;
4200
Thomas Graf04561c12006-11-14 19:53:58 -08004201 nla_nest_end(skb, protoinfo);
4202 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203
Thomas Graf04561c12006-11-14 19:53:58 -08004204nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08004205 nlmsg_cancel(skb, nlh);
4206 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004207}
4208
4209static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
4210{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09004211 struct net *net = sock_net(skb->sk);
Eric Dumazet84d26972009-11-09 12:11:28 +00004212 int h, s_h;
David S. Miller434a8a582009-11-11 18:53:00 -08004213 int idx = 0, s_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214 struct net_device *dev;
4215 struct inet6_dev *idev;
Eric Dumazet84d26972009-11-09 12:11:28 +00004216 struct hlist_head *head;
4217 struct hlist_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218
Eric Dumazet84d26972009-11-09 12:11:28 +00004219 s_h = cb->args[0];
4220 s_idx = cb->args[1];
4221
4222 rcu_read_lock();
4223 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4224 idx = 0;
4225 head = &net->dev_index_head[h];
4226 hlist_for_each_entry_rcu(dev, node, head, index_hlist) {
4227 if (idx < s_idx)
4228 goto cont;
4229 idev = __in6_dev_get(dev);
4230 if (!idev)
4231 goto cont;
4232 if (inet6_fill_ifinfo(skb, idev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004233 NETLINK_CB(cb->skb).portid,
Eric Dumazet84d26972009-11-09 12:11:28 +00004234 cb->nlh->nlmsg_seq,
4235 RTM_NEWLINK, NLM_F_MULTI) <= 0)
4236 goto out;
Pavel Emelianov7562f872007-05-03 15:13:45 -07004237cont:
Eric Dumazet84d26972009-11-09 12:11:28 +00004238 idx++;
4239 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240 }
Eric Dumazet84d26972009-11-09 12:11:28 +00004241out:
4242 rcu_read_unlock();
4243 cb->args[1] = idx;
4244 cb->args[0] = h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245
4246 return skb->len;
4247}
4248
4249void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
4250{
4251 struct sk_buff *skb;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09004252 struct net *net = dev_net(idev->dev);
Thomas Graf8d7a76c2006-08-15 00:35:47 -07004253 int err = -ENOBUFS;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004254
Thomas Graf339bf982006-11-10 14:10:15 -08004255 skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC);
Thomas Graf8d7a76c2006-08-15 00:35:47 -07004256 if (skb == NULL)
4257 goto errout;
4258
4259 err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08004260 if (err < 0) {
4261 /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */
4262 WARN_ON(err == -EMSGSIZE);
4263 kfree_skb(skb);
4264 goto errout;
4265 }
Nicolas Dichtel5f75a102010-12-07 23:38:31 +00004266 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFINFO, NULL, GFP_ATOMIC);
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08004267 return;
Thomas Graf8d7a76c2006-08-15 00:35:47 -07004268errout:
4269 if (err < 0)
Nicolas Dichtel5f75a102010-12-07 23:38:31 +00004270 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFINFO, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271}
4272
Thomas Graf339bf982006-11-10 14:10:15 -08004273static inline size_t inet6_prefix_nlmsg_size(void)
4274{
4275 return NLMSG_ALIGN(sizeof(struct prefixmsg))
4276 + nla_total_size(sizeof(struct in6_addr))
4277 + nla_total_size(sizeof(struct prefix_cacheinfo));
4278}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07004279
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004281 struct prefix_info *pinfo, u32 portid, u32 seq,
Thomas Graf6051e2f2006-11-14 19:54:19 -08004282 int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283{
Thomas Graf6051e2f2006-11-14 19:54:19 -08004284 struct prefixmsg *pmsg;
4285 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286 struct prefix_cacheinfo ci;
4287
Eric W. Biederman15e47302012-09-07 20:12:54 +00004288 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*pmsg), flags);
Thomas Graf6051e2f2006-11-14 19:54:19 -08004289 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08004290 return -EMSGSIZE;
Thomas Graf6051e2f2006-11-14 19:54:19 -08004291
4292 pmsg = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293 pmsg->prefix_family = AF_INET6;
Patrick McHardy8a470772005-06-28 12:56:45 -07004294 pmsg->prefix_pad1 = 0;
4295 pmsg->prefix_pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296 pmsg->prefix_ifindex = idev->dev->ifindex;
4297 pmsg->prefix_len = pinfo->prefix_len;
4298 pmsg->prefix_type = pinfo->type;
Patrick McHardy8a470772005-06-28 12:56:45 -07004299 pmsg->prefix_pad3 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300 pmsg->prefix_flags = 0;
4301 if (pinfo->onlink)
4302 pmsg->prefix_flags |= IF_PREFIX_ONLINK;
4303 if (pinfo->autoconf)
4304 pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
4305
David S. Millerc78679e2012-04-01 20:27:33 -04004306 if (nla_put(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix))
4307 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308 ci.preferred_time = ntohl(pinfo->prefered);
4309 ci.valid_time = ntohl(pinfo->valid);
David S. Millerc78679e2012-04-01 20:27:33 -04004310 if (nla_put(skb, PREFIX_CACHEINFO, sizeof(ci), &ci))
4311 goto nla_put_failure;
Thomas Graf6051e2f2006-11-14 19:54:19 -08004312 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313
Thomas Graf6051e2f2006-11-14 19:54:19 -08004314nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08004315 nlmsg_cancel(skb, nlh);
4316 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317}
4318
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004319static void inet6_prefix_notify(int event, struct inet6_dev *idev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320 struct prefix_info *pinfo)
4321{
4322 struct sk_buff *skb;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09004323 struct net *net = dev_net(idev->dev);
Thomas Graf8c384bfa2006-08-15 00:36:07 -07004324 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325
Thomas Graf339bf982006-11-10 14:10:15 -08004326 skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC);
Thomas Graf8c384bfa2006-08-15 00:36:07 -07004327 if (skb == NULL)
4328 goto errout;
4329
4330 err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08004331 if (err < 0) {
4332 /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */
4333 WARN_ON(err == -EMSGSIZE);
4334 kfree_skb(skb);
4335 goto errout;
4336 }
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08004337 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC);
4338 return;
Thomas Graf8c384bfa2006-08-15 00:36:07 -07004339errout:
4340 if (err < 0)
Benjamin Thery6fda7352008-03-05 10:47:47 -08004341 rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342}
4343
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
4345{
4346 inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
4347
4348 switch (event) {
4349 case RTM_NEWADDR:
Neil Horman95c385b2007-04-25 17:08:10 -07004350 /*
4351 * If the address was optimistic
4352 * we inserted the route at the start of
4353 * our DAD process, so we don't need
4354 * to do it again
4355 */
4356 if (!(ifp->rt->rt6i_node))
4357 ip6_ins_rt(ifp->rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004358 if (ifp->idev->cnf.forwarding)
4359 addrconf_join_anycast(ifp);
4360 break;
4361 case RTM_DELADDR:
4362 if (ifp->idev->cnf.forwarding)
4363 addrconf_leave_anycast(ifp);
4364 addrconf_leave_solict(ifp->idev, &ifp->addr);
Changli Gaod8d1f302010-06-10 23:31:35 -07004365 dst_hold(&ifp->rt->dst);
stephen hemminger93fa1592010-04-12 05:41:31 +00004366
David S. Miller73a8bd72011-01-23 23:27:15 -08004367 if (ip6_del_rt(ifp->rt))
Changli Gaod8d1f302010-06-10 23:31:35 -07004368 dst_free(&ifp->rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004369 break;
4370 }
4371}
4372
4373static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
4374{
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07004375 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376 if (likely(ifp->idev->dead == 0))
4377 __ipv6_ifa_notify(event, ifp);
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07004378 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379}
4380
4381#ifdef CONFIG_SYSCTL
4382
4383static
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07004384int addrconf_sysctl_forward(ctl_table *ctl, int write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385 void __user *buffer, size_t *lenp, loff_t *ppos)
4386{
4387 int *valp = ctl->data;
4388 int val = *valp;
Eric W. Biederman88af1822010-02-19 13:22:59 +00004389 loff_t pos = *ppos;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00004390 ctl_table lctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391 int ret;
4392
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00004393 /*
4394 * ctl->data points to idev->cnf.forwarding, we should
4395 * not modify it until we get the rtnl lock.
4396 */
4397 lctl = *ctl;
4398 lctl.data = &val;
4399
4400 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -08004402 if (write)
Stephen Hemmingerb325fdd2009-02-26 06:55:31 +00004403 ret = addrconf_fixup_forwarding(ctl, valp, val);
Eric W. Biederman88af1822010-02-19 13:22:59 +00004404 if (ret)
4405 *ppos = pos;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004406 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004407}
4408
Brian Haley56d417b2009-06-01 03:07:33 -07004409static void dev_disable_change(struct inet6_dev *idev)
4410{
4411 if (!idev || !idev->dev)
4412 return;
4413
4414 if (idev->cnf.disable_ipv6)
4415 addrconf_notify(NULL, NETDEV_DOWN, idev->dev);
4416 else
4417 addrconf_notify(NULL, NETDEV_UP, idev->dev);
4418}
4419
4420static void addrconf_disable_change(struct net *net, __s32 newf)
4421{
4422 struct net_device *dev;
4423 struct inet6_dev *idev;
4424
Eric Dumazetc6d14c82009-11-04 05:43:23 -08004425 rcu_read_lock();
4426 for_each_netdev_rcu(net, dev) {
Brian Haley56d417b2009-06-01 03:07:33 -07004427 idev = __in6_dev_get(dev);
4428 if (idev) {
4429 int changed = (!idev->cnf.disable_ipv6) ^ (!newf);
4430 idev->cnf.disable_ipv6 = newf;
4431 if (changed)
4432 dev_disable_change(idev);
4433 }
Brian Haley56d417b2009-06-01 03:07:33 -07004434 }
Eric Dumazetc6d14c82009-11-04 05:43:23 -08004435 rcu_read_unlock();
Brian Haley56d417b2009-06-01 03:07:33 -07004436}
4437
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00004438static int addrconf_disable_ipv6(struct ctl_table *table, int *p, int newf)
Brian Haley56d417b2009-06-01 03:07:33 -07004439{
4440 struct net *net;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00004441 int old;
4442
4443 if (!rtnl_trylock())
4444 return restart_syscall();
Brian Haley56d417b2009-06-01 03:07:33 -07004445
4446 net = (struct net *)table->extra2;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00004447 old = *p;
4448 *p = newf;
Brian Haley56d417b2009-06-01 03:07:33 -07004449
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00004450 if (p == &net->ipv6.devconf_dflt->disable_ipv6) {
4451 rtnl_unlock();
Brian Haley56d417b2009-06-01 03:07:33 -07004452 return 0;
Eric W. Biederman88af1822010-02-19 13:22:59 +00004453 }
Brian Haley56d417b2009-06-01 03:07:33 -07004454
4455 if (p == &net->ipv6.devconf_all->disable_ipv6) {
Brian Haley56d417b2009-06-01 03:07:33 -07004456 net->ipv6.devconf_dflt->disable_ipv6 = newf;
4457 addrconf_disable_change(net, newf);
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00004458 } else if ((!newf) ^ (!old))
Brian Haley56d417b2009-06-01 03:07:33 -07004459 dev_disable_change((struct inet6_dev *)table->extra1);
4460
4461 rtnl_unlock();
4462 return 0;
4463}
4464
4465static
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07004466int addrconf_sysctl_disable(ctl_table *ctl, int write,
Brian Haley56d417b2009-06-01 03:07:33 -07004467 void __user *buffer, size_t *lenp, loff_t *ppos)
4468{
4469 int *valp = ctl->data;
4470 int val = *valp;
Eric W. Biederman88af1822010-02-19 13:22:59 +00004471 loff_t pos = *ppos;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00004472 ctl_table lctl;
Brian Haley56d417b2009-06-01 03:07:33 -07004473 int ret;
4474
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00004475 /*
4476 * ctl->data points to idev->cnf.disable_ipv6, we should
4477 * not modify it until we get the rtnl lock.
4478 */
4479 lctl = *ctl;
4480 lctl.data = &val;
4481
4482 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
Brian Haley56d417b2009-06-01 03:07:33 -07004483
4484 if (write)
4485 ret = addrconf_disable_ipv6(ctl, valp, val);
Eric W. Biederman88af1822010-02-19 13:22:59 +00004486 if (ret)
4487 *ppos = pos;
Brian Haley56d417b2009-06-01 03:07:33 -07004488 return ret;
4489}
4490
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491static struct addrconf_sysctl_table
4492{
4493 struct ctl_table_header *sysctl_header;
YOSHIFUJI Hideakif6a07b22008-03-25 00:25:11 +09004494 ctl_table addrconf_vars[DEVCONF_MAX+1];
Brian Haleyab32ea52006-09-22 14:15:41 -07004495} addrconf_sysctl __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496 .sysctl_header = NULL,
4497 .addrconf_vars = {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004498 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004499 .procname = "forwarding",
4500 .data = &ipv6_devconf.forwarding,
4501 .maxlen = sizeof(int),
4502 .mode = 0644,
4503 .proc_handler = addrconf_sysctl_forward,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004504 },
4505 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004506 .procname = "hop_limit",
4507 .data = &ipv6_devconf.hop_limit,
4508 .maxlen = sizeof(int),
4509 .mode = 0644,
4510 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511 },
4512 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004513 .procname = "mtu",
4514 .data = &ipv6_devconf.mtu6,
4515 .maxlen = sizeof(int),
4516 .mode = 0644,
4517 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518 },
4519 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004520 .procname = "accept_ra",
4521 .data = &ipv6_devconf.accept_ra,
4522 .maxlen = sizeof(int),
4523 .mode = 0644,
4524 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525 },
4526 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004527 .procname = "accept_redirects",
4528 .data = &ipv6_devconf.accept_redirects,
4529 .maxlen = sizeof(int),
4530 .mode = 0644,
4531 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532 },
4533 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004534 .procname = "autoconf",
4535 .data = &ipv6_devconf.autoconf,
4536 .maxlen = sizeof(int),
4537 .mode = 0644,
4538 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539 },
4540 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004541 .procname = "dad_transmits",
4542 .data = &ipv6_devconf.dad_transmits,
4543 .maxlen = sizeof(int),
4544 .mode = 0644,
4545 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546 },
4547 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004548 .procname = "router_solicitations",
4549 .data = &ipv6_devconf.rtr_solicits,
4550 .maxlen = sizeof(int),
4551 .mode = 0644,
4552 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004553 },
4554 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004555 .procname = "router_solicitation_interval",
4556 .data = &ipv6_devconf.rtr_solicit_interval,
4557 .maxlen = sizeof(int),
4558 .mode = 0644,
4559 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560 },
4561 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004562 .procname = "router_solicitation_delay",
4563 .data = &ipv6_devconf.rtr_solicit_delay,
4564 .maxlen = sizeof(int),
4565 .mode = 0644,
4566 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567 },
4568 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004569 .procname = "force_mld_version",
4570 .data = &ipv6_devconf.force_mld_version,
4571 .maxlen = sizeof(int),
4572 .mode = 0644,
4573 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574 },
4575#ifdef CONFIG_IPV6_PRIVACY
4576 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004577 .procname = "use_tempaddr",
4578 .data = &ipv6_devconf.use_tempaddr,
4579 .maxlen = sizeof(int),
4580 .mode = 0644,
4581 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582 },
4583 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004584 .procname = "temp_valid_lft",
4585 .data = &ipv6_devconf.temp_valid_lft,
4586 .maxlen = sizeof(int),
4587 .mode = 0644,
4588 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589 },
4590 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004591 .procname = "temp_prefered_lft",
4592 .data = &ipv6_devconf.temp_prefered_lft,
4593 .maxlen = sizeof(int),
4594 .mode = 0644,
4595 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596 },
4597 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004598 .procname = "regen_max_retry",
4599 .data = &ipv6_devconf.regen_max_retry,
4600 .maxlen = sizeof(int),
4601 .mode = 0644,
4602 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603 },
4604 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004605 .procname = "max_desync_factor",
4606 .data = &ipv6_devconf.max_desync_factor,
4607 .maxlen = sizeof(int),
4608 .mode = 0644,
4609 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610 },
4611#endif
4612 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004613 .procname = "max_addresses",
4614 .data = &ipv6_devconf.max_addresses,
4615 .maxlen = sizeof(int),
4616 .mode = 0644,
4617 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618 },
4619 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004620 .procname = "accept_ra_defrtr",
4621 .data = &ipv6_devconf.accept_ra_defrtr,
4622 .maxlen = sizeof(int),
4623 .mode = 0644,
4624 .proc_handler = proc_dointvec,
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -08004625 },
4626 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004627 .procname = "accept_ra_pinfo",
4628 .data = &ipv6_devconf.accept_ra_pinfo,
4629 .maxlen = sizeof(int),
4630 .mode = 0644,
4631 .proc_handler = proc_dointvec,
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -08004632 },
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08004633#ifdef CONFIG_IPV6_ROUTER_PREF
4634 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004635 .procname = "accept_ra_rtr_pref",
4636 .data = &ipv6_devconf.accept_ra_rtr_pref,
4637 .maxlen = sizeof(int),
4638 .mode = 0644,
4639 .proc_handler = proc_dointvec,
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08004640 },
YOSHIFUJI Hideaki52e16352006-03-20 17:05:47 -08004641 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004642 .procname = "router_probe_interval",
4643 .data = &ipv6_devconf.rtr_probe_interval,
4644 .maxlen = sizeof(int),
4645 .mode = 0644,
4646 .proc_handler = proc_dointvec_jiffies,
YOSHIFUJI Hideaki52e16352006-03-20 17:05:47 -08004647 },
Neil Hormanfa03ef32007-01-30 14:30:10 -08004648#ifdef CONFIG_IPV6_ROUTE_INFO
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -08004649 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004650 .procname = "accept_ra_rt_info_max_plen",
4651 .data = &ipv6_devconf.accept_ra_rt_info_max_plen,
4652 .maxlen = sizeof(int),
4653 .mode = 0644,
4654 .proc_handler = proc_dointvec,
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -08004655 },
4656#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08004657#endif
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -08004658 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004659 .procname = "proxy_ndp",
4660 .data = &ipv6_devconf.proxy_ndp,
4661 .maxlen = sizeof(int),
4662 .mode = 0644,
4663 .proc_handler = proc_dointvec,
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -07004664 },
4665 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004666 .procname = "accept_source_route",
4667 .data = &ipv6_devconf.accept_source_route,
4668 .maxlen = sizeof(int),
4669 .mode = 0644,
4670 .proc_handler = proc_dointvec,
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -07004671 },
Neil Horman95c385b2007-04-25 17:08:10 -07004672#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
4673 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004674 .procname = "optimistic_dad",
4675 .data = &ipv6_devconf.optimistic_dad,
4676 .maxlen = sizeof(int),
4677 .mode = 0644,
4678 .proc_handler = proc_dointvec,
Neil Horman95c385b2007-04-25 17:08:10 -07004679
4680 },
4681#endif
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09004682#ifdef CONFIG_IPV6_MROUTE
4683 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004684 .procname = "mc_forwarding",
4685 .data = &ipv6_devconf.mc_forwarding,
4686 .maxlen = sizeof(int),
4687 .mode = 0444,
4688 .proc_handler = proc_dointvec,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09004689 },
4690#endif
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -07004691 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004692 .procname = "disable_ipv6",
4693 .data = &ipv6_devconf.disable_ipv6,
4694 .maxlen = sizeof(int),
4695 .mode = 0644,
4696 .proc_handler = addrconf_sysctl_disable,
YOSHIFUJI Hideaki778d80b2008-06-28 14:17:11 +09004697 },
4698 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004699 .procname = "accept_dad",
4700 .data = &ipv6_devconf.accept_dad,
4701 .maxlen = sizeof(int),
4702 .mode = 0644,
4703 .proc_handler = proc_dointvec,
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09004704 },
4705 {
Octavian Purdilaf7734fd2009-10-02 11:39:15 +00004706 .procname = "force_tllao",
4707 .data = &ipv6_devconf.force_tllao,
4708 .maxlen = sizeof(int),
4709 .mode = 0644,
4710 .proc_handler = proc_dointvec
4711 },
4712 {
Hannes Frederic Sowa5cb04432012-11-06 16:46:20 +00004713 .procname = "ndisc_notify",
4714 .data = &ipv6_devconf.ndisc_notify,
4715 .maxlen = sizeof(int),
4716 .mode = 0644,
4717 .proc_handler = proc_dointvec
4718 },
4719 {
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08004720 /* sentinel */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721 }
4722 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07004723};
4724
Pavel Emelyanovbff16c22008-01-10 17:42:13 -08004725static int __addrconf_sysctl_register(struct net *net, char *dev_name,
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08004726 struct inet6_dev *idev, struct ipv6_devconf *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004727{
4728 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004729 struct addrconf_sysctl_table *t;
Eric W. Biederman6105e292012-04-19 13:41:24 +00004730 char path[sizeof("net/ipv6/conf/") + IFNAMSIZ];
Pavel Emelyanov1dab6222007-12-02 00:59:38 +11004731
Arnaldo Carvalho de Meloaf879cc2006-11-17 12:14:37 -02004732 t = kmemdup(&addrconf_sysctl, sizeof(*t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733 if (t == NULL)
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11004734 goto out;
4735
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07004736 for (i = 0; t->addrconf_vars[i].data; i++) {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004737 t->addrconf_vars[i].data += (char *)p - (char *)&ipv6_devconf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004738 t->addrconf_vars[i].extra1 = idev; /* embedded; no ref */
Pavel Emelyanovbff16c22008-01-10 17:42:13 -08004739 t->addrconf_vars[i].extra2 = net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741
Eric W. Biederman6105e292012-04-19 13:41:24 +00004742 snprintf(path, sizeof(path), "net/ipv6/conf/%s", dev_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004743
Eric W. Biederman6105e292012-04-19 13:41:24 +00004744 t->sysctl_header = register_net_sysctl(net, path, t->addrconf_vars);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004745 if (t->sysctl_header == NULL)
Eric W. Biederman6105e292012-04-19 13:41:24 +00004746 goto free;
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11004747
4748 p->sysctl = t;
Pavel Emelyanov95897312008-01-10 17:41:45 -08004749 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004750
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11004751free:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004752 kfree(t);
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11004753out:
Pavel Emelyanov95897312008-01-10 17:41:45 -08004754 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755}
4756
Pavel Emelyanov408c4762008-01-10 17:41:21 -08004757static void __addrconf_sysctl_unregister(struct ipv6_devconf *p)
4758{
4759 struct addrconf_sysctl_table *t;
4760
4761 if (p->sysctl == NULL)
4762 return;
4763
4764 t = p->sysctl;
4765 p->sysctl = NULL;
Lucian Adrian Grijincuff538812011-05-01 01:44:01 +00004766 unregister_net_sysctl_table(t->sysctl_header);
Pavel Emelyanov408c4762008-01-10 17:41:21 -08004767 kfree(t);
4768}
4769
Pavel Emelyanovf52295a2007-12-02 00:58:37 +11004770static void addrconf_sysctl_register(struct inet6_dev *idev)
4771{
Eric W. Biederman54716e32010-02-14 03:27:03 +00004772 neigh_sysctl_register(idev->dev, idev->nd_parms, "ipv6",
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08004773 &ndisc_ifinfo_sysctl_change);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09004774 __addrconf_sysctl_register(dev_net(idev->dev), idev->dev->name,
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08004775 idev, &idev->cnf);
Pavel Emelyanovf52295a2007-12-02 00:58:37 +11004776}
4777
Pavel Emelyanov408c4762008-01-10 17:41:21 -08004778static void addrconf_sysctl_unregister(struct inet6_dev *idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779{
Pavel Emelyanov408c4762008-01-10 17:41:21 -08004780 __addrconf_sysctl_unregister(&idev->cnf);
4781 neigh_sysctl_unregister(idev->nd_parms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782}
4783
4784
4785#endif
4786
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004787static int __net_init addrconf_init_net(struct net *net)
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08004788{
4789 int err;
4790 struct ipv6_devconf *all, *dflt;
4791
4792 err = -ENOMEM;
4793 all = &ipv6_devconf;
4794 dflt = &ipv6_devconf_dflt;
4795
Octavian Purdila09ad9bc2009-11-25 15:14:13 -08004796 if (!net_eq(net, &init_net)) {
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08004797 all = kmemdup(all, sizeof(ipv6_devconf), GFP_KERNEL);
4798 if (all == NULL)
4799 goto err_alloc_all;
4800
4801 dflt = kmemdup(dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL);
4802 if (dflt == NULL)
4803 goto err_alloc_dflt;
Brian Haley56d417b2009-06-01 03:07:33 -07004804 } else {
4805 /* these will be inherited by all namespaces */
4806 dflt->autoconf = ipv6_defaults.autoconf;
4807 dflt->disable_ipv6 = ipv6_defaults.disable_ipv6;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08004808 }
4809
4810 net->ipv6.devconf_all = all;
4811 net->ipv6.devconf_dflt = dflt;
4812
4813#ifdef CONFIG_SYSCTL
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08004814 err = __addrconf_sysctl_register(net, "all", NULL, all);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08004815 if (err < 0)
4816 goto err_reg_all;
4817
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08004818 err = __addrconf_sysctl_register(net, "default", NULL, dflt);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08004819 if (err < 0)
4820 goto err_reg_dflt;
4821#endif
4822 return 0;
4823
4824#ifdef CONFIG_SYSCTL
4825err_reg_dflt:
4826 __addrconf_sysctl_unregister(all);
4827err_reg_all:
4828 kfree(dflt);
4829#endif
4830err_alloc_dflt:
4831 kfree(all);
4832err_alloc_all:
4833 return err;
4834}
4835
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004836static void __net_exit addrconf_exit_net(struct net *net)
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08004837{
4838#ifdef CONFIG_SYSCTL
4839 __addrconf_sysctl_unregister(net->ipv6.devconf_dflt);
4840 __addrconf_sysctl_unregister(net->ipv6.devconf_all);
4841#endif
Octavian Purdila09ad9bc2009-11-25 15:14:13 -08004842 if (!net_eq(net, &init_net)) {
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08004843 kfree(net->ipv6.devconf_dflt);
4844 kfree(net->ipv6.devconf_all);
4845 }
4846}
4847
4848static struct pernet_operations addrconf_ops = {
4849 .init = addrconf_init_net,
4850 .exit = addrconf_exit_net,
4851};
4852
Linus Torvalds1da177e2005-04-16 15:20:36 -07004853/*
4854 * Device notifier
4855 */
4856
4857int register_inet6addr_notifier(struct notifier_block *nb)
4858{
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004859 return atomic_notifier_chain_register(&inet6addr_chain, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860}
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +09004861EXPORT_SYMBOL(register_inet6addr_notifier);
4862
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863int unregister_inet6addr_notifier(struct notifier_block *nb)
4864{
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004865 return atomic_notifier_chain_unregister(&inet6addr_chain, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004866}
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +09004867EXPORT_SYMBOL(unregister_inet6addr_notifier);
4868
Thomas Grafb382b192010-11-16 04:33:57 +00004869static struct rtnl_af_ops inet6_ops = {
4870 .family = AF_INET6,
4871 .fill_link_af = inet6_fill_link_af,
4872 .get_link_af_size = inet6_get_link_af_size,
Thomas Grafb382b192010-11-16 04:33:57 +00004873};
4874
Linus Torvalds1da177e2005-04-16 15:20:36 -07004875/*
4876 * Init / cleanup code
4877 */
4878
4879int __init addrconf_init(void)
4880{
stephen hemmingerc2e21292010-03-17 20:31:10 +00004881 int i, err;
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +09004882
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004883 err = ipv6_addr_label_init();
4884 if (err < 0) {
Joe Perchesf3213832012-05-15 14:11:53 +00004885 pr_crit("%s: cannot initialize default policy table: %d\n",
4886 __func__, err);
Neil Horman2cc6d2b2010-09-24 09:55:52 +00004887 goto out;
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +09004888 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004889
Neil Horman2cc6d2b2010-09-24 09:55:52 +00004890 err = register_pernet_subsys(&addrconf_ops);
4891 if (err < 0)
4892 goto out_addrlabel;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08004893
Linus Torvalds1da177e2005-04-16 15:20:36 -07004894 /* The addrconf netdev notifier requires that loopback_dev
4895 * has it's ipv6 private information allocated and setup
4896 * before it can bring up and give link-local addresses
4897 * to other devices which are up.
4898 *
4899 * Unfortunately, loopback_dev is not necessarily the first
4900 * entry in the global dev_base list of net devices. In fact,
4901 * it is likely to be the very last entry on that list.
4902 * So this causes the notifier registry below to try and
4903 * give link-local addresses to all devices besides loopback_dev
4904 * first, then loopback_dev, which cases all the non-loopback_dev
4905 * devices to fail to get a link-local address.
4906 *
4907 * So, as a temporary fix, allocate the ipv6 structure for
4908 * loopback_dev first by hand.
4909 * Longer term, all of the dependencies ipv6 has upon the loopback
4910 * device and it being up should be removed.
4911 */
4912 rtnl_lock();
Eric W. Biederman2774c7a2007-09-26 22:10:56 -07004913 if (!ipv6_add_dev(init_net.loopback_dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914 err = -ENOMEM;
4915 rtnl_unlock();
4916 if (err)
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08004917 goto errlo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004918
stephen hemmingerc2e21292010-03-17 20:31:10 +00004919 for (i = 0; i < IN6_ADDR_HSIZE; i++)
4920 INIT_HLIST_HEAD(&inet6_addr_lst[i]);
4921
Linus Torvalds1da177e2005-04-16 15:20:36 -07004922 register_netdevice_notifier(&ipv6_dev_notf);
4923
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924 addrconf_verify(0);
Thomas Grafc127ea22007-03-22 11:58:32 -07004925
Thomas Grafb382b192010-11-16 04:33:57 +00004926 err = rtnl_af_register(&inet6_ops);
4927 if (err < 0)
4928 goto errout_af;
4929
Greg Rosec7ac8672011-06-10 01:27:09 +00004930 err = __rtnl_register(PF_INET6, RTM_GETLINK, NULL, inet6_dump_ifinfo,
4931 NULL);
Thomas Grafc127ea22007-03-22 11:58:32 -07004932 if (err < 0)
4933 goto errout;
4934
4935 /* Only the first call to __rtnl_register can fail */
Greg Rosec7ac8672011-06-10 01:27:09 +00004936 __rtnl_register(PF_INET6, RTM_NEWADDR, inet6_rtm_newaddr, NULL, NULL);
4937 __rtnl_register(PF_INET6, RTM_DELADDR, inet6_rtm_deladdr, NULL, NULL);
4938 __rtnl_register(PF_INET6, RTM_GETADDR, inet6_rtm_getaddr,
4939 inet6_dump_ifaddr, NULL);
4940 __rtnl_register(PF_INET6, RTM_GETMULTICAST, NULL,
4941 inet6_dump_ifmcaddr, NULL);
4942 __rtnl_register(PF_INET6, RTM_GETANYCAST, NULL,
4943 inet6_dump_ifacaddr, NULL);
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +00004944 __rtnl_register(PF_INET6, RTM_GETNETCONF, inet6_netconf_get_devconf,
4945 NULL, NULL);
Thomas Grafc127ea22007-03-22 11:58:32 -07004946
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +09004947 ipv6_addr_label_rtnl_register();
4948
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949 return 0;
Thomas Grafc127ea22007-03-22 11:58:32 -07004950errout:
Thomas Grafb382b192010-11-16 04:33:57 +00004951 rtnl_af_unregister(&inet6_ops);
4952errout_af:
Thomas Grafc127ea22007-03-22 11:58:32 -07004953 unregister_netdevice_notifier(&ipv6_dev_notf);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08004954errlo:
4955 unregister_pernet_subsys(&addrconf_ops);
Neil Horman2cc6d2b2010-09-24 09:55:52 +00004956out_addrlabel:
4957 ipv6_addr_label_cleanup();
4958out:
Thomas Grafc127ea22007-03-22 11:58:32 -07004959 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004960}
4961
Daniel Lezcano09f77092007-12-13 05:34:58 -08004962void addrconf_cleanup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004963{
Daniel Lezcano176c39a2009-03-03 01:06:45 -08004964 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004965 int i;
4966
4967 unregister_netdevice_notifier(&ipv6_dev_notf);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08004968 unregister_pernet_subsys(&addrconf_ops);
Neil Horman2cc6d2b2010-09-24 09:55:52 +00004969 ipv6_addr_label_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004970
4971 rtnl_lock();
4972
Thomas Grafb382b192010-11-16 04:33:57 +00004973 __rtnl_af_unregister(&inet6_ops);
4974
Daniel Lezcano176c39a2009-03-03 01:06:45 -08004975 /* clean dev list */
4976 for_each_netdev(&init_net, dev) {
4977 if (__in6_dev_get(dev) == NULL)
4978 continue;
4979 addrconf_ifdown(dev, 1);
4980 }
4981 addrconf_ifdown(init_net.loopback_dev, 2);
4982
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004984 * Check hash table.
4985 */
stephen hemminger5c578ae2010-03-17 20:31:11 +00004986 spin_lock_bh(&addrconf_hash_lock);
stephen hemmingerc2e21292010-03-17 20:31:10 +00004987 for (i = 0; i < IN6_ADDR_HSIZE; i++)
4988 WARN_ON(!hlist_empty(&inet6_addr_lst[i]));
stephen hemminger5c578ae2010-03-17 20:31:11 +00004989 spin_unlock_bh(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004990
4991 del_timer(&addr_chk_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004992 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004993}