blob: 46d614b611db260d6566c7d6b4d45111a6a027ec [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
Hangbin Liud0098e42020-03-03 14:37:35 +08001229cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long expires,
1230 bool del_rt, bool del_peer)
Thomas Haller5b84efe2014-01-15 15:36:59 +01001231{
David Ahern93c2fb22018-04-18 15:38:59 -07001232 struct fib6_info *f6i;
Thomas Haller5b84efe2014-01-15 15:36:59 +01001233
Hangbin Liud0098e42020-03-03 14:37:35 +08001234 f6i = addrconf_get_prefix_route(del_peer ? &ifp->peer_addr : &ifp->addr,
1235 ifp->prefix_len,
David Ahern2b2450c2019-03-27 20:53:52 -07001236 ifp->idev->dev, 0, RTF_DEFAULT, true);
David Ahern93c2fb22018-04-18 15:38:59 -07001237 if (f6i) {
Thomas Haller5b84efe2014-01-15 15:36:59 +01001238 if (del_rt)
David Ahern93c2fb22018-04-18 15:38:59 -07001239 ip6_del_rt(dev_net(ifp->idev->dev), f6i);
Thomas Haller5b84efe2014-01-15 15:36:59 +01001240 else {
David Ahern93c2fb22018-04-18 15:38:59 -07001241 if (!(f6i->fib6_flags & RTF_EXPIRES))
1242 fib6_set_expires(f6i, expires);
1243 fib6_info_release(f6i);
Thomas Haller5b84efe2014-01-15 15:36:59 +01001244 }
1245 }
1246}
1247
1248
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249/* This function wants to get referenced ifp and releases it before return */
1250
1251static void ipv6_del_addr(struct inet6_ifaddr *ifp)
1252{
Herbert Xu4c5ff6a2010-05-18 15:54:18 -07001253 int state;
Thomas Haller5b84efe2014-01-15 15:36:59 +01001254 enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_NOP;
1255 unsigned long expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01001257 ASSERT_RTNL();
1258
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01001259 spin_lock_bh(&ifp->lock);
Herbert Xu4c5ff6a2010-05-18 15:54:18 -07001260 state = ifp->state;
Herbert Xue9d3e082010-05-18 15:36:06 -07001261 ifp->state = INET6_IFADDR_STATE_DEAD;
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01001262 spin_unlock_bh(&ifp->lock);
Herbert Xu4c5ff6a2010-05-18 15:54:18 -07001263
1264 if (state == INET6_IFADDR_STATE_DEAD)
1265 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
stephen hemminger5c578aed2010-03-17 20:31:11 +00001267 spin_lock_bh(&addrconf_hash_lock);
1268 hlist_del_init_rcu(&ifp->addr_lst);
stephen hemminger5c578aed2010-03-17 20:31:11 +00001269 spin_unlock_bh(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
Thomas Haller5b84efe2014-01-15 15:36:59 +01001271 write_lock_bh(&ifp->idev->lock);
David S. Miller5d9efa72013-10-28 20:07:50 -04001272
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 if (ifp->flags&IFA_F_TEMPORARY) {
stephen hemminger372e6c82010-03-17 20:31:09 +00001274 list_del(&ifp->tmp_list);
1275 if (ifp->ifpub) {
1276 in6_ifa_put(ifp->ifpub);
1277 ifp->ifpub = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 }
stephen hemminger372e6c82010-03-17 20:31:09 +00001279 __in6_ifa_put(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
Thomas Haller5b84efe2014-01-15 15:36:59 +01001282 if (ifp->flags & IFA_F_PERMANENT && !(ifp->flags & IFA_F_NOPREFIXROUTE))
1283 action = check_cleanup_prefix_route(ifp, &expires);
stephen hemminger502a2ff2010-03-17 20:31:13 +00001284
Eric Dumazet8ef802a2017-10-07 19:30:23 -07001285 list_del_rcu(&ifp->if_list);
Thomas Haller5b84efe2014-01-15 15:36:59 +01001286 __in6_ifa_put(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
Thomas Haller5b84efe2014-01-15 15:36:59 +01001288 write_unlock_bh(&ifp->idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01001290 addrconf_del_dad_work(ifp);
Andrey Vaginb2238562008-07-08 15:13:31 -07001291
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 ipv6_ifa_notify(RTM_DELADDR, ifp);
1293
Cong Wangf88c91d2013-04-14 23:18:43 +08001294 inet6addr_notifier_call_chain(NETDEV_DOWN, ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
Thomas Haller5b84efe2014-01-15 15:36:59 +01001296 if (action != CLEANUP_PREFIX_RT_NOP) {
1297 cleanup_prefix_route(ifp, expires,
Hangbin Liud0098e42020-03-03 14:37:35 +08001298 action == CLEANUP_PREFIX_RT_DEL, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 }
1300
Daniel Walterc3968a82011-04-13 21:10:57 +00001301 /* clean up prefsrc entries */
1302 rt6_remove_prefsrc(ifp);
Herbert Xu4c5ff6a2010-05-18 15:54:18 -07001303out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 in6_ifa_put(ifp);
1305}
1306
Eric Dumazetfffcefe2017-11-06 14:13:29 -08001307static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp,
1308 struct inet6_ifaddr *ift,
1309 bool block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310{
1311 struct inet6_dev *idev = ifp->idev;
1312 struct in6_addr addr, *tmpaddr;
David Aherne6464b82018-05-27 08:09:53 -07001313 unsigned long tmp_tstamp, age;
Benoit Boissinoteac55bf2008-04-02 00:01:35 -07001314 unsigned long regen_advance;
David Aherne6464b82018-05-27 08:09:53 -07001315 struct ifa6_config cfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 int ret = 0;
Lorenzo Colitti76f793e2011-07-26 13:50:49 +00001317 unsigned long now = jiffies;
Jiri Bohac76506a92016-10-13 18:52:15 +02001318 long max_desync_factor;
Jiri Bohac7aa8e632016-10-20 12:29:26 +02001319 s32 cnf_temp_preferred_lft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
Jiri Pirko53bd6742013-12-06 09:45:22 +01001321 write_lock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 if (ift) {
1323 spin_lock_bh(&ift->lock);
1324 memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8);
1325 spin_unlock_bh(&ift->lock);
1326 tmpaddr = &addr;
1327 } else {
1328 tmpaddr = NULL;
1329 }
1330retry:
1331 in6_dev_hold(idev);
1332 if (idev->cnf.use_tempaddr <= 0) {
Jiri Pirko53bd6742013-12-06 09:45:22 +01001333 write_unlock_bh(&idev->lock);
Joe Perchesf3213832012-05-15 14:11:53 +00001334 pr_info("%s: use_tempaddr is disabled\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 in6_dev_put(idev);
1336 ret = -1;
1337 goto out;
1338 }
1339 spin_lock_bh(&ifp->lock);
1340 if (ifp->regen_count++ >= idev->cnf.regen_max_retry) {
1341 idev->cnf.use_tempaddr = -1; /*XXX*/
1342 spin_unlock_bh(&ifp->lock);
Jiri Pirko53bd6742013-12-06 09:45:22 +01001343 write_unlock_bh(&idev->lock);
Joe Perchesf3213832012-05-15 14:11:53 +00001344 pr_warn("%s: regeneration time exceeded - disabled temporary address support\n",
1345 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 in6_dev_put(idev);
1347 ret = -1;
1348 goto out;
1349 }
1350 in6_ifa_hold(ifp);
1351 memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
Jiri Bohac9d6280d2016-10-13 18:50:02 +02001352 ipv6_try_regen_rndid(idev, tmpaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 memcpy(&addr.s6_addr[8], idev->rndid, 8);
Lorenzo Colitti76f793e2011-07-26 13:50:49 +00001354 age = (now - ifp->tstamp) / HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
Benoit Boissinoteac55bf2008-04-02 00:01:35 -07001356 regen_advance = idev->cnf.regen_max_retry *
Ian Morris67ba4152014-08-24 21:53:10 +01001357 idev->cnf.dad_transmits *
1358 NEIGH_VAR(idev->nd_parms, RETRANS_TIME) / HZ;
Jiri Bohac76506a92016-10-13 18:52:15 +02001359
1360 /* recalculate max_desync_factor each time and update
1361 * idev->desync_factor if it's larger
1362 */
Jiri Bohac7aa8e632016-10-20 12:29:26 +02001363 cnf_temp_preferred_lft = READ_ONCE(idev->cnf.temp_prefered_lft);
Jiri Bohac76506a92016-10-13 18:52:15 +02001364 max_desync_factor = min_t(__u32,
1365 idev->cnf.max_desync_factor,
Jiri Bohac7aa8e632016-10-20 12:29:26 +02001366 cnf_temp_preferred_lft - regen_advance);
Jiri Bohac76506a92016-10-13 18:52:15 +02001367
1368 if (unlikely(idev->desync_factor > max_desync_factor)) {
1369 if (max_desync_factor > 0) {
1370 get_random_bytes(&idev->desync_factor,
1371 sizeof(idev->desync_factor));
1372 idev->desync_factor %= max_desync_factor;
1373 } else {
1374 idev->desync_factor = 0;
1375 }
1376 }
1377
David Ahern3f2d67b2018-06-11 07:12:12 -07001378 memset(&cfg, 0, sizeof(cfg));
David Aherne6464b82018-05-27 08:09:53 -07001379 cfg.valid_lft = min_t(__u32, ifp->valid_lft,
Jiri Bohac76506a92016-10-13 18:52:15 +02001380 idev->cnf.temp_valid_lft + age);
David Aherne6464b82018-05-27 08:09:53 -07001381 cfg.preferred_lft = cnf_temp_preferred_lft + age - idev->desync_factor;
1382 cfg.preferred_lft = min_t(__u32, ifp->prefered_lft, cfg.preferred_lft);
1383
1384 cfg.plen = ifp->prefix_len;
Jiri Bohac76506a92016-10-13 18:52:15 +02001385 tmp_tstamp = ifp->tstamp;
1386 spin_unlock_bh(&ifp->lock);
1387
Jiri Pirko53bd6742013-12-06 09:45:22 +01001388 write_unlock_bh(&idev->lock);
Neil Horman95c385b2007-04-25 17:08:10 -07001389
Benoit Boissinoteac55bf2008-04-02 00:01:35 -07001390 /* A temporary address is created only if this calculated Preferred
1391 * Lifetime is greater than REGEN_ADVANCE time units. In particular,
1392 * an implementation must not create a temporary address with a zero
1393 * Preferred Lifetime.
Heiner Kallweitecab6702014-03-12 22:13:19 +01001394 * Use age calculation as in addrconf_verify to avoid unnecessary
1395 * temporary addresses being generated.
Benoit Boissinoteac55bf2008-04-02 00:01:35 -07001396 */
Heiner Kallweitecab6702014-03-12 22:13:19 +01001397 age = (now - tmp_tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
David Aherne6464b82018-05-27 08:09:53 -07001398 if (cfg.preferred_lft <= regen_advance + age) {
Benoit Boissinoteac55bf2008-04-02 00:01:35 -07001399 in6_ifa_put(ifp);
1400 in6_dev_put(idev);
1401 ret = -1;
1402 goto out;
1403 }
1404
David Aherne6464b82018-05-27 08:09:53 -07001405 cfg.ifa_flags = IFA_F_TEMPORARY;
Neil Horman95c385b2007-04-25 17:08:10 -07001406 /* set in addrconf_prefix_rcv() */
1407 if (ifp->flags & IFA_F_OPTIMISTIC)
David Aherne6464b82018-05-27 08:09:53 -07001408 cfg.ifa_flags |= IFA_F_OPTIMISTIC;
Neil Horman95c385b2007-04-25 17:08:10 -07001409
David Aherne6464b82018-05-27 08:09:53 -07001410 cfg.pfx = &addr;
1411 cfg.scope = ipv6_addr_scope(cfg.pfx);
1412
1413 ift = ipv6_add_addr(idev, &cfg, block, NULL);
Hannes Frederic Sowa4b08a8f2013-08-16 13:02:27 +02001414 if (IS_ERR(ift)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 in6_ifa_put(ifp);
1416 in6_dev_put(idev);
Joe Perchesf3213832012-05-15 14:11:53 +00001417 pr_info("%s: retry temporary address regeneration\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 tmpaddr = &addr;
Jiri Pirko53bd6742013-12-06 09:45:22 +01001419 write_lock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 goto retry;
1421 }
1422
1423 spin_lock_bh(&ift->lock);
1424 ift->ifpub = ifp;
Lorenzo Colitti76f793e2011-07-26 13:50:49 +00001425 ift->cstamp = now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 ift->tstamp = tmp_tstamp;
1427 spin_unlock_bh(&ift->lock);
1428
David S. Millercf22f9a2012-04-14 21:37:40 -04001429 addrconf_dad_start(ift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 in6_ifa_put(ift);
1431 in6_dev_put(idev);
1432out:
1433 return ret;
1434}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435
1436/*
YOSHIFUJI Hideaki072047e42005-11-08 09:38:30 -08001437 * Choose an appropriate source address (RFC3484)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 */
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001439enum {
1440 IPV6_SADDR_RULE_INIT = 0,
1441 IPV6_SADDR_RULE_LOCAL,
1442 IPV6_SADDR_RULE_SCOPE,
1443 IPV6_SADDR_RULE_PREFERRED,
1444#ifdef CONFIG_IPV6_MIP6
1445 IPV6_SADDR_RULE_HOA,
1446#endif
1447 IPV6_SADDR_RULE_OIF,
1448 IPV6_SADDR_RULE_LABEL,
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001449 IPV6_SADDR_RULE_PRIVACY,
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001450 IPV6_SADDR_RULE_ORCHID,
1451 IPV6_SADDR_RULE_PREFIX,
Erik Kline7fd25612014-10-28 18:11:14 +09001452#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1453 IPV6_SADDR_RULE_NOT_OPTIMISTIC,
1454#endif
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001455 IPV6_SADDR_RULE_MAX
YOSHIFUJI Hideaki072047e42005-11-08 09:38:30 -08001456};
1457
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001458struct ipv6_saddr_score {
1459 int rule;
1460 int addr_type;
1461 struct inet6_ifaddr *ifa;
1462 DECLARE_BITMAP(scorebits, IPV6_SADDR_RULE_MAX);
1463 int scopedist;
1464 int matchlen;
1465};
1466
1467struct ipv6_saddr_dst {
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +09001468 const struct in6_addr *addr;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001469 int ifindex;
1470 int scope;
1471 int label;
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001472 unsigned int prefs;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001473};
YOSHIFUJI Hideaki072047e42005-11-08 09:38:30 -08001474
Dave Jonesb6f99a22007-03-22 12:27:49 -07001475static inline int ipv6_saddr_preferred(int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476{
Ulrich Weber45bb0062010-02-25 23:28:58 +00001477 if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|IPV6_ADDR_LOOPBACK))
YOSHIFUJI Hideaki072047e42005-11-08 09:38:30 -08001478 return 1;
1479 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480}
1481
Matteo Croce35e015e2017-09-12 17:46:37 +02001482static bool ipv6_use_optimistic_addr(struct net *net,
1483 struct inet6_dev *idev)
Erik Kline7fd25612014-10-28 18:11:14 +09001484{
1485#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
Matteo Croce35e015e2017-09-12 17:46:37 +02001486 if (!idev)
1487 return false;
1488 if (!net->ipv6.devconf_all->optimistic_dad && !idev->cnf.optimistic_dad)
1489 return false;
1490 if (!net->ipv6.devconf_all->use_optimistic && !idev->cnf.use_optimistic)
1491 return false;
1492
1493 return true;
Erik Kline7fd25612014-10-28 18:11:14 +09001494#else
1495 return false;
1496#endif
1497}
1498
Sabrina Dubrocaf1c02cf2018-02-28 16:40:08 +01001499static bool ipv6_allow_optimistic_dad(struct net *net,
1500 struct inet6_dev *idev)
1501{
1502#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1503 if (!idev)
1504 return false;
1505 if (!net->ipv6.devconf_all->optimistic_dad && !idev->cnf.optimistic_dad)
1506 return false;
1507
1508 return true;
1509#else
1510 return false;
1511#endif
1512}
1513
Benjamin Thery3de23252008-05-28 14:51:24 +02001514static int ipv6_get_saddr_eval(struct net *net,
1515 struct ipv6_saddr_score *score,
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001516 struct ipv6_saddr_dst *dst,
1517 int i)
1518{
1519 int ret;
1520
1521 if (i <= score->rule) {
1522 switch (i) {
1523 case IPV6_SADDR_RULE_SCOPE:
1524 ret = score->scopedist;
1525 break;
1526 case IPV6_SADDR_RULE_PREFIX:
1527 ret = score->matchlen;
1528 break;
1529 default:
1530 ret = !!test_bit(i, score->scorebits);
1531 }
1532 goto out;
1533 }
1534
1535 switch (i) {
1536 case IPV6_SADDR_RULE_INIT:
1537 /* Rule 0: remember if hiscore is not ready yet */
1538 ret = !!score->ifa;
1539 break;
1540 case IPV6_SADDR_RULE_LOCAL:
1541 /* Rule 1: Prefer same address */
1542 ret = ipv6_addr_equal(&score->ifa->addr, dst->addr);
1543 break;
1544 case IPV6_SADDR_RULE_SCOPE:
1545 /* Rule 2: Prefer appropriate scope
1546 *
1547 * ret
1548 * ^
1549 * -1 | d 15
1550 * ---+--+-+---> scope
1551 * |
1552 * | d is scope of the destination.
1553 * B-d | \
1554 * | \ <- smaller scope is better if
stephen hemmingerdb9c7c32014-01-12 11:26:32 -08001555 * B-15 | \ if scope is enough for destination.
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001556 * | ret = B - scope (-1 <= scope >= d <= 15).
1557 * d-C-1 | /
1558 * |/ <- greater is better
1559 * -C / if scope is not enough for destination.
1560 * /| ret = scope - C (-1 <= d < scope <= 15).
1561 *
1562 * d - C - 1 < B -15 (for all -1 <= d <= 15).
1563 * C > d + 14 - B >= 15 + 14 - B = 29 - B.
1564 * Assume B = 0 and we get C > 29.
1565 */
1566 ret = __ipv6_addr_src_scope(score->addr_type);
1567 if (ret >= dst->scope)
1568 ret = -ret;
1569 else
1570 ret -= 128; /* 30 is enough */
1571 score->scopedist = ret;
1572 break;
1573 case IPV6_SADDR_RULE_PREFERRED:
Erik Kline7fd25612014-10-28 18:11:14 +09001574 {
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001575 /* Rule 3: Avoid deprecated and optimistic addresses */
Erik Kline7fd25612014-10-28 18:11:14 +09001576 u8 avoid = IFA_F_DEPRECATED;
1577
Matteo Croce35e015e2017-09-12 17:46:37 +02001578 if (!ipv6_use_optimistic_addr(net, score->ifa->idev))
Erik Kline7fd25612014-10-28 18:11:14 +09001579 avoid |= IFA_F_OPTIMISTIC;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001580 ret = ipv6_saddr_preferred(score->addr_type) ||
Erik Kline7fd25612014-10-28 18:11:14 +09001581 !(score->ifa->flags & avoid);
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001582 break;
Erik Kline7fd25612014-10-28 18:11:14 +09001583 }
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001584#ifdef CONFIG_IPV6_MIP6
1585 case IPV6_SADDR_RULE_HOA:
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001586 {
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001587 /* Rule 4: Prefer home address */
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001588 int prefhome = !(dst->prefs & IPV6_PREFER_SRC_COA);
1589 ret = !(score->ifa->flags & IFA_F_HOMEADDRESS) ^ prefhome;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001590 break;
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001591 }
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001592#endif
1593 case IPV6_SADDR_RULE_OIF:
1594 /* Rule 5: Prefer outgoing interface */
1595 ret = (!dst->ifindex ||
1596 dst->ifindex == score->ifa->idev->dev->ifindex);
1597 break;
1598 case IPV6_SADDR_RULE_LABEL:
1599 /* Rule 6: Prefer matching label */
Benjamin Thery3de23252008-05-28 14:51:24 +02001600 ret = ipv6_addr_label(net,
1601 &score->ifa->addr, score->addr_type,
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001602 score->ifa->idev->dev->ifindex) == dst->label;
1603 break;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001604 case IPV6_SADDR_RULE_PRIVACY:
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001605 {
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001606 /* Rule 7: Prefer public address
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001607 * Note: prefer temporary address if use_tempaddr >= 2
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001608 */
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001609 int preftmp = dst->prefs & (IPV6_PREFER_SRC_PUBLIC|IPV6_PREFER_SRC_TMP) ?
1610 !!(dst->prefs & IPV6_PREFER_SRC_TMP) :
1611 score->ifa->idev->cnf.use_tempaddr >= 2;
1612 ret = (!(score->ifa->flags & IFA_F_TEMPORARY)) ^ preftmp;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001613 break;
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001614 }
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001615 case IPV6_SADDR_RULE_ORCHID:
1616 /* Rule 8-: Prefer ORCHID vs ORCHID or
1617 * non-ORCHID vs non-ORCHID
1618 */
1619 ret = !(ipv6_addr_orchid(&score->ifa->addr) ^
1620 ipv6_addr_orchid(dst->addr));
1621 break;
1622 case IPV6_SADDR_RULE_PREFIX:
1623 /* Rule 8: Use longest matching prefix */
YOSHIFUJI Hideaki / 吉藤英明91b4b042012-09-10 18:41:07 +00001624 ret = ipv6_addr_diff(&score->ifa->addr, dst->addr);
1625 if (ret > score->ifa->prefix_len)
1626 ret = score->ifa->prefix_len;
1627 score->matchlen = ret;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001628 break;
Erik Kline7fd25612014-10-28 18:11:14 +09001629#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1630 case IPV6_SADDR_RULE_NOT_OPTIMISTIC:
1631 /* Optimistic addresses still have lower precedence than other
1632 * preferred addresses.
1633 */
1634 ret = !(score->ifa->flags & IFA_F_OPTIMISTIC);
1635 break;
1636#endif
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001637 default:
1638 ret = 0;
1639 }
1640
1641 if (ret)
1642 __set_bit(i, score->scorebits);
1643 score->rule = i;
1644out:
1645 return ret;
1646}
1647
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001648static int __ipv6_dev_get_saddr(struct net *net,
1649 struct ipv6_saddr_dst *dst,
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001650 struct inet6_dev *idev,
1651 struct ipv6_saddr_score *scores,
1652 int hiscore_idx)
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001653{
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001654 struct ipv6_saddr_score *score = &scores[1 - hiscore_idx], *hiscore = &scores[hiscore_idx];
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001655
Eric Dumazetf59c0312017-10-07 19:30:27 -07001656 list_for_each_entry_rcu(score->ifa, &idev->addr_list, if_list) {
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001657 int i;
1658
1659 /*
1660 * - Tentative Address (RFC2462 section 5.4)
1661 * - A tentative address is not considered
1662 * "assigned to an interface" in the traditional
1663 * sense, unless it is also flagged as optimistic.
1664 * - Candidate Source Address (section 4)
1665 * - In any case, anycast addresses, multicast
1666 * addresses, and the unspecified address MUST
1667 * NOT be included in a candidate set.
1668 */
1669 if ((score->ifa->flags & IFA_F_TENTATIVE) &&
1670 (!(score->ifa->flags & IFA_F_OPTIMISTIC)))
1671 continue;
1672
1673 score->addr_type = __ipv6_addr_type(&score->ifa->addr);
1674
1675 if (unlikely(score->addr_type == IPV6_ADDR_ANY ||
1676 score->addr_type & IPV6_ADDR_MULTICAST)) {
1677 net_dbg_ratelimited("ADDRCONF: unspecified / multicast address assigned as unicast address on %s",
1678 idev->dev->name);
1679 continue;
1680 }
1681
1682 score->rule = -1;
1683 bitmap_zero(score->scorebits, IPV6_SADDR_RULE_MAX);
1684
1685 for (i = 0; i < IPV6_SADDR_RULE_MAX; i++) {
1686 int minihiscore, miniscore;
1687
1688 minihiscore = ipv6_get_saddr_eval(net, hiscore, dst, i);
1689 miniscore = ipv6_get_saddr_eval(net, score, dst, i);
1690
1691 if (minihiscore > miniscore) {
1692 if (i == IPV6_SADDR_RULE_SCOPE &&
1693 score->scopedist > 0) {
1694 /*
1695 * special case:
1696 * each remaining entry
1697 * has too small (not enough)
1698 * scope, because ifa entries
1699 * are sorted by their scope
1700 * values.
1701 */
1702 goto out;
1703 }
1704 break;
1705 } else if (minihiscore < miniscore) {
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001706 swap(hiscore, score);
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001707 hiscore_idx = 1 - hiscore_idx;
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001708
1709 /* restore our iterator */
1710 score->ifa = hiscore->ifa;
1711
1712 break;
1713 }
1714 }
1715 }
1716out:
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001717 return hiscore_idx;
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001718}
1719
David Ahernafbac6012016-06-16 16:24:26 -07001720static int ipv6_get_saddr_master(struct net *net,
1721 const struct net_device *dst_dev,
1722 const struct net_device *master,
1723 struct ipv6_saddr_dst *dst,
1724 struct ipv6_saddr_score *scores,
1725 int hiscore_idx)
1726{
1727 struct inet6_dev *idev;
1728
1729 idev = __in6_dev_get(dst_dev);
1730 if (idev)
1731 hiscore_idx = __ipv6_dev_get_saddr(net, dst, idev,
1732 scores, hiscore_idx);
1733
1734 idev = __in6_dev_get(master);
1735 if (idev)
1736 hiscore_idx = __ipv6_dev_get_saddr(net, dst, idev,
1737 scores, hiscore_idx);
1738
1739 return hiscore_idx;
1740}
1741
Patrick McHardyb3f644f2012-08-26 19:14:14 +02001742int ipv6_dev_get_saddr(struct net *net, const struct net_device *dst_dev,
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +09001743 const struct in6_addr *daddr, unsigned int prefs,
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001744 struct in6_addr *saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745{
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001746 struct ipv6_saddr_score scores[2], *hiscore;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001747 struct ipv6_saddr_dst dst;
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001748 struct inet6_dev *idev;
YOSHIFUJI Hideaki072047e42005-11-08 09:38:30 -08001749 struct net_device *dev;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001750 int dst_type;
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001751 bool use_oif_addr = false;
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001752 int hiscore_idx = 0;
Eric Dumazetcc429c82017-10-07 19:30:28 -07001753 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001755 dst_type = __ipv6_addr_type(daddr);
1756 dst.addr = daddr;
1757 dst.ifindex = dst_dev ? dst_dev->ifindex : 0;
1758 dst.scope = __ipv6_addr_src_scope(dst_type);
Benjamin Thery3de23252008-05-28 14:51:24 +02001759 dst.label = ipv6_addr_label(net, daddr, dst_type, dst.ifindex);
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001760 dst.prefs = prefs;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001761
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001762 scores[hiscore_idx].rule = -1;
1763 scores[hiscore_idx].ifa = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001765 rcu_read_lock();
YOSHIFUJI Hideaki072047e42005-11-08 09:38:30 -08001766
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001767 /* Candidate Source Address (section 4)
1768 * - multicast and link-local destination address,
1769 * the set of candidate source address MUST only
1770 * include addresses assigned to interfaces
1771 * belonging to the same link as the outgoing
1772 * interface.
1773 * (- For site-local destination addresses, the
1774 * set of candidate source addresses MUST only
1775 * include addresses assigned to interfaces
1776 * belonging to the same site as the outgoing
1777 * interface.)
Erik Kline3985e8a2015-07-22 16:38:25 +09001778 * - "It is RECOMMENDED that the candidate source addresses
1779 * be the set of unicast addresses assigned to the
1780 * interface that will be used to send to the destination
1781 * (the 'outgoing' interface)." (RFC 6724)
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001782 */
1783 if (dst_dev) {
Erik Kline3985e8a2015-07-22 16:38:25 +09001784 idev = __in6_dev_get(dst_dev);
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001785 if ((dst_type & IPV6_ADDR_MULTICAST) ||
Erik Kline3985e8a2015-07-22 16:38:25 +09001786 dst.scope <= IPV6_ADDR_SCOPE_LINKLOCAL ||
1787 (idev && idev->cnf.use_oif_addrs_only)) {
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001788 use_oif_addr = true;
YOSHIFUJI Hideaki072047e42005-11-08 09:38:30 -08001789 }
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001790 }
1791
1792 if (use_oif_addr) {
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001793 if (idev)
YOSHIFUJI Hideakic15df302015-07-16 16:51:30 +09001794 hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx);
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001795 } else {
David Ahernafbac6012016-06-16 16:24:26 -07001796 const struct net_device *master;
1797 int master_idx = 0;
1798
1799 /* if dst_dev exists and is enslaved to an L3 device, then
1800 * prefer addresses from dst_dev and then the master over
1801 * any other enslaved devices in the L3 domain.
1802 */
1803 master = l3mdev_master_dev_rcu(dst_dev);
1804 if (master) {
1805 master_idx = master->ifindex;
1806
1807 hiscore_idx = ipv6_get_saddr_master(net, dst_dev,
1808 master, &dst,
1809 scores, hiscore_idx);
1810
1811 if (scores[hiscore_idx].ifa)
1812 goto out;
1813 }
1814
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001815 for_each_netdev_rcu(net, dev) {
David Ahernafbac6012016-06-16 16:24:26 -07001816 /* only consider addresses on devices in the
1817 * same L3 domain
1818 */
1819 if (l3mdev_master_ifindex_rcu(dev) != master_idx)
1820 continue;
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001821 idev = __in6_dev_get(dev);
1822 if (!idev)
1823 continue;
YOSHIFUJI Hideakic15df302015-07-16 16:51:30 +09001824 hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx);
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001825 }
YOSHIFUJI Hideaki072047e42005-11-08 09:38:30 -08001826 }
David Ahernafbac6012016-06-16 16:24:26 -07001827
1828out:
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001829 hiscore = &scores[hiscore_idx];
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001830 if (!hiscore->ifa)
Eric Dumazetcc429c82017-10-07 19:30:28 -07001831 ret = -EADDRNOTAVAIL;
1832 else
1833 *saddr = hiscore->ifa->addr;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001834
Eric Dumazetcc429c82017-10-07 19:30:28 -07001835 rcu_read_unlock();
1836 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837}
YOSHIFUJI Hideaki5e5f3f02008-03-03 21:44:34 +09001838EXPORT_SYMBOL(ipv6_dev_get_saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839
Amerigo Wang89657792013-06-29 21:30:49 +08001840int __ipv6_get_lladdr(struct inet6_dev *idev, struct in6_addr *addr,
Jiri Pirko479840f2013-12-06 09:45:21 +01001841 u32 banned_flags)
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02001842{
1843 struct inet6_ifaddr *ifp;
1844 int err = -EADDRNOTAVAIL;
1845
Hannes Frederic Sowa602582c2014-01-19 21:58:19 +01001846 list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) {
1847 if (ifp->scope > IFA_LINK)
1848 break;
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02001849 if (ifp->scope == IFA_LINK &&
1850 !(ifp->flags & banned_flags)) {
1851 *addr = ifp->addr;
1852 err = 0;
1853 break;
1854 }
1855 }
1856 return err;
1857}
1858
Neil Horman95c385b2007-04-25 17:08:10 -07001859int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr,
Jiri Pirko479840f2013-12-06 09:45:21 +01001860 u32 banned_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861{
1862 struct inet6_dev *idev;
1863 int err = -EADDRNOTAVAIL;
1864
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001865 rcu_read_lock();
Stephen Hemmingere21e8462010-03-20 16:09:01 -07001866 idev = __in6_dev_get(dev);
1867 if (idev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 read_lock_bh(&idev->lock);
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02001869 err = __ipv6_get_lladdr(idev, addr, banned_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 read_unlock_bh(&idev->lock);
1871 }
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001872 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 return err;
1874}
1875
Eric Dumazetd9bf82c2017-10-07 19:30:24 -07001876static int ipv6_count_addresses(const struct inet6_dev *idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877{
Eric Dumazetd9bf82c2017-10-07 19:30:24 -07001878 const struct inet6_ifaddr *ifp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 int cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
Eric Dumazetd9bf82c2017-10-07 19:30:24 -07001881 rcu_read_lock();
1882 list_for_each_entry_rcu(ifp, &idev->addr_list, if_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 cnt++;
Eric Dumazetd9bf82c2017-10-07 19:30:24 -07001884 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 return cnt;
1886}
1887
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001888int ipv6_chk_addr(struct net *net, const struct in6_addr *addr,
Florian Westphal2a7851b2013-05-17 03:56:10 +00001889 const struct net_device *dev, int strict)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890{
David Ahern232378e2018-03-13 08:29:37 -07001891 return ipv6_chk_addr_and_flags(net, addr, dev, !dev,
1892 strict, IFA_F_TENTATIVE);
Erik Klinec58da4c2015-02-04 20:01:23 +09001893}
1894EXPORT_SYMBOL(ipv6_chk_addr);
1895
David Ahern1893ff22018-03-13 08:29:38 -07001896/* device argument is used to find the L3 domain of interest. If
1897 * skip_dev_check is set, then the ifp device is not checked against
1898 * the passed in dev argument. So the 2 cases for addresses checks are:
1899 * 1. does the address exist in the L3 domain that dev is part of
1900 * (skip_dev_check = true), or
1901 *
1902 * 2. does the address exist on the specific device
1903 * (skip_dev_check = false)
1904 */
Erik Klinec58da4c2015-02-04 20:01:23 +09001905int ipv6_chk_addr_and_flags(struct net *net, const struct in6_addr *addr,
David Ahern232378e2018-03-13 08:29:37 -07001906 const struct net_device *dev, bool skip_dev_check,
1907 int strict, u32 banned_flags)
Erik Klinec58da4c2015-02-04 20:01:23 +09001908{
Eric Dumazet3f27fb22017-10-23 16:17:47 -07001909 unsigned int hash = inet6_addr_hash(net, addr);
David Ahern1893ff22018-03-13 08:29:38 -07001910 const struct net_device *l3mdev;
Stephen Hemmingereedf0422010-05-17 22:27:12 -07001911 struct inet6_ifaddr *ifp;
Erik Klinec58da4c2015-02-04 20:01:23 +09001912 u32 ifp_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913
Eric Dumazet480318a2017-10-23 16:17:48 -07001914 rcu_read_lock();
David Ahern232378e2018-03-13 08:29:37 -07001915
David Ahern1893ff22018-03-13 08:29:38 -07001916 l3mdev = l3mdev_master_dev_rcu(dev);
David Ahern232378e2018-03-13 08:29:37 -07001917 if (skip_dev_check)
1918 dev = NULL;
1919
Sasha Levinb67bfe02013-02-27 17:06:00 -08001920 hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09001921 if (!net_eq(dev_net(ifp->idev->dev), net))
Daniel Lezcanobfeade02008-01-10 22:43:18 -08001922 continue;
David Ahern1893ff22018-03-13 08:29:38 -07001923
1924 if (l3mdev_master_dev_rcu(ifp->idev->dev) != l3mdev)
1925 continue;
1926
Erik Klinec58da4c2015-02-04 20:01:23 +09001927 /* Decouple optimistic from tentative for evaluation here.
1928 * Ban optimistic addresses explicitly, when required.
1929 */
1930 ifp_flags = (ifp->flags&IFA_F_OPTIMISTIC)
1931 ? (ifp->flags&~IFA_F_TENTATIVE)
1932 : ifp->flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 if (ipv6_addr_equal(&ifp->addr, addr) &&
Erik Klinec58da4c2015-02-04 20:01:23 +09001934 !(ifp_flags&banned_flags) &&
Ian Morris63159f22015-03-29 14:00:04 +01001935 (!dev || ifp->idev->dev == dev ||
Stephen Hemmingereedf0422010-05-17 22:27:12 -07001936 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))) {
Eric Dumazet480318a2017-10-23 16:17:48 -07001937 rcu_read_unlock();
Stephen Hemmingereedf0422010-05-17 22:27:12 -07001938 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 }
1940 }
stephen hemminger5c578aed2010-03-17 20:31:11 +00001941
Eric Dumazet480318a2017-10-23 16:17:48 -07001942 rcu_read_unlock();
Stephen Hemmingereedf0422010-05-17 22:27:12 -07001943 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944}
Erik Klinec58da4c2015-02-04 20:01:23 +09001945EXPORT_SYMBOL(ipv6_chk_addr_and_flags);
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +09001946
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947
Catalin\(ux\) M. BOIE7df37ff2013-09-23 23:04:19 +03001948/* Compares an address/prefix_len with addresses on device @dev.
1949 * If one is found it returns true.
1950 */
1951bool ipv6_chk_custom_prefix(const struct in6_addr *addr,
1952 const unsigned int prefix_len, struct net_device *dev)
1953{
Eric Dumazet47e26942017-10-07 19:30:25 -07001954 const struct inet6_ifaddr *ifa;
1955 const struct inet6_dev *idev;
Catalin\(ux\) M. BOIE7df37ff2013-09-23 23:04:19 +03001956 bool ret = false;
1957
1958 rcu_read_lock();
1959 idev = __in6_dev_get(dev);
1960 if (idev) {
Eric Dumazet47e26942017-10-07 19:30:25 -07001961 list_for_each_entry_rcu(ifa, &idev->addr_list, if_list) {
Catalin\(ux\) M. BOIE7df37ff2013-09-23 23:04:19 +03001962 ret = ipv6_prefix_equal(addr, &ifa->addr, prefix_len);
1963 if (ret)
1964 break;
1965 }
Catalin\(ux\) M. BOIE7df37ff2013-09-23 23:04:19 +03001966 }
1967 rcu_read_unlock();
1968
1969 return ret;
1970}
1971EXPORT_SYMBOL(ipv6_chk_custom_prefix);
1972
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001973int ipv6_chk_prefix(const struct in6_addr *addr, struct net_device *dev)
YOSHIFUJI Hideaki52eeeb82008-03-15 22:54:23 -04001974{
Eric Dumazet24ba3332017-10-07 19:30:26 -07001975 const struct inet6_ifaddr *ifa;
1976 const struct inet6_dev *idev;
YOSHIFUJI Hideaki52eeeb82008-03-15 22:54:23 -04001977 int onlink;
1978
1979 onlink = 0;
1980 rcu_read_lock();
1981 idev = __in6_dev_get(dev);
1982 if (idev) {
Eric Dumazet24ba3332017-10-07 19:30:26 -07001983 list_for_each_entry_rcu(ifa, &idev->addr_list, if_list) {
YOSHIFUJI Hideaki52eeeb82008-03-15 22:54:23 -04001984 onlink = ipv6_prefix_equal(addr, &ifa->addr,
1985 ifa->prefix_len);
1986 if (onlink)
1987 break;
1988 }
YOSHIFUJI Hideaki52eeeb82008-03-15 22:54:23 -04001989 }
1990 rcu_read_unlock();
1991 return onlink;
1992}
YOSHIFUJI Hideaki52eeeb82008-03-15 22:54:23 -04001993EXPORT_SYMBOL(ipv6_chk_prefix);
1994
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +09001995struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, const struct in6_addr *addr,
Daniel Lezcano1cab3da2008-01-10 22:44:09 -08001996 struct net_device *dev, int strict)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997{
Eric Dumazet3f27fb22017-10-23 16:17:47 -07001998 unsigned int hash = inet6_addr_hash(net, addr);
David S. Millerb79d1d52010-03-25 21:39:21 -07001999 struct inet6_ifaddr *ifp, *result = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000
Eric Dumazet24f226d2017-10-23 16:17:49 -07002001 rcu_read_lock();
2002 hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09002003 if (!net_eq(dev_net(ifp->idev->dev), net))
Daniel Lezcano1cab3da2008-01-10 22:44:09 -08002004 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 if (ipv6_addr_equal(&ifp->addr, addr)) {
Ian Morris63159f22015-03-29 14:00:04 +01002006 if (!dev || ifp->idev->dev == dev ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
David S. Millerb79d1d52010-03-25 21:39:21 -07002008 result = ifp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 in6_ifa_hold(ifp);
2010 break;
2011 }
2012 }
2013 }
Eric Dumazet24f226d2017-10-23 16:17:49 -07002014 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015
David S. Millerb79d1d52010-03-25 21:39:21 -07002016 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017}
2018
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019/* Gets referenced address, destroys ifaddr */
2020
Brian Haleycc411d02009-09-09 14:41:32 +00002021static void addrconf_dad_stop(struct inet6_ifaddr *ifp, int dad_failed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022{
Lubomir Rintel3d171f32016-01-08 13:47:23 +01002023 if (dad_failed)
2024 ifp->flags |= IFA_F_DADFAILED;
2025
Sabrina Dubrocaec8add22017-06-29 16:56:54 +02002026 if (ifp->flags&IFA_F_TEMPORARY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 struct inet6_ifaddr *ifpub;
2028 spin_lock_bh(&ifp->lock);
2029 ifpub = ifp->ifpub;
2030 if (ifpub) {
2031 in6_ifa_hold(ifpub);
2032 spin_unlock_bh(&ifp->lock);
Eric Dumazetfffcefe2017-11-06 14:13:29 -08002033 ipv6_create_tempaddr(ifpub, ifp, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 in6_ifa_put(ifpub);
2035 } else {
2036 spin_unlock_bh(&ifp->lock);
2037 }
2038 ipv6_del_addr(ifp);
Sabrina Dubrocaec8add22017-06-29 16:56:54 +02002039 } else if (ifp->flags&IFA_F_PERMANENT || !dad_failed) {
2040 spin_lock_bh(&ifp->lock);
2041 addrconf_del_dad_work(ifp);
2042 ifp->flags |= IFA_F_TENTATIVE;
Sabrina Dubrocaf1c02cf2018-02-28 16:40:08 +01002043 if (dad_failed)
2044 ifp->flags &= ~IFA_F_OPTIMISTIC;
Sabrina Dubrocaec8add22017-06-29 16:56:54 +02002045 spin_unlock_bh(&ifp->lock);
2046 if (dad_failed)
2047 ipv6_ifa_notify(0, ifp);
2048 in6_ifa_put(ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01002049 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 ipv6_del_addr(ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01002051 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052}
2053
Herbert Xuf2344a12010-05-18 15:55:27 -07002054static int addrconf_dad_end(struct inet6_ifaddr *ifp)
2055{
2056 int err = -ENOENT;
2057
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01002058 spin_lock_bh(&ifp->lock);
Herbert Xuf2344a12010-05-18 15:55:27 -07002059 if (ifp->state == INET6_IFADDR_STATE_DAD) {
2060 ifp->state = INET6_IFADDR_STATE_POSTDAD;
2061 err = 0;
2062 }
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01002063 spin_unlock_bh(&ifp->lock);
Herbert Xuf2344a12010-05-18 15:55:27 -07002064
2065 return err;
2066}
2067
Vishwanath Paida13c592017-10-30 19:38:52 -04002068void addrconf_dad_failure(struct sk_buff *skb, struct inet6_ifaddr *ifp)
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002069{
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09002070 struct inet6_dev *idev = ifp->idev;
Hannes Frederic Sowa1855b7c2015-03-23 23:36:05 +01002071 struct net *net = dev_net(ifp->idev->dev);
Brian Haley9bdd8d42009-03-18 18:22:48 -07002072
Ursula Braun853dc2e2010-10-24 23:06:43 +00002073 if (addrconf_dad_end(ifp)) {
2074 in6_ifa_put(ifp);
Herbert Xuf2344a12010-05-18 15:55:27 -07002075 return;
Ursula Braun853dc2e2010-10-24 23:06:43 +00002076 }
Herbert Xuf2344a12010-05-18 15:55:27 -07002077
Vishwanath Paida13c592017-10-30 19:38:52 -04002078 net_info_ratelimited("%s: IPv6 duplicate address %pI6c used by %pM detected!\n",
2079 ifp->idev->dev->name, &ifp->addr, eth_hdr(skb)->h_source);
Brian Haley9bdd8d42009-03-18 18:22:48 -07002080
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01002081 spin_lock_bh(&ifp->lock);
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09002082
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01002083 if (ifp->flags & IFA_F_STABLE_PRIVACY) {
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01002084 struct in6_addr new_addr;
2085 struct inet6_ifaddr *ifp2;
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01002086 int retries = ifp->stable_privacy_retry + 1;
David Aherne6464b82018-05-27 08:09:53 -07002087 struct ifa6_config cfg = {
2088 .pfx = &new_addr,
2089 .plen = ifp->prefix_len,
2090 .ifa_flags = ifp->flags,
2091 .valid_lft = ifp->valid_lft,
2092 .preferred_lft = ifp->prefered_lft,
2093 .scope = ifp->scope,
2094 };
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01002095
Hannes Frederic Sowa1855b7c2015-03-23 23:36:05 +01002096 if (retries > net->ipv6.sysctl.idgen_retries) {
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01002097 net_info_ratelimited("%s: privacy stable address generation failed because of DAD conflicts!\n",
2098 ifp->idev->dev->name);
2099 goto errdad;
2100 }
2101
2102 new_addr = ifp->addr;
2103 if (ipv6_generate_stable_address(&new_addr, retries,
2104 idev))
2105 goto errdad;
2106
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01002107 spin_unlock_bh(&ifp->lock);
2108
2109 if (idev->cnf.max_addresses &&
2110 ipv6_count_addresses(idev) >=
2111 idev->cnf.max_addresses)
2112 goto lock_errdad;
2113
2114 net_info_ratelimited("%s: generating new stable privacy address because of DAD conflict\n",
2115 ifp->idev->dev->name);
2116
David Aherne6464b82018-05-27 08:09:53 -07002117 ifp2 = ipv6_add_addr(idev, &cfg, false, NULL);
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01002118 if (IS_ERR(ifp2))
2119 goto lock_errdad;
2120
2121 spin_lock_bh(&ifp2->lock);
2122 ifp2->stable_privacy_retry = retries;
2123 ifp2->state = INET6_IFADDR_STATE_PREDAD;
2124 spin_unlock_bh(&ifp2->lock);
2125
Hannes Frederic Sowa1855b7c2015-03-23 23:36:05 +01002126 addrconf_mod_dad_work(ifp2, net->ipv6.sysctl.idgen_delay);
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01002127 in6_ifa_put(ifp2);
2128lock_errdad:
2129 spin_lock_bh(&ifp->lock);
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09002130 }
2131
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01002132errdad:
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01002133 /* transition from _POSTDAD to _ERRDAD */
2134 ifp->state = INET6_IFADDR_STATE_ERRDAD;
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01002135 spin_unlock_bh(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01002136
2137 addrconf_mod_dad_work(ifp, 0);
Wei Yongjun751eb6b2016-09-05 16:06:31 +08002138 in6_ifa_put(ifp);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002139}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140
Sabrina Dubrocaa9ed4a22014-09-02 10:29:29 +02002141/* Join to solicited addr multicast group.
2142 * caller must hold RTNL */
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002143void addrconf_join_solict(struct net_device *dev, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144{
2145 struct in6_addr maddr;
2146
2147 if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
2148 return;
2149
2150 addrconf_addr_solict_mult(addr, &maddr);
2151 ipv6_dev_mc_inc(dev, &maddr);
2152}
2153
Sabrina Dubrocaa9ed4a22014-09-02 10:29:29 +02002154/* caller must hold RTNL */
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002155void addrconf_leave_solict(struct inet6_dev *idev, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156{
2157 struct in6_addr maddr;
2158
2159 if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP))
2160 return;
2161
2162 addrconf_addr_solict_mult(addr, &maddr);
2163 __ipv6_dev_mc_dec(idev, &maddr);
2164}
2165
Sabrina Dubrocaa9ed4a22014-09-02 10:29:29 +02002166/* caller must hold RTNL */
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03002167static void addrconf_join_anycast(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168{
2169 struct in6_addr addr;
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01002170
Hannes Frederic Sowa88ad3142014-01-06 17:53:14 +01002171 if (ifp->prefix_len >= 127) /* RFC 6164 */
Bjørn Mork2bda8a02011-07-05 23:04:13 +00002172 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
2174 if (ipv6_addr_any(&addr))
2175 return;
WANG Cong013b4d92014-09-11 15:35:11 -07002176 __ipv6_dev_ac_inc(ifp->idev, &addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177}
2178
Sabrina Dubrocaa9ed4a22014-09-02 10:29:29 +02002179/* caller must hold RTNL */
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03002180static void addrconf_leave_anycast(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181{
2182 struct in6_addr addr;
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01002183
Hannes Frederic Sowa88ad3142014-01-06 17:53:14 +01002184 if (ifp->prefix_len >= 127) /* RFC 6164 */
YOSHIFUJI Hideaki32019e62011-07-24 11:44:34 +00002185 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
2187 if (ipv6_addr_any(&addr))
2188 return;
2189 __ipv6_dev_ac_dec(ifp->idev, &addr);
2190}
2191
Alexander Aring8a7a4b42017-03-12 10:19:36 +02002192static int addrconf_ifid_6lowpan(u8 *eui, struct net_device *dev)
alex.bluesman.smirnov@gmail.com06a4c1c2012-05-10 03:25:52 +00002193{
Alexander Aring8a7a4b42017-03-12 10:19:36 +02002194 switch (dev->addr_len) {
2195 case ETH_ALEN:
Luiz Augusto von Dentz9dae2e02017-03-12 10:19:38 +02002196 memcpy(eui, dev->dev_addr, 3);
2197 eui[3] = 0xFF;
2198 eui[4] = 0xFE;
2199 memcpy(eui + 5, dev->dev_addr + 3, 3);
2200 break;
Alexander Aring8a7a4b42017-03-12 10:19:36 +02002201 case EUI64_ADDR_LEN:
2202 memcpy(eui, dev->dev_addr, EUI64_ADDR_LEN);
2203 eui[0] ^= 2;
2204 break;
2205 default:
alex.bluesman.smirnov@gmail.com06a4c1c2012-05-10 03:25:52 +00002206 return -1;
Alexander Aring8a7a4b42017-03-12 10:19:36 +02002207 }
2208
alex.bluesman.smirnov@gmail.com06a4c1c2012-05-10 03:25:52 +00002209 return 0;
2210}
2211
YOSHIFUJI Hideaki / 吉藤英明cb6bf352013-03-25 08:26:24 +00002212static int addrconf_ifid_ieee1394(u8 *eui, struct net_device *dev)
2213{
2214 union fwnet_hwaddr *ha;
2215
2216 if (dev->addr_len != FWNET_ALEN)
2217 return -1;
2218
2219 ha = (union fwnet_hwaddr *)dev->dev_addr;
2220
2221 memcpy(eui, &ha->uc.uniq_id, sizeof(ha->uc.uniq_id));
2222 eui[0] ^= 2;
2223 return 0;
2224}
2225
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08002226static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev)
2227{
2228 /* XXX: inherit EUI-64 from other interface -- yoshfuji */
2229 if (dev->addr_len != ARCNET_ALEN)
2230 return -1;
2231 memset(eui, 0, 7);
Eldad Zack8e5e8f32012-04-01 07:49:08 +00002232 eui[7] = *(u8 *)dev->dev_addr;
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08002233 return 0;
2234}
2235
2236static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev)
2237{
2238 if (dev->addr_len != INFINIBAND_ALEN)
2239 return -1;
2240 memcpy(eui, dev->dev_addr + 12, 8);
2241 eui[0] |= 2;
2242 return 0;
2243}
2244
stephen hemmingerc61393e2010-10-04 20:17:53 +00002245static int __ipv6_isatap_ifid(u8 *eui, __be32 addr)
YOSHIFUJI Hideakidfd982b2008-04-10 15:42:09 +09002246{
Sascha Hlusiak9af28512009-05-19 12:56:51 +00002247 if (addr == 0)
2248 return -1;
YOSHIFUJI Hideakidfd982b2008-04-10 15:42:09 +09002249 eui[0] = (ipv4_is_zeronet(addr) || ipv4_is_private_10(addr) ||
2250 ipv4_is_loopback(addr) || ipv4_is_linklocal_169(addr) ||
2251 ipv4_is_private_172(addr) || ipv4_is_test_192(addr) ||
2252 ipv4_is_anycast_6to4(addr) || ipv4_is_private_192(addr) ||
2253 ipv4_is_test_198(addr) || ipv4_is_multicast(addr) ||
2254 ipv4_is_lbcast(addr)) ? 0x00 : 0x02;
2255 eui[1] = 0;
2256 eui[2] = 0x5E;
2257 eui[3] = 0xFE;
2258 memcpy(eui + 4, &addr, 4);
2259 return 0;
2260}
YOSHIFUJI Hideakidfd982b2008-04-10 15:42:09 +09002261
2262static int addrconf_ifid_sit(u8 *eui, struct net_device *dev)
2263{
2264 if (dev->priv_flags & IFF_ISATAP)
2265 return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
2266 return -1;
2267}
2268
stephen hemmingeraee80b52011-06-08 10:44:30 +00002269static int addrconf_ifid_gre(u8 *eui, struct net_device *dev)
2270{
2271 return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
2272}
2273
Nicolas Dichtele8377352013-08-20 12:16:06 +02002274static int addrconf_ifid_ip6tnl(u8 *eui, struct net_device *dev)
2275{
2276 memcpy(eui, dev->perm_addr, 3);
2277 memcpy(eui + 5, dev->perm_addr + 3, 3);
2278 eui[3] = 0xFF;
2279 eui[4] = 0xFE;
2280 eui[0] ^= 2;
2281 return 0;
2282}
2283
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
2285{
2286 switch (dev->type) {
2287 case ARPHRD_ETHER:
2288 case ARPHRD_FDDI:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08002289 return addrconf_ifid_eui48(eui, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 case ARPHRD_ARCNET:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08002291 return addrconf_ifid_arcnet(eui, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 case ARPHRD_INFINIBAND:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08002293 return addrconf_ifid_infiniband(eui, dev);
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11002294 case ARPHRD_SIT:
YOSHIFUJI Hideakidfd982b2008-04-10 15:42:09 +09002295 return addrconf_ifid_sit(eui, dev);
stephen hemmingeraee80b52011-06-08 10:44:30 +00002296 case ARPHRD_IPGRE:
Felix Jia45ce0fd2017-01-26 16:59:18 +13002297 case ARPHRD_TUNNEL:
stephen hemmingeraee80b52011-06-08 10:44:30 +00002298 return addrconf_ifid_gre(eui, dev);
Jukka Rissanene74bccb82013-12-11 17:05:36 +02002299 case ARPHRD_6LOWPAN:
Alexander Aring8a7a4b42017-03-12 10:19:36 +02002300 return addrconf_ifid_6lowpan(eui, dev);
YOSHIFUJI Hideaki / 吉藤英明cb6bf352013-03-25 08:26:24 +00002301 case ARPHRD_IEEE1394:
2302 return addrconf_ifid_ieee1394(eui, dev);
Nicolas Dichtele8377352013-08-20 12:16:06 +02002303 case ARPHRD_TUNNEL6:
Felix Jia45ce0fd2017-01-26 16:59:18 +13002304 case ARPHRD_IP6GRE:
Subash Abhinov Kasiviswanathan9deb4412018-06-04 19:26:07 -06002305 case ARPHRD_RAWIP:
Nicolas Dichtele8377352013-08-20 12:16:06 +02002306 return addrconf_ifid_ip6tnl(eui, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 }
2308 return -1;
2309}
2310
2311static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
2312{
2313 int err = -1;
2314 struct inet6_ifaddr *ifp;
2315
2316 read_lock_bh(&idev->lock);
Hannes Frederic Sowa602582c2014-01-19 21:58:19 +01002317 list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) {
2318 if (ifp->scope > IFA_LINK)
2319 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
2321 memcpy(eui, ifp->addr.s6_addr+8, 8);
2322 err = 0;
2323 break;
2324 }
2325 }
2326 read_unlock_bh(&idev->lock);
2327 return err;
2328}
2329
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330/* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
Jiri Bohac9d6280d2016-10-13 18:50:02 +02002331static void ipv6_regen_rndid(struct inet6_dev *idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333regen:
YOSHIFUJI Hideaki955189e2006-03-20 16:54:09 -08002334 get_random_bytes(idev->rndid, sizeof(idev->rndid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 idev->rndid[0] &= ~0x02;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336
2337 /*
2338 * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
2339 * check if generated address is not inappropriate
2340 *
2341 * - Reserved subnet anycast (RFC 2526)
2342 * 11111101 11....11 1xxxxxxx
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11002343 * - ISATAP (RFC4214) 6.1
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 * 00-00-5E-FE-xx-xx-xx-xx
2345 * - value 0
2346 * - XXX: already assigned to an address on the device
2347 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002348 if (idev->rndid[0] == 0xfd &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff &&
2350 (idev->rndid[7]&0x80))
2351 goto regen;
2352 if ((idev->rndid[0]|idev->rndid[1]) == 0) {
2353 if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe)
2354 goto regen;
2355 if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00)
2356 goto regen;
2357 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358}
2359
Jiri Bohac9d6280d2016-10-13 18:50:02 +02002360static void ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr)
Eldad Zack8e5e8f32012-04-01 07:49:08 +00002361{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0)
Jiri Bohac9d6280d2016-10-13 18:50:02 +02002363 ipv6_regen_rndid(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365
2366/*
2367 * Add prefix route.
2368 */
2369
2370static void
David Ahern8308f3f2018-05-27 08:09:58 -07002371addrconf_prefix_route(struct in6_addr *pfx, int plen, u32 metric,
2372 struct net_device *dev, unsigned long expires,
2373 u32 flags, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374{
Thomas Graf86872cb2006-08-22 00:01:08 -07002375 struct fib6_config cfg = {
David Ahernca254492015-10-12 11:47:10 -07002376 .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_PREFIX,
David Ahern8308f3f2018-05-27 08:09:58 -07002377 .fc_metric = metric ? : IP6_RT_PRIO_ADDRCONF,
Thomas Graf86872cb2006-08-22 00:01:08 -07002378 .fc_ifindex = dev->ifindex,
2379 .fc_expires = expires,
2380 .fc_dst_len = plen,
2381 .fc_flags = RTF_UP | flags,
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002382 .fc_nlinfo.nl_net = dev_net(dev),
Stephen Hemmingerf410a1f2008-08-23 05:16:46 -07002383 .fc_protocol = RTPROT_KERNEL,
David Aherne8478e82018-04-17 17:33:13 -07002384 .fc_type = RTN_UNICAST,
Thomas Graf86872cb2006-08-22 00:01:08 -07002385 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002387 cfg.fc_dst = *pfx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388
2389 /* Prevent useless cloning on PtP SIT.
2390 This thing is done here expecting that the whole
2391 class of non-broadcast devices need not cloning.
2392 */
Amerigo Wang07a93622012-10-29 16:23:10 +00002393#if IS_ENABLED(CONFIG_IPV6_SIT)
Thomas Graf86872cb2006-08-22 00:01:08 -07002394 if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT))
2395 cfg.fc_flags |= RTF_NONEXTHOP;
Joerg Roedel0be669b2006-10-10 14:49:53 -07002396#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397
David Ahernacb54e32018-04-17 17:33:22 -07002398 ip6_route_add(&cfg, gfp_flags, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399}
2400
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002401
David Ahern8d1c8022018-04-17 17:33:26 -07002402static struct fib6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002403 int plen,
2404 const struct net_device *dev,
David Ahern2b2450c2019-03-27 20:53:52 -07002405 u32 flags, u32 noflags,
2406 bool no_gw)
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002407{
2408 struct fib6_node *fn;
David Ahern8d1c8022018-04-17 17:33:26 -07002409 struct fib6_info *rt = NULL;
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002410 struct fib6_table *table;
David Ahernca254492015-10-12 11:47:10 -07002411 u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_PREFIX;
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002412
David Ahernca254492015-10-12 11:47:10 -07002413 table = fib6_get_table(dev_net(dev), tb_id);
Ian Morris63159f22015-03-29 14:00:04 +01002414 if (!table)
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002415 return NULL;
2416
Wei Wang66f5d6c2017-10-06 12:06:10 -07002417 rcu_read_lock();
Wei Wang38fbeee2017-10-06 12:06:02 -07002418 fn = fib6_locate(&table->tb6_root, pfx, plen, NULL, 0, true);
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002419 if (!fn)
2420 goto out;
Martin KaFai Lau1f56a01f2015-04-28 13:03:03 -07002421
Wei Wang66f5d6c2017-10-06 12:06:10 -07002422 for_each_fib6_node_rt_rcu(fn) {
David Ahernf88d8ea2019-06-03 20:19:52 -07002423 /* prefix routes only use builtin fib6_nh */
2424 if (rt->nh)
2425 continue;
2426
David Ahern1cf844c2019-05-22 20:27:59 -07002427 if (rt->fib6_nh->fib_nh_dev->ifindex != dev->ifindex)
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002428 continue;
David Ahern1cf844c2019-05-22 20:27:59 -07002429 if (no_gw && rt->fib6_nh->fib_nh_gw_family)
David Ahern2b2450c2019-03-27 20:53:52 -07002430 continue;
David Ahern93c2fb22018-04-18 15:38:59 -07002431 if ((rt->fib6_flags & flags) != flags)
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002432 continue;
David Ahern93c2fb22018-04-18 15:38:59 -07002433 if ((rt->fib6_flags & noflags) != 0)
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002434 continue;
Wei Wange873e4b2018-07-21 20:56:32 -07002435 if (!fib6_info_hold_safe(rt))
2436 continue;
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002437 break;
2438 }
2439out:
Wei Wang66f5d6c2017-10-06 12:06:10 -07002440 rcu_read_unlock();
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002441 return rt;
2442}
2443
2444
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445/* Create "default" multicast route to the interface */
2446
2447static void addrconf_add_mroute(struct net_device *dev)
2448{
Thomas Graf86872cb2006-08-22 00:01:08 -07002449 struct fib6_config cfg = {
David Ahernca254492015-10-12 11:47:10 -07002450 .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_LOCAL,
Thomas Graf86872cb2006-08-22 00:01:08 -07002451 .fc_metric = IP6_RT_PRIO_ADDRCONF,
2452 .fc_ifindex = dev->ifindex,
2453 .fc_dst_len = 8,
2454 .fc_flags = RTF_UP,
David Aherne8478e82018-04-17 17:33:13 -07002455 .fc_type = RTN_UNICAST,
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002456 .fc_nlinfo.nl_net = dev_net(dev),
Thomas Graf86872cb2006-08-22 00:01:08 -07002457 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458
Thomas Graf86872cb2006-08-22 00:01:08 -07002459 ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0);
2460
Cong Wange500c6d2018-08-22 12:58:34 -07002461 ip6_route_add(&cfg, GFP_KERNEL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462}
2463
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
2465{
2466 struct inet6_dev *idev;
2467
2468 ASSERT_RTNL();
2469
Stephen Hemmingere21e8462010-03-20 16:09:01 -07002470 idev = ipv6_find_idev(dev);
Sabrina Dubrocadb0b99f2019-08-23 15:44:36 +02002471 if (IS_ERR(idev))
2472 return idev;
Brian Haley64e724f2010-07-20 10:34:30 +00002473
2474 if (idev->cnf.disable_ipv6)
2475 return ERR_PTR(-EACCES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476
2477 /* Add default multicast route */
David Ahernba46ee42016-06-13 13:44:18 -07002478 if (!(dev->flags & IFF_LOOPBACK) && !netif_is_l3_master(dev))
Li Wei4af04ab2011-12-06 21:23:45 +00002479 addrconf_add_mroute(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 return idev;
2482}
2483
Jiri Pirko53bd6742013-12-06 09:45:22 +01002484static void manage_tempaddrs(struct inet6_dev *idev,
2485 struct inet6_ifaddr *ifp,
2486 __u32 valid_lft, __u32 prefered_lft,
2487 bool create, unsigned long now)
2488{
2489 u32 flags;
2490 struct inet6_ifaddr *ift;
2491
2492 read_lock_bh(&idev->lock);
2493 /* update all temporary addresses in the list */
2494 list_for_each_entry(ift, &idev->tempaddr_list, tmp_list) {
2495 int age, max_valid, max_prefered;
2496
2497 if (ifp != ift->ifpub)
2498 continue;
2499
2500 /* RFC 4941 section 3.3:
2501 * If a received option will extend the lifetime of a public
2502 * address, the lifetimes of temporary addresses should
2503 * be extended, subject to the overall constraint that no
2504 * temporary addresses should ever remain "valid" or "preferred"
2505 * for a time longer than (TEMP_VALID_LIFETIME) or
2506 * (TEMP_PREFERRED_LIFETIME - DESYNC_FACTOR), respectively.
2507 */
2508 age = (now - ift->cstamp) / HZ;
2509 max_valid = idev->cnf.temp_valid_lft - age;
2510 if (max_valid < 0)
2511 max_valid = 0;
2512
2513 max_prefered = idev->cnf.temp_prefered_lft -
Jiri Bohac76506a92016-10-13 18:52:15 +02002514 idev->desync_factor - age;
Jiri Pirko53bd6742013-12-06 09:45:22 +01002515 if (max_prefered < 0)
2516 max_prefered = 0;
2517
2518 if (valid_lft > max_valid)
2519 valid_lft = max_valid;
2520
2521 if (prefered_lft > max_prefered)
2522 prefered_lft = max_prefered;
2523
2524 spin_lock(&ift->lock);
2525 flags = ift->flags;
2526 ift->valid_lft = valid_lft;
2527 ift->prefered_lft = prefered_lft;
2528 ift->tstamp = now;
2529 if (prefered_lft > 0)
2530 ift->flags &= ~IFA_F_DEPRECATED;
2531
2532 spin_unlock(&ift->lock);
2533 if (!(flags&IFA_F_TENTATIVE))
2534 ipv6_ifa_notify(0, ift);
2535 }
2536
2537 if ((create || list_empty(&idev->tempaddr_list)) &&
2538 idev->cnf.use_tempaddr > 0) {
2539 /* When a new public address is created as described
2540 * in [ADDRCONF], also create a new temporary address.
2541 * Also create a temporary address if it's enabled but
2542 * no temporary address currently exists.
2543 */
2544 read_unlock_bh(&idev->lock);
Eric Dumazetfffcefe2017-11-06 14:13:29 -08002545 ipv6_create_tempaddr(ifp, NULL, false);
Jiri Pirko53bd6742013-12-06 09:45:22 +01002546 } else {
2547 read_unlock_bh(&idev->lock);
2548 }
2549}
2550
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01002551static bool is_addr_mode_generate_stable(struct inet6_dev *idev)
2552{
Felix Jiad35a00b2017-01-26 16:59:17 +13002553 return idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY ||
2554 idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_RANDOM;
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01002555}
2556
Alexander Aringcc84b3c2016-06-15 21:20:24 +02002557int addrconf_prefix_rcv_add_addr(struct net *net, struct net_device *dev,
2558 const struct prefix_info *pinfo,
2559 struct inet6_dev *in6_dev,
2560 const struct in6_addr *addr, int addr_type,
2561 u32 addr_flags, bool sllao, bool tokenized,
2562 __u32 valid_lft, u32 prefered_lft)
Alexander Aring4f672232016-06-15 21:20:22 +02002563{
2564 struct inet6_ifaddr *ifp = ipv6_get_ifaddr(net, addr, dev, 1);
2565 int create = 0, update_lft = 0;
2566
2567 if (!ifp && valid_lft) {
2568 int max_addresses = in6_dev->cnf.max_addresses;
David Aherne6464b82018-05-27 08:09:53 -07002569 struct ifa6_config cfg = {
2570 .pfx = addr,
2571 .plen = pinfo->prefix_len,
2572 .ifa_flags = addr_flags,
2573 .valid_lft = valid_lft,
2574 .preferred_lft = prefered_lft,
2575 .scope = addr_type & IPV6_ADDR_SCOPE_MASK,
2576 };
Alexander Aring4f672232016-06-15 21:20:22 +02002577
2578#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
Matteo Croce35e015e2017-09-12 17:46:37 +02002579 if ((net->ipv6.devconf_all->optimistic_dad ||
2580 in6_dev->cnf.optimistic_dad) &&
Alexander Aring4f672232016-06-15 21:20:22 +02002581 !net->ipv6.devconf_all->forwarding && sllao)
David Aherne6464b82018-05-27 08:09:53 -07002582 cfg.ifa_flags |= IFA_F_OPTIMISTIC;
Alexander Aring4f672232016-06-15 21:20:22 +02002583#endif
2584
2585 /* Do not allow to create too much of autoconfigured
2586 * addresses; this would be too easy way to crash kernel.
2587 */
2588 if (!max_addresses ||
2589 ipv6_count_addresses(in6_dev) < max_addresses)
David Aherne6464b82018-05-27 08:09:53 -07002590 ifp = ipv6_add_addr(in6_dev, &cfg, false, NULL);
Alexander Aring4f672232016-06-15 21:20:22 +02002591
2592 if (IS_ERR_OR_NULL(ifp))
2593 return -1;
2594
Alexander Aring4f672232016-06-15 21:20:22 +02002595 create = 1;
2596 spin_lock_bh(&ifp->lock);
2597 ifp->flags |= IFA_F_MANAGETEMPADDR;
2598 ifp->cstamp = jiffies;
2599 ifp->tokenized = tokenized;
2600 spin_unlock_bh(&ifp->lock);
2601 addrconf_dad_start(ifp);
2602 }
2603
2604 if (ifp) {
2605 u32 flags;
2606 unsigned long now;
2607 u32 stored_lft;
2608
2609 /* update lifetime (RFC2462 5.5.3 e) */
2610 spin_lock_bh(&ifp->lock);
2611 now = jiffies;
2612 if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
2613 stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
2614 else
2615 stored_lft = 0;
Lorenzo Bianconif85f94b2018-04-16 17:52:59 +02002616 if (!create && stored_lft) {
Alexander Aring4f672232016-06-15 21:20:22 +02002617 const u32 minimum_lft = min_t(u32,
2618 stored_lft, MIN_VALID_LIFETIME);
2619 valid_lft = max(valid_lft, minimum_lft);
2620
2621 /* RFC4862 Section 5.5.3e:
2622 * "Note that the preferred lifetime of the
2623 * corresponding address is always reset to
2624 * the Preferred Lifetime in the received
2625 * Prefix Information option, regardless of
2626 * whether the valid lifetime is also reset or
2627 * ignored."
2628 *
2629 * So we should always update prefered_lft here.
2630 */
2631 update_lft = 1;
2632 }
2633
2634 if (update_lft) {
2635 ifp->valid_lft = valid_lft;
2636 ifp->prefered_lft = prefered_lft;
2637 ifp->tstamp = now;
2638 flags = ifp->flags;
2639 ifp->flags &= ~IFA_F_DEPRECATED;
2640 spin_unlock_bh(&ifp->lock);
2641
2642 if (!(flags&IFA_F_TENTATIVE))
2643 ipv6_ifa_notify(0, ifp);
2644 } else
2645 spin_unlock_bh(&ifp->lock);
2646
2647 manage_tempaddrs(in6_dev, ifp, valid_lft, prefered_lft,
2648 create, now);
2649
2650 in6_ifa_put(ifp);
2651 addrconf_verify();
2652 }
2653
2654 return 0;
2655}
Alexander Aringcc84b3c2016-06-15 21:20:24 +02002656EXPORT_SYMBOL_GPL(addrconf_prefix_rcv_add_addr);
Alexander Aring4f672232016-06-15 21:20:22 +02002657
Neil Hormane6bff992012-01-04 10:49:15 +00002658void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len, bool sllao)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659{
2660 struct prefix_info *pinfo;
2661 __u32 valid_lft;
2662 __u32 prefered_lft;
Alexander Aring4f672232016-06-15 21:20:22 +02002663 int addr_type, err;
Hannes Frederic Sowa64236f32015-03-23 23:36:02 +01002664 u32 addr_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 struct inet6_dev *in6_dev;
Brian Haley56d417b2009-06-01 03:07:33 -07002666 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667
2668 pinfo = (struct prefix_info *) opt;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002669
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 if (len < sizeof(struct prefix_info)) {
Joe Perchese32ac252018-03-26 08:35:01 -07002671 netdev_dbg(dev, "addrconf: prefix option too short\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 return;
2673 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002674
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 /*
2676 * Validation checks ([ADDRCONF], page 19)
2677 */
2678
2679 addr_type = ipv6_addr_type(&pinfo->prefix);
2680
2681 if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
2682 return;
2683
2684 valid_lft = ntohl(pinfo->valid);
2685 prefered_lft = ntohl(pinfo->prefered);
2686
2687 if (prefered_lft > valid_lft) {
Joe Perchese87cc472012-05-13 21:56:26 +00002688 net_warn_ratelimited("addrconf: prefix option has invalid lifetime\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 return;
2690 }
2691
2692 in6_dev = in6_dev_get(dev);
2693
Ian Morris63159f22015-03-29 14:00:04 +01002694 if (!in6_dev) {
Joe Perchese87cc472012-05-13 21:56:26 +00002695 net_dbg_ratelimited("addrconf: device %s not configured\n",
2696 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 return;
2698 }
2699
2700 /*
2701 * Two things going on here:
2702 * 1) Add routes for on-link prefixes
2703 * 2) Configure prefixes with the auto flag set
2704 */
2705
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002706 if (pinfo->onlink) {
David Ahern8d1c8022018-04-17 17:33:26 -07002707 struct fib6_info *rt;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002708 unsigned long rt_expires;
2709
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002710 /* Avoid arithmetic overflow. Really, we could
2711 * save rt_expires in seconds, likely valid_lft,
2712 * but it would require division in fib gc, that it
2713 * not good.
2714 */
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002715 if (HZ > USER_HZ)
2716 rt_expires = addrconf_timeout_fixup(valid_lft, HZ);
2717 else
2718 rt_expires = addrconf_timeout_fixup(valid_lft, USER_HZ);
YOSHIFUJI Hideaki3dd4bc62005-12-19 14:02:45 -08002719
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002720 if (addrconf_finite_timeout(rt_expires))
2721 rt_expires *= HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002723 rt = addrconf_get_prefix_route(&pinfo->prefix,
2724 pinfo->prefix_len,
2725 dev,
2726 RTF_ADDRCONF | RTF_PREFIX_RT,
David Ahern2b2450c2019-03-27 20:53:52 -07002727 RTF_DEFAULT, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002729 if (rt) {
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002730 /* Autoconf prefix route */
2731 if (valid_lft == 0) {
David Ahernafb1d4b52018-04-17 17:33:11 -07002732 ip6_del_rt(net, rt);
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002733 rt = NULL;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002734 } else if (addrconf_finite_timeout(rt_expires)) {
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002735 /* not infinity */
David Ahern14895682018-04-17 17:33:17 -07002736 fib6_set_expires(rt, jiffies + rt_expires);
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002737 } else {
David Ahern14895682018-04-17 17:33:17 -07002738 fib6_clean_expires(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 }
2740 } else if (valid_lft) {
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002741 clock_t expires = 0;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002742 int flags = RTF_ADDRCONF | RTF_PREFIX_RT;
2743 if (addrconf_finite_timeout(rt_expires)) {
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002744 /* not infinity */
2745 flags |= RTF_EXPIRES;
2746 expires = jiffies_to_clock_t(rt_expires);
2747 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
David Ahern8308f3f2018-05-27 08:09:58 -07002749 0, dev, expires, flags,
2750 GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 }
David Ahern93531c62018-04-17 17:33:25 -07002752 fib6_info_release(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 }
2754
2755 /* Try to figure out our local address for this prefix */
2756
2757 if (pinfo->autoconf && in6_dev->cnf.autoconf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 struct in6_addr addr;
Alexander Aringf997c552016-06-15 21:20:23 +02002759 bool tokenized = false, dev_addr_generated = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760
2761 if (pinfo->prefix_len == 64) {
2762 memcpy(&addr, &pinfo->prefix, 8);
Daniel Borkmannf53adae2013-04-08 04:01:30 +00002763
2764 if (!ipv6_addr_any(&in6_dev->token)) {
2765 read_lock_bh(&in6_dev->lock);
2766 memcpy(addr.s6_addr + 8,
2767 in6_dev->token.s6_addr + 8, 8);
2768 read_unlock_bh(&in6_dev->lock);
Daniel Borkmann617fe292013-04-09 03:47:16 +00002769 tokenized = true;
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01002770 } else if (is_addr_mode_generate_stable(in6_dev) &&
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01002771 !ipv6_generate_stable_address(&addr, 0,
2772 in6_dev)) {
Hannes Frederic Sowa64236f32015-03-23 23:36:02 +01002773 addr_flags |= IFA_F_STABLE_PRIVACY;
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01002774 goto ok;
Daniel Borkmannf53adae2013-04-08 04:01:30 +00002775 } else if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
2776 ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
Alexander Aring4f672232016-06-15 21:20:22 +02002777 goto put;
Alexander Aringf997c552016-06-15 21:20:23 +02002778 } else {
2779 dev_addr_generated = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 }
2781 goto ok;
2782 }
Joe Perchese87cc472012-05-13 21:56:26 +00002783 net_dbg_ratelimited("IPv6 addrconf: prefix with wrong length %d\n",
2784 pinfo->prefix_len);
Alexander Aring4f672232016-06-15 21:20:22 +02002785 goto put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786
2787ok:
Alexander Aring4f672232016-06-15 21:20:22 +02002788 err = addrconf_prefix_rcv_add_addr(net, dev, pinfo, in6_dev,
2789 &addr, addr_type,
2790 addr_flags, sllao,
2791 tokenized, valid_lft,
2792 prefered_lft);
2793 if (err)
2794 goto put;
Alexander Aringf997c552016-06-15 21:20:23 +02002795
2796 /* Ignore error case here because previous prefix add addr was
2797 * successful which will be notified.
2798 */
2799 ndisc_ops_prefix_rcv_add_addr(net, dev, pinfo, in6_dev, &addr,
2800 addr_type, addr_flags, sllao,
2801 tokenized, valid_lft,
2802 prefered_lft,
2803 dev_addr_generated);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 }
2805 inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
Alexander Aring4f672232016-06-15 21:20:22 +02002806put:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 in6_dev_put(in6_dev);
2808}
2809
2810/*
2811 * Set destination address.
2812 * Special case for SIT interfaces where we create a new "virtual"
2813 * device.
2814 */
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002815int addrconf_set_dstaddr(struct net *net, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816{
2817 struct in6_ifreq ireq;
2818 struct net_device *dev;
2819 int err = -EINVAL;
2820
2821 rtnl_lock();
2822
2823 err = -EFAULT;
2824 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2825 goto err_exit;
2826
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002827 dev = __dev_get_by_index(net, ireq.ifr6_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828
2829 err = -ENODEV;
Ian Morris63159f22015-03-29 14:00:04 +01002830 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 goto err_exit;
2832
Amerigo Wang07a93622012-10-29 16:23:10 +00002833#if IS_ENABLED(CONFIG_IPV6_SIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 if (dev->type == ARPHRD_SIT) {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -08002835 const struct net_device_ops *ops = dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 struct ifreq ifr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 struct ip_tunnel_parm p;
2838
2839 err = -EADDRNOTAVAIL;
2840 if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4))
2841 goto err_exit;
2842
2843 memset(&p, 0, sizeof(p));
2844 p.iph.daddr = ireq.ifr6_addr.s6_addr32[3];
2845 p.iph.saddr = 0;
2846 p.iph.version = 4;
2847 p.iph.ihl = 5;
2848 p.iph.protocol = IPPROTO_IPV6;
2849 p.iph.ttl = 64;
Stephen Hemmingerd20b3102008-01-21 00:48:43 -08002850 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -08002852 if (ops->ndo_do_ioctl) {
2853 mm_segment_t oldfs = get_fs();
2854
2855 set_fs(KERNEL_DS);
2856 err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
2857 set_fs(oldfs);
2858 } else
2859 err = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860
2861 if (err == 0) {
2862 err = -ENOBUFS;
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002863 dev = __dev_get_by_name(net, p.name);
2864 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 goto err_exit;
Petr Machata00f54e62018-12-06 17:05:36 +00002866 err = dev_open(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 }
2868 }
Joerg Roedel0be669b2006-10-10 14:49:53 -07002869#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870
2871err_exit:
2872 rtnl_unlock();
2873 return err;
2874}
2875
Madhu Challa93a714d2015-02-25 09:58:35 -08002876static int ipv6_mc_config(struct sock *sk, bool join,
2877 const struct in6_addr *addr, int ifindex)
2878{
2879 int ret;
2880
2881 ASSERT_RTNL();
2882
2883 lock_sock(sk);
2884 if (join)
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03002885 ret = ipv6_sock_mc_join(sk, ifindex, addr);
Madhu Challa93a714d2015-02-25 09:58:35 -08002886 else
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03002887 ret = ipv6_sock_mc_drop(sk, ifindex, addr);
Madhu Challa93a714d2015-02-25 09:58:35 -08002888 release_sock(sk);
2889
2890 return ret;
2891}
2892
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893/*
2894 * Manual configuration of address on an interface
2895 */
Jiri Pirko479840f2013-12-06 09:45:21 +01002896static int inet6_addr_add(struct net *net, int ifindex,
David Ahern19b15182018-05-27 08:09:54 -07002897 struct ifa6_config *cfg,
David Ahernde95e042017-10-18 09:56:54 -07002898 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899{
2900 struct inet6_ifaddr *ifp;
2901 struct inet6_dev *idev;
2902 struct net_device *dev;
Madhu Challa93a714d2015-02-25 09:58:35 -08002903 unsigned long timeout;
2904 clock_t expires;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002905 u32 flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906
2907 ASSERT_RTNL();
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002908
David Ahern19b15182018-05-27 08:09:54 -07002909 if (cfg->plen > 128)
Thomas Graf24ef0da2008-05-28 16:54:22 +02002910 return -EINVAL;
2911
Noriaki TAKAMIYA0778769d2006-07-28 18:12:10 +09002912 /* check the lifetime */
David Ahern19b15182018-05-27 08:09:54 -07002913 if (!cfg->valid_lft || cfg->preferred_lft > cfg->valid_lft)
Noriaki TAKAMIYA0778769d2006-07-28 18:12:10 +09002914 return -EINVAL;
2915
David Ahern19b15182018-05-27 08:09:54 -07002916 if (cfg->ifa_flags & IFA_F_MANAGETEMPADDR && cfg->plen != 64)
Jiri Pirko53bd6742013-12-06 09:45:22 +01002917 return -EINVAL;
2918
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002919 dev = __dev_get_by_index(net, ifindex);
2920 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921 return -ENODEV;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002922
Brian Haley64e724f2010-07-20 10:34:30 +00002923 idev = addrconf_add_dev(dev);
2924 if (IS_ERR(idev))
2925 return PTR_ERR(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926
David Ahern19b15182018-05-27 08:09:54 -07002927 if (cfg->ifa_flags & IFA_F_MCAUTOJOIN) {
Madhu Challa93a714d2015-02-25 09:58:35 -08002928 int ret = ipv6_mc_config(net->ipv6.mc_autojoin_sk,
David Ahern19b15182018-05-27 08:09:54 -07002929 true, cfg->pfx, ifindex);
Madhu Challa93a714d2015-02-25 09:58:35 -08002930
2931 if (ret < 0)
2932 return ret;
2933 }
2934
David Ahern19b15182018-05-27 08:09:54 -07002935 cfg->scope = ipv6_addr_scope(cfg->pfx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936
David Ahern19b15182018-05-27 08:09:54 -07002937 timeout = addrconf_timeout_fixup(cfg->valid_lft, HZ);
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002938 if (addrconf_finite_timeout(timeout)) {
2939 expires = jiffies_to_clock_t(timeout * HZ);
David Ahern19b15182018-05-27 08:09:54 -07002940 cfg->valid_lft = timeout;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002941 flags = RTF_EXPIRES;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002942 } else {
2943 expires = 0;
2944 flags = 0;
David Ahern19b15182018-05-27 08:09:54 -07002945 cfg->ifa_flags |= IFA_F_PERMANENT;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002946 }
Noriaki TAKAMIYA0778769d2006-07-28 18:12:10 +09002947
David Ahern19b15182018-05-27 08:09:54 -07002948 timeout = addrconf_timeout_fixup(cfg->preferred_lft, HZ);
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002949 if (addrconf_finite_timeout(timeout)) {
2950 if (timeout == 0)
David Ahern19b15182018-05-27 08:09:54 -07002951 cfg->ifa_flags |= IFA_F_DEPRECATED;
2952 cfg->preferred_lft = timeout;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002953 }
Noriaki TAKAMIYA0778769d2006-07-28 18:12:10 +09002954
David Ahern19b15182018-05-27 08:09:54 -07002955 ifp = ipv6_add_addr(idev, cfg, true, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 if (!IS_ERR(ifp)) {
David Ahern19b15182018-05-27 08:09:54 -07002957 if (!(cfg->ifa_flags & IFA_F_NOPREFIXROUTE)) {
David Ahern8308f3f2018-05-27 08:09:58 -07002958 addrconf_prefix_route(&ifp->addr, ifp->prefix_len,
2959 ifp->rt_priority, dev, expires,
2960 flags, GFP_KERNEL);
Thomas Haller761aac72014-01-15 15:36:58 +01002961 }
2962
Lorenzo Bianconia2d481b2018-04-17 11:54:39 +02002963 /* Send a netlink notification if DAD is enabled and
2964 * optimistic flag is not set
2965 */
2966 if (!(ifp->flags & (IFA_F_OPTIMISTIC | IFA_F_NODAD)))
2967 ipv6_ifa_notify(0, ifp);
Neil Horman95c385b2007-04-25 17:08:10 -07002968 /*
2969 * Note that section 3.1 of RFC 4429 indicates
2970 * that the Optimistic flag should not be set for
2971 * manually configured addresses
2972 */
David S. Millercf22f9a2012-04-14 21:37:40 -04002973 addrconf_dad_start(ifp);
David Ahern19b15182018-05-27 08:09:54 -07002974 if (cfg->ifa_flags & IFA_F_MANAGETEMPADDR)
2975 manage_tempaddrs(idev, ifp, cfg->valid_lft,
2976 cfg->preferred_lft, true, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 in6_ifa_put(ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01002978 addrconf_verify_rtnl();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 return 0;
David Ahern19b15182018-05-27 08:09:54 -07002980 } else if (cfg->ifa_flags & IFA_F_MCAUTOJOIN) {
2981 ipv6_mc_config(net->ipv6.mc_autojoin_sk, false,
2982 cfg->pfx, ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 }
2984
2985 return PTR_ERR(ifp);
2986}
2987
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02002988static int inet6_addr_del(struct net *net, int ifindex, u32 ifa_flags,
2989 const struct in6_addr *pfx, unsigned int plen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990{
2991 struct inet6_ifaddr *ifp;
2992 struct inet6_dev *idev;
2993 struct net_device *dev;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002994
Thomas Graf24ef0da2008-05-28 16:54:22 +02002995 if (plen > 128)
2996 return -EINVAL;
2997
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002998 dev = __dev_get_by_index(net, ifindex);
2999 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 return -ENODEV;
3001
Ian Morrise5d08d72014-11-23 21:28:43 +00003002 idev = __in6_dev_get(dev);
Ian Morris63159f22015-03-29 14:00:04 +01003003 if (!idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 return -ENXIO;
3005
3006 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00003007 list_for_each_entry(ifp, &idev->addr_list, if_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 if (ifp->prefix_len == plen &&
3009 ipv6_addr_equal(pfx, &ifp->addr)) {
3010 in6_ifa_hold(ifp);
3011 read_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003012
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02003013 if (!(ifp->flags & IFA_F_TEMPORARY) &&
3014 (ifa_flags & IFA_F_MANAGETEMPADDR))
3015 manage_tempaddrs(idev, ifp, 0, 0, false,
3016 jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 ipv6_del_addr(ifp);
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02003018 addrconf_verify_rtnl();
Madhu Challa93a714d2015-02-25 09:58:35 -08003019 if (ipv6_addr_is_multicast(pfx)) {
3020 ipv6_mc_config(net->ipv6.mc_autojoin_sk,
3021 false, pfx, dev->ifindex);
3022 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023 return 0;
3024 }
3025 }
3026 read_unlock_bh(&idev->lock);
3027 return -EADDRNOTAVAIL;
3028}
3029
3030
Daniel Lezcanoaf284932008-03-05 10:46:57 -08003031int addrconf_add_ifaddr(struct net *net, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032{
David Ahern19b15182018-05-27 08:09:54 -07003033 struct ifa6_config cfg = {
3034 .ifa_flags = IFA_F_PERMANENT,
3035 .preferred_lft = INFINITY_LIFE_TIME,
3036 .valid_lft = INFINITY_LIFE_TIME,
3037 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 struct in6_ifreq ireq;
3039 int err;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003040
Eric W. Biedermanaf31f412012-11-16 03:03:06 +00003041 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 return -EPERM;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003043
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
3045 return -EFAULT;
3046
David Ahern19b15182018-05-27 08:09:54 -07003047 cfg.pfx = &ireq.ifr6_addr;
3048 cfg.plen = ireq.ifr6_prefixlen;
3049
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 rtnl_lock();
David Ahern19b15182018-05-27 08:09:54 -07003051 err = inet6_addr_add(net, ireq.ifr6_ifindex, &cfg, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052 rtnl_unlock();
3053 return err;
3054}
3055
Daniel Lezcanoaf284932008-03-05 10:46:57 -08003056int addrconf_del_ifaddr(struct net *net, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057{
3058 struct in6_ifreq ireq;
3059 int err;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003060
Eric W. Biedermanaf31f412012-11-16 03:03:06 +00003061 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 return -EPERM;
3063
3064 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
3065 return -EFAULT;
3066
3067 rtnl_lock();
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02003068 err = inet6_addr_del(net, ireq.ifr6_ifindex, 0, &ireq.ifr6_addr,
Daniel Lezcanoaf284932008-03-05 10:46:57 -08003069 ireq.ifr6_prefixlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 rtnl_unlock();
3071 return err;
3072}
3073
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08003074static void add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
3075 int plen, int scope)
3076{
3077 struct inet6_ifaddr *ifp;
David Aherne6464b82018-05-27 08:09:53 -07003078 struct ifa6_config cfg = {
3079 .pfx = addr,
3080 .plen = plen,
3081 .ifa_flags = IFA_F_PERMANENT,
3082 .valid_lft = INFINITY_LIFE_TIME,
3083 .preferred_lft = INFINITY_LIFE_TIME,
3084 .scope = scope
3085 };
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08003086
David Aherne6464b82018-05-27 08:09:53 -07003087 ifp = ipv6_add_addr(idev, &cfg, true, NULL);
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08003088 if (!IS_ERR(ifp)) {
3089 spin_lock_bh(&ifp->lock);
3090 ifp->flags &= ~IFA_F_TENTATIVE;
3091 spin_unlock_bh(&ifp->lock);
Paolo Abeni764d3be2016-11-22 16:57:40 +01003092 rt_genid_bump_ipv6(dev_net(idev->dev));
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08003093 ipv6_ifa_notify(RTM_NEWADDR, ifp);
3094 in6_ifa_put(ifp);
3095 }
3096}
3097
Amerigo Wang07a93622012-10-29 16:23:10 +00003098#if IS_ENABLED(CONFIG_IPV6_SIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099static void sit_add_v4_addrs(struct inet6_dev *idev)
3100{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 struct in6_addr addr;
3102 struct net_device *dev;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09003103 struct net *net = dev_net(idev->dev);
Nicolas Dichtel929c9cf2013-11-14 13:51:05 +01003104 int scope, plen;
Nicolas Dichtelf0e2acf2013-11-14 13:51:06 +01003105 u32 pflags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106
3107 ASSERT_RTNL();
3108
3109 memset(&addr, 0, sizeof(struct in6_addr));
3110 memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
3111
3112 if (idev->dev->flags&IFF_POINTOPOINT) {
3113 addr.s6_addr32[0] = htonl(0xfe800000);
3114 scope = IFA_LINK;
Nicolas Dichtel929c9cf2013-11-14 13:51:05 +01003115 plen = 64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116 } else {
3117 scope = IPV6_ADDR_COMPATv4;
Nicolas Dichtel929c9cf2013-11-14 13:51:05 +01003118 plen = 96;
Nicolas Dichtelf0e2acf2013-11-14 13:51:06 +01003119 pflags |= RTF_NONEXTHOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 }
3121
3122 if (addr.s6_addr32[3]) {
Nicolas Dichtel929c9cf2013-11-14 13:51:05 +01003123 add_addr(idev, &addr, plen, scope);
David Ahern8308f3f2018-05-27 08:09:58 -07003124 addrconf_prefix_route(&addr, plen, 0, idev->dev, 0, pflags,
Cong Wange500c6d2018-08-22 12:58:34 -07003125 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126 return;
3127 }
3128
Benjamin Thery6fda7352008-03-05 10:47:47 -08003129 for_each_netdev(net, dev) {
Eldad Zack8e5e8f32012-04-01 07:49:08 +00003130 struct in_device *in_dev = __in_dev_get_rtnl(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 if (in_dev && (dev->flags & IFF_UP)) {
Eldad Zack8e5e8f32012-04-01 07:49:08 +00003132 struct in_ifaddr *ifa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 int flag = scope;
3134
Florian Westphalcd5a4112019-05-31 18:27:07 +02003135 in_dev_for_each_ifa_rtnl(ifa, in_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 addr.s6_addr32[3] = ifa->ifa_local;
3137
3138 if (ifa->ifa_scope == RT_SCOPE_LINK)
3139 continue;
3140 if (ifa->ifa_scope >= RT_SCOPE_HOST) {
3141 if (idev->dev->flags&IFF_POINTOPOINT)
3142 continue;
3143 flag |= IFA_HOST;
3144 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08003146 add_addr(idev, &addr, plen, flag);
David Ahern8308f3f2018-05-27 08:09:58 -07003147 addrconf_prefix_route(&addr, plen, 0, idev->dev,
Cong Wange500c6d2018-08-22 12:58:34 -07003148 0, pflags, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 }
3150 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003151 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152}
Joerg Roedel0be669b2006-10-10 14:49:53 -07003153#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154
3155static void init_loopback(struct net_device *dev)
3156{
3157 struct inet6_dev *idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158
3159 /* ::1 */
3160
3161 ASSERT_RTNL();
3162
Ian Morrise5d08d72014-11-23 21:28:43 +00003163 idev = ipv6_find_idev(dev);
Sabrina Dubrocadb0b99f2019-08-23 15:44:36 +02003164 if (IS_ERR(idev)) {
Joe Perches91df42b2012-05-15 14:11:54 +00003165 pr_debug("%s: add_dev failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166 return;
3167 }
3168
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08003169 add_addr(idev, &in6addr_loopback, 128, IFA_HOST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170}
3171
Alexander Aring2ad3ed52016-06-15 21:20:17 +02003172void addrconf_add_linklocal(struct inet6_dev *idev,
3173 const struct in6_addr *addr, u32 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174{
David Aherne6464b82018-05-27 08:09:53 -07003175 struct ifa6_config cfg = {
3176 .pfx = addr,
3177 .plen = 64,
3178 .ifa_flags = flags | IFA_F_PERMANENT,
3179 .valid_lft = INFINITY_LIFE_TIME,
3180 .preferred_lft = INFINITY_LIFE_TIME,
3181 .scope = IFA_LINK
3182 };
Eldad Zack8e5e8f32012-04-01 07:49:08 +00003183 struct inet6_ifaddr *ifp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184
Neil Horman95c385b2007-04-25 17:08:10 -07003185#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
Matteo Croce35e015e2017-09-12 17:46:37 +02003186 if ((dev_net(idev->dev)->ipv6.devconf_all->optimistic_dad ||
3187 idev->cnf.optimistic_dad) &&
David Miller702beb82008-07-20 18:17:02 -07003188 !dev_net(idev->dev)->ipv6.devconf_all->forwarding)
David Aherne6464b82018-05-27 08:09:53 -07003189 cfg.ifa_flags |= IFA_F_OPTIMISTIC;
Neil Horman95c385b2007-04-25 17:08:10 -07003190#endif
3191
David Aherne6464b82018-05-27 08:09:53 -07003192 ifp = ipv6_add_addr(idev, &cfg, true, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 if (!IS_ERR(ifp)) {
David Ahern8308f3f2018-05-27 08:09:58 -07003194 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, 0, idev->dev,
David Ahernacb54e32018-04-17 17:33:22 -07003195 0, 0, GFP_ATOMIC);
David S. Millercf22f9a2012-04-14 21:37:40 -04003196 addrconf_dad_start(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 in6_ifa_put(ifp);
3198 }
3199}
Alexander Aring2ad3ed52016-06-15 21:20:17 +02003200EXPORT_SYMBOL_GPL(addrconf_add_linklocal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01003202static bool ipv6_reserved_interfaceid(struct in6_addr address)
3203{
3204 if ((address.s6_addr32[2] | address.s6_addr32[3]) == 0)
3205 return true;
3206
3207 if (address.s6_addr32[2] == htonl(0x02005eff) &&
3208 ((address.s6_addr32[3] & htonl(0xfe000000)) == htonl(0xfe000000)))
3209 return true;
3210
3211 if (address.s6_addr32[2] == htonl(0xfdffffff) &&
3212 ((address.s6_addr32[3] & htonl(0xffffff80)) == htonl(0xffffff80)))
3213 return true;
3214
3215 return false;
3216}
3217
3218static int ipv6_generate_stable_address(struct in6_addr *address,
3219 u8 dad_count,
3220 const struct inet6_dev *idev)
3221{
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01003222 static DEFINE_SPINLOCK(lock);
3223 static __u32 digest[SHA_DIGEST_WORDS];
3224 static __u32 workspace[SHA_WORKSPACE_WORDS];
3225
3226 static union {
3227 char __data[SHA_MESSAGE_BYTES];
3228 struct {
3229 struct in6_addr secret;
Hannes Frederic Sowaff402172015-03-24 11:05:28 +01003230 __be32 prefix[2];
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01003231 unsigned char hwaddr[MAX_ADDR_LEN];
3232 u8 dad_count;
3233 } __packed;
3234 } data;
3235
3236 struct in6_addr secret;
3237 struct in6_addr temp;
3238 struct net *net = dev_net(idev->dev);
3239
3240 BUILD_BUG_ON(sizeof(data.__data) != sizeof(data));
3241
3242 if (idev->cnf.stable_secret.initialized)
3243 secret = idev->cnf.stable_secret.secret;
3244 else if (net->ipv6.devconf_dflt->stable_secret.initialized)
3245 secret = net->ipv6.devconf_dflt->stable_secret.secret;
3246 else
3247 return -1;
3248
3249retry:
3250 spin_lock_bh(&lock);
3251
3252 sha_init(digest);
3253 memset(&data, 0, sizeof(data));
3254 memset(workspace, 0, sizeof(workspace));
3255 memcpy(data.hwaddr, idev->dev->perm_addr, idev->dev->addr_len);
Hannes Frederic Sowaff402172015-03-24 11:05:28 +01003256 data.prefix[0] = address->s6_addr32[0];
3257 data.prefix[1] = address->s6_addr32[1];
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01003258 data.secret = secret;
3259 data.dad_count = dad_count;
3260
3261 sha_transform(digest, data.__data, workspace);
3262
3263 temp = *address;
Hannes Frederic Sowaff402172015-03-24 11:05:28 +01003264 temp.s6_addr32[2] = (__force __be32)digest[0];
3265 temp.s6_addr32[3] = (__force __be32)digest[1];
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01003266
3267 spin_unlock_bh(&lock);
3268
3269 if (ipv6_reserved_interfaceid(temp)) {
3270 dad_count++;
Hannes Frederic Sowa1855b7c2015-03-23 23:36:05 +01003271 if (dad_count > dev_net(idev->dev)->ipv6.sysctl.idgen_retries)
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01003272 return -1;
3273 goto retry;
3274 }
3275
3276 *address = temp;
3277 return 0;
3278}
3279
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003280static void ipv6_gen_mode_random_init(struct inet6_dev *idev)
3281{
3282 struct ipv6_stable_secret *s = &idev->cnf.stable_secret;
3283
3284 if (s->initialized)
3285 return;
3286 s = &idev->cnf.stable_secret;
3287 get_random_bytes(&s->secret, sizeof(s->secret));
3288 s->initialized = true;
3289}
3290
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003291static void addrconf_addr_gen(struct inet6_dev *idev, bool prefix_route)
3292{
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01003293 struct in6_addr addr;
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003294
David Ahernca254492015-10-12 11:47:10 -07003295 /* no link local addresses on L3 master devices */
3296 if (netif_is_l3_master(idev->dev))
3297 return;
3298
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01003299 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
3300
Felix Jiad35a00b2017-01-26 16:59:17 +13003301 switch (idev->cnf.addr_gen_mode) {
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003302 case IN6_ADDR_GEN_MODE_RANDOM:
3303 ipv6_gen_mode_random_init(idev);
3304 /* fallthrough */
3305 case IN6_ADDR_GEN_MODE_STABLE_PRIVACY:
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01003306 if (!ipv6_generate_stable_address(&addr, 0, idev))
Hannes Frederic Sowa64236f32015-03-23 23:36:02 +01003307 addrconf_add_linklocal(idev, &addr,
3308 IFA_F_STABLE_PRIVACY);
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01003309 else if (prefix_route)
David Ahern8308f3f2018-05-27 08:09:58 -07003310 addrconf_prefix_route(&addr, 64, 0, idev->dev,
David Ahernacb54e32018-04-17 17:33:22 -07003311 0, 0, GFP_KERNEL);
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003312 break;
3313 case IN6_ADDR_GEN_MODE_EUI64:
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003314 /* addrconf_add_linklocal also adds a prefix_route and we
3315 * only need to care about prefix routes if ipv6_generate_eui64
3316 * couldn't generate one.
3317 */
3318 if (ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) == 0)
Hannes Frederic Sowa64236f32015-03-23 23:36:02 +01003319 addrconf_add_linklocal(idev, &addr, 0);
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003320 else if (prefix_route)
David Ahern8308f3f2018-05-27 08:09:58 -07003321 addrconf_prefix_route(&addr, 64, 0, idev->dev,
David Ahern27b10602018-04-18 15:39:06 -07003322 0, 0, GFP_KERNEL);
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003323 break;
3324 case IN6_ADDR_GEN_MODE_NONE:
3325 default:
3326 /* will not add any link local address */
3327 break;
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003328 }
3329}
3330
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331static void addrconf_dev_config(struct net_device *dev)
3332{
Eldad Zack8e5e8f32012-04-01 07:49:08 +00003333 struct inet6_dev *idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334
3335 ASSERT_RTNL();
3336
Herbert Xu74235a22007-06-14 13:02:55 -07003337 if ((dev->type != ARPHRD_ETHER) &&
3338 (dev->type != ARPHRD_FDDI) &&
Herbert Xu74235a22007-06-14 13:02:55 -07003339 (dev->type != ARPHRD_ARCNET) &&
alex.bluesman.smirnov@gmail.com06a4c1c2012-05-10 03:25:52 +00003340 (dev->type != ARPHRD_INFINIBAND) &&
Nicolas Dichtele8377352013-08-20 12:16:06 +02003341 (dev->type != ARPHRD_IEEE1394) &&
Jukka Rissanene74bccb82013-12-11 17:05:36 +02003342 (dev->type != ARPHRD_TUNNEL6) &&
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003343 (dev->type != ARPHRD_6LOWPAN) &&
Felix Jia45ce0fd2017-01-26 16:59:18 +13003344 (dev->type != ARPHRD_IP6GRE) &&
3345 (dev->type != ARPHRD_IPGRE) &&
3346 (dev->type != ARPHRD_TUNNEL) &&
Subash Abhinov Kasiviswanathan9deb4412018-06-04 19:26:07 -06003347 (dev->type != ARPHRD_NONE) &&
3348 (dev->type != ARPHRD_RAWIP)) {
Herbert Xu74235a22007-06-14 13:02:55 -07003349 /* Alas, we support only Ethernet autoconfiguration. */
Hangbin Liu60380482020-03-10 15:27:37 +08003350 idev = __in6_dev_get(dev);
3351 if (!IS_ERR_OR_NULL(idev) && dev->flags & IFF_UP &&
3352 dev->flags & IFF_MULTICAST)
3353 ipv6_mc_up(idev);
Herbert Xu74235a22007-06-14 13:02:55 -07003354 return;
3355 }
3356
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 idev = addrconf_add_dev(dev);
Brian Haley64e724f2010-07-20 10:34:30 +00003358 if (IS_ERR(idev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359 return;
3360
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003361 /* this device type has no EUI support */
3362 if (dev->type == ARPHRD_NONE &&
Felix Jiad35a00b2017-01-26 16:59:17 +13003363 idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_EUI64)
3364 idev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_RANDOM;
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003365
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003366 addrconf_addr_gen(idev, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367}
3368
Amerigo Wang07a93622012-10-29 16:23:10 +00003369#if IS_ENABLED(CONFIG_IPV6_SIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370static void addrconf_sit_config(struct net_device *dev)
3371{
3372 struct inet6_dev *idev;
3373
3374 ASSERT_RTNL();
3375
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003376 /*
3377 * Configure the tunnel with one of our IPv4
3378 * addresses... we should configure all of
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 * our v4 addrs in the tunnel
3380 */
3381
Ian Morrise5d08d72014-11-23 21:28:43 +00003382 idev = ipv6_find_idev(dev);
Sabrina Dubrocadb0b99f2019-08-23 15:44:36 +02003383 if (IS_ERR(idev)) {
Joe Perches91df42b2012-05-15 14:11:54 +00003384 pr_debug("%s: add_dev failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385 return;
3386 }
3387
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11003388 if (dev->priv_flags & IFF_ISATAP) {
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003389 addrconf_addr_gen(idev, false);
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11003390 return;
3391 }
3392
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393 sit_add_v4_addrs(idev);
3394
Nicolas Dichtel62b54dd2012-10-01 23:19:14 +00003395 if (dev->flags&IFF_POINTOPOINT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396 addrconf_add_mroute(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397}
Joerg Roedel0be669b2006-10-10 14:49:53 -07003398#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399
Amerigo Wang07a93622012-10-29 16:23:10 +00003400#if IS_ENABLED(CONFIG_NET_IPGRE)
stephen hemmingeraee80b52011-06-08 10:44:30 +00003401static void addrconf_gre_config(struct net_device *dev)
3402{
3403 struct inet6_dev *idev;
stephen hemmingeraee80b52011-06-08 10:44:30 +00003404
stephen hemmingeraee80b52011-06-08 10:44:30 +00003405 ASSERT_RTNL();
3406
Ian Morrise5d08d72014-11-23 21:28:43 +00003407 idev = ipv6_find_idev(dev);
Sabrina Dubrocadb0b99f2019-08-23 15:44:36 +02003408 if (IS_ERR(idev)) {
Joe Perches91df42b2012-05-15 14:11:54 +00003409 pr_debug("%s: add_dev failed\n", __func__);
stephen hemmingeraee80b52011-06-08 10:44:30 +00003410 return;
3411 }
3412
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003413 addrconf_addr_gen(idev, true);
Hannes Frederic Sowad9e4ce62015-10-08 18:19:39 +02003414 if (dev->flags & IFF_POINTOPOINT)
3415 addrconf_add_mroute(dev);
stephen hemmingeraee80b52011-06-08 10:44:30 +00003416}
3417#endif
3418
David Ahernafb1d4b52018-04-17 17:33:11 -07003419static int fixup_permanent_addr(struct net *net,
3420 struct inet6_dev *idev,
David Ahernf1705ec2016-02-24 09:25:37 -08003421 struct inet6_ifaddr *ifp)
3422{
David Ahern93c2fb22018-04-18 15:38:59 -07003423 /* !fib6_node means the host route was removed from the
David Ahern8048ced2017-04-25 09:17:29 -07003424 * FIB, for example, if 'lo' device is taken down. In that
3425 * case regenerate the host route.
3426 */
David Ahern93c2fb22018-04-18 15:38:59 -07003427 if (!ifp->rt || !ifp->rt->fib6_node) {
David Ahern360a9882018-04-18 15:39:00 -07003428 struct fib6_info *f6i, *prev;
David Ahernf1705ec2016-02-24 09:25:37 -08003429
David Ahern360a9882018-04-18 15:39:00 -07003430 f6i = addrconf_f6i_alloc(net, idev, &ifp->addr, false,
3431 GFP_ATOMIC);
3432 if (IS_ERR(f6i))
3433 return PTR_ERR(f6i);
David Ahernf1705ec2016-02-24 09:25:37 -08003434
David Ahern8048ced2017-04-25 09:17:29 -07003435 /* ifp->rt can be accessed outside of rtnl */
3436 spin_lock(&ifp->lock);
3437 prev = ifp->rt;
David Ahern360a9882018-04-18 15:39:00 -07003438 ifp->rt = f6i;
David Ahern8048ced2017-04-25 09:17:29 -07003439 spin_unlock(&ifp->lock);
3440
David Ahern93531c62018-04-17 17:33:25 -07003441 fib6_info_release(prev);
David Ahernf1705ec2016-02-24 09:25:37 -08003442 }
3443
3444 if (!(ifp->flags & IFA_F_NOPREFIXROUTE)) {
3445 addrconf_prefix_route(&ifp->addr, ifp->prefix_len,
David Ahern8308f3f2018-05-27 08:09:58 -07003446 ifp->rt_priority, idev->dev, 0, 0,
3447 GFP_ATOMIC);
David Ahernf1705ec2016-02-24 09:25:37 -08003448 }
3449
David Ahern6d717132017-05-02 14:43:44 -07003450 if (ifp->state == INET6_IFADDR_STATE_PREDAD)
3451 addrconf_dad_start(ifp);
David Ahernf1705ec2016-02-24 09:25:37 -08003452
3453 return 0;
3454}
3455
David Ahernafb1d4b52018-04-17 17:33:11 -07003456static void addrconf_permanent_addr(struct net *net, struct net_device *dev)
David Ahernf1705ec2016-02-24 09:25:37 -08003457{
3458 struct inet6_ifaddr *ifp, *tmp;
3459 struct inet6_dev *idev;
3460
3461 idev = __in6_dev_get(dev);
3462 if (!idev)
3463 return;
3464
3465 write_lock_bh(&idev->lock);
3466
3467 list_for_each_entry_safe(ifp, tmp, &idev->addr_list, if_list) {
3468 if ((ifp->flags & IFA_F_PERMANENT) &&
David Ahernafb1d4b52018-04-17 17:33:11 -07003469 fixup_permanent_addr(net, idev, ifp) < 0) {
David Ahernf1705ec2016-02-24 09:25:37 -08003470 write_unlock_bh(&idev->lock);
Eric Dumazete669b862017-10-30 22:47:09 -07003471 in6_ifa_hold(ifp);
David Ahernf1705ec2016-02-24 09:25:37 -08003472 ipv6_del_addr(ifp);
3473 write_lock_bh(&idev->lock);
3474
3475 net_info_ratelimited("%s: Failed to add prefix route for address %pI6c; dropping\n",
3476 idev->dev->name, &ifp->addr);
3477 }
3478 }
3479
3480 write_unlock_bh(&idev->lock);
3481}
3482
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003483static int addrconf_notify(struct notifier_block *this, unsigned long event,
Jiri Pirko351638e2013-05-28 01:30:21 +00003484 void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485{
Jiri Pirko351638e2013-05-28 01:30:21 +00003486 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Hangbin Liu896585d2018-11-21 21:52:33 +08003487 struct netdev_notifier_change_info *change_info;
David Ahern4f7f34e2016-04-07 11:10:41 -07003488 struct netdev_notifier_changeupper_info *info;
Eric Dumazet748e2d92012-08-22 21:50:59 +00003489 struct inet6_dev *idev = __in6_dev_get(dev);
WANG Cong60abc0b2017-06-21 14:34:58 -07003490 struct net *net = dev_net(dev);
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09003491 int run_pending = 0;
Herbert Xub217d612007-07-30 17:04:52 -07003492 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493
Stephen Hemmingere21e8462010-03-20 16:09:01 -07003494 switch (event) {
YOSHIFUJI Hideaki45ba9dd2007-02-15 02:07:27 +09003495 case NETDEV_REGISTER:
Herbert Xu74235a22007-06-14 13:02:55 -07003496 if (!idev && dev->mtu >= IPV6_MIN_MTU) {
YOSHIFUJI Hideaki45ba9dd2007-02-15 02:07:27 +09003497 idev = ipv6_add_dev(dev);
WANG Conga317a2f2014-07-25 15:25:09 -07003498 if (IS_ERR(idev))
3499 return notifier_from_errno(PTR_ERR(idev));
YOSHIFUJI Hideaki45ba9dd2007-02-15 02:07:27 +09003500 }
3501 break;
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003502
Alexander Duyckb7b0b1d2015-10-26 11:06:33 -07003503 case NETDEV_CHANGEMTU:
3504 /* if MTU under IPV6_MIN_MTU stop IPv6 on this interface. */
3505 if (dev->mtu < IPV6_MIN_MTU) {
WANG Cong60abc0b2017-06-21 14:34:58 -07003506 addrconf_ifdown(dev, dev != net->loopback_dev);
Alexander Duyckb7b0b1d2015-10-26 11:06:33 -07003507 break;
3508 }
3509
3510 if (idev) {
3511 rt6_mtu_change(dev, dev->mtu);
3512 idev->cnf.mtu6 = dev->mtu;
3513 break;
3514 }
3515
3516 /* allocate new idev */
3517 idev = ipv6_add_dev(dev);
3518 if (IS_ERR(idev))
3519 break;
3520
3521 /* device is still not ready */
3522 if (!(idev->if_flags & IF_READY))
3523 break;
3524
3525 run_pending = 1;
3526
3527 /* fall through */
3528
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529 case NETDEV_UP:
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003530 case NETDEV_CHANGE:
Jay Vosburghc2edacf2007-07-09 10:42:47 -07003531 if (dev->flags & IFF_SLAVE)
3532 break;
3533
WANG Cong3ce62a82014-09-11 15:07:16 -07003534 if (idev && idev->cnf.disable_ipv6)
3535 break;
3536
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003537 if (event == NETDEV_UP) {
David Ahern38bd10c2016-04-21 20:56:12 -07003538 /* restore routes for permanent addresses */
David Ahernafb1d4b52018-04-17 17:33:11 -07003539 addrconf_permanent_addr(net, dev);
David Ahern38bd10c2016-04-21 20:56:12 -07003540
Mike Manning1f372c72017-09-25 22:01:36 +01003541 if (!addrconf_link_ready(dev)) {
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003542 /* device is not ready yet. */
Lubomir Rintel7c62b8d2019-01-21 14:54:20 +01003543 pr_debug("ADDRCONF(NETDEV_UP): %s: link is not ready\n",
3544 dev->name);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003545 break;
3546 }
Kristian Slavov99081042006-02-08 16:10:53 -08003547
Evgeniy Polyakovd31c7b82007-11-30 23:36:08 +11003548 if (!idev && dev->mtu >= IPV6_MIN_MTU)
3549 idev = ipv6_add_dev(dev);
3550
WANG Conga317a2f2014-07-25 15:25:09 -07003551 if (!IS_ERR_OR_NULL(idev)) {
Kristian Slavov99081042006-02-08 16:10:53 -08003552 idev->if_flags |= IF_READY;
Benjamin Therye3ec6cf2008-11-05 01:43:57 -08003553 run_pending = 1;
3554 }
Alexander Duyckb7b0b1d2015-10-26 11:06:33 -07003555 } else if (event == NETDEV_CHANGE) {
Mike Manning1f372c72017-09-25 22:01:36 +01003556 if (!addrconf_link_ready(dev)) {
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003557 /* device is still not ready. */
Ido Schimmel27c6fa72018-01-07 12:45:05 +02003558 rt6_sync_down_dev(dev, event);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003559 break;
3560 }
3561
Hangbin Liu896585d2018-11-21 21:52:33 +08003562 if (!IS_ERR_OR_NULL(idev)) {
Linus Lüssinga088d1d2017-02-03 08:11:03 +01003563 if (idev->if_flags & IF_READY) {
3564 /* device is already configured -
3565 * but resend MLD reports, we might
3566 * have roamed and need to update
3567 * multicast snooping switches
3568 */
3569 ipv6_mc_up(idev);
Hangbin Liu896585d2018-11-21 21:52:33 +08003570 change_info = ptr;
3571 if (change_info->flags_changed & IFF_NOARP)
3572 addrconf_dad_run(idev, true);
Ido Schimmel27c6fa72018-01-07 12:45:05 +02003573 rt6_sync_up(dev, RTNH_F_LINKDOWN);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003574 break;
Linus Lüssinga088d1d2017-02-03 08:11:03 +01003575 }
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003576 idev->if_flags |= IF_READY;
3577 }
3578
Joe Perchesf3213832012-05-15 14:11:53 +00003579 pr_info("ADDRCONF(NETDEV_CHANGE): %s: link becomes ready\n",
3580 dev->name);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003581
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09003582 run_pending = 1;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003583 }
3584
Stephen Hemmingere21e8462010-03-20 16:09:01 -07003585 switch (dev->type) {
Amerigo Wang07a93622012-10-29 16:23:10 +00003586#if IS_ENABLED(CONFIG_IPV6_SIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587 case ARPHRD_SIT:
3588 addrconf_sit_config(dev);
3589 break;
Joerg Roedel0be669b2006-10-10 14:49:53 -07003590#endif
Amerigo Wang07a93622012-10-29 16:23:10 +00003591#if IS_ENABLED(CONFIG_NET_IPGRE)
stephen hemmingeraee80b52011-06-08 10:44:30 +00003592 case ARPHRD_IPGRE:
3593 addrconf_gre_config(dev);
3594 break;
3595#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596 case ARPHRD_LOOPBACK:
3597 init_loopback(dev);
3598 break;
3599
3600 default:
3601 addrconf_dev_config(dev);
3602 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07003603 }
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003604
WANG Conga317a2f2014-07-25 15:25:09 -07003605 if (!IS_ERR_OR_NULL(idev)) {
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09003606 if (run_pending)
Hangbin Liu896585d2018-11-21 21:52:33 +08003607 addrconf_dad_run(idev, false);
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09003608
Ido Schimmel2127d952018-01-07 12:45:03 +02003609 /* Device has an address by now */
3610 rt6_sync_up(dev, RTNH_F_DEAD);
3611
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003612 /*
3613 * If the MTU changed during the interface down,
3614 * when the interface up, the changed MTU must be
3615 * reflected in the idev as well as routers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616 */
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003617 if (idev->cnf.mtu6 != dev->mtu &&
3618 dev->mtu >= IPV6_MIN_MTU) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619 rt6_mtu_change(dev, dev->mtu);
3620 idev->cnf.mtu6 = dev->mtu;
3621 }
3622 idev->tstamp = jiffies;
3623 inet6_ifinfo_notify(RTM_NEWLINK, idev);
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003624
3625 /*
3626 * If the changed mtu during down is lower than
3627 * IPV6_MIN_MTU stop IPv6 on this interface.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628 */
3629 if (dev->mtu < IPV6_MIN_MTU)
WANG Cong60abc0b2017-06-21 14:34:58 -07003630 addrconf_ifdown(dev, dev != net->loopback_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631 }
3632 break;
3633
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634 case NETDEV_DOWN:
3635 case NETDEV_UNREGISTER:
3636 /*
3637 * Remove all addresses from this interface.
3638 */
3639 addrconf_ifdown(dev, event != NETDEV_DOWN);
3640 break;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003641
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642 case NETDEV_CHANGENAME:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643 if (idev) {
Stephen Hemminger5632c512007-04-28 21:16:39 -07003644 snmp6_unregister_dev(idev);
Pavel Emelyanov408c4762008-01-10 17:41:21 -08003645 addrconf_sysctl_unregister(idev);
WANG Conga317a2f2014-07-25 15:25:09 -07003646 err = addrconf_sysctl_register(idev);
Herbert Xub217d612007-07-30 17:04:52 -07003647 if (err)
3648 return notifier_from_errno(err);
WANG Conga317a2f2014-07-25 15:25:09 -07003649 err = snmp6_register_dev(idev);
3650 if (err) {
3651 addrconf_sysctl_unregister(idev);
3652 return notifier_from_errno(err);
3653 }
Stephen Hemminger5632c512007-04-28 21:16:39 -07003654 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655 break;
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003656
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00003657 case NETDEV_PRE_TYPE_CHANGE:
3658 case NETDEV_POST_TYPE_CHANGE:
Andrew Lunn3ef09522015-12-03 21:12:32 +01003659 if (idev)
3660 addrconf_type_change(dev, event);
Moni Shoua75c78502009-09-15 02:37:40 -07003661 break;
David Ahern4f7f34e2016-04-07 11:10:41 -07003662
3663 case NETDEV_CHANGEUPPER:
3664 info = ptr;
3665
3666 /* flush all routes if dev is linked to or unlinked from
3667 * an L3 master device (e.g., VRF)
3668 */
3669 if (info->upper_dev && netif_is_l3_master(info->upper_dev))
3670 addrconf_ifdown(dev, 0);
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07003671 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672
3673 return NOTIFY_OK;
3674}
3675
3676/*
3677 * addrconf module should be notified of a device going up
3678 */
3679static struct notifier_block ipv6_dev_notf = {
3680 .notifier_call = addrconf_notify,
WANG Cong242d3a42017-05-08 10:12:13 -07003681 .priority = ADDRCONF_NOTIFY_PRIORITY,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682};
3683
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00003684static void addrconf_type_change(struct net_device *dev, unsigned long event)
Moni Shoua75c78502009-09-15 02:37:40 -07003685{
3686 struct inet6_dev *idev;
3687 ASSERT_RTNL();
3688
3689 idev = __in6_dev_get(dev);
3690
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00003691 if (event == NETDEV_POST_TYPE_CHANGE)
Moni Shoua75c78502009-09-15 02:37:40 -07003692 ipv6_mc_remap(idev);
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00003693 else if (event == NETDEV_PRE_TYPE_CHANGE)
Moni Shoua75c78502009-09-15 02:37:40 -07003694 ipv6_mc_unmap(idev);
3695}
3696
David Ahern70af9212016-04-08 12:01:21 -07003697static bool addr_is_local(const struct in6_addr *addr)
3698{
3699 return ipv6_addr_type(addr) &
3700 (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK);
3701}
3702
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703static int addrconf_ifdown(struct net_device *dev, int how)
3704{
Ido Schimmel4c981e22018-01-07 12:45:04 +02003705 unsigned long event = how ? NETDEV_UNREGISTER : NETDEV_DOWN;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09003706 struct net *net = dev_net(dev);
stephen hemminger502a2ff2010-03-17 20:31:13 +00003707 struct inet6_dev *idev;
David Ahernf1705ec2016-02-24 09:25:37 -08003708 struct inet6_ifaddr *ifa, *tmp;
Ivan Vecera0aef78a2018-04-24 19:51:29 +02003709 bool keep_addr = false;
David S. Miller73a8bd72011-01-23 23:27:15 -08003710 int state, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711
3712 ASSERT_RTNL();
3713
Ido Schimmel4c981e22018-01-07 12:45:04 +02003714 rt6_disable_ip(dev, event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715
3716 idev = __in6_dev_get(dev);
Ian Morris63159f22015-03-29 14:00:04 +01003717 if (!idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718 return -ENODEV;
3719
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003720 /*
3721 * Step 1: remove reference to ipv6 device from parent device.
3722 * Do not dev_put!
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723 */
Denis V. Lunev439e2382008-04-03 13:30:17 -07003724 if (how) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725 idev->dead = 1;
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07003726
3727 /* protected by rtnl_lock */
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00003728 RCU_INIT_POINTER(dev->ip6_ptr, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729
3730 /* Step 1.5: remove snmp6 entry */
3731 snmp6_unregister_dev(idev);
3732
3733 }
3734
David Ahernf1705ec2016-02-24 09:25:37 -08003735 /* combine the user config with event to determine if permanent
3736 * addresses are to be removed from address hash table
3737 */
Ivan Vecera0aef78a2018-04-24 19:51:29 +02003738 if (!how && !idev->cnf.disable_ipv6) {
3739 /* aggregate the system setting and interface setting */
3740 int _keep_addr = net->ipv6.devconf_all->keep_addr_on_down;
3741
3742 if (!_keep_addr)
3743 _keep_addr = idev->cnf.keep_addr_on_down;
3744
3745 keep_addr = (_keep_addr > 0);
3746 }
David Ahernf1705ec2016-02-24 09:25:37 -08003747
David S. Miller73a8bd72011-01-23 23:27:15 -08003748 /* Step 2: clear hash table */
3749 for (i = 0; i < IN6_ADDR_HSIZE; i++) {
3750 struct hlist_head *h = &inet6_addr_lst[i];
David S. Miller73a8bd72011-01-23 23:27:15 -08003751
3752 spin_lock_bh(&addrconf_hash_lock);
Ian Morris67ba4152014-08-24 21:53:10 +01003753restart:
Sasha Levinb67bfe02013-02-27 17:06:00 -08003754 hlist_for_each_entry_rcu(ifa, h, addr_lst) {
David S. Miller73a8bd72011-01-23 23:27:15 -08003755 if (ifa->idev == idev) {
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003756 addrconf_del_dad_work(ifa);
David Ahernf1705ec2016-02-24 09:25:37 -08003757 /* combined flag + permanent flag decide if
3758 * address is retained on a down event
3759 */
3760 if (!keep_addr ||
David Ahern70af9212016-04-08 12:01:21 -07003761 !(ifa->flags & IFA_F_PERMANENT) ||
3762 addr_is_local(&ifa->addr)) {
David Ahernf1705ec2016-02-24 09:25:37 -08003763 hlist_del_init_rcu(&ifa->addr_lst);
3764 goto restart;
3765 }
David S. Miller73a8bd72011-01-23 23:27:15 -08003766 }
3767 }
3768 spin_unlock_bh(&addrconf_hash_lock);
3769 }
3770
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771 write_lock_bh(&idev->lock);
3772
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003773 addrconf_del_rs_timer(idev);
3774
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003775 /* Step 2: clear flags for stateless addrconf */
Denis V. Lunev439e2382008-04-03 13:30:17 -07003776 if (!how)
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003777 idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003779 /* Step 3: clear tempaddr list */
stephen hemminger372e6c82010-03-17 20:31:09 +00003780 while (!list_empty(&idev->tempaddr_list)) {
3781 ifa = list_first_entry(&idev->tempaddr_list,
3782 struct inet6_ifaddr, tmp_list);
3783 list_del(&ifa->tmp_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784 write_unlock_bh(&idev->lock);
3785 spin_lock_bh(&ifa->lock);
3786
3787 if (ifa->ifpub) {
3788 in6_ifa_put(ifa->ifpub);
3789 ifa->ifpub = NULL;
3790 }
3791 spin_unlock_bh(&ifa->lock);
3792 in6_ifa_put(ifa);
3793 write_lock_bh(&idev->lock);
3794 }
stephen hemminger8f37ada2010-03-03 08:19:59 +00003795
David Ahernf1705ec2016-02-24 09:25:37 -08003796 list_for_each_entry_safe(ifa, tmp, &idev->addr_list, if_list) {
David Ahern8d1c8022018-04-17 17:33:26 -07003797 struct fib6_info *rt = NULL;
Rabin Vincenta2d6cbb2017-04-10 08:36:39 +02003798 bool keep;
David Ahern38bd10c2016-04-21 20:56:12 -07003799
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003800 addrconf_del_dad_work(ifa);
stephen hemminger84e8b802010-03-02 13:32:46 +00003801
Rabin Vincenta2d6cbb2017-04-10 08:36:39 +02003802 keep = keep_addr && (ifa->flags & IFA_F_PERMANENT) &&
3803 !addr_is_local(&ifa->addr);
Rabin Vincenta2d6cbb2017-04-10 08:36:39 +02003804
David S. Miller73a8bd72011-01-23 23:27:15 -08003805 write_unlock_bh(&idev->lock);
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01003806 spin_lock_bh(&ifa->lock);
David Ahernf1705ec2016-02-24 09:25:37 -08003807
Rabin Vincenta2d6cbb2017-04-10 08:36:39 +02003808 if (keep) {
David Ahernf1705ec2016-02-24 09:25:37 -08003809 /* set state to skip the notifier below */
3810 state = INET6_IFADDR_STATE_DEAD;
David Ahern6d717132017-05-02 14:43:44 -07003811 ifa->state = INET6_IFADDR_STATE_PREDAD;
David Ahernf1705ec2016-02-24 09:25:37 -08003812 if (!(ifa->flags & IFA_F_NODAD))
3813 ifa->flags |= IFA_F_TENTATIVE;
David Ahern38bd10c2016-04-21 20:56:12 -07003814
3815 rt = ifa->rt;
3816 ifa->rt = NULL;
David Ahernf1705ec2016-02-24 09:25:37 -08003817 } else {
3818 state = ifa->state;
3819 ifa->state = INET6_IFADDR_STATE_DEAD;
David Ahernf1705ec2016-02-24 09:25:37 -08003820 }
3821
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01003822 spin_unlock_bh(&ifa->lock);
stephen hemminger84e8b802010-03-02 13:32:46 +00003823
David Ahern38bd10c2016-04-21 20:56:12 -07003824 if (rt)
David Ahernafb1d4b52018-04-17 17:33:11 -07003825 ip6_del_rt(net, rt);
David Ahern38bd10c2016-04-21 20:56:12 -07003826
David S. Miller73a8bd72011-01-23 23:27:15 -08003827 if (state != INET6_IFADDR_STATE_DEAD) {
3828 __ipv6_ifa_notify(RTM_DELADDR, ifa);
Cong Wangf88c91d2013-04-14 23:18:43 +08003829 inet6addr_notifier_call_chain(NETDEV_DOWN, ifa);
Mike Manningea06f712016-07-22 18:32:11 +01003830 } else {
3831 if (idev->cnf.forwarding)
3832 addrconf_leave_anycast(ifa);
3833 addrconf_leave_solict(ifa->idev, &ifa->addr);
stephen hemminger27bdb2a2010-04-12 05:41:32 +00003834 }
stephen hemminger8f37ada2010-03-03 08:19:59 +00003835
David S. Miller73a8bd72011-01-23 23:27:15 -08003836 write_lock_bh(&idev->lock);
Eric Dumazet8ef802a2017-10-07 19:30:23 -07003837 if (!keep) {
3838 list_del_rcu(&ifa->if_list);
3839 in6_ifa_put(ifa);
3840 }
David S. Miller73a8bd72011-01-23 23:27:15 -08003841 }
stephen hemminger8f37ada2010-03-03 08:19:59 +00003842
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843 write_unlock_bh(&idev->lock);
3844
Sabrina Dubroca381f4dc2014-09-10 23:23:02 +02003845 /* Step 5: Discard anycast and multicast list */
3846 if (how) {
3847 ipv6_ac_destroy_dev(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848 ipv6_mc_destroy_dev(idev);
Sabrina Dubroca381f4dc2014-09-10 23:23:02 +02003849 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850 ipv6_mc_down(idev);
Sabrina Dubroca381f4dc2014-09-10 23:23:02 +02003851 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853 idev->tstamp = jiffies;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003854
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003855 /* Last: Shot the device (if unregistered) */
Denis V. Lunev439e2382008-04-03 13:30:17 -07003856 if (how) {
Pavel Emelyanov408c4762008-01-10 17:41:21 -08003857 addrconf_sysctl_unregister(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858 neigh_parms_release(&nd_tbl, idev->nd_parms);
3859 neigh_ifdown(&nd_tbl, dev);
3860 in6_dev_put(idev);
3861 }
3862 return 0;
3863}
3864
Kees Cooke99e88a2017-10-16 14:43:17 -07003865static void addrconf_rs_timer(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866{
Kees Cooke99e88a2017-10-16 14:43:17 -07003867 struct inet6_dev *idev = from_timer(idev, t, rs_timer);
Cong Wangcaf92bc2013-08-31 13:44:32 +08003868 struct net_device *dev = idev->dev;
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003869 struct in6_addr lladdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003871 write_lock(&idev->lock);
stephen hemminger5b2a1952010-03-02 13:32:45 +00003872 if (idev->dead || !(idev->if_flags & IF_READY))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873 goto out;
3874
Shmulik Ladkani9ba2add2012-12-02 01:44:53 +00003875 if (!ipv6_accept_ra(idev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876 goto out;
stephen hemminger5b2a1952010-03-02 13:32:45 +00003877
3878 /* Announcement received after solicitation was sent */
3879 if (idev->if_flags & IF_RA_RCVD)
3880 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07003882 if (idev->rs_probes++ < idev->cnf.rtr_solicits || idev->cnf.rtr_solicits < 0) {
Cong Wangcaf92bc2013-08-31 13:44:32 +08003883 write_unlock(&idev->lock);
3884 if (!ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
3885 ndisc_send_rs(dev, &lladdr,
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003886 &in6addr_linklocal_allrouters);
3887 else
Cong Wangcaf92bc2013-08-31 13:44:32 +08003888 goto put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889
Cong Wangcaf92bc2013-08-31 13:44:32 +08003890 write_lock(&idev->lock);
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07003891 idev->rs_interval = rfc3315_s14_backoff_update(
3892 idev->rs_interval, idev->cnf.rtr_solicit_max_interval);
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003893 /* The wait after the last probe can be shorter */
3894 addrconf_mod_rs_timer(idev, (idev->rs_probes ==
3895 idev->cnf.rtr_solicits) ?
3896 idev->cnf.rtr_solicit_delay :
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07003897 idev->rs_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003898 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899 /*
3900 * Note: we do not support deprecated "all on-link"
3901 * assumption any longer.
3902 */
Joe Perches91df42b2012-05-15 14:11:54 +00003903 pr_debug("%s: no IPv6 routers present\n", idev->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904 }
3905
3906out:
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003907 write_unlock(&idev->lock);
Cong Wangcaf92bc2013-08-31 13:44:32 +08003908put:
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003909 in6_dev_put(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910}
3911
3912/*
3913 * Duplicate Address Detection
3914 */
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003915static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
3916{
3917 unsigned long rand_num;
3918 struct inet6_dev *idev = ifp->idev;
Erik Nordmarkadc176c2016-12-02 14:00:08 -08003919 u64 nonce;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003920
Neil Horman95c385b2007-04-25 17:08:10 -07003921 if (ifp->flags & IFA_F_OPTIMISTIC)
3922 rand_num = 0;
3923 else
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05003924 rand_num = prandom_u32() % (idev->cnf.rtr_solicit_delay ? : 1);
Neil Horman95c385b2007-04-25 17:08:10 -07003925
Erik Nordmarkadc176c2016-12-02 14:00:08 -08003926 nonce = 0;
3927 if (idev->cnf.enhanced_dad ||
3928 dev_net(idev->dev)->ipv6.devconf_all->enhanced_dad) {
3929 do
3930 get_random_bytes(&nonce, 6);
3931 while (nonce == 0);
3932 }
3933 ifp->dad_nonce = nonce;
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003934 ifp->dad_probes = idev->cnf.dad_transmits;
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003935 addrconf_mod_dad_work(ifp, rand_num);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003936}
3937
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003938static void addrconf_dad_begin(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939{
3940 struct inet6_dev *idev = ifp->idev;
3941 struct net_device *dev = idev->dev;
Paolo Abeni764d3be2016-11-22 16:57:40 +01003942 bool bump_id, notify = false;
David Ahernafb1d4b52018-04-17 17:33:11 -07003943 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944
3945 addrconf_join_solict(dev, &ifp->addr);
3946
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05003947 prandom_seed((__force u32) ifp->addr.s6_addr32[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948
3949 read_lock_bh(&idev->lock);
Herbert Xu622ccdf2010-05-18 15:56:06 -07003950 spin_lock(&ifp->lock);
Herbert Xue9d3e082010-05-18 15:36:06 -07003951 if (ifp->state == INET6_IFADDR_STATE_DEAD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953
David Ahernafb1d4b52018-04-17 17:33:11 -07003954 net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955 if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
David Ahernafb1d4b52018-04-17 17:33:11 -07003956 (net->ipv6.devconf_all->accept_dad < 1 &&
Matteo Crocea2d3f3e2017-10-05 19:03:05 +02003957 idev->cnf.accept_dad < 1) ||
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003958 !(ifp->flags&IFA_F_TENTATIVE) ||
3959 ifp->flags & IFA_F_NODAD) {
David Ahernc76fe2d2018-01-25 20:16:29 -08003960 bool send_na = false;
3961
3962 if (ifp->flags & IFA_F_TENTATIVE &&
3963 !(ifp->flags & IFA_F_OPTIMISTIC))
3964 send_na = true;
Paolo Abeni764d3be2016-11-22 16:57:40 +01003965 bump_id = ifp->flags & IFA_F_TENTATIVE;
Brian Haleycc411d02009-09-09 14:41:32 +00003966 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
stephen hemminger21809fa2010-02-08 19:48:52 +00003967 spin_unlock(&ifp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968 read_unlock_bh(&idev->lock);
3969
David Ahernc76fe2d2018-01-25 20:16:29 -08003970 addrconf_dad_completed(ifp, bump_id, send_na);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971 return;
3972 }
3973
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08003974 if (!(idev->if_flags & IF_READY)) {
stephen hemminger21809fa2010-02-08 19:48:52 +00003975 spin_unlock(&ifp->lock);
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08003976 read_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003977 /*
Masatake YAMATO590a9882009-06-09 10:41:12 +09003978 * If the device is not ready:
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003979 * - keep it tentative if it is a permanent address.
3980 * - otherwise, kill it.
3981 */
3982 in6_ifa_hold(ifp);
Brian Haleycc411d02009-09-09 14:41:32 +00003983 addrconf_dad_stop(ifp, 0);
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08003984 return;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003985 }
Neil Horman95c385b2007-04-25 17:08:10 -07003986
3987 /*
3988 * Optimistic nodes can start receiving
3989 * Frames right away
3990 */
Erik Kline7fd25612014-10-28 18:11:14 +09003991 if (ifp->flags & IFA_F_OPTIMISTIC) {
David Ahernafb1d4b52018-04-17 17:33:11 -07003992 ip6_ins_rt(net, ifp->rt);
3993 if (ipv6_use_optimistic_addr(net, idev)) {
Erik Kline7fd25612014-10-28 18:11:14 +09003994 /* Because optimistic nodes can use this address,
3995 * notify listeners. If DAD fails, RTM_DELADDR is sent.
3996 */
subashab@codeaurora.org16186a82016-02-02 02:11:10 +00003997 notify = true;
Erik Kline7fd25612014-10-28 18:11:14 +09003998 }
3999 }
Neil Horman95c385b2007-04-25 17:08:10 -07004000
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08004001 addrconf_dad_kick(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002out:
Herbert Xu622ccdf2010-05-18 15:56:06 -07004003 spin_unlock(&ifp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004 read_unlock_bh(&idev->lock);
subashab@codeaurora.org16186a82016-02-02 02:11:10 +00004005 if (notify)
4006 ipv6_ifa_notify(RTM_NEWADDR, ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007}
4008
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004009static void addrconf_dad_start(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010{
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004011 bool begin_dad = false;
4012
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01004013 spin_lock_bh(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004014 if (ifp->state != INET6_IFADDR_STATE_DEAD) {
4015 ifp->state = INET6_IFADDR_STATE_PREDAD;
4016 begin_dad = true;
4017 }
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01004018 spin_unlock_bh(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004019
4020 if (begin_dad)
4021 addrconf_mod_dad_work(ifp, 0);
4022}
4023
4024static void addrconf_dad_work(struct work_struct *w)
4025{
4026 struct inet6_ifaddr *ifp = container_of(to_delayed_work(w),
4027 struct inet6_ifaddr,
4028 dad_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029 struct inet6_dev *idev = ifp->idev;
Paolo Abeni764d3be2016-11-22 16:57:40 +01004030 bool bump_id, disable_ipv6 = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031 struct in6_addr mcaddr;
4032
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004033 enum {
4034 DAD_PROCESS,
4035 DAD_BEGIN,
4036 DAD_ABORT,
4037 } action = DAD_PROCESS;
4038
4039 rtnl_lock();
4040
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01004041 spin_lock_bh(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004042 if (ifp->state == INET6_IFADDR_STATE_PREDAD) {
4043 action = DAD_BEGIN;
4044 ifp->state = INET6_IFADDR_STATE_DAD;
4045 } else if (ifp->state == INET6_IFADDR_STATE_ERRDAD) {
4046 action = DAD_ABORT;
4047 ifp->state = INET6_IFADDR_STATE_POSTDAD;
Mike Manning85b51b12016-08-18 14:39:40 +01004048
Matteo Croce35e015e2017-09-12 17:46:37 +02004049 if ((dev_net(idev->dev)->ipv6.devconf_all->accept_dad > 1 ||
4050 idev->cnf.accept_dad > 1) &&
4051 !idev->cnf.disable_ipv6 &&
Mike Manning85b51b12016-08-18 14:39:40 +01004052 !(ifp->flags & IFA_F_STABLE_PRIVACY)) {
4053 struct in6_addr addr;
4054
4055 addr.s6_addr32[0] = htonl(0xfe800000);
4056 addr.s6_addr32[1] = 0;
4057
4058 if (!ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) &&
4059 ipv6_addr_equal(&ifp->addr, &addr)) {
4060 /* DAD failed for link-local based on MAC */
4061 idev->cnf.disable_ipv6 = 1;
4062
4063 pr_info("%s: IPv6 being disabled!\n",
4064 ifp->idev->dev->name);
4065 disable_ipv6 = true;
4066 }
4067 }
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004068 }
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01004069 spin_unlock_bh(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004070
4071 if (action == DAD_BEGIN) {
4072 addrconf_dad_begin(ifp);
4073 goto out;
4074 } else if (action == DAD_ABORT) {
Wei Yongjun751eb6b2016-09-05 16:06:31 +08004075 in6_ifa_hold(ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004076 addrconf_dad_stop(ifp, 1);
Mike Manning85b51b12016-08-18 14:39:40 +01004077 if (disable_ipv6)
4078 addrconf_ifdown(idev->dev, 0);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004079 goto out;
4080 }
4081
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02004082 if (!ifp->dad_probes && addrconf_dad_end(ifp))
Herbert Xuf2344a12010-05-18 15:55:27 -07004083 goto out;
4084
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004085 write_lock_bh(&idev->lock);
stephen hemminger122e4512010-03-02 13:32:44 +00004086 if (idev->dead || !(idev->if_flags & IF_READY)) {
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004087 write_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088 goto out;
4089 }
stephen hemminger21809fa2010-02-08 19:48:52 +00004090
4091 spin_lock(&ifp->lock);
Herbert Xu622ccdf2010-05-18 15:56:06 -07004092 if (ifp->state == INET6_IFADDR_STATE_DEAD) {
4093 spin_unlock(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004094 write_unlock_bh(&idev->lock);
Herbert Xu622ccdf2010-05-18 15:56:06 -07004095 goto out;
4096 }
4097
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02004098 if (ifp->dad_probes == 0) {
David Ahernc76fe2d2018-01-25 20:16:29 -08004099 bool send_na = false;
4100
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101 /*
4102 * DAD was successful
4103 */
4104
David Ahernc76fe2d2018-01-25 20:16:29 -08004105 if (ifp->flags & IFA_F_TENTATIVE &&
4106 !(ifp->flags & IFA_F_OPTIMISTIC))
4107 send_na = true;
Paolo Abeni764d3be2016-11-22 16:57:40 +01004108 bump_id = ifp->flags & IFA_F_TENTATIVE;
Brian Haleycc411d02009-09-09 14:41:32 +00004109 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
stephen hemminger21809fa2010-02-08 19:48:52 +00004110 spin_unlock(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004111 write_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112
David Ahernc76fe2d2018-01-25 20:16:29 -08004113 addrconf_dad_completed(ifp, bump_id, send_na);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114
4115 goto out;
4116 }
4117
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02004118 ifp->dad_probes--;
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004119 addrconf_mod_dad_work(ifp,
4120 NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME));
stephen hemminger21809fa2010-02-08 19:48:52 +00004121 spin_unlock(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004122 write_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123
4124 /* send a neighbour solicitation for our addr */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125 addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
Erik Nordmarkadc176c2016-12-02 14:00:08 -08004126 ndisc_send_ns(ifp->idev->dev, &ifp->addr, &mcaddr, &in6addr_any,
4127 ifp->dad_nonce);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128out:
4129 in6_ifa_put(ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004130 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131}
4132
Hannes Frederic Sowa11ffff72014-01-16 20:13:04 +01004133/* ifp->idev must be at least read locked */
4134static bool ipv6_lonely_lladdr(struct inet6_ifaddr *ifp)
4135{
4136 struct inet6_ifaddr *ifpiter;
4137 struct inet6_dev *idev = ifp->idev;
4138
Hannes Frederic Sowa602582c2014-01-19 21:58:19 +01004139 list_for_each_entry_reverse(ifpiter, &idev->addr_list, if_list) {
4140 if (ifpiter->scope > IFA_LINK)
4141 break;
Hannes Frederic Sowa11ffff72014-01-16 20:13:04 +01004142 if (ifp != ifpiter && ifpiter->scope == IFA_LINK &&
4143 (ifpiter->flags & (IFA_F_PERMANENT|IFA_F_TENTATIVE|
4144 IFA_F_OPTIMISTIC|IFA_F_DADFAILED)) ==
4145 IFA_F_PERMANENT)
4146 return false;
4147 }
4148 return true;
4149}
4150
David Ahernc76fe2d2018-01-25 20:16:29 -08004151static void addrconf_dad_completed(struct inet6_ifaddr *ifp, bool bump_id,
4152 bool send_na)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153{
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004154 struct net_device *dev = ifp->idev->dev;
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02004155 struct in6_addr lladdr;
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02004156 bool send_rs, send_mld;
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02004157
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004158 addrconf_del_dad_work(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159
4160 /*
4161 * Configure the address for reception. Now it is valid.
4162 */
4163
4164 ipv6_ifa_notify(RTM_NEWADDR, ifp);
4165
Tore Anderson026359b2011-08-28 23:47:33 +00004166 /* If added prefix is link local and we are prepared to process
4167 router advertisements, start sending router solicitations.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168 */
4169
Hannes Frederic Sowa1ec047e2013-06-27 00:06:56 +02004170 read_lock_bh(&ifp->idev->lock);
Hannes Frederic Sowa11ffff72014-01-16 20:13:04 +01004171 send_mld = ifp->scope == IFA_LINK && ipv6_lonely_lladdr(ifp);
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02004172 send_rs = send_mld &&
4173 ipv6_accept_ra(ifp->idev) &&
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07004174 ifp->idev->cnf.rtr_solicits != 0 &&
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02004175 (dev->flags&IFF_LOOPBACK) == 0;
Hannes Frederic Sowa1ec047e2013-06-27 00:06:56 +02004176 read_unlock_bh(&ifp->idev->lock);
4177
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02004178 /* While dad is in progress mld report's source address is in6_addrany.
4179 * Resend with proper ll now.
4180 */
4181 if (send_mld)
4182 ipv6_mc_dad_complete(ifp->idev);
4183
David Ahernc76fe2d2018-01-25 20:16:29 -08004184 /* send unsolicited NA if enabled */
4185 if (send_na &&
4186 (ifp->idev->cnf.ndisc_notify ||
4187 dev_net(dev)->ipv6.devconf_all->ndisc_notify)) {
4188 ndisc_send_na(dev, &in6addr_linklocal_allnodes, &ifp->addr,
4189 /*router=*/ !!ifp->idev->cnf.forwarding,
4190 /*solicited=*/ false, /*override=*/ true,
4191 /*inc_opt=*/ true);
4192 }
4193
Hannes Frederic Sowa1ec047e2013-06-27 00:06:56 +02004194 if (send_rs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195 /*
4196 * If a host as already performed a random delay
4197 * [...] as part of DAD [...] there is no need
4198 * to delay again before sending the first RS
4199 */
Hannes Frederic Sowa1ec047e2013-06-27 00:06:56 +02004200 if (ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02004201 return;
Hannes Frederic Sowa1ec047e2013-06-27 00:06:56 +02004202 ndisc_send_rs(dev, &lladdr, &in6addr_linklocal_allrouters);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02004204 write_lock_bh(&ifp->idev->lock);
4205 spin_lock(&ifp->lock);
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07004206 ifp->idev->rs_interval = rfc3315_s14_backoff_init(
4207 ifp->idev->cnf.rtr_solicit_interval);
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02004208 ifp->idev->rs_probes = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209 ifp->idev->if_flags |= IF_RS_SENT;
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07004210 addrconf_mod_rs_timer(ifp->idev, ifp->idev->rs_interval);
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02004211 spin_unlock(&ifp->lock);
4212 write_unlock_bh(&ifp->idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213 }
Paolo Abeni764d3be2016-11-22 16:57:40 +01004214
4215 if (bump_id)
4216 rt_genid_bump_ipv6(dev_net(dev));
Marcus Huewea11a7f72017-02-06 18:34:56 +01004217
4218 /* Make sure that a new temporary address will be created
4219 * before this temporary address becomes deprecated.
4220 */
4221 if (ifp->flags & IFA_F_TEMPORARY)
4222 addrconf_verify_rtnl();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223}
4224
Hangbin Liu896585d2018-11-21 21:52:33 +08004225static void addrconf_dad_run(struct inet6_dev *idev, bool restart)
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004226{
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09004227 struct inet6_ifaddr *ifp;
4228
4229 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00004230 list_for_each_entry(ifp, &idev->addr_list, if_list) {
stephen hemminger21809fa2010-02-08 19:48:52 +00004231 spin_lock(&ifp->lock);
Hangbin Liu896585d2018-11-21 21:52:33 +08004232 if ((ifp->flags & IFA_F_TENTATIVE &&
4233 ifp->state == INET6_IFADDR_STATE_DAD) || restart) {
4234 if (restart)
4235 ifp->state = INET6_IFADDR_STATE_PREDAD;
Herbert Xuf2344a12010-05-18 15:55:27 -07004236 addrconf_dad_kick(ifp);
Hangbin Liu896585d2018-11-21 21:52:33 +08004237 }
stephen hemminger21809fa2010-02-08 19:48:52 +00004238 spin_unlock(&ifp->lock);
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09004239 }
4240 read_unlock_bh(&idev->lock);
4241}
4242
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243#ifdef CONFIG_PROC_FS
4244struct if6_iter_state {
Daniel Lezcano3c400902008-01-10 22:42:49 -08004245 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246 int bucket;
Mihai Maruseac1d578302012-01-03 23:31:35 +00004247 int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248};
4249
Mihai Maruseac1d578302012-01-03 23:31:35 +00004250static struct inet6_ifaddr *if6_get_first(struct seq_file *seq, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252 struct if6_iter_state *state = seq->private;
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09004253 struct net *net = seq_file_net(seq);
Eric Dumazeta5c1d982017-10-23 16:17:50 -07004254 struct inet6_ifaddr *ifa = NULL;
Mihai Maruseac1d578302012-01-03 23:31:35 +00004255 int p = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256
Mihai Maruseac1d578302012-01-03 23:31:35 +00004257 /* initial bucket if pos is 0 */
4258 if (pos == 0) {
4259 state->bucket = 0;
4260 state->offset = 0;
4261 }
4262
4263 for (; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
Eric Dumazeta5c1d982017-10-23 16:17:50 -07004264 hlist_for_each_entry_rcu(ifa, &inet6_addr_lst[state->bucket],
Mihai Maruseac1d578302012-01-03 23:31:35 +00004265 addr_lst) {
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00004266 if (!net_eq(dev_net(ifa->idev->dev), net))
4267 continue;
Mihai Maruseac1d578302012-01-03 23:31:35 +00004268 /* sync with offset */
4269 if (p < state->offset) {
4270 p++;
4271 continue;
4272 }
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00004273 return ifa;
Mihai Maruseac1d578302012-01-03 23:31:35 +00004274 }
4275
4276 /* prepare for next bucket */
4277 state->offset = 0;
4278 p = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279 }
stephen hemmingerc2e21292010-03-17 20:31:10 +00004280 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281}
4282
stephen hemmingerc2e21292010-03-17 20:31:10 +00004283static struct inet6_ifaddr *if6_get_next(struct seq_file *seq,
4284 struct inet6_ifaddr *ifa)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285{
4286 struct if6_iter_state *state = seq->private;
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09004287 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288
Eric Dumazeta5c1d982017-10-23 16:17:50 -07004289 hlist_for_each_entry_continue_rcu(ifa, addr_lst) {
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00004290 if (!net_eq(dev_net(ifa->idev->dev), net))
4291 continue;
Mihai Maruseac1d578302012-01-03 23:31:35 +00004292 state->offset++;
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00004293 return ifa;
Mihai Maruseac1d578302012-01-03 23:31:35 +00004294 }
stephen hemmingerc2e21292010-03-17 20:31:10 +00004295
Jeff Barnhill86f9bd12018-09-21 00:45:27 +00004296 state->offset = 0;
stephen hemmingerc2e21292010-03-17 20:31:10 +00004297 while (++state->bucket < IN6_ADDR_HSIZE) {
Eric Dumazeta5c1d982017-10-23 16:17:50 -07004298 hlist_for_each_entry_rcu(ifa,
stephen hemmingerc2e21292010-03-17 20:31:10 +00004299 &inet6_addr_lst[state->bucket], addr_lst) {
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00004300 if (!net_eq(dev_net(ifa->idev->dev), net))
4301 continue;
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00004302 return ifa;
Daniel Lezcano3c400902008-01-10 22:42:49 -08004303 }
4304 }
4305
stephen hemmingerc2e21292010-03-17 20:31:10 +00004306 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307}
4308
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
Eric Dumazeta5c1d982017-10-23 16:17:50 -07004310 __acquires(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311{
Eric Dumazeta5c1d982017-10-23 16:17:50 -07004312 rcu_read_lock();
Mihai Maruseac1d578302012-01-03 23:31:35 +00004313 return if6_get_first(seq, *pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314}
4315
4316static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
4317{
4318 struct inet6_ifaddr *ifa;
4319
4320 ifa = if6_get_next(seq, v);
4321 ++*pos;
4322 return ifa;
4323}
4324
4325static void if6_seq_stop(struct seq_file *seq, void *v)
Eric Dumazeta5c1d982017-10-23 16:17:50 -07004326 __releases(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327{
Eric Dumazeta5c1d982017-10-23 16:17:50 -07004328 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329}
4330
4331static int if6_seq_show(struct seq_file *seq, void *v)
4332{
4333 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
Jiri Pirko971a3512013-12-10 13:56:29 +01004334 seq_printf(seq, "%pi6 %02x %02x %02x %02x %8s\n",
Harvey Harrisonb0711952008-10-28 16:05:40 -07004335 &ifp->addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336 ifp->idev->dev->ifindex,
4337 ifp->prefix_len,
4338 ifp->scope,
Jiri Pirko971a3512013-12-10 13:56:29 +01004339 (u8) ifp->flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340 ifp->idev->dev->name);
4341 return 0;
4342}
4343
Philippe De Muyter56b3d972007-07-10 23:07:31 -07004344static const struct seq_operations if6_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345 .start = if6_seq_start,
4346 .next = if6_seq_next,
4347 .show = if6_seq_show,
4348 .stop = if6_seq_stop,
4349};
4350
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004351static int __net_init if6_proc_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352{
Christoph Hellwigc3506372018-04-10 19:42:55 +02004353 if (!proc_create_net("if_inet6", 0444, net->proc_net, &if6_seq_ops,
4354 sizeof(struct if6_iter_state)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355 return -ENOMEM;
4356 return 0;
4357}
4358
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004359static void __net_exit if6_proc_net_exit(struct net *net)
Daniel Lezcano3c400902008-01-10 22:42:49 -08004360{
Gao fengece31ff2013-02-18 01:34:56 +00004361 remove_proc_entry("if_inet6", net->proc_net);
Daniel Lezcano3c400902008-01-10 22:42:49 -08004362}
4363
4364static struct pernet_operations if6_proc_net_ops = {
Ian Morris67ba4152014-08-24 21:53:10 +01004365 .init = if6_proc_net_init,
4366 .exit = if6_proc_net_exit,
Daniel Lezcano3c400902008-01-10 22:42:49 -08004367};
4368
4369int __init if6_proc_init(void)
4370{
4371 return register_pernet_subsys(&if6_proc_net_ops);
4372}
4373
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374void if6_proc_exit(void)
4375{
Daniel Lezcano3c400902008-01-10 22:42:49 -08004376 unregister_pernet_subsys(&if6_proc_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377}
4378#endif /* CONFIG_PROC_FS */
4379
Amerigo Wang07a93622012-10-29 16:23:10 +00004380#if IS_ENABLED(CONFIG_IPV6_MIP6)
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07004381/* Check if address is a home address configured on any interface. */
Eric Dumazetb71d1d42011-04-22 04:53:02 +00004382int ipv6_chk_home_addr(struct net *net, const struct in6_addr *addr)
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07004383{
Eric Dumazet3f27fb22017-10-23 16:17:47 -07004384 unsigned int hash = inet6_addr_hash(net, addr);
stephen hemmingerc2e21292010-03-17 20:31:10 +00004385 struct inet6_ifaddr *ifp = NULL;
Eric Dumazet3f27fb22017-10-23 16:17:47 -07004386 int ret = 0;
stephen hemmingerc2e21292010-03-17 20:31:10 +00004387
Eric Dumazet4e5f47a2017-10-23 16:17:51 -07004388 rcu_read_lock();
4389 hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09004390 if (!net_eq(dev_net(ifp->idev->dev), net))
Daniel Lezcano389f6612008-01-10 22:44:40 -08004391 continue;
YOSHIFUJI Hideakicaad2952008-04-10 15:42:07 +09004392 if (ipv6_addr_equal(&ifp->addr, addr) &&
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07004393 (ifp->flags & IFA_F_HOMEADDRESS)) {
4394 ret = 1;
4395 break;
4396 }
4397 }
Eric Dumazet4e5f47a2017-10-23 16:17:51 -07004398 rcu_read_unlock();
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07004399 return ret;
4400}
4401#endif
4402
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403/*
4404 * Periodic address status verification
4405 */
4406
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004407static void addrconf_verify_rtnl(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408{
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -07004409 unsigned long now, next, next_sec, next_sched;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410 struct inet6_ifaddr *ifp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004411 int i;
4412
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004413 ASSERT_RTNL();
4414
stephen hemminger5c578aed2010-03-17 20:31:11 +00004415 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416 now = jiffies;
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -07004417 next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004419 cancel_delayed_work(&addr_chk_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07004421 for (i = 0; i < IN6_ADDR_HSIZE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422restart:
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004423 hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[i], addr_lst) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424 unsigned long age;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425
Yasushi Asanofad8da32013-12-31 12:04:19 +09004426 /* When setting preferred_lft to a value not zero or
4427 * infinity, while valid_lft is infinity
4428 * IFA_F_PERMANENT has a non-infinity life time.
4429 */
4430 if ((ifp->flags & IFA_F_PERMANENT) &&
4431 (ifp->prefered_lft == INFINITY_LIFE_TIME))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432 continue;
4433
4434 spin_lock(&ifp->lock);
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -07004435 /* We try to batch several events at once. */
4436 age = (now - ifp->tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437
YOSHIFUJI Hideaki8f27ebb2006-07-28 18:12:11 +09004438 if (ifp->valid_lft != INFINITY_LIFE_TIME &&
4439 age >= ifp->valid_lft) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440 spin_unlock(&ifp->lock);
4441 in6_ifa_hold(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442 ipv6_del_addr(ifp);
4443 goto restart;
YOSHIFUJI Hideaki8f27ebb2006-07-28 18:12:11 +09004444 } else if (ifp->prefered_lft == INFINITY_LIFE_TIME) {
4445 spin_unlock(&ifp->lock);
4446 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447 } else if (age >= ifp->prefered_lft) {
Brian Haleya1ed0522009-07-02 07:10:52 +00004448 /* jiffies - ifp->tstamp > age >= ifp->prefered_lft */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449 int deprecate = 0;
4450
4451 if (!(ifp->flags&IFA_F_DEPRECATED)) {
4452 deprecate = 1;
4453 ifp->flags |= IFA_F_DEPRECATED;
4454 }
4455
Yasushi Asanofad8da32013-12-31 12:04:19 +09004456 if ((ifp->valid_lft != INFINITY_LIFE_TIME) &&
4457 (time_before(ifp->tstamp + ifp->valid_lft * HZ, next)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004458 next = ifp->tstamp + ifp->valid_lft * HZ;
4459
4460 spin_unlock(&ifp->lock);
4461
4462 if (deprecate) {
4463 in6_ifa_hold(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004464
4465 ipv6_ifa_notify(0, ifp);
4466 in6_ifa_put(ifp);
4467 goto restart;
4468 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469 } else if ((ifp->flags&IFA_F_TEMPORARY) &&
4470 !(ifp->flags&IFA_F_TENTATIVE)) {
stephen hemminger88949cf2010-03-17 20:31:17 +00004471 unsigned long regen_advance = ifp->idev->cnf.regen_max_retry *
4472 ifp->idev->cnf.dad_transmits *
Jiri Pirko1f9248e2013-12-07 19:26:53 +01004473 NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME) / HZ;
stephen hemminger88949cf2010-03-17 20:31:17 +00004474
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475 if (age >= ifp->prefered_lft - regen_advance) {
4476 struct inet6_ifaddr *ifpub = ifp->ifpub;
4477 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
4478 next = ifp->tstamp + ifp->prefered_lft * HZ;
4479 if (!ifp->regen_count && ifpub) {
4480 ifp->regen_count++;
4481 in6_ifa_hold(ifp);
4482 in6_ifa_hold(ifpub);
4483 spin_unlock(&ifp->lock);
stephen hemminger5c578aed2010-03-17 20:31:11 +00004484
Hiroyuki YAMAMORI291d8092005-12-23 11:24:05 -08004485 spin_lock(&ifpub->lock);
4486 ifpub->regen_count = 0;
4487 spin_unlock(&ifpub->lock);
Eric Dumazete64e4692018-01-26 16:10:43 -08004488 rcu_read_unlock_bh();
Eric Dumazetfffcefe2017-11-06 14:13:29 -08004489 ipv6_create_tempaddr(ifpub, ifp, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490 in6_ifa_put(ifpub);
4491 in6_ifa_put(ifp);
Eric Dumazete64e4692018-01-26 16:10:43 -08004492 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493 goto restart;
4494 }
4495 } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
4496 next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
4497 spin_unlock(&ifp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498 } else {
4499 /* ifp->prefered_lft <= ifp->valid_lft */
4500 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
4501 next = ifp->tstamp + ifp->prefered_lft * HZ;
4502 spin_unlock(&ifp->lock);
4503 }
4504 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505 }
4506
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -07004507 next_sec = round_jiffies_up(next);
4508 next_sched = next;
4509
4510 /* If rounded timeout is accurate enough, accept it. */
4511 if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ))
4512 next_sched = next_sec;
4513
4514 /* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */
4515 if (time_before(next_sched, jiffies + ADDRCONF_TIMER_FUZZ_MAX))
4516 next_sched = jiffies + ADDRCONF_TIMER_FUZZ_MAX;
4517
Joe Perchese32ac252018-03-26 08:35:01 -07004518 pr_debug("now = %lu, schedule = %lu, rounded schedule = %lu => %lu\n",
4519 now, next, next_sec, next_sched);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004520 mod_delayed_work(addrconf_wq, &addr_chk_work, next_sched - now);
stephen hemminger5c578aed2010-03-17 20:31:11 +00004521 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522}
4523
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004524static void addrconf_verify_work(struct work_struct *w)
4525{
4526 rtnl_lock();
4527 addrconf_verify_rtnl();
4528 rtnl_unlock();
4529}
4530
4531static void addrconf_verify(void)
4532{
4533 mod_delayed_work(addrconf_wq, &addr_chk_work, 0);
4534}
4535
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004536static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local,
4537 struct in6_addr **peer_pfx)
Thomas Graf461d8832006-09-18 00:09:49 -07004538{
4539 struct in6_addr *pfx = NULL;
4540
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004541 *peer_pfx = NULL;
4542
Thomas Graf461d8832006-09-18 00:09:49 -07004543 if (addr)
4544 pfx = nla_data(addr);
4545
4546 if (local) {
4547 if (pfx && nla_memcmp(local, pfx, sizeof(*pfx)))
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004548 *peer_pfx = pfx;
4549 pfx = nla_data(local);
Thomas Graf461d8832006-09-18 00:09:49 -07004550 }
4551
4552 return pfx;
4553}
4554
Patrick McHardyef7c79e2007-06-05 12:38:30 -07004555static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = {
Thomas Graf461d8832006-09-18 00:09:49 -07004556 [IFA_ADDRESS] = { .len = sizeof(struct in6_addr) },
4557 [IFA_LOCAL] = { .len = sizeof(struct in6_addr) },
4558 [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) },
Jiri Pirko479840f2013-12-06 09:45:21 +01004559 [IFA_FLAGS] = { .len = sizeof(u32) },
David Ahern8308f3f2018-05-27 08:09:58 -07004560 [IFA_RT_PRIORITY] = { .len = sizeof(u32) },
Christian Brauner6ecf4c32018-09-04 21:53:50 +02004561 [IFA_TARGET_NETNSID] = { .type = NLA_S32 },
Thomas Graf461d8832006-09-18 00:09:49 -07004562};
4563
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564static int
David Ahernc21ef3e2017-04-16 09:48:24 -07004565inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh,
4566 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09004568 struct net *net = sock_net(skb->sk);
Thomas Grafb933f712006-09-18 00:10:19 -07004569 struct ifaddrmsg *ifm;
4570 struct nlattr *tb[IFA_MAX+1];
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004571 struct in6_addr *pfx, *peer_pfx;
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02004572 u32 ifa_flags;
Thomas Grafb933f712006-09-18 00:10:19 -07004573 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574
Johannes Berg8cb08172019-04-26 14:07:28 +02004575 err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFA_MAX,
4576 ifa_ipv6_policy, extack);
Thomas Grafb933f712006-09-18 00:10:19 -07004577 if (err < 0)
4578 return err;
4579
4580 ifm = nlmsg_data(nlh);
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004581 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
Ian Morris63159f22015-03-29 14:00:04 +01004582 if (!pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583 return -EINVAL;
4584
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02004585 ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags;
4586
4587 /* We ignore other flags so far. */
4588 ifa_flags &= IFA_F_MANAGETEMPADDR;
4589
4590 return inet6_addr_del(net, ifm->ifa_index, ifa_flags, pfx,
4591 ifm->ifa_prefixlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592}
4593
David Ahern8308f3f2018-05-27 08:09:58 -07004594static int modify_prefix_route(struct inet6_ifaddr *ifp,
Hangbin Liu61794012020-03-03 14:37:34 +08004595 unsigned long expires, u32 flags,
4596 bool modify_peer)
David Ahern8308f3f2018-05-27 08:09:58 -07004597{
4598 struct fib6_info *f6i;
David Aherne7c7faa2018-06-28 13:36:55 -07004599 u32 prio;
David Ahern8308f3f2018-05-27 08:09:58 -07004600
Hangbin Liu61794012020-03-03 14:37:34 +08004601 f6i = addrconf_get_prefix_route(modify_peer ? &ifp->peer_addr : &ifp->addr,
4602 ifp->prefix_len,
David Ahern2b2450c2019-03-27 20:53:52 -07004603 ifp->idev->dev, 0, RTF_DEFAULT, true);
David Ahern8308f3f2018-05-27 08:09:58 -07004604 if (!f6i)
4605 return -ENOENT;
4606
David Aherne7c7faa2018-06-28 13:36:55 -07004607 prio = ifp->rt_priority ? : IP6_RT_PRIO_ADDRCONF;
4608 if (f6i->fib6_metric != prio) {
4609 /* delete old one */
4610 ip6_del_rt(dev_net(ifp->idev->dev), f6i);
4611
David Ahern8308f3f2018-05-27 08:09:58 -07004612 /* add new one */
Hangbin Liu61794012020-03-03 14:37:34 +08004613 addrconf_prefix_route(modify_peer ? &ifp->peer_addr : &ifp->addr,
4614 ifp->prefix_len,
David Ahern8308f3f2018-05-27 08:09:58 -07004615 ifp->rt_priority, ifp->idev->dev,
4616 expires, flags, GFP_KERNEL);
David Ahern8308f3f2018-05-27 08:09:58 -07004617 } else {
4618 if (!expires)
4619 fib6_clean_expires(f6i);
4620 else
4621 fib6_set_expires(f6i, expires);
4622
4623 fib6_info_release(f6i);
4624 }
4625
4626 return 0;
4627}
4628
David Ahernd169a1f2018-05-27 08:09:55 -07004629static int inet6_addr_modify(struct inet6_ifaddr *ifp, struct ifa6_config *cfg)
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004630{
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07004631 u32 flags;
4632 clock_t expires;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09004633 unsigned long timeout;
Jiri Pirko53bd6742013-12-06 09:45:22 +01004634 bool was_managetempaddr;
Thomas Haller5b84efe2014-01-15 15:36:59 +01004635 bool had_prefixroute;
Hangbin Liud0098e42020-03-03 14:37:35 +08004636 bool new_peer = false;
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09004637
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004638 ASSERT_RTNL();
4639
David Ahernd169a1f2018-05-27 08:09:55 -07004640 if (!cfg->valid_lft || cfg->preferred_lft > cfg->valid_lft)
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004641 return -EINVAL;
4642
David Ahernd169a1f2018-05-27 08:09:55 -07004643 if (cfg->ifa_flags & IFA_F_MANAGETEMPADDR &&
Jiri Pirko53bd6742013-12-06 09:45:22 +01004644 (ifp->flags & IFA_F_TEMPORARY || ifp->prefix_len != 64))
4645 return -EINVAL;
4646
Sabrina Dubrocaf1c02cf2018-02-28 16:40:08 +01004647 if (!(ifp->flags & IFA_F_TENTATIVE) || ifp->flags & IFA_F_DADFAILED)
David Ahernd169a1f2018-05-27 08:09:55 -07004648 cfg->ifa_flags &= ~IFA_F_OPTIMISTIC;
Sabrina Dubrocaf1c02cf2018-02-28 16:40:08 +01004649
David Ahernd169a1f2018-05-27 08:09:55 -07004650 timeout = addrconf_timeout_fixup(cfg->valid_lft, HZ);
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09004651 if (addrconf_finite_timeout(timeout)) {
4652 expires = jiffies_to_clock_t(timeout * HZ);
David Ahernd169a1f2018-05-27 08:09:55 -07004653 cfg->valid_lft = timeout;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07004654 flags = RTF_EXPIRES;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09004655 } else {
4656 expires = 0;
4657 flags = 0;
David Ahernd169a1f2018-05-27 08:09:55 -07004658 cfg->ifa_flags |= IFA_F_PERMANENT;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07004659 }
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004660
David Ahernd169a1f2018-05-27 08:09:55 -07004661 timeout = addrconf_timeout_fixup(cfg->preferred_lft, HZ);
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09004662 if (addrconf_finite_timeout(timeout)) {
4663 if (timeout == 0)
David Ahernd169a1f2018-05-27 08:09:55 -07004664 cfg->ifa_flags |= IFA_F_DEPRECATED;
4665 cfg->preferred_lft = timeout;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09004666 }
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004667
Hangbin Liud0098e42020-03-03 14:37:35 +08004668 if (cfg->peer_pfx &&
4669 memcmp(&ifp->peer_addr, cfg->peer_pfx, sizeof(struct in6_addr))) {
4670 if (!ipv6_addr_any(&ifp->peer_addr))
4671 cleanup_prefix_route(ifp, expires, true, true);
4672 new_peer = true;
4673 }
4674
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004675 spin_lock_bh(&ifp->lock);
Jiri Pirko53bd6742013-12-06 09:45:22 +01004676 was_managetempaddr = ifp->flags & IFA_F_MANAGETEMPADDR;
Thomas Haller5b84efe2014-01-15 15:36:59 +01004677 had_prefixroute = ifp->flags & IFA_F_PERMANENT &&
4678 !(ifp->flags & IFA_F_NOPREFIXROUTE);
Jiri Pirko53bd6742013-12-06 09:45:22 +01004679 ifp->flags &= ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD |
Thomas Haller761aac72014-01-15 15:36:58 +01004680 IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR |
4681 IFA_F_NOPREFIXROUTE);
David Ahernd169a1f2018-05-27 08:09:55 -07004682 ifp->flags |= cfg->ifa_flags;
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004683 ifp->tstamp = jiffies;
David Ahernd169a1f2018-05-27 08:09:55 -07004684 ifp->valid_lft = cfg->valid_lft;
4685 ifp->prefered_lft = cfg->preferred_lft;
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004686
David Ahern8308f3f2018-05-27 08:09:58 -07004687 if (cfg->rt_priority && cfg->rt_priority != ifp->rt_priority)
4688 ifp->rt_priority = cfg->rt_priority;
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004689
Hangbin Liud0098e42020-03-03 14:37:35 +08004690 if (new_peer)
4691 ifp->peer_addr = *cfg->peer_pfx;
4692
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004693 spin_unlock_bh(&ifp->lock);
4694 if (!(ifp->flags&IFA_F_TENTATIVE))
4695 ipv6_ifa_notify(0, ifp);
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004696
David Ahernd169a1f2018-05-27 08:09:55 -07004697 if (!(cfg->ifa_flags & IFA_F_NOPREFIXROUTE)) {
David Ahern8308f3f2018-05-27 08:09:58 -07004698 int rc = -ENOENT;
4699
4700 if (had_prefixroute)
Hangbin Liu61794012020-03-03 14:37:34 +08004701 rc = modify_prefix_route(ifp, expires, flags, false);
David Ahern8308f3f2018-05-27 08:09:58 -07004702
4703 /* prefix route could have been deleted; if so restore it */
4704 if (rc == -ENOENT) {
4705 addrconf_prefix_route(&ifp->addr, ifp->prefix_len,
4706 ifp->rt_priority, ifp->idev->dev,
4707 expires, flags, GFP_KERNEL);
4708 }
Hangbin Liu61794012020-03-03 14:37:34 +08004709
4710 if (had_prefixroute && !ipv6_addr_any(&ifp->peer_addr))
4711 rc = modify_prefix_route(ifp, expires, flags, true);
4712
4713 if (rc == -ENOENT && !ipv6_addr_any(&ifp->peer_addr)) {
4714 addrconf_prefix_route(&ifp->peer_addr, ifp->prefix_len,
4715 ifp->rt_priority, ifp->idev->dev,
4716 expires, flags, GFP_KERNEL);
4717 }
Thomas Haller5b84efe2014-01-15 15:36:59 +01004718 } else if (had_prefixroute) {
4719 enum cleanup_prefix_rt_t action;
4720 unsigned long rt_expires;
4721
4722 write_lock_bh(&ifp->idev->lock);
4723 action = check_cleanup_prefix_route(ifp, &rt_expires);
4724 write_unlock_bh(&ifp->idev->lock);
4725
4726 if (action != CLEANUP_PREFIX_RT_NOP) {
4727 cleanup_prefix_route(ifp, rt_expires,
Hangbin Liud0098e42020-03-03 14:37:35 +08004728 action == CLEANUP_PREFIX_RT_DEL, false);
Thomas Haller5b84efe2014-01-15 15:36:59 +01004729 }
Thomas Haller761aac72014-01-15 15:36:58 +01004730 }
Jiri Pirko53bd6742013-12-06 09:45:22 +01004731
4732 if (was_managetempaddr || ifp->flags & IFA_F_MANAGETEMPADDR) {
David Ahernd169a1f2018-05-27 08:09:55 -07004733 if (was_managetempaddr &&
4734 !(ifp->flags & IFA_F_MANAGETEMPADDR)) {
4735 cfg->valid_lft = 0;
4736 cfg->preferred_lft = 0;
4737 }
4738 manage_tempaddrs(ifp->idev, ifp, cfg->valid_lft,
4739 cfg->preferred_lft, !was_managetempaddr,
4740 jiffies);
Jiri Pirko53bd6742013-12-06 09:45:22 +01004741 }
4742
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004743 addrconf_verify_rtnl();
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004744
4745 return 0;
4746}
4747
4748static int
David Ahernc21ef3e2017-04-16 09:48:24 -07004749inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh,
4750 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004751{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09004752 struct net *net = sock_net(skb->sk);
Thomas Graf461d8832006-09-18 00:09:49 -07004753 struct ifaddrmsg *ifm;
4754 struct nlattr *tb[IFA_MAX+1];
David Ahern19b15182018-05-27 08:09:54 -07004755 struct in6_addr *peer_pfx;
Thomas Graf7198f8c2006-09-18 00:13:46 -07004756 struct inet6_ifaddr *ifa;
4757 struct net_device *dev;
Sabrina Dubrocaf1c02cf2018-02-28 16:40:08 +01004758 struct inet6_dev *idev;
David Ahern19b15182018-05-27 08:09:54 -07004759 struct ifa6_config cfg;
Thomas Graf461d8832006-09-18 00:09:49 -07004760 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004761
Johannes Berg8cb08172019-04-26 14:07:28 +02004762 err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFA_MAX,
4763 ifa_ipv6_policy, extack);
Thomas Graf461d8832006-09-18 00:09:49 -07004764 if (err < 0)
4765 return err;
4766
David Ahern19b15182018-05-27 08:09:54 -07004767 memset(&cfg, 0, sizeof(cfg));
4768
Thomas Graf461d8832006-09-18 00:09:49 -07004769 ifm = nlmsg_data(nlh);
David Ahern19b15182018-05-27 08:09:54 -07004770 cfg.pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
4771 if (!cfg.pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772 return -EINVAL;
4773
David Ahern19b15182018-05-27 08:09:54 -07004774 cfg.peer_pfx = peer_pfx;
4775 cfg.plen = ifm->ifa_prefixlen;
David Ahern8308f3f2018-05-27 08:09:58 -07004776 if (tb[IFA_RT_PRIORITY])
4777 cfg.rt_priority = nla_get_u32(tb[IFA_RT_PRIORITY]);
4778
David Ahern19b15182018-05-27 08:09:54 -07004779 cfg.valid_lft = INFINITY_LIFE_TIME;
4780 cfg.preferred_lft = INFINITY_LIFE_TIME;
4781
Thomas Graf461d8832006-09-18 00:09:49 -07004782 if (tb[IFA_CACHEINFO]) {
Noriaki TAKAMIYA0778769d2006-07-28 18:12:10 +09004783 struct ifa_cacheinfo *ci;
Thomas Graf461d8832006-09-18 00:09:49 -07004784
4785 ci = nla_data(tb[IFA_CACHEINFO]);
David Ahern19b15182018-05-27 08:09:54 -07004786 cfg.valid_lft = ci->ifa_valid;
4787 cfg.preferred_lft = ci->ifa_prefered;
Noriaki TAKAMIYA0778769d2006-07-28 18:12:10 +09004788 }
4789
Daniel Lezcanoaf284932008-03-05 10:46:57 -08004790 dev = __dev_get_by_index(net, ifm->ifa_index);
Ian Morris63159f22015-03-29 14:00:04 +01004791 if (!dev)
Thomas Graf7198f8c2006-09-18 00:13:46 -07004792 return -ENODEV;
4793
David Ahern19b15182018-05-27 08:09:54 -07004794 if (tb[IFA_FLAGS])
4795 cfg.ifa_flags = nla_get_u32(tb[IFA_FLAGS]);
4796 else
4797 cfg.ifa_flags = ifm->ifa_flags;
Jiri Pirko479840f2013-12-06 09:45:21 +01004798
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07004799 /* We ignore other flags so far. */
David Ahern19b15182018-05-27 08:09:54 -07004800 cfg.ifa_flags &= IFA_F_NODAD | IFA_F_HOMEADDRESS |
4801 IFA_F_MANAGETEMPADDR | IFA_F_NOPREFIXROUTE |
4802 IFA_F_MCAUTOJOIN | IFA_F_OPTIMISTIC;
Sabrina Dubrocaf1c02cf2018-02-28 16:40:08 +01004803
4804 idev = ipv6_find_idev(dev);
Sabrina Dubrocadb0b99f2019-08-23 15:44:36 +02004805 if (IS_ERR(idev))
4806 return PTR_ERR(idev);
Sabrina Dubrocaf1c02cf2018-02-28 16:40:08 +01004807
4808 if (!ipv6_allow_optimistic_dad(net, idev))
David Ahern19b15182018-05-27 08:09:54 -07004809 cfg.ifa_flags &= ~IFA_F_OPTIMISTIC;
Sabrina Dubrocaf1c02cf2018-02-28 16:40:08 +01004810
David Ahern19b15182018-05-27 08:09:54 -07004811 if (cfg.ifa_flags & IFA_F_NODAD &&
4812 cfg.ifa_flags & IFA_F_OPTIMISTIC) {
Sabrina Dubrocaf1c02cf2018-02-28 16:40:08 +01004813 NL_SET_ERR_MSG(extack, "IFA_F_NODAD and IFA_F_OPTIMISTIC are mutually exclusive");
4814 return -EINVAL;
4815 }
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07004816
David Ahern19b15182018-05-27 08:09:54 -07004817 ifa = ipv6_get_ifaddr(net, cfg.pfx, dev, 1);
Ian Morris63159f22015-03-29 14:00:04 +01004818 if (!ifa) {
Thomas Graf7198f8c2006-09-18 00:13:46 -07004819 /*
4820 * It would be best to check for !NLM_F_CREATE here but
stephen hemmingerdb9c7c32014-01-12 11:26:32 -08004821 * userspace already relies on not having to provide this.
Thomas Graf7198f8c2006-09-18 00:13:46 -07004822 */
David Ahern19b15182018-05-27 08:09:54 -07004823 return inet6_addr_add(net, ifm->ifa_index, &cfg, extack);
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004824 }
4825
Thomas Graf7198f8c2006-09-18 00:13:46 -07004826 if (nlh->nlmsg_flags & NLM_F_EXCL ||
4827 !(nlh->nlmsg_flags & NLM_F_REPLACE))
4828 err = -EEXIST;
4829 else
David Ahernd169a1f2018-05-27 08:09:55 -07004830 err = inet6_addr_modify(ifa, &cfg);
Thomas Graf7198f8c2006-09-18 00:13:46 -07004831
4832 in6_ifa_put(ifa);
4833
4834 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004835}
4836
Jiri Pirko479840f2013-12-06 09:45:21 +01004837static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u32 flags,
Thomas Graf101bb222006-09-18 00:11:52 -07004838 u8 scope, int ifindex)
4839{
4840 struct ifaddrmsg *ifm;
4841
4842 ifm = nlmsg_data(nlh);
4843 ifm->ifa_family = AF_INET6;
4844 ifm->ifa_prefixlen = prefixlen;
4845 ifm->ifa_flags = flags;
4846 ifm->ifa_scope = scope;
4847 ifm->ifa_index = ifindex;
4848}
4849
Thomas Graf85486af2006-09-18 00:11:24 -07004850static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
4851 unsigned long tstamp, u32 preferred, u32 valid)
4852{
4853 struct ifa_cacheinfo ci;
4854
Thomas Graf18a31e12010-11-17 04:12:02 +00004855 ci.cstamp = cstamp_delta(cstamp);
4856 ci.tstamp = cstamp_delta(tstamp);
Thomas Graf85486af2006-09-18 00:11:24 -07004857 ci.ifa_prefered = preferred;
4858 ci.ifa_valid = valid;
4859
4860 return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
4861}
4862
Thomas Graf101bb222006-09-18 00:11:52 -07004863static inline int rt_scope(int ifa_scope)
4864{
4865 if (ifa_scope & IFA_HOST)
4866 return RT_SCOPE_HOST;
4867 else if (ifa_scope & IFA_LINK)
4868 return RT_SCOPE_LINK;
4869 else if (ifa_scope & IFA_SITE)
4870 return RT_SCOPE_SITE;
4871 else
4872 return RT_SCOPE_UNIVERSE;
4873}
4874
Thomas Graf0ab68032006-09-18 00:12:35 -07004875static inline int inet6_ifaddr_msgsize(void)
4876{
Thomas Graf339bf982006-11-10 14:10:15 -08004877 return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004878 + nla_total_size(16) /* IFA_LOCAL */
Thomas Graf339bf982006-11-10 14:10:15 -08004879 + nla_total_size(16) /* IFA_ADDRESS */
Jiri Pirko479840f2013-12-06 09:45:21 +01004880 + nla_total_size(sizeof(struct ifa_cacheinfo))
David Ahern8308f3f2018-05-27 08:09:58 -07004881 + nla_total_size(4) /* IFA_FLAGS */
4882 + nla_total_size(4) /* IFA_RT_PRIORITY */;
Thomas Graf0ab68032006-09-18 00:12:35 -07004883}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07004884
David Ahern6ba1e6e2018-10-07 20:16:26 -07004885enum addr_type_t {
4886 UNICAST_ADDR,
4887 MULTICAST_ADDR,
4888 ANYCAST_ADDR,
4889};
4890
Christian Brauner203651b2018-09-04 21:53:55 +02004891struct inet6_fill_args {
4892 u32 portid;
4893 u32 seq;
4894 int event;
4895 unsigned int flags;
4896 int netnsid;
David Ahern6371a712018-10-19 12:45:30 -07004897 int ifindex;
David Ahern6ba1e6e2018-10-07 20:16:26 -07004898 enum addr_type_t type;
Christian Brauner203651b2018-09-04 21:53:55 +02004899};
4900
Linus Torvalds1da177e2005-04-16 15:20:36 -07004901static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
Christian Brauner203651b2018-09-04 21:53:55 +02004902 struct inet6_fill_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004903{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004904 struct nlmsghdr *nlh;
Thomas Graf85486af2006-09-18 00:11:24 -07004905 u32 preferred, valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004906
Christian Brauner203651b2018-09-04 21:53:55 +02004907 nlh = nlmsg_put(skb, args->portid, args->seq, args->event,
4908 sizeof(struct ifaddrmsg), args->flags);
Ian Morris63159f22015-03-29 14:00:04 +01004909 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08004910 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07004911
Thomas Graf101bb222006-09-18 00:11:52 -07004912 put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope),
4913 ifa->idev->dev->ifindex);
4914
Christian Brauner203651b2018-09-04 21:53:55 +02004915 if (args->netnsid >= 0 &&
4916 nla_put_s32(skb, IFA_TARGET_NETNSID, args->netnsid))
Christian Brauner6ecf4c32018-09-04 21:53:50 +02004917 goto error;
4918
Yasushi Asanofad8da32013-12-31 12:04:19 +09004919 if (!((ifa->flags&IFA_F_PERMANENT) &&
4920 (ifa->prefered_lft == INFINITY_LIFE_TIME))) {
Thomas Graf85486af2006-09-18 00:11:24 -07004921 preferred = ifa->prefered_lft;
4922 valid = ifa->valid_lft;
4923 if (preferred != INFINITY_LIFE_TIME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924 long tval = (jiffies - ifa->tstamp)/HZ;
Jens Rosenbooma1faa692009-06-25 04:55:50 +00004925 if (preferred > tval)
4926 preferred -= tval;
4927 else
4928 preferred = 0;
Ben Hutchingsf56619f2010-06-26 11:37:47 +00004929 if (valid != INFINITY_LIFE_TIME) {
4930 if (valid > tval)
4931 valid -= tval;
4932 else
4933 valid = 0;
4934 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004935 }
4936 } else {
Thomas Graf85486af2006-09-18 00:11:24 -07004937 preferred = INFINITY_LIFE_TIME;
4938 valid = INFINITY_LIFE_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004939 }
Thomas Graf85486af2006-09-18 00:11:24 -07004940
Cong Wang7996c792013-05-22 05:41:06 +00004941 if (!ipv6_addr_any(&ifa->peer_addr)) {
Jiri Benc930345e2015-03-29 16:59:25 +02004942 if (nla_put_in6_addr(skb, IFA_LOCAL, &ifa->addr) < 0 ||
4943 nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->peer_addr) < 0)
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004944 goto error;
4945 } else
Jiri Benc930345e2015-03-29 16:59:25 +02004946 if (nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->addr) < 0)
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004947 goto error;
4948
David Ahern8308f3f2018-05-27 08:09:58 -07004949 if (ifa->rt_priority &&
4950 nla_put_u32(skb, IFA_RT_PRIORITY, ifa->rt_priority))
4951 goto error;
4952
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004953 if (put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0)
4954 goto error;
Thomas Graf85486af2006-09-18 00:11:24 -07004955
Jiri Pirko479840f2013-12-06 09:45:21 +01004956 if (nla_put_u32(skb, IFA_FLAGS, ifa->flags) < 0)
4957 goto error;
4958
Johannes Berg053c0952015-01-16 22:09:00 +01004959 nlmsg_end(skb, nlh);
4960 return 0;
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004961
4962error:
4963 nlmsg_cancel(skb, nlh);
4964 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004965}
4966
4967static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
Christian Brauner203651b2018-09-04 21:53:55 +02004968 struct inet6_fill_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004970 struct nlmsghdr *nlh;
Thomas Graf101bb222006-09-18 00:11:52 -07004971 u8 scope = RT_SCOPE_UNIVERSE;
4972 int ifindex = ifmca->idev->dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973
Thomas Graf101bb222006-09-18 00:11:52 -07004974 if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE)
4975 scope = RT_SCOPE_SITE;
4976
Christian Brauner203651b2018-09-04 21:53:55 +02004977 nlh = nlmsg_put(skb, args->portid, args->seq, args->event,
4978 sizeof(struct ifaddrmsg), args->flags);
Ian Morris63159f22015-03-29 14:00:04 +01004979 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08004980 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07004981
Christian Brauner203651b2018-09-04 21:53:55 +02004982 if (args->netnsid >= 0 &&
4983 nla_put_s32(skb, IFA_TARGET_NETNSID, args->netnsid))
Christian Brauner6ecf4c32018-09-04 21:53:50 +02004984 return -EMSGSIZE;
4985
Thomas Graf101bb222006-09-18 00:11:52 -07004986 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
Jiri Benc930345e2015-03-29 16:59:25 +02004987 if (nla_put_in6_addr(skb, IFA_MULTICAST, &ifmca->mca_addr) < 0 ||
Thomas Graf0ab68032006-09-18 00:12:35 -07004988 put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp,
Patrick McHardy26932562007-01-31 23:16:40 -08004989 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
4990 nlmsg_cancel(skb, nlh);
4991 return -EMSGSIZE;
4992 }
Thomas Graf85486af2006-09-18 00:11:24 -07004993
Johannes Berg053c0952015-01-16 22:09:00 +01004994 nlmsg_end(skb, nlh);
4995 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004996}
4997
4998static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
Christian Brauner203651b2018-09-04 21:53:55 +02004999 struct inet6_fill_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005000{
David Ahern9ee8cbb2018-04-18 15:39:01 -07005001 struct net_device *dev = fib6_info_nh_dev(ifaca->aca_rt);
5002 int ifindex = dev ? dev->ifindex : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005003 struct nlmsghdr *nlh;
Thomas Graf101bb222006-09-18 00:11:52 -07005004 u8 scope = RT_SCOPE_UNIVERSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005005
Thomas Graf101bb222006-09-18 00:11:52 -07005006 if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE)
5007 scope = RT_SCOPE_SITE;
5008
Christian Brauner203651b2018-09-04 21:53:55 +02005009 nlh = nlmsg_put(skb, args->portid, args->seq, args->event,
5010 sizeof(struct ifaddrmsg), args->flags);
Ian Morris63159f22015-03-29 14:00:04 +01005011 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08005012 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07005013
Christian Brauner203651b2018-09-04 21:53:55 +02005014 if (args->netnsid >= 0 &&
5015 nla_put_s32(skb, IFA_TARGET_NETNSID, args->netnsid))
Christian Brauner6ecf4c32018-09-04 21:53:50 +02005016 return -EMSGSIZE;
5017
Thomas Graf101bb222006-09-18 00:11:52 -07005018 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
Jiri Benc930345e2015-03-29 16:59:25 +02005019 if (nla_put_in6_addr(skb, IFA_ANYCAST, &ifaca->aca_addr) < 0 ||
Thomas Graf0ab68032006-09-18 00:12:35 -07005020 put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp,
Patrick McHardy26932562007-01-31 23:16:40 -08005021 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
5022 nlmsg_cancel(skb, nlh);
5023 return -EMSGSIZE;
5024 }
Thomas Graf85486af2006-09-18 00:11:24 -07005025
Johannes Berg053c0952015-01-16 22:09:00 +01005026 nlmsg_end(skb, nlh);
5027 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005028}
5029
Eric Dumazet234b27c2009-11-12 04:11:50 +00005030/* called with rcu_read_lock() */
5031static int in6_dump_addrs(struct inet6_dev *idev, struct sk_buff *skb,
David Ahernfe884c22018-10-19 12:45:28 -07005032 struct netlink_callback *cb, int s_ip_idx,
David Ahern6ba1e6e2018-10-07 20:16:26 -07005033 struct inet6_fill_args *fillargs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005034{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005035 struct ifmcaddr6 *ifmca;
5036 struct ifacaddr6 *ifaca;
David Ahernfe884c22018-10-19 12:45:28 -07005037 int ip_idx = 0;
Eric Dumazet234b27c2009-11-12 04:11:50 +00005038 int err = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005039
Eric Dumazet234b27c2009-11-12 04:11:50 +00005040 read_lock_bh(&idev->lock);
David Ahern6ba1e6e2018-10-07 20:16:26 -07005041 switch (fillargs->type) {
stephen hemminger502a2ff2010-03-17 20:31:13 +00005042 case UNICAST_ADDR: {
5043 struct inet6_ifaddr *ifa;
David Ahern6ba1e6e2018-10-07 20:16:26 -07005044 fillargs->event = RTM_NEWADDR;
stephen hemminger502a2ff2010-03-17 20:31:13 +00005045
Eric Dumazet234b27c2009-11-12 04:11:50 +00005046 /* unicast address incl. temp addr */
stephen hemminger502a2ff2010-03-17 20:31:13 +00005047 list_for_each_entry(ifa, &idev->addr_list, if_list) {
David Ahern4ba4c562018-10-19 10:00:19 -07005048 if (ip_idx < s_ip_idx)
5049 goto next;
David Ahern6ba1e6e2018-10-07 20:16:26 -07005050 err = inet6_fill_ifaddr(skb, ifa, fillargs);
Johannes Berg053c0952015-01-16 22:09:00 +01005051 if (err < 0)
Eric Dumazet234b27c2009-11-12 04:11:50 +00005052 break;
Nicolas Dichtel63998ac2013-03-22 06:28:43 +00005053 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
David Ahern4ba4c562018-10-19 10:00:19 -07005054next:
5055 ip_idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005056 }
Eric Dumazet234b27c2009-11-12 04:11:50 +00005057 break;
stephen hemminger502a2ff2010-03-17 20:31:13 +00005058 }
Eric Dumazet234b27c2009-11-12 04:11:50 +00005059 case MULTICAST_ADDR:
David Ahern6ba1e6e2018-10-07 20:16:26 -07005060 fillargs->event = RTM_GETMULTICAST;
Christian Brauner203651b2018-09-04 21:53:55 +02005061
Eric Dumazet234b27c2009-11-12 04:11:50 +00005062 /* multicast address */
5063 for (ifmca = idev->mc_list; ifmca;
5064 ifmca = ifmca->next, ip_idx++) {
5065 if (ip_idx < s_ip_idx)
5066 continue;
David Ahern6ba1e6e2018-10-07 20:16:26 -07005067 err = inet6_fill_ifmcaddr(skb, ifmca, fillargs);
Johannes Berg053c0952015-01-16 22:09:00 +01005068 if (err < 0)
Eric Dumazet234b27c2009-11-12 04:11:50 +00005069 break;
5070 }
5071 break;
5072 case ANYCAST_ADDR:
David Ahern6ba1e6e2018-10-07 20:16:26 -07005073 fillargs->event = RTM_GETANYCAST;
Eric Dumazet234b27c2009-11-12 04:11:50 +00005074 /* anycast address */
5075 for (ifaca = idev->ac_list; ifaca;
5076 ifaca = ifaca->aca_next, ip_idx++) {
5077 if (ip_idx < s_ip_idx)
5078 continue;
David Ahern6ba1e6e2018-10-07 20:16:26 -07005079 err = inet6_fill_ifacaddr(skb, ifaca, fillargs);
Johannes Berg053c0952015-01-16 22:09:00 +01005080 if (err < 0)
Eric Dumazet234b27c2009-11-12 04:11:50 +00005081 break;
5082 }
5083 break;
5084 default:
5085 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005086 }
Eric Dumazet234b27c2009-11-12 04:11:50 +00005087 read_unlock_bh(&idev->lock);
David Ahernfe884c22018-10-19 12:45:28 -07005088 cb->args[2] = ip_idx;
Eric Dumazet234b27c2009-11-12 04:11:50 +00005089 return err;
5090}
5091
David Aherned6eff12018-10-07 20:16:29 -07005092static int inet6_valid_dump_ifaddr_req(const struct nlmsghdr *nlh,
5093 struct inet6_fill_args *fillargs,
5094 struct net **tgt_net, struct sock *sk,
David Ahern6371a712018-10-19 12:45:30 -07005095 struct netlink_callback *cb)
David Aherned6eff12018-10-07 20:16:29 -07005096{
David Ahern6371a712018-10-19 12:45:30 -07005097 struct netlink_ext_ack *extack = cb->extack;
David Aherned6eff12018-10-07 20:16:29 -07005098 struct nlattr *tb[IFA_MAX+1];
5099 struct ifaddrmsg *ifm;
5100 int err, i;
5101
5102 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
5103 NL_SET_ERR_MSG_MOD(extack, "Invalid header for address dump request");
5104 return -EINVAL;
5105 }
5106
5107 ifm = nlmsg_data(nlh);
5108 if (ifm->ifa_prefixlen || ifm->ifa_flags || ifm->ifa_scope) {
5109 NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for address dump request");
5110 return -EINVAL;
5111 }
David Ahern6371a712018-10-19 12:45:30 -07005112
5113 fillargs->ifindex = ifm->ifa_index;
5114 if (fillargs->ifindex) {
5115 cb->answer_flags |= NLM_F_DUMP_FILTERED;
5116 fillargs->flags |= NLM_F_DUMP_FILTERED;
David Aherned6eff12018-10-07 20:16:29 -07005117 }
5118
Johannes Berg8cb08172019-04-26 14:07:28 +02005119 err = nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, IFA_MAX,
5120 ifa_ipv6_policy, extack);
David Aherned6eff12018-10-07 20:16:29 -07005121 if (err < 0)
5122 return err;
5123
5124 for (i = 0; i <= IFA_MAX; ++i) {
5125 if (!tb[i])
5126 continue;
5127
5128 if (i == IFA_TARGET_NETNSID) {
5129 struct net *net;
5130
5131 fillargs->netnsid = nla_get_s32(tb[i]);
5132 net = rtnl_get_net_ns_capable(sk, fillargs->netnsid);
5133 if (IS_ERR(net)) {
Bjørn Morkbf4cc402018-10-25 21:18:25 +02005134 fillargs->netnsid = -1;
David Aherned6eff12018-10-07 20:16:29 -07005135 NL_SET_ERR_MSG_MOD(extack, "Invalid target network namespace id");
5136 return PTR_ERR(net);
5137 }
5138 *tgt_net = net;
5139 } else {
5140 NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in dump request");
5141 return -EINVAL;
5142 }
5143 }
5144
5145 return 0;
5146}
5147
Eric Dumazet234b27c2009-11-12 04:11:50 +00005148static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
5149 enum addr_type_t type)
5150{
David Aherned6eff12018-10-07 20:16:29 -07005151 const struct nlmsghdr *nlh = cb->nlh;
David Ahern6ba1e6e2018-10-07 20:16:26 -07005152 struct inet6_fill_args fillargs = {
5153 .portid = NETLINK_CB(cb->skb).portid,
5154 .seq = cb->nlh->nlmsg_seq,
5155 .flags = NLM_F_MULTI,
5156 .netnsid = -1,
5157 .type = type,
5158 };
Eric Dumazet234b27c2009-11-12 04:11:50 +00005159 struct net *net = sock_net(skb->sk);
Christian Brauner6ecf4c32018-09-04 21:53:50 +02005160 struct net *tgt_net = net;
David Ahernfe884c22018-10-19 12:45:28 -07005161 int idx, s_idx, s_ip_idx;
Eric Dumazet234b27c2009-11-12 04:11:50 +00005162 int h, s_h;
Eric Dumazet234b27c2009-11-12 04:11:50 +00005163 struct net_device *dev;
5164 struct inet6_dev *idev;
5165 struct hlist_head *head;
David Ahern242afaa2018-10-24 12:59:00 -07005166 int err = 0;
Eric Dumazet234b27c2009-11-12 04:11:50 +00005167
5168 s_h = cb->args[0];
5169 s_idx = idx = cb->args[1];
David Ahernfe884c22018-10-19 12:45:28 -07005170 s_ip_idx = cb->args[2];
Eric Dumazet234b27c2009-11-12 04:11:50 +00005171
David Aherned6eff12018-10-07 20:16:29 -07005172 if (cb->strict_check) {
David Aherned6eff12018-10-07 20:16:29 -07005173 err = inet6_valid_dump_ifaddr_req(nlh, &fillargs, &tgt_net,
David Ahern6371a712018-10-19 12:45:30 -07005174 skb->sk, cb);
David Aherned6eff12018-10-07 20:16:29 -07005175 if (err < 0)
David Ahern242afaa2018-10-24 12:59:00 -07005176 goto put_tgt_net;
David Ahern6371a712018-10-19 12:45:30 -07005177
David Ahern242afaa2018-10-24 12:59:00 -07005178 err = 0;
David Ahern6371a712018-10-19 12:45:30 -07005179 if (fillargs.ifindex) {
5180 dev = __dev_get_by_index(tgt_net, fillargs.ifindex);
David Ahern242afaa2018-10-24 12:59:00 -07005181 if (!dev) {
5182 err = -ENODEV;
5183 goto put_tgt_net;
5184 }
David Ahern6371a712018-10-19 12:45:30 -07005185 idev = __in6_dev_get(dev);
5186 if (idev) {
5187 err = in6_dump_addrs(idev, skb, cb, s_ip_idx,
5188 &fillargs);
Jakub Kicinski15180392019-01-22 14:47:19 -08005189 if (err > 0)
5190 err = 0;
David Ahern6371a712018-10-19 12:45:30 -07005191 }
5192 goto put_tgt_net;
5193 }
Christian Brauner6ecf4c32018-09-04 21:53:50 +02005194 }
5195
Eric Dumazet234b27c2009-11-12 04:11:50 +00005196 rcu_read_lock();
Christian Brauner6ecf4c32018-09-04 21:53:50 +02005197 cb->seq = atomic_read(&tgt_net->ipv6.dev_addr_genid) ^ tgt_net->dev_base_seq;
Eric Dumazet234b27c2009-11-12 04:11:50 +00005198 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
5199 idx = 0;
Christian Brauner6ecf4c32018-09-04 21:53:50 +02005200 head = &tgt_net->dev_index_head[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -08005201 hlist_for_each_entry_rcu(dev, head, index_hlist) {
Eric Dumazet234b27c2009-11-12 04:11:50 +00005202 if (idx < s_idx)
5203 goto cont;
Patrick McHardy4b97efd2010-03-26 20:27:49 -07005204 if (h > s_h || idx > s_idx)
Eric Dumazet234b27c2009-11-12 04:11:50 +00005205 s_ip_idx = 0;
Stephen Hemmingere21e8462010-03-20 16:09:01 -07005206 idev = __in6_dev_get(dev);
5207 if (!idev)
Eric Dumazet234b27c2009-11-12 04:11:50 +00005208 goto cont;
5209
David Ahernfe884c22018-10-19 12:45:28 -07005210 if (in6_dump_addrs(idev, skb, cb, s_ip_idx,
David Ahern6ba1e6e2018-10-07 20:16:26 -07005211 &fillargs) < 0)
Eric Dumazet234b27c2009-11-12 04:11:50 +00005212 goto done;
5213cont:
5214 idx++;
5215 }
5216 }
5217done:
5218 rcu_read_unlock();
5219 cb->args[0] = h;
5220 cb->args[1] = idx;
David Ahern6371a712018-10-19 12:45:30 -07005221put_tgt_net:
David Ahern6ba1e6e2018-10-07 20:16:26 -07005222 if (fillargs.netnsid >= 0)
Christian Brauner6ecf4c32018-09-04 21:53:50 +02005223 put_net(tgt_net);
Eric Dumazet234b27c2009-11-12 04:11:50 +00005224
Arthur Gautier7c1e8a32018-12-31 02:10:58 +00005225 return skb->len ? : err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005226}
5227
5228static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
5229{
5230 enum addr_type_t type = UNICAST_ADDR;
Denis V. Lunevb8542722007-12-01 00:21:31 +11005231
Linus Torvalds1da177e2005-04-16 15:20:36 -07005232 return inet6_dump_addr(skb, cb, type);
5233}
5234
5235static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
5236{
5237 enum addr_type_t type = MULTICAST_ADDR;
Denis V. Lunevb8542722007-12-01 00:21:31 +11005238
Linus Torvalds1da177e2005-04-16 15:20:36 -07005239 return inet6_dump_addr(skb, cb, type);
5240}
5241
5242
5243static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
5244{
5245 enum addr_type_t type = ANYCAST_ADDR;
Denis V. Lunevb8542722007-12-01 00:21:31 +11005246
Linus Torvalds1da177e2005-04-16 15:20:36 -07005247 return inet6_dump_addr(skb, cb, type);
5248}
5249
Jakub Kicinski4b1373d2019-01-18 10:46:21 -08005250static int inet6_rtm_valid_getaddr_req(struct sk_buff *skb,
5251 const struct nlmsghdr *nlh,
5252 struct nlattr **tb,
5253 struct netlink_ext_ack *extack)
5254{
5255 struct ifaddrmsg *ifm;
5256 int i, err;
5257
5258 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
5259 NL_SET_ERR_MSG_MOD(extack, "Invalid header for get address request");
5260 return -EINVAL;
5261 }
5262
Hangbin Liu2beb6d22019-12-11 22:20:16 +08005263 if (!netlink_strict_get_check(skb))
5264 return nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFA_MAX,
5265 ifa_ipv6_policy, extack);
5266
Jakub Kicinski4b1373d2019-01-18 10:46:21 -08005267 ifm = nlmsg_data(nlh);
5268 if (ifm->ifa_prefixlen || ifm->ifa_flags || ifm->ifa_scope) {
5269 NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for get address request");
5270 return -EINVAL;
5271 }
5272
Johannes Berg8cb08172019-04-26 14:07:28 +02005273 err = nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, IFA_MAX,
5274 ifa_ipv6_policy, extack);
Jakub Kicinski4b1373d2019-01-18 10:46:21 -08005275 if (err)
5276 return err;
5277
5278 for (i = 0; i <= IFA_MAX; i++) {
5279 if (!tb[i])
5280 continue;
5281
5282 switch (i) {
5283 case IFA_TARGET_NETNSID:
5284 case IFA_ADDRESS:
5285 case IFA_LOCAL:
5286 break;
5287 default:
5288 NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in get address request");
5289 return -EINVAL;
5290 }
5291 }
5292
5293 return 0;
5294}
5295
David Ahernc21ef3e2017-04-16 09:48:24 -07005296static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr *nlh,
5297 struct netlink_ext_ack *extack)
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09005298{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09005299 struct net *net = sock_net(in_skb->sk);
Christian Brauner203651b2018-09-04 21:53:55 +02005300 struct inet6_fill_args fillargs = {
5301 .portid = NETLINK_CB(in_skb).portid,
5302 .seq = nlh->nlmsg_seq,
5303 .event = RTM_NEWADDR,
5304 .flags = 0,
5305 .netnsid = -1,
5306 };
Christian Brauner6ecf4c32018-09-04 21:53:50 +02005307 struct net *tgt_net = net;
Thomas Graf1b29fc22006-09-18 00:10:50 -07005308 struct ifaddrmsg *ifm;
5309 struct nlattr *tb[IFA_MAX+1];
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00005310 struct in6_addr *addr = NULL, *peer;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09005311 struct net_device *dev = NULL;
5312 struct inet6_ifaddr *ifa;
5313 struct sk_buff *skb;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09005314 int err;
5315
Jakub Kicinski4b1373d2019-01-18 10:46:21 -08005316 err = inet6_rtm_valid_getaddr_req(in_skb, nlh, tb, extack);
Thomas Graf1b29fc22006-09-18 00:10:50 -07005317 if (err < 0)
Florian Westphalc24675f2017-10-11 10:28:01 +02005318 return err;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09005319
Christian Brauner6ecf4c32018-09-04 21:53:50 +02005320 if (tb[IFA_TARGET_NETNSID]) {
Christian Brauner203651b2018-09-04 21:53:55 +02005321 fillargs.netnsid = nla_get_s32(tb[IFA_TARGET_NETNSID]);
Christian Brauner6ecf4c32018-09-04 21:53:50 +02005322
5323 tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(in_skb).sk,
Christian Brauner203651b2018-09-04 21:53:55 +02005324 fillargs.netnsid);
Christian Brauner6ecf4c32018-09-04 21:53:50 +02005325 if (IS_ERR(tgt_net))
5326 return PTR_ERR(tgt_net);
5327 }
5328
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00005329 addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer);
Florian Westphalc24675f2017-10-11 10:28:01 +02005330 if (!addr)
5331 return -EINVAL;
Thomas Graf1b29fc22006-09-18 00:10:50 -07005332
5333 ifm = nlmsg_data(nlh);
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09005334 if (ifm->ifa_index)
Christian Brauner6ecf4c32018-09-04 21:53:50 +02005335 dev = dev_get_by_index(tgt_net, ifm->ifa_index);
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09005336
Christian Brauner6ecf4c32018-09-04 21:53:50 +02005337 ifa = ipv6_get_ifaddr(tgt_net, addr, dev, 1);
Stephen Hemmingere21e8462010-03-20 16:09:01 -07005338 if (!ifa) {
Thomas Graf1b29fc22006-09-18 00:10:50 -07005339 err = -EADDRNOTAVAIL;
5340 goto errout;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09005341 }
5342
Stephen Hemmingere21e8462010-03-20 16:09:01 -07005343 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL);
5344 if (!skb) {
Thomas Graf1b29fc22006-09-18 00:10:50 -07005345 err = -ENOBUFS;
5346 goto errout_ifa;
5347 }
5348
Christian Brauner203651b2018-09-04 21:53:55 +02005349 err = inet6_fill_ifaddr(skb, ifa, &fillargs);
Patrick McHardy26932562007-01-31 23:16:40 -08005350 if (err < 0) {
5351 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
5352 WARN_ON(err == -EMSGSIZE);
5353 kfree_skb(skb);
5354 goto errout_ifa;
5355 }
Christian Brauner6ecf4c32018-09-04 21:53:50 +02005356 err = rtnl_unicast(skb, tgt_net, NETLINK_CB(in_skb).portid);
Thomas Graf1b29fc22006-09-18 00:10:50 -07005357errout_ifa:
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09005358 in6_ifa_put(ifa);
Thomas Graf1b29fc22006-09-18 00:10:50 -07005359errout:
Florian Westphalc24675f2017-10-11 10:28:01 +02005360 if (dev)
5361 dev_put(dev);
Christian Brauner203651b2018-09-04 21:53:55 +02005362 if (fillargs.netnsid >= 0)
Christian Brauner6ecf4c32018-09-04 21:53:50 +02005363 put_net(tgt_net);
5364
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09005365 return err;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09005366}
5367
Linus Torvalds1da177e2005-04-16 15:20:36 -07005368static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
5369{
5370 struct sk_buff *skb;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09005371 struct net *net = dev_net(ifa->idev->dev);
Christian Brauner203651b2018-09-04 21:53:55 +02005372 struct inet6_fill_args fillargs = {
5373 .portid = 0,
5374 .seq = 0,
5375 .event = event,
5376 .flags = 0,
5377 .netnsid = -1,
5378 };
Thomas Graf5d620262006-08-15 00:35:02 -07005379 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005380
Thomas Graf0ab68032006-09-18 00:12:35 -07005381 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC);
Ian Morris63159f22015-03-29 14:00:04 +01005382 if (!skb)
Thomas Graf5d620262006-08-15 00:35:02 -07005383 goto errout;
5384
Christian Brauner203651b2018-09-04 21:53:55 +02005385 err = inet6_fill_ifaddr(skb, ifa, &fillargs);
Patrick McHardy26932562007-01-31 23:16:40 -08005386 if (err < 0) {
5387 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
5388 WARN_ON(err == -EMSGSIZE);
5389 kfree_skb(skb);
5390 goto errout;
5391 }
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08005392 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
5393 return;
Thomas Graf5d620262006-08-15 00:35:02 -07005394errout:
5395 if (err < 0)
Benjamin Thery6fda7352008-03-05 10:47:47 -08005396 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005397}
5398
Dave Jonesb6f99a22007-03-22 12:27:49 -07005399static inline void ipv6_store_devconf(struct ipv6_devconf *cnf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005400 __s32 *array, int bytes)
5401{
Thomas Graf04561c12006-11-14 19:53:58 -08005402 BUG_ON(bytes < (DEVCONF_MAX * 4));
5403
Linus Torvalds1da177e2005-04-16 15:20:36 -07005404 memset(array, 0, bytes);
5405 array[DEVCONF_FORWARDING] = cnf->forwarding;
5406 array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
5407 array[DEVCONF_MTU6] = cnf->mtu6;
5408 array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
5409 array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
5410 array[DEVCONF_AUTOCONF] = cnf->autoconf;
5411 array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
5412 array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
Thomas Graf93908d12010-11-17 01:44:24 +00005413 array[DEVCONF_RTR_SOLICIT_INTERVAL] =
5414 jiffies_to_msecs(cnf->rtr_solicit_interval);
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07005415 array[DEVCONF_RTR_SOLICIT_MAX_INTERVAL] =
5416 jiffies_to_msecs(cnf->rtr_solicit_max_interval);
Thomas Graf93908d12010-11-17 01:44:24 +00005417 array[DEVCONF_RTR_SOLICIT_DELAY] =
5418 jiffies_to_msecs(cnf->rtr_solicit_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005419 array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
Hannes Frederic Sowafc4eba52013-08-14 01:03:46 +02005420 array[DEVCONF_MLDV1_UNSOLICITED_REPORT_INTERVAL] =
5421 jiffies_to_msecs(cnf->mldv1_unsolicited_report_interval);
5422 array[DEVCONF_MLDV2_UNSOLICITED_REPORT_INTERVAL] =
5423 jiffies_to_msecs(cnf->mldv2_unsolicited_report_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005424 array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
5425 array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
5426 array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
5427 array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
5428 array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005429 array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -08005430 array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr;
Hangbin Liu8013d1d72015-07-30 14:28:42 +08005431 array[DEVCONF_ACCEPT_RA_MIN_HOP_LIMIT] = cnf->accept_ra_min_hop_limit;
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -08005432 array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo;
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08005433#ifdef CONFIG_IPV6_ROUTER_PREF
5434 array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref;
Thomas Graf93908d12010-11-17 01:44:24 +00005435 array[DEVCONF_RTR_PROBE_INTERVAL] =
5436 jiffies_to_msecs(cnf->rtr_probe_interval);
Neil Hormanfa03ef32007-01-30 14:30:10 -08005437#ifdef CONFIG_IPV6_ROUTE_INFO
Joel Scherpelzbbea1242017-03-22 18:19:04 +09005438 array[DEVCONF_ACCEPT_RA_RT_INFO_MIN_PLEN] = cnf->accept_ra_rt_info_min_plen;
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -08005439 array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen;
5440#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08005441#endif
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -07005442 array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp;
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -07005443 array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route;
Neil Horman95c385b2007-04-25 17:08:10 -07005444#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
5445 array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad;
Erik Kline7fd25612014-10-28 18:11:14 +09005446 array[DEVCONF_USE_OPTIMISTIC] = cnf->use_optimistic;
Neil Horman95c385b2007-04-25 17:08:10 -07005447#endif
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09005448#ifdef CONFIG_IPV6_MROUTE
5449 array[DEVCONF_MC_FORWARDING] = cnf->mc_forwarding;
5450#endif
YOSHIFUJI Hideaki778d80b2008-06-28 14:17:11 +09005451 array[DEVCONF_DISABLE_IPV6] = cnf->disable_ipv6;
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09005452 array[DEVCONF_ACCEPT_DAD] = cnf->accept_dad;
Cosmin Ratiuc3faca02009-10-09 03:11:14 +00005453 array[DEVCONF_FORCE_TLLAO] = cnf->force_tllao;
Hannes Frederic Sowa5cb04432012-11-06 16:46:20 +00005454 array[DEVCONF_NDISC_NOTIFY] = cnf->ndisc_notify;
Hannes Frederic Sowab800c3b2013-08-27 01:36:51 +02005455 array[DEVCONF_SUPPRESS_FRAG_NDISC] = cnf->suppress_frag_ndisc;
Ben Greeard9333192014-06-25 14:44:53 -07005456 array[DEVCONF_ACCEPT_RA_FROM_LOCAL] = cnf->accept_ra_from_local;
Harout Hedeshianc2943f12015-01-20 10:06:05 -07005457 array[DEVCONF_ACCEPT_RA_MTU] = cnf->accept_ra_mtu;
Andy Gospodarek35103d12015-08-13 10:39:01 -04005458 array[DEVCONF_IGNORE_ROUTES_WITH_LINKDOWN] = cnf->ignore_routes_with_linkdown;
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01005459 /* we omit DEVCONF_STABLE_SECRET for now */
Erik Kline3985e8a2015-07-22 16:38:25 +09005460 array[DEVCONF_USE_OIF_ADDRS_ONLY] = cnf->use_oif_addrs_only;
Johannes Bergabbc3042016-02-04 13:31:19 +01005461 array[DEVCONF_DROP_UNICAST_IN_L2_MULTICAST] = cnf->drop_unicast_in_l2_multicast;
Johannes Berg7a02bf82016-02-04 13:31:20 +01005462 array[DEVCONF_DROP_UNSOLICITED_NA] = cnf->drop_unsolicited_na;
David Ahernf1705ec2016-02-24 09:25:37 -08005463 array[DEVCONF_KEEP_ADDR_ON_DOWN] = cnf->keep_addr_on_down;
David Lebrun1ababeb2016-11-08 14:57:39 +01005464 array[DEVCONF_SEG6_ENABLED] = cnf->seg6_enabled;
David Lebrunbf355b82016-11-08 14:57:42 +01005465#ifdef CONFIG_IPV6_SEG6_HMAC
5466 array[DEVCONF_SEG6_REQUIRE_HMAC] = cnf->seg6_require_hmac;
5467#endif
Erik Nordmarkadc176c2016-12-02 14:00:08 -08005468 array[DEVCONF_ENHANCED_DAD] = cnf->enhanced_dad;
Felix Jiad35a00b2017-01-26 16:59:17 +13005469 array[DEVCONF_ADDR_GEN_MODE] = cnf->addr_gen_mode;
David Forsterdf789fe2017-02-23 16:27:18 +00005470 array[DEVCONF_DISABLE_POLICY] = cnf->disable_policy;
Maciej Żenczykowski2210d6b2017-11-07 21:52:09 -08005471 array[DEVCONF_NDISC_TCLASS] = cnf->ndisc_tclass;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005472}
5473
Thomas Grafb382b192010-11-16 04:33:57 +00005474static inline size_t inet6_ifla6_size(void)
5475{
5476 return nla_total_size(4) /* IFLA_INET6_FLAGS */
5477 + nla_total_size(sizeof(struct ifla_cacheinfo))
5478 + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */
5479 + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005480 + nla_total_size(ICMP6_MIB_MAX * 8) /* IFLA_INET6_ICMP6STATS */
Sabrina Dubrocabdd72f42018-07-09 12:25:16 +02005481 + nla_total_size(sizeof(struct in6_addr)) /* IFLA_INET6_TOKEN */
5482 + nla_total_size(1) /* IFLA_INET6_ADDR_GEN_MODE */
5483 + 0;
Thomas Grafb382b192010-11-16 04:33:57 +00005484}
5485
Thomas Graf339bf982006-11-10 14:10:15 -08005486static inline size_t inet6_if_nlmsg_size(void)
5487{
5488 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
5489 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
5490 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
5491 + nla_total_size(4) /* IFLA_MTU */
5492 + nla_total_size(4) /* IFLA_LINK */
Andy Gospodarek03443382015-08-13 15:26:35 -04005493 + nla_total_size(1) /* IFLA_OPERSTATE */
Thomas Grafb382b192010-11-16 04:33:57 +00005494 + nla_total_size(inet6_ifla6_size()); /* IFLA_PROTINFO */
Thomas Graf339bf982006-11-10 14:10:15 -08005495}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07005496
Eric Dumazetbe281e52011-05-19 01:14:23 +00005497static inline void __snmp6_fill_statsdev(u64 *stats, atomic_long_t *mib,
Jia Heaca05672016-09-30 11:29:03 +08005498 int bytes)
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005499{
5500 int i;
Jia Heaca05672016-09-30 11:29:03 +08005501 int pad = bytes - sizeof(u64) * ICMP6_MIB_MAX;
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005502 BUG_ON(pad < 0);
5503
5504 /* Use put_unaligned() because stats may not be aligned for u64. */
Jia Heaca05672016-09-30 11:29:03 +08005505 put_unaligned(ICMP6_MIB_MAX, &stats[0]);
5506 for (i = 1; i < ICMP6_MIB_MAX; i++)
Eric Dumazetbe281e52011-05-19 01:14:23 +00005507 put_unaligned(atomic_long_read(&mib[i]), &stats[i]);
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005508
Jia Heaca05672016-09-30 11:29:03 +08005509 memset(&stats[ICMP6_MIB_MAX], 0, pad);
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005510}
5511
WANG Cong698365f2014-05-05 15:55:55 -07005512static inline void __snmp6_fill_stats64(u64 *stats, void __percpu *mib,
Raghavendra K Ta3a77372015-08-30 11:29:42 +05305513 int bytes, size_t syncpoff)
Eric Dumazet4ce3c182010-06-30 13:31:19 -07005514{
Raghavendra K Ta3a77372015-08-30 11:29:42 +05305515 int i, c;
5516 u64 buff[IPSTATS_MIB_MAX];
5517 int pad = bytes - sizeof(u64) * IPSTATS_MIB_MAX;
5518
Eric Dumazet4ce3c182010-06-30 13:31:19 -07005519 BUG_ON(pad < 0);
5520
Raghavendra K Ta3a77372015-08-30 11:29:42 +05305521 memset(buff, 0, sizeof(buff));
5522 buff[0] = IPSTATS_MIB_MAX;
Eric Dumazet4ce3c182010-06-30 13:31:19 -07005523
Raghavendra K Ta3a77372015-08-30 11:29:42 +05305524 for_each_possible_cpu(c) {
5525 for (i = 1; i < IPSTATS_MIB_MAX; i++)
5526 buff[i] += snmp_get_cpu_field64(mib, c, i, syncpoff);
5527 }
5528
5529 memcpy(stats, buff, IPSTATS_MIB_MAX * sizeof(u64));
5530 memset(&stats[IPSTATS_MIB_MAX], 0, pad);
Eric Dumazet4ce3c182010-06-30 13:31:19 -07005531}
5532
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005533static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype,
5534 int bytes)
5535{
Stephen Hemmingere21e8462010-03-20 16:09:01 -07005536 switch (attrtype) {
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005537 case IFLA_INET6_STATS:
Raghavendra K Ta3a77372015-08-30 11:29:42 +05305538 __snmp6_fill_stats64(stats, idev->stats.ipv6, bytes,
5539 offsetof(struct ipstats_mib, syncp));
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005540 break;
5541 case IFLA_INET6_ICMP6STATS:
Jia Heaca05672016-09-30 11:29:03 +08005542 __snmp6_fill_statsdev(stats, idev->stats.icmpv6dev->mibs, bytes);
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005543 break;
5544 }
5545}
5546
Sowmini Varadhand5566fd2015-09-11 16:48:48 -04005547static int inet6_fill_ifla6_attrs(struct sk_buff *skb, struct inet6_dev *idev,
5548 u32 ext_filter_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005549{
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005550 struct nlattr *nla;
Thomas Graf04561c12006-11-14 19:53:58 -08005551 struct ifla_cacheinfo ci;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005552
David S. Millerc78679e2012-04-01 20:27:33 -04005553 if (nla_put_u32(skb, IFLA_INET6_FLAGS, idev->if_flags))
5554 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005555 ci.max_reasm_len = IPV6_MAXPLEN;
David S. Miller24912422010-11-19 13:13:47 -08005556 ci.tstamp = cstamp_delta(idev->tstamp);
5557 ci.reachable_time = jiffies_to_msecs(idev->nd_parms->reachable_time);
Jiri Pirko1f9248e2013-12-07 19:26:53 +01005558 ci.retrans_time = jiffies_to_msecs(NEIGH_VAR(idev->nd_parms, RETRANS_TIME));
David S. Millerc78679e2012-04-01 20:27:33 -04005559 if (nla_put(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci))
5560 goto nla_put_failure;
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005561 nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32));
Ian Morris63159f22015-03-29 14:00:04 +01005562 if (!nla)
Thomas Graf04561c12006-11-14 19:53:58 -08005563 goto nla_put_failure;
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005564 ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005565
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005566 /* XXX - MC not implemented */
5567
Sowmini Varadhand5566fd2015-09-11 16:48:48 -04005568 if (ext_filter_mask & RTEXT_FILTER_SKIP_STATS)
5569 return 0;
5570
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005571 nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64));
Ian Morris63159f22015-03-29 14:00:04 +01005572 if (!nla)
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005573 goto nla_put_failure;
5574 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla));
5575
5576 nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64));
Ian Morris63159f22015-03-29 14:00:04 +01005577 if (!nla)
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005578 goto nla_put_failure;
5579 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005580
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005581 nla = nla_reserve(skb, IFLA_INET6_TOKEN, sizeof(struct in6_addr));
Ian Morris63159f22015-03-29 14:00:04 +01005582 if (!nla)
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005583 goto nla_put_failure;
5584 read_lock_bh(&idev->lock);
5585 memcpy(nla_data(nla), idev->token.s6_addr, nla_len(nla));
5586 read_unlock_bh(&idev->lock);
5587
Nicolas Dichtel0d7982c2019-09-30 14:02:16 +02005588 if (nla_put_u8(skb, IFLA_INET6_ADDR_GEN_MODE, idev->cnf.addr_gen_mode))
5589 goto nla_put_failure;
5590
Thomas Grafb382b192010-11-16 04:33:57 +00005591 return 0;
5592
5593nla_put_failure:
5594 return -EMSGSIZE;
5595}
5596
Arad, Ronenb1974ed2015-10-19 09:23:28 -07005597static size_t inet6_get_link_af_size(const struct net_device *dev,
5598 u32 ext_filter_mask)
Thomas Grafb382b192010-11-16 04:33:57 +00005599{
5600 if (!__in6_dev_get(dev))
5601 return 0;
5602
5603 return inet6_ifla6_size();
5604}
5605
Sowmini Varadhand5566fd2015-09-11 16:48:48 -04005606static int inet6_fill_link_af(struct sk_buff *skb, const struct net_device *dev,
5607 u32 ext_filter_mask)
Thomas Grafb382b192010-11-16 04:33:57 +00005608{
5609 struct inet6_dev *idev = __in6_dev_get(dev);
5610
5611 if (!idev)
5612 return -ENODATA;
5613
Sowmini Varadhand5566fd2015-09-11 16:48:48 -04005614 if (inet6_fill_ifla6_attrs(skb, idev, ext_filter_mask) < 0)
Thomas Grafb382b192010-11-16 04:33:57 +00005615 return -EMSGSIZE;
5616
5617 return 0;
5618}
5619
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005620static int inet6_set_iftoken(struct inet6_dev *idev, struct in6_addr *token)
5621{
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005622 struct inet6_ifaddr *ifp;
5623 struct net_device *dev = idev->dev;
Daniel Borkmann47e27d52016-04-08 15:55:00 +02005624 bool clear_token, update_rs = false;
Hannes Frederic Sowadc848292013-06-24 21:42:40 +02005625 struct in6_addr ll_addr;
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005626
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01005627 ASSERT_RTNL();
5628
Ian Morris63159f22015-03-29 14:00:04 +01005629 if (!token)
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005630 return -EINVAL;
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005631 if (dev->flags & (IFF_LOOPBACK | IFF_NOARP))
5632 return -EINVAL;
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005633 if (!ipv6_accept_ra(idev))
5634 return -EINVAL;
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07005635 if (idev->cnf.rtr_solicits == 0)
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005636 return -EINVAL;
5637
5638 write_lock_bh(&idev->lock);
5639
5640 BUILD_BUG_ON(sizeof(token->s6_addr) != 16);
5641 memcpy(idev->token.s6_addr + 8, token->s6_addr + 8, 8);
5642
5643 write_unlock_bh(&idev->lock);
5644
Daniel Borkmann47e27d52016-04-08 15:55:00 +02005645 clear_token = ipv6_addr_any(token);
5646 if (clear_token)
5647 goto update_lft;
5648
Hannes Frederic Sowadc848292013-06-24 21:42:40 +02005649 if (!idev->dead && (idev->if_flags & IF_READY) &&
5650 !ipv6_get_lladdr(dev, &ll_addr, IFA_F_TENTATIVE |
5651 IFA_F_OPTIMISTIC)) {
Daniel Borkmannfc403832013-04-09 03:47:15 +00005652 /* If we're not ready, then normal ifup will take care
5653 * of this. Otherwise, we need to request our rs here.
5654 */
5655 ndisc_send_rs(dev, &ll_addr, &in6addr_linklocal_allrouters);
5656 update_rs = true;
5657 }
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005658
Daniel Borkmann47e27d52016-04-08 15:55:00 +02005659update_lft:
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005660 write_lock_bh(&idev->lock);
Daniel Borkmannfc403832013-04-09 03:47:15 +00005661
Hannes Frederic Sowa77ecaac2013-06-26 03:41:49 +02005662 if (update_rs) {
Daniel Borkmannfc403832013-04-09 03:47:15 +00005663 idev->if_flags |= IF_RS_SENT;
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07005664 idev->rs_interval = rfc3315_s14_backoff_init(
5665 idev->cnf.rtr_solicit_interval);
Hannes Frederic Sowa77ecaac2013-06-26 03:41:49 +02005666 idev->rs_probes = 1;
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07005667 addrconf_mod_rs_timer(idev, idev->rs_interval);
Hannes Frederic Sowa77ecaac2013-06-26 03:41:49 +02005668 }
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005669
5670 /* Well, that's kinda nasty ... */
5671 list_for_each_entry(ifp, &idev->addr_list, if_list) {
5672 spin_lock(&ifp->lock);
Daniel Borkmann617fe292013-04-09 03:47:16 +00005673 if (ifp->tokenized) {
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005674 ifp->valid_lft = 0;
5675 ifp->prefered_lft = 0;
5676 }
5677 spin_unlock(&ifp->lock);
5678 }
5679
5680 write_unlock_bh(&idev->lock);
Lubomir Rintelb2ed64a2014-10-27 17:39:16 +01005681 inet6_ifinfo_notify(RTM_NEWLINK, idev);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01005682 addrconf_verify_rtnl();
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005683 return 0;
5684}
5685
Daniel Borkmann11b1f822015-02-05 14:39:11 +01005686static const struct nla_policy inet6_af_policy[IFLA_INET6_MAX + 1] = {
5687 [IFLA_INET6_ADDR_GEN_MODE] = { .type = NLA_U8 },
5688 [IFLA_INET6_TOKEN] = { .len = sizeof(struct in6_addr) },
5689};
5690
Felix Jiad35a00b2017-01-26 16:59:17 +13005691static int check_addr_gen_mode(int mode)
5692{
5693 if (mode != IN6_ADDR_GEN_MODE_EUI64 &&
5694 mode != IN6_ADDR_GEN_MODE_NONE &&
5695 mode != IN6_ADDR_GEN_MODE_STABLE_PRIVACY &&
5696 mode != IN6_ADDR_GEN_MODE_RANDOM)
5697 return -EINVAL;
5698 return 1;
5699}
5700
5701static int check_stable_privacy(struct inet6_dev *idev, struct net *net,
5702 int mode)
5703{
5704 if (mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY &&
5705 !idev->cnf.stable_secret.initialized &&
5706 !net->ipv6.devconf_dflt->stable_secret.initialized)
5707 return -EINVAL;
5708 return 1;
5709}
5710
Maxim Mikityanskiy7dc2bcc2019-05-21 06:40:04 +00005711static int inet6_validate_link_af(const struct net_device *dev,
5712 const struct nlattr *nla)
5713{
5714 struct nlattr *tb[IFLA_INET6_MAX + 1];
5715 struct inet6_dev *idev = NULL;
5716 int err;
5717
5718 if (dev) {
5719 idev = __in6_dev_get(dev);
5720 if (!idev)
5721 return -EAFNOSUPPORT;
5722 }
5723
5724 err = nla_parse_nested_deprecated(tb, IFLA_INET6_MAX, nla,
5725 inet6_af_policy, NULL);
5726 if (err)
5727 return err;
5728
5729 if (!tb[IFLA_INET6_TOKEN] && !tb[IFLA_INET6_ADDR_GEN_MODE])
5730 return -EINVAL;
5731
5732 if (tb[IFLA_INET6_ADDR_GEN_MODE]) {
5733 u8 mode = nla_get_u8(tb[IFLA_INET6_ADDR_GEN_MODE]);
5734
5735 if (check_addr_gen_mode(mode) < 0)
5736 return -EINVAL;
5737 if (dev && check_stable_privacy(idev, dev_net(dev), mode) < 0)
5738 return -EINVAL;
5739 }
5740
5741 return 0;
5742}
5743
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005744static int inet6_set_link_af(struct net_device *dev, const struct nlattr *nla)
5745{
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005746 struct inet6_dev *idev = __in6_dev_get(dev);
5747 struct nlattr *tb[IFLA_INET6_MAX + 1];
Maxim Mikityanskiy7dc2bcc2019-05-21 06:40:04 +00005748 int err;
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005749
Eric Dumazetdb3fa272020-02-07 07:16:37 -08005750 if (!idev)
5751 return -EAFNOSUPPORT;
5752
Johannes Berg8cb08172019-04-26 14:07:28 +02005753 if (nla_parse_nested_deprecated(tb, IFLA_INET6_MAX, nla, NULL, NULL) < 0)
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005754 BUG();
5755
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02005756 if (tb[IFLA_INET6_TOKEN]) {
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005757 err = inet6_set_iftoken(idev, nla_data(tb[IFLA_INET6_TOKEN]));
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02005758 if (err)
5759 return err;
5760 }
5761
5762 if (tb[IFLA_INET6_ADDR_GEN_MODE]) {
5763 u8 mode = nla_get_u8(tb[IFLA_INET6_ADDR_GEN_MODE]);
5764
Felix Jiad35a00b2017-01-26 16:59:17 +13005765 idev->cnf.addr_gen_mode = mode;
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02005766 }
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005767
Maxim Mikityanskiy7dc2bcc2019-05-21 06:40:04 +00005768 return 0;
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005769}
5770
Thomas Grafb382b192010-11-16 04:33:57 +00005771static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005772 u32 portid, u32 seq, int event, unsigned int flags)
Thomas Grafb382b192010-11-16 04:33:57 +00005773{
5774 struct net_device *dev = idev->dev;
5775 struct ifinfomsg *hdr;
5776 struct nlmsghdr *nlh;
5777 void *protoinfo;
5778
Eric W. Biederman15e47302012-09-07 20:12:54 +00005779 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*hdr), flags);
Ian Morris63159f22015-03-29 14:00:04 +01005780 if (!nlh)
Thomas Grafb382b192010-11-16 04:33:57 +00005781 return -EMSGSIZE;
5782
5783 hdr = nlmsg_data(nlh);
5784 hdr->ifi_family = AF_INET6;
5785 hdr->__ifi_pad = 0;
5786 hdr->ifi_type = dev->type;
5787 hdr->ifi_index = dev->ifindex;
5788 hdr->ifi_flags = dev_get_flags(dev);
5789 hdr->ifi_change = 0;
5790
David S. Millerc78679e2012-04-01 20:27:33 -04005791 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
5792 (dev->addr_len &&
5793 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
5794 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
Nicolas Dichtela54acb32015-04-02 17:07:00 +02005795 (dev->ifindex != dev_get_iflink(dev) &&
Andy Gospodarek03443382015-08-13 15:26:35 -04005796 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))) ||
5797 nla_put_u8(skb, IFLA_OPERSTATE,
5798 netif_running(dev) ? dev->operstate : IF_OPER_DOWN))
David S. Millerc78679e2012-04-01 20:27:33 -04005799 goto nla_put_failure;
Michal Kubecekae0be8d2019-04-26 11:13:06 +02005800 protoinfo = nla_nest_start_noflag(skb, IFLA_PROTINFO);
Ian Morris63159f22015-03-29 14:00:04 +01005801 if (!protoinfo)
Thomas Grafb382b192010-11-16 04:33:57 +00005802 goto nla_put_failure;
5803
Sowmini Varadhand5566fd2015-09-11 16:48:48 -04005804 if (inet6_fill_ifla6_attrs(skb, idev, 0) < 0)
Thomas Grafb382b192010-11-16 04:33:57 +00005805 goto nla_put_failure;
5806
Thomas Graf04561c12006-11-14 19:53:58 -08005807 nla_nest_end(skb, protoinfo);
Johannes Berg053c0952015-01-16 22:09:00 +01005808 nlmsg_end(skb, nlh);
5809 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005810
Thomas Graf04561c12006-11-14 19:53:58 -08005811nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08005812 nlmsg_cancel(skb, nlh);
5813 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005814}
5815
David Ahern786e0002018-10-07 20:16:33 -07005816static int inet6_valid_dump_ifinfo(const struct nlmsghdr *nlh,
5817 struct netlink_ext_ack *extack)
5818{
5819 struct ifinfomsg *ifm;
5820
5821 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
5822 NL_SET_ERR_MSG_MOD(extack, "Invalid header for link dump request");
5823 return -EINVAL;
5824 }
5825
5826 if (nlmsg_attrlen(nlh, sizeof(*ifm))) {
5827 NL_SET_ERR_MSG_MOD(extack, "Invalid data after header");
5828 return -EINVAL;
5829 }
5830
5831 ifm = nlmsg_data(nlh);
5832 if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
5833 ifm->ifi_change || ifm->ifi_index) {
5834 NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for dump request");
5835 return -EINVAL;
5836 }
5837
5838 return 0;
5839}
5840
Linus Torvalds1da177e2005-04-16 15:20:36 -07005841static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
5842{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09005843 struct net *net = sock_net(skb->sk);
Eric Dumazet84d26972009-11-09 12:11:28 +00005844 int h, s_h;
David S. Miller434a8a52009-11-11 18:53:00 -08005845 int idx = 0, s_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005846 struct net_device *dev;
5847 struct inet6_dev *idev;
Eric Dumazet84d26972009-11-09 12:11:28 +00005848 struct hlist_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005849
David Ahern786e0002018-10-07 20:16:33 -07005850 /* only requests using strict checking can pass data to
5851 * influence the dump
5852 */
5853 if (cb->strict_check) {
5854 int err = inet6_valid_dump_ifinfo(cb->nlh, cb->extack);
5855
5856 if (err < 0)
5857 return err;
5858 }
5859
Eric Dumazet84d26972009-11-09 12:11:28 +00005860 s_h = cb->args[0];
5861 s_idx = cb->args[1];
5862
5863 rcu_read_lock();
5864 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
5865 idx = 0;
5866 head = &net->dev_index_head[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -08005867 hlist_for_each_entry_rcu(dev, head, index_hlist) {
Eric Dumazet84d26972009-11-09 12:11:28 +00005868 if (idx < s_idx)
5869 goto cont;
5870 idev = __in6_dev_get(dev);
5871 if (!idev)
5872 goto cont;
5873 if (inet6_fill_ifinfo(skb, idev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005874 NETLINK_CB(cb->skb).portid,
Eric Dumazet84d26972009-11-09 12:11:28 +00005875 cb->nlh->nlmsg_seq,
Johannes Berg053c0952015-01-16 22:09:00 +01005876 RTM_NEWLINK, NLM_F_MULTI) < 0)
Eric Dumazet84d26972009-11-09 12:11:28 +00005877 goto out;
Pavel Emelianov7562f872007-05-03 15:13:45 -07005878cont:
Eric Dumazet84d26972009-11-09 12:11:28 +00005879 idx++;
5880 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005881 }
Eric Dumazet84d26972009-11-09 12:11:28 +00005882out:
5883 rcu_read_unlock();
5884 cb->args[1] = idx;
5885 cb->args[0] = h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005886
5887 return skb->len;
5888}
5889
5890void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
5891{
5892 struct sk_buff *skb;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09005893 struct net *net = dev_net(idev->dev);
Thomas Graf8d7a76c2006-08-15 00:35:47 -07005894 int err = -ENOBUFS;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09005895
Thomas Graf339bf982006-11-10 14:10:15 -08005896 skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC);
Ian Morris63159f22015-03-29 14:00:04 +01005897 if (!skb)
Thomas Graf8d7a76c2006-08-15 00:35:47 -07005898 goto errout;
5899
5900 err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08005901 if (err < 0) {
5902 /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */
5903 WARN_ON(err == -EMSGSIZE);
5904 kfree_skb(skb);
5905 goto errout;
5906 }
Nicolas Dichtel5f75a102010-12-07 23:38:31 +00005907 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFINFO, NULL, GFP_ATOMIC);
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08005908 return;
Thomas Graf8d7a76c2006-08-15 00:35:47 -07005909errout:
5910 if (err < 0)
Nicolas Dichtel5f75a102010-12-07 23:38:31 +00005911 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFINFO, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005912}
5913
Thomas Graf339bf982006-11-10 14:10:15 -08005914static inline size_t inet6_prefix_nlmsg_size(void)
5915{
5916 return NLMSG_ALIGN(sizeof(struct prefixmsg))
5917 + nla_total_size(sizeof(struct in6_addr))
5918 + nla_total_size(sizeof(struct prefix_cacheinfo));
5919}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07005920
Linus Torvalds1da177e2005-04-16 15:20:36 -07005921static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005922 struct prefix_info *pinfo, u32 portid, u32 seq,
Thomas Graf6051e2f2006-11-14 19:54:19 -08005923 int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005924{
Thomas Graf6051e2f2006-11-14 19:54:19 -08005925 struct prefixmsg *pmsg;
5926 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005927 struct prefix_cacheinfo ci;
5928
Eric W. Biederman15e47302012-09-07 20:12:54 +00005929 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*pmsg), flags);
Ian Morris63159f22015-03-29 14:00:04 +01005930 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08005931 return -EMSGSIZE;
Thomas Graf6051e2f2006-11-14 19:54:19 -08005932
5933 pmsg = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005934 pmsg->prefix_family = AF_INET6;
Patrick McHardy8a470772005-06-28 12:56:45 -07005935 pmsg->prefix_pad1 = 0;
5936 pmsg->prefix_pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005937 pmsg->prefix_ifindex = idev->dev->ifindex;
5938 pmsg->prefix_len = pinfo->prefix_len;
5939 pmsg->prefix_type = pinfo->type;
Patrick McHardy8a470772005-06-28 12:56:45 -07005940 pmsg->prefix_pad3 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005941 pmsg->prefix_flags = 0;
5942 if (pinfo->onlink)
5943 pmsg->prefix_flags |= IF_PREFIX_ONLINK;
5944 if (pinfo->autoconf)
5945 pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
5946
David S. Millerc78679e2012-04-01 20:27:33 -04005947 if (nla_put(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix))
5948 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005949 ci.preferred_time = ntohl(pinfo->prefered);
5950 ci.valid_time = ntohl(pinfo->valid);
David S. Millerc78679e2012-04-01 20:27:33 -04005951 if (nla_put(skb, PREFIX_CACHEINFO, sizeof(ci), &ci))
5952 goto nla_put_failure;
Johannes Berg053c0952015-01-16 22:09:00 +01005953 nlmsg_end(skb, nlh);
5954 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005955
Thomas Graf6051e2f2006-11-14 19:54:19 -08005956nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08005957 nlmsg_cancel(skb, nlh);
5958 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005959}
5960
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09005961static void inet6_prefix_notify(int event, struct inet6_dev *idev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005962 struct prefix_info *pinfo)
5963{
5964 struct sk_buff *skb;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09005965 struct net *net = dev_net(idev->dev);
Thomas Graf8c384bfa2006-08-15 00:36:07 -07005966 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005967
Thomas Graf339bf982006-11-10 14:10:15 -08005968 skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC);
Ian Morris63159f22015-03-29 14:00:04 +01005969 if (!skb)
Thomas Graf8c384bfa2006-08-15 00:36:07 -07005970 goto errout;
5971
5972 err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08005973 if (err < 0) {
5974 /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */
5975 WARN_ON(err == -EMSGSIZE);
5976 kfree_skb(skb);
5977 goto errout;
5978 }
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08005979 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC);
5980 return;
Thomas Graf8c384bfa2006-08-15 00:36:07 -07005981errout:
5982 if (err < 0)
Benjamin Thery6fda7352008-03-05 10:47:47 -08005983 rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005984}
5985
Linus Torvalds1da177e2005-04-16 15:20:36 -07005986static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
5987{
Nicolas Dichtel63998ac2013-03-22 06:28:43 +00005988 struct net *net = dev_net(ifp->idev->dev);
5989
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01005990 if (event)
5991 ASSERT_RTNL();
5992
Linus Torvalds1da177e2005-04-16 15:20:36 -07005993 inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
5994
5995 switch (event) {
5996 case RTM_NEWADDR:
Neil Horman95c385b2007-04-25 17:08:10 -07005997 /*
David Ahern2d819d252019-10-04 08:03:09 -07005998 * If the address was optimistic we inserted the route at the
5999 * start of our DAD process, so we don't need to do it again.
6000 * If the device was taken down in the middle of the DAD
6001 * cycle there is a race where we could get here without a
6002 * host route, so nothing to insert. That will be fixed when
6003 * the device is brought up.
Neil Horman95c385b2007-04-25 17:08:10 -07006004 */
David Ahern2d819d252019-10-04 08:03:09 -07006005 if (ifp->rt && !rcu_access_pointer(ifp->rt->fib6_node)) {
David Ahernafb1d4b52018-04-17 17:33:11 -07006006 ip6_ins_rt(net, ifp->rt);
David Ahern2d819d252019-10-04 08:03:09 -07006007 } else if (!ifp->rt && (ifp->idev->dev->flags & IFF_UP)) {
6008 pr_warn("BUG: Address %pI6c on device %s is missing its host route.\n",
6009 &ifp->addr, ifp->idev->dev->name);
6010 }
6011
Linus Torvalds1da177e2005-04-16 15:20:36 -07006012 if (ifp->idev->cnf.forwarding)
6013 addrconf_join_anycast(ifp);
Cong Wang7996c792013-05-22 05:41:06 +00006014 if (!ipv6_addr_any(&ifp->peer_addr))
Hangbin Liu07758eb2020-02-29 17:27:13 +08006015 addrconf_prefix_route(&ifp->peer_addr, 128,
6016 ifp->rt_priority, ifp->idev->dev,
6017 0, 0, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006018 break;
6019 case RTM_DELADDR:
6020 if (ifp->idev->cnf.forwarding)
6021 addrconf_leave_anycast(ifp);
6022 addrconf_leave_solict(ifp->idev, &ifp->addr);
Cong Wang7996c792013-05-22 05:41:06 +00006023 if (!ipv6_addr_any(&ifp->peer_addr)) {
David Ahern8d1c8022018-04-17 17:33:26 -07006024 struct fib6_info *rt;
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00006025
Nicolas Dichtele7478df2014-09-03 23:59:22 +02006026 rt = addrconf_get_prefix_route(&ifp->peer_addr, 128,
David Ahern2b2450c2019-03-27 20:53:52 -07006027 ifp->idev->dev, 0, 0,
6028 false);
Martin KaFai Lau8e3d5be2015-09-15 14:30:08 -07006029 if (rt)
David Ahernafb1d4b52018-04-17 17:33:11 -07006030 ip6_del_rt(net, rt);
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00006031 }
David Ahern38bd10c2016-04-21 20:56:12 -07006032 if (ifp->rt) {
David Ahern93531c62018-04-17 17:33:25 -07006033 ip6_del_rt(net, ifp->rt);
6034 ifp->rt = NULL;
David Ahern38bd10c2016-04-21 20:56:12 -07006035 }
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02006036 rt_genid_bump_ipv6(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006037 break;
6038 }
Nicolas Dichtel63998ac2013-03-22 06:28:43 +00006039 atomic_inc(&net->ipv6.dev_addr_genid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006040}
6041
6042static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
6043{
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07006044 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006045 if (likely(ifp->idev->dead == 0))
6046 __ipv6_ifa_notify(event, ifp);
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07006047 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006048}
6049
6050#ifdef CONFIG_SYSCTL
6051
6052static
Joe Perchesfe2c6332013-06-11 23:04:25 -07006053int addrconf_sysctl_forward(struct ctl_table *ctl, int write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006054 void __user *buffer, size_t *lenp, loff_t *ppos)
6055{
6056 int *valp = ctl->data;
6057 int val = *valp;
Eric W. Biederman88af1822010-02-19 13:22:59 +00006058 loff_t pos = *ppos;
Joe Perchesfe2c6332013-06-11 23:04:25 -07006059 struct ctl_table lctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006060 int ret;
6061
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00006062 /*
6063 * ctl->data points to idev->cnf.forwarding, we should
6064 * not modify it until we get the rtnl lock.
6065 */
6066 lctl = *ctl;
6067 lctl.data = &val;
6068
6069 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006070
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -08006071 if (write)
Stephen Hemmingerb325fdd2009-02-26 06:55:31 +00006072 ret = addrconf_fixup_forwarding(ctl, valp, val);
Eric W. Biederman88af1822010-02-19 13:22:59 +00006073 if (ret)
6074 *ppos = pos;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09006075 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006076}
6077
Marcelo Leitner77751422015-02-23 11:17:13 -03006078static
6079int addrconf_sysctl_mtu(struct ctl_table *ctl, int write,
6080 void __user *buffer, size_t *lenp, loff_t *ppos)
6081{
6082 struct inet6_dev *idev = ctl->extra1;
6083 int min_mtu = IPV6_MIN_MTU;
6084 struct ctl_table lctl;
6085
6086 lctl = *ctl;
6087 lctl.extra1 = &min_mtu;
6088 lctl.extra2 = idev ? &idev->dev->mtu : NULL;
6089
6090 return proc_dointvec_minmax(&lctl, write, buffer, lenp, ppos);
6091}
6092
Brian Haley56d417b2009-06-01 03:07:33 -07006093static void dev_disable_change(struct inet6_dev *idev)
6094{
Cong Wang75538c22013-05-29 11:30:50 +08006095 struct netdev_notifier_info info;
6096
Brian Haley56d417b2009-06-01 03:07:33 -07006097 if (!idev || !idev->dev)
6098 return;
6099
Cong Wang75538c22013-05-29 11:30:50 +08006100 netdev_notifier_info_init(&info, idev->dev);
Brian Haley56d417b2009-06-01 03:07:33 -07006101 if (idev->cnf.disable_ipv6)
Cong Wang75538c22013-05-29 11:30:50 +08006102 addrconf_notify(NULL, NETDEV_DOWN, &info);
Brian Haley56d417b2009-06-01 03:07:33 -07006103 else
Cong Wang75538c22013-05-29 11:30:50 +08006104 addrconf_notify(NULL, NETDEV_UP, &info);
Brian Haley56d417b2009-06-01 03:07:33 -07006105}
6106
6107static void addrconf_disable_change(struct net *net, __s32 newf)
6108{
6109 struct net_device *dev;
6110 struct inet6_dev *idev;
6111
Kefeng Wang03e4def2017-01-19 16:26:21 +08006112 for_each_netdev(net, dev) {
Brian Haley56d417b2009-06-01 03:07:33 -07006113 idev = __in6_dev_get(dev);
6114 if (idev) {
6115 int changed = (!idev->cnf.disable_ipv6) ^ (!newf);
6116 idev->cnf.disable_ipv6 = newf;
6117 if (changed)
6118 dev_disable_change(idev);
6119 }
Brian Haley56d417b2009-06-01 03:07:33 -07006120 }
Brian Haley56d417b2009-06-01 03:07:33 -07006121}
6122
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00006123static int addrconf_disable_ipv6(struct ctl_table *table, int *p, int newf)
Brian Haley56d417b2009-06-01 03:07:33 -07006124{
6125 struct net *net;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00006126 int old;
6127
6128 if (!rtnl_trylock())
6129 return restart_syscall();
Brian Haley56d417b2009-06-01 03:07:33 -07006130
6131 net = (struct net *)table->extra2;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00006132 old = *p;
6133 *p = newf;
Brian Haley56d417b2009-06-01 03:07:33 -07006134
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00006135 if (p == &net->ipv6.devconf_dflt->disable_ipv6) {
6136 rtnl_unlock();
Brian Haley56d417b2009-06-01 03:07:33 -07006137 return 0;
Eric W. Biederman88af1822010-02-19 13:22:59 +00006138 }
Brian Haley56d417b2009-06-01 03:07:33 -07006139
6140 if (p == &net->ipv6.devconf_all->disable_ipv6) {
Brian Haley56d417b2009-06-01 03:07:33 -07006141 net->ipv6.devconf_dflt->disable_ipv6 = newf;
6142 addrconf_disable_change(net, newf);
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00006143 } else if ((!newf) ^ (!old))
Brian Haley56d417b2009-06-01 03:07:33 -07006144 dev_disable_change((struct inet6_dev *)table->extra1);
6145
6146 rtnl_unlock();
6147 return 0;
6148}
6149
6150static
Joe Perchesfe2c6332013-06-11 23:04:25 -07006151int addrconf_sysctl_disable(struct ctl_table *ctl, int write,
Brian Haley56d417b2009-06-01 03:07:33 -07006152 void __user *buffer, size_t *lenp, loff_t *ppos)
6153{
6154 int *valp = ctl->data;
6155 int val = *valp;
Eric W. Biederman88af1822010-02-19 13:22:59 +00006156 loff_t pos = *ppos;
Joe Perchesfe2c6332013-06-11 23:04:25 -07006157 struct ctl_table lctl;
Brian Haley56d417b2009-06-01 03:07:33 -07006158 int ret;
6159
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00006160 /*
6161 * ctl->data points to idev->cnf.disable_ipv6, we should
6162 * not modify it until we get the rtnl lock.
6163 */
6164 lctl = *ctl;
6165 lctl.data = &val;
6166
6167 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
Brian Haley56d417b2009-06-01 03:07:33 -07006168
6169 if (write)
6170 ret = addrconf_disable_ipv6(ctl, valp, val);
Eric W. Biederman88af1822010-02-19 13:22:59 +00006171 if (ret)
6172 *ppos = pos;
Brian Haley56d417b2009-06-01 03:07:33 -07006173 return ret;
6174}
6175
stephen hemmingerc92d5492013-12-17 22:37:14 -08006176static
6177int addrconf_sysctl_proxy_ndp(struct ctl_table *ctl, int write,
6178 void __user *buffer, size_t *lenp, loff_t *ppos)
6179{
6180 int *valp = ctl->data;
6181 int ret;
6182 int old, new;
6183
6184 old = *valp;
6185 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
6186 new = *valp;
6187
6188 if (write && old != new) {
6189 struct net *net = ctl->extra2;
6190
6191 if (!rtnl_trylock())
6192 return restart_syscall();
6193
6194 if (valp == &net->ipv6.devconf_dflt->proxy_ndp)
David Ahern85b3daa2017-03-28 14:28:04 -07006195 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
6196 NETCONFA_PROXY_NEIGH,
stephen hemmingerc92d5492013-12-17 22:37:14 -08006197 NETCONFA_IFINDEX_DEFAULT,
6198 net->ipv6.devconf_dflt);
6199 else if (valp == &net->ipv6.devconf_all->proxy_ndp)
David Ahern85b3daa2017-03-28 14:28:04 -07006200 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
6201 NETCONFA_PROXY_NEIGH,
stephen hemmingerc92d5492013-12-17 22:37:14 -08006202 NETCONFA_IFINDEX_ALL,
6203 net->ipv6.devconf_all);
6204 else {
6205 struct inet6_dev *idev = ctl->extra1;
6206
David Ahern85b3daa2017-03-28 14:28:04 -07006207 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
6208 NETCONFA_PROXY_NEIGH,
stephen hemmingerc92d5492013-12-17 22:37:14 -08006209 idev->dev->ifindex,
6210 &idev->cnf);
6211 }
6212 rtnl_unlock();
6213 }
6214
6215 return ret;
6216}
6217
Felix Jiad35a00b2017-01-26 16:59:17 +13006218static int addrconf_sysctl_addr_gen_mode(struct ctl_table *ctl, int write,
6219 void __user *buffer, size_t *lenp,
6220 loff_t *ppos)
6221{
6222 int ret = 0;
Sabrina Dubrocac6dbf7a2018-07-09 12:25:14 +02006223 u32 new_val;
Felix Jiad35a00b2017-01-26 16:59:17 +13006224 struct inet6_dev *idev = (struct inet6_dev *)ctl->extra1;
6225 struct net *net = (struct net *)ctl->extra2;
Sabrina Dubrocac6dbf7a2018-07-09 12:25:14 +02006226 struct ctl_table tmp = {
6227 .data = &new_val,
6228 .maxlen = sizeof(new_val),
6229 .mode = ctl->mode,
6230 };
Felix Jiad35a00b2017-01-26 16:59:17 +13006231
Felix Jia8c171d62017-02-27 12:41:23 +13006232 if (!rtnl_trylock())
6233 return restart_syscall();
6234
Sabrina Dubrocac6dbf7a2018-07-09 12:25:14 +02006235 new_val = *((u32 *)ctl->data);
6236
6237 ret = proc_douintvec(&tmp, write, buffer, lenp, ppos);
6238 if (ret != 0)
6239 goto out;
Felix Jiad35a00b2017-01-26 16:59:17 +13006240
6241 if (write) {
Felix Jia8c171d62017-02-27 12:41:23 +13006242 if (check_addr_gen_mode(new_val) < 0) {
6243 ret = -EINVAL;
6244 goto out;
6245 }
Felix Jiad35a00b2017-01-26 16:59:17 +13006246
Sabrina Dubrocac6dbf7a2018-07-09 12:25:14 +02006247 if (idev) {
Felix Jia8c171d62017-02-27 12:41:23 +13006248 if (check_stable_privacy(idev, net, new_val) < 0) {
6249 ret = -EINVAL;
6250 goto out;
6251 }
Felix Jiad35a00b2017-01-26 16:59:17 +13006252
6253 if (idev->cnf.addr_gen_mode != new_val) {
6254 idev->cnf.addr_gen_mode = new_val;
Felix Jiad35a00b2017-01-26 16:59:17 +13006255 addrconf_dev_config(idev->dev);
Felix Jiad35a00b2017-01-26 16:59:17 +13006256 }
Sabrina Dubrocaf24c5982018-07-09 12:25:17 +02006257 } else if (&net->ipv6.devconf_all->addr_gen_mode == ctl->data) {
6258 struct net_device *dev;
6259
6260 net->ipv6.devconf_dflt->addr_gen_mode = new_val;
6261 for_each_netdev(net, dev) {
6262 idev = __in6_dev_get(dev);
6263 if (idev &&
6264 idev->cnf.addr_gen_mode != new_val) {
6265 idev->cnf.addr_gen_mode = new_val;
6266 addrconf_dev_config(idev->dev);
6267 }
6268 }
Felix Jiad35a00b2017-01-26 16:59:17 +13006269 }
Sabrina Dubrocac6dbf7a2018-07-09 12:25:14 +02006270
6271 *((u32 *)ctl->data) = new_val;
Felix Jiad35a00b2017-01-26 16:59:17 +13006272 }
6273
Felix Jia8c171d62017-02-27 12:41:23 +13006274out:
6275 rtnl_unlock();
6276
Felix Jiad35a00b2017-01-26 16:59:17 +13006277 return ret;
6278}
6279
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01006280static int addrconf_sysctl_stable_secret(struct ctl_table *ctl, int write,
6281 void __user *buffer, size_t *lenp,
6282 loff_t *ppos)
6283{
6284 int err;
6285 struct in6_addr addr;
6286 char str[IPV6_MAX_STRLEN];
6287 struct ctl_table lctl = *ctl;
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01006288 struct net *net = ctl->extra2;
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01006289 struct ipv6_stable_secret *secret = ctl->data;
6290
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01006291 if (&net->ipv6.devconf_all->stable_secret == ctl->data)
6292 return -EIO;
6293
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01006294 lctl.maxlen = IPV6_MAX_STRLEN;
6295 lctl.data = str;
6296
6297 if (!rtnl_trylock())
6298 return restart_syscall();
6299
6300 if (!write && !secret->initialized) {
6301 err = -EIO;
6302 goto out;
6303 }
6304
WANG Cong5449a5c2015-12-21 10:55:45 -08006305 err = snprintf(str, sizeof(str), "%pI6", &secret->secret);
6306 if (err >= sizeof(str)) {
6307 err = -EIO;
6308 goto out;
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01006309 }
6310
6311 err = proc_dostring(&lctl, write, buffer, lenp, ppos);
6312 if (err || !write)
6313 goto out;
6314
6315 if (in6_pton(str, -1, addr.in6_u.u6_addr8, -1, NULL) != 1) {
6316 err = -EIO;
6317 goto out;
6318 }
6319
6320 secret->initialized = true;
6321 secret->secret = addr;
6322
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01006323 if (&net->ipv6.devconf_dflt->stable_secret == ctl->data) {
6324 struct net_device *dev;
6325
6326 for_each_netdev(net, dev) {
6327 struct inet6_dev *idev = __in6_dev_get(dev);
6328
6329 if (idev) {
Felix Jiad35a00b2017-01-26 16:59:17 +13006330 idev->cnf.addr_gen_mode =
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01006331 IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
6332 }
6333 }
6334 } else {
6335 struct inet6_dev *idev = ctl->extra1;
6336
Felix Jiad35a00b2017-01-26 16:59:17 +13006337 idev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01006338 }
6339
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01006340out:
6341 rtnl_unlock();
6342
6343 return err;
6344}
stephen hemmingerc92d5492013-12-17 22:37:14 -08006345
Andy Gospodarek35103d12015-08-13 10:39:01 -04006346static
6347int addrconf_sysctl_ignore_routes_with_linkdown(struct ctl_table *ctl,
6348 int write,
6349 void __user *buffer,
6350 size_t *lenp,
6351 loff_t *ppos)
6352{
6353 int *valp = ctl->data;
6354 int val = *valp;
6355 loff_t pos = *ppos;
6356 struct ctl_table lctl;
6357 int ret;
6358
6359 /* ctl->data points to idev->cnf.ignore_routes_when_linkdown
6360 * we should not modify it until we get the rtnl lock.
6361 */
6362 lctl = *ctl;
6363 lctl.data = &val;
6364
6365 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
6366
6367 if (write)
6368 ret = addrconf_fixup_linkdown(ctl, valp, val);
6369 if (ret)
6370 *ppos = pos;
6371 return ret;
6372}
6373
David Forsterdf789fe2017-02-23 16:27:18 +00006374static
6375void addrconf_set_nopolicy(struct rt6_info *rt, int action)
6376{
6377 if (rt) {
6378 if (action)
6379 rt->dst.flags |= DST_NOPOLICY;
6380 else
6381 rt->dst.flags &= ~DST_NOPOLICY;
6382 }
6383}
6384
6385static
6386void addrconf_disable_policy_idev(struct inet6_dev *idev, int val)
6387{
6388 struct inet6_ifaddr *ifa;
6389
6390 read_lock_bh(&idev->lock);
6391 list_for_each_entry(ifa, &idev->addr_list, if_list) {
6392 spin_lock(&ifa->lock);
6393 if (ifa->rt) {
David Ahernf88d8ea2019-06-03 20:19:52 -07006394 /* host routes only use builtin fib6_nh */
David Ahern1cf844c2019-05-22 20:27:59 -07006395 struct fib6_nh *nh = ifa->rt->fib6_nh;
David Forsterdf789fe2017-02-23 16:27:18 +00006396 int cpu;
6397
Wei Wang66f5d6c2017-10-06 12:06:10 -07006398 rcu_read_lock();
David Ahern3b6761d2018-04-17 17:33:20 -07006399 ifa->rt->dst_nopolicy = val ? true : false;
David Ahernf40b6ae2019-05-22 20:27:55 -07006400 if (nh->rt6i_pcpu) {
David Forsterdf789fe2017-02-23 16:27:18 +00006401 for_each_possible_cpu(cpu) {
6402 struct rt6_info **rtp;
6403
David Ahernf40b6ae2019-05-22 20:27:55 -07006404 rtp = per_cpu_ptr(nh->rt6i_pcpu, cpu);
David Forsterdf789fe2017-02-23 16:27:18 +00006405 addrconf_set_nopolicy(*rtp, val);
6406 }
6407 }
Wei Wang66f5d6c2017-10-06 12:06:10 -07006408 rcu_read_unlock();
David Forsterdf789fe2017-02-23 16:27:18 +00006409 }
6410 spin_unlock(&ifa->lock);
6411 }
6412 read_unlock_bh(&idev->lock);
6413}
6414
6415static
6416int addrconf_disable_policy(struct ctl_table *ctl, int *valp, int val)
6417{
6418 struct inet6_dev *idev;
6419 struct net *net;
6420
6421 if (!rtnl_trylock())
6422 return restart_syscall();
6423
6424 *valp = val;
6425
6426 net = (struct net *)ctl->extra2;
6427 if (valp == &net->ipv6.devconf_dflt->disable_policy) {
6428 rtnl_unlock();
6429 return 0;
6430 }
6431
6432 if (valp == &net->ipv6.devconf_all->disable_policy) {
6433 struct net_device *dev;
6434
6435 for_each_netdev(net, dev) {
6436 idev = __in6_dev_get(dev);
6437 if (idev)
6438 addrconf_disable_policy_idev(idev, val);
6439 }
6440 } else {
6441 idev = (struct inet6_dev *)ctl->extra1;
6442 addrconf_disable_policy_idev(idev, val);
6443 }
6444
6445 rtnl_unlock();
6446 return 0;
6447}
6448
6449static
6450int addrconf_sysctl_disable_policy(struct ctl_table *ctl, int write,
6451 void __user *buffer, size_t *lenp,
6452 loff_t *ppos)
6453{
6454 int *valp = ctl->data;
6455 int val = *valp;
6456 loff_t pos = *ppos;
6457 struct ctl_table lctl;
6458 int ret;
6459
6460 lctl = *ctl;
6461 lctl.data = &val;
6462 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
6463
6464 if (write && (*valp != val))
6465 ret = addrconf_disable_policy(ctl, valp, val);
6466
6467 if (ret)
6468 *ppos = pos;
6469
6470 return ret;
6471}
6472
Maciej Żenczykowskicb4a4c62016-10-07 01:00:49 -07006473static int minus_one = -1;
Maciej Żenczykowskicb9e6842016-09-29 00:33:43 -07006474static const int two_five_five = 255;
6475
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006476static const struct ctl_table addrconf_sysctl[] = {
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006477 {
6478 .procname = "forwarding",
6479 .data = &ipv6_devconf.forwarding,
6480 .maxlen = sizeof(int),
6481 .mode = 0644,
6482 .proc_handler = addrconf_sysctl_forward,
6483 },
6484 {
6485 .procname = "hop_limit",
6486 .data = &ipv6_devconf.hop_limit,
6487 .maxlen = sizeof(int),
6488 .mode = 0644,
Maciej Żenczykowskicb9e6842016-09-29 00:33:43 -07006489 .proc_handler = proc_dointvec_minmax,
Matteo Croceeec48442019-07-18 15:58:50 -07006490 .extra1 = (void *)SYSCTL_ONE,
Maciej Żenczykowskicb9e6842016-09-29 00:33:43 -07006491 .extra2 = (void *)&two_five_five,
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006492 },
6493 {
6494 .procname = "mtu",
6495 .data = &ipv6_devconf.mtu6,
6496 .maxlen = sizeof(int),
6497 .mode = 0644,
6498 .proc_handler = addrconf_sysctl_mtu,
6499 },
6500 {
6501 .procname = "accept_ra",
6502 .data = &ipv6_devconf.accept_ra,
6503 .maxlen = sizeof(int),
6504 .mode = 0644,
6505 .proc_handler = proc_dointvec,
6506 },
6507 {
6508 .procname = "accept_redirects",
6509 .data = &ipv6_devconf.accept_redirects,
6510 .maxlen = sizeof(int),
6511 .mode = 0644,
6512 .proc_handler = proc_dointvec,
6513 },
6514 {
6515 .procname = "autoconf",
6516 .data = &ipv6_devconf.autoconf,
6517 .maxlen = sizeof(int),
6518 .mode = 0644,
6519 .proc_handler = proc_dointvec,
6520 },
6521 {
6522 .procname = "dad_transmits",
6523 .data = &ipv6_devconf.dad_transmits,
6524 .maxlen = sizeof(int),
6525 .mode = 0644,
6526 .proc_handler = proc_dointvec,
6527 },
6528 {
6529 .procname = "router_solicitations",
6530 .data = &ipv6_devconf.rtr_solicits,
6531 .maxlen = sizeof(int),
6532 .mode = 0644,
Maciej Żenczykowskicb4a4c62016-10-07 01:00:49 -07006533 .proc_handler = proc_dointvec_minmax,
6534 .extra1 = &minus_one,
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006535 },
6536 {
6537 .procname = "router_solicitation_interval",
6538 .data = &ipv6_devconf.rtr_solicit_interval,
6539 .maxlen = sizeof(int),
6540 .mode = 0644,
6541 .proc_handler = proc_dointvec_jiffies,
6542 },
6543 {
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07006544 .procname = "router_solicitation_max_interval",
6545 .data = &ipv6_devconf.rtr_solicit_max_interval,
6546 .maxlen = sizeof(int),
6547 .mode = 0644,
6548 .proc_handler = proc_dointvec_jiffies,
6549 },
6550 {
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006551 .procname = "router_solicitation_delay",
6552 .data = &ipv6_devconf.rtr_solicit_delay,
6553 .maxlen = sizeof(int),
6554 .mode = 0644,
6555 .proc_handler = proc_dointvec_jiffies,
6556 },
6557 {
6558 .procname = "force_mld_version",
6559 .data = &ipv6_devconf.force_mld_version,
6560 .maxlen = sizeof(int),
6561 .mode = 0644,
6562 .proc_handler = proc_dointvec,
6563 },
6564 {
6565 .procname = "mldv1_unsolicited_report_interval",
6566 .data =
6567 &ipv6_devconf.mldv1_unsolicited_report_interval,
6568 .maxlen = sizeof(int),
6569 .mode = 0644,
6570 .proc_handler = proc_dointvec_ms_jiffies,
6571 },
6572 {
6573 .procname = "mldv2_unsolicited_report_interval",
6574 .data =
6575 &ipv6_devconf.mldv2_unsolicited_report_interval,
6576 .maxlen = sizeof(int),
6577 .mode = 0644,
6578 .proc_handler = proc_dointvec_ms_jiffies,
6579 },
6580 {
6581 .procname = "use_tempaddr",
6582 .data = &ipv6_devconf.use_tempaddr,
6583 .maxlen = sizeof(int),
6584 .mode = 0644,
6585 .proc_handler = proc_dointvec,
6586 },
6587 {
6588 .procname = "temp_valid_lft",
6589 .data = &ipv6_devconf.temp_valid_lft,
6590 .maxlen = sizeof(int),
6591 .mode = 0644,
6592 .proc_handler = proc_dointvec,
6593 },
6594 {
6595 .procname = "temp_prefered_lft",
6596 .data = &ipv6_devconf.temp_prefered_lft,
6597 .maxlen = sizeof(int),
6598 .mode = 0644,
6599 .proc_handler = proc_dointvec,
6600 },
6601 {
6602 .procname = "regen_max_retry",
6603 .data = &ipv6_devconf.regen_max_retry,
6604 .maxlen = sizeof(int),
6605 .mode = 0644,
6606 .proc_handler = proc_dointvec,
6607 },
6608 {
6609 .procname = "max_desync_factor",
6610 .data = &ipv6_devconf.max_desync_factor,
6611 .maxlen = sizeof(int),
6612 .mode = 0644,
6613 .proc_handler = proc_dointvec,
6614 },
6615 {
6616 .procname = "max_addresses",
6617 .data = &ipv6_devconf.max_addresses,
6618 .maxlen = sizeof(int),
6619 .mode = 0644,
6620 .proc_handler = proc_dointvec,
6621 },
6622 {
6623 .procname = "accept_ra_defrtr",
6624 .data = &ipv6_devconf.accept_ra_defrtr,
6625 .maxlen = sizeof(int),
6626 .mode = 0644,
6627 .proc_handler = proc_dointvec,
6628 },
6629 {
6630 .procname = "accept_ra_min_hop_limit",
6631 .data = &ipv6_devconf.accept_ra_min_hop_limit,
6632 .maxlen = sizeof(int),
6633 .mode = 0644,
6634 .proc_handler = proc_dointvec,
6635 },
6636 {
6637 .procname = "accept_ra_pinfo",
6638 .data = &ipv6_devconf.accept_ra_pinfo,
6639 .maxlen = sizeof(int),
6640 .mode = 0644,
6641 .proc_handler = proc_dointvec,
6642 },
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08006643#ifdef CONFIG_IPV6_ROUTER_PREF
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006644 {
6645 .procname = "accept_ra_rtr_pref",
6646 .data = &ipv6_devconf.accept_ra_rtr_pref,
6647 .maxlen = sizeof(int),
6648 .mode = 0644,
6649 .proc_handler = proc_dointvec,
6650 },
6651 {
6652 .procname = "router_probe_interval",
6653 .data = &ipv6_devconf.rtr_probe_interval,
6654 .maxlen = sizeof(int),
6655 .mode = 0644,
6656 .proc_handler = proc_dointvec_jiffies,
6657 },
Neil Hormanfa03ef32007-01-30 14:30:10 -08006658#ifdef CONFIG_IPV6_ROUTE_INFO
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006659 {
Joel Scherpelzbbea1242017-03-22 18:19:04 +09006660 .procname = "accept_ra_rt_info_min_plen",
6661 .data = &ipv6_devconf.accept_ra_rt_info_min_plen,
6662 .maxlen = sizeof(int),
6663 .mode = 0644,
6664 .proc_handler = proc_dointvec,
6665 },
6666 {
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006667 .procname = "accept_ra_rt_info_max_plen",
6668 .data = &ipv6_devconf.accept_ra_rt_info_max_plen,
6669 .maxlen = sizeof(int),
6670 .mode = 0644,
6671 .proc_handler = proc_dointvec,
6672 },
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -08006673#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08006674#endif
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006675 {
6676 .procname = "proxy_ndp",
6677 .data = &ipv6_devconf.proxy_ndp,
6678 .maxlen = sizeof(int),
6679 .mode = 0644,
6680 .proc_handler = addrconf_sysctl_proxy_ndp,
6681 },
6682 {
6683 .procname = "accept_source_route",
6684 .data = &ipv6_devconf.accept_source_route,
6685 .maxlen = sizeof(int),
6686 .mode = 0644,
6687 .proc_handler = proc_dointvec,
6688 },
Neil Horman95c385b2007-04-25 17:08:10 -07006689#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006690 {
6691 .procname = "optimistic_dad",
6692 .data = &ipv6_devconf.optimistic_dad,
6693 .maxlen = sizeof(int),
6694 .mode = 0644,
6695 .proc_handler = proc_dointvec,
6696 },
6697 {
6698 .procname = "use_optimistic",
6699 .data = &ipv6_devconf.use_optimistic,
6700 .maxlen = sizeof(int),
6701 .mode = 0644,
6702 .proc_handler = proc_dointvec,
6703 },
Neil Horman95c385b2007-04-25 17:08:10 -07006704#endif
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09006705#ifdef CONFIG_IPV6_MROUTE
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006706 {
6707 .procname = "mc_forwarding",
6708 .data = &ipv6_devconf.mc_forwarding,
6709 .maxlen = sizeof(int),
6710 .mode = 0444,
6711 .proc_handler = proc_dointvec,
6712 },
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09006713#endif
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006714 {
6715 .procname = "disable_ipv6",
6716 .data = &ipv6_devconf.disable_ipv6,
6717 .maxlen = sizeof(int),
6718 .mode = 0644,
6719 .proc_handler = addrconf_sysctl_disable,
6720 },
6721 {
6722 .procname = "accept_dad",
6723 .data = &ipv6_devconf.accept_dad,
6724 .maxlen = sizeof(int),
6725 .mode = 0644,
6726 .proc_handler = proc_dointvec,
6727 },
6728 {
6729 .procname = "force_tllao",
6730 .data = &ipv6_devconf.force_tllao,
6731 .maxlen = sizeof(int),
6732 .mode = 0644,
6733 .proc_handler = proc_dointvec
6734 },
6735 {
6736 .procname = "ndisc_notify",
6737 .data = &ipv6_devconf.ndisc_notify,
6738 .maxlen = sizeof(int),
6739 .mode = 0644,
6740 .proc_handler = proc_dointvec
6741 },
6742 {
6743 .procname = "suppress_frag_ndisc",
6744 .data = &ipv6_devconf.suppress_frag_ndisc,
6745 .maxlen = sizeof(int),
6746 .mode = 0644,
6747 .proc_handler = proc_dointvec
6748 },
6749 {
6750 .procname = "accept_ra_from_local",
6751 .data = &ipv6_devconf.accept_ra_from_local,
6752 .maxlen = sizeof(int),
6753 .mode = 0644,
6754 .proc_handler = proc_dointvec,
6755 },
6756 {
6757 .procname = "accept_ra_mtu",
6758 .data = &ipv6_devconf.accept_ra_mtu,
6759 .maxlen = sizeof(int),
6760 .mode = 0644,
6761 .proc_handler = proc_dointvec,
6762 },
6763 {
6764 .procname = "stable_secret",
6765 .data = &ipv6_devconf.stable_secret,
6766 .maxlen = IPV6_MAX_STRLEN,
6767 .mode = 0600,
6768 .proc_handler = addrconf_sysctl_stable_secret,
6769 },
6770 {
6771 .procname = "use_oif_addrs_only",
6772 .data = &ipv6_devconf.use_oif_addrs_only,
6773 .maxlen = sizeof(int),
6774 .mode = 0644,
6775 .proc_handler = proc_dointvec,
6776 },
6777 {
6778 .procname = "ignore_routes_with_linkdown",
6779 .data = &ipv6_devconf.ignore_routes_with_linkdown,
6780 .maxlen = sizeof(int),
6781 .mode = 0644,
6782 .proc_handler = addrconf_sysctl_ignore_routes_with_linkdown,
6783 },
6784 {
6785 .procname = "drop_unicast_in_l2_multicast",
6786 .data = &ipv6_devconf.drop_unicast_in_l2_multicast,
6787 .maxlen = sizeof(int),
6788 .mode = 0644,
6789 .proc_handler = proc_dointvec,
6790 },
6791 {
6792 .procname = "drop_unsolicited_na",
6793 .data = &ipv6_devconf.drop_unsolicited_na,
6794 .maxlen = sizeof(int),
6795 .mode = 0644,
6796 .proc_handler = proc_dointvec,
6797 },
6798 {
6799 .procname = "keep_addr_on_down",
6800 .data = &ipv6_devconf.keep_addr_on_down,
6801 .maxlen = sizeof(int),
6802 .mode = 0644,
6803 .proc_handler = proc_dointvec,
David Ahernf1705ec2016-02-24 09:25:37 -08006804
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006805 },
6806 {
David Lebrun1ababeb2016-11-08 14:57:39 +01006807 .procname = "seg6_enabled",
6808 .data = &ipv6_devconf.seg6_enabled,
6809 .maxlen = sizeof(int),
6810 .mode = 0644,
6811 .proc_handler = proc_dointvec,
6812 },
David Lebrunbf355b82016-11-08 14:57:42 +01006813#ifdef CONFIG_IPV6_SEG6_HMAC
6814 {
6815 .procname = "seg6_require_hmac",
6816 .data = &ipv6_devconf.seg6_require_hmac,
6817 .maxlen = sizeof(int),
6818 .mode = 0644,
6819 .proc_handler = proc_dointvec,
6820 },
6821#endif
David Lebrun1ababeb2016-11-08 14:57:39 +01006822 {
Erik Nordmarkadc176c2016-12-02 14:00:08 -08006823 .procname = "enhanced_dad",
6824 .data = &ipv6_devconf.enhanced_dad,
6825 .maxlen = sizeof(int),
6826 .mode = 0644,
6827 .proc_handler = proc_dointvec,
6828 },
6829 {
Felix Jiad35a00b2017-01-26 16:59:17 +13006830 .procname = "addr_gen_mode",
6831 .data = &ipv6_devconf.addr_gen_mode,
6832 .maxlen = sizeof(int),
6833 .mode = 0644,
6834 .proc_handler = addrconf_sysctl_addr_gen_mode,
6835 },
6836 {
David Forsterdf789fe2017-02-23 16:27:18 +00006837 .procname = "disable_policy",
6838 .data = &ipv6_devconf.disable_policy,
6839 .maxlen = sizeof(int),
6840 .mode = 0644,
6841 .proc_handler = addrconf_sysctl_disable_policy,
6842 },
6843 {
Maciej Żenczykowski2210d6b2017-11-07 21:52:09 -08006844 .procname = "ndisc_tclass",
6845 .data = &ipv6_devconf.ndisc_tclass,
6846 .maxlen = sizeof(int),
6847 .mode = 0644,
6848 .proc_handler = proc_dointvec_minmax,
Matteo Croceeec48442019-07-18 15:58:50 -07006849 .extra1 = (void *)SYSCTL_ZERO,
Maciej Żenczykowski2210d6b2017-11-07 21:52:09 -08006850 .extra2 = (void *)&two_five_five,
6851 },
6852 {
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006853 /* sentinel */
6854 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006855};
6856
Pavel Emelyanovbff16c22008-01-10 17:42:13 -08006857static int __addrconf_sysctl_register(struct net *net, char *dev_name,
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08006858 struct inet6_dev *idev, struct ipv6_devconf *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006859{
Nicolas Dichtel29c994e2016-08-30 10:09:22 +02006860 int i, ifindex;
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006861 struct ctl_table *table;
Eric W. Biederman6105e292012-04-19 13:41:24 +00006862 char path[sizeof("net/ipv6/conf/") + IFNAMSIZ];
Pavel Emelyanov1dab6222007-12-02 00:59:38 +11006863
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006864 table = kmemdup(addrconf_sysctl, sizeof(addrconf_sysctl), GFP_KERNEL);
6865 if (!table)
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11006866 goto out;
6867
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006868 for (i = 0; table[i].data; i++) {
6869 table[i].data += (char *)p - (char *)&ipv6_devconf;
Maciej Żenczykowskicb9e6842016-09-29 00:33:43 -07006870 /* If one of these is already set, then it is not safe to
6871 * overwrite either of them: this makes proc_dointvec_minmax
6872 * usable.
6873 */
6874 if (!table[i].extra1 && !table[i].extra2) {
6875 table[i].extra1 = idev; /* embedded; no ref */
6876 table[i].extra2 = net;
6877 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006878 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006879
Eric W. Biederman6105e292012-04-19 13:41:24 +00006880 snprintf(path, sizeof(path), "net/ipv6/conf/%s", dev_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006881
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006882 p->sysctl_header = register_net_sysctl(net, path, table);
6883 if (!p->sysctl_header)
Eric W. Biederman6105e292012-04-19 13:41:24 +00006884 goto free;
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11006885
Nicolas Dichtel29c994e2016-08-30 10:09:22 +02006886 if (!strcmp(dev_name, "all"))
6887 ifindex = NETCONFA_IFINDEX_ALL;
6888 else if (!strcmp(dev_name, "default"))
6889 ifindex = NETCONFA_IFINDEX_DEFAULT;
6890 else
6891 ifindex = idev->dev->ifindex;
David Ahern85b3daa2017-03-28 14:28:04 -07006892 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF, NETCONFA_ALL,
6893 ifindex, p);
Pavel Emelyanov95897312008-01-10 17:41:45 -08006894 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006895
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11006896free:
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006897 kfree(table);
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11006898out:
Pavel Emelyanov95897312008-01-10 17:41:45 -08006899 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006900}
6901
David Ahern23452172017-03-28 14:28:05 -07006902static void __addrconf_sysctl_unregister(struct net *net,
6903 struct ipv6_devconf *p, int ifindex)
Pavel Emelyanov408c4762008-01-10 17:41:21 -08006904{
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006905 struct ctl_table *table;
Pavel Emelyanov408c4762008-01-10 17:41:21 -08006906
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006907 if (!p->sysctl_header)
Pavel Emelyanov408c4762008-01-10 17:41:21 -08006908 return;
6909
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006910 table = p->sysctl_header->ctl_table_arg;
6911 unregister_net_sysctl_table(p->sysctl_header);
6912 p->sysctl_header = NULL;
6913 kfree(table);
David Ahern23452172017-03-28 14:28:05 -07006914
6915 inet6_netconf_notify_devconf(net, RTM_DELNETCONF, 0, ifindex, NULL);
Pavel Emelyanov408c4762008-01-10 17:41:21 -08006916}
6917
WANG Conga317a2f2014-07-25 15:25:09 -07006918static int addrconf_sysctl_register(struct inet6_dev *idev)
Pavel Emelyanovf52295a2007-12-02 00:58:37 +11006919{
WANG Conga317a2f2014-07-25 15:25:09 -07006920 int err;
6921
6922 if (!sysctl_dev_name_is_allowed(idev->dev->name))
6923 return -EINVAL;
6924
6925 err = neigh_sysctl_register(idev->dev, idev->nd_parms,
6926 &ndisc_ifinfo_sysctl_change);
6927 if (err)
6928 return err;
6929 err = __addrconf_sysctl_register(dev_net(idev->dev), idev->dev->name,
6930 idev, &idev->cnf);
6931 if (err)
6932 neigh_sysctl_unregister(idev->nd_parms);
6933
6934 return err;
Pavel Emelyanovf52295a2007-12-02 00:58:37 +11006935}
6936
Pavel Emelyanov408c4762008-01-10 17:41:21 -08006937static void addrconf_sysctl_unregister(struct inet6_dev *idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006938{
David Ahern23452172017-03-28 14:28:05 -07006939 __addrconf_sysctl_unregister(dev_net(idev->dev), &idev->cnf,
6940 idev->dev->ifindex);
Pavel Emelyanov408c4762008-01-10 17:41:21 -08006941 neigh_sysctl_unregister(idev->nd_parms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006942}
6943
6944
6945#endif
6946
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00006947static int __net_init addrconf_init_net(struct net *net)
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006948{
Hong Zhiguoa79ca222013-03-26 01:52:45 +08006949 int err = -ENOMEM;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006950 struct ipv6_devconf *all, *dflt;
6951
Hong Zhiguoa79ca222013-03-26 01:52:45 +08006952 all = kmemdup(&ipv6_devconf, sizeof(ipv6_devconf), GFP_KERNEL);
Ian Morris63159f22015-03-29 14:00:04 +01006953 if (!all)
Hong Zhiguoa79ca222013-03-26 01:52:45 +08006954 goto err_alloc_all;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006955
Hong Zhiguoa79ca222013-03-26 01:52:45 +08006956 dflt = kmemdup(&ipv6_devconf_dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL);
Ian Morris63159f22015-03-29 14:00:04 +01006957 if (!dflt)
Hong Zhiguoa79ca222013-03-26 01:52:45 +08006958 goto err_alloc_dflt;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006959
Arnd Bergmanna154d5d2019-03-04 21:38:03 +01006960 if (IS_ENABLED(CONFIG_SYSCTL) &&
6961 sysctl_devconf_inherit_init_net == 1 && !net_eq(net, &init_net)) {
Cong Wang856c3952019-01-17 23:27:11 -08006962 memcpy(all, init_net.ipv6.devconf_all, sizeof(ipv6_devconf));
6963 memcpy(dflt, init_net.ipv6.devconf_dflt, sizeof(ipv6_devconf_dflt));
6964 }
6965
Hong Zhiguoa79ca222013-03-26 01:52:45 +08006966 /* these will be inherited by all namespaces */
6967 dflt->autoconf = ipv6_defaults.autoconf;
6968 dflt->disable_ipv6 = ipv6_defaults.disable_ipv6;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006969
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01006970 dflt->stable_secret.initialized = false;
6971 all->stable_secret.initialized = false;
6972
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006973 net->ipv6.devconf_all = all;
6974 net->ipv6.devconf_dflt = dflt;
6975
6976#ifdef CONFIG_SYSCTL
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08006977 err = __addrconf_sysctl_register(net, "all", NULL, all);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006978 if (err < 0)
6979 goto err_reg_all;
6980
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08006981 err = __addrconf_sysctl_register(net, "default", NULL, dflt);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006982 if (err < 0)
6983 goto err_reg_dflt;
6984#endif
6985 return 0;
6986
6987#ifdef CONFIG_SYSCTL
6988err_reg_dflt:
David Ahern23452172017-03-28 14:28:05 -07006989 __addrconf_sysctl_unregister(net, all, NETCONFA_IFINDEX_ALL);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006990err_reg_all:
6991 kfree(dflt);
6992#endif
6993err_alloc_dflt:
6994 kfree(all);
6995err_alloc_all:
6996 return err;
6997}
6998
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00006999static void __net_exit addrconf_exit_net(struct net *net)
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08007000{
7001#ifdef CONFIG_SYSCTL
David Ahern23452172017-03-28 14:28:05 -07007002 __addrconf_sysctl_unregister(net, net->ipv6.devconf_dflt,
7003 NETCONFA_IFINDEX_DEFAULT);
7004 __addrconf_sysctl_unregister(net, net->ipv6.devconf_all,
7005 NETCONFA_IFINDEX_ALL);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08007006#endif
zhuyj73cf0e922014-11-26 10:25:58 +08007007 kfree(net->ipv6.devconf_dflt);
7008 kfree(net->ipv6.devconf_all);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08007009}
7010
7011static struct pernet_operations addrconf_ops = {
7012 .init = addrconf_init_net,
7013 .exit = addrconf_exit_net,
7014};
7015
Daniel Borkmann207895fd32015-01-29 12:15:03 +01007016static struct rtnl_af_ops inet6_ops __read_mostly = {
Thomas Grafb382b192010-11-16 04:33:57 +00007017 .family = AF_INET6,
7018 .fill_link_af = inet6_fill_link_af,
7019 .get_link_af_size = inet6_get_link_af_size,
Daniel Borkmann11b1f822015-02-05 14:39:11 +01007020 .validate_link_af = inet6_validate_link_af,
Daniel Borkmannf53adae2013-04-08 04:01:30 +00007021 .set_link_af = inet6_set_link_af,
Thomas Grafb382b192010-11-16 04:33:57 +00007022};
7023
Linus Torvalds1da177e2005-04-16 15:20:36 -07007024/*
7025 * Init / cleanup code
7026 */
7027
7028int __init addrconf_init(void)
7029{
WANG Conga317a2f2014-07-25 15:25:09 -07007030 struct inet6_dev *idev;
stephen hemmingerc2e21292010-03-17 20:31:10 +00007031 int i, err;
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +09007032
Stephen Hemmingere21e8462010-03-20 16:09:01 -07007033 err = ipv6_addr_label_init();
7034 if (err < 0) {
Joe Perchesf3213832012-05-15 14:11:53 +00007035 pr_crit("%s: cannot initialize default policy table: %d\n",
7036 __func__, err);
Neil Horman2cc6d2b2010-09-24 09:55:52 +00007037 goto out;
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +09007038 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007039
Neil Horman2cc6d2b2010-09-24 09:55:52 +00007040 err = register_pernet_subsys(&addrconf_ops);
7041 if (err < 0)
7042 goto out_addrlabel;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08007043
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01007044 addrconf_wq = create_workqueue("ipv6_addrconf");
7045 if (!addrconf_wq) {
7046 err = -ENOMEM;
7047 goto out_nowq;
7048 }
7049
Linus Torvalds1da177e2005-04-16 15:20:36 -07007050 /* The addrconf netdev notifier requires that loopback_dev
7051 * has it's ipv6 private information allocated and setup
7052 * before it can bring up and give link-local addresses
7053 * to other devices which are up.
7054 *
7055 * Unfortunately, loopback_dev is not necessarily the first
7056 * entry in the global dev_base list of net devices. In fact,
7057 * it is likely to be the very last entry on that list.
7058 * So this causes the notifier registry below to try and
7059 * give link-local addresses to all devices besides loopback_dev
7060 * first, then loopback_dev, which cases all the non-loopback_dev
7061 * devices to fail to get a link-local address.
7062 *
7063 * So, as a temporary fix, allocate the ipv6 structure for
7064 * loopback_dev first by hand.
7065 * Longer term, all of the dependencies ipv6 has upon the loopback
7066 * device and it being up should be removed.
7067 */
7068 rtnl_lock();
WANG Conga317a2f2014-07-25 15:25:09 -07007069 idev = ipv6_add_dev(init_net.loopback_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007070 rtnl_unlock();
WANG Conga317a2f2014-07-25 15:25:09 -07007071 if (IS_ERR(idev)) {
7072 err = PTR_ERR(idev);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08007073 goto errlo;
WANG Conga317a2f2014-07-25 15:25:09 -07007074 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007075
WANG Cong2f460932017-05-03 22:07:31 -07007076 ip6_route_init_special_entries();
7077
stephen hemmingerc2e21292010-03-17 20:31:10 +00007078 for (i = 0; i < IN6_ADDR_HSIZE; i++)
7079 INIT_HLIST_HEAD(&inet6_addr_lst[i]);
7080
Linus Torvalds1da177e2005-04-16 15:20:36 -07007081 register_netdevice_notifier(&ipv6_dev_notf);
7082
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01007083 addrconf_verify();
Thomas Grafc127ea22007-03-22 11:58:32 -07007084
stephen hemminger3678a9d2013-12-30 10:41:32 -08007085 rtnl_af_register(&inet6_ops);
Thomas Grafb382b192010-11-16 04:33:57 +00007086
Florian Westphal16feebc2017-12-02 21:44:08 +01007087 err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETLINK,
7088 NULL, inet6_dump_ifinfo, 0);
Thomas Grafc127ea22007-03-22 11:58:32 -07007089 if (err < 0)
7090 goto errout;
7091
Florian Westphal16feebc2017-12-02 21:44:08 +01007092 err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_NEWADDR,
7093 inet6_rtm_newaddr, NULL, 0);
7094 if (err < 0)
7095 goto errout;
7096 err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_DELADDR,
7097 inet6_rtm_deladdr, NULL, 0);
7098 if (err < 0)
7099 goto errout;
7100 err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETADDR,
7101 inet6_rtm_getaddr, inet6_dump_ifaddr,
7102 RTNL_FLAG_DOIT_UNLOCKED);
7103 if (err < 0)
7104 goto errout;
7105 err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETMULTICAST,
7106 NULL, inet6_dump_ifmcaddr, 0);
7107 if (err < 0)
7108 goto errout;
7109 err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETANYCAST,
7110 NULL, inet6_dump_ifacaddr, 0);
7111 if (err < 0)
7112 goto errout;
7113 err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETNETCONF,
7114 inet6_netconf_get_devconf,
7115 inet6_netconf_dump_devconf,
7116 RTNL_FLAG_DOIT_UNLOCKED);
7117 if (err < 0)
7118 goto errout;
Florian Westphala3fde2a2017-12-04 19:19:18 +01007119 err = ipv6_addr_label_rtnl_register();
7120 if (err < 0)
7121 goto errout;
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +09007122
Linus Torvalds1da177e2005-04-16 15:20:36 -07007123 return 0;
Thomas Grafc127ea22007-03-22 11:58:32 -07007124errout:
Florian Westphal16feebc2017-12-02 21:44:08 +01007125 rtnl_unregister_all(PF_INET6);
Thomas Grafb382b192010-11-16 04:33:57 +00007126 rtnl_af_unregister(&inet6_ops);
Thomas Grafc127ea22007-03-22 11:58:32 -07007127 unregister_netdevice_notifier(&ipv6_dev_notf);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08007128errlo:
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01007129 destroy_workqueue(addrconf_wq);
7130out_nowq:
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08007131 unregister_pernet_subsys(&addrconf_ops);
Neil Horman2cc6d2b2010-09-24 09:55:52 +00007132out_addrlabel:
7133 ipv6_addr_label_cleanup();
7134out:
Thomas Grafc127ea22007-03-22 11:58:32 -07007135 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007136}
7137
Daniel Lezcano09f77092007-12-13 05:34:58 -08007138void addrconf_cleanup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007139{
Daniel Lezcano176c39a2009-03-03 01:06:45 -08007140 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007141 int i;
7142
7143 unregister_netdevice_notifier(&ipv6_dev_notf);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08007144 unregister_pernet_subsys(&addrconf_ops);
Neil Horman2cc6d2b2010-09-24 09:55:52 +00007145 ipv6_addr_label_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007146
Florian Westphal5c451212017-10-04 15:58:49 +02007147 rtnl_af_unregister(&inet6_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007148
Florian Westphal5c451212017-10-04 15:58:49 +02007149 rtnl_lock();
Thomas Grafb382b192010-11-16 04:33:57 +00007150
Daniel Lezcano176c39a2009-03-03 01:06:45 -08007151 /* clean dev list */
7152 for_each_netdev(&init_net, dev) {
7153 if (__in6_dev_get(dev) == NULL)
7154 continue;
7155 addrconf_ifdown(dev, 1);
7156 }
7157 addrconf_ifdown(init_net.loopback_dev, 2);
7158
Linus Torvalds1da177e2005-04-16 15:20:36 -07007159 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07007160 * Check hash table.
7161 */
stephen hemminger5c578aed2010-03-17 20:31:11 +00007162 spin_lock_bh(&addrconf_hash_lock);
stephen hemmingerc2e21292010-03-17 20:31:10 +00007163 for (i = 0; i < IN6_ADDR_HSIZE; i++)
7164 WARN_ON(!hlist_empty(&inet6_addr_lst[i]));
stephen hemminger5c578aed2010-03-17 20:31:11 +00007165 spin_unlock_bh(&addrconf_hash_lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01007166 cancel_delayed_work(&addr_chk_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007167 rtnl_unlock();
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01007168
7169 destroy_workqueue(addrconf_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007170}