blob: 36ac80c6a48969d00d1be34b0e75e629c49dfcf2 [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>
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +010049#include <linux/inet.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <linux/in6.h>
51#include <linux/netdevice.h>
Thomas Graf18237302006-08-04 23:04:54 -070052#include <linux/if_addr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <linux/if_arp.h>
54#include <linux/if_arcnet.h>
55#include <linux/if_infiniband.h>
56#include <linux/route.h>
57#include <linux/inetdevice.h>
58#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090059#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#ifdef CONFIG_SYSCTL
61#include <linux/sysctl.h>
62#endif
Randy Dunlap4fc268d2006-01-11 12:17:47 -080063#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#include <linux/delay.h>
65#include <linux/notifier.h>
Paulo Marques543537b2005-06-23 00:09:02 -070066#include <linux/string.h>
Eric Dumazetddbe5032012-07-18 08:11:12 +000067#include <linux/hash.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020069#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070070#include <net/sock.h>
71#include <net/snmp.h>
72
Alexander Aring5241c2d2015-12-14 20:55:22 +010073#include <net/6lowpan.h>
YOSHIFUJI Hideaki / 吉藤英明cb6bf352013-03-25 08:26:24 +000074#include <net/firewire.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#include <net/ipv6.h>
76#include <net/protocol.h>
77#include <net/ndisc.h>
78#include <net/ip6_route.h>
79#include <net/addrconf.h>
80#include <net/tcp.h>
81#include <net/ip.h>
Thomas Graf5d620262006-08-15 00:35:02 -070082#include <net/netlink.h>
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -070083#include <net/pkt_sched.h>
David Ahernca254492015-10-12 11:47:10 -070084#include <net/l3mdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070085#include <linux/if_tunnel.h>
86#include <linux/rtnetlink.h>
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +000087#include <linux/netconf.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070088#include <linux/random.h>
Stephen Hemmingere21e8462010-03-20 16:09:01 -070089#include <linux/uaccess.h>
Herbert Xu7f7d9a62007-04-24 21:54:09 -070090#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92#include <linux/proc_fs.h>
93#include <linux/seq_file.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040094#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96/* Set to 3 to get tracing... */
97#define ACONF_DEBUG 2
98
99#if ACONF_DEBUG >= 3
dingtianhongba3542e2013-08-17 10:27:04 +0800100#define ADBG(fmt, ...) printk(fmt, ##__VA_ARGS__)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101#else
dingtianhongba3542e2013-08-17 10:27:04 +0800102#define ADBG(fmt, ...) do { if (0) printk(fmt, ##__VA_ARGS__); } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103#endif
104
105#define INFINITY_LIFE_TIME 0xFFFFFFFF
Thomas Graf18a31e12010-11-17 04:12:02 +0000106
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +0100107#define IPV6_MAX_STRLEN \
108 sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")
109
Thomas Graf18a31e12010-11-17 04:12:02 +0000110static inline u32 cstamp_delta(unsigned long cstamp)
111{
112 return (cstamp - INITIAL_JIFFIES) * 100UL / HZ;
113}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115#ifdef CONFIG_SYSCTL
WANG Conga317a2f2014-07-25 15:25:09 -0700116static int addrconf_sysctl_register(struct inet6_dev *idev);
Pavel Emelyanov408c4762008-01-10 17:41:21 -0800117static void addrconf_sysctl_unregister(struct inet6_dev *idev);
118#else
WANG Conga317a2f2014-07-25 15:25:09 -0700119static inline int addrconf_sysctl_register(struct inet6_dev *idev)
Pavel Emelyanov408c4762008-01-10 17:41:21 -0800120{
WANG Conga317a2f2014-07-25 15:25:09 -0700121 return 0;
Pavel Emelyanov408c4762008-01-10 17:41:21 -0800122}
123
124static inline void addrconf_sysctl_unregister(struct inet6_dev *idev)
125{
126}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127#endif
128
Sorin Dumitrueb7e0572012-08-30 02:01:45 +0000129static void __ipv6_regen_rndid(struct inet6_dev *idev);
130static void __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131static void ipv6_regen_rndid(unsigned long data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
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);
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +0100135static int ipv6_generate_stable_address(struct in6_addr *addr,
136 u8 dad_count,
137 const struct inet6_dev *idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
139/*
140 * Configured unicast address hash table
141 */
stephen hemmingerc2e21292010-03-17 20:31:10 +0000142static struct hlist_head inet6_addr_lst[IN6_ADDR_HSIZE];
stephen hemminger5c578ae2010-03-17 20:31:11 +0000143static DEFINE_SPINLOCK(addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100145static void addrconf_verify(void);
146static void addrconf_verify_rtnl(void);
147static void addrconf_verify_work(struct work_struct *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100149static struct workqueue_struct *addrconf_wq;
150static DECLARE_DELAYED_WORK(addr_chk_work, addrconf_verify_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
152static void addrconf_join_anycast(struct inet6_ifaddr *ifp);
153static void addrconf_leave_anycast(struct inet6_ifaddr *ifp);
154
Jiri Pirko93d9b7d2010-03-10 10:28:56 +0000155static void addrconf_type_change(struct net_device *dev,
156 unsigned long event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157static int addrconf_ifdown(struct net_device *dev, int how);
158
Romain Kuntz21caa662013-01-09 21:06:03 +0000159static struct rt6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
160 int plen,
161 const struct net_device *dev,
162 u32 flags, u32 noflags);
163
David S. Millercf22f9a2012-04-14 21:37:40 -0400164static void addrconf_dad_start(struct inet6_ifaddr *ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100165static void addrconf_dad_work(struct work_struct *w);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166static void addrconf_dad_completed(struct inet6_ifaddr *ifp);
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +0900167static void addrconf_dad_run(struct inet6_dev *idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168static void addrconf_rs_timer(unsigned long data);
169static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
170static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
171
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900172static void inet6_prefix_notify(int event, struct inet6_dev *idev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 struct prefix_info *pinfo);
David S. Miller3e81c6d2010-03-20 16:18:00 -0700174static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
175 struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Adrian Bunk888c8482008-07-22 14:21:58 -0700177static struct ipv6_devconf ipv6_devconf __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 .forwarding = 0,
179 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
180 .mtu6 = IPV6_MIN_MTU,
181 .accept_ra = 1,
182 .accept_redirects = 1,
183 .autoconf = 1,
184 .force_mld_version = 0,
Hannes Frederic Sowafc4eba52013-08-14 01:03:46 +0200185 .mldv1_unsolicited_report_interval = 10 * HZ,
186 .mldv2_unsolicited_report_interval = HZ,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 .dad_transmits = 1,
188 .rtr_solicits = MAX_RTR_SOLICITATIONS,
189 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
190 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
Ian Morris67ba4152014-08-24 21:53:10 +0100191 .use_tempaddr = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 .temp_valid_lft = TEMP_VALID_LIFETIME,
193 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
194 .regen_max_retry = REGEN_MAX_RETRY,
195 .max_desync_factor = MAX_DESYNC_FACTOR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 .max_addresses = IPV6_MAX_ADDRESSES,
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -0800197 .accept_ra_defrtr = 1,
Ben Greeard9333192014-06-25 14:44:53 -0700198 .accept_ra_from_local = 0,
Hangbin Liu8013d1d2015-07-30 14:28:42 +0800199 .accept_ra_min_hop_limit= 1,
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -0800200 .accept_ra_pinfo = 1,
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800201#ifdef CONFIG_IPV6_ROUTER_PREF
202 .accept_ra_rtr_pref = 1,
YOSHIFUJI Hideaki52e16352006-03-20 17:05:47 -0800203 .rtr_probe_interval = 60 * HZ,
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -0800204#ifdef CONFIG_IPV6_ROUTE_INFO
205 .accept_ra_rt_info_max_plen = 0,
206#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800207#endif
Lorenzo Colitti2b253062014-03-26 19:35:41 +0900208 .accept_ra_rt_table = 0,
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -0700209 .proxy_ndp = 0,
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -0700210 .accept_source_route = 0, /* we do not accept RH0 by default. */
YOSHIFUJI Hideaki778d80b2008-06-28 14:17:11 +0900211 .disable_ipv6 = 0,
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +0900212 .accept_dad = 1,
Hannes Frederic Sowab800c3b2013-08-27 01:36:51 +0200213 .suppress_frag_ndisc = 1,
Harout Hedeshianc2943f12015-01-20 10:06:05 -0700214 .accept_ra_mtu = 1,
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +0100215 .stable_secret = {
216 .initialized = false,
Erik Kline3985e8a2015-07-22 16:38:25 +0900217 },
218 .use_oif_addrs_only = 0,
Andy Gospodarek35103d12015-08-13 10:39:01 -0400219 .ignore_routes_with_linkdown = 0,
David Ahernf1705ec2016-02-24 09:25:37 -0800220 .keep_addr_on_down = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221};
222
Brian Haleyab32ea52006-09-22 14:15:41 -0700223static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 .forwarding = 0,
225 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
226 .mtu6 = IPV6_MIN_MTU,
227 .accept_ra = 1,
228 .accept_redirects = 1,
229 .autoconf = 1,
Hannes Frederic Sowafc4eba52013-08-14 01:03:46 +0200230 .force_mld_version = 0,
231 .mldv1_unsolicited_report_interval = 10 * HZ,
232 .mldv2_unsolicited_report_interval = HZ,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 .dad_transmits = 1,
234 .rtr_solicits = MAX_RTR_SOLICITATIONS,
235 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
236 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 .use_tempaddr = 0,
238 .temp_valid_lft = TEMP_VALID_LIFETIME,
239 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
240 .regen_max_retry = REGEN_MAX_RETRY,
241 .max_desync_factor = MAX_DESYNC_FACTOR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 .max_addresses = IPV6_MAX_ADDRESSES,
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -0800243 .accept_ra_defrtr = 1,
Ben Greeard9333192014-06-25 14:44:53 -0700244 .accept_ra_from_local = 0,
Hangbin Liu8013d1d2015-07-30 14:28:42 +0800245 .accept_ra_min_hop_limit= 1,
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -0800246 .accept_ra_pinfo = 1,
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800247#ifdef CONFIG_IPV6_ROUTER_PREF
248 .accept_ra_rtr_pref = 1,
YOSHIFUJI Hideaki52e16352006-03-20 17:05:47 -0800249 .rtr_probe_interval = 60 * HZ,
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -0800250#ifdef CONFIG_IPV6_ROUTE_INFO
251 .accept_ra_rt_info_max_plen = 0,
252#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800253#endif
Lorenzo Colitti2b253062014-03-26 19:35:41 +0900254 .accept_ra_rt_table = 0,
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -0700255 .proxy_ndp = 0,
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -0700256 .accept_source_route = 0, /* we do not accept RH0 by default. */
YOSHIFUJI Hideaki778d80b2008-06-28 14:17:11 +0900257 .disable_ipv6 = 0,
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +0900258 .accept_dad = 1,
Hannes Frederic Sowab800c3b2013-08-27 01:36:51 +0200259 .suppress_frag_ndisc = 1,
Harout Hedeshianc2943f12015-01-20 10:06:05 -0700260 .accept_ra_mtu = 1,
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +0100261 .stable_secret = {
262 .initialized = false,
263 },
Erik Kline3985e8a2015-07-22 16:38:25 +0900264 .use_oif_addrs_only = 0,
Andy Gospodarek35103d12015-08-13 10:39:01 -0400265 .ignore_routes_with_linkdown = 0,
David Ahernf1705ec2016-02-24 09:25:37 -0800266 .keep_addr_on_down = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267};
268
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -0700269/* Check if a valid qdisc is available */
David S. Miller05297942008-07-08 23:01:27 -0700270static inline bool addrconf_qdisc_ok(const struct net_device *dev)
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -0700271{
David S. Miller05297942008-07-08 23:01:27 -0700272 return !qdisc_tx_is_noop(dev);
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -0700273}
274
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200275static void addrconf_del_rs_timer(struct inet6_dev *idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200277 if (del_timer(&idev->rs_timer))
278 __in6_dev_put(idev);
279}
280
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100281static void addrconf_del_dad_work(struct inet6_ifaddr *ifp)
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200282{
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100283 if (cancel_delayed_work(&ifp->dad_work))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 __in6_ifa_put(ifp);
285}
286
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200287static void addrconf_mod_rs_timer(struct inet6_dev *idev,
288 unsigned long when)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200290 if (!timer_pending(&idev->rs_timer))
291 in6_dev_hold(idev);
292 mod_timer(&idev->rs_timer, jiffies + when);
293}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100295static void addrconf_mod_dad_work(struct inet6_ifaddr *ifp,
296 unsigned long delay)
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200297{
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100298 if (!delayed_work_pending(&ifp->dad_work))
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200299 in6_ifa_hold(ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100300 mod_delayed_work(addrconf_wq, &ifp->dad_work, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301}
302
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700303static int snmp6_alloc_dev(struct inet6_dev *idev)
304{
John Stultz827da442013-10-07 15:51:58 -0700305 int i;
306
WANG Cong698365f2014-05-05 15:55:55 -0700307 idev->stats.ipv6 = alloc_percpu(struct ipstats_mib);
308 if (!idev->stats.ipv6)
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700309 goto err_ip;
John Stultz827da442013-10-07 15:51:58 -0700310
311 for_each_possible_cpu(i) {
312 struct ipstats_mib *addrconf_stats;
WANG Cong698365f2014-05-05 15:55:55 -0700313 addrconf_stats = per_cpu_ptr(idev->stats.ipv6, i);
John Stultz827da442013-10-07 15:51:58 -0700314 u64_stats_init(&addrconf_stats->syncp);
John Stultz827da442013-10-07 15:51:58 -0700315 }
316
317
Eric Dumazetbe281e52011-05-19 01:14:23 +0000318 idev->stats.icmpv6dev = kzalloc(sizeof(struct icmpv6_mib_device),
319 GFP_KERNEL);
320 if (!idev->stats.icmpv6dev)
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700321 goto err_icmp;
Eric Dumazetbe281e52011-05-19 01:14:23 +0000322 idev->stats.icmpv6msgdev = kzalloc(sizeof(struct icmpv6msg_mib_device),
323 GFP_KERNEL);
324 if (!idev->stats.icmpv6msgdev)
David L Stevens14878f72007-09-16 16:52:35 -0700325 goto err_icmpmsg;
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700326
327 return 0;
328
David L Stevens14878f72007-09-16 16:52:35 -0700329err_icmpmsg:
Eric Dumazetbe281e52011-05-19 01:14:23 +0000330 kfree(idev->stats.icmpv6dev);
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700331err_icmp:
WANG Cong698365f2014-05-05 15:55:55 -0700332 free_percpu(idev->stats.ipv6);
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700333err_ip:
Pavel Emelyanovaaf70ec2007-10-17 21:25:32 -0700334 return -ENOMEM;
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700335}
336
Eldad Zack8e5e8f32012-04-01 07:49:08 +0000337static struct inet6_dev *ipv6_add_dev(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
339 struct inet6_dev *ndev;
WANG Conga317a2f2014-07-25 15:25:09 -0700340 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
342 ASSERT_RTNL();
343
344 if (dev->mtu < IPV6_MIN_MTU)
WANG Conga317a2f2014-07-25 15:25:09 -0700345 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900347 ndev = kzalloc(sizeof(struct inet6_dev), GFP_KERNEL);
Ian Morris63159f22015-03-29 14:00:04 +0100348 if (!ndev)
WANG Conga317a2f2014-07-25 15:25:09 -0700349 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Ingo Oeser322f74a2006-03-20 23:01:47 -0800351 rwlock_init(&ndev->lock);
352 ndev->dev = dev;
stephen hemminger502a2ff2010-03-17 20:31:13 +0000353 INIT_LIST_HEAD(&ndev->addr_list);
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200354 setup_timer(&ndev->rs_timer, addrconf_rs_timer,
355 (unsigned long)ndev);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900356 memcpy(&ndev->cnf, dev_net(dev)->ipv6.devconf_dflt, sizeof(ndev->cnf));
Hannes Frederic Sowa9b29c692015-12-15 22:59:12 +0100357
358 if (ndev->cnf.stable_secret.initialized)
359 ndev->addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
360 else
361 ndev->addr_gen_mode = IN6_ADDR_GEN_MODE_EUI64;
362
Ingo Oeser322f74a2006-03-20 23:01:47 -0800363 ndev->cnf.mtu6 = dev->mtu;
Ingo Oeser322f74a2006-03-20 23:01:47 -0800364 ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl);
Ian Morris63159f22015-03-29 14:00:04 +0100365 if (!ndev->nd_parms) {
Ingo Oeser322f74a2006-03-20 23:01:47 -0800366 kfree(ndev);
WANG Conga317a2f2014-07-25 15:25:09 -0700367 return ERR_PTR(err);
Ingo Oeser322f74a2006-03-20 23:01:47 -0800368 }
Ben Hutchings0187bdf2008-06-19 16:15:47 -0700369 if (ndev->cnf.forwarding)
370 dev_disable_lro(dev);
Ingo Oeser322f74a2006-03-20 23:01:47 -0800371 /* We refer to the device */
372 dev_hold(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Ingo Oeser322f74a2006-03-20 23:01:47 -0800374 if (snmp6_alloc_dev(ndev) < 0) {
dingtianhongba3542e2013-08-17 10:27:04 +0800375 ADBG(KERN_WARNING
Joe Perchesf3213832012-05-15 14:11:53 +0000376 "%s: cannot allocate memory for statistics; dev=%s.\n",
dingtianhongba3542e2013-08-17 10:27:04 +0800377 __func__, dev->name);
Ingo Oeser322f74a2006-03-20 23:01:47 -0800378 neigh_parms_release(&nd_tbl, ndev->nd_parms);
Roy Li8603e332011-09-20 15:10:16 -0400379 dev_put(dev);
380 kfree(ndev);
WANG Conga317a2f2014-07-25 15:25:09 -0700381 return ERR_PTR(err);
Ingo Oeser322f74a2006-03-20 23:01:47 -0800382 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Ingo Oeser322f74a2006-03-20 23:01:47 -0800384 if (snmp6_register_dev(ndev) < 0) {
dingtianhongba3542e2013-08-17 10:27:04 +0800385 ADBG(KERN_WARNING
Joe Perchesf3213832012-05-15 14:11:53 +0000386 "%s: cannot create /proc/net/dev_snmp6/%s\n",
dingtianhongba3542e2013-08-17 10:27:04 +0800387 __func__, dev->name);
WANG Conga317a2f2014-07-25 15:25:09 -0700388 goto err_release;
Ingo Oeser322f74a2006-03-20 23:01:47 -0800389 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
Ingo Oeser322f74a2006-03-20 23:01:47 -0800391 /* One reference from device. We must do this before
392 * we invoke __ipv6_regen_rndid().
393 */
394 in6_dev_hold(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +0900396 if (dev->flags & (IFF_NOARP | IFF_LOOPBACK))
397 ndev->cnf.accept_dad = -1;
398
Amerigo Wang07a93622012-10-29 16:23:10 +0000399#if IS_ENABLED(CONFIG_IPV6_SIT)
YOSHIFUJI Hideakib077d7a2008-04-13 23:42:18 -0700400 if (dev->type == ARPHRD_SIT && (dev->priv_flags & IFF_ISATAP)) {
Joe Perchesf3213832012-05-15 14:11:53 +0000401 pr_info("%s: Disabled Multicast RS\n", dev->name);
YOSHIFUJI Hideakib077d7a2008-04-13 23:42:18 -0700402 ndev->cnf.rtr_solicits = 0;
403 }
404#endif
405
stephen hemminger372e6c82010-03-17 20:31:09 +0000406 INIT_LIST_HEAD(&ndev->tempaddr_list);
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800407 setup_timer(&ndev->regen_timer, ipv6_regen_rndid, (unsigned long)ndev);
Ingo Oeser322f74a2006-03-20 23:01:47 -0800408 if ((dev->flags&IFF_LOOPBACK) ||
409 dev->type == ARPHRD_TUNNEL ||
YOSHIFUJI Hideaki9625ed72008-04-13 23:47:11 -0700410 dev->type == ARPHRD_TUNNEL6 ||
Joerg Roedel0be669b2006-10-10 14:49:53 -0700411 dev->type == ARPHRD_SIT ||
Joerg Roedel0be669b2006-10-10 14:49:53 -0700412 dev->type == ARPHRD_NONE) {
Ingo Oeser322f74a2006-03-20 23:01:47 -0800413 ndev->cnf.use_tempaddr = -1;
414 } else {
415 in6_dev_hold(ndev);
416 ipv6_regen_rndid((unsigned long) ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 }
David S. Miller5d9efa72013-10-28 20:07:50 -0400418
Daniel Borkmann914faa12013-04-09 03:47:14 +0000419 ndev->token = in6addr_any;
Ingo Oeser322f74a2006-03-20 23:01:47 -0800420
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -0700421 if (netif_running(dev) && addrconf_qdisc_ok(dev))
Herbert Xu53aadcc2007-03-27 14:31:52 -0700422 ndev->if_flags |= IF_READY;
423
Ingo Oeser322f74a2006-03-20 23:01:47 -0800424 ipv6_mc_init_dev(ndev);
425 ndev->tstamp = jiffies;
WANG Conga317a2f2014-07-25 15:25:09 -0700426 err = addrconf_sysctl_register(ndev);
427 if (err) {
428 ipv6_mc_destroy_dev(ndev);
429 del_timer(&ndev->regen_timer);
Sabrina Dubroca2a189f92015-11-04 14:47:53 +0100430 snmp6_unregister_dev(ndev);
WANG Conga317a2f2014-07-25 15:25:09 -0700431 goto err_release;
432 }
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;
WANG Conga317a2f2014-07-25 15:25:09 -0700447
448err_release:
449 neigh_parms_release(&nd_tbl, ndev->nd_parms);
450 ndev->dead = 1;
451 in6_dev_finish_destroy(ndev);
452 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453}
454
Eldad Zack8e5e8f32012-04-01 07:49:08 +0000455static struct inet6_dev *ipv6_find_idev(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456{
457 struct inet6_dev *idev;
458
459 ASSERT_RTNL();
460
Stephen Hemmingere21e8462010-03-20 16:09:01 -0700461 idev = __in6_dev_get(dev);
462 if (!idev) {
463 idev = ipv6_add_dev(dev);
WANG Conga317a2f2014-07-25 15:25:09 -0700464 if (IS_ERR(idev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 return NULL;
466 }
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +0900467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 if (dev->flags&IFF_UP)
469 ipv6_mc_up(idev);
470 return idev;
471}
472
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000473static int inet6_netconf_msgsize_devconf(int type)
474{
475 int size = NLMSG_ALIGN(sizeof(struct netconfmsg))
476 + nla_total_size(4); /* NETCONFA_IFINDEX */
Zhang Shengju136ba622016-03-10 08:55:50 +0000477 bool all = false;
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000478
Zhang Shengju136ba622016-03-10 08:55:50 +0000479 if (type == NETCONFA_ALL)
480 all = true;
481
482 if (all || type == NETCONFA_FORWARDING)
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000483 size += nla_total_size(4);
David S. Millerb1afce92012-12-04 14:46:34 -0500484#ifdef CONFIG_IPV6_MROUTE
Zhang Shengju136ba622016-03-10 08:55:50 +0000485 if (all || type == NETCONFA_MC_FORWARDING)
Nicolas Dichteld67b8c62012-12-04 01:13:35 +0000486 size += nla_total_size(4);
David S. Millerb1afce92012-12-04 14:46:34 -0500487#endif
Zhang Shengju136ba622016-03-10 08:55:50 +0000488 if (all || type == NETCONFA_PROXY_NEIGH)
stephen hemmingerc92d5492013-12-17 22:37:14 -0800489 size += nla_total_size(4);
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000490
Zhang Shengju136ba622016-03-10 08:55:50 +0000491 if (all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN)
Andy Gospodarek35103d12015-08-13 10:39:01 -0400492 size += nla_total_size(4);
493
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000494 return size;
495}
496
497static int inet6_netconf_fill_devconf(struct sk_buff *skb, int ifindex,
498 struct ipv6_devconf *devconf, u32 portid,
499 u32 seq, int event, unsigned int flags,
500 int type)
501{
502 struct nlmsghdr *nlh;
503 struct netconfmsg *ncm;
Zhang Shengju136ba622016-03-10 08:55:50 +0000504 bool all = false;
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000505
506 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct netconfmsg),
507 flags);
Ian Morris63159f22015-03-29 14:00:04 +0100508 if (!nlh)
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000509 return -EMSGSIZE;
510
Zhang Shengju136ba622016-03-10 08:55:50 +0000511 if (type == NETCONFA_ALL)
512 all = true;
513
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000514 ncm = nlmsg_data(nlh);
515 ncm->ncm_family = AF_INET6;
516
517 if (nla_put_s32(skb, NETCONFA_IFINDEX, ifindex) < 0)
518 goto nla_put_failure;
519
Zhang Shengju136ba622016-03-10 08:55:50 +0000520 if ((all || type == NETCONFA_FORWARDING) &&
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000521 nla_put_s32(skb, NETCONFA_FORWARDING, devconf->forwarding) < 0)
522 goto nla_put_failure;
David S. Millerb1afce92012-12-04 14:46:34 -0500523#ifdef CONFIG_IPV6_MROUTE
Zhang Shengju136ba622016-03-10 08:55:50 +0000524 if ((all || type == NETCONFA_MC_FORWARDING) &&
Nicolas Dichteld67b8c62012-12-04 01:13:35 +0000525 nla_put_s32(skb, NETCONFA_MC_FORWARDING,
526 devconf->mc_forwarding) < 0)
527 goto nla_put_failure;
David S. Millerb1afce92012-12-04 14:46:34 -0500528#endif
Zhang Shengju136ba622016-03-10 08:55:50 +0000529 if ((all || type == NETCONFA_PROXY_NEIGH) &&
stephen hemmingerc92d5492013-12-17 22:37:14 -0800530 nla_put_s32(skb, NETCONFA_PROXY_NEIGH, devconf->proxy_ndp) < 0)
531 goto nla_put_failure;
532
Zhang Shengju136ba622016-03-10 08:55:50 +0000533 if ((all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN) &&
Andy Gospodarek35103d12015-08-13 10:39:01 -0400534 nla_put_s32(skb, NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
535 devconf->ignore_routes_with_linkdown) < 0)
536 goto nla_put_failure;
537
Johannes Berg053c0952015-01-16 22:09:00 +0100538 nlmsg_end(skb, nlh);
539 return 0;
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000540
541nla_put_failure:
542 nlmsg_cancel(skb, nlh);
543 return -EMSGSIZE;
544}
545
Nicolas Dichteld67b8c62012-12-04 01:13:35 +0000546void inet6_netconf_notify_devconf(struct net *net, int type, int ifindex,
547 struct ipv6_devconf *devconf)
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000548{
549 struct sk_buff *skb;
550 int err = -ENOBUFS;
551
Eric Dumazet927265b2016-07-08 05:46:04 +0200552 skb = nlmsg_new(inet6_netconf_msgsize_devconf(type), GFP_KERNEL);
Ian Morris63159f22015-03-29 14:00:04 +0100553 if (!skb)
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000554 goto errout;
555
556 err = inet6_netconf_fill_devconf(skb, ifindex, devconf, 0, 0,
557 RTM_NEWNETCONF, 0, type);
558 if (err < 0) {
559 /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
560 WARN_ON(err == -EMSGSIZE);
561 kfree_skb(skb);
562 goto errout;
563 }
Eric Dumazet927265b2016-07-08 05:46:04 +0200564 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_NETCONF, NULL, GFP_KERNEL);
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000565 return;
566errout:
Cong Dingbd779022012-12-18 12:08:56 +0000567 rtnl_set_sk_err(net, RTNLGRP_IPV6_NETCONF, err);
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000568}
569
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000570static const struct nla_policy devconf_ipv6_policy[NETCONFA_MAX+1] = {
571 [NETCONFA_IFINDEX] = { .len = sizeof(int) },
572 [NETCONFA_FORWARDING] = { .len = sizeof(int) },
stephen hemmingerc92d5492013-12-17 22:37:14 -0800573 [NETCONFA_PROXY_NEIGH] = { .len = sizeof(int) },
Andy Gospodarek35103d12015-08-13 10:39:01 -0400574 [NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN] = { .len = sizeof(int) },
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000575};
576
577static int inet6_netconf_get_devconf(struct sk_buff *in_skb,
Thomas Graf661d2962013-03-21 07:45:29 +0000578 struct nlmsghdr *nlh)
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000579{
580 struct net *net = sock_net(in_skb->sk);
581 struct nlattr *tb[NETCONFA_MAX+1];
582 struct netconfmsg *ncm;
583 struct sk_buff *skb;
584 struct ipv6_devconf *devconf;
585 struct inet6_dev *in6_dev;
586 struct net_device *dev;
587 int ifindex;
588 int err;
589
590 err = nlmsg_parse(nlh, sizeof(*ncm), tb, NETCONFA_MAX,
591 devconf_ipv6_policy);
592 if (err < 0)
593 goto errout;
594
Anton Protopopova97eb332016-02-16 21:43:16 -0500595 err = -EINVAL;
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000596 if (!tb[NETCONFA_IFINDEX])
597 goto errout;
598
599 ifindex = nla_get_s32(tb[NETCONFA_IFINDEX]);
600 switch (ifindex) {
601 case NETCONFA_IFINDEX_ALL:
602 devconf = net->ipv6.devconf_all;
603 break;
604 case NETCONFA_IFINDEX_DEFAULT:
605 devconf = net->ipv6.devconf_dflt;
606 break;
607 default:
608 dev = __dev_get_by_index(net, ifindex);
Ian Morris63159f22015-03-29 14:00:04 +0100609 if (!dev)
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000610 goto errout;
611 in6_dev = __in6_dev_get(dev);
Ian Morris63159f22015-03-29 14:00:04 +0100612 if (!in6_dev)
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000613 goto errout;
614 devconf = &in6_dev->cnf;
615 break;
616 }
617
618 err = -ENOBUFS;
Zhang Shengju136ba622016-03-10 08:55:50 +0000619 skb = nlmsg_new(inet6_netconf_msgsize_devconf(NETCONFA_ALL), GFP_ATOMIC);
Ian Morris63159f22015-03-29 14:00:04 +0100620 if (!skb)
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000621 goto errout;
622
623 err = inet6_netconf_fill_devconf(skb, ifindex, devconf,
624 NETLINK_CB(in_skb).portid,
625 nlh->nlmsg_seq, RTM_NEWNETCONF, 0,
Zhang Shengju136ba622016-03-10 08:55:50 +0000626 NETCONFA_ALL);
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000627 if (err < 0) {
628 /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
629 WARN_ON(err == -EMSGSIZE);
630 kfree_skb(skb);
631 goto errout;
632 }
633 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
634errout:
635 return err;
636}
637
Nicolas Dichtel7a674202013-03-05 23:42:06 +0000638static int inet6_netconf_dump_devconf(struct sk_buff *skb,
639 struct netlink_callback *cb)
640{
641 struct net *net = sock_net(skb->sk);
642 int h, s_h;
643 int idx, s_idx;
644 struct net_device *dev;
645 struct inet6_dev *idev;
646 struct hlist_head *head;
647
648 s_h = cb->args[0];
649 s_idx = idx = cb->args[1];
650
651 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
652 idx = 0;
653 head = &net->dev_index_head[h];
654 rcu_read_lock();
Nicolas Dichtel63998ac2013-03-22 06:28:43 +0000655 cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^
656 net->dev_base_seq;
Nicolas Dichtel7a674202013-03-05 23:42:06 +0000657 hlist_for_each_entry_rcu(dev, head, index_hlist) {
658 if (idx < s_idx)
659 goto cont;
660 idev = __in6_dev_get(dev);
661 if (!idev)
662 goto cont;
663
664 if (inet6_netconf_fill_devconf(skb, dev->ifindex,
665 &idev->cnf,
666 NETLINK_CB(cb->skb).portid,
667 cb->nlh->nlmsg_seq,
668 RTM_NEWNETCONF,
669 NLM_F_MULTI,
Zhang Shengju136ba622016-03-10 08:55:50 +0000670 NETCONFA_ALL) < 0) {
Nicolas Dichtel7a674202013-03-05 23:42:06 +0000671 rcu_read_unlock();
672 goto done;
673 }
Nicolas Dichtel63998ac2013-03-22 06:28:43 +0000674 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
Nicolas Dichtel7a674202013-03-05 23:42:06 +0000675cont:
676 idx++;
677 }
678 rcu_read_unlock();
679 }
680 if (h == NETDEV_HASHENTRIES) {
681 if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_ALL,
682 net->ipv6.devconf_all,
683 NETLINK_CB(cb->skb).portid,
684 cb->nlh->nlmsg_seq,
685 RTM_NEWNETCONF, NLM_F_MULTI,
Zhang Shengju136ba622016-03-10 08:55:50 +0000686 NETCONFA_ALL) < 0)
Nicolas Dichtel7a674202013-03-05 23:42:06 +0000687 goto done;
688 else
689 h++;
690 }
691 if (h == NETDEV_HASHENTRIES + 1) {
692 if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_DEFAULT,
693 net->ipv6.devconf_dflt,
694 NETLINK_CB(cb->skb).portid,
695 cb->nlh->nlmsg_seq,
696 RTM_NEWNETCONF, NLM_F_MULTI,
Zhang Shengju136ba622016-03-10 08:55:50 +0000697 NETCONFA_ALL) < 0)
Nicolas Dichtel7a674202013-03-05 23:42:06 +0000698 goto done;
699 else
700 h++;
701 }
702done:
703 cb->args[0] = h;
704 cb->args[1] = idx;
705
706 return skb->len;
707}
708
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709#ifdef CONFIG_SYSCTL
710static void dev_forward_change(struct inet6_dev *idev)
711{
712 struct net_device *dev;
713 struct inet6_ifaddr *ifa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
715 if (!idev)
716 return;
717 dev = idev->dev;
Ben Hutchings0187bdf2008-06-19 16:15:47 -0700718 if (idev->cnf.forwarding)
719 dev_disable_lro(dev);
Amerigo Wang1a940832012-10-28 17:43:53 +0000720 if (dev->flags & IFF_MULTICAST) {
Hannes Frederic Sowa2c5e8932013-02-10 03:50:18 +0000721 if (idev->cnf.forwarding) {
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +0900722 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
Hannes Frederic Sowa2c5e8932013-02-10 03:50:18 +0000723 ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allrouters);
724 ipv6_dev_mc_inc(dev, &in6addr_sitelocal_allrouters);
725 } else {
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +0900726 ipv6_dev_mc_dec(dev, &in6addr_linklocal_allrouters);
Hannes Frederic Sowa2c5e8932013-02-10 03:50:18 +0000727 ipv6_dev_mc_dec(dev, &in6addr_interfacelocal_allrouters);
728 ipv6_dev_mc_dec(dev, &in6addr_sitelocal_allrouters);
729 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 }
stephen hemminger502a2ff2010-03-17 20:31:13 +0000731
732 list_for_each_entry(ifa, &idev->addr_list, if_list) {
Michal Wrobel2c12a742007-02-26 15:36:10 -0800733 if (ifa->flags&IFA_F_TENTATIVE)
734 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 if (idev->cnf.forwarding)
736 addrconf_join_anycast(ifa);
737 else
738 addrconf_leave_anycast(ifa);
739 }
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000740 inet6_netconf_notify_devconf(dev_net(dev), NETCONFA_FORWARDING,
741 dev->ifindex, &idev->cnf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742}
743
744
Pavel Emelyanove1869322008-01-10 17:43:50 -0800745static void addrconf_forward_change(struct net *net, __s32 newf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746{
747 struct net_device *dev;
748 struct inet6_dev *idev;
749
Ben Hutchings4acd4942012-08-14 08:54:51 +0000750 for_each_netdev(net, dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 idev = __in6_dev_get(dev);
752 if (idev) {
Pavel Emelyanove1869322008-01-10 17:43:50 -0800753 int changed = (!idev->cnf.forwarding) ^ (!newf);
754 idev->cnf.forwarding = newf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 if (changed)
756 dev_forward_change(idev);
757 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759}
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800760
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000761static int addrconf_fixup_forwarding(struct ctl_table *table, int *p, int newf)
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800762{
Pavel Emelyanovbff16c22008-01-10 17:42:13 -0800763 struct net *net;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000764 int old;
765
766 if (!rtnl_trylock())
767 return restart_syscall();
Pavel Emelyanovbff16c22008-01-10 17:42:13 -0800768
769 net = (struct net *)table->extra2;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000770 old = *p;
771 *p = newf;
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800772
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000773 if (p == &net->ipv6.devconf_dflt->forwarding) {
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000774 if ((!newf) ^ (!old))
775 inet6_netconf_notify_devconf(net, NETCONFA_FORWARDING,
776 NETCONFA_IFINDEX_DEFAULT,
777 net->ipv6.devconf_dflt);
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000778 rtnl_unlock();
779 return 0;
Eric W. Biederman88af1822010-02-19 13:22:59 +0000780 }
Stephen Hemmingerb325fdd2009-02-26 06:55:31 +0000781
Pavel Emelyanove1869322008-01-10 17:43:50 -0800782 if (p == &net->ipv6.devconf_all->forwarding) {
Pavel Emelyanove1869322008-01-10 17:43:50 -0800783 net->ipv6.devconf_dflt->forwarding = newf;
784 addrconf_forward_change(net, newf);
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000785 if ((!newf) ^ (!old))
786 inet6_netconf_notify_devconf(net, NETCONFA_FORWARDING,
787 NETCONFA_IFINDEX_ALL,
788 net->ipv6.devconf_all);
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000789 } else if ((!newf) ^ (!old))
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800790 dev_forward_change((struct inet6_dev *)table->extra1);
Ben Hutchings0187bdf2008-06-19 16:15:47 -0700791 rtnl_unlock();
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800792
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000793 if (newf)
Daniel Lezcano7b4da532008-03-04 13:47:14 -0800794 rt6_purge_dflt_routers(net);
Stephen Hemmingerb325fdd2009-02-26 06:55:31 +0000795 return 1;
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800796}
Andy Gospodarek35103d12015-08-13 10:39:01 -0400797
798static void addrconf_linkdown_change(struct net *net, __s32 newf)
799{
800 struct net_device *dev;
801 struct inet6_dev *idev;
802
803 for_each_netdev(net, dev) {
804 idev = __in6_dev_get(dev);
805 if (idev) {
806 int changed = (!idev->cnf.ignore_routes_with_linkdown) ^ (!newf);
807
808 idev->cnf.ignore_routes_with_linkdown = newf;
809 if (changed)
810 inet6_netconf_notify_devconf(dev_net(dev),
811 NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
812 dev->ifindex,
813 &idev->cnf);
814 }
815 }
816}
817
818static int addrconf_fixup_linkdown(struct ctl_table *table, int *p, int newf)
819{
820 struct net *net;
821 int old;
822
823 if (!rtnl_trylock())
824 return restart_syscall();
825
826 net = (struct net *)table->extra2;
827 old = *p;
828 *p = newf;
829
830 if (p == &net->ipv6.devconf_dflt->ignore_routes_with_linkdown) {
831 if ((!newf) ^ (!old))
832 inet6_netconf_notify_devconf(net,
833 NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
834 NETCONFA_IFINDEX_DEFAULT,
835 net->ipv6.devconf_dflt);
836 rtnl_unlock();
837 return 0;
838 }
839
840 if (p == &net->ipv6.devconf_all->ignore_routes_with_linkdown) {
841 net->ipv6.devconf_dflt->ignore_routes_with_linkdown = newf;
842 addrconf_linkdown_change(net, newf);
843 if ((!newf) ^ (!old))
844 inet6_netconf_notify_devconf(net,
845 NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
846 NETCONFA_IFINDEX_ALL,
847 net->ipv6.devconf_all);
848 }
849 rtnl_unlock();
850
851 return 1;
852}
853
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854#endif
855
stephen hemminger5c578ae2010-03-17 20:31:11 +0000856/* Nobody refers to this ifaddr, destroy it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp)
858{
stephen hemmingerc2e21292010-03-17 20:31:10 +0000859 WARN_ON(!hlist_unhashed(&ifp->addr_lst));
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700860
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861#ifdef NET_REFCNT_DEBUG
Joe Perches91df42b2012-05-15 14:11:54 +0000862 pr_debug("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863#endif
864
865 in6_dev_put(ifp->idev);
866
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100867 if (cancel_delayed_work(&ifp->dad_work))
868 pr_notice("delayed DAD work was pending while freeing ifa=%p\n",
869 ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
Herbert Xue9d3e082010-05-18 15:36:06 -0700871 if (ifp->state != INET6_IFADDR_STATE_DEAD) {
Joe Perchesf3213832012-05-15 14:11:53 +0000872 pr_warn("Freeing alive inet6 address %p\n", ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 return;
874 }
Amerigo Wang94e187c2012-10-29 00:13:19 +0000875 ip6_rt_put(ifp->rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
Lai Jiangshane5785982011-03-15 18:00:14 +0800877 kfree_rcu(ifp, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878}
879
Brian Haleye55ffac2006-07-10 15:25:51 -0700880static void
881ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp)
882{
stephen hemminger502a2ff2010-03-17 20:31:13 +0000883 struct list_head *p;
YOSHIFUJI Hideaki8a6ce0c2006-07-11 13:05:30 -0700884 int ifp_scope = ipv6_addr_src_scope(&ifp->addr);
Brian Haleye55ffac2006-07-10 15:25:51 -0700885
886 /*
887 * Each device address list is sorted in order of scope -
888 * global before linklocal.
889 */
stephen hemminger502a2ff2010-03-17 20:31:13 +0000890 list_for_each(p, &idev->addr_list) {
891 struct inet6_ifaddr *ifa
892 = list_entry(p, struct inet6_ifaddr, if_list);
YOSHIFUJI Hideaki8a6ce0c2006-07-11 13:05:30 -0700893 if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr))
Brian Haleye55ffac2006-07-10 15:25:51 -0700894 break;
895 }
896
David S. Millerb54c9b92010-03-25 21:25:30 -0700897 list_add_tail(&ifp->if_list, p);
Brian Haleye55ffac2006-07-10 15:25:51 -0700898}
899
Eric Dumazetddbe5032012-07-18 08:11:12 +0000900static u32 inet6_addr_hash(const struct in6_addr *addr)
YOSHIFUJI Hideaki3eb84f42008-04-10 15:42:08 +0900901{
Eric Dumazetddbe5032012-07-18 08:11:12 +0000902 return hash_32(ipv6_addr_hash(addr), IN6_ADDR_HSIZE_SHIFT);
YOSHIFUJI Hideaki3eb84f42008-04-10 15:42:08 +0900903}
904
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905/* On success it returns ifp with increased reference count */
906
907static struct inet6_ifaddr *
Jiri Pirko3f8f5292013-08-01 10:41:27 +0200908ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
909 const struct in6_addr *peer_addr, int pfxlen,
Jiri Benc8a226b22013-08-01 10:41:28 +0200910 int scope, u32 flags, u32 valid_lft, u32 prefered_lft)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911{
912 struct inet6_ifaddr *ifa = NULL;
913 struct rt6_info *rt;
stephen hemminger3a88a812010-03-17 20:31:12 +0000914 unsigned int hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 int err = 0;
YOSHIFUJI Hideakid68b8272008-06-25 16:26:47 +0900916 int addr_type = ipv6_addr_type(addr);
917
918 if (addr_type == IPV6_ADDR_ANY ||
919 addr_type & IPV6_ADDR_MULTICAST ||
920 (!(idev->dev->flags & IFF_LOOPBACK) &&
921 addr_type & IPV6_ADDR_LOOPBACK))
922 return ERR_PTR(-EADDRNOTAVAIL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -0700924 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 if (idev->dead) {
926 err = -ENODEV; /*XXX*/
927 goto out2;
928 }
929
Brian Haley56d417b2009-06-01 03:07:33 -0700930 if (idev->cnf.disable_ipv6) {
Brian Haley9bdd8d42009-03-18 18:22:48 -0700931 err = -EACCES;
932 goto out2;
933 }
934
stephen hemminger5c578ae2010-03-17 20:31:11 +0000935 spin_lock(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
937 /* Ignore adding duplicate addresses on an interface */
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900938 if (ipv6_chk_same_addr(dev_net(idev->dev), addr, idev->dev)) {
dingtianhongba3542e2013-08-17 10:27:04 +0800939 ADBG("ipv6_add_addr: already assigned\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 err = -EEXIST;
941 goto out;
942 }
943
Ingo Oeser322f74a2006-03-20 23:01:47 -0800944 ifa = kzalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
Ian Morris63159f22015-03-29 14:00:04 +0100946 if (!ifa) {
dingtianhongba3542e2013-08-17 10:27:04 +0800947 ADBG("ipv6_add_addr: malloc failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 err = -ENOBUFS;
949 goto out;
950 }
951
David S. Miller8f031512011-12-06 16:48:14 -0500952 rt = addrconf_dst_alloc(idev, addr, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 if (IS_ERR(rt)) {
954 err = PTR_ERR(rt);
955 goto out;
956 }
957
Jiri Pirkobba24892013-12-07 19:26:57 +0100958 neigh_parms_data_state_setall(idev->nd_parms);
959
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000960 ifa->addr = *addr;
Jiri Pirko3f8f5292013-08-01 10:41:27 +0200961 if (peer_addr)
962 ifa->peer_addr = *peer_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
964 spin_lock_init(&ifa->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100965 INIT_DELAYED_WORK(&ifa->dad_work, addrconf_dad_work);
stephen hemmingerc2e21292010-03-17 20:31:10 +0000966 INIT_HLIST_NODE(&ifa->addr_lst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 ifa->scope = scope;
968 ifa->prefix_len = pfxlen;
969 ifa->flags = flags | IFA_F_TENTATIVE;
Jiri Benc8a226b22013-08-01 10:41:28 +0200970 ifa->valid_lft = valid_lft;
971 ifa->prefered_lft = prefered_lft;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 ifa->cstamp = ifa->tstamp = jiffies;
Daniel Borkmann617fe292013-04-09 03:47:16 +0000973 ifa->tokenized = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
Keir Fraser57f5f542006-08-29 02:43:49 -0700975 ifa->rt = rt;
976
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 ifa->idev = idev;
978 in6_dev_hold(idev);
979 /* For caller */
980 in6_ifa_hold(ifa);
981
982 /* Add to big hash table */
Eric Dumazetddbe5032012-07-18 08:11:12 +0000983 hash = inet6_addr_hash(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
stephen hemminger5c578ae2010-03-17 20:31:11 +0000985 hlist_add_head_rcu(&ifa->addr_lst, &inet6_addr_lst[hash]);
stephen hemminger5c578ae2010-03-17 20:31:11 +0000986 spin_unlock(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
988 write_lock(&idev->lock);
989 /* Add to inet6_dev unicast addr list. */
Brian Haleye55ffac2006-07-10 15:25:51 -0700990 ipv6_link_dev_addr(idev, ifa);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 if (ifa->flags&IFA_F_TEMPORARY) {
stephen hemminger372e6c82010-03-17 20:31:09 +0000993 list_add(&ifa->tmp_list, &idev->tempaddr_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 in6_ifa_hold(ifa);
995 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 in6_ifa_hold(ifa);
998 write_unlock(&idev->lock);
999out2:
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001000 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
YOSHIFUJI Hideakifd928332005-04-19 22:27:09 -07001002 if (likely(err == 0))
Cong Wangf88c91d2013-04-14 23:18:43 +08001003 inet6addr_notifier_call_chain(NETDEV_UP, ifa);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 else {
1005 kfree(ifa);
1006 ifa = ERR_PTR(err);
1007 }
1008
1009 return ifa;
1010out:
stephen hemminger5c578ae2010-03-17 20:31:11 +00001011 spin_unlock(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 goto out2;
1013}
1014
Thomas Haller5b84efe2014-01-15 15:36:59 +01001015enum cleanup_prefix_rt_t {
1016 CLEANUP_PREFIX_RT_NOP, /* no cleanup action for prefix route */
1017 CLEANUP_PREFIX_RT_DEL, /* delete the prefix route */
1018 CLEANUP_PREFIX_RT_EXPIRE, /* update the lifetime of the prefix route */
1019};
1020
1021/*
1022 * Check, whether the prefix for ifp would still need a prefix route
1023 * after deleting ifp. The function returns one of the CLEANUP_PREFIX_RT_*
1024 * constants.
1025 *
1026 * 1) we don't purge prefix if address was not permanent.
1027 * prefix is managed by its own lifetime.
1028 * 2) we also don't purge, if the address was IFA_F_NOPREFIXROUTE.
1029 * 3) if there are no addresses, delete prefix.
1030 * 4) if there are still other permanent address(es),
1031 * corresponding prefix is still permanent.
1032 * 5) if there are still other addresses with IFA_F_NOPREFIXROUTE,
1033 * don't purge the prefix, assume user space is managing it.
1034 * 6) otherwise, update prefix lifetime to the
1035 * longest valid lifetime among the corresponding
1036 * addresses on the device.
1037 * Note: subsequent RA will update lifetime.
1038 **/
1039static enum cleanup_prefix_rt_t
1040check_cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long *expires)
1041{
1042 struct inet6_ifaddr *ifa;
1043 struct inet6_dev *idev = ifp->idev;
1044 unsigned long lifetime;
1045 enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_DEL;
1046
1047 *expires = jiffies;
1048
1049 list_for_each_entry(ifa, &idev->addr_list, if_list) {
1050 if (ifa == ifp)
1051 continue;
1052 if (!ipv6_prefix_equal(&ifa->addr, &ifp->addr,
1053 ifp->prefix_len))
1054 continue;
1055 if (ifa->flags & (IFA_F_PERMANENT | IFA_F_NOPREFIXROUTE))
1056 return CLEANUP_PREFIX_RT_NOP;
1057
1058 action = CLEANUP_PREFIX_RT_EXPIRE;
1059
1060 spin_lock(&ifa->lock);
1061
1062 lifetime = addrconf_timeout_fixup(ifa->valid_lft, HZ);
1063 /*
1064 * Note: Because this address is
1065 * not permanent, lifetime <
1066 * LONG_MAX / HZ here.
1067 */
1068 if (time_before(*expires, ifa->tstamp + lifetime * HZ))
1069 *expires = ifa->tstamp + lifetime * HZ;
1070 spin_unlock(&ifa->lock);
1071 }
1072
1073 return action;
1074}
1075
1076static void
1077cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long expires, bool del_rt)
1078{
1079 struct rt6_info *rt;
1080
1081 rt = addrconf_get_prefix_route(&ifp->addr,
1082 ifp->prefix_len,
1083 ifp->idev->dev,
1084 0, RTF_GATEWAY | RTF_DEFAULT);
1085 if (rt) {
1086 if (del_rt)
1087 ip6_del_rt(rt);
1088 else {
1089 if (!(rt->rt6i_flags & RTF_EXPIRES))
1090 rt6_set_expires(rt, expires);
1091 ip6_rt_put(rt);
1092 }
1093 }
1094}
1095
1096
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097/* This function wants to get referenced ifp and releases it before return */
1098
1099static void ipv6_del_addr(struct inet6_ifaddr *ifp)
1100{
Herbert Xu4c5ff6a2010-05-18 15:54:18 -07001101 int state;
Thomas Haller5b84efe2014-01-15 15:36:59 +01001102 enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_NOP;
1103 unsigned long expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01001105 ASSERT_RTNL();
1106
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01001107 spin_lock_bh(&ifp->lock);
Herbert Xu4c5ff6a2010-05-18 15:54:18 -07001108 state = ifp->state;
Herbert Xue9d3e082010-05-18 15:36:06 -07001109 ifp->state = INET6_IFADDR_STATE_DEAD;
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01001110 spin_unlock_bh(&ifp->lock);
Herbert Xu4c5ff6a2010-05-18 15:54:18 -07001111
1112 if (state == INET6_IFADDR_STATE_DEAD)
1113 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
stephen hemminger5c578ae2010-03-17 20:31:11 +00001115 spin_lock_bh(&addrconf_hash_lock);
1116 hlist_del_init_rcu(&ifp->addr_lst);
stephen hemminger5c578ae2010-03-17 20:31:11 +00001117 spin_unlock_bh(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
Thomas Haller5b84efe2014-01-15 15:36:59 +01001119 write_lock_bh(&ifp->idev->lock);
David S. Miller5d9efa72013-10-28 20:07:50 -04001120
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 if (ifp->flags&IFA_F_TEMPORARY) {
stephen hemminger372e6c82010-03-17 20:31:09 +00001122 list_del(&ifp->tmp_list);
1123 if (ifp->ifpub) {
1124 in6_ifa_put(ifp->ifpub);
1125 ifp->ifpub = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 }
stephen hemminger372e6c82010-03-17 20:31:09 +00001127 __in6_ifa_put(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
Thomas Haller5b84efe2014-01-15 15:36:59 +01001130 if (ifp->flags & IFA_F_PERMANENT && !(ifp->flags & IFA_F_NOPREFIXROUTE))
1131 action = check_cleanup_prefix_route(ifp, &expires);
stephen hemminger502a2ff2010-03-17 20:31:13 +00001132
Thomas Haller5b84efe2014-01-15 15:36:59 +01001133 list_del_init(&ifp->if_list);
1134 __in6_ifa_put(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
Thomas Haller5b84efe2014-01-15 15:36:59 +01001136 write_unlock_bh(&ifp->idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01001138 addrconf_del_dad_work(ifp);
Andrey Vaginb2238562008-07-08 15:13:31 -07001139
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 ipv6_ifa_notify(RTM_DELADDR, ifp);
1141
Cong Wangf88c91d2013-04-14 23:18:43 +08001142 inet6addr_notifier_call_chain(NETDEV_DOWN, ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
Thomas Haller5b84efe2014-01-15 15:36:59 +01001144 if (action != CLEANUP_PREFIX_RT_NOP) {
1145 cleanup_prefix_route(ifp, expires,
1146 action == CLEANUP_PREFIX_RT_DEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 }
1148
Daniel Walterc3968a82011-04-13 21:10:57 +00001149 /* clean up prefsrc entries */
1150 rt6_remove_prefsrc(ifp);
Herbert Xu4c5ff6a2010-05-18 15:54:18 -07001151out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 in6_ifa_put(ifp);
1153}
1154
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift)
1156{
1157 struct inet6_dev *idev = ifp->idev;
1158 struct in6_addr addr, *tmpaddr;
Lorenzo Colitti76f793e2011-07-26 13:50:49 +00001159 unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_tstamp, age;
Benoit Boissinoteac55bf2008-04-02 00:01:35 -07001160 unsigned long regen_advance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 int tmp_plen;
1162 int ret = 0;
Neil Horman95c385b2007-04-25 17:08:10 -07001163 u32 addr_flags;
Lorenzo Colitti76f793e2011-07-26 13:50:49 +00001164 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
Jiri Pirko53bd6742013-12-06 09:45:22 +01001166 write_lock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 if (ift) {
1168 spin_lock_bh(&ift->lock);
1169 memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8);
1170 spin_unlock_bh(&ift->lock);
1171 tmpaddr = &addr;
1172 } else {
1173 tmpaddr = NULL;
1174 }
1175retry:
1176 in6_dev_hold(idev);
1177 if (idev->cnf.use_tempaddr <= 0) {
Jiri Pirko53bd6742013-12-06 09:45:22 +01001178 write_unlock_bh(&idev->lock);
Joe Perchesf3213832012-05-15 14:11:53 +00001179 pr_info("%s: use_tempaddr is disabled\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 in6_dev_put(idev);
1181 ret = -1;
1182 goto out;
1183 }
1184 spin_lock_bh(&ifp->lock);
1185 if (ifp->regen_count++ >= idev->cnf.regen_max_retry) {
1186 idev->cnf.use_tempaddr = -1; /*XXX*/
1187 spin_unlock_bh(&ifp->lock);
Jiri Pirko53bd6742013-12-06 09:45:22 +01001188 write_unlock_bh(&idev->lock);
Joe Perchesf3213832012-05-15 14:11:53 +00001189 pr_warn("%s: regeneration time exceeded - disabled temporary address support\n",
1190 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 in6_dev_put(idev);
1192 ret = -1;
1193 goto out;
1194 }
1195 in6_ifa_hold(ifp);
1196 memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
Sorin Dumitrueb7e0572012-08-30 02:01:45 +00001197 __ipv6_try_regen_rndid(idev, tmpaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 memcpy(&addr.s6_addr[8], idev->rndid, 8);
Lorenzo Colitti76f793e2011-07-26 13:50:49 +00001199 age = (now - ifp->tstamp) / HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 tmp_valid_lft = min_t(__u32,
1201 ifp->valid_lft,
Glenn Wurster7a876b02010-09-27 07:10:10 +00001202 idev->cnf.temp_valid_lft + age);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001203 tmp_prefered_lft = min_t(__u32,
1204 ifp->prefered_lft,
Glenn Wurster7a876b02010-09-27 07:10:10 +00001205 idev->cnf.temp_prefered_lft + age -
Ben Hutchings784e2712010-06-26 11:42:55 +00001206 idev->cnf.max_desync_factor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 tmp_plen = ifp->prefix_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 tmp_tstamp = ifp->tstamp;
1209 spin_unlock_bh(&ifp->lock);
1210
Benoit Boissinoteac55bf2008-04-02 00:01:35 -07001211 regen_advance = idev->cnf.regen_max_retry *
Ian Morris67ba4152014-08-24 21:53:10 +01001212 idev->cnf.dad_transmits *
1213 NEIGH_VAR(idev->nd_parms, RETRANS_TIME) / HZ;
Jiri Pirko53bd6742013-12-06 09:45:22 +01001214 write_unlock_bh(&idev->lock);
Neil Horman95c385b2007-04-25 17:08:10 -07001215
Benoit Boissinoteac55bf2008-04-02 00:01:35 -07001216 /* A temporary address is created only if this calculated Preferred
1217 * Lifetime is greater than REGEN_ADVANCE time units. In particular,
1218 * an implementation must not create a temporary address with a zero
1219 * Preferred Lifetime.
Heiner Kallweitecab6702014-03-12 22:13:19 +01001220 * Use age calculation as in addrconf_verify to avoid unnecessary
1221 * temporary addresses being generated.
Benoit Boissinoteac55bf2008-04-02 00:01:35 -07001222 */
Heiner Kallweitecab6702014-03-12 22:13:19 +01001223 age = (now - tmp_tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
1224 if (tmp_prefered_lft <= regen_advance + age) {
Benoit Boissinoteac55bf2008-04-02 00:01:35 -07001225 in6_ifa_put(ifp);
1226 in6_dev_put(idev);
1227 ret = -1;
1228 goto out;
1229 }
1230
Neil Horman95c385b2007-04-25 17:08:10 -07001231 addr_flags = IFA_F_TEMPORARY;
1232 /* set in addrconf_prefix_rcv() */
1233 if (ifp->flags & IFA_F_OPTIMISTIC)
1234 addr_flags |= IFA_F_OPTIMISTIC;
1235
Hannes Frederic Sowa4b08a8f2013-08-16 13:02:27 +02001236 ift = ipv6_add_addr(idev, &addr, NULL, tmp_plen,
1237 ipv6_addr_scope(&addr), addr_flags,
1238 tmp_valid_lft, tmp_prefered_lft);
1239 if (IS_ERR(ift)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 in6_ifa_put(ifp);
1241 in6_dev_put(idev);
Joe Perchesf3213832012-05-15 14:11:53 +00001242 pr_info("%s: retry temporary address regeneration\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 tmpaddr = &addr;
Jiri Pirko53bd6742013-12-06 09:45:22 +01001244 write_lock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 goto retry;
1246 }
1247
1248 spin_lock_bh(&ift->lock);
1249 ift->ifpub = ifp;
Lorenzo Colitti76f793e2011-07-26 13:50:49 +00001250 ift->cstamp = now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 ift->tstamp = tmp_tstamp;
1252 spin_unlock_bh(&ift->lock);
1253
David S. Millercf22f9a2012-04-14 21:37:40 -04001254 addrconf_dad_start(ift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 in6_ifa_put(ift);
1256 in6_dev_put(idev);
1257out:
1258 return ret;
1259}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
1261/*
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001262 * Choose an appropriate source address (RFC3484)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 */
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001264enum {
1265 IPV6_SADDR_RULE_INIT = 0,
1266 IPV6_SADDR_RULE_LOCAL,
1267 IPV6_SADDR_RULE_SCOPE,
1268 IPV6_SADDR_RULE_PREFERRED,
1269#ifdef CONFIG_IPV6_MIP6
1270 IPV6_SADDR_RULE_HOA,
1271#endif
1272 IPV6_SADDR_RULE_OIF,
1273 IPV6_SADDR_RULE_LABEL,
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001274 IPV6_SADDR_RULE_PRIVACY,
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001275 IPV6_SADDR_RULE_ORCHID,
1276 IPV6_SADDR_RULE_PREFIX,
Erik Kline7fd25612014-10-28 18:11:14 +09001277#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1278 IPV6_SADDR_RULE_NOT_OPTIMISTIC,
1279#endif
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001280 IPV6_SADDR_RULE_MAX
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001281};
1282
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001283struct ipv6_saddr_score {
1284 int rule;
1285 int addr_type;
1286 struct inet6_ifaddr *ifa;
1287 DECLARE_BITMAP(scorebits, IPV6_SADDR_RULE_MAX);
1288 int scopedist;
1289 int matchlen;
1290};
1291
1292struct ipv6_saddr_dst {
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +09001293 const struct in6_addr *addr;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001294 int ifindex;
1295 int scope;
1296 int label;
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001297 unsigned int prefs;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001298};
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001299
Dave Jonesb6f99a22007-03-22 12:27:49 -07001300static inline int ipv6_saddr_preferred(int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301{
Ulrich Weber45bb0062010-02-25 23:28:58 +00001302 if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|IPV6_ADDR_LOOPBACK))
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001303 return 1;
1304 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305}
1306
Erik Kline7fd25612014-10-28 18:11:14 +09001307static inline bool ipv6_use_optimistic_addr(struct inet6_dev *idev)
1308{
1309#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1310 return idev && idev->cnf.optimistic_dad && idev->cnf.use_optimistic;
1311#else
1312 return false;
1313#endif
1314}
1315
Benjamin Thery3de23252008-05-28 14:51:24 +02001316static int ipv6_get_saddr_eval(struct net *net,
1317 struct ipv6_saddr_score *score,
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001318 struct ipv6_saddr_dst *dst,
1319 int i)
1320{
1321 int ret;
1322
1323 if (i <= score->rule) {
1324 switch (i) {
1325 case IPV6_SADDR_RULE_SCOPE:
1326 ret = score->scopedist;
1327 break;
1328 case IPV6_SADDR_RULE_PREFIX:
1329 ret = score->matchlen;
1330 break;
1331 default:
1332 ret = !!test_bit(i, score->scorebits);
1333 }
1334 goto out;
1335 }
1336
1337 switch (i) {
1338 case IPV6_SADDR_RULE_INIT:
1339 /* Rule 0: remember if hiscore is not ready yet */
1340 ret = !!score->ifa;
1341 break;
1342 case IPV6_SADDR_RULE_LOCAL:
1343 /* Rule 1: Prefer same address */
1344 ret = ipv6_addr_equal(&score->ifa->addr, dst->addr);
1345 break;
1346 case IPV6_SADDR_RULE_SCOPE:
1347 /* Rule 2: Prefer appropriate scope
1348 *
1349 * ret
1350 * ^
1351 * -1 | d 15
1352 * ---+--+-+---> scope
1353 * |
1354 * | d is scope of the destination.
1355 * B-d | \
1356 * | \ <- smaller scope is better if
stephen hemmingerdb9c7c32014-01-12 11:26:32 -08001357 * B-15 | \ if scope is enough for destination.
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001358 * | ret = B - scope (-1 <= scope >= d <= 15).
1359 * d-C-1 | /
1360 * |/ <- greater is better
1361 * -C / if scope is not enough for destination.
1362 * /| ret = scope - C (-1 <= d < scope <= 15).
1363 *
1364 * d - C - 1 < B -15 (for all -1 <= d <= 15).
1365 * C > d + 14 - B >= 15 + 14 - B = 29 - B.
1366 * Assume B = 0 and we get C > 29.
1367 */
1368 ret = __ipv6_addr_src_scope(score->addr_type);
1369 if (ret >= dst->scope)
1370 ret = -ret;
1371 else
1372 ret -= 128; /* 30 is enough */
1373 score->scopedist = ret;
1374 break;
1375 case IPV6_SADDR_RULE_PREFERRED:
Erik Kline7fd25612014-10-28 18:11:14 +09001376 {
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001377 /* Rule 3: Avoid deprecated and optimistic addresses */
Erik Kline7fd25612014-10-28 18:11:14 +09001378 u8 avoid = IFA_F_DEPRECATED;
1379
1380 if (!ipv6_use_optimistic_addr(score->ifa->idev))
1381 avoid |= IFA_F_OPTIMISTIC;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001382 ret = ipv6_saddr_preferred(score->addr_type) ||
Erik Kline7fd25612014-10-28 18:11:14 +09001383 !(score->ifa->flags & avoid);
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001384 break;
Erik Kline7fd25612014-10-28 18:11:14 +09001385 }
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001386#ifdef CONFIG_IPV6_MIP6
1387 case IPV6_SADDR_RULE_HOA:
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001388 {
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001389 /* Rule 4: Prefer home address */
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001390 int prefhome = !(dst->prefs & IPV6_PREFER_SRC_COA);
1391 ret = !(score->ifa->flags & IFA_F_HOMEADDRESS) ^ prefhome;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001392 break;
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001393 }
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001394#endif
1395 case IPV6_SADDR_RULE_OIF:
1396 /* Rule 5: Prefer outgoing interface */
1397 ret = (!dst->ifindex ||
1398 dst->ifindex == score->ifa->idev->dev->ifindex);
1399 break;
1400 case IPV6_SADDR_RULE_LABEL:
1401 /* Rule 6: Prefer matching label */
Benjamin Thery3de23252008-05-28 14:51:24 +02001402 ret = ipv6_addr_label(net,
1403 &score->ifa->addr, score->addr_type,
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001404 score->ifa->idev->dev->ifindex) == dst->label;
1405 break;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001406 case IPV6_SADDR_RULE_PRIVACY:
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001407 {
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001408 /* Rule 7: Prefer public address
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001409 * Note: prefer temporary address if use_tempaddr >= 2
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001410 */
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001411 int preftmp = dst->prefs & (IPV6_PREFER_SRC_PUBLIC|IPV6_PREFER_SRC_TMP) ?
1412 !!(dst->prefs & IPV6_PREFER_SRC_TMP) :
1413 score->ifa->idev->cnf.use_tempaddr >= 2;
1414 ret = (!(score->ifa->flags & IFA_F_TEMPORARY)) ^ preftmp;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001415 break;
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001416 }
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001417 case IPV6_SADDR_RULE_ORCHID:
1418 /* Rule 8-: Prefer ORCHID vs ORCHID or
1419 * non-ORCHID vs non-ORCHID
1420 */
1421 ret = !(ipv6_addr_orchid(&score->ifa->addr) ^
1422 ipv6_addr_orchid(dst->addr));
1423 break;
1424 case IPV6_SADDR_RULE_PREFIX:
1425 /* Rule 8: Use longest matching prefix */
YOSHIFUJI Hideaki / 吉藤英明91b4b042012-09-10 18:41:07 +00001426 ret = ipv6_addr_diff(&score->ifa->addr, dst->addr);
1427 if (ret > score->ifa->prefix_len)
1428 ret = score->ifa->prefix_len;
1429 score->matchlen = ret;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001430 break;
Erik Kline7fd25612014-10-28 18:11:14 +09001431#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1432 case IPV6_SADDR_RULE_NOT_OPTIMISTIC:
1433 /* Optimistic addresses still have lower precedence than other
1434 * preferred addresses.
1435 */
1436 ret = !(score->ifa->flags & IFA_F_OPTIMISTIC);
1437 break;
1438#endif
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001439 default:
1440 ret = 0;
1441 }
1442
1443 if (ret)
1444 __set_bit(i, score->scorebits);
1445 score->rule = i;
1446out:
1447 return ret;
1448}
1449
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001450static int __ipv6_dev_get_saddr(struct net *net,
1451 struct ipv6_saddr_dst *dst,
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001452 struct inet6_dev *idev,
1453 struct ipv6_saddr_score *scores,
1454 int hiscore_idx)
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001455{
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001456 struct ipv6_saddr_score *score = &scores[1 - hiscore_idx], *hiscore = &scores[hiscore_idx];
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001457
1458 read_lock_bh(&idev->lock);
1459 list_for_each_entry(score->ifa, &idev->addr_list, if_list) {
1460 int i;
1461
1462 /*
1463 * - Tentative Address (RFC2462 section 5.4)
1464 * - A tentative address is not considered
1465 * "assigned to an interface" in the traditional
1466 * sense, unless it is also flagged as optimistic.
1467 * - Candidate Source Address (section 4)
1468 * - In any case, anycast addresses, multicast
1469 * addresses, and the unspecified address MUST
1470 * NOT be included in a candidate set.
1471 */
1472 if ((score->ifa->flags & IFA_F_TENTATIVE) &&
1473 (!(score->ifa->flags & IFA_F_OPTIMISTIC)))
1474 continue;
1475
1476 score->addr_type = __ipv6_addr_type(&score->ifa->addr);
1477
1478 if (unlikely(score->addr_type == IPV6_ADDR_ANY ||
1479 score->addr_type & IPV6_ADDR_MULTICAST)) {
1480 net_dbg_ratelimited("ADDRCONF: unspecified / multicast address assigned as unicast address on %s",
1481 idev->dev->name);
1482 continue;
1483 }
1484
1485 score->rule = -1;
1486 bitmap_zero(score->scorebits, IPV6_SADDR_RULE_MAX);
1487
1488 for (i = 0; i < IPV6_SADDR_RULE_MAX; i++) {
1489 int minihiscore, miniscore;
1490
1491 minihiscore = ipv6_get_saddr_eval(net, hiscore, dst, i);
1492 miniscore = ipv6_get_saddr_eval(net, score, dst, i);
1493
1494 if (minihiscore > miniscore) {
1495 if (i == IPV6_SADDR_RULE_SCOPE &&
1496 score->scopedist > 0) {
1497 /*
1498 * special case:
1499 * each remaining entry
1500 * has too small (not enough)
1501 * scope, because ifa entries
1502 * are sorted by their scope
1503 * values.
1504 */
1505 goto out;
1506 }
1507 break;
1508 } else if (minihiscore < miniscore) {
1509 if (hiscore->ifa)
1510 in6_ifa_put(hiscore->ifa);
1511
1512 in6_ifa_hold(score->ifa);
1513
1514 swap(hiscore, score);
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001515 hiscore_idx = 1 - hiscore_idx;
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001516
1517 /* restore our iterator */
1518 score->ifa = hiscore->ifa;
1519
1520 break;
1521 }
1522 }
1523 }
1524out:
1525 read_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001526 return hiscore_idx;
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001527}
1528
David Ahernafbac6012016-06-16 16:24:26 -07001529static int ipv6_get_saddr_master(struct net *net,
1530 const struct net_device *dst_dev,
1531 const struct net_device *master,
1532 struct ipv6_saddr_dst *dst,
1533 struct ipv6_saddr_score *scores,
1534 int hiscore_idx)
1535{
1536 struct inet6_dev *idev;
1537
1538 idev = __in6_dev_get(dst_dev);
1539 if (idev)
1540 hiscore_idx = __ipv6_dev_get_saddr(net, dst, idev,
1541 scores, hiscore_idx);
1542
1543 idev = __in6_dev_get(master);
1544 if (idev)
1545 hiscore_idx = __ipv6_dev_get_saddr(net, dst, idev,
1546 scores, hiscore_idx);
1547
1548 return hiscore_idx;
1549}
1550
Patrick McHardyb3f644f2012-08-26 19:14:14 +02001551int ipv6_dev_get_saddr(struct net *net, const struct net_device *dst_dev,
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +09001552 const struct in6_addr *daddr, unsigned int prefs,
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001553 struct in6_addr *saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554{
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001555 struct ipv6_saddr_score scores[2], *hiscore;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001556 struct ipv6_saddr_dst dst;
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001557 struct inet6_dev *idev;
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001558 struct net_device *dev;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001559 int dst_type;
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001560 bool use_oif_addr = false;
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001561 int hiscore_idx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001563 dst_type = __ipv6_addr_type(daddr);
1564 dst.addr = daddr;
1565 dst.ifindex = dst_dev ? dst_dev->ifindex : 0;
1566 dst.scope = __ipv6_addr_src_scope(dst_type);
Benjamin Thery3de23252008-05-28 14:51:24 +02001567 dst.label = ipv6_addr_label(net, daddr, dst_type, dst.ifindex);
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001568 dst.prefs = prefs;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001569
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001570 scores[hiscore_idx].rule = -1;
1571 scores[hiscore_idx].ifa = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001573 rcu_read_lock();
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001574
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001575 /* Candidate Source Address (section 4)
1576 * - multicast and link-local destination address,
1577 * the set of candidate source address MUST only
1578 * include addresses assigned to interfaces
1579 * belonging to the same link as the outgoing
1580 * interface.
1581 * (- For site-local destination addresses, the
1582 * set of candidate source addresses MUST only
1583 * include addresses assigned to interfaces
1584 * belonging to the same site as the outgoing
1585 * interface.)
Erik Kline3985e8a2015-07-22 16:38:25 +09001586 * - "It is RECOMMENDED that the candidate source addresses
1587 * be the set of unicast addresses assigned to the
1588 * interface that will be used to send to the destination
1589 * (the 'outgoing' interface)." (RFC 6724)
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001590 */
1591 if (dst_dev) {
Erik Kline3985e8a2015-07-22 16:38:25 +09001592 idev = __in6_dev_get(dst_dev);
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001593 if ((dst_type & IPV6_ADDR_MULTICAST) ||
Erik Kline3985e8a2015-07-22 16:38:25 +09001594 dst.scope <= IPV6_ADDR_SCOPE_LINKLOCAL ||
1595 (idev && idev->cnf.use_oif_addrs_only)) {
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001596 use_oif_addr = true;
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001597 }
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001598 }
1599
1600 if (use_oif_addr) {
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001601 if (idev)
YOSHIFUJI Hideakic15df302015-07-16 16:51:30 +09001602 hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx);
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001603 } else {
David Ahernafbac6012016-06-16 16:24:26 -07001604 const struct net_device *master;
1605 int master_idx = 0;
1606
1607 /* if dst_dev exists and is enslaved to an L3 device, then
1608 * prefer addresses from dst_dev and then the master over
1609 * any other enslaved devices in the L3 domain.
1610 */
1611 master = l3mdev_master_dev_rcu(dst_dev);
1612 if (master) {
1613 master_idx = master->ifindex;
1614
1615 hiscore_idx = ipv6_get_saddr_master(net, dst_dev,
1616 master, &dst,
1617 scores, hiscore_idx);
1618
1619 if (scores[hiscore_idx].ifa)
1620 goto out;
1621 }
1622
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001623 for_each_netdev_rcu(net, dev) {
David Ahernafbac6012016-06-16 16:24:26 -07001624 /* only consider addresses on devices in the
1625 * same L3 domain
1626 */
1627 if (l3mdev_master_ifindex_rcu(dev) != master_idx)
1628 continue;
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001629 idev = __in6_dev_get(dev);
1630 if (!idev)
1631 continue;
YOSHIFUJI Hideakic15df302015-07-16 16:51:30 +09001632 hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx);
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001633 }
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001634 }
David Ahernafbac6012016-06-16 16:24:26 -07001635
1636out:
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001637 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001639 hiscore = &scores[hiscore_idx];
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001640 if (!hiscore->ifa)
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001641 return -EADDRNOTAVAIL;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001642
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001643 *saddr = hiscore->ifa->addr;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001644 in6_ifa_put(hiscore->ifa);
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001645 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646}
YOSHIFUJI Hideaki5e5f3f02008-03-03 21:44:34 +09001647EXPORT_SYMBOL(ipv6_dev_get_saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648
Amerigo Wang89657792013-06-29 21:30:49 +08001649int __ipv6_get_lladdr(struct inet6_dev *idev, struct in6_addr *addr,
Jiri Pirko479840f2013-12-06 09:45:21 +01001650 u32 banned_flags)
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02001651{
1652 struct inet6_ifaddr *ifp;
1653 int err = -EADDRNOTAVAIL;
1654
Hannes Frederic Sowa602582c2014-01-19 21:58:19 +01001655 list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) {
1656 if (ifp->scope > IFA_LINK)
1657 break;
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02001658 if (ifp->scope == IFA_LINK &&
1659 !(ifp->flags & banned_flags)) {
1660 *addr = ifp->addr;
1661 err = 0;
1662 break;
1663 }
1664 }
1665 return err;
1666}
1667
Neil Horman95c385b2007-04-25 17:08:10 -07001668int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr,
Jiri Pirko479840f2013-12-06 09:45:21 +01001669 u32 banned_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670{
1671 struct inet6_dev *idev;
1672 int err = -EADDRNOTAVAIL;
1673
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001674 rcu_read_lock();
Stephen Hemmingere21e8462010-03-20 16:09:01 -07001675 idev = __in6_dev_get(dev);
1676 if (idev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 read_lock_bh(&idev->lock);
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02001678 err = __ipv6_get_lladdr(idev, addr, banned_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 read_unlock_bh(&idev->lock);
1680 }
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001681 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 return err;
1683}
1684
1685static int ipv6_count_addresses(struct inet6_dev *idev)
1686{
1687 int cnt = 0;
1688 struct inet6_ifaddr *ifp;
1689
1690 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00001691 list_for_each_entry(ifp, &idev->addr_list, if_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 cnt++;
1693 read_unlock_bh(&idev->lock);
1694 return cnt;
1695}
1696
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001697int ipv6_chk_addr(struct net *net, const struct in6_addr *addr,
Florian Westphal2a7851b2013-05-17 03:56:10 +00001698 const struct net_device *dev, int strict)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699{
Erik Klinec58da4c2015-02-04 20:01:23 +09001700 return ipv6_chk_addr_and_flags(net, addr, dev, strict, IFA_F_TENTATIVE);
1701}
1702EXPORT_SYMBOL(ipv6_chk_addr);
1703
1704int ipv6_chk_addr_and_flags(struct net *net, const struct in6_addr *addr,
1705 const struct net_device *dev, int strict,
1706 u32 banned_flags)
1707{
Stephen Hemmingereedf0422010-05-17 22:27:12 -07001708 struct inet6_ifaddr *ifp;
Eric Dumazetddbe5032012-07-18 08:11:12 +00001709 unsigned int hash = inet6_addr_hash(addr);
Erik Klinec58da4c2015-02-04 20:01:23 +09001710 u32 ifp_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711
stephen hemminger5c578ae2010-03-17 20:31:11 +00001712 rcu_read_lock_bh();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001713 hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09001714 if (!net_eq(dev_net(ifp->idev->dev), net))
Daniel Lezcanobfeade02008-01-10 22:43:18 -08001715 continue;
Erik Klinec58da4c2015-02-04 20:01:23 +09001716 /* Decouple optimistic from tentative for evaluation here.
1717 * Ban optimistic addresses explicitly, when required.
1718 */
1719 ifp_flags = (ifp->flags&IFA_F_OPTIMISTIC)
1720 ? (ifp->flags&~IFA_F_TENTATIVE)
1721 : ifp->flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 if (ipv6_addr_equal(&ifp->addr, addr) &&
Erik Klinec58da4c2015-02-04 20:01:23 +09001723 !(ifp_flags&banned_flags) &&
Ian Morris63159f22015-03-29 14:00:04 +01001724 (!dev || ifp->idev->dev == dev ||
Stephen Hemmingereedf0422010-05-17 22:27:12 -07001725 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))) {
1726 rcu_read_unlock_bh();
1727 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 }
1729 }
stephen hemminger5c578ae2010-03-17 20:31:11 +00001730
Stephen Hemmingereedf0422010-05-17 22:27:12 -07001731 rcu_read_unlock_bh();
1732 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733}
Erik Klinec58da4c2015-02-04 20:01:23 +09001734EXPORT_SYMBOL(ipv6_chk_addr_and_flags);
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +09001735
David S. Miller3e81c6d2010-03-20 16:18:00 -07001736static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
1737 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738{
Eric Dumazetddbe5032012-07-18 08:11:12 +00001739 unsigned int hash = inet6_addr_hash(addr);
stephen hemmingerc2e21292010-03-17 20:31:10 +00001740 struct inet6_ifaddr *ifp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741
Sasha Levinb67bfe02013-02-27 17:06:00 -08001742 hlist_for_each_entry(ifp, &inet6_addr_lst[hash], addr_lst) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09001743 if (!net_eq(dev_net(ifp->idev->dev), net))
Daniel Lezcano06bfe652008-01-10 22:43:42 -08001744 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 if (ipv6_addr_equal(&ifp->addr, addr)) {
Ian Morris63159f22015-03-29 14:00:04 +01001746 if (!dev || ifp->idev->dev == dev)
David S. Miller3e81c6d2010-03-20 16:18:00 -07001747 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 }
1749 }
David S. Miller3e81c6d2010-03-20 16:18:00 -07001750 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751}
1752
Catalin\(ux\) M. BOIE7df37ff2013-09-23 23:04:19 +03001753/* Compares an address/prefix_len with addresses on device @dev.
1754 * If one is found it returns true.
1755 */
1756bool ipv6_chk_custom_prefix(const struct in6_addr *addr,
1757 const unsigned int prefix_len, struct net_device *dev)
1758{
1759 struct inet6_dev *idev;
1760 struct inet6_ifaddr *ifa;
1761 bool ret = false;
1762
1763 rcu_read_lock();
1764 idev = __in6_dev_get(dev);
1765 if (idev) {
1766 read_lock_bh(&idev->lock);
1767 list_for_each_entry(ifa, &idev->addr_list, if_list) {
1768 ret = ipv6_prefix_equal(addr, &ifa->addr, prefix_len);
1769 if (ret)
1770 break;
1771 }
1772 read_unlock_bh(&idev->lock);
1773 }
1774 rcu_read_unlock();
1775
1776 return ret;
1777}
1778EXPORT_SYMBOL(ipv6_chk_custom_prefix);
1779
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001780int ipv6_chk_prefix(const struct in6_addr *addr, struct net_device *dev)
YOSHIFUJI Hideaki52eeeb82008-03-15 22:54:23 -04001781{
1782 struct inet6_dev *idev;
1783 struct inet6_ifaddr *ifa;
1784 int onlink;
1785
1786 onlink = 0;
1787 rcu_read_lock();
1788 idev = __in6_dev_get(dev);
1789 if (idev) {
1790 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00001791 list_for_each_entry(ifa, &idev->addr_list, if_list) {
YOSHIFUJI Hideaki52eeeb82008-03-15 22:54:23 -04001792 onlink = ipv6_prefix_equal(addr, &ifa->addr,
1793 ifa->prefix_len);
1794 if (onlink)
1795 break;
1796 }
1797 read_unlock_bh(&idev->lock);
1798 }
1799 rcu_read_unlock();
1800 return onlink;
1801}
YOSHIFUJI Hideaki52eeeb82008-03-15 22:54:23 -04001802EXPORT_SYMBOL(ipv6_chk_prefix);
1803
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +09001804struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, const struct in6_addr *addr,
Daniel Lezcano1cab3da2008-01-10 22:44:09 -08001805 struct net_device *dev, int strict)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806{
David S. Millerb79d1d52010-03-25 21:39:21 -07001807 struct inet6_ifaddr *ifp, *result = NULL;
Eric Dumazetddbe5032012-07-18 08:11:12 +00001808 unsigned int hash = inet6_addr_hash(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809
stephen hemminger5c578ae2010-03-17 20:31:11 +00001810 rcu_read_lock_bh();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001811 hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09001812 if (!net_eq(dev_net(ifp->idev->dev), net))
Daniel Lezcano1cab3da2008-01-10 22:44:09 -08001813 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 if (ipv6_addr_equal(&ifp->addr, addr)) {
Ian Morris63159f22015-03-29 14:00:04 +01001815 if (!dev || ifp->idev->dev == dev ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
David S. Millerb79d1d52010-03-25 21:39:21 -07001817 result = ifp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 in6_ifa_hold(ifp);
1819 break;
1820 }
1821 }
1822 }
stephen hemminger5c578ae2010-03-17 20:31:11 +00001823 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824
David S. Millerb79d1d52010-03-25 21:39:21 -07001825 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826}
1827
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828/* Gets referenced address, destroys ifaddr */
1829
Brian Haleycc411d02009-09-09 14:41:32 +00001830static void addrconf_dad_stop(struct inet6_ifaddr *ifp, int dad_failed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831{
Lubomir Rintel3d171f32016-01-08 13:47:23 +01001832 if (dad_failed)
1833 ifp->flags |= IFA_F_DADFAILED;
1834
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 if (ifp->flags&IFA_F_PERMANENT) {
1836 spin_lock_bh(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01001837 addrconf_del_dad_work(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 ifp->flags |= IFA_F_TENTATIVE;
1839 spin_unlock_bh(&ifp->lock);
Herbert Xue2577a02010-03-13 12:23:29 -08001840 if (dad_failed)
1841 ipv6_ifa_notify(0, ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 in6_ifa_put(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 } else if (ifp->flags&IFA_F_TEMPORARY) {
1844 struct inet6_ifaddr *ifpub;
1845 spin_lock_bh(&ifp->lock);
1846 ifpub = ifp->ifpub;
1847 if (ifpub) {
1848 in6_ifa_hold(ifpub);
1849 spin_unlock_bh(&ifp->lock);
1850 ipv6_create_tempaddr(ifpub, ifp);
1851 in6_ifa_put(ifpub);
1852 } else {
1853 spin_unlock_bh(&ifp->lock);
1854 }
1855 ipv6_del_addr(ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01001856 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 ipv6_del_addr(ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01001858 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859}
1860
Herbert Xuf2344a12010-05-18 15:55:27 -07001861static int addrconf_dad_end(struct inet6_ifaddr *ifp)
1862{
1863 int err = -ENOENT;
1864
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01001865 spin_lock_bh(&ifp->lock);
Herbert Xuf2344a12010-05-18 15:55:27 -07001866 if (ifp->state == INET6_IFADDR_STATE_DAD) {
1867 ifp->state = INET6_IFADDR_STATE_POSTDAD;
1868 err = 0;
1869 }
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01001870 spin_unlock_bh(&ifp->lock);
Herbert Xuf2344a12010-05-18 15:55:27 -07001871
1872 return err;
1873}
1874
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09001875void addrconf_dad_failure(struct inet6_ifaddr *ifp)
1876{
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01001877 struct in6_addr addr;
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09001878 struct inet6_dev *idev = ifp->idev;
Hannes Frederic Sowa1855b7c2015-03-23 23:36:05 +01001879 struct net *net = dev_net(ifp->idev->dev);
Brian Haley9bdd8d42009-03-18 18:22:48 -07001880
Ursula Braun853dc2e2010-10-24 23:06:43 +00001881 if (addrconf_dad_end(ifp)) {
1882 in6_ifa_put(ifp);
Herbert Xuf2344a12010-05-18 15:55:27 -07001883 return;
Ursula Braun853dc2e2010-10-24 23:06:43 +00001884 }
Herbert Xuf2344a12010-05-18 15:55:27 -07001885
Joe Perchese87cc472012-05-13 21:56:26 +00001886 net_info_ratelimited("%s: IPv6 duplicate address %pI6c detected!\n",
1887 ifp->idev->dev->name, &ifp->addr);
Brian Haley9bdd8d42009-03-18 18:22:48 -07001888
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01001889 spin_lock_bh(&ifp->lock);
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09001890
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01001891 if (ifp->flags & IFA_F_STABLE_PRIVACY) {
1892 int scope = ifp->scope;
1893 u32 flags = ifp->flags;
1894 struct in6_addr new_addr;
1895 struct inet6_ifaddr *ifp2;
1896 u32 valid_lft, preferred_lft;
1897 int pfxlen = ifp->prefix_len;
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01001898 int retries = ifp->stable_privacy_retry + 1;
1899
Hannes Frederic Sowa1855b7c2015-03-23 23:36:05 +01001900 if (retries > net->ipv6.sysctl.idgen_retries) {
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01001901 net_info_ratelimited("%s: privacy stable address generation failed because of DAD conflicts!\n",
1902 ifp->idev->dev->name);
1903 goto errdad;
1904 }
1905
1906 new_addr = ifp->addr;
1907 if (ipv6_generate_stable_address(&new_addr, retries,
1908 idev))
1909 goto errdad;
1910
1911 valid_lft = ifp->valid_lft;
1912 preferred_lft = ifp->prefered_lft;
1913
1914 spin_unlock_bh(&ifp->lock);
1915
1916 if (idev->cnf.max_addresses &&
1917 ipv6_count_addresses(idev) >=
1918 idev->cnf.max_addresses)
1919 goto lock_errdad;
1920
1921 net_info_ratelimited("%s: generating new stable privacy address because of DAD conflict\n",
1922 ifp->idev->dev->name);
1923
1924 ifp2 = ipv6_add_addr(idev, &new_addr, NULL, pfxlen,
1925 scope, flags, valid_lft,
1926 preferred_lft);
1927 if (IS_ERR(ifp2))
1928 goto lock_errdad;
1929
1930 spin_lock_bh(&ifp2->lock);
1931 ifp2->stable_privacy_retry = retries;
1932 ifp2->state = INET6_IFADDR_STATE_PREDAD;
1933 spin_unlock_bh(&ifp2->lock);
1934
Hannes Frederic Sowa1855b7c2015-03-23 23:36:05 +01001935 addrconf_mod_dad_work(ifp2, net->ipv6.sysctl.idgen_delay);
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01001936 in6_ifa_put(ifp2);
1937lock_errdad:
1938 spin_lock_bh(&ifp->lock);
1939 } else if (idev->cnf.accept_dad > 1 && !idev->cnf.disable_ipv6) {
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09001940 addr.s6_addr32[0] = htonl(0xfe800000);
1941 addr.s6_addr32[1] = 0;
1942
1943 if (!ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) &&
1944 ipv6_addr_equal(&ifp->addr, &addr)) {
1945 /* DAD failed for link-local based on MAC address */
1946 idev->cnf.disable_ipv6 = 1;
Brian Haley9bdd8d42009-03-18 18:22:48 -07001947
Joe Perchesf3213832012-05-15 14:11:53 +00001948 pr_info("%s: IPv6 being disabled!\n",
Brian Haley9bdd8d42009-03-18 18:22:48 -07001949 ifp->idev->dev->name);
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09001950 }
1951 }
1952
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01001953errdad:
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01001954 /* transition from _POSTDAD to _ERRDAD */
1955 ifp->state = INET6_IFADDR_STATE_ERRDAD;
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01001956 spin_unlock_bh(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01001957
1958 addrconf_mod_dad_work(ifp, 0);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09001959}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960
Sabrina Dubrocaa9ed4a22014-09-02 10:29:29 +02001961/* Join to solicited addr multicast group.
1962 * caller must hold RTNL */
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001963void addrconf_join_solict(struct net_device *dev, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964{
1965 struct in6_addr maddr;
1966
1967 if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1968 return;
1969
1970 addrconf_addr_solict_mult(addr, &maddr);
1971 ipv6_dev_mc_inc(dev, &maddr);
1972}
1973
Sabrina Dubrocaa9ed4a22014-09-02 10:29:29 +02001974/* caller must hold RTNL */
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001975void addrconf_leave_solict(struct inet6_dev *idev, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976{
1977 struct in6_addr maddr;
1978
1979 if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1980 return;
1981
1982 addrconf_addr_solict_mult(addr, &maddr);
1983 __ipv6_dev_mc_dec(idev, &maddr);
1984}
1985
Sabrina Dubrocaa9ed4a22014-09-02 10:29:29 +02001986/* caller must hold RTNL */
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03001987static void addrconf_join_anycast(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988{
1989 struct in6_addr addr;
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01001990
Hannes Frederic Sowa88ad3142014-01-06 17:53:14 +01001991 if (ifp->prefix_len >= 127) /* RFC 6164 */
Bjørn Mork2bda8a02011-07-05 23:04:13 +00001992 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1994 if (ipv6_addr_any(&addr))
1995 return;
WANG Cong013b4d92014-09-11 15:35:11 -07001996 __ipv6_dev_ac_inc(ifp->idev, &addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997}
1998
Sabrina Dubrocaa9ed4a22014-09-02 10:29:29 +02001999/* caller must hold RTNL */
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03002000static void addrconf_leave_anycast(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001{
2002 struct in6_addr addr;
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01002003
Hannes Frederic Sowa88ad3142014-01-06 17:53:14 +01002004 if (ifp->prefix_len >= 127) /* RFC 6164 */
YOSHIFUJI Hideaki32019e62011-07-24 11:44:34 +00002005 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
2007 if (ipv6_addr_any(&addr))
2008 return;
2009 __ipv6_dev_ac_dec(ifp->idev, &addr);
2010}
2011
alex.bluesman.smirnov@gmail.com06a4c1c2012-05-10 03:25:52 +00002012static int addrconf_ifid_eui64(u8 *eui, struct net_device *dev)
2013{
Alexander Aring5241c2d2015-12-14 20:55:22 +01002014 if (dev->addr_len != EUI64_ADDR_LEN)
alex.bluesman.smirnov@gmail.com06a4c1c2012-05-10 03:25:52 +00002015 return -1;
Alexander Aring5241c2d2015-12-14 20:55:22 +01002016 memcpy(eui, dev->dev_addr, EUI64_ADDR_LEN);
YOSHIFUJI Hideaki / 吉藤英明5e98a362013-01-28 10:44:29 +00002017 eui[0] ^= 2;
alex.bluesman.smirnov@gmail.com06a4c1c2012-05-10 03:25:52 +00002018 return 0;
2019}
2020
YOSHIFUJI Hideaki / 吉藤英明cb6bf352013-03-25 08:26:24 +00002021static int addrconf_ifid_ieee1394(u8 *eui, struct net_device *dev)
2022{
2023 union fwnet_hwaddr *ha;
2024
2025 if (dev->addr_len != FWNET_ALEN)
2026 return -1;
2027
2028 ha = (union fwnet_hwaddr *)dev->dev_addr;
2029
2030 memcpy(eui, &ha->uc.uniq_id, sizeof(ha->uc.uniq_id));
2031 eui[0] ^= 2;
2032 return 0;
2033}
2034
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08002035static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev)
2036{
2037 /* XXX: inherit EUI-64 from other interface -- yoshfuji */
2038 if (dev->addr_len != ARCNET_ALEN)
2039 return -1;
2040 memset(eui, 0, 7);
Eldad Zack8e5e8f32012-04-01 07:49:08 +00002041 eui[7] = *(u8 *)dev->dev_addr;
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08002042 return 0;
2043}
2044
2045static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev)
2046{
2047 if (dev->addr_len != INFINIBAND_ALEN)
2048 return -1;
2049 memcpy(eui, dev->dev_addr + 12, 8);
2050 eui[0] |= 2;
2051 return 0;
2052}
2053
stephen hemmingerc61393e2010-10-04 20:17:53 +00002054static int __ipv6_isatap_ifid(u8 *eui, __be32 addr)
YOSHIFUJI Hideakidfd982b2008-04-10 15:42:09 +09002055{
Sascha Hlusiak9af28512009-05-19 12:56:51 +00002056 if (addr == 0)
2057 return -1;
YOSHIFUJI Hideakidfd982b2008-04-10 15:42:09 +09002058 eui[0] = (ipv4_is_zeronet(addr) || ipv4_is_private_10(addr) ||
2059 ipv4_is_loopback(addr) || ipv4_is_linklocal_169(addr) ||
2060 ipv4_is_private_172(addr) || ipv4_is_test_192(addr) ||
2061 ipv4_is_anycast_6to4(addr) || ipv4_is_private_192(addr) ||
2062 ipv4_is_test_198(addr) || ipv4_is_multicast(addr) ||
2063 ipv4_is_lbcast(addr)) ? 0x00 : 0x02;
2064 eui[1] = 0;
2065 eui[2] = 0x5E;
2066 eui[3] = 0xFE;
2067 memcpy(eui + 4, &addr, 4);
2068 return 0;
2069}
YOSHIFUJI Hideakidfd982b2008-04-10 15:42:09 +09002070
2071static int addrconf_ifid_sit(u8 *eui, struct net_device *dev)
2072{
2073 if (dev->priv_flags & IFF_ISATAP)
2074 return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
2075 return -1;
2076}
2077
stephen hemmingeraee80b52011-06-08 10:44:30 +00002078static int addrconf_ifid_gre(u8 *eui, struct net_device *dev)
2079{
2080 return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
2081}
2082
Nicolas Dichtele8377352013-08-20 12:16:06 +02002083static int addrconf_ifid_ip6tnl(u8 *eui, struct net_device *dev)
2084{
2085 memcpy(eui, dev->perm_addr, 3);
2086 memcpy(eui + 5, dev->perm_addr + 3, 3);
2087 eui[3] = 0xFF;
2088 eui[4] = 0xFE;
2089 eui[0] ^= 2;
2090 return 0;
2091}
2092
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
2094{
2095 switch (dev->type) {
2096 case ARPHRD_ETHER:
2097 case ARPHRD_FDDI:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08002098 return addrconf_ifid_eui48(eui, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 case ARPHRD_ARCNET:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08002100 return addrconf_ifid_arcnet(eui, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 case ARPHRD_INFINIBAND:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08002102 return addrconf_ifid_infiniband(eui, dev);
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11002103 case ARPHRD_SIT:
YOSHIFUJI Hideakidfd982b2008-04-10 15:42:09 +09002104 return addrconf_ifid_sit(eui, dev);
stephen hemmingeraee80b52011-06-08 10:44:30 +00002105 case ARPHRD_IPGRE:
2106 return addrconf_ifid_gre(eui, dev);
Jukka Rissanene74bccb2013-12-11 17:05:36 +02002107 case ARPHRD_6LOWPAN:
alex.bluesman.smirnov@gmail.com06a4c1c2012-05-10 03:25:52 +00002108 return addrconf_ifid_eui64(eui, dev);
YOSHIFUJI Hideaki / 吉藤英明cb6bf352013-03-25 08:26:24 +00002109 case ARPHRD_IEEE1394:
2110 return addrconf_ifid_ieee1394(eui, dev);
Nicolas Dichtele8377352013-08-20 12:16:06 +02002111 case ARPHRD_TUNNEL6:
2112 return addrconf_ifid_ip6tnl(eui, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 }
2114 return -1;
2115}
2116
2117static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
2118{
2119 int err = -1;
2120 struct inet6_ifaddr *ifp;
2121
2122 read_lock_bh(&idev->lock);
Hannes Frederic Sowa602582c2014-01-19 21:58:19 +01002123 list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) {
2124 if (ifp->scope > IFA_LINK)
2125 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
2127 memcpy(eui, ifp->addr.s6_addr+8, 8);
2128 err = 0;
2129 break;
2130 }
2131 }
2132 read_unlock_bh(&idev->lock);
2133 return err;
2134}
2135
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136/* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
Sorin Dumitrueb7e0572012-08-30 02:01:45 +00002137static void __ipv6_regen_rndid(struct inet6_dev *idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139regen:
YOSHIFUJI Hideaki955189e2006-03-20 16:54:09 -08002140 get_random_bytes(idev->rndid, sizeof(idev->rndid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 idev->rndid[0] &= ~0x02;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142
2143 /*
2144 * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
2145 * check if generated address is not inappropriate
2146 *
2147 * - Reserved subnet anycast (RFC 2526)
2148 * 11111101 11....11 1xxxxxxx
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11002149 * - ISATAP (RFC4214) 6.1
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 * 00-00-5E-FE-xx-xx-xx-xx
2151 * - value 0
2152 * - XXX: already assigned to an address on the device
2153 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002154 if (idev->rndid[0] == 0xfd &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff &&
2156 (idev->rndid[7]&0x80))
2157 goto regen;
2158 if ((idev->rndid[0]|idev->rndid[1]) == 0) {
2159 if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe)
2160 goto regen;
2161 if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00)
2162 goto regen;
2163 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164}
2165
2166static void ipv6_regen_rndid(unsigned long data)
2167{
2168 struct inet6_dev *idev = (struct inet6_dev *) data;
2169 unsigned long expires;
2170
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07002171 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 write_lock_bh(&idev->lock);
2173
2174 if (idev->dead)
2175 goto out;
2176
Sorin Dumitrueb7e0572012-08-30 02:01:45 +00002177 __ipv6_regen_rndid(idev);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002178
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 expires = jiffies +
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002180 idev->cnf.temp_prefered_lft * HZ -
Jiri Pirko1f9248e2013-12-07 19:26:53 +01002181 idev->cnf.regen_max_retry * idev->cnf.dad_transmits *
2182 NEIGH_VAR(idev->nd_parms, RETRANS_TIME) -
Ben Hutchings784e2712010-06-26 11:42:55 +00002183 idev->cnf.max_desync_factor * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 if (time_before(expires, jiffies)) {
Joe Perchesf3213832012-05-15 14:11:53 +00002185 pr_warn("%s: too short regeneration interval; timer disabled for %s\n",
2186 __func__, idev->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 goto out;
2188 }
2189
2190 if (!mod_timer(&idev->regen_timer, expires))
2191 in6_dev_hold(idev);
2192
2193out:
2194 write_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07002195 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 in6_dev_put(idev);
2197}
2198
Sorin Dumitrueb7e0572012-08-30 02:01:45 +00002199static void __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr)
Eldad Zack8e5e8f32012-04-01 07:49:08 +00002200{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0)
Sorin Dumitrueb7e0572012-08-30 02:01:45 +00002202 __ipv6_regen_rndid(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204
Lorenzo Colitti2b253062014-03-26 19:35:41 +09002205u32 addrconf_rt_table(const struct net_device *dev, u32 default_table) {
2206 /* Determines into what table to put autoconf PIO/RIO/default routes
2207 * learned on this device.
2208 *
2209 * - If 0, use the same table for every device. This puts routes into
2210 * one of RT_TABLE_{PREFIX,INFO,DFLT} depending on the type of route
2211 * (but note that these three are currently all equal to
2212 * RT6_TABLE_MAIN).
2213 * - If > 0, use the specified table.
2214 * - If < 0, put routes into table dev->ifindex + (-rt_table).
2215 */
2216 struct inet6_dev *idev = in6_dev_get(dev);
2217 u32 table;
2218 int sysctl = idev->cnf.accept_ra_rt_table;
2219 if (sysctl == 0) {
2220 table = default_table;
2221 } else if (sysctl > 0) {
2222 table = (u32) sysctl;
2223 } else {
2224 table = (unsigned) dev->ifindex + (-sysctl);
2225 }
2226 in6_dev_put(idev);
2227 return table;
2228}
2229
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230/*
2231 * Add prefix route.
2232 */
2233
2234static void
2235addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev,
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -07002236 unsigned long expires, u32 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237{
Thomas Graf86872cb2006-08-22 00:01:08 -07002238 struct fib6_config cfg = {
Lorenzo Colitti2b253062014-03-26 19:35:41 +09002239 .fc_table = l3mdev_fib_table(dev) ? : addrconf_rt_table(dev, RT6_TABLE_PREFIX),
Thomas Graf86872cb2006-08-22 00:01:08 -07002240 .fc_metric = IP6_RT_PRIO_ADDRCONF,
2241 .fc_ifindex = dev->ifindex,
2242 .fc_expires = expires,
2243 .fc_dst_len = plen,
2244 .fc_flags = RTF_UP | flags,
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002245 .fc_nlinfo.nl_net = dev_net(dev),
Stephen Hemmingerf410a1f2008-08-23 05:16:46 -07002246 .fc_protocol = RTPROT_KERNEL,
Thomas Graf86872cb2006-08-22 00:01:08 -07002247 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002249 cfg.fc_dst = *pfx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250
2251 /* Prevent useless cloning on PtP SIT.
2252 This thing is done here expecting that the whole
2253 class of non-broadcast devices need not cloning.
2254 */
Amerigo Wang07a93622012-10-29 16:23:10 +00002255#if IS_ENABLED(CONFIG_IPV6_SIT)
Thomas Graf86872cb2006-08-22 00:01:08 -07002256 if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT))
2257 cfg.fc_flags |= RTF_NONEXTHOP;
Joerg Roedel0be669b2006-10-10 14:49:53 -07002258#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259
Thomas Graf86872cb2006-08-22 00:01:08 -07002260 ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261}
2262
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002263
2264static struct rt6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
2265 int plen,
2266 const struct net_device *dev,
2267 u32 flags, u32 noflags)
2268{
2269 struct fib6_node *fn;
2270 struct rt6_info *rt = NULL;
2271 struct fib6_table *table;
Lorenzo Colitti2b253062014-03-26 19:35:41 +09002272 u32 tb_id = l3mdev_fib_table(dev) ? : addrconf_rt_table(dev, RT6_TABLE_PREFIX);
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002273
David Ahernca254492015-10-12 11:47:10 -07002274 table = fib6_get_table(dev_net(dev), tb_id);
Ian Morris63159f22015-03-29 14:00:04 +01002275 if (!table)
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002276 return NULL;
2277
Li RongQing5744dd92012-09-11 21:59:01 +00002278 read_lock_bh(&table->tb6_lock);
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002279 fn = fib6_locate(&table->tb6_root, pfx, plen, NULL, 0);
2280 if (!fn)
2281 goto out;
Martin KaFai Lau1f56a012015-04-28 13:03:03 -07002282
2283 noflags |= RTF_CACHE;
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002284 for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
David S. Millerd1918542011-12-28 20:19:20 -05002285 if (rt->dst.dev->ifindex != dev->ifindex)
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002286 continue;
2287 if ((rt->rt6i_flags & flags) != flags)
2288 continue;
Romain Kuntz85da53b2013-01-09 15:02:26 +01002289 if ((rt->rt6i_flags & noflags) != 0)
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002290 continue;
2291 dst_hold(&rt->dst);
2292 break;
2293 }
2294out:
Li RongQing5744dd92012-09-11 21:59:01 +00002295 read_unlock_bh(&table->tb6_lock);
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002296 return rt;
2297}
2298
2299
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300/* Create "default" multicast route to the interface */
2301
2302static void addrconf_add_mroute(struct net_device *dev)
2303{
Thomas Graf86872cb2006-08-22 00:01:08 -07002304 struct fib6_config cfg = {
David Ahernca254492015-10-12 11:47:10 -07002305 .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_LOCAL,
Thomas Graf86872cb2006-08-22 00:01:08 -07002306 .fc_metric = IP6_RT_PRIO_ADDRCONF,
2307 .fc_ifindex = dev->ifindex,
2308 .fc_dst_len = 8,
2309 .fc_flags = RTF_UP,
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002310 .fc_nlinfo.nl_net = dev_net(dev),
Thomas Graf86872cb2006-08-22 00:01:08 -07002311 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312
Thomas Graf86872cb2006-08-22 00:01:08 -07002313 ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0);
2314
2315 ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316}
2317
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
2319{
2320 struct inet6_dev *idev;
2321
2322 ASSERT_RTNL();
2323
Stephen Hemmingere21e8462010-03-20 16:09:01 -07002324 idev = ipv6_find_idev(dev);
2325 if (!idev)
Brian Haley64e724f2010-07-20 10:34:30 +00002326 return ERR_PTR(-ENOBUFS);
2327
2328 if (idev->cnf.disable_ipv6)
2329 return ERR_PTR(-EACCES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330
2331 /* Add default multicast route */
David Ahernba46ee42016-06-13 13:44:18 -07002332 if (!(dev->flags & IFF_LOOPBACK) && !netif_is_l3_master(dev))
Li Wei4af04ab2011-12-06 21:23:45 +00002333 addrconf_add_mroute(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 return idev;
2336}
2337
Jiri Pirko53bd6742013-12-06 09:45:22 +01002338static void manage_tempaddrs(struct inet6_dev *idev,
2339 struct inet6_ifaddr *ifp,
2340 __u32 valid_lft, __u32 prefered_lft,
2341 bool create, unsigned long now)
2342{
2343 u32 flags;
2344 struct inet6_ifaddr *ift;
2345
2346 read_lock_bh(&idev->lock);
2347 /* update all temporary addresses in the list */
2348 list_for_each_entry(ift, &idev->tempaddr_list, tmp_list) {
2349 int age, max_valid, max_prefered;
2350
2351 if (ifp != ift->ifpub)
2352 continue;
2353
2354 /* RFC 4941 section 3.3:
2355 * If a received option will extend the lifetime of a public
2356 * address, the lifetimes of temporary addresses should
2357 * be extended, subject to the overall constraint that no
2358 * temporary addresses should ever remain "valid" or "preferred"
2359 * for a time longer than (TEMP_VALID_LIFETIME) or
2360 * (TEMP_PREFERRED_LIFETIME - DESYNC_FACTOR), respectively.
2361 */
2362 age = (now - ift->cstamp) / HZ;
2363 max_valid = idev->cnf.temp_valid_lft - age;
2364 if (max_valid < 0)
2365 max_valid = 0;
2366
2367 max_prefered = idev->cnf.temp_prefered_lft -
2368 idev->cnf.max_desync_factor - age;
2369 if (max_prefered < 0)
2370 max_prefered = 0;
2371
2372 if (valid_lft > max_valid)
2373 valid_lft = max_valid;
2374
2375 if (prefered_lft > max_prefered)
2376 prefered_lft = max_prefered;
2377
2378 spin_lock(&ift->lock);
2379 flags = ift->flags;
2380 ift->valid_lft = valid_lft;
2381 ift->prefered_lft = prefered_lft;
2382 ift->tstamp = now;
2383 if (prefered_lft > 0)
2384 ift->flags &= ~IFA_F_DEPRECATED;
2385
2386 spin_unlock(&ift->lock);
2387 if (!(flags&IFA_F_TENTATIVE))
2388 ipv6_ifa_notify(0, ift);
2389 }
2390
2391 if ((create || list_empty(&idev->tempaddr_list)) &&
2392 idev->cnf.use_tempaddr > 0) {
2393 /* When a new public address is created as described
2394 * in [ADDRCONF], also create a new temporary address.
2395 * Also create a temporary address if it's enabled but
2396 * no temporary address currently exists.
2397 */
2398 read_unlock_bh(&idev->lock);
2399 ipv6_create_tempaddr(ifp, NULL);
2400 } else {
2401 read_unlock_bh(&idev->lock);
2402 }
2403}
2404
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01002405static bool is_addr_mode_generate_stable(struct inet6_dev *idev)
2406{
2407 return idev->addr_gen_mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY ||
2408 idev->addr_gen_mode == IN6_ADDR_GEN_MODE_RANDOM;
2409}
2410
Alexander Aringcc84b3c2016-06-15 21:20:24 +02002411int addrconf_prefix_rcv_add_addr(struct net *net, struct net_device *dev,
2412 const struct prefix_info *pinfo,
2413 struct inet6_dev *in6_dev,
2414 const struct in6_addr *addr, int addr_type,
2415 u32 addr_flags, bool sllao, bool tokenized,
2416 __u32 valid_lft, u32 prefered_lft)
Alexander Aring4f672232016-06-15 21:20:22 +02002417{
2418 struct inet6_ifaddr *ifp = ipv6_get_ifaddr(net, addr, dev, 1);
2419 int create = 0, update_lft = 0;
2420
2421 if (!ifp && valid_lft) {
2422 int max_addresses = in6_dev->cnf.max_addresses;
2423
2424#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
2425 if (in6_dev->cnf.optimistic_dad &&
2426 !net->ipv6.devconf_all->forwarding && sllao)
2427 addr_flags |= IFA_F_OPTIMISTIC;
2428#endif
2429
2430 /* Do not allow to create too much of autoconfigured
2431 * addresses; this would be too easy way to crash kernel.
2432 */
2433 if (!max_addresses ||
2434 ipv6_count_addresses(in6_dev) < max_addresses)
2435 ifp = ipv6_add_addr(in6_dev, addr, NULL,
2436 pinfo->prefix_len,
2437 addr_type&IPV6_ADDR_SCOPE_MASK,
2438 addr_flags, valid_lft,
2439 prefered_lft);
2440
2441 if (IS_ERR_OR_NULL(ifp))
2442 return -1;
2443
2444 update_lft = 0;
2445 create = 1;
2446 spin_lock_bh(&ifp->lock);
2447 ifp->flags |= IFA_F_MANAGETEMPADDR;
2448 ifp->cstamp = jiffies;
2449 ifp->tokenized = tokenized;
2450 spin_unlock_bh(&ifp->lock);
2451 addrconf_dad_start(ifp);
2452 }
2453
2454 if (ifp) {
2455 u32 flags;
2456 unsigned long now;
2457 u32 stored_lft;
2458
2459 /* update lifetime (RFC2462 5.5.3 e) */
2460 spin_lock_bh(&ifp->lock);
2461 now = jiffies;
2462 if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
2463 stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
2464 else
2465 stored_lft = 0;
2466 if (!update_lft && !create && stored_lft) {
2467 const u32 minimum_lft = min_t(u32,
2468 stored_lft, MIN_VALID_LIFETIME);
2469 valid_lft = max(valid_lft, minimum_lft);
2470
2471 /* RFC4862 Section 5.5.3e:
2472 * "Note that the preferred lifetime of the
2473 * corresponding address is always reset to
2474 * the Preferred Lifetime in the received
2475 * Prefix Information option, regardless of
2476 * whether the valid lifetime is also reset or
2477 * ignored."
2478 *
2479 * So we should always update prefered_lft here.
2480 */
2481 update_lft = 1;
2482 }
2483
2484 if (update_lft) {
2485 ifp->valid_lft = valid_lft;
2486 ifp->prefered_lft = prefered_lft;
2487 ifp->tstamp = now;
2488 flags = ifp->flags;
2489 ifp->flags &= ~IFA_F_DEPRECATED;
2490 spin_unlock_bh(&ifp->lock);
2491
2492 if (!(flags&IFA_F_TENTATIVE))
2493 ipv6_ifa_notify(0, ifp);
2494 } else
2495 spin_unlock_bh(&ifp->lock);
2496
2497 manage_tempaddrs(in6_dev, ifp, valid_lft, prefered_lft,
2498 create, now);
2499
2500 in6_ifa_put(ifp);
2501 addrconf_verify();
2502 }
2503
2504 return 0;
2505}
Alexander Aringcc84b3c2016-06-15 21:20:24 +02002506EXPORT_SYMBOL_GPL(addrconf_prefix_rcv_add_addr);
Alexander Aring4f672232016-06-15 21:20:22 +02002507
Neil Hormane6bff992012-01-04 10:49:15 +00002508void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len, bool sllao)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509{
2510 struct prefix_info *pinfo;
2511 __u32 valid_lft;
2512 __u32 prefered_lft;
Alexander Aring4f672232016-06-15 21:20:22 +02002513 int addr_type, err;
Hannes Frederic Sowa64236f32015-03-23 23:36:02 +01002514 u32 addr_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 struct inet6_dev *in6_dev;
Brian Haley56d417b2009-06-01 03:07:33 -07002516 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517
2518 pinfo = (struct prefix_info *) opt;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002519
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 if (len < sizeof(struct prefix_info)) {
dingtianhongba3542e2013-08-17 10:27:04 +08002521 ADBG("addrconf: prefix option too short\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 return;
2523 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002524
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 /*
2526 * Validation checks ([ADDRCONF], page 19)
2527 */
2528
2529 addr_type = ipv6_addr_type(&pinfo->prefix);
2530
2531 if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
2532 return;
2533
2534 valid_lft = ntohl(pinfo->valid);
2535 prefered_lft = ntohl(pinfo->prefered);
2536
2537 if (prefered_lft > valid_lft) {
Joe Perchese87cc472012-05-13 21:56:26 +00002538 net_warn_ratelimited("addrconf: prefix option has invalid lifetime\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 return;
2540 }
2541
2542 in6_dev = in6_dev_get(dev);
2543
Ian Morris63159f22015-03-29 14:00:04 +01002544 if (!in6_dev) {
Joe Perchese87cc472012-05-13 21:56:26 +00002545 net_dbg_ratelimited("addrconf: device %s not configured\n",
2546 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 return;
2548 }
2549
2550 /*
2551 * Two things going on here:
2552 * 1) Add routes for on-link prefixes
2553 * 2) Configure prefixes with the auto flag set
2554 */
2555
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002556 if (pinfo->onlink) {
2557 struct rt6_info *rt;
2558 unsigned long rt_expires;
2559
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002560 /* Avoid arithmetic overflow. Really, we could
2561 * save rt_expires in seconds, likely valid_lft,
2562 * but it would require division in fib gc, that it
2563 * not good.
2564 */
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002565 if (HZ > USER_HZ)
2566 rt_expires = addrconf_timeout_fixup(valid_lft, HZ);
2567 else
2568 rt_expires = addrconf_timeout_fixup(valid_lft, USER_HZ);
YOSHIFUJI Hideaki3dd4bc62005-12-19 14:02:45 -08002569
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002570 if (addrconf_finite_timeout(rt_expires))
2571 rt_expires *= HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002573 rt = addrconf_get_prefix_route(&pinfo->prefix,
2574 pinfo->prefix_len,
2575 dev,
2576 RTF_ADDRCONF | RTF_PREFIX_RT,
2577 RTF_GATEWAY | RTF_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002579 if (rt) {
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002580 /* Autoconf prefix route */
2581 if (valid_lft == 0) {
2582 ip6_del_rt(rt);
2583 rt = NULL;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002584 } else if (addrconf_finite_timeout(rt_expires)) {
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002585 /* not infinity */
Gao feng1716a962012-04-06 00:13:10 +00002586 rt6_set_expires(rt, jiffies + rt_expires);
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002587 } else {
Gao feng1716a962012-04-06 00:13:10 +00002588 rt6_clean_expires(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 }
2590 } else if (valid_lft) {
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002591 clock_t expires = 0;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002592 int flags = RTF_ADDRCONF | RTF_PREFIX_RT;
2593 if (addrconf_finite_timeout(rt_expires)) {
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002594 /* not infinity */
2595 flags |= RTF_EXPIRES;
2596 expires = jiffies_to_clock_t(rt_expires);
2597 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002599 dev, expires, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 }
Amerigo Wang94e187c2012-10-29 00:13:19 +00002601 ip6_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 }
2603
2604 /* Try to figure out our local address for this prefix */
2605
2606 if (pinfo->autoconf && in6_dev->cnf.autoconf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 struct in6_addr addr;
Alexander Aringf997c552016-06-15 21:20:23 +02002608 bool tokenized = false, dev_addr_generated = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609
2610 if (pinfo->prefix_len == 64) {
2611 memcpy(&addr, &pinfo->prefix, 8);
Daniel Borkmannf53adae2013-04-08 04:01:30 +00002612
2613 if (!ipv6_addr_any(&in6_dev->token)) {
2614 read_lock_bh(&in6_dev->lock);
2615 memcpy(addr.s6_addr + 8,
2616 in6_dev->token.s6_addr + 8, 8);
2617 read_unlock_bh(&in6_dev->lock);
Daniel Borkmann617fe292013-04-09 03:47:16 +00002618 tokenized = true;
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01002619 } else if (is_addr_mode_generate_stable(in6_dev) &&
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01002620 !ipv6_generate_stable_address(&addr, 0,
2621 in6_dev)) {
Hannes Frederic Sowa64236f32015-03-23 23:36:02 +01002622 addr_flags |= IFA_F_STABLE_PRIVACY;
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01002623 goto ok;
Daniel Borkmannf53adae2013-04-08 04:01:30 +00002624 } else if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
2625 ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
Alexander Aring4f672232016-06-15 21:20:22 +02002626 goto put;
Alexander Aringf997c552016-06-15 21:20:23 +02002627 } else {
2628 dev_addr_generated = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 }
2630 goto ok;
2631 }
Joe Perchese87cc472012-05-13 21:56:26 +00002632 net_dbg_ratelimited("IPv6 addrconf: prefix with wrong length %d\n",
2633 pinfo->prefix_len);
Alexander Aring4f672232016-06-15 21:20:22 +02002634 goto put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635
2636ok:
Alexander Aring4f672232016-06-15 21:20:22 +02002637 err = addrconf_prefix_rcv_add_addr(net, dev, pinfo, in6_dev,
2638 &addr, addr_type,
2639 addr_flags, sllao,
2640 tokenized, valid_lft,
2641 prefered_lft);
2642 if (err)
2643 goto put;
Alexander Aringf997c552016-06-15 21:20:23 +02002644
2645 /* Ignore error case here because previous prefix add addr was
2646 * successful which will be notified.
2647 */
2648 ndisc_ops_prefix_rcv_add_addr(net, dev, pinfo, in6_dev, &addr,
2649 addr_type, addr_flags, sllao,
2650 tokenized, valid_lft,
2651 prefered_lft,
2652 dev_addr_generated);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 }
2654 inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
Alexander Aring4f672232016-06-15 21:20:22 +02002655put:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 in6_dev_put(in6_dev);
2657}
2658
2659/*
2660 * Set destination address.
2661 * Special case for SIT interfaces where we create a new "virtual"
2662 * device.
2663 */
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002664int addrconf_set_dstaddr(struct net *net, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665{
2666 struct in6_ifreq ireq;
2667 struct net_device *dev;
2668 int err = -EINVAL;
2669
2670 rtnl_lock();
2671
2672 err = -EFAULT;
2673 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2674 goto err_exit;
2675
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002676 dev = __dev_get_by_index(net, ireq.ifr6_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677
2678 err = -ENODEV;
Ian Morris63159f22015-03-29 14:00:04 +01002679 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 goto err_exit;
2681
Amerigo Wang07a93622012-10-29 16:23:10 +00002682#if IS_ENABLED(CONFIG_IPV6_SIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 if (dev->type == ARPHRD_SIT) {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -08002684 const struct net_device_ops *ops = dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 struct ifreq ifr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 struct ip_tunnel_parm p;
2687
2688 err = -EADDRNOTAVAIL;
2689 if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4))
2690 goto err_exit;
2691
2692 memset(&p, 0, sizeof(p));
2693 p.iph.daddr = ireq.ifr6_addr.s6_addr32[3];
2694 p.iph.saddr = 0;
2695 p.iph.version = 4;
2696 p.iph.ihl = 5;
2697 p.iph.protocol = IPPROTO_IPV6;
2698 p.iph.ttl = 64;
Stephen Hemmingerd20b3102008-01-21 00:48:43 -08002699 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -08002701 if (ops->ndo_do_ioctl) {
2702 mm_segment_t oldfs = get_fs();
2703
2704 set_fs(KERNEL_DS);
2705 err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
2706 set_fs(oldfs);
2707 } else
2708 err = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709
2710 if (err == 0) {
2711 err = -ENOBUFS;
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002712 dev = __dev_get_by_name(net, p.name);
2713 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 goto err_exit;
2715 err = dev_open(dev);
2716 }
2717 }
Joerg Roedel0be669b2006-10-10 14:49:53 -07002718#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719
2720err_exit:
2721 rtnl_unlock();
2722 return err;
2723}
2724
Madhu Challa93a714d2015-02-25 09:58:35 -08002725static int ipv6_mc_config(struct sock *sk, bool join,
2726 const struct in6_addr *addr, int ifindex)
2727{
2728 int ret;
2729
2730 ASSERT_RTNL();
2731
2732 lock_sock(sk);
2733 if (join)
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03002734 ret = ipv6_sock_mc_join(sk, ifindex, addr);
Madhu Challa93a714d2015-02-25 09:58:35 -08002735 else
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03002736 ret = ipv6_sock_mc_drop(sk, ifindex, addr);
Madhu Challa93a714d2015-02-25 09:58:35 -08002737 release_sock(sk);
2738
2739 return ret;
2740}
2741
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742/*
2743 * Manual configuration of address on an interface
2744 */
Jiri Pirko479840f2013-12-06 09:45:21 +01002745static int inet6_addr_add(struct net *net, int ifindex,
2746 const struct in6_addr *pfx,
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00002747 const struct in6_addr *peer_pfx,
Jiri Pirko479840f2013-12-06 09:45:21 +01002748 unsigned int plen, __u32 ifa_flags,
2749 __u32 prefered_lft, __u32 valid_lft)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750{
2751 struct inet6_ifaddr *ifp;
2752 struct inet6_dev *idev;
2753 struct net_device *dev;
Madhu Challa93a714d2015-02-25 09:58:35 -08002754 unsigned long timeout;
2755 clock_t expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 int scope;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002757 u32 flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758
2759 ASSERT_RTNL();
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002760
Thomas Graf24ef0da2008-05-28 16:54:22 +02002761 if (plen > 128)
2762 return -EINVAL;
2763
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002764 /* check the lifetime */
2765 if (!valid_lft || prefered_lft > valid_lft)
2766 return -EINVAL;
2767
Jiri Pirko53bd6742013-12-06 09:45:22 +01002768 if (ifa_flags & IFA_F_MANAGETEMPADDR && plen != 64)
2769 return -EINVAL;
2770
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002771 dev = __dev_get_by_index(net, ifindex);
2772 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 return -ENODEV;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002774
Brian Haley64e724f2010-07-20 10:34:30 +00002775 idev = addrconf_add_dev(dev);
2776 if (IS_ERR(idev))
2777 return PTR_ERR(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778
Madhu Challa93a714d2015-02-25 09:58:35 -08002779 if (ifa_flags & IFA_F_MCAUTOJOIN) {
2780 int ret = ipv6_mc_config(net->ipv6.mc_autojoin_sk,
2781 true, pfx, ifindex);
2782
2783 if (ret < 0)
2784 return ret;
2785 }
2786
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 scope = ipv6_addr_scope(pfx);
2788
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002789 timeout = addrconf_timeout_fixup(valid_lft, HZ);
2790 if (addrconf_finite_timeout(timeout)) {
2791 expires = jiffies_to_clock_t(timeout * HZ);
2792 valid_lft = timeout;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002793 flags = RTF_EXPIRES;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002794 } else {
2795 expires = 0;
2796 flags = 0;
2797 ifa_flags |= IFA_F_PERMANENT;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002798 }
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002799
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002800 timeout = addrconf_timeout_fixup(prefered_lft, HZ);
2801 if (addrconf_finite_timeout(timeout)) {
2802 if (timeout == 0)
2803 ifa_flags |= IFA_F_DEPRECATED;
2804 prefered_lft = timeout;
2805 }
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002806
Jiri Benc8a226b22013-08-01 10:41:28 +02002807 ifp = ipv6_add_addr(idev, pfx, peer_pfx, plen, scope, ifa_flags,
2808 valid_lft, prefered_lft);
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002809
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 if (!IS_ERR(ifp)) {
Thomas Haller761aac72014-01-15 15:36:58 +01002811 if (!(ifa_flags & IFA_F_NOPREFIXROUTE)) {
2812 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev,
2813 expires, flags);
2814 }
2815
Neil Horman95c385b2007-04-25 17:08:10 -07002816 /*
2817 * Note that section 3.1 of RFC 4429 indicates
2818 * that the Optimistic flag should not be set for
2819 * manually configured addresses
2820 */
David S. Millercf22f9a2012-04-14 21:37:40 -04002821 addrconf_dad_start(ifp);
Jiri Pirko53bd6742013-12-06 09:45:22 +01002822 if (ifa_flags & IFA_F_MANAGETEMPADDR)
2823 manage_tempaddrs(idev, ifp, valid_lft, prefered_lft,
2824 true, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 in6_ifa_put(ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01002826 addrconf_verify_rtnl();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 return 0;
Madhu Challa93a714d2015-02-25 09:58:35 -08002828 } else if (ifa_flags & IFA_F_MCAUTOJOIN) {
2829 ipv6_mc_config(net->ipv6.mc_autojoin_sk,
2830 false, pfx, ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 }
2832
2833 return PTR_ERR(ifp);
2834}
2835
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02002836static int inet6_addr_del(struct net *net, int ifindex, u32 ifa_flags,
2837 const struct in6_addr *pfx, unsigned int plen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838{
2839 struct inet6_ifaddr *ifp;
2840 struct inet6_dev *idev;
2841 struct net_device *dev;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002842
Thomas Graf24ef0da2008-05-28 16:54:22 +02002843 if (plen > 128)
2844 return -EINVAL;
2845
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002846 dev = __dev_get_by_index(net, ifindex);
2847 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 return -ENODEV;
2849
Ian Morrise5d08d72014-11-23 21:28:43 +00002850 idev = __in6_dev_get(dev);
Ian Morris63159f22015-03-29 14:00:04 +01002851 if (!idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 return -ENXIO;
2853
2854 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00002855 list_for_each_entry(ifp, &idev->addr_list, if_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 if (ifp->prefix_len == plen &&
2857 ipv6_addr_equal(pfx, &ifp->addr)) {
2858 in6_ifa_hold(ifp);
2859 read_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002860
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02002861 if (!(ifp->flags & IFA_F_TEMPORARY) &&
2862 (ifa_flags & IFA_F_MANAGETEMPADDR))
2863 manage_tempaddrs(idev, ifp, 0, 0, false,
2864 jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 ipv6_del_addr(ifp);
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02002866 addrconf_verify_rtnl();
Madhu Challa93a714d2015-02-25 09:58:35 -08002867 if (ipv6_addr_is_multicast(pfx)) {
2868 ipv6_mc_config(net->ipv6.mc_autojoin_sk,
2869 false, pfx, dev->ifindex);
2870 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 return 0;
2872 }
2873 }
2874 read_unlock_bh(&idev->lock);
2875 return -EADDRNOTAVAIL;
2876}
2877
2878
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002879int addrconf_add_ifaddr(struct net *net, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880{
2881 struct in6_ifreq ireq;
2882 int err;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002883
Eric W. Biedermanaf31f412012-11-16 03:03:06 +00002884 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 return -EPERM;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002886
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2888 return -EFAULT;
2889
2890 rtnl_lock();
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00002891 err = inet6_addr_add(net, ireq.ifr6_ifindex, &ireq.ifr6_addr, NULL,
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002892 ireq.ifr6_prefixlen, IFA_F_PERMANENT,
2893 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 rtnl_unlock();
2895 return err;
2896}
2897
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002898int addrconf_del_ifaddr(struct net *net, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899{
2900 struct in6_ifreq ireq;
2901 int err;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002902
Eric W. Biedermanaf31f412012-11-16 03:03:06 +00002903 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 return -EPERM;
2905
2906 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2907 return -EFAULT;
2908
2909 rtnl_lock();
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02002910 err = inet6_addr_del(net, ireq.ifr6_ifindex, 0, &ireq.ifr6_addr,
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002911 ireq.ifr6_prefixlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 rtnl_unlock();
2913 return err;
2914}
2915
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08002916static void add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
2917 int plen, int scope)
2918{
2919 struct inet6_ifaddr *ifp;
2920
Jiri Benc8a226b22013-08-01 10:41:28 +02002921 ifp = ipv6_add_addr(idev, addr, NULL, plen,
Hannes Frederic Sowa07edd742014-01-08 15:43:22 +01002922 scope, IFA_F_PERMANENT,
2923 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08002924 if (!IS_ERR(ifp)) {
2925 spin_lock_bh(&ifp->lock);
2926 ifp->flags &= ~IFA_F_TENTATIVE;
2927 spin_unlock_bh(&ifp->lock);
2928 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2929 in6_ifa_put(ifp);
2930 }
2931}
2932
Amerigo Wang07a93622012-10-29 16:23:10 +00002933#if IS_ENABLED(CONFIG_IPV6_SIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934static void sit_add_v4_addrs(struct inet6_dev *idev)
2935{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 struct in6_addr addr;
2937 struct net_device *dev;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002938 struct net *net = dev_net(idev->dev);
Nicolas Dichtel929c9cf2013-11-14 13:51:05 +01002939 int scope, plen;
Nicolas Dichtelf0e2acf2013-11-14 13:51:06 +01002940 u32 pflags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941
2942 ASSERT_RTNL();
2943
2944 memset(&addr, 0, sizeof(struct in6_addr));
2945 memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
2946
2947 if (idev->dev->flags&IFF_POINTOPOINT) {
2948 addr.s6_addr32[0] = htonl(0xfe800000);
2949 scope = IFA_LINK;
Nicolas Dichtel929c9cf2013-11-14 13:51:05 +01002950 plen = 64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 } else {
2952 scope = IPV6_ADDR_COMPATv4;
Nicolas Dichtel929c9cf2013-11-14 13:51:05 +01002953 plen = 96;
Nicolas Dichtelf0e2acf2013-11-14 13:51:06 +01002954 pflags |= RTF_NONEXTHOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 }
2956
2957 if (addr.s6_addr32[3]) {
Nicolas Dichtel929c9cf2013-11-14 13:51:05 +01002958 add_addr(idev, &addr, plen, scope);
Nicolas Dichtelf0e2acf2013-11-14 13:51:06 +01002959 addrconf_prefix_route(&addr, plen, idev->dev, 0, pflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 return;
2961 }
2962
Benjamin Thery6fda7352008-03-05 10:47:47 -08002963 for_each_netdev(net, dev) {
Eldad Zack8e5e8f32012-04-01 07:49:08 +00002964 struct in_device *in_dev = __in_dev_get_rtnl(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 if (in_dev && (dev->flags & IFF_UP)) {
Eldad Zack8e5e8f32012-04-01 07:49:08 +00002966 struct in_ifaddr *ifa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967
2968 int flag = scope;
2969
2970 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971
2972 addr.s6_addr32[3] = ifa->ifa_local;
2973
2974 if (ifa->ifa_scope == RT_SCOPE_LINK)
2975 continue;
2976 if (ifa->ifa_scope >= RT_SCOPE_HOST) {
2977 if (idev->dev->flags&IFF_POINTOPOINT)
2978 continue;
2979 flag |= IFA_HOST;
2980 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08002982 add_addr(idev, &addr, plen, flag);
Nicolas Dichtelf0e2acf2013-11-14 13:51:06 +01002983 addrconf_prefix_route(&addr, plen, idev->dev, 0,
2984 pflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 }
2986 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002987 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988}
Joerg Roedel0be669b2006-10-10 14:49:53 -07002989#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990
2991static void init_loopback(struct net_device *dev)
2992{
2993 struct inet6_dev *idev;
Balakumaran Kannan25fb6ca42013-04-02 16:15:05 +05302994 struct net_device *sp_dev;
2995 struct inet6_ifaddr *sp_ifa;
2996 struct rt6_info *sp_rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997
2998 /* ::1 */
2999
3000 ASSERT_RTNL();
3001
Ian Morrise5d08d72014-11-23 21:28:43 +00003002 idev = ipv6_find_idev(dev);
Ian Morris63159f22015-03-29 14:00:04 +01003003 if (!idev) {
Joe Perches91df42b2012-05-15 14:11:54 +00003004 pr_debug("%s: add_dev failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 return;
3006 }
3007
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08003008 add_addr(idev, &in6addr_loopback, 128, IFA_HOST);
Balakumaran Kannan25fb6ca42013-04-02 16:15:05 +05303009
3010 /* Add routes to other interface's IPv6 addresses */
3011 for_each_netdev(dev_net(dev), sp_dev) {
3012 if (!strcmp(sp_dev->name, dev->name))
3013 continue;
3014
3015 idev = __in6_dev_get(sp_dev);
3016 if (!idev)
3017 continue;
3018
3019 read_lock_bh(&idev->lock);
3020 list_for_each_entry(sp_ifa, &idev->addr_list, if_list) {
3021
3022 if (sp_ifa->flags & (IFA_F_DADFAILED | IFA_F_TENTATIVE))
3023 continue;
3024
Gao feng33d99112014-01-24 16:29:11 +08003025 if (sp_ifa->rt) {
3026 /* This dst has been added to garbage list when
3027 * lo device down, release this obsolete dst and
3028 * reallocate a new router for ifa.
3029 */
3030 if (sp_ifa->rt->dst.obsolete > 0) {
3031 ip6_rt_put(sp_ifa->rt);
3032 sp_ifa->rt = NULL;
3033 } else {
3034 continue;
3035 }
3036 }
Gao fenga881ae12013-06-16 11:14:30 +08003037
François-Xavier Le Bail57ec0af2013-12-02 11:28:49 +01003038 sp_rt = addrconf_dst_alloc(idev, &sp_ifa->addr, false);
Balakumaran Kannan25fb6ca42013-04-02 16:15:05 +05303039
3040 /* Failure cases are ignored */
Gao feng534c8772013-06-02 22:16:21 +00003041 if (!IS_ERR(sp_rt)) {
3042 sp_ifa->rt = sp_rt;
Balakumaran Kannan25fb6ca42013-04-02 16:15:05 +05303043 ip6_ins_rt(sp_rt);
Gao feng534c8772013-06-02 22:16:21 +00003044 }
Balakumaran Kannan25fb6ca42013-04-02 16:15:05 +05303045 }
3046 read_unlock_bh(&idev->lock);
3047 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048}
3049
Alexander Aring2ad3ed52016-06-15 21:20:17 +02003050void addrconf_add_linklocal(struct inet6_dev *idev,
3051 const struct in6_addr *addr, u32 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052{
Eldad Zack8e5e8f32012-04-01 07:49:08 +00003053 struct inet6_ifaddr *ifp;
Hannes Frederic Sowa64236f32015-03-23 23:36:02 +01003054 u32 addr_flags = flags | IFA_F_PERMANENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055
Neil Horman95c385b2007-04-25 17:08:10 -07003056#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
3057 if (idev->cnf.optimistic_dad &&
David Miller702beb82008-07-20 18:17:02 -07003058 !dev_net(idev->dev)->ipv6.devconf_all->forwarding)
Neil Horman95c385b2007-04-25 17:08:10 -07003059 addr_flags |= IFA_F_OPTIMISTIC;
3060#endif
3061
Hannes Frederic Sowa07edd742014-01-08 15:43:22 +01003062 ifp = ipv6_add_addr(idev, addr, NULL, 64, IFA_LINK, addr_flags,
3063 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 if (!IS_ERR(ifp)) {
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09003065 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, idev->dev, 0, 0);
David S. Millercf22f9a2012-04-14 21:37:40 -04003066 addrconf_dad_start(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 in6_ifa_put(ifp);
3068 }
3069}
Alexander Aring2ad3ed52016-06-15 21:20:17 +02003070EXPORT_SYMBOL_GPL(addrconf_add_linklocal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01003072static bool ipv6_reserved_interfaceid(struct in6_addr address)
3073{
3074 if ((address.s6_addr32[2] | address.s6_addr32[3]) == 0)
3075 return true;
3076
3077 if (address.s6_addr32[2] == htonl(0x02005eff) &&
3078 ((address.s6_addr32[3] & htonl(0xfe000000)) == htonl(0xfe000000)))
3079 return true;
3080
3081 if (address.s6_addr32[2] == htonl(0xfdffffff) &&
3082 ((address.s6_addr32[3] & htonl(0xffffff80)) == htonl(0xffffff80)))
3083 return true;
3084
3085 return false;
3086}
3087
3088static int ipv6_generate_stable_address(struct in6_addr *address,
3089 u8 dad_count,
3090 const struct inet6_dev *idev)
3091{
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01003092 static DEFINE_SPINLOCK(lock);
3093 static __u32 digest[SHA_DIGEST_WORDS];
3094 static __u32 workspace[SHA_WORKSPACE_WORDS];
3095
3096 static union {
3097 char __data[SHA_MESSAGE_BYTES];
3098 struct {
3099 struct in6_addr secret;
Hannes Frederic Sowaff402172015-03-24 11:05:28 +01003100 __be32 prefix[2];
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01003101 unsigned char hwaddr[MAX_ADDR_LEN];
3102 u8 dad_count;
3103 } __packed;
3104 } data;
3105
3106 struct in6_addr secret;
3107 struct in6_addr temp;
3108 struct net *net = dev_net(idev->dev);
3109
3110 BUILD_BUG_ON(sizeof(data.__data) != sizeof(data));
3111
3112 if (idev->cnf.stable_secret.initialized)
3113 secret = idev->cnf.stable_secret.secret;
3114 else if (net->ipv6.devconf_dflt->stable_secret.initialized)
3115 secret = net->ipv6.devconf_dflt->stable_secret.secret;
3116 else
3117 return -1;
3118
3119retry:
3120 spin_lock_bh(&lock);
3121
3122 sha_init(digest);
3123 memset(&data, 0, sizeof(data));
3124 memset(workspace, 0, sizeof(workspace));
3125 memcpy(data.hwaddr, idev->dev->perm_addr, idev->dev->addr_len);
Hannes Frederic Sowaff402172015-03-24 11:05:28 +01003126 data.prefix[0] = address->s6_addr32[0];
3127 data.prefix[1] = address->s6_addr32[1];
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01003128 data.secret = secret;
3129 data.dad_count = dad_count;
3130
3131 sha_transform(digest, data.__data, workspace);
3132
3133 temp = *address;
Hannes Frederic Sowaff402172015-03-24 11:05:28 +01003134 temp.s6_addr32[2] = (__force __be32)digest[0];
3135 temp.s6_addr32[3] = (__force __be32)digest[1];
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01003136
3137 spin_unlock_bh(&lock);
3138
3139 if (ipv6_reserved_interfaceid(temp)) {
3140 dad_count++;
Hannes Frederic Sowa1855b7c2015-03-23 23:36:05 +01003141 if (dad_count > dev_net(idev->dev)->ipv6.sysctl.idgen_retries)
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01003142 return -1;
3143 goto retry;
3144 }
3145
3146 *address = temp;
3147 return 0;
3148}
3149
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003150static void ipv6_gen_mode_random_init(struct inet6_dev *idev)
3151{
3152 struct ipv6_stable_secret *s = &idev->cnf.stable_secret;
3153
3154 if (s->initialized)
3155 return;
3156 s = &idev->cnf.stable_secret;
3157 get_random_bytes(&s->secret, sizeof(s->secret));
3158 s->initialized = true;
3159}
3160
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003161static void addrconf_addr_gen(struct inet6_dev *idev, bool prefix_route)
3162{
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01003163 struct in6_addr addr;
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003164
David Ahernca254492015-10-12 11:47:10 -07003165 /* no link local addresses on L3 master devices */
3166 if (netif_is_l3_master(idev->dev))
3167 return;
3168
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01003169 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
3170
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003171 switch (idev->addr_gen_mode) {
3172 case IN6_ADDR_GEN_MODE_RANDOM:
3173 ipv6_gen_mode_random_init(idev);
3174 /* fallthrough */
3175 case IN6_ADDR_GEN_MODE_STABLE_PRIVACY:
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01003176 if (!ipv6_generate_stable_address(&addr, 0, idev))
Hannes Frederic Sowa64236f32015-03-23 23:36:02 +01003177 addrconf_add_linklocal(idev, &addr,
3178 IFA_F_STABLE_PRIVACY);
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01003179 else if (prefix_route)
3180 addrconf_prefix_route(&addr, 64, idev->dev, 0, 0);
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003181 break;
3182 case IN6_ADDR_GEN_MODE_EUI64:
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003183 /* addrconf_add_linklocal also adds a prefix_route and we
3184 * only need to care about prefix routes if ipv6_generate_eui64
3185 * couldn't generate one.
3186 */
3187 if (ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) == 0)
Hannes Frederic Sowa64236f32015-03-23 23:36:02 +01003188 addrconf_add_linklocal(idev, &addr, 0);
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003189 else if (prefix_route)
3190 addrconf_prefix_route(&addr, 64, idev->dev, 0, 0);
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003191 break;
3192 case IN6_ADDR_GEN_MODE_NONE:
3193 default:
3194 /* will not add any link local address */
3195 break;
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003196 }
3197}
3198
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199static void addrconf_dev_config(struct net_device *dev)
3200{
Eldad Zack8e5e8f32012-04-01 07:49:08 +00003201 struct inet6_dev *idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202
3203 ASSERT_RTNL();
3204
Herbert Xu74235a22007-06-14 13:02:55 -07003205 if ((dev->type != ARPHRD_ETHER) &&
3206 (dev->type != ARPHRD_FDDI) &&
Herbert Xu74235a22007-06-14 13:02:55 -07003207 (dev->type != ARPHRD_ARCNET) &&
alex.bluesman.smirnov@gmail.com06a4c1c2012-05-10 03:25:52 +00003208 (dev->type != ARPHRD_INFINIBAND) &&
Nicolas Dichtele8377352013-08-20 12:16:06 +02003209 (dev->type != ARPHRD_IEEE1394) &&
Jukka Rissanene74bccb2013-12-11 17:05:36 +02003210 (dev->type != ARPHRD_TUNNEL6) &&
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003211 (dev->type != ARPHRD_6LOWPAN) &&
3212 (dev->type != ARPHRD_NONE)) {
Herbert Xu74235a22007-06-14 13:02:55 -07003213 /* Alas, we support only Ethernet autoconfiguration. */
3214 return;
3215 }
3216
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 idev = addrconf_add_dev(dev);
Brian Haley64e724f2010-07-20 10:34:30 +00003218 if (IS_ERR(idev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 return;
3220
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003221 /* this device type has no EUI support */
3222 if (dev->type == ARPHRD_NONE &&
3223 idev->addr_gen_mode == IN6_ADDR_GEN_MODE_EUI64)
3224 idev->addr_gen_mode = IN6_ADDR_GEN_MODE_RANDOM;
3225
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003226 addrconf_addr_gen(idev, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227}
3228
Amerigo Wang07a93622012-10-29 16:23:10 +00003229#if IS_ENABLED(CONFIG_IPV6_SIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230static void addrconf_sit_config(struct net_device *dev)
3231{
3232 struct inet6_dev *idev;
3233
3234 ASSERT_RTNL();
3235
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003236 /*
3237 * Configure the tunnel with one of our IPv4
3238 * addresses... we should configure all of
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239 * our v4 addrs in the tunnel
3240 */
3241
Ian Morrise5d08d72014-11-23 21:28:43 +00003242 idev = ipv6_find_idev(dev);
Ian Morris63159f22015-03-29 14:00:04 +01003243 if (!idev) {
Joe Perches91df42b2012-05-15 14:11:54 +00003244 pr_debug("%s: add_dev failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 return;
3246 }
3247
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11003248 if (dev->priv_flags & IFF_ISATAP) {
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003249 addrconf_addr_gen(idev, false);
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11003250 return;
3251 }
3252
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 sit_add_v4_addrs(idev);
3254
Nicolas Dichtel62b54dd2012-10-01 23:19:14 +00003255 if (dev->flags&IFF_POINTOPOINT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256 addrconf_add_mroute(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257}
Joerg Roedel0be669b2006-10-10 14:49:53 -07003258#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259
Amerigo Wang07a93622012-10-29 16:23:10 +00003260#if IS_ENABLED(CONFIG_NET_IPGRE)
stephen hemmingeraee80b52011-06-08 10:44:30 +00003261static void addrconf_gre_config(struct net_device *dev)
3262{
3263 struct inet6_dev *idev;
stephen hemmingeraee80b52011-06-08 10:44:30 +00003264
stephen hemmingeraee80b52011-06-08 10:44:30 +00003265 ASSERT_RTNL();
3266
Ian Morrise5d08d72014-11-23 21:28:43 +00003267 idev = ipv6_find_idev(dev);
Ian Morris63159f22015-03-29 14:00:04 +01003268 if (!idev) {
Joe Perches91df42b2012-05-15 14:11:54 +00003269 pr_debug("%s: add_dev failed\n", __func__);
stephen hemmingeraee80b52011-06-08 10:44:30 +00003270 return;
3271 }
3272
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003273 addrconf_addr_gen(idev, true);
Hannes Frederic Sowad9e4ce62015-10-08 18:19:39 +02003274 if (dev->flags & IFF_POINTOPOINT)
3275 addrconf_add_mroute(dev);
stephen hemmingeraee80b52011-06-08 10:44:30 +00003276}
3277#endif
3278
David Ahernf1705ec2016-02-24 09:25:37 -08003279static int fixup_permanent_addr(struct inet6_dev *idev,
3280 struct inet6_ifaddr *ifp)
3281{
3282 if (!ifp->rt) {
3283 struct rt6_info *rt;
3284
3285 rt = addrconf_dst_alloc(idev, &ifp->addr, false);
3286 if (unlikely(IS_ERR(rt)))
3287 return PTR_ERR(rt);
3288
3289 ifp->rt = rt;
3290 }
3291
3292 if (!(ifp->flags & IFA_F_NOPREFIXROUTE)) {
3293 addrconf_prefix_route(&ifp->addr, ifp->prefix_len,
3294 idev->dev, 0, 0);
3295 }
3296
3297 addrconf_dad_start(ifp);
3298
3299 return 0;
3300}
3301
3302static void addrconf_permanent_addr(struct net_device *dev)
3303{
3304 struct inet6_ifaddr *ifp, *tmp;
3305 struct inet6_dev *idev;
3306
3307 idev = __in6_dev_get(dev);
3308 if (!idev)
3309 return;
3310
3311 write_lock_bh(&idev->lock);
3312
3313 list_for_each_entry_safe(ifp, tmp, &idev->addr_list, if_list) {
3314 if ((ifp->flags & IFA_F_PERMANENT) &&
3315 fixup_permanent_addr(idev, ifp) < 0) {
3316 write_unlock_bh(&idev->lock);
3317 ipv6_del_addr(ifp);
3318 write_lock_bh(&idev->lock);
3319
3320 net_info_ratelimited("%s: Failed to add prefix route for address %pI6c; dropping\n",
3321 idev->dev->name, &ifp->addr);
3322 }
3323 }
3324
3325 write_unlock_bh(&idev->lock);
3326}
3327
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003328static int addrconf_notify(struct notifier_block *this, unsigned long event,
Jiri Pirko351638e2013-05-28 01:30:21 +00003329 void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330{
Jiri Pirko351638e2013-05-28 01:30:21 +00003331 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
David Ahern4f7f34e2016-04-07 11:10:41 -07003332 struct netdev_notifier_changeupper_info *info;
Eric Dumazet748e2d92012-08-22 21:50:59 +00003333 struct inet6_dev *idev = __in6_dev_get(dev);
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09003334 int run_pending = 0;
Herbert Xub217d612007-07-30 17:04:52 -07003335 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336
Stephen Hemmingere21e8462010-03-20 16:09:01 -07003337 switch (event) {
YOSHIFUJI Hideaki45ba9dd2007-02-15 02:07:27 +09003338 case NETDEV_REGISTER:
Herbert Xu74235a22007-06-14 13:02:55 -07003339 if (!idev && dev->mtu >= IPV6_MIN_MTU) {
YOSHIFUJI Hideaki45ba9dd2007-02-15 02:07:27 +09003340 idev = ipv6_add_dev(dev);
WANG Conga317a2f2014-07-25 15:25:09 -07003341 if (IS_ERR(idev))
3342 return notifier_from_errno(PTR_ERR(idev));
YOSHIFUJI Hideaki45ba9dd2007-02-15 02:07:27 +09003343 }
3344 break;
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003345
Alexander Duyckb7b0b1d2015-10-26 11:06:33 -07003346 case NETDEV_CHANGEMTU:
3347 /* if MTU under IPV6_MIN_MTU stop IPv6 on this interface. */
3348 if (dev->mtu < IPV6_MIN_MTU) {
3349 addrconf_ifdown(dev, 1);
3350 break;
3351 }
3352
3353 if (idev) {
3354 rt6_mtu_change(dev, dev->mtu);
3355 idev->cnf.mtu6 = dev->mtu;
3356 break;
3357 }
3358
3359 /* allocate new idev */
3360 idev = ipv6_add_dev(dev);
3361 if (IS_ERR(idev))
3362 break;
3363
3364 /* device is still not ready */
3365 if (!(idev->if_flags & IF_READY))
3366 break;
3367
3368 run_pending = 1;
3369
3370 /* fall through */
3371
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372 case NETDEV_UP:
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003373 case NETDEV_CHANGE:
Jay Vosburghc2edacf2007-07-09 10:42:47 -07003374 if (dev->flags & IFF_SLAVE)
3375 break;
3376
WANG Cong3ce62a82014-09-11 15:07:16 -07003377 if (idev && idev->cnf.disable_ipv6)
3378 break;
3379
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003380 if (event == NETDEV_UP) {
David Ahern38bd10c2016-04-21 20:56:12 -07003381 /* restore routes for permanent addresses */
3382 addrconf_permanent_addr(dev);
3383
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -07003384 if (!addrconf_qdisc_ok(dev)) {
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003385 /* device is not ready yet. */
Joe Perchesf3213832012-05-15 14:11:53 +00003386 pr_info("ADDRCONF(NETDEV_UP): %s: link is not ready\n",
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003387 dev->name);
3388 break;
3389 }
Kristian Slavov99081042006-02-08 16:10:53 -08003390
Evgeniy Polyakovd31c7b82007-11-30 23:36:08 +11003391 if (!idev && dev->mtu >= IPV6_MIN_MTU)
3392 idev = ipv6_add_dev(dev);
3393
WANG Conga317a2f2014-07-25 15:25:09 -07003394 if (!IS_ERR_OR_NULL(idev)) {
Kristian Slavov99081042006-02-08 16:10:53 -08003395 idev->if_flags |= IF_READY;
Benjamin Therye3ec6cf2008-11-05 01:43:57 -08003396 run_pending = 1;
3397 }
Alexander Duyckb7b0b1d2015-10-26 11:06:33 -07003398 } else if (event == NETDEV_CHANGE) {
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -07003399 if (!addrconf_qdisc_ok(dev)) {
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003400 /* device is still not ready. */
3401 break;
3402 }
3403
3404 if (idev) {
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003405 if (idev->if_flags & IF_READY)
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003406 /* device is already configured. */
3407 break;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003408 idev->if_flags |= IF_READY;
3409 }
3410
Joe Perchesf3213832012-05-15 14:11:53 +00003411 pr_info("ADDRCONF(NETDEV_CHANGE): %s: link becomes ready\n",
3412 dev->name);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003413
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09003414 run_pending = 1;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003415 }
3416
Stephen Hemmingere21e8462010-03-20 16:09:01 -07003417 switch (dev->type) {
Amerigo Wang07a93622012-10-29 16:23:10 +00003418#if IS_ENABLED(CONFIG_IPV6_SIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419 case ARPHRD_SIT:
3420 addrconf_sit_config(dev);
3421 break;
Joerg Roedel0be669b2006-10-10 14:49:53 -07003422#endif
Amerigo Wang07a93622012-10-29 16:23:10 +00003423#if IS_ENABLED(CONFIG_NET_IPGRE)
stephen hemmingeraee80b52011-06-08 10:44:30 +00003424 case ARPHRD_IPGRE:
3425 addrconf_gre_config(dev);
3426 break;
3427#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428 case ARPHRD_LOOPBACK:
3429 init_loopback(dev);
3430 break;
3431
3432 default:
3433 addrconf_dev_config(dev);
3434 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07003435 }
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003436
WANG Conga317a2f2014-07-25 15:25:09 -07003437 if (!IS_ERR_OR_NULL(idev)) {
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09003438 if (run_pending)
3439 addrconf_dad_run(idev);
3440
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003441 /*
3442 * If the MTU changed during the interface down,
3443 * when the interface up, the changed MTU must be
3444 * reflected in the idev as well as routers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445 */
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003446 if (idev->cnf.mtu6 != dev->mtu &&
3447 dev->mtu >= IPV6_MIN_MTU) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448 rt6_mtu_change(dev, dev->mtu);
3449 idev->cnf.mtu6 = dev->mtu;
3450 }
3451 idev->tstamp = jiffies;
3452 inet6_ifinfo_notify(RTM_NEWLINK, idev);
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003453
3454 /*
3455 * If the changed mtu during down is lower than
3456 * IPV6_MIN_MTU stop IPv6 on this interface.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457 */
3458 if (dev->mtu < IPV6_MIN_MTU)
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003459 addrconf_ifdown(dev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460 }
3461 break;
3462
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463 case NETDEV_DOWN:
3464 case NETDEV_UNREGISTER:
3465 /*
3466 * Remove all addresses from this interface.
3467 */
3468 addrconf_ifdown(dev, event != NETDEV_DOWN);
3469 break;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003470
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471 case NETDEV_CHANGENAME:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472 if (idev) {
Stephen Hemminger5632c512007-04-28 21:16:39 -07003473 snmp6_unregister_dev(idev);
Pavel Emelyanov408c4762008-01-10 17:41:21 -08003474 addrconf_sysctl_unregister(idev);
WANG Conga317a2f2014-07-25 15:25:09 -07003475 err = addrconf_sysctl_register(idev);
Herbert Xub217d612007-07-30 17:04:52 -07003476 if (err)
3477 return notifier_from_errno(err);
WANG Conga317a2f2014-07-25 15:25:09 -07003478 err = snmp6_register_dev(idev);
3479 if (err) {
3480 addrconf_sysctl_unregister(idev);
3481 return notifier_from_errno(err);
3482 }
Stephen Hemminger5632c512007-04-28 21:16:39 -07003483 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484 break;
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003485
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00003486 case NETDEV_PRE_TYPE_CHANGE:
3487 case NETDEV_POST_TYPE_CHANGE:
Andrew Lunn3ef09522015-12-03 21:12:32 +01003488 if (idev)
3489 addrconf_type_change(dev, event);
Moni Shoua75c78502009-09-15 02:37:40 -07003490 break;
David Ahern4f7f34e2016-04-07 11:10:41 -07003491
3492 case NETDEV_CHANGEUPPER:
3493 info = ptr;
3494
3495 /* flush all routes if dev is linked to or unlinked from
3496 * an L3 master device (e.g., VRF)
3497 */
3498 if (info->upper_dev && netif_is_l3_master(info->upper_dev))
3499 addrconf_ifdown(dev, 0);
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07003500 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501
3502 return NOTIFY_OK;
3503}
3504
3505/*
3506 * addrconf module should be notified of a device going up
3507 */
3508static struct notifier_block ipv6_dev_notf = {
3509 .notifier_call = addrconf_notify,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510};
3511
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00003512static void addrconf_type_change(struct net_device *dev, unsigned long event)
Moni Shoua75c78502009-09-15 02:37:40 -07003513{
3514 struct inet6_dev *idev;
3515 ASSERT_RTNL();
3516
3517 idev = __in6_dev_get(dev);
3518
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00003519 if (event == NETDEV_POST_TYPE_CHANGE)
Moni Shoua75c78502009-09-15 02:37:40 -07003520 ipv6_mc_remap(idev);
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00003521 else if (event == NETDEV_PRE_TYPE_CHANGE)
Moni Shoua75c78502009-09-15 02:37:40 -07003522 ipv6_mc_unmap(idev);
3523}
3524
David Ahern70af9212016-04-08 12:01:21 -07003525static bool addr_is_local(const struct in6_addr *addr)
3526{
3527 return ipv6_addr_type(addr) &
3528 (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK);
3529}
3530
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531static int addrconf_ifdown(struct net_device *dev, int how)
3532{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09003533 struct net *net = dev_net(dev);
stephen hemminger502a2ff2010-03-17 20:31:13 +00003534 struct inet6_dev *idev;
David Ahernf1705ec2016-02-24 09:25:37 -08003535 struct inet6_ifaddr *ifa, *tmp;
3536 struct list_head del_list;
3537 int _keep_addr;
3538 bool keep_addr;
David S. Miller73a8bd72011-01-23 23:27:15 -08003539 int state, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540
3541 ASSERT_RTNL();
3542
David S. Miller73a8bd72011-01-23 23:27:15 -08003543 rt6_ifdown(net, dev);
3544 neigh_ifdown(&nd_tbl, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545
3546 idev = __in6_dev_get(dev);
Ian Morris63159f22015-03-29 14:00:04 +01003547 if (!idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548 return -ENODEV;
3549
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003550 /*
3551 * Step 1: remove reference to ipv6 device from parent device.
3552 * Do not dev_put!
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553 */
Denis V. Lunev439e2382008-04-03 13:30:17 -07003554 if (how) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555 idev->dead = 1;
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07003556
3557 /* protected by rtnl_lock */
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00003558 RCU_INIT_POINTER(dev->ip6_ptr, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559
3560 /* Step 1.5: remove snmp6 entry */
3561 snmp6_unregister_dev(idev);
3562
3563 }
3564
David Ahernf1705ec2016-02-24 09:25:37 -08003565 /* aggregate the system setting and interface setting */
3566 _keep_addr = net->ipv6.devconf_all->keep_addr_on_down;
3567 if (!_keep_addr)
3568 _keep_addr = idev->cnf.keep_addr_on_down;
3569
3570 /* combine the user config with event to determine if permanent
3571 * addresses are to be removed from address hash table
3572 */
Mike Manningbc561632016-08-12 12:02:38 +01003573 keep_addr = !(how || _keep_addr <= 0 || idev->cnf.disable_ipv6);
David Ahernf1705ec2016-02-24 09:25:37 -08003574
David S. Miller73a8bd72011-01-23 23:27:15 -08003575 /* Step 2: clear hash table */
3576 for (i = 0; i < IN6_ADDR_HSIZE; i++) {
3577 struct hlist_head *h = &inet6_addr_lst[i];
David S. Miller73a8bd72011-01-23 23:27:15 -08003578
3579 spin_lock_bh(&addrconf_hash_lock);
Ian Morris67ba4152014-08-24 21:53:10 +01003580restart:
Sasha Levinb67bfe02013-02-27 17:06:00 -08003581 hlist_for_each_entry_rcu(ifa, h, addr_lst) {
David S. Miller73a8bd72011-01-23 23:27:15 -08003582 if (ifa->idev == idev) {
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003583 addrconf_del_dad_work(ifa);
David Ahernf1705ec2016-02-24 09:25:37 -08003584 /* combined flag + permanent flag decide if
3585 * address is retained on a down event
3586 */
3587 if (!keep_addr ||
David Ahern70af9212016-04-08 12:01:21 -07003588 !(ifa->flags & IFA_F_PERMANENT) ||
3589 addr_is_local(&ifa->addr)) {
David Ahernf1705ec2016-02-24 09:25:37 -08003590 hlist_del_init_rcu(&ifa->addr_lst);
3591 goto restart;
3592 }
David S. Miller73a8bd72011-01-23 23:27:15 -08003593 }
3594 }
3595 spin_unlock_bh(&addrconf_hash_lock);
3596 }
3597
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598 write_lock_bh(&idev->lock);
3599
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003600 addrconf_del_rs_timer(idev);
3601
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003602 /* Step 2: clear flags for stateless addrconf */
Denis V. Lunev439e2382008-04-03 13:30:17 -07003603 if (!how)
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003604 idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605
Denis V. Lunev439e2382008-04-03 13:30:17 -07003606 if (how && del_timer(&idev->regen_timer))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607 in6_dev_put(idev);
3608
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003609 /* Step 3: clear tempaddr list */
stephen hemminger372e6c82010-03-17 20:31:09 +00003610 while (!list_empty(&idev->tempaddr_list)) {
3611 ifa = list_first_entry(&idev->tempaddr_list,
3612 struct inet6_ifaddr, tmp_list);
3613 list_del(&ifa->tmp_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614 write_unlock_bh(&idev->lock);
3615 spin_lock_bh(&ifa->lock);
3616
3617 if (ifa->ifpub) {
3618 in6_ifa_put(ifa->ifpub);
3619 ifa->ifpub = NULL;
3620 }
3621 spin_unlock_bh(&ifa->lock);
3622 in6_ifa_put(ifa);
3623 write_lock_bh(&idev->lock);
3624 }
stephen hemminger8f37ada2010-03-03 08:19:59 +00003625
David Ahernf1705ec2016-02-24 09:25:37 -08003626 /* re-combine the user config with event to determine if permanent
3627 * addresses are to be removed from the interface list
3628 */
Mike Manningbc561632016-08-12 12:02:38 +01003629 keep_addr = (!how && _keep_addr > 0 && !idev->cnf.disable_ipv6);
David Ahernf1705ec2016-02-24 09:25:37 -08003630
3631 INIT_LIST_HEAD(&del_list);
3632 list_for_each_entry_safe(ifa, tmp, &idev->addr_list, if_list) {
David Ahern38bd10c2016-04-21 20:56:12 -07003633 struct rt6_info *rt = NULL;
3634
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003635 addrconf_del_dad_work(ifa);
stephen hemminger84e8b802010-03-02 13:32:46 +00003636
David S. Miller73a8bd72011-01-23 23:27:15 -08003637 write_unlock_bh(&idev->lock);
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01003638 spin_lock_bh(&ifa->lock);
David Ahernf1705ec2016-02-24 09:25:37 -08003639
David Ahern70af9212016-04-08 12:01:21 -07003640 if (keep_addr && (ifa->flags & IFA_F_PERMANENT) &&
3641 !addr_is_local(&ifa->addr)) {
David Ahernf1705ec2016-02-24 09:25:37 -08003642 /* set state to skip the notifier below */
3643 state = INET6_IFADDR_STATE_DEAD;
3644 ifa->state = 0;
3645 if (!(ifa->flags & IFA_F_NODAD))
3646 ifa->flags |= IFA_F_TENTATIVE;
David Ahern38bd10c2016-04-21 20:56:12 -07003647
3648 rt = ifa->rt;
3649 ifa->rt = NULL;
David Ahernf1705ec2016-02-24 09:25:37 -08003650 } else {
3651 state = ifa->state;
3652 ifa->state = INET6_IFADDR_STATE_DEAD;
3653
Wei Yongjunc8822192016-07-28 16:19:03 +00003654 list_move(&ifa->if_list, &del_list);
David Ahernf1705ec2016-02-24 09:25:37 -08003655 }
3656
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01003657 spin_unlock_bh(&ifa->lock);
stephen hemminger84e8b802010-03-02 13:32:46 +00003658
David Ahern38bd10c2016-04-21 20:56:12 -07003659 if (rt)
3660 ip6_del_rt(rt);
3661
David S. Miller73a8bd72011-01-23 23:27:15 -08003662 if (state != INET6_IFADDR_STATE_DEAD) {
3663 __ipv6_ifa_notify(RTM_DELADDR, ifa);
Cong Wangf88c91d2013-04-14 23:18:43 +08003664 inet6addr_notifier_call_chain(NETDEV_DOWN, ifa);
Mike Manningea06f712016-07-22 18:32:11 +01003665 } else {
3666 if (idev->cnf.forwarding)
3667 addrconf_leave_anycast(ifa);
3668 addrconf_leave_solict(ifa->idev, &ifa->addr);
stephen hemminger27bdb2a2010-04-12 05:41:32 +00003669 }
stephen hemminger8f37ada2010-03-03 08:19:59 +00003670
David S. Miller73a8bd72011-01-23 23:27:15 -08003671 write_lock_bh(&idev->lock);
3672 }
stephen hemminger8f37ada2010-03-03 08:19:59 +00003673
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674 write_unlock_bh(&idev->lock);
3675
David Ahernf1705ec2016-02-24 09:25:37 -08003676 /* now clean up addresses to be removed */
3677 while (!list_empty(&del_list)) {
3678 ifa = list_first_entry(&del_list,
3679 struct inet6_ifaddr, if_list);
3680 list_del(&ifa->if_list);
3681
3682 in6_ifa_put(ifa);
3683 }
3684
Sabrina Dubroca381f4dc2014-09-10 23:23:02 +02003685 /* Step 5: Discard anycast and multicast list */
3686 if (how) {
3687 ipv6_ac_destroy_dev(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688 ipv6_mc_destroy_dev(idev);
Sabrina Dubroca381f4dc2014-09-10 23:23:02 +02003689 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690 ipv6_mc_down(idev);
Sabrina Dubroca381f4dc2014-09-10 23:23:02 +02003691 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693 idev->tstamp = jiffies;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003694
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003695 /* Last: Shot the device (if unregistered) */
Denis V. Lunev439e2382008-04-03 13:30:17 -07003696 if (how) {
Pavel Emelyanov408c4762008-01-10 17:41:21 -08003697 addrconf_sysctl_unregister(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698 neigh_parms_release(&nd_tbl, idev->nd_parms);
3699 neigh_ifdown(&nd_tbl, dev);
3700 in6_dev_put(idev);
3701 }
3702 return 0;
3703}
3704
3705static void addrconf_rs_timer(unsigned long data)
3706{
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003707 struct inet6_dev *idev = (struct inet6_dev *)data;
Cong Wangcaf92bc2013-08-31 13:44:32 +08003708 struct net_device *dev = idev->dev;
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003709 struct in6_addr lladdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003711 write_lock(&idev->lock);
stephen hemminger5b2a1952010-03-02 13:32:45 +00003712 if (idev->dead || !(idev->if_flags & IF_READY))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713 goto out;
3714
Shmulik Ladkani9ba2add2012-12-02 01:44:53 +00003715 if (!ipv6_accept_ra(idev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716 goto out;
stephen hemminger5b2a1952010-03-02 13:32:45 +00003717
3718 /* Announcement received after solicitation was sent */
3719 if (idev->if_flags & IF_RA_RCVD)
3720 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003722 if (idev->rs_probes++ < idev->cnf.rtr_solicits) {
Cong Wangcaf92bc2013-08-31 13:44:32 +08003723 write_unlock(&idev->lock);
3724 if (!ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
3725 ndisc_send_rs(dev, &lladdr,
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003726 &in6addr_linklocal_allrouters);
3727 else
Cong Wangcaf92bc2013-08-31 13:44:32 +08003728 goto put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729
Cong Wangcaf92bc2013-08-31 13:44:32 +08003730 write_lock(&idev->lock);
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003731 /* The wait after the last probe can be shorter */
3732 addrconf_mod_rs_timer(idev, (idev->rs_probes ==
3733 idev->cnf.rtr_solicits) ?
3734 idev->cnf.rtr_solicit_delay :
3735 idev->cnf.rtr_solicit_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003737 /*
3738 * Note: we do not support deprecated "all on-link"
3739 * assumption any longer.
3740 */
Joe Perches91df42b2012-05-15 14:11:54 +00003741 pr_debug("%s: no IPv6 routers present\n", idev->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742 }
3743
3744out:
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003745 write_unlock(&idev->lock);
Cong Wangcaf92bc2013-08-31 13:44:32 +08003746put:
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003747 in6_dev_put(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748}
3749
3750/*
3751 * Duplicate Address Detection
3752 */
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003753static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
3754{
3755 unsigned long rand_num;
3756 struct inet6_dev *idev = ifp->idev;
3757
Neil Horman95c385b2007-04-25 17:08:10 -07003758 if (ifp->flags & IFA_F_OPTIMISTIC)
3759 rand_num = 0;
3760 else
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05003761 rand_num = prandom_u32() % (idev->cnf.rtr_solicit_delay ? : 1);
Neil Horman95c385b2007-04-25 17:08:10 -07003762
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003763 ifp->dad_probes = idev->cnf.dad_transmits;
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003764 addrconf_mod_dad_work(ifp, rand_num);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003765}
3766
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003767static void addrconf_dad_begin(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768{
3769 struct inet6_dev *idev = ifp->idev;
3770 struct net_device *dev = idev->dev;
subashab@codeaurora.org16186a82016-02-02 02:11:10 +00003771 bool notify = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772
3773 addrconf_join_solict(dev, &ifp->addr);
3774
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05003775 prandom_seed((__force u32) ifp->addr.s6_addr32[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776
3777 read_lock_bh(&idev->lock);
Herbert Xu622ccdf2010-05-18 15:56:06 -07003778 spin_lock(&ifp->lock);
Herbert Xue9d3e082010-05-18 15:36:06 -07003779 if (ifp->state == INET6_IFADDR_STATE_DEAD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781
3782 if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09003783 idev->cnf.accept_dad < 1 ||
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003784 !(ifp->flags&IFA_F_TENTATIVE) ||
3785 ifp->flags & IFA_F_NODAD) {
Brian Haleycc411d02009-09-09 14:41:32 +00003786 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
stephen hemminger21809fa2010-02-08 19:48:52 +00003787 spin_unlock(&ifp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788 read_unlock_bh(&idev->lock);
3789
3790 addrconf_dad_completed(ifp);
3791 return;
3792 }
3793
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08003794 if (!(idev->if_flags & IF_READY)) {
stephen hemminger21809fa2010-02-08 19:48:52 +00003795 spin_unlock(&ifp->lock);
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08003796 read_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003797 /*
Masatake YAMATO590a9882009-06-09 10:41:12 +09003798 * If the device is not ready:
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003799 * - keep it tentative if it is a permanent address.
3800 * - otherwise, kill it.
3801 */
3802 in6_ifa_hold(ifp);
Brian Haleycc411d02009-09-09 14:41:32 +00003803 addrconf_dad_stop(ifp, 0);
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08003804 return;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003805 }
Neil Horman95c385b2007-04-25 17:08:10 -07003806
3807 /*
3808 * Optimistic nodes can start receiving
3809 * Frames right away
3810 */
Erik Kline7fd25612014-10-28 18:11:14 +09003811 if (ifp->flags & IFA_F_OPTIMISTIC) {
Neil Horman95c385b2007-04-25 17:08:10 -07003812 ip6_ins_rt(ifp->rt);
Erik Kline7fd25612014-10-28 18:11:14 +09003813 if (ipv6_use_optimistic_addr(idev)) {
3814 /* Because optimistic nodes can use this address,
3815 * notify listeners. If DAD fails, RTM_DELADDR is sent.
3816 */
subashab@codeaurora.org16186a82016-02-02 02:11:10 +00003817 notify = true;
Erik Kline7fd25612014-10-28 18:11:14 +09003818 }
3819 }
Neil Horman95c385b2007-04-25 17:08:10 -07003820
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08003821 addrconf_dad_kick(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822out:
Herbert Xu622ccdf2010-05-18 15:56:06 -07003823 spin_unlock(&ifp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824 read_unlock_bh(&idev->lock);
subashab@codeaurora.org16186a82016-02-02 02:11:10 +00003825 if (notify)
3826 ipv6_ifa_notify(RTM_NEWADDR, ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827}
3828
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003829static void addrconf_dad_start(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830{
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003831 bool begin_dad = false;
3832
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01003833 spin_lock_bh(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003834 if (ifp->state != INET6_IFADDR_STATE_DEAD) {
3835 ifp->state = INET6_IFADDR_STATE_PREDAD;
3836 begin_dad = true;
3837 }
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01003838 spin_unlock_bh(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003839
3840 if (begin_dad)
3841 addrconf_mod_dad_work(ifp, 0);
3842}
3843
3844static void addrconf_dad_work(struct work_struct *w)
3845{
3846 struct inet6_ifaddr *ifp = container_of(to_delayed_work(w),
3847 struct inet6_ifaddr,
3848 dad_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849 struct inet6_dev *idev = ifp->idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850 struct in6_addr mcaddr;
3851
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003852 enum {
3853 DAD_PROCESS,
3854 DAD_BEGIN,
3855 DAD_ABORT,
3856 } action = DAD_PROCESS;
3857
3858 rtnl_lock();
3859
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01003860 spin_lock_bh(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003861 if (ifp->state == INET6_IFADDR_STATE_PREDAD) {
3862 action = DAD_BEGIN;
3863 ifp->state = INET6_IFADDR_STATE_DAD;
3864 } else if (ifp->state == INET6_IFADDR_STATE_ERRDAD) {
3865 action = DAD_ABORT;
3866 ifp->state = INET6_IFADDR_STATE_POSTDAD;
3867 }
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01003868 spin_unlock_bh(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003869
3870 if (action == DAD_BEGIN) {
3871 addrconf_dad_begin(ifp);
3872 goto out;
3873 } else if (action == DAD_ABORT) {
3874 addrconf_dad_stop(ifp, 1);
3875 goto out;
3876 }
3877
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003878 if (!ifp->dad_probes && addrconf_dad_end(ifp))
Herbert Xuf2344a12010-05-18 15:55:27 -07003879 goto out;
3880
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003881 write_lock_bh(&idev->lock);
stephen hemminger122e4512010-03-02 13:32:44 +00003882 if (idev->dead || !(idev->if_flags & IF_READY)) {
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003883 write_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884 goto out;
3885 }
stephen hemminger21809fa2010-02-08 19:48:52 +00003886
3887 spin_lock(&ifp->lock);
Herbert Xu622ccdf2010-05-18 15:56:06 -07003888 if (ifp->state == INET6_IFADDR_STATE_DEAD) {
3889 spin_unlock(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003890 write_unlock_bh(&idev->lock);
Herbert Xu622ccdf2010-05-18 15:56:06 -07003891 goto out;
3892 }
3893
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003894 if (ifp->dad_probes == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895 /*
3896 * DAD was successful
3897 */
3898
Brian Haleycc411d02009-09-09 14:41:32 +00003899 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
stephen hemminger21809fa2010-02-08 19:48:52 +00003900 spin_unlock(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003901 write_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902
3903 addrconf_dad_completed(ifp);
3904
3905 goto out;
3906 }
3907
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003908 ifp->dad_probes--;
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003909 addrconf_mod_dad_work(ifp,
3910 NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME));
stephen hemminger21809fa2010-02-08 19:48:52 +00003911 spin_unlock(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003912 write_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913
3914 /* send a neighbour solicitation for our addr */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915 addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
Nicolas Dichtel304d8882015-11-27 18:17:05 +01003916 ndisc_send_ns(ifp->idev->dev, &ifp->addr, &mcaddr, &in6addr_any);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917out:
3918 in6_ifa_put(ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003919 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003920}
3921
Hannes Frederic Sowa11ffff72014-01-16 20:13:04 +01003922/* ifp->idev must be at least read locked */
3923static bool ipv6_lonely_lladdr(struct inet6_ifaddr *ifp)
3924{
3925 struct inet6_ifaddr *ifpiter;
3926 struct inet6_dev *idev = ifp->idev;
3927
Hannes Frederic Sowa602582c2014-01-19 21:58:19 +01003928 list_for_each_entry_reverse(ifpiter, &idev->addr_list, if_list) {
3929 if (ifpiter->scope > IFA_LINK)
3930 break;
Hannes Frederic Sowa11ffff72014-01-16 20:13:04 +01003931 if (ifp != ifpiter && ifpiter->scope == IFA_LINK &&
3932 (ifpiter->flags & (IFA_F_PERMANENT|IFA_F_TENTATIVE|
3933 IFA_F_OPTIMISTIC|IFA_F_DADFAILED)) ==
3934 IFA_F_PERMANENT)
3935 return false;
3936 }
3937 return true;
3938}
3939
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940static void addrconf_dad_completed(struct inet6_ifaddr *ifp)
3941{
Stephen Hemmingere21e8462010-03-20 16:09:01 -07003942 struct net_device *dev = ifp->idev->dev;
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003943 struct in6_addr lladdr;
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02003944 bool send_rs, send_mld;
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003945
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003946 addrconf_del_dad_work(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947
3948 /*
3949 * Configure the address for reception. Now it is valid.
3950 */
3951
3952 ipv6_ifa_notify(RTM_NEWADDR, ifp);
3953
Tore Anderson026359b2011-08-28 23:47:33 +00003954 /* If added prefix is link local and we are prepared to process
3955 router advertisements, start sending router solicitations.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956 */
3957
Hannes Frederic Sowa1ec047e2013-06-27 00:06:56 +02003958 read_lock_bh(&ifp->idev->lock);
Hannes Frederic Sowa11ffff72014-01-16 20:13:04 +01003959 send_mld = ifp->scope == IFA_LINK && ipv6_lonely_lladdr(ifp);
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02003960 send_rs = send_mld &&
3961 ipv6_accept_ra(ifp->idev) &&
Hannes Frederic Sowa1ec047e2013-06-27 00:06:56 +02003962 ifp->idev->cnf.rtr_solicits > 0 &&
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02003963 (dev->flags&IFF_LOOPBACK) == 0;
Hannes Frederic Sowa1ec047e2013-06-27 00:06:56 +02003964 read_unlock_bh(&ifp->idev->lock);
3965
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02003966 /* While dad is in progress mld report's source address is in6_addrany.
3967 * Resend with proper ll now.
3968 */
3969 if (send_mld)
3970 ipv6_mc_dad_complete(ifp->idev);
3971
Hannes Frederic Sowa1ec047e2013-06-27 00:06:56 +02003972 if (send_rs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973 /*
3974 * If a host as already performed a random delay
3975 * [...] as part of DAD [...] there is no need
3976 * to delay again before sending the first RS
3977 */
Hannes Frederic Sowa1ec047e2013-06-27 00:06:56 +02003978 if (ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003979 return;
Hannes Frederic Sowa1ec047e2013-06-27 00:06:56 +02003980 ndisc_send_rs(dev, &lladdr, &in6addr_linklocal_allrouters);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003982 write_lock_bh(&ifp->idev->lock);
3983 spin_lock(&ifp->lock);
3984 ifp->idev->rs_probes = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985 ifp->idev->if_flags |= IF_RS_SENT;
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003986 addrconf_mod_rs_timer(ifp->idev,
3987 ifp->idev->cnf.rtr_solicit_interval);
3988 spin_unlock(&ifp->lock);
3989 write_unlock_bh(&ifp->idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990 }
3991}
3992
Stephen Hemmingere21e8462010-03-20 16:09:01 -07003993static void addrconf_dad_run(struct inet6_dev *idev)
3994{
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09003995 struct inet6_ifaddr *ifp;
3996
3997 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00003998 list_for_each_entry(ifp, &idev->addr_list, if_list) {
stephen hemminger21809fa2010-02-08 19:48:52 +00003999 spin_lock(&ifp->lock);
Herbert Xuf2344a12010-05-18 15:55:27 -07004000 if (ifp->flags & IFA_F_TENTATIVE &&
4001 ifp->state == INET6_IFADDR_STATE_DAD)
4002 addrconf_dad_kick(ifp);
stephen hemminger21809fa2010-02-08 19:48:52 +00004003 spin_unlock(&ifp->lock);
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09004004 }
4005 read_unlock_bh(&idev->lock);
4006}
4007
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008#ifdef CONFIG_PROC_FS
4009struct if6_iter_state {
Daniel Lezcano3c400902008-01-10 22:42:49 -08004010 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011 int bucket;
Mihai Maruseac1d578302012-01-03 23:31:35 +00004012 int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013};
4014
Mihai Maruseac1d578302012-01-03 23:31:35 +00004015static struct inet6_ifaddr *if6_get_first(struct seq_file *seq, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016{
4017 struct inet6_ifaddr *ifa = NULL;
4018 struct if6_iter_state *state = seq->private;
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09004019 struct net *net = seq_file_net(seq);
Mihai Maruseac1d578302012-01-03 23:31:35 +00004020 int p = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021
Mihai Maruseac1d578302012-01-03 23:31:35 +00004022 /* initial bucket if pos is 0 */
4023 if (pos == 0) {
4024 state->bucket = 0;
4025 state->offset = 0;
4026 }
4027
4028 for (; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08004029 hlist_for_each_entry_rcu_bh(ifa, &inet6_addr_lst[state->bucket],
Mihai Maruseac1d578302012-01-03 23:31:35 +00004030 addr_lst) {
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00004031 if (!net_eq(dev_net(ifa->idev->dev), net))
4032 continue;
Mihai Maruseac1d578302012-01-03 23:31:35 +00004033 /* sync with offset */
4034 if (p < state->offset) {
4035 p++;
4036 continue;
4037 }
4038 state->offset++;
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00004039 return ifa;
Mihai Maruseac1d578302012-01-03 23:31:35 +00004040 }
4041
4042 /* prepare for next bucket */
4043 state->offset = 0;
4044 p = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045 }
stephen hemmingerc2e21292010-03-17 20:31:10 +00004046 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047}
4048
stephen hemmingerc2e21292010-03-17 20:31:10 +00004049static struct inet6_ifaddr *if6_get_next(struct seq_file *seq,
4050 struct inet6_ifaddr *ifa)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051{
4052 struct if6_iter_state *state = seq->private;
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09004053 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054
Sasha Levinb67bfe02013-02-27 17:06:00 -08004055 hlist_for_each_entry_continue_rcu_bh(ifa, addr_lst) {
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00004056 if (!net_eq(dev_net(ifa->idev->dev), net))
4057 continue;
Mihai Maruseac1d578302012-01-03 23:31:35 +00004058 state->offset++;
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00004059 return ifa;
Mihai Maruseac1d578302012-01-03 23:31:35 +00004060 }
stephen hemmingerc2e21292010-03-17 20:31:10 +00004061
4062 while (++state->bucket < IN6_ADDR_HSIZE) {
Mihai Maruseac1d578302012-01-03 23:31:35 +00004063 state->offset = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -08004064 hlist_for_each_entry_rcu_bh(ifa,
stephen hemmingerc2e21292010-03-17 20:31:10 +00004065 &inet6_addr_lst[state->bucket], addr_lst) {
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00004066 if (!net_eq(dev_net(ifa->idev->dev), net))
4067 continue;
Mihai Maruseac1d578302012-01-03 23:31:35 +00004068 state->offset++;
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00004069 return ifa;
Daniel Lezcano3c400902008-01-10 22:42:49 -08004070 }
4071 }
4072
stephen hemmingerc2e21292010-03-17 20:31:10 +00004073 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074}
4075
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
Eric Dumazet4f70ecc2010-05-03 10:50:14 +00004077 __acquires(rcu_bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078{
stephen hemminger5c578ae2010-03-17 20:31:11 +00004079 rcu_read_lock_bh();
Mihai Maruseac1d578302012-01-03 23:31:35 +00004080 return if6_get_first(seq, *pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081}
4082
4083static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
4084{
4085 struct inet6_ifaddr *ifa;
4086
4087 ifa = if6_get_next(seq, v);
4088 ++*pos;
4089 return ifa;
4090}
4091
4092static void if6_seq_stop(struct seq_file *seq, void *v)
Eric Dumazet4f70ecc2010-05-03 10:50:14 +00004093 __releases(rcu_bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094{
stephen hemminger5c578ae2010-03-17 20:31:11 +00004095 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096}
4097
4098static int if6_seq_show(struct seq_file *seq, void *v)
4099{
4100 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
Jiri Pirko971a3512013-12-10 13:56:29 +01004101 seq_printf(seq, "%pi6 %02x %02x %02x %02x %8s\n",
Harvey Harrisonb0711952008-10-28 16:05:40 -07004102 &ifp->addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004103 ifp->idev->dev->ifindex,
4104 ifp->prefix_len,
4105 ifp->scope,
Jiri Pirko971a3512013-12-10 13:56:29 +01004106 (u8) ifp->flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107 ifp->idev->dev->name);
4108 return 0;
4109}
4110
Philippe De Muyter56b3d972007-07-10 23:07:31 -07004111static const struct seq_operations if6_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112 .start = if6_seq_start,
4113 .next = if6_seq_next,
4114 .show = if6_seq_show,
4115 .stop = if6_seq_stop,
4116};
4117
4118static int if6_seq_open(struct inode *inode, struct file *file)
4119{
Daniel Lezcano3c400902008-01-10 22:42:49 -08004120 return seq_open_net(inode, file, &if6_seq_ops,
4121 sizeof(struct if6_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122}
4123
Arjan van de Ven9a321442007-02-12 00:55:35 -08004124static const struct file_operations if6_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125 .owner = THIS_MODULE,
4126 .open = if6_seq_open,
4127 .read = seq_read,
4128 .llseek = seq_lseek,
Daniel Lezcano3c400902008-01-10 22:42:49 -08004129 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130};
4131
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004132static int __net_init if6_proc_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133{
Gao fengd4beaa62013-02-18 01:34:54 +00004134 if (!proc_create("if_inet6", S_IRUGO, net->proc_net, &if6_fops))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004135 return -ENOMEM;
4136 return 0;
4137}
4138
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004139static void __net_exit if6_proc_net_exit(struct net *net)
Daniel Lezcano3c400902008-01-10 22:42:49 -08004140{
Gao fengece31ff2013-02-18 01:34:56 +00004141 remove_proc_entry("if_inet6", net->proc_net);
Daniel Lezcano3c400902008-01-10 22:42:49 -08004142}
4143
4144static struct pernet_operations if6_proc_net_ops = {
Ian Morris67ba4152014-08-24 21:53:10 +01004145 .init = if6_proc_net_init,
4146 .exit = if6_proc_net_exit,
Daniel Lezcano3c400902008-01-10 22:42:49 -08004147};
4148
4149int __init if6_proc_init(void)
4150{
4151 return register_pernet_subsys(&if6_proc_net_ops);
4152}
4153
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154void if6_proc_exit(void)
4155{
Daniel Lezcano3c400902008-01-10 22:42:49 -08004156 unregister_pernet_subsys(&if6_proc_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157}
4158#endif /* CONFIG_PROC_FS */
4159
Amerigo Wang07a93622012-10-29 16:23:10 +00004160#if IS_ENABLED(CONFIG_IPV6_MIP6)
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07004161/* Check if address is a home address configured on any interface. */
Eric Dumazetb71d1d42011-04-22 04:53:02 +00004162int ipv6_chk_home_addr(struct net *net, const struct in6_addr *addr)
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07004163{
4164 int ret = 0;
stephen hemmingerc2e21292010-03-17 20:31:10 +00004165 struct inet6_ifaddr *ifp = NULL;
Eric Dumazetddbe5032012-07-18 08:11:12 +00004166 unsigned int hash = inet6_addr_hash(addr);
stephen hemmingerc2e21292010-03-17 20:31:10 +00004167
stephen hemminger5c578ae2010-03-17 20:31:11 +00004168 rcu_read_lock_bh();
Sasha Levinb67bfe02013-02-27 17:06:00 -08004169 hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09004170 if (!net_eq(dev_net(ifp->idev->dev), net))
Daniel Lezcano389f6612008-01-10 22:44:40 -08004171 continue;
YOSHIFUJI Hideakicaad2952008-04-10 15:42:07 +09004172 if (ipv6_addr_equal(&ifp->addr, addr) &&
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07004173 (ifp->flags & IFA_F_HOMEADDRESS)) {
4174 ret = 1;
4175 break;
4176 }
4177 }
stephen hemminger5c578ae2010-03-17 20:31:11 +00004178 rcu_read_unlock_bh();
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07004179 return ret;
4180}
4181#endif
4182
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183/*
4184 * Periodic address status verification
4185 */
4186
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004187static void addrconf_verify_rtnl(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188{
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -07004189 unsigned long now, next, next_sec, next_sched;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190 struct inet6_ifaddr *ifp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191 int i;
4192
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004193 ASSERT_RTNL();
4194
stephen hemminger5c578ae2010-03-17 20:31:11 +00004195 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196 now = jiffies;
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -07004197 next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004199 cancel_delayed_work(&addr_chk_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07004201 for (i = 0; i < IN6_ADDR_HSIZE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004202restart:
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004203 hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[i], addr_lst) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204 unsigned long age;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004205
Yasushi Asanofad8da32013-12-31 12:04:19 +09004206 /* When setting preferred_lft to a value not zero or
4207 * infinity, while valid_lft is infinity
4208 * IFA_F_PERMANENT has a non-infinity life time.
4209 */
4210 if ((ifp->flags & IFA_F_PERMANENT) &&
4211 (ifp->prefered_lft == INFINITY_LIFE_TIME))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212 continue;
4213
4214 spin_lock(&ifp->lock);
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -07004215 /* We try to batch several events at once. */
4216 age = (now - ifp->tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217
YOSHIFUJI Hideaki8f27ebb2006-07-28 18:12:11 +09004218 if (ifp->valid_lft != INFINITY_LIFE_TIME &&
4219 age >= ifp->valid_lft) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220 spin_unlock(&ifp->lock);
4221 in6_ifa_hold(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004222 ipv6_del_addr(ifp);
4223 goto restart;
YOSHIFUJI Hideaki8f27ebb2006-07-28 18:12:11 +09004224 } else if (ifp->prefered_lft == INFINITY_LIFE_TIME) {
4225 spin_unlock(&ifp->lock);
4226 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227 } else if (age >= ifp->prefered_lft) {
Brian Haleya1ed0522009-07-02 07:10:52 +00004228 /* jiffies - ifp->tstamp > age >= ifp->prefered_lft */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229 int deprecate = 0;
4230
4231 if (!(ifp->flags&IFA_F_DEPRECATED)) {
4232 deprecate = 1;
4233 ifp->flags |= IFA_F_DEPRECATED;
4234 }
4235
Yasushi Asanofad8da32013-12-31 12:04:19 +09004236 if ((ifp->valid_lft != INFINITY_LIFE_TIME) &&
4237 (time_before(ifp->tstamp + ifp->valid_lft * HZ, next)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238 next = ifp->tstamp + ifp->valid_lft * HZ;
4239
4240 spin_unlock(&ifp->lock);
4241
4242 if (deprecate) {
4243 in6_ifa_hold(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244
4245 ipv6_ifa_notify(0, ifp);
4246 in6_ifa_put(ifp);
4247 goto restart;
4248 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249 } else if ((ifp->flags&IFA_F_TEMPORARY) &&
4250 !(ifp->flags&IFA_F_TENTATIVE)) {
stephen hemminger88949cf2010-03-17 20:31:17 +00004251 unsigned long regen_advance = ifp->idev->cnf.regen_max_retry *
4252 ifp->idev->cnf.dad_transmits *
Jiri Pirko1f9248e2013-12-07 19:26:53 +01004253 NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME) / HZ;
stephen hemminger88949cf2010-03-17 20:31:17 +00004254
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255 if (age >= ifp->prefered_lft - regen_advance) {
4256 struct inet6_ifaddr *ifpub = ifp->ifpub;
4257 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
4258 next = ifp->tstamp + ifp->prefered_lft * HZ;
4259 if (!ifp->regen_count && ifpub) {
4260 ifp->regen_count++;
4261 in6_ifa_hold(ifp);
4262 in6_ifa_hold(ifpub);
4263 spin_unlock(&ifp->lock);
stephen hemminger5c578ae2010-03-17 20:31:11 +00004264
Hiroyuki YAMAMORI291d8092005-12-23 11:24:05 -08004265 spin_lock(&ifpub->lock);
4266 ifpub->regen_count = 0;
4267 spin_unlock(&ifpub->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268 ipv6_create_tempaddr(ifpub, ifp);
4269 in6_ifa_put(ifpub);
4270 in6_ifa_put(ifp);
4271 goto restart;
4272 }
4273 } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
4274 next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
4275 spin_unlock(&ifp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276 } else {
4277 /* ifp->prefered_lft <= ifp->valid_lft */
4278 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
4279 next = ifp->tstamp + ifp->prefered_lft * HZ;
4280 spin_unlock(&ifp->lock);
4281 }
4282 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283 }
4284
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -07004285 next_sec = round_jiffies_up(next);
4286 next_sched = next;
4287
4288 /* If rounded timeout is accurate enough, accept it. */
4289 if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ))
4290 next_sched = next_sec;
4291
4292 /* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */
4293 if (time_before(next_sched, jiffies + ADDRCONF_TIMER_FUZZ_MAX))
4294 next_sched = jiffies + ADDRCONF_TIMER_FUZZ_MAX;
4295
dingtianhongba3542e2013-08-17 10:27:04 +08004296 ADBG(KERN_DEBUG "now = %lu, schedule = %lu, rounded schedule = %lu => %lu\n",
4297 now, next, next_sec, next_sched);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004298 mod_delayed_work(addrconf_wq, &addr_chk_work, next_sched - now);
stephen hemminger5c578ae2010-03-17 20:31:11 +00004299 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300}
4301
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004302static void addrconf_verify_work(struct work_struct *w)
4303{
4304 rtnl_lock();
4305 addrconf_verify_rtnl();
4306 rtnl_unlock();
4307}
4308
4309static void addrconf_verify(void)
4310{
4311 mod_delayed_work(addrconf_wq, &addr_chk_work, 0);
4312}
4313
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004314static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local,
4315 struct in6_addr **peer_pfx)
Thomas Graf461d8832006-09-18 00:09:49 -07004316{
4317 struct in6_addr *pfx = NULL;
4318
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004319 *peer_pfx = NULL;
4320
Thomas Graf461d8832006-09-18 00:09:49 -07004321 if (addr)
4322 pfx = nla_data(addr);
4323
4324 if (local) {
4325 if (pfx && nla_memcmp(local, pfx, sizeof(*pfx)))
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004326 *peer_pfx = pfx;
4327 pfx = nla_data(local);
Thomas Graf461d8832006-09-18 00:09:49 -07004328 }
4329
4330 return pfx;
4331}
4332
Patrick McHardyef7c79e2007-06-05 12:38:30 -07004333static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = {
Thomas Graf461d8832006-09-18 00:09:49 -07004334 [IFA_ADDRESS] = { .len = sizeof(struct in6_addr) },
4335 [IFA_LOCAL] = { .len = sizeof(struct in6_addr) },
4336 [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) },
Jiri Pirko479840f2013-12-06 09:45:21 +01004337 [IFA_FLAGS] = { .len = sizeof(u32) },
Thomas Graf461d8832006-09-18 00:09:49 -07004338};
4339
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340static int
Thomas Graf661d2962013-03-21 07:45:29 +00004341inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09004343 struct net *net = sock_net(skb->sk);
Thomas Grafb933f712006-09-18 00:10:19 -07004344 struct ifaddrmsg *ifm;
4345 struct nlattr *tb[IFA_MAX+1];
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004346 struct in6_addr *pfx, *peer_pfx;
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02004347 u32 ifa_flags;
Thomas Grafb933f712006-09-18 00:10:19 -07004348 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349
Thomas Grafb933f712006-09-18 00:10:19 -07004350 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
4351 if (err < 0)
4352 return err;
4353
4354 ifm = nlmsg_data(nlh);
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004355 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
Ian Morris63159f22015-03-29 14:00:04 +01004356 if (!pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357 return -EINVAL;
4358
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02004359 ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags;
4360
4361 /* We ignore other flags so far. */
4362 ifa_flags &= IFA_F_MANAGETEMPADDR;
4363
4364 return inet6_addr_del(net, ifm->ifa_index, ifa_flags, pfx,
4365 ifm->ifa_prefixlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004366}
4367
Jiri Pirko479840f2013-12-06 09:45:21 +01004368static int inet6_addr_modify(struct inet6_ifaddr *ifp, u32 ifa_flags,
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07004369 u32 prefered_lft, u32 valid_lft)
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004370{
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07004371 u32 flags;
4372 clock_t expires;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09004373 unsigned long timeout;
Jiri Pirko53bd6742013-12-06 09:45:22 +01004374 bool was_managetempaddr;
Thomas Haller5b84efe2014-01-15 15:36:59 +01004375 bool had_prefixroute;
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09004376
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004377 ASSERT_RTNL();
4378
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004379 if (!valid_lft || (prefered_lft > valid_lft))
4380 return -EINVAL;
4381
Jiri Pirko53bd6742013-12-06 09:45:22 +01004382 if (ifa_flags & IFA_F_MANAGETEMPADDR &&
4383 (ifp->flags & IFA_F_TEMPORARY || ifp->prefix_len != 64))
4384 return -EINVAL;
4385
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09004386 timeout = addrconf_timeout_fixup(valid_lft, HZ);
4387 if (addrconf_finite_timeout(timeout)) {
4388 expires = jiffies_to_clock_t(timeout * HZ);
4389 valid_lft = timeout;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07004390 flags = RTF_EXPIRES;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09004391 } else {
4392 expires = 0;
4393 flags = 0;
4394 ifa_flags |= IFA_F_PERMANENT;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07004395 }
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004396
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09004397 timeout = addrconf_timeout_fixup(prefered_lft, HZ);
4398 if (addrconf_finite_timeout(timeout)) {
4399 if (timeout == 0)
4400 ifa_flags |= IFA_F_DEPRECATED;
4401 prefered_lft = timeout;
4402 }
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004403
4404 spin_lock_bh(&ifp->lock);
Jiri Pirko53bd6742013-12-06 09:45:22 +01004405 was_managetempaddr = ifp->flags & IFA_F_MANAGETEMPADDR;
Thomas Haller5b84efe2014-01-15 15:36:59 +01004406 had_prefixroute = ifp->flags & IFA_F_PERMANENT &&
4407 !(ifp->flags & IFA_F_NOPREFIXROUTE);
Jiri Pirko53bd6742013-12-06 09:45:22 +01004408 ifp->flags &= ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD |
Thomas Haller761aac72014-01-15 15:36:58 +01004409 IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR |
4410 IFA_F_NOPREFIXROUTE);
Jiri Pirko53bd6742013-12-06 09:45:22 +01004411 ifp->flags |= ifa_flags;
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004412 ifp->tstamp = jiffies;
4413 ifp->valid_lft = valid_lft;
4414 ifp->prefered_lft = prefered_lft;
4415
4416 spin_unlock_bh(&ifp->lock);
4417 if (!(ifp->flags&IFA_F_TENTATIVE))
4418 ipv6_ifa_notify(0, ifp);
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004419
Thomas Haller761aac72014-01-15 15:36:58 +01004420 if (!(ifa_flags & IFA_F_NOPREFIXROUTE)) {
4421 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, ifp->idev->dev,
4422 expires, flags);
Thomas Haller5b84efe2014-01-15 15:36:59 +01004423 } else if (had_prefixroute) {
4424 enum cleanup_prefix_rt_t action;
4425 unsigned long rt_expires;
4426
4427 write_lock_bh(&ifp->idev->lock);
4428 action = check_cleanup_prefix_route(ifp, &rt_expires);
4429 write_unlock_bh(&ifp->idev->lock);
4430
4431 if (action != CLEANUP_PREFIX_RT_NOP) {
4432 cleanup_prefix_route(ifp, rt_expires,
4433 action == CLEANUP_PREFIX_RT_DEL);
4434 }
Thomas Haller761aac72014-01-15 15:36:58 +01004435 }
Jiri Pirko53bd6742013-12-06 09:45:22 +01004436
4437 if (was_managetempaddr || ifp->flags & IFA_F_MANAGETEMPADDR) {
4438 if (was_managetempaddr && !(ifp->flags & IFA_F_MANAGETEMPADDR))
4439 valid_lft = prefered_lft = 0;
4440 manage_tempaddrs(ifp->idev, ifp, valid_lft, prefered_lft,
4441 !was_managetempaddr, jiffies);
4442 }
4443
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004444 addrconf_verify_rtnl();
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004445
4446 return 0;
4447}
4448
4449static int
Thomas Graf661d2962013-03-21 07:45:29 +00004450inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09004452 struct net *net = sock_net(skb->sk);
Thomas Graf461d8832006-09-18 00:09:49 -07004453 struct ifaddrmsg *ifm;
4454 struct nlattr *tb[IFA_MAX+1];
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004455 struct in6_addr *pfx, *peer_pfx;
Thomas Graf7198f8c2006-09-18 00:13:46 -07004456 struct inet6_ifaddr *ifa;
4457 struct net_device *dev;
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07004458 u32 valid_lft = INFINITY_LIFE_TIME, preferred_lft = INFINITY_LIFE_TIME;
Jiri Pirko479840f2013-12-06 09:45:21 +01004459 u32 ifa_flags;
Thomas Graf461d8832006-09-18 00:09:49 -07004460 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461
Thomas Graf461d8832006-09-18 00:09:49 -07004462 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
4463 if (err < 0)
4464 return err;
4465
4466 ifm = nlmsg_data(nlh);
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004467 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
Ian Morris63159f22015-03-29 14:00:04 +01004468 if (!pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469 return -EINVAL;
4470
Thomas Graf461d8832006-09-18 00:09:49 -07004471 if (tb[IFA_CACHEINFO]) {
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09004472 struct ifa_cacheinfo *ci;
Thomas Graf461d8832006-09-18 00:09:49 -07004473
4474 ci = nla_data(tb[IFA_CACHEINFO]);
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09004475 valid_lft = ci->ifa_valid;
Thomas Graf461d8832006-09-18 00:09:49 -07004476 preferred_lft = ci->ifa_prefered;
4477 } else {
4478 preferred_lft = INFINITY_LIFE_TIME;
4479 valid_lft = INFINITY_LIFE_TIME;
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09004480 }
4481
Daniel Lezcanoaf284932008-03-05 10:46:57 -08004482 dev = __dev_get_by_index(net, ifm->ifa_index);
Ian Morris63159f22015-03-29 14:00:04 +01004483 if (!dev)
Thomas Graf7198f8c2006-09-18 00:13:46 -07004484 return -ENODEV;
4485
Jiri Pirko479840f2013-12-06 09:45:21 +01004486 ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags;
4487
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07004488 /* We ignore other flags so far. */
Thomas Haller761aac72014-01-15 15:36:58 +01004489 ifa_flags &= IFA_F_NODAD | IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR |
Madhu Challa93a714d2015-02-25 09:58:35 -08004490 IFA_F_NOPREFIXROUTE | IFA_F_MCAUTOJOIN;
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07004491
Daniel Lezcano1cab3da2008-01-10 22:44:09 -08004492 ifa = ipv6_get_ifaddr(net, pfx, dev, 1);
Ian Morris63159f22015-03-29 14:00:04 +01004493 if (!ifa) {
Thomas Graf7198f8c2006-09-18 00:13:46 -07004494 /*
4495 * It would be best to check for !NLM_F_CREATE here but
stephen hemmingerdb9c7c32014-01-12 11:26:32 -08004496 * userspace already relies on not having to provide this.
Thomas Graf7198f8c2006-09-18 00:13:46 -07004497 */
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004498 return inet6_addr_add(net, ifm->ifa_index, pfx, peer_pfx,
Daniel Lezcanoaf284932008-03-05 10:46:57 -08004499 ifm->ifa_prefixlen, ifa_flags,
4500 preferred_lft, valid_lft);
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004501 }
4502
Thomas Graf7198f8c2006-09-18 00:13:46 -07004503 if (nlh->nlmsg_flags & NLM_F_EXCL ||
4504 !(nlh->nlmsg_flags & NLM_F_REPLACE))
4505 err = -EEXIST;
4506 else
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07004507 err = inet6_addr_modify(ifa, ifa_flags, preferred_lft, valid_lft);
Thomas Graf7198f8c2006-09-18 00:13:46 -07004508
4509 in6_ifa_put(ifa);
4510
4511 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512}
4513
Jiri Pirko479840f2013-12-06 09:45:21 +01004514static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u32 flags,
Thomas Graf101bb222006-09-18 00:11:52 -07004515 u8 scope, int ifindex)
4516{
4517 struct ifaddrmsg *ifm;
4518
4519 ifm = nlmsg_data(nlh);
4520 ifm->ifa_family = AF_INET6;
4521 ifm->ifa_prefixlen = prefixlen;
4522 ifm->ifa_flags = flags;
4523 ifm->ifa_scope = scope;
4524 ifm->ifa_index = ifindex;
4525}
4526
Thomas Graf85486af2006-09-18 00:11:24 -07004527static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
4528 unsigned long tstamp, u32 preferred, u32 valid)
4529{
4530 struct ifa_cacheinfo ci;
4531
Thomas Graf18a31e12010-11-17 04:12:02 +00004532 ci.cstamp = cstamp_delta(cstamp);
4533 ci.tstamp = cstamp_delta(tstamp);
Thomas Graf85486af2006-09-18 00:11:24 -07004534 ci.ifa_prefered = preferred;
4535 ci.ifa_valid = valid;
4536
4537 return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
4538}
4539
Thomas Graf101bb222006-09-18 00:11:52 -07004540static inline int rt_scope(int ifa_scope)
4541{
4542 if (ifa_scope & IFA_HOST)
4543 return RT_SCOPE_HOST;
4544 else if (ifa_scope & IFA_LINK)
4545 return RT_SCOPE_LINK;
4546 else if (ifa_scope & IFA_SITE)
4547 return RT_SCOPE_SITE;
4548 else
4549 return RT_SCOPE_UNIVERSE;
4550}
4551
Thomas Graf0ab68032006-09-18 00:12:35 -07004552static inline int inet6_ifaddr_msgsize(void)
4553{
Thomas Graf339bf982006-11-10 14:10:15 -08004554 return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004555 + nla_total_size(16) /* IFA_LOCAL */
Thomas Graf339bf982006-11-10 14:10:15 -08004556 + nla_total_size(16) /* IFA_ADDRESS */
Jiri Pirko479840f2013-12-06 09:45:21 +01004557 + nla_total_size(sizeof(struct ifa_cacheinfo))
4558 + nla_total_size(4) /* IFA_FLAGS */;
Thomas Graf0ab68032006-09-18 00:12:35 -07004559}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07004560
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004562 u32 portid, u32 seq, int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564 struct nlmsghdr *nlh;
Thomas Graf85486af2006-09-18 00:11:24 -07004565 u32 preferred, valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566
Eric W. Biederman15e47302012-09-07 20:12:54 +00004567 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
Ian Morris63159f22015-03-29 14:00:04 +01004568 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08004569 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07004570
Thomas Graf101bb222006-09-18 00:11:52 -07004571 put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope),
4572 ifa->idev->dev->ifindex);
4573
Yasushi Asanofad8da32013-12-31 12:04:19 +09004574 if (!((ifa->flags&IFA_F_PERMANENT) &&
4575 (ifa->prefered_lft == INFINITY_LIFE_TIME))) {
Thomas Graf85486af2006-09-18 00:11:24 -07004576 preferred = ifa->prefered_lft;
4577 valid = ifa->valid_lft;
4578 if (preferred != INFINITY_LIFE_TIME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579 long tval = (jiffies - ifa->tstamp)/HZ;
Jens Rosenbooma1faa692009-06-25 04:55:50 +00004580 if (preferred > tval)
4581 preferred -= tval;
4582 else
4583 preferred = 0;
Ben Hutchingsf56619f2010-06-26 11:37:47 +00004584 if (valid != INFINITY_LIFE_TIME) {
4585 if (valid > tval)
4586 valid -= tval;
4587 else
4588 valid = 0;
4589 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590 }
4591 } else {
Thomas Graf85486af2006-09-18 00:11:24 -07004592 preferred = INFINITY_LIFE_TIME;
4593 valid = INFINITY_LIFE_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004594 }
Thomas Graf85486af2006-09-18 00:11:24 -07004595
Cong Wang7996c792013-05-22 05:41:06 +00004596 if (!ipv6_addr_any(&ifa->peer_addr)) {
Jiri Benc930345e2015-03-29 16:59:25 +02004597 if (nla_put_in6_addr(skb, IFA_LOCAL, &ifa->addr) < 0 ||
4598 nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->peer_addr) < 0)
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004599 goto error;
4600 } else
Jiri Benc930345e2015-03-29 16:59:25 +02004601 if (nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->addr) < 0)
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004602 goto error;
4603
4604 if (put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0)
4605 goto error;
Thomas Graf85486af2006-09-18 00:11:24 -07004606
Jiri Pirko479840f2013-12-06 09:45:21 +01004607 if (nla_put_u32(skb, IFA_FLAGS, ifa->flags) < 0)
4608 goto error;
4609
Johannes Berg053c0952015-01-16 22:09:00 +01004610 nlmsg_end(skb, nlh);
4611 return 0;
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004612
4613error:
4614 nlmsg_cancel(skb, nlh);
4615 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004616}
4617
4618static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004619 u32 portid, u32 seq, int event, u16 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004620{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621 struct nlmsghdr *nlh;
Thomas Graf101bb222006-09-18 00:11:52 -07004622 u8 scope = RT_SCOPE_UNIVERSE;
4623 int ifindex = ifmca->idev->dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624
Thomas Graf101bb222006-09-18 00:11:52 -07004625 if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE)
4626 scope = RT_SCOPE_SITE;
4627
Eric W. Biederman15e47302012-09-07 20:12:54 +00004628 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
Ian Morris63159f22015-03-29 14:00:04 +01004629 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08004630 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07004631
Thomas Graf101bb222006-09-18 00:11:52 -07004632 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
Jiri Benc930345e2015-03-29 16:59:25 +02004633 if (nla_put_in6_addr(skb, IFA_MULTICAST, &ifmca->mca_addr) < 0 ||
Thomas Graf0ab68032006-09-18 00:12:35 -07004634 put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp,
Patrick McHardy26932562007-01-31 23:16:40 -08004635 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
4636 nlmsg_cancel(skb, nlh);
4637 return -EMSGSIZE;
4638 }
Thomas Graf85486af2006-09-18 00:11:24 -07004639
Johannes Berg053c0952015-01-16 22:09:00 +01004640 nlmsg_end(skb, nlh);
4641 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642}
4643
4644static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004645 u32 portid, u32 seq, int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647 struct nlmsghdr *nlh;
Thomas Graf101bb222006-09-18 00:11:52 -07004648 u8 scope = RT_SCOPE_UNIVERSE;
4649 int ifindex = ifaca->aca_idev->dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650
Thomas Graf101bb222006-09-18 00:11:52 -07004651 if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE)
4652 scope = RT_SCOPE_SITE;
4653
Eric W. Biederman15e47302012-09-07 20:12:54 +00004654 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
Ian Morris63159f22015-03-29 14:00:04 +01004655 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08004656 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07004657
Thomas Graf101bb222006-09-18 00:11:52 -07004658 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
Jiri Benc930345e2015-03-29 16:59:25 +02004659 if (nla_put_in6_addr(skb, IFA_ANYCAST, &ifaca->aca_addr) < 0 ||
Thomas Graf0ab68032006-09-18 00:12:35 -07004660 put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp,
Patrick McHardy26932562007-01-31 23:16:40 -08004661 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
4662 nlmsg_cancel(skb, nlh);
4663 return -EMSGSIZE;
4664 }
Thomas Graf85486af2006-09-18 00:11:24 -07004665
Johannes Berg053c0952015-01-16 22:09:00 +01004666 nlmsg_end(skb, nlh);
4667 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668}
4669
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004670enum addr_type_t {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004671 UNICAST_ADDR,
4672 MULTICAST_ADDR,
4673 ANYCAST_ADDR,
4674};
4675
Eric Dumazet234b27c2009-11-12 04:11:50 +00004676/* called with rcu_read_lock() */
4677static int in6_dump_addrs(struct inet6_dev *idev, struct sk_buff *skb,
4678 struct netlink_callback *cb, enum addr_type_t type,
4679 int s_ip_idx, int *p_ip_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681 struct ifmcaddr6 *ifmca;
4682 struct ifacaddr6 *ifaca;
Eric Dumazet234b27c2009-11-12 04:11:50 +00004683 int err = 1;
4684 int ip_idx = *p_ip_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685
Eric Dumazet234b27c2009-11-12 04:11:50 +00004686 read_lock_bh(&idev->lock);
4687 switch (type) {
stephen hemminger502a2ff2010-03-17 20:31:13 +00004688 case UNICAST_ADDR: {
4689 struct inet6_ifaddr *ifa;
4690
Eric Dumazet234b27c2009-11-12 04:11:50 +00004691 /* unicast address incl. temp addr */
stephen hemminger502a2ff2010-03-17 20:31:13 +00004692 list_for_each_entry(ifa, &idev->addr_list, if_list) {
4693 if (++ip_idx < s_ip_idx)
Eric Dumazet234b27c2009-11-12 04:11:50 +00004694 continue;
4695 err = inet6_fill_ifaddr(skb, ifa,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004696 NETLINK_CB(cb->skb).portid,
Eric Dumazet234b27c2009-11-12 04:11:50 +00004697 cb->nlh->nlmsg_seq,
4698 RTM_NEWADDR,
4699 NLM_F_MULTI);
Johannes Berg053c0952015-01-16 22:09:00 +01004700 if (err < 0)
Eric Dumazet234b27c2009-11-12 04:11:50 +00004701 break;
Nicolas Dichtel63998ac2013-03-22 06:28:43 +00004702 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703 }
Eric Dumazet234b27c2009-11-12 04:11:50 +00004704 break;
stephen hemminger502a2ff2010-03-17 20:31:13 +00004705 }
Eric Dumazet234b27c2009-11-12 04:11:50 +00004706 case MULTICAST_ADDR:
4707 /* multicast address */
4708 for (ifmca = idev->mc_list; ifmca;
4709 ifmca = ifmca->next, ip_idx++) {
4710 if (ip_idx < s_ip_idx)
4711 continue;
4712 err = inet6_fill_ifmcaddr(skb, ifmca,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004713 NETLINK_CB(cb->skb).portid,
Eric Dumazet234b27c2009-11-12 04:11:50 +00004714 cb->nlh->nlmsg_seq,
4715 RTM_GETMULTICAST,
4716 NLM_F_MULTI);
Johannes Berg053c0952015-01-16 22:09:00 +01004717 if (err < 0)
Eric Dumazet234b27c2009-11-12 04:11:50 +00004718 break;
4719 }
4720 break;
4721 case ANYCAST_ADDR:
4722 /* anycast address */
4723 for (ifaca = idev->ac_list; ifaca;
4724 ifaca = ifaca->aca_next, ip_idx++) {
4725 if (ip_idx < s_ip_idx)
4726 continue;
4727 err = inet6_fill_ifacaddr(skb, ifaca,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004728 NETLINK_CB(cb->skb).portid,
Eric Dumazet234b27c2009-11-12 04:11:50 +00004729 cb->nlh->nlmsg_seq,
4730 RTM_GETANYCAST,
4731 NLM_F_MULTI);
Johannes Berg053c0952015-01-16 22:09:00 +01004732 if (err < 0)
Eric Dumazet234b27c2009-11-12 04:11:50 +00004733 break;
4734 }
4735 break;
4736 default:
4737 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004738 }
Eric Dumazet234b27c2009-11-12 04:11:50 +00004739 read_unlock_bh(&idev->lock);
4740 *p_ip_idx = ip_idx;
4741 return err;
4742}
4743
4744static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
4745 enum addr_type_t type)
4746{
4747 struct net *net = sock_net(skb->sk);
4748 int h, s_h;
4749 int idx, ip_idx;
4750 int s_idx, s_ip_idx;
4751 struct net_device *dev;
4752 struct inet6_dev *idev;
4753 struct hlist_head *head;
Eric Dumazet234b27c2009-11-12 04:11:50 +00004754
4755 s_h = cb->args[0];
4756 s_idx = idx = cb->args[1];
4757 s_ip_idx = ip_idx = cb->args[2];
4758
4759 rcu_read_lock();
Nicolas Dichtel63998ac2013-03-22 06:28:43 +00004760 cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^ net->dev_base_seq;
Eric Dumazet234b27c2009-11-12 04:11:50 +00004761 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4762 idx = 0;
4763 head = &net->dev_index_head[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -08004764 hlist_for_each_entry_rcu(dev, head, index_hlist) {
Eric Dumazet234b27c2009-11-12 04:11:50 +00004765 if (idx < s_idx)
4766 goto cont;
Patrick McHardy4b97efd2010-03-26 20:27:49 -07004767 if (h > s_h || idx > s_idx)
Eric Dumazet234b27c2009-11-12 04:11:50 +00004768 s_ip_idx = 0;
4769 ip_idx = 0;
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004770 idev = __in6_dev_get(dev);
4771 if (!idev)
Eric Dumazet234b27c2009-11-12 04:11:50 +00004772 goto cont;
4773
4774 if (in6_dump_addrs(idev, skb, cb, type,
David S. Miller7b46a642015-01-18 23:36:08 -05004775 s_ip_idx, &ip_idx) < 0)
Eric Dumazet234b27c2009-11-12 04:11:50 +00004776 goto done;
4777cont:
4778 idx++;
4779 }
4780 }
4781done:
4782 rcu_read_unlock();
4783 cb->args[0] = h;
4784 cb->args[1] = idx;
4785 cb->args[2] = ip_idx;
4786
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787 return skb->len;
4788}
4789
4790static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
4791{
4792 enum addr_type_t type = UNICAST_ADDR;
Denis V. Lunevb8542722007-12-01 00:21:31 +11004793
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794 return inet6_dump_addr(skb, cb, type);
4795}
4796
4797static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
4798{
4799 enum addr_type_t type = MULTICAST_ADDR;
Denis V. Lunevb8542722007-12-01 00:21:31 +11004800
Linus Torvalds1da177e2005-04-16 15:20:36 -07004801 return inet6_dump_addr(skb, cb, type);
4802}
4803
4804
4805static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
4806{
4807 enum addr_type_t type = ANYCAST_ADDR;
Denis V. Lunevb8542722007-12-01 00:21:31 +11004808
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809 return inet6_dump_addr(skb, cb, type);
4810}
4811
Thomas Graf661d2962013-03-21 07:45:29 +00004812static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr *nlh)
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004813{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09004814 struct net *net = sock_net(in_skb->sk);
Thomas Graf1b29fc22006-09-18 00:10:50 -07004815 struct ifaddrmsg *ifm;
4816 struct nlattr *tb[IFA_MAX+1];
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004817 struct in6_addr *addr = NULL, *peer;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004818 struct net_device *dev = NULL;
4819 struct inet6_ifaddr *ifa;
4820 struct sk_buff *skb;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004821 int err;
4822
Thomas Graf1b29fc22006-09-18 00:10:50 -07004823 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
4824 if (err < 0)
4825 goto errout;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004826
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004827 addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer);
Ian Morris63159f22015-03-29 14:00:04 +01004828 if (!addr) {
Thomas Graf1b29fc22006-09-18 00:10:50 -07004829 err = -EINVAL;
4830 goto errout;
4831 }
4832
4833 ifm = nlmsg_data(nlh);
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004834 if (ifm->ifa_index)
Benjamin Thery6fda7352008-03-05 10:47:47 -08004835 dev = __dev_get_by_index(net, ifm->ifa_index);
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004836
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004837 ifa = ipv6_get_ifaddr(net, addr, dev, 1);
4838 if (!ifa) {
Thomas Graf1b29fc22006-09-18 00:10:50 -07004839 err = -EADDRNOTAVAIL;
4840 goto errout;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004841 }
4842
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004843 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL);
4844 if (!skb) {
Thomas Graf1b29fc22006-09-18 00:10:50 -07004845 err = -ENOBUFS;
4846 goto errout_ifa;
4847 }
4848
Eric W. Biederman15e47302012-09-07 20:12:54 +00004849 err = inet6_fill_ifaddr(skb, ifa, NETLINK_CB(in_skb).portid,
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004850 nlh->nlmsg_seq, RTM_NEWADDR, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08004851 if (err < 0) {
4852 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
4853 WARN_ON(err == -EMSGSIZE);
4854 kfree_skb(skb);
4855 goto errout_ifa;
4856 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00004857 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
Thomas Graf1b29fc22006-09-18 00:10:50 -07004858errout_ifa:
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004859 in6_ifa_put(ifa);
Thomas Graf1b29fc22006-09-18 00:10:50 -07004860errout:
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004861 return err;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004862}
4863
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
4865{
4866 struct sk_buff *skb;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09004867 struct net *net = dev_net(ifa->idev->dev);
Thomas Graf5d620262006-08-15 00:35:02 -07004868 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004869
Thomas Graf0ab68032006-09-18 00:12:35 -07004870 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC);
Ian Morris63159f22015-03-29 14:00:04 +01004871 if (!skb)
Thomas Graf5d620262006-08-15 00:35:02 -07004872 goto errout;
4873
4874 err = inet6_fill_ifaddr(skb, ifa, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08004875 if (err < 0) {
4876 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
4877 WARN_ON(err == -EMSGSIZE);
4878 kfree_skb(skb);
4879 goto errout;
4880 }
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08004881 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
4882 return;
Thomas Graf5d620262006-08-15 00:35:02 -07004883errout:
4884 if (err < 0)
Benjamin Thery6fda7352008-03-05 10:47:47 -08004885 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004886}
4887
Dave Jonesb6f99a22007-03-22 12:27:49 -07004888static inline void ipv6_store_devconf(struct ipv6_devconf *cnf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004889 __s32 *array, int bytes)
4890{
Thomas Graf04561c12006-11-14 19:53:58 -08004891 BUG_ON(bytes < (DEVCONF_MAX * 4));
4892
Linus Torvalds1da177e2005-04-16 15:20:36 -07004893 memset(array, 0, bytes);
4894 array[DEVCONF_FORWARDING] = cnf->forwarding;
4895 array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
4896 array[DEVCONF_MTU6] = cnf->mtu6;
4897 array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
4898 array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
4899 array[DEVCONF_AUTOCONF] = cnf->autoconf;
4900 array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
4901 array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
Thomas Graf93908d12010-11-17 01:44:24 +00004902 array[DEVCONF_RTR_SOLICIT_INTERVAL] =
4903 jiffies_to_msecs(cnf->rtr_solicit_interval);
4904 array[DEVCONF_RTR_SOLICIT_DELAY] =
4905 jiffies_to_msecs(cnf->rtr_solicit_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004906 array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
Hannes Frederic Sowafc4eba52013-08-14 01:03:46 +02004907 array[DEVCONF_MLDV1_UNSOLICITED_REPORT_INTERVAL] =
4908 jiffies_to_msecs(cnf->mldv1_unsolicited_report_interval);
4909 array[DEVCONF_MLDV2_UNSOLICITED_REPORT_INTERVAL] =
4910 jiffies_to_msecs(cnf->mldv2_unsolicited_report_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004911 array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
4912 array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
4913 array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
4914 array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
4915 array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916 array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -08004917 array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr;
Hangbin Liu8013d1d2015-07-30 14:28:42 +08004918 array[DEVCONF_ACCEPT_RA_MIN_HOP_LIMIT] = cnf->accept_ra_min_hop_limit;
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -08004919 array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo;
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08004920#ifdef CONFIG_IPV6_ROUTER_PREF
4921 array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref;
Thomas Graf93908d12010-11-17 01:44:24 +00004922 array[DEVCONF_RTR_PROBE_INTERVAL] =
4923 jiffies_to_msecs(cnf->rtr_probe_interval);
Neil Hormanfa03ef32007-01-30 14:30:10 -08004924#ifdef CONFIG_IPV6_ROUTE_INFO
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -08004925 array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen;
4926#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08004927#endif
Lorenzo Colitti2b253062014-03-26 19:35:41 +09004928 array[DEVCONF_ACCEPT_RA_RT_TABLE] = cnf->accept_ra_rt_table;
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -07004929 array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp;
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -07004930 array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route;
Neil Horman95c385b2007-04-25 17:08:10 -07004931#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
4932 array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad;
Erik Kline7fd25612014-10-28 18:11:14 +09004933 array[DEVCONF_USE_OPTIMISTIC] = cnf->use_optimistic;
Neil Horman95c385b2007-04-25 17:08:10 -07004934#endif
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09004935#ifdef CONFIG_IPV6_MROUTE
4936 array[DEVCONF_MC_FORWARDING] = cnf->mc_forwarding;
4937#endif
YOSHIFUJI Hideaki778d80b2008-06-28 14:17:11 +09004938 array[DEVCONF_DISABLE_IPV6] = cnf->disable_ipv6;
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09004939 array[DEVCONF_ACCEPT_DAD] = cnf->accept_dad;
Cosmin Ratiuc3faca02009-10-09 03:11:14 +00004940 array[DEVCONF_FORCE_TLLAO] = cnf->force_tllao;
Hannes Frederic Sowa5cb04432012-11-06 16:46:20 +00004941 array[DEVCONF_NDISC_NOTIFY] = cnf->ndisc_notify;
Hannes Frederic Sowab800c3b2013-08-27 01:36:51 +02004942 array[DEVCONF_SUPPRESS_FRAG_NDISC] = cnf->suppress_frag_ndisc;
Ben Greeard9333192014-06-25 14:44:53 -07004943 array[DEVCONF_ACCEPT_RA_FROM_LOCAL] = cnf->accept_ra_from_local;
Harout Hedeshianc2943f12015-01-20 10:06:05 -07004944 array[DEVCONF_ACCEPT_RA_MTU] = cnf->accept_ra_mtu;
Andy Gospodarek35103d12015-08-13 10:39:01 -04004945 array[DEVCONF_IGNORE_ROUTES_WITH_LINKDOWN] = cnf->ignore_routes_with_linkdown;
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01004946 /* we omit DEVCONF_STABLE_SECRET for now */
Erik Kline3985e8a2015-07-22 16:38:25 +09004947 array[DEVCONF_USE_OIF_ADDRS_ONLY] = cnf->use_oif_addrs_only;
Johannes Bergabbc3042016-02-04 13:31:19 +01004948 array[DEVCONF_DROP_UNICAST_IN_L2_MULTICAST] = cnf->drop_unicast_in_l2_multicast;
Johannes Berg7a02bf82016-02-04 13:31:20 +01004949 array[DEVCONF_DROP_UNSOLICITED_NA] = cnf->drop_unsolicited_na;
David Ahernf1705ec2016-02-24 09:25:37 -08004950 array[DEVCONF_KEEP_ADDR_ON_DOWN] = cnf->keep_addr_on_down;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951}
4952
Thomas Grafb382b192010-11-16 04:33:57 +00004953static inline size_t inet6_ifla6_size(void)
4954{
4955 return nla_total_size(4) /* IFLA_INET6_FLAGS */
4956 + nla_total_size(sizeof(struct ifla_cacheinfo))
4957 + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */
4958 + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */
Daniel Borkmannf53adae2013-04-08 04:01:30 +00004959 + nla_total_size(ICMP6_MIB_MAX * 8) /* IFLA_INET6_ICMP6STATS */
4960 + nla_total_size(sizeof(struct in6_addr)); /* IFLA_INET6_TOKEN */
Thomas Grafb382b192010-11-16 04:33:57 +00004961}
4962
Thomas Graf339bf982006-11-10 14:10:15 -08004963static inline size_t inet6_if_nlmsg_size(void)
4964{
4965 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
4966 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
4967 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
4968 + nla_total_size(4) /* IFLA_MTU */
4969 + nla_total_size(4) /* IFLA_LINK */
Andy Gospodarek03443382015-08-13 15:26:35 -04004970 + nla_total_size(1) /* IFLA_OPERSTATE */
Thomas Grafb382b192010-11-16 04:33:57 +00004971 + nla_total_size(inet6_ifla6_size()); /* IFLA_PROTINFO */
Thomas Graf339bf982006-11-10 14:10:15 -08004972}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07004973
Eric Dumazetbe281e52011-05-19 01:14:23 +00004974static inline void __snmp6_fill_statsdev(u64 *stats, atomic_long_t *mib,
Tejun Heo7d720c32010-02-16 15:20:26 +00004975 int items, int bytes)
Herbert Xu7f7d9a62007-04-24 21:54:09 -07004976{
4977 int i;
4978 int pad = bytes - sizeof(u64) * items;
4979 BUG_ON(pad < 0);
4980
4981 /* Use put_unaligned() because stats may not be aligned for u64. */
4982 put_unaligned(items, &stats[0]);
4983 for (i = 1; i < items; i++)
Eric Dumazetbe281e52011-05-19 01:14:23 +00004984 put_unaligned(atomic_long_read(&mib[i]), &stats[i]);
Herbert Xu7f7d9a62007-04-24 21:54:09 -07004985
4986 memset(&stats[items], 0, pad);
4987}
4988
WANG Cong698365f2014-05-05 15:55:55 -07004989static inline void __snmp6_fill_stats64(u64 *stats, void __percpu *mib,
Raghavendra K Ta3a77372015-08-30 11:29:42 +05304990 int bytes, size_t syncpoff)
Eric Dumazet4ce3c182010-06-30 13:31:19 -07004991{
Raghavendra K Ta3a77372015-08-30 11:29:42 +05304992 int i, c;
4993 u64 buff[IPSTATS_MIB_MAX];
4994 int pad = bytes - sizeof(u64) * IPSTATS_MIB_MAX;
4995
Eric Dumazet4ce3c182010-06-30 13:31:19 -07004996 BUG_ON(pad < 0);
4997
Raghavendra K Ta3a77372015-08-30 11:29:42 +05304998 memset(buff, 0, sizeof(buff));
4999 buff[0] = IPSTATS_MIB_MAX;
Eric Dumazet4ce3c182010-06-30 13:31:19 -07005000
Raghavendra K Ta3a77372015-08-30 11:29:42 +05305001 for_each_possible_cpu(c) {
5002 for (i = 1; i < IPSTATS_MIB_MAX; i++)
5003 buff[i] += snmp_get_cpu_field64(mib, c, i, syncpoff);
5004 }
5005
5006 memcpy(stats, buff, IPSTATS_MIB_MAX * sizeof(u64));
5007 memset(&stats[IPSTATS_MIB_MAX], 0, pad);
Eric Dumazet4ce3c182010-06-30 13:31:19 -07005008}
5009
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005010static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype,
5011 int bytes)
5012{
Stephen Hemmingere21e8462010-03-20 16:09:01 -07005013 switch (attrtype) {
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005014 case IFLA_INET6_STATS:
Raghavendra K Ta3a77372015-08-30 11:29:42 +05305015 __snmp6_fill_stats64(stats, idev->stats.ipv6, bytes,
5016 offsetof(struct ipstats_mib, syncp));
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005017 break;
5018 case IFLA_INET6_ICMP6STATS:
Eric Dumazetbe281e52011-05-19 01:14:23 +00005019 __snmp6_fill_statsdev(stats, idev->stats.icmpv6dev->mibs, ICMP6_MIB_MAX, bytes);
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005020 break;
5021 }
5022}
5023
Sowmini Varadhand5566fd2015-09-11 16:48:48 -04005024static int inet6_fill_ifla6_attrs(struct sk_buff *skb, struct inet6_dev *idev,
5025 u32 ext_filter_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005026{
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005027 struct nlattr *nla;
Thomas Graf04561c12006-11-14 19:53:58 -08005028 struct ifla_cacheinfo ci;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005029
David S. Millerc78679e2012-04-01 20:27:33 -04005030 if (nla_put_u32(skb, IFLA_INET6_FLAGS, idev->if_flags))
5031 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005032 ci.max_reasm_len = IPV6_MAXPLEN;
David S. Miller24912422010-11-19 13:13:47 -08005033 ci.tstamp = cstamp_delta(idev->tstamp);
5034 ci.reachable_time = jiffies_to_msecs(idev->nd_parms->reachable_time);
Jiri Pirko1f9248e2013-12-07 19:26:53 +01005035 ci.retrans_time = jiffies_to_msecs(NEIGH_VAR(idev->nd_parms, RETRANS_TIME));
David S. Millerc78679e2012-04-01 20:27:33 -04005036 if (nla_put(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci))
5037 goto nla_put_failure;
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005038 nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32));
Ian Morris63159f22015-03-29 14:00:04 +01005039 if (!nla)
Thomas Graf04561c12006-11-14 19:53:58 -08005040 goto nla_put_failure;
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005041 ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005042
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005043 /* XXX - MC not implemented */
5044
Sowmini Varadhand5566fd2015-09-11 16:48:48 -04005045 if (ext_filter_mask & RTEXT_FILTER_SKIP_STATS)
5046 return 0;
5047
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005048 nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64));
Ian Morris63159f22015-03-29 14:00:04 +01005049 if (!nla)
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005050 goto nla_put_failure;
5051 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla));
5052
5053 nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64));
Ian Morris63159f22015-03-29 14:00:04 +01005054 if (!nla)
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005055 goto nla_put_failure;
5056 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005057
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005058 nla = nla_reserve(skb, IFLA_INET6_TOKEN, sizeof(struct in6_addr));
Ian Morris63159f22015-03-29 14:00:04 +01005059 if (!nla)
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005060 goto nla_put_failure;
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02005061
5062 if (nla_put_u8(skb, IFLA_INET6_ADDR_GEN_MODE, idev->addr_gen_mode))
5063 goto nla_put_failure;
5064
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005065 read_lock_bh(&idev->lock);
5066 memcpy(nla_data(nla), idev->token.s6_addr, nla_len(nla));
5067 read_unlock_bh(&idev->lock);
5068
Thomas Grafb382b192010-11-16 04:33:57 +00005069 return 0;
5070
5071nla_put_failure:
5072 return -EMSGSIZE;
5073}
5074
Arad, Ronenb1974ed2015-10-19 09:23:28 -07005075static size_t inet6_get_link_af_size(const struct net_device *dev,
5076 u32 ext_filter_mask)
Thomas Grafb382b192010-11-16 04:33:57 +00005077{
5078 if (!__in6_dev_get(dev))
5079 return 0;
5080
5081 return inet6_ifla6_size();
5082}
5083
Sowmini Varadhand5566fd2015-09-11 16:48:48 -04005084static int inet6_fill_link_af(struct sk_buff *skb, const struct net_device *dev,
5085 u32 ext_filter_mask)
Thomas Grafb382b192010-11-16 04:33:57 +00005086{
5087 struct inet6_dev *idev = __in6_dev_get(dev);
5088
5089 if (!idev)
5090 return -ENODATA;
5091
Sowmini Varadhand5566fd2015-09-11 16:48:48 -04005092 if (inet6_fill_ifla6_attrs(skb, idev, ext_filter_mask) < 0)
Thomas Grafb382b192010-11-16 04:33:57 +00005093 return -EMSGSIZE;
5094
5095 return 0;
5096}
5097
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005098static int inet6_set_iftoken(struct inet6_dev *idev, struct in6_addr *token)
5099{
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005100 struct inet6_ifaddr *ifp;
5101 struct net_device *dev = idev->dev;
Daniel Borkmann47e27d52016-04-08 15:55:00 +02005102 bool clear_token, update_rs = false;
Hannes Frederic Sowadc848292013-06-24 21:42:40 +02005103 struct in6_addr ll_addr;
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005104
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01005105 ASSERT_RTNL();
5106
Ian Morris63159f22015-03-29 14:00:04 +01005107 if (!token)
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005108 return -EINVAL;
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005109 if (dev->flags & (IFF_LOOPBACK | IFF_NOARP))
5110 return -EINVAL;
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005111 if (!ipv6_accept_ra(idev))
5112 return -EINVAL;
5113 if (idev->cnf.rtr_solicits <= 0)
5114 return -EINVAL;
5115
5116 write_lock_bh(&idev->lock);
5117
5118 BUILD_BUG_ON(sizeof(token->s6_addr) != 16);
5119 memcpy(idev->token.s6_addr + 8, token->s6_addr + 8, 8);
5120
5121 write_unlock_bh(&idev->lock);
5122
Daniel Borkmann47e27d52016-04-08 15:55:00 +02005123 clear_token = ipv6_addr_any(token);
5124 if (clear_token)
5125 goto update_lft;
5126
Hannes Frederic Sowadc848292013-06-24 21:42:40 +02005127 if (!idev->dead && (idev->if_flags & IF_READY) &&
5128 !ipv6_get_lladdr(dev, &ll_addr, IFA_F_TENTATIVE |
5129 IFA_F_OPTIMISTIC)) {
Daniel Borkmannfc403832013-04-09 03:47:15 +00005130 /* If we're not ready, then normal ifup will take care
5131 * of this. Otherwise, we need to request our rs here.
5132 */
5133 ndisc_send_rs(dev, &ll_addr, &in6addr_linklocal_allrouters);
5134 update_rs = true;
5135 }
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005136
Daniel Borkmann47e27d52016-04-08 15:55:00 +02005137update_lft:
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005138 write_lock_bh(&idev->lock);
Daniel Borkmannfc403832013-04-09 03:47:15 +00005139
Hannes Frederic Sowa77ecaac2013-06-26 03:41:49 +02005140 if (update_rs) {
Daniel Borkmannfc403832013-04-09 03:47:15 +00005141 idev->if_flags |= IF_RS_SENT;
Hannes Frederic Sowa77ecaac2013-06-26 03:41:49 +02005142 idev->rs_probes = 1;
5143 addrconf_mod_rs_timer(idev, idev->cnf.rtr_solicit_interval);
5144 }
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005145
5146 /* Well, that's kinda nasty ... */
5147 list_for_each_entry(ifp, &idev->addr_list, if_list) {
5148 spin_lock(&ifp->lock);
Daniel Borkmann617fe292013-04-09 03:47:16 +00005149 if (ifp->tokenized) {
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005150 ifp->valid_lft = 0;
5151 ifp->prefered_lft = 0;
5152 }
5153 spin_unlock(&ifp->lock);
5154 }
5155
5156 write_unlock_bh(&idev->lock);
Lubomir Rintelb2ed64a2014-10-27 17:39:16 +01005157 inet6_ifinfo_notify(RTM_NEWLINK, idev);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01005158 addrconf_verify_rtnl();
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005159 return 0;
5160}
5161
Daniel Borkmann11b1f822015-02-05 14:39:11 +01005162static const struct nla_policy inet6_af_policy[IFLA_INET6_MAX + 1] = {
5163 [IFLA_INET6_ADDR_GEN_MODE] = { .type = NLA_U8 },
5164 [IFLA_INET6_TOKEN] = { .len = sizeof(struct in6_addr) },
5165};
5166
5167static int inet6_validate_link_af(const struct net_device *dev,
5168 const struct nlattr *nla)
5169{
5170 struct nlattr *tb[IFLA_INET6_MAX + 1];
5171
5172 if (dev && !__in6_dev_get(dev))
5173 return -EAFNOSUPPORT;
5174
5175 return nla_parse_nested(tb, IFLA_INET6_MAX, nla, inet6_af_policy);
5176}
5177
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005178static int inet6_set_link_af(struct net_device *dev, const struct nlattr *nla)
5179{
5180 int err = -EINVAL;
5181 struct inet6_dev *idev = __in6_dev_get(dev);
5182 struct nlattr *tb[IFLA_INET6_MAX + 1];
5183
5184 if (!idev)
5185 return -EAFNOSUPPORT;
5186
5187 if (nla_parse_nested(tb, IFLA_INET6_MAX, nla, NULL) < 0)
5188 BUG();
5189
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02005190 if (tb[IFLA_INET6_TOKEN]) {
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005191 err = inet6_set_iftoken(idev, nla_data(tb[IFLA_INET6_TOKEN]));
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02005192 if (err)
5193 return err;
5194 }
5195
5196 if (tb[IFLA_INET6_ADDR_GEN_MODE]) {
5197 u8 mode = nla_get_u8(tb[IFLA_INET6_ADDR_GEN_MODE]);
5198
5199 if (mode != IN6_ADDR_GEN_MODE_EUI64 &&
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01005200 mode != IN6_ADDR_GEN_MODE_NONE &&
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01005201 mode != IN6_ADDR_GEN_MODE_STABLE_PRIVACY &&
5202 mode != IN6_ADDR_GEN_MODE_RANDOM)
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02005203 return -EINVAL;
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01005204
5205 if (mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY &&
5206 !idev->cnf.stable_secret.initialized &&
5207 !dev_net(dev)->ipv6.devconf_dflt->stable_secret.initialized)
5208 return -EINVAL;
5209
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02005210 idev->addr_gen_mode = mode;
5211 err = 0;
5212 }
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005213
5214 return err;
5215}
5216
Thomas Grafb382b192010-11-16 04:33:57 +00005217static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005218 u32 portid, u32 seq, int event, unsigned int flags)
Thomas Grafb382b192010-11-16 04:33:57 +00005219{
5220 struct net_device *dev = idev->dev;
5221 struct ifinfomsg *hdr;
5222 struct nlmsghdr *nlh;
5223 void *protoinfo;
5224
Eric W. Biederman15e47302012-09-07 20:12:54 +00005225 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*hdr), flags);
Ian Morris63159f22015-03-29 14:00:04 +01005226 if (!nlh)
Thomas Grafb382b192010-11-16 04:33:57 +00005227 return -EMSGSIZE;
5228
5229 hdr = nlmsg_data(nlh);
5230 hdr->ifi_family = AF_INET6;
5231 hdr->__ifi_pad = 0;
5232 hdr->ifi_type = dev->type;
5233 hdr->ifi_index = dev->ifindex;
5234 hdr->ifi_flags = dev_get_flags(dev);
5235 hdr->ifi_change = 0;
5236
David S. Millerc78679e2012-04-01 20:27:33 -04005237 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
5238 (dev->addr_len &&
5239 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
5240 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
Nicolas Dichtela54acb32015-04-02 17:07:00 +02005241 (dev->ifindex != dev_get_iflink(dev) &&
Andy Gospodarek03443382015-08-13 15:26:35 -04005242 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))) ||
5243 nla_put_u8(skb, IFLA_OPERSTATE,
5244 netif_running(dev) ? dev->operstate : IF_OPER_DOWN))
David S. Millerc78679e2012-04-01 20:27:33 -04005245 goto nla_put_failure;
Thomas Grafb382b192010-11-16 04:33:57 +00005246 protoinfo = nla_nest_start(skb, IFLA_PROTINFO);
Ian Morris63159f22015-03-29 14:00:04 +01005247 if (!protoinfo)
Thomas Grafb382b192010-11-16 04:33:57 +00005248 goto nla_put_failure;
5249
Sowmini Varadhand5566fd2015-09-11 16:48:48 -04005250 if (inet6_fill_ifla6_attrs(skb, idev, 0) < 0)
Thomas Grafb382b192010-11-16 04:33:57 +00005251 goto nla_put_failure;
5252
Thomas Graf04561c12006-11-14 19:53:58 -08005253 nla_nest_end(skb, protoinfo);
Johannes Berg053c0952015-01-16 22:09:00 +01005254 nlmsg_end(skb, nlh);
5255 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005256
Thomas Graf04561c12006-11-14 19:53:58 -08005257nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08005258 nlmsg_cancel(skb, nlh);
5259 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005260}
5261
5262static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
5263{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09005264 struct net *net = sock_net(skb->sk);
Eric Dumazet84d26972009-11-09 12:11:28 +00005265 int h, s_h;
David S. Miller434a8a582009-11-11 18:53:00 -08005266 int idx = 0, s_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005267 struct net_device *dev;
5268 struct inet6_dev *idev;
Eric Dumazet84d26972009-11-09 12:11:28 +00005269 struct hlist_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005270
Eric Dumazet84d26972009-11-09 12:11:28 +00005271 s_h = cb->args[0];
5272 s_idx = cb->args[1];
5273
5274 rcu_read_lock();
5275 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
5276 idx = 0;
5277 head = &net->dev_index_head[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -08005278 hlist_for_each_entry_rcu(dev, head, index_hlist) {
Eric Dumazet84d26972009-11-09 12:11:28 +00005279 if (idx < s_idx)
5280 goto cont;
5281 idev = __in6_dev_get(dev);
5282 if (!idev)
5283 goto cont;
5284 if (inet6_fill_ifinfo(skb, idev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005285 NETLINK_CB(cb->skb).portid,
Eric Dumazet84d26972009-11-09 12:11:28 +00005286 cb->nlh->nlmsg_seq,
Johannes Berg053c0952015-01-16 22:09:00 +01005287 RTM_NEWLINK, NLM_F_MULTI) < 0)
Eric Dumazet84d26972009-11-09 12:11:28 +00005288 goto out;
Pavel Emelianov7562f872007-05-03 15:13:45 -07005289cont:
Eric Dumazet84d26972009-11-09 12:11:28 +00005290 idx++;
5291 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005292 }
Eric Dumazet84d26972009-11-09 12:11:28 +00005293out:
5294 rcu_read_unlock();
5295 cb->args[1] = idx;
5296 cb->args[0] = h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005297
5298 return skb->len;
5299}
5300
5301void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
5302{
5303 struct sk_buff *skb;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09005304 struct net *net = dev_net(idev->dev);
Thomas Graf8d7a76c2006-08-15 00:35:47 -07005305 int err = -ENOBUFS;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09005306
Thomas Graf339bf982006-11-10 14:10:15 -08005307 skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC);
Ian Morris63159f22015-03-29 14:00:04 +01005308 if (!skb)
Thomas Graf8d7a76c2006-08-15 00:35:47 -07005309 goto errout;
5310
5311 err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08005312 if (err < 0) {
5313 /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */
5314 WARN_ON(err == -EMSGSIZE);
5315 kfree_skb(skb);
5316 goto errout;
5317 }
Nicolas Dichtel5f75a102010-12-07 23:38:31 +00005318 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFINFO, NULL, GFP_ATOMIC);
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08005319 return;
Thomas Graf8d7a76c2006-08-15 00:35:47 -07005320errout:
5321 if (err < 0)
Nicolas Dichtel5f75a102010-12-07 23:38:31 +00005322 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFINFO, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005323}
5324
Thomas Graf339bf982006-11-10 14:10:15 -08005325static inline size_t inet6_prefix_nlmsg_size(void)
5326{
5327 return NLMSG_ALIGN(sizeof(struct prefixmsg))
5328 + nla_total_size(sizeof(struct in6_addr))
5329 + nla_total_size(sizeof(struct prefix_cacheinfo));
5330}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07005331
Linus Torvalds1da177e2005-04-16 15:20:36 -07005332static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005333 struct prefix_info *pinfo, u32 portid, u32 seq,
Thomas Graf6051e2f2006-11-14 19:54:19 -08005334 int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005335{
Thomas Graf6051e2f2006-11-14 19:54:19 -08005336 struct prefixmsg *pmsg;
5337 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005338 struct prefix_cacheinfo ci;
5339
Eric W. Biederman15e47302012-09-07 20:12:54 +00005340 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*pmsg), flags);
Ian Morris63159f22015-03-29 14:00:04 +01005341 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08005342 return -EMSGSIZE;
Thomas Graf6051e2f2006-11-14 19:54:19 -08005343
5344 pmsg = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005345 pmsg->prefix_family = AF_INET6;
Patrick McHardy8a470772005-06-28 12:56:45 -07005346 pmsg->prefix_pad1 = 0;
5347 pmsg->prefix_pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005348 pmsg->prefix_ifindex = idev->dev->ifindex;
5349 pmsg->prefix_len = pinfo->prefix_len;
5350 pmsg->prefix_type = pinfo->type;
Patrick McHardy8a470772005-06-28 12:56:45 -07005351 pmsg->prefix_pad3 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005352 pmsg->prefix_flags = 0;
5353 if (pinfo->onlink)
5354 pmsg->prefix_flags |= IF_PREFIX_ONLINK;
5355 if (pinfo->autoconf)
5356 pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
5357
David S. Millerc78679e2012-04-01 20:27:33 -04005358 if (nla_put(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix))
5359 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005360 ci.preferred_time = ntohl(pinfo->prefered);
5361 ci.valid_time = ntohl(pinfo->valid);
David S. Millerc78679e2012-04-01 20:27:33 -04005362 if (nla_put(skb, PREFIX_CACHEINFO, sizeof(ci), &ci))
5363 goto nla_put_failure;
Johannes Berg053c0952015-01-16 22:09:00 +01005364 nlmsg_end(skb, nlh);
5365 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005366
Thomas Graf6051e2f2006-11-14 19:54:19 -08005367nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08005368 nlmsg_cancel(skb, nlh);
5369 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005370}
5371
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09005372static void inet6_prefix_notify(int event, struct inet6_dev *idev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005373 struct prefix_info *pinfo)
5374{
5375 struct sk_buff *skb;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09005376 struct net *net = dev_net(idev->dev);
Thomas Graf8c384bfa2006-08-15 00:36:07 -07005377 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005378
Thomas Graf339bf982006-11-10 14:10:15 -08005379 skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC);
Ian Morris63159f22015-03-29 14:00:04 +01005380 if (!skb)
Thomas Graf8c384bfa2006-08-15 00:36:07 -07005381 goto errout;
5382
5383 err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08005384 if (err < 0) {
5385 /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */
5386 WARN_ON(err == -EMSGSIZE);
5387 kfree_skb(skb);
5388 goto errout;
5389 }
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08005390 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC);
5391 return;
Thomas Graf8c384bfa2006-08-15 00:36:07 -07005392errout:
5393 if (err < 0)
Benjamin Thery6fda7352008-03-05 10:47:47 -08005394 rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005395}
5396
Linus Torvalds1da177e2005-04-16 15:20:36 -07005397static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
5398{
Nicolas Dichtel63998ac2013-03-22 06:28:43 +00005399 struct net *net = dev_net(ifp->idev->dev);
5400
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01005401 if (event)
5402 ASSERT_RTNL();
5403
Linus Torvalds1da177e2005-04-16 15:20:36 -07005404 inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
5405
5406 switch (event) {
5407 case RTM_NEWADDR:
Neil Horman95c385b2007-04-25 17:08:10 -07005408 /*
5409 * If the address was optimistic
5410 * we inserted the route at the start of
5411 * our DAD process, so we don't need
5412 * to do it again
5413 */
5414 if (!(ifp->rt->rt6i_node))
5415 ip6_ins_rt(ifp->rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005416 if (ifp->idev->cnf.forwarding)
5417 addrconf_join_anycast(ifp);
Cong Wang7996c792013-05-22 05:41:06 +00005418 if (!ipv6_addr_any(&ifp->peer_addr))
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00005419 addrconf_prefix_route(&ifp->peer_addr, 128,
5420 ifp->idev->dev, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005421 break;
5422 case RTM_DELADDR:
5423 if (ifp->idev->cnf.forwarding)
5424 addrconf_leave_anycast(ifp);
5425 addrconf_leave_solict(ifp->idev, &ifp->addr);
Cong Wang7996c792013-05-22 05:41:06 +00005426 if (!ipv6_addr_any(&ifp->peer_addr)) {
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00005427 struct rt6_info *rt;
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00005428
Nicolas Dichtele7478df2014-09-03 23:59:22 +02005429 rt = addrconf_get_prefix_route(&ifp->peer_addr, 128,
5430 ifp->idev->dev, 0, 0);
Martin KaFai Lau8e3d5be2015-09-15 14:30:08 -07005431 if (rt)
5432 ip6_del_rt(rt);
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00005433 }
David Ahern38bd10c2016-04-21 20:56:12 -07005434 if (ifp->rt) {
5435 dst_hold(&ifp->rt->dst);
5436 ip6_del_rt(ifp->rt);
5437 }
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02005438 rt_genid_bump_ipv6(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005439 break;
5440 }
Nicolas Dichtel63998ac2013-03-22 06:28:43 +00005441 atomic_inc(&net->ipv6.dev_addr_genid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005442}
5443
5444static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
5445{
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07005446 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005447 if (likely(ifp->idev->dead == 0))
5448 __ipv6_ifa_notify(event, ifp);
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07005449 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005450}
5451
5452#ifdef CONFIG_SYSCTL
5453
5454static
Joe Perchesfe2c6332013-06-11 23:04:25 -07005455int addrconf_sysctl_forward(struct ctl_table *ctl, int write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005456 void __user *buffer, size_t *lenp, loff_t *ppos)
5457{
5458 int *valp = ctl->data;
5459 int val = *valp;
Eric W. Biederman88af1822010-02-19 13:22:59 +00005460 loff_t pos = *ppos;
Joe Perchesfe2c6332013-06-11 23:04:25 -07005461 struct ctl_table lctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005462 int ret;
5463
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00005464 /*
5465 * ctl->data points to idev->cnf.forwarding, we should
5466 * not modify it until we get the rtnl lock.
5467 */
5468 lctl = *ctl;
5469 lctl.data = &val;
5470
5471 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005472
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -08005473 if (write)
Stephen Hemmingerb325fdd2009-02-26 06:55:31 +00005474 ret = addrconf_fixup_forwarding(ctl, valp, val);
Eric W. Biederman88af1822010-02-19 13:22:59 +00005475 if (ret)
5476 *ppos = pos;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09005477 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478}
5479
Marcelo Leitner77751422015-02-23 11:17:13 -03005480static
Phil Sutterd6df1982015-12-01 22:45:15 +01005481int addrconf_sysctl_hop_limit(struct ctl_table *ctl, int write,
5482 void __user *buffer, size_t *lenp, loff_t *ppos)
5483{
5484 struct ctl_table lctl;
5485 int min_hl = 1, max_hl = 255;
5486
5487 lctl = *ctl;
5488 lctl.extra1 = &min_hl;
5489 lctl.extra2 = &max_hl;
5490
5491 return proc_dointvec_minmax(&lctl, write, buffer, lenp, ppos);
5492}
5493
5494static
Marcelo Leitner77751422015-02-23 11:17:13 -03005495int addrconf_sysctl_mtu(struct ctl_table *ctl, int write,
5496 void __user *buffer, size_t *lenp, loff_t *ppos)
5497{
5498 struct inet6_dev *idev = ctl->extra1;
5499 int min_mtu = IPV6_MIN_MTU;
5500 struct ctl_table lctl;
5501
5502 lctl = *ctl;
5503 lctl.extra1 = &min_mtu;
5504 lctl.extra2 = idev ? &idev->dev->mtu : NULL;
5505
5506 return proc_dointvec_minmax(&lctl, write, buffer, lenp, ppos);
5507}
5508
Brian Haley56d417b2009-06-01 03:07:33 -07005509static void dev_disable_change(struct inet6_dev *idev)
5510{
Cong Wang75538c22013-05-29 11:30:50 +08005511 struct netdev_notifier_info info;
5512
Brian Haley56d417b2009-06-01 03:07:33 -07005513 if (!idev || !idev->dev)
5514 return;
5515
Cong Wang75538c22013-05-29 11:30:50 +08005516 netdev_notifier_info_init(&info, idev->dev);
Brian Haley56d417b2009-06-01 03:07:33 -07005517 if (idev->cnf.disable_ipv6)
Cong Wang75538c22013-05-29 11:30:50 +08005518 addrconf_notify(NULL, NETDEV_DOWN, &info);
Brian Haley56d417b2009-06-01 03:07:33 -07005519 else
Cong Wang75538c22013-05-29 11:30:50 +08005520 addrconf_notify(NULL, NETDEV_UP, &info);
Brian Haley56d417b2009-06-01 03:07:33 -07005521}
5522
5523static void addrconf_disable_change(struct net *net, __s32 newf)
5524{
5525 struct net_device *dev;
5526 struct inet6_dev *idev;
5527
Eric Dumazetc6d14c82009-11-04 05:43:23 -08005528 rcu_read_lock();
5529 for_each_netdev_rcu(net, dev) {
Brian Haley56d417b2009-06-01 03:07:33 -07005530 idev = __in6_dev_get(dev);
5531 if (idev) {
5532 int changed = (!idev->cnf.disable_ipv6) ^ (!newf);
5533 idev->cnf.disable_ipv6 = newf;
5534 if (changed)
5535 dev_disable_change(idev);
5536 }
Brian Haley56d417b2009-06-01 03:07:33 -07005537 }
Eric Dumazetc6d14c82009-11-04 05:43:23 -08005538 rcu_read_unlock();
Brian Haley56d417b2009-06-01 03:07:33 -07005539}
5540
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00005541static int addrconf_disable_ipv6(struct ctl_table *table, int *p, int newf)
Brian Haley56d417b2009-06-01 03:07:33 -07005542{
5543 struct net *net;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00005544 int old;
5545
5546 if (!rtnl_trylock())
5547 return restart_syscall();
Brian Haley56d417b2009-06-01 03:07:33 -07005548
5549 net = (struct net *)table->extra2;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00005550 old = *p;
5551 *p = newf;
Brian Haley56d417b2009-06-01 03:07:33 -07005552
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00005553 if (p == &net->ipv6.devconf_dflt->disable_ipv6) {
5554 rtnl_unlock();
Brian Haley56d417b2009-06-01 03:07:33 -07005555 return 0;
Eric W. Biederman88af1822010-02-19 13:22:59 +00005556 }
Brian Haley56d417b2009-06-01 03:07:33 -07005557
5558 if (p == &net->ipv6.devconf_all->disable_ipv6) {
Brian Haley56d417b2009-06-01 03:07:33 -07005559 net->ipv6.devconf_dflt->disable_ipv6 = newf;
5560 addrconf_disable_change(net, newf);
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00005561 } else if ((!newf) ^ (!old))
Brian Haley56d417b2009-06-01 03:07:33 -07005562 dev_disable_change((struct inet6_dev *)table->extra1);
5563
5564 rtnl_unlock();
5565 return 0;
5566}
5567
5568static
Joe Perchesfe2c6332013-06-11 23:04:25 -07005569int addrconf_sysctl_disable(struct ctl_table *ctl, int write,
Brian Haley56d417b2009-06-01 03:07:33 -07005570 void __user *buffer, size_t *lenp, loff_t *ppos)
5571{
5572 int *valp = ctl->data;
5573 int val = *valp;
Eric W. Biederman88af1822010-02-19 13:22:59 +00005574 loff_t pos = *ppos;
Joe Perchesfe2c6332013-06-11 23:04:25 -07005575 struct ctl_table lctl;
Brian Haley56d417b2009-06-01 03:07:33 -07005576 int ret;
5577
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00005578 /*
5579 * ctl->data points to idev->cnf.disable_ipv6, we should
5580 * not modify it until we get the rtnl lock.
5581 */
5582 lctl = *ctl;
5583 lctl.data = &val;
5584
5585 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
Brian Haley56d417b2009-06-01 03:07:33 -07005586
5587 if (write)
5588 ret = addrconf_disable_ipv6(ctl, valp, val);
Eric W. Biederman88af1822010-02-19 13:22:59 +00005589 if (ret)
5590 *ppos = pos;
Brian Haley56d417b2009-06-01 03:07:33 -07005591 return ret;
5592}
5593
stephen hemmingerc92d5492013-12-17 22:37:14 -08005594static
5595int addrconf_sysctl_proxy_ndp(struct ctl_table *ctl, int write,
5596 void __user *buffer, size_t *lenp, loff_t *ppos)
5597{
5598 int *valp = ctl->data;
5599 int ret;
5600 int old, new;
5601
5602 old = *valp;
5603 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
5604 new = *valp;
5605
5606 if (write && old != new) {
5607 struct net *net = ctl->extra2;
5608
5609 if (!rtnl_trylock())
5610 return restart_syscall();
5611
5612 if (valp == &net->ipv6.devconf_dflt->proxy_ndp)
5613 inet6_netconf_notify_devconf(net, NETCONFA_PROXY_NEIGH,
5614 NETCONFA_IFINDEX_DEFAULT,
5615 net->ipv6.devconf_dflt);
5616 else if (valp == &net->ipv6.devconf_all->proxy_ndp)
5617 inet6_netconf_notify_devconf(net, NETCONFA_PROXY_NEIGH,
5618 NETCONFA_IFINDEX_ALL,
5619 net->ipv6.devconf_all);
5620 else {
5621 struct inet6_dev *idev = ctl->extra1;
5622
5623 inet6_netconf_notify_devconf(net, NETCONFA_PROXY_NEIGH,
5624 idev->dev->ifindex,
5625 &idev->cnf);
5626 }
5627 rtnl_unlock();
5628 }
5629
5630 return ret;
5631}
5632
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01005633static int addrconf_sysctl_stable_secret(struct ctl_table *ctl, int write,
5634 void __user *buffer, size_t *lenp,
5635 loff_t *ppos)
5636{
5637 int err;
5638 struct in6_addr addr;
5639 char str[IPV6_MAX_STRLEN];
5640 struct ctl_table lctl = *ctl;
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01005641 struct net *net = ctl->extra2;
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01005642 struct ipv6_stable_secret *secret = ctl->data;
5643
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01005644 if (&net->ipv6.devconf_all->stable_secret == ctl->data)
5645 return -EIO;
5646
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01005647 lctl.maxlen = IPV6_MAX_STRLEN;
5648 lctl.data = str;
5649
5650 if (!rtnl_trylock())
5651 return restart_syscall();
5652
5653 if (!write && !secret->initialized) {
5654 err = -EIO;
5655 goto out;
5656 }
5657
WANG Cong5449a5c2015-12-21 10:55:45 -08005658 err = snprintf(str, sizeof(str), "%pI6", &secret->secret);
5659 if (err >= sizeof(str)) {
5660 err = -EIO;
5661 goto out;
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01005662 }
5663
5664 err = proc_dostring(&lctl, write, buffer, lenp, ppos);
5665 if (err || !write)
5666 goto out;
5667
5668 if (in6_pton(str, -1, addr.in6_u.u6_addr8, -1, NULL) != 1) {
5669 err = -EIO;
5670 goto out;
5671 }
5672
5673 secret->initialized = true;
5674 secret->secret = addr;
5675
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01005676 if (&net->ipv6.devconf_dflt->stable_secret == ctl->data) {
5677 struct net_device *dev;
5678
5679 for_each_netdev(net, dev) {
5680 struct inet6_dev *idev = __in6_dev_get(dev);
5681
5682 if (idev) {
5683 idev->addr_gen_mode =
5684 IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
5685 }
5686 }
5687 } else {
5688 struct inet6_dev *idev = ctl->extra1;
5689
5690 idev->addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
5691 }
5692
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01005693out:
5694 rtnl_unlock();
5695
5696 return err;
5697}
stephen hemmingerc92d5492013-12-17 22:37:14 -08005698
Andy Gospodarek35103d12015-08-13 10:39:01 -04005699static
5700int addrconf_sysctl_ignore_routes_with_linkdown(struct ctl_table *ctl,
5701 int write,
5702 void __user *buffer,
5703 size_t *lenp,
5704 loff_t *ppos)
5705{
5706 int *valp = ctl->data;
5707 int val = *valp;
5708 loff_t pos = *ppos;
5709 struct ctl_table lctl;
5710 int ret;
5711
5712 /* ctl->data points to idev->cnf.ignore_routes_when_linkdown
5713 * we should not modify it until we get the rtnl lock.
5714 */
5715 lctl = *ctl;
5716 lctl.data = &val;
5717
5718 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
5719
5720 if (write)
5721 ret = addrconf_fixup_linkdown(ctl, valp, val);
5722 if (ret)
5723 *ppos = pos;
5724 return ret;
5725}
5726
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03005727static const struct ctl_table addrconf_sysctl[] = {
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03005728 {
5729 .procname = "forwarding",
5730 .data = &ipv6_devconf.forwarding,
5731 .maxlen = sizeof(int),
5732 .mode = 0644,
5733 .proc_handler = addrconf_sysctl_forward,
5734 },
5735 {
5736 .procname = "hop_limit",
5737 .data = &ipv6_devconf.hop_limit,
5738 .maxlen = sizeof(int),
5739 .mode = 0644,
5740 .proc_handler = addrconf_sysctl_hop_limit,
5741 },
5742 {
5743 .procname = "mtu",
5744 .data = &ipv6_devconf.mtu6,
5745 .maxlen = sizeof(int),
5746 .mode = 0644,
5747 .proc_handler = addrconf_sysctl_mtu,
5748 },
5749 {
5750 .procname = "accept_ra",
5751 .data = &ipv6_devconf.accept_ra,
5752 .maxlen = sizeof(int),
5753 .mode = 0644,
5754 .proc_handler = proc_dointvec,
5755 },
5756 {
5757 .procname = "accept_redirects",
5758 .data = &ipv6_devconf.accept_redirects,
5759 .maxlen = sizeof(int),
5760 .mode = 0644,
5761 .proc_handler = proc_dointvec,
5762 },
5763 {
5764 .procname = "autoconf",
5765 .data = &ipv6_devconf.autoconf,
5766 .maxlen = sizeof(int),
5767 .mode = 0644,
5768 .proc_handler = proc_dointvec,
5769 },
5770 {
5771 .procname = "dad_transmits",
5772 .data = &ipv6_devconf.dad_transmits,
5773 .maxlen = sizeof(int),
5774 .mode = 0644,
5775 .proc_handler = proc_dointvec,
5776 },
5777 {
5778 .procname = "router_solicitations",
5779 .data = &ipv6_devconf.rtr_solicits,
5780 .maxlen = sizeof(int),
5781 .mode = 0644,
5782 .proc_handler = proc_dointvec,
5783 },
5784 {
5785 .procname = "router_solicitation_interval",
5786 .data = &ipv6_devconf.rtr_solicit_interval,
5787 .maxlen = sizeof(int),
5788 .mode = 0644,
5789 .proc_handler = proc_dointvec_jiffies,
5790 },
5791 {
5792 .procname = "router_solicitation_delay",
5793 .data = &ipv6_devconf.rtr_solicit_delay,
5794 .maxlen = sizeof(int),
5795 .mode = 0644,
5796 .proc_handler = proc_dointvec_jiffies,
5797 },
5798 {
5799 .procname = "force_mld_version",
5800 .data = &ipv6_devconf.force_mld_version,
5801 .maxlen = sizeof(int),
5802 .mode = 0644,
5803 .proc_handler = proc_dointvec,
5804 },
5805 {
5806 .procname = "mldv1_unsolicited_report_interval",
5807 .data =
5808 &ipv6_devconf.mldv1_unsolicited_report_interval,
5809 .maxlen = sizeof(int),
5810 .mode = 0644,
5811 .proc_handler = proc_dointvec_ms_jiffies,
5812 },
5813 {
5814 .procname = "mldv2_unsolicited_report_interval",
5815 .data =
5816 &ipv6_devconf.mldv2_unsolicited_report_interval,
5817 .maxlen = sizeof(int),
5818 .mode = 0644,
5819 .proc_handler = proc_dointvec_ms_jiffies,
5820 },
5821 {
5822 .procname = "use_tempaddr",
5823 .data = &ipv6_devconf.use_tempaddr,
5824 .maxlen = sizeof(int),
5825 .mode = 0644,
5826 .proc_handler = proc_dointvec,
5827 },
5828 {
5829 .procname = "temp_valid_lft",
5830 .data = &ipv6_devconf.temp_valid_lft,
5831 .maxlen = sizeof(int),
5832 .mode = 0644,
5833 .proc_handler = proc_dointvec,
5834 },
5835 {
5836 .procname = "temp_prefered_lft",
5837 .data = &ipv6_devconf.temp_prefered_lft,
5838 .maxlen = sizeof(int),
5839 .mode = 0644,
5840 .proc_handler = proc_dointvec,
5841 },
5842 {
5843 .procname = "regen_max_retry",
5844 .data = &ipv6_devconf.regen_max_retry,
5845 .maxlen = sizeof(int),
5846 .mode = 0644,
5847 .proc_handler = proc_dointvec,
5848 },
5849 {
5850 .procname = "max_desync_factor",
5851 .data = &ipv6_devconf.max_desync_factor,
5852 .maxlen = sizeof(int),
5853 .mode = 0644,
5854 .proc_handler = proc_dointvec,
5855 },
5856 {
5857 .procname = "max_addresses",
5858 .data = &ipv6_devconf.max_addresses,
5859 .maxlen = sizeof(int),
5860 .mode = 0644,
5861 .proc_handler = proc_dointvec,
5862 },
5863 {
5864 .procname = "accept_ra_defrtr",
5865 .data = &ipv6_devconf.accept_ra_defrtr,
5866 .maxlen = sizeof(int),
5867 .mode = 0644,
5868 .proc_handler = proc_dointvec,
5869 },
5870 {
5871 .procname = "accept_ra_min_hop_limit",
5872 .data = &ipv6_devconf.accept_ra_min_hop_limit,
5873 .maxlen = sizeof(int),
5874 .mode = 0644,
5875 .proc_handler = proc_dointvec,
5876 },
5877 {
5878 .procname = "accept_ra_pinfo",
5879 .data = &ipv6_devconf.accept_ra_pinfo,
5880 .maxlen = sizeof(int),
5881 .mode = 0644,
5882 .proc_handler = proc_dointvec,
5883 },
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08005884#ifdef CONFIG_IPV6_ROUTER_PREF
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03005885 {
5886 .procname = "accept_ra_rtr_pref",
5887 .data = &ipv6_devconf.accept_ra_rtr_pref,
5888 .maxlen = sizeof(int),
5889 .mode = 0644,
5890 .proc_handler = proc_dointvec,
5891 },
5892 {
5893 .procname = "router_probe_interval",
5894 .data = &ipv6_devconf.rtr_probe_interval,
5895 .maxlen = sizeof(int),
5896 .mode = 0644,
5897 .proc_handler = proc_dointvec_jiffies,
5898 },
Neil Hormanfa03ef32007-01-30 14:30:10 -08005899#ifdef CONFIG_IPV6_ROUTE_INFO
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03005900 {
5901 .procname = "accept_ra_rt_info_max_plen",
5902 .data = &ipv6_devconf.accept_ra_rt_info_max_plen,
5903 .maxlen = sizeof(int),
5904 .mode = 0644,
5905 .proc_handler = proc_dointvec,
5906 },
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -08005907#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08005908#endif
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03005909 {
5910 .procname = "proxy_ndp",
5911 .data = &ipv6_devconf.proxy_ndp,
5912 .maxlen = sizeof(int),
5913 .mode = 0644,
5914 .proc_handler = addrconf_sysctl_proxy_ndp,
5915 },
5916 {
5917 .procname = "accept_source_route",
5918 .data = &ipv6_devconf.accept_source_route,
5919 .maxlen = sizeof(int),
5920 .mode = 0644,
5921 .proc_handler = proc_dointvec,
5922 },
Lorenzo Colitti2b253062014-03-26 19:35:41 +09005923 {
5924 .procname = "accept_ra_rt_table",
5925 .data = &ipv6_devconf.accept_ra_rt_table,
5926 .maxlen = sizeof(int),
5927 .mode = 0644,
5928 .proc_handler = proc_dointvec,
5929 },
Neil Horman95c385b2007-04-25 17:08:10 -07005930#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03005931 {
5932 .procname = "optimistic_dad",
5933 .data = &ipv6_devconf.optimistic_dad,
5934 .maxlen = sizeof(int),
5935 .mode = 0644,
5936 .proc_handler = proc_dointvec,
5937 },
5938 {
5939 .procname = "use_optimistic",
5940 .data = &ipv6_devconf.use_optimistic,
5941 .maxlen = sizeof(int),
5942 .mode = 0644,
5943 .proc_handler = proc_dointvec,
5944 },
Neil Horman95c385b2007-04-25 17:08:10 -07005945#endif
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09005946#ifdef CONFIG_IPV6_MROUTE
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03005947 {
5948 .procname = "mc_forwarding",
5949 .data = &ipv6_devconf.mc_forwarding,
5950 .maxlen = sizeof(int),
5951 .mode = 0444,
5952 .proc_handler = proc_dointvec,
5953 },
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09005954#endif
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03005955 {
5956 .procname = "disable_ipv6",
5957 .data = &ipv6_devconf.disable_ipv6,
5958 .maxlen = sizeof(int),
5959 .mode = 0644,
5960 .proc_handler = addrconf_sysctl_disable,
5961 },
5962 {
5963 .procname = "accept_dad",
5964 .data = &ipv6_devconf.accept_dad,
5965 .maxlen = sizeof(int),
5966 .mode = 0644,
5967 .proc_handler = proc_dointvec,
5968 },
5969 {
5970 .procname = "force_tllao",
5971 .data = &ipv6_devconf.force_tllao,
5972 .maxlen = sizeof(int),
5973 .mode = 0644,
5974 .proc_handler = proc_dointvec
5975 },
5976 {
5977 .procname = "ndisc_notify",
5978 .data = &ipv6_devconf.ndisc_notify,
5979 .maxlen = sizeof(int),
5980 .mode = 0644,
5981 .proc_handler = proc_dointvec
5982 },
5983 {
5984 .procname = "suppress_frag_ndisc",
5985 .data = &ipv6_devconf.suppress_frag_ndisc,
5986 .maxlen = sizeof(int),
5987 .mode = 0644,
5988 .proc_handler = proc_dointvec
5989 },
5990 {
5991 .procname = "accept_ra_from_local",
5992 .data = &ipv6_devconf.accept_ra_from_local,
5993 .maxlen = sizeof(int),
5994 .mode = 0644,
5995 .proc_handler = proc_dointvec,
5996 },
5997 {
5998 .procname = "accept_ra_mtu",
5999 .data = &ipv6_devconf.accept_ra_mtu,
6000 .maxlen = sizeof(int),
6001 .mode = 0644,
6002 .proc_handler = proc_dointvec,
6003 },
6004 {
6005 .procname = "stable_secret",
6006 .data = &ipv6_devconf.stable_secret,
6007 .maxlen = IPV6_MAX_STRLEN,
6008 .mode = 0600,
6009 .proc_handler = addrconf_sysctl_stable_secret,
6010 },
6011 {
6012 .procname = "use_oif_addrs_only",
6013 .data = &ipv6_devconf.use_oif_addrs_only,
6014 .maxlen = sizeof(int),
6015 .mode = 0644,
6016 .proc_handler = proc_dointvec,
6017 },
6018 {
6019 .procname = "ignore_routes_with_linkdown",
6020 .data = &ipv6_devconf.ignore_routes_with_linkdown,
6021 .maxlen = sizeof(int),
6022 .mode = 0644,
6023 .proc_handler = addrconf_sysctl_ignore_routes_with_linkdown,
6024 },
6025 {
6026 .procname = "drop_unicast_in_l2_multicast",
6027 .data = &ipv6_devconf.drop_unicast_in_l2_multicast,
6028 .maxlen = sizeof(int),
6029 .mode = 0644,
6030 .proc_handler = proc_dointvec,
6031 },
6032 {
6033 .procname = "drop_unsolicited_na",
6034 .data = &ipv6_devconf.drop_unsolicited_na,
6035 .maxlen = sizeof(int),
6036 .mode = 0644,
6037 .proc_handler = proc_dointvec,
6038 },
6039 {
6040 .procname = "keep_addr_on_down",
6041 .data = &ipv6_devconf.keep_addr_on_down,
6042 .maxlen = sizeof(int),
6043 .mode = 0644,
6044 .proc_handler = proc_dointvec,
David Ahernf1705ec2016-02-24 09:25:37 -08006045
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006046 },
6047 {
6048 /* sentinel */
6049 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006050};
6051
Pavel Emelyanovbff16c22008-01-10 17:42:13 -08006052static int __addrconf_sysctl_register(struct net *net, char *dev_name,
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08006053 struct inet6_dev *idev, struct ipv6_devconf *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006054{
6055 int i;
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006056 struct ctl_table *table;
Eric W. Biederman6105e292012-04-19 13:41:24 +00006057 char path[sizeof("net/ipv6/conf/") + IFNAMSIZ];
Pavel Emelyanov1dab6222007-12-02 00:59:38 +11006058
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006059 table = kmemdup(addrconf_sysctl, sizeof(addrconf_sysctl), GFP_KERNEL);
6060 if (!table)
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11006061 goto out;
6062
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006063 for (i = 0; table[i].data; i++) {
6064 table[i].data += (char *)p - (char *)&ipv6_devconf;
6065 table[i].extra1 = idev; /* embedded; no ref */
6066 table[i].extra2 = net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006067 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006068
Eric W. Biederman6105e292012-04-19 13:41:24 +00006069 snprintf(path, sizeof(path), "net/ipv6/conf/%s", dev_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006070
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006071 p->sysctl_header = register_net_sysctl(net, path, table);
6072 if (!p->sysctl_header)
Eric W. Biederman6105e292012-04-19 13:41:24 +00006073 goto free;
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11006074
Pavel Emelyanov95897312008-01-10 17:41:45 -08006075 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006076
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11006077free:
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006078 kfree(table);
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11006079out:
Pavel Emelyanov95897312008-01-10 17:41:45 -08006080 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006081}
6082
Pavel Emelyanov408c4762008-01-10 17:41:21 -08006083static void __addrconf_sysctl_unregister(struct ipv6_devconf *p)
6084{
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006085 struct ctl_table *table;
Pavel Emelyanov408c4762008-01-10 17:41:21 -08006086
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006087 if (!p->sysctl_header)
Pavel Emelyanov408c4762008-01-10 17:41:21 -08006088 return;
6089
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006090 table = p->sysctl_header->ctl_table_arg;
6091 unregister_net_sysctl_table(p->sysctl_header);
6092 p->sysctl_header = NULL;
6093 kfree(table);
Pavel Emelyanov408c4762008-01-10 17:41:21 -08006094}
6095
WANG Conga317a2f2014-07-25 15:25:09 -07006096static int addrconf_sysctl_register(struct inet6_dev *idev)
Pavel Emelyanovf52295a2007-12-02 00:58:37 +11006097{
WANG Conga317a2f2014-07-25 15:25:09 -07006098 int err;
6099
6100 if (!sysctl_dev_name_is_allowed(idev->dev->name))
6101 return -EINVAL;
6102
6103 err = neigh_sysctl_register(idev->dev, idev->nd_parms,
6104 &ndisc_ifinfo_sysctl_change);
6105 if (err)
6106 return err;
6107 err = __addrconf_sysctl_register(dev_net(idev->dev), idev->dev->name,
6108 idev, &idev->cnf);
6109 if (err)
6110 neigh_sysctl_unregister(idev->nd_parms);
6111
6112 return err;
Pavel Emelyanovf52295a2007-12-02 00:58:37 +11006113}
6114
Pavel Emelyanov408c4762008-01-10 17:41:21 -08006115static void addrconf_sysctl_unregister(struct inet6_dev *idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006116{
Pavel Emelyanov408c4762008-01-10 17:41:21 -08006117 __addrconf_sysctl_unregister(&idev->cnf);
6118 neigh_sysctl_unregister(idev->nd_parms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006119}
6120
6121
6122#endif
6123
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00006124static int __net_init addrconf_init_net(struct net *net)
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006125{
Hong Zhiguoa79ca222013-03-26 01:52:45 +08006126 int err = -ENOMEM;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006127 struct ipv6_devconf *all, *dflt;
6128
Hong Zhiguoa79ca222013-03-26 01:52:45 +08006129 all = kmemdup(&ipv6_devconf, sizeof(ipv6_devconf), GFP_KERNEL);
Ian Morris63159f22015-03-29 14:00:04 +01006130 if (!all)
Hong Zhiguoa79ca222013-03-26 01:52:45 +08006131 goto err_alloc_all;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006132
Hong Zhiguoa79ca222013-03-26 01:52:45 +08006133 dflt = kmemdup(&ipv6_devconf_dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL);
Ian Morris63159f22015-03-29 14:00:04 +01006134 if (!dflt)
Hong Zhiguoa79ca222013-03-26 01:52:45 +08006135 goto err_alloc_dflt;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006136
Hong Zhiguoa79ca222013-03-26 01:52:45 +08006137 /* these will be inherited by all namespaces */
6138 dflt->autoconf = ipv6_defaults.autoconf;
6139 dflt->disable_ipv6 = ipv6_defaults.disable_ipv6;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006140
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01006141 dflt->stable_secret.initialized = false;
6142 all->stable_secret.initialized = false;
6143
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006144 net->ipv6.devconf_all = all;
6145 net->ipv6.devconf_dflt = dflt;
6146
6147#ifdef CONFIG_SYSCTL
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08006148 err = __addrconf_sysctl_register(net, "all", NULL, all);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006149 if (err < 0)
6150 goto err_reg_all;
6151
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08006152 err = __addrconf_sysctl_register(net, "default", NULL, dflt);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006153 if (err < 0)
6154 goto err_reg_dflt;
6155#endif
6156 return 0;
6157
6158#ifdef CONFIG_SYSCTL
6159err_reg_dflt:
6160 __addrconf_sysctl_unregister(all);
6161err_reg_all:
6162 kfree(dflt);
6163#endif
6164err_alloc_dflt:
6165 kfree(all);
6166err_alloc_all:
6167 return err;
6168}
6169
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00006170static void __net_exit addrconf_exit_net(struct net *net)
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006171{
6172#ifdef CONFIG_SYSCTL
6173 __addrconf_sysctl_unregister(net->ipv6.devconf_dflt);
6174 __addrconf_sysctl_unregister(net->ipv6.devconf_all);
6175#endif
zhuyj73cf0e92014-11-26 10:25:58 +08006176 kfree(net->ipv6.devconf_dflt);
6177 kfree(net->ipv6.devconf_all);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006178}
6179
6180static struct pernet_operations addrconf_ops = {
6181 .init = addrconf_init_net,
6182 .exit = addrconf_exit_net,
6183};
6184
Daniel Borkmann207895f2015-01-29 12:15:03 +01006185static struct rtnl_af_ops inet6_ops __read_mostly = {
Thomas Grafb382b192010-11-16 04:33:57 +00006186 .family = AF_INET6,
6187 .fill_link_af = inet6_fill_link_af,
6188 .get_link_af_size = inet6_get_link_af_size,
Daniel Borkmann11b1f822015-02-05 14:39:11 +01006189 .validate_link_af = inet6_validate_link_af,
Daniel Borkmannf53adae2013-04-08 04:01:30 +00006190 .set_link_af = inet6_set_link_af,
Thomas Grafb382b192010-11-16 04:33:57 +00006191};
6192
Linus Torvalds1da177e2005-04-16 15:20:36 -07006193/*
6194 * Init / cleanup code
6195 */
6196
6197int __init addrconf_init(void)
6198{
WANG Conga317a2f2014-07-25 15:25:09 -07006199 struct inet6_dev *idev;
stephen hemmingerc2e21292010-03-17 20:31:10 +00006200 int i, err;
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +09006201
Stephen Hemmingere21e8462010-03-20 16:09:01 -07006202 err = ipv6_addr_label_init();
6203 if (err < 0) {
Joe Perchesf3213832012-05-15 14:11:53 +00006204 pr_crit("%s: cannot initialize default policy table: %d\n",
6205 __func__, err);
Neil Horman2cc6d2b2010-09-24 09:55:52 +00006206 goto out;
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +09006207 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006208
Neil Horman2cc6d2b2010-09-24 09:55:52 +00006209 err = register_pernet_subsys(&addrconf_ops);
6210 if (err < 0)
6211 goto out_addrlabel;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006212
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01006213 addrconf_wq = create_workqueue("ipv6_addrconf");
6214 if (!addrconf_wq) {
6215 err = -ENOMEM;
6216 goto out_nowq;
6217 }
6218
Linus Torvalds1da177e2005-04-16 15:20:36 -07006219 /* The addrconf netdev notifier requires that loopback_dev
6220 * has it's ipv6 private information allocated and setup
6221 * before it can bring up and give link-local addresses
6222 * to other devices which are up.
6223 *
6224 * Unfortunately, loopback_dev is not necessarily the first
6225 * entry in the global dev_base list of net devices. In fact,
6226 * it is likely to be the very last entry on that list.
6227 * So this causes the notifier registry below to try and
6228 * give link-local addresses to all devices besides loopback_dev
6229 * first, then loopback_dev, which cases all the non-loopback_dev
6230 * devices to fail to get a link-local address.
6231 *
6232 * So, as a temporary fix, allocate the ipv6 structure for
6233 * loopback_dev first by hand.
6234 * Longer term, all of the dependencies ipv6 has upon the loopback
6235 * device and it being up should be removed.
6236 */
6237 rtnl_lock();
WANG Conga317a2f2014-07-25 15:25:09 -07006238 idev = ipv6_add_dev(init_net.loopback_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006239 rtnl_unlock();
WANG Conga317a2f2014-07-25 15:25:09 -07006240 if (IS_ERR(idev)) {
6241 err = PTR_ERR(idev);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006242 goto errlo;
WANG Conga317a2f2014-07-25 15:25:09 -07006243 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006244
stephen hemmingerc2e21292010-03-17 20:31:10 +00006245 for (i = 0; i < IN6_ADDR_HSIZE; i++)
6246 INIT_HLIST_HEAD(&inet6_addr_lst[i]);
6247
Linus Torvalds1da177e2005-04-16 15:20:36 -07006248 register_netdevice_notifier(&ipv6_dev_notf);
6249
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01006250 addrconf_verify();
Thomas Grafc127ea22007-03-22 11:58:32 -07006251
stephen hemminger3678a9d2013-12-30 10:41:32 -08006252 rtnl_af_register(&inet6_ops);
Thomas Grafb382b192010-11-16 04:33:57 +00006253
Greg Rosec7ac8672011-06-10 01:27:09 +00006254 err = __rtnl_register(PF_INET6, RTM_GETLINK, NULL, inet6_dump_ifinfo,
6255 NULL);
Thomas Grafc127ea22007-03-22 11:58:32 -07006256 if (err < 0)
6257 goto errout;
6258
6259 /* Only the first call to __rtnl_register can fail */
Greg Rosec7ac8672011-06-10 01:27:09 +00006260 __rtnl_register(PF_INET6, RTM_NEWADDR, inet6_rtm_newaddr, NULL, NULL);
6261 __rtnl_register(PF_INET6, RTM_DELADDR, inet6_rtm_deladdr, NULL, NULL);
6262 __rtnl_register(PF_INET6, RTM_GETADDR, inet6_rtm_getaddr,
6263 inet6_dump_ifaddr, NULL);
6264 __rtnl_register(PF_INET6, RTM_GETMULTICAST, NULL,
6265 inet6_dump_ifmcaddr, NULL);
6266 __rtnl_register(PF_INET6, RTM_GETANYCAST, NULL,
6267 inet6_dump_ifacaddr, NULL);
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +00006268 __rtnl_register(PF_INET6, RTM_GETNETCONF, inet6_netconf_get_devconf,
Nicolas Dichtel7a674202013-03-05 23:42:06 +00006269 inet6_netconf_dump_devconf, NULL);
Thomas Grafc127ea22007-03-22 11:58:32 -07006270
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +09006271 ipv6_addr_label_rtnl_register();
6272
Linus Torvalds1da177e2005-04-16 15:20:36 -07006273 return 0;
Thomas Grafc127ea22007-03-22 11:58:32 -07006274errout:
Thomas Grafb382b192010-11-16 04:33:57 +00006275 rtnl_af_unregister(&inet6_ops);
Thomas Grafc127ea22007-03-22 11:58:32 -07006276 unregister_netdevice_notifier(&ipv6_dev_notf);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006277errlo:
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01006278 destroy_workqueue(addrconf_wq);
6279out_nowq:
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006280 unregister_pernet_subsys(&addrconf_ops);
Neil Horman2cc6d2b2010-09-24 09:55:52 +00006281out_addrlabel:
6282 ipv6_addr_label_cleanup();
6283out:
Thomas Grafc127ea22007-03-22 11:58:32 -07006284 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006285}
6286
Daniel Lezcano09f77092007-12-13 05:34:58 -08006287void addrconf_cleanup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006288{
Daniel Lezcano176c39a2009-03-03 01:06:45 -08006289 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006290 int i;
6291
6292 unregister_netdevice_notifier(&ipv6_dev_notf);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006293 unregister_pernet_subsys(&addrconf_ops);
Neil Horman2cc6d2b2010-09-24 09:55:52 +00006294 ipv6_addr_label_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006295
6296 rtnl_lock();
6297
Thomas Grafb382b192010-11-16 04:33:57 +00006298 __rtnl_af_unregister(&inet6_ops);
6299
Daniel Lezcano176c39a2009-03-03 01:06:45 -08006300 /* clean dev list */
6301 for_each_netdev(&init_net, dev) {
6302 if (__in6_dev_get(dev) == NULL)
6303 continue;
6304 addrconf_ifdown(dev, 1);
6305 }
6306 addrconf_ifdown(init_net.loopback_dev, 2);
6307
Linus Torvalds1da177e2005-04-16 15:20:36 -07006308 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006309 * Check hash table.
6310 */
stephen hemminger5c578ae2010-03-17 20:31:11 +00006311 spin_lock_bh(&addrconf_hash_lock);
stephen hemmingerc2e21292010-03-17 20:31:10 +00006312 for (i = 0; i < IN6_ADDR_HSIZE; i++)
6313 WARN_ON(!hlist_empty(&inet6_addr_lst[i]));
stephen hemminger5c578ae2010-03-17 20:31:11 +00006314 spin_unlock_bh(&addrconf_hash_lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01006315 cancel_delayed_work(&addr_chk_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006316 rtnl_unlock();
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01006317
6318 destroy_workqueue(addrconf_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006319}