blob: 65d8139d60c50650400e11a40e463d6b5415d584 [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>
YOSHIFUJI Hideaki / 吉藤英明cb6bf352013-03-25 08:26:24 +000073#include <net/firewire.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#include <net/ipv6.h>
75#include <net/protocol.h>
76#include <net/ndisc.h>
77#include <net/ip6_route.h>
78#include <net/addrconf.h>
79#include <net/tcp.h>
80#include <net/ip.h>
Thomas Graf5d620262006-08-15 00:35:02 -070081#include <net/netlink.h>
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -070082#include <net/pkt_sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070083#include <linux/if_tunnel.h>
84#include <linux/rtnetlink.h>
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +000085#include <linux/netconf.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87#ifdef CONFIG_IPV6_PRIVACY
88#include <linux/random.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070089#endif
90
Stephen Hemmingere21e8462010-03-20 16:09:01 -070091#include <linux/uaccess.h>
Herbert Xu7f7d9a62007-04-24 21:54:09 -070092#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
94#include <linux/proc_fs.h>
95#include <linux/seq_file.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040096#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98/* Set to 3 to get tracing... */
99#define ACONF_DEBUG 2
100
101#if ACONF_DEBUG >= 3
102#define ADBG(x) printk x
103#else
104#define ADBG(x)
105#endif
106
107#define INFINITY_LIFE_TIME 0xFFFFFFFF
Thomas Graf18a31e12010-11-17 04:12:02 +0000108
109static inline u32 cstamp_delta(unsigned long cstamp)
110{
111 return (cstamp - INITIAL_JIFFIES) * 100UL / HZ;
112}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114#ifdef CONFIG_SYSCTL
Pavel Emelyanovf52295a2007-12-02 00:58:37 +1100115static void addrconf_sysctl_register(struct inet6_dev *idev);
Pavel Emelyanov408c4762008-01-10 17:41:21 -0800116static void addrconf_sysctl_unregister(struct inet6_dev *idev);
117#else
118static inline void addrconf_sysctl_register(struct inet6_dev *idev)
119{
120}
121
122static inline void addrconf_sysctl_unregister(struct inet6_dev *idev)
123{
124}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125#endif
126
127#ifdef CONFIG_IPV6_PRIVACY
Sorin Dumitrueb7e0572012-08-30 02:01:45 +0000128static void __ipv6_regen_rndid(struct inet6_dev *idev);
129static void __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130static void ipv6_regen_rndid(unsigned long data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131#endif
132
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +0900133static int ipv6_generate_eui64(u8 *eui, struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134static int ipv6_count_addresses(struct inet6_dev *idev);
135
136/*
137 * Configured unicast address hash table
138 */
stephen hemmingerc2e21292010-03-17 20:31:10 +0000139static struct hlist_head inet6_addr_lst[IN6_ADDR_HSIZE];
stephen hemminger5c578aed2010-03-17 20:31:11 +0000140static DEFINE_SPINLOCK(addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142static void addrconf_verify(unsigned long);
143
Ingo Molnar8d06afa2005-09-09 13:10:40 -0700144static DEFINE_TIMER(addr_chk_timer, addrconf_verify, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145static DEFINE_SPINLOCK(addrconf_verify_lock);
146
147static void addrconf_join_anycast(struct inet6_ifaddr *ifp);
148static void addrconf_leave_anycast(struct inet6_ifaddr *ifp);
149
Jiri Pirko93d9b7d2010-03-10 10:28:56 +0000150static void addrconf_type_change(struct net_device *dev,
151 unsigned long event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152static int addrconf_ifdown(struct net_device *dev, int how);
153
Romain Kuntz21caa662013-01-09 21:06:03 +0000154static struct rt6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
155 int plen,
156 const struct net_device *dev,
157 u32 flags, u32 noflags);
158
David S. Millercf22f9a2012-04-14 21:37:40 -0400159static void addrconf_dad_start(struct inet6_ifaddr *ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160static void addrconf_dad_timer(unsigned long data);
161static void addrconf_dad_completed(struct inet6_ifaddr *ifp);
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +0900162static void addrconf_dad_run(struct inet6_dev *idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163static void addrconf_rs_timer(unsigned long data);
164static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
165static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
166
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900167static void inet6_prefix_notify(int event, struct inet6_dev *idev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 struct prefix_info *pinfo);
David S. Miller3e81c6d2010-03-20 16:18:00 -0700169static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
170 struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Alan Sterne041c682006-03-27 01:16:30 -0800172static ATOMIC_NOTIFIER_HEAD(inet6addr_chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Adrian Bunk888c8482008-07-22 14:21:58 -0700174static struct ipv6_devconf ipv6_devconf __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 .forwarding = 0,
176 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
177 .mtu6 = IPV6_MIN_MTU,
178 .accept_ra = 1,
179 .accept_redirects = 1,
180 .autoconf = 1,
181 .force_mld_version = 0,
182 .dad_transmits = 1,
183 .rtr_solicits = MAX_RTR_SOLICITATIONS,
184 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
185 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
186#ifdef CONFIG_IPV6_PRIVACY
187 .use_tempaddr = 0,
188 .temp_valid_lft = TEMP_VALID_LIFETIME,
189 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
190 .regen_max_retry = REGEN_MAX_RETRY,
191 .max_desync_factor = MAX_DESYNC_FACTOR,
192#endif
193 .max_addresses = IPV6_MAX_ADDRESSES,
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -0800194 .accept_ra_defrtr = 1,
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -0800195 .accept_ra_pinfo = 1,
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800196#ifdef CONFIG_IPV6_ROUTER_PREF
197 .accept_ra_rtr_pref = 1,
YOSHIFUJI Hideaki52e163562006-03-20 17:05:47 -0800198 .rtr_probe_interval = 60 * HZ,
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -0800199#ifdef CONFIG_IPV6_ROUTE_INFO
200 .accept_ra_rt_info_max_plen = 0,
201#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800202#endif
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -0700203 .proxy_ndp = 0,
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -0700204 .accept_source_route = 0, /* we do not accept RH0 by default. */
YOSHIFUJI Hideaki778d80b2008-06-28 14:17:11 +0900205 .disable_ipv6 = 0,
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +0900206 .accept_dad = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207};
208
Brian Haleyab32ea52006-09-22 14:15:41 -0700209static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 .forwarding = 0,
211 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
212 .mtu6 = IPV6_MIN_MTU,
213 .accept_ra = 1,
214 .accept_redirects = 1,
215 .autoconf = 1,
216 .dad_transmits = 1,
217 .rtr_solicits = MAX_RTR_SOLICITATIONS,
218 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
219 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
220#ifdef CONFIG_IPV6_PRIVACY
221 .use_tempaddr = 0,
222 .temp_valid_lft = TEMP_VALID_LIFETIME,
223 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
224 .regen_max_retry = REGEN_MAX_RETRY,
225 .max_desync_factor = MAX_DESYNC_FACTOR,
226#endif
227 .max_addresses = IPV6_MAX_ADDRESSES,
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -0800228 .accept_ra_defrtr = 1,
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -0800229 .accept_ra_pinfo = 1,
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800230#ifdef CONFIG_IPV6_ROUTER_PREF
231 .accept_ra_rtr_pref = 1,
YOSHIFUJI Hideaki52e163562006-03-20 17:05:47 -0800232 .rtr_probe_interval = 60 * HZ,
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -0800233#ifdef CONFIG_IPV6_ROUTE_INFO
234 .accept_ra_rt_info_max_plen = 0,
235#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800236#endif
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -0700237 .proxy_ndp = 0,
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -0700238 .accept_source_route = 0, /* we do not accept RH0 by default. */
YOSHIFUJI Hideaki778d80b2008-06-28 14:17:11 +0900239 .disable_ipv6 = 0,
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +0900240 .accept_dad = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241};
242
243/* IPv6 Wildcard Address and Loopback Address defined by RFC2553 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +0900246const struct in6_addr in6addr_linklocal_allnodes = IN6ADDR_LINKLOCAL_ALLNODES_INIT;
247const struct in6_addr in6addr_linklocal_allrouters = IN6ADDR_LINKLOCAL_ALLROUTERS_INIT;
Hannes Frederic Sowa2c5e8932013-02-10 03:50:18 +0000248const struct in6_addr in6addr_interfacelocal_allnodes = IN6ADDR_INTERFACELOCAL_ALLNODES_INIT;
249const struct in6_addr in6addr_interfacelocal_allrouters = IN6ADDR_INTERFACELOCAL_ALLROUTERS_INIT;
250const struct in6_addr in6addr_sitelocal_allrouters = IN6ADDR_SITELOCAL_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
Daniel Borkmannf53adae2013-04-08 04:01:30 +0000425 memset(ndev->token.s6_addr, 0, sizeof(ndev->token.s6_addr));
Ingo Oeser322f74a2006-03-20 23:01:47 -0800426
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -0700427 if (netif_running(dev) && addrconf_qdisc_ok(dev))
Herbert Xu53aadcc2007-03-27 14:31:52 -0700428 ndev->if_flags |= IF_READY;
429
Ingo Oeser322f74a2006-03-20 23:01:47 -0800430 ipv6_mc_init_dev(ndev);
431 ndev->tstamp = jiffies;
Pavel Emelyanovf52295a2007-12-02 00:58:37 +1100432 addrconf_sysctl_register(ndev);
David L Stevens30c4cf52007-01-04 12:31:14 -0800433 /* protected by rtnl_lock */
Eric Dumazetcf778b02012-01-12 04:41:32 +0000434 rcu_assign_pointer(dev->ip6_ptr, ndev);
YOSHIFUJI Hideakid88ae4c2007-01-14 21:48:40 -0800435
Hannes Frederic Sowa2c5e8932013-02-10 03:50:18 +0000436 /* Join interface-local all-node multicast group */
437 ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allnodes);
438
YOSHIFUJI Hideakid88ae4c2007-01-14 21:48:40 -0800439 /* Join all-node multicast group */
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +0900440 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allnodes);
YOSHIFUJI Hideakid88ae4c2007-01-14 21:48:40 -0800441
Li Weid6ddef92012-03-05 14:45:17 +0000442 /* Join all-router multicast group if forwarding is set */
Li Wei8b2aaed2012-03-07 14:58:07 +0000443 if (ndev->cnf.forwarding && (dev->flags & IFF_MULTICAST))
Li Weid6ddef92012-03-05 14:45:17 +0000444 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
445
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 return ndev;
447}
448
Eldad Zack8e5e8f32012-04-01 07:49:08 +0000449static struct inet6_dev *ipv6_find_idev(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450{
451 struct inet6_dev *idev;
452
453 ASSERT_RTNL();
454
Stephen Hemmingere21e8462010-03-20 16:09:01 -0700455 idev = __in6_dev_get(dev);
456 if (!idev) {
457 idev = ipv6_add_dev(dev);
458 if (!idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 return NULL;
460 }
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +0900461
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 if (dev->flags&IFF_UP)
463 ipv6_mc_up(idev);
464 return idev;
465}
466
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000467static int inet6_netconf_msgsize_devconf(int type)
468{
469 int size = NLMSG_ALIGN(sizeof(struct netconfmsg))
470 + nla_total_size(4); /* NETCONFA_IFINDEX */
471
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000472 /* type -1 is used for ALL */
473 if (type == -1 || type == NETCONFA_FORWARDING)
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000474 size += nla_total_size(4);
David S. Millerb1afce92012-12-04 14:46:34 -0500475#ifdef CONFIG_IPV6_MROUTE
Nicolas Dichteld67b8c62012-12-04 01:13:35 +0000476 if (type == -1 || type == NETCONFA_MC_FORWARDING)
477 size += nla_total_size(4);
David S. Millerb1afce92012-12-04 14:46:34 -0500478#endif
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000479
480 return size;
481}
482
483static int inet6_netconf_fill_devconf(struct sk_buff *skb, int ifindex,
484 struct ipv6_devconf *devconf, u32 portid,
485 u32 seq, int event, unsigned int flags,
486 int type)
487{
488 struct nlmsghdr *nlh;
489 struct netconfmsg *ncm;
490
491 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct netconfmsg),
492 flags);
493 if (nlh == NULL)
494 return -EMSGSIZE;
495
496 ncm = nlmsg_data(nlh);
497 ncm->ncm_family = AF_INET6;
498
499 if (nla_put_s32(skb, NETCONFA_IFINDEX, ifindex) < 0)
500 goto nla_put_failure;
501
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000502 /* type -1 is used for ALL */
503 if ((type == -1 || type == NETCONFA_FORWARDING) &&
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000504 nla_put_s32(skb, NETCONFA_FORWARDING, devconf->forwarding) < 0)
505 goto nla_put_failure;
David S. Millerb1afce92012-12-04 14:46:34 -0500506#ifdef CONFIG_IPV6_MROUTE
Nicolas Dichteld67b8c62012-12-04 01:13:35 +0000507 if ((type == -1 || type == NETCONFA_MC_FORWARDING) &&
508 nla_put_s32(skb, NETCONFA_MC_FORWARDING,
509 devconf->mc_forwarding) < 0)
510 goto nla_put_failure;
David S. Millerb1afce92012-12-04 14:46:34 -0500511#endif
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000512 return nlmsg_end(skb, nlh);
513
514nla_put_failure:
515 nlmsg_cancel(skb, nlh);
516 return -EMSGSIZE;
517}
518
Nicolas Dichteld67b8c62012-12-04 01:13:35 +0000519void inet6_netconf_notify_devconf(struct net *net, int type, int ifindex,
520 struct ipv6_devconf *devconf)
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000521{
522 struct sk_buff *skb;
523 int err = -ENOBUFS;
524
525 skb = nlmsg_new(inet6_netconf_msgsize_devconf(type), GFP_ATOMIC);
526 if (skb == NULL)
527 goto errout;
528
529 err = inet6_netconf_fill_devconf(skb, ifindex, devconf, 0, 0,
530 RTM_NEWNETCONF, 0, type);
531 if (err < 0) {
532 /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
533 WARN_ON(err == -EMSGSIZE);
534 kfree_skb(skb);
535 goto errout;
536 }
537 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_NETCONF, NULL, GFP_ATOMIC);
538 return;
539errout:
Cong Dingbd779022012-12-18 12:08:56 +0000540 rtnl_set_sk_err(net, RTNLGRP_IPV6_NETCONF, err);
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000541}
542
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000543static const struct nla_policy devconf_ipv6_policy[NETCONFA_MAX+1] = {
544 [NETCONFA_IFINDEX] = { .len = sizeof(int) },
545 [NETCONFA_FORWARDING] = { .len = sizeof(int) },
546};
547
548static int inet6_netconf_get_devconf(struct sk_buff *in_skb,
Thomas Graf661d2962013-03-21 07:45:29 +0000549 struct nlmsghdr *nlh)
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000550{
551 struct net *net = sock_net(in_skb->sk);
552 struct nlattr *tb[NETCONFA_MAX+1];
553 struct netconfmsg *ncm;
554 struct sk_buff *skb;
555 struct ipv6_devconf *devconf;
556 struct inet6_dev *in6_dev;
557 struct net_device *dev;
558 int ifindex;
559 int err;
560
561 err = nlmsg_parse(nlh, sizeof(*ncm), tb, NETCONFA_MAX,
562 devconf_ipv6_policy);
563 if (err < 0)
564 goto errout;
565
566 err = EINVAL;
567 if (!tb[NETCONFA_IFINDEX])
568 goto errout;
569
570 ifindex = nla_get_s32(tb[NETCONFA_IFINDEX]);
571 switch (ifindex) {
572 case NETCONFA_IFINDEX_ALL:
573 devconf = net->ipv6.devconf_all;
574 break;
575 case NETCONFA_IFINDEX_DEFAULT:
576 devconf = net->ipv6.devconf_dflt;
577 break;
578 default:
579 dev = __dev_get_by_index(net, ifindex);
580 if (dev == NULL)
581 goto errout;
582 in6_dev = __in6_dev_get(dev);
583 if (in6_dev == NULL)
584 goto errout;
585 devconf = &in6_dev->cnf;
586 break;
587 }
588
589 err = -ENOBUFS;
590 skb = nlmsg_new(inet6_netconf_msgsize_devconf(-1), GFP_ATOMIC);
591 if (skb == NULL)
592 goto errout;
593
594 err = inet6_netconf_fill_devconf(skb, ifindex, devconf,
595 NETLINK_CB(in_skb).portid,
596 nlh->nlmsg_seq, RTM_NEWNETCONF, 0,
597 -1);
598 if (err < 0) {
599 /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
600 WARN_ON(err == -EMSGSIZE);
601 kfree_skb(skb);
602 goto errout;
603 }
604 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
605errout:
606 return err;
607}
608
Nicolas Dichtel7a674202013-03-05 23:42:06 +0000609static int inet6_netconf_dump_devconf(struct sk_buff *skb,
610 struct netlink_callback *cb)
611{
612 struct net *net = sock_net(skb->sk);
613 int h, s_h;
614 int idx, s_idx;
615 struct net_device *dev;
616 struct inet6_dev *idev;
617 struct hlist_head *head;
618
619 s_h = cb->args[0];
620 s_idx = idx = cb->args[1];
621
622 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
623 idx = 0;
624 head = &net->dev_index_head[h];
625 rcu_read_lock();
Nicolas Dichtel63998ac2013-03-22 06:28:43 +0000626 cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^
627 net->dev_base_seq;
Nicolas Dichtel7a674202013-03-05 23:42:06 +0000628 hlist_for_each_entry_rcu(dev, head, index_hlist) {
629 if (idx < s_idx)
630 goto cont;
631 idev = __in6_dev_get(dev);
632 if (!idev)
633 goto cont;
634
635 if (inet6_netconf_fill_devconf(skb, dev->ifindex,
636 &idev->cnf,
637 NETLINK_CB(cb->skb).portid,
638 cb->nlh->nlmsg_seq,
639 RTM_NEWNETCONF,
640 NLM_F_MULTI,
641 -1) <= 0) {
642 rcu_read_unlock();
643 goto done;
644 }
Nicolas Dichtel63998ac2013-03-22 06:28:43 +0000645 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
Nicolas Dichtel7a674202013-03-05 23:42:06 +0000646cont:
647 idx++;
648 }
649 rcu_read_unlock();
650 }
651 if (h == NETDEV_HASHENTRIES) {
652 if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_ALL,
653 net->ipv6.devconf_all,
654 NETLINK_CB(cb->skb).portid,
655 cb->nlh->nlmsg_seq,
656 RTM_NEWNETCONF, NLM_F_MULTI,
657 -1) <= 0)
658 goto done;
659 else
660 h++;
661 }
662 if (h == NETDEV_HASHENTRIES + 1) {
663 if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_DEFAULT,
664 net->ipv6.devconf_dflt,
665 NETLINK_CB(cb->skb).portid,
666 cb->nlh->nlmsg_seq,
667 RTM_NEWNETCONF, NLM_F_MULTI,
668 -1) <= 0)
669 goto done;
670 else
671 h++;
672 }
673done:
674 cb->args[0] = h;
675 cb->args[1] = idx;
676
677 return skb->len;
678}
679
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680#ifdef CONFIG_SYSCTL
681static void dev_forward_change(struct inet6_dev *idev)
682{
683 struct net_device *dev;
684 struct inet6_ifaddr *ifa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
686 if (!idev)
687 return;
688 dev = idev->dev;
Ben Hutchings0187bdf2008-06-19 16:15:47 -0700689 if (idev->cnf.forwarding)
690 dev_disable_lro(dev);
Amerigo Wang1a940832012-10-28 17:43:53 +0000691 if (dev->flags & IFF_MULTICAST) {
Hannes Frederic Sowa2c5e8932013-02-10 03:50:18 +0000692 if (idev->cnf.forwarding) {
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +0900693 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
Hannes Frederic Sowa2c5e8932013-02-10 03:50:18 +0000694 ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allrouters);
695 ipv6_dev_mc_inc(dev, &in6addr_sitelocal_allrouters);
696 } else {
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +0900697 ipv6_dev_mc_dec(dev, &in6addr_linklocal_allrouters);
Hannes Frederic Sowa2c5e8932013-02-10 03:50:18 +0000698 ipv6_dev_mc_dec(dev, &in6addr_interfacelocal_allrouters);
699 ipv6_dev_mc_dec(dev, &in6addr_sitelocal_allrouters);
700 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 }
stephen hemminger502a2ff2010-03-17 20:31:13 +0000702
703 list_for_each_entry(ifa, &idev->addr_list, if_list) {
Michal Wrobel2c12a742007-02-26 15:36:10 -0800704 if (ifa->flags&IFA_F_TENTATIVE)
705 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 if (idev->cnf.forwarding)
707 addrconf_join_anycast(ifa);
708 else
709 addrconf_leave_anycast(ifa);
710 }
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000711 inet6_netconf_notify_devconf(dev_net(dev), NETCONFA_FORWARDING,
712 dev->ifindex, &idev->cnf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713}
714
715
Pavel Emelyanove1869322008-01-10 17:43:50 -0800716static void addrconf_forward_change(struct net *net, __s32 newf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717{
718 struct net_device *dev;
719 struct inet6_dev *idev;
720
Ben Hutchings4acd4942012-08-14 08:54:51 +0000721 for_each_netdev(net, dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 idev = __in6_dev_get(dev);
723 if (idev) {
Pavel Emelyanove1869322008-01-10 17:43:50 -0800724 int changed = (!idev->cnf.forwarding) ^ (!newf);
725 idev->cnf.forwarding = newf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 if (changed)
727 dev_forward_change(idev);
728 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730}
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800731
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000732static int addrconf_fixup_forwarding(struct ctl_table *table, int *p, int newf)
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800733{
Pavel Emelyanovbff16c22008-01-10 17:42:13 -0800734 struct net *net;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000735 int old;
736
737 if (!rtnl_trylock())
738 return restart_syscall();
Pavel Emelyanovbff16c22008-01-10 17:42:13 -0800739
740 net = (struct net *)table->extra2;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000741 old = *p;
742 *p = newf;
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800743
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000744 if (p == &net->ipv6.devconf_dflt->forwarding) {
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000745 if ((!newf) ^ (!old))
746 inet6_netconf_notify_devconf(net, NETCONFA_FORWARDING,
747 NETCONFA_IFINDEX_DEFAULT,
748 net->ipv6.devconf_dflt);
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000749 rtnl_unlock();
750 return 0;
Eric W. Biederman88af1822010-02-19 13:22:59 +0000751 }
Stephen Hemmingerb325fdd2009-02-26 06:55:31 +0000752
Pavel Emelyanove1869322008-01-10 17:43:50 -0800753 if (p == &net->ipv6.devconf_all->forwarding) {
Pavel Emelyanove1869322008-01-10 17:43:50 -0800754 net->ipv6.devconf_dflt->forwarding = newf;
755 addrconf_forward_change(net, newf);
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000756 if ((!newf) ^ (!old))
757 inet6_netconf_notify_devconf(net, NETCONFA_FORWARDING,
758 NETCONFA_IFINDEX_ALL,
759 net->ipv6.devconf_all);
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000760 } else if ((!newf) ^ (!old))
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800761 dev_forward_change((struct inet6_dev *)table->extra1);
Ben Hutchings0187bdf2008-06-19 16:15:47 -0700762 rtnl_unlock();
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800763
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000764 if (newf)
Daniel Lezcano7b4da532008-03-04 13:47:14 -0800765 rt6_purge_dflt_routers(net);
Stephen Hemmingerb325fdd2009-02-26 06:55:31 +0000766 return 1;
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800767}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768#endif
769
stephen hemminger5c578aed2010-03-17 20:31:11 +0000770/* Nobody refers to this ifaddr, destroy it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp)
772{
stephen hemmingerc2e21292010-03-17 20:31:10 +0000773 WARN_ON(!hlist_unhashed(&ifp->addr_lst));
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700774
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775#ifdef NET_REFCNT_DEBUG
Joe Perches91df42b2012-05-15 14:11:54 +0000776 pr_debug("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777#endif
778
779 in6_dev_put(ifp->idev);
780
781 if (del_timer(&ifp->timer))
Stephen Hemmingere21e8462010-03-20 16:09:01 -0700782 pr_notice("Timer is still running, when freeing ifa=%p\n", ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
Herbert Xue9d3e082010-05-18 15:36:06 -0700784 if (ifp->state != INET6_IFADDR_STATE_DEAD) {
Joe Perchesf3213832012-05-15 14:11:53 +0000785 pr_warn("Freeing alive inet6 address %p\n", ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 return;
787 }
Amerigo Wang94e187c2012-10-29 00:13:19 +0000788 ip6_rt_put(ifp->rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
Lai Jiangshane5785982011-03-15 18:00:14 +0800790 kfree_rcu(ifp, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791}
792
Brian Haleye55ffac2006-07-10 15:25:51 -0700793static void
794ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp)
795{
stephen hemminger502a2ff2010-03-17 20:31:13 +0000796 struct list_head *p;
YOSHIFUJI Hideaki8a6ce0c2006-07-11 13:05:30 -0700797 int ifp_scope = ipv6_addr_src_scope(&ifp->addr);
Brian Haleye55ffac2006-07-10 15:25:51 -0700798
799 /*
800 * Each device address list is sorted in order of scope -
801 * global before linklocal.
802 */
stephen hemminger502a2ff2010-03-17 20:31:13 +0000803 list_for_each(p, &idev->addr_list) {
804 struct inet6_ifaddr *ifa
805 = list_entry(p, struct inet6_ifaddr, if_list);
YOSHIFUJI Hideaki8a6ce0c2006-07-11 13:05:30 -0700806 if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr))
Brian Haleye55ffac2006-07-10 15:25:51 -0700807 break;
808 }
809
David S. Millerb54c9b92010-03-25 21:25:30 -0700810 list_add_tail(&ifp->if_list, p);
Brian Haleye55ffac2006-07-10 15:25:51 -0700811}
812
Eric Dumazetddbe5032012-07-18 08:11:12 +0000813static u32 inet6_addr_hash(const struct in6_addr *addr)
YOSHIFUJI Hideaki3eb84f42008-04-10 15:42:08 +0900814{
Eric Dumazetddbe5032012-07-18 08:11:12 +0000815 return hash_32(ipv6_addr_hash(addr), IN6_ADDR_HSIZE_SHIFT);
YOSHIFUJI Hideaki3eb84f42008-04-10 15:42:08 +0900816}
817
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818/* On success it returns ifp with increased reference count */
819
820static struct inet6_ifaddr *
821ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr, int pfxlen,
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -0700822 int scope, u32 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823{
824 struct inet6_ifaddr *ifa = NULL;
825 struct rt6_info *rt;
stephen hemminger3a88a812010-03-17 20:31:12 +0000826 unsigned int hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 int err = 0;
YOSHIFUJI Hideakid68b8272008-06-25 16:26:47 +0900828 int addr_type = ipv6_addr_type(addr);
829
830 if (addr_type == IPV6_ADDR_ANY ||
831 addr_type & IPV6_ADDR_MULTICAST ||
832 (!(idev->dev->flags & IFF_LOOPBACK) &&
833 addr_type & IPV6_ADDR_LOOPBACK))
834 return ERR_PTR(-EADDRNOTAVAIL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -0700836 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 if (idev->dead) {
838 err = -ENODEV; /*XXX*/
839 goto out2;
840 }
841
Brian Haley56d417b2009-06-01 03:07:33 -0700842 if (idev->cnf.disable_ipv6) {
Brian Haley9bdd8d42009-03-18 18:22:48 -0700843 err = -EACCES;
844 goto out2;
845 }
846
stephen hemminger5c578aed2010-03-17 20:31:11 +0000847 spin_lock(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
849 /* Ignore adding duplicate addresses on an interface */
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900850 if (ipv6_chk_same_addr(dev_net(idev->dev), addr, idev->dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 ADBG(("ipv6_add_addr: already assigned\n"));
852 err = -EEXIST;
853 goto out;
854 }
855
Ingo Oeser322f74a2006-03-20 23:01:47 -0800856 ifa = kzalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
858 if (ifa == NULL) {
859 ADBG(("ipv6_add_addr: malloc failed\n"));
860 err = -ENOBUFS;
861 goto out;
862 }
863
David S. Miller8f031512011-12-06 16:48:14 -0500864 rt = addrconf_dst_alloc(idev, addr, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 if (IS_ERR(rt)) {
866 err = PTR_ERR(rt);
867 goto out;
868 }
869
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000870 ifa->addr = *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
872 spin_lock_init(&ifa->lock);
Herbert Xue9d3e082010-05-18 15:36:06 -0700873 spin_lock_init(&ifa->state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 init_timer(&ifa->timer);
stephen hemmingerc2e21292010-03-17 20:31:10 +0000875 INIT_HLIST_NODE(&ifa->addr_lst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 ifa->timer.data = (unsigned long) ifa;
877 ifa->scope = scope;
878 ifa->prefix_len = pfxlen;
879 ifa->flags = flags | IFA_F_TENTATIVE;
880 ifa->cstamp = ifa->tstamp = jiffies;
881
Keir Fraser57f5f542006-08-29 02:43:49 -0700882 ifa->rt = rt;
883
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 ifa->idev = idev;
885 in6_dev_hold(idev);
886 /* For caller */
887 in6_ifa_hold(ifa);
888
889 /* Add to big hash table */
Eric Dumazetddbe5032012-07-18 08:11:12 +0000890 hash = inet6_addr_hash(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
stephen hemminger5c578aed2010-03-17 20:31:11 +0000892 hlist_add_head_rcu(&ifa->addr_lst, &inet6_addr_lst[hash]);
stephen hemminger5c578aed2010-03-17 20:31:11 +0000893 spin_unlock(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
895 write_lock(&idev->lock);
896 /* Add to inet6_dev unicast addr list. */
Brian Haleye55ffac2006-07-10 15:25:51 -0700897 ipv6_link_dev_addr(idev, ifa);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
899#ifdef CONFIG_IPV6_PRIVACY
900 if (ifa->flags&IFA_F_TEMPORARY) {
stephen hemminger372e6c82010-03-17 20:31:09 +0000901 list_add(&ifa->tmp_list, &idev->tempaddr_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 in6_ifa_hold(ifa);
903 }
904#endif
905
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 in6_ifa_hold(ifa);
907 write_unlock(&idev->lock);
908out2:
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -0700909 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
YOSHIFUJI Hideakifd928332005-04-19 22:27:09 -0700911 if (likely(err == 0))
Alan Sterne041c682006-03-27 01:16:30 -0800912 atomic_notifier_call_chain(&inet6addr_chain, NETDEV_UP, ifa);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 else {
914 kfree(ifa);
915 ifa = ERR_PTR(err);
916 }
917
918 return ifa;
919out:
stephen hemminger5c578aed2010-03-17 20:31:11 +0000920 spin_unlock(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 goto out2;
922}
923
924/* This function wants to get referenced ifp and releases it before return */
925
926static void ipv6_del_addr(struct inet6_ifaddr *ifp)
927{
stephen hemminger502a2ff2010-03-17 20:31:13 +0000928 struct inet6_ifaddr *ifa, *ifn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 struct inet6_dev *idev = ifp->idev;
Herbert Xu4c5ff6a2010-05-18 15:54:18 -0700930 int state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 int deleted = 0, onlink = 0;
932 unsigned long expires = jiffies;
933
Herbert Xu4c5ff6a2010-05-18 15:54:18 -0700934 spin_lock_bh(&ifp->state_lock);
935 state = ifp->state;
Herbert Xue9d3e082010-05-18 15:36:06 -0700936 ifp->state = INET6_IFADDR_STATE_DEAD;
Herbert Xu4c5ff6a2010-05-18 15:54:18 -0700937 spin_unlock_bh(&ifp->state_lock);
938
939 if (state == INET6_IFADDR_STATE_DEAD)
940 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
stephen hemminger5c578aed2010-03-17 20:31:11 +0000942 spin_lock_bh(&addrconf_hash_lock);
943 hlist_del_init_rcu(&ifp->addr_lst);
stephen hemminger5c578aed2010-03-17 20:31:11 +0000944 spin_unlock_bh(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
946 write_lock_bh(&idev->lock);
947#ifdef CONFIG_IPV6_PRIVACY
948 if (ifp->flags&IFA_F_TEMPORARY) {
stephen hemminger372e6c82010-03-17 20:31:09 +0000949 list_del(&ifp->tmp_list);
950 if (ifp->ifpub) {
951 in6_ifa_put(ifp->ifpub);
952 ifp->ifpub = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 }
stephen hemminger372e6c82010-03-17 20:31:09 +0000954 __in6_ifa_put(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 }
956#endif
957
stephen hemminger502a2ff2010-03-17 20:31:13 +0000958 list_for_each_entry_safe(ifa, ifn, &idev->addr_list, if_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 if (ifa == ifp) {
stephen hemminger502a2ff2010-03-17 20:31:13 +0000960 list_del_init(&ifp->if_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 __in6_ifa_put(ifp);
stephen hemminger502a2ff2010-03-17 20:31:13 +0000962
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 if (!(ifp->flags & IFA_F_PERMANENT) || onlink > 0)
964 break;
965 deleted = 1;
Kristian Slavov1d142802005-12-21 18:47:24 -0800966 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 } else if (ifp->flags & IFA_F_PERMANENT) {
968 if (ipv6_prefix_equal(&ifa->addr, &ifp->addr,
969 ifp->prefix_len)) {
970 if (ifa->flags & IFA_F_PERMANENT) {
971 onlink = 1;
972 if (deleted)
973 break;
974 } else {
975 unsigned long lifetime;
976
977 if (!onlink)
978 onlink = -1;
979
980 spin_lock(&ifa->lock);
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +0900981
982 lifetime = addrconf_timeout_fixup(ifa->valid_lft, HZ);
983 /*
984 * Note: Because this address is
985 * not permanent, lifetime <
986 * LONG_MAX / HZ here.
987 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 if (time_before(expires,
989 ifa->tstamp + lifetime * HZ))
990 expires = ifa->tstamp + lifetime * HZ;
991 spin_unlock(&ifa->lock);
992 }
993 }
994 }
995 }
996 write_unlock_bh(&idev->lock);
997
Andrey Vaginb2238562008-07-08 15:13:31 -0700998 addrconf_del_timer(ifp);
999
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 ipv6_ifa_notify(RTM_DELADDR, ifp);
1001
Alan Sterne041c682006-03-27 01:16:30 -08001002 atomic_notifier_call_chain(&inet6addr_chain, NETDEV_DOWN, ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 /*
1005 * Purge or update corresponding prefix
1006 *
1007 * 1) we don't purge prefix here if address was not permanent.
1008 * prefix is managed by its own lifetime.
1009 * 2) if there're no addresses, delete prefix.
1010 * 3) if there're still other permanent address(es),
1011 * corresponding prefix is still permanent.
1012 * 4) otherwise, update prefix lifetime to the
1013 * longest valid lifetime among the corresponding
1014 * addresses on the device.
1015 * Note: subsequent RA will update lifetime.
1016 *
1017 * --yoshfuji
1018 */
1019 if ((ifp->flags & IFA_F_PERMANENT) && onlink < 1) {
1020 struct in6_addr prefix;
1021 struct rt6_info *rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
Nicolas Dichtel64c6d082012-09-26 00:04:55 +00001023 ipv6_addr_prefix(&prefix, &ifp->addr, ifp->prefix_len);
Nicolas Dichtel64c6d082012-09-26 00:04:55 +00001024
Romain Kuntz21caa662013-01-09 21:06:03 +00001025 rt = addrconf_get_prefix_route(&prefix,
1026 ifp->prefix_len,
1027 ifp->idev->dev,
1028 0, RTF_GATEWAY | RTF_DEFAULT);
1029
1030 if (rt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 if (onlink == 0) {
Thomas Grafe0a1ad732006-08-22 00:00:21 -07001032 ip6_del_rt(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 rt = NULL;
1034 } else if (!(rt->rt6i_flags & RTF_EXPIRES)) {
Gao feng1716a962012-04-06 00:13:10 +00001035 rt6_set_expires(rt, expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 }
1037 }
Amerigo Wang94e187c2012-10-29 00:13:19 +00001038 ip6_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 }
1040
Daniel Walterc3968a82011-04-13 21:10:57 +00001041 /* clean up prefsrc entries */
1042 rt6_remove_prefsrc(ifp);
Herbert Xu4c5ff6a2010-05-18 15:54:18 -07001043out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 in6_ifa_put(ifp);
1045}
1046
1047#ifdef CONFIG_IPV6_PRIVACY
1048static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift)
1049{
1050 struct inet6_dev *idev = ifp->idev;
1051 struct in6_addr addr, *tmpaddr;
Lorenzo Colitti76f793e2011-07-26 13:50:49 +00001052 unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_tstamp, age;
Benoit Boissinoteac55bf2008-04-02 00:01:35 -07001053 unsigned long regen_advance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 int tmp_plen;
1055 int ret = 0;
1056 int max_addresses;
Neil Horman95c385b2007-04-25 17:08:10 -07001057 u32 addr_flags;
Lorenzo Colitti76f793e2011-07-26 13:50:49 +00001058 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
1060 write_lock(&idev->lock);
1061 if (ift) {
1062 spin_lock_bh(&ift->lock);
1063 memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8);
1064 spin_unlock_bh(&ift->lock);
1065 tmpaddr = &addr;
1066 } else {
1067 tmpaddr = NULL;
1068 }
1069retry:
1070 in6_dev_hold(idev);
1071 if (idev->cnf.use_tempaddr <= 0) {
1072 write_unlock(&idev->lock);
Joe Perchesf3213832012-05-15 14:11:53 +00001073 pr_info("%s: use_tempaddr is disabled\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 in6_dev_put(idev);
1075 ret = -1;
1076 goto out;
1077 }
1078 spin_lock_bh(&ifp->lock);
1079 if (ifp->regen_count++ >= idev->cnf.regen_max_retry) {
1080 idev->cnf.use_tempaddr = -1; /*XXX*/
1081 spin_unlock_bh(&ifp->lock);
1082 write_unlock(&idev->lock);
Joe Perchesf3213832012-05-15 14:11:53 +00001083 pr_warn("%s: regeneration time exceeded - disabled temporary address support\n",
1084 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 in6_dev_put(idev);
1086 ret = -1;
1087 goto out;
1088 }
1089 in6_ifa_hold(ifp);
1090 memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
Sorin Dumitrueb7e0572012-08-30 02:01:45 +00001091 __ipv6_try_regen_rndid(idev, tmpaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 memcpy(&addr.s6_addr[8], idev->rndid, 8);
Lorenzo Colitti76f793e2011-07-26 13:50:49 +00001093 age = (now - ifp->tstamp) / HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 tmp_valid_lft = min_t(__u32,
1095 ifp->valid_lft,
Glenn Wurster7a876b02010-09-27 07:10:10 +00001096 idev->cnf.temp_valid_lft + age);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001097 tmp_prefered_lft = min_t(__u32,
1098 ifp->prefered_lft,
Glenn Wurster7a876b02010-09-27 07:10:10 +00001099 idev->cnf.temp_prefered_lft + age -
Ben Hutchings784e2712010-06-26 11:42:55 +00001100 idev->cnf.max_desync_factor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 tmp_plen = ifp->prefix_len;
1102 max_addresses = idev->cnf.max_addresses;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 tmp_tstamp = ifp->tstamp;
1104 spin_unlock_bh(&ifp->lock);
1105
Benoit Boissinoteac55bf2008-04-02 00:01:35 -07001106 regen_advance = idev->cnf.regen_max_retry *
1107 idev->cnf.dad_transmits *
1108 idev->nd_parms->retrans_time / HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 write_unlock(&idev->lock);
Neil Horman95c385b2007-04-25 17:08:10 -07001110
Benoit Boissinoteac55bf2008-04-02 00:01:35 -07001111 /* A temporary address is created only if this calculated Preferred
1112 * Lifetime is greater than REGEN_ADVANCE time units. In particular,
1113 * an implementation must not create a temporary address with a zero
1114 * Preferred Lifetime.
1115 */
1116 if (tmp_prefered_lft <= regen_advance) {
1117 in6_ifa_put(ifp);
1118 in6_dev_put(idev);
1119 ret = -1;
1120 goto out;
1121 }
1122
Neil Horman95c385b2007-04-25 17:08:10 -07001123 addr_flags = IFA_F_TEMPORARY;
1124 /* set in addrconf_prefix_rcv() */
1125 if (ifp->flags & IFA_F_OPTIMISTIC)
1126 addr_flags |= IFA_F_OPTIMISTIC;
1127
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 ift = !max_addresses ||
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001129 ipv6_count_addresses(idev) < max_addresses ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 ipv6_add_addr(idev, &addr, tmp_plen,
Neil Horman95c385b2007-04-25 17:08:10 -07001131 ipv6_addr_type(&addr)&IPV6_ADDR_SCOPE_MASK,
1132 addr_flags) : NULL;
YOSHIFUJI Hideaki / 吉藤英明3f0d2ba2013-01-22 06:32:54 +00001133 if (IS_ERR_OR_NULL(ift)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 in6_ifa_put(ifp);
1135 in6_dev_put(idev);
Joe Perchesf3213832012-05-15 14:11:53 +00001136 pr_info("%s: retry temporary address regeneration\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 tmpaddr = &addr;
1138 write_lock(&idev->lock);
1139 goto retry;
1140 }
1141
1142 spin_lock_bh(&ift->lock);
1143 ift->ifpub = ifp;
1144 ift->valid_lft = tmp_valid_lft;
1145 ift->prefered_lft = tmp_prefered_lft;
Lorenzo Colitti76f793e2011-07-26 13:50:49 +00001146 ift->cstamp = now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 ift->tstamp = tmp_tstamp;
1148 spin_unlock_bh(&ift->lock);
1149
David S. Millercf22f9a2012-04-14 21:37:40 -04001150 addrconf_dad_start(ift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 in6_ifa_put(ift);
1152 in6_dev_put(idev);
1153out:
1154 return ret;
1155}
1156#endif
1157
1158/*
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001159 * Choose an appropriate source address (RFC3484)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 */
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001161enum {
1162 IPV6_SADDR_RULE_INIT = 0,
1163 IPV6_SADDR_RULE_LOCAL,
1164 IPV6_SADDR_RULE_SCOPE,
1165 IPV6_SADDR_RULE_PREFERRED,
1166#ifdef CONFIG_IPV6_MIP6
1167 IPV6_SADDR_RULE_HOA,
1168#endif
1169 IPV6_SADDR_RULE_OIF,
1170 IPV6_SADDR_RULE_LABEL,
1171#ifdef CONFIG_IPV6_PRIVACY
1172 IPV6_SADDR_RULE_PRIVACY,
1173#endif
1174 IPV6_SADDR_RULE_ORCHID,
1175 IPV6_SADDR_RULE_PREFIX,
1176 IPV6_SADDR_RULE_MAX
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001177};
1178
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001179struct ipv6_saddr_score {
1180 int rule;
1181 int addr_type;
1182 struct inet6_ifaddr *ifa;
1183 DECLARE_BITMAP(scorebits, IPV6_SADDR_RULE_MAX);
1184 int scopedist;
1185 int matchlen;
1186};
1187
1188struct ipv6_saddr_dst {
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +09001189 const struct in6_addr *addr;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001190 int ifindex;
1191 int scope;
1192 int label;
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001193 unsigned int prefs;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001194};
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001195
Dave Jonesb6f99a22007-03-22 12:27:49 -07001196static inline int ipv6_saddr_preferred(int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197{
Ulrich Weber45bb0062010-02-25 23:28:58 +00001198 if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|IPV6_ADDR_LOOPBACK))
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001199 return 1;
1200 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201}
1202
Benjamin Thery3de23252008-05-28 14:51:24 +02001203static int ipv6_get_saddr_eval(struct net *net,
1204 struct ipv6_saddr_score *score,
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001205 struct ipv6_saddr_dst *dst,
1206 int i)
1207{
1208 int ret;
1209
1210 if (i <= score->rule) {
1211 switch (i) {
1212 case IPV6_SADDR_RULE_SCOPE:
1213 ret = score->scopedist;
1214 break;
1215 case IPV6_SADDR_RULE_PREFIX:
1216 ret = score->matchlen;
1217 break;
1218 default:
1219 ret = !!test_bit(i, score->scorebits);
1220 }
1221 goto out;
1222 }
1223
1224 switch (i) {
1225 case IPV6_SADDR_RULE_INIT:
1226 /* Rule 0: remember if hiscore is not ready yet */
1227 ret = !!score->ifa;
1228 break;
1229 case IPV6_SADDR_RULE_LOCAL:
1230 /* Rule 1: Prefer same address */
1231 ret = ipv6_addr_equal(&score->ifa->addr, dst->addr);
1232 break;
1233 case IPV6_SADDR_RULE_SCOPE:
1234 /* Rule 2: Prefer appropriate scope
1235 *
1236 * ret
1237 * ^
1238 * -1 | d 15
1239 * ---+--+-+---> scope
1240 * |
1241 * | d is scope of the destination.
1242 * B-d | \
1243 * | \ <- smaller scope is better if
1244 * B-15 | \ if scope is enough for destinaion.
1245 * | ret = B - scope (-1 <= scope >= d <= 15).
1246 * d-C-1 | /
1247 * |/ <- greater is better
1248 * -C / if scope is not enough for destination.
1249 * /| ret = scope - C (-1 <= d < scope <= 15).
1250 *
1251 * d - C - 1 < B -15 (for all -1 <= d <= 15).
1252 * C > d + 14 - B >= 15 + 14 - B = 29 - B.
1253 * Assume B = 0 and we get C > 29.
1254 */
1255 ret = __ipv6_addr_src_scope(score->addr_type);
1256 if (ret >= dst->scope)
1257 ret = -ret;
1258 else
1259 ret -= 128; /* 30 is enough */
1260 score->scopedist = ret;
1261 break;
1262 case IPV6_SADDR_RULE_PREFERRED:
1263 /* Rule 3: Avoid deprecated and optimistic addresses */
1264 ret = ipv6_saddr_preferred(score->addr_type) ||
1265 !(score->ifa->flags & (IFA_F_DEPRECATED|IFA_F_OPTIMISTIC));
1266 break;
1267#ifdef CONFIG_IPV6_MIP6
1268 case IPV6_SADDR_RULE_HOA:
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001269 {
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001270 /* Rule 4: Prefer home address */
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001271 int prefhome = !(dst->prefs & IPV6_PREFER_SRC_COA);
1272 ret = !(score->ifa->flags & IFA_F_HOMEADDRESS) ^ prefhome;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001273 break;
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001274 }
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001275#endif
1276 case IPV6_SADDR_RULE_OIF:
1277 /* Rule 5: Prefer outgoing interface */
1278 ret = (!dst->ifindex ||
1279 dst->ifindex == score->ifa->idev->dev->ifindex);
1280 break;
1281 case IPV6_SADDR_RULE_LABEL:
1282 /* Rule 6: Prefer matching label */
Benjamin Thery3de23252008-05-28 14:51:24 +02001283 ret = ipv6_addr_label(net,
1284 &score->ifa->addr, score->addr_type,
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001285 score->ifa->idev->dev->ifindex) == dst->label;
1286 break;
1287#ifdef CONFIG_IPV6_PRIVACY
1288 case IPV6_SADDR_RULE_PRIVACY:
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001289 {
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001290 /* Rule 7: Prefer public address
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001291 * Note: prefer temporary address if use_tempaddr >= 2
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001292 */
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001293 int preftmp = dst->prefs & (IPV6_PREFER_SRC_PUBLIC|IPV6_PREFER_SRC_TMP) ?
1294 !!(dst->prefs & IPV6_PREFER_SRC_TMP) :
1295 score->ifa->idev->cnf.use_tempaddr >= 2;
1296 ret = (!(score->ifa->flags & IFA_F_TEMPORARY)) ^ preftmp;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001297 break;
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001298 }
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001299#endif
1300 case IPV6_SADDR_RULE_ORCHID:
1301 /* Rule 8-: Prefer ORCHID vs ORCHID or
1302 * non-ORCHID vs non-ORCHID
1303 */
1304 ret = !(ipv6_addr_orchid(&score->ifa->addr) ^
1305 ipv6_addr_orchid(dst->addr));
1306 break;
1307 case IPV6_SADDR_RULE_PREFIX:
1308 /* Rule 8: Use longest matching prefix */
YOSHIFUJI Hideaki / 吉藤英明91b4b042012-09-10 18:41:07 +00001309 ret = ipv6_addr_diff(&score->ifa->addr, dst->addr);
1310 if (ret > score->ifa->prefix_len)
1311 ret = score->ifa->prefix_len;
1312 score->matchlen = ret;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001313 break;
1314 default:
1315 ret = 0;
1316 }
1317
1318 if (ret)
1319 __set_bit(i, score->scorebits);
1320 score->rule = i;
1321out:
1322 return ret;
1323}
1324
Patrick McHardyb3f644f2012-08-26 19:14:14 +02001325int ipv6_dev_get_saddr(struct net *net, const struct net_device *dst_dev,
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +09001326 const struct in6_addr *daddr, unsigned int prefs,
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001327 struct in6_addr *saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328{
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001329 struct ipv6_saddr_score scores[2],
1330 *score = &scores[0], *hiscore = &scores[1];
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001331 struct ipv6_saddr_dst dst;
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001332 struct net_device *dev;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001333 int dst_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001335 dst_type = __ipv6_addr_type(daddr);
1336 dst.addr = daddr;
1337 dst.ifindex = dst_dev ? dst_dev->ifindex : 0;
1338 dst.scope = __ipv6_addr_src_scope(dst_type);
Benjamin Thery3de23252008-05-28 14:51:24 +02001339 dst.label = ipv6_addr_label(net, daddr, dst_type, dst.ifindex);
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001340 dst.prefs = prefs;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001341
1342 hiscore->rule = -1;
1343 hiscore->ifa = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001345 rcu_read_lock();
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001346
Eric Dumazetc6d14c82009-11-04 05:43:23 -08001347 for_each_netdev_rcu(net, dev) {
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001348 struct inet6_dev *idev;
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001349
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001350 /* Candidate Source Address (section 4)
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001351 * - multicast and link-local destination address,
1352 * the set of candidate source address MUST only
1353 * include addresses assigned to interfaces
1354 * belonging to the same link as the outgoing
1355 * interface.
1356 * (- For site-local destination addresses, the
1357 * set of candidate source addresses MUST only
1358 * include addresses assigned to interfaces
1359 * belonging to the same site as the outgoing
1360 * interface.)
1361 */
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001362 if (((dst_type & IPV6_ADDR_MULTICAST) ||
1363 dst.scope <= IPV6_ADDR_SCOPE_LINKLOCAL) &&
1364 dst.ifindex && dev->ifindex != dst.ifindex)
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001365 continue;
1366
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 idev = __in6_dev_get(dev);
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001368 if (!idev)
1369 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001371 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00001372 list_for_each_entry(score->ifa, &idev->addr_list, if_list) {
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001373 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001375 /*
YOSHIFUJI Hideaki6b3ae802005-12-21 22:58:01 +09001376 * - Tentative Address (RFC2462 section 5.4)
1377 * - A tentative address is not considered
1378 * "assigned to an interface" in the traditional
Neil Horman95c385b2007-04-25 17:08:10 -07001379 * sense, unless it is also flagged as optimistic.
YOSHIFUJI Hideaki6b3ae802005-12-21 22:58:01 +09001380 * - Candidate Source Address (section 4)
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001381 * - In any case, anycast addresses, multicast
1382 * addresses, and the unspecified address MUST
1383 * NOT be included in a candidate set.
1384 */
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001385 if ((score->ifa->flags & IFA_F_TENTATIVE) &&
1386 (!(score->ifa->flags & IFA_F_OPTIMISTIC)))
YOSHIFUJI Hideaki6b3ae802005-12-21 22:58:01 +09001387 continue;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001388
1389 score->addr_type = __ipv6_addr_type(&score->ifa->addr);
1390
1391 if (unlikely(score->addr_type == IPV6_ADDR_ANY ||
1392 score->addr_type & IPV6_ADDR_MULTICAST)) {
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001393 LIMIT_NETDEBUG(KERN_DEBUG
Joe Perches3b6d821c2007-11-19 23:47:25 -08001394 "ADDRCONF: unspecified / multicast address "
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001395 "assigned as unicast address on %s",
1396 dev->name);
1397 continue;
1398 }
1399
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001400 score->rule = -1;
1401 bitmap_zero(score->scorebits, IPV6_SADDR_RULE_MAX);
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001402
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001403 for (i = 0; i < IPV6_SADDR_RULE_MAX; i++) {
1404 int minihiscore, miniscore;
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001405
Benjamin Thery3de23252008-05-28 14:51:24 +02001406 minihiscore = ipv6_get_saddr_eval(net, hiscore, &dst, i);
1407 miniscore = ipv6_get_saddr_eval(net, score, &dst, i);
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001408
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001409 if (minihiscore > miniscore) {
1410 if (i == IPV6_SADDR_RULE_SCOPE &&
1411 score->scopedist > 0) {
1412 /*
1413 * special case:
1414 * each remaining entry
1415 * has too small (not enough)
1416 * scope, because ifa entries
1417 * are sorted by their scope
1418 * values.
1419 */
1420 goto try_nextdev;
1421 }
1422 break;
1423 } else if (minihiscore < miniscore) {
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001424 if (hiscore->ifa)
1425 in6_ifa_put(hiscore->ifa);
1426
1427 in6_ifa_hold(score->ifa);
1428
Ilpo Järvinena0bffff2009-03-21 13:36:17 -07001429 swap(hiscore, score);
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001430
1431 /* restore our iterator */
1432 score->ifa = hiscore->ifa;
1433
1434 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 }
1436 }
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001437 }
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001438try_nextdev:
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001439 read_unlock_bh(&idev->lock);
1440 }
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001441 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001443 if (!hiscore->ifa)
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001444 return -EADDRNOTAVAIL;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001445
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001446 *saddr = hiscore->ifa->addr;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001447 in6_ifa_put(hiscore->ifa);
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001448 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449}
YOSHIFUJI Hideaki5e5f3f02008-03-03 21:44:34 +09001450EXPORT_SYMBOL(ipv6_dev_get_saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
Neil Horman95c385b2007-04-25 17:08:10 -07001452int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr,
1453 unsigned char banned_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454{
1455 struct inet6_dev *idev;
1456 int err = -EADDRNOTAVAIL;
1457
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001458 rcu_read_lock();
Stephen Hemmingere21e8462010-03-20 16:09:01 -07001459 idev = __in6_dev_get(dev);
1460 if (idev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 struct inet6_ifaddr *ifp;
1462
1463 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00001464 list_for_each_entry(ifp, &idev->addr_list, if_list) {
1465 if (ifp->scope == IFA_LINK &&
1466 !(ifp->flags & banned_flags)) {
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001467 *addr = ifp->addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 err = 0;
1469 break;
1470 }
1471 }
1472 read_unlock_bh(&idev->lock);
1473 }
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001474 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 return err;
1476}
1477
1478static int ipv6_count_addresses(struct inet6_dev *idev)
1479{
1480 int cnt = 0;
1481 struct inet6_ifaddr *ifp;
1482
1483 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00001484 list_for_each_entry(ifp, &idev->addr_list, if_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 cnt++;
1486 read_unlock_bh(&idev->lock);
1487 return cnt;
1488}
1489
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001490int ipv6_chk_addr(struct net *net, const struct in6_addr *addr,
Daniel Lezcanobfeade02008-01-10 22:43:18 -08001491 struct net_device *dev, int strict)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492{
Stephen Hemmingereedf0422010-05-17 22:27:12 -07001493 struct inet6_ifaddr *ifp;
Eric Dumazetddbe5032012-07-18 08:11:12 +00001494 unsigned int hash = inet6_addr_hash(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495
stephen hemminger5c578aed2010-03-17 20:31:11 +00001496 rcu_read_lock_bh();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001497 hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09001498 if (!net_eq(dev_net(ifp->idev->dev), net))
Daniel Lezcanobfeade02008-01-10 22:43:18 -08001499 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 if (ipv6_addr_equal(&ifp->addr, addr) &&
Stephen Hemmingereedf0422010-05-17 22:27:12 -07001501 !(ifp->flags&IFA_F_TENTATIVE) &&
1502 (dev == NULL || ifp->idev->dev == dev ||
1503 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))) {
1504 rcu_read_unlock_bh();
1505 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 }
1507 }
stephen hemminger5c578aed2010-03-17 20:31:11 +00001508
Stephen Hemmingereedf0422010-05-17 22:27:12 -07001509 rcu_read_unlock_bh();
1510 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511}
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +09001512EXPORT_SYMBOL(ipv6_chk_addr);
1513
David S. Miller3e81c6d2010-03-20 16:18:00 -07001514static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
1515 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516{
Eric Dumazetddbe5032012-07-18 08:11:12 +00001517 unsigned int hash = inet6_addr_hash(addr);
stephen hemmingerc2e21292010-03-17 20:31:10 +00001518 struct inet6_ifaddr *ifp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
Sasha Levinb67bfe02013-02-27 17:06:00 -08001520 hlist_for_each_entry(ifp, &inet6_addr_lst[hash], addr_lst) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09001521 if (!net_eq(dev_net(ifp->idev->dev), net))
Daniel Lezcano06bfe652008-01-10 22:43:42 -08001522 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 if (ipv6_addr_equal(&ifp->addr, addr)) {
1524 if (dev == NULL || ifp->idev->dev == dev)
David S. Miller3e81c6d2010-03-20 16:18:00 -07001525 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 }
1527 }
David S. Miller3e81c6d2010-03-20 16:18:00 -07001528 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529}
1530
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001531int ipv6_chk_prefix(const struct in6_addr *addr, struct net_device *dev)
YOSHIFUJI Hideaki52eeeb82008-03-15 22:54:23 -04001532{
1533 struct inet6_dev *idev;
1534 struct inet6_ifaddr *ifa;
1535 int onlink;
1536
1537 onlink = 0;
1538 rcu_read_lock();
1539 idev = __in6_dev_get(dev);
1540 if (idev) {
1541 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00001542 list_for_each_entry(ifa, &idev->addr_list, if_list) {
YOSHIFUJI Hideaki52eeeb82008-03-15 22:54:23 -04001543 onlink = ipv6_prefix_equal(addr, &ifa->addr,
1544 ifa->prefix_len);
1545 if (onlink)
1546 break;
1547 }
1548 read_unlock_bh(&idev->lock);
1549 }
1550 rcu_read_unlock();
1551 return onlink;
1552}
YOSHIFUJI Hideaki52eeeb82008-03-15 22:54:23 -04001553EXPORT_SYMBOL(ipv6_chk_prefix);
1554
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +09001555struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, const struct in6_addr *addr,
Daniel Lezcano1cab3da2008-01-10 22:44:09 -08001556 struct net_device *dev, int strict)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557{
David S. Millerb79d1d52010-03-25 21:39:21 -07001558 struct inet6_ifaddr *ifp, *result = NULL;
Eric Dumazetddbe5032012-07-18 08:11:12 +00001559 unsigned int hash = inet6_addr_hash(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560
stephen hemminger5c578aed2010-03-17 20:31:11 +00001561 rcu_read_lock_bh();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001562 hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09001563 if (!net_eq(dev_net(ifp->idev->dev), net))
Daniel Lezcano1cab3da2008-01-10 22:44:09 -08001564 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 if (ipv6_addr_equal(&ifp->addr, addr)) {
1566 if (dev == NULL || ifp->idev->dev == dev ||
1567 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
David S. Millerb79d1d52010-03-25 21:39:21 -07001568 result = ifp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 in6_ifa_hold(ifp);
1570 break;
1571 }
1572 }
1573 }
stephen hemminger5c578aed2010-03-17 20:31:11 +00001574 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575
David S. Millerb79d1d52010-03-25 21:39:21 -07001576 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577}
1578
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579/* Gets referenced address, destroys ifaddr */
1580
Brian Haleycc411d02009-09-09 14:41:32 +00001581static void addrconf_dad_stop(struct inet6_ifaddr *ifp, int dad_failed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 if (ifp->flags&IFA_F_PERMANENT) {
1584 spin_lock_bh(&ifp->lock);
1585 addrconf_del_timer(ifp);
1586 ifp->flags |= IFA_F_TENTATIVE;
Brian Haleycc411d02009-09-09 14:41:32 +00001587 if (dad_failed)
1588 ifp->flags |= IFA_F_DADFAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 spin_unlock_bh(&ifp->lock);
Herbert Xue2577a02010-03-13 12:23:29 -08001590 if (dad_failed)
1591 ipv6_ifa_notify(0, ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 in6_ifa_put(ifp);
1593#ifdef CONFIG_IPV6_PRIVACY
1594 } else if (ifp->flags&IFA_F_TEMPORARY) {
1595 struct inet6_ifaddr *ifpub;
1596 spin_lock_bh(&ifp->lock);
1597 ifpub = ifp->ifpub;
1598 if (ifpub) {
1599 in6_ifa_hold(ifpub);
1600 spin_unlock_bh(&ifp->lock);
1601 ipv6_create_tempaddr(ifpub, ifp);
1602 in6_ifa_put(ifpub);
1603 } else {
1604 spin_unlock_bh(&ifp->lock);
1605 }
1606 ipv6_del_addr(ifp);
1607#endif
1608 } else
1609 ipv6_del_addr(ifp);
1610}
1611
Herbert Xuf2344a12010-05-18 15:55:27 -07001612static int addrconf_dad_end(struct inet6_ifaddr *ifp)
1613{
1614 int err = -ENOENT;
1615
1616 spin_lock(&ifp->state_lock);
1617 if (ifp->state == INET6_IFADDR_STATE_DAD) {
1618 ifp->state = INET6_IFADDR_STATE_POSTDAD;
1619 err = 0;
1620 }
1621 spin_unlock(&ifp->state_lock);
1622
1623 return err;
1624}
1625
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09001626void addrconf_dad_failure(struct inet6_ifaddr *ifp)
1627{
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09001628 struct inet6_dev *idev = ifp->idev;
Brian Haley9bdd8d42009-03-18 18:22:48 -07001629
Ursula Braun853dc2e2010-10-24 23:06:43 +00001630 if (addrconf_dad_end(ifp)) {
1631 in6_ifa_put(ifp);
Herbert Xuf2344a12010-05-18 15:55:27 -07001632 return;
Ursula Braun853dc2e2010-10-24 23:06:43 +00001633 }
Herbert Xuf2344a12010-05-18 15:55:27 -07001634
Joe Perchese87cc472012-05-13 21:56:26 +00001635 net_info_ratelimited("%s: IPv6 duplicate address %pI6c detected!\n",
1636 ifp->idev->dev->name, &ifp->addr);
Brian Haley9bdd8d42009-03-18 18:22:48 -07001637
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09001638 if (idev->cnf.accept_dad > 1 && !idev->cnf.disable_ipv6) {
1639 struct in6_addr addr;
1640
1641 addr.s6_addr32[0] = htonl(0xfe800000);
1642 addr.s6_addr32[1] = 0;
1643
1644 if (!ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) &&
1645 ipv6_addr_equal(&ifp->addr, &addr)) {
1646 /* DAD failed for link-local based on MAC address */
1647 idev->cnf.disable_ipv6 = 1;
Brian Haley9bdd8d42009-03-18 18:22:48 -07001648
Joe Perchesf3213832012-05-15 14:11:53 +00001649 pr_info("%s: IPv6 being disabled!\n",
Brian Haley9bdd8d42009-03-18 18:22:48 -07001650 ifp->idev->dev->name);
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09001651 }
1652 }
1653
Brian Haleycc411d02009-09-09 14:41:32 +00001654 addrconf_dad_stop(ifp, 1);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09001655}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656
1657/* Join to solicited addr multicast group. */
1658
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001659void addrconf_join_solict(struct net_device *dev, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660{
1661 struct in6_addr maddr;
1662
1663 if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1664 return;
1665
1666 addrconf_addr_solict_mult(addr, &maddr);
1667 ipv6_dev_mc_inc(dev, &maddr);
1668}
1669
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001670void addrconf_leave_solict(struct inet6_dev *idev, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671{
1672 struct in6_addr maddr;
1673
1674 if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1675 return;
1676
1677 addrconf_addr_solict_mult(addr, &maddr);
1678 __ipv6_dev_mc_dec(idev, &maddr);
1679}
1680
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03001681static void addrconf_join_anycast(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682{
1683 struct in6_addr addr;
Bjørn Mork2bda8a02011-07-05 23:04:13 +00001684 if (ifp->prefix_len == 127) /* RFC 6164 */
1685 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1687 if (ipv6_addr_any(&addr))
1688 return;
1689 ipv6_dev_ac_inc(ifp->idev->dev, &addr);
1690}
1691
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03001692static void addrconf_leave_anycast(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693{
1694 struct in6_addr addr;
YOSHIFUJI Hideaki32019e62011-07-24 11:44:34 +00001695 if (ifp->prefix_len == 127) /* RFC 6164 */
1696 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1698 if (ipv6_addr_any(&addr))
1699 return;
1700 __ipv6_dev_ac_dec(ifp->idev, &addr);
1701}
1702
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08001703static int addrconf_ifid_eui48(u8 *eui, struct net_device *dev)
1704{
1705 if (dev->addr_len != ETH_ALEN)
1706 return -1;
1707 memcpy(eui, dev->dev_addr, 3);
1708 memcpy(eui + 5, dev->dev_addr + 3, 3);
1709
1710 /*
1711 * The zSeries OSA network cards can be shared among various
1712 * OS instances, but the OSA cards have only one MAC address.
1713 * This leads to duplicate address conflicts in conjunction
1714 * with IPv6 if more than one instance uses the same card.
1715 *
1716 * The driver for these cards can deliver a unique 16-bit
1717 * identifier for each instance sharing the same card. It is
1718 * placed instead of 0xFFFE in the interface identifier. The
1719 * "u" bit of the interface identifier is not inverted in this
1720 * case. Hence the resulting interface identifier has local
1721 * scope according to RFC2373.
1722 */
1723 if (dev->dev_id) {
1724 eui[3] = (dev->dev_id >> 8) & 0xFF;
1725 eui[4] = dev->dev_id & 0xFF;
1726 } else {
1727 eui[3] = 0xFF;
1728 eui[4] = 0xFE;
1729 eui[0] ^= 2;
1730 }
1731 return 0;
1732}
1733
alex.bluesman.smirnov@gmail.com06a4c1c2012-05-10 03:25:52 +00001734static int addrconf_ifid_eui64(u8 *eui, struct net_device *dev)
1735{
1736 if (dev->addr_len != IEEE802154_ADDR_LEN)
1737 return -1;
1738 memcpy(eui, dev->dev_addr, 8);
YOSHIFUJI Hideaki / 吉藤英明5e98a362013-01-28 10:44:29 +00001739 eui[0] ^= 2;
alex.bluesman.smirnov@gmail.com06a4c1c2012-05-10 03:25:52 +00001740 return 0;
1741}
1742
YOSHIFUJI Hideaki / 吉藤英明cb6bf352013-03-25 08:26:24 +00001743static int addrconf_ifid_ieee1394(u8 *eui, struct net_device *dev)
1744{
1745 union fwnet_hwaddr *ha;
1746
1747 if (dev->addr_len != FWNET_ALEN)
1748 return -1;
1749
1750 ha = (union fwnet_hwaddr *)dev->dev_addr;
1751
1752 memcpy(eui, &ha->uc.uniq_id, sizeof(ha->uc.uniq_id));
1753 eui[0] ^= 2;
1754 return 0;
1755}
1756
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08001757static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev)
1758{
1759 /* XXX: inherit EUI-64 from other interface -- yoshfuji */
1760 if (dev->addr_len != ARCNET_ALEN)
1761 return -1;
1762 memset(eui, 0, 7);
Eldad Zack8e5e8f32012-04-01 07:49:08 +00001763 eui[7] = *(u8 *)dev->dev_addr;
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08001764 return 0;
1765}
1766
1767static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev)
1768{
1769 if (dev->addr_len != INFINIBAND_ALEN)
1770 return -1;
1771 memcpy(eui, dev->dev_addr + 12, 8);
1772 eui[0] |= 2;
1773 return 0;
1774}
1775
stephen hemmingerc61393e2010-10-04 20:17:53 +00001776static int __ipv6_isatap_ifid(u8 *eui, __be32 addr)
YOSHIFUJI Hideakidfd982b2008-04-10 15:42:09 +09001777{
Sascha Hlusiak9af28512009-05-19 12:56:51 +00001778 if (addr == 0)
1779 return -1;
YOSHIFUJI Hideakidfd982b2008-04-10 15:42:09 +09001780 eui[0] = (ipv4_is_zeronet(addr) || ipv4_is_private_10(addr) ||
1781 ipv4_is_loopback(addr) || ipv4_is_linklocal_169(addr) ||
1782 ipv4_is_private_172(addr) || ipv4_is_test_192(addr) ||
1783 ipv4_is_anycast_6to4(addr) || ipv4_is_private_192(addr) ||
1784 ipv4_is_test_198(addr) || ipv4_is_multicast(addr) ||
1785 ipv4_is_lbcast(addr)) ? 0x00 : 0x02;
1786 eui[1] = 0;
1787 eui[2] = 0x5E;
1788 eui[3] = 0xFE;
1789 memcpy(eui + 4, &addr, 4);
1790 return 0;
1791}
YOSHIFUJI Hideakidfd982b2008-04-10 15:42:09 +09001792
1793static int addrconf_ifid_sit(u8 *eui, struct net_device *dev)
1794{
1795 if (dev->priv_flags & IFF_ISATAP)
1796 return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
1797 return -1;
1798}
1799
stephen hemmingeraee80b52011-06-08 10:44:30 +00001800static int addrconf_ifid_gre(u8 *eui, struct net_device *dev)
1801{
1802 return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
1803}
1804
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
1806{
1807 switch (dev->type) {
1808 case ARPHRD_ETHER:
1809 case ARPHRD_FDDI:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08001810 return addrconf_ifid_eui48(eui, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 case ARPHRD_ARCNET:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08001812 return addrconf_ifid_arcnet(eui, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 case ARPHRD_INFINIBAND:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08001814 return addrconf_ifid_infiniband(eui, dev);
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11001815 case ARPHRD_SIT:
YOSHIFUJI Hideakidfd982b2008-04-10 15:42:09 +09001816 return addrconf_ifid_sit(eui, dev);
stephen hemmingeraee80b52011-06-08 10:44:30 +00001817 case ARPHRD_IPGRE:
1818 return addrconf_ifid_gre(eui, dev);
alex.bluesman.smirnov@gmail.com06a4c1c2012-05-10 03:25:52 +00001819 case ARPHRD_IEEE802154:
1820 return addrconf_ifid_eui64(eui, dev);
YOSHIFUJI Hideaki / 吉藤英明cb6bf352013-03-25 08:26:24 +00001821 case ARPHRD_IEEE1394:
1822 return addrconf_ifid_ieee1394(eui, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 }
1824 return -1;
1825}
1826
1827static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
1828{
1829 int err = -1;
1830 struct inet6_ifaddr *ifp;
1831
1832 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00001833 list_for_each_entry(ifp, &idev->addr_list, if_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
1835 memcpy(eui, ifp->addr.s6_addr+8, 8);
1836 err = 0;
1837 break;
1838 }
1839 }
1840 read_unlock_bh(&idev->lock);
1841 return err;
1842}
1843
1844#ifdef CONFIG_IPV6_PRIVACY
1845/* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
Sorin Dumitrueb7e0572012-08-30 02:01:45 +00001846static void __ipv6_regen_rndid(struct inet6_dev *idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848regen:
YOSHIFUJI Hideaki955189e2006-03-20 16:54:09 -08001849 get_random_bytes(idev->rndid, sizeof(idev->rndid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 idev->rndid[0] &= ~0x02;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851
1852 /*
1853 * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
1854 * check if generated address is not inappropriate
1855 *
1856 * - Reserved subnet anycast (RFC 2526)
1857 * 11111101 11....11 1xxxxxxx
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11001858 * - ISATAP (RFC4214) 6.1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 * 00-00-5E-FE-xx-xx-xx-xx
1860 * - value 0
1861 * - XXX: already assigned to an address on the device
1862 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001863 if (idev->rndid[0] == 0xfd &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff &&
1865 (idev->rndid[7]&0x80))
1866 goto regen;
1867 if ((idev->rndid[0]|idev->rndid[1]) == 0) {
1868 if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe)
1869 goto regen;
1870 if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00)
1871 goto regen;
1872 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873}
1874
1875static void ipv6_regen_rndid(unsigned long data)
1876{
1877 struct inet6_dev *idev = (struct inet6_dev *) data;
1878 unsigned long expires;
1879
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001880 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 write_lock_bh(&idev->lock);
1882
1883 if (idev->dead)
1884 goto out;
1885
Sorin Dumitrueb7e0572012-08-30 02:01:45 +00001886 __ipv6_regen_rndid(idev);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001887
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 expires = jiffies +
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001889 idev->cnf.temp_prefered_lft * HZ -
Ben Hutchings784e2712010-06-26 11:42:55 +00001890 idev->cnf.regen_max_retry * idev->cnf.dad_transmits * idev->nd_parms->retrans_time -
1891 idev->cnf.max_desync_factor * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 if (time_before(expires, jiffies)) {
Joe Perchesf3213832012-05-15 14:11:53 +00001893 pr_warn("%s: too short regeneration interval; timer disabled for %s\n",
1894 __func__, idev->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 goto out;
1896 }
1897
1898 if (!mod_timer(&idev->regen_timer, expires))
1899 in6_dev_hold(idev);
1900
1901out:
1902 write_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001903 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 in6_dev_put(idev);
1905}
1906
Sorin Dumitrueb7e0572012-08-30 02:01:45 +00001907static void __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr)
Eldad Zack8e5e8f32012-04-01 07:49:08 +00001908{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0)
Sorin Dumitrueb7e0572012-08-30 02:01:45 +00001910 __ipv6_regen_rndid(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911}
1912#endif
1913
1914/*
1915 * Add prefix route.
1916 */
1917
1918static void
1919addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev,
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -07001920 unsigned long expires, u32 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921{
Thomas Graf86872cb2006-08-22 00:01:08 -07001922 struct fib6_config cfg = {
1923 .fc_table = RT6_TABLE_PREFIX,
1924 .fc_metric = IP6_RT_PRIO_ADDRCONF,
1925 .fc_ifindex = dev->ifindex,
1926 .fc_expires = expires,
1927 .fc_dst_len = plen,
1928 .fc_flags = RTF_UP | flags,
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001929 .fc_nlinfo.nl_net = dev_net(dev),
Stephen Hemmingerf410a1f2008-08-23 05:16:46 -07001930 .fc_protocol = RTPROT_KERNEL,
Thomas Graf86872cb2006-08-22 00:01:08 -07001931 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001933 cfg.fc_dst = *pfx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934
1935 /* Prevent useless cloning on PtP SIT.
1936 This thing is done here expecting that the whole
1937 class of non-broadcast devices need not cloning.
1938 */
Amerigo Wang07a93622012-10-29 16:23:10 +00001939#if IS_ENABLED(CONFIG_IPV6_SIT)
Thomas Graf86872cb2006-08-22 00:01:08 -07001940 if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT))
1941 cfg.fc_flags |= RTF_NONEXTHOP;
Joerg Roedel0be669b2006-10-10 14:49:53 -07001942#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943
Thomas Graf86872cb2006-08-22 00:01:08 -07001944 ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945}
1946
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00001947
1948static struct rt6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
1949 int plen,
1950 const struct net_device *dev,
1951 u32 flags, u32 noflags)
1952{
1953 struct fib6_node *fn;
1954 struct rt6_info *rt = NULL;
1955 struct fib6_table *table;
1956
1957 table = fib6_get_table(dev_net(dev), RT6_TABLE_PREFIX);
1958 if (table == NULL)
1959 return NULL;
1960
Li RongQing5744dd92012-09-11 21:59:01 +00001961 read_lock_bh(&table->tb6_lock);
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00001962 fn = fib6_locate(&table->tb6_root, pfx, plen, NULL, 0);
1963 if (!fn)
1964 goto out;
1965 for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
David S. Millerd1918542011-12-28 20:19:20 -05001966 if (rt->dst.dev->ifindex != dev->ifindex)
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00001967 continue;
1968 if ((rt->rt6i_flags & flags) != flags)
1969 continue;
Romain Kuntz85da53b2013-01-09 15:02:26 +01001970 if ((rt->rt6i_flags & noflags) != 0)
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00001971 continue;
1972 dst_hold(&rt->dst);
1973 break;
1974 }
1975out:
Li RongQing5744dd92012-09-11 21:59:01 +00001976 read_unlock_bh(&table->tb6_lock);
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00001977 return rt;
1978}
1979
1980
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981/* Create "default" multicast route to the interface */
1982
1983static void addrconf_add_mroute(struct net_device *dev)
1984{
Thomas Graf86872cb2006-08-22 00:01:08 -07001985 struct fib6_config cfg = {
1986 .fc_table = RT6_TABLE_LOCAL,
1987 .fc_metric = IP6_RT_PRIO_ADDRCONF,
1988 .fc_ifindex = dev->ifindex,
1989 .fc_dst_len = 8,
1990 .fc_flags = RTF_UP,
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001991 .fc_nlinfo.nl_net = dev_net(dev),
Thomas Graf86872cb2006-08-22 00:01:08 -07001992 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993
Thomas Graf86872cb2006-08-22 00:01:08 -07001994 ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0);
1995
1996 ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997}
1998
Amerigo Wang07a93622012-10-29 16:23:10 +00001999#if IS_ENABLED(CONFIG_IPV6_SIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000static void sit_route_add(struct net_device *dev)
2001{
Thomas Graf86872cb2006-08-22 00:01:08 -07002002 struct fib6_config cfg = {
2003 .fc_table = RT6_TABLE_MAIN,
2004 .fc_metric = IP6_RT_PRIO_ADDRCONF,
2005 .fc_ifindex = dev->ifindex,
2006 .fc_dst_len = 96,
2007 .fc_flags = RTF_UP | RTF_NONEXTHOP,
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002008 .fc_nlinfo.nl_net = dev_net(dev),
Thomas Graf86872cb2006-08-22 00:01:08 -07002009 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010
2011 /* prefix length - 96 bits "::d.d.d.d" */
Thomas Graf86872cb2006-08-22 00:01:08 -07002012 ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013}
Joerg Roedel0be669b2006-10-10 14:49:53 -07002014#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
2017{
2018 struct inet6_dev *idev;
2019
2020 ASSERT_RTNL();
2021
Stephen Hemmingere21e8462010-03-20 16:09:01 -07002022 idev = ipv6_find_idev(dev);
2023 if (!idev)
Brian Haley64e724f2010-07-20 10:34:30 +00002024 return ERR_PTR(-ENOBUFS);
2025
2026 if (idev->cnf.disable_ipv6)
2027 return ERR_PTR(-EACCES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028
2029 /* Add default multicast route */
Li Wei4af04ab2011-12-06 21:23:45 +00002030 if (!(dev->flags & IFF_LOOPBACK))
2031 addrconf_add_mroute(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 return idev;
2034}
2035
Neil Hormane6bff992012-01-04 10:49:15 +00002036void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len, bool sllao)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037{
2038 struct prefix_info *pinfo;
2039 __u32 valid_lft;
2040 __u32 prefered_lft;
2041 int addr_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 struct inet6_dev *in6_dev;
Brian Haley56d417b2009-06-01 03:07:33 -07002043 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044
2045 pinfo = (struct prefix_info *) opt;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002046
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 if (len < sizeof(struct prefix_info)) {
2048 ADBG(("addrconf: prefix option too short\n"));
2049 return;
2050 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002051
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 /*
2053 * Validation checks ([ADDRCONF], page 19)
2054 */
2055
2056 addr_type = ipv6_addr_type(&pinfo->prefix);
2057
2058 if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
2059 return;
2060
2061 valid_lft = ntohl(pinfo->valid);
2062 prefered_lft = ntohl(pinfo->prefered);
2063
2064 if (prefered_lft > valid_lft) {
Joe Perchese87cc472012-05-13 21:56:26 +00002065 net_warn_ratelimited("addrconf: prefix option has invalid lifetime\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 return;
2067 }
2068
2069 in6_dev = in6_dev_get(dev);
2070
2071 if (in6_dev == NULL) {
Joe Perchese87cc472012-05-13 21:56:26 +00002072 net_dbg_ratelimited("addrconf: device %s not configured\n",
2073 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 return;
2075 }
2076
2077 /*
2078 * Two things going on here:
2079 * 1) Add routes for on-link prefixes
2080 * 2) Configure prefixes with the auto flag set
2081 */
2082
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002083 if (pinfo->onlink) {
2084 struct rt6_info *rt;
2085 unsigned long rt_expires;
2086
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002087 /* Avoid arithmetic overflow. Really, we could
2088 * save rt_expires in seconds, likely valid_lft,
2089 * but it would require division in fib gc, that it
2090 * not good.
2091 */
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002092 if (HZ > USER_HZ)
2093 rt_expires = addrconf_timeout_fixup(valid_lft, HZ);
2094 else
2095 rt_expires = addrconf_timeout_fixup(valid_lft, USER_HZ);
YOSHIFUJI Hideaki3dd4bc62005-12-19 14:02:45 -08002096
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002097 if (addrconf_finite_timeout(rt_expires))
2098 rt_expires *= HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002100 rt = addrconf_get_prefix_route(&pinfo->prefix,
2101 pinfo->prefix_len,
2102 dev,
2103 RTF_ADDRCONF | RTF_PREFIX_RT,
2104 RTF_GATEWAY | RTF_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002106 if (rt) {
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002107 /* Autoconf prefix route */
2108 if (valid_lft == 0) {
2109 ip6_del_rt(rt);
2110 rt = NULL;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002111 } else if (addrconf_finite_timeout(rt_expires)) {
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002112 /* not infinity */
Gao feng1716a962012-04-06 00:13:10 +00002113 rt6_set_expires(rt, jiffies + rt_expires);
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002114 } else {
Gao feng1716a962012-04-06 00:13:10 +00002115 rt6_clean_expires(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 }
2117 } else if (valid_lft) {
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002118 clock_t expires = 0;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002119 int flags = RTF_ADDRCONF | RTF_PREFIX_RT;
2120 if (addrconf_finite_timeout(rt_expires)) {
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002121 /* not infinity */
2122 flags |= RTF_EXPIRES;
2123 expires = jiffies_to_clock_t(rt_expires);
2124 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002126 dev, expires, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 }
Amerigo Wang94e187c2012-10-29 00:13:19 +00002128 ip6_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 }
2130
2131 /* Try to figure out our local address for this prefix */
2132
2133 if (pinfo->autoconf && in6_dev->cnf.autoconf) {
Eldad Zack8e5e8f32012-04-01 07:49:08 +00002134 struct inet6_ifaddr *ifp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 struct in6_addr addr;
2136 int create = 0, update_lft = 0;
2137
2138 if (pinfo->prefix_len == 64) {
2139 memcpy(&addr, &pinfo->prefix, 8);
Daniel Borkmannf53adae2013-04-08 04:01:30 +00002140
2141 if (!ipv6_addr_any(&in6_dev->token)) {
2142 read_lock_bh(&in6_dev->lock);
2143 memcpy(addr.s6_addr + 8,
2144 in6_dev->token.s6_addr + 8, 8);
2145 read_unlock_bh(&in6_dev->lock);
2146 } else if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
2147 ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 in6_dev_put(in6_dev);
2149 return;
2150 }
2151 goto ok;
2152 }
Joe Perchese87cc472012-05-13 21:56:26 +00002153 net_dbg_ratelimited("IPv6 addrconf: prefix with wrong length %d\n",
2154 pinfo->prefix_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 in6_dev_put(in6_dev);
2156 return;
2157
2158ok:
2159
YOSHIFUJI Hideaki53b79972008-07-19 22:35:03 -07002160 ifp = ipv6_get_ifaddr(net, &addr, dev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161
2162 if (ifp == NULL && valid_lft) {
2163 int max_addresses = in6_dev->cnf.max_addresses;
Neil Horman95c385b2007-04-25 17:08:10 -07002164 u32 addr_flags = 0;
2165
2166#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
2167 if (in6_dev->cnf.optimistic_dad &&
Neil Hormane6bff992012-01-04 10:49:15 +00002168 !net->ipv6.devconf_all->forwarding && sllao)
Neil Horman95c385b2007-04-25 17:08:10 -07002169 addr_flags = IFA_F_OPTIMISTIC;
2170#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171
2172 /* Do not allow to create too much of autoconfigured
2173 * addresses; this would be too easy way to crash kernel.
2174 */
2175 if (!max_addresses ||
2176 ipv6_count_addresses(in6_dev) < max_addresses)
2177 ifp = ipv6_add_addr(in6_dev, &addr, pinfo->prefix_len,
Neil Horman95c385b2007-04-25 17:08:10 -07002178 addr_type&IPV6_ADDR_SCOPE_MASK,
2179 addr_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180
YOSHIFUJI Hideaki / 吉藤英明3f0d2ba2013-01-22 06:32:54 +00002181 if (IS_ERR_OR_NULL(ifp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 in6_dev_put(in6_dev);
2183 return;
2184 }
2185
2186 update_lft = create = 1;
2187 ifp->cstamp = jiffies;
David S. Millercf22f9a2012-04-14 21:37:40 -04002188 addrconf_dad_start(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 }
2190
2191 if (ifp) {
2192 int flags;
2193 unsigned long now;
2194#ifdef CONFIG_IPV6_PRIVACY
2195 struct inet6_ifaddr *ift;
2196#endif
2197 u32 stored_lft;
2198
2199 /* update lifetime (RFC2462 5.5.3 e) */
2200 spin_lock(&ifp->lock);
2201 now = jiffies;
2202 if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
2203 stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
2204 else
2205 stored_lft = 0;
2206 if (!update_lft && stored_lft) {
2207 if (valid_lft > MIN_VALID_LIFETIME ||
2208 valid_lft > stored_lft)
2209 update_lft = 1;
2210 else if (stored_lft <= MIN_VALID_LIFETIME) {
2211 /* valid_lft <= stored_lft is always true */
Brian Haleya1ed0522009-07-02 07:10:52 +00002212 /*
2213 * RFC 4862 Section 5.5.3e:
2214 * "Note that the preferred lifetime of
2215 * the corresponding address is always
2216 * reset to the Preferred Lifetime in
2217 * the received Prefix Information
2218 * option, regardless of whether the
2219 * valid lifetime is also reset or
2220 * ignored."
2221 *
2222 * So if the preferred lifetime in
2223 * this advertisement is different
2224 * than what we have stored, but the
2225 * valid lifetime is invalid, just
2226 * reset prefered_lft.
2227 *
2228 * We must set the valid lifetime
2229 * to the stored lifetime since we'll
2230 * be updating the timestamp below,
2231 * else we'll set it back to the
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002232 * minimum.
Brian Haleya1ed0522009-07-02 07:10:52 +00002233 */
2234 if (prefered_lft != ifp->prefered_lft) {
2235 valid_lft = stored_lft;
2236 update_lft = 1;
2237 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 } else {
2239 valid_lft = MIN_VALID_LIFETIME;
2240 if (valid_lft < prefered_lft)
2241 prefered_lft = valid_lft;
2242 update_lft = 1;
2243 }
2244 }
2245
2246 if (update_lft) {
2247 ifp->valid_lft = valid_lft;
2248 ifp->prefered_lft = prefered_lft;
2249 ifp->tstamp = now;
2250 flags = ifp->flags;
2251 ifp->flags &= ~IFA_F_DEPRECATED;
2252 spin_unlock(&ifp->lock);
2253
2254 if (!(flags&IFA_F_TENTATIVE))
2255 ipv6_ifa_notify(0, ifp);
2256 } else
2257 spin_unlock(&ifp->lock);
2258
2259#ifdef CONFIG_IPV6_PRIVACY
2260 read_lock_bh(&in6_dev->lock);
2261 /* update all temporary addresses in the list */
Lorenzo Colitti76f793e2011-07-26 13:50:49 +00002262 list_for_each_entry(ift, &in6_dev->tempaddr_list,
2263 tmp_list) {
2264 int age, max_valid, max_prefered;
2265
Benoit Boissinotc6fbfac2008-04-02 00:00:58 -07002266 if (ifp != ift->ifpub)
2267 continue;
2268
Lorenzo Colitti76f793e2011-07-26 13:50:49 +00002269 /*
2270 * RFC 4941 section 3.3:
2271 * If a received option will extend the lifetime
2272 * of a public address, the lifetimes of
2273 * temporary addresses should be extended,
2274 * subject to the overall constraint that no
2275 * temporary addresses should ever remain
2276 * "valid" or "preferred" for a time longer than
2277 * (TEMP_VALID_LIFETIME) or
2278 * (TEMP_PREFERRED_LIFETIME - DESYNC_FACTOR),
2279 * respectively.
2280 */
2281 age = (now - ift->cstamp) / HZ;
2282 max_valid = in6_dev->cnf.temp_valid_lft - age;
2283 if (max_valid < 0)
2284 max_valid = 0;
2285
2286 max_prefered = in6_dev->cnf.temp_prefered_lft -
2287 in6_dev->cnf.max_desync_factor -
2288 age;
2289 if (max_prefered < 0)
2290 max_prefered = 0;
2291
2292 if (valid_lft > max_valid)
2293 valid_lft = max_valid;
2294
2295 if (prefered_lft > max_prefered)
2296 prefered_lft = max_prefered;
2297
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 spin_lock(&ift->lock);
2299 flags = ift->flags;
Lorenzo Colitti76f793e2011-07-26 13:50:49 +00002300 ift->valid_lft = valid_lft;
2301 ift->prefered_lft = prefered_lft;
2302 ift->tstamp = now;
2303 if (prefered_lft > 0)
2304 ift->flags &= ~IFA_F_DEPRECATED;
2305
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 spin_unlock(&ift->lock);
2307 if (!(flags&IFA_F_TENTATIVE))
2308 ipv6_ifa_notify(0, ift);
2309 }
2310
Glenn Wursteraed65502010-09-27 07:04:30 +00002311 if ((create || list_empty(&in6_dev->tempaddr_list)) && in6_dev->cnf.use_tempaddr > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 /*
Lorenzo Colitti76f793e2011-07-26 13:50:49 +00002313 * When a new public address is created as
2314 * described in [ADDRCONF], also create a new
2315 * temporary address. Also create a temporary
2316 * address if it's enabled but no temporary
2317 * address currently exists.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002319 read_unlock_bh(&in6_dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 ipv6_create_tempaddr(ifp, NULL);
2321 } else {
2322 read_unlock_bh(&in6_dev->lock);
2323 }
2324#endif
2325 in6_ifa_put(ifp);
2326 addrconf_verify(0);
2327 }
2328 }
2329 inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
2330 in6_dev_put(in6_dev);
2331}
2332
2333/*
2334 * Set destination address.
2335 * Special case for SIT interfaces where we create a new "virtual"
2336 * device.
2337 */
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002338int addrconf_set_dstaddr(struct net *net, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339{
2340 struct in6_ifreq ireq;
2341 struct net_device *dev;
2342 int err = -EINVAL;
2343
2344 rtnl_lock();
2345
2346 err = -EFAULT;
2347 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2348 goto err_exit;
2349
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002350 dev = __dev_get_by_index(net, ireq.ifr6_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351
2352 err = -ENODEV;
2353 if (dev == NULL)
2354 goto err_exit;
2355
Amerigo Wang07a93622012-10-29 16:23:10 +00002356#if IS_ENABLED(CONFIG_IPV6_SIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 if (dev->type == ARPHRD_SIT) {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -08002358 const struct net_device_ops *ops = dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 struct ifreq ifr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 struct ip_tunnel_parm p;
2361
2362 err = -EADDRNOTAVAIL;
2363 if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4))
2364 goto err_exit;
2365
2366 memset(&p, 0, sizeof(p));
2367 p.iph.daddr = ireq.ifr6_addr.s6_addr32[3];
2368 p.iph.saddr = 0;
2369 p.iph.version = 4;
2370 p.iph.ihl = 5;
2371 p.iph.protocol = IPPROTO_IPV6;
2372 p.iph.ttl = 64;
Stephen Hemmingerd20b3102008-01-21 00:48:43 -08002373 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -08002375 if (ops->ndo_do_ioctl) {
2376 mm_segment_t oldfs = get_fs();
2377
2378 set_fs(KERNEL_DS);
2379 err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
2380 set_fs(oldfs);
2381 } else
2382 err = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383
2384 if (err == 0) {
2385 err = -ENOBUFS;
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002386 dev = __dev_get_by_name(net, p.name);
2387 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 goto err_exit;
2389 err = dev_open(dev);
2390 }
2391 }
Joerg Roedel0be669b2006-10-10 14:49:53 -07002392#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393
2394err_exit:
2395 rtnl_unlock();
2396 return err;
2397}
2398
2399/*
2400 * Manual configuration of address on an interface
2401 */
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002402static int inet6_addr_add(struct net *net, int ifindex, const struct in6_addr *pfx,
Thomas Graf24ef0da2008-05-28 16:54:22 +02002403 unsigned int plen, __u8 ifa_flags, __u32 prefered_lft,
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002404 __u32 valid_lft)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405{
2406 struct inet6_ifaddr *ifp;
2407 struct inet6_dev *idev;
2408 struct net_device *dev;
2409 int scope;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002410 u32 flags;
2411 clock_t expires;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002412 unsigned long timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413
2414 ASSERT_RTNL();
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002415
Thomas Graf24ef0da2008-05-28 16:54:22 +02002416 if (plen > 128)
2417 return -EINVAL;
2418
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002419 /* check the lifetime */
2420 if (!valid_lft || prefered_lft > valid_lft)
2421 return -EINVAL;
2422
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002423 dev = __dev_get_by_index(net, ifindex);
2424 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 return -ENODEV;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002426
Brian Haley64e724f2010-07-20 10:34:30 +00002427 idev = addrconf_add_dev(dev);
2428 if (IS_ERR(idev))
2429 return PTR_ERR(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430
2431 scope = ipv6_addr_scope(pfx);
2432
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002433 timeout = addrconf_timeout_fixup(valid_lft, HZ);
2434 if (addrconf_finite_timeout(timeout)) {
2435 expires = jiffies_to_clock_t(timeout * HZ);
2436 valid_lft = timeout;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002437 flags = RTF_EXPIRES;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002438 } else {
2439 expires = 0;
2440 flags = 0;
2441 ifa_flags |= IFA_F_PERMANENT;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002442 }
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002443
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002444 timeout = addrconf_timeout_fixup(prefered_lft, HZ);
2445 if (addrconf_finite_timeout(timeout)) {
2446 if (timeout == 0)
2447 ifa_flags |= IFA_F_DEPRECATED;
2448 prefered_lft = timeout;
2449 }
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002450
2451 ifp = ipv6_add_addr(idev, pfx, plen, scope, ifa_flags);
2452
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 if (!IS_ERR(ifp)) {
Herbert Xu06aebfb2006-08-09 16:52:04 -07002454 spin_lock_bh(&ifp->lock);
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002455 ifp->valid_lft = valid_lft;
2456 ifp->prefered_lft = prefered_lft;
2457 ifp->tstamp = jiffies;
Herbert Xu06aebfb2006-08-09 16:52:04 -07002458 spin_unlock_bh(&ifp->lock);
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002459
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09002460 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev,
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002461 expires, flags);
Neil Horman95c385b2007-04-25 17:08:10 -07002462 /*
2463 * Note that section 3.1 of RFC 4429 indicates
2464 * that the Optimistic flag should not be set for
2465 * manually configured addresses
2466 */
David S. Millercf22f9a2012-04-14 21:37:40 -04002467 addrconf_dad_start(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 in6_ifa_put(ifp);
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002469 addrconf_verify(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 return 0;
2471 }
2472
2473 return PTR_ERR(ifp);
2474}
2475
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002476static int inet6_addr_del(struct net *net, int ifindex, const struct in6_addr *pfx,
Thomas Graf24ef0da2008-05-28 16:54:22 +02002477 unsigned int plen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478{
2479 struct inet6_ifaddr *ifp;
2480 struct inet6_dev *idev;
2481 struct net_device *dev;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002482
Thomas Graf24ef0da2008-05-28 16:54:22 +02002483 if (plen > 128)
2484 return -EINVAL;
2485
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002486 dev = __dev_get_by_index(net, ifindex);
2487 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 return -ENODEV;
2489
2490 if ((idev = __in6_dev_get(dev)) == NULL)
2491 return -ENXIO;
2492
2493 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00002494 list_for_each_entry(ifp, &idev->addr_list, if_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 if (ifp->prefix_len == plen &&
2496 ipv6_addr_equal(pfx, &ifp->addr)) {
2497 in6_ifa_hold(ifp);
2498 read_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002499
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 ipv6_del_addr(ifp);
2501
2502 /* If the last address is deleted administratively,
2503 disable IPv6 on this interface.
2504 */
stephen hemminger502a2ff2010-03-17 20:31:13 +00002505 if (list_empty(&idev->addr_list))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 addrconf_ifdown(idev->dev, 1);
2507 return 0;
2508 }
2509 }
2510 read_unlock_bh(&idev->lock);
2511 return -EADDRNOTAVAIL;
2512}
2513
2514
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002515int addrconf_add_ifaddr(struct net *net, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516{
2517 struct in6_ifreq ireq;
2518 int err;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002519
Eric W. Biedermanaf31f412012-11-16 03:03:06 +00002520 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 return -EPERM;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002522
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2524 return -EFAULT;
2525
2526 rtnl_lock();
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002527 err = inet6_addr_add(net, ireq.ifr6_ifindex, &ireq.ifr6_addr,
2528 ireq.ifr6_prefixlen, IFA_F_PERMANENT,
2529 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 rtnl_unlock();
2531 return err;
2532}
2533
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002534int addrconf_del_ifaddr(struct net *net, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535{
2536 struct in6_ifreq ireq;
2537 int err;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002538
Eric W. Biedermanaf31f412012-11-16 03:03:06 +00002539 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 return -EPERM;
2541
2542 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2543 return -EFAULT;
2544
2545 rtnl_lock();
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002546 err = inet6_addr_del(net, ireq.ifr6_ifindex, &ireq.ifr6_addr,
2547 ireq.ifr6_prefixlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 rtnl_unlock();
2549 return err;
2550}
2551
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08002552static void add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
2553 int plen, int scope)
2554{
2555 struct inet6_ifaddr *ifp;
2556
2557 ifp = ipv6_add_addr(idev, addr, plen, scope, IFA_F_PERMANENT);
2558 if (!IS_ERR(ifp)) {
2559 spin_lock_bh(&ifp->lock);
2560 ifp->flags &= ~IFA_F_TENTATIVE;
2561 spin_unlock_bh(&ifp->lock);
2562 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2563 in6_ifa_put(ifp);
2564 }
2565}
2566
Amerigo Wang07a93622012-10-29 16:23:10 +00002567#if IS_ENABLED(CONFIG_IPV6_SIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568static void sit_add_v4_addrs(struct inet6_dev *idev)
2569{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 struct in6_addr addr;
2571 struct net_device *dev;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002572 struct net *net = dev_net(idev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 int scope;
2574
2575 ASSERT_RTNL();
2576
2577 memset(&addr, 0, sizeof(struct in6_addr));
2578 memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
2579
2580 if (idev->dev->flags&IFF_POINTOPOINT) {
2581 addr.s6_addr32[0] = htonl(0xfe800000);
2582 scope = IFA_LINK;
2583 } else {
2584 scope = IPV6_ADDR_COMPATv4;
2585 }
2586
2587 if (addr.s6_addr32[3]) {
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08002588 add_addr(idev, &addr, 128, scope);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 return;
2590 }
2591
Benjamin Thery6fda7352008-03-05 10:47:47 -08002592 for_each_netdev(net, dev) {
Eldad Zack8e5e8f32012-04-01 07:49:08 +00002593 struct in_device *in_dev = __in_dev_get_rtnl(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 if (in_dev && (dev->flags & IFF_UP)) {
Eldad Zack8e5e8f32012-04-01 07:49:08 +00002595 struct in_ifaddr *ifa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596
2597 int flag = scope;
2598
2599 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
2600 int plen;
2601
2602 addr.s6_addr32[3] = ifa->ifa_local;
2603
2604 if (ifa->ifa_scope == RT_SCOPE_LINK)
2605 continue;
2606 if (ifa->ifa_scope >= RT_SCOPE_HOST) {
2607 if (idev->dev->flags&IFF_POINTOPOINT)
2608 continue;
2609 flag |= IFA_HOST;
2610 }
2611 if (idev->dev->flags&IFF_POINTOPOINT)
2612 plen = 64;
2613 else
2614 plen = 96;
2615
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08002616 add_addr(idev, &addr, plen, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 }
2618 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002619 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620}
Joerg Roedel0be669b2006-10-10 14:49:53 -07002621#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622
2623static void init_loopback(struct net_device *dev)
2624{
2625 struct inet6_dev *idev;
Balakumaran Kannan25fb6ca42013-04-02 16:15:05 +05302626 struct net_device *sp_dev;
2627 struct inet6_ifaddr *sp_ifa;
2628 struct rt6_info *sp_rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629
2630 /* ::1 */
2631
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__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 return;
2637 }
2638
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08002639 add_addr(idev, &in6addr_loopback, 128, IFA_HOST);
Balakumaran Kannan25fb6ca42013-04-02 16:15:05 +05302640
2641 /* Add routes to other interface's IPv6 addresses */
2642 for_each_netdev(dev_net(dev), sp_dev) {
2643 if (!strcmp(sp_dev->name, dev->name))
2644 continue;
2645
2646 idev = __in6_dev_get(sp_dev);
2647 if (!idev)
2648 continue;
2649
2650 read_lock_bh(&idev->lock);
2651 list_for_each_entry(sp_ifa, &idev->addr_list, if_list) {
2652
2653 if (sp_ifa->flags & (IFA_F_DADFAILED | IFA_F_TENTATIVE))
2654 continue;
2655
2656 sp_rt = addrconf_dst_alloc(idev, &sp_ifa->addr, 0);
2657
2658 /* Failure cases are ignored */
2659 if (!IS_ERR(sp_rt))
2660 ip6_ins_rt(sp_rt);
2661 }
2662 read_unlock_bh(&idev->lock);
2663 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664}
2665
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002666static void addrconf_add_linklocal(struct inet6_dev *idev, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667{
Eldad Zack8e5e8f32012-04-01 07:49:08 +00002668 struct inet6_ifaddr *ifp;
Neil Horman95c385b2007-04-25 17:08:10 -07002669 u32 addr_flags = IFA_F_PERMANENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670
Neil Horman95c385b2007-04-25 17:08:10 -07002671#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
2672 if (idev->cnf.optimistic_dad &&
David Miller702beb82008-07-20 18:17:02 -07002673 !dev_net(idev->dev)->ipv6.devconf_all->forwarding)
Neil Horman95c385b2007-04-25 17:08:10 -07002674 addr_flags |= IFA_F_OPTIMISTIC;
2675#endif
2676
2677
2678 ifp = ipv6_add_addr(idev, addr, 64, IFA_LINK, addr_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 if (!IS_ERR(ifp)) {
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09002680 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, idev->dev, 0, 0);
David S. Millercf22f9a2012-04-14 21:37:40 -04002681 addrconf_dad_start(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 in6_ifa_put(ifp);
2683 }
2684}
2685
2686static void addrconf_dev_config(struct net_device *dev)
2687{
2688 struct in6_addr addr;
Eldad Zack8e5e8f32012-04-01 07:49:08 +00002689 struct inet6_dev *idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690
2691 ASSERT_RTNL();
2692
Herbert Xu74235a22007-06-14 13:02:55 -07002693 if ((dev->type != ARPHRD_ETHER) &&
2694 (dev->type != ARPHRD_FDDI) &&
Herbert Xu74235a22007-06-14 13:02:55 -07002695 (dev->type != ARPHRD_ARCNET) &&
alex.bluesman.smirnov@gmail.com06a4c1c2012-05-10 03:25:52 +00002696 (dev->type != ARPHRD_INFINIBAND) &&
YOSHIFUJI Hideaki / 吉藤英明cb6bf352013-03-25 08:26:24 +00002697 (dev->type != ARPHRD_IEEE802154) &&
2698 (dev->type != ARPHRD_IEEE1394)) {
Herbert Xu74235a22007-06-14 13:02:55 -07002699 /* Alas, we support only Ethernet autoconfiguration. */
2700 return;
2701 }
2702
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 idev = addrconf_add_dev(dev);
Brian Haley64e724f2010-07-20 10:34:30 +00002704 if (IS_ERR(idev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 return;
2706
2707 memset(&addr, 0, sizeof(struct in6_addr));
2708 addr.s6_addr32[0] = htonl(0xFE800000);
2709
2710 if (ipv6_generate_eui64(addr.s6_addr + 8, dev) == 0)
2711 addrconf_add_linklocal(idev, &addr);
2712}
2713
Amerigo Wang07a93622012-10-29 16:23:10 +00002714#if IS_ENABLED(CONFIG_IPV6_SIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715static void addrconf_sit_config(struct net_device *dev)
2716{
2717 struct inet6_dev *idev;
2718
2719 ASSERT_RTNL();
2720
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002721 /*
2722 * Configure the tunnel with one of our IPv4
2723 * addresses... we should configure all of
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 * our v4 addrs in the tunnel
2725 */
2726
2727 if ((idev = ipv6_find_idev(dev)) == NULL) {
Joe Perches91df42b2012-05-15 14:11:54 +00002728 pr_debug("%s: add_dev failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 return;
2730 }
2731
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11002732 if (dev->priv_flags & IFF_ISATAP) {
2733 struct in6_addr addr;
2734
2735 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
2736 addrconf_prefix_route(&addr, 64, dev, 0, 0);
2737 if (!ipv6_generate_eui64(addr.s6_addr + 8, dev))
2738 addrconf_add_linklocal(idev, &addr);
2739 return;
2740 }
2741
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 sit_add_v4_addrs(idev);
2743
Nicolas Dichtel62b54dd2012-10-01 23:19:14 +00002744 if (dev->flags&IFF_POINTOPOINT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 addrconf_add_mroute(dev);
Nicolas Dichtel62b54dd2012-10-01 23:19:14 +00002746 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 sit_route_add(dev);
2748}
Joerg Roedel0be669b2006-10-10 14:49:53 -07002749#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750
Amerigo Wang07a93622012-10-29 16:23:10 +00002751#if IS_ENABLED(CONFIG_NET_IPGRE)
stephen hemmingeraee80b52011-06-08 10:44:30 +00002752static void addrconf_gre_config(struct net_device *dev)
2753{
2754 struct inet6_dev *idev;
2755 struct in6_addr addr;
2756
Joe Perchesf3213832012-05-15 14:11:53 +00002757 pr_info("%s(%s)\n", __func__, dev->name);
stephen hemmingeraee80b52011-06-08 10:44:30 +00002758
2759 ASSERT_RTNL();
2760
2761 if ((idev = ipv6_find_idev(dev)) == NULL) {
Joe Perches91df42b2012-05-15 14:11:54 +00002762 pr_debug("%s: add_dev failed\n", __func__);
stephen hemmingeraee80b52011-06-08 10:44:30 +00002763 return;
2764 }
2765
2766 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
2767 addrconf_prefix_route(&addr, 64, dev, 0, 0);
2768
2769 if (!ipv6_generate_eui64(addr.s6_addr + 8, dev))
2770 addrconf_add_linklocal(idev, &addr);
2771}
2772#endif
2773
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774static inline int
2775ipv6_inherit_linklocal(struct inet6_dev *idev, struct net_device *link_dev)
2776{
2777 struct in6_addr lladdr;
2778
Neil Horman95c385b2007-04-25 17:08:10 -07002779 if (!ipv6_get_lladdr(link_dev, &lladdr, IFA_F_TENTATIVE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 addrconf_add_linklocal(idev, &lladdr);
2781 return 0;
2782 }
2783 return -1;
2784}
2785
2786static void ip6_tnl_add_linklocal(struct inet6_dev *idev)
2787{
2788 struct net_device *link_dev;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002789 struct net *net = dev_net(idev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790
2791 /* first try to inherit the link-local address from the link device */
2792 if (idev->dev->iflink &&
Benjamin Thery6fda7352008-03-05 10:47:47 -08002793 (link_dev = __dev_get_by_index(net, idev->dev->iflink))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 if (!ipv6_inherit_linklocal(idev, link_dev))
2795 return;
2796 }
2797 /* then try to inherit it from any device */
Benjamin Thery6fda7352008-03-05 10:47:47 -08002798 for_each_netdev(net, link_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 if (!ipv6_inherit_linklocal(idev, link_dev))
2800 return;
2801 }
Joe Perches91df42b2012-05-15 14:11:54 +00002802 pr_debug("init ip6-ip6: add_linklocal failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803}
2804
2805/*
2806 * Autoconfigure tunnel with a link-local address so routing protocols,
2807 * DHCPv6, MLD etc. can be run over the virtual link
2808 */
2809
2810static void addrconf_ip6_tnl_config(struct net_device *dev)
2811{
2812 struct inet6_dev *idev;
2813
2814 ASSERT_RTNL();
2815
Stephen Hemmingere21e8462010-03-20 16:09:01 -07002816 idev = addrconf_add_dev(dev);
Brian Haley64e724f2010-07-20 10:34:30 +00002817 if (IS_ERR(idev)) {
Joe Perches91df42b2012-05-15 14:11:54 +00002818 pr_debug("init ip6-ip6: add_dev failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 return;
2820 }
2821 ip6_tnl_add_linklocal(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822}
2823
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002824static int addrconf_notify(struct notifier_block *this, unsigned long event,
Eldad Zack8e5e8f32012-04-01 07:49:08 +00002825 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826{
2827 struct net_device *dev = (struct net_device *) data;
Eric Dumazet748e2d92012-08-22 21:50:59 +00002828 struct inet6_dev *idev = __in6_dev_get(dev);
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09002829 int run_pending = 0;
Herbert Xub217d612007-07-30 17:04:52 -07002830 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831
Stephen Hemmingere21e8462010-03-20 16:09:01 -07002832 switch (event) {
YOSHIFUJI Hideaki45ba9dd2007-02-15 02:07:27 +09002833 case NETDEV_REGISTER:
Herbert Xu74235a22007-06-14 13:02:55 -07002834 if (!idev && dev->mtu >= IPV6_MIN_MTU) {
YOSHIFUJI Hideaki45ba9dd2007-02-15 02:07:27 +09002835 idev = ipv6_add_dev(dev);
2836 if (!idev)
Herbert Xub217d612007-07-30 17:04:52 -07002837 return notifier_from_errno(-ENOMEM);
YOSHIFUJI Hideaki45ba9dd2007-02-15 02:07:27 +09002838 }
2839 break;
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07002840
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 case NETDEV_UP:
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002842 case NETDEV_CHANGE:
Jay Vosburghc2edacf2007-07-09 10:42:47 -07002843 if (dev->flags & IFF_SLAVE)
2844 break;
2845
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002846 if (event == NETDEV_UP) {
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -07002847 if (!addrconf_qdisc_ok(dev)) {
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002848 /* device is not ready yet. */
Joe Perchesf3213832012-05-15 14:11:53 +00002849 pr_info("ADDRCONF(NETDEV_UP): %s: link is not ready\n",
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002850 dev->name);
2851 break;
2852 }
Kristian Slavov99081042006-02-08 16:10:53 -08002853
Evgeniy Polyakovd31c7b82007-11-30 23:36:08 +11002854 if (!idev && dev->mtu >= IPV6_MIN_MTU)
2855 idev = ipv6_add_dev(dev);
2856
Benjamin Therye3ec6cf2008-11-05 01:43:57 -08002857 if (idev) {
Kristian Slavov99081042006-02-08 16:10:53 -08002858 idev->if_flags |= IF_READY;
Benjamin Therye3ec6cf2008-11-05 01:43:57 -08002859 run_pending = 1;
2860 }
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002861 } else {
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -07002862 if (!addrconf_qdisc_ok(dev)) {
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002863 /* device is still not ready. */
2864 break;
2865 }
2866
2867 if (idev) {
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07002868 if (idev->if_flags & IF_READY)
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002869 /* device is already configured. */
2870 break;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002871 idev->if_flags |= IF_READY;
2872 }
2873
Joe Perchesf3213832012-05-15 14:11:53 +00002874 pr_info("ADDRCONF(NETDEV_CHANGE): %s: link becomes ready\n",
2875 dev->name);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002876
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09002877 run_pending = 1;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002878 }
2879
Stephen Hemmingere21e8462010-03-20 16:09:01 -07002880 switch (dev->type) {
Amerigo Wang07a93622012-10-29 16:23:10 +00002881#if IS_ENABLED(CONFIG_IPV6_SIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 case ARPHRD_SIT:
2883 addrconf_sit_config(dev);
2884 break;
Joerg Roedel0be669b2006-10-10 14:49:53 -07002885#endif
Amerigo Wang07a93622012-10-29 16:23:10 +00002886#if IS_ENABLED(CONFIG_NET_IPGRE)
stephen hemmingeraee80b52011-06-08 10:44:30 +00002887 case ARPHRD_IPGRE:
2888 addrconf_gre_config(dev);
2889 break;
2890#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 case ARPHRD_TUNNEL6:
2892 addrconf_ip6_tnl_config(dev);
2893 break;
2894 case ARPHRD_LOOPBACK:
2895 init_loopback(dev);
2896 break;
2897
2898 default:
2899 addrconf_dev_config(dev);
2900 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002901 }
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07002902
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903 if (idev) {
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09002904 if (run_pending)
2905 addrconf_dad_run(idev);
2906
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07002907 /*
2908 * If the MTU changed during the interface down,
2909 * when the interface up, the changed MTU must be
2910 * reflected in the idev as well as routers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 */
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07002912 if (idev->cnf.mtu6 != dev->mtu &&
2913 dev->mtu >= IPV6_MIN_MTU) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 rt6_mtu_change(dev, dev->mtu);
2915 idev->cnf.mtu6 = dev->mtu;
2916 }
2917 idev->tstamp = jiffies;
2918 inet6_ifinfo_notify(RTM_NEWLINK, idev);
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07002919
2920 /*
2921 * If the changed mtu during down is lower than
2922 * IPV6_MIN_MTU stop IPv6 on this interface.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 */
2924 if (dev->mtu < IPV6_MIN_MTU)
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07002925 addrconf_ifdown(dev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 }
2927 break;
2928
2929 case NETDEV_CHANGEMTU:
Evgeniy Polyakovd31c7b82007-11-30 23:36:08 +11002930 if (idev && dev->mtu >= IPV6_MIN_MTU) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 rt6_mtu_change(dev, dev->mtu);
2932 idev->cnf.mtu6 = dev->mtu;
2933 break;
2934 }
2935
Evgeniy Polyakovd31c7b82007-11-30 23:36:08 +11002936 if (!idev && dev->mtu >= IPV6_MIN_MTU) {
2937 idev = ipv6_add_dev(dev);
2938 if (idev)
2939 break;
2940 }
2941
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07002942 /*
2943 * MTU falled under IPV6_MIN_MTU.
2944 * Stop IPv6 on this interface.
2945 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946
2947 case NETDEV_DOWN:
2948 case NETDEV_UNREGISTER:
2949 /*
2950 * Remove all addresses from this interface.
2951 */
2952 addrconf_ifdown(dev, event != NETDEV_DOWN);
2953 break;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002954
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 case NETDEV_CHANGENAME:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 if (idev) {
Stephen Hemminger5632c512007-04-28 21:16:39 -07002957 snmp6_unregister_dev(idev);
Pavel Emelyanov408c4762008-01-10 17:41:21 -08002958 addrconf_sysctl_unregister(idev);
Pavel Emelyanovf52295a2007-12-02 00:58:37 +11002959 addrconf_sysctl_register(idev);
Herbert Xub217d612007-07-30 17:04:52 -07002960 err = snmp6_register_dev(idev);
2961 if (err)
2962 return notifier_from_errno(err);
Stephen Hemminger5632c512007-04-28 21:16:39 -07002963 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 break;
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07002965
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00002966 case NETDEV_PRE_TYPE_CHANGE:
2967 case NETDEV_POST_TYPE_CHANGE:
2968 addrconf_type_change(dev, event);
Moni Shoua75c78502009-09-15 02:37:40 -07002969 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002970 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971
2972 return NOTIFY_OK;
2973}
2974
2975/*
2976 * addrconf module should be notified of a device going up
2977 */
2978static struct notifier_block ipv6_dev_notf = {
2979 .notifier_call = addrconf_notify,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980};
2981
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00002982static void addrconf_type_change(struct net_device *dev, unsigned long event)
Moni Shoua75c78502009-09-15 02:37:40 -07002983{
2984 struct inet6_dev *idev;
2985 ASSERT_RTNL();
2986
2987 idev = __in6_dev_get(dev);
2988
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00002989 if (event == NETDEV_POST_TYPE_CHANGE)
Moni Shoua75c78502009-09-15 02:37:40 -07002990 ipv6_mc_remap(idev);
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00002991 else if (event == NETDEV_PRE_TYPE_CHANGE)
Moni Shoua75c78502009-09-15 02:37:40 -07002992 ipv6_mc_unmap(idev);
2993}
2994
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995static int addrconf_ifdown(struct net_device *dev, int how)
2996{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002997 struct net *net = dev_net(dev);
stephen hemminger502a2ff2010-03-17 20:31:13 +00002998 struct inet6_dev *idev;
2999 struct inet6_ifaddr *ifa;
David S. Miller73a8bd72011-01-23 23:27:15 -08003000 int state, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001
3002 ASSERT_RTNL();
3003
David S. Miller73a8bd72011-01-23 23:27:15 -08003004 rt6_ifdown(net, dev);
3005 neigh_ifdown(&nd_tbl, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006
3007 idev = __in6_dev_get(dev);
3008 if (idev == NULL)
3009 return -ENODEV;
3010
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003011 /*
3012 * Step 1: remove reference to ipv6 device from parent device.
3013 * Do not dev_put!
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014 */
Denis V. Lunev439e2382008-04-03 13:30:17 -07003015 if (how) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 idev->dead = 1;
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07003017
3018 /* protected by rtnl_lock */
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00003019 RCU_INIT_POINTER(dev->ip6_ptr, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020
3021 /* Step 1.5: remove snmp6 entry */
3022 snmp6_unregister_dev(idev);
3023
3024 }
3025
David S. Miller73a8bd72011-01-23 23:27:15 -08003026 /* Step 2: clear hash table */
3027 for (i = 0; i < IN6_ADDR_HSIZE; i++) {
3028 struct hlist_head *h = &inet6_addr_lst[i];
David S. Miller73a8bd72011-01-23 23:27:15 -08003029
3030 spin_lock_bh(&addrconf_hash_lock);
3031 restart:
Sasha Levinb67bfe02013-02-27 17:06:00 -08003032 hlist_for_each_entry_rcu(ifa, h, addr_lst) {
David S. Miller73a8bd72011-01-23 23:27:15 -08003033 if (ifa->idev == idev) {
3034 hlist_del_init_rcu(&ifa->addr_lst);
3035 addrconf_del_timer(ifa);
3036 goto restart;
3037 }
3038 }
3039 spin_unlock_bh(&addrconf_hash_lock);
3040 }
3041
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 write_lock_bh(&idev->lock);
3043
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003044 /* Step 2: clear flags for stateless addrconf */
Denis V. Lunev439e2382008-04-03 13:30:17 -07003045 if (!how)
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003046 idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048#ifdef CONFIG_IPV6_PRIVACY
Denis V. Lunev439e2382008-04-03 13:30:17 -07003049 if (how && del_timer(&idev->regen_timer))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 in6_dev_put(idev);
3051
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003052 /* Step 3: clear tempaddr list */
stephen hemminger372e6c82010-03-17 20:31:09 +00003053 while (!list_empty(&idev->tempaddr_list)) {
3054 ifa = list_first_entry(&idev->tempaddr_list,
3055 struct inet6_ifaddr, tmp_list);
3056 list_del(&ifa->tmp_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 write_unlock_bh(&idev->lock);
3058 spin_lock_bh(&ifa->lock);
3059
3060 if (ifa->ifpub) {
3061 in6_ifa_put(ifa->ifpub);
3062 ifa->ifpub = NULL;
3063 }
3064 spin_unlock_bh(&ifa->lock);
3065 in6_ifa_put(ifa);
3066 write_lock_bh(&idev->lock);
3067 }
3068#endif
stephen hemminger8f37ada2010-03-03 08:19:59 +00003069
stephen hemminger502a2ff2010-03-17 20:31:13 +00003070 while (!list_empty(&idev->addr_list)) {
3071 ifa = list_first_entry(&idev->addr_list,
3072 struct inet6_ifaddr, if_list);
stephen hemminger84e8b802010-03-02 13:32:46 +00003073 addrconf_del_timer(ifa);
3074
David S. Miller73a8bd72011-01-23 23:27:15 -08003075 list_del(&ifa->if_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076
David S. Miller73a8bd72011-01-23 23:27:15 -08003077 write_unlock_bh(&idev->lock);
stephen hemminger84e8b802010-03-02 13:32:46 +00003078
David S. Miller73a8bd72011-01-23 23:27:15 -08003079 spin_lock_bh(&ifa->state_lock);
3080 state = ifa->state;
3081 ifa->state = INET6_IFADDR_STATE_DEAD;
3082 spin_unlock_bh(&ifa->state_lock);
stephen hemminger84e8b802010-03-02 13:32:46 +00003083
David S. Miller73a8bd72011-01-23 23:27:15 -08003084 if (state != INET6_IFADDR_STATE_DEAD) {
3085 __ipv6_ifa_notify(RTM_DELADDR, ifa);
3086 atomic_notifier_call_chain(&inet6addr_chain, NETDEV_DOWN, ifa);
stephen hemminger27bdb2a2010-04-12 05:41:32 +00003087 }
David S. Miller73a8bd72011-01-23 23:27:15 -08003088 in6_ifa_put(ifa);
stephen hemminger8f37ada2010-03-03 08:19:59 +00003089
David S. Miller73a8bd72011-01-23 23:27:15 -08003090 write_lock_bh(&idev->lock);
3091 }
stephen hemminger8f37ada2010-03-03 08:19:59 +00003092
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 write_unlock_bh(&idev->lock);
3094
3095 /* Step 5: Discard multicast list */
Denis V. Lunev439e2382008-04-03 13:30:17 -07003096 if (how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 ipv6_mc_destroy_dev(idev);
3098 else
3099 ipv6_mc_down(idev);
3100
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 idev->tstamp = jiffies;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003102
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003103 /* Last: Shot the device (if unregistered) */
Denis V. Lunev439e2382008-04-03 13:30:17 -07003104 if (how) {
Pavel Emelyanov408c4762008-01-10 17:41:21 -08003105 addrconf_sysctl_unregister(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 neigh_parms_release(&nd_tbl, idev->nd_parms);
3107 neigh_ifdown(&nd_tbl, dev);
3108 in6_dev_put(idev);
3109 }
3110 return 0;
3111}
3112
3113static void addrconf_rs_timer(unsigned long data)
3114{
3115 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
stephen hemminger5b2a1952010-03-02 13:32:45 +00003116 struct inet6_dev *idev = ifp->idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117
stephen hemminger5b2a1952010-03-02 13:32:45 +00003118 read_lock(&idev->lock);
3119 if (idev->dead || !(idev->if_flags & IF_READY))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 goto out;
3121
Shmulik Ladkani9ba2add2012-12-02 01:44:53 +00003122 if (!ipv6_accept_ra(idev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 goto out;
stephen hemminger5b2a1952010-03-02 13:32:45 +00003124
3125 /* Announcement received after solicitation was sent */
3126 if (idev->if_flags & IF_RA_RCVD)
3127 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128
3129 spin_lock(&ifp->lock);
stephen hemminger5b2a1952010-03-02 13:32:45 +00003130 if (ifp->probes++ < idev->cnf.rtr_solicits) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 /* The wait after the last probe can be shorter */
3132 addrconf_mod_timer(ifp, AC_RS,
stephen hemminger5b2a1952010-03-02 13:32:45 +00003133 (ifp->probes == idev->cnf.rtr_solicits) ?
3134 idev->cnf.rtr_solicit_delay :
3135 idev->cnf.rtr_solicit_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 spin_unlock(&ifp->lock);
3137
stephen hemminger5b2a1952010-03-02 13:32:45 +00003138 ndisc_send_rs(idev->dev, &ifp->addr, &in6addr_linklocal_allrouters);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139 } else {
3140 spin_unlock(&ifp->lock);
3141 /*
3142 * Note: we do not support deprecated "all on-link"
3143 * assumption any longer.
3144 */
Joe Perches91df42b2012-05-15 14:11:54 +00003145 pr_debug("%s: no IPv6 routers present\n", idev->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 }
3147
3148out:
stephen hemminger5b2a1952010-03-02 13:32:45 +00003149 read_unlock(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 in6_ifa_put(ifp);
3151}
3152
3153/*
3154 * Duplicate Address Detection
3155 */
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003156static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
3157{
3158 unsigned long rand_num;
3159 struct inet6_dev *idev = ifp->idev;
3160
Neil Horman95c385b2007-04-25 17:08:10 -07003161 if (ifp->flags & IFA_F_OPTIMISTIC)
3162 rand_num = 0;
3163 else
3164 rand_num = net_random() % (idev->cnf.rtr_solicit_delay ? : 1);
3165
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003166 ifp->probes = idev->cnf.dad_transmits;
3167 addrconf_mod_timer(ifp, AC_DAD, rand_num);
3168}
3169
David S. Millercf22f9a2012-04-14 21:37:40 -04003170static void addrconf_dad_start(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171{
3172 struct inet6_dev *idev = ifp->idev;
3173 struct net_device *dev = idev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174
3175 addrconf_join_solict(dev, &ifp->addr);
3176
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177 net_srandom(ifp->addr.s6_addr32[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178
3179 read_lock_bh(&idev->lock);
Herbert Xu622ccdf2010-05-18 15:56:06 -07003180 spin_lock(&ifp->lock);
Herbert Xue9d3e082010-05-18 15:36:06 -07003181 if (ifp->state == INET6_IFADDR_STATE_DEAD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183
3184 if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09003185 idev->cnf.accept_dad < 1 ||
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003186 !(ifp->flags&IFA_F_TENTATIVE) ||
3187 ifp->flags & IFA_F_NODAD) {
Brian Haleycc411d02009-09-09 14:41:32 +00003188 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
stephen hemminger21809fa2010-02-08 19:48:52 +00003189 spin_unlock(&ifp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190 read_unlock_bh(&idev->lock);
3191
3192 addrconf_dad_completed(ifp);
3193 return;
3194 }
3195
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08003196 if (!(idev->if_flags & IF_READY)) {
stephen hemminger21809fa2010-02-08 19:48:52 +00003197 spin_unlock(&ifp->lock);
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08003198 read_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003199 /*
Masatake YAMATO590a9882009-06-09 10:41:12 +09003200 * If the device is not ready:
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003201 * - keep it tentative if it is a permanent address.
3202 * - otherwise, kill it.
3203 */
3204 in6_ifa_hold(ifp);
Brian Haleycc411d02009-09-09 14:41:32 +00003205 addrconf_dad_stop(ifp, 0);
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08003206 return;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003207 }
Neil Horman95c385b2007-04-25 17:08:10 -07003208
3209 /*
3210 * Optimistic nodes can start receiving
3211 * Frames right away
3212 */
Stephen Hemmingere21e8462010-03-20 16:09:01 -07003213 if (ifp->flags & IFA_F_OPTIMISTIC)
Neil Horman95c385b2007-04-25 17:08:10 -07003214 ip6_ins_rt(ifp->rt);
3215
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08003216 addrconf_dad_kick(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217out:
Herbert Xu622ccdf2010-05-18 15:56:06 -07003218 spin_unlock(&ifp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 read_unlock_bh(&idev->lock);
3220}
3221
3222static void addrconf_dad_timer(unsigned long data)
3223{
3224 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
3225 struct inet6_dev *idev = ifp->idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226 struct in6_addr mcaddr;
3227
Herbert Xuf2344a12010-05-18 15:55:27 -07003228 if (!ifp->probes && addrconf_dad_end(ifp))
3229 goto out;
3230
stephen hemminger122e4512010-03-02 13:32:44 +00003231 read_lock(&idev->lock);
3232 if (idev->dead || !(idev->if_flags & IF_READY)) {
3233 read_unlock(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 goto out;
3235 }
stephen hemminger21809fa2010-02-08 19:48:52 +00003236
3237 spin_lock(&ifp->lock);
Herbert Xu622ccdf2010-05-18 15:56:06 -07003238 if (ifp->state == INET6_IFADDR_STATE_DEAD) {
3239 spin_unlock(&ifp->lock);
3240 read_unlock(&idev->lock);
3241 goto out;
3242 }
3243
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244 if (ifp->probes == 0) {
3245 /*
3246 * DAD was successful
3247 */
3248
Brian Haleycc411d02009-09-09 14:41:32 +00003249 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
stephen hemminger21809fa2010-02-08 19:48:52 +00003250 spin_unlock(&ifp->lock);
stephen hemminger122e4512010-03-02 13:32:44 +00003251 read_unlock(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252
3253 addrconf_dad_completed(ifp);
3254
3255 goto out;
3256 }
3257
3258 ifp->probes--;
3259 addrconf_mod_timer(ifp, AC_DAD, ifp->idev->nd_parms->retrans_time);
stephen hemminger21809fa2010-02-08 19:48:52 +00003260 spin_unlock(&ifp->lock);
stephen hemminger122e4512010-03-02 13:32:44 +00003261 read_unlock(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262
3263 /* send a neighbour solicitation for our addr */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264 addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
YOSHIFUJI Hideakid7aabf22008-04-10 15:42:11 +09003265 ndisc_send_ns(ifp->idev->dev, NULL, &ifp->addr, &mcaddr, &in6addr_any);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266out:
3267 in6_ifa_put(ifp);
3268}
3269
3270static void addrconf_dad_completed(struct inet6_ifaddr *ifp)
3271{
Stephen Hemmingere21e8462010-03-20 16:09:01 -07003272 struct net_device *dev = ifp->idev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273
3274 /*
3275 * Configure the address for reception. Now it is valid.
3276 */
3277
3278 ipv6_ifa_notify(RTM_NEWADDR, ifp);
3279
Tore Anderson026359b2011-08-28 23:47:33 +00003280 /* If added prefix is link local and we are prepared to process
3281 router advertisements, start sending router solicitations.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 */
3283
Shmulik Ladkaniaeaf6e92012-11-30 10:25:59 +00003284 if (ipv6_accept_ra(ifp->idev) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285 ifp->idev->cnf.rtr_solicits > 0 &&
3286 (dev->flags&IFF_LOOPBACK) == 0 &&
3287 (ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288 /*
3289 * If a host as already performed a random delay
3290 * [...] as part of DAD [...] there is no need
3291 * to delay again before sending the first RS
3292 */
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +09003293 ndisc_send_rs(ifp->idev->dev, &ifp->addr, &in6addr_linklocal_allrouters);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294
3295 spin_lock_bh(&ifp->lock);
3296 ifp->probes = 1;
3297 ifp->idev->if_flags |= IF_RS_SENT;
3298 addrconf_mod_timer(ifp, AC_RS, ifp->idev->cnf.rtr_solicit_interval);
3299 spin_unlock_bh(&ifp->lock);
3300 }
3301}
3302
Stephen Hemmingere21e8462010-03-20 16:09:01 -07003303static void addrconf_dad_run(struct inet6_dev *idev)
3304{
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09003305 struct inet6_ifaddr *ifp;
3306
3307 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00003308 list_for_each_entry(ifp, &idev->addr_list, if_list) {
stephen hemminger21809fa2010-02-08 19:48:52 +00003309 spin_lock(&ifp->lock);
Herbert Xuf2344a12010-05-18 15:55:27 -07003310 if (ifp->flags & IFA_F_TENTATIVE &&
3311 ifp->state == INET6_IFADDR_STATE_DAD)
3312 addrconf_dad_kick(ifp);
stephen hemminger21809fa2010-02-08 19:48:52 +00003313 spin_unlock(&ifp->lock);
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09003314 }
3315 read_unlock_bh(&idev->lock);
3316}
3317
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318#ifdef CONFIG_PROC_FS
3319struct if6_iter_state {
Daniel Lezcano3c400902008-01-10 22:42:49 -08003320 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 int bucket;
Mihai Maruseac1d578302012-01-03 23:31:35 +00003322 int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323};
3324
Mihai Maruseac1d578302012-01-03 23:31:35 +00003325static struct inet6_ifaddr *if6_get_first(struct seq_file *seq, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326{
3327 struct inet6_ifaddr *ifa = NULL;
3328 struct if6_iter_state *state = seq->private;
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09003329 struct net *net = seq_file_net(seq);
Mihai Maruseac1d578302012-01-03 23:31:35 +00003330 int p = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331
Mihai Maruseac1d578302012-01-03 23:31:35 +00003332 /* initial bucket if pos is 0 */
3333 if (pos == 0) {
3334 state->bucket = 0;
3335 state->offset = 0;
3336 }
3337
3338 for (; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08003339 hlist_for_each_entry_rcu_bh(ifa, &inet6_addr_lst[state->bucket],
Mihai Maruseac1d578302012-01-03 23:31:35 +00003340 addr_lst) {
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00003341 if (!net_eq(dev_net(ifa->idev->dev), net))
3342 continue;
Mihai Maruseac1d578302012-01-03 23:31:35 +00003343 /* sync with offset */
3344 if (p < state->offset) {
3345 p++;
3346 continue;
3347 }
3348 state->offset++;
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00003349 return ifa;
Mihai Maruseac1d578302012-01-03 23:31:35 +00003350 }
3351
3352 /* prepare for next bucket */
3353 state->offset = 0;
3354 p = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355 }
stephen hemmingerc2e21292010-03-17 20:31:10 +00003356 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357}
3358
stephen hemmingerc2e21292010-03-17 20:31:10 +00003359static struct inet6_ifaddr *if6_get_next(struct seq_file *seq,
3360 struct inet6_ifaddr *ifa)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361{
3362 struct if6_iter_state *state = seq->private;
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09003363 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364
Sasha Levinb67bfe02013-02-27 17:06:00 -08003365 hlist_for_each_entry_continue_rcu_bh(ifa, addr_lst) {
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00003366 if (!net_eq(dev_net(ifa->idev->dev), net))
3367 continue;
Mihai Maruseac1d578302012-01-03 23:31:35 +00003368 state->offset++;
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00003369 return ifa;
Mihai Maruseac1d578302012-01-03 23:31:35 +00003370 }
stephen hemmingerc2e21292010-03-17 20:31:10 +00003371
3372 while (++state->bucket < IN6_ADDR_HSIZE) {
Mihai Maruseac1d578302012-01-03 23:31:35 +00003373 state->offset = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -08003374 hlist_for_each_entry_rcu_bh(ifa,
stephen hemmingerc2e21292010-03-17 20:31:10 +00003375 &inet6_addr_lst[state->bucket], addr_lst) {
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00003376 if (!net_eq(dev_net(ifa->idev->dev), net))
3377 continue;
Mihai Maruseac1d578302012-01-03 23:31:35 +00003378 state->offset++;
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00003379 return ifa;
Daniel Lezcano3c400902008-01-10 22:42:49 -08003380 }
3381 }
3382
stephen hemmingerc2e21292010-03-17 20:31:10 +00003383 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384}
3385
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
Eric Dumazet4f70ecc2010-05-03 10:50:14 +00003387 __acquires(rcu_bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388{
stephen hemminger5c578aed2010-03-17 20:31:11 +00003389 rcu_read_lock_bh();
Mihai Maruseac1d578302012-01-03 23:31:35 +00003390 return if6_get_first(seq, *pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391}
3392
3393static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3394{
3395 struct inet6_ifaddr *ifa;
3396
3397 ifa = if6_get_next(seq, v);
3398 ++*pos;
3399 return ifa;
3400}
3401
3402static void if6_seq_stop(struct seq_file *seq, void *v)
Eric Dumazet4f70ecc2010-05-03 10:50:14 +00003403 __releases(rcu_bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404{
stephen hemminger5c578aed2010-03-17 20:31:11 +00003405 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406}
3407
3408static int if6_seq_show(struct seq_file *seq, void *v)
3409{
3410 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
Harvey Harrison4b7a4272008-10-29 12:50:24 -07003411 seq_printf(seq, "%pi6 %02x %02x %02x %02x %8s\n",
Harvey Harrisonb0711952008-10-28 16:05:40 -07003412 &ifp->addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413 ifp->idev->dev->ifindex,
3414 ifp->prefix_len,
3415 ifp->scope,
3416 ifp->flags,
3417 ifp->idev->dev->name);
3418 return 0;
3419}
3420
Philippe De Muyter56b3d972007-07-10 23:07:31 -07003421static const struct seq_operations if6_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422 .start = if6_seq_start,
3423 .next = if6_seq_next,
3424 .show = if6_seq_show,
3425 .stop = if6_seq_stop,
3426};
3427
3428static int if6_seq_open(struct inode *inode, struct file *file)
3429{
Daniel Lezcano3c400902008-01-10 22:42:49 -08003430 return seq_open_net(inode, file, &if6_seq_ops,
3431 sizeof(struct if6_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432}
3433
Arjan van de Ven9a321442007-02-12 00:55:35 -08003434static const struct file_operations if6_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435 .owner = THIS_MODULE,
3436 .open = if6_seq_open,
3437 .read = seq_read,
3438 .llseek = seq_lseek,
Daniel Lezcano3c400902008-01-10 22:42:49 -08003439 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440};
3441
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003442static int __net_init if6_proc_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443{
Gao fengd4beaa62013-02-18 01:34:54 +00003444 if (!proc_create("if_inet6", S_IRUGO, net->proc_net, &if6_fops))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445 return -ENOMEM;
3446 return 0;
3447}
3448
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003449static void __net_exit if6_proc_net_exit(struct net *net)
Daniel Lezcano3c400902008-01-10 22:42:49 -08003450{
Gao fengece31ff2013-02-18 01:34:56 +00003451 remove_proc_entry("if_inet6", net->proc_net);
Daniel Lezcano3c400902008-01-10 22:42:49 -08003452}
3453
3454static struct pernet_operations if6_proc_net_ops = {
3455 .init = if6_proc_net_init,
3456 .exit = if6_proc_net_exit,
3457};
3458
3459int __init if6_proc_init(void)
3460{
3461 return register_pernet_subsys(&if6_proc_net_ops);
3462}
3463
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464void if6_proc_exit(void)
3465{
Daniel Lezcano3c400902008-01-10 22:42:49 -08003466 unregister_pernet_subsys(&if6_proc_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467}
3468#endif /* CONFIG_PROC_FS */
3469
Amerigo Wang07a93622012-10-29 16:23:10 +00003470#if IS_ENABLED(CONFIG_IPV6_MIP6)
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07003471/* Check if address is a home address configured on any interface. */
Eric Dumazetb71d1d42011-04-22 04:53:02 +00003472int ipv6_chk_home_addr(struct net *net, const struct in6_addr *addr)
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07003473{
3474 int ret = 0;
stephen hemmingerc2e21292010-03-17 20:31:10 +00003475 struct inet6_ifaddr *ifp = NULL;
Eric Dumazetddbe5032012-07-18 08:11:12 +00003476 unsigned int hash = inet6_addr_hash(addr);
stephen hemmingerc2e21292010-03-17 20:31:10 +00003477
stephen hemminger5c578aed2010-03-17 20:31:11 +00003478 rcu_read_lock_bh();
Sasha Levinb67bfe02013-02-27 17:06:00 -08003479 hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09003480 if (!net_eq(dev_net(ifp->idev->dev), net))
Daniel Lezcano389f6612008-01-10 22:44:40 -08003481 continue;
YOSHIFUJI Hideakicaad2952008-04-10 15:42:07 +09003482 if (ipv6_addr_equal(&ifp->addr, addr) &&
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07003483 (ifp->flags & IFA_F_HOMEADDRESS)) {
3484 ret = 1;
3485 break;
3486 }
3487 }
stephen hemminger5c578aed2010-03-17 20:31:11 +00003488 rcu_read_unlock_bh();
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07003489 return ret;
3490}
3491#endif
3492
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493/*
3494 * Periodic address status verification
3495 */
3496
3497static void addrconf_verify(unsigned long foo)
3498{
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -07003499 unsigned long now, next, next_sec, next_sched;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500 struct inet6_ifaddr *ifp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501 int i;
3502
stephen hemminger5c578aed2010-03-17 20:31:11 +00003503 rcu_read_lock_bh();
3504 spin_lock(&addrconf_verify_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505 now = jiffies;
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -07003506 next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507
3508 del_timer(&addr_chk_timer);
3509
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003510 for (i = 0; i < IN6_ADDR_HSIZE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511restart:
Sasha Levinb67bfe02013-02-27 17:06:00 -08003512 hlist_for_each_entry_rcu_bh(ifp,
stephen hemminger5c578aed2010-03-17 20:31:11 +00003513 &inet6_addr_lst[i], addr_lst) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514 unsigned long age;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515
3516 if (ifp->flags & IFA_F_PERMANENT)
3517 continue;
3518
3519 spin_lock(&ifp->lock);
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -07003520 /* We try to batch several events at once. */
3521 age = (now - ifp->tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522
YOSHIFUJI Hideaki8f27ebb2006-07-28 18:12:11 +09003523 if (ifp->valid_lft != INFINITY_LIFE_TIME &&
3524 age >= ifp->valid_lft) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525 spin_unlock(&ifp->lock);
3526 in6_ifa_hold(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527 ipv6_del_addr(ifp);
3528 goto restart;
YOSHIFUJI Hideaki8f27ebb2006-07-28 18:12:11 +09003529 } else if (ifp->prefered_lft == INFINITY_LIFE_TIME) {
3530 spin_unlock(&ifp->lock);
3531 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532 } else if (age >= ifp->prefered_lft) {
Brian Haleya1ed0522009-07-02 07:10:52 +00003533 /* jiffies - ifp->tstamp > age >= ifp->prefered_lft */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534 int deprecate = 0;
3535
3536 if (!(ifp->flags&IFA_F_DEPRECATED)) {
3537 deprecate = 1;
3538 ifp->flags |= IFA_F_DEPRECATED;
3539 }
3540
3541 if (time_before(ifp->tstamp + ifp->valid_lft * HZ, next))
3542 next = ifp->tstamp + ifp->valid_lft * HZ;
3543
3544 spin_unlock(&ifp->lock);
3545
3546 if (deprecate) {
3547 in6_ifa_hold(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548
3549 ipv6_ifa_notify(0, ifp);
3550 in6_ifa_put(ifp);
3551 goto restart;
3552 }
3553#ifdef CONFIG_IPV6_PRIVACY
3554 } else if ((ifp->flags&IFA_F_TEMPORARY) &&
3555 !(ifp->flags&IFA_F_TENTATIVE)) {
stephen hemminger88949cf2010-03-17 20:31:17 +00003556 unsigned long regen_advance = ifp->idev->cnf.regen_max_retry *
3557 ifp->idev->cnf.dad_transmits *
3558 ifp->idev->nd_parms->retrans_time / HZ;
3559
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560 if (age >= ifp->prefered_lft - regen_advance) {
3561 struct inet6_ifaddr *ifpub = ifp->ifpub;
3562 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
3563 next = ifp->tstamp + ifp->prefered_lft * HZ;
3564 if (!ifp->regen_count && ifpub) {
3565 ifp->regen_count++;
3566 in6_ifa_hold(ifp);
3567 in6_ifa_hold(ifpub);
3568 spin_unlock(&ifp->lock);
stephen hemminger5c578aed2010-03-17 20:31:11 +00003569
Hiroyuki YAMAMORI291d8092005-12-23 11:24:05 -08003570 spin_lock(&ifpub->lock);
3571 ifpub->regen_count = 0;
3572 spin_unlock(&ifpub->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573 ipv6_create_tempaddr(ifpub, ifp);
3574 in6_ifa_put(ifpub);
3575 in6_ifa_put(ifp);
3576 goto restart;
3577 }
3578 } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
3579 next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
3580 spin_unlock(&ifp->lock);
3581#endif
3582 } else {
3583 /* ifp->prefered_lft <= ifp->valid_lft */
3584 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
3585 next = ifp->tstamp + ifp->prefered_lft * HZ;
3586 spin_unlock(&ifp->lock);
3587 }
3588 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589 }
3590
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -07003591 next_sec = round_jiffies_up(next);
3592 next_sched = next;
3593
3594 /* If rounded timeout is accurate enough, accept it. */
3595 if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ))
3596 next_sched = next_sec;
3597
3598 /* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */
3599 if (time_before(next_sched, jiffies + ADDRCONF_TIMER_FUZZ_MAX))
3600 next_sched = jiffies + ADDRCONF_TIMER_FUZZ_MAX;
3601
3602 ADBG((KERN_DEBUG "now = %lu, schedule = %lu, rounded schedule = %lu => %lu\n",
3603 now, next, next_sec, next_sched));
3604
3605 addr_chk_timer.expires = next_sched;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606 add_timer(&addr_chk_timer);
stephen hemminger5c578aed2010-03-17 20:31:11 +00003607 spin_unlock(&addrconf_verify_lock);
3608 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609}
3610
Thomas Graf461d8832006-09-18 00:09:49 -07003611static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local)
3612{
3613 struct in6_addr *pfx = NULL;
3614
3615 if (addr)
3616 pfx = nla_data(addr);
3617
3618 if (local) {
3619 if (pfx && nla_memcmp(local, pfx, sizeof(*pfx)))
3620 pfx = NULL;
3621 else
3622 pfx = nla_data(local);
3623 }
3624
3625 return pfx;
3626}
3627
Patrick McHardyef7c79e2007-06-05 12:38:30 -07003628static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = {
Thomas Graf461d8832006-09-18 00:09:49 -07003629 [IFA_ADDRESS] = { .len = sizeof(struct in6_addr) },
3630 [IFA_LOCAL] = { .len = sizeof(struct in6_addr) },
3631 [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) },
3632};
3633
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634static int
Thomas Graf661d2962013-03-21 07:45:29 +00003635inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09003637 struct net *net = sock_net(skb->sk);
Thomas Grafb933f712006-09-18 00:10:19 -07003638 struct ifaddrmsg *ifm;
3639 struct nlattr *tb[IFA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640 struct in6_addr *pfx;
Thomas Grafb933f712006-09-18 00:10:19 -07003641 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642
Thomas Grafb933f712006-09-18 00:10:19 -07003643 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
3644 if (err < 0)
3645 return err;
3646
3647 ifm = nlmsg_data(nlh);
3648 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649 if (pfx == NULL)
3650 return -EINVAL;
3651
Daniel Lezcanoaf284932008-03-05 10:46:57 -08003652 return inet6_addr_del(net, ifm->ifa_index, pfx, ifm->ifa_prefixlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653}
3654
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003655static int inet6_addr_modify(struct inet6_ifaddr *ifp, u8 ifa_flags,
3656 u32 prefered_lft, u32 valid_lft)
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09003657{
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07003658 u32 flags;
3659 clock_t expires;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09003660 unsigned long timeout;
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09003661
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09003662 if (!valid_lft || (prefered_lft > valid_lft))
3663 return -EINVAL;
3664
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09003665 timeout = addrconf_timeout_fixup(valid_lft, HZ);
3666 if (addrconf_finite_timeout(timeout)) {
3667 expires = jiffies_to_clock_t(timeout * HZ);
3668 valid_lft = timeout;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07003669 flags = RTF_EXPIRES;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09003670 } else {
3671 expires = 0;
3672 flags = 0;
3673 ifa_flags |= IFA_F_PERMANENT;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07003674 }
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09003675
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09003676 timeout = addrconf_timeout_fixup(prefered_lft, HZ);
3677 if (addrconf_finite_timeout(timeout)) {
3678 if (timeout == 0)
3679 ifa_flags |= IFA_F_DEPRECATED;
3680 prefered_lft = timeout;
3681 }
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09003682
3683 spin_lock_bh(&ifp->lock);
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07003684 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 +09003685 ifp->tstamp = jiffies;
3686 ifp->valid_lft = valid_lft;
3687 ifp->prefered_lft = prefered_lft;
3688
3689 spin_unlock_bh(&ifp->lock);
3690 if (!(ifp->flags&IFA_F_TENTATIVE))
3691 ipv6_ifa_notify(0, ifp);
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09003692
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09003693 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, ifp->idev->dev,
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07003694 expires, flags);
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09003695 addrconf_verify(0);
3696
3697 return 0;
3698}
3699
3700static int
Thomas Graf661d2962013-03-21 07:45:29 +00003701inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09003703 struct net *net = sock_net(skb->sk);
Thomas Graf461d8832006-09-18 00:09:49 -07003704 struct ifaddrmsg *ifm;
3705 struct nlattr *tb[IFA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706 struct in6_addr *pfx;
Thomas Graf7198f8c2006-09-18 00:13:46 -07003707 struct inet6_ifaddr *ifa;
3708 struct net_device *dev;
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003709 u32 valid_lft = INFINITY_LIFE_TIME, preferred_lft = INFINITY_LIFE_TIME;
3710 u8 ifa_flags;
Thomas Graf461d8832006-09-18 00:09:49 -07003711 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712
Thomas Graf461d8832006-09-18 00:09:49 -07003713 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
3714 if (err < 0)
3715 return err;
3716
3717 ifm = nlmsg_data(nlh);
3718 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719 if (pfx == NULL)
3720 return -EINVAL;
3721
Thomas Graf461d8832006-09-18 00:09:49 -07003722 if (tb[IFA_CACHEINFO]) {
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09003723 struct ifa_cacheinfo *ci;
Thomas Graf461d8832006-09-18 00:09:49 -07003724
3725 ci = nla_data(tb[IFA_CACHEINFO]);
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09003726 valid_lft = ci->ifa_valid;
Thomas Graf461d8832006-09-18 00:09:49 -07003727 preferred_lft = ci->ifa_prefered;
3728 } else {
3729 preferred_lft = INFINITY_LIFE_TIME;
3730 valid_lft = INFINITY_LIFE_TIME;
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09003731 }
3732
Daniel Lezcanoaf284932008-03-05 10:46:57 -08003733 dev = __dev_get_by_index(net, ifm->ifa_index);
Thomas Graf7198f8c2006-09-18 00:13:46 -07003734 if (dev == NULL)
3735 return -ENODEV;
3736
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003737 /* We ignore other flags so far. */
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07003738 ifa_flags = ifm->ifa_flags & (IFA_F_NODAD | IFA_F_HOMEADDRESS);
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003739
Daniel Lezcano1cab3da2008-01-10 22:44:09 -08003740 ifa = ipv6_get_ifaddr(net, pfx, dev, 1);
Thomas Graf7198f8c2006-09-18 00:13:46 -07003741 if (ifa == NULL) {
3742 /*
3743 * It would be best to check for !NLM_F_CREATE here but
3744 * userspace alreay relies on not having to provide this.
3745 */
Daniel Lezcanoaf284932008-03-05 10:46:57 -08003746 return inet6_addr_add(net, ifm->ifa_index, pfx,
3747 ifm->ifa_prefixlen, ifa_flags,
3748 preferred_lft, valid_lft);
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09003749 }
3750
Thomas Graf7198f8c2006-09-18 00:13:46 -07003751 if (nlh->nlmsg_flags & NLM_F_EXCL ||
3752 !(nlh->nlmsg_flags & NLM_F_REPLACE))
3753 err = -EEXIST;
3754 else
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003755 err = inet6_addr_modify(ifa, ifa_flags, preferred_lft, valid_lft);
Thomas Graf7198f8c2006-09-18 00:13:46 -07003756
3757 in6_ifa_put(ifa);
3758
3759 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760}
3761
Thomas Graf101bb222006-09-18 00:11:52 -07003762static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u8 flags,
3763 u8 scope, int ifindex)
3764{
3765 struct ifaddrmsg *ifm;
3766
3767 ifm = nlmsg_data(nlh);
3768 ifm->ifa_family = AF_INET6;
3769 ifm->ifa_prefixlen = prefixlen;
3770 ifm->ifa_flags = flags;
3771 ifm->ifa_scope = scope;
3772 ifm->ifa_index = ifindex;
3773}
3774
Thomas Graf85486af2006-09-18 00:11:24 -07003775static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
3776 unsigned long tstamp, u32 preferred, u32 valid)
3777{
3778 struct ifa_cacheinfo ci;
3779
Thomas Graf18a31e12010-11-17 04:12:02 +00003780 ci.cstamp = cstamp_delta(cstamp);
3781 ci.tstamp = cstamp_delta(tstamp);
Thomas Graf85486af2006-09-18 00:11:24 -07003782 ci.ifa_prefered = preferred;
3783 ci.ifa_valid = valid;
3784
3785 return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
3786}
3787
Thomas Graf101bb222006-09-18 00:11:52 -07003788static inline int rt_scope(int ifa_scope)
3789{
3790 if (ifa_scope & IFA_HOST)
3791 return RT_SCOPE_HOST;
3792 else if (ifa_scope & IFA_LINK)
3793 return RT_SCOPE_LINK;
3794 else if (ifa_scope & IFA_SITE)
3795 return RT_SCOPE_SITE;
3796 else
3797 return RT_SCOPE_UNIVERSE;
3798}
3799
Thomas Graf0ab68032006-09-18 00:12:35 -07003800static inline int inet6_ifaddr_msgsize(void)
3801{
Thomas Graf339bf982006-11-10 14:10:15 -08003802 return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
3803 + nla_total_size(16) /* IFA_ADDRESS */
3804 + nla_total_size(sizeof(struct ifa_cacheinfo));
Thomas Graf0ab68032006-09-18 00:12:35 -07003805}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07003806
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
Eric W. Biederman15e47302012-09-07 20:12:54 +00003808 u32 portid, u32 seq, int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810 struct nlmsghdr *nlh;
Thomas Graf85486af2006-09-18 00:11:24 -07003811 u32 preferred, valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812
Eric W. Biederman15e47302012-09-07 20:12:54 +00003813 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
Thomas Graf0ab68032006-09-18 00:12:35 -07003814 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08003815 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07003816
Thomas Graf101bb222006-09-18 00:11:52 -07003817 put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope),
3818 ifa->idev->dev->ifindex);
3819
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820 if (!(ifa->flags&IFA_F_PERMANENT)) {
Thomas Graf85486af2006-09-18 00:11:24 -07003821 preferred = ifa->prefered_lft;
3822 valid = ifa->valid_lft;
3823 if (preferred != INFINITY_LIFE_TIME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824 long tval = (jiffies - ifa->tstamp)/HZ;
Jens Rosenbooma1faa692009-06-25 04:55:50 +00003825 if (preferred > tval)
3826 preferred -= tval;
3827 else
3828 preferred = 0;
Ben Hutchingsf56619f2010-06-26 11:37:47 +00003829 if (valid != INFINITY_LIFE_TIME) {
3830 if (valid > tval)
3831 valid -= tval;
3832 else
3833 valid = 0;
3834 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835 }
3836 } else {
Thomas Graf85486af2006-09-18 00:11:24 -07003837 preferred = INFINITY_LIFE_TIME;
3838 valid = INFINITY_LIFE_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839 }
Thomas Graf85486af2006-09-18 00:11:24 -07003840
Thomas Graf0ab68032006-09-18 00:12:35 -07003841 if (nla_put(skb, IFA_ADDRESS, 16, &ifa->addr) < 0 ||
Patrick McHardy26932562007-01-31 23:16:40 -08003842 put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0) {
3843 nlmsg_cancel(skb, nlh);
3844 return -EMSGSIZE;
3845 }
Thomas Graf85486af2006-09-18 00:11:24 -07003846
Thomas Graf0ab68032006-09-18 00:12:35 -07003847 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848}
3849
3850static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
Eric W. Biederman15e47302012-09-07 20:12:54 +00003851 u32 portid, u32 seq, int event, u16 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853 struct nlmsghdr *nlh;
Thomas Graf101bb222006-09-18 00:11:52 -07003854 u8 scope = RT_SCOPE_UNIVERSE;
3855 int ifindex = ifmca->idev->dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856
Thomas Graf101bb222006-09-18 00:11:52 -07003857 if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE)
3858 scope = RT_SCOPE_SITE;
3859
Eric W. Biederman15e47302012-09-07 20:12:54 +00003860 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
Thomas Graf0ab68032006-09-18 00:12:35 -07003861 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08003862 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07003863
Thomas Graf101bb222006-09-18 00:11:52 -07003864 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
Thomas Graf0ab68032006-09-18 00:12:35 -07003865 if (nla_put(skb, IFA_MULTICAST, 16, &ifmca->mca_addr) < 0 ||
3866 put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp,
Patrick McHardy26932562007-01-31 23:16:40 -08003867 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
3868 nlmsg_cancel(skb, nlh);
3869 return -EMSGSIZE;
3870 }
Thomas Graf85486af2006-09-18 00:11:24 -07003871
Thomas Graf0ab68032006-09-18 00:12:35 -07003872 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873}
3874
3875static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
Eric W. Biederman15e47302012-09-07 20:12:54 +00003876 u32 portid, u32 seq, int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878 struct nlmsghdr *nlh;
Thomas Graf101bb222006-09-18 00:11:52 -07003879 u8 scope = RT_SCOPE_UNIVERSE;
3880 int ifindex = ifaca->aca_idev->dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881
Thomas Graf101bb222006-09-18 00:11:52 -07003882 if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE)
3883 scope = RT_SCOPE_SITE;
3884
Eric W. Biederman15e47302012-09-07 20:12:54 +00003885 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
Thomas Graf0ab68032006-09-18 00:12:35 -07003886 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08003887 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07003888
Thomas Graf101bb222006-09-18 00:11:52 -07003889 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
Thomas Graf0ab68032006-09-18 00:12:35 -07003890 if (nla_put(skb, IFA_ANYCAST, 16, &ifaca->aca_addr) < 0 ||
3891 put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp,
Patrick McHardy26932562007-01-31 23:16:40 -08003892 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
3893 nlmsg_cancel(skb, nlh);
3894 return -EMSGSIZE;
3895 }
Thomas Graf85486af2006-09-18 00:11:24 -07003896
Thomas Graf0ab68032006-09-18 00:12:35 -07003897 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003898}
3899
Stephen Hemmingere21e8462010-03-20 16:09:01 -07003900enum addr_type_t {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901 UNICAST_ADDR,
3902 MULTICAST_ADDR,
3903 ANYCAST_ADDR,
3904};
3905
Eric Dumazet234b27c2009-11-12 04:11:50 +00003906/* called with rcu_read_lock() */
3907static int in6_dump_addrs(struct inet6_dev *idev, struct sk_buff *skb,
3908 struct netlink_callback *cb, enum addr_type_t type,
3909 int s_ip_idx, int *p_ip_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003911 struct ifmcaddr6 *ifmca;
3912 struct ifacaddr6 *ifaca;
Eric Dumazet234b27c2009-11-12 04:11:50 +00003913 int err = 1;
3914 int ip_idx = *p_ip_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915
Eric Dumazet234b27c2009-11-12 04:11:50 +00003916 read_lock_bh(&idev->lock);
3917 switch (type) {
stephen hemminger502a2ff2010-03-17 20:31:13 +00003918 case UNICAST_ADDR: {
3919 struct inet6_ifaddr *ifa;
3920
Eric Dumazet234b27c2009-11-12 04:11:50 +00003921 /* unicast address incl. temp addr */
stephen hemminger502a2ff2010-03-17 20:31:13 +00003922 list_for_each_entry(ifa, &idev->addr_list, if_list) {
3923 if (++ip_idx < s_ip_idx)
Eric Dumazet234b27c2009-11-12 04:11:50 +00003924 continue;
3925 err = inet6_fill_ifaddr(skb, ifa,
Eric W. Biederman15e47302012-09-07 20:12:54 +00003926 NETLINK_CB(cb->skb).portid,
Eric Dumazet234b27c2009-11-12 04:11:50 +00003927 cb->nlh->nlmsg_seq,
3928 RTM_NEWADDR,
3929 NLM_F_MULTI);
3930 if (err <= 0)
3931 break;
Nicolas Dichtel63998ac2013-03-22 06:28:43 +00003932 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933 }
Eric Dumazet234b27c2009-11-12 04:11:50 +00003934 break;
stephen hemminger502a2ff2010-03-17 20:31:13 +00003935 }
Eric Dumazet234b27c2009-11-12 04:11:50 +00003936 case MULTICAST_ADDR:
3937 /* multicast address */
3938 for (ifmca = idev->mc_list; ifmca;
3939 ifmca = ifmca->next, ip_idx++) {
3940 if (ip_idx < s_ip_idx)
3941 continue;
3942 err = inet6_fill_ifmcaddr(skb, ifmca,
Eric W. Biederman15e47302012-09-07 20:12:54 +00003943 NETLINK_CB(cb->skb).portid,
Eric Dumazet234b27c2009-11-12 04:11:50 +00003944 cb->nlh->nlmsg_seq,
3945 RTM_GETMULTICAST,
3946 NLM_F_MULTI);
3947 if (err <= 0)
3948 break;
3949 }
3950 break;
3951 case ANYCAST_ADDR:
3952 /* anycast address */
3953 for (ifaca = idev->ac_list; ifaca;
3954 ifaca = ifaca->aca_next, ip_idx++) {
3955 if (ip_idx < s_ip_idx)
3956 continue;
3957 err = inet6_fill_ifacaddr(skb, ifaca,
Eric W. Biederman15e47302012-09-07 20:12:54 +00003958 NETLINK_CB(cb->skb).portid,
Eric Dumazet234b27c2009-11-12 04:11:50 +00003959 cb->nlh->nlmsg_seq,
3960 RTM_GETANYCAST,
3961 NLM_F_MULTI);
3962 if (err <= 0)
3963 break;
3964 }
3965 break;
3966 default:
3967 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968 }
Eric Dumazet234b27c2009-11-12 04:11:50 +00003969 read_unlock_bh(&idev->lock);
3970 *p_ip_idx = ip_idx;
3971 return err;
3972}
3973
3974static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
3975 enum addr_type_t type)
3976{
3977 struct net *net = sock_net(skb->sk);
3978 int h, s_h;
3979 int idx, ip_idx;
3980 int s_idx, s_ip_idx;
3981 struct net_device *dev;
3982 struct inet6_dev *idev;
3983 struct hlist_head *head;
Eric Dumazet234b27c2009-11-12 04:11:50 +00003984
3985 s_h = cb->args[0];
3986 s_idx = idx = cb->args[1];
3987 s_ip_idx = ip_idx = cb->args[2];
3988
3989 rcu_read_lock();
Nicolas Dichtel63998ac2013-03-22 06:28:43 +00003990 cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^ net->dev_base_seq;
Eric Dumazet234b27c2009-11-12 04:11:50 +00003991 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
3992 idx = 0;
3993 head = &net->dev_index_head[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -08003994 hlist_for_each_entry_rcu(dev, head, index_hlist) {
Eric Dumazet234b27c2009-11-12 04:11:50 +00003995 if (idx < s_idx)
3996 goto cont;
Patrick McHardy4b97efd2010-03-26 20:27:49 -07003997 if (h > s_h || idx > s_idx)
Eric Dumazet234b27c2009-11-12 04:11:50 +00003998 s_ip_idx = 0;
3999 ip_idx = 0;
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004000 idev = __in6_dev_get(dev);
4001 if (!idev)
Eric Dumazet234b27c2009-11-12 04:11:50 +00004002 goto cont;
4003
4004 if (in6_dump_addrs(idev, skb, cb, type,
4005 s_ip_idx, &ip_idx) <= 0)
4006 goto done;
4007cont:
4008 idx++;
4009 }
4010 }
4011done:
4012 rcu_read_unlock();
4013 cb->args[0] = h;
4014 cb->args[1] = idx;
4015 cb->args[2] = ip_idx;
4016
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017 return skb->len;
4018}
4019
4020static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
4021{
4022 enum addr_type_t type = UNICAST_ADDR;
Denis V. Lunevb8542722007-12-01 00:21:31 +11004023
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024 return inet6_dump_addr(skb, cb, type);
4025}
4026
4027static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
4028{
4029 enum addr_type_t type = MULTICAST_ADDR;
Denis V. Lunevb8542722007-12-01 00:21:31 +11004030
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031 return inet6_dump_addr(skb, cb, type);
4032}
4033
4034
4035static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
4036{
4037 enum addr_type_t type = ANYCAST_ADDR;
Denis V. Lunevb8542722007-12-01 00:21:31 +11004038
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039 return inet6_dump_addr(skb, cb, type);
4040}
4041
Thomas Graf661d2962013-03-21 07:45:29 +00004042static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr *nlh)
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004043{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09004044 struct net *net = sock_net(in_skb->sk);
Thomas Graf1b29fc22006-09-18 00:10:50 -07004045 struct ifaddrmsg *ifm;
4046 struct nlattr *tb[IFA_MAX+1];
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004047 struct in6_addr *addr = NULL;
4048 struct net_device *dev = NULL;
4049 struct inet6_ifaddr *ifa;
4050 struct sk_buff *skb;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004051 int err;
4052
Thomas Graf1b29fc22006-09-18 00:10:50 -07004053 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
4054 if (err < 0)
4055 goto errout;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004056
Thomas Graf1b29fc22006-09-18 00:10:50 -07004057 addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
4058 if (addr == NULL) {
4059 err = -EINVAL;
4060 goto errout;
4061 }
4062
4063 ifm = nlmsg_data(nlh);
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004064 if (ifm->ifa_index)
Benjamin Thery6fda7352008-03-05 10:47:47 -08004065 dev = __dev_get_by_index(net, ifm->ifa_index);
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004066
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004067 ifa = ipv6_get_ifaddr(net, addr, dev, 1);
4068 if (!ifa) {
Thomas Graf1b29fc22006-09-18 00:10:50 -07004069 err = -EADDRNOTAVAIL;
4070 goto errout;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004071 }
4072
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004073 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL);
4074 if (!skb) {
Thomas Graf1b29fc22006-09-18 00:10:50 -07004075 err = -ENOBUFS;
4076 goto errout_ifa;
4077 }
4078
Eric W. Biederman15e47302012-09-07 20:12:54 +00004079 err = inet6_fill_ifaddr(skb, ifa, NETLINK_CB(in_skb).portid,
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004080 nlh->nlmsg_seq, RTM_NEWADDR, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08004081 if (err < 0) {
4082 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
4083 WARN_ON(err == -EMSGSIZE);
4084 kfree_skb(skb);
4085 goto errout_ifa;
4086 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00004087 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
Thomas Graf1b29fc22006-09-18 00:10:50 -07004088errout_ifa:
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004089 in6_ifa_put(ifa);
Thomas Graf1b29fc22006-09-18 00:10:50 -07004090errout:
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004091 return err;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004092}
4093
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
4095{
4096 struct sk_buff *skb;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09004097 struct net *net = dev_net(ifa->idev->dev);
Thomas Graf5d620262006-08-15 00:35:02 -07004098 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099
Thomas Graf0ab68032006-09-18 00:12:35 -07004100 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC);
Thomas Graf5d620262006-08-15 00:35:02 -07004101 if (skb == NULL)
4102 goto errout;
4103
4104 err = inet6_fill_ifaddr(skb, ifa, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08004105 if (err < 0) {
4106 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
4107 WARN_ON(err == -EMSGSIZE);
4108 kfree_skb(skb);
4109 goto errout;
4110 }
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08004111 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
4112 return;
Thomas Graf5d620262006-08-15 00:35:02 -07004113errout:
4114 if (err < 0)
Benjamin Thery6fda7352008-03-05 10:47:47 -08004115 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116}
4117
Dave Jonesb6f99a22007-03-22 12:27:49 -07004118static inline void ipv6_store_devconf(struct ipv6_devconf *cnf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119 __s32 *array, int bytes)
4120{
Thomas Graf04561c12006-11-14 19:53:58 -08004121 BUG_ON(bytes < (DEVCONF_MAX * 4));
4122
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123 memset(array, 0, bytes);
4124 array[DEVCONF_FORWARDING] = cnf->forwarding;
4125 array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
4126 array[DEVCONF_MTU6] = cnf->mtu6;
4127 array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
4128 array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
4129 array[DEVCONF_AUTOCONF] = cnf->autoconf;
4130 array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
4131 array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
Thomas Graf93908d12010-11-17 01:44:24 +00004132 array[DEVCONF_RTR_SOLICIT_INTERVAL] =
4133 jiffies_to_msecs(cnf->rtr_solicit_interval);
4134 array[DEVCONF_RTR_SOLICIT_DELAY] =
4135 jiffies_to_msecs(cnf->rtr_solicit_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136 array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
4137#ifdef CONFIG_IPV6_PRIVACY
4138 array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
4139 array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
4140 array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
4141 array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
4142 array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
4143#endif
4144 array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -08004145 array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr;
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -08004146 array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo;
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08004147#ifdef CONFIG_IPV6_ROUTER_PREF
4148 array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref;
Thomas Graf93908d12010-11-17 01:44:24 +00004149 array[DEVCONF_RTR_PROBE_INTERVAL] =
4150 jiffies_to_msecs(cnf->rtr_probe_interval);
Neil Hormanfa03ef32007-01-30 14:30:10 -08004151#ifdef CONFIG_IPV6_ROUTE_INFO
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -08004152 array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen;
4153#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08004154#endif
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -07004155 array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp;
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -07004156 array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route;
Neil Horman95c385b2007-04-25 17:08:10 -07004157#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
4158 array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad;
4159#endif
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09004160#ifdef CONFIG_IPV6_MROUTE
4161 array[DEVCONF_MC_FORWARDING] = cnf->mc_forwarding;
4162#endif
YOSHIFUJI Hideaki778d80b2008-06-28 14:17:11 +09004163 array[DEVCONF_DISABLE_IPV6] = cnf->disable_ipv6;
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09004164 array[DEVCONF_ACCEPT_DAD] = cnf->accept_dad;
Cosmin Ratiuc3faca02009-10-09 03:11:14 +00004165 array[DEVCONF_FORCE_TLLAO] = cnf->force_tllao;
Hannes Frederic Sowa5cb04432012-11-06 16:46:20 +00004166 array[DEVCONF_NDISC_NOTIFY] = cnf->ndisc_notify;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167}
4168
Thomas Grafb382b192010-11-16 04:33:57 +00004169static inline size_t inet6_ifla6_size(void)
4170{
4171 return nla_total_size(4) /* IFLA_INET6_FLAGS */
4172 + nla_total_size(sizeof(struct ifla_cacheinfo))
4173 + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */
4174 + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */
Daniel Borkmannf53adae2013-04-08 04:01:30 +00004175 + nla_total_size(ICMP6_MIB_MAX * 8) /* IFLA_INET6_ICMP6STATS */
4176 + nla_total_size(sizeof(struct in6_addr)); /* IFLA_INET6_TOKEN */
Thomas Grafb382b192010-11-16 04:33:57 +00004177}
4178
Thomas Graf339bf982006-11-10 14:10:15 -08004179static inline size_t inet6_if_nlmsg_size(void)
4180{
4181 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
4182 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
4183 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
4184 + nla_total_size(4) /* IFLA_MTU */
4185 + nla_total_size(4) /* IFLA_LINK */
Thomas Grafb382b192010-11-16 04:33:57 +00004186 + nla_total_size(inet6_ifla6_size()); /* IFLA_PROTINFO */
Thomas Graf339bf982006-11-10 14:10:15 -08004187}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07004188
Eric Dumazetbe281e52011-05-19 01:14:23 +00004189static inline void __snmp6_fill_statsdev(u64 *stats, atomic_long_t *mib,
Tejun Heo7d720c32010-02-16 15:20:26 +00004190 int items, int bytes)
Herbert Xu7f7d9a62007-04-24 21:54:09 -07004191{
4192 int i;
4193 int pad = bytes - sizeof(u64) * items;
4194 BUG_ON(pad < 0);
4195
4196 /* Use put_unaligned() because stats may not be aligned for u64. */
4197 put_unaligned(items, &stats[0]);
4198 for (i = 1; i < items; i++)
Eric Dumazetbe281e52011-05-19 01:14:23 +00004199 put_unaligned(atomic_long_read(&mib[i]), &stats[i]);
Herbert Xu7f7d9a62007-04-24 21:54:09 -07004200
4201 memset(&stats[items], 0, pad);
4202}
4203
Eric Dumazet4ce3c182010-06-30 13:31:19 -07004204static inline void __snmp6_fill_stats64(u64 *stats, void __percpu **mib,
4205 int items, int bytes, size_t syncpoff)
4206{
4207 int i;
4208 int pad = bytes - sizeof(u64) * items;
4209 BUG_ON(pad < 0);
4210
4211 /* Use put_unaligned() because stats may not be aligned for u64. */
4212 put_unaligned(items, &stats[0]);
4213 for (i = 1; i < items; i++)
4214 put_unaligned(snmp_fold_field64(mib, i, syncpoff), &stats[i]);
4215
4216 memset(&stats[items], 0, pad);
4217}
4218
Herbert Xu7f7d9a62007-04-24 21:54:09 -07004219static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype,
4220 int bytes)
4221{
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004222 switch (attrtype) {
Herbert Xu7f7d9a62007-04-24 21:54:09 -07004223 case IFLA_INET6_STATS:
Eric Dumazet4ce3c182010-06-30 13:31:19 -07004224 __snmp6_fill_stats64(stats, (void __percpu **)idev->stats.ipv6,
4225 IPSTATS_MIB_MAX, bytes, offsetof(struct ipstats_mib, syncp));
Herbert Xu7f7d9a62007-04-24 21:54:09 -07004226 break;
4227 case IFLA_INET6_ICMP6STATS:
Eric Dumazetbe281e52011-05-19 01:14:23 +00004228 __snmp6_fill_statsdev(stats, idev->stats.icmpv6dev->mibs, ICMP6_MIB_MAX, bytes);
Herbert Xu7f7d9a62007-04-24 21:54:09 -07004229 break;
4230 }
4231}
4232
Thomas Grafb382b192010-11-16 04:33:57 +00004233static int inet6_fill_ifla6_attrs(struct sk_buff *skb, struct inet6_dev *idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234{
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07004235 struct nlattr *nla;
Thomas Graf04561c12006-11-14 19:53:58 -08004236 struct ifla_cacheinfo ci;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237
David S. Millerc78679e2012-04-01 20:27:33 -04004238 if (nla_put_u32(skb, IFLA_INET6_FLAGS, idev->if_flags))
4239 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240 ci.max_reasm_len = IPV6_MAXPLEN;
David S. Miller24912422010-11-19 13:13:47 -08004241 ci.tstamp = cstamp_delta(idev->tstamp);
4242 ci.reachable_time = jiffies_to_msecs(idev->nd_parms->reachable_time);
4243 ci.retrans_time = jiffies_to_msecs(idev->nd_parms->retrans_time);
David S. Millerc78679e2012-04-01 20:27:33 -04004244 if (nla_put(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci))
4245 goto nla_put_failure;
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07004246 nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32));
4247 if (nla == NULL)
Thomas Graf04561c12006-11-14 19:53:58 -08004248 goto nla_put_failure;
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07004249 ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07004251 /* XXX - MC not implemented */
4252
4253 nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64));
4254 if (nla == NULL)
4255 goto nla_put_failure;
4256 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla));
4257
4258 nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64));
4259 if (nla == NULL)
4260 goto nla_put_failure;
4261 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262
Daniel Borkmannf53adae2013-04-08 04:01:30 +00004263 nla = nla_reserve(skb, IFLA_INET6_TOKEN, sizeof(struct in6_addr));
4264 if (nla == NULL)
4265 goto nla_put_failure;
4266 read_lock_bh(&idev->lock);
4267 memcpy(nla_data(nla), idev->token.s6_addr, nla_len(nla));
4268 read_unlock_bh(&idev->lock);
4269
Thomas Grafb382b192010-11-16 04:33:57 +00004270 return 0;
4271
4272nla_put_failure:
4273 return -EMSGSIZE;
4274}
4275
4276static size_t inet6_get_link_af_size(const struct net_device *dev)
4277{
4278 if (!__in6_dev_get(dev))
4279 return 0;
4280
4281 return inet6_ifla6_size();
4282}
4283
4284static int inet6_fill_link_af(struct sk_buff *skb, const struct net_device *dev)
4285{
4286 struct inet6_dev *idev = __in6_dev_get(dev);
4287
4288 if (!idev)
4289 return -ENODATA;
4290
4291 if (inet6_fill_ifla6_attrs(skb, idev) < 0)
4292 return -EMSGSIZE;
4293
4294 return 0;
4295}
4296
Daniel Borkmannf53adae2013-04-08 04:01:30 +00004297static int inet6_set_iftoken(struct inet6_dev *idev, struct in6_addr *token)
4298{
4299 struct in6_addr ll_addr;
4300 struct inet6_ifaddr *ifp;
4301 struct net_device *dev = idev->dev;
4302
4303 if (token == NULL)
4304 return -EINVAL;
4305 if (ipv6_addr_any(token))
4306 return -EINVAL;
4307 if (dev->flags & (IFF_LOOPBACK | IFF_NOARP))
4308 return -EINVAL;
4309 if (idev->dead || !(idev->if_flags & IF_READY))
4310 return -EINVAL;
4311 if (!ipv6_accept_ra(idev))
4312 return -EINVAL;
4313 if (idev->cnf.rtr_solicits <= 0)
4314 return -EINVAL;
4315
4316 write_lock_bh(&idev->lock);
4317
4318 BUILD_BUG_ON(sizeof(token->s6_addr) != 16);
4319 memcpy(idev->token.s6_addr + 8, token->s6_addr + 8, 8);
4320
4321 write_unlock_bh(&idev->lock);
4322
4323 ipv6_get_lladdr(dev, &ll_addr, IFA_F_TENTATIVE | IFA_F_OPTIMISTIC);
4324 ndisc_send_rs(dev, &ll_addr, &in6addr_linklocal_allrouters);
4325
4326 write_lock_bh(&idev->lock);
4327 idev->if_flags |= IF_RS_SENT;
4328
4329 /* Well, that's kinda nasty ... */
4330 list_for_each_entry(ifp, &idev->addr_list, if_list) {
4331 spin_lock(&ifp->lock);
4332 if (ipv6_addr_src_scope(&ifp->addr) ==
4333 IPV6_ADDR_SCOPE_GLOBAL) {
4334 ifp->valid_lft = 0;
4335 ifp->prefered_lft = 0;
4336 }
4337 spin_unlock(&ifp->lock);
4338 }
4339
4340 write_unlock_bh(&idev->lock);
4341 return 0;
4342}
4343
4344static int inet6_set_link_af(struct net_device *dev, const struct nlattr *nla)
4345{
4346 int err = -EINVAL;
4347 struct inet6_dev *idev = __in6_dev_get(dev);
4348 struct nlattr *tb[IFLA_INET6_MAX + 1];
4349
4350 if (!idev)
4351 return -EAFNOSUPPORT;
4352
4353 if (nla_parse_nested(tb, IFLA_INET6_MAX, nla, NULL) < 0)
4354 BUG();
4355
4356 if (tb[IFLA_INET6_TOKEN])
4357 err = inet6_set_iftoken(idev, nla_data(tb[IFLA_INET6_TOKEN]));
4358
4359 return err;
4360}
4361
Thomas Grafb382b192010-11-16 04:33:57 +00004362static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004363 u32 portid, u32 seq, int event, unsigned int flags)
Thomas Grafb382b192010-11-16 04:33:57 +00004364{
4365 struct net_device *dev = idev->dev;
4366 struct ifinfomsg *hdr;
4367 struct nlmsghdr *nlh;
4368 void *protoinfo;
4369
Eric W. Biederman15e47302012-09-07 20:12:54 +00004370 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*hdr), flags);
Thomas Grafb382b192010-11-16 04:33:57 +00004371 if (nlh == NULL)
4372 return -EMSGSIZE;
4373
4374 hdr = nlmsg_data(nlh);
4375 hdr->ifi_family = AF_INET6;
4376 hdr->__ifi_pad = 0;
4377 hdr->ifi_type = dev->type;
4378 hdr->ifi_index = dev->ifindex;
4379 hdr->ifi_flags = dev_get_flags(dev);
4380 hdr->ifi_change = 0;
4381
David S. Millerc78679e2012-04-01 20:27:33 -04004382 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
4383 (dev->addr_len &&
4384 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
4385 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
4386 (dev->ifindex != dev->iflink &&
4387 nla_put_u32(skb, IFLA_LINK, dev->iflink)))
4388 goto nla_put_failure;
Thomas Grafb382b192010-11-16 04:33:57 +00004389 protoinfo = nla_nest_start(skb, IFLA_PROTINFO);
4390 if (protoinfo == NULL)
4391 goto nla_put_failure;
4392
4393 if (inet6_fill_ifla6_attrs(skb, idev) < 0)
4394 goto nla_put_failure;
4395
Thomas Graf04561c12006-11-14 19:53:58 -08004396 nla_nest_end(skb, protoinfo);
4397 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398
Thomas Graf04561c12006-11-14 19:53:58 -08004399nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08004400 nlmsg_cancel(skb, nlh);
4401 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004402}
4403
4404static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
4405{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09004406 struct net *net = sock_net(skb->sk);
Eric Dumazet84d26972009-11-09 12:11:28 +00004407 int h, s_h;
David S. Miller434a8a52009-11-11 18:53:00 -08004408 int idx = 0, s_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409 struct net_device *dev;
4410 struct inet6_dev *idev;
Eric Dumazet84d26972009-11-09 12:11:28 +00004411 struct hlist_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004412
Eric Dumazet84d26972009-11-09 12:11:28 +00004413 s_h = cb->args[0];
4414 s_idx = cb->args[1];
4415
4416 rcu_read_lock();
4417 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4418 idx = 0;
4419 head = &net->dev_index_head[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -08004420 hlist_for_each_entry_rcu(dev, head, index_hlist) {
Eric Dumazet84d26972009-11-09 12:11:28 +00004421 if (idx < s_idx)
4422 goto cont;
4423 idev = __in6_dev_get(dev);
4424 if (!idev)
4425 goto cont;
4426 if (inet6_fill_ifinfo(skb, idev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004427 NETLINK_CB(cb->skb).portid,
Eric Dumazet84d26972009-11-09 12:11:28 +00004428 cb->nlh->nlmsg_seq,
4429 RTM_NEWLINK, NLM_F_MULTI) <= 0)
4430 goto out;
Pavel Emelianov7562f872007-05-03 15:13:45 -07004431cont:
Eric Dumazet84d26972009-11-09 12:11:28 +00004432 idx++;
4433 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434 }
Eric Dumazet84d26972009-11-09 12:11:28 +00004435out:
4436 rcu_read_unlock();
4437 cb->args[1] = idx;
4438 cb->args[0] = h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439
4440 return skb->len;
4441}
4442
4443void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
4444{
4445 struct sk_buff *skb;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09004446 struct net *net = dev_net(idev->dev);
Thomas Graf8d7a76c2006-08-15 00:35:47 -07004447 int err = -ENOBUFS;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004448
Thomas Graf339bf982006-11-10 14:10:15 -08004449 skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC);
Thomas Graf8d7a76c2006-08-15 00:35:47 -07004450 if (skb == NULL)
4451 goto errout;
4452
4453 err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08004454 if (err < 0) {
4455 /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */
4456 WARN_ON(err == -EMSGSIZE);
4457 kfree_skb(skb);
4458 goto errout;
4459 }
Nicolas Dichtel5f75a102010-12-07 23:38:31 +00004460 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFINFO, NULL, GFP_ATOMIC);
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08004461 return;
Thomas Graf8d7a76c2006-08-15 00:35:47 -07004462errout:
4463 if (err < 0)
Nicolas Dichtel5f75a102010-12-07 23:38:31 +00004464 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFINFO, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465}
4466
Thomas Graf339bf982006-11-10 14:10:15 -08004467static inline size_t inet6_prefix_nlmsg_size(void)
4468{
4469 return NLMSG_ALIGN(sizeof(struct prefixmsg))
4470 + nla_total_size(sizeof(struct in6_addr))
4471 + nla_total_size(sizeof(struct prefix_cacheinfo));
4472}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07004473
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004475 struct prefix_info *pinfo, u32 portid, u32 seq,
Thomas Graf6051e2f2006-11-14 19:54:19 -08004476 int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477{
Thomas Graf6051e2f2006-11-14 19:54:19 -08004478 struct prefixmsg *pmsg;
4479 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480 struct prefix_cacheinfo ci;
4481
Eric W. Biederman15e47302012-09-07 20:12:54 +00004482 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*pmsg), flags);
Thomas Graf6051e2f2006-11-14 19:54:19 -08004483 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08004484 return -EMSGSIZE;
Thomas Graf6051e2f2006-11-14 19:54:19 -08004485
4486 pmsg = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487 pmsg->prefix_family = AF_INET6;
Patrick McHardy8a470772005-06-28 12:56:45 -07004488 pmsg->prefix_pad1 = 0;
4489 pmsg->prefix_pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490 pmsg->prefix_ifindex = idev->dev->ifindex;
4491 pmsg->prefix_len = pinfo->prefix_len;
4492 pmsg->prefix_type = pinfo->type;
Patrick McHardy8a470772005-06-28 12:56:45 -07004493 pmsg->prefix_pad3 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494 pmsg->prefix_flags = 0;
4495 if (pinfo->onlink)
4496 pmsg->prefix_flags |= IF_PREFIX_ONLINK;
4497 if (pinfo->autoconf)
4498 pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
4499
David S. Millerc78679e2012-04-01 20:27:33 -04004500 if (nla_put(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix))
4501 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502 ci.preferred_time = ntohl(pinfo->prefered);
4503 ci.valid_time = ntohl(pinfo->valid);
David S. Millerc78679e2012-04-01 20:27:33 -04004504 if (nla_put(skb, PREFIX_CACHEINFO, sizeof(ci), &ci))
4505 goto nla_put_failure;
Thomas Graf6051e2f2006-11-14 19:54:19 -08004506 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507
Thomas Graf6051e2f2006-11-14 19:54:19 -08004508nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08004509 nlmsg_cancel(skb, nlh);
4510 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511}
4512
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004513static void inet6_prefix_notify(int event, struct inet6_dev *idev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514 struct prefix_info *pinfo)
4515{
4516 struct sk_buff *skb;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09004517 struct net *net = dev_net(idev->dev);
Thomas Graf8c384bfa2006-08-15 00:36:07 -07004518 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519
Thomas Graf339bf982006-11-10 14:10:15 -08004520 skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC);
Thomas Graf8c384bfa2006-08-15 00:36:07 -07004521 if (skb == NULL)
4522 goto errout;
4523
4524 err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08004525 if (err < 0) {
4526 /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */
4527 WARN_ON(err == -EMSGSIZE);
4528 kfree_skb(skb);
4529 goto errout;
4530 }
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08004531 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC);
4532 return;
Thomas Graf8c384bfa2006-08-15 00:36:07 -07004533errout:
4534 if (err < 0)
Benjamin Thery6fda7352008-03-05 10:47:47 -08004535 rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004536}
4537
Linus Torvalds1da177e2005-04-16 15:20:36 -07004538static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
4539{
Nicolas Dichtel63998ac2013-03-22 06:28:43 +00004540 struct net *net = dev_net(ifp->idev->dev);
4541
Linus Torvalds1da177e2005-04-16 15:20:36 -07004542 inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
4543
4544 switch (event) {
4545 case RTM_NEWADDR:
Neil Horman95c385b2007-04-25 17:08:10 -07004546 /*
4547 * If the address was optimistic
4548 * we inserted the route at the start of
4549 * our DAD process, so we don't need
4550 * to do it again
4551 */
4552 if (!(ifp->rt->rt6i_node))
4553 ip6_ins_rt(ifp->rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004554 if (ifp->idev->cnf.forwarding)
4555 addrconf_join_anycast(ifp);
4556 break;
4557 case RTM_DELADDR:
4558 if (ifp->idev->cnf.forwarding)
4559 addrconf_leave_anycast(ifp);
4560 addrconf_leave_solict(ifp->idev, &ifp->addr);
Changli Gaod8d1f302010-06-10 23:31:35 -07004561 dst_hold(&ifp->rt->dst);
stephen hemminger93fa1592010-04-12 05:41:31 +00004562
David S. Miller73a8bd72011-01-23 23:27:15 -08004563 if (ip6_del_rt(ifp->rt))
Changli Gaod8d1f302010-06-10 23:31:35 -07004564 dst_free(&ifp->rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565 break;
4566 }
Nicolas Dichtel63998ac2013-03-22 06:28:43 +00004567 atomic_inc(&net->ipv6.dev_addr_genid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004568}
4569
4570static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
4571{
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07004572 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004573 if (likely(ifp->idev->dead == 0))
4574 __ipv6_ifa_notify(event, ifp);
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07004575 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576}
4577
4578#ifdef CONFIG_SYSCTL
4579
4580static
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07004581int addrconf_sysctl_forward(ctl_table *ctl, int write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582 void __user *buffer, size_t *lenp, loff_t *ppos)
4583{
4584 int *valp = ctl->data;
4585 int val = *valp;
Eric W. Biederman88af1822010-02-19 13:22:59 +00004586 loff_t pos = *ppos;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00004587 ctl_table lctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004588 int ret;
4589
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00004590 /*
4591 * ctl->data points to idev->cnf.forwarding, we should
4592 * not modify it until we get the rtnl lock.
4593 */
4594 lctl = *ctl;
4595 lctl.data = &val;
4596
4597 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -08004599 if (write)
Stephen Hemmingerb325fdd2009-02-26 06:55:31 +00004600 ret = addrconf_fixup_forwarding(ctl, valp, val);
Eric W. Biederman88af1822010-02-19 13:22:59 +00004601 if (ret)
4602 *ppos = pos;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004603 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604}
4605
Brian Haley56d417b2009-06-01 03:07:33 -07004606static void dev_disable_change(struct inet6_dev *idev)
4607{
4608 if (!idev || !idev->dev)
4609 return;
4610
4611 if (idev->cnf.disable_ipv6)
4612 addrconf_notify(NULL, NETDEV_DOWN, idev->dev);
4613 else
4614 addrconf_notify(NULL, NETDEV_UP, idev->dev);
4615}
4616
4617static void addrconf_disable_change(struct net *net, __s32 newf)
4618{
4619 struct net_device *dev;
4620 struct inet6_dev *idev;
4621
Eric Dumazetc6d14c82009-11-04 05:43:23 -08004622 rcu_read_lock();
4623 for_each_netdev_rcu(net, dev) {
Brian Haley56d417b2009-06-01 03:07:33 -07004624 idev = __in6_dev_get(dev);
4625 if (idev) {
4626 int changed = (!idev->cnf.disable_ipv6) ^ (!newf);
4627 idev->cnf.disable_ipv6 = newf;
4628 if (changed)
4629 dev_disable_change(idev);
4630 }
Brian Haley56d417b2009-06-01 03:07:33 -07004631 }
Eric Dumazetc6d14c82009-11-04 05:43:23 -08004632 rcu_read_unlock();
Brian Haley56d417b2009-06-01 03:07:33 -07004633}
4634
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00004635static int addrconf_disable_ipv6(struct ctl_table *table, int *p, int newf)
Brian Haley56d417b2009-06-01 03:07:33 -07004636{
4637 struct net *net;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00004638 int old;
4639
4640 if (!rtnl_trylock())
4641 return restart_syscall();
Brian Haley56d417b2009-06-01 03:07:33 -07004642
4643 net = (struct net *)table->extra2;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00004644 old = *p;
4645 *p = newf;
Brian Haley56d417b2009-06-01 03:07:33 -07004646
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00004647 if (p == &net->ipv6.devconf_dflt->disable_ipv6) {
4648 rtnl_unlock();
Brian Haley56d417b2009-06-01 03:07:33 -07004649 return 0;
Eric W. Biederman88af1822010-02-19 13:22:59 +00004650 }
Brian Haley56d417b2009-06-01 03:07:33 -07004651
4652 if (p == &net->ipv6.devconf_all->disable_ipv6) {
Brian Haley56d417b2009-06-01 03:07:33 -07004653 net->ipv6.devconf_dflt->disable_ipv6 = newf;
4654 addrconf_disable_change(net, newf);
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00004655 } else if ((!newf) ^ (!old))
Brian Haley56d417b2009-06-01 03:07:33 -07004656 dev_disable_change((struct inet6_dev *)table->extra1);
4657
4658 rtnl_unlock();
4659 return 0;
4660}
4661
4662static
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07004663int addrconf_sysctl_disable(ctl_table *ctl, int write,
Brian Haley56d417b2009-06-01 03:07:33 -07004664 void __user *buffer, size_t *lenp, loff_t *ppos)
4665{
4666 int *valp = ctl->data;
4667 int val = *valp;
Eric W. Biederman88af1822010-02-19 13:22:59 +00004668 loff_t pos = *ppos;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00004669 ctl_table lctl;
Brian Haley56d417b2009-06-01 03:07:33 -07004670 int ret;
4671
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00004672 /*
4673 * ctl->data points to idev->cnf.disable_ipv6, we should
4674 * not modify it until we get the rtnl lock.
4675 */
4676 lctl = *ctl;
4677 lctl.data = &val;
4678
4679 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
Brian Haley56d417b2009-06-01 03:07:33 -07004680
4681 if (write)
4682 ret = addrconf_disable_ipv6(ctl, valp, val);
Eric W. Biederman88af1822010-02-19 13:22:59 +00004683 if (ret)
4684 *ppos = pos;
Brian Haley56d417b2009-06-01 03:07:33 -07004685 return ret;
4686}
4687
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688static struct addrconf_sysctl_table
4689{
4690 struct ctl_table_header *sysctl_header;
YOSHIFUJI Hideakif6a07b22008-03-25 00:25:11 +09004691 ctl_table addrconf_vars[DEVCONF_MAX+1];
Brian Haleyab32ea52006-09-22 14:15:41 -07004692} addrconf_sysctl __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004693 .sysctl_header = NULL,
4694 .addrconf_vars = {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004695 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004696 .procname = "forwarding",
4697 .data = &ipv6_devconf.forwarding,
4698 .maxlen = sizeof(int),
4699 .mode = 0644,
4700 .proc_handler = addrconf_sysctl_forward,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004701 },
4702 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004703 .procname = "hop_limit",
4704 .data = &ipv6_devconf.hop_limit,
4705 .maxlen = sizeof(int),
4706 .mode = 0644,
4707 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708 },
4709 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004710 .procname = "mtu",
4711 .data = &ipv6_devconf.mtu6,
4712 .maxlen = sizeof(int),
4713 .mode = 0644,
4714 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715 },
4716 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004717 .procname = "accept_ra",
4718 .data = &ipv6_devconf.accept_ra,
4719 .maxlen = sizeof(int),
4720 .mode = 0644,
4721 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722 },
4723 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004724 .procname = "accept_redirects",
4725 .data = &ipv6_devconf.accept_redirects,
4726 .maxlen = sizeof(int),
4727 .mode = 0644,
4728 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004729 },
4730 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004731 .procname = "autoconf",
4732 .data = &ipv6_devconf.autoconf,
4733 .maxlen = sizeof(int),
4734 .mode = 0644,
4735 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736 },
4737 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004738 .procname = "dad_transmits",
4739 .data = &ipv6_devconf.dad_transmits,
4740 .maxlen = sizeof(int),
4741 .mode = 0644,
4742 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004743 },
4744 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004745 .procname = "router_solicitations",
4746 .data = &ipv6_devconf.rtr_solicits,
4747 .maxlen = sizeof(int),
4748 .mode = 0644,
4749 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004750 },
4751 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004752 .procname = "router_solicitation_interval",
4753 .data = &ipv6_devconf.rtr_solicit_interval,
4754 .maxlen = sizeof(int),
4755 .mode = 0644,
4756 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004757 },
4758 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004759 .procname = "router_solicitation_delay",
4760 .data = &ipv6_devconf.rtr_solicit_delay,
4761 .maxlen = sizeof(int),
4762 .mode = 0644,
4763 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764 },
4765 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004766 .procname = "force_mld_version",
4767 .data = &ipv6_devconf.force_mld_version,
4768 .maxlen = sizeof(int),
4769 .mode = 0644,
4770 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771 },
4772#ifdef CONFIG_IPV6_PRIVACY
4773 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004774 .procname = "use_tempaddr",
4775 .data = &ipv6_devconf.use_tempaddr,
4776 .maxlen = sizeof(int),
4777 .mode = 0644,
4778 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779 },
4780 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004781 .procname = "temp_valid_lft",
4782 .data = &ipv6_devconf.temp_valid_lft,
4783 .maxlen = sizeof(int),
4784 .mode = 0644,
4785 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786 },
4787 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004788 .procname = "temp_prefered_lft",
4789 .data = &ipv6_devconf.temp_prefered_lft,
4790 .maxlen = sizeof(int),
4791 .mode = 0644,
4792 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004793 },
4794 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004795 .procname = "regen_max_retry",
4796 .data = &ipv6_devconf.regen_max_retry,
4797 .maxlen = sizeof(int),
4798 .mode = 0644,
4799 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800 },
4801 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004802 .procname = "max_desync_factor",
4803 .data = &ipv6_devconf.max_desync_factor,
4804 .maxlen = sizeof(int),
4805 .mode = 0644,
4806 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807 },
4808#endif
4809 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004810 .procname = "max_addresses",
4811 .data = &ipv6_devconf.max_addresses,
4812 .maxlen = sizeof(int),
4813 .mode = 0644,
4814 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815 },
4816 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004817 .procname = "accept_ra_defrtr",
4818 .data = &ipv6_devconf.accept_ra_defrtr,
4819 .maxlen = sizeof(int),
4820 .mode = 0644,
4821 .proc_handler = proc_dointvec,
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -08004822 },
4823 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004824 .procname = "accept_ra_pinfo",
4825 .data = &ipv6_devconf.accept_ra_pinfo,
4826 .maxlen = sizeof(int),
4827 .mode = 0644,
4828 .proc_handler = proc_dointvec,
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -08004829 },
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08004830#ifdef CONFIG_IPV6_ROUTER_PREF
4831 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004832 .procname = "accept_ra_rtr_pref",
4833 .data = &ipv6_devconf.accept_ra_rtr_pref,
4834 .maxlen = sizeof(int),
4835 .mode = 0644,
4836 .proc_handler = proc_dointvec,
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08004837 },
YOSHIFUJI Hideaki52e163562006-03-20 17:05:47 -08004838 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004839 .procname = "router_probe_interval",
4840 .data = &ipv6_devconf.rtr_probe_interval,
4841 .maxlen = sizeof(int),
4842 .mode = 0644,
4843 .proc_handler = proc_dointvec_jiffies,
YOSHIFUJI Hideaki52e163562006-03-20 17:05:47 -08004844 },
Neil Hormanfa03ef32007-01-30 14:30:10 -08004845#ifdef CONFIG_IPV6_ROUTE_INFO
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -08004846 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004847 .procname = "accept_ra_rt_info_max_plen",
4848 .data = &ipv6_devconf.accept_ra_rt_info_max_plen,
4849 .maxlen = sizeof(int),
4850 .mode = 0644,
4851 .proc_handler = proc_dointvec,
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -08004852 },
4853#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08004854#endif
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -08004855 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004856 .procname = "proxy_ndp",
4857 .data = &ipv6_devconf.proxy_ndp,
4858 .maxlen = sizeof(int),
4859 .mode = 0644,
4860 .proc_handler = proc_dointvec,
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -07004861 },
4862 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004863 .procname = "accept_source_route",
4864 .data = &ipv6_devconf.accept_source_route,
4865 .maxlen = sizeof(int),
4866 .mode = 0644,
4867 .proc_handler = proc_dointvec,
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -07004868 },
Neil Horman95c385b2007-04-25 17:08:10 -07004869#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
4870 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004871 .procname = "optimistic_dad",
4872 .data = &ipv6_devconf.optimistic_dad,
4873 .maxlen = sizeof(int),
4874 .mode = 0644,
4875 .proc_handler = proc_dointvec,
Neil Horman95c385b2007-04-25 17:08:10 -07004876
4877 },
4878#endif
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09004879#ifdef CONFIG_IPV6_MROUTE
4880 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004881 .procname = "mc_forwarding",
4882 .data = &ipv6_devconf.mc_forwarding,
4883 .maxlen = sizeof(int),
4884 .mode = 0444,
4885 .proc_handler = proc_dointvec,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09004886 },
4887#endif
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -07004888 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004889 .procname = "disable_ipv6",
4890 .data = &ipv6_devconf.disable_ipv6,
4891 .maxlen = sizeof(int),
4892 .mode = 0644,
4893 .proc_handler = addrconf_sysctl_disable,
YOSHIFUJI Hideaki778d80b2008-06-28 14:17:11 +09004894 },
4895 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004896 .procname = "accept_dad",
4897 .data = &ipv6_devconf.accept_dad,
4898 .maxlen = sizeof(int),
4899 .mode = 0644,
4900 .proc_handler = proc_dointvec,
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09004901 },
4902 {
Octavian Purdilaf7734fd2009-10-02 11:39:15 +00004903 .procname = "force_tllao",
4904 .data = &ipv6_devconf.force_tllao,
4905 .maxlen = sizeof(int),
4906 .mode = 0644,
4907 .proc_handler = proc_dointvec
4908 },
4909 {
Hannes Frederic Sowa5cb04432012-11-06 16:46:20 +00004910 .procname = "ndisc_notify",
4911 .data = &ipv6_devconf.ndisc_notify,
4912 .maxlen = sizeof(int),
4913 .mode = 0644,
4914 .proc_handler = proc_dointvec
4915 },
4916 {
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08004917 /* sentinel */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004918 }
4919 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07004920};
4921
Pavel Emelyanovbff16c22008-01-10 17:42:13 -08004922static int __addrconf_sysctl_register(struct net *net, char *dev_name,
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08004923 struct inet6_dev *idev, struct ipv6_devconf *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924{
4925 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004926 struct addrconf_sysctl_table *t;
Eric W. Biederman6105e292012-04-19 13:41:24 +00004927 char path[sizeof("net/ipv6/conf/") + IFNAMSIZ];
Pavel Emelyanov1dab6222007-12-02 00:59:38 +11004928
Arnaldo Carvalho de Meloaf879cc2006-11-17 12:14:37 -02004929 t = kmemdup(&addrconf_sysctl, sizeof(*t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004930 if (t == NULL)
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11004931 goto out;
4932
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07004933 for (i = 0; t->addrconf_vars[i].data; i++) {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004934 t->addrconf_vars[i].data += (char *)p - (char *)&ipv6_devconf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004935 t->addrconf_vars[i].extra1 = idev; /* embedded; no ref */
Pavel Emelyanovbff16c22008-01-10 17:42:13 -08004936 t->addrconf_vars[i].extra2 = net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004938
Eric W. Biederman6105e292012-04-19 13:41:24 +00004939 snprintf(path, sizeof(path), "net/ipv6/conf/%s", dev_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004940
Eric W. Biederman6105e292012-04-19 13:41:24 +00004941 t->sysctl_header = register_net_sysctl(net, path, t->addrconf_vars);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942 if (t->sysctl_header == NULL)
Eric W. Biederman6105e292012-04-19 13:41:24 +00004943 goto free;
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11004944
4945 p->sysctl = t;
Pavel Emelyanov95897312008-01-10 17:41:45 -08004946 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004947
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11004948free:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949 kfree(t);
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11004950out:
Pavel Emelyanov95897312008-01-10 17:41:45 -08004951 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004952}
4953
Pavel Emelyanov408c4762008-01-10 17:41:21 -08004954static void __addrconf_sysctl_unregister(struct ipv6_devconf *p)
4955{
4956 struct addrconf_sysctl_table *t;
4957
4958 if (p->sysctl == NULL)
4959 return;
4960
4961 t = p->sysctl;
4962 p->sysctl = NULL;
Lucian Adrian Grijincuff538812011-05-01 01:44:01 +00004963 unregister_net_sysctl_table(t->sysctl_header);
Pavel Emelyanov408c4762008-01-10 17:41:21 -08004964 kfree(t);
4965}
4966
Pavel Emelyanovf52295a2007-12-02 00:58:37 +11004967static void addrconf_sysctl_register(struct inet6_dev *idev)
4968{
Eric W. Biederman54716e32010-02-14 03:27:03 +00004969 neigh_sysctl_register(idev->dev, idev->nd_parms, "ipv6",
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08004970 &ndisc_ifinfo_sysctl_change);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09004971 __addrconf_sysctl_register(dev_net(idev->dev), idev->dev->name,
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08004972 idev, &idev->cnf);
Pavel Emelyanovf52295a2007-12-02 00:58:37 +11004973}
4974
Pavel Emelyanov408c4762008-01-10 17:41:21 -08004975static void addrconf_sysctl_unregister(struct inet6_dev *idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004976{
Pavel Emelyanov408c4762008-01-10 17:41:21 -08004977 __addrconf_sysctl_unregister(&idev->cnf);
4978 neigh_sysctl_unregister(idev->nd_parms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004979}
4980
4981
4982#endif
4983
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004984static int __net_init addrconf_init_net(struct net *net)
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08004985{
Hong Zhiguoa79ca222013-03-26 01:52:45 +08004986 int err = -ENOMEM;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08004987 struct ipv6_devconf *all, *dflt;
4988
Hong Zhiguoa79ca222013-03-26 01:52:45 +08004989 all = kmemdup(&ipv6_devconf, sizeof(ipv6_devconf), GFP_KERNEL);
4990 if (all == NULL)
4991 goto err_alloc_all;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08004992
Hong Zhiguoa79ca222013-03-26 01:52:45 +08004993 dflt = kmemdup(&ipv6_devconf_dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL);
4994 if (dflt == NULL)
4995 goto err_alloc_dflt;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08004996
Hong Zhiguoa79ca222013-03-26 01:52:45 +08004997 /* these will be inherited by all namespaces */
4998 dflt->autoconf = ipv6_defaults.autoconf;
4999 dflt->disable_ipv6 = ipv6_defaults.disable_ipv6;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08005000
5001 net->ipv6.devconf_all = all;
5002 net->ipv6.devconf_dflt = dflt;
5003
5004#ifdef CONFIG_SYSCTL
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08005005 err = __addrconf_sysctl_register(net, "all", NULL, all);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08005006 if (err < 0)
5007 goto err_reg_all;
5008
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08005009 err = __addrconf_sysctl_register(net, "default", NULL, dflt);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08005010 if (err < 0)
5011 goto err_reg_dflt;
5012#endif
5013 return 0;
5014
5015#ifdef CONFIG_SYSCTL
5016err_reg_dflt:
5017 __addrconf_sysctl_unregister(all);
5018err_reg_all:
5019 kfree(dflt);
5020#endif
5021err_alloc_dflt:
5022 kfree(all);
5023err_alloc_all:
5024 return err;
5025}
5026
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00005027static void __net_exit addrconf_exit_net(struct net *net)
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08005028{
5029#ifdef CONFIG_SYSCTL
5030 __addrconf_sysctl_unregister(net->ipv6.devconf_dflt);
5031 __addrconf_sysctl_unregister(net->ipv6.devconf_all);
5032#endif
Octavian Purdila09ad9bc2009-11-25 15:14:13 -08005033 if (!net_eq(net, &init_net)) {
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08005034 kfree(net->ipv6.devconf_dflt);
5035 kfree(net->ipv6.devconf_all);
5036 }
5037}
5038
5039static struct pernet_operations addrconf_ops = {
5040 .init = addrconf_init_net,
5041 .exit = addrconf_exit_net,
5042};
5043
Linus Torvalds1da177e2005-04-16 15:20:36 -07005044/*
5045 * Device notifier
5046 */
5047
5048int register_inet6addr_notifier(struct notifier_block *nb)
5049{
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09005050 return atomic_notifier_chain_register(&inet6addr_chain, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005051}
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +09005052EXPORT_SYMBOL(register_inet6addr_notifier);
5053
Linus Torvalds1da177e2005-04-16 15:20:36 -07005054int unregister_inet6addr_notifier(struct notifier_block *nb)
5055{
Stephen Hemmingere21e8462010-03-20 16:09:01 -07005056 return atomic_notifier_chain_unregister(&inet6addr_chain, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005057}
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +09005058EXPORT_SYMBOL(unregister_inet6addr_notifier);
5059
Thomas Grafb382b192010-11-16 04:33:57 +00005060static struct rtnl_af_ops inet6_ops = {
5061 .family = AF_INET6,
5062 .fill_link_af = inet6_fill_link_af,
5063 .get_link_af_size = inet6_get_link_af_size,
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005064 .set_link_af = inet6_set_link_af,
Thomas Grafb382b192010-11-16 04:33:57 +00005065};
5066
Linus Torvalds1da177e2005-04-16 15:20:36 -07005067/*
5068 * Init / cleanup code
5069 */
5070
5071int __init addrconf_init(void)
5072{
stephen hemmingerc2e21292010-03-17 20:31:10 +00005073 int i, err;
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +09005074
Stephen Hemmingere21e8462010-03-20 16:09:01 -07005075 err = ipv6_addr_label_init();
5076 if (err < 0) {
Joe Perchesf3213832012-05-15 14:11:53 +00005077 pr_crit("%s: cannot initialize default policy table: %d\n",
5078 __func__, err);
Neil Horman2cc6d2b2010-09-24 09:55:52 +00005079 goto out;
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +09005080 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005081
Neil Horman2cc6d2b2010-09-24 09:55:52 +00005082 err = register_pernet_subsys(&addrconf_ops);
5083 if (err < 0)
5084 goto out_addrlabel;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08005085
Linus Torvalds1da177e2005-04-16 15:20:36 -07005086 /* The addrconf netdev notifier requires that loopback_dev
5087 * has it's ipv6 private information allocated and setup
5088 * before it can bring up and give link-local addresses
5089 * to other devices which are up.
5090 *
5091 * Unfortunately, loopback_dev is not necessarily the first
5092 * entry in the global dev_base list of net devices. In fact,
5093 * it is likely to be the very last entry on that list.
5094 * So this causes the notifier registry below to try and
5095 * give link-local addresses to all devices besides loopback_dev
5096 * first, then loopback_dev, which cases all the non-loopback_dev
5097 * devices to fail to get a link-local address.
5098 *
5099 * So, as a temporary fix, allocate the ipv6 structure for
5100 * loopback_dev first by hand.
5101 * Longer term, all of the dependencies ipv6 has upon the loopback
5102 * device and it being up should be removed.
5103 */
5104 rtnl_lock();
Eric W. Biederman2774c7a2007-09-26 22:10:56 -07005105 if (!ipv6_add_dev(init_net.loopback_dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005106 err = -ENOMEM;
5107 rtnl_unlock();
5108 if (err)
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08005109 goto errlo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005110
stephen hemmingerc2e21292010-03-17 20:31:10 +00005111 for (i = 0; i < IN6_ADDR_HSIZE; i++)
5112 INIT_HLIST_HEAD(&inet6_addr_lst[i]);
5113
Linus Torvalds1da177e2005-04-16 15:20:36 -07005114 register_netdevice_notifier(&ipv6_dev_notf);
5115
Linus Torvalds1da177e2005-04-16 15:20:36 -07005116 addrconf_verify(0);
Thomas Grafc127ea22007-03-22 11:58:32 -07005117
Thomas Grafb382b192010-11-16 04:33:57 +00005118 err = rtnl_af_register(&inet6_ops);
5119 if (err < 0)
5120 goto errout_af;
5121
Greg Rosec7ac8672011-06-10 01:27:09 +00005122 err = __rtnl_register(PF_INET6, RTM_GETLINK, NULL, inet6_dump_ifinfo,
5123 NULL);
Thomas Grafc127ea22007-03-22 11:58:32 -07005124 if (err < 0)
5125 goto errout;
5126
5127 /* Only the first call to __rtnl_register can fail */
Greg Rosec7ac8672011-06-10 01:27:09 +00005128 __rtnl_register(PF_INET6, RTM_NEWADDR, inet6_rtm_newaddr, NULL, NULL);
5129 __rtnl_register(PF_INET6, RTM_DELADDR, inet6_rtm_deladdr, NULL, NULL);
5130 __rtnl_register(PF_INET6, RTM_GETADDR, inet6_rtm_getaddr,
5131 inet6_dump_ifaddr, NULL);
5132 __rtnl_register(PF_INET6, RTM_GETMULTICAST, NULL,
5133 inet6_dump_ifmcaddr, NULL);
5134 __rtnl_register(PF_INET6, RTM_GETANYCAST, NULL,
5135 inet6_dump_ifacaddr, NULL);
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +00005136 __rtnl_register(PF_INET6, RTM_GETNETCONF, inet6_netconf_get_devconf,
Nicolas Dichtel7a674202013-03-05 23:42:06 +00005137 inet6_netconf_dump_devconf, NULL);
Thomas Grafc127ea22007-03-22 11:58:32 -07005138
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +09005139 ipv6_addr_label_rtnl_register();
5140
Linus Torvalds1da177e2005-04-16 15:20:36 -07005141 return 0;
Thomas Grafc127ea22007-03-22 11:58:32 -07005142errout:
Thomas Grafb382b192010-11-16 04:33:57 +00005143 rtnl_af_unregister(&inet6_ops);
5144errout_af:
Thomas Grafc127ea22007-03-22 11:58:32 -07005145 unregister_netdevice_notifier(&ipv6_dev_notf);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08005146errlo:
5147 unregister_pernet_subsys(&addrconf_ops);
Neil Horman2cc6d2b2010-09-24 09:55:52 +00005148out_addrlabel:
5149 ipv6_addr_label_cleanup();
5150out:
Thomas Grafc127ea22007-03-22 11:58:32 -07005151 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005152}
5153
Daniel Lezcano09f77092007-12-13 05:34:58 -08005154void addrconf_cleanup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005155{
Daniel Lezcano176c39a2009-03-03 01:06:45 -08005156 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005157 int i;
5158
5159 unregister_netdevice_notifier(&ipv6_dev_notf);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08005160 unregister_pernet_subsys(&addrconf_ops);
Neil Horman2cc6d2b2010-09-24 09:55:52 +00005161 ipv6_addr_label_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005162
5163 rtnl_lock();
5164
Thomas Grafb382b192010-11-16 04:33:57 +00005165 __rtnl_af_unregister(&inet6_ops);
5166
Daniel Lezcano176c39a2009-03-03 01:06:45 -08005167 /* clean dev list */
5168 for_each_netdev(&init_net, dev) {
5169 if (__in6_dev_get(dev) == NULL)
5170 continue;
5171 addrconf_ifdown(dev, 1);
5172 }
5173 addrconf_ifdown(init_net.loopback_dev, 2);
5174
Linus Torvalds1da177e2005-04-16 15:20:36 -07005175 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005176 * Check hash table.
5177 */
stephen hemminger5c578aed2010-03-17 20:31:11 +00005178 spin_lock_bh(&addrconf_hash_lock);
stephen hemmingerc2e21292010-03-17 20:31:10 +00005179 for (i = 0; i < IN6_ADDR_HSIZE; i++)
5180 WARN_ON(!hlist_empty(&inet6_addr_lst[i]));
stephen hemminger5c578aed2010-03-17 20:31:11 +00005181 spin_unlock_bh(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005182
5183 del_timer(&addr_chk_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005184 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005185}