blob: 6a576ff92c399fbb686c839c525e700f2579b1a9 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * IPv6 Address [auto]configuration
4 * Linux INET6 implementation
5 *
6 * Authors:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09007 * Pedro Roque <roque@di.fc.ul.pt>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
10
11/*
12 * Changes:
13 *
14 * Janos Farkas : delete timer on ifdown
15 * <chexum@bankinf.banki.hu>
16 * Andi Kleen : kill double kfree on module
17 * unload.
18 * Maciej W. Rozycki : FDDI support
19 * sekiya@USAGI : Don't send too many RS
20 * packets.
21 * yoshfuji@USAGI : Fixed interval between DAD
22 * packets.
23 * YOSHIFUJI Hideaki @USAGI : improved accuracy of
24 * address validation timer.
25 * YOSHIFUJI Hideaki @USAGI : Privacy Extensions (RFC3041)
26 * support.
27 * Yuji SEKIYA @USAGI : Don't assign a same IPv6
28 * address on a same interface.
29 * YOSHIFUJI Hideaki @USAGI : ARCnet support
30 * YOSHIFUJI Hideaki @USAGI : convert /proc/net/if_inet6 to
31 * seq_file.
YOSHIFUJI Hideakib1cacb62005-11-08 09:38:12 -080032 * YOSHIFUJI Hideaki @USAGI : improved source address
33 * selection; consider scope,
34 * status etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 */
36
Joe Perchesf3213832012-05-15 14:11:53 +000037#define pr_fmt(fmt) "IPv6: " fmt
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/errno.h>
40#include <linux/types.h>
Ilpo Järvinena0bffff2009-03-21 13:36:17 -070041#include <linux/kernel.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010042#include <linux/sched/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/socket.h>
44#include <linux/sockios.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/net.h>
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +010046#include <linux/inet.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/in6.h>
48#include <linux/netdevice.h>
Thomas Graf18237302006-08-04 23:04:54 -070049#include <linux/if_addr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <linux/if_arp.h>
51#include <linux/if_arcnet.h>
52#include <linux/if_infiniband.h>
53#include <linux/route.h>
54#include <linux/inetdevice.h>
55#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090056#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#ifdef CONFIG_SYSCTL
58#include <linux/sysctl.h>
59#endif
Randy Dunlap4fc268d2006-01-11 12:17:47 -080060#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <linux/delay.h>
62#include <linux/notifier.h>
Paulo Marques543537b2005-06-23 00:09:02 -070063#include <linux/string.h>
Eric Dumazetddbe5032012-07-18 08:11:12 +000064#include <linux/hash.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020066#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#include <net/sock.h>
68#include <net/snmp.h>
69
Alexander Aring5241c2d2015-12-14 20:55:22 +010070#include <net/6lowpan.h>
YOSHIFUJI Hideaki / 吉藤英明cb6bf352013-03-25 08:26:24 +000071#include <net/firewire.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#include <net/ipv6.h>
73#include <net/protocol.h>
74#include <net/ndisc.h>
75#include <net/ip6_route.h>
76#include <net/addrconf.h>
77#include <net/tcp.h>
78#include <net/ip.h>
Thomas Graf5d620262006-08-15 00:35:02 -070079#include <net/netlink.h>
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -070080#include <net/pkt_sched.h>
David Ahernca254492015-10-12 11:47:10 -070081#include <net/l3mdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070082#include <linux/if_tunnel.h>
83#include <linux/rtnetlink.h>
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +000084#include <linux/netconf.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070085#include <linux/random.h>
Stephen Hemmingere21e8462010-03-20 16:09:01 -070086#include <linux/uaccess.h>
Herbert Xu7f7d9a62007-04-24 21:54:09 -070087#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
89#include <linux/proc_fs.h>
90#include <linux/seq_file.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040091#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Linus Torvalds1da177e2005-04-16 15:20:36 -070093#define INFINITY_LIFE_TIME 0xFFFFFFFF
Thomas Graf18a31e12010-11-17 04:12:02 +000094
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +010095#define IPV6_MAX_STRLEN \
96 sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")
97
Thomas Graf18a31e12010-11-17 04:12:02 +000098static inline u32 cstamp_delta(unsigned long cstamp)
99{
100 return (cstamp - INITIAL_JIFFIES) * 100UL / HZ;
101}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -0700103static inline s32 rfc3315_s14_backoff_init(s32 irt)
104{
105 /* multiply 'initial retransmission time' by 0.9 .. 1.1 */
106 u64 tmp = (900000 + prandom_u32() % 200001) * (u64)irt;
107 do_div(tmp, 1000000);
108 return (s32)tmp;
109}
110
111static inline s32 rfc3315_s14_backoff_update(s32 rt, s32 mrt)
112{
113 /* multiply 'retransmission timeout' by 1.9 .. 2.1 */
114 u64 tmp = (1900000 + prandom_u32() % 200001) * (u64)rt;
115 do_div(tmp, 1000000);
116 if ((s32)tmp > mrt) {
117 /* multiply 'maximum retransmission time' by 0.9 .. 1.1 */
118 tmp = (900000 + prandom_u32() % 200001) * (u64)mrt;
119 do_div(tmp, 1000000);
120 }
121 return (s32)tmp;
122}
123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124#ifdef CONFIG_SYSCTL
WANG Conga317a2f2014-07-25 15:25:09 -0700125static int addrconf_sysctl_register(struct inet6_dev *idev);
Pavel Emelyanov408c4762008-01-10 17:41:21 -0800126static void addrconf_sysctl_unregister(struct inet6_dev *idev);
127#else
WANG Conga317a2f2014-07-25 15:25:09 -0700128static inline int addrconf_sysctl_register(struct inet6_dev *idev)
Pavel Emelyanov408c4762008-01-10 17:41:21 -0800129{
WANG Conga317a2f2014-07-25 15:25:09 -0700130 return 0;
Pavel Emelyanov408c4762008-01-10 17:41:21 -0800131}
132
133static inline void addrconf_sysctl_unregister(struct inet6_dev *idev)
134{
135}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136#endif
137
Jiri Bohac9d6280d2016-10-13 18:50:02 +0200138static void ipv6_regen_rndid(struct inet6_dev *idev);
139static void ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +0900141static int ipv6_generate_eui64(u8 *eui, struct net_device *dev);
Eric Dumazetd9bf82c2017-10-07 19:30:24 -0700142static int ipv6_count_addresses(const struct inet6_dev *idev);
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +0100143static int ipv6_generate_stable_address(struct in6_addr *addr,
144 u8 dad_count,
145 const struct inet6_dev *idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Eric Dumazet27c565a2017-11-04 08:53:27 -0700147#define IN6_ADDR_HSIZE_SHIFT 8
148#define IN6_ADDR_HSIZE (1 << IN6_ADDR_HSIZE_SHIFT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149/*
150 * Configured unicast address hash table
151 */
stephen hemmingerc2e21292010-03-17 20:31:10 +0000152static struct hlist_head inet6_addr_lst[IN6_ADDR_HSIZE];
stephen hemminger5c578aed2010-03-17 20:31:11 +0000153static DEFINE_SPINLOCK(addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100155static void addrconf_verify(void);
156static void addrconf_verify_rtnl(void);
157static void addrconf_verify_work(struct work_struct *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100159static struct workqueue_struct *addrconf_wq;
160static DECLARE_DELAYED_WORK(addr_chk_work, addrconf_verify_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
162static void addrconf_join_anycast(struct inet6_ifaddr *ifp);
163static void addrconf_leave_anycast(struct inet6_ifaddr *ifp);
164
Jiri Pirko93d9b7d2010-03-10 10:28:56 +0000165static void addrconf_type_change(struct net_device *dev,
166 unsigned long event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167static int addrconf_ifdown(struct net_device *dev, int how);
168
David Ahern8d1c8022018-04-17 17:33:26 -0700169static struct fib6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
Romain Kuntz21caa662013-01-09 21:06:03 +0000170 int plen,
171 const struct net_device *dev,
David Ahern2b2450c2019-03-27 20:53:52 -0700172 u32 flags, u32 noflags,
173 bool no_gw);
Romain Kuntz21caa662013-01-09 21:06:03 +0000174
David S. Millercf22f9a2012-04-14 21:37:40 -0400175static void addrconf_dad_start(struct inet6_ifaddr *ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100176static void addrconf_dad_work(struct work_struct *w);
David Ahernc76fe2d2018-01-25 20:16:29 -0800177static void addrconf_dad_completed(struct inet6_ifaddr *ifp, bool bump_id,
178 bool send_na);
Hangbin Liu896585d2018-11-21 21:52:33 +0800179static void addrconf_dad_run(struct inet6_dev *idev, bool restart);
Kees Cooke99e88a2017-10-16 14:43:17 -0700180static void addrconf_rs_timer(struct timer_list *t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
182static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
183
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900184static void inet6_prefix_notify(int event, struct inet6_dev *idev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 struct prefix_info *pinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Adrian Bunk888c8482008-07-22 14:21:58 -0700187static struct ipv6_devconf ipv6_devconf __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 .forwarding = 0,
189 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
190 .mtu6 = IPV6_MIN_MTU,
191 .accept_ra = 1,
192 .accept_redirects = 1,
193 .autoconf = 1,
194 .force_mld_version = 0,
Hannes Frederic Sowafc4eba52013-08-14 01:03:46 +0200195 .mldv1_unsolicited_report_interval = 10 * HZ,
196 .mldv2_unsolicited_report_interval = HZ,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 .dad_transmits = 1,
198 .rtr_solicits = MAX_RTR_SOLICITATIONS,
199 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -0700200 .rtr_solicit_max_interval = RTR_SOLICITATION_MAX_INTERVAL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
Ian Morris67ba4152014-08-24 21:53:10 +0100202 .use_tempaddr = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 .temp_valid_lft = TEMP_VALID_LIFETIME,
204 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
205 .regen_max_retry = REGEN_MAX_RETRY,
206 .max_desync_factor = MAX_DESYNC_FACTOR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 .max_addresses = IPV6_MAX_ADDRESSES,
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -0800208 .accept_ra_defrtr = 1,
Ben Greeard9333192014-06-25 14:44:53 -0700209 .accept_ra_from_local = 0,
Hangbin Liu8013d1d72015-07-30 14:28:42 +0800210 .accept_ra_min_hop_limit= 1,
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -0800211 .accept_ra_pinfo = 1,
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800212#ifdef CONFIG_IPV6_ROUTER_PREF
213 .accept_ra_rtr_pref = 1,
YOSHIFUJI Hideaki52e163562006-03-20 17:05:47 -0800214 .rtr_probe_interval = 60 * HZ,
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -0800215#ifdef CONFIG_IPV6_ROUTE_INFO
Joel Scherpelzbbea1242017-03-22 18:19:04 +0900216 .accept_ra_rt_info_min_plen = 0,
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -0800217 .accept_ra_rt_info_max_plen = 0,
218#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800219#endif
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -0700220 .proxy_ndp = 0,
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -0700221 .accept_source_route = 0, /* we do not accept RH0 by default. */
YOSHIFUJI Hideaki778d80b2008-06-28 14:17:11 +0900222 .disable_ipv6 = 0,
Nicolas Dichtel09400952017-11-14 14:21:32 +0100223 .accept_dad = 0,
Hannes Frederic Sowab800c3b2013-08-27 01:36:51 +0200224 .suppress_frag_ndisc = 1,
Harout Hedeshianc2943f12015-01-20 10:06:05 -0700225 .accept_ra_mtu = 1,
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +0100226 .stable_secret = {
227 .initialized = false,
Erik Kline3985e8a2015-07-22 16:38:25 +0900228 },
229 .use_oif_addrs_only = 0,
Andy Gospodarek35103d12015-08-13 10:39:01 -0400230 .ignore_routes_with_linkdown = 0,
David Ahernf1705ec2016-02-24 09:25:37 -0800231 .keep_addr_on_down = 0,
David Lebrun1ababeb2016-11-08 14:57:39 +0100232 .seg6_enabled = 0,
David Lebrunbf355b82016-11-08 14:57:42 +0100233#ifdef CONFIG_IPV6_SEG6_HMAC
234 .seg6_require_hmac = 0,
235#endif
Erik Nordmarkadc176c2016-12-02 14:00:08 -0800236 .enhanced_dad = 1,
Felix Jiad35a00b2017-01-26 16:59:17 +1300237 .addr_gen_mode = IN6_ADDR_GEN_MODE_EUI64,
David Forsterdf789fe2017-02-23 16:27:18 +0000238 .disable_policy = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239};
240
Brian Haleyab32ea52006-09-22 14:15:41 -0700241static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 .forwarding = 0,
243 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
244 .mtu6 = IPV6_MIN_MTU,
245 .accept_ra = 1,
246 .accept_redirects = 1,
247 .autoconf = 1,
Hannes Frederic Sowafc4eba52013-08-14 01:03:46 +0200248 .force_mld_version = 0,
249 .mldv1_unsolicited_report_interval = 10 * HZ,
250 .mldv2_unsolicited_report_interval = HZ,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 .dad_transmits = 1,
252 .rtr_solicits = MAX_RTR_SOLICITATIONS,
253 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -0700254 .rtr_solicit_max_interval = RTR_SOLICITATION_MAX_INTERVAL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 .use_tempaddr = 0,
257 .temp_valid_lft = TEMP_VALID_LIFETIME,
258 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
259 .regen_max_retry = REGEN_MAX_RETRY,
260 .max_desync_factor = MAX_DESYNC_FACTOR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 .max_addresses = IPV6_MAX_ADDRESSES,
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -0800262 .accept_ra_defrtr = 1,
Ben Greeard9333192014-06-25 14:44:53 -0700263 .accept_ra_from_local = 0,
Hangbin Liu8013d1d72015-07-30 14:28:42 +0800264 .accept_ra_min_hop_limit= 1,
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -0800265 .accept_ra_pinfo = 1,
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800266#ifdef CONFIG_IPV6_ROUTER_PREF
267 .accept_ra_rtr_pref = 1,
YOSHIFUJI Hideaki52e163562006-03-20 17:05:47 -0800268 .rtr_probe_interval = 60 * HZ,
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -0800269#ifdef CONFIG_IPV6_ROUTE_INFO
Joel Scherpelzbbea1242017-03-22 18:19:04 +0900270 .accept_ra_rt_info_min_plen = 0,
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -0800271 .accept_ra_rt_info_max_plen = 0,
272#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800273#endif
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -0700274 .proxy_ndp = 0,
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -0700275 .accept_source_route = 0, /* we do not accept RH0 by default. */
YOSHIFUJI Hideaki778d80b2008-06-28 14:17:11 +0900276 .disable_ipv6 = 0,
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +0900277 .accept_dad = 1,
Hannes Frederic Sowab800c3b2013-08-27 01:36:51 +0200278 .suppress_frag_ndisc = 1,
Harout Hedeshianc2943f12015-01-20 10:06:05 -0700279 .accept_ra_mtu = 1,
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +0100280 .stable_secret = {
281 .initialized = false,
282 },
Erik Kline3985e8a2015-07-22 16:38:25 +0900283 .use_oif_addrs_only = 0,
Andy Gospodarek35103d12015-08-13 10:39:01 -0400284 .ignore_routes_with_linkdown = 0,
David Ahernf1705ec2016-02-24 09:25:37 -0800285 .keep_addr_on_down = 0,
David Lebrun1ababeb2016-11-08 14:57:39 +0100286 .seg6_enabled = 0,
David Lebrunbf355b82016-11-08 14:57:42 +0100287#ifdef CONFIG_IPV6_SEG6_HMAC
288 .seg6_require_hmac = 0,
289#endif
Erik Nordmarkadc176c2016-12-02 14:00:08 -0800290 .enhanced_dad = 1,
Felix Jiad35a00b2017-01-26 16:59:17 +1300291 .addr_gen_mode = IN6_ADDR_GEN_MODE_EUI64,
David Forsterdf789fe2017-02-23 16:27:18 +0000292 .disable_policy = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293};
294
Mike Manning1f372c72017-09-25 22:01:36 +0100295/* Check if link is ready: is it up and is a valid qdisc available */
296static inline bool addrconf_link_ready(const struct net_device *dev)
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -0700297{
Mike Manning1f372c72017-09-25 22:01:36 +0100298 return netif_oper_up(dev) && !qdisc_tx_is_noop(dev);
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -0700299}
300
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200301static void addrconf_del_rs_timer(struct inet6_dev *idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302{
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200303 if (del_timer(&idev->rs_timer))
304 __in6_dev_put(idev);
305}
306
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100307static void addrconf_del_dad_work(struct inet6_ifaddr *ifp)
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200308{
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100309 if (cancel_delayed_work(&ifp->dad_work))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 __in6_ifa_put(ifp);
311}
312
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200313static void addrconf_mod_rs_timer(struct inet6_dev *idev,
314 unsigned long when)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200316 if (!timer_pending(&idev->rs_timer))
317 in6_dev_hold(idev);
318 mod_timer(&idev->rs_timer, jiffies + when);
319}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100321static void addrconf_mod_dad_work(struct inet6_ifaddr *ifp,
322 unsigned long delay)
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200323{
Xin Longf8a894b2017-06-15 16:33:58 +0800324 in6_ifa_hold(ifp);
325 if (mod_delayed_work(addrconf_wq, &ifp->dad_work, delay))
326 in6_ifa_put(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327}
328
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700329static int snmp6_alloc_dev(struct inet6_dev *idev)
330{
John Stultz827da442013-10-07 15:51:58 -0700331 int i;
332
WANG Cong698365f2014-05-05 15:55:55 -0700333 idev->stats.ipv6 = alloc_percpu(struct ipstats_mib);
334 if (!idev->stats.ipv6)
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700335 goto err_ip;
John Stultz827da442013-10-07 15:51:58 -0700336
337 for_each_possible_cpu(i) {
338 struct ipstats_mib *addrconf_stats;
WANG Cong698365f2014-05-05 15:55:55 -0700339 addrconf_stats = per_cpu_ptr(idev->stats.ipv6, i);
John Stultz827da442013-10-07 15:51:58 -0700340 u64_stats_init(&addrconf_stats->syncp);
John Stultz827da442013-10-07 15:51:58 -0700341 }
342
343
Eric Dumazetbe281e52011-05-19 01:14:23 +0000344 idev->stats.icmpv6dev = kzalloc(sizeof(struct icmpv6_mib_device),
345 GFP_KERNEL);
346 if (!idev->stats.icmpv6dev)
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700347 goto err_icmp;
Eric Dumazetbe281e52011-05-19 01:14:23 +0000348 idev->stats.icmpv6msgdev = kzalloc(sizeof(struct icmpv6msg_mib_device),
349 GFP_KERNEL);
350 if (!idev->stats.icmpv6msgdev)
David L Stevens14878f72007-09-16 16:52:35 -0700351 goto err_icmpmsg;
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700352
353 return 0;
354
David L Stevens14878f72007-09-16 16:52:35 -0700355err_icmpmsg:
Eric Dumazetbe281e52011-05-19 01:14:23 +0000356 kfree(idev->stats.icmpv6dev);
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700357err_icmp:
WANG Cong698365f2014-05-05 15:55:55 -0700358 free_percpu(idev->stats.ipv6);
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700359err_ip:
Pavel Emelyanovaaf70ec2007-10-17 21:25:32 -0700360 return -ENOMEM;
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700361}
362
Eldad Zack8e5e8f32012-04-01 07:49:08 +0000363static struct inet6_dev *ipv6_add_dev(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364{
365 struct inet6_dev *ndev;
WANG Conga317a2f2014-07-25 15:25:09 -0700366 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
368 ASSERT_RTNL();
369
370 if (dev->mtu < IPV6_MIN_MTU)
WANG Conga317a2f2014-07-25 15:25:09 -0700371 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900373 ndev = kzalloc(sizeof(struct inet6_dev), GFP_KERNEL);
Ian Morris63159f22015-03-29 14:00:04 +0100374 if (!ndev)
WANG Conga317a2f2014-07-25 15:25:09 -0700375 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Ingo Oeser322f74a2006-03-20 23:01:47 -0800377 rwlock_init(&ndev->lock);
378 ndev->dev = dev;
stephen hemminger502a2ff2010-03-17 20:31:13 +0000379 INIT_LIST_HEAD(&ndev->addr_list);
Kees Cooke99e88a2017-10-16 14:43:17 -0700380 timer_setup(&ndev->rs_timer, addrconf_rs_timer, 0);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900381 memcpy(&ndev->cnf, dev_net(dev)->ipv6.devconf_dflt, sizeof(ndev->cnf));
Hannes Frederic Sowa9b29c692015-12-15 22:59:12 +0100382
383 if (ndev->cnf.stable_secret.initialized)
Felix Jiad35a00b2017-01-26 16:59:17 +1300384 ndev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
Hannes Frederic Sowa9b29c692015-12-15 22:59:12 +0100385
Ingo Oeser322f74a2006-03-20 23:01:47 -0800386 ndev->cnf.mtu6 = dev->mtu;
Ingo Oeser322f74a2006-03-20 23:01:47 -0800387 ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl);
Ian Morris63159f22015-03-29 14:00:04 +0100388 if (!ndev->nd_parms) {
Ingo Oeser322f74a2006-03-20 23:01:47 -0800389 kfree(ndev);
WANG Conga317a2f2014-07-25 15:25:09 -0700390 return ERR_PTR(err);
Ingo Oeser322f74a2006-03-20 23:01:47 -0800391 }
Ben Hutchings0187bdf2008-06-19 16:15:47 -0700392 if (ndev->cnf.forwarding)
393 dev_disable_lro(dev);
Ingo Oeser322f74a2006-03-20 23:01:47 -0800394 /* We refer to the device */
395 dev_hold(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
Ingo Oeser322f74a2006-03-20 23:01:47 -0800397 if (snmp6_alloc_dev(ndev) < 0) {
Joe Perchese32ac252018-03-26 08:35:01 -0700398 netdev_dbg(dev, "%s: cannot allocate memory for statistics\n",
399 __func__);
Ingo Oeser322f74a2006-03-20 23:01:47 -0800400 neigh_parms_release(&nd_tbl, ndev->nd_parms);
Roy Li8603e332011-09-20 15:10:16 -0400401 dev_put(dev);
402 kfree(ndev);
WANG Conga317a2f2014-07-25 15:25:09 -0700403 return ERR_PTR(err);
Ingo Oeser322f74a2006-03-20 23:01:47 -0800404 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Ingo Oeser322f74a2006-03-20 23:01:47 -0800406 if (snmp6_register_dev(ndev) < 0) {
Joe Perchese32ac252018-03-26 08:35:01 -0700407 netdev_dbg(dev, "%s: cannot create /proc/net/dev_snmp6/%s\n",
408 __func__, dev->name);
WANG Conga317a2f2014-07-25 15:25:09 -0700409 goto err_release;
Ingo Oeser322f74a2006-03-20 23:01:47 -0800410 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
Jiri Bohac9d6280d2016-10-13 18:50:02 +0200412 /* One reference from device. */
Reshetova, Elena1be92462017-07-04 09:34:55 +0300413 refcount_set(&ndev->refcnt, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +0900415 if (dev->flags & (IFF_NOARP | IFF_LOOPBACK))
416 ndev->cnf.accept_dad = -1;
417
Amerigo Wang07a93622012-10-29 16:23:10 +0000418#if IS_ENABLED(CONFIG_IPV6_SIT)
YOSHIFUJI Hideakib077d7a2008-04-13 23:42:18 -0700419 if (dev->type == ARPHRD_SIT && (dev->priv_flags & IFF_ISATAP)) {
Joe Perchesf3213832012-05-15 14:11:53 +0000420 pr_info("%s: Disabled Multicast RS\n", dev->name);
YOSHIFUJI Hideakib077d7a2008-04-13 23:42:18 -0700421 ndev->cnf.rtr_solicits = 0;
422 }
423#endif
424
stephen hemminger372e6c82010-03-17 20:31:09 +0000425 INIT_LIST_HEAD(&ndev->tempaddr_list);
Jiri Bohac76506a92016-10-13 18:52:15 +0200426 ndev->desync_factor = U32_MAX;
Ingo Oeser322f74a2006-03-20 23:01:47 -0800427 if ((dev->flags&IFF_LOOPBACK) ||
428 dev->type == ARPHRD_TUNNEL ||
YOSHIFUJI Hideaki9625ed72008-04-13 23:47:11 -0700429 dev->type == ARPHRD_TUNNEL6 ||
Joerg Roedel0be669b2006-10-10 14:49:53 -0700430 dev->type == ARPHRD_SIT ||
Joerg Roedel0be669b2006-10-10 14:49:53 -0700431 dev->type == ARPHRD_NONE) {
Ingo Oeser322f74a2006-03-20 23:01:47 -0800432 ndev->cnf.use_tempaddr = -1;
Jiri Bohac9d6280d2016-10-13 18:50:02 +0200433 } else
434 ipv6_regen_rndid(ndev);
David S. Miller5d9efa72013-10-28 20:07:50 -0400435
Daniel Borkmann914faa12013-04-09 03:47:14 +0000436 ndev->token = in6addr_any;
Ingo Oeser322f74a2006-03-20 23:01:47 -0800437
Mike Manning1f372c72017-09-25 22:01:36 +0100438 if (netif_running(dev) && addrconf_link_ready(dev))
Herbert Xu53aadcc2007-03-27 14:31:52 -0700439 ndev->if_flags |= IF_READY;
440
Ingo Oeser322f74a2006-03-20 23:01:47 -0800441 ipv6_mc_init_dev(ndev);
442 ndev->tstamp = jiffies;
WANG Conga317a2f2014-07-25 15:25:09 -0700443 err = addrconf_sysctl_register(ndev);
444 if (err) {
445 ipv6_mc_destroy_dev(ndev);
Sabrina Dubroca2a189f9e2015-11-04 14:47:53 +0100446 snmp6_unregister_dev(ndev);
WANG Conga317a2f2014-07-25 15:25:09 -0700447 goto err_release;
448 }
David L Stevens30c4cf52007-01-04 12:31:14 -0800449 /* protected by rtnl_lock */
Eric Dumazetcf778b02012-01-12 04:41:32 +0000450 rcu_assign_pointer(dev->ip6_ptr, ndev);
YOSHIFUJI Hideakid88ae4c2007-01-14 21:48:40 -0800451
Hannes Frederic Sowa2c5e8932013-02-10 03:50:18 +0000452 /* Join interface-local all-node multicast group */
453 ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allnodes);
454
YOSHIFUJI Hideakid88ae4c2007-01-14 21:48:40 -0800455 /* Join all-node multicast group */
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +0900456 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allnodes);
YOSHIFUJI Hideakid88ae4c2007-01-14 21:48:40 -0800457
Li Weid6ddef92012-03-05 14:45:17 +0000458 /* Join all-router multicast group if forwarding is set */
Li Wei8b2aaed2012-03-07 14:58:07 +0000459 if (ndev->cnf.forwarding && (dev->flags & IFF_MULTICAST))
Li Weid6ddef92012-03-05 14:45:17 +0000460 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
461
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 return ndev;
WANG Conga317a2f2014-07-25 15:25:09 -0700463
464err_release:
465 neigh_parms_release(&nd_tbl, ndev->nd_parms);
466 ndev->dead = 1;
467 in6_dev_finish_destroy(ndev);
468 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469}
470
Eldad Zack8e5e8f32012-04-01 07:49:08 +0000471static struct inet6_dev *ipv6_find_idev(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472{
473 struct inet6_dev *idev;
474
475 ASSERT_RTNL();
476
Stephen Hemmingere21e8462010-03-20 16:09:01 -0700477 idev = __in6_dev_get(dev);
478 if (!idev) {
479 idev = ipv6_add_dev(dev);
WANG Conga317a2f2014-07-25 15:25:09 -0700480 if (IS_ERR(idev))
Sabrina Dubrocadb0b99f2019-08-23 15:44:36 +0200481 return idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 }
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +0900483
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 if (dev->flags&IFF_UP)
485 ipv6_mc_up(idev);
486 return idev;
487}
488
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000489static int inet6_netconf_msgsize_devconf(int type)
490{
491 int size = NLMSG_ALIGN(sizeof(struct netconfmsg))
492 + nla_total_size(4); /* NETCONFA_IFINDEX */
Zhang Shengju136ba622016-03-10 08:55:50 +0000493 bool all = false;
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000494
Zhang Shengju136ba622016-03-10 08:55:50 +0000495 if (type == NETCONFA_ALL)
496 all = true;
497
498 if (all || type == NETCONFA_FORWARDING)
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000499 size += nla_total_size(4);
David S. Millerb1afce92012-12-04 14:46:34 -0500500#ifdef CONFIG_IPV6_MROUTE
Zhang Shengju136ba622016-03-10 08:55:50 +0000501 if (all || type == NETCONFA_MC_FORWARDING)
Nicolas Dichteld67b8c62012-12-04 01:13:35 +0000502 size += nla_total_size(4);
David S. Millerb1afce92012-12-04 14:46:34 -0500503#endif
Zhang Shengju136ba622016-03-10 08:55:50 +0000504 if (all || type == NETCONFA_PROXY_NEIGH)
stephen hemmingerc92d5492013-12-17 22:37:14 -0800505 size += nla_total_size(4);
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000506
Zhang Shengju136ba622016-03-10 08:55:50 +0000507 if (all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN)
Andy Gospodarek35103d12015-08-13 10:39:01 -0400508 size += nla_total_size(4);
509
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000510 return size;
511}
512
513static int inet6_netconf_fill_devconf(struct sk_buff *skb, int ifindex,
514 struct ipv6_devconf *devconf, u32 portid,
515 u32 seq, int event, unsigned int flags,
516 int type)
517{
518 struct nlmsghdr *nlh;
519 struct netconfmsg *ncm;
Zhang Shengju136ba622016-03-10 08:55:50 +0000520 bool all = false;
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000521
522 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct netconfmsg),
523 flags);
Ian Morris63159f22015-03-29 14:00:04 +0100524 if (!nlh)
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000525 return -EMSGSIZE;
526
Zhang Shengju136ba622016-03-10 08:55:50 +0000527 if (type == NETCONFA_ALL)
528 all = true;
529
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000530 ncm = nlmsg_data(nlh);
531 ncm->ncm_family = AF_INET6;
532
533 if (nla_put_s32(skb, NETCONFA_IFINDEX, ifindex) < 0)
534 goto nla_put_failure;
535
David Ahern23452172017-03-28 14:28:05 -0700536 if (!devconf)
537 goto out;
538
Zhang Shengju136ba622016-03-10 08:55:50 +0000539 if ((all || type == NETCONFA_FORWARDING) &&
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000540 nla_put_s32(skb, NETCONFA_FORWARDING, devconf->forwarding) < 0)
541 goto nla_put_failure;
David S. Millerb1afce92012-12-04 14:46:34 -0500542#ifdef CONFIG_IPV6_MROUTE
Zhang Shengju136ba622016-03-10 08:55:50 +0000543 if ((all || type == NETCONFA_MC_FORWARDING) &&
Nicolas Dichteld67b8c62012-12-04 01:13:35 +0000544 nla_put_s32(skb, NETCONFA_MC_FORWARDING,
545 devconf->mc_forwarding) < 0)
546 goto nla_put_failure;
David S. Millerb1afce92012-12-04 14:46:34 -0500547#endif
Zhang Shengju136ba622016-03-10 08:55:50 +0000548 if ((all || type == NETCONFA_PROXY_NEIGH) &&
stephen hemmingerc92d5492013-12-17 22:37:14 -0800549 nla_put_s32(skb, NETCONFA_PROXY_NEIGH, devconf->proxy_ndp) < 0)
550 goto nla_put_failure;
551
Zhang Shengju136ba622016-03-10 08:55:50 +0000552 if ((all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN) &&
Andy Gospodarek35103d12015-08-13 10:39:01 -0400553 nla_put_s32(skb, NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
554 devconf->ignore_routes_with_linkdown) < 0)
555 goto nla_put_failure;
556
David Ahern23452172017-03-28 14:28:05 -0700557out:
Johannes Berg053c0952015-01-16 22:09:00 +0100558 nlmsg_end(skb, nlh);
559 return 0;
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000560
561nla_put_failure:
562 nlmsg_cancel(skb, nlh);
563 return -EMSGSIZE;
564}
565
David Ahern85b3daa2017-03-28 14:28:04 -0700566void inet6_netconf_notify_devconf(struct net *net, int event, int type,
567 int ifindex, struct ipv6_devconf *devconf)
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000568{
569 struct sk_buff *skb;
570 int err = -ENOBUFS;
571
Eric Dumazet927265b2016-07-08 05:46:04 +0200572 skb = nlmsg_new(inet6_netconf_msgsize_devconf(type), GFP_KERNEL);
Ian Morris63159f22015-03-29 14:00:04 +0100573 if (!skb)
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000574 goto errout;
575
576 err = inet6_netconf_fill_devconf(skb, ifindex, devconf, 0, 0,
David Ahern85b3daa2017-03-28 14:28:04 -0700577 event, 0, type);
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000578 if (err < 0) {
579 /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
580 WARN_ON(err == -EMSGSIZE);
581 kfree_skb(skb);
582 goto errout;
583 }
Eric Dumazet927265b2016-07-08 05:46:04 +0200584 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_NETCONF, NULL, GFP_KERNEL);
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000585 return;
586errout:
Cong Dingbd779022012-12-18 12:08:56 +0000587 rtnl_set_sk_err(net, RTNLGRP_IPV6_NETCONF, err);
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000588}
589
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000590static const struct nla_policy devconf_ipv6_policy[NETCONFA_MAX+1] = {
591 [NETCONFA_IFINDEX] = { .len = sizeof(int) },
592 [NETCONFA_FORWARDING] = { .len = sizeof(int) },
stephen hemmingerc92d5492013-12-17 22:37:14 -0800593 [NETCONFA_PROXY_NEIGH] = { .len = sizeof(int) },
Andy Gospodarek35103d12015-08-13 10:39:01 -0400594 [NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN] = { .len = sizeof(int) },
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000595};
596
Jakub Kicinski38d51812019-01-18 10:46:22 -0800597static int inet6_netconf_valid_get_req(struct sk_buff *skb,
598 const struct nlmsghdr *nlh,
599 struct nlattr **tb,
600 struct netlink_ext_ack *extack)
601{
602 int i, err;
603
604 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(struct netconfmsg))) {
605 NL_SET_ERR_MSG_MOD(extack, "Invalid header for netconf get request");
606 return -EINVAL;
607 }
608
609 if (!netlink_strict_get_check(skb))
Johannes Berg8cb08172019-04-26 14:07:28 +0200610 return nlmsg_parse_deprecated(nlh, sizeof(struct netconfmsg),
611 tb, NETCONFA_MAX,
612 devconf_ipv6_policy, extack);
Jakub Kicinski38d51812019-01-18 10:46:22 -0800613
Johannes Berg8cb08172019-04-26 14:07:28 +0200614 err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct netconfmsg),
615 tb, NETCONFA_MAX,
616 devconf_ipv6_policy, extack);
Jakub Kicinski38d51812019-01-18 10:46:22 -0800617 if (err)
618 return err;
619
620 for (i = 0; i <= NETCONFA_MAX; i++) {
621 if (!tb[i])
622 continue;
623
624 switch (i) {
625 case NETCONFA_IFINDEX:
626 break;
627 default:
628 NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in netconf get request");
629 return -EINVAL;
630 }
631 }
632
633 return 0;
634}
635
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000636static int inet6_netconf_get_devconf(struct sk_buff *in_skb,
David Ahernc21ef3e2017-04-16 09:48:24 -0700637 struct nlmsghdr *nlh,
638 struct netlink_ext_ack *extack)
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000639{
640 struct net *net = sock_net(in_skb->sk);
641 struct nlattr *tb[NETCONFA_MAX+1];
Florian Westphal4ea26072017-10-11 10:28:00 +0200642 struct inet6_dev *in6_dev = NULL;
643 struct net_device *dev = NULL;
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000644 struct sk_buff *skb;
645 struct ipv6_devconf *devconf;
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000646 int ifindex;
647 int err;
648
Jakub Kicinski38d51812019-01-18 10:46:22 -0800649 err = inet6_netconf_valid_get_req(in_skb, nlh, tb, extack);
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000650 if (err < 0)
Florian Westphal4ea26072017-10-11 10:28:00 +0200651 return err;
652
653 if (!tb[NETCONFA_IFINDEX])
654 return -EINVAL;
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000655
Anton Protopopova97eb332016-02-16 21:43:16 -0500656 err = -EINVAL;
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000657 ifindex = nla_get_s32(tb[NETCONFA_IFINDEX]);
658 switch (ifindex) {
659 case NETCONFA_IFINDEX_ALL:
660 devconf = net->ipv6.devconf_all;
661 break;
662 case NETCONFA_IFINDEX_DEFAULT:
663 devconf = net->ipv6.devconf_dflt;
664 break;
665 default:
Florian Westphal4ea26072017-10-11 10:28:00 +0200666 dev = dev_get_by_index(net, ifindex);
Ian Morris63159f22015-03-29 14:00:04 +0100667 if (!dev)
Florian Westphal4ea26072017-10-11 10:28:00 +0200668 return -EINVAL;
669 in6_dev = in6_dev_get(dev);
Ian Morris63159f22015-03-29 14:00:04 +0100670 if (!in6_dev)
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000671 goto errout;
672 devconf = &in6_dev->cnf;
673 break;
674 }
675
676 err = -ENOBUFS;
Florian Westphal4ea26072017-10-11 10:28:00 +0200677 skb = nlmsg_new(inet6_netconf_msgsize_devconf(NETCONFA_ALL), GFP_KERNEL);
Ian Morris63159f22015-03-29 14:00:04 +0100678 if (!skb)
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000679 goto errout;
680
681 err = inet6_netconf_fill_devconf(skb, ifindex, devconf,
682 NETLINK_CB(in_skb).portid,
683 nlh->nlmsg_seq, RTM_NEWNETCONF, 0,
Zhang Shengju136ba622016-03-10 08:55:50 +0000684 NETCONFA_ALL);
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000685 if (err < 0) {
686 /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
687 WARN_ON(err == -EMSGSIZE);
688 kfree_skb(skb);
689 goto errout;
690 }
691 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
692errout:
Florian Westphal4ea26072017-10-11 10:28:00 +0200693 if (in6_dev)
694 in6_dev_put(in6_dev);
695 if (dev)
696 dev_put(dev);
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000697 return err;
698}
699
Nicolas Dichtel7a674202013-03-05 23:42:06 +0000700static int inet6_netconf_dump_devconf(struct sk_buff *skb,
701 struct netlink_callback *cb)
702{
David Ahernaddd3832018-10-07 20:16:41 -0700703 const struct nlmsghdr *nlh = cb->nlh;
Nicolas Dichtel7a674202013-03-05 23:42:06 +0000704 struct net *net = sock_net(skb->sk);
705 int h, s_h;
706 int idx, s_idx;
707 struct net_device *dev;
708 struct inet6_dev *idev;
709 struct hlist_head *head;
710
David Ahernaddd3832018-10-07 20:16:41 -0700711 if (cb->strict_check) {
712 struct netlink_ext_ack *extack = cb->extack;
713 struct netconfmsg *ncm;
714
715 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ncm))) {
716 NL_SET_ERR_MSG_MOD(extack, "Invalid header for netconf dump request");
717 return -EINVAL;
718 }
719
720 if (nlmsg_attrlen(nlh, sizeof(*ncm))) {
721 NL_SET_ERR_MSG_MOD(extack, "Invalid data after header in netconf dump request");
722 return -EINVAL;
723 }
724 }
725
Nicolas Dichtel7a674202013-03-05 23:42:06 +0000726 s_h = cb->args[0];
727 s_idx = idx = cb->args[1];
728
729 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
730 idx = 0;
731 head = &net->dev_index_head[h];
732 rcu_read_lock();
Nicolas Dichtel63998ac2013-03-22 06:28:43 +0000733 cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^
734 net->dev_base_seq;
Nicolas Dichtel7a674202013-03-05 23:42:06 +0000735 hlist_for_each_entry_rcu(dev, head, index_hlist) {
736 if (idx < s_idx)
737 goto cont;
738 idev = __in6_dev_get(dev);
739 if (!idev)
740 goto cont;
741
742 if (inet6_netconf_fill_devconf(skb, dev->ifindex,
743 &idev->cnf,
744 NETLINK_CB(cb->skb).portid,
David Ahernaddd3832018-10-07 20:16:41 -0700745 nlh->nlmsg_seq,
Nicolas Dichtel7a674202013-03-05 23:42:06 +0000746 RTM_NEWNETCONF,
747 NLM_F_MULTI,
Zhang Shengju136ba622016-03-10 08:55:50 +0000748 NETCONFA_ALL) < 0) {
Nicolas Dichtel7a674202013-03-05 23:42:06 +0000749 rcu_read_unlock();
750 goto done;
751 }
Nicolas Dichtel63998ac2013-03-22 06:28:43 +0000752 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
Nicolas Dichtel7a674202013-03-05 23:42:06 +0000753cont:
754 idx++;
755 }
756 rcu_read_unlock();
757 }
758 if (h == NETDEV_HASHENTRIES) {
759 if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_ALL,
760 net->ipv6.devconf_all,
761 NETLINK_CB(cb->skb).portid,
David Ahernaddd3832018-10-07 20:16:41 -0700762 nlh->nlmsg_seq,
Nicolas Dichtel7a674202013-03-05 23:42:06 +0000763 RTM_NEWNETCONF, NLM_F_MULTI,
Zhang Shengju136ba622016-03-10 08:55:50 +0000764 NETCONFA_ALL) < 0)
Nicolas Dichtel7a674202013-03-05 23:42:06 +0000765 goto done;
766 else
767 h++;
768 }
769 if (h == NETDEV_HASHENTRIES + 1) {
770 if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_DEFAULT,
771 net->ipv6.devconf_dflt,
772 NETLINK_CB(cb->skb).portid,
David Ahernaddd3832018-10-07 20:16:41 -0700773 nlh->nlmsg_seq,
Nicolas Dichtel7a674202013-03-05 23:42:06 +0000774 RTM_NEWNETCONF, NLM_F_MULTI,
Zhang Shengju136ba622016-03-10 08:55:50 +0000775 NETCONFA_ALL) < 0)
Nicolas Dichtel7a674202013-03-05 23:42:06 +0000776 goto done;
777 else
778 h++;
779 }
780done:
781 cb->args[0] = h;
782 cb->args[1] = idx;
783
784 return skb->len;
785}
786
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787#ifdef CONFIG_SYSCTL
788static void dev_forward_change(struct inet6_dev *idev)
789{
790 struct net_device *dev;
791 struct inet6_ifaddr *ifa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
793 if (!idev)
794 return;
795 dev = idev->dev;
Ben Hutchings0187bdf2008-06-19 16:15:47 -0700796 if (idev->cnf.forwarding)
797 dev_disable_lro(dev);
Amerigo Wang1a940832012-10-28 17:43:53 +0000798 if (dev->flags & IFF_MULTICAST) {
Hannes Frederic Sowa2c5e8932013-02-10 03:50:18 +0000799 if (idev->cnf.forwarding) {
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +0900800 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
Hannes Frederic Sowa2c5e8932013-02-10 03:50:18 +0000801 ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allrouters);
802 ipv6_dev_mc_inc(dev, &in6addr_sitelocal_allrouters);
803 } else {
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +0900804 ipv6_dev_mc_dec(dev, &in6addr_linklocal_allrouters);
Hannes Frederic Sowa2c5e8932013-02-10 03:50:18 +0000805 ipv6_dev_mc_dec(dev, &in6addr_interfacelocal_allrouters);
806 ipv6_dev_mc_dec(dev, &in6addr_sitelocal_allrouters);
807 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 }
stephen hemminger502a2ff2010-03-17 20:31:13 +0000809
810 list_for_each_entry(ifa, &idev->addr_list, if_list) {
Michal Wrobel2c12a742007-02-26 15:36:10 -0800811 if (ifa->flags&IFA_F_TENTATIVE)
812 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 if (idev->cnf.forwarding)
814 addrconf_join_anycast(ifa);
815 else
816 addrconf_leave_anycast(ifa);
817 }
David Ahern85b3daa2017-03-28 14:28:04 -0700818 inet6_netconf_notify_devconf(dev_net(dev), RTM_NEWNETCONF,
819 NETCONFA_FORWARDING,
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000820 dev->ifindex, &idev->cnf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821}
822
823
Pavel Emelyanove1869322008-01-10 17:43:50 -0800824static void addrconf_forward_change(struct net *net, __s32 newf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825{
826 struct net_device *dev;
827 struct inet6_dev *idev;
828
Ben Hutchings4acd4942012-08-14 08:54:51 +0000829 for_each_netdev(net, dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 idev = __in6_dev_get(dev);
831 if (idev) {
Pavel Emelyanove1869322008-01-10 17:43:50 -0800832 int changed = (!idev->cnf.forwarding) ^ (!newf);
833 idev->cnf.forwarding = newf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 if (changed)
835 dev_forward_change(idev);
836 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838}
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800839
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000840static int addrconf_fixup_forwarding(struct ctl_table *table, int *p, int newf)
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800841{
Pavel Emelyanovbff16c22008-01-10 17:42:13 -0800842 struct net *net;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000843 int old;
844
845 if (!rtnl_trylock())
846 return restart_syscall();
Pavel Emelyanovbff16c22008-01-10 17:42:13 -0800847
848 net = (struct net *)table->extra2;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000849 old = *p;
850 *p = newf;
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800851
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000852 if (p == &net->ipv6.devconf_dflt->forwarding) {
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000853 if ((!newf) ^ (!old))
David Ahern85b3daa2017-03-28 14:28:04 -0700854 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
855 NETCONFA_FORWARDING,
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000856 NETCONFA_IFINDEX_DEFAULT,
857 net->ipv6.devconf_dflt);
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000858 rtnl_unlock();
859 return 0;
Eric W. Biederman88af1822010-02-19 13:22:59 +0000860 }
Stephen Hemmingerb325fdd2009-02-26 06:55:31 +0000861
Pavel Emelyanove1869322008-01-10 17:43:50 -0800862 if (p == &net->ipv6.devconf_all->forwarding) {
Nicolas Dichteld26c6382016-08-30 10:09:21 +0200863 int old_dflt = net->ipv6.devconf_dflt->forwarding;
864
Pavel Emelyanove1869322008-01-10 17:43:50 -0800865 net->ipv6.devconf_dflt->forwarding = newf;
Nicolas Dichteld26c6382016-08-30 10:09:21 +0200866 if ((!newf) ^ (!old_dflt))
David Ahern85b3daa2017-03-28 14:28:04 -0700867 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
868 NETCONFA_FORWARDING,
Nicolas Dichteld26c6382016-08-30 10:09:21 +0200869 NETCONFA_IFINDEX_DEFAULT,
870 net->ipv6.devconf_dflt);
871
Pavel Emelyanove1869322008-01-10 17:43:50 -0800872 addrconf_forward_change(net, newf);
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000873 if ((!newf) ^ (!old))
David Ahern85b3daa2017-03-28 14:28:04 -0700874 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
875 NETCONFA_FORWARDING,
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000876 NETCONFA_IFINDEX_ALL,
877 net->ipv6.devconf_all);
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000878 } else if ((!newf) ^ (!old))
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800879 dev_forward_change((struct inet6_dev *)table->extra1);
Ben Hutchings0187bdf2008-06-19 16:15:47 -0700880 rtnl_unlock();
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800881
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000882 if (newf)
Daniel Lezcano7b4da532008-03-04 13:47:14 -0800883 rt6_purge_dflt_routers(net);
Stephen Hemmingerb325fdd2009-02-26 06:55:31 +0000884 return 1;
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800885}
Andy Gospodarek35103d12015-08-13 10:39:01 -0400886
887static void addrconf_linkdown_change(struct net *net, __s32 newf)
888{
889 struct net_device *dev;
890 struct inet6_dev *idev;
891
892 for_each_netdev(net, dev) {
893 idev = __in6_dev_get(dev);
894 if (idev) {
895 int changed = (!idev->cnf.ignore_routes_with_linkdown) ^ (!newf);
896
897 idev->cnf.ignore_routes_with_linkdown = newf;
898 if (changed)
899 inet6_netconf_notify_devconf(dev_net(dev),
David Ahern85b3daa2017-03-28 14:28:04 -0700900 RTM_NEWNETCONF,
Andy Gospodarek35103d12015-08-13 10:39:01 -0400901 NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
902 dev->ifindex,
903 &idev->cnf);
904 }
905 }
906}
907
908static int addrconf_fixup_linkdown(struct ctl_table *table, int *p, int newf)
909{
910 struct net *net;
911 int old;
912
913 if (!rtnl_trylock())
914 return restart_syscall();
915
916 net = (struct net *)table->extra2;
917 old = *p;
918 *p = newf;
919
920 if (p == &net->ipv6.devconf_dflt->ignore_routes_with_linkdown) {
921 if ((!newf) ^ (!old))
922 inet6_netconf_notify_devconf(net,
David Ahern85b3daa2017-03-28 14:28:04 -0700923 RTM_NEWNETCONF,
Andy Gospodarek35103d12015-08-13 10:39:01 -0400924 NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
925 NETCONFA_IFINDEX_DEFAULT,
926 net->ipv6.devconf_dflt);
927 rtnl_unlock();
928 return 0;
929 }
930
931 if (p == &net->ipv6.devconf_all->ignore_routes_with_linkdown) {
932 net->ipv6.devconf_dflt->ignore_routes_with_linkdown = newf;
933 addrconf_linkdown_change(net, newf);
934 if ((!newf) ^ (!old))
935 inet6_netconf_notify_devconf(net,
David Ahern85b3daa2017-03-28 14:28:04 -0700936 RTM_NEWNETCONF,
Andy Gospodarek35103d12015-08-13 10:39:01 -0400937 NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
938 NETCONFA_IFINDEX_ALL,
939 net->ipv6.devconf_all);
940 }
941 rtnl_unlock();
942
943 return 1;
944}
945
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946#endif
947
stephen hemminger5c578aed2010-03-17 20:31:11 +0000948/* Nobody refers to this ifaddr, destroy it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp)
950{
stephen hemmingerc2e21292010-03-17 20:31:10 +0000951 WARN_ON(!hlist_unhashed(&ifp->addr_lst));
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700952
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953#ifdef NET_REFCNT_DEBUG
Joe Perches91df42b2012-05-15 14:11:54 +0000954 pr_debug("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955#endif
956
957 in6_dev_put(ifp->idev);
958
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100959 if (cancel_delayed_work(&ifp->dad_work))
960 pr_notice("delayed DAD work was pending while freeing ifa=%p\n",
961 ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
Herbert Xue9d3e082010-05-18 15:36:06 -0700963 if (ifp->state != INET6_IFADDR_STATE_DEAD) {
Joe Perchesf3213832012-05-15 14:11:53 +0000964 pr_warn("Freeing alive inet6 address %p\n", ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 return;
966 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
Lai Jiangshane5785982011-03-15 18:00:14 +0800968 kfree_rcu(ifp, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969}
970
Brian Haleye55ffac2006-07-10 15:25:51 -0700971static void
972ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp)
973{
stephen hemminger502a2ff2010-03-17 20:31:13 +0000974 struct list_head *p;
YOSHIFUJI Hideaki8a6ce0c2006-07-11 13:05:30 -0700975 int ifp_scope = ipv6_addr_src_scope(&ifp->addr);
Brian Haleye55ffac2006-07-10 15:25:51 -0700976
977 /*
978 * Each device address list is sorted in order of scope -
979 * global before linklocal.
980 */
stephen hemminger502a2ff2010-03-17 20:31:13 +0000981 list_for_each(p, &idev->addr_list) {
982 struct inet6_ifaddr *ifa
983 = list_entry(p, struct inet6_ifaddr, if_list);
YOSHIFUJI Hideaki8a6ce0c2006-07-11 13:05:30 -0700984 if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr))
Brian Haleye55ffac2006-07-10 15:25:51 -0700985 break;
986 }
987
Eric Dumazet8ef802a2017-10-07 19:30:23 -0700988 list_add_tail_rcu(&ifp->if_list, p);
Brian Haleye55ffac2006-07-10 15:25:51 -0700989}
990
Eric Dumazet3f27fb22017-10-23 16:17:47 -0700991static u32 inet6_addr_hash(const struct net *net, const struct in6_addr *addr)
YOSHIFUJI Hideaki3eb84f42008-04-10 15:42:08 +0900992{
Eric Dumazet3f27fb22017-10-23 16:17:47 -0700993 u32 val = ipv6_addr_hash(addr) ^ net_hash_mix(net);
994
995 return hash_32(val, IN6_ADDR_HSIZE_SHIFT);
YOSHIFUJI Hideaki3eb84f42008-04-10 15:42:08 +0900996}
997
Eric Dumazet56fc7092017-10-23 16:17:45 -0700998static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
Eric Dumazet752a9292017-10-23 16:17:46 -0700999 struct net_device *dev, unsigned int hash)
Eric Dumazet56fc7092017-10-23 16:17:45 -07001000{
Eric Dumazet56fc7092017-10-23 16:17:45 -07001001 struct inet6_ifaddr *ifp;
1002
1003 hlist_for_each_entry(ifp, &inet6_addr_lst[hash], addr_lst) {
1004 if (!net_eq(dev_net(ifp->idev->dev), net))
1005 continue;
1006 if (ipv6_addr_equal(&ifp->addr, addr)) {
1007 if (!dev || ifp->idev->dev == dev)
1008 return true;
1009 }
1010 }
1011 return false;
1012}
1013
David Ahernf3d98322017-10-18 09:56:52 -07001014static int ipv6_add_addr_hash(struct net_device *dev, struct inet6_ifaddr *ifa)
1015{
Eric Dumazet3f27fb22017-10-23 16:17:47 -07001016 unsigned int hash = inet6_addr_hash(dev_net(dev), &ifa->addr);
David Ahernf3d98322017-10-18 09:56:52 -07001017 int err = 0;
1018
1019 spin_lock(&addrconf_hash_lock);
1020
1021 /* Ignore adding duplicate addresses on an interface */
Eric Dumazet752a9292017-10-23 16:17:46 -07001022 if (ipv6_chk_same_addr(dev_net(dev), &ifa->addr, dev, hash)) {
Joe Perchese32ac252018-03-26 08:35:01 -07001023 netdev_dbg(dev, "ipv6_add_addr: already assigned\n");
David Ahernf3d98322017-10-18 09:56:52 -07001024 err = -EEXIST;
Eric Dumazet752a9292017-10-23 16:17:46 -07001025 } else {
1026 hlist_add_head_rcu(&ifa->addr_lst, &inet6_addr_lst[hash]);
David Ahernf3d98322017-10-18 09:56:52 -07001027 }
1028
David Ahernf3d98322017-10-18 09:56:52 -07001029 spin_unlock(&addrconf_hash_lock);
1030
1031 return err;
1032}
1033
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034/* On success it returns ifp with increased reference count */
1035
1036static struct inet6_ifaddr *
David Aherne6464b82018-05-27 08:09:53 -07001037ipv6_add_addr(struct inet6_dev *idev, struct ifa6_config *cfg,
David Ahernde95e042017-10-18 09:56:54 -07001038 bool can_block, struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039{
David Ahernf3d98322017-10-18 09:56:52 -07001040 gfp_t gfp_flags = can_block ? GFP_KERNEL : GFP_ATOMIC;
David Aherne6464b82018-05-27 08:09:53 -07001041 int addr_type = ipv6_addr_type(cfg->pfx);
David Forsterdf789fe2017-02-23 16:27:18 +00001042 struct net *net = dev_net(idev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 struct inet6_ifaddr *ifa = NULL;
David Ahern360a9882018-04-18 15:39:00 -07001044 struct fib6_info *f6i = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 int err = 0;
YOSHIFUJI Hideakid68b8272008-06-25 16:26:47 +09001046
1047 if (addr_type == IPV6_ADDR_ANY ||
Hangbin Liuf17f7642019-08-20 10:19:47 +08001048 (addr_type & IPV6_ADDR_MULTICAST &&
1049 !(cfg->ifa_flags & IFA_F_MCAUTOJOIN)) ||
YOSHIFUJI Hideakid68b8272008-06-25 16:26:47 +09001050 (!(idev->dev->flags & IFF_LOOPBACK) &&
Robert Shearman3ede0bb2018-09-19 13:56:53 +01001051 !netif_is_l3_master(idev->dev) &&
YOSHIFUJI Hideakid68b8272008-06-25 16:26:47 +09001052 addr_type & IPV6_ADDR_LOOPBACK))
1053 return ERR_PTR(-EADDRNOTAVAIL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 if (idev->dead) {
1056 err = -ENODEV; /*XXX*/
David Ahernf3d98322017-10-18 09:56:52 -07001057 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 }
1059
Brian Haley56d417b2009-06-01 03:07:33 -07001060 if (idev->cnf.disable_ipv6) {
Brian Haley9bdd8d42009-03-18 18:22:48 -07001061 err = -EACCES;
David Ahernf3d98322017-10-18 09:56:52 -07001062 goto out;
Brian Haley9bdd8d42009-03-18 18:22:48 -07001063 }
1064
David Ahernff7883e2017-10-18 09:56:53 -07001065 /* validator notifier needs to be blocking;
1066 * do not call in atomic context
1067 */
1068 if (can_block) {
1069 struct in6_validator_info i6vi = {
David Aherne6464b82018-05-27 08:09:53 -07001070 .i6vi_addr = *cfg->pfx,
David Ahernff7883e2017-10-18 09:56:53 -07001071 .i6vi_dev = idev,
David Ahernde95e042017-10-18 09:56:54 -07001072 .extack = extack,
David Ahernff7883e2017-10-18 09:56:53 -07001073 };
1074
1075 err = inet6addr_validator_notifier_call_chain(NETDEV_UP, &i6vi);
1076 err = notifier_to_errno(err);
1077 if (err < 0)
1078 goto out;
1079 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
David Ahernf3d98322017-10-18 09:56:52 -07001081 ifa = kzalloc(sizeof(*ifa), gfp_flags);
Ian Morris63159f22015-03-29 14:00:04 +01001082 if (!ifa) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 err = -ENOBUFS;
1084 goto out;
1085 }
1086
David Aherne6464b82018-05-27 08:09:53 -07001087 f6i = addrconf_f6i_alloc(net, idev, cfg->pfx, false, gfp_flags);
David Ahern360a9882018-04-18 15:39:00 -07001088 if (IS_ERR(f6i)) {
1089 err = PTR_ERR(f6i);
1090 f6i = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 goto out;
1092 }
1093
David Forsterdf789fe2017-02-23 16:27:18 +00001094 if (net->ipv6.devconf_all->disable_policy ||
1095 idev->cnf.disable_policy)
David Ahern360a9882018-04-18 15:39:00 -07001096 f6i->dst_nopolicy = true;
David Forsterdf789fe2017-02-23 16:27:18 +00001097
Jiri Pirkobba24892013-12-07 19:26:57 +01001098 neigh_parms_data_state_setall(idev->nd_parms);
1099
David Aherne6464b82018-05-27 08:09:53 -07001100 ifa->addr = *cfg->pfx;
1101 if (cfg->peer_pfx)
1102 ifa->peer_addr = *cfg->peer_pfx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
1104 spin_lock_init(&ifa->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01001105 INIT_DELAYED_WORK(&ifa->dad_work, addrconf_dad_work);
stephen hemmingerc2e21292010-03-17 20:31:10 +00001106 INIT_HLIST_NODE(&ifa->addr_lst);
David Aherne6464b82018-05-27 08:09:53 -07001107 ifa->scope = cfg->scope;
1108 ifa->prefix_len = cfg->plen;
David Ahern8308f3f2018-05-27 08:09:58 -07001109 ifa->rt_priority = cfg->rt_priority;
David Aherne6464b82018-05-27 08:09:53 -07001110 ifa->flags = cfg->ifa_flags;
Mahesh Bandewar66eb9f82017-05-12 17:03:39 -07001111 /* No need to add the TENTATIVE flag for addresses with NODAD */
David Aherne6464b82018-05-27 08:09:53 -07001112 if (!(cfg->ifa_flags & IFA_F_NODAD))
Mahesh Bandewar66eb9f82017-05-12 17:03:39 -07001113 ifa->flags |= IFA_F_TENTATIVE;
David Aherne6464b82018-05-27 08:09:53 -07001114 ifa->valid_lft = cfg->valid_lft;
1115 ifa->prefered_lft = cfg->preferred_lft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 ifa->cstamp = ifa->tstamp = jiffies;
Daniel Borkmann617fe292013-04-09 03:47:16 +00001117 ifa->tokenized = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
David Ahern360a9882018-04-18 15:39:00 -07001119 ifa->rt = f6i;
Keir Fraser57f5f542006-08-29 02:43:49 -07001120
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 ifa->idev = idev;
David Ahernf3d98322017-10-18 09:56:52 -07001122 in6_dev_hold(idev);
1123
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 /* For caller */
Reshetova, Elena271201c2017-07-04 09:34:56 +03001125 refcount_set(&ifa->refcnt, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
David Ahernf3d98322017-10-18 09:56:52 -07001127 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
David Ahernf3d98322017-10-18 09:56:52 -07001129 err = ipv6_add_addr_hash(idev->dev, ifa);
1130 if (err < 0) {
1131 rcu_read_unlock_bh();
1132 goto out;
1133 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
1135 write_lock(&idev->lock);
David Ahernf3d98322017-10-18 09:56:52 -07001136
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 /* Add to inet6_dev unicast addr list. */
Brian Haleye55ffac2006-07-10 15:25:51 -07001138 ipv6_link_dev_addr(idev, ifa);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 if (ifa->flags&IFA_F_TEMPORARY) {
stephen hemminger372e6c82010-03-17 20:31:09 +00001141 list_add(&ifa->tmp_list, &idev->tempaddr_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 in6_ifa_hold(ifa);
1143 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 in6_ifa_hold(ifa);
1146 write_unlock(&idev->lock);
David Ahernf3d98322017-10-18 09:56:52 -07001147
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001148 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
David Ahernf3d98322017-10-18 09:56:52 -07001150 inet6addr_notifier_call_chain(NETDEV_UP, ifa);
1151out:
1152 if (unlikely(err < 0)) {
David Ahern360a9882018-04-18 15:39:00 -07001153 fib6_info_release(f6i);
David Ahern93531c62018-04-17 17:33:25 -07001154
David Ahernf3d98322017-10-18 09:56:52 -07001155 if (ifa) {
1156 if (ifa->idev)
1157 in6_dev_put(ifa->idev);
1158 kfree(ifa);
1159 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 ifa = ERR_PTR(err);
1161 }
1162
1163 return ifa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164}
1165
Thomas Haller5b84efe2014-01-15 15:36:59 +01001166enum cleanup_prefix_rt_t {
1167 CLEANUP_PREFIX_RT_NOP, /* no cleanup action for prefix route */
1168 CLEANUP_PREFIX_RT_DEL, /* delete the prefix route */
1169 CLEANUP_PREFIX_RT_EXPIRE, /* update the lifetime of the prefix route */
1170};
1171
1172/*
1173 * Check, whether the prefix for ifp would still need a prefix route
1174 * after deleting ifp. The function returns one of the CLEANUP_PREFIX_RT_*
1175 * constants.
1176 *
1177 * 1) we don't purge prefix if address was not permanent.
1178 * prefix is managed by its own lifetime.
1179 * 2) we also don't purge, if the address was IFA_F_NOPREFIXROUTE.
1180 * 3) if there are no addresses, delete prefix.
1181 * 4) if there are still other permanent address(es),
1182 * corresponding prefix is still permanent.
1183 * 5) if there are still other addresses with IFA_F_NOPREFIXROUTE,
1184 * don't purge the prefix, assume user space is managing it.
1185 * 6) otherwise, update prefix lifetime to the
1186 * longest valid lifetime among the corresponding
1187 * addresses on the device.
1188 * Note: subsequent RA will update lifetime.
1189 **/
1190static enum cleanup_prefix_rt_t
1191check_cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long *expires)
1192{
1193 struct inet6_ifaddr *ifa;
1194 struct inet6_dev *idev = ifp->idev;
1195 unsigned long lifetime;
1196 enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_DEL;
1197
1198 *expires = jiffies;
1199
1200 list_for_each_entry(ifa, &idev->addr_list, if_list) {
1201 if (ifa == ifp)
1202 continue;
Zhiqiang Liue75913c2019-02-11 10:57:46 +08001203 if (ifa->prefix_len != ifp->prefix_len ||
1204 !ipv6_prefix_equal(&ifa->addr, &ifp->addr,
Thomas Haller5b84efe2014-01-15 15:36:59 +01001205 ifp->prefix_len))
1206 continue;
1207 if (ifa->flags & (IFA_F_PERMANENT | IFA_F_NOPREFIXROUTE))
1208 return CLEANUP_PREFIX_RT_NOP;
1209
1210 action = CLEANUP_PREFIX_RT_EXPIRE;
1211
1212 spin_lock(&ifa->lock);
1213
1214 lifetime = addrconf_timeout_fixup(ifa->valid_lft, HZ);
1215 /*
1216 * Note: Because this address is
1217 * not permanent, lifetime <
1218 * LONG_MAX / HZ here.
1219 */
1220 if (time_before(*expires, ifa->tstamp + lifetime * HZ))
1221 *expires = ifa->tstamp + lifetime * HZ;
1222 spin_unlock(&ifa->lock);
1223 }
1224
1225 return action;
1226}
1227
1228static void
1229cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long expires, bool del_rt)
1230{
David Ahern93c2fb22018-04-18 15:38:59 -07001231 struct fib6_info *f6i;
Thomas Haller5b84efe2014-01-15 15:36:59 +01001232
David Ahern2b2450c2019-03-27 20:53:52 -07001233 f6i = addrconf_get_prefix_route(&ifp->addr, ifp->prefix_len,
1234 ifp->idev->dev, 0, RTF_DEFAULT, true);
David Ahern93c2fb22018-04-18 15:38:59 -07001235 if (f6i) {
Thomas Haller5b84efe2014-01-15 15:36:59 +01001236 if (del_rt)
David Ahern93c2fb22018-04-18 15:38:59 -07001237 ip6_del_rt(dev_net(ifp->idev->dev), f6i);
Thomas Haller5b84efe2014-01-15 15:36:59 +01001238 else {
David Ahern93c2fb22018-04-18 15:38:59 -07001239 if (!(f6i->fib6_flags & RTF_EXPIRES))
1240 fib6_set_expires(f6i, expires);
1241 fib6_info_release(f6i);
Thomas Haller5b84efe2014-01-15 15:36:59 +01001242 }
1243 }
1244}
1245
1246
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247/* This function wants to get referenced ifp and releases it before return */
1248
1249static void ipv6_del_addr(struct inet6_ifaddr *ifp)
1250{
Herbert Xu4c5ff6a2010-05-18 15:54:18 -07001251 int state;
Thomas Haller5b84efe2014-01-15 15:36:59 +01001252 enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_NOP;
1253 unsigned long expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01001255 ASSERT_RTNL();
1256
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01001257 spin_lock_bh(&ifp->lock);
Herbert Xu4c5ff6a2010-05-18 15:54:18 -07001258 state = ifp->state;
Herbert Xue9d3e082010-05-18 15:36:06 -07001259 ifp->state = INET6_IFADDR_STATE_DEAD;
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01001260 spin_unlock_bh(&ifp->lock);
Herbert Xu4c5ff6a2010-05-18 15:54:18 -07001261
1262 if (state == INET6_IFADDR_STATE_DEAD)
1263 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
stephen hemminger5c578aed2010-03-17 20:31:11 +00001265 spin_lock_bh(&addrconf_hash_lock);
1266 hlist_del_init_rcu(&ifp->addr_lst);
stephen hemminger5c578aed2010-03-17 20:31:11 +00001267 spin_unlock_bh(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
Thomas Haller5b84efe2014-01-15 15:36:59 +01001269 write_lock_bh(&ifp->idev->lock);
David S. Miller5d9efa72013-10-28 20:07:50 -04001270
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 if (ifp->flags&IFA_F_TEMPORARY) {
stephen hemminger372e6c82010-03-17 20:31:09 +00001272 list_del(&ifp->tmp_list);
1273 if (ifp->ifpub) {
1274 in6_ifa_put(ifp->ifpub);
1275 ifp->ifpub = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 }
stephen hemminger372e6c82010-03-17 20:31:09 +00001277 __in6_ifa_put(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
Thomas Haller5b84efe2014-01-15 15:36:59 +01001280 if (ifp->flags & IFA_F_PERMANENT && !(ifp->flags & IFA_F_NOPREFIXROUTE))
1281 action = check_cleanup_prefix_route(ifp, &expires);
stephen hemminger502a2ff2010-03-17 20:31:13 +00001282
Eric Dumazet8ef802a2017-10-07 19:30:23 -07001283 list_del_rcu(&ifp->if_list);
Thomas Haller5b84efe2014-01-15 15:36:59 +01001284 __in6_ifa_put(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
Thomas Haller5b84efe2014-01-15 15:36:59 +01001286 write_unlock_bh(&ifp->idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01001288 addrconf_del_dad_work(ifp);
Andrey Vaginb2238562008-07-08 15:13:31 -07001289
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 ipv6_ifa_notify(RTM_DELADDR, ifp);
1291
Cong Wangf88c91d2013-04-14 23:18:43 +08001292 inet6addr_notifier_call_chain(NETDEV_DOWN, ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
Thomas Haller5b84efe2014-01-15 15:36:59 +01001294 if (action != CLEANUP_PREFIX_RT_NOP) {
1295 cleanup_prefix_route(ifp, expires,
1296 action == CLEANUP_PREFIX_RT_DEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 }
1298
Daniel Walterc3968a82011-04-13 21:10:57 +00001299 /* clean up prefsrc entries */
1300 rt6_remove_prefsrc(ifp);
Herbert Xu4c5ff6a2010-05-18 15:54:18 -07001301out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 in6_ifa_put(ifp);
1303}
1304
Eric Dumazetfffcefe2017-11-06 14:13:29 -08001305static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp,
1306 struct inet6_ifaddr *ift,
1307 bool block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308{
1309 struct inet6_dev *idev = ifp->idev;
1310 struct in6_addr addr, *tmpaddr;
David Aherne6464b82018-05-27 08:09:53 -07001311 unsigned long tmp_tstamp, age;
Benoit Boissinoteac55bf2008-04-02 00:01:35 -07001312 unsigned long regen_advance;
David Aherne6464b82018-05-27 08:09:53 -07001313 struct ifa6_config cfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 int ret = 0;
Lorenzo Colitti76f793e2011-07-26 13:50:49 +00001315 unsigned long now = jiffies;
Jiri Bohac76506a92016-10-13 18:52:15 +02001316 long max_desync_factor;
Jiri Bohac7aa8e632016-10-20 12:29:26 +02001317 s32 cnf_temp_preferred_lft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
Jiri Pirko53bd6742013-12-06 09:45:22 +01001319 write_lock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 if (ift) {
1321 spin_lock_bh(&ift->lock);
1322 memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8);
1323 spin_unlock_bh(&ift->lock);
1324 tmpaddr = &addr;
1325 } else {
1326 tmpaddr = NULL;
1327 }
1328retry:
1329 in6_dev_hold(idev);
1330 if (idev->cnf.use_tempaddr <= 0) {
Jiri Pirko53bd6742013-12-06 09:45:22 +01001331 write_unlock_bh(&idev->lock);
Joe Perchesf3213832012-05-15 14:11:53 +00001332 pr_info("%s: use_tempaddr is disabled\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 in6_dev_put(idev);
1334 ret = -1;
1335 goto out;
1336 }
1337 spin_lock_bh(&ifp->lock);
1338 if (ifp->regen_count++ >= idev->cnf.regen_max_retry) {
1339 idev->cnf.use_tempaddr = -1; /*XXX*/
1340 spin_unlock_bh(&ifp->lock);
Jiri Pirko53bd6742013-12-06 09:45:22 +01001341 write_unlock_bh(&idev->lock);
Joe Perchesf3213832012-05-15 14:11:53 +00001342 pr_warn("%s: regeneration time exceeded - disabled temporary address support\n",
1343 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 in6_dev_put(idev);
1345 ret = -1;
1346 goto out;
1347 }
1348 in6_ifa_hold(ifp);
1349 memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
Jiri Bohac9d6280d2016-10-13 18:50:02 +02001350 ipv6_try_regen_rndid(idev, tmpaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 memcpy(&addr.s6_addr[8], idev->rndid, 8);
Lorenzo Colitti76f793e2011-07-26 13:50:49 +00001352 age = (now - ifp->tstamp) / HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
Benoit Boissinoteac55bf2008-04-02 00:01:35 -07001354 regen_advance = idev->cnf.regen_max_retry *
Ian Morris67ba4152014-08-24 21:53:10 +01001355 idev->cnf.dad_transmits *
1356 NEIGH_VAR(idev->nd_parms, RETRANS_TIME) / HZ;
Jiri Bohac76506a92016-10-13 18:52:15 +02001357
1358 /* recalculate max_desync_factor each time and update
1359 * idev->desync_factor if it's larger
1360 */
Jiri Bohac7aa8e632016-10-20 12:29:26 +02001361 cnf_temp_preferred_lft = READ_ONCE(idev->cnf.temp_prefered_lft);
Jiri Bohac76506a92016-10-13 18:52:15 +02001362 max_desync_factor = min_t(__u32,
1363 idev->cnf.max_desync_factor,
Jiri Bohac7aa8e632016-10-20 12:29:26 +02001364 cnf_temp_preferred_lft - regen_advance);
Jiri Bohac76506a92016-10-13 18:52:15 +02001365
1366 if (unlikely(idev->desync_factor > max_desync_factor)) {
1367 if (max_desync_factor > 0) {
1368 get_random_bytes(&idev->desync_factor,
1369 sizeof(idev->desync_factor));
1370 idev->desync_factor %= max_desync_factor;
1371 } else {
1372 idev->desync_factor = 0;
1373 }
1374 }
1375
David Ahern3f2d67b2018-06-11 07:12:12 -07001376 memset(&cfg, 0, sizeof(cfg));
David Aherne6464b82018-05-27 08:09:53 -07001377 cfg.valid_lft = min_t(__u32, ifp->valid_lft,
Jiri Bohac76506a92016-10-13 18:52:15 +02001378 idev->cnf.temp_valid_lft + age);
David Aherne6464b82018-05-27 08:09:53 -07001379 cfg.preferred_lft = cnf_temp_preferred_lft + age - idev->desync_factor;
1380 cfg.preferred_lft = min_t(__u32, ifp->prefered_lft, cfg.preferred_lft);
1381
1382 cfg.plen = ifp->prefix_len;
Jiri Bohac76506a92016-10-13 18:52:15 +02001383 tmp_tstamp = ifp->tstamp;
1384 spin_unlock_bh(&ifp->lock);
1385
Jiri Pirko53bd6742013-12-06 09:45:22 +01001386 write_unlock_bh(&idev->lock);
Neil Horman95c385b2007-04-25 17:08:10 -07001387
Benoit Boissinoteac55bf2008-04-02 00:01:35 -07001388 /* A temporary address is created only if this calculated Preferred
1389 * Lifetime is greater than REGEN_ADVANCE time units. In particular,
1390 * an implementation must not create a temporary address with a zero
1391 * Preferred Lifetime.
Heiner Kallweitecab6702014-03-12 22:13:19 +01001392 * Use age calculation as in addrconf_verify to avoid unnecessary
1393 * temporary addresses being generated.
Benoit Boissinoteac55bf2008-04-02 00:01:35 -07001394 */
Heiner Kallweitecab6702014-03-12 22:13:19 +01001395 age = (now - tmp_tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
David Aherne6464b82018-05-27 08:09:53 -07001396 if (cfg.preferred_lft <= regen_advance + age) {
Benoit Boissinoteac55bf2008-04-02 00:01:35 -07001397 in6_ifa_put(ifp);
1398 in6_dev_put(idev);
1399 ret = -1;
1400 goto out;
1401 }
1402
David Aherne6464b82018-05-27 08:09:53 -07001403 cfg.ifa_flags = IFA_F_TEMPORARY;
Neil Horman95c385b2007-04-25 17:08:10 -07001404 /* set in addrconf_prefix_rcv() */
1405 if (ifp->flags & IFA_F_OPTIMISTIC)
David Aherne6464b82018-05-27 08:09:53 -07001406 cfg.ifa_flags |= IFA_F_OPTIMISTIC;
Neil Horman95c385b2007-04-25 17:08:10 -07001407
David Aherne6464b82018-05-27 08:09:53 -07001408 cfg.pfx = &addr;
1409 cfg.scope = ipv6_addr_scope(cfg.pfx);
1410
1411 ift = ipv6_add_addr(idev, &cfg, block, NULL);
Hannes Frederic Sowa4b08a8f2013-08-16 13:02:27 +02001412 if (IS_ERR(ift)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 in6_ifa_put(ifp);
1414 in6_dev_put(idev);
Joe Perchesf3213832012-05-15 14:11:53 +00001415 pr_info("%s: retry temporary address regeneration\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 tmpaddr = &addr;
Jiri Pirko53bd6742013-12-06 09:45:22 +01001417 write_lock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 goto retry;
1419 }
1420
1421 spin_lock_bh(&ift->lock);
1422 ift->ifpub = ifp;
Lorenzo Colitti76f793e2011-07-26 13:50:49 +00001423 ift->cstamp = now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 ift->tstamp = tmp_tstamp;
1425 spin_unlock_bh(&ift->lock);
1426
David S. Millercf22f9a2012-04-14 21:37:40 -04001427 addrconf_dad_start(ift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 in6_ifa_put(ift);
1429 in6_dev_put(idev);
1430out:
1431 return ret;
1432}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
1434/*
YOSHIFUJI Hideaki072047e42005-11-08 09:38:30 -08001435 * Choose an appropriate source address (RFC3484)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 */
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001437enum {
1438 IPV6_SADDR_RULE_INIT = 0,
1439 IPV6_SADDR_RULE_LOCAL,
1440 IPV6_SADDR_RULE_SCOPE,
1441 IPV6_SADDR_RULE_PREFERRED,
1442#ifdef CONFIG_IPV6_MIP6
1443 IPV6_SADDR_RULE_HOA,
1444#endif
1445 IPV6_SADDR_RULE_OIF,
1446 IPV6_SADDR_RULE_LABEL,
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001447 IPV6_SADDR_RULE_PRIVACY,
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001448 IPV6_SADDR_RULE_ORCHID,
1449 IPV6_SADDR_RULE_PREFIX,
Erik Kline7fd25612014-10-28 18:11:14 +09001450#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1451 IPV6_SADDR_RULE_NOT_OPTIMISTIC,
1452#endif
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001453 IPV6_SADDR_RULE_MAX
YOSHIFUJI Hideaki072047e42005-11-08 09:38:30 -08001454};
1455
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001456struct ipv6_saddr_score {
1457 int rule;
1458 int addr_type;
1459 struct inet6_ifaddr *ifa;
1460 DECLARE_BITMAP(scorebits, IPV6_SADDR_RULE_MAX);
1461 int scopedist;
1462 int matchlen;
1463};
1464
1465struct ipv6_saddr_dst {
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +09001466 const struct in6_addr *addr;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001467 int ifindex;
1468 int scope;
1469 int label;
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001470 unsigned int prefs;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001471};
YOSHIFUJI Hideaki072047e42005-11-08 09:38:30 -08001472
Dave Jonesb6f99a22007-03-22 12:27:49 -07001473static inline int ipv6_saddr_preferred(int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474{
Ulrich Weber45bb0062010-02-25 23:28:58 +00001475 if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|IPV6_ADDR_LOOPBACK))
YOSHIFUJI Hideaki072047e42005-11-08 09:38:30 -08001476 return 1;
1477 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478}
1479
Matteo Croce35e015e2017-09-12 17:46:37 +02001480static bool ipv6_use_optimistic_addr(struct net *net,
1481 struct inet6_dev *idev)
Erik Kline7fd25612014-10-28 18:11:14 +09001482{
1483#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
Matteo Croce35e015e2017-09-12 17:46:37 +02001484 if (!idev)
1485 return false;
1486 if (!net->ipv6.devconf_all->optimistic_dad && !idev->cnf.optimistic_dad)
1487 return false;
1488 if (!net->ipv6.devconf_all->use_optimistic && !idev->cnf.use_optimistic)
1489 return false;
1490
1491 return true;
Erik Kline7fd25612014-10-28 18:11:14 +09001492#else
1493 return false;
1494#endif
1495}
1496
Sabrina Dubrocaf1c02cf2018-02-28 16:40:08 +01001497static bool ipv6_allow_optimistic_dad(struct net *net,
1498 struct inet6_dev *idev)
1499{
1500#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1501 if (!idev)
1502 return false;
1503 if (!net->ipv6.devconf_all->optimistic_dad && !idev->cnf.optimistic_dad)
1504 return false;
1505
1506 return true;
1507#else
1508 return false;
1509#endif
1510}
1511
Benjamin Thery3de23252008-05-28 14:51:24 +02001512static int ipv6_get_saddr_eval(struct net *net,
1513 struct ipv6_saddr_score *score,
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001514 struct ipv6_saddr_dst *dst,
1515 int i)
1516{
1517 int ret;
1518
1519 if (i <= score->rule) {
1520 switch (i) {
1521 case IPV6_SADDR_RULE_SCOPE:
1522 ret = score->scopedist;
1523 break;
1524 case IPV6_SADDR_RULE_PREFIX:
1525 ret = score->matchlen;
1526 break;
1527 default:
1528 ret = !!test_bit(i, score->scorebits);
1529 }
1530 goto out;
1531 }
1532
1533 switch (i) {
1534 case IPV6_SADDR_RULE_INIT:
1535 /* Rule 0: remember if hiscore is not ready yet */
1536 ret = !!score->ifa;
1537 break;
1538 case IPV6_SADDR_RULE_LOCAL:
1539 /* Rule 1: Prefer same address */
1540 ret = ipv6_addr_equal(&score->ifa->addr, dst->addr);
1541 break;
1542 case IPV6_SADDR_RULE_SCOPE:
1543 /* Rule 2: Prefer appropriate scope
1544 *
1545 * ret
1546 * ^
1547 * -1 | d 15
1548 * ---+--+-+---> scope
1549 * |
1550 * | d is scope of the destination.
1551 * B-d | \
1552 * | \ <- smaller scope is better if
stephen hemmingerdb9c7c32014-01-12 11:26:32 -08001553 * B-15 | \ if scope is enough for destination.
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001554 * | ret = B - scope (-1 <= scope >= d <= 15).
1555 * d-C-1 | /
1556 * |/ <- greater is better
1557 * -C / if scope is not enough for destination.
1558 * /| ret = scope - C (-1 <= d < scope <= 15).
1559 *
1560 * d - C - 1 < B -15 (for all -1 <= d <= 15).
1561 * C > d + 14 - B >= 15 + 14 - B = 29 - B.
1562 * Assume B = 0 and we get C > 29.
1563 */
1564 ret = __ipv6_addr_src_scope(score->addr_type);
1565 if (ret >= dst->scope)
1566 ret = -ret;
1567 else
1568 ret -= 128; /* 30 is enough */
1569 score->scopedist = ret;
1570 break;
1571 case IPV6_SADDR_RULE_PREFERRED:
Erik Kline7fd25612014-10-28 18:11:14 +09001572 {
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001573 /* Rule 3: Avoid deprecated and optimistic addresses */
Erik Kline7fd25612014-10-28 18:11:14 +09001574 u8 avoid = IFA_F_DEPRECATED;
1575
Matteo Croce35e015e2017-09-12 17:46:37 +02001576 if (!ipv6_use_optimistic_addr(net, score->ifa->idev))
Erik Kline7fd25612014-10-28 18:11:14 +09001577 avoid |= IFA_F_OPTIMISTIC;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001578 ret = ipv6_saddr_preferred(score->addr_type) ||
Erik Kline7fd25612014-10-28 18:11:14 +09001579 !(score->ifa->flags & avoid);
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001580 break;
Erik Kline7fd25612014-10-28 18:11:14 +09001581 }
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001582#ifdef CONFIG_IPV6_MIP6
1583 case IPV6_SADDR_RULE_HOA:
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001584 {
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001585 /* Rule 4: Prefer home address */
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001586 int prefhome = !(dst->prefs & IPV6_PREFER_SRC_COA);
1587 ret = !(score->ifa->flags & IFA_F_HOMEADDRESS) ^ prefhome;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001588 break;
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001589 }
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001590#endif
1591 case IPV6_SADDR_RULE_OIF:
1592 /* Rule 5: Prefer outgoing interface */
1593 ret = (!dst->ifindex ||
1594 dst->ifindex == score->ifa->idev->dev->ifindex);
1595 break;
1596 case IPV6_SADDR_RULE_LABEL:
1597 /* Rule 6: Prefer matching label */
Benjamin Thery3de23252008-05-28 14:51:24 +02001598 ret = ipv6_addr_label(net,
1599 &score->ifa->addr, score->addr_type,
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001600 score->ifa->idev->dev->ifindex) == dst->label;
1601 break;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001602 case IPV6_SADDR_RULE_PRIVACY:
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001603 {
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001604 /* Rule 7: Prefer public address
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001605 * Note: prefer temporary address if use_tempaddr >= 2
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001606 */
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001607 int preftmp = dst->prefs & (IPV6_PREFER_SRC_PUBLIC|IPV6_PREFER_SRC_TMP) ?
1608 !!(dst->prefs & IPV6_PREFER_SRC_TMP) :
1609 score->ifa->idev->cnf.use_tempaddr >= 2;
1610 ret = (!(score->ifa->flags & IFA_F_TEMPORARY)) ^ preftmp;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001611 break;
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001612 }
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001613 case IPV6_SADDR_RULE_ORCHID:
1614 /* Rule 8-: Prefer ORCHID vs ORCHID or
1615 * non-ORCHID vs non-ORCHID
1616 */
1617 ret = !(ipv6_addr_orchid(&score->ifa->addr) ^
1618 ipv6_addr_orchid(dst->addr));
1619 break;
1620 case IPV6_SADDR_RULE_PREFIX:
1621 /* Rule 8: Use longest matching prefix */
YOSHIFUJI Hideaki / 吉藤英明91b4b042012-09-10 18:41:07 +00001622 ret = ipv6_addr_diff(&score->ifa->addr, dst->addr);
1623 if (ret > score->ifa->prefix_len)
1624 ret = score->ifa->prefix_len;
1625 score->matchlen = ret;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001626 break;
Erik Kline7fd25612014-10-28 18:11:14 +09001627#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1628 case IPV6_SADDR_RULE_NOT_OPTIMISTIC:
1629 /* Optimistic addresses still have lower precedence than other
1630 * preferred addresses.
1631 */
1632 ret = !(score->ifa->flags & IFA_F_OPTIMISTIC);
1633 break;
1634#endif
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001635 default:
1636 ret = 0;
1637 }
1638
1639 if (ret)
1640 __set_bit(i, score->scorebits);
1641 score->rule = i;
1642out:
1643 return ret;
1644}
1645
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001646static int __ipv6_dev_get_saddr(struct net *net,
1647 struct ipv6_saddr_dst *dst,
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001648 struct inet6_dev *idev,
1649 struct ipv6_saddr_score *scores,
1650 int hiscore_idx)
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001651{
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001652 struct ipv6_saddr_score *score = &scores[1 - hiscore_idx], *hiscore = &scores[hiscore_idx];
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001653
Eric Dumazetf59c0312017-10-07 19:30:27 -07001654 list_for_each_entry_rcu(score->ifa, &idev->addr_list, if_list) {
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001655 int i;
1656
1657 /*
1658 * - Tentative Address (RFC2462 section 5.4)
1659 * - A tentative address is not considered
1660 * "assigned to an interface" in the traditional
1661 * sense, unless it is also flagged as optimistic.
1662 * - Candidate Source Address (section 4)
1663 * - In any case, anycast addresses, multicast
1664 * addresses, and the unspecified address MUST
1665 * NOT be included in a candidate set.
1666 */
1667 if ((score->ifa->flags & IFA_F_TENTATIVE) &&
1668 (!(score->ifa->flags & IFA_F_OPTIMISTIC)))
1669 continue;
1670
1671 score->addr_type = __ipv6_addr_type(&score->ifa->addr);
1672
1673 if (unlikely(score->addr_type == IPV6_ADDR_ANY ||
1674 score->addr_type & IPV6_ADDR_MULTICAST)) {
1675 net_dbg_ratelimited("ADDRCONF: unspecified / multicast address assigned as unicast address on %s",
1676 idev->dev->name);
1677 continue;
1678 }
1679
1680 score->rule = -1;
1681 bitmap_zero(score->scorebits, IPV6_SADDR_RULE_MAX);
1682
1683 for (i = 0; i < IPV6_SADDR_RULE_MAX; i++) {
1684 int minihiscore, miniscore;
1685
1686 minihiscore = ipv6_get_saddr_eval(net, hiscore, dst, i);
1687 miniscore = ipv6_get_saddr_eval(net, score, dst, i);
1688
1689 if (minihiscore > miniscore) {
1690 if (i == IPV6_SADDR_RULE_SCOPE &&
1691 score->scopedist > 0) {
1692 /*
1693 * special case:
1694 * each remaining entry
1695 * has too small (not enough)
1696 * scope, because ifa entries
1697 * are sorted by their scope
1698 * values.
1699 */
1700 goto out;
1701 }
1702 break;
1703 } else if (minihiscore < miniscore) {
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001704 swap(hiscore, score);
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001705 hiscore_idx = 1 - hiscore_idx;
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001706
1707 /* restore our iterator */
1708 score->ifa = hiscore->ifa;
1709
1710 break;
1711 }
1712 }
1713 }
1714out:
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001715 return hiscore_idx;
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001716}
1717
David Ahernafbac6012016-06-16 16:24:26 -07001718static int ipv6_get_saddr_master(struct net *net,
1719 const struct net_device *dst_dev,
1720 const struct net_device *master,
1721 struct ipv6_saddr_dst *dst,
1722 struct ipv6_saddr_score *scores,
1723 int hiscore_idx)
1724{
1725 struct inet6_dev *idev;
1726
1727 idev = __in6_dev_get(dst_dev);
1728 if (idev)
1729 hiscore_idx = __ipv6_dev_get_saddr(net, dst, idev,
1730 scores, hiscore_idx);
1731
1732 idev = __in6_dev_get(master);
1733 if (idev)
1734 hiscore_idx = __ipv6_dev_get_saddr(net, dst, idev,
1735 scores, hiscore_idx);
1736
1737 return hiscore_idx;
1738}
1739
Patrick McHardyb3f644f2012-08-26 19:14:14 +02001740int ipv6_dev_get_saddr(struct net *net, const struct net_device *dst_dev,
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +09001741 const struct in6_addr *daddr, unsigned int prefs,
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001742 struct in6_addr *saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743{
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001744 struct ipv6_saddr_score scores[2], *hiscore;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001745 struct ipv6_saddr_dst dst;
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001746 struct inet6_dev *idev;
YOSHIFUJI Hideaki072047e42005-11-08 09:38:30 -08001747 struct net_device *dev;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001748 int dst_type;
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001749 bool use_oif_addr = false;
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001750 int hiscore_idx = 0;
Eric Dumazetcc429c82017-10-07 19:30:28 -07001751 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001753 dst_type = __ipv6_addr_type(daddr);
1754 dst.addr = daddr;
1755 dst.ifindex = dst_dev ? dst_dev->ifindex : 0;
1756 dst.scope = __ipv6_addr_src_scope(dst_type);
Benjamin Thery3de23252008-05-28 14:51:24 +02001757 dst.label = ipv6_addr_label(net, daddr, dst_type, dst.ifindex);
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001758 dst.prefs = prefs;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001759
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001760 scores[hiscore_idx].rule = -1;
1761 scores[hiscore_idx].ifa = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001763 rcu_read_lock();
YOSHIFUJI Hideaki072047e42005-11-08 09:38:30 -08001764
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001765 /* Candidate Source Address (section 4)
1766 * - multicast and link-local destination address,
1767 * the set of candidate source address MUST only
1768 * include addresses assigned to interfaces
1769 * belonging to the same link as the outgoing
1770 * interface.
1771 * (- For site-local destination addresses, the
1772 * set of candidate source addresses MUST only
1773 * include addresses assigned to interfaces
1774 * belonging to the same site as the outgoing
1775 * interface.)
Erik Kline3985e8a2015-07-22 16:38:25 +09001776 * - "It is RECOMMENDED that the candidate source addresses
1777 * be the set of unicast addresses assigned to the
1778 * interface that will be used to send to the destination
1779 * (the 'outgoing' interface)." (RFC 6724)
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001780 */
1781 if (dst_dev) {
Erik Kline3985e8a2015-07-22 16:38:25 +09001782 idev = __in6_dev_get(dst_dev);
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001783 if ((dst_type & IPV6_ADDR_MULTICAST) ||
Erik Kline3985e8a2015-07-22 16:38:25 +09001784 dst.scope <= IPV6_ADDR_SCOPE_LINKLOCAL ||
1785 (idev && idev->cnf.use_oif_addrs_only)) {
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001786 use_oif_addr = true;
YOSHIFUJI Hideaki072047e42005-11-08 09:38:30 -08001787 }
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001788 }
1789
1790 if (use_oif_addr) {
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001791 if (idev)
YOSHIFUJI Hideakic15df302015-07-16 16:51:30 +09001792 hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx);
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001793 } else {
David Ahernafbac6012016-06-16 16:24:26 -07001794 const struct net_device *master;
1795 int master_idx = 0;
1796
1797 /* if dst_dev exists and is enslaved to an L3 device, then
1798 * prefer addresses from dst_dev and then the master over
1799 * any other enslaved devices in the L3 domain.
1800 */
1801 master = l3mdev_master_dev_rcu(dst_dev);
1802 if (master) {
1803 master_idx = master->ifindex;
1804
1805 hiscore_idx = ipv6_get_saddr_master(net, dst_dev,
1806 master, &dst,
1807 scores, hiscore_idx);
1808
1809 if (scores[hiscore_idx].ifa)
1810 goto out;
1811 }
1812
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001813 for_each_netdev_rcu(net, dev) {
David Ahernafbac6012016-06-16 16:24:26 -07001814 /* only consider addresses on devices in the
1815 * same L3 domain
1816 */
1817 if (l3mdev_master_ifindex_rcu(dev) != master_idx)
1818 continue;
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001819 idev = __in6_dev_get(dev);
1820 if (!idev)
1821 continue;
YOSHIFUJI Hideakic15df302015-07-16 16:51:30 +09001822 hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx);
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001823 }
YOSHIFUJI Hideaki072047e42005-11-08 09:38:30 -08001824 }
David Ahernafbac6012016-06-16 16:24:26 -07001825
1826out:
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001827 hiscore = &scores[hiscore_idx];
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001828 if (!hiscore->ifa)
Eric Dumazetcc429c82017-10-07 19:30:28 -07001829 ret = -EADDRNOTAVAIL;
1830 else
1831 *saddr = hiscore->ifa->addr;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001832
Eric Dumazetcc429c82017-10-07 19:30:28 -07001833 rcu_read_unlock();
1834 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835}
YOSHIFUJI Hideaki5e5f3f02008-03-03 21:44:34 +09001836EXPORT_SYMBOL(ipv6_dev_get_saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837
Amerigo Wang89657792013-06-29 21:30:49 +08001838int __ipv6_get_lladdr(struct inet6_dev *idev, struct in6_addr *addr,
Jiri Pirko479840f2013-12-06 09:45:21 +01001839 u32 banned_flags)
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02001840{
1841 struct inet6_ifaddr *ifp;
1842 int err = -EADDRNOTAVAIL;
1843
Hannes Frederic Sowa602582c2014-01-19 21:58:19 +01001844 list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) {
1845 if (ifp->scope > IFA_LINK)
1846 break;
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02001847 if (ifp->scope == IFA_LINK &&
1848 !(ifp->flags & banned_flags)) {
1849 *addr = ifp->addr;
1850 err = 0;
1851 break;
1852 }
1853 }
1854 return err;
1855}
1856
Neil Horman95c385b2007-04-25 17:08:10 -07001857int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr,
Jiri Pirko479840f2013-12-06 09:45:21 +01001858 u32 banned_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859{
1860 struct inet6_dev *idev;
1861 int err = -EADDRNOTAVAIL;
1862
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001863 rcu_read_lock();
Stephen Hemmingere21e8462010-03-20 16:09:01 -07001864 idev = __in6_dev_get(dev);
1865 if (idev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 read_lock_bh(&idev->lock);
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02001867 err = __ipv6_get_lladdr(idev, addr, banned_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 read_unlock_bh(&idev->lock);
1869 }
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001870 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 return err;
1872}
1873
Eric Dumazetd9bf82c2017-10-07 19:30:24 -07001874static int ipv6_count_addresses(const struct inet6_dev *idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875{
Eric Dumazetd9bf82c2017-10-07 19:30:24 -07001876 const struct inet6_ifaddr *ifp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 int cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878
Eric Dumazetd9bf82c2017-10-07 19:30:24 -07001879 rcu_read_lock();
1880 list_for_each_entry_rcu(ifp, &idev->addr_list, if_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 cnt++;
Eric Dumazetd9bf82c2017-10-07 19:30:24 -07001882 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 return cnt;
1884}
1885
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001886int ipv6_chk_addr(struct net *net, const struct in6_addr *addr,
Florian Westphal2a7851b2013-05-17 03:56:10 +00001887 const struct net_device *dev, int strict)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888{
David Ahern232378e2018-03-13 08:29:37 -07001889 return ipv6_chk_addr_and_flags(net, addr, dev, !dev,
1890 strict, IFA_F_TENTATIVE);
Erik Klinec58da4c2015-02-04 20:01:23 +09001891}
1892EXPORT_SYMBOL(ipv6_chk_addr);
1893
David Ahern1893ff22018-03-13 08:29:38 -07001894/* device argument is used to find the L3 domain of interest. If
1895 * skip_dev_check is set, then the ifp device is not checked against
1896 * the passed in dev argument. So the 2 cases for addresses checks are:
1897 * 1. does the address exist in the L3 domain that dev is part of
1898 * (skip_dev_check = true), or
1899 *
1900 * 2. does the address exist on the specific device
1901 * (skip_dev_check = false)
1902 */
Erik Klinec58da4c2015-02-04 20:01:23 +09001903int ipv6_chk_addr_and_flags(struct net *net, const struct in6_addr *addr,
David Ahern232378e2018-03-13 08:29:37 -07001904 const struct net_device *dev, bool skip_dev_check,
1905 int strict, u32 banned_flags)
Erik Klinec58da4c2015-02-04 20:01:23 +09001906{
Eric Dumazet3f27fb22017-10-23 16:17:47 -07001907 unsigned int hash = inet6_addr_hash(net, addr);
David Ahern1893ff22018-03-13 08:29:38 -07001908 const struct net_device *l3mdev;
Stephen Hemmingereedf0422010-05-17 22:27:12 -07001909 struct inet6_ifaddr *ifp;
Erik Klinec58da4c2015-02-04 20:01:23 +09001910 u32 ifp_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911
Eric Dumazet480318a2017-10-23 16:17:48 -07001912 rcu_read_lock();
David Ahern232378e2018-03-13 08:29:37 -07001913
David Ahern1893ff22018-03-13 08:29:38 -07001914 l3mdev = l3mdev_master_dev_rcu(dev);
David Ahern232378e2018-03-13 08:29:37 -07001915 if (skip_dev_check)
1916 dev = NULL;
1917
Sasha Levinb67bfe02013-02-27 17:06:00 -08001918 hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09001919 if (!net_eq(dev_net(ifp->idev->dev), net))
Daniel Lezcanobfeade02008-01-10 22:43:18 -08001920 continue;
David Ahern1893ff22018-03-13 08:29:38 -07001921
1922 if (l3mdev_master_dev_rcu(ifp->idev->dev) != l3mdev)
1923 continue;
1924
Erik Klinec58da4c2015-02-04 20:01:23 +09001925 /* Decouple optimistic from tentative for evaluation here.
1926 * Ban optimistic addresses explicitly, when required.
1927 */
1928 ifp_flags = (ifp->flags&IFA_F_OPTIMISTIC)
1929 ? (ifp->flags&~IFA_F_TENTATIVE)
1930 : ifp->flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 if (ipv6_addr_equal(&ifp->addr, addr) &&
Erik Klinec58da4c2015-02-04 20:01:23 +09001932 !(ifp_flags&banned_flags) &&
Ian Morris63159f22015-03-29 14:00:04 +01001933 (!dev || ifp->idev->dev == dev ||
Stephen Hemmingereedf0422010-05-17 22:27:12 -07001934 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))) {
Eric Dumazet480318a2017-10-23 16:17:48 -07001935 rcu_read_unlock();
Stephen Hemmingereedf0422010-05-17 22:27:12 -07001936 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 }
1938 }
stephen hemminger5c578aed2010-03-17 20:31:11 +00001939
Eric Dumazet480318a2017-10-23 16:17:48 -07001940 rcu_read_unlock();
Stephen Hemmingereedf0422010-05-17 22:27:12 -07001941 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942}
Erik Klinec58da4c2015-02-04 20:01:23 +09001943EXPORT_SYMBOL(ipv6_chk_addr_and_flags);
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +09001944
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945
Catalin\(ux\) M. BOIE7df37ff2013-09-23 23:04:19 +03001946/* Compares an address/prefix_len with addresses on device @dev.
1947 * If one is found it returns true.
1948 */
1949bool ipv6_chk_custom_prefix(const struct in6_addr *addr,
1950 const unsigned int prefix_len, struct net_device *dev)
1951{
Eric Dumazet47e26942017-10-07 19:30:25 -07001952 const struct inet6_ifaddr *ifa;
1953 const struct inet6_dev *idev;
Catalin\(ux\) M. BOIE7df37ff2013-09-23 23:04:19 +03001954 bool ret = false;
1955
1956 rcu_read_lock();
1957 idev = __in6_dev_get(dev);
1958 if (idev) {
Eric Dumazet47e26942017-10-07 19:30:25 -07001959 list_for_each_entry_rcu(ifa, &idev->addr_list, if_list) {
Catalin\(ux\) M. BOIE7df37ff2013-09-23 23:04:19 +03001960 ret = ipv6_prefix_equal(addr, &ifa->addr, prefix_len);
1961 if (ret)
1962 break;
1963 }
Catalin\(ux\) M. BOIE7df37ff2013-09-23 23:04:19 +03001964 }
1965 rcu_read_unlock();
1966
1967 return ret;
1968}
1969EXPORT_SYMBOL(ipv6_chk_custom_prefix);
1970
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001971int ipv6_chk_prefix(const struct in6_addr *addr, struct net_device *dev)
YOSHIFUJI Hideaki52eeeb82008-03-15 22:54:23 -04001972{
Eric Dumazet24ba3332017-10-07 19:30:26 -07001973 const struct inet6_ifaddr *ifa;
1974 const struct inet6_dev *idev;
YOSHIFUJI Hideaki52eeeb82008-03-15 22:54:23 -04001975 int onlink;
1976
1977 onlink = 0;
1978 rcu_read_lock();
1979 idev = __in6_dev_get(dev);
1980 if (idev) {
Eric Dumazet24ba3332017-10-07 19:30:26 -07001981 list_for_each_entry_rcu(ifa, &idev->addr_list, if_list) {
YOSHIFUJI Hideaki52eeeb82008-03-15 22:54:23 -04001982 onlink = ipv6_prefix_equal(addr, &ifa->addr,
1983 ifa->prefix_len);
1984 if (onlink)
1985 break;
1986 }
YOSHIFUJI Hideaki52eeeb82008-03-15 22:54:23 -04001987 }
1988 rcu_read_unlock();
1989 return onlink;
1990}
YOSHIFUJI Hideaki52eeeb82008-03-15 22:54:23 -04001991EXPORT_SYMBOL(ipv6_chk_prefix);
1992
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +09001993struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, const struct in6_addr *addr,
Daniel Lezcano1cab3da2008-01-10 22:44:09 -08001994 struct net_device *dev, int strict)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995{
Eric Dumazet3f27fb22017-10-23 16:17:47 -07001996 unsigned int hash = inet6_addr_hash(net, addr);
David S. Millerb79d1d52010-03-25 21:39:21 -07001997 struct inet6_ifaddr *ifp, *result = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998
Eric Dumazet24f226d2017-10-23 16:17:49 -07001999 rcu_read_lock();
2000 hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09002001 if (!net_eq(dev_net(ifp->idev->dev), net))
Daniel Lezcano1cab3da2008-01-10 22:44:09 -08002002 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 if (ipv6_addr_equal(&ifp->addr, addr)) {
Ian Morris63159f22015-03-29 14:00:04 +01002004 if (!dev || ifp->idev->dev == dev ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
David S. Millerb79d1d52010-03-25 21:39:21 -07002006 result = ifp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 in6_ifa_hold(ifp);
2008 break;
2009 }
2010 }
2011 }
Eric Dumazet24f226d2017-10-23 16:17:49 -07002012 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013
David S. Millerb79d1d52010-03-25 21:39:21 -07002014 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015}
2016
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017/* Gets referenced address, destroys ifaddr */
2018
Brian Haleycc411d02009-09-09 14:41:32 +00002019static void addrconf_dad_stop(struct inet6_ifaddr *ifp, int dad_failed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020{
Lubomir Rintel3d171f32016-01-08 13:47:23 +01002021 if (dad_failed)
2022 ifp->flags |= IFA_F_DADFAILED;
2023
Sabrina Dubrocaec8add22017-06-29 16:56:54 +02002024 if (ifp->flags&IFA_F_TEMPORARY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 struct inet6_ifaddr *ifpub;
2026 spin_lock_bh(&ifp->lock);
2027 ifpub = ifp->ifpub;
2028 if (ifpub) {
2029 in6_ifa_hold(ifpub);
2030 spin_unlock_bh(&ifp->lock);
Eric Dumazetfffcefe2017-11-06 14:13:29 -08002031 ipv6_create_tempaddr(ifpub, ifp, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 in6_ifa_put(ifpub);
2033 } else {
2034 spin_unlock_bh(&ifp->lock);
2035 }
2036 ipv6_del_addr(ifp);
Sabrina Dubrocaec8add22017-06-29 16:56:54 +02002037 } else if (ifp->flags&IFA_F_PERMANENT || !dad_failed) {
2038 spin_lock_bh(&ifp->lock);
2039 addrconf_del_dad_work(ifp);
2040 ifp->flags |= IFA_F_TENTATIVE;
Sabrina Dubrocaf1c02cf2018-02-28 16:40:08 +01002041 if (dad_failed)
2042 ifp->flags &= ~IFA_F_OPTIMISTIC;
Sabrina Dubrocaec8add22017-06-29 16:56:54 +02002043 spin_unlock_bh(&ifp->lock);
2044 if (dad_failed)
2045 ipv6_ifa_notify(0, ifp);
2046 in6_ifa_put(ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01002047 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 ipv6_del_addr(ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01002049 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050}
2051
Herbert Xuf2344a12010-05-18 15:55:27 -07002052static int addrconf_dad_end(struct inet6_ifaddr *ifp)
2053{
2054 int err = -ENOENT;
2055
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01002056 spin_lock_bh(&ifp->lock);
Herbert Xuf2344a12010-05-18 15:55:27 -07002057 if (ifp->state == INET6_IFADDR_STATE_DAD) {
2058 ifp->state = INET6_IFADDR_STATE_POSTDAD;
2059 err = 0;
2060 }
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01002061 spin_unlock_bh(&ifp->lock);
Herbert Xuf2344a12010-05-18 15:55:27 -07002062
2063 return err;
2064}
2065
Vishwanath Paida13c592017-10-30 19:38:52 -04002066void addrconf_dad_failure(struct sk_buff *skb, struct inet6_ifaddr *ifp)
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002067{
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09002068 struct inet6_dev *idev = ifp->idev;
Hannes Frederic Sowa1855b7c2015-03-23 23:36:05 +01002069 struct net *net = dev_net(ifp->idev->dev);
Brian Haley9bdd8d42009-03-18 18:22:48 -07002070
Ursula Braun853dc2e2010-10-24 23:06:43 +00002071 if (addrconf_dad_end(ifp)) {
2072 in6_ifa_put(ifp);
Herbert Xuf2344a12010-05-18 15:55:27 -07002073 return;
Ursula Braun853dc2e2010-10-24 23:06:43 +00002074 }
Herbert Xuf2344a12010-05-18 15:55:27 -07002075
Vishwanath Paida13c592017-10-30 19:38:52 -04002076 net_info_ratelimited("%s: IPv6 duplicate address %pI6c used by %pM detected!\n",
2077 ifp->idev->dev->name, &ifp->addr, eth_hdr(skb)->h_source);
Brian Haley9bdd8d42009-03-18 18:22:48 -07002078
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01002079 spin_lock_bh(&ifp->lock);
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09002080
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01002081 if (ifp->flags & IFA_F_STABLE_PRIVACY) {
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01002082 struct in6_addr new_addr;
2083 struct inet6_ifaddr *ifp2;
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01002084 int retries = ifp->stable_privacy_retry + 1;
David Aherne6464b82018-05-27 08:09:53 -07002085 struct ifa6_config cfg = {
2086 .pfx = &new_addr,
2087 .plen = ifp->prefix_len,
2088 .ifa_flags = ifp->flags,
2089 .valid_lft = ifp->valid_lft,
2090 .preferred_lft = ifp->prefered_lft,
2091 .scope = ifp->scope,
2092 };
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01002093
Hannes Frederic Sowa1855b7c2015-03-23 23:36:05 +01002094 if (retries > net->ipv6.sysctl.idgen_retries) {
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01002095 net_info_ratelimited("%s: privacy stable address generation failed because of DAD conflicts!\n",
2096 ifp->idev->dev->name);
2097 goto errdad;
2098 }
2099
2100 new_addr = ifp->addr;
2101 if (ipv6_generate_stable_address(&new_addr, retries,
2102 idev))
2103 goto errdad;
2104
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01002105 spin_unlock_bh(&ifp->lock);
2106
2107 if (idev->cnf.max_addresses &&
2108 ipv6_count_addresses(idev) >=
2109 idev->cnf.max_addresses)
2110 goto lock_errdad;
2111
2112 net_info_ratelimited("%s: generating new stable privacy address because of DAD conflict\n",
2113 ifp->idev->dev->name);
2114
David Aherne6464b82018-05-27 08:09:53 -07002115 ifp2 = ipv6_add_addr(idev, &cfg, false, NULL);
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01002116 if (IS_ERR(ifp2))
2117 goto lock_errdad;
2118
2119 spin_lock_bh(&ifp2->lock);
2120 ifp2->stable_privacy_retry = retries;
2121 ifp2->state = INET6_IFADDR_STATE_PREDAD;
2122 spin_unlock_bh(&ifp2->lock);
2123
Hannes Frederic Sowa1855b7c2015-03-23 23:36:05 +01002124 addrconf_mod_dad_work(ifp2, net->ipv6.sysctl.idgen_delay);
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01002125 in6_ifa_put(ifp2);
2126lock_errdad:
2127 spin_lock_bh(&ifp->lock);
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09002128 }
2129
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01002130errdad:
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01002131 /* transition from _POSTDAD to _ERRDAD */
2132 ifp->state = INET6_IFADDR_STATE_ERRDAD;
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01002133 spin_unlock_bh(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01002134
2135 addrconf_mod_dad_work(ifp, 0);
Wei Yongjun751eb6b2016-09-05 16:06:31 +08002136 in6_ifa_put(ifp);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002137}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138
Sabrina Dubrocaa9ed4a22014-09-02 10:29:29 +02002139/* Join to solicited addr multicast group.
2140 * caller must hold RTNL */
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002141void addrconf_join_solict(struct net_device *dev, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142{
2143 struct in6_addr maddr;
2144
2145 if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
2146 return;
2147
2148 addrconf_addr_solict_mult(addr, &maddr);
2149 ipv6_dev_mc_inc(dev, &maddr);
2150}
2151
Sabrina Dubrocaa9ed4a22014-09-02 10:29:29 +02002152/* caller must hold RTNL */
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002153void addrconf_leave_solict(struct inet6_dev *idev, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154{
2155 struct in6_addr maddr;
2156
2157 if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP))
2158 return;
2159
2160 addrconf_addr_solict_mult(addr, &maddr);
2161 __ipv6_dev_mc_dec(idev, &maddr);
2162}
2163
Sabrina Dubrocaa9ed4a22014-09-02 10:29:29 +02002164/* caller must hold RTNL */
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03002165static void addrconf_join_anycast(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166{
2167 struct in6_addr addr;
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01002168
Hannes Frederic Sowa88ad3142014-01-06 17:53:14 +01002169 if (ifp->prefix_len >= 127) /* RFC 6164 */
Bjørn Mork2bda8a02011-07-05 23:04:13 +00002170 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
2172 if (ipv6_addr_any(&addr))
2173 return;
WANG Cong013b4d92014-09-11 15:35:11 -07002174 __ipv6_dev_ac_inc(ifp->idev, &addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175}
2176
Sabrina Dubrocaa9ed4a22014-09-02 10:29:29 +02002177/* caller must hold RTNL */
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03002178static void addrconf_leave_anycast(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179{
2180 struct in6_addr addr;
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01002181
Hannes Frederic Sowa88ad3142014-01-06 17:53:14 +01002182 if (ifp->prefix_len >= 127) /* RFC 6164 */
YOSHIFUJI Hideaki32019e62011-07-24 11:44:34 +00002183 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
2185 if (ipv6_addr_any(&addr))
2186 return;
2187 __ipv6_dev_ac_dec(ifp->idev, &addr);
2188}
2189
Alexander Aring8a7a4b42017-03-12 10:19:36 +02002190static int addrconf_ifid_6lowpan(u8 *eui, struct net_device *dev)
alex.bluesman.smirnov@gmail.com06a4c1c2012-05-10 03:25:52 +00002191{
Alexander Aring8a7a4b42017-03-12 10:19:36 +02002192 switch (dev->addr_len) {
2193 case ETH_ALEN:
Luiz Augusto von Dentz9dae2e02017-03-12 10:19:38 +02002194 memcpy(eui, dev->dev_addr, 3);
2195 eui[3] = 0xFF;
2196 eui[4] = 0xFE;
2197 memcpy(eui + 5, dev->dev_addr + 3, 3);
2198 break;
Alexander Aring8a7a4b42017-03-12 10:19:36 +02002199 case EUI64_ADDR_LEN:
2200 memcpy(eui, dev->dev_addr, EUI64_ADDR_LEN);
2201 eui[0] ^= 2;
2202 break;
2203 default:
alex.bluesman.smirnov@gmail.com06a4c1c2012-05-10 03:25:52 +00002204 return -1;
Alexander Aring8a7a4b42017-03-12 10:19:36 +02002205 }
2206
alex.bluesman.smirnov@gmail.com06a4c1c2012-05-10 03:25:52 +00002207 return 0;
2208}
2209
YOSHIFUJI Hideaki / 吉藤英明cb6bf352013-03-25 08:26:24 +00002210static int addrconf_ifid_ieee1394(u8 *eui, struct net_device *dev)
2211{
2212 union fwnet_hwaddr *ha;
2213
2214 if (dev->addr_len != FWNET_ALEN)
2215 return -1;
2216
2217 ha = (union fwnet_hwaddr *)dev->dev_addr;
2218
2219 memcpy(eui, &ha->uc.uniq_id, sizeof(ha->uc.uniq_id));
2220 eui[0] ^= 2;
2221 return 0;
2222}
2223
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08002224static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev)
2225{
2226 /* XXX: inherit EUI-64 from other interface -- yoshfuji */
2227 if (dev->addr_len != ARCNET_ALEN)
2228 return -1;
2229 memset(eui, 0, 7);
Eldad Zack8e5e8f32012-04-01 07:49:08 +00002230 eui[7] = *(u8 *)dev->dev_addr;
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08002231 return 0;
2232}
2233
2234static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev)
2235{
2236 if (dev->addr_len != INFINIBAND_ALEN)
2237 return -1;
2238 memcpy(eui, dev->dev_addr + 12, 8);
2239 eui[0] |= 2;
2240 return 0;
2241}
2242
stephen hemmingerc61393e2010-10-04 20:17:53 +00002243static int __ipv6_isatap_ifid(u8 *eui, __be32 addr)
YOSHIFUJI Hideakidfd982b2008-04-10 15:42:09 +09002244{
Sascha Hlusiak9af28512009-05-19 12:56:51 +00002245 if (addr == 0)
2246 return -1;
YOSHIFUJI Hideakidfd982b2008-04-10 15:42:09 +09002247 eui[0] = (ipv4_is_zeronet(addr) || ipv4_is_private_10(addr) ||
2248 ipv4_is_loopback(addr) || ipv4_is_linklocal_169(addr) ||
2249 ipv4_is_private_172(addr) || ipv4_is_test_192(addr) ||
2250 ipv4_is_anycast_6to4(addr) || ipv4_is_private_192(addr) ||
2251 ipv4_is_test_198(addr) || ipv4_is_multicast(addr) ||
2252 ipv4_is_lbcast(addr)) ? 0x00 : 0x02;
2253 eui[1] = 0;
2254 eui[2] = 0x5E;
2255 eui[3] = 0xFE;
2256 memcpy(eui + 4, &addr, 4);
2257 return 0;
2258}
YOSHIFUJI Hideakidfd982b2008-04-10 15:42:09 +09002259
2260static int addrconf_ifid_sit(u8 *eui, struct net_device *dev)
2261{
2262 if (dev->priv_flags & IFF_ISATAP)
2263 return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
2264 return -1;
2265}
2266
stephen hemmingeraee80b52011-06-08 10:44:30 +00002267static int addrconf_ifid_gre(u8 *eui, struct net_device *dev)
2268{
2269 return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
2270}
2271
Nicolas Dichtele8377352013-08-20 12:16:06 +02002272static int addrconf_ifid_ip6tnl(u8 *eui, struct net_device *dev)
2273{
2274 memcpy(eui, dev->perm_addr, 3);
2275 memcpy(eui + 5, dev->perm_addr + 3, 3);
2276 eui[3] = 0xFF;
2277 eui[4] = 0xFE;
2278 eui[0] ^= 2;
2279 return 0;
2280}
2281
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
2283{
2284 switch (dev->type) {
2285 case ARPHRD_ETHER:
2286 case ARPHRD_FDDI:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08002287 return addrconf_ifid_eui48(eui, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 case ARPHRD_ARCNET:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08002289 return addrconf_ifid_arcnet(eui, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 case ARPHRD_INFINIBAND:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08002291 return addrconf_ifid_infiniband(eui, dev);
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11002292 case ARPHRD_SIT:
YOSHIFUJI Hideakidfd982b2008-04-10 15:42:09 +09002293 return addrconf_ifid_sit(eui, dev);
stephen hemmingeraee80b52011-06-08 10:44:30 +00002294 case ARPHRD_IPGRE:
Felix Jia45ce0fd2017-01-26 16:59:18 +13002295 case ARPHRD_TUNNEL:
stephen hemmingeraee80b52011-06-08 10:44:30 +00002296 return addrconf_ifid_gre(eui, dev);
Jukka Rissanene74bccb82013-12-11 17:05:36 +02002297 case ARPHRD_6LOWPAN:
Alexander Aring8a7a4b42017-03-12 10:19:36 +02002298 return addrconf_ifid_6lowpan(eui, dev);
YOSHIFUJI Hideaki / 吉藤英明cb6bf352013-03-25 08:26:24 +00002299 case ARPHRD_IEEE1394:
2300 return addrconf_ifid_ieee1394(eui, dev);
Nicolas Dichtele8377352013-08-20 12:16:06 +02002301 case ARPHRD_TUNNEL6:
Felix Jia45ce0fd2017-01-26 16:59:18 +13002302 case ARPHRD_IP6GRE:
Subash Abhinov Kasiviswanathan9deb4412018-06-04 19:26:07 -06002303 case ARPHRD_RAWIP:
Nicolas Dichtele8377352013-08-20 12:16:06 +02002304 return addrconf_ifid_ip6tnl(eui, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 }
2306 return -1;
2307}
2308
2309static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
2310{
2311 int err = -1;
2312 struct inet6_ifaddr *ifp;
2313
2314 read_lock_bh(&idev->lock);
Hannes Frederic Sowa602582c2014-01-19 21:58:19 +01002315 list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) {
2316 if (ifp->scope > IFA_LINK)
2317 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
2319 memcpy(eui, ifp->addr.s6_addr+8, 8);
2320 err = 0;
2321 break;
2322 }
2323 }
2324 read_unlock_bh(&idev->lock);
2325 return err;
2326}
2327
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328/* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
Jiri Bohac9d6280d2016-10-13 18:50:02 +02002329static void ipv6_regen_rndid(struct inet6_dev *idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331regen:
YOSHIFUJI Hideaki955189e2006-03-20 16:54:09 -08002332 get_random_bytes(idev->rndid, sizeof(idev->rndid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 idev->rndid[0] &= ~0x02;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334
2335 /*
2336 * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
2337 * check if generated address is not inappropriate
2338 *
2339 * - Reserved subnet anycast (RFC 2526)
2340 * 11111101 11....11 1xxxxxxx
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11002341 * - ISATAP (RFC4214) 6.1
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 * 00-00-5E-FE-xx-xx-xx-xx
2343 * - value 0
2344 * - XXX: already assigned to an address on the device
2345 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002346 if (idev->rndid[0] == 0xfd &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff &&
2348 (idev->rndid[7]&0x80))
2349 goto regen;
2350 if ((idev->rndid[0]|idev->rndid[1]) == 0) {
2351 if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe)
2352 goto regen;
2353 if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00)
2354 goto regen;
2355 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356}
2357
Jiri Bohac9d6280d2016-10-13 18:50:02 +02002358static void ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr)
Eldad Zack8e5e8f32012-04-01 07:49:08 +00002359{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0)
Jiri Bohac9d6280d2016-10-13 18:50:02 +02002361 ipv6_regen_rndid(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363
2364/*
2365 * Add prefix route.
2366 */
2367
2368static void
David Ahern8308f3f2018-05-27 08:09:58 -07002369addrconf_prefix_route(struct in6_addr *pfx, int plen, u32 metric,
2370 struct net_device *dev, unsigned long expires,
2371 u32 flags, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372{
Thomas Graf86872cb2006-08-22 00:01:08 -07002373 struct fib6_config cfg = {
David Ahernca254492015-10-12 11:47:10 -07002374 .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_PREFIX,
David Ahern8308f3f2018-05-27 08:09:58 -07002375 .fc_metric = metric ? : IP6_RT_PRIO_ADDRCONF,
Thomas Graf86872cb2006-08-22 00:01:08 -07002376 .fc_ifindex = dev->ifindex,
2377 .fc_expires = expires,
2378 .fc_dst_len = plen,
2379 .fc_flags = RTF_UP | flags,
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002380 .fc_nlinfo.nl_net = dev_net(dev),
Stephen Hemmingerf410a1f2008-08-23 05:16:46 -07002381 .fc_protocol = RTPROT_KERNEL,
David Aherne8478e82018-04-17 17:33:13 -07002382 .fc_type = RTN_UNICAST,
Thomas Graf86872cb2006-08-22 00:01:08 -07002383 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002385 cfg.fc_dst = *pfx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386
2387 /* Prevent useless cloning on PtP SIT.
2388 This thing is done here expecting that the whole
2389 class of non-broadcast devices need not cloning.
2390 */
Amerigo Wang07a93622012-10-29 16:23:10 +00002391#if IS_ENABLED(CONFIG_IPV6_SIT)
Thomas Graf86872cb2006-08-22 00:01:08 -07002392 if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT))
2393 cfg.fc_flags |= RTF_NONEXTHOP;
Joerg Roedel0be669b2006-10-10 14:49:53 -07002394#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395
David Ahernacb54e32018-04-17 17:33:22 -07002396 ip6_route_add(&cfg, gfp_flags, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397}
2398
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002399
David Ahern8d1c8022018-04-17 17:33:26 -07002400static struct fib6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002401 int plen,
2402 const struct net_device *dev,
David Ahern2b2450c2019-03-27 20:53:52 -07002403 u32 flags, u32 noflags,
2404 bool no_gw)
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002405{
2406 struct fib6_node *fn;
David Ahern8d1c8022018-04-17 17:33:26 -07002407 struct fib6_info *rt = NULL;
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002408 struct fib6_table *table;
David Ahernca254492015-10-12 11:47:10 -07002409 u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_PREFIX;
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002410
David Ahernca254492015-10-12 11:47:10 -07002411 table = fib6_get_table(dev_net(dev), tb_id);
Ian Morris63159f22015-03-29 14:00:04 +01002412 if (!table)
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002413 return NULL;
2414
Wei Wang66f5d6c2017-10-06 12:06:10 -07002415 rcu_read_lock();
Wei Wang38fbeee2017-10-06 12:06:02 -07002416 fn = fib6_locate(&table->tb6_root, pfx, plen, NULL, 0, true);
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002417 if (!fn)
2418 goto out;
Martin KaFai Lau1f56a01f2015-04-28 13:03:03 -07002419
Wei Wang66f5d6c2017-10-06 12:06:10 -07002420 for_each_fib6_node_rt_rcu(fn) {
David Ahernf88d8ea2019-06-03 20:19:52 -07002421 /* prefix routes only use builtin fib6_nh */
2422 if (rt->nh)
2423 continue;
2424
David Ahern1cf844c2019-05-22 20:27:59 -07002425 if (rt->fib6_nh->fib_nh_dev->ifindex != dev->ifindex)
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002426 continue;
David Ahern1cf844c2019-05-22 20:27:59 -07002427 if (no_gw && rt->fib6_nh->fib_nh_gw_family)
David Ahern2b2450c2019-03-27 20:53:52 -07002428 continue;
David Ahern93c2fb22018-04-18 15:38:59 -07002429 if ((rt->fib6_flags & flags) != flags)
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002430 continue;
David Ahern93c2fb22018-04-18 15:38:59 -07002431 if ((rt->fib6_flags & noflags) != 0)
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002432 continue;
Wei Wange873e4b2018-07-21 20:56:32 -07002433 if (!fib6_info_hold_safe(rt))
2434 continue;
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002435 break;
2436 }
2437out:
Wei Wang66f5d6c2017-10-06 12:06:10 -07002438 rcu_read_unlock();
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002439 return rt;
2440}
2441
2442
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443/* Create "default" multicast route to the interface */
2444
2445static void addrconf_add_mroute(struct net_device *dev)
2446{
Thomas Graf86872cb2006-08-22 00:01:08 -07002447 struct fib6_config cfg = {
David Ahernca254492015-10-12 11:47:10 -07002448 .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_LOCAL,
Thomas Graf86872cb2006-08-22 00:01:08 -07002449 .fc_metric = IP6_RT_PRIO_ADDRCONF,
2450 .fc_ifindex = dev->ifindex,
2451 .fc_dst_len = 8,
2452 .fc_flags = RTF_UP,
David Aherne8478e82018-04-17 17:33:13 -07002453 .fc_type = RTN_UNICAST,
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002454 .fc_nlinfo.nl_net = dev_net(dev),
Thomas Graf86872cb2006-08-22 00:01:08 -07002455 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456
Thomas Graf86872cb2006-08-22 00:01:08 -07002457 ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0);
2458
Cong Wange500c6d2018-08-22 12:58:34 -07002459 ip6_route_add(&cfg, GFP_KERNEL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460}
2461
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
2463{
2464 struct inet6_dev *idev;
2465
2466 ASSERT_RTNL();
2467
Stephen Hemmingere21e8462010-03-20 16:09:01 -07002468 idev = ipv6_find_idev(dev);
Sabrina Dubrocadb0b99f2019-08-23 15:44:36 +02002469 if (IS_ERR(idev))
2470 return idev;
Brian Haley64e724f2010-07-20 10:34:30 +00002471
2472 if (idev->cnf.disable_ipv6)
2473 return ERR_PTR(-EACCES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474
2475 /* Add default multicast route */
David Ahernba46ee42016-06-13 13:44:18 -07002476 if (!(dev->flags & IFF_LOOPBACK) && !netif_is_l3_master(dev))
Li Wei4af04ab2011-12-06 21:23:45 +00002477 addrconf_add_mroute(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 return idev;
2480}
2481
Jiri Pirko53bd6742013-12-06 09:45:22 +01002482static void manage_tempaddrs(struct inet6_dev *idev,
2483 struct inet6_ifaddr *ifp,
2484 __u32 valid_lft, __u32 prefered_lft,
2485 bool create, unsigned long now)
2486{
2487 u32 flags;
2488 struct inet6_ifaddr *ift;
2489
2490 read_lock_bh(&idev->lock);
2491 /* update all temporary addresses in the list */
2492 list_for_each_entry(ift, &idev->tempaddr_list, tmp_list) {
2493 int age, max_valid, max_prefered;
2494
2495 if (ifp != ift->ifpub)
2496 continue;
2497
2498 /* RFC 4941 section 3.3:
2499 * If a received option will extend the lifetime of a public
2500 * address, the lifetimes of temporary addresses should
2501 * be extended, subject to the overall constraint that no
2502 * temporary addresses should ever remain "valid" or "preferred"
2503 * for a time longer than (TEMP_VALID_LIFETIME) or
2504 * (TEMP_PREFERRED_LIFETIME - DESYNC_FACTOR), respectively.
2505 */
2506 age = (now - ift->cstamp) / HZ;
2507 max_valid = idev->cnf.temp_valid_lft - age;
2508 if (max_valid < 0)
2509 max_valid = 0;
2510
2511 max_prefered = idev->cnf.temp_prefered_lft -
Jiri Bohac76506a92016-10-13 18:52:15 +02002512 idev->desync_factor - age;
Jiri Pirko53bd6742013-12-06 09:45:22 +01002513 if (max_prefered < 0)
2514 max_prefered = 0;
2515
2516 if (valid_lft > max_valid)
2517 valid_lft = max_valid;
2518
2519 if (prefered_lft > max_prefered)
2520 prefered_lft = max_prefered;
2521
2522 spin_lock(&ift->lock);
2523 flags = ift->flags;
2524 ift->valid_lft = valid_lft;
2525 ift->prefered_lft = prefered_lft;
2526 ift->tstamp = now;
2527 if (prefered_lft > 0)
2528 ift->flags &= ~IFA_F_DEPRECATED;
2529
2530 spin_unlock(&ift->lock);
2531 if (!(flags&IFA_F_TENTATIVE))
2532 ipv6_ifa_notify(0, ift);
2533 }
2534
2535 if ((create || list_empty(&idev->tempaddr_list)) &&
2536 idev->cnf.use_tempaddr > 0) {
2537 /* When a new public address is created as described
2538 * in [ADDRCONF], also create a new temporary address.
2539 * Also create a temporary address if it's enabled but
2540 * no temporary address currently exists.
2541 */
2542 read_unlock_bh(&idev->lock);
Eric Dumazetfffcefe2017-11-06 14:13:29 -08002543 ipv6_create_tempaddr(ifp, NULL, false);
Jiri Pirko53bd6742013-12-06 09:45:22 +01002544 } else {
2545 read_unlock_bh(&idev->lock);
2546 }
2547}
2548
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01002549static bool is_addr_mode_generate_stable(struct inet6_dev *idev)
2550{
Felix Jiad35a00b2017-01-26 16:59:17 +13002551 return idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY ||
2552 idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_RANDOM;
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01002553}
2554
Alexander Aringcc84b3c2016-06-15 21:20:24 +02002555int addrconf_prefix_rcv_add_addr(struct net *net, struct net_device *dev,
2556 const struct prefix_info *pinfo,
2557 struct inet6_dev *in6_dev,
2558 const struct in6_addr *addr, int addr_type,
2559 u32 addr_flags, bool sllao, bool tokenized,
2560 __u32 valid_lft, u32 prefered_lft)
Alexander Aring4f672232016-06-15 21:20:22 +02002561{
2562 struct inet6_ifaddr *ifp = ipv6_get_ifaddr(net, addr, dev, 1);
2563 int create = 0, update_lft = 0;
2564
2565 if (!ifp && valid_lft) {
2566 int max_addresses = in6_dev->cnf.max_addresses;
David Aherne6464b82018-05-27 08:09:53 -07002567 struct ifa6_config cfg = {
2568 .pfx = addr,
2569 .plen = pinfo->prefix_len,
2570 .ifa_flags = addr_flags,
2571 .valid_lft = valid_lft,
2572 .preferred_lft = prefered_lft,
2573 .scope = addr_type & IPV6_ADDR_SCOPE_MASK,
2574 };
Alexander Aring4f672232016-06-15 21:20:22 +02002575
2576#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
Matteo Croce35e015e2017-09-12 17:46:37 +02002577 if ((net->ipv6.devconf_all->optimistic_dad ||
2578 in6_dev->cnf.optimistic_dad) &&
Alexander Aring4f672232016-06-15 21:20:22 +02002579 !net->ipv6.devconf_all->forwarding && sllao)
David Aherne6464b82018-05-27 08:09:53 -07002580 cfg.ifa_flags |= IFA_F_OPTIMISTIC;
Alexander Aring4f672232016-06-15 21:20:22 +02002581#endif
2582
2583 /* Do not allow to create too much of autoconfigured
2584 * addresses; this would be too easy way to crash kernel.
2585 */
2586 if (!max_addresses ||
2587 ipv6_count_addresses(in6_dev) < max_addresses)
David Aherne6464b82018-05-27 08:09:53 -07002588 ifp = ipv6_add_addr(in6_dev, &cfg, false, NULL);
Alexander Aring4f672232016-06-15 21:20:22 +02002589
2590 if (IS_ERR_OR_NULL(ifp))
2591 return -1;
2592
Alexander Aring4f672232016-06-15 21:20:22 +02002593 create = 1;
2594 spin_lock_bh(&ifp->lock);
2595 ifp->flags |= IFA_F_MANAGETEMPADDR;
2596 ifp->cstamp = jiffies;
2597 ifp->tokenized = tokenized;
2598 spin_unlock_bh(&ifp->lock);
2599 addrconf_dad_start(ifp);
2600 }
2601
2602 if (ifp) {
2603 u32 flags;
2604 unsigned long now;
2605 u32 stored_lft;
2606
2607 /* update lifetime (RFC2462 5.5.3 e) */
2608 spin_lock_bh(&ifp->lock);
2609 now = jiffies;
2610 if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
2611 stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
2612 else
2613 stored_lft = 0;
Lorenzo Bianconif85f94b2018-04-16 17:52:59 +02002614 if (!create && stored_lft) {
Alexander Aring4f672232016-06-15 21:20:22 +02002615 const u32 minimum_lft = min_t(u32,
2616 stored_lft, MIN_VALID_LIFETIME);
2617 valid_lft = max(valid_lft, minimum_lft);
2618
2619 /* RFC4862 Section 5.5.3e:
2620 * "Note that the preferred lifetime of the
2621 * corresponding address is always reset to
2622 * the Preferred Lifetime in the received
2623 * Prefix Information option, regardless of
2624 * whether the valid lifetime is also reset or
2625 * ignored."
2626 *
2627 * So we should always update prefered_lft here.
2628 */
2629 update_lft = 1;
2630 }
2631
2632 if (update_lft) {
2633 ifp->valid_lft = valid_lft;
2634 ifp->prefered_lft = prefered_lft;
2635 ifp->tstamp = now;
2636 flags = ifp->flags;
2637 ifp->flags &= ~IFA_F_DEPRECATED;
2638 spin_unlock_bh(&ifp->lock);
2639
2640 if (!(flags&IFA_F_TENTATIVE))
2641 ipv6_ifa_notify(0, ifp);
2642 } else
2643 spin_unlock_bh(&ifp->lock);
2644
2645 manage_tempaddrs(in6_dev, ifp, valid_lft, prefered_lft,
2646 create, now);
2647
2648 in6_ifa_put(ifp);
2649 addrconf_verify();
2650 }
2651
2652 return 0;
2653}
Alexander Aringcc84b3c2016-06-15 21:20:24 +02002654EXPORT_SYMBOL_GPL(addrconf_prefix_rcv_add_addr);
Alexander Aring4f672232016-06-15 21:20:22 +02002655
Neil Hormane6bff992012-01-04 10:49:15 +00002656void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len, bool sllao)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657{
2658 struct prefix_info *pinfo;
2659 __u32 valid_lft;
2660 __u32 prefered_lft;
Alexander Aring4f672232016-06-15 21:20:22 +02002661 int addr_type, err;
Hannes Frederic Sowa64236f32015-03-23 23:36:02 +01002662 u32 addr_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 struct inet6_dev *in6_dev;
Brian Haley56d417b2009-06-01 03:07:33 -07002664 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665
2666 pinfo = (struct prefix_info *) opt;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002667
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 if (len < sizeof(struct prefix_info)) {
Joe Perchese32ac252018-03-26 08:35:01 -07002669 netdev_dbg(dev, "addrconf: prefix option too short\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 return;
2671 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002672
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 /*
2674 * Validation checks ([ADDRCONF], page 19)
2675 */
2676
2677 addr_type = ipv6_addr_type(&pinfo->prefix);
2678
2679 if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
2680 return;
2681
2682 valid_lft = ntohl(pinfo->valid);
2683 prefered_lft = ntohl(pinfo->prefered);
2684
2685 if (prefered_lft > valid_lft) {
Joe Perchese87cc472012-05-13 21:56:26 +00002686 net_warn_ratelimited("addrconf: prefix option has invalid lifetime\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 return;
2688 }
2689
2690 in6_dev = in6_dev_get(dev);
2691
Ian Morris63159f22015-03-29 14:00:04 +01002692 if (!in6_dev) {
Joe Perchese87cc472012-05-13 21:56:26 +00002693 net_dbg_ratelimited("addrconf: device %s not configured\n",
2694 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 return;
2696 }
2697
2698 /*
2699 * Two things going on here:
2700 * 1) Add routes for on-link prefixes
2701 * 2) Configure prefixes with the auto flag set
2702 */
2703
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002704 if (pinfo->onlink) {
David Ahern8d1c8022018-04-17 17:33:26 -07002705 struct fib6_info *rt;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002706 unsigned long rt_expires;
2707
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002708 /* Avoid arithmetic overflow. Really, we could
2709 * save rt_expires in seconds, likely valid_lft,
2710 * but it would require division in fib gc, that it
2711 * not good.
2712 */
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002713 if (HZ > USER_HZ)
2714 rt_expires = addrconf_timeout_fixup(valid_lft, HZ);
2715 else
2716 rt_expires = addrconf_timeout_fixup(valid_lft, USER_HZ);
YOSHIFUJI Hideaki3dd4bc62005-12-19 14:02:45 -08002717
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002718 if (addrconf_finite_timeout(rt_expires))
2719 rt_expires *= HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002721 rt = addrconf_get_prefix_route(&pinfo->prefix,
2722 pinfo->prefix_len,
2723 dev,
2724 RTF_ADDRCONF | RTF_PREFIX_RT,
David Ahern2b2450c2019-03-27 20:53:52 -07002725 RTF_DEFAULT, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002727 if (rt) {
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002728 /* Autoconf prefix route */
2729 if (valid_lft == 0) {
David Ahernafb1d4b52018-04-17 17:33:11 -07002730 ip6_del_rt(net, rt);
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002731 rt = NULL;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002732 } else if (addrconf_finite_timeout(rt_expires)) {
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002733 /* not infinity */
David Ahern14895682018-04-17 17:33:17 -07002734 fib6_set_expires(rt, jiffies + rt_expires);
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002735 } else {
David Ahern14895682018-04-17 17:33:17 -07002736 fib6_clean_expires(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 }
2738 } else if (valid_lft) {
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002739 clock_t expires = 0;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002740 int flags = RTF_ADDRCONF | RTF_PREFIX_RT;
2741 if (addrconf_finite_timeout(rt_expires)) {
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002742 /* not infinity */
2743 flags |= RTF_EXPIRES;
2744 expires = jiffies_to_clock_t(rt_expires);
2745 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
David Ahern8308f3f2018-05-27 08:09:58 -07002747 0, dev, expires, flags,
2748 GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 }
David Ahern93531c62018-04-17 17:33:25 -07002750 fib6_info_release(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 }
2752
2753 /* Try to figure out our local address for this prefix */
2754
2755 if (pinfo->autoconf && in6_dev->cnf.autoconf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 struct in6_addr addr;
Alexander Aringf997c552016-06-15 21:20:23 +02002757 bool tokenized = false, dev_addr_generated = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758
2759 if (pinfo->prefix_len == 64) {
2760 memcpy(&addr, &pinfo->prefix, 8);
Daniel Borkmannf53adae2013-04-08 04:01:30 +00002761
2762 if (!ipv6_addr_any(&in6_dev->token)) {
2763 read_lock_bh(&in6_dev->lock);
2764 memcpy(addr.s6_addr + 8,
2765 in6_dev->token.s6_addr + 8, 8);
2766 read_unlock_bh(&in6_dev->lock);
Daniel Borkmann617fe292013-04-09 03:47:16 +00002767 tokenized = true;
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01002768 } else if (is_addr_mode_generate_stable(in6_dev) &&
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01002769 !ipv6_generate_stable_address(&addr, 0,
2770 in6_dev)) {
Hannes Frederic Sowa64236f32015-03-23 23:36:02 +01002771 addr_flags |= IFA_F_STABLE_PRIVACY;
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01002772 goto ok;
Daniel Borkmannf53adae2013-04-08 04:01:30 +00002773 } else if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
2774 ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
Alexander Aring4f672232016-06-15 21:20:22 +02002775 goto put;
Alexander Aringf997c552016-06-15 21:20:23 +02002776 } else {
2777 dev_addr_generated = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 }
2779 goto ok;
2780 }
Joe Perchese87cc472012-05-13 21:56:26 +00002781 net_dbg_ratelimited("IPv6 addrconf: prefix with wrong length %d\n",
2782 pinfo->prefix_len);
Alexander Aring4f672232016-06-15 21:20:22 +02002783 goto put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784
2785ok:
Alexander Aring4f672232016-06-15 21:20:22 +02002786 err = addrconf_prefix_rcv_add_addr(net, dev, pinfo, in6_dev,
2787 &addr, addr_type,
2788 addr_flags, sllao,
2789 tokenized, valid_lft,
2790 prefered_lft);
2791 if (err)
2792 goto put;
Alexander Aringf997c552016-06-15 21:20:23 +02002793
2794 /* Ignore error case here because previous prefix add addr was
2795 * successful which will be notified.
2796 */
2797 ndisc_ops_prefix_rcv_add_addr(net, dev, pinfo, in6_dev, &addr,
2798 addr_type, addr_flags, sllao,
2799 tokenized, valid_lft,
2800 prefered_lft,
2801 dev_addr_generated);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 }
2803 inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
Alexander Aring4f672232016-06-15 21:20:22 +02002804put:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 in6_dev_put(in6_dev);
2806}
2807
2808/*
2809 * Set destination address.
2810 * Special case for SIT interfaces where we create a new "virtual"
2811 * device.
2812 */
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002813int addrconf_set_dstaddr(struct net *net, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814{
2815 struct in6_ifreq ireq;
2816 struct net_device *dev;
2817 int err = -EINVAL;
2818
2819 rtnl_lock();
2820
2821 err = -EFAULT;
2822 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2823 goto err_exit;
2824
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002825 dev = __dev_get_by_index(net, ireq.ifr6_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826
2827 err = -ENODEV;
Ian Morris63159f22015-03-29 14:00:04 +01002828 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 goto err_exit;
2830
Amerigo Wang07a93622012-10-29 16:23:10 +00002831#if IS_ENABLED(CONFIG_IPV6_SIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 if (dev->type == ARPHRD_SIT) {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -08002833 const struct net_device_ops *ops = dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 struct ifreq ifr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 struct ip_tunnel_parm p;
2836
2837 err = -EADDRNOTAVAIL;
2838 if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4))
2839 goto err_exit;
2840
2841 memset(&p, 0, sizeof(p));
2842 p.iph.daddr = ireq.ifr6_addr.s6_addr32[3];
2843 p.iph.saddr = 0;
2844 p.iph.version = 4;
2845 p.iph.ihl = 5;
2846 p.iph.protocol = IPPROTO_IPV6;
2847 p.iph.ttl = 64;
Stephen Hemmingerd20b3102008-01-21 00:48:43 -08002848 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -08002850 if (ops->ndo_do_ioctl) {
2851 mm_segment_t oldfs = get_fs();
2852
2853 set_fs(KERNEL_DS);
2854 err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
2855 set_fs(oldfs);
2856 } else
2857 err = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858
2859 if (err == 0) {
2860 err = -ENOBUFS;
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002861 dev = __dev_get_by_name(net, p.name);
2862 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 goto err_exit;
Petr Machata00f54e62018-12-06 17:05:36 +00002864 err = dev_open(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 }
2866 }
Joerg Roedel0be669b2006-10-10 14:49:53 -07002867#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868
2869err_exit:
2870 rtnl_unlock();
2871 return err;
2872}
2873
Madhu Challa93a714d2015-02-25 09:58:35 -08002874static int ipv6_mc_config(struct sock *sk, bool join,
2875 const struct in6_addr *addr, int ifindex)
2876{
2877 int ret;
2878
2879 ASSERT_RTNL();
2880
2881 lock_sock(sk);
2882 if (join)
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03002883 ret = ipv6_sock_mc_join(sk, ifindex, addr);
Madhu Challa93a714d2015-02-25 09:58:35 -08002884 else
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03002885 ret = ipv6_sock_mc_drop(sk, ifindex, addr);
Madhu Challa93a714d2015-02-25 09:58:35 -08002886 release_sock(sk);
2887
2888 return ret;
2889}
2890
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891/*
2892 * Manual configuration of address on an interface
2893 */
Jiri Pirko479840f2013-12-06 09:45:21 +01002894static int inet6_addr_add(struct net *net, int ifindex,
David Ahern19b15182018-05-27 08:09:54 -07002895 struct ifa6_config *cfg,
David Ahernde95e042017-10-18 09:56:54 -07002896 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897{
2898 struct inet6_ifaddr *ifp;
2899 struct inet6_dev *idev;
2900 struct net_device *dev;
Madhu Challa93a714d2015-02-25 09:58:35 -08002901 unsigned long timeout;
2902 clock_t expires;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002903 u32 flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904
2905 ASSERT_RTNL();
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002906
David Ahern19b15182018-05-27 08:09:54 -07002907 if (cfg->plen > 128)
Thomas Graf24ef0da2008-05-28 16:54:22 +02002908 return -EINVAL;
2909
Noriaki TAKAMIYA0778769d2006-07-28 18:12:10 +09002910 /* check the lifetime */
David Ahern19b15182018-05-27 08:09:54 -07002911 if (!cfg->valid_lft || cfg->preferred_lft > cfg->valid_lft)
Noriaki TAKAMIYA0778769d2006-07-28 18:12:10 +09002912 return -EINVAL;
2913
David Ahern19b15182018-05-27 08:09:54 -07002914 if (cfg->ifa_flags & IFA_F_MANAGETEMPADDR && cfg->plen != 64)
Jiri Pirko53bd6742013-12-06 09:45:22 +01002915 return -EINVAL;
2916
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002917 dev = __dev_get_by_index(net, ifindex);
2918 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 return -ENODEV;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002920
Brian Haley64e724f2010-07-20 10:34:30 +00002921 idev = addrconf_add_dev(dev);
2922 if (IS_ERR(idev))
2923 return PTR_ERR(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924
David Ahern19b15182018-05-27 08:09:54 -07002925 if (cfg->ifa_flags & IFA_F_MCAUTOJOIN) {
Madhu Challa93a714d2015-02-25 09:58:35 -08002926 int ret = ipv6_mc_config(net->ipv6.mc_autojoin_sk,
David Ahern19b15182018-05-27 08:09:54 -07002927 true, cfg->pfx, ifindex);
Madhu Challa93a714d2015-02-25 09:58:35 -08002928
2929 if (ret < 0)
2930 return ret;
2931 }
2932
David Ahern19b15182018-05-27 08:09:54 -07002933 cfg->scope = ipv6_addr_scope(cfg->pfx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934
David Ahern19b15182018-05-27 08:09:54 -07002935 timeout = addrconf_timeout_fixup(cfg->valid_lft, HZ);
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002936 if (addrconf_finite_timeout(timeout)) {
2937 expires = jiffies_to_clock_t(timeout * HZ);
David Ahern19b15182018-05-27 08:09:54 -07002938 cfg->valid_lft = timeout;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002939 flags = RTF_EXPIRES;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002940 } else {
2941 expires = 0;
2942 flags = 0;
David Ahern19b15182018-05-27 08:09:54 -07002943 cfg->ifa_flags |= IFA_F_PERMANENT;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002944 }
Noriaki TAKAMIYA0778769d2006-07-28 18:12:10 +09002945
David Ahern19b15182018-05-27 08:09:54 -07002946 timeout = addrconf_timeout_fixup(cfg->preferred_lft, HZ);
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002947 if (addrconf_finite_timeout(timeout)) {
2948 if (timeout == 0)
David Ahern19b15182018-05-27 08:09:54 -07002949 cfg->ifa_flags |= IFA_F_DEPRECATED;
2950 cfg->preferred_lft = timeout;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002951 }
Noriaki TAKAMIYA0778769d2006-07-28 18:12:10 +09002952
David Ahern19b15182018-05-27 08:09:54 -07002953 ifp = ipv6_add_addr(idev, cfg, true, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 if (!IS_ERR(ifp)) {
David Ahern19b15182018-05-27 08:09:54 -07002955 if (!(cfg->ifa_flags & IFA_F_NOPREFIXROUTE)) {
David Ahern8308f3f2018-05-27 08:09:58 -07002956 addrconf_prefix_route(&ifp->addr, ifp->prefix_len,
2957 ifp->rt_priority, dev, expires,
2958 flags, GFP_KERNEL);
Thomas Haller761aac72014-01-15 15:36:58 +01002959 }
2960
Lorenzo Bianconia2d481b2018-04-17 11:54:39 +02002961 /* Send a netlink notification if DAD is enabled and
2962 * optimistic flag is not set
2963 */
2964 if (!(ifp->flags & (IFA_F_OPTIMISTIC | IFA_F_NODAD)))
2965 ipv6_ifa_notify(0, ifp);
Neil Horman95c385b2007-04-25 17:08:10 -07002966 /*
2967 * Note that section 3.1 of RFC 4429 indicates
2968 * that the Optimistic flag should not be set for
2969 * manually configured addresses
2970 */
David S. Millercf22f9a2012-04-14 21:37:40 -04002971 addrconf_dad_start(ifp);
David Ahern19b15182018-05-27 08:09:54 -07002972 if (cfg->ifa_flags & IFA_F_MANAGETEMPADDR)
2973 manage_tempaddrs(idev, ifp, cfg->valid_lft,
2974 cfg->preferred_lft, true, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 in6_ifa_put(ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01002976 addrconf_verify_rtnl();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 return 0;
David Ahern19b15182018-05-27 08:09:54 -07002978 } else if (cfg->ifa_flags & IFA_F_MCAUTOJOIN) {
2979 ipv6_mc_config(net->ipv6.mc_autojoin_sk, false,
2980 cfg->pfx, ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 }
2982
2983 return PTR_ERR(ifp);
2984}
2985
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02002986static int inet6_addr_del(struct net *net, int ifindex, u32 ifa_flags,
2987 const struct in6_addr *pfx, unsigned int plen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988{
2989 struct inet6_ifaddr *ifp;
2990 struct inet6_dev *idev;
2991 struct net_device *dev;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002992
Thomas Graf24ef0da2008-05-28 16:54:22 +02002993 if (plen > 128)
2994 return -EINVAL;
2995
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002996 dev = __dev_get_by_index(net, ifindex);
2997 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 return -ENODEV;
2999
Ian Morrise5d08d72014-11-23 21:28:43 +00003000 idev = __in6_dev_get(dev);
Ian Morris63159f22015-03-29 14:00:04 +01003001 if (!idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 return -ENXIO;
3003
3004 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00003005 list_for_each_entry(ifp, &idev->addr_list, if_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 if (ifp->prefix_len == plen &&
3007 ipv6_addr_equal(pfx, &ifp->addr)) {
3008 in6_ifa_hold(ifp);
3009 read_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003010
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02003011 if (!(ifp->flags & IFA_F_TEMPORARY) &&
3012 (ifa_flags & IFA_F_MANAGETEMPADDR))
3013 manage_tempaddrs(idev, ifp, 0, 0, false,
3014 jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 ipv6_del_addr(ifp);
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02003016 addrconf_verify_rtnl();
Madhu Challa93a714d2015-02-25 09:58:35 -08003017 if (ipv6_addr_is_multicast(pfx)) {
3018 ipv6_mc_config(net->ipv6.mc_autojoin_sk,
3019 false, pfx, dev->ifindex);
3020 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 return 0;
3022 }
3023 }
3024 read_unlock_bh(&idev->lock);
3025 return -EADDRNOTAVAIL;
3026}
3027
3028
Daniel Lezcanoaf284932008-03-05 10:46:57 -08003029int addrconf_add_ifaddr(struct net *net, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030{
David Ahern19b15182018-05-27 08:09:54 -07003031 struct ifa6_config cfg = {
3032 .ifa_flags = IFA_F_PERMANENT,
3033 .preferred_lft = INFINITY_LIFE_TIME,
3034 .valid_lft = INFINITY_LIFE_TIME,
3035 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 struct in6_ifreq ireq;
3037 int err;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003038
Eric W. Biedermanaf31f412012-11-16 03:03:06 +00003039 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 return -EPERM;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003041
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
3043 return -EFAULT;
3044
David Ahern19b15182018-05-27 08:09:54 -07003045 cfg.pfx = &ireq.ifr6_addr;
3046 cfg.plen = ireq.ifr6_prefixlen;
3047
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 rtnl_lock();
David Ahern19b15182018-05-27 08:09:54 -07003049 err = inet6_addr_add(net, ireq.ifr6_ifindex, &cfg, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 rtnl_unlock();
3051 return err;
3052}
3053
Daniel Lezcanoaf284932008-03-05 10:46:57 -08003054int addrconf_del_ifaddr(struct net *net, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055{
3056 struct in6_ifreq ireq;
3057 int err;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003058
Eric W. Biedermanaf31f412012-11-16 03:03:06 +00003059 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 return -EPERM;
3061
3062 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
3063 return -EFAULT;
3064
3065 rtnl_lock();
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02003066 err = inet6_addr_del(net, ireq.ifr6_ifindex, 0, &ireq.ifr6_addr,
Daniel Lezcanoaf284932008-03-05 10:46:57 -08003067 ireq.ifr6_prefixlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 rtnl_unlock();
3069 return err;
3070}
3071
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08003072static void add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
3073 int plen, int scope)
3074{
3075 struct inet6_ifaddr *ifp;
David Aherne6464b82018-05-27 08:09:53 -07003076 struct ifa6_config cfg = {
3077 .pfx = addr,
3078 .plen = plen,
3079 .ifa_flags = IFA_F_PERMANENT,
3080 .valid_lft = INFINITY_LIFE_TIME,
3081 .preferred_lft = INFINITY_LIFE_TIME,
3082 .scope = scope
3083 };
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08003084
David Aherne6464b82018-05-27 08:09:53 -07003085 ifp = ipv6_add_addr(idev, &cfg, true, NULL);
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08003086 if (!IS_ERR(ifp)) {
3087 spin_lock_bh(&ifp->lock);
3088 ifp->flags &= ~IFA_F_TENTATIVE;
3089 spin_unlock_bh(&ifp->lock);
Paolo Abeni764d3be2016-11-22 16:57:40 +01003090 rt_genid_bump_ipv6(dev_net(idev->dev));
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08003091 ipv6_ifa_notify(RTM_NEWADDR, ifp);
3092 in6_ifa_put(ifp);
3093 }
3094}
3095
Amerigo Wang07a93622012-10-29 16:23:10 +00003096#if IS_ENABLED(CONFIG_IPV6_SIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097static void sit_add_v4_addrs(struct inet6_dev *idev)
3098{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 struct in6_addr addr;
3100 struct net_device *dev;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09003101 struct net *net = dev_net(idev->dev);
Nicolas Dichtel929c9cf2013-11-14 13:51:05 +01003102 int scope, plen;
Nicolas Dichtelf0e2acf2013-11-14 13:51:06 +01003103 u32 pflags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104
3105 ASSERT_RTNL();
3106
3107 memset(&addr, 0, sizeof(struct in6_addr));
3108 memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
3109
3110 if (idev->dev->flags&IFF_POINTOPOINT) {
3111 addr.s6_addr32[0] = htonl(0xfe800000);
3112 scope = IFA_LINK;
Nicolas Dichtel929c9cf2013-11-14 13:51:05 +01003113 plen = 64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114 } else {
3115 scope = IPV6_ADDR_COMPATv4;
Nicolas Dichtel929c9cf2013-11-14 13:51:05 +01003116 plen = 96;
Nicolas Dichtelf0e2acf2013-11-14 13:51:06 +01003117 pflags |= RTF_NONEXTHOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118 }
3119
3120 if (addr.s6_addr32[3]) {
Nicolas Dichtel929c9cf2013-11-14 13:51:05 +01003121 add_addr(idev, &addr, plen, scope);
David Ahern8308f3f2018-05-27 08:09:58 -07003122 addrconf_prefix_route(&addr, plen, 0, idev->dev, 0, pflags,
Cong Wange500c6d2018-08-22 12:58:34 -07003123 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 return;
3125 }
3126
Benjamin Thery6fda7352008-03-05 10:47:47 -08003127 for_each_netdev(net, dev) {
Eldad Zack8e5e8f32012-04-01 07:49:08 +00003128 struct in_device *in_dev = __in_dev_get_rtnl(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 if (in_dev && (dev->flags & IFF_UP)) {
Eldad Zack8e5e8f32012-04-01 07:49:08 +00003130 struct in_ifaddr *ifa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 int flag = scope;
3132
Florian Westphalcd5a4112019-05-31 18:27:07 +02003133 in_dev_for_each_ifa_rtnl(ifa, in_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 addr.s6_addr32[3] = ifa->ifa_local;
3135
3136 if (ifa->ifa_scope == RT_SCOPE_LINK)
3137 continue;
3138 if (ifa->ifa_scope >= RT_SCOPE_HOST) {
3139 if (idev->dev->flags&IFF_POINTOPOINT)
3140 continue;
3141 flag |= IFA_HOST;
3142 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08003144 add_addr(idev, &addr, plen, flag);
David Ahern8308f3f2018-05-27 08:09:58 -07003145 addrconf_prefix_route(&addr, plen, 0, idev->dev,
Cong Wange500c6d2018-08-22 12:58:34 -07003146 0, pflags, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 }
3148 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003149 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150}
Joerg Roedel0be669b2006-10-10 14:49:53 -07003151#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152
3153static void init_loopback(struct net_device *dev)
3154{
3155 struct inet6_dev *idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156
3157 /* ::1 */
3158
3159 ASSERT_RTNL();
3160
Ian Morrise5d08d72014-11-23 21:28:43 +00003161 idev = ipv6_find_idev(dev);
Sabrina Dubrocadb0b99f2019-08-23 15:44:36 +02003162 if (IS_ERR(idev)) {
Joe Perches91df42b2012-05-15 14:11:54 +00003163 pr_debug("%s: add_dev failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 return;
3165 }
3166
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08003167 add_addr(idev, &in6addr_loopback, 128, IFA_HOST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168}
3169
Alexander Aring2ad3ed52016-06-15 21:20:17 +02003170void addrconf_add_linklocal(struct inet6_dev *idev,
3171 const struct in6_addr *addr, u32 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172{
David Aherne6464b82018-05-27 08:09:53 -07003173 struct ifa6_config cfg = {
3174 .pfx = addr,
3175 .plen = 64,
3176 .ifa_flags = flags | IFA_F_PERMANENT,
3177 .valid_lft = INFINITY_LIFE_TIME,
3178 .preferred_lft = INFINITY_LIFE_TIME,
3179 .scope = IFA_LINK
3180 };
Eldad Zack8e5e8f32012-04-01 07:49:08 +00003181 struct inet6_ifaddr *ifp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182
Neil Horman95c385b2007-04-25 17:08:10 -07003183#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
Matteo Croce35e015e2017-09-12 17:46:37 +02003184 if ((dev_net(idev->dev)->ipv6.devconf_all->optimistic_dad ||
3185 idev->cnf.optimistic_dad) &&
David Miller702beb82008-07-20 18:17:02 -07003186 !dev_net(idev->dev)->ipv6.devconf_all->forwarding)
David Aherne6464b82018-05-27 08:09:53 -07003187 cfg.ifa_flags |= IFA_F_OPTIMISTIC;
Neil Horman95c385b2007-04-25 17:08:10 -07003188#endif
3189
David Aherne6464b82018-05-27 08:09:53 -07003190 ifp = ipv6_add_addr(idev, &cfg, true, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 if (!IS_ERR(ifp)) {
David Ahern8308f3f2018-05-27 08:09:58 -07003192 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, 0, idev->dev,
David Ahernacb54e32018-04-17 17:33:22 -07003193 0, 0, GFP_ATOMIC);
David S. Millercf22f9a2012-04-14 21:37:40 -04003194 addrconf_dad_start(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 in6_ifa_put(ifp);
3196 }
3197}
Alexander Aring2ad3ed52016-06-15 21:20:17 +02003198EXPORT_SYMBOL_GPL(addrconf_add_linklocal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01003200static bool ipv6_reserved_interfaceid(struct in6_addr address)
3201{
3202 if ((address.s6_addr32[2] | address.s6_addr32[3]) == 0)
3203 return true;
3204
3205 if (address.s6_addr32[2] == htonl(0x02005eff) &&
3206 ((address.s6_addr32[3] & htonl(0xfe000000)) == htonl(0xfe000000)))
3207 return true;
3208
3209 if (address.s6_addr32[2] == htonl(0xfdffffff) &&
3210 ((address.s6_addr32[3] & htonl(0xffffff80)) == htonl(0xffffff80)))
3211 return true;
3212
3213 return false;
3214}
3215
3216static int ipv6_generate_stable_address(struct in6_addr *address,
3217 u8 dad_count,
3218 const struct inet6_dev *idev)
3219{
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01003220 static DEFINE_SPINLOCK(lock);
3221 static __u32 digest[SHA_DIGEST_WORDS];
3222 static __u32 workspace[SHA_WORKSPACE_WORDS];
3223
3224 static union {
3225 char __data[SHA_MESSAGE_BYTES];
3226 struct {
3227 struct in6_addr secret;
Hannes Frederic Sowaff402172015-03-24 11:05:28 +01003228 __be32 prefix[2];
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01003229 unsigned char hwaddr[MAX_ADDR_LEN];
3230 u8 dad_count;
3231 } __packed;
3232 } data;
3233
3234 struct in6_addr secret;
3235 struct in6_addr temp;
3236 struct net *net = dev_net(idev->dev);
3237
3238 BUILD_BUG_ON(sizeof(data.__data) != sizeof(data));
3239
3240 if (idev->cnf.stable_secret.initialized)
3241 secret = idev->cnf.stable_secret.secret;
3242 else if (net->ipv6.devconf_dflt->stable_secret.initialized)
3243 secret = net->ipv6.devconf_dflt->stable_secret.secret;
3244 else
3245 return -1;
3246
3247retry:
3248 spin_lock_bh(&lock);
3249
3250 sha_init(digest);
3251 memset(&data, 0, sizeof(data));
3252 memset(workspace, 0, sizeof(workspace));
3253 memcpy(data.hwaddr, idev->dev->perm_addr, idev->dev->addr_len);
Hannes Frederic Sowaff402172015-03-24 11:05:28 +01003254 data.prefix[0] = address->s6_addr32[0];
3255 data.prefix[1] = address->s6_addr32[1];
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01003256 data.secret = secret;
3257 data.dad_count = dad_count;
3258
3259 sha_transform(digest, data.__data, workspace);
3260
3261 temp = *address;
Hannes Frederic Sowaff402172015-03-24 11:05:28 +01003262 temp.s6_addr32[2] = (__force __be32)digest[0];
3263 temp.s6_addr32[3] = (__force __be32)digest[1];
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01003264
3265 spin_unlock_bh(&lock);
3266
3267 if (ipv6_reserved_interfaceid(temp)) {
3268 dad_count++;
Hannes Frederic Sowa1855b7c2015-03-23 23:36:05 +01003269 if (dad_count > dev_net(idev->dev)->ipv6.sysctl.idgen_retries)
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01003270 return -1;
3271 goto retry;
3272 }
3273
3274 *address = temp;
3275 return 0;
3276}
3277
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003278static void ipv6_gen_mode_random_init(struct inet6_dev *idev)
3279{
3280 struct ipv6_stable_secret *s = &idev->cnf.stable_secret;
3281
3282 if (s->initialized)
3283 return;
3284 s = &idev->cnf.stable_secret;
3285 get_random_bytes(&s->secret, sizeof(s->secret));
3286 s->initialized = true;
3287}
3288
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003289static void addrconf_addr_gen(struct inet6_dev *idev, bool prefix_route)
3290{
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01003291 struct in6_addr addr;
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003292
David Ahernca254492015-10-12 11:47:10 -07003293 /* no link local addresses on L3 master devices */
3294 if (netif_is_l3_master(idev->dev))
3295 return;
3296
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01003297 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
3298
Felix Jiad35a00b2017-01-26 16:59:17 +13003299 switch (idev->cnf.addr_gen_mode) {
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003300 case IN6_ADDR_GEN_MODE_RANDOM:
3301 ipv6_gen_mode_random_init(idev);
3302 /* fallthrough */
3303 case IN6_ADDR_GEN_MODE_STABLE_PRIVACY:
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01003304 if (!ipv6_generate_stable_address(&addr, 0, idev))
Hannes Frederic Sowa64236f32015-03-23 23:36:02 +01003305 addrconf_add_linklocal(idev, &addr,
3306 IFA_F_STABLE_PRIVACY);
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01003307 else if (prefix_route)
David Ahern8308f3f2018-05-27 08:09:58 -07003308 addrconf_prefix_route(&addr, 64, 0, idev->dev,
David Ahernacb54e32018-04-17 17:33:22 -07003309 0, 0, GFP_KERNEL);
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003310 break;
3311 case IN6_ADDR_GEN_MODE_EUI64:
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003312 /* addrconf_add_linklocal also adds a prefix_route and we
3313 * only need to care about prefix routes if ipv6_generate_eui64
3314 * couldn't generate one.
3315 */
3316 if (ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) == 0)
Hannes Frederic Sowa64236f32015-03-23 23:36:02 +01003317 addrconf_add_linklocal(idev, &addr, 0);
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003318 else if (prefix_route)
David Ahern8308f3f2018-05-27 08:09:58 -07003319 addrconf_prefix_route(&addr, 64, 0, idev->dev,
David Ahern27b10602018-04-18 15:39:06 -07003320 0, 0, GFP_KERNEL);
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003321 break;
3322 case IN6_ADDR_GEN_MODE_NONE:
3323 default:
3324 /* will not add any link local address */
3325 break;
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003326 }
3327}
3328
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329static void addrconf_dev_config(struct net_device *dev)
3330{
Eldad Zack8e5e8f32012-04-01 07:49:08 +00003331 struct inet6_dev *idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332
3333 ASSERT_RTNL();
3334
Herbert Xu74235a22007-06-14 13:02:55 -07003335 if ((dev->type != ARPHRD_ETHER) &&
3336 (dev->type != ARPHRD_FDDI) &&
Herbert Xu74235a22007-06-14 13:02:55 -07003337 (dev->type != ARPHRD_ARCNET) &&
alex.bluesman.smirnov@gmail.com06a4c1c2012-05-10 03:25:52 +00003338 (dev->type != ARPHRD_INFINIBAND) &&
Nicolas Dichtele8377352013-08-20 12:16:06 +02003339 (dev->type != ARPHRD_IEEE1394) &&
Jukka Rissanene74bccb82013-12-11 17:05:36 +02003340 (dev->type != ARPHRD_TUNNEL6) &&
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003341 (dev->type != ARPHRD_6LOWPAN) &&
Felix Jia45ce0fd2017-01-26 16:59:18 +13003342 (dev->type != ARPHRD_IP6GRE) &&
3343 (dev->type != ARPHRD_IPGRE) &&
3344 (dev->type != ARPHRD_TUNNEL) &&
Subash Abhinov Kasiviswanathan9deb4412018-06-04 19:26:07 -06003345 (dev->type != ARPHRD_NONE) &&
3346 (dev->type != ARPHRD_RAWIP)) {
Herbert Xu74235a22007-06-14 13:02:55 -07003347 /* Alas, we support only Ethernet autoconfiguration. */
3348 return;
3349 }
3350
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351 idev = addrconf_add_dev(dev);
Brian Haley64e724f2010-07-20 10:34:30 +00003352 if (IS_ERR(idev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353 return;
3354
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003355 /* this device type has no EUI support */
3356 if (dev->type == ARPHRD_NONE &&
Felix Jiad35a00b2017-01-26 16:59:17 +13003357 idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_EUI64)
3358 idev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_RANDOM;
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003359
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003360 addrconf_addr_gen(idev, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361}
3362
Amerigo Wang07a93622012-10-29 16:23:10 +00003363#if IS_ENABLED(CONFIG_IPV6_SIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364static void addrconf_sit_config(struct net_device *dev)
3365{
3366 struct inet6_dev *idev;
3367
3368 ASSERT_RTNL();
3369
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003370 /*
3371 * Configure the tunnel with one of our IPv4
3372 * addresses... we should configure all of
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373 * our v4 addrs in the tunnel
3374 */
3375
Ian Morrise5d08d72014-11-23 21:28:43 +00003376 idev = ipv6_find_idev(dev);
Sabrina Dubrocadb0b99f2019-08-23 15:44:36 +02003377 if (IS_ERR(idev)) {
Joe Perches91df42b2012-05-15 14:11:54 +00003378 pr_debug("%s: add_dev failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 return;
3380 }
3381
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11003382 if (dev->priv_flags & IFF_ISATAP) {
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003383 addrconf_addr_gen(idev, false);
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11003384 return;
3385 }
3386
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387 sit_add_v4_addrs(idev);
3388
Nicolas Dichtel62b54dd2012-10-01 23:19:14 +00003389 if (dev->flags&IFF_POINTOPOINT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390 addrconf_add_mroute(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391}
Joerg Roedel0be669b2006-10-10 14:49:53 -07003392#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393
Amerigo Wang07a93622012-10-29 16:23:10 +00003394#if IS_ENABLED(CONFIG_NET_IPGRE)
stephen hemmingeraee80b52011-06-08 10:44:30 +00003395static void addrconf_gre_config(struct net_device *dev)
3396{
3397 struct inet6_dev *idev;
stephen hemmingeraee80b52011-06-08 10:44:30 +00003398
stephen hemmingeraee80b52011-06-08 10:44:30 +00003399 ASSERT_RTNL();
3400
Ian Morrise5d08d72014-11-23 21:28:43 +00003401 idev = ipv6_find_idev(dev);
Sabrina Dubrocadb0b99f2019-08-23 15:44:36 +02003402 if (IS_ERR(idev)) {
Joe Perches91df42b2012-05-15 14:11:54 +00003403 pr_debug("%s: add_dev failed\n", __func__);
stephen hemmingeraee80b52011-06-08 10:44:30 +00003404 return;
3405 }
3406
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003407 addrconf_addr_gen(idev, true);
Hannes Frederic Sowad9e4ce62015-10-08 18:19:39 +02003408 if (dev->flags & IFF_POINTOPOINT)
3409 addrconf_add_mroute(dev);
stephen hemmingeraee80b52011-06-08 10:44:30 +00003410}
3411#endif
3412
David Ahernafb1d4b52018-04-17 17:33:11 -07003413static int fixup_permanent_addr(struct net *net,
3414 struct inet6_dev *idev,
David Ahernf1705ec2016-02-24 09:25:37 -08003415 struct inet6_ifaddr *ifp)
3416{
David Ahern93c2fb22018-04-18 15:38:59 -07003417 /* !fib6_node means the host route was removed from the
David Ahern8048ced2017-04-25 09:17:29 -07003418 * FIB, for example, if 'lo' device is taken down. In that
3419 * case regenerate the host route.
3420 */
David Ahern93c2fb22018-04-18 15:38:59 -07003421 if (!ifp->rt || !ifp->rt->fib6_node) {
David Ahern360a9882018-04-18 15:39:00 -07003422 struct fib6_info *f6i, *prev;
David Ahernf1705ec2016-02-24 09:25:37 -08003423
David Ahern360a9882018-04-18 15:39:00 -07003424 f6i = addrconf_f6i_alloc(net, idev, &ifp->addr, false,
3425 GFP_ATOMIC);
3426 if (IS_ERR(f6i))
3427 return PTR_ERR(f6i);
David Ahernf1705ec2016-02-24 09:25:37 -08003428
David Ahern8048ced2017-04-25 09:17:29 -07003429 /* ifp->rt can be accessed outside of rtnl */
3430 spin_lock(&ifp->lock);
3431 prev = ifp->rt;
David Ahern360a9882018-04-18 15:39:00 -07003432 ifp->rt = f6i;
David Ahern8048ced2017-04-25 09:17:29 -07003433 spin_unlock(&ifp->lock);
3434
David Ahern93531c62018-04-17 17:33:25 -07003435 fib6_info_release(prev);
David Ahernf1705ec2016-02-24 09:25:37 -08003436 }
3437
3438 if (!(ifp->flags & IFA_F_NOPREFIXROUTE)) {
3439 addrconf_prefix_route(&ifp->addr, ifp->prefix_len,
David Ahern8308f3f2018-05-27 08:09:58 -07003440 ifp->rt_priority, idev->dev, 0, 0,
3441 GFP_ATOMIC);
David Ahernf1705ec2016-02-24 09:25:37 -08003442 }
3443
David Ahern6d717132017-05-02 14:43:44 -07003444 if (ifp->state == INET6_IFADDR_STATE_PREDAD)
3445 addrconf_dad_start(ifp);
David Ahernf1705ec2016-02-24 09:25:37 -08003446
3447 return 0;
3448}
3449
David Ahernafb1d4b52018-04-17 17:33:11 -07003450static void addrconf_permanent_addr(struct net *net, struct net_device *dev)
David Ahernf1705ec2016-02-24 09:25:37 -08003451{
3452 struct inet6_ifaddr *ifp, *tmp;
3453 struct inet6_dev *idev;
3454
3455 idev = __in6_dev_get(dev);
3456 if (!idev)
3457 return;
3458
3459 write_lock_bh(&idev->lock);
3460
3461 list_for_each_entry_safe(ifp, tmp, &idev->addr_list, if_list) {
3462 if ((ifp->flags & IFA_F_PERMANENT) &&
David Ahernafb1d4b52018-04-17 17:33:11 -07003463 fixup_permanent_addr(net, idev, ifp) < 0) {
David Ahernf1705ec2016-02-24 09:25:37 -08003464 write_unlock_bh(&idev->lock);
Eric Dumazete669b862017-10-30 22:47:09 -07003465 in6_ifa_hold(ifp);
David Ahernf1705ec2016-02-24 09:25:37 -08003466 ipv6_del_addr(ifp);
3467 write_lock_bh(&idev->lock);
3468
3469 net_info_ratelimited("%s: Failed to add prefix route for address %pI6c; dropping\n",
3470 idev->dev->name, &ifp->addr);
3471 }
3472 }
3473
3474 write_unlock_bh(&idev->lock);
3475}
3476
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003477static int addrconf_notify(struct notifier_block *this, unsigned long event,
Jiri Pirko351638e2013-05-28 01:30:21 +00003478 void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479{
Jiri Pirko351638e2013-05-28 01:30:21 +00003480 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Hangbin Liu896585d2018-11-21 21:52:33 +08003481 struct netdev_notifier_change_info *change_info;
David Ahern4f7f34e2016-04-07 11:10:41 -07003482 struct netdev_notifier_changeupper_info *info;
Eric Dumazet748e2d92012-08-22 21:50:59 +00003483 struct inet6_dev *idev = __in6_dev_get(dev);
WANG Cong60abc0b2017-06-21 14:34:58 -07003484 struct net *net = dev_net(dev);
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09003485 int run_pending = 0;
Herbert Xub217d612007-07-30 17:04:52 -07003486 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487
Stephen Hemmingere21e8462010-03-20 16:09:01 -07003488 switch (event) {
YOSHIFUJI Hideaki45ba9dd2007-02-15 02:07:27 +09003489 case NETDEV_REGISTER:
Herbert Xu74235a22007-06-14 13:02:55 -07003490 if (!idev && dev->mtu >= IPV6_MIN_MTU) {
YOSHIFUJI Hideaki45ba9dd2007-02-15 02:07:27 +09003491 idev = ipv6_add_dev(dev);
WANG Conga317a2f2014-07-25 15:25:09 -07003492 if (IS_ERR(idev))
3493 return notifier_from_errno(PTR_ERR(idev));
YOSHIFUJI Hideaki45ba9dd2007-02-15 02:07:27 +09003494 }
3495 break;
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003496
Alexander Duyckb7b0b1d2015-10-26 11:06:33 -07003497 case NETDEV_CHANGEMTU:
3498 /* if MTU under IPV6_MIN_MTU stop IPv6 on this interface. */
3499 if (dev->mtu < IPV6_MIN_MTU) {
WANG Cong60abc0b2017-06-21 14:34:58 -07003500 addrconf_ifdown(dev, dev != net->loopback_dev);
Alexander Duyckb7b0b1d2015-10-26 11:06:33 -07003501 break;
3502 }
3503
3504 if (idev) {
3505 rt6_mtu_change(dev, dev->mtu);
3506 idev->cnf.mtu6 = dev->mtu;
3507 break;
3508 }
3509
3510 /* allocate new idev */
3511 idev = ipv6_add_dev(dev);
3512 if (IS_ERR(idev))
3513 break;
3514
3515 /* device is still not ready */
3516 if (!(idev->if_flags & IF_READY))
3517 break;
3518
3519 run_pending = 1;
3520
3521 /* fall through */
3522
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523 case NETDEV_UP:
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003524 case NETDEV_CHANGE:
Jay Vosburghc2edacf2007-07-09 10:42:47 -07003525 if (dev->flags & IFF_SLAVE)
3526 break;
3527
WANG Cong3ce62a82014-09-11 15:07:16 -07003528 if (idev && idev->cnf.disable_ipv6)
3529 break;
3530
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003531 if (event == NETDEV_UP) {
David Ahern38bd10c2016-04-21 20:56:12 -07003532 /* restore routes for permanent addresses */
David Ahernafb1d4b52018-04-17 17:33:11 -07003533 addrconf_permanent_addr(net, dev);
David Ahern38bd10c2016-04-21 20:56:12 -07003534
Mike Manning1f372c72017-09-25 22:01:36 +01003535 if (!addrconf_link_ready(dev)) {
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003536 /* device is not ready yet. */
Lubomir Rintel7c62b8d2019-01-21 14:54:20 +01003537 pr_debug("ADDRCONF(NETDEV_UP): %s: link is not ready\n",
3538 dev->name);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003539 break;
3540 }
Kristian Slavov99081042006-02-08 16:10:53 -08003541
Evgeniy Polyakovd31c7b82007-11-30 23:36:08 +11003542 if (!idev && dev->mtu >= IPV6_MIN_MTU)
3543 idev = ipv6_add_dev(dev);
3544
WANG Conga317a2f2014-07-25 15:25:09 -07003545 if (!IS_ERR_OR_NULL(idev)) {
Kristian Slavov99081042006-02-08 16:10:53 -08003546 idev->if_flags |= IF_READY;
Benjamin Therye3ec6cf2008-11-05 01:43:57 -08003547 run_pending = 1;
3548 }
Alexander Duyckb7b0b1d2015-10-26 11:06:33 -07003549 } else if (event == NETDEV_CHANGE) {
Mike Manning1f372c72017-09-25 22:01:36 +01003550 if (!addrconf_link_ready(dev)) {
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003551 /* device is still not ready. */
Ido Schimmel27c6fa72018-01-07 12:45:05 +02003552 rt6_sync_down_dev(dev, event);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003553 break;
3554 }
3555
Hangbin Liu896585d2018-11-21 21:52:33 +08003556 if (!IS_ERR_OR_NULL(idev)) {
Linus Lüssinga088d1d2017-02-03 08:11:03 +01003557 if (idev->if_flags & IF_READY) {
3558 /* device is already configured -
3559 * but resend MLD reports, we might
3560 * have roamed and need to update
3561 * multicast snooping switches
3562 */
3563 ipv6_mc_up(idev);
Hangbin Liu896585d2018-11-21 21:52:33 +08003564 change_info = ptr;
3565 if (change_info->flags_changed & IFF_NOARP)
3566 addrconf_dad_run(idev, true);
Ido Schimmel27c6fa72018-01-07 12:45:05 +02003567 rt6_sync_up(dev, RTNH_F_LINKDOWN);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003568 break;
Linus Lüssinga088d1d2017-02-03 08:11:03 +01003569 }
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003570 idev->if_flags |= IF_READY;
3571 }
3572
Joe Perchesf3213832012-05-15 14:11:53 +00003573 pr_info("ADDRCONF(NETDEV_CHANGE): %s: link becomes ready\n",
3574 dev->name);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003575
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09003576 run_pending = 1;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003577 }
3578
Stephen Hemmingere21e8462010-03-20 16:09:01 -07003579 switch (dev->type) {
Amerigo Wang07a93622012-10-29 16:23:10 +00003580#if IS_ENABLED(CONFIG_IPV6_SIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581 case ARPHRD_SIT:
3582 addrconf_sit_config(dev);
3583 break;
Joerg Roedel0be669b2006-10-10 14:49:53 -07003584#endif
Amerigo Wang07a93622012-10-29 16:23:10 +00003585#if IS_ENABLED(CONFIG_NET_IPGRE)
stephen hemmingeraee80b52011-06-08 10:44:30 +00003586 case ARPHRD_IPGRE:
3587 addrconf_gre_config(dev);
3588 break;
3589#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590 case ARPHRD_LOOPBACK:
3591 init_loopback(dev);
3592 break;
3593
3594 default:
3595 addrconf_dev_config(dev);
3596 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07003597 }
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003598
WANG Conga317a2f2014-07-25 15:25:09 -07003599 if (!IS_ERR_OR_NULL(idev)) {
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09003600 if (run_pending)
Hangbin Liu896585d2018-11-21 21:52:33 +08003601 addrconf_dad_run(idev, false);
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09003602
Ido Schimmel2127d952018-01-07 12:45:03 +02003603 /* Device has an address by now */
3604 rt6_sync_up(dev, RTNH_F_DEAD);
3605
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003606 /*
3607 * If the MTU changed during the interface down,
3608 * when the interface up, the changed MTU must be
3609 * reflected in the idev as well as routers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610 */
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003611 if (idev->cnf.mtu6 != dev->mtu &&
3612 dev->mtu >= IPV6_MIN_MTU) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613 rt6_mtu_change(dev, dev->mtu);
3614 idev->cnf.mtu6 = dev->mtu;
3615 }
3616 idev->tstamp = jiffies;
3617 inet6_ifinfo_notify(RTM_NEWLINK, idev);
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003618
3619 /*
3620 * If the changed mtu during down is lower than
3621 * IPV6_MIN_MTU stop IPv6 on this interface.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622 */
3623 if (dev->mtu < IPV6_MIN_MTU)
WANG Cong60abc0b2017-06-21 14:34:58 -07003624 addrconf_ifdown(dev, dev != net->loopback_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625 }
3626 break;
3627
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628 case NETDEV_DOWN:
3629 case NETDEV_UNREGISTER:
3630 /*
3631 * Remove all addresses from this interface.
3632 */
3633 addrconf_ifdown(dev, event != NETDEV_DOWN);
3634 break;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003635
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636 case NETDEV_CHANGENAME:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637 if (idev) {
Stephen Hemminger5632c512007-04-28 21:16:39 -07003638 snmp6_unregister_dev(idev);
Pavel Emelyanov408c4762008-01-10 17:41:21 -08003639 addrconf_sysctl_unregister(idev);
WANG Conga317a2f2014-07-25 15:25:09 -07003640 err = addrconf_sysctl_register(idev);
Herbert Xub217d612007-07-30 17:04:52 -07003641 if (err)
3642 return notifier_from_errno(err);
WANG Conga317a2f2014-07-25 15:25:09 -07003643 err = snmp6_register_dev(idev);
3644 if (err) {
3645 addrconf_sysctl_unregister(idev);
3646 return notifier_from_errno(err);
3647 }
Stephen Hemminger5632c512007-04-28 21:16:39 -07003648 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649 break;
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003650
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00003651 case NETDEV_PRE_TYPE_CHANGE:
3652 case NETDEV_POST_TYPE_CHANGE:
Andrew Lunn3ef09522015-12-03 21:12:32 +01003653 if (idev)
3654 addrconf_type_change(dev, event);
Moni Shoua75c78502009-09-15 02:37:40 -07003655 break;
David Ahern4f7f34e2016-04-07 11:10:41 -07003656
3657 case NETDEV_CHANGEUPPER:
3658 info = ptr;
3659
3660 /* flush all routes if dev is linked to or unlinked from
3661 * an L3 master device (e.g., VRF)
3662 */
3663 if (info->upper_dev && netif_is_l3_master(info->upper_dev))
3664 addrconf_ifdown(dev, 0);
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07003665 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666
3667 return NOTIFY_OK;
3668}
3669
3670/*
3671 * addrconf module should be notified of a device going up
3672 */
3673static struct notifier_block ipv6_dev_notf = {
3674 .notifier_call = addrconf_notify,
WANG Cong242d3a42017-05-08 10:12:13 -07003675 .priority = ADDRCONF_NOTIFY_PRIORITY,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676};
3677
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00003678static void addrconf_type_change(struct net_device *dev, unsigned long event)
Moni Shoua75c78502009-09-15 02:37:40 -07003679{
3680 struct inet6_dev *idev;
3681 ASSERT_RTNL();
3682
3683 idev = __in6_dev_get(dev);
3684
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00003685 if (event == NETDEV_POST_TYPE_CHANGE)
Moni Shoua75c78502009-09-15 02:37:40 -07003686 ipv6_mc_remap(idev);
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00003687 else if (event == NETDEV_PRE_TYPE_CHANGE)
Moni Shoua75c78502009-09-15 02:37:40 -07003688 ipv6_mc_unmap(idev);
3689}
3690
David Ahern70af9212016-04-08 12:01:21 -07003691static bool addr_is_local(const struct in6_addr *addr)
3692{
3693 return ipv6_addr_type(addr) &
3694 (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK);
3695}
3696
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697static int addrconf_ifdown(struct net_device *dev, int how)
3698{
Ido Schimmel4c981e22018-01-07 12:45:04 +02003699 unsigned long event = how ? NETDEV_UNREGISTER : NETDEV_DOWN;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09003700 struct net *net = dev_net(dev);
stephen hemminger502a2ff2010-03-17 20:31:13 +00003701 struct inet6_dev *idev;
David Ahernf1705ec2016-02-24 09:25:37 -08003702 struct inet6_ifaddr *ifa, *tmp;
Ivan Vecera0aef78a2018-04-24 19:51:29 +02003703 bool keep_addr = false;
David S. Miller73a8bd72011-01-23 23:27:15 -08003704 int state, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705
3706 ASSERT_RTNL();
3707
Ido Schimmel4c981e22018-01-07 12:45:04 +02003708 rt6_disable_ip(dev, event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709
3710 idev = __in6_dev_get(dev);
Ian Morris63159f22015-03-29 14:00:04 +01003711 if (!idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712 return -ENODEV;
3713
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003714 /*
3715 * Step 1: remove reference to ipv6 device from parent device.
3716 * Do not dev_put!
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717 */
Denis V. Lunev439e2382008-04-03 13:30:17 -07003718 if (how) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719 idev->dead = 1;
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07003720
3721 /* protected by rtnl_lock */
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00003722 RCU_INIT_POINTER(dev->ip6_ptr, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723
3724 /* Step 1.5: remove snmp6 entry */
3725 snmp6_unregister_dev(idev);
3726
3727 }
3728
David Ahernf1705ec2016-02-24 09:25:37 -08003729 /* combine the user config with event to determine if permanent
3730 * addresses are to be removed from address hash table
3731 */
Ivan Vecera0aef78a2018-04-24 19:51:29 +02003732 if (!how && !idev->cnf.disable_ipv6) {
3733 /* aggregate the system setting and interface setting */
3734 int _keep_addr = net->ipv6.devconf_all->keep_addr_on_down;
3735
3736 if (!_keep_addr)
3737 _keep_addr = idev->cnf.keep_addr_on_down;
3738
3739 keep_addr = (_keep_addr > 0);
3740 }
David Ahernf1705ec2016-02-24 09:25:37 -08003741
David S. Miller73a8bd72011-01-23 23:27:15 -08003742 /* Step 2: clear hash table */
3743 for (i = 0; i < IN6_ADDR_HSIZE; i++) {
3744 struct hlist_head *h = &inet6_addr_lst[i];
David S. Miller73a8bd72011-01-23 23:27:15 -08003745
3746 spin_lock_bh(&addrconf_hash_lock);
Ian Morris67ba4152014-08-24 21:53:10 +01003747restart:
Sasha Levinb67bfe02013-02-27 17:06:00 -08003748 hlist_for_each_entry_rcu(ifa, h, addr_lst) {
David S. Miller73a8bd72011-01-23 23:27:15 -08003749 if (ifa->idev == idev) {
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003750 addrconf_del_dad_work(ifa);
David Ahernf1705ec2016-02-24 09:25:37 -08003751 /* combined flag + permanent flag decide if
3752 * address is retained on a down event
3753 */
3754 if (!keep_addr ||
David Ahern70af9212016-04-08 12:01:21 -07003755 !(ifa->flags & IFA_F_PERMANENT) ||
3756 addr_is_local(&ifa->addr)) {
David Ahernf1705ec2016-02-24 09:25:37 -08003757 hlist_del_init_rcu(&ifa->addr_lst);
3758 goto restart;
3759 }
David S. Miller73a8bd72011-01-23 23:27:15 -08003760 }
3761 }
3762 spin_unlock_bh(&addrconf_hash_lock);
3763 }
3764
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765 write_lock_bh(&idev->lock);
3766
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003767 addrconf_del_rs_timer(idev);
3768
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003769 /* Step 2: clear flags for stateless addrconf */
Denis V. Lunev439e2382008-04-03 13:30:17 -07003770 if (!how)
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003771 idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003773 /* Step 3: clear tempaddr list */
stephen hemminger372e6c82010-03-17 20:31:09 +00003774 while (!list_empty(&idev->tempaddr_list)) {
3775 ifa = list_first_entry(&idev->tempaddr_list,
3776 struct inet6_ifaddr, tmp_list);
3777 list_del(&ifa->tmp_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778 write_unlock_bh(&idev->lock);
3779 spin_lock_bh(&ifa->lock);
3780
3781 if (ifa->ifpub) {
3782 in6_ifa_put(ifa->ifpub);
3783 ifa->ifpub = NULL;
3784 }
3785 spin_unlock_bh(&ifa->lock);
3786 in6_ifa_put(ifa);
3787 write_lock_bh(&idev->lock);
3788 }
stephen hemminger8f37ada2010-03-03 08:19:59 +00003789
David Ahernf1705ec2016-02-24 09:25:37 -08003790 list_for_each_entry_safe(ifa, tmp, &idev->addr_list, if_list) {
David Ahern8d1c8022018-04-17 17:33:26 -07003791 struct fib6_info *rt = NULL;
Rabin Vincenta2d6cbb2017-04-10 08:36:39 +02003792 bool keep;
David Ahern38bd10c2016-04-21 20:56:12 -07003793
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003794 addrconf_del_dad_work(ifa);
stephen hemminger84e8b802010-03-02 13:32:46 +00003795
Rabin Vincenta2d6cbb2017-04-10 08:36:39 +02003796 keep = keep_addr && (ifa->flags & IFA_F_PERMANENT) &&
3797 !addr_is_local(&ifa->addr);
Rabin Vincenta2d6cbb2017-04-10 08:36:39 +02003798
David S. Miller73a8bd72011-01-23 23:27:15 -08003799 write_unlock_bh(&idev->lock);
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01003800 spin_lock_bh(&ifa->lock);
David Ahernf1705ec2016-02-24 09:25:37 -08003801
Rabin Vincenta2d6cbb2017-04-10 08:36:39 +02003802 if (keep) {
David Ahernf1705ec2016-02-24 09:25:37 -08003803 /* set state to skip the notifier below */
3804 state = INET6_IFADDR_STATE_DEAD;
David Ahern6d717132017-05-02 14:43:44 -07003805 ifa->state = INET6_IFADDR_STATE_PREDAD;
David Ahernf1705ec2016-02-24 09:25:37 -08003806 if (!(ifa->flags & IFA_F_NODAD))
3807 ifa->flags |= IFA_F_TENTATIVE;
David Ahern38bd10c2016-04-21 20:56:12 -07003808
3809 rt = ifa->rt;
3810 ifa->rt = NULL;
David Ahernf1705ec2016-02-24 09:25:37 -08003811 } else {
3812 state = ifa->state;
3813 ifa->state = INET6_IFADDR_STATE_DEAD;
David Ahernf1705ec2016-02-24 09:25:37 -08003814 }
3815
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01003816 spin_unlock_bh(&ifa->lock);
stephen hemminger84e8b802010-03-02 13:32:46 +00003817
David Ahern38bd10c2016-04-21 20:56:12 -07003818 if (rt)
David Ahernafb1d4b52018-04-17 17:33:11 -07003819 ip6_del_rt(net, rt);
David Ahern38bd10c2016-04-21 20:56:12 -07003820
David S. Miller73a8bd72011-01-23 23:27:15 -08003821 if (state != INET6_IFADDR_STATE_DEAD) {
3822 __ipv6_ifa_notify(RTM_DELADDR, ifa);
Cong Wangf88c91d2013-04-14 23:18:43 +08003823 inet6addr_notifier_call_chain(NETDEV_DOWN, ifa);
Mike Manningea06f712016-07-22 18:32:11 +01003824 } else {
3825 if (idev->cnf.forwarding)
3826 addrconf_leave_anycast(ifa);
3827 addrconf_leave_solict(ifa->idev, &ifa->addr);
stephen hemminger27bdb2a2010-04-12 05:41:32 +00003828 }
stephen hemminger8f37ada2010-03-03 08:19:59 +00003829
David S. Miller73a8bd72011-01-23 23:27:15 -08003830 write_lock_bh(&idev->lock);
Eric Dumazet8ef802a2017-10-07 19:30:23 -07003831 if (!keep) {
3832 list_del_rcu(&ifa->if_list);
3833 in6_ifa_put(ifa);
3834 }
David S. Miller73a8bd72011-01-23 23:27:15 -08003835 }
stephen hemminger8f37ada2010-03-03 08:19:59 +00003836
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837 write_unlock_bh(&idev->lock);
3838
Sabrina Dubroca381f4dc2014-09-10 23:23:02 +02003839 /* Step 5: Discard anycast and multicast list */
3840 if (how) {
3841 ipv6_ac_destroy_dev(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003842 ipv6_mc_destroy_dev(idev);
Sabrina Dubroca381f4dc2014-09-10 23:23:02 +02003843 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844 ipv6_mc_down(idev);
Sabrina Dubroca381f4dc2014-09-10 23:23:02 +02003845 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003846
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847 idev->tstamp = jiffies;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003848
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003849 /* Last: Shot the device (if unregistered) */
Denis V. Lunev439e2382008-04-03 13:30:17 -07003850 if (how) {
Pavel Emelyanov408c4762008-01-10 17:41:21 -08003851 addrconf_sysctl_unregister(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852 neigh_parms_release(&nd_tbl, idev->nd_parms);
3853 neigh_ifdown(&nd_tbl, dev);
3854 in6_dev_put(idev);
3855 }
3856 return 0;
3857}
3858
Kees Cooke99e88a2017-10-16 14:43:17 -07003859static void addrconf_rs_timer(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003860{
Kees Cooke99e88a2017-10-16 14:43:17 -07003861 struct inet6_dev *idev = from_timer(idev, t, rs_timer);
Cong Wangcaf92bc2013-08-31 13:44:32 +08003862 struct net_device *dev = idev->dev;
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003863 struct in6_addr lladdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003864
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003865 write_lock(&idev->lock);
stephen hemminger5b2a1952010-03-02 13:32:45 +00003866 if (idev->dead || !(idev->if_flags & IF_READY))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867 goto out;
3868
Shmulik Ladkani9ba2add2012-12-02 01:44:53 +00003869 if (!ipv6_accept_ra(idev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870 goto out;
stephen hemminger5b2a1952010-03-02 13:32:45 +00003871
3872 /* Announcement received after solicitation was sent */
3873 if (idev->if_flags & IF_RA_RCVD)
3874 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07003876 if (idev->rs_probes++ < idev->cnf.rtr_solicits || idev->cnf.rtr_solicits < 0) {
Cong Wangcaf92bc2013-08-31 13:44:32 +08003877 write_unlock(&idev->lock);
3878 if (!ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
3879 ndisc_send_rs(dev, &lladdr,
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003880 &in6addr_linklocal_allrouters);
3881 else
Cong Wangcaf92bc2013-08-31 13:44:32 +08003882 goto put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883
Cong Wangcaf92bc2013-08-31 13:44:32 +08003884 write_lock(&idev->lock);
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07003885 idev->rs_interval = rfc3315_s14_backoff_update(
3886 idev->rs_interval, idev->cnf.rtr_solicit_max_interval);
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003887 /* The wait after the last probe can be shorter */
3888 addrconf_mod_rs_timer(idev, (idev->rs_probes ==
3889 idev->cnf.rtr_solicits) ?
3890 idev->cnf.rtr_solicit_delay :
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07003891 idev->rs_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893 /*
3894 * Note: we do not support deprecated "all on-link"
3895 * assumption any longer.
3896 */
Joe Perches91df42b2012-05-15 14:11:54 +00003897 pr_debug("%s: no IPv6 routers present\n", idev->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003898 }
3899
3900out:
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003901 write_unlock(&idev->lock);
Cong Wangcaf92bc2013-08-31 13:44:32 +08003902put:
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003903 in6_dev_put(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904}
3905
3906/*
3907 * Duplicate Address Detection
3908 */
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003909static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
3910{
3911 unsigned long rand_num;
3912 struct inet6_dev *idev = ifp->idev;
Erik Nordmarkadc176c2016-12-02 14:00:08 -08003913 u64 nonce;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003914
Neil Horman95c385b2007-04-25 17:08:10 -07003915 if (ifp->flags & IFA_F_OPTIMISTIC)
3916 rand_num = 0;
3917 else
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05003918 rand_num = prandom_u32() % (idev->cnf.rtr_solicit_delay ? : 1);
Neil Horman95c385b2007-04-25 17:08:10 -07003919
Erik Nordmarkadc176c2016-12-02 14:00:08 -08003920 nonce = 0;
3921 if (idev->cnf.enhanced_dad ||
3922 dev_net(idev->dev)->ipv6.devconf_all->enhanced_dad) {
3923 do
3924 get_random_bytes(&nonce, 6);
3925 while (nonce == 0);
3926 }
3927 ifp->dad_nonce = nonce;
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003928 ifp->dad_probes = idev->cnf.dad_transmits;
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003929 addrconf_mod_dad_work(ifp, rand_num);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003930}
3931
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003932static void addrconf_dad_begin(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933{
3934 struct inet6_dev *idev = ifp->idev;
3935 struct net_device *dev = idev->dev;
Paolo Abeni764d3be2016-11-22 16:57:40 +01003936 bool bump_id, notify = false;
David Ahernafb1d4b52018-04-17 17:33:11 -07003937 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938
3939 addrconf_join_solict(dev, &ifp->addr);
3940
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05003941 prandom_seed((__force u32) ifp->addr.s6_addr32[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942
3943 read_lock_bh(&idev->lock);
Herbert Xu622ccdf2010-05-18 15:56:06 -07003944 spin_lock(&ifp->lock);
Herbert Xue9d3e082010-05-18 15:36:06 -07003945 if (ifp->state == INET6_IFADDR_STATE_DEAD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947
David Ahernafb1d4b52018-04-17 17:33:11 -07003948 net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949 if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
David Ahernafb1d4b52018-04-17 17:33:11 -07003950 (net->ipv6.devconf_all->accept_dad < 1 &&
Matteo Crocea2d3f3e2017-10-05 19:03:05 +02003951 idev->cnf.accept_dad < 1) ||
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003952 !(ifp->flags&IFA_F_TENTATIVE) ||
3953 ifp->flags & IFA_F_NODAD) {
David Ahernc76fe2d2018-01-25 20:16:29 -08003954 bool send_na = false;
3955
3956 if (ifp->flags & IFA_F_TENTATIVE &&
3957 !(ifp->flags & IFA_F_OPTIMISTIC))
3958 send_na = true;
Paolo Abeni764d3be2016-11-22 16:57:40 +01003959 bump_id = ifp->flags & IFA_F_TENTATIVE;
Brian Haleycc411d02009-09-09 14:41:32 +00003960 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
stephen hemminger21809fa2010-02-08 19:48:52 +00003961 spin_unlock(&ifp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962 read_unlock_bh(&idev->lock);
3963
David Ahernc76fe2d2018-01-25 20:16:29 -08003964 addrconf_dad_completed(ifp, bump_id, send_na);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965 return;
3966 }
3967
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08003968 if (!(idev->if_flags & IF_READY)) {
stephen hemminger21809fa2010-02-08 19:48:52 +00003969 spin_unlock(&ifp->lock);
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08003970 read_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003971 /*
Masatake YAMATO590a9882009-06-09 10:41:12 +09003972 * If the device is not ready:
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003973 * - keep it tentative if it is a permanent address.
3974 * - otherwise, kill it.
3975 */
3976 in6_ifa_hold(ifp);
Brian Haleycc411d02009-09-09 14:41:32 +00003977 addrconf_dad_stop(ifp, 0);
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08003978 return;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003979 }
Neil Horman95c385b2007-04-25 17:08:10 -07003980
3981 /*
3982 * Optimistic nodes can start receiving
3983 * Frames right away
3984 */
Erik Kline7fd25612014-10-28 18:11:14 +09003985 if (ifp->flags & IFA_F_OPTIMISTIC) {
David Ahernafb1d4b52018-04-17 17:33:11 -07003986 ip6_ins_rt(net, ifp->rt);
3987 if (ipv6_use_optimistic_addr(net, idev)) {
Erik Kline7fd25612014-10-28 18:11:14 +09003988 /* Because optimistic nodes can use this address,
3989 * notify listeners. If DAD fails, RTM_DELADDR is sent.
3990 */
subashab@codeaurora.org16186a82016-02-02 02:11:10 +00003991 notify = true;
Erik Kline7fd25612014-10-28 18:11:14 +09003992 }
3993 }
Neil Horman95c385b2007-04-25 17:08:10 -07003994
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08003995 addrconf_dad_kick(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996out:
Herbert Xu622ccdf2010-05-18 15:56:06 -07003997 spin_unlock(&ifp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998 read_unlock_bh(&idev->lock);
subashab@codeaurora.org16186a82016-02-02 02:11:10 +00003999 if (notify)
4000 ipv6_ifa_notify(RTM_NEWADDR, ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001}
4002
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004003static void addrconf_dad_start(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004{
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004005 bool begin_dad = false;
4006
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01004007 spin_lock_bh(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004008 if (ifp->state != INET6_IFADDR_STATE_DEAD) {
4009 ifp->state = INET6_IFADDR_STATE_PREDAD;
4010 begin_dad = true;
4011 }
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01004012 spin_unlock_bh(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004013
4014 if (begin_dad)
4015 addrconf_mod_dad_work(ifp, 0);
4016}
4017
4018static void addrconf_dad_work(struct work_struct *w)
4019{
4020 struct inet6_ifaddr *ifp = container_of(to_delayed_work(w),
4021 struct inet6_ifaddr,
4022 dad_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023 struct inet6_dev *idev = ifp->idev;
Paolo Abeni764d3be2016-11-22 16:57:40 +01004024 bool bump_id, disable_ipv6 = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025 struct in6_addr mcaddr;
4026
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004027 enum {
4028 DAD_PROCESS,
4029 DAD_BEGIN,
4030 DAD_ABORT,
4031 } action = DAD_PROCESS;
4032
4033 rtnl_lock();
4034
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01004035 spin_lock_bh(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004036 if (ifp->state == INET6_IFADDR_STATE_PREDAD) {
4037 action = DAD_BEGIN;
4038 ifp->state = INET6_IFADDR_STATE_DAD;
4039 } else if (ifp->state == INET6_IFADDR_STATE_ERRDAD) {
4040 action = DAD_ABORT;
4041 ifp->state = INET6_IFADDR_STATE_POSTDAD;
Mike Manning85b51b12016-08-18 14:39:40 +01004042
Matteo Croce35e015e2017-09-12 17:46:37 +02004043 if ((dev_net(idev->dev)->ipv6.devconf_all->accept_dad > 1 ||
4044 idev->cnf.accept_dad > 1) &&
4045 !idev->cnf.disable_ipv6 &&
Mike Manning85b51b12016-08-18 14:39:40 +01004046 !(ifp->flags & IFA_F_STABLE_PRIVACY)) {
4047 struct in6_addr addr;
4048
4049 addr.s6_addr32[0] = htonl(0xfe800000);
4050 addr.s6_addr32[1] = 0;
4051
4052 if (!ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) &&
4053 ipv6_addr_equal(&ifp->addr, &addr)) {
4054 /* DAD failed for link-local based on MAC */
4055 idev->cnf.disable_ipv6 = 1;
4056
4057 pr_info("%s: IPv6 being disabled!\n",
4058 ifp->idev->dev->name);
4059 disable_ipv6 = true;
4060 }
4061 }
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004062 }
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01004063 spin_unlock_bh(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004064
4065 if (action == DAD_BEGIN) {
4066 addrconf_dad_begin(ifp);
4067 goto out;
4068 } else if (action == DAD_ABORT) {
Wei Yongjun751eb6b2016-09-05 16:06:31 +08004069 in6_ifa_hold(ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004070 addrconf_dad_stop(ifp, 1);
Mike Manning85b51b12016-08-18 14:39:40 +01004071 if (disable_ipv6)
4072 addrconf_ifdown(idev->dev, 0);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004073 goto out;
4074 }
4075
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02004076 if (!ifp->dad_probes && addrconf_dad_end(ifp))
Herbert Xuf2344a12010-05-18 15:55:27 -07004077 goto out;
4078
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004079 write_lock_bh(&idev->lock);
stephen hemminger122e4512010-03-02 13:32:44 +00004080 if (idev->dead || !(idev->if_flags & IF_READY)) {
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004081 write_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004082 goto out;
4083 }
stephen hemminger21809fa2010-02-08 19:48:52 +00004084
4085 spin_lock(&ifp->lock);
Herbert Xu622ccdf2010-05-18 15:56:06 -07004086 if (ifp->state == INET6_IFADDR_STATE_DEAD) {
4087 spin_unlock(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004088 write_unlock_bh(&idev->lock);
Herbert Xu622ccdf2010-05-18 15:56:06 -07004089 goto out;
4090 }
4091
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02004092 if (ifp->dad_probes == 0) {
David Ahernc76fe2d2018-01-25 20:16:29 -08004093 bool send_na = false;
4094
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095 /*
4096 * DAD was successful
4097 */
4098
David Ahernc76fe2d2018-01-25 20:16:29 -08004099 if (ifp->flags & IFA_F_TENTATIVE &&
4100 !(ifp->flags & IFA_F_OPTIMISTIC))
4101 send_na = true;
Paolo Abeni764d3be2016-11-22 16:57:40 +01004102 bump_id = ifp->flags & IFA_F_TENTATIVE;
Brian Haleycc411d02009-09-09 14:41:32 +00004103 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
stephen hemminger21809fa2010-02-08 19:48:52 +00004104 spin_unlock(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004105 write_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106
David Ahernc76fe2d2018-01-25 20:16:29 -08004107 addrconf_dad_completed(ifp, bump_id, send_na);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108
4109 goto out;
4110 }
4111
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02004112 ifp->dad_probes--;
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004113 addrconf_mod_dad_work(ifp,
4114 NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME));
stephen hemminger21809fa2010-02-08 19:48:52 +00004115 spin_unlock(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004116 write_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117
4118 /* send a neighbour solicitation for our addr */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119 addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
Erik Nordmarkadc176c2016-12-02 14:00:08 -08004120 ndisc_send_ns(ifp->idev->dev, &ifp->addr, &mcaddr, &in6addr_any,
4121 ifp->dad_nonce);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122out:
4123 in6_ifa_put(ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004124 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125}
4126
Hannes Frederic Sowa11ffff72014-01-16 20:13:04 +01004127/* ifp->idev must be at least read locked */
4128static bool ipv6_lonely_lladdr(struct inet6_ifaddr *ifp)
4129{
4130 struct inet6_ifaddr *ifpiter;
4131 struct inet6_dev *idev = ifp->idev;
4132
Hannes Frederic Sowa602582c2014-01-19 21:58:19 +01004133 list_for_each_entry_reverse(ifpiter, &idev->addr_list, if_list) {
4134 if (ifpiter->scope > IFA_LINK)
4135 break;
Hannes Frederic Sowa11ffff72014-01-16 20:13:04 +01004136 if (ifp != ifpiter && ifpiter->scope == IFA_LINK &&
4137 (ifpiter->flags & (IFA_F_PERMANENT|IFA_F_TENTATIVE|
4138 IFA_F_OPTIMISTIC|IFA_F_DADFAILED)) ==
4139 IFA_F_PERMANENT)
4140 return false;
4141 }
4142 return true;
4143}
4144
David Ahernc76fe2d2018-01-25 20:16:29 -08004145static void addrconf_dad_completed(struct inet6_ifaddr *ifp, bool bump_id,
4146 bool send_na)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147{
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004148 struct net_device *dev = ifp->idev->dev;
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02004149 struct in6_addr lladdr;
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02004150 bool send_rs, send_mld;
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02004151
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004152 addrconf_del_dad_work(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153
4154 /*
4155 * Configure the address for reception. Now it is valid.
4156 */
4157
4158 ipv6_ifa_notify(RTM_NEWADDR, ifp);
4159
Tore Anderson026359b2011-08-28 23:47:33 +00004160 /* If added prefix is link local and we are prepared to process
4161 router advertisements, start sending router solicitations.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162 */
4163
Hannes Frederic Sowa1ec047e2013-06-27 00:06:56 +02004164 read_lock_bh(&ifp->idev->lock);
Hannes Frederic Sowa11ffff72014-01-16 20:13:04 +01004165 send_mld = ifp->scope == IFA_LINK && ipv6_lonely_lladdr(ifp);
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02004166 send_rs = send_mld &&
4167 ipv6_accept_ra(ifp->idev) &&
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07004168 ifp->idev->cnf.rtr_solicits != 0 &&
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02004169 (dev->flags&IFF_LOOPBACK) == 0;
Hannes Frederic Sowa1ec047e2013-06-27 00:06:56 +02004170 read_unlock_bh(&ifp->idev->lock);
4171
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02004172 /* While dad is in progress mld report's source address is in6_addrany.
4173 * Resend with proper ll now.
4174 */
4175 if (send_mld)
4176 ipv6_mc_dad_complete(ifp->idev);
4177
David Ahernc76fe2d2018-01-25 20:16:29 -08004178 /* send unsolicited NA if enabled */
4179 if (send_na &&
4180 (ifp->idev->cnf.ndisc_notify ||
4181 dev_net(dev)->ipv6.devconf_all->ndisc_notify)) {
4182 ndisc_send_na(dev, &in6addr_linklocal_allnodes, &ifp->addr,
4183 /*router=*/ !!ifp->idev->cnf.forwarding,
4184 /*solicited=*/ false, /*override=*/ true,
4185 /*inc_opt=*/ true);
4186 }
4187
Hannes Frederic Sowa1ec047e2013-06-27 00:06:56 +02004188 if (send_rs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189 /*
4190 * If a host as already performed a random delay
4191 * [...] as part of DAD [...] there is no need
4192 * to delay again before sending the first RS
4193 */
Hannes Frederic Sowa1ec047e2013-06-27 00:06:56 +02004194 if (ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02004195 return;
Hannes Frederic Sowa1ec047e2013-06-27 00:06:56 +02004196 ndisc_send_rs(dev, &lladdr, &in6addr_linklocal_allrouters);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02004198 write_lock_bh(&ifp->idev->lock);
4199 spin_lock(&ifp->lock);
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07004200 ifp->idev->rs_interval = rfc3315_s14_backoff_init(
4201 ifp->idev->cnf.rtr_solicit_interval);
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02004202 ifp->idev->rs_probes = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203 ifp->idev->if_flags |= IF_RS_SENT;
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07004204 addrconf_mod_rs_timer(ifp->idev, ifp->idev->rs_interval);
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02004205 spin_unlock(&ifp->lock);
4206 write_unlock_bh(&ifp->idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004207 }
Paolo Abeni764d3be2016-11-22 16:57:40 +01004208
4209 if (bump_id)
4210 rt_genid_bump_ipv6(dev_net(dev));
Marcus Huewea11a7f72017-02-06 18:34:56 +01004211
4212 /* Make sure that a new temporary address will be created
4213 * before this temporary address becomes deprecated.
4214 */
4215 if (ifp->flags & IFA_F_TEMPORARY)
4216 addrconf_verify_rtnl();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217}
4218
Hangbin Liu896585d2018-11-21 21:52:33 +08004219static void addrconf_dad_run(struct inet6_dev *idev, bool restart)
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004220{
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09004221 struct inet6_ifaddr *ifp;
4222
4223 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00004224 list_for_each_entry(ifp, &idev->addr_list, if_list) {
stephen hemminger21809fa2010-02-08 19:48:52 +00004225 spin_lock(&ifp->lock);
Hangbin Liu896585d2018-11-21 21:52:33 +08004226 if ((ifp->flags & IFA_F_TENTATIVE &&
4227 ifp->state == INET6_IFADDR_STATE_DAD) || restart) {
4228 if (restart)
4229 ifp->state = INET6_IFADDR_STATE_PREDAD;
Herbert Xuf2344a12010-05-18 15:55:27 -07004230 addrconf_dad_kick(ifp);
Hangbin Liu896585d2018-11-21 21:52:33 +08004231 }
stephen hemminger21809fa2010-02-08 19:48:52 +00004232 spin_unlock(&ifp->lock);
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09004233 }
4234 read_unlock_bh(&idev->lock);
4235}
4236
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237#ifdef CONFIG_PROC_FS
4238struct if6_iter_state {
Daniel Lezcano3c400902008-01-10 22:42:49 -08004239 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240 int bucket;
Mihai Maruseac1d578302012-01-03 23:31:35 +00004241 int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242};
4243
Mihai Maruseac1d578302012-01-03 23:31:35 +00004244static struct inet6_ifaddr *if6_get_first(struct seq_file *seq, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246 struct if6_iter_state *state = seq->private;
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09004247 struct net *net = seq_file_net(seq);
Eric Dumazeta5c1d982017-10-23 16:17:50 -07004248 struct inet6_ifaddr *ifa = NULL;
Mihai Maruseac1d578302012-01-03 23:31:35 +00004249 int p = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250
Mihai Maruseac1d578302012-01-03 23:31:35 +00004251 /* initial bucket if pos is 0 */
4252 if (pos == 0) {
4253 state->bucket = 0;
4254 state->offset = 0;
4255 }
4256
4257 for (; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
Eric Dumazeta5c1d982017-10-23 16:17:50 -07004258 hlist_for_each_entry_rcu(ifa, &inet6_addr_lst[state->bucket],
Mihai Maruseac1d578302012-01-03 23:31:35 +00004259 addr_lst) {
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00004260 if (!net_eq(dev_net(ifa->idev->dev), net))
4261 continue;
Mihai Maruseac1d578302012-01-03 23:31:35 +00004262 /* sync with offset */
4263 if (p < state->offset) {
4264 p++;
4265 continue;
4266 }
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00004267 return ifa;
Mihai Maruseac1d578302012-01-03 23:31:35 +00004268 }
4269
4270 /* prepare for next bucket */
4271 state->offset = 0;
4272 p = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273 }
stephen hemmingerc2e21292010-03-17 20:31:10 +00004274 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275}
4276
stephen hemmingerc2e21292010-03-17 20:31:10 +00004277static struct inet6_ifaddr *if6_get_next(struct seq_file *seq,
4278 struct inet6_ifaddr *ifa)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279{
4280 struct if6_iter_state *state = seq->private;
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09004281 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282
Eric Dumazeta5c1d982017-10-23 16:17:50 -07004283 hlist_for_each_entry_continue_rcu(ifa, addr_lst) {
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00004284 if (!net_eq(dev_net(ifa->idev->dev), net))
4285 continue;
Mihai Maruseac1d578302012-01-03 23:31:35 +00004286 state->offset++;
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00004287 return ifa;
Mihai Maruseac1d578302012-01-03 23:31:35 +00004288 }
stephen hemmingerc2e21292010-03-17 20:31:10 +00004289
Jeff Barnhill86f9bd12018-09-21 00:45:27 +00004290 state->offset = 0;
stephen hemmingerc2e21292010-03-17 20:31:10 +00004291 while (++state->bucket < IN6_ADDR_HSIZE) {
Eric Dumazeta5c1d982017-10-23 16:17:50 -07004292 hlist_for_each_entry_rcu(ifa,
stephen hemmingerc2e21292010-03-17 20:31:10 +00004293 &inet6_addr_lst[state->bucket], addr_lst) {
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00004294 if (!net_eq(dev_net(ifa->idev->dev), net))
4295 continue;
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00004296 return ifa;
Daniel Lezcano3c400902008-01-10 22:42:49 -08004297 }
4298 }
4299
stephen hemmingerc2e21292010-03-17 20:31:10 +00004300 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301}
4302
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
Eric Dumazeta5c1d982017-10-23 16:17:50 -07004304 __acquires(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305{
Eric Dumazeta5c1d982017-10-23 16:17:50 -07004306 rcu_read_lock();
Mihai Maruseac1d578302012-01-03 23:31:35 +00004307 return if6_get_first(seq, *pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308}
4309
4310static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
4311{
4312 struct inet6_ifaddr *ifa;
4313
4314 ifa = if6_get_next(seq, v);
4315 ++*pos;
4316 return ifa;
4317}
4318
4319static void if6_seq_stop(struct seq_file *seq, void *v)
Eric Dumazeta5c1d982017-10-23 16:17:50 -07004320 __releases(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321{
Eric Dumazeta5c1d982017-10-23 16:17:50 -07004322 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323}
4324
4325static int if6_seq_show(struct seq_file *seq, void *v)
4326{
4327 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
Jiri Pirko971a3512013-12-10 13:56:29 +01004328 seq_printf(seq, "%pi6 %02x %02x %02x %02x %8s\n",
Harvey Harrisonb0711952008-10-28 16:05:40 -07004329 &ifp->addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330 ifp->idev->dev->ifindex,
4331 ifp->prefix_len,
4332 ifp->scope,
Jiri Pirko971a3512013-12-10 13:56:29 +01004333 (u8) ifp->flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334 ifp->idev->dev->name);
4335 return 0;
4336}
4337
Philippe De Muyter56b3d972007-07-10 23:07:31 -07004338static const struct seq_operations if6_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339 .start = if6_seq_start,
4340 .next = if6_seq_next,
4341 .show = if6_seq_show,
4342 .stop = if6_seq_stop,
4343};
4344
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004345static int __net_init if6_proc_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346{
Christoph Hellwigc3506372018-04-10 19:42:55 +02004347 if (!proc_create_net("if_inet6", 0444, net->proc_net, &if6_seq_ops,
4348 sizeof(struct if6_iter_state)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349 return -ENOMEM;
4350 return 0;
4351}
4352
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004353static void __net_exit if6_proc_net_exit(struct net *net)
Daniel Lezcano3c400902008-01-10 22:42:49 -08004354{
Gao fengece31ff2013-02-18 01:34:56 +00004355 remove_proc_entry("if_inet6", net->proc_net);
Daniel Lezcano3c400902008-01-10 22:42:49 -08004356}
4357
4358static struct pernet_operations if6_proc_net_ops = {
Ian Morris67ba4152014-08-24 21:53:10 +01004359 .init = if6_proc_net_init,
4360 .exit = if6_proc_net_exit,
Daniel Lezcano3c400902008-01-10 22:42:49 -08004361};
4362
4363int __init if6_proc_init(void)
4364{
4365 return register_pernet_subsys(&if6_proc_net_ops);
4366}
4367
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368void if6_proc_exit(void)
4369{
Daniel Lezcano3c400902008-01-10 22:42:49 -08004370 unregister_pernet_subsys(&if6_proc_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004371}
4372#endif /* CONFIG_PROC_FS */
4373
Amerigo Wang07a93622012-10-29 16:23:10 +00004374#if IS_ENABLED(CONFIG_IPV6_MIP6)
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07004375/* Check if address is a home address configured on any interface. */
Eric Dumazetb71d1d42011-04-22 04:53:02 +00004376int ipv6_chk_home_addr(struct net *net, const struct in6_addr *addr)
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07004377{
Eric Dumazet3f27fb22017-10-23 16:17:47 -07004378 unsigned int hash = inet6_addr_hash(net, addr);
stephen hemmingerc2e21292010-03-17 20:31:10 +00004379 struct inet6_ifaddr *ifp = NULL;
Eric Dumazet3f27fb22017-10-23 16:17:47 -07004380 int ret = 0;
stephen hemmingerc2e21292010-03-17 20:31:10 +00004381
Eric Dumazet4e5f47a2017-10-23 16:17:51 -07004382 rcu_read_lock();
4383 hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09004384 if (!net_eq(dev_net(ifp->idev->dev), net))
Daniel Lezcano389f6612008-01-10 22:44:40 -08004385 continue;
YOSHIFUJI Hideakicaad2952008-04-10 15:42:07 +09004386 if (ipv6_addr_equal(&ifp->addr, addr) &&
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07004387 (ifp->flags & IFA_F_HOMEADDRESS)) {
4388 ret = 1;
4389 break;
4390 }
4391 }
Eric Dumazet4e5f47a2017-10-23 16:17:51 -07004392 rcu_read_unlock();
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07004393 return ret;
4394}
4395#endif
4396
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397/*
4398 * Periodic address status verification
4399 */
4400
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004401static void addrconf_verify_rtnl(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004402{
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -07004403 unsigned long now, next, next_sec, next_sched;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404 struct inet6_ifaddr *ifp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405 int i;
4406
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004407 ASSERT_RTNL();
4408
stephen hemminger5c578aed2010-03-17 20:31:11 +00004409 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410 now = jiffies;
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -07004411 next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004412
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004413 cancel_delayed_work(&addr_chk_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07004415 for (i = 0; i < IN6_ADDR_HSIZE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416restart:
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004417 hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[i], addr_lst) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418 unsigned long age;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419
Yasushi Asanofad8da32013-12-31 12:04:19 +09004420 /* When setting preferred_lft to a value not zero or
4421 * infinity, while valid_lft is infinity
4422 * IFA_F_PERMANENT has a non-infinity life time.
4423 */
4424 if ((ifp->flags & IFA_F_PERMANENT) &&
4425 (ifp->prefered_lft == INFINITY_LIFE_TIME))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426 continue;
4427
4428 spin_lock(&ifp->lock);
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -07004429 /* We try to batch several events at once. */
4430 age = (now - ifp->tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431
YOSHIFUJI Hideaki8f27ebb2006-07-28 18:12:11 +09004432 if (ifp->valid_lft != INFINITY_LIFE_TIME &&
4433 age >= ifp->valid_lft) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434 spin_unlock(&ifp->lock);
4435 in6_ifa_hold(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436 ipv6_del_addr(ifp);
4437 goto restart;
YOSHIFUJI Hideaki8f27ebb2006-07-28 18:12:11 +09004438 } else if (ifp->prefered_lft == INFINITY_LIFE_TIME) {
4439 spin_unlock(&ifp->lock);
4440 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441 } else if (age >= ifp->prefered_lft) {
Brian Haleya1ed0522009-07-02 07:10:52 +00004442 /* jiffies - ifp->tstamp > age >= ifp->prefered_lft */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443 int deprecate = 0;
4444
4445 if (!(ifp->flags&IFA_F_DEPRECATED)) {
4446 deprecate = 1;
4447 ifp->flags |= IFA_F_DEPRECATED;
4448 }
4449
Yasushi Asanofad8da32013-12-31 12:04:19 +09004450 if ((ifp->valid_lft != INFINITY_LIFE_TIME) &&
4451 (time_before(ifp->tstamp + ifp->valid_lft * HZ, next)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004452 next = ifp->tstamp + ifp->valid_lft * HZ;
4453
4454 spin_unlock(&ifp->lock);
4455
4456 if (deprecate) {
4457 in6_ifa_hold(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004458
4459 ipv6_ifa_notify(0, ifp);
4460 in6_ifa_put(ifp);
4461 goto restart;
4462 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463 } else if ((ifp->flags&IFA_F_TEMPORARY) &&
4464 !(ifp->flags&IFA_F_TENTATIVE)) {
stephen hemminger88949cf2010-03-17 20:31:17 +00004465 unsigned long regen_advance = ifp->idev->cnf.regen_max_retry *
4466 ifp->idev->cnf.dad_transmits *
Jiri Pirko1f9248e2013-12-07 19:26:53 +01004467 NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME) / HZ;
stephen hemminger88949cf2010-03-17 20:31:17 +00004468
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469 if (age >= ifp->prefered_lft - regen_advance) {
4470 struct inet6_ifaddr *ifpub = ifp->ifpub;
4471 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
4472 next = ifp->tstamp + ifp->prefered_lft * HZ;
4473 if (!ifp->regen_count && ifpub) {
4474 ifp->regen_count++;
4475 in6_ifa_hold(ifp);
4476 in6_ifa_hold(ifpub);
4477 spin_unlock(&ifp->lock);
stephen hemminger5c578aed2010-03-17 20:31:11 +00004478
Hiroyuki YAMAMORI291d8092005-12-23 11:24:05 -08004479 spin_lock(&ifpub->lock);
4480 ifpub->regen_count = 0;
4481 spin_unlock(&ifpub->lock);
Eric Dumazete64e4692018-01-26 16:10:43 -08004482 rcu_read_unlock_bh();
Eric Dumazetfffcefe2017-11-06 14:13:29 -08004483 ipv6_create_tempaddr(ifpub, ifp, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484 in6_ifa_put(ifpub);
4485 in6_ifa_put(ifp);
Eric Dumazete64e4692018-01-26 16:10:43 -08004486 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487 goto restart;
4488 }
4489 } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
4490 next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
4491 spin_unlock(&ifp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492 } else {
4493 /* ifp->prefered_lft <= ifp->valid_lft */
4494 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
4495 next = ifp->tstamp + ifp->prefered_lft * HZ;
4496 spin_unlock(&ifp->lock);
4497 }
4498 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004499 }
4500
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -07004501 next_sec = round_jiffies_up(next);
4502 next_sched = next;
4503
4504 /* If rounded timeout is accurate enough, accept it. */
4505 if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ))
4506 next_sched = next_sec;
4507
4508 /* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */
4509 if (time_before(next_sched, jiffies + ADDRCONF_TIMER_FUZZ_MAX))
4510 next_sched = jiffies + ADDRCONF_TIMER_FUZZ_MAX;
4511
Joe Perchese32ac252018-03-26 08:35:01 -07004512 pr_debug("now = %lu, schedule = %lu, rounded schedule = %lu => %lu\n",
4513 now, next, next_sec, next_sched);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004514 mod_delayed_work(addrconf_wq, &addr_chk_work, next_sched - now);
stephen hemminger5c578aed2010-03-17 20:31:11 +00004515 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516}
4517
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004518static void addrconf_verify_work(struct work_struct *w)
4519{
4520 rtnl_lock();
4521 addrconf_verify_rtnl();
4522 rtnl_unlock();
4523}
4524
4525static void addrconf_verify(void)
4526{
4527 mod_delayed_work(addrconf_wq, &addr_chk_work, 0);
4528}
4529
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004530static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local,
4531 struct in6_addr **peer_pfx)
Thomas Graf461d8832006-09-18 00:09:49 -07004532{
4533 struct in6_addr *pfx = NULL;
4534
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004535 *peer_pfx = NULL;
4536
Thomas Graf461d8832006-09-18 00:09:49 -07004537 if (addr)
4538 pfx = nla_data(addr);
4539
4540 if (local) {
4541 if (pfx && nla_memcmp(local, pfx, sizeof(*pfx)))
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004542 *peer_pfx = pfx;
4543 pfx = nla_data(local);
Thomas Graf461d8832006-09-18 00:09:49 -07004544 }
4545
4546 return pfx;
4547}
4548
Patrick McHardyef7c79e2007-06-05 12:38:30 -07004549static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = {
Thomas Graf461d8832006-09-18 00:09:49 -07004550 [IFA_ADDRESS] = { .len = sizeof(struct in6_addr) },
4551 [IFA_LOCAL] = { .len = sizeof(struct in6_addr) },
4552 [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) },
Jiri Pirko479840f2013-12-06 09:45:21 +01004553 [IFA_FLAGS] = { .len = sizeof(u32) },
David Ahern8308f3f2018-05-27 08:09:58 -07004554 [IFA_RT_PRIORITY] = { .len = sizeof(u32) },
Christian Brauner6ecf4c32018-09-04 21:53:50 +02004555 [IFA_TARGET_NETNSID] = { .type = NLA_S32 },
Thomas Graf461d8832006-09-18 00:09:49 -07004556};
4557
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558static int
David Ahernc21ef3e2017-04-16 09:48:24 -07004559inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh,
4560 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09004562 struct net *net = sock_net(skb->sk);
Thomas Grafb933f712006-09-18 00:10:19 -07004563 struct ifaddrmsg *ifm;
4564 struct nlattr *tb[IFA_MAX+1];
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004565 struct in6_addr *pfx, *peer_pfx;
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02004566 u32 ifa_flags;
Thomas Grafb933f712006-09-18 00:10:19 -07004567 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004568
Johannes Berg8cb08172019-04-26 14:07:28 +02004569 err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFA_MAX,
4570 ifa_ipv6_policy, extack);
Thomas Grafb933f712006-09-18 00:10:19 -07004571 if (err < 0)
4572 return err;
4573
4574 ifm = nlmsg_data(nlh);
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004575 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
Ian Morris63159f22015-03-29 14:00:04 +01004576 if (!pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004577 return -EINVAL;
4578
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02004579 ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags;
4580
4581 /* We ignore other flags so far. */
4582 ifa_flags &= IFA_F_MANAGETEMPADDR;
4583
4584 return inet6_addr_del(net, ifm->ifa_index, ifa_flags, pfx,
4585 ifm->ifa_prefixlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586}
4587
David Ahern8308f3f2018-05-27 08:09:58 -07004588static int modify_prefix_route(struct inet6_ifaddr *ifp,
4589 unsigned long expires, u32 flags)
4590{
4591 struct fib6_info *f6i;
David Aherne7c7faa2018-06-28 13:36:55 -07004592 u32 prio;
David Ahern8308f3f2018-05-27 08:09:58 -07004593
David Ahern2b2450c2019-03-27 20:53:52 -07004594 f6i = addrconf_get_prefix_route(&ifp->addr, ifp->prefix_len,
4595 ifp->idev->dev, 0, RTF_DEFAULT, true);
David Ahern8308f3f2018-05-27 08:09:58 -07004596 if (!f6i)
4597 return -ENOENT;
4598
David Aherne7c7faa2018-06-28 13:36:55 -07004599 prio = ifp->rt_priority ? : IP6_RT_PRIO_ADDRCONF;
4600 if (f6i->fib6_metric != prio) {
4601 /* delete old one */
4602 ip6_del_rt(dev_net(ifp->idev->dev), f6i);
4603
David Ahern8308f3f2018-05-27 08:09:58 -07004604 /* add new one */
4605 addrconf_prefix_route(&ifp->addr, ifp->prefix_len,
4606 ifp->rt_priority, ifp->idev->dev,
4607 expires, flags, GFP_KERNEL);
David Ahern8308f3f2018-05-27 08:09:58 -07004608 } else {
4609 if (!expires)
4610 fib6_clean_expires(f6i);
4611 else
4612 fib6_set_expires(f6i, expires);
4613
4614 fib6_info_release(f6i);
4615 }
4616
4617 return 0;
4618}
4619
David Ahernd169a1f2018-05-27 08:09:55 -07004620static int inet6_addr_modify(struct inet6_ifaddr *ifp, struct ifa6_config *cfg)
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004621{
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07004622 u32 flags;
4623 clock_t expires;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09004624 unsigned long timeout;
Jiri Pirko53bd6742013-12-06 09:45:22 +01004625 bool was_managetempaddr;
Thomas Haller5b84efe2014-01-15 15:36:59 +01004626 bool had_prefixroute;
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09004627
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004628 ASSERT_RTNL();
4629
David Ahernd169a1f2018-05-27 08:09:55 -07004630 if (!cfg->valid_lft || cfg->preferred_lft > cfg->valid_lft)
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004631 return -EINVAL;
4632
David Ahernd169a1f2018-05-27 08:09:55 -07004633 if (cfg->ifa_flags & IFA_F_MANAGETEMPADDR &&
Jiri Pirko53bd6742013-12-06 09:45:22 +01004634 (ifp->flags & IFA_F_TEMPORARY || ifp->prefix_len != 64))
4635 return -EINVAL;
4636
Sabrina Dubrocaf1c02cf2018-02-28 16:40:08 +01004637 if (!(ifp->flags & IFA_F_TENTATIVE) || ifp->flags & IFA_F_DADFAILED)
David Ahernd169a1f2018-05-27 08:09:55 -07004638 cfg->ifa_flags &= ~IFA_F_OPTIMISTIC;
Sabrina Dubrocaf1c02cf2018-02-28 16:40:08 +01004639
David Ahernd169a1f2018-05-27 08:09:55 -07004640 timeout = addrconf_timeout_fixup(cfg->valid_lft, HZ);
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09004641 if (addrconf_finite_timeout(timeout)) {
4642 expires = jiffies_to_clock_t(timeout * HZ);
David Ahernd169a1f2018-05-27 08:09:55 -07004643 cfg->valid_lft = timeout;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07004644 flags = RTF_EXPIRES;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09004645 } else {
4646 expires = 0;
4647 flags = 0;
David Ahernd169a1f2018-05-27 08:09:55 -07004648 cfg->ifa_flags |= IFA_F_PERMANENT;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07004649 }
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004650
David Ahernd169a1f2018-05-27 08:09:55 -07004651 timeout = addrconf_timeout_fixup(cfg->preferred_lft, HZ);
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09004652 if (addrconf_finite_timeout(timeout)) {
4653 if (timeout == 0)
David Ahernd169a1f2018-05-27 08:09:55 -07004654 cfg->ifa_flags |= IFA_F_DEPRECATED;
4655 cfg->preferred_lft = timeout;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09004656 }
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004657
4658 spin_lock_bh(&ifp->lock);
Jiri Pirko53bd6742013-12-06 09:45:22 +01004659 was_managetempaddr = ifp->flags & IFA_F_MANAGETEMPADDR;
Thomas Haller5b84efe2014-01-15 15:36:59 +01004660 had_prefixroute = ifp->flags & IFA_F_PERMANENT &&
4661 !(ifp->flags & IFA_F_NOPREFIXROUTE);
Jiri Pirko53bd6742013-12-06 09:45:22 +01004662 ifp->flags &= ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD |
Thomas Haller761aac72014-01-15 15:36:58 +01004663 IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR |
4664 IFA_F_NOPREFIXROUTE);
David Ahernd169a1f2018-05-27 08:09:55 -07004665 ifp->flags |= cfg->ifa_flags;
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004666 ifp->tstamp = jiffies;
David Ahernd169a1f2018-05-27 08:09:55 -07004667 ifp->valid_lft = cfg->valid_lft;
4668 ifp->prefered_lft = cfg->preferred_lft;
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004669
David Ahern8308f3f2018-05-27 08:09:58 -07004670 if (cfg->rt_priority && cfg->rt_priority != ifp->rt_priority)
4671 ifp->rt_priority = cfg->rt_priority;
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004672
4673 spin_unlock_bh(&ifp->lock);
4674 if (!(ifp->flags&IFA_F_TENTATIVE))
4675 ipv6_ifa_notify(0, ifp);
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004676
David Ahernd169a1f2018-05-27 08:09:55 -07004677 if (!(cfg->ifa_flags & IFA_F_NOPREFIXROUTE)) {
David Ahern8308f3f2018-05-27 08:09:58 -07004678 int rc = -ENOENT;
4679
4680 if (had_prefixroute)
4681 rc = modify_prefix_route(ifp, expires, flags);
4682
4683 /* prefix route could have been deleted; if so restore it */
4684 if (rc == -ENOENT) {
4685 addrconf_prefix_route(&ifp->addr, ifp->prefix_len,
4686 ifp->rt_priority, ifp->idev->dev,
4687 expires, flags, GFP_KERNEL);
4688 }
Thomas Haller5b84efe2014-01-15 15:36:59 +01004689 } else if (had_prefixroute) {
4690 enum cleanup_prefix_rt_t action;
4691 unsigned long rt_expires;
4692
4693 write_lock_bh(&ifp->idev->lock);
4694 action = check_cleanup_prefix_route(ifp, &rt_expires);
4695 write_unlock_bh(&ifp->idev->lock);
4696
4697 if (action != CLEANUP_PREFIX_RT_NOP) {
4698 cleanup_prefix_route(ifp, rt_expires,
4699 action == CLEANUP_PREFIX_RT_DEL);
4700 }
Thomas Haller761aac72014-01-15 15:36:58 +01004701 }
Jiri Pirko53bd6742013-12-06 09:45:22 +01004702
4703 if (was_managetempaddr || ifp->flags & IFA_F_MANAGETEMPADDR) {
David Ahernd169a1f2018-05-27 08:09:55 -07004704 if (was_managetempaddr &&
4705 !(ifp->flags & IFA_F_MANAGETEMPADDR)) {
4706 cfg->valid_lft = 0;
4707 cfg->preferred_lft = 0;
4708 }
4709 manage_tempaddrs(ifp->idev, ifp, cfg->valid_lft,
4710 cfg->preferred_lft, !was_managetempaddr,
4711 jiffies);
Jiri Pirko53bd6742013-12-06 09:45:22 +01004712 }
4713
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004714 addrconf_verify_rtnl();
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004715
4716 return 0;
4717}
4718
4719static int
David Ahernc21ef3e2017-04-16 09:48:24 -07004720inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh,
4721 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09004723 struct net *net = sock_net(skb->sk);
Thomas Graf461d8832006-09-18 00:09:49 -07004724 struct ifaddrmsg *ifm;
4725 struct nlattr *tb[IFA_MAX+1];
David Ahern19b15182018-05-27 08:09:54 -07004726 struct in6_addr *peer_pfx;
Thomas Graf7198f8c2006-09-18 00:13:46 -07004727 struct inet6_ifaddr *ifa;
4728 struct net_device *dev;
Sabrina Dubrocaf1c02cf2018-02-28 16:40:08 +01004729 struct inet6_dev *idev;
David Ahern19b15182018-05-27 08:09:54 -07004730 struct ifa6_config cfg;
Thomas Graf461d8832006-09-18 00:09:49 -07004731 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004732
Johannes Berg8cb08172019-04-26 14:07:28 +02004733 err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFA_MAX,
4734 ifa_ipv6_policy, extack);
Thomas Graf461d8832006-09-18 00:09:49 -07004735 if (err < 0)
4736 return err;
4737
David Ahern19b15182018-05-27 08:09:54 -07004738 memset(&cfg, 0, sizeof(cfg));
4739
Thomas Graf461d8832006-09-18 00:09:49 -07004740 ifm = nlmsg_data(nlh);
David Ahern19b15182018-05-27 08:09:54 -07004741 cfg.pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
4742 if (!cfg.pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004743 return -EINVAL;
4744
David Ahern19b15182018-05-27 08:09:54 -07004745 cfg.peer_pfx = peer_pfx;
4746 cfg.plen = ifm->ifa_prefixlen;
David Ahern8308f3f2018-05-27 08:09:58 -07004747 if (tb[IFA_RT_PRIORITY])
4748 cfg.rt_priority = nla_get_u32(tb[IFA_RT_PRIORITY]);
4749
David Ahern19b15182018-05-27 08:09:54 -07004750 cfg.valid_lft = INFINITY_LIFE_TIME;
4751 cfg.preferred_lft = INFINITY_LIFE_TIME;
4752
Thomas Graf461d8832006-09-18 00:09:49 -07004753 if (tb[IFA_CACHEINFO]) {
Noriaki TAKAMIYA0778769d2006-07-28 18:12:10 +09004754 struct ifa_cacheinfo *ci;
Thomas Graf461d8832006-09-18 00:09:49 -07004755
4756 ci = nla_data(tb[IFA_CACHEINFO]);
David Ahern19b15182018-05-27 08:09:54 -07004757 cfg.valid_lft = ci->ifa_valid;
4758 cfg.preferred_lft = ci->ifa_prefered;
Noriaki TAKAMIYA0778769d2006-07-28 18:12:10 +09004759 }
4760
Daniel Lezcanoaf284932008-03-05 10:46:57 -08004761 dev = __dev_get_by_index(net, ifm->ifa_index);
Ian Morris63159f22015-03-29 14:00:04 +01004762 if (!dev)
Thomas Graf7198f8c2006-09-18 00:13:46 -07004763 return -ENODEV;
4764
David Ahern19b15182018-05-27 08:09:54 -07004765 if (tb[IFA_FLAGS])
4766 cfg.ifa_flags = nla_get_u32(tb[IFA_FLAGS]);
4767 else
4768 cfg.ifa_flags = ifm->ifa_flags;
Jiri Pirko479840f2013-12-06 09:45:21 +01004769
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07004770 /* We ignore other flags so far. */
David Ahern19b15182018-05-27 08:09:54 -07004771 cfg.ifa_flags &= IFA_F_NODAD | IFA_F_HOMEADDRESS |
4772 IFA_F_MANAGETEMPADDR | IFA_F_NOPREFIXROUTE |
4773 IFA_F_MCAUTOJOIN | IFA_F_OPTIMISTIC;
Sabrina Dubrocaf1c02cf2018-02-28 16:40:08 +01004774
4775 idev = ipv6_find_idev(dev);
Sabrina Dubrocadb0b99f2019-08-23 15:44:36 +02004776 if (IS_ERR(idev))
4777 return PTR_ERR(idev);
Sabrina Dubrocaf1c02cf2018-02-28 16:40:08 +01004778
4779 if (!ipv6_allow_optimistic_dad(net, idev))
David Ahern19b15182018-05-27 08:09:54 -07004780 cfg.ifa_flags &= ~IFA_F_OPTIMISTIC;
Sabrina Dubrocaf1c02cf2018-02-28 16:40:08 +01004781
David Ahern19b15182018-05-27 08:09:54 -07004782 if (cfg.ifa_flags & IFA_F_NODAD &&
4783 cfg.ifa_flags & IFA_F_OPTIMISTIC) {
Sabrina Dubrocaf1c02cf2018-02-28 16:40:08 +01004784 NL_SET_ERR_MSG(extack, "IFA_F_NODAD and IFA_F_OPTIMISTIC are mutually exclusive");
4785 return -EINVAL;
4786 }
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07004787
David Ahern19b15182018-05-27 08:09:54 -07004788 ifa = ipv6_get_ifaddr(net, cfg.pfx, dev, 1);
Ian Morris63159f22015-03-29 14:00:04 +01004789 if (!ifa) {
Thomas Graf7198f8c2006-09-18 00:13:46 -07004790 /*
4791 * It would be best to check for !NLM_F_CREATE here but
stephen hemmingerdb9c7c32014-01-12 11:26:32 -08004792 * userspace already relies on not having to provide this.
Thomas Graf7198f8c2006-09-18 00:13:46 -07004793 */
David Ahern19b15182018-05-27 08:09:54 -07004794 return inet6_addr_add(net, ifm->ifa_index, &cfg, extack);
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004795 }
4796
Thomas Graf7198f8c2006-09-18 00:13:46 -07004797 if (nlh->nlmsg_flags & NLM_F_EXCL ||
4798 !(nlh->nlmsg_flags & NLM_F_REPLACE))
4799 err = -EEXIST;
4800 else
David Ahernd169a1f2018-05-27 08:09:55 -07004801 err = inet6_addr_modify(ifa, &cfg);
Thomas Graf7198f8c2006-09-18 00:13:46 -07004802
4803 in6_ifa_put(ifa);
4804
4805 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004806}
4807
Jiri Pirko479840f2013-12-06 09:45:21 +01004808static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u32 flags,
Thomas Graf101bb222006-09-18 00:11:52 -07004809 u8 scope, int ifindex)
4810{
4811 struct ifaddrmsg *ifm;
4812
4813 ifm = nlmsg_data(nlh);
4814 ifm->ifa_family = AF_INET6;
4815 ifm->ifa_prefixlen = prefixlen;
4816 ifm->ifa_flags = flags;
4817 ifm->ifa_scope = scope;
4818 ifm->ifa_index = ifindex;
4819}
4820
Thomas Graf85486af2006-09-18 00:11:24 -07004821static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
4822 unsigned long tstamp, u32 preferred, u32 valid)
4823{
4824 struct ifa_cacheinfo ci;
4825
Thomas Graf18a31e12010-11-17 04:12:02 +00004826 ci.cstamp = cstamp_delta(cstamp);
4827 ci.tstamp = cstamp_delta(tstamp);
Thomas Graf85486af2006-09-18 00:11:24 -07004828 ci.ifa_prefered = preferred;
4829 ci.ifa_valid = valid;
4830
4831 return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
4832}
4833
Thomas Graf101bb222006-09-18 00:11:52 -07004834static inline int rt_scope(int ifa_scope)
4835{
4836 if (ifa_scope & IFA_HOST)
4837 return RT_SCOPE_HOST;
4838 else if (ifa_scope & IFA_LINK)
4839 return RT_SCOPE_LINK;
4840 else if (ifa_scope & IFA_SITE)
4841 return RT_SCOPE_SITE;
4842 else
4843 return RT_SCOPE_UNIVERSE;
4844}
4845
Thomas Graf0ab68032006-09-18 00:12:35 -07004846static inline int inet6_ifaddr_msgsize(void)
4847{
Thomas Graf339bf982006-11-10 14:10:15 -08004848 return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004849 + nla_total_size(16) /* IFA_LOCAL */
Thomas Graf339bf982006-11-10 14:10:15 -08004850 + nla_total_size(16) /* IFA_ADDRESS */
Jiri Pirko479840f2013-12-06 09:45:21 +01004851 + nla_total_size(sizeof(struct ifa_cacheinfo))
David Ahern8308f3f2018-05-27 08:09:58 -07004852 + nla_total_size(4) /* IFA_FLAGS */
4853 + nla_total_size(4) /* IFA_RT_PRIORITY */;
Thomas Graf0ab68032006-09-18 00:12:35 -07004854}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07004855
David Ahern6ba1e6e2018-10-07 20:16:26 -07004856enum addr_type_t {
4857 UNICAST_ADDR,
4858 MULTICAST_ADDR,
4859 ANYCAST_ADDR,
4860};
4861
Christian Brauner203651b2018-09-04 21:53:55 +02004862struct inet6_fill_args {
4863 u32 portid;
4864 u32 seq;
4865 int event;
4866 unsigned int flags;
4867 int netnsid;
David Ahern6371a712018-10-19 12:45:30 -07004868 int ifindex;
David Ahern6ba1e6e2018-10-07 20:16:26 -07004869 enum addr_type_t type;
Christian Brauner203651b2018-09-04 21:53:55 +02004870};
4871
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
Christian Brauner203651b2018-09-04 21:53:55 +02004873 struct inet6_fill_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004874{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004875 struct nlmsghdr *nlh;
Thomas Graf85486af2006-09-18 00:11:24 -07004876 u32 preferred, valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004877
Christian Brauner203651b2018-09-04 21:53:55 +02004878 nlh = nlmsg_put(skb, args->portid, args->seq, args->event,
4879 sizeof(struct ifaddrmsg), args->flags);
Ian Morris63159f22015-03-29 14:00:04 +01004880 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08004881 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07004882
Thomas Graf101bb222006-09-18 00:11:52 -07004883 put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope),
4884 ifa->idev->dev->ifindex);
4885
Christian Brauner203651b2018-09-04 21:53:55 +02004886 if (args->netnsid >= 0 &&
4887 nla_put_s32(skb, IFA_TARGET_NETNSID, args->netnsid))
Christian Brauner6ecf4c32018-09-04 21:53:50 +02004888 goto error;
4889
Yasushi Asanofad8da32013-12-31 12:04:19 +09004890 if (!((ifa->flags&IFA_F_PERMANENT) &&
4891 (ifa->prefered_lft == INFINITY_LIFE_TIME))) {
Thomas Graf85486af2006-09-18 00:11:24 -07004892 preferred = ifa->prefered_lft;
4893 valid = ifa->valid_lft;
4894 if (preferred != INFINITY_LIFE_TIME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895 long tval = (jiffies - ifa->tstamp)/HZ;
Jens Rosenbooma1faa692009-06-25 04:55:50 +00004896 if (preferred > tval)
4897 preferred -= tval;
4898 else
4899 preferred = 0;
Ben Hutchingsf56619f2010-06-26 11:37:47 +00004900 if (valid != INFINITY_LIFE_TIME) {
4901 if (valid > tval)
4902 valid -= tval;
4903 else
4904 valid = 0;
4905 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004906 }
4907 } else {
Thomas Graf85486af2006-09-18 00:11:24 -07004908 preferred = INFINITY_LIFE_TIME;
4909 valid = INFINITY_LIFE_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004910 }
Thomas Graf85486af2006-09-18 00:11:24 -07004911
Cong Wang7996c792013-05-22 05:41:06 +00004912 if (!ipv6_addr_any(&ifa->peer_addr)) {
Jiri Benc930345e2015-03-29 16:59:25 +02004913 if (nla_put_in6_addr(skb, IFA_LOCAL, &ifa->addr) < 0 ||
4914 nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->peer_addr) < 0)
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004915 goto error;
4916 } else
Jiri Benc930345e2015-03-29 16:59:25 +02004917 if (nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->addr) < 0)
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004918 goto error;
4919
David Ahern8308f3f2018-05-27 08:09:58 -07004920 if (ifa->rt_priority &&
4921 nla_put_u32(skb, IFA_RT_PRIORITY, ifa->rt_priority))
4922 goto error;
4923
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004924 if (put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0)
4925 goto error;
Thomas Graf85486af2006-09-18 00:11:24 -07004926
Jiri Pirko479840f2013-12-06 09:45:21 +01004927 if (nla_put_u32(skb, IFA_FLAGS, ifa->flags) < 0)
4928 goto error;
4929
Johannes Berg053c0952015-01-16 22:09:00 +01004930 nlmsg_end(skb, nlh);
4931 return 0;
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004932
4933error:
4934 nlmsg_cancel(skb, nlh);
4935 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004936}
4937
4938static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
Christian Brauner203651b2018-09-04 21:53:55 +02004939 struct inet6_fill_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004940{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004941 struct nlmsghdr *nlh;
Thomas Graf101bb222006-09-18 00:11:52 -07004942 u8 scope = RT_SCOPE_UNIVERSE;
4943 int ifindex = ifmca->idev->dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944
Thomas Graf101bb222006-09-18 00:11:52 -07004945 if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE)
4946 scope = RT_SCOPE_SITE;
4947
Christian Brauner203651b2018-09-04 21:53:55 +02004948 nlh = nlmsg_put(skb, args->portid, args->seq, args->event,
4949 sizeof(struct ifaddrmsg), args->flags);
Ian Morris63159f22015-03-29 14:00:04 +01004950 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08004951 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07004952
Christian Brauner203651b2018-09-04 21:53:55 +02004953 if (args->netnsid >= 0 &&
4954 nla_put_s32(skb, IFA_TARGET_NETNSID, args->netnsid))
Christian Brauner6ecf4c32018-09-04 21:53:50 +02004955 return -EMSGSIZE;
4956
Thomas Graf101bb222006-09-18 00:11:52 -07004957 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
Jiri Benc930345e2015-03-29 16:59:25 +02004958 if (nla_put_in6_addr(skb, IFA_MULTICAST, &ifmca->mca_addr) < 0 ||
Thomas Graf0ab68032006-09-18 00:12:35 -07004959 put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp,
Patrick McHardy26932562007-01-31 23:16:40 -08004960 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
4961 nlmsg_cancel(skb, nlh);
4962 return -EMSGSIZE;
4963 }
Thomas Graf85486af2006-09-18 00:11:24 -07004964
Johannes Berg053c0952015-01-16 22:09:00 +01004965 nlmsg_end(skb, nlh);
4966 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967}
4968
4969static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
Christian Brauner203651b2018-09-04 21:53:55 +02004970 struct inet6_fill_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004971{
David Ahern9ee8cbb2018-04-18 15:39:01 -07004972 struct net_device *dev = fib6_info_nh_dev(ifaca->aca_rt);
4973 int ifindex = dev ? dev->ifindex : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004974 struct nlmsghdr *nlh;
Thomas Graf101bb222006-09-18 00:11:52 -07004975 u8 scope = RT_SCOPE_UNIVERSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004976
Thomas Graf101bb222006-09-18 00:11:52 -07004977 if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE)
4978 scope = RT_SCOPE_SITE;
4979
Christian Brauner203651b2018-09-04 21:53:55 +02004980 nlh = nlmsg_put(skb, args->portid, args->seq, args->event,
4981 sizeof(struct ifaddrmsg), args->flags);
Ian Morris63159f22015-03-29 14:00:04 +01004982 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08004983 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07004984
Christian Brauner203651b2018-09-04 21:53:55 +02004985 if (args->netnsid >= 0 &&
4986 nla_put_s32(skb, IFA_TARGET_NETNSID, args->netnsid))
Christian Brauner6ecf4c32018-09-04 21:53:50 +02004987 return -EMSGSIZE;
4988
Thomas Graf101bb222006-09-18 00:11:52 -07004989 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
Jiri Benc930345e2015-03-29 16:59:25 +02004990 if (nla_put_in6_addr(skb, IFA_ANYCAST, &ifaca->aca_addr) < 0 ||
Thomas Graf0ab68032006-09-18 00:12:35 -07004991 put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp,
Patrick McHardy26932562007-01-31 23:16:40 -08004992 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
4993 nlmsg_cancel(skb, nlh);
4994 return -EMSGSIZE;
4995 }
Thomas Graf85486af2006-09-18 00:11:24 -07004996
Johannes Berg053c0952015-01-16 22:09:00 +01004997 nlmsg_end(skb, nlh);
4998 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004999}
5000
Eric Dumazet234b27c2009-11-12 04:11:50 +00005001/* called with rcu_read_lock() */
5002static int in6_dump_addrs(struct inet6_dev *idev, struct sk_buff *skb,
David Ahernfe884c22018-10-19 12:45:28 -07005003 struct netlink_callback *cb, int s_ip_idx,
David Ahern6ba1e6e2018-10-07 20:16:26 -07005004 struct inet6_fill_args *fillargs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005005{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005006 struct ifmcaddr6 *ifmca;
5007 struct ifacaddr6 *ifaca;
David Ahernfe884c22018-10-19 12:45:28 -07005008 int ip_idx = 0;
Eric Dumazet234b27c2009-11-12 04:11:50 +00005009 int err = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005010
Eric Dumazet234b27c2009-11-12 04:11:50 +00005011 read_lock_bh(&idev->lock);
David Ahern6ba1e6e2018-10-07 20:16:26 -07005012 switch (fillargs->type) {
stephen hemminger502a2ff2010-03-17 20:31:13 +00005013 case UNICAST_ADDR: {
5014 struct inet6_ifaddr *ifa;
David Ahern6ba1e6e2018-10-07 20:16:26 -07005015 fillargs->event = RTM_NEWADDR;
stephen hemminger502a2ff2010-03-17 20:31:13 +00005016
Eric Dumazet234b27c2009-11-12 04:11:50 +00005017 /* unicast address incl. temp addr */
stephen hemminger502a2ff2010-03-17 20:31:13 +00005018 list_for_each_entry(ifa, &idev->addr_list, if_list) {
David Ahern4ba4c562018-10-19 10:00:19 -07005019 if (ip_idx < s_ip_idx)
5020 goto next;
David Ahern6ba1e6e2018-10-07 20:16:26 -07005021 err = inet6_fill_ifaddr(skb, ifa, fillargs);
Johannes Berg053c0952015-01-16 22:09:00 +01005022 if (err < 0)
Eric Dumazet234b27c2009-11-12 04:11:50 +00005023 break;
Nicolas Dichtel63998ac2013-03-22 06:28:43 +00005024 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
David Ahern4ba4c562018-10-19 10:00:19 -07005025next:
5026 ip_idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005027 }
Eric Dumazet234b27c2009-11-12 04:11:50 +00005028 break;
stephen hemminger502a2ff2010-03-17 20:31:13 +00005029 }
Eric Dumazet234b27c2009-11-12 04:11:50 +00005030 case MULTICAST_ADDR:
David Ahern6ba1e6e2018-10-07 20:16:26 -07005031 fillargs->event = RTM_GETMULTICAST;
Christian Brauner203651b2018-09-04 21:53:55 +02005032
Eric Dumazet234b27c2009-11-12 04:11:50 +00005033 /* multicast address */
5034 for (ifmca = idev->mc_list; ifmca;
5035 ifmca = ifmca->next, ip_idx++) {
5036 if (ip_idx < s_ip_idx)
5037 continue;
David Ahern6ba1e6e2018-10-07 20:16:26 -07005038 err = inet6_fill_ifmcaddr(skb, ifmca, fillargs);
Johannes Berg053c0952015-01-16 22:09:00 +01005039 if (err < 0)
Eric Dumazet234b27c2009-11-12 04:11:50 +00005040 break;
5041 }
5042 break;
5043 case ANYCAST_ADDR:
David Ahern6ba1e6e2018-10-07 20:16:26 -07005044 fillargs->event = RTM_GETANYCAST;
Eric Dumazet234b27c2009-11-12 04:11:50 +00005045 /* anycast address */
5046 for (ifaca = idev->ac_list; ifaca;
5047 ifaca = ifaca->aca_next, ip_idx++) {
5048 if (ip_idx < s_ip_idx)
5049 continue;
David Ahern6ba1e6e2018-10-07 20:16:26 -07005050 err = inet6_fill_ifacaddr(skb, ifaca, fillargs);
Johannes Berg053c0952015-01-16 22:09:00 +01005051 if (err < 0)
Eric Dumazet234b27c2009-11-12 04:11:50 +00005052 break;
5053 }
5054 break;
5055 default:
5056 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005057 }
Eric Dumazet234b27c2009-11-12 04:11:50 +00005058 read_unlock_bh(&idev->lock);
David Ahernfe884c22018-10-19 12:45:28 -07005059 cb->args[2] = ip_idx;
Eric Dumazet234b27c2009-11-12 04:11:50 +00005060 return err;
5061}
5062
David Aherned6eff12018-10-07 20:16:29 -07005063static int inet6_valid_dump_ifaddr_req(const struct nlmsghdr *nlh,
5064 struct inet6_fill_args *fillargs,
5065 struct net **tgt_net, struct sock *sk,
David Ahern6371a712018-10-19 12:45:30 -07005066 struct netlink_callback *cb)
David Aherned6eff12018-10-07 20:16:29 -07005067{
David Ahern6371a712018-10-19 12:45:30 -07005068 struct netlink_ext_ack *extack = cb->extack;
David Aherned6eff12018-10-07 20:16:29 -07005069 struct nlattr *tb[IFA_MAX+1];
5070 struct ifaddrmsg *ifm;
5071 int err, i;
5072
5073 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
5074 NL_SET_ERR_MSG_MOD(extack, "Invalid header for address dump request");
5075 return -EINVAL;
5076 }
5077
5078 ifm = nlmsg_data(nlh);
5079 if (ifm->ifa_prefixlen || ifm->ifa_flags || ifm->ifa_scope) {
5080 NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for address dump request");
5081 return -EINVAL;
5082 }
David Ahern6371a712018-10-19 12:45:30 -07005083
5084 fillargs->ifindex = ifm->ifa_index;
5085 if (fillargs->ifindex) {
5086 cb->answer_flags |= NLM_F_DUMP_FILTERED;
5087 fillargs->flags |= NLM_F_DUMP_FILTERED;
David Aherned6eff12018-10-07 20:16:29 -07005088 }
5089
Johannes Berg8cb08172019-04-26 14:07:28 +02005090 err = nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, IFA_MAX,
5091 ifa_ipv6_policy, extack);
David Aherned6eff12018-10-07 20:16:29 -07005092 if (err < 0)
5093 return err;
5094
5095 for (i = 0; i <= IFA_MAX; ++i) {
5096 if (!tb[i])
5097 continue;
5098
5099 if (i == IFA_TARGET_NETNSID) {
5100 struct net *net;
5101
5102 fillargs->netnsid = nla_get_s32(tb[i]);
5103 net = rtnl_get_net_ns_capable(sk, fillargs->netnsid);
5104 if (IS_ERR(net)) {
Bjørn Morkbf4cc402018-10-25 21:18:25 +02005105 fillargs->netnsid = -1;
David Aherned6eff12018-10-07 20:16:29 -07005106 NL_SET_ERR_MSG_MOD(extack, "Invalid target network namespace id");
5107 return PTR_ERR(net);
5108 }
5109 *tgt_net = net;
5110 } else {
5111 NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in dump request");
5112 return -EINVAL;
5113 }
5114 }
5115
5116 return 0;
5117}
5118
Eric Dumazet234b27c2009-11-12 04:11:50 +00005119static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
5120 enum addr_type_t type)
5121{
David Aherned6eff12018-10-07 20:16:29 -07005122 const struct nlmsghdr *nlh = cb->nlh;
David Ahern6ba1e6e2018-10-07 20:16:26 -07005123 struct inet6_fill_args fillargs = {
5124 .portid = NETLINK_CB(cb->skb).portid,
5125 .seq = cb->nlh->nlmsg_seq,
5126 .flags = NLM_F_MULTI,
5127 .netnsid = -1,
5128 .type = type,
5129 };
Eric Dumazet234b27c2009-11-12 04:11:50 +00005130 struct net *net = sock_net(skb->sk);
Christian Brauner6ecf4c32018-09-04 21:53:50 +02005131 struct net *tgt_net = net;
David Ahernfe884c22018-10-19 12:45:28 -07005132 int idx, s_idx, s_ip_idx;
Eric Dumazet234b27c2009-11-12 04:11:50 +00005133 int h, s_h;
Eric Dumazet234b27c2009-11-12 04:11:50 +00005134 struct net_device *dev;
5135 struct inet6_dev *idev;
5136 struct hlist_head *head;
David Ahern242afaa2018-10-24 12:59:00 -07005137 int err = 0;
Eric Dumazet234b27c2009-11-12 04:11:50 +00005138
5139 s_h = cb->args[0];
5140 s_idx = idx = cb->args[1];
David Ahernfe884c22018-10-19 12:45:28 -07005141 s_ip_idx = cb->args[2];
Eric Dumazet234b27c2009-11-12 04:11:50 +00005142
David Aherned6eff12018-10-07 20:16:29 -07005143 if (cb->strict_check) {
David Aherned6eff12018-10-07 20:16:29 -07005144 err = inet6_valid_dump_ifaddr_req(nlh, &fillargs, &tgt_net,
David Ahern6371a712018-10-19 12:45:30 -07005145 skb->sk, cb);
David Aherned6eff12018-10-07 20:16:29 -07005146 if (err < 0)
David Ahern242afaa2018-10-24 12:59:00 -07005147 goto put_tgt_net;
David Ahern6371a712018-10-19 12:45:30 -07005148
David Ahern242afaa2018-10-24 12:59:00 -07005149 err = 0;
David Ahern6371a712018-10-19 12:45:30 -07005150 if (fillargs.ifindex) {
5151 dev = __dev_get_by_index(tgt_net, fillargs.ifindex);
David Ahern242afaa2018-10-24 12:59:00 -07005152 if (!dev) {
5153 err = -ENODEV;
5154 goto put_tgt_net;
5155 }
David Ahern6371a712018-10-19 12:45:30 -07005156 idev = __in6_dev_get(dev);
5157 if (idev) {
5158 err = in6_dump_addrs(idev, skb, cb, s_ip_idx,
5159 &fillargs);
Jakub Kicinski15180392019-01-22 14:47:19 -08005160 if (err > 0)
5161 err = 0;
David Ahern6371a712018-10-19 12:45:30 -07005162 }
5163 goto put_tgt_net;
5164 }
Christian Brauner6ecf4c32018-09-04 21:53:50 +02005165 }
5166
Eric Dumazet234b27c2009-11-12 04:11:50 +00005167 rcu_read_lock();
Christian Brauner6ecf4c32018-09-04 21:53:50 +02005168 cb->seq = atomic_read(&tgt_net->ipv6.dev_addr_genid) ^ tgt_net->dev_base_seq;
Eric Dumazet234b27c2009-11-12 04:11:50 +00005169 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
5170 idx = 0;
Christian Brauner6ecf4c32018-09-04 21:53:50 +02005171 head = &tgt_net->dev_index_head[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -08005172 hlist_for_each_entry_rcu(dev, head, index_hlist) {
Eric Dumazet234b27c2009-11-12 04:11:50 +00005173 if (idx < s_idx)
5174 goto cont;
Patrick McHardy4b97efd2010-03-26 20:27:49 -07005175 if (h > s_h || idx > s_idx)
Eric Dumazet234b27c2009-11-12 04:11:50 +00005176 s_ip_idx = 0;
Stephen Hemmingere21e8462010-03-20 16:09:01 -07005177 idev = __in6_dev_get(dev);
5178 if (!idev)
Eric Dumazet234b27c2009-11-12 04:11:50 +00005179 goto cont;
5180
David Ahernfe884c22018-10-19 12:45:28 -07005181 if (in6_dump_addrs(idev, skb, cb, s_ip_idx,
David Ahern6ba1e6e2018-10-07 20:16:26 -07005182 &fillargs) < 0)
Eric Dumazet234b27c2009-11-12 04:11:50 +00005183 goto done;
5184cont:
5185 idx++;
5186 }
5187 }
5188done:
5189 rcu_read_unlock();
5190 cb->args[0] = h;
5191 cb->args[1] = idx;
David Ahern6371a712018-10-19 12:45:30 -07005192put_tgt_net:
David Ahern6ba1e6e2018-10-07 20:16:26 -07005193 if (fillargs.netnsid >= 0)
Christian Brauner6ecf4c32018-09-04 21:53:50 +02005194 put_net(tgt_net);
Eric Dumazet234b27c2009-11-12 04:11:50 +00005195
Arthur Gautier7c1e8a32018-12-31 02:10:58 +00005196 return skb->len ? : err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005197}
5198
5199static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
5200{
5201 enum addr_type_t type = UNICAST_ADDR;
Denis V. Lunevb8542722007-12-01 00:21:31 +11005202
Linus Torvalds1da177e2005-04-16 15:20:36 -07005203 return inet6_dump_addr(skb, cb, type);
5204}
5205
5206static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
5207{
5208 enum addr_type_t type = MULTICAST_ADDR;
Denis V. Lunevb8542722007-12-01 00:21:31 +11005209
Linus Torvalds1da177e2005-04-16 15:20:36 -07005210 return inet6_dump_addr(skb, cb, type);
5211}
5212
5213
5214static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
5215{
5216 enum addr_type_t type = ANYCAST_ADDR;
Denis V. Lunevb8542722007-12-01 00:21:31 +11005217
Linus Torvalds1da177e2005-04-16 15:20:36 -07005218 return inet6_dump_addr(skb, cb, type);
5219}
5220
Jakub Kicinski4b1373d2019-01-18 10:46:21 -08005221static int inet6_rtm_valid_getaddr_req(struct sk_buff *skb,
5222 const struct nlmsghdr *nlh,
5223 struct nlattr **tb,
5224 struct netlink_ext_ack *extack)
5225{
5226 struct ifaddrmsg *ifm;
5227 int i, err;
5228
5229 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
5230 NL_SET_ERR_MSG_MOD(extack, "Invalid header for get address request");
5231 return -EINVAL;
5232 }
5233
5234 ifm = nlmsg_data(nlh);
5235 if (ifm->ifa_prefixlen || ifm->ifa_flags || ifm->ifa_scope) {
5236 NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for get address request");
5237 return -EINVAL;
5238 }
5239
5240 if (!netlink_strict_get_check(skb))
Johannes Berg8cb08172019-04-26 14:07:28 +02005241 return nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFA_MAX,
5242 ifa_ipv6_policy, extack);
Jakub Kicinski4b1373d2019-01-18 10:46:21 -08005243
Johannes Berg8cb08172019-04-26 14:07:28 +02005244 err = nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, IFA_MAX,
5245 ifa_ipv6_policy, extack);
Jakub Kicinski4b1373d2019-01-18 10:46:21 -08005246 if (err)
5247 return err;
5248
5249 for (i = 0; i <= IFA_MAX; i++) {
5250 if (!tb[i])
5251 continue;
5252
5253 switch (i) {
5254 case IFA_TARGET_NETNSID:
5255 case IFA_ADDRESS:
5256 case IFA_LOCAL:
5257 break;
5258 default:
5259 NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in get address request");
5260 return -EINVAL;
5261 }
5262 }
5263
5264 return 0;
5265}
5266
David Ahernc21ef3e2017-04-16 09:48:24 -07005267static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr *nlh,
5268 struct netlink_ext_ack *extack)
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09005269{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09005270 struct net *net = sock_net(in_skb->sk);
Christian Brauner203651b2018-09-04 21:53:55 +02005271 struct inet6_fill_args fillargs = {
5272 .portid = NETLINK_CB(in_skb).portid,
5273 .seq = nlh->nlmsg_seq,
5274 .event = RTM_NEWADDR,
5275 .flags = 0,
5276 .netnsid = -1,
5277 };
Christian Brauner6ecf4c32018-09-04 21:53:50 +02005278 struct net *tgt_net = net;
Thomas Graf1b29fc22006-09-18 00:10:50 -07005279 struct ifaddrmsg *ifm;
5280 struct nlattr *tb[IFA_MAX+1];
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00005281 struct in6_addr *addr = NULL, *peer;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09005282 struct net_device *dev = NULL;
5283 struct inet6_ifaddr *ifa;
5284 struct sk_buff *skb;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09005285 int err;
5286
Jakub Kicinski4b1373d2019-01-18 10:46:21 -08005287 err = inet6_rtm_valid_getaddr_req(in_skb, nlh, tb, extack);
Thomas Graf1b29fc22006-09-18 00:10:50 -07005288 if (err < 0)
Florian Westphalc24675f2017-10-11 10:28:01 +02005289 return err;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09005290
Christian Brauner6ecf4c32018-09-04 21:53:50 +02005291 if (tb[IFA_TARGET_NETNSID]) {
Christian Brauner203651b2018-09-04 21:53:55 +02005292 fillargs.netnsid = nla_get_s32(tb[IFA_TARGET_NETNSID]);
Christian Brauner6ecf4c32018-09-04 21:53:50 +02005293
5294 tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(in_skb).sk,
Christian Brauner203651b2018-09-04 21:53:55 +02005295 fillargs.netnsid);
Christian Brauner6ecf4c32018-09-04 21:53:50 +02005296 if (IS_ERR(tgt_net))
5297 return PTR_ERR(tgt_net);
5298 }
5299
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00005300 addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer);
Florian Westphalc24675f2017-10-11 10:28:01 +02005301 if (!addr)
5302 return -EINVAL;
Thomas Graf1b29fc22006-09-18 00:10:50 -07005303
5304 ifm = nlmsg_data(nlh);
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09005305 if (ifm->ifa_index)
Christian Brauner6ecf4c32018-09-04 21:53:50 +02005306 dev = dev_get_by_index(tgt_net, ifm->ifa_index);
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09005307
Christian Brauner6ecf4c32018-09-04 21:53:50 +02005308 ifa = ipv6_get_ifaddr(tgt_net, addr, dev, 1);
Stephen Hemmingere21e8462010-03-20 16:09:01 -07005309 if (!ifa) {
Thomas Graf1b29fc22006-09-18 00:10:50 -07005310 err = -EADDRNOTAVAIL;
5311 goto errout;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09005312 }
5313
Stephen Hemmingere21e8462010-03-20 16:09:01 -07005314 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL);
5315 if (!skb) {
Thomas Graf1b29fc22006-09-18 00:10:50 -07005316 err = -ENOBUFS;
5317 goto errout_ifa;
5318 }
5319
Christian Brauner203651b2018-09-04 21:53:55 +02005320 err = inet6_fill_ifaddr(skb, ifa, &fillargs);
Patrick McHardy26932562007-01-31 23:16:40 -08005321 if (err < 0) {
5322 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
5323 WARN_ON(err == -EMSGSIZE);
5324 kfree_skb(skb);
5325 goto errout_ifa;
5326 }
Christian Brauner6ecf4c32018-09-04 21:53:50 +02005327 err = rtnl_unicast(skb, tgt_net, NETLINK_CB(in_skb).portid);
Thomas Graf1b29fc22006-09-18 00:10:50 -07005328errout_ifa:
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09005329 in6_ifa_put(ifa);
Thomas Graf1b29fc22006-09-18 00:10:50 -07005330errout:
Florian Westphalc24675f2017-10-11 10:28:01 +02005331 if (dev)
5332 dev_put(dev);
Christian Brauner203651b2018-09-04 21:53:55 +02005333 if (fillargs.netnsid >= 0)
Christian Brauner6ecf4c32018-09-04 21:53:50 +02005334 put_net(tgt_net);
5335
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09005336 return err;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09005337}
5338
Linus Torvalds1da177e2005-04-16 15:20:36 -07005339static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
5340{
5341 struct sk_buff *skb;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09005342 struct net *net = dev_net(ifa->idev->dev);
Christian Brauner203651b2018-09-04 21:53:55 +02005343 struct inet6_fill_args fillargs = {
5344 .portid = 0,
5345 .seq = 0,
5346 .event = event,
5347 .flags = 0,
5348 .netnsid = -1,
5349 };
Thomas Graf5d620262006-08-15 00:35:02 -07005350 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005351
Thomas Graf0ab68032006-09-18 00:12:35 -07005352 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC);
Ian Morris63159f22015-03-29 14:00:04 +01005353 if (!skb)
Thomas Graf5d620262006-08-15 00:35:02 -07005354 goto errout;
5355
Christian Brauner203651b2018-09-04 21:53:55 +02005356 err = inet6_fill_ifaddr(skb, ifa, &fillargs);
Patrick McHardy26932562007-01-31 23:16:40 -08005357 if (err < 0) {
5358 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
5359 WARN_ON(err == -EMSGSIZE);
5360 kfree_skb(skb);
5361 goto errout;
5362 }
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08005363 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
5364 return;
Thomas Graf5d620262006-08-15 00:35:02 -07005365errout:
5366 if (err < 0)
Benjamin Thery6fda7352008-03-05 10:47:47 -08005367 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005368}
5369
Dave Jonesb6f99a22007-03-22 12:27:49 -07005370static inline void ipv6_store_devconf(struct ipv6_devconf *cnf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005371 __s32 *array, int bytes)
5372{
Thomas Graf04561c12006-11-14 19:53:58 -08005373 BUG_ON(bytes < (DEVCONF_MAX * 4));
5374
Linus Torvalds1da177e2005-04-16 15:20:36 -07005375 memset(array, 0, bytes);
5376 array[DEVCONF_FORWARDING] = cnf->forwarding;
5377 array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
5378 array[DEVCONF_MTU6] = cnf->mtu6;
5379 array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
5380 array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
5381 array[DEVCONF_AUTOCONF] = cnf->autoconf;
5382 array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
5383 array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
Thomas Graf93908d12010-11-17 01:44:24 +00005384 array[DEVCONF_RTR_SOLICIT_INTERVAL] =
5385 jiffies_to_msecs(cnf->rtr_solicit_interval);
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07005386 array[DEVCONF_RTR_SOLICIT_MAX_INTERVAL] =
5387 jiffies_to_msecs(cnf->rtr_solicit_max_interval);
Thomas Graf93908d12010-11-17 01:44:24 +00005388 array[DEVCONF_RTR_SOLICIT_DELAY] =
5389 jiffies_to_msecs(cnf->rtr_solicit_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005390 array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
Hannes Frederic Sowafc4eba52013-08-14 01:03:46 +02005391 array[DEVCONF_MLDV1_UNSOLICITED_REPORT_INTERVAL] =
5392 jiffies_to_msecs(cnf->mldv1_unsolicited_report_interval);
5393 array[DEVCONF_MLDV2_UNSOLICITED_REPORT_INTERVAL] =
5394 jiffies_to_msecs(cnf->mldv2_unsolicited_report_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005395 array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
5396 array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
5397 array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
5398 array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
5399 array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005400 array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -08005401 array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr;
Hangbin Liu8013d1d72015-07-30 14:28:42 +08005402 array[DEVCONF_ACCEPT_RA_MIN_HOP_LIMIT] = cnf->accept_ra_min_hop_limit;
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -08005403 array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo;
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08005404#ifdef CONFIG_IPV6_ROUTER_PREF
5405 array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref;
Thomas Graf93908d12010-11-17 01:44:24 +00005406 array[DEVCONF_RTR_PROBE_INTERVAL] =
5407 jiffies_to_msecs(cnf->rtr_probe_interval);
Neil Hormanfa03ef32007-01-30 14:30:10 -08005408#ifdef CONFIG_IPV6_ROUTE_INFO
Joel Scherpelzbbea1242017-03-22 18:19:04 +09005409 array[DEVCONF_ACCEPT_RA_RT_INFO_MIN_PLEN] = cnf->accept_ra_rt_info_min_plen;
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -08005410 array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen;
5411#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08005412#endif
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -07005413 array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp;
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -07005414 array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route;
Neil Horman95c385b2007-04-25 17:08:10 -07005415#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
5416 array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad;
Erik Kline7fd25612014-10-28 18:11:14 +09005417 array[DEVCONF_USE_OPTIMISTIC] = cnf->use_optimistic;
Neil Horman95c385b2007-04-25 17:08:10 -07005418#endif
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09005419#ifdef CONFIG_IPV6_MROUTE
5420 array[DEVCONF_MC_FORWARDING] = cnf->mc_forwarding;
5421#endif
YOSHIFUJI Hideaki778d80b2008-06-28 14:17:11 +09005422 array[DEVCONF_DISABLE_IPV6] = cnf->disable_ipv6;
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09005423 array[DEVCONF_ACCEPT_DAD] = cnf->accept_dad;
Cosmin Ratiuc3faca02009-10-09 03:11:14 +00005424 array[DEVCONF_FORCE_TLLAO] = cnf->force_tllao;
Hannes Frederic Sowa5cb04432012-11-06 16:46:20 +00005425 array[DEVCONF_NDISC_NOTIFY] = cnf->ndisc_notify;
Hannes Frederic Sowab800c3b2013-08-27 01:36:51 +02005426 array[DEVCONF_SUPPRESS_FRAG_NDISC] = cnf->suppress_frag_ndisc;
Ben Greeard9333192014-06-25 14:44:53 -07005427 array[DEVCONF_ACCEPT_RA_FROM_LOCAL] = cnf->accept_ra_from_local;
Harout Hedeshianc2943f12015-01-20 10:06:05 -07005428 array[DEVCONF_ACCEPT_RA_MTU] = cnf->accept_ra_mtu;
Andy Gospodarek35103d12015-08-13 10:39:01 -04005429 array[DEVCONF_IGNORE_ROUTES_WITH_LINKDOWN] = cnf->ignore_routes_with_linkdown;
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01005430 /* we omit DEVCONF_STABLE_SECRET for now */
Erik Kline3985e8a2015-07-22 16:38:25 +09005431 array[DEVCONF_USE_OIF_ADDRS_ONLY] = cnf->use_oif_addrs_only;
Johannes Bergabbc3042016-02-04 13:31:19 +01005432 array[DEVCONF_DROP_UNICAST_IN_L2_MULTICAST] = cnf->drop_unicast_in_l2_multicast;
Johannes Berg7a02bf82016-02-04 13:31:20 +01005433 array[DEVCONF_DROP_UNSOLICITED_NA] = cnf->drop_unsolicited_na;
David Ahernf1705ec2016-02-24 09:25:37 -08005434 array[DEVCONF_KEEP_ADDR_ON_DOWN] = cnf->keep_addr_on_down;
David Lebrun1ababeb2016-11-08 14:57:39 +01005435 array[DEVCONF_SEG6_ENABLED] = cnf->seg6_enabled;
David Lebrunbf355b82016-11-08 14:57:42 +01005436#ifdef CONFIG_IPV6_SEG6_HMAC
5437 array[DEVCONF_SEG6_REQUIRE_HMAC] = cnf->seg6_require_hmac;
5438#endif
Erik Nordmarkadc176c2016-12-02 14:00:08 -08005439 array[DEVCONF_ENHANCED_DAD] = cnf->enhanced_dad;
Felix Jiad35a00b2017-01-26 16:59:17 +13005440 array[DEVCONF_ADDR_GEN_MODE] = cnf->addr_gen_mode;
David Forsterdf789fe2017-02-23 16:27:18 +00005441 array[DEVCONF_DISABLE_POLICY] = cnf->disable_policy;
Maciej Żenczykowski2210d6b2017-11-07 21:52:09 -08005442 array[DEVCONF_NDISC_TCLASS] = cnf->ndisc_tclass;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005443}
5444
Thomas Grafb382b192010-11-16 04:33:57 +00005445static inline size_t inet6_ifla6_size(void)
5446{
5447 return nla_total_size(4) /* IFLA_INET6_FLAGS */
5448 + nla_total_size(sizeof(struct ifla_cacheinfo))
5449 + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */
5450 + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005451 + nla_total_size(ICMP6_MIB_MAX * 8) /* IFLA_INET6_ICMP6STATS */
Sabrina Dubrocabdd72f42018-07-09 12:25:16 +02005452 + nla_total_size(sizeof(struct in6_addr)) /* IFLA_INET6_TOKEN */
5453 + nla_total_size(1) /* IFLA_INET6_ADDR_GEN_MODE */
5454 + 0;
Thomas Grafb382b192010-11-16 04:33:57 +00005455}
5456
Thomas Graf339bf982006-11-10 14:10:15 -08005457static inline size_t inet6_if_nlmsg_size(void)
5458{
5459 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
5460 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
5461 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
5462 + nla_total_size(4) /* IFLA_MTU */
5463 + nla_total_size(4) /* IFLA_LINK */
Andy Gospodarek03443382015-08-13 15:26:35 -04005464 + nla_total_size(1) /* IFLA_OPERSTATE */
Thomas Grafb382b192010-11-16 04:33:57 +00005465 + nla_total_size(inet6_ifla6_size()); /* IFLA_PROTINFO */
Thomas Graf339bf982006-11-10 14:10:15 -08005466}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07005467
Eric Dumazetbe281e52011-05-19 01:14:23 +00005468static inline void __snmp6_fill_statsdev(u64 *stats, atomic_long_t *mib,
Jia Heaca05672016-09-30 11:29:03 +08005469 int bytes)
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005470{
5471 int i;
Jia Heaca05672016-09-30 11:29:03 +08005472 int pad = bytes - sizeof(u64) * ICMP6_MIB_MAX;
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005473 BUG_ON(pad < 0);
5474
5475 /* Use put_unaligned() because stats may not be aligned for u64. */
Jia Heaca05672016-09-30 11:29:03 +08005476 put_unaligned(ICMP6_MIB_MAX, &stats[0]);
5477 for (i = 1; i < ICMP6_MIB_MAX; i++)
Eric Dumazetbe281e52011-05-19 01:14:23 +00005478 put_unaligned(atomic_long_read(&mib[i]), &stats[i]);
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005479
Jia Heaca05672016-09-30 11:29:03 +08005480 memset(&stats[ICMP6_MIB_MAX], 0, pad);
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005481}
5482
WANG Cong698365f2014-05-05 15:55:55 -07005483static inline void __snmp6_fill_stats64(u64 *stats, void __percpu *mib,
Raghavendra K Ta3a77372015-08-30 11:29:42 +05305484 int bytes, size_t syncpoff)
Eric Dumazet4ce3c182010-06-30 13:31:19 -07005485{
Raghavendra K Ta3a77372015-08-30 11:29:42 +05305486 int i, c;
5487 u64 buff[IPSTATS_MIB_MAX];
5488 int pad = bytes - sizeof(u64) * IPSTATS_MIB_MAX;
5489
Eric Dumazet4ce3c182010-06-30 13:31:19 -07005490 BUG_ON(pad < 0);
5491
Raghavendra K Ta3a77372015-08-30 11:29:42 +05305492 memset(buff, 0, sizeof(buff));
5493 buff[0] = IPSTATS_MIB_MAX;
Eric Dumazet4ce3c182010-06-30 13:31:19 -07005494
Raghavendra K Ta3a77372015-08-30 11:29:42 +05305495 for_each_possible_cpu(c) {
5496 for (i = 1; i < IPSTATS_MIB_MAX; i++)
5497 buff[i] += snmp_get_cpu_field64(mib, c, i, syncpoff);
5498 }
5499
5500 memcpy(stats, buff, IPSTATS_MIB_MAX * sizeof(u64));
5501 memset(&stats[IPSTATS_MIB_MAX], 0, pad);
Eric Dumazet4ce3c182010-06-30 13:31:19 -07005502}
5503
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005504static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype,
5505 int bytes)
5506{
Stephen Hemmingere21e8462010-03-20 16:09:01 -07005507 switch (attrtype) {
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005508 case IFLA_INET6_STATS:
Raghavendra K Ta3a77372015-08-30 11:29:42 +05305509 __snmp6_fill_stats64(stats, idev->stats.ipv6, bytes,
5510 offsetof(struct ipstats_mib, syncp));
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005511 break;
5512 case IFLA_INET6_ICMP6STATS:
Jia Heaca05672016-09-30 11:29:03 +08005513 __snmp6_fill_statsdev(stats, idev->stats.icmpv6dev->mibs, bytes);
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005514 break;
5515 }
5516}
5517
Sowmini Varadhand5566fd2015-09-11 16:48:48 -04005518static int inet6_fill_ifla6_attrs(struct sk_buff *skb, struct inet6_dev *idev,
5519 u32 ext_filter_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005520{
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005521 struct nlattr *nla;
Thomas Graf04561c12006-11-14 19:53:58 -08005522 struct ifla_cacheinfo ci;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005523
David S. Millerc78679e2012-04-01 20:27:33 -04005524 if (nla_put_u32(skb, IFLA_INET6_FLAGS, idev->if_flags))
5525 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005526 ci.max_reasm_len = IPV6_MAXPLEN;
David S. Miller24912422010-11-19 13:13:47 -08005527 ci.tstamp = cstamp_delta(idev->tstamp);
5528 ci.reachable_time = jiffies_to_msecs(idev->nd_parms->reachable_time);
Jiri Pirko1f9248e2013-12-07 19:26:53 +01005529 ci.retrans_time = jiffies_to_msecs(NEIGH_VAR(idev->nd_parms, RETRANS_TIME));
David S. Millerc78679e2012-04-01 20:27:33 -04005530 if (nla_put(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci))
5531 goto nla_put_failure;
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005532 nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32));
Ian Morris63159f22015-03-29 14:00:04 +01005533 if (!nla)
Thomas Graf04561c12006-11-14 19:53:58 -08005534 goto nla_put_failure;
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005535 ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005536
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005537 /* XXX - MC not implemented */
5538
Sowmini Varadhand5566fd2015-09-11 16:48:48 -04005539 if (ext_filter_mask & RTEXT_FILTER_SKIP_STATS)
5540 return 0;
5541
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005542 nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64));
Ian Morris63159f22015-03-29 14:00:04 +01005543 if (!nla)
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005544 goto nla_put_failure;
5545 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla));
5546
5547 nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64));
Ian Morris63159f22015-03-29 14:00:04 +01005548 if (!nla)
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005549 goto nla_put_failure;
5550 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005551
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005552 nla = nla_reserve(skb, IFLA_INET6_TOKEN, sizeof(struct in6_addr));
Ian Morris63159f22015-03-29 14:00:04 +01005553 if (!nla)
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005554 goto nla_put_failure;
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02005555
Felix Jiad35a00b2017-01-26 16:59:17 +13005556 if (nla_put_u8(skb, IFLA_INET6_ADDR_GEN_MODE, idev->cnf.addr_gen_mode))
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02005557 goto nla_put_failure;
5558
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005559 read_lock_bh(&idev->lock);
5560 memcpy(nla_data(nla), idev->token.s6_addr, nla_len(nla));
5561 read_unlock_bh(&idev->lock);
5562
Thomas Grafb382b192010-11-16 04:33:57 +00005563 return 0;
5564
5565nla_put_failure:
5566 return -EMSGSIZE;
5567}
5568
Arad, Ronenb1974ed2015-10-19 09:23:28 -07005569static size_t inet6_get_link_af_size(const struct net_device *dev,
5570 u32 ext_filter_mask)
Thomas Grafb382b192010-11-16 04:33:57 +00005571{
5572 if (!__in6_dev_get(dev))
5573 return 0;
5574
5575 return inet6_ifla6_size();
5576}
5577
Sowmini Varadhand5566fd2015-09-11 16:48:48 -04005578static int inet6_fill_link_af(struct sk_buff *skb, const struct net_device *dev,
5579 u32 ext_filter_mask)
Thomas Grafb382b192010-11-16 04:33:57 +00005580{
5581 struct inet6_dev *idev = __in6_dev_get(dev);
5582
5583 if (!idev)
5584 return -ENODATA;
5585
Sowmini Varadhand5566fd2015-09-11 16:48:48 -04005586 if (inet6_fill_ifla6_attrs(skb, idev, ext_filter_mask) < 0)
Thomas Grafb382b192010-11-16 04:33:57 +00005587 return -EMSGSIZE;
5588
5589 return 0;
5590}
5591
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005592static int inet6_set_iftoken(struct inet6_dev *idev, struct in6_addr *token)
5593{
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005594 struct inet6_ifaddr *ifp;
5595 struct net_device *dev = idev->dev;
Daniel Borkmann47e27d52016-04-08 15:55:00 +02005596 bool clear_token, update_rs = false;
Hannes Frederic Sowadc848292013-06-24 21:42:40 +02005597 struct in6_addr ll_addr;
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005598
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01005599 ASSERT_RTNL();
5600
Ian Morris63159f22015-03-29 14:00:04 +01005601 if (!token)
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005602 return -EINVAL;
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005603 if (dev->flags & (IFF_LOOPBACK | IFF_NOARP))
5604 return -EINVAL;
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005605 if (!ipv6_accept_ra(idev))
5606 return -EINVAL;
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07005607 if (idev->cnf.rtr_solicits == 0)
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005608 return -EINVAL;
5609
5610 write_lock_bh(&idev->lock);
5611
5612 BUILD_BUG_ON(sizeof(token->s6_addr) != 16);
5613 memcpy(idev->token.s6_addr + 8, token->s6_addr + 8, 8);
5614
5615 write_unlock_bh(&idev->lock);
5616
Daniel Borkmann47e27d52016-04-08 15:55:00 +02005617 clear_token = ipv6_addr_any(token);
5618 if (clear_token)
5619 goto update_lft;
5620
Hannes Frederic Sowadc848292013-06-24 21:42:40 +02005621 if (!idev->dead && (idev->if_flags & IF_READY) &&
5622 !ipv6_get_lladdr(dev, &ll_addr, IFA_F_TENTATIVE |
5623 IFA_F_OPTIMISTIC)) {
Daniel Borkmannfc403832013-04-09 03:47:15 +00005624 /* If we're not ready, then normal ifup will take care
5625 * of this. Otherwise, we need to request our rs here.
5626 */
5627 ndisc_send_rs(dev, &ll_addr, &in6addr_linklocal_allrouters);
5628 update_rs = true;
5629 }
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005630
Daniel Borkmann47e27d52016-04-08 15:55:00 +02005631update_lft:
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005632 write_lock_bh(&idev->lock);
Daniel Borkmannfc403832013-04-09 03:47:15 +00005633
Hannes Frederic Sowa77ecaac2013-06-26 03:41:49 +02005634 if (update_rs) {
Daniel Borkmannfc403832013-04-09 03:47:15 +00005635 idev->if_flags |= IF_RS_SENT;
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07005636 idev->rs_interval = rfc3315_s14_backoff_init(
5637 idev->cnf.rtr_solicit_interval);
Hannes Frederic Sowa77ecaac2013-06-26 03:41:49 +02005638 idev->rs_probes = 1;
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07005639 addrconf_mod_rs_timer(idev, idev->rs_interval);
Hannes Frederic Sowa77ecaac2013-06-26 03:41:49 +02005640 }
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005641
5642 /* Well, that's kinda nasty ... */
5643 list_for_each_entry(ifp, &idev->addr_list, if_list) {
5644 spin_lock(&ifp->lock);
Daniel Borkmann617fe292013-04-09 03:47:16 +00005645 if (ifp->tokenized) {
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005646 ifp->valid_lft = 0;
5647 ifp->prefered_lft = 0;
5648 }
5649 spin_unlock(&ifp->lock);
5650 }
5651
5652 write_unlock_bh(&idev->lock);
Lubomir Rintelb2ed64a2014-10-27 17:39:16 +01005653 inet6_ifinfo_notify(RTM_NEWLINK, idev);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01005654 addrconf_verify_rtnl();
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005655 return 0;
5656}
5657
Daniel Borkmann11b1f822015-02-05 14:39:11 +01005658static const struct nla_policy inet6_af_policy[IFLA_INET6_MAX + 1] = {
5659 [IFLA_INET6_ADDR_GEN_MODE] = { .type = NLA_U8 },
5660 [IFLA_INET6_TOKEN] = { .len = sizeof(struct in6_addr) },
5661};
5662
Felix Jiad35a00b2017-01-26 16:59:17 +13005663static int check_addr_gen_mode(int mode)
5664{
5665 if (mode != IN6_ADDR_GEN_MODE_EUI64 &&
5666 mode != IN6_ADDR_GEN_MODE_NONE &&
5667 mode != IN6_ADDR_GEN_MODE_STABLE_PRIVACY &&
5668 mode != IN6_ADDR_GEN_MODE_RANDOM)
5669 return -EINVAL;
5670 return 1;
5671}
5672
5673static int check_stable_privacy(struct inet6_dev *idev, struct net *net,
5674 int mode)
5675{
5676 if (mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY &&
5677 !idev->cnf.stable_secret.initialized &&
5678 !net->ipv6.devconf_dflt->stable_secret.initialized)
5679 return -EINVAL;
5680 return 1;
5681}
5682
Maxim Mikityanskiy7dc2bcc2019-05-21 06:40:04 +00005683static int inet6_validate_link_af(const struct net_device *dev,
5684 const struct nlattr *nla)
5685{
5686 struct nlattr *tb[IFLA_INET6_MAX + 1];
5687 struct inet6_dev *idev = NULL;
5688 int err;
5689
5690 if (dev) {
5691 idev = __in6_dev_get(dev);
5692 if (!idev)
5693 return -EAFNOSUPPORT;
5694 }
5695
5696 err = nla_parse_nested_deprecated(tb, IFLA_INET6_MAX, nla,
5697 inet6_af_policy, NULL);
5698 if (err)
5699 return err;
5700
5701 if (!tb[IFLA_INET6_TOKEN] && !tb[IFLA_INET6_ADDR_GEN_MODE])
5702 return -EINVAL;
5703
5704 if (tb[IFLA_INET6_ADDR_GEN_MODE]) {
5705 u8 mode = nla_get_u8(tb[IFLA_INET6_ADDR_GEN_MODE]);
5706
5707 if (check_addr_gen_mode(mode) < 0)
5708 return -EINVAL;
5709 if (dev && check_stable_privacy(idev, dev_net(dev), mode) < 0)
5710 return -EINVAL;
5711 }
5712
5713 return 0;
5714}
5715
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005716static int inet6_set_link_af(struct net_device *dev, const struct nlattr *nla)
5717{
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005718 struct inet6_dev *idev = __in6_dev_get(dev);
5719 struct nlattr *tb[IFLA_INET6_MAX + 1];
Maxim Mikityanskiy7dc2bcc2019-05-21 06:40:04 +00005720 int err;
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005721
Johannes Berg8cb08172019-04-26 14:07:28 +02005722 if (nla_parse_nested_deprecated(tb, IFLA_INET6_MAX, nla, NULL, NULL) < 0)
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005723 BUG();
5724
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02005725 if (tb[IFLA_INET6_TOKEN]) {
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005726 err = inet6_set_iftoken(idev, nla_data(tb[IFLA_INET6_TOKEN]));
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02005727 if (err)
5728 return err;
5729 }
5730
5731 if (tb[IFLA_INET6_ADDR_GEN_MODE]) {
5732 u8 mode = nla_get_u8(tb[IFLA_INET6_ADDR_GEN_MODE]);
5733
Felix Jiad35a00b2017-01-26 16:59:17 +13005734 idev->cnf.addr_gen_mode = mode;
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02005735 }
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005736
Maxim Mikityanskiy7dc2bcc2019-05-21 06:40:04 +00005737 return 0;
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005738}
5739
Thomas Grafb382b192010-11-16 04:33:57 +00005740static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005741 u32 portid, u32 seq, int event, unsigned int flags)
Thomas Grafb382b192010-11-16 04:33:57 +00005742{
5743 struct net_device *dev = idev->dev;
5744 struct ifinfomsg *hdr;
5745 struct nlmsghdr *nlh;
5746 void *protoinfo;
5747
Eric W. Biederman15e47302012-09-07 20:12:54 +00005748 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*hdr), flags);
Ian Morris63159f22015-03-29 14:00:04 +01005749 if (!nlh)
Thomas Grafb382b192010-11-16 04:33:57 +00005750 return -EMSGSIZE;
5751
5752 hdr = nlmsg_data(nlh);
5753 hdr->ifi_family = AF_INET6;
5754 hdr->__ifi_pad = 0;
5755 hdr->ifi_type = dev->type;
5756 hdr->ifi_index = dev->ifindex;
5757 hdr->ifi_flags = dev_get_flags(dev);
5758 hdr->ifi_change = 0;
5759
David S. Millerc78679e2012-04-01 20:27:33 -04005760 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
5761 (dev->addr_len &&
5762 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
5763 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
Nicolas Dichtela54acb32015-04-02 17:07:00 +02005764 (dev->ifindex != dev_get_iflink(dev) &&
Andy Gospodarek03443382015-08-13 15:26:35 -04005765 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))) ||
5766 nla_put_u8(skb, IFLA_OPERSTATE,
5767 netif_running(dev) ? dev->operstate : IF_OPER_DOWN))
David S. Millerc78679e2012-04-01 20:27:33 -04005768 goto nla_put_failure;
Michal Kubecekae0be8d2019-04-26 11:13:06 +02005769 protoinfo = nla_nest_start_noflag(skb, IFLA_PROTINFO);
Ian Morris63159f22015-03-29 14:00:04 +01005770 if (!protoinfo)
Thomas Grafb382b192010-11-16 04:33:57 +00005771 goto nla_put_failure;
5772
Sowmini Varadhand5566fd2015-09-11 16:48:48 -04005773 if (inet6_fill_ifla6_attrs(skb, idev, 0) < 0)
Thomas Grafb382b192010-11-16 04:33:57 +00005774 goto nla_put_failure;
5775
Thomas Graf04561c12006-11-14 19:53:58 -08005776 nla_nest_end(skb, protoinfo);
Johannes Berg053c0952015-01-16 22:09:00 +01005777 nlmsg_end(skb, nlh);
5778 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005779
Thomas Graf04561c12006-11-14 19:53:58 -08005780nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08005781 nlmsg_cancel(skb, nlh);
5782 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005783}
5784
David Ahern786e0002018-10-07 20:16:33 -07005785static int inet6_valid_dump_ifinfo(const struct nlmsghdr *nlh,
5786 struct netlink_ext_ack *extack)
5787{
5788 struct ifinfomsg *ifm;
5789
5790 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
5791 NL_SET_ERR_MSG_MOD(extack, "Invalid header for link dump request");
5792 return -EINVAL;
5793 }
5794
5795 if (nlmsg_attrlen(nlh, sizeof(*ifm))) {
5796 NL_SET_ERR_MSG_MOD(extack, "Invalid data after header");
5797 return -EINVAL;
5798 }
5799
5800 ifm = nlmsg_data(nlh);
5801 if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
5802 ifm->ifi_change || ifm->ifi_index) {
5803 NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for dump request");
5804 return -EINVAL;
5805 }
5806
5807 return 0;
5808}
5809
Linus Torvalds1da177e2005-04-16 15:20:36 -07005810static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
5811{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09005812 struct net *net = sock_net(skb->sk);
Eric Dumazet84d26972009-11-09 12:11:28 +00005813 int h, s_h;
David S. Miller434a8a52009-11-11 18:53:00 -08005814 int idx = 0, s_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005815 struct net_device *dev;
5816 struct inet6_dev *idev;
Eric Dumazet84d26972009-11-09 12:11:28 +00005817 struct hlist_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005818
David Ahern786e0002018-10-07 20:16:33 -07005819 /* only requests using strict checking can pass data to
5820 * influence the dump
5821 */
5822 if (cb->strict_check) {
5823 int err = inet6_valid_dump_ifinfo(cb->nlh, cb->extack);
5824
5825 if (err < 0)
5826 return err;
5827 }
5828
Eric Dumazet84d26972009-11-09 12:11:28 +00005829 s_h = cb->args[0];
5830 s_idx = cb->args[1];
5831
5832 rcu_read_lock();
5833 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
5834 idx = 0;
5835 head = &net->dev_index_head[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -08005836 hlist_for_each_entry_rcu(dev, head, index_hlist) {
Eric Dumazet84d26972009-11-09 12:11:28 +00005837 if (idx < s_idx)
5838 goto cont;
5839 idev = __in6_dev_get(dev);
5840 if (!idev)
5841 goto cont;
5842 if (inet6_fill_ifinfo(skb, idev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005843 NETLINK_CB(cb->skb).portid,
Eric Dumazet84d26972009-11-09 12:11:28 +00005844 cb->nlh->nlmsg_seq,
Johannes Berg053c0952015-01-16 22:09:00 +01005845 RTM_NEWLINK, NLM_F_MULTI) < 0)
Eric Dumazet84d26972009-11-09 12:11:28 +00005846 goto out;
Pavel Emelianov7562f872007-05-03 15:13:45 -07005847cont:
Eric Dumazet84d26972009-11-09 12:11:28 +00005848 idx++;
5849 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005850 }
Eric Dumazet84d26972009-11-09 12:11:28 +00005851out:
5852 rcu_read_unlock();
5853 cb->args[1] = idx;
5854 cb->args[0] = h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005855
5856 return skb->len;
5857}
5858
5859void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
5860{
5861 struct sk_buff *skb;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09005862 struct net *net = dev_net(idev->dev);
Thomas Graf8d7a76c2006-08-15 00:35:47 -07005863 int err = -ENOBUFS;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09005864
Thomas Graf339bf982006-11-10 14:10:15 -08005865 skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC);
Ian Morris63159f22015-03-29 14:00:04 +01005866 if (!skb)
Thomas Graf8d7a76c2006-08-15 00:35:47 -07005867 goto errout;
5868
5869 err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08005870 if (err < 0) {
5871 /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */
5872 WARN_ON(err == -EMSGSIZE);
5873 kfree_skb(skb);
5874 goto errout;
5875 }
Nicolas Dichtel5f75a102010-12-07 23:38:31 +00005876 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFINFO, NULL, GFP_ATOMIC);
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08005877 return;
Thomas Graf8d7a76c2006-08-15 00:35:47 -07005878errout:
5879 if (err < 0)
Nicolas Dichtel5f75a102010-12-07 23:38:31 +00005880 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFINFO, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005881}
5882
Thomas Graf339bf982006-11-10 14:10:15 -08005883static inline size_t inet6_prefix_nlmsg_size(void)
5884{
5885 return NLMSG_ALIGN(sizeof(struct prefixmsg))
5886 + nla_total_size(sizeof(struct in6_addr))
5887 + nla_total_size(sizeof(struct prefix_cacheinfo));
5888}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07005889
Linus Torvalds1da177e2005-04-16 15:20:36 -07005890static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005891 struct prefix_info *pinfo, u32 portid, u32 seq,
Thomas Graf6051e2f2006-11-14 19:54:19 -08005892 int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005893{
Thomas Graf6051e2f2006-11-14 19:54:19 -08005894 struct prefixmsg *pmsg;
5895 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005896 struct prefix_cacheinfo ci;
5897
Eric W. Biederman15e47302012-09-07 20:12:54 +00005898 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*pmsg), flags);
Ian Morris63159f22015-03-29 14:00:04 +01005899 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08005900 return -EMSGSIZE;
Thomas Graf6051e2f2006-11-14 19:54:19 -08005901
5902 pmsg = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005903 pmsg->prefix_family = AF_INET6;
Patrick McHardy8a470772005-06-28 12:56:45 -07005904 pmsg->prefix_pad1 = 0;
5905 pmsg->prefix_pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005906 pmsg->prefix_ifindex = idev->dev->ifindex;
5907 pmsg->prefix_len = pinfo->prefix_len;
5908 pmsg->prefix_type = pinfo->type;
Patrick McHardy8a470772005-06-28 12:56:45 -07005909 pmsg->prefix_pad3 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005910 pmsg->prefix_flags = 0;
5911 if (pinfo->onlink)
5912 pmsg->prefix_flags |= IF_PREFIX_ONLINK;
5913 if (pinfo->autoconf)
5914 pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
5915
David S. Millerc78679e2012-04-01 20:27:33 -04005916 if (nla_put(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix))
5917 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005918 ci.preferred_time = ntohl(pinfo->prefered);
5919 ci.valid_time = ntohl(pinfo->valid);
David S. Millerc78679e2012-04-01 20:27:33 -04005920 if (nla_put(skb, PREFIX_CACHEINFO, sizeof(ci), &ci))
5921 goto nla_put_failure;
Johannes Berg053c0952015-01-16 22:09:00 +01005922 nlmsg_end(skb, nlh);
5923 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005924
Thomas Graf6051e2f2006-11-14 19:54:19 -08005925nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08005926 nlmsg_cancel(skb, nlh);
5927 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005928}
5929
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09005930static void inet6_prefix_notify(int event, struct inet6_dev *idev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005931 struct prefix_info *pinfo)
5932{
5933 struct sk_buff *skb;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09005934 struct net *net = dev_net(idev->dev);
Thomas Graf8c384bfa2006-08-15 00:36:07 -07005935 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005936
Thomas Graf339bf982006-11-10 14:10:15 -08005937 skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC);
Ian Morris63159f22015-03-29 14:00:04 +01005938 if (!skb)
Thomas Graf8c384bfa2006-08-15 00:36:07 -07005939 goto errout;
5940
5941 err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08005942 if (err < 0) {
5943 /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */
5944 WARN_ON(err == -EMSGSIZE);
5945 kfree_skb(skb);
5946 goto errout;
5947 }
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08005948 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC);
5949 return;
Thomas Graf8c384bfa2006-08-15 00:36:07 -07005950errout:
5951 if (err < 0)
Benjamin Thery6fda7352008-03-05 10:47:47 -08005952 rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005953}
5954
Linus Torvalds1da177e2005-04-16 15:20:36 -07005955static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
5956{
Nicolas Dichtel63998ac2013-03-22 06:28:43 +00005957 struct net *net = dev_net(ifp->idev->dev);
5958
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01005959 if (event)
5960 ASSERT_RTNL();
5961
Linus Torvalds1da177e2005-04-16 15:20:36 -07005962 inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
5963
5964 switch (event) {
5965 case RTM_NEWADDR:
Neil Horman95c385b2007-04-25 17:08:10 -07005966 /*
5967 * If the address was optimistic
5968 * we inserted the route at the start of
5969 * our DAD process, so we don't need
5970 * to do it again
5971 */
David Ahern93c2fb22018-04-18 15:38:59 -07005972 if (!rcu_access_pointer(ifp->rt->fib6_node))
David Ahernafb1d4b52018-04-17 17:33:11 -07005973 ip6_ins_rt(net, ifp->rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005974 if (ifp->idev->cnf.forwarding)
5975 addrconf_join_anycast(ifp);
Cong Wang7996c792013-05-22 05:41:06 +00005976 if (!ipv6_addr_any(&ifp->peer_addr))
David Ahern8308f3f2018-05-27 08:09:58 -07005977 addrconf_prefix_route(&ifp->peer_addr, 128, 0,
David Ahernacb54e32018-04-17 17:33:22 -07005978 ifp->idev->dev, 0, 0,
David Ahern27b10602018-04-18 15:39:06 -07005979 GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005980 break;
5981 case RTM_DELADDR:
5982 if (ifp->idev->cnf.forwarding)
5983 addrconf_leave_anycast(ifp);
5984 addrconf_leave_solict(ifp->idev, &ifp->addr);
Cong Wang7996c792013-05-22 05:41:06 +00005985 if (!ipv6_addr_any(&ifp->peer_addr)) {
David Ahern8d1c8022018-04-17 17:33:26 -07005986 struct fib6_info *rt;
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00005987
Nicolas Dichtele7478df2014-09-03 23:59:22 +02005988 rt = addrconf_get_prefix_route(&ifp->peer_addr, 128,
David Ahern2b2450c2019-03-27 20:53:52 -07005989 ifp->idev->dev, 0, 0,
5990 false);
Martin KaFai Lau8e3d5be2015-09-15 14:30:08 -07005991 if (rt)
David Ahernafb1d4b52018-04-17 17:33:11 -07005992 ip6_del_rt(net, rt);
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00005993 }
David Ahern38bd10c2016-04-21 20:56:12 -07005994 if (ifp->rt) {
David Ahern93531c62018-04-17 17:33:25 -07005995 ip6_del_rt(net, ifp->rt);
5996 ifp->rt = NULL;
David Ahern38bd10c2016-04-21 20:56:12 -07005997 }
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02005998 rt_genid_bump_ipv6(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005999 break;
6000 }
Nicolas Dichtel63998ac2013-03-22 06:28:43 +00006001 atomic_inc(&net->ipv6.dev_addr_genid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006002}
6003
6004static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
6005{
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07006006 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006007 if (likely(ifp->idev->dead == 0))
6008 __ipv6_ifa_notify(event, ifp);
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07006009 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006010}
6011
6012#ifdef CONFIG_SYSCTL
6013
6014static
Joe Perchesfe2c6332013-06-11 23:04:25 -07006015int addrconf_sysctl_forward(struct ctl_table *ctl, int write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006016 void __user *buffer, size_t *lenp, loff_t *ppos)
6017{
6018 int *valp = ctl->data;
6019 int val = *valp;
Eric W. Biederman88af1822010-02-19 13:22:59 +00006020 loff_t pos = *ppos;
Joe Perchesfe2c6332013-06-11 23:04:25 -07006021 struct ctl_table lctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006022 int ret;
6023
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00006024 /*
6025 * ctl->data points to idev->cnf.forwarding, we should
6026 * not modify it until we get the rtnl lock.
6027 */
6028 lctl = *ctl;
6029 lctl.data = &val;
6030
6031 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006032
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -08006033 if (write)
Stephen Hemmingerb325fdd2009-02-26 06:55:31 +00006034 ret = addrconf_fixup_forwarding(ctl, valp, val);
Eric W. Biederman88af1822010-02-19 13:22:59 +00006035 if (ret)
6036 *ppos = pos;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09006037 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006038}
6039
Marcelo Leitner77751422015-02-23 11:17:13 -03006040static
6041int addrconf_sysctl_mtu(struct ctl_table *ctl, int write,
6042 void __user *buffer, size_t *lenp, loff_t *ppos)
6043{
6044 struct inet6_dev *idev = ctl->extra1;
6045 int min_mtu = IPV6_MIN_MTU;
6046 struct ctl_table lctl;
6047
6048 lctl = *ctl;
6049 lctl.extra1 = &min_mtu;
6050 lctl.extra2 = idev ? &idev->dev->mtu : NULL;
6051
6052 return proc_dointvec_minmax(&lctl, write, buffer, lenp, ppos);
6053}
6054
Brian Haley56d417b2009-06-01 03:07:33 -07006055static void dev_disable_change(struct inet6_dev *idev)
6056{
Cong Wang75538c22013-05-29 11:30:50 +08006057 struct netdev_notifier_info info;
6058
Brian Haley56d417b2009-06-01 03:07:33 -07006059 if (!idev || !idev->dev)
6060 return;
6061
Cong Wang75538c22013-05-29 11:30:50 +08006062 netdev_notifier_info_init(&info, idev->dev);
Brian Haley56d417b2009-06-01 03:07:33 -07006063 if (idev->cnf.disable_ipv6)
Cong Wang75538c22013-05-29 11:30:50 +08006064 addrconf_notify(NULL, NETDEV_DOWN, &info);
Brian Haley56d417b2009-06-01 03:07:33 -07006065 else
Cong Wang75538c22013-05-29 11:30:50 +08006066 addrconf_notify(NULL, NETDEV_UP, &info);
Brian Haley56d417b2009-06-01 03:07:33 -07006067}
6068
6069static void addrconf_disable_change(struct net *net, __s32 newf)
6070{
6071 struct net_device *dev;
6072 struct inet6_dev *idev;
6073
Kefeng Wang03e4def2017-01-19 16:26:21 +08006074 for_each_netdev(net, dev) {
Brian Haley56d417b2009-06-01 03:07:33 -07006075 idev = __in6_dev_get(dev);
6076 if (idev) {
6077 int changed = (!idev->cnf.disable_ipv6) ^ (!newf);
6078 idev->cnf.disable_ipv6 = newf;
6079 if (changed)
6080 dev_disable_change(idev);
6081 }
Brian Haley56d417b2009-06-01 03:07:33 -07006082 }
Brian Haley56d417b2009-06-01 03:07:33 -07006083}
6084
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00006085static int addrconf_disable_ipv6(struct ctl_table *table, int *p, int newf)
Brian Haley56d417b2009-06-01 03:07:33 -07006086{
6087 struct net *net;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00006088 int old;
6089
6090 if (!rtnl_trylock())
6091 return restart_syscall();
Brian Haley56d417b2009-06-01 03:07:33 -07006092
6093 net = (struct net *)table->extra2;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00006094 old = *p;
6095 *p = newf;
Brian Haley56d417b2009-06-01 03:07:33 -07006096
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00006097 if (p == &net->ipv6.devconf_dflt->disable_ipv6) {
6098 rtnl_unlock();
Brian Haley56d417b2009-06-01 03:07:33 -07006099 return 0;
Eric W. Biederman88af1822010-02-19 13:22:59 +00006100 }
Brian Haley56d417b2009-06-01 03:07:33 -07006101
6102 if (p == &net->ipv6.devconf_all->disable_ipv6) {
Brian Haley56d417b2009-06-01 03:07:33 -07006103 net->ipv6.devconf_dflt->disable_ipv6 = newf;
6104 addrconf_disable_change(net, newf);
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00006105 } else if ((!newf) ^ (!old))
Brian Haley56d417b2009-06-01 03:07:33 -07006106 dev_disable_change((struct inet6_dev *)table->extra1);
6107
6108 rtnl_unlock();
6109 return 0;
6110}
6111
6112static
Joe Perchesfe2c6332013-06-11 23:04:25 -07006113int addrconf_sysctl_disable(struct ctl_table *ctl, int write,
Brian Haley56d417b2009-06-01 03:07:33 -07006114 void __user *buffer, size_t *lenp, loff_t *ppos)
6115{
6116 int *valp = ctl->data;
6117 int val = *valp;
Eric W. Biederman88af1822010-02-19 13:22:59 +00006118 loff_t pos = *ppos;
Joe Perchesfe2c6332013-06-11 23:04:25 -07006119 struct ctl_table lctl;
Brian Haley56d417b2009-06-01 03:07:33 -07006120 int ret;
6121
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00006122 /*
6123 * ctl->data points to idev->cnf.disable_ipv6, we should
6124 * not modify it until we get the rtnl lock.
6125 */
6126 lctl = *ctl;
6127 lctl.data = &val;
6128
6129 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
Brian Haley56d417b2009-06-01 03:07:33 -07006130
6131 if (write)
6132 ret = addrconf_disable_ipv6(ctl, valp, val);
Eric W. Biederman88af1822010-02-19 13:22:59 +00006133 if (ret)
6134 *ppos = pos;
Brian Haley56d417b2009-06-01 03:07:33 -07006135 return ret;
6136}
6137
stephen hemmingerc92d5492013-12-17 22:37:14 -08006138static
6139int addrconf_sysctl_proxy_ndp(struct ctl_table *ctl, int write,
6140 void __user *buffer, size_t *lenp, loff_t *ppos)
6141{
6142 int *valp = ctl->data;
6143 int ret;
6144 int old, new;
6145
6146 old = *valp;
6147 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
6148 new = *valp;
6149
6150 if (write && old != new) {
6151 struct net *net = ctl->extra2;
6152
6153 if (!rtnl_trylock())
6154 return restart_syscall();
6155
6156 if (valp == &net->ipv6.devconf_dflt->proxy_ndp)
David Ahern85b3daa2017-03-28 14:28:04 -07006157 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
6158 NETCONFA_PROXY_NEIGH,
stephen hemmingerc92d5492013-12-17 22:37:14 -08006159 NETCONFA_IFINDEX_DEFAULT,
6160 net->ipv6.devconf_dflt);
6161 else if (valp == &net->ipv6.devconf_all->proxy_ndp)
David Ahern85b3daa2017-03-28 14:28:04 -07006162 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
6163 NETCONFA_PROXY_NEIGH,
stephen hemmingerc92d5492013-12-17 22:37:14 -08006164 NETCONFA_IFINDEX_ALL,
6165 net->ipv6.devconf_all);
6166 else {
6167 struct inet6_dev *idev = ctl->extra1;
6168
David Ahern85b3daa2017-03-28 14:28:04 -07006169 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
6170 NETCONFA_PROXY_NEIGH,
stephen hemmingerc92d5492013-12-17 22:37:14 -08006171 idev->dev->ifindex,
6172 &idev->cnf);
6173 }
6174 rtnl_unlock();
6175 }
6176
6177 return ret;
6178}
6179
Felix Jiad35a00b2017-01-26 16:59:17 +13006180static int addrconf_sysctl_addr_gen_mode(struct ctl_table *ctl, int write,
6181 void __user *buffer, size_t *lenp,
6182 loff_t *ppos)
6183{
6184 int ret = 0;
Sabrina Dubrocac6dbf7a2018-07-09 12:25:14 +02006185 u32 new_val;
Felix Jiad35a00b2017-01-26 16:59:17 +13006186 struct inet6_dev *idev = (struct inet6_dev *)ctl->extra1;
6187 struct net *net = (struct net *)ctl->extra2;
Sabrina Dubrocac6dbf7a2018-07-09 12:25:14 +02006188 struct ctl_table tmp = {
6189 .data = &new_val,
6190 .maxlen = sizeof(new_val),
6191 .mode = ctl->mode,
6192 };
Felix Jiad35a00b2017-01-26 16:59:17 +13006193
Felix Jia8c171d62017-02-27 12:41:23 +13006194 if (!rtnl_trylock())
6195 return restart_syscall();
6196
Sabrina Dubrocac6dbf7a2018-07-09 12:25:14 +02006197 new_val = *((u32 *)ctl->data);
6198
6199 ret = proc_douintvec(&tmp, write, buffer, lenp, ppos);
6200 if (ret != 0)
6201 goto out;
Felix Jiad35a00b2017-01-26 16:59:17 +13006202
6203 if (write) {
Felix Jia8c171d62017-02-27 12:41:23 +13006204 if (check_addr_gen_mode(new_val) < 0) {
6205 ret = -EINVAL;
6206 goto out;
6207 }
Felix Jiad35a00b2017-01-26 16:59:17 +13006208
Sabrina Dubrocac6dbf7a2018-07-09 12:25:14 +02006209 if (idev) {
Felix Jia8c171d62017-02-27 12:41:23 +13006210 if (check_stable_privacy(idev, net, new_val) < 0) {
6211 ret = -EINVAL;
6212 goto out;
6213 }
Felix Jiad35a00b2017-01-26 16:59:17 +13006214
6215 if (idev->cnf.addr_gen_mode != new_val) {
6216 idev->cnf.addr_gen_mode = new_val;
Felix Jiad35a00b2017-01-26 16:59:17 +13006217 addrconf_dev_config(idev->dev);
Felix Jiad35a00b2017-01-26 16:59:17 +13006218 }
Sabrina Dubrocaf24c5982018-07-09 12:25:17 +02006219 } else if (&net->ipv6.devconf_all->addr_gen_mode == ctl->data) {
6220 struct net_device *dev;
6221
6222 net->ipv6.devconf_dflt->addr_gen_mode = new_val;
6223 for_each_netdev(net, dev) {
6224 idev = __in6_dev_get(dev);
6225 if (idev &&
6226 idev->cnf.addr_gen_mode != new_val) {
6227 idev->cnf.addr_gen_mode = new_val;
6228 addrconf_dev_config(idev->dev);
6229 }
6230 }
Felix Jiad35a00b2017-01-26 16:59:17 +13006231 }
Sabrina Dubrocac6dbf7a2018-07-09 12:25:14 +02006232
6233 *((u32 *)ctl->data) = new_val;
Felix Jiad35a00b2017-01-26 16:59:17 +13006234 }
6235
Felix Jia8c171d62017-02-27 12:41:23 +13006236out:
6237 rtnl_unlock();
6238
Felix Jiad35a00b2017-01-26 16:59:17 +13006239 return ret;
6240}
6241
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01006242static int addrconf_sysctl_stable_secret(struct ctl_table *ctl, int write,
6243 void __user *buffer, size_t *lenp,
6244 loff_t *ppos)
6245{
6246 int err;
6247 struct in6_addr addr;
6248 char str[IPV6_MAX_STRLEN];
6249 struct ctl_table lctl = *ctl;
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01006250 struct net *net = ctl->extra2;
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01006251 struct ipv6_stable_secret *secret = ctl->data;
6252
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01006253 if (&net->ipv6.devconf_all->stable_secret == ctl->data)
6254 return -EIO;
6255
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01006256 lctl.maxlen = IPV6_MAX_STRLEN;
6257 lctl.data = str;
6258
6259 if (!rtnl_trylock())
6260 return restart_syscall();
6261
6262 if (!write && !secret->initialized) {
6263 err = -EIO;
6264 goto out;
6265 }
6266
WANG Cong5449a5c2015-12-21 10:55:45 -08006267 err = snprintf(str, sizeof(str), "%pI6", &secret->secret);
6268 if (err >= sizeof(str)) {
6269 err = -EIO;
6270 goto out;
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01006271 }
6272
6273 err = proc_dostring(&lctl, write, buffer, lenp, ppos);
6274 if (err || !write)
6275 goto out;
6276
6277 if (in6_pton(str, -1, addr.in6_u.u6_addr8, -1, NULL) != 1) {
6278 err = -EIO;
6279 goto out;
6280 }
6281
6282 secret->initialized = true;
6283 secret->secret = addr;
6284
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01006285 if (&net->ipv6.devconf_dflt->stable_secret == ctl->data) {
6286 struct net_device *dev;
6287
6288 for_each_netdev(net, dev) {
6289 struct inet6_dev *idev = __in6_dev_get(dev);
6290
6291 if (idev) {
Felix Jiad35a00b2017-01-26 16:59:17 +13006292 idev->cnf.addr_gen_mode =
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01006293 IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
6294 }
6295 }
6296 } else {
6297 struct inet6_dev *idev = ctl->extra1;
6298
Felix Jiad35a00b2017-01-26 16:59:17 +13006299 idev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01006300 }
6301
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01006302out:
6303 rtnl_unlock();
6304
6305 return err;
6306}
stephen hemmingerc92d5492013-12-17 22:37:14 -08006307
Andy Gospodarek35103d12015-08-13 10:39:01 -04006308static
6309int addrconf_sysctl_ignore_routes_with_linkdown(struct ctl_table *ctl,
6310 int write,
6311 void __user *buffer,
6312 size_t *lenp,
6313 loff_t *ppos)
6314{
6315 int *valp = ctl->data;
6316 int val = *valp;
6317 loff_t pos = *ppos;
6318 struct ctl_table lctl;
6319 int ret;
6320
6321 /* ctl->data points to idev->cnf.ignore_routes_when_linkdown
6322 * we should not modify it until we get the rtnl lock.
6323 */
6324 lctl = *ctl;
6325 lctl.data = &val;
6326
6327 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
6328
6329 if (write)
6330 ret = addrconf_fixup_linkdown(ctl, valp, val);
6331 if (ret)
6332 *ppos = pos;
6333 return ret;
6334}
6335
David Forsterdf789fe2017-02-23 16:27:18 +00006336static
6337void addrconf_set_nopolicy(struct rt6_info *rt, int action)
6338{
6339 if (rt) {
6340 if (action)
6341 rt->dst.flags |= DST_NOPOLICY;
6342 else
6343 rt->dst.flags &= ~DST_NOPOLICY;
6344 }
6345}
6346
6347static
6348void addrconf_disable_policy_idev(struct inet6_dev *idev, int val)
6349{
6350 struct inet6_ifaddr *ifa;
6351
6352 read_lock_bh(&idev->lock);
6353 list_for_each_entry(ifa, &idev->addr_list, if_list) {
6354 spin_lock(&ifa->lock);
6355 if (ifa->rt) {
David Ahernf88d8ea2019-06-03 20:19:52 -07006356 /* host routes only use builtin fib6_nh */
David Ahern1cf844c2019-05-22 20:27:59 -07006357 struct fib6_nh *nh = ifa->rt->fib6_nh;
David Forsterdf789fe2017-02-23 16:27:18 +00006358 int cpu;
6359
Wei Wang66f5d6c2017-10-06 12:06:10 -07006360 rcu_read_lock();
David Ahern3b6761d2018-04-17 17:33:20 -07006361 ifa->rt->dst_nopolicy = val ? true : false;
David Ahernf40b6ae2019-05-22 20:27:55 -07006362 if (nh->rt6i_pcpu) {
David Forsterdf789fe2017-02-23 16:27:18 +00006363 for_each_possible_cpu(cpu) {
6364 struct rt6_info **rtp;
6365
David Ahernf40b6ae2019-05-22 20:27:55 -07006366 rtp = per_cpu_ptr(nh->rt6i_pcpu, cpu);
David Forsterdf789fe2017-02-23 16:27:18 +00006367 addrconf_set_nopolicy(*rtp, val);
6368 }
6369 }
Wei Wang66f5d6c2017-10-06 12:06:10 -07006370 rcu_read_unlock();
David Forsterdf789fe2017-02-23 16:27:18 +00006371 }
6372 spin_unlock(&ifa->lock);
6373 }
6374 read_unlock_bh(&idev->lock);
6375}
6376
6377static
6378int addrconf_disable_policy(struct ctl_table *ctl, int *valp, int val)
6379{
6380 struct inet6_dev *idev;
6381 struct net *net;
6382
6383 if (!rtnl_trylock())
6384 return restart_syscall();
6385
6386 *valp = val;
6387
6388 net = (struct net *)ctl->extra2;
6389 if (valp == &net->ipv6.devconf_dflt->disable_policy) {
6390 rtnl_unlock();
6391 return 0;
6392 }
6393
6394 if (valp == &net->ipv6.devconf_all->disable_policy) {
6395 struct net_device *dev;
6396
6397 for_each_netdev(net, dev) {
6398 idev = __in6_dev_get(dev);
6399 if (idev)
6400 addrconf_disable_policy_idev(idev, val);
6401 }
6402 } else {
6403 idev = (struct inet6_dev *)ctl->extra1;
6404 addrconf_disable_policy_idev(idev, val);
6405 }
6406
6407 rtnl_unlock();
6408 return 0;
6409}
6410
6411static
6412int addrconf_sysctl_disable_policy(struct ctl_table *ctl, int write,
6413 void __user *buffer, size_t *lenp,
6414 loff_t *ppos)
6415{
6416 int *valp = ctl->data;
6417 int val = *valp;
6418 loff_t pos = *ppos;
6419 struct ctl_table lctl;
6420 int ret;
6421
6422 lctl = *ctl;
6423 lctl.data = &val;
6424 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
6425
6426 if (write && (*valp != val))
6427 ret = addrconf_disable_policy(ctl, valp, val);
6428
6429 if (ret)
6430 *ppos = pos;
6431
6432 return ret;
6433}
6434
Maciej Żenczykowskicb4a4c62016-10-07 01:00:49 -07006435static int minus_one = -1;
Maciej Żenczykowskicb9e6842016-09-29 00:33:43 -07006436static const int two_five_five = 255;
6437
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006438static const struct ctl_table addrconf_sysctl[] = {
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006439 {
6440 .procname = "forwarding",
6441 .data = &ipv6_devconf.forwarding,
6442 .maxlen = sizeof(int),
6443 .mode = 0644,
6444 .proc_handler = addrconf_sysctl_forward,
6445 },
6446 {
6447 .procname = "hop_limit",
6448 .data = &ipv6_devconf.hop_limit,
6449 .maxlen = sizeof(int),
6450 .mode = 0644,
Maciej Żenczykowskicb9e6842016-09-29 00:33:43 -07006451 .proc_handler = proc_dointvec_minmax,
Matteo Croceeec48442019-07-18 15:58:50 -07006452 .extra1 = (void *)SYSCTL_ONE,
Maciej Żenczykowskicb9e6842016-09-29 00:33:43 -07006453 .extra2 = (void *)&two_five_five,
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006454 },
6455 {
6456 .procname = "mtu",
6457 .data = &ipv6_devconf.mtu6,
6458 .maxlen = sizeof(int),
6459 .mode = 0644,
6460 .proc_handler = addrconf_sysctl_mtu,
6461 },
6462 {
6463 .procname = "accept_ra",
6464 .data = &ipv6_devconf.accept_ra,
6465 .maxlen = sizeof(int),
6466 .mode = 0644,
6467 .proc_handler = proc_dointvec,
6468 },
6469 {
6470 .procname = "accept_redirects",
6471 .data = &ipv6_devconf.accept_redirects,
6472 .maxlen = sizeof(int),
6473 .mode = 0644,
6474 .proc_handler = proc_dointvec,
6475 },
6476 {
6477 .procname = "autoconf",
6478 .data = &ipv6_devconf.autoconf,
6479 .maxlen = sizeof(int),
6480 .mode = 0644,
6481 .proc_handler = proc_dointvec,
6482 },
6483 {
6484 .procname = "dad_transmits",
6485 .data = &ipv6_devconf.dad_transmits,
6486 .maxlen = sizeof(int),
6487 .mode = 0644,
6488 .proc_handler = proc_dointvec,
6489 },
6490 {
6491 .procname = "router_solicitations",
6492 .data = &ipv6_devconf.rtr_solicits,
6493 .maxlen = sizeof(int),
6494 .mode = 0644,
Maciej Żenczykowskicb4a4c62016-10-07 01:00:49 -07006495 .proc_handler = proc_dointvec_minmax,
6496 .extra1 = &minus_one,
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006497 },
6498 {
6499 .procname = "router_solicitation_interval",
6500 .data = &ipv6_devconf.rtr_solicit_interval,
6501 .maxlen = sizeof(int),
6502 .mode = 0644,
6503 .proc_handler = proc_dointvec_jiffies,
6504 },
6505 {
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07006506 .procname = "router_solicitation_max_interval",
6507 .data = &ipv6_devconf.rtr_solicit_max_interval,
6508 .maxlen = sizeof(int),
6509 .mode = 0644,
6510 .proc_handler = proc_dointvec_jiffies,
6511 },
6512 {
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006513 .procname = "router_solicitation_delay",
6514 .data = &ipv6_devconf.rtr_solicit_delay,
6515 .maxlen = sizeof(int),
6516 .mode = 0644,
6517 .proc_handler = proc_dointvec_jiffies,
6518 },
6519 {
6520 .procname = "force_mld_version",
6521 .data = &ipv6_devconf.force_mld_version,
6522 .maxlen = sizeof(int),
6523 .mode = 0644,
6524 .proc_handler = proc_dointvec,
6525 },
6526 {
6527 .procname = "mldv1_unsolicited_report_interval",
6528 .data =
6529 &ipv6_devconf.mldv1_unsolicited_report_interval,
6530 .maxlen = sizeof(int),
6531 .mode = 0644,
6532 .proc_handler = proc_dointvec_ms_jiffies,
6533 },
6534 {
6535 .procname = "mldv2_unsolicited_report_interval",
6536 .data =
6537 &ipv6_devconf.mldv2_unsolicited_report_interval,
6538 .maxlen = sizeof(int),
6539 .mode = 0644,
6540 .proc_handler = proc_dointvec_ms_jiffies,
6541 },
6542 {
6543 .procname = "use_tempaddr",
6544 .data = &ipv6_devconf.use_tempaddr,
6545 .maxlen = sizeof(int),
6546 .mode = 0644,
6547 .proc_handler = proc_dointvec,
6548 },
6549 {
6550 .procname = "temp_valid_lft",
6551 .data = &ipv6_devconf.temp_valid_lft,
6552 .maxlen = sizeof(int),
6553 .mode = 0644,
6554 .proc_handler = proc_dointvec,
6555 },
6556 {
6557 .procname = "temp_prefered_lft",
6558 .data = &ipv6_devconf.temp_prefered_lft,
6559 .maxlen = sizeof(int),
6560 .mode = 0644,
6561 .proc_handler = proc_dointvec,
6562 },
6563 {
6564 .procname = "regen_max_retry",
6565 .data = &ipv6_devconf.regen_max_retry,
6566 .maxlen = sizeof(int),
6567 .mode = 0644,
6568 .proc_handler = proc_dointvec,
6569 },
6570 {
6571 .procname = "max_desync_factor",
6572 .data = &ipv6_devconf.max_desync_factor,
6573 .maxlen = sizeof(int),
6574 .mode = 0644,
6575 .proc_handler = proc_dointvec,
6576 },
6577 {
6578 .procname = "max_addresses",
6579 .data = &ipv6_devconf.max_addresses,
6580 .maxlen = sizeof(int),
6581 .mode = 0644,
6582 .proc_handler = proc_dointvec,
6583 },
6584 {
6585 .procname = "accept_ra_defrtr",
6586 .data = &ipv6_devconf.accept_ra_defrtr,
6587 .maxlen = sizeof(int),
6588 .mode = 0644,
6589 .proc_handler = proc_dointvec,
6590 },
6591 {
6592 .procname = "accept_ra_min_hop_limit",
6593 .data = &ipv6_devconf.accept_ra_min_hop_limit,
6594 .maxlen = sizeof(int),
6595 .mode = 0644,
6596 .proc_handler = proc_dointvec,
6597 },
6598 {
6599 .procname = "accept_ra_pinfo",
6600 .data = &ipv6_devconf.accept_ra_pinfo,
6601 .maxlen = sizeof(int),
6602 .mode = 0644,
6603 .proc_handler = proc_dointvec,
6604 },
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08006605#ifdef CONFIG_IPV6_ROUTER_PREF
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006606 {
6607 .procname = "accept_ra_rtr_pref",
6608 .data = &ipv6_devconf.accept_ra_rtr_pref,
6609 .maxlen = sizeof(int),
6610 .mode = 0644,
6611 .proc_handler = proc_dointvec,
6612 },
6613 {
6614 .procname = "router_probe_interval",
6615 .data = &ipv6_devconf.rtr_probe_interval,
6616 .maxlen = sizeof(int),
6617 .mode = 0644,
6618 .proc_handler = proc_dointvec_jiffies,
6619 },
Neil Hormanfa03ef32007-01-30 14:30:10 -08006620#ifdef CONFIG_IPV6_ROUTE_INFO
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006621 {
Joel Scherpelzbbea1242017-03-22 18:19:04 +09006622 .procname = "accept_ra_rt_info_min_plen",
6623 .data = &ipv6_devconf.accept_ra_rt_info_min_plen,
6624 .maxlen = sizeof(int),
6625 .mode = 0644,
6626 .proc_handler = proc_dointvec,
6627 },
6628 {
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006629 .procname = "accept_ra_rt_info_max_plen",
6630 .data = &ipv6_devconf.accept_ra_rt_info_max_plen,
6631 .maxlen = sizeof(int),
6632 .mode = 0644,
6633 .proc_handler = proc_dointvec,
6634 },
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -08006635#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08006636#endif
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006637 {
6638 .procname = "proxy_ndp",
6639 .data = &ipv6_devconf.proxy_ndp,
6640 .maxlen = sizeof(int),
6641 .mode = 0644,
6642 .proc_handler = addrconf_sysctl_proxy_ndp,
6643 },
6644 {
6645 .procname = "accept_source_route",
6646 .data = &ipv6_devconf.accept_source_route,
6647 .maxlen = sizeof(int),
6648 .mode = 0644,
6649 .proc_handler = proc_dointvec,
6650 },
Neil Horman95c385b2007-04-25 17:08:10 -07006651#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006652 {
6653 .procname = "optimistic_dad",
6654 .data = &ipv6_devconf.optimistic_dad,
6655 .maxlen = sizeof(int),
6656 .mode = 0644,
6657 .proc_handler = proc_dointvec,
6658 },
6659 {
6660 .procname = "use_optimistic",
6661 .data = &ipv6_devconf.use_optimistic,
6662 .maxlen = sizeof(int),
6663 .mode = 0644,
6664 .proc_handler = proc_dointvec,
6665 },
Neil Horman95c385b2007-04-25 17:08:10 -07006666#endif
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09006667#ifdef CONFIG_IPV6_MROUTE
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006668 {
6669 .procname = "mc_forwarding",
6670 .data = &ipv6_devconf.mc_forwarding,
6671 .maxlen = sizeof(int),
6672 .mode = 0444,
6673 .proc_handler = proc_dointvec,
6674 },
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09006675#endif
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006676 {
6677 .procname = "disable_ipv6",
6678 .data = &ipv6_devconf.disable_ipv6,
6679 .maxlen = sizeof(int),
6680 .mode = 0644,
6681 .proc_handler = addrconf_sysctl_disable,
6682 },
6683 {
6684 .procname = "accept_dad",
6685 .data = &ipv6_devconf.accept_dad,
6686 .maxlen = sizeof(int),
6687 .mode = 0644,
6688 .proc_handler = proc_dointvec,
6689 },
6690 {
6691 .procname = "force_tllao",
6692 .data = &ipv6_devconf.force_tllao,
6693 .maxlen = sizeof(int),
6694 .mode = 0644,
6695 .proc_handler = proc_dointvec
6696 },
6697 {
6698 .procname = "ndisc_notify",
6699 .data = &ipv6_devconf.ndisc_notify,
6700 .maxlen = sizeof(int),
6701 .mode = 0644,
6702 .proc_handler = proc_dointvec
6703 },
6704 {
6705 .procname = "suppress_frag_ndisc",
6706 .data = &ipv6_devconf.suppress_frag_ndisc,
6707 .maxlen = sizeof(int),
6708 .mode = 0644,
6709 .proc_handler = proc_dointvec
6710 },
6711 {
6712 .procname = "accept_ra_from_local",
6713 .data = &ipv6_devconf.accept_ra_from_local,
6714 .maxlen = sizeof(int),
6715 .mode = 0644,
6716 .proc_handler = proc_dointvec,
6717 },
6718 {
6719 .procname = "accept_ra_mtu",
6720 .data = &ipv6_devconf.accept_ra_mtu,
6721 .maxlen = sizeof(int),
6722 .mode = 0644,
6723 .proc_handler = proc_dointvec,
6724 },
6725 {
6726 .procname = "stable_secret",
6727 .data = &ipv6_devconf.stable_secret,
6728 .maxlen = IPV6_MAX_STRLEN,
6729 .mode = 0600,
6730 .proc_handler = addrconf_sysctl_stable_secret,
6731 },
6732 {
6733 .procname = "use_oif_addrs_only",
6734 .data = &ipv6_devconf.use_oif_addrs_only,
6735 .maxlen = sizeof(int),
6736 .mode = 0644,
6737 .proc_handler = proc_dointvec,
6738 },
6739 {
6740 .procname = "ignore_routes_with_linkdown",
6741 .data = &ipv6_devconf.ignore_routes_with_linkdown,
6742 .maxlen = sizeof(int),
6743 .mode = 0644,
6744 .proc_handler = addrconf_sysctl_ignore_routes_with_linkdown,
6745 },
6746 {
6747 .procname = "drop_unicast_in_l2_multicast",
6748 .data = &ipv6_devconf.drop_unicast_in_l2_multicast,
6749 .maxlen = sizeof(int),
6750 .mode = 0644,
6751 .proc_handler = proc_dointvec,
6752 },
6753 {
6754 .procname = "drop_unsolicited_na",
6755 .data = &ipv6_devconf.drop_unsolicited_na,
6756 .maxlen = sizeof(int),
6757 .mode = 0644,
6758 .proc_handler = proc_dointvec,
6759 },
6760 {
6761 .procname = "keep_addr_on_down",
6762 .data = &ipv6_devconf.keep_addr_on_down,
6763 .maxlen = sizeof(int),
6764 .mode = 0644,
6765 .proc_handler = proc_dointvec,
David Ahernf1705ec2016-02-24 09:25:37 -08006766
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006767 },
6768 {
David Lebrun1ababeb2016-11-08 14:57:39 +01006769 .procname = "seg6_enabled",
6770 .data = &ipv6_devconf.seg6_enabled,
6771 .maxlen = sizeof(int),
6772 .mode = 0644,
6773 .proc_handler = proc_dointvec,
6774 },
David Lebrunbf355b82016-11-08 14:57:42 +01006775#ifdef CONFIG_IPV6_SEG6_HMAC
6776 {
6777 .procname = "seg6_require_hmac",
6778 .data = &ipv6_devconf.seg6_require_hmac,
6779 .maxlen = sizeof(int),
6780 .mode = 0644,
6781 .proc_handler = proc_dointvec,
6782 },
6783#endif
David Lebrun1ababeb2016-11-08 14:57:39 +01006784 {
Erik Nordmarkadc176c2016-12-02 14:00:08 -08006785 .procname = "enhanced_dad",
6786 .data = &ipv6_devconf.enhanced_dad,
6787 .maxlen = sizeof(int),
6788 .mode = 0644,
6789 .proc_handler = proc_dointvec,
6790 },
6791 {
Felix Jiad35a00b2017-01-26 16:59:17 +13006792 .procname = "addr_gen_mode",
6793 .data = &ipv6_devconf.addr_gen_mode,
6794 .maxlen = sizeof(int),
6795 .mode = 0644,
6796 .proc_handler = addrconf_sysctl_addr_gen_mode,
6797 },
6798 {
David Forsterdf789fe2017-02-23 16:27:18 +00006799 .procname = "disable_policy",
6800 .data = &ipv6_devconf.disable_policy,
6801 .maxlen = sizeof(int),
6802 .mode = 0644,
6803 .proc_handler = addrconf_sysctl_disable_policy,
6804 },
6805 {
Maciej Żenczykowski2210d6b2017-11-07 21:52:09 -08006806 .procname = "ndisc_tclass",
6807 .data = &ipv6_devconf.ndisc_tclass,
6808 .maxlen = sizeof(int),
6809 .mode = 0644,
6810 .proc_handler = proc_dointvec_minmax,
Matteo Croceeec48442019-07-18 15:58:50 -07006811 .extra1 = (void *)SYSCTL_ZERO,
Maciej Żenczykowski2210d6b2017-11-07 21:52:09 -08006812 .extra2 = (void *)&two_five_five,
6813 },
6814 {
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006815 /* sentinel */
6816 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006817};
6818
Pavel Emelyanovbff16c22008-01-10 17:42:13 -08006819static int __addrconf_sysctl_register(struct net *net, char *dev_name,
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08006820 struct inet6_dev *idev, struct ipv6_devconf *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006821{
Nicolas Dichtel29c994e2016-08-30 10:09:22 +02006822 int i, ifindex;
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006823 struct ctl_table *table;
Eric W. Biederman6105e292012-04-19 13:41:24 +00006824 char path[sizeof("net/ipv6/conf/") + IFNAMSIZ];
Pavel Emelyanov1dab6222007-12-02 00:59:38 +11006825
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006826 table = kmemdup(addrconf_sysctl, sizeof(addrconf_sysctl), GFP_KERNEL);
6827 if (!table)
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11006828 goto out;
6829
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006830 for (i = 0; table[i].data; i++) {
6831 table[i].data += (char *)p - (char *)&ipv6_devconf;
Maciej Żenczykowskicb9e6842016-09-29 00:33:43 -07006832 /* If one of these is already set, then it is not safe to
6833 * overwrite either of them: this makes proc_dointvec_minmax
6834 * usable.
6835 */
6836 if (!table[i].extra1 && !table[i].extra2) {
6837 table[i].extra1 = idev; /* embedded; no ref */
6838 table[i].extra2 = net;
6839 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006840 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006841
Eric W. Biederman6105e292012-04-19 13:41:24 +00006842 snprintf(path, sizeof(path), "net/ipv6/conf/%s", dev_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006843
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006844 p->sysctl_header = register_net_sysctl(net, path, table);
6845 if (!p->sysctl_header)
Eric W. Biederman6105e292012-04-19 13:41:24 +00006846 goto free;
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11006847
Nicolas Dichtel29c994e2016-08-30 10:09:22 +02006848 if (!strcmp(dev_name, "all"))
6849 ifindex = NETCONFA_IFINDEX_ALL;
6850 else if (!strcmp(dev_name, "default"))
6851 ifindex = NETCONFA_IFINDEX_DEFAULT;
6852 else
6853 ifindex = idev->dev->ifindex;
David Ahern85b3daa2017-03-28 14:28:04 -07006854 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF, NETCONFA_ALL,
6855 ifindex, p);
Pavel Emelyanov95897312008-01-10 17:41:45 -08006856 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006857
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11006858free:
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006859 kfree(table);
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11006860out:
Pavel Emelyanov95897312008-01-10 17:41:45 -08006861 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006862}
6863
David Ahern23452172017-03-28 14:28:05 -07006864static void __addrconf_sysctl_unregister(struct net *net,
6865 struct ipv6_devconf *p, int ifindex)
Pavel Emelyanov408c4762008-01-10 17:41:21 -08006866{
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006867 struct ctl_table *table;
Pavel Emelyanov408c4762008-01-10 17:41:21 -08006868
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006869 if (!p->sysctl_header)
Pavel Emelyanov408c4762008-01-10 17:41:21 -08006870 return;
6871
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006872 table = p->sysctl_header->ctl_table_arg;
6873 unregister_net_sysctl_table(p->sysctl_header);
6874 p->sysctl_header = NULL;
6875 kfree(table);
David Ahern23452172017-03-28 14:28:05 -07006876
6877 inet6_netconf_notify_devconf(net, RTM_DELNETCONF, 0, ifindex, NULL);
Pavel Emelyanov408c4762008-01-10 17:41:21 -08006878}
6879
WANG Conga317a2f2014-07-25 15:25:09 -07006880static int addrconf_sysctl_register(struct inet6_dev *idev)
Pavel Emelyanovf52295a2007-12-02 00:58:37 +11006881{
WANG Conga317a2f2014-07-25 15:25:09 -07006882 int err;
6883
6884 if (!sysctl_dev_name_is_allowed(idev->dev->name))
6885 return -EINVAL;
6886
6887 err = neigh_sysctl_register(idev->dev, idev->nd_parms,
6888 &ndisc_ifinfo_sysctl_change);
6889 if (err)
6890 return err;
6891 err = __addrconf_sysctl_register(dev_net(idev->dev), idev->dev->name,
6892 idev, &idev->cnf);
6893 if (err)
6894 neigh_sysctl_unregister(idev->nd_parms);
6895
6896 return err;
Pavel Emelyanovf52295a2007-12-02 00:58:37 +11006897}
6898
Pavel Emelyanov408c4762008-01-10 17:41:21 -08006899static void addrconf_sysctl_unregister(struct inet6_dev *idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006900{
David Ahern23452172017-03-28 14:28:05 -07006901 __addrconf_sysctl_unregister(dev_net(idev->dev), &idev->cnf,
6902 idev->dev->ifindex);
Pavel Emelyanov408c4762008-01-10 17:41:21 -08006903 neigh_sysctl_unregister(idev->nd_parms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006904}
6905
6906
6907#endif
6908
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00006909static int __net_init addrconf_init_net(struct net *net)
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006910{
Hong Zhiguoa79ca222013-03-26 01:52:45 +08006911 int err = -ENOMEM;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006912 struct ipv6_devconf *all, *dflt;
6913
Hong Zhiguoa79ca222013-03-26 01:52:45 +08006914 all = kmemdup(&ipv6_devconf, sizeof(ipv6_devconf), GFP_KERNEL);
Ian Morris63159f22015-03-29 14:00:04 +01006915 if (!all)
Hong Zhiguoa79ca222013-03-26 01:52:45 +08006916 goto err_alloc_all;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006917
Hong Zhiguoa79ca222013-03-26 01:52:45 +08006918 dflt = kmemdup(&ipv6_devconf_dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL);
Ian Morris63159f22015-03-29 14:00:04 +01006919 if (!dflt)
Hong Zhiguoa79ca222013-03-26 01:52:45 +08006920 goto err_alloc_dflt;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006921
Arnd Bergmanna154d5d2019-03-04 21:38:03 +01006922 if (IS_ENABLED(CONFIG_SYSCTL) &&
6923 sysctl_devconf_inherit_init_net == 1 && !net_eq(net, &init_net)) {
Cong Wang856c3952019-01-17 23:27:11 -08006924 memcpy(all, init_net.ipv6.devconf_all, sizeof(ipv6_devconf));
6925 memcpy(dflt, init_net.ipv6.devconf_dflt, sizeof(ipv6_devconf_dflt));
6926 }
6927
Hong Zhiguoa79ca222013-03-26 01:52:45 +08006928 /* these will be inherited by all namespaces */
6929 dflt->autoconf = ipv6_defaults.autoconf;
6930 dflt->disable_ipv6 = ipv6_defaults.disable_ipv6;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006931
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01006932 dflt->stable_secret.initialized = false;
6933 all->stable_secret.initialized = false;
6934
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006935 net->ipv6.devconf_all = all;
6936 net->ipv6.devconf_dflt = dflt;
6937
6938#ifdef CONFIG_SYSCTL
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08006939 err = __addrconf_sysctl_register(net, "all", NULL, all);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006940 if (err < 0)
6941 goto err_reg_all;
6942
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08006943 err = __addrconf_sysctl_register(net, "default", NULL, dflt);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006944 if (err < 0)
6945 goto err_reg_dflt;
6946#endif
6947 return 0;
6948
6949#ifdef CONFIG_SYSCTL
6950err_reg_dflt:
David Ahern23452172017-03-28 14:28:05 -07006951 __addrconf_sysctl_unregister(net, all, NETCONFA_IFINDEX_ALL);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006952err_reg_all:
6953 kfree(dflt);
6954#endif
6955err_alloc_dflt:
6956 kfree(all);
6957err_alloc_all:
6958 return err;
6959}
6960
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00006961static void __net_exit addrconf_exit_net(struct net *net)
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006962{
6963#ifdef CONFIG_SYSCTL
David Ahern23452172017-03-28 14:28:05 -07006964 __addrconf_sysctl_unregister(net, net->ipv6.devconf_dflt,
6965 NETCONFA_IFINDEX_DEFAULT);
6966 __addrconf_sysctl_unregister(net, net->ipv6.devconf_all,
6967 NETCONFA_IFINDEX_ALL);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006968#endif
zhuyj73cf0e922014-11-26 10:25:58 +08006969 kfree(net->ipv6.devconf_dflt);
6970 kfree(net->ipv6.devconf_all);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006971}
6972
6973static struct pernet_operations addrconf_ops = {
6974 .init = addrconf_init_net,
6975 .exit = addrconf_exit_net,
6976};
6977
Daniel Borkmann207895fd32015-01-29 12:15:03 +01006978static struct rtnl_af_ops inet6_ops __read_mostly = {
Thomas Grafb382b192010-11-16 04:33:57 +00006979 .family = AF_INET6,
6980 .fill_link_af = inet6_fill_link_af,
6981 .get_link_af_size = inet6_get_link_af_size,
Daniel Borkmann11b1f822015-02-05 14:39:11 +01006982 .validate_link_af = inet6_validate_link_af,
Daniel Borkmannf53adae2013-04-08 04:01:30 +00006983 .set_link_af = inet6_set_link_af,
Thomas Grafb382b192010-11-16 04:33:57 +00006984};
6985
Linus Torvalds1da177e2005-04-16 15:20:36 -07006986/*
6987 * Init / cleanup code
6988 */
6989
6990int __init addrconf_init(void)
6991{
WANG Conga317a2f2014-07-25 15:25:09 -07006992 struct inet6_dev *idev;
stephen hemmingerc2e21292010-03-17 20:31:10 +00006993 int i, err;
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +09006994
Stephen Hemmingere21e8462010-03-20 16:09:01 -07006995 err = ipv6_addr_label_init();
6996 if (err < 0) {
Joe Perchesf3213832012-05-15 14:11:53 +00006997 pr_crit("%s: cannot initialize default policy table: %d\n",
6998 __func__, err);
Neil Horman2cc6d2b2010-09-24 09:55:52 +00006999 goto out;
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +09007000 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007001
Neil Horman2cc6d2b2010-09-24 09:55:52 +00007002 err = register_pernet_subsys(&addrconf_ops);
7003 if (err < 0)
7004 goto out_addrlabel;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08007005
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01007006 addrconf_wq = create_workqueue("ipv6_addrconf");
7007 if (!addrconf_wq) {
7008 err = -ENOMEM;
7009 goto out_nowq;
7010 }
7011
Linus Torvalds1da177e2005-04-16 15:20:36 -07007012 /* The addrconf netdev notifier requires that loopback_dev
7013 * has it's ipv6 private information allocated and setup
7014 * before it can bring up and give link-local addresses
7015 * to other devices which are up.
7016 *
7017 * Unfortunately, loopback_dev is not necessarily the first
7018 * entry in the global dev_base list of net devices. In fact,
7019 * it is likely to be the very last entry on that list.
7020 * So this causes the notifier registry below to try and
7021 * give link-local addresses to all devices besides loopback_dev
7022 * first, then loopback_dev, which cases all the non-loopback_dev
7023 * devices to fail to get a link-local address.
7024 *
7025 * So, as a temporary fix, allocate the ipv6 structure for
7026 * loopback_dev first by hand.
7027 * Longer term, all of the dependencies ipv6 has upon the loopback
7028 * device and it being up should be removed.
7029 */
7030 rtnl_lock();
WANG Conga317a2f2014-07-25 15:25:09 -07007031 idev = ipv6_add_dev(init_net.loopback_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007032 rtnl_unlock();
WANG Conga317a2f2014-07-25 15:25:09 -07007033 if (IS_ERR(idev)) {
7034 err = PTR_ERR(idev);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08007035 goto errlo;
WANG Conga317a2f2014-07-25 15:25:09 -07007036 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007037
WANG Cong2f460932017-05-03 22:07:31 -07007038 ip6_route_init_special_entries();
7039
stephen hemmingerc2e21292010-03-17 20:31:10 +00007040 for (i = 0; i < IN6_ADDR_HSIZE; i++)
7041 INIT_HLIST_HEAD(&inet6_addr_lst[i]);
7042
Linus Torvalds1da177e2005-04-16 15:20:36 -07007043 register_netdevice_notifier(&ipv6_dev_notf);
7044
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01007045 addrconf_verify();
Thomas Grafc127ea22007-03-22 11:58:32 -07007046
stephen hemminger3678a9d2013-12-30 10:41:32 -08007047 rtnl_af_register(&inet6_ops);
Thomas Grafb382b192010-11-16 04:33:57 +00007048
Florian Westphal16feebc2017-12-02 21:44:08 +01007049 err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETLINK,
7050 NULL, inet6_dump_ifinfo, 0);
Thomas Grafc127ea22007-03-22 11:58:32 -07007051 if (err < 0)
7052 goto errout;
7053
Florian Westphal16feebc2017-12-02 21:44:08 +01007054 err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_NEWADDR,
7055 inet6_rtm_newaddr, NULL, 0);
7056 if (err < 0)
7057 goto errout;
7058 err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_DELADDR,
7059 inet6_rtm_deladdr, NULL, 0);
7060 if (err < 0)
7061 goto errout;
7062 err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETADDR,
7063 inet6_rtm_getaddr, inet6_dump_ifaddr,
7064 RTNL_FLAG_DOIT_UNLOCKED);
7065 if (err < 0)
7066 goto errout;
7067 err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETMULTICAST,
7068 NULL, inet6_dump_ifmcaddr, 0);
7069 if (err < 0)
7070 goto errout;
7071 err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETANYCAST,
7072 NULL, inet6_dump_ifacaddr, 0);
7073 if (err < 0)
7074 goto errout;
7075 err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETNETCONF,
7076 inet6_netconf_get_devconf,
7077 inet6_netconf_dump_devconf,
7078 RTNL_FLAG_DOIT_UNLOCKED);
7079 if (err < 0)
7080 goto errout;
Florian Westphala3fde2a2017-12-04 19:19:18 +01007081 err = ipv6_addr_label_rtnl_register();
7082 if (err < 0)
7083 goto errout;
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +09007084
Linus Torvalds1da177e2005-04-16 15:20:36 -07007085 return 0;
Thomas Grafc127ea22007-03-22 11:58:32 -07007086errout:
Florian Westphal16feebc2017-12-02 21:44:08 +01007087 rtnl_unregister_all(PF_INET6);
Thomas Grafb382b192010-11-16 04:33:57 +00007088 rtnl_af_unregister(&inet6_ops);
Thomas Grafc127ea22007-03-22 11:58:32 -07007089 unregister_netdevice_notifier(&ipv6_dev_notf);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08007090errlo:
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01007091 destroy_workqueue(addrconf_wq);
7092out_nowq:
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08007093 unregister_pernet_subsys(&addrconf_ops);
Neil Horman2cc6d2b2010-09-24 09:55:52 +00007094out_addrlabel:
7095 ipv6_addr_label_cleanup();
7096out:
Thomas Grafc127ea22007-03-22 11:58:32 -07007097 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007098}
7099
Daniel Lezcano09f77092007-12-13 05:34:58 -08007100void addrconf_cleanup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007101{
Daniel Lezcano176c39a2009-03-03 01:06:45 -08007102 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007103 int i;
7104
7105 unregister_netdevice_notifier(&ipv6_dev_notf);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08007106 unregister_pernet_subsys(&addrconf_ops);
Neil Horman2cc6d2b2010-09-24 09:55:52 +00007107 ipv6_addr_label_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007108
Florian Westphal5c451212017-10-04 15:58:49 +02007109 rtnl_af_unregister(&inet6_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007110
Florian Westphal5c451212017-10-04 15:58:49 +02007111 rtnl_lock();
Thomas Grafb382b192010-11-16 04:33:57 +00007112
Daniel Lezcano176c39a2009-03-03 01:06:45 -08007113 /* clean dev list */
7114 for_each_netdev(&init_net, dev) {
7115 if (__in6_dev_get(dev) == NULL)
7116 continue;
7117 addrconf_ifdown(dev, 1);
7118 }
7119 addrconf_ifdown(init_net.loopback_dev, 2);
7120
Linus Torvalds1da177e2005-04-16 15:20:36 -07007121 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07007122 * Check hash table.
7123 */
stephen hemminger5c578aed2010-03-17 20:31:11 +00007124 spin_lock_bh(&addrconf_hash_lock);
stephen hemmingerc2e21292010-03-17 20:31:10 +00007125 for (i = 0; i < IN6_ADDR_HSIZE; i++)
7126 WARN_ON(!hlist_empty(&inet6_addr_lst[i]));
stephen hemminger5c578aed2010-03-17 20:31:11 +00007127 spin_unlock_bh(&addrconf_hash_lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01007128 cancel_delayed_work(&addr_chk_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007129 rtnl_unlock();
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01007130
7131 destroy_workqueue(addrconf_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007132}